1 | typedef enum {A, B, C, X} selection; |
---|
2 | typedef enum {IDLE, READY, BUSY} controller_state; |
---|
3 | typedef enum {NO_REQ, REQ, HAVE_TOKEN} client_state; |
---|
4 | |
---|
5 | module main(clk); |
---|
6 | input clk; |
---|
7 | output ackA, ackB, ackC; |
---|
8 | |
---|
9 | selection wire sel; |
---|
10 | wire active; |
---|
11 | |
---|
12 | assign active = pass_tokenA || pass_tokenB || pass_tokenC; |
---|
13 | |
---|
14 | controller controllerA(clk, reqA, ackA, sel, pass_tokenA, A); |
---|
15 | controller controllerB(clk, reqB, ackB, sel, pass_tokenB, B); |
---|
16 | controller controllerC(clk, reqC, ackC, sel, pass_tokenC, C); |
---|
17 | arbiter arbiter(clk, sel, active); |
---|
18 | |
---|
19 | client clientA(clk, reqA, ackA); |
---|
20 | client clientB(clk, reqB, ackB); |
---|
21 | client clientC(clk, reqC, ackC); |
---|
22 | |
---|
23 | endmodule |
---|
24 | |
---|
25 | module controller(clk, req, ack, sel, pass_token, id); |
---|
26 | input clk, req, sel, id; |
---|
27 | output ack, pass_token; |
---|
28 | |
---|
29 | selection wire sel, id; |
---|
30 | reg ack, pass_token; |
---|
31 | controller_state reg state; |
---|
32 | |
---|
33 | initial state = IDLE; |
---|
34 | initial ack = 0; |
---|
35 | initial pass_token = 1; |
---|
36 | |
---|
37 | wire is_selected; |
---|
38 | assign is_selected = (sel == id); |
---|
39 | |
---|
40 | always @(posedge clk) begin |
---|
41 | case(state) |
---|
42 | IDLE: |
---|
43 | if (is_selected) |
---|
44 | if (req) |
---|
45 | begin |
---|
46 | state = READY; |
---|
47 | pass_token = 0; /* dropping off this line causes a safety bug */ |
---|
48 | end |
---|
49 | else |
---|
50 | pass_token = 1; |
---|
51 | else |
---|
52 | pass_token = 0; |
---|
53 | READY: |
---|
54 | begin |
---|
55 | state = BUSY; |
---|
56 | ack = 1; |
---|
57 | end |
---|
58 | BUSY: |
---|
59 | if (!req) |
---|
60 | begin |
---|
61 | state = IDLE; |
---|
62 | ack = 0; |
---|
63 | pass_token = 1; |
---|
64 | end |
---|
65 | endcase |
---|
66 | end |
---|
67 | endmodule |
---|
68 | |
---|
69 | module arbiter(clk, sel, active); |
---|
70 | input clk, active; |
---|
71 | output sel; |
---|
72 | |
---|
73 | selection wire sel; |
---|
74 | selection reg state; |
---|
75 | |
---|
76 | initial state = A; |
---|
77 | |
---|
78 | assign sel = active ? state: X; |
---|
79 | |
---|
80 | always @(posedge clk) begin |
---|
81 | case(state) |
---|
82 | A: |
---|
83 | state = B; |
---|
84 | B: |
---|
85 | state = C; |
---|
86 | C: |
---|
87 | state = A; |
---|
88 | endcase |
---|
89 | end |
---|
90 | endmodule |
---|
91 | |
---|
92 | module client(clk, req, ack); |
---|
93 | input clk, ack; |
---|
94 | output req; |
---|
95 | |
---|
96 | reg req; |
---|
97 | client_state reg state; |
---|
98 | |
---|
99 | wire rand_choice; |
---|
100 | |
---|
101 | initial req = 0; |
---|
102 | initial state = NO_REQ; |
---|
103 | |
---|
104 | assign rand_choice = $ND(0,1); |
---|
105 | |
---|
106 | always @(posedge clk) begin |
---|
107 | case(state) |
---|
108 | NO_REQ: |
---|
109 | if (rand_choice) |
---|
110 | begin |
---|
111 | req = 1; |
---|
112 | state = REQ; |
---|
113 | end |
---|
114 | REQ: |
---|
115 | if (ack) |
---|
116 | state = HAVE_TOKEN; |
---|
117 | HAVE_TOKEN: |
---|
118 | if (rand_choice) |
---|
119 | begin |
---|
120 | req = 0; |
---|
121 | state = NO_REQ; |
---|
122 | end |
---|
123 | endcase |
---|
124 | end |
---|
125 | endmodule |
---|