This instructable will show the process and modules required to create our Binary to Decimal Matching game. Within 60 seconds, users will translate and input as many randomly generated decimal numbers on the seven segment display into binary by toggling the switches and submitting with a guess button. Once completed, a final score will be displayed and then reset to play again.
Understanding binary and quick reflexes are beneficial to do well, but the reset button is provided in case one wants to instantly try again.
Step 1: Setting Up the Clock Divider
The backbone of this entire project comes from the correct synchronization of all the parts in this gizmo. Our Finite State Machine uses the internal clock, but the seven segment display and timer must use an altered version of the clock.
This "new clock" comes from dividing the internal clock by a desired period to attain a certain frequency needed for each specific component. This has been done in previous labs and from experience, we know the timer has the "one's" digit set to 0.1 Hz, and the "tens" digit being the 1 Hz
Inputs: ClkIn, divisor(32 bit)
Step 2: Creating a Finite-State Machine (FSM)
In our Finite-State Machine, we decided that five states (Start, Display, Check, Score and End) would be necessary with five inputs (start, reset, guess, equal, timeout). The only output in our State Machine is a 3 bit number that represents what state the user is in (000, 001, 011, 101, 100) with respect to the states below.
Remember that a Finite State Machine does not actually preform the functions below, instead it just tells what state the program is on and what . What actually happens is determined by the top module explained below.
Start State (000)
The Start State is where the user will begin until the start input is high, this is also the state that will be reached whenever the reset button is pressed.
Game State (001)
The Game State is the beginning of the game, where the random number is generated and the user toggles the switches to create an input. Once the guess button is pressed, the game is moved into the Check State.
Check State (011)
This state is where the comparator is being used, which will compare the values of the user's input and the randomly generated number. If the submission is correct, the equal value is high and the FSM goes to the Score State; however, if the submission is incorrect, the FSM reverts back to the Display State until the submission is correct.
This Check State happens relatively quickly compared to the others, as it is only happening as long as the check button is pressed
Score State (101)
Since the equal value is high, the submission was correct. In this state, the score value will increase by one and a new number will be generated for the user to input. This new number brings us back to the Start State where the user will once again toggle the switches.
End State (100)
Once the timer of 60 seconds is up, the timeout input will be high and the user reaches the End State where the final score is displayed. The reset input will then be pressed and the FSM starts over at the Start State again.
Inputs: Clk, rst, start, guess, equal, timeout
Output: state (3 bit)
Step 3: Mapping the Seven Segment Display
The Seven Segment Display is a key part of the entire project as the first two digits on the screen are used as the output of the random number generator, while the last two digits are the timer. Although we have implemented an example of this in the last lab in terms of having digits on the screen, it was displayed in hexadecimal. To fix this issue, we used a converter and clock divider that is further explained below.
The display shows all 0's until the FSM enters the game state; however, in the end state, the display should just show the user's score.
Since we are using all four digits of the seven segment display, we need to cycle through each anode quick enough at 300 Hz in order to be perceived as always lit.
Inputs: Clk, sevensegment
Outputs: cathodes(7 bit), anodes(4 bit)
Step 4: Creating the Comparator
This submodule is used in the Check State in terms of how it is comparing the 7 bit binary inputted guess versus the actual decimal value.
We had an if statement that evaluated both inputs and two outputs depending on whether or not the equal value was high or low. As important as this module is, it is by far one of the simpler programs to design in this project.
Inputs: switches(8 bit), number(8 bit)
Step 5: Setting Up a Timer
Our timer is essentially two different counters that are increasing at different rates. One counter in the "one's" value, (the first seven segment display) and one counter for the "ten's" value (second digit on seven segment display). Each digit is based off the rising edge of the clock, and once the counter reaches 60 seconds, time_out will be high and the game will end and return to the start state.
Inputs: Clk, state(3 bit), start
Outputs: Current(8 bit), timeout
Step 6: Designing the Pseudo Random Number Generator
Another alternative method for a number generator specifically for this case is to have a repeating counter from 0-99 (in binary) that outputs the counted number when the input is high, as this would take out the need for using an LFSR.
The number changes every rising edge of the internal clock (10 nano-seconds) and cycles through all 100 numbers in one microsecond. Whenever the user wants a new number from the number generator, it outputs the number it was on,
Although this process is not entirely random, the probability of finding related outputs from this process is low enough to be pseudo-random.
Inputs: Clk, changenum, equal
Outputs: number(8 bit)
Step 7: Creating a Converter
A necessary component is the Converter, which we used to display decimal numbers on the seven segment display instead of the original hexadecimal. Although both numbers are based off of a 7 bit Binary number, we created an entire module designated to converting hexadecimal to decimal.
For example, if our final output for the score was 0010001 (seventeen), the seven segment display would show the hexadecimal value of 11, instead of the decimal number of 17.
Input: Numin(8 bit)
Output: Numout(8 bit)
Step 8: Putting Everything Together in the Game Module
For our components, we used the necessary switches 0-6 for the user to toggle, with the three buttons to act as the user inputs for start, reset, and guess. The seven segment display and clock components are also components we have done from previous labs but had to alter in order to fit this project.
We split this project into the six modules shown above to break down the entire gizmo into many working parts, however, the way they are connected is quite intricate and is shown from the black box picture attached.
While the game is happening, 7 LED's are lit to notify the user which switches to use, and when the game ends, we also programmed LED's to flash
Inputs: switches(8 bit), Clk, reset, start, guess
Outputs: cathodes(7 bit), anodes(4 bit), LEDs(7 bit)
Step 9: Extra Problems Encountered
Although only seven switches are used in this game, the code has it set as an 8 bit number. We did this in order to have a more convenient comparator that would compare these 8 bits to the 8 bit number we generated from the random number generator.
The score also gave us a bit of trouble at first because we set it to increase one point when the the FSM was in the score state; however what happened instead was that the score kept increasing as long as the state was on, giving us a unreasonably high score we could not deal with. We fixed this by adding a pulse signal that was synchronized with the rising edge of the clock, as seen in the code in step 8.
Finally, the timer took a lot of time to debug as it would distort our seven segment display when it was counting down, so we had to change it from counting down from 60 to counting up from 0.
Step 10: Source Files and Constraints
If you would rather pull from our source files instead of creating your own, here they are. Also, the constraint file is included.