Improve gbdiff script

Use better constructs where possible
- <<< instead of echo|
- Parameter expansions instead of `cut`
- Etc.
Improves performance and reliability somewhat

Also, accept "d" between diff line info parts
This commit is contained in:
ISSOtm
2020-12-26 01:44:45 +01:00
parent c20ac35074
commit 900fd8cabc

View File

@@ -31,29 +31,29 @@ diff <(xxd $1) <(xxd $2) | while read -r LINE; do
# Separator between files switches states # Separator between files switches states
echo $LINE echo $LINE
STATE=3 STATE=3
elif echo $LINE | grep -Eq '^[0-9]+(,[0-9]+)?c[0-9]+(,[0-9]+)?'; then elif grep -Eq '^[0-9]+(,[0-9]+)?[cd][0-9]+(,[0-9]+)?' <<< "$LINE"; then
# Line info resets the whole thing # Line info resets the whole thing
STATE=1 STATE=1
elif [ $STATE -eq 1 -o $STATE -eq 3 ]; then elif [ $STATE -eq 1 -o $STATE -eq 3 ]; then
# Compute the GB address from the ROM offset # Compute the GB address from the ROM offset
OFS=$(echo $LINE | cut -d ' ' -f 2 | tr -d ':') OFS=$(cut -d ' ' -f 2 <<< "$LINE" | tr -d ':')
BANK=$((0x$OFS / 0x4000)) BANK=$((0x$OFS / 0x4000))
ADDR=$((0x$OFS % 0x4000 + ($BANK != 0) * 0x4000)) ADDR=$((0x$OFS % 0x4000 + ($BANK != 0) * 0x4000))
# Try finding the preceding symbol closest to the diff # Try finding the preceding symbol closest to the diff
if [ $STATE -eq 1 ]; then if [ $STATE -eq 1 ]; then
STATE=2 STATE=2
SYMFILE=$(echo $1 | cut -d '.' -f 1).sym SYMFILE=${1%.*}.sym
else else
STATE=4 STATE=4
SYMFILE=$(echo $2 | cut -d '.' -f 1).sym SYMFILE=${2%.*}.sym
fi fi
EXTRA=$(if [ -f "$SYMFILE" ]; then EXTRA=$(if [ -f "$SYMFILE" ]; then
# Read the sym file for such a symbol # Read the sym file for such a symbol
# Ignore comment lines, only pick matching bank # Ignore comment lines, only pick matching bank
grep -Fv ';' "$SYMFILE" | cut -d ';' -f 1 "$SYMFILE" |
grep -Ei $(printf "^%02x:" $BANK) | grep -Ei $(printf "^%02x:" $BANK) |
while read -r SYMADDR SYM; do while read -r SYMADDR SYM; do
SYMADDR=$((0x$(echo $SYMADDR | cut -d ':' -f 2))) SYMADDR=$((0x${SYMADDR#*:}))
if [ $SYMADDR -le $ADDR ]; then if [ $SYMADDR -le $ADDR ]; then
printf " (%s+%#x)\n" $SYM $(($ADDR - $SYMADDR)) printf " (%s+%#x)\n" $SYM $(($ADDR - $SYMADDR))
fi fi
@@ -63,9 +63,9 @@ diff <(xxd $1) <(xxd $2) | while read -r LINE; do
printf "%02x:%04x %s\n" $BANK $ADDR $EXTRA printf "%02x:%04x %s\n" $BANK $ADDR $EXTRA
fi fi
if [ $STATE -eq 2 -o $STATE -eq 4 ]; then if [ $STATE -eq 2 -o $STATE -eq 4 ]; then
OFS=$(echo $LINE | cut -d ' ' -f 2 | tr -d ':') OFS=$(cut -d ' ' -f 2 <<< "$LINE" | tr -d ':')
BANK=$((0x$OFS / 0x4000)) BANK=$((0x$OFS / 0x4000))
ADDR=$((0x$OFS % 0x4000 + ($BANK != 0) * 0x4000)) ADDR=$((0x$OFS % 0x4000 + ($BANK != 0) * 0x4000))
printf "%s %02x:%04x: %s\n" "$(echo $LINE | cut -d ' ' -f 1)" $BANK $ADDR "$(echo $LINE | cut -d ' ' -f 3-)" printf "%s %02x:%04x: %s\n" "${LINE:0:1}" $BANK $ADDR "${LINE#*: }"
fi fi
done done