2017-08-04 15:05:05 -04:00
|
|
|
/*
|
2017-08-07 07:38:07 -04:00
|
|
|
* PicoSoC - A simple example SoC using PicoRV32
|
2017-08-04 15:05:05 -04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-05-18 17:28:18 -04:00
|
|
|
`ifndef PICORV32_REGS
|
2017-10-01 09:45:46 -04:00
|
|
|
`ifdef PICORV32_V
|
|
|
|
`error "picosoc.v must be read before picorv32.v!"
|
|
|
|
`endif
|
|
|
|
|
|
|
|
`define PICORV32_REGS picosoc_regs
|
2018-05-18 17:28:18 -04:00
|
|
|
`endif
|
2017-10-01 09:45:46 -04:00
|
|
|
|
2017-08-07 07:38:07 -04:00
|
|
|
module picosoc (
|
2017-07-29 10:01:39 -04:00
|
|
|
input clk,
|
2017-08-07 09:13:27 -04:00
|
|
|
input resetn,
|
2017-07-29 10:01:39 -04:00
|
|
|
|
2017-08-07 09:13:27 -04:00
|
|
|
output iomem_valid,
|
|
|
|
input iomem_ready,
|
|
|
|
output [ 3:0] iomem_wstrb,
|
|
|
|
output [31:0] iomem_addr,
|
|
|
|
output [31:0] iomem_wdata,
|
|
|
|
input [31:0] iomem_rdata,
|
2017-07-29 10:01:39 -04:00
|
|
|
|
2017-09-21 22:52:44 -04:00
|
|
|
input irq_5,
|
|
|
|
input irq_6,
|
|
|
|
input irq_7,
|
|
|
|
|
2017-08-11 13:30:53 -04:00
|
|
|
output ser_tx,
|
|
|
|
input ser_rx,
|
|
|
|
|
2017-08-04 15:05:05 -04:00
|
|
|
output flash_csb,
|
|
|
|
output flash_clk,
|
2017-08-07 09:13:27 -04:00
|
|
|
|
|
|
|
output flash_io0_oe,
|
|
|
|
output flash_io1_oe,
|
|
|
|
output flash_io2_oe,
|
|
|
|
output flash_io3_oe,
|
|
|
|
|
|
|
|
output flash_io0_do,
|
|
|
|
output flash_io1_do,
|
|
|
|
output flash_io2_do,
|
|
|
|
output flash_io3_do,
|
|
|
|
|
|
|
|
input flash_io0_di,
|
|
|
|
input flash_io1_di,
|
|
|
|
input flash_io2_di,
|
|
|
|
input flash_io3_di
|
2017-07-29 10:01:39 -04:00
|
|
|
);
|
|
|
|
parameter integer MEM_WORDS = 256;
|
|
|
|
parameter [31:0] STACKADDR = (4*MEM_WORDS); // end of memory
|
2017-09-15 08:47:50 -04:00
|
|
|
parameter [31:0] PROGADDR_RESET = 32'h 0010_0000; // 1 MB into flash
|
2017-07-29 10:01:39 -04:00
|
|
|
|
2017-09-21 22:52:44 -04:00
|
|
|
reg [31:0] irq;
|
|
|
|
wire irq_stall = 0;
|
|
|
|
wire irq_uart = 0;
|
|
|
|
|
|
|
|
always @* begin
|
|
|
|
irq = 0;
|
|
|
|
irq[3] = irq_stall;
|
|
|
|
irq[4] = irq_uart;
|
|
|
|
irq[5] = irq_5;
|
|
|
|
irq[6] = irq_6;
|
|
|
|
irq[7] = irq_7;
|
|
|
|
end
|
|
|
|
|
2017-07-29 10:01:39 -04:00
|
|
|
wire mem_valid;
|
|
|
|
wire mem_instr;
|
2017-08-07 09:13:27 -04:00
|
|
|
wire mem_ready;
|
2017-07-29 10:01:39 -04:00
|
|
|
wire [31:0] mem_addr;
|
|
|
|
wire [31:0] mem_wdata;
|
|
|
|
wire [3:0] mem_wstrb;
|
2017-08-07 09:13:27 -04:00
|
|
|
wire [31:0] mem_rdata;
|
2017-07-29 10:01:39 -04:00
|
|
|
|
|
|
|
wire spimem_ready;
|
|
|
|
wire [31:0] spimem_rdata;
|
|
|
|
|
2017-08-07 09:13:27 -04:00
|
|
|
reg ram_ready;
|
2017-10-01 09:45:46 -04:00
|
|
|
wire [31:0] ram_rdata;
|
2017-08-07 09:13:27 -04:00
|
|
|
|
|
|
|
assign iomem_valid = mem_valid && (mem_addr[31:24] > 8'h 01);
|
|
|
|
assign iomem_wstrb = mem_wstrb;
|
|
|
|
assign iomem_addr = mem_addr;
|
|
|
|
assign iomem_wdata = mem_wdata;
|
|
|
|
|
2017-09-17 14:38:03 -04:00
|
|
|
wire spimemio_cfgreg_sel = mem_valid && (mem_addr == 32'h 0200_0000);
|
2017-08-11 09:57:42 -04:00
|
|
|
wire [31:0] spimemio_cfgreg_do;
|
|
|
|
|
2017-09-17 14:38:03 -04:00
|
|
|
wire simpleuart_reg_div_sel = mem_valid && (mem_addr == 32'h 0200_0004);
|
2017-08-11 13:30:53 -04:00
|
|
|
wire [31:0] simpleuart_reg_div_do;
|
|
|
|
|
2017-09-17 14:38:03 -04:00
|
|
|
wire simpleuart_reg_dat_sel = mem_valid && (mem_addr == 32'h 0200_0008);
|
2017-08-11 13:30:53 -04:00
|
|
|
wire [31:0] simpleuart_reg_dat_do;
|
|
|
|
wire simpleuart_reg_dat_wait;
|
|
|
|
|
|
|
|
assign mem_ready = (iomem_valid && iomem_ready) || spimem_ready || ram_ready || spimemio_cfgreg_sel ||
|
|
|
|
simpleuart_reg_div_sel || (simpleuart_reg_dat_sel && !simpleuart_reg_dat_wait);
|
|
|
|
|
2017-08-11 09:57:42 -04:00
|
|
|
assign mem_rdata = (iomem_valid && iomem_ready) ? iomem_rdata : spimem_ready ? spimem_rdata : ram_ready ? ram_rdata :
|
2017-08-11 13:30:53 -04:00
|
|
|
spimemio_cfgreg_sel ? spimemio_cfgreg_do : simpleuart_reg_div_sel ? simpleuart_reg_div_do :
|
2017-09-15 08:47:50 -04:00
|
|
|
simpleuart_reg_dat_sel ? simpleuart_reg_dat_do : 32'h 0000_0000;
|
2017-08-07 09:13:27 -04:00
|
|
|
|
2017-07-29 10:01:39 -04:00
|
|
|
picorv32 #(
|
|
|
|
.STACKADDR(STACKADDR),
|
2017-09-21 22:52:44 -04:00
|
|
|
.PROGADDR_RESET(PROGADDR_RESET),
|
|
|
|
.PROGADDR_IRQ(32'h 0000_0000),
|
|
|
|
.BARREL_SHIFTER(1),
|
|
|
|
.COMPRESSED_ISA(1),
|
|
|
|
.ENABLE_MUL(1),
|
|
|
|
.ENABLE_DIV(1),
|
|
|
|
.ENABLE_IRQ(1),
|
|
|
|
.ENABLE_IRQ_QREGS(0)
|
2017-07-29 10:01:39 -04:00
|
|
|
) cpu (
|
|
|
|
.clk (clk ),
|
|
|
|
.resetn (resetn ),
|
|
|
|
.mem_valid (mem_valid ),
|
|
|
|
.mem_instr (mem_instr ),
|
2017-08-07 09:13:27 -04:00
|
|
|
.mem_ready (mem_ready ),
|
2017-07-29 10:01:39 -04:00
|
|
|
.mem_addr (mem_addr ),
|
|
|
|
.mem_wdata (mem_wdata ),
|
|
|
|
.mem_wstrb (mem_wstrb ),
|
2017-09-21 22:52:44 -04:00
|
|
|
.mem_rdata (mem_rdata ),
|
|
|
|
.irq (irq )
|
2017-07-29 10:01:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
spimemio spimemio (
|
2017-08-07 10:27:57 -04:00
|
|
|
.clk (clk),
|
|
|
|
.resetn (resetn),
|
2017-09-15 08:47:50 -04:00
|
|
|
.valid (mem_valid && mem_addr >= 4*MEM_WORDS && mem_addr < 32'h 0200_0000),
|
2017-08-07 10:27:57 -04:00
|
|
|
.ready (spimem_ready),
|
|
|
|
.addr (mem_addr[23:0]),
|
|
|
|
.rdata (spimem_rdata),
|
|
|
|
|
|
|
|
.flash_csb (flash_csb ),
|
|
|
|
.flash_clk (flash_clk ),
|
|
|
|
|
|
|
|
.flash_io0_oe (flash_io0_oe),
|
|
|
|
.flash_io1_oe (flash_io1_oe),
|
|
|
|
.flash_io2_oe (flash_io2_oe),
|
|
|
|
.flash_io3_oe (flash_io3_oe),
|
|
|
|
|
|
|
|
.flash_io0_do (flash_io0_do),
|
|
|
|
.flash_io1_do (flash_io1_do),
|
|
|
|
.flash_io2_do (flash_io2_do),
|
|
|
|
.flash_io3_do (flash_io3_do),
|
|
|
|
|
|
|
|
.flash_io0_di (flash_io0_di),
|
|
|
|
.flash_io1_di (flash_io1_di),
|
|
|
|
.flash_io2_di (flash_io2_di),
|
2017-08-11 09:57:42 -04:00
|
|
|
.flash_io3_di (flash_io3_di),
|
|
|
|
|
|
|
|
.cfgreg_we(spimemio_cfgreg_sel ? mem_wstrb : 4'b 0000),
|
|
|
|
.cfgreg_di(mem_wdata),
|
|
|
|
.cfgreg_do(spimemio_cfgreg_do)
|
2017-07-29 10:01:39 -04:00
|
|
|
);
|
|
|
|
|
2017-08-11 13:30:53 -04:00
|
|
|
simpleuart simpleuart (
|
|
|
|
.clk (clk ),
|
|
|
|
.resetn (resetn ),
|
|
|
|
|
|
|
|
.ser_tx (ser_tx ),
|
|
|
|
.ser_rx (ser_rx ),
|
|
|
|
|
|
|
|
.reg_div_we (simpleuart_reg_div_sel ? mem_wstrb : 4'b 0000),
|
|
|
|
.reg_div_di (mem_wdata),
|
|
|
|
.reg_div_do (simpleuart_reg_div_do),
|
|
|
|
|
|
|
|
.reg_dat_we (simpleuart_reg_dat_sel ? mem_wstrb[0] : 1'b 0),
|
|
|
|
.reg_dat_re (simpleuart_reg_dat_sel && !mem_wstrb),
|
|
|
|
.reg_dat_di (mem_wdata),
|
|
|
|
.reg_dat_do (simpleuart_reg_dat_do),
|
|
|
|
.reg_dat_wait(simpleuart_reg_dat_wait)
|
|
|
|
);
|
|
|
|
|
2017-10-01 09:45:46 -04:00
|
|
|
always @(posedge clk)
|
|
|
|
ram_ready <= mem_valid && !mem_ready && mem_addr < 4*MEM_WORDS;
|
|
|
|
|
|
|
|
picosoc_mem #(.WORDS(MEM_WORDS)) memory (
|
|
|
|
.clk(clk),
|
|
|
|
.wen((mem_valid && !mem_ready && mem_addr < 4*MEM_WORDS) ? mem_wstrb : 4'b0),
|
|
|
|
.addr(mem_addr[23:2]),
|
|
|
|
.wdata(mem_wdata),
|
|
|
|
.rdata(ram_rdata)
|
|
|
|
);
|
|
|
|
endmodule
|
|
|
|
|
|
|
|
// Implementation note:
|
|
|
|
// Replace the following two modules with wrappers for your SRAM cells.
|
|
|
|
|
|
|
|
module picosoc_regs (
|
|
|
|
input clk, wen,
|
|
|
|
input [5:0] waddr,
|
|
|
|
input [5:0] raddr1,
|
|
|
|
input [5:0] raddr2,
|
|
|
|
input [31:0] wdata,
|
|
|
|
output [31:0] rdata1,
|
|
|
|
output [31:0] rdata2
|
|
|
|
);
|
|
|
|
reg [31:0] regs [0:31];
|
|
|
|
|
|
|
|
always @(posedge clk)
|
|
|
|
if (wen) regs[waddr[4:0]] <= wdata;
|
|
|
|
|
|
|
|
assign rdata1 = regs[raddr1[4:0]];
|
|
|
|
assign rdata2 = regs[raddr2[4:0]];
|
|
|
|
endmodule
|
|
|
|
|
|
|
|
module picosoc_mem #(
|
|
|
|
parameter integer WORDS = 256
|
|
|
|
) (
|
|
|
|
input clk,
|
|
|
|
input [3:0] wen,
|
|
|
|
input [21:0] addr,
|
|
|
|
input [31:0] wdata,
|
|
|
|
output reg [31:0] rdata
|
|
|
|
);
|
|
|
|
reg [31:0] mem [0:WORDS-1];
|
2017-07-29 10:01:39 -04:00
|
|
|
|
|
|
|
always @(posedge clk) begin
|
2017-10-01 09:45:46 -04:00
|
|
|
rdata <= mem[addr];
|
|
|
|
if (wen[0]) mem[addr][ 7: 0] <= wdata[ 7: 0];
|
|
|
|
if (wen[1]) mem[addr][15: 8] <= wdata[15: 8];
|
|
|
|
if (wen[2]) mem[addr][23:16] <= wdata[23:16];
|
|
|
|
if (wen[3]) mem[addr][31:24] <= wdata[31:24];
|
2017-07-29 10:01:39 -04:00
|
|
|
end
|
|
|
|
endmodule
|
2017-10-01 09:45:46 -04:00
|
|
|
|