Commands for reading keyboard/writing to screen?

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

Commands for reading keyboard/writing to screen?

Magerman
This post was updated on .
I've been a little frustrated trying to code the Fill program exercise at the end of Ch. 4. Specifically, I'm pretty sure I understand what should be happening, but I am at a loss for how to tell the computer what I want to do via code.

So, the general idea is to have an infinite loop running that listens to keyboard inputs and makes the simulated screen either pure black (if any key is pressed) or pure white (if nothing is pressed). The book specifically says that whenever a key on the keyboard is pressed, its ASCII code appears in RAM[24576], and when no key is pressed that location defaults to the output of 0. Alright, so it seems like we basically need a statement that goes to (loop that makes screen all white) if RAM[24576]=0, and which goes to (loop that makes screen all black) if RAM[24576] has an ASCII code in it (i.e. it is anything other than 0). Seems simple enough.

The problem is that this statement the book makes doesn't really appear to be true? Or, at any rate, when I compile and run a program in the CPU Emulator, RAM location 24576 is literally always 0, even if a key is pressed and held down (Edit: to clarify, these keys are pressed after activating the CPU Emulator's keyboard function). I pressed just about every key on the keyboard and it never changed. So...how exactly is the program supposed to listen to the keyboard?

I looked all throughout the chapter and even in the appendix, but there doesn't seem to be any concrete info on the I/O commands for the assembly language; literally all I can find is that RAM address which doesn't appear to work. It doesn't help that the only example code that features I/O referencing might as well not be there at all, as it doesn't appear to work on its own...

Can anyone tell me how to listen to the keyboard, or at least point me to the place in the book where it's explained? Is there some special way to code the loops that's different from the other Ch. 4 exercise; like do you need to do something special for continuous listening other than just making it loop infinitely? I will probably need help in writing to the screen as well...I think I understand it from reading the chapter (you basically need to write a loop that darkens one "word's" worth of pixels at a time until the whole screen is black, using the RAM location formula given in the chapter to get them all, as I understand it), but I thought I understood how to read the keyboard input too, so I wouldn't be surprised if I was missing something there too =(
Reply | Threaded
Open this post in threaded view
|

Re: Commands for reading keyboard/writing to screen?

WBahn
Administrator
I'm not on a machine that has the tools installed, so I'm going from memory.

I believe the CPU Emulator has a button below the screen with an icon of a keyboard on it that enables putting character codes from the keyboard into the RAM location. Try to look for something like that.
Reply | Threaded
Open this post in threaded view
|

Re: Commands for reading keyboard/writing to screen?

ivant
WBahn wrote
I'm not on a machine that has the tools installed, so I'm going from memory.

I believe the CPU Emulator has a button below the screen with an icon of a keyboard on it that enables putting character codes from the keyboard into the RAM location. Try to look for something like that.
That's right, pressing the keyboard button with your mouse makes further key-presses to be handled by the emulator. You can try this, by running the CPU Emulator without any program, then just scroll down the RAM list to the last position, which is where the keyboard is mapped to. Then click on the icon and start pressing keys.

Another thing to note is, you should move the speed slider to "Fast" and change the animation to "No animation" when running interactive programs.
Reply | Threaded
Open this post in threaded view
|

Re: Commands for reading keyboard/writing to screen?

Magerman
Yes, I've done all of those things. Moved the slider to Fast, executed the program (both the .hack and the .tst versions in case it only worked with one of them), clicked the keyboard icon, and pushed keys on my keyboard (they appeared in the CPU emulator display so I'm pretty sure I did it right), but RAM address 24576 never changes, it's always 0 even when I'm mashing keys/holding keys down after activating the keyboard part of the emulator =(
Reply | Threaded
Open this post in threaded view
|

Re: Commands for reading keyboard/writing to screen?

WBahn
Administrator
Do you have the mode set to Animate?

If you have no animation, then you won't see values change in the RAM or the machine internals -- that's the whole point of animating them.
Reply | Threaded
Open this post in threaded view
|

Re: Commands for reading keyboard/writing to screen?

xedover
In reply to this post by Magerman
yes, you'll want to create a loop to listen for a keypress

if the memory address @KBD has any value other than 0, then a key is being pressed. So you can test the value at that memory address and jump if equal to zero one direction, or another direction if not equal to zero. But you'll need to continually loop and check the value.
Reply | Threaded
Open this post in threaded view
|

Re: Commands for reading keyboard/writing to screen?

Magerman
I think I solved the issue: I did indeed have "Animate" set to "No Animation." I had initially checked what the code looked like when the animation was set to "Program Flow" and it never progressed passed the first loop I programmed in, so I thought there must have still been something wrong even though I wasn't able to scroll down and check the output of the keyboard location. After a little more investigation though I found out that that was because I had failed to have it properly re-reference any new inputs to the keyboard within that loop.

It appears to be working now, thanks for the help everyone =)