2020-05-19 12:42:52 +00:00
|
|
|
// This file is part of www.nand2tetris.org
|
|
|
|
// and the book "The Elements of Computing Systems"
|
|
|
|
// by Nisan and Schocken, MIT Press.
|
|
|
|
// File name: projects/04/Fill.asm
|
|
|
|
|
|
|
|
// Runs an infinite loop that listens to the keyboard input.
|
|
|
|
// When a key is pressed (any key), the program blackens the screen,
|
|
|
|
// i.e. writes "black" in every pixel;
|
2020-05-28 10:29:49 +00:00
|
|
|
// the screen should remain fully black as long as the key is pressed.
|
2020-05-19 12:42:52 +00:00
|
|
|
// When no key is pressed, the program clears the screen, i.e. writes
|
|
|
|
// "white" in every pixel;
|
|
|
|
// the screen should remain fully clear as long as no key is pressed.
|
|
|
|
|
2020-05-28 11:15:38 +00:00
|
|
|
// See NOTES.md for details on how this works
|
|
|
|
|
|
|
|
(PROGRAM_START)
|
|
|
|
// Set R0 to SCREEN
|
|
|
|
@SCREEN
|
2020-05-28 10:29:49 +00:00
|
|
|
D=A
|
2020-05-28 11:15:38 +00:00
|
|
|
@R0
|
2020-05-28 10:29:49 +00:00
|
|
|
M=D
|
|
|
|
|
|
|
|
(KEYBOARD_LOOP)
|
|
|
|
|
|
|
|
// Read the Keyboard and set color
|
|
|
|
@color
|
|
|
|
M=0
|
|
|
|
@KBD
|
|
|
|
D=M
|
|
|
|
@ENDKBDIF
|
|
|
|
D;JEQ
|
|
|
|
// If keyboard is pressed
|
|
|
|
@0
|
|
|
|
D=A
|
|
|
|
D=D-1
|
|
|
|
@color
|
|
|
|
M=D
|
|
|
|
(ENDKBDIF)
|
|
|
|
|
|
|
|
// At this point @color = 0|-1 depending on whether a key is pressed
|
|
|
|
|
|
|
|
// coloring code goes here
|
|
|
|
|
2020-05-28 11:15:38 +00:00
|
|
|
// Read color to D
|
2020-05-28 10:29:49 +00:00
|
|
|
|
|
|
|
@color
|
|
|
|
D=M
|
|
|
|
|
2020-05-28 11:15:38 +00:00
|
|
|
// Load the value in R0 to A
|
|
|
|
@R0
|
|
|
|
A=M
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:00
|
|
|
// If R0-24575 <= 0, then jump to program start
|
|
|
|
@24575
|
|
|
|
D=A
|
|
|
|
@R0
|
|
|
|
D=M-D // (D=R0-24575)
|
2020-05-28 10:29:49 +00:00
|
|
|
@ENDIF
|
2020-05-28 11:15:38 +00:00
|
|
|
D;JLE
|
2020-05-28 10:29:49 +00:00
|
|
|
|
2020-05-28 11:15:38 +00:00
|
|
|
// Here, R1 is negative
|
|
|
|
// set R0=@SCREEN and let the loop continue
|
|
|
|
@SCREEN
|
2020-05-28 10:29:49 +00:00
|
|
|
D=A
|
2020-05-28 11:15:38 +00:00
|
|
|
@R0
|
2020-05-28 10:29:49 +00:00
|
|
|
M=D
|
|
|
|
|
|
|
|
|
2020-05-28 11:15:38 +00:00
|
|
|
(ENDIF)
|
2020-05-28 10:29:49 +00:00
|
|
|
|
2020-05-28 11:15:38 +00:00
|
|
|
// OTHERWISE, we just restart the keyboard loop
|
2020-05-28 10:29:49 +00:00
|
|
|
@KEYBOARD_LOOP
|
|
|
|
0;JMP
|