-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.v
127 lines (121 loc) · 4.25 KB
/
project.v
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
module project(input clk, input [3:0] A, input [3:0] B,
input [2:0] opcode, output reg [3:0] C,
output reg carr, output reg sign, output reg zero);
// Will be using state to indicate state of the machine.
reg [1:0] state = 0;
// Negative numbers will be represented in 2s complement.
reg signed [3:0] sub_result;
reg signed [3:0] sub_a;
reg signed [3:0] sub_b;
always @ (posedge clk) begin
case (state)
2'b00: begin
case (opcode)
3'b000: begin
C <= 4'b0000; //RESET operation
carr <= 1'b0;
sign <= 1'b0;
zero <= 1'b1;
end
3'b001: begin //XNOR operation on LSBs
C[0] <= ~(A[0] ^ B[0]);
zero <= C[0] == 1'b0;
end
3'b010: begin //SUB operation on LSBs
sub_a <= A;
sub_b <= B;
sub_result <= sub_b - sub_a;
C[0] <= sub_result[0];
zero <= C[0] == 1'b0;
end
3'b011: begin //NAND operation on LSBs
C[0] <= ~(A[0] & B[0]);
zero <= C[0] == 1'b0;
end
3'b100: begin //ADD operation on LSBs
{carr, C[0]} <= A[0] + B[0];
zero <= C[0] == 1'b0;
end
endcase
if (opcode != 3'b000) begin
state <= 2'b01;
end
end
2'b01: begin
case (opcode)
3'b001: begin //XNOR operation on next bit
C[1] <= ~(A[1] ^ B[1]);
zero <= zero & (C[1] == 1'b0);
end
3'b010: begin //SUB operation on next bit
sub_a <= A;
sub_b <= B;
sub_result <= sub_b - sub_a;
C[1] <= sub_result[1];
zero <= C[1] == 1'b0;
end
3'b011: begin //NAND operation on next bit
C[1] <= ~(A[1] & B[1]);
zero <= zero & (C[1] == 1'b0);
end
3'b100: begin //ADD operation on next bit
{carr, C[1]} <= A[1] + B[1] + carr;
zero <= zero & (C[1] == 1'b0);
end
endcase
state <= 2'b10;
end
2'b10: begin
case (opcode)
3'b001: begin //XNOR operation on next bit
C[2] <= ~(A[2] ^ B[2]);
zero <= zero & (C[2] == 1'b0);
end
3'b010: begin //SUB operation on next bit
sub_a <= A;
sub_b <= B;
sub_result <= sub_b - sub_a;
C[2] <= sub_result[2];
zero <= C[2] == 1'b0;
end
3'b011: begin //NAND operation on next bit
C[2] <= ~(A[2] & B[2]);
zero <= zero & (C[2] == 1'b0);
end
3'b100: begin //ADD operation on next bit
{carr, C[2]} <= A[2] + B[2] + carr;
zero <= zero & (C[2] == 1'b0);
end
endcase
state <= 2'b11;
end
2'b11: begin
case (opcode)
3'b001: begin //XNOR operation on MSBs
C[3] <= ~(A[3] ^ B[3]);
sign <= C[3];
zero <= C == 4'b0000;
end
3'b010: begin //SUB operation on MSBs
sub_a <= A;
sub_b <= B;
{carr, sub_result} <= sub_b - sub_a;
C[3] <= sub_result[3];
zero <= C == 4'b0000;
end
3'b011: begin //NAND operation on MSBs
C[3] <= ~(A[3] & B[3]);
sign <= C[3];
zero <= C == 4'b0000;
end
3'b100: begin //ADD operation on MSBs
{carr, C[3]} <= A[3] + B[3] + carr;
sign <= C[3];
zero <= C == 4'b0000;
end
endcase
state <= 2'b00;
end
endcase
end
endmodule