1 from nmigen
import Array
, Module
, Signal
2 from nmigen
.lib
.coding
import Encoder
, Decoder
3 from nmigen
.cli
import main
#, verilog
5 from CamEntry
import CamEntry
8 """ Content Addressable Memory (CAM)
10 The purpose of this module is to quickly look up whether an
11 entry exists given a certain key and return the mapped data.
12 This module when given a key will search for the given key
13 in all internal entries and output whether a match was found or not.
14 If an entry is found the data will be returned and data_hit is HIGH,
15 if it is not LOW is asserted on data_hit. When given a write
16 command it will write the given key and data into the given cam
18 Entry managment should be performed one level above this block
19 as lookup is performed within.
22 The search, write, and reset operations take one clock cycle
23 to complete. Performing a read immediately after a search will cause
24 the read to be ignored.
27 def __init__(self
, data_size
, cam_size
):
29 * data_size: (bit count) The size of the data
30 * cam_size: (entry count) The number of entries int he CAM
34 self
.cam_size
= cam_size
35 self
.encoder
= Encoder(cam_size
)
36 self
.decoder
= Decoder(cam_size
)
37 self
.entry_array
= Array(CamEntry(data_size
) \
38 for x
in range(cam_size
))
41 # 000 => NA 001 => Read 010 => Write 011 => Search
42 # 100 => Reset 101, 110, 111 => Reserved
43 self
.command
= Signal(3)
44 self
.enable
= Signal(1)
45 self
.data_in
= Signal(data_size
) # The data to be written
46 self
.data_mask
= Signal(data_size
) # mask for ternary writes
47 self
.write_enable
= Signal(1) # write
48 self
.address
= Signal(max=cam_size
) # address of CAM Entry to write/read
51 self
.data_hit
= Signal(1) # Denotes a key data pair was stored at key_in
52 self
.data_out
= Signal(data_size
) # The data mapped to by key_in
54 def elaborate(self
, platform
=None):
56 # Encoder is used to selecting what data is output when searching
57 m
.submodules
+= self
.encoder
58 # Decoder is used to select which entry will be written to
59 m
.submodules
+= self
.decoder
60 # Don't forget to add all entries to the submodule list
61 entry_array
= self
.entry_array
62 m
.submodules
+= entry_array
66 self
.decoder
.i
.eq(self
.address
),
70 # Set the key value for every CamEntry
71 for index
in range(self
.cam_size
):
72 with m
.Switch(self
.command
):
73 # Read from a single entry
75 m
.d
.comb
+= entry_array
[index
].command
.eq(1)
76 # Only read if an encoder value is not ready
77 with m
.If(self
.decoder
.o
[index
] & self
.encoder
.n
):
78 m
.d
.comb
+= self
.data_out
.eq(entry_array
[index
].data
)
79 # Write only to one entry
81 # Address is decoded and selects which
82 # entry will be written to
83 with m
.If(self
.decoder
.o
[index
]):
84 m
.d
.comb
+= entry_array
[index
].command
.eq(2)
86 m
.d
.comb
+= entry_array
[index
].command
.eq(0)
89 m
.d
.comb
+= entry_array
[index
].command
.eq(1)
92 m
.d
.comb
+= entry_array
[index
].command
.eq(3)
95 m
.d
.comb
+= entry_array
[index
].command
.eq(0)
98 entry_array
[index
].data_in
.eq(self
.data_in
),
99 self
.encoder
.i
[index
].eq(entry_array
[index
].match
)
102 # Process out data based on encoder address
103 with m
.If(self
.encoder
.n
== 0):
106 self
.data_out
.eq(entry_array
[self
.encoder
.o
].data
)
109 m
.d
.comb
+= self
.data_hit
.eq(0)
113 if __name__
== '__main__':
115 main(cam
, ports
=[cam
.command
, cam
.address
,
116 cam
.data_in
, cam
.data_hit
,