Debian Codegraph @

examples/femto-riscv-memtest.v

raw
1 /**
2 * a RISC-V processor
3 *
4 * Copyright (c) 2020, Bruno Levy
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 `default_nettype none
34 `include "clockworks.v"
35 `include "emitter_uart.v"
36
37 module Memory (
38 input wire clk,
39 input wire [31:0] mem_addr, // address to be read
40 output reg [31:0] mem_rdata, // data read from memory
41 input wire mem_rstrb, // goes high when processor wants to read
42 input wire [31:0] mem_wdata, // data to be written
43 input wire [3:0] mem_wmask // masks for writing the 4 bytes (1=write byte)
44 );
45
46 `ifdef RISCV_MEM_16K
47 `define RISCV_MEM 16384
48 `define FIRMWARE "riscv-firmware/memtest16.hex"
49 `else
50 `ifdef RISCV_MEM_48K
51 `define RISCV_MEM 49152
52 `define FIRMWARE "riscv-firmware/memtest48.hex"
53 `else
54 `ifdef RISCV_MEM_88K
55 `define RISCV_MEM 90112
56 `define FIRMWARE "riscv-firmware/memtest88.hex"
57 `endif
58 `endif
59 `endif
60 reg [31:0] MEM [0:`RISCV_MEM / 4 - 1]; // RAM in total
61
62 initial begin
63 $readmemh(`FIRMWARE, MEM);
64 end
65
66 wire [29:0] word_addr = mem_addr[31:2];
67
68 always @(posedge clk) begin
69 if(mem_rstrb) begin
70 mem_rdata <= MEM[word_addr];
71 end
72 if(mem_wmask[0]) MEM[word_addr][ 7:0 ] <= mem_wdata[ 7:0 ];
73 if(mem_wmask[1]) MEM[word_addr][15:8 ] <= mem_wdata[15:8 ];
74 if(mem_wmask[2]) MEM[word_addr][23:16] <= mem_wdata[23:16];
75 if(mem_wmask[3]) MEM[word_addr][31:24] <= mem_wdata[31:24];
76 end
77 endmodule
78
79 module Processor (
80 input wire clk,
81 input wire resetn,
82 output wire [31:0] mem_addr,
83 input wire [31:0] mem_rdata,
84 output wire mem_rstrb,
85 output wire [31:0] mem_wdata,
86 output wire [3:0] mem_wmask
87 );
88
89 reg [31:0] PC=0; // program counter
90 reg [31:0] instr; // current instruction
91
92 // See the table P. 105 in RISC-V manual
93
94 // The 10 RISC-V instructions
95 wire isALUreg = (instr[6:0] == 7'b0110011); // rd <- rs1 OP rs2
96 wire isALUimm = (instr[6:0] == 7'b0010011); // rd <- rs1 OP Iimm
97 wire isBranch = (instr[6:0] == 7'b1100011); // if(rs1 OP rs2) PC<-PC+Bimm
98 wire isJALR = (instr[6:0] == 7'b1100111); // rd <- PC+4; PC<-rs1+Iimm
99 wire isJAL = (instr[6:0] == 7'b1101111); // rd <- PC+4; PC<-PC+Jimm
100 wire isAUIPC = (instr[6:0] == 7'b0010111); // rd <- PC + Uimm
101 wire isLUI = (instr[6:0] == 7'b0110111); // rd <- Uimm
102 wire isLoad = (instr[6:0] == 7'b0000011); // rd <- mem[rs1+Iimm]
103 wire isStore = (instr[6:0] == 7'b0100011); // mem[rs1+Simm] <- rs2
104 wire isSYSTEM = (instr[6:0] == 7'b1110011); // special
105
106 // The 5 immediate formats
107 wire [31:0] Uimm={ instr[31], instr[30:12], {12{1'b0}}};
108 wire [31:0] Iimm={{21{instr[31]}}, instr[30:20]};
109 wire [31:0] Simm={{21{instr[31]}}, instr[30:25],instr[11:7]};
110 wire [31:0] Bimm={{20{instr[31]}}, instr[7],instr[30:25],instr[11:8],1'b0};
111 wire [31:0] Jimm={{12{instr[31]}}, instr[19:12],instr[20],instr[30:21],1'b0};
112
113 // Source and destination registers
114 wire [4:0] rs1Id = instr[19:15];
115 wire [4:0] rs2Id = instr[24:20];
116 wire [4:0] rdId = instr[11:7];
117
118 // function codes
119 wire [2:0] funct3 = instr[14:12];
120 wire [6:0] funct7 = instr[31:25];
121
122 // The registers bank
123 `ifdef FORCE_BRAM
124 (* ram_style = "block" *)
125 `endif
126 reg [31:0] RegisterBank [0:31];
127 reg [31:0] rs1; // value of source
128 reg [31:0] rs2; // registers.
129 wire [31:0] writeBackData; // data to be written to rd
130 wire writeBackEn; // asserted if data should be written to rd
131
132
133 // The ALU
134 wire [31:0] aluIn1 = rs1;
135 wire [31:0] aluIn2 = isALUreg | isBranch ? rs2 : Iimm;
136
137 wire [4:0] shamt = isALUreg ? rs2[4:0] : instr[24:20]; // shift amount
138
139 // The adder is used by both arithmetic instructions and JALR.
140 wire [31:0] aluPlus = aluIn1 + aluIn2;
141
142 // Use a single 33 bits subtract to do subtraction and all comparisons
143 // (trick borrowed from swapforth/J1)
144 wire [32:0] aluMinus = {1'b1, ~aluIn2} + {1'b0,aluIn1} + 33'b1;
145 wire LT = (aluIn1[31] ^ aluIn2[31]) ? aluIn1[31] : aluMinus[32];
146 wire LTU = aluMinus[32];
147 wire EQ = (aluMinus[31:0] == 0);
148
149 // Flip a 32 bit word. Used by the shifter (a single shifter for
150 // left and right shifts, saves silicium !)
151 function [31:0] flip32;
152 input [31:0] x;
153 flip32 = {x[ 0], x[ 1], x[ 2], x[ 3], x[ 4], x[ 5], x[ 6], x[ 7],
154 x[ 8], x[ 9], x[10], x[11], x[12], x[13], x[14], x[15],
155 x[16], x[17], x[18], x[19], x[20], x[21], x[22], x[23],
156 x[24], x[25], x[26], x[27], x[28], x[29], x[30], x[31]};
157 endfunction
158
159 wire [31:0] shifter_in = (funct3 == 3'b001) ? flip32(aluIn1) : aluIn1;
160
161 /* verilator lint_off WIDTH */
162 wire [31:0] shifter =
163 $signed({instr[30] & aluIn1[31], shifter_in}) >>> aluIn2[4:0];
164 /* verilator lint_on WIDTH */
165
166 wire [31:0] leftshift = flip32(shifter);
167
168
169
170 // ADD/SUB/ADDI:
171 // funct7[5] is 1 for SUB and 0 for ADD. We need also to test instr[5]
172 // to make the difference with ADDI
173 //
174 // SRLI/SRAI/SRL/SRA:
175 // funct7[5] is 1 for arithmetic shift (SRA/SRAI) and
176 // 0 for logical shift (SRL/SRLI)
177 reg [31:0] aluOut;
178 always @(*) begin
179 case(funct3)
180 3'b000: aluOut = (funct7[5] & instr[5]) ? aluMinus[31:0] : aluPlus;
181 3'b001: aluOut = leftshift;
182 3'b010: aluOut = {31'b0, LT};
183 3'b011: aluOut = {31'b0, LTU};
184 3'b100: aluOut = (aluIn1 ^ aluIn2);
185 3'b101: aluOut = shifter;
186 3'b110: aluOut = (aluIn1 | aluIn2);
187 3'b111: aluOut = (aluIn1 & aluIn2);
188 endcase
189 end
190
191 // The predicate for branch instructions
192 reg takeBranch;
193 always @(*) begin
194 case(funct3)
195 3'b000: takeBranch = EQ;
196 3'b001: takeBranch = !EQ;
197 3'b100: takeBranch = LT;
198 3'b101: takeBranch = !LT;
199 3'b110: takeBranch = LTU;
200 3'b111: takeBranch = !LTU;
201 default: takeBranch = 1'b0;
202 endcase
203 end
204
205
206 // Address computation
207 // An adder used to compute branch address, JAL address and AUIPC.
208 // branch->PC+Bimm AUIPC->PC+Uimm JAL->PC+Jimm
209 // Equivalent to PCplusImm = PC + (isJAL ? Jimm : isAUIPC ? Uimm : Bimm)
210 wire [31:0] PCplusImm = PC + ( instr[3] ? Jimm[31:0] :
211 instr[4] ? Uimm[31:0] :
212 Bimm[31:0] );
213 wire [31:0] PCplus4 = PC+4;
214
215 // register write back
216 assign writeBackData = (isJAL || isJALR) ? PCplus4 :
217 isLUI ? Uimm :
218 isAUIPC ? PCplusImm :
219 isLoad ? LOAD_data :
220 aluOut;
221
222 wire [31:0] nextPC = ((isBranch && takeBranch) || isJAL) ? PCplusImm :
223 isJALR ? {aluPlus[31:1],1'b0} :
224 PCplus4;
225
226 wire [31:0] loadstore_addr = rs1 + (isStore ? Simm : Iimm);
227
228 // Load
229 // All memory accesses are aligned on 32 bits boundary. For this
230 // reason, we need some circuitry that does unaligned halfword
231 // and byte load/store, based on:
232 // - funct3[1:0]: 00->byte 01->halfword 10->word
233 // - mem_addr[1:0]: indicates which byte/halfword is accessed
234
235 wire mem_byteAccess = funct3[1:0] == 2'b00;
236 wire mem_halfwordAccess = funct3[1:0] == 2'b01;
237
238
239 wire [15:0] LOAD_halfword =
240 loadstore_addr[1] ? mem_rdata[31:16] : mem_rdata[15:0];
241
242 wire [7:0] LOAD_byte =
243 loadstore_addr[0] ? LOAD_halfword[15:8] : LOAD_halfword[7:0];
244
245 // LOAD, in addition to funct3[1:0], LOAD depends on:
246 // - funct3[2] (instr[14]): 0->do sign expansion 1->no sign expansion
247 wire LOAD_sign =
248 !funct3[2] & (mem_byteAccess ? LOAD_byte[7] : LOAD_halfword[15]);
249
250 wire [31:0] LOAD_data =
251 mem_byteAccess ? {{24{LOAD_sign}}, LOAD_byte} :
252 mem_halfwordAccess ? {{16{LOAD_sign}}, LOAD_halfword} :
253 mem_rdata ;
254
255 // Store
256 // ------------------------------------------------------------------------
257
258 assign mem_wdata[ 7: 0] = rs2[7:0];
259 assign mem_wdata[15: 8] = loadstore_addr[0] ? rs2[7:0] : rs2[15: 8];
260 assign mem_wdata[23:16] = loadstore_addr[1] ? rs2[7:0] : rs2[23:16];
261 assign mem_wdata[31:24] = loadstore_addr[0] ? rs2[7:0] :
262 loadstore_addr[1] ? rs2[15:8] : rs2[31:24];
263
264 // The memory write mask:
265 // 1111 if writing a word
266 // 0011 or 1100 if writing a halfword
267 // (depending on loadstore_addr[1])
268 // 0001, 0010, 0100 or 1000 if writing a byte
269 // (depending on loadstore_addr[1:0])
270
271 wire [3:0] STORE_wmask =
272 mem_byteAccess ?
273 (loadstore_addr[1] ?
274 (loadstore_addr[0] ? 4'b1000 : 4'b0100) :
275 (loadstore_addr[0] ? 4'b0010 : 4'b0001)
276 ) :
277 mem_halfwordAccess ?
278 (loadstore_addr[1] ? 4'b1100 : 4'b0011) :
279 4'b1111;
280
281 // The state machine
282 localparam FETCH_INSTR = 0;
283 localparam WAIT_INSTR = 1;
284 localparam FETCH_REGS = 2;
285 localparam EXECUTE = 3;
286 localparam LOAD = 4;
287 localparam WAIT_DATA = 5;
288 localparam STORE = 6;
289 reg [2:0] state = FETCH_INSTR;
290
291 always @(posedge clk) begin
292 if(!resetn) begin
293 PC <= 0;
294 state <= FETCH_INSTR;
295 end else begin
296 if(writeBackEn && rdId != 0) begin
297 RegisterBank[rdId] <= writeBackData;
298 end
299 case(state)
300 FETCH_INSTR: begin
301 state <= WAIT_INSTR;
302 end
303 WAIT_INSTR: begin
304 instr <= mem_rdata;
305 state <= FETCH_REGS;
306 end
307 FETCH_REGS: begin
308 rs1 <= RegisterBank[rs1Id];
309 rs2 <= RegisterBank[rs2Id];
310 state <= EXECUTE;
311 end
312 EXECUTE: begin
313 if(!isSYSTEM) begin
314 PC <= nextPC;
315 end
316 state <= isLoad ? LOAD :
317 isStore ? STORE :
318 FETCH_INSTR;
319 end
320 LOAD: begin
321 state <= WAIT_DATA;
322 end
323 WAIT_DATA: begin
324 state <= FETCH_INSTR;
325 end
326 STORE: begin
327 state <= FETCH_INSTR;
328 end
329 endcase
330 end
331 end
332
333 assign writeBackEn = (state==EXECUTE && !isBranch && !isStore) ||
334 (state==WAIT_DATA) ;
335
336 assign mem_addr = (state == WAIT_INSTR || state == FETCH_INSTR) ?
337 PC : loadstore_addr ;
338 assign mem_rstrb = (state == FETCH_INSTR || state == LOAD);
339 assign mem_wmask = {4{(state == STORE)}} & STORE_wmask;
340
341 endmodule
342
343 module top (
344 input wire clk_i, // system clock
345 input wire rst_i, // reset button
346 output reg [5:0] led, // system LEDs
347 input wire RXD, // UART receive
348 output wire TXD // UART transmit
349 );
350
351 wire clk;
352 wire resetn;
353
354 wire [31:0] mem_addr;
355 wire [31:0] mem_rdata;
356 wire mem_rstrb;
357 wire [31:0] mem_wdata;
358 wire [3:0] mem_wmask;
359
360 Processor CPU(
361 .clk(clk),
362 .resetn(resetn),
363 .mem_addr(mem_addr),
364 .mem_rdata(mem_rdata),
365 .mem_rstrb(mem_rstrb),
366 .mem_wdata(mem_wdata),
367 .mem_wmask(mem_wmask)
368 );
369
370 wire [31:0] RAM_rdata;
371 wire [29:0] mem_wordaddr = mem_addr[31:2];
372 wire isIO = mem_addr[22];
373 wire isRAM = !isIO;
374 wire mem_wstrb = |mem_wmask;
375
376 Memory RAM(
377 .clk(clk),
378 .mem_addr(mem_addr),
379 .mem_rdata(RAM_rdata),
380 .mem_rstrb(isRAM & mem_rstrb),
381 .mem_wdata(mem_wdata),
382 .mem_wmask({4{isRAM}}&mem_wmask)
383 );
384
385
386 // Memory-mapped IO in IO page, 1-hot addressing in word address.
387 localparam IO_LEDS_bit = 0; // W five leds
388 localparam IO_UART_DAT_bit = 1; // W data to send (8 bits)
389 localparam IO_UART_CNTL_bit = 2; // R status. bit 9: busy sending
390 localparam IO_DSP_CLK_bit = 3; // DSP clock
391
392 wire uart_valid = isIO & mem_wstrb & mem_wordaddr[IO_UART_DAT_bit];
393 wire uart_ready;
394
395 corescore_emitter_uart #(
396 .clk_freq_hz(`CPU_FREQ*1000000),
397 .baud_rate(115200)
398 ) UART(
399 .i_clk(clk),
400 .i_rst(!resetn),
401 .i_data(mem_wdata[7:0]),
402 .i_valid(uart_valid),
403 .o_ready(uart_ready),
404 .o_uart_tx(TXD)
405 );
406
407 wire [31:0] IO_rdata =
408 mem_wordaddr[IO_UART_CNTL_bit] ? { 22'b0, !uart_ready, 9'b0} :
409 32'b0;
410
411 assign mem_rdata = isRAM ? RAM_rdata :
412 IO_rdata ;
413
414 // Gearbox and reset circuitry.
415 Clockworks CW(
416 .CLK(clk_i),
417 .RESET(rst_i),
418 .clk(clk),
419 .resetn(resetn)
420 );
421 endmodule
422