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.

Five questions this page answers
Question
1Why can software not choose the address of its own first instruction?
2Why must the memory at the reset vector be ROM?
3What does the boot strap choose, and when is it read?
4What does the loader do before it jumps?
5What 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

New words on this page
WordMeaning
Cold startA start from power-off. SRAM holds nothing useful.
Reset addressThe 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 ROMA small ROM at the reset vector. Its content is fixed when the chip is made.
LoaderA program whose job is to bring another program into memory and start it.
ImageThe complete program as stored: every word, in order.
EntryThe address of the first instruction of an image. On ap_soc it is 0x80000000, the start of SRAM.
Strap pinA 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 strapThe bit the chip stored at that sample. On ap_soc: 0 = start in the boot ROM, 1 = start at 0x80000000, in SRAM.
HandoverThe jump from the loader to the entry of the image.

One Picture: Five Steps

Five Steps from Reset to the ProgramStep through the picture. Each step uses only what the step before it made ready.
  • Control / FSM
  • Processor
  • Memory
  • Peripheral
Five Steps From Reset to the ProgramStep through the picture. Each step uses only what the step before it made ready. component Strap pin sampled once, thensaved component Processor PC = the resetvector component Boot ROM the loader, fixed inthe chip component Flash chip holds the image withthe power off component SRAM empty at power-on 0: ROM, 1: SRAMfirst fetchone word at a timecopyjump to the entry
  1. component Strap pinsampled once, then saved
    • 0: ROM, 1: SRAM → Processor
  2. component ProcessorPC = the reset vector
    • first fetch → Boot ROM
    • jump to the entry → SRAM
  3. component Boot ROMthe loader, fixed in the chip
    • copy → SRAM
  4. component Flash chipholds the image with the power off
    • one word at a time → Boot ROM
  5. component SRAMempty at power-on
  1. 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.

  2. 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.

  3. The loader copies the image

    Word by word, the loader reads the image from flash and writes it to SRAM.

  4. The loader jumps

    After the last word is written, the loader jumps to the entry of the image. Your program runs.

  5. 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.

Six transfers, by hand
TransferWho asksWhereWhat comes back, or goes inWords of the image in SRAM
1The processor, after resetThe reset vector, 0, in ROMThe first instruction of the loader0
2The loaderThe flash data registerWord A0
3The loaderThe entry, in SRAMWord A goes in1
4The loaderThe flash data register, againWord B1
5The loaderThe entry plus 4Word B goes in2
6The processor, after the jumpThe entryWord A, as an instruction2

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

  1. Rule 1: Reset gives the PC its first address.

    It is the reset vector. No instruction chooses it.

  2. 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.

  3. Rule 3: The strap is sampled once and saved.

    The saved bit picks the reset vector. A pin that moves later changes nothing.

  4. Rule 4: The loader copies every word of the image.

    A word that was never written holds whatever SRAM woke up with.

  5. Rule 5: The loader jumps only after the last write.

    The handover is the last thing the loader does.

  6. 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.

Recorded on Icarus with the strap at 0. Each row is one completed word transfer.
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=02a00513

Rules 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.

From the Reset Address to the Entry of the ProgramEach column is one completed word transfer. Follow the address down to the word it carried.
  • Peripheral
  • Processor
  • Memory
next wordcopyfirst fetchFlash chipLoader in ROMSRAM
next word→ Loader in ROMcopy→ SRAMfirst fetch→ Loader in ROMFlash chipLoader in ROMSRAM

A lit wire carries a 1. A pill is a transfer in that cycle.

012345address000000001000500080000000100050008000000480000000word100042b702a005130000006f02a00513reset fetchflash readSRAM writeentry fetchfirst word in SRAMwhole image in SRAM
Scroll horizontally ↔
  1. Transfer 0

    The saved strap is 0, so the first fetch goes to the boot ROM. SRAM holds nothing yet.

  2. Transfer 1

    The flash data register supplies the first word of the image.

  3. Transfer 2

    SRAM now holds the first word. The second is still missing.

  4. Transfer 3

    The same flash address supplies the second word.

  5. Transfer 4

    The last write completes. Both words of the image are in SRAM.

  6. 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 choiceThe alternativeWhat the choice buys, and costs
A small ROM at the reset vectorThe program itself at the reset vectorThe 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 startAlways start in ROMA 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 onceRead the pin whenever askedThe pin can be used for something else afterwards. When you debug, read the saved bit, not the pin.
Copy the image to SRAMRun it straight from flashFetches 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.

The same lab with the copy shortened to one word, and one more fetch at the second word.
SRAM write addr=80000000 data=02a00513
entry fetch addr=80000000 data=02a00513
second fetch addr=80000004 data=xxxxxxxx

The 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 page1 loader, 2 words
A phone chipseveral loaders, one after another

This section looks ahead. On a large chip the chain has more links, and each link is owned by a different team.

  1. 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.

  2. 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.

  3. 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.

  4. 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 withMeaning
reset fetchThe first fetch after reset, at the reset vector.
flash readA read of the flash data register. Each read takes the next word.
SRAM writeThe loader writes one word of the image.
entry fetchThe first fetch from SRAM, at the entry of the image.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=02a00513
Read 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

Predict the outputIn this run the strap is sampled at 1. What is the only row the lab prints?
module tb;
  logic clk = 0, reset_n = 0, strap_pin = 1, request = 0, writing = 0;
  logic [31:0] address, write_data, result;
  wire [31:0] read_data;
  wire hit, boot_sel;
  boot_window memory (.*);
  always #5 clk = ~clk;

  task automatic transfer(input string step, input bit wr,
                          input logic [31:0] addr, data);
    @(negedge clk);
    request = 1; writing = wr; address = addr; write_data = data;
    @(posedge clk);
    if (!hit) $fatal(1, "No target at %h", addr);
    result = wr ? write_data : read_data;
    $display("%s addr=%08h data=%08h", step, addr, result);
    #1 request = 0;
  endtask

  initial begin
    @(negedge clk); reset_n = 1;
    @(posedge clk); #1 strap_pin = !strap_pin;
    // The saved strap picks the first address: 0 = boot ROM, 1 = SRAM.
    transfer("reset fetch", 0, boot_sel ? 32'h8000_0000 : 0, 0);
    if (!boot_sel) begin
      // The loader in ROM copies the image from flash, then jumps.
      for (int i = 0; i < 2; i++) begin
        transfer("flash read", 0, 32'h1000_5000, 0);
        transfer("SRAM write", 1, 32'h8000_0000 + 4*i, result);
      end
      transfer("entry fetch", 0, 32'h8000_0000, 0);
    end
    $finish;
  end
endmodule
Read 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
ConceptWhy can software not choose the address of its own first instruction?
ConceptWhy is the memory at the reset vector a ROM?
ConceptThe jumper of the strap is moved while the board runs. What picks the start address?
ConceptWhat is the last thing a loader does?
ConceptSRAM returns the right word at the entry. What has been proved?

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.

Practice

Report a problem with this page