From b3765592967e61da705d6ee5553b0baead4361a5 Mon Sep 17 00:00:00 2001 From: Derek Hensley Date: Tue, 27 Jun 2023 17:58:03 -0700 Subject: [PATCH] Irix support (libultra_rom/libultra OK) (#44) * WIP * Add ar * POC * WIP libultra_rom * Simplify Makefile and match initalize * 1 c 1 s left * exceptasm OK * Different weak impl * COMPARE_AR comment for irix * Get ar working and clean up makefile * Some more small cleanup * Split makefile * dereference instead of [0] * Small cleanups * initialize msp and kmc libultra * libultra_rom OK * Warnings * Add to readme * Fix ido ci? * Make libultra_rom default again * PR review * libultra OK * Update Readme * whitespace removal * Small exceptasm clean up * Asm symbols * Bring over improved asm.h * build improvements * asm PR suggestions * Make comment * Fix readme table * strip debug on setup * GBIDEFINEs --- .github/workflows/ci_egcs.txt | 4 +- .github/workflows/ci_gcc.yml | 2 +- .github/workflows/{ci_ido.txt => ci_ido.yml} | 7 +- Makefile | 92 ++-- Makefile.gcc | 50 ++ Makefile.ido | 45 ++ README.md | 9 +- include/PR/os_internal.h | 1 - include/PR/os_internal_flash.h | 8 +- include/PR/rcp.h | 124 ++--- include/ido/math.h | 1 + include/ido/memory.h | 23 + include/ido/stdarg.h | 33 ++ include/ido/stdio.h | 1 + include/ido/stdlib.h | 81 ++++ include/ido/string.h | 42 ++ include/macros.h | 2 +- include/sys/asm.h | 114 +++-- src/debug/assert.c | 7 +- src/debug/kdebugserver.c | 8 +- src/debug/osint_debug.h | 4 +- src/debug/profile.c | 31 +- src/flash/flashallerase.c | 4 +- src/flash/flashallerasethrough.c | 3 +- src/flash/flashchange.c | 4 +- src/flash/flashcheckeraseend.c | 5 +- src/flash/flashclearstatus.c | 3 +- src/flash/flashgetaddr.c | 3 +- src/flash/flashinit.c | 14 +- src/flash/flashreadarray.c | 4 +- src/flash/flashreadid.c | 3 +- src/flash/flashreadstatus.c | 3 +- src/flash/flashreinit.c | 4 +- src/flash/flashsectorerase.c | 3 +- src/flash/flashsectorerasethrough.c | 3 +- src/flash/flashwritearray.c | 5 +- src/flash/flashwritebuffer.c | 3 +- src/host/host_ptn64.c | 13 +- src/host/readhost.c | 6 +- src/io/aisetfreq.c | 2 + src/io/contchannelreset.c | 1 + src/io/conteepread.c | 2 +- src/io/contpfs.c | 5 +- src/io/contramread.c | 10 +- src/io/contramwrite.c | 8 +- src/io/contreaddata.c | 4 +- src/io/contreset.c | 2 +- src/io/controller.c | 7 +- src/io/controller.h | 12 +- src/io/crc.c | 2 +- src/io/gbpakinit.c | 2 +- src/io/gbpakreadwrite.c | 2 + src/io/pfsallocatefile.c | 5 +- src/io/pfschecker.c | 2 +- src/io/pfsfilestate.c | 2 +- src/io/pfsgetlabel.c | 4 +- src/io/pfsinitpak.c | 1 + src/io/pfsisplug.c | 1 + src/io/pi.c | 4 +- src/io/piacs.c | 2 +- src/io/piint.h | 2 +- src/io/viextendvstart.c | 4 +- src/io/vigetcurrframebuf.c | 2 +- src/io/vimgr.c | 4 +- src/io/visetevent.c | 2 +- src/io/visetmode.c | 2 +- src/io/visetspecial.c | 4 +- src/libc/bcmp.s | 12 +- src/libc/bcopy.s | 14 +- src/libc/bzero.s | 16 +- src/libc/ll.c | 2 + src/libc/llbit.c | 1 + src/libc/sprintf.c | 1 + src/libc/string.c | 2 +- src/libc/syncprintf.c | 2 + src/os/createmesgqueue.c | 6 +- src/os/createthread.c | 4 +- src/os/exceptasm.h | 4 +- src/os/exceptasm.s | 41 +- src/os/getfpccsr.s | 3 +- src/os/getintmask.s | 3 +- src/os/initialize.c | 12 +- src/os/initialize_isv.c | 28 +- src/os/initialize_kmc.c | 4 +- src/os/initialize_msp.c | 4 +- src/os/interrupt.s | 2 +- src/os/invaldcache.s | 19 +- src/os/invalicache.s | 11 +- src/os/maptlb.s | 51 +- src/os/osint.h | 2 - src/os/parameters.s | 3 +- src/os/rdbsend.c | 2 +- src/os/seteventmesg.c | 2 +- src/os/setfpccsr.s | 3 +- src/os/setsr.s | 2 +- src/os/setthreadpri.c | 2 +- src/os/settime.c | 2 +- src/os/settimer.c | 2 +- src/os/settlbasid.s | 23 + src/os/setwatchlo.s | 2 +- src/os/startthread.c | 2 +- src/os/stopthread.c | 2 +- src/os/syncputchars.c | 9 +- src/os/thread.c | 2 +- src/os/threadasm.h | 4 +- src/os/timerintr.c | 14 +- src/os/unmaptlb.s | 21 + src/rg/free.c | 8 +- src/rg/getbufcount.c | 2 +- src/rg/getsize.c | 2 +- src/rg/malloc.c | 4 +- src/rg/region.c | 2 +- src/rmon/rmonbrk.c | 16 +- src/rmon/rmonint.h | 2 +- src/rmon/rmonmain.c | 8 +- src/rmon/rmonmem.c | 9 +- src/rmon/rmonmisc.c | 3 +- src/voice/voicecheckresult.c | 19 +- src/voice/voicecleardictionary.c | 3 +- src/voice/voicecontread2.c | 9 +- src/voice/voicecontread36.c | 13 +- src/voice/voicecontrolgain.c | 3 +- src/voice/voicecontwrite20.c | 7 +- src/voice/voicecontwrite4.c | 13 +- src/voice/voicecrc.c | 3 +- src/voice/voicegetreaddata.c | 2 +- src/voice/voicegetstatus.c | 3 +- src/voice/voiceinit.c | 1 + src/voice/voiceinternal.h | 10 +- src/voice/voicemaskdictionary.c | 2 +- src/voice/voicesetadconverter.c | 5 +- src/voice/voicesetword.c | 1 + tools/ar.py | 476 +++++++++++++++++++ tools/libelf.py | 15 +- tools/patch_ar_meta.py | 47 +- tools/strip_debug.sh | 10 + 136 files changed, 1441 insertions(+), 470 deletions(-) rename .github/workflows/{ci_ido.txt => ci_ido.yml} (84%) create mode 100644 Makefile.gcc create mode 100644 Makefile.ido create mode 100644 include/ido/math.h create mode 100644 include/ido/memory.h create mode 100644 include/ido/stdarg.h create mode 100644 include/ido/stdio.h create mode 100644 include/ido/stdlib.h create mode 100644 include/ido/string.h create mode 100755 tools/ar.py create mode 100755 tools/strip_debug.sh diff --git a/.github/workflows/ci_egcs.txt b/.github/workflows/ci_egcs.txt index f903a0a..b6d8fe7 100644 --- a/.github/workflows/ci_egcs.txt +++ b/.github/workflows/ci_egcs.txt @@ -15,10 +15,10 @@ jobs: fail-fast: false matrix: version: [ique_v1.5] - suffix: [_rom] # [, _d, _rom] + suffix: [_rom] # [~, _d, _rom] steps: - - name: Checkout reposistory + - name: Checkout repository uses: actions/checkout@v3 with: ref: ${{ github.event.pull_request.head.sha }} diff --git a/.github/workflows/ci_gcc.yml b/.github/workflows/ci_gcc.yml index 342cdd9..054270a 100644 --- a/.github/workflows/ci_gcc.yml +++ b/.github/workflows/ci_gcc.yml @@ -17,7 +17,7 @@ jobs: suffix: [~, _d, _rom] steps: - - name: Checkout reposistory + - name: Checkout repository uses: actions/checkout@v3 with: ref: ${{ github.event.pull_request.head.sha }} diff --git a/.github/workflows/ci_ido.txt b/.github/workflows/ci_ido.yml similarity index 84% rename from .github/workflows/ci_ido.txt rename to .github/workflows/ci_ido.yml index 36896a9..4d7f8dc 100644 --- a/.github/workflows/ci_ido.txt +++ b/.github/workflows/ci_ido.yml @@ -1,5 +1,4 @@ # CI file for IDO builds -# TODO: rename to `ci_ido.yml` when the repo has IDO support name: Build IDO libultra @@ -15,16 +14,16 @@ jobs: fail-fast: false matrix: version: [L] # [E, F, G, H, I, I_patch, J, K, L] - suffix: [_rom] # [, _d, _rom] + suffix: [~, _rom] # [~, _d, _rom] steps: - - name: Checkout reposistory + - name: Checkout repository uses: actions/checkout@v3 with: ref: ${{ github.event.pull_request.head.sha }} - name: Install package requirements - run: sudo apt-get install -y build-essential python3 + run: sudo apt-get install -y build-essential python3 binutils-mips-linux-gnu - name: Get extra dependencies uses: actions/checkout@v3 diff --git a/Makefile b/Makefile index 8c5ab03..cf87da4 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,10 @@ NON_MATCHING ?= 0 -# One of libgultra_rom, libgultra_d, libgultra +# One of: +# libgultra_rom, libgultra_d, libgultra +# libultra_rom, libultra_d, libultra TARGET ?= libgultra_rom +CROSS ?= mips-linux-gnu- BASE_DIR := base_$(TARGET) BASE_AR := $(TARGET).a @@ -13,24 +16,19 @@ WORKING_DIR := $(shell pwd) CPP := cpp -P AR := ar -AS := tools/gcc/as -CC := tools/gcc/gcc -AR_OLD := tools/gcc/ar -export COMPILER_PATH := $(WORKING_DIR)/tools/gcc - -CFLAGS := -w -nostdinc -c -G 0 -mgp32 -mfp32 -mips3 -D_LANGUAGE_C -ASFLAGS := -w -nostdinc -c -G 0 -mgp32 -mfp32 -mips3 -DMIPSEB -D_LANGUAGE_ASSEMBLY -D_MIPS_SIM=1 -D_ULTRA64 -x assembler-with-cpp -GBIDEFINE := -DF3DEX_GBI_2 -CPPFLAGS = -D_MIPS_SZLONG=32 -D__USE_ISOC99 $(GBIDEFINE) -INCLUDES = -I . -I $(WORKING_DIR)/include -I $(WORKING_DIR)/include/gcc -I $(WORKING_DIR)/include/PR +ifeq ($(findstring libgultra,$(TARGET)),libgultra) +-include Makefile.gcc +else ifeq ($(findstring libultra,$(TARGET)),libultra) +-include Makefile.ido +else +$(error Invalid Target) +endif ifeq ($(findstring _d,$(TARGET)),_d) CPPFLAGS += -D_DEBUG -OPTFLAGS := -O0 else CPPFLAGS += -DNDEBUG -OPTFLAGS := -O3 endif ifeq ($(findstring _rom,$(TARGET)),_rom) @@ -51,6 +49,11 @@ MARKER_FILES := $(O_FILES:.o=.marker) ifneq ($(NON_MATCHING),1) COMPARE_OBJ = cmp $(BASE_DIR)/$(@F:.marker=.o) $(@:.marker=.o) && echo "$(@:.marker=.o): OK" COMPARE_AR = cmp $(BASE_AR) $@ && echo "$@: OK" +ifeq ($(COMPILER),ido) +COMPARE_OBJ = $(CROSS)objcopy -p --strip-debug $(WORKING_DIR)/$(@:.marker=.o) $(WORKING_DIR)/$(@:.marker=.cmp.o) && \ + cmp $(BASE_DIR)/.cmp/$(@F:.marker=.cmp.o) $(WORKING_DIR)/$(@:.marker=.cmp.o) && echo "$(@:.marker=.o): OK" +COMPARE_AR = echo "$@: Cannot compare archive currently" +endif else COMPARE_OBJ := COMPARE_AR := @@ -59,7 +62,7 @@ endif BASE_OBJS := $(wildcard $(BASE_DIR)/*.o) # Try to find a file corresponding to an archive file in any of src/ asm/ or the base directory, prioritizing src then asm then the original file -AR_ORDER = $(foreach f,$(shell $(AR) t $(BASE_AR)),$(shell find $(BUILD_DIR)/src $(BUILD_DIR)/asm $(BUILD_DIR)/$(BASE_DIR) -name $f -type f -print -quit)) +AR_ORDER = $(foreach f,$(shell $(AR) t $(BASE_AR)),$(shell find $(BUILD_DIR)/src $(BUILD_DIR)/asm $(BUILD_DIR)/$(BASE_DIR) -iname $f -type f -print -quit)) MATCHED_OBJS = $(filter-out $(BUILD_DIR)/$(BASE_DIR)/%,$(AR_ORDER)) UNMATCHED_OBJS = $(filter-out $(MATCHED_OBJS),$(AR_ORDER)) NUM_OBJS = $(words $(AR_ORDER)) @@ -76,7 +79,7 @@ $(BUILD_AR): $(MARKER_FILES) ifneq ($(NON_MATCHING),1) # patch archive creation time and individual files' ownership & permissions dd bs=1 skip=24 seek=24 count=12 conv=notrunc if=$(BASE_AR) of=$@ status=none - python3 tools/patch_ar_meta.py $@ + python3 tools/patch_ar_meta.py $@ $(BASE_AR) $(PATCH_AR_FLAGS) @$(COMPARE_AR) @echo "Matched: $(NUM_OBJS_MATCHED)/$(NUM_OBJS)" endif @@ -92,78 +95,53 @@ setup: $(MAKE) -C tools cd $(BASE_DIR) && $(AR) xo ../$(BASE_AR) chmod -R +rw $(BASE_DIR) - -# KMC gcc has a custom flag, N64ALIGN, which forces 8 byte alignment on arrays. This can be used to match, but -# an explicit aligned(8) attribute can be used instead. We opted for the latter for better compatibilty with -# other versions of GCC that do not have this flag. -# export N64ALIGN := ON -export VR4300MUL := ON +ifeq ($(COMPILER),ido) + export CROSS=$(CROSS) && ./tools/strip_debug.sh $(BASE_DIR) +endif $(BUILD_DIR)/$(BASE_DIR)/%.marker: $(BASE_DIR)/%.o cp $< $(@:.marker=.o) ifneq ($(NON_MATCHING),1) -# @$(COMPARE_OBJ) # change file timestamps to match original @touch -r $(BASE_DIR)/$(@F:.marker=.o) $(@:.marker=.o) @$(COMPARE_OBJ) @touch $@ endif -ifeq ($(findstring _d,$(TARGET)),_d) -$(BUILD_DIR)/src/rmon/%.marker: OPTFLAGS := -O0 -endif +GBIDEFINE := -DF3DEX_GBI_2 -STRIP = - -$(BUILD_DIR)/src/os/initialize_isv.marker: OPTFLAGS := -O2 -$(BUILD_DIR)/src/os/initialize_isv.marker: STRIP = && tools/gcc/strip-2.7 -N initialize_isv.c $(WORKING_DIR)/$(@:.marker=.o) $(WORKING_DIR)/$(@:.marker=.o) -$(BUILD_DIR)/src/os/assert.marker: OPTFLAGS := -O0 -$(BUILD_DIR)/src/os/seterrorhandler.marker: OPTFLAGS := -O0 -$(BUILD_DIR)/src/gu/parse_gbi.marker: GBIDEFINE := -$(BUILD_DIR)/src/gu/us2dex_emu.marker: GBIDEFINE := -DF3DEX_GBI -$(BUILD_DIR)/src/sp/sprite.marker: GBIDEFINE := -$(BUILD_DIR)/src/sp/spriteex.marker: GBIDEFINE := -$(BUILD_DIR)/src/sp/spriteex2.marker: GBIDEFINE := -$(BUILD_DIR)/src/mgu/%.marker: export VR4300MUL := OFF -$(BUILD_DIR)/src/mgu/rotate.marker: export VR4300MUL := ON -$(BUILD_DIR)/src/debug/%.marker: ASFLAGS += -P -$(BUILD_DIR)/src/error/%.marker: ASFLAGS += -P -$(BUILD_DIR)/src/log/%.marker: ASFLAGS += -P -$(BUILD_DIR)/src/os/%.marker: ASFLAGS += -P -$(BUILD_DIR)/src/gu/%.marker: ASFLAGS += -P -$(BUILD_DIR)/src/libc/%.marker: ASFLAGS += -P -$(BUILD_DIR)/src/rmon/%.marker: ASFLAGS += -P +$(BUILD_DIR)/src/gu/parse_gbi.marker: GBIDEFINE := -DF3D_GBI +$(BUILD_DIR)/src/gu/us2dex_emu.marker: GBIDEFINE := +$(BUILD_DIR)/src/gu/us2dex2_emu.marker: GBIDEFINE := +$(BUILD_DIR)/src/sp/sprite.marker: GBIDEFINE := -DF3D_GBI +$(BUILD_DIR)/src/sp/spriteex.marker: GBIDEFINE := +$(BUILD_DIR)/src/sp/spriteex2.marker: GBIDEFINE := $(BUILD_DIR)/src/voice/%.marker: OPTFLAGS += -DLANG_JAPANESE -I$(WORKING_DIR)/src -I$(WORKING_DIR)/src/voice $(BUILD_DIR)/src/voice/%.marker: CC := tools/compile_sjis.py -D__CC=$(WORKING_DIR)/$(CC) -D__BUILD_DIR=$(BUILD_DIR) -$(BUILD_DIR)/src/host/host_ptn64.marker: CFLAGS += -fno-builtin # Probably a better way to solve this - -MDEBUG_FILES := $(BUILD_DIR)/src/monutil.marker -$(BUILD_DIR)/src/monutil.marker: CC := tools/ido/cc -$(BUILD_DIR)/src/monutil.marker: ASFLAGS := -non_shared -mips2 -fullwarn -verbose -Xcpluscomm -G 0 -woff 516,649,838,712 -Wab,-r4300_mul -nostdinc -o32 -c $(BUILD_DIR)/%.marker: %.c - cd $( I/DMEM +/* SP read DMA length (R/W): [11:0] length, [19:12] count, [31:20] skip; RDRAM -> I/DMEM */ #define SP_RD_LEN_REG (SP_BASE_REG + 0x08) -//! SP write DMA length (R/W): [11:0] length, [19:12] count, [31:20] skip; I/DMEM -> RDRAM +/* SP write DMA length (R/W): [11:0] length, [19:12] count, [31:20] skip; I/DMEM -> RDRAM */ #define SP_WR_LEN_REG (SP_BASE_REG + 0x0C) -//! SP status (R/W): [14:0] valid bits; see below for write/read mode +/* SP status (R/W): [14:0] valid bits; see below for write/read mode */ #define SP_STATUS_REG (SP_BASE_REG + 0x10) -//! SP DMA full (R): [0] dma full +/* SP DMA full (R): [0] dma full */ #define SP_DMA_FULL_REG (SP_BASE_REG + 0x14) -//! SP DMA busy (R): [0] dma busy +/* SP DMA busy (R): [0] dma busy */ #define SP_DMA_BUSY_REG (SP_BASE_REG + 0x18) -//! SP semaphore (R/W): Read: [0] acquire semaphore; Write: [] release semaphore +/* SP semaphore (R/W): Read: [0] acquire semaphore; Write: [] release semaphore */ #define SP_SEMAPHORE_REG (SP_BASE_REG + 0x1C) -//! SP PC (R/W): [11:0] program counter +/* SP PC (R/W): [11:0] program counter */ #define SP_PC_REG 0x04080000 /** @@ -192,31 +192,31 @@ /** * SP_STATUS_REG: write bits */ -#define SP_CLR_HALT (1 << 0) // clear halt -#define SP_SET_HALT (1 << 1) // set halt -#define SP_CLR_BROKE (1 << 2) // clear broke -#define SP_CLR_INTR (1 << 3) // clear interrupt -#define SP_SET_INTR (1 << 4) // set interrupt -#define SP_CLR_SSTEP (1 << 5) // clear sstep -#define SP_SET_SSTEP (1 << 6) // set sstep -#define SP_CLR_INTR_BREAK (1 << 7) // clear interrupt on break -#define SP_SET_INTR_BREAK (1 << 8) // set interrupt on break -#define SP_CLR_SIG0 (1 << 9) // clear signal 0 -#define SP_SET_SIG0 (1 << 10) // set signal 0 -#define SP_CLR_SIG1 (1 << 11) // clear signal 1 -#define SP_SET_SIG1 (1 << 12) // set signal 1 -#define SP_CLR_SIG2 (1 << 13) // clear signal 2 -#define SP_SET_SIG2 (1 << 14) // set signal 2 -#define SP_CLR_SIG3 (1 << 15) // clear signal 3 -#define SP_SET_SIG3 (1 << 16) // set signal 3 -#define SP_CLR_SIG4 (1 << 17) // clear signal 4 -#define SP_SET_SIG4 (1 << 18) // set signal 4 -#define SP_CLR_SIG5 (1 << 19) // clear signal 5 -#define SP_SET_SIG5 (1 << 20) // set signal 5 -#define SP_CLR_SIG6 (1 << 21) // clear signal 6 -#define SP_SET_SIG6 (1 << 22) // set signal 6 -#define SP_CLR_SIG7 (1 << 23) // clear signal 7 -#define SP_SET_SIG7 (1 << 24) // set signal 7 +#define SP_CLR_HALT (1 << 0) /* clear halt */ +#define SP_SET_HALT (1 << 1) /* set halt */ +#define SP_CLR_BROKE (1 << 2) /* clear broke */ +#define SP_CLR_INTR (1 << 3) /* clear interrupt */ +#define SP_SET_INTR (1 << 4) /* set interrupt */ +#define SP_CLR_SSTEP (1 << 5) /* clear sstep */ +#define SP_SET_SSTEP (1 << 6) /* set sstep */ +#define SP_CLR_INTR_BREAK (1 << 7) /* clear interrupt on break */ +#define SP_SET_INTR_BREAK (1 << 8) /* set interrupt on break */ +#define SP_CLR_SIG0 (1 << 9) /* clear signal 0 */ +#define SP_SET_SIG0 (1 << 10) /* set signal 0 */ +#define SP_CLR_SIG1 (1 << 11) /* clear signal 1 */ +#define SP_SET_SIG1 (1 << 12) /* set signal 1 */ +#define SP_CLR_SIG2 (1 << 13) /* clear signal 2 */ +#define SP_SET_SIG2 (1 << 14) /* set signal 2 */ +#define SP_CLR_SIG3 (1 << 15) /* clear signal 3 */ +#define SP_SET_SIG3 (1 << 16) /* set signal 3 */ +#define SP_CLR_SIG4 (1 << 17) /* clear signal 4 */ +#define SP_SET_SIG4 (1 << 18) /* set signal 4 */ +#define SP_CLR_SIG5 (1 << 19) /* clear signal 5 */ +#define SP_SET_SIG5 (1 << 20) /* set signal 5 */ +#define SP_CLR_SIG6 (1 << 21) /* clear signal 6 */ +#define SP_SET_SIG6 (1 << 22) /* set signal 6 */ +#define SP_CLR_SIG7 (1 << 23) /* clear signal 7 */ +#define SP_SET_SIG7 (1 << 24) /* set signal 7 */ /* * SP_STATUS_REG: read bits @@ -264,44 +264,44 @@ /* * SP_IBIST_REG: write bits */ -#define SP_IBIST_CHECK (1 << 0) // BIST check -#define SP_IBIST_GO (1 << 1) // BIST go -#define SP_IBIST_CLEAR (1 << 2) // BIST clear +#define SP_IBIST_CHECK (1 << 0) /* BIST check */ +#define SP_IBIST_GO (1 << 1) /* BIST go */ +#define SP_IBIST_CLEAR (1 << 2) /* BIST clear */ /* * SP_BIST_REG: read bits * First 2 bits are same as in write mode */ #define SP_IBIST_DONE (1 << 2) -#define SP_IBIST_FAILED 0x78 // bits [6:3], BIST fail +#define SP_IBIST_FAILED 0x78 /* bits [6:3], BIST fail */ /** * Display Processor Command (DPC) Registers */ #define DPC_BASE_REG 0x04100000 -//! DP CMD DMA start (R/W): [23:0] DMEM/RDRAM start address +/* DP CMD DMA start (R/W): [23:0] DMEM/RDRAM start address */ #define DPC_START_REG (DPC_BASE_REG + 0x00) -//! DP CMD DMA end (R/W): [23:0] DMEM/RDRAM end address +/* DP CMD DMA end (R/W): [23:0] DMEM/RDRAM end address */ #define DPC_END_REG (DPC_BASE_REG + 0x04) -//! DP CMD DMA end (R): [23:0] DMEM/RDRAM current address +/* DP CMD DMA end (R): [23:0] DMEM/RDRAM current address */ #define DPC_CURRENT_REG (DPC_BASE_REG + 0x08) -//! DP CMD status (R/W): [9:0] valid bits - see below for definitions +/* DP CMD status (R/W): [9:0] valid bits - see below for definitions */ #define DPC_STATUS_REG (DPC_BASE_REG + 0x0C) -//! DP clock counter (R): [23:0] clock counter +/* DP clock counter (R): [23:0] clock counter */ #define DPC_CLOCK_REG (DPC_BASE_REG + 0x10) -//! DP buffer busy counter (R): [23:0] clock counter +/* DP buffer busy counter (R): [23:0] clock counter */ #define DPC_BUFBUSY_REG (DPC_BASE_REG + 0x14) -//! DP pipe busy counter (R): [23:0] clock counter +/* DP pipe busy counter (R): [23:0] clock counter */ #define DPC_PIPEBUSY_REG (DPC_BASE_REG + 0x18) -//! DP TMEM load counter (R): [23:0] clock counter +/* DP TMEM load counter (R): [23:0] clock counter */ #define DPC_TMEM_REG (DPC_BASE_REG + 0x1C) /** @@ -338,16 +338,16 @@ */ #define DPS_BASE_REG 0x04200000 -//! DP tmem built-in self-test (R/W): [10:0] BIST status bits +/* DP tmem built-in self-test (R/W): [10:0] BIST status bits */ #define DPS_TBIST_REG (DPS_BASE_REG + 0x00) -//! DP span test mode (R/W): [0] Span buffer test access enable +/* DP span test mode (R/W): [0] Span buffer test access enable */ #define DPS_TEST_MODE_REG (DPS_BASE_REG + 0x04) -//! DP span buffer test address (R/W): [6:0] bits +/* DP span buffer test address (R/W): [6:0] bits */ #define DPS_BUFTEST_ADDR_REG (DPS_BASE_REG + 0x08) -//! DP span buffer test data (R/W): [31:0] span buffer data +/* DP span buffer test data (R/W): [31:0] span buffer data */ #define DPS_BUFTEST_DATA_REG (DPS_BASE_REG + 0x0C) /* @@ -362,7 +362,7 @@ * First 2 bits are same as in write mode */ #define DPS_TBIST_DONE (1 << 2) -#define DPS_TBIST_FAILED 0x7F8 // bits [10:3], BIST fail +#define DPS_TBIST_FAILED 0x7F8 /* bits [10:3], BIST fail */ /** * MIPS Interface (MI) Registers @@ -395,11 +395,11 @@ #define MI_MODE_EBUS (1 << 8) /* ebus test mode */ #define MI_MODE_RDRAM (1 << 9) /* RDRAM reg mode */ -//! MI version (R): [7:0] io, [15:8] rac, [23:16] rdp, [31:24] rsp +/* MI version (R): [7:0] io, [15:8] rac, [23:16] rdp, [31:24] rsp */ #define MI_VERSION_REG (MI_BASE_REG + 0x04) #define MI_NOOP_REG MI_VERSION_REG -//! MI interrupt (R): [5:0] valid bits - see below for bit patterns +/* MI interrupt (R): [5:0] valid bits - see below for bit patterns */ #define MI_INTR_REG (MI_BASE_REG + 0x08) /* @@ -792,27 +792,27 @@ */ #define SI_BASE_REG 0x04800000 -//! SI DRAM address (R/W): [23:0] starting RDRAM address +/* SI DRAM address (R/W): [23:0] starting RDRAM address */ #define SI_DRAM_ADDR_REG (SI_BASE_REG + 0x00) -//! SI address read 64B (W): [] write begins a 64B DMA write PIF RAM -> RDRAM +/* SI address read 64B (W): [] write begins a 64B DMA write PIF RAM -> RDRAM */ #define SI_PIF_ADDR_RD64B_REG (SI_BASE_REG + 0x04) -//! Address SI_BASE_REG + (0x08, 0x0C, 0x14) are reserved +/* Address SI_BASE_REG + (0x08, 0x0C, 0x14) are reserved */ -//! SI address write 64B (W): [] write begins a 64B DMA read RDRAM -> PIF RAM */ +/* SI address write 64B (W): [] write begins a 64B DMA read RDRAM -> PIF RAM */ #define SI_PIF_ADDR_WR64B_REG (SI_BASE_REG + 0x10) -//! SI status (R/W): [] any write clears interrupt +/* SI status (R/W): [] any write clears interrupt */ #define SI_STATUS_REG (SI_BASE_REG + 0x18) /* * SI_STATUS_REG: read bits */ -#define SI_STATUS_DMA_BUSY (1 << 0) // DMA in progress -#define SI_STATUS_RD_BUSY (1 << 1) // IO access in progress -#define SI_STATUS_DMA_ERROR (1 << 3) // Overlapping DMA requests -#define SI_STATUS_INTERRUPT (1 << 12) // Interrupt is set +#define SI_STATUS_DMA_BUSY (1 << 0) /* DMA in progress */ +#define SI_STATUS_RD_BUSY (1 << 1) /* IO access in progress */ +#define SI_STATUS_DMA_ERROR (1 << 3) /* Overlapping DMA requests */ +#define SI_STATUS_INTERRUPT (1 << 12) /* Interrupt is set */ /** * Development Board GIO Control Registers diff --git a/include/ido/math.h b/include/ido/math.h new file mode 100644 index 0000000..3e93ceb --- /dev/null +++ b/include/ido/math.h @@ -0,0 +1 @@ +// Nothing needed here diff --git a/include/ido/memory.h b/include/ido/memory.h new file mode 100644 index 0000000..edfff5e --- /dev/null +++ b/include/ido/memory.h @@ -0,0 +1,23 @@ +#ifndef _MEMORY_H +#define _MEMORY_H +/* + memory.h +*/ + +#ifndef _SIZE_T_DEF +#define _SIZE_T_DEF +typedef unsigned size_t; +#endif + +void *memccpy(void *,void *,int,size_t); +void *memchr(void *,int,size_t); +int memcmp(const void *,const void *,size_t); +void *memcpy(void *,const void *,size_t); +int memicmp(void *,void *,size_t); +void *memmove(void *,void *,size_t); +void *memset(void *,int,size_t); + +void movmem(void *,void *,unsigned); +void setmem(void *,unsigned,int); + +#endif diff --git a/include/ido/stdarg.h b/include/ido/stdarg.h new file mode 100644 index 0000000..62f68e0 --- /dev/null +++ b/include/ido/stdarg.h @@ -0,0 +1,33 @@ +#ifndef _STDARG_H +#define _STDARG_H + +typedef char *va_list; +#define _FP 1 +#define _INT 0 +#define _STRUCT 2 + +#define _VA_FP_SAVE_AREA 0x10 +#define _VA_ALIGN(p, a) (((unsigned int)(((char *)p) + ((a) > 4 ? (a) : 4) - 1)) & -((a) > 4 ? (a) : 4)) +#define va_start(vp, parmN) (vp = ((va_list)&parmN + sizeof(parmN))) + +#define __va_stack_arg(list, mode) \ + ( \ + ((list) = (char *)_VA_ALIGN(list, __builtin_alignof(mode)) + \ + _VA_ALIGN(sizeof(mode), 4)), \ + (((char *)list) - (_VA_ALIGN(sizeof(mode), 4) - sizeof(mode)))) + +#define __va_double_arg(list, mode) \ + ( \ + (((long)list & 0x1) /* 1 byte aligned? */ \ + ? (list = (char *)((long)list + 7), (char *)((long)list - 6 - _VA_FP_SAVE_AREA)) \ + : (((long)list & 0x2) /* 2 byte aligned? */ \ + ? (list = (char *)((long)list + 10), (char *)((long)list - 24 - _VA_FP_SAVE_AREA)) \ + : __va_stack_arg(list, mode)))) + +#define va_arg(list, mode) ((mode *)(((__builtin_classof(mode) == _FP && \ + __builtin_alignof(mode) == sizeof(double)) \ + ? __va_double_arg(list, mode) \ + : __va_stack_arg(list, mode))))[-1] +#define va_end(__list) + +#endif /* STDARG_H */ diff --git a/include/ido/stdio.h b/include/ido/stdio.h new file mode 100644 index 0000000..3e93ceb --- /dev/null +++ b/include/ido/stdio.h @@ -0,0 +1 @@ +// Nothing needed here diff --git a/include/ido/stdlib.h b/include/ido/stdlib.h new file mode 100644 index 0000000..98f8a2e --- /dev/null +++ b/include/ido/stdlib.h @@ -0,0 +1,81 @@ +#ifndef _STDLIB_H +#define _STDLIB_H +/* + stdlib.h +*/ + +#ifndef _SIZE_T_DEF +#define _SIZE_T_DEF +typedef unsigned size_t; +#endif + +#ifndef _DIV_T_DEF +#define _DIV_T_DEF +typedef struct DIV_T { + int quot; + int rem; +} div_t; +#endif + +#ifndef _LDIV_T_DEF +#define _LDIV_T_DEF +typedef struct LDIV_T { + long quot; + long rem; +} ldiv_t; +#endif + +#ifndef _LLDIV_T_DEF +#define _LLDIV_T_DEF +typedef struct lldiv_t +{ + long long quot; + long long rem; +} lldiv_t; +#endif + +#ifndef NULL +#define NULL 0 +#endif + +#define _max(a,b) (((a) > (b)) ? (a) : (b)) +#define _min(a,b) (((a) < (b)) ? (a) : (b)) + +#define RAND_MAX 32767 + +int rand(void); +void srand(unsigned); + +int abs(int); +long labs(long); + +div_t div(int,int); +ldiv_t ldiv(long,long); +lldiv_t lldiv(long long, long long); + +int atoi(const char *); +long atol(const char *); + +long strtol(const char *,char **,int); +unsigned long strtoul(const char *,char **,int); + +char *itoa(int,char *,int); +char *ltoa(long,char *,int); +char *ultoa(unsigned long,char *,int); + +double atof(const char *); +double strtod(const char *,char **); + +void qsort(void *,size_t,size_t,int (*)(const void *,const void *)); +void *bsearch(const void *,const void *,size_t,size_t,int (*)(const void *,const void *)); + +void *malloc(size_t); +void *calloc(size_t,size_t); +void *realloc(void *,size_t); +void free(void *); + +void exit(int); + +void abort(void); + +#endif diff --git a/include/ido/string.h b/include/ido/string.h new file mode 100644 index 0000000..d82e1f1 --- /dev/null +++ b/include/ido/string.h @@ -0,0 +1,42 @@ +#ifndef _STRING_H +#define _STRING_H +/* + string.h +*/ + +#ifndef _SIZE_T_DEF +#define _SIZE_T_DEF +typedef unsigned size_t; +#endif + +#include "memory.h" + +char *stpcpy(char *,const char *); +char *strcat(char *,const char *); +char *strchr(const char *,int); +int strcmp(const char *,const char *); +char *strcpy(char *,const char *); +size_t strcspn(const char *,const char *); +char *strdup(const char *); +char *strerror(int); +int stricmp(const char *,const char *); +size_t strlen(const char *); +char *strlwr(char *); +char *strncat(char *,const char *,size_t); +int strncmp(const char *,const char *,size_t); +char *strncpy(char *,const char *,size_t); +int strnicmp(const char *,const char *,size_t); +char *strnset(char *,int,size_t); +char *strpbrk(const char *,const char *); +char *strrchr(const char *,int); +char *strrev(char *); +char *strset(char *,int); +size_t strspn(const char *,const char *); +char *strstr(const char *,const char *); +char *strtok(char *,const char *); +char *strupr(char *); + +#define strcmpi(s1,s2) stricmp(s1,s2) +#define strncmpi(s1,s2,n) strnicmp(s1,s2,n) + +#endif diff --git a/include/macros.h b/include/macros.h index f726659..3057fc8 100644 --- a/include/macros.h +++ b/include/macros.h @@ -5,7 +5,7 @@ #define ARRLEN(x) ((s32)(sizeof(x) / sizeof(x[0]))) -#define STUBBED_PRINTF(x) +#define STUBBED_PRINTF(x) ((void)(x)) #define UNUSED __attribute__((unused)) diff --git a/include/sys/asm.h b/include/sys/asm.h index 445a2bf..97f8fb2 100644 --- a/include/sys/asm.h +++ b/include/sys/asm.h @@ -1,7 +1,7 @@ /************************************************************************ Copyright (C) 1998,1999 NINTENDO Co,Ltd, Copyright (C) 1998,1999 MONEGI CORPORATION, - All Rights Reserved + All Rights Reserved This program is a trade secret of NINTENDO Co,Ltd and MONEGI Corp. and it is not to be reproduced, published, disclosed to others, copied, adapted, distributed, or displayed without the prior authorization of @@ -22,48 +22,92 @@ modified versions thereof. extern "C" { #endif -#define _MIPS_ISA_MIPS1 1 /* R2/3K */ -#define _MIPS_ISA_MIPS2 2 /* R4K/6K */ -#define _MIPS_ISA_MIPS3 3 /* R4K */ -#define _MIPS_ISA_MIPS4 4 /* TFP */ +#define _MIPS_ISA_MIPS1 1 /* R2/3K */ +#define _MIPS_ISA_MIPS2 2 /* R4K/6K */ +#define _MIPS_ISA_MIPS3 3 /* R4K */ +#define _MIPS_ISA_MIPS4 4 /* TFP */ -#define _MIPS_SIM_ABI32 1 /* MIPS MSIG calling convention */ -#define _MIPS_SIM_NABI32 2 /* MIPS new 32-bit abi */ - /* NABI32 is 64bit calling convention but 32bit type sizes) */ -#define _MIPS_SIM_ABI64 3 /* MIPS 64 calling convention */ +#define _MIPS_SIM_ABI32 1 /* MIPS MSIG calling convention */ +#define _MIPS_SIM_NABI32 2 /* MIPS new 32-bit abi */ + /* NABI32 is 64bit calling convention but 32bit type sizes) */ +#define _MIPS_SIM_ABI64 3 /* MIPS 64 calling convention */ -#define LEAF(x) \ - .globl x; \ - .ent x,0; \ -x:; \ - .frame sp,0,ra -#define XLEAF(x) \ - .global x; +/* libgultra doesn't match with the .type directive but iQue sdk asm.h uses it */ +#ifdef BBPLAYER +#define ASM_TYPE_FUNC(x) .type x, @function +#else +#define ASM_TYPE_FUNC(x) +#endif -#define END(proc) \ - .end proc +#define LEAF(x) \ + .globl x ;\ + .align 2 ;\ + ASM_TYPE_FUNC(x) ;\ + .ent x,0 ;\ + x: ;\ + .frame sp,0,ra -#define ABS(x, y) \ - .globl x; \ - x = y - -#define EXPORT(x) \ - .globl x; \ -x: -/* -#define WEAK(x, y) \ - .weak x; \ - .set x,y; -*/ +#if defined(BBPLAYER) || defined(__sgi) +#define XLEAF(x) \ + .globl x ;\ + .aent x,0 ;\ + x: +#else +#define XLEAF(x) \ + .globl x +#endif +#ifdef BBPLAYER +#define END(proc) \ + .end proc ;\ + .size proc, . - proc +#else +#define END(proc) \ + .end proc +#endif + +#define ABS(x, y) \ + .globl x ;\ + x = y + +#define EXPORT(x) \ + .globl x ;\ + x: + +#if defined(BBPLAYER) || defined(__sgi) +#define WEAK(x, y) \ + .weakext x, y +#else #define WEAK(x, y) +#endif -#define STAY1(stmnt) .set noreorder; stmnt; .set reorder; -#define STAY2(stmnt, arg1) .set noreorder; stmnt, arg1; .set reorder; -#define STAY3(stmnt, arg1, arg2) .set noreorder; stmnt, arg1, arg2; .set reorder; -#define NOP .set noreorder; nop; .set reorder; -#define CACHE(op, reg) .set noreorder; cache op, reg; .set reorder; + + +#define STAY1(stmnt) \ + .set noreorder ;\ + stmnt ;\ + .set reorder + +#define STAY2(stmnt, arg1) \ + .set noreorder ;\ + stmnt, arg1 ;\ + .set reorder + +#define STAY3(stmnt, arg1, arg2) \ + .set noreorder ;\ + stmnt, arg1, arg2 ;\ + .set reorder + +#define NOP \ + .set noreorder ;\ + nop ;\ + .set reorder + +#define CACHE(op, reg) \ + .set noreorder ;\ + cache op, reg ;\ + .set reorder #ifdef __cplusplus } diff --git a/src/debug/assert.c b/src/debug/assert.c index f198c38..4bf6f1e 100644 --- a/src/debug/assert.c +++ b/src/debug/assert.c @@ -1,5 +1,10 @@ -#include "PR/os_internal.h" +#include "os.h" +#include "../os/osint.h" +#include "osint_debug.h" + +void __assertBreak(void); void __assert(const char* exp, const char* filename, int line) { osSyncPrintf("\nASSERTION FAULT: %s, %d: \"%s\"\n", filename, line, exp); + __assertBreak; // Doesn't actually do anything, but is needed for matching } diff --git a/src/debug/kdebugserver.c b/src/debug/kdebugserver.c index e1c5e23..948a460 100644 --- a/src/debug/kdebugserver.c +++ b/src/debug/kdebugserver.c @@ -1,13 +1,13 @@ #include "PR/os_internal.h" #include "PR/rcp.h" #include "PR/rdb.h" -#include "../os/osint.h" - -extern u32 __osRdb_IP6_Empty; // not included in final rom, but __osThreadSave is here for some reason OSThread __osThreadSave; +extern OSThread *__osRunningThread; +extern u32 __osRdb_IP6_Empty; + #ifndef _FINALROM static u8 buffer[12]; @@ -34,7 +34,7 @@ static void send_packet(u8* s, u32 n) { for (i = 0; i < n; i++) { packet.buf[i] = s[i]; } - *(volatile rdbPacket*)RDB_BASE_REG = packet; + *(vu32*)RDB_BASE_REG = *(u32*)&packet; } static void clear_IP6(void) { diff --git a/src/debug/osint_debug.h b/src/debug/osint_debug.h index b493035..6d644f2 100644 --- a/src/debug/osint_debug.h +++ b/src/debug/osint_debug.h @@ -4,8 +4,8 @@ extern s32 __osThprofFlag; extern void (*__osThprofFunc)(OSThread*); extern u32 __osThprofLastTimer; extern u32 __osThprofCount; -extern __OSThreadprofile_s thprof[THPROF_IDMAX]; -extern u64 __osThprofHeap[THPROF_STACKSIZE]; +extern __OSThreadprofile_s thprof[]; +extern u64 __osThprofHeap[]; extern void* __osThprofStack; void osThreadProfileCallback(OSThread*); diff --git a/src/debug/profile.c b/src/debug/profile.c index e409382..e98d192 100644 --- a/src/debug/profile.c +++ b/src/debug/profile.c @@ -1,24 +1,17 @@ #include "PR/rdb.h" #include "PR/ultratypes.h" #include "PR/os.h" -#include "stdarg.h" #include "PR/ultraerror.h" #include "PR/ultralog.h" #include "PR/sptask.h" #include "../os/osint.h" #include "macros.h" +#include "osint_debug.h" #ifndef _FINALROM -static u32 __osProfileActive = FALSE; -static u32 __osProfileIOActive = FALSE; - -static OSThread __osProfileIOThread; -unsigned char __osProfileIOStack[2400] ALIGNED(16); - OSTimer __osProfTimer; OSMesg __osProfTimerMsg; -u32 __osProfTimerPeriod; OSMesgQueue __osProfFlushMQ ALIGNED(8); OSMesg __osProfFlushMesg; @@ -26,8 +19,19 @@ OSMesg __osProfFlushMesg; OSMesgQueue __osProfAckMQ ALIGNED(8); OSMesg __osProfAckMesg; +u32 __osProfTimerPeriod; + u32 __osProfNumSections; +static u32 __osProfileActive = FALSE; +static u32 __osProfileIOActive = FALSE; + +unsigned char __osProfileIOStack[2400] ALIGNED(16); + +static OSThread __osProfileIOThread; + +void osProfSendWord(u32 word); + void __osProfileIO(void* arg) { s32 totalBytes; u32 bytesThisBlock; @@ -41,8 +45,7 @@ void __osProfileIO(void* arg) { osProfSendWord(__osProfTimerPeriod); osProfSendWord(__osProfileOverflowBin); - t = __osProfileList; - while (t < __osProfileListEnd) { + for (t = __osProfileList; t < __osProfileListEnd; t++) { osProfSendWord(t->text_start); osProfSendWord(t->histo_size); osRecvMesg(&__osProfAckMQ, NULL, OS_MESG_BLOCK); @@ -61,17 +64,13 @@ void __osProfileIO(void* arg) { totalBytes -= bytesThisBlock; osRecvMesg(&__osProfAckMQ, NULL, OS_MESG_BLOCK); } - t++; } } } void osProfSendWord(u32 word) { - u32 ct; - u8* sendPtr; - - ct = 0; - sendPtr = &word; + u32 ct = 0; + u8* sendPtr = &word; while (ct < sizeof(word)) { ct += __osRdbSend(sendPtr + ct, sizeof(word) - ct, RDB_TYPE_GtoH_PROF_DATA); diff --git a/src/flash/flashallerase.c b/src/flash/flashallerase.c index c66dbd6..0e982a5 100644 --- a/src/flash/flashallerase.c +++ b/src/flash/flashallerase.c @@ -1,4 +1,6 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" + s32 osFlashAllErase(void) { u32 status; diff --git a/src/flash/flashallerasethrough.c b/src/flash/flashallerasethrough.c index 9e52b97..1c3c1dd 100644 --- a/src/flash/flashallerasethrough.c +++ b/src/flash/flashallerasethrough.c @@ -1,4 +1,5 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" void osFlashAllEraseThrough(void) { // start chip erase operation, no waiting for completion diff --git a/src/flash/flashchange.c b/src/flash/flashchange.c index e667668..d92ff7b 100644 --- a/src/flash/flashchange.c +++ b/src/flash/flashchange.c @@ -1,5 +1,5 @@ -#include "PR/os_internal.h" -#include "PR/R4300.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" void osFlashChange(u32 flash_num) { __osFlashHandler.baseAddress = PHYS_TO_K1((FLASH_START_ADDR + flash_num * FLASH_SIZE)); diff --git a/src/flash/flashcheckeraseend.c b/src/flash/flashcheckeraseend.c index 743d24d..c87876b 100644 --- a/src/flash/flashcheckeraseend.c +++ b/src/flash/flashcheckeraseend.c @@ -1,11 +1,12 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" s32 osFlashCheckEraseEnd(void) { u8 status; osFlashReadStatus(&status); - if (status & FLASH_STATUS_ERASE_BUSY) { + if ((status & FLASH_STATUS_ERASE_BUSY) == FLASH_STATUS_ERASE_BUSY) { return FLASH_STATUS_ERASE_BUSY; } else { // not busy, read and clear status diff --git a/src/flash/flashclearstatus.c b/src/flash/flashclearstatus.c index 414b65b..86165d6 100644 --- a/src/flash/flashclearstatus.c +++ b/src/flash/flashclearstatus.c @@ -1,4 +1,5 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" void osFlashClearStatus(void) { // select status mode diff --git a/src/flash/flashgetaddr.c b/src/flash/flashgetaddr.c index a5a9631..dafcab1 100644 --- a/src/flash/flashgetaddr.c +++ b/src/flash/flashgetaddr.c @@ -1,4 +1,5 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" u32 __osFlashGetAddr(u32 page_num) { u32 devAddr; diff --git a/src/flash/flashinit.c b/src/flash/flashinit.c index 25194cd..33c7dc3 100644 --- a/src/flash/flashinit.c +++ b/src/flash/flashinit.c @@ -1,13 +1,13 @@ -#include "PR/os_internal.h" -#include "PR/R4300.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" #include "macros.h" -OSMesgQueue __osFlashMessageQ ALIGNED(8); -OSMesg __osFlashMsgBuf[1]; -OSPiHandle __osFlashHandler ALIGNED(8); -s32 __osFlashVersion; -OSIoMesg __osFlashMsg ALIGNED(8); u32 __osFlashID[4] ALIGNED(8); +OSIoMesg __osFlashMsg ALIGNED(8); +OSMesgQueue __osFlashMessageQ ALIGNED(8); +OSPiHandle __osFlashHandler ALIGNED(8); +OSMesg __osFlashMsgBuf[1]; +s32 __osFlashVersion; OSPiHandle* osFlashInit(void) { u32 flash_type; diff --git a/src/flash/flashreadarray.c b/src/flash/flashreadarray.c index 6134d33..543f236 100644 --- a/src/flash/flashreadarray.c +++ b/src/flash/flashreadarray.c @@ -1,4 +1,6 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" + s32 osFlashReadArray(OSIoMesg* mb, s32 priority, u32 page_num, void* dramAddr, u32 n_pages, OSMesgQueue* mq) { u32 ret; diff --git a/src/flash/flashreadid.c b/src/flash/flashreadid.c index 7752785..3112d74 100644 --- a/src/flash/flashreadid.c +++ b/src/flash/flashreadid.c @@ -1,4 +1,5 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" void osFlashReadId(u32* flash_type, u32* flash_maker) { u8 tmp; diff --git a/src/flash/flashreadstatus.c b/src/flash/flashreadstatus.c index 52df44d..6ff5560 100644 --- a/src/flash/flashreadstatus.c +++ b/src/flash/flashreadstatus.c @@ -1,4 +1,5 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" void osFlashReadStatus(u8* flash_status) { u32 status; diff --git a/src/flash/flashreinit.c b/src/flash/flashreinit.c index deb6070..b2ab93d 100644 --- a/src/flash/flashreinit.c +++ b/src/flash/flashreinit.c @@ -1,5 +1,5 @@ -#include "PR/os_internal.h" -#include "PR/R4300.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" OSPiHandle* osFlashReInit(u8 latency, u8 pulse, u8 page_size, u8 rel_duration, u32 start) { __osFlashHandler.baseAddress = PHYS_TO_K1(start); diff --git a/src/flash/flashsectorerase.c b/src/flash/flashsectorerase.c index fcccc6e..14a0583 100644 --- a/src/flash/flashsectorerase.c +++ b/src/flash/flashsectorerase.c @@ -1,4 +1,5 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" s32 osFlashSectorErase(u32 page_num) { u32 status; diff --git a/src/flash/flashsectorerasethrough.c b/src/flash/flashsectorerasethrough.c index 52097e1..474ee96 100644 --- a/src/flash/flashsectorerasethrough.c +++ b/src/flash/flashsectorerasethrough.c @@ -1,4 +1,5 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" void osFlashSectorEraseThrough(u32 page_num) { // start sector erase operation diff --git a/src/flash/flashwritearray.c b/src/flash/flashwritearray.c index 555f1df..d5f56a0 100644 --- a/src/flash/flashwritearray.c +++ b/src/flash/flashwritearray.c @@ -1,4 +1,5 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" s32 osFlashWriteArray(u32 page_num) { u32 status; @@ -6,7 +7,7 @@ s32 osFlashWriteArray(u32 page_num) { OSMesgQueue timerMesgQueue; OSMesg dummy; - if (__osFlashVersion == NEW_FLASH) { + if ((u32)__osFlashVersion == NEW_FLASH) { osEPiWriteIo(&__osFlashHandler, __osFlashHandler.baseAddress | FLASH_CMD_REG, FLASH_CMD_PAGE_PROGRAM); } diff --git a/src/flash/flashwritebuffer.c b/src/flash/flashwritebuffer.c index 8cbd0c9..ad3f790 100644 --- a/src/flash/flashwritebuffer.c +++ b/src/flash/flashwritebuffer.c @@ -1,4 +1,5 @@ -#include "PR/os_internal.h" +#include "ultra64.h" +#include "PR/os_internal_flash.h" s32 osFlashWriteBuffer(OSIoMesg* mb, s32 priority, void* dramAddr, OSMesgQueue* mq) { s32 ret; diff --git a/src/host/host_ptn64.c b/src/host/host_ptn64.c index 154d0d1..6b33c4e 100644 --- a/src/host/host_ptn64.c +++ b/src/host/host_ptn64.c @@ -1,6 +1,6 @@ #include "PR/os_internal.h" #include "PR/rcp.h" -#include "../io/piint.h" +#include "memory.h" #include "macros.h" @@ -13,6 +13,9 @@ static OSMesgQueue waitPtQueue ALIGNED(8); static OSMesg waitPtQueueBuf; static u32 isWaitPtQueueCreated = FALSE; +void __osPiRelAccess(void); +void __osPiGetAccess(void); + static void createWaitPtQueue(void) { osCreateMesgQueue(&waitPtQueue, &waitPtQueueBuf, 1); isWaitPtQueueCreated = TRUE; @@ -103,14 +106,13 @@ void osReadHost_pt(void* dramAddr, u32 nbytes) { while (ct != 0) { if (ct > 0x100) { - ct1 = 0x100; - ct1_bak = 0x100; + ct1_bak = ct1 = 0x100; ct -= 0x100; } else { ct1_bak = ct1 = ct; ct = 0; } - bp = &buf; + bp = (u32*)&buf; while (ct1 != 0) { *(bp++) = getPT(); @@ -179,8 +181,7 @@ void osWriteHost_pt(void* dramAddr, u32 nbytes) { while (ct != 0) { if (ct > 0x100) { - ct1 = 0x100; - ct1_bak = 0x100; + ct1_bak = ct1 = 0x100; ct -= 0x100; } else { ct1_bak = ct1 = ct; diff --git a/src/host/readhost.c b/src/host/readhost.c index d9ae077..ed6402d 100644 --- a/src/host/readhost.c +++ b/src/host/readhost.c @@ -5,13 +5,13 @@ #include "macros.h" -u32 __osRdb_Read_Data_Buf; -u32 __osRdb_Read_Data_Ct; - static int readHostInitialized = FALSE; static OSMesgQueue readHostMesgQueue ALIGNED(8); static OSMesg readHostMesgBuf[1]; +u32 __osRdb_Read_Data_Buf; +u32 __osRdb_Read_Data_Ct; + void osReadHost(void* dramAddr, u32 nbytes) { char tstr[4]; u32 sent = 0; diff --git a/src/io/aisetfreq.c b/src/io/aisetfreq.c index 4f9afb5..55cf2af 100644 --- a/src/io/aisetfreq.c +++ b/src/io/aisetfreq.c @@ -1,6 +1,8 @@ #include "PR/rcp.h" #include "PR/ultraerror.h" #include "../os/osint.h" +// TODO: not sure if this should be here +extern s32 osViClock; // TODO: this comes from a header #ident "$Revision: 1.17 $" diff --git a/src/io/contchannelreset.c b/src/io/contchannelreset.c index d209ffc..f6ed8a2 100644 --- a/src/io/contchannelreset.c +++ b/src/io/contchannelreset.c @@ -1,5 +1,6 @@ #include "PR/os_internal.h" #include "controller.h" +#include "siint.h" s32 __osContChannelReset(OSMesgQueue* mq, int channel) { s32 i; diff --git a/src/io/conteepread.c b/src/io/conteepread.c index 674254c..708a976 100644 --- a/src/io/conteepread.c +++ b/src/io/conteepread.c @@ -3,9 +3,9 @@ #include "controller.h" #include "siint.h" -static void __osPackEepReadData(u8 address); OSPifRam __osEepPifRam ALIGNED(16); s32 __osEepromRead16K; +static void __osPackEepReadData(u8 address); s32 osEepromRead(OSMesgQueue* mq, u8 address, u8* buffer) { s32 ret = 0; diff --git a/src/io/contpfs.c b/src/io/contpfs.c index bb8e0bf..5f3854d 100644 --- a/src/io/contpfs.c +++ b/src/io/contpfs.c @@ -1,6 +1,7 @@ #include "macros.h" #include "PR/os_internal.h" #include "controller.h" +#include "PR/rmon.h" __OSInode __osPfsInodeCache ALIGNED(8); s32 __osPfsInodeCacheChannel = -1; @@ -15,7 +16,7 @@ u16 __osSumcalc(u8* ptr, int length) { sum += *tmp++; } - return sum; + return sum & 0xFFFF; } s32 __osIdCheckSum(u16* ptr, u16* csum, u16* icsum) { @@ -25,7 +26,7 @@ s32 __osIdCheckSum(u16* ptr, u16* csum, u16* icsum) { *csum = *icsum = 0; for (j = 0; j < ((sizeof(__OSPackId) - sizeof(u32)) / sizeof(u8)); j += 2) { - data = *(u16*)((u8*)ptr + j); + data = *(u16*)((u32)ptr + j); *csum += data; *icsum += ~data; } diff --git a/src/io/contramread.c b/src/io/contramread.c index 3ddf264..b0346e1 100644 --- a/src/io/contramread.c +++ b/src/io/contramread.c @@ -18,13 +18,11 @@ s32 __osContRamRead(OSMesgQueue* mq, int channel, u16 address, u8* buffer) { do { ptr = (u8*)&__osPfsPifRam; - if (__osContLastCmd != CONT_CMD_READ_PAK || __osPfsLastChannel != channel) { + if (__osContLastCmd != CONT_CMD_READ_PAK || (u32)__osPfsLastChannel != channel) { __osContLastCmd = CONT_CMD_READ_PAK; __osPfsLastChannel = channel; - for (i = 0; i < channel; i++) { - *ptr++ = CONT_CMD_REQUEST_STATUS; - } + for (i = 0; i < channel; i++) { *ptr++ = CONT_CMD_REQUEST_STATUS; } __osPfsPifRam.pifstatus = CONT_CMD_EXE; @@ -51,7 +49,7 @@ s32 __osContRamRead(OSMesgQueue* mq, int channel, u16 address, u8* buffer) { ret = CHNL_ERR(*READFORMAT(ptr)); if (!ret) { - if (__osContDataCrc(&READFORMAT(ptr)->data) != READFORMAT(ptr)->datacrc) { + if (__osContDataCrc(READFORMAT(ptr)->data) != READFORMAT(ptr)->datacrc) { ret = __osPfsGetStatus(mq, channel); if (ret) { @@ -60,7 +58,7 @@ s32 __osContRamRead(OSMesgQueue* mq, int channel, u16 address, u8* buffer) { ret = PFS_ERR_CONTRFAIL; } } else { - bcopy(&READFORMAT(ptr)->data, buffer, BLOCKSIZE); + bcopy(READFORMAT(ptr)->data, buffer, BLOCKSIZE); } } else { ret = PFS_ERR_NOPACK; diff --git a/src/io/contramwrite.c b/src/io/contramwrite.c index dde216c..8960c90 100644 --- a/src/io/contramwrite.c +++ b/src/io/contramwrite.c @@ -21,15 +21,13 @@ s32 __osContRamWrite(OSMesgQueue* mq, int channel, u16 address, u8* buffer, int __osSiGetAccess(); do { - ptr = __osPfsPifRam.ramarray; + ptr = (u8*)__osPfsPifRam.ramarray; - if (__osContLastCmd != CONT_CMD_WRITE_PAK || __osPfsLastChannel != channel) { + if (__osContLastCmd != CONT_CMD_WRITE_PAK || (u32)__osPfsLastChannel != channel) { __osContLastCmd = CONT_CMD_WRITE_PAK; __osPfsLastChannel = channel; - for (i = 0; i < channel; i++) { - *ptr++ = CONT_CMD_REQUEST_STATUS; - } + for (i = 0; i < channel; i++) { *ptr++ = CONT_CMD_REQUEST_STATUS; } __osPfsPifRam.pifstatus = CONT_CMD_EXE; diff --git a/src/io/contreaddata.c b/src/io/contreaddata.c index e7c0794..da0cac3 100644 --- a/src/io/contreaddata.c +++ b/src/io/contreaddata.c @@ -23,7 +23,7 @@ s32 osContStartReadData(OSMesgQueue* mq) { } void osContGetReadData(OSContPad* data) { - u8* ptr = __osContPifRam.ramarray; + u8* ptr = (u8*)__osContPifRam.ramarray; __OSContReadFormat readformat; int i; @@ -42,7 +42,7 @@ void osContGetReadData(OSContPad* data) { } static void __osPackReadData(void) { - u8* ptr = __osContPifRam.ramarray; + u8* ptr = (u8*)__osContPifRam.ramarray; __OSContReadFormat readformat; int i; diff --git a/src/io/contreset.c b/src/io/contreset.c index 905de3d..140088a 100644 --- a/src/io/contreset.c +++ b/src/io/contreset.c @@ -41,7 +41,7 @@ void __osPackResetData(void) { int i; __osContPifRam.pifstatus = CONT_CMD_EXE; - ptr = __osContPifRam.ramarray; + ptr = (u8*)__osContPifRam.ramarray; for (i = 0; i < MAXCONTROLLERS; i++) { *ptr++ = CONT_CMD_CHANNEL_RESET; diff --git a/src/io/controller.c b/src/io/controller.c index b397d50..728c207 100644 --- a/src/io/controller.c +++ b/src/io/controller.c @@ -1,8 +1,7 @@ #include "macros.h" #include "PR/os_internal.h" #include "controller.h" - -s32 __osContinitialized = 0; +#include "siint.h" OSPifRam __osContPifRam ALIGNED(16); u8 __osContLastCmd; @@ -12,6 +11,8 @@ OSTimer __osEepromTimer; OSMesgQueue __osEepromTimerQ ALIGNED(8); OSMesg __osEepromTimerMsg; +s32 __osContinitialized = 0; + s32 osContInit(OSMesgQueue* mq, u8* bitpattern, OSContStatus* data) { OSMesg dummy; s32 ret = 0; @@ -82,7 +83,7 @@ void __osPackRequestData(u8 cmd) { } __osContPifRam.pifstatus = CONT_CMD_EXE; - ptr = __osContPifRam.ramarray; + ptr = (u8*)__osContPifRam.ramarray; requestHeader.dummy = CONT_CMD_NOP; requestHeader.txsize = CONT_CMD_RESET_TX; requestHeader.rxsize = CONT_CMD_RESET_RX; diff --git a/src/io/controller.h b/src/io/controller.h index efce970..ef67aa4 100644 --- a/src/io/controller.h +++ b/src/io/controller.h @@ -231,27 +231,27 @@ extern u8 __osMaxControllers; #define ERRCK(fn) \ ret = fn; \ if (ret != 0) \ - return ret; + return ret #define SET_ACTIVEBANK_TO_ZERO \ if (pfs->activebank != 0) \ { \ - ERRCK(__osPfsSelectBank(pfs, 0)) \ - } + ERRCK(__osPfsSelectBank(pfs, 0)); \ + } (void)0 #define PFS_CHECK_ID \ if (__osCheckId(pfs) == PFS_ERR_NEW_PACK) \ - return PFS_ERR_NEW_PACK; + return PFS_ERR_NEW_PACK #define PFS_CHECK_STATUS \ if ((pfs->status & PFS_INITIALIZED) == 0) \ - return PFS_ERR_INVALID; + return PFS_ERR_INVALID #define PFS_GET_STATUS \ __osSiGetAccess(); \ ret = __osPfsGetStatus(queue, channel); \ __osSiRelAccess(); \ if (ret != 0) \ - return ret; + return ret #endif diff --git a/src/io/crc.c b/src/io/crc.c index 4463ee3..05f2698 100644 --- a/src/io/crc.c +++ b/src/io/crc.c @@ -7,7 +7,7 @@ u8 __osContAddressCrc(u16 addr) { do { temp <<= 1; - if (addr & i) { + if ((u32)addr & i) { if (temp & 0x20) { temp ^= 0x14; } else { diff --git a/src/io/gbpakinit.c b/src/io/gbpakinit.c index c8bc07e..d3f19b1 100644 --- a/src/io/gbpakinit.c +++ b/src/io/gbpakinit.c @@ -4,8 +4,8 @@ #include "controller_gbpak.h" OSTimer __osGbpakTimer; -OSMesg __osGbpakTimerMsg; OSMesgQueue __osGbpakTimerQ ALIGNED(8); +OSMesg __osGbpakTimerMsg; s32 osGbpakInit(OSMesgQueue* mq, OSPfs* pfs, int channel) { int i; diff --git a/src/io/gbpakreadwrite.c b/src/io/gbpakreadwrite.c index eef27cf..c73b536 100644 --- a/src/io/gbpakreadwrite.c +++ b/src/io/gbpakreadwrite.c @@ -1,6 +1,8 @@ #include "PR/os_internal.h" #include "controller.h" +s32 __osGbpakSetBank(OSPfs* pfs, u8 bank); + s32 osGbpakReadWrite(OSPfs* pfs, u16 flag, u16 address, u8* buffer, u16 size) { s32 i; s32 ret; diff --git a/src/io/pfsallocatefile.c b/src/io/pfsallocatefile.c index 77dcf23..5e1a85f 100644 --- a/src/io/pfsallocatefile.c +++ b/src/io/pfsallocatefile.c @@ -1,5 +1,6 @@ #include "PR/os_internal.h" #include "controller.h" +#include "PR/rmon.h" #define ROUND_UP_DIVIDE(numerator, denominator) (((numerator) + (denominator)-1) / (denominator)) @@ -54,7 +55,7 @@ s32 osPfsAllocateFile(OSPfs* pfs, u16 company_code, u32 game_code, u8* game_name } for (bank = 0; bank < pfs->banks; bank++) { - ERRCK(__osPfsRWInode(pfs, &inode, OS_READ, bank)); + ERRCK(__osPfsRWInode(pfs, &inode, PFS_READ, bank)); ERRCK(__osPfsDeclearPage(pfs, &inode, file_size_in_pages, &start_page, bank, &decleared, &last_page)); if (start_page != -1) { @@ -64,7 +65,7 @@ s32 osPfsAllocateFile(OSPfs* pfs, u16 company_code, u32 game_code, u8* game_name } else { backup_inode.inode_page[old_last_page].inode_t.bank = bank; backup_inode.inode_page[old_last_page].inode_t.page = start_page; - ERRCK(__osPfsRWInode(pfs, &backup_inode, OS_WRITE, old_bank)); + ERRCK(__osPfsRWInode(pfs, &backup_inode, PFS_WRITE, old_bank)); } if (file_size_in_pages > decleared) { diff --git a/src/io/pfschecker.c b/src/io/pfschecker.c index d813442..6830c0e 100644 --- a/src/io/pfschecker.c +++ b/src/io/pfschecker.c @@ -61,7 +61,7 @@ s32 osPfsChecker(OSPfs* pfs) { } } - if (cc = corrupted(pfs, next_page, &cache) - cl) { + if ((cc = corrupted(pfs, next_page, &cache) - cl) != 0) { break; } diff --git a/src/io/pfsfilestate.c b/src/io/pfsfilestate.c index e85cb96..e617887 100644 --- a/src/io/pfsfilestate.c +++ b/src/io/pfsfilestate.c @@ -43,7 +43,7 @@ s32 osPfsFileState(OSPfs* pfs, s32 file_no, OSPfsState* state) { return PFS_ERR_INCONSISTENT; } - state->file_size = pages * PFS_ONE_PAGE * BLOCKSIZE; + state->file_size = pages * (PFS_ONE_PAGE * BLOCKSIZE); state->company_code = dir.company_code; state->game_code = dir.game_code; bcopy(&dir.game_name, state->game_name, PFS_FILE_NAME_LEN); diff --git a/src/io/pfsgetlabel.c b/src/io/pfsgetlabel.c index 4627711..e8a62fd 100644 --- a/src/io/pfsgetlabel.c +++ b/src/io/pfsgetlabel.c @@ -11,11 +11,11 @@ s32 osPfsGetLabel(OSPfs* pfs, u8* label, int* len) { PFS_CHECK_ID; for (i = 0; i < ARRLEN(pfs->label); i++) { - if (*(pfs->label + i) == 0) { + if (*(u8*)(u32)(i + pfs->label) == 0) { break; } - *label++ = *(pfs->label + i); + *label++ = *(u8*)(u32)(i + pfs->label); } *len = i; diff --git a/src/io/pfsinitpak.c b/src/io/pfsinitpak.c index 5026335..68fa5db 100644 --- a/src/io/pfsinitpak.c +++ b/src/io/pfsinitpak.c @@ -1,5 +1,6 @@ #include "PR/os_internal.h" #include "controller.h" +#include "siint.h" static s32 __osPfsCheckRamArea(OSPfs* pfs); diff --git a/src/io/pfsisplug.c b/src/io/pfsisplug.c index 6c425e9..9607907 100644 --- a/src/io/pfsisplug.c +++ b/src/io/pfsisplug.c @@ -1,6 +1,7 @@ #include "macros.h" #include "PR/os_internal.h" #include "controller.h" +#include "siint.h" OSPifRam __osPfsPifRam ALIGNED(16); diff --git a/src/io/pi.c b/src/io/pi.c index a9cde68..3e67ddb 100644 --- a/src/io/pi.c +++ b/src/io/pi.c @@ -8,8 +8,8 @@ int __osPiDeviceBusy() { register u32 stat = IO_READ(PI_STATUS_REG); if (stat & (PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY)) { - return 1; + return TRUE; } else { - return 0; + return FALSE; } } diff --git a/src/io/piacs.c b/src/io/piacs.c index bd896fb..c95147f 100644 --- a/src/io/piacs.c +++ b/src/io/piacs.c @@ -3,8 +3,8 @@ #define PI_Q_BUF_LEN 1 u32 __osPiAccessQueueEnabled = 0; -OSMesgQueue __osPiAccessQueue ALIGNED(8); static OSMesg piAccessBuf[PI_Q_BUF_LEN]; +OSMesgQueue __osPiAccessQueue ALIGNED(8); void __osPiCreateAccessQueue(void) { __osPiAccessQueueEnabled = 1; diff --git a/src/io/piint.h b/src/io/piint.h index 0c505fa..5551c4b 100644 --- a/src/io/piint.h +++ b/src/io/piint.h @@ -94,7 +94,7 @@ #define LEO_ERROR_29 29 // extern OSDevMgr __osPiDevMgr; -extern OSPiHandle *__osCurrentHandle[2]; +extern OSPiHandle *__osCurrentHandle[]; extern OSPiHandle CartRomHandle; extern OSPiHandle LeoDiskHandle; extern OSMesgQueue __osPiAccessQueue; diff --git a/src/io/viextendvstart.c b/src/io/viextendvstart.c index a1ca8e6..7ee06d3 100644 --- a/src/io/viextendvstart.c +++ b/src/io/viextendvstart.c @@ -6,12 +6,12 @@ void osViExtendVStart(u32 value) { #ifdef _DEBUG if (!__osViDevMgr.active) { __osError(134, 0); - return 0; + return; } if (value > 48) { __osError(135, 1, value); - return 0; + return; } #endif __additional_scanline = value; diff --git a/src/io/vigetcurrframebuf.c b/src/io/vigetcurrframebuf.c index e83006a..de36712 100644 --- a/src/io/vigetcurrframebuf.c +++ b/src/io/vigetcurrframebuf.c @@ -9,7 +9,7 @@ void* osViGetCurrentFramebuffer(void) { #ifdef _DEBUG if (!__osViDevMgr.active) { __osError(ERR_OSVIGETCURRENTFRAMEBUFFER, 0); - return 0; + return NULL; } #endif diff --git a/src/io/vimgr.c b/src/io/vimgr.c index 761ae0d..1f2e5e8 100644 --- a/src/io/vimgr.c +++ b/src/io/vimgr.c @@ -23,12 +23,12 @@ void osCreateViManager(OSPri pri) { #ifdef _DEBUG if ((pri < OS_PRIORITY_IDLE) || (pri > OS_PRIORITY_MAX)) { __osError(ERR_OSCREATEVIMANAGER, 1, pri); - return 0; + return; } #endif if (__osViDevMgr.active) { - return 0; + return; } __osTimerServicesInit(); __additional_scanline = 0; diff --git a/src/io/visetevent.c b/src/io/visetevent.c index ebf3f82..f741a0e 100644 --- a/src/io/visetevent.c +++ b/src/io/visetevent.c @@ -55,7 +55,7 @@ void osViSetEvent(OSMesgQueue* mq, OSMesg m, u32 retraceCount) { #ifdef _DEBUG if (!__osViDevMgr.active) { __osError(ERR_OSVISETEVENT, 0); - return 0; + return; } assert(mq != NULL); diff --git a/src/io/visetmode.c b/src/io/visetmode.c index 1f69993..5ab2094 100644 --- a/src/io/visetmode.c +++ b/src/io/visetmode.c @@ -52,7 +52,7 @@ void osViSetMode(OSViMode* modep) { #ifdef _DEBUG if (!__osViDevMgr.active) { __osError(ERR_OSVISETMODE, 0); - return 0; + return; } assert(modep != NULL); diff --git a/src/io/visetspecial.c b/src/io/visetspecial.c index 1b22dd4..f3bd773 100644 --- a/src/io/visetspecial.c +++ b/src/io/visetspecial.c @@ -16,12 +16,12 @@ void osViSetSpecialFeatures(u32 func) { #ifdef _DEBUG if (!__osViDevMgr.active) { __osError(ERR_OSVISETSPECIAL_VIMGR, 0); - return 0; + return; } if ((func < OS_VI_GAMMA_ON) || (func > OS_VI_SPECIAL_MAX)) { __osError(ERR_OSVISETSPECIAL_VALUE, 1, func); - return 0; + return; } #endif diff --git a/src/libc/bcmp.s b/src/libc/bcmp.s index f6fcc8b..11db46d 100644 --- a/src/libc/bcmp.s +++ b/src/libc/bcmp.s @@ -3,8 +3,12 @@ #include "sys/regdef.h" .text -WEAK(_bcmp, bcmp) -LEAF(bcmp) +#ifdef __sgi +WEAK(bcmp, _bcmp) +#else +#define _bcmp bcmp +#endif +LEAF(_bcmp) xor v0, a0, a1 blt a2, 16, bytecmp @@ -88,5 +92,5 @@ cmpdone: cmpne: li v0, 1 jr ra - -END(bcmp) + +.end _bcmp diff --git a/src/libc/bcopy.s b/src/libc/bcopy.s index 9bf1583..216c378 100644 --- a/src/libc/bcopy.s +++ b/src/libc/bcopy.s @@ -3,8 +3,12 @@ #include "sys/regdef.h" .text -WEAK(_bcopy, bcopy) -LEAF(bcopy) +#ifdef __sgi +WEAK(bcopy, _bcopy) +#else +#define _bcopy bcopy +#endif +LEAF(_bcopy) move a3, a1 beqz a2, ret beq a0, a1, ret @@ -212,7 +216,5 @@ backwards_4: addiu a1, a1, -4 addiu a2, a2, -4 b backwards_4 - -END(bcopy) -/* -*/ + +.end _bcopy diff --git a/src/libc/bzero.s b/src/libc/bzero.s index 13dcfa2..894c186 100644 --- a/src/libc/bzero.s +++ b/src/libc/bzero.s @@ -3,10 +3,15 @@ #include "sys/regdef.h" .text -WEAK(_bzero, bzero) -WEAK(_blkclr, blkclr) -LEAF(bzero) -XLEAF(blkclr) +#ifdef __sgi +WEAK(bzero, _bzero) +WEAK(blkclr, _blkclr) +#else +#define _bzero bzero +#define _blkclr blkclr +#endif +LEAF(_bzero) +XLEAF(_blkclr) negu v1, a0 blt a1, 12, bytezero @@ -55,4 +60,5 @@ bytezero: bne a0, a1, 1b zerodone: jr ra -END(bzero) + +.end _bzero diff --git a/src/libc/ll.c b/src/libc/ll.c index 512a998..4334b03 100644 --- a/src/libc/ll.c +++ b/src/libc/ll.c @@ -1,3 +1,5 @@ +#include "PR/os.h" + unsigned long long __ull_rshift(unsigned long long a0, unsigned long long a1) { return a0 >> a1; } diff --git a/src/libc/llbit.c b/src/libc/llbit.c index 3e7e03b..0680726 100644 --- a/src/libc/llbit.c +++ b/src/libc/llbit.c @@ -1,3 +1,4 @@ +#include "PR/os.h" #include "PR/ultratypes.h" s64 __ll_bit_extract(u64* addr, unsigned int start_bit, unsigned int length) { diff --git a/src/libc/sprintf.c b/src/libc/sprintf.c index 7dd86d8..fd95523 100644 --- a/src/libc/sprintf.c +++ b/src/libc/sprintf.c @@ -1,5 +1,6 @@ #include "xstdio.h" #include "string.h" +#include "os.h" // TODO: this comes from a header #ident "$Revision: 1.23 $" diff --git a/src/libc/string.c b/src/libc/string.c index ed4c480..0f01d1f 100644 --- a/src/libc/string.c +++ b/src/libc/string.c @@ -17,7 +17,7 @@ char* strchr(const char* s, int c) { size_t strlen(const char* s) { const char* sc = s; - while (*sc) { + while (*sc != 0) { sc++; } return sc - s; diff --git a/src/libc/syncprintf.c b/src/libc/syncprintf.c index 6e13534..867bd99 100644 --- a/src/libc/syncprintf.c +++ b/src/libc/syncprintf.c @@ -1,5 +1,7 @@ #include "stdarg.h" #include "PR/os.h" +#include "PR/rdb.h" +#include "xstdio.h" extern void* __printfunc; diff --git a/src/os/createmesgqueue.c b/src/os/createmesgqueue.c index 8c5e800..5c92f04 100644 --- a/src/os/createmesgqueue.c +++ b/src/os/createmesgqueue.c @@ -7,12 +7,12 @@ void osCreateMesgQueue(OSMesgQueue* mq, OSMesg* msg, s32 msgCount) { #ifdef _DEBUG if (msgCount <= 0) { __osError(ERR_OSCREATEMESGQUEUE, 1, msgCount); - return 0; + return; } #endif - mq->mtqueue = &__osThreadTail.next; - mq->fullqueue = &__osThreadTail.next; + mq->mtqueue = (OSThread*)&__osThreadTail.next; + mq->fullqueue = (OSThread*)&__osThreadTail.next; mq->validCount = 0; mq->first = 0; mq->msgCount = msgCount; diff --git a/src/os/createthread.c b/src/os/createthread.c index ee2ef60..fdf0c20 100644 --- a/src/os/createthread.c +++ b/src/os/createthread.c @@ -12,12 +12,12 @@ void osCreateThread(OSThread* t, OSId id, void (*entry)(void*), void* arg, void* #ifdef _DEBUG if ((u32)sp & 0x7) { __osError(ERR_OSCREATETHREAD_SP, 1, sp); - return 0; + return; } if ((p < OS_PRIORITY_IDLE) || (p > OS_PRIORITY_MAX)) { __osError(ERR_OSCREATETHREAD_PRI, 1, p); - return 0; + return; } #endif diff --git a/src/os/exceptasm.h b/src/os/exceptasm.h index 48c1234..3e599ee 100644 --- a/src/os/exceptasm.h +++ b/src/os/exceptasm.h @@ -13,11 +13,11 @@ #define OS_EVENTSTATE_MESSAGE_QUEUE 0 #define OS_EVENTSTATE_MESSAGE 4 -// __osHwInt struct member offsets +/* __osHwInt struct member offsets */ #define HWINTR_CALLBACK 0x00 #define HWINTR_SP 0x04 -// __osHwInt struct size +/* __osHwInt struct size */ #define HWINTR_SIZE 0x8 #endif diff --git a/src/os/exceptasm.s b/src/os/exceptasm.s index 5a0bdcf..577aed5 100644 --- a/src/os/exceptasm.s +++ b/src/os/exceptasm.s @@ -64,7 +64,6 @@ __osIntTable: EXPORT(__osCauseTable_pt) .byte 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 .byte 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 - .byte 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 #endif .data @@ -109,7 +108,7 @@ LEAF(__ptExceptionPreamble) nop nop pt_next: - addiu k0, %lo(__ptException) + addiu k0, k0, %lo(__ptException) jr k0 nop .set reorder @@ -194,14 +193,14 @@ STAY2(mfc0 t0, C0_CAUSE) andi t2, t1, CAUSE_IP7 beqz t2, notIP7 /* clear rdb write interrupt */ - li t1, RDB_WRITE_INTR_REG + la t1, RDB_WRITE_INTR_REG sw zero, (t1) IP7check: STAY2(mfc0 t0, C0_CAUSE) andi t0, t0, CAUSE_IP7 bne zero, t0, IP7check - lui t2, (RDB_BASE_REG >> 16) - lw t0, (RDB_BASE_REG & 0xFFFF)(t2) + la t2, RDB_BASE_REG + lw t0, (t2) srl t1, t0, 0x1a andi t1, t1, 0x3f li t2, RDB_TYPE_HtoG_DATA @@ -331,14 +330,14 @@ notIP7: andi t2, t1, CAUSE_IP6 beqz t2, savecontext /* clear rdb read interrupt */ - li t1, RDB_READ_INTR_REG + la t1, RDB_READ_INTR_REG sw zero, (t1) lw t2, __osRdb_IP6_Ct - bnez t2, 1f + bnez t2, 2f li t2, 1 sw t2, __osRdb_IP6_Empty b rdbout -1: +2: addi t2, t2, -1 sw t2, __osRdb_IP6_Ct lw t0, __osRdb_IP6_Data @@ -357,8 +356,8 @@ checkIP6: STAY2(mfc0 t0, C0_CAUSE) andi t0, t0, CAUSE_IP6 bne zero, t0, checkIP6 - lui t0, (RDB_BASE_REG >> 16) - sw t2, (RDB_BASE_REG & 0xFFFF)(t0) + la t0, RDB_BASE_REG + sw t2, (t0) rdbout: ld t0, THREAD_GP8(k0) ld t1, THREAD_GP9(k0) @@ -368,11 +367,13 @@ rdbout: .set at lw k1, THREAD_SR(k0) STAY2(mtc0 k1, C0_SR) +.set noreorder nop nop nop nop eret +.set reorder skip_kmc_mode: #endif @@ -393,6 +394,7 @@ savecontext: sd t1, THREAD_GP9(k0) ld t1, THREAD_GP10(t0) sd t1, THREAD_GP10(k0) +3: sd $2, THREAD_GP2(k0) sd $3, THREAD_GP3(k0) sd $4, THREAD_GP4(k0) @@ -461,8 +463,8 @@ STAY2(mfc0 t0, C0_EPC) sw t0, THREAD_PC(k0) lw t0, THREAD_FP(k0) beqz t0, 1f - cfc1 t0, fcr31 - nop +STAY2(cfc1 t0, fcr31) + NOP sw t0, THREAD_FPCSR(k0) sdc1 $f0, THREAD_FP0(k0) sdc1 $f2, THREAD_FP2(k0) @@ -484,8 +486,10 @@ STAY2(mfc0 t0, C0_EPC) STAY2(mfc0 t0, C0_CAUSE) sw t0, THREAD_CAUSE(k0) +.set noreorder li t1, OS_STATE_RUNNABLE sh t1, THREAD_STATE(k0) +.set reorder #ifndef _FINALROM lw a0, __os_Kdebug_Pkt @@ -881,13 +885,13 @@ LEAF(__osEnqueueThread) lw t8, 0(a0) lw ta3, THREAD_PRI(a1) lw ta2, THREAD_PRI(t8) - blt ta2, ta3, 1f -2: + blt ta2, ta3, 2f +1: move t9, t8 lw t8, THREAD_NEXT(t8) lw ta2, THREAD_PRI(t8) - bge ta2, ta3, 2b -1: + bge ta2, ta3, 1b +2: lw t8, THREAD_NEXT(t9) sw t8, THREAD_NEXT(a1) sw a1, THREAD_NEXT(t9) @@ -918,6 +922,7 @@ LEAF(__osDispatchThread) la t0, __osThprofFunc lw t0, (t0) beqz t0, __osDispatchThreadSave +1: lw a0, __osPreviousThread lw sp, __osThprofStack jalr t0 @@ -993,6 +998,7 @@ STAY2(ctc1 k1, fcr31) ldc1 $f30, THREAD_FP30(k0) 1: +.set noreorder lw k1, THREAD_RCP(k0) la k0, __OSGlobalIntMask lw k0, 0(k0) @@ -1010,10 +1016,13 @@ STAY2(ctc1 k1, fcr31) nop nop eret +.set reorder END(__osDispatchThread) LEAF(__osCleanupThread) move a0, zero +#if !defined(BBPLAYER) && !defined(__sgi) nop +#endif jal osDestroyThread END(__osCleanupThread) diff --git a/src/os/getfpccsr.s b/src/os/getfpccsr.s index 05bb19d..56837a5 100644 --- a/src/os/getfpccsr.s +++ b/src/os/getfpccsr.s @@ -6,5 +6,4 @@ LEAF(__osGetFpcCsr) STAY2(cfc1 v0, fcr31) jr ra -END(__osGetFpcCsr) -.globl __osGetSR +END(__osGetSR) # @bug: Should be __osGetFpcCsr diff --git a/src/os/getintmask.s b/src/os/getintmask.s index deaccb6..573eed1 100644 --- a/src/os/getintmask.s +++ b/src/os/getintmask.s @@ -18,7 +18,7 @@ LEAF(osGetIntMask) lw t1, PHYS_TO_K1(MI_INTR_MASK_REG) beqz t1, 1f - la t0, __OSGlobalIntMask #this is intentionally a macro in the branch delay slot + la t0, __OSGlobalIntMask # this is intentionally a macro in the branch delay slot lw t0, 0(t0) srl t0, t0, 0x10 @@ -29,4 +29,5 @@ LEAF(osGetIntMask) sll t2, t1, 0x10 or v0, v0, t2 jr ra + nop END(osGetIntMask) diff --git a/src/os/initialize.c b/src/os/initialize.c index 95f6c8c..3edfe45 100644 --- a/src/os/initialize.c +++ b/src/os/initialize.c @@ -7,7 +7,7 @@ typedef struct { /* 0x8 */ unsigned int inst3; /* 0xC */ unsigned int inst4; } __osExceptionVector; -extern __osExceptionVector __osExceptionPreamble; +extern __osExceptionVector __osExceptionPreamble[]; extern OSPiHandle __Dom1SpeedParam; extern OSPiHandle __Dom2SpeedParam; @@ -19,8 +19,8 @@ u32 __OSGlobalIntMask = OS_IM_ALL; #ifdef _FINALROM u32 __osFinalrom; #else -void* __printfunc = NULL; u32 __kmc_pt_mode; +void* __printfunc = NULL; #endif void __createSpeedParam(void) { @@ -54,10 +54,10 @@ void __osInitialize_common() { while (__osSiRawWriteIo(PIF_RAM_END - 3, pifdata | 8)) { ; // todo: magic contant } - *(__osExceptionVector*)UT_VEC = __osExceptionPreamble; - *(__osExceptionVector*)XUT_VEC = __osExceptionPreamble; - *(__osExceptionVector*)ECC_VEC = __osExceptionPreamble; - *(__osExceptionVector*)E_VEC = __osExceptionPreamble; + *(__osExceptionVector*)UT_VEC = *__osExceptionPreamble; + *(__osExceptionVector*)XUT_VEC = *__osExceptionPreamble; + *(__osExceptionVector*)ECC_VEC = *__osExceptionPreamble; + *(__osExceptionVector*)E_VEC = *__osExceptionPreamble; osWritebackDCache((void*)UT_VEC, E_VEC - UT_VEC + sizeof(__osExceptionVector)); osInvalICache((void*)UT_VEC, E_VEC - UT_VEC + sizeof(__osExceptionVector)); __createSpeedParam(); diff --git a/src/os/initialize_isv.c b/src/os/initialize_isv.c index c434543..60c1385 100644 --- a/src/os/initialize_isv.c +++ b/src/os/initialize_isv.c @@ -28,13 +28,11 @@ typedef struct { #define IS64_MAGIC 'IS64' -ISVDbg* gISVDbgPrnAdrs; - -u32 leoComuBuffAdd; +__osExceptionVector ramOldVector ALIGNED(8); u32 gISVFlag; u16 gISVChk; - -__osExceptionVector ramOldVector ALIGNED(8); +ISVDbg* gISVDbgPrnAdrs; +u32 leoComuBuffAdd; static OSPiHandle* is_Handle; @@ -43,7 +41,7 @@ void isPrintfInit(void) { osEPiWriteIo(is_Handle, &gISVDbgPrnAdrs->put, 0); osEPiWriteIo(is_Handle, &gISVDbgPrnAdrs->get, 0); - osEPiWriteIo(is_Handle, gISVDbgPrnAdrs, IS64_MAGIC); + osEPiWriteIo(is_Handle, &gISVDbgPrnAdrs->magic, IS64_MAGIC); } static void* is_proutSyncPrintf(void* arg, const u8* str, u32 count) { @@ -51,12 +49,13 @@ static void* is_proutSyncPrintf(void* arg, const u8* str, u32 count) { s32 p; s32 start; s32 end; + u32* magic = &gISVDbgPrnAdrs->magic; if (gISVDbgPrnAdrs == NULL) { return 0; } - osEPiReadIo(is_Handle, (u32)&gISVDbgPrnAdrs->magic, &data); + osEPiReadIo(is_Handle, (u32)magic, &data); if (data != IS64_MAGIC) { return 1; } @@ -102,9 +101,8 @@ static void* is_proutSyncPrintf(void* arg, const u8* str, u32 count) { int __checkHardware_isv(void) { u32 i = 0; - u32 data; // BUG: data is used uninitialized + u32 data; u32 save[4]; - u32 addr; OSPiHandle* hnd = osCartRomInit(); gISVDbgPrnAdrs = NULL; @@ -113,15 +111,17 @@ int __checkHardware_isv(void) { gISVChk = 0; for (i = 0; i < 4; i++) { - osEPiReadIo(hnd, 0xB0000100 + i * 4, save + i); + osEPiReadIo(hnd, 0xB0000100 + i * 4, &save[i]); } - // data = 0; +#ifndef __GNU__ // BUG: data is used uninitialized for GCC + data = 0; +#endif osEPiWriteIo(hnd, 0xB000010C, data); data = IS64_MAGIC; osEPiWriteIo(hnd, 0xB0000100, IS64_MAGIC); - for (i = 0; i <= 0x1FFFF; i++) { + for (i = 0; i < 0x20000; i++) { osEPiReadIo(hnd, 0xB000010C, &data); if (data == IS64_MAGIC) { data = 0; @@ -147,6 +147,8 @@ int __checkHardware_isv(void) { void __osInitialize_isv(void) { void (*fn)(void); OSPiHandle* hnd; + s32 pad; + s32 pad2; if (gISVFlag == IS64_MAGIC || __checkHardware_isv()) { if (gISVDbgPrnAdrs != NULL) { @@ -163,7 +165,7 @@ void __osInitialize_isv(void) { osInvalICache(&ramOldVector, 0x10); osWritebackDCache(0x80000000, 0x190); osInvalICache(0x80000000, 0x190); - osEPiReadIo(hnd, 0xBFF00010, &fn); + osEPiReadIo(hnd, 0xBFF00010, (u32*)&fn); fn(); } if (gISVChk & 2) { diff --git a/src/os/initialize_kmc.c b/src/os/initialize_kmc.c index 20e5d2c..4952a0d 100644 --- a/src/os/initialize_kmc.c +++ b/src/os/initialize_kmc.c @@ -11,7 +11,7 @@ typedef struct { /* 0xC */ unsigned int inst4; } __osExceptionVector; -extern __osExceptionVector* __ptExceptionPreamble; +extern __osExceptionVector __ptExceptionPreamble[]; static volatile unsigned int* stat = (unsigned*)0xbff08004; static volatile unsigned int* wport = (unsigned*)0xbff08000; @@ -96,7 +96,7 @@ void __osInitialize_kmc(void) { return; } - src = (unsigned*)&__ptExceptionPreamble; + src = (unsigned*)__ptExceptionPreamble; dst = (unsigned*)E_VEC; *dst++ = *src++; *dst++ = *src++; diff --git a/src/os/initialize_msp.c b/src/os/initialize_msp.c index f8ce401..1f120e1 100644 --- a/src/os/initialize_msp.c +++ b/src/os/initialize_msp.c @@ -11,7 +11,7 @@ typedef struct { /* 0xC */ unsigned int inst4; } __osExceptionVector; -extern __osExceptionVector* __ptExceptionPreamble; +extern __osExceptionVector __ptExceptionPreamble[]; static volatile unsigned int* stat = (unsigned*)0xbff08004; static volatile unsigned int* wport = (unsigned*)0xbff08000; @@ -96,7 +96,7 @@ void __osInitialize_msp(void) { return; } - src = (unsigned*)&__ptExceptionPreamble; + src = (unsigned*)__ptExceptionPreamble; dst = (unsigned*)E_VEC; *dst++ = *src++; *dst++ = *src++; diff --git a/src/os/interrupt.s b/src/os/interrupt.s index 8fac481..e8f02d0 100644 --- a/src/os/interrupt.s +++ b/src/os/interrupt.s @@ -15,7 +15,7 @@ LEAF(__osDisableInt) lw t0, (t2) andi t0, 0xFF00 beq t0, t3, No_Change_Global_Int - la t2, __osRunningThread + la t2, __osRunningThread # this is intentionally a macro in the branch delay slot lw t1, 280(t2) andi t2, t1, 0xFF00 and t2, t0 diff --git a/src/os/invaldcache.s b/src/os/invaldcache.s index 24cfb1c..ea6a657 100644 --- a/src/os/invaldcache.s +++ b/src/os/invaldcache.s @@ -3,38 +3,36 @@ #include "sys/regdef.h" .text -.set noreorder LEAF(osInvalDCache) blez a1, 3f - nop li t3, DCACHE_SIZE bgeu a1, t3, 4f - nop + move t0, a0 addu t1, a0, a1 bgeu t0, t1, 3f - nop + addiu t1, t1, -DCACHE_LINESIZE andi t2, t0, DCACHE_LINEMASK beqz t2, 1f - nop + subu t0, t0, t2 CACHE((C_HWBINV|CACH_PD), (t0)) bgeu t0, t1, 3f - # nop + addiu t0, t0, DCACHE_LINESIZE 1: andi t2, t1, DCACHE_LINEMASK beqz t2, 2f - # nop + subu t1, t1, t2 CACHE((C_HWBINV|CACH_PD), 0x10(t1)) bltu t1, t0, 3f - # nop + 2: CACHE((C_HINV|CACH_PD), (t0)) .set noreorder @@ -43,7 +41,7 @@ LEAF(osInvalDCache) .set reorder 3: jr ra - # nop + 4: li t0, KUSIZE addu t1, t0, t3 @@ -56,6 +54,5 @@ LEAF(osInvalDCache) .set reorder jr ra - # nop - # nop + END(osInvalDCache) diff --git a/src/os/invalicache.s b/src/os/invalicache.s index aeaf200..f5ceb58 100644 --- a/src/os/invalicache.s +++ b/src/os/invalicache.s @@ -3,17 +3,16 @@ #include "sys/regdef.h" .text -.set noreorder LEAF(osInvalICache) blez a1, 2f - nop + li t3, ICACHE_SIZE bgeu a1, t3, 3f - nop + move t0, a0 addu t1, a0, a1 bgeu t0, t1, 2f - nop + addiu t1, t1, -ICACHE_LINESIZE andi t2, t0, ICACHE_LINEMASK subu t0, t0, t2 @@ -25,7 +24,7 @@ LEAF(osInvalICache) .set reorder 2: jr ra - #nop + 3: li t0, KUSIZE addu t1, t0, t3 @@ -38,5 +37,5 @@ LEAF(osInvalICache) .set reorder jr ra - #nop + END(osInvalICache) diff --git a/src/os/maptlb.s b/src/os/maptlb.s index d02e3fd..20125b6 100644 --- a/src/os/maptlb.s +++ b/src/os/maptlb.s @@ -1,3 +1,4 @@ +#include "PR/ultraerror.h" #include "PR/R4300.h" #include "sys/asm.h" #include "sys/regdef.h" @@ -9,7 +10,53 @@ #define oddpaddr 16(sp) #define asid 20(sp) .text +.set noreorder LEAF(osMapTLB) +#if defined (_DEBUG) && defined (__sgi) + bgez index, 1f + nop + b 2f + nop +1: + li t0, 0x1F +.set noat + slt AT, index, t0 + bnez AT, 3f + nop +.set at +2: + move a2, a0 + li a0, ERR_OSMAPTLB_INDEX + li a1, 1 + j __osError + nop + +3: + lw t0, asid + li t1, -1 +.set noat + slt AT, t0, t1 + beqz AT, 4f + nop +.set at + b 5f + nop +4: + li t1, 0xFF +.set noat + slt AT, t1, t0 + beqz AT, 6f + nop +.set at +5: + move a2, t0 + li a0, ERR_OSMAPTLB_ASID + li a1, 1 + j __osError + nop +6: +.set reorder +#endif STAY2(mfc0 t0, C0_ENTRYHI) STAY2(mtc0 index, C0_INX) STAY2(mtc0 pm, C0_PAGEMASK) @@ -17,10 +64,10 @@ LEAF(osMapTLB) lw t1, asid beq t1, -1, 7f li ta0, TLBLO_G -.set reorder li t2, TLBLO_NONCOHRNT | TLBLO_D | TLBLO_V - or vaddr, vaddr, t1 b 8f + or vaddr, vaddr, t1 +.set reorder 7: li t2, TLBLO_NONCOHRNT | TLBLO_D | TLBLO_V | TLBLO_G diff --git a/src/os/osint.h b/src/os/osint.h index 9472ae9..75b94ea 100644 --- a/src/os/osint.h +++ b/src/os/osint.h @@ -47,8 +47,6 @@ extern u32 __osProfileOverflowBin; extern __OSEventState __osEventStateTab[OS_NUM_EVENTS]; -//not sure if this should be here -extern s32 osViClock; extern void __osTimerServicesInit(void); extern s32 __osAiDeviceBusy(void); extern int __osDpDeviceBusy(void); diff --git a/src/os/parameters.s b/src/os/parameters.s index ce6ebb5..450af31 100644 --- a/src/os/parameters.s +++ b/src/os/parameters.s @@ -12,5 +12,6 @@ ABS(osCicId, 0x80000310) ABS(osVersion, 0x80000314) ABS(osMemSize, 0x80000318) ABS(osAppNMIBuffer, 0x8000031c) -.fill 0x60 + +.space 0x60 /* padded to 0x60 in the object file */ diff --git a/src/os/rdbsend.c b/src/os/rdbsend.c index c9d7865..0dc111e 100644 --- a/src/os/rdbsend.c +++ b/src/os/rdbsend.c @@ -66,7 +66,7 @@ u32 __osRdbSend(u8* buf, u32 size, u32 type) { } if (needFirst) { - *(rdbPacket*)RDB_BASE_REG = pkt; + *(u32*)RDB_BASE_REG = *(u32*)&pkt; } __osRestoreInt(mask); diff --git a/src/os/seteventmesg.c b/src/os/seteventmesg.c index 50d7ad9..5a00bc9 100644 --- a/src/os/seteventmesg.c +++ b/src/os/seteventmesg.c @@ -13,7 +13,7 @@ void osSetEventMesg(OSEvent event, OSMesgQueue* mq, OSMesg msg) { #ifdef _DEBUG if (event >= OS_NUM_EVENTS) { __osError(ERR_OSSETEVENTMESG, 1, event); - return 0; + return; } #endif diff --git a/src/os/setfpccsr.s b/src/os/setfpccsr.s index f96ab33..f288437 100644 --- a/src/os/setfpccsr.s +++ b/src/os/setfpccsr.s @@ -7,5 +7,4 @@ LEAF(__osSetFpcCsr) STAY2(cfc1 v0, fcr31) STAY2(ctc1 a0, fcr31) jr ra -END(__osSetFpcCsr) -.globl __osSetSR +END(__osSetSR) # @bug: Should be __osGetFpcCsr diff --git a/src/os/setsr.s b/src/os/setsr.s index c2eb1d7..d7d98fa 100644 --- a/src/os/setsr.s +++ b/src/os/setsr.s @@ -5,6 +5,6 @@ .text LEAF(__osSetSR) STAY2(mtc0 a0, C0_SR) - nop + NOP jr ra END(__osSetSR) diff --git a/src/os/setthreadpri.c b/src/os/setthreadpri.c index 91c9c5f..d836323 100644 --- a/src/os/setthreadpri.c +++ b/src/os/setthreadpri.c @@ -8,7 +8,7 @@ void osSetThreadPri(OSThread* t, OSPri pri) { #ifdef _DEBUG if ((pri < OS_PRIORITY_IDLE) || (pri > OS_PRIORITY_MAX)) { __osError(ERR_OSSETTHREADPRI, 1, pri); - return 0; + return; } #endif diff --git a/src/os/settime.c b/src/os/settime.c index 5bcae42..b7d14e3 100644 --- a/src/os/settime.c +++ b/src/os/settime.c @@ -8,7 +8,7 @@ void osSetTime(OSTime time) { #ifdef _DEBUG if (!__osViDevMgr.active) { __osError(ERR_OSSETTIME, 0); - return 0; + return; } #endif diff --git a/src/os/settimer.c b/src/os/settimer.c index d2c5650..1eb7fbe 100644 --- a/src/os/settimer.c +++ b/src/os/settimer.c @@ -20,7 +20,7 @@ int osSetTimer(OSTimer* t, OSTime countdown, OSTime interval, OSMesgQueue* mq, O t->next = NULL; t->prev = NULL; t->interval = interval; - t->value = (countdown == 0) ? interval : countdown; + t->value = (countdown != 0) ? countdown : interval; t->mq = mq; t->msg = msg; diff --git a/src/os/settlbasid.s b/src/os/settlbasid.s index efaed5e..13a17e8 100644 --- a/src/os/settlbasid.s +++ b/src/os/settlbasid.s @@ -1,9 +1,32 @@ +#include "PR/ultraerror.h" #include "PR/R4300.h" #include "sys/asm.h" #include "sys/regdef.h" .text LEAF(osSetTLBASID) +#if defined (_DEBUG) && defined (__sgi) +.set noreorder + bgez a0, 1f + nop + b 2f + nop +1: + li t1, 0xFF +.set noat + slt AT, t1, a0 + beqz AT, 3f + nop +.set at +2: + move a2, a0 + li a0, ERR_OSSETTLBASID + li a1, 1 + j __osError + nop +3: +.set reorder +#endif STAY2(mtc0 a0, C0_ENTRYHI) jr ra END(osSetTLBASID) diff --git a/src/os/setwatchlo.s b/src/os/setwatchlo.s index 2f645a7..91cc58e 100644 --- a/src/os/setwatchlo.s +++ b/src/os/setwatchlo.s @@ -5,6 +5,6 @@ .text LEAF(__osSetWatchLo) STAY2(mtc0 a0, C0_WATCHLO) - nop + NOP jr ra END(__osSetWatchLo) diff --git a/src/os/startthread.c b/src/os/startthread.c index 888d91c..2ce1546 100644 --- a/src/os/startthread.c +++ b/src/os/startthread.c @@ -24,7 +24,7 @@ void osStartThread(OSThread* t) { default: __osError(ERR_OSSTARTTHREAD, 0); __osRestoreInt(saveMask); - return 0; + return; #endif } diff --git a/src/os/stopthread.c b/src/os/stopthread.c index 2f1eae4..9a51652 100644 --- a/src/os/stopthread.c +++ b/src/os/stopthread.c @@ -5,7 +5,7 @@ void osStopThread(OSThread* t) { register u32 saveMask = __osDisableInt(); register u16 state; - state = (t != NULL) ? t->state : OS_STATE_RUNNING; + state = (t == NULL) ? OS_STATE_RUNNING: t->state; switch (state) { case OS_STATE_RUNNING: diff --git a/src/os/syncputchars.c b/src/os/syncputchars.c index 29d8954..d2d5332 100644 --- a/src/os/syncputchars.c +++ b/src/os/syncputchars.c @@ -10,6 +10,7 @@ void __osSyncPutChars(int type, int length, const char* buf) { rdbPacket packet; int i; u32 mask; + packet.type = type; packet.length = length; @@ -17,12 +18,16 @@ void __osSyncPutChars(int type, int length, const char* buf) { packet.buf[i] = buf[i]; } - while (!__osAtomicDec(&__osRdbWriteOK)) {} + while (!__osAtomicDec(&__osRdbWriteOK)) { + ; + } mask = __osDisableInt(); *(vu32*)RDB_BASE_REG = *(vu32*)&packet; - while (!(__osGetCause() & CAUSE_IP6)) {} + while (!(__osGetCause() & CAUSE_IP6)) { + ; + } *(vu32*)RDB_READ_INTR_REG = 0; __osRdbWriteOK++; diff --git a/src/os/thread.c b/src/os/thread.c index 4e5e890..9a4a141 100644 --- a/src/os/thread.c +++ b/src/os/thread.c @@ -11,7 +11,7 @@ void __osDequeueThread(register OSThread** queue, register OSThread* t) { register OSThread* pred; register OSThread* succ; - pred = queue; + pred = (OSThread*)queue; succ = pred->next; while (succ != NULL) { diff --git a/src/os/threadasm.h b/src/os/threadasm.h index c5a73da..dfd91b6 100644 --- a/src/os/threadasm.h +++ b/src/os/threadasm.h @@ -1,7 +1,7 @@ #ifndef _THREADASM_H_ #define _THREADASM_H_ -// Offsets of members in the OSThread struct +/* Offsets of members in the OSThread struct */ #define THREAD_NEXT 0 #define THREAD_PRI 4 #define THREAD_QUEUE 8 @@ -35,7 +35,7 @@ #define THREAD_GP23 208 #define THREAD_GP24 216 #define THREAD_GP25 224 -// k0 and k1 are reserved for the kernel +/* k0 and k1 are reserved for the kernel */ #define THREAD_GP28 232 #define THREAD_GP29 240 #define THREAD_GP30 248 diff --git a/src/os/timerintr.c b/src/os/timerintr.c index ffb3417..46beadf 100644 --- a/src/os/timerintr.c +++ b/src/os/timerintr.c @@ -73,18 +73,15 @@ void __osTimerInterrupt(void) { osSendMesg(t->mq, t->msg, OS_MESG_NOBLOCK); } else { pc = __osRunQueue->context.pc; - prof = __osProfileList; - - while (prof < __osProfileListEnd) { + for (prof = __osProfileList; prof < __osProfileListEnd; prof++) { offset = pc - (u32)prof->text_start; if (offset >= 0) { if ((s32)prof->text_end - (s32)pc > 0) { - prof->histo_base[offset >> 2]++; + (*(u16*)(u32)((offset >> 2) + prof->histo_base))++; goto __ProfDone; } } - prof++; } __osProfileOverflowBin++; @@ -119,15 +116,12 @@ void __osSetTimerIntr(OSTime tim) { OSTime __osInsertTimer(OSTimer* t) { OSTimer* timep; OSTime tim; - u32 savedMask; - savedMask = __osDisableInt(); + u32 savedMask = __osDisableInt(); timep = __osTimerList->next; tim = t->value; - - while (timep != __osTimerList && tim > timep->value) { + for (; timep != __osTimerList && tim > timep->value; timep = timep->next) { tim -= timep->value; - timep = timep->next; } t->value = tim; diff --git a/src/os/unmaptlb.s b/src/os/unmaptlb.s index 280a42e..362fd1e 100644 --- a/src/os/unmaptlb.s +++ b/src/os/unmaptlb.s @@ -1,3 +1,4 @@ +#include "PR/ultraerror.h" #include "PR/R4300.h" #include "sys/asm.h" #include "sys/regdef.h" @@ -5,6 +6,26 @@ .text .set noreorder LEAF(osUnmapTLB) +#if defined (_DEBUG) && defined (__sgi) + bgez a0, 1f + nop + b 2f + nop +1: + li t0, 0x1F +.set noat + slt AT, a0, t0 + bnez AT, 3f + nop +.set at +2: + move a2, a0 + li a0, ERR_OSUNMAPTLB + li a1, 1 + j __osError + nop +3: +#endif mfc0 t0, C0_ENTRYHI mtc0 a0, C0_INX li t1, K0BASE diff --git a/src/rg/free.c b/src/rg/free.c index 3a267e2..c426000 100644 --- a/src/rg/free.c +++ b/src/rg/free.c @@ -1,6 +1,7 @@ #include "PR/region.h" #include "PR/R4300.h" #include "PR/ultraerror.h" +#include "PR/os_internal.h" #include "assert.h" @@ -37,7 +38,6 @@ - // TODO: this comes from a header @@ -51,7 +51,7 @@ void osFree(void* region, void* addr) { assert((region != NULL) && (addr != NULL)); if (((char*)rp + ALIGN(sizeof(OSRegion), rp->r_alignSize)) != rp->r_startBufferAddress) { __osError(ERR_OSFREE_REGION, 1, region); - return 0; + return; } #endif @@ -60,12 +60,12 @@ void osFree(void* region, void* addr) { #ifdef _DEBUG if ((i < 0) || (i >= rp->r_bufferCount)) { __osError(ERR_OSFREE_ADDR, 2, addr, region); - return 0; + return; } if (((unsigned char*)addr - rp->r_startBufferAddress) % (rp->r_bufferSize) != 0) { __osError(ERR_OSFREE_ADDR, 2, addr, region); - return 0; + return; } #endif *(u16*)(&rp->r_startBufferAddress[i * rp->r_bufferSize]) = rp->r_freeList; diff --git a/src/rg/getbufcount.c b/src/rg/getbufcount.c index ea5f7b2..d4deeba 100644 --- a/src/rg/getbufcount.c +++ b/src/rg/getbufcount.c @@ -1,5 +1,6 @@ #include "PR/region.h" #include "PR/ultraerror.h" +#include "PR/os_internal.h" #include "assert.h" @@ -37,7 +38,6 @@ - // TODO: this comes from a header diff --git a/src/rg/getsize.c b/src/rg/getsize.c index 0571d04..2b1fb90 100644 --- a/src/rg/getsize.c +++ b/src/rg/getsize.c @@ -1,5 +1,6 @@ #include "PR/region.h" #include "PR/ultraerror.h" +#include "PR/os_internal.h" #include "assert.h" @@ -36,7 +37,6 @@ - // TODO: this comes from a header diff --git a/src/rg/malloc.c b/src/rg/malloc.c index cd14245..59c0399 100644 --- a/src/rg/malloc.c +++ b/src/rg/malloc.c @@ -1,5 +1,6 @@ #include "PR/region.h" #include "PR/ultraerror.h" +#include "PR/os_internal.h" #include "assert.h" @@ -42,7 +43,6 @@ - // TODO: this comes from a header @@ -56,7 +56,7 @@ void* osMalloc(void* region) { assert(rp != NULL); if (((char*)rp + ALIGN(sizeof(OSRegion), rp->r_alignSize)) != rp->r_startBufferAddress) { __osError(ERR_OSMALLOC, 1, region); - return 0; + return NULL; } #endif diff --git a/src/rg/region.c b/src/rg/region.c index b0c4746..377ec9e 100644 --- a/src/rg/region.c +++ b/src/rg/region.c @@ -1,5 +1,6 @@ #include "PR/region.h" #include "PR/ultraerror.h" +#include "os_internal_error.h" #include "assert.h" @@ -176,7 +177,6 @@ - // TODO: this comes from a header diff --git a/src/rmon/rmonbrk.c b/src/rmon/rmonbrk.c index 1a779c7..b3cf239 100644 --- a/src/rmon/rmonbrk.c +++ b/src/rmon/rmonbrk.c @@ -11,8 +11,6 @@ // TODO: this comes from a header #ident "$Revision: 1.4 $" -static void rmonFindFaultedThreads(void); - #define TMP_BP 0 #define NUM_BREAKPOINTS 16 @@ -37,6 +35,8 @@ static BREAKINFO RCPbreakpoints[NUM_BREAKPOINTS] ALIGNED(8); u8 __rmonRcpAtBreak; +static void rmonFindFaultedThreads(void); + static void SetTempBreakpoint(u32* addr1, u32* addr2) { STUBBED_PRINTF(("Set temp BP at %08x", addr1)); if (addr2 != NULL) { @@ -246,7 +246,7 @@ u32 __rmonGetBranchTarget(int method, int thread, char* addr) { case 17: /* BGEZAL */ case 18: /* BLTZALL */ case 19: /* BGEZALL */ - return (u32)addr + ((inst << 0x10) >> 0xE) + 4; + return (((inst << 0x10) >> 0xE) + addr + 4); } break; case 2: /* J */ @@ -256,13 +256,13 @@ u32 __rmonGetBranchTarget(int method, int thread, char* addr) { case 5: /* BNE */ case 20: /* BEQL */ case 21: /* BNEL */ - return (u32)addr + ((inst << 0x10) >> 0xE) + 4; + return (((inst << 0x10) >> 0xE) + addr + 4); case 6: /* BLEZ */ case 7: /* BGTZ */ case 22: /* BLEZL */ case 23: /* BGTZL */ if (((inst >> 16) & 0x1F) == 0) { - return (u32)addr + ((inst << 0x10) >> 0xE) + 4; + return (((inst << 0x10) >> 0xE) + addr + 4); } break; case 16: /* COP0 */ @@ -275,7 +275,7 @@ u32 __rmonGetBranchTarget(int method, int thread, char* addr) { case 1: /* BCzT */ case 2: /* BCzFL */ case 3: /* BCzTL */ - return (u32)addr + ((inst << 0x10) >> 0xE) + 4; + return (((inst << 0x10) >> 0xE) + addr + 4); } } break; @@ -310,10 +310,10 @@ int __rmonSetSingleStep(int thread, u32* instptr) { if ((branchTarget & 3) != 0) { /* no branch target, set breakpoint at next pc */ SetTempBreakpoint(instptr + 1, NULL); - } else if ((u32*)branchTarget == instptr) { + } else if (branchTarget == (u32)instptr) { /* branch target is this instruction, can't single step here */ return FALSE; - } else if (IsJump(*instptr) || (u32*)branchTarget == instptr + 2) { + } else if (IsJump(*instptr) || branchTarget == (u32)(instptr + 2)) { /* unconditional branch, set at branch target */ SetTempBreakpoint((u32*)branchTarget, NULL); } else { diff --git a/src/rmon/rmonint.h b/src/rmon/rmonint.h index 350b779..59ad736 100644 --- a/src/rmon/rmonint.h +++ b/src/rmon/rmonint.h @@ -92,7 +92,7 @@ u32 __rmonReadWordAt(u32* addr); void __rmonMemcpy(u8* dest, u8* srce, u32 count); void __rmonCopyWords(u32* dest, u32* srce, u32 count); -extern u8 __rmonUtilityBuffer[256]; +extern u8 __rmonUtilityBuffer[]; /* rmonsio */ diff --git a/src/rmon/rmonmain.c b/src/rmon/rmonmain.c index 7a63cc8..8cb0c1b 100644 --- a/src/rmon/rmonmain.c +++ b/src/rmon/rmonmain.c @@ -91,10 +91,8 @@ void rmonMain(void) { __rmonInit(); __rmonActive = TRUE; - state = 0; - newChars = 0; - inPointer = (void*)&inbuffer; + state = 0, newChars = 0, inPointer = (void*)&inbuffer; for (;;) { OSMesg work; @@ -116,11 +114,11 @@ void rmonMain(void) { } if (somethingToDo & 0x10) { somethingToDo; - somethingToDo &= 0xEF; + somethingToDo &= (u8)~0x10; } if (somethingToDo & 0x20) { somethingToDo; - somethingToDo &= 0xDF; + somethingToDo &= (u8)~0x20; } } } diff --git a/src/rmon/rmonmem.c b/src/rmon/rmonmem.c index 709c4a1..0880fe9 100644 --- a/src/rmon/rmonmem.c +++ b/src/rmon/rmonmem.c @@ -70,10 +70,9 @@ int __rmonReadMem(KKHeader* req) { } if (req->method == RMON_RSP) { - if ((request->addr < SP_IMEM_START || (request->addr + request->nbytes) > SP_IMEM_END)) { - if ((request->addr < SP_DMEM_START || (request->addr + request->nbytes) > SP_DMEM_END)) { + if (!((request->addr < SP_IMEM_START || (request->addr + request->nbytes) > SP_IMEM_END) ? FALSE : TRUE) && + !((request->addr < SP_DMEM_START || (request->addr + request->nbytes) > SP_DMEM_END) ? FALSE : TRUE)) { return TV_ERROR_INVALID_ADDRESS; - } } } else if (osVirtualToPhysical((void*)request->addr) == (u32)-1) { return TV_ERROR_INVALID_ADDRESS; @@ -110,8 +109,8 @@ int __rmonWriteMem(KKHeader* req) { return TV_ERROR_INVALID_CAPABILITY; } - if ((request->writeHeader.addr >= SP_DMEM_START && - (request->writeHeader.addr + request->writeHeader.nbytes) < 0x05000000)) { + if (((request->writeHeader.addr < SP_DMEM_START || + (request->writeHeader.addr + request->writeHeader.nbytes) > 0x04FFFFFF) ? FALSE : TRUE)) { int align; u32 word; diff --git a/src/rmon/rmonmisc.c b/src/rmon/rmonmisc.c index 4bfe176..aec32d6 100644 --- a/src/rmon/rmonmisc.c +++ b/src/rmon/rmonmisc.c @@ -10,8 +10,6 @@ // TODO: this comes from a header #ident "$Revision: 1.4 $" -OSMesgQueue __rmonMQ ALIGNED(8); - int __rmonSetFault(KKHeader* req) { KKFaultRequest* request = (KKFaultRequest*)req; KKObjectEvent reply; @@ -26,6 +24,7 @@ int __rmonSetFault(KKHeader* req) { return TV_ERROR_NO_ERROR; } +OSMesgQueue __rmonMQ ALIGNED(8); static OSThread rmonIOThread ALIGNED(8); static OSMesg rmonMsgs[8] ALIGNED(8); static u64 rmonIOStack[2048] ALIGNED(16); diff --git a/src/voice/voicecheckresult.c b/src/voice/voicecheckresult.c index e49f271..5318652 100644 --- a/src/voice/voicecheckresult.c +++ b/src/voice/voicecheckresult.c @@ -1,21 +1,26 @@ #include "PR/os_internal.h" #include "io/controller.h" #include "PR/os_voice.h" +#include "voiceinternal.h" s32 __osVoiceCheckResult(OSVoiceHandle* hd, u8* stat) { s32 ret; u8 buf[2]; - if (ret = __osVoiceGetStatus(hd->__mq, hd->__channel, stat), ret == 0) { + ret = __osVoiceGetStatus(hd->__mq, hd->__channel, stat); + if (ret == 0) { if (*stat & 1) { ret = CONT_ERR_VOICE_NO_RESPONSE; - } else if (ret = __osVoiceContRead2(hd->__mq, hd->__channel, 0, buf), ret == 0) { - hd->cmd_status = buf[0] & 7; + } else { + ret = __osVoiceContRead2(hd->__mq, hd->__channel, 0, buf); + if (ret == 0) { + hd->cmd_status = buf[0] & 7; - if (buf[0] & 0x40) { - ret = CONT_ERR_VOICE_NO_RESPONSE; - } else { - ret = buf[1] << 8; + if (buf[0] & 0x40) { + ret = CONT_ERR_VOICE_NO_RESPONSE; + } else { + ret = buf[1] << 8; + } } } } diff --git a/src/voice/voicecleardictionary.c b/src/voice/voicecleardictionary.c index 267724f..6ed1464 100644 --- a/src/voice/voicecleardictionary.c +++ b/src/voice/voicecleardictionary.c @@ -1,13 +1,14 @@ #include "PR/os_internal.h" #include "io/controller.h" #include "PR/os_voice.h" +#include "voiceinternal.h" s32 osVoiceClearDictionary(OSVoiceHandle* hd, u8 words) { s32 ret; u8 stat; u8 buf[4]; - ret = (__osVoiceGetStatus(hd->__mq, hd->__channel, &stat)); + ret = __osVoiceGetStatus(hd->__mq, hd->__channel, &stat); if (ret != 0) { return ret; } else if (stat & 2) { diff --git a/src/voice/voicecontread2.c b/src/voice/voicecontread2.c index c16a198..3808c7b 100644 --- a/src/voice/voicecontread2.c +++ b/src/voice/voicecontread2.c @@ -3,6 +3,7 @@ #include "PR/os_voice.h" #include "voiceinternal.h" #include "io/controller_voice.h" +#include "io/siint.h" #define READ2FORMAT(p) ((__OSVoiceRead2Format*)(ptr)) @@ -21,13 +22,11 @@ s32 __osVoiceContRead2(OSMesgQueue* mq, int channel, u16 address, u8* buffer) { ptr = (u8*)&__osPfsPifRam.ramarray; - if ((__osContLastCmd != CONT_CMD_READ2_VOICE) || (__osPfsLastChannel != channel)) { + if ((__osContLastCmd != CONT_CMD_READ2_VOICE) || ((u32)__osPfsLastChannel != channel)) { __osContLastCmd = CONT_CMD_READ2_VOICE; __osPfsLastChannel = channel; - for (i = 0; i < channel; i++) { - *ptr++ = 0; - } + for (i = 0; i < channel; i++) { *ptr++ = 0; } __osPfsPifRam.pifstatus = CONT_CMD_EXE; @@ -53,7 +52,7 @@ s32 __osVoiceContRead2(OSMesgQueue* mq, int channel, u16 address, u8* buffer) { ret = CHNL_ERR(*READ2FORMAT(ptr)); if (ret == 0) { - if (__osVoiceContDataCrc(&READ2FORMAT(ptr)->data, ARRLEN(READ2FORMAT(ptr)->data)) != + if (__osVoiceContDataCrc(READ2FORMAT(ptr)->data, ARRLEN(READ2FORMAT(ptr)->data)) != READ2FORMAT(ptr)->datacrc) { ret = __osVoiceGetStatus(mq, channel, &status); if (ret != 0) { diff --git a/src/voice/voicecontread36.c b/src/voice/voicecontread36.c index c4d3b7a..6c1677e 100644 --- a/src/voice/voicecontread36.c +++ b/src/voice/voicecontread36.c @@ -3,10 +3,11 @@ #include "PR/os_voice.h" #include "voiceinternal.h" #include "io/controller_voice.h" +#include "io/siint.h" #define READ36FORMAT(p) ((__OSVoiceRead36Format*)(ptr)) -s32 __osVoiceContRead36(OSMesgQueue* mq, s32 channel, u16 address, u8* buffer) { +s32 __osVoiceContRead36(OSMesgQueue* mq, int channel, u16 address, u8* buffer) { s32 ret = 0; u8 status; s32 i; @@ -19,13 +20,11 @@ s32 __osVoiceContRead36(OSMesgQueue* mq, s32 channel, u16 address, u8* buffer) { ptr = (u8*)&__osPfsPifRam.ramarray; - if ((__osContLastCmd != CONT_CMD_READ36_VOICE) || (__osPfsLastChannel != channel)) { + if ((__osContLastCmd != CONT_CMD_READ36_VOICE) || ((u32)__osPfsLastChannel != channel)) { __osContLastCmd = CONT_CMD_READ36_VOICE; __osPfsLastChannel = channel; - for (i = 0; i < channel; i++) { - *ptr++ = 0; - } + for (i = 0; i < channel; i++) { *ptr++ = 0; } __osPfsPifRam.pifstatus = CONT_CMD_EXE; @@ -51,7 +50,7 @@ s32 __osVoiceContRead36(OSMesgQueue* mq, s32 channel, u16 address, u8* buffer) { ret = CHNL_ERR(*READ36FORMAT(ptr)); if (ret == 0) { - if (__osVoiceContDataCrc(&READ36FORMAT(ptr)->data, ARRLEN(READ36FORMAT(ptr)->data)) != + if (__osVoiceContDataCrc(READ36FORMAT(ptr)->data, ARRLEN(READ36FORMAT(ptr)->data)) != READ36FORMAT(ptr)->datacrc) { ret = __osVoiceGetStatus(mq, channel, &status); if (ret != 0) { @@ -60,7 +59,7 @@ s32 __osVoiceContRead36(OSMesgQueue* mq, s32 channel, u16 address, u8* buffer) { ret = CONT_ERR_CONTRFAIL; } } else { - bcopy(&READ36FORMAT(ptr)->data, buffer, ARRLEN(READ36FORMAT(ptr)->data)); + bcopy(READ36FORMAT(ptr)->data, buffer, ARRLEN(READ36FORMAT(ptr)->data)); } } else { ret = CONT_ERR_NO_CONTROLLER; diff --git a/src/voice/voicecontrolgain.c b/src/voice/voicecontrolgain.c index 9c323f0..95e9bb2 100644 --- a/src/voice/voicecontrolgain.c +++ b/src/voice/voicecontrolgain.c @@ -1,8 +1,7 @@ #include "PR/os_internal.h" #include "io/controller.h" #include "PR/os_voice.h" - -s32 __osVoiceSetADConverter(OSMesgQueue*, int ch, u8 cmd); +#include "voiceinternal.h" s32 osVoiceControlGain(OSVoiceHandle* hd, s32 analog, s32 digital) { s32 ret; diff --git a/src/voice/voicecontwrite20.c b/src/voice/voicecontwrite20.c index c94df0d..53b78eb 100644 --- a/src/voice/voicecontwrite20.c +++ b/src/voice/voicecontwrite20.c @@ -3,6 +3,7 @@ #include "PR/os_voice.h" #include "voiceinternal.h" #include "io/controller_voice.h" +#include "io/siint.h" #define WRITE20FORMAT(p) ((__OSVoiceWrite20Format*)(ptr)) @@ -22,13 +23,11 @@ s32 __osVoiceContWrite20(OSMesgQueue* mq, int channel, u16 address, u8* buffer) ptr = (u8*)&__osPfsPifRam; - if ((__osContLastCmd != CONT_CMD_WRITE20_VOICE) || (__osPfsLastChannel != channel)) { + if ((__osContLastCmd != CONT_CMD_WRITE20_VOICE) || ((u32)__osPfsLastChannel != channel)) { __osContLastCmd = CONT_CMD_WRITE20_VOICE; __osPfsLastChannel = channel; - for (i = 0; i < channel; i++) { - *ptr++ = 0; - } + for (i = 0; i < channel; i++) { *ptr++ = 0; } __osPfsPifRam.pifstatus = CONT_CMD_EXE; diff --git a/src/voice/voicecontwrite4.c b/src/voice/voicecontwrite4.c index 4bae207..949952c 100644 --- a/src/voice/voicecontwrite4.c +++ b/src/voice/voicecontwrite4.c @@ -3,10 +3,11 @@ #include "PR/os_voice.h" #include "voiceinternal.h" #include "io/controller_voice.h" +#include "io/siint.h" #define WRITE4FORMAT(p) ((__OSVoiceWrite4Format*)(ptr)) -s32 __osVoiceContWrite4(OSMesgQueue* mq, int channel, u16 address, u8 dst[4]) { +s32 __osVoiceContWrite4(OSMesgQueue* mq, int channel, u16 address, u8* buffer) { s32 ret = 0; u8 status; int i; @@ -20,13 +21,11 @@ s32 __osVoiceContWrite4(OSMesgQueue* mq, int channel, u16 address, u8 dst[4]) { ptr = (u8*)&__osPfsPifRam; - if ((__osContLastCmd != CONT_CMD_WRITE4_VOICE) || (__osPfsLastChannel != channel)) { + if ((__osContLastCmd != CONT_CMD_WRITE4_VOICE) || ((u32)__osPfsLastChannel != channel)) { __osContLastCmd = CONT_CMD_WRITE4_VOICE; __osPfsLastChannel = channel; - for (i = 0; i < channel; i++) { - *ptr++ = 0; - } + for (i = 0; i < channel; i++) { *ptr++ = 0; } __osPfsPifRam.pifstatus = CONT_CMD_EXE; @@ -44,10 +43,10 @@ s32 __osVoiceContWrite4(OSMesgQueue* mq, int channel, u16 address, u8 dst[4]) { WRITE4FORMAT(ptr)->addrh = address >> 3; WRITE4FORMAT(ptr)->addrl = (address << 5) | __osContAddressCrc(address); - bcopy(dst, &WRITE4FORMAT(ptr)->data, 4); + bcopy(buffer, &WRITE4FORMAT(ptr)->data, 4); ret = __osSiRawStartDma(OS_WRITE, &__osPfsPifRam); - crc = __osVoiceContDataCrc(dst, 4); + crc = __osVoiceContDataCrc(buffer, 4); osRecvMesg(mq, NULL, OS_MESG_BLOCK); ret = __osSiRawStartDma(OS_READ, &__osPfsPifRam); osRecvMesg(mq, NULL, OS_MESG_BLOCK); diff --git a/src/voice/voicecrc.c b/src/voice/voicecrc.c index 911f44c..56b4838 100644 --- a/src/voice/voicecrc.c +++ b/src/voice/voicecrc.c @@ -10,7 +10,7 @@ u8 __osVoiceContDataCrc(u8* data, u32 length) { u32 i; u32 j; - for (i = length; i != 0; data++, i--) { + for (i = length; i != 0; i--) { // Loop over each j in the i starting with most significant for (j = (1 << (VOICE_CRC_LENGTH - 1)); j != 0; j >>= 1) { temp <<= 1; @@ -25,6 +25,7 @@ u8 __osVoiceContDataCrc(u8* data, u32 length) { temp ^= VOICE_CRC_GENERATOR; } } + data++; } // Act like a i of zeros is appended to data do { diff --git a/src/voice/voicegetreaddata.c b/src/voice/voicegetreaddata.c index 5627846..d2f5063 100644 --- a/src/voice/voicegetreaddata.c +++ b/src/voice/voicegetreaddata.c @@ -29,7 +29,7 @@ s32 osVoiceGetReadData(OSVoiceHandle* hd, OSVoiceData* micdata) { case 2: hd->__mode = 2; - ERRCK(__osVoiceGetStatus(hd->__mq, hd->__channel, &stat)) + ERRCK(__osVoiceGetStatus(hd->__mq, hd->__channel, &stat)); if (stat & 2) { return CONT_ERR_VOICE_NO_RESPONSE; diff --git a/src/voice/voicegetstatus.c b/src/voice/voicegetstatus.c index 8d33f31..966e539 100644 --- a/src/voice/voicegetstatus.c +++ b/src/voice/voicegetstatus.c @@ -2,6 +2,7 @@ #include "io/controller.h" #include "PR/os_voice.h" #include "voiceinternal.h" +#include "io/siint.h" s32 __osVoiceGetStatus(OSMesgQueue* mq, s32 port, u8* status) { __OSContRequesFormatShort header; @@ -37,7 +38,7 @@ s32 __osVoiceGetStatus(OSMesgQueue* mq, s32 port, u8* status) { header = *((__OSContRequesFormatShort*)ptr); - ret = CHNL_ERR(header); + ret = (u8)CHNL_ERR(header); *status = header.status; if (ret == 0) { diff --git a/src/voice/voiceinit.c b/src/voice/voiceinit.c index adb39a4..5074a84 100644 --- a/src/voice/voiceinit.c +++ b/src/voice/voiceinit.c @@ -1,6 +1,7 @@ #include "PR/os_internal.h" #include "io/controller.h" #include "PR/os_voice.h" +#include "voiceinternal.h" s32 osVoiceInit(OSMesgQueue* mq, OSVoiceHandle* handle, int channel) { s32 ret; diff --git a/src/voice/voiceinternal.h b/src/voice/voiceinternal.h index 899c150..8dc6299 100644 --- a/src/voice/voiceinternal.h +++ b/src/voice/voiceinternal.h @@ -4,7 +4,15 @@ // Various prototypes and externs that don't show up in os_voice.h extern s32 __osPfsLastChannel; -u8 __osVoiceContDataCrc(u8* data, u32 length); +s32 __osContChannelReset(OSMesgQueue* mq, int channel); + s32 __osVoiceCheckResult(OSVoiceHandle* hd, u8* stat); +s32 __osVoiceContRead2(OSMesgQueue* mq, int channel, u16 address, u8* buffer); +s32 __osVoiceContRead36(OSMesgQueue* mq, int channel, u16 address, u8* buffer); +s32 __osVoiceContWrite4(OSMesgQueue* mq, int channel, u16 address, u8* buffer); +s32 __osVoiceContWrite20(OSMesgQueue* mq, int channel, u16 address, u8* buffer); +u8 __osVoiceContDataCrc(u8* data, u32 length); +s32 __osVoiceGetStatus(OSMesgQueue* mq, s32 port, u8* status); +s32 __osVoiceSetADConverter(OSMesgQueue*, s32 channel, u8 cmd); #endif diff --git a/src/voice/voicemaskdictionary.c b/src/voice/voicemaskdictionary.c index 913c4df..39db7ac 100644 --- a/src/voice/voicemaskdictionary.c +++ b/src/voice/voicemaskdictionary.c @@ -36,7 +36,7 @@ s32 osVoiceMaskDictionary(OSVoiceHandle* hd, u8* pattern, int size) { buf[ARRLEN(buf) - 1] = 0; } - ret = __osVoiceContWrite20(hd->__mq, hd->__channel, 0, &buf); + ret = __osVoiceContWrite20(hd->__mq, hd->__channel, 0, (u8*)&buf); if (ret == 0) { ret = __osVoiceCheckResult(hd, &stat); if (ret & 0xFF00) { diff --git a/src/voice/voicesetadconverter.c b/src/voice/voicesetadconverter.c index 45512af..7dcbaf0 100644 --- a/src/voice/voicesetadconverter.c +++ b/src/voice/voicesetadconverter.c @@ -3,6 +3,7 @@ #include "PR/os_voice.h" #include "voiceinternal.h" #include "io/controller_voice.h" +#include "io/siint.h" #define SWRITEFORMAT(p) ((__OSVoiceSWriteFormat*)(p)) @@ -23,9 +24,7 @@ s32 __osVoiceSetADConverter(OSMesgQueue* mq, s32 channel, u8 data) { __osContLastCmd = CONT_CMD_SWRITE_VOICE; __osPfsLastChannel = channel; - for (i = 0; i < channel; i++) { - *ptr++ = 0; - } + for (i = 0; i < channel; i++) { *ptr++ = 0; } __osPfsPifRam.pifstatus = CONT_CMD_EXE; diff --git a/src/voice/voicesetword.c b/src/voice/voicesetword.c index 59effcb..7969fae 100644 --- a/src/voice/voicesetword.c +++ b/src/voice/voicesetword.c @@ -1,6 +1,7 @@ #include "PR/os_internal.h" #include "io/controller.h" #include "PR/os_voice.h" +#include "voiceinternal.h" s32 osVoiceSetWord(OSVoiceHandle* hd, u8* word) { s32 j; diff --git a/tools/ar.py b/tools/ar.py new file mode 100755 index 0000000..0f3e574 --- /dev/null +++ b/tools/ar.py @@ -0,0 +1,476 @@ +#!/usr/bin/env python3 +# +# ar +# + +from genericpath import isfile +import os, struct, time +from dataclasses import dataclass +from libelf import ElfFile, SB_GLOBAL, SHN_UND + +class Archive: + @dataclass + class ArchiveFileRecord: + """ + AR file headers + data + + Offset Length Name Format + 0 16 File identifier ASCII + 16 12 File modification timestamp (in seconds) Decimal + 28 6 Owner ID Decimal + 34 6 Group ID Decimal + 40 8 File mode (type and permission) Octal + 48 10 File size in bytes Decimal + 58 2 Ending characters 0x60 0x0A + """ + name : str + time : int + uid : int + gid : int + mode : int + size : int + data : bytes + + def __init__(self): + self.files = [] # List[ArchiveFileRecord] + self.armap_entries = None + self.time = int(time.time()) + + def add_data(self, name, time, uid, gid, mode, size, data): + self.files.append(Archive.ArchiveFileRecord(name, time, uid, gid, mode, size, data)) + + def add_file(self, file_path): + if not os.path.isfile(file_path): + print(f"Error: No file named {file_path}") + return + + st = os.stat(file_path) + name = os.path.basename(file_path) + time = int(st.st_mtime) + uid = st.st_uid + gid = st.st_gid + mode = st.st_mode + size = st.st_size + + data = None + with open(file_path, "rb") as infile: + data = bytes(infile.read()) + + self.add_data(name, time, uid, gid, mode, size, data) + + def add_ar(self, file_path): + for file in Archive.from_file(file_path).files: + self.add_data(file) + + def build_image(self): + def add_bin(ba, bin): + for b in bin: + ba.append(b) + + def add_str(ba, s, pad_to_len=-1): + if pad_to_len != -1: + s = f"{s:{pad_to_len}}" + s = s.encode("ASCII") + for c in s: + ba.append(c) + + def add_pad(ba): + if len(ba) % 2 != 0: + ba.append(0) + + def add_ar_hdr(ba, name, time, uid, gid, mode, length): + add_str(ba, name, 16) + add_str(ba, str(time), 12) + add_str(ba, str(uid), 6) + add_str(ba, str(gid), 6) + add_str(ba, oct(mode)[2:], 8) + add_str(ba, str(length), 10) + add_str(ba, "`\n") + + b = bytearray() + + # MAGIC + + add_str(b, "!\n") + + # ARMAP + + armap_data= bytearray() + + armap_entries_files = [] + + num_entries = 0 + armap_size = 4 + armap_string_data = bytearray() + for i,file in enumerate(self.files): + elf = ElfFile(file.data) + if elf.symtab is not None: + for sym in elf.symtab.symbol_entries: + if sym.st_shndx != SHN_UND and sym.bind == SB_GLOBAL: + num_entries += 1 + armap_size += 4 + len(sym.name) + 1 + armap_string_data.extend(sym.name.encode("latin1") + b'\0') + armap_entries_files.append(i) + + armap_data.extend(struct.pack(">I", num_entries)) + armap_data.extend([0] * 4 * num_entries) # defer writing file positions until files are emplaced later + armap_data.extend(armap_string_data) + + if len(armap_data) % 4 != 0: + alsiz = (len(armap_data) + 3) & ~3 + armap_data.extend([0] * (alsiz - len(armap_data))) + + current_time = int(time.time()) + + add_ar_hdr(b, "/", current_time, 0, 0, 0, len(armap_data)) + armap_offsets_start = len(b) + 4 + add_bin(b, armap_data) + add_pad(b) + + # LONG STRINGS + + long_strings = "" + + file_names = [] + flag = False + for _,file in enumerate(self.files): + if len(file.name) >= 16: + fname = f"{file.name}/\n" + ind = len(long_strings) + long_strings += fname + else: + fname = f"{file.name}/" + ind = None + file_names.append((fname, ind)) + + # Weird hack + if len(long_strings) != 0 and not flag: + flag = True + long_strings += "/\n" + + long_strings += "/" + + add_ar_hdr(b, "//", current_time, 0, 0, 0, len(long_strings)) + add_str(b, long_strings) + add_pad(b) + + # FILES + + armap_pos = 0 + for i,(file,(fname,ind)) in enumerate(zip(self.files, file_names)): + file_pos = len(b) + add_ar_hdr(b, f"/{ind}" if ind is not None else fname, file.time, file.uid, file.gid, file.mode, file.size) + add_bin(b, file.data) + add_pad(b) + + # Patch the armap with file locations + while armap_pos < len(armap_entries_files) and armap_entries_files[armap_pos] == i: + b[armap_offsets_start+armap_pos*4:armap_offsets_start+armap_pos*4+4] = struct.pack(">I", file_pos) + armap_pos += 1 + + return b + + def write(self, file_path): + ar = self.build_image() + with open(file_path, "wb") as outfile: + outfile.write(ar) + + @staticmethod + def from_image(ar_data): + long_strings = None + ar = Archive() + + assert ar_data[:8].decode("ASCII") == "!\n" , "Not an archive file? Bad file magic value" + + i = 8 + while i < len(ar_data): + file_name = ar_data[i:][ 0:][:16].decode("ASCII").strip() + file_time = int(ar_data[i:][16:][:12].decode("ASCII").strip()) + file_uid = int(ar_data[i:][28:][: 6].decode("ASCII").strip()) + file_gid = int(ar_data[i:][34:][: 6].decode("ASCII").strip()) + file_mode = int(ar_data[i:][40:][: 8].decode("ASCII").strip(), 8) + file_size = int(ar_data[i:][48:][:10].decode("ASCII").strip()) + end = ar_data[i:][58:][:2].decode("ASCII") + assert end == "`\n" + + data = ar_data[i:][60:][:file_size] + assert len(data) == file_size + + if file_name == '/': + """ + "armap" + The special filename "/" denotes that this entry contains a symbol lookup table used by some libraries + to speed up file access. + The symbol table is comprised of three contiguous parts: + - A 32-bit Big Endian integer recording the number of symbol entries. + - A list of 32-bit Big Endian integers for each symbol entry, recording the position within the + archive of the header of the file containing the symbol. + - A list of null-terminated strings, the symbol names for each symbol entry. + + We rebuild this from scratch on write out. + """ + ar.time = file_time + assert file_uid == 0 + assert file_gid == 0 + assert file_mode == 0 + + # Code to interpret the armap, currently unused + """ + armap_n_syms = struct.unpack(">I", data[0:4])[0] + offsets = [i[0] for i in struct.iter_unpack(">I", data[4:4+4*armap_n_syms])] + + strings = [] + ofs = 4 + 4 * armap_n_syms + for _ in range(armap_n_syms): + to = data.find(b'\0', ofs) + assert to != -1 + string = data[ofs:to].decode('latin1') + strings.append(string) + ofs += len(string) + 1 + assert all([b == 0 for b in data[ofs:]]) + assert len(strings) == len(offsets) + + ar.armap_entries = list(zip(offsets, strings)) + """ + elif file_name == '//': + """ + Long string table. Strings larger than 16 are placed here and referenced from the header by /. + + We rebuild this from scratch on write out. + """ + assert file_time == ar.time + assert file_uid == 0 + assert file_gid == 0 + assert file_mode == 0 + long_strings = data.decode("ASCII") + else: + """ + Normal files. + """ + if file_name.startswith("/"): + assert long_strings is not None + # Fetch the name from the long string table + file_name = long_strings[int(file_name[1:]):].split("\n")[0] + # Add file + ar.add_data(file_name[:-1], file_time, file_uid, file_gid, file_mode, file_size, data) + + if file_size % 2 != 0: + file_size += 1 + i += 60 + file_size + + return ar + + @staticmethod + def from_file(file_path): + ar_file = None + with open(file_path, "rb") as infile: + ar_file = infile.read() + + return Archive.from_image(ar_file) + +def ar_usage(progname): + print(f"Usage: {progname} [-]{{dmpqrstx}}[abcDfilMNoOPsSTuvV] [member-name] [count] archive-file file...") + print(f" commands:") + print(f" d - delete file(s) from the archive") + print(f" m[ab] - move file(s) in the archive") + print(f" p - print file(s) found in the archive") + print(f" q[f] - quick append file(s) to the archive") + print(f" r[ab][f][u] - replace existing or insert new file(s) into the archive") + print(f" s - act as ranlib") + print(f" t[O][v] - display contents of the archive") + print(f" x[o] - extract file(s) from the archive") + print(f" command specific modifiers:") + print(f" [a] - put file(s) after [member-name]") + print(f" [b] - put file(s) before [member-name] (same as [i])") + print(f" [D] - use zero for timestamps and uids/gids (default)") + print(f" [U] - use actual timestamps and uids/gids") + print(f" [N] - use instance [count] of name") + print(f" [f] - truncate inserted file names") + print(f" [P] - use full path names when matching") + print(f" [o] - preserve original dates") + print(f" [O] - display offsets of files in the archive") + print(f" [u] - only replace files that are newer than current archive contents") + print(f" generic modifiers:") + print(f" [c] - do not warn if the library had to be created") + print(f" [s] - create an archive index (cf. ranlib)") + print(f" [S] - do not build a symbol table") + print(f" [v] - be verbose") + print(f" [V] - display the version number") + print(f" @ - read options from ") + print(f" --output=DIRNAME - specify the output directory for extraction operations") + return 1 + +def ar(argv): + if len(argv) < 2: + return ar_usage(argv[0]) + + progname = argv[0] + create_ok = False + make_ar_idx = False + no_symtab = False + verbose = False + + def verbose_print(msg): + if verbose: + print(msg) + + def dcmd(modifiers, args): + if modifiers != "": + print(f"{progname}: bad modifiers -- '{modifiers}'") + return ar_usage(progname) + + ar_file = args[0] + o_files = args[1:] + + ar = Archive.from_file(ar_file) + for file in ar.files: + if file.name in o_files: + ar.files.remove(file) + ar.write(ar_file) + return 0 + + def mcmd(modifiers, args): + print("") + return 1 + + def pcmd(modifiers, args): + if modifiers != "": + print(f"{progname}: bad modifiers -- '{modifiers}'") + return ar_usage(progname) + if len(args) != 1: + print(f"{progname} p: bad args") + return ar_usage(progname) + + ar_file = args[0] + + ar = Archive.from_file(ar_file) + for file in ar.files: + print(file.data) + return 0 + + def qcmd(modifiers, args): + print("") + return 1 + + def rcmd(modifiers, args): + if modifiers !="": + print("") + if len(args) < 2: + print(f"{progname} r: bad args") + return ar_usage(progname) + + ar_file = args[0] + o_files = args[1:] + + ar = Archive() + if os.path.isfile(ar_file): + ar = Archive.from_file(ar_file) + else: + if not create_ok: + print(f"Warning: Created file {ar_file}") + ar = Archive() + + for o_file in o_files: + ar.add_file(o_file) + + ar.write(ar_file) + + def scmd(modifiers, args): + print("") + return 1 + + def tcmd(modifiers, args): + # TODO modifiers + + if len(args) != 1: + print(f"{progname} t: bad args") + return ar_usage(progname) + + ar_file = args[0] + + ar = Archive.from_file(ar_file) + for file in ar.files: + print(file.name) + return 0 + + def xcmd(modifiers, args): + if modifiers not in ('', 'o'): + print(f"{progname}: bad modifiers -- '{modifiers}'") + return ar_usage(progname) + if len(args) not in (1, 3) or not (args[0].startswith("--output") or args[1].startswith("--output")): + print(f"{progname} t: bad args") + return ar_usage(progname) + + original_times = modifiers == 'o' + + if args[0].startswith("--output"): + out_dir = args[1] + ar_file = args[2] + elif args[1].startswith("--output"): + out_dir = args[2] + ar_file = args[0] + else: + ar_file = args[0] + out_dir = "" + + # Create dir if not exists + if not os.path.exists(out_dir): + os.makedirs(out_dir, exist_ok=True) + + if not os.path.isdir(out_dir): + print(f"Output directory {out_dir} is a file") + return ar_usage(progname) + + # Extract files to destination + ar = Archive.from_file(ar_file) + for file in ar.files: + opath =os.path.join(out_dir, file.name) + with open(opath, "wb") as ofile: + ofile.write(file.data) + + if not original_times: + t = time.time() + os.utime(opath, (t, t)) + # TODO patch with original dates etc. + + return 0 + + argtbl = { + 'd': dcmd, # delete file(s) from the archive + 'm': mcmd, # [ab] move file(s) in the archive + 'p': pcmd, # print file(s) found in the archive + 'q': qcmd, # [f] quick append file(s) to the archive + 'r': rcmd, # [ab][f][u] replace existing or insert new file(s) into the archive + 's': scmd, # act as ranlib + 't': tcmd, # [O][v] display contents of the archive + 'x': xcmd, # [o] extract file(s) from the archive + } + + arg1 = argv[1] + if arg1[0] == '-': + arg1 = arg1[1:] + cmd = arg1[0] + + if cmd not in argtbl: + print(f"{progname}: invalid option -- '{cmd}'") + return ar_usage(argv[0]) + + modifiers = arg1[1:] + + create_ok = "c" in modifiers + modifiers = modifiers.replace("c","") + make_ar_idx = "s" in modifiers + modifiers = modifiers.replace("s","") + no_symtab = "S" in modifiers + modifiers = modifiers.replace("S","") + verbose = "v" in modifiers + modifiers = modifiers.replace("v","") + + if "V" in modifiers: + print(f"{progname} v1.0") + + return argtbl[cmd](modifiers, argv[2:]) + +if __name__ == '__main__': + import sys + sys.exit(ar(sys.argv)) diff --git a/tools/libelf.py b/tools/libelf.py index 0c9cc3a..11c4c03 100755 --- a/tools/libelf.py +++ b/tools/libelf.py @@ -1102,9 +1102,9 @@ class ElfFile: if s.sh_type == SHT_SYMTAB: assert not symtab , "Found more than one symtab section?" symtab = s - assert symtab is not None , "Could not find symtab" self.symtab = symtab - self.symtab.late_init() + if self.symtab is not None: + self.symtab.late_init() # Late Init sections for s in self.sections: @@ -1147,11 +1147,12 @@ if __name__ == "__main__": for i,s in enumerate(elf_file.sections,0): print(f" [{i:2}] {s}") # Symbols Info Test - print("") - print(f"Symbol table '{elf_file.symtab.name}' contains {len(elf_file.symtab.symbol_entries)} entries") - print(" Num: Value Size Type Bind Vis Ndx Name") - for i,sym in enumerate(elf_file.symtab.symbol_entries,0): - print(f"{i:6}: {sym}") + if elf_file.symtab is not None: + print("") + print(f"Symbol table '{elf_file.symtab.name}' contains {len(elf_file.symtab.symbol_entries)} entries") + print(" Num: Value Size Type Bind Vis Ndx Name") + for i,sym in enumerate(elf_file.symtab.symbol_entries,0): + print(f"{i:6}: {sym}") # Relocations Info Test print("") for s in elf_file.sections: diff --git a/tools/patch_ar_meta.py b/tools/patch_ar_meta.py index 98d11f8..d831785 100755 --- a/tools/patch_ar_meta.py +++ b/tools/patch_ar_meta.py @@ -5,7 +5,7 @@ import argparse -def ar_patch(ar, new_uid, new_gid, new_filemode): +def ar_patch(ar, original, new_uid, new_gid, new_filemode): """ AR file headers @@ -20,6 +20,29 @@ def ar_patch(ar, new_uid, new_gid, new_filemode): """ assert len(ar) > 0x24 , "Got empty archive?" + armap_time1 = None + armap_time2 = None + + i = 8 + while i < len(original): + file_name = original[i:][0:][:16].decode("ASCII") + file_size = int(original[i:][48:][:10].decode("ASCII").strip()) + end = original[i:][58:][:2].decode("ASCII") + + assert end == "`\n" + + if file_name.strip() == "/": + armap_time1 = original[i+16:i+16+12] + elif file_name.strip() == "//": + armap_time2 = original[i+16:i+16+12] + + if file_size % 2 != 0: + file_size += 1 + i += file_size + 60 + + assert armap_time1 is not None + assert armap_time2 is not None + i = 8 while i < len(ar): file_name = ar[i:][0:][:16].decode("ASCII") @@ -28,7 +51,11 @@ def ar_patch(ar, new_uid, new_gid, new_filemode): assert end == "`\n" - if file_name.strip() not in ["/", "//"]: # Skip armap + if file_name.strip() == "/": + ar[i+16:i+16+12] = armap_time1 + elif file_name.strip() == "//": + ar[i+16:i+16+12] = armap_time2 + else: ar[i+28:i+28+6] = f"{new_uid:<6}".encode("ASCII")[:6] ar[i+34:i+34+6] = f"{new_gid:<6}".encode("ASCII")[:6] ar[i+40:i+40+8] = f"{new_filemode:<8}".encode("ASCII")[:8] @@ -39,16 +66,24 @@ def ar_patch(ar, new_uid, new_gid, new_filemode): def main(): parser = argparse.ArgumentParser(description="Patch metadata in .a files.") - parser.add_argument("archive", help="path to the ar file") + parser.add_argument("target", help="path to the ar file to patch") + parser.add_argument("original", help="path to the original ar file") + parser.add_argument("uid", help="new uid") + parser.add_argument("gid", help="new gid") + parser.add_argument("filemode", help="new filemode") args = parser.parse_args() ar = None - with open(args.archive, "rb") as ar_file: + with open(args.target, "rb") as ar_file: ar = bytearray(ar_file.read()) - ar_patch(ar, 0, 0, 37777700) + original = None + with open(args.original, "rb") as original_ar_file: + original = bytearray(original_ar_file.read()) - with open(args.archive, "wb") as ar_file: + ar_patch(ar, original, args.uid, args.gid, args.filemode) + + with open(args.target, "wb") as ar_file: ar_file.write(ar) if __name__ == '__main__': diff --git a/tools/strip_debug.sh b/tools/strip_debug.sh new file mode 100755 index 0000000..4a8be78 --- /dev/null +++ b/tools/strip_debug.sh @@ -0,0 +1,10 @@ +#!/usr/bin/bash + +cd $1 + +mkdir -p .cmp + +for f in *.o ; +do + ${CROSS}objcopy -p --strip-debug $f .cmp/${f/.o/.cmp.o} +done