How does cache work?

Using the cache is optional, but it is critically important if you want to maximize performance.

You can manually load cache or have it automatically load itself as you execute code.

Once data is in the cache, it can be read and modified without accessing main memory. If the cache is large enough to hold all the data needed by your application, it may never have to update main memory. This will enable maximum performance and will reduce power consumed by accessing main memory.

Cache Hit

When the CPU fetches data from cached memory, the system checks if the required data exists in the cache. If it exists, the data is read directly from the cache with no performance penalty. This is called a "cache hit".

Cache Miss

If required data does not exist in the cache, hardware fills the cache with data from main memory. The CPU must wait for the cache fill to complete before reading data. This is called a “cache miss”. A cache miss incurs a performance penalty proportional to the time required to perform the cache fill.

Dirty Cache

If the CPU modifies a location in cache without writing it back to main memory, that cache line is said to be “dirty”. The cache controller keeps track of these dirty lines.

Update main memory before evicting data from cache

At some point, the hardware may need to make room in the cache for new data. Before modified (dirty) data can be evicted from the cache, it must be written back to main memory. The hardware will do this automatically.

There are two methods the hardware uses to update main memory:

  1. Immediately update main memory when cache changes (write-through).
  2. Wait to update main memory when data is evicted from the cache (write-back).

You control which method is used.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.