source: vis_dev/vis-2.1/examples/arbiter/arbiter_le.v @ 12

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

Add vis

File size: 2.6 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
23observer observer(clk, reqA, ackA);
24
25endmodule
26
27module controller(clk, req, ack, sel, pass_token, id);
28input clk, req, sel, id;
29output ack, pass_token;
30
31selection wire sel, id;
32reg ack, pass_token;
33controller_state reg state;
34
35initial state = IDLE;
36initial ack = 0;
37initial pass_token = 1;
38
39wire is_selected;
40assign is_selected = (sel == id);
41
42always @(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
68end
69endmodule
70
71module arbiter(clk, sel, active);
72input clk, active;
73output sel;
74
75selection wire sel;
76selection reg state;
77
78initial state = A;
79
80assign sel = active ? state: X;
81
82always @(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
92end
93endmodule
94
95module client(clk, req, ack);
96input clk, ack;
97output req;
98
99reg req;
100client_state reg state;
101
102wire rand_choice;
103
104initial req = 0;
105initial state = NO_REQ;
106
107assign rand_choice = $ND(0,1);
108
109always @(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
127end
128endmodule
129
130typedef enum {IDLE, BAD, GOOD} observer_state;
131
132module observer(clk, req, ack);
133input clk, req, ack;
134
135observer_state reg state;
136initial state = IDLE;
137
138wire rand_choice;
139assign rand_choice = $ND(0,1);
140
141always @(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
150end
151endmodule
Note: See TracBrowser for help on using the repository browser.