Skip to content

Commit

Permalink
New Error Handler with backtrace and symbol support (#377)
Browse files Browse the repository at this point in the history
* New error handler from MD Debugger project with backtrace and source code symbols
  The original SGDK's error handler is available via LEGACY_ERROR_HANDLER option.
* Makefile.gen: Remove redundant character in `convsym` format string
* Add ConvSym 2.12 source code
* Split error handler from `sega.s` to `error_a.s`
  • Loading branch information
vladikcomper authored Dec 12, 2024
1 parent de4c620 commit 40071af
Show file tree
Hide file tree
Showing 58 changed files with 4,075 additions and 2,835 deletions.
Binary file added bin/convsym.exe
Binary file not shown.
2 changes: 2 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ifeq ($(OS),Windows_NT)
LD:= $(BIN)/ld
NM:= $(BIN)/nm
OBJCPY := $(BIN)/objcopy
CONVSYM := $(BIN)/convsym
ASMZ80 := $(BIN)/sjasm
MACCER := $(BIN)/mac68k
BINTOS := $(BIN)/bintos
Expand All @@ -37,6 +38,7 @@ else
LD := $(PREFIX)ld
NM := $(PREFIX)nm
OBJCPY := $(PREFIX)objcopy
CONVSYM := convsym
ASMZ80 := sjasm
MACCER := mac68k
BINTOS := bintos
Expand Down
10 changes: 10 additions & 0 deletions inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
*/
#define LEGACY_SPRITE_ENGINE 0

/**
* \brief
* Set it to 1 to use the original SGDK's error handling screen and vectors.<br>
* Otherwise error handler from the MD Debugger project is used, which supports source code symbols with "debug" build proifle and backtrace.<br>
*
* The new error handler, however, completely takes over exception vectors and doesn't allow to set your own callbacks for errors (e.g. addressErrorCB).<br>
* If you rely on callbacks to override error handling behavior, consider using legacy system instead.
*/
#define LEGACY_ERROR_HANDLER 0

/**
* \brief
* Set it to 1 to enable automatic bank switch using official SEGA mapper for ROM > 4MB.
Expand Down
4 changes: 3 additions & 1 deletion inc/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ typedef enum
ON_VBLANK_START /** Start VBlank process on VBlank *start* period, means that we wait the next *start* of VBlank period if we missed it */
} VBlankProcessTime;


#if LEGACY_ERROR_HANDLER
/**
* \brief
* Bus error interrupt callback.
Expand Down Expand Up @@ -164,6 +164,8 @@ extern VoidCallback *line1x1xCB;
* You can modify it to use your own callback (for debug purpose).
*/
extern VoidCallback *errorExceptionCB;
#endif

/**
* \brief
* Level interrupt callback.
Expand Down
Binary file modified lib/libmd.a
Binary file not shown.
Binary file modified lib/libmd_debug.a
Binary file not shown.
12 changes: 8 additions & 4 deletions makefile.gen
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ release: $(OUT)/rom.bin $(OUT)/symbol.txt
#release: $(info $$var is [${SRC_C}])

debug: FLAGS= $(DEFAULT_FLAGS) -O1 -DDEBUG=1
debug: CFLAGS= $(FLAGS) -ggdb
debug: CFLAGS= $(FLAGS) -ggdb -g
debug: AFLAGS= $(FLAGS)
debug: LIBMD= $(LIB)/libmd_debug.a
debug: $(OUT)/rom.bin $(OUT)/rom.out $(OUT)/symbol.txt
.PHONY: debug
debug: injectSymbolsInROM
.PHONY: debug injectSymbolsInROM

asm: FLAGS= $(DEFAULT_FLAGS) -O3 -fuse-linker-plugin -fno-web -fno-gcse -fomit-frame-pointer -S
asm: CFLAGS= $(FLAGS)
Expand Down Expand Up @@ -159,12 +160,15 @@ cleanAsm: cleanasm
.PHONY: cleanRelease cleanDebug cleanAsm


$(OUT)/rom.bin: $(OUT)/rom.out
injectSymbolsInROM: $(OUT)/rom.bin $(OUT)/rom.out $(OUT)/symbol.txt
$(CONVSYM) $(OUT)/symbol.txt $(OUT)/rom.bin -in txt -inopt "/fmt='%X %*[TtBbCcDd] %511s' /offsetFirst+" -range 0 FFFFFF -a -ref @MDDBG__SymbolDataPtr

$(OUT)/rom.bin: $(OUT)/rom.out $(OUT)/symbol.txt
$(OBJCPY) -O binary $(OUT)/rom.out $(OUT)/rom.bin
$(SIZEBND) $(OUT)/rom.bin -sizealign 131072 -checksum

$(OUT)/symbol.txt: $(OUT)/rom.out
$(NM) $(LTO_PLUGIN) -n $(OUT)/rom.out > $(OUT)/symbol.txt
$(NM) $(LTO_PLUGIN) -nl $(OUT)/rom.out > $(OUT)/symbol.txt

$(OUT)/rom.out: $(OUT)/sega.o $(OUT)/cmd_ $(LIBMD)
$(MKDIR) -p $(dir $@)
Expand Down
167 changes: 19 additions & 148 deletions sample/basics/hello-world/src/boot/sega.s
Original file line number Diff line number Diff line change
Expand Up @@ -52,175 +52,46 @@ rom_header:
.incbin "out/rom_head.bin", 0, 0x100

_Entry_Point:
* disable interrupts
move #0x2700,%sr

* Configure a USER_STACK_LENGTH bytes user stack at bottom, and system stack on top of it
move %sp, %usp
sub #USER_STACK_LENGTH, %sp

* Halt Z80 (need to be done as soon as possible on reset)
move.l #0xA11100,%a0 /* Z80_HALT_PORT */
move.w #0x0100,%d0
move.w %d0,(%a0) /* HALT Z80 */
move.w %d0,0x0100(%a0) /* END RESET Z80 */

