Skip to content

Move https://github.com/The-OpenROAD-Project/FakeRAM2.0 into ORFS - #4393

Merged
maliberty merged 2 commits into
The-OpenROAD-Project:masterfrom
The-OpenROAD-Project-staging:add-fakeram2.0
Aug 5, 2026
Merged

maliberty merged 2 commits into
The-OpenROAD-Project:masterfrom
The-OpenROAD-Project-staging:add-fakeram2.0

Conversation

@maliberty

Copy link
Copy Markdown
Member

No need to keep it separate anymore.

No need to keep it separate anymore.

Signed-off-by: Matt Liberty <mliberty@precisioninno.com>
@maliberty maliberty self-assigned this Aug 4, 2026
@maliberty

Copy link
Copy Markdown
Member Author

@oharboe @jeffng-or FYI

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces FakeRAM2.0, a fake memory compiler that generates PDK-compatible memory models (LEF, Liberty, and Verilog) from configuration files or custom spreadsheets. The feedback highlights several critical issues, including potential TypeError and KeyError exceptions in the process and port organizer utilities, syntax errors in the generated Verilog/SystemVerilog outputs, and risky overridable method calls in the Memory constructor. Additionally, the reviewer suggested various style and encapsulation improvements, such as using cls for class methods, avoiding implicit boolean-to-integer conversions, adhering to public getters in TimingData, and using array-based execution in bash scripts to prevent word splitting.

Comment on lines +88 to +89
contacted_poly_pitch_um = self.contacted_poly_pitch_nm / 1000
fin_pitch_um = self.fin_pitch_nm / 1000

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If bitcell_width_um and bitcell_height_um are not defined, and either contacted_poly_pitch_nm or fin_pitch_nm is missing from the configuration, this code will raise a TypeError when attempting to divide None by 1000. It is safer to check if these values are defined and raise a descriptive ValueError instead.

        else:
            if self.contacted_poly_pitch_nm is None or self.fin_pitch_nm is None:
                raise ValueError("contacted_poly_pitch_nm and fin_pitch_nm must be defined if bitcell size is not specified.")
            contacted_poly_pitch_um = self.contacted_poly_pitch_nm / 1000
            fin_pitch_um = self.fin_pitch_nm / 1000

Comment on lines +90 to +91
last_char = port_data["name"][-1]
self._rw_groups[last_char][port_data["type"]] = port_data

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If len(address_busses) > 1, this loop attempts to group various port types by their suffix (last_char). However, if a port name does not end with one of the address bus suffixes (e.g., shared or unsuffixed ports like toggle_power), last_char will not be present in self._rw_groups, causing a KeyError. Consider checking if last_char exists in self._rw_groups first, and falling back to misc ports if it does not.

Suggested change
last_char = port_data["name"][-1]
self._rw_groups[last_char][port_data["type"]] = port_data
last_char = port_data["name"][-1]
if last_char in self._rw_groups:
self._rw_groups[last_char][port_data["type"]] = port_data
else:
if "msb" in port_data:
self._misc_busses.append(port_data)
else:
self._misc_ports.append(port_data)

