gpt4 book ai didi

c - (Game Boy Advance) 程序从 0x00000000 而不是 0x08000000 开始。如何告诉 gcc 编译到特定地址?

转载 作者:行者123 更新时间:2023-12-05 05:53:29 28 4
gpt4 key购买 nike

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

我对 makefile、编译器、链接器等的概念还很陌生,所以我可能没有把所有东西都设置好。我将展示我的 makefile、运行 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:\SrcGBA\PaintBoyAdvance\include\bitmap.h"
#include "M:\SrcGBA\PaintBoyAdvance\include\bitmap.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:\SrcGBA\PaintBoyAdvance
make
if not "%errorlevel%"=="0" goto Abandon

C:\devkitPro\devkitARM\bin\arm-none-eabi-objdump -h M:\SrcGBA\PaintBoyAdvance\build\paintboyadvance.o

C:\Users\puppy\Documents\VisualBoyAdvance\visualboyadvance-m.exe M:\SrcGBA\PaintBoyAdvance\PaintBoyAdvance.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:\SrcGBA\PaintBoyAdvance\build\paintboyadvance.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

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

\SrcGBA\PaintBoyAdvance\compile.bat paintboyadvance.c M:\SrcGBA\PaintBoyAdvance\source nopause
Process started >>>
paintboyadvance.c
linking cartridge
built ... PaintBoyAdvance.gba
ROM fixed!

M:\SrcGBA\PaintBoyAdvance\build\paintboyadvance.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

最佳答案

关键是使用链接描述文件。我将只发布一个完整的工作示例。

启动.s

.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
}

罗马 map

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 允许你运行 multiboot 文件和 gba 文件

vba notmain.mb

vba motmain.gba

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

您不必在链接描述文件中同时使用 MEMORY 和 SECTIONS,可以在节中硬编码地址,但我建议内存也在那里。我建议不要使用 (rwx) 东西,YMMV。

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

名字 rom 并不重要——你可以叫它 pickle 就可以了。它只是一个名称,用于将内存范围定义连接到一个或多个部分。 .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

游戏结束了,那将会崩溃。肯定不会像您希望/期望的那样运行(有时您可能会很幸运,但总的来说这是失败的)。

我在 Bootstrap 中为 Logo 数据留出了空间,这样可以让它作为 mgba 的真正 rom 或带有正确墨盒的真正 gba 工作(有些有菜单,你不需要正确的墨盒头) .在多重引导级别,您不需要拥有所有空间,只需从 Bootstrap 开始即可。

你可以通过 -Ttext=0x08000000 作为一个快速而肮脏的 hack,但是 gnu ld 仍然使用你正在使用的工具链中内置的默认链接器脚本,你是只需调整该部分类型。如果你用更复杂的东西来插入它,你就会开始陷入奇怪的境地。所以你可以看到使用链接描述文件是多么简单,它们不必很复杂。许多人倾向于过度复杂化,老实说,gnu 链接器脚本语言可能会很痛苦,因为它并不总是以一种理智和一致的方式工作。我更喜欢在其他地方而不是在链接描述文件中完成大部分工作。

关于c - (Game Boy Advance) 程序从 0x00000000 而不是 0x08000000 开始。如何告诉 gcc 编译到特定地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69875381/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com