Skip to content

Instantly share code, notes, and snippets.

@DNA64
Last active February 4, 2023 09:01
Show Gist options
  • Save DNA64/1b3aa3ef14378cb5bb6fdff0411e17c3 to your computer and use it in GitHub Desktop.
Save DNA64/1b3aa3ef14378cb5bb6fdff0411e17c3 to your computer and use it in GitHub Desktop.
Command line tool to patch game-and-watch-flashloader and game-and-watch-retro-go to support 32bit addressing.
#!/bin/bash
# THIS SCRIPT IS NO LONGER REQUIRED. PLEASE DO NOT USE IT.
# This script will modify your game-and-watch-flashloader and game-and-watch-retro-go installations
# to allow flashing of flash memory chips exceeding 128Mb (16MB).
# Useage: Place this file in the same directory as both the 'game-and-watch-flashloader' and
# 'game-and-watch-retro-go folders', then run the script and follow the instructions.
# After patching, make sure to rebuild 'game-and-watch-flashloader'.
# Now just use `make LARGE_FLASH=1 flash`. No need to use any other flags, it's all done for you!
# Source/Updates: https://gist.github.com/DNA64/1b3aa3ef14378cb5bb6fdff0411e17c3
# Clean logfile
if test -f debug.log; then
rm -rf debug.log
fi
function file_check(){
# Let's check to make sure all our files are present?
echo "Looking for (flash.c) in the game-and-watch-flashloader directory..." >> debug.log
if ! test -f game-and-watch-flashloader/Core/Src/flash.c; then
echo "flash.c file not found in game-and-watch-flashloader/flash.c"
echo ""
echo "Are you sure you're running this script in the right directory?"
exit 1
fi
echo "Looking for (main.c) in the game-and-watch-flashloader directory..." >> debug.log
if ! test -f game-and-watch-flashloader/Core/Src/main.c; then
echo "main.c file not found in game-and-watch-flashloader/main.c"
echo ""
echo "Are you sure you're running this script in the right directory?"
exit 1
fi
echo "Looking for (Makefile.common) in the game-and-watch-retro-go directory..." >> debug.log
if ! test -f game-and-watch-retro-go/Makefile.common; then
echo "Makefile.common file not found in game-and-watch-retro-go/Makefile.common"
echo ""
echo "Are you sure you're running this script in the right directory?"
exit 1
fi
echo "Looking for (gw_flash.c) in the game-and-watch-retro-go directory..." >> debug.log
if ! test -f game-and-watch-retro-go/Core/Src/gw_flash.c; then
echo "gw_flash.c file not found in game-and-watch-retro-go/gw_flash.c"
echo ""
echo "Are you sure you're running this script in the right directory?"
exit 1
fi
echo "Looking for (main.c) in the game-and-watch-retro-go directory..." >> debug.log
if ! test -f game-and-watch-retro-go/Core/Src/main.c; then
echo "main.c file not found in game-and-watch-retro-go/main.c"
echo ""
echo "Are you sure you're running this script in the right directory?"
exit 1
fi
echo "All files found! We're off to a good start!" >> debug.log
}
function file_backup(){
# We don't really need to make backups, but I felt it was the right thing to do, just in case.
echo "Backing up files..." #Do not remove this line
if ! test -f game-and-watch-flashloader/Core/Src/backup_flash.c; then
cp game-and-watch-flashloader/Core/Src/flash.c game-and-watch-flashloader/Core/Src/backup_flash.c
echo "flash.c backup created! - game-and-watch-flashloader/Core/Src/backup_flash.c" >> debug.log
else
echo "game-and-watch-flashloader/Core/Src/backup_flash.c already exists." >> debug.log
fi
if ! test -f game-and-watch-flashloader/Core/Src/backup_main.c; then
cp game-and-watch-flashloader/Core/Src/main.c game-and-watch-flashloader/Core/Src/backup_main.c
echo "main.c backup created! - game-and-watch-flashloader/Core/Src/backup_main.c" >> debug.log
else
echo "game-and-watch-flashloader/Core/Src/backup_main.c already exists." >> debug.log
fi
if ! test -f game-and-watch-retro-go/backup_Makefile.common; then
cp game-and-watch-retro-go/Makefile.common game-and-watch-retro-go/backup_Makefile.common
echo "Makefile.common backup created! - game-and-watch-retro-go/backup_Makefile.common" >> debug.log
else
echo "game-and-watch-retro-go/backup_Makefile.common already exists." >> debug.log
fi
if ! test -f game-and-watch-retro-go/Core/Src/backup_gw_flash.c; then
cp game-and-watch-retro-go/Core/Src/gw_flash.c game-and-watch-retro-go/Core/Src/backup_gw_flash.c
echo "flash.c backup created! - game-and-watch-retro-go/Core/Src/backup_gw_flash.c" >> debug.log
else
echo "game-and-watch-retro-go/Core/Src/backup_gw_flash.c already exists." >> debug.log
fi
if ! test -f game-and-watch-retro-go/Core/Src/backup_main.c; then
cp game-and-watch-retro-go/Core/Src/main.c game-and-watch-retro-go/Core/Src/backup_main.c
echo "main.c backup created! - game-and-watch-retro-go/Core/Src/backup_main.c" >> debug.log
else
echo "game-and-watch-retro-go/Core/Src/backup_main.c already exists." >> debug.log
fi
}
function file_check_backup(){
# Let's check to make sure all our backup files are still present?
echo "Looking for (backup_flash.c) in the game-and-watch-flashloader directory..." >> debug.log
if ! test -f game-and-watch-flashloader/Core/Src/backup_flash.c; then
echo "backup_flash.c file not found in game-and-watch-flashloader/"
echo "Are you sure you're running this script in the right directory?"
echo ""
echo "This error will also appear if no backups have been created,"
echo "or if the backup has already been restored."
read -p "Press any key to return to the menu..." -n1 -s
show_menu ;
fi
echo "Looking for (backup_main.c) in the game-and-watch-flashloader directory..." >> debug.log
if ! test -f game-and-watch-flashloader/Core/Src/backup_main.c; then
echo "backup_main.c file not found in game-and-watch-flashloader/"
echo "Are you sure you're running this script in the right directory?"
echo ""
echo "This error will also appear if no backups have been created,"
echo "or if the backup has already been restored."
read -p "Press any key to return to the menu..." -n1 -s
show_menu ;
fi
echo "Looking for (backup_Makefile.common) in the game-and-watch-retro-go directory..." >> debug.log
if ! test -f game-and-watch-retro-go/backup_Makefile.common; then
echo "backup_Makefile.common file not found in game-and-watch-retro-go/"
echo "Are you sure you're running this script in the right directory?"
echo ""
echo "This error will also appear if no backups have been created,"
echo "or if the backup has already been restored."
read -p "Press any key to return to the menu..." -n1 -s
show_menu ;
fi
echo "Looking for (backup_gw_flash.c) in the game-and-watch-retro-go directory..." >> debug.log
if ! test -f game-and-watch-retro-go/Core/Src/backup_gw_flash.c; then
echo "backup_gw_flash.c file not found in game-and-watch-retro-go/"
echo "Are you sure you're running this script in the right directory?"
echo ""
echo "This error will also appear if no backups have been created,"
echo "or if the backup has already been restored."
read -p "Press any key to return to the menu..." -n1 -s
show_menu ;
fi
echo "Looking for (backup_main.c) in the game-and-watch-retro-go directory..." >> debug.log
if ! test -f game-and-watch-retro-go/Core/Src/backup_main.c; then
echo "backup_main.c file not found in game-and-watch-retro-go/"
echo "Are you sure you're running this script in the right directory?"
echo ""
echo "This error will also appear if no backups have been created,"
echo "or if the backup has already been restored."
read -p "Press any key to return to the menu..." -n1 -s
show_menu ;
fi
echo "All backup files found!" >> debug.log
}
function file_restore_backup(){
varMode=MODE_24BIT && echo $varMode > config
echo "Restoring backup files..." >> debug.log
mv -f game-and-watch-flashloader/Core/Src/backup_flash.c game-and-watch-flashloader/Core/Src/flash.c
echo "flash.c backup restored! - game-and-watch-flashloader/Core/Src/flash.c" >> debug.log
mv -f game-and-watch-flashloader/Core/Src/backup_main.c game-and-watch-flashloader/Core/Src/main.c
echo "main.c backup restored! - game-and-watch-flashloader/Core/Src/main.c" >> debug.log
mv -f game-and-watch-retro-go/backup_Makefile.common game-and-watch-retro-go/Makefile.common
echo "Makefile.common backup restored! - game-and-watch-retro-go/Makefile.common" >> debug.log
mv -f game-and-watch-retro-go/Core/Src/backup_gw_flash.c game-and-watch-retro-go/Core/Src/gw_flash.c
echo "flash.c backup restored! - game-and-watch-retro-go/Core/Src/gw_flash.c" >> debug.log
mv -f game-and-watch-retro-go/Core/Src/backup_main.c game-and-watch-retro-go/Core/Src/main.c
echo "main.c backup restored! - game-and-watch-retro-go/Core/Src/main.c" >> debug.log
echo "Backup files restored!"
sleep 1
echo ""
read -p "Press any key to return to the menu..." -n1 -s
show_menu ;
}
function file_restore_backup_warning {
clear
while true; do
echo "WARNING! CAUTION! - RESTORING YOUR BACKUPS WILL OVERWRITE YOUR CURRENT FILES!"
echo ""
echo "If you updated either retro-go or flashloader after creating these backups"
echo "restoring these files isn't recomended, and may cause issues with your build."
echo ""
echo "The backups are intended for emergency use, failing option 2 from the main menu."
echo ""
read -p "$*Are you sure you want to continue overwriting your current files? [y/n]: " yn
case $yn in
[Yy]*) file_restore_backup ;return 0 ;;
[Nn]*) clear ; echo "Aborted! No files were overwritten." ; echo "Returning to menu.." ; sleep 2 ; return 1 ;;
esac
done
}
function change_mode_32bit(){
varMode=MODE_32BIT && echo $varMode > config
echo "Changing to 32Bit Addressing mode..."
# flash.c
varDir1=game-and-watch-flashloader/Core/Src/flash.c
echo "modifying $varDir1" >> debug.log
sed -i 's/HAL_OSPI_ADDRESS_24_BITS/HAL_OSPI_ADDRESS_32_BITS/Ig' $varDir1
sed -i 's/0xD8/0xdc/Ig' $varDir1 #64KB Block Erase (BE)
sed -i 's/0x20/0x21/g' $varDir1 #4KB Sector Erase (SE)
sed -i 's/0x02/0x12/g' $varDir1 #Page Programming (PP)
sed -i 's/0x38/0x3e/Ig' $varDir1 #Quad Page Programming (4PP)
sed -i 's/0x0B/0x0c/Ig' $varDir1 #Fast Read
sed -i 's/0xeb/0xec/Ig' $varDir1 #Quad I/O Read (4Read)
# main.c
echo "modifying game-and-watch-flashloader/Core/Src/main.c" >> debug.log
sed -i 's/uint32_t program_device_size = 24;/uint32_t program_device_size = 26;/Ig' game-and-watch-flashloader/Core/Src/main.c
# Makefile.common
sed -i 's/LARGE_FLASH ?= 0/LARGE_FLASH ?= 1/' game-and-watch-retro-go/Makefile.common
sed -i 's/EXTFLASH_SIZE ?= 16777216/EXTFLASH_SIZE ?= 67108864/' game-and-watch-retro-go/Makefile.common
#gw_flash.c
varDir2=game-and-watch-retro-go/Core/Src/gw_flash.c
echo "modifying $varDir2" >> debug.log
sed -i 's/HAL_OSPI_ADDRESS_24_BITS/HAL_OSPI_ADDRESS_32_BITS/Ig' $varDir2
sed -i 's/0xD8/0xdc/Ig' $varDir2
sed -i 's/0x52/0x5C/Ig' $varDir2
sed -i 's/0x20/0x21/g' $varDir2
sed -i 's/0x02/0x12/g' $varDir2
sed -i 's/0x38/0x3e/Ig' $varDir2
sed -i 's/0x0B/0x0c/Ig' $varDir2
sed -i 's/0xeb/0xec/Ig' $varDir2
# main.c
echo "modifying game-and-watch-retro-go/Core/Src/main.c" >> debug.log
sed -i 's/hospi1.Init.DeviceSize = 24/hospi1.Init.DeviceSize = 26/I' game-and-watch-retro-go/Core/Src/main.c
sleep 1
echo "Abracadabra!"
sleep 1
echo ""
read -p "Press any key to return to the menu...the magic is over ;) " -n1 -s
show_menu ;
}
function change_mode_24bit(){
varMode=MODE_24BIT && echo $varMode > config
echo "Changing to 24Bit Addressing mode...(Default)"
# Flashloader
# flash.c
varDir1=game-and-watch-flashloader/Core/Src/flash.c
echo "modifying $varDir1" >> debug.log
sed -i 's/HAL_OSPI_ADDRESS_32_BITS/HAL_OSPI_ADDRESS_24_BITS/' $varDir1
sed -i 's/0xdc/0xD8/g' $varDir1
sed -i 's/0x21/0x20/g' $varDir1
sed -i 's/0x12/0x02/g' $varDir1
sed -i 's/0x3e/0x38/g' $varDir1
sed -i 's/0x0c/0x0B/g' $varDir1
sed -i 's/0xec/0xeb/g' $varDir1
echo "modifying game-and-watch-flashloader/Core/Src/main.c" >> debug.log
sed -i 's/uint32_t program_device_size = 26;/uint32_t program_device_size = 24;/g' game-and-watch-flashloader/Core/Src/main.c
# Retro-Go
# Makefile.common
echo "modifying game-and-watch-retro-go/Makefile.common" >> debug.log
sed -i 's/LARGE_FLASH ?= 1/LARGE_FLASH ?= 0/' game-and-watch-retro-go/Makefile.common
sed -i 's/EXTFLASH_SIZE ?= 67108864/EXTFLASH_SIZE ?= 16777216/' game-and-watch-retro-go/Makefile.common
# gw_flash.c
varDir2=game-and-watch-retro-go/Core/Src/gw_flash.c
echo "modifying $varDir2" >> debug.log
sed -i 's/HAL_OSPI_ADDRESS_32_BITS/HAL_OSPI_ADDRESS_24_BITS/g' $varDir2
sed -i 's/0xdc/0xD8/g' $varDir2
sed -i 's/0x5C/0x52/g' $varDir2
sed -i 's/0x21/0x20/g' $varDir2
sed -i 's/0x12/0x02/g' $varDir2
sed -i 's/0x3e/0x38/g' $varDir2
sed -i 's/0x0c/0x0B/g' $varDir2
sed -i 's/0xec/0xeb/g' $varDir2
# main.c
echo "modifying game-and-watch-retro-go/Core/Src/main.c" >> debug.log
sed -i 's/hospi1.Init.DeviceSize = 26/hospi1.Init.DeviceSize = 24/' game-and-watch-retro-go/Core/Src/main.c
sleep 1
echo "Abracadabra!"
sleep 1
echo ""
read -p "Press any key to return to the menu...the magic is over ;) " -n1 -s
show_menu ;
}
function more_info(){
echo "Just a quick note on:"
echo "High Density Serial Flash Addressing and the 128Mb Memory Address Limitation"
echo ""
echo "Due to addressing constraints of the original 24 bit address protocol,"
echo "serial flash can only access density sizes below 128Mb (16MB)."
echo "For memory devices of 256Mb (64MB) and above,"
echo "32bit addressing is required to access the higher memory areas."
echo ""
echo "Use this to script to enable full access to flash densities of 256Mb (64MB)"
echo "and larger when using game-and-watch-flashloader &/or game-and-watch-retro-go."
echo ""
read -p "Press any key to return to the menu " -n1 -s
show_menu ;
}
function mode_status(){
if ! test -f config; then
echo "MODE_24BIT"
else
cat config
fi
}
function menu_option_one(){
if ! grep -q "0xDC" "game-and-watch-retro-go/Core/Src/gw_flash.c"
then
clear ; file_check ;
clear ; file_backup ;
clear ; change_mode_32bit ;
else
echo "You're already in 32Bit addressing mode"
echo "Returning to to the menu..."
sleep 2
fi
}
function menu_option_two(){
if ! grep -q "0xD8" "game-and-watch-retro-go/Core/Src/gw_flash.c"
then
change_mode_24bit ;
else
echo "You're already in 24Bit addressing mode"
echo "Returning to to the menu..."
sleep 2
fi
}
function menu_option_three(){
clear ; file_check_backup ;
clear ; file_restore_backup_warning ;
}
function menu_option_four(){
clear ; more_info ;
}
function selection_error() {
echo "Selection error! Please make a selection using the numbers and press enter."
read -p "Press any key to quit...the magic is over ;) " -n1 -s
}
function show_menu() {
until [ "local $selection" = "0" ]; do
clear
echo "Use this to script to enable full access to flash densities of 256Mb (64MB)"
echo "and larger in game-and-watch-flashloader & game-and-watch-retro-go."
echo ""
echo "Script By DNA64"
echo ""
echo "Current status: "
mode_status ; echo "Addressing"
echo ""
echo " 1) - Enable 32Bit Address Support (32MB+ Flash Chips)"
echo " 2) - Enable 24bit Addressing (1MB-16MB Flash Chips)"
echo " 3) - Restore backup files"
echo " 4) - More info.."
echo " 0) - Exit"
echo ""
echo -n " Enter selection: "
read selection
echo ""
case $selection in
1 ) clear ; menu_option_one ;;
2 ) clear ; menu_option_two ;;
3 ) clear ; menu_option_three ;;
4 ) clear ; menu_option_four ;;
0 ) clear ; exit ;;
* ) clear ; selection_error ;;
esac
done
}
clear; show_menu ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment