1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-13 20:19:03 -08:00
rc/sh/sram

49 lines
1 KiB
Text
Raw Normal View History

2015-11-28 15:03:30 -08:00
#!/usr/bin/env zsh
# compat: -ash +bash -dash +zsh
2015-11-28 15:03:30 -08:00
2021-07-30 17:57:08 -07:00
sram() { ### @-
### convert between a couple saveram formats for N64 emulators.
2015-11-28 15:03:30 -08:00
sram_crop() {
2015-11-28 15:03:30 -08:00
tail -n +$((($1)/16+1))
}
sram_revend() {
2015-11-28 15:03:30 -08:00
objcopy -I binary -O binary --reverse-bytes=4 "$@"
}
sram_dump() {
2015-11-28 15:03:30 -08:00
xxd -ps -c 16 "$@"
}
sram_zeros() {
2015-11-28 15:03:30 -08:00
# one row (16) of zeros
yes "00000000000000000000000000000000" | head -n $1
}
game="$1"
in="$2"
out="$3"
if [ "$game" = "mm" ]; then
2015-11-28 15:03:30 -08:00
headsize=$((0x20800))
elif [ "$game" = "oot" ]; then
2015-11-28 15:03:30 -08:00
headsize=$((0x40800))
else
echo "Unknown game." >&2
return 1
fi
[ -e "$2" ] || { echo -E "input save does not exist" >&2; return 1; }
2015-11-28 15:03:30 -08:00
if [ "${in##*.}" = "SaveRAM" ]; then
sram_dump "$in" | sram_crop $headsize | sram_dump -r > "$out"
2015-11-28 15:03:30 -08:00
else
cat <(sram_zeros $(($headsize/16))) <(sram_dump "$in") | sram_dump -r > "$out"
2015-11-28 15:03:30 -08:00
fi
sram_revend "$out"
2015-11-28 15:03:30 -08:00
}
[ -n "${preload+-}" ] || sram "$@"