Group 19
JOEL ANG | MICHAEL LIM | SNG HAN JIE
TABLE OF CONTENTS
1
Introduction ....................................................................................................................... 2
1.1
2.2
2.3
Background ............................................................................................................... 2
3.1.1
Components ...................................................................................................... 4
3.1.2
Instructions .......................................................................................................... 5
3.2
Rules ............................................................................................................................ 6
3.3
3.4
How to XOR................................................................................................................ 6
4.2
4.3
Summary .......................................................................................................................... 11
References ...................................................................................................................... 12
Appendix ......................................................................................................................... 13
8.1
8.2
8.2
8.3
1|Page
INTRODUCTION
1.1
BACKGROUND
For this 1D project, we were tasked to design and build a prototype of an electronic
game using an 8-bit Arithmetic Logic Unit (ALU) which we had to implement
ourselves on a Mojo V3 FPGA using Lucid HDL. The game has to be educational,
logic-based, and is able to demonstrate the functionality of the ALU.
GAME INFORMATION
2.1
GAME DESCRIPTION
2.2
GAME DESIGN
The game makes use of Lucid HDL to implement ROM, a Finite State Machine, D flipflops (Registers) and the self-designed ALU, which are components in the Mojo-V3
FPGA. The D flip-flops are used as registers to store important values for each state of
the game. Each of these values are all displayed on 7-segment LEDs. The ALU is
utilized as the player performs the different mathematical operations on the given
number options. Buttons are used by the player as command inputs for selecting
which number option they wish to use and also the operations to perform on them.
The game is split into 3 levels:
Level
Easy
Medium
Hard
Options
4 single-digit numbers
5 single-digit numbers
5 single-digit numbers
Operators
3 (ADD, SUB, MUL)
3 (ADD, SUB, MUL)
4 (ADD, SUB, MUL, XOR)
In each level, there are three stages of similar difficulty but each have different sets
of options. These problems are all hard-coded into the game.
2|Page
2.3
TEST SCENARIOS
The game was tested by pushing each button in each stage of the game and
ensuring that only the buttons that were meant to work were working while the rest
had no effect on the game.
The initial start phase of the game was tested by ensuring that the user could only
press the start/restart button and then all the 7-segment LEDs would light up and
display something.
Pressing the 1, 2, or 3, buttons selects the level of the game while the rest of
the buttons are disabled at that point in time.
Trying out the different combinations of solutions to achieve the target goal and
actually achieving it resulted in a PASS while obtaining a value different from the
target result gave a FAIL.
Pressing the start/restart button during any level in the game would reset the level.
The operator buttons would not work if there was no input number selected. After
completing each level, pressing the start/restart button would either restart or move
on to the next stage.
By ensuring all these worked, the game can be played without issues.
3|Page
USER MANUAL
3.1
HOW TO PLAY?
3.1.1 COMPONENTS
DISPLAYS
GOAL (4 X 7-segment BLUE LEDs) displays the final result that players must
achieve at the end of the round.
OPTIONS (5 X 7-segment BLUE LEDs) displays the given number options
(single-digits) that players can choose from. When each number is used up,
the display is blank.
SELECTED (S) (1 X 7-segment RED LED) displays selected number that player
chooses from set of numbers in OPTIONS.
CURRENT RESULT (C) (4 X 7-segment RED LEDs) displays current result of all
operations players have performed on selected numbers. Starts at 0.
BUTTONS
START/RESTART (single button) used to start the game, restart the current
stage the user is in and to move on to the next stage.
OPERATORS (5 X buttons) +, S-C, C-S, X, XOR. Used to perform the different
mathematical operations. Results will show up on the CURRENT RESULT display.
NUMBER SELECTORS (5 X buttons) 1, 2, 3, 4, 5. Used to select the different
digits to use from the OPTIONS display and puts it into the SELECTED display.
4|Page
3.1.2 INSTRUCTIONS
1. Press Start/Reset button to begin.
2. The OPTIONS LED will display LSEL, which stands for Level Select. Select your
difficulty by pressing Button 1 for Easy level, Button 2 for Medium level, and
Button 3 for Hard level.
3. PLAY! Press number selector buttons (1-5) to input numbers into SELECTED.
Each button corresponds to the number displayed in the OPTIONS LED directly
above it.
4. Press operator buttons to perform an operation for the numbers in SELECTED
and CURRENT RESULT. The result will replace the CURRENT RESULT value.
For a start, you should enter the first number you want to use into SELECTED and press
the + button to add SELECTED into CURRENT RESULT. Then, select the next number
that you want to enter into SELECTED and then the next operator to perform
operations on both the SELECTED and CURRENT RESULT values.
Here is an example to help you:
OPTIONS: 1, 2, 3, 4 | GOAL = 10 | Plan of approach: 1+2+3+4 = 10.
1.
2.
3.
4.
5.
6.
7.
8.
9.
*NOTE 1* Your CURRENT RESULT will be automatically compared to GOAL once you
have used up all the numbers from OPTIONS.
*NOTE 2* At any point in time, you can press the START/RESTART button to reset your
current stage. (Dont worry it will not restart the entire level)
5. If your CURRENT RESULT value is equal to GOAL you will see PASS displayed on
OPTIONS. Now press the start/reset button to start the next stage of the
current level. If it is not equal, you will see FAIL displayed instead. Press the
start/reset button to redo the stage.
6. After completing 3 stages: Congrats! Press the start/reset button to go to
Level Select and you can select a higher difficulty.
7. Compete with your friends to see who can solve the puzzles fastest and HAVE
FUN!
5|Page
3.2
RULES
1. You must enter a value into SELECTED before any operations can be
performed.
2. You have to use ALL the OPTIONS numbers before the game will check if
CURRENT RESULT is equal to GOAL. Even if you obtain the GOAL value without
using all the OPTIONS numbers, you will not pass the stage.
3. There are no negative numbers allowed. You are only able to subtract a
smaller number from a bigger number (Hence, the presence of two subtract
functions).
4. You are not allowed to multiply by 0 or XOR 0. None of the answers will require
you to do so as well.
5. There may be multiple paths to take to attain the GOAL value. It does not
matter which path you take, as long as your CURRENT RESULT value matches
GOAL, you will be considered correct.
3.3
3.4
EXPLANATION OF OPERATIONS
+ (S+C) adds the current values in SELECTED to CURRENT RESULT
S-C subtracts the current values in CURRENT RESULT from SELECTED
C-S subtracts the current values in SELECTED from CURRENT RESULT
X (S*C) multiplies the current values in SELECTED by CURRENT RESULT
XOR (S XOR C) converts the current values in SELECTED and CURRENT RESULT
to binary and XORs the values
HOW TO XOR
1=0x2+1
And, converting 8 into binary:
8=4x2+0
4=2x2+0
2=1x2+0
1=0x2+1
6|Page
0 XOR 1 = 1
1 XOR 0 = 1
0 XOR 0 = 0
1 XOR 0 = 1
Result = b1101
3. Convert the resultant binary number back into a decimal number and you
are done!
1 x 23 = 8
1 x 22 = 4
0 x 21 = 0
1 x 20 = 1
8+4+0+1 = 13
Therefore, 5 XOR 8 = 13.
Useful tables
Truth Table for XOR
A
A XOR B
Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
7|Page
DESIGN PROCESS
4.1
Software
We first designed our own 8-bit ALU that had the following functions: Adder,
Boolean, Comparator, Shifter. We tested it to ensure that each unit worked perfectly
fine.
After coming up with the game design and the overall schematics, we proceeded
to use the ROM, D flip-flops, ALU and finite state machine components in the Mojo to
build the game. We made use of the button conditioner and edge detector
components available to prevent metastability issues with the button inputs of the
game. We then uploaded the code to Mojo and tested it with the breadboard
prototype. We debugged the code until we were satisfied that the game was up to
standard before moving on to properly building the hardware.
Hardware
Initially, we used breadboards and jumper cables for our entire game (shown
below).
4.2
DESIGN ISSUES
Cluttering of wires
As seen from the picture of the breadboard-based prototype, there were too many
connections which caused a big issue in trying making our game playable and
presentable.
Division
We wanted to include a division function but this would be an issue as we would
have to deal with non-whole numbers. This would definitely make the game more
challenging but programs tend to round down values and thus makes division
inaccurate. Division also gives rise to irrational numbers and repeating decimal
numbers which complicates our game.
Subtraction
Subtracting two numbers could give us a negative number. Since negative numbers
are not allowed for our game, we had issues with making sure the player only
subtracts a smaller number from a larger one.
Ghosting of LEDs
In the initial stages, all the LEDs always displayed the ghosting value of 0. In the
picture below, the LEDs were supposed to display 1 1 1 1. However, you can see
the faint lighting up of the 0s that make the 1 hard to distinguish.
9|Page
4.3
PROBLEMS SOLVED
Cluttering of wires
This issue was solved when we decided to change our prototype from using a
breadboard and jumper cables to a stripboard and single core wires. It was much
neater but we faced soldering issues instead, which led to connection problems as
well. Eventually, we managed to overcome these issues in the end by our endless
efforts in re-soldering the faulty connections.
Division
We decided to make do without the division function. We did consider replacing it
with a modulo function, but we felt that it made the game too easy for the player.
Hence, we decided to just stick with the basic ADD, SUBTRACT and MULTIPLY.
Subtraction
We added in a second subtraction function (C-S). One allowed SELECTED minus
CURRENT RESULT (S-C) the other, SELECTED minus CURRENT RESULT (C-S). Furthermore
to fool proof it, we set it such that if the player tried to subtract a bigger number from
a smaller number, the subtraction button that selects this option would be disabled.
This means that the player would only be able to press the other subtraction button.
Ghosting of LEDs
After much troubleshooting, we realised that it was due to the code issue. The LED
was a common cathode LED but the tutorial LED from the IO shield that we used
was a common anode LED. This meant that we had to edit the code (remove the
inverse command on the output to the LED) to solve the ghosting issue.
10 | P a g e
COMPONENTS BUDGET
S.N.
1
Component
Unit Price Quantity Total Price
Momentary Push Button Switch 12 mm
$0.80
11
$8.80
Square
2
7-Segment Display 20mm (Blue)
$3.90
2
$7.80
3
7-Segment Display 20mm (Red)
$2.60
2
$5.20
4
7-Segment Blue LED
$2.90
1
$2.90
5
7-Segment Red LED
$2.30
1
$2.30
6
Single Core Wires *
7
Stripboard *
8
Mojo-V3 FPGA *
Total
$27.00
*Items were provided for/scavenged and therefore, not accounted for in our
budget.
SUMMARY
This project gave us the opportunity to showcase what we have learnt so far in
50.002. In the course, we have dissected a computer down to its MOSFET level, and
this game stands testimony of our comprehensive knowledge of the biology of a
computer. One of the most important things we have learnt is the difficulty of
integrating both hardware and software. A good design and plan of approach is
needed for an efficient process in creating something small like a mathematical
game.
Some points of improvement are the number of operators and stages. We used a
Python code to generate OPTIONS and GOALS for each stage. If we could
implement it into the game such that the generation of each stage is done by the
code, it would be a really flexible and widely accommodating game each player
can choose how many options and operations they want and the code will
generate the OPTIONS and GOAL accordingly.
11 | P a g e
REFERENCES
The tutorials that taught us how to use Lucid HDL to code on the Mojo V3-FPGA
helped us greatly. Link: https://embeddedmicro.com/tutorials/lucid
We also received invaluable advice and guidance from our course Professors (Prof
Oka and Prof Zhang Yue) and we would like to thank them for it.
12 | P a g e
APPENDIX
8.1
The ALU design was based on LAB 3 of our 50.002 course. After doing the JSIM code,
we applied the same design architecture of the ALU on the Mojo.
The Lucid HDL code for the ALU and test cases can be found on the link provided
below:
https://github.com/jang93/1D-ALU-Mojo.git
8.2
The Lucid HDL code for the game can be found on the link provided below:
https://github.com/jang93/MMMMATHNESS.git
8.2
PROTOTYPE SCHEMATICS
The following diagrams illustrate the overall schematics of the game as well as the
state transitions that we used to build the software and hardware prototype.
13 | P a g e
This was our initial user interface design for our game. Our final prototype looks
almost similar to it except for the position of the START/RESTART button.
Components in prototype
1. ALU used for addition, subtraction, multiplication, XOR functions
2. D Flip-flop (Register) used to store the different values required for each
stage of the game. (Level, Goal, Options, Selected, Current Result)
3. Finite State Machine used to transition between states of game. (IDLE,
LEVELSELECT, LOAD, LEVEL, WIN, LOSE)
4. Buttons used for player inputs into game. (Operator, Number Selector,
Start/Restart)
14 | P a g e
8.3
COMPONENT SPECIFICATIONS
Push Buttons
http://www.sgbotic.com//products/datasheets/components/TSS-TC-0102.pdf
Blue 4 X 7-Segment LED
http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Components/LED/1LEDBLUCC.pdf
Blue 7-Segment LED
http://www.sgbotic.com/products/datasheets/display/YSD-160AB3C-8.pdf
Red 4 X 7-Segment LED
http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Components/LED/1LEDREDCC.pdf
Red 7-Segment LED
http://www.sgbotic.com/products/datasheets/display/YSD-160AR4B-8.pdf
8.4
Who Is Responsible
Due Date
Done
All
30/10/15
Ideation of game
All
15/11/15
Design of game
implementation
Software design and
implementation
Hardware Prototyping
(Soldering + Wire
Connections)
Designing Levels for Game
(Test cases)
User Interface Design &
Implementation
Software and Hardware
debugging
All
27/11/15
Joel
07/12/15
Michael
08/12/15
Han Jie
10/12/15
Han Jie
10/12/15
All
11/12/15
15 | P a g e