-
Notifications
You must be signed in to change notification settings - Fork 0
/
riscv.v
376 lines (320 loc) · 8.16 KB
/
riscv.v
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
// cpu
`default_nettype none
`define CPU
//`define DEBUG
module m_branch(
input wire clk,
input wire [2:0] fun3,
input wire [31:0] left,
input wire [31:0] right,
output reg branch_out
);
always@(posedge clk)
begin
case (fun3)
3'b000: branch_out <= left == right;
3'b001: branch_out <= left != right;
3'b100: branch_out <= $signed(left) < $signed(right);
3'b101: branch_out <= $signed(left) >= $signed(right);
3'b110: branch_out <= left < right;
3'b111: branch_out <= left >= right;
default: branch_out <= 1'b0;
endcase
end
endmodule
//module m_store(
// input wire clk,
// input wire [ 2:0] fun3,
// input wire [31:0] val,
// input wire [ 1:0] mod,
// output reg [31:0] mem_out
// );
//
//always@(posedge clk)
//begin
// case (fun3)
// 3'b000: mem_out[mod*8+7-:8] <= val[ 7:0];
// 3'b001: mem_out[15:0] <= val[15:0];
// 3'b010: mem_out <= val;
// default: mem_out <= 32'b0;
// endcase
//end
//
//endmodule
module m_arith(
input wire clk,
output reg [31:0] out,
input wire [ 2:0] fun3,
input wire [31:0] left,
input wire [31:0] right,
input wire alt
);
always@(posedge clk)
begin
case (fun3)
3'b000: // ADD
out <= (alt) ? left - right : left + right;
3'b001: // SLL
out <= left << right[4:0];
3'b010: // SLT
out <= $signed(left) < $signed(right);
3'b011: // SLTU
out <= left < right;
3'b100: // XOR
out <= left ^ right;
3'b101: // SRL, SRA
out <= (alt) ? $signed(left) >>> right : left >> right;
3'b110: // OR
out <= left | right;
3'b111: // AND
out <= left & right;
default:
out <= 32'b0;
endcase
end
endmodule
module riscv_i (
input clk,
input reset,
output reg [9:0] led,
input ser_rx,
output ser_tx
);
localparam MEMSIZE = 4095;
localparam ENTRY = 'h8000_0000;
localparam LOAD = 7'b000_0011;
localparam OP_IMM = 7'b001_0011;
localparam OP = 7'b011_0011;
localparam AUIPC = 7'b001_0111;
localparam LUI = 7'b011_0111;
localparam JALR = 7'b110_0111;
localparam JAL = 7'b110_1111;
localparam STORE = 7'b010_0011;
localparam BRANCH = 7'b110_0011;
localparam SYSTEM = 7'b111_0011;
localparam MISC_MEM = 7'b000_1111;
reg [31:0] mem[0:MEMSIZE];
reg [31:0] PC;
reg [31:0] rgs[0:31];
reg [31:0] ins;
reg [ 6:0] op;
reg [ 2:0] fun3;
reg [31:0] imm_i;
reg [31:0] imm_s;
reg [31:0] imm_b;
reg [31:0] imm_u;
reg [31:0] imm_j;
reg [ 4:0] rd;
reg [ 4:0] rs1;
reg [ 4:0] rs2;
// working reg
reg [4:0] step;
wire [31:0] store_reg;
wire branch_reg;
wire [31:0] arith0;
wire [31:0] arith3;
wire [31:0] arithlui;
//wire [31:0] index;
reg [31:0] x;
reg [31:0] y;
reg alt;
reg [31:0] out;
reg success = 1'b0;
reg fail;
//function [31:0] idx(input [31:0] addr);
//begin
// idx = (addr - ENTRY) >> 2;
//end
//endfunction
//assign index = (arith0 - ENTRY) >> 2;
//function [31:0] extend(
// input [ 2:0] fun3_in,
// input [31:0] val_in,
// input [ 1:0] mod_in
// );
//reg [ 1:0] mod;
//reg [31:0] word;
//reg [ 2:0] size;
//begin
// if (mod_in == 2'b0)
// word = val_in;
// else if (mod_in == 2'b10)
// word = { 16'b0, val_in[31:16] };
// else if (mod_in == 2'b1)
// word = { 8'b0, val_in[31: 8] };
// else
// word = { 24'b0, val_in[31:24] };
//
// case (fun3_in)
// 3'b000: extend = { {25{word[ 7]}}, word[ 6:0] }; // LB
// 3'b001: extend = { {17{word[15]}}, word[14:0] }; // LH
// 3'b010: extend = word; // LW
// 3'b100: extend = { 24'b0, word[ 7:0] }; // LBU
// 3'b101: extend = { 16'b0, word[15:0] }; // LHU
// default: extend = 32'b0;
// endcase
// extend = 32'b0;
//end
//endfunction
m_arith arith_m1 (.clk(clk), .out(arith0), .fun3(3'b0), .left(x), .right(y), .alt(alt));
m_arith arith_m2 (.clk(clk), .out(arith3), .fun3(fun3), .left(x), .right(y), .alt(alt));
m_arith arith_m3 (.clk(clk), .out(arithlui), .fun3(3'b0), .left(32'b0), .right(y), .alt(alt));
//m_store store_1 (.clk(clk), .fun3(fun3), .val(rgs[rs2]), .mod(arith0[1:0]), .mem_out(store_reg));
m_branch branch_1 (.clk(clk), .fun3(fun3), .left(rgs[rs1]), .right(rgs[rs2]), .branch_out(branch_reg));
integer i;
initial
begin
//$readmemh("tests/rv32ui-p-addi.dat", mem);
$readmemh("src/firmware.hex", mem);
for (i = 0; i < 32; i = i + 1)
rgs[i] <= 32'b0;
end
`ifdef CPU
always @(posedge clk)
begin
if (reset)
begin
PC <= ENTRY;
step <= 5'b0;
end
// fetch
if (~step[0])
begin
ins <= mem[(PC - ENTRY) >> 2];
step <= 5'b1;
end
// decode
if (step[0])
begin
$display("step 1: PC %h ins %h", PC, ins);
imm_i <= { { 20{ins[31]}}, ins[31:20] };
imm_s <= { { 20{ins[31]}}, ins[31:25], ins[11:7] };
imm_b <= { { 20{ins[31]}}, ins[ 7: 7], ins[30:25], ins[11: 8], 1'b0 };
imm_u <= { ins[31:12], 12'b0 };
imm_j <= { { 12{ins[31]}}, ins[19:12], ins[20:20], ins[30:21], 1'b0 };
op <= ins[ 6: 0];
fun3 <= ins[14:12];
rd <= ins[11: 7];
rs1 <= ins[19:15];
rs2 <= ins[24:20];
step <= step << 1;
end
// execute
if (step[1])
begin
$display("step 2: op %08b fn3 %08b rd %08d rs1 %08d rs2 %08d", op, fun3, rd, rs1, rs2);
case (op)
OP_IMM : x <= PC; // op_imm
BRANCH : x <= PC; // branch
JAL : x <= PC; // jal
default : x <= rgs[rs1];
endcase
case (op)
LOAD : y <= imm_i; // load
STORE : y <= imm_s; // store
OP_IMM : y <= imm_i; // op_imm
OP : y <= rgs[rs2]; // op
AUIPC : y <= imm_u; // auipc
LUI : y <= imm_u; // lui
BRANCH : y <= imm_b; // branch
JALR : y <= imm_i; // jalr
JAL : y <= imm_j; // jal
SYSTEM : y <= imm_i; // system
MISC_MEM: y <= 32'b0; // misc_mem
default : y <= 32'b0;
endcase
case (op)
7'b001_0011: // op_imm
if (fun3 == 3'b101) // SRL
alt <= 1'b1 & ins[30];
7'b011_0011: // op
case (fun3)
3'b101: alt <= 1'b1 & ins[30]; // SRL
3'b0 : alt <= 1'b1 & ins[30]; // ADD
endcase
default:
alt <= 1'b0;
endcase
step <= step << 1;
end
// for arith to process
if (step[2])
step <= step << 1;
if (step[3])
begin
if (op == OP_IMM || op == OP)
out <= arith3;
else if (op == LUI)
out <= arithlui;
else if (op == JALR)
out <= arith0 & 32'hfffffffe;
else if (op == AUIPC || op == BRANCH || op == JAL)
out <= arith0;
else if (op == SYSTEM)
if (ins == 32'hc0001073)
success <= 1'b1;
else if (fun3 == 3'b0 && rgs[3] > 1)
fail <= 1'b0;
else
out <= 32'b0;
// LOAD from memory later
// out <= mem[idx];
step <= step << 1;
end
// store
if (step[4])
begin
$display("step 3: out %h x %h y %h alt %08b ar0 %h ar3 %h arl %h", out, x, y, alt, arith0, arith3, arithlui);
if (~rd)
begin
if (op == LOAD || op == OP_IMM || op == OP || op == AUIPC || op == LUI)
rgs[rd] <= out;
else if (op == JALR || op == JAL)
rgs[rd] <= PC + 4;
end
// STORE from memory later
// mem[idx] <= store_reg;
// debugging information
`ifdef DEBUG
$display($time, " PC : %08x OP: %08x", PC, ins);
$display("imm_i: %08x", imm_i);
$display("imm_s: %08x", imm_s);
$display("imm_b: %08x", imm_b);
$display("imm_u: %08x", imm_u);
$display("imm_j: %08x", imm_j);
$display(" x0: %08x", rgs[0]);
$display(" a0: %08x", rgs[10]);
$display(" ra: %08x", rgs[1]);
$display(" sp: %08x", rgs[2]);
$display(" a4: %08x", rgs[14]);
$display(" a5: %08x", rgs[15]);
$display(" t2: %08x", rgs[27]);
$display(" t4: %08x", rgs[29]);
$display(" x: %08x", x);
$display(" y: %08x", y);
$display(" out: %08x\n", out);
`endif
// jump
if (op == JAL || op == JALR || (op == BRANCH && branch_reg))
PC <= out;
else
PC <= PC + 4;
step <= 5'b1;
end
if (success)
begin
$display("Success");
led <= 10'b1010101010;
$finish;
end
else if (fail)
begin
$display("Failed");
led <= 10'b101010101;
$finish;
end
else
led <= PC[9:0];
end
`endif
endmodule