Skip to content

Commit

Permalink
Merge branch 'riscv:master' into improve_readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Myrausman authored Oct 31, 2024
2 parents 2465bdc + f73c2a4 commit c4e177a
Show file tree
Hide file tree
Showing 12 changed files with 1,350 additions and 1,200 deletions.
27 changes: 18 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,39 @@ ENV_H := ../riscv-tests/env/encoding.h
OPENOCD_H := ../riscv-openocd/src/target/riscv/encoding.h
INSTALL_HEADER_FILES := $(ISASIM_H) $(PK_H) $(ENV_H) $(OPENOCD_H)

ifdef PSEUDO
PSEUDO_FLAG := -pseudo
else
PSEUDO_FLAG :=
endif

default: everything

.PHONY: everything encoding.out.h inst.chisel inst.go latex inst.sverilog inst.rs clean install instr-table.tex priv-instr-table.tex inst.spinalhdl
.PHONY: everything encoding.out.h inst.chisel inst.go latex inst.sverilog inst.rs clean install instr-table.tex priv-instr-table.tex inst.spinalhdl pseudo

pseudo:
@$(MAKE) PSEUDO=1 everything

everything:
@./parse.py -c -go -chisel -sverilog -rust -latex -spinalhdl $(EXTENSIONS)
@./parse.py $(PSEUDO_FLAG) -c -go -chisel -sverilog -rust -latex -spinalhdl $(EXTENSIONS)

encoding.out.h:
@./parse.py -c rv* unratified/rv_* unratified/rv32* unratified/rv64*
@./parse.py -c $(PSEUDO_FLAG) rv* unratified/rv_* unratified/rv32* unratified/rv64*

inst.chisel:
@./parse.py -chisel $(EXTENSIONS)
@./parse.py -chisel $(PSEUDO_FLAG) $(EXTENSIONS)

inst.go:
@./parse.py -go $(EXTENSIONS)
@./parse.py -go $(PSEUDO_FLAG) $(EXTENSIONS)

latex:
@./parse.py -latex $(EXTENSIONS)
@./parse.py -latex $(PSEUDO_FLAG) $(EXTENSIONS)

inst.sverilog:
@./parse.py -sverilog $(EXTENSIONS)
@./parse.py -sverilog $(PSEUDO_FLAG) $(EXTENSIONS)

inst.rs:
@./parse.py -rust $(EXTENSIONS)
@./parse.py -rust $(PSEUDO_FLAG) $(EXTENSIONS)

clean:
rm -f inst* priv-instr-table.tex encoding.out.h
Expand All @@ -44,4 +53,4 @@ instr-table.tex: latex
priv-instr-table.tex: latex

inst.spinalhdl:
@./parse.py -spinalhdl $(EXTENSIONS)
@./parse.py -spinalhdl $(PSEUDO_FLAG) $(EXTENSIONS)
78 changes: 78 additions & 0 deletions c_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import logging
import os
import pprint

from shared_utils import *

pp = pprint.PrettyPrinter(indent=2)
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")


def make_c(instr_dict):
mask_match_str = ""
declare_insn_str = ""
for i in instr_dict:
mask_match_str += (
f'#define MATCH_{i.upper().replace(".","_")} {instr_dict[i]["match"]}\n'
)
mask_match_str += (
f'#define MASK_{i.upper().replace(".","_")} {instr_dict[i]["mask"]}\n'
)
declare_insn_str += f'DECLARE_INSN({i.replace(".","_")}, MATCH_{i.upper().replace(".","_")}, MASK_{i.upper().replace(".","_")})\n'

csr_names_str = ""
declare_csr_str = ""
for num, name in csrs + csrs32:
csr_names_str += f"#define CSR_{name.upper()} {hex(num)}\n"
declare_csr_str += f"DECLARE_CSR({name}, CSR_{name.upper()})\n"

causes_str = ""
declare_cause_str = ""
for num, name in causes:
causes_str += f"#define CAUSE_{name.upper().replace(' ', '_')} {hex(num)}\n"
declare_cause_str += (
f"DECLARE_CAUSE(\"{name}\", CAUSE_{name.upper().replace(' ','_')})\n"
)

