forked from bzhxx/game-and-watch-retro-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
program_saves.sh
executable file
·90 lines (73 loc) · 2.5 KB
/
program_saves.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
if [[ "$VERBOSE" == "1" ]]; then
set -ex
else
set -e
fi
if [[ "${GCC_PATH}" != "" ]]; then
DEFAULT_OBJDUMP=${GCC_PATH}/arm-none-eabi-objdump
DEFAULT_GDB=${GCC_PATH}/arm-none-eabi-gdb
else
DEFAULT_OBJDUMP=arm-none-eabi-objdump
DEFAULT_GDB=arm-none-eabi-gdb
fi
FLSHLD_DIR=${OBJDUMP:-../game-and-watch-flashloader}
FLASHLOADER=${FLASHLOADER:-${FLSHLD_DIR}/flash_multi.sh}
OBJDUMP=${OBJDUMP:-$DEFAULT_OBJDUMP}
GDB=${GDB:-$DEFAULT_GDB}
ADAPTER=${ADAPTER:-stlink}
OPENOCD=${OPENOCD:-$(which openocd || true)}
if [[ -z ${OPENOCD} ]]; then
echo "Cannot find 'openocd' in the PATH. You can set the environment variable 'OPENOCD' to manually specify the location"
exit 2
fi
if [[ $# -lt 1 ]]; then
echo "Usage: $(basename $0) <currently_running_binary.elf> [backup directory]"
echo "This will program all save states from the backup directory that match what is included in the elf file"
exit 1
fi
ELF="$1"
INDIR=save_states
if [[ $# -gt 1 ]]; then
INDIR="$2"
fi
function get_number_of_saves {
prefix=$1
objdump_cmd="${OBJDUMP} -t ${ELF}"
echo $(${objdump_cmd} | grep " $prefix" | wc -l)
}
# Start processing
mkdir -p "$INDIR"
for emu in gb gg nes pce sms gw; do
mkdir -p "${INDIR}/${emu}"
COUNT=$(get_number_of_saves SAVE_$(echo ${emu} | awk '{print toupper($0)}')_)
for i in $(seq 0 $(( COUNT - 1 ))); do
name=$(${GDB} "${ELF}" --batch -q -ex "printf \"%s\n\", ${emu}_roms[${i}].name")
# Note that 0x90000000 is subtracted from the address.
address=$(${GDB} "${ELF}" --batch -q -ex "printf \"%ld\n\", ${emu}_roms[${i}].save_address - 0x90000000")
size=$(${GDB} "${ELF}" --batch -q -ex "printf \"%d\n\", ${emu}_roms[${i}].save_size")
image="${INDIR}/${emu}/${name}.save"
if [[ -e "$image" ]]; then
echo ""
echo ""
echo "Programming save for:"
echo " rom_name=\"$name\""
echo " save_address=$address"
echo " save_size=$size"
echo ""
echo ""
${FLASHLOADER} "${image}" ${address} ${size}
else
echo ""
echo ""
echo "Skipping: (missing save file in backup directory)"
echo " rom_name=\"$name\""
echo " save_address=$address"
echo " save_size=$size"
echo ""
echo ""
fi
done
done
# Reset the device
${OPENOCD} -f ${FLSHLD_DIR}/interface_${ADAPTER}.cfg -c "init; halt; reset run; exit;"