[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 | */ |
---|
| 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 | |
---|
| 30 | boolean wire ua1, ua2, ua3, ua4, xa, ya, sa; |
---|
| 31 | handShakeType wire ur1, ur2, ur3, ur4, xr, yr, sr; |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | boolean wire constTRUE, constFALSE; |
---|
| 35 | |
---|
| 36 | assign constTRUE = myTRUE; |
---|
| 37 | assign constFALSE = myFALSE; |
---|
| 38 | |
---|
| 39 | assign sa = constFALSE; |
---|
| 40 | |
---|
| 41 | arbitCell C0 ( clk, constTRUE, xr, yr, xa, ya, sr, sa ); |
---|
| 42 | arbitCell C1 ( clk, constFALSE, ur1, ur2, ua1, ua2, xr, xa ); |
---|
| 43 | arbitCell C2 ( clk, constFALSE, ur3, ur4, ua3, ua4, yr, ya ); |
---|
| 44 | |
---|
| 45 | procModel P1( clk, ua1, ur1 ); |
---|
| 46 | procModel P2( clk, ua2, ur2 ); |
---|
| 47 | procModel P3( clk, ua3, ur3 ); |
---|
| 48 | procModel P4( clk, ua4, ur4 ); |
---|
| 49 | |
---|
| 50 | endmodule |
---|
| 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 | |
---|
| 62 | module arbitCell(clk, topCell, urLeft, urRight, uaLeft, uaRight, xr, xa); |
---|
| 63 | input clk; |
---|
| 64 | input topCell, urLeft, urRight, xa; |
---|
| 65 | output uaLeft, uaRight, xr; |
---|
| 66 | |
---|
| 67 | boolean wire topCell, uaLeft, uaRight, xa; |
---|
| 68 | handShakeType wire urLeft, urRight, xr; |
---|
| 69 | |
---|
| 70 | boolean wire uaLeft, uaRight; |
---|
| 71 | |
---|
| 72 | boolean reg prevLeft, prevRight; |
---|
| 73 | initial prevLeft = myFALSE; |
---|
| 74 | initial prevRight = myTRUE; |
---|
| 75 | |
---|
| 76 | boolean reg processedLeft, processedRight; |
---|
| 77 | initial processedLeft = myFALSE; |
---|
| 78 | initial processedRight = myFALSE; |
---|
| 79 | |
---|
| 80 | boolean wire mustGiveParent; /* essentially a macro for checking if must release the token to parent */ |
---|
| 81 | assign mustGiveParent = ( processedLeft == myTRUE ) && ( processedRight == myTRUE ) |
---|
| 82 | && ( !( topCell == myTRUE ) ) ? myTRUE : myFALSE; |
---|
| 83 | |
---|
| 84 | boolean reg holdToken; |
---|
| 85 | initial holdToken = topCell; |
---|
| 86 | |
---|
| 87 | boolean wire childOwns; /* essentially a macro for checking if a descendant owns the token */ |
---|
| 88 | assign childOwns = ( urLeft == lock || urRight == lock ) ? myTRUE : myFALSE; |
---|
| 89 | |
---|
| 90 | boolean wire giveChild; /* essentially a macro for checking if a child is being given the token */ |
---|
| 91 | assign 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 | */ |
---|
| 99 | assign uaLeft = ( !( mustGiveParent == myTRUE ) && ( holdToken == myTRUE && urLeft == request |
---|
| 100 | && ( ! ( urRight == request ) || prevRight == myTRUE ) ) ) ? myTRUE : myFALSE; |
---|
| 101 | |
---|
| 102 | /* |
---|
| 103 | * same as above for right |
---|
| 104 | * |
---|
| 105 | */ |
---|
| 106 | assign 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 | */ |
---|
| 118 | assign 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 | |
---|
| 127 | always @(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 | |
---|
| 189 | end |
---|
| 190 | |
---|
| 191 | endmodule |
---|
| 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 | */ |
---|
| 200 | module procModel(clk, ack, req ); |
---|
| 201 | input clk; |
---|
| 202 | input ack; |
---|
| 203 | output req; |
---|
| 204 | |
---|
| 205 | boolean wire ack; |
---|
| 206 | handShakeType wire req; |
---|
| 207 | |
---|
| 208 | assign req = procState; |
---|
| 209 | |
---|
| 210 | wire[0:2] randChoice; |
---|
| 211 | handShakeType reg procState; |
---|
| 212 | |
---|
| 213 | initial procState = idle; |
---|
| 214 | |
---|
| 215 | assign randChoice = $ND(0,1,2,3,4,5,6,7); |
---|
| 216 | |
---|
| 217 | always @(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 |
---|
| 235 | end |
---|
| 236 | |
---|
| 237 | endmodule |
---|