How sha1v1/cache-simulator works
Purpose and Runtime
A C command-line cache simulator models reads and writes between a configurable cache and main memory. The executable is built via Makefile and run as ./build/cache_sim; interactive control begins in src/main.c.
Startup and Configuration
src/main.c initializes simulator state, loads settings, and presents the read/write/display/exit menu. Configuration parsing is separated into src/config.c, with public configuration types and APIs in include/config.h. The repository-provided runtime configuration is config.txt.
Main Memory Boundary
Main memory is an in-process byte-addressable backing store, not an external service or persistent datastore. src/memory.c implements allocation and address-level read/write behavior exposed through include/memory.h. Cache misses fetch blocks from this layer; write-through operations propagate updates to it.
Cache Core
src/cache.c implements cache organization and operations defined by include/cache.h: mapping an address to a set/tag/block location, probing valid lines, loading blocks on misses, updating cached data, and rendering cache state. Its state is governed by configured set count, lines per set, and memory/block relationships.
Read Flow
A menu read request enters src/main.c, then the cache API in src/cache.c. The cache derives the target set and tag, returns data on a hit, or selects a line and fetches the relevant block from src/memory.c on a miss. The loaded block becomes a valid cache line before the requested byte is returned.
Write Flow and Policies
Write requests follow the same cache lookup path in src/cache.c. On hits, cached data is updated; under the documented WRITE_THROUGH policy, the corresponding main-memory address is also updated through src/memory.c. Miss handling and eviction are controlled by the configured replacement policy (LRU or RANDOM) from src/config.c/config.txt.
Replacement and Observability
Replacement policy is an architectural decision within cache-line selection in src/cache.c; it determines which conflicting line is overwritten when a set has no free line. The same module provides cache-state display used by the CLI, exposing valid bits, tags, sets, lines, and block contents for simulation inspection.
Verification Boundary
The project uses the embedded Unity C test runtime under src/unity/. Cache and memory behavior are independently exercised by tests/test_cache.c and tests/test_memory.c; these validate the core simulation layers beneath the interactive entry point.
Main entry points: cache.c Cache core · config.c Configuration parser · main.c Interactive CLI · memory.c Main memory
Components 14
Simulator Runtime
- Build · Builds the simulator executable.
Makefile - Interactive CLI · Initializes state and handles the read/write/display menu.
src/main.c - cache_sim
- Runtime settings
config.txt
Simulation Core
- Configuration parser
src/config.c - Configuration API
include/config.h - Cache core · Maps addresses, probes lines, loads blocks, updates data, and renders state.
src/cache.c - Cache API
include/cache.h - Line selection · LRU or RANDOM selects an eviction target when a set is full.
src/cache.c - Main memory · Backing store for block fills and write-through updates.
src/memory.c - Memory API
include/memory.h
Verification
- Unity runtime
- Cache tests
tests/test_cache.c - Memory tests
tests/test_memory.c
Connections 16
- Build → cache_sim: builds
- cache_sim → Interactive CLI: starts
- Interactive CLI → Configuration parser: loads settings
- Runtime settings → Configuration parser: parsed by
- Configuration parser → Configuration API: implements
- Interactive CLI → Cache core: read / write / display
- Cache core → Cache API: implements
- Cache core → Line selection: uses on eviction
- Configuration parser → Line selection: configures policy
- Configuration parser → Cache core: configures geometry
- Cache core → Main memory: miss block fill; write-through
- Main memory → Memory API: implements
- Cache tests → Cache core: exercises
- Memory tests → Main memory: exercises
- Cache tests → Unity runtime: runs with
- Memory tests → Unity runtime: runs with