| 1 |
50 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
| 2 |
3 |
dgisselq |
//
|
| 3 |
|
|
// Filename: pipemem.v
|
| 4 |
|
|
//
|
| 5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
|
| 6 |
|
|
//
|
| 7 |
|
|
// Purpose: A memory unit to support a CPU, this time one supporting
|
| 8 |
|
|
// pipelined wishbone memory accesses. The goal is to be able
|
| 9 |
|
|
// to issue one pipelined wishbone access per clock, and (given the memory
|
| 10 |
|
|
// is fast enough) to be able to read the results back at one access per
|
| 11 |
|
|
// clock. This renders on-chip memory fast enough to handle single cycle
|
| 12 |
|
|
// (pipelined) access.
|
| 13 |
|
|
//
|
| 14 |
|
|
//
|
| 15 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
| 16 |
|
|
// Gisselquist Technology, LLC
|
| 17 |
|
|
//
|
| 18 |
50 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
| 19 |
3 |
dgisselq |
//
|
| 20 |
50 |
dgisselq |
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
|
| 21 |
3 |
dgisselq |
//
|
| 22 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
| 23 |
|
|
// modify it under the terms of the GNU General Public License as published
|
| 24 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
| 25 |
|
|
// your option) any later version.
|
| 26 |
|
|
//
|
| 27 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
| 28 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
| 29 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 30 |
|
|
// for more details.
|
| 31 |
|
|
//
|
| 32 |
50 |
dgisselq |
// You should have received a copy of the GNU General Public License along
|
| 33 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
| 34 |
|
|
// target there if the PDF file isn't present.) If not, see
|
| 35 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
| 36 |
|
|
//
|
| 37 |
3 |
dgisselq |
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 38 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
| 39 |
|
|
//
|
| 40 |
|
|
//
|
| 41 |
50 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
| 42 |
3 |
dgisselq |
//
|
| 43 |
50 |
dgisselq |
//
|
| 44 |
3 |
dgisselq |
module pipemem(i_clk, i_rst, i_pipe_stb, i_lock,
|
| 45 |
|
|
i_op, i_addr, i_data, i_oreg,
|
| 46 |
|
|
o_busy, o_pipe_stalled, o_valid, o_err, o_wreg, o_result,
|
| 47 |
|
|
o_wb_cyc_gbl, o_wb_cyc_lcl,
|
| 48 |
|
|
o_wb_stb_gbl, o_wb_stb_lcl,
|
| 49 |
50 |
dgisselq |
o_wb_we, o_wb_addr, o_wb_data, o_wb_sel,
|
| 50 |
3 |
dgisselq |
i_wb_ack, i_wb_stall, i_wb_err, i_wb_data);
|
| 51 |
50 |
dgisselq |
parameter ADDRESS_WIDTH=30, IMPLEMENT_LOCK=0;
|
| 52 |
49 |
dgisselq |
localparam AW=ADDRESS_WIDTH;
|
| 53 |
3 |
dgisselq |
input i_clk, i_rst;
|
| 54 |
|
|
input i_pipe_stb, i_lock;
|
| 55 |
|
|
// CPU interface
|
| 56 |
50 |
dgisselq |
input [2:0] i_op;
|
| 57 |
3 |
dgisselq |
input [31:0] i_addr;
|
| 58 |
|
|
input [31:0] i_data;
|
| 59 |
|
|
input [4:0] i_oreg;
|
| 60 |
|
|
// CPU outputs
|
| 61 |
|
|
output wire o_busy;
|
| 62 |
32 |
dgisselq |
output wire o_pipe_stalled;
|
| 63 |
3 |
dgisselq |
output reg o_valid;
|
| 64 |
|
|
output reg o_err;
|
| 65 |
|
|
output reg [4:0] o_wreg;
|
| 66 |
|
|
output reg [31:0] o_result;
|
| 67 |
|
|
// Wishbone outputs
|
| 68 |
|
|
output wire o_wb_cyc_gbl;
|
| 69 |
|
|
output reg o_wb_stb_gbl;
|
| 70 |
|
|
output wire o_wb_cyc_lcl;
|
| 71 |
|
|
output reg o_wb_stb_lcl, o_wb_we;
|
| 72 |
|
|
output reg [(AW-1):0] o_wb_addr;
|
| 73 |
|
|
output reg [31:0] o_wb_data;
|
| 74 |
50 |
dgisselq |
output reg [3:0] o_wb_sel;
|
| 75 |
3 |
dgisselq |
// Wishbone inputs
|
| 76 |
|
|
input i_wb_ack, i_wb_stall, i_wb_err;
|
| 77 |
|
|
input [31:0] i_wb_data;
|
| 78 |
|
|
|
| 79 |
|
|
reg cyc;
|
| 80 |
|
|
reg r_wb_cyc_gbl, r_wb_cyc_lcl;
|
| 81 |
|
|
reg [3:0] rdaddr, wraddr;
|
| 82 |
|
|
wire [3:0] nxt_rdaddr;
|
| 83 |
50 |
dgisselq |
reg [(4+5-1):0] fifo_oreg [0:15];
|
| 84 |
3 |
dgisselq |
initial rdaddr = 0;
|
| 85 |
|
|
initial wraddr = 0;
|
| 86 |
50 |
dgisselq |
|
| 87 |
3 |
dgisselq |
always @(posedge i_clk)
|
| 88 |
50 |
dgisselq |
fifo_oreg[wraddr] <= { i_oreg, i_op[2:1], i_addr[1:0] };
|
| 89 |
|
|
|
| 90 |
3 |
dgisselq |
always @(posedge i_clk)
|
| 91 |
|
|
if ((i_rst)||(i_wb_err))
|
| 92 |
|
|
wraddr <= 0;
|
| 93 |
|
|
else if (i_pipe_stb)
|
| 94 |
50 |
dgisselq |
wraddr <= wraddr + 1'b1;
|
| 95 |
3 |
dgisselq |
always @(posedge i_clk)
|
| 96 |
|
|
if ((i_rst)||(i_wb_err))
|
| 97 |
|
|
rdaddr <= 0;
|
| 98 |
|
|
else if ((i_wb_ack)&&(cyc))
|
| 99 |
50 |
dgisselq |
rdaddr <= rdaddr + 1'b1;
|
| 100 |
|
|
assign nxt_rdaddr = rdaddr + 1'b1;
|
| 101 |
3 |
dgisselq |
|
| 102 |
|
|
wire gbl_stb, lcl_stb;
|
| 103 |
49 |
dgisselq |
assign lcl_stb = (i_addr[31:24]==8'hff);
|
| 104 |
3 |
dgisselq |
assign gbl_stb = (~lcl_stb);
|
| 105 |
|
|
//= ((i_addr[31:8]!=24'hc00000)||(i_addr[7:5]!=3'h0));
|
| 106 |
|
|
|
| 107 |
|
|
initial cyc = 0;
|
| 108 |
|
|
initial r_wb_cyc_lcl = 0;
|
| 109 |
|
|
initial r_wb_cyc_gbl = 0;
|
| 110 |
|
|
always @(posedge i_clk)
|
| 111 |
|
|
if (i_rst)
|
| 112 |
|
|
begin
|
| 113 |
|
|
r_wb_cyc_gbl <= 1'b0;
|
| 114 |
|
|
r_wb_cyc_lcl <= 1'b0;
|
| 115 |
|
|
o_wb_stb_gbl <= 1'b0;
|
| 116 |
|
|
o_wb_stb_lcl <= 1'b0;
|
| 117 |
|
|
cyc <= 1'b0;
|
| 118 |
|
|
end else if (cyc)
|
| 119 |
|
|
begin
|
| 120 |
|
|
if ((~i_wb_stall)&&(~i_pipe_stb))
|
| 121 |
|
|
begin
|
| 122 |
|
|
o_wb_stb_gbl <= 1'b0;
|
| 123 |
|
|
o_wb_stb_lcl <= 1'b0;
|
| 124 |
|
|
// end else if ((i_pipe_stb)&&(~i_wb_stall))
|
| 125 |
|
|
// begin
|
| 126 |
|
|
// o_wb_addr <= i_addr[(AW-1):0];
|
| 127 |
|
|
// o_wb_data <= i_data;
|
| 128 |
|
|
end
|
| 129 |
|
|
|
| 130 |
|
|
if (((i_wb_ack)&&(nxt_rdaddr == wraddr))||(i_wb_err))
|
| 131 |
|
|
begin
|
| 132 |
|
|
r_wb_cyc_gbl <= 1'b0;
|
| 133 |
|
|
r_wb_cyc_lcl <= 1'b0;
|
| 134 |
|
|
cyc <= 1'b0;
|
| 135 |
|
|
end
|
| 136 |
|
|
end else if (i_pipe_stb) // New memory operation
|
| 137 |
|
|
begin // Grab the wishbone
|
| 138 |
|
|
r_wb_cyc_lcl <= lcl_stb;
|
| 139 |
|
|
r_wb_cyc_gbl <= gbl_stb;
|
| 140 |
|
|
o_wb_stb_lcl <= lcl_stb;
|
| 141 |
|
|
o_wb_stb_gbl <= gbl_stb;
|
| 142 |
|
|
cyc <= 1'b1;
|
| 143 |
|
|
// o_wb_addr <= i_addr[(AW-1):0];
|
| 144 |
|
|
// o_wb_data <= i_data;
|
| 145 |
|
|
// o_wb_we <= i_op
|
| 146 |
|
|
end
|
| 147 |
|
|
always @(posedge i_clk)
|
| 148 |
50 |
dgisselq |
if ((!cyc)||(!i_wb_stall))
|
| 149 |
3 |
dgisselq |
begin
|
| 150 |
50 |
dgisselq |
o_wb_addr <= i_addr[(AW+1):2];
|
| 151 |
|
|
if (!i_op[0]) // Always select everything on reads
|
| 152 |
|
|
o_wb_sel <= 4'b1111; // Op is even
|
| 153 |
|
|
else casez({ i_op[2:1], i_addr[1:0] })
|
| 154 |
|
|
4'b100?: o_wb_sel <= 4'b1100; // Op = 5
|
| 155 |
|
|
4'b101?: o_wb_sel <= 4'b0011; // Op = 5
|
| 156 |
|
|
4'b1100: o_wb_sel <= 4'b1000; // Op = 5
|
| 157 |
|
|
4'b1101: o_wb_sel <= 4'b0100; // Op = 7
|
| 158 |
|
|
4'b1110: o_wb_sel <= 4'b0010; // Op = 7
|
| 159 |
|
|
4'b1111: o_wb_sel <= 4'b0001; // Op = 7
|
| 160 |
|
|
default: o_wb_sel <= 4'b1111; // Op = 7
|
| 161 |
|
|
endcase
|
| 162 |
|
|
|
| 163 |
|
|
casez({ i_op[2:1], i_addr[1:0] })
|
| 164 |
|
|
4'b100?: o_wb_data <= { i_data[15:0], 16'h00 };
|
| 165 |
|
|
4'b101?: o_wb_data <= { 16'h00, i_data[15:0] };
|
| 166 |
|
|
4'b1100: o_wb_data <= { i_data[7:0], 24'h00 };
|
| 167 |
|
|
4'b1101: o_wb_data <= { 8'h00, i_data[7:0], 16'h00 };
|
| 168 |
|
|
4'b1110: o_wb_data <= { 16'h00, i_data[7:0], 8'h00 };
|
| 169 |
|
|
4'b1111: o_wb_data <= { 24'h00, i_data[7:0] };
|
| 170 |
|
|
default: o_wb_data <= i_data;
|
| 171 |
|
|
endcase
|
| 172 |
|
|
|
| 173 |
3 |
dgisselq |
end
|
| 174 |
50 |
dgisselq |
|
| 175 |
3 |
dgisselq |
always @(posedge i_clk)
|
| 176 |
|
|
if ((i_pipe_stb)&&(~cyc))
|
| 177 |
50 |
dgisselq |
o_wb_we <= i_op[0];
|
| 178 |
3 |
dgisselq |
|
| 179 |
|
|
initial o_valid = 1'b0;
|
| 180 |
|
|
always @(posedge i_clk)
|
| 181 |
|
|
o_valid <= (cyc)&&(i_wb_ack)&&(~o_wb_we);
|
| 182 |
|
|
initial o_err = 1'b0;
|
| 183 |
|
|
always @(posedge i_clk)
|
| 184 |
|
|
o_err <= (cyc)&&(i_wb_err);
|
| 185 |
|
|
assign o_busy = cyc;
|
| 186 |
|
|
|
| 187 |
50 |
dgisselq |
wire [8:0] w_wreg;
|
| 188 |
|
|
assign w_wreg = fifo_oreg[rdaddr];
|
| 189 |
3 |
dgisselq |
always @(posedge i_clk)
|
| 190 |
50 |
dgisselq |
o_wreg <= w_wreg[8:4];
|
| 191 |
3 |
dgisselq |
always @(posedge i_clk)
|
| 192 |
50 |
dgisselq |
casez(w_wreg[3:0])
|
| 193 |
|
|
4'b1100: o_result = { 24'h00, i_wb_data[31:24] };
|
| 194 |
|
|
4'b1101: o_result = { 24'h00, i_wb_data[23:16] };
|
| 195 |
|
|
4'b1110: o_result = { 24'h00, i_wb_data[15: 8] };
|
| 196 |
|
|
4'b1111: o_result = { 24'h00, i_wb_data[ 7: 0] };
|
| 197 |
|
|
4'b100?: o_result = { 16'h00, i_wb_data[31:16] };
|
| 198 |
|
|
4'b101?: o_result = { 16'h00, i_wb_data[15: 0] };
|
| 199 |
|
|
default: o_result = i_wb_data[31:0];
|
| 200 |
|
|
endcase
|
| 201 |
3 |
dgisselq |
|
| 202 |
|
|
assign o_pipe_stalled = (cyc)
|
| 203 |
|
|
&&((i_wb_stall)||((~o_wb_stb_lcl)&&(~o_wb_stb_gbl)));
|
| 204 |
|
|
|
| 205 |
|
|
generate
|
| 206 |
|
|
if (IMPLEMENT_LOCK != 0)
|
| 207 |
|
|
begin
|
| 208 |
|
|
reg lock_gbl, lock_lcl;
|
| 209 |
|
|
|
| 210 |
|
|
initial lock_gbl = 1'b0;
|
| 211 |
|
|
initial lock_lcl = 1'b0;
|
| 212 |
|
|
always @(posedge i_clk)
|
| 213 |
|
|
begin
|
| 214 |
|
|
lock_gbl <= (i_lock)&&((r_wb_cyc_gbl)||(lock_gbl));
|
| 215 |
50 |
dgisselq |
lock_lcl <= (i_lock)&&((r_wb_cyc_lcl)||(lock_lcl));
|
| 216 |
3 |
dgisselq |
end
|
| 217 |
|
|
|
| 218 |
|
|
assign o_wb_cyc_gbl = (r_wb_cyc_gbl)||(lock_gbl);
|
| 219 |
|
|
assign o_wb_cyc_lcl = (r_wb_cyc_lcl)||(lock_lcl);
|
| 220 |
|
|
|
| 221 |
|
|
end else begin
|
| 222 |
|
|
assign o_wb_cyc_gbl = (r_wb_cyc_gbl);
|
| 223 |
|
|
assign o_wb_cyc_lcl = (r_wb_cyc_lcl);
|
| 224 |
|
|
end endgenerate
|
| 225 |
|
|
|
| 226 |
|
|
endmodule
|