From a367ebb0e3cb1b8325d4a0e6a8d616238852a7d4 Mon Sep 17 00:00:00 2001 From: Jakob Gruber Date: Wed, 29 Aug 2012 14:16:59 +0200 Subject: [PATCH] ioport: Restore PIN to PORT values when DDR is set to output --- simavr/sim/avr_ioport.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/simavr/sim/avr_ioport.c b/simavr/sim/avr_ioport.c index 6e0966e..24a8a9b 100644 --- a/simavr/sim/avr_ioport.c +++ b/simavr/sim/avr_ioport.c @@ -61,7 +61,8 @@ static void avr_ioport_pin_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t /* * This is a the callback for the DDR register. There is nothing much to do here, apart - * from triggering an IRQ in case any 'client' code is interested in the information. + * from triggering an IRQ in case any 'client' code is interested in the information, + * and restoring all PIN bits marked as output to PORT values. */ static void avr_ioport_ddr_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param) { @@ -69,6 +70,14 @@ static void avr_ioport_ddr_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t avr_raise_irq(p->io.irq + IOPORT_IRQ_DIRECTION_ALL, v); avr_core_watch_write(avr, addr, v); + + const uint8_t oldpin = avr->data[p->r_pin]; + const uint8_t pin = (oldpin & ~v) | (avr->data[p->r_port] & v); + avr_core_watch_write(avr, p->r_pin, pin); + for (int i = 0; i < 8; i++) + if (((oldpin ^ pin) >> i) & 1) + avr_raise_irq(p->io.irq + i, (pin >> i) & 1); + avr_raise_irq(p->io.irq + IOPORT_IRQ_PIN_ALL, pin); } /* -- 2.39.5