1 | /* Dining philosophers of E. W. Dijkstra |
---|
2 | based on S/R implementation by R. Kurshan |
---|
3 | Ramin Hojati, May 1993 |
---|
4 | |
---|
5 | */ |
---|
6 | typedef enum {THINKING, HUNGRY, EATING, READING} t_state; |
---|
7 | |
---|
8 | /************************************************************************/ |
---|
9 | module diners(clk); |
---|
10 | input clk; |
---|
11 | |
---|
12 | t_state wire s0, s1, s2; |
---|
13 | |
---|
14 | philosopher ph0(clk, s0, s1, s2, EATING); |
---|
15 | philosopher ph1(clk, s1, s2, s0, READING); |
---|
16 | philosopher ph2(clk, s2, s0, s1, HUNGRY); |
---|
17 | |
---|
18 | starvation str(clk, s0); |
---|
19 | |
---|
20 | endmodule |
---|
21 | |
---|
22 | /************************************************************************/ |
---|
23 | module philosopher(clk, out, left, right, init); |
---|
24 | input clk; |
---|
25 | input left, right, init; |
---|
26 | output out; |
---|
27 | t_state wire left, right, init, out; |
---|
28 | t_state reg state; |
---|
29 | t_state wire r0_state,r1_state; |
---|
30 | |
---|
31 | |
---|
32 | initial state = init; |
---|
33 | |
---|
34 | assign r0_state = $ND(THINKING,HUNGRY); |
---|
35 | assign r1_state = $ND(THINKING,EATING); |
---|
36 | assign out = state; |
---|
37 | |
---|
38 | always @(posedge clk) begin |
---|
39 | case(state) |
---|
40 | READING: |
---|
41 | if (left == THINKING) state = THINKING; |
---|
42 | |
---|
43 | THINKING: |
---|
44 | begin |
---|
45 | if ( right == READING ) state = READING; |
---|
46 | else state = r0_state; |
---|
47 | end |
---|
48 | |
---|
49 | EATING: |
---|
50 | state = r1_state; |
---|
51 | |
---|
52 | HUNGRY: |
---|
53 | if ( left != EATING && right != HUNGRY && right != EATING) |
---|
54 | state = EATING; |
---|
55 | endcase |
---|
56 | end |
---|
57 | endmodule |
---|
58 | |
---|
59 | /************************************************************************/ |
---|
60 | module starvation( clk, starv ); |
---|
61 | input clk; |
---|
62 | input starv; |
---|
63 | t_state wire starv; |
---|
64 | reg state; |
---|
65 | |
---|
66 | initial state = 0; |
---|
67 | |
---|
68 | always @(posedge clk) begin |
---|
69 | case(state) |
---|
70 | 0: if ( starv == HUNGRY ) state = 1; |
---|
71 | |
---|
72 | 1: if ( starv == THINKING ) state = 0; |
---|
73 | |
---|
74 | endcase |
---|
75 | end |
---|
76 | endmodule |
---|