-
Notifications
You must be signed in to change notification settings - Fork 0
/
c16.v.bak
546 lines (442 loc) · 10.8 KB
/
c16.v.bak
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
536
537
538
539
540
541
542
543
544
545
546
// Execute ops
module c16(
//////////// LED //////////
LEDG,
LEDR,
//////////// KEY //////////
CPU_RESET_n,
KEY,
//////////// SW //////////
SW,
//////////// SEG7 //////////
HEX0,
HEX1,
HEX2,
HEX3
);
//=======================================================
// PARAMETER declarations
//=======================================================
//=======================================================
// PORT declarations
//=======================================================
//////////// LED //////////
output [7:0] LEDG;
output [9:0] LEDR;
//////////// KEY //////////
input CPU_RESET_n;
input [3:0] KEY;
//////////// SW //////////
input [9:0] SW;
//////////// SEG7 //////////
output [6:0] HEX0;
output [6:0] HEX1;
output [6:0] HEX2;
output [6:0] HEX3;
wire clk = KEY[0]; // single step using key0
wire[15:0] reg_0;
wire[15:0] reg_1;
wire[15:0] reg_dbg;
wire[15:0] reg_write_value;
wire[2:0] reg_addr_0;
wire[2:0] reg_addr_1;
wire[2:0] reg_write_dest;
wire reg_write_enable;
registers(
.clk(clk),
.read_addr_0(reg_addr_0),
.read_addr_1(reg_addr_1),
.read_addr_dbg(SW[2:0]),
.write_addr(reg_write_dest),
.write_value(reg_write_value),
.write_enable(reg_write_enable),
.read_value_0(reg_0),
.read_value_1(reg_1),
.read_value_dbg(reg_dbg)
);
wire[15:0] instruction;
wire[15:0] branch_pc;
wire[15:0] fetch_pc;
wire pc_write_enable;
fetcher(
.clk(clk),
.pc_write_enable(pc_write_enable),
.pc_in(branch_pc),
.pc_out(fetch_pc),
.instruction(instruction)
);
wire[2:0] next_x_dest;
wire[3:0] next_x_op;
wire[15:0] next_x_pc;
wire[15:0] arg_0;
wire[15:0] arg_1;
decoder(
.clk(clk),
.instruction(instruction),
.pc_in(fetch_pc),
.reg_0(reg_0),
.reg_1(reg_1),
.execute_op(next_x_op),
.reg_addr_0(reg_addr_0),
.reg_addr_1(reg_addr_1),
.arg_0(arg_0),
.arg_1(arg_1),
.pc_out(next_x_pc),
.dest(next_x_dest)
);
executor(
.clk(clk),
.execute_op(next_x_op),
.arg_0(arg_0),
.arg_1(arg_1),
.dest_in(next_x_dest),
.pc_in(next_x_pc),
.dest_out(reg_write_dest),
.reg_value_out(reg_write_value),
.reg_write_enable(reg_write_enable),
.pc_value_out(branch_pc),
.pc_write_enable(pc_write_enable)
);
///////////////////
// debug support //
///////////////////
reg [15:0]debug;
assign LEDR = fetch_pc[9:0];
display(debug[15:12], HEX3);
display(debug[11:8], HEX2);
display(debug[7:4], HEX1);
display(debug[3:0], HEX0);
// what do we display
always @(*) begin
if (SW[3]) begin
debug = instruction;
end else begin
debug = reg_dbg;
end
end
endmodule
/////////////////////////
// REGISTER FILE //
/////////////////////////
module registers(clk, read_addr_0, read_addr_1, read_addr_dbg, write_addr, write_value, write_enable, read_value_0, read_value_1, read_value_dbg);
input[2:0] read_addr_0;
input[2:0] read_addr_1;
input[2:0] read_addr_dbg;
input[2:0] write_addr;
input write_enable;
input clk;
input[15:0] write_value;
output[15:0] read_value_0;
output[15:0] read_value_1;
output[15:0] read_value_dbg;
reg[15:0] rv0;
reg[15:0] rv1;
reg[15:0] rvdbg;
reg [15:0]regs[7:0];
initial begin
regs[0] = 0;
regs[1] = 0;
regs[2] = 0;
regs[3] = 0;
regs[4] = 0;
regs[5] = 0;
regs[6] = 0;
regs[7] = 0;
end
always @(*) begin
rv0 = regs[read_addr_0];
rv1 = regs[read_addr_1];
rvdbg = regs[read_addr_dbg];
if (write_enable && write_addr != 7) begin
if (write_addr == read_addr_0) begin
rv0 = write_value;
end
if (write_addr == read_addr_1) begin
rv1 = write_value;
end
if (write_addr == read_addr_dbg) begin
rvdbg = write_value;
end
end
end
always @(posedge clk) begin
if (write_enable && write_addr != 7) begin
regs[write_addr] <= write_value;
end
end
assign read_value_0 = rv0;
assign read_value_1 = rv1;
assign read_value_dbg = rvdbg;
endmodule
/////////////////////////
// FETCH STAGE //
/////////////////////////
module fetcher(clk, pc_write_enable, pc_in, pc_out, instruction);
input clk;
input pc_write_enable;
input[15:0] pc_in;
output[15:0] pc_out;
output[15:0] instruction;
reg[15:0] fetch_pc;
reg[15:0] next_fetch_pc;
wire [15:0]mem_out;
initial begin
fetch_pc = -1;
end
always @(*) begin
if(pc_write_enable == 1) begin
next_fetch_pc = pc_in;
end else begin
next_fetch_pc = fetch_pc + 1;
end
end
always @(posedge clk) begin
fetch_pc <= next_fetch_pc;
end
ram (next_fetch_pc, clk, 0, 0, mem_out);
assign instruction = mem_out;
assign pc_out = fetch_pc;
endmodule
/////////////////////////
// DECODE STAGE //
/////////////////////////
module decoder(clk, instruction, pc_in, reg_0, reg_1, execute_op, reg_addr_0, reg_addr_1, arg_0, arg_1, pc_out, dest);
parameter ADD = 4'h0;
parameter SET = 4'h1;
parameter NOP = 4'h2;
parameter SHFT = 4'h3;
parameter CALL = 4'h4;
parameter BRZ = 4'h5;
input clk;
input[15:0] instruction;
input[15:0] pc_in;
input[15:0] reg_0;
input[15:0] reg_1;
output[15:0] arg_0;
output[15:0] arg_1;
output[15:0] pc_out;
output[2:0] dest;
output[3:0] execute_op;
output[2:0] reg_addr_0;
output[2:0] reg_addr_1;
reg[3:0] execute_op_reg;
reg[15:0] arg_0_reg;
reg[15:0] arg_1_reg;
reg[15:0] pc_out_reg;
reg[2:0] dest_reg;
reg[3:0] execute_op_out;
reg[15:0] arg_0_out;
reg[15:0] arg_1_out;
reg[15:0] pc_out_out;
reg[2:0] dest_out;
// Used to implement brz
reg[2:0] reg_addr_1_reg;
wire [4:0] opcode = instruction[15:11];
wire [2:0] rd = instruction[10:8];
wire [15:0]imm5 = $signed(instruction[4:0]);
wire [15:0]imm8 = $signed(instruction[7:0]);
always @(*) begin
pc_out_reg = pc_in;
dest_reg = rd;
execute_op_reg = NOP;
arg_0_reg = 0;
arg_1_reg = 0;
reg_addr_1_reg = instruction[2:0];
case (opcode)
// Add, f = 0
5'b00000: begin
execute_op_reg = ADD;
arg_0_reg = reg_0;
arg_1_reg = imm5;
end
// Add, f = 1
5'b00001: begin
execute_op_reg = ADD;
arg_0_reg = reg_0;
arg_1_reg = reg_1;
end
// Slt, f = 0
5'b00100: begin
execute_op_reg = SET;
arg_0_reg = reg_0;
arg_1_reg = imm5;
end
// Slt, f = 1
5'b00101: begin
execute_op_reg = SET;
arg_0_reg = reg_0;
arg_1_reg = reg_1;
end
// Lea, f = 0
5'b11000: begin
execute_op_reg = ADD;
arg_0_reg = reg_0;
arg_1_reg = imm5;
end
// Lea, f = 1
5'b11001: begin
execute_op_reg = ADD;
arg_0_reg = pc_in;
arg_1_reg = imm8;
end
// Call, f = 0
5'b11010: begin
execute_op_reg = CALL;
arg_0_reg = reg_0;
arg_1_reg = imm5;
end
// Call, f = 1
5'b11011: begin
execute_op_reg = CALL;
arg_0_reg = pc_in;
arg_1_reg = imm8;
end
// brz, f = 0
5'b11110: begin
execute_op_reg = BRZ;
arg_0_reg = reg_0;
arg_1_reg = imm5;
reg_addr_1_reg = rd;
end
// brz, f = 1
5'b11111: begin
execute_op_reg = BRZ;
arg_0_reg = pc_in;
arg_1_reg = imm8;
reg_addr_1_reg = rd;
end
// shl, f = 0
5'b10000: begin
execute_op_reg = SHFT;
arg_0_reg = reg_0;
arg_1_reg = imm5;
end
// shl, f = 1
5'b10001: begin
execute_op_reg = SHFT;
arg_0_reg = reg_0;
arg_1_reg = imm5;
end
endcase
end
assign reg_addr_0 = instruction[7:5];
assign reg_addr_1 = reg_addr_1_reg;
always @(posedge clk) begin
arg_0_out <= arg_0_reg;
arg_1_out <= arg_1_reg;
// Special case for BRZ
execute_op_out <= execute_op_reg;
if (execute_op_reg == BRZ && reg_1 != 0) begin
execute_op_out <= NOP;
end
pc_out_out <= pc_out_reg;
dest_out <= dest_reg;
end
assign arg_0 = arg_0_out;
assign arg_1 = arg_1_out;
assign execute_op = execute_op_out;
assign pc_out = pc_out_out;
assign dest = dest_out;
endmodule
/////////////////////////
// EXECUTE STAGE //
/////////////////////////
module executor(clk, execute_op, arg_0, arg_1, dest_in, pc_in, dest_out, reg_value_out, reg_write_enable, pc_value_out, pc_write_enable);
parameter ADD = 4'h0;
parameter SET = 4'h1;
parameter NOP = 4'h2;
parameter SHFT = 4'h3;
parameter CALL = 4'h4;
parameter BRZ = 4'h5;
input clk;
input[3:0] execute_op;
input[3:0] dest_in;
input[15:0] arg_0;
input[15:0] arg_1;
input[15:0] pc_in;
reg[3:0] dest_out_reg;
reg[15:0] reg_value_out_reg;
reg[15:0] pc_value_out_reg;
reg reg_write_enable_reg;
reg pc_write_enable_reg;
reg[3:0] dest_out_out;
reg[15:0] reg_value_out_out;
reg[15:0] pc_value_out_out;
reg reg_write_enable_out;
reg pc_write_enable_out;
output[3:0] dest_out;
output[15:0] reg_value_out;
output[15:0] pc_value_out;
output reg_write_enable;
output pc_write_enable;
always @(*) begin
dest_out_reg = dest_in; //y
reg_value_out_reg = 0;
pc_value_out_reg = 0;
reg_write_enable_reg = 0;
pc_write_enable_reg = 0;
case (execute_op)
ADD: begin
reg_value_out_reg = arg_0 + arg_1;
reg_write_enable_reg = 1;
end
SET: begin
reg_value_out_reg = (arg_0 < arg_1);
reg_write_enable_reg = 1;
end
SHFT: begin
reg_value_out_reg = arg_0 << arg_1[3:0];
reg_write_enable_reg = 1;
end
CALL: begin
reg_value_out_reg = pc_in;
pc_value_out_reg = arg_0 + arg_1;
pc_write_enable_reg = 1;
reg_write_enable_reg = 1;
end
BRZ: begin
pc_value_out_reg = arg_0 + arg_1;
pc_write_enable_reg = 1;
end
endcase
end
always @(posedge clk) begin
dest_out_out <= dest_out_reg;
reg_value_out_out <= reg_value_out_reg;
pc_value_out_out <= pc_value_out_reg;
reg_write_enable_out <= reg_write_enable_reg;
pc_write_enable_out <= pc_write_enable_reg;
end
assign dest_out = dest_out_out;
assign reg_value_out = reg_value_out_out;
assign pc_value_out = pc_value_out_out;
assign reg_write_enable = reg_write_enable_out;
assign pc_write_enable = pc_write_enable_out;
endmodule
/////////////////////////
// 7 SEG //
/////////////////////////
module display(NUM, HEX);
input[3:0] NUM;
output[6:0] HEX;
reg[6:0] HEX;
always @(*)
case (NUM)
4'h0 : HEX = 7'b1000000;
4'h1 : HEX = 7'b1111001;
4'h2 : HEX = 7'b0100100;
4'h3 : HEX = 7'b0110000;
4'h4 : HEX = 7'b0011001;
4'h5 : HEX = 7'b0010010;
4'h6 : HEX = 7'b0000010;
4'h7 : HEX = 7'b1111000;
4'h8 : HEX = 7'b0000000;
4'h9 : HEX = 7'b0010000;
4'hA : HEX = 7'b0001000;
4'hB : HEX = 7'b0000011;
4'hC : HEX = 7'b0100111;
4'hD : HEX = 7'b0100001;
4'hE : HEX = 7'b0000110;
4'hF : HEX = 7'b0001110;
endcase
endmodule