Repository navigation
Expand file tree
/
Copy pathdatapath.v
More file actions
427 lines (329 loc) · 10.9 KB
/
Copy pathdatapath.v
File metadata and controls
427 lines (329 loc) · 10.9 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
`timescale 1ns / 1ps
`include "cpu_constants.vh"
//============================================================
// Project : FlexRISC16
// Module : Datapath
// Description : Processor Datapath
//============================================================
module datapath(
input wire clk,
input wire rst,
//--------------------------------------------------------
// Control Word
//--------------------------------------------------------
input wire [15:0] control_word,
//--------------------------------------------------------
// Outputs to Control Unit
//--------------------------------------------------------
output wire [3:0] opcode,
output wire [1:0] instr_class,
output wire is_load,
output wire is_store,
output wire is_branch,
output wire is_immediate,
output wire zero_flag,
output wire carry_flag,
//----------------------------------------------------
// Debug Signals
//----------------------------------------------------
output wire [`ADDR_WIDTH-1:0] debug_pc,
output wire [`DATA_WIDTH-1:0] debug_instruction,
output wire [`DATA_WIDTH-1:0] debug_alu_result,
output wire [`DATA_WIDTH-1:0] debug_writeback
);
//============================================================
// Internal Signals
//============================================================
//------------------------------------------------------------
// Program Counter
//------------------------------------------------------------
wire [`ADDR_WIDTH-1:0] pc;
wire [`ADDR_WIDTH-1:0] next_pc;
wire branch_taken;
//------------------------------------------------------------
// Instruction Memory
//------------------------------------------------------------
wire [`DATA_WIDTH-1:0] instruction;
//------------------------------------------------------------
// Instruction Register
//------------------------------------------------------------
wire [`DATA_WIDTH-1:0] ir_instruction;
//============================================================
// DECODE STAGE SIGNALS
//============================================================
// Instruction Fields
wire [3:0] rd;
wire [3:0] rs1;
wire [3:0] rs2;
wire [3:0] write_reg_addr;
// Decoded Information
//wire [1:0] instr_class;
wire [3:0] alu_func;
//wire is_load;
//wire is_store;
//wire is_branch;
//wire is_immediate;
wire alu_src;
// Immediate
wire [`DATA_WIDTH-1:0] immediate;
// Register File
wire [`DATA_WIDTH-1:0] reg_data1;
wire [`DATA_WIDTH-1:0] reg_data2;
// Write Back
wire [`DATA_WIDTH-1:0] wb_data;
// ALU Control
wire [3:0] alu_operation;
wire [`DATA_WIDTH-1:0] alu_operand_b;
//============================================================
// Execute Stage Signals
//============================================================
// ALU Outputs
wire [`DATA_WIDTH-1:0] alu_result;
wire carry;
wire zero;
wire negative;
wire overflow;
// STATUS REGISTER
wire sr_zero_flag;
wire sr_carry_flag;
wire sr_negative_flag;
wire sr_overflow_flag;
// Branch
wire [`ADDR_WIDTH-1:0] branch_target;
//============================================================
// Memory Stage Signals
//============================================================
wire [`DATA_WIDTH-1:0] memory_data;
//------------------------------------------------------------
// Control Signals
//------------------------------------------------------------
wire pc_enable;
wire ir_load;
wire alu_enable;
wire mem_read;
wire mem_write;
wire reg_write;
wire flag_write;
wire [2:0] wb_sel;
//------------------------------------------------------------
// PC + 1
//------------------------------------------------------------
wire [`ADDR_WIDTH-1:0] pc_plus_one;
//============================================================
// Decode Control Word
//============================================================
assign pc_enable = control_word[`CTRL_PC_EN];
assign ir_load = control_word[`CTRL_IR_LOAD];
assign alu_enable = control_word[`CTRL_ALU_EN];
assign mem_read = control_word[`CTRL_MEM_RD];
assign mem_write = control_word[`CTRL_MEM_WR];
assign reg_write = control_word[`CTRL_REG_WR];
assign flag_write = control_word[`CTRL_FLAG_WR];
assign wb_sel = control_word[`CTRL_WB_SEL_MSB:`CTRL_WB_SEL_LSB];
assign write_reg_addr = rd;
//============================================================
// FETCH STAGE
//============================================================
//------------------------------------------------------------
// Program Counter
//------------------------------------------------------------
program_counter PC(
.clk(clk),
.rst(rst),
.pc_enable(pc_enable),
.stall(1'b0),
.pc_load(branch_taken),
.pc_next(next_pc),
.pc(pc)
);
//------------------------------------------------------------
// PC + 1
//------------------------------------------------------------
assign pc_plus_one = pc + 1'b1;
//------------------------------------------------------------
// Instruction Memory
//------------------------------------------------------------
instruction_memory IMEM(
.address(pc),
.instruction(instruction)
);
//------------------------------------------------------------
// Instruction Register
//------------------------------------------------------------
instruction_register IR(
.clk(clk),
.rst(rst),
.enable(ir_load),
.stall(1'b0),
.flush(1'b0),
.instruction_in(instruction),
.instruction_out(ir_instruction)
);
//============================================================
// DECODE STAGE
//============================================================
//------------------------------------------------------------
// Instruction Decoder
//------------------------------------------------------------
instruction_decoder DECODER(
.instruction(ir_instruction),
.opcode(opcode),
.rd(rd),
.rs1(rs1),
.rs2(rs2),
.instr_class(instr_class),
.alu_func(alu_func),
.is_load(is_load),
.is_store(is_store),
.is_branch(is_branch),
.is_immediate(is_immediate),
.alu_src(alu_src)
);
//------------------------------------------------------------
// Immediate Generator
//------------------------------------------------------------
immediate_generator IMMGEN(
.instruction(ir_instruction),
.instr_class(instr_class),
.immediate(immediate)
);
//------------------------------------------------------------
// Register File
//------------------------------------------------------------
register_file RF(
.clk(clk),
.rst(rst),
.we(reg_write),
.wr_addr(write_reg_addr),
.wr_data(wb_data),
.rd_addr1(rs1),
.rd_addr2(rs2),
.rd_data1(reg_data1),
.rd_data2(reg_data2)
);
//------------------------------------------------------------
// ALU Control
//------------------------------------------------------------
alu_control ALUCTRL(
.alu_enable(alu_enable),
.instr_class(instr_class),
.alu_func(alu_func),
.alu_operation(alu_operation)
);
//============================================================
// EXECUTE STAGE
//============================================================
//------------------------------------------------------------
// ALU Operand B Multiplexer
//------------------------------------------------------------
alu_operand_mux ALU_MUX(
.sel(alu_src),
.reg_data(reg_data2),
.immediate(immediate),
.operand_b(alu_operand_b)
);
//------------------------------------------------------------
// Arithmetic Logic Unit
//------------------------------------------------------------
alu ALU(
.operand_a(reg_data1),
.operand_b(alu_operand_b),
.alu_operation(alu_operation),
.result(alu_result),
.carry(carry),
.zero(zero),
.negative(negative),
.overflow(overflow)
);
//------------------------------------------------------------
// Status Register
//------------------------------------------------------------
status_register STATUS(
.clk(clk),
.rst(rst),
.enable(flag_write),
.carry_in(carry),
.zero_in(zero),
.negative_in(negative),
.overflow_in(overflow),
.carry_out(sr_carry_flag),
.zero_out(sr_zero_flag),
.negative_out(sr_negative_flag),
.overflow_out(sr_overflow_flag)
);
//------------------------------------------------------------
// Branch Unit
//------------------------------------------------------------
branch_unit BRANCH(
.opcode(opcode),
.carry_flag(sr_carry_flag),
.zero_flag(sr_zero_flag),
.negative_flag(sr_negative_flag),
.overflow_flag(sr_overflow_flag),
.branch_taken(branch_taken)
);
//============================================================
// MEMORY STAGE
//============================================================
//------------------------------------------------------------
// Data Memory
//------------------------------------------------------------
data_memory DMEM (
.clk(clk),
.read_enable(mem_read),
.write_enable(mem_write),
// Address calculated by the ALU
.address(alu_result[`ADDR_WIDTH-1:0]),
// Data to store comes from RS2
.write_data(reg_data2),
// Data read from memory
.read_data(memory_data)
);
//============================================================
// WRITE BACK STAGE
//============================================================
//------------------------------------------------------------
// Write Back Multiplexer
//------------------------------------------------------------
write_back_mux WBMUX(
.wb_sel(wb_sel),
.alu_data(alu_result),
.mem_data(memory_data),
.pc_data({{(`DATA_WIDTH-`ADDR_WIDTH){1'b0}},pc_plus_one}),
.imm_data(immediate),
.wb_data(wb_data)
);
//============================================================
// NEXT PC LOGIC
//============================================================
//------------------------------------------------------------
// Branch Target
//------------------------------------------------------------
//------------------------------------------------------------
// PC Relative Branch Address
//------------------------------------------------------------
assign branch_target = pc_plus_one + immediate[`ADDR_WIDTH-1:0];
//------------------------------------------------------------
// Next PC MUX Selection
//------------------------------------------------------------
next_pc_mux NPCMUX(
.pc_load(branch_taken),
.pc_plus_one(pc_plus_one),
.branch_target(branch_target),
.next_pc(next_pc)
);
//------------------------------------------------------------
// Datapath Outputs
//------------------------------------------------------------
// Status flags come from the Status Register
// through the Branch Unit.
assign zero_flag = sr_zero_flag;
assign carry_flag = sr_carry_flag;
//----------------------------------------------------
// Debug Signals
//----------------------------------------------------
assign debug_pc = pc;
assign debug_instruction = ir_instruction;
assign debug_alu_result = alu_result;
assign debug_writeback = wb_data;
endmodule