Debian Codegraph @

examples/gw5a/femto-riscv-15.v

raw
1 /**
2 *
3 * Step 15: Creating a RISC-V processor
4 * Load
5 *
6 * Copyright (c) 2020, Bruno Levy
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the copyright holder nor the names of its
20 * contributors may be used to endorse or promote products derived from
21 * this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 `default_nettype none
36 `include "clockworks.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 );
44
45 reg [31:0] MEM [0:255];
46
47 localparam slow_bit=21;
48
49 `include "riscv_assembly.v"
50 integer L0_ = 8;
51 integer wait_ = 32;
52 integer L1_ = 40;
53
54 initial begin
55 LI(s0,0);
56 LI(s1,16);
57 Label(L0_);
58 LB(a0,s0,400); // LEDs are plugged on a0 (=x10)
59 CALL(LabelRef(wait_));
60 ADDI(s0,s0,1);
61 BNE(s0,s1, LabelRef(L0_));
62 EBREAK();
63
64 Label(wait_);
65 LI(t0,1);
66 SLLI(t0,t0,slow_bit);
67 Label(L1_);
68 ADDI(t0,t0,-1);
69 BNEZ(t0,LabelRef(L1_));
70 RET();
71
72 endASM();
73
74 // Note: index 100 (word address)
75 // corresponds to
76 // address 400 (byte address)
77 MEM[100] = {8'h4, 8'h3, 8'h2, 8'h1};
78 MEM[101] = {8'h8, 8'h7, 8'h6, 8'h5};
79 MEM[102] = {8'hc, 8'hb, 8'ha, 8'h9};
80 MEM[103] = {8'hff, 8'hf, 8'he, 8'hd};
81 end
82
83 always @(posedge clk) begin
84 if(mem_rstrb) begin
85 mem_rdata <= MEM[mem_addr[31:2]];
86 end
87 end
88 endmodule
89
90
91 module Processor (
92 input clk,
93 input resetn,
94 output [31:0] mem_addr,
95 input [31:0] mem_rdata,
96 output mem_rstrb,
97 output reg [31:0] x10 = 0
98 );
99
100 reg [31:0] PC=0; // program counter
101 reg [31:0] instr; // current instruction
102
103 // See the table P. 105 in RISC-V manual
104
105 // The 10 RISC-V instructions
106 wire isALUreg = (instr[6:0] == 7'b0110011); // rd <- rs1 OP rs2
107 wire isALUimm = (instr[6:0] == 7'b0010011); // rd <- rs1 OP Iimm
108 wire isBranch = (instr[6:0] == 7'b1100011); // if(rs1 OP rs2) PC<-PC+Bimm
109 wire isJALR = (instr[6:0] == 7'b1100111); // rd <- PC+4; PC<-rs1+Iimm
110 wire isJAL = (instr[6:0] == 7'b1101111); // rd <- PC+4; PC<-PC+Jimm
111 wire isAUIPC = (instr[6:0] == 7'b0010111); // rd <- PC + Uimm
112 wire isLUI = (instr[6:0] == 7'b0110111); // rd <- Uimm
113 wire isLoad = (instr[6:0] == 7'b0000011); // rd <- mem[rs1+Iimm]
114 wire isStore = (instr[6:0] == 7'b0100011); // mem[rs1+Simm] <- rs2
115 wire isSYSTEM = (instr[6:0] == 7'b1110011); // special
116
117 // The 5 immediate formats
118 wire [31:0] Uimm={ instr[31], instr[30:12], {12{1'b0}}};
119 wire [31:0] Iimm={{21{instr[31]}}, instr[30:20]};
120 wire [31:0] Simm={{21{instr[31]}}, instr[30:25],instr[11:7]};
121 wire [31:0] Bimm={{20{instr[31]}}, instr[7],instr[30:25],instr[11:8],1'b0};
122 wire [31:0] Jimm={{12{instr[31]}}, instr[19:12],instr[20],instr[30:21],1'b0};
123
124 // Source and destination registers
125 wire [4:0] rs1Id = instr[19:15];
126 wire [4:0] rs2Id = instr[24:20];
127 wire [4:0] rdId = instr[11:7];
128
129 // function codes
130 wire [2:0] funct3 = instr[14:12];
131 wire [6:0] funct7 = instr[31:25];
132
133 // The registers bank
134 `ifdef FORCE_BRAM
135 (* ram_style = "block" *)
136 `endif
137 reg [31:0] RegisterBank [0:31];
138 reg [31:0] rs1; // value of source
139 reg [31:0] rs2; // registers.
140 wire [31:0] writeBackData; // data to be written to rd
141 wire writeBackEn; // asserted if data should be written to rd
142
143 // The ALU
144 wire [31:0] aluIn1 = rs1;
145 wire [31:0] aluIn2 = isALUreg | isBranch ? rs2 : Iimm;
146
147 wire [4:0] shamt = isALUreg ? rs2[4:0] : instr[24:20]; // shift amount
148
149 // The adder is used by both arithmetic instructions and JALR.
150 wire [31:0] aluPlus = aluIn1 + aluIn2;
151
152 // Use a single 33 bits subtract to do subtraction and all comparisons
153 // (trick borrowed from swapforth/J1)
154 wire [32:0] aluMinus = {1'b1, ~aluIn2} + {1'b0,aluIn1} + 33'b1;
155 wire LT = (aluIn1[31] ^ aluIn2[31]) ? aluIn1[31] : aluMinus[32];
156 wire LTU = aluMinus[32];
157 wire EQ = (aluMinus[31:0] == 0);
158
159 // Flip a 32 bit word. Used by the shifter (a single shifter for
160 // left and right shifts, saves silicium !)
161 function [31:0] flip32;
162 input [31:0] x;
163 flip32 = {x[ 0], x[ 1], x[ 2], x[ 3], x[ 4], x[ 5], x[ 6], x[ 7],
164 x[ 8], x[ 9], x[10], x[11], x[12], x[13], x[14], x[15],
165 x[16], x[17], x[18], x[19], x[20], x[21], x[22], x[23],
166 x[24], x[25], x[26], x[27], x[28], x[29], x[30], x[31]};
167 endfunction
168
169 wire [31:0] shifter_in = (funct3 == 3'b001) ? flip32(aluIn1) : aluIn1;
170
171 /* verilator lint_off WIDTH */
172 wire [31:0] shifter =
173 $signed({instr[30] & aluIn1[31], shifter_in}) >>> aluIn2[4:0];
174 /* verilator lint_on WIDTH */
175
176 wire [31:0] leftshift = flip32(shifter);
177
178
179
180 // ADD/SUB/ADDI:
181 // funct7[5] is 1 for SUB and 0 for ADD. We need also to test instr[5]
182 // to make the difference with ADDI
183 //
184 // SRLI/SRAI/SRL/SRA:
185 // funct7[5] is 1 for arithmetic shift (SRA/SRAI) and
186 // 0 for logical shift (SRL/SRLI)
187 reg [31:0] aluOut;
188 always @(*) begin
189 case(funct3)
190 3'b000: aluOut = (funct7[5] & instr[5]) ? aluMinus[31:0] : aluPlus;
191 3'b001: aluOut = leftshift;
192 3'b010: aluOut = {31'b0, LT};
193 3'b011: aluOut = {31'b0, LTU};
194 3'b100: aluOut = (aluIn1 ^ aluIn2);
195 3'b101: aluOut = shifter;
196 3'b110: aluOut = (aluIn1 | aluIn2);
197 3'b111: aluOut = (aluIn1 & aluIn2);
198 endcase
199 end
200
201 // The predicate for branch instructions
202 reg takeBranch;
203 always @(*) begin
204 case(funct3)
205 3'b000: takeBranch = EQ;
206 3'b001: takeBranch = !EQ;
207 3'b100: takeBranch = LT;
208 3'b101: takeBranch = !LT;
209 3'b110: takeBranch = LTU;
210 3'b111: takeBranch = !LTU;
211 default: takeBranch = 1'b0;
212 endcase
213 end
214
215
216 // Address computation
217 // An adder used to compute branch address, JAL address and AUIPC.
218 // branch->PC+Bimm AUIPC->PC+Uimm JAL->PC+Jimm
219 // Equivalent to PCplusImm = PC + (isJAL ? Jimm : isAUIPC ? Uimm : Bimm)
220 wire [31:0] PCplusImm = PC + ( instr[3] ? Jimm[31:0] :
221 instr[4] ? Uimm[31:0] :
222 Bimm[31:0] );
223 wire [31:0] PCplus4 = PC+4;
224
225 // register write back
226 assign writeBackData = (isJAL || isJALR) ? PCplus4 :
227 isLUI ? Uimm :
228 isAUIPC ? PCplusImm :
229 isLoad ? LOAD_data :
230 aluOut;
231
232 assign writeBackEn = (state==EXECUTE && !isBranch && !isStore && !isLoad) ||
233 (state==WAIT_DATA) ;
234
235 wire [31:0] nextPC = ((isBranch && takeBranch) || isJAL) ? PCplusImm :
236 isJALR ? {aluPlus[31:1],1'b0} :
237 PCplus4;
238
239 wire [31:0] loadstore_addr = rs1 + Iimm;
240
241 // Load
242 // All memory accesses are aligned on 32 bits boundary. For this
243 // reason, we need some circuitry that does unaligned halfword
244 // and byte load/store, based on:
245 // - funct3[1:0]: 00->byte 01->halfword 10->word
246 // - mem_addr[1:0]: indicates which byte/halfword is accessed
247
248 wire mem_byteAccess = funct3[1:0] == 2'b00;
249 wire mem_halfwordAccess = funct3[1:0] == 2'b01;
250
251
252 wire [15:0] LOAD_halfword =
253 loadstore_addr[1] ? mem_rdata[31:16] : mem_rdata[15:0];
254
255 wire [7:0] LOAD_byte =
256 loadstore_addr[0] ? LOAD_halfword[15:8] : LOAD_halfword[7:0];
257
258 // LOAD, in addition to funct3[1:0], LOAD depends on:
259 // - funct3[2] (instr[14]): 0->do sign expansion 1->no sign expansion
260 wire LOAD_sign =
261 !funct3[2] & (mem_byteAccess ? LOAD_byte[7] : LOAD_halfword[15]);
262
263 wire [31:0] LOAD_data =
264 mem_byteAccess ? {{24{LOAD_sign}}, LOAD_byte} :
265 mem_halfwordAccess ? {{16{LOAD_sign}}, LOAD_halfword} :
266 mem_rdata ;
267
268
269 // The state machine
270 localparam FETCH_INSTR = 0;
271 localparam WAIT_INSTR = 1;
272 localparam FETCH_REGS = 2;
273 localparam EXECUTE = 3;
274 localparam LOAD = 4;
275 localparam WAIT_DATA = 5;
276 reg [2:0] state = FETCH_INSTR;
277
278 always @(posedge clk) begin
279 if(!resetn) begin
280 PC <= 0;
281 state <= FETCH_INSTR;
282 end else begin
283 if(writeBackEn && rdId != 0) begin
284 RegisterBank[rdId] <= writeBackData;
285 if(rdId == 10) begin
286 x10 <= writeBackData;
287 end
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 : FETCH_INSTR;
307 end
308 LOAD: begin
309 state <= WAIT_DATA;
310 end
311 WAIT_DATA: begin
312 state <= FETCH_INSTR;
313 end
314 endcase
315 end
316 end
317
318 assign mem_addr = (state == WAIT_INSTR || state == FETCH_INSTR) ?
319 PC : loadstore_addr ;
320 assign mem_rstrb = (state == FETCH_INSTR || state == LOAD);
321
322 endmodule
323
324
325 module top (
326 input clk_i, // system clock
327 input rst_i, // reset button
328 output [5:0] led, // system LEDs
329 input RXD, // UART receive
330 output TXD // UART transmit
331 );
332
333 wire clk;
334 wire resetn;
335
336 Memory RAM(
337 .clk(clk),
338 .mem_addr(mem_addr),
339 .mem_rdata(mem_rdata),
340 .mem_rstrb(mem_rstrb)
341 );
342
343 wire [31:0] mem_addr;
344 wire [31:0] mem_rdata;
345 wire mem_rstrb;
346 wire [31:0] x10;
347
348 Processor CPU(
349 .clk(clk),
350 .resetn(resetn),
351 .mem_addr(mem_addr),
352 .mem_rdata(mem_rdata),
353 .mem_rstrb(mem_rstrb),
354 .x10(x10)
355 );
356 assign led = ~x10[5:0];
357
358 // Gearbox and reset circuitry.
359 Clockworks #(
360 .SLOW(0) //Specifying a value other than zero here may result in
361 // nextpnr being unable to route the clock for some boards. BUGFIX is under development.
362 ) CW (
363 .CLK(clk_i),
364 .RESET(rst_i),
365 .clk(clk),
366 .resetn(resetn)
367 );
368
369 assign TXD = 1'b0; // not used for now
370 endmodule
371