To poll a device is to actively and continuously check its status to see if it is ready to be used in some way. In this lab we will use the polling concept for two devices, the JTAG Universal Asynchronous Receiver/Transmitter (UART) and the Timer, in order to create a simple game.
You are to create a game, where the numbers 0-9 scroll across the terminal from right to left, and must be removed before they reach the end. Numbers are generated using the provided random number generator and should be added to the end of the line, and the entire line should continuously scroll left. If any number reaches the leftmost space on the line, the game is over and you should display "GAME OVER". By pressing a key on the keyboard, all of the corresponding characters should be removed from the line.
For example, the program begins with a blank line. After a few seconds, it might look like:
12 4 51844 5
If the number "4" was then pressed, it would look like:
12 518 5
Then some time later, it might look like:
3 145 57 8 2232 1347 15 45
If the number 3 was not cleared before the next line shift, the game would end and the window would display
GAME OVER
The line should scroll at a constant rate of 1 shift/second and should be 50 characters long.
To implement this game you need three things: (i) the ability to display characters to the user, (ii) the ability to receive character input from the user, and (iii) the ability to measure 1 second in time. The JTAG UART will handle (i) and (ii), while the Timer will be used for (iii).
A UART in general is a device which translates between parallel (many bits at a time) communication to serial (one bit at a time) communication. UART's are often built ontop of other serial standards such as RS232 which you can connect to your PC and view using Hyperterminal. However in this lab we will use a JTAG-based UART which is specially setup to transfer data serially through the USB-Blaster programming cable, while the Nios II processor on the DE2 board sends/receives data in parallel.
Entering "nios2-terminal" in the Nios II Command Shell activates the terminal program which communicates with the JTAG UART on the PC side. You can send data to the UART by typing in this window, and any data the UART receives from the DE2 board gets displayed on this terminal. A diagram of the system is shown below.
The documentation of the JTAG UART can be found here. As listed in the documentation, bit 15 of the Data Register is a "data valid" bit. To see if a character is ready to be received from the UART, one can continually check this bit in a loop, and when it is valid, branch to code that actually receives it. To determine when a new character can be written to the UART, one can check the write FIFO space in the Control Register - when it is non-zero, there is room for more characters to be sent to the UART. This is the concept of polling as applied to the JTAG UART.
To keep accurate time of how long to wait between every line shift, you must use the Timer peripheral. You can find the documentation of it here, note that there are 2 timers in the system, you can use either one. Like the JTAG UART, the Timer can be polled to see if it has come time to shift the line of numbers. Make sure you correctly set the period for the timer.
Notice that you will need to be polling both the Timer (to see when to shift), and the JTAG (to see if the player has pressed a button), for when characters are received, at the same time. Therefore, you will need a single loop to check both of these peripherals, and separate branches for each condition. Ensure that both are checked in every loop, or your program will not function correctly.
For creating random numbers to append to the line, you should use the random number generator function provided here. Just call the function "lab5_rand" and the ASCII character for a random number (or sometimes a space) will be returned in register r2. To call the function use the example below and note that the function only uses r2 and r3 so you don't need to save any (other) registers.
call lab5_rand /* random character is returned in r2 */To compile your program (assuming it's called lab5.s), use the following command which will also link in the random number generator:
make SRCS="lab5.s lab5_rand.s" compile
Demonstrate your working game.