diff options
author | Bad Diode <bd@badd10de.dev> | 2022-10-19 10:00:42 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2022-10-19 10:00:42 +0200 |
commit | 0702260e53a18dacfae58582da8a4d518c3c41ee (patch) | |
tree | 51e35f761f6e23fd4f9ce54cfa3d119e2e4bdc64 | |
parent | 219029f317e20a5248735d7f7bf6bb88ecb95d28 (diff) | |
download | uxn64-0702260e53a18dacfae58582da8a4d518c3c41ee.tar.gz uxn64-0702260e53a18dacfae58582da8a4d518c3c41ee.zip |
Replace `spicy` with linker scripts
-rw-r--r-- | Makefile | 29 | ||||
-rw-r--r-- | src/entry.s | 16 | ||||
-rw-r--r-- | src/linker.ld | 50 | ||||
-rw-r--r-- | src/main.c | 13 |
4 files changed, 86 insertions, 22 deletions
@@ -12,11 +12,15 @@ LIBULTRA := $(LIBULTRA_DIR)/usr/lib/libgultra.a | |||
12 | # Source code location and files to watch for changes. | 12 | # Source code location and files to watch for changes. |
13 | SRC_DIR := src | 13 | SRC_DIR := src |
14 | BUILD_DIR := build | 14 | BUILD_DIR := build |
15 | SRC_MAIN := $(SRC_DIR)/main.c | 15 | SRC_MAIN := $(SRC_DIR)/main.c $(SRC_DIR)/entry.s |
16 | SRC_LN := $(SRC_DIR)/linker.ld | ||
16 | SRC_OBJ := | 17 | SRC_OBJ := |
17 | OBJECTS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC_OBJ)) | 18 | OBJECTS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC_OBJ)) |
18 | 19 | WATCH_SRC := $(shell find $(SRC_DIR) \ | |
19 | WATCH_SRC := $(shell find $(SRC_DIR) -name "*.c" -or -name "*.s" -or -name "*.h") | 20 | -name "*.c" -or \ |
21 | -name "*.s" -or \ | ||
22 | -name "*.h" -or \ | ||
23 | -name "**.ld") | ||
20 | INC_DIRS := $(shell find $(SRC_DIR) -type d) | 24 | INC_DIRS := $(shell find $(SRC_DIR) -type d) |
21 | INC_DIRS += $(LIBULTRA_INC) | 25 | INC_DIRS += $(LIBULTRA_INC) |
22 | INC_FLAGS := $(addprefix -I,$(INC_DIRS)) | 26 | INC_FLAGS := $(addprefix -I,$(INC_DIRS)) |
@@ -31,8 +35,8 @@ CC := $(SDK_BIN)/mips32-elf-gcc | |||
31 | LD := $(SDK_BIN)/mips32-elf-ld | 35 | LD := $(SDK_BIN)/mips32-elf-ld |
32 | AS := $(SDK_BIN)/mips32-elf-as | 36 | AS := $(SDK_BIN)/mips32-elf-as |
33 | OBJDUMP := $(SDK_BIN)/mips32-elf-objdump | 37 | OBJDUMP := $(SDK_BIN)/mips32-elf-objdump |
38 | OBJCOPY := $(SDK_BIN)/mips32-elf-objcopy | ||
34 | # TODO: Replace with internal tools to avoid dependencies. | 39 | # TODO: Replace with internal tools to avoid dependencies. |
35 | SPICY := $(SDK_BIN)/spicy | ||
36 | MAKEMASK := $(SDK_BIN)/makemask | 40 | MAKEMASK := $(SDK_BIN)/makemask |
37 | 41 | ||
38 | # Compiler and linker configuration. | 42 | # Compiler and linker configuration. |
@@ -40,8 +44,8 @@ CFLAGS := -Wall -Wextra -pedantic | |||
40 | CFLAGS += -mabi=32 -mfix4300 | 44 | CFLAGS += -mabi=32 -mfix4300 |
41 | CFLAGS += -ffreestanding -G 0 | 45 | CFLAGS += -ffreestanding -G 0 |
42 | CFLAGS += $(INC_FLAGS) | 46 | CFLAGS += $(INC_FLAGS) |
43 | CFLAGS += -DF3DEX_GBI_2 -nostdlib -r | 47 | CFLAGS += -DF3DEX_GBI_2 |
44 | LDFLAGS := -nostdlib -r | 48 | LDFLAGS := -nostdlib |
45 | LDLIBS := $(LIBULTRA) $(SDK_BASE)/lib/gcc/mips32-elf/13.0.0/libgcc.a | 49 | LDLIBS := $(LIBULTRA) $(SDK_BASE)/lib/gcc/mips32-elf/13.0.0/libgcc.a |
46 | RELEASE_CFLAGS := -O2 -DNDEBUG -D_FINALROM | 50 | RELEASE_CFLAGS := -O2 -DNDEBUG -D_FINALROM |
47 | DEBUG_CFLAGS := -O0 -DDEBUG -D_FINALROM | 51 | DEBUG_CFLAGS := -O0 -DDEBUG -D_FINALROM |
@@ -59,15 +63,10 @@ endif | |||
59 | main: $(BIN) | 63 | main: $(BIN) |
60 | 64 | ||
61 | $(ELF): $(SRC_MAIN) $(WATCH_SRC) | $(BUILD_DIR) | 65 | $(ELF): $(SRC_MAIN) $(WATCH_SRC) | $(BUILD_DIR) |
62 | $(CC) $(CFLAGS) $(LDFLAGS) -o $(ELF) $(SRC_MAIN) $(LDLIBS) | 66 | $(CC) $(CFLAGS) $(LDFLAGS) -o $(ELF) -T $(SRC_LN) $(SRC_MAIN) $(LDLIBS) |
63 | 67 | ||
64 | $(BIN): $(ELF) $(OBJECTS) $(WATCH_SRC) | $(BUILD_DIR) | 68 | $(BIN): $(ELF) $(OBJECTS) $(WATCH_SRC) | $(BUILD_DIR) |
65 | $(SPICY) -r $@ $(SRC_DIR)/spec \ | 69 | $(OBJCOPY) -O binary $< $@ |
66 | --as_command="$(SDK_BIN)/mips32-elf-as" \ | ||
67 | --cpp_command="$(SDK_BIN)/mips32-elf-gcc" \ | ||
68 | --ld_command="$(SDK_BIN)/mips32-elf-ld" \ | ||
69 | --objcopy_command="$(SDK_BIN)/mips32-elf-objcopy" | ||
70 | rm a.out | ||
71 | $(MAKEMASK) $(BIN) | 70 | $(MAKEMASK) $(BIN) |
72 | 71 | ||
73 | # Test the output .n64 in an emulator. | 72 | # Test the output .n64 in an emulator. |
@@ -86,3 +85,7 @@ $(BUILD_DIR): | |||
86 | # Inference rules for C files. | 85 | # Inference rules for C files. |
87 | $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR) | 86 | $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR) |
88 | $(CC) $(CFLAGS) $< -o $@ | 87 | $(CC) $(CFLAGS) $< -o $@ |
88 | |||
89 | # Inference rules for assembly files. | ||
90 | $(BUILD_DIR)/%.o: $(SRC_DIR)/%.s | $(BUILD_DIR) | ||
91 | $(AS) $(AFLAGS) -o $@ -c $< | ||
diff --git a/src/entry.s b/src/entry.s new file mode 100644 index 0000000..3ff60ba --- /dev/null +++ b/src/entry.s | |||
@@ -0,0 +1,16 @@ | |||
1 | # Entry point for the ROM image. | ||
2 | .section .text.entry | ||
3 | .global _start | ||
4 | _start: | ||
5 | |||
6 | # Set up the stack | ||
7 | la $sp, _boot_stack | ||
8 | |||
9 | # Zero BSS. | ||
10 | la $a0, _bss_start | ||
11 | la $a1, _bss_end | ||
12 | subu $a1, $a1, $a0 | ||
13 | jal bzero | ||
14 | |||
15 | # Jump to C entry point. | ||
16 | j boot | ||
diff --git a/src/linker.ld b/src/linker.ld new file mode 100644 index 0000000..91bd28a --- /dev/null +++ b/src/linker.ld | |||
@@ -0,0 +1,50 @@ | |||
1 | ENTRY(_start) | ||
2 | |||
3 | STACK_SIZE = 8K; | ||
4 | |||
5 | MEMORY { | ||
6 | rom (R) : ORIGIN = 0, LENGTH = 64M | ||
7 | ram (RWX) : ORIGIN = 0x80000400, LENGTH = 4M - 0x400 | ||
8 | } | ||
9 | |||
10 | SECTIONS { | ||
11 | .header : { | ||
12 | LONG(0x80371240) | ||
13 | LONG(0x0000000f) | ||
14 | LONG(0x80000400) | ||
15 | LONG(0x0000144c) | ||
16 | . = 0x1000; | ||
17 | } >rom | ||
18 | |||
19 | .text : { | ||
20 | _text_start = .; | ||
21 | *(.text.entry) | ||
22 | *(.text .text.*) | ||
23 | *(.rodata .rodata.*) | ||
24 | *(.data .data.*) | ||
25 | _text_end = .; | ||
26 | } >ram AT>rom | ||
27 | |||
28 | .bss (NOLOAD) : ALIGN(16) { | ||
29 | _bss_start = .; | ||
30 | *(.bss .bss.*) | ||
31 | *(COMMON) | ||
32 | *(.scommon .scommon.*) | ||
33 | _bss_end = .; | ||
34 | } >ram | ||
35 | |||
36 | .stack (NOLOAD) : ALIGN(16) { | ||
37 | . += STACK_SIZE; | ||
38 | . = ALIGN(8); | ||
39 | _boot_stack = .; | ||
40 | _main_thread_stack = .; | ||
41 | |||
42 | . += STACK_SIZE; | ||
43 | . = ALIGN(8); | ||
44 | _idle_thread_stack = .; | ||
45 | } >ram | ||
46 | |||
47 | /DISCARD/ : { | ||
48 | *(*) | ||
49 | } | ||
50 | } | ||
@@ -2,19 +2,14 @@ | |||
2 | 2 | ||
3 | #include "types.h" | 3 | #include "types.h" |
4 | 4 | ||
5 | // Get a pointer to the start (top) of a stack. | ||
6 | #define STACK_START(stack) ((stack) + sizeof((stack))) | ||
7 | |||
8 | // | 5 | // |
9 | // Threads and stacks. | 6 | // Threads and stacks. |
10 | // | 7 | // |
11 | 8 | ||
12 | #define STACK_SIZE 8 * 1024 | ||
13 | static OSThread idle_thread; | 9 | static OSThread idle_thread; |
14 | static OSThread main_thread; | 10 | static OSThread main_thread; |
15 | static u8 idle_thread_stack[STACK_SIZE] __attribute__((aligned(8))); | 11 | extern u8 _idle_thread_stack[]; |
16 | static u8 main_thread_stack[STACK_SIZE] __attribute__((aligned(8))); | 12 | extern u8 _main_thread_stack[]; |
17 | u64 boot_stack[STACK_SIZE / sizeof(u64)] __attribute__((aligned(8))); | ||
18 | 13 | ||
19 | // | 14 | // |
20 | // Message buffers and queues. | 15 | // Message buffers and queues. |
@@ -268,7 +263,7 @@ idle_proc(void *arg) { | |||
268 | osCreatePiManager((OSPri)OS_PRIORITY_PIMGR, &pi_msg_queue, pi_msg, NUM_PI_MSGS); | 263 | osCreatePiManager((OSPri)OS_PRIORITY_PIMGR, &pi_msg_queue, pi_msg, NUM_PI_MSGS); |
269 | 264 | ||
270 | // Create main thread. | 265 | // Create main thread. |
271 | osCreateThread(&main_thread, 3, main_proc, NULL, STACK_START(main_thread_stack), 10); | 266 | osCreateThread(&main_thread, 3, main_proc, NULL, _main_thread_stack, 10); |
272 | osStartThread(&main_thread); | 267 | osStartThread(&main_thread); |
273 | 268 | ||
274 | // Become the idle thread. | 269 | // Become the idle thread. |
@@ -281,6 +276,6 @@ void | |||
281 | boot(void) { | 276 | boot(void) { |
282 | osInitialize(); | 277 | osInitialize(); |
283 | rom_handle = osCartRomInit(); | 278 | rom_handle = osCartRomInit(); |
284 | osCreateThread(&idle_thread, 1, idle_proc, NULL, STACK_START(idle_thread_stack), 10); | 279 | osCreateThread(&idle_thread, 1, idle_proc, NULL, _idle_thread_stack, 10); |
285 | osStartThread(&idle_thread); | 280 | osStartThread(&idle_thread); |
286 | } | 281 | } |