1 |
50 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
3 |
dgisselq |
//
|
3 |
|
|
// Filename: wbarbiter.v
|
4 |
|
|
//
|
5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
|
6 |
|
|
//
|
7 |
|
|
// Purpose: At some point in time, I might wish to have two masters connect
|
8 |
|
|
// to the same wishbone bus. As an example, I might wish to have
|
9 |
|
|
// both the instruction fetch and the load/store operators
|
10 |
|
|
// of my Zip CPU access the the same bus. How shall they both
|
11 |
|
|
// get access to the same resource? This module allows the
|
12 |
|
|
// wishbone interfaces from two sources to drive the bus, while
|
13 |
|
|
// guaranteeing that only one drives the bus at a time.
|
14 |
|
|
//
|
15 |
|
|
// The core logic works like this:
|
16 |
|
|
//
|
17 |
|
|
// 1. If 'A' or 'B' asserts the o_cyc line, a bus cycle will begin,
|
18 |
|
|
// with acccess granted to whomever requested it.
|
19 |
|
|
// 2. If both 'A' and 'B' assert o_cyc at the same time, only 'A'
|
20 |
|
|
// will be granted the bus. (If the alternating parameter
|
21 |
|
|
// is set, A and B will alternate who gets the bus in
|
22 |
|
|
// this case.)
|
23 |
|
|
// 3. The bus will remain owned by whomever the bus was granted to
|
24 |
|
|
// until they deassert the o_cyc line.
|
25 |
|
|
// 4. At the end of a bus cycle, o_cyc is guaranteed to be
|
26 |
|
|
// deasserted (low) for one clock.
|
27 |
|
|
// 5. On the next clock, bus arbitration takes place again. If
|
28 |
|
|
// 'A' requests the bus, no matter how long 'B' was
|
29 |
|
|
// waiting, 'A' will then be granted the bus. (Unless
|
30 |
|
|
// again the alternating parameter is set, then the
|
31 |
|
|
// access is guaranteed to switch to B.)
|
32 |
|
|
//
|
33 |
|
|
//
|
34 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
35 |
|
|
// Gisselquist Technology, LLC
|
36 |
|
|
//
|
37 |
50 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
38 |
3 |
dgisselq |
//
|
39 |
50 |
dgisselq |
// Copyright (C) 2015,2017, Gisselquist Technology, LLC
|
40 |
3 |
dgisselq |
//
|
41 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
42 |
|
|
// modify it under the terms of the GNU General Public License as published
|
43 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
44 |
|
|
// your option) any later version.
|
45 |
|
|
//
|
46 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
47 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
48 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
49 |
|
|
// for more details.
|
50 |
|
|
//
|
51 |
50 |
dgisselq |
// You should have received a copy of the GNU General Public License along
|
52 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
53 |
|
|
// target there if the PDF file isn't present.) If not, see
|
54 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
55 |
|
|
//
|
56 |
3 |
dgisselq |
// License: GPL, v3, as defined and found on www.gnu.org,
|
57 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
58 |
|
|
//
|
59 |
|
|
//
|
60 |
50 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
61 |
3 |
dgisselq |
//
|
62 |
50 |
dgisselq |
//
|
63 |
|
|
`define WBA_ALTERNATING
|
64 |
3 |
dgisselq |
module wbarbiter(i_clk, i_rst,
|
65 |
50 |
dgisselq |
// Bus A
|
66 |
|
|
i_a_cyc, i_a_stb, i_a_we, i_a_adr, i_a_dat, i_a_sel, o_a_ack, o_a_stall, o_a_err,
|
67 |
3 |
dgisselq |
// Bus B
|
68 |
50 |
dgisselq |
i_b_cyc, i_b_stb, i_b_we, i_b_adr, i_b_dat, i_b_sel, o_b_ack, o_b_stall, o_b_err,
|
69 |
3 |
dgisselq |
// Both buses
|
70 |
50 |
dgisselq |
o_cyc, o_stb, o_we, o_adr, o_dat, o_sel, i_ack, i_stall, i_err);
|
71 |
3 |
dgisselq |
// 18 bits will address one GB, 4 bytes at a time.
|
72 |
|
|
// 19 bits will allow the ability to address things other than just
|
73 |
|
|
// the 1GB of memory we are expecting.
|
74 |
|
|
parameter DW=32, AW=19;
|
75 |
|
|
// Wishbone doesn't use an i_ce signal. While it could, they dislike
|
76 |
|
|
// what it would (might) do to the synchronous reset signal, i_rst.
|
77 |
|
|
input i_clk, i_rst;
|
78 |
|
|
input [(AW-1):0] i_a_adr, i_b_adr;
|
79 |
|
|
input [(DW-1):0] i_a_dat, i_b_dat;
|
80 |
50 |
dgisselq |
input [(DW/8-1):0] i_a_sel, i_b_sel;
|
81 |
3 |
dgisselq |
input i_a_we, i_a_stb, i_a_cyc;
|
82 |
|
|
input i_b_we, i_b_stb, i_b_cyc;
|
83 |
|
|
output wire o_a_ack, o_b_ack, o_a_stall, o_b_stall,
|
84 |
|
|
o_a_err, o_b_err;
|
85 |
|
|
output wire [(AW-1):0] o_adr;
|
86 |
|
|
output wire [(DW-1):0] o_dat;
|
87 |
50 |
dgisselq |
output wire [(DW/8-1):0] o_sel;
|
88 |
3 |
dgisselq |
output wire o_we, o_stb, o_cyc;
|
89 |
|
|
input i_ack, i_stall, i_err;
|
90 |
|
|
|
91 |
|
|
// All the fancy stuff here is done with the three primary signals:
|
92 |
|
|
// o_cyc
|
93 |
|
|
// w_a_owner
|
94 |
|
|
// w_b_owner
|
95 |
|
|
// These signals are helped by r_cyc, r_a_owner, and r_b_owner.
|
96 |
|
|
// If you understand these signals, all else will fall into place.
|
97 |
|
|
|
98 |
|
|
// r_cyc just keeps track of the last o_cyc value. That way, on
|
99 |
|
|
// the next clock we can tell if we've had one non-cycle before
|
100 |
|
|
// starting another cycle. Specifically, no new cycles will be
|
101 |
|
|
// allowed to begin unless r_cyc=0.
|
102 |
|
|
reg r_cyc;
|
103 |
|
|
always @(posedge i_clk)
|
104 |
|
|
if (i_rst)
|
105 |
|
|
r_cyc <= 1'b0;
|
106 |
|
|
else
|
107 |
|
|
r_cyc <= o_cyc;
|
108 |
|
|
|
109 |
|
|
// Go high immediately (new cycle) if ...
|
110 |
|
|
// Previous cycle was low and *someone* is requesting a bus cycle
|
111 |
|
|
// Go low immadiately if ...
|
112 |
|
|
// We were just high and the owner no longer wants the bus
|
113 |
|
|
// WISHBONE Spec recommends no logic between a FF and the o_cyc
|
114 |
|
|
// This violates that spec. (Rec 3.15, p35)
|
115 |
|
|
assign o_cyc = ((~r_cyc)&&((i_a_cyc)||(i_b_cyc))) || ((r_cyc)&&((w_a_owner)||(w_b_owner)));
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
// Register keeping track of the last owner, wire keeping track of the
|
119 |
|
|
// current owner allowing us to not lose a clock in arbitrating the
|
120 |
|
|
// first clock of the bus cycle
|
121 |
|
|
reg r_a_owner, r_b_owner;
|
122 |
|
|
wire w_a_owner, w_b_owner;
|
123 |
|
|
`ifdef WBA_ALTERNATING
|
124 |
|
|
reg r_a_last_owner;
|
125 |
|
|
|
126 |
|
|
`endif
|
127 |
|
|
always @(posedge i_clk)
|
128 |
|
|
if (i_rst)
|
129 |
|
|
begin
|
130 |
|
|
r_a_owner <= 1'b0;
|
131 |
|
|
r_b_owner <= 1'b0;
|
132 |
|
|
end else begin
|
133 |
|
|
r_a_owner <= w_a_owner;
|
134 |
|
|
r_b_owner <= w_b_owner;
|
135 |
|
|
`ifdef WBA_ALTERNATING
|
136 |
|
|
if (w_a_owner)
|
137 |
|
|
r_a_last_owner <= 1'b1;
|
138 |
|
|
else if (w_b_owner)
|
139 |
|
|
r_a_last_owner <= 1'b0;
|
140 |
|
|
`endif
|
141 |
|
|
end
|
142 |
|
|
//
|
143 |
|
|
// If you are the owner, retain ownership until i_x_cyc is no
|
144 |
|
|
// longer asserted. Likewise, you cannot become owner until o_cyc
|
145 |
|
|
// is de-asserted for one cycle.
|
146 |
|
|
//
|
147 |
|
|
// 'A' is given arbitrary priority over 'B'
|
148 |
|
|
// 'A' may own the bus only if he wants it. When 'A' drops i_a_cyc,
|
149 |
|
|
// o_cyc must drop and so must w_a_owner on the same cycle.
|
150 |
|
|
// However, when 'A' asserts i_a_cyc, he can only capture the bus if
|
151 |
|
|
// it's had an idle cycle.
|
152 |
|
|
// The same is true for 'B' with one exception: if both contend for the
|
153 |
|
|
// bus on the same cycle, 'A' arbitrarily wins.
|
154 |
|
|
`ifdef WBA_ALTERNATING
|
155 |
|
|
assign w_a_owner = (i_a_cyc) // if A requests ownership, and either
|
156 |
|
|
&& ((r_a_owner) // A has already been recognized or
|
157 |
|
|
|| ((~r_cyc) // the bus is free and
|
158 |
|
|
&&((~i_b_cyc) // B has not requested, or if he
|
159 |
|
|
||(~r_a_last_owner)) )); // has, it's A's turn
|
160 |
|
|
assign w_b_owner = (i_b_cyc)&& ((r_b_owner) || ((~r_cyc)&&((~i_a_cyc)||(r_a_last_owner)) ));
|
161 |
|
|
`else
|
162 |
|
|
assign w_a_owner = (i_a_cyc)&& ((r_a_owner) || (~r_cyc) );
|
163 |
|
|
assign w_b_owner = (i_b_cyc)&& ((r_b_owner) || ((~r_cyc)&&(~i_a_cyc)) );
|
164 |
|
|
`endif
|
165 |
|
|
|
166 |
|
|
// Realistically, if neither master owns the bus, the output is a
|
167 |
|
|
// don't care. Thus we trigger off whether or not 'A' owns the bus.
|
168 |
|
|
// If 'B' owns it all we care is that 'A' does not. Likewise, if
|
169 |
|
|
// neither owns the bus than the values on the various lines are
|
170 |
50 |
dgisselq |
// irrelevant. (This allows us to get two outputs per Xilinx 6-LUT)
|
171 |
|
|
assign o_stb = (o_cyc) && ((w_a_owner) ? i_a_stb : i_b_stb);
|
172 |
|
|
assign o_we = (w_a_owner) ? i_a_we : i_b_we;
|
173 |
3 |
dgisselq |
assign o_adr = (w_a_owner) ? i_a_adr : i_b_adr;
|
174 |
|
|
assign o_dat = (w_a_owner) ? i_a_dat : i_b_dat;
|
175 |
50 |
dgisselq |
assign o_sel = (w_a_owner) ? i_a_sel : i_b_sel;
|
176 |
3 |
dgisselq |
|
177 |
|
|
// We cannot allow the return acknowledgement to ever go high if
|
178 |
|
|
// the master in question does not own the bus. Hence we force it
|
179 |
|
|
// low if the particular master doesn't own the bus.
|
180 |
|
|
assign o_a_ack = (w_a_owner) ? i_ack : 1'b0;
|
181 |
|
|
assign o_b_ack = (w_b_owner) ? i_ack : 1'b0;
|
182 |
|
|
|
183 |
|
|
// Stall must be asserted on the same cycle the input master asserts
|
184 |
|
|
// the bus, if the bus isn't granted to him.
|
185 |
|
|
assign o_a_stall = (w_a_owner) ? i_stall : 1'b1;
|
186 |
|
|
assign o_b_stall = (w_b_owner) ? i_stall : 1'b1;
|
187 |
|
|
|
188 |
|
|
//
|
189 |
|
|
//
|
190 |
|
|
assign o_a_err = (w_a_owner) ? i_err : 1'b0;
|
191 |
|
|
assign o_b_err = (w_b_owner) ? i_err : 1'b0;
|
192 |
|
|
|
193 |
|
|
endmodule
|
194 |
|
|
|