├── .github └── workflows │ └── ci.yml ├── .gitignore ├── Doxyfile ├── Makefile ├── README.md ├── arm7 └── Makefile ├── arm9 └── Makefile ├── docs.css ├── icon.bmp ├── include ├── gl2d.h ├── nds.h └── nds │ ├── arm7 │ ├── aes.h │ ├── audio.h │ ├── codec.h │ ├── i2c.h │ └── serial.h │ ├── arm9 │ ├── background.h │ ├── boxtest.h │ ├── cache.h │ ├── cache_asm.h │ ├── console.h │ ├── decompress.h │ ├── dldi.h │ ├── dldi_asm.h │ ├── dynamicArray.h │ ├── exceptions.h │ ├── guitarGrip.h │ ├── image.h │ ├── input.h │ ├── keyboard.h │ ├── linkedlist.h │ ├── math.h │ ├── paddle.h │ ├── pcx.h │ ├── piano.h │ ├── postest.h │ ├── rumble.h │ ├── sassert.h │ ├── sound.h │ ├── sprite.h │ ├── trig_lut.h │ ├── video.h │ ├── videoGL.h │ └── window.h │ ├── asminc.h │ ├── bios.h │ ├── card.h │ ├── debug.h │ ├── disc_io.h │ ├── dldi.h │ ├── dma.h │ ├── input.h │ ├── interrupts.h │ ├── ipc.h │ ├── memory.h │ ├── ndstypes.h │ ├── rsa.h │ ├── sha1.h │ ├── system.h │ ├── timers.h │ └── touch.h ├── libnds_license.txt └── source ├── arm9 ├── background.c ├── boxtest.c ├── console.c ├── decompress.c ├── default_font.bin ├── dynamicArray.c ├── exceptionDefault.c ├── exceptionEntry.s ├── gfx │ ├── keyboardGfx.grit │ └── keyboardGfx.png ├── gl2d.c ├── guitarGrip.c ├── gurumeditation.c ├── heapfuncs.c ├── image.c ├── keyboard.c ├── keys.c ├── linkedlist.c ├── paddle.c ├── pcx.c ├── piano.c ├── rumble.c ├── sassert.c ├── shadowRegs.c ├── sound.c ├── sprite.c ├── sprite_alloc.c ├── system.c ├── system │ └── cpu_clock.s ├── trig.c ├── video.c ├── videoGL.c ├── videoGL_base.c └── window.c └── common ├── card.c ├── cardEeprom.c ├── dldi.c ├── rsa.c ├── sha1.c └── timers.c /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | name: ubuntu-latest 8 | runs-on: ubuntu-latest 9 | container: devkitpro/devkitarm:latest 10 | 11 | steps: 12 | - uses: actions/checkout@v4 13 | 14 | - name: Build 15 | run: | 16 | make -j 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .*/ 2 | arm7/debug 3 | arm7/release 4 | arm9/debug 5 | arm9/release 6 | include/nds/libversion.h 7 | lib 8 | docs 9 | warn.log 10 | *.bz2 11 | *~ 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(strip $(DEVKITPRO)),) 2 | $(error "Please set DEVKITPRO in your environment. export DEVKITPRO=devkitPro) 3 | endif 4 | 5 | export TOPDIR := $(CURDIR) 6 | 7 | export LIBNDS_MAJOR := 2 8 | export LIBNDS_MINOR := 0 9 | export LIBNDS_PATCH := 0 10 | 11 | 12 | VERSION := $(LIBNDS_MAJOR).$(LIBNDS_MINOR).$(LIBNDS_PATCH) 13 | 14 | 15 | .PHONY: release debug clean all docs 16 | 17 | all: include/nds/libversion.h release debug 18 | 19 | #------------------------------------------------------------------------------- 20 | release: lib 21 | #------------------------------------------------------------------------------- 22 | $(MAKE) -C arm9 BUILD=release || { exit 1;} 23 | $(MAKE) -C arm7 BUILD=release || { exit 1;} 24 | 25 | #------------------------------------------------------------------------------- 26 | debug: lib 27 | #------------------------------------------------------------------------------- 28 | $(MAKE) -C arm9 BUILD=debug || { exit 1;} 29 | $(MAKE) -C arm7 BUILD=debug || { exit 1;} 30 | 31 | #------------------------------------------------------------------------------- 32 | lib: 33 | #------------------------------------------------------------------------------- 34 | mkdir lib 35 | 36 | #------------------------------------------------------------------------------- 37 | clean: 38 | #------------------------------------------------------------------------------- 39 | @$(MAKE) -C arm9 clean 40 | @$(MAKE) -C arm7 clean 41 | 42 | #------------------------------------------------------------------------------- 43 | dist-src: 44 | #------------------------------------------------------------------------------- 45 | @tar --exclude=*CVS* --exclude=.svn -cjf libnds-src-$(VERSION).tar.bz2 arm7/Makefile arm9/Makefile source include Makefile libnds_license.txt Doxyfile 46 | 47 | #------------------------------------------------------------------------------- 48 | dist-bin: all 49 | #------------------------------------------------------------------------------- 50 | @tar --exclude=*CVS* --exclude=.svn -cjf libnds-$(VERSION).tar.bz2 include lib libnds_license.txt 51 | 52 | dist: dist-bin dist-src 53 | 54 | #------------------------------------------------------------------------------- 55 | install: dist-bin 56 | #------------------------------------------------------------------------------- 57 | mkdir -p $(DESTDIR)$(DEVKITPRO)/libnds 58 | bzip2 -cd libnds-$(VERSION).tar.bz2 | tar -xf - -C $(DESTDIR)$(DEVKITPRO)/libnds 59 | 60 | #--------------------------------------------------------------------------------- 61 | docs: 62 | #--------------------------------------------------------------------------------- 63 | doxygen Doxyfile 64 | cat warn.log 65 | 66 | #--------------------------------------------------------------------------------- 67 | include/nds/libversion.h : Makefile 68 | #--------------------------------------------------------------------------------- 69 | @echo "#ifndef __LIBNDSVERSION_H__" > $@ 70 | @echo "#define __LIBNDSVERSION_H__" >> $@ 71 | @echo >> $@ 72 | @echo "#define _LIBNDS_MAJOR_ $(LIBNDS_MAJOR)" >> $@ 73 | @echo "#define _LIBNDS_MINOR_ $(LIBNDS_MINOR)" >> $@ 74 | @echo "#define _LIBNDS_PATCH_ $(LIBNDS_PATCH)" >> $@ 75 | @echo >> $@ 76 | @echo '#define _LIBNDS_STRING "libNDS Release '$(LIBNDS_MAJOR).$(LIBNDS_MINOR).$(LIBNDS_PATCH)'"' >> $@ 77 | @echo >> $@ 78 | @echo "#endif // __LIBNDSVERSION_H__" >> $@ 79 | 80 | 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libnds 2 | -------------------------------------------------------------------------------- /arm7/Makefile: -------------------------------------------------------------------------------- 1 | #--------------------------------------------------------------------------------- 2 | .SUFFIXES: 3 | #--------------------------------------------------------------------------------- 4 | ifeq ($(strip $(DEVKITARM)),) 5 | $(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM) 6 | endif 7 | 8 | include $(DEVKITARM)/ds_rules 9 | 10 | TOPDIR ?= $(CURDIR)/.. 11 | 12 | #--------------------------------------------------------------------------------- 13 | # BUILD is the directory where object files & intermediate files will be placed 14 | # SOURCES is a list of directories containing source code 15 | # INCLUDES is a list of directories containing extra header files 16 | # DATA is a list of directories containing binary files 17 | # all directories are relative to this makefile 18 | #--------------------------------------------------------------------------------- 19 | BUILD ?= release 20 | SOURCES := ../source/arm7 ../source/common 21 | INCLUDES := ../include ../source/common 22 | 23 | #--------------------------------------------------------------------------------- 24 | # options for code generation 25 | #--------------------------------------------------------------------------------- 26 | ARCH := -mthumb -mthumb-interwork -march=armv4t -mtune=arm7tdmi -DARM7 27 | 28 | CFLAGS := -g -Wall -Os \ 29 | -ffunction-sections -fdata-sections \ 30 | $(ARCH) 31 | 32 | CFLAGS += $(INCLUDE) 33 | CXXFLAGS := $(CFLAGS) 34 | 35 | ASFLAGS := -g $(ARCH) 36 | ASFLAGS += $(INCLUDE) 37 | 38 | ifneq ($(BUILD),debug) 39 | export ARM7BIN := $(TOPDIR)/lib/libnds7.a 40 | CFLAGS += -DNDEBUG 41 | else 42 | export ARM7BIN := $(TOPDIR)/lib/libnds7d.a 43 | CFLAGS += -DDEBUG 44 | endif 45 | 46 | 47 | #--------------------------------------------------------------------------------- 48 | # any extra libraries we wish to link with the project 49 | #--------------------------------------------------------------------------------- 50 | LIBS := 51 | 52 | #--------------------------------------------------------------------------------- 53 | # list of directories containing libraries, this must be the top level containing 54 | # include and lib 55 | #--------------------------------------------------------------------------------- 56 | LIBDIRS := $(LIBNDS) 57 | 58 | #--------------------------------------------------------------------------------- 59 | # no real need to edit anything past this point unless you need to add additional 60 | # rules for different file extensions 61 | #--------------------------------------------------------------------------------- 62 | ifneq ($(BUILD),$(notdir $(CURDIR))) 63 | #--------------------------------------------------------------------------------- 64 | 65 | 66 | export DEPSDIR := $(CURDIR)/$(BUILD) 67 | 68 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) 69 | 70 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 71 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 72 | 73 | #--------------------------------------------------------------------------------- 74 | # use CXX for linking C++ projects, CC for standard C 75 | #--------------------------------------------------------------------------------- 76 | ifeq ($(strip $(CPPFILES)),) 77 | #--------------------------------------------------------------------------------- 78 | export LD := $(CC) 79 | #--------------------------------------------------------------------------------- 80 | else 81 | #--------------------------------------------------------------------------------- 82 | export LD := $(CXX) 83 | #--------------------------------------------------------------------------------- 84 | endif 85 | #--------------------------------------------------------------------------------- 86 | 87 | export OFILES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) 88 | 89 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 90 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 91 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 92 | -I$(CURDIR)/$(BUILD) 93 | 94 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 95 | 96 | .PHONY: $(BUILD) clean 97 | 98 | #--------------------------------------------------------------------------------- 99 | $(BUILD): 100 | @[ -d $@ ] || mkdir -p $@ 101 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 102 | 103 | #--------------------------------------------------------------------------------- 104 | clean: 105 | @echo clean ... 106 | @rm -fr debug release 107 | @rm -f $(TOPDIR)/lib/libnds7* 108 | 109 | all: $(ARM7BIN) 110 | 111 | #--------------------------------------------------------------------------------- 112 | else 113 | 114 | DEPENDS := $(OFILES:.o=.d) 115 | 116 | #--------------------------------------------------------------------------------- 117 | # main targets 118 | #--------------------------------------------------------------------------------- 119 | $(ARM7BIN) : $(OFILES) 120 | @rm -f "$(ARM7BIN)" 121 | @$(AR) rcs "$(ARM7BIN)" $(OFILES) 122 | @echo built ... $(notdir $@) 123 | 124 | -include $(DEPENDS) 125 | 126 | #--------------------------------------------------------------------------------------- 127 | endif 128 | #--------------------------------------------------------------------------------------- 129 | -------------------------------------------------------------------------------- /arm9/Makefile: -------------------------------------------------------------------------------- 1 | .SECONDARY: 2 | #--------------------------------------------------------------------------------- 3 | .SUFFIXES: 4 | #--------------------------------------------------------------------------------- 5 | ifeq ($(strip $(DEVKITARM)),) 6 | $(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM) 7 | endif 8 | 9 | include $(DEVKITARM)/ds_rules 10 | 11 | TOPDIR ?= $(CURDIR)/.. 12 | 13 | #--------------------------------------------------------------------------------- 14 | # BUILD is the directory where object files & intermediate files will be placed 15 | # SOURCES is a list of directories containing source code 16 | # INCLUDES is a list of directories containing extra header files 17 | # DATA is a list of directories containing binary files 18 | # all directories are relative to this makefile 19 | #--------------------------------------------------------------------------------- 20 | BUILD ?= release 21 | SOURCES := ../source/arm9 ../source/arm9/system ../source/common 22 | INCLUDES := ../include ../source/common 23 | GRAPHICS := ../source/arm9/gfx 24 | DATA := ../source/arm9 25 | 26 | #--------------------------------------------------------------------------------- 27 | # options for code generation 28 | #--------------------------------------------------------------------------------- 29 | ARCH := -march=armv5te -mtune=arm946e-s -mthumb -DARM9 30 | 31 | CFLAGS := -g -Wall -O2\ 32 | -ffunction-sections -fdata-sections \ 33 | $(ARCH) 34 | 35 | CFLAGS += $(INCLUDE) 36 | 37 | CXXFLAGS := $(CFLAGS) 38 | 39 | ASFLAGS = -g $(ARCH) 40 | ASFLAGS += $(INCLUDE) 41 | 42 | ifneq ($(BUILD),debug) 43 | export ARM9BIN := $(TOPDIR)/lib/libnds9.a 44 | CFLAGS += -DNDEBUG 45 | else 46 | export ARM9BIN := $(TOPDIR)/lib/libnds9d.a 47 | CFLAGS += -DDEBUG 48 | endif 49 | 50 | 51 | #--------------------------------------------------------------------------------- 52 | # any extra libraries we wish to link with the project 53 | #--------------------------------------------------------------------------------- 54 | LIBS := 55 | 56 | #--------------------------------------------------------------------------------- 57 | # list of directories containing libraries, this must be the top level containing 58 | # include and lib 59 | #--------------------------------------------------------------------------------- 60 | LIBDIRS := $(LIBNDS) 61 | 62 | #--------------------------------------------------------------------------------- 63 | # no real need to edit anything past this point unless you need to add additional 64 | # rules for different file extensions 65 | #--------------------------------------------------------------------------------- 66 | ifneq ($(BUILD),$(notdir $(CURDIR))) 67 | #--------------------------------------------------------------------------------- 68 | 69 | 70 | export DEPSDIR := $(CURDIR)/$(BUILD) 71 | 72 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 73 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) \ 74 | $(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir)) 75 | 76 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 77 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 78 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.bin))) 79 | PNGFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png))) 80 | 81 | #--------------------------------------------------------------------------------- 82 | # use CXX for linking C++ projects, CC for standard C 83 | #--------------------------------------------------------------------------------- 84 | ifeq ($(strip $(CPPFILES)),) 85 | #--------------------------------------------------------------------------------- 86 | export LD := $(CC) 87 | #--------------------------------------------------------------------------------- 88 | else 89 | #--------------------------------------------------------------------------------- 90 | export LD := $(CXX) 91 | #--------------------------------------------------------------------------------- 92 | endif 93 | #--------------------------------------------------------------------------------- 94 | 95 | export OFILES_BIN := $(addsuffix .o,$(BINFILES)) $(PNGFILES:.png=.o) 96 | 97 | export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) 98 | 99 | export OFILES := $(OFILES_BIN) $(OFILES_SRC) 100 | 101 | export HFILES := $(PNGFILES:.png=.h) $(addsuffix .h,$(subst .,_,$(BINFILES))) 102 | 103 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 104 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 105 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 106 | -I$(CURDIR)/$(BUILD) 107 | 108 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 109 | 110 | .PHONY: $(BUILD) clean 111 | 112 | #--------------------------------------------------------------------------------- 113 | $(BUILD): 114 | @[ -d $@ ] || mkdir -p $@ 115 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 116 | 117 | #--------------------------------------------------------------------------------- 118 | clean: 119 | @echo clean ... 120 | @rm -fr debug release 121 | @rm -f $(TOPDIR)/lib/libnds9* 122 | 123 | all: $(ARM9BIN) 124 | 125 | #--------------------------------------------------------------------------------- 126 | else 127 | 128 | DEPENDS := $(OFILES:.o=.d) 129 | 130 | #--------------------------------------------------------------------------------- 131 | # main targets 132 | #--------------------------------------------------------------------------------- 133 | $(ARM9BIN) : $(OFILES) 134 | @rm -f "$(ARM9BIN)" 135 | @$(AR) rcs "$(ARM9BIN)" $(OFILES) 136 | @echo built ... $(notdir $@) 137 | 138 | $(OFILES_SRC) : $(HFILES) 139 | 140 | #--------------------------------------------------------------------------------- 141 | # you need a rule like this for each extension you use as binary data 142 | #--------------------------------------------------------------------------------- 143 | %_bin.h %.bin.o : %.bin 144 | #--------------------------------------------------------------------------------- 145 | @echo $(notdir $<) 146 | @$(bin2o) 147 | 148 | #--------------------------------------------------------------------------------- 149 | %.s %.h : %.png %.grit 150 | #--------------------------------------------------------------------------------- 151 | grit $< -fts -o$* 152 | 153 | -include $(DEPENDS) 154 | 155 | #--------------------------------------------------------------------------------------- 156 | endif 157 | #--------------------------------------------------------------------------------------- 158 | -------------------------------------------------------------------------------- /icon.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devkitPro/libnds/6194b32d8f94e2ebc8078e64bf213ffc13ba1985/icon.bmp -------------------------------------------------------------------------------- /include/nds/arm7/aes.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | ARM7 AES 4 | 5 | Copyright (C) 2017 6 | Dave Murphy (WinterMute) 7 | 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 2. Altered source versions must be plainly marked as such, and 21 | must not be misrepresented as being the original software. 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | ---------------------------------------------------------------------------------*/ 26 | 27 | #ifndef AES_ARM7_INCLUDE 28 | #define AES_ARM7_INCLUDE 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | #include 35 | #include 36 | 37 | typedef struct aes_keyslot { 38 | u8 normalkey[16]; 39 | u8 key_x[16]; 40 | u8 key_y[16]; 41 | } aes_keyslot_t; 42 | 43 | enum { 44 | AES_MODE_CCM_decrypt = AesMode_CcmDecrypt, 45 | AES_MODE_CCM_encrypt = AesMode_CcmEncrypt, 46 | AES_MODE_CTR = AesMode_Ctr, 47 | }; 48 | 49 | enum { 50 | CCM_MAC_SIZE_2, 51 | CCM_MAC_SIZE_4, 52 | CCM_MAC_SIZE_6, 53 | CCM_MAC_SIZE_8, 54 | CCM_MAC_SIZE_10, 55 | CCM_MAC_SIZE_12, 56 | CCM_MAC_SIZE_14, 57 | CCM_MAC_SIZE_16, 58 | }; 59 | 60 | #define AES_CNT_DMA_WRITE_SIZE(size) AES_WRFIFO_DMA_SIZE(size) 61 | #define AES_CNT_DMA_READ_SIZE(size) AES_RDFIFO_DMA_SIZE(size) 62 | 63 | #define AES_CNT_MAC_SIZE(size) ((((_x)/2-1)&7)<<16) 64 | 65 | #define AES_CNT_KEY_APPLY AES_KEY_SELECT 66 | 67 | #define AES_CNT_KEYSLOT(slot) AES_KEY_SLOT(slot) 68 | 69 | #define AES_CNT_MODE(mode) AES_MODE(mode) 70 | 71 | #define AES_CNT_IRQ AES_IRQ_ENABLE 72 | 73 | #define AES_CNT_ENABLE AES_ENABLE 74 | 75 | #define REG_AES_BLKCNT REG_AES_LEN 76 | 77 | #define AES_KEYSLOT ((aes_keyslot_t *)(MM_IO + IO_AES_SLOTxKEY(0))) 78 | #define AES_KEYSLOT0 MK_REG(aes_keyslot_t, IO_AES_SLOTxKEY(0)) 79 | #define AES_KEYSLOT1 MK_REG(aes_keyslot_t, IO_AES_SLOTxKEY(1)) 80 | #define AES_KEYSLOT2 MK_REG(aes_keyslot_t, IO_AES_SLOTxKEY(2)) 81 | #define AES_KEYSLOT3 MK_REG(aes_keyslot_t, IO_AES_SLOTxKEY(3)) 82 | 83 | #ifdef __cplusplus 84 | } 85 | #endif 86 | 87 | #endif // AES_ARM7_INCLUDE -------------------------------------------------------------------------------- /include/nds/arm7/audio.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | ARM7 audio control 4 | 5 | Copyright (C) 2005 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Dave Murphy (WinterMute) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 3. This notice may not be removed or altered from any source 25 | distribution. 26 | 27 | ---------------------------------------------------------------------------------*/ 28 | 29 | #ifndef AUDIO_ARM7_INCLUDE 30 | #define AUDIO_ARM7_INCLUDE 31 | 32 | //--------------------------------------------------------------------------------- 33 | // Sound (ARM7 only) 34 | //--------------------------------------------------------------------------------- 35 | #ifndef ARM7 36 | #error Audio is only available on the ARM7 37 | #endif 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | #include 44 | #include 45 | #include 46 | #include 47 | 48 | #define SOUND_VOL(n) SOUNDCNT_VOL(n) 49 | #define SOUND_FREQ(n) (-soundTimerFromHz(n)) 50 | #define SOUND_ENABLE SOUNDCNT_ENABLE 51 | 52 | #define SOUND_REPEAT SOUNDxCNT_MODE(SoundMode_Repeat) 53 | #define SOUND_ONE_SHOT SOUNDxCNT_MODE(SoundMode_OneShot) 54 | 55 | #define SOUND_FORMAT_16BIT SOUNDxCNT_FMT(SoundFmt_Pcm16) 56 | #define SOUND_FORMAT_8BIT SOUNDxCNT_FMT(SoundFmt_Pcm8) 57 | #define SOUND_FORMAT_PSG SOUNDxCNT_FMT(SoundFmt_Psg) 58 | #define SOUND_FORMAT_ADPCM SOUNDxCNT_FMT(SoundFmt_ImaAdpcm) 59 | 60 | #define SOUND_PAN(n) SOUNDxCNT_PAN(n) 61 | 62 | #define SCHANNEL_ENABLE SOUNDxCNT_ENABLE 63 | 64 | //--------------------------------------------------------------------------------- 65 | // registers 66 | //--------------------------------------------------------------------------------- 67 | 68 | #define REG_MASTER_VOLUME REG_SOUNDCNTVOL 69 | 70 | 71 | #define SCHANNEL_CR(n) REG_SOUNDxCNT(n) 72 | #define SCHANNEL_VOL(n) REG_SOUNDxVOL(n) 73 | #define SCHANNEL_PAN(n) REG_SOUNDxPAN(n) 74 | #define SCHANNEL_SOURCE(n) REG_SOUNDxSAD(n) 75 | #define SCHANNEL_TIMER(n) REG_SOUNDxTMR(n) 76 | #define SCHANNEL_REPEAT_POINT(n) REG_SOUNDxPNT(n) 77 | #define SCHANNEL_LENGTH(n) REG_SOUNDxLEN(n) 78 | 79 | 80 | //--------------------------------------------------------------------------------- 81 | // Sound Capture Registers 82 | //--------------------------------------------------------------------------------- 83 | #define REG_SNDCAP0CNT REG_SNDCAPxCNT(0) 84 | #define REG_SNDCAP1CNT REG_SNDCAPxCNT(1) 85 | 86 | #define REG_SNDCAP0DAD REG_SNDCAPxDAD(0) 87 | #define REG_SNDCAP0LEN REG_SNDCAPxLEN(0) 88 | #define REG_SNDCAP1DAD REG_SNDCAPxDAD(1) 89 | #define REG_SNDCAP1LEN REG_SNDCAPxLEN(1) 90 | 91 | //--------------------------------------------------------------------------------- 92 | // DSi Registers 93 | //--------------------------------------------------------------------------------- 94 | 95 | #define REG_SNDEXTCNT REG_SNDEXCNT 96 | #define REG_MICCNT REG_MICEX_CNT 97 | #define REG_MICDATA REG_MICEX_DATA 98 | 99 | #define SNDEXTCNT_RATIO(n) SNDEXCNT_MIX_RATIO(n) 100 | #define SNDEXTCNT_FREQ_32KHZ SNDEXCNT_I2S_32728_HZ 101 | #define SNDEXTCNT_FREQ_47KHZ SNDEXCNT_I2S_4761x_HZ 102 | #define SNDEXTCNT_MUTE SNDEXCNT_MUTE 103 | #define SNDEXTCNT_ENABLE SNDEXCNT_ENABLE 104 | 105 | #define MICCNT_FORMAT(n) ((n)&3) // unknown, always set to '2' 106 | #define MICCNT_FREQ_DIV(n) MICEX_CNT_RATE_DIV(n) 107 | #define MICCNT_EMPTY MICEX_CNT_FIFO_EMPTY 108 | #define MICCNT_NOT_EMPTY MICEX_CNT_FIFO_HALF 109 | #define MICCNT_MORE_DATA MICEX_CNT_FIFO_FULL 110 | #define MICCNT_OVERRUN MICEX_CNT_FIFO_BORKED 111 | #define MICCNT_CLEAR_FIFO MICEX_CNT_CLEAR_FIFO 112 | #define MICCNT_ENABLE_IRQ MICEX_CNT_IE_FIFO_HALF 113 | #define MICCNT_ENABLE_IRQ2 MICEX_CNT_IE_FIFO_FULL 114 | #define MICCNT_ENABLE MICEX_CNT_ENABLE 115 | 116 | #ifdef __cplusplus 117 | } 118 | #endif 119 | 120 | #endif 121 | 122 | -------------------------------------------------------------------------------- /include/nds/arm7/codec.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | DSi "codec" Touchscreen/Sound Controller control for ARM7 4 | 5 | Copyright (C) 2017 6 | fincs 7 | 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 2. Altered source versions must be plainly marked as such, and 21 | must not be misrepresented as being the original software. 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | ---------------------------------------------------------------------------------*/ 26 | #ifndef ARM7_CODEC_INCLUDE 27 | #define ARM7_CODEC_INCLUDE 28 | //--------------------------------------------------------------------------------- 29 | 30 | 31 | #ifndef ARM7 32 | #error DSi TSC is only available on the ARM7 33 | #endif 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | #define cdcIsAvailable cdcIsTwlMode 45 | 46 | #define CDC_CONTROL CdcPage_Control 47 | #define CDC_SOUND CdcPage_Sound 48 | #define CDC_TOUCHCNT CdcPage_TscControl 49 | #define CDC_TOUCHDATA CdcPage_TscData 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | 55 | 56 | //--------------------------------------------------------------------------------- 57 | #endif // ARM7_CODEC_INCLUDE 58 | //--------------------------------------------------------------------------------- 59 | 60 | -------------------------------------------------------------------------------- /include/nds/arm7/i2c.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | I2C control for the ARM7 4 | 5 | Copyright (C) 2011 6 | Dave Murphy (WinterMute) 7 | 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 2. Altered source versions must be plainly marked as such, and 21 | must not be misrepresented as being the original software. 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | ---------------------------------------------------------------------------------*/ 26 | #ifndef I2C_ARM7_INCLUDE 27 | #define I2C_ARM7_INCLUDE 28 | 29 | #ifndef ARM7 30 | #error i2c header is for ARM7 only 31 | #endif 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | #define REG_I2CDATA REG_I2C_DATA 38 | #define REG_I2CCNT REG_I2C_CNT 39 | 40 | static inline void i2cWaitBusy() { 41 | while(REG_I2CCNT & 0x80); 42 | } 43 | 44 | enum i2cDevices { 45 | I2C_CAM0 = 0x7A, 46 | I2C_CAM1 = 0x78, 47 | I2C_UNK1 = 0xA0, 48 | I2C_UNK2 = 0xE0, 49 | I2C_PM = I2cDev_MCU, 50 | I2C_UNK3 = 0x40, 51 | I2C_GPIO = 0x90 52 | }; 53 | 54 | // Registers for Power Management (I2C_PM) 55 | #define I2CREGPM_BATUNK McuReg_Version 56 | #define I2CREGPM_PWRIF McuReg_IrqFlags 57 | #define I2CREGPM_PWRCNT McuReg_DoReset 58 | #define I2CREGPM_MMCPWR McuReg_Config 59 | #define I2CREGPM_BATTERY McuReg_BatteryState 60 | #define I2CREGPM_CAMLED McuReg_CamLed 61 | #define I2CREGPM_VOL McuReg_VolumeLevel 62 | #define I2CREGPM_RESETFLAG McuReg_WarmbootFlag 63 | 64 | static inline u8 i2cWriteRegister(u8 device, u8 reg, u8 data) 65 | { 66 | i2cLock(); 67 | bool ret = i2cWriteRegister8((I2cDevice)device, reg, data); 68 | i2cUnlock(); 69 | return ret; 70 | } 71 | 72 | static inline u8 i2cReadRegister(u8 device, u8 reg) 73 | { 74 | i2cLock(); 75 | u8 ret = i2cReadRegister8((I2cDevice)device, reg); 76 | i2cUnlock(); 77 | return ret; 78 | } 79 | 80 | #endif // I2C_ARM7_INCLUDE -------------------------------------------------------------------------------- /include/nds/arm7/serial.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | SPI control for the ARM7 4 | 5 | Copyright (C) 2005 - 2011 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Dave Murphy (WinterMute) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 3. This notice may not be removed or altered from any source 25 | distribution. 26 | 27 | ---------------------------------------------------------------------------------*/ 28 | 29 | #ifndef SERIAL_ARM7_INCLUDE 30 | #define SERIAL_ARM7_INCLUDE 31 | 32 | #ifndef ARM7 33 | #error Serial header is for ARM7 only 34 | #endif 35 | 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | // 'Networking' 42 | #define REG_KEYXY REG_RCNT_EXT 43 | #define RTC_CR REG_RCNT_RTC 44 | 45 | #define REG_SIOCNT (*(vu16*)0x04000128) 46 | 47 | #define SIO_DATA8 (*(vu8*)0x0400012A) 48 | #define SIO_DATA32 (*(vu32*)0x04000120) 49 | 50 | // Fixme: Does the hardware still support 16 bit comms mode? 51 | // BIOS makes use of 32 bit mode, so some regs still exist 52 | #define SIO_MULTI_0 (*(vu16*)0x04000120) 53 | #define SIO_MULTI_1 (*(vu16*)0x04000122) 54 | #define SIO_MULTI_2 (*(vu16*)0x04000124) 55 | #define SIO_MULTI_3 (*(vu16*)0x04000126) 56 | #define SIO_MULTI_SEND (*(vu16*)0x0400012A) 57 | 58 | #define SerialWaitBusy spiWaitBusy 59 | 60 | #define SPI_ENABLE SPICNT_ENABLE 61 | #define SPI_IRQ SPICNT_IRQ_ENABLE 62 | #define SPI_BUSY SPICNT_BUSY 63 | 64 | // meh 65 | #define SPI_BAUD_4MHz SpiBaud_4MHz 66 | #define SPI_BAUD_2MHz SpiBaud_2MHz 67 | #define SPI_BAUD_1MHz SpiBaud_1MHz 68 | #define SPI_BAUD_512KHz SpiBaud_512KHz 69 | #define SPI_BAUD_8MHz SpiBaud_8MHz // DSi only 70 | 71 | // Pick the SPI transfer length 72 | #define SPI_BYTE_MODE (0<<10) 73 | #define SPI_HWORD_MODE (1<<10) // bugged/useless 74 | 75 | // Pick the SPI device 76 | #define SPI_DEVICE_POWER SPICNT_DEVICE(SpiDev_PMIC) 77 | #define SPI_DEVICE_FIRMWARE SPICNT_DEVICE(SpiDev_NVRAM) 78 | #define SPI_DEVICE_NVRAM SPICNT_DEVICE(SpiDev_NVRAM) 79 | #define SPI_DEVICE_TOUCH SPICNT_DEVICE(SpiDev_TSC) 80 | #define SPI_DEVICE_MICROPHONE SPICNT_DEVICE(SpiDev_TSC) 81 | 82 | // When used, the /CS line will stay low after the transfer ends 83 | // i.e. when we're part of a continuous transfer 84 | #define SPI_CONTINUOUS SPICNT_HOLD 85 | 86 | // Firmware commands 87 | #define FIRMWARE_WREN 0x06 88 | #define FIRMWARE_WRDI 0x04 89 | #define FIRMWARE_RDID 0x9F 90 | #define FIRMWARE_RDSR 0x05 91 | #define FIRMWARE_READ 0x03 92 | #define FIRMWARE_PW 0x0A 93 | #define FIRMWARE_PP 0x02 94 | #define FIRMWARE_FAST 0x0B 95 | #define FIRMWARE_PE 0xDB 96 | #define FIRMWARE_SE 0xD8 97 | #define FIRMWARE_DP 0xB9 98 | #define FIRMWARE_RDP 0xAB 99 | 100 | #endif 101 | -------------------------------------------------------------------------------- /include/nds/arm9/boxtest.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | BoxTest.h -- Code for performing hardware box test against viewing frustrum 4 | 5 | Copyright (C) 2005 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Dave Murphy (WinterMute) 9 | Mike Parks (BigRedPimp) 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | Permission is granted to anyone to use this software for any 14 | purpose, including commercial applications, and to alter it and 15 | redistribute it freely, subject to the following restrictions: 16 | 17 | 1. The origin of this software must not be misrepresented; you 18 | must not claim that you wrote the original software. If you use 19 | this software in a product, an acknowledgment in the product 20 | documentation would be appreciated but is not required. 21 | 2. Altered source versions must be plainly marked as such, and 22 | must not be misrepresented as being the original software. 23 | 3. This notice may not be removed or altered from any source 24 | distribution. 25 | 26 | 27 | ---------------------------------------------------------------------------------*/ 28 | #ifndef BOX_TEST_INCLUDE 29 | #define BOX_TEST_INCLUDE 30 | 31 | #include "nds/arm9/video.h" 32 | #include "nds/arm9/videoGL.h" 33 | 34 | /*! \file boxtest.h 35 | \brief Box Test Functions. 36 | */ 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | 43 | /*! \brief Performs a test to determine if the provided box is in the view frustrum. 44 | \param x (x, y, z) point of a vertex on the box 45 | \param y (x, y, z) point of a vertex on the box 46 | \param z (x, y, z) point of a vertex on the box 47 | \param height (height, width, depth) describe the size of the box referenced from (x, y, z) 48 | \param width (height, width, depth) describe the size of the box referenced from (x, y, z) 49 | \param depth (height, width, depth) describe the size of the box referenced from (x, y, z) 50 | 51 | \return non zero if any or all of the box is in the view frustum. 52 | */ 53 | int BoxTest(v16 x, v16 y, v16 z, v16 width, v16 height, v16 depth); 54 | 55 | /*! \brief Performs a test to determine if the provided box is in the view frustum. 56 | \param x (x, y, z) point of a vertex on the box 57 | \param y (x, y, z) point of a vertex on the box 58 | \param z (x, y, z) point of a vertex on the box 59 | \param width (width, height, depth) describe the size of the box referenced from (x, y, z) 60 | \param height (width, height, depth) describe the size of the box referenced from (x, y, z) 61 | \param depth (width, height, depth) describe the size of the box referenced from (x, y, z) 62 | 63 | \return non zero if any or all of the box is in the view frustum. 64 | */ 65 | int BoxTestf(float x, float y, float z, float width, float height, float depth); 66 | 67 | /*! \brief Performs a test to determine if the provided box is in the view frustum. 68 | Performs a test to determine if the provided box is in the view frustum. 69 | BoxTestResult must be called to get the result of this operation. 70 | 71 | \param x (x, y, z) point of a vertex on the box 72 | \param y (x, y, z) point of a vertex on the box 73 | \param z (x, y, z) point of a vertex on the box 74 | \param width (width, height, depth) describe the size of the box referenced from (x, y, z) 75 | \param height (width, height, depth) describe the size of the box referenced from (x, y, z) 76 | \param depth (width, height, depth) describe the size of the box referenced from (x, y, z) 77 | */ 78 | void BoxTest_Asynch(v16 x, v16 y, v16 z, v16 height, v16 width, v16 depth); 79 | 80 | /*! \brief Performs a test to determine if the provided box is in the view frustum. 81 | Performs a test to determine if the provided box is in the view frustum. 82 | BoxTestResult must be called to get the result of this operation. 83 | 84 | \param x (x, y, z) point of a vertex on the box 85 | \param y (x, y, z) point of a vertex on the box 86 | \param z (x, y, z) point of a vertex on the box 87 | \param width (width, height, depth) describe the size of the box referenced from (x, y, z) 88 | \param height (width, height, depth) describe the size of the box referenced from (x, y, z) 89 | \param depth (width, height, depth) describe the size of the box referenced from (x, y, z) 90 | */ 91 | void BoxTestf_Asynch(float x, float y, float z, float width, float height, float depth); 92 | 93 | /*! \brief Gets the result of the last box test. Needed for asynch box test calls. 94 | \return non zero if any or all of the box is in the view frustum. 95 | */ 96 | int BoxTestResult(void); 97 | 98 | #ifdef __cplusplus 99 | } 100 | #endif 101 | #endif 102 | -------------------------------------------------------------------------------- /include/nds/arm9/cache.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | $Id: cache.h,v 1.8 2008-02-12 00:45:58 wntrmute Exp $ 3 | 4 | key input code -- provides slightly higher level input forming 5 | 6 | Copyright (C) 2005 7 | Michael Noland (joat) 8 | Jason Rogers (dovoto) 9 | Dave Murphy (WinterMute) 10 | 11 | This software is provided 'as-is', without any express or implied 12 | warranty. In no event will the authors be held liable for any 13 | damages arising from the use of this software. 14 | 15 | Permission is granted to anyone to use this software for any 16 | purpose, including commercial applications, and to alter it and 17 | redistribute it freely, subject to the following restrictions: 18 | 19 | 1. The origin of this software must not be misrepresented; you 20 | must not claim that you wrote the original software. If you use 21 | this software in a product, an acknowledgment in the product 22 | documentation would be appreciated but is not required. 23 | 2. Altered source versions must be plainly marked as such, and 24 | must not be misrepresented as being the original software. 25 | 3. This notice may not be removed or altered from any source 26 | distribution. 27 | 28 | ---------------------------------------------------------------------------------*/ 29 | /*! \file cache.h 30 | \brief ARM9 cache control functions. 31 | */ 32 | #ifndef _cache_h_ 33 | #define _cache_h_ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #include 40 | 41 | 42 | /*! \fn IC_InvalidateAll() 43 | \brief invalidate entire instruction cache. 44 | */ 45 | void IC_InvalidateAll(void) __asm__("armICacheInvalidateAll"); 46 | 47 | 48 | /*! \fn IC_InvalidateRange(const void *base, u32 size) 49 | \brief invalidate the instruction cache for a range of addresses. 50 | \param base base address of the region to invalidate 51 | \param size size of the region to invalidate. 52 | */ 53 | void IC_InvalidateRange(const void *base, u32 size) __asm__("armICacheInvalidate"); 54 | 55 | 56 | /*! \fn DC_FlushAll() 57 | \brief flush the entire data cache to memory. 58 | */ 59 | void DC_FlushAll(void) __asm__("armDCacheFlushAll"); 60 | 61 | 62 | /*! \fn DC_FlushRange(const void *base, u32 size) 63 | \brief flush the data cache for a range of addresses to memory. 64 | \param base base address of the region to flush. 65 | \param size size of the region to flush. 66 | */ 67 | void DC_FlushRange(const void *base, u32 size) __asm__("armDCacheFlush"); 68 | 69 | 70 | /*! \fn DC_InvalidateRange(const void *base, u32 size) 71 | \brief invalidate the data cache for a range of addresses. 72 | \param base base address of the region to invalidate 73 | \param size size of the region to invalidate. 74 | \warning Base address and size must be cache line size (32-byte) aligned! 75 | */ 76 | void DC_InvalidateRange(const void *base, u32 size) __asm__("armDCacheInvalidate"); 77 | 78 | #ifdef __cplusplus 79 | } 80 | #endif 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /include/nds/arm9/cache_asm.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2016 4 | Dave Murphy (WinterMute) 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any 8 | damages arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any 11 | purpose, including commercial applications, and to alter it and 12 | redistribute it freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you 15 | must not claim that you wrote the original software. If you use 16 | this software in a product, an acknowledgment in the product 17 | documentation would be appreciated but is not required. 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | 23 | ---------------------------------------------------------------------------------*/ 24 | /*! \file cache.h 25 | \brief ARM9 cache defines. 26 | */ 27 | #ifndef _cache_asm_h_ 28 | #define _cache_asm_h_ 29 | 30 | #define PAGE_4K (0b01011 << 1) 31 | #define PAGE_8K (0b01100 << 1) 32 | #define PAGE_16K (0b01101 << 1) 33 | #define PAGE_32K (0b01110 << 1) 34 | #define PAGE_64K (0b01111 << 1) 35 | #define PAGE_128K (0b10000 << 1) 36 | #define PAGE_256K (0b10001 << 1) 37 | #define PAGE_512K (0b10010 << 1) 38 | #define PAGE_1M (0b10011 << 1) 39 | #define PAGE_2M (0b10100 << 1) 40 | #define PAGE_4M (0b10101 << 1) 41 | #define PAGE_8M (0b10110 << 1) 42 | #define PAGE_16M (0b10111 << 1) 43 | #define PAGE_32M (0b11000 << 1) 44 | #define PAGE_64M (0b11001 << 1) 45 | #define PAGE_128M (0b11010 << 1) 46 | #define PAGE_256M (0b11011 << 1) 47 | #define PAGE_512M (0b11100 << 1) 48 | #define PAGE_1G (0b11101 << 1) 49 | #define PAGE_2G (0b11110 << 1) 50 | #define PAGE_4G (0b11111 << 1) 51 | 52 | #define ITCM_LOAD (1<<19) 53 | #define ITCM_ENABLE (1<<18) 54 | #define DTCM_LOAD (1<<17) 55 | #define DTCM_ENABLE (1<<16) 56 | #define DISABLE_TBIT (1<<15) 57 | #define ROUND_ROBIN (1<<14) 58 | #define ALT_VECTORS (1<<13) 59 | #define ICACHE_ENABLE (1<<12) 60 | #define BIG_ENDIAN (1<<7) 61 | #define DCACHE_ENABLE (1<<2) 62 | #define PROTECT_ENABLE (1<<0) 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /include/nds/arm9/decompress.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Jason Rogers (dovoto) 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any 8 | damages arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any 11 | purpose, including commercial applications, and to alter it and 12 | redistribute it freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you 15 | must not claim that you wrote the original software. If you use 16 | this software in a product, an acknowledgment in the product 17 | documentation would be appreciated but is not required. 18 | 19 | 2. Altered source versions must be plainly marked as such, and 20 | must not be misrepresented as being the original software. 21 | 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | 26 | ---------------------------------------------------------------------------------*/ 27 | /*! \file decompress.h 28 | \brief wraps the bios decompress functionality into something a bit easier to deal with. 29 | */ 30 | 31 | 32 | #ifndef NDS_DECOMPRESS 33 | #define NDS_DECOMPRESS 34 | 35 | #include 36 | #include 37 | 38 | 39 | //! the types of decompression available. 40 | typedef enum 41 | { 42 | LZ77, //!< LZ77 decompression. 43 | LZ77Vram, //!< vram safe LZ77 decompression. 44 | HUFF, //!< vram safe huff decompression. 45 | RLE, //!< run length encoded decompression. 46 | RLEVram //!< vram safe run length encoded decompression. 47 | }DecompressType; 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | /*! 54 | \brief decompresses data using the suported type 55 | \param dst the destination to decompress to 56 | \param data the data to decompress 57 | \param type the type of data to decompress 58 | */ 59 | void decompress(const void* data, void* dst, DecompressType type); 60 | 61 | /*! 62 | \brief decompresses data using the suported type (only LZ77Vram, HUFF, and RLEVram support streaming) 63 | \param dst the destination to decompress to. 64 | \param data the data to decompress. 65 | \param type the type of data to decompress. 66 | \param readCB a callback to read the next byte of data. 67 | \param getHeaderCB a callback to read the 32 byte header. 68 | */ 69 | void decompressStream(const void* data, void* dst, DecompressType type, getByteCallback readCB, getHeaderCallback getHeaderCB); 70 | 71 | #ifdef __cplusplus 72 | } 73 | #endif 74 | 75 | #endif 76 | 77 | -------------------------------------------------------------------------------- /include/nds/arm9/dldi.h: -------------------------------------------------------------------------------- 1 | /* 2 | dldi.h 3 | 4 | Copyright (c) 2006 Michael "Chishm" Chisholm 5 | 6 | Redistribution and use in source and binary forms, with or without modification, 7 | are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | 2. Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation and/or 13 | other materials provided with the distribution. 14 | 3. The name of the author may not be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 19 | AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 25 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef NDS_DLDI_INCLUDE 29 | #define NDS_DLDI_INCLUDE 30 | 31 | #include "../dldi.h" 32 | 33 | #endif // NDS_DLDI_INCLUDE 34 | -------------------------------------------------------------------------------- /include/nds/arm9/dldi_asm.h: -------------------------------------------------------------------------------- 1 | #define FEATURE_MEDIUM_CANREAD 0x00000001 2 | #define FEATURE_MEDIUM_CANWRITE 0x00000002 3 | #define FEATURE_SLOT_GBA 0x00000010 4 | #define FEATURE_SLOT_NDS 0x00000020 5 | 6 | #define FIX_ALL 0x01 7 | #define FIX_GLUE 0x02 8 | #define FIX_GOT 0x04 9 | #define FIX_BSS 0x08 10 | 11 | #define DLDI_SIZE_32KB 0x0f 12 | #define DLDI_SIZE_16KB 0x0e 13 | #define DLDI_SIZE_8KB 0x0d 14 | #define DLDI_SIZE_4KB 0x0c 15 | #define DLDI_SIZE_1KB 0x0a 16 | -------------------------------------------------------------------------------- /include/nds/arm9/dynamicArray.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | A simple vector like dynamic data structure 4 | 5 | Copyright (C) 2005 6 | Jason Rogers (dovoto) 7 | 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 2. Altered source versions must be plainly marked as such, and 21 | must not be misrepresented as being the original software. 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | 26 | ---------------------------------------------------------------------------------*/ 27 | /*! \file dynamicArray.h 28 | \brief A dynamically resizing array for general use. 29 | */ 30 | 31 | #ifndef __DYNAMICARRAY_H__ 32 | #define __DYNAMICARRAY_H__ 33 | 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | 40 | //! A resizable array 41 | typedef struct DynamicArray 42 | { 43 | void** data; //!< pointer to array of void pointers 44 | unsigned int cur_size; //!< currently allocated size of the array 45 | }DynamicArray; 46 | 47 | 48 | 49 | /*! \brief Initializes an array with the supplied initial size 50 | \param v the array to initialize 51 | \param initialSize the initial size to allocate 52 | \return a pointer to the data, or NULL on error. 53 | */ 54 | void* DynamicArrayInit(DynamicArray* v, unsigned int initialSize); 55 | 56 | 57 | /*! \brief Frees memory allocated by the dynamic array 58 | \param v The array to delete 59 | */ 60 | void DynamicArrayDelete(DynamicArray* v); 61 | 62 | 63 | /*! \brief Gets the entry at the supplied index 64 | \param v The array to get from. 65 | \param index The index of the data to get. 66 | \return The data or NULL if v is NULL or the index is out of range. 67 | */ 68 | void* DynamicArrayGet(DynamicArray* v, unsigned int index); 69 | 70 | 71 | /*! \brief Sets the entry to the supplied value 72 | \param v The array to set 73 | \param index The index of the data to set (array will be resized to fit the index). 74 | \param item The data to set. 75 | \return false if v is NULL or there isn't enough memory, true otherwise 76 | */ 77 | bool DynamicArraySet(DynamicArray *v, unsigned int index, void* item); 78 | 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /include/nds/arm9/exceptions.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Dave Murphy (WinterMute) 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any 8 | damages arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any 11 | purpose, including commercial applications, and to alter it and 12 | redistribute it freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you 15 | must not claim that you wrote the original software. If you use 16 | this software in a product, an acknowledgment in the product 17 | documentation would be appreciated but is not required. 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | 23 | ---------------------------------------------------------------------------------*/ 24 | #ifndef _exceptions_h_ 25 | #define _exceptions_h_ 26 | //--------------------------------------------------------------------------------- 27 | 28 | #include "../ndstypes.h" 29 | #include 30 | #include 31 | 32 | /** \file 33 | \brief functions to handle hardware exceptions. 34 | */ 35 | 36 | #define EXCEPTION_VECTOR (*(VoidFn *)MM_ENV_EXCPT_VECTOR) 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | //! Exception context structure. 43 | typedef struct ExcptContext { 44 | u32 r[16]; 45 | u32 cpsr; 46 | u32 spsr; 47 | u32 cp15cr; 48 | u32 excpt_cpsr; 49 | } ExcptContext; 50 | 51 | //! Exception handler function type. 52 | typedef void (* ExcptHandler)(ExcptContext* ctx, unsigned flags); 53 | 54 | //! sets a custom hardware exception handler. 55 | static inline void setExceptionHandler(ExcptHandler handler) 56 | { 57 | extern ExcptHandler g_excptHandler; 58 | void __libnds_excpt(void); 59 | 60 | EXCEPTION_VECTOR = __libnds_excpt; 61 | g_excptHandler = handler; 62 | } 63 | 64 | //! sets the default hardware exception handler. 65 | void defaultExceptionHandler(void); 66 | 67 | //! prints the given exception context. 68 | void guruMeditationDump(ExcptContext* ctx, unsigned flags); 69 | 70 | //! decodes the fault address from a given opcode. 71 | u32 getExceptionAddress(const ExcptContext* ctx, u32 opcodeAddress); 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | //--------------------------------------------------------------------------------- 78 | #endif // _exceptions_h_ 79 | //--------------------------------------------------------------------------------- 80 | -------------------------------------------------------------------------------- /include/nds/arm9/guitarGrip.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | This software is provided 'as-is', without any express or implied 4 | warranty. In no event will the authors be held liable for any 5 | damages arising from the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any 8 | purpose, including commercial applications, and to alter it and 9 | redistribute it freely, subject to the following restrictions: 10 | 11 | 1. The origin of this software must not be misrepresented; you 12 | must not claim that you wrote the original software. If you use 13 | this software in a product, an acknowledgment in the product 14 | documentation would be appreciated but is not required. 15 | 2. Altered source versions must be plainly marked as such, and 16 | must not be misrepresented as being the original software. 17 | 3. This notice may not be removed or altered from any source 18 | distribution. 19 | 20 | ---------------------------------------------------------------------------------*/ 21 | 22 | /*! \file guitarGrip.h 23 | \brief guitar grip device slot-2 addon support. 24 | */ 25 | #ifndef GUITARGRIP_HEADER_INCLUDE 26 | #define GUITARGRIP_HEADER_INCLUDE 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | #define GUITARGRIP_GREEN BIT(6) 33 | #define GUITARGRIP_RED BIT(5) 34 | #define GUITARGRIP_YELLOW BIT(4) 35 | #define GUITARGRIP_BLUE BIT(3) 36 | 37 | 38 | /*! \fn bool guitarGripIsInserted() 39 | \brief Check for the guitar grip 40 | \return true if that's what is in the slot-2 41 | */ 42 | bool guitarGripIsInserted(); 43 | 44 | /*! \fn void guitarGripScanKeys() 45 | \brief Obtain the current guitar grip state. 46 | Call this function once per main loop to use the guitarGrip functions. 47 | */ 48 | void guitarGripScanKeys(); 49 | 50 | //! Obtains the current guitar grip keys held state 51 | u8 guitarGripKeysHeld(); 52 | 53 | //! Obtains the current guitar grip keys pressed state 54 | u16 guitarGripKeysDown(); 55 | 56 | //! Obtains the current guitar grip keys released state 57 | u16 guitarGripKeysUp(); 58 | 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | 64 | #endif 65 | 66 | -------------------------------------------------------------------------------- /include/nds/arm9/image.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Jason Rogers (dovoto) 5 | Dave Murphy (WinterMute) 6 | 7 | This software is provided 'as-is', without any express or implied 8 | warranty. In no event will the authors be held liable for any 9 | damages arising from the use of this software. 10 | 11 | Permission is granted to anyone to use this software for any 12 | purpose, including commercial applications, and to alter it and 13 | redistribute it freely, subject to the following restrictions: 14 | 15 | 1. The origin of this software must not be misrepresented; you 16 | must not claim that you wrote the original software. If you use 17 | this software in a product, an acknowledgment in the product 18 | documentation would be appreciated but is not required. 19 | 2. Altered source versions must be plainly marked as such, and 20 | must not be misrepresented as being the original software. 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | 24 | ---------------------------------------------------------------------------------*/ 25 | /*! \file image.h 26 | \brief An image abstraction for working with image data. 27 | 28 |
29 | Image data buffers must be allocated using malloc() rather than pointing to stack data, as the conversion 30 | routines will free() the argument's image buffer and allocate a new block for the replacement data. 31 | As such, any loader implemented utilizing this structure must use malloc() to allocate the image 32 | buffer. 33 |
34 | */ 35 | 36 | #ifndef IMAGE_H 37 | #define IMAGE_H 38 | 39 | #include 40 | 41 | //! \brief holds a red green blue triplet 42 | typedef struct RGB_24 43 | { 44 | unsigned char r; //!< 8 bits for the red value. 45 | unsigned char g; //!< 8 bits for the green value. 46 | unsigned char b; //!< 8 bits for the blue value. 47 | } __attribute__ ((packed)) RGB_24; 48 | 49 | 50 | 51 | //! A generic image structure. 52 | typedef struct sImage 53 | { 54 | short height; /*!< \brief The height of the image in pixels */ 55 | short width; /*!< \brief The width of the image in pixels */ 56 | int bpp; /*!< \brief Bits per pixel (should be 4, 8, 16, or 24) */ 57 | unsigned short* palette; /*!< \brief A pointer to the palette data */ 58 | 59 | //! A union of data pointers to the pixel data. 60 | union 61 | { 62 | u8* data8; //!< pointer to 8 bit data. 63 | u16* data16; //!< pointer to 16 bit data. 64 | u32* data32; //!< pointer to 32 bit data. 65 | } image; 66 | 67 | } sImage, *psImage; 68 | 69 | #ifdef __cplusplus 70 | extern "C" { 71 | #endif 72 | 73 | /*! \brief Destructively converts a 24-bit image to 16-bit 74 | \param img a pointer to image to manipulate 75 | */ 76 | void image24to16(sImage* img); 77 | 78 | /*! \brief Destructively converts an 8-bit image to 16 bit setting the alpha bit 79 | \param img a pointer to image to manipulate 80 | */ 81 | void image8to16(sImage* img); 82 | 83 | /*! \brief Destructively converts an 8-bit image to 16-bit with alpha bit cleared for the supplied palette index 84 | \param img a pointer to image to manipulate 85 | \param transparentColor Color indexes equal to this value will have the alpha bit clear 86 | */ 87 | void image8to16trans(sImage* img, u8 transparentColor); 88 | 89 | /*! \brief frees the image data. Only call if the image data was returned from an image loader 90 | \param img a pointer to image to manipulate (the image data will be free() ) 91 | */ 92 | void imageDestroy(sImage* img); 93 | 94 | /*! \brief Tiles 8-bit image data into a sequence of 8x8 tiles 95 | \param img a pointer to image to manipulate 96 | */ 97 | void imageTileData(sImage* img); 98 | 99 | #ifdef __cplusplus 100 | } 101 | #endif 102 | 103 | //why is this included here? 104 | #include 105 | 106 | #endif 107 | -------------------------------------------------------------------------------- /include/nds/arm9/input.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | key input code -- provides slightly higher level input forming 4 | 5 | Copyright (C) 2005 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Christian Auby (DesktopMan) 9 | Dave Murphy (WinterMute) 10 | 11 | This software is provided 'as-is', without any express or implied 12 | warranty. In no event will the authors be held liable for any 13 | damages arising from the use of this software. 14 | 15 | Permission is granted to anyone to use this software for any 16 | purpose, including commercial applications, and to alter it and 17 | redistribute it freely, subject to the following restrictions: 18 | 19 | 1. The origin of this software must not be misrepresented; you 20 | must not claim that you wrote the original software. If you use 21 | this software in a product, an acknowledgment in the product 22 | documentation would be appreciated but is not required. 23 | 2. Altered source versions must be plainly marked as such, and 24 | must not be misrepresented as being the original software. 25 | 3. This notice may not be removed or altered from any source 26 | distribution. 27 | 28 | 29 | ---------------------------------------------------------------------------------*/ 30 | /*! \file 31 | \brief NDS button and touchscreen input support. 32 | 33 | The state of the keypad must be read from hardware into memory using scanKeys() whenever 34 | you want an updated input state. After reading, call one of the associated "keys" functions to see 35 | what event was triggered. These events are computed as the difference between the current and previous 36 | key state you read. It's generally a good idea to scan keys frequently to insure your application's input system 37 | is responsive.\n 38 | 39 | After reading the key state, you will be given an integer representing which keys are in the requested state, 40 | to mask of specific buttons, use the key masks described in nds/input.h . 41 | \see nds/input.h available key masks on the Nintendo DS 42 | 43 | */ 44 | 45 | //--------------------------------------------------------------------------------- 46 | #ifndef INPUT_HEADER_INCLUDE 47 | #define INPUT_HEADER_INCLUDE 48 | //--------------------------------------------------------------------------------- 49 | #include 50 | #include 51 | 52 | #ifdef __cplusplus 53 | extern "C" { 54 | #endif 55 | 56 | /*! \brief Obtains the current keypad state. 57 | Call this function once per main loop in order to use the keypad functions. 58 | */ 59 | void scanKeys(void); 60 | 61 | /*! \brief Obtains the current keypad state. 62 | Call this function to get keypad state without affecting state of other key functions (keysUp keysHeld etc...) 63 | */ 64 | u32 keysCurrent(void); 65 | 66 | //! Obtains the current keypad held state. 67 | u32 keysHeld(void); 68 | 69 | //! Obtains the current keypad pressed state. 70 | u32 keysDown(void); 71 | 72 | //! Obtains the current keypad pressed or repeating state. 73 | u32 keysDownRepeat(void); 74 | 75 | /*! \brief Sets the key repeat parameters. 76 | \param setDelay Number of %scanKeys calls before keys start to repeat. 77 | \param setRepeat Number of %scanKeys calls before keys repeat. 78 | */ 79 | void keysSetRepeat( u8 setDelay, u8 setRepeat ); 80 | 81 | //! Obtains the current keypad released state. 82 | u32 keysUp(void); 83 | 84 | #ifdef __cplusplus 85 | } 86 | #endif 87 | 88 | //--------------------------------------------------------------------------------- 89 | #endif // INPUT_HEADER_INCLUDE 90 | //--------------------------------------------------------------------------------- 91 | -------------------------------------------------------------------------------- /include/nds/arm9/linkedlist.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | A simple linked list data structure 4 | 5 | Copyright (C) 2008 6 | Jason Rogers (dovoto) 7 | 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 2. Altered source versions must be plainly marked as such, and 21 | must not be misrepresented as being the original software. 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | 26 | ---------------------------------------------------------------------------------*/ 27 | /*! \file 28 | \brief A simple doubly linked, unsorted list implementation. 29 | */ 30 | 31 | 32 | #ifndef __LINKEDLIST_H__ 33 | #define __LINKEDLIST_H__ 34 | 35 | #include 36 | 37 | 38 | //! A node for the linked list. 39 | typedef struct LinkedList{ 40 | struct LinkedList *next; //!< A pointer to the next node. 41 | struct LinkedList *prev; //!< A pointer to the previous node. 42 | void *data; //!< A pointer to some data. 43 | }LinkedList; 44 | 45 | 46 | /*! 47 | \brief Adds data to a linked list. 48 | 49 | This will only store the pointer to the data, so you have to make sure that the pointer stays valid. 50 | 51 | \param front A pointer to a pointer to the front of the linked list (or a pointer to NULL if you don't have a linked list yet). 52 | \param data A pointer to the data you want to store. 53 | 54 | \return A pointer to the new node, which is also the new front, or NULL if there is not enough memory. 55 | */ 56 | LinkedList* linkedlistAdd(LinkedList **front, void* data); 57 | 58 | /*! 59 | \brief Removes a node from a linked list. 60 | 61 | The data pointer of the node will be lost after this, so make sure you don't need it anymore. 62 | 63 | \param node The node you want to remove. 64 | */ 65 | void linkedlistRemove(LinkedList *node); 66 | 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /include/nds/arm9/paddle.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | This software is provided 'as-is', without any express or implied 4 | warranty. In no event will the authors be held liable for any 5 | damages arising from the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any 8 | purpose, including commercial applications, and to alter it and 9 | redistribute it freely, subject to the following restrictions: 10 | 11 | 1. The origin of this software must not be misrepresented; you 12 | must not claim that you wrote the original software. If you use 13 | this software in a product, an acknowledgment in the product 14 | documentation would be appreciated but is not required. 15 | 2. Altered source versions must be plainly marked as such, and 16 | must not be misrepresented as being the original software. 17 | 3. This notice may not be removed or altered from any source 18 | distribution. 19 | 20 | ---------------------------------------------------------------------------------*/ 21 | 22 | /*! \file paddle.h 23 | \brief paddle device slot-2 addon support. 24 | */ 25 | #ifndef PADDLE_HEADER_INCLUDE 26 | #define PADDLE_HEADER_INCLUDE 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | 33 | /*! \fn bool paddleIsInserted() 34 | \brief Check for the paddle 35 | \return true if that's what is in the slot-2 36 | */ 37 | bool paddleIsInserted(); 38 | 39 | /*! \fn void paddleRead() 40 | \brief Obtain the current paddle state 41 | \return a u16 containing a 12bit number (fixed point fraction), incrementing for clockwise rotations and decrementing for counterclockwise 42 | */ 43 | u16 paddleRead(); 44 | 45 | //! Resets the paddle device. May change the current value to 0xFFF, 0x000, or 0x001. May perform other unknown internal reset operations. Normally not needed. 46 | void paddleReset(); 47 | 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif 54 | 55 | -------------------------------------------------------------------------------- /include/nds/arm9/pcx.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | $Id: pcx.h,v 1.4 2008-04-15 02:18:41 dovoto Exp $ 3 | 4 | 5 | Copyright (C) 2005 6 | Jason Rogers (dovoto) 7 | Dave Murphy (WinterMute) 8 | 9 | This software is provided 'as-is', without any express or implied 10 | warranty. In no event will the authors be held liable for any 11 | damages arising from the use of this software. 12 | 13 | Permission is granted to anyone to use this software for any 14 | purpose, including commercial applications, and to alter it and 15 | redistribute it freely, subject to the following restrictions: 16 | 17 | 1. The origin of this software must not be misrepresented; you 18 | must not claim that you wrote the original software. If you use 19 | this software in a product, an acknowledgment in the product 20 | documentation would be appreciated but is not required. 21 | 2. Altered source versions must be plainly marked as such, and 22 | must not be misrepresented as being the original software. 23 | 3. This notice may not be removed or altered from any source 24 | distribution. 25 | 26 | 27 | 28 | 29 | ---------------------------------------------------------------------------------*/ 30 | /*! \file pcx.h 31 | \brief A simple 256 color pcx file loader. 32 | */ 33 | 34 | #ifndef PCX_H 35 | #define PCX_H 36 | 37 | typedef struct PCXHeader 38 | { 39 | char manufacturer; //should be 0 40 | char version; //should be 5 41 | char encoding; //should be 1 42 | char bitsPerPixel; //should be 8 43 | short int xmin,ymin; //coordinates for top left,bottom right 44 | short int xmax,ymax; 45 | short int hres; //resolution 46 | short int vres; 47 | char palette16[48]; //16 color palette if 16 color image 48 | char reserved; //ignore 49 | char colorPlanes; //ignore 50 | short int bytesPerLine; 51 | short int paletteYype; //should be 2 52 | char filler[58]; //ignore 53 | }__attribute__ ((packed)) PCXHeader, *pPCXHeader; 54 | 55 | #ifdef __cplusplus 56 | extern "C" { 57 | #endif 58 | 59 | /*! \brief Loads an image structure with data from PCX formatted data 60 | \param pcx a pointer to the pcx file loaded into memory 61 | \param image the image structure to fill in (the loader will allocate room for the palette and pixel data) 62 | \return 1 on success, 0 on failure 63 | */ 64 | int loadPCX(const unsigned char* pcx, sImage* image); 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif 71 | 72 | -------------------------------------------------------------------------------- /include/nds/arm9/piano.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2011 4 | Tobias Weyand (0xtob) 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any 8 | damages arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any 11 | purpose, including commercial applications, and to alter it and 12 | redistribute it freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you 15 | must not claim that you wrote the original software. If you use 16 | this software in a product, an acknowledgment in the product 17 | documentation would be appreciated but is not required. 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | 23 | ---------------------------------------------------------------------------------*/ 24 | 25 | /*! \file piano.h 26 | \brief NDS Easy Piano option pack support. 27 | */ 28 | #ifndef PIANO_HEADER_INCLUDE 29 | #define PIANO_HEADER_INCLUDE 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | #define PIANO_PAK (*(vu16 *)0x09FFFFFE) 36 | 37 | #define PIANO_C BIT(0) 38 | #define PIANO_CS BIT(1) 39 | #define PIANO_D BIT(2) 40 | #define PIANO_DS BIT(3) 41 | #define PIANO_E BIT(4) 42 | #define PIANO_F BIT(5) 43 | #define PIANO_FS BIT(6) 44 | #define PIANO_G BIT(7) 45 | #define PIANO_GS BIT(8) 46 | #define PIANO_A BIT(9) 47 | #define PIANO_AS BIT(10) 48 | #define PIANO_B BIT(13) 49 | #define PIANO_C2 BIT(14) 50 | 51 | /*! \fn bool pianoIsInserted() 52 | \brief Check for piano option pack. 53 | \return true if the cart in the GBA slot is the piano option pack. 54 | */ 55 | bool pianoIsInserted(); 56 | 57 | /*! \fn void pianoScanKeys() 58 | \brief Obtain the current piano state. 59 | Call this function once per main loop to use the piano functions. 60 | */ 61 | void pianoScanKeys(); 62 | 63 | //! Obtains the current piano keys held state 64 | u16 pianoKeysHeld(); 65 | 66 | //! Obtains the current piano keys pressed state 67 | u16 pianoKeysDown(); 68 | 69 | //! Obtains the current piano keys released state 70 | u16 pianoKeysUp(); 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | #endif 77 | 78 | -------------------------------------------------------------------------------- /include/nds/arm9/postest.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | PosTest.c -- Code for performing hardware position testing 4 | 5 | Copyright (C) 2007 6 | Gabe Ghearing (gabebear) 7 | 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 21 | 2. Altered source versions must be plainly marked as such, and 22 | must not be misrepresented as being the original software. 23 | 24 | 3. This notice may not be removed or altered from any source 25 | distribution. 26 | 27 | ---------------------------------------------------------------------------------*/ 28 | 29 | /*! \file postest.h 30 | \brief Position Test Functions.
31 | 32 |
33 | The position test multiplies a given vector by the position matrix and returns the coords(x,y,z,w). The position test is really quick, about 10x faster than a box test. 34 |
35 | */ 36 | 37 | #ifndef POS_TEST_INCLUDE 38 | #define POS_TEST_INCLUDE 39 | 40 | #include 41 | #include 42 | 43 | 44 | GL_STATIC_INL 45 | /*! \brief true if the hardware is currently performing a position/vertex/box test. 46 | \return whether a test is being performed 47 | */ 48 | bool PosTestBusy() 49 | { 50 | return (GFX_STATUS & BIT(0))!=0; 51 | } 52 | 53 | GL_STATIC_INL 54 | /*! \brief Starts a position test asynchronously 55 | \param x specifies x offset from the current modelview matrix 56 | \param y specifies y offset from the current modelview matrix 57 | \param z specifies z offset from the current modelview matrix 58 | */ 59 | void PosTest_Asynch(v16 x, v16 y, v16 z) 60 | { 61 | GFX_POS_TEST = VERTEX_PACK(x, y); 62 | GFX_POS_TEST = z; 63 | } 64 | 65 | 66 | GL_STATIC_INL 67 | /*! \brief Performs a position test 68 | \param x specifies x offset from the current modelview matrix 69 | \param y specifies y offset from the current modelview matrix 70 | \param z specifies z offset from the current modelview matrix 71 | */ 72 | void PosTest(v16 x, v16 y, v16 z) 73 | { 74 | PosTest_Asynch(x,y,z); 75 | while(PosTestBusy()); 76 | } 77 | 78 | GL_STATIC_INL 79 | /*! \brief Returns the distance from the camera of the last position test, pretty darn useful. 80 | \return W magnitude 81 | */ 82 | s32 PosTestWresult() 83 | { 84 | return GFX_POS_RESULT[3]; 85 | } 86 | 87 | GL_STATIC_INL 88 | /*! \brief Returns absolute X position of the last position test (location if the modelview matrix was identity) 89 | \return Absolute X position 90 | */ 91 | s32 PosTestXresult() 92 | { 93 | return GFX_POS_RESULT[0]; 94 | } 95 | 96 | GL_STATIC_INL 97 | /*! \brief Returns absolute Y position of the last position test (location if the modelview matrix was identity) 98 | \return Absolute Y position 99 | */ 100 | s32 PosTestYresult() 101 | { 102 | return GFX_POS_RESULT[1]; 103 | } 104 | 105 | GL_STATIC_INL 106 | /*! \brief Returns absolute Z position of the last position test (location if the modelview matrix was identity) 107 | \return Absolute Z position 108 | */ 109 | s32 PosTestZresult() 110 | { 111 | return GFX_POS_RESULT[2]; 112 | } 113 | 114 | #endif // ifndef POS_TEST_INCLUDE 115 | 116 | -------------------------------------------------------------------------------- /include/nds/arm9/rumble.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Michael Noland (joat) 5 | Jason Rogers (dovoto) 6 | Dave Murphy (WinterMute) 7 | Mike Parks (BigRedPimp) 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | Permission is granted to anyone to use this software for any 12 | purpose, including commercial applications, and to alter it and 13 | redistribute it freely, subject to the following restrictions: 14 | 15 | 1. The origin of this software must not be misrepresented; you 16 | must not claim that you wrote the original software. If you use 17 | this software in a product, an acknowledgment in the product 18 | documentation would be appreciated but is not required. 19 | 2. Altered source versions must be plainly marked as such, and 20 | must not be misrepresented as being the original software. 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | 24 | ---------------------------------------------------------------------------------*/ 25 | /*! \file rumble.h 26 | \brief nds rumble option pak support. 27 | */ 28 | #ifndef RUMBLE_HEADER_INCLUDE 29 | #define RUMBLE_HEADER_INCLUDE 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | #define RUMBLE_PAK (*(vu16 *)0x08001000) 36 | #define WARIOWARE_PAK (*(vu16 *)0x080000C4) 37 | #define WARIOWARE_ENABLE (*(vu16 *)0x080000C6) 38 | 39 | typedef enum { 40 | RUMBLE, 41 | WARIOWARE 42 | }RUMBLE_TYPE; 43 | 44 | /*! \fn bool rumbleIsInserted(void); 45 | \brief Check for rumble option pak. 46 | \return true if the cart in the GBA slot is a Rumble option pak. 47 | */ 48 | bool rumbleIsInserted(void); 49 | 50 | /*! \fn void rumbleSet(bool position); 51 | \param position Alternates position of the actuator in the pak 52 | \brief Fires the rumble actuator. 53 | */ 54 | void rumbleSet(bool position); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /include/nds/arm9/sassert.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | sassert.h -- definitons for DS assertions 4 | 5 | Copyright (C) 2013 6 | Dave Murphy (WinterMute) 7 | Jason Rogers (Dovoto) 8 | Michael Theall (mtheall) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 23 | 2. Altered source versions must be plainly marked as such, and 24 | must not be misrepresented as being the original software. 25 | 26 | 3. This notice may not be removed or altered from any source 27 | distribution. 28 | 29 | ---------------------------------------------------------------------------------*/ 30 | /*! \file sassert.h 31 | \brief Simple assertion with a message conplies to nop if NDEBUG is defined 32 | */ 33 | 34 | #ifndef _sassert_h_ 35 | #define _sassert_h_ 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | #include "_ansi.h" 42 | 43 | #undef sassert 44 | 45 | #ifdef NDEBUG /* required by ANSI standard */ 46 | #define sassert(e, ...) ((void)0) 47 | #else 48 | //! Causes a blue screen of death if e is not true with the msg "msg" displayed 49 | #define sassert(e,...) ((e) ? (void)0 : __sassert (__FILE__, __LINE__, #e, __VA_ARGS__)) 50 | 51 | #endif /* NDEBUG */ 52 | 53 | void __sassert(const char *fileName, int lineNumber, const char* conditionString, const char* format, ...) 54 | __attribute__((format(printf,4,5))); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif // _sassert_h_ 61 | -------------------------------------------------------------------------------- /include/nds/arm9/trig_lut.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Trig_lut.h provides access to external precompiled trig look up tables 4 | 5 | Copyright (C) 2005 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Dave Murphy (WinterMute) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 3. This notice may not be removed or altered from any source 25 | distribution. 26 | 27 | 28 | 29 | ---------------------------------------------------------------------------------*/ 30 | #ifndef TRIG_LUT_H 31 | #define TRIG_LUT_H 32 | 33 | #include 34 | 35 | /*! \file trig_lut.h 36 | 37 | \brief fixed point trig functions. Angle can be in the range of -32768 to 38 | 32767. There are 32768 degrees in the unit circle used by nds. To convert 39 | between standard degrees (360 per circle): 40 | 41 | angle = degreesToAngle(angleInDegrees); 42 | 43 | or 44 | 45 | angle = angleInDegrees * 32768 / 360; 46 | 47 | This unit of measure is sometimes refered to as a binary radian (brad) or binary 48 | degree. It allows for more precise representation of angle and faster calculation 49 | as the DS has no floating point processor. 50 | */ 51 | 52 | 53 | #ifdef __cplusplus 54 | extern "C" { 55 | #endif 56 | 57 | //! number of degrees in a circle. 58 | #define DEGREES_IN_CIRCLE (1 << 15) 59 | 60 | /*! \brief convert a fixed point number to an integer. 61 | \param n the number the number to convert. 62 | \param bits the number of bits used for the decimal part. 63 | \return the integer part. 64 | */ 65 | #define fixedToInt(n, bits) ((int)((n)>>(bits))) 66 | 67 | /*! \brief converts an integer to a fixed point number. 68 | \param n the integer to convert. 69 | \param bits the number of bits used for the decimal part. 70 | \return the fixed point number. 71 | */ 72 | #define intToFixed(n, bits) ((int)((n)<<(bits))) 73 | 74 | 75 | /*! \brief converts a floating point number to a fixed point number. 76 | \param n the floating point number to convert. 77 | \param bits the number of bits used for the decimal part. 78 | \return the fixed point number. 79 | */ 80 | #define floatToFixed(n, bits) ((int)((n) * (float)(1<<(bits)))) 81 | 82 | /*! \brief converts a fixed point number to a floating point number. 83 | \param n the fixed point number to convert. 84 | \param bits the number of bits used for the decimal part. 85 | \return the floating point number. 86 | */ 87 | #define fixedToFloat(n, bits) (((float)(n)) / (float)(1<<(bits))) 88 | 89 | /*! \brief removes the decimal part of a fixed point number. 90 | \param n the fixed point number. 91 | \param bits the number of bits used for the decimal part. 92 | \return a fixed point number with 0 as a decimal part. 93 | */ 94 | #define floorFixed(n, bits) ((int)((n) & ~(((1 << (bits)) - 1)))) 95 | 96 | 97 | //! convert an angle in 360 degree format to the format used by libnds. 98 | #define degreesToAngle(degrees) ((degrees) * DEGREES_IN_CIRCLE / 360) 99 | //! converts an angle in the format used by libnds in the 360 degree format. 100 | #define angleToDegrees(angle) ((angle) * 360 / DEGREES_IN_CIRCLE) 101 | 102 | 103 | 104 | 105 | /*! \brief fixed point sine 106 | \param angle (-32768 to 32767) 107 | \return 4.12 fixed point number with the range [-1, 1] 108 | */ 109 | s16 sinLerp(s16 angle); 110 | 111 | /*! \brief fixed point cosine 112 | \param angle (-32768 to 32767) 113 | \return 4.12 fixed point number with the range [-1, 1] 114 | */ 115 | s16 cosLerp(s16 angle); 116 | 117 | /*! \brief fixed point tangent 118 | \param angle (-32768 to 32767) 119 | \return 20.12 fixed point number with the range [-81.483, 524287.999] 120 | */ 121 | s32 tanLerp(s16 angle); 122 | 123 | 124 | /*! \brief fixed point arcsin 125 | \param par 4.12 fixed point number with the range [-1, 1] 126 | \return s16 angle (-32768 to 32767) 127 | */ 128 | s16 asinLerp(s16 par); 129 | 130 | /*! \brief fixed point arccos 131 | \param par 4.12 fixed point number with the range [-1, 1] 132 | \return s16 angle (-32768 to 32767) 133 | */ 134 | s16 acosLerp(s16 par); 135 | 136 | 137 | 138 | 139 | 140 | #ifdef __cplusplus 141 | } 142 | #endif 143 | 144 | #endif 145 | -------------------------------------------------------------------------------- /include/nds/arm9/window.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | window.h -- definitions for object and background windowing 4 | 5 | Copyright (C) 2007 6 | Dave Murphy (WinterMute) 7 | Jason Rogers (Dovoto) 8 | 9 | This software is provided 'as-is', without any express or implied 10 | warranty. In no event will the authors be held liable for any 11 | damages arising from the use of this software. 12 | 13 | Permission is granted to anyone to use this software for any 14 | purpose, including commercial applications, and to alter it and 15 | redistribute it freely, subject to the following restrictions: 16 | 17 | 1. The origin of this software must not be misrepresented; you 18 | must not claim that you wrote the original software. If you use 19 | this software in a product, an acknowledgment in the product 20 | documentation would be appreciated but is not required. 21 | 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 25 | 3. This notice may not be removed or altered from any source 26 | distribution. 27 | 28 | ---------------------------------------------------------------------------------*/ 29 | /*! \file window.h 30 | \brief windowing support functions for objects and backgrounds 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | /*! \brief the supported windows*/ 42 | typedef enum { 43 | WINDOW_0 = DISPLAY_WIN0_ON, //!< Window 0. 44 | WINDOW_1 = DISPLAY_WIN1_ON, //!< Window 1 45 | WINDOW_OBJ = DISPLAY_SPR_WIN_ON, //!< Object window 46 | WINDOW_OUT = BIT(16), //!< Area outside all windows 47 | 48 | }WINDOW; 49 | 50 | #define WINDOW_MASK (WINDOW_0|WINDOW_1|WINDOW_OBJ) 51 | 52 | static inline 53 | /** 54 | * \brief Enable the specified window(s) 55 | * \param window The window to set bounds on (may be ORed together) 56 | */ 57 | void windowEnable(WINDOW w) { REG_DISPCNT |= w & WINDOW_MASK; } 58 | 59 | static inline 60 | /** 61 | * \brief Enable the specified window(s) 62 | * \param window The window to set bounds on (may be ORed together) 63 | */ 64 | void windowDisable(WINDOW w) { REG_DISPCNT &= ~(w & WINDOW_MASK); } 65 | 66 | static inline 67 | /** 68 | * \brief Enable the specified window(s) 69 | * \param window The window to set bounds on (may be ORed together) 70 | */ 71 | void windowEnableSub(WINDOW w) { REG_DISPCNT_SUB |= w & WINDOW_MASK; } 72 | 73 | static inline 74 | /** 75 | * \brief Enable the specified window(s) 76 | * \param window The window to set bounds on (may be ORed together) 77 | */ 78 | void windowDisableSub(WINDOW w) { REG_DISPCNT_SUB &= ~(w & WINDOW_MASK); } 79 | 80 | /** 81 | * \brief Set the windows bounds 82 | * \param window The window to set bounds on 83 | * \param left The X coordinate of the left hand side of the rectangle 84 | * \param top The Y coordinate of the top of the rectangle 85 | * \param right The X coordinate of the right hand side of the rectangle 86 | * \param bottom The Y coordinate of the bottom of the rectangle 87 | */ 88 | void windowSetBounds(WINDOW window, u8 left, u8 top, u8 right, u8 bottom); 89 | 90 | /** 91 | * \brief Set the windows bounds (Sub engine) 92 | * \param window The window to set bounds on 93 | * \param left The X coordinate of the left hand side of the rectangle 94 | * \param top The Y coordinate of the top of the rectangle 95 | * \param right The X coordinate of the right hand side of the rectangle 96 | * \param bottom The Y coordinate of the bottom of the rectangle 97 | */ 98 | void windowSetBoundsSub(WINDOW window, u8 left, u8 top, u8 right, u8 bottom); 99 | 100 | 101 | 102 | /*! \brief Enables the window on the supplied background. 103 | \param id 104 | background id returned from bgInit or bgInitSub 105 | \param window 106 | the the window to enable 107 | */ 108 | void bgWindowEnable(int id, WINDOW window); 109 | /*! \brief Disables the window on the supplied background. 110 | \param id 111 | background id returned from bgInit or bgInitSub 112 | \param window 113 | the the window to disable 114 | */ 115 | void bgWindowDisable(int id, WINDOW window); 116 | 117 | /** 118 | * \brief Enables the specified window. 119 | * \param oam must be: &oamMain or &oamSub 120 | * \param the window to enable 121 | */ 122 | void oamWindowEnable(OamState* oam, WINDOW w); 123 | 124 | /** 125 | * \brief Disables the specified window. 126 | * \param oam must be: &oamMain or &oamSub 127 | * \param the window to disable 128 | */ 129 | void oamWindowDisable(OamState* oam, WINDOW w); -------------------------------------------------------------------------------- /include/nds/asminc.h: -------------------------------------------------------------------------------- 1 | #ifndef _ASMINC_H_ 2 | #define _ASMINC_H_ 3 | 4 | #if !__ASSEMBLER__ 5 | #error This header file is only for use in assembly files! 6 | #endif // !__ASSEMBLER__ 7 | 8 | 9 | .macro BEGIN_ASM_FUNC name section=text 10 | .section .\section\().\name\(), "ax", %progbits 11 | .global \name 12 | .type \name, %function 13 | .align 2 14 | \name: 15 | .endm 16 | 17 | #define ICACHE_SIZE 0x2000 18 | #define DCACHE_SIZE 0x1000 19 | #define CACHE_LINE_SIZE 32 20 | 21 | #endif // _ASMINC_H_ 22 | -------------------------------------------------------------------------------- /include/nds/card.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Michael Noland (joat) 5 | Jason Rogers (dovoto) 6 | Dave Murphy (WinterMute) 7 | 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 2. Altered source versions must be plainly marked as such, and 21 | must not be misrepresented as being the original software. 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | ---------------------------------------------------------------------------------*/ 26 | 27 | #ifndef NDS_CARD_INCLUDE 28 | #define NDS_CARD_INCLUDE 29 | 30 | #include "ndstypes.h" 31 | #include 32 | 33 | // Card bus 34 | #define REG_CARD_DATA_RD (*(vu32*)0x04100010) 35 | 36 | #define REG_AUXSPICNT (*(vu16*)0x040001A0) 37 | #define REG_AUXSPICNTH (*(vu8*)0x040001A1) 38 | #define REG_AUXSPIDATA (*(vu8*)0x040001A2) 39 | #define REG_ROMCTRL (*(vu32*)0x040001A4) 40 | 41 | #define REG_CARD_COMMAND ((vu8*)0x040001A8) 42 | 43 | #define REG_CARD_1B0 (*(vu32*)0x040001B0) 44 | #define REG_CARD_1B4 (*(vu32*)0x040001B4) 45 | #define REG_CARD_1B8 (*(vu16*)0x040001B8) 46 | #define REG_CARD_1BA (*(vu16*)0x040001BA) 47 | 48 | #define CARD_CR1_ENABLE 0x80 // in byte 1, i.e. 0x8000 49 | #define CARD_CR1_IRQ 0x40 // in byte 1, i.e. 0x4000 50 | 51 | // SPI EEPROM COMMANDS 52 | #define SPI_EEPROM_WRSR 0x01 53 | #define SPI_EEPROM_PP 0x02 // Page Program 54 | #define SPI_EEPROM_READ 0x03 55 | #define SPI_EEPROM_WRDI 0x04 // Write disable 56 | #define SPI_EEPROM_RDSR 0x05 // Read status register 57 | #define SPI_EEPROM_WREN 0x06 // Write enable 58 | #define SPI_EEPROM_PW 0x0a // Page Write 59 | #define SPI_EEPROM_FAST 0x0b // Fast Read 60 | #define SPI_EEPROM_RDID 0x9f 61 | #define SPI_EEPROM_RDP 0xab // Release from deep power down 62 | #define SPI_EEPROM_DPD 0xb9 // Deep power down 63 | 64 | #define CARD_ACTIVATE (1<<31) // when writing, get the ball rolling 65 | #define CARD_WR (1<<30) // Card write enable 66 | #define CARD_nRESET (1<<29) // value on the /reset pin (1 = high out, not a reset state, 0 = low out = in reset) 67 | #define CARD_SEC_LARGE (1<<28) // Use "other" secure area mode, which tranfers blocks of 0x1000 bytes at a time 68 | #define CARD_CLK_SLOW (1<<27) // Transfer clock rate (0 = 6.7MHz, 1 = 4.2MHz) 69 | #define CARD_BLK_SIZE(n) (((n)&0x7)<<24) // Transfer block size, (0 = None, 1..6 = (0x100 << n) bytes, 7 = 4 bytes) 70 | #define CARD_SEC_CMD (1<<22) // The command transfer will be hardware encrypted (KEY2) 71 | #define CARD_DELAY2(n) (((n)&0x3F)<<16) // Transfer delay length part 2 72 | #define CARD_SEC_SEED (1<<15) // Apply encryption (KEY2) seed to hardware registers 73 | #define CARD_SEC_EN (1<<14) // Security enable 74 | #define CARD_SEC_DAT (1<<13) // The data transfer will be hardware encrypted (KEY2) 75 | #define CARD_DELAY1(n) ((n)&0x1FFF) // Transfer delay length part 1 76 | 77 | // 3 bits in b10..b8 indicate something 78 | // read bits 79 | #define CARD_BUSY (1<<31) // when reading, still expecting incomming data? 80 | #define CARD_DATA_READY (1<<23) // when reading, CARD_DATA_RD or CARD_DATA has another word of data and is good to go 81 | 82 | // Card commands 83 | #define CARD_CMD_DUMMY 0x9F 84 | #define CARD_CMD_HEADER_READ 0x00 85 | #define CARD_CMD_HEADER_CHIPID 0x90 86 | #define CARD_CMD_ACTIVATE_BF 0x3C // Go into blowfish (KEY1) encryption mode 87 | #define CARD_CMD_ACTIVATE_SEC 0x40 // Go into hardware (KEY2) encryption mode 88 | #define CARD_CMD_SECURE_CHIPID 0x10 89 | #define CARD_CMD_SECURE_READ 0x20 90 | #define CARD_CMD_DISABLE_SEC 0x60 // Leave hardware (KEY2) encryption mode 91 | #define CARD_CMD_DATA_MODE 0xA0 92 | #define CARD_CMD_DATA_READ 0xB7 93 | #define CARD_CMD_DATA_CHIPID 0xB8 94 | 95 | //REG_AUXSPICNT 96 | #define CARD_ENABLE (1<<15) 97 | #define CARD_SPI_ENABLE (1<<13) 98 | #define CARD_SPI_BUSY (1<<7) 99 | #define CARD_SPI_HOLD (1<<6) 100 | 101 | #define CARD_SPICNTH_ENABLE (1<<7) // in byte 1, i.e. 0x8000 102 | #define CARD_SPICNTH_IRQ (1<<6) // in byte 1, i.e. 0x4000 103 | 104 | #ifdef __cplusplus 105 | extern "C" { 106 | #endif 107 | 108 | //--------------------------------------------------------------------------------- 109 | static inline void enableSlot1(void) { 110 | //--------------------------------------------------------------------------------- 111 | scfgSetMcPower(true); 112 | } 113 | 114 | //--------------------------------------------------------------------------------- 115 | static inline void disableSlot1(void) { 116 | //--------------------------------------------------------------------------------- 117 | scfgSetMcPower(false); 118 | } 119 | 120 | void cardWriteCommand(const u8 *command); 121 | void cardPolledTransfer(u32 flags, u32 *destination, u32 length, const u8 *command); 122 | void cardStartTransfer(const u8 *command, u32 *destination, int channel, u32 flags); 123 | u32 cardWriteAndRead(const u8 *command, u32 flags); 124 | void cardParamCommand (u8 command, u32 parameter, u32 flags, u32 *destination, u32 length); 125 | 126 | // These commands require the cart to not be initialized yet, which may mean the user 127 | // needs to eject and reinsert the cart or they will return random data. 128 | void cardReadHeader(u8 *header); 129 | u32 cardReadID(u32 flags); 130 | void cardReset(); 131 | 132 | //--------------------------------------------------------------------------------- 133 | static inline void eepromWaitBusy() { 134 | //--------------------------------------------------------------------------------- 135 | while (REG_AUXSPICNT & CARD_SPI_BUSY); 136 | } 137 | 138 | // Reads from the EEPROM 139 | void cardReadEeprom(u32 address, u8 *data, u32 length, u32 addrtype); 140 | 141 | // Writes to the EEPROM. TYPE 3 EEPROM must be erased first (I think?) 142 | void cardWriteEeprom(u32 address, u8 *data, u32 length, u32 addrtype); 143 | 144 | // Returns the ID of the EEPROM chip? Doesn't work well, most chips give ff,ff 145 | // i = 0 or 1 146 | u32 cardEepromReadID(); 147 | 148 | // Sends a command to the EEPROM 149 | u8 cardEepromCommand(u8 command); 150 | 151 | /* 152 | * -1:no card or no EEPROM 153 | * 0:unknown PassMe? 154 | * 1:TYPE 1 4Kbit(512Byte) EEPROM 155 | * 2:TYPE 2 64Kbit(8KByte)or 512kbit(64Kbyte) EEPROM 156 | * 3:TYPE 3 2Mbit(256KByte) FLASH MEMORY (some rare 4Mbit and 8Mbit chips also) 157 | */ 158 | int cardEepromGetType(void); 159 | 160 | // Returns the size in bytes of EEPROM 161 | u32 cardEepromGetSize(); 162 | 163 | // Erases the entire chip. TYPE 3 chips MUST be erased before writing to them. (I think?) 164 | void cardEepromChipErase(void); 165 | 166 | // Erases a single sector of the TYPE 3 chip 167 | void cardEepromSectorErase(u32 address); 168 | 169 | #ifdef __cplusplus 170 | } 171 | #endif 172 | 173 | 174 | #endif 175 | 176 | -------------------------------------------------------------------------------- /include/nds/debug.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Jason Rogers (dovoto) 5 | Dave Murphy (WinterMute) 6 | 7 | This software is provided 'as-is', without any express or implied 8 | warranty. In no event will the authors be held liable for any 9 | damages arising from the use of this software. 10 | 11 | Permission is granted to anyone to use this software for any 12 | purpose, including commercial applications, and to alter it and 13 | redistribute it freely, subject to the following restrictions: 14 | 15 | 1. The origin of this software must not be misrepresented; you 16 | must not claim that you wrote the original software. If you use 17 | this software in a product, an acknowledgment in the product 18 | documentation would be appreciated but is not required. 19 | 20 | 2. Altered source versions must be plainly marked as such, and 21 | must not be misrepresented as being the original software. 22 | 23 | 3. This notice may not be removed or altered from any source 24 | distribution. 25 | 26 | 27 | ---------------------------------------------------------------------------------*/ 28 | /*! \file debug.h 29 | \brief Currently only used to send debug messages to NO$GBA debug window 30 | 31 |
32 | On the ARM 9 this functionality is best accessed via the console studio integration. 33 | - \ref console.h "Debug Messages via stdio" 34 | 35 |
36 | */ 37 | 38 | #ifndef NDS_DEBUG_INCLUDE 39 | #define NDS_DEBUG_INCLUDE 40 | 41 | #include 42 | #include "ndstypes.h" 43 | 44 | #define REG_NOCASH_OUT_STRZ (*(vu32*)0x4fffa10) 45 | #define REG_NOCASH_OUT_CHAR (*(vu8*) 0x4fffa1c) 46 | 47 | /*! \brief Send a message to the no$gba debug window 48 | \param message The message to send 49 | \param len Length in characters of the message to send 50 | */ 51 | static inline void nocashWrite(const char *message, size_t len) 52 | { 53 | for (size_t i = 0; i < len; i ++) { 54 | REG_NOCASH_OUT_CHAR = message[i]; 55 | } 56 | } 57 | 58 | /*! \brief Send a message to the no$gba debug window 59 | \param message The message to send (zero-terminated string) 60 | */ 61 | static inline void nocashMessage(const char *message) 62 | { 63 | REG_NOCASH_OUT_STRZ = (u32)message; 64 | } 65 | 66 | #endif // NDS_DEBUG_INCLUDE 67 | 68 | -------------------------------------------------------------------------------- /include/nds/disc_io.h: -------------------------------------------------------------------------------- 1 | /* 2 | disc_io.h 3 | Interface template for low level disc functions. 4 | 5 | Copyright (c) 2006 Michael "Chishm" Chisholm 6 | Based on code originally written by MightyMax 7 | 8 | Redistribution and use in source and binary forms, with or without modification, 9 | are permitted provided that the following conditions are met: 10 | 11 | 1. Redistributions of source code must retain the above copyright notice, 12 | this list of conditions and the following disclaimer. 13 | 2. Redistributions in binary form must reproduce the above copyright notice, 14 | this list of conditions and the following disclaimer in the documentation and/or 15 | other materials provided with the distribution. 16 | 3. The name of the author may not be used to endorse or promote products derived 17 | from this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 20 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 21 | AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE 22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 27 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef NDS_DISC_IO_INCLUDE 31 | #define NDS_DISC_IO_INCLUDE 32 | 33 | #include 34 | 35 | #endif // define NDS_DISC_IO_INCLUDE 36 | -------------------------------------------------------------------------------- /include/nds/dldi.h: -------------------------------------------------------------------------------- 1 | /* 2 | dldi.h 3 | 4 | Copyright (c) 2006 Michael "Chishm" Chisholm 5 | 6 | Redistribution and use in source and binary forms, with or without modification, 7 | are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | 2. Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation and/or 13 | other materials provided with the distribution. 14 | 3. The name of the author may not be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 19 | AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 25 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | #include 35 | #include 36 | 37 | typedef DldiHeader DLDI_INTERFACE; 38 | 39 | /// Determine if a given DLDI driver structure is valid 40 | bool dldiIsValid(const DLDI_INTERFACE* io); 41 | 42 | /// Find a DLDI driver area within a given binary 43 | DLDI_INTERFACE* dldiFindDriverArea(void* bin, size_t bin_size); 44 | 45 | /// Adjust the pointer addresses within a DLDI driver for the target virtual address 46 | void dldiFixDriverAddresses(DLDI_INTERFACE* io, uptr dldi_vaddr); 47 | 48 | /// Patch a DLDI driver into a given area 49 | bool dldiApplyPatch(DLDI_INTERFACE* area, const DLDI_INTERFACE* io); 50 | 51 | /// Patch a binary using a given DLDI driver 52 | MK_INLINE bool dldiPatchBinary(void* bin, size_t bin_size, const DLDI_INTERFACE* io) 53 | { 54 | DLDI_INTERFACE* area = dldiFindDriverArea(bin, bin_size); 55 | return area ? dldiApplyPatch(area, io) : false; 56 | } 57 | 58 | #ifdef __cplusplus 59 | } 60 | #endif 61 | -------------------------------------------------------------------------------- /include/nds/input.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2010 4 | Jason Rogers (dovoto) 5 | Dave Murphy (WinterMute) 6 | 7 | This software is provided 'as-is', without any express or implied 8 | warranty. In no event will the authors be held liable for any 9 | damages arising from the use of this software. 10 | 11 | Permission is granted to anyone to use this software for any 12 | purpose, including commercial applications, and to alter it and 13 | redistribute it freely, subject to the following restrictions: 14 | 15 | 1. The origin of this software must not be misrepresented; you 16 | must not claim that you wrote the original software. If you use 17 | this software in a product, an acknowledgment in the product 18 | documentation would be appreciated but is not required. 19 | 2. Altered source versions must be plainly marked as such, and 20 | must not be misrepresented as being the original software. 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | 24 | ---------------------------------------------------------------------------------*/ 25 | #ifndef _INPUT_H_ 26 | #define _INPUT_H_ 27 | 28 | #include 29 | 30 | #define KEY_LID KEY_HINGE 31 | #define KEY_TOUCH BIT(14) 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/nds/interrupts.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Interrupt registers and vector pointers 4 | 5 | Copyright (C) 2005 6 | Jason Rogers (dovoto) 7 | Dave Murphy (WinterMute) 8 | 9 | This software is provided 'as-is', without any express or implied 10 | warranty. In no event will the authors be held liable for any 11 | damages arising from the use of this software. 12 | 13 | Permission is granted to anyone to use this software for any 14 | purpose, including commercial applications, and to alter it and 15 | redistribute it freely, subject to the following restrictions: 16 | 17 | 1. The origin of this software must not be misrepresented; you 18 | must not claim that you wrote the original software. If you use 19 | this software in a product, an acknowledgment in the product 20 | documentation would be appreciated but is not required. 21 | 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 25 | 3. This notice may not be removed or altered from any source 26 | distribution. 27 | 28 | 29 | ---------------------------------------------------------------------------------*/ 30 | 31 | /*! \file interrupts.h 32 | \brief nds interrupt support. 33 | */ 34 | 35 | #ifndef NDS_INTERRUPTS_INCLUDE 36 | #define NDS_INTERRUPTS_INCLUDE 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | #define REG_AUXIE REG_IE2 43 | #define REG_AUXIF REG_IF2 44 | 45 | #define IRQ_NETWORK IRQ_RTC 46 | #define IRQ_KEYS IRQ_KEYPAD 47 | #define IRQ_CART IRQ_SLOT2 48 | #define IRQ_IPC_SYNC IRQ_PXI_SYNC 49 | #define IRQ_FIFO_EMPTY IRQ_PXI_SEND 50 | #define IRQ_FIFO_NOT_EMPTY IRQ_PXI_RECV 51 | #define IRQ_CARD IRQ_SLOT1_TX 52 | #define IRQ_CARD_LINE IRQ_SLOT1_IREQ 53 | #define IRQ_LID IRQ_HINGE 54 | #define IRQ_ALL UINT32_MAX 55 | 56 | #define IRQ_I2C IRQ2_MCU 57 | #define IRQ_SDMMC IRQ2_TMIO0 58 | 59 | #define irqSetAUX irqSet2 60 | #define irqClearAUX irqClear2 61 | #define irqEnableAUX irqEnable2 62 | #define irqDisableAUX irqDisable2 63 | 64 | #define swiIntrWait threadIrqWait 65 | #define swiWaitForVBlank threadWaitForVBlank 66 | 67 | typedef u32 IRQ_MASKS; 68 | typedef u32 IRQ_MASKSAUX; 69 | 70 | //! maximum number of interrupts. 71 | #define MAX_INTERRUPTS MK_IRQ_NUM_HANDLERS 72 | 73 | //! values allowed for REG_IME 74 | enum IME_VALUE { 75 | IME_DISABLE = 0, /*!< Disable all interrupts. */ 76 | IME_ENABLE = 1, /*!< Enable all interrupts not masked out in REG_IE */ 77 | }; 78 | 79 | #ifdef __cplusplus 80 | extern "C" { 81 | #endif 82 | 83 | static inline int enterCriticalSection(void) { 84 | return irqLock(); 85 | } 86 | 87 | static inline void leaveCriticalSection(int oldIME) { 88 | irqUnlock(oldIME); 89 | } 90 | 91 | #ifdef __cplusplus 92 | } 93 | #endif 94 | 95 | #endif //NDS_INTERRUPTS_INCLUDE 96 | -------------------------------------------------------------------------------- /include/nds/ipc.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Inter Processor Communication 4 | 5 | Copyright (C) 2005 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Dave Murphy (WinterMute) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 3. This notice may not be removed or altered from any source 25 | distribution. 26 | 27 | ---------------------------------------------------------------------------------*/ 28 | 29 | #ifndef NDS_IPC_INCLUDE 30 | #define NDS_IPC_INCLUDE 31 | 32 | #include "ndstypes.h" 33 | 34 | //--------------------------------------------------------------------------------- 35 | // Synchronization register 36 | //--------------------------------------------------------------------------------- 37 | #define REG_IPC_SYNC (*(vu16*)0x04000180) 38 | 39 | enum IPC_SYNC_BITS { 40 | IPC_SYNC_IRQ_ENABLE = BIT(14), 41 | IPC_SYNC_IRQ_REQUEST = BIT(13) 42 | }; 43 | 44 | //--------------------------------------------------------------------------------- 45 | static inline void IPC_SendSync(unsigned int sync) { 46 | //--------------------------------------------------------------------------------- 47 | REG_IPC_SYNC = (REG_IPC_SYNC & 0xf0ff) | (((sync) & 0x0f) << 8) | IPC_SYNC_IRQ_REQUEST; 48 | } 49 | 50 | //--------------------------------------------------------------------------------- 51 | static inline int IPC_GetSync() { 52 | //--------------------------------------------------------------------------------- 53 | return REG_IPC_SYNC & 0x0f; 54 | } 55 | 56 | //--------------------------------------------------------------------------------- 57 | // fifo 58 | //--------------------------------------------------------------------------------- 59 | #define REG_IPC_FIFO_TX (*(vu32*)0x4000188) 60 | #define REG_IPC_FIFO_RX (*(vu32*)0x4100000) 61 | #define REG_IPC_FIFO_CR (*(vu16*)0x4000184) 62 | 63 | enum IPC_CONTROL_BITS { 64 | IPC_FIFO_SEND_EMPTY = (1<<0), 65 | IPC_FIFO_SEND_FULL = (1<<1), 66 | IPC_FIFO_SEND_IRQ = (1<<2), 67 | IPC_FIFO_SEND_CLEAR = (1<<3), 68 | IPC_FIFO_RECV_EMPTY = (1<<8), 69 | IPC_FIFO_RECV_FULL = (1<<9), 70 | IPC_FIFO_RECV_IRQ = (1<<10), 71 | IPC_FIFO_ERROR = (1<<14), 72 | IPC_FIFO_ENABLE = (1<<15) 73 | }; 74 | 75 | #endif // NDS_IPC_INCLUDE 76 | 77 | 78 | -------------------------------------------------------------------------------- /include/nds/ndstypes.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | ndstypes.h -- Common types (and a few useful macros) 4 | 5 | Copyright (C) 2005 - 2008 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Dave Murphy (WinterMute) 9 | Chris Double (doublec) 10 | 11 | This software is provided 'as-is', without any express or implied 12 | warranty. In no event will the authors be held liable for any 13 | damages arising from the use of this software. 14 | 15 | Permission is granted to anyone to use this software for any 16 | purpose, including commercial applications, and to alter it and 17 | redistribute it freely, subject to the following restrictions: 18 | 19 | 1. The origin of this software must not be misrepresented; you 20 | must not claim that you wrote the original software. If you use 21 | this software in a product, an acknowledgment in the product 22 | documentation would be appreciated but is not required. 23 | 2. Altered source versions must be plainly marked as such, and 24 | must not be misrepresented as being the original software. 25 | 3. This notice may not be removed or altered from any source 26 | distribution. 27 | 28 | ---------------------------------------------------------------------------------*/ 29 | /*! \file ndstypes.h 30 | \brief Custom types employed by libnds 31 | */ 32 | 33 | #ifndef _NDSTYPES_INCLUDE 34 | #define _NDSTYPES_INCLUDE 35 | //--------------------------------------------------------------------------------- 36 | #include 37 | 38 | //--------------------------------------------------------------------------------- 39 | // libgba compatible section macros 40 | //--------------------------------------------------------------------------------- 41 | #define ITCM_CODE __attribute__((section(".itcm"), long_call)) 42 | 43 | #define DTCM_DATA __attribute__((section(".dtcm"))) 44 | #define DTCM_BSS __attribute__((section(".sbss"))) 45 | 46 | #define TWL_CODE __attribute__((section(".twl"))) 47 | #define TWL_DATA __attribute__((section(".twl"))) 48 | #define TWL_BSS __attribute__((section(".twl_bss"))) 49 | 50 | //! aligns a struct (and other types?) to m, making sure that the size of the struct is a multiple of m. 51 | #define ALIGN(m) __attribute__((aligned (m))) 52 | 53 | //! packs a struct (and other types?) so it won't include padding bytes. 54 | #define PACKED __attribute__ ((packed)) 55 | #define packed_struct struct PACKED 56 | 57 | //--------------------------------------------------------------------------------- 58 | // These are linked to the bin2o macro in the Makefile 59 | //--------------------------------------------------------------------------------- 60 | #define GETRAW(name) (name) 61 | #define GETRAWSIZE(name) ((int)name##_size) 62 | #define GETRAWEND(name) ((int)name##_end) 63 | 64 | /*! 65 | \brief returns a number with the nth bit set. 66 | */ 67 | #define BIT(n) (1 << (n)) 68 | 69 | typedef uint8_t uint8 MK_DEPRECATED; 70 | typedef uint16_t uint16 MK_DEPRECATED; 71 | typedef uint32_t uint32 MK_DEPRECATED; 72 | typedef uint64_t uint64 MK_DEPRECATED; 73 | typedef int8_t int8 MK_DEPRECATED; 74 | typedef int16_t int16 MK_DEPRECATED; 75 | typedef int32_t int32 MK_DEPRECATED; 76 | typedef int64_t int64 MK_DEPRECATED; 77 | typedef float float32 MK_DEPRECATED; 78 | typedef double float64 MK_DEPRECATED; 79 | 80 | typedef volatile uint8_t vuint8 MK_DEPRECATED; 81 | typedef volatile uint16_t vuint16 MK_DEPRECATED; 82 | typedef volatile uint32_t vuint32 MK_DEPRECATED; 83 | typedef volatile uint64_t vuint64 MK_DEPRECATED; 84 | typedef volatile int8_t vint8 MK_DEPRECATED; 85 | typedef volatile int16_t vint16 MK_DEPRECATED; 86 | typedef volatile int32_t vint32 MK_DEPRECATED; 87 | typedef volatile int64_t vint64 MK_DEPRECATED; 88 | typedef volatile float32 vfloat32 MK_DEPRECATED; 89 | typedef volatile float64 vfloat64 MK_DEPRECATED; 90 | 91 | #ifndef TRUE 92 | #define TRUE 1 93 | #define FALSE 0 94 | #endif 95 | 96 | // Handy function pointer typedefs 97 | //! a function pointer that takes no arguments and doesn't return anything. 98 | typedef void (* VoidFn)(void); 99 | 100 | typedef void (* IntFn)(void); 101 | typedef void (* fp)(void); 102 | 103 | //--------------------------------------------------------------------------------- 104 | #endif 105 | //--------------------------------------------------------------------------------- 106 | -------------------------------------------------------------------------------- /include/nds/rsa.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2017 4 | Dave Murphy (WinterMute) 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any 8 | damages arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any 11 | purpose, including commercial applications, and to alter it and 12 | redistribute it freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you 15 | must not claim that you wrote the original software. If you use 16 | this software in a product, an acknowledgment in the product 17 | documentation would be appreciated but is not required. 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | 23 | ---------------------------------------------------------------------------------*/ 24 | #ifndef RSA_H_INCLUDE 25 | #define RSA_H_INCLUDE 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include "nds/ndstypes.h" 32 | #include 33 | 34 | typedef SvcRsaHeapContext swiRSAHeapContext_t; 35 | typedef SvcRsaParams swiRSAbuffers_t; 36 | 37 | int swiRSAInitHeap(swiRSAHeapContext_t *ctx, void *heapStart, size_t heapSize); 38 | int swiRSADecryptRAW(swiRSAHeapContext_t *ctx, const swiRSAbuffers_t *rsabuffers, size_t *out_len); 39 | int swiRSADecrypt(swiRSAHeapContext_t *ctx, void *dst, const void *sig, const void *key); 40 | int swiRSADecryptPGP(swiRSAHeapContext_t *ctx, void *dst, const void *sig, const void *key); 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | 46 | 47 | #endif // RSA_H_INCLUDE -------------------------------------------------------------------------------- /include/nds/sha1.h: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | DSi sha1 functions 4 | 5 | Copyright (C) 2017 6 | Dave Murphy (WinterMute) 7 | 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 2. Altered source versions must be plainly marked as such, and 21 | must not be misrepresented as being the original software. 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | ---------------------------------------------------------------------------------*/ 26 | /*! \file sha1.h 27 | \brief DSi SHA1 functions. 28 | */ 29 | 30 | #ifndef SHA1_H_INCLUDE 31 | #define SHA1_H_INCLUDE 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | #include "nds/ndstypes.h" 38 | #include 39 | 40 | typedef SvcSha1Context swiSHA1context_t; 41 | 42 | /** 43 | * \brief SHA-1 context setup 44 | * 45 | * \param ctx context to be initialized 46 | */ 47 | void swiSHA1Init(swiSHA1context_t *ctx); 48 | 49 | /** 50 | * \brief SHA-1 process buffer 51 | * 52 | * \param ctx SHA-1 context 53 | * \param data buffer to process 54 | * \param len length of data 55 | */ 56 | void swiSHA1Update(swiSHA1context_t *ctx, const void *data, size_t len); 57 | 58 | /** 59 | * \brief SHA-1 final digest 60 | * 61 | * \param digest buffer to hold SHA-1 checksum result 62 | * \param ctx SHA-1 context 63 | */ 64 | void swiSHA1Final(void *digest, swiSHA1context_t *ctx); 65 | 66 | /** 67 | * \brief SHA-1 checksum 68 | * 69 | * \param digest buffer to hold SHA-1 checksum result 70 | * \param data buffer to process 71 | * \param len length of data 72 | */ 73 | void swiSHA1Calc(void *digest, const void *data, size_t len); 74 | 75 | /** 76 | * \brief SHA-1 verify 77 | * 78 | * \param digest1 buffer containing hash to verify 79 | * \param digest2 buffer containing hash to verify 80 | */ 81 | bool swiSHA1Verify(const void *digest1, const void *digest2); 82 | 83 | #ifdef __cplusplus 84 | } 85 | #endif 86 | 87 | 88 | #endif // SHA1_H_INCLUDE -------------------------------------------------------------------------------- /include/nds/touch.h: -------------------------------------------------------------------------------- 1 | #ifndef __TOUCH_H__ 2 | #define __TOUCH_H__ 3 | 4 | #include 5 | 6 | typedef TouchData touchPosition; 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /libnds_license.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2005 - 2024 2 | Michael Noland (joat) 3 | Jason Rogers (dovoto) 4 | Dave Murphy (WinterMute) 5 | fincs 6 | 7 | This software is provided 'as-is', without any express or implied 8 | warranty. In no event will the authors be held liable for any 9 | damages arising from the use of this software. 10 | 11 | Permission is granted to anyone to use this software for any 12 | purpose, including commercial applications, and to alter it and 13 | redistribute it freely, subject to the following restrictions: 14 | 15 | 1. The origin of this software must not be misrepresented; you 16 | must not claim that you wrote the original software. If you use 17 | this software in a product, an acknowledgment in the product 18 | documentation would be appreciated but is not required. 19 | 2. Altered source versions must be plainly marked as such, and 20 | must not be misrepresented as being the original software. 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | -------------------------------------------------------------------------------- /source/arm9/background.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | background.c -- DS Background Control 4 | 5 | Copyright (C) 2007 6 | Dave Murphy (WinterMute) 7 | Jason Rogers (Dovoto) 8 | 9 | This software is provided 'as-is', without any express or implied 10 | warranty. In no event will the authors be held liable for any 11 | damages arising from the use of this software. 12 | 13 | Permission is granted to anyone to use this software for any 14 | purpose, including commercial applications, and to alter it and 15 | redistribute it freely, subject to the following restrictions: 16 | 17 | 1. The origin of this software must not be misrepresented; you 18 | must not claim that you wrote the original software. If you use 19 | this software in a product, an acknowledgment in the product 20 | documentation would be appreciated but is not required. 21 | 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 25 | 3. This notice may not be removed or altered from any source 26 | distribution. 27 | 28 | ---------------------------------------------------------------------------------*/ 29 | 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | //const char* BgUsage = 36 | //"______________________________\n" 37 | //"|Mode | BG0 | BG1 | BG2 | BG3 |\n" 38 | //"| 0 | T | T | T | T |\n" 39 | //"| 1 | T | T | T | R |\n" 40 | //"| 2 | T | T | R | R |\n" 41 | //"| 3 | T | T | T | E |\n" 42 | //"| 4 | T | T | R | E |\n" 43 | //"| 5 | T | T | E | E |\n" 44 | //"|_____|_____|_____|_____|_____|\n" 45 | //"T = Text\n" 46 | //"R = Rotation\n" 47 | //"E = Extended Rotation (Bitmap or tiled)\n"; 48 | 49 | //look up tables for smoothing register access between the two 50 | //displays 51 | vu16* bgControl[8] = { 52 | ®_BG0CNT, 53 | ®_BG1CNT, 54 | ®_BG2CNT, 55 | ®_BG3CNT, 56 | ®_BG0CNT_SUB, 57 | ®_BG1CNT_SUB, 58 | ®_BG2CNT_SUB, 59 | ®_BG3CNT_SUB, 60 | }; 61 | 62 | bg_scroll* bgScrollTable[8] = { 63 | &BG_OFFSET[0], 64 | &BG_OFFSET[1], 65 | &BG_OFFSET[2], 66 | &BG_OFFSET[3], 67 | 68 | &BG_OFFSET_SUB[0], 69 | &BG_OFFSET_SUB[1], 70 | &BG_OFFSET_SUB[2], 71 | &BG_OFFSET_SUB[3] 72 | }; 73 | 74 | bg_transform* bgTransform[8] = { 75 | (bg_transform*)0, 76 | (bg_transform*)0, 77 | (bg_transform*)0x04000020, 78 | (bg_transform*)0x04000030, 79 | 80 | (bg_transform*)0, 81 | (bg_transform*)0, 82 | (bg_transform*)0x04001020, 83 | (bg_transform*)0x04001030, 84 | }; 85 | 86 | BgState bgState[8]; 87 | 88 | bool bgIsTextLut[8]; 89 | 90 | bool bgIsText(int id) { 91 | return bgIsTextLut[id]; 92 | } 93 | 94 | bool checkIfText(int id) { 95 | if(id < 2 || (id > 3 && id < 6)) return true; 96 | 97 | u8 mode = (id < 4) ? (videoGetMode() & 7) : (videoGetModeSub() & 7); 98 | 99 | if(!mode) return true; 100 | 101 | if(mode == 1 || mode == 3) { 102 | return id == 3 || id == 7 ? false : true; 103 | } 104 | 105 | return false; 106 | } 107 | 108 | void bgUpdate(void) { 109 | 110 | int i = 0; 111 | 112 | for(i = 0; i < 8; i++) 113 | { 114 | if(!bgState[i].dirty) continue; 115 | 116 | if(bgIsTextLut[i]) 117 | { 118 | 119 | bgScrollTable[i]->x = bgState[i].scrollX >> 8; 120 | bgScrollTable[i]->y = bgState[i].scrollY >> 8; 121 | 122 | } 123 | else 124 | { 125 | s16 angleSin; 126 | s16 angleCos; 127 | 128 | s32 pa, pb, pc, pd; 129 | 130 | // Compute sin and cos 131 | angleSin = sinLerp(bgState[i].angle); 132 | angleCos = cosLerp(bgState[i].angle); 133 | 134 | // Set the background registers 135 | pa = ( angleCos * bgState[i].scaleX ) >> 12; 136 | pb = (-angleSin * bgState[i].scaleX ) >> 12; 137 | pc = ( angleSin * bgState[i].scaleY ) >> 12; 138 | pd = ( angleCos * bgState[i].scaleY ) >> 12; 139 | 140 | bgTransform[i]->hdx = pa; 141 | bgTransform[i]->vdx = pb; 142 | bgTransform[i]->hdy = pc; 143 | bgTransform[i]->vdy = pd; 144 | 145 | bgTransform[i]->dx = (bgState[i].scrollX) - ((pa * bgState[i].centerX + pb * bgState[i].centerY) >> 8); 146 | bgTransform[i]->dy = (bgState[i].scrollY) - ((pc * bgState[i].centerX + pd * bgState[i].centerY) >> 8); 147 | } 148 | 149 | bgState[i].dirty = false; 150 | } 151 | } 152 | 153 | 154 | 155 | //initializes and enables the appropriate background with the supplied attributes 156 | //returns an id which must be supplied to the remainder of the background functions 157 | int bgInit_call(int layer, BgType type, BgSize size, int mapBase, int tileBase) { 158 | 159 | BGCTRL[layer] = BG_MAP_BASE(mapBase) | BG_TILE_BASE(tileBase) 160 | | size | ((type == BgType_Text8bpp) ? BG_COLOR_256 : 0); 161 | 162 | memset(&bgState[layer], 0, sizeof(BgState) ); 163 | 164 | bgIsTextLut[layer] = checkIfText(layer); 165 | 166 | if(type != BgType_Text8bpp && type != BgType_Text4bpp) { 167 | bgSetScale(layer, 1 << 8, 1 << 8); 168 | bgSetRotate(layer, 0); 169 | } 170 | 171 | bgState[layer].type = type; 172 | bgState[layer].size = size; 173 | 174 | videoBgEnable(layer); 175 | 176 | bgState[layer].dirty = true; 177 | 178 | bgUpdate(); 179 | 180 | return layer; 181 | } 182 | 183 | 184 | int bgInitSub_call(int layer, BgType type, BgSize size, int mapBase, int tileBase) { 185 | 186 | BGCTRL_SUB[layer] = BG_MAP_BASE(mapBase) | BG_TILE_BASE(tileBase) 187 | | size | ((type == BgType_Text8bpp) ? BG_COLOR_256 : 0) ; 188 | 189 | memset(&bgState[layer + 4], 0, sizeof(BgState) ); 190 | 191 | bgIsTextLut[layer + 4] = checkIfText(layer + 4); 192 | 193 | if(type != BgType_Text8bpp && type != BgType_Text4bpp) { 194 | bgSetScale(layer + 4, 1 << 8, 1 << 8); 195 | bgSetRotate(layer + 4, 0); 196 | } 197 | 198 | bgState[layer + 4].type = type; 199 | bgState[layer + 4].size = size; 200 | 201 | videoBgEnableSub(layer); 202 | 203 | bgState[layer + 4].dirty = true; 204 | 205 | bgUpdate(); 206 | 207 | return layer + 4; 208 | } 209 | 210 | -------------------------------------------------------------------------------- /source/arm9/boxtest.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | BoxTest.c -- Code for performing hardware box test against viewing frustrum 4 | 5 | Copyright (C) 2005 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Dave Murphy (WinterMute) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 23 | 2. Altered source versions must be plainly marked as such, and 24 | must not be misrepresented as being the original software. 25 | 26 | 3. This notice may not be removed or altered from any source 27 | distribution. 28 | 29 | ---------------------------------------------------------------------------------*/ 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | //--------------------------------------------------------------------------------- 36 | void BoxTest_Asynch(v16 x, v16 y, v16 z, v16 width, v16 height, v16 depth) { 37 | //--------------------------------------------------------------------------------- 38 | glPolyFmt(BIT(12) | BIT(13)); 39 | glBegin(GL_TRIANGLES); 40 | glEnd(); 41 | 42 | GFX_BOX_TEST = VERTEX_PACK(x, y); 43 | GFX_BOX_TEST = VERTEX_PACK(z, width); 44 | GFX_BOX_TEST = VERTEX_PACK(height, depth); 45 | } 46 | 47 | //--------------------------------------------------------------------------------- 48 | void BoxTestf_Asynch(float x, float y, float z, float width, float height, float depth) { 49 | //--------------------------------------------------------------------------------- 50 | BoxTest_Asynch(floattov16(x), floattov16(y), floattov16(z), 51 | floattov16(width), floattov16(height), floattov16(depth)); 52 | } 53 | 54 | //--------------------------------------------------------------------------------- 55 | int BoxTestResult(void) { 56 | //--------------------------------------------------------------------------------- 57 | while(GFX_STATUS & BIT(0)); 58 | 59 | return (GFX_STATUS & BIT(1)); 60 | } 61 | 62 | //--------------------------------------------------------------------------------- 63 | int BoxTest(v16 x, v16 y, v16 z, v16 width, v16 height, v16 depth) 64 | //--------------------------------------------------------------------------------- 65 | { 66 | glPolyFmt(BIT(12) | BIT(13)); 67 | glBegin(GL_TRIANGLES); 68 | glEnd(); 69 | 70 | GFX_BOX_TEST = VERTEX_PACK(x, y); 71 | GFX_BOX_TEST = VERTEX_PACK(z, width); 72 | GFX_BOX_TEST = VERTEX_PACK(height, depth); 73 | 74 | while(GFX_STATUS & BIT(0)); 75 | 76 | return (GFX_STATUS & BIT(1)); 77 | } 78 | 79 | //--------------------------------------------------------------------------------- 80 | int BoxTestf(float x, float y, float z, float width, float height, float depth) 81 | //--------------------------------------------------------------------------------- 82 | { 83 | return BoxTest(floattov16(x), floattov16(y), floattov16(z), 84 | floattov16(width), floattov16(height), floattov16(depth)); 85 | } 86 | -------------------------------------------------------------------------------- /source/arm9/decompress.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Jason Rogers (dovoto) 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any 8 | damages arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any 11 | purpose, including commercial applications, and to alter it and 12 | redistribute it freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you 15 | must not claim that you wrote the original software. If you use 16 | this software in a product, an acknowledgment in the product 17 | documentation would be appreciated but is not required. 18 | 19 | 2. Altered source versions must be plainly marked as such, and 20 | must not be misrepresented as being the original software. 21 | 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | 26 | ---------------------------------------------------------------------------------*/ 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | static int getHeader(u8 *source, u16 *dest, u32 arg) { 33 | return *(u32*)source; 34 | } 35 | 36 | static u8 readByte(u8 *source) { 37 | return *source; 38 | } 39 | 40 | TDecompressionStream decomStream = { 41 | getHeader, 42 | 0, 43 | readByte 44 | }; 45 | 46 | void decompress(const void* data, void* dst, DecompressType type) 47 | { 48 | switch(type) 49 | { 50 | case LZ77Vram: 51 | swiDecompressLZSSVram((void*)data, (void*)dst, 0, &decomStream); 52 | break; 53 | case LZ77: 54 | swiDecompressLZSSWram((void*)data, (void*)dst); 55 | break; 56 | case HUFF: 57 | swiDecompressHuffman((void*)data, (void*)dst, 0, &decomStream); 58 | break; 59 | case RLE: 60 | swiDecompressRLEWram((void*)data, (void*)dst); 61 | break; 62 | case RLEVram: 63 | swiDecompressRLEVram((void*)data, (void*)dst, 0, &decomStream); 64 | break; 65 | default: 66 | break; 67 | } 68 | } 69 | 70 | void decompressStream(const void* data, void* dst, DecompressType type, getByteCallback readCB, getHeaderCallback getHeaderCB) 71 | { 72 | #ifdef ARM9 73 | sassert(type != LZ77 && type != RLE, "LZ77 and RLE do not support streaming, use Vram versions"); 74 | #endif 75 | 76 | 77 | TDecompressionStream decompresStream = 78 | { 79 | getHeaderCB, 80 | 0, 81 | readCB 82 | }; 83 | 84 | switch(type) 85 | { 86 | case LZ77Vram: 87 | swiDecompressLZSSVram((void*)data, (void*)dst, 0, &decompresStream); 88 | break; 89 | case HUFF: 90 | swiDecompressHuffman((void*)data, (void*)dst, 0, &decompresStream); 91 | break; 92 | case RLEVram: 93 | swiDecompressRLEVram((void*)data, (void*)dst, 0, &decompresStream); 94 | break; 95 | default: 96 | break; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /source/arm9/default_font.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devkitPro/libnds/6194b32d8f94e2ebc8078e64bf213ffc13ba1985/source/arm9/default_font.bin -------------------------------------------------------------------------------- /source/arm9/dynamicArray.c: -------------------------------------------------------------------------------- 1 | /* 2 | include licence stuff? 3 | 4 | see dynamicArray for licence... 5 | */ 6 | 7 | 8 | #include 9 | 10 | 11 | void* DynamicArrayInit(DynamicArray* v, unsigned int initialSize) 12 | { 13 | if(v == NULL) 14 | { 15 | return NULL; 16 | } 17 | 18 | v->cur_size = initialSize; 19 | v->data = (void**)malloc(sizeof(void*) * initialSize); 20 | 21 | return v->data; 22 | } 23 | 24 | 25 | 26 | void DynamicArrayDelete(DynamicArray* v) 27 | { 28 | if(v == NULL) 29 | { 30 | return; 31 | } 32 | 33 | if(v->data != NULL) 34 | { 35 | free(v->data); 36 | } 37 | } 38 | 39 | 40 | 41 | void* DynamicArrayGet(DynamicArray* v, unsigned int index) 42 | { 43 | if(v == NULL) 44 | { 45 | return NULL; 46 | } 47 | 48 | if(index >= v->cur_size) 49 | { 50 | return NULL; 51 | } 52 | 53 | return v->data[index]; 54 | } 55 | 56 | 57 | 58 | bool DynamicArraySet(DynamicArray *v, unsigned int index, void* item) 59 | { 60 | if(v == NULL) 61 | { 62 | return false; 63 | } 64 | 65 | if(index >= v->cur_size) 66 | { 67 | //resize the array, making sure it is bigger than index. 68 | unsigned int newSize = (v->cur_size * 2 > index ? v->cur_size * 2: index + 1); 69 | 70 | void** temp = (void**)realloc(v->data, sizeof(void*) * newSize); 71 | 72 | if(temp == NULL) return false; 73 | v->data = temp; 74 | memset(v->data + v->cur_size, 0, sizeof(void*) * (newSize - v->cur_size)); 75 | v->cur_size = newSize; 76 | } 77 | 78 | v->data[index] = item; 79 | return true; 80 | } 81 | -------------------------------------------------------------------------------- /source/arm9/exceptionDefault.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Dave Murphy (WinterMute) 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any 8 | damages arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any 11 | purpose, including commercial applications, and to alter it and 12 | redistribute it freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you 15 | must not claim that you wrote the original software. If you use 16 | this software in a product, an acknowledgment in the product 17 | documentation would be appreciated but is not required. 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | 23 | ---------------------------------------------------------------------------------*/ 24 | 25 | #include 26 | #include 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | #include 35 | 36 | #include 37 | 38 | //--------------------------------------------------------------------------------- 39 | static void excptPrint(const char* buf, size_t size) { 40 | //--------------------------------------------------------------------------------- 41 | if (buf) { 42 | write(1, buf, size); 43 | } else { 44 | for (size_t i = 0; i < size; i ++) { 45 | char spc = ' '; 46 | write(1, &spc, 1); 47 | } 48 | } 49 | } 50 | 51 | //--------------------------------------------------------------------------------- 52 | static void defaultHandler(ExcptContext* ctx, unsigned flags) { 53 | //--------------------------------------------------------------------------------- 54 | consoleDemoInit(); 55 | dietPrintSetFunc(excptPrint); 56 | 57 | BG_PALETTE_SUB[0] = RGB15(31,0,0); 58 | BG_PALETTE_SUB[255] = RGB15(31,31,31); 59 | 60 | guruMeditationDump(ctx, flags); 61 | 62 | Thread* cur = threadGetSelf(); 63 | dietPrint("thrd: %08lX tp : %08lX\n", (u32)cur, (u32)cur->tp); 64 | 65 | u32* stack = (u32*)ctx->r[13]; 66 | for (unsigned i = 0; i < 8; i ++) { 67 | dietPrint("\n %08lX: %08lX %08lX", (u32)&stack[i*2], stack[i*2], stack[(i*2)+1]); 68 | } 69 | } 70 | 71 | //--------------------------------------------------------------------------------- 72 | void defaultExceptionHandler(void) { 73 | //--------------------------------------------------------------------------------- 74 | setExceptionHandler(defaultHandler) ; 75 | } 76 | -------------------------------------------------------------------------------- /source/arm9/exceptionEntry.s: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2023 fincs 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any 7 | damages arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any 10 | purpose, including commercial applications, and to alter it and 11 | redistribute it freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you 14 | must not claim that you wrote the original software. If you use 15 | this software in a product, an acknowledgment in the product 16 | documentation would be appreciated but is not required. 17 | 2. Altered source versions must be plainly marked as such, and 18 | must not be misrepresented as being the original software. 19 | 3. This notice may not be removed or altered from any source 20 | distribution. 21 | 22 | ---------------------------------------------------------------------------------*/ 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | @--------------------------------------------------------------------------------- 29 | FUNC_START32 __libnds_excpt 30 | @--------------------------------------------------------------------------------- 31 | 32 | @ Configure 32K ITCM (mirrored throughout 0x0000000..0x1FFFFFF) 33 | mov r12, #CP15_TCM_32M 34 | mcr p15, 0, r12, c9, c1, 1 35 | 36 | @ Separate extra flags from stack pointer 37 | bic r12, sp, #3 @ BIOS exception stack 38 | and lr, sp, #3 @ extra flags 39 | 40 | @ Set up new exception stack on ITCM & push context structure 41 | mov sp, #MM_MAINRAM @ end of ITCM 42 | sub sp, sp, #20*4 43 | 44 | @ Grab non-banked registers directly 45 | stmia sp, {r0-r7} 46 | 47 | @ Grab saved registers from BIOS exception stack 48 | ldmia r12, {r0-r3} @ cp15cr cpsr r12 pc 49 | str r0, [sp, #18*4] @ cp15cr 50 | str r1, [sp, #16*4] @ cpsr 51 | mov r12, r2 52 | str r3, [sp, #15*4] @ pc 53 | 54 | @ Grab banked registers from correct processor mode 55 | mrs r2, cpsr 56 | ands r4, r1, #0xf @ Extract and check mode 57 | moveq r4, #0xf @ If user: replace with system 58 | cmpne r4, #0xf @ Z flag for later = mode is user or system 59 | orr r4, r4, #(ARM_PSR_I | ARM_PSR_F | ARM_PSR_MODE_USR) 60 | add r5, sp, #8*4 61 | msr cpsr_c, r4 62 | stmia r5, {r8-r14} 63 | mrsne r6, spsr @ If not user/system: grab spsr too 64 | msr cpsr_c, r2 65 | 66 | @ Store remaining registers 67 | str r6, [sp, #17*4] @ spsr 68 | str r2, [sp, #19*4] @ excpt_cpsr 69 | 70 | @-------------------------------------------------------------------------- 71 | @ MPU reconfiguration 72 | 73 | @ Configure MPU regions 0-2: background, main RAM, BIOS 74 | ldr r4, =CP15_PU_ENABLE | CP15_PU_4G 75 | mcr p15, 0, r4, c6, c0, 0 76 | ldr r4, =CP15_PU_ENABLE | CP15_PU_16M | MM_MAINRAM 77 | mcr p15, 0, r4, c6, c1, 0 78 | ldr r4, =CP15_PU_ENABLE | CP15_PU_32K | MM_BIOS 79 | mcr p15, 0, r4, c6, c2, 0 80 | 81 | @ Configure MPU region 3: DTCM 82 | mrc p15, 0, r4, c9, c1, 0 83 | bic r4, r4, #0xff 84 | orr r4, r4, #CP15_PU_ENABLE | CP15_PU_16K 85 | mcr p15, 0, r4, c6, c3, 0 86 | 87 | @ Clear unused MPU regions 4-7 88 | mov r4, #0 89 | mcr p15, 0, r4, c6, c4, 0 90 | mcr p15, 0, r4, c6, c5, 0 91 | mcr p15, 0, r4, c6, c6, 0 92 | mcr p15, 0, r4, c6, c7, 0 93 | 94 | @ Set up MPU region attributes 95 | ldr r4, =0x3633 96 | mcr p15, 0, r4, c5, c0, 2 @ Data permissions 97 | ldr r4, =0x0666 98 | mcr p15, 0, r4, c5, c0, 3 @ Code permissions 99 | mov r4, #0b0110 100 | mcr p15, 0, r4, c2, c0, 0 @ Data cacheability 101 | mcr p15, 0, r4, c2, c0, 1 @ Code cacheability 102 | mov r4, #0b0010 103 | mcr p15, 0, r4, c3, c0, 0 @ Write buffer enable 104 | 105 | @ Reenable MPU 106 | mrc p15, 0, r4, c1, c0, 0 107 | orr r4, r4, #CP15_CR_PU_ENABLE 108 | mcr p15, 0, r4, c1, c0, 0 109 | 110 | @-------------------------------------------------------------------------- 111 | 112 | @ Grab C function & call it 113 | ldr r4, =g_excptHandler 114 | mov r0, sp 115 | ldr r4, [r4] 116 | mov r1, lr 117 | blx r4 118 | 119 | @ Infinite loop 120 | 1: b 1b 121 | 122 | FUNC_END 123 | 124 | @--------------------------------------------------------------------------------- 125 | SECT_BSS g_excptHandler 126 | @--------------------------------------------------------------------------------- 127 | .weak g_excptHandler 128 | @--------------------------------------------------------------------------------- 129 | g_excptHandler: 130 | @--------------------------------------------------------------------------------- 131 | .space 4 132 | -------------------------------------------------------------------------------- /source/arm9/gfx/keyboardGfx.grit: -------------------------------------------------------------------------------- 1 | -W3 2 | 3 | -gB4 4 | 5 | -m 6 | 7 | -gzl 8 | 9 | -pe16 -------------------------------------------------------------------------------- /source/arm9/gfx/keyboardGfx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devkitPro/libnds/6194b32d8f94e2ebc8078e64bf213ffc13ba1985/source/arm9/gfx/keyboardGfx.png -------------------------------------------------------------------------------- /source/arm9/guitarGrip.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static u8 guitar_keys = 0; 7 | static u8 guitar_keys_old = 0; 8 | 9 | //------------------------------------------------------------------------------ 10 | bool guitarGripIsInserted() { 11 | //------------------------------------------------------------------------------ 12 | if(!gbacartIsOpen()) return false; 13 | 14 | //This is 0x96h is a GBA game is inserted 15 | if(GBA_HEADER.is96h == 0x96) return false; 16 | 17 | //guitar grip signifies itself this way (AD9/AD10 pulled low) 18 | if(*(vu16*)0x08000000 != 0xF9FF) return false; 19 | 20 | return true; 21 | } 22 | 23 | //------------------------------------------------------------------------------ 24 | void guitarGripScanKeys() { 25 | //------------------------------------------------------------------------------ 26 | guitar_keys_old = guitar_keys; 27 | guitar_keys = ~(*(vu8*)0x0A000000); 28 | } 29 | 30 | //------------------------------------------------------------------------------ 31 | u8 guitarGripKeysHeld() { 32 | //------------------------------------------------------------------------------ 33 | return guitar_keys; 34 | } 35 | 36 | //------------------------------------------------------------------------------ 37 | u16 guitarGripKeysDown() { 38 | //------------------------------------------------------------------------------ 39 | return guitar_keys & ~guitar_keys_old; 40 | } 41 | 42 | //------------------------------------------------------------------------------ 43 | u16 guitarGripKeysUp() { 44 | //------------------------------------------------------------------------------ 45 | return (guitar_keys ^ guitar_keys_old) & ~guitar_keys; 46 | } 47 | -------------------------------------------------------------------------------- /source/arm9/heapfuncs.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2011 4 | Dave Murphy (WinterMute) 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any 8 | damages arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any 11 | purpose, including commercial applications, and to alter it and 12 | redistribute it freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you 15 | must not claim that you wrote the original software. If you use 16 | this software in a product, an acknowledgment in the product 17 | documentation would be appreciated but is not required. 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | 23 | ---------------------------------------------------------------------------------*/ 24 | #include 25 | #include 26 | #include // for sbrk() 27 | 28 | extern u8 *fake_heap_end; // current heap start 29 | extern u8 *fake_heap_start; // current heap end 30 | 31 | u8* getHeapStart() { 32 | return fake_heap_start; 33 | } 34 | 35 | u8* getHeapEnd() { 36 | return (u8*)sbrk(0); 37 | } 38 | 39 | u8* getHeapLimit() { 40 | return fake_heap_end; 41 | } 42 | -------------------------------------------------------------------------------- /source/arm9/image.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | $Id: image.c,v 1.11 2008-04-12 03:34:03 dovoto Exp $ 3 | 4 | 5 | Copyright (C) 2005 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Dave Murphy (WinterMute) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 3. This notice may not be removed or altered from any source 25 | distribution. 26 | 27 | 28 | 29 | 30 | ---------------------------------------------------------------------------------*/ 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include 37 | #include 38 | 39 | //--------------------------------------------------------------------------------- 40 | void image24to16(sImage* img) { 41 | //--------------------------------------------------------------------------------- 42 | 43 | int x; 44 | int y; 45 | 46 | u16* temp = (u16*)malloc(img->height*img->width*2); 47 | 48 | for(y=0;yheight;y++) 49 | { 50 | for(x=0;xwidth;x++) 51 | temp[x+y*img->width]=(1<<15)|RGB15(img->image.data8[x*3+y*img->width*3]>>3, \ 52 | img->image.data8[x*3+y*img->width*3+1]>>3, img->image.data8[x*3+y*img->width*3+2]>>3); 53 | } 54 | 55 | free(img->image.data8); 56 | 57 | img->bpp=16; 58 | img->image.data16 = temp; 59 | } 60 | 61 | //--------------------------------------------------------------------------------- 62 | void image8to16(sImage* img) { 63 | //--------------------------------------------------------------------------------- 64 | int i; 65 | 66 | sassert(img->bpp == 8, "image must be 8 bpp"); 67 | sassert(img->palette != NULL, "image must have a palette set"); 68 | 69 | u16* temp = (u16*)malloc(img->height*img->width*2); 70 | 71 | for(i = 0; i < img->height * img->width; i++) 72 | temp[i] = img->palette[img->image.data8[i]] | (1<<15); 73 | 74 | free (img->image.data8); 75 | free (img->palette); 76 | 77 | img->palette = NULL; 78 | 79 | img->bpp = 16; 80 | img->image.data16 = temp; 81 | } 82 | 83 | //--------------------------------------------------------------------------------- 84 | void image8to16trans(sImage* img, u8 transparentColor) { 85 | //--------------------------------------------------------------------------------- 86 | int i; 87 | u8 c; 88 | 89 | sassert(img->bpp == 8, "image must be 8 bpp"); 90 | sassert(img->palette != NULL, "image must have a palette set"); 91 | 92 | u16* temp = (u16*)malloc(img->height*img->width*2); 93 | 94 | for(i = 0; i < img->height * img->width; i++) { 95 | 96 | c = img->image.data8[i]; 97 | 98 | if(c != transparentColor) 99 | temp[i] = img->palette[c] | (1<<15); 100 | else 101 | temp[i] = img->palette[c]; 102 | } 103 | 104 | free (img->image.data8); 105 | free (img->palette); 106 | 107 | img->palette = NULL; 108 | 109 | img->bpp = 16; 110 | img->image.data16 = temp; 111 | } 112 | //--------------------------------------------------------------------------------- 113 | void imageTileData(sImage* img) { 114 | //--------------------------------------------------------------------------------- 115 | u32* temp; 116 | 117 | int ix, iy, tx, ty; 118 | 119 | int th, tw; 120 | 121 | int i = 0; 122 | 123 | //can only tile 8 bit data that is a multiple of 8 in dimention 124 | if(img->bpp != 8 || (img->height & 3) != 0 || (img->width & 3) != 0) return; 125 | 126 | th = img->height >> 3; 127 | tw = img->width >> 3; 128 | 129 | //buffer to hold data 130 | temp = (u32*)malloc(img->height * img->width); 131 | 132 | for(ty = 0; ty < th; ty++) 133 | for(tx = 0; tx < tw; tx++) 134 | for(iy = 0; iy < 8; iy++) 135 | for(ix = 0; ix < 2; ix++) 136 | temp[i++] = img->image.data32[ix + tx * 2 + (iy + ty * 8) * tw * 2 ]; 137 | 138 | free(img->image.data32); 139 | 140 | img->image.data32 = (u32*)temp; 141 | } 142 | 143 | //--------------------------------------------------------------------------------- 144 | void imageDestroy(sImage* img) { 145 | //--------------------------------------------------------------------------------- 146 | if(img->image.data8) free (img->image.data8); 147 | if(img->palette && img->bpp == 8) free (img->palette); 148 | } 149 | -------------------------------------------------------------------------------- /source/arm9/keys.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | $Id: keys.c,v 1.19 2008-12-11 19:56:44 dovoto Exp $ 3 | 4 | key input code -- provides slightly higher level input forming 5 | 6 | Copyright (C) 2005 7 | Christian Auby (DesktopMan) 8 | Dave Murphy (WinterMute) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 3. This notice may not be removed or altered from any source 25 | distribution. 26 | 27 | ------------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | //------------------------------------------------------------------------------ 36 | 37 | static Keypad s_keypad; 38 | 39 | static u16 keysrepeat = 0; 40 | 41 | static u8 delay = 30, repeat = 15, count = 30; 42 | 43 | //------------------------------------------------------------------------------ 44 | void scanKeys(void) { 45 | //------------------------------------------------------------------------------ 46 | s_keypad.old = s_keypad.cur; 47 | s_keypad.cur = keysCurrent(); 48 | 49 | if ( delay != 0 ) { 50 | if ( s_keypad.cur != s_keypad.old ) { 51 | count = delay; 52 | keysrepeat = keypadDown(&s_keypad); 53 | } 54 | count--; 55 | if ( count == 0 ) { 56 | count = repeat; 57 | keysrepeat = s_keypad.cur; 58 | } 59 | } 60 | } 61 | 62 | //------------------------------------------------------------------------------ 63 | u32 keysHeld(void) { 64 | //------------------------------------------------------------------------------ 65 | return keypadHeld(&s_keypad); 66 | } 67 | 68 | //------------------------------------------------------------------------------ 69 | u32 keysDown(void) { 70 | //------------------------------------------------------------------------------ 71 | return keypadDown(&s_keypad); 72 | } 73 | 74 | //------------------------------------------------------------------------------ 75 | u32 keysDownRepeat(void) { 76 | //------------------------------------------------------------------------------ 77 | u32 tmp = keysrepeat; 78 | 79 | keysrepeat = 0; 80 | 81 | return tmp; 82 | } 83 | 84 | //------------------------------------------------------------------------------ 85 | void keysSetRepeat( u8 setDelay, u8 setRepeat ) { 86 | //------------------------------------------------------------------------------ 87 | delay = setDelay ; 88 | repeat = setRepeat ; 89 | count = delay ; 90 | keysrepeat = 0 ; 91 | } 92 | 93 | //------------------------------------------------------------------------------ 94 | u32 keysUp(void) { 95 | //------------------------------------------------------------------------------ 96 | return keypadUp(&s_keypad); 97 | } 98 | 99 | //------------------------------------------------------------------------------ 100 | u32 keysCurrent(void) { 101 | //------------------------------------------------------------------------------ 102 | touchPosition tp; 103 | Keypad k = {0}; 104 | keypadRead(&k); 105 | return keypadHeld(&k) | (touchRead(&tp) ? KEY_TOUCH : 0); 106 | } 107 | -------------------------------------------------------------------------------- /source/arm9/linkedlist.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | 6 | 7 | LinkedList* linkedlistAdd(LinkedList **front, void* data) 8 | { 9 | LinkedList *node = (LinkedList*)malloc(sizeof(LinkedList)); 10 | 11 | if(node == NULL) 12 | { 13 | return NULL; 14 | } 15 | 16 | node->prev = NULL; 17 | node->data = data; 18 | 19 | if(*front == NULL) 20 | { 21 | node->next = NULL; 22 | 23 | *front = node; 24 | } 25 | else 26 | { 27 | node->next = *front; 28 | 29 | (*front)->prev = node; 30 | } 31 | 32 | return node; 33 | } 34 | 35 | 36 | 37 | void linkedlistRemove(LinkedList *node) 38 | { 39 | if(node == NULL) 40 | { 41 | return; 42 | } 43 | 44 | if(node->prev != NULL) 45 | { 46 | node->prev->next = node->next; 47 | } 48 | 49 | if(node->next != NULL) 50 | { 51 | node->next->prev = node->prev; 52 | } 53 | 54 | free(node); 55 | } 56 | 57 | -------------------------------------------------------------------------------- /source/arm9/paddle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //------------------------------------------------------------------------------ 7 | static void paddleSetBus() { 8 | //------------------------------------------------------------------------------ 9 | //setting the bus owner is not sufficient, as we need to ensure that the bus speeds are adequately slowed 10 | gbacartSetTiming(GBA_WAIT_SRAM_MASK | GBA_PHI_MASK, 11 | GBA_WAIT_SRAM_18 | //make sure SRAM is slow enough 12 | GBA_PHI_4_19 //a nonzero value for this is required for correct functioning. arkanoid uses 1. it is uncertain what other values will do. 13 | ); 14 | } 15 | 16 | //------------------------------------------------------------------------------ 17 | bool paddleIsInserted() { 18 | //------------------------------------------------------------------------------ 19 | if(!gbacartIsOpen()) return false; 20 | 21 | //This is 0x96h is a GBA game is inserted 22 | if(GBA_HEADER.is96h == 0x96) return false; 23 | 24 | //paddle signifies itself this way (AD12 pulled low) 25 | if(*(vu16*)0x08000000 != 0xEFFF) return false; 26 | 27 | return true; 28 | } 29 | 30 | //------------------------------------------------------------------------------ 31 | u16 paddleRead() { 32 | //------------------------------------------------------------------------------ 33 | paddleSetBus(); 34 | return (*(vu8*)0x0A000000) | ((*(vu8*)0x0A000001)<<8); 35 | } 36 | 37 | //------------------------------------------------------------------------------ 38 | void paddleReset() { 39 | //------------------------------------------------------------------------------ 40 | (*(vu8*)0x0A000000) = 0; 41 | } 42 | -------------------------------------------------------------------------------- /source/arm9/pcx.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Jason Rogers (dovoto) 5 | Dave Murphy (WinterMute) 6 | 7 | This software is provided 'as-is', without any express or implied 8 | warranty. In no event will the authors be held liable for any 9 | damages arising from the use of this software. 10 | 11 | Permission is granted to anyone to use this software for any 12 | purpose, including commercial applications, and to alter it and 13 | redistribute it freely, subject to the following restrictions: 14 | 15 | 1. The origin of this software must not be misrepresented; you 16 | must not claim that you wrote the original software. If you use 17 | this software in a product, an acknowledgment in the product 18 | documentation would be appreciated but is not required. 19 | 2. Altered source versions must be plainly marked as such, and 20 | must not be misrepresented as being the original software. 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | 24 | ---------------------------------------------------------------------------------*/ 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | //--------------------------------------------------------------------------------- 31 | int loadPCX(const unsigned char* pcx, sImage* image) { 32 | //--------------------------------------------------------------------------------- 33 | //struct rgb {unsigned char b,g,r;}; 34 | RGB_24* pal; 35 | 36 | PCXHeader* hdr = (PCXHeader*) pcx; 37 | 38 | pcx += sizeof(PCXHeader); 39 | 40 | unsigned char c; 41 | int size; 42 | int count; 43 | int run; 44 | int i; 45 | int iy; 46 | int width, height; 47 | int scansize = hdr->bytesPerLine; 48 | unsigned char *scanline; 49 | 50 | 51 | width = image->width = hdr->xmax - hdr->xmin + 1 ; 52 | height = image->height = hdr->ymax - hdr->ymin + 1; 53 | 54 | size = image->width * image->height; 55 | 56 | if(hdr->bitsPerPixel != 8) 57 | return 0; 58 | 59 | scanline = image->image.data8 = (unsigned char*)malloc(size); 60 | image->palette = (unsigned short*)malloc(256 * 2); 61 | 62 | count = 0; 63 | 64 | for(iy = 0; iy < height; iy++) { 65 | count = 0; 66 | while(count < scansize) 67 | { 68 | c = *pcx++; 69 | 70 | if(c < 192) { 71 | scanline[count++] = c; 72 | } else { 73 | run = c - 192; 74 | 75 | c = *pcx++; 76 | 77 | for(i = 0; i < run && count < scansize; i++) 78 | scanline[count++] = c; 79 | } 80 | } 81 | scanline += width; 82 | } 83 | 84 | //check for the palette marker. 85 | //I have seen PCX files without this, but the docs don't seem ambiguous--it must be here. 86 | //Anyway, the support among other apps is poor, so we're going to reject it. 87 | if(*pcx != 0x0C) 88 | { 89 | free(image->image.data8); 90 | image->image.data8 = 0; 91 | free(image->palette); 92 | image->palette = 0; 93 | return 0; 94 | } 95 | 96 | pcx++; 97 | 98 | pal = (RGB_24*)(pcx); 99 | 100 | image->bpp = 8; 101 | 102 | for(i = 0; i < 256; i++) 103 | { 104 | u8 r = (pal[i].r + 4 > 255) ? 255 : (pal[i].r + 4); 105 | u8 g = (pal[i].g + 4 > 255) ? 255 : (pal[i].g + 4); 106 | u8 b = (pal[i].b + 4 > 255) ? 255 : (pal[i].b + 4); 107 | image->palette[i] = RGB15(r >> 3 , g >> 3 , b >> 3) ; 108 | } 109 | return 1; 110 | } 111 | -------------------------------------------------------------------------------- /source/arm9/piano.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static u16 piano_keys = 0; 7 | static u16 piano_keys_old = 0; 8 | 9 | //------------------------------------------------------------------------------ 10 | bool pianoIsInserted() { 11 | //------------------------------------------------------------------------------ 12 | if (!gbacartIsOpen()) return false; 13 | 14 | // This is 0x96h is a GBA game is inserted 15 | if(GBA_HEADER.is96h == 0x96) return false; 16 | 17 | //piano signifies itself this way (AD11/AD12 pulled low) 18 | if(*(vu16*)0x08000000 != 0xE7FF) return false; 19 | 20 | return true; 21 | } 22 | 23 | //------------------------------------------------------------------------------ 24 | void pianoScanKeys() { 25 | //------------------------------------------------------------------------------ 26 | piano_keys_old = piano_keys; 27 | piano_keys = ~PIANO_PAK; 28 | } 29 | 30 | //------------------------------------------------------------------------------ 31 | u16 pianoKeysHeld() { 32 | //------------------------------------------------------------------------------ 33 | return piano_keys; 34 | } 35 | 36 | //------------------------------------------------------------------------------ 37 | u16 pianoKeysDown() { 38 | //------------------------------------------------------------------------------ 39 | return piano_keys & ~piano_keys_old; 40 | } 41 | 42 | //------------------------------------------------------------------------------ 43 | u16 pianoKeysUp() { 44 | //------------------------------------------------------------------------------ 45 | return (piano_keys ^ piano_keys_old) & ~piano_keys; 46 | } 47 | -------------------------------------------------------------------------------- /source/arm9/rumble.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Michael Noland (joat) 5 | Jason Rogers (dovoto) 6 | Dave Murphy (WinterMute) 7 | Mike Parks (BigRedPimp) 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | Permission is granted to anyone to use this software for any 12 | purpose, including commercial applications, and to alter it and 13 | redistribute it freely, subject to the following restrictions: 14 | 15 | 1. The origin of this software must not be misrepresented; you 16 | must not claim that you wrote the original software. If you use 17 | this software in a product, an acknowledgment in the product 18 | documentation would be appreciated but is not required. 19 | 2. Altered source versions must be plainly marked as such, and 20 | must not be misrepresented as being the original software. 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | 24 | ---------------------------------------------------------------------------------*/ 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | static RUMBLE_TYPE rumbleType; 31 | 32 | //--------------------------------------------------------------------------------- 33 | bool rumbleIsInserted(void) { 34 | //--------------------------------------------------------------------------------- 35 | if (!gbacartIsOpen()) return false; 36 | 37 | // First, check for 0x96 to see if it's a GBA game 38 | if (GBA_HEADER.is96h == 0x96) { 39 | //if it is a game, we check the game code 40 | //to see if it is warioware twisted 41 | if ( (GBA_HEADER.gamecode[0] == 'R') && 42 | (GBA_HEADER.gamecode[1] == 'Z') && 43 | (GBA_HEADER.gamecode[2] == 'W') && 44 | (GBA_HEADER.gamecode[3] == 'E') 45 | ) 46 | { 47 | rumbleType = WARIOWARE; 48 | WARIOWARE_ENABLE = 8; 49 | return true; 50 | } 51 | 52 | // Otherwise, we have a different game and thus no rumble 53 | return false; 54 | } 55 | 56 | // The Rumble Pak signifies itself this way (D1 pulled low) 57 | rumbleType = RUMBLE; 58 | return *(vu16*)0x08000000 == 0xFFFD; 59 | } 60 | 61 | //--------------------------------------------------------------------------------- 62 | void rumbleSet(bool position) { 63 | //--------------------------------------------------------------------------------- 64 | 65 | if( rumbleType == WARIOWARE) { 66 | WARIOWARE_PAK = (position ? 8 : 0); 67 | } else { 68 | RUMBLE_PAK = (position ? 2 : 0); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /source/arm9/sassert.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | sassert.c -- definitons for DS assertions 4 | 5 | Copyright (C) 2013 6 | Jason Rogers (Dovoto) 7 | Michael Theall (mtheall) 8 | 9 | This software is provided 'as-is', without any express or implied 10 | warranty. In no event will the authors be held liable for any 11 | damages arising from the use of this software. 12 | 13 | Permission is granted to anyone to use this software for any 14 | purpose, including commercial applications, and to alter it and 15 | redistribute it freely, subject to the following restrictions: 16 | 17 | 1. The origin of this software must not be misrepresented; you 18 | must not claim that you wrote the original software. If you use 19 | this software in a product, an acknowledgment in the product 20 | documentation would be appreciated but is not required. 21 | 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 25 | 3. This notice may not be removed or altered from any source 26 | distribution. 27 | 28 | ---------------------------------------------------------------------------------*/ 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | void __sassert(const char *fileName, int lineNumber, const char* conditionString, const char* format, ...) 35 | { 36 | va_list ap; 37 | 38 | consoleDemoInit(); 39 | 40 | iprintf("\x1b[j" /* clear screen */ 41 | "\x1b[42mAssertion!\n" /* print in green? */ 42 | "\x1b[39mFile: \n" /* print in default color */ 43 | "%s\n\n" /* print filename */ 44 | "Line: %d\n\n" /* print line number */ 45 | "Condition:\n" 46 | "%s\n\n" /* print condition message */ 47 | "\x1b[41m", /* change font color to red */ 48 | fileName, lineNumber, conditionString); 49 | 50 | va_start(ap, format); 51 | viprintf(format, ap); 52 | va_end(ap); 53 | 54 | //todo: exit properly 55 | while(1); 56 | } 57 | -------------------------------------------------------------------------------- /source/arm9/shadowRegs.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | shadowRegs.c -- shadow variables for write only registers 4 | 5 | Copyright (C) 2007 6 | Dave Murphy (WinterMute) 7 | 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 21 | 2. Altered source versions must be plainly marked as such, and 22 | must not be misrepresented as being the original software. 23 | 24 | 3. This notice may not be removed or altered from any source 25 | distribution. 26 | 27 | ---------------------------------------------------------------------------------*/ 28 | #include 29 | 30 | u16 mosaicShadow = 0; 31 | u16 mosaicShadowSub =0; 32 | -------------------------------------------------------------------------------- /source/arm9/sound.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Sound Functions 4 | 5 | Copyright (C) 2008 6 | Dave Murphy (WinterMute) 7 | Jason Rogers (Dovoto) 8 | 9 | This software is provided 'as-is', without any express or implied 10 | warranty. In no event will the authors be held liable for any 11 | damages arising from the use of this software. 12 | 13 | Permission is granted to anyone to use this software for any 14 | purpose, including commercial applications, and to alter it and 15 | redistribute it freely, subject to the following restrictions: 16 | 17 | 1. The origin of this software must not be misrepresented; you 18 | must not claim that you wrote the original software. If you use 19 | this software in a product, an acknowledgment in the product 20 | documentation would be appreciated but is not required. 21 | 2. Altered source versions must be plainly marked as such, and 22 | must not be misrepresented as being the original software. 23 | 3. This notice may not be removed or altered from any source 24 | distribution. 25 | 26 | 27 | ---------------------------------------------------------------------------------*/ 28 | #include 29 | #include 30 | #include 31 | 32 | __attribute__((constructor)) static void _soundEnsureInit(void) 33 | { 34 | soundInit(); 35 | micInit(); 36 | } 37 | 38 | static int _soundFindFreeChannel(int chmin, int chmax) 39 | { 40 | unsigned mask = soundGetActiveChannels(); 41 | for (int i = chmin; i < chmax; i ++) { 42 | if (!(mask & (1U<= 0) { 54 | soundPreparePsg(ch | SOUND_START, volume<<4, pan, soundTimerFromHz(freq), cycle); 55 | } 56 | 57 | return ch; 58 | } 59 | 60 | int soundPlayNoise(u16 freq, u8 volume, u8 pan) 61 | { 62 | int ch = _soundFindFreeChannel(14, 16); 63 | if (ch >= 0) { 64 | soundPreparePsg(ch | SOUND_START, volume<<4, pan, soundTimerFromHz(freq), SoundDuty_12_5); 65 | } 66 | 67 | return ch; 68 | } 69 | 70 | int soundPlaySample(const void* data, SoundFormat format, u32 dataSize, u16 freq, u8 volume, u8 pan, bool loop, u16 loopPoint) 71 | { 72 | int ch = _soundFindFreeChannel(0, 16); 73 | 74 | if (ch >= 0) { 75 | soundPreparePcm(ch | SOUND_START, volume<<4, pan, soundTimerFromHz(freq), 76 | loop ? SoundMode_Repeat : SoundMode_OneShot, format, data, loopPoint, dataSize/4); 77 | } 78 | 79 | return ch; 80 | } 81 | 82 | static void _soundMicCallback(void* user, void* buf, size_t byte_sz) 83 | { 84 | MicCallback cb = (MicCallback)user; 85 | cb(buf, byte_sz); 86 | } 87 | 88 | int soundMicRecord(void *buffer, u32 bufferLength, MicFormat format, int freq, MicCallback callback) 89 | { 90 | if (!micSetCpuTimer(TIMER_PRESCALER_1, timerCalcPeriod(TIMER_PRESCALER_1, freq))) { 91 | return 0; 92 | } 93 | 94 | if (callback) { 95 | micSetCallback(_soundMicCallback, callback); 96 | } else { 97 | micSetCallback(NULL, NULL); 98 | } 99 | 100 | pmMicSetAmp(true, PmMicGain_160); 101 | 102 | if (!micStart(buffer, bufferLength/2, format, MicMode_DoubleBuffer)) { 103 | pmMicSetAmp(false, 0); 104 | return 0; 105 | } 106 | 107 | return 1; 108 | } 109 | 110 | void soundMicOff(void) 111 | { 112 | micStop(); 113 | pmMicSetAmp(false, 0); 114 | } 115 | -------------------------------------------------------------------------------- /source/arm9/sprite.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include // for NULL 9 | 10 | SpriteEntry OamMemorySub[128]; 11 | SpriteEntry OamMemory[128]; 12 | 13 | OamState oamMain = { 14 | -1, 15 | 0, 16 | NULL, 17 | 32, 18 | {OamMemory}, 19 | SpriteMapping_1D_128 20 | }; 21 | 22 | OamState oamSub = { 23 | -1, 24 | 0, 25 | NULL, 26 | 32, 27 | {OamMemorySub}, 28 | SpriteMapping_1D_128 29 | }; 30 | 31 | //--------------------------------------------------------------------------------- 32 | void oamInit(OamState *oam, SpriteMapping mapping, bool extPalette) { 33 | //--------------------------------------------------------------------------------- 34 | int i; 35 | int extPaletteFlag = extPalette ? DISPLAY_SPR_EXT_PALETTE : 0; 36 | 37 | oam->gfxOffsetStep = (mapping & 3) + 5; 38 | 39 | oam->spriteMapping = mapping; 40 | 41 | dmaFillWords(0, oam->oamMemory, sizeof(OamMemory)); 42 | 43 | for(i = 0; i < 128; i++) { 44 | oam->oamMemory[i].isHidden = true; 45 | } 46 | 47 | for(i = 0; i < 32; i++) { 48 | oam->oamRotationMemory[i].hdx = (1<<8); 49 | oam->oamRotationMemory[i].vdy = (1<<8); 50 | } 51 | 52 | swiWaitForVBlank(); 53 | 54 | DC_FlushRange(oam->oamMemory, sizeof(OamMemory)); 55 | 56 | if(oam == &oamMain) { 57 | dmaCopy(oam->oamMemory, OAM, sizeof(OamMemory)); 58 | 59 | REG_DISPCNT &= ~DISPLAY_SPRITE_ATTR_MASK; 60 | REG_DISPCNT |= DISPLAY_SPR_ACTIVE | (mapping & 0xffffff0) | extPaletteFlag; 61 | } else { 62 | dmaCopy(oam->oamMemory, OAM_SUB, sizeof(OamMemory)); 63 | 64 | REG_DISPCNT_SUB &= ~DISPLAY_SPRITE_ATTR_MASK; 65 | REG_DISPCNT_SUB |= DISPLAY_SPR_ACTIVE | (mapping & 0xffffff0) | extPaletteFlag; 66 | } 67 | 68 | oamAllocReset(oam); 69 | } 70 | 71 | //--------------------------------------------------------------------------------- 72 | void oamDisable(OamState *oam) { 73 | //--------------------------------------------------------------------------------- 74 | if(oam == &oamMain) { 75 | REG_DISPCNT &= ~DISPLAY_SPR_ACTIVE; 76 | } else { 77 | REG_DISPCNT_SUB &= ~DISPLAY_SPR_ACTIVE; 78 | } 79 | } 80 | 81 | //--------------------------------------------------------------------------------- 82 | void oamEnable(OamState *oam) { 83 | //--------------------------------------------------------------------------------- 84 | if(oam == &oamMain) { 85 | REG_DISPCNT |= DISPLAY_SPR_ACTIVE; 86 | } else { 87 | REG_DISPCNT_SUB |= DISPLAY_SPR_ACTIVE; 88 | } 89 | } 90 | 91 | //--------------------------------------------------------------------------------- 92 | u16* oamGetGfxPtr(OamState *oam, int gfxOffsetIndex) { 93 | //--------------------------------------------------------------------------------- 94 | if(gfxOffsetIndex < 0) return NULL; 95 | 96 | if(oam == &oamMain) { 97 | return &SPRITE_GFX[(gfxOffsetIndex << oam->gfxOffsetStep) >> 1]; 98 | } else { 99 | return &SPRITE_GFX_SUB[(gfxOffsetIndex << oam->gfxOffsetStep) >> 1]; 100 | } 101 | } 102 | 103 | 104 | //--------------------------------------------------------------------------------- 105 | void oamClear(OamState *oam, int start, int count) { 106 | //--------------------------------------------------------------------------------- 107 | int i = 0; 108 | 109 | if(count == 0) { 110 | count = 128; 111 | start = 0; 112 | } 113 | 114 | for(i = start; i < count + start; i++) { 115 | 116 | oam->oamMemory[i].attribute[0] = ATTR0_DISABLED; 117 | } 118 | } 119 | 120 | //--------------------------------------------------------------------------------- 121 | unsigned int oamGfxPtrToOffset(OamState *oam, const void* offset) { 122 | //--------------------------------------------------------------------------------- 123 | if(oam->spriteMapping & DISPLAY_SPR_1D) 124 | { 125 | return ((u32)offset & 0xFFFFF) >> oam->gfxOffsetStep;; 126 | } 127 | else 128 | { 129 | u32 size = (oam->spriteMapping & DISPLAY_SPR_2D_BMP_256); 130 | 131 | u32 toffset = (((u32)offset) & 0xFFFFF) >> 1; 132 | 133 | if (size == DISPLAY_SPR_2D_BMP_256) 134 | { 135 | u32 x = (toffset & 0xFF); 136 | u32 y = (toffset >> (8 + 3)); 137 | 138 | return (x >> 3) | (y << 5); 139 | } 140 | else 141 | { 142 | u32 x = (toffset & 0x7F); 143 | u32 y = (toffset >> (7 + 3)) ; 144 | 145 | return (x >> 3)| (y << 4); 146 | } 147 | } 148 | } 149 | 150 | 151 | 152 | //--------------------------------------------------------------------------------- 153 | void oamSet(OamState* oam, int id, int x, int y, int priority, 154 | int palette_alpha, SpriteSize size,SpriteColorFormat format, 155 | const void* gfxOffset, 156 | int affineIndex, 157 | bool sizeDouble, bool hide, bool hflip, bool vflip, bool mosaic) { 158 | //--------------------------------------------------------------------------------- 159 | 160 | if(hide) { 161 | oam->oamMemory[id].attribute[0] = ATTR0_DISABLED; 162 | return; 163 | } 164 | 165 | oam->oamMemory[id].shape = SPRITE_SIZE_SHAPE(size); 166 | oam->oamMemory[id].size = SPRITE_SIZE_SIZE(size); 167 | oam->oamMemory[id].x = x; 168 | oam->oamMemory[id].y = y; 169 | oam->oamMemory[id].palette = palette_alpha; 170 | oam->oamMemory[id].priority = priority; 171 | oam->oamMemory[id].hFlip = hflip; 172 | oam->oamMemory[id].vFlip = vflip; 173 | oam->oamMemory[id].isMosaic = mosaic; 174 | oam->oamMemory[id].gfxIndex = oamGfxPtrToOffset(oam, gfxOffset); 175 | 176 | 177 | if(affineIndex >= 0 && affineIndex < 32) { 178 | oam->oamMemory[id].rotationIndex = affineIndex; 179 | oam->oamMemory[id].isSizeDouble = sizeDouble; 180 | oam->oamMemory[id].isRotateScale = true; 181 | } else { 182 | oam->oamMemory[id].isSizeDouble = false; 183 | oam->oamMemory[id].isRotateScale = false; 184 | } 185 | 186 | if(format != SpriteColorFormat_Bmp) { 187 | oam->oamMemory[id].colorMode = format; 188 | } else { 189 | oam->oamMemory[id].blendMode = format; 190 | oam->oamMemory[id].colorMode = 0; 191 | } 192 | } 193 | 194 | //--------------------------------------------------------------------------------- 195 | void oamUpdate(OamState* oam) { 196 | //--------------------------------------------------------------------------------- 197 | DC_FlushRange(oam->oamMemory, sizeof(OamMemory)); 198 | 199 | if(oam == &oamMain) { 200 | dmaCopy(oam->oamMemory, OAM, sizeof(OamMemory)); 201 | } else { 202 | dmaCopy(oam->oamMemory, OAM_SUB, sizeof(OamMemory)); 203 | } 204 | } 205 | 206 | //--------------------------------------------------------------------------------- 207 | void oamRotateScale(OamState* oam, int rotId, int angle, int sx, int sy){ 208 | //--------------------------------------------------------------------------------- 209 | sassert(rotId >= 0 && rotId < 32, "oamRotateScale() rotId is out of bounds, must be 0-31"); 210 | 211 | int ss = sinLerp(angle); 212 | int cc = cosLerp(angle); 213 | 214 | oam->oamRotationMemory[rotId].hdx = ( cc*sx)>>12; 215 | oam->oamRotationMemory[rotId].vdx = (-ss*sx)>>12; 216 | oam->oamRotationMemory[rotId].hdy = ( ss*sy)>>12; 217 | oam->oamRotationMemory[rotId].vdy = ( cc*sy)>>12; 218 | } 219 | 220 | -------------------------------------------------------------------------------- /source/arm9/sprite_alloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | // A terse macro to convert an allocation buffer index 12 | // into an AllocHeader. Assumes 'oam' is in the current scope. 13 | 14 | #define AH(id) getAllocHeader(oam, id) 15 | 16 | 17 | //buffer grows depending on usage 18 | static void resizeBuffer(OamState *oam) 19 | { 20 | oam->allocBufferSize *= 2; 21 | 22 | oam->allocBuffer = (AllocHeader*)realloc(oam->allocBuffer, sizeof(AllocHeader) * oam->allocBufferSize); 23 | } 24 | 25 | static AllocHeader* getAllocHeader(OamState *oam, int index) 26 | { 27 | // Note that this function may resize the allocBuffer any time you refer to 28 | // a new 'index' for the first time. This will invalidate any pointers that 29 | // getAllocHeader() has previously returned, since the buffer may have moved 30 | // to a different location in memory. The pointers returned by this function 31 | // must be discarded any time a new allocHeader may have been allocated. 32 | 33 | //resize buffer if needed 34 | if(index >= oam->allocBufferSize) 35 | resizeBuffer(oam); 36 | 37 | return &oam->allocBuffer[index]; 38 | } 39 | 40 | static void oamAllocPrepare(OamState *oam) 41 | { 42 | //allocate the buffer if null 43 | if(oam->allocBuffer == NULL) 44 | { 45 | oam->allocBuffer = (AllocHeader*)malloc(sizeof(AllocHeader) * oam->allocBufferSize); 46 | AH(0)->nextFree = 1024; 47 | AH(0)->size = 1024; 48 | } 49 | } 50 | 51 | void oamAllocReset(OamState *oam) 52 | { 53 | //free the buffer if not null & reset size 54 | if(oam->allocBuffer != NULL) 55 | { 56 | free(oam->allocBuffer); 57 | oam->allocBuffer = NULL; 58 | oam->allocBufferSize = 32; 59 | oam->firstFree = 0; 60 | } 61 | } 62 | 63 | static int simpleAlloc(OamState *oam, int size) 64 | { 65 | oamAllocPrepare(oam); 66 | 67 | u16 curOffset = oam->firstFree; 68 | 69 | //check for out of memory 70 | if(oam->firstFree >= 1024 || oam->firstFree == -1) 71 | { 72 | oam->firstFree = -1; 73 | return -1; 74 | } 75 | 76 | int misalignment = curOffset & (size - 1); 77 | 78 | if(misalignment) 79 | misalignment = size - misalignment; 80 | 81 | int next = oam->firstFree; 82 | int last = next; 83 | 84 | //find a big enough block 85 | while(AH(next)->size - misalignment < size) 86 | { 87 | curOffset = AH(next)->nextFree; 88 | 89 | misalignment = curOffset & (size - 1); 90 | 91 | if(misalignment) 92 | misalignment = size - misalignment; 93 | 94 | if(curOffset >= 1024) 95 | { 96 | return -1; 97 | } 98 | 99 | last = next; 100 | next = curOffset; 101 | } 102 | 103 | //next should now point to a large enough block and last should point to the block prior 104 | 105 | ////align to block size 106 | if(misalignment) 107 | { 108 | int tempSize = AH(next)->size; 109 | int tempNextFree = AH(next)->nextFree; 110 | 111 | curOffset += misalignment; 112 | 113 | AH(next)->size = misalignment; 114 | AH(next)->nextFree = curOffset; 115 | 116 | last = next; 117 | next = curOffset; 118 | 119 | AH(next)->size = tempSize - misalignment; 120 | AH(next)->nextFree = tempNextFree; 121 | } 122 | 123 | //is the block the first free block 124 | if(curOffset == oam->firstFree) 125 | { 126 | if(AH(next)->size == size) 127 | { 128 | oam->firstFree = AH(next)->nextFree; 129 | } 130 | else 131 | { 132 | oam->firstFree = curOffset + size; 133 | AH(oam->firstFree)->nextFree = AH(next)->nextFree; 134 | AH(oam->firstFree)->size = AH(next)->size - size; 135 | } 136 | } 137 | else 138 | { 139 | if(AH(next)->size == size) 140 | { 141 | AH(last)->nextFree = AH(next)->nextFree; 142 | } 143 | else 144 | { 145 | AH(last)->nextFree = curOffset + size; 146 | 147 | AH(curOffset + size)->nextFree = AH(next)->nextFree; 148 | AH(curOffset + size)->size = AH(next)->size - size; 149 | } 150 | } 151 | 152 | AH(next)->size = size; 153 | 154 | return curOffset; 155 | } 156 | 157 | static void simpleFree(OamState *oam, int index) 158 | { 159 | u16 curOffset = oam->firstFree; 160 | 161 | int next = oam->firstFree; 162 | int current = index; 163 | 164 | //if we were out of memory its trivial 165 | if(oam->firstFree == -1 || oam->firstFree >= 1024) 166 | { 167 | oam->firstFree = index; 168 | AH(current)->nextFree = 1024; 169 | return; 170 | } 171 | 172 | //if this index is before the first free block its also trivial 173 | if(index < oam->firstFree) 174 | { 175 | //check for abutment and combine if necessary 176 | if(index + AH(current)->size == oam->firstFree) 177 | { 178 | AH(current)->size += AH(next)->size; 179 | AH(current)->nextFree = AH(next)->nextFree; 180 | } 181 | else 182 | { 183 | AH(current)->nextFree = oam->firstFree; 184 | } 185 | 186 | oam->firstFree = index; 187 | 188 | return; 189 | } 190 | 191 | //otherwise locate the free block prior to index 192 | while(index > AH(next)->nextFree) 193 | { 194 | curOffset = AH(next)->nextFree; 195 | 196 | next = AH(next)->nextFree; 197 | } 198 | 199 | 200 | //check if the next free block and current can be appended 201 | // [curOffset] [index] [next->nextFree] 202 | // next | ~ | current | ~ | nextFree 203 | 204 | //check if current abuts nextFree 205 | if(AH(next)->nextFree == index + AH(current)->size && AH(next)->nextFree < 1024) 206 | { 207 | AH(current)->size += AH(AH(next)->nextFree)->size; 208 | AH(current)->nextFree = AH(AH(next)->nextFree)->nextFree; 209 | } 210 | else 211 | { 212 | AH(current)->nextFree = AH(next)->nextFree; 213 | } 214 | 215 | //check if current abuts previous free block 216 | if (curOffset + AH(next)->size == index) 217 | { 218 | AH(next)->size += AH(current)->size; 219 | AH(next)->nextFree = AH(current)->nextFree; 220 | } 221 | else 222 | { 223 | AH(next)->nextFree = index; 224 | } 225 | } 226 | 227 | 228 | 229 | u16* oamAllocateGfx(OamState *oam, SpriteSize size, SpriteColorFormat colorFormat) 230 | { 231 | int bytes = SPRITE_SIZE_PIXELS(size); 232 | 233 | if(colorFormat == SpriteColorFormat_16Color) 234 | bytes = bytes >> 1; 235 | else if(colorFormat == SpriteColorFormat_Bmp) 236 | bytes = bytes << 1; 237 | 238 | bytes = bytes >> oam->gfxOffsetStep; 239 | 240 | int offset = simpleAlloc(oam, bytes ? bytes : 1); 241 | 242 | return oamGetGfxPtr(oam, offset); 243 | } 244 | 245 | void oamFreeGfx(OamState *oam, const void* gfxOffset) 246 | { 247 | simpleFree(oam, oamGfxPtrToOffset(oam, gfxOffset)); 248 | } 249 | 250 | int oamCountFragments(OamState *oam) 251 | { 252 | int frags = 1; 253 | 254 | int curOffset; 255 | 256 | if(oam->allocBuffer == NULL) 257 | { 258 | return 0; 259 | } 260 | 261 | AllocHeader *next = getAllocHeader(oam, oam->firstFree); 262 | 263 | curOffset = next->nextFree; 264 | 265 | while(curOffset < 1024 && curOffset < oam->allocBufferSize) 266 | { 267 | curOffset = next->nextFree; 268 | next = getAllocHeader(oam, next->nextFree); 269 | frags++; 270 | } 271 | 272 | return frags; 273 | } 274 | -------------------------------------------------------------------------------- /source/arm9/system.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | system.c -- System code 4 | 5 | Copyright (C) 2005 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Dave Murphy (WinterMute) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 23 | 2. Altered source versions must be plainly marked as such, and 24 | must not be misrepresented as being the original software. 25 | 26 | 3. This notice may not be removed or altered from any source 27 | distribution. 28 | 29 | 30 | ---------------------------------------------------------------------------------*/ 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | //todo document 38 | // 39 | 40 | //--------------------------------------------------------------------------------- 41 | void powerOn(int bits) { 42 | //--------------------------------------------------------------------------------- 43 | if(bits & BIT(16)) 44 | REG_POWERCNT |= bits & 0xFFFF; 45 | //else 46 | // fifoSendValue32(FIFO_PM, PM_REQ_ON | (bits & 0xFFFF)); 47 | } 48 | 49 | //--------------------------------------------------------------------------------- 50 | void powerOff(int bits) { 51 | //--------------------------------------------------------------------------------- 52 | if(bits & BIT(16)) 53 | REG_POWERCNT &= ~(bits & 0xFFFF); 54 | //else 55 | // fifoSendValue32(FIFO_PM, PM_REQ_OFF | (bits & 0xFFFF)); 56 | } 57 | -------------------------------------------------------------------------------- /source/arm9/system/cpu_clock.s: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | .arch armv5te 4 | .cpu arm946e-s 5 | .section .itcm, "ax", %progbits 6 | .arm 7 | 8 | BEGIN_ASM_FUNC setCpuClock 9 | mov r12, #0x4000000 @ temp = REG_IME; 10 | ldrb r3, [r12, #0x208] 11 | strb r12, [r12, #0x208] @ REG_IME = 0; 12 | push {r3} 13 | 14 | cmp r0, #0 15 | ldr r3, =0x4004004 16 | ldrh r0, [r3] @ Read REG_SCFG_CLK 17 | mov r2, #8 @ CPU clock cycles to wait 18 | biceq r1, r0, #1 @ Adjust bit0 according to requested clock 19 | orrne r1, r0, #1 20 | cmp r1, r0 21 | and r0, r0, #1 @ Set return value (old CPU clock) 22 | beq restoreIrq @ Skip switch if clock already in desired state 23 | 24 | strh r1, [r3] @ Update REG_SCFG_CLK 25 | 1: subs r2, r2, #1 @ Wait for CPU clock to stabilize 26 | bge 1b 27 | 28 | restoreIrq: 29 | pop {r3} @ REG_IME = temp; 30 | strb r3, [r12, #0x208] 31 | bx lr 32 | -------------------------------------------------------------------------------- /source/arm9/video.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 - 2010 4 | Michael Noland (joat) 5 | Jason Rogers (dovoto) 6 | Dave Murphy (WinterMute) 7 | Mike Parks (BigRedPimp) 8 | 9 | This software is provided 'as-is', without any express or implied 10 | warranty. In no event will the authors be held liable for any 11 | damages arising from the use of this software. 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 2. Altered source versions must be plainly marked as such, and 21 | must not be misrepresented as being the original software. 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | ---------------------------------------------------------------------------------*/ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | 32 | //--------------------------------------------------------------------------------- 33 | u32 vramSetPrimaryBanks(VRAM_A_TYPE a, VRAM_B_TYPE b, VRAM_C_TYPE c, VRAM_D_TYPE d) { 34 | //--------------------------------------------------------------------------------- 35 | u32 vramTemp = VRAM_CR; 36 | 37 | VRAM_A_CR = VRAM_ENABLE | a; 38 | VRAM_B_CR = VRAM_ENABLE | b; 39 | VRAM_C_CR = VRAM_ENABLE | c; 40 | VRAM_D_CR = VRAM_ENABLE | d; 41 | 42 | return vramTemp; 43 | } 44 | 45 | //--------------------------------------------------------------------------------- 46 | u32 vramSetBanks_EFG(VRAM_E_TYPE e, VRAM_F_TYPE f, VRAM_G_TYPE g) { 47 | //--------------------------------------------------------------------------------- 48 | u32 vramTemp = VRAM_EFG_CR; 49 | 50 | VRAM_E_CR = VRAM_ENABLE | e; 51 | VRAM_F_CR = VRAM_ENABLE | f; 52 | VRAM_G_CR = VRAM_ENABLE | g; 53 | 54 | return vramTemp; 55 | } 56 | 57 | //--------------------------------------------------------------------------------- 58 | void vramRestorePrimaryBanks(u32 vramTemp) { 59 | //--------------------------------------------------------------------------------- 60 | VRAM_CR = vramTemp; 61 | } 62 | 63 | //--------------------------------------------------------------------------------- 64 | void vramRestoreBanks_EFG(u32 vramTemp) { 65 | //--------------------------------------------------------------------------------- 66 | VRAM_EFG_CR = vramTemp; 67 | } 68 | 69 | //--------------------------------------------------------------------------------- 70 | void setBrightness( int screen, int level) { 71 | //--------------------------------------------------------------------------------- 72 | int mode = 1<<14; 73 | 74 | if ( level < 0){ 75 | level = -level; 76 | mode = 2<<14; 77 | } 78 | 79 | if (level>16) level =16; 80 | 81 | if (screen & 1) REG_MASTER_BRIGHT=(mode | level); 82 | if (screen & 2) REG_MASTER_BRIGHT_SUB=(mode | level); 83 | } 84 | 85 | //--------------------------------------------------------------------------------- 86 | u32 __attribute__((weak)) vramDefault() { 87 | //--------------------------------------------------------------------------------- 88 | 89 | // map all VRAM banks to lcdc mode 90 | VRAM_CR = 0x80808080; 91 | VRAM_E_CR = 0x80; 92 | VRAM_F_CR = 0x80; 93 | VRAM_G_CR = 0x80; 94 | VRAM_H_CR = 0x80; 95 | VRAM_I_CR = 0x80; 96 | 97 | dmaFillWords(0, BG_PALETTE, (2*1024)); // clear main and sub palette 98 | dmaFillWords(0, OAM, 2*1024); // clear main and sub OAM 99 | dmaFillWords(0, VRAM, 656*1024); // clear all VRAM 100 | 101 | 102 | return vramSetPrimaryBanks(VRAM_A_MAIN_BG, VRAM_B_MAIN_SPRITE, VRAM_C_SUB_BG, VRAM_D_SUB_SPRITE); 103 | } 104 | 105 | -------------------------------------------------------------------------------- /source/arm9/videoGL_base.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Video API vaguely similar to OpenGL 4 | 5 | Copyright (C) 2005 6 | Michael Noland (joat) 7 | Jason Rogers (dovoto) 8 | Dave Murphy (WinterMute) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 3. This notice may not be removed or altered from any source 25 | distribution. 26 | 27 | 28 | ---------------------------------------------------------------------------------*/ 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | u32 glCurClearColor; 41 | 42 | //--------------------------------------------------------------------------------- 43 | void glRotatef32i(int angle, s32 x, s32 y, s32 z) { 44 | //--------------------------------------------------------------------------------- 45 | s32 axis[3]; 46 | s32 sine = sinLerp(angle);//SIN[angle & LUT_MASK]; 47 | s32 cosine = cosLerp(angle);//COS[angle & LUT_MASK]; 48 | s32 one_minus_cosine = inttof32(1) - cosine; 49 | 50 | axis[0]=x; 51 | axis[1]=y; 52 | axis[2]=z; 53 | 54 | normalizef32(axis); // should require passed in normalized? 55 | 56 | MATRIX_MULT3x3 = cosine + mulf32(one_minus_cosine, mulf32(axis[0], axis[0])); 57 | MATRIX_MULT3x3 = mulf32(one_minus_cosine, mulf32(axis[0], axis[1])) + mulf32(axis[2], sine); 58 | MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[2]) - mulf32(axis[1], sine); 59 | 60 | MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[1]) - mulf32(axis[2], sine); 61 | MATRIX_MULT3x3 = cosine + mulf32(mulf32(one_minus_cosine, axis[1]), axis[1]); 62 | MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[1]), axis[2]) + mulf32(axis[0], sine); 63 | 64 | MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[2]) + mulf32(axis[1], sine); 65 | MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[1]), axis[2]) - mulf32(axis[0], sine); 66 | MATRIX_MULT3x3 = cosine + mulf32(mulf32(one_minus_cosine, axis[2]), axis[2]); 67 | } 68 | 69 | //--------------------------------------------------------------------------------- 70 | void glMaterialf(GL_MATERIALS_ENUM mode, rgb color) { 71 | //--------------------------------------------------------------------------------- 72 | static u32 diffuse_ambient = 0; 73 | static u32 specular_emission = 0; 74 | 75 | switch(mode) { 76 | case GL_AMBIENT: 77 | diffuse_ambient = (color << 16) | (diffuse_ambient & 0xFFFF); 78 | break; 79 | case GL_DIFFUSE: 80 | diffuse_ambient = color | (diffuse_ambient & 0xFFFF0000); 81 | break; 82 | case GL_AMBIENT_AND_DIFFUSE: 83 | diffuse_ambient= color + (color << 16); 84 | break; 85 | case GL_SPECULAR: 86 | specular_emission = color | (specular_emission & 0xFFFF0000); 87 | break; 88 | case GL_SHININESS: 89 | break; 90 | case GL_EMISSION: 91 | specular_emission = (color << 16) | (specular_emission & 0xFFFF); 92 | break; 93 | } 94 | 95 | GFX_DIFFUSE_AMBIENT = diffuse_ambient; 96 | GFX_SPECULAR_EMISSION = specular_emission; 97 | } 98 | 99 | //--------------------------------------------------------------------------------- 100 | MK_WEAK void _glInitGlobals(void) { 101 | //--------------------------------------------------------------------------------- 102 | // Dummy implementation when not using the texture VRAM manager 103 | } 104 | 105 | //--------------------------------------------------------------------------------- 106 | void glInit(void) { 107 | //--------------------------------------------------------------------------------- 108 | powerOn(POWER_3D_CORE | POWER_MATRIX); // enable 3D core & geometry engine 109 | 110 | // Initialize texture VRAM manager (if needed) 111 | _glInitGlobals(); 112 | 113 | while (GFX_STATUS & (1<<27)); // wait till gfx engine is not busy 114 | 115 | // Clear the FIFO 116 | GFX_STATUS |= (1<<29); 117 | 118 | // Clear overflows from list memory 119 | glResetMatrixStack(); 120 | 121 | // prime the vertex/polygon buffers 122 | glFlush(0); 123 | 124 | // reset the control bits 125 | GFX_CONTROL = 0; 126 | 127 | // reset the rear-plane(a.k.a. clear color) to black, ID=0, and opaque 128 | glCurClearColor = 0; 129 | glClearColor(0,0,0,31); 130 | glClearPolyID(0); 131 | 132 | // reset the depth to it's max 133 | glClearDepth(GL_MAX_DEPTH); 134 | 135 | GFX_TEX_FORMAT = 0; 136 | GFX_POLY_FORMAT = 0; 137 | 138 | glMatrixMode(GL_PROJECTION); 139 | glLoadIdentity(); 140 | 141 | glMatrixMode(GL_MODELVIEW); 142 | glLoadIdentity(); 143 | 144 | glMatrixMode(GL_TEXTURE); 145 | glLoadIdentity(); 146 | } 147 | -------------------------------------------------------------------------------- /source/arm9/window.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | //set window dimensions 5 | void windowSetBounds(WINDOW w, u8 left, u8 top, u8 right,u8 bottom) { 6 | sassert(w == WINDOW_0 || w == WINDOW_1, "Only WINDOW_0 or WINDOW_1 have bounds"); 7 | 8 | if(w == WINDOW_0) { 9 | WIN0_X0 = left; 10 | WIN0_X1 = right; 11 | WIN0_Y0 = top; 12 | WIN0_Y1 = bottom; 13 | } 14 | else if(w == WINDOW_1) { 15 | WIN1_X0 = left; 16 | WIN1_X1 = right; 17 | WIN1_Y0 = top; 18 | WIN1_Y1 = bottom; 19 | } 20 | } 21 | void windowSetBoundsSub(WINDOW w, u8 left, u8 top, u8 right,u8 bottom) { 22 | sassert(w == WINDOW_0 || w == WINDOW_1, "Only WINDOW_0 or WINDOW_1 have bounds."); 23 | 24 | if(w == WINDOW_0) { 25 | SUB_WIN0_X0 = left; 26 | SUB_WIN0_X1 = right; 27 | SUB_WIN0_Y0 = top; 28 | SUB_WIN0_Y1 = bottom; 29 | } 30 | else if(w == WINDOW_1) { 31 | SUB_WIN1_X0 = left; 32 | SUB_WIN1_X1 = right; 33 | SUB_WIN1_Y0 = top; 34 | SUB_WIN1_Y1 = bottom; 35 | } 36 | } 37 | 38 | 39 | //background id, the target window 40 | void bgWindowEnable(int id, WINDOW w) { 41 | vu32 *win = 0; 42 | u32 mask = 0; 43 | 44 | sassert(id >= 0 && id <= 7, "Must choose a valid bg id."); 45 | 46 | if(id < 4) 47 | win = (vu32*)&WIN_IN; 48 | else { 49 | id -= 4; 50 | win = (vu32*)&SUB_WIN_IN; 51 | } 52 | 53 | if(w & WINDOW_0) mask |= BIT(id); 54 | if(w & WINDOW_1) mask |= BIT(id + 8); 55 | if(w & WINDOW_OUT) mask |= BIT(id + 16); 56 | if(w & WINDOW_OBJ) mask |= BIT(id + 24); 57 | 58 | *win |= mask; 59 | } 60 | void bgWindowDisable(int id, WINDOW w) { 61 | vu32 *win = 0; 62 | u32 mask = 0; 63 | 64 | sassert(id >= 0 && id <= 7, "Must choose a valid bg id."); 65 | 66 | if(id < 4) 67 | win = (vu32*)(&WIN_IN); 68 | else { 69 | id -= 4; 70 | win = (vu32*)(&SUB_WIN_IN); 71 | } 72 | 73 | if(w & WINDOW_0) mask |= BIT(id); 74 | if(w & WINDOW_1) mask |= BIT(id + 8); 75 | if(w & WINDOW_OUT) mask |= BIT(id + 16); 76 | if(w & WINDOW_OBJ) mask |= BIT(id + 24); 77 | 78 | *win &= ~mask; 79 | } 80 | 81 | void oamWindowEnable(OamState* oam, WINDOW w) { 82 | vu32 *win = 0; 83 | u32 mask = 0; 84 | 85 | sassert(oam == &oamMain || oam == &oamSub, "Must choose a valid OamState."); 86 | 87 | if(oam == &oamMain) 88 | win = (vu32*)(&WIN_IN); 89 | else 90 | win = (vu32*)(&SUB_WIN_IN); 91 | 92 | if(w & WINDOW_0) mask |= BIT(4); 93 | if(w & WINDOW_1) mask |= BIT(12); 94 | if(w & WINDOW_OUT) mask |= BIT(20); 95 | if(w & WINDOW_OBJ) mask |= BIT(28); 96 | 97 | *win |= mask; 98 | } 99 | void oamWindowDisable(OamState* oam, WINDOW w) { 100 | vu32 *win = 0; 101 | u32 mask = 0; 102 | 103 | sassert(oam == &oamMain || oam == &oamSub, "Must choose a valid OamState."); 104 | 105 | if(oam == &oamMain) 106 | win = (vu32*)(&WIN_IN); 107 | else 108 | win = (vu32*)(&SUB_WIN_IN); 109 | 110 | if(w & WINDOW_0) mask |= BIT(4); 111 | if(w & WINDOW_1) mask |= BIT(12); 112 | if(w & WINDOW_OUT) mask |= BIT(20); 113 | if(w & WINDOW_OBJ) mask |= BIT(28); 114 | 115 | *win &= ~mask; 116 | } -------------------------------------------------------------------------------- /source/common/card.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2005 4 | Michael Noland (joat) 5 | Jason Rogers (dovoto) 6 | Dave Murphy (WinterMute) 7 | 8 | This software is provided 'as-is', without any express or implied 9 | warranty. In no event will the authors be held liable for any 10 | damages arising from the use of this software. 11 | 12 | Permission is granted to anyone to use this software for any 13 | purpose, including commercial applications, and to alter it and 14 | redistribute it freely, subject to the following restrictions: 15 | 16 | 1. The origin of this software must not be misrepresented; you 17 | must not claim that you wrote the original software. If you use 18 | this software in a product, an acknowledgment in the product 19 | documentation would be appreciated but is not required. 20 | 2. Altered source versions must be plainly marked as such, and 21 | must not be misrepresented as being the original software. 22 | 3. This notice may not be removed or altered from any source 23 | distribution. 24 | 25 | 26 | ---------------------------------------------------------------------------------*/ 27 | #include "nds/card.h" 28 | #include "nds/dma.h" 29 | #include "nds/memory.h" 30 | #include "nds/bios.h" 31 | 32 | #include 33 | 34 | //--------------------------------------------------------------------------------- 35 | void cardWriteCommand(const u8 *command) { 36 | //--------------------------------------------------------------------------------- 37 | int index; 38 | 39 | REG_AUXSPICNTH = CARD_CR1_ENABLE | CARD_CR1_IRQ; 40 | 41 | for (index = 0; index < 8; index++) { 42 | REG_CARD_COMMAND[7-index] = command[index]; 43 | } 44 | } 45 | 46 | 47 | //--------------------------------------------------------------------------------- 48 | void cardPolledTransfer(u32 flags, u32 *destination, u32 length, const u8 *command) { 49 | //--------------------------------------------------------------------------------- 50 | u32 data; 51 | cardWriteCommand(command); 52 | REG_ROMCTRL = flags; 53 | u32 * target = destination + length; 54 | do { 55 | // Read data if available 56 | if (REG_ROMCTRL & CARD_DATA_READY) { 57 | data=REG_CARD_DATA_RD; 58 | if (NULL != destination && destination < target) 59 | *destination++ = data; 60 | } 61 | } while (REG_ROMCTRL & CARD_BUSY); 62 | } 63 | 64 | 65 | //--------------------------------------------------------------------------------- 66 | void cardStartTransfer(const u8 *command, u32 *destination, int channel, u32 flags) { 67 | //--------------------------------------------------------------------------------- 68 | cardWriteCommand(command); 69 | 70 | // Set up a DMA channel to transfer a word every time the card makes one 71 | DMA_SRC(channel) = (u32)®_CARD_DATA_RD; 72 | DMA_DEST(channel) = (u32)destination; 73 | DMA_CR(channel) = DMA_ENABLE | DMA_START_CARD | DMA_32_BIT | DMA_REPEAT | DMA_SRC_FIX | 0x0001; 74 | 75 | REG_ROMCTRL = flags; 76 | } 77 | 78 | 79 | //--------------------------------------------------------------------------------- 80 | u32 cardWriteAndRead(const u8 *command, u32 flags) { 81 | //--------------------------------------------------------------------------------- 82 | cardWriteCommand(command); 83 | REG_ROMCTRL = flags | CARD_ACTIVATE | CARD_nRESET | CARD_BLK_SIZE(7); 84 | while (!(REG_ROMCTRL & CARD_DATA_READY)) ; 85 | return REG_CARD_DATA_RD; 86 | } 87 | 88 | //--------------------------------------------------------------------------------- 89 | void cardParamCommand (u8 command, u32 parameter, u32 flags, u32 *destination, u32 length) { 90 | //--------------------------------------------------------------------------------- 91 | u8 cmdData[8]; 92 | 93 | cmdData[7] = (u8) command; 94 | cmdData[6] = (u8) (parameter >> 24); 95 | cmdData[5] = (u8) (parameter >> 16); 96 | cmdData[4] = (u8) (parameter >> 8); 97 | cmdData[3] = (u8) (parameter >> 0); 98 | cmdData[2] = 0; 99 | cmdData[1] = 0; 100 | cmdData[0] = 0; 101 | 102 | cardPolledTransfer(flags, destination, length, cmdData); 103 | } 104 | 105 | //--------------------------------------------------------------------------------- 106 | void cardReadHeader(u8 *header) { 107 | //--------------------------------------------------------------------------------- 108 | REG_ROMCTRL=0; 109 | REG_AUXSPICNTH=0; 110 | swiDelay(167550); 111 | REG_AUXSPICNTH=CARD_CR1_ENABLE|CARD_CR1_IRQ; 112 | REG_ROMCTRL=CARD_nRESET|CARD_SEC_SEED; 113 | while(REG_ROMCTRL&CARD_BUSY) ; 114 | cardReset(); 115 | while(REG_ROMCTRL&CARD_BUSY) ; 116 | 117 | cardParamCommand(CARD_CMD_HEADER_READ,0,CARD_ACTIVATE|CARD_nRESET|CARD_CLK_SLOW|CARD_BLK_SIZE(1)|CARD_DELAY1(0x1FFF)|CARD_DELAY2(0x3F),(u32*)header,512/4); 118 | } 119 | 120 | 121 | //--------------------------------------------------------------------------------- 122 | u32 cardReadID(u32 flags) { 123 | //--------------------------------------------------------------------------------- 124 | const u8 command[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, CARD_CMD_HEADER_CHIPID}; 125 | return cardWriteAndRead(command, flags); 126 | } 127 | 128 | 129 | //--------------------------------------------------------------------------------- 130 | void cardReset() { 131 | //--------------------------------------------------------------------------------- 132 | const u8 cmdData[8]={0,0,0,0,0,0,0,CARD_CMD_DUMMY}; 133 | cardWriteCommand(cmdData); 134 | REG_ROMCTRL=CARD_ACTIVATE|CARD_nRESET|CARD_CLK_SLOW|CARD_BLK_SIZE(5)|CARD_DELAY2(0x18); 135 | u32 read=0; 136 | 137 | do { 138 | if(REG_ROMCTRL&CARD_DATA_READY) { 139 | if(read<0x2000) { 140 | u32 data=REG_CARD_DATA_RD; 141 | (void)data; 142 | read+=4; 143 | } 144 | } 145 | } while(REG_ROMCTRL&CARD_BUSY); 146 | } 147 | 148 | 149 | 150 | 151 | -------------------------------------------------------------------------------- /source/common/dldi.c: -------------------------------------------------------------------------------- 1 | /* 2 | dldi.c 3 | 4 | Copyright (c) 2006 Michael "Chishm" Chisholm 5 | Copyright (c) 2023 fincs 6 | 7 | Redistribution and use in source and binary forms, with or without modification, 8 | are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright notice, 11 | this list of conditions and the following disclaimer. 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation and/or 14 | other materials provided with the distribution. 15 | 3. The name of the author may not be used to endorse or promote products derived 16 | from this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20 | AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 26 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | // Stored backwards to prevent it being picked up by DLDI patchers 33 | static const char s_dldiMagicBackwards[DLDI_MAGIC_STRING_LEN] = { 34 | '\0', 'm', 'h', 's', 'i', 'h', 'C', ' ' 35 | }; 36 | 37 | bool dldiIsValid(const DLDI_INTERFACE* io) 38 | { 39 | if (io->magic_num != DLDI_MAGIC_VAL) { 40 | return false; 41 | } 42 | 43 | for (unsigned i = 0; i < DLDI_MAGIC_STRING_LEN; i++) { 44 | if (io->magic_str[i] != s_dldiMagicBackwards[DLDI_MAGIC_STRING_LEN-1-i]) { 45 | return false; 46 | } 47 | } 48 | 49 | return true; 50 | } 51 | 52 | DLDI_INTERFACE* dldiFindDriverArea(void* bin, size_t bin_size) 53 | { 54 | bin_size -= sizeof(DLDI_INTERFACE)-1; 55 | for (size_t i = 0; i < bin_size; i += 4) { 56 | // Check if the current address is a valid DLDI driver 57 | DLDI_INTERFACE* io = (DLDI_INTERFACE*)((u8*)bin + i); 58 | if (dldiIsValid(io)) { 59 | // Ensure the claimed DLDI header doesn't go out of bounds 60 | size_t rem_sz = bin_size - i; 61 | if (rem_sz >= (1U << io->alloc_sz_log2)) { 62 | return io; 63 | } 64 | } 65 | } 66 | 67 | return NULL; 68 | } 69 | 70 | static void _dldiRelocateRegion(DLDI_INTERFACE* io, uptr start_vaddr, uptr end_vaddr, sptr adjustment) 71 | { 72 | uptr dldi_start_vaddr = io->dldi_start; 73 | uptr dldi_end_vaddr = io->dldi_end; 74 | 75 | uptr* start = (uptr*)(start_vaddr - dldi_start_vaddr + (uptr)io); 76 | uptr* end = (uptr*)(end_vaddr - dldi_start_vaddr + (uptr)io); 77 | 78 | for (uptr* cur = start; cur != end; cur ++) { 79 | uptr addr = *cur + adjustment; 80 | if (addr >= dldi_start_vaddr && addr < dldi_end_vaddr) { 81 | *cur = addr; 82 | } 83 | } 84 | } 85 | 86 | void dldiFixDriverAddresses(DLDI_INTERFACE* io, uptr dldi_vaddr) 87 | { 88 | sptr adjustment = dldi_vaddr - io->dldi_start; 89 | if (adjustment == 0) { 90 | return; 91 | } 92 | 93 | // Correct all pointers to the offsets from the location of this interface 94 | io->dldi_start += adjustment; 95 | io->dldi_end += adjustment; 96 | io->glue_start += adjustment; 97 | io->glue_end += adjustment; 98 | io->got_start += adjustment; 99 | io->got_end += adjustment; 100 | io->bss_start += adjustment; 101 | io->bss_end += adjustment; 102 | 103 | io->disc.startup = (void*)((uptr)io->disc.startup + adjustment); 104 | io->disc.isInserted = (void*)((uptr)io->disc.isInserted + adjustment); 105 | io->disc.readSectors = (void*)((uptr)io->disc.readSectors + adjustment); 106 | io->disc.writeSectors = (void*)((uptr)io->disc.writeSectors + adjustment); 107 | io->disc.clearStatus = (void*)((uptr)io->disc.clearStatus + adjustment); 108 | io->disc.shutdown = (void*)((uptr)io->disc.shutdown + adjustment); 109 | 110 | // Fix all addresses within the DLDI 111 | if (io->fix_flags & DLDI_FIX_ALL) { 112 | _dldiRelocateRegion(io, io->dldi_start, io->dldi_end, adjustment); 113 | } else { 114 | // Fix the interworking glue section 115 | if (io->fix_flags & DLDI_FIX_GLUE) { 116 | _dldiRelocateRegion(io, io->glue_start, io->glue_end, adjustment); 117 | } 118 | 119 | // Fix the global offset table section 120 | if (io->fix_flags & DLDI_FIX_GOT) { 121 | _dldiRelocateRegion(io, io->got_start, io->got_end, adjustment); 122 | } 123 | } 124 | 125 | // Clear the BSS 126 | if (io->fix_flags & DLDI_FIX_BSS) { 127 | armFillMem32((void*)(io->bss_start - io->dldi_start + (uptr)io), 0, io->bss_end - io->bss_start); 128 | } 129 | } 130 | 131 | bool dldiApplyPatch(DLDI_INTERFACE* area, const DLDI_INTERFACE* io) 132 | { 133 | uptr target_vaddr = area->dldi_start; 134 | unsigned target_sz_log2 = area->alloc_sz_log2; 135 | 136 | if (target_sz_log2 < io->driver_sz_log2) { 137 | return false; 138 | } 139 | 140 | armCopyMem32(area, io, 1U << io->driver_sz_log2); 141 | area->alloc_sz_log2 = target_sz_log2; 142 | 143 | dldiFixDriverAddresses(area, target_vaddr); 144 | return true; 145 | } 146 | -------------------------------------------------------------------------------- /source/common/rsa.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2017 4 | Dave Murphy (WinterMute) 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any 8 | damages arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any 11 | purpose, including commercial applications, and to alter it and 12 | redistribute it freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you 15 | must not claim that you wrote the original software. If you use 16 | this software in a product, an acknowledgment in the product 17 | documentation would be appreciated but is not required. 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | 23 | ---------------------------------------------------------------------------------*/ 24 | 25 | #include 26 | #include 27 | 28 | //--------------------------------------------------------------------------------- 29 | int swiRSAInitHeap(swiRSAHeapContext_t *ctx, void *heapStart, size_t heapSize) { 30 | //--------------------------------------------------------------------------------- 31 | if (isDSiMode()) svcRsaHeapInitTWL(ctx, heapStart, heapSize); 32 | return 0; 33 | } 34 | 35 | //--------------------------------------------------------------------------------- 36 | int swiRSADecryptRAW(swiRSAHeapContext_t *ctx, const swiRSAbuffers_t *rsabuffers, size_t *out_len) { 37 | //--------------------------------------------------------------------------------- 38 | if (isDSiMode()) return svcRsaDecryptRawTWL(ctx, rsabuffers, out_len); 39 | return 0; 40 | } 41 | 42 | //--------------------------------------------------------------------------------- 43 | int swiRSADecrypt(swiRSAHeapContext_t *ctx, void *dst, const void *sig, const void *key) { 44 | //--------------------------------------------------------------------------------- 45 | if (isDSiMode()) return svcRsaDecryptUnpadTWL(ctx, dst, sig, key); 46 | return 0; 47 | } 48 | 49 | //--------------------------------------------------------------------------------- 50 | int swiRSADecryptPGP(swiRSAHeapContext_t *ctx, void *dst, const void *sig, const void *key) { 51 | //--------------------------------------------------------------------------------- 52 | if (isDSiMode()) return svcRsaDecryptDerSha1TWL(ctx, dst, sig, key); 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /source/common/sha1.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Copyright (C) 2017 4 | Dave Murphy (WinterMute) 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any 8 | damages arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any 11 | purpose, including commercial applications, and to alter it and 12 | redistribute it freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you 15 | must not claim that you wrote the original software. If you use 16 | this software in a product, an acknowledgment in the product 17 | documentation would be appreciated but is not required. 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | 23 | ---------------------------------------------------------------------------------*/ 24 | 25 | #include 26 | #include 27 | 28 | //--------------------------------------------------------------------------------- 29 | void swiSHA1Init(swiSHA1context_t *ctx) { 30 | //--------------------------------------------------------------------------------- 31 | ctx->hash_block = NULL; 32 | if (isDSiMode()) svcSha1InitTWL(ctx); 33 | } 34 | 35 | //--------------------------------------------------------------------------------- 36 | void swiSHA1Update(swiSHA1context_t *ctx, const void *data, size_t len) { 37 | //--------------------------------------------------------------------------------- 38 | if (isDSiMode()) svcSha1UpdateTWL(ctx, data, len); 39 | 40 | } 41 | 42 | //--------------------------------------------------------------------------------- 43 | void swiSHA1Final(void *digest, swiSHA1context_t *ctx) { 44 | //--------------------------------------------------------------------------------- 45 | if (isDSiMode()) svcSha1DigestTWL(digest, ctx); 46 | } 47 | 48 | //--------------------------------------------------------------------------------- 49 | void swiSHA1Calc(void *digest, const void *data, size_t len) { 50 | //--------------------------------------------------------------------------------- 51 | if (isDSiMode()) svcSha1CalcTWL(digest, data, len); 52 | } 53 | 54 | //--------------------------------------------------------------------------------- 55 | bool swiSHA1Verify(const void *digest1, const void *digest2) { 56 | //--------------------------------------------------------------------------------- 57 | if (isDSiMode()) { 58 | return svcSha1VerifyTWL(digest1, digest2); 59 | } else { 60 | return false; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /source/common/timers.c: -------------------------------------------------------------------------------- 1 | /*--------------------------------------------------------------------------------- 2 | 3 | Timer API 4 | 5 | Copyright (C) 2008 - 2011 6 | Jason Rogers (dovoto) 7 | Mukunda Johnson (eKid) 8 | Dave Murphy (WinterMute) 9 | 10 | This software is provided 'as-is', without any express or implied 11 | warranty. In no event will the authors be held liable for any 12 | damages arising from the use of this software. 13 | 14 | Permission is granted to anyone to use this software for any 15 | purpose, including commercial applications, and to alter it and 16 | redistribute it freely, subject to the following restrictions: 17 | 18 | 1. The origin of this software must not be misrepresented; you 19 | must not claim that you wrote the original software. If you use 20 | this software in a product, an acknowledgment in the product 21 | documentation would be appreciated but is not required. 22 | 2. Altered source versions must be plainly marked as such, and 23 | must not be misrepresented as being the original software. 24 | 3. This notice may not be removed or altered from any source 25 | distribution. 26 | 27 | 28 | ---------------------------------------------------------------------------------*/ 29 | #include 30 | #include 31 | 32 | #include 33 | //--------------------------------------------------------------------------------- 34 | void timerStart(unsigned timer, ClockDivider divider, u16 ticks, VoidFn callback){ 35 | //--------------------------------------------------------------------------------- 36 | sassert(timer < 2, "timer must be in range 0 - 1"); 37 | TIMER_CR(timer) = 0; 38 | TIMER_DATA(timer) = ticks; 39 | 40 | if(callback) { 41 | irqSet(IRQ_TIMER(timer), callback); 42 | irqEnable(IRQ_TIMER(timer)); 43 | TIMER_CR(timer) = TIMER_IRQ_REQ | divider | TIMER_ENABLE; 44 | } else { 45 | TIMER_CR(timer) = divider | TIMER_ENABLE; 46 | } 47 | } 48 | 49 | 50 | static u16 elapsed[2]; 51 | 52 | //--------------------------------------------------------------------------------- 53 | u16 timerElapsed(unsigned timer) { 54 | //--------------------------------------------------------------------------------- 55 | sassert(timer < 2, "timer must be in range 0 - 1"); 56 | u16 time = TIMER_DATA(timer); 57 | 58 | s32 result = (s32)time - (s32)elapsed[timer]; 59 | 60 | //overflow...this will only be accurate if it has overflowed no more than once. 61 | if(result < 0) { 62 | result = time + (0x10000 - elapsed[timer]); 63 | } 64 | 65 | elapsed[timer] = time; 66 | 67 | return (u16) result; 68 | } 69 | 70 | 71 | //--------------------------------------------------------------------------------- 72 | u16 timerPause(unsigned timer) { 73 | //--------------------------------------------------------------------------------- 74 | sassert(timer < 2, "timer must be in range 0 - 1"); 75 | TIMER_CR(timer) &= ~TIMER_ENABLE; 76 | u16 temp = timerElapsed(timer); 77 | elapsed[timer] = 0; 78 | return temp; 79 | } 80 | 81 | //--------------------------------------------------------------------------------- 82 | u16 timerStop(unsigned timer) { 83 | //--------------------------------------------------------------------------------- 84 | sassert(timer < 2, "timer must be in range 0 - 1"); 85 | TIMER_CR(timer) = 0; 86 | u16 temp = timerElapsed(timer); 87 | elapsed[timer] = 0; 88 | return temp; 89 | } 90 | 91 | 92 | static u32 tickRef; 93 | 94 | //--------------------------------------------------------------------------------- 95 | void cpuStartTiming(int unused) { 96 | //--------------------------------------------------------------------------------- 97 | tickInit(); 98 | tickRef = tickGetCount(); 99 | } 100 | 101 | //--------------------------------------------------------------------------------- 102 | u32 cpuGetTiming(void) { 103 | //--------------------------------------------------------------------------------- 104 | return (tickGetCount() - tickRef) * 64; 105 | } 106 | --------------------------------------------------------------------------------