aboutsummaryrefslogtreecommitdiff
path: root/src/riscv_opcodes/chisel_utils.py
diff options
context:
space:
mode:
authorAndrew Waterman <andrew@sifive.com>2025-10-13 16:24:22 -0700
committerGitHub <noreply@github.com>2025-10-13 16:24:22 -0700
commit26e2c04c913a67ac51bf0a354f21f2d7d5c07c40 (patch)
tree7db4f4637f71f1777f86702a2228ccf3b9217a03 /src/riscv_opcodes/chisel_utils.py
parent433081c02368eb72e6dea5413e404a8ef231658e (diff)
downloadriscv-opcodes-master.zip
riscv-opcodes-master.tar.gz
riscv-opcodes-master.tar.bz2
Reorganise Python code using pyproject.toml (#378)HEADmaster
Add pyproject.toml (the modern alternative to requirements.txt), making this a proper Python package that can be installed via pip and potentially uploaded to PyPI. This also loads the files using `importlib.resources` and installs them into the wheel. This means that when you create a wheel using `uv build` it will still be able to load all the opcodes and CSV files. To avoid moving those resource files in the source repo, the Python build backend (hatchling) is instructed to move them to the right place when building a wheel, and the `resource_root()` function checks in both places so it always works. This is a little hacky but it works. CI builds source and binary wheels (not actually binary) that can be uploaded to PyPI. If we do upload them then using this project is as simple as ``` uvx riscv_opcodes -c 'rv*' ``` Co-authored-by: Tim Hutt <timothy.hutt@codasip.com>
Diffstat (limited to 'src/riscv_opcodes/chisel_utils.py')
-rw-r--r--src/riscv_opcodes/chisel_utils.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/riscv_opcodes/chisel_utils.py b/src/riscv_opcodes/chisel_utils.py
new file mode 100644
index 0000000..46cb0b6
--- /dev/null
+++ b/src/riscv_opcodes/chisel_utils.py
@@ -0,0 +1,82 @@
+import logging
+import pprint
+
+from .constants import causes, csrs, csrs32
+from .shared_utils import InstrDict, instr_dict_2_extensions
+
+pp = pprint.PrettyPrinter(indent=2)
+logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")
+
+
+def make_chisel(instr_dict: InstrDict, spinal_hdl: bool = False):
+
+ chisel_names = ""
+ cause_names_str = ""
+ csr_names_str = ""
+ for i in instr_dict:
+ if spinal_hdl:
+ chisel_names += f' def {i.upper().replace(".","_"):<18s} = M"b{instr_dict[i]["encoding"].replace("-","-")}"\n'
+ # else:
+ # chisel_names += f' def {i.upper().replace(".","_"):<18s} = BitPat("b{instr_dict[i]["encoding"].replace("-","?")}")\n'
+ if not spinal_hdl:
+ extensions = instr_dict_2_extensions(instr_dict)
+ for e in extensions:
+ if "rv64_" in e:
+ e_format = e.replace("rv64_", "").upper() + "64"
+ elif "rv32_" in e:
+ e_format = e.replace("rv32_", "").upper() + "32"
+ elif "rv_" in e:
+ e_format = e.replace("rv_", "").upper()
+ else:
+ e_format = e.upper()
+ chisel_names += f' val {e_format+"Type"} = Map(\n'
+ for instr_name, instr in instr_dict.items():
+ if instr["extension"][0] == e:
+ tmp_instr_name = '"' + instr_name.upper().replace(".", "_") + '"'
+ chisel_names += f' {tmp_instr_name:<18s} -> BitPat("b{instr["encoding"].replace("-","?")}"),\n'
+ chisel_names += " )\n"
+
+ for num, name in causes:
+ cause_names_str += f' val {name.lower().replace(" ","_")} = {hex(num)}\n'
+ cause_names_str += """ val all = {
+ val res = collection.mutable.ArrayBuffer[Int]()
+"""
+ for num, name in causes:
+ cause_names_str += f' res += {name.lower().replace(" ","_")}\n'
+ cause_names_str += """ res.toArray
+ }"""
+
+ for num, name in csrs + csrs32:
+ csr_names_str += f" val {name} = {hex(num)}\n"
+ csr_names_str += """ val all = {
+ val res = collection.mutable.ArrayBuffer[Int]()
+"""
+ for num, name in csrs:
+ csr_names_str += f""" res += {name}\n"""
+ csr_names_str += """ res.toArray
+ }
+ val all32 = {
+ val res = collection.mutable.ArrayBuffer(all:_*)
+"""
+ for num, name in csrs32:
+ csr_names_str += f""" res += {name}\n"""
+ csr_names_str += """ res.toArray
+ }"""
+
+ with open(
+ "inst.spinalhdl" if spinal_hdl else "inst.chisel", "w", encoding="utf-8"
+ ) as chisel_file:
+ chisel_file.write(
+ f"""
+/* Automatically generated by parse_opcodes */
+object Instructions {{
+{chisel_names}
+}}
+object Causes {{
+{cause_names_str}
+}}
+object CSRs {{
+{csr_names_str}
+}}
+"""
+ )