diff options
| author | Uri Shaked | 2020-05-29 11:23:49 +0300 |
|---|---|---|
| committer | Uri Shaked | 2020-05-29 11:23:49 +0300 |
| commit | db422fab7e38e769a164a67221634c9bfc77ee60 (patch) | |
| tree | f9bfb488e58d95e46d99d149df8505d358867bc7 /src | |
| parent | chore(deps): @wokwi/elements 0.16.1 (diff) | |
| download | avr8js-db422fab7e38e769a164a67221634c9bfc77ee60.tar.gz avr8js-db422fab7e38e769a164a67221634c9bfc77ee60.tar.bz2 avr8js-db422fab7e38e769a164a67221634c9bfc77ee60.zip | |
fix(gpio): port state not updated on DDR write
Calling `pinState()` inside a GPIO port listener returns incorrect values after changing DDR
close #47
Diffstat (limited to '')
| -rw-r--r-- | src/peripherals/gpio.spec.ts | 13 | ||||
| -rw-r--r-- | src/peripherals/gpio.ts | 2 |
2 files changed, 15 insertions, 0 deletions
diff --git a/src/peripherals/gpio.spec.ts b/src/peripherals/gpio.spec.ts index 1dfaf7d..44232f4 100644 --- a/src/peripherals/gpio.spec.ts +++ b/src/peripherals/gpio.spec.ts @@ -92,6 +92,19 @@ describe('GPIO', () => { cpu.writeData(0x25, 0x01); // PORTB <- 0x01 expect(listener).toHaveBeenCalled(); }); + + it('should reflect the current port state when called inside a listener after DDR change', () => { + // Related issue: https://github.com/wokwi/avr8js/issues/47 + const cpu = new CPU(new Uint16Array(1024)); + const port = new AVRIOPort(cpu, portBConfig); + const listener = jest.fn(() => { + expect(port.pinState(0)).toBe(PinState.Low); + }); + expect(port.pinState(0)).toBe(PinState.Input); + port.addListener(listener); + cpu.writeData(0x24, 0x01); // DDRB <- 0x01 + expect(listener).toHaveBeenCalled(); + }); }); describe('setPin', () => { diff --git a/src/peripherals/gpio.ts b/src/peripherals/gpio.ts index cb21669..e0861d7 100644 --- a/src/peripherals/gpio.ts +++ b/src/peripherals/gpio.ts @@ -110,8 +110,10 @@ export class AVRIOPort { constructor(private cpu: CPU, private portConfig: AVRPortConfig) { cpu.writeHooks[portConfig.DDR] = (value: u8) => { const portValue = cpu.data[portConfig.PORT]; + cpu.data[portConfig.DDR] = value; this.updatePinRegister(portValue, value); this.writeGpio(portValue, value); + return true; }; cpu.writeHooks[portConfig.PORT] = (value: u8) => { const ddrMask = cpu.data[portConfig.DDR]; |
