S3.3.2 The Hit Path

MediumImplement35 minIcarus Verilog · serverReview the topic

Build the read side of a direct-mapped cache: split the address, read one line, compare one tag, pick one word. Add the port that fills a line and a reset that clears the right bits.

Hardware context: A direct-mapped read cache of four lines with blocks of four words, in front of a slow memory, as test code. Designing and checking the hit path and the reset of a cache

A direct-mapped cache serves byte addresses of 16 bits. It has 8 lines. Each line holds a valid bit, a tag and a block of 4 words of 32 bits. Write the module that keeps these lines, answers a read, and takes a block when it is told to fill.

The block that decides when to fill, and that fetches the block from memory, is not part of this problem. The bench plays that part.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
PortMeaning
addrThe byte address of a read. Split it into tag, index and offset for this cache yourself.
hit1 when the line of addr is valid and holds the block of addr. It follows addr with no clock edge in between.
rdataOn a hit, the word of the block that addr names. On a miss, 0.
fill, fill_addr, fill_blockAt a rising edge with fill at 1, the line of fill_addr takes fill_block, becomes valid, and holds the block of fill_addr from then on. Word 0 of the block is bits 31 to 0.
rst_nAt a rising edge with rst_n at 0, no line is valid afterwards.

fill_addr may be any byte address inside the block, not only its first byte. addr may be any byte address: the two lowest bits pick a byte inside the word and are of no use here.

Examples

Example 1

Miss, Fill, Two Hits
Input
read 0x1234; then fill the block of 0x1234 with words 0xa0a00000, 0xb1b10001, 0x82820002, 0x93930003; then read 0x1234; then read 0x1238
Output
hits = 2, last_hit = 1, last_word = 0x82820002, signature = 0xe1e00002

After the reset nothing is valid, so the first read is a miss and returns 0. After the fill, 0x1234 picks word 1 of the block and 0x1238 picks word 2.

Example 2

Two Blocks, One Line
Input
fill the block of 0x0040 with words 0xb0b00000, 0xa1a10001, 0x92920002, 0x83830003; then fill the block of 0x8040 with words 0xc0c00000, 0xd1d10001, 0xe2e20002, 0xf3f30003; then read 0x0040; then read 0x804c
Output
hits = 1, last_hit = 1, last_word = 0xf3f30003, signature = 0xf3f30002

The two blocks have the same index and different tags. The second fill replaces the first. So 0x0040 is a miss, and 0x804c is a hit on word 3 of the second block.

Example 3

A Reset in the Middle
Input
fill the block of 0x00a0 with words 0xd0d00000, 0xc1c10001, 0xf2f20002, 0xe3e30003; then read 0x00a0; then reset; then read 0x00a0
Output
hits = 1, last_hit = 0, last_word = 0x0, signature = 0xa1a00003

A reset clears the valid bits. The read after it is a miss, although the line still holds the old tag and block.

Constraints and Contract

  • hit and rdata depend on addr and on the stored lines only. A fill becomes visible in the cycle after its edge, not before.
  • A fill replaces whatever its line held, valid or not.
  • At an edge with rst_n at 0, a fill is ignored.
  • The bench also lowers rst_n in the middle of a run. Every read after that must miss until its line is filled again.
  • hit must be 0 or 1 from the first reset on, and never unknown.

Hints

Hint 1

Work out the three fields first: how many bytes has a block, and how many lines are there?

Hint 2

A hit needs two things at once. Ask what a line holds that was never filled.

Hint 3

Ask which of the stored bits a reset must touch, and which it may leave alone.

What the grader checks: Every line and every word; each tag bit and each index bit changed alone; a fill named by any byte of its block; a read in the cycle of a fill; misses on a valid line, on a line never filled and after a reset in the middle; a fill in the cycle of a reset; random sequences. Each check compares the number of hits, the last answer and a signature over every answer.

97 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

Miss, Fill, Two Hits

Input
read 0x1234; then fill the block of 0x1234 with words 0xa0a00000, 0xb1b10001, 0x82820002, 0x93930003; then read 0x1234; then read 0x1238
Expected output
hits = 2, last_hit = 1, last_word = 0x82820002, signature = 0xe1e00002

After the reset nothing is valid, so the first read is a miss and returns 0. After the fill, 0x1234 picks word 1 of the block and 0x1238 picks word 2.

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

Icarus Verilog · server