Advertisement
Guest User

CPU.hdl

a guest
Dec 23rd, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. // This file is part of www.nand2tetris.org
  2. // and the book "The Elements of Computing Systems"
  3. // by Nisan and Schocken, MIT Press.
  4. // File name: projects/05/CPU.hdl
  5.  
  6. /**
  7. * The Hack CPU (Central Processing unit), consisting of an ALU,
  8. * two registers named A and D, and a program counter named PC.
  9. * The CPU is designed to fetch and execute instructions written in
  10. * the Hack machine language. In particular, functions as follows:
  11. * Executes the inputted instruction according to the Hack machine
  12. * language specification. The D and A in the language specification
  13. * refer to CPU-resident registers, while M refers to the external
  14. * memory location addressed by A, i.e. to Memory[A]. The inM input
  15. * holds the value of this location. If the current instruction needs
  16. * to write a value to M, the value is placed in outM, the address
  17. * of the target location is placed in the addressM output, and the
  18. * writeM control bit is asserted. (When writeM==0, any value may
  19. * appear in outM). The outM and writeM outputs are combinational:
  20. * they are affected instantaneously by the execution of the current
  21. * instruction. The addressM and pc outputs are clocked: although they
  22. * are affected by the execution of the current instruction, they commit
  23. * to their new values only in the next time step. If reset==1 then the
  24. * CPU jumps to address 0 (i.e. pc is set to 0 in next time step) rather
  25. * than to the address resulting from executing the current instruction.
  26. */
  27.  
  28. CHIP CPU {
  29.  
  30. IN inM[16], // M value input (M = contents of RAM[A])
  31. instruction[16], // Instruction for execution
  32. reset; // Signals whether to re-start the current
  33. // program (reset==1) or continue executing
  34. // the current program (reset==0).
  35.  
  36. OUT outM[16], // M value output
  37. writeM, // Write to M?
  38. addressM[15], // Address in data memory (of M)
  39. pc[15]; // address of next instruction
  40.  
  41. PARTS:
  42. // Put your code here:
  43. Mux16(a=instruction, b=fromalu, sel=instruction[15], out=toaregister);
  44. And(a=instruction[15], b=instruction[3], out=writeM);
  45.  
  46. // A-Register
  47. // any a-instruction
  48. // c instruction with d1 set
  49. And(a=instruction[15], b=instruction[5], out=loadaregister);
  50. Not(in=instruction[15], out=notins);
  51. Or(a=notins, b=loadaregister, out=loadareg);
  52.  
  53. ARegister(in=toaregister, load=loadareg, out=fromaregister, out[0..14]=addressM);
  54.  
  55. // M/A only active during c instruction
  56. And(a=instruction[15], b=instruction[12], out=loadm);
  57. Mux16(a=fromaregister, b=inM, sel=loadm, out=frommora);
  58.  
  59. // D-Register also active during c instruction
  60. And(a=instruction[15], b=instruction[4], out=loaddregister);
  61. DRegister(in=todregister, load=loaddregister, out=fromdregister);
  62.  
  63. // ALU
  64. ALU(x=fromdregister, y=frommora, zx=instruction[11], nx=instruction[10], zy=instruction[9], ny=instruction[8], f=instruction[7], no=instruction[6], out=outM, out=fromalu, out=todregister, zr=outzr, ng=outng);
  65.  
  66. // ALU status bit(zr, ng)
  67. Or(a=outzr, b=outng, out=outzrng);
  68. Not(in=outzrng, out=pos);
  69.  
  70. //JGT
  71. And(a=pos, b=instruction[0], out=jgt);
  72.  
  73. //JEQ
  74. And(a=outzr, b=instruction[1], out=jeq);
  75.  
  76. //JGE
  77. Or(a=outzr, b=pos, out=zrorpos);
  78. And(a=zrorpos, b=instruction[0], out=jgefirst);
  79. And(a=jgefirst, b=instruction[1], out=jge);
  80.  
  81. //JLT
  82. And(a=outng, b=instruction[2], out=jlt);
  83.  
  84. //JNE
  85. Or(a=outng, b=pos, out=ngorpos);
  86. And(a=ngorpos, b=instruction[0], out=jnefirst);
  87. And(a=jnefirst, b=instruction[2], out=jne);
  88.  
  89. //JLE
  90. Or(a=outng, b=outzr, out=ngorzr);
  91. And(a=ngorzr, b=instruction[1], out=jlefirst);
  92. And(a=jlefirst, b=instruction[2], out=jle);
  93.  
  94. //JMP
  95. Or(a=outng, b=outzr, out=zrorng);
  96. Or(a=zrorng, b=pos, out=zrorngorpos);
  97. And(a=zrorngorpos, b=instruction[0], out=jmpfirst);
  98. And(a=jmpfirst, b=instruction[1], out=jmpsecond);
  99. And(a=jmpsecond, b=instruction[2], out=jmp);
  100.  
  101. Or(a=jgt, b=jeq, out=firstload);
  102. Or(a=jge, b=jlt, out=secondload);
  103. Or(a=jne, b=jle, out=thirdload);
  104.  
  105. Or(a=firstload, b=secondload, out=firstsecond);
  106. Or(a=thirdload, b=jmp, out=thirdjmp);
  107.  
  108. Or(a=firstsecond, b=thirdjmp, out=loadjmp);
  109.  
  110. And(a=instruction[15], b=loadjmp, out=loadto);
  111.  
  112. PC(in=fromaregister, load=loadto, inc=true, reset=reset, out[0..14]=pc);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement