TCAM stands for Ternary Content Addressable Memory.
In this, instead of data, the search returns address and number of entries that match the search key.
What the code does:
- TCAM is 16 locations deep.
- TCAM class contains 3 methods.
- load_tcam ( ) - It takes data and address as inputs and loads the TCAM memory
- search_tcam ( ) - Lets dive more into this method, this is where things are handled differently when compared with the normal SRAM's. The inputs contain key and mask. Key is used to search all the entries and find the matching addresses. If there is a single hit, output bit match is set to 1. For multiple entries, match_entries field corresponding to the hit index is set to 1. Mask is what differentiates a normal CAM with TCAM. When MASK field is set to 1, the entry is always treated as matched irrespective of the contents of the entry. Finally address ( output ), this can be set as per the priority order set. In this case, I choose last match as the highest priority.
- clear_tcam() - To clear all contents of the TCAM memory.
class tcam; logic [31:0] tcam_mem[16]; virtual task clear_tcam ( ); foreach (tcam_mem[i]) tcam_mem[i] = 0; endtask: clear_tcam virtual task load_tcam ( input logic [31:0] data , input logic [31:0] addr ); tcam_mem[addr] = data; endtask: load_tcam virtual task search_tcam (
input logic [31:0] key,
input logic [31:0] mask,
output bit match,
output bit [15:0] match_entries,
output bit [31:0] address );
for ( int i=0;i<16;i++ ) begin //{ // mask[i] == 1, don't care, always matches irrespective of key if(tcam_mem[i] == key || mask[i]) begin //{ match = 1; match_entries[i] = 1; address = i; // You can choose any priority, I choose priority of Last match address end //} end //} endtask: search_tcam endclass: tcam module top; tcam t; initial begin logic match; logic [31:0] match_entries; logic [31:0] match_address; t = new; t.load_tcam(10,0); t.load_tcam(20,1); t.load_tcam(25,2); t.load_tcam(30,3); t.load_tcam(20,4); t.search_tcam(20,0,match,match_entries,match_address); $display ("Match:%0d Entries:%0b Address:%0d",match, match_entries,match_address ); end endmodule: top
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Jul 26 13:19 2025
Match:1 Entries:10010 Address:4
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.530 seconds; Data structure size: 0.0Mb
Sat Jul 26 13:19:05 2025
Done
No comments:
Post a Comment