From feb0648b9a92c737a5b8dba28decc050d58b8098 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Mon, 6 Nov 2023 01:56:09 -0500 Subject: [PATCH] *: clean up code The EE side of the code has been moved to a library at rom/ee. The rom/ tree now simply builds the rom entrypoint, and will link to both libraries, allowing them to be seperate and manages. The buildsystem has now been greatly refactored, and IOP support kind of exists now? Only kind of, I haven't really tested if it works yet --- Makefile | 11 ++++++++++- README.md | 6 ++++++ build/core.mk | 10 +++++----- build/ee-bin.mk | 12 ------------ build/ee-common.mk | 36 ------------------------------------ build/ee-lib.mk | 12 ------------ build/products/bin.mk | 7 +++++++ build/products/lib.mk | 8 ++++++++ build/targets/ee.mk | 41 +++++++++++++++++++++++++++++++++++++++++ build/targets/iop.mk | 35 +++++++++++++++++++++++++++++++++++ rom/Makefile | 35 +++++++++++++++++------------------ rom/ee/Makefile | 28 ++++++++++++++++++++++++++++ rom/ee/_ee_start.s | 9 +++++++++ rom/{ => ee}/ee_start.c | 12 ++---------- rom/rom_start.s | 25 +++++++++++++++++++++++++ rom/start.s | 16 ---------------- 16 files changed, 193 insertions(+), 110 deletions(-) create mode 100644 README.md delete mode 100644 build/ee-bin.mk delete mode 100644 build/ee-common.mk delete mode 100644 build/ee-lib.mk create mode 100644 build/products/bin.mk create mode 100644 build/products/lib.mk create mode 100644 build/targets/ee.mk create mode 100644 build/targets/iop.mk create mode 100644 rom/ee/Makefile create mode 100644 rom/ee/_ee_start.s rename rom/{ => ee}/ee_start.c (55%) create mode 100644 rom/rom_start.s delete mode 100644 rom/start.s diff --git a/Makefile b/Makefile index 42bc0f8..8695c14 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all clean +.PHONY: all copy clean TOP=$(shell pwd) @@ -8,7 +8,16 @@ CONFIG=release endif all: + make -C rom/ee TOP=$(TOP) CONFIG=$(CONFIG) make -C rom TOP=$(TOP) CONFIG=$(CONFIG) clean: + make -C rom/ee TOP=$(TOP) CONFIG=$(CONFIG) clean make -C rom TOP=$(TOP) CONFIG=$(CONFIG) clean + +# copy to PCSX2 BIOS root +# NOTE: You will have to manually edit the PCSX2 ini file, +# since our ROM file does not have anything resembling a +# ROMDIR (and the BIOS selection menu only shows ROMs with a ROMDIR) +copy: + cp bin/ps2rom_$(CONFIG).rom ~/.config/PCSX2/bios diff --git a/README.md b/README.md new file mode 100644 index 0000000..a168e4f --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# ps2rom + +A bare-metal "template"? for the PS2. This essentially builds a replacement BIOS for both the EE and IOP. + +No EE kernel, or IOP RTOS is loaded in this state. It's up to you to write the code to initalize either side of the system. + diff --git a/build/core.mk b/build/core.mk index 04bad6a..9e16157 100644 --- a/build/core.mk +++ b/build/core.mk @@ -23,14 +23,14 @@ 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))) +# include target-specific recipies specifying how to +# build for the target. +include $(TOP)/build/targets/$(TARGET).mk + + diff --git a/build/ee-bin.mk b/build/ee-bin.mk deleted file mode 100644 index a63e298..0000000 --- a/build/ee-bin.mk +++ /dev/null @@ -1,12 +0,0 @@ -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 deleted file mode 100644 index 4f04fef..0000000 --- a/build/ee-common.mk +++ /dev/null @@ -1,36 +0,0 @@ -# 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 deleted file mode 100644 index e58e13e..0000000 --- a/build/ee-lib.mk +++ /dev/null @@ -1,12 +0,0 @@ -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/build/products/bin.mk b/build/products/bin.mk new file mode 100644 index 0000000..75634b1 --- /dev/null +++ b/build/products/bin.mk @@ -0,0 +1,7 @@ +$(BINDIR)/$(FINALNAME).elf: $(OBJDIR)/ $(BINDIR)/ $(OBJECTS) + $(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@ + +clean-products: + -rm $(BINDIR)/$(FINALNAME).elf + -rm $(BINDIR)/$(FINALNAME).map + -rm -rf obj diff --git a/build/products/lib.mk b/build/products/lib.mk new file mode 100644 index 0000000..c15c1c8 --- /dev/null +++ b/build/products/lib.mk @@ -0,0 +1,8 @@ + +$(BINDIR)/lib$(FINALNAME).a: $(OBJDIR)/ $(BINDIR)/ $(OBJECTS) + $(AR) rv $@ $(OBJECTS) + + +clean-products: + -rm $(BINDIR)/lib$(FINALNAME).a + -rm -rf obj diff --git a/build/targets/ee.mk b/build/targets/ee.mk new file mode 100644 index 0000000..79acab1 --- /dev/null +++ b/build/targets/ee.mk @@ -0,0 +1,41 @@ +# common EE toolchain stuff + +# EE toolchain root. +EE_TRIPLET=mips64r5900el-ps2-elf + +CC=$(EE_TRIPLET)-gcc +AS=$(CC) +CXX=$(EE_TRIPLET)-g++ + +# we need to use this to link, because +# we target the 32bit ABI, which the +# EE linker really doesn't like. +# It seems like a hack, probably because it is. +LD=mipsel-ps2-irx-gcc +AR=$(EE_TRIPLET)-ar +OBJCOPY=$(EE_TRIPLET)-objcopy + +ifeq ($(CONFIG),release) +CFLAGS := $(CFLAGS) -mabi=32 -G0 -O3 -Wall -Wextra -Werror -fno-common -fno-strict-aliasing +ASFLAGS := -mabi=32 -xassembler-with-cpp $(ASFLAGS) +LDFLAGS := -s $(LDFLAGS) +endif + +ifeq ($(CONFIG),debug) +CFLAGS := $(CFLAGS) -mabi=32 -G0 -O0 -g3 -Wall -Wextra -fno-common -fno-strict-aliasing +ASFLAGS := -mabi=32 -xassembler-with-cpp -g3 $(ASFLAGS) +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/targets/iop.mk b/build/targets/iop.mk new file mode 100644 index 0000000..715a736 --- /dev/null +++ b/build/targets/iop.mk @@ -0,0 +1,35 @@ +# common IOP toolchain stuff + +# EE toolchain root. +IOP_TRIPLET=mipsel-ps2-irx + +CC=$(IOP_TRIPLET)-gcc +AS=$(CC) +CXX=$(IOP_TRIPLET)-g++ +LD=$(CC) +AR=$(IOP_TRIPLET)-ar +OBJCOPY=$(IOP_TRIPLET)-objcopy + +ifeq ($(CONFIG),release) +CFLAGS := $(CFLAGS) -msoft-float -G0 -O3 -Wall -Wextra -Werror -fno-common -fno-strict-aliasing +ASFLAGS := -EL -xassembler-with-cpp +endif + +ifeq ($(CONFIG),debug) +CFLAGS := $(CFLAGS) -msoft-float -G0 -O0 -g3 -Wall -Wextra -fno-common -fno-strict-aliasing +ASFLAGS := -EL -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/rom/Makefile b/rom/Makefile index ecc5ebd..e198fad 100644 --- a/rom/Makefile +++ b/rom/Makefile @@ -3,42 +3,41 @@ TARGET=ee KIND=bin SELF=$(shell pwd) -INCS = -I$(TOP)/src +INCS = -I$(TOP)/rom -CFLAGS = -ffreestanding -fno-stack-protector -mabi=32 +CFLAGS = -ffreestanding -fno-stack-protector CXXFLAGS = -fno-exceptions -fno-rtti -LDFLAGS = -nostartfiles -nodefaultlibs -Wl,-Map,$(BINDIR)/$(FINALNAME).map -T$(SELF)/link.ld +LDFLAGS = -nostartfiles -nodefaultlibs -Wl,-Map,$(BINDIR)/$(FINALNAME).map -T$(SELF)/link.ld -L$(BINDIR) + +# Link to each portion of the ROM codebase. For now, +# this will only link to the EE ROM codebase. +LIBS=-lps2rom_ee_$(CONFIG) # sources -SRCS = start.s \ - ee_start.c +SRCS = rom_start.s - -.PHONY: all clean clean-products include $(TOP)/build/core.mk -all: $(BINDIR)/$(FINALNAME).rom +.PHONY: all clean clean-products +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 - +# make sure to assemble the primary ROM start routine +# as MIPS-I, so that we don't get into any issues. +$(OBJDIR)/rom_start.o: AS += -march=r3000 $(BINDIR)/$(FINALNAME).rom: $(BINDIR)/$(FINALNAME).elf $(OBJCOPY) -O binary $< $@ -$(BINDIR)/$(FINALNAME).elf: $(OBJDIR)/ $(BINDIR)/ $(OBJECTS) - $(LD) $(LDFLAGS) $(LIBS) $(OBJECTS) -o $@ +clean: clean-products + -rm $(BINDIR)/$(FINALNAME).rom +include $(TOP)/build/products/$(KIND).mk diff --git a/rom/ee/Makefile b/rom/ee/Makefile new file mode 100644 index 0000000..008fd19 --- /dev/null +++ b/rom/ee/Makefile @@ -0,0 +1,28 @@ +NAME=ps2rom_ee +TARGET=ee +KIND=lib + +INCS = -I$(TOP)/rom + +CFLAGS = -ffreestanding -fno-stack-protector +CXXFLAGS = -fno-exceptions -fno-rtti + +# sources +SRCS = _ee_start.s \ + ee_start.c + +.PHONY: all clean clean-products + +include $(TOP)/build/core.mk + +all: $(BINDIR)/lib$(FINALNAME).a + +$(BINDIR)/: + mkdir -p $@ + +$(OBJDIR)/: + mkdir -p $@ + +clean: clean-products + +include $(TOP)/build/products/$(KIND).mk diff --git a/rom/ee/_ee_start.s b/rom/ee/_ee_start.s new file mode 100644 index 0000000..9de8e67 --- /dev/null +++ b/rom/ee/_ee_start.s @@ -0,0 +1,9 @@ +.set noat +.globl _ee_start +.extern ee_start + +// __noreturn __naked void _ee_start() +// This is the primary entrypoint for the EE code +_ee_start: + li $sp, 0x80010000 // temporary stack + j ee_start diff --git a/rom/ee_start.c b/rom/ee/ee_start.c similarity index 55% rename from rom/ee_start.c rename to rom/ee/ee_start.c index 7485070..85ccf64 100644 --- a/rom/ee_start.c +++ b/rom/ee/ee_start.c @@ -6,19 +6,11 @@ static void putc(char c) { } static void puts(const char* s) { - while (*s != 0) { + while (*s) putc(*s++); - } } - __noreturn void _ee_start() { - puts("Hello World from Riri~ (EE)\n"); + puts("EE: Hello World from Lily~\n"); while(1); } - -__asm__ ( - ".global ee_start\n" - "ee_start:\n" - "li $sp, 0x80010000\n" - "j _ee_start\n"); diff --git a/rom/rom_start.s b/rom/rom_start.s new file mode 100644 index 0000000..ff394c2 --- /dev/null +++ b/rom/rom_start.s @@ -0,0 +1,25 @@ +// This is the entry point of the ROM. +// It is limited to 32-bit MIPS-I code, +// because it runs on both the EE and the IOP. +// +// The task of this relatively short bit of code +// is to start the approiate part of the ROM +// on the correct processor. +// +// It's a bit creative, but also a bit annoying. +.org 0 +.set noat + +// externs +.extern _ee_start + +.globl _start +_start: + mfc0 $at, $15 // load the PRID + sltiu $at, 0x59 // Check for EE PRID + bne $15, 0, __iop // Branch if not EE + nop + j _ee_start // Call EE start routine + nop +__iop: // for now the iop stalls. + j __iop diff --git a/rom/start.s b/rom/start.s deleted file mode 100644 index 23e4799..0000000 --- a/rom/start.s +++ /dev/null @@ -1,16 +0,0 @@ -.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