source: vis_dev/vis-2.1/examples/treearbiter/4-arbit.v @ 14

Last change on this file since 14 was 11, checked in by cecile, 13 years ago

Add vis

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