2023-10-20 09:48:18 +00:00
|
|
|
`timescale 1ns / 1ps
|
2023-10-23 08:34:37 +00:00
|
|
|
`include "tb_tools.vh"
|
2023-10-20 09:48:18 +00:00
|
|
|
|
|
|
|
module tb_alu ();
|
2023-10-24 01:49:29 +00:00
|
|
|
`include "../rtl/alu_func.vh"
|
|
|
|
|
2023-10-23 05:15:21 +00:00
|
|
|
reg [31:0] in_a;
|
|
|
|
reg [31:0] in_b;
|
2023-10-24 10:39:42 +00:00
|
|
|
reg [3:0] func;
|
2023-10-23 05:15:21 +00:00
|
|
|
wire [31:0] out;
|
2023-10-20 09:48:18 +00:00
|
|
|
|
2023-10-23 05:15:21 +00:00
|
|
|
alu alu (
|
|
|
|
.in_a(in_a),
|
|
|
|
.in_b(in_b),
|
2023-10-24 10:39:42 +00:00
|
|
|
.func(func),
|
2023-10-23 05:15:21 +00:00
|
|
|
.out(out)
|
|
|
|
);
|
2023-10-20 09:48:18 +00:00
|
|
|
|
2023-10-23 05:15:21 +00:00
|
|
|
initial begin
|
2023-10-24 10:36:34 +00:00
|
|
|
// ALU - add
|
2023-10-24 10:39:42 +00:00
|
|
|
func = ADD;
|
2023-10-23 08:34:37 +00:00
|
|
|
in_a = 32'b0;
|
|
|
|
in_b = 32'b0;
|
|
|
|
`assert("alu : 0 + 0", out, 0)
|
|
|
|
in_a = 32'b1;
|
|
|
|
`assert("alu : 1 + 0", out, 1)
|
|
|
|
in_b = 32'b1;
|
|
|
|
`assert("alu : 1 + 1", out, 2)
|
2023-10-24 10:36:34 +00:00
|
|
|
in_a = 32'b0;
|
|
|
|
`assert("alu : 0 + 1", out, 1)
|
|
|
|
in_a = 32'b1111;
|
|
|
|
in_b = 32'b1111;
|
|
|
|
`assert("alu : 15 + 15", out, 30)
|
|
|
|
in_a = 32'b11111111111111111111111111111111;
|
|
|
|
in_b = 32'b00000000000000000000000000000000;
|
|
|
|
`assert("alu : 0 + -1", out, 32'b11111111111111111111111111111111)
|
|
|
|
in_a = 32'b11111111111111111111111111111111;
|
|
|
|
in_b = 32'b00000000000000000000000000000001;
|
|
|
|
`assert("alu : 1 + -1", out, 0)
|
|
|
|
in_a = 32'b10000000000000000000000000000000;
|
|
|
|
in_b = 32'b11111111111111111111111111111111;
|
|
|
|
`assert("alu : MIN_INT + -1", out, 32'b01111111111111111111111111111111)
|
|
|
|
|
|
|
|
// ALU - sub
|
2023-10-24 10:39:42 +00:00
|
|
|
func = SUB;
|
2023-10-24 10:36:34 +00:00
|
|
|
in_a = 32'b0;
|
|
|
|
in_b = 32'b0;
|
|
|
|
`assert("alu : 0 - 0", out, 0)
|
|
|
|
in_a = 32'b1;
|
|
|
|
`assert("alu : 1 - 0", out, 1)
|
|
|
|
in_b = 32'b1;
|
2023-10-23 08:34:37 +00:00
|
|
|
`assert("alu : 1 - 1", out, 0)
|
2023-10-24 10:36:34 +00:00
|
|
|
in_a = 32'b0;
|
|
|
|
`assert("alu : 0 - 1", out, 32'b11111111111111111111111111111111)
|
|
|
|
in_a = 32'b11111;
|
|
|
|
in_b = 32'b1111;
|
|
|
|
`assert("alu : 31 - 15", out, 16)
|
|
|
|
in_a = 32'b11111111111111111111111111111111;
|
|
|
|
in_b = 32'b00000000000000000000000000000000;
|
|
|
|
`assert("alu : -1 - 0", out, 32'b11111111111111111111111111111111)
|
|
|
|
in_a = 32'b11111111111111111111111111111111;
|
|
|
|
in_b = 32'b00000000000000000000000000000001;
|
|
|
|
`assert("alu : -1 - 1", out, 32'b11111111111111111111111111111110)
|
|
|
|
in_a = 32'b10000000000000000000000000000000;
|
|
|
|
in_b = 32'b11111111111111111111111111111111;
|
|
|
|
`assert("alu : MIN_INT - -1", out, 32'b10000000000000000000000000000001)
|
2023-10-20 09:48:18 +00:00
|
|
|
|
2023-10-24 01:49:29 +00:00
|
|
|
// ALU - left shift
|
2023-10-24 10:39:42 +00:00
|
|
|
func = SLL;
|
2023-10-24 01:49:29 +00:00
|
|
|
in_a = 32'b1;
|
|
|
|
in_b = 32'b1;
|
|
|
|
`assert("alu : 1 << 1", out, 2)
|
|
|
|
in_b = 32'b10;
|
|
|
|
`assert("alu : 1 << 2", out, 4)
|
|
|
|
in_a = 32'b11;
|
|
|
|
`assert("alu : 3 << 2", out, 12)
|
|
|
|
in_b = 32'b11110;
|
|
|
|
`assert("alu : 3 << 30", out, 32'b11000000000000000000000000000000)
|
|
|
|
in_b = 32'b11111;
|
|
|
|
`assert("alu : 3 << 31", out, 32'b10000000000000000000000000000000)
|
|
|
|
in_b = 32'b100000;
|
|
|
|
`assert("alu : 3 << 31", out, 32'b00000000000000000000000000000000)
|
|
|
|
|
|
|
|
// ALU - less than
|
2023-10-24 10:39:42 +00:00
|
|
|
func = SLT;
|
2023-10-24 01:49:29 +00:00
|
|
|
in_a = 32'b0;
|
|
|
|
in_b = 32'b0;
|
|
|
|
`assert("alu : 0 < 0", out, 0)
|
|
|
|
in_b = 32'b10;
|
|
|
|
`assert("alu : 0 << 2", out, 1)
|
|
|
|
in_a = 32'b11;
|
|
|
|
`assert("alu : 3 < 2", out, 0)
|
|
|
|
in_b = 32'b11111111111111111111111111111111;
|
|
|
|
in_a = 32'b11111111111111111111111111111111;
|
|
|
|
`assert("alu : -1 < -1", out, 0)
|
|
|
|
in_b = 32'b0;
|
|
|
|
`assert("alu : -1 < 0", out, 1)
|
|
|
|
in_a = 32'b10000000000000000000000000000000;
|
|
|
|
in_b = 32'b10000000000000000000000000000001;
|
|
|
|
`assert("alu : MIN_INT << MIN_INT + 1", out, 1)
|
|
|
|
|
|
|
|
// ALU - xor
|
2023-10-24 10:39:42 +00:00
|
|
|
func = XOR;
|
2023-10-24 01:49:29 +00:00
|
|
|
in_a = 32'b0;
|
|
|
|
in_b = 32'b0;
|
|
|
|
`assert("alu : 0 ^ 0", out, 32'b00000000000000000000000000000000)
|
|
|
|
in_a = 32'b1;
|
|
|
|
`assert("alu : 1 ^ 0", out, 32'b00000000000000000000000000000001)
|
|
|
|
in_a = 32'b0;
|
|
|
|
in_b = 32'b1;
|
|
|
|
`assert("alu : 0 ^ 1", out, 32'b00000000000000000000000000000001)
|
|
|
|
in_a = 32'b11111111111111111111111111111111;
|
|
|
|
in_b = 32'b11111111111111111111111111111111;
|
|
|
|
`assert("alu : MAX_INT ^ MAX_INT", out, 32'b00000000000000000000000000000000)
|
|
|
|
in_a = 32'b00000000000000000000000000000000;
|
|
|
|
in_b = 32'b11111111111111111111111111111111;
|
|
|
|
`assert("alu : 0 ^ MAX_INT", out, 32'b11111111111111111111111111111111)
|
|
|
|
in_a = 32'b00000011001000010001000011000000;
|
|
|
|
in_b = 32'b10101111001011101110111111111011;
|
|
|
|
`assert("alu : 00000011001000010001000011000000 ^ 10101111001011101110111111111011", out, 32'b10101100000011111111111100111011)
|
|
|
|
|
|
|
|
// ALU - right shift
|
2023-10-24 10:39:42 +00:00
|
|
|
func = SRL;
|
2023-10-24 01:49:29 +00:00
|
|
|
in_a = 32'b1;
|
|
|
|
in_b = 32'b1;
|
|
|
|
`assert("alu : 1 >> 1", out, 0)
|
|
|
|
in_a = 32'b10;
|
|
|
|
`assert("alu : 2 >> 1", out, 1)
|
|
|
|
in_a = 32'b11;
|
|
|
|
`assert("alu : 3 >> 2", out, 1)
|
|
|
|
in_a = 32'b11110;
|
|
|
|
in_b = 32'b1;
|
|
|
|
`assert("alu : 30 >> 1", out, 32'b1111)
|
|
|
|
in_a = 32'b10000000000000000000000000000000;
|
|
|
|
in_b = 32'b11111;
|
|
|
|
`assert("alu : 1000...000 >> 31", out, 32'b00000000000000000000000000000001)
|
|
|
|
in_a = 32'b10000000111100000000000111111111;
|
|
|
|
in_b = 32'b11111;
|
|
|
|
`assert("alu : 1000..111 >> 31", out, 32'b00000000000000000000000000000001)
|
|
|
|
|
2023-10-24 10:36:34 +00:00
|
|
|
// ALU - arithmetic right shift
|
2023-10-24 10:39:42 +00:00
|
|
|
func = SRA;
|
2023-10-24 10:36:34 +00:00
|
|
|
in_a = 32'b1;
|
|
|
|
in_b = 32'b1;
|
|
|
|
`assert("alu : 1 >>> 1", out, 0)
|
|
|
|
in_a = 32'b10;
|
|
|
|
`assert("alu : 2 >>> 1", out, 1)
|
|
|
|
in_a = 32'b11;
|
|
|
|
`assert("alu : 3 >>> 2", out, 1)
|
|
|
|
in_a = 32'b11110;
|
|
|
|
in_b = 32'b1;
|
|
|
|
`assert("alu : 30 >>> 1", out, 32'b1111)
|
|
|
|
in_a = 32'b10000000000000000000000000000000;
|
|
|
|
in_b = 32'b11111;
|
|
|
|
`assert("alu : 1000...000 >>> 31", out, 32'b11111111111111111111111111111111)
|
|
|
|
in_a = 32'b10000000111100000000000111111111;
|
|
|
|
in_b = 32'b11111;
|
|
|
|
`assert("alu : 1000..111 >>> 31", out, 32'b11111111111111111111111111111111)
|
|
|
|
|
|
|
|
// ALU - or
|
2023-10-24 10:39:42 +00:00
|
|
|
func = OR;
|
2023-10-24 10:36:34 +00:00
|
|
|
in_a = 32'b0;
|
|
|
|
in_b = 32'b0;
|
|
|
|
`assert("alu : 0 | 0", out, 32'b00000000000000000000000000000000)
|
|
|
|
in_a = 32'b1;
|
|
|
|
`assert("alu : 1 | 0", out, 32'b00000000000000000000000000000001)
|
|
|
|
in_a = 32'b0;
|
|
|
|
in_b = 32'b1;
|
|
|
|
`assert("alu : 0 | 1", out, 32'b00000000000000000000000000000001)
|
|
|
|
in_a = 32'b11111111111111111111111111111111;
|
|
|
|
in_b = 32'b11111111111111111111111111111111;
|
|
|
|
`assert("alu : MAX_INT | MAX_INT", out, 32'b11111111111111111111111111111111)
|
|
|
|
in_a = 32'b00000000000000000000000000000000;
|
|
|
|
in_b = 32'b11111111111111111111111111111111;
|
|
|
|
`assert("alu : 0 | MAX_INT", out, 32'b11111111111111111111111111111111)
|
|
|
|
in_a = 32'b00000011001000010001000011000000;
|
|
|
|
in_b = 32'b10101111001011101110111111111011;
|
|
|
|
`assert("alu : 00000011001000010001000011000000 | 10101111001011101110111111111011", out, 32'b10101111001011111111111111111011)
|
|
|
|
|
|
|
|
// ALU - and
|
2023-10-24 10:39:42 +00:00
|
|
|
func = AND;
|
2023-10-24 10:36:34 +00:00
|
|
|
in_a = 32'b0;
|
|
|
|
in_b = 32'b0;
|
|
|
|
`assert("alu : 0 & 0", out, 32'b00000000000000000000000000000000)
|
|
|
|
in_a = 32'b1;
|
|
|
|
`assert("alu : 1 & 0", out, 32'b00000000000000000000000000000000)
|
|
|
|
in_a = 32'b0;
|
|
|
|
in_b = 32'b1;
|
|
|
|
`assert("alu : 0 & 1", out, 32'b00000000000000000000000000000000)
|
|
|
|
in_a = 32'b11111111111111111111111111111111;
|
|
|
|
in_b = 32'b11111111111111111111111111111111;
|
|
|
|
`assert("alu : MAX_INT & MAX_INT", out, 32'b11111111111111111111111111111111)
|
|
|
|
in_a = 32'b00000000000000000000000000000000;
|
|
|
|
in_b = 32'b11111111111111111111111111111111;
|
|
|
|
`assert("alu : 0 & MAX_INT", out, 32'b00000000000000000000000000000000)
|
|
|
|
in_a = 32'b00000011001000010001000011000000;
|
|
|
|
in_b = 32'b10101111001011101110111111111011;
|
|
|
|
`assert("alu : 00000011001000010001000011000000 & 10101111001011101110111111111011", out, 32'b00000011001000000000000011000000)
|
2023-10-24 01:49:29 +00:00
|
|
|
|
2023-10-23 08:34:37 +00:00
|
|
|
`end_message
|
2023-10-23 05:15:21 +00:00
|
|
|
end
|
2023-10-20 09:48:18 +00:00
|
|
|
|
|
|
|
endmodule : tb_alu
|