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 | observer observer(clk, reqA, ackA); |
---|
24 | |
---|
25 | endmodule |
---|
26 | |
---|
27 | module controller(clk, req, ack, sel, pass_token, id); |
---|
28 | input clk, req, sel, id; |
---|
29 | output ack, pass_token; |
---|
30 | |
---|
31 | selection wire sel, id; |
---|
32 | reg ack, pass_token; |
---|
33 | controller_state reg state; |
---|
34 | |
---|
35 | initial state = IDLE; |
---|
36 | initial ack = 0; |
---|
37 | initial pass_token = 1; |
---|
38 | |
---|
39 | wire is_selected; |
---|
40 | assign is_selected = (sel == id); |
---|
41 | |
---|
42 | always @(posedge clk) begin |
---|
43 | case(state) |
---|
44 | IDLE: |
---|
45 | if (is_selected) |
---|
46 | if (req) |
---|
47 | begin |
---|
48 | state = READY; |
---|
49 | pass_token = 0; /* dropping off this line causes a safety bug */ |
---|
50 | end |
---|
51 | else |
---|
52 | pass_token = 1; |
---|
53 | else |
---|
54 | pass_token = 0; |
---|
55 | READY: |
---|
56 | begin |
---|
57 | state = BUSY; |
---|
58 | ack = 1; |
---|
59 | end |
---|
60 | BUSY: |
---|
61 | if (!req) |
---|
62 | begin |
---|
63 | state = IDLE; |
---|
64 | ack = 0; |
---|
65 | pass_token = 1; |
---|
66 | end |
---|
67 | endcase |
---|
68 | end |
---|
69 | endmodule |
---|
70 | |
---|
71 | module arbiter(clk, sel, active); |
---|
72 | input clk, active; |
---|
73 | output sel; |
---|
74 | |
---|
75 | selection wire sel; |
---|
76 | selection reg state; |
---|
77 | |
---|
78 | initial state = A; |
---|
79 | |
---|
80 | assign sel = active ? state: X; |
---|
81 | |
---|
82 | always @(posedge clk) begin |
---|
83 | if (active) |
---|
84 | case(state) |
---|
85 | A: |
---|
86 | state = B; |
---|
87 | B: |
---|
88 | state = C; |
---|
89 | C: |
---|
90 | state = A; |
---|
91 | endcase |
---|
92 | end |
---|
93 | endmodule |
---|
94 | |
---|
95 | module client(clk, req, ack); |
---|
96 | input clk, ack; |
---|
97 | output req; |
---|
98 | |
---|
99 | reg req; |
---|
100 | client_state reg state; |
---|
101 | |
---|
102 | wire rand_choice; |
---|
103 | |
---|
104 | initial req = 0; |
---|
105 | initial state = NO_REQ; |
---|
106 | |
---|
107 | assign rand_choice = $ND(0,1); |
---|
108 | |
---|
109 | always @(posedge clk) begin |
---|
110 | case(state) |
---|
111 | NO_REQ: |
---|
112 | if (rand_choice) |
---|
113 | begin |
---|
114 | req = 1; |
---|
115 | state = REQ; |
---|
116 | end |
---|
117 | REQ: |
---|
118 | if (ack) |
---|
119 | state = HAVE_TOKEN; |
---|
120 | HAVE_TOKEN: |
---|
121 | if (rand_choice) |
---|
122 | begin |
---|
123 | req = 0; |
---|
124 | state = NO_REQ; |
---|
125 | end |
---|
126 | endcase |
---|
127 | end |
---|
128 | endmodule |
---|
129 | |
---|
130 | typedef enum {IDLE, BAD, GOOD} observer_state; |
---|
131 | |
---|
132 | module observer(clk, req, ack); |
---|
133 | input clk, req, ack; |
---|
134 | |
---|
135 | observer_state reg state; |
---|
136 | initial state = IDLE; |
---|
137 | |
---|
138 | wire rand_choice; |
---|
139 | assign rand_choice = $ND(0,1); |
---|
140 | |
---|
141 | always @(posedge clk) begin |
---|
142 | case(state) |
---|
143 | IDLE: |
---|
144 | if (req && rand_choice) |
---|
145 | state = BAD; |
---|
146 | BAD: |
---|
147 | if (ack) |
---|
148 | state = GOOD; |
---|
149 | endcase |
---|
150 | end |
---|
151 | endmodule |
---|