Skip to content

Commit 8737993

Browse files
committed
Rearranged bare metal examples and minor fix to SGDK ones
1 parent 732bd59 commit 8737993

File tree

9 files changed

+183
-51
lines changed

9 files changed

+183
-51
lines changed

examples/md-cpp-test/boot.s

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
.section .data
2+
3+
/* .globl exposes symbols to the linker, and may be referred to in C code as extern */
4+
.globl v_err_reg
5+
.globl v_err_pc
6+
.globl v_err_addr
7+
.globl v_err_ext1
8+
.globl v_err_ext2
9+
.globl v_err_sr
10+
.globl v_err_type
11+
12+
/* Used for the crash handler (see error.c and the error handlers below) */
13+
v_err_reg: ds.l 16
14+
v_err_pc: ds.l 1
15+
v_err_addr: ds.l 1
16+
v_err_ext1: ds.w 1
17+
v_err_ext2: ds.w 1
18+
v_err_sr: ds.w 1
19+
v_err_type: ds.b 1
20+
21+
.section .text
22+
23+
.org 0x00000000 /* Forces linker to put us at the beginning */
24+
25+
RomStart:
26+
dc.l 0x000000 /* Initial stack pointer address */
27+
dc.l _start /* Program start address */
28+
dc.l BusError /* Not thrown on MD */
29+
dc.l AddressError /* Thrown when a W or L instruction uses an odd address */
30+
dc.l IllegalInst /* Thrown when the CPU encounters an invalid instruction */
31+
dc.l ZeroDivide /* Thrown when DIV receives a 0 on the left hand side */
32+
dc.l 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
33+
dc.l 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
34+
dc.l ExtInt, 0 /* External Interrupt */
35+
dc.l HBlank, 0 /* Horizontal Blank Interrupt */
36+
dc.l VBlank, 0 /* Vertical Blank Interrupt */
37+
.rept 8
38+
dc.l 0, 0, 0, 0
39+
.endr
40+
41+
RomHeader:
42+
.ascii "SEGA MEGA DRIVE " /* First 4 bytes must be "SEGA" */
43+
.ascii "GRIND 2022.OCT" /* Copyright and date */
44+
.ascii "Example Project " /* JP Name */
45+
.ascii "Example Project " /* EN Name */
46+
.ascii "GM CHANGEME-XX" /* Serial No. */
47+
dc.w 0
48+
.ascii "J " /* Controller support */
49+
dc.l 0x000000 /* ROM Start */
50+
dc.l 0x3FFFFF /* ROM End (4MB) */
51+
dc.l 0xFF0000 /* RAM Start */
52+
dc.l 0xFFFFFF /* RAM End (64KB) */
53+
.ascii "RA" /* "RA" to enable SRAM, " " to disable */
54+
dc.w 0xF820 /* SRAM writes to odd bytes */
55+
dc.l 0x200001 /* SRAM Start */
56+
dc.l 0x20FFFF /* SRAM End (32KB) */
57+
.ascii " "
58+
.ascii " "
59+
.ascii "JUE " /* Region */
60+
61+
_start:
62+
move #0x2700,sr /* Disable interrupts */
63+
move.b (0xA10001),d0 /* Check console version */
64+
andi.b #0x0F,d0 /* Version 0 = skip TMSS */
65+
beq.s NoTMSS
66+
move.l (0x100),0xA14000 /* Write 'SEGA' to TMSS register */
67+
NoTMSS:
68+
move.w (0xC00004),d0 /* Read VDP status */
69+
move.w #0x0100,(0xA11100) /* Halt / Reset Z80 */
70+
move.w #0x0100,(0xA11200)
71+
.globl _hard_reset
72+
_hard_reset: /* SYS_HardReset() resets sp and jumps here */
73+
lea 0xFF0000,a0 /* First RAM address */
74+
moveq #0,d0
75+
move.w #0x3FFF,d1 /* (Size of RAM - 1) / Size of long */
76+
ClearRam:
77+
move.l d0,(a0)+
78+
dbra d1,ClearRam
79+
lea _stext,a0 /* Start of initialized data (BSS) in ROM */
80+
lea 0xFF0000,a1 /* First RAM address */
81+
move.l #_sdata,d0 /* (Size of BSS + 1) / 2 */
82+
addq.l #1,d0
83+
lsr.l #1,d0
84+
beq NoCopy
85+
subq.w #1,d0 /* sub extra iteration */
86+
CopyVar:
87+
move.w (a0)+,(a1)+ /* Copy initialized data to RAM */
88+
dbra d0,CopyVar
89+
NoCopy:
90+
jsr main /* IT BEGINS */
91+
beq.s _hard_reset /* main returned, reset */
92+
93+
/* Error handling */
94+
95+
BusError:
96+
move.b #0,(v_err_type)
97+
bra.s AddressDump
98+
99+
AddressError:
100+
move.b #1,(v_err_type)
101+
bra.s AddressDump
102+
103+
IllegalInst:
104+
move.b #2,(v_err_type)
105+
bra.s IllegalDump
106+
107+
ZeroDivide:
108+
move.b #3,(v_err_type)
109+
bra.s ZeroDump
110+
111+
AddressDump:
112+
move.w 4(sp),v_err_ext1
113+
move.l 6(sp),v_err_addr
114+
move.w 10(sp),v_err_ext2
115+
move.w 12(sp),v_err_sr
116+
move.l 14(sp),v_err_pc
117+
bra.s RegDump
118+
IllegalDump:
119+
move.w 10(sp),v_err_ext1
120+
ZeroDump:
121+
move.w 4(sp),v_err_sr
122+
move.l 6(sp),v_err_pc
123+
RegDump:
124+
move.l d0,v_err_reg+0
125+
move.l d1,v_err_reg+4
126+
move.l d2,v_err_reg+8
127+
move.l d3,v_err_reg+12
128+
move.l d4,v_err_reg+16
129+
move.l d5,v_err_reg+20
130+
move.l d6,v_err_reg+24
131+
move.l d7,v_err_reg+28
132+
move.l a0,v_err_reg+32
133+
move.l a1,v_err_reg+36
134+
move.l a2,v_err_reg+40
135+
move.l a3,v_err_reg+44
136+
move.l a4,v_err_reg+48
137+
move.l a5,v_err_reg+52
138+
move.l a6,v_err_reg+56
139+
move.l a7,v_err_reg+60
140+
jmp _error
141+
142+
/* Standard interrupts */
143+
144+
ExtInt:
145+
rte
146+
147+
HBlank:
148+
rte
149+
150+
VBlank:
151+
rte

examples/md-newlib-test/Makefile

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ ASMO = $(RESS:.res=.o)
5757
ASMO += $(Z80S:.s80=.o)
5858
ASMO += $(CS:%.c=asmout/%.s)
5959

60-
# Need to keep the .elf to extract the symbol table from it
61-
.SECONDARY: out.elf
62-
6360
.PHONY: all release asm debug
64-
6561
all: release
6662

6763
release: OPTIONS = -O3 -fno-web -fno-gcse -fno-unit-at-a-time -fomit-frame-pointer
@@ -79,19 +75,18 @@ debug: out.bin symbol.txt
7975
# Generates a symbol table that is very helpful in debugging crashes
8076
# Cross reference symbol.txt with the addresses displayed in the crash handler
8177
symbol.txt: out.bin
82-
$(NM) --plugin=$(PLUGIN)/$(LTO_SO) -n out.elf > symbol.txt
83-
84-
boot.o:
85-
$(AS) $(ASFLAGS) boot.s -o $@
78+
@echo "Creating symbol.txt"
79+
@$(NM) --plugin=$(PLUGIN)/$(LTO_SO) -n out.elf > symbol.txt
8680

8781
%.bin: %.elf
8882
@echo "Stripping ELF header..."
8983
@$(OBJC) -O binary $< temp.bin
90-
@dd if=temp.bin of=$@ bs=8192 conv=sync
84+
@dd if=temp.bin of=$@ bs=8K conv=sync
9185
@rm -f temp.bin
9286

93-
%.elf: boot.o $(OBJS)
94-
$(CC) -o $@ $(LDFLAGS) boot.o $(OBJS) $(LIBS)
87+
%.elf: $(OBJS)
88+
@echo "Linking $@"
89+
@$(CC) -o $@ $(LDFLAGS) $(OBJS) $(LIBS)
9590

9691
%.o: %.c
9792
@echo "CC $<"
@@ -113,7 +108,6 @@ asmout/%.s: %.c
113108
$(CC) $(CCFLAGS) $(OPTIONS) $(INCS) -S $< -o $@
114109

115110
.PHONY: clean
116-
117111
clean:
118112
rm -f $(OBJS) out.bin out.elf symbol.txt boot.o
119113
rm -rf asmout
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ v_err_ext2: ds.w 1
1818
v_err_sr: ds.w 1
1919
v_err_type: ds.b 1
2020

21-
.section .text
21+
.section .text.keepboot
2222

2323
.org 0x00000000 /* Forces linker to put us at the beginning */
2424

examples/md-skeleton/Makefile

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ ASMO = $(RESS:.res=.o)
5555
ASMO += $(Z80S:.s80=.o)
5656
ASMO += $(CS:%.c=asmout/%.s)
5757

58-
# Need to keep the .elf to extract the symbol table from it
59-
.SECONDARY: out.elf
60-
6158
.PHONY: all release asm debug
62-
6359
all: release
6460

6561
release: OPTIONS = -O3 -fno-web -fno-gcse -fno-unit-at-a-time -fomit-frame-pointer
@@ -77,19 +73,18 @@ debug: out.bin symbol.txt
7773
# Generates a symbol table that is very helpful in debugging crashes
7874
# Cross reference symbol.txt with the addresses displayed in the crash handler
7975
symbol.txt: out.bin
80-
$(NM) --plugin=$(PLUGIN)/$(LTO_SO) -n out.elf > symbol.txt
81-
82-
boot.o:
83-
$(AS) $(ASFLAGS) boot.s -o $@
76+
@echo "Creating symbol.txt"
77+
@$(NM) --plugin=$(PLUGIN)/$(LTO_SO) -n out.elf > symbol.txt
8478

8579
%.bin: %.elf
8680
@echo "Stripping ELF header..."
8781
@$(OBJC) -O binary $< temp.bin
88-
@dd if=temp.bin of=$@ bs=8192 conv=sync
82+
@dd if=temp.bin of=$@ bs=8K conv=sync
8983
@rm -f temp.bin
9084

91-
%.elf: boot.o $(OBJS)
92-
$(CC) -o $@ $(LDFLAGS) boot.o $(OBJS) $(LIBS)
85+
%.elf: $(OBJS)
86+
@echo "Linking $@"
87+
@$(CC) -o $@ $(LDFLAGS) $(OBJS) $(LIBS)
9388

9489
%.o: %.c
9590
@echo "CC $<"
@@ -111,7 +106,6 @@ asmout/%.s: %.c
111106
$(CC) $(CCFLAGS) $(OPTIONS) $(INCS) -S $< -o $@
112107

113108
.PHONY: clean
114-
115109
clean:
116110
rm -f $(OBJS) out.bin out.elf symbol.txt boot.o
117111
rm -rf asmout
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ v_err_ext2: ds.w 1
1818
v_err_sr: ds.w 1
1919
v_err_type: ds.b 1
2020

21-
.section .text
21+
.section .text.keepboot
2222

2323
.org 0x00000000 /* Forces linker to put us at the beginning */
2424

examples/sgdk-skeleton/Makefile

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ OBJS += $(SS:.s=.o)
6767
ASMO = $(RESS:.res=.o)
6868
ASMO += $(CS:%.c=asmout/%.s)
6969

70-
.SECONDARY: out.elf
71-
7270
.PHONY: all release asm debug
73-
7471
all: release
7572

7673
release: OPTIONS = -O3 -fno-web -fno-gcse -fno-unit-at-a-time -fomit-frame-pointer
@@ -91,7 +88,8 @@ debug: out.bin symbol.txt
9188
# Generates a symbol table that is very helpful in debugging crashes
9289
# Cross reference symbol.txt with the addresses displayed in the crash handler
9390
symbol.txt: out.bin
94-
$(NM) --plugin=$(PLUGIN)/$(LTO_SO) -n out.elf > symbol.txt
91+
@echo "Creating symbol.txt"
92+
@$(NM) --plugin=$(PLUGIN)/$(LTO_SO) -n out.elf > symbol.txt
9593

9694
boot/sega.o: boot/rom_head.bin
9795
@echo "AS boot/sega.s"
@@ -102,7 +100,7 @@ boot/rom_head.bin: boot/rom_head.o
102100

103101
boot/rom_head.o: boot/rom_head.c
104102
@echo "CC $<"
105-
@$(CC) $(CFLAGS) $(INCS) -c $< -o $@
103+
@$(CC) $(CCFLAGS) $(INCS) -c $< -o $@
106104

107105
%.bin: %.elf
108106
@echo "Stripping ELF header..."
@@ -111,7 +109,8 @@ boot/rom_head.o: boot/rom_head.c
111109
@rm -f temp.bin
112110

113111
%.elf: boot/sega.o $(OBJS)
114-
$(CC) -o $@ $(LDFLAGS) boot/sega.o $(OBJS) $(LIBS)
112+
@echo "Linking $@"
113+
@$(CC) -o $@ $(LDFLAGS) boot/sega.o $(OBJS) $(LIBS)
115114

116115
%.o: %.c
117116
@echo "CC $<"
@@ -136,7 +135,6 @@ asmout/%.s: %.c
136135
$(CC) $(CCFLAGS) $(OPTIONS) $(INCS) -S $< -o $@
137136

138137
.PHONY: clean
139-
140138
clean:
141139
rm -f $(OBJS) out.bin out.elf symbol.txt
142140
rm -f boot/sega.o boot/rom_head.o boot/rom_head.bin

examples/sgdk-ssfmapper/Makefile

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,29 @@ OBJS += $(SS:.s=.o)
6767
ASMO = $(RESS:.res=.o)
6868
ASMO += $(CS:%.c=asmout/%.s)
6969

70-
.SECONDARY: out.elf
71-
7270
.PHONY: all release asm debug
73-
7471
all: release
7572

7673
release: OPTIONS = -O3 -fno-web -fno-gcse -fno-unit-at-a-time -fomit-frame-pointer
7774
release: OPTIONS += -fshort-enums -flto -fuse-linker-plugin
78-
release: LIBS += -L$(MARSDEV)/m68k-elf/lib -lmd
75+
release: LIBS += -L$(MARSDEV)/m68k-elf/lib -lmd-far
7976
release: out.bin symbol.txt
8077

8178
asm: OPTIONS = -O3 -fno-web -fno-gcse -fno-unit-at-a-time -fomit-frame-pointer
8279
asm: OPTIONS += -fshort-enums
83-
asm: LIBS += -L$(MARSDEV)/m68k-elf/lib -lmd
80+
asm: LIBS += -L$(MARSDEV)/m68k-elf/lib -lmd-far
8481
asm: asm-dir $(ASMO)
8582

8683
# Gens-KMod, BlastEm and UMDK support GDB tracing, enabled by this target
8784
debug: OPTIONS = -g -Og -DDEBUG -DKDEBUG -fno-web -fno-gcse -fno-unit-at-a-time -fshort-enums
88-
debug: LIBS += -L$(MARSDEV)/m68k-elf/lib -lmd-debug
85+
debug: LIBS += -L$(MARSDEV)/m68k-elf/lib -lmd-far-debug
8986
debug: out.bin symbol.txt
9087

9188
# Generates a symbol table that is very helpful in debugging crashes
9289
# Cross reference symbol.txt with the addresses displayed in the crash handler
9390
symbol.txt: out.bin
94-
$(NM) --plugin=$(PLUGIN)/$(LTO_SO) -n out.elf > symbol.txt
91+
@echo "Creating symbol.txt"
92+
@$(NM) --plugin=$(PLUGIN)/$(LTO_SO) -n out.elf > symbol.txt
9593

9694
boot/sega.o: boot/rom_head.bin
9795
@echo "AS boot/sega.s"
@@ -102,7 +100,7 @@ boot/rom_head.bin: boot/rom_head.o
102100

103101
boot/rom_head.o: boot/rom_head.c
104102
@echo "CC $<"
105-
@$(CC) $(CFLAGS) $(INCS) -c $< -o $@
103+
@$(CC) $(CCFLAGS) $(INCS) -c $< -o $@
106104

107105
%.bin: %.elf
108106
@echo "Stripping ELF header..."
@@ -111,7 +109,8 @@ boot/rom_head.o: boot/rom_head.c
111109
@rm -f temp.bin
112110

113111
%.elf: boot/sega.o $(OBJS)
114-
$(CC) -o $@ $(LDFLAGS) boot/sega.o $(OBJS) $(LIBS)
112+
@echo "Linking $@"
113+
@$(CC) -o $@ $(LDFLAGS) boot/sega.o $(OBJS) $(LIBS)
115114

116115
%.o: %.c
117116
@echo "CC $<"
@@ -136,7 +135,6 @@ asmout/%.s: %.c
136135
$(CC) $(CCFLAGS) $(OPTIONS) $(INCS) -S $< -o $@
137136

138137
.PHONY: clean
139-
140138
clean:
141139
rm -f $(OBJS) out.bin out.elf symbol.txt
142140
rm -f boot/sega.o boot/rom_head.o boot/rom_head.bin

0 commit comments

Comments
 (0)