c语言 - (Game Boy Advance)计划从0x00000000开始,而不是0x08000000.如何告诉 gcc



我让DevKitPro开发Game Boy Advance,但我遇到了一些问题。我看到的最大问题是我的代码是在0x00000000而不是 ROM 盒的正常0x08000000组装的。我的理解是,C 编译器不使用.org指令在指定的内存位置创建代码;相反,链接器应该为我处理所有这些。但它似乎将代码放在"错误"的地址。游戏将正常运行,但我想这是因为它在模拟器上运行,模拟器不在乎它是否位于不应该位于的地方。如何让代码在0x08000000"组装"?

我对 makefile、编译器、链接器等的概念很陌生,所以我可能把所有东西都设置得很差。我将展示我的生成文件、运行生成文件的批处理脚本以及正在编译的 C 代码。我还包含了 objdump 以防相关。

C 代码:

// LIBGBA HEADERS
#include <gba_console.h>
#include <gba_video.h>
#include <gba_interrupt.h>
#include <gba_systemcalls.h>
#include <gba_input.h>
#include <stdio.h>
#include <stdlib.h>
// GAME-SPECIFIC INCLUDES
#include "M:SrcGBAPaintBoyAdvanceincludebitmap.h"   
#include "M:SrcGBAPaintBoyAdvanceincludebitmap.c"   //BITMAP SCREEN FUNCTIONS
//---------------------------------------------------------------------------------
// Program entry point
//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------

// the vblank interrupt must be enabled for VBlankIntrWait() to work 
// since the default dispatcher handles the bios flags no vblank handler
// is required
irqInit();
irqEnable(IRQ_VBLANK);

// consoleDemoInit();
REG_DISPCNT = 0x1403;

while (1) {
VBlankIntrWait();
}
}

批处理脚本:

@echo off
set path=C:devkitPro;%path%
cd M:SrcGBAPaintBoyAdvance
make
if not "%errorlevel%"=="0" goto Abandon
C:devkitProdevkitARMbinarm-none-eabi-objdump -h M:SrcGBAPaintBoyAdvancebuildpaintboyadvance.o
C:UserspuppyDocumentsVisualBoyAdvancevisualboyadvance-m.exe M:SrcGBAPaintBoyAdvancePaintBoyAdvance.gba
:Abandon
exit

制作文件:

