Debian Codegraph @

examples/gw5a/femto-riscv-16.v

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