Repository navigation
Expand file tree
/
Copy pathPIPE_CPU.v
More file actions
535 lines (492 loc) · 20.7 KB
/
Copy pathPIPE_CPU.v
File metadata and controls
535 lines (492 loc) · 20.7 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//+FHDR****************************************************************
// ECE department, TAMU
// --------------------------------------------------------------------
// FILE NAME : PIPE_CPU.v
// AUTHER : Jiafan Wang
// DATE : 12/21/2015
// VERSION : 1.0
// PURPOSE : the kernel of a pipeline CPU with five stages
// --------------------------------------------------------------------
// ABSTRACT
//
// --------------------------------------------------------------------
`timescale 1ns / 1ps
`include "DEFINE_CPU.v"
`ifndef PIPE_CPU_V
`define PIPE_CPU_V
module PIPE_CPU(
clk,
enable,
rst_n,
start,
i_datain,
d_datain,
// output
nxt,
i_addr,
d_addr,
d_we,
d_dataout,
io_status,
io_control,
io_datainA,
io_datainB,
io_dataoutA,
io_dataoutB
);
parameter INSTRT_ADDR_WIDTH = 8,
INSTRT_DATA_WIDTH = 16,
D_MEM_ADDR_WIDTH = 8,
D_MEM_DATA_WIDTH = 16,
GENERAL_REG_WIDTH = 16,// width of the internal general registers
PC_MEM_ADDR_WIDTH = 8;
parameter MSB_OP_16B = 16,
OP_WIDTH_5B = 5,
MSB_OPER1_11B = 11,
MSB_OPER2_8B = 8,
MSB_OPER3_4B = 4,
MSB_VAL2_7B = 7,
MSB_VAL3_3B = 3;
input clk;
input enable;
input rst_n;
input start;
input [INSTRT_DATA_WIDTH-1:0] i_datain; //input instruction data
input [D_MEM_DATA_WIDTH-1:0] d_datain; //input memory data
output nxt;
output [INSTRT_ADDR_WIDTH-1:0] i_addr; //output instruction address
output [D_MEM_ADDR_WIDTH-1:0] d_addr; //output memory data address
output d_we; //memory read or write signal, 1: write
output [D_MEM_DATA_WIDTH-1:0] d_dataout; //output memory data
// for I/O control and data in&out
input [GENERAL_REG_WIDTH-1:0] io_status;
input [GENERAL_REG_WIDTH-1:0] io_datainA;
input [GENERAL_REG_WIDTH-1:0] io_datainB;
output [GENERAL_REG_WIDTH-1:0] io_control;
output [GENERAL_REG_WIDTH-1:0] io_dataoutA;
output [GENERAL_REG_WIDTH-1:0] io_dataoutB;
reg cf_buf;
reg [GENERAL_REG_WIDTH-1:0] ALUo;
reg state, next_state, nxt;
reg zf, nf, cf, dw; //flag registers
reg [PC_MEM_ADDR_WIDTH-1:0] pc;
reg [GENERAL_REG_WIDTH-1:0] id_ir, ex_ir, mem_ir, wb_ir;// instruction registers
reg [GENERAL_REG_WIDTH-1:0] reg_A, reg_B, reg_C, reg_C1, smdr, smdr1;
reg [GENERAL_REG_WIDTH-1:0] gr[7:0];
wire branch_flag;
assign io_control = gr[1];
assign io_dataoutA = gr[2];
assign io_dataoutB = gr[3];
//*********** Facilitate code learning ***********//
wire [4:0] code_type;
wire [2:0] oper1_r1;
wire [3:0] oper2_r2;
wire oper2_is_val;
wire [3:0] oper3_r3;
wire oper3_is_val;
assign code_type = id_ir[MSB_OP_16B-1:MSB_OPER1_11B];
assign oper1_r1 = id_ir[MSB_OPER1_11B-1:MSB_OPER2_8B];
assign oper2_r2 = id_ir[MSB_OPER2_8B-1:MSB_OPER3_4B];
assign oper3_r3 = id_ir[MSB_OPER3_4B-1:0];
assign oper2_is_val = id_ir[MSB_OPER2_8B-1];
assign oper3_is_val = id_ir[MSB_OPER3_4B-1];
//************* CPU Control *************//
always @(posedge clk)
begin
if (!rst_n)
state <= `idle;
else
state <= next_state;
end
//************* CPU Control *************//
always @(*)
begin
case (state)
`idle :
if ((enable == 1'b1) && (start == 1'b1)) begin
nxt <= 1'b0;
next_state <= `exec;
end
else begin
nxt <= 1'b0;
next_state <= `idle;
end
`exec :
if ((enable == 1'b0) || (wb_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `HALT)) begin
nxt <= 1'b1;
next_state <= `idle;
end
else begin
nxt <= 1'b0;
next_state <= `exec;
end
endcase
end
//************* IF *************//
assign i_addr = pc;
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
id_ir <= {`NOP, 11'b000_0000_0000};
pc <= 8'b0000_0000;
end
else if (state ==`exec)
begin
id_ir <= i_datain;
if(branch_flag)
pc <= reg_C[PC_MEM_ADDR_WIDTH-1:0];
else
pc <= pc + 1;
end
end
//************* ID *************//
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
ex_ir <= {`NOP, 11'b000_0000_0000};
reg_A <= 16'b0000_0000_0000_0000;
reg_B <= 16'b0000_0000_0000_0000;
smdr <= 16'b0000_0000_0000_0000;
end
else if (state == `exec)
begin
ex_ir <= id_ir;
if (id_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `STORE)
smdr <= gr[id_ir[MSB_OPER1_11B-1:MSB_OPER2_8B]];
else
smdr <= smdr;
if (id_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `JUMP)
reg_A <= 16'b0000_0000_0000_0000;
else if (I_R1_TYPE(id_ir[MSB_OP_16B-1:MSB_OPER1_11B]))
reg_A <= gr[id_ir[MSB_OPER1_11B-1:MSB_OPER2_8B]];
else if (I_R2_TYPE(id_ir[MSB_OP_16B-1:MSB_OPER1_11B]))
reg_A <= gr[id_ir[MSB_VAL2_7B-1:MSB_OPER3_4B]];
else
reg_A <= reg_A;
if (I_V3_TYPE(id_ir[MSB_OP_16B-1:MSB_OPER1_11B]))
reg_B <= {12'b0000_0000_0000, id_ir[MSB_OPER3_4B-1:0]};
else if (I_ZEROV2V3_TYPE(id_ir[MSB_OP_16B-1:MSB_OPER1_11B]))
reg_B <= {8'b0000_0000, id_ir[MSB_OPER2_8B-1:0]};
else if (I_V2V3ZERO_TYPE(id_ir[MSB_OP_16B-1:MSB_OPER1_11B]))
reg_B <= {id_ir[MSB_OPER2_8B-1:0], 8'b0000_0000};
else if (I_R3_TYPE(id_ir[MSB_OP_16B-1:MSB_OPER1_11B]))
reg_B <= gr[id_ir[MSB_VAL3_3B-1:0]];
else
reg_B <= reg_B;
end
end
//************* EX *************//
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
mem_ir <= {`NOP, 11'b000_0000_0000};
reg_C <= 16'b0000_0000_0000_0000;
smdr1 <= 16'b0000_0000_0000_0000;
dw <= 1'b0;
zf <= 1'b0;
nf <= 1'b0;
cf <= 1'b0;
end
else if (state == `exec)
begin
if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `LIOA)
reg_C <= io_datainA;
else if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `LIOB)
reg_C <= io_datainB;
else if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `LIOS)
reg_C <= io_status;
else
reg_C <= ALUo;
mem_ir <= ex_ir;
if ((ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `LDIH)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SUIH)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `ADD)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `ADDI)
//|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `ADDC)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SUB)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SUBI)
//|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SUBC)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `CMP)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `AND)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `OR)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `XOR)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SLL)
|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SRL))
//|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SLA)
//|| (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SRA))
begin
cf <= cf_buf;
if (ALUo == 16'b0000_0000_0000_0000)
zf <= 1'b1;
else
zf <= 1'b0;
if (ALUo[GENERAL_REG_WIDTH-1] == 1'b1)
nf <= 1'b1;
else
nf <= 1'b0;
end
else
begin
zf <= zf;
nf <= nf;
cf <= cf;
end
if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `STORE)
begin
dw <= 1'b1;
smdr1 <= smdr;
end
else
begin
dw <= 1'b0;
smdr1 <= smdr1;
end
end
end
always @(*)
begin
case(ex_ir[MSB_OP_16B-1:MSB_OPER1_11B])
`AND: {cf_buf, ALUo} <= {1'b0, reg_A & reg_B};
`OR: {cf_buf, ALUo} <= {1'b0, reg_A | reg_B};
`XOR: {cf_buf, ALUo} <= {1'b0, reg_A ^ reg_B};
`SLL: {cf_buf, ALUo} <= {cf_buf, reg_A << reg_B[MSB_OPER3_4B-1:0]};
`SRL: {cf_buf, ALUo} <= {cf_buf, reg_A >> reg_B[MSB_OPER3_4B-1:0]};
//`SLA: {cf_buf, ALUo} <= {cf_buf, reg_A <<< reg_B[MSB_OPER3_4B-1:0]};
//`SRA: {cf_buf, ALUo} <= {cf_buf, reg_A >>> reg_B[MSB_OPER3_4B-1:0]};
`SET: {cf_buf, ALUo} <= {1'b0, reg_B};
//`JUMP: {cf_buf, ALUo} <= {cf_buf, reg_B};
//`LDIH:
`ADD: {cf_buf, ALUo} <= reg_A + reg_B;
`ADDI: {cf_buf, ALUo} <= reg_A + reg_B;
//`ADDC: {cf_buf, ALUo} <= reg_A + reg_B + cf;
`SUB: {cf_buf, ALUo} <= reg_A - reg_B;
`SUBI: {cf_buf, ALUo} <= reg_A - reg_B;
`SUIH: {cf_buf, ALUo} <= reg_A - reg_B;
//`SUBC: {cf_buf, ALUo} <= reg_A - reg_B - cf;
`CMP: {cf_buf, ALUo} <= reg_A - reg_B;
//`LOAD:`STORE:`JMPR:
//`BZ:`BNZ:`BN:`BNN:`BC:`BNC:
default:{cf_buf, ALUo} <= {cf_buf, reg_A + reg_B};
endcase
// if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `AND)
// begin
// cf_buf <= 1'b0;
// ALUo <= reg_A & reg_B;
// end
// else if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `OR)
// begin
// cf_buf <= 1'b0;
// ALUo <= reg_A | reg_B;
// end
// else if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `XOR)
// begin
// cf_buf <= 1'b0;
// ALUo <= reg_A ^ reg_B;
// end
// else if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SLL)
// {cf_buf, ALUo[GENERAL_REG_WIDTH-1:0]} <= {cf, reg_A[GENERAL_REG_WIDTH-1:0]} << reg_B[MSB_OPER3_4B-1:0];
// else if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SRL)
// {ALUo[GENERAL_REG_WIDTH-1:0], cf_buf} <= {reg_A[GENERAL_REG_WIDTH-1:0], cf} >> reg_B[MSB_OPER3_4B-1:0];
// else if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SLA)
// {cf_buf, ALUo[GENERAL_REG_WIDTH-1:0]} <= {cf, reg_A[GENERAL_REG_WIDTH-1:0]} <<< reg_B[MSB_OPER3_4B-1:0];
// else if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SRA)
// {ALUo[GENERAL_REG_WIDTH-1:0], cf_buf} <= {reg_A[GENERAL_REG_WIDTH-1:0], cf} >>> reg_B[MSB_OPER3_4B-1:0];
// else if ((ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SUB)
// || (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SUBI)
// || (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `CMP)
// || (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SUIH))
// {cf_buf, ALUo} <= reg_A - reg_B;
// else if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `SUBC)
// {cf_buf, ALUo} <= reg_A - reg_B - cf;
// else if (ex_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `ADDC)
// {cf_buf, ALUo} <= reg_A + reg_B + cf;
// else
// {cf_buf, ALUo} <= reg_A + reg_B;
end
//************* MEM *************//
assign d_addr = reg_C[D_MEM_ADDR_WIDTH-1:0];
assign d_we = dw;
assign d_dataout = smdr1;
assign branch_flag = ((mem_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `JUMP)
|| (mem_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `JMPR)
|| ((mem_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `BZ) && (zf == 1'b1))
|| ((mem_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `BNZ) && (zf == 1'b0))
|| ((mem_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `BN) && (nf == 1'b1))
|| ((mem_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `BNN) && (nf == 1'b0))
|| ((mem_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `BC) && (cf == 1'b1))
|| ((mem_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `BNC) && (cf == 1'b0)));
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
wb_ir <= {`NOP, 11'b000_0000_0000};
reg_C1 <= 16'b0000_0000_0000_0000;
end
else if (state == `exec)
begin
wb_ir <= mem_ir;
if (mem_ir[MSB_OP_16B-1:MSB_OPER1_11B] == `LOAD)
reg_C1 <= d_datain;
else
reg_C1 <= reg_C;
end
end
//************* WB *************//
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
gr[0] <= 16'b0000_0000_0000_0000;
gr[1] <= 16'b0000_0000_0000_0000;
gr[2] <= 16'b0000_0000_0000_0000;
gr[3] <= 16'b0000_0000_0000_0000;
gr[4] <= 16'b0000_0000_0000_0000;
gr[5] <= 16'b0000_0000_0000_0000;
gr[6] <= 16'b0000_0000_0000_0000;
gr[7] <= 16'b0000_0000_0000_0000;
end
else if (state == `exec)
begin
if (I_REG_TYPE(wb_ir[MSB_OP_16B-1:MSB_OPER1_11B]))
gr[wb_ir[MSB_OPER1_11B-1:MSB_OPER2_8B]] <= reg_C1;
end
end
/**************select Y*****************/
// always@(*)
// begin
// case(select_y)
// 4'b0000: y <= reg_C;
// 4'b0001: y <= reg_A;
// 4'b0010: y <= reg_B;
// 4'b0011: y <= {pc, 8'b0000_0000};
// 4'b0100: y <= id_ir;
// 4'b0101: y <= smdr;
// 4'b0110: y <= reg_C1;
// 4'b0111: y <= smdr1;
// 4'b1000: y <= ex_ir;
// 4'b1001: y <= mem_ir;
// 4'b1010: y <= wb_ir;
// default: y <= reg_C;
// endcase
// end
/***************************************/
//***** Judge an instruction whether alter the value of a register *****//
function I_REG_TYPE;
input [OP_WIDTH_5B-1:0] op;
begin
I_REG_TYPE = ((op == `LOAD)
|| (op == `LDIH)
|| (op == `ADD)
|| (op == `ADDI)
//|| (op == `ADDC)
|| (op == `SUIH)
|| (op == `SUB)
|| (op == `SUBI)
//|| (op == `SUBC)
|| (op == `AND)
|| (op == `OR)
|| (op == `XOR)
|| (op == `SLL)
|| (op == `SRL)
//|| (op == `SLA)
//|| (op == `SRA)
|| (op == `LIOA)
|| (op == `LIOB)
|| (op == `LIOS)
|| (op == `SET));
end
endfunction
//************* R1 as reg_A *************//
function I_R1_TYPE;
input [OP_WIDTH_5B-1:0] op;
begin
I_R1_TYPE = ((op == `LDIH)
|| (op == `SUIH)
|| (op == `ADDI)
|| (op == `SUBI)
|| (op == `JMPR)
|| (op == `BZ)
|| (op == `BNZ)
|| (op == `BN)
|| (op == `BNN)
|| (op == `BC)
|| (op == `BNC)
|| (op == `SET));
end
endfunction
//************* R2 as reg_A *************//
function I_R2_TYPE;
input [OP_WIDTH_5B-1:0] op;
begin
I_R2_TYPE = ((op == `LOAD)
|| (op == `STORE)
|| (op == `ADD)
//|| (op == `ADDC)
|| (op == `SUB)
//|| (op == `SUBC)
|| (op == `CMP)
|| (op == `AND)
|| (op == `OR)
|| (op == `XOR)
|| (op == `SLL)
|| (op == `SRL));
//|| (op == `SLA)
//|| (op == `SRA));
end
endfunction
//************* R3 as reg_B *************//
function I_R3_TYPE;
input [OP_WIDTH_5B-1:0] op;
begin
I_R3_TYPE = ((op == `ADD)
//|| (op == `ADDC)
|| (op == `SUB)
//|| (op == `SUBC)
|| (op == `CMP)
|| (op == `AND)
|| (op == `OR)
|| (op == `XOR));
end
endfunction
//************* val3 as reg_B *************//
function I_V3_TYPE;
input [OP_WIDTH_5B-1:0] op;
begin
I_V3_TYPE = ((op == `LOAD)
|| (op == `STORE)
|| (op == `SLL)
|| (op == `SRL));
//|| (op == `SLA)
//|| (op == `SRA));
end
endfunction
//************* {0000_0000,val2,val3} as reg_B *************//
function I_ZEROV2V3_TYPE;
input [OP_WIDTH_5B-1:0] op;
begin
I_ZEROV2V3_TYPE = ((op == `ADDI)
|| (op == `SUBI)
|| (op == `JUMP)
|| (op == `JMPR)
|| (op == `BZ)
|| (op == `BNZ)
|| (op == `BN)
|| (op == `BNN)
|| (op == `BC)
|| (op == `BNC)
|| (op == `SET));
end
endfunction
//************* {val2,val3,0000_0000} as reg_B *************//
function I_V2V3ZERO_TYPE;
input [OP_WIDTH_5B-1:0] op;
begin
I_V2V3ZERO_TYPE = ((op == `LDIH)
|| (op == `SUIH));
end
endfunction
endmodule
`endif//PIPE_CPU_V