Debian Codegraph @

examples/femto-riscv-userflash.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 `include "uflash_controller.v"
38
39 module Memory (
40 input clk,
41 input [31:0] mem_addr, // address to be read
42 output reg [31:0] mem_rdata, // data read from memory
43 input mem_rstrb, // goes high when processor wants to read
44 input [31:0] mem_wdata, // data to be written
45 input [3:0] mem_wmask // masks for writing the 4 bytes (1=write byte)
46 );
47
48 reg [31:0] MEM [0:1535]; // 1536 4-bytes words = 6 Kb of RAM in total
49
50 initial begin
51 $readmemh("riscv-firmware/userflash-9k.hex", MEM);
52 end
53
54 localparam slow_bit=19;
55
56 wire [29:0] word_addr = mem_addr[31:2];
57
58 always @(posedge clk) begin
59 if(mem_rstrb) begin
60 mem_rdata <= MEM[word_addr];
61 end
62 if(mem_wmask[0]) MEM[word_addr][ 7:0 ] <= mem_wdata[ 7:0 ];
63 if(mem_wmask[1]) MEM[word_addr][15:8 ] <= mem_wdata[15:8 ];
64 if(mem_wmask[2]) MEM[word_addr][23:16] <= mem_wdata[23:16];
65 if(mem_wmask[3]) MEM[word_addr][31:24] <= mem_wdata[31:24];
66 end
67 endmodule
68
69 module Processor (
70 input clk,
71 input resetn,
72 output [31:0] mem_addr,
73 input [31:0] mem_rdata,
74 input wire mem_rready,
75 output mem_rstrb,
76 output [31:0] mem_wdata,
77 output [3:0] mem_wmask
78 );
79
80 reg [31:0] PC=0; // program counter
81 reg [31:0] instr; // current instruction
82
83 // See the table P. 105 in RISC-V manual
84
85 // The 10 RISC-V instructions
86 wire isALUreg = (instr[6:0] == 7'b0110011); // rd <- rs1 OP rs2
87 wire isALUimm = (instr[6:0] == 7'b0010011); // rd <- rs1 OP Iimm
88 wire isBranch = (instr[6:0] == 7'b1100011); // if(rs1 OP rs2) PC<-PC+Bimm
89 wire isJALR = (instr[6:0] == 7'b1100111); // rd <- PC+4; PC<-rs1+Iimm
90 wire isJAL = (instr[6:0] == 7'b1101111); // rd <- PC+4; PC<-PC+Jimm
91 wire isAUIPC = (instr[6:0] == 7'b0010111); // rd <- PC + Uimm
92 wire isLUI = (instr[6:0] == 7'b0110111); // rd <- Uimm
93 wire isLoad = (instr[6:0] == 7'b0000011); // rd <- mem[rs1+Iimm]
94 wire isStore = (instr[6:0] == 7'b0100011); // mem[rs1+Simm] <- rs2
95 wire isSYSTEM = (instr[6:0] == 7'b1110011); // special
96
97 // The 5 immediate formats
98 wire [31:0] Uimm={ instr[31], instr[30:12], {12{1'b0}}};
99 wire [31:0] Iimm={{21{instr[31]}}, instr[30:20]};
100 wire [31:0] Simm={{21{instr[31]}}, instr[30:25],instr[11:7]};
101 wire [31:0] Bimm={{20{instr[31]}}, instr[7],instr[30:25],instr[11:8],1'b0};
102 wire [31:0] Jimm={{12{instr[31]}}, instr[19:12],instr[20],instr[30:21],1'b0};
103
104 // Source and destination registers
105 wire [4:0] rs1Id = instr[19:15];
106 wire [4:0] rs2Id = instr[24:20];
107 wire [4:0] rdId = instr[11:7];
108
109 // function codes
110 wire [2:0] funct3 = instr[14:12];
111 wire [6:0] funct7 = instr[31:25];
112
113 // The registers bank
114 `ifdef FORCE_BRAM
115 (* ram_style = "block" *)
116 `endif
117 reg [31:0] RegisterBank [0:31];
118 reg [31:0] rs1; // value of source
119 reg [31:0] rs2; // registers.
120 wire [31:0] writeBackData; // data to be written to rd
121 wire writeBackEn; // asserted if data should be written to rd
122
123 // The ALU
124 wire [31:0] aluIn1 = rs1;
125 wire [31:0] aluIn2 = isALUreg | isBranch ? rs2 : Iimm;
126
127 wire [4:0] shamt = isALUreg ? rs2[4:0] : instr[24:20]; // shift amount
128
129 // The adder is used by both arithmetic instructions and JALR.
130 wire [31:0] aluPlus = aluIn1 + aluIn2;
131
132 // Use a single 33 bits subtract to do subtraction and all comparisons
133 // (trick borrowed from swapforth/J1)
134 wire [32:0] aluMinus = {1'b1, ~aluIn2} + {1'b0,aluIn1} + 33'b1;
135 wire LT = (aluIn1[31] ^ aluIn2[31]) ? aluIn1[31] : aluMinus[32];
136 wire LTU = aluMinus[32];
137 wire EQ = (aluMinus[31:0] == 0);
138
139 // Flip a 32 bit word. Used by the shifter (a single shifter for
140 // left and right shifts, saves silicium !)
141 function [31:0] flip32;
142 input [31:0] x;
143 flip32 = {x[ 0], x[ 1], x[ 2], x[ 3], x[ 4], x[ 5], x[ 6], x[ 7],
144 x[ 8], x[ 9], x[10], x[11], x[12], x[13], x[14], x[15],
145 x[16], x[17], x[18], x[19], x[20], x[21], x[22], x[23],
146 x[24], x[25], x[26], x[27], x[28], x[29], x[30], x[31]};
147 endfunction
148
149 wire [31:0] shifter_in = (funct3 == 3'b001) ? flip32(aluIn1) : aluIn1;
150
151 /* verilator lint_off WIDTH */
152 wire [31:0] shifter =
153 $signed({instr[30] & aluIn1[31], shifter_in}) >>> aluIn2[4:0];
154 /* verilator lint_on WIDTH */
155
156 wire [31:0] leftshift = flip32(shifter);
157
158
159
160 // ADD/SUB/ADDI:
161 // funct7[5] is 1 for SUB and 0 for ADD. We need also to test instr[5]
162 // to make the difference with ADDI
163 //
164 // SRLI/SRAI/SRL/SRA:
165 // funct7[5] is 1 for arithmetic shift (SRA/SRAI) and
166 // 0 for logical shift (SRL/SRLI)
167 reg [31:0] aluOut;
168 always @(*) begin
169 case(funct3)
170 3'b000: aluOut = (funct7[5] & instr[5]) ? aluMinus[31:0] : aluPlus;
171 3'b001: aluOut = leftshift;
172 3'b010: aluOut = {31'b0, LT};
173 3'b011: aluOut = {31'b0, LTU};
174 3'b100: aluOut = (aluIn1 ^ aluIn2);
175 3'b101: aluOut = shifter;
176 3'b110: aluOut = (aluIn1 | aluIn2);
177 3'b111: aluOut = (aluIn1 & aluIn2);
178 endcase
179 end
180
181 // The predicate for branch instructions
182 reg takeBranch;
183 always @(*) begin
184 case(funct3)
185 3'b000: takeBranch = EQ;
186 3'b001: takeBranch = !EQ;
187 3'b100: takeBranch = LT;
188 3'b101: takeBranch = !LT;
189 3'b110: takeBranch = LTU;
190 3'b111: takeBranch = !LTU;
191 default: takeBranch = 1'b0;
192 endcase
193 end
194
195
196 // Address computation
197 // An adder used to compute branch address, JAL address and AUIPC.
198 // branch->PC+Bimm AUIPC->PC+Uimm JAL->PC+Jimm
199 // Equivalent to PCplusImm = PC + (isJAL ? Jimm : isAUIPC ? Uimm : Bimm)
200 wire [31:0] PCplusImm = PC + ( instr[3] ? Jimm[31:0] :
201 instr[4] ? Uimm[31:0] :
202 Bimm[31:0] );
203 wire [31:0] PCplus4 = PC+4;
204
205 // register write back
206 assign writeBackData = (isJAL || isJALR) ? PCplus4 :
207 isLUI ? Uimm :
208 isAUIPC ? PCplusImm :
209 isLoad ? LOAD_data :
210 aluOut;
211
212 wire [31:0] nextPC = ((isBranch && takeBranch) || isJAL) ? PCplusImm :
213 isJALR ? {aluPlus[31:1],1'b0} :
214 PCplus4;
215
216 wire [31:0] loadstore_addr = rs1 + (isStore ? Simm : Iimm);
217
218 // Load
219 // All memory accesses are aligned on 32 bits boundary. For this
220 // reason, we need some circuitry that does unaligned halfword
221 // and byte load/store, based on:
222 // - funct3[1:0]: 00->byte 01->halfword 10->word
223 // - mem_addr[1:0]: indicates which byte/halfword is accessed
224
225 wire mem_byteAccess = funct3[1:0] == 2'b00;
226 wire mem_halfwordAccess = funct3[1:0] == 2'b01;
227
228
229 wire [15:0] LOAD_halfword =
230 loadstore_addr[1] ? mem_rdata[31:16] : mem_rdata[15:0];
231
232 wire [7:0] LOAD_byte =
233 loadstore_addr[0] ? LOAD_halfword[15:8] : LOAD_halfword[7:0];
234
235 // LOAD, in addition to funct3[1:0], LOAD depends on:
236 // - funct3[2] (instr[14]): 0->do sign expansion 1->no sign expansion
237 wire LOAD_sign =
238 !funct3[2] & (mem_byteAccess ? LOAD_byte[7] : LOAD_halfword[15]);
239
240 wire [31:0] LOAD_data =
241 mem_byteAccess ? {{24{LOAD_sign}}, LOAD_byte} :
242 mem_halfwordAccess ? {{16{LOAD_sign}}, LOAD_halfword} :
243 mem_rdata ;
244
245 // Store
246 // ------------------------------------------------------------------------
247
248 assign mem_wdata[ 7: 0] = rs2[7:0];
249 assign mem_wdata[15: 8] = loadstore_addr[0] ? rs2[7:0] : rs2[15: 8];
250 assign mem_wdata[23:16] = loadstore_addr[1] ? rs2[7:0] : rs2[23:16];
251 assign mem_wdata[31:24] = loadstore_addr[0] ? rs2[7:0] :
252 loadstore_addr[1] ? rs2[15:8] : rs2[31:24];
253
254 // The memory write mask:
255 // 1111 if writing a word
256 // 0011 or 1100 if writing a halfword
257 // (depending on loadstore_addr[1])
258 // 0001, 0010, 0100 or 1000 if writing a byte
259 // (depending on loadstore_addr[1:0])
260
261 wire [3:0] STORE_wmask =
262 mem_byteAccess ?
263 (loadstore_addr[1] ?
264 (loadstore_addr[0] ? 4'b1000 : 4'b0100) :
265 (loadstore_addr[0] ? 4'b0010 : 4'b0001)
266 ) :
267 mem_halfwordAccess ?
268 (loadstore_addr[1] ? 4'b1100 : 4'b0011) :
269 4'b1111;
270
271 // The state machine
272 localparam FETCH_INSTR = 0;
273 localparam WAIT_INSTR = 1;
274 localparam FETCH_REGS = 2;
275 localparam EXECUTE = 3;
276 localparam LOAD = 4;
277 localparam WAIT_DATA = 5;
278 localparam STORE = 6;
279 reg [2:0] state = FETCH_INSTR;
280
281 always @(posedge clk) begin
282 if(!resetn) begin
283 PC <= 0;
284 state <= FETCH_INSTR;
285 end else begin
286 if(writeBackEn && rdId != 0) begin
287 RegisterBank[rdId] <= writeBackData;
288 end
289 case(state)
290 FETCH_INSTR: begin
291 state <= WAIT_INSTR;
292 end
293 WAIT_INSTR: begin
294 instr <= mem_rdata;
295 state <= FETCH_REGS;
296 end
297 FETCH_REGS: begin
298 rs1 <= RegisterBank[rs1Id];
299 rs2 <= RegisterBank[rs2Id];
300 state <= EXECUTE;
301 end
302 EXECUTE: begin
303 if(!isSYSTEM) begin
304 PC <= nextPC;
305 end
306 state <= isLoad ? LOAD :
307 isStore ? STORE :
308 FETCH_INSTR;
309 end
310 LOAD: begin
311 state <= WAIT_DATA;
312 end
313 WAIT_DATA: begin
314 if (mem_rready) begin
315 state <= FETCH_INSTR;
316 end
317 end
318 STORE: begin
319 state <= WAIT_DATA;
320 //state <= FETCH_INSTR;
321 end
322 endcase
323 end
324 end
325
326 assign writeBackEn = (state==EXECUTE && !isBranch && !isStore) ||
327 (state==WAIT_DATA) ;
328
329 assign mem_addr = (state == WAIT_INSTR || state == FETCH_INSTR) ?
330 PC : loadstore_addr ;
331 assign mem_rstrb = (state == FETCH_INSTR || state == LOAD);
332 assign mem_wmask = {4{(state == STORE)}} & STORE_wmask;
333
334 endmodule
335
336
337 module top (
338 input clk_i, // system clock
339 input rst_i, // reset button
340 output [5:0] led, // system LEDs
341 input RXD, // UART receive
342 output TXD // UART transmit
343 );
344
345 wire clk;
346 wire resetn;
347
348 wire [31:0] mem_addr;
349 wire [31:0] mem_rdata;
350 wire mem_rready;
351 wire mem_rstrb;
352 wire [31:0] mem_wdata;
353 wire [3:0] mem_wmask;
354
355 Processor CPU(
356 .clk(clk),
357 .resetn(resetn),
358 .mem_addr(mem_addr),
359 .mem_rdata(mem_rdata),
360 .mem_rready(mem_rready),
361 .mem_rstrb(mem_rstrb),
362 .mem_wdata(mem_wdata),
363 .mem_wmask(mem_wmask)
364 );
365
366 wire [31:0] RAM_rdata;
367 wire [29:0] mem_wordaddr = mem_addr[31:2];
368 wire isUserFlash = mem_addr[23:22] == 2'b10;
369 wire isIO = mem_addr[23:22] == 2'b01;
370 wire isRAM = mem_addr[23:22] == 2'b00;
371 wire mem_wstrb = |mem_wmask;
372
373 Memory RAM(
374 .clk(clk),
375 .mem_addr(mem_addr),
376 .mem_rdata(RAM_rdata),
377 .mem_rstrb(isRAM & mem_rstrb),
378 .mem_wdata(mem_wdata),
379 .mem_wmask({4{isRAM}}&mem_wmask)
380 );
381
382 // UserFlash
383 wire [31:0] UserFlash_rdata;
384 wire UserFlash_ready;
385 wire UserFlash_strob = isUserFlash & (mem_wstrb | mem_rstrb);
386 uflash UserFlash(
387 .reset_n(resetn),
388 .clk(clk),
389 .sel(UserFlash_strob),
390 .wstrb(mem_wmask),
391 .addr(mem_wordaddr[14:0]),
392 .data_i(mem_wdata),
393 .ready(UserFlash_ready),
394 .data_o(UserFlash_rdata)
395 );
396 defparam UserFlash.CLK_FREQ=`CPU_FREQ*1000000;
397
398 // Memory-mapped IO in IO page, 1-hot addressing in word address.
399 localparam IO_LEDS_bit = 0; // W five leds
400 localparam IO_UART_DAT_bit = 1; // W data to send (8 bits)
401 localparam IO_UART_CNTL_bit = 2; // R status. bit 9: busy sending
402
403 always @(posedge clk) begin
404 if(isIO & mem_wstrb & mem_wordaddr[IO_LEDS_bit]) begin
405 led <= mem_wdata;
406 end
407 end
408
409 wire uart_valid = isIO & mem_wstrb & mem_wordaddr[IO_UART_DAT_bit];
410 wire uart_ready;
411
412 corescore_emitter_uart #(
413 .clk_freq_hz(`CPU_FREQ*1000000),
414 .baud_rate(`BAUD_RATE)
415 ) UART(
416 .i_clk(clk),
417 .i_rst(!resetn),
418 .i_data(mem_wdata[7:0]),
419 .i_valid(uart_valid),
420 .o_ready(uart_ready),
421 .o_uart_tx(TXD)
422 );
423
424 wire [31:0] IO_rdata =
425 mem_wordaddr[IO_UART_CNTL_bit] ? { 22'b0, !uart_ready, 9'b0}
426 : 32'b0;
427
428 assign mem_rdata = isRAM ? RAM_rdata :
429 isUserFlash ? UserFlash_rdata :
430 IO_rdata ;
431 assign mem_rready = isUserFlash ? UserFlash_ready : 1'b1;
432
433
434 // Gearbox and reset circuitry.
435 Clockworks #(
436 .SLOW(0) //Specifying a value other than zero here may result in
437 // nextpnr being unable to route the clock for some boards. BUGFIX is under development.
438 ) CW (
439 .CLK(clk_i),
440 .RESET(rst_i),
441 .clk(clk),
442 .resetn(resetn)
443 );
444
445 endmodule
446