source: vis_dev/vis-2.1/examples/ping_pong_new/ping_pong_new.v @ 11

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

Add vis

File size: 4.0 KB
Line 
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
14typedef enum {HIT, WAIT_GOING, WAIT_COMING} player_status;
15typedef enum {HIT, IDLE} action_type;
16typedef enum {TO_A, TO_B, OUT_OF_PLAY} ball_status;
17
18module ping_pong(clk); 
19input clk; 
20
21/* signal declarations */
22action_type wire action_A, action_B;
23player_status wire state_A, state_B; 
24ball_status wire state_ball_1, state_ball_2;
25
26/* the ping pong players */
27player player_A(clk, action_B, action_A, state_A);
28player player_B(clk, action_A, action_B, state_B);
29
30/* the balls */
31ball ball_1(clk, action_A, action_B, state_ball_1, TO_A);
32ball ball_2(clk, action_A, action_B, state_ball_2, TO_B);
33
34endmodule 
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 */
49module player(clk, opponent, out, state); 
50input clk; 
51input opponent; 
52output out;
53output state;
54
55action_type wire opponent, out;
56player_status reg state;
57player_status wire r_state;
58
59assign out = (state==HIT) ? HIT : IDLE;
60assign r_state = $ND(WAIT_COMING, HIT);
61
62initial state = HIT;
63
64always @(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;
87end
88endmodule
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 */
104module ball(clk, action_A, action_B, state, init); 
105input clk; 
106input action_A, action_B; 
107input init;
108output state;
109
110action_type wire action_A, action_B;
111ball_status reg state;
112ball_status wire init;
113
114initial state = init;
115
116always @(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;
141end
142endmodule
143
Note: See TracBrowser for help on using the repository browser.