S3.5.2 Finish the Controller FSM

HardImplement45 minIcarus Verilog · serverReview the topic

Write the state machine of a write-back cache: compare, write a dirty block back, fetch the new block, and compare again.

Hardware context: A direct-mapped cache with its controller in front of a memory on the bus, acted out edge by edge as a script. Designing and timing the miss path of a cache

A direct-mapped cache with write-back and write-allocate has its lines, its tag compare and its dirty bits. What it lacks is the controller that decides what happens in each cycle. The states are given. Write the state register and the five outputs.

The lines, the memory and the processor are part of the bench. They do exactly what your outputs say, at the rising edge of the cycle in which you say it.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
What each state does
StateIn this stateIt leaves when
IDLENothing. Every output is 0.cpu_req is 1: go to COMPARE.
COMPAREOn a hit the request is done: cpu_ready is 1, and line_write is 1 if it is a write.Always. A hit goes to IDLE. A miss goes to WRITE_BACK if dirty is 1, and to ALLOCATE if not.
WRITE_BACKmem_write is 1 in every cycle of this state.mem_ready is 1: go to ALLOCATE.
ALLOCATEmem_read is 1 in every cycle of this state. line_load is 1 in the cycle with mem_ready at 1, and only then.mem_ready is 1: go to COMPARE.

hit and dirty describe the line of the address that the processor shows. They change when the line changes: after line_load the same request is a hit. Memory raises mem_ready in the last cycle of a block transfer, after one, two or three cycles.

Examples

Example 1

A Miss, Then a Hit
Input
Memory needs 2 cycles for a block. read word 0x14, then read word 0x14
Output
cycles = 7, memory_reads = 1, memory_writes = 0, last_read = 0xc0de0014, signature = 0xbce01fcd

The first read misses: IDLE, COMPARE, two cycles of ALLOCATE, COMPARE again. That is 5 cycles and one memory read. The second read hits in 2 cycles: 7 in all.

Example 2

A Write That Misses
Input
Memory needs 1 cycle for a block. write 0x11112222 to word 0x21, then read word 0x21
Output
cycles = 6, memory_reads = 1, memory_writes = 0, last_read = 0x11112222, signature = 0x1800f9

The write misses, so the block is fetched first, and the word goes into the line in the second COMPARE: 4 cycles. The read hits and returns the new word. Nothing was written to memory.

Example 3

A Dirty Block Is Pushed Out
Input
Memory needs 2 cycles for a block. write 0x11112222 to word 0x21, then read word 0x41, then read word 0x21
Output
cycles = 17, memory_reads = 3, memory_writes = 1, last_read = 0x11112222, signature = 0xe7fc0161

Word 0x41 uses the same line as 0x21. The dirty block goes back to memory first, and then the new block is fetched. The last read fetches 0x21 again and finds the written word: two memory writes are not needed, one is.

Constraints and Contract

  • The states and their order are fixed, as in the table. A machine that answers a hit in one cycle, or that skips the second COMPARE, takes another number of cycles and is refused.
  • Each output must be 0 in every state that the table does not name for it.
  • mem_read and mem_write must never be 1 in the same cycle. cpu_ready must be 1 only while cpu_req is 1.
  • At a rising edge with rst_n at 0 the machine goes to IDLE, from any state. The bench does this in the middle of a run too. The request is still there afterwards and must then be served.

Hints

Hint 1

Write the next state for each of the four states first. Three of them wait for one input.

Hint 2

Then write each output as one line: in which state is it 1, and with which input?

Hint 3

After the fetch, the line holds the block. Ask what COMPARE sees when it runs again.

What the grader checks: Clean misses, write misses, write hits, dirty and clean blocks pushed out by reads and by writes, with memory of one, two and three cycles; cycles with no request; a reset in the middle of a fetch, of a write back and of a compare; random sequences. Each check compares the cycle in which the last request was done, the memory reads and writes, the last read word, and a signature over every read word, the whole memory and the four lines.

87 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 4 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

A Miss, Then a Hit

Input
Memory needs 2 cycles for a block. read word 0x14, then read word 0x14
Expected output
cycles = 7, memory_reads = 1, memory_writes = 0, last_read = 0xc0de0014, signature = 0xbce01fcd

The first read misses: IDLE, COMPARE, two cycles of ALLOCATE, COMPARE again. That is 5 cycles and one memory read. The second read hits in 2 cycles: 7 in all.

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

Icarus Verilog · server