How would I go about the write port of a set-associative cache memory?
That is one of the key questions in designing a set-associative cache &mdash; what is the "replacement strategy?" In other words, when you need to write to the cache, which element of the set do you write to?

There are a number of basic rules:


An element that is "empty" can be written to.
An element that is "dirty" (hasn't been copied back to main memory yet) CANNOT be written to.
Of the remaining elements (not empty and not dirty), picking the correct one to replace is a key factor in the overall performance of the cache.


Round-robin replacement (a simple counter) is cheap and easy to implement.

LRU (least-recently used) attempts to take advantage of the "locality of reference" (in both time and space) exhibited by the vast majority of software, but requires more logic to implement.