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.
We want a signal that says "stored bit D ≠ search bit S". The logic function for inequality of two bits is XOR:
mismatch=D⊕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.
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 n bits, the match line stays HIGH only if no bit discharges it:
where ⊙ 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.
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.
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) 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.