Debian Codegraph @

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