Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
5769094
Add register, state machine, and data memory files
vivienyuwenchen Oct 21, 2018
f167b1c
adding alu
lapudvan Oct 21, 2018
68020c3
Add instructions/decoder skeleton, memory
vivienyuwenchen Oct 22, 2018
65cc4d3
Add work plan
vivienyuwenchen Oct 23, 2018
138b993
Complete instruction LUT, delete FSM
vivienyuwenchen Oct 25, 2018
886dd5c
Adding instruction decoder module
SamYoung20 Oct 25, 2018
3164e2d
merging
SamYoung20 Oct 25, 2018
976cad3
adding cpu
SamYoung20 Oct 28, 2018
006b449
Add new mux, create execution submodule
vivienyuwenchen Oct 28, 2018
9ab6ad1
Merge branch 'master' of https://github.com/vivienyuwenchen/Lab3
vivienyuwenchen Oct 28, 2018
b7135e8
Update basicbuildingblocks.v
lapudvan Oct 28, 2018
01f2f83
Update basicbuildingblocks.v
lapudvan Oct 28, 2018
fa2bb09
jumpbranch
lapudvan Oct 28, 2018
e6c33a4
Complete execution (actually cpu), add sign extend, add op/funct code…
vivienyuwenchen Oct 29, 2018
edda1a9
Add mux for shift2, change sign extend/shift execution, delete unused…
vivienyuwenchen Oct 29, 2018
a8dd97a
file not used anymore
lapudvan Oct 29, 2018
696f75c
Merge branch 'master' of https://github.com/vivienyuwenchen/Lab3
vivienyuwenchen Oct 29, 2018
4f3aa6d
file not used anymore
lapudvan Oct 29, 2018
7e85060
file not used anymore
lapudvan Oct 29, 2018
c192560
file not used anymore
lapudvan Oct 29, 2018
d13989c
Replace decoder and muxes to basicbuildingblocks for regfile
vivienyuwenchen Oct 29, 2018
b1df9d1
Merge branch 'master' of https://github.com/vivienyuwenchen/Lab3
vivienyuwenchen Oct 29, 2018
bffc592
Update regfile include
vivienyuwenchen Oct 29, 2018
5dc6837
Fixing syntax
SamYoung20 Oct 29, 2018
cd2c0be
Mergine
SamYoung20 Oct 29, 2018
cde3f59
merging
SamYoung20 Oct 29, 2018
2e221fa
Simplying file organization
lapudvan Oct 29, 2018
e6a015b
file not needed
lapudvan Oct 29, 2018
646be3e
Add lut test bench, fix lut wire size
vivienyuwenchen Oct 29, 2018
b46903b
Fixing errors through main execution file run
SamYoung20 Oct 29, 2018
af318c7
Create memory.v
lapudvan Oct 29, 2018
bb4a023
fixing memory.v merge
SamYoung20 Oct 29, 2018
6a50a98
merging memv
SamYoung20 Oct 29, 2018
83decc8
Partial lut test bench
vivienyuwenchen Oct 30, 2018
1681409
Complete lut testbench, lut passes all tests
vivienyuwenchen Oct 30, 2018
5ea3b7e
fixing sizing errors in execution memory
SamYoung20 Oct 30, 2018
239481e
adding assembly test
lapudvan Oct 30, 2018
a855960
adding assembly test
lapudvan Oct 30, 2018
13a0e0f
adding assembly test
lapudvan Oct 30, 2018
c83604e
file not needed
lapudvan Oct 30, 2018
3b8f4ad
Simplifying file structure and test benches
lapudvan Oct 30, 2018
59b8ddd
Simplifying file structure and test benches
lapudvan Oct 30, 2018
0647ff2
adding memory and instruction decoder test bench
SamYoung20 Oct 31, 2018
9408cad
fixing memory merge conflict
SamYoung20 Oct 31, 2018
3f6d169
Complete basicbuildingblocks testbench, reorganize regfile submodules
vivienyuwenchen Oct 31, 2018
429d6fa
adding test benches and verilog files
SamYoung20 Oct 31, 2018
f0ed194
adding test benches and verilog files
SamYoung20 Oct 31, 2018
0ab69bd
making changes to executions
SamYoung20 Nov 1, 2018
a6bb839
Replace wires
vivienyuwenchen Nov 1, 2018
4154122
fixing merge
SamYoung20 Nov 1, 2018
dd6c480
Initialize dff for pc counter
vivienyuwenchen Nov 1, 2018
971d50c
pushing assmebly test
SamYoung20 Nov 1, 2018
2ac5374
Messy
vivienyuwenchen Nov 1, 2018
d0d6725
Even messier
vivienyuwenchen Nov 1, 2018
83db894
Combine memory modules, remove instr memory from decoder, clean data
vivienyuwenchen Nov 1, 2018
37eb52e
Adding test bench for cpu
SamYoung20 Nov 2, 2018
0b1426d
Complete decoder testbench, start memory testbench
vivienyuwenchen Nov 2, 2018
a782c8e
Data memory index error fixed
vivienyuwenchen Nov 2, 2018
2f1dca1
adding fib test bench
SamYoung20 Nov 2, 2018
595c533
merging
SamYoung20 Nov 2, 2018
6f2d690
pushing potentially related test code?
SamYoung20 Nov 2, 2018
22cf01a
adding fib asm
SamYoung20 Nov 2, 2018
69e4760
adding final test benches
SamYoung20 Nov 2, 2018
24c07a0
fixing test benches
SamYoung20 Nov 2, 2018
0779f4f
fixing file names and making respective changes in test benches
SamYoung20 Nov 2, 2018
8bfeab6
adding comments and cleaning
SamYoung20 Nov 2, 2018
fab34ee
fixing test benches for full cpu
SamYoung20 Nov 2, 2018
25d63df
organizing files and adding readmes
SamYoung20 Nov 2, 2018
45abb3a
fixing readme
SamYoung20 Nov 2, 2018
69e4d58
Create makefile, debug testbenches
vivienyuwenchen Nov 2, 2018
feba64a
Merge branch 'master' of https://github.com/vivienyuwenchen/Lab3
vivienyuwenchen Nov 2, 2018
4299f14
Add instructions on how to run
vivienyuwenchen Nov 2, 2018
d288c8d
Fix sizing of readme
vivienyuwenchen Nov 2, 2018
e402c38
Adding write up
lapudvan Nov 2, 2018
61ed125
Editting Lab
SamYoung20 Nov 2, 2018
25ed3d6
Delete Report.pdf
SamYoung20 Nov 2, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions HOW_TO_RUN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# How to Run Test Benches

