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

Subversion Repositories zipcpu

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

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

powered by: WebSVN 2.1.0

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