4.1.13Memory Technologies

Content-addressable memory (CAM)

2,153 words10 min readdifficulty · medium

WHY does CAM exist?

The classic killer app: a router's forwarding table. A packet arrives, you must instantly know which output port matches its destination IP. Sequential search is far too slow for line-rate networking — so routers use CAM.


WHAT exactly is CAM?

Two flavours:


HOW is it built? (Derivation from first principles)

We build CAM up from the humble comparison of one bit, then a word, then the whole array.

Step 1 — Compare one bit

We want a signal that says "stored bit DD ≠ search bit SS". The logic function for inequality of two bits is XOR:

mismatch=DS\text{mismatch} = D \oplus S

Why this step? Because a match requires every bit to agree. It's easier to build a circuit that detects disagreement and pulls a shared wire, than to AND together thousands of agreements.

Step 2 — The shared Match Line (ML)

Every cell in a row is wired to a single match line (ML), which is precharged HIGH before the search. Each cell contains a transistor path that discharges the ML to LOW if that bit mismatches.

So for a word of nn bits, the match line stays HIGH only if no bit discharges it:

MLrow=i=0n1(DiSi)=i=0n1(DiSi)=i=0n1(DiSi)\text{ML}_{\text{row}} = \overline{\bigvee_{i=0}^{n-1} (D_i \oplus S_i)} = \bigwedge_{i=0}^{n-1}\overline{(D_i \oplus S_i)} = \bigwedge_{i=0}^{n-1}(D_i \odot S_i)

where \odot is XNOR (equality). This is just De Morgan: "NOT (any mismatch)" = "all bits equal".

Why this step? The wired-OR on ML is the trick that makes search parallel: one shared wire per row, and any single disagreeing cell can single-handedly kill the match. No adder or comparator tree needed — physics does the OR for free.

Step 3 — The full array + encoder

  • RR rows, each an nn-bit word, each with its own match line MLr\text{ML}_r.
  • All match lines are searched simultaneously — one comparison cycle for the whole memory.
  • A priority encoder converts the (possibly many) HIGH match lines into a single output address (the highest-priority / lowest-index match).

search time  O(1)(independent of R,n in principle)\text{search time} \ \propto\ O(1)\quad\text{(independent of } R,n\text{ in principle)}

Figure — Content-addressable memory (CAM)

Worked examples


Common mistakes (steel-manned)


Where CAM is actually used


Recall Feynman: explain to a 12-year-old

Imagine a huge classroom. Normal memory (RAM) is like the teacher saying "seat number 42, stand up" — you look up a seat and see who's there. CAM is the opposite: the teacher shouts "whoever is named Sam, raise your hand!" and instantly every student named Sam raises a hand at the same moment. You didn't check one-by-one — everybody checked themselves simultaneously. That "everyone checks themselves at once" is why CAM is so fast, but also why it needs a tiny brain in every seat (extra transistors) and a lot of energy.


Active-recall flashcards

#flashcards/hardware

What question does CAM answer, vs RAM?
CAM: "which address holds this data?" (data→address). RAM: "what data is at this address?" (address→data).
What logic gate detects a single-bit mismatch in a CAM cell?
XOR (DSD\oplus S); the whole row uses NOT-OR-of-mismatches = AND of XNORs (equality).
Why is a match line precharged HIGH before a search?
So a mismatching cell can quickly discharge it; matched line stays HIGH. Dynamic-logic style gives O(1)O(1) parallel search.
Why is a priority encoder needed in a CAM?
Multiple rows may match simultaneously; the encoder selects one deterministic output address (e.g. lowest index / longest prefix).
Difference between BCAM and TCAM?
BCAM stores 0/1 (exact match). TCAM adds an X don't-care state (matches 0 or 1) using a mask bit, enabling longest-prefix matching.
Roughly how many transistors per BCAM vs TCAM cell?
BCAM ≈ 9–10T (6T SRAM + compare logic); TCAM ≈ 16T (needs 2 storage bits for 0/1/X).
Why is CAM not used as general-purpose memory?
High area, cost, and power — every cell has compare logic and every search charges every match line.
Give three real uses of CAM.
TLBs, fully-associative cache tag lookup, network router forwarding/ACL tables (TCAM).
Write the TCAM per-bit match condition with mask MiM_i.
matchi=Mi+(DiSi)\text{match}_i = M_i + (D_i \odot S_i) — a don't-care (Mi=1M_i=1) always matches.

Connections

  • Static RAM (SRAM) — CAM cell = SRAM storage + comparison logic
  • Cache Memory — fully-associative caches use CAM for tag matching
  • Translation Lookaside Buffer (TLB) — associative address translation
  • Priority Encoder — resolves multiple simultaneous matches
  • Boolean Algebra & De Morgan's Laws — match-line = NOT(OR of mismatches) = AND of equalities
  • Longest Prefix Matching — TCAM + priority for IP routing
  • Dynamic Logic (Precharge/Evaluate) — the electrical style of the match line

Concept Map

flips the question

input

output

searches

achieves

trades

used by

needs

type

type

enables

wired-OR discharges

De Morgan

Normal RAM: address to data

CAM: data to address

Search key word

Match line address

Parallel comparison all rows

O of 1 lookup time

More silicon and power

Router forwarding table

Longest-prefix match

Binary CAM exact match

Ternary CAM with don't-care X

Per-bit mismatch = XOR

Match line stays HIGH if all bits equal

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, normal RAM aur CAM ulte kaam karte hain. RAM mein tum address dete ho aur woh data return karta hai — jaise "seat number 42 pe kaun baitha hai?". CAM ismein reverse hai: tum data (search key) dete ho aur woh address batata hai — "Sam naam ka banda kis seat pe hai?". Isliye CAM ko associative memory bhi kehte hain.

Ab magic ye hai ki CAM saari rows ko ek saath, ek hi cycle mein compare karta hai — sequential search nahi. Har row ke saath ek match line hoti hai jo pehle HIGH charge kar di jaati hai. Har cell mein XOR/XNOR logic hota hai: agar koi bhi ek bit mismatch ho gaya, woh cell match line ko LOW kheench deta hai. Matlab match line tabhi HIGH rehti hai jab saare bits match karein. Ye "wired-OR" trick hi CAM ko O(1)O(1) fast banati hai.

Ek important cheez: ek se zyada rows match ho sakti hain, isliye ek priority encoder lagta hai jo ek final address chunta hai. Aur TCAM mein ek extra state hota hai — X (don't-care) — jo 0 aur 1 dono se match karta hai. Ye routers mein longest-prefix matching ke liye zaroori hai, jaise 192.168.*.*.

Cost ka funda samajh lo: har CAM cell mein extra transistors (BCAM ~9-10, TCAM ~16) lagte hain, aur har search mein saari match lines charge hoti hain — isliye CAM mehenga aur power-hungry hai. Isiliye ise sirf wahan use karte hain jahan super-fast parallel search chahiye: TLB, fully-associative cache, aur network routers. Baaki jagah normal RAM hi theek hai.

Go deeper — visual, from zero

Test yourself — Memory Technologies

Connections