OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [rtl/] [aux/] [wbarbiter.v] - Blame information for rev 173

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
///////////////////////////////////////////////////////////////////////////
2
//
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 69 dgisselq
//              Gisselquist Technology, LLC
36 2 dgisselq
//
37
///////////////////////////////////////////////////////////////////////////
38
//
39
// Copyright (C) 2015, Gisselquist Technology, LLC
40
//
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
// License:     GPL, v3, as defined and found on www.gnu.org,
52
//              http://www.gnu.org/licenses/gpl.html
53
//
54
//
55
///////////////////////////////////////////////////////////////////////////
56
//
57
`define WBA_ALTERNATING
58
module  wbarbiter(i_clk, i_rst,
59
        // Bus A
60 36 dgisselq
        i_a_adr, i_a_dat, i_a_we, i_a_stb, i_a_cyc, o_a_ack, o_a_stall, o_a_err,
61 2 dgisselq
        // Bus B
62 36 dgisselq
        i_b_adr, i_b_dat, i_b_we, i_b_stb, i_b_cyc, o_b_ack, o_b_stall, o_b_err,
63 2 dgisselq
        // Both buses
64 36 dgisselq
        o_adr, o_dat, o_we, o_stb, o_cyc, i_ack, i_stall, i_err);
65 2 dgisselq
        // 18 bits will address one GB, 4 bytes at a time.
66
        // 19 bits will allow the ability to address things other than just
67
        // the 1GB of memory we are expecting.
68
        parameter                       DW=32, AW=19;
69
        // Wishbone doesn't use an i_ce signal.  While it could, they dislike
70
        // what it would (might) do to the synchronous reset signal, i_rst.
71
        input                           i_clk, i_rst;
72
        input           [(AW-1):0]       i_a_adr, i_b_adr;
73
        input           [(DW-1):0]       i_a_dat, i_b_dat;
74
        input                           i_a_we, i_a_stb, i_a_cyc;
75
        input                           i_b_we, i_b_stb, i_b_cyc;
76 36 dgisselq
        output  wire                    o_a_ack, o_b_ack, o_a_stall, o_b_stall,
77
                                        o_a_err, o_b_err;
78 2 dgisselq
        output  wire    [(AW-1):0]       o_adr;
79
        output  wire    [(DW-1):0]       o_dat;
80
        output  wire                    o_we, o_stb, o_cyc;
81 36 dgisselq
        input                           i_ack, i_stall, i_err;
82 2 dgisselq
 
83
        // All the fancy stuff here is done with the three primary signals:
84
        //      o_cyc
85
        //      w_a_owner
86
        //      w_b_owner
87
        // These signals are helped by r_cyc, r_a_owner, and r_b_owner.
88
        // If you understand these signals, all else will fall into place.
89
 
90
        // r_cyc just keeps track of the last o_cyc value.  That way, on
91
        // the next clock we can tell if we've had one non-cycle before
92
        // starting another cycle.  Specifically, no new cycles will be
93
        // allowed to begin unless r_cyc=0.
94
        reg     r_cyc;
95
        always @(posedge i_clk)
96
                if (i_rst)
97
                        r_cyc <= 1'b0;
98
                else
99
                        r_cyc <= o_cyc;
100
 
101
        // Go high immediately (new cycle) if ...
102
        //      Previous cycle was low and *someone* is requesting a bus cycle
103
        // Go low immadiately if ...
104
        //      We were just high and the owner no longer wants the bus
105
        // WISHBONE Spec recommends no logic between a FF and the o_cyc
106
        //      This violates that spec.  (Rec 3.15, p35)
107
        assign o_cyc = ((~r_cyc)&&((i_a_cyc)||(i_b_cyc))) || ((r_cyc)&&((w_a_owner)||(w_b_owner)));
108
 
109
 
110
        // Register keeping track of the last owner, wire keeping track of the
111
        // current owner allowing us to not lose a clock in arbitrating the
112
        // first clock of the bus cycle
113
        reg     r_a_owner, r_b_owner;
114
        wire    w_a_owner, w_b_owner;
115
`ifdef  WBA_ALTERNATING
116
        reg     r_a_last_owner;
