Anda di halaman 1dari 16

b

50.002 1D Project report

Group 19
JOEL ANG | MICHAEL LIM | SNG HAN JIE

TABLE OF CONTENTS
1

Introduction ....................................................................................................................... 2
1.1

Game Information ........................................................................................................... 2


2.1

Game Description .................................................................................................... 2

2.2

Game Design ............................................................................................................ 2

2.3

Test Scenarios ............................................................................................................ 3

User Manual ...................................................................................................................... 4


3.1

Background ............................................................................................................... 2

How To Play? ............................................................................................................. 4

3.1.1

Components ...................................................................................................... 4

3.1.2

Instructions .......................................................................................................... 5

3.2

Rules ............................................................................................................................ 6

3.3

Explanation of operations ....................................................................................... 6

3.4

How to XOR................................................................................................................ 6

Design Process .................................................................................................................. 8


4.1

Building the Prototype .............................................................................................. 8

4.2

Design Issues .............................................................................................................. 9

4.3

Problems Solved ...................................................................................................... 10

Components Budget ..................................................................................................... 11

Summary .......................................................................................................................... 11

References ...................................................................................................................... 12

Appendix ......................................................................................................................... 13
8.1

ALU Design and Tests .............................................................................................. 13

8.2

Game code for mojo ............................................................................................. 13

8.2

Prototype Schematics ............................................................................................ 13

8.3

Component Specifications ................................................................................... 15

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

MMMMATHNESS is a mathematical problem-solving game where the player makes


use of various mathematical operations to manipulate a given set of numbers to
achieve a predetermined target result. The player is challenged to logically work out
the right sequence of operations to perform on the right sequence of numbers so as
to achieve the result.
Given a random set of numbers from 1-9, the player will have to use the numbers
and perform various operations to obtain a set target goal result. Only after
producing the goal using all of the given numbers and any of the available
mathematical functions will the player be able to pass the stage and win the game.
There are 3 main levels (easy, medium, hard) in the game; each with a different
feature to vary the difficulty of the game. Each level also has 3 stages (3 problems to
solve).
The twist in this game is a new (XOR) operation, featured in the hardest level, taking
the mathematical functions across both decimal and binary based numbers and
also opening up the options for the player.

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.

PRESS 1 to select 1 from OPTIONS and display it in SELECTED.


PRESS + so that the CURRENT RESULT will display 1.
PRESS 2 to select 2 from OPTIONS and display it in SELECTED.
PRESS + so that the CURRENT RESULT will display 3.
PRESS 3 to select 3 from OPTIONS and display it in SELECTED.
PRESS + so that the CURRENT RESULT will display 6.
PRESS 4 to select 4 from OPTIONS and display it in SELECTED.
PRESS + so that the CURRENT RESULT will display 10.
SUCCESS!

*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

What better way to explain than through an example?


5 XOR 8
1. Convert the decimal numbers into binary.
5=2x2+1
2=1x2+0

read from bottom up b0101

1=0x2+1
And, converting 8 into binary:
8=4x2+0
4=2x2+0

read from bottom up - b1000

2=1x2+0
1=0x2+1

6|Page

2. XOR each corresponding bit of the numbers.


5

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

Conversion Table from Decimal to Binary


Decimal
0
1
2
3
4
5
6
7
8
9

Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001

7|Page

DESIGN PROCESS

4.1

BUILDING THE PROTOTYPE

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).

Eventually, we moved on to implementing our game on a stripboard instead.


We decided on a layout to follow and soldered on the LEDs and buttons
accordingly. Then, we connected the wires and soldered them to the stripboards
and their corresponding hardware parts. We proceeded to test the circuit to check
that the game worked fine. Once we confirmed that the parts and wires were
properly soldered, we cut the wires to reduce their length to its minimum so that we
could reduce the cluttering. Finally, we build a cover for the prototype and labelled
the different LEDs and buttons.
Integrating the software and hardware was tough as we were met with many issues
(explained in the next section) but with time and effort, we managed to successfully
troubleshoot our prototype. More information on the prototype schematics can be
found in Appendix 8.1.
8|Page

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.

Keeping track of IO pins and corresponding wire connections


We initially simply connected all the wires into any random IO pin into the Mojo. This
became a huge problem when we had to define the constraints for the IO pins in
the code. We had too many wires which made it difficult to see which pin number
was connected to the specific wire.

9|Page

Single digit on 4 x 7-segment LED issue


The first digit in our OPTIONS 4 x 6-Segment LED (BLUE) did not light up (as shown
below). Even though we continuously checked the connection using a multimeter.
We were unsure if it was due to the overloading of pins, soldering issues, or that the
LED had overheated.

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

Keeping track of IO pins and corresponding wire connections


We decided to come up with an organised and systematic way of plugging the
wires into the IO pins instead. We recorded the IO pin number of each wire and used
that to update the constraints file in the Mojo accordingly.
Single digit on 4 x 7-segment LED issue
After many failed attempts to solve this issue, we decided to shift down the words
that we initially wanted to display on the first four digits of the OPTIONS LEDs. For the
PASS and FAIL displays, we shifted it down to the second to fifth LEDs. Also, we
discovered that the first digit could still display 1 and 4 without issues. Hence, we
decided to change our OPTIONS for each stage to have at least a 1 or 4 in them
and display them at the first digit for each stage.

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

ALU DESIGN AND TESTS

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

GAME CODE FOR MOJO

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

PROJECT MANAGEMENT LOG

Project Name: MMMMATHNESS


Team Members: Joel Ang, Michael Lim, Sng Han Jie
Task

Who Is Responsible

Due Date

Done

Design and Implementation


of ALU

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

Anda mungkin juga menyukai