tst.l 0xa10008
bne.s SkipJoyDetect
bne.s SkipInit

tst.w 0xa1000c
bne.s SkipInit

SkipJoyDetect:
bne.s SkipSetup

lea Table,%a5
movem.w (%a5)+,%d5-%d7
movem.l (%a5)+,%a0-%a4
* Check Version Number
move.b -0x10ff(%a1),%d0
move.b -0x10ff(%a0),%d0
andi.b #0x0f,%d0
beq.s WrongVersion
beq.s NoTMSS

* Sega Security Code (SEGA)
move.l #0x53454741,0x2f00(%a1)
WrongVersion:
* Read from the control port to cancel any pending read/write command
move.w (%a4),%d0

* Configure a USER_STACK_LENGTH bytes user stack at bottom, and system stack on top of it
move %sp, %usp
sub #USER_STACK_LENGTH, %sp

move.w %d7,(%a1)
move.w %d7,(%a2)

* Jump to initialisation process now...
move.l #0x53454741,0x2f00(%a0)

NoTMSS:
jmp _start_entry

SkipSetup:
SkipInit:
jmp _reset_entry


Table:
dc.w 0x8000,0x3fff,0x0100
dc.l 0xA00000,0xA11100,0xA11200,0xC00000,0xC00004


*------------------------------------------------
*
* interrupt functions
*
*------------------------------------------------

registersDump:
move.l %d0,registerState+0
move.l %d1,registerState+4
move.l %d2,registerState+8
move.l %d3,registerState+12
move.l %d4,registerState+16
move.l %d5,registerState+20
move.l %d6,registerState+24
move.l %d7,registerState+28
move.l %a0,registerState+32
move.l %a1,registerState+36
move.l %a2,registerState+40
move.l %a3,registerState+44
move.l %a4,registerState+48
move.l %a5,registerState+52
move.l %a6,registerState+56
move.l %a7,registerState+60
rts

busAddressErrorDump:
move.w 4(%sp),ext1State
move.l 6(%sp),addrState
move.w 10(%sp),ext2State
move.w 12(%sp),srState
move.l 14(%sp),pcState
jmp registersDump

exception4WDump:
move.w 4(%sp),srState
move.l 6(%sp),pcState
move.w 10(%sp),ext1State
jmp registersDump

exceptionDump:
move.w 4(%sp),srState
move.l 6(%sp),pcState
jmp registersDump


_Bus_Error:
jsr busAddressErrorDump
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l busErrorCB, %a0
jsr (%a0)
movem.l (%sp)+,%d0-%d1/%a0-%a1
rte

_Address_Error:
jsr busAddressErrorDump
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l addressErrorCB, %a0
jsr (%a0)
movem.l (%sp)+,%d0-%d1/%a0-%a1
rte

_Illegal_Instruction:
jsr exception4WDump
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l illegalInstCB, %a0
jsr (%a0)
movem.l (%sp)+,%d0-%d1/%a0-%a1
rte

_Zero_Divide:
jsr exceptionDump
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l zeroDivideCB, %a0
jsr (%a0)
movem.l (%sp)+,%d0-%d1/%a0-%a1
rte

_Chk_Instruction:
jsr exception4WDump
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l chkInstCB, %a0
jsr (%a0)
movem.l (%sp)+,%d0-%d1/%a0-%a1
rte

_Trapv_Instruction:
jsr exception4WDump
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l trapvInstCB, %a0
jsr (%a0)
movem.l (%sp)+,%d0-%d1/%a0-%a1
rte

_Privilege_Violation:
jsr exceptionDump
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l privilegeViolationCB, %a0
jsr (%a0)
movem.l (%sp)+,%d0-%d1/%a0-%a1
rte

_Trace:
jsr exceptionDump
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l traceCB, %a0
jsr (%a0)
movem.l (%sp)+,%d0-%d1/%a0-%a1
rte

_Line_1010_Emulation:
_Line_1111_Emulation:
jsr exceptionDump
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l line1x1xCB, %a0
jsr (%a0)
movem.l (%sp)+,%d0-%d1/%a0-%a1
rte

_Error_Exception:
jsr exceptionDump
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l errorExceptionCB, %a0
jsr (%a0)
movem.l (%sp)+,%d0-%d1/%a0-%a1
rte

_INT:
movem.l %d0-%d1/%a0-%a1,-(%sp)
move.l intCB, %a0
Expand Down
Loading

0 comments on commit 40071af

Please sign in to comment.