Capstone Step: Firmware Bring-Up

A new chip runs its first software. Do not start with the product. Start with four small programs, each one proving one more part.

You Will Be Able To

  • Put bring-up programs in an order where each adds one new part of the chip.
  • Name the suspect parts from the first step that fails.
  • Find why an interrupt never arrives, or arrives only once, by walking its path.
Lesson
S6.16
Time
26 min
Runs on
Your browser
Check
6 questions

The first board of a new chip is on the bench. Someone loads the full product firmware. The console stays dark. The cause could be the clock, the processor, the memory, the UART or ten thousand lines of code.

Before You Start

You need The Boot Flow and Start-Up Code: what runs from reset, and the stack pointer.

You need The Interrupt Path, with claim and completion, and The Enable Bits. You need the machine timer of Timers and DMA.

Five questions this page answers
Question
1What is firmware bring-up, and why does it start small?
2In which order do the four programs run, and what does each one prove?
3Which parts are suspects when step 3 fails after steps 1 and 2 pass?
4How do you find why an interrupt never arrives?
5How do you find why an interrupt arrives once and never again?

The Idea: A Ladder of Small Programs

A chip is built and verified block by block. The first time all the blocks run software together is called bring-up. It happens on the first silicon, and earlier on a simulation or an FPGA model.

The product firmware is the worst first program. It uses every part at once. When it fails, it tells you nothing about which part broke.

So bring-up starts with a program that uses almost nothing: the processor, Boot ROM and one output. Each next program adds one or two parts. Each ends with a check that reads a result.

Then a failure is small news. The steps that passed have cleared the parts they used. The suspects are the parts that the failing step added. That is the whole method of this page.

New Words

New words on this page
WordMeaning
Bring-upThe first runs of software on a new chip or a new board, to find out which parts work.
Bring-up programA small program that uses a few parts of the chip and ends with a check of its result.
Bring-up checklistThe bring-up programs in order, each with its check. A step passes when its check holds.
EchoSending back each received byte, unchanged.
PollingReading a status bit in a loop until it changes.
HangA program that never reaches its end, so its check never runs.

One Picture: The Checklist That Ticks

Four Programs, One Part More Each TimeStep through the picture. Each step is one bring-up program. It lights the parts that the program uses, and its check ticks.
  • Memory
  • Processor
  • Peripheral
  • Clock / reset
  • Bus
  • Control / FSM
Four Programs, One Part More Each TimeStep through the picture. Each step is one bring-up program. It lights the parts that the program uses, and its check ticks. component Boot ROM the program, fromaddress 0 component Processor ap_rv32, after reset component UART0 transmit in step 1,receive in step 3 component ap_clint timer mtime and mtimecmp; acore-local request component SRAM a count, or the bytesof a copy component ap_plic external requests:UART0 is 1, DMA is 5 component DMA a second manager onthe bus instructionsbytes to sendtimer requesta countsource 1external requestcopy, byte by bytesource 5: done
  1. component Boot ROMthe program, from address 0
    • instructions → Processor
  2. component Processorap_rv32, after reset
    • bytes to send → UART0
    • a count → SRAM
  3. component UART0transmit in step 1, receive in step 3
    • source 1 → ap_plic
  4. component ap_clint timermtime and mtimecmp; a core-local request
    • timer request → Processor
  5. component SRAMa count, or the bytes of a copy
  6. component ap_plicexternal requests: UART0 is 1, DMA is 5
    • external request → Processor
  7. component DMAa second manager on the bus
    • copy, byte by byte → SRAM
    • source 5: done → ap_plic
  1. Step 1 ticks: boot to hello

    The processor fetches from Boot ROM and writes two bytes to UART0. "hi" appears. Reset, fetch and one device store all work.

  2. Step 2 ticks: a timer tick

    The timer of ap_clint interrupts three times. The handler counts in SRAM. Trap entry, mret and the enable bits work.

  3. Step 3 ticks: UART echo by interrupt

    Each received byte becomes a request of source 1. ap_plic passes it, and the handler sends the byte back. The external path works, there and back.

  4. Step 4 ticks: a DMA copy

    The DMA copies six bytes inside SRAM, with no help from the processor. Its done request shows as a pending bit in ap_plic.

Read the diagram as text
  • Boot ROM: the program, from address 0
  • Processor: ap_rv32, after reset
  • UART0: transmit in step 1, receive in step 3
  • ap_clint timer: mtime and mtimecmp; a core-local request
  • SRAM: a count, or the bytes of a copy
  • ap_plic: external requests: UART0 is 1, DMA is 5
  • DMA: a second manager on the bus
  • Boot ROM → Processor: instructions
  • Processor → UART0: bytes to send
  • ap_clint timer → Processor: timer request
  • Processor → SRAM: a count
  • UART0 → ap_plic: source 1
  • ap_plic → Processor: external request
  • DMA → SRAM: copy, byte by byte
  • DMA → ap_plic: source 5: done

The bring-up checklist of the lab. Each step uses parts that earlier steps proved, and adds new ones.

Read the four steps again. Steps 2 and 3 both interrupt the processor, by different roads. Step 2 uses the core-local road of ap_clint. Step 3 uses the external road through ap_plic.

The Smallest Example, Worked by Hand

List the parts of the chip, and mark what each program uses. "new" means that the program is the first to use the part.

Which step uses which part
PartStep 1: helloStep 2: tickStep 3: echoStep 4: DMA
Processor and Boot ROMnewusesusesuses
UART0 transmitnewuses
SRAMnewusesuses
Trap entry, mtvec and mretnewuses
ap_clint timer and mie bit 7new
ap_plic and mie bit 11newpending bit only
UART0 receivenew
DMAnew

Worked out from the four programs of the lab. Step 4 reads the pending bit of the DMA in ap_plic. It takes no interrupt.

Now say steps 1 and 2 pass, and step 3 fails. Read down the column of step 3. The parts marked "uses" were cleared by steps 1 and 2. The parts marked "new" are the suspects: ap_plic, mie bit 11 and UART0 receive.

Three suspects, not eight. And each has a register you can read. That is what the order bought.

The Rules

  1. Rule 1: Order the programs so that each adds little.

    The first uses the processor, Boot ROM and one output. Each next one adds one or two parts.

  2. Rule 2: End every program with a check that reads a result.

    A text, a count in SRAM, a copied word. "It did not crash" is not a check.

  3. Rule 3: When a step fails, suspect only what it added.

    The steps that passed have cleared every part they used.

  4. Rule 4: For a silent interrupt, walk the request path in order.

    Device, then ap_plic: priority, enable, threshold. Then mip, mie and mstatus. The first stage that holds 0 is the break.

  5. Rule 5: Check the way back as well.

    One event served and the next one lost means a record was left behind, such as a missing completion.

  6. Rule 6: Keep the programs.

    Run them first on every new version of the chip, in simulation and on silicon.

