From d7e07280d0406883e01d1c8a7836b44a0895def6 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Thu, 2 Nov 2023 03:57:19 -0400 Subject: [PATCH] ps2rom: Initial commit This is the first version of the code which works. --- .gitignore | 2 ++ Makefile | 14 ++++++++++ build/core.mk | 36 ++++++++++++++++++++++++ build/ee-bin.mk | 12 ++++++++ build/ee-common.mk | 36 ++++++++++++++++++++++++ build/ee-lib.mk | 12 ++++++++ rom/Makefile | 44 ++++++++++++++++++++++++++++++ rom/ee_start.c | 24 ++++++++++++++++ rom/link.ld | 68 ++++++++++++++++++++++++++++++++++++++++++++++ rom/start.s | 16 +++++++++++ 10 files changed, 264 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 build/core.mk create mode 100644 build/ee-bin.mk create mode 100644 build/ee-common.mk create mode 100644 build/ee-lib.mk create mode 100644 rom/Makefile create mode 100644 rom/ee_start.c create mode 100644 rom/link.ld create mode 100644 rom/start.s diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2c5afb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +**/obj diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..42bc0f8 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +.PHONY: all clean + +TOP=$(shell pwd) + +# Set a default configuration. +ifeq ($(CONFIG),) +CONFIG=release +endif + +all: + make -C rom TOP=$(TOP) CONFIG=$(CONFIG) + +clean: + make -C rom TOP=$(TOP) CONFIG=$(CONFIG) clean diff --git a/build/core.mk b/build/core.mk new file mode 100644 index 0000000..04bad6a --- /dev/null +++ b/build/core.mk @@ -0,0 +1,36 @@ +# Common GNU Make rules for building the project + +# This makefile is expected to be included after the variables given +# have been defined: +# +# NAME=name +# +# TARGET=ee|iop +# +# KIND=bin|lib +# +# SRCS=[... source-file-list] +# + +debug_Valid := y +release_Valid := y + +ifneq ($($(CONFIG)_Valid),y) +$(error Invalid configuration $(CONFIG) specified) +endif + +BINDIR = $(TOP)/bin +OBJDIR = obj/$(CONFIG) + + +# include target-specific recipies specifying how to +# build for the target. +include $(TOP)/build/$(TARGET)-common.mk +#include $(TOP)/build/$(TARGET)-$(KIND).mk + +FINALNAME=$(NAME)_$(CONFIG) + +OBJECTS := $(patsubst %.c,obj/$(CONFIG)/%.o,$(filter %.c,$(SRCS))) +OBJECTS += $(patsubst %.cpp,obj/$(CONFIG)/%.o,$(filter %.cpp,$(SRCS))) +OBJECTS += $(patsubst %.s,obj/$(CONFIG)/%.o,$(filter %.s,$(SRCS))) + diff --git a/build/ee-bin.mk b/build/ee-bin.mk new file mode 100644 index 0000000..a63e298 --- /dev/null +++ b/build/ee-bin.mk @@ -0,0 +1,12 @@ +FINALNAME=$(NAME)_$(CONFIG) + +OBJECTS := $(patsubst %.c,obj/$(CONFIG)/%.o,$(filter %.c,$(SRCS))) +OBJECTS += $(patsubst %.cpp,obj/$(CONFIG)/%.o,$(filter %.cpp,$(SRCS))) +OBJECTS += $(patsubst %.s,obj/$(CONFIG)/%.o,$(filter %.s,$(SRCS))) + +$(BINDIR)/$(FINALNAME).elf: $(OBJDIR)/ $(BINDIR)/ $(OBJECTS) + $(LD) $(LDFLAGS) $(LIBS) $(OBJECTS) -o $@ + +clean-products: + -rm $(BINDIR)/$(FINALNAME).elf + -rm -rf obj diff --git a/build/ee-common.mk b/build/ee-common.mk new file mode 100644 index 0000000..4f04fef --- /dev/null +++ b/build/ee-common.mk @@ -0,0 +1,36 @@ +# common EE toolchain stuff + +# EE toolchain root. +EE_TRIPLET=mips64r5900el-ps2-elf + +CC=$(EE_TRIPLET)-gcc +AS=$(CC) -mabi=32 -march=r3000 #HACK +CXX=$(EE_TRIPLET)-g++ +#LD=$(CC) +LD=mipsel-ps2-irx-gcc # HACK +AR=$(EE_TRIPLET)-ar +OBJCOPY=$(EE_TRIPLET)-objcopy + +ifeq ($(CONFIG),release) +CFLAGS := $(CFLAGS) -G0 -O3 -Wall -Wextra -Werror -fno-common -fno-strict-aliasing +ASFLAGS := -xassembler-with-cpp +endif + +ifeq ($(CONFIG),debug) +CFLAGS := $(CFLAGS) -G0 -O0 -g3 -Wall -Wextra -fno-common -fno-strict-aliasing +ASFLAGS := -xassembler-with-cpp -g3 +endif + +CXXFLAGS := $(CXXFLAGS) $(CFLAGS) + +# compile rules +$(OBJDIR)/%.o: %.c + $(CC) -c $(CFLAGS) $< -o $@ + +$(OBJDIR)/%.o: %.cpp + $(CXX) -c $(CXXFLAGS) $< -o $@ + +$(OBJDIR)/%.o: %.s + $(AS) -c $(ASFLAGS) $< -o $@ + + diff --git a/build/ee-lib.mk b/build/ee-lib.mk new file mode 100644 index 0000000..e58e13e --- /dev/null +++ b/build/ee-lib.mk @@ -0,0 +1,12 @@ +FINALNAME=$(NAME)_$(CONFIG) + +OBJECTS := $(patsubst %.c,obj/$(CONFIG)/%.o,$(filter %.c,$(SRCS))) +OBJECTS += $(patsubst %.cpp,obj/$(CONFIG)/%.o,$(filter %.cpp,$(SRCS))) +OBJECTS += $(patsubst %.s,obj/$(CONFIG)/%.o,$(filter %.s,$(SRCS))) + +clean-products: + -rm $(BINDIR)/lib$(FINALNAME).a + -rm -rf obj + +$(BINDIR)/lib$(FINALNAME).a: $(OBJDIR)/ $(BINDIR)/ $(OBJECTS) + $(AR) rv $@ $(OBJECTS) diff --git a/rom/Makefile b/rom/Makefile new file mode 100644 index 0000000..ecc5ebd --- /dev/null +++ b/rom/Makefile @@ -0,0 +1,44 @@ +NAME=ps2rom +TARGET=ee +KIND=bin + +SELF=$(shell pwd) +INCS = -I$(TOP)/src + +CFLAGS = -ffreestanding -fno-stack-protector -mabi=32 +CXXFLAGS = -fno-exceptions -fno-rtti +LDFLAGS = -nostartfiles -nodefaultlibs -Wl,-Map,$(BINDIR)/$(FINALNAME).map -T$(SELF)/link.ld + +# sources +SRCS = start.s \ + ee_start.c + + +.PHONY: all clean clean-products +include $(TOP)/build/core.mk + + +all: $(BINDIR)/$(FINALNAME).rom + + +$(BINDIR)/: + mkdir -p $@ + +$(OBJDIR)/: + mkdir -p $@ + + +clean-products: + -rm $(BINDIR)/$(FINALNAME).elf + -rm -rf obj + +clean: clean-products + -rm $(BINDIR)/$(FINALNAME).rom + + +$(BINDIR)/$(FINALNAME).rom: $(BINDIR)/$(FINALNAME).elf + $(OBJCOPY) -O binary $< $@ + +$(BINDIR)/$(FINALNAME).elf: $(OBJDIR)/ $(BINDIR)/ $(OBJECTS) + $(LD) $(LDFLAGS) $(LIBS) $(OBJECTS) -o $@ + diff --git a/rom/ee_start.c b/rom/ee_start.c new file mode 100644 index 0000000..7485070 --- /dev/null +++ b/rom/ee_start.c @@ -0,0 +1,24 @@ + +#define __noreturn __attribute__((noreturn)) + +static void putc(char c) { + *((char*)0x1000f180) = c; +} + +static void puts(const char* s) { + while (*s != 0) { + putc(*s++); + } +} + + +__noreturn void _ee_start() { + puts("Hello World from Riri~ (EE)\n"); + while(1); +} + +__asm__ ( + ".global ee_start\n" + "ee_start:\n" + "li $sp, 0x80010000\n" + "j _ee_start\n"); diff --git a/rom/link.ld b/rom/link.ld new file mode 100644 index 0000000..b75c158 --- /dev/null +++ b/rom/link.ld @@ -0,0 +1,68 @@ +_stack_size = 0x80000; +_heap_size = 1024*1024*10; + + +ENTRY(_start); +SECTIONS { +/* 0xbfc00000 */ + .text 0xbfc00000 : { + _text_origin = .; + *start.o /* Always link reset first */ + + *(.text) + *(.rodata) + } + _text_size = . - _text_origin; + + .reginfo ALIGN(128) : { + *(.reginfo) + } + + .data ALIGN(128) : { + _data_origin = .; + *(.data) + } + + .rdata ALIGN(128) : { + *(.rdata) + } + + _data_size = . - _data_origin; + + _gp = ALIGN(128) + 0x7ff0; + .lit4 ALIGN(128) : { + *(.lit4) + } + .lit8 ALIGN(128) : { + *(.lit8) + } + .sdata ALIGN(128) : { + *(.sdata) + } + + .sbss ALIGN(128) (NOLOAD) : { /* uninitialized data */ + _fbss = . ; + *(.scommon) + *(.sbss) + } + + .bss ALIGN(128) (NOLOAD) : { /* uninitialized data */ + *(.bss) + } + + .COMMON ALIGN(128) (NOLOAD) : { /* uninitialized data */ + *(COMMON) + } + + _bss_size = . - _fbss; + + _end_bss = . - 4; + _stack = .; + . += _stack_size ; + _end_stack = . - 8*5; + _end = . ; + __lc_bh = . ; + . += _heap_size ; + __lc_eh = .; + +} diff --git a/rom/start.s b/rom/start.s new file mode 100644 index 0000000..23e4799 --- /dev/null +++ b/rom/start.s @@ -0,0 +1,16 @@ +.org 0 +.set noat + +// externs +.extern ee_start + +.globl _start +_start: + mfc0 $at, $15 // load PRID + sltiu $at, 0x59 + bne $15, 0, __iop + nop + j ee_start + nop +__iop: // for now iop stalls. + j __iop