1 |
49 |
dgisselq |
///////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
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 Tecnology, LLC
|
17 |
|
|
//
|
18 |
|
|
///////////////////////////////////////////////////////////////////////////
|
19 |
|
|
//
|
20 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
21 |
|
|
//
|
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 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
33 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
34 |
|
|
//
|
35 |
|
|
//
|
36 |
|
|
///////////////////////////////////////////////////////////////////////////
|
37 |
|
|
//
|
38 |
|
|
module pipemem(i_clk, i_rst, i_pipe_stb,
|
39 |
|
|
i_op, i_addr, i_data, i_oreg,
|
40 |
|
|
o_busy, o_pipe_stalled, o_valid, o_err, o_wreg, o_result,
|
41 |
|
|
o_wb_cyc_gbl, o_wb_cyc_lcl,
|
42 |
|
|
o_wb_stb_gbl, o_wb_stb_lcl,
|
43 |
|
|
o_wb_we, o_wb_addr, o_wb_data,
|
44 |
|
|
i_wb_ack, i_wb_stall, i_wb_err, i_wb_data);
|
45 |
|
|
parameter ADDRESS_WIDTH = 24, AW=ADDRESS_WIDTH;
|
46 |
|
|
input i_clk, i_rst;
|
47 |
|
|
input i_pipe_stb;
|
48 |
|
|
// CPU interface
|
49 |
|
|
input i_op;
|
50 |
|
|
input [31:0] i_addr;
|
51 |
|
|
input [31:0] i_data;
|
52 |
|
|
input [4:0] i_oreg;
|
53 |
|
|
// CPU outputs
|
54 |
|
|
output wire o_busy;
|
55 |
|
|
output wire o_pipe_stalled;
|
56 |
|
|
output reg o_valid;
|
57 |
|
|
output reg o_err;
|
58 |
|
|
output reg [4:0] o_wreg;
|
59 |
|
|
output reg [31:0] o_result;
|
60 |
|
|
// Wishbone outputs
|
61 |
|
|
output reg o_wb_cyc_gbl, o_wb_stb_gbl;
|
62 |
|
|
output reg o_wb_cyc_lcl, o_wb_stb_lcl, o_wb_we;
|
63 |
|
|
output reg [(AW-1):0] o_wb_addr;
|
64 |
|
|
output reg [31:0] o_wb_data;
|
65 |
|
|
// Wishbone inputs
|
66 |
|
|
input i_wb_ack, i_wb_stall, i_wb_err;
|
67 |
|
|
input [31:0] i_wb_data;
|
68 |
|
|
|
69 |
|
|
reg [3:0] rdaddr, wraddr;
|
70 |
|
|
wire [3:0] nxt_rdaddr;
|
71 |
|
|
reg [(5-1):0] fifo_oreg [0:15];
|
72 |
|
|
initial rdaddr = 0;
|
73 |
|
|
initial wraddr = 0;
|
74 |
|
|
always @(posedge i_clk)
|
75 |
|
|
fifo_oreg[wraddr] <= i_oreg;
|
76 |
|
|
always @(posedge i_clk)
|
77 |
|
|
if ((i_rst)||(i_wb_err))
|
78 |
|
|
wraddr <= 0;
|
79 |
|
|
else if (i_pipe_stb)
|
80 |
|
|
wraddr <= wraddr + 1;
|
81 |
|
|
always @(posedge i_clk)
|
82 |
|
|
if ((i_rst)||(i_wb_err))
|
83 |
|
|
rdaddr <= 0;
|
84 |
|
|
else if ((i_wb_ack)&&((o_wb_cyc_gbl)||(o_wb_cyc_lcl)))
|
85 |
|
|
rdaddr <= rdaddr + 1;
|
86 |
|
|
assign nxt_rdaddr = rdaddr + 1;
|
87 |
|
|
|
88 |
|
|
wire gbl_stb, lcl_stb;
|
89 |
|
|
assign lcl_stb = (i_addr[31:8]==24'hc00000)&&(i_addr[7:5]==3'h0);
|
90 |
|
|
assign gbl_stb = ((i_addr[31:8]!=24'hc00000)||(i_addr[7:5]!=3'h0));
|
91 |
|
|
|
92 |
|
|
always @(posedge i_clk)
|
93 |
|
|
if (i_rst)
|
94 |
|
|
begin
|
95 |
|
|
o_wb_cyc_gbl <= 1'b0;
|
96 |
|
|
o_wb_cyc_lcl <= 1'b0;
|
97 |
|
|
o_wb_stb_gbl <= 1'b0;
|
98 |
|
|
o_wb_stb_lcl <= 1'b0;
|
99 |
|
|
end else if ((o_wb_cyc_gbl)||(o_wb_cyc_lcl))
|
100 |
|
|
begin
|
101 |
|
|
if ((~i_wb_stall)&&(~i_pipe_stb))
|
102 |
|
|
begin
|
103 |
|
|
o_wb_stb_gbl <= 1'b0;
|
104 |
|
|
o_wb_stb_lcl <= 1'b0;
|
105 |
|
|
end else if ((i_pipe_stb)&&(~i_wb_stall))
|
106 |
|
|
begin
|
107 |
|
|
o_wb_addr <= i_addr[(AW-1):0];
|
108 |
|
|
o_wb_data <= i_data;
|
109 |
|
|
end
|
110 |
|
|
|
111 |
|
|
if (((i_wb_ack)&&(nxt_rdaddr == wraddr))||(i_wb_err))
|
112 |
|
|
begin
|
113 |
|
|
o_wb_cyc_gbl <= 1'b0;
|
114 |
|
|
o_wb_cyc_lcl <= 1'b0;
|
115 |
|
|
end
|
116 |
|
|
end else if (i_pipe_stb) // New memory operation
|
117 |
|
|
begin // Grab the wishbone
|
118 |
|
|
o_wb_cyc_lcl <= lcl_stb;
|
119 |
|
|
o_wb_cyc_gbl <= gbl_stb;
|
120 |
|
|
o_wb_stb_lcl <= lcl_stb;
|
121 |
|
|
o_wb_stb_gbl <= gbl_stb;
|
122 |
|
|
o_wb_addr <= i_addr[(AW-1):0];
|
123 |
|
|
o_wb_data <= i_data;
|
124 |
|
|
// o_wb_we <= i_op
|
125 |
|
|
end
|
126 |
|
|
always @(posedge i_clk)
|
127 |
|
|
if ((i_pipe_stb)
|
128 |
|
|
&&((~i_wb_stall)
|
129 |
|
|
||((~o_wb_cyc_gbl)&&(~o_wb_cyc_lcl))))
|
130 |
|
|
o_wb_we <= i_op;
|
131 |
|
|
|
132 |
|
|
initial o_valid = 1'b0;
|
133 |
|
|
always @(posedge i_clk)
|
134 |
|
|
o_valid <= ((o_wb_cyc_gbl)||(o_wb_cyc_lcl))&&(i_wb_ack)&&(~o_wb_we);
|
135 |
|
|
initial o_err = 1'b0;
|
136 |
|
|
always @(posedge i_clk)
|
137 |
|
|
o_err <= ((o_wb_cyc_gbl)||(o_wb_cyc_lcl))&&(i_wb_err);
|
138 |
|
|
assign o_busy = (o_wb_cyc_gbl)||(o_wb_cyc_lcl);
|
139 |
|
|
|
140 |
|
|
always @(posedge i_clk)
|
141 |
|
|
o_wreg <= fifo_oreg[rdaddr];
|
142 |
|
|
always @(posedge i_clk)
|
143 |
|
|
if (i_wb_ack)
|
144 |
|
|
o_result <= i_wb_data;
|
145 |
|
|
|
146 |
|
|
assign o_pipe_stalled = ((o_wb_cyc_gbl)||(o_wb_cyc_lcl))
|
147 |
|
|
&&((i_wb_stall)||((~o_wb_stb_lcl)&&(~o_wb_stb_gbl)));
|
148 |
|
|
endmodule
|