Lab10 Dice Game
Lab10 Dice Game
Overview
We have looked at sequential building blocks as well as begun to look at synchronous sequential circuit
design. This lab will allow us to design a game in hardware. Dice games are very common and easy to
implement in hardware. This dice game will allow the user to roll two die and then compare the sum of
the values with specific values for a win or lose situation. Finite state machine along with datapath and
control circuits will be emphasized.
Before beginning this module, you should… After completing this module, you should…
Background
Craps – a dice game
Craps is a dice game in which players place wagers on the outcome of the roll, or a series of rolls, of a pair of
dice. This lab simulates the dice game. The basic rule for craps is like following: if, on the first roll, you make a
7 or 11, you've rolled a "natural" and you win. If you roll a 2, 3 or 12 on your first throw, that is called "craps"
and you lose. If, on the first roll, you shoot a 4, 5, 6, 8, 9 or 10, that is you are established "box point." The
object then is to keep rolling the dice until you make that number again. You lose, however, if you roll a
seven before making your box point.
Procedure
The overview of the game is on the following flow chart.
So far we have looked at combinational circuits and sequential circuits. In this final lab, we are going to put all
we learn together. Last lab we made a dice roller, the lab before last lab was 4-bit register and card checker
in Lab4 where 7 or 11 produces WIN and 2, 3, or 12 LOSE. Overall system can be drawn like following diagram
Because of the time limitation for the lab, we only make ‘dice roller’ and ‘controller’. The other three
modules (Register, Comparator, and Test logic) will be assigned as prelab. However, we already made
register and the circuit similar to Test logic. See the details on circuit design.
Example: Controller
module controller(CLK, RESET, BTN, D7, D711, D2312, EQUAL, ROLL, WIN,
LOSE, SAVE, NEXT_STATE);
input CLK, RESET, BTN, D7, D711, D2312, EQUAL;
output ROLL, WIN, LOSE, SAVE, [2:0] NEXT_STATE;
reg WIN,LOSE,SAVE,ROLL;
reg [2:0] STATE, NEXT_STATE;
initial begin
STATE = S0;
NEXT_STATE = S0;
end
// Defining outputs and next state depending on current state and control signal
always @(STATE or ROLL or D7 or D711 or D2312 or EQUAL)
case(STATE) // Needs to set all state(S0~S5) using case statement
S0: begin
// Defining output
WIN = 0;
LOSE = 0;
// Defining next state using if~else statement
if(ROLL != 1) begin
NEXT_STATE = S0;
end
else begin
NEXT_STATE = S1;
end
end
S1: begin
if(ROLL == 1) begin
NEXT_STATE = S1;
end
else begin
//Defining output
SAVE = 1;
// Defining next state using if~else statement
if (D711 == 1) begin
NEXT_STATE = S3;
end
else if (D2312 == 1) begin
NEXT_STATE = S2;
end
else begin
NEXT_STATE = S4;
end
end
end
S2: begin ...
register_4bit: we already made this module in lab 8. If don’t have it, implement it according to Lab8
manual. Names are not matter for inputs and outputs, but the module must have same function of
inputs and outputs.
o input: CLK, CLR, D_IN[3:0], LOAD(or LD)
o output: D_OUT[3:0]
o Verify it with test bench waveform (you can just use the code in Lab 8)
full_adder_3bit: If you already have it, just check input and output form and generate testbench
waveform according to Apendix
o input: A [2:0], B[2:0]
o output: S [3:0]
o Verify this with testbench waveform according to Apendix
test_logic: according to lab4, write this function. You can ‘copy and paste’ of the code from the lab.
However, make sure the form of input and output. You need to write a function for D7 which is very
similar to D711. See the win_check and lose_check module in Lab4.
o input: DICE_SUM [3:0]
o output: D7, D711, D2312
o Verify this with testbench waveform by setting 7, 11, 2, 3, and 12 on DICE_SUM
dice_roller: This module calls three sub modules; clock divider, two counters from last lab, and
full_adder_3bit. The skeleton code will be given.
o input: CLK, CLR, ROLL
o output: DICE_SUM[3:0]
o test on the BaSYS board (test module will be given)
o Other cases include D2312 after ROLL signal, EQUAL signal after ROLL signal .
comparator_4bit: This module compares two hex values and generate true (1) or false (0) signal on
the output, EQUAL. There are many ways to implement this. One of them is first to use ‘xnor’
operator on two signals, and ‘and’ operation on element of the results. (Because ‘xnor’ operator
performs ‘xor’ operation followed by ‘not’ operation. Here ‘xor’ generates 1 if two signals are the
same, otherwise 0)
o input: A[3:0], B[3:0]
o output: EQUAL
o Verify it by setting A and B are equal or different.
endmodule
// you have to put three instantiation of 1bit adder. I put one and write other two.
full_adder_1bit A1 ( a[0], b[0], 0, c[0],s[0] );
assign s[3] = c[2]; // this will put the last carry out to the most significant bit of the results
endmodule