Skip to content

Instantly share code, notes, and snippets.

@shmerl
Last active January 22, 2024 06:35
Show Gist options
  • Save shmerl/202332219bebb5e5135ad41140f8f6d5 to your computer and use it in GitHub Desktop.
Save shmerl/202332219bebb5e5135ad41140f8f6d5 to your computer and use it in GitHub Desktop.
For extracting GOG Konami Collector's Series
#!/bin/bash
# Extractor for game ROMs from Konami Collector's Series.
#
# Usage: gog_extract_konami_collectors_series.sh <path_to>/cc.exe
#
# Make sure you have xxd hexdump tool and dd installed.
#
# Games locations:
#
# Castlevania : 01E570 - 03E56F (0x20000 bytes)
# Castlevania II : 03E570 - 07E56F (0x40000 bytes)
# Castlevania III: 07E570 - 0DE56F (0x60000 bytes)
# Contra : 0DE570 - 0FE56F (0x20000 bytes)
# Jackal : 0FE580 - 11E57F (0x20000 bytes)
# Super C : 11E580 - 15E57F (0x40000 bytes)
header_hex1='4E45531A080021000000000000000000'
header_hex2='4E45531A081010000000000000000000'
header_hex3='4E45531A101050000000000000000000'
header_hex4='4E45531A081041000000000000000000'
source="$1"
if ! [ -e "$source" ]; then
echo "Error: can't find ${source}!"
exit 1
fi
declare -A roms
roms["castlevania"]="0x1E570:0x20000"
roms["castlevania_ii"]="0x3E570:0x40000"
roms["castlevania_iii"]="0x7E570:0x60000"
roms["contra"]="0xDE570:0x20000"
roms["jackal"]="0xFE580:0x20000"
roms["super_c"]="0x11E580:0x40000"
declare -A headers
headers["castlevania"]=$header_hex1
headers["castlevania_ii"]=$header_hex2
headers["castlevania_iii"]=$header_hex3
headers["contra"]=$header_hex1
headers["jackal"]=$header_hex1
headers["super_c"]=$header_hex4
for rom in "${!roms[@]}"; do
offset=${roms[$rom]%%:*}
length=${roms[$rom]##*:}
target="${rom}.nes"
echo "Composing ${target}"
dd if="$source" of="${rom}.tmp" skip=$(($offset))B count=$(($length))B bs=10M
cat <(echo ${headers[$rom]} | xxd --revert --plain) "${rom}.tmp" > "$target"
rm -v "${rom}.tmp"
done
@shmerl
Copy link
Author

shmerl commented Aug 3, 2023

Speeding up dd usage with large block size and byte offest / count (that seems to be a poorly documented dd feature).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment