Debian Codegraph @

examples/gw5a/femto-riscv-18.v

raw
1 /**
2 * Step 18: Creating a RISC-V processor
3 * Mandelbrot in the terminal
4 *
5 * Copyright (c) 2020, Bruno Levy
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 `default_nettype none
35 `include "clockworks.v"
36 `include "emitter_uart.v"
37
38 module Memory (
39 input clk,
40 input [31:0] mem_addr, // address to be read
41 output reg [31:0] mem_rdata, // data read from memory
42 input mem_rstrb, // goes high when processor wants to read
43 input [31:0] mem_wdata, // data to be written
44 input [3:0] mem_wmask // masks for writing the 4 bytes (1=write byte)
45 );
46
47 reg [31:0] MEM [0:1535]; // 1536 4-bytes words = 6 Kb of RAM in total
48
49 localparam slow_bit=19;
50
51 // Memory-mapped IO in IO page, 1-hot addressing in word address.
52 localparam IO_LEDS_bit = 0; // W five leds
53 localparam IO_UART_DAT_bit = 1; // W data to send (8 bits)
54 localparam IO_UART_CNTL_bit = 2; // R status. bit 9: busy sending
55
56 // Converts an IO_xxx_bit constant into an offset in IO page.
57 function [31:0] IO_BIT_TO_OFFSET;
58 input [31:0] bitid;
59 begin
60 IO_BIT_TO_OFFSET = 1 << (bitid + 2);
61 end
62 endfunction
63
64 `include "riscv_assembly.v"
65
66 `define mandel_shift 10
67 `define mandel_mul (1 << `mandel_shift)
68 `define xmin (-2*`mandel_mul)
69 `define xmax ( 2*`mandel_mul)
70 `define ymin (-2*`mandel_mul)
71 `define ymax ( 2*`mandel_mul)
72 `define dx ((`xmax-`xmin)/80)
73 `define dy ((`ymax-`ymin)/80)
74 `define norm_max (4 << `mandel_shift)
75
76 integer mandelstart_ = 12;
77 integer blink_ = 16;
78 integer loop_y_ = 76;
79 integer loop_x_ = 84;
80 integer loop_Z_ = 96;
81 integer exit_Z_ = 188;
82 integer wait_ = 264;
83 integer wait_L0_ = 272;
84 integer putc_ = 284;
85 integer putc_L0_ = 292;
86 integer mulsi3_ = 308;
87 integer mulsi3_L0_ = 316;
88 integer mulsi3_L1_ = 328;
89
90 integer colormap_ = 344;
91
92 // X,Y : s0,s1
93 // Cr,Ci : s2,s3
94 // Zr,Zi : s4,s5
95 // Zrr,2Zri,Zii: s6,s7,s8
96 // cnt: s10
97 // 128: s11
98
99 initial begin
100 LI(sp,32'h1800); // End of RAM, 6kB
101 LI(gp,32'h400000); // IO page
102
103 Label(mandelstart_);
104
105 // Blink 5 times.
106 LI(s0,5);
107 Label(blink_);
108 LI(a0,5);
109 SW(a0,gp,IO_BIT_TO_OFFSET(IO_LEDS_bit));
110 CALL(LabelRef(wait_));
111 LI(a0,10);
112 SW(a0,gp,IO_BIT_TO_OFFSET(IO_LEDS_bit));
113 CALL(LabelRef(wait_));
114 ADDI(s0,s0,-1);
115 BNEZ(s0,LabelRef(blink_));
116 LI(a0,0);
117 SW(a0,gp,IO_BIT_TO_OFFSET(IO_LEDS_bit));
118
119
120 LI(s1,0);
121 LI(s3,`xmin);
122 LI(s11,80);
123
124 Label(loop_y_);
125 LI(s0,0);
126 LI(s2,`ymin);
127
128 Label(loop_x_);
129 MV(s4,s2); // Z <- C
130 MV(s5,s3);
131
132 LI(s10,9); // iter <- 9
133
134 Label(loop_Z_);
135 MV(a0,s4); // Zrr <- (Zr*Zr) >> mandel_shift
136 MV(a1,s4);
137 CALL(LabelRef(mulsi3_));
138 SRLI(s6,a0,`mandel_shift);
139 MV(a0,s4); // Zri <- (Zr*Zi) >> (mandel_shift-1)
140 MV(a1,s5);
141 CALL(LabelRef(mulsi3_));
142 SRAI(s7,a0,`mandel_shift-1);
143 MV(a0,s5); // Zii <- (Zi*Zi) >> (mandel_shift)
144 MV(a1,s5);
145 CALL(LabelRef(mulsi3_));
146 SRLI(s8,a0,`mandel_shift);
147 SUB(s4,s6,s8); // Zr <- Zrr - Zii + Cr
148 ADD(s4,s4,s2);
149 ADD(s5,s7,s3); // Zi <- 2Zri + Cr
150
151 ADD(s6,s6,s8); // if norm > norm max, exit loop
152 LI(s7,`norm_max);
153 BGT(s6,s7,LabelRef(exit_Z_));
154
155 ADDI(s10,s10,-1); // iter--, loop if non-zero
156 BNEZ(s10,LabelRef(loop_Z_));
157
158 Label(exit_Z_);
159 LI(a0,colormap_);
160 ADD(a0,a0,s10);
161 LBU(a0,a0,0);
162 CALL(LabelRef(putc_));
163
164 ADDI(s0,s0,1);
165 ADDI(s2,s2,`dx);
166 BNE(s0,s11,LabelRef(loop_x_));
167
168 LI(a0," ");
169 CALL(LabelRef(putc_));
170 LI(a0,"\n");
171 CALL(LabelRef(putc_));
172
173 ADDI(s1,s1,1);
174 ADDI(s3,s3,`dy);
175 BNE(s1,s11,LabelRef(loop_y_));
176
177
178 J(LabelRef(mandelstart_));
179
180 EBREAK(); // I systematically keep it before functions
181 // in case I decide to remove the loop...
182
183 Label(wait_);
184 LI(t0,1);
185 SLLI(t0,t0,slow_bit);
186 Label(wait_L0_);
187 ADDI(t0,t0,-1);
188 BNEZ(t0,LabelRef(wait_L0_));
189 RET();
190
191 Label(putc_);
192 // Send character to UART
193 SW(a0,gp,IO_BIT_TO_OFFSET(IO_UART_DAT_bit));
194 // Read UART status, and loop until bit 9 (busy sending)
195 // is zero.
196 LI(t0,1<<9);
197 Label(putc_L0_);
198 LW(t1,gp,IO_BIT_TO_OFFSET(IO_UART_CNTL_bit));
199 AND(t1,t1,t0);
200 BNEZ(t1,LabelRef(putc_L0_));
201 RET();
202
203 // Mutiplication routine,
204 // Input in a0 and a1
205 // Result in a0
206 Label(mulsi3_);
207 MV(a2,a0);
208 LI(a0,0);
209 Label(mulsi3_L0_);
210 ANDI(a3,a1,1);
211 BEQZ(a3,LabelRef(mulsi3_L1_));
212 ADD(a0,a0,a2);
213 Label(mulsi3_L1_);
214 SRLI(a1,a1,1);
215 SLLI(a2,a2,1);
216 BNEZ(a1,LabelRef(mulsi3_L0_));
217 RET();
218
219 Label(colormap_);
220 DATAB(" ",".",",",":");
221 DATAB(";","o","x","%");
222 DATAB("#","@", 0 , 0 );
223 endASM();
224 end
225
226 wire [29:0] word_addr = mem_addr[31:2];
227
228 always @(posedge clk) begin
229 if(mem_rstrb) begin
230 mem_rdata <= MEM[word_addr];
231 end
232 if(mem_wmask[0]) MEM[word_addr][ 7:0 ] <= mem_wdata[ 7:0 ];
233 if(mem_wmask[1]) MEM[word_addr][15:8 ] <= mem_wdata[15:8 ];
234 if(mem_wmask[2]) MEM[word_addr][23:16] <= mem_wdata[23:16];
235 if(mem_wmask[3]) MEM[word_addr][31:24] <= mem_wdata[31:24];
236 end
237 endmodule
238
239 module Processor (
240 input clk,
241 input resetn,
242 output [31:0] mem_addr,
243 input [31:0] mem_rdata,
244 output mem_rstrb,
245 output [31:0] mem_wdata,
246 output [3:0] mem_wmask
247 );
248
249 reg [31:0] PC=0; // program counter
250 reg [31:0] instr; // current instruction
251
252 // See the table P. 105 in RISC-V manual
253
254 // The 10 RISC-V instructions
255 wire isALUreg = (instr[6:0] == 7'b0110011); // rd <- rs1 OP rs2
256 wire isALUimm = (instr[6:0] == 7'b0010011); // rd <- rs1 OP Iimm
257 wire isBranch = (instr[6:0] == 7'b1100011); // if(rs1 OP rs2) PC<-PC+Bimm
258 wire isJALR = (instr[6:0] == 7'b1100111); // rd <- PC+4; PC<-rs1+Iimm
259 wire isJAL = (instr[6:0] == 7'b1101111); // rd <- PC+4; PC<-PC+Jimm
260 wire isAUIPC = (instr[6:0] == 7'b0010111); // rd <- PC + Uimm
261 wire isLUI = (instr[6:0] == 7'b0110111); // rd <- Uimm
262 wire isLoad = (instr[6:0] == 7'b0000011); // rd <- mem[rs1+Iimm]
263 wire isStore = (instr[6:0] == 7'b0100011); // mem[rs1+Simm] <- rs2
264 wire isSYSTEM = (instr[6:0] == 7'b1110011); // special
265
266 // The 5 immediate formats
267 wire [31:0] Uimm={ instr[31], instr[30:12], {12{1'b0}}};
268 wire [31:0] Iimm={{21{instr[31]}}, instr[30:20]};
269 wire [31:0] Simm={{21{instr[31]}}, instr[30:25],instr[11:7]};
270 wire [31:0] Bimm={{20{instr[31]}}, instr[7],instr[30:25],instr[11:8],1'b0};
271 wire [31:0] Jimm={{12{instr[31]}}, instr[19:12],instr[20],instr[30:21],1'b0};
272
273 // Source and destination registers
274 wire [4:0] rs1Id = instr[19:15];
275 wire [4:0] rs2Id = instr[24:20];
276 wire [4:0] rdId = instr[11:7];
277
278 // function codes
279 wire [2:0] funct3 = instr[14:12];
280 wire [6:0] funct7 = instr[31:25];
281
282 // The registers bank
283 `ifdef FORCE_BRAM
284 (* ram_style = "block" *)
285 `endif
286 reg [31:0] RegisterBank [0:31];
287 reg [31:0] rs1; // value of source
288 reg [31:0] rs2; // registers.
289 wire [31:0] writeBackData; // data to be written to rd
290 wire writeBackEn; // asserted if data should be written to rd
291
292 // The ALU
293 wire [31:0] aluIn1 = rs1;
294 wire [31:0] aluIn2 = isALUreg | isBranch ? rs2 : Iimm;
295
296 wire [4:0] shamt = isALUreg ? rs2[4:0] : instr[24:20]; // shift amount
297
298 // The adder is used by both arithmetic instructions and JALR.
299 wire [31:0] aluPlus = aluIn1 + aluIn2;
300
301 // Use a single 33 bits subtract to do subtraction and all comparisons
302 // (trick borrowed from swapforth/J1)
303 wire [32:0] aluMinus = {1'b1, ~aluIn2} + {1'b0,aluIn1} + 33'b1;
304 wire LT = (aluIn1[31] ^ aluIn2[31]) ? aluIn1[31] : aluMinus[32];
305 wire LTU = aluMinus[32];
306 wire EQ = (aluMinus[31:0] == 0);
307
308 // Flip a 32 bit word. Used by the shifter (a single shifter for
309 // left and right shifts, saves silicium !)
310 function [31:0] flip32;
311 input [31:0] x;
312 flip32 = {x[ 0], x[ 1], x[ 2], x[ 3], x[ 4], x[ 5], x[ 6], x[ 7],
313 x[ 8], x[ 9], x[10], x[11], x[12], x[13], x[14], x[15],
314 x[16], x[17], x[18], x[19], x[20], x[21], x[22], x[23],
315 x[24], x[25], x[26], x[27], x[28], x[29], x[30], x[31]};
316 endfunction
317
318 wire [31:0] shifter_in = (funct3 == 3'b001) ? flip32(aluIn1) : aluIn1;
319
320 /* verilator lint_off WIDTH */
321 wire [31:0] shifter =
322 $signed({instr[30] & aluIn1[31], shifter_in}) >>> aluIn2[4:0];
323 /* verilator lint_on WIDTH */
324
325 wire [31:0] leftshift = flip32(shifter);
326
327
328
329 // ADD/SUB/ADDI:
330 // funct7[5] is 1 for SUB and 0 for ADD. We need also to test instr[5]
331 // to make the difference with ADDI
332 //
333 // SRLI/SRAI/SRL/SRA:
334 // funct7[5] is 1 for arithmetic shift (SRA/SRAI) and
335 // 0 for logical shift (SRL/SRLI)
336 reg [31:0] aluOut;
337 always @(*) begin
338 case(funct3)
339 3'b000: aluOut = (funct7[5] & instr[5]) ? aluMinus[31:0] : aluPlus;
340 3'b001: aluOut = leftshift;
341 3'b010: aluOut = {31'b0, LT};
342 3'b011: aluOut = {31'b0, LTU};
343 3'b100: aluOut = (aluIn1 ^ aluIn2);
344 3'b101: aluOut = shifter;
345 3'b110: aluOut = (aluIn1 | aluIn2);
346 3'b111: aluOut = (aluIn1 & aluIn2);
347 endcase
348 end
349
350 // The predicate for branch instructions
351 reg takeBranch;
352 always @(*) begin
353 case(funct3)
354 3'b000: takeBranch = EQ;
355 3'b001: takeBranch = !EQ;
356 3'b100: takeBranch = LT;
357 3'b101: takeBranch = !LT;
358 3'b110: takeBranch = LTU;
359 3'b111: takeBranch = !LTU;
360 default: takeBranch = 1'b0;
361 endcase
362 end
363
364
365 // Address computation
366 // An adder used to compute branch address, JAL address and AUIPC.
367 // branch->PC+Bimm AUIPC->PC+Uimm JAL->PC+Jimm
368 // Equivalent to PCplusImm = PC + (isJAL ? Jimm : isAUIPC ? Uimm : Bimm)
369 wire [31:0] PCplusImm = PC + ( instr[3] ? Jimm[31:0] :
370 instr[4] ? Uimm[31:0] :
371 Bimm[31:0] );
372 wire [31:0] PCplus4 = PC+4;
373
374 // register write back
375 assign writeBackData = (isJAL || isJALR) ? PCplus4 :
376 isLUI ? Uimm :
377 isAUIPC ? PCplusImm :
378 isLoad ? LOAD_data :
379 aluOut;
380
381 wire [31:0] nextPC = ((isBranch && takeBranch) || isJAL) ? PCplusImm :
382 isJALR ? {aluPlus[31:1],1'b0} :
383 PCplus4;
384
385 wire [31:0] loadstore_addr = rs1 + (isStore ? Simm : Iimm);
386
387 // Load
388 // All memory accesses are aligned on 32 bits boundary. For this
389 // reason, we need some circuitry that does unaligned halfword
390 // and byte load/store, based on:
391 // - funct3[1:0]: 00->byte 01->halfword 10->word
392 // - mem_addr[1:0]: indicates which byte/halfword is accessed
393
394 wire mem_byteAccess = funct3[1:0] == 2'b00;
395 wire mem_halfwordAccess = funct3[1:0] == 2'b01;
396
397
398 wire [15:0] LOAD_halfword =
399 loadstore_addr[1] ? mem_rdata[31:16] : mem_rdata[15:0];
400
401 wire [7:0] LOAD_byte =
402 loadstore_addr[0] ? LOAD_halfword[15:8] : LOAD_halfword[7:0];
403
404 // LOAD, in addition to funct3[1:0], LOAD depends on:
405 // - funct3[2] (instr[14]): 0->do sign expansion 1->no sign expansion
406 wire LOAD_sign =
407 !funct3[2] & (mem_byteAccess ? LOAD_byte[7] : LOAD_halfword[15]);
408
409 wire [31:0] LOAD_data =
410 mem_byteAccess ? {{24{LOAD_sign}}, LOAD_byte} :
411 mem_halfwordAccess ? {{16{LOAD_sign}}, LOAD_halfword} :
412 mem_rdata ;
413
414 // Store
415 // ------------------------------------------------------------------------
416
417 assign mem_wdata[ 7: 0] = rs2[7:0];
418 assign mem_wdata[15: 8] = loadstore_addr[0] ? rs2[7:0] : rs2[15: 8];
419 assign mem_wdata[23:16] = loadstore_addr[1] ? rs2[7:0] : rs2[23:16];
420 assign mem_wdata[31:24] = loadstore_addr[0] ? rs2[7:0] :
421 loadstore_addr[1] ? rs2[15:8] : rs2[31:24];
422
423 // The memory write mask:
424 // 1111 if writing a word
425 // 0011 or 1100 if writing a halfword
426 // (depending on loadstore_addr[1])
427 // 0001, 0010, 0100 or 1000 if writing a byte
428 // (depending on loadstore_addr[1:0])
429
430 wire [3:0] STORE_wmask =
431 mem_byteAccess ?
432 (loadstore_addr[1] ?
433 (loadstore_addr[0] ? 4'b1000 : 4'b0100) :
434 (loadstore_addr[0] ? 4'b0010 : 4'b0001)
435 ) :
436 mem_halfwordAccess ?
437 (loadstore_addr[1] ? 4'b1100 : 4'b0011) :
438 4'b1111;
439
440 // The state machine
441 localparam FETCH_INSTR = 0;
442 localparam WAIT_INSTR = 1;
443 localparam FETCH_REGS = 2;
444 localparam EXECUTE = 3;
445 localparam LOAD = 4;
446 localparam WAIT_DATA = 5;
447 localparam STORE = 6;
448 reg [2:0] state = FETCH_INSTR;
449
450 always @(posedge clk) begin
451 if(!resetn) begin
452 PC <= 0;
453 state <= FETCH_INSTR;
454 end else begin
455 if(writeBackEn && rdId != 0) begin
456 RegisterBank[rdId] <= writeBackData;
457 end
458 case(state)
459 FETCH_INSTR: begin
460 state <= WAIT_INSTR;
461 end
462 WAIT_INSTR: begin
463 instr <= mem_rdata;
464 state <= FETCH_REGS;
465 end
466 FETCH_REGS: begin
467 rs1 <= RegisterBank[rs1Id];
468 rs2 <= RegisterBank[rs2Id];
469 state <= EXECUTE;
470 end
471 EXECUTE: begin
472 if(!isSYSTEM) begin
473 PC <= nextPC;
474 end
475 state <= isLoad ? LOAD :
476 isStore ? STORE :
477 FETCH_INSTR;
478 end
479 LOAD: begin
480 state <= WAIT_DATA;
481 end
482 WAIT_DATA: begin
483 state <= FETCH_INSTR;
484 end
485 STORE: begin
486 state <= FETCH_INSTR;
487 end
488 endcase
489 end
490 end
491
492 assign writeBackEn = (state==EXECUTE && !isBranch && !isStore) ||
493 (state==WAIT_DATA) ;
494
495 assign mem_addr = (state == WAIT_INSTR || state == FETCH_INSTR) ?
496 PC : loadstore_addr ;
497 assign mem_rstrb = (state == FETCH_INSTR || state == LOAD);
498 assign mem_wmask = {4{(state == STORE)}} & STORE_wmask;
499
500 endmodule
501
502
503 module top (
504 input clk_i, // system clock
505 input rst_i, // reset button
506 output [5:0] led, // system LEDs
507 input RXD, // UART receive
508 output TXD // UART transmit
509 );
510
511 wire clk;
512 wire resetn;
513
514 wire [31:0] mem_addr;
515 wire [31:0] mem_rdata;
516 wire mem_rstrb;
517 wire [31:0] mem_wdata;
518 wire [3:0] mem_wmask;
519
520 Processor CPU(
521 .clk(clk),
522 .resetn(resetn),
523 .mem_addr(mem_addr),
524 .mem_rdata(mem_rdata),
525 .mem_rstrb(mem_rstrb),
526 .mem_wdata(mem_wdata),
527 .mem_wmask(mem_wmask)
528 );
529
530 wire [31:0] RAM_rdata;
531 wire [29:0] mem_wordaddr = mem_addr[31:2];
532 wire isIO = mem_addr[22];
533 wire isRAM = !isIO;
534 wire mem_wstrb = |mem_wmask;
535
536 Memory RAM(
537 .clk(clk),
538 .mem_addr(mem_addr),
539 .mem_rdata(RAM_rdata),
540 .mem_rstrb(isRAM & mem_rstrb),
541 .mem_wdata(mem_wdata),
542 .mem_wmask({4{isRAM}}&mem_wmask)
543 );
544
545
546 // Memory-mapped IO in IO page, 1-hot addressing in word address.
547 localparam IO_LEDS_bit = 0; // W five leds
548 localparam IO_UART_DAT_bit = 1; // W data to send (8 bits)
549 localparam IO_UART_CNTL_bit = 2; // R status. bit 9: busy sending
550
551 always @(posedge clk) begin
552 if(isIO & mem_wstrb & mem_wordaddr[IO_LEDS_bit]) begin
553 led <= mem_wdata;
554 end
555 end
556
557 wire uart_valid = isIO & mem_wstrb & mem_wordaddr[IO_UART_DAT_bit];
558 wire uart_ready;
559
560 corescore_emitter_uart #(
561 .clk_freq_hz(`CPU_FREQ*1000000),
562 //.clk_freq_hz(27*1000000),
563 .baud_rate(`BAUD_RATE)
564 ) UART(
565 .i_clk(clk),
566 .i_rst(!resetn),
567 .i_data(mem_wdata[7:0]),
568 .i_valid(uart_valid),
569 .o_ready(uart_ready),
570 .o_uart_tx(TXD)
571 );
572
573 wire [31:0] IO_rdata =
574 mem_wordaddr[IO_UART_CNTL_bit] ? { 22'b0, !uart_ready, 9'b0}
575 : 32'b0;
576
577 assign mem_rdata = isRAM ? RAM_rdata :
578 IO_rdata ;
579
580
581 // Gearbox and reset circuitry.
582 Clockworks #(
583 .SLOW(0) //Specifying a value other than zero here may result in
584 // nextpnr being unable to route the clock for some boards. BUGFIX is under development.
585 ) CW (
586 .CLK(clk_i),
587 .RESET(rst_i),
588 .clk(clk),
589 .resetn(resetn)
590 );
591
592 endmodule
593