+/TECS

git clone https://git.julia.blue/+/TECS

P1n3appl3Remove headers5e97a3a

master
2.9 KiB66 linesraw
1/**
2 * The Hack CPU (Central Processing unit), consisting of an ALU,
3 * two registers named A and D, and a program counter named PC.
4 * The CPU is designed to fetch and execute instructions written in
5 * the Hack machine language. In particular, functions as follows:
6 * Executes the inputted instruction according to the Hack machine
7 * language specification. The D and A in the language specification
8 * refer to CPU-resident registers, while M refers to the external
9 * memory location addressed by A, i.e. to Memory[A]. The inM input
10 * holds the value of this location. If the current instruction needs
11 * to write a value to M, the value is placed in outM, the address
12 * of the target location is placed in the addressM output, and the
13 * writeM control bit is asserted. (When writeM==0, any value may
14 * appear in outM). The outM and writeM outputs are combinational:
15 * they are affected instantaneously by the execution of the current
16 * instruction. The addressM and pc outputs are clocked: although they
17 * are affected by the execution of the current instruction, they commit
18 * to their new values only in the next time step. If reset==1 then the
19 * CPU jumps to address 0 (i.e. pc is set to 0 in next time step) rather
20 * than to the address resulting from executing the current instruction.
21 */
22
23// 1xx
24
25CHIP CPU {
26
27    IN  inM[16],         // M value input  (M = contents of RAM[A])
28        instruction[16], // Instruction for execution
29        reset;           // Signals whether to re-start the current
30                         // program (reset==1) or continue executing
31                         // the current program (reset==0).
32
33    OUT outM[16],        //x M value outputs
34        writeM,          //x Write to M?
35        addressM[15],    //x Address in data memory (of M)
36        pc[15];          //x address of next instruction
37
38    PARTS:
39    Not(in=instruction[15], out=ainst);
40    Not(in=ainst, out=cinst);
41
42    And(a=instruction[1],b=zero,out=j1);
43    And(a=instruction[2],b=less,out=j2);
44    Or(a=j1,b=j2,out=j3);
45    Not(in=less,out=m1);
46    Not(in=zero,out=notzero);
47    And(a=notzero,b=m1,out=more);
48    And(a=instruction[0],b=more,out=j4);
49    Or(a=j3,b=j4,out=j5);
50    And(a=j5,b=cinst,out=jump);
51    Not(in=jump,out=inc);
52    PC(in=aout,load=jump,inc=inc,reset=reset,out[0..14]=pc);
53
54    Or(a=instruction[5], b=ainst, out=aload);
55    Mux16(a=instruction,b=aluout,sel=cinst,out=ain);
56    ARegister(in=ain,load=aload,out[0..14]=addressM,out=aout);
57
58    And(a=cinst,b=instruction[4],out=dload);
59    DRegister(in=aluout,load=dload,out=dout);
60
61    And(a=cinst,b=instruction[3],out=writeM);
62
63    Mux16(a=aout,b=inM,sel=instruction[12],out=aluin);
64    ALU(x=dout,y=aluin,zx=instruction[11],nx=instruction[10],zy=instruction[9],ny=instruction[8],f=instruction[7],no=instruction[6],out=aluout,out=outM,zr=zero,ng=less);
65
66    }