Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto format constants in decimal or hex #1913

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions litex/gen/fhdl/verilog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from litex.gen import LiteXContext
from litex.gen.fhdl.namer import build_signal_namespace
from litex.gen.fhdl.hierarchy import LiteXHierarchyExplorer
from litex.gen.format import format_constant

from litex.build.tools import get_litex_git_revision

Expand Down Expand Up @@ -165,10 +166,10 @@ def _generate_hierarchy(top):
# Print Constant -----------------------------------------------------------------------------------

def _generate_constant(node):
return "{sign}{bits}'d{value}".format(
return "{sign}{bits}'{value}".format(
sign = "" if node.value >= 0 else "-",
bits = str(node.nbits),
value = abs(node.value),
value = format_constant(abs(node.value), verilog=True),
), node.signed

# Print Signal -------------------------------------------------------------------------------------
Expand Down
34 changes: 34 additions & 0 deletions litex/gen/format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

def format_constant(c, verilog:bool = False):
"""Automatically format constants based on type and value.

If the argument `verilog` isn't provided, a 'c' style constant is generated.

Integer constants are formatted as decimal or hexadecimal depending on which
value will give the clearest representation, as indicated by the least unique
numbers in the string representation.

NOTE: Integers below 100 are always formatted as decimal.

Parameters
----------
c : any
constant to format. Only integer constants are formatted.

verilog : bool, optional
Format for verilog (default is False)

Raises
------
None
"""

if not isinstance(c, int):
return c

d, x = f"{c:d}", f"{c:x}"
decimal = abs(c) < 100 or len(set(d)) <= len(set(x))
if verilog:
return "d" + d if decimal else "h" + x
else:
return d if decimal else "0x" + x
3 changes: 2 additions & 1 deletion litex/soc/integration/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from migen import *

from litex.gen.format import format_constant
from litex.soc.interconnect.csr import CSRStatus
from litex.soc.integration.soc import SoCRegion

Expand Down Expand Up @@ -181,7 +182,7 @@ def get_soc_header(constants, with_access_functions=True):
value = "\"" + value + "\""
ctype = "const char *"
else:
value = str(value)
value = format_constant(value)
ctype = "int"
r += "#define "+name+" "+value+"\n"
if with_access_functions:
Expand Down
Loading