COMSATS University Islamabad, Lahore Campus
Department of Computer Engineering
NAME FATIMA NASIR
MINAHIL MEHMOOD
REGISTRATION NUMBER FA20-BCE-035
FA20-BCE-068
COURSE DIGITAL SYSTEM DESIGN
ASSIGNMENT 4
SUBMITTED TO MA’AM WAJEEHA
Case Study: Dice Game
Introduction:
In this dice game, two six-sided dice are rolled, and their sum of outcome leads to different
outputs.
The player rolls two six-sided dice, and the sum of the dice determines the outcome of the game.
If the sum is 7 or 11, the player wins. If the sum is 2, 3, or 12, the player loses. If the sum is 4, 5,
6, 8, 9, or 10, the player gets to roll again, and the result is evaluated again on same rules.
Possible Outcomes:
• On the first roll of the two dice:
If the sum of the two dice is 7 or 11, the player wins.
If the sum is 2, 3, or 12, the player loses.
If the sum is any other number (4, 5, 6, 8, 9, or 10), that number becomes the
player's "point," and they need to roll the dice again.
• On next dice rolls:
If the sum of the two dice which are rolled again equals either 7 or 11 the player
wins.
If the sum is 2, 3, or 12 the player loses.
If the sum is any other number, the player keeps rolling dice until they either get
a winning or losing number.
SM Chart:
SM Chart Explanation:
The SM chart depicts the sequential flow of the dice game, it shows the transitions between
different states and outputs. The initial state, T0, represents the waiting phase where the game
waits for the player to roll the dice.
When the player rolls the dice, the game transitions to state S1 where the sum of the two dice is
calculated. The sum is then evaluated in state S2 to determine if it is a winning sum (7 or 11), a
losing sum (2, 3, or 12), or neither.
If the sum is winning, the game proceeds to state S3, where the player wins.
If the sum is losing, the game moves to state S4, indicating a loss.
In the case of neither winning nor losing, the game returns to state S1 for another roll.
States:
• T0: The initial state. The game waits for a roll request from the player.
• S1: The state where the dice are rolled, and the sum is calculated.
• S2: The state where the sum is checked to see if it is a winning or losing sum.
• S3: The state where the player wins the game.
• S4: The state where the player loses the game.
State transitions:
• T0 to S1: When the game receives a roll request from the player.
• S1 to T1: After the dice have been rolled and the sum has been calculated.
• T1 to S2: After the sum has been calculated.
• S2 to S3: If the sum is a winning sum.
• S2 to S4: If the sum is a losing sum.
• S2 to S1: If the sum is neither winning nor losing.
Finite State Machine:
The FSM, or finite state machine, is a more detailed representation of the game's logic and state
transitions.
It has four main states: T0, S1, S2, and either S3 or S4, depending on the outcome of the dice
roll.
State T0 is the starting point, where the game waits for the player to roll the dice. Once the player
rolls the dice, the game moves to state S1, where the sum of the dice is calculated. The sum is
then evaluated in state S2 to see if it is a winning sum (7 or 11) or a losing sum (2, 3, or 12). If
it's a winning sum, the game goes to state S3, where the player wins.
If it's a losing sum, the game goes to state S4, where the player loses. If the sum is neither
winning nor losing, the game goes back to state S1 for another roll. The FSM keeps going
through these states until the player either wins or loses the game.
VHDL Implementation:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity DiceGame is
port(Rb, Reset, CLK: in bit;
Sum: in integer range 2 to 12;
Roll, Win, Lose: out bit);
end DiceGame;
architecture DiceBehave of DiceGame is
signal State, Nextstate: integer range 0 to 5;
signal Point: integer range 2 to 12;
signal Sp: bit;
begin
process(Rb, Reset, Sum, State)
begin
Sp <= '0'; Roll <= '0'; Win <= '0'; Lose <= '0';
case State is
when 0 => if Rb = '1' then Nextstate <= 1; end if;
when 1 =>
if Rb = '1' then Roll <= '1';
elsif Sum = 7 or Sum = 11 then Nextstate <= 2;
elsif Sum = 2 or Sum = 3 or Sum = 12 then Nextstate <= 3;
else Sp <= '1'; Nextstate <= 4;
end if;
when 2 => Win <= '1';
if Reset = '1' then Nextstate <= 0;
end if;
when 3 => Lose <= '1';
if Reset = '1' then Nextstate <= 0; end if;
when 4 => if Rb = '1' then Nextstate <= 5; end if;
when 5 =>
if Rb = '1' then Roll <= '1';
elsif Sum = Point then Nextstate <= 2;
elsif Sum = 7 then Nextstate <= 3;
else Nextstate <= 4;
end if;
end case;
end process;
process(CLK)
begin
if CLK'event and CLK = '1' then
State <= Nextstate;
if Sp = '1' then Point <= Sum; end if;
end if;
end process;
end DiceBehave;
entity GameTest is
port(Rb, Reset: out bit;
Sum: out integer range 2 to 12;
CLK: inout bit;
Roll, Win, Lose: in bit);
end GameTest;
architecture dicetest of GameTest is
signal Tstate, Tnext: integer range 0 to 3;
signal Trig1: bit;
type arr is array(0 to 11) of integer;
constant Sumarray:arr := (7, 11, 2, 4, 7, 5, 6, 7, 6, 8, 9, 6);
begin
CLK <= not CLK after 20 ns;
process(Roll, Win, Lose, Tstate)
variable i: natural; -- i is initialized to 0
begin
case Tstate is
when 0 => Rb <= '1'; -- wait for Roll
Reset <= '0';
if i >= 12 then Tnext <= 3;
elsif Roll = '1' then
Sum <= Sumarray(i);
i := i + 1;
Tnext <= 1;
end if;
when 1 => Rb <= '0'; Tnext <= 2;
when 2 => Tnext <= 0;
Trig1 <= not Trig1; -- toggle Trig1
if (Win or Lose) = '1' then
Reset <= '1';
end if;
when 3 => null; -- Stop state
end case;
end process;
process(CLK)
begin
if CLK = '1' and CLK'event
then
Tstate <= Tnext;
end if;
end process;
end Dicetest;
entity tester is
end tester;
architecture test of tester is
component GameTest
port(Rb, Reset: out bit;
Sum: out integer range 2 to 12;
CLK: inout bit;
Roll, Win, Lose: in bit);
end component;
component DiceGame
port(Rb, Reset, CLK: in bit;
Sum: in integer range 2 to 12;
Roll, Win, Lose: out bit);
end component;
signal rb1, reset1, clk1, roll1, win1, lose1: bit;
signal sum1: integer range 2 to 12;
begin
Dice: Dicegame port map (rb1, reset1, clk1, sum1, roll1, win1, lose1);
Dicetest: GameTest port map (rb1, reset1, sum1, clk1, roll1, win1, lose1);
end test;
Outputs:
Case 1:
Player wins because sum of roll of dice = 7
Case 2:
Player loses because sum of roll of dice = 2
Another example of player losing because sum of roll = 10
Case 3: Player rolls the dice 2 times to get a winning result.
On the very first roll of dice the player gets sum of dice = 6, now we know that
when sum = 6, player gets to roll the dice again and on the second roll of dice
player gets sum = 11, and because of that player wins on the second roll.
Case 4: Player rolls the dice 2 times to get a losing result.
In this case, player rolls the dice for the first time and gets sum of 6, which allows
him to roll again, on the second roll player gets sum = 12, which is a losing sum
Case 5: Player neither wins nor loses, the player keeps rolling forever
In this case the player keeps rolling forever because he is never getting winning or
losing sum of dice. The following example shows that player rolls the dice 3 times
but still the win or lose signal stays at 0.
First roll sum = 4
Second roll sum = 5
Third roll sum = 6
The player will stay in this state unless he gets a sum that is either winning or losing.