From 958691d82600406110b49b41b5ca11d4c4597a00 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Thu, 2 Apr 2020 13:18:05 +0300 Subject: fix: GPIO port listeners not invoked when writing to DDR registers close #28 --- src/peripherals/gpio.spec.ts | 16 +++++++++++++--- src/peripherals/gpio.ts | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/peripherals/gpio.spec.ts b/src/peripherals/gpio.spec.ts index 5a282e6..f3b1a0f 100644 --- a/src/peripherals/gpio.spec.ts +++ b/src/peripherals/gpio.spec.ts @@ -6,13 +6,23 @@ describe('GPIO', () => { const cpu = new CPU(new Uint16Array(1024)); const port = new AVRIOPort(cpu, portBConfig); const listener = jest.fn(); - port.addListener(listener); cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f + port.addListener(listener); cpu.writeData(0x25, 0x55); // PORTB <- 0x55 expect(listener).toHaveBeenCalledWith(0x05, 0); expect(cpu.data[0x23]).toEqual(0x5); // PINB should return port value }); + it('should invoke the listeners when DDR changes (issue #28)', () => { + const cpu = new CPU(new Uint16Array(1024)); + const port = new AVRIOPort(cpu, portBConfig); + const listener = jest.fn(); + cpu.writeData(0x25, 0x55); // PORTB <- 0x55 + port.addListener(listener); + cpu.writeData(0x24, 0xf0); // DDRB <- 0xf0 + expect(listener).toHaveBeenCalledWith(0x50, 0); + }); + it('should toggle the pin when writing to the PIN register', () => { const cpu = new CPU(new Uint16Array(1024)); const port = new AVRIOPort(cpu, portBConfig); @@ -34,7 +44,7 @@ describe('GPIO', () => { cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f port.removeListener(listener); cpu.writeData(0x25, 0x99); // PORTB <- 0x99 - expect(listener).not.toHaveBeenCalled(); + expect(listener).toBeCalledTimes(1); }); }); @@ -76,9 +86,9 @@ describe('GPIO', () => { const listener = jest.fn(() => { expect(port.pinState(0)).toBe(PinState.High); }); - port.addListener(listener); expect(port.pinState(0)).toBe(PinState.Input); cpu.writeData(0x24, 0x01); // DDRB <- 0x01 + port.addListener(listener); cpu.writeData(0x25, 0x01); // PORTB <- 0x01 expect(listener).toHaveBeenCalled(); }); diff --git a/src/peripherals/gpio.ts b/src/peripherals/gpio.ts index a667967..35030b8 100644 --- a/src/peripherals/gpio.ts +++ b/src/peripherals/gpio.ts @@ -94,6 +94,10 @@ export class AVRIOPort { private listeners: GPIOListener[] = []; constructor(private cpu: CPU, private portConfig: AVRPortConfig) { + cpu.writeHooks[portConfig.DDR] = (value, oldValue) => { + const portValue = cpu.data[portConfig.PORT]; + this.writeGpio(value & portValue, oldValue & oldValue); + }; cpu.writeHooks[portConfig.PORT] = (value: u8, oldValue: u8) => { const ddrMask = cpu.data[portConfig.DDR]; cpu.data[portConfig.PORT] = value; -- cgit v1.2.3