The Boot Flow
Your board runs as soon as a debugger loads the program. Pull the cable and power it up again. Who puts the program there this time?
You Will Be Able To
- Trace a cold start from reset release to the first instruction fetched from SRAM.
- Tell an address that answers from a complete program that is ready to run.
- Locate a boot failure from the saved strap and the completed transfers.
- Lesson
- S6.6
- Time
- 26 min
- Runs on
- Your browser
- Check
- 6 questions
You can see the first request reach SRAM, right where your program belongs. Yet the board never reaches its first line. Work backwards from that fetch: who last wrote the word it picked up?
Before You Start
You need S0.6: the PC, a fetch and a jump. You need S0.7: ROM, RAM and flash, and which of them forget.
You need S0.8: reset puts a fixed address in the PC. The Power-Up Sequence brings power, clock and reset up to the point where this page starts.
| Question | |
|---|---|
| 1 | Why can software not choose the address of its own first instruction? |
| 2 | Why must the memory at the reset vector be ROM? |
| 3 | What does the boot strap choose, and when is it read? |
| 4 | What does the loader do before it jumps? |
| 5 | What does a correct first fetch from SRAM prove, and what does it not prove? |
The Idea: Each Step Makes the Next One Possible
Start one step earlier than usual, before the board has run a single instruction. Software cannot choose its own first address, because choosing would already take an instruction. So hardware chooses. Reset puts one fixed address in the PC. That address is the reset address. Engineers call it the reset vector, and so does this page.
Something must be waiting there. SRAM forgets when the power goes, so at power-on it holds nothing useful. The chip therefore puts a small memory that never forgets at the reset vector. It is the boot ROM, and its content is a short program named the loader.
The loader has one job: find the real program and start it. It copies the program from the flash chip into SRAM. Then it jumps there. This chain, from reset to the first instruction of your program, is the boot flow. To boot is to go through it.
One pin of the board decides whether the loader runs at all. It is the boot strap of The Power-Up Sequence. The chip samples it once at power-up and saves the bit. At 0 the core starts at 0, in the boot ROM. At 1 it starts at 0x80000000, in SRAM, and the ROM never runs. That only works if something, usually a debugger, has already loaded SRAM.
Every link uses only what the link before it made ready. Break one link and the board is silent. That is why boot faults are found by walking the chain, not by reading the program.
New Words
| Word | Meaning |
|---|---|
| Cold start | A start from power-off. SRAM holds nothing useful. |
| Reset address | The address that reset puts in the PC. Engineers also call it the reset vector. On ap_soc the strap picks it: 0 or 0x80000000. |
| Boot ROM | A small ROM at the reset vector. Its content is fixed when the chip is made. |
| Loader | A program whose job is to bring another program into memory and start it. |
| Image | The complete program as stored: every word, in order. |
| Entry | The address of the first instruction of an image. On ap_soc it is 0x80000000, the start of SRAM. |
| Strap pin | A pin of the board held at 0 or 1 during start. The chip samples it once at power-up and saves the bit. A strap for short. |
| Saved strap | The bit the chip stored at that sample. On ap_soc: 0 = start in the boot ROM, 1 = start at 0x80000000, in SRAM. |
| Handover | The jump from the loader to the entry of the image. |
One Picture: Five Steps
- Control / FSM
- Processor
- Memory
- Peripheral
- Reset picks the address
Reset puts the reset vector in the PC. No instruction has run, so no program chose it. The strap, saved at power-up, picks it.
- ROM answers
With the strap at 0 the PC starts at 0, where Boot ROM sits. Its content was fixed when the chip was made, so the first fetch always finds an instruction.
- The loader copies the image
Word by word, the loader reads the image from flash and writes it to SRAM.
- The loader jumps
After the last word is written, the loader jumps to the entry of the image. Your program runs.
- The other setting
With the strap at 1 the PC starts at 0x80000000, in SRAM. The ROM never runs. Something else must have put the image there first.
Read the diagram as text
- Strap pin: sampled once, then saved
- Processor: PC = the reset vector
- Boot ROM: the loader, fixed in the chip
- Flash chip: holds the image with the power off
- SRAM: empty at power-on
- Strap pin → Processor: 0: ROM, 1: SRAM
- Processor → Boot ROM: first fetch
- Flash chip → Boot ROM: one word at a time
- Boot ROM → SRAM: copy
- Processor → SRAM: jump to the entry
The boot flow of ap_soc. The lab below has ROM, the saved strap, a flash data register and two words of SRAM.
Read the picture once more as a question at each step: what must already work? At step 2 only ROM. At step 3, ROM, the flash chip and SRAM. Your program needs all of it. Step 5 is the other road: no ROM, and SRAM loaded by someone else.
The Smallest Example, Worked by Hand
An image of two words, A and B, sits in flash. The saved strap is 0. Follow the loader, and count the words of the image that SRAM holds after each step.
| Transfer | Who asks | Where | What comes back, or goes in | Words of the image in SRAM |
|---|---|---|---|---|
| 1 | The processor, after reset | The reset vector, 0, in ROM | The first instruction of the loader | 0 |
| 2 | The loader | The flash data register | Word A | 0 |
| 3 | The loader | The entry, in SRAM | Word A goes in | 1 |
| 4 | The loader | The flash data register, again | Word B | 1 |
| 5 | The loader | The entry plus 4 | Word B goes in | 2 |
| 6 | The processor, after the jump | The entry | Word A, as an instruction | 2 |
Two things to notice. The flash address is the same in transfers 2 and 4: each read of that register takes the next word. The SRAM address steps by four, because both words must survive.
The Rules
Rule 1: Reset gives the PC its first address.
It is the reset vector. No instruction chooses it.
Rule 2: At a cold start, the reset vector must be in memory that does not forget.
So the strap at 0 starts in ROM. The strap at 1 starts in SRAM, which works only after someone loaded it.
Rule 3: The strap is sampled once and saved.
The saved bit picks the reset vector. A pin that moves later changes nothing.
Rule 4: The loader copies every word of the image.
A word that was never written holds whatever SRAM woke up with.
Rule 5: The loader jumps only after the last write.
The handover is the last thing the loader does.
Rule 6: One good fetch proves one word.
An address that answers is not a complete image.
Sources for These Rules
ROM and the memory that holds a start program: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 5.5.6. Reset and the first address: the same book, section 6.6.3.
Our own choices for ap_soc: the strap of one bit, its two start addresses (0 and 0x80000000) and the moment of sampling. They are the same as in The Power-Up Sequence. The two-word image and the entry at 0x80000000 are ours too.
Rules 3 to 6 are practice, shown by the lab. No specification in our library sets them.
Now Run It
The lab at the end of this page has ROM, the saved strap and a flash data register. It has two words of SRAM. Our processor is built later, so a bus-functional model stands in for it. That is test code that makes the reads and writes the loader would make.
reset fetch addr=00000000 data=100042b7
flash read addr=10005000 data=02a00513
SRAM write addr=80000000 data=02a00513
flash read addr=10005000 data=0000006f
SRAM write addr=80000004 data=0000006f
entry fetch addr=80000000 data=02a00513Rules 1 and 2 at work: the row "reset fetch" has address 00000000, and the word 100042b7 was waiting there. Nothing had written to SRAM yet.
Rule 3 at work: the lab flips the strap pin to 1 right after sampling. The row "reset fetch" still goes to 00000000, in ROM.
Rules 4 and 5 at work: follow 02a00513. It leaves 10005000 as data and arrives at 80000000 as data. In the last row it comes back as an instruction. Nothing inside the word changed.
- Peripheral
- Processor
- Memory
A lit wire carries a 1. A pill is a transfer in that cycle.
- Transfer 0
The saved strap is 0, so the first fetch goes to the boot ROM. SRAM holds nothing yet.
- Transfer 1
The flash data register supplies the first word of the image.
- Transfer 2
SRAM now holds the first word. The second is still missing.
- Transfer 3
The same flash address supplies the second word.
- Transfer 4
The last write completes. Both words of the image are in SRAM.
- Transfer 5
The first fetch from SRAM. Compare its word with transfer two.
Six word transfers from the lab on Icarus, with the strap at 0. The two bottom lanes say how much of the image SRAM holds after each transfer. Idle time between transfers is left out.
This lab leaves things out. No instruction is executed: the model only makes the transfers. The flash chip has both words ready, and the serial link to it is not modelled. Clock and reset start-up are over before the first row.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A small ROM at the reset vector | The program itself at the reset vector | The program can change with every release and the ROM never does. A fault in ROM cannot be repaired. |
| A strap picks ROM or SRAM as the start | Always start in ROM | A debugger can load SRAM and start a program with no flash and no loader. A pin is spent, and a wrong jumper starts in an empty SRAM. |
| Sample the strap once | Read the pin whenever asked | The pin can be used for something else afterwards. When you debug, read the saved bit, not the pin. |
| Copy the image to SRAM | Run it straight from flash | Fetches are fast, and the image can be checked first. The copy takes time at every start, and SRAM must hold it. |
The Trap: A Board That Boots Only Under the Debugger
Back to the board from the top of the page. The first fetch reaches SRAM and returns the right word. Under the debugger everything works. On a cold start nothing does.
Here is a loader with one fault: its loop copies one word instead of two. The lab then fetches both words.
SRAM write addr=80000000 data=02a00513
entry fetch addr=80000000 data=02a00513
second fetch addr=80000004 data=xxxxxxxxThe entry fetch is perfect. The second fetch returns x, which in a simulator means "never written". On silicon it is whatever SRAM woke up with. Rule 6 is the lesson: one good fetch proves one word.
Now the debugger. When it loads your program it writes the whole image into SRAM itself. That quietly supplies the write the loader forgot. So take the cable off and capture every write of a cold start. Then compare the last one with the end of the image.
Check one more suspect: a strap left at 1. Then the ROM never runs, and only the debugger ever fills SRAM.
At Scale
This section looks ahead. On a large chip the chain has more links, and each link is owned by a different team.
A second loader
External memory needs set-up before it can hold anything. So the ROM loads a small second loader into on-chip SRAM, and that one prepares the big memory and loads the program.
A description for the next program
An operating system must learn which memory and which devices the board has. One common handover passes it a table that describes the board.
Who owns an interrupt at the handover
A request can be waiting at the moment of the jump. Agree which program has a handler ready before interrupts are enabled. Interrupts at the Core covers the enable bits.
Checking the image
A loader can refuse an image that was changed. Secure boot, later in the track, builds on this page. A strap at 1 skips the ROM, and with it any check the ROM makes.
Try It
Run the copy. Pair each flash read with the SRAM write that follows it.
Change strap_pin to 1 where it is declared. Find the first row that differs.
Shorten the loop to one word, and add a fetch at 0x80000004. This is the trap run of this page.
Names Printed by the Lab
| Row starts with | Meaning |
|---|---|
| reset fetch | The first fetch after reset, at the reset vector. |
| flash read | A read of the flash data register. Each read takes the next word. |
| SRAM write | The loader writes one word of the image. |
| entry fetch | The first fetch from SRAM, at the entry of the image. |
reset fetch addr=00000000 data=100042b7 flash read addr=10005000 data=02a00513 SRAM write addr=80000000 data=02a00513 flash read addr=10005000 data=0000006f SRAM write addr=80000004 data=0000006f entry fetch addr=80000000 data=02a00513Read included file: boot_window.sv
// Word-transaction model of the locations touched by the S6 bootstrap.
// The board strap is sampled once, on the first clock out of reset, and saved as boot_sel.
// As in ap_crg_boot.sv: 0 = start at 0x0000_0000 (boot ROM), 1 = start at 0x8000_0000 (SRAM).
// SPI_DATA abstracts a flash controller with a two-word receive stream.
module boot_window(
input logic clk, reset_n, strap_pin, request, writing,
input logic [31:0] address, write_data,
output logic [31:0] read_data, output logic hit, boot_sel
);
localparam logic [31:0] ROM = 0, SPI_DATA = 32'h1000_5000, RAM = 32'h8000_0000;
logic sampled;
logic [1:0] cursor;
logic [31:0] ram [0:1];
always_comb begin
hit = 1;
case (address)
ROM: read_data = 32'h1000_42b7; // lui t0,0x10004
SPI_DATA: begin
hit = cursor < 2;
read_data = cursor == 0 ? 32'h02a0_0513 : 32'h0000_006f;
end
RAM, RAM+4: read_data = ram[address[2]];
default: begin hit = 0; read_data = 0; end
endcase
if (writing && address != RAM && address != RAM+4) hit = 0;
end
always_ff @(posedge clk) begin
if (!reset_n) begin
sampled <= 0;
boot_sel <= 0;
cursor <= 0;
end else begin
if (!sampled) begin boot_sel <= strap_pin; sampled <= 1; end
if (request && hit && writing) ram[address[2]] <= write_data;
if (request && hit && !writing && address == SPI_DATA) cursor <= cursor + 1'b1;
end
end
endmodule
Check Yourself
Common Mistakes
Wrong Use the address of the program as the reset vector.
At a cold start SRAM holds no program.
Fix Start in ROM and let the loader bring the program in.
Wrong Read the strap pin with a probe to learn where the core started.
The chip uses the bit saved at power-up. The pin may have moved since.
Fix Read the saved bit (boot_sel in the lab), or the address of the first fetch.
Wrong Take one good fetch as proof of a good image.
A short copy still includes the first word.
Fix Check the whole range, and the last completed write.
Remember
- Reset puts a fixed address, the reset vector, in the PC. Boot ROM sits there because it never forgets.
- A strap, sampled once and saved, picks the start: 0 = the boot ROM and its loader, 1 = SRAM directly.
- The loader copies the whole image to SRAM and only then jumps to its entry.
- One good fetch proves one word. Check the last write of a cold start.