42b7f028b1bda677faf7c9dbb04b1a3abf90090d
1 from nmigen
import Array
, Module
, Signal
2 from nmigen
.lib
.coding
import Encoder
3 from nmigen
.compat
.fhdl
.structure
import ClockDomain
5 from CamEntry
import CamEntry
7 # Content Addressable Memory (CAM)
8 # The purpose of this module is to quickly look up whether an entry exists
9 # given a certain key and return the mapped data.
10 # This module when given a key will search for the given key
11 # in all internal entries and output whether a match was found or not.
12 # If an entry is found the data will be returned and data_hit is HIGH,
13 # if it is not LOW is asserted on data_hit. When given a write
14 # command it will write the given key and data into the given cam entry index.
15 # Entry managment should be performed one level above this block as lookup is
20 # key_size: (bit count) The size of the key
21 # data_size: (bit count) The size of the data
22 # cam_size: (entry count) The number of entries int he CAM
23 def __init__(self
, key_size
, data_size
, cam_size
):
25 self
.cam_size
= cam_size
26 self
.entry_array
= Array(CamEntry(key_size
, data_size
) \
27 for x
in range(cam_size
))
30 self
.command
= Signal(2) # 00 => NA 01 => Read 10 => Write 11 => Search
31 self
.address
= Signal(max=cam_size
) # address of CAM Entry to write/read
32 self
.key_in
= Signal(key_size
) # The key to search for or to be written
33 self
.data_in
= Signal(key_size
) # The data to be written
36 self
.data_hit
= Signal(1) # Denotes a key data pair was stored at key_in
37 self
.data_out
= Signal(data_size
) # The data mapped to by key_in
39 def elaborate(self
, platform
=None):
42 m
.submodules
.encoder
= encoder
= Encoder(self
.cam_size
)
43 m
.submodules
+= self
.entry_array
45 # Set the key value for every CamEntry
46 for index
in range(self
.cam_size
):
47 with m
.Switch(self
.command
):
48 # Read and Search both read from the CamEntry
50 m
.d
.comb
+= self
.entry_array
[index
].command
.eq(1)
51 # Write only to one entry
53 with m
.If(self
.address
== index
):
54 m
.d
.comb
+= self
.entry_array
[index
].command
.eq(2)
56 m
.d
.comb
+= self
.entry_array
[index
].command
.eq(0)
59 m
.d
.comb
+= self
.entry_array
[index
].command
.eq(0)
62 self
.entry_array
[index
].key_in
.eq(self
.key_in
),
63 self
.entry_array
[index
].data_in
.eq(self
.data_in
),
64 encoder
.i
[index
].eq(self
.entry_array
[index
].match
)
68 with m
.Switch(self
.command
):
73 self
.data_out
.eq(self
.entry_array
[self
.address
].data
)
79 self
.entry_array
[self
.address
].key_in
.eq(self
.key_in
),
80 self
.entry_array
[self
.address
].data_in
.eq(self
.data_in
)
84 with m
.If(encoder
.n
== 0):
87 self
.data_out
.eq(self
.entry_array
[encoder
.o
].data
)
90 m
.d
.comb
+= self
.data_hit
.eq(0)