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 | if (active) |
---|
82 | case(state) |
---|
83 | A: |
---|
84 | state = B; |
---|
85 | B: |
---|
86 | state = C; |
---|
87 | C: |
---|
88 | state = A; |
---|
89 | endcase |
---|
90 | end |
---|
91 | endmodule |
---|
92 | |
---|
93 | module client(clk, req, ack); |
---|
94 | input clk, ack; |
---|
95 | output req; |
---|
96 | |
---|
97 | reg req; |
---|
98 | client_state reg state; |
---|
99 | |
---|
100 | wire rand_choice; |
---|
101 | |
---|
102 | initial req = 0; |
---|
103 | initial state = NO_REQ; |
---|
104 | |
---|
105 | assign rand_choice = $ND(0,1); |
---|
106 | |
---|
107 | always @(posedge clk) begin |
---|
108 | case(state) |
---|
109 | NO_REQ: |
---|
110 | if (rand_choice) |
---|
111 | begin |
---|
112 | req = 1; |
---|
113 | state = REQ; |
---|
114 | end |
---|
115 | REQ: |
---|
116 | if (ack) |
---|
117 | state = HAVE_TOKEN; |
---|
118 | HAVE_TOKEN: |
---|
119 | if (rand_choice) |
---|
120 | begin |
---|
121 | req = 0; |
---|
122 | state = NO_REQ; |
---|
123 | end |
---|
124 | endcase |
---|
125 | end |
---|
126 | endmodule |
---|