aboutsummaryrefslogtreecommitdiff
path: root/src/twi.spec.ts
blob: dacd9586fdd0b16e85db29284894188ede7a4c6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { CPU } from './cpu';
import { AVRTWI, twiConfig } from './twi';

const FREQ_16MHZ = 16e6;

describe('TWI', () => {
  it('should correctly calculate the sclFrequency from TWBR', () => {
    const cpu = new CPU(new Uint16Array(1024));
    const twi = new AVRTWI(cpu, twiConfig, FREQ_16MHZ);
    cpu.writeData(0xb8, 0x48); // TWBR <- 0x48
    cpu.writeData(0xb9, 0); // TWSR <- 0 (prescaler: 1)
    expect(twi.sclFrequency).toEqual(100000);
  });

  it('should take the prescaler into consideration when calculating sclFrequency', () => {
    const cpu = new CPU(new Uint16Array(1024));
    const twi = new AVRTWI(cpu, twiConfig, FREQ_16MHZ);
    cpu.writeData(0xb8, 0x03); // TWBR <- 0x03
    cpu.writeData(0xb9, 0x01); // TWSR <- 1 (prescaler: 4)
    expect(twi.sclFrequency).toEqual(400000);
  });
});