Sources for These Rules

Mehta, ASIC/SoC Functional Design Verification, section 15.3.5, describes hardware bring-up. It is the first meeting of the chip with system software. The software team prepares a reduced image to test the logic and the interfaces. A fault found there is fixed in hardware or worked around in software.

The order of the steps and rules 1 to 3 and 6 are common practice. No source in our library lists them.

Rules 4 and 5 follow the interrupt path of S6.1 and the enable bits of S6.4. The addresses and source numbers are those of ap_soc.

Now Run It

The lab at the end of this page runs the four programs one after another. Each program is rows of instructions in Boot ROM. A small model of the processor runs them in front of ap_plic and the ap_clint timer, unchanged. UART0 and the DMA are test code.

Recorded from the lab on Icarus: the whole output. Each step prints its check and the result it read.
step 1, boot to hello: PASS (UART0 text "hi")
step 2, a timer tick: PASS (3 ticks counted, 3 interrupts)
step 3, UART echo by interrupt: PASS (UART0 text "abc", 3 interrupts)
step 4, a DMA copy: PASS (destination 64636261 00006665)
bring-up: 4 of 4 steps passed

Each program also runs alone, printing its events. The numbers after "t=" are ticks of the model: one instruction for each tick. Here is step 2.

Recorded from the timer program, run alone.
t=208 | interrupt, machine timer: mcause=80000007 mepc=00000040, next is 00000054
t=214 | mret: back to 00000040
t=408 | interrupt, machine timer: mcause=80000007 mepc=00000044, next is 00000054
t=414 | mret: back to 00000044
t=608 | interrupt, machine timer: mcause=80000007 mepc=0000003c, next is 00000054
t=614 | mret: back to 0000003c
t=620 | the program stops at 00000050
ticks counted: 3, interrupts taken: 3

The handler adds 200 to the old deadline, not to the time it runs. So the interrupts come exactly 200 ticks apart: at 208, 408 and 608. The mepc values differ, because each interrupt lands at another point of the wait loop.

Step 3, for the first byte only.

Recorded from the echo program, run alone: the first six rows.
t=300 | UART0 receives 61 "a"
t=300 | interrupt, external: mcause=8000000b mepc=00000048, next is 00000060
t=300 | PLIC claim returns 1
t=302 | UART0 sends 61 "a"
t=306 | PLIC complete 1
t=307 | mret: back to 00000048

Rule 5 at work: the handler claims, gets 1, sends the byte back, and completes with the same 1. Only then may source 1 ask again. Bytes b and c follow the same six rows.

Recorded from the DMA program, run alone.
t=18 | DMA starts: 6 bytes from 80000100 to 80000200
t=24 | DMA done
t=31 | the program stops at 00000064
destination: 64636261 00006665

Work the copy out by hand. The source holds the bytes 61 to 68. Six of them are copied, and the destination was cleared. The first word holds 61 to 64, lowest byte first. The second holds 65 and 66, then two zeros.

The model stands in for the real processor. A probe ran the same four programs on the real five-stage ap_rv32 of this course, with ap_plic and ap_clint_timer behind its bus. Its final results were equal to the model in all four programs.

From the probe, in step order: the UART0 text, the GPIO word, the checked SRAM words, the stop address and the number of exceptions.
uart=hi gpio=0000 ram= stop=00000018 exceptions=0
uart= gpio=0000 ram=00000003 stop=00000050 exceptions=0
uart=abc gpio=0000 ram=00000003 stop=0000005c exceptions=0
uart= gpio=0000 ram=64636261,00006665 stop=00000064 exceptions=0

These labs leave things out. There is no clock set-up, no flash and no C. A real first program also checks the clocks and the reset cause.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Four small programsOne full firmware image firstA failure names two or three parts. Four programs must be written and kept.
A check that a script readsWatch the board and judgeThe same check runs on every new version. Someone must write the expected result first.
Step 4 polls a pending bitStep 4 takes the DMA interruptStep 4 then needs no handler, and fails only for the DMA. The processor waits and does nothing else.
Programs written in instructionsC with the full start-up codeNothing runs that the step does not need. Bring-up moves to C once start-up code is proved.

The Trap: Two Silent Failures

Back to the dark console. Say the clocks are fine, and step 1 now prints "hi". Two failures on interrupts are common next, and both are silent. Here is the timer program with its write to mie left out.

The timer program, with the write to mie left out. It is stopped after 700 instructions.
still running after 700 instructions, 0 exceptions and 0 interrupts
ticks counted: 0, interrupts taken: 0, mip=00000080 mie=00000000

Walk the path of rule 4. mip is 00000080: bit 7, the timer request, is 1. So the timer works. mie is 0: the enable of bit 7 is missing. The program is not broken. It is waiting for an interrupt that it never allowed.

The second failure passes a quick test. Here is the echo program with the completion left out.

The echo program, with the completion left out, and three bytes sent.
t=300 | UART0 receives 61 "a"
t=300 | interrupt, external: mcause=8000000b mepc=00000048, next is 00000060
t=300 | PLIC claim returns 1
t=302 | UART0 sends 61 "a"
t=306 | mret: back to 00000048
t=600 | UART0 receives 62 "b"
t=900 | UART0 receives 63 "c"
still running after 1499 instructions, 0 exceptions and 1 interrupts
UART0 text: "a", bytes counted: 1, interrupts taken: 1

The first byte is echoed. Bytes b and c arrive, and no interrupt follows. The gateway of source 1 stays closed, because no completion named 1. A test that sends one byte passes. Send three, and count the interrupts.

At Scale

This page4 programs, 8 parts
A product chiphundreds of steps

This section looks ahead. A product chip has hundreds of parts, so its bring-up checklist is longer. The method stays the same.

  1. Before the silicon

    The same programs run first in simulation and on an FPGA model. On first silicon they run again, unchanged.

  2. In C

    Real bring-up programs are C, built with the start-up code of S6.7 and the linker script of S6.17.

  3. Hardware and software together

    A fault found here may be fixed in the chip, or worked around in software. Mehta, section 15.3.5, describes this meeting.

  4. The watchdog comes last

    A watchdog that resets the chip during a slow step hides every other fault. The next lesson arms it as step 5.

  5. The first tests

    The bring-up programs become the first tests of every new version of the chip. A verification track grows them into register tests and interrupt test plans.

Try It

Run the lab. For each step, name the part of the chip it proves for the first time.

In program 2, the row at 02c puts 128 in x5 before the write to mie. Change 128 to 8. Predict which step fails, then run.

