source: vis_dev/vis-2.1/examples/arbiter/arbiter_bug.v @ 16

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

Add vis

File size: 2.2 KB
Line 
1typedef enum {A, B, C, X} selection;
2typedef enum {IDLE, READY, BUSY} controller_state;
3typedef enum {NO_REQ, REQ, HAVE_TOKEN} client_state;
4
5module main(clk);
6input clk;
7output ackA, ackB, ackC;
8
9selection wire sel;
10wire active;
11
12assign active = pass_tokenA || pass_tokenB || pass_tokenC;
13
14controller controllerA(clk, reqA, ackA, sel, pass_tokenA, A);
15controller controllerB(clk, reqB, ackB, sel, pass_tokenB, B);
16controller controllerC(clk, reqC, ackC, sel, pass_tokenC, C);
17arbiter arbiter(clk, sel, active);
18
19client clientA(clk, reqA, ackA);
20client clientB(clk, reqB, ackB);
21client clientC(clk, reqC, ackC);
22
23endmodule
24
25module controller(clk, req, ack, sel, pass_token, id);
26input clk, req, sel, id;
27output ack, pass_token;
28
29selection wire sel, id;
30reg ack, pass_token;
31controller_state reg state;
32
33initial state = IDLE;
34initial ack = 0;
35initial pass_token = 1;
36
37wire is_selected;
38assign is_selected = (sel == id);
39
40always @(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
66end
67endmodule
68
69module arbiter(clk, sel, active);
70input clk, active;
71output sel;
72
73selection wire sel;
74selection reg state;
75
76initial state = A;
77
78assign sel = active ? state: X;
79
80always @(posedge clk) begin
81  case(state) 
82    A:
83      state = B;
84    B:
85      state = C;
86    C:
87      state = A;
88  endcase
89end
90endmodule
91
92module client(clk, req, ack);
93input clk, ack;
94output req;
95
96reg req;
97client_state reg state;
98
99wire rand_choice;
100
101initial req = 0;
102initial state = NO_REQ;
103
104assign rand_choice = $ND(0,1);
105
106always @(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
124end
125endmodule
Note: See TracBrowser for help on using the repository browser.