From e36735f6c2a434df5a71a55bf47231a1c8df8ac4 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Tue, 3 Dec 2024 10:43:08 -0600 Subject: [PATCH] improve for out-of-tree usage --- Makefile | 42 ++++++------------------------------ base.mk | 46 ++++++++++++++++++++++++++++++++++++--- common.mk | 54 +++++++++++----------------------------------- example/example.mk | 3 +++ example/sheik.cpp | 18 +++++----------- ip.mk | 29 +++++++++++++++++++++++++ memory.hpp | 8 +++---- 7 files changed, 103 insertions(+), 97 deletions(-) create mode 100644 ip.mk diff --git a/Makefile b/Makefile index b411a33..5aca7d7 100644 --- a/Makefile +++ b/Makefile @@ -1,44 +1,14 @@ all: $(patsubst %.cpp,%.elf,$(wildcard example/*.cpp)) -phony: - -example/arm/%.bin: phony - make -C example/arm $*.bin - +include base.mk include common.mk +include headers.mk -geometry/%.hpp: geometry/%.obj - PYTHONPATH=regs/gen python tools/obj_to_cpp.py $< > $@.tmp - mv $@.tmp $@ +MAKEFILE_PATH := $(patsubst %/,%,$(dir $(abspath $(firstword $(MAKEFILE_LIST))))) +CFLAGS += -I$(MAKEFILE_PATH) +LIB ?= $(MAKEFILE_PATH) -%.data.h: %.data - $(BUILD_BINARY_H) - -%.data.pal.h: %.data.pal - $(BUILD_BINARY_H) - -%.alpha.h: %.alpha - $(BUILD_BINARY_H) - -%.alpha.o: %.alpha - $(BUILD_BINARY_O) - -%.alpha.pal.h: %.alpha.pal - $(BUILD_BINARY_H) - -%.alpha.pal.o: %.alpha.pal - $(BUILD_BINARY_O) - -%.vq.h: %.vq - $(BUILD_BINARY_H) - -%.vq.o: %.vq - $(BUILD_BINARY_O) - -build-fonts: - ./tools/ttf_outline 20 7f 20 0 little /usr/share/fonts/dejavu/DejaVuSans.ttf font/dejavusansmono/dejavusansmono.data - ./tools/ttf_outline 20 7f 20 1 little /usr/share/fonts/dejavu/DejaVuSans.ttf font/dejavusansmono/dejavusansmono_mono.data - ./tools/ttf_outline 20 7f 20 1 little /usr/share/fonts/terminus/ter-u20n.otb font/ter_u20n/ter_u20n.data +include ip.mk include example/example.mk include chess/chess.mk diff --git a/base.mk b/base.mk index 375733d..2547119 100644 --- a/base.mk +++ b/base.mk @@ -34,19 +34,35 @@ define BUILD_BINARY_O $< $@ endef -makefile_path := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) +makefile_path := $(dir $(abspath $(firstword $(MAKEFILE_LIST)))) makefile_relative = $(shell realpath --relative-to $(makefile_path) $(1)) as_obj_binary = _binary_$(subst .,_,$(subst /,_,$(subst .h,,$(call makefile_relative,$(1))))) define BUILD_BINARY_H @echo gen $(call makefile_relative,$@) @echo '#pragma once' > $@ - @echo '#include ' >> $@ + @echo '' >> $@ + @echo '#include ' >> $@ + @echo '' >> $@ + @echo '#ifdef __cplusplus' >> $@ + @echo 'extern "C" {' >> $@ + @echo '#endif' >> $@ + @echo '' >> $@ @echo 'extern uint32_t $(call as_obj_binary,$@)_start __asm("$(call as_obj_binary,$@)_start");' >> $@ @echo 'extern uint32_t $(call as_obj_binary,$@)_end __asm("$(call as_obj_binary,$@)_end");' >> $@ @echo 'extern uint32_t $(call as_obj_binary,$@)_size __asm("$(call as_obj_binary,$@)_size");' >> $@ + @echo '' >> $@ + @echo '#ifdef __cplusplus' >> $@ + @echo '}' >> $@ + @echo '#endif' >> $@ endef +%.txt.o: %.txt + $(BUILD_BINARY_O) + +%.txt.h: %.txt + $(BUILD_BINARY_H) + %.bin.o: %.bin $(BUILD_BINARY_O) @@ -56,9 +72,33 @@ endef %.data.o: %.data $(BUILD_BINARY_O) +%.data.h: %.data + $(BUILD_BINARY_H) + %.data.pal.o: %.data.pal $(BUILD_BINARY_O) +%.data.pal.h: %.data.pal + $(BUILD_BINARY_H) + +%.alpha.h: %.alpha + $(BUILD_BINARY_H) + +%.alpha.o: %.alpha + $(BUILD_BINARY_O) + +%.alpha.pal.h: %.alpha.pal + $(BUILD_BINARY_H) + +%.alpha.pal.o: %.alpha.pal + $(BUILD_BINARY_O) + +%.vq.h: %.vq + $(BUILD_BINARY_H) + +%.vq.o: %.vq + $(BUILD_BINARY_O) + %.o: %.s $(AS) $(AARCH) $(AFLAGS) $(DEBUG) $< -o $@ @@ -69,7 +109,7 @@ endef $(CXX) $(CARCH) $(CFLAGS) $(CXXFLAGS) $(OPT) $(DEBUG) $(DEPFLAGS) -MF ${<}.d -c $< -o $@ %.elf: - $(LD) $(LDFLAGS) -T $(LDSCRIPT) $^ -o $@ + $(LD) $(LDFLAGS) -L $(LIB) -T $(LDSCRIPT) $^ -o $@ %.bin: %.elf $(OBJCOPY) -O binary $< $@ diff --git a/common.mk b/common.mk index 13a0c86..cebe704 100644 --- a/common.mk +++ b/common.mk @@ -1,7 +1,3 @@ -MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) -DIR := $(dir $(MAKEFILE_PATH)) - -LIB ?= . OPT ?= -O2 GENERATED ?= @@ -9,7 +5,6 @@ AARCH = --isa=sh4 --little CARCH = -m4-single-only -ml CFLAGS += -mfsca -funsafe-math-optimizations -ffast-math -CFLAGS += -I"$(dir $(MAKEFILE_PATH))" CXXFLAGS += -std=c++23 @@ -18,41 +13,18 @@ OBJARCH = -O elf32-shl -B sh4 TARGET = sh4-none-elf- START_OBJ = \ - start.o \ - runtime.o \ - sh7091/cache.o + $(LIB)/start.o \ + $(LIB)/runtime.o \ + $(LIB)/sh7091/cache.o -IP_OBJ = \ - systemid.o \ - toc.o \ - sg/sg_sec.o \ - sg_arejp.o \ - sg_areus.o \ - sg_areec.o \ - sg_are00.o \ - sg_are01.o \ - sg_are02.o \ - sg_are03.o \ - sg_are04.o \ - $(START_OBJ) \ - example/serial_transfer.o \ - sh7091/serial.o \ - serial_load.o \ - maple/maple.o \ - font/portfolio_6x8/portfolio_6x8.data.o \ - crc32.o -#sg_ini.o \ -#aip.o +geometry/%.hpp: geometry/%.obj + PYTHONPATH=regs/gen python tools/obj_to_cpp.py $< > $@.tmp + mv $@.tmp $@ -%.o: %.obj - $(OBJCOPY) -g \ - --rename-section IP=.text.$* \ - $< $@ - -ip.elf: $(IP_OBJ) - $(LD) --orphan-handling=error --print-memory-usage -T $(LIB)/ip.lds $^ -o $@ - -include base.mk +build-fonts: + ./tools/ttf_outline 20 7f 20 0 little /usr/share/fonts/dejavu/DejaVuSans.ttf font/dejavusansmono/dejavusansmono.data + ./tools/ttf_outline 20 7f 20 1 little /usr/share/fonts/dejavu/DejaVuSans.ttf font/dejavusansmono/dejavusansmono_mono.data + ./tools/ttf_outline 20 7f 20 1 little /usr/share/fonts/terminus/ter-u20n.otb font/ter_u20n/ter_u20n.data sine.pcm: common.mk sox \ @@ -92,18 +64,18 @@ sine.pcm: common.mk /=./pcm/ELEC.PCM \ /=./pcm/ECCLESIA.PCM -include headers.mk - clean: find -P \ -regextype posix-egrep \ -regex '.*\.(iso|o|d|bin|elf|cue|gch|scramble)$$' \ -exec rm {} \; +phony: + .SUFFIXES: .INTERMEDIATE: .SECONDARY: -.PHONY: all clean +.PHONY: all clean phony %: RCS/%,v %: RCS/% diff --git a/example/example.mk b/example/example.mk index 94ec557..ef585ad 100644 --- a/example/example.mk +++ b/example/example.mk @@ -1,3 +1,6 @@ +example/arm/%.bin: phony + make -C example/arm $*.bin + VIDEO_OUTPUT_OBJ = \ example/video_output.o \ holly/video_output.o \ diff --git a/example/sheik.cpp b/example/sheik.cpp index 933b889..cdbed29 100644 --- a/example/sheik.cpp +++ b/example/sheik.cpp @@ -28,6 +28,8 @@ #include "model/sheik/material.h" #include "model/sheik/model.h" +#include "memory.hpp" + void transfer_scene(float theta) { const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume @@ -122,25 +124,15 @@ void transfer_scene(float theta) sq_transfer_32byte(ta_fifo_polygon_converter); } -template -inline void copy(T * dst, const T * src, const int32_t n) noexcept -{ - int32_t n_t = n / (sizeof (T)); - while (n_t > 0) { - *dst++ = *src++; - n_t--; - } -} - void texture_init() { int length = (sizeof (material)) / (sizeof (material[0])); for (int i = 0; i < length; i++) { uint32_t offset = texture_memory_alloc::texture.start + material[i].pixel.vram_offset; - copy(&texture_memory64[offset / 4], - reinterpret_cast(material[i].pixel.start), - material[i].pixel.size); + memory::copy(&texture_memory64[offset / 4], + reinterpret_cast(material[i].pixel.start), + material[i].pixel.size); } } diff --git a/ip.mk b/ip.mk new file mode 100644 index 0000000..1b4e3b2 --- /dev/null +++ b/ip.mk @@ -0,0 +1,29 @@ +IP_OBJ = \ + systemid.o \ + toc.o \ + sg/sg_sec.o \ + sg_arejp.o \ + sg_areus.o \ + sg_areec.o \ + sg_are00.o \ + sg_are01.o \ + sg_are02.o \ + sg_are03.o \ + sg_are04.o \ + $(START_OBJ) \ + example/serial_transfer.o \ + sh7091/serial.o \ + serial_load.o \ + maple/maple.o \ + font/portfolio_6x8/portfolio_6x8.data.o \ + crc32.o +#sg_ini.o \ +#aip.o + +%.o: %.obj + $(OBJCOPY) -g \ + --rename-section IP=.text.$* \ + $< $@ + +ip.elf: $(IP_OBJ) + $(LD) --orphan-handling=error --print-memory-usage -T $(LIB)/ip.lds $^ -o $@ diff --git a/memory.hpp b/memory.hpp index b5fe51f..c462361 100644 --- a/memory.hpp +++ b/memory.hpp @@ -1,23 +1,23 @@ namespace memory { template -void move(T * dst, const T * src, const uint32_t n) +void move(T * dst, const T * src, uint32_t n) { if (dst < src) { while (n > 0) { - *d++ = *s++; + *dst++ = *src++; n--; } } else { while (n > 0) { n--; - d[n] = s[n]; + dst[n] = src[n]; } } } template -inline void copy(T * dst, const T * src, const uint32_t n) +inline void copy(T * dst, const T * src, uint32_t n) { while (n > 0) { *dst++ = *src++;