# Data Structures to be used for a TCAM

**URL:** <https://verificationacademy.com/forums/t/data-structures-to-be-used-for-a-tcam/36296>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, associative-array, accessing-the-associative-array-elements, systemverilogmultidimentional-arraya-memory, Content-Addressable-Memory\
**Created:** [July 14, 2019, 5:53pm UTC](https://verificationacademy.com/forums/t/data-structures-to-be-used-for-a-tcam/36296 "2019-07-14T17:53:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pranavsamsung](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/p/76d3ee/32.png) [@pranavsamsung](https://verificationacademy.com/forums/u/pranavsamsung)\
**Post date:** [July 14, 2019, 5:53pm UTC](https://verificationacademy.com/forums/t/data-structures-to-be-used-for-a-tcam/36296/1 "2019-07-14T17:53:36Z")

</div>

Hi

I have a TCAM (ternary Content Adressible memory) and Id like to see what data structures I can use to describe it.

For simplicity lets say I have data[31:0] and addr[31:0] and mask[31:0].

I need to be able to query the address based on the data(so I guess we can use an associative array).

but since we also need to use the mask where if the mask bit is set then the corresponding addr bit is checked else it is a don’t care.

e.g.  
data[31:0] = 0x0000\_ABCD  
addr[31:0] = 0xCAFE\_CAFE  
mask[31:0] = 0x0000\_FFFF

Now a query for data[31:0] = 0x???\_ABCD should return 0xCAFE\_CAFE where ? represents 0,1,X (since mask is 0 for msb 16 bits)

I thought of using an assoc array like this

I saw an example in this post below where u have used an assoc array as shown below.  
([Multi-dimensional arrays | Verification Academy](https://verificationacademy.com/forums/systemverilog/multi-dimensional-arrays-0))

but im not sure if that is the right way to go about for this problem.

Thanks for the help

Pranav

---

<div class="post-metadata">

**Author:** ![Digeshkumar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/49beb7/32.png) [@Digeshkumar](https://verificationacademy.com/forums/u/Digeshkumar)\
**Post date:** [August 6, 2019, 11:29am UTC](https://verificationacademy.com/forums/t/data-structures-to-be-used-for-a-tcam/36296/2 "2019-08-06T11:29:56Z")

</div>

_In reply to [pranavsamsung](https://verificationacademy.com/forums/t/data-structures-to-be-used-for-a-tcam/36296):_

I am not sure I understood your question right or not but I assume you have 3 variables of 32 bits each and you want to know which is best way to handle that without taking multiple arrays ?

You can think of single associative array with one of the variable being it’s key(Index) and storage of 64 bit data. You can concatenation remaining 2 variables as single location data.

For example:  
bit [63:0] addr\_n\_mask[int]; // Here Index is address and data\_n\_mask[63:32] is addr, data\_n\_mask[31:0] is mask

---

<div class="post-metadata">

**Author:** ![javatea](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@javatea](https://verificationacademy.com/forums/u/javatea)\
**Post date:** [August 7, 2019, 10:15pm UTC](https://verificationacademy.com/forums/t/data-structures-to-be-used-for-a-tcam/36296/3 "2019-08-07T22:15:01Z")

</div>

```verilog

// I think you want to query address based on {data & mask}. 
// and I assume returning address might be greater than 1
// using associative array : 
// - key_index is "data & mask" 
// - item is "32-bit queue" to represent multiple address items      
// associative array cost is fairly huge ... be careful

    typedef struct packed {
      bit [31:0] data;
      bit [31:0] mask;
    } data_s;

            data_s mem[];
            bit [31:0] lookup_table [bit[31:0]][$]

            xmem = new[10];
            // preparing memory
            for (int i = 0; i < mem.size(); i++) begin
                mem[i].data = $urandom_range(0,5);
                mem[i].mask = '1;
            end
            // preparing lookup table
            for (int j = 0; j < mem.size(); j++) begin
                key = mem[j].data & mem[j].mask;
                if (lookup_tbl.exists(key)) begin
                    lookup_tbl[key].push_back(j); 
                end 
                else begin
                    addr_q = {j};
                    lookup_tbl[key] = addr_q;
                end
            end

```
