-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU_topfile.v
More file actions
164 lines (152 loc) · 3.52 KB
/
CPU_topfile.v
File metadata and controls
164 lines (152 loc) · 3.52 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
module CPU_controller
(
input wire Z,
input wire C,
input wire clk,
input wire reset,
input wire [3:0]opcode,
output reg Load_IR,
output reg Inc_PC,
output reg Load_PC,
output reg Load_A,
output reg Load_B,
output reg Load_C,
output reg wen_DM,
output reg Sel2,
output reg [3:0]opcode_ALU,
output reg [1:0]alu_mode
);
reg [1:0] present_state;
reg temp1,temp2;
reg [1:0] next_state;
parameter rst = 2'b00, fetch = 2'b01, execute =2'b10;
always @(posedge clk or reset)
begin
if(reset == 1'b1)
present_state <= rst;
else
present_state <= next_state;
end
always @ (posedge clk) begin
if(present_state == rst)
begin
opcode_ALU <= 4'd0;
Load_IR = 1'b0;
Load_A =1'b0;
Sel2 <= 1'b0;
Load_B <= 1'b0;
Load_C = 1'b0;
Load_PC = 1'b0;
Inc_PC <= 1'b0;
wen_DM = 1'b0;
alu_mode = 2'b11;
next_state = fetch;
end
else if(present_state == fetch)
begin
Load_IR = 1'b1;
Load_A =1'b0;
Sel2 <= 1'b0;
Load_B <= 1'b0;
Load_C = 1'b0;
Load_PC = 1'b0;
Inc_PC <= 1'b0;
wen_DM = 1'b0;
alu_mode = 2'b11;
opcode_ALU <= 4'b0000;
next_state = execute;
end
else if(present_state == execute)
begin
Load_IR <= 1'b0;
case (opcode)
4'b0001 : begin //ADD operation.
opcode_ALU <= opcode;
alu_mode = 2'b00;
Load_A = 1'b1;
Load_B =1'b1;
Load_C = 1'b1;
wen_DM = 1'b1;
Sel2 = 1'b1;
Load_PC <= 1'b1;
Inc_PC <= 1'b1;
if(C==1'b1)temp1 = 1'b1;
else temp1 =1'b0;
if(Z==1'b1)temp2 = 1'b1;
else temp2 =1'b0;
end
4'b1000 : begin
opcode_ALU <= opcode; // subtract.
alu_mode = 2'b00;
Load_A = 1'b1;
Load_B =1'b1;
Load_C = 1'b1;
wen_DM = 1'b1;
Sel2 = 1'b1;
Load_PC <= 1'b1;
Inc_PC <= 1'b1;
if(C==1'b1)temp1 = 1'b1;
else temp1 =1'b0;
if(Z==1'b1)temp2 = 1'b1;
else temp2 =1'b0;
end
4'b0011 : begin // Store data from Register C to DM
opcode_ALU <= 4'b0000;
alu_mode <= 2'b11;
Load_C <= 1'b0;
wen_DM = 1'b1;
Load_B = 1'b0;
Load_A = 1'b0;
Load_PC = 1'b0;
Inc_PC = 1'b1;
Sel2 = 1'b1;
end
4'b0100 : begin
opcode_ALU <= 4'b0000;
alu_mode <= 2'b11;
Load_C <= 1'b1;
wen_DM = 1'b0;
Load_B = 1'b1;
Load_A = 1'b1;
Load_PC = 1'b1;
Inc_PC = 1'b1;
Sel2 = 1'b0; // Load an immediate value into register C.
end
4'b0101 : begin // Load a value specified by the address in the IR to A from DM.
opcode_ALU <= 4'b0000;
alu_mode <= 2'b11;
Load_C <= 1'b0;
wen_DM = 1'b0;
Load_B = 1'b0;
Load_A = 1'b1;
Load_PC = 1'b0;
Inc_PC = 1'b1;
Sel2 = 1'b1;
end
4'b0110 : begin // Load a value specified by the address to B from DM.
opcode_ALU =4'b0000;
alu_mode <= 2'b11;
Load_C <= 1'b0;
wen_DM = 1'b0;
Load_B = 1'b1;
Load_A = 1'b0;
Load_PC = 1'b0;
Inc_PC = 1'b1;
Sel2 = 1'b1;
end
4'b1001: begin //and
opcode_ALU = 4'b0000;
alu_mode <= 2'b01;
Load_C <= 1'b1;
wen_DM = 1'b0;
Load_B = 1'b1;
Load_A = 1'b1;
Load_PC = 1'b0;
Inc_PC = 1'b1;
Sel2 = 1'b1;
end
endcase
next_state = fetch;
end
end
endmodule