Showing posts with label Nand2Tetris. Show all posts
Showing posts with label Nand2Tetris. Show all posts

Tuesday, June 11, 2013

From Nand to Tetris - My Solutions to the Projects


I have created a repository to contain my solutions to the projects of the course "From Nand to Tetris"
(You can also get the reading materials and projec files at http://www.cs.huji.ac.il/course/2002/nand2tet/projects.html)

Here is the repository of my solutions: https://github.com/jboyflaga/Nand2Tetris
(Some of my solutions are not very good even though the output is still correct. Be sure to also look at other people's solutions for reference.)

UPDATE (November 17, 2013): I deleted the GitHub repository of my solutions to the Nand2Tetris projects 1 to 8 because someone who is teaching it (Alex Lipov) requested for it to be removed because it violated the course terms (http://www.nand2tetris.org/terms.php).

If you want to know how to start the course just read the whole Chapter 1 available in http://www.nand2tetris.org/course.php or http://www.cs.huji.ac.il/course/2002/nand2tet/projects/proj1.html. You will know what to do especially after you read the last part of chapter 1.

Enjoy!!!

Friday, May 31, 2013

From Nand to Tetris - Project 4 (Fill)

This is my solution to the exercises (Fill) in Project 4 of the free course From Nand to Tetris
Other Links:
http://www.cs.huji.ac.il/course/2002/nand2tet/projects.html
// 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
// program clears the screen, i.e. writes "white" in every pixel.

// (RESET_SCREEN_COUNTER)
 // @SCREEN
 // D=A
 // @counter
 // M=D    // counter = SCREEN

(LISTEN_FOR_NO_KEY_PRESS)
 // reset screen counter
 @SCREEN
 D=A
 @counter
 M=D
 
 // if KBD == 0 jump to CLEAR_SCREEN
 @KBD
 D=M
 @CLEAR_SCREEN
 D;JEQ

 // loop (keep on checking if KBD == 0
 @LISTEN_FOR_NO_KEY_PRESS
 0;JEQ

(LISTEN_FOR_KEY_PRESS)
 // reset screen counter
 @SCREEN
 D=A
 @counter
 M=D
 
 // if KBD > 0 jump to BLACKEN_SCREEN
 @KBD
 D=M
 @BLACKEN_SCREEN
 D;JGT
 
 // loop (keep on checking if KBD > 0
 @LISTEN_FOR_KEY_PRESS
 0;JEQ
 
(BLACKEN_SCREEN) 
 // blacken 16-bit memory location
 @counter
 A=M    // A = counter
 M=-1   // blacken current memory location
  
 // if KBD == 0 jump to LISTEN_FOR_NO_KEY_PRESS (to reset counter)
 @KBD
 D=M
 @LISTEN_FOR_NO_KEY_PRESS
 D;JEQ

 // increment counter
 @counter
 M=M+1
 
 // check if you have reached memory location 24575 (the final address for the screen)
 D=M
 @24575
 D=A-D
 @LISTEN_FOR_NO_KEY_PRESS
 D;JLT   // goto LISTEN_FOR_NO_KEY_PRESS if counter > 24575
 
 // loop BLACKEN_SCREEN if you have NOT reached memory location 24575
 @BLACKEN_SCREEN
 0;JEQ 

(CLEAR_SCREEN)
 // clear 16-bit memory location
 @counter
 A=M    // A = counter
 M=0    // clear current memory location
 
 // if KBD > 0 jump to LISTEN_FOR_KEY_PRESS (to reset counter)
 @KBD
 D=M
 @LISTEN_FOR_KEY_PRESS
 D;JGT
 
 // increment counter
 @counter
 M=M+1
 
 // check if you have reached memory location 24575 (the final address for the screen)
 D=M
 @24575
 D=A-D
 @LISTEN_FOR_KEY_PRESS
 D;JLT   // goto LISTEN_FOR_KEY_PRESS if counter > 24575
 
 // loop CLEAR_SCREEN if you have NOT reached memory location 24575
 @CLEAR_SCREEN
 0;JEQ