|
| 1 | +import { CPU } from './cpu'; |
| 2 | +import { AVRIOPort, portBConfig } from './gpio'; |
| 3 | + |
| 4 | +describe('GPIO', () => { |
| 5 | + it('should invoke the listeners when the port is written to', () => { |
| 6 | + const cpu = new CPU(new Uint16Array(1024)); |
| 7 | + const port = new AVRIOPort(cpu, portBConfig); |
| 8 | + const listener = jest.fn(); |
| 9 | + port.addListener(listener); |
| 10 | + cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f |
| 11 | + cpu.writeData(0x25, 0x55); // PORTB <- 0x55 |
| 12 | + expect(listener).toHaveBeenCalledWith(0x05, 0); |
| 13 | + expect(cpu.data[0x23]).toEqual(0x5); // PINB should return port value |
| 14 | + }); |
| 15 | + |
| 16 | + it('should toggle the pin when writing to the PIN register', () => { |
| 17 | + const cpu = new CPU(new Uint16Array(1024)); |
| 18 | + const port = new AVRIOPort(cpu, portBConfig); |
| 19 | + const listener = jest.fn(); |
| 20 | + port.addListener(listener); |
| 21 | + cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f |
| 22 | + cpu.writeData(0x25, 0x55); // PORTB <- 0x55 |
| 23 | + cpu.writeData(0x23, 0x01); // PINB <- 0x0f |
| 24 | + expect(listener).toHaveBeenCalledWith(0x04, 0x5); |
| 25 | + expect(cpu.data[0x23]).toEqual(0x4); // PINB should return port value |
| 26 | + }); |
| 27 | + |
| 28 | + describe('removeListener', () => { |
| 29 | + it('should remove the given listener', () => { |
| 30 | + const cpu = new CPU(new Uint16Array(1024)); |
| 31 | + const port = new AVRIOPort(cpu, portBConfig); |
| 32 | + const listener = jest.fn(); |
| 33 | + port.addListener(listener); |
| 34 | + cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f |
| 35 | + port.removeListener(listener); |
| 36 | + cpu.writeData(0x25, 0x99); // PORTB <- 0x99 |
| 37 | + expect(listener).not.toHaveBeenCalled(); |
| 38 | + }); |
| 39 | + }); |
| 40 | +}); |
0 commit comments