CS641 Tiny Computer in Verilog

 

// from Hyde at Bucknell: Ultra simple computer

// edited for cs641

// available in $cs641/verilog

// Note: doesn’t work on sf06

module ultra;

 

parameter clock = 1;

reg [31:0] AC, IR, MD;

reg [9:0] PC, MA;

reg [31:0] MEM[1023:0];

initial begin   // stop at 100 ticks

  #(100*clock) $stop;

end

 

initial begin: init

PC=10;

// data

MEM[3] = 32'b00000000000000000000000000000010;

MEM[4] = 32'b00000000000000000000000000000001;

// program

MEM[10] = 32'b00000000000000000000000000000011;  // load 3

MEM[11] = 32'b00100000000000000000000000000100;  // add 4

MEM[12] = 32'b00010000000000000000000000000101;  // store 5

MEM[13] = 32'b00110000000000000000000000001011;  // jump 11

 

$display("Time PC IR        MA  MD       AC        MEM[5]");

$monitor("  %0d %h %h  %h %h  %h %h ", $time, PC, IR, MA, MD, AC, MEM[5])

end

always begin

 

#clock IR <= MEM[PC]; PC <= PC + 1;  // instruction fetch

#clock MA <= IR[9:0];  //instruction decode: last 10 bits are addr

if(IR[31:28] == 4'b0000) begin  // load

  #clock  AC <= MEM[MA];  // mem access

end

if(IR[31:28] == 4'b0001) begin  // store

  #clock MEM[MA] <= AC;  // mem access

end

if(IR[31:28] == 4'b0010) begin  // add

  #clock MD <= MEM[MA];  // mem access

  #clock AC <= AC + MD;   // ALU

end

if(IR[31:28] == 4'b0011) begin  // jump

  #clock PC <= MA;   // new PC

end

 

end

endmodule

 

C:\cs\cs641\verilog>vvp a.out

Time PC IR        MA  MD       AC        MEM[5]

  0 00a xxxxxxxx  xxx xxxxxxxx  xxxxxxxx xxxxxxxx

  1 00b 00000003  xxx xxxxxxxx  xxxxxxxx xxxxxxxx

  2 00b 00000003  003 xxxxxxxx  xxxxxxxx xxxxxxxx

  3 00b 00000003  003 xxxxxxxx  00000002 xxxxxxxx

  4 00c 20000004  003 xxxxxxxx  00000002 xxxxxxxx

  5 00c 20000004  004 xxxxxxxx  00000002 xxxxxxxx

  6 00c 20000004  004 00000001  00000002 xxxxxxxx

  7 00c 20000004  004 00000001  00000003 xxxxxxxx

  8 00d 10000005  004 00000001  00000003 xxxxxxxx

  9 00d 10000005  005 00000001  00000003 xxxxxxxx

  10 00d 10000005  005 00000001  00000003 00000003

  11 00e 3000000b  005 00000001  00000003 00000003

  12 00e 3000000b  00b 00000001  00000003 00000003

  13 00b 3000000b  00b 00000001  00000003 00000003

  14 00c 20000004  00b 00000001  00000003 00000003

  15 00c 20000004  004 00000001  00000003 00000003

  17 00c 20000004  004 00000001  00000004 00000003

  18 00d 10000005  004 00000001  00000004 00000003

  19 00d 10000005  005 00000001  00000004 00000003

  20 00d 10000005  005 00000001  00000004 00000004

  21 00e 3000000b  005 00000001  00000004 00000004

  22 00e 3000000b  00b 00000001  00000004 00000004

  23 00b 3000000b  00b 00000001  00000004 00000004

  24 00c 20000004  00b 00000001  00000004 00000004

  25 00c 20000004  004 00000001  00000004 00000004

  27 00c 20000004  004 00000001  00000005 00000004

  28 00d 10000005  004 00000001  00000005 00000004

  29 00d 10000005  005 00000001  00000005 00000004

  30 00d 10000005  005 00000001  00000005 00000005

  31 00e 3000000b  005 00000001  00000005 00000005

  32 00e 3000000b  00b 00000001  00000005 00000005

  33 00b 3000000b  00b 00000001  00000005 00000005

  34 00c 20000004  00b 00000001  00000005 00000005

  35 00c 20000004  004 00000001  00000005 00000005

  37 00c 20000004  004 00000001  00000006 00000005

  38 00d 10000005  004 00000001  00000006 00000005

  39 00d 10000005  005 00000001  00000006 00000005

  40 00d 10000005  005 00000001  00000006 00000006

  41 00e 3000000b  005 00000001  00000006 00000006

  42 00e 3000000b  00b 00000001  00000006 00000006

  43 00b 3000000b  00b 00000001  00000006 00000006

  44 00c 20000004  00b 00000001  00000006 00000006

  45 00c 20000004  004 00000001  00000006 00000006

  47 00c 20000004  004 00000001  00000007 00000006

  48 00d 10000005  004 00000001  00000007 00000006

  49 00d 10000005  005 00000001  00000007 00000006

  50 00d 10000005  005 00000001  00000007 00000007

  51 00e 3000000b  005 00000001  00000007 00000007

  52 00e 3000000b  00b 00000001  00000007 00000007

  53 00b 3000000b  00b 00000001  00000007 00000007

  54 00c 20000004  00b 00000001  00000007 00000007

  55 00c 20000004  004 00000001  00000007 00000007

  57 00c 20000004  004 00000001  00000008 00000007

  58 00d 10000005  004 00000001  00000008 00000007

  59 00d 10000005  005 00000001  00000008 00000007

  60 00d 10000005  005 00000001  00000008 00000008

  61 00e 3000000b  005 00000001  00000008 00000008

  62 00e 3000000b  00b 00000001  00000008 00000008

  63 00b 3000000b  00b 00000001  00000008 00000008

  64 00c 20000004  00b 00000001  00000008 00000008

  65 00c 20000004  004 00000001  00000008 00000008

  67 00c 20000004  004 00000001  00000009 00000008

  68 00d 10000005  004 00000001  00000009 00000008

  69 00d 10000005  005 00000001  00000009 00000008

  70 00d 10000005  005 00000001  00000009 00000009

  71 00e 3000000b  005 00000001  00000009 00000009

  72 00e 3000000b  00b 00000001  00000009 00000009

  73 00b 3000000b  00b 00000001  00000009 00000009

  74 00c 20000004  00b 00000001  00000009 00000009

  75 00c 20000004  004 00000001  00000009 00000009

  77 00c 20000004  004 00000001  0000000a 00000009

  78 00d 10000005  004 00000001  0000000a 00000009

  79 00d 10000005  005 00000001  0000000a 00000009

  80 00d 10000005  005 00000001  0000000a 0000000a

  81 00e 3000000b  005 00000001  0000000a 0000000a

  82 00e 3000000b  00b 00000001  0000000a 0000000a

  83 00b 3000000b  00b 00000001  0000000a 0000000a

  84 00c 20000004  00b 00000001  0000000a 0000000a

  85 00c 20000004  004 00000001  0000000a 0000000a

  87 00c 20000004  004 00000001  0000000b 0000000a

  88 00d 10000005  004 00000001  0000000b 0000000a

  89 00d 10000005  005 00000001  0000000b 0000000a

  90 00d 10000005  005 00000001  0000000b 0000000b

  91 00e 3000000b  005 00000001  0000000b 0000000b

  92 00e 3000000b  00b 00000001  0000000b 0000000b