V0.1.2 Count the Bus Accesses

EasyImplement12 minIcarus Verilog · serverReview the topic

Predict how many loads and stores reach the bus for small register fragments. Compare them with and without volatile, as the RISC-V compiler builds them.

Hardware context: A host-native model of the UART0 transmit register and status word of ap_soc. Predicting the bus traffic a compiled register access really makes.

Recorded with riscv64-unknown-elf-gcc (14.2.0+19) 14.2.0 (-march=rv32i_zicsr -mabi=ilp32 -O2 -ffreestanding -nostdlib). IN_P and OUT_P are plain pointers to GPIO IN and OUT; IN_V and OUT_V are the same addresses through volatile pointers.

This worksheet uses named constants. The first three entries show how to record your decisions.

Replace each unresolved value of 255 with your result.

Signals in the Timeline

SignalWidthRole
question8input of each check
Full specification
NameC fragment
blink_plainvoid blink_plain(void) { OUT_P = 1; OUT_P = 0; OUT_P = 1; }
blink_volvoid blink_vol(void) { OUT_V = 1; OUT_V = 0; OUT_V = 1; }
twice_plainuint32_t twice_plain(void) { uint32_t a = IN_P; uint32_t b = IN_P; return a + b; }
twice_voluint32_t twice_vol(void) { uint32_t a = IN_V; uint32_t b = IN_V; return a + b; }
discard_plainvoid discard_plain(void) { (void)IN_P; }
discard_volvoid discard_vol(void) { (void)IN_V; }
rmw_volvoid rmw_vol(void) { OUT_V |= 4u; }
rmw2_plainvoid rmw2_plain(void) { OUT_P |= 4u; OUT_P |= 8u; }
rmw2_volvoid rmw2_vol(void) { OUT_V |= 4u; OUT_V |= 8u; }
wait_volvoid wait_vol(void) { while ((IN_V & 8u) == 0) { } }

Count bus accesses the program makes when it runs, not lines of assembly. A load (lw) reads the bus once; a store (sw) writes it once.

EntryYour decision
BLINK_PLAIN_STORESStores made by blink_plain.
BLINK_VOL_STORESStores made by blink_vol.
TWICE_VOL_LOADSLoads made by twice_vol.
TWICE_PLAIN_LOADSLoads made by twice_plain.
DISCARD_PLAIN_LOADSLoads made by discard_plain.
DISCARD_VOL_LOADSLoads made by discard_vol.
RMW_VOLBus accesses made by rmw_vol.
RMW2_PLAINBus accesses made by rmw2_plain.
RMW2_VOLBus accesses made by rmw2_vol.
WAIT_VOL_HIGH5Loads made by wait_vol when pin 3 first reads high on the fifth read.
LOOP_ACCESSESBus accesses made when this runs: for (unsigned i = 0; i < N; i++) OUT_V |= 1u << i;

Examples

Example 1

BLINK PLAIN STORES
Input
Stores made by blink_plain.
Output
matches = 1

The compiler keeps only the last store: OUT_P = 1.

Example 2

BLINK VOL STORES
Input
Stores made by blink_vol.
Output
matches = 1

volatile forbids merging: 1, 0, 1 all reach the bus.

Example 3

TWICE VOL LOADS
Input
Loads made by twice_vol.
Output
matches = 1

Two volatile reads, two loads.

Constraints and Contract

  • Keep the parameter and named constants. Use the numeric codes and units on this page.
  • For LOOP_ACCESSES, enter a constant expression using N. Only this entry changes with the parameter. The supported integer range is 1 through 8, inclusive.

Hints

Hint 1

It may merge, move or delete accesses it can prove are not needed in C.

Hint 2

Every volatile access in the source happens, in order.

Hint 3

A loop with one load in its body makes one load per pass.

What the grader checks: Each check compares the full eight-bit answer. Parameter trials change one condition and keep the other decisions fixed.

15 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

BLINK PLAIN STORES

Input
Stores made by blink_plain.
Expected output
matches = 1

The compiler keeps only the last store: OUT_P = 1.

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

Icarus Verilog · server