picorv32/picosoc/picosoc.v

139 lines
3.9 KiB
Coq
Raw Normal View History

/*
2017-08-07 19:38:07 +08:00
* PicoSoC - A simple example SoC using PicoRV32
*
* 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.
*
*/
2017-08-07 19:38:07 +08:00
module picosoc (
2017-07-29 22:01:39 +08:00
input clk,
2017-08-07 21:13:27 +08:00
input resetn,
2017-07-29 22:01:39 +08:00
2017-08-07 21:13:27 +08: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 22:01:39 +08:00
output flash_csb,
output flash_clk,
2017-08-07 21:13:27 +08: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 22:01:39 +08:00
);
parameter integer MEM_WORDS = 256;
parameter [31:0] STACKADDR = (4*MEM_WORDS); // end of memory
2017-08-07 21:13:27 +08:00
parameter [31:0] PROGADDR_RESET = 32'h 0110_0000; // 1 MB into flash
2017-07-29 22:01:39 +08:00
wire mem_valid;
wire mem_instr;
2017-08-07 21:13:27 +08:00
wire mem_ready;
2017-07-29 22:01:39 +08:00
wire [31:0] mem_addr;
wire [31:0] mem_wdata;
wire [3:0] mem_wstrb;
2017-08-07 21:13:27 +08:00
wire [31:0] mem_rdata;
2017-07-29 22:01:39 +08:00
wire spimem_ready;
wire [31:0] spimem_rdata;
2017-08-07 21:13:27 +08:00
reg ram_ready;
reg [31:0] ram_rdata;
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;
wire spimemio_cfgreg_sel = (mem_addr == 32'h 0200_0000);
wire [31:0] spimemio_cfgreg_do;
assign mem_ready = (iomem_valid && iomem_ready) || spimem_ready || ram_ready || spimemio_cfgreg_sel;
assign mem_rdata = (iomem_valid && iomem_ready) ? iomem_rdata : spimem_ready ? spimem_rdata : ram_ready ? ram_rdata :
spimemio_cfgreg_sel ? spimemio_cfgreg_do : 32'h xxxx_xxxx;
2017-08-07 21:13:27 +08:00
2017-07-29 22:01:39 +08:00
picorv32 #(
.STACKADDR(STACKADDR),
.PROGADDR_RESET(PROGADDR_RESET)
) cpu (
.clk (clk ),
.resetn (resetn ),
.mem_valid (mem_valid ),
.mem_instr (mem_instr ),
2017-08-07 21:13:27 +08:00
.mem_ready (mem_ready ),
2017-07-29 22:01:39 +08:00
.mem_addr (mem_addr ),
.mem_wdata (mem_wdata ),
.mem_wstrb (mem_wstrb ),
2017-08-07 21:13:27 +08:00
.mem_rdata (mem_rdata )
2017-07-29 22:01:39 +08:00
);
spimemio spimemio (
2017-08-07 22:27:57 +08:00
.clk (clk),
.resetn (resetn),
.valid (mem_valid && mem_addr[31:24] == 8'h 01),
.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),
.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 22:01:39 +08:00
);
reg [31:0] memory [0:MEM_WORDS-1];
always @(posedge clk) begin
2017-08-07 21:13:27 +08:00
ram_ready <= 0;
if (mem_valid && !mem_ready && mem_addr[31:24] == 8'h 00) begin
ram_ready <= 1;
ram_rdata <= memory[mem_addr >> 2];
if (mem_wstrb[0]) memory[mem_addr >> 2][ 7: 0] <= mem_wdata[ 7: 0];
if (mem_wstrb[1]) memory[mem_addr >> 2][15: 8] <= mem_wdata[15: 8];
if (mem_wstrb[2]) memory[mem_addr >> 2][23:16] <= mem_wdata[23:16];
if (mem_wstrb[3]) memory[mem_addr >> 2][31:24] <= mem_wdata[31:24];
2017-07-29 22:01:39 +08:00
end
end
endmodule