// 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/4/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. When no key is pressed,
// the screen should be cleared.
//
// Hack assembly SCREEN input storage starts at RAM[16384] is 8192 words(16bit arrays) long. Actually an 8k RAM
// So from RAM[16384] to RAM[16384+8192](24575) is the screen memory
// Hack has no single bit manipulation so when changing one word, it is actually 16pixels wide.
// Screen RAM[16384] can be accessed at @SCREEN
//
// Keyboard starts at RAM[24576] and is 1 word long. ASCII Key letters. Is empty if no key is pressed and has the value of the pressed down key.
// Keyboard can be accessed @KBD
//
// Replace this comment with your code.
//
// Pseudo code
// Loop indefinetely
//
// Listen for key press on RAM[24576]
// loops from @SCREEN to RAM[24575] and changes all values to 1/0 all the time
// If any value in that address paint paint all pixels black.
// This is done by first checking if RAM[24576] is bigger than 0
// If it is jump to another loop that
//
//
// Declare Variables
// @isBlack = 0/1
// 8192 loop length @loopLength
// screenStart @SCREEN
// screenEnd @SCREEN+8192

// Variables


// LOOP
(LOOP)
    @isBlack
    M=0
    @loopedScreen
    M=0
    @SCREEN
    D=A
    @paintStartAddr
    M=D
    // Check Keyboard
    @KBD
    D=M
    @KEY_PRESSED
    D;JGT
    @PAINT_SCREEN
    0;JMP

(KEY_PRESSED)
    @isBlack
    M=-1 // set isBlack to 1
    @PAINT_SCREEN
    0;JMP
(KEY_NOT_PRESSED)
    @isBlack
    M=1 // set isBlack to 0
    @PAINT_SCREEN
    0;JMP
(PAINT_SCREEN)
    @isBlack
    D=M
    // Paint all of screen to isBlack
    @paintStartAddr // RAM[16385] first time around
    A=M // Point the A Register to value of
    M=D
    @paintStartAddr
    M=M+1      // paintStartAddr = 16385
    @loopedScreen
    MD=M+1
    @8192 // To see where we are.
    D=A-D // If we are in Screen range, keep painting
    @PAINT_SCREEN
    D;JGT
    // End Jumpo to top infinite loop.
    @LOOP
    0;JMP
