Skip to content

Commit

Permalink
Merge pull request #12 from BrianPugh/more-int
Browse files Browse the repository at this point in the history
Complete system revamp
  • Loading branch information
BrianPugh authored Sep 24, 2021
2 parents b17d69e + 52cca52 commit 242b84f
Show file tree
Hide file tree
Showing 17 changed files with 1,714 additions and 684 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
Drivers/
build/
dump.txt
tileset_palette_*.png
iconset_palette_*.png
capture/
16 changes: 15 additions & 1 deletion Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const ISzAlloc g_Alloc = { SzAlloc, SzFree };

/**
* Dropin replacement for memcpy for loading compressed assets.
* @param n Compressed data length
*/
void *memcpy_inflate(uint8_t *dst, uint8_t *src, size_t n){
unsigned char lzma_heap[LZMA_BUF_SIZE];
Expand All @@ -108,6 +109,19 @@ void *memcpy_inflate(uint8_t *dst, uint8_t *src, size_t n){
return dst;
}

int32_t *rwdata_inflate(int32_t *table){
uint8_t *data = (uint8_t *)table + table[0];
int32_t len = table[1];
uint8_t *ram = table[2];
memcpy_inflate(ram, data, len);
return table + 3;
}


void NMI_Handler(void) {
__BKPT(0);
}

void Error_Handler(){}
void HardFault_Handler(void) {
__BKPT(0);
}
49 changes: 41 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ TARGET = gw_patch
######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Og

PATCH_PARAMS ?=

ifneq (,$(findstring --clock-only, $(PATCH_PARAMS)))
C_DEFS += -DCLOCK_ONLY
endif

ifneq (,$(findstring --debug, $(PATCH_PARAMS)))
DEBUG = 1
C_DEFS += -DDEBUG
endif

ADAPTER ?= stlink
#######################################
# paths
Expand Down Expand Up @@ -109,6 +114,8 @@ CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2 -O0
else
CFLAGS += -Os
endif

# Generate dependency information
Expand All @@ -128,13 +135,17 @@ LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BU
-Wl,--undefined=bootloader \
-Wl,--undefined=read_buttons \
-Wl,--undefined=memcpy_inflate \
-Wl,--undefined=rwdata_inflate \
-Wl,--undefined=NMI_Handler \
-Wl,--undefined=HardFault_Handler \

# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin $(BUILD_DIR)/internal_flash_patched.bin


include Makefile.sdk


#######################################
# build the application
#######################################
Expand All @@ -145,10 +156,10 @@ vpath %.c $(sort $(dir $(C_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
$(BUILD_DIR)/%.o: %.c Makefile $(BUILD_DIR)/env | $(BUILD_DIR)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
$(BUILD_DIR)/%.o: %.s Makefile $(BUILD_DIR)/env | $(BUILD_DIR)
$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
Expand All @@ -162,7 +173,14 @@ $(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(BIN) $< $@

$(BUILD_DIR):
mkdir $@
mkdir -p $@

# Rebuild if PATCH_PARAMS doesn't match the values when last ran
$(BUILD_DIR)/env: $(BUILD_DIR) scripts/check_env_vars.py FORCE
$(PYTHON) scripts/check_env_vars.py $@ $(PATCH_PARAMS)

FORCE: ;


.EXPORT_ALL_VARIABLES:

Expand All @@ -174,6 +192,18 @@ erase_int:
$(OPENOCD) -f openocd/interface_$(ADAPTER).cfg -c "init; halt; flash erase_address 0x08000000 131072; resume; exit"
.PHONY: erase_int

$(BUILD_DIR)/dummy.bin:
$(PYTHON) -c "with open('$@', 'wb') as f: f.write(b'\xFF'*1048576)"

erase_ext: $(BUILD_DIR)/dummy.bin
$(FLASHAPP) $(ADAPTER) $<
make reset
.PHONY: erase_ext

dump_ext:
$(OPENOCD) -f openocd/interface_$(ADAPTER).cfg -c "init; halt; dump_image \"dump_ext.bin\" 0x90000000 0x100000; resume; exit;"
.PHONY: dump_ext

flash_stock_int: internal_flash_backup.bin
$(OPENOCD) -f openocd/interface_"$(ADAPTER)".cfg \
-c "init; halt;" \
Expand All @@ -189,7 +219,7 @@ flash_stock_ext: flash_backup.bin
flash_stock: flash_stock_int flash_stock_ext reset
.PHONY: flash_stock

$(BUILD_DIR)/internal_flash_patched.bin $(BUILD_DIR)/external_flash_patched.bin &: $(BUILD_DIR)/$(TARGET).bin patch.py patches/patches.py patches/patch.py
$(BUILD_DIR)/internal_flash_patched.bin $(BUILD_DIR)/external_flash_patched.bin &: $(BUILD_DIR)/$(TARGET).bin patch.py $(shell find patches -type f)
$(PYTHON) patch.py $(PATCH_PARAMS)

patch: $(BUILD_DIR)/internal_flash_patched.bin $(BUILD_DIR)/external_flash_patched.bin
Expand All @@ -213,6 +243,9 @@ flash_patched: flash_patched_int flash_patched_ext reset
flash: flash_patched
.PHONY: flash

dump:
arm-none-eabi-objdump -xDSs build/gw_patch.elf > dump.txt && vim dump.txt
.PHONY: dump

# Starts openocd and attaches to the target. To be used with 'flash_intflash_nc' and 'gdb'
openocd:
Expand Down
47 changes: 19 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ Custom firmware for the Game and Watch: Super Mario Bros. console.


# What is this?
This repo contains custom code as well as a patching utility to add additional
functionality to the stock Game and Watch firmware. This is the only custom firmware that allows you to run both the stock firmware as well as retro-go without soldering a higher capacity flash chip!
This repo contains custom code as well as a patching utility to add additional functionality to the stock Game and Watch firmware. This is the only custom firmware that allows you to run both the stock firmware as well as retro-go without soldering a higher capacity flash chip!


# Features
* Works correctly with [retro-go](https://github.com/kbeckmann/game-and-watch-retro-go) in internal flash bank 2.

* Press button combination (`LEFT` + `GAME`) to launch retro-go from internal flash bank 2.
* Configurable sleep timeout.
* Configurable hard-reset timeout.
* Reduced external flash firmware size `--slim`; reduced from 1,048,576 bytes to just **180,224** bytes
* Removed the "Mario Song" easter egg.

* Ability to store the enitre firmware in internal flash! No external flash required!

* Option to remove the "Mario Song" easter egg.
* Removed the 5 sleeping illustrations.
* LZMA compressed SMB2 ROM and Clock graphic tiles.
* Move Clock graphic tiles to internal flash.
* An even further stripped version `--clock-only` that further reduces extflash size to just **139,264** bytes. Uses all the techniques described in `slim` plus:
* Removed SMB2 ROM.
* Set retro-go macro to just `GAME`.

* LZMA compressed data.

* Configurable timeouts.

* Run `make help` to see all configuration options.

# Usage
Place your `internal_flash_backup.bin` and `flash_backup.bin` in the root of this
Expand All @@ -39,7 +40,7 @@ Download STM32 Driver files:
make download_sdk
```

To just build and flash, you can just run `make flash`, however it's a bit finicky. You'll probably have better success running:
To just build and flash, you can just run `make flash`, however it's a bit finicky. You'll probably have better success running the following command (but see the [retro-go section](##retro-go) for suggested usage).:

```
make flash_patched_ext
Expand All @@ -54,23 +55,19 @@ For additional configuration options, run `make help`.


### Retro Go
Since most people are going to be using this with retro-go, here are the recommend commands:
Since most people are going to be using this with retro-go, want the minimum amount of external storage used, and don't care about the sleeping images or the mario song easter egg, here are the recommend commands. Note that this uses an undocumented 128KB of internal Bank 1 and requires a [patched version of openocd](https://github.com/kbeckmann/ubuntu-openocd-git-builder) installed.

```
# in this repo
make clean
make PATCH_PARAMS="--slim" flash_patched_ext
make PATCH_PARAMS="--slim" flash_patched_int
make PATCH_PARAMS="--internal-only" flash_patched_int
# in the retro-go repo; this assumes you have the stock 1MB flashchip
# NOTE: MUST have the patched openocd installed:
# https://github.com/kbeckmann/ubuntu-openocd-git-builder
make clean
make -j8 EXTFLASH_SIZE=868352 EXTFLASH_OFFSET=180224 INTFLASH_BANK=2 flash
make -j8 INTFLASH_BANK=2 flash
```

# Advanced usage
Other potentially useful make targets:
Other potentially useful make targets are listed below. Note that external flash only needs to be flashed if the patched external binary is greater than zero bytes.

```
make clean
Expand All @@ -81,17 +78,11 @@ make flash_patch_int
make flash_patch_ext
```

# Known issues (--slim)
* Some of the audio samples in BALL got deleted. Need to fix this.


# TODO
* Fix known issues.
* Figure out safe place in RAM to store global/static variables. The current
configuration described in the linker file is unsafe, but currently we have
no global/static variables.
* Custom sprites for clock.
* Add option to move external flash assets to the extended region of bank 1.
no global/static variables, so this is low priority.
* Custom sprites, icons, and other graphical mods.

# Development
Main stages to developing a feature:
Expand Down
6 changes: 3 additions & 3 deletions STM32H7B0VBTx_FLASH.ld
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ SECTIONS
/* The program code and other data goes into FLASH */
.text :
{
. = 0x00019300;
. = 0x00018100;
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
Expand Down Expand Up @@ -158,9 +158,9 @@ SECTIONS

.fill :
{
FILL(0xFF);
FILL(0x00);
. = ORIGIN(FLASH) + LENGTH(FLASH) - 1;
BYTE(0xFF)
BYTE(0x00)
} >FLASH

/* Uninitialized data section */
Expand Down
Loading

0 comments on commit 242b84f

Please sign in to comment.