How to install Nand2Tetris on macOS ( including Apple M1) for Computer Systems course
This tutorial will show you the instructions to set up the environment for Nand2Tetris and get Nand2Tetris installed properly.
One command to get everything ready
/bin/sh -c "$(curl -fsSL http://tiny.cc/2gmlsz)" && test "$SHELL" = "/bin/zsh" && source ~/.zshrc ; test "$SHELL" = "/bin/bash" && source ~/.bashrc
If you prefer to install Nand2Tetris step by step, just follow the rest of this tutorial.
Check your environment
Verify if you have java installed on your computer:
In the same terminal, type the following command:
java -version
# If you can see the output below, openjdk version "11.0.10" 2021-01-19OpenJDK Runtime Environment (build 11.0.10+9)OpenJDK 64-Bit Server VM (build 11.0.10+9, mixed mode)
If you can see java version
or openjdk version
then the java has been installed already. Otherwise, there is no java on your computer. Then you have to follow the instructions to install Java.
Step 1, install brew — package manager for macOS
In the terminal window copy and paste the following command to install brew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Step 2, install Java
Java 11 is one of most popular version and it is LTS version. So, we use it here.
brew install java11
It may takes a while to get Java installed. Once it has done, run the following command again to see if java has been installed properly.
java -version
Step 3, Download and set up nand2tetris
This version was released by the University of Adelaide, which includes some patches for fixing some issues, and it can be used for the similar courses of other universities
cd $HOME
wget https://raw.githubusercontent.com/wxw-matt/nand2tetris-macos/master/csadelaide-nand2tetris.zip
unzip csadelaide-nand2tetris.zip
cd csadelaide-nand2tetris
chmod +x tools/*.sh
Step 4, Test Nand2Tetris
Run Hardware Simulator:
$HOME/csadelaide-nand2tetris/tools/HardwareSimulator.sh
Run Jack Compiler:
$HOME/csadelaide-nand2tetris/tools/JackCompiler.sh
$HOME/csadelaide-nand2tetris/projects/09/Square
Other commands you can use:
$HOME/csadelaide-nand2tetris/tools/Assembler.sh
$HOME/csadelaide-nand2tetris/tools/CPUEmulator.sh
$HOME/csadelaide-nand2tetris/tools/TextComparer.sh
$HOME/csadelaide-nand2tetris/tools/VMEmulator.sh
Step 5, Add your path to zshrc or bashrc
Check your shell by typing echo $SHELL
in the terminal window, you are most likely to see
/bin/zsh
or
/bin/bash
If you are using zsh, use the following command to add your path to zshrc then reload zshrc:
echo 'export PATH="$HOME/csadelaide-nand2tetris/tools:$PATH"' >> $HOME/.zshrc
source $HOME/.zshrc
If you are using bash, use the following command to add your path to bashrc and reload it:
echo 'export PATH="$HOME/csadelaide-nand2tetris/tools:$PATH"' >> $HOME/.bashrc
source $HOME/.bashrc
Conclusion
As macOS now does not has Java installed by default, we must install Java first. To make Nand2tetris accessible from terminal, we need to assign executable permissions to some files and add them to PATH
,which we did in step 5.
After add your path to environment variable PATH, you can simply type CPUEmulator.sh
in the terminal to launch CPU Emulator program.
If you have any questions, please open an issues or email me.