summaryrefslogtreecommitdiff
path: root/nandgame
diff options
context:
space:
mode:
Diffstat (limited to 'nandgame')
-rw-r--r--nandgame/alu_tb.sv162
1 files changed, 162 insertions, 0 deletions
diff --git a/nandgame/alu_tb.sv b/nandgame/alu_tb.sv
new file mode 100644
index 0000000..cb5bfa5
--- /dev/null
+++ b/nandgame/alu_tb.sv
@@ -0,0 +1,162 @@
+`timescale 1us/1us
+
+`include "nandgame_types.v"
+
+module alu_tb;
+
+logic [15:0] in1;
+logic [15:0] in2;
+logic [1:0] opcode;
+logic [15:0] result;
+logic u, zx, sw;
+
+alu uut (
+ .X(in1),
+ .Y(in2),
+ .opcode(opcode),
+ .RES(result),
+ .u(u),
+ .zx(zx),
+ .sw(sw)
+);
+
+string filename;
+initial begin
+`ifdef DUMP_FILE_NAME
+ filename=`DUMP_FILE_NAME;
+`else
+ filename="alu.lxt2";
+`endif
+ $dumpfile(filename); $dumpvars();
+end
+
+initial begin
+ zx = 0;
+ sw = 0;
+
+ // Arith tests
+ u = 1;
+ in1 = 13;
+ in2 = 17;
+ opcode = ARITH_PLUS;
+ #1
+ assert(result == 30)
+ else $error("Expected: 30, got: %d", result);
+ #1
+ in1 = 12;
+ in2 = 4;
+ opcode = ARITH_MINUS;
+ #1
+ assert(result == 8)
+ else $error("Expected: 8, got: %d", result);
+ #1
+ in1 = 12;
+ in2 = 13;
+ opcode = ARITH_MINUS;
+ #1
+ assert(result == 16'hffff)
+ else $error("Expected: 16'hffff, got: %d", result);
+ #1
+ in1 = 13;
+ in2 = 17;
+ opcode = ARITH_INC;
+ #1
+ assert(result == 14)
+ else $error("Expected: 14, got: %d", result);
+ #1
+ in1 = 13;
+ in2 = 17;
+ opcode = ARITH_DEC;
+ #1
+ assert(result == 12)
+ else $error("Expected: 12, got: %d", result);
+ #1
+ in1 = 0;
+ in2 = 17;
+ opcode = ARITH_DEC;
+ #1
+ assert(result == 16'hffff)
+ else $error("Expected: 16'hffff, got: %d", result);
+ #1
+
+ // logic tests
+ u = 0;
+ in1 = 16'b1010;
+ in2 = 16'b1100;
+ opcode = LOGIC_AND;
+ #1
+ assert(result == 16'b1000)
+ else $error("Expected: 16'b1000, got: %d", result);
+ #1
+ in1 = 16'b1010;
+ in2 = 16'b1100;
+ opcode = LOGIC_OR;
+ #1
+ assert(result == 16'b1110)
+ else $error("Expected: 16'b1110, got: %d", result);
+ #1
+ in1 = 16'b1010;
+ in2 = 16'b1100;
+ opcode = LOGIC_XOR;
+ #1
+ assert(result == 16'b0110)
+ else $error("Expected: 16'b0110, got: %d", result);
+ #1
+ in1 = 16'b1010101010101010;
+ opcode = LOGIC_NEGT;
+ #1
+ assert(result == 16'b0101010101010101)
+ else $error("Expected: 16'b0101010101010101, got: %d", result);
+ #1
+
+ // zero - tests
+ zx = 1;
+ sw = 0;
+ // back to logic
+ u = 0;
+ in1 = 2;
+ in2 = 4;
+ opcode = LOGIC_OR;
+ #1
+ assert(result == 4)
+ else $error("Expected 4, got %d", result);
+ #1
+
+ // back to arith
+ u = 1;
+ in1 = 2;
+ in2 = 4;
+ opcode = ARITH_PLUS;
+ #1
+ assert(result == 4)
+ else $error("Expected 4, got %d", result);
+ #1
+
+ // swap tests
+ zx = 0;
+ sw = 1;
+ u = 1;
+ in1 = 2;
+ in2 = 3;
+ opcode = ARITH_MINUS;
+ #1
+ // 3 - 2
+ assert(result == 1)
+ else $error("Expected 1, got %d", result);
+
+ // zero-swap
+ zx = 1;
+ sw = 1;
+ u = 1;
+ in1 = 2;
+ in2 = 3;
+ opcode = ARITH_PLUS;
+ #1
+ // 2 + 0
+ assert(result == 2)
+ else $error("Expected 2, got %d", result);
+
+ $finish();
+end
+
+endmodule