nand2tetris / web-ide

A web-based IDE for https://nand2tetris.org
https://nand2tetris.github.io/web-ide
Other
65 stars 17 forks source link

[bug]: Wrong value written to memory for MD=... or AMD=... commands #337

Closed axelkern closed 3 months ago

axelkern commented 3 months ago

Tool

CPU Emulator

Interface

Website (https://nand2tetris.github.io/web-ide)

Contact Details

No response

What happened?

/web-ide/simulator/src/cpu/cpu.ts wrongly updates the D register during the tick. Therefore the following sequence will fail:

@0  
M=1 // init RAM[0]=1  
D=1  
MD=D+1 

D is 2 as expected, but RAM[0] will be 3 !

The CPU simulator acts as follows: cpuTick(): D=D+1 cpuTock(): outM=[updatedD]+1=[originalD]+1+1 // new ALU calculation is triggered Finally tick() writes the wrong outM to the memory

Any computation involving D with destinationD set, will result in the wrong value written to memory. i.e. AMD=D+A, etc.

Additional Comments

No response

Do you want to try to fix this bug?

Code of Conduct

DavidSouther commented 3 months ago

Thank you for the bug report! We've added this test case and a fix in #340.