Comment on lines +82 to +97
if index != 0:
out_fh.write(",\n")
if rw_port_group.get_write_enable_name():
out_fh.write(f" {rw_port_group.get_write_enable_name()},\n")
if rw_port_group.get_address_bus_name():
out_fh.write(f" {rw_port_group.get_address_bus_name()},\n")
if rw_port_group.get_data_input_bus_name():
out_fh.write(f" {rw_port_group.get_data_input_bus_name()},\n")
if rw_port_group.get_data_output_bus_name():
out_fh.write(f" {rw_port_group.get_data_output_bus_name()},\n")
if rw_port_group.get_clock_name():
out_fh.write(f" {rw_port_group.get_clock_name()}")
for port_name in rw_port_group.get_related_pins():
out_fh.write(f" {port_name}")
for bus_name in rw_port_group.get_related_busses():
out_fh.write(f" {bus_name}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

When related_pins or related_busses are present, they are written directly after the clock pin without any separator (comma or newline), resulting in syntactically invalid Verilog (e.g., clk_a related_pin). Refactoring this method to collect all active port declarations in a list and joining them with ",\n" is much cleaner and avoids syntax errors.

Suggested change
if index != 0:
out_fh.write(",\n")
if rw_port_group.get_write_enable_name():
out_fh.write(f" {rw_port_group.get_write_enable_name()},\n")
if rw_port_group.get_address_bus_name():
out_fh.write(f" {rw_port_group.get_address_bus_name()},\n")
if rw_port_group.get_data_input_bus_name():
out_fh.write(f" {rw_port_group.get_data_input_bus_name()},\n")
if rw_port_group.get_data_output_bus_name():
out_fh.write(f" {rw_port_group.get_data_output_bus_name()},\n")
if rw_port_group.get_clock_name():
out_fh.write(f" {rw_port_group.get_clock_name()}")
for port_name in rw_port_group.get_related_pins():
out_fh.write(f" {port_name}")
for bus_name in rw_port_group.get_related_busses():
out_fh.write(f" {bus_name}")
ports = []
if rw_port_group.get_write_enable_name():
ports.append(f" {rw_port_group.get_write_enable_name()}")
if rw_port_group.get_address_bus_name():
ports.append(f" {rw_port_group.get_address_bus_name()}")
if rw_port_group.get_data_input_bus_name():
ports.append(f" {rw_port_group.get_data_input_bus_name()}")
if rw_port_group.get_data_output_bus_name():
ports.append(f" {rw_port_group.get_data_output_bus_name()}")
if rw_port_group.get_clock_name():
ports.append(f" {rw_port_group.get_clock_name()}")
for port_name in rw_port_group.get_related_pins():
ports.append(f" {port_name}")
for bus_name in rw_port_group.get_related_busses():
ports.append(f" {bus_name}")
if ports:
if index != 0:
out_fh.write(",\n")
out_fh.write(",\n".join(ports))

Comment on lines +166 to +197
def export_bb_port_decl_set(self, rw_port_group, out_fh, index):
"""Writes the SystemVerilog port declarations"""

mem = self.get_memory()
addr_bus_msb = mem.get_addr_bus_msb()
data_bus_msb = mem.get_data_bus_msb()
if index != 0:
out_fh.write(",\n")
if rw_port_group.get_write_enable_name():
out_fh.write(f" input {rw_port_group.get_write_enable_name()},\n")
if rw_port_group.get_address_bus_name():
out_fh.write(
f" input [{addr_bus_msb}:0] {rw_port_group.get_address_bus_name()},\n"
)
if rw_port_group.get_data_input_bus_name():
out_fh.write(
f" input [{data_bus_msb}:0] {rw_port_group.get_data_input_bus_name()},\n"
)
if rw_port_group.get_data_output_bus_name():
out_fh.write(
f" output reg [{data_bus_msb}:0] {rw_port_group.get_data_output_bus_name()},\n"
)
for pin_name in rw_port_group.get_related_pins():
out_fh.write(
f" input {pin_name},\n"
)
for bus_name, bus_data in rw_port_group.get_related_busses().items():
out_fh.write(
f" input [{bus_data['msb']}:{bus_data['lsb']}] {bus_name},\n"
)
if rw_port_group.get_clock_name():
out_fh.write(f" input {rw_port_group.get_clock_name()}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If clock_name is None, the last item written by export_bb_port_decl_set will have a trailing comma. When followed by misc ports in export_blackbox (which write leading commas), this results in a syntax error due to double commas ,,. Refactoring export_bb_port_decl_set to return a list of strings allows export_blackbox to cleanly join all declarations with ",\n".

    def export_bb_port_decl_set(self, rw_port_group):
        """Returns a list of SystemVerilog port declarations"""

        mem = self.get_memory()
        addr_bus_msb = mem.get_addr_bus_msb()
        data_bus_msb = mem.get_data_bus_msb()
        ports = []
        if rw_port_group.get_write_enable_name():
            ports.append(f"    input {rw_port_group.get_write_enable_name()}")
        if rw_port_group.get_address_bus_name():
            ports.append(f"    input [{addr_bus_msb}:0] {rw_port_group.get_address_bus_name()}")
        if rw_port_group.get_data_input_bus_name():
            ports.append(f"    input [{data_bus_msb}:0] {rw_port_group.get_data_input_bus_name()}")
        if rw_port_group.get_data_output_bus_name():
            ports.append(f"    output reg [{data_bus_msb}:0] {rw_port_group.get_data_output_bus_name()}")
        for pin_name in rw_port_group.get_related_pins():
            ports.append(f"    input {pin_name}")
        for bus_name, bus_data in rw_port_group.get_related_busses().items():
            ports.append(f"    input [{bus_data['msb']}:{bus_data['lsb']}] {bus_name}")
        if rw_port_group.get_clock_name():
            ports.append(f"    input {rw_port_group.get_clock_name()}")
        return ports

Comment on lines +200 to +212
"""Writes the blackbox content to the output stream"""

self.export_bb_header(out_fh)
for index, rw_port_group in enumerate(self.get_memory().get_rw_port_groups()):
self.export_bb_port_decl_set(rw_port_group, out_fh, index)
for bus_name, bus_data in self.get_memory().get_misc_busses().items():
out_fh.write(
f",\n input [{bus_data['msb']}:{bus_data['lsb']}] {bus_name}"
)
for pin in sorted(self.get_memory().get_misc_ports()):
out_fh.write(f",\n input {pin}")
out_fh.write("\n")
self.export_bb_footer(out_fh)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Refactor export_blackbox to collect all port declarations in a list and join them with ",\n" to avoid potential double comma syntax errors when clock_name is None.

Suggested change
"""Writes the blackbox content to the output stream"""
self.export_bb_header(out_fh)
for index, rw_port_group in enumerate(self.get_memory().get_rw_port_groups()):
self.export_bb_port_decl_set(rw_port_group, out_fh, index)
for bus_name, bus_data in self.get_memory().get_misc_busses().items():
out_fh.write(
f",\n input [{bus_data['msb']}:{bus_data['lsb']}] {bus_name}"
)
for pin in sorted(self.get_memory().get_misc_ports()):
out_fh.write(f",\n input {pin}")
out_fh.write("\n")
self.export_bb_footer(out_fh)
def export_blackbox(self, out_fh):
"""Writes the blackbox content to the output stream"""
self.export_bb_header(out_fh)
ports = []
for rw_port_group in self.get_memory().get_rw_port_groups():
ports.extend(self.export_bb_port_decl_set(rw_port_group))
for bus_name, bus_data in self.get_memory().get_misc_busses().items():
ports.append(f" input [{bus_data['msb']}:{bus_data['lsb']}] {bus_name}")
for pin in sorted(self.get_memory().get_misc_ports()):
ports.append(f" input {pin}")
out_fh.write(",\n".join(ports))
out_fh.write("\n")
self.export_bb_footer(out_fh)

self.physical.snap_to_grid(
self.process.snap_width_nm, self.process.snap_height_nm
)
num_pins = self.get_num_pins()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Calling the overridable method self.get_num_pins() inside the base class Memory.__init__ constructor is risky. If a subclass overrides get_num_pins() and accesses any subclass-specific attributes initialized in its own __init__ (which runs after super().__init__()), it will raise an AttributeError. It is safer to perform physical dimension calculations in a separate initialization method called after the object is fully constructed.

else:
fid.write(f" LAYER {layer_name} ;\n")
for rect in layer_data["rects"]:
fid.write(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Inconsistent coordinate formatting in write_obs. rect[0] and rect[1] are printed without formatting, while rect[2] and rect[3] are printed with :.5f. Format all four coordinates with :.5f for consistency and precision.

                    f"    RECT {rect[0]:.5f} {rect[1]:.5f} {rect[2]:.5f} {rect[3]:.5f} ;\n"

Comment on lines +267 to +272
timing_data = self.get_memory().get_timing_data()
max_load = timing_data.max_load
slew_indices = timing_data.slew_indices
load_indices = timing_data.load_indices
min_slew = timing_data.min_slew
max_slew = timing_data.max_slew

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Directly accessing protected/private-like attributes of TimingData (e.g., timing_data.max_load, timing_data.slew_indices, etc.) violates encapsulation. Use the public getter methods defined in TimingData instead.

Suggested change
timing_data = self.get_memory().get_timing_data()
max_load = timing_data.max_load
slew_indices = timing_data.slew_indices
load_indices = timing_data.load_indices
min_slew = timing_data.min_slew
max_slew = timing_data.max_slew
max_load = timing_data.get_max_load()
slew_indices = timing_data.get_slew_indices_str()
load_indices = timing_data.get_load_indices_str()
min_slew = timing_data.get_min_slew()
max_slew = timing_data.get_max_slew()
tcq = timing_data.get_access_time()

Comment on lines +5 to +16
if [[ -v COVERAGE_RUN ]]; then
rm -rf .coverage*
cmd_preamble="coverage run --parallel-mode"
else
cmd_preamble=""
fi

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

for test_file in $SCRIPT_DIR/*_test.py; do
$cmd_preamble $test_file
done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using unquoted variables like $cmd_preamble and $test_file can lead to word splitting and globbing issues. It is much safer and more robust to use a bash array for cmd_preamble and execute it with "${cmd_preamble[@]}".

Suggested change
if [[ -v COVERAGE_RUN ]]; then
rm -rf .coverage*
cmd_preamble="coverage run --parallel-mode"
else
cmd_preamble=""
fi
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
for test_file in $SCRIPT_DIR/*_test.py; do
$cmd_preamble $test_file
done
if [[ -v COVERAGE_RUN ]]; then
rm -rf .coverage*
cmd_preamble=(coverage run --parallel-mode)
else
cmd_preamble=(python3)
fi
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
for test_file in "$SCRIPT_DIR"/*_test.py; do
"${cmd_preamble[@]}" "$test_file"
done

"""Ensures that the results directory exists"""

p = str(Path(output_dir).expanduser().resolve(strict=False))
results_dir = os.sep.join([p, memory_name])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using os.sep.join to construct paths is less robust and less idiomatic than using os.path.join or Path objects, which handle platform-specific path separators automatically.

Suggested change
results_dir = os.sep.join([p, memory_name])
results_dir = os.path.join(p, memory_name)

Signed-off-by: Matt Liberty <mliberty@precisioninno.com>
@maliberty

Copy link
Copy Markdown
Member Author

I fixed the black python formatting but will leave the general code comments for future work as this is just a code move not a refactor.

@maliberty
maliberty merged commit 9fae12c into The-OpenROAD-Project:master Aug 5, 2026
8 checks passed
@maliberty
maliberty deleted the add-fakeram2.0 branch August 5, 2026 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant