S3.11.1 Stretch the Read Until Data Arrives

MediumImplement45 minIcarus Verilog · serverReview the topic

Turn one bus read into one exchange with a serial flash chip, and keep the bus waiting until the word is there.

Hardware context: A small model of a serial flash chip (read command 03) and test code that acts as its controller on the four SPI wires. Execute-in-place flash controllers of small microcontrollers

A small chip runs its program straight from a serial flash chip on the board. The processor reads a word from the bus, as it would from a ROM. A controller in between fetches the word over four wires, one bit at a time.

Write that controller. While the bits arrive, the bus must wait. When the word is complete, the controller says so for one cycle.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
One exchange with the flash
Serial clocksWireWhat is sent
1 to 8mosithe command 03 in hex, highest bit first
9 to 32mosithe 24 address bits, highest bit first
33 to 64misofour bytes, each with its highest bit first. The first byte is the one at addr
The serial wires, in SPI mode 0
RuleDetail
Chip select frames the exchangecs_n is 0 from before the first serial clock until after the last. It is 1 at all other times
The flash takes mosi as sck risesSo mosi must already hold its bit in the cycle before sck rises, and keep it while sck is 1
The flash changes miso after sck fallsSo miso is good to sample as sck rises
The clock rests at 0sck is 0 whenever cs_n is 1, and in the cycle in which cs_n falls

How to read a result: "reads_answered" counts the cycles with ready at 1. "flash_exchanges" counts how often chip select fell. "rules_broken" counts breaks of the tables above and of the contract. "last_word" is rdata of the last answer.

Examples

Example 1

One Read
Input
read 000100
Output
reads_answered = 1, flash_exchanges = 1, rules_broken = 0, last_word = 0x7b54310a, signature = 0x3a14320a

One read. The flash sees one exchange of 64 serial clocks: the command 03 and the address 000100. The four bytes that it sends are 0a, 31, 54, 7b. The first byte is the lowest byte of the word.

Example 2

Two Reads in a Row
Input
read 000100, then read 000104
Output
reads_answered = 2, flash_exchanges = 2, rules_broken = 0, last_word = 0xfe8c59e, signature = 0x3a84a28a

Two reads, one straight after the other. The flash sees two separate exchanges, each with its own command and address.

Example 3

Idle Cycles, and the Last Word
Input
3 idle cycles, then read 000200, then 5 idle cycles, then read fffffc
Output
reads_answered = 2, flash_exchanges = 2, rules_broken = 0, last_word = 0xe6c19c77, signature = 0x2d53fd9a

The bus is idle before the first read and between the two, with other values on addr. The flash sees exactly two exchanges. The last word of the flash is read with all 24 address bits.

Constraints and Contract

  • The bus shows req at 1 with an address, and holds both until the cycle in which ready is 1. In that cycle rdata is the word.
  • The word is little-endian: the byte at addr is bits 7 to 0, and the byte at addr + 3 is bits 31 to 24.
  • ready is 1 for exactly one cycle for each read, and never without a request. It comes at most 200 cycles after req first shows.
  • Each read is one exchange of exactly 64 serial clocks. A new read is a new exchange: cs_n is 1 for at least one cycle in between.
  • In the cycle after ready, the bus may be idle or may show a new request. While req is 0, addr holds other values, and nothing must start.
  • Every output changes only at a rising edge of clk. You choose how many cycles sck stays at 1 and at 0.
  • At a rising edge with rst_n at 0 the controller goes idle: cs_n 1, sck 0, ready 0. The bench resets only at the start.

Hints

Hint 1

Count first: how many bits go out, how many come back, and so how many serial clocks one read needs.

Hint 2

One shift register can send, and one can receive. Decide at which edge of sck each one moves.

Hint 3

Draw the last serial clock, the rise of cs_n and the cycle of ready on paper before you write the end of the read.

What the grader checks: Every address bit alone and all but that bit; address 0 and the last word; one to five reads with no gap, the same address three times, gaps of 1 to 40 idle cycles with other values on addr; no read at all; 40 random sequences. A flash model looks at the wires once in each cycle. It checks the command, the address, the number of serial clocks of each exchange, mosi before each rising edge, and sck while cs_n is 1. After the reset the three outputs must be idle. A reset in the middle of a read is not tried.

74 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 Read

Input
read 000100
Expected output
reads_answered = 1, flash_exchanges = 1, rules_broken = 0, last_word = 0x7b54310a, signature = 0x3a14320a

One read. The flash sees one exchange of 64 serial clocks: the command 03 and the address 000100. The four bytes that it sends are 0a, 31, 54, 7b. The first byte is the lowest byte of the word.

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

Icarus Verilog · server