diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e39d5e7 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +test: build + echo "Running ALU test bench" + ./alu + echo -e "\n\nRunning memory test bench" + ./memory + echo -e "\n\nRunning register file test bench" + ./registerFile + echo -e "\n\nRunning CPU test bench" + ./cpu + +build: alu.t.v alu.v cpu.t.v cpu.v memory.t.v memory.v registerFile.t.v registerFile.v + iverilog alu.t.v -o alu + iverilog cpu.t.v -o cpu + iverilog memory.t.v -o memory + iverilog registerFile.t.v -o registerFile + +clean: + rm alu cpu memory registerFile diff --git a/Writeup.pdf b/Writeup.pdf new file mode 100644 index 0000000..28f02da Binary files /dev/null and b/Writeup.pdf differ diff --git a/alu.t.v b/alu.t.v new file mode 100644 index 0000000..aedb1ed --- /dev/null +++ b/alu.t.v @@ -0,0 +1,137 @@ +`include "alu.v" + +module aluTestBench(); + reg signed [31:0] A; + reg signed [31:0] B; + reg [1:0] op; + wire signed[31:0] out; + wire eq; + reg dutPassed; + alu dut(out, eq, op, A, B); + initial begin + dutPassed = 1; + A = 32'd10; + B = 32'd14; + op = `addOp; + #1; + if(out != 24 || ^out === 1'bx) begin + $display("Add test failed"); + dutPassed = 0; + end + + A = 32'd10; + B = 32'd14; + op = `subOp; + #1; + if(out != -4 || ^out === 1'bx) begin + $display("Sub test failed"); + dutPassed = 0; + end + + A = 32'b00001111000011110000111100001111; + B = 32'b10101010101010101010101010101010; + op = `xorOp; + #1; + if(out != 32'b10100101101001011010010110100101 || ^out === 1'bx) begin + $display("Xor test failed"); + dutPassed = 0; + end + + A = 14; + B = 32; + op = `sltOp; + #1; + if(out != 1 || ^out === 1'bx) begin + $display("Slt test failed"); + $display("14 >= 32"); + dutPassed = 0; + end + + A = 32; + B = 14; + op = `sltOp; + #1; + if(out != 0 || ^out === 1'bx) begin + $display("Slt test failed"); + $display("32 < 14"); + dutPassed = 0; + end + + A = -14; + B = -32; + op = `sltOp; + #1; + if(out != 0 || ^out === 1'bx) begin + $display("Slt test failed"); + $display("-14 < -32"); + dutPassed = 0; + end + + A = -32; + B = -14; + op = `sltOp; + #1; + if(out != 1 || ^out === 1'bx) begin + $display("Slt test failed"); + $display("-32 >= -14"); + dutPassed = 0; + end + + A = 14; + B = -32; + op = `sltOp; + #1; + if(out != 0 || ^out === 1'bx) begin + $display("Slt test failed"); + $display("14 < -32"); + dutPassed = 0; + end + + A = -32; + B = 14; + op = `sltOp; + #1; + if(out != 1 || ^out === 1'bx) begin + $display("Slt test failed"); + $display("-32 >= 14"); + dutPassed = 0; + end + + A = 14; + B = 14; + op = `sltOp; + #1; + if(out != 0 || ^out === 1'bx) begin + $display("Slt test failed"); + $display("14 < 14"); + dutPassed = 0; + end + + A = -14; + B = -14; + op = `sltOp; + #1; + if(out != 0 || ^out === 1'bx) begin + $display("Slt test failed"); + $display("-14 < -14"); + dutPassed = 0; + end + + if(!eq || eq === 1'bx) begin + $display("eq tes failed"); + $display("-14 != -14"); + end + A = -14; + B = 14; + #1; + if(eq || eq === 1'bx) begin + $display("eq tes failed"); + $display("-14 == 14"); + end + + if(dutPassed) begin + $display("DUT passed!"); + end + end + +endmodule diff --git a/alu.v b/alu.v new file mode 100644 index 0000000..ac189c1 --- /dev/null +++ b/alu.v @@ -0,0 +1,28 @@ +//ops: XOR, ADD, SUB, SLT +`define xorOp 2'b00 +`define addOp 2'b01 +`define subOp 2'b10 +`define sltOp 2'b11 + +module alu + #( + parameter width = 32 + ) + (output signed [width-1:0] out, + output eq, + input[1:0] op, + input signed [width-1:0] A, + input signed [width-1:0] B); + + wire[width-1:0] outputs[4]; + + assign outputs[`xorOp] = A^B; + assign outputs[`addOp] = A+B; + assign outputs[`subOp] = A-B; + assign outputs[`sltOp][0] = A