#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif
include $(DEVKITARM)/gba_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
# DATA is a list of directories containing binary data
# GRAPHICS is a list of directories containing files to be processed by grit
#
# All directories are specified relative to the project directory where
# the makefile is found
#
#---------------------------------------------------------------------------------
TARGET      := $(notdir $(CURDIR))
BUILD       := build
SOURCES     := source
INCLUDES    := include
DATA        := data
MUSIC       :=
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH    :=  -mthumb -mthumb-interwork
SPECS   :=  -specs=gba.specs
CFLAGS  :=  -g -Wall -O2
-mcpu=arm7tdmi -mtune=arm7tdmi
-ffreestanding 
$(ARCH)
CFLAGS  :=  $(INCLUDE)
CXXFLAGS    :=  $(CFLAGS) -fno-rtti -fno-exceptions 
ASFLAGS :=  -g $(ARCH)
LDFLAGS =   -g $(ARCH) $(INCLUDE) -Wl,-Map,$(notdir $@.map)
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS    := -lmm -lgba

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS :=  $(LIBGBA)
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------

ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT   :=  $(CURDIR)/$(TARGET)
export VPATH    :=  $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) 
$(foreach dir,$(DATA),$(CURDIR)/$(dir)) 
$(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir))
export DEPSDIR  :=  $(CURDIR)/$(BUILD)
CFILES      :=  $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES    :=  $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES      :=  $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES    :=  $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
ifneq ($(strip $(MUSIC)),)
export AUDIOFILES   :=  $(foreach dir,$(notdir $(wildcard $(MUSIC)/*.*)),$(CURDIR)/$(MUSIC)/$(dir))
BINFILES += soundbank.bin
endif
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD   :=  $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD   :=  $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES)
export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))
export INCLUDE  :=  $(foreach dir,$(INCLUDES),-iquote $(CURDIR)/$(dir)) 
$(foreach dir,$(LIBDIRS),-I$(dir)/include) 
-I$(CURDIR)/$(BUILD)
export LIBPATHS :=  $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
.PHONY: $(BUILD) clean
#---------------------------------------------------------------------------------
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).elf $(TARGET).gba

#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).gba   :   $(OUTPUT).elf
$(OUTPUT).elf   :   $(OFILES)
$(OFILES_SOURCES) : $(HFILES)
#---------------------------------------------------------------------------------
# The bin2o rule should be copied and modified
# for each extension used in the data directories
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
# rule to build soundbank from music files
#---------------------------------------------------------------------------------
soundbank.bin soundbank.h : $(AUDIOFILES)
#---------------------------------------------------------------------------------
@mmutil $^ -osoundbank.bin -hsoundbank.h
#---------------------------------------------------------------------------------
# This rule links in binary data with the .bin extension
#---------------------------------------------------------------------------------
%.bin.o %_bin.h :   %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)

-include $(DEPSDIR)/*.d
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

对象转储的输出:

M:SrcGBAPaintBoyAdvancebuildpaintboyadvance.o:     file format elf32-littlearm
Sections:
Idx Name          Size      VMA       LMA       File off  Algn
0 .text         00000268  00000000  00000000  00000034  2**2
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data         00000000  00000000  00000000  0000029c  2**0
CONTENTS, ALLOC, LOAD, DATA
2 .bss          00000000  00000000  00000000  0000029c  2**0
ALLOC
3 .comment      00000024  00000000  00000000  0000029c  2**0
CONTENTS, READONLY
4 .ARM.attributes 0000002a  00000000  00000000  000002c0  2**0
CONTENTS, READONLY

编辑:根据要求,这里是批处理文件的输出:

SrcGBAPaintBoyAdvancecompile.bat paintboyadvance.c M:SrcGBAPaintBoyAdvancesource nopause
Process started >>>
paintboyadvance.c
linking cartridge
built ... PaintBoyAdvance.gba
ROM fixed!
M:SrcGBAPaintBoyAdvancebuildpaintboyadvance.o:     file format elf32-littlearm
Sections:
Idx Name          Size      VMA       LMA       File off  Algn
0 .text         00000280  00000000  00000000  00000034  2**2
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data         00000000  00000000  00000000  000002b4  2**0
CONTENTS, ALLOC, LOAD, DATA
2 .bss          00000000  00000000  00000000  000002b4  2**0
ALLOC
3 .comment      00000024  00000000  00000000  000002b4  2**0
CONTENTS, READONLY
4 .ARM.attributes 0000002a  00000000  00000000  000002d8  2**0
CONTENTS, READONLY

关键是使用链接器脚本。 我将只发布一个完整的工作示例。

创业公司

.cpu arm7tdmi
.code 32
rom_start:
b ram_start
.space 0xA0-0x04,0
.space 0xC0-0xA0,0
ram_start:
b _start
.space 0xE0-0xC4,0
_start:
ldr sp,=0x03008000
bl notmain
hang:
b hang
.globl PUT16
PUT16:
strh r1,[r0]
bx lr
.globl GET16
GET16:
ldrh r0,[r0]
bx lr
.globl PUT32
PUT32:
str r1,[r0]
bx lr
.globl GET32
GET32:
ldr r0,[r0]
bx lr

notmain.c

extern void PUT32 ( unsigned int, unsigned int );
extern unsigned int GET32 ( unsigned int );
extern void PUT16 ( unsigned int, unsigned int );
extern unsigned int GET16 ( unsigned int );
#define DISPCNT 0x04000000
#define BG0CNT  0x04000008
#define PMEM 0x05000000
#define TMEM 0x06000000
#define VMEM 0x06008000
//#define TWIDE 32
//#define THIGH 20
//#define TVISIBLE 30
void notmain ( void )
{
unsigned int ra;
unsigned int rb;
unsigned int rc;
//display control,
//mode 0
//enable BG0
PUT16(DISPCNT,0x0100);
//BG0 control
//256 color palette
//tiles defined at 0x60000000
//screen at 0x60008000
PUT16( BG0CNT,0x1080);
//setup the first 8 colors
PUT16(PMEM+0x0,0x0000); //BLACK
PUT16(PMEM+0x2,0x001F); //RED
PUT16(PMEM+0x4,0x03E0); //GREEN
PUT16(PMEM+0x6,0x03FF); //GREEN+RED
PUT16(PMEM+0x8,0x7C00); //BLUE
PUT16(PMEM+0xA,0x7C1F); //BLUE+RED
PUT16(PMEM+0xC,0x7FE0); //BLUE+GREEN
PUT16(PMEM+0xE,0x7FFF); //BLUE+GREEN+RED (WHITE)
// Let's make a few tiles 64 bytes per tile.
ra=TMEM;
PUT32(ra,0x00000000); ra+=4; PUT32(ra,0x00000000); ra+=4;
PUT32(ra,0x00000000); ra+=4; PUT32(ra,0x00000000); ra+=4;
PUT32(ra,0x00000000); ra+=4; PUT32(ra,0x00000000); ra+=4;
PUT32(ra,0x00000000); ra+=4; PUT32(ra,0x00000000); ra+=4;
PUT32(ra,0x00000000); ra+=4; PUT32(ra,0x00000000); ra+=4;
PUT32(ra,0x00000000); ra+=4; PUT32(ra,0x00000000); ra+=4;
PUT32(ra,0x00000000); ra+=4; PUT32(ra,0x00000000); ra+=4;
PUT32(ra,0x00000000); ra+=4; PUT32(ra,0x00000000); ra+=4;
PUT32(ra,0x01010000); ra+=4; PUT32(ra,0x01010000); ra+=4;
PUT32(ra,0x01010000); ra+=4; PUT32(ra,0x01010000); ra+=4;
PUT32(ra,0x01010000); ra+=4; PUT32(ra,0x01010000); ra+=4;
PUT32(ra,0x01010000); ra+=4; PUT32(ra,0x01010000); ra+=4;
PUT32(ra,0x00010001); ra+=4; PUT32(ra,0x00010001); ra+=4;
PUT32(ra,0x01000100); ra+=4; PUT32(ra,0x01000100); ra+=4;
PUT32(ra,0x00010001); ra+=4; PUT32(ra,0x00010001); ra+=4;
PUT32(ra,0x01000100); ra+=4; PUT32(ra,0x01000100); ra+=4;
PUT32(ra,0x02020000); ra+=4; PUT32(ra,0x02020000); ra+=4;
PUT32(ra,0x02020000); ra+=4; PUT32(ra,0x02020000); ra+=4;
PUT32(ra,0x02020000); ra+=4; PUT32(ra,0x02020000); ra+=4;
PUT32(ra,0x02020000); ra+=4; PUT32(ra,0x02020000); ra+=4;
PUT32(ra,0x00020002); ra+=4; PUT32(ra,0x00020002); ra+=4;
PUT32(ra,0x02000200); ra+=4; PUT32(ra,0x02000200); ra+=4;
PUT32(ra,0x00020002); ra+=4; PUT32(ra,0x00020002); ra+=4;
PUT32(ra,0x02000200); ra+=4; PUT32(ra,0x02000200); ra+=4;
PUT32(ra,0x03030000); ra+=4; PUT32(ra,0x03030000); ra+=4;
PUT32(ra,0x03030000); ra+=4; PUT32(ra,0x03030000); ra+=4;
PUT32(ra,0x03030000); ra+=4; PUT32(ra,0x03030000); ra+=4;
PUT32(ra,0x03030000); ra+=4; PUT32(ra,0x03030000); ra+=4;
PUT32(ra,0x00030003); ra+=4; PUT32(ra,0x00030003); ra+=4;
PUT32(ra,0x03000300); ra+=4; PUT32(ra,0x03000300); ra+=4;
PUT32(ra,0x00030003); ra+=4; PUT32(ra,0x00030003); ra+=4;
PUT32(ra,0x03000300); ra+=4; PUT32(ra,0x03000300); ra+=4;
PUT32(ra,0x04040000); ra+=4; PUT32(ra,0x04040000); ra+=4;
PUT32(ra,0x04040000); ra+=4; PUT32(ra,0x04040000); ra+=4;
PUT32(ra,0x04040000); ra+=4; PUT32(ra,0x04040000); ra+=4;
PUT32(ra,0x04040000); ra+=4; PUT32(ra,0x04040000); ra+=4;
PUT32(ra,0x00040004); ra+=4; PUT32(ra,0x00040004); ra+=4;
PUT32(ra,0x04000400); ra+=4; PUT32(ra,0x04000400); ra+=4;
PUT32(ra,0x00040004); ra+=4; PUT32(ra,0x00040004); ra+=4;
PUT32(ra,0x04000400); ra+=4; PUT32(ra,0x04000400); ra+=4;
PUT32(ra,0x05050000); ra+=4; PUT32(ra,0x05050000); ra+=4;
PUT32(ra,0x05050000); ra+=4; PUT32(ra,0x05050000); ra+=4;
PUT32(ra,0x05050000); ra+=4; PUT32(ra,0x05050000); ra+=4;
PUT32(ra,0x05050000); ra+=4; PUT32(ra,0x05050000); ra+=4;
PUT32(ra,0x00050005); ra+=4; PUT32(ra,0x00050005); ra+=4;
PUT32(ra,0x05000500); ra+=4; PUT32(ra,0x05000500); ra+=4;
PUT32(ra,0x00050005); ra+=4; PUT32(ra,0x00050005); ra+=4;
PUT32(ra,0x05000500); ra+=4; PUT32(ra,0x05000500); ra+=4;
PUT32(ra,0x06060000); ra+=4; PUT32(ra,0x06060000); ra+=4;
PUT32(ra,0x06060000); ra+=4; PUT32(ra,0x06060000); ra+=4;
PUT32(ra,0x06060000); ra+=4; PUT32(ra,0x06060000); ra+=4;
PUT32(ra,0x06060000); ra+=4; PUT32(ra,0x06060000); ra+=4;
PUT32(ra,0x00060006); ra+=4; PUT32(ra,0x00060006); ra+=4;
PUT32(ra,0x06000600); ra+=4; PUT32(ra,0x06000600); ra+=4;
PUT32(ra,0x00060006); ra+=4; PUT32(ra,0x00060006); ra+=4;
PUT32(ra,0x06000600); ra+=4; PUT32(ra,0x06000600); ra+=4;
PUT32(ra,0x07070000); ra+=4; PUT32(ra,0x07070000); ra+=4;
PUT32(ra,0x07070000); ra+=4; PUT32(ra,0x07070000); ra+=4;
PUT32(ra,0x07070000); ra+=4; PUT32(ra,0x07070000); ra+=4;
PUT32(ra,0x07070000); ra+=4; PUT32(ra,0x07070000); ra+=4;
PUT32(ra,0x00070007); ra+=4; PUT32(ra,0x00070007); ra+=4;
PUT32(ra,0x07000700); ra+=4; PUT32(ra,0x07000700); ra+=4;
PUT32(ra,0x00070007); ra+=4; PUT32(ra,0x00070007); ra+=4;
PUT32(ra,0x07000700); ra+=4; PUT32(ra,0x07000700); ra+=4;
//make screen black/clear screen
ra=VMEM;
for(rb=0;rb<(32*20);rb++)
{
PUT16(ra,0x0000);
ra+=2;
}
//put some tiles on the screen
ra=VMEM;
for(rc=0,rb=0;rb<(32*10);rb++,rc++)
{
rc&=7;
if(rc==0) rc=1;
PUT16(ra,rc);
ra+=2;
}
}

内存映射

MEMORY
{
ewram : ORIGIN = 0x02000000, LENGTH = 256K
}
SECTIONS
{
.text : { *(.text*) } > ewram
}

罗马地图

MEMORY
{
rom  : ORIGIN = 0x08000000, LENGTH = 32K
}
SECTIONS
{
.text : { *(.text*) } > rom
}

arm-none-eabi-as --warn --fatal-warnings  startup.s -o startup.o
arm-none-eabi-gcc -c -mcpu=arm7tdmi -Wall -Werror -O2 -ffreestanding  notmain.c -o notmain.o
arm-none-eabi-ld -T rommap startup.o notmain.o -o gbarom.elf
arm-none-eabi-objdump -D gbarom.elf > gbarom.list
arm-none-eabi-objcopy gbarom.elf -O binary notmain.gba
arm-none-eabi-ld -T memmap startup.o notmain.o -o notmain.elf
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy notmain.elf -O binary notmain.mb

现在 vba 允许您运行多重引导文件以及 gba 文件

vba notmain.mb
vba motmain.gba

一个使用EWRAM,另一个使用GAME ROM。

您不必在链接器脚本中同时使用 MEMORY 和 SECTION,可以在部分中对地址进行硬编码,但我建议内存也在那里。 我建议不要使用(rwx)的东西,YMMV。

MEMORY
{
rom  : ORIGIN = 0x08000000, LENGTH = 32K
}
SECTIONS
{
.text : { *(.text*) } > rom
}

rom 这个名字并不重要——你可以叫它泡菜,它会没事的。它只是用于将内存范围定义连接到一个或多个部分的名称。 里面的.text*是对象中以.text开头的所有内容(.text是指令)。外面的 *() 表示通过链接器馈送的所有对象,您可以为每个条目指定特定的对象以进行更精细的控制(请注意链接器脚本语言并不像您期望的那样好和完美,有时调用对象不会按照您想象的方式工作)。

然后前面的 .text 是这样输出二进制文件使用该名称,您可以调用该 .baseball,它可以正常工作,只是您提供该文件的工具不知道该怎么做,所以只使用 .text 是个好主意。

你添加更多这样的东西

MEMORY
{
ewram : ORIGIN = 0x02000000, LENGTH = 256K
}
SECTIONS
{
.text : { *(.text*) } > ewram
.rodata : { *(.rodata*) } > ewram
.bss : { *(.bss*) } > ewram
.data : { *(.data*) } > ewram
}

它将按顺序处理这些内容,因为 .text 内容首先进入输出,然后是 .rodata,然后是 .bss,最后是 .data。另请注意,由于此处没有调用任何文件或对象,因此命令行也驱动事物的顺序。

arm-none-eabi-ld -T memmap startup.o notmain.o -o notmain.elf

我们需要入口点代码排在第一位,以便文件在命令行上排在第一位(比用编造的部分名称等使其复杂化更容易)。

请注意,在构建后和尝试将某些内容提交到 flash 之前

看拆解:

Disassembly of section .text:
02000000 <rom_start>:
2000000:   ea00002e    b   20000c0 <ram_start>
...
020000c0 <ram_start>:
20000c0:   ea000006    b   20000e0 <_start>
...
020000e0 <_start>:
20000e0:   e59fd024    ldr sp, [pc, #36]   ; 200010c <GET32+0x8>
20000e4:   eb000009    bl  2000110 <notmain>
020000e8 <hang>:
20000e8:   eafffffe    b   20000e8 <hang>

如果在命令行上交换文件

Disassembly of section .text:
02000000 <notmain>:
2000000:   e92d4070    push    {r4, r5, r6, lr}
2000004:   e3a01c01    mov r1, #256    ; 0x100
2000008:   e3a00301    mov r0, #67108864   ; 0x4000000
200000c:   eb00028f    bl  2000a50 <PUT16>
2000010:   e3a01d42    mov r1, #4224   ; 0x1080
2000014:   e59f06bc    ldr r0, [pc, #1724] ; 20006d8 <notmai

游戏结束了,那只会崩溃。 当然不会像你希望/期望的那样运行(有时你可能会很幸运,但总的来说这是一个失败)。

我在引导程序中为徽标数据留出了空间,以便让它作为 mgba 的真正 rom 或带有正确墨盒的真正 gba 工作(有些有菜单,您不必有正确的墨盒头)。 在多重引导级别,您不需要将所有空间都放在那里,只需从引导程序开始即可。

你可以使用-Ttext=0x08000000作为一个快速而肮脏的黑客,但使用 gnu ld 仍然使用内置于你正在使用的工具链中的默认链接器脚本,你只是在调整该部分类型。 如果你用更复杂的东西来推动它,你就会开始陷入奇怪的事情。 因此,您可以看到使用链接器脚本是多么微不足道,它们不必很复杂。 所以很多人倾向于过于复杂,老实说,GNU 链接器脚本语言可能会很痛苦,因为它并不总是以理智和一致的方式工作。 我更喜欢在其他地方而不是在链接器脚本中完成大部分工作。

最新更新