tags:

views:

40

answers:

1

This is/was for assignment four of From NAND to Tetris, and OCW from Hebrew University (I don't attend, I'm just doing the course for kicks). What it does is, when any key is held down, it blackens out every pixel. When the key is released, the "screen" is cleared.

Here's the ROM itself:

// Fill.asm

// Fills the 'screen' with black,
// one pixel at a time, when any 
// key is pressed and held.

// When the key is released, the 
// screen should clear.


// i = 0

@i
M=0

(LOOP)
 // color = 0xFFFF (black) = !0
 D=0
 D=!D
 @color
 M=D

 @KBD        // Location of keypress code
     // is RAM[24576] = RAM(0x600)
 D=M         // D != 0 if a key is pressed

 @SKIP_WHITE
 D;JNE


 // if (key == 0 )
 // {
 //   color = 0 (white)

 @color
 M=0

 // }


(SKIP_WHITE)
 // Ensure that the offset is 
 // within the screen's 8K
 // memory area
        @i
 M=M+1
 @8191
 D=A
 @i
 M=M&D


 @i
 D=M
 @SCREEN
 D=D+A     
 @target   
 M=D
 @color
 D=M
 @target   
 A=M      // GOTO Pixel specified by target
 M=D


 @LOOP // infinite loop, baby
 0;JMP

What I'm really curious about is the second to last block of code, the one about the offset of the screen's memory area. I wrote everything else, but the program didn't work until someone supplied me that block of code. Can anyone explain to me what it's doing?

A: 

I figured it out!

8191 is the maximum number of bits within the virtual screen's 8K memory area. By ANDing the iterator with the maximum value, we ensure that we do not try to write to pixels outside of the screen.

Andrew