Skip to content

Implementation of a complete computer from Nand gates. <The Elements of Computing Systems>

Notifications You must be signed in to change notification settings

jacepark12/Nand2Tetris

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nand2Tetris

Completed Projects

  • 1. Boolean Logic
  • 2. Boolean Arithmetic
  • 3. Sequential Logic
  • 4. Machine Language
  • 5. Computer Architecture
  • 6. Assembler
  • 7. Virtual Machine I: Stack Arithmetic
  • 8. Virtual Machine II: ProgramControl
  • 9. High-Level Language
  • 10. Compiler I: Syntax Analys
  • 11. Compiler II: Code Generation
  • 12. Operating System

Chapter 1. Boolean Logic

  • Xor
  • Not16
  • Mux
  • DMux
  • Mux16
  • Mux4Way16

Chapter 2. Boolean Arithmetic

  • HalfAdder
  • FullAdder
  • Inc16
  • ALU

Chapter 3. Sequential Logic

  • 1 Bit register
  • 16 Bit register
  • RAM8
  • PC

Chapter 4. Machine Language

  • Mult.asm (Multiplication Program)
  • Fill.asm (I/O-Handling Program)

Chapter 5. Computer Architecture

  • Memory
  • 16-bit 6-opcode CPU
  • Computer Chip

Chapter 6. Assembler

  • Assembler (python)
    • Add.asm
    • Max.asm

Chapter. 7 Virtual Machine I: Stack Arithmetic

  • VM-Translator (python)
    • SimpleAdd.vm
    • StackTest.vm
    • BasicTest.vm
    • PointerTest.vm
    • StaticTest.vm

Chapter 8. Virtual Machine II: ProgramControl

Marker feature for easy debugging

class CommandMarker:
def __init__(self, func):
self.func = func
def __get__(self, instance, owner):
def wrapper(*args, **kwargs):
command_string = " ".join([str(x) for x in args])
if instance.debug_mode:
instance.output_lines.append(f"// start of [{command_string}]\n")
result = self.func(instance, *args, **kwargs)
if instance.debug_mode:
instance.output_lines.append(f"// end of [{command_string}]\n")
return result
return wrapper

  • Command marker decorator inserts comment while writing asm code.
// start of [push constant 10]
@10
D=A
@SP
A=M
M=D
@SP
M=M+1
// end of [push constant 10]
  • VM-Translator (python)
    • ProgramFlow/BasicLoop
    • ProgramFlow/FibonacciSeries
    • FunctionCalls/SimpleFunction
    • FunctionCalls/FibonacciElement
    • FunctionCalls/StaticsTest

Chapter 10. Compiler I: Syntax Analys

  • JackAnalyzer

Chapter 11. Compiler II: Code Generation

Class definition for rule define

class RuleElement:
def __init__(self, rule_element_type: RuleElementType, desc: str):
self.element_type = rule_element_type
self.desc = desc
def compare_with_token(self, token: str) -> bool:
if self.element_type == RuleElementType.FIXED_TERMINAL:
return token == self.desc
elif self.element_type == RuleElementType.VAR_TERMINAL:
return True
raise NotImplementedError("Need to implement")
# ex) classVarDec*
class MultipleRuleElement(RuleElement):
def __init__(self, desc: str, ref: Union[RuleElement, Callable]):
super().__init__(rule_element_type=RuleElementType.MULTIPLE, desc=desc)
self.ref = ref
class ZeroOneRuleElement(RuleElement):
def __init__(self, desc: str, ref: Union[RuleElement, Callable]):
super().__init__(rule_element_type=RuleElementType.ZERO_OR_ONE, desc=desc)
self.ref = ref

Example rule definition for parser

TYPE_RULE_ELEMENTS = RefRuleElement(
desc="type",
ref=OrRuleElement(
desc="'int' | 'char' | 'boolean' | className",
or_elements=[
RuleElement(RuleElementType.FIXED_TERMINAL, "int"),
RuleElement(RuleElementType.FIXED_TERMINAL, "char"),
RuleElement(RuleElementType.FIXED_TERMINAL, "boolean"),
RuleElement(RuleElementType.VAR_TERMINAL, "className"),
],
),
)
KEYWORD_CONSTANT_RULE_ELEMENTS = RefRuleElement(
desc="keywordConstant",
ref=OrRuleElement(
desc="'true' | 'false' | 'null' | 'this'",
or_elements=[
RuleElement(RuleElementType.FIXED_TERMINAL, "true"),
RuleElement(RuleElementType.FIXED_TERMINAL, "false"),
RuleElement(RuleElementType.FIXED_TERMINAL, "null"),
RuleElement(RuleElementType.FIXED_TERMINAL, "this"),
],
),
)

Rule processing implementation

def process_rule_element(
self, rule_element: RuleElement
) -> Optional[List[TreeElement]]:
token_type = self.tokenizer.token_type()
token_value = self.tokenizer.current_token.value
if rule_element.element_type in [
RuleElementType.STRING_CONSTANT,
RuleElementType.INTEGER_CONSTANT,
RuleElementType.FIXED_TERMINAL,
RuleElementType.VAR_TERMINAL,
]:
return self._process_general_rule(
token_type, token_value, rule_element=rule_element
)
elif rule_element.element_type == RuleElementType.REF:
return self._process_ref_rule(rule_element=rule_element)
elif rule_element.element_type == RuleElementType.OR:
return self._process_or_rule(rule_element=rule_element)
elif rule_element.element_type == RuleElementType.LIST:
return self._process_list_rule(rule_element=rule_element)
elif rule_element.element_type == RuleElementType.MULTIPLE:
return self._process_multiple_rule(rule_element=rule_element)
elif rule_element.element_type == RuleElementType.ZERO_OR_ONE:
return self._process_zero_one_rule(rule_element=rule_element)

  • Seven
  • ConvertToBin
  • Square
  • Average
  • Pong
  • Complex Array

Chapter 12. Operating System

  • Array.jack
  • Math.jack
  • Memory.jack
  • Sys.jack
  • Keyboard.jack
  • Screen.jack
  • Output.jack
  • String.jack

About

Implementation of a complete computer from Nand gates. <The Elements of Computing Systems>

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published