1 | /************************************************************************** |
---|
2 | |
---|
3 | This is a simple tree arbiter, adapted from Dills' thesis p 89. |
---|
4 | There are four processors which |
---|
5 | share a single resource. A token defines which processor has the |
---|
6 | resource. The arbiter cells have two children and one parent. |
---|
7 | An arbiter can request the token, release it, etc. |
---|
8 | |
---|
9 | Adnan Aziz |
---|
10 | July 10, 1996 |
---|
11 | UT Austin |
---|
12 | |
---|
13 | ***************************************************************************/ |
---|
14 | |
---|
15 | /* |
---|
16 | * Symbolic variables. |
---|
17 | * |
---|
18 | */ |
---|
19 | typedef enum {myTRUE, myFALSE} boolean; |
---|
20 | typedef enum {idle, request, lock, release} handShakeType; |
---|
21 | |
---|
22 | /* |
---|
23 | * The inteconnections between the processors and the cells. |
---|
24 | * |
---|
25 | */ |
---|
26 | module main( clk ); |
---|
27 | input clk; |
---|
28 | |
---|
29 | boolean wire xa, ya, sa; |
---|
30 | handShakeType wire xr, yr, sr; |
---|
31 | |
---|
32 | boolean wire constTRUE; |
---|
33 | |
---|
34 | |
---|
35 | assign sa = myFALSE; |
---|
36 | assign constTRUE = myTRUE; |
---|
37 | |
---|
38 | arbitCell F0( clk, constTRUE, xr, yr, xa, ya, sr, sa ); |
---|
39 | fourCells G1( clk, xa, xr ); |
---|
40 | fourCells G2( clk, ya, yr ); |
---|
41 | |
---|
42 | endmodule |
---|
43 | |
---|
44 | |
---|
45 | module fourCells( clk, sa, sr ); |
---|
46 | input clk; |
---|
47 | input sa; |
---|
48 | output sr; |
---|
49 | |
---|
50 | boolean wire ua1, ua2, ua3, ua4, xa, ya, sa; |
---|
51 | handShakeType wire ur1, ur2, ur3, ur4, xr, yr, sr; |
---|
52 | |
---|
53 | wire[0:1] procChoice; |
---|
54 | |
---|
55 | boolean wire constTRUE, constFALSE; |
---|
56 | |
---|
57 | assign constTRUE = myTRUE; |
---|
58 | assign constFALSE = myFALSE; |
---|
59 | |
---|
60 | arbitCell C0 ( clk, constFALSE, xr, yr, xa, ya, sr, sa ); |
---|
61 | arbitCell C1 ( clk, constFALSE, ur1, ur2, ua1, ua2, xr, xa ); |
---|
62 | arbitCell C2 ( clk, constFALSE, ur3, ur4, ua3, ua4, yr, ya ); |
---|
63 | |
---|
64 | procModel P1( clk, ua1, ur1 ); |
---|
65 | procModel P2( clk, ua2, ur2 ); |
---|
66 | procModel P3( clk, ua3, ur3 ); |
---|
67 | procModel P4( clk, ua4, ur4 ); |
---|
68 | |
---|
69 | endmodule |
---|
70 | |
---|
71 | |
---|
72 | /* |
---|
73 | * The arbiter cell has two inputs from children and two outputs to chidren. |
---|
74 | * One input from parent, and one output to parent. The latch holdToken corresponds |
---|
75 | * to whether the cell holds the token. The latches prevLeft and prevRight are |
---|
76 | * used to keep track of which way the token went last, to impart fairness |
---|
77 | * in the scheduling of the children. |
---|
78 | * |
---|
79 | */ |
---|
80 | |
---|
81 | |
---|
82 | /* |
---|
83 | * Add comments |
---|
84 | */ |
---|
85 | module procModel(clk, ack, req ); |
---|
86 | input clk; |
---|
87 | input ack; |
---|
88 | output req; |
---|
89 | |
---|
90 | boolean wire ack; |
---|
91 | handShakeType wire req; |
---|
92 | wire randChoice; |
---|
93 | |
---|
94 | assign req = procState; |
---|
95 | assign randChoice = $ND(0,1); |
---|
96 | |
---|
97 | handShakeType reg procState; |
---|
98 | |
---|
99 | initial procState = idle; |
---|
100 | |
---|
101 | always @(posedge clk) begin |
---|
102 | |
---|
103 | if ( procState == idle && (randChoice == 1) ) |
---|
104 | begin |
---|
105 | procState = request; |
---|
106 | end |
---|
107 | else if ( procState == request && ack == myTRUE ) |
---|
108 | begin |
---|
109 | procState = lock; |
---|
110 | end |
---|
111 | else if ( procState == lock && (randChoice == 1 ) ) |
---|
112 | begin |
---|
113 | procState = release; |
---|
114 | end |
---|
115 | else if ( procState == release ) |
---|
116 | begin |
---|
117 | procState = idle; |
---|
118 | end |
---|
119 | end |
---|
120 | |
---|
121 | endmodule |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | module arbitCell(clk, topCell, urLeft, urRight, uaLeft, uaRight, xr, xa); |
---|
126 | input clk; |
---|
127 | input topCell, urLeft, urRight, xa; |
---|
128 | output uaLeft, uaRight, xr; |
---|
129 | |
---|
130 | boolean wire topCell, uaLeft, uaRight, xa; |
---|
131 | handShakeType wire urLeft, urRight, xr; |
---|
132 | |
---|
133 | boolean wire uaLeft, uaRight; |
---|
134 | |
---|
135 | boolean reg prevLeft, prevRight; |
---|
136 | initial prevLeft = myFALSE; |
---|
137 | initial prevRight = myTRUE; |
---|
138 | |
---|
139 | boolean reg processedLeft, processedRight; |
---|
140 | initial processedLeft = myFALSE; |
---|
141 | initial processedRight = myFALSE; |
---|
142 | |
---|
143 | boolean wire mustGiveParent; /* essentially a macro for checking if must release the token to parent */ |
---|
144 | assign mustGiveParent = ( processedLeft == myTRUE ) && ( processedRight == myTRUE ) |
---|
145 | && ( !( topCell == myTRUE ) ) ? myTRUE : myFALSE; |
---|
146 | |
---|
147 | boolean reg holdToken; |
---|
148 | initial holdToken = topCell; |
---|
149 | |
---|
150 | boolean wire childOwns; /* essentially a macro for checking if a descendant owns the token */ |
---|
151 | assign childOwns = ( urLeft == lock || urRight == lock ) ? myTRUE : myFALSE; |
---|
152 | |
---|
153 | boolean wire giveChild; /* essentially a macro for checking if a child is being given the token */ |
---|
154 | assign giveChild = ( uaLeft == myTRUE || uaRight == myTRUE ) ? myTRUE : myFALSE; |
---|
155 | |
---|
156 | /* |
---|
157 | * Condition under which the token is given to the left child |
---|
158 | * Must own token, have request from left, and either no request from right or if there is |
---|
159 | * a request from the right it, should be lefts turn (since right went the last time |
---|
160 | * |
---|
161 | */ |
---|
162 | assign uaLeft = ( !( mustGiveParent == myTRUE ) && ( holdToken == myTRUE && urLeft == request |
---|
163 | && ( ! ( urRight == request ) || prevRight == myTRUE ) ) ) ? myTRUE : myFALSE; |
---|
164 | |
---|
165 | /* |
---|
166 | * same as above for right |
---|
167 | * |
---|
168 | */ |
---|
169 | assign uaRight = ( !( mustGiveParent == myTRUE ) && ( holdToken == myTRUE && urRight == request |
---|
170 | && ( ! ( urLeft == request ) || prevLeft == myTRUE ) ) ) ? myTRUE : myFALSE; |
---|
171 | |
---|
172 | /* |
---|
173 | * signal to parent: |
---|
174 | * |
---|
175 | * 1. request if dont own the token, |
---|
176 | * 2. lock if descendant has locked the token |
---|
177 | * 3. release if child has released token |
---|
178 | * 4. idle otherwise |
---|
179 | * |
---|
180 | */ |
---|
181 | assign xr = ( holdToken == myFALSE && ( urLeft == request || urRight == request ) ) |
---|
182 | ? request : |
---|
183 | ( childOwns == myTRUE ) |
---|
184 | ? lock : |
---|
185 | ( holdToken == myTRUE && ( ( ( mustGiveParent == myTRUE ) || |
---|
186 | ! ( ( urLeft == request || urRight == request ) ) ) |
---|
187 | && !( topCell == myTRUE ) ) ) |
---|
188 | ? release : idle; |
---|
189 | |
---|
190 | always @(posedge clk) begin |
---|
191 | |
---|
192 | /* |
---|
193 | * keep track of whether we hold the token or not |
---|
194 | * |
---|
195 | */ |
---|
196 | |
---|
197 | if ( xa == myTRUE ) |
---|
198 | begin |
---|
199 | holdToken = myTRUE; |
---|
200 | end |
---|
201 | else if ( giveChild == myTRUE ) |
---|
202 | begin |
---|
203 | holdToken = myFALSE; |
---|
204 | end |
---|
205 | else if ( urLeft == release || urRight == release ) |
---|
206 | begin |
---|
207 | holdToken = myTRUE; |
---|
208 | end |
---|
209 | else if ( xr == release ) |
---|
210 | begin |
---|
211 | holdToken = myFALSE; |
---|
212 | end |
---|
213 | |
---|
214 | /* |
---|
215 | * keep track of which child got the token last |
---|
216 | * |
---|
217 | */ |
---|
218 | if ( uaLeft == myTRUE ) |
---|
219 | begin |
---|
220 | prevLeft = myTRUE; |
---|
221 | prevRight = myFALSE; |
---|
222 | end |
---|
223 | else if ( uaRight == myTRUE ) |
---|
224 | begin |
---|
225 | prevLeft = myFALSE; |
---|
226 | prevRight = myTRUE; |
---|
227 | end |
---|
228 | |
---|
229 | /* |
---|
230 | * child has finished processing the token |
---|
231 | * |
---|
232 | */ |
---|
233 | |
---|
234 | if ( urLeft == release ) |
---|
235 | begin |
---|
236 | processedLeft = myTRUE; |
---|
237 | end |
---|
238 | else if ( urRight == release ) |
---|
239 | begin |
---|
240 | processedRight = myTRUE; |
---|
241 | end |
---|
242 | /* |
---|
243 | * if we have given the token to both children, must now give it up |
---|
244 | * |
---|
245 | */ |
---|
246 | else if ( ( processedLeft == myTRUE ) && ( processedRight == myTRUE ) ) |
---|
247 | begin |
---|
248 | processedLeft = myFALSE; |
---|
249 | processedRight = myFALSE; |
---|
250 | end |
---|
251 | |
---|
252 | end |
---|
253 | |
---|
254 | endmodule |
---|