| 1 |
2 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//
|
| 3 |
|
|
// Filename: prefetch.v
|
| 4 |
|
|
//
|
| 5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
|
| 6 |
|
|
//
|
| 7 |
|
|
// Purpose: This is a very simple instruction fetch approach. It gets
|
| 8 |
|
|
// one instruction at a time. Future versions should pipeline
|
| 9 |
|
|
// fetches and perhaps even cache results--this doesn't do that.
|
| 10 |
|
|
// It should, however, be simple enough to get things running.
|
| 11 |
|
|
//
|
| 12 |
|
|
// The interface is fascinating. The 'i_pc' input wire is just
|
| 13 |
|
|
// a suggestion of what to load. Other wires may be loaded
|
| 14 |
|
|
// instead. i_pc is what must be output, not necessarily input.
|
| 15 |
|
|
//
|
| 16 |
36 |
dgisselq |
// 20150919 -- Added support for the WB error signal. When reading an
|
| 17 |
|
|
// instruction results in this signal being raised, the pipefetch
|
| 18 |
|
|
// module will set an illegal instruction flag to be returned to
|
| 19 |
|
|
// the CPU together with the instruction. Hence, the ZipCPU
|
| 20 |
|
|
// can trap on it if necessary.
|
| 21 |
|
|
//
|
| 22 |
2 |
dgisselq |
// Creator: Dan Gisselquist, Ph.D.
|
| 23 |
69 |
dgisselq |
// Gisselquist Technology, LLC
|
| 24 |
2 |
dgisselq |
//
|
| 25 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
| 26 |
|
|
//
|
| 27 |
201 |
dgisselq |
// Copyright (C) 2015,2017, Gisselquist Technology, LLC
|
| 28 |
2 |
dgisselq |
//
|
| 29 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
| 30 |
|
|
// modify it under the terms of the GNU General Public License as published
|
| 31 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
| 32 |
|
|
// your option) any later version.
|
| 33 |
|
|
//
|
| 34 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
| 35 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
| 36 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 37 |
|
|
// for more details.
|
| 38 |
|
|
//
|
| 39 |
201 |
dgisselq |
// You should have received a copy of the GNU General Public License along
|
| 40 |
|
|
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
| 41 |
|
|
// target there if the PDF file isn't present.) If not, see
|
| 42 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
| 43 |
|
|
//
|
| 44 |
2 |
dgisselq |
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 45 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
| 46 |
|
|
//
|
| 47 |
|
|
//
|
| 48 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
| 49 |
|
|
//
|
| 50 |
201 |
dgisselq |
//
|
| 51 |
2 |
dgisselq |
// Flash requires a minimum of 4 clocks per byte to read, so that would be
|
| 52 |
|
|
// 4*(4bytes/32bit word) = 16 clocks per word read---and that's in pipeline
|
| 53 |
|
|
// mode which this prefetch does not support. In non--pipelined mode, the
|
| 54 |
|
|
// flash will require (16+6+6)*2 = 56 clocks plus 16 clocks per word read,
|
| 55 |
|
|
// or 72 clocks to fetch one instruction.
|
| 56 |
205 |
dgisselq |
module prefetch(i_clk, i_rst, i_new_pc, i_clear_cache, i_stalled_n, i_pc,
|
| 57 |
|
|
o_i, o_pc, o_valid, o_illegal,
|
| 58 |
2 |
dgisselq |
o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data,
|
| 59 |
36 |
dgisselq |
i_wb_ack, i_wb_stall, i_wb_err, i_wb_data);
|
| 60 |
205 |
dgisselq |
parameter ADDRESS_WIDTH=32;
|
| 61 |
|
|
localparam AW=ADDRESS_WIDTH;
|
| 62 |
|
|
input i_clk, i_rst, i_new_pc, i_clear_cache,
|
| 63 |
|
|
i_stalled_n;
|
| 64 |
48 |
dgisselq |
input [(AW-1):0] i_pc;
|
| 65 |
|
|
output reg [31:0] o_i;
|
| 66 |
205 |
dgisselq |
output wire [(AW-1):0] o_pc;
|
| 67 |
|
|
output reg o_valid;
|
| 68 |
2 |
dgisselq |
// Wishbone outputs
|
| 69 |
48 |
dgisselq |
output reg o_wb_cyc, o_wb_stb;
|
| 70 |
|
|
output wire o_wb_we;
|
| 71 |
|
|
output reg [(AW-1):0] o_wb_addr;
|
| 72 |
|
|
output wire [31:0] o_wb_data;
|
| 73 |
2 |
dgisselq |
// And return inputs
|
| 74 |
205 |
dgisselq |
input i_wb_ack, i_wb_stall, i_wb_err;
|
| 75 |
|
|
input [31:0] i_wb_data;
|
| 76 |
|
|
output reg o_illegal;
|
| 77 |
2 |
dgisselq |
|
| 78 |
|
|
assign o_wb_we = 1'b0;
|
| 79 |
|
|
assign o_wb_data = 32'h0000;
|
| 80 |
|
|
|
| 81 |
|
|
// Let's build it simple and upgrade later: For each instruction
|
| 82 |
|
|
// we do one bus cycle to get the instruction. Later we should
|
| 83 |
|
|
// pipeline this, but for now let's just do one at a time.
|
| 84 |
|
|
initial o_wb_cyc = 1'b0;
|
| 85 |
|
|
initial o_wb_stb = 1'b0;
|
| 86 |
|
|
initial o_wb_addr= 0;
|
| 87 |
|
|
always @(posedge i_clk)
|
| 88 |
205 |
dgisselq |
if ((i_rst)||(i_wb_ack)||(i_wb_err))
|
| 89 |
2 |
dgisselq |
begin
|
| 90 |
|
|
o_wb_cyc <= 1'b0;
|
| 91 |
63 |
dgisselq |
o_wb_stb <= 1'b0;
|
| 92 |
205 |
dgisselq |
end else if ((!o_wb_cyc)&&((i_stalled_n)||(!o_valid)))
|
| 93 |
|
|
begin // Initiate a bus cycle
|
| 94 |
2 |
dgisselq |
o_wb_cyc <= 1'b1;
|
| 95 |
|
|
o_wb_stb <= 1'b1;
|
| 96 |
|
|
end else if (o_wb_cyc) // Independent of ce
|
| 97 |
|
|
begin
|
| 98 |
205 |
dgisselq |
if (~i_wb_stall)
|
| 99 |
2 |
dgisselq |
o_wb_stb <= 1'b0;
|
| 100 |
|
|
end
|
| 101 |
|
|
|
| 102 |
205 |
dgisselq |
reg invalid;
|
| 103 |
|
|
initial invalid = 1'b0;
|
| 104 |
2 |
dgisselq |
always @(posedge i_clk)
|
| 105 |
205 |
dgisselq |
if (!o_wb_cyc)
|
| 106 |
|
|
invalid <= 1'b0;
|
| 107 |
|
|
else if ((i_new_pc)||(i_clear_cache))
|
| 108 |
|
|
invalid <= (!o_wb_stb);
|
| 109 |
|
|
|
| 110 |
|
|
always @(posedge i_clk)
|
| 111 |
|
|
if (i_new_pc)
|
| 112 |
63 |
dgisselq |
o_wb_addr <= i_pc;
|
| 113 |
205 |
dgisselq |
else if ((!o_wb_cyc)&&(i_stalled_n)&&(!invalid))
|
| 114 |
|
|
o_wb_addr <= o_wb_addr + 1'b1;
|
| 115 |
|
|
|
| 116 |
63 |
dgisselq |
always @(posedge i_clk)
|
| 117 |
2 |
dgisselq |
if ((o_wb_cyc)&&(i_wb_ack))
|
| 118 |
|
|
o_i <= i_wb_data;
|
| 119 |
205 |
dgisselq |
|
| 120 |
69 |
dgisselq |
initial o_valid = 1'b0;
|
| 121 |
|
|
initial o_illegal = 1'b0;
|
| 122 |
|
|
always @(posedge i_clk)
|
| 123 |
205 |
dgisselq |
if (i_rst)
|
| 124 |
69 |
dgisselq |
begin
|
| 125 |
205 |
dgisselq |
o_valid <= 1'b0;
|
| 126 |
|
|
o_illegal <= 1'b0;
|
| 127 |
|
|
end else if ((o_wb_cyc)&&(i_wb_ack))
|
| 128 |
69 |
dgisselq |
begin
|
| 129 |
205 |
dgisselq |
o_valid <= (!i_wb_err)&&(!invalid);
|
| 130 |
|
|
o_illegal <= ( i_wb_err)&&(!invalid);
|
| 131 |
|
|
end else if ((i_stalled_n)||(i_clear_cache))
|
| 132 |
|
|
begin
|
| 133 |
69 |
dgisselq |
o_valid <= 1'b0;
|
| 134 |
|
|
o_illegal <= 1'b0;
|
| 135 |
|
|
end
|
| 136 |
2 |
dgisselq |
|
| 137 |
205 |
dgisselq |
assign o_pc = o_wb_addr;
|
| 138 |
2 |
dgisselq |
endmodule
|