S6.16.1 Blink on a Timer Interrupt

MediumImplement40 minIcarus Verilog · serverReview the topic

Make pin 0 of GPIO change every PERIOD ticks, from a timer handler that another program never notices.

Hardware context: Four bring-up programs on a small processor model in front of ap_plic and the ap_clint timer, checked against the real ap_rv32. First firmware on a new chip or board

A board has a lamp on pin 0 of GPIO. It must blink at an exact rate while the main program does its own work.

Write the set-up code, from address 0, and the handler of the machine timer interrupt. The set-up code ends with a jump to MAIN. The main program belongs to the bench and never returns.

Signals in the Timeline

SignalWidthRole
trial8input of each check
Full specification
Registers you need
AddressNameWhat it does
0x0200_BFF8mtime, low wordRead: the time, in ticks. It adds 1 at every tick.
0x0200_4000 and 0x0200_4004mtimecmp, low and high wordThe timer requests an interrupt while mtime is at least mtimecmp. After reset both words are all ones.
0x1000_1014GPIO TGLWrite: a 1 inverts that bit of the output word. A 0 leaves it.
mie, bit 7The enable of the machine timer interrupt.
mstatus, bit 3MIE, the global enable.
mtvecThe address of the handler.
What the bench checks
CheckMeaning
The first change of pin 0at most PERIOD + 64 ticks after reset
Every later changeexactly PERIOD ticks after the one before
The other 15 pinsnever change
x1 and x3 to x31, as the main program sees themnever changed by the handler
sp while the main program runsSTACK_TOP
The main programkeeps running, with interrupts enabled

The main program uses x5 to x31 and no stack. One tick is one instruction in this model. The numbers stay small: ignore the high word of mtime.

A row
CSR names: MSTATUS, MIE, MTVEC, MSCRATCH, MEPC, MCAUSE, MIP. Also available: ANDI, ORI, SLLI, SRLI, ADD, SUB, AND, OR. Row n is at address 4 times n.
FormMeaning
i(LUI, a, 0, c)x[a] = c shifted left by 12. c has at most 20 bits.
i(ADDI, a, b, c)x[a] = x[b] + c. c is from -2048 to 2047.
i(LW, a, b, c) and i(SW, a, b, c)load x[a] from, or store x[a] to, the address x[b] + c
i(BEQ, a, b, c), BNE, BLT, BGEcompare x[a] with x[b]; if true, go to address c
i(JAL, a, 0, c) and i(JALR, a, b, c)x[a] = the address of the next row; go to c, or to x[b] + c
i(CSRR, a, 0, name), i(CSRW, a, 0, name)read a CSR into x[a]; write x[a] to a CSR
i(CSRS, a, 0, name), i(CSRC, a, 0, name)set, or clear, the bits of a CSR that are 1 in x[a]
i(MRET, 0, 0, 0)return from a handler
at(address)the rows that follow start at this address

Examples

Example 1

A Period of 200 Ticks
Input
Parameter set 1; GPIO OUT starts at 0x0000
Output
no_exception = 1, first_toggle_in_time = 1, period_exact = 1, still_blinking = 1, pins = 1, registers_kept = 1, sp_kept = 1, main_running = 1

All eight checks hold with a period of 200 ticks.

Example 2

A Period of 150 Ticks
Input
Parameter set 2; GPIO OUT starts at 0x5550
Output
no_exception = 1, first_toggle_in_time = 1, period_exact = 1, still_blinking = 1, pins = 1, registers_kept = 1, sp_kept = 1, main_running = 1

The second parameter set has a period of 150 ticks, another main address and another stack top.

Example 3

Other Pins High
Input
Parameter set 1; GPIO OUT starts at 0xfffe
Output
no_exception = 1, first_toggle_in_time = 1, period_exact = 1, still_blinking = 1, pins = 1, registers_kept = 1, sp_kept = 1, main_running = 1

Fifteen pins start high and must stay high.

Constraints and Contract

  • Keep module my_blink, its three parameters and the declarations below the task. Change only the rows of the_program.
  • Use the parameters. The bench builds the module twice, with different values. At most 128 rows.

Hints

Hint 1

An exact period needs each deadline to come from the deadline before it, not from the time the handler happens to run.

Hint 2

The handler interrupts a program that uses every register from x5 up. Save what you use, and restore it.

Hint 3

Writing a later deadline to mtimecmp is what drops the request.

What the grader checks: Each trial runs 1300 ticks of the model with the bench main program, and records the tick of every change of the GPIO output word. It checks the period, the other pins, 27 registers and sp, with two sets of parameters. One tick is one instruction in the model; on the real core an instruction takes several clock cycles.

6 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 problems on this path

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

A Period of 200 Ticks

Input
Parameter set 1; GPIO OUT starts at 0x0000
Expected output
no_exception = 1, first_toggle_in_time = 1, period_exact = 1, still_blinking = 1, pins = 1, registers_kept = 1, sp_kept = 1, main_running = 1

All eight checks hold with a period of 200 ticks.

Run samples executes all 3 public checks. Check all tests runs 6 checks on the server.

Icarus Verilog · server