generateParallelAssociativeMemoryModelVerilog function
- ParallelAssociativeMemoryModelDescription description,
- String moduleName
Generates a simulation-only implementation of the parallel CAM contract.
Implementation
String generateParallelAssociativeMemoryModelVerilog(
ParallelAssociativeMemoryModelDescription description, String moduleName) {
if (!Sanitizer.isSanitary(moduleName)) {
throw ArgumentError.value(moduleName, 'moduleName', 'Invalid identifier.');
}
final d = description;
final selectWidth = max(1, (d.entryCount - 1).bitLength);
final countWidth = d.entryCount.bitLength;
String declaration(String direction, MapEntry<String, int> port) =>
'$direction logic [${port.value - 1}:0] ${port.key}';
final ports = [
for (final port in d.inputPorts.entries) declaration('input', port),
for (final port in d.outputPorts.entries) declaration('output', port),
];
final source = StringBuffer('''
module $moduleName (
${ports.join(',\n ')}
);
logic [${d.keyWidth - 1}:0] keys [0:${d.entryCount - 1}];
logic valid [0:${d.entryCount - 1}];
logic old_clock;
logic reset_seen = 1'b0;
initial old_clock=clk;
function automatic [${d.keyWidth - 1}:0] normalize_key(input [${d.keyWidth - 1}:0] value);
for (integer b=0;b<${d.keyWidth};b=b+1)
normalize_key[b] = value[b] === 1'bz ? 1'bx : value[b];
endfunction
function automatic normalize_bit(input logic value);
normalize_bit = value === 1'bz ? 1'bx : value;
endfunction
always @(clk) begin : update_state
logic [${d.keyWidth - 1}:0] next_key;
logic next_valid, selected;
if (\$isunknown(old_clock) || \$isunknown(clk)) begin
for (integer i=0;i<${d.entryCount};i=i+1) begin
keys[i]='x; valid[i]=1'bx;
end
end else if (old_clock == 1'b0 && clk == 1'b1) begin
if (reset === 1'b1) begin
reset_seen=1'b1;
for (integer i=0;i<${d.entryCount};i=i+1) begin
keys[i]='0; valid[i]=1'b0;
end
end else if (reset !== 1'b0) begin
for (integer i=0;i<${d.entryCount};i=i+1) begin
keys[i]='x; valid[i]=1'bx;
end
end else begin
for (integer i=0;i<${d.entryCount};i=i+1) begin
next_key=keys[i]; next_valid=valid[i];
''');
final order = d.collision == AssociativeUpdateCollisionPolicy.lowestPortWins
? List.generate(d.updateCount, (i) => d.updateCount - i - 1)
: List.generate(d.updateCount, (i) => i);
for (final port in order) {
source.writeln('''
selected=update_en_$port & (update_index_$port == ${d.indexWidth}'(i));
if (selected === 1'b1) begin
next_key=normalize_key(update_key_$port);
next_valid=normalize_bit(update_entryValid_$port);
end else if (selected !== 1'b0) begin
next_key='x; next_valid=1'bx;
end
''');
}
source.writeln('''
keys[i]=next_key; valid[i]=next_valid;
end
end
end
old_clock=clk;
end
''');
if (d.trackValidity) {
source.writeln('''
always @* begin
valid_count='0;
for (integer i=0;i<${d.entryCount};i=i+1) begin
valid_mask[i]=valid[i];
valid_count=valid_count + {{${countWidth - 1}{1'b0}},valid[i]};
end
full=&valid_mask;
empty=~|valid_mask;
end
''');
}
for (var port = 0; port < d.matchVectors.length; port++) {
final comparison =
d.selection == AssociativeMatchSelection.lowestIndex ? '<' : '>';
source.writeln('''
always @* begin : lookup_$port
logic [${d.entryCount - 1}:0] match_bits;
logic [${selectWidth - 1}:0] chosen, candidate;
logic found, matched;
match_bits='0; chosen='0; found=1'b0;
for (integer i=0;i<${d.entryCount};i=i+1) begin
candidate=$selectWidth'(i);
matched=valid[i] & (lookup_key_$port == keys[i]);
match_bits[i]=matched;
chosen=(matched & (~found | (candidate $comparison chosen))) ? candidate : chosen;
found=found | matched;
end
lookup_hit_$port=lookup_en_$port & found;
lookup_index_$port=lookup_hit_$port ? ${d.indexWidth}'(chosen) : '0;
${d.matchVectors[port] ? "lookup_matchVector_$port=lookup_en_$port ? match_bits : '0;" : ''}
end
''');
}
for (var port = 0; port < d.sideReadCount; port++) {
final suffix = port == 0 ? '' : '_$port';
source.writeln('''
always @* begin
if (\$isunknown(side_read_index$suffix)) begin
side_read_key$suffix='x; side_read_valid$suffix=1'bx;
end else if (side_read_index$suffix >= ${d.indexWidth + 1}'(${d.entryCount})) begin
side_read_key$suffix='0; side_read_valid$suffix=1'b0;
end else begin
side_read_key$suffix=keys[side_read_index$suffix];
side_read_valid$suffix=valid[side_read_index$suffix];
end
end
''');
}
source.writeln('''
logic [${d.keyWidth - 1}:0] loaded_keys [0:${d.entryCount - 1}];
logic loaded_valid [0:${d.entryCount - 1}];
task automatic write_storage(input integer fd);
begin
\$fwrite(fd,"RHCL_ASSOC_STORAGE_V1 ${d.keyWidth} ${d.entryCount}\\n");
for (integer i=0;i<${d.entryCount};i=i+1)
\$fwrite(fd,"%b %b\\n",valid[i],keys[i]);
end
endtask
task automatic read_storage(input integer fd);
integer status;
string magic, width_text, count_text, valid_text, key_text, extra;
begin
status=\$fscanf(fd,"%s %s %s",magic,width_text,count_text);
if (status!=3 || magic!="RHCL_ASSOC_STORAGE_V1" ||
width_text!="${d.keyWidth}" || count_text!="${d.entryCount}")
\$fatal(1,"Invalid associative storage header");
for (integer i=0;i<${d.entryCount};i=i+1) begin
status=\$fscanf(fd,"%s %s",valid_text,key_text);
if (status!=2 || valid_text.len()!=1 || key_text.len()!=${d.keyWidth})
\$fatal(1,"Invalid associative storage entry");
for (integer b=0;b<valid_text.len();b=b+1)
if (valid_text[b]!="0" && valid_text[b]!="1" &&
valid_text[b]!="x" && valid_text[b]!="X")
\$fatal(1,"Invalid associative validity");
for (integer b=0;b<key_text.len();b=b+1)
if (key_text[b]!="0" && key_text[b]!="1" &&
key_text[b]!="x" && key_text[b]!="X")
\$fatal(1,"Invalid associative key");
status=\$sscanf(valid_text,"%b",loaded_valid[i]);
if (status!=1) \$fatal(1,"Invalid associative validity");
status=\$sscanf(key_text,"%b",loaded_keys[i]);
if (status!=1) \$fatal(1,"Invalid associative key");
end
status=\$fscanf(fd,"%s",extra);
if (status==1) \$fatal(1,"Trailing associative storage data");
end
endtask
task automatic commit_storage;
begin
for (integer i=0;i<${d.entryCount};i=i+1) begin
keys[i]=loaded_keys[i]; valid[i]=loaded_valid[i];
end
end
endtask
task automatic save_storage(input string path);
integer fd;
begin
fd=\$fopen(path,"w");
if (fd==0) \$fatal(1,"Cannot open associative storage file");
write_storage(fd);
\$fclose(fd);
end
endtask
task automatic load_storage(input string path);
integer fd;
begin
fd=\$fopen(path,"r");
if (fd==0) \$fatal(1,"Cannot open associative storage file");
read_storage(fd);
\$fclose(fd);
commit_storage();
end
endtask
task automatic save_execution(input string path);
integer fd;
begin
if (\$isunknown(clk) || old_clock !== clk)
\$fatal(1,"Execution image requires a settled known clock");
fd=\$fopen(path,"w");
if (fd==0) \$fatal(1,"Cannot open associative execution file");
\$fwrite(fd,"${d.portableExecutionIdentity} %b %b\\n",clk,reset_seen);
write_storage(fd);
\$fclose(fd);
end
endtask
task automatic load_execution(input string path);
integer fd, status;
string identity, phase, reset_history;
begin
fd=\$fopen(path,"r");
if (fd==0) \$fatal(1,"Cannot open associative execution file");
status=\$fscanf(fd,"%s %s %s",identity,phase,reset_history);
if (status!=3 || identity!="${d.portableExecutionIdentity}" ||
(phase!="0" && phase!="1") ||
(reset_history!="0" && reset_history!="1"))
\$fatal(1,"Invalid associative execution header");
if (\$isunknown(clk) || old_clock !== clk ||
(phase=="0" && clk !== 1'b0) ||
(phase=="1" && clk !== 1'b1))
\$fatal(1,"Execution image clock does not match");
read_storage(fd);
\$fclose(fd);
commit_storage();
reset_seen=(reset_history=="1");
end
endtask
endmodule
''');
return source.toString();
}