@@ -213,15 +213,15 @@ export class AVRIOPort {
213213 cpu . writeHooks [ portConfig . DDR ] = ( value : u8 ) => {
214214 const portValue = cpu . data [ portConfig . PORT ] ;
215215 cpu . data [ portConfig . DDR ] = value ;
216- this . updatePinRegister ( portValue , value ) ;
217216 this . writeGpio ( portValue , value ) ;
217+ this . updatePinRegister ( value ) ;
218218 return true ;
219219 } ;
220220 cpu . writeHooks [ portConfig . PORT ] = ( value : u8 ) => {
221221 const ddrMask = cpu . data [ portConfig . DDR ] ;
222222 cpu . data [ portConfig . PORT ] = value ;
223- this . updatePinRegister ( value , ddrMask ) ;
224223 this . writeGpio ( value , ddrMask ) ;
224+ this . updatePinRegister ( ddrMask ) ;
225225 return true ;
226226 } ;
227227 cpu . writeHooks [ portConfig . PIN ] = ( value : u8 ) => {
@@ -230,8 +230,8 @@ export class AVRIOPort {
230230 const ddrMask = cpu . data [ portConfig . DDR ] ;
231231 const portValue = oldPortValue ^ value ;
232232 cpu . data [ portConfig . PORT ] = portValue ;
233- this . updatePinRegister ( portValue , ddrMask ) ;
234233 this . writeGpio ( portValue , ddrMask ) ;
234+ this . updatePinRegister ( ddrMask ) ;
235235 return true ;
236236 } ;
237237 // The following hook is used by the timer compare output to override GPIO pins:
@@ -258,7 +258,9 @@ export class AVRIOPort {
258258 break ;
259259 }
260260 }
261- this . writeGpio ( cpu . data [ portConfig . PORT ] , cpu . data [ portConfig . DDR ] ) ;
261+ const ddrMask = cpu . data [ portConfig . DDR ] ;
262+ this . writeGpio ( cpu . data [ portConfig . PORT ] , ddrMask ) ;
263+ this . updatePinRegister ( ddrMask ) ;
262264 } ;
263265
264266 // External interrupts
@@ -355,11 +357,11 @@ export class AVRIOPort {
355357 if ( value ) {
356358 this . pinValue |= bitMask ;
357359 }
358- this . updatePinRegister ( this . cpu . data [ this . portConfig . PORT ] , this . cpu . data [ this . portConfig . DDR ] ) ;
360+ this . updatePinRegister ( this . cpu . data [ this . portConfig . DDR ] ) ;
359361 }
360362
361- private updatePinRegister ( port : u8 , ddr : u8 ) {
362- const newPin = ( this . pinValue & ~ ddr ) | ( port & ddr ) ;
363+ private updatePinRegister ( ddr : u8 ) {
364+ const newPin = ( this . pinValue & ~ ddr ) | ( this . lastValue & ddr ) ;
363365 this . cpu . data [ this . portConfig . PIN ] = newPin ;
364366 if ( this . lastPin !== newPin ) {
365367 for ( let index = 0 ; index < 8 ; index ++ ) {
0 commit comments