Skip to content

P4R1H/CacheSim

Repository files navigation

Cache Simulator

Dynamic C program that simulates a cache memory with different configurations and policies.

Features

  • The program can implement different policies based on user choices and simulate a cache according to them.
  • The program can perform read or write operations on the cache with given addresses and data.
  • The program can test the cache performance by running random operations and measuring the hit rate, miss rate, and access time.
  • The program can handle errors and invalid inputs.

Functions

The program consists of the following functions:

  • initializeCache: This function creates the cache with the given size, block size, associativity, coherency, and policy.
  • initializeMemory: This function creates the memory with the given block size and fills it with random data.
  • readCache: This function reads the data from the cache with the given address and updates the cache fields and the performance metrics.
  • writeCache: This function writes the data to the cache with the given address and data and updates the cache fields and the performance metrics.
  • runRandomOperations: This function tests the cache performance by running a given number of random operations and prints the hit rate, miss rate, and average access time.
  • decimalToBinary, binaryToDecimal, hexToBinary, hexToDecimal: These functions convert between decimal, binary, and hexadecimal number systems and return the converted number.
  • log2n: Calculates the logarithm base 2 of a given decimal.
  • removePrefix: Removes 0x from the entered address, if present.

How to use

To use the program, follow these steps:

  • Compile the program using the command gcc cache_simulator.c -o cache_simulator.
  • Run the program using the command ./cache_simulator.
  • Enter the cache parameters and tune the memory to your liking (optional)
  • The Cache size needs to be in kb, and the block size in b, though this can be changed with very slight modifications in the code.
  • Enter the operation type, the address and data (if applicable) when prompted.
  • Repeat step 4 until you want to exit the program.

Working explanation

The program works by simulating the cache and memory as arrays of structures. Each structure represents a block and contains the tag, index, offset, data, valid bit, dirty bit, and reference bit. The program calculates the bit for address/tag/offset operations to manipulate the cache fields and to convert between number systems. The program also uses pointers and dynamic memory allocation to create and manipulate the cache and memory arrays. The program implements different cache configurations and policies such as cache size, block size, associativity, coherency, and replacement policy. The program uses the following steps to perform a read or write operation on the cache:

  • It extracts the tag, index, and offset from the address.
  • It calculates the set number by dividing the index by the associativity.
  • It searches for the block with the matching tag in the set using a loop.
  • If the block is found (hit), it performs the corresponding operation and updates the cache fields and the performance metrics (if testing).
  • If the block is not found (miss), it selects a block to replace from the set using the replacement policy (If filled) and updates the cache fields and the performance metrics.

Scope to improve

  • Max memory size: 2^30 (Due to the constraints of int datatype in c)
  • Increasing the levels of cache (L1, L2, L3)
  • Remove repeated code with functional programming
  • Working with byte-addressable data-types instead of int/char/etc

About

A dynamic Cache simulator made in c

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages