Skip to content

Commit 40071af

Browse files
authored
New Error Handler with backtrace and symbol support (#377)
* 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`
1 parent de4c620 commit 40071af

File tree

58 files changed

+4075
-2835
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+4075
-2835
lines changed

bin/convsym.exe

2.69 MB
Binary file not shown.

common.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ifeq ($(OS),Windows_NT)
1919
LD:= $(BIN)/ld
2020
NM:= $(BIN)/nm
2121
OBJCPY := $(BIN)/objcopy
22+
CONVSYM := $(BIN)/convsym
2223
ASMZ80 := $(BIN)/sjasm
2324
MACCER := $(BIN)/mac68k
2425
BINTOS := $(BIN)/bintos
@@ -37,6 +38,7 @@ else
3738
LD := $(PREFIX)ld
3839
NM := $(PREFIX)nm
3940
OBJCPY := $(PREFIX)objcopy
41+
CONVSYM := convsym
4042
ASMZ80 := sjasm
4143
MACCER := mac68k
4244
BINTOS := bintos

inc/config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@
8181
*/
8282
#define LEGACY_SPRITE_ENGINE 0
8383

84+
/**
85+
* \brief
86+
* Set it to 1 to use the original SGDK's error handling screen and vectors.<br>
87+
* Otherwise error handler from the MD Debugger project is used, which supports source code symbols with "debug" build proifle and backtrace.<br>
88+
*
89+
* 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>
90+
* If you rely on callbacks to override error handling behavior, consider using legacy system instead.
91+
*/
92+
#define LEGACY_ERROR_HANDLER 0
93+
8494
/**
8595
* \brief
8696
* Set it to 1 to enable automatic bank switch using official SEGA mapper for ROM > 4MB.

inc/sys.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ typedef enum
9393
ON_VBLANK_START /** Start VBlank process on VBlank *start* period, means that we wait the next *start* of VBlank period if we missed it */
9494
} VBlankProcessTime;
9595

96-
96+
#if LEGACY_ERROR_HANDLER
9797
/**
9898
* \brief
9999
* Bus error interrupt callback.
@@ -164,6 +164,8 @@ extern VoidCallback *line1x1xCB;
164164
* You can modify it to use your own callback (for debug purpose).
165165
*/
166166
extern VoidCallback *errorExceptionCB;
167+
#endif
168+
167169
/**
168170
* \brief
169171
* Level interrupt callback.

lib/libmd.a

1010 KB
Binary file not shown.

lib/libmd_debug.a

-18.7 KB
Binary file not shown.

makefile.gen

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ release: $(OUT)/rom.bin $(OUT)/symbol.txt
8181
#release: $(info $$var is [${SRC_C}])
8282

8383
debug: FLAGS= $(DEFAULT_FLAGS) -O1 -DDEBUG=1
84-
debug: CFLAGS= $(FLAGS) -ggdb
84+
debug: CFLAGS= $(FLAGS) -ggdb -g
8585
debug: AFLAGS= $(FLAGS)
8686
debug: LIBMD= $(LIB)/libmd_debug.a
8787
debug: $(OUT)/rom.bin $(OUT)/rom.out $(OUT)/symbol.txt
88-
.PHONY: debug
88+
debug: injectSymbolsInROM
89+
.PHONY: debug injectSymbolsInROM
8990

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

161162

162-
$(OUT)/rom.bin: $(OUT)/rom.out
163+
injectSymbolsInROM: $(OUT)/rom.bin $(OUT)/rom.out $(OUT)/symbol.txt
164+
$(CONVSYM) $(OUT)/symbol.txt $(OUT)/rom.bin -in txt -inopt "/fmt='%X %*[TtBbCcDd] %511s' /offsetFirst+" -range 0 FFFFFF -a -ref @MDDBG__SymbolDataPtr
165+
166+
$(OUT)/rom.bin: $(OUT)/rom.out $(OUT)/symbol.txt
163167
$(OBJCPY) -O binary $(OUT)/rom.out $(OUT)/rom.bin
164168
$(SIZEBND) $(OUT)/rom.bin -sizealign 131072 -checksum
165169

166170
$(OUT)/symbol.txt: $(OUT)/rom.out
167-
$(NM) $(LTO_PLUGIN) -n $(OUT)/rom.out > $(OUT)/symbol.txt
171+
$(NM) $(LTO_PLUGIN) -nl $(OUT)/rom.out > $(OUT)/symbol.txt
168172

169173
$(OUT)/rom.out: $(OUT)/sega.o $(OUT)/cmd_ $(LIBMD)
170174
$(MKDIR) -p $(dir $@)

sample/basics/hello-world/src/boot/sega.s

Lines changed: 19 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -52,175 +52,46 @@ rom_header:
5252
.incbin "out/rom_head.bin", 0, 0x100
5353

5454
_Entry_Point:
55+
* disable interrupts
5556
move #0x2700,%sr
57+
58+
* Configure a USER_STACK_LENGTH bytes user stack at bottom, and system stack on top of it
59+
move %sp, %usp
60+
sub #USER_STACK_LENGTH, %sp
61+
62+
* Halt Z80 (need to be done as soon as possible on reset)
63+
move.l #0xA11100,%a0 /* Z80_HALT_PORT */
64+
move.w #0x0100,%d0
65+
move.w %d0,(%a0) /* HALT Z80 */
66+
move.w %d0,0x0100(%a0) /* END RESET Z80 */
67+
5668
tst.l 0xa10008
57-
bne.s SkipJoyDetect
69+
bne.s SkipInit
5870

5971
tst.w 0xa1000c
72+
bne.s SkipInit
6073

61-
SkipJoyDetect:
62-
bne.s SkipSetup
63-
64-
lea Table,%a5
65-
movem.w (%a5)+,%d5-%d7
66-
movem.l (%a5)+,%a0-%a4
6774
* Check Version Number
68-
move.b -0x10ff(%a1),%d0
75+
move.b -0x10ff(%a0),%d0
6976
andi.b #0x0f,%d0
70-
beq.s WrongVersion
77+
beq.s NoTMSS
7178

7279
* Sega Security Code (SEGA)
73-
move.l #0x53454741,0x2f00(%a1)
74-
WrongVersion:
75-
* Read from the control port to cancel any pending read/write command
76-
move.w (%a4),%d0
77-
78-
* Configure a USER_STACK_LENGTH bytes user stack at bottom, and system stack on top of it
79-
move %sp, %usp
80-
sub #USER_STACK_LENGTH, %sp
81-
82-
move.w %d7,(%a1)
83-
move.w %d7,(%a2)
84-
85-
* Jump to initialisation process now...
80+
move.l #0x53454741,0x2f00(%a0)
8681

82+
NoTMSS:
8783
jmp _start_entry
8884

89-
SkipSetup:
85+
SkipInit:
9086
jmp _reset_entry
9187

9288

93-
Table:
94-
dc.w 0x8000,0x3fff,0x0100
95-
dc.l 0xA00000,0xA11100,0xA11200,0xC00000,0xC00004
96-
97-
9889
*------------------------------------------------
9990
*
10091
* interrupt functions
10192
*
10293
*------------------------------------------------
10394

104-
registersDump:
105-
move.l %d0,registerState+0
106-
move.l %d1,registerState+4
107-
move.l %d2,registerState+8
108-
move.l %d3,registerState+12
109-
move.l %d4,registerState+16
110-
move.l %d5,registerState+20
111-
move.l %d6,registerState+24
112-
move.l %d7,registerState+28
113-
move.l %a0,registerState+32
114-
move.l %a1,registerState+36
115-
move.l %a2,registerState+40
116-
move.l %a3,registerState+44
117-
move.l %a4,registerState+48
118-
move.l %a5,registerState+52
119-
move.l %a6,registerState+56
120-
move.l %a7,registerState+60
121-
rts
122-
123-
busAddressErrorDump:
124-
move.w 4(%sp),ext1State
125-
move.l 6(%sp),addrState
126-
move.w 10(%sp),ext2State
127-
move.w 12(%sp),srState
128-
move.l 14(%sp),pcState
129-
jmp registersDump
130-
131-
exception4WDump:
132-
move.w 4(%sp),srState
133-
move.l 6(%sp),pcState
134-
move.w 10(%sp),ext1State
135-
jmp registersDump
136-
137-
exceptionDump:
138-
move.w 4(%sp),srState
139-
move.l 6(%sp),pcState
140-
jmp registersDump
141-
142-
143-
_Bus_Error:
144-
jsr busAddressErrorDump
145-
movem.l %d0-%d1/%a0-%a1,-(%sp)
146-
move.l busErrorCB, %a0
147-
jsr (%a0)
148-
movem.l (%sp)+,%d0-%d1/%a0-%a1
149-
rte
150-
151-
_Address_Error:
152-
jsr busAddressErrorDump
153-
movem.l %d0-%d1/%a0-%a1,-(%sp)
154-
move.l addressErrorCB, %a0
155-
jsr (%a0)
156-
movem.l (%sp)+,%d0-%d1/%a0-%a1
157-
rte
158-
159-
_Illegal_Instruction:
160-
jsr exception4WDump
161-
movem.l %d0-%d1/%a0-%a1,-(%sp)
162-
move.l illegalInstCB, %a0
163-
jsr (%a0)
164-
movem.l (%sp)+,%d0-%d1/%a0-%a1
165-
rte
166-
167-
_Zero_Divide:
168-
jsr exceptionDump
169-
movem.l %d0-%d1/%a0-%a1,-(%sp)
170-
move.l zeroDivideCB, %a0
171-
jsr (%a0)
172-
movem.l (%sp)+,%d0-%d1/%a0-%a1
173-
rte
174-
175-
_Chk_Instruction:
176-
jsr exception4WDump
177-
movem.l %d0-%d1/%a0-%a1,-(%sp)
178-
move.l chkInstCB, %a0
179-
jsr (%a0)
180-
movem.l (%sp)+,%d0-%d1/%a0-%a1
181-
rte
182-
183-
_Trapv_Instruction:
184-
jsr exception4WDump
185-
movem.l %d0-%d1/%a0-%a1,-(%sp)
186-
move.l trapvInstCB, %a0
187-
jsr (%a0)
188-
movem.l (%sp)+,%d0-%d1/%a0-%a1
189-
rte
190-
191-
_Privilege_Violation:
192-
jsr exceptionDump
193-
movem.l %d0-%d1/%a0-%a1,-(%sp)
194-
move.l privilegeViolationCB, %a0
195-
jsr (%a0)
196-
movem.l (%sp)+,%d0-%d1/%a0-%a1
197-
rte
198-
199-
_Trace:
200-
jsr exceptionDump
201-
movem.l %d0-%d1/%a0-%a1,-(%sp)
202-
move.l traceCB, %a0
203-
jsr (%a0)
204-
movem.l (%sp)+,%d0-%d1/%a0-%a1
205-
rte
206-
207-
_Line_1010_Emulation:
208-
_Line_1111_Emulation:
209-
jsr exceptionDump
210-
movem.l %d0-%d1/%a0-%a1,-(%sp)
211-
move.l line1x1xCB, %a0
212-
jsr (%a0)
213-
movem.l (%sp)+,%d0-%d1/%a0-%a1
214-
rte
215-
216-
_Error_Exception:
217-
jsr exceptionDump
218-
movem.l %d0-%d1/%a0-%a1,-(%sp)
219-
move.l errorExceptionCB, %a0
220-
jsr (%a0)
221-
movem.l (%sp)+,%d0-%d1/%a0-%a1
222-
rte
223-
22495
_INT:
22596
movem.l %d0-%d1/%a0-%a1,-(%sp)
22697
move.l intCB, %a0

0 commit comments

Comments
 (0)