Hardware Simulator Keyboard Repeat Bug

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Hardware Simulator Keyboard Repeat Bug

brianshmrian
This post was updated on .
I think there's a bug involving the HardwareSimulator and keyboard input.  I discovered this while trying to test my Tetris program all the way down on the HardwareSimulator.  The program would run perfectly fine until I started holding down keys, at which point various screen garbage or halting errors would occur.  I kept thinking that I must have a problem in my CPU/Memory/Computer implementation since everything worked fine in the VMEmulator and CPUEmulator.

To investigate more thoroughly, I created a simplified version of the program in which one block is simply falling down and responds to keyboard input.  I also changed the OS Output.init function to do nothing since this function takes millions of clock cycles to complete and slows down testing.

It doesn't matter whether I call the Keyboard.keyPressed() function or not.  And it doesn't matter whether I hook up the Keyboard out pins to the Memory out pins in the Memory.hdl file.  In one test I placed a right-arrow key into keyboard memory like this:

CHIP Memory {
    IN in[16], load, address[15];
    OUT out[16];

    PARTS:
        DMux4Way(sel[1]=address[14],sel[0]=address[13],in=load,a=dataload1,b=dataload2,c=displayload);
        Or(a=dataload1,b=dataload2,out=dataload);
       
        RAM16K(in=in,address=address[0..13],load=dataload,out=dataout);
        Screen(in=in,address=address[0..12],load=displayload,out=displayout);
        Keyboard(out=nowhere);
        Or16(a[0..1]=false,a[2]=true,a[3..6]=false,a[7]=true,a[8..15]=false,b=false,out=keyboardout);
       
        Mux4Way16(sel[1]=address[14],sel[0]=address[13],a=dataout,b=dataout,c=displayout,d=keyboardout,out=out);
}

In every case, everything works fine until I start holding down a key in the HardwareSimulator.  After a few seconds of holding down a key, things get corrupted in semi-random ways and the program stops functioning correctly.  To be clear, in the case where my Memory chip is always outputting a right-arrow (132) as the keyboard output, the square happily moves to the right _until_ I start holding down the right-arrow key myself, at which point, after a few seconds, some sort of of corruption occurs.

Anyway, I thought I'd post here to maybe save some other people from going crazy wondering why their interactive programs aren't working in the HardwareSimulator.  Although it's probably too late to save them since they're already crazy to be trying it in the first place :-)

My machine is:

Windows 7 64-bit
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) Client VM (build 25.151-b12, mixed mode, sharing)

I'm running these tests on version 2.6.2 of the tools from here:

https://github.com/itoshkov/nand2tetris-emu/releases


I think these posts are related to the problem:

http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Keyboard-input-not-being-recognized-td4030093.html

http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Fill-asm-random-whitespace-td2739375.html#a2743055

Here are some screenshots of my test program...

A happy square:


The happy square happily moves to the right without me physically pressing a key (because I'm inserting the right-arrow key directly into the keyboard output in Memory.hdl):


But when I physically hold down the right-arrow key for a while, the happy square gets sad:

Reply | Threaded
Open this post in threaded view
|

Re: Hardware Simulator Keyboard Repeat Bug

brianshmrian
I just thought to test this bug on the "Square" program from project 09, and it does indeed happen for me.  Again, it's much faster to test if you change the top lines of Output.vm to the following before translating the vm code to assembly and then assembling to hack:

function Output.init 0
return
function Output.initold 0


Sad square dance: