-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sv
287 lines (254 loc) · 8.22 KB
/
main.sv
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
module part2(
input logic Clock,
input logic Reset,
input logic Go,
input logic [7:0] DataIn,
output logic [7:0] DataResult,
output logic ResultValid
);
// lots of wires to connect our datapath and control
logic ld_a, ld_b, ld_r, ld_x, ld_c;
// TODO: Add other ld_* signals you need here.
logic ld_alu_out;
logic [1:0] alu_select_a, alu_select_b;
logic alu_op;
control C0(
.clk(Clock),
.reset(Reset),
.go(Go),
.ld_alu_out(ld_alu_out),
.ld_a(ld_a),
.ld_b(ld_b),
.ld_r(ld_r),
.ld_x(ld_x),
.ld_c(ld_c),
// TODO: Add other connections here.
.alu_select_a(alu_select_a),
.alu_select_b(alu_select_b),
.alu_op(alu_op),
.result_valid(ResultValid)
);
datapath D0(
.clk(Clock),
.reset(Reset),
.ld_alu_out(ld_alu_out),
.ld_a(ld_a),
.ld_b(ld_b),
.ld_r(ld_r),
.ld_x(ld_x),
.ld_c(ld_c),
// TODO: Add other connections here.
.alu_select_a(alu_select_a),
.alu_select_b(alu_select_b),
.alu_op(alu_op),
.data_in(DataIn),
.data_result(DataResult)
);
endmodule
module control(
input logic clk,
input logic reset,
input logic go,
output logic ld_a, ld_b, ld_c, ld_x, ld_r,
output logic ld_alu_out,
output logic [1:0] alu_select_a, alu_select_b,
output logic alu_op,
output logic result_valid
);
typedef enum logic [3:0] { S_LOAD_A_RST = 'd0,
S_LOAD_A = 'd1,
S_LOAD_A_WAIT = 'd2,
S_LOAD_B = 'd3,
S_LOAD_B_WAIT = 'd4,
// TODO: Add states to load other inputs here.
S_CYCLE_0 = 'd5,
S_CYCLE_1 = 'd6,
S_LOAD_C = 'd7,
S_LOAD_C_WAIT = 'd8,
S_LOAD_X = 'd9,
S_LOAD_X_WAIT = 'd10,
S_CYCLE_2 = 'd11,
S_CYCLE_3 = 'd12,
S_CYCLE_4 = 'd13} statetype;
statetype current_state, next_state;
// Next state logic aka our state table
always_comb begin
case (current_state)
S_LOAD_A_RST: next_state = go ? S_LOAD_A_WAIT : S_LOAD_A_RST; // Loop in current state until value is input
S_LOAD_A: next_state = go ? S_LOAD_A_WAIT : S_LOAD_A; // Loop in current state until value is input
S_LOAD_A_WAIT: next_state = go ? S_LOAD_A_WAIT : S_LOAD_B;
S_LOAD_B: next_state = go ? S_LOAD_B_WAIT : S_LOAD_B;
S_LOAD_B_WAIT: next_state = go ? S_LOAD_B_WAIT : S_LOAD_C;
S_LOAD_C: next_state = go ? S_LOAD_C_WAIT : S_LOAD_C;
S_LOAD_C_WAIT: next_state = go ? S_LOAD_C_WAIT : S_LOAD_X;
S_LOAD_X: next_state = go ? S_LOAD_X_WAIT : S_LOAD_X;
S_LOAD_X_WAIT: next_state = go ? S_LOAD_X_WAIT : S_CYCLE_0;
// TODO: Add states for other inputs here.
S_CYCLE_0: next_state = S_CYCLE_1;
// TODO: Add new states for the required operation.
S_CYCLE_1: next_state = S_CYCLE_2;
S_CYCLE_2: next_state = S_CYCLE_3;
S_CYCLE_3: next_state = S_CYCLE_4;
S_CYCLE_4: next_state = S_LOAD_A; // we will be done our two operations, start over after
default: next_state = S_LOAD_A_RST;
endcase
end // state_table
// output logic logic aka all of our datapath control signals
always_comb begin
// By default make all our signals 0
ld_alu_out = 1'b0;
ld_a = 1'b0;
ld_b = 1'b0;
ld_r = 1'b0;
ld_x = 1'b0;
ld_c = 1'b0;
alu_select_a = 2'b00;
alu_select_b = 2'b00;
alu_op = 1'b0;
result_valid = 1'b0;
case (current_state)
S_LOAD_A_RST: begin
ld_a = 1'b1;
end
S_LOAD_A: begin
ld_a = 1'b1;
result_valid = 1'b1;
end
S_LOAD_B: begin
ld_b = 1'b1;
end
S_LOAD_C: begin
ld_c = 1'b1;
end
S_LOAD_X: begin
ld_x = 1'b1;
end
// S_CYCLE_0: begin // Do A <- A * A
// ld_alu_out = 1'b1;
// ld_a = 1'b1; // store result back into A
// alu_select_a = 2'b00; // Select register A
// alu_select_b = 2'b00; // Also select register A
// alu_op = 1'b1; // Do multiply operation
// end
// S_CYCLE_1: begin
// ld_r = 1'b1; // store result in result register
// alu_select_a = 2'b00; // Select register A
// alu_select_b = 2'b01; // Select register B
// alu_op = 1'b0; // Do Add operation
// end
S_CYCLE_0: begin // Do A <- A * x
ld_alu_out = 1'b1;
ld_a = 1'b1;
alu_select_a = 2'b00;
alu_select_b = 2'b11;
alu_op = 1'b1;
end
S_CYCLE_1: begin // Do A <- (A * x) * x
ld_alu_out = 1'b1;
ld_a = 1'b1;
alu_select_a = 2'b00;
alu_select_b = 2'b11;
alu_op = 1'b1;
end
S_CYCLE_2: begin // Do B <- B * x
ld_alu_out = 1'b1;
ld_b = 1'b1;
alu_select_a = 2'b01;
alu_select_b = 2'b11;
alu_op = 1'b1;
end
S_CYCLE_3: begin // Do A <- (A * x^2) + (B*x)
ld_alu_out = 1'b1;
ld_a = 1'b1;
alu_select_a = 2'b00;
alu_select_b = 2'b01;
alu_op = 1'b0;
end
S_CYCLE_4: begin // Do R <- (A * x^2 + B*x) + C
ld_r = 1'b1;
alu_select_a = 2'b00;
alu_select_b = 2'b10;
alu_op = 1'b0;
end
// We don't need a default case since we already made sure all of our outputs were assigned a value at the start of the always block.
endcase
end // enable_signals
// current_state logicisters
always_ff@(posedge clk) begin
if(reset)
current_state <= S_LOAD_A_RST;
else
current_state <= next_state;
end // state_FFS
endmodule
module datapath(
input logic clk,
input logic reset,
input logic [7:0] data_in,
input logic ld_alu_out,
input logic ld_a, ld_b,
// TODO: Add additional signals from control path here.
input logic ld_c, ld_x,
input logic ld_r,
input logic alu_op,
input logic [1:0] alu_select_a, alu_select_b,
output logic [7:0] data_result
);
// input logic logicisters
logic [7:0] a, b, c, x;
// output logic of the alu
logic [7:0] alu_out;
// alu input logic muxes
logic [7:0] alu_a, alu_b;
// registers a and b with associated logic
always_ff @(posedge clk) begin
if(reset) begin
a <= 8'b0;
b <= 8'b0;
c <= 8'b0;
x <= 8'b0;
end
else begin
if(ld_a) a <= ld_alu_out ? alu_out : data_in; // load alu_out if load_alu_out signal is high, otherwise load from data_in
if(ld_b) b <= ld_alu_out ? alu_out : data_in;
//TODO: Add signals to set additional registers.
// Note that only registers A and B have a mux to load values from data_in or from alu_out
if (ld_c) c <= ld_alu_out ? alu_out : data_in;
if (ld_x) x <= ld_alu_out ? alu_out : data_in;
end
end
// output logic result logicister
always@(posedge clk) begin
if(reset) begin
data_result <= 8'b0;
end
else
if(ld_r)
data_result <= alu_out;
end
// The ALU input logic multiplexers
always_comb begin
case (alu_select_a)
2'b00: alu_a = a;
2'b01: alu_a = b;
2'b10: alu_a = c;
2'b11: alu_a = x;
default: alu_a = 8'b0;
endcase
case (alu_select_b)
2'b00: alu_b = a;
2'b01: alu_b = b;
2'b10: alu_b = c;
2'b11: alu_b = x;
default: alu_b = 8'b0;
endcase
end
// The ALU
always_comb begin : ALU
case (alu_op)
0: alu_out = alu_a + alu_b; //performs addition
1: alu_out = alu_a * alu_b; //performs multiplication
default: alu_out = 8'b0;
endcase
end
endmodule