diff --git a/ADD.JPG b/ADD.JPG new file mode 100644 index 0000000..997a195 Binary files /dev/null and b/ADD.JPG differ diff --git a/AND.JPG b/AND.JPG new file mode 100644 index 0000000..457689b Binary files /dev/null and b/AND.JPG differ diff --git a/CompArch_Lab1.pdf b/CompArch_Lab1.pdf new file mode 100644 index 0000000..9b5a213 Binary files /dev/null and b/CompArch_Lab1.pdf differ diff --git a/NAND.JPG b/NAND.JPG new file mode 100644 index 0000000..a81c899 Binary files /dev/null and b/NAND.JPG differ diff --git a/OR.JPG b/OR.JPG new file mode 100644 index 0000000..18ee2b7 Binary files /dev/null and b/OR.JPG differ diff --git a/SLT.JPG b/SLT.JPG new file mode 100644 index 0000000..a6f1aa4 Binary files /dev/null and b/SLT.JPG differ diff --git a/SUB.JPG b/SUB.JPG new file mode 100644 index 0000000..a28058c Binary files /dev/null and b/SUB.JPG differ diff --git a/Waveform.JPG b/Waveform.JPG new file mode 100644 index 0000000..07469bc Binary files /dev/null and b/Waveform.JPG differ diff --git a/XOR.JPG b/XOR.JPG new file mode 100644 index 0000000..53ed2f4 Binary files /dev/null and b/XOR.JPG differ diff --git a/alu.t.v b/alu.t.v new file mode 100644 index 0000000..f906dc4 --- /dev/null +++ b/alu.t.v @@ -0,0 +1,108 @@ +`include "alu.v" + +module testALU (); + reg [31:0] operandA, operandB; + reg [2:0] command; + reg [9:0] numBroken = 0; + wire [31:0] result; + wire carryout, ofl, zero; + + ALU testALU(.result (result), .carryout (carryout), .overflow (ofl), .zero (zero), .operandA (operandA), .operandB (operandB), .command (command)); + + task checkTestCase; + input [31:0] expectedResult; + input expectedCarryout, expectedOfl, expectedZero; + + begin + $display("Testing command %b, operandA %b, and operandB %b", command, operandA, operandB); + if (result == expectedResult) begin + $display("Test Passed, result is %b, carryout is %b, ofl is %b, and zero is %b", result, carryout, ofl, zero); + end else begin + numBroken = numBroken + 1; + $display("ERROR: Expected %b for result, %b for carryout, %b for ofl, and %b for zero, but actual output was %b, actual carryout was %b, actual ofl was %b, and actual zero is %b", expectedResult, expectedCarryout, expectedOfl, expectedZero, result, carryout, ofl, zero); + end + end + endtask + + initial begin + + //Add + command = 0; operandA = 0; operandB = 0; #2900 + checkTestCase(0, 0, 0, 1); + command = 0; operandA = 32'b11111111111111111111111111111111; operandB = 32'b11111111111111111111111111111111; #2910 + checkTestCase(32'b11111111111111111111111111111110, 1, 0, 0); + command = 0; operandA = 32'b10000000000000000000000000000001; operandB = 32'b01111111111111111111111111111111; #2910 + checkTestCase(0, 1, 0, 1); + command = 0; operandA = 32'b10000000000000000000000000000000; operandB = 32'b10000000000000000000000000000000; #2910 + checkTestCase(0, 1, 1, 1); + command = 0; operandA = 32'b11010101010101010101011010101010; operandB = 32'b00000001111111111111111101101010; #2910 + checkTestCase(32'b11010111010101010101011000010100, 0, 0, 0); + + //Sub + command = 1; operandA = 0; operandB = 0; #5800 + checkTestCase(0, 0, 0, 1); + command = 1; operandA = 32'b11111111111111111111111111111111; operandB = 32'b00000000000000000000000000000001; #5810 + checkTestCase(32'b11111111111111111111111111111110, 1, 0, 0); + command = 1; operandA = 32'b10000000000000000000000000000001; operandB = 32'b10000000000000000000000000000001; #5810 + checkTestCase(0, 1, 0, 1); + command = 1; operandA = 32'b10000000000000000000000000000000; operandB = 32'b10000000000000000000000000000000; #5810 + checkTestCase(0, 1, 1, 1); + command = 1; operandA = 32'b11010101010101010101011010101010; operandB = 32'b11111110000000000000000010010101; #5810 + checkTestCase(32'b11010111010101010101011000010101, 0, 0, 0); + + //XOR + command = 2; operandA = 32'b11111111111111111111111111111111; operandB = 32'b00000000000000000000000000000000; #80 + checkTestCase(32'b11111111111111111111111111111111, 0, 0, 0); + command = 2; operandA = 32'b11111111111111111111111111111111; operandB = 32'b11111111111111111111111111111111; #80 + checkTestCase(32'b00000000000000000000000000000000, 0, 0, 1); + command = 2; operandA = 32'b00101010101010101010111010111101; operandB = 32'b00001010101010110101010010110101; #80 + checkTestCase(32'b00100000000000011111101000001000, 0, 0, 0); + + //SLT + command = 3; operandA = 32'b11111111111111111111111111111111; operandB = 32'b00000000000000000000000000000000; #5850 + checkTestCase(32'b11111111111111111111111111111111, 0, 0, 1); + command = 3; operandA = 32'b11111111111111111111111111111111; operandB = 32'b11111111111111111111111111111111; #5850 + checkTestCase(32'b00000000000000000000000000000000, 0, 0, 1); + command = 3; operandA = 32'b00000000000000000000000000000000; operandB = 32'b11111111111111111111111111111111; #5850 + checkTestCase(32'b00000000000000000000000000000000, 0, 0, 0); + + //AND + command = 4; operandA = 32'b11111111111111111111111111111111; operandB = 32'b00000000000000000000000000000000; #40 + checkTestCase(32'b00000000000000000000000000000000, 0, 0, 1); + command = 4; operandA = 32'b11111111111111111111111111111111; operandB = 32'b11111111111111111111111111111111; #40 + checkTestCase(32'b11111111111111111111111111111111, 0, 0, 0); + command = 4; operandA = 32'b00000000000000111111111111111111; operandB = 32'b01000100000101011101110111011011; #40 + checkTestCase(32'b00000000000000011101110111011011, 0, 0, 0); + + //NAND + command = 5; operandA = 32'b11111111111111111111111111111111; operandB = 32'b00000000000000000000000000000000; #50 + checkTestCase(32'b11111111111111111111111111111111, 0, 0, 0); + command = 5; operandA = 32'b11111111111111111111111111111111; operandB = 32'b11111111111111111111111111111111; #50 + checkTestCase(32'b00000000000000000000000000000000, 0, 0, 1); + command = 5; operandA = 32'b00000000000000111111111111111111; operandB = 32'b01000100000101011101110111011011; #50 + checkTestCase(32'b11111111111111100010001000100100, 0, 0, 0); + + //NOR + command = 6; operandA = 32'b11111111111111111111111111111111; operandB = 32'b00000000000000000000000000000000; #50 + checkTestCase(32'b00000000000000000000000000000000, 0, 0, 1); + command = 6; operandA = 32'b00000000000000000000000000000000; operandB = 32'b00000000000000000000000000000000; #50 + checkTestCase(32'b11111111111111111111111111111111, 0, 0, 0); + command = 6; operandA = 32'b00000000000000111111111111111111; operandB = 32'b01000100000101011101110111011011; #50 + checkTestCase(32'b10111011111010000000000000000000, 0, 0, 0); + + //OR + command = 7; operandA = 32'b11111111111111111111111111111111; operandB = 32'b00000000000000000000000000000000; #40 + checkTestCase(32'b11111111111111111111111111111111, 0, 0, 0); + command = 7; operandA = 32'b00000000000000000000000000000000; operandB = 32'b00000000000000000000000000000000; #40 + checkTestCase(32'b00000000000000000000000000000000, 0, 0, 1); + command = 7; operandA = 32'b00000000000000111111111111111111; operandB = 32'b01000100000101011101110111011011; #40 + checkTestCase(32'b01000100000101111111111111111111, 0, 0, 0); + + $display("%d test cases failed", numBroken); + + end +endmodule + + + + \ No newline at end of file diff --git a/alu.v b/alu.v new file mode 100644 index 0000000..68453a1 --- /dev/null +++ b/alu.v @@ -0,0 +1,307 @@ +`define AND and #30 +`define OR or #30 +`define NOT not #10 +`define NAND nand #20 +`define NOR nor #20 +`define XOR xor #30 + + +module ALU(result, carryout, overflow, zero, operandA, operandB, command); + + output [31:0] result; + output carryout; + output overflow; + output zero; + input [31:0] operandA; + input [31:0] operandB; + input [2:0] command; + + wire subOverflow,addOverflow,subCarryout,addCarryout; + wire zero; + wire [31:0] carryin, addedResult, subResult, xorResult, andResult, nandResult, norResult, orResult; + wire [31:0] sltResult; + reg [31:0] result; + reg overflow,carryout; + + zeroes is_zero(zero,result); + multi_bit_adder adder(addedResult, addCarryout, addOverflow, operandA, operandB); + multi_bit_subtracter subtracter(subResult, subCarryout, subOverflow, operandA, operandB); + multi_bit_SLT slt(sltResult,operandA,operandB); + multi_bit_and andit(andResult,operandA,operandB); + multi_bit_or orit(orResult,operandA,operandB); + multi_bit_xor xorit(xorResult,operandA,operandB); + //output_mux result_mux(.result(result),.command(command),.operandA(operandA),.operandB(operandB),.addedResult(addedResult),.subResult(subResult),.sltResult(sltResult)); + + always @ (*) begin + case(command) + 3'b000: begin result = addedResult; carryout = addCarryout; overflow = addOverflow; end + 3'b001: begin result = subResult; carryout = subCarryout; overflow = subOverflow; end + 3'b010: result = xorResult; + 3'b011: result = sltResult; + 3'b100: result = andResult; + 3'b101: result = ~andResult; + 3'b110: result = ~orResult; + 3'b111: result = orResult; + endcase + end +endmodule + +module zeroes(zero,result); + output zero; + input [31:0] result; + + assign zero = !(1'b0||result[0]) && !(1'b0||result[1]) && !(1'b0||result[2]) && !(1'b0||result[3]) && !(1'b0||result[4]) && !(1'b0||result[5]) && !(1'b0||result[6]) && !(1'b0||result[7]) && !(1'b0||result[8]) && !(1'b0||result[9]) && !(1'b0||result[10]) && !(1'b0||result[11]) && !(1'b0||result[12]) && !(1'b0||result[13]) && !(1'b0||result[14]) && !(1'b0||result[15]) && !(1'b0||result[16]) && !(1'b0||result[17]) && !(1'b0||result[18]) && !(1'b0||result[19]) && !(1'b0||result[20]) && !(1'b0||result[21]) && !(1'b0||result[22]) && !(1'b0||result[23]) && !(1'b0||result[24]) && !(1'b0||result[25]) && !(1'b0||result[26]) && !(1'b0||result[27]) && !(1'b0||result[28]) && !(1'b0||result[29]) && !(1'b0||result[30]) && !(1'b0||result[31]); + +endmodule + + +//We can check if A