1 | /* |
---|
2 | * This code implements a game of ping pong (table tennis) between 2 players, with |
---|
3 | * a slight twist: each player puts a ball into play at the same time. There can |
---|
4 | * be an arbitrary, but finite, delay from the time one player hits a ball until |
---|
5 | * the time that the other player returns it. If both balls are in play, then |
---|
6 | * both players always hit each ball at the same instant. |
---|
7 | * |
---|
8 | * While both balls are in play, exactly one player may choose not to return his |
---|
9 | * ball. This leaves one ball in play. If just one ball is in play, the ball |
---|
10 | * should remain in play forever. |
---|
11 | * |
---|
12 | */ |
---|
13 | |
---|
14 | typedef enum {HIT, WAIT_GOING, WAIT_COMING} player_status; |
---|
15 | typedef enum {HIT, IDLE} action_type; |
---|
16 | typedef enum {TO_A, TO_B, OUT_OF_PLAY} ball_status; |
---|
17 | |
---|
18 | module ping_pong(clk); |
---|
19 | input clk; |
---|
20 | |
---|
21 | /* signal declarations */ |
---|
22 | action_type wire action_A, action_B; |
---|
23 | player_status wire state_A, state_B; |
---|
24 | ball_status wire state_ball_1, state_ball_2; |
---|
25 | |
---|
26 | /* the ping pong players */ |
---|
27 | player player_A(clk, action_B, action_A, state_A); |
---|
28 | player player_B(clk, action_A, action_B, state_B); |
---|
29 | |
---|
30 | /* the balls */ |
---|
31 | ball ball_1(clk, action_A, action_B, state_ball_1, TO_A); |
---|
32 | ball ball_2(clk, action_A, action_B, state_ball_2, TO_B); |
---|
33 | |
---|
34 | endmodule |
---|
35 | |
---|
36 | /* |
---|
37 | * Three state process. If the player is waiting while the ball is going away from |
---|
38 | * him (state WAIT_GOING) and the opponent hits the ball, then go to state WAIT_COMING. |
---|
39 | * The player can remain in state WAIT_COMING an arbitrary, but finite, amount of time. |
---|
40 | * The player non-deterministically moves from state WAIT_COMING to state HIT. From |
---|
41 | * state HIT, the player immediately hits the ball. Depending on the action of the |
---|
42 | * opponent, the player either moves to state WAIT_GOING or to state WAIT_COMING. |
---|
43 | * |
---|
44 | * Note that in state WAIT_GOING, there is only one ball still in play. Also, in state |
---|
45 | * WAIT_COMING, it's possible that there is a ball going away, as well as coming towards |
---|
46 | * the player. However, in state WAIT_GOING, there cannot be a ball coming towards |
---|
47 | * the player. |
---|
48 | */ |
---|
49 | module player(clk, opponent, out, state); |
---|
50 | input clk; |
---|
51 | input opponent; |
---|
52 | output out; |
---|
53 | output state; |
---|
54 | |
---|
55 | action_type wire opponent, out; |
---|
56 | player_status reg state; |
---|
57 | player_status wire r_state; |
---|
58 | |
---|
59 | assign out = (state==HIT) ? HIT : IDLE; |
---|
60 | assign r_state = $ND(WAIT_COMING, HIT); |
---|
61 | |
---|
62 | initial state = HIT; |
---|
63 | |
---|
64 | always @(posedge clk) begin |
---|
65 | case(state) |
---|
66 | HIT: |
---|
67 | begin |
---|
68 | if (opponent == IDLE) |
---|
69 | state = WAIT_GOING; |
---|
70 | else if (opponent == HIT) |
---|
71 | state = WAIT_COMING; |
---|
72 | end |
---|
73 | |
---|
74 | WAIT_GOING: |
---|
75 | begin |
---|
76 | if (opponent == HIT) |
---|
77 | state = WAIT_COMING; |
---|
78 | end |
---|
79 | |
---|
80 | WAIT_COMING: |
---|
81 | begin |
---|
82 | state = r_state; |
---|
83 | end |
---|
84 | |
---|
85 | default:; |
---|
86 | endcase; |
---|
87 | end |
---|
88 | endmodule |
---|
89 | |
---|
90 | |
---|
91 | /* |
---|
92 | * Three state process. The ball is either going towards player A |
---|
93 | * (state TO_A) or towards B. If it's ever the case that the ball |
---|
94 | * is going towards A, and A is IDLE and B HITS another ball, then |
---|
95 | * this ball goes OUT_OF_PLAY, since two balls are now going |
---|
96 | * towards A, and the assumption is that A cannot return both balls. |
---|
97 | * The symmetic case holds when in TO_B. |
---|
98 | * |
---|
99 | * Note that this module does not produce any outputs (except for its |
---|
100 | * state, which is read only by the property). Thus, if the |
---|
101 | * property being checked just deals directly with the players, then |
---|
102 | * the balls can be safely ignored. |
---|
103 | */ |
---|
104 | module ball(clk, action_A, action_B, state, init); |
---|
105 | input clk; |
---|
106 | input action_A, action_B; |
---|
107 | input init; |
---|
108 | output state; |
---|
109 | |
---|
110 | action_type wire action_A, action_B; |
---|
111 | ball_status reg state; |
---|
112 | ball_status wire init; |
---|
113 | |
---|
114 | initial state = init; |
---|
115 | |
---|
116 | always @(posedge clk) begin |
---|
117 | case(state) |
---|
118 | TO_A: |
---|
119 | begin |
---|
120 | if (action_A == HIT) |
---|
121 | state = TO_B; |
---|
122 | else if ((action_A == IDLE) && (action_B == HIT)) |
---|
123 | state = OUT_OF_PLAY; |
---|
124 | end |
---|
125 | |
---|
126 | TO_B: |
---|
127 | begin |
---|
128 | if (action_B == HIT) |
---|
129 | state = TO_A; |
---|
130 | else if ((action_B == IDLE) && (action_A == HIT)) |
---|
131 | state = OUT_OF_PLAY; |
---|
132 | end |
---|
133 | |
---|
134 | OUT_OF_PLAY: |
---|
135 | begin |
---|
136 | state = OUT_OF_PLAY; |
---|
137 | end |
---|
138 | |
---|
139 | default:; |
---|
140 | endcase; |
---|
141 | end |
---|
142 | endmodule |
---|
143 | |
---|