117 36 dgisselq
        // Stall must be asserted on the same cycle the input master asserts
118
        // the bus, if the bus isn't granted to him.
119
        assign  o_a_stall = (w_a_owner) ? i_stall : 1'b1;
120
        assign  o_b_stall = (w_b_owner) ? i_stall : 1'b1;
121
 
122 2 dgisselq
`endif
123
        always @(posedge i_clk)
124
                if (i_rst)
125
                begin
126
                        r_a_owner <= 1'b0;
127
                        r_b_owner <= 1'b0;
128
                end else begin
129
                        r_a_owner <= w_a_owner;
130
                        r_b_owner <= w_b_owner;
131
`ifdef  WBA_ALTERNATING
132
                        if (w_a_owner)
133
                                r_a_last_owner <= 1'b1;
134
                        else if (w_b_owner)
135
                                r_a_last_owner <= 1'b0;
136
`endif
137
                end
138
        //
139
        // If you are the owner, retain ownership until i_x_cyc is no
140
        // longer asserted.  Likewise, you cannot become owner until o_cyc
141
        // is de-asserted for one cycle.
142
        //
143
        // 'A' is given arbitrary priority over 'B'
144
        // 'A' may own the bus only if he wants it.  When 'A' drops i_a_cyc,
145
        // o_cyc must drop and so must w_a_owner on the same cycle.
146
        // However, when 'A' asserts i_a_cyc, he can only capture the bus if
147
        // it's had an idle cycle.
148
        // The same is true for 'B' with one exception: if both contend for the
149
        // bus on the same cycle, 'A' arbitrarily wins.
150
`ifdef  WBA_ALTERNATING
151
        assign w_a_owner = (i_a_cyc)    // if A requests ownership, and either
152
                        && ((r_a_owner) // A has already been recognized or
153
                        || ((~r_cyc) // the bus is free and
154
                                &&((~i_b_cyc) // B has not requested, or if he 
155
                                ||(~r_a_last_owner)) )); // has, it's A's turn
156
        assign w_b_owner = (i_b_cyc)&& ((r_b_owner) || ((~r_cyc)&&((~i_a_cyc)||(r_a_last_owner)) ));
157
`else
158
        assign w_a_owner = (i_a_cyc)&& ((r_a_owner) ||  (~r_cyc) );
159
        assign w_b_owner = (i_b_cyc)&& ((r_b_owner) || ((~r_cyc)&&(~i_a_cyc)) );
160
`endif
161
 
162
        // Realistically, if neither master owns the bus, the output is a
163
        // don't care.  Thus we trigger off whether or not 'A' owns the bus.
164
        // If 'B' owns it all we care is that 'A' does not.  Likewise, if 
165
        // neither owns the bus than the values on the various lines are
166
        // irrelevant.
167
        assign o_adr = (w_a_owner) ? i_a_adr : i_b_adr;
168
        assign o_dat = (w_a_owner) ? i_a_dat : i_b_dat;
169
        assign o_we  = (w_a_owner) ? i_a_we  : i_b_we;
170
        assign o_stb = (o_cyc) && ((w_a_owner) ? i_a_stb : i_b_stb);
171
 
172
        // We cannot allow the return acknowledgement to ever go high if
173
        // the master in question does not own the bus.  Hence we force it
174
        // low if the particular master doesn't own the bus.
175
        assign  o_a_ack   = (w_a_owner) ? i_ack   : 1'b0;
176
        assign  o_b_ack   = (w_b_owner) ? i_ack   : 1'b0;
177
 
178 36 dgisselq
        //
179
        //
180
        assign  o_a_err = (w_a_owner) ? i_err : 1'b0;
181
        assign  o_b_err = (w_b_owner) ? i_err : 1'b0;
182 2 dgisselq
 
183
endmodule
184
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.