S6.7.2 Write the Start-Up Loop

MediumImplement35 minIcarus Verilog · serverReview the topic

A linker left two tables in Boot ROM: regions to copy and regions to clear. Write the start-up code that walks both and then calls main.

Hardware context: Start-up code in Boot ROM on a small processor model, checked against the real ap_rv32. Bare-metal firmware start-up

A larger program has several regions of initialized data and several regions that must start at 0. Its linker script writes them into two tables in Boot ROM.

Write the start-up code, from address 0, as rows of instructions. It sets the stack pointer and the trap vector, walks both tables, calls main, and stops if main returns.

Signals in the Timeline

SignalWidthRole
trial8input of each check
Full specification
The two tables
An end address is one past the last word of the region. A region whose first address equals its end address is empty: nothing is copied or cleared. Every address is a multiple of 4.
TableOne entryThe table ends
Copy table, at COPY_TABLEthree words: the load address, the first run address, the end addressat an entry whose end address is 0
Zero table, at ZERO_TABLEtwo words: the first address, the end addressat an entry whose end address is 0
What main must find
ItemValue
sp, which is x2STACK_TOP
mtvecTRAP
Every word of every region of the copy tablethe word at the same place of its load image
Every word of every region of the zero table0
Every other word of SRAMuntouched

Call main with "jal x1, MAIN". If main returns, the code must stay in a loop of one jump to itself.

A row
Also available: ANDI, ORI, SLLI, SRLI, ADD, SUB, AND, OR. Row n is at address 4 times n. A jump of a row to itself stops the run.
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)x[a] = the address of the next row; go to address c
i(JALR, a, b, c)x[a] = the address of the next row; go to x[b] + c
i(CSRW, a, 0, MTVEC)mtvec = x[a]

Examples

Example 1

One Region to Copy and One to Clear
Input
Parameter set 1; one region to copy and one to clear
Output
stops_after_main = 1, no_exception = 1, main_called_once = 1, sp = 1, mtvec = 1, copied = 1, cleared = 1, rest_untouched = 1

One region of two words is copied and one of two words is cleared.

Example 2

Two Regions in Each Table
Input
Parameter set 2; two regions in each table
Output
stops_after_main = 1, no_exception = 1, main_called_once = 1, sp = 1, mtvec = 1, copied = 1, cleared = 1, rest_untouched = 1

Two entries in each table. The second parameter set moves the tables, main, the handler and the stack.

Example 3

Two Empty Tables
Input
Parameter set 1; two empty tables
Output
stops_after_main = 1, no_exception = 1, main_called_once = 1, sp = 1, mtvec = 1, copied = 1, cleared = 1, rest_untouched = 1

Both tables hold only their closing entry. Nothing is copied or cleared, and main is still called once.

Constraints and Contract

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

Hints

Hint 1

Write the walk of one table first. Read an entry, test its end address for 0, handle the region, and step to the next entry.

Hint 2

Test for an empty region before the first word, not after it.

Hint 3

STACK_TOP is a multiple of 4096, so one lui gives it. The other parameters are below 2048.

What the grader checks: Each trial fills SRAM with words that are not 0 and builds both tables. It looks at the whole SRAM, sp and mtvec at the moment main starts. Trials cover empty tables, empty regions, long regions and regions at the top of SRAM, with two sets of parameters. Timing is not checked.

14 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

One Region to Copy and One to Clear

Input
Parameter set 1; one region to copy and one to clear
Expected output
stops_after_main = 1, no_exception = 1, main_called_once = 1, sp = 1, mtvec = 1, copied = 1, cleared = 1, rest_untouched = 1

One region of two words is copied and one of two words is cleared.

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

Icarus Verilog · server