Skip to content

Commit

Permalink
tools: Add logs and debug messages
Browse files Browse the repository at this point in the history
Internal-tag: [#43412]
Signed-off-by: Tomasz Michalak <[email protected]>
  • Loading branch information
tmichalak committed Oct 24, 2024
1 parent 82b6321 commit d32bdfd
Showing 1 changed file with 67 additions and 24 deletions.
91 changes: 67 additions & 24 deletions tools/prefix_macros.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# !/bin/bash

# Set flag to exit the script on first error
set -e

# Prefix that will be added to all required macro/struct/module names
PREFIX="${PREFIX:-veer0_}"
# Path to directory where common_defines.vh, el2_param.vh, el2_pdef.vh and pd_defines.vh reside
Expand All @@ -14,55 +17,95 @@ PD_DEFINES="$DEFINES_PATH/pd_defines.vh"
EL2_DEF="$DESIGN_DIR/include/el2_def.sv"
EL2_IFU_IC_MEM="$DESIGN_DIR/ifu/el2_ifu_ic_mem.sv"

echo "Starting script with following settings:"
echo "PREFIX=$PREFIX"
echo "DEFINES_PATH=$DEFINES_PATH"
echo -e "DESIGN_DIR=$DESIGN_DIR\n"

# Define regex patterns for matching defines
DEFINES_REGEX="s/((\`define)|(\`ifndef)|(\`undef)) ([A-Z0-9_]+).*/\5/p"
DEFINES_REPLACE_REGEX="s/((\`define)|(\`ifndef)|(\`undef)) ([A-Z0-9_]+)/\1 "$PREFIX"\5/"
STRUCT_REPLACE_REGEX="s/el2_param_t/"$PREFIX"el2_param_t/g"
MODULES_REGEX="s/^module ([\`A-Za-z0-9_]+).*/\1/p"

# Extract unique defines from all sources
DEFINES="$(sed -nr "$DEFINES_REGEX" $COMMON_DEFINES $PD_DEFINES $EL2_IFU_IC_MEM | sort -ur)"

# Skip files that should not be processed
SKIP_DESIGN_FILES="el2_param.vh\|el2_pdef.vh\|common_defines.vh\|pd_defines.vh"
DESIGN_FILES="$(find $DESIGN_DIR -name "*.sv" -o -name "*.vh" -o -name "*.v" | grep -v $SKIP_DESIGN_FILES)"
MODULES="$(sed -nr "$MODULES_REGEX" $DESIGN_FILES | sort -ur)"

if [ "${DEBUG}" = "1" ]; then
echo "DEBUG: DEFINES_REGEX=$DEFINES_REGEX"
echo "DEBUG: DEFINES_REPLACE_REGEX=$DEFINES_REPLACE_REGEX"
echo "DEBUG: STRUCT_REPLACE_REGEX=$STRUCT_REPLACE_REGEX"
echo "DEBUG: MODULES_REGEX=$MODULES_REGEX"
echo
echo "DEBUG: DEFINES=$DEFINES"
echo "DEBUG: DESIGN_FILES=$DESIGN_FILES"
echo "DEBUG: MODULES=$MODULES"
echo
fi

# add prefix to macro names
sed -E "$DEFINES_REPLACE_REGEX" $COMMON_DEFINES > $DEFINES_PATH/"$PREFIX"common_defines.vh
sed -E "$DEFINES_REPLACE_REGEX" $PD_DEFINES > $DEFINES_PATH/"$PREFIX"pd_defines.vh
# Add prefix to macro names
OUTPUT_COMMON_DEFINES=$DEFINES_PATH/"$PREFIX"common_defines.vh
OUTPUT_PD_DEFINES=$DEFINES_PATH/"$PREFIX"pd_defines.vh
echo "Adding prefix to macro names in $OUTPUT_COMMON_DEFINES and $OUTPUT_PD_DEFINES"
sed -E "$DEFINES_REPLACE_REGEX" $COMMON_DEFINES >$OUTPUT_COMMON_DEFINES
sed -E "$DEFINES_REPLACE_REGEX" $PD_DEFINES >$OUTPUT_PD_DEFINES

# replace renamed macros in RTL sources
# Add prefix to VeeR config struct
OUTPUT_EL2_PARAM=$DEFINES_PATH/"$PREFIX"el2_param.vh
OUTPUT_EL2_PDEF=$DEFINES_PATH/"$PREFIX"el2_pdef.vh
echo "Adding prefix to VeeR config struct in $OUTPUT_EL2_PARAM and $OUTPUT_EL2_PDEF"

sed "$STRUCT_REPLACE_REGEX" "$EL2_PARAM" >$DEFINES_PATH/"$PREFIX"el2_param.vh
sed "$STRUCT_REPLACE_REGEX" "$EL2_PDEF" >$DEFINES_PATH/"$PREFIX"el2_pdef.vh
sed -i "$STRUCT_REPLACE_REGEX" $DESIGN_FILES

# Replace renamed macros in RTL sources
echo "Replacing renamed macros in RTL sources"
for DEFINE in $DEFINES; do
sed -i "s/\`$DEFINE/\`"$PREFIX"$DEFINE/g" $DESIGN_FILES
sed -i -E "s/((\`ifdef)|(\`ifndef)) $DEFINE/\1 "$PREFIX"$DEFINE/g" $DESIGN_FILES
sed -i "s/\`$DEFINE/\`"$PREFIX"$DEFINE/g" $DESIGN_FILES
sed -i -E "s/((\`ifdef)|(\`ifndef)) $DEFINE/\1 "$PREFIX"$DEFINE/g" $DESIGN_FILES
done

# add prefix to VeeR config struct
STRUCT_SED="s/el2_param_t/"$PREFIX"el2_param_t/g"
sed "$STRUCT_SED" "$EL2_PARAM" > $DEFINES_PATH/"$PREFIX"el2_param.vh
sed "$STRUCT_SED" "$EL2_PDEF" > $DEFINES_PATH/"$PREFIX"el2_pdef.vh
sed -i "$STRUCT_SED" $DESIGN_FILES

# replace include names in RTL sources
# Replace include names in RTL sources
echo "Replacing include names in RTL sources"
sed -i "s/include \"el2_param.vh\"/include \""$PREFIX"el2_param.vh\"/g" $DESIGN_FILES
sed -i "s/include \"el2_pdef.vh\"/include \""$PREFIX"el2_pdef.vh\"/g" $DESIGN_FILES
sed -i "s/include \"common_defines.vh\"/include \""$PREFIX"common_defines.vh\"/g" $OUTPUT_PD_DEFINES

# replace package name and its imports in RTL sources
# Replace package name and its imports in RTL sources
echo "Replacing package name and its imports in RTL sources"
sed -i "s/import el2_pkg/import "$PREFIX"el2_pkg/g" $DESIGN_FILES
sed -i "s/package el2_pkg/package "$PREFIX"el2_pkg/g" $EL2_DEF

MODULES_REGEX="s/^module ([\`A-Za-z0-9_]+).*/\1/p"
MODULES="$(sed -nr "$MODULES_REGEX" $DESIGN_FILES | sort -ur)"

# add prefix to all module names
sed -i -E "s/module ([\`A-Za-z0-9_]+)/module "$PREFIX"\1/g" $DESIGN_FILES
# Add prefix to all module names
echo "Adding prefix to all module names"
perl -pi -e "s/module (?!${PREFIX})([\`A-Za-z0-9_]+)/module ${PREFIX}\1/g" $DESIGN_FILES

# add prefix to all module instantiations
# Add prefix to all module instantiations
echo "Adding prefix to all module instantiations"
for MODULE in $MODULES; do
sed -i -E "s/(^|[^A-Za-z0-9_])$MODULE([^A-Za-z0-9_])/\1"$PREFIX"$MODULE\2/g" $DESIGN_FILES
echo "Processing MODULE=$MODULE"
perl -pi -e "s/(^|[^A-Za-z0-9_])(?<!${PREFIX})${MODULE}([^A-Za-z0-9_])/\1${PREFIX}${MODULE}\2/g" $DESIGN_FILES
done

# remove old header files to avoid redefining their contents during elaboration
rm $COMMON_DEFINES $EL2_PARAM $EL2_PDEF $PD_DEFINES
# Remove old header files to avoid redefining their contents during elaboration
echo "Removing old header files"
rm -f $COMMON_DEFINES $EL2_PARAM $EL2_PDEF $PD_DEFINES

# add prefix to el2_mem_if interface
# Add prefix to el2_mem_if interface
echo "Adding prefix to el2_mem_if interface"
sed -i -E "s/el2_mem_if/"$PREFIX"el2_mem_if/g" $DESIGN_FILES

# prefix memory macro names in el2_ifu_ic_mem.sv
echo "Prefixing memory macro names in $EL2_IFU_IC_MEM"
sed -i "s/EL2_IC_TAG_PACKED_SRAM/"$PREFIX"EL2_IC_TAG_PACKED_SRAM/g" $EL2_IFU_IC_MEM
sed -i "s/EL2_IC_TAG_SRAM/"$PREFIX"EL2_IC_TAG_SRAM/g" $EL2_IFU_IC_MEM
sed -i "s/EL2_PACKED_IC_DATA_SRAM/"$PREFIX"EL2_PACKED_IC_DATA_SRAM/g" $EL2_IFU_IC_MEM
sed -i "s/EL2_IC_DATA_SRAM/"$PREFIX"EL2_IC_DATA_SRAM/g" $EL2_IFU_IC_MEM

echo "Script finished successfully"

0 comments on commit d32bdfd

Please sign in to comment.