picorv32/spiflash/spimemio.v

110 lines
2.6 KiB
Coq
Raw Normal View History

/*
* Interface module for SPI flash and PicoRV32 native memory interface
*
* 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-07-29 22:01:39 +08:00
module spimemio (
input clk, resetn,
input valid,
output reg ready,
input [23:0] addr,
output reg [31:0] rdata,
output reg flash_csb,
output reg flash_clk,
output flash_io0,
input flash_io1,
input flash_io2,
input flash_io3
2017-07-29 22:01:39 +08:00
);
2017-07-30 03:34:11 +08:00
parameter ENABLE_PREFETCH = 1;
2017-07-29 22:01:39 +08:00
reg [23:0] addr_q;
reg addr_q_vld;
reg [31:0] buffer;
reg [6:0] xfer_cnt;
reg xfer_wait;
2017-07-30 03:34:11 +08:00
reg prefetch;
2017-07-29 22:01:39 +08:00
reg spi_mosi;
wire spi_miso;
assign flash_io0 = spi_mosi;
assign spi_miso = flash_io1;
2017-07-29 22:01:39 +08:00
always @(posedge clk) begin
ready <= 0;
if (!resetn) begin
flash_csb <= 1;
flash_clk <= 1;
2017-07-29 22:01:39 +08:00
xfer_cnt <= 8;
buffer <= 8'hAB << 24;
addr_q_vld <= 0;
xfer_wait <= 0;
2017-07-30 03:34:11 +08:00
prefetch <= 0;
2017-07-29 22:01:39 +08:00
end else
if (xfer_cnt) begin
if (flash_csb) begin
flash_csb <= 0;
2017-07-29 22:01:39 +08:00
end else
if (flash_clk) begin
flash_clk <= 0;
2017-07-29 22:01:39 +08:00
spi_mosi <= buffer[31];
end else begin
flash_clk <= 1;
2017-07-29 22:01:39 +08:00
buffer <= {buffer, spi_miso};
xfer_cnt <= xfer_cnt - 1;
end
end else
if (xfer_wait) begin
ready <= 1;
rdata <= {buffer[7:0], buffer[15:8], buffer[23:16], buffer[31:24]};
xfer_wait <= 0;
end else
if (valid && !ready) begin
if (addr_q_vld && addr_q == addr) begin
addr_q <= addr + 4;
addr_q_vld <= 1;
2017-07-30 03:34:11 +08:00
if (!prefetch)
xfer_cnt <= 32;
2017-07-29 22:01:39 +08:00
xfer_wait <= 1;
2017-07-30 03:34:11 +08:00
prefetch <= 0;
2017-07-29 22:01:39 +08:00
end else begin
flash_csb <= 1;
2017-07-29 22:01:39 +08:00
buffer <= {8'h 03, addr};
addr_q <= addr + 4;
addr_q_vld <= 1;
xfer_cnt <= 64;
xfer_wait <= 1;
2017-07-30 03:34:11 +08:00
prefetch <= 0;
2017-07-29 22:01:39 +08:00
end
2017-07-30 03:34:11 +08:00
end else if (ENABLE_PREFETCH && !prefetch) begin
prefetch <= 1;
xfer_cnt <= 32;
end
if (ENABLE_PREFETCH && resetn && prefetch && valid && !ready && addr_q != addr) begin
prefetch <= 0;
xfer_cnt <= 0;
xfer_wait <= 0;
flash_clk <= 1;
2017-07-29 22:01:39 +08:00
end
end
endmodule