Verilog Dice Game
Verilog Dice Game
Craps is a dice game in which players roll a pair of dice and bet on the outcome of the roll or a
series of rolls. The main idea behind this Verilog electronic version of craps is to have two
counters that are used to simulate the roll of the dice. Each one of these counters counts in the
sequence 1, 2, 3, 4, 5, 6 and then keeps repeating this pattern.
After the roll of the dice, the values in the two counters which ranges between 2 and 12 is
summed up. Using this sum and a set of rules for the game, a winner can be determined. In order
to win on the first roll, a player must obtain a sum of 7 or 11. Otherwise, if a sum of 2, 3, or 12 is
obtained, the player loses and all other sums become the “point”. If the sum equals the “point”
on the second or subsequent roll, then the player wins. Otherwise, the player loses if the sum
equals 7. The game is then continued in this fashion until the player wins or loses.
The inputs to the dice game come from two push buttons, a roll button and a reset button. The
reset button is used to initiate a new game and pushing the roll button after a reset mimics the
rolling of the dice. The rolling of the dice is synchronously triggered using a clock pulse
generated by the BASYS 2 board. The frequency of the clock provided on the BASYS 2 board
on which this game was tested had a frequency of 50 MHz. This frequency was scaled down
using a clock divider resulting in a period of 6.4 ms
During each roll, the face values obtained are multiplexed onto two 7-segment Light Emitting
Diode (LED) displays on the BASYS 2 board. In the event that a player loses after a roll, an “L”
is displayed. Otherwise, a “U” is displayed to indicate a win. If the player doesn’t win or lose
during the first or subsequent rolls, an “A” is displayed to indicate that the player can roll again.
If the game is reset, a “0” is displayed. In addition to displaying all these different states, the 7-
segment LEDS are multiplexed to display the face values of the dice during and after a roll.
Game Flow chart (image credit: Jim Plusquellic):
parameter S_idle = 0;
parameter S_rolling = 1;
parameter S_pause = 2;
parameter S_repeat = 3;
parameter S_lose = 4;
parameter S_win = 5;
initial begin
choice <= 0;
le <= 1;
end
//multiplexor
clock_divider divider(.cin(clk), .cout(clk_segments));
always @ (posedge clk_segments) begin
case (choice)
0: begin
choice <= 1;
digit <= 4'b0111;
segments <= worl;
end
1: begin
choice <= 2;
digit <= 4'b1101;
case (lsb)
1 : begin
segments <= 8'b10011111;
end
2 : begin
segments <= 8'b00100101;
end
3 : begin
segments <= 8'b00001101;
end
4 : begin
segments <= 8'b10011001;
end
5 : begin
segments <= 8'b01001001;
end
6 : begin
segments <= 8'b01000001;
end
default : begin
segments <= 8'b11111101;
end
endcase
end
2: begin
choice <= 0;
digit <= 4'b1110;
case (msb)
1 : begin
segments <= 8'b10011111;
end
2 : begin
segments <= 8'b00100101;
end
3 : begin
segments <= 8'b00001101;
end
4 : begin
segments <= 8'b10011001;
end
5 : begin
segments <= 8'b01001001;
end
6 : begin
segments <= 8'b01000001;
end
default : begin
segments <= 8'b11111101;
end
endcase
end
endcase
end //always
//end multiplexor
S_rolling: if (roll)
next_state <= S_rolling;
else
if (sum == 2 || sum == 3 || sum == 12)
next_state <= S_lose;
else
if (sum == 7 || sum == 11)
next_state <= S_win;
else next_state <= S_pause;
S_pause: if (roll)
next_state <= S_repeat;
else next_state <= S_pause;
S_repeat: if (roll)
next_state <= S_repeat;
else
if (match)
next_state <= S_win;
else
if (sum == 7)
next_state <= S_lose;
else next_state <= S_pause;
endmodule // latch
Clock divider:
module clock_divider ( cin, cout );
input cin;
output cout;
cout <= d;
end
endmodule