Dice Game Chapter 22 The dice game in

  • Slides: 12
Download presentation
Dice Game (Chapter 22) • The dice game in Chapter 22 is a good

Dice Game (Chapter 22) • The dice game in Chapter 22 is a good example of a Finite State Machine controlling a Datapath. – The combined FSM/Datapath implement a Dice Game. • Dice Game Rules: – After 1 st roll of dice, player wins if roll if 7 or 11. Player loses if roll is 2, 3 or 12. Anything else is saved as the players “POINT”. – On subsequent rolls, player loses if roll is a 7. Player wins if the dice roll is equal to their “POINT”. You must keep rolling until you win or lose. BR 1/99 1

What do we need: A Dice Roll • Some way to simulate a dice

What do we need: A Dice Roll • Some way to simulate a dice roll. Dice have values of 1, 2, 3, 4, 5, 6. • We could generate a “random” number between 1 -6. – Could use a psuedo random sequence generator in the form of a shift register for this. – Can be difficult to test if using psuedo random sequences • An easier way is to use a counter whose count sequence is 1, 2, 3, 4, 5, 6, 1, 2, etc. – Even though count sequence is NON-RANDOM, a high speed clock and a user pushing buttons to stop/start the roll will make the dice roll look random!!! BR 1/99 2

S 0 Q=1 1 -6 Counter En=0 En=1 S 1 Need two counters, one

S 0 Q=1 1 -6 Counter En=0 En=1 S 1 Need two counters, one for each dice. Q=2 En=0 En=1 S 2 Q=3 Need an ADDER to sum the two 3 -bit dice outputs to produce a four bit sum that we can test! En=0 En=1 S 3 Q=4 En=0 S 4 En=1 Q=5 En=0 En=1 S 5 Q=6 En=0 En=1 BR 1/99 3

“dpatha” Details Cntr EN Roll (from control) Clk Reset C R Roll Logic 3

“dpatha” Details Cntr EN Roll (from control) Clk Reset C R Roll Logic 3 Clk Reset Q Counters count 1, 2, 3, 4, 5, 6, 1, 2, etc Cntb 3 Ena Cntr EN C R Cnta A d d e r dicesum 4 Q 3 Ena = 1 when Cntb = 6 and Roll = 1 BR 1/99 4

What else do we need? ? ? • We need to save the “POINT”

What else do we need? ? ? • We need to save the “POINT” value from the first roll somewhere. – Use a Register! • We need to compare our POINT value and the current dice roll to several values – Compare Current dice roll to POINT value: Use a comparator – Compare Current dice roll to values of 7, 11, 2, 3, 12: Use comparator logic • Need inputs to start the dice roll, stop the dice roll. BR 1/99 5

“dpathb” Details Dicesum (from dpatha) sp (from control) Test Logic Register DIN 4 D

“dpathb” Details Dicesum (from dpatha) sp (from control) Test Logic Register DIN 4 D 11 (1 if sum=11) D 2312 (1 if sum=2, 3 or 12) LD Point Clk Reset D 7 (1 if sum=7) C R Compare Eq (1 if point = sum) 4 “sp” (save point) loads value of dicesum into register. BR 1/99 6

Cnta Cntb Dice Game Chap 22 control. vhd Rb 1 -to-6 Cntr Roll 1

Cnta Cntb Dice Game Chap 22 control. vhd Rb 1 -to-6 Cntr Roll 1 -to-6 Cntr Adder Dpatha. vhd dicesum D 7 Dpathb. vhd Point Register Test Logic Comparator D 11 D 2312 eq C o n t r o l Ra win lose sp BR 1/99 7

Roll Dice Y Sum=7 or 11? N N Y Sum=2, 3 or 12? Store

Roll Dice Y Sum=7 or 11? N N Y Sum=2, 3 or 12? Store Sum in Point Register Dice Game Flow Chart Roll Dice Y Sum= Point? N Sum= 7? N Y Win Y Reset? Lose N N BR 1/99 Reset? Y 8

S 0 Dice Game ASM Chart 0 Roll Rb? 0 1 S 1 Ra?

S 0 Dice Game ASM Chart 0 Roll Rb? 0 1 S 1 Ra? 1 1 D 7 or D 11? 0 D 2312? 0 Sp 1 S 4 S 2 0 S 3 Rb? Lose 1 S 5 Roll Win 0 1 Ra? 1 Eq? BR 1/99 0 0 D 7? 1 9

Dice Game Implementation • Dice game implemented in three 22 V 10 PLDs –

Dice Game Implementation • Dice game implemented in three 22 V 10 PLDs – control. vhd (Finite State Machine) – dpatha. vhd (two 1 -6 counters, adder) – dpathb. vhd (point register, comparator, test logic) • The Control FSM is implemented using one-hot encoding. – Outputs q 0, q 1, q 5 are states S 0, S 1, S 5. Output “win” is state S 2, output “lose” is state S 3. Outputs q 0, q 1, q 5 are available just for debugging purposes. BR 1/99 10

Finite State Machine Changes Asynchronous Reset now use to exit states S 2, S

Finite State Machine Changes Asynchronous Reset now use to exit states S 2, S 3. Resets back to State S 0. S 2 Win S 3 Lose BR 1/99 11

Finite State Machine Changes (cont). Because we don’t have debounced switches, added extra “roll”

Finite State Machine Changes (cont). Because we don’t have debounced switches, added extra “roll” input called “Ra”. Changed States S 1, S 5 to use Ra, not Rb. S 1 0 Ra Roll 1 S 5 0 Ra Roll 1 BR 1/99 This means that to roll the dice, you use flip switch Rb up, then down. This starts the dice rolling. To stop the dice, Flip Ra switch up then down. 12