Cache Simulator
Supplementary material for Computer Architecture: Design and Analysis (2026 edition) · Krerk Piromsopa, Ph.D. · Chulalongkorn University
CacheSim is a configurable direct-mapped cache simulator written in C. It reads a load-address trace (one hex address per line) and reports hit count, miss count, and miss rate. Students modify the header file parameters and the source code to explore how cache size, block size, and associativity affect miss rate — the key exercise in the Cache activity of Appendix D.
Downloads
Quick Start
Compile and run on Linux / macOS (any C99 compiler):
On Windows (MinGW / WSL):
Configuration Parameters (CacheSim.h)
Change these #define values to model different cache configurations.
The bit-field lengths must satisfy:
TAGLEN + INDEXLEN + OFFSETLEN = 32.
| Parameter | Default | Meaning |
|---|---|---|
CACHE_SIZE | 1024 | Total cache size in bytes |
INDEX_SIZE | 256 | Number of cache lines (= CACHE_SIZE / BLOCK_SIZE) |
BLOCK_SIZE | 4 | Block (cache line) size in bytes |
TAGLEN | 22 | Bits used for the tag field |
INDEXLEN | 8 | Bits used to index into the cache (log₂ INDEX_SIZE) |
OFFSETLEN | 2 | Bits used for the block offset (log₂ BLOCK_SIZE) |
BLOCK_SIZE = 16, INDEX_SIZE = 256,
OFFSETLEN = 4, INDEXLEN = 8, TAGLEN = 20
Address Trace Format
Each line of the trace file contains a single load address in lowercase hex
with the 0x prefix:
You can generate your own traces using valgrind's
lackey tool or by instrumenting programs with
Valgrind / PIN.
Exercise Tasks (Appendix D)
The Appendix D cache activity asks you to run CacheSim with the two provided traces and fill in the following experiments:
-
Block size tradeoff on a direct-mapped cache —
vary
BLOCK_SIZE(4, 8, 16, 32 bytes) andCACHE_SIZE(4 KB, 8 KB, 16 KB, 32 KB). Record the miss rate for each combination. -
Associativity — extend
CacheSim.cto support N-way set-associative caches with LRU and Round-Robin replacement. Compare miss rates for 2-way and 4-way at multiple cache sizes. - Analysis — plot miss rate vs. cache size for each block size, and discuss the tradeoffs observed.
Cache struct into an array of N ways,
add an LRU counter, and update the access() function accordingly.
This is intentionally left as a programming exercise.