A Verilog implementation of a two-level cache hierarchy with a direct-mapped L1 cache, a 2-way set-associative L2 cache, a centralized FSM controller, latency modeling, and a simulation testbench.
This project was developed for a Computer Architecture course to model cache lookup behavior, associativity, replacement metadata, promotion/demotion between cache levels, write policies, and access latency.
The design models a two-level cache hierarchy:
- L1 Cache: direct-mapped cache with 8 lines
- L2 Cache: 2-way set-associative cache with 16 sets
- Controller: centralized FSM that manages requests, latency counters, hit/miss handling, promotion, demotion, invalidation, and completion signaling
- Memory: modeled only through latency; no actual memory data array is required
Only cache metadata is modeled: tags, valid bits, and LRU bits. The project does not model real cache data payloads.
| Component | Organization | Index Bits | Tag Bits | Notes |
|---|---|---|---|---|
| L1 Cache | Direct-mapped, 8 lines | address[6:4] |
address[31:7] |
Fastest cache level |
| L2 Cache | 2-way set-associative, 16 sets | address[7:4] |
address[31:8] |
Uses one LRU bit per set |
| Memory | Latency-only model | N/A | N/A | No data payload modeled |
- Direct-mapped L1 cache
- 2-way set-associative L2 cache
- Valid-bit and tag-based hit detection
- LRU replacement metadata for L2
- Centralized FSM controller
- Read hit, read miss, promotion, and demotion behavior
- Write-through and no-write-allocate policy
- Write-hit invalidation
- Latency modeling for L1, L2, and memory
- Simulation testbench with multiple cache scenarios
- Waveform dump support through VCD output
| Module | Description |
|---|---|
L1.v |
Implements the direct-mapped L1 cache with tag storage, valid bits, hit detection, installation, and invalidation. |
L2.v |
Implements the 2-way set-associative L2 cache with two tag arrays, valid bits, LRU metadata, hit detection, installation, and invalidation. |
top_module.v |
Contains the centralized FSM controller and connects L1, L2, request handling, latency counters, promotion/demotion, and done signaling. |
testbench.v |
Runs simulation scenarios for read/write hits and misses and generates a VCD waveform file. |
- L1 hit: complete after L1 latency.
- L2 hit: promote the requested block to L1 and demote the L1 victim to L2 if the victim is valid.
- Miss in both caches: wait for memory latency, install the requested block into L1, and demote the L1 victim to L2 if valid.
- Write hit in L1 or L2: invalidate the hit cache line.
- Write miss: no-write-allocate; the request completes without installing a new cache line.
| Access Level | Latency |
|---|---|
| L1 | 1 cycle |
| L2 | 5 cycles |
| Memory | 20 cycles |
The controller checks L1 first. If L1 misses, it checks L2. If L2 also misses, memory latency is modeled before installing the block into L1.
The controller in top_module.v uses the following states:
| State | Purpose |
|---|---|
S_IDLE |
Waits for a new request. |
S_L1_WAIT |
Models L1 latency and checks the L1 hit result. |
S_L2_WAIT |
Models L2 latency and checks the L2 hit result. |
S_MEM_WAIT |
Models memory latency on a read miss. |
S_PROMOTE |
Promotes an L2 hit into L1 and demotes the L1 victim to L2 if valid. |
S_INSTALL |
Installs a memory-fetched block into L1 and demotes the L1 victim if valid. |
S_WRITE_INV |
Invalidates the cache line on a write hit. |
S_DONE |
Raises the done signal and reports hit information. |
For a more detailed explanation of the cache hierarchy, FSM behavior, tag/index layout, promotion/demotion logic, LRU handling, and testbench scenarios, see:
- Open Xilinx Vivado.
- Create a new RTL project.
- Add all Verilog files from the
src/directory. - Add
sim/testbench.vas the simulation source. - Set
testbenchas the simulation top module. - Run behavioral simulation.
- Inspect the console output and waveform signals.
The testbench covers the required cache behaviors:
- Read miss
- Read hit in L1
- Read miss with L1 victim demotion to L2
- Read hit in L2 with promotion back to L1
- Additional L1 hit after promotion
- L2 way installation behavior
- L2 hit behavior
- Write hit in L1 with invalidation
- Write hit in L2 with invalidation
- Write miss with no-write-allocate behavior
When viewing the waveform, useful signals include:
clkrstreqmemWriteaddressl1_hitl2_hitdonel1_validl2_validstatecycle_counterl1_installl2_installl1_invalidatel2_invalidate
This project is an educational cache simulation and does not implement a complete memory system.
Current limitations include:
- Cache data payloads are not modeled.
- Main memory contents are not modeled.
- Only one outstanding request is supported at a time.
- The design is intended for simulation rather than FPGA deployment.
- Replacement behavior is simplified for educational clarity.
- No processor core is connected to the cache hierarchy.
- Add actual cache data storage.
- Add a memory module with data payloads.
- Add separate module-level testbenches for L1, L2, and the controller.
- Add a diagram of the cache hierarchy and FSM.
- Add support for configurable cache sizes and latencies.
This project is intended for educational and portfolio purposes.