Put it back. In program 3, delete the completion: the store of x28 to offset 4 of x19. Compare the step 3 line with the trap on this page.

Add a fourth byte to step 3: board_sends(1200, "d"). Predict whether the step still passes, and why.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: step 1, boot to hello: PASS (UART0 text "hi") step 2, a timer tick: PASS (3 ticks counted, 3 interrupts) step 3, UART echo by interrupt: PASS (UART0 text "abc", 3 interrupts) step 4, a DMA copy: PASS (destination 64636261 00006665) bring-up: 4 of 4 steps passed
Read included file: ap_plic.sv
// One machine context, synchronous level sources. Register-side interface:
// commit pulses once at a successful word transfer's data edge.
module ap_plic #(parameter integer SOURCES = 16) (
  input logic clk, reset_n, commit, write_en,
  input logic [21:0] offset,
  input logic [31:0] wdata,
  input logic [SOURCES:1] level_irq,
  output logic [31:0] rdata,
  output logic notify_irq
);
  localparam ENABLE = 'h2000, THRESHOLD = 'h200000, CLAIM = 'h200004;
  logic [2:0] weight [1:SOURCES];
  logic [2:0] threshold, best;
  logic [SOURCES:1] pending, busy, enabled;
  integer winner;

  always_comb begin
    best = 0;
    winner = 0;
    for (int id = 1; id <= SOURCES; id++) begin
      if (pending[id] && enabled[id] && weight[id] > best) begin
        best = weight[id];
        winner = id;
      end
    end
    notify_irq = best > threshold;
    rdata = 0;
    for (int id = 1; id <= SOURCES; id++) begin
      if (offset == 4*id) rdata = weight[id];
      if (offset == 'h1000 + 4*(id/32)) rdata[id%32] = pending[id];
      if (offset == ENABLE + 4*(id/32)) rdata[id%32] = enabled[id];
    end
    case (offset)
      THRESHOLD: rdata = threshold;
      CLAIM: rdata = winner;
    endcase
  end

  always_ff @(posedge clk) begin
    if (!reset_n) begin
      pending <= '0;
      busy <= '0;
      enabled <= '0;
      threshold <= 0;
      for (int id = 1; id <= SOURCES; id++) weight[id] <= 0;
    end else begin
      for (int id = 1; id <= SOURCES; id++) begin
        if (level_irq[id] && !busy[id]) begin
          pending[id] <= 1;
          busy[id] <= 1;
        end
        if (commit && write_en && offset == 4*id) weight[id] <= wdata[2:0];
        if (commit && write_en && offset == ENABLE + 4*(id/32))
          enabled[id] <= wdata[id%32];
        if (commit && offset == CLAIM) begin
          if (!write_en && winner == id) pending[id] <= 0;
          if (write_en && enabled[id] && wdata == id && busy[id] && !pending[id]) busy[id] <= 0;
        end
      end
      if (commit && write_en && offset == THRESHOLD) threshold <= wdata[2:0];
    end
  end
endmodule
Read included file: ap_clint_timer.sv
// Timer slice of ap_soc's local interrupt block. One hart, one timebase tick
// per clk and a registered interrupt output. Word strobes arrive at commitment.
module ap_clint_timer #(parameter logic [63:0] RESET_COMPARE = '1) (
  input logic clk, reset_n, write_cmp_low, write_cmp_high,
  input logic [31:0] wdata,
  output logic [63:0] mtime, mtimecmp,
  output logic mtip
);
  always_ff @(posedge clk) begin
    if (!reset_n) begin
      mtime <= 0;
      mtimecmp <= RESET_COMPARE;
      mtip <= 0;
    end else begin
      mtime <= mtime + 1'b1;
      if (write_cmp_low) mtimecmp[31:0] <= wdata;
      if (write_cmp_high) mtimecmp[63:32] <= wdata;
      mtip <= mtime >= mtimecmp;
    end
  end
endmodule

Check Yourself

