Computer architecture · A level · OCR H446 1.2.1, AQA 7517 4.7.3.6 · about 20 min
Sources of interrupts, polling, the check at the end of each cycle, saving the volatile environment on a stack, and priorities.
[1 mark]When does the processor check for an interrupt?
[1 mark]Why is the volatile environment saved before an ISR runs?
[1 mark]Put the handling of an interrupt in order.
Number the lines 1 to 6 to put them in the right order.
The address of the ISR is loaded into the PCThe registers are pushed onto the stackThe registers are popped off the stack and the program resumesThe current instruction finishes executingThe processor checks for interrupts and finds one of higher priorityThe ISR runsThe current instruction finishes executing The processor checks for interrupts and finds one of higher priority The registers are pushed onto the stack The address of the ISR is loaded into the PC The ISR runs The registers are popped off the stack and the program resumes
Finish, check, save, jump to the ISR, run it, restore.
[1 mark]Why is a stack the right structure for saving registers when interrupts are nested?
[1 mark]Which is a disadvantage of polling compared with interrupts?
Simulate a processor that is interrupted by a timer. The main program runs 12 cycles. In each cycle it drives forward 3 cm, adds 3 to acc, and adds 1 to pc, in that order.
At the end of any cycle after which pc is a multiple of 4 (so after cycles 4, 8 and 12), the timer interrupts. Handle it like this:
1. Save the volatile environment by pushing pc, then acc, onto the list stack, and print saved PC=4 ACC=12 (with the real values).
2. Run the ISR. It uses the accumulator as its working register: set acc to 200 plus 100 times the number of interrupts so far including this one (300, then 400, then 500), and play acc as a tone for 0.2 seconds.
3. Restore the registers by popping them off stack in the reverse order, and print restored PC=4 ACC=12.
After the 12 cycles, print done PC=12 ACC=36, using the values in the registers.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
pc = 0
acc = 0
stack = []
while pc < 12:
forward(50, distance=3)
acc = acc + 3
pc = pc + 1
print(f"done PC={pc} ACC={acc}")The hint students can ask for: The check belongs after the main program's work in each cycle, not before it. Count the interrupts in a separate variable. A stack gives back the last thing pushed first, so think about which register must come off first.
from bugbot import *
connect()
pc = 0
acc = 0
stack = []
interrupts = 0
while pc < 12:
forward(50, distance=3)
acc = acc + 3
pc = pc + 1
if pc % 4 == 0:
stack.append(pc)
stack.append(acc)
print(f"saved PC={pc} ACC={acc}")
interrupts = interrupts + 1
acc = 200 + 100 * interrupts
tone(acc, 0.2)
acc = stack.pop()
pc = stack.pop()
print(f"restored PC={pc} ACC={acc}")
print(f"done PC={pc} ACC={acc}")
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.