projects/05/CPU.hdl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
// 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/05/CPU.hdl /** * The Hack CPU (Central Processing unit), consisting of an ALU, * two registers named A and D, and a program counter named PC. * The CPU is designed to fetch and execute instructions written in * the Hack machine language. In particular, functions as follows: * Executes the inputted instruction according to the Hack machine * language specification. The D and A in the language specification * refer to CPU-resident registers, while M refers to the external * memory location addressed by A, i.e. to Memory[A]. The inM input * holds the value of this location. If the current instruction needs * to write a value to M, the value is placed in outM, the address * of the target location is placed in the addressM output, and the * writeM control bit is asserted. (When writeM==0, any value may * appear in outM). The outM and writeM outputs are combinational: * they are affected instantaneously by the execution of the current * instruction. The addressM and pc outputs are clocked: although they * are affected by the execution of the current instruction, they commit * to their new values only in the next time step. If reset==1 then the * CPU jumps to address 0 (i.e. pc is set to 0 in next time step) rather * than to the address resulting from executing the current instruction. */ CHIP CPU { IN inM[16], // M value input (M = contents of RAM[A]) instruction[16], // Instruction for execution reset; // Signals whether to re-start the current // program (reset==1) or continue executing // the current program (reset==0). OUT outM[16], // M value output writeM, // Write to M? addressM[15], // Address in data memory (of M) pc[15]; // address of next instruction PARTS: // Put your code here: /* Instruction bit explanation * * A-instruction format: 0xxxxxxxxxxxxxxx * - place xxxxxxxxxxxxxxx directly into register A * * C-instruction format: 111accccccdddjjj (MSB is 1, LSB is j) * bit 0 (j): jump if ALU output is "positive" or if other j-bit is set * bit 1 (j): jump if ALU output is zero or if other j-bit is set * bit 2 (j): jump if ALU output is "negative" or if other j-bit is set * bit 3 (d): ALU output destination is or includes memory (RAM[A]) * bit 4 (d): ALU output destination is or includes D register * bit 5 (d): ALU output destination is or includes A register * bit 6 (c): set ALU no, NOT ALU output if 1 * bit 7 (c): set ALU f, decide if performing addition or logical AND * bit 8 (c): set ALU ny, decide if ALU x-input should be inverted (NOT) * bit 9 (c): set ALU zy, decide if ALU y-input should be zero * bit 10 (c): set ALU nx, decide if ALU x-input should be inverted (NOT) * bit 11 (c): set ALU zx, decide if ALU x-input should be zero * bit 12 (a): decide if ALU y-input should be inM (memory) or A register * bit 13 (1): always 1 (TODO: try setting to 0 and running) * bit 14 (1): always 1 (TODO: try setting to 0 and running) * bit 15 (1): A-type instruction if 0, C-type instruction if 1 */ Mux16(a=instruction, b=alu-out, sel=instruction[15], out=val-set-a); // if NOT A-type instruction OR destination A register Not(in=instruction[15], out=not-instr-MSB); Or(a=not-instr-MSB, b=instruction[5], out=load-a); ARegister(in=val-set-a, load=load-a, out=a-reg-out, out[0..14]=addressM); PC(in=a-reg-out, load=jump-if-c-instr, inc=not-jump-if-c-instr, reset=reset, out[0..14]=pc); Mux16(a=a-reg-out, b=inM, sel=instruction[12], out=a-or-m); ALU(x=d-reg-out, y=a-or-m, zx=instruction[11], nx=instruction[10], zy=instruction[9], ny=instruction[8], f=instruction[7], no=instruction[6], out=alu-out, out=outM, zr=alu-zr, ng=alu-ng); // if C-type instruction AND destination D register And(a=instruction[15], b=instruction[4], out=load-d); DRegister(in=alu-out, load=load-d, out=d-reg-out); // if a C-instruction (MSB==1) AND RAM[A] is a destination, then writeM And(a=instruction[15], b=instruction[3], out=writeM); // "jump-if" module between ALU and PC Or(a=alu-zr, b=alu-ng, out=j-or-out); Not(in=j-or-out, out=nor-out); // handle bit 0 (j1), jump if positive And(a=nor-out, b=instruction[0], out=jump-positive); // handle bit 1 (j2), jump if zero And(a=alu-zr, b=instruction[1], out=jump-zero); // handle bit 2 (j3), jump if negative And(a=alu-ng, b=instruction[2], out=jump-negative); // basically a 3-way AND Or(a=jump-positive, b=jump-zero, out=or1); Or(a=or1, b=jump-negative, out=jump-or-not); // Logic: if not jump (set PC=A), then inc. Vice-versa, hence the Not() And(a=jump-or-not, b=instruction[15], out=jump-if-c-instr); Not(in=jump-if-c-instr, out=not-jump-if-c-instr); } |