arg_str = ""
for name, rng in arg_lut.items():
sanitized_name = name.replace(" ", "_").replace("=", "_eq_")
begin = rng[1]
end = rng[0]
mask = ((1 << (end - begin + 1)) - 1) << begin
arg_str += f"#define INSN_FIELD_{sanitized_name.upper()} {hex(mask)}\n"

with open(f"{os.path.dirname(__file__)}/encoding.h", "r") as file:
enc_header = file.read()

commit = os.popen('git log -1 --format="format:%h"').read()

# Generate the output as a string
output_str = f"""/* SPDX-License-Identifier: BSD-3-Clause */
/* Copyright (c) 2023 RISC-V International */
/*
* This file is auto-generated by running 'make' in
* https://github.com/riscv/riscv-opcodes ({commit})
*/
{enc_header}
/* Automatically generated by parse_opcodes. */
#ifndef RISCV_ENCODING_H
#define RISCV_ENCODING_H
{mask_match_str}
{csr_names_str}
{causes_str}
{arg_str}#endif
#ifdef DECLARE_INSN
{declare_insn_str}#endif
#ifdef DECLARE_CSR
{declare_csr_str}#endif
#ifdef DECLARE_CAUSE
{declare_cause_str}#endif
"""

# Write the modified output to the file
with open("encoding.out.h", "w") as enc_file:
enc_file.write(output_str)
86 changes: 86 additions & 0 deletions chisel_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import logging
import pprint

from constants import *

# from shared_utils import overlaps, overlap_allowed, extension_overlap_allowed, instruction_overlap_allowed, process_enc_line, same_base_isa, add_segmented_vls_insn, expand_nf_field
from shared_utils import *

pp = pprint.PrettyPrinter(indent=2)
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")


def make_chisel(instr_dict, spinal_hdl=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:
e_instrs = filter(lambda i: instr_dict[i]["extension"][0] == e, instr_dict)
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 in e_instrs:
tmp_instr_name = '"' + instr.upper().replace(".", "_") + '"'
chisel_names += f' {tmp_instr_name:<18s} -> BitPat("b{instr_dict[instr]["encoding"].replace("-","?")}"),\n'
chisel_names += f" )\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
}"""

if spinal_hdl:
chisel_file = open("inst.spinalhdl", "w")
else:
chisel_file = open("inst.chisel", "w")
chisel_file.write(
f"""
/* Automatically generated by parse_opcodes */
object Instructions {{
{chisel_names}
}}
object Causes {{
{cause_names_str}
}}
object CSRs {{
{csr_names_str}
}}
"""
)
chisel_file.close()
62 changes: 62 additions & 0 deletions go_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import logging
import pprint
import sys

from shared_utils import *

pp = pprint.PrettyPrinter(indent=2)
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")


def make_go(instr_dict):

args = " ".join(sys.argv)
prelude = f"""// Code generated by {args}; DO NOT EDIT."""

prelude += """
package riscv
import "cmd/internal/obj"
type inst struct {
opcode uint32
funct3 uint32
rs1 uint32
rs2 uint32
csr int64
funct7 uint32
}
func encode(a obj.As) *inst {
switch a {
"""

endoffile = """ }
return nil
}
"""

instr_str = ""
for i in instr_dict:
enc_match = int(instr_dict[i]["match"], 0)
opcode = (enc_match >> 0) & ((1 << 7) - 1)
funct3 = (enc_match >> 12) & ((1 << 3) - 1)
rs1 = (enc_match >> 15) & ((1 << 5) - 1)
rs2 = (enc_match >> 20) & ((1 << 5) - 1)
csr = (enc_match >> 20) & ((1 << 12) - 1)
funct7 = (enc_match >> 25) & ((1 << 7) - 1)
instr_str += f""" case A{i.upper().replace("_","")}:
return &inst{{ {hex(opcode)}, {hex(funct3)}, {hex(rs1)}, {hex(rs2)}, {signed(csr,12)}, {hex(funct7)} }}
"""

with open("inst.go", "w") as file:
file.write(prelude)
file.write(instr_str)
file.write(endoffile)

try:
import subprocess

subprocess.run(["go", "fmt", "inst.go"])
except:
pass
Loading

0 comments on commit c4e177a

Please sign in to comment.