{"id":15047439,"url":"https://github.com/ttwag/p11_lru_cache","last_synced_at":"2026-02-13T12:15:26.672Z","repository":{"id":215379614,"uuid":"738782989","full_name":"ttwag/P11_LRU_Cache","owner":"ttwag","description":"Simulate a fully-associative LRU cache with user-modifiable block number","archived":false,"fork":false,"pushed_at":"2024-01-06T23:52:32.000Z","size":501,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-13T20:21:26.411Z","etag":null,"topics":["cache-simulator","cpp14","dsa-algorithm","dsa-practive","google-test","hardware-designs","object-oriented-programming","test-automation"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ttwag.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-04T03:13:11.000Z","updated_at":"2024-01-05T19:52:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"1853717a-e8d7-4002-a5b0-4749828de3a5","html_url":"https://github.com/ttwag/P11_LRU_Cache","commit_stats":null,"previous_names":["ttwag/p11_lru_cache"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ttwag/P11_LRU_Cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttwag%2FP11_LRU_Cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttwag%2FP11_LRU_Cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttwag%2FP11_LRU_Cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttwag%2FP11_LRU_Cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ttwag","download_url":"https://codeload.github.com/ttwag/P11_LRU_Cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttwag%2FP11_LRU_Cache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259752078,"owners_count":22905972,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache-simulator","cpp14","dsa-algorithm","dsa-practive","google-test","hardware-designs","object-oriented-programming","test-automation"],"created_at":"2024-09-24T20:58:23.687Z","updated_at":"2026-02-13T12:15:26.621Z","avatar_url":"https://github.com/ttwag.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# P11_LRU_Cache\nSimulate a fully-associative LRU cache\n\n![Figure1](./Figure1.png)\n\n# Introduction\n* Cache is an important part of the CPU. It exploits the spatial and temporal locality of the program, and stores frequently accessed items near the CPU for quick access.\n* The purpose of this program is to simulate a basic cache and display the hit and miss rate.\n* To understand the parameters of this cache simulation program, I recommend you to check out the reading list in the Helpful Link section. \n## Cache Parameter\n* In this design, our cache has 1 set and holds user-decidable number of blocks, each with 4 bytes of data and is byte-addressable.\n* Each cache block has a tag extracted from the memory address.\n* Each memory address is 16 bits (EX: 0x0000) and stores 1 byte of data.\n* This is an LRU cache. \n* There is no valid bit or data in the cache.\n* There is only a read operation and no write operation because there's no data.\n\n## Memory Address Translation\n* Since the cache only has a set, there's no index bit.\n* Since each block stores 4 bytes, the Block Offset has $log_2 4 = 2$ bits.\n* To get the number of tag bits, subtract the Block Offset bits from the total bits and we get 14 bits.\n\n 16 Bits Memory Address\n\n| Tag | Index | Block Offset |\n|----:|------:|-------------:|\n|  14 |     0 |            2 |\n\n### Inside the Cache\n* Each of the blocks should have a valid bit, a tag, and the data.\n* For simulation purposes, I ignored the valid bit and the data, leaving only the tag.\n\n| Valid | Tag | Data |\n|------:|----:|-----:|\n\n## Acronym and Definition\n* **LRU (Least-Recently-Used)** - a cache replacement policy when the cache is full. It replaces the least-recently-used block by the new block. \n* **MRU (Most Recently Used)** - the most recently used (read or write) block in the cache.  \n* **LSB (Least Significant Bit)** - the right most bit in a binary number.\n* **Cache Hit** - when you read a cache, and the data stored in the memory address you want to read **is in the cache**. Since there's no data in this cache, finding the same tag in cache is a cache hit.  \n* **Cache Miss** - when you read a cache, and the data stored in the memory address you want to read is **not in the cache**. The cache will then bring the data from the RAM. Here, not finding the tag is a cache miss. \n\n## Installation\n### Linux\n```\n// In a directory that you want to download this project\n\ngit clone \"https://github.com/ttwag/P11_LRU_Cache\"\n\n// Run the demo\n\ncd P11_LRU_Cache\ncmake .\nmake\n./P11_LRU_Cache\n```\n\n### Run the Test\n* This program is tested with the GoogleTest C++ framework.  \n* There are 30 test cases to test this cache simulation program.\n* 5 of them test for unexpected inputs and the other 25 tests for LRU replacement policy.\n* You can edit the test inside the test.cpp using GoogleTest's syntax.\n* To run the test, navigate inside the tests directory, then \n```\n// Inside of tests directory\n\ncmake .\nmake\n./LRU_Cache_Test\n```\n\n## How to Use this Program\nThis program is packaged into a LRUCache class.\n\nYou could interact with the LRUCache in the main.cpp or import the defined LRUCache class into your own C++ file.\n\nRead about the implementation details in the comments inside C++ class and header files.\n### Object Initialization\n* Initialize a LRUCache object and input the number of blocks you want into the constructor.\n```\n// Creates a new LRUCache object with 1 block\n \nLRUCache* myCache = new LRUCache(1);\n```\n\n### Read the Cache\n* You could read the cache with an input address, and the cache will return **true** for cache hit, and false for a miss. Note that there's a hit if the **address's tag** is found.  \n* read(): read the data of a memory address from the cache. Returns true for cache hit and false for cache miss.\n\n```\n// Read the memory address 0x1111, which has the tag 0x1110\n\nmyCache-\u003eread(0x1111);  // If the cache is empty, this is a cache miss\nmyCache-\u003eread(0x1111);  // This is a cache hit\nmyCache-\u003eread(0x1113);  // This is a cache hit, 0x1111 and 0x1113 has the same tag\nmyCache-\u003eread(0x1121);  // This is a cache miss, LRU will replace 0x1111's tag with that of 0x1121\nmyCache-\u003eread(0x1111);  // This is a cache miss, \n```\n\n### Print the Cache\n* You could print the tags in the cache with the print() method. It displays the least-recently-used tag from top to bottom.\n```\nmyCache-\u003eprint();  // Print the cache in the last example\n\n// Console Output\nHit Rate (%): 40\nMiss Rate (%): 60\nStored Tags\nLRU:    1110\n```\n\n## File Structure and Dependency\n* P11_LRU_Cache\n  * main.cpp: runs the demo program by making a LRUCahe object.\n  * LRUCache.cpp: contains the class implementation of the LRUCache class.\n  * LRUCache.h: the class header file that contains the LRUCache class declaration.\n  * CMakeLists.txt: builds the main.cpp.\n  * Figure1.png: the image at the beginning of the README.\n  * tests: test directory\n    * test.cpp: contains the test cases and runs them.\n    * CMakeLists.txt: builds the test.cpp and imports the GoogleTest framework.\n\n## Development Environment\nCLion 2023.3.2\n\n## Helpful Link\n* I did this project after completing the class, [EEC 170](https://ece.ucdavis.edu/course-catalog), at UC Davis. I thought it would be nice to program a basic cache.\n* If you want to program a basic cache as well, check out this [LeetCode Problem](https://leetcode.com/problems/lru-cache/).\n* For understanding the basics of cache: [Computer Organization and Design RISC-V 2nd Edition](https://www.amazon.com/Computer-Organization-Design-RISC-V-Architecture-dp-0128203315/dp/0128203315/ref=dp_ob_title_bk) Section 5.1 - 5.3\n* [GoogleTest](https://github.com/google/googletest)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fttwag%2Fp11_lru_cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fttwag%2Fp11_lru_cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fttwag%2Fp11_lru_cache/lists"}