```shell
$ make run
```

## To run CPU tests

```shell
$ make cpu
```

## To run submodule test benches

```shell
$ make tests
```

## To clean directory

```shell
$ make clean
```
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cpu: cpu_test_addN.t.v cpu_test_fib.t.v cpu_test_xor_sub_slt.t.v
iverilog -o cpu_test_addN.o cpu_test_addN.t.v
iverilog -o cpu_test_fib.o cpu_test_fib.t.v
iverilog -o cpu_test_xor_sub_slt.o cpu_test_xor_sub_slt.t.v
./cpu_test_addN.o
./cpu_test_fib.o
./cpu_test_xor_sub_slt.o

tests: alu.t.v basicbuildingblocks.t.v instructiondecoder.t.v lut.t.v memory.t.v regfile.t.v
iverilog -o alu.o alu.t.v
iverilog -o basicbuildingblocks.o basicbuildingblocks.t.v
iverilog -o instructiondecoder.o instructiondecoder.t.v
iverilog -o lut.o lut.t.v
iverilog -o memory.o memory.t.v
iverilog -o regfile.o regfile.t.v
./alu.o
./basicbuildingblocks.o
./instructiondecoder.o
./lut.o
./memory.o
./regfile.o

clean:
rm *.o
rm *.vcd

run:
make cpu
make tests
make clean
Binary file added _Report.pdf
Binary file not shown.
246 changes: 246 additions & 0 deletions alu.t.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
//------------------------------------------------------------------------
// Test Bench for ALU Module
//------------------------------------------------------------------------

`timescale 1 ns / 1 ps
`include "alu.v"

module testALU();
reg [31:0] operandA, operandB;
reg [2:0] command;
wire [31:0] result;
wire carryout, zero, overflow;

ALU aluer (result, carryout, zero, overflow, operandA, operandB, command);

initial begin
$display("--------------------------------------------------");
$display("ALU tests starting...");

$display("Starting ADD tests...");
// ADD test 1
command=`ADD; operandA=32'h000A0000; operandB=32'h00000070; #5000 // carryout = 0
if(result != 32'h000A0070)
$display("ADD test 1 - result: %h, expected: 000A0070", result);
if(carryout != 1'b0)
$display("ADD test 1 - carryout: %h, expected: 0", carryout);
if(zero != 1'b0)
$display("ADD test 1 - zero: %h, expected: 0", zero);
if(overflow != 1'b0)
$display("ADD test 1 - overflow: %h, expected: 0", overflow);
// ADD test 2
command=`ADD; operandA=32'h7FFFFFFF; operandB=32'h7FFFFFFF; #5000
if(result != 32'hFFFFFFFE)
$display("ADD test 2 - result: %h, expected: FFFFFFFE", result);
if(carryout != 1'b0)
$display("ADD test 2 - carryout: %h, expected: 0", carryout);
if(zero != 1'b0)
$display("ADD test 2 - zero: %h, expected: 0", zero);
if(overflow != 1'b1)
$display("ADD test 2 - overflow: %h, expected: 1", overflow);
// ADD test 3
command=`ADD; operandA=32'h00000001; operandB=32'hFFFFFFFF; #5000
if(result != 32'h00000000)
$display("ADD test 3 - result: %h, expected: 00000000", result);
if(carryout != 1'b1)
$display("ADD test 3 - carryout: %h, expected: 1", carryout);
if(zero != 1'b1)
$display("ADD test 3 - zero: %h, expected: 1", zero);
if(overflow != 1'b0)
$display("ADD test 3 - overflow: %h, expected: 0", overflow);
// ADD test 4
command=`ADD; operandA=32'h80000000; operandB=32'h80000000; #5000
if(result != 32'h00000000)
$display("ADD test 4 - result: %h, expected: 00000000", result);
if(carryout != 1'b1)
$display("ADD test 4 - carryout: %h, expected: 1", carryout);
if(zero != 1'b1)
$display("ADD test 4 - zero: %h, expected: 1", zero);
if(overflow != 1'b1)
$display("ADD test 4 - overflow: %h, expected: 1", overflow);

$display("Starting SUB tests...");
// SUB test 1
command=`SUB; operandA=32'h000A0000; operandB=32'hFFFFFF90; #5000
if(result != 32'h000A0070)
$display("SUB test 1 - result: %h, expected: 000A0070", result);
if(carryout != 1'b0)
$display("SUB test 1 - carryout: %h, expected: 0", carryout);
if(zero != 1'b0)
$display("SUB test 1 - zero: %h, expected: 0", zero);
if(overflow != 1'b0)
$display("SUB test 1 - overflow: %h, expected: 0", overflow);
// SUB test 2
command=`SUB; operandA=32'h7FFFFFFF; operandB=32'h80000001; #5000
if(result != 32'hFFFFFFFE)
$display("SUB test 2 - result: %h, expected: FFFFFFFE", result);
if(carryout != 1'b0)
$display("SUB test 2 - carryout: %h, expected: 0", carryout);
if(zero != 1'b0)
$display("SUB test 2 - zero: %h, expected: 0", zero);
if(overflow != 1'b1)
$display("SUB test 2 - overflow: %h, expected: 1", overflow);
// SUB test 3
command=`SUB; operandA=32'h00000001; operandB=32'h00000001; #5000
if(result != 32'h00000000)
$display("SUB test 3 - result: %h, expected: 00000000", result);
if(carryout != 1'b1)
$display("SUB test 3 - carryout: %h, expected: 1", carryout);
if(zero != 1'b1)
$display("SUB test 3 - zero: %h, expected: 1", zero);
if(overflow != 1'b0)
$display("SUB test 3 - overflow: %h, expected: 0", overflow);
//SUB test 4
command=`SUB; operandA=32'h80000000; operandB=32'h70000000; #5000
if(result != 32'h10000000)
$display("SUB test 4 - result: %h, expected: 10000000", result);
if(carryout != 1'b1)
$display("SUB test 4 - carryout: %h, expected: 1", carryout);
if(zero != 1'b0)
$display("SUB test 4 - zero: %h, expected: 0", zero);
if(overflow != 1'b1)
$display("SUB test 4 - overflow: %h, expected: 1", overflow);

$display("Starting XOR tests...");
// XOR test 1
command=`Xor; operandA=32'h88888888; operandB=32'h11111111; #5000
if(result != 32'h99999999)
$display("XOR test 1 - result: %h, expected: 99999999", result);
if(zero != 1'b0)
$display("XOR test 1 - zero: %h, expected: 0", zero);
// XOR test 2
command=`Xor; operandA=32'hCCCCCCCC; operandB=32'hCCCCCCCC; #5000
if(result != 32'h00000000)
$display("XOR test 2 - result: %h, expected: 00000000", result);
if(zero != 1'b1)
$display("XOR test 2 - zero: %h, expected: 1", zero);
// XOR test 3
command=`Xor; operandA=32'hBBBBBBBB; operandB=32'h55555555; #5000
if(result != 32'hEEEEEEEE)
$display("XOR test 3 - result: %h, expected: EEEEEEEE", result);
if(zero != 1'b0)
$display("XOR test 3 - zero: %h, expected: 0", zero);

$display("Starting SLT tests...");
// SLT test 1
command=`SLT; operandA=32'h00000001; operandB=32'h05000000; #5000 //positive numbers
if(result != 32'h00000001)
$display("SLT test 1 - result: %h, expected: 00000001", result);
// SLT test a
command=`SLT; operandA=32'h7FFFFFFF; operandB=32'h80000001; #5000
if(result != 32'h00000000)
$display("SLT test 2 - result: %h, expected: 00000000", result);
// SLT test b
command=`SLT; operandA=32'h00000001; operandB=32'h00000001; #5000
if(result != 32'h00000000)
$display("SLT test 2 - result: %h, expected: 00000000", result);
// SLT test 2
command=`SLT; operandA=32'h05000000; operandB=32'h00000001; #5000
if(result != 32'h00000000)
$display("SLT test 2 - result: %h, expected: 00000000", result);
// SLT test 3
command=`SLT; operandA=32'h80000000; operandB=32'hF0000000; #5000 //negative numbers
if(result != 32'h00000001)
$display("SLT test 3 - result: %h, expected: 00000001", result);
// SLT test 4
command=`SLT; operandA=32'hF0000000; operandB=32'h80000000; #5000
if(result != 32'h00000000)
$display("SLT test 4 - result: %h, expected: 00000000", result);
// SLT test 5
command=`SLT; operandA=32'h80000000; operandB=32'h05000000; #5000 //positive and negative numbers
if(result != 32'h00000001)
$display("SLT test 5 - result: %h, expected: 00000001", result);
// SLT test 6
command=`SLT; operandA=32'h05000000; operandB=32'h80000000; #5000
if(result != 32'h00000000)
$display("SLT test 6 - result: %h, expected: 00000000", result);
// SLT test 7
command=`SLT; operandA=32'h00000000; operandB=32'h00000000; #5000 //zeros
if(result != 32'h00000000)
$display("SLT test 7 - result: %h, expected: 00000000", result);

$display("Starting AND tests...");
// AND test 1
command=`And; operandA=32'h88888888; operandB=32'h11111111; #5000
if(result != 32'h00000000)
$display("AND test 1 - result: %h, expected: 00000000", result);
if(zero != 1'b1)
$display("AND test 1 - zero: %h, expected: 1", zero);
// AND test 2
command=`And; operandA=32'hCCCCCCCC; operandB=32'hCCCCCCCC; #5000
if(result != 32'hCCCCCCCC)
$display("AND test 2 - result: %h, expected: CCCCCCCC", result);
if(zero != 1'b0)
$display("AND test 2 - zero: %h, expected: 0", zero);
// AND test 3
command=`And; operandA=32'hBBBBBBBB; operandB=32'h55555555; #5000
if(result != 32'h11111111)
$display("AND test 3 - result: %h, expected: 11111111", result);
if(zero != 1'b0)
$display("AND test 3 - zero: %h, expected: 0", zero);

$display("Starting NAND tests...");
// NAND test 1
command=`Nand; operandA=32'h88888888; operandB=32'h11111111; #5000
if(result != 32'hFFFFFFFF)
$display("NAND test 1 - result: %h, expected: FFFFFFFF", result);
if(zero != 1'b0)
$display("NAND test 1 - zero: %h, expected: 0", zero);
// NAND test 2
command=`Nand; operandA=32'hCCCCCCCC; operandB=32'hCCCCCCCC; #5000
if(result != 32'h33333333)
$display("NAND test 2 - result: %h, expected: 33333333", result);
if(zero != 1'b0)
$display("NAND test 2 - zero: %h, expected: 0", zero);
// NAND test 3
command=`Nand; operandA=32'hBBBBBBBB; operandB=32'h55555555; #5000
if(result != 32'hEEEEEEEE)
$display("NAND test 3 - result: %h, expected: EEEEEEEE", result);
if(zero != 1'b0)
$display("NAND test 3 - zero: %h, expected: 0", zero);

$display("Starting NOR tests...");
// NOR test 1
command=`Nor; operandA=32'h88888888; operandB=32'h11111111; #5000
if(result != 32'h66666666)
$display("NOR test 1 - result: %h, expected: 66666666", result);
if(zero != 1'b0)
$display("NOR test 1 - zero: %h, expected: 0", zero);
// NOR test 2
command=`Nor; operandA=32'hCCCCCCCC; operandB=32'hCCCCCCCC; #5000
if(result != 32'h33333333)
$display("NOR test 2 - result: %h, expected: 33333333", result);
if(zero != 1'b0)
$display("NOR test 2 - zero: %h, expected: 0", zero);
// NOR test 3
command=`Nor; operandA=32'hBBBBBBBB; operandB=32'h55555555; #5000
if(result != 32'h00000000)
$display("NOR test 3 - result: %h, expected: 00000000", result);
if(zero != 1'b1)
$display("NOR test 3 - zero: %h, expected: 1", zero);

$display("Starting OR tests...");
// OR test 1
command=`Or; operandA=32'h88888888; operandB=32'h11111111; #5000
if(result != 32'h99999999)
$display("OR test 1 - result: %h, expected: 99999999", result);
if(zero != 1'b0)
$display("OR test 1 - zero: %h, expected: 0", zero);
// OR test 2
command=`Or; operandA=32'hCCCCCCCC; operandB=32'hCCCCCCCC; #5000
if(result != 32'hCCCCCCCC)
$display("OR test 2 - result: %h, expected: CCCCCCCC", result);
if(zero != 1'b0)
$display("OR test 2 - zero: %h, expected: 0", zero);
// OR test 3
command=`Or; operandA=32'hBBBBBBBB; operandB=32'h55555555; #5000
if(result != 32'hFFFFFFFF)
$display("OR test 3 - result: %h, expected: FFFFFFFF", result);
if(zero != 1'b0)
$display("OR test 3 - zero: %h, expected: 0", zero);

$display("ALU tests done!");
$display("--------------------------------------------------");
$finish();
end
endmodule
97 changes: 97 additions & 0 deletions alu.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//------------------------------------------------------------------------
// ALU Module
//------------------------------------------------------------------------

`define ADD 3'd0
`define SUB 3'd1
`define Xor 3'd2
`define SLT 3'd3
`define And 3'd4
`define Nand 3'd5
`define Nor 3'd6
`define Or 3'd7
`include "alu_function.v"
`define NOR32 nor //32 input NOR

module ALU
(
output[31:0] result,
output carryout,
output zero,
output overflow,
input[31:0] operandA,
input[31:0] operandB,
input[2:0] command
);
//declare wires
wire[31:0] out0, out1, out2, out3, out4;
wire cout0, cout1, cout2, cout3, cout4;
wire over0, over1, over2, over3, over4;
wire invert;
wire[2:0] muxindex;
wire[32:0] carryin0;

//use LUT to get variable assignments for muxindex and invert depending on the input command
ALUcontrolLUT lut(.muxindex(muxindex), .invert(invert), .ALUcommand(command));
//first carryin of add-subtract module is identical to the invert signal
assign carryin0[0] = invert;

//bit slice approach, generate 32 of each module for full capability
genvar i;
generate for (i = 0; i < 32; i = i + 1) begin
AddSubN adder(.sum(out0[i]), .carryout(carryin0[i+1]), .a(operandA[i]), .b(operandB[i]), .carryin(carryin0[i]), .subtract(invert));

XORmod xorer(.out(out1[i]), .carryout(cout1), .overflow(over1), .a(operandA[i]), .b(operandB[i]));

NANDmod nander(.out(out3[i]), .carryout(cout3), .overflow(over3), .a(operandA[i]), .b(operandB[i]), .invert(invert));

NORmod norer(.out(out4[i]), .carryout(cout4), .overflow(over4), .a(operandA[i]), .b(operandB[i]), .invert(invert));
end
endgenerate

SLTmod slter (.slt(out2), .carryout(cout2), .overflow(over2), .a(operandA), .b(operandB));

//set carryout for adder, equivalent to the "carryin" of the 33rd bit
assign cout0 = carryin0[32];
//calculate overflow for adder; overflow if final carryout is not equal to carryin of most significant bit
`XOR OVERFLOW(over0, cout0, carryin0[31]);

//mux between generated outputs depending on muxindex given by ALUcommand
genvar n;
generate for (n = 0; n < 32; n = n + 1) begin
structuralMultiplexer5 resultmux (.out(result[n]), .command(muxindex), .in0(out0[n]), .in1(out1[n]), .in2(out2[n]), .in3(out3[n]), .in4(out4[n]));
end
endgenerate

//mux between carryouts
structuralMultiplexer5 coutmux (.out(carryout), .command(muxindex), .in0(cout0), .in1(cout1), .in2(cout2), .in3(cout3), .in4(cout4));
//mux between overflows
structuralMultiplexer5 overmux (.out(overflow), .command(muxindex), .in0(over0), .in1(over1), .in2(over2), .in3(over3), .in4(over4));

//if all bits of the result are zero, the output zero should return zero
`NOR32 norgate(zero, result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10],
result[11], result[12], result[13], result[14], result[15], result[16], result[17], result[18], result[19], result[20], result[21], result[22],
result[23], result[24], result[25], result[26], result[27], result[28], result[29], result[30], result[31]);

endmodule


module ALUcontrolLUT
(
output reg[2:0] muxindex,
output reg invert,
input[2:0] ALUcommand
);
always @(ALUcommand) begin
case (ALUcommand)
`ADD: begin muxindex = 0; invert=0; end
`SUB: begin muxindex = 0; invert=1; end
`Xor: begin muxindex = 1; invert=0; end
`SLT: begin muxindex = 2; invert=0; end
`Nand: begin muxindex = 3; invert=0; end
`And: begin muxindex = 3; invert=1; end
`Nor: begin muxindex = 4; invert=0; end
`Or: begin muxindex = 4; invert=1; end
endcase
end
endmodule
Loading