04-Exceptions and Interrupts
04-Exceptions and Interrupts
Objectives
The objective of this exercise is to reinforce the ideas presented in lectures on exceptions and
interrupts. To achieve this, you’ll be creating a stopwatch program that utilises the interrupt and
exception handling mechanisms of the WRAMP CPU.
It is highly recommended that you read this entire specification before beginning to write any code.
Assessment
This exercise contributes 10% towards your internal grade, to be assessed in two parts:
● The correctness and formatting of your source code, to be submitted via Moodle (7%)
● Your completion of an online quiz about the exercise, also on Moodle (3%)
This is an individual exercise; while you may discuss the assignment with others in general terms,
you must write your own code, and complete the quiz by yourself.
For example, the first question in this exercise would be called ex4_q1.srec
You should fully comment all your programs and indent them tidily so that your code is easy to read.
You can check your answers using the automatic marker provided. You can use it in the labs,
matching the following format:
Introduction
In this exercise, you’ll be creating a stopwatch program for the Basys board, using the interrupt
handling features of the WRAMP CPU. Each question builds on the previous question, so make sure
that each step is working flawlessly before moving on – fixing bugs while the program is small is much
easier than fixing them later! Remember to keep copies of the code for each question, because they
all need to be handed in as part of your submission. Use the mark203 tool to make sure each step is
working correctly.
Before starting this exercise, you’ll need to read Chapter 4 of the WRAMP manual.
Notes
This is probably the most challenging exercise in the course, with the exception of the upcoming
multitasking kernel assignment. Be kind to yourself and start early, so that you can make the most
effective use of the supervised lab sessions if you get stuck. Work in small steps, and make sure you
understand what you’re doing before attempting to write any code. If in doubt, ask!
Questions
1. Counter
Write an assembly program (called ex4_q1.s) that counts the number of times that any
button (User Interrupt Button and/or Push Buttons 0, 1, or 2) is pressed. This will consist of
two parts:
a. A mainline loop that continuously reads from a location in memory (perhaps called
counter in the .data segment), and writes its value to the SSDs as a two-digit decimal
number.
b. Exception handlers for the User Interrupt Button and Parallel Port Interrupt. These
handlers should simply increment counter by one.
Make sure that your exception handlers are compliant; that is, they must not alter the value
stored in any register other than $13. Also, make sure that you correctly call the default
exception handler for any exceptions that your code doesn’t handle directly.
Note: pressing both push buttons should have the same effect as a single button being
pressed, and make sure that other parallel port interrupts (i.e. from the switches changing
state) are ignored.
2. Timer
Create a copy of your code from question 1, and call it ex4_q2.s. Now modify the code so
that the SSDs show the number of seconds that the program has been running. To do this,
set up the Programmable Timer so that it generates one interrupt per second, and update
your exception handling code accordingly. ALL buttons should no longer have any effect.
3. Stopwatch
Add the ability to start, stop, and reset the stopwatch with the push buttons. To do this,
create a copy of your code from question 2 (call it ex4_q3.s) and add a second exception
handler that responds to interrupts from the Parallel Port:
a. Push Button 0 should start (or resume) the stopwatch if it is not currently running,
or stop (pause) the stopwatch if it is currently running. Hint: just toggle the Timer
Enable bit.
b. Push Button 1 should reset the counter to zero if the timer is not currently running.
If the timer is currently running, this button should do nothing.
c. Push Button 2 should terminate the program (e.g. jr $ra from the context of
main). Hint: use a termination flag that is set when button 2 is pressed. Remember
not to use a syscall here!
Note: make sure that other parallel port interrupts (i.e. from the switches changing state) are
ignored.
Now that you have a start/stop button, modify your timer initialisation so that the timer is
not running when the program first starts. In other words, the timer should only start
running when the user presses Push Button 0.
The string printed to the serial port must be in the format “\r\nss.ss”, where ‘\r’ is the
carriage return character, ‘\n’ is the newline character, and “ss.ss” is the number of seconds
elapsed, including hundredths.
Note: steps a and b are reasonably straightforward, but c requires a bit more work. Recall
that slow operations like polled I/O should never be done in an exception handler; instead,
either use polled I/O in the mainline code (recommended approach), or use interrupt-driven
I/O (better, but much more complicated!). You may find it useful to begin by transmitting a
single character to the serial port when the lap button is pressed, before trying to transmit
the properly-formatted string.
Model Solutions
For reference, model solutions are (for each question):
If you’re writing significantly more than this, chances are that you’re doing something wrong, or are
missing a potentially simpler approach. In this case, please ask for assistance at one of the
supervised lab sessions. Note that the line counts quoted are for model solutions that have fully-
compliant exception handlers, but do not comply with the WRAMP ABI for subroutines in the
mainline code. ABI compliance is not necessary for this exercise.
Submission
You are required to submit all source files that you have written; you do not need to submit any files
that we have provided, nor any files that have been generated by a tool, e.g. wcc, wasm or wlink.
Each file must follow the naming convention indicated below.
● ex4_q1.s (Question 1)
● ex4_q2.s (Question 2)
● ex4_q3.s (Question 3)
● ex4_q4.s (Question 4)
These files must be compressed into a single “dot tgz” archive called firstName_lastName.tgz
(replace firstName_lastName with your own name) before being submitted to Moodle. You can
create this archive from the terminal, using the command:
Please be aware that a grade penalty applies if you do not submit your code in the correct format,
including using the correct file names and file extensions. If you are unsure if what you’ve done is
correct, please ask and we’ll help you out.