summaryrefslogtreecommitdiff
path: root/alu_tb.v
blob: 6afafbcaadc19a28850f9ec513ad4f3242451f45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
`timescale 1 ns / 100 ps

module alu_tb ();
	reg [3:0] op;
	reg [15:0] x;
	reg [15:0] y;
	wire [15:0] result;
	reg [15:0] expected_result;
	wire zero;
	reg expected_zero;
	wire carry;
	reg expected_carry;

	initial begin
		$dumpfile("alu_tb.vcd");
		$dumpvars(0, alu_tb);

		x = 16'h0123;
		y = 16'h1234;

		op = 4'h0;  // add
		expected_result = 16'h1357;
		expected_zero = 1'b0;
		expected_carry = 1'b0;

		#2
		op = 4'h1;  // subtract
		expected_result = 16'hEEEF;
		expected_carry = 1'b1;
		expected_zero = 1'b0;

		#2
		y = 16'h0123;
		expected_result = 16'h0;
		expected_zero = 1'b1;
		expected_carry = 1'b0;

		#2
		y = 16'h1234;
		op = 4'h2;  // multiply
		expected_result = 16'hB11C;
		expected_zero = 1'b0;
		expected_carry = 1'b0;

		#2
		x = 16'h3E58;
		y = 16'h0078;
		op = 4'h3;  // divide
		expected_result = 16'h0085;

		#2
		x = 16'hAF74;
		y = 16'h7CC7;
		op = 4'h4;  // and
		expected_result = 16'h2C44;

		#2
		op = 4'h5;  // or
		expected_result = 16'hFFF7;

		#2
		op = 4'h6;  // xor
		expected_result = 16'hD3B3;

		#5
		$finish;
	end

	alu alu_inst (
		.op(op),
		.x(x),
		.y(y),
		.result(result),
		.zero(zero),
		.carry(carry)
	);
endmodule