Predict the outputProgram 4 now asks the DMA for 4 bytes. The check of step 4 still expects 6. What is the last line the lab prints?
// Firmware bring-up: four programs, one after another. Each proves one more part of the chip.
// A program is a task of rows: i(kind, a, b, c). The comment beside a row is its address and its assembly.
module tb;
  // ---- Program 1: boot to hello ----
  task automatic program_1();
    i(LUI,   2,  0,  'h80010);   // 000: lui x2, 0x80010      the stack pointer
    i(LUI,   8,  0,  'h10000);   // 004: lui x8, 0x10000      x8: UART0
    i(ADDI,  10, 0,  104);       // 008: addi x10, x0, 104      the letter h
    i(JAL,   1,  0,  'h1c);      // 00c: jal x1, 0x1c
    i(ADDI,  10, 0,  105);       // 010: addi x10, x0, 105      the letter i
    i(JAL,   1,  0,  'h1c);      // 014: jal x1, 0x1c
    i(JAL,   0,  0,  'h18);      // 018: jal x0, 0x18
    // putc: wait for room, then write the byte in x10
    i(LW,    5,  8,  4);         // 01c: lw x5, 4(x8)      STATUS
    i(ANDI,  5,  5,  1);         // 020: andi x5, x5, 1      bit 0: ROOM
    i(BEQ,   5,  0,  'h1c);      // 024: beq x5, x0, 0x1c
    i(SW,    10, 8,  0);         // 028: sw x10, 0(x8)      TXDATA
    i(JALR,  0,  1,  0);         // 02c: jalr x0, 0(x1)      return
  endtask

  // ---- Program 2: a timer tick ----
  task automatic program_2();
    i(LUI,   2,  0,  'h80010);   // 000: lui x2, 0x80010      the stack pointer
    i(ADDI,  5,  0,  84);        // 004: addi x5, x0, 84
    i(CSRW,  5,  0,  MTVEC);     // 008: csrw mtvec, x5      mtvec: the handler
    i(LUI,   8,  0,  'h80000);   // 00c: lui x8, 0x80000      x8: SRAM, where the tick count is
    i(SW,    0,  8,  0);         // 010: sw x0, 0(x8)      count = 0
    i(LUI,   9,  0,  'h2004);    // 014: lui x9, 0x2004      x9: mtimecmp
    i(LUI,   18, 0,  'h200c);    // 018: lui x18, 0x200c
    i(LW,    5,  18, -8);        // 01c: lw x5, -8(x18)      mtime
    i(ADDI,  5,  5,  200);       // 020: addi x5, x5, 200
    i(SW,    5,  9,  0);         // 024: sw x5, 0(x9)      the first deadline: now + 200
    i(SW,    0,  9,  4);         // 028: sw x0, 4(x9)      the high word of mtimecmp
    i(ADDI,  5,  0,  128);       // 02c: addi x5, x0, 128
    i(CSRS,  5,  0,  MIE);       // 030: csrs mie, x5      mie: the timer enable, bit 7
    i(ADDI,  5,  0,  8);         // 034: addi x5, x0, 8
    i(CSRS,  5,  0,  MSTATUS);   // 038: csrs mstatus, x5      mstatus: MIE, the global enable
    i(LW,    6,  8,  0);         // 03c: lw x6, 0(x8)
    i(ADDI,  7,  0,  3);         // 040: addi x7, x0, 3
    i(BLT,   6,  7,  'h3c);      // 044: blt x6, x7, 0x3c      wait for 3 ticks
    i(ADDI,  5,  0,  8);         // 048: addi x5, x0, 8
    i(CSRC,  5,  0,  MSTATUS);   // 04c: csrc mstatus, x5      interrupts off
    i(JAL,   0,  0,  'h50);      // 050: jal x0, 0x50
    // the handler: move the deadline, count the tick. It uses x28 only, which main does not use.
    i(LW,    28, 9,  0);         // 054: lw x28, 0(x9)
    i(ADDI,  28, 28, 200);       // 058: addi x28, x28, 200
    i(SW,    28, 9,  0);         // 05c: sw x28, 0(x9)      the next deadline; this drops the request
    i(LW,    28, 8,  0);         // 060: lw x28, 0(x8)
    i(ADDI,  28, 28, 1);         // 064: addi x28, x28, 1
    i(SW,    28, 8,  0);         // 068: sw x28, 0(x8)      count = count + 1
    i(MRET,  0,  0,  0);         // 06c: mret
  endtask

  // ---- Program 3: UART echo by interrupt ----
  task automatic program_3();
    i(LUI,   2,  0,  'h80010);   // 000: lui x2, 0x80010      the stack pointer
    i(ADDI,  5,  0,  96);        // 004: addi x5, x0, 96
    i(CSRW,  5,  0,  MTVEC);     // 008: csrw mtvec, x5      mtvec: the handler
    i(LUI,   8,  0,  'h80000);   // 00c: lui x8, 0x80000      x8: SRAM, where the byte count is
    i(SW,    0,  8,  0);         // 010: sw x0, 0(x8)      count = 0
    i(LUI,   9,  0,  'hc000);    // 014: lui x9, 0xc000      x9: ap_plic
    i(ADDI,  5,  0,  1);         // 018: addi x5, x0, 1
    i(SW,    5,  9,  4);         // 01c: sw x5, 4(x9)      priority of source 1 (UART0) = 1
    i(LUI,   18, 0,  'hc002);    // 020: lui x18, 0xc002
    i(ADDI,  5,  0,  2);         // 024: addi x5, x0, 2
    i(SW,    5,  18, 0);         // 028: sw x5, 0(x18)      enable bit 1
    i(LUI,   19, 0,  'hc200);    // 02c: lui x19, 0xc200      x19: threshold, and claim at offset 4
    i(LUI,   20, 0,  'h10000);   // 030: lui x20, 0x10000      x20: UART0
    i(LUI,   5,  0,  'h1);       // 034: lui x5, 0x1
    i(ADDI,  5,  5,  -2048);     // 038: addi x5, x5, -2048      x5 = 0x800
    i(CSRS,  5,  0,  MIE);       // 03c: csrs mie, x5      mie: the external enable, bit 11
    i(ADDI,  5,  0,  8);         // 040: addi x5, x0, 8
    i(CSRS,  5,  0,  MSTATUS);   // 044: csrs mstatus, x5      mstatus: MIE
    i(LW,    6,  8,  0);         // 048: lw x6, 0(x8)
    i(ADDI,  7,  0,  3);         // 04c: addi x7, x0, 3
    i(BLT,   6,  7,  'h48);      // 050: blt x6, x7, 0x48      wait for 3 bytes
    i(ADDI,  5,  0,  8);         // 054: addi x5, x0, 8
    i(CSRC,  5,  0,  MSTATUS);   // 058: csrc mstatus, x5      interrupts off
    i(JAL,   0,  0,  'h5c);      // 05c: jal x0, 0x5c
    // the handler: claim, read the byte, send it back, count it, complete. It uses x28 to x30 only.
    i(LW,    28, 19, 4);         // 060: lw x28, 4(x19)      claim: which source?
    i(LW,    29, 20, 12);        // 064: lw x29, 12(x20)      RXDATA: the read removes the byte
    i(SW,    29, 20, 0);         // 068: sw x29, 0(x20)      TXDATA
    i(LW,    30, 8,  0);         // 06c: lw x30, 0(x8)
    i(ADDI,  30, 30, 1);         // 070: addi x30, x30, 1
    i(SW,    30, 8,  0);         // 074: sw x30, 0(x8)      count = count + 1
    i(SW,    28, 19, 4);         // 078: sw x28, 4(x19)      complete: the same number goes back
    i(MRET,  0,  0,  0);         // 07c: mret
  endtask

  // ---- Program 4: a DMA copy ----
  task automatic program_4();
    i(LUI,   2,  0,  'h80010);   // 000: lui x2, 0x80010      the stack pointer
    i(LUI,   8,  0,  'h80000);   // 004: lui x8, 0x80000      x8: SRAM
    i(LUI,   5,  0,  'h64636);   // 008: lui x5, 0x64636
    i(ADDI,  5,  5,  609);       // 00c: addi x5, x5, 609
    i(SW,    5,  8,  256);       // 010: sw x5, 256(x8)      source bytes 61 62 63 64
    i(LUI,   5,  0,  'h68676);   // 014: lui x5, 0x68676
    i(ADDI,  5,  5,  1637);      // 018: addi x5, x5, 1637
    i(SW,    5,  8,  260);       // 01c: sw x5, 260(x8)      source bytes 65 66 67 68
    i(SW,    0,  8,  512);       // 020: sw x0, 512(x8)      clear the destination
    i(SW,    0,  8,  516);       // 024: sw x0, 516(x8)
    i(LUI,   9,  0,  'h10006);   // 028: lui x9, 0x10006      x9: DMA
    i(ADDI,  5,  8,  256);       // 02c: addi x5, x8, 256
    i(SW,    5,  9,  0);         // 030: sw x5, 0(x9)      SRC
    i(ADDI,  5,  8,  512);       // 034: addi x5, x8, 512
    i(SW,    5,  9,  4);         // 038: sw x5, 4(x9)      DST
    i(ADDI,  5,  0,  4);         // 03c: addi x5, x0, 4
    i(SW,    5,  9,  8);         // 040: sw x5, 8(x9)      LEN: 4 bytes
    i(ADDI,  5,  0,  1);         // 044: addi x5, x0, 1
    i(SW,    5,  9,  12);        // 048: sw x5, 12(x9)      START, written last
    i(LUI,   18, 0,  'hc001);    // 04c: lui x18, 0xc001      x18: the pending bits of ap_plic
    i(LW,    6,  18, 0);         // 050: lw x6, 0(x18)
    i(ANDI,  6,  6,  32);        // 054: andi x6, x6, 32      bit 5: DMA
    i(BEQ,   6,  0,  'h50);      // 058: beq x6, x0, 0x50      wait until the DMA is done
    i(LW,    10, 8,  512);       // 05c: lw x10, 512(x8)      read the copy back
    i(LW,    11, 8,  516);       // 060: lw x11, 516(x8)
    i(JAL,   0,  0,  'h64);      // 064: jal x0, 0x64
  endtask

  // ---- From here down: a small model of the processor and of three devices, as test code. ----
  // The interrupt controller and the machine timer are the blocks of ap_soc themselves.
  localparam int LUI = 0, ADDI = 1, ANDI = 2, ORI = 3, SLLI = 4, SRLI = 5, ADD = 6, SUB = 7, AND = 8, OR = 9, LW = 10, SW = 11,
                 BEQ = 12, BNE = 13, BLT = 14, BGE = 15, JAL = 16, JALR = 17, CSRR = 18, CSRW = 19, CSRS = 20, CSRC = 21, MRET = 22, WORD = 23;
  localparam int MSTATUS = 'h300, MIE = 'h304, MTVEC = 'h305, MSCRATCH = 'h340, MEPC = 'h341, MCAUSE = 'h342, MIP = 'h344;
  int kind [0:1023], fa [0:1023], fb [0:1023];   // the Boot ROM: one row for each word
  logic [31:0] fc [0:1023];
  int here = 0;                                   // the address of the next row
  task automatic at(input int address);           // the rows that follow start at this address
    here = address;
  endtask
  task automatic i(input int k, input int a, input int b, input logic [31:0] c);   // add one row
    if (here < 4096) begin kind[here >> 2] = k; fa[here >> 2] = a; fb[here >> 2] = b; fc[here >> 2] = c; end
    here += 4;
  endtask
  logic        clk = 0;
  logic [31:0] x [0:31];             // the 32 registers
  logic [31:0] pc;                   // the address of the next instruction
  logic [31:0] ram [0:16383];        // SRAM: 64 KiB from 8000_0000. Nothing is in it at power-on
  logic [31:0] mstatus, mie, mtvec, mscratch, mepc, mcause;   // the machine CSRs of this model
  int          steps, traps, taken;  // instructions, exceptions, interrupts
  bit          stopped;
  localparam int SHOW = 0;            // 2: print every instruction. 1: print events only. 0: print nothing
  task automatic say(input string text);
    if (SHOW != 0) $display("t=%0d | %s", ticks, text);
  endtask
  // ---- UART0 (test code). TXDATA 00, STATUS 04 (bit 0 ROOM, bit 1 RX_READY), RXDATA 0c ----
  string       uart_text = "";                 // every byte that was sent, as text
  int          uart_wait = 0;                  // after a byte: 12 ticks with no room (a lab number)
  logic [7:0]  rx_queue [0:3];                 // received bytes that software has not read yet
  int          rx_count = 0;
  logic [7:0]  rx_plan [0:7];                  // the bytes the board will send, and at which tick
  int          rx_tick [0:7], rx_planned = 0;
  wire         rx_ready = (rx_count != 0);     // UART0 asks for an interrupt while a received byte waits
  // ---- GPIO (test code). OUT 08, TGL 14 ----
  logic [15:0] gpio_out = 0;
  int          gpio_changes = 0;
  // ---- DMA (test code). SRC 00, DST 04, LEN 08, START 0c. It moves one byte of SRAM in each tick. ----
  logic [31:0] dma_src = 0, dma_dst = 0, dma_len = 0;
  int          dma_left = 0;
  bit          dma_done = 0;                   // its interrupt request: 1 from the last byte until the next start
  int          ticks = 0;

  function automatic logic [31:0] tc_peek(input logic [31:0] ad);
    case (ad)
      32'h1000_0004: return {30'b0, rx_ready, (uart_wait == 0)};
      32'h1000_000c: return {24'b0, rx_queue[0]};
      32'h1000_1008: return {16'b0, gpio_out};
      default:       return 32'h0;
    endcase
  endfunction
  function automatic bit tc_exists(input bit wr, input logic [31:0] ad);   // 1: a register is at this address
    case (ad)
      32'h1000_0000, 32'h1000_1014, 32'h1000_6000, 32'h1000_6004, 32'h1000_6008, 32'h1000_600c: return wr;
      32'h1000_0004, 32'h1000_000c: return !wr;
      32'h1000_1008: return 1;
      default: return 0;
    endcase
  endfunction
  task automatic tc_read(input logic [31:0] ad);
    if (ad == 32'h1000_000c && rx_count != 0) begin        // reading RXDATA removes the oldest byte
      for (int n = 0; n < 3; n++) rx_queue[n] = rx_queue[n + 1];
      rx_count--;
    end
  endtask
  task automatic tc_write(input logic [31:0] ad, input logic [31:0] wd);
    case (ad)
      32'h1000_0000: if (uart_wait == 0) begin             // a byte written with no room is lost
        uart_text = {uart_text, $sformatf("%c", wd[7:0])};  uart_wait = 12;
        say($sformatf("UART0 sends %02h \"%c\"", wd[7:0], wd[7:0]));
      end else say($sformatf("UART0 has no room: %02h is lost", wd[7:0]));
      32'h1000_1008: begin gpio_out = wd[15:0];  gpio_changes++;  say($sformatf("GPIO OUT = %04h", gpio_out)); end
      32'h1000_1014: begin gpio_out = gpio_out ^ wd[15:0];  gpio_changes++;  say($sformatf("GPIO OUT = %04h", gpio_out)); end
      32'h1000_6000: dma_src = wd;
      32'h1000_6004: dma_dst = wd;
      32'h1000_6008: dma_len = wd;
      32'h1000_600c: if (wd[0]) begin
        dma_left = dma_len;  dma_done = 0;
        say($sformatf("DMA starts: %0d bytes from %08h to %08h", dma_len, dma_src, dma_dst));
      end
      default: ;
    endcase
  endtask
  task automatic tc_tick();
    ticks++;
    if (uart_wait != 0) uart_wait--;
    for (int n = 0; n < rx_planned; n++) if (rx_tick[n] == ticks && rx_count < 4) begin
      rx_queue[rx_count] = rx_plan[n];  rx_count++;
      say($sformatf("UART0 receives %02h \"%c\"", rx_plan[n], rx_plan[n]));
    end
    if (dma_left != 0) begin                               // one byte: read it from SRAM, write it to SRAM
      ram[dma_dst[15:2]][8 * dma_dst[1:0] +: 8] = ram[dma_src[15:2]][8 * dma_src[1:0] +: 8];
      dma_src++;  dma_dst++;  dma_left--;
      if (dma_left == 0) begin dma_done = 1;  say("DMA done"); end
    end
  endtask
  task automatic board_sends(input int tick, input logic [7:0] value);   // plan one received byte
    rx_plan[rx_planned] = value;  rx_tick[rx_planned] = tick;  rx_planned++;
  endtask
  // Interrupt sources of ap_soc: 1 UART0, 2 GPIO, 3 timer and PWM, 4 SPI, 5 DMA, 6 watchdog, 7 to 16 spare.
  wire         wdg_warn;                       // the early warning of the watchdog; tied to 0 where a lab has none
  wire [16:1] sources = {10'b0, wdg_warn, dma_done, 3'b0, rx_ready};
  // ---- The two chip blocks of ap_soc, unchanged: the interrupt controller and the machine timer ----
  logic        reset_n = 0, plic_commit = 0, plic_write = 0, cmp_low = 0, cmp_high = 0;
  logic [21:0] plic_offset = 0;
  logic [31:0] dev_wdata = 0;
  wire  [31:0] plic_rdata;
  wire  [63:0] mtime, mtimecmp;
  wire         irq_external, irq_timer;
  ap_plic plic (.clk, .reset_n, .commit(plic_commit), .write_en(plic_write), .offset(plic_offset), .wdata(dev_wdata),
                .level_irq(sources), .rdata(plic_rdata), .notify_irq(irq_external));
  ap_clint_timer clint (.clk, .reset_n, .write_cmp_low(cmp_low), .write_cmp_high(cmp_high), .wdata(dev_wdata),
                        .mtime, .mtimecmp, .mtip(irq_timer));
  assign wdg_warn = 1'b0;                      // no watchdog in this lab
  wire [31:0] mip = {20'b0, irq_external, 3'b0, irq_timer, 7'b0};   // bit 11: external. bit 7: machine timer

  function automatic string cn(input logic [11:0] a);   // the name of a CSR
    case (a)
      MSTATUS: return "mstatus";  MIE: return "mie";  MTVEC: return "mtvec";  MSCRATCH: return "mscratch";
      MEPC: return "mepc";  MCAUSE: return "mcause";  MIP: return "mip";
      default: return "?";
    endcase
  endfunction
  function automatic logic [31:0] csr_value(input logic [11:0] a);
    case (a)
      MSTATUS: return mstatus;  MIE: return mie;  MTVEC: return mtvec;  MSCRATCH: return mscratch;
      MEPC: return mepc;  MCAUSE: return mcause;
      default: return mip;
    endcase
  endfunction
  task automatic csr_store(input logic [11:0] a, input logic [31:0] v);   // each CSR keeps only its writable bits
    case (a)
      MSTATUS:  begin mstatus[3] = v[3];  mstatus[7] = v[7]; end           // MIE and MPIE
      MIE:      mie = v & 32'h0000_0888;                                   // external, timer and software enables
      MTVEC:    mtvec = v & 32'hffff_fffd;
      MSCRATCH: mscratch = v;
      MEPC:     mepc = v & 32'hffff_fffc;
      MCAUSE:   mcause = v;
      default: ;                                                           // mip is read-only
    endcase
  endtask
  // The entry of a trap, for an exception or an interrupt: what the processor does by itself.
  task automatic enter(input logic [31:0] cause, input string text);
    mepc = pc & 32'hffff_fffc;  mcause = cause;
    mstatus[7] = mstatus[3];  mstatus[3] = 1'b0;                           // MPIE takes MIE, MIE closes
    say($sformatf("%s: mcause=%08h mepc=%08h, next is %08h", text, cause, mepc, mtvec & 32'hffff_fffc));
    pc = mtvec & 32'hffff_fffc;
  endtask
  // Start one read or write of a word. The value of a read is in bus_rdata after half a tick.
  logic [31:0] bus_rdata;
  bit          bus_fault;
  task automatic bus(input bit wr, input logic [31:0] ad, input logic [31:0] wd);
    bus_fault = 0;  bus_rdata = 0;
    if (ad[31:12] == 20'h0) begin                                          // Boot ROM: data words can be read
      if (wr) bus_fault = 1;
      else bus_rdata = (kind[ad[11:2]] == WORD) ? fc[ad[11:2]] : 32'h0;
    end
    else if (ad[31:16] == 16'h8000) begin                                  // SRAM
      if (wr) ram[ad[15:2]] = wd;
      else bus_rdata = ram[ad[15:2]];
    end
    else if (ad[31:22] == 10'h030) begin                                   // ap_plic, from 0c00_0000
      plic_offset = ad[21:0];  plic_write = wr;  dev_wdata = wd;  plic_commit = 1;
    end
    else if (ad == 32'h0200_4000 || ad == 32'h0200_4004) begin              // ap_clint: mtimecmp
      if (wr) begin dev_wdata = wd;  cmp_low = !ad[2];  cmp_high = ad[2]; end
      else bus_rdata = ad[2] ? mtimecmp[63:32] : mtimecmp[31:0];
    end
    else if (ad == 32'h0200_bff8 || ad == 32'h0200_bffc) begin              // ap_clint: mtime, read only here
      if (wr) bus_fault = 1;
      else bus_rdata = ad[2] ? mtime[63:32] : mtime[31:0];
    end
    else if (tc_exists(wr, ad)) begin                                      // UART0, GPIO, DMA
      if (wr) tc_write(ad, wd);
      else begin bus_rdata = tc_peek(ad);  tc_read(ad); end
    end
    else bus_fault = 1;                                                    // nothing answers at this address
  endtask
  // One tick: one instruction, then one rising clock edge.
  task automatic step();
    int k, a, b;
    logic [31:0] c, r1, r2, res, next, ad;
    string text, effect;
    bit writes, take, done, access;
    // An interrupt is taken between two instructions: MIE at 1, and a request that is pending and enabled.
    if (mstatus[3] && (mie & mip) != 0) begin
      if (mie[11] && mip[11]) enter(32'h8000_000b, "interrupt, external");
      else                    enter(32'h8000_0007, "interrupt, machine timer");
      taken++;
    end
    done = 0;  writes = 1;  access = 0;  res = 0;  effect = "";  next = pc + 4;  text = "";
    if (pc >= 32'h1000) begin enter(1, "exception, no instruction at this address");  traps++;  done = 1;  k = WORD;  a = 0;  b = 0;  c = 0; end
    else begin k = kind[pc >> 2];  a = fa[pc >> 2];  b = fb[pc >> 2];  c = fc[pc >> 2]; end
    r1 = x[b[4:0]];  r2 = x[c[4:0]];
    // An immediate has 12 bits with a sign (20 bits in lui). A row outside that range is no instruction.
    if (!done && ((k == LUI && c[31:20] != 0) || ((k == ADDI || k == ANDI || k == ORI || k == LW || k == SW || k == JALR) && !($signed(c) >= -2048 && $signed(c) <= 2047)))) begin
      enter(2, $sformatf("exception, immediate out of range at %08h", pc));  traps++;  done = 1;
    end
    if (!done) case (k)
      LUI:  begin res = c << 12;  text = $sformatf("lui x%0d, 0x%0h", a, c); end
      ADDI: begin res = r1 + c;          text = $sformatf("addi x%0d, x%0d, %0d", a, b, $signed(c)); end
      ANDI: begin res = r1 & c;          text = $sformatf("andi x%0d, x%0d, %0d", a, b, $signed(c)); end
      ORI:  begin res = r1 | c;          text = $sformatf("ori x%0d, x%0d, %0d", a, b, $signed(c)); end
      SLLI: begin res = r1 << c[4:0];    text = $sformatf("slli x%0d, x%0d, %0d", a, b, c); end
      SRLI: begin res = r1 >> c[4:0];    text = $sformatf("srli x%0d, x%0d, %0d", a, b, c); end
      ADD:  begin res = r1 + r2;         text = $sformatf("add x%0d, x%0d, x%0d", a, b, c[4:0]); end
      SUB:  begin res = r1 - r2;         text = $sformatf("sub x%0d, x%0d, x%0d", a, b, c[4:0]); end
      AND:  begin res = r1 & r2;         text = $sformatf("and x%0d, x%0d, x%0d", a, b, c[4:0]); end
      OR:   begin res = r1 | r2;         text = $sformatf("or x%0d, x%0d, x%0d", a, b, c[4:0]); end
      LW, SW: begin
        ad = r1 + c;  access = 1;
        text = $sformatf("%s x%0d, %0d(x%0d)", (k == LW) ? "lw" : "sw", a, $signed(c), b);
        bus(k == SW, ad, x[a[4:0]]);
        if (k == SW) begin writes = 0;  effect = $sformatf("mem[%08h] = %08h", ad, x[a[4:0]]); end
      end
      BEQ, BNE, BLT, BGE: begin
        r1 = x[a[4:0]];  r2 = x[b[4:0]];  writes = 0;
        case (k)
          BEQ: take = (r1 == r2);
          BNE: take = (r1 != r2);
          BLT: take = ($signed(r1) < $signed(r2));
          default: take = ($signed(r1) >= $signed(r2));
        endcase
        text = $sformatf("%s x%0d, x%0d, 0x%0h", (k == BEQ) ? "beq" : (k == BNE) ? "bne" : (k == BLT) ? "blt" : "bge", a, b, c);
        if (take) begin next = c;  effect = $sformatf("taken: next is %08h", next); end
        else effect = "not taken";
      end
      JAL, JALR: begin
        res = pc + 4;
        if (k == JAL) begin next = c;  text = $sformatf("jal x%0d, 0x%0h", a, c); end
        else begin next = (r1 + c) & ~32'd1;  text = $sformatf("jalr x%0d, %0d(x%0d)", a, $signed(c), b); end
        if (k == JAL && next == pc) begin
          if (SHOW == 2) $display("%08h | %s | the program stops here", pc, text);
          else say($sformatf("the program stops at %08h", pc));
          stopped = 1;  done = 1;
        end
      end
      CSRR: begin res = csr_value(c[11:0]);  text = $sformatf("csrr x%0d, %s", a, cn(c[11:0])); end
      CSRW, CSRS, CSRC: begin
        writes = 0;
        case (k)
          CSRW:    csr_store(c[11:0], x[a[4:0]]);
          CSRS:    csr_store(c[11:0], csr_value(c[11:0]) | x[a[4:0]]);
          default: csr_store(c[11:0], csr_value(c[11:0]) & ~x[a[4:0]]);
        endcase
        text = $sformatf("%s %s, x%0d", (k == CSRW) ? "csrw" : (k == CSRS) ? "csrs" : "csrc", cn(c[11:0]), a);
        effect = $sformatf("%s = %08h", cn(c[11:0]), csr_value(c[11:0]));
      end
      MRET: begin
        writes = 0;  next = mepc;  text = "mret";
        mstatus[3] = mstatus[7];  mstatus[7] = 1'b1;                       // MIE takes MPIE back
        effect = $sformatf("next is %08h, MIE=%0d", next, mstatus[3]);
        if (SHOW == 1) say($sformatf("mret: back to %08h", next));
      end
      default: begin enter(2, $sformatf("exception, illegal instruction %08h at %08h", c, pc));  traps++;  done = 1; end
    endcase
    #1;                                                                    // half a tick: the devices answer
    if (access && !done) begin
      if (plic_commit && !plic_write) begin
        bus_rdata = plic_rdata;
        if (plic_offset == 22'h20_0004) say($sformatf("PLIC claim returns %0d", plic_rdata));
      end
      if (plic_commit && plic_write && plic_offset == 22'h20_0004) say($sformatf("PLIC complete %0d", dev_wdata));
      if (bus_fault) begin
        enter((k == LW) ? 5 : 7, $sformatf("exception, no memory at %08h for %s at %08h", ad, (k == LW) ? "a load" : "a store", pc));
        traps++;  done = 1;
      end
      else if (k == LW) begin res = bus_rdata;  effect = $sformatf(" from mem[%08h]", ad); end
    end
    if (!done) begin
      if (writes) begin
        if (a[4:0] != 0) x[a[4:0]] = res;                                  // register 0 is never written
        effect = {$sformatf("x%0d = %08h", a, x[a[4:0]]), effect};
        if (k == JAL || k == JALR) effect = {effect, $sformatf(", next is %08h", next)};
      end
      if (SHOW == 2) $display("%08h | %s | %s", pc, text, effect);
      pc = next;  steps++;
    end
    tc_tick();
    clk = 1;  #1;  clk = 0;                                                // the rising edge: registers of the chip blocks load
    plic_commit = 0;  plic_write = 0;  cmp_low = 0;  cmp_high = 0;
  endtask
  task automatic power_on();
    for (int n = 0; n < 32; n++) x[n] = 0;
    for (int n = 0; n < 16384; n++) ram[n] = 'x;                           // SRAM holds nothing known
    mstatus = 32'h0000_1800;  mie = 0;  mtvec = 0;  mscratch = 0;  mepc = 0;  mcause = 0;   // the values after reset
    pc = 0;  steps = 0;  traps = 0;  taken = 0;  stopped = 0;
    uart_text = "";  uart_wait = 0;  rx_count = 0;  rx_planned = 0;  gpio_out = 0;  gpio_changes = 0;
    dma_left = 0;  dma_done = 0;  ticks = 0;
    reset_n = 0;  #1 clk = 1;  #1 clk = 0;  reset_n = 1;                   // one clock edge in reset for the chip blocks
  endtask
  task automatic run();
    while (!stopped && steps + traps + taken < 2500) step();
  endtask
  task automatic load(input int n);        // put program n in the Boot ROM
    for (int m = 0; m < 1024; m++) begin kind[m] = WORD; fa[m] = 0; fb[m] = 0; fc[m] = 0; end
    here = 0;
    case (n)
      1: program_1();
      2: program_2();
      3: program_3();
      4: program_4();
      default: ;
    endcase
  endtask
  int passed = 0;
  task automatic report(input int n, input string name, input bit ok, input string seen);
    $display("step %0d, %s: %s (%s)", n, name, ok ? "PASS" : "FAIL", seen);
    if (ok) passed++;
  endtask
  initial begin
    load(1);  power_on();
    run();
    report(1, "boot to hello", stopped && traps == 0 && uart_text == "hi", $sformatf("UART0 text \"%s\"", uart_text));
    load(2);  power_on();
    run();
    report(2, "a timer tick", stopped && traps == 0 && ram[0] == 3 && taken == 3, $sformatf("%0d ticks counted, %0d interrupts", ram[0], taken));
    load(3);  power_on();
      board_sends(300, "a");
      board_sends(600, "b");
      board_sends(900, "c");
    run();
    report(3, "UART echo by interrupt", stopped && traps == 0 && uart_text == "abc" && ram[0] == 3, $sformatf("UART0 text \"%s\", %0d interrupts", uart_text, taken));
    load(4);  power_on();
    run();
    report(4, "a DMA copy", stopped && traps == 0 && ram[128] == 32'h6463_6261 && ram[129] == 32'h0000_6665, $sformatf("destination %08h %08h", ram[128], ram[129]));
    $display("bring-up: %0d of 4 steps passed", passed);
    $finish;
  end
endmodule
Read included file: ap_plic.sv
// One machine context, synchronous level sources. Register-side interface:
// commit pulses once at a successful word transfer's data edge.
module ap_plic #(parameter integer SOURCES = 16) (
  input logic clk, reset_n, commit, write_en,
  input logic [21:0] offset,
  input logic [31:0] wdata,
  input logic [SOURCES:1] level_irq,
  output logic [31:0] rdata,
  output logic notify_irq
);
  localparam ENABLE = 'h2000, THRESHOLD = 'h200000, CLAIM = 'h200004;
  logic [2:0] weight [1:SOURCES];
  logic [2:0] threshold, best;
  logic [SOURCES:1] pending, busy, enabled;
  integer winner;

  always_comb begin
    best = 0;
    winner = 0;
    for (int id = 1; id <= SOURCES; id++) begin
      if (pending[id] && enabled[id] && weight[id] > best) begin
        best = weight[id];
        winner = id;
      end
    end
    notify_irq = best > threshold;
    rdata = 0;
    for (int id = 1; id <= SOURCES; id++) begin
      if (offset == 4*id) rdata = weight[id];
      if (offset == 'h1000 + 4*(id/32)) rdata[id%32] = pending[id];
      if (offset == ENABLE + 4*(id/32)) rdata[id%32] = enabled[id];
    end
    case (offset)
      THRESHOLD: rdata = threshold;
      CLAIM: rdata = winner;
    endcase
  end

  always_ff @(posedge clk) begin
    if (!reset_n) begin
      pending <= '0;
      busy <= '0;
      enabled <= '0;
      threshold <= 0;
      for (int id = 1; id <= SOURCES; id++) weight[id] <= 0;
    end else begin
      for (int id = 1; id <= SOURCES; id++) begin
        if (level_irq[id] && !busy[id]) begin
          pending[id] <= 1;
          busy[id] <= 1;
        end
        if (commit && write_en && offset == 4*id) weight[id] <= wdata[2:0];
        if (commit && write_en && offset == ENABLE + 4*(id/32))
          enabled[id] <= wdata[id%32];
        if (commit && offset == CLAIM) begin
          if (!write_en && winner == id) pending[id] <= 0;
          if (write_en && enabled[id] && wdata == id && busy[id] && !pending[id]) busy[id] <= 0;
        end
      end
      if (commit && write_en && offset == THRESHOLD) threshold <= wdata[2:0];
    end
  end
endmodule
Read included file: ap_clint_timer.sv
// Timer slice of ap_soc's local interrupt block. One hart, one timebase tick
// per clk and a registered interrupt output. Word strobes arrive at commitment.
module ap_clint_timer #(parameter logic [63:0] RESET_COMPARE = '1) (
  input logic clk, reset_n, write_cmp_low, write_cmp_high,
  input logic [31:0] wdata,
  output logic [63:0] mtime, mtimecmp,
  output logic mtip
);
  always_ff @(posedge clk) begin
    if (!reset_n) begin
      mtime <= 0;
      mtimecmp <= RESET_COMPARE;
      mtip <= 0;
    end else begin
      mtime <= mtime + 1'b1;
      if (write_cmp_low) mtimecmp[31:0] <= wdata;
      if (write_cmp_high) mtimecmp[63:32] <= wdata;
      mtip <= mtime >= mtimecmp;
    end
  end
endmodule
ConceptWhy does bring-up start with a small program, not the product firmware?
ConceptIn the lab, what does step 2, the timer tick, prove for the first time?
ConceptSteps 1 and 2 pass, step 3 fails. Which parts are the suspects?
ConceptAn interrupt never arrives. mip shows the request, and mie is 0. What is wrong?
ConceptThe first byte is echoed, and later bytes cause no interrupt. What is the most likely fault?

Common Mistakes

Wrong Load the product firmware first.

It uses every part at once, so a failure names none of them.

Fix Run small programs in order, each adding one or two parts.

Wrong Judge a step by watching the board.

A program that hangs looks the same as one that is still working.

Fix End each program with a check that reads a result.

Wrong Test an interrupt with one event.

A missing completion lets the first event through and blocks the rest.

Fix Send several events, and compare their count with the interrupts taken.

Remember

  • Bring-up is the first run of software on a new chip. It starts small.
  • Each bring-up program adds one or two parts and ends with a check that reads a result.
  • The first failing step names its suspects: the parts it added.
  • A silent interrupt: walk the path from the device to mstatus. The first 0 is the break.
  • An interrupt that arrives once: look at the way back, such as the completion.

Practice

Report a problem with this page