1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-12 15:49:03 -08:00
rc/sh/sram
Connor Olding d5f3f14d72 merge compatibility lines
RIP meaningful last-modified dates
2024-03-26 15:08:14 -07:00

48 lines
1 KiB
Bash
Executable file

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