├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE.txt ├── LICENSE_DOC.txt ├── Makefile ├── Makefile.dkp ├── README.md ├── doc ├── bios.md ├── build.md ├── headers.md ├── io │ ├── dispcnt.md │ ├── dmacnt.md │ └── ie.md ├── registers.md ├── rnd │ ├── fastbios.h │ └── overlay.s ├── status.md ├── svc.md └── video_mode.md ├── examples ├── meson.build ├── template │ ├── .gitignore │ ├── Makefile │ ├── meson.build │ └── src │ │ └── main.c ├── vsync-callback │ ├── .gitignore │ ├── Makefile │ ├── meson.build │ └── src │ │ └── main.c └── vsync │ ├── .gitignore │ ├── Makefile │ ├── meson.build │ └── src │ └── main.c ├── gba.txt ├── include └── seven │ ├── asm │ ├── base.s │ ├── hw │ │ ├── dma.s │ │ ├── gpio.s │ │ ├── input.s │ │ ├── irq.s │ │ ├── memory.s │ │ ├── svc.s │ │ ├── timer.s │ │ ├── video.s │ │ └── waitstate.s │ └── prelude.s │ ├── base.h │ ├── base │ ├── addresses.h │ ├── attributes.h │ ├── bits.h │ ├── types.h │ └── version.h │ ├── hw │ ├── cpu.h │ ├── dma.h │ ├── gpio.h │ ├── input.h │ ├── irq.h │ ├── memory.h │ ├── serial.h │ ├── sound.h │ ├── sram.h │ ├── svc.h │ ├── timer.h │ ├── video.h │ └── waitstate.h │ ├── prelude.h │ ├── serial │ ├── joybus.h │ ├── multiplayer.h │ ├── raw.h │ └── uart.h │ ├── svc │ ├── affine.h │ ├── decompression.h │ ├── math.h │ ├── memory.h │ ├── reset.h │ ├── sound.h │ ├── system.h │ └── wait.h │ ├── util │ ├── debug.h │ ├── log.h │ ├── math.h │ ├── memory.h │ ├── overlay.h │ ├── profile.h │ ├── random.h │ ├── simd.h │ └── string.h │ └── video │ ├── bg_affine.h │ ├── bg_bitmap.h │ ├── bg_mixed.h │ ├── bg_regular.h │ ├── bg_scroll.h │ ├── bg_tiled.h │ ├── bg_transform.h │ ├── blend.h │ ├── color.h │ ├── matrix.h │ ├── mosaic.h │ ├── oam.h │ ├── object.h │ ├── palette.h │ ├── prelude.h │ └── window.h ├── meson.build ├── meta ├── header.h ├── include.s └── source.s └── src ├── hw ├── dma.s ├── input.s ├── irq.s ├── sram.s ├── svc.s └── timer.s ├── macros.s ├── util ├── debug.s ├── log.c ├── mem.s ├── profile.s ├── rand.s ├── simd.s └── str.s └── video ├── bmp.s └── oam.s /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /build-meson 3 | /lib 4 | .obsidian/ 5 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "gba-minrt"] 2 | path = subprojects/minrt 3 | url = https://github.com/LunarLambda/gba-minrt 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This Source Code Form is subject to the terms of the Mozilla Public 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this 4 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | # 6 | 7 | cmake_minimum_required(VERSION 3.0) 8 | 9 | project(libseven C ASM) 10 | set(CMAKE_INCLUDE_FLAG_ASM "-Wa,-I") 11 | 12 | add_library(seven STATIC 13 | src/hw/dma.s 14 | src/hw/input.s 15 | src/hw/irq.s 16 | src/hw/sram.s 17 | src/hw/svc.s 18 | src/hw/timer.s 19 | src/util/log.c 20 | src/util/mem.s 21 | src/util/profile.s 22 | src/util/rand.s 23 | src/util/simd.s 24 | src/util/str.s 25 | src/video/bmp.s 26 | src/video/oam.s 27 | ) 28 | 29 | target_include_directories(seven PUBLIC include/) 30 | target_include_directories(seven PRIVATE src/) 31 | 32 | target_compile_options(seven PRIVATE 33 | $<$:-Os -g3 -gdwarf-4 -ffunction-sections -fdata-sections -ffreestanding -std=c99 -Wall -Wpedantic -mabi=aapcs -mcpu=arm7tdmi -mthumb> 34 | ) 35 | 36 | # Allow the string "libseven" as a parameter for target_link_libraries 37 | add_library(libseven ALIAS seven) 38 | 39 | if(EXISTS $ENV{DEVKITPRO}) 40 | install(TARGETS seven DESTINATION "$ENV{DEVKITPRO}/libseven/lib") 41 | install(DIRECTORY include/ DESTINATION "$ENV{DEVKITPRO}/libseven/include") 42 | install(FILES LICENSE.txt DESTINATION "$ENV{DEVKITPRO}/libseven") 43 | endif() 44 | -------------------------------------------------------------------------------- /LICENSE_DOC.txt: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This Source Code Form is subject to the terms of the Mozilla Public 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this 4 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | # 6 | 7 | CC = arm-none-eabi-gcc 8 | AR = arm-none-eabi-ar 9 | 10 | PROJECT = seven 11 | 12 | SOURCES = \ 13 | src/hw/dma.s \ 14 | src/hw/input.s \ 15 | src/hw/irq.s \ 16 | src/hw/sram.s \ 17 | src/hw/svc.s \ 18 | src/hw/timer.s \ 19 | src/util/debug.s \ 20 | src/util/log.c \ 21 | src/util/mem.s \ 22 | src/util/profile.s \ 23 | src/util/rand.s \ 24 | src/util/simd.s \ 25 | src/util/str.s \ 26 | src/video/bmp.s \ 27 | src/video/oam.s \ 28 | 29 | INCLUDES = \ 30 | src \ 31 | include 32 | 33 | CFLAGS = \ 34 | -Os \ 35 | -g3 -gdwarf-4 \ 36 | -ffunction-sections \ 37 | -fdata-sections \ 38 | -ffreestanding \ 39 | -std=c99 \ 40 | -Wall \ 41 | -Wpedantic \ 42 | -mabi=aapcs \ 43 | -mcpu=arm7tdmi \ 44 | -mthumb \ 45 | $(INCLUDES:%=-I%) \ 46 | $(INCLUDES:%=-Wa,-I%) 47 | 48 | BUILD = build 49 | LIB = lib 50 | 51 | 52 | # 53 | # You don't need to touch anything below this point! 54 | # 55 | 56 | TARGET = $(LIB)/lib$(PROJECT).a 57 | 58 | OBJECTS = $(SOURCES:%=$(BUILD)/obj/%.o) 59 | DEPENDS = $(SOURCES:%=$(BUILD)/dep/%.d) 60 | OBJDIRS = $(dir $(OBJECTS) $(DEPENDS)) 61 | 62 | $(TARGET): $(OBJECTS) 63 | @echo "$@" 64 | @$(AR) rcs $@ $^ 65 | 66 | $(OBJECTS): | builddirs 67 | 68 | $(BUILD)/obj/%.o: % 69 | @echo "$<" 70 | @$(CC) -c -o $@ $(CFLAGS) -MMD -MP -MF $(BUILD)/dep/$<.d $< 71 | 72 | builddirs: 73 | @mkdir -p $(OBJDIRS) $(LIB) 74 | 75 | clean: 76 | @echo "clean: $(BUILD) $(LIB)" 77 | @rm -rf $(BUILD) $(LIB) 78 | 79 | install: $(TARGET) 80 | @echo "install: $(DESTDIR)$(DEVKITPRO)" 81 | @mkdir -p $(DESTDIR)$(DEVKITPRO)/libseven/ 82 | @cp -r include $(DESTDIR)$(DEVKITPRO)/libseven/ 83 | @cp -r lib $(DESTDIR)$(DEVKITPRO)/libseven/ 84 | 85 | .PHONY: builddirs clean install 86 | 87 | -include $(DEPENDS) 88 | -------------------------------------------------------------------------------- /Makefile.dkp: -------------------------------------------------------------------------------- 1 | # 2 | # This Source Code Form is subject to the terms of the Mozilla Public 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this 4 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | # 6 | 7 | ifeq ($(strip $(DEVKITPRO)),) 8 | $(error "Please set DEVKITPRO in your environment.") 9 | endif 10 | 11 | include $(DEVKITARM)/base_tools 12 | include Makefile 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libseven 2 | 3 | A fully from-scratch library for GBA development, meant to 4 | replace existing libraries like libgba or libtonc. 5 | 6 | Reach out to us in the `#libseven` channel in the 7 | [gbadev discord](https://discord.io/gbadev). 8 | 9 | ### Goals 10 | 11 | - Intuitive, consistent, and well organized API 12 | - Optimized code with minimal RAM and ROM size 13 | - Correctness without compromises 14 | - High-quality, understandable documentation 15 | 16 | ### [Implementation Status](./doc/status.md) 17 | 18 | ### [Build, Installation, and Linking](./doc/build.md) 19 | 20 | ### [Documentation](./doc) 21 | 22 | ### Support 23 | 24 | Development of libseven and other GBA homebrew projects such as gba-minrt 25 | can be supported through [Patreon](https://patreon.com/LunarLambda). 26 | 27 | ### License 28 | 29 | All source code in this project is licensed under the 30 | Mozilla Public License Version 2.0.\ 31 | See [LICENSE.txt](./LICENSE.txt) for more information. 32 | 33 | All documentation in this project is licensed under the 34 | CC0 1.0 Universal license.\ 35 | See [LICENSE\_DOC.txt](./LICENSE_DOC.txt) for more information. 36 | -------------------------------------------------------------------------------- /doc/bios.md: -------------------------------------------------------------------------------- 1 | # Game Boy Advance — Official BIOS Behaviours 2 | 3 | Based on disassembly of legally dumped BIOS binary with SHA256 4 | `fd2547724b505f487e6dcb29ec2ecff3af35a841a77ab2e85fd87350abd36570`. 5 | 6 | ## SoftReset 7 | 8 | - Entry Address Flag loaded from 0x03FF\_FFFA 9 | - Stack Initialization 10 | - `sp_svc` = 0x0300\_7FE0 (64 bytes down) 11 | - `sp_irq` = 0x0300\_7FA0 (160 bytes down) 12 | - `sp_sys` = 0x0300\_7F00 13 | - SPSR of SVC and IRQ modes are zeroed out 14 | - Last 512 bytes of IWRAM are zeroed out (0x0300\_7E00 - 0x0300\_7FFF) 15 | - CPU Set to SYS mode, `r0` - `r12` zeroed out, CPSR flags and IRQ bits zeroed out 16 | - `r14` set to 0x0800\_0000 (ROM, Entry Address Flag == 0) or 0x0200\_0000 17 | (EWRAM, Entry Address Flag != 0) 18 | 19 | Note: During the clearing of IWRAM, IRQs are not masked, so resetting without 20 | disabling the IME could cause spurious breakage 21 | 22 | ## HardReset 23 | 24 | No noteworthy behaviour, the BIOS startup sequence with its logo and sound 25 | is repeated. The SoftReset sequence cleans everything up before the ROM 26 | runs. The end of the BIOS sequence also resets all registers and RAM, 27 | except when Entry Address Flag is != 0, then EWRAM and SIO are not reset. 28 | For a ROM cold-boot, then, there should be no observable artifact from the BIOS 29 | startup. 30 | 31 | On flashcarts, the current game remains mapped on Hard Reset, 32 | making it a potential fatal-crash recovery strategy, so long as no crashing IRQs 33 | occur in the short window before SVC 38 is dispatched. 34 | 35 | Although, since Hard Reset leaves no artifacts, detecting a crash-by-hard-reset 36 | may not be possible in all cases. Possible solutions may be hiding data in the 37 | cartridge GPIO ports or leaving a marker in the save medium. 38 | 39 | ## IRQ Entry 40 | 41 | - Save `r0`, `r1`, `r2`, `r3`, `r12`, and `r14_irq` to IRQ stack. 42 | - `r0` = 0x0400\_0000 43 | - Jump to address stored at 0x0300\_7FFC (mirrored at 0x03FF\_FFFC) in ARM mode 44 | - Restore regs and return 45 | 46 | CPSR I bit is set on IRQ entry. 47 | 48 | ## SVC Entry 49 | 50 | CPSR I bit is set on SVC entry, when BIOS functions are dispatched in 51 | SYS mode, the I bit from `spsr\_svc` is used. Only the LSB of the SVC number 52 | is used, so ARM mode SVCs require the number be shifted left by 16. 53 | 54 | `r2` is always preserved. 55 | 56 | ## FIQ Entry & Other Exceptions 57 | 58 | - `sp_fiq` = 0x0300\_7FF0 (16 bytes down) 59 | - Save `r12`, `lr_fiq`, `spsr_fiq`, and `cpsr` to FIQ stack. 60 | - If byte at 0x0800\_009C is 0xA5: 61 | - If byte at 0x0800\_00B4 has bit 7 set: 62 | - Jump to 0x09FF\_C000 in ARM mode 63 | - Else: 64 | - Jump to 0x09FE\_2000 65 | - Restore registers and return 66 | 67 | On non-debug ROMs this is essentially an expensive nop. The undefined 68 | instruction and abort vectors also jump to this code, so undefined instructions 69 | are generally ignored by the GBA hardware. 70 | -------------------------------------------------------------------------------- /doc/build.md: -------------------------------------------------------------------------------- 1 | # libseven - Building, Installation, Linking 2 | 3 | libseven itself can be built with a standard arm-none-eabi GCC toolchain and 4 | GNU make. 5 | 6 | ## Build 7 | 8 | Using a standard toolchain: 9 | 10 | ```sh 11 | make 12 | ``` 13 | 14 | Or with meson: 15 | 16 | ```sh 17 | meson --cross-file gba.txt build-meson 18 | meson compile -C build-meson 19 | ``` 20 | 21 | Using a devkitARM toolchain: 22 | 23 | ```sh 24 | make -f Makefile.dkp 25 | ``` 26 | 27 | If you get the message `please set DEVKITPRO in your environment`, make sure 28 | your shell sources `/etc/profile.d/devkit-env.sh`. 29 | 30 | ## Installation 31 | 32 | You can install libseven globally, into an existing devkitPRO installation. 33 | This can make it easier to integrate into projects, see 34 | "Linking" below. 35 | 36 | ### Makefile 37 | 38 | ```sh 39 | make -f Makefile.dkp 40 | sudo -E make -f Makefile.dkp install 41 | ``` 42 | 43 | Note: Windows/MSYS2 users don't need to use `sudo -E`. 44 | 45 | ## Uninstallation 46 | 47 | All installed files are contained in one directory, generally 48 | `$DEVKITPRO/libseven`. It can simply be removed, for example, 49 | to clean-install from the Makefile. 50 | 51 | ## Linking 52 | 53 | This method can be used if you would like to pull in libseven as a git 54 | submodule, or if writing examples for the project. 55 | 56 | - Set the path to libseven in your build system, i.e. in your Makefile 57 | - Add `$(LIBSEVEN)/include` to your include search path. 58 | - Add `$(LIBSEVEN)/lib` to your library search path. 59 | - Add `-lseven` to your linker flags. 60 | 61 | See the [template project](../examples/template/Makefile) 62 | for how to set this up. 63 | 64 | If using the devkitARM provided Makefiles, this will look like this: 65 | 66 | ```makefile 67 | #--------------------------------------------------------------------------------- 68 | # any extra libraries we wish to link with the project 69 | #--------------------------------------------------------------------------------- 70 | LIBS := -lmm -lseven 71 | 72 | 73 | #--------------------------------------------------------------------------------- 74 | # list of directories containing libraries, this must be the top level containing 75 | # include and lib 76 | #--------------------------------------------------------------------------------- 77 | LIBDIRS := $(DEVKITPRO)/libseven 78 | ``` 79 | -------------------------------------------------------------------------------- /doc/headers.md: -------------------------------------------------------------------------------- 1 | # Header Reference 2 | 3 | ## [seven](../include/seven) 4 | 5 | - ### [base.h](../include/seven/base.h) 6 | - ### [prelude.h](../include/seven/prelude.h) 7 | 8 | - ### [base](../include/seven/base) 9 | - [addresses.h](../include/seven/base/addresses.h) 10 | - [attributes.h](../include/seven/base/attributes.h) 11 | - [bits.h](../include/seven/base/bits.h) 12 | - [types.h](../include/seven/base/types.h) 13 | - [version.h](../include/seven/base/version.h) 14 | 15 | - ### [hw](../include/seven/hw) 16 | - [cpu.h](../include/seven/hw/cpu.h) 17 | - [dma.h](../include/seven/hw/dma.h) 18 | - [gpio.h](../include/seven/hw/gpio.h) 19 | - [input.h](../include/seven/hw/input.h) 20 | - [irq.h](../include/seven/hw/irq.h) 21 | - [memory.h](../include/seven/hw/memory.h) 22 | - [serial.h](../include/seven/hw/serial.h) 23 | - [sound.h](../include/seven/hw/sound.h) 24 | - [sram.h](../include/seven/hw/sram.h) 25 | - [svc.h](../include/seven/hw/svc.h) 26 | - [timer.h](../include/seven/hw/timer.h) 27 | - [video.h](../include/seven/hw/video.h) 28 | - [waitstate.h](../include/seven/hw/waitstate.h) 29 | 30 | - ### [serial](../include/seven/serial) 31 | - [joybus.h](../include/seven/serial/joybus.h) 32 | - [multiplayer.h](../include/seven/serial/multiplayer.h) 33 | - [raw.h](../include/seven/serial/raw.h) 34 | - [uart.h](../include/seven/serial/uart.h) 35 | 36 | - ### [svc](../include/seven/svc) 37 | - [affine.h](../include/seven/svc/affine.h) 38 | - [decompression.h](../include/seven/svc/decompression.h) 39 | - [math.h](../include/seven/svc/math.h) 40 | - [memory.h](../include/seven/svc/memory.h) 41 | - [reset.h](../include/seven/svc/reset.h) 42 | - [sound.h](../include/seven/svc/sound.h) 43 | - [system.h](../include/seven/svc/system.h) 44 | - [wait.h](../include/seven/svc/wait.h) 45 | 46 | - ### [util](../include/seven/util) 47 | - [log.h](../include/seven/util/log.h) 48 | - [math.h](../include/seven/util/math.h) 49 | - [memory.h](../include/seven/util/memory.h) 50 | - [overlay.h](../include/seven/util/overlay.h) 51 | - [profile.h](../include/seven/util/profile.h) 52 | - [random.h](../include/seven/util/random.h) 53 | - [string.h](../include/seven/util/string.h) 54 | 55 | - ### [video](../include/seven/video) 56 | - [bg_affine.h](../include/seven/video/bg_affine.h) 57 | - [bg_bitmap.h](../include/seven/video/bg_bitmap.h) 58 | - [bg_mixed.h](../include/seven/video/bg_mixed.h) 59 | - [bg_regular.h](../include/seven/video/bg_regular.h) 60 | - [blend.h](../include/seven/video/blend.h) 61 | - [color.h](../include/seven/video/color.h) 62 | - [matrix.h](../include/seven/video/matrix.h) 63 | - [mosaic.h](../include/seven/video/mosaic.h) 64 | - [oam.h](../include/seven/video/oam.h) 65 | - [object.h](../include/seven/video/object.h) 66 | - [palette.h](../include/seven/video/palette.h) 67 | - [prelude.h](../include/seven/video/prelude.h) 68 | - [scroll.h](../include/seven/video/scroll.h) 69 | - [window.h](../include/seven/video/window.h) 70 | -------------------------------------------------------------------------------- /doc/io/dispcnt.md: -------------------------------------------------------------------------------- 1 | # I/O Register Reference - Display Control 2 | 3 | Defined in [seven/hw/video.h](../../include/seven/hw/video.h) 4 | 5 | Address | Size | Access | GBATEK | libgba | libtonc | libseven 6 | :----------:|:----:|:------:|:--------|:--------|:------------|:-------- 7 | 0400 0000 | 16 | R/W | DISPCNT | DISPCNT | DISPCNT [¹] | DISPCNT 8 | 9 | ## Contents 10 | 11 | Bits | Access | Description | C Constants 12 | ----:|:------:|:-------------------------------------|:----------- 13 | 0-2 | R/W | Video mode | VIDEO_MODE_\* 14 | 3 | R/BIOS | Game Boy mode (0 = GBA, 1 = CGB) | - 15 | 4 | R/W | Bitmap frame select | VIDEO_FRAME_SELECT 16 | 5 | R/W | Disable object processing in H-Blank | - 17 | 6 | R/W | Object tile mapping (1 = 1D, 0 = 2D) | VIDEO_OBJ_MAPPING_1D / VIDEO_OBJ_MAPPING_2D 18 | 7 | R/W | Force display blank | VIDEO_FORCE_BLANK 19 | 8 | R/W | Display background 0 | VIDEO_BG0_ENABLE 20 | 9 | R/W | Display background 1 | VIDEO_BG1_ENABLE 21 | 10 | R/W | Display background 2 | VIDEO_BG2_ENABLE 22 | 11 | R/W | Display background 3 | VIDEO_BG3_ENABLE 23 | 12 | R/W | Display objects | VIDEO_OBJ_ENABLE 24 | 13 | R/W | Enable window 0 | VIDEO_WIN0_ENABLE 25 | 14 | R/W | Enable window 1 | VIDEO_WIN1_ENABLE 26 | 15 | R/W | Enable object window | VIDEO_OBJ_WIN_ENABLE 27 | 28 | ## VIDEO_MODE Bitfield 29 | 30 | Value | Description | C Constants 31 | -----:|:-------------------------------------|:----------- 32 | 0 | 4 regular backgrounds | VIDEO_MODE_REGULAR 33 | 1 | 2 regular + 1 affine background | VIDEO_MODE_MIXED 34 | 2 | 2 affine backgrounds | VIDEO_MODE_AFFINE 35 | 3 | 1 240x160 16-bpp framebuffer | VIDEO_MODE_BITMAP 36 | 4 | 2 240x160 8-bpp indexed framebuffers | VIDEO_MODE_BITMAP_INDEXED 37 | 5 | 2 160x128 16-bpp framebuffers | VIDEO_MODE_BITMAP_SMALL 38 | 39 | [¹]: ../registers.md#errata 40 | -------------------------------------------------------------------------------- /doc/io/dmacnt.md: -------------------------------------------------------------------------------- 1 | # I/O Register Reference - DMA Control 2 | 3 | Defined in [seven/hw/dma.h](../../include/seven/hw/dma.h) 4 | 5 | Address | Size | Access | GBATEK | libgba | libtonc | libseven 6 | :----------:|:----:|:------:|:----------|:-------|:----------|:-------- 7 | 0400 00BA | 16 | R/W | DMA0CNT_H | - | DMA0CNT_H | **DMA0CNT** 8 | 0400 00C6 | 16 | R/W | DMA1CNT_H | - | DMA1CNT_H | **DMA1CNT** 9 | 0400 00D2 | 16 | R/W | DMA2CNT_H | - | DMA2CNT_H | **DMA2CNT** 10 | 0400 00DE | 16 | R/W | DMA3CNT_H | - | DMA3CNT_H | **DMA3CNT** 11 | 12 | ## Contents 13 | 14 | Bits | Access | Description | C Constants 15 | -----:|:------:|:-----------------------------------|:----------- 16 | 0-4 | - | - | - 17 | 5-6 | R/W | Destination addressing mode | DMA_DST_\* 18 | 7-8 | R/W | Source addressing mode | DMA_SRC_\* 19 | 9 | R/W | Repeat transfer | DMA_REPEAT 20 | 10 | R/W | Unit size (0 = 16-bit, 1 = 32-bit) | DMA_16BIT, DMA_32BIT 21 | 11 | R/W | Respond to cartridge DMA request | - 22 | 12-13 | R/W | Start timing | DMA_START_\* 23 | 14 | R/W | Trigger interrupt on completion | DMA_IRQ_ENABLE 24 | 15 | R/W | Enable DMA channel | DMA_ENABLE 25 | 26 | ## DMA_DST Bitfield 27 | 28 | Value | Description | C Constants 29 | -----:|:------------------------------------------------------|:----------- 30 | 0 | Increment address | DMA_DST_INCREMENT 31 | 1 | Decrement address | DMA_DST_DECREMENT 32 | 2 | Do not change address | DMA_DST_FIXED 33 | 3 | Increment address and reload register upon completion | DMA_DST_RELOAD 34 | 35 | ## DMA_SRC Bitfield 36 | 37 | Value | Description | C Constants 38 | -----:|:----------------------|:----------- 39 | 0 | Increment address | DMA_SRC_INCREMENT 40 | 1 | Decrement address | DMA_SRC_DECREMENT 41 | 2 | Do not change address | DMA_SRC_FIXED 42 | 43 | ## DMA_START Bitfield 44 | 45 | Value | Description | C Constants 46 | -----:|:----------------------------------------------------|:----------- 47 | 0 | Start immediately | DMA_START_NOW 48 | 1 | Start at H-Blank | DMA_START_HBLANK 49 | 2 | Start at V-Blank | DMA_START_VBLANK 50 | 3 | Forbidden (DMA 0) | - 51 | 3 | Start when sound FIFOs empty (DMA 1, DMA 2) | DMA_START_SPECIAL / DMA_START_SOUND 52 | 3 | Start in scanline 2, run until scanline 162 (DMA 3) | DMA_START_SPECIAL / DMA_START_CAPTURE 53 | -------------------------------------------------------------------------------- /doc/io/ie.md: -------------------------------------------------------------------------------- 1 | # I/O Register Reference - Interrupt Enable 2 | 3 | Defined in [seven/hw/irq.h](../../include/seven/hw/irq.h) 4 | 5 | Address | Size | Access | GBATEK | libgba | libtonc | libseven 6 | :--------:|:----:|:------:|:-------|:-------|:--------|:-------- 7 | 0400 0200 | 16 | R/W | IE | IE | IE | IE 8 | 9 | ## Contents 10 | 11 | Bits | Description | C Constants 12 | -----:|:----------------------------|:----------- 13 | 0 | Start of V-Blank | IRQ_VBLANK 14 | 1 | Start of H-Blank | IRQ_HBLANK 15 | 2 | Scanline counter match | IRQ_VCOUNT 16 | 3 | Timer 0 overflow | IRQ_TIMER_0 17 | 4 | Timer 1 overflow | IRQ_TIMER_1 18 | 5 | Timer 2 overflow | IRQ_TIMER_2 19 | 6 | Timer 3 overflow | IRQ_TIMER_3 20 | 7 | Serial communication | IRQ_SERIAL 21 | 8 | DMA 0 transfer completion | IRQ_DMA_0 22 | 9 | DMA 1 transfer completion | IRQ_DMA_1 23 | 10 | DMA 2 transfer completion | IRQ_DMA_2 24 | 11 | DMA 3 transfer completion | IRQ_DMA_3 25 | 12 | D-pad/button input | IRQ_KEYPAD 26 | 13 | Cartridge interrupt request | IRQ_CARTRIDGE 27 | 14-15 | - | - 28 | -------------------------------------------------------------------------------- /doc/registers.md: -------------------------------------------------------------------------------- 1 | # I/O Register Reference 2 | 3 | Address | Size | Access | GBATEK | libgba | libtonc | libseven 4 | :----------:|:----:|:------:|:------------|:----------------|:------------|:-------- 5 | [0400 0000] | 16 | R/W | DISPCNT | DISPCNT | DISPCNT [¹] | DISPCNT 6 | 0400 0004 | 16 | R/W | DISPSTAT | DISPSTAT | DISPSTAT | DISPSTAT 7 | 0400 0006 | 16/8 | R | VCOUNT | VCOUNT [²] | VCOUNT [²] | VCOUNT 8 | 0400 0008 | 16 | R/W | BG0CNT | BG0CNT | BG0CNT | BG0CNT 9 | 0400 000A | 16 | R/W | BG1CNT | BG1CNT | BG1CNT | BG1CNT 10 | 0400 000C | 16 | R/W | BG2CNT | BG2CNT | BG2CNT | BG2CNT 11 | 0400 000E | 16 | R/W | BG3CNT | BG3CNT | BG3CNT | BG3CNT 12 | 0400 0010 | 16 | W | BG0HOFS | BG0HOFS [³] | BG0HOFS | BG0HOFS 13 | 0400 0012 | 16 | W | BG0VOFS | BG0VOFS [³] | BG0VOFS | BG0VOFS 14 | 0400 0014 | 16 | W | BG1HOFS | BG1HOFS [³] | BG1HOFS | BG1HOFS 15 | 0400 0016 | 16 | W | BG1VOFS | BG1VOFS [³] | BG1VOFS | BG1VOFS 16 | 0400 0018 | 16 | W | BG2HOFS | BG2HOFS [³] | BG2HOFS | BG2HOFS 17 | 0400 001A | 16 | W | BG2VOFS | BG2VOFS [³] | BG2VOFS | BG2VOFS 18 | 0400 001C | 16 | W | BG3HOFS | BG3HOFS [³] | BG3HOFS | BG3HOFS 19 | 0400 001E | 16 | W | BG3VOFS | BG3VOFS [³] | BG3VOFS | BG3VOFS 20 | 0400 0020 | 16 | W | BG2PA | BG2PA [³] | BG2PA | BG2PA 21 | 0400 0022 | 16 | W | BG2PB | BG2PB [³] | BG2PB | BG2PB 22 | 0400 0024 | 16 | W | BG2PC | BG2PC [³] | BG2PC | BG2PC 23 | 0400 0026 | 16 | W | BG2PD | BG2PD [³] | BG2PD | BG2PD 24 | 0400 0028 | 32 | W | BG2X | BG2X [³] | BG2X [⁴] | BG2X 25 | 0400 002C | 32 | W | BG2Y | BG2Y [³] | BG2Y [⁴] | BG2Y 26 | 0400 0030 | 16 | W | BG3PA | BG3PA [³] | BG3PA | BG3PA 27 | 0400 0032 | 16 | W | BG3PB | BG3PB [³] | BG3PB | BG3PB 28 | 0400 0034 | 16 | W | BG3PC | BG3PC [³] | BG3PC | BG3PC 29 | 0400 0036 | 16 | W | BG3PD | BG3PD [³] | BG3PD | BG3PD 30 | 0400 0038 | 32 | W | BG3X | BG3X [³] | BG3X [⁴] | BG3X 31 | 0400 003C | 32 | W | BG3Y | BG3Y [³] | BG3Y [⁴] | BG3Y 32 | 0400 0040 | 16 | W | WIN0H | WIN0H | WIN0H | WIN0H 33 | 0400 0042 | 16 | W | WIN1H | WIN1H | WIN1H | WIN1H 34 | 0400 0044 | 16 | W | WIN0V | WIN0V | WIN0V | WIN0V 35 | 0400 0046 | 16 | W | WIN1V | WIN1V | WIN1V | WIN1V 36 | 0400 0048 | 16 | R/W | WININ | WININ | WININ | WININ 37 | 0400 004A | 16 | R/W | WINOUT | WINOUT | WINOUT | WINOUT 38 | 0400 004C | 16 | W | MOSAIC | MOSAIC | MOSAIC | MOSAIC 39 | 0400 0050 | 16 | R/W | BLDCNT | BLDCNT | BLDCNT | BLDCNT 40 | 0400 0052 | 16 | R/W | BLDALPHA | BLDALPHA | BLDALPHA | BLDALPHA 41 | 0400 0054 | 16 | W | BLDY | BLDY | BLDY | **BLDVAL** 42 | ​||||||| 43 | 0400 0060 | 16 | R/W | SOUND1CNT_L | SOUND1CNT_L | SOUND1CNT_L | - 44 | 0400 0062 | 16 | R/W | SOUND1CNT_H | SOUND1CNT_H | SOUND1CNT_H | - 45 | 0400 0064 | 16 | R/W | SOUND1CNT_X | SOUND1CNT_X | SOUND1CNT_X | - 46 | 0400 0068 | 16 | R/W | SOUND2CNT_L | SOUND2CNT_L | SOUND2CNT_L | - 47 | 0400 006C | 16 | R/W | SOUND2CNT_H | SOUND2CNT_H | SOUND2CNT_H | - 48 | 0400 0070 | 16 | R/W | SOUND3CNT_L | SOUND3CNT_L | SOUND3CNT_L | - 49 | 0400 0072 | 16 | R/W | SOUND3CNT_H | SOUND3CNT_H | SOUND3CNT_H | - 50 | 0400 0074 | 16 | R/W | SOUND3CNT_X | SOUND3CNT_X | SOUND3CNT_X | - 51 | 0400 0078 | 16 | R/W | SOUND4CNT_L | SOUND4CNT_L | SOUND4CNT_L | - 52 | 0400 007C | 16 | R/W | SOUND4CNT_H | SOUND4CNT_H | SOUND4CNT_H | - 53 | 0400 0080 | 16 | R/W | SOUNDCNT_L | SOUNDCNT_L | SOUNDCNT_L | - 54 | 0400 0082 | 16 | R/W | SOUNDCNT_H | SOUNDCNT_H | SOUNDCNT_H | - 55 | 0400 0084 | 16 | R/W | SOUNDCNT_X | SOUNDCNT_X | SOUNDCNT_X | - 56 | 0400 0088 | 16 | R/W | SOUNDBIAS | SOUNDBIAS | SOUNDBIAS | - 57 | 0400 0090 | 32x4 | R/W | WAVE_RAM | WAVE_RAM | WAVE_RAM [¹]| - 58 | 0400 00A0 | 32 | W | FIFO_A | FIFO_A | FIFO_A | - 59 | 0400 00A4 | 32 | W | FIFO_B | FIFO_B | FIFO_B | - 60 | ​||||||| 61 | 0400 00B0 | 32 | W | DMA0SAD | DMA0SAD | DMA0SAD | **DMA0SRC** 62 | 0400 00B4 | 32 | W | DMA0DAD | DMA0DAD | DMA0DAD | **DMA0DST** 63 | 0400 00B8 | 16 | W | DMA0CNT_L | **DMA0CNT** [¹] | DMA0CNT_L | **DMA0LEN** 64 | [0400 00BA] | 16 | R/W | DMA0CNT_H | - | DMA0CNT_H | **DMA0CNT** 65 | 0400 00BC | 32 | W | DMA1SAD | DMA1SAD | DMA1SAD | **DMA1SRC** 66 | 0400 00C0 | 32 | W | DMA1DAD | DMA1DAD | DMA1DAD | **DMA1DST** 67 | 0400 00C4 | 16 | W | DMA1CNT_L | **DMA1CNT** [¹] | DMA1CNT_L | **DMA1LEN** 68 | [0400 00C6] | 16 | R/W | DMA1CNT_H | - | DMA1CNT_H | **DMA1CNT** 69 | 0400 00C8 | 32 | W | DMA2SAD | DMA2SAD | DMA2SAD | **DMA2SRC** 70 | 0400 00CC | 32 | W | DMA2DAD | DMA2DAD | DMA2DAD | **DMA2DST** 71 | 0400 00D0 | 16 | W | DMA2CNT_L | **DMA2CNT** [¹] | DMA2CNT_L | **DMA2LEN** 72 | [0400 00D2] | 16 | R/W | DMA2CNT_H | - | DMA2CNT_H | **DMA2CNT** 73 | 0400 00D4 | 32 | W | DMA3SAD | DMA3SAD | DMA3SAD | **DMA3SRC** 74 | 0400 00D8 | 32 | W | DMA3DAD | DMA3DAD | DMA3DAD | **DMA3DST** 75 | 0400 00DC | 16 | W | DMA3CNT_L | **DMA3CNT** [¹] | DMA3CNT_L | **DMA3LEN** 76 | [0400 00DE] | 16 | R/W | DMA3CNT_H | - | DMA3CNT_H | **DMA3CNT** 77 | ​||||||| 78 | 0400 0100 | 16 | R/W | TM0CNT_L | TM0CNT_L | TM0CNT_L | **TM0VAL** 79 | 0400 0102 | 16 | R/W | TM0CNT_H | TM0CNT_H | TM0CNT_H | **TM0CNT** 80 | 0400 0104 | 16 | R/W | TM1CNT_L | TM1CNT_L | TM1CNT_L | **TM1VAL** 81 | 0400 0106 | 16 | R/W | TM1CNT_H | TM1CNT_H | TM1CNT_H | **TM1CNT** 82 | 0400 0108 | 16 | R/W | TM2CNT_L | TM2CNT_L | TM2CNT_L | **TM2VAL** 83 | 0400 010A | 16 | R/W | TM2CNT_H | TM2CNT_H | TM2CNT_H | **TM2CNT** 84 | 0400 010C | 16 | R/W | TM3CNT_L | TM3CNT_L | TM3CNT_L | **TM3VAL** 85 | 0400 010E | 16 | R/W | TM3CNT_H | TM3CNT_H | TM3CNT_H | **TM3CNT** 86 | ​||||||| 87 | 0400 0120 | 32 | R/W | SIODATA32 | SIODATA32 | SIODATA32 | SIODATA32 88 | 0400 0120 | 16 | R/W | SIOMULTI0 | SIOMULTI0 | SIOMULTI0 | - 89 | 0400 0122 | 16 | R/W | SIOMULTI1 | SIOMULTI1 | SIOMULTI1 | - 90 | 0400 0124 | 16 | R/W | SIOMULTI2 | SIOMULTI2 | SIOMULTI2 | - 91 | 0400 0126 | 16 | R/W | SIOMULTI3 | SIOMULTI3 | SIOMULTI3 | - 92 | 0400 0128 | 16 | R/W | SIOCNT | SIOCNT | SIOCNT | SIOCNT 93 | 0400 012A | 16 | R/W | SIOMLT_SEND | SIOMLT_SEND | SIOMLT_SEND | - 94 | 0400 012A | 16/8 | R/W | SIODATA8 | SIODATA8 | SIODATA8 | SIODATA8 95 | ​||||||| 96 | 0400 0130 | 16 | R | KEYINPUT | KEYINPUT [²] | KEYINPUT [²]| KEYINPUT 97 | 0400 0132 | 16 | R/W | KEYCNT | KEYCNT | KEYCNT | KEYCNT 98 | ​||||||| 99 | 0400 0134 | 16 | R/W | RCNT | RCNT | RCNT | **SIOMODE2** 100 | 0400 0140 | 16 | R/W | JOYCNT | **HS_CTRL** | JOYCNT | - 101 | 0400 0150 | 32 | R/W | JOY_RECV | **JOYRE** | JOY_RECV | - 102 | 0400 0154 | 32 | R/W | JOY_TRANS | **JOYTR** | JOY_TRANS | - 103 | 0400 0158 | 16 | R | JOYSTAT | **JSTAT** [²] | JOYSTAT [²] | - 104 | ​||||||| 105 | [0400 0200] | 16 | R/W | IE | IE | IE | IE 106 | 0400 0202 | 16 | R/W | IF | IF | IF | IF 107 | 0400 0204 | 16 | R/W | WAITCNT | - | WAITCNT | WAITCNT 108 | 0400 0208 | 16/1 | R/W | IME | IME | IME | IME 109 | 110 | ## Errata 111 | 112 | 1. Register is not marked as 16-bit 113 | 2. Register is not marked as read-only 114 | 3. Register is not marked as volatile 115 | 116 | [¹]: #errata 117 | [²]: #errata 118 | [³]: #errata 119 | 120 | [0400 0000]: io/dispcnt.md 121 | [0400 00BA]: io/dmacnt.h 122 | [0400 00C6]: io/dmacnt.h 123 | [0400 00D2]: io/dmacnt.h 124 | [0400 00DE]: io/dmacnt.h 125 | [0400 0200]: io/ie.md 126 | -------------------------------------------------------------------------------- /doc/rnd/fastbios.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_UTIL_FASTBIOS_H 8 | #define _LIBSEVEN_UTIL_FASTBIOS_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | enum BiosFunctionAddress 15 | { 16 | BIOSADDR_SOFTRESET = 0x00B4, 17 | BIOSADDR_REGISTERRAMRESET = 0x09C3, /* THUMB */ 18 | BIOSADDR_HALT = 0x01A0, 19 | BIOSADDR_STOP = 0x01A8, 20 | BIOSADDR_INTRWAIT = 0x0330, 21 | BIOSADDR_VBLANKINTRWAIT = 0x0328, 22 | BIOSADDR_DIV = 0x03B4, 23 | /* SVC 7 / DIVARM */ 24 | BIOSADDR_SQRT = 0x0404, 25 | BIOSADDR_ARCTAN = 0x0474, 26 | BIOSADDR_ARCTAN2 = 0x04FD, /* THUMB */ 27 | BIOSADDR_CPUSET = 0x0B4D, /* THUMB */ 28 | BIOSADDR_CPUFASTSET = 0x0BC4, 29 | BIOSADDR_BIOSCHECKSUM = 0x0378, 30 | BIOSADDR_OBJAFFINESET = 0x0C2C, 31 | BIOSADDR_BGAFFINESET = 0x0CE0, 32 | BIOSADDR_BITUNPACK = 0x0F60, 33 | BIOSADDR_LZ77UNCOMPWRAM = 0x10FC, 34 | BIOSADDR_LZ77UNCOMPVRAM = 0x1194, 35 | BIOSADDR_HUFFUNCOMP = 0x1014, 36 | BIOSADDR_RLUNCOMPWRAM = 0x1279, /* THUMB */ 37 | BIOSADDR_RLUNCOMPVRAM = 0x12C1, /* THUMB */ 38 | BIOSADDR_DIFF8BITUNFILTERWRAM = 0x1333, /* THUMB */ 39 | BIOSADDR_DIFF8BITUNFILTERVRAM = 0x135D, /* THUMB */ 40 | BIOSADDR_DIFF16BITUNFILTER = 0x1399, /* THUMB */ 41 | BIOSADDR_SOUNDBIAS = 0x0801, /* THUMB */ 42 | BIOSADDR_SOUNDDRIVERINIT = 0x1665, /* THUMB */ 43 | BIOSADDR_SOUNDDRIVERMODE = 0x179D, /* THUMB */ 44 | BIOSADDR_SOUNDDRIVERMAIN = 0x1DC5, /* THUMB */ 45 | BIOSADDR_SOUNDDRIVERVSYNC = 0x210D, /* THUMB */ 46 | BIOSADDR_SOUNDCHANNELCLEAR = 0x1825, /* THUMB */ 47 | BIOSADDR_MIDIKEY2FREQ = 0x18D9, /* THUMB */ 48 | BIOSADDR_MUSICPLAYEROPEN = 0x13C5, /* THUMB */ 49 | BIOSADDR_MUSICPLAYERSTART = 0x1435, /* THUMB */ 50 | BIOSADDR_MUSICPLAYERSTOP = 0x14C1, /* THUMB */ 51 | BIOSADDR_MUSICPLAYERCONTINUE = 0x14FD, /* THUMB */ 52 | BIOSADDR_MUSICPLAYERFADEOUT = 0x1515, /* THUMB */ 53 | BIOSADDR_MULTIBOOT = 0x28CF, /* THUMB */ 54 | BIOSADDR_HARDRESET = 0x008C, 55 | /* SVC 39 */ 56 | BIOSADDR_SOUNDDRIVERVSYNCOFF = 0x1879, /* THUMB */ 57 | BIOSADDR_SOUNDDRIVERVSYNCON = 0x18C9, /* THUMB */ 58 | /* SVC 42 */ 59 | }; 60 | 61 | _LIBSEVEN_EXTERN_C_END 62 | 63 | #endif /* !_LIBSEVEN_UTIL_FASTBIOS_H */ 64 | -------------------------------------------------------------------------------- /doc/rnd/overlay.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | .include "seven/asm/base.s" 12 | 13 | CONST N_OVERLAYS, 10 14 | 15 | rodata IWRAM_OVERLAYS 16 | .word __iwram_overlay_vma 17 | .word __load_start_iwram0 18 | .word __load_stop_iwram0 19 | .word __load_start_iwram1 20 | .word __load_stop_iwram1 21 | .word __load_start_iwram2 22 | .word __load_stop_iwram2 23 | .word __load_start_iwram3 24 | .word __load_stop_iwram3 25 | .word __load_start_iwram4 26 | .word __load_stop_iwram4 27 | .word __load_start_iwram5 28 | .word __load_stop_iwram5 29 | .word __load_start_iwram6 30 | .word __load_stop_iwram6 31 | .word __load_start_iwram7 32 | .word __load_stop_iwram7 33 | .word __load_start_iwram8 34 | .word __load_stop_iwram8 35 | .word __load_start_iwram9 36 | .word __load_stop_iwram9 37 | endr 38 | 39 | rodata EWRAM_OVERLAYS 40 | .word __ewram_overlay_vma 41 | .word __load_start_ewram0 42 | .word __load_stop_ewram0 43 | .word __load_start_ewram1 44 | .word __load_stop_ewram1 45 | .word __load_start_ewram2 46 | .word __load_stop_ewram2 47 | .word __load_start_ewram3 48 | .word __load_stop_ewram3 49 | .word __load_start_ewram4 50 | .word __load_stop_ewram4 51 | .word __load_start_ewram5 52 | .word __load_stop_ewram5 53 | .word __load_start_ewram6 54 | .word __load_stop_ewram6 55 | .word __load_start_ewram7 56 | .word __load_stop_ewram7 57 | .word __load_start_ewram8 58 | .word __load_stop_ewram8 59 | .word __load_start_ewram9 60 | .word __load_stop_ewram9 61 | endr 62 | 63 | fn overlayInitIwram thumb 64 | movs r3, #0 65 | cmp r0, #N_OVERLAYS 66 | bge 1f 67 | 68 | ldr r3, =IWRAM_OVERLAYS 69 | ldm r3!, {r2} 70 | lsls r0, r0, #3 71 | adds r3, r3, r0 72 | ldm r3!, {r0, r1} 73 | 74 | bne overlayCopy 75 | 76 | 1: 77 | 78 | bx lr 79 | endfn 80 | 81 | fn overlayInitIwram thumb 82 | movs r3, #0 83 | cmp r0, #N_OVERLAYS 84 | bge 1f 85 | 86 | ldr r1, =EWRAM_OVERLAYS 87 | ldm r1!, {r3} 88 | lsls r0, r0, #3 89 | adds r1, r1, r0 90 | ldm r1!, {r1, r2} 91 | subs r0, r2, r1 92 | 93 | bne overlayCopy 94 | 95 | 1: 96 | bx lr 97 | endfn 98 | 99 | fn overlayCopy thumb local 100 | 1: 101 | ldm r1!, {r3} 102 | stm r0!, {r3} 103 | cmp r1, r2 104 | blt 1b 105 | 106 | pop {r4, r5} 107 | bx lr 108 | endfn 109 | 110 | @ vim: ft=armv4 et sta sw=4 sts=8 111 | -------------------------------------------------------------------------------- /doc/status.md: -------------------------------------------------------------------------------- 1 | # libseven - Implementation Status 2 | 3 | ## GBA Hardware 4 | 5 | - [x] DMA 6 | - [x] IRQ 7 | - [x] Input 8 | - [ ] SIO 9 | - [x] Normal mode 10 | - [ ] UART mode 11 | - [ ] Multiplayer mode 12 | - [ ] Raw mode 13 | - [ ] JOY Bus mode 14 | - [ ] Sound 15 | - [x] SRAM 16 | - [x] Timers 17 | - [x] Video 18 | - [x] Control and Status Registers 19 | - [x] Affine Backgrounds 20 | - [x] Bitmap Backgrounds 21 | - [x] Regular Backgrounds 22 | - [x] Blending Effects 23 | - [x] Colors 24 | - [x] Affine Matrices 25 | - [x] Mosaic Effects 26 | - [x] OAM Management 27 | - [x] Objects 28 | - [ ] Pallete Management 29 | - [x] Scrolling Effects 30 | - [x] Windowing Effects 31 | - [x] BIOS Calls 32 | - [x] Reset 33 | - [x] IRQ Waiting 34 | - [x] Math 35 | - [x] Memory Copy 36 | - [x] BIOS Checksum 37 | - [x] Affine Functions 38 | - [x] Decompression 39 | - [ ] Sound/MP2000 40 | - [ ] Multiboot 41 | 42 | ## Utility APIs 43 | 44 | - [x] Bit manipulation helpers, including named bitfields 45 | - [x] Logging (mGBA, no$gba, VBA, Custom) 46 | - [x] Fast PRNG (Xoshiro128++) 47 | - [x] Basic profiler routine 48 | - [ ] Fixed point helpers 49 | - [ ] Fast string routines 50 | - [ ] Fast memory routines 51 | - [ ] Fast math routines 52 | 53 | ## Toolchain Features 54 | 55 | - [x] -ffreestanding support 56 | - [x] --gc-sections support 57 | - [x] RAM section support 58 | - [ ] RAM overlay support 59 | - [ ] LTO support (needs testing) 60 | -------------------------------------------------------------------------------- /doc/svc.md: -------------------------------------------------------------------------------- 1 | # Supervisor Call Reference 2 | 3 | Name | SVC Number | Implemented | Notes 4 | :-----------------------|:----------:|:-----------:|:----- 5 | SoftReset | 0 | ✔️ | 6 | RegisterRamReset | 1 | ✔️ | 7 | Halt | 2 | ✔️ | 8 | Stop | 3 | ✔️ | 9 | IntrWait | 4 | ✔️ | 10 | VBlankIntrWait | 5 | ✔️ | 11 | Div | 6 | ✔️ | 12 | DivArm | 7 | ❌ | Redundant 13 | Sqrt | 8 | ✔️ | 14 | ArcTan | 9 | ✔️ | 15 | ArcTan2 | 10 | ✔️ | 16 | CpuSet | 11 | ✔️ | 17 | CpuFastSet | 12 | ✔️ | 18 | BiosChecksum | 13 | ✔️ | Undocumented 19 | BgAffineSet | 14 | ✔️ | 20 | ObjAffineSet | 15 | ✔️ | 21 | BitUnPack | 16 | ✔️ | 22 | LZ77UnCompWram | 17 | ✔️ | 23 | LZ77UnCompVram | 18 | ✔️ | 24 | HuffUnComp | 19 | ✔️ | 25 | RLUnCompWram | 20 | ✔️ | 26 | RLUnCompVram | 21 | ✔️ | 27 | Diff8bitUnFilterWram | 22 | ✔️ | 28 | Diff8bitUnFilterVram | 23 | ✔️ | 29 | Diff16bitUnFilter | 24 | ✔️ | 30 | SoundBiasChange | 25 | ❌ | 31 | SoundDriverInit | 26 | ❌ | 32 | SoundDriverMode | 27 | ❌ | 33 | SoundDriverMain | 28 | ✔️ | 34 | SoundDriverVSync | 29 | ✔️ | 35 | SoundDriverChannelClear | 30 | ✔️ | 36 | MidiKey2Freq | 31 | ❌ | 37 | MusicPlayerOpen | 32 | ❌ | 38 | MusicPlayerStart | 33 | ❌ | 39 | MusicPlayerStop | 34 | ❌ | 40 | MusicPlayerContinue | 35 | ❌ | 41 | MusicPlayerFadeOut | 36 | ❌ | 42 | MultiBoot | 37 | ❌ | 43 | HardReset | 38 | ✔️ | Undocumented, resets back to BIOS start animation 44 | ? | 39 | ❌ | Undocumented, used internally by Stop/Halt 45 | SoundDriverVSyncOff | 40 | ✔️ | 46 | SoundDriverVSyncOn | 41 | ✔️ | 47 | ? | 42 | ❌ | Undocumented, copies pointers to sound related functions 48 | -------------------------------------------------------------------------------- /doc/video_mode.md: -------------------------------------------------------------------------------- 1 | # Video Mode Reference 2 | 3 | Mode | BG0 | BG1 | BG2 | BG3 | Scroll | Transform | Object VRAM 4 | :---:|:---:|:---:|:---:|:---:|:-----------|:----------|:----------- 5 | 0 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ❌ | 32K 6 | 1 | ✔️ | ✔️ | ✔️ | ❌ | ✔️ BG0, BG1 | ✔️ BG2 | 32K 7 | 2 | ❌ | ❌ | ✔️ | ✔️ | ❌ | ✔️ | 32K 8 | 3 | ❌ | ❌ | ✔️ | ❌ | ❌ | ✔️ | 16K 9 | 4 | ❌ | ❌ | ✔️ | ❌ | ❌ | ✔️ | 16K 10 | 5 | ❌ | ❌ | ✔️ | ❌ | ❌ | ✔️ | 16K 11 | 6 | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | 16K? 12 | 7 | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | 16K? 13 | 14 | Objects, blending, mosaic and windows are always available. 15 | 16 | Modes 6 and 7 are not intended to be used and only display objects. 17 | -------------------------------------------------------------------------------- /examples/meson.build: -------------------------------------------------------------------------------- 1 | subdir('template') 2 | subdir('vsync') 3 | subdir('vsync-callback') 4 | -------------------------------------------------------------------------------- /examples/template/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /examples/template/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This Source Code Form is subject to the terms of the Mozilla Public 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this 4 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | # 6 | 7 | .SUFFIXES: 8 | 9 | # Path to gba-minrt 10 | MINRT := ../../subprojects/minrt 11 | 12 | # Path to libseven 13 | LIBSEVEN := ../.. 14 | 15 | # Name of your ROM 16 | PROJECT := libseven-template 17 | 18 | # Source files 19 | SOURCES := $(MINRT)/src/crt0.s src/main.c 20 | 21 | # Include directories 22 | INCLUDES := $(MINRT)/src 23 | 24 | # Library directories, with /include and /lib 25 | LIBDIRS := $(MINRT) $(LIBSEVEN) 26 | 27 | # Libraries to link 28 | LIBS := seven 29 | 30 | # All build output goes here 31 | BUILDDIR := build 32 | 33 | # C compiler flags 34 | CFLAGS := -g3 -gdwarf-4 -O2 -std=c99 -ffunction-sections -fdata-sections 35 | 36 | # Linker flags 37 | LDFLAGS := -mthumb -nostartfiles \ 38 | -specs=nano.specs -specs=nosys.specs \ 39 | -Wl,-Trom.ld 40 | 41 | # Toolchain prefix 42 | # 43 | # Only change this if you want to use a different compiler 44 | TOOLCHAIN := arm-none-eabi 45 | 46 | # Uncomment this if you want to use C++ 47 | # 48 | # USE_CXX := yes 49 | 50 | # Uncomment this if you want to use Link Time Optimization 51 | # 52 | # USE_LTO := yes 53 | 54 | # 55 | # Internal 56 | # 57 | 58 | CC := $(TOOLCHAIN)-gcc 59 | CXX := $(TOOLCHAIN)-g++ 60 | OBJCOPY := $(TOOLCHAIN)-objcopy 61 | LD := $(if $(USE_CXX),$(CXX),$(CC)) 62 | 63 | ELFFILE := $(BUILDDIR)/$(PROJECT).elf 64 | ROMFILE := $(BUILDDIR)/$(PROJECT).gba 65 | MAPFILE := $(BUILDDIR)/$(PROJECT).map 66 | 67 | default: $(ROMFILE) 68 | 69 | CFLAGS += \ 70 | -mcpu=arm7tdmi \ 71 | -mabi=aapcs \ 72 | -mthumb \ 73 | $(LIBDIRS:%=-I%/include) \ 74 | $(INCLUDES:%=-I%) \ 75 | $(if $(USE_LTO),-flto,-fno-lto) \ 76 | 77 | LDFLAGS += \ 78 | -Wl,--gc-sections \ 79 | -Wl,-Map,$(MAPFILE) \ 80 | $(LIBDIRS:%=-L%/lib) \ 81 | $(LIBS:%=-l%) \ 82 | $(if $(USE_LTO),-flto,-fno-lto) \ 83 | 84 | OBJECTS := 85 | DEPENDS := 86 | 87 | SPACE := $(subst ,, ) 88 | 89 | eq = $(and $(findstring x$(1),x$(2)),$(findstring x$(2),x$(1))) 90 | 91 | pathmap = $(2)$(subst $(SPACE),/,$(foreach component,$(subst /, ,$(1)),$(if $(call eq,$(component),..),__,$(component))))$(3) 92 | 93 | obj = $(call pathmap,$(1),$(BUILDDIR)/obj/,.o) 94 | dep = $(call pathmap,$(1),$(BUILDDIR)/dep/,.d) 95 | 96 | define compile = 97 | $(call obj,$(1)): $(1) 98 | @echo "compile $$<" 99 | @$$(CC) -c -o $$@ $$(CFLAGS) -MMD -MP -MF $(call dep,$(1)) $$< 100 | 101 | OBJECTS += $(call obj,$(1)) 102 | DEPENDS += $(call dep,$(1)) 103 | endef 104 | 105 | $(foreach source,$(SOURCES),$(eval $(call compile,$(source)))) 106 | 107 | DIRS := $(dir $(BUILDDIR) $(OBJECTS) $(DEPENDS)) 108 | 109 | $(ELFFILE): $(OBJECTS) 110 | $(ROMFILE): $(ELFFILE) 111 | $(OBJECTS): | dirs 112 | 113 | %.elf: 114 | @echo "link $@" 115 | @$(LD) -o $@ $^ $(LDFLAGS) 116 | 117 | %.gba: 118 | @echo "objcopy $@" 119 | @$(OBJCOPY) -O binary $< $@ 120 | 121 | dirs: 122 | @mkdir -p $(DIRS) 123 | 124 | clean: 125 | @echo "clean $(BUILDDIR)" 126 | @rm -rf $(BUILDDIR) 127 | 128 | run: $(ELFFILE) 129 | @mgba-qt $(ELFFILE) 130 | 131 | .PHONY: default dirs clean run 132 | 133 | -include $(DEPENDS) 134 | -------------------------------------------------------------------------------- /examples/template/meson.build: -------------------------------------------------------------------------------- 1 | executable('example-template', 'src/main.c', 2 | dependencies: [dependency('libseven'), dependency('minrt')], 3 | name_suffix: 'elf') 4 | -------------------------------------------------------------------------------- /examples/template/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | REG_DISPCNT = VIDEO_MODE_BITMAP | VIDEO_BG2_ENABLE; 7 | 8 | MODE3_FRAME[80][120] = COLOR_RED; 9 | MODE3_FRAME[80][136] = COLOR_GREEN; 10 | MODE3_FRAME[96][120] = COLOR_BLUE; 11 | 12 | while (1) {} 13 | } 14 | -------------------------------------------------------------------------------- /examples/vsync-callback/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /examples/vsync-callback/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This Source Code Form is subject to the terms of the Mozilla Public 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this 4 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | # 6 | 7 | .SUFFIXES: 8 | 9 | # Path to gba-minrt 10 | MINRT := ../../subprojects/minrt 11 | 12 | # Path to libseven 13 | LIBSEVEN := ../.. 14 | 15 | # Name of your ROM 16 | PROJECT := example-vsync-callback 17 | 18 | # Source files 19 | SOURCES := $(MINRT)/src/crt0.s src/main.c 20 | 21 | # Include directories 22 | INCLUDES := $(MINRT)/src 23 | 24 | # Library directories, with /include and /lib 25 | LIBDIRS := $(MINRT) $(LIBSEVEN) 26 | 27 | # Libraries to link 28 | LIBS := seven 29 | 30 | # All build output goes here 31 | BUILDDIR := build 32 | 33 | # C compiler flags 34 | CFLAGS := -g3 -gdwarf-4 -O2 -std=c99 -ffunction-sections -fdata-sections 35 | 36 | # Linker flags 37 | LDFLAGS := -mthumb -nostartfiles \ 38 | -specs=nano.specs -specs=nosys.specs \ 39 | -Wl,-Trom.ld 40 | 41 | # Toolchain prefix 42 | # 43 | # Only change this if you want to use a different compiler 44 | TOOLCHAIN := arm-none-eabi 45 | 46 | # Uncomment this if you want to use C++ 47 | # 48 | # USE_CXX := yes 49 | 50 | # Uncomment this if you want to use Link Time Optimization 51 | # 52 | # USE_LTO := yes 53 | 54 | # 55 | # Internal 56 | # 57 | 58 | CC := $(TOOLCHAIN)-gcc 59 | CXX := $(TOOLCHAIN)-g++ 60 | OBJCOPY := $(TOOLCHAIN)-objcopy 61 | LD := $(if $(USE_CXX),$(CXX),$(CC)) 62 | 63 | ELFFILE := $(BUILDDIR)/$(PROJECT).elf 64 | ROMFILE := $(BUILDDIR)/$(PROJECT).gba 65 | MAPFILE := $(BUILDDIR)/$(PROJECT).map 66 | 67 | default: $(ROMFILE) 68 | 69 | CFLAGS += \ 70 | -mcpu=arm7tdmi \ 71 | -mabi=aapcs \ 72 | -mthumb \ 73 | $(LIBDIRS:%=-I%/include) \ 74 | $(INCLUDES:%=-I%) \ 75 | $(if $(USE_LTO),-flto,-fno-lto) \ 76 | 77 | LDFLAGS += \ 78 | -Wl,--gc-sections \ 79 | -Wl,-Map,$(MAPFILE) \ 80 | $(LIBDIRS:%=-L%/lib) \ 81 | $(LIBS:%=-l%) \ 82 | $(if $(USE_LTO),-flto,-fno-lto) \ 83 | 84 | OBJECTS := 85 | DEPENDS := 86 | 87 | SPACE := $(subst ,, ) 88 | 89 | eq = $(and $(findstring x$(1),x$(2)),$(findstring x$(2),x$(1))) 90 | 91 | pathmap = $(2)$(subst $(SPACE),/,$(foreach component,$(subst /, ,$(1)),$(if $(call eq,$(component),..),__,$(component))))$(3) 92 | 93 | obj = $(call pathmap,$(1),$(BUILDDIR)/obj/,.o) 94 | dep = $(call pathmap,$(1),$(BUILDDIR)/dep/,.d) 95 | 96 | define compile = 97 | $(call obj,$(1)): $(1) 98 | @echo "compile $$<" 99 | @$$(CC) -c -o $$@ $$(CFLAGS) -MMD -MP -MF $(call dep,$(1)) $$< 100 | 101 | OBJECTS += $(call obj,$(1)) 102 | DEPENDS += $(call dep,$(1)) 103 | endef 104 | 105 | $(foreach source,$(SOURCES),$(eval $(call compile,$(source)))) 106 | 107 | DIRS := $(dir $(BUILDDIR) $(OBJECTS) $(DEPENDS)) 108 | 109 | $(ELFFILE): $(OBJECTS) 110 | $(ROMFILE): $(ELFFILE) 111 | $(OBJECTS): | dirs 112 | 113 | %.elf: 114 | @echo "link $@" 115 | @$(LD) -o $@ $^ $(LDFLAGS) 116 | 117 | %.gba: 118 | @echo "objcopy $@" 119 | @$(OBJCOPY) -O binary $< $@ 120 | 121 | dirs: 122 | @mkdir -p $(DIRS) 123 | 124 | clean: 125 | @echo "clean $(BUILDDIR)" 126 | @rm -rf $(BUILDDIR) 127 | 128 | run: $(ELFFILE) 129 | @mgba-qt $(ELFFILE) 130 | 131 | .PHONY: default dirs clean run 132 | 133 | -include $(DEPENDS) 134 | -------------------------------------------------------------------------------- /examples/vsync-callback/meson.build: -------------------------------------------------------------------------------- 1 | executable('example-vsync-callback', 'src/main.c', 2 | dependencies: [dependency('libseven'), dependency('minrt')], 3 | name_suffix: 'elf') 4 | -------------------------------------------------------------------------------- /examples/vsync-callback/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void vblank_callback(u16 irqs) 4 | { 5 | BG_PALETTE[0] += 1; 6 | } 7 | 8 | int main(void) 9 | { 10 | // Set up the necessary interrupt handling 11 | irqInitDefault(); 12 | irqEnableFull(IRQ_VBLANK); 13 | irqCallbackSet(IRQ_VBLANK, vblank_callback, 0); 14 | 15 | // Clear the force-blank bit 16 | REG_DISPCNT = 0; 17 | 18 | while (1) 19 | { 20 | // Wait for V-sync 21 | svcVBlankIntrWait(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /examples/vsync/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /examples/vsync/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This Source Code Form is subject to the terms of the Mozilla Public 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this 4 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | # 6 | 7 | .SUFFIXES: 8 | 9 | # Path to gba-minrt 10 | MINRT := ../../subprojects/minrt 11 | 12 | # Path to libseven 13 | LIBSEVEN := ../.. 14 | 15 | # Name of your ROM 16 | PROJECT := example-vsync 17 | 18 | # Source files 19 | SOURCES := $(MINRT)/src/crt0.s src/main.c 20 | 21 | # Include directories 22 | INCLUDES := $(MINRT)/src 23 | 24 | # Library directories, with /include and /lib 25 | LIBDIRS := $(MINRT) $(LIBSEVEN) 26 | 27 | # Libraries to link 28 | LIBS := seven 29 | 30 | # All build output goes here 31 | BUILDDIR := build 32 | 33 | # C compiler flags 34 | CFLAGS := -g3 -gdwarf-4 -O2 -std=c99 -ffunction-sections -fdata-sections 35 | 36 | # Linker flags 37 | LDFLAGS := -mthumb -nostartfiles \ 38 | -specs=nano.specs -specs=nosys.specs \ 39 | -Wl,-Trom.ld 40 | 41 | # Toolchain prefix 42 | # 43 | # Only change this if you want to use a different compiler 44 | TOOLCHAIN := arm-none-eabi 45 | 46 | # Uncomment this if you want to use C++ 47 | # 48 | # USE_CXX := yes 49 | 50 | # Uncomment this if you want to use Link Time Optimization 51 | # 52 | # USE_LTO := yes 53 | 54 | # 55 | # Internal 56 | # 57 | 58 | CC := $(TOOLCHAIN)-gcc 59 | CXX := $(TOOLCHAIN)-g++ 60 | OBJCOPY := $(TOOLCHAIN)-objcopy 61 | LD := $(if $(USE_CXX),$(CXX),$(CC)) 62 | 63 | ELFFILE := $(BUILDDIR)/$(PROJECT).elf 64 | ROMFILE := $(BUILDDIR)/$(PROJECT).gba 65 | MAPFILE := $(BUILDDIR)/$(PROJECT).map 66 | 67 | default: $(ROMFILE) 68 | 69 | CFLAGS += \ 70 | -mcpu=arm7tdmi \ 71 | -mabi=aapcs \ 72 | -mthumb \ 73 | $(LIBDIRS:%=-I%/include) \ 74 | $(INCLUDES:%=-I%) \ 75 | $(if $(USE_LTO),-flto,-fno-lto) \ 76 | 77 | LDFLAGS += \ 78 | -Wl,--gc-sections \ 79 | -Wl,-Map,$(MAPFILE) \ 80 | $(LIBDIRS:%=-L%/lib) \ 81 | $(LIBS:%=-l%) \ 82 | $(if $(USE_LTO),-flto,-fno-lto) \ 83 | 84 | OBJECTS := 85 | DEPENDS := 86 | 87 | SPACE := $(subst ,, ) 88 | 89 | eq = $(and $(findstring x$(1),x$(2)),$(findstring x$(2),x$(1))) 90 | 91 | pathmap = $(2)$(subst $(SPACE),/,$(foreach component,$(subst /, ,$(1)),$(if $(call eq,$(component),..),__,$(component))))$(3) 92 | 93 | obj = $(call pathmap,$(1),$(BUILDDIR)/obj/,.o) 94 | dep = $(call pathmap,$(1),$(BUILDDIR)/dep/,.d) 95 | 96 | define compile = 97 | $(call obj,$(1)): $(1) 98 | @echo "compile $$<" 99 | @$$(CC) -c -o $$@ $$(CFLAGS) -MMD -MP -MF $(call dep,$(1)) $$< 100 | 101 | OBJECTS += $(call obj,$(1)) 102 | DEPENDS += $(call dep,$(1)) 103 | endef 104 | 105 | $(foreach source,$(SOURCES),$(eval $(call compile,$(source)))) 106 | 107 | DIRS := $(dir $(BUILDDIR) $(OBJECTS) $(DEPENDS)) 108 | 109 | $(ELFFILE): $(OBJECTS) 110 | $(ROMFILE): $(ELFFILE) 111 | $(OBJECTS): | dirs 112 | 113 | %.elf: 114 | @echo "link $@" 115 | @$(LD) -o $@ $^ $(LDFLAGS) 116 | 117 | %.gba: 118 | @echo "objcopy $@" 119 | @$(OBJCOPY) -O binary $< $@ 120 | 121 | dirs: 122 | @mkdir -p $(DIRS) 123 | 124 | clean: 125 | @echo "clean $(BUILDDIR)" 126 | @rm -rf $(BUILDDIR) 127 | 128 | run: $(ELFFILE) 129 | @mgba-qt $(ELFFILE) 130 | 131 | .PHONY: default dirs clean run 132 | 133 | -include $(DEPENDS) 134 | -------------------------------------------------------------------------------- /examples/vsync/meson.build: -------------------------------------------------------------------------------- 1 | executable('example-vsync', 'src/main.c', 2 | dependencies: [dependency('libseven'), dependency('minrt')], 3 | name_suffix: 'elf') 4 | -------------------------------------------------------------------------------- /examples/vsync/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | // Set up the necessary interrupt handling 6 | irqInitDefault(); 7 | irqEnableFull(IRQ_VBLANK); 8 | 9 | // Clear the force-blank bit 10 | REG_DISPCNT = 0; 11 | 12 | while (1) 13 | { 14 | // Wait for V-sync 15 | svcVBlankIntrWait(); 16 | 17 | // Update the background color for the next frame 18 | BG_PALETTE[0] += 1; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /gba.txt: -------------------------------------------------------------------------------- 1 | [binaries] 2 | c = 'arm-none-eabi-gcc' 3 | cpp = 'arm-none-eabi-g++' 4 | ar = 'arm-none-eabi-gcc-ar' 5 | strip = 'arm-none-eabi-strip' 6 | objcopy = 'arm-none-eabi-objcopy' 7 | nm = 'arm-none-eabi-nm' 8 | 9 | [built-in options] 10 | c_args = ['-mcpu=arm7tdmi', '-mfloat-abi=soft', '-mabi=aapcs'] 11 | cpp_args = c_args 12 | 13 | [host_machine] 14 | system = 'none' 15 | cpu_family = 'arm' 16 | cpu = 'arm7tdmi' 17 | endian = 'little' 18 | -------------------------------------------------------------------------------- /include/seven/asm/base.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_BASE_S 8 | .equiv _LIBSEVEN_ASM_BASE_S, 1 9 | 10 | .equiv REG_BASE, 0x04000000 11 | 12 | .macro CONST id:req, v:req 13 | .equiv \id, \v 14 | .endm 15 | 16 | .macro REG id:req addr:req 17 | CONST REG_\id, \addr 18 | CONST OFF_\id, (\addr) - REG_BASE 19 | .endm 20 | 21 | .macro BIT id:req n:req 22 | CONST \id, 1 << (\n) 23 | .endm 24 | 25 | .macro BITS id:req n:req 26 | CONST \id, (1 << (\n)) - 1 27 | .endm 28 | 29 | .macro BITFIELD id:req sid:req value:req 30 | CONST \id\()_\sid, ((\value) & ((1 << BF_\id\()_LENGTH) - 1)) << BF_\id\()_OFFSET 31 | .endm 32 | 33 | .macro EXTFN id:req 34 | .extern \id 35 | .type \id STT_FUNC 36 | .endm 37 | 38 | .endif @ !_LIBSEVEN_ASM_BASE_S 39 | 40 | @ vim: ft=armv4 et sta sw=4 sts=8 41 | -------------------------------------------------------------------------------- /include/seven/asm/hw/dma.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_HW_DMA_S 8 | .equiv _LIBSEVEN_ASM_HW_DMA_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | 12 | REG DMA0SRC, 0x040000B0 13 | REG DMA0DST, 0x040000B4 14 | REG DMA0LEN, 0x040000B8 15 | REG DMA0CNT, 0x040000BA 16 | 17 | REG DMA1SRC, 0x040000BC 18 | REG DMA1DST, 0x040000C0 19 | REG DMA1LEN, 0x040000C4 20 | REG DMA1CNT, 0x040000C6 21 | 22 | REG DMA2SRC, 0x040000C8 23 | REG DMA2DST, 0x040000CC 24 | REG DMA2LEN, 0x040000D0 25 | REG DMA2CNT, 0x040000D2 26 | 27 | REG DMA3SRC, 0x040000D4 28 | REG DMA3DST, 0x040000D8 29 | REG DMA3LEN, 0x040000DC 30 | REG DMA3CNT, 0x040000DE 31 | 32 | CONST BF_DMA_DST_OFFSET, 5 33 | CONST BF_DMA_DST_LENGTH, 2 34 | CONST BF_DMA_SRC_OFFSET, 7 35 | CONST BF_DMA_SRC_LENGTH, 2 36 | CONST BF_DMA_START_OFFSET, 12 37 | CONST BF_DMA_START_LENGTH, 2 38 | 39 | BITFIELD DMA_DST INCREMENT, 0 40 | BITFIELD DMA_DST DECREMENT, 1 41 | BITFIELD DMA_DST FIXED, 2 42 | BITFIELD DMA_DST REPEAT, 3 43 | 44 | BITFIELD DMA_SRC INCREMENT, 0 45 | BITFIELD DMA_SRC DECREMENT, 1 46 | BITFIELD DMA_SRC FIXED, 2 47 | 48 | BIT DMA_REPEAT, 9 49 | 50 | BIT DMA_32BIT, 10 51 | CONST DMA_16BIT, !DMA_32BIT 52 | 53 | BIT DMA_CARTRIDGE_DRQ, 11 54 | 55 | BITFIELD DMA_START NOW, 0 56 | BITFIELD DMA_START HBLANK, 1 57 | BITFIELD DMA_START VBLANK, 2 58 | BITFIELD DMA_START SPECIAL, 3 59 | CONST DMA_START_SOUND, DMA_START_SPECIAL 60 | CONST DMA_START_CAPTURE, DMA_START_SPECIAL 61 | 62 | BIT DMA_IRQ_ENABLE, 14 63 | BIT DMA_ENABLE, 15 64 | 65 | EXTFN dmaEnable 66 | EXTFN dmaDisable 67 | EXTFN dmaCopy16 68 | EXTFN dmaCopy32 69 | EXTFN dmaSoundFifoATransfer 70 | EXTFN dmaSoundFifoBTransfer 71 | EXTFN dmaHBlankTransfer 72 | EXTFN dmaAtomicSet 73 | 74 | .endif @ !_LIBSEVEN_ASM_HW_DMA_S 75 | 76 | @ vim: ft=armv4 et sta sw=4 sts=8 77 | -------------------------------------------------------------------------------- /include/seven/asm/hw/gpio.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_HW_GPIO_S 8 | .equiv _LIBSEVEN_ASM_HW_GPIO_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | 12 | REG GPIODAT, 0x080000C4 13 | REG GPIODIR, 0x080000C6 14 | REG GPIOCNT, 0x080000C8 15 | 16 | CONST GPIO_IN, 0 17 | CONST GPIO_OUT, 1 18 | 19 | CONST GPIO_WRITEONLY, 0 20 | CONSt GPIO_READWRITE, 1 21 | 22 | .endif @ !_LIBSEVEN_ASM_HW_GPIO_S 23 | 24 | @ vim: ft=armv4 et sta sw=4 sts=8 25 | -------------------------------------------------------------------------------- /include/seven/asm/hw/input.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_HW_INPUT_S 8 | .equiv _LIBSEVEN_ASM_HW_INPUT_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | 12 | REG KEYINPUT, 0x04000130 13 | REG KEYCNT, 0x04000132 14 | 15 | BIT KEY_A, 0 16 | BIT KEY_B, 1 17 | BIT KEY_SELECT, 2 18 | BIT KEY_START, 3 19 | BIT KEY_RIGHT, 4 20 | BIT KEY_LEFT, 5 21 | BIT KEY_UP, 6 22 | BIT KEY_DOWN, 7 23 | BIT KEY_R, 8 24 | BIT KEY_L, 9 25 | 26 | CONST KEYS_DPAD_X, KEY_LEFT | KEY_RIGHT 27 | CONST KEYS_DPAD_Y, KEY_UP | KEY_DOWN 28 | CONST KEYS_DPAD, KEYS_DPAD_X | KEYS_DPAD_Y 29 | CONST KEYS_AB, KEY_A | KEY_B 30 | CONST KEYS_LR, KEY_L | KEY_R 31 | CONST KEYS_STARTSELECT, KEY_START | KEY_SELECT 32 | CONST KEYS_BUTTONS, KEYS_AB | KEYS_LR | KEYS_STARTSELECT 33 | CONST KEYS_ALL KEYS_DPAD | KEYS_BUTTONS 34 | 35 | CONST KEY_INDEX_A, 0 36 | CONST KEY_INDEX_B, 1 37 | CONST KEY_INDEX_SELECT, 2 38 | CONST KEY_INDEX_START, 3 39 | CONST KEY_INDEX_RIGHT, 4 40 | CONST KEY_INDEX_LEFT, 5 41 | CONST KEY_INDEX_UP, 6 42 | CONST KEY_INDEX_DOWN, 7 43 | CONST KEY_INDEX_R, 8 44 | CONST KEY_INDEX_L, 9 45 | 46 | BIT KEY_IRQ_ENABLE, 14 47 | BIT KEY_IRQ_PRESS_ALL, 15 48 | CONST KEY_IRQ_PRESS_ANY, !KEY_IRQ_PRESS_ALL 49 | 50 | EXTFN inputPoll 51 | EXTFN inputState 52 | EXTFN inputKeysPressed 53 | EXTFN inputKeysReleased 54 | EXTFN inputKeysDown 55 | EXTFN inputKeysUp 56 | EXTFN inputAxisX 57 | EXTFN inputAxisY 58 | EXTFN inputAxisLR 59 | EXTFN inputAxisAB 60 | 61 | .endif @ !_LIBSEVEN_ASM_HW_INPUT_S 62 | 63 | @ vim: ft=armv4 et sta sw=4 sts=8 64 | -------------------------------------------------------------------------------- /include/seven/asm/hw/irq.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_HW_IRQ_S 8 | .equiv _LIBSEVEN_ASM_HW_IRQ_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | 12 | REG IE, 0x04000200 13 | REG IF, 0x04000202 14 | REG IME, 0x04000208 15 | REG IFBIOS, 0x03FFFFF8 16 | 17 | CONST IRQ_HANDLER, 0x03FFFFFC 18 | 19 | BIT IRQ_VBLANK, 0 20 | BIT IRQ_HBLANK, 1 21 | BIT IRQ_VCOUNT, 2 22 | BIT IRQ_TIMER_0, 3 23 | BIT IRQ_TIMER_1, 4 24 | BIT IRQ_TIMER_2, 5 25 | BIT IRQ_TIMER_3, 6 26 | BIT IRQ_SERIAL, 7 27 | BIT IRQ_DMA_0, 8 28 | BIT IRQ_DMA_1, 9 29 | BIT IRQ_DMA_2, 10 30 | BIT IRQ_DMA_3, 11 31 | BIT IRQ_KEYPAD, 12 32 | BIT IRQ_CARTRIDGE, 13 33 | 34 | CONST IRQS_BLANK, IRQ_VBLANK | IRQ_HBLANK 35 | CONST IRQS_TIMER, IRQ_TIMER_0 | IRQ_TIMER_1 | IRQ_TIMER_2 | IRQ_TIMER_3 36 | CONST IRQS_DMA, IRQ_DMA_0 | IRQ_DMA_1 | IRQ_DMA_2 | IRQ_DMA_3 37 | CONST IRQS_EXTERNAL, IRQ_SERIAL | IRQ_KEYPAD | IRQ_CARTRIDGE 38 | BITS IRQS_ALL, 14 39 | 40 | CONST IRQ_INDEX_VBLANK, 0 41 | CONST IRQ_INDEX_HBLANK, 1 42 | CONST IRQ_INDEX_VCOUNT, 2 43 | CONST IRQ_INDEX_TIMER_0, 3 44 | CONST IRQ_INDEX_TIMER_1, 4 45 | CONST IRQ_INDEX_TIMER_2, 5 46 | CONST IRQ_INDEX_TIMER_3, 6 47 | CONST IRQ_INDEX_SERIAL, 7 48 | CONST IRQ_INDEX_DMA_0, 8 49 | CONST IRQ_INDEX_DMA_1, 9 50 | CONST IRQ_INDEX_DMA_2, 10 51 | CONST IRQ_INDEX_DMA_3, 11 52 | CONST IRQ_INDEX_KEYPAD, 12 53 | CONST IRQ_INDEX_CARTRIDGE, 13 54 | 55 | EXTFN irqInit 56 | EXTFN irqInitDefault 57 | EXTFN irqInitSimple 58 | EXTFN irqInitStub 59 | EXTFN irqCallbackSet 60 | EXTFN irqCallbackDelete 61 | EXTFN irqCallbackLookup 62 | EXTFN irqEnable 63 | EXTFN irqDisable 64 | EXTFN irqEnableFull 65 | EXTFN irqDisableFull 66 | EXTFN irqCriticalSectionEnter 67 | EXTFN irqCriticalSectionExit 68 | EXTFN irqCriticalSectionActive 69 | 70 | .endif @ !_LIBSEVEN_ASM_HW_IRQ_S 71 | 72 | @ vim: ft=armv4 et sta sw=4 sts=8 73 | -------------------------------------------------------------------------------- /include/seven/asm/hw/memory.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_HW_MEMORY_S 8 | .equiv _LIBSEVEN_ASM_HW_MEMORY_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | 12 | CONST MEM_BIOS, 0 13 | CONST MEM_BIOS_SIZE, 0x4000 14 | 15 | CONST MEM_EWRAM, 0x02000000 16 | CONST MEM_EWRAM_SIZE, 0x40000 17 | 18 | CONST MEM_IWRAM, 0x03000000 19 | CONST MEM_IWRAM_SIZE, 0x8000 20 | 21 | CONST MEM_IO, 0x04000000 22 | CONST MEM_IO_SIZE, 0x020C 23 | 24 | CONST MEM_PALETTE, 0x05000000 25 | CONST MEM_PALETTE_SITE, 0x400 26 | 27 | CONST MEM_VRAM, 0x06000000 28 | CONST MEM_VRAM_SIZE, 0x18000 29 | 30 | CONST MEM_OAM, 0x07000000 31 | CONST MEM_OAM_SIZE, 0x400 32 | 33 | CONST MEM_ROM, 0x08000000 34 | CONST MEM_ROM_SIZE, 0x02000000 35 | 36 | CONST MEM_SRAM, 0x0E000000 37 | CONST MEM_SRAM_SIZe, 0x8000 38 | 39 | .endif @ !_LIBSEVEN_ASM_HW_MEMORY_S 40 | 41 | @ vim: ft=armv4 et sta sw=4 sts=8 42 | -------------------------------------------------------------------------------- /include/seven/asm/hw/svc.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_HW_SVC_S 8 | .equiv _LIBSEVEN_ASM_HW_SVC_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | 12 | CONST SVC_SOFTRESET, 0 13 | CONST SVC_REGISTERRAMRESET, 1 14 | CONST SVC_HALT, 2 15 | CONST SVC_STOP, 3 16 | CONST SVC_INTRWAIT, 4 17 | CONST SVC_VBLANKINTRWAIT, 5 18 | CONST SVC_DIV, 6 19 | CONST SVC_SQRT, 8 20 | CONST SVC_ARCTAN, 9 21 | CONST SVC_ARCTAN2, 10 22 | CONST SVC_CPUSET, 11 23 | CONST SVC_CPUFASTSET, 12 24 | CONST SVC_BIOSCHECKSUM, 13 25 | CONST SVC_BGAFFINESET, 14 26 | CONST SVC_OBJAFFINESET, 15 27 | CONST SVC_BITUNPACK, 16 28 | CONST SVC_LZ77UNCOMPWRAM, 17 29 | CONST SVC_LZ77UNCOMPVRAM, 18 30 | CONST SVC_HUFFUNCOMP, 19 31 | CONST SVC_RLUNCOMPWRAM, 20 32 | CONST SVC_RLUNCOMPVRAM, 21 33 | CONST SVC_DIFF8BITUNFILTERWRAM, 22 34 | CONST SVC_DIFF8BITUNFILTERVRAM, 23 35 | CONST SVC_DIFF16BITUNFILTER, 24 36 | CONST SVC_SOUNDBIAS, 25 37 | CONST SVC_SOUNDDRIVERINIT, 26 38 | CONST SVC_SOUNDDRIVERMODE, 27 39 | CONST SVC_SOUNDDRIVERMAIN, 28 40 | CONST SVC_SOUNDDRIVERVSYNC, 29 41 | CONST SVC_SOUNDCHANNELCLEAR, 30 42 | CONST SVC_MIDIKEY2FREQ, 31 43 | CONST SVC_MUSICPLAYEROPEN, 32 44 | CONST SVC_MUSICPLAYERSTART, 33 45 | CONST SVC_MUSICPLAYERSTOP, 34 46 | CONST SVC_MUSICPLAYERCONTINUE, 35 47 | CONST SVC_MUSICPLAYERFADEOUT, 36 48 | CONST SVC_MULTIBOOT, 37 49 | CONST SVC_HARDRESET, 38 50 | CONST SVC_SOUNDDRIVERVSYNCOFF, 40 51 | CONST SVC_SOUNDDRIVERVSYNCON, 41 52 | 53 | EXTFN svcSoftReset 54 | EXTFN svcRegisterRamReset 55 | EXTFN svcHalt 56 | EXTFN svcStop 57 | EXTFN svcIntrWait 58 | EXTFN svcVBlankIntrWait 59 | EXTFN svcSqrt 60 | EXTFN svcArcTan 61 | EXTFN svcArcTan2 62 | EXTFN svcCpuSet 63 | EXTFN svcCpuFastSet 64 | EXTFN svcBiosChecksum 65 | EXTFN svcBgAffineSet 66 | EXTFN svcObjAffineSet 67 | EXTFN svcBitUnPack 68 | EXTFN svcLZ77UnCompWram 69 | EXTFN svcLZ77UnCompVram 70 | EXTFN svcHuffUnComp 71 | EXTFN svcRLUnCompWram 72 | EXTFN svcRLUnCompVram 73 | EXTFN svcDiff8bitUnFilterWram 74 | EXTFN svcDiff8bitUnFilterVram 75 | EXTFN svcDiff16bitUnFilter 76 | EXTFN svcSoundBiasChange 77 | EXTFN svcSoundDriverInit 78 | EXTFN svcSoundDriverMode 79 | EXTFN svcSoundDriverMain 80 | EXTFN svcSoundDriverVSync 81 | EXTFN svcSoundChannelClear 82 | EXTFN svcMidiKey2Freq 83 | EXTFN svcMusicPlayerOpen 84 | EXTFN svcMusicPlayerStart 85 | EXTFN svcMusicPlayerStop 86 | EXTFN svcMusicPlayerContinue 87 | EXTFN svcMusicPlayerFadeOut 88 | EXTFN svcMultiBoot 89 | EXTFN svcHardReset 90 | EXTFN svcSoundDriverVSyncOff 91 | EXTFN svcSoundDriverVSyncOn 92 | 93 | EXTFN svcSoftResetEx 94 | EXTFN svcIntrWaitEx 95 | EXTFN svcCpuSetFixed 96 | EXTFN svcCpuFastSetFixed 97 | EXTFN svcIsSystemDS 98 | 99 | .endif @ !_LIBSEVEN_ASM_HW_SVC_S 100 | 101 | @ vim: ft=armv4 et sta sw=4 sts=8 102 | -------------------------------------------------------------------------------- /include/seven/asm/hw/timer.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_HW_TIMER_S 8 | .equiv _LIBSEVEN_ASM_HW_TIMER_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | 12 | REG TM0VAL, 0x04000100 13 | REG TM0CNT, 0x04000102 14 | REG TM1VAL, 0x04000104 15 | REG TM1CNT, 0x04000106 16 | REG TM2VAL, 0x04000108 17 | REG TM2CNT, 0x0400010A 18 | REG TM3VAL, 0x0400010C 19 | REG TM3CNT, 0x0400010E 20 | 21 | CONST BF_TIMER_FREQ_OFFSET, 0 22 | CONST BF_TIMER_FREQ_LENGTH, 3 23 | 24 | BITFIELD TIMER_FREQ 16MHZ, 0 25 | BITFIELD TIMER_FREQ 262KHZ, 1 26 | BITFIELD TIMER_FREQ 64KHZ, 2 27 | BITFIELD TIMER_FREQ 16KHZ, 3 28 | BITFIELD TIMER_FREQ CASCADE, 4 29 | BIT TIMER_IRQ_ENABLE, 6 30 | BIT TIMER_ENABLE, 7 31 | 32 | EXTFN timerSet 33 | EXTFN timerEnable 34 | EXTFN timerDisable 35 | EXTFN timerGetValue 36 | 37 | .endif @ !_LIBSEVEN_ASM_HW_TIMER_S 38 | 39 | @ vim: ft=armv4 et sta sw=4 sts=8 40 | -------------------------------------------------------------------------------- /include/seven/asm/hw/video.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_HW_VIDEO_S 8 | .equiv _LIBSEVEN_ASM_HW_VIDEO_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | 12 | REG DISPCNT, 0x04000000 13 | REG GREENSWP, 0x04000002 14 | REG DISPSTAT, 0x04000004 15 | REG VCOUNT, 0x04000006 16 | 17 | REG BG0CNT, 0x04000008 18 | REG BG1CNT, 0x0400000A 19 | REG BG2CNT, 0x0400000C 20 | REG BG3CNT, 0x0400000E 21 | 22 | REG BG0HOFS, 0x04000010 23 | REG BG0VOFS, 0x04000012 24 | REG BG1HOFS, 0x04000014 25 | REG BG1VOFS, 0x04000016 26 | REG BG2HOFS, 0x04000018 27 | REG BG2VOFS, 0x0400001A 28 | REG BG3HOFS, 0x0400001C 29 | REG BG3VOFS, 0x0400001E 30 | 31 | @ Affine 32 | 33 | REG BG2PA, 0x04000020 34 | REG BG2PB, 0x04000022 35 | REG BG2PC, 0x04000024 36 | REG BG2PD, 0x04000026 37 | REG BG2X, 0x04000028 38 | REG BG2Y, 0x0400002C 39 | 40 | REG BG3PA, 0x04000030 41 | REG BG3PB, 0x04000032 42 | REG BG3PC, 0x04000034 43 | REG BG3PD, 0x04000036 44 | REG BG3X, 0x04000038 45 | REG BG3Y, 0x0400003C 46 | 47 | @ Window 48 | 49 | REG WIN0H, 0x04000040 50 | REG WIN1H, 0x04000042 51 | REG WIN0V, 0x04000044 52 | REG WIN1V, 0x04000046 53 | 54 | REG WIN0IN, 0x04000048 55 | REG WIN1IN, 0x04000049 56 | REG WIN0OUT, 0x0400004A 57 | REG WIN1OUT, 0x0400004B 58 | 59 | @ Mosaic 60 | 61 | REG MOSAIC, 0x0400004C 62 | 63 | @ Blending 64 | 65 | REG BLDCNT, 0x04000050 66 | REG BLDALPHA, 0x04000052 67 | REG BLDCOEFF, 0x04000054 68 | 69 | CONST BF_LCD_MODE_OFFSET, 0 70 | CONST BF_LCD_MODE_LENGTH, 3 71 | 72 | BITFIELD LCD_MODE 0, 0 73 | BITFIELD LCD_MODE 1, 1 74 | BITFIELD LCD_MODE 2, 2 75 | BITFIELD LCD_MODE 3, 3 76 | BITFIELD LCD_MODE 4, 4 77 | BITFIELD LCD_MODE 5, 5 78 | 79 | CONST LCD_MODE_TILED, LCD_MODE_0 80 | CONST LCD_MODE_MIXED, LCD_MODE_1 81 | CONST LCD_MODE_AFFINE, LCD_MODE_2 82 | CONST LCD_MODE_BITMAP, LCD_MODE_3 83 | CONST LCD_MODE_BITMAP_PALETTED, LCD_MODE_4 84 | CONST LCD_MODE_BITMAP_SMALL, LCD_MODE_5 85 | 86 | BIT LCD_FRAME_SELECT, 4 87 | BIT LCD_HBLANK_OAM_ACCESS, 5 88 | BIT LCD_OBJ_MAPPING_1D, 6 89 | CONST LCD_OBJ_MAPPING_2D, !LCD_OBJ_MAPPING_1D 90 | 91 | BIT LCD_FORCE_BLANK, 7 92 | BIT LCD_BG0_ENABLE, 8 93 | BIT LCD_BG1_ENABLE, 9 94 | BIT LCD_BG2_ENABLE, 10 95 | BIT LCD_BG3_ENABLE, 11 96 | BIT LCD_OBJ_ENABLE, 12 97 | BIT LCD_WIN0_ENABLE, 13 98 | BIT LCD_WIN1_ENABLE, 14 99 | BIT LCD_OBJ_WINDOW_ENABLE, 15 100 | 101 | BIT LCD_IN_VBLANK, 0 102 | BIT LCD_IN_HBLANK, 1 103 | BIT LCD_VCOUNT_MATCH, 2 104 | BIT LCD_VBLANK_IRQ_ENABLE, 3 105 | BIT LCD_HBLANK_IRQ_ENABLE, 4 106 | BIT LCD_VCOUNT_IRQ_EMABLE, 5 107 | 108 | CONST VCOUNT_DISPLAY_START, 0 109 | CONST VCOUNT_VBLANK_START, 160 110 | 111 | CONST BF_BG_PRIO_OFFSET, 0 112 | CONST BF_BG_PRIO_LENGTH, 2 113 | 114 | CONST BF_BG_SIZE_OFFSET, 14 115 | CONST BF_BG_SIZE_LENGTH, 2 116 | CONST BF_BG_AFF_SIZE_OFFSET, 14 117 | CONST BF_BG_AFF_SIZE_LENGTH, 2 118 | 119 | BITFIELD BG_PRIO 0, 0 120 | BITFIELD BG_PRIO 1, 1 121 | BITFIELD BG_PRIO 2, 2 122 | BITFIELD BG_PRIO 3, 3 123 | 124 | CONST BF_BG_GFX_BASE_OFFSET, 2 125 | CONST BF_BG_GFX_BASE_LENGTH, 2 126 | 127 | CONST BF_BG_MAP_BASE_OFFSET, 8 128 | CONST BF_BG_MAP_BASE_LENGTH, 5 129 | 130 | CONST BG_PRIO_MIN, BG_PRIO_3 131 | CONST BG_PRIO_MAX, BG_PRIO_0 132 | 133 | BIT BG_MOSAIC_ENABLE, 6 134 | BIT BG_256_COLOR, 7 135 | CONST BG_USE_PALETTES, !BG_256_COLOR 136 | BIT BG_AFF_WRAP, 13 137 | 138 | BITFIELD BG_SIZE 256x256, 0 139 | BITFIELD BG_SIZE 512x256, 1 140 | BITFIELD BG_SIZE 256x512, 2 141 | BITFIELD BG_SIZE 512x512, 3 142 | 143 | CONST BG_AFF_SIZE_128x128, BG_SIZE_256x256 144 | CONST BG_AFF_SIZE_256x256, BG_SIZE_512x256 145 | CONST BG_AFF_SIZE_512x512, BG_SIZE_256x512 146 | CONST BG_AFF_SIZE_1024x1024, BG_SIZE_512x512 147 | 148 | BIT WIN_BG0_ENABLE, 0 149 | BIT WIN_BG1_ENABLE, 1 150 | BIT WIN_BG2_ENABLE, 2 151 | BIT WIN_BG3_ENABLE, 3 152 | BIT WIN_OBJ_ENABLE, 4 153 | BIT WIN_BLEND_ENABLE, 5 154 | 155 | CONST BF_MOSAIC_BG_H_OFFSET, 0 156 | CONST BF_MOSAIC_BG_H_LENGTH, 4 157 | 158 | CONST BF_MOSAIC_BG_V_OFFSET, 4 159 | CONST BF_MOSAIC_BG_V_LENGTH, 4 160 | 161 | CONST BF_MOSAIC_OBJ_H_OFFSET, 8 162 | CONST BF_MOSAIC_OBJ_H_LENGTH, 4 163 | 164 | CONST BF_MOSAIC_OBJ_V_OFFSET, 12 165 | CONST BF_MOSAIC_OBJ_V_LENGTH, 4 166 | 167 | CONST BF_BLD_MODE_OFFSET, 6 168 | CONST BF_BLD_MODE_LENGTH, 2 169 | 170 | BIT BLD_TARGET_BG0, 0 171 | BIT BLD_TARGET_BG1, 1 172 | BIT BLD_TARGET_BG2, 2 173 | BIT BLD_TARGET_BG3, 3 174 | BIT BLD_TARGET_OBJ, 4 175 | BIT BLD_TARGET_BD, 5 176 | 177 | BITFIELD BLD_MODE NONE, 0 178 | BITFIELD BLD_MODE ALPHA, 1 179 | BITFIELD BLD_MODE WHITE, 2 180 | BITFIELD BLD_MODE BLACK, 3 181 | 182 | BIT BLD_TARGET2_BG0, 8 183 | BIT BLD_TARGET2_BG1, 9 184 | BIT BLD_TARGET2_BG2, 10 185 | BIT BLD_TARGET2_BG3, 11 186 | BIT BLD_TARGET2_OBJ, 12 187 | BIT BLD_TARGET2_BD, 13 188 | 189 | @ Color 190 | 191 | CONST BF_COLOR_R_OFFSET, 0 192 | CONST BF_COLOR_R_LENGTH, 5 193 | CONST BF_COLOR_G_OFFSET, 5 194 | CONST BF_COLOR_G_LENGTH, 5 195 | CONST BF_COLOR_B_OFFSET, 10 196 | CONST BF_COLRO_B_LENGTH, 5 197 | 198 | .macro RGB5 id:req r:req g:req b:req 199 | CONST \id, ((\r) & 31) | (((\g) & 31) << 5) | (((\b) & 31) << 10) 200 | .endm 201 | 202 | .macro RGB8 id:req r:req g:req b:req 203 | CONST \id, (((\r) >> 3) & 31) | ((((\g) >> 3) & 31) << 5) | ((((\b) >> 3) & 31) << 10) 204 | .endm 205 | 206 | CONST BG_PALETTE, MEM_PALETTE 207 | CONST OBJ_PALETTE, MEM_PALETTE+512 208 | 209 | .endif @ !_LIBSEVEN_ASM_HW_VIDEO_S 210 | 211 | @ vim: ft=armv4 et sta sw=4 sts=8 212 | -------------------------------------------------------------------------------- /include/seven/asm/hw/waitstate.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_HW_WAITSTATES_S 8 | .equiv _LIBSEVEN_ASM_HW_WAITSTATES_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | 12 | REG WAITCNT, 0x04000204 13 | 14 | CONST BF_WAIT_SRAM_OFFSET, 0 15 | CONST BF_WAIT_SRAM_LENGTH, 2 16 | 17 | CONST BF_WAIT_ROM_N_OFFSET, 2 18 | CONST BF_WAIT_ROM_N_LENGTH, 2 19 | 20 | CONST BF_WAIT_ROM_S_OFFSET, 4 21 | CONST BF_WAIT_ROM_S_LENGTH, 1 22 | 23 | BITFIELD WAIT_SRAM 4, 0 24 | BITFIELD WAIT_SRAM 3, 1 25 | BITFIELD WAIT_SRAM 2, 2 26 | BITFIELD WAIT_SRAM 8, 3 27 | 28 | BITFIELD WAIT_ROM_N 4, 0 29 | BITFIELD WAIT_ROM_N 3, 1 30 | BITFIELD WAIT_ROM_N 2, 2 31 | BITFIELD WAIT_ROM_N 8, 3 32 | 33 | BITFIELD WAIT_ROM_S 2, 0 34 | BITFIELD WAIT_ROM_S 1, 1 35 | 36 | BIT WAIT_PREFETCH_ENABLE, 14 37 | 38 | .endif @ !_LIBSEVEN_ASM_HW_WAITSTATES_S 39 | 40 | @ vim: ft=armv4 et sta sw=4 sts=8 41 | -------------------------------------------------------------------------------- /include/seven/asm/prelude.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_PRELUDE_S 8 | .equiv _LIBSEVEN_ASM_PRELUDE_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | .include "seven/asm/hw/input.s" 12 | .include "seven/asm/hw/irq.s" 13 | .include "seven/asm/hw/memory.s" 14 | .include "seven/asm/hw/svc.s" 15 | 16 | .endif @ !_LIBSEVEN_ASM_PRELUDE_S 17 | 18 | @ vim: ft=armv4 et sta sw=4 sts=8 19 | -------------------------------------------------------------------------------- /include/seven/base.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_BASE_H 8 | #define _LIBSEVEN_BASE_H 9 | 10 | #ifdef __cplusplus 11 | #define _LIBSEVEN_EXTERN_C extern "C" { 12 | #define _LIBSEVEN_EXTERN_C_END } 13 | #else 14 | #define _LIBSEVEN_EXTERN_C 15 | #define _LIBSEVEN_EXTERN_C_END 16 | #endif 17 | 18 | #define _LIBSEVEN_STR(s) #s 19 | #define _LIBSEVEN_STR2(s) _LIBSEVEN_STR(s) 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #endif /* !_LIBSEVEN_BASE_H */ 28 | -------------------------------------------------------------------------------- /include/seven/base/addresses.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_BASE_REGISTERS_H 8 | #define _LIBSEVEN_BASE_REGISTERS_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #define VOLADDR(addr, type) (*(type volatile *)(addr)) 15 | #define VOLARRAY(addr, type, size) (*(type volatile (*)[size])(addr)) 16 | 17 | #define VOLSERIES(addr, type, stride, index) \ 18 | (*(type volatile *)((void*)(addr) + (stride) * (index))) 19 | 20 | #define MEMADDR(addr, type) (*(type *)(addr)) 21 | #define MEMARRAY(addr, type, size) (*(type (*)[size])(addr)) 22 | 23 | #define MEMSERIES(addr, type, stride, index) \ 24 | (*(type *)((void*)(addr) + (stride) * (index))) 25 | 26 | _LIBSEVEN_EXTERN_C_END 27 | 28 | #endif /* !_LIBSEVEN_BASE_REGISTERS_H */ 29 | -------------------------------------------------------------------------------- /include/seven/base/attributes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_BASE_ATTRIBUTES_H 8 | #define _LIBSEVEN_BASE_ATTRIBUTES_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #define _LIBSEVEN_SECCOUNT _LIBSEVEN_STR2(__COUNTER__) 15 | 16 | #define PACKED __attribute__((packed)) 17 | #define ALIGN(n) __attribute__((aligned(n))) 18 | #define NORETURN __attribute__((__noreturn__)) 19 | #define NOINLINE __attribute__((noinline)) 20 | #define ARM_CODE __attribute__((target("arm"))) 21 | #define THUMB_CODE __attribute__((target("thumb"))) 22 | 23 | #define SECTION(name) __attribute__((section(name))) 24 | 25 | #define IWRAM_SECTION(suffix) SECTION(".iwram" suffix) 26 | #define IWRAM_OVERLAY(number) SECTION(".iwram" #number) 27 | 28 | #define IWRAM_CODE IWRAM_SECTION(".text." _LIBSEVEN_SECCOUNT) NOINLINE 29 | #define IWRAM_DATA IWRAM_SECTION(".data." _LIBSEVEN_SECCOUNT) 30 | #define IWRAM_BSS SECTION(".bss." _LIBSEVEN_SECCOUNT) 31 | 32 | #define EWRAM_SECTION(suffix) SECTION(".ewram" suffix) 33 | #define EWRAM_OVERLAY(number) SECTION(".ewram" #number) 34 | 35 | #define EWRAM_CODE EWRAM_SECTION(".text." _LIBSEVEN_SECCOUNT) NOINLINE 36 | #define EWRAM_DATA EWRAM_SECTION(".data." _LIBSEVEN_SECCOUNT) 37 | #define EWRAM_BSS SECTION(".sbss." _LIBSEVEN_SECCOUNT) 38 | 39 | _LIBSEVEN_EXTERN_C_END 40 | 41 | #endif /* !_LIBSEVEN_BASE_ATTRIBUTES_H */ 42 | -------------------------------------------------------------------------------- /include/seven/base/bits.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_BASE_BITS_H 8 | #define _LIBSEVEN_BASE_BITS_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #define BIT(n) (1 << (n)) 15 | #define BITS(n) (BIT(n) - 1) 16 | 17 | #define ROR(x, a) ((u32)(x) >> (a) | (u32)(x) << (32-(a))) 18 | #define ROL(x, a) ((u32)(x) << (a) | (u32)(x) >> (32-(a))) 19 | 20 | #define BIT_TRISTATE(val, m, p) \ 21 | (((val)>>(p)&1)-((val)>>(m)&1)) 22 | 23 | #define BITFIELD(name, value) \ 24 | (((value) & BITS((BF_##name##_LENGTH))) << (BF_##name##_OFFSET)) 25 | 26 | #define BF_AND(lhs, name, rhs) ((lhs) & BITFIELD(name, (rhs))) 27 | #define BF_ORR(lhs, name, rhs) ((lhs) | BITFIELD(name, (rhs))) 28 | #define BF_EOR(lhs, name, rhs) ((lhs) ^ BITFIELD(name, (rhs))) 29 | #define BF_BIC(lhs, name, rhs) ((lhs) & ~BITFIELD(name, (rhs))) 30 | 31 | #define BF_NOT(lhs, name) ((lhs) ^ BF_MASK(name)) 32 | #define BF_NEG(lhs, name) (BF_SET((lhs), name, -BF_GET((lhs), name))) 33 | 34 | #define BF_ADD(lhs, name, rhs) (BF_SET((lhs), name, BF_GET((lhs), name) + (rhs))) 35 | #define BF_SUB(lhs, name, rhs) (BF_SET((lhs), name, BF_GET((lhs), name) - (rhs))) 36 | #define BF_MUL(lhs, name, rhs) (BF_SET((lhs), name, BF_GET((lhs), name) * (rhs))) 37 | #define BF_DIV(lhs, name, rhs) (BF_SET((lhs), name, BF_GET((lhs), name) / (rhs))) 38 | #define BF_MOD(lhs, name, rhs) (BF_SET((lhs), name, BF_GET((lhs), name) % (rhs))) 39 | 40 | #define BF_LSL(lhs, name, rhs) (BF_SET((lhs), name, BF_GET((lhs), name) << (rhs))) 41 | #define BF_LSR(lhs, name, rhs) (BF_SET((lhs), name, BF_GET((lhs), name) >> (rhs))) 42 | #define BF_ROL(lhs, name, rhs) (BF_SET((lhs), name, BF_GET((lhs), name) << (rhs) | BF_GET((lhs), name) >> ((BF_##name##_LENGTH) - (rhs)))) 43 | #define BF_ROR(lhs, name, rhs) (BF_SET((lhs), name, BF_GET((lhs), name) >> (rhs) | BF_GET((lhs), name) << ((BF_##name##_LENGTH) - (rhs)))) 44 | 45 | #define BF_MASK(name) \ 46 | (BITS((BF_##name##_LENGTH)) << (BF_##name##_OFFSET)) 47 | 48 | #define BF_GET(lhs, name) \ 49 | ((lhs) >> ((BF_##name##_OFFSET)) & BITS((BF_##name##_LENGTH))) 50 | 51 | #define BF_SET(lhs, name, rhs) \ 52 | (BF_ORR((lhs) & ~BF_MASK(name), name, (rhs))) 53 | 54 | #define BF_ESET(lhs, name, enum) \ 55 | (((lhs) & ~BF_MASK(name)) | (name##_##enum)) 56 | 57 | #define BF_CMP(lhs, name, rhs) \ 58 | (BF_GET((lhs), name) == (rhs)) 59 | 60 | #define BF_ECMP(lhs, name, enum) \ 61 | (((lhs) & BF_MASK(name)) == name##_##enum) 62 | 63 | _LIBSEVEN_EXTERN_C_END 64 | 65 | #endif /* !_LIBSEVEN_BASE_BITS_H */ 66 | -------------------------------------------------------------------------------- /include/seven/base/types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_BASE_TYPES_H 8 | #define _LIBSEVEN_BASE_TYPES_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | _LIBSEVEN_EXTERN_C 17 | 18 | typedef int8_t i8; 19 | typedef int16_t i16; 20 | typedef int32_t i32; 21 | typedef int64_t i64; 22 | typedef ptrdiff_t isize; 23 | typedef intptr_t iptr; 24 | 25 | typedef int8_t s8; 26 | typedef int16_t s16; 27 | typedef int32_t s32; 28 | typedef int64_t s64; 29 | typedef ptrdiff_t ssize; 30 | typedef intptr_t sptr; 31 | 32 | typedef uint8_t u8; 33 | typedef uint16_t u16; 34 | typedef uint32_t u32; 35 | typedef uint64_t u64; 36 | typedef size_t usize; 37 | typedef uintptr_t uptr; 38 | 39 | #define I8_MIN INT8_MIN 40 | #define I16_MIN INT16_MIN 41 | #define I32_MIN INT32_MIN 42 | #define I64_MIN INT64_MIN 43 | #define ISIZE_MIN PTRDIFF_MIN 44 | #define IPTR_MIN INTPTR_MIN 45 | 46 | #define I8_MAX INT8_MAX 47 | #define I16_MAX INT16_MAX 48 | #define I32_MAX INT32_MAX 49 | #define I64_MAX INT64_MAX 50 | #define ISIZE_MAX PTRDIFF_MAX 51 | #define IPTR_MAX INTPTR_MAX 52 | 53 | #define I8_BITS 8 54 | #define I16_BITS 16 55 | #define I32_BITS 32 56 | #define I64_BITS 64 57 | #define ISIZE_BITS (CHAR_BIT * sizeof(isize)) 58 | #define IPTR_BITS (CHAR_BIT * sizeof(iptr)) 59 | 60 | #define S8_MIN INT8_MIN 61 | #define S16_MIN INT16_MIN 62 | #define S32_MIN INT32_MIN 63 | #define S64_MIN INT64_MIN 64 | #define SSIZE_MIN PTRDIFF_MIN 65 | #define SPTR_MIN INTPTR_MIN 66 | 67 | #define S8_MAX INT8_MAX 68 | #define S16_MAX INT16_MAX 69 | #define S32_MAX INT32_MAX 70 | #define S64_MAX INT64_MAX 71 | #define SSIZE_MAX PTRDIFF_MAX 72 | #define SPTR_MAX INTPTR_MAX 73 | 74 | #define S8_BITS 8 75 | #define S16_BITS 16 76 | #define S32_BITS 32 77 | #define S64_BITS 64 78 | #define SSIZE_BITS (CHAR_BIT * sizeof(ssize)) 79 | #define SPTR_BITS (CHAR_BIT * sizeof(sptr)) 80 | 81 | #define U8_MIN 0 82 | #define U16_MIN 0 83 | #define U32_MIN 0 84 | #define U64_MIN 0 85 | #define USIZE_MIN 0 86 | #define UPTR_MIN 0 87 | 88 | #define U8_MAX UINT8_MAX 89 | #define U16_MAX UINT16_MAX 90 | #define U32_MAX UINT32_MAX 91 | #define U64_MAX UINT64_MAX 92 | #define USIZE_MAX SIZE_MAX 93 | #define UPTR_MAX UINTPTR_MAX 94 | 95 | #define U8_BITS 8 96 | #define U16_BITS 16 97 | #define U32_BITS 32 98 | #define U64_BITS 64 99 | #define USIZE_BITS (CHAR_BIT * sizeof(usize)) 100 | #define UPTR_BITS (CHAR_BIT * sizeof(uptr)) 101 | 102 | _LIBSEVEN_EXTERN_C_END 103 | 104 | #endif /* !_LIBSEVEN_BASE_TYPES_H */ 105 | -------------------------------------------------------------------------------- /include/seven/base/version.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_BASE_VERSION_H 8 | #define _LIBSEVEN_BASE_VERSION_H 9 | 10 | #include 11 | 12 | #define LIBSEVEN_VERSION_MAJOR 0 13 | #define LIBSEVEN_VERSION_MINOR 6 14 | #define LIBSEVEN_VERSION_PATCH 0 15 | 16 | #define LIBSEVEN_VERSION \ 17 | _LIBSEVEN_STR2(LIBSEVEN_VERSION_MAJOR) "." \ 18 | _LIBSEVEN_STR2(LIBSEVEN_VERSION_MINOR) "." \ 19 | _LIBSEVEN_STR2(LIBSEVEN_VERSION_PATCH) 20 | 21 | #endif /* !_LIBSEVEN_BASE_VERSION_H */ 22 | 23 | -------------------------------------------------------------------------------- /include/seven/hw/cpu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_CPU_H 8 | #define _LIBSEVEN_HW_CPU_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #define BF_PSR_MODE_OFFSET 0 15 | #define BF_PSR_MODE_LENGTH 5 16 | 17 | #define PSR_MODE(n) BITFIELD(PSR_MODE, (n)) 18 | 19 | #define BF_PSR_CONTROL_BITS_OFFSET 0 20 | #define BF_PSR_CONTROL_BITS_LENGTH 8 21 | 22 | #define PSR_CONTROL_BITS(n) BITFIELD(PSR_CONTROL_BITS, (n)) 23 | 24 | #define BF_PSR_FLAGS_OFFSET 28 25 | #define BF_PSR_FLAGS_LENGHT 4 26 | 27 | #define PSR_FLAGS(n) BITFIELD(PSR_FLAGS, (n)) 28 | 29 | enum ProgramStatusRegister 30 | { 31 | PSR_MODE_USR = PSR_MODE(0x10), 32 | PSR_MODE_FIQ = PSR_MODE(0x11), 33 | PSR_MODE_IRQ = PSR_MODE(0x12), 34 | PSR_MODE_SVC = PSR_MODE(0x13), 35 | PSR_MODE_ABT = PSR_MODE(0x17), 36 | PSR_MODE_UND = PSR_MODE(0x1B), 37 | PSR_MODE_SYS = PSR_MODE(0x1F), 38 | 39 | PSR_STATE_THUMB = BIT(5), 40 | 41 | PSR_FIQ_DISABLE = BIT(6), 42 | PSR_IRQ_DISABLE = BIT(7), 43 | 44 | PSR_FLAG_V = BIT(28), 45 | PSR_FLAG_C = BIT(29), 46 | PSR_FLAG_Z = BIT(30), 47 | PSR_FLAG_N = BIT(31), 48 | }; 49 | 50 | _LIBSEVEN_EXTERN_C_END 51 | 52 | #endif /* !_LIBSEVEN_HW_CPU_H */ 53 | -------------------------------------------------------------------------------- /include/seven/hw/dma.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_DMA_H 8 | #define _LIBSEVEN_HW_DMA_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | // RAM ROM Transfer limit 15 | // DMA 0 R/W - 16K 16 | // DMA 1 R/W R 16K 17 | // DMA 2 R/W R 16K 18 | // DMA 3 R/W R/W 64K 19 | 20 | #define REG_DMA0SRC VOLADDR(0x040000B0, const void *) 21 | #define REG_DMA0DST VOLADDR(0x040000B4, void *) 22 | #define REG_DMA0LEN VOLADDR(0x040000B8, u16) 23 | #define REG_DMA0CNT VOLADDR(0x040000BA, u16) 24 | 25 | #define REG_DMA1SRC VOLADDR(0x040000BC, const void *) 26 | #define REG_DMA1DST VOLADDR(0x040000C0, void *) 27 | #define REG_DMA1LEN VOLADDR(0x040000C4, u16) 28 | #define REG_DMA1CNT VOLADDR(0x040000C6, u16) 29 | 30 | #define REG_DMA2SRC VOLADDR(0x040000C8, const void *) 31 | #define REG_DMA2DST VOLADDR(0x040000CC, void *) 32 | #define REG_DMA2LEN VOLADDR(0x040000D0, u16) 33 | #define REG_DMA2CNT VOLADDR(0x040000D2, u16) 34 | 35 | #define REG_DMA3SRC VOLADDR(0x040000D4, const void *) 36 | #define REG_DMA3DST VOLADDR(0x040000D8, void *) 37 | #define REG_DMA3LEN VOLADDR(0x040000DC, u16) 38 | #define REG_DMA3CNT VOLADDR(0x040000DE, u16) 39 | 40 | #define BF_DMA_DST_OFFSET 5 41 | #define BF_DMA_DST_LENGTH 2 42 | 43 | #define DMA_DST(n) BITFIELD(DMA_DST, (n)) 44 | 45 | #define BF_DMA_SRC_OFFSET 7 46 | #define BF_DMA_SRC_LENGTH 2 47 | 48 | #define DMA_SRC(n) BITFIELD(DMA_SRC, (n)) 49 | 50 | #define BF_DMA_START_OFFSET 12 51 | #define BF_DMA_START_LENGTH 2 52 | 53 | #define DMA_START(n) BITFIELD(DMA_START, (n)) 54 | 55 | enum DMAControl 56 | { 57 | DMA_DST_INCREMENT = DMA_DST(0), 58 | DMA_DST_DECREMENT = DMA_DST(1), 59 | DMA_DST_FIXED = DMA_DST(2), 60 | DNA_DST_RELOAD = DMA_DST(3), 61 | 62 | DMA_SRC_INCREMENT = DMA_SRC(0), 63 | DMA_SRC_DECREMENT = DMA_SRC(1), 64 | DMA_SRC_FIXED = DMA_SRC(2), 65 | 66 | DMA_REPEAT = BIT(9), 67 | 68 | DMA_32BIT = BIT(10), 69 | DMA_16BIT = !DMA_32BIT, 70 | 71 | DMA_START_NOW = DMA_START(0), 72 | DMA_START_HBLANK = DMA_START(1), 73 | DMA_START_VBLANK = DMA_START(2), 74 | DMA_START_SPECIAL = DMA_START(3), 75 | 76 | // DMA1, DMA2 77 | DMA_START_SOUND = DMA_START_SPECIAL, 78 | // DMA3 79 | DMA_START_CAPTURE = DMA_START_SPECIAL, 80 | 81 | // On transfer completion 82 | DMA_IRQ_ENABLE = BIT(14), 83 | DMA_ENABLE = BIT(15), 84 | }; 85 | 86 | extern void dmaEnable(u32 num); 87 | extern void dmaDisable(u32 num); 88 | 89 | // General purpose memory copy using DMA3 90 | extern void dmaCopy16(const void *src, void *dst, u32 len); 91 | extern void dmaCopy32(const void *src, void *dst, u32 len); 92 | 93 | // Repeating Sound FIFO A transfer using DMA1 94 | extern void dmaSoundFifoATransfer(const void *src, u16 flags); 95 | 96 | // Repeating Sound FIFO B transfer using DMA2 97 | extern void dmaSoundFifoBTransfer(const void *src, u16 flags); 98 | 99 | // Repeating H-Blank transfer using DMA0. 100 | extern void dmaHBlankTransfer(const void *src, void *dst, u32 len, u16 flags); 101 | 102 | // Atomically sets up a DMA channel, using a byte count 103 | extern void dmaAtomicSet(u32 num, const void *src, void *dst, u32 len, u16 flags); 104 | 105 | _LIBSEVEN_EXTERN_C_END 106 | 107 | #endif /* !_LIBSEVEN_HW_DMA_H */ 108 | -------------------------------------------------------------------------------- /include/seven/hw/gpio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_GPIO_H 8 | #define _LIBSEVEN_HW_GPIO_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | // GPIO Data 15 | #define REG_GPIODAT VOLADDR(0x080000C4, u16) 16 | // GPIO Direction 17 | #define REG_GPIODIR VOLADDR(0x080000C6, u16) 18 | // GPIO Control 19 | #define REG_GPIOCNT VOLADDR(0x080000C8, u16) 20 | 21 | enum GpioDirection 22 | { 23 | GPIO_IN = 0, 24 | GPIO_OUT = 1, 25 | }; 26 | 27 | enum GpioControl 28 | { 29 | GPIO_WRITEONLY = 0, 30 | GPIO_READWRITE = 1, 31 | }; 32 | 33 | _LIBSEVEN_EXTERN_C_END 34 | 35 | #endif /* !_LIBSEVEN_HW_GPIO_H */ 36 | -------------------------------------------------------------------------------- /include/seven/hw/input.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_INPUT_H 8 | #define _LIBSEVEN_HW_INPUT_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | // Keypad input register. Reports held keys as active-low bits. 15 | // 16 | #define REG_KEYINPUT VOLADDR(0x04000130, const u16) 17 | 18 | // Keypad control register. Allows configuring the keypad IRQ. 19 | // 20 | #define REG_KEYCNT VOLADDR(0x04000132, u16) 21 | 22 | // Key bits as used by KEYINPUT and KEYCNT. 23 | enum Key 24 | { 25 | KEY_A = BIT(0), 26 | KEY_B = BIT(1), 27 | KEY_SELECT = BIT(2), 28 | KEY_START = BIT(3), 29 | KEY_RIGHT = BIT(4), 30 | KEY_LEFT = BIT(5), 31 | KEY_UP = BIT(6), 32 | KEY_DOWN = BIT(7), 33 | KEY_R = BIT(8), 34 | KEY_L = BIT(9), 35 | }; 36 | 37 | // Groupings of key bits. 38 | enum KeyGroup 39 | { 40 | KEYS_DPAD_X = KEY_LEFT | KEY_RIGHT, 41 | KEYS_DPAD_Y = KEY_UP | KEY_DOWN, 42 | KEYS_DPAD = KEYS_DPAD_X | KEYS_DPAD_Y, 43 | KEYS_AB = KEY_A | KEY_B, 44 | KEYS_LR = KEY_L | KEY_R, 45 | KEYS_STARTSELECT = KEY_START | KEY_SELECT, 46 | KEYS_BUTTONS = KEYS_AB | KEYS_LR | KEYS_STARTSELECT, 47 | KEYS_ALL = KEYS_DPAD | KEYS_BUTTONS, 48 | }; 49 | 50 | // Bit indices of keys bits. 51 | enum KeyIndex 52 | { 53 | KEY_INDEX_A, 54 | KEY_INDEX_B, 55 | KEY_INDEX_SELECT, 56 | KEY_INDEX_START, 57 | KEY_INDEX_RIGHT, 58 | KEY_INDEX_LEFT, 59 | KEY_INDEX_UP, 60 | KEY_INDEX_DOWN, 61 | KEY_INDEX_R, 62 | KEY_INDEX_L, 63 | KEY_INDEX_MAX, 64 | }; 65 | 66 | enum KeyIRQ 67 | { 68 | KEY_IRQ_ENABLE = BIT(14), 69 | KEY_IRQ_PRESS_ALL = BIT(15), 70 | KEY_IRQ_PRESS_ANY = !KEY_IRQ_PRESS_ALL, 71 | }; 72 | 73 | // Updates the internal keypad state. Should be called once per frame. 74 | extern void inputPoll(void); 75 | 76 | // Gets the internal keypad state. The lower halfword contains the current 77 | // input state, the upper halfword contains the last input state. 78 | extern u32 inputState(void); 79 | 80 | // Returns the keys that were pressed this frame. ("Rising egde") 81 | extern u16 inputKeysPressed(u16 keys); 82 | 83 | // Returns the keys that were released this frame. ("Falling edge") 84 | extern u16 inputKeysReleased(u16 keys); 85 | 86 | // Returns the keys that are being held this frame. 87 | extern u16 inputKeysDown(u16 keys); 88 | 89 | // Returns the keys that are not being held this frame. 90 | extern u16 inputKeysUp(u16 keys); 91 | 92 | // Gets the state of the Dpad X-axis. 93 | // -1: Left 94 | // 0: None 95 | // 1: Right 96 | extern i32 inputAxisX(void); 97 | 98 | // Gets the state of the Dpad Y-axis. 99 | // -1: Up 100 | // 0: None 101 | // 1: Down 102 | extern i32 inputAxisY(void); 103 | 104 | // Gets the state of the shoulder button axis. 105 | // -1: L 106 | // 0: None/Both 107 | // 1: R 108 | extern i32 inputAxisLR(void); 109 | 110 | // Gets the state of the face button axis. 111 | // -1: B 112 | // 0: None/Both 113 | // 1: A 114 | extern i32 inputAxisAB(void); 115 | 116 | _LIBSEVEN_EXTERN_C_END 117 | 118 | #endif /* !_LIBSEVEN_HW_INPUT_H */ 119 | -------------------------------------------------------------------------------- /include/seven/hw/irq.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_IRQ_H 8 | #define _LIBSEVEN_HW_IRQ_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | // Attribute for compiling functions in a way that is suitable for IRQ handlers. 15 | // 16 | #define IRQ_HANDLER IWRAM_CODE ARM_CODE 17 | 18 | // Interrupt Enable 19 | // 20 | #define REG_IE VOLADDR(0x04000200, u16) 21 | 22 | // Interrupt Flags 23 | // 24 | #define REG_IF VOLADDR(0x04000202, u16) 25 | 26 | // Interrupt Master Enable 27 | // 28 | #define REG_IME VOLADDR(0x04000208, u16) 29 | 30 | // IRQ bitflags 31 | // 32 | enum IRQ 33 | { 34 | IRQ_VBLANK = BIT(0), 35 | IRQ_HBLANK = BIT(1), 36 | IRQ_VCOUNT = BIT(2), 37 | IRQ_TIMER_0 = BIT(3), 38 | IRQ_TIMER_1 = BIT(4), 39 | IRQ_TIMER_2 = BIT(5), 40 | IRQ_TIMER_3 = BIT(6), 41 | IRQ_SERIAL = BIT(7), 42 | IRQ_DMA_0 = BIT(8), 43 | IRQ_DMA_1 = BIT(9), 44 | IRQ_DMA_2 = BIT(10), 45 | IRQ_DMA_3 = BIT(11), 46 | IRQ_KEYPAD = BIT(12), 47 | IRQ_CARTRIDGE = BIT(13), 48 | }; 49 | 50 | // Common sets of IRQs 51 | // 52 | enum IRQGroup 53 | { 54 | IRQS_BLANK = IRQ_VBLANK | IRQ_HBLANK, 55 | IRQS_TIMER = IRQ_TIMER_0 | IRQ_TIMER_1 | IRQ_TIMER_2 | IRQ_TIMER_3, 56 | IRQS_DMA = IRQ_DMA_0 | IRQ_DMA_1 | IRQ_DMA_2 | IRQ_DMA_3, 57 | // IRQs in this group can wake up the GBA from a svcStop() call. 58 | IRQS_EXTERNAL = IRQ_SERIAL | IRQ_KEYPAD | IRQ_CARTRIDGE, 59 | IRQS_ALL = BITS(14), 60 | }; 61 | 62 | // Bit indices of IRQs 63 | enum IRQIndex 64 | { 65 | IRQ_INDEX_VBLANK, 66 | IRQ_INDEX_HBLANK, 67 | IRQ_INDEX_VCOUNT, 68 | IRQ_INDEX_TIMER_0, 69 | IRQ_INDEX_TIMER_1, 70 | IRQ_INDEX_TIMER_2, 71 | IRQ_INDEX_TIMER_3, 72 | IRQ_INDEX_SERIAL, 73 | IRQ_INDEX_DMA_0, 74 | IRQ_INDEX_DMA_1, 75 | IRQ_INDEX_DMA_2, 76 | IRQ_INDEX_DMA_3, 77 | IRQ_INDEX_KEYPAD, 78 | IRQ_INDEX_CARTRIDGE, 79 | IRQ_INDEX_MAX, 80 | }; 81 | 82 | // Function type for interrupt service routines (ISRs) 83 | // 84 | // Function must be ARM code, and ideally be placed in IWRAM. 85 | // 86 | // Use the IRQ_HANDLER attribute to mark the function appropriately. 87 | typedef void IrqHandlerFn(void); 88 | 89 | // Function type for interrupt callbacks. 90 | // 91 | // The function receives the triggered IRQs as the first parameter. 92 | typedef void IrqCallbackFn(u16); 93 | 94 | // Initialize interrupt handling with a user-provided function. 95 | // 96 | extern void irqInit(IrqHandlerFn *isr); 97 | 98 | // Initialize interrupt handling using a callback system. 99 | // 100 | extern void irqInitDefault(void); 101 | 102 | // Initialize interrupt handling using a single callback function. 103 | // 104 | // irqInitSimple(my_callback) is roughly similar to 105 | // irqCallbackSet(IRQS_ALL, my_callback, 0), 106 | // but has a lower overhead. 107 | // 108 | extern void irqInitSimple(IrqCallbackFn *fn); 109 | 110 | // Initialize interrupt handling using a stub handler 111 | // that only acknowledges the IRQs and returns. 112 | // 113 | // This is enough for svcHalt, svcIntrWait, and svcVBlankIntrWait to work. 114 | // 115 | extern void irqInitStub(void); 116 | 117 | // Set the callback associated with the specified irqs. 118 | // 119 | // The priority argument is used to set the search order. 120 | // A lower value specifies a higher priority. 121 | // 122 | // Fails if any of the irqs already have a callback set. 123 | // 124 | extern bool irqCallbackSet(u16 irqs, IrqCallbackFn *fn, u16 priority); 125 | 126 | // Delete the callback associated with the specified irqs. 127 | // 128 | // Fails if the irqs don't share a slot, or any of the irqs don't have a slot. 129 | // 130 | extern bool irqCallbackDelete(u16 irqs); 131 | 132 | // Gets the highest priority callback function matching the specified irqs, 133 | // or NULL if none is found. 134 | // 135 | // This function is placed in IWRAM so that it can be called quickly from 136 | // a user-provided IRQ handler. 137 | extern IrqCallbackFn* irqCallbackLookup(u16 irqs); 138 | 139 | // Enable the specified IRQs. 140 | // 141 | // Returns the old value of the IE register. 142 | // 143 | extern u16 irqEnable(u16 irqs); 144 | 145 | // Disable the specified IRQs. 146 | // 147 | // Returns the old value of the IE register. 148 | // 149 | extern u16 irqDisable(u16 irqs); 150 | 151 | // Enable the specified IRQs and set the matching I/O registers. 152 | // 153 | // Returns the old value of the IE register. 154 | // 155 | extern u16 irqEnableFull(u16 irqs); 156 | 157 | // Disable the specified IRQs and set the matching I/O registers. 158 | // 159 | // Returns the old value of the IE register. 160 | // 161 | extern u16 irqDisableFull(u16 irqs); 162 | 163 | // Saves the current value of the IME register and disables it until 164 | // a call to irqCriticalSectionExit. 165 | // 166 | // Calls can be nested. Only the outermost call affects IME. 167 | // 168 | extern void irqCriticalSectionEnter(void); 169 | 170 | // Restores the value of IME saved by a 171 | // previous call to irqCriticalSectionEnter. 172 | // 173 | // Only the outermost call affects IME. 174 | // 175 | extern void irqCriticalSectionExit(void); 176 | 177 | // Returns true if currently inside a critical section. 178 | // 179 | extern bool irqCriticalSectionActive(void); 180 | 181 | _LIBSEVEN_EXTERN_C_END 182 | 183 | #endif /* !_LIBSEVEN_HW_IRQ_H */ 184 | -------------------------------------------------------------------------------- /include/seven/hw/memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_MEMORY_H 8 | #define _LIBSEVEN_HW_MEMORY_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | // BIOS ROM (16K) 15 | #define MEM_BIOS ((const void*)0x00000000) 16 | #define MEM_BIOS_SIZE ((usize)0x4000) 17 | 18 | // External Work RAM (256K) 19 | #define MEM_EWRAM ((void*)0x02000000) 20 | #define MEM_EWRAM_SIZE ((usize)0x40000) 21 | 22 | // Internal Work RAM (32K) 23 | #define MEM_IWRAM ((void*)0x03000000) 24 | #define MEM_IWRAM_SIZE ((usize)0x8000) 25 | 26 | // I/O Register Area 27 | #define REG_BASE ((volatile void*)0x04000000) 28 | #define MEM_IO ((volatile void*)0x04000000) 29 | // If you include undocumented registers, it's 0x804. 30 | #define MEM_IO_SIZE ((usize)0x20C) 31 | 32 | // Video Palette (1K) 33 | #define MEM_PALETTE ((void*)0x05000000) 34 | #define MEM_PALETTE_SIZE ((usize)0x400) 35 | 36 | // Video RAM (96K) 37 | #define MEM_VRAM ((void*)0x06000000) 38 | #define MEM_VRAM_SIZE ((usize)0x18000) 39 | 40 | // Object Attribute Memory (1K) 41 | #define MEM_OAM ((void*)0x07000000) 42 | #define MEM_OAM_SIZE ((usize)0x400) 43 | 44 | // Cartridge ROM (32M) 45 | #define MEM_ROM ((const void*)0x08000000) 46 | #define MEM_ROM_SIZE ((usize)0x02000000) 47 | 48 | // Cartridge Static RAM (32K) 49 | #define MEM_SRAM ((void*)0x0E000000) 50 | #define MEM_SRAM_SIZE ((usize)0x8000) 51 | 52 | _LIBSEVEN_EXTERN_C_END 53 | 54 | #endif /* !_LIBSEVEN_HW_MEMORY_H */ 55 | -------------------------------------------------------------------------------- /include/seven/hw/serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_SERIAL_H 8 | #define _LIBSEVEN_HW_SERIAL_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #define REG_SIOCNT VOLADDR(0x04000128, u16) 15 | #define REG_SIOMODE2 VOLADDR(0x04000134, u16) 16 | #define REG_SIODATA8 VOLADDR(0x0400012A, u8) 17 | #define REG_SIODATA32 VOLADDR(0x04000120, u32) 18 | 19 | enum SerialIOControl 20 | { 21 | SIO_CLOCK_INTERNAL = BIT(0), 22 | SIO_CLOCK_EXTERNAL = !SIO_CLOCK_INTERNAL, 23 | 24 | SIO_CLOCK_FREQ_2MHZ = BIT(1), 25 | SIO_CLOCK_FREQ_256KHZ = !SIO_CLOCK_FREQ_2MHZ, 26 | 27 | SIO_SO_READY_HIGH = BIT(3), 28 | SIO_SO_READY_LOW = !SIO_SO_READY_HIGH, 29 | 30 | SIO_START = BIT(7), 31 | 32 | SIO_32BIT = BIT(12), 33 | SIO_8BIT = !SIO_32BIT, 34 | 35 | #define BF_SIO_MODE_OFFSET 12 36 | #define BF_SIO_MODE_LENGTH 2 37 | 38 | #define SIO_MODE(n) BITFIELD(SIO_MODE, (n)) 39 | 40 | SIO_MODE_NORMAL = SIO_MODE(0), 41 | SIO_MODE_MULTI = SIO_MODE(2), 42 | SIO_MODE_UART = SIO_MODE(3), 43 | 44 | // These are for symmetry, raw & JOY Bus modes ignore the SIO_MODE bits. 45 | SIO_MODE_RAW = SIO_MODE(0), 46 | SIO_MODE_JOYBUS = SIO_MODE(0), 47 | 48 | SIO_IRQ_ENABLE = BIT(14), 49 | }; 50 | 51 | enum SerialIOMode2 52 | { 53 | #define BF_SIO_MODE2_OFFSET 14 54 | #define BF_SIO_MODE2_LENGTH 2 55 | 56 | #define SIO_MODE2(n) BITFIELD(SIO_MODE2, (n)) 57 | 58 | SIO_MODE2_NORMAL = SIO_MODE2(0), 59 | SIO_MODE2_MULTI = SIO_MODE2(0), 60 | SIO_MODE2_UART = SIO_MODE2(0), 61 | 62 | SIO_MODE2_RAW = SIO_MODE2(2), 63 | SIO_MODE2_JOYBUS = SIO_MODE2(3), 64 | }; 65 | 66 | _LIBSEVEN_EXTERN_C_END 67 | 68 | #endif /* !_LIBSEVEN_HW_SERIAL_H */ 69 | -------------------------------------------------------------------------------- /include/seven/hw/sound.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_SOUND_H 8 | #define _LIBSEVEN_HW_SOUND_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #error "seven/hw/sound.h is a stub header" 15 | 16 | _LIBSEVEN_EXTERN_C_END 17 | 18 | #endif /* !_LIBSEVEN_HW_SOUND_H */ 19 | -------------------------------------------------------------------------------- /include/seven/hw/sram.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_SRAM_H 8 | #define _LIBSEVEN_HW_SRAM_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | extern void sramRead(void *dst, usize len); 15 | 16 | extern void sramReadAt(void *dst, usize len, usize off); 17 | 18 | extern void sramWrite(const void *src, usize len); 19 | 20 | extern void sramWriteAt(const void *src, usize len, usize off); 21 | 22 | extern usize sramCompare(const void *src, usize len); 23 | 24 | extern usize sramCompareAt(const void *src, usize len, usize off); 25 | 26 | extern void sramClear(usize len); 27 | 28 | extern void sramClearAt(usize len, usize off); 29 | 30 | extern void sramRead64(void *dst, size_t len); 31 | 32 | extern void sramReadAt64(void *dst, size_t len, size_t off); 33 | 34 | extern void sramWrite64(const void *src, size_t len); 35 | 36 | extern void sramWriteAt64(const void *src, size_t len, size_t off); 37 | 38 | extern void sramClear64(size_t len); 39 | 40 | extern void sramClearAt64(size_t len, size_t off); 41 | 42 | _LIBSEVEN_EXTERN_C_END 43 | 44 | #endif /* !_LIBSEVEN_HW_SRAM_H */ 45 | -------------------------------------------------------------------------------- /include/seven/hw/svc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_SVC_H 8 | #define _LIBSEVEN_HW_SVC_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | // Just for reference, or if you want to use an inline SVC macro. 15 | // I will not provide you with one, inline SVCs are a terrible idea! 16 | // Not like you're losing much speed compared to the BIOS dispatch overhead... 17 | enum SupervisorCallNumber 18 | { 19 | SVC_SOFTRESET = 0, 20 | SVC_REGISTERRAMRESET = 1, 21 | 22 | SVC_HALT = 2, 23 | SVC_STOP = 3, 24 | SVC_INTRWAIT = 4, 25 | SVC_VBLANKINTRWAIT = 5, 26 | 27 | SVC_DIV = 6, 28 | SVC_SQRT = 8, 29 | SVC_ARCTAN = 9, 30 | SVC_ARCTAN2 = 10, 31 | 32 | SVC_CPUSET = 11, 33 | SVC_CPUFASTSET = 12, 34 | 35 | SVC_BIOSCHECKSUM = 13, 36 | 37 | SVC_BGAFFINESET = 14, 38 | SVC_OBJAFFINESET = 15, 39 | 40 | SVC_BITUNPACK = 16, 41 | SVC_LZ77UNCOMPWRAM = 17, 42 | SVC_LZ77UNCOMPVRAM = 18, 43 | SVC_HUFFUNCOMP = 19, 44 | SVC_RLUNCOMPWRAM = 20, 45 | SVC_RLUNCOMPVRAM = 21, 46 | SVC_DIFF8BITUNFILTERWRAM = 22, 47 | SVC_DIFF8BITUNFILTERVRAM = 23, 48 | SVC_DIFF16BITUNFILTER = 24, 49 | 50 | SVC_SOUNDBIAS = 25, 51 | SVC_SOUNDDRIVERINIT = 26, 52 | SVC_SOUNDDRIVERMODE = 27, 53 | SVC_SOUNDDRIVERMAIN = 28, 54 | SVC_SOUNDDRIVERVSYNC = 29, 55 | SVC_SOUNDCHANNELCLEAR = 30, 56 | SVC_MIDIKEY2FREQ = 31, 57 | SVC_MUSICPLAYEROPEN = 32, 58 | SVC_MUSICPLAYERSTART = 33, 59 | SVC_MUSICPLAYERSTOP = 34, 60 | SVC_MUSICPLAYERCONTINUE = 35, 61 | SVC_MUSICPLAYERFADEOUT = 36, 62 | 63 | SVC_MULTIBOOT = 37, 64 | 65 | SVC_HARDRESET = 38, 66 | 67 | SVC_SOUNDDRIVERVSYNCOFF = 40, 68 | SVC_SOUNDDRIVERVSYNCON = 41, 69 | }; 70 | 71 | _LIBSEVEN_EXTERN_C_END 72 | 73 | #endif /* !_LIBSEVEN_HW_SVC_H */ 74 | -------------------------------------------------------------------------------- /include/seven/hw/timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_TIMER_H 8 | #define _LIBSEVEN_HW_TIMER_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #define REG_TM0VAL VOLADDR(0x04000100, u16) 15 | #define REG_TM0CNT VOLADDR(0x04000102, u16) 16 | 17 | #define REG_TM1VAL VOLADDR(0x04000104, u16) 18 | #define REG_TM1CNT VOLADDR(0x04000106, u16) 19 | 20 | #define REG_TM2VAL VOLADDR(0x04000108, u16) 21 | #define REG_TM2CNT VOLADDR(0x0400010A, u16) 22 | 23 | #define REG_TM3VAL VOLADDR(0x0400010C, u16) 24 | #define REG_TM3CNT VOLADDR(0x0400010E, u16) 25 | 26 | #define BF_TIMER_FREQ_OFFSET 0 27 | #define BF_TIMER_FREQ_LENGTH 3 28 | 29 | #define TIMER_FREQ(n) BITFIELD(TIMER_FREQ, (n)) 30 | 31 | enum TimerControl 32 | { 33 | TIMER_FREQ_16MHZ = TIMER_FREQ(0), 34 | TIMER_FREQ_262KHZ = TIMER_FREQ(1), 35 | TIMER_FREQ_64KHZ = TIMER_FREQ(2), 36 | TIMER_FREQ_16KHZ = TIMER_FREQ(3), 37 | TIMER_FREQ_CASCADE = TIMER_FREQ(4), 38 | TIMER_IRQ_ENABLE = BIT(6), 39 | TIMER_ENABLE = BIT(7), 40 | }; 41 | 42 | extern void timerSet(u32 num, u16 reload, u16 flags); 43 | 44 | extern void timerEnable(u32 num); 45 | 46 | extern void timerDisable(u32 num); 47 | 48 | extern u16 timerGetValue(u32 num); 49 | 50 | _LIBSEVEN_EXTERN_C_END 51 | 52 | #endif /* !_LIBSEVEN_HW_TIMER_H */ 53 | -------------------------------------------------------------------------------- /include/seven/hw/video.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_VIDEO_H 8 | #define _LIBSEVEN_HW_VIDEO_H 9 | 10 | #include 11 | #include 12 | 13 | _LIBSEVEN_EXTERN_C 14 | 15 | #define REG_DISPCNT VOLADDR(0x04000000, u16) 16 | #define REG_DISPSTAT VOLADDR(0x04000004, u16) 17 | #define REG_VCOUNT VOLADDR(0x04000006, const u16) 18 | #define REG_BGCNT VOLARRAY(0x04000008, u16, 4) 19 | #define REG_BG0CNT VOLADDR(0x04000008, u16) 20 | #define REG_BG1CNT VOLADDR(0x0400000A, u16) 21 | #define REG_BG2CNT VOLADDR(0x0400000C, u16) 22 | #define REG_BG3CNT VOLADDR(0x0400000E, u16) 23 | 24 | enum LCDDimensions 25 | { 26 | LCD_WIDTH = 240, 27 | LCD_HEIGHT = 160, 28 | LCD_SCANLINES = 228, 29 | }; 30 | 31 | enum DisplayControl 32 | { 33 | 34 | #define BF_VIDEO_MODE_OFFSET 0 35 | #define BF_VIDEO_MODE_LENGTH 3 36 | 37 | #define VIDEO_MODE(n) BITFIELD(VIDEO_MODE, (n)) 38 | 39 | VIDEO_MODE_REGULAR = VIDEO_MODE(0), 40 | VIDEO_MODE_MIXED = VIDEO_MODE(1), 41 | VIDEO_MODE_AFFINE = VIDEO_MODE(2), 42 | VIDEO_MODE_BITMAP = VIDEO_MODE(3), 43 | VIDEO_MODE_BITMAP_INDEXED = VIDEO_MODE(4), 44 | VIDEO_MODE_BITMAP_SMALL = VIDEO_MODE(5), 45 | 46 | VIDEO_FRAME_SELECT = BIT(4), 47 | 48 | VIDEO_OBJ_LAYOUT_1D = BIT(6), 49 | VIDEO_OBJ_LAYOUT_2D = !VIDEO_OBJ_LAYOUT_1D, 50 | 51 | VIDEO_FORCE_BLANK = BIT(7), 52 | 53 | VIDEO_BG0_ENABLE = BIT(8), 54 | VIDEO_BG1_ENABLE = BIT(9), 55 | VIDEO_BG2_ENABLE = BIT(10), 56 | VIDEO_BG3_ENABLE = BIT(11), 57 | VIDEO_OBJ_ENABLE = BIT(12), 58 | VIDEO_WIN0_ENABLE = BIT(13), 59 | VIDEO_WIN1_ENABLE = BIT(14), 60 | VIDEO_OBJ_WIN_ENABLE = BIT(15), 61 | }; 62 | 63 | enum DisplayStatus 64 | { 65 | LCD_IN_VBLANK = BIT(0), 66 | LCD_IN_HBLANK = BIT(1), 67 | LCD_VCOUNT_MATCH = BIT(2), 68 | 69 | LCD_VBLANK_IRQ_ENABLE = BIT(3), 70 | LCD_HBLANK_IRQ_ENABLE = BIT(4), 71 | LCD_VCOUNT_IRQ_ENABLE = BIT(5), 72 | 73 | #define BF_LCD_TARGET_VCOUNT_OFFSET 8 74 | #define BF_LCD_TARGET_VCOUNT_LENGTH 8 75 | 76 | #define LCD_TARGET_VCOUNT(n) BITFIELD(LCD_TARGET_VCOUNT, (n)) 77 | }; 78 | 79 | enum VerticalCount 80 | { 81 | VCOUNT_DRAW_START = 0, 82 | VCOUNT_DRAW_END = 160, 83 | VCOUNT_BLANK_START = 160, 84 | VCOUNT_BLANK_END = 0, 85 | }; 86 | 87 | enum BackgroundControl 88 | { 89 | #define BF_BG_PRIORITY_OFFSET 0 90 | #define BF_BG_PRIORITY_LENGTH 2 91 | 92 | #define BG_PRIORITY(n) BITFIELD(BG_PRIORITY, (n)) 93 | 94 | BG_PRIORITY_MIN = BG_PRIORITY(3), 95 | BG_PRIORITY_MAX = BG_PRIORITY(0), 96 | 97 | #define BF_BG_GFX_BASE_OFFSET 2 98 | #define BF_BG_GFX_BASE_LENGTH 2 99 | 100 | #define BG_GFX_BASE(n) BITFIELD(BG_GFX_BASE, (n)) 101 | 102 | BG_MOSAIC_ENABLE = BIT(6), 103 | 104 | BG_TILE_8BPP = BIT(7), 105 | BG_TILE_4BPP = !BG_TILE_8BPP, 106 | 107 | #define BF_BG_MAP_BASE_OFFSET 8 108 | #define BF_BG_MAP_BASE_LENGTH 5 109 | 110 | #define BG_MAP_BASE(n) BITFIELD(BG_MAP_BASE, (n)) 111 | 112 | BG_AFFINE_WRAP = BIT(13), 113 | 114 | #define BF_BG_SIZE_OFFSET 14 115 | #define BF_BG_SIZE_LENGTH 2 116 | 117 | #define BG_SIZE(n) BITFIELD(BG_SIZE, (n)) 118 | 119 | BG_SIZE_256X256 = BG_SIZE(0), 120 | BG_SIZE_512X256 = BG_SIZE(1), 121 | BG_SIZE_256X512 = BG_SIZE(2), 122 | BG_SIZE_512X512 = BG_SIZE(3), 123 | 124 | #define BF_BG_AFFINE_SIZE_OFFSET 14 125 | #define BF_BG_AFFINE_SIZE_LENGTH 2 126 | 127 | #define BG_AFFINE_SIZE(n) BITFIELD(BG_AFFINE_SIZE, (n)) 128 | 129 | BG_AFFINE_SIZE_128X128 = BG_AFFINE_SIZE(0), 130 | BG_AFFINE_SIZE_256X256 = BG_AFFINE_SIZE(1), 131 | BG_AFFINE_SIZE_512X512 = BG_AFFINE_SIZE(2), 132 | BG_AFFINE_SIZE_1024X1024 = BG_AFFINE_SIZE(3), 133 | }; 134 | 135 | #define GFX_BASE_ADDR(n) (MEM_VRAM + ((n) << 14)) 136 | #define MAP_BASE_ADDR(n) (MEM_VRAM + ((n) << 11)) 137 | 138 | // TODO: Move these? 139 | typedef u16 Color; 140 | typedef Color Palette[256]; 141 | typedef Color PaletteBank[16]; 142 | 143 | #define BG_PALETTE MEMADDR(MEM_PALETTE, Palette) 144 | #define OBJ_PALETTE MEMADDR(MEM_PALETTE + 512, Palette) 145 | 146 | #define BG_PALETTE_BANK MEMARRAY(MEM_PALETTE, PaletteBank, 16) 147 | #define OBJ_PALETTE_BANK MEMARRAY(MEM_PALETTE + 512, PaletteBank, 16) 148 | 149 | _LIBSEVEN_EXTERN_C_END 150 | 151 | #endif /* !_LIBSEVEN_HW_VIDEO_H */ 152 | -------------------------------------------------------------------------------- /include/seven/hw/waitstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_HW_WAITSTATE_H 8 | #define _LIBSEVEN_HW_WAITSTATE_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #define REG_WAITCNT VOLADDR(0x04000204, u16) 15 | 16 | #define BF_WAIT_SRAM_OFFSET 0 17 | #define BF_WAIT_SRAM_LENGTH 2 18 | 19 | #define WAIT_SRAM(n) BITFIELD(WAIT_SRAM, (n)) 20 | 21 | #define BF_WAIT_ROM_N_OFFSET 2 22 | #define BF_WAIT_ROM_N_LENGTH 2 23 | 24 | #define WAIT_ROM_N(n) BITFIELD(WAIT_ROM_N, (n)) 25 | 26 | #define BF_WAIT_ROM_S_OFFSET 4 27 | #define BF_WAIT_ROM_S_LENGTH 1 28 | 29 | #define WAIT_ROM_S(n) BITFIELD(WAIT_ROM_S, (n)) 30 | 31 | enum Waitstate 32 | { 33 | WAIT_SRAM_4 = WAIT_SRAM(0), 34 | WAIT_SRAM_3 = WAIT_SRAM(1), 35 | WAIT_SRAM_2 = WAIT_SRAM(2), 36 | WAIT_SRAM_8 = WAIT_SRAM(3), 37 | 38 | WAIT_ROM_N_4 = WAIT_ROM_N(0), 39 | WAIT_ROM_N_3 = WAIT_ROM_N(1), 40 | WAIT_ROM_N_2 = WAIT_ROM_N(2), 41 | WAIT_ROM_N_8 = WAIT_ROM_N(3), 42 | 43 | WAIT_ROM_S_2 = WAIT_ROM_S(0), 44 | WAIT_ROM_S_1 = WAIT_ROM_S(1), 45 | 46 | WAIT_PREFETCH_ENABLE = BIT(14), 47 | }; 48 | 49 | _LIBSEVEN_EXTERN_C_END 50 | 51 | #endif /* !_LIBSEVEN_HW_WAITSTATE_H */ 52 | -------------------------------------------------------------------------------- /include/seven/prelude.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_PRELUDE_H 8 | #define _LIBSEVEN_PRELUDE_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #endif /* !_LIBSEVEN_PRELUDE_H */ 18 | -------------------------------------------------------------------------------- /include/seven/serial/joybus.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SERIAL_JOYBUS_H 8 | #define _LIBSEVEN_SERIAL_JOYBUS_H 9 | 10 | #error "seven/serial/joybus.h is unfinished and should not be used" 11 | 12 | #include 13 | #include 14 | 15 | _LIBSEVEN_EXTERN_C 16 | 17 | // Joy BUS Control 18 | #define REG_JOYCNT VOLADDR(0x04000140, u16) 19 | // Send/Receive for JOY Bus 20 | #define REG_JOYRECV VOLADDR(0x04000150, u32) 21 | #define REG_JOYSEND VOLADDR(0x04000154, u32) 22 | // JOY Bus Status 23 | #define REG_JOYSTAT VOlADDR(0x04000158, u16) 24 | 25 | _LIBSEVEN_EXTERN_C_END 26 | 27 | #endif /* !_LIBSEVEN_SERIAL_JOYBUS_H */ 28 | -------------------------------------------------------------------------------- /include/seven/serial/multiplayer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SERIAL_MULTIPLAYER_H 8 | #define _LIBSEVEN_SERIAL_MULTIPLAYER_H 9 | 10 | #error "seven/serial/multiplayer.h is unfinished and should not be used" 11 | 12 | #include 13 | #include 14 | 15 | _LIBSEVEN_EXTERN_C 16 | 17 | // Send for Multiplayer 18 | #define REG_SIODATA16 VOLADDR(0x0400012A, u16) 19 | 20 | // Receive for Multiplayer 21 | #define REG_SIOMULTI0 VOLADDR(0x04000120, u16) 22 | #define REG_SIOMULTI1 VOLADDR(0x04000122, u16) 23 | #define REG_SIOMULTI2 VOLADDR(0x04000124, u16) 24 | #define REG_SIOMULTI3 VOLADDR(0x04000126, u16) 25 | 26 | // 0-1: Baud rate (9600, 38400, 57600, 115200) 27 | // 2: SI Pin (0 = Parent, 1 = Child) 28 | // 3: SD Pin (0 = Error, 1 = Everyone Ready) 29 | // 4-5: Client ID (0 = Server) 30 | // 6: Error 31 | // 7: Start/Busy (0 = Inactive, 1 = Start/Busy) 32 | // 14: IRQ Enable 33 | enum SerialIOControlMultiplayer 34 | { 35 | 36 | }; 37 | 38 | _LIBSEVEN_EXTERN_C_END 39 | 40 | #endif /* !_LIBSEVEN_SERIAL_MULTIPLAYER_H */ 41 | -------------------------------------------------------------------------------- /include/seven/serial/raw.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SERIAL_RAW_H 8 | #define _LIBSEVEN_SERIAL_RAW_H 9 | 10 | #error "seven/serial/raw.h is unfinished and should not be used" 11 | 12 | #include 13 | #include 14 | 15 | _LIBSEVEN_EXTERN_C 16 | 17 | // "Raw mode" access register 18 | #define REG_SIORAW VOLADDR(0x04000134, u16) 19 | 20 | enum SerialIORaw 21 | { 22 | SIO_RAW_SC_DATA = BIT(0), 23 | SIO_RAW_SD_DATA = BIT(1), 24 | SIO_RAW_SI_DATA = BIT(2), 25 | SIO_RAW_SO_DATA = BIT(3), 26 | 27 | SIO_RAW_SC_DIR_OUT = BIT(4), 28 | SIO_RAW_SC_DIR_IN = !SIO_RAW_SC_DIR_OUT, 29 | 30 | SIO_RAW_SD_DIR_OUT = BIT(5), 31 | SIO_RAW_SD_DIR_IN = !SIO_RAW_SD_DIR_OUT, 32 | 33 | SIO_RAW_SI_DIR_OUT = BIT(6), 34 | SIO_RAW_SI_DIR_IN = !SIO_RAW_SI_DIR_OUT, 35 | 36 | SIO_RAW_SO_DIR_OUT = BIT(7), 37 | SIO_RAW_SO_DIR_IN = !SIO_RAW_SO_DIR_OUT, 38 | 39 | SIO_RAW_SI_IRQ_ENABLE = BIT(8), 40 | }; 41 | 42 | _LIBSEVEN_EXTERN_C_END 43 | 44 | #endif /* !_LIBSEVEN_SERIAL_RAW_H */ 45 | -------------------------------------------------------------------------------- /include/seven/serial/uart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SERIAL_UART_H 8 | #define _LIBSEVEN_SERIAL_UART_H 9 | 10 | #error "seven/serial/uart.h is unfinished and should not be used" 11 | 12 | #include 13 | #include 14 | 15 | _LIBSEVEN_EXTERN_C 16 | // 17 | // 0-1: Baud rate (Like above) 18 | // 2: CTS (Clearance to Send Enable) (0 = Send always, 1 = Wait for SC=0) 19 | // 3: Parity control (0 = Even, 1 = Odd) 20 | // 4: Send data flag (0 = Not full, 1 = Full) 21 | // 5: Recv data flag (0 = Not empty, 1 = Empty) 22 | // 6: Error 23 | // 7: Data length (0 = 7 Bit, 1 = 8 Bit) 24 | // 8: FIFO Enable 25 | // 9: Parity Enable 26 | // 10: Send Enable 27 | // 11: Recv Enable 28 | // 14: IRQ Enable 29 | enum SerialIOControlUART 30 | { 31 | }; 32 | 33 | _LIBSEVEN_EXTERN_C_END 34 | 35 | #endif /* !_LIBSEVEN_SERIAL_UART_H */ 36 | -------------------------------------------------------------------------------- /include/seven/svc/affine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SVC_AFFINE_H 8 | #define _LIBSEVEN_SVC_AFFINE_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | struct BgAffineSrcData 15 | { 16 | i32 src_center_x; 17 | i32 src_center_y; 18 | i16 disp_center_x; 19 | i16 disp_center_y; 20 | i16 ratio_x; 21 | i16 ratio_y; 22 | u16 theta; 23 | }; 24 | 25 | struct BgAffineDstData 26 | { 27 | i16 h_diff_x; 28 | i16 v_diff_x; 29 | i16 h_diff_y; 30 | i16 v_diff_y; 31 | i32 start_x; 32 | i32 start_y; 33 | }; 34 | 35 | extern void svcBgAffineSet( 36 | const struct BgAffineSrcData *src, 37 | struct BgAffineDstData *dst, 38 | u32 num); 39 | 40 | struct ObjAffineSrcData 41 | { 42 | i16 ratio_x; 43 | i16 ratio_y; 44 | u16 theta; 45 | }; 46 | 47 | struct ObjAffineDstData 48 | { 49 | i16 h_diff_x; 50 | i16 v_diff_x; 51 | i16 h_diff_y; 52 | i16 v_diff_y; 53 | }; 54 | 55 | enum ObjAffineSetOffset 56 | { 57 | OAS_OFFSET_DSTDATA = 2, 58 | OAS_OFFSET_OAM = 8, 59 | }; 60 | 61 | extern void svcObjAffineSet( 62 | const struct ObjAffineSrcData *src, 63 | void *dst, 64 | u32 num, 65 | u32 offset); 66 | 67 | _LIBSEVEN_EXTERN_C_END 68 | 69 | #endif /* !_LIBSEVEN_SVC_AFFINE_H */ 70 | -------------------------------------------------------------------------------- /include/seven/svc/decompression.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SVC_DECOMPRESSION_H 8 | #define _LIBSEVEN_SVC_DECOMPRESSION_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | struct BitUnPackParam 15 | { 16 | u16 src_length; // in bytes 17 | u8 src_width; // width of each src element (1, 2, 4, 8) 18 | u8 dst_width; // width of each dst element (1, 2, 4, 8, 16, 32) 19 | u32 offset:31; // value to add to each dst element 20 | u32 keep_zeroes:1; // whether to add offset to zero elements too 21 | }; 22 | 23 | extern void svcBitUnPack( 24 | const void *src, 25 | void *dst, 26 | const struct BitUnPackParam *param); 27 | 28 | extern void svcLZ77UnCompWram(const void *src, void *dst); 29 | extern void svcLZ77UnCompVram(const void *src, void *dst); 30 | extern void svcHuffUnComp(const void *src, void *dst); 31 | extern void svcRLUnCompWram(const void *src, void *dst); 32 | extern void svcRLUnCompVram(const void *src, void *dst); 33 | extern void svcDiff8bitUnFilterWram(const void *src, void *dst); 34 | extern void svcDiff8bitUnFilterVram(const void *src, void *dst); 35 | extern void svcDiff16bitUnFilter(const void *src, void *dst); 36 | 37 | _LIBSEVEN_EXTERN_C_END 38 | 39 | #endif /* !_LIBSEVEN_SVC_DECOMPRESSION_H */ 40 | -------------------------------------------------------------------------------- /include/seven/svc/math.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SVC_MATH_H 8 | #define _LIBSEVEN_SVC_MATH_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | struct Div 15 | { 16 | i32 quot; 17 | i32 rem; 18 | }; 19 | 20 | extern struct Div svcDiv(i32 numerator, i32 denominator); 21 | extern u16 svcSqrt(u32 x); 22 | extern i16 svcArcTan(i16 tan); 23 | extern u16 svcArcTan2(i16 x, i16 y); 24 | 25 | _LIBSEVEN_EXTERN_C_END 26 | 27 | #endif /* !_LIBSEVEN_SVC_MATH_H */ 28 | -------------------------------------------------------------------------------- /include/seven/svc/memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SVC_MEMORY_H 8 | #define _LIBSEVEN_SVC_MEMORY_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | enum CpuSetFlags 15 | { 16 | CS_SRC_FIXED = BIT(24), 17 | CS_32BIT = BIT(26), 18 | CS_16BIT = !CS_32BIT, 19 | }; 20 | 21 | extern void svcCpuSet(const void *src, void *dst, u32 ctrl); 22 | extern void svcCpuSetFixed(u32 value, void *dst, u32 ctrl); 23 | 24 | enum CpuFastSetFlags 25 | { 26 | CFS_SRC_FIXED = BIT(24), 27 | }; 28 | 29 | extern void svcCpuFastSet(const void *src, void *dst, u32 ctrl); 30 | extern void svcCpuFastSetFixed(u32 value, void *dst, u32 ctrl); 31 | 32 | _LIBSEVEN_EXTERN_C_END 33 | 34 | #endif /* !_LIBSEVEN_SVC_MEMORY_H */ 35 | -------------------------------------------------------------------------------- /include/seven/svc/reset.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SVC_RESET_H 8 | #define _LIBSEVEN_SVC_RESET_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | extern void NORETURN svcSoftReset(void); 15 | extern void NORETURN svcHardReset(void); 16 | 17 | enum SoftResetExFlags 18 | { 19 | SRE_FROM_ROM = 0, 20 | SRE_FROM_RAM = 1, 21 | }; 22 | 23 | // Combines svcRegisterRamReset and svcSoftReset 24 | // Allows reset from EWRAM, automatically unsets RRR_EWRAM 25 | // Disables IME to prevent IRQs crashing from a dangling handler 26 | extern void NORETURN svcSoftResetEx(u8 reset_flags, bool from_ewram); 27 | 28 | enum RegisterRamResetFlags 29 | { 30 | RRR_EWRAM = BIT(0), 31 | RRR_IWRAM = BIT(1), 32 | RRR_PALETTE = BIT(2), 33 | RRR_VRAM = BIT(3), 34 | RRR_OAM = BIT(4), 35 | RRR_SIO = BIT(5), 36 | RRR_SOUND = BIT(6), 37 | RRR_REGISTERS = BIT(7), 38 | RRR_EVERYTHING = BITS(8), 39 | }; 40 | 41 | extern void svcRegisterRamReset(u8 reset_flags); 42 | 43 | _LIBSEVEN_EXTERN_C_END 44 | 45 | #endif /* !_LIBSEVEN_SVC_RESET_H */ 46 | -------------------------------------------------------------------------------- /include/seven/svc/sound.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SVC_SOUND_H 8 | #define _LIBSEVEN_SVC_SOUND_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | extern void svcSoundDriverMain(void); 15 | extern void svcSoundDriverVSync(void); 16 | extern void svcSoundChannelClear(void); 17 | extern void svcSoundDriverVSyncOff(void); 18 | extern void svcSoundDriverVSyncOn(void); 19 | 20 | _LIBSEVEN_EXTERN_C_END 21 | 22 | #endif /* !_LIBSEVEN_SVC_SOUND_H */ 23 | -------------------------------------------------------------------------------- /include/seven/svc/system.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SVC_SYSTEM_H 8 | #define _LIBSEVEN_SVC_SYSTEM_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | enum BiosChecksum 15 | { 16 | BIOS_CHECKSUM_GBA = 0xBAAE187F, 17 | BIOS_CHECKSUM_NDS = 0xBAAE1880, 18 | }; 19 | 20 | extern u32 svcBiosChecksum(void); 21 | 22 | _LIBSEVEN_EXTERN_C_END 23 | 24 | #endif /* !_LIBSEVEN_SVC_SYSTEM_H */ 25 | 26 | -------------------------------------------------------------------------------- /include/seven/svc/wait.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_SVC_WAIT_H 8 | #define _LIBSEVEN_SVC_WAIT_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | extern void svcHalt(void); 15 | extern void svcStop(void); 16 | 17 | extern void svcIntrWait(bool wait_next, u16 intr_flags); 18 | 19 | enum IntrWaitExFlags 20 | { 21 | IWE_WAIT_NEXT = BIT(0), 22 | IWE_EXCLUSIVE = BIT(1), 23 | IWE_INCLUSIVE = !IWE_EXCLUSIVE, 24 | }; 25 | 26 | extern void svcIntrWaitEx(u8 op, u16 intr_flags); 27 | 28 | extern void svcVBlankIntrWait(void); 29 | 30 | _LIBSEVEN_EXTERN_C_END 31 | 32 | #endif /* !_LIBSEVEN_SVC_WAIT_H */ 33 | -------------------------------------------------------------------------------- /include/seven/util/debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_TEMPLATE_H 8 | #define _LIBSEVEN_TEMPLATE_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | enum DebugException 15 | { 16 | EXCEPTION_UNDEFINED_INSTRUCTION = 0, 17 | EXCEPTION_PREFETCH_ABORT = 1, 18 | EXCEPTION_DATA_ABORT = 2, 19 | EXCEPTION_IRQ = 3, 20 | EXCEPTION_FIQ = 4, 21 | }; 22 | 23 | // Emulates entry to the given exception vector 24 | bool dbgRaiseException(u32 exception); 25 | 26 | _LIBSEVEN_EXTERN_C_END 27 | 28 | #endif /* !_LIBSEVEN_TEMPLATE_H */ 29 | 30 | -------------------------------------------------------------------------------- /include/seven/util/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_UTIL_LOG_H 8 | #define _LIBSEVEN_UTIL_LOG_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | enum LogLevel 15 | { 16 | // logSetMaxLevel(LOG_OFF); 17 | LOG_OFF = 0, 18 | LOG_FATAL, 19 | LOG_ERROR, 20 | LOG_WARN, 21 | LOG_INFO, 22 | LOG_DEBUG, 23 | LOG_TRACE, 24 | }; 25 | 26 | // Supported log interfaces 27 | enum LogInterface 28 | { 29 | // No interface available 30 | LOGIF_NONE = 0, 31 | LOGIF_MGBA, 32 | LOGIF_NOCASH, 33 | LOGIF_VBA, 34 | LOGIF_CUSTOM = U8_MAX, 35 | }; 36 | 37 | typedef void LogCustomOutputFunction(u8, const char *); 38 | 39 | // Initializes the first available log interface, or returns LOGIF_NONE 40 | extern u8 logInit(void); 41 | 42 | // Tries to initialize the given log interface. 43 | extern bool logInitInterface(u8 interface); 44 | 45 | // Register a custom logging function. 46 | extern void logInitCustom(LogCustomOutputFunction *f); 47 | 48 | // Gets the current log interface. 49 | extern u8 logGetInterface(void); 50 | 51 | // Gets the name of the current log interface. 52 | extern const char* logGetInterfaceName(void); 53 | 54 | // Sets the current max log level. 55 | extern void logSetMaxLevel(u8 level); 56 | 57 | // Gets the current max log level. 58 | extern u8 logGetMaxLevel(void); 59 | 60 | // Output a message via the currently initialized log interface 61 | extern void logOutput(u8 level, const char *message); 62 | 63 | _LIBSEVEN_EXTERN_C_END 64 | 65 | #endif /* !_LIBSEVEN_UTIL_LOG_H */ 66 | -------------------------------------------------------------------------------- /include/seven/util/math.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_UTIL_MATH_H 8 | #define _LIBSEVEN_UTIL_MATH_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #error "seven/util/math.h is a stub header" 15 | 16 | _LIBSEVEN_EXTERN_C_END 17 | 18 | #endif /* !_LIBSEVEN_UTIL_MATH_H */ 19 | -------------------------------------------------------------------------------- /include/seven/util/memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_UTIL_MEMORY_H 8 | #define _LIBSEVEN_UTIL_MEMORY_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #error "seven/util/memory.h is a stub header" 15 | 16 | _LIBSEVEN_EXTERN_C_END 17 | 18 | #endif /* !_LIBSEVEN_UTIL_MEMORY_H */ 19 | -------------------------------------------------------------------------------- /include/seven/util/overlay.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_UTIL_OVERLAY_H 8 | #define _LIBSEVEN_UTIL_OVERLAY_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #error "seven/util/overlay.h is a stub header" 15 | 16 | _LIBSEVEN_EXTERN_C_END 17 | 18 | #endif /* !_LIBSEVEN_UTIL_OVERLAY_H */ 19 | -------------------------------------------------------------------------------- /include/seven/util/profile.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_UTIL_PROFILE_H 8 | #define _LIBSEVEN_UTIL_PROFILE_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | // Measures how many CPU cycles it takes to execute the given function, 15 | // using timers 2 and 3. 16 | // 17 | // If the function returns a value in r0, and return_value is not NULL, 18 | // the value will be stored in return_value. 19 | extern u32 profileRun(u32 (*function)(void), u32 *return_value); 20 | 21 | _LIBSEVEN_EXTERN_C_END 22 | 23 | #endif /* !_LIBSEVEN_UTIL_PROFILE_H */ 24 | -------------------------------------------------------------------------------- /include/seven/util/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_UTIL_RANDOM_H 8 | #define _LIBSEVEN_UTIL_RANDOM_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | extern void randSetSeed(u32 seed); 15 | 16 | extern u32 randNext(void); 17 | 18 | _LIBSEVEN_EXTERN_C_END 19 | 20 | #endif /* !_LIBSEVEN_UTIL_RANDOM_H */ 21 | -------------------------------------------------------------------------------- /include/seven/util/simd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_UTIL_SIMD_H 8 | #define _LIBSEVEN_UTIL_SIMD_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | enum SIMDMask 15 | { 16 | SIMD_MASK_4X8 = 0x80808080, // 4 Bytes packed 17 | SIMD_MASK_2X16 = 0x80008000, // 2 Halfwords packed 18 | SIMD_MASK_2XRGB = 0xC210C210, // 2 RGB5 colors packed 19 | }; 20 | 21 | extern void simdAdd(u32 mask, void *lhs, const void *rhs, usize len); 22 | extern void simdSub(u32 mask, void *lhs, const void *rhs, usize len); 23 | 24 | _LIBSEVEN_EXTERN_C_END 25 | 26 | #endif /* !_LIBSEVEN_UTIL_SIMD_H */ 27 | -------------------------------------------------------------------------------- /include/seven/util/string.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_UTIL_STRING_H 8 | #define _LIBSEVEN_UTIL_STRING_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | #error "seven/util/string.h is a stub header" 15 | 16 | _LIBSEVEN_EXTERN_C_END 17 | 18 | #endif /* !_LIBSEVEN_UTIL_STRING_H */ 19 | -------------------------------------------------------------------------------- /include/seven/video/bg_affine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_BG_AFFINE_H 8 | #define _LIBSEVEN_VIDEO_BG_AFFINE_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | _LIBSEVEN_EXTERN_C 16 | 17 | _LIBSEVEN_EXTERN_C_END 18 | 19 | #endif /* !_LIBSEVEN_VIDEO_BG_AFFINE_H */ 20 | -------------------------------------------------------------------------------- /include/seven/video/bg_bitmap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_BG_BITMAP_H 8 | #define _LIBSEVEN_VIDEO_BG_BITMAP_H 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | _LIBSEVEN_EXTERN_C 15 | 16 | #define MODE3_WIDTH 240 17 | #define MODE3_HEIGHT 160 18 | #define MODE3_FRAME MEMADDR(MEM_VRAM, Mode3Frame) 19 | 20 | #define MODE4_WIDTH 240 21 | #define MODE4_HEIGHT 160 22 | #define MODE4_FRAME_0 MEMADDR(MEM_VRAM, Mode4Frame) 23 | #define MODE4_FRAME_1 MEMADDR(MEM_VRAM + 0xA000, Mode4Frame) 24 | 25 | #define M4PX(l, h) ((u16)((l) << 8 | (u8)(h))) 26 | 27 | #define MODE5_WIDTH 160 28 | #define MODE5_HEIGHT 128 29 | #define MODE5_FRAME_0 MEMADDR(MEM_VRAM, Mode5Frame) 30 | #define MODE5_FRAME_1 MEMADDR(MEM_VRAM + 0xA000, Mode5Frame) 31 | 32 | typedef u16 Mode3Frame[MODE3_HEIGHT][MODE3_WIDTH]; 33 | typedef u16 Mode4Frame[MODE4_HEIGHT][MODE4_WIDTH/2]; 34 | typedef u16 Mode5Frame[MODE5_HEIGHT][MODE5_HEIGHT]; 35 | 36 | extern void* bmpInitMode3(void); 37 | extern void* bmpInitMode4(void); 38 | extern void* bmpInitMode5(void); 39 | extern void* bmpSwapBuffers(void); 40 | 41 | _LIBSEVEN_EXTERN_C_END 42 | 43 | #endif /* !_LIBSEVEN_VIDEO_BG_BITMAP_H */ 44 | -------------------------------------------------------------------------------- /include/seven/video/bg_mixed.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_BG_MIXED_H 8 | #define _LIBSEVEN_VIDEO_BG_MIXED_H 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #endif /* !_LIBSEVEN_VIDEO_BG_MIXED_H */ 15 | -------------------------------------------------------------------------------- /include/seven/video/bg_regular.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_BG_REGULAR_H 8 | #define _LIBSEVEN_VIDEO_BG_REGULAR_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | _LIBSEVEN_EXTERN_C 16 | 17 | #define BF_TILE_NUMBER_OFFSET 0 18 | #define BF_TILE_NUMBER_LENGTH 10 19 | 20 | #define TILE_NUMBER(n) BITFIELD(TILE_NUMBER, (n)) 21 | 22 | #define BF_TILE_PALETTE_OFFSET 12 23 | #define BF_TILE_PALETTE_LENGTH 4 24 | 25 | #define TILE_PALETTE(n) BITFIELD(TILE_PALETTE, (n)) 26 | 27 | enum Tile 28 | { 29 | TILE_FLIP_H = BIT(10), 30 | TILE_FLIP_V = BIT(11), 31 | }; 32 | 33 | _LIBSEVEN_EXTERN_C_END 34 | 35 | #endif /* !_LIBSEVEN_VIDEO_BG_REGULAR_H */ 36 | -------------------------------------------------------------------------------- /include/seven/video/bg_scroll.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_BG_SCROLL_H 8 | #define _LIBSEVEN_VIDEO_BG_SCROLL_H 9 | 10 | #include 11 | #include 12 | 13 | _LIBSEVEN_EXTERN_C 14 | 15 | #define REG_BG0HOFS VOLADDR(0x04000010, u16) 16 | #define REG_BG0VOFS VOLADDR(0x04000012, u16) 17 | #define REG_BG1HOFS VOLADDR(0x04000014, u16) 18 | #define REG_BG1VOFS VOLADDR(0x04000016, u16) 19 | #define REG_BG2HOFS VOLADDR(0x04000018, u16) 20 | #define REG_BG2VOFS VOLADDR(0x0400001A, u16) 21 | #define REG_BG3HOFS VOLADDR(0x0400001C, u16) 22 | #define REG_BG3VOFS VOLADDR(0x0400001E, u16) 23 | 24 | _LIBSEVEN_EXTERN_C_END 25 | 26 | #endif /* !_LIBSEVEN_VIDEO_BG_SCROLL_H */ 27 | -------------------------------------------------------------------------------- /include/seven/video/bg_tiled.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_BG_TILED_H 8 | #define _LIBSEVEN_VIDEO_BG_TILED_H 9 | 10 | #include 11 | #include 12 | 13 | _LIBSEVEN_EXTERN_C 14 | 15 | _LIBSEVEN_EXTERN_C_END 16 | 17 | #endif /* !_LIBSEVEN_VIDEO_BG_TILED_H */ 18 | -------------------------------------------------------------------------------- /include/seven/video/bg_transform.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_BG_TRANSFORM_H 8 | #define _LIBSEVEN_VIDEO_BG_TRANSFORM_H 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | _LIBSEVEN_EXTERN_C 15 | 16 | #define REG_BG2PA VOLADDR(0x04000020, i16) 17 | #define REG_BG2PB VOLADDR(0x04000022, i16) 18 | #define REG_BG2PC VOLADDR(0x04000024, i16) 19 | #define REG_BG2PD VOLADDR(0x04000026, i16) 20 | 21 | #define REG_BG2MAT VOLADDR(0x04000020, struct Matrix) 22 | 23 | #define REG_BG2X VOLADDR(0x04000028, i32) 24 | #define REG_BG2Y VOLADDR(0x0400002C, i32) 25 | 26 | #define REG_BG3PA VOLADDR(0x04000030, i16) 27 | #define REG_BG3PB VOLADDR(0x04000032, i16) 28 | #define REG_BG3PC VOLADDR(0x04000034, i16) 29 | #define REG_BG3PD VOLADDR(0x04000036, i16) 30 | 31 | #define REG_BG3MAT VOLADDR(0x04000030, struct Matrix) 32 | 33 | #define REG_BG3X VOLADDR(0x04000038, i32) 34 | #define REG_BG3Y VOLADDR(0x0400003C, i32) 35 | 36 | _LIBSEVEN_EXTERN_C_END 37 | 38 | #endif /* !_LIBSEVEN_VIDEO_BG_TRANSFORM_H */ 39 | -------------------------------------------------------------------------------- /include/seven/video/blend.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_BLEND_H 8 | #define _LIBSEVEN_VIDEO_BLEND_H 9 | 10 | #include 11 | #include 12 | 13 | _LIBSEVEN_EXTERN_C 14 | 15 | #define REG_BLDCNT VOLADDR(0x04000050, u16) 16 | #define REG_BLDALPHA VOLADDR(0x04000052, u16) 17 | #define REG_BLDVAL VOLADDR(0x04000054, u16) 18 | 19 | #define BF_BLEND_MODE_OFFSET 6 20 | #define BF_BLEND_MODE_LENGTH 2 21 | 22 | #define BLEND_MODE(n) BITFIELD(BLEND_MODE, (n)) 23 | 24 | enum BlendControl 25 | { 26 | BLEND_TARGET_BG0 = BIT(0), 27 | BLEND_TARGET_BG1 = BIT(1), 28 | BLEND_TARGET_BG2 = BIT(2), 29 | BLEND_TARGET_BG3 = BIT(3), 30 | BLEND_TARGET_OBJ = BIT(4), 31 | BLEND_TARGET_BD = BIT(5), 32 | 33 | BLEND_MODE_NONE = BLEND_MODE(0), 34 | BLEND_MODE_ALPHA = BLEND_MODE(1), 35 | BLEND_MODE_WHITE = BLEND_MODE(2), 36 | BLEND_MODE_BLACK = BLEND_MODE(3), 37 | 38 | // Only used with BLEND_MODE_ALPHA 39 | BLEND_TARGET2_BG0 = BIT(8), 40 | BLEND_TARGET2_BG1 = BIT(9), 41 | BLEND_TARGET2_BG2 = BIT(10), 42 | BLEND_TARGET2_BG3 = BIT(11), 43 | BLEND_TARGET2_OBJ = BIT(12), 44 | BLEND_TARGET2_BD = BIT(13), 45 | }; 46 | 47 | _LIBSEVEN_EXTERN_C_END 48 | 49 | #endif /* !_LIBSEVEN_VIDEO_BLEND_H */ 50 | -------------------------------------------------------------------------------- /include/seven/video/color.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_COLOR_H 8 | #define _LIBSEVEN_VIDEO_COLOR_H 9 | 10 | #include 11 | #include 12 | 13 | _LIBSEVEN_EXTERN_C 14 | 15 | #define BF_COLOR_R_OFFSET 0 16 | #define BF_COLOR_R_LENGTH 5 17 | 18 | #define COLOR_R(n) BITFIELD(COLOR_R, (n)) 19 | 20 | #define BF_COLOR_G_OFFSET 5 21 | #define BF_COLOR_G_LENGTH 5 22 | 23 | #define COLOR_G(n) BITFIELD(COLOR_G, (n)) 24 | 25 | #define BF_COLOR_B_OFFSET 10 26 | #define BF_COLOR_B_LENGTH 5 27 | 28 | #define COLOR_B(n) BITFIELD(COLOR_B, (n)) 29 | 30 | #define RGB5(r, g, b) \ 31 | ((u16)(COLOR_R((r)) | COLOR_G((g)) | COLOR_B((b)))) 32 | 33 | #define RGB8(r, g, b) \ 34 | RGB5((r) >> 3, (g) >> 3, (b) >> 3) 35 | 36 | #define COLOR_RED RGB5(31, 0, 0) 37 | #define COLOR_RED_ORANGE RGB5(31, 8, 0) 38 | #define COLOR_ORANGE RGB5(31, 15, 0) 39 | #define COLOR_ORANGE_YELLOW RGB5(31, 23, 0) 40 | #define COLOR_YELLOW RGB5(31, 31, 0) 41 | #define COLOR_YELLOW_LIME RGB5(23, 31, 0) 42 | #define COLOR_LIME RGB5(15, 31, 0) 43 | #define COLOR_LIME_GREEN RGB5( 8, 31, 0) 44 | #define COLOR_GREEN RGB5( 0, 31, 0) 45 | #define COLOR_GREEN_MINT RGB5( 0, 31, 8) 46 | #define COLOR_MINT RGB5( 0, 31, 15) 47 | #define COLOR_MINT_CYAN RGB5( 0, 31, 23) 48 | #define COLOR_CYAN RGB5( 0, 31, 31) 49 | #define COLOR_CYAN_SKYBLUE RGB5( 0, 23, 31) 50 | #define COLOR_SKYBLUE RGB5( 0, 15, 31) 51 | #define COLOR_SKYBLUE_BLUE RGB5( 0, 8, 31) 52 | #define COLOR_BLUE RGB5( 0, 0, 31) 53 | #define COLOR_BLUE_PURPLE RGB5( 8, 0, 31) 54 | #define COLOR_PURPLE RGB5(15, 0, 31) 55 | #define COLOR_PURPLE_MAGENTA RGB5(23, 0, 31) 56 | #define COLOR_MAGENTA RGB5(31, 0, 31) 57 | #define COLOR_MAGENTA_PINK RGB5(31, 0, 23) 58 | #define COLOR_PINK RGB5(31, 0, 15) 59 | #define COLOR_PINK_RED RGB5(31, 0, 8) 60 | 61 | #define COLOR_WHITE RGB5(31, 31, 31) 62 | #define COLOR_BLACK RGB5( 0, 0, 0) 63 | 64 | #define COLOR_GRAY_100 RGB5(31, 31, 31) 65 | #define COLOR_GRAY_95 RGB5(29, 29, 29) 66 | #define COLOR_GRAY_90 RGB5(28, 28, 28) 67 | #define COLOR_GRAY_85 RGB5(26, 26, 26) 68 | #define COLOR_GRAY_80 RGB5(25, 25, 25) 69 | #define COLOR_GRAY_75 RGB5(23, 23, 23) 70 | #define COLOR_GRAY_70 RGB5(22, 22, 22) 71 | #define COLOR_GRAY_65 RGB5(20, 20, 20) 72 | #define COLOR_GRAY_60 RGB5(19, 19, 19) 73 | #define COLOR_GRAY_55 RGB5(17, 17, 17) 74 | #define COLOR_GRAY_50 RGB5(15, 15, 15) 75 | #define COLOR_GRAY_45 RGB5(14, 14, 14) 76 | #define COLOR_GRAY_40 RGB5(12, 12, 12) 77 | #define COLOR_GRAY_35 RGB5(11, 11, 11) 78 | #define COLOR_GRAY_30 RGB5( 9, 9, 9) 79 | #define COLOR_GRAY_25 RGB5( 8, 8, 8) 80 | #define COLOR_GRAY_20 RGB5( 6, 6, 6) 81 | #define COLOR_GRAY_15 RGB5( 5, 5, 5) 82 | #define COLOR_GRAY_10 RGB5( 3, 3, 3) 83 | #define COLOR_GRAY_5 RGB5( 2, 2, 2) 84 | #define COLOR_GRAY_0 RGB5( 0, 0, 0) 85 | 86 | _LIBSEVEN_EXTERN_C_END 87 | 88 | #endif /* !_LIBSEVEN_VIDEO_COLOR_H */ 89 | -------------------------------------------------------------------------------- /include/seven/video/matrix.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_MATRIX_H 8 | #define _LIBSEVEN_VIDEO_MATRIX_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | struct Matrix 15 | { 16 | i16 hdx; 17 | i16 vdx; 18 | i16 hdy; 19 | i16 vdy; 20 | } ALIGN(4); 21 | 22 | _LIBSEVEN_EXTERN_C_END 23 | 24 | #endif /* !_LIBSEVEN_VIDEO_MATRIX_H */ 25 | -------------------------------------------------------------------------------- /include/seven/video/mosaic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_MOSAIC_H 8 | #define _LIBSEVEN_VIDEO_MOSAIC_H 9 | 10 | #include 11 | #include 12 | 13 | _LIBSEVEN_EXTERN_C 14 | 15 | #define REG_MOSAIC VOLADDR(0x0400004C, u16) 16 | 17 | #define BF_MOSAIC_BG_H_OFFSET 0 18 | #define BF_MOSAIC_BG_H_LENGTH 4 19 | 20 | #define MOSAIC_BG_H(v) BITFIELD(MOSAIC_BG_H, (v)) 21 | 22 | #define BF_MOSAIC_BG_V_OFFSET 4 23 | #define BF_MOSAIC_BG_V_LENGTH 4 24 | 25 | #define MOSAIC_BG_V(v) BITFIELD(MOSAIC_BG_V, (v)) 26 | 27 | #define BF_MOSAIC_OBJ_H_OFFSET 8 28 | #define BF_MOSAIC_OBJ_H_LENGTH 4 29 | 30 | #define MOSAIC_OBJ_H(v) BITFIELD(MOSAIC_OBJ_H, (v)) 31 | 32 | #define BF_MOSAIC_OBJ_V_OFFSET 12 33 | #define BF_MOSAIC_OBJ_V_LENGTH 4 34 | 35 | #define MOSAIC_OBJ_V(v) BITFIELD(MOSAIC_OBJ_V, (v)) 36 | 37 | _LIBSEVEN_EXTERN_C_END 38 | 39 | #endif /* !_LIBSEVEN_VIDEO_MOSAIC_H */ 40 | -------------------------------------------------------------------------------- /include/seven/video/oam.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_OAM_H 8 | #define _LIBSEVEN_VIDEO_OAM_H 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | _LIBSEVEN_EXTERN_C 15 | 16 | usize oamWriteObjects(usize oam_index, struct Object *objs, usize n_objs); 17 | usize oamWriteObjectsUnchecked(usize oam_index, struct Object *objs, usize n_objs); 18 | 19 | usize oamWriteMatrices(usize oam_index, struct Matrix *mats, usize n_mats); 20 | usize oamWriteMatricesUnchecked(usize oam_index, struct Matrix *mats, usize n_mats); 21 | 22 | _LIBSEVEN_EXTERN_C_END 23 | 24 | #endif /* !_LIBSEVEN_VIDEO_OAM_H */ 25 | -------------------------------------------------------------------------------- /include/seven/video/object.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_OBJECT_H 8 | #define _LIBSEVEN_VIDEO_OBJECT_H 9 | 10 | #include 11 | #include 12 | 13 | _LIBSEVEN_EXTERN_C 14 | 15 | struct Object 16 | { 17 | u16 attr0; 18 | u16 attr1; 19 | u16 attr2; 20 | u16 prio; 21 | } ALIGN(4); 22 | 23 | enum ObjectAttribute0 24 | { 25 | #define BF_OBJ_Y_POS_OFFSET 0 26 | #define BF_OBJ_Y_POS_LENGTH 8 27 | 28 | #define OBJ_Y_POS(n) BITFIELD(OBJ_Y_POS, n) 29 | 30 | #define BF_OBJ_MODE_OFFSET 8 31 | #define BF_OBJ_MODE_LENGTH 2 32 | 33 | #define OBJ_MODE(n) BITFIELD(OBJ_MODE, n) 34 | 35 | OBJ_MODE_REGULAR = OBJ_MODE(0), 36 | OBJ_MODE_AFFINE = OBJ_MODE(1), 37 | OBJ_MODE_HIDDEN = OBJ_MODE(2), 38 | OBJ_MODE_DOUBLE = OBJ_MODE(3), 39 | 40 | #define BF_OBJ_TYPE_OFFSET 10 41 | #define BF_OBJ_TYPE_LENGTH 2 42 | 43 | #define OBJ_TYPE(n) BITFIELD(OBJ_TYPE, n) 44 | 45 | OBJ_TYPE_REGULAR = OBJ_TYPE(0), 46 | OBJ_TYPE_BLEND = OBJ_TYPE(1), 47 | OBJ_TYPE_WINDOW = OBJ_TYPE(2), 48 | 49 | OBJ_MOSAIC_ENABLE = BIT(12), 50 | 51 | OBJ_TILE_8BPP = BIT(13), 52 | OBJ_TILE_4BPP = !OBJ_TILE_8BPP, 53 | 54 | #define BF_OBJ_SHAPE_OFFSET 14 55 | #define BF_OBJ_SHAPE_LENGTH 2 56 | 57 | #define OBJ_SHAPE(n) BITFIELD(OBJ_SHAPE, n) 58 | 59 | OBJ_SHAPE_SQUARE = OBJ_SHAPE(0), 60 | OBJ_SHAPE_WIDE = OBJ_SHAPE(1), 61 | OBJ_SHAPE_TALL = OBJ_SHAPE(2), 62 | }; 63 | 64 | enum ObjectAttribute1 65 | { 66 | #define BF_OBJ_X_POS_OFFSET 0 67 | #define BF_OBJ_X_POS_LENGTH 9 68 | 69 | #define OBJ_X_POS(n) BITFIELD(OBJ_X_POS, n) 70 | 71 | #define BF_OBJ_AFFINE_MATRIX_OFFSET 9 72 | #define BF_OBJ_AFFINE_MATRIX_LENGTH 5 73 | 74 | #define OBJ_AFFINE_MATRIX(n) BITFIELD(OBJ_AFFINE_MATRIX, n) 75 | 76 | OBJ_FLIP_H = BIT(12), 77 | OBJ_FLIP_V = BIT(13), 78 | 79 | #define BF_OBJ_SIZE_OFFSET 14 80 | #define BF_OBJ_SIZE_LENGTH 2 81 | 82 | #define OBJ_SIZE(n) BITFIELD(OBJ_SIZE, n) 83 | 84 | // Square 85 | OBJ_SIZE_8X8 = OBJ_SIZE(0), 86 | OBJ_SIZE_16X16 = OBJ_SIZE(1), 87 | OBJ_SIZE_32X32 = OBJ_SIZE(2), 88 | OBJ_SIZE_64X64 = OBJ_SIZE(3), 89 | 90 | // Wide 91 | OBJ_SIZE_16X8 = OBJ_SIZE(0), 92 | OBJ_SIZE_32X8 = OBJ_SIZE(1), 93 | OBJ_SIZE_32X16 = OBJ_SIZE(2), 94 | OBJ_SIZE_64X32 = OBJ_SIZE(3), 95 | 96 | // Tall 97 | OBJ_SIZE_8X16 = OBJ_SIZE(0), 98 | OBJ_SIZE_8X32 = OBJ_SIZE(1), 99 | OBJ_SIZE_16X32 = OBJ_SIZE(2), 100 | OBJ_SIZE_32X64 = OBJ_SIZE(3), 101 | }; 102 | 103 | enum ObjectAttribute2 { 104 | #define BF_OBJ_TILE_NUMBER_OFFSET 0 105 | #define BF_OBJ_TILE_NUMBER_LENGTH 10 106 | 107 | #define OBJ_TILE_NUMBER(n) BITFIELD(OBJ_TILE_NUMBER, n) 108 | 109 | #define BF_OBJ_PRIORITY_OFFSET 10 110 | #define BF_OBJ_PRIORITY_LENGTH 2 111 | 112 | #define OBJ_PRIORITY(n) BITFIELD(OBJ_PRIORITY, n) 113 | 114 | OBJ_PRIORITY_MIN = OBJ_PRIORITY(3), 115 | OBJ_PROIRITY_MAX = OBJ_PRIORITY(0), 116 | 117 | #define BF_OBJ_PALETTE_NUMBER_OFFSET 12 118 | #define BF_OBJ_PALETTE_NUMBER_LENGTH 4 119 | 120 | #define OBJ_PALETTE_NUMBER(n) BITFIELD(OBJ_PALETTE_NUMBER, n) 121 | }; 122 | 123 | _LIBSEVEN_EXTERN_C_END 124 | 125 | #endif /* !_LIBSEVEN_VIDEO_OBJECT_H */ 126 | -------------------------------------------------------------------------------- /include/seven/video/palette.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_PALETTE_H 8 | #define _LIBSEVEN_VIDEO_PALETTE_H 9 | 10 | #include 11 | #include 12 | 13 | _LIBSEVEN_EXTERN_C 14 | 15 | #error "seven/video/palette.h is a stub header" 16 | 17 | _LIBSEVEN_EXTERN_C_END 18 | 19 | #endif /* !_LIBSEVEN_VIDEO_PALETTE_H */ 20 | -------------------------------------------------------------------------------- /include/seven/video/prelude.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_PRELUDE_H 8 | #define _LIBSEVEN_VIDEO_PRELUDE_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #endif /* !_LIBSEVEN_VIDEO_PRELUDE_H */ 16 | -------------------------------------------------------------------------------- /include/seven/video/window.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_VIDEO_WINDOW_H 8 | #define _LIBSEVEN_VIDEO_WINDOW_H 9 | 10 | #include 11 | #include 12 | 13 | _LIBSEVEN_EXTERN_C 14 | 15 | #define REG_WIN0H VOLADDR(0x04000040, u16) 16 | #define REG_WIN1H VOLADDR(0x04000042, u16) 17 | #define REG_WIN0V VOLADDR(0x04000044, u16) 18 | #define REG_WIN1V VOLADDR(0x04000046, u16) 19 | 20 | #define REG_WIN0IN VOLADDR(0x04000048, u8) 21 | #define REG_WIN1IN VOLADDR(0x04000049, u8) 22 | #define REG_WIN0OUT VOLADDR(0x0400004A, u8) 23 | #define REG_WIN1OUT VOLADDR(0x0400004B, u8) 24 | 25 | enum WindowControl 26 | { 27 | WINDOW_BG0_ENABLE = BIT(0), 28 | WINDOW_BG1_ENABLE = BIT(1), 29 | WINDOW_BG2_ENABLE = BIT(2), 30 | WINDOW_BG3_ENABLE = BIT(3), 31 | WINDOW_OBJ_ENABLE = BIT(4), 32 | WINDOW_BLEND_ENABLE = BIT(5), 33 | }; 34 | 35 | #define WINDOW_DIM(l, h) ((((l) & 255) << 8) | ((h) & 255)) 36 | 37 | _LIBSEVEN_EXTERN_C_END 38 | 39 | #endif /* !_LIBSEVEN_VIDEO_WINDOW_H */ 40 | -------------------------------------------------------------------------------- /meson.build: -------------------------------------------------------------------------------- 1 | project( 2 | 'libseven', 'c', 3 | version: '0.6.0', 4 | license: 'MPL-2.0', 5 | default_options: ['c_std=c99', 'c_link_args=-Wl,--no-warn-rwx-segments']) 6 | 7 | subproject('minrt') 8 | 9 | sources = [ 10 | 'src/hw/dma.s', 11 | 'src/hw/input.s', 12 | 'src/hw/irq.s', 13 | 'src/hw/sram.s', 14 | 'src/hw/svc.s', 15 | 'src/hw/timer.s', 16 | 'src/util/debug.s', 17 | 'src/util/log.c', 18 | 'src/util/mem.s', 19 | 'src/util/profile.s', 20 | 'src/util/rand.s', 21 | 'src/util/simd.s', 22 | 'src/util/str.s', 23 | 'src/video/bmp.s', 24 | 'src/video/oam.s', 25 | ] 26 | 27 | libseven = static_library( 28 | 'seven', 29 | sources, 30 | include_directories: ['include', 'src'], 31 | c_args: ['-ffunction-sections', '-fdata-sections', '-ffreestanding']) 32 | 33 | libseven_dep = declare_dependency( 34 | include_directories: ['include'], 35 | link_with: libseven) 36 | 37 | meson.override_dependency('libseven', libseven_dep) 38 | 39 | subdir('examples') 40 | -------------------------------------------------------------------------------- /meta/header.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #ifndef _LIBSEVEN_TEMPLATE_H 8 | #define _LIBSEVEN_TEMPLATE_H 9 | 10 | #include 11 | 12 | _LIBSEVEN_EXTERN_C 13 | 14 | _LIBSEVEN_EXTERN_C_END 15 | 16 | #endif /* !_LIBSEVEN_TEMPLATE_H */ 17 | -------------------------------------------------------------------------------- /meta/include.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .ifndef _LIBSEVEN_ASM_TEMPLATE_S 8 | .equiv _LIBSEVEN_ASM_TEMPLATE_S, 1 9 | 10 | .include "seven/asm/base.s" 11 | 12 | .endif @ !_LIBSEVEN_ASM_TEMPLATE_S 13 | 14 | @ vim: ft=armv4 et sta sw=4 sts=8 15 | -------------------------------------------------------------------------------- /meta/source.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | 12 | @ vim: ft=armv4 et sta sw=4 sts=8 13 | -------------------------------------------------------------------------------- /src/hw/dma.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | .include "seven/asm/hw/dma.s" 12 | 13 | CONST SOUND_FIFO_A, 0x040000A0 14 | CONST SOUND_FIFO_B, 0x040000A4 15 | 16 | @ void dmaEnable(u32 num) 17 | @ 18 | @ r0 num 19 | fn dmaEnable thumb 20 | cmp r0, #3 21 | bgt .Leout 22 | 23 | @ * 8 24 | lsls r1, r0, #3 25 | @ * 4 26 | lsls r0, r0, #2 27 | @ = * 12 28 | adds r0, r0, r1 29 | 30 | @ Read DMA0CNT 31 | ldr r1, =REG_DMA0CNT 32 | ldrh r2, [r1, r0] 33 | 34 | @ Add DMA_ENABLE 35 | movs r3, #0x80 36 | lsls r3, r3, #8 37 | orrs r2, r2, r3 38 | 39 | strh r2, [r1, r0] 40 | .Leout: 41 | bx lr 42 | endfn 43 | 44 | @ void dmaDisable(u32 num) 45 | @ 46 | @ r0 num 47 | fn dmaDisable thumb 48 | cmp r0, #3 49 | 50 | bgt .Ldout 51 | @ * 8 52 | lsls r1, r0, #3 53 | @ * 4 54 | lsls r0, r0, #2 55 | @ = * 12 56 | adds r0, r0, r1 57 | 58 | @ Read DMA0CNT 59 | ldr r1, =REG_DMA0CNT 60 | ldrh r2, [r1, r0] 61 | 62 | @ Remove DMA_ENABLE 63 | movs r3, #0x80 64 | lsls r3, r3, #8 65 | bics r2, r2, r3 66 | 67 | strh r2, [r1, r0] 68 | .Ldout: 69 | bx lr 70 | endfn 71 | 72 | @ void dmaCopy16(const void *src, void *dst, u32 len) 73 | @ 74 | @ r0 src 75 | @ r1 dst 76 | @ r2 len 77 | fn dmaCopy16 thumb 78 | @ DMA_ENABLE 79 | movs r3, #0x80 80 | lsls r3, r3, #24 81 | @ len = (len / 2) & 0xFFFF 82 | lsls r2, r2, #15 83 | lsrs r2, r2, #16 84 | orrs r2, r2, r3 85 | ldr r3, =REG_DMA3SRC 86 | stm r3!, {r0, r1, r2} 87 | bx lr 88 | endfn 89 | 90 | @ void dmaCopy32(const void *src, void *dst, u32 len) 91 | @ 92 | @ r0 src 93 | @ r1 dst 94 | @ r2 len 95 | fn dmaCopy32 thumb 96 | @ DMA_ENABLE | DMA_32BIT 97 | movs r3, #0x84 98 | lsls r3, r3, #24 99 | @ len = (len / 4) & 0xFFFF 100 | lsls r2, r2, #14 101 | lsrs r2, r2, #16 102 | orrs r2, r2, r3 103 | ldr r3, =REG_DMA3SRC 104 | stm r3!, {r0, r1, r2} 105 | bx lr 106 | endfn 107 | 108 | @ void dmaSoundFifoATransfer(const void *src) 109 | @ 110 | @ r0 src 111 | fn dmaSoundFifoATransfer thumb 112 | lsls r3, r1, #16 113 | ldr r1, =SOUND_FIFO_A 114 | @ DMA_REPEAT | DMA_START_SOUND 115 | movs r2, #0x32 116 | lsls r2, r2, #24 117 | orrs r2, r2, r3 118 | ldr r3, =REG_DMA1SRC 119 | stm r3!, {r0, r1, r2} 120 | bx lr 121 | endfn 122 | 123 | @ void dmaSoundFifoBTransfer(const void *src) 124 | @ 125 | @ r0 src 126 | fn dmaSoundFifoBTransfer thumb 127 | lsls r3, r1, #16 128 | ldr r1, =SOUND_FIFO_B 129 | movs r2, #0x32 130 | lsls r2, r2, #24 131 | orrs r2, r2, r3 132 | ldr r3, =REG_DMA2SRC 133 | stm r3!, {r0, r1, r2} 134 | bx lr 135 | endfn 136 | 137 | @ void dmaHBlankTransfer(const void *src, void *dst, u32 len, u16 flags); 138 | @ 139 | @ r0 src 140 | @ r1 dst 141 | @ r2 len 142 | @ r3 flags 143 | fn dmaHBlankTransfer thumb 144 | @ Clear DMA_START flags 145 | mov r12, r4 146 | movs r4, #0x30 147 | lsls r4, r4, #8 148 | bics r3, r3, r4 149 | @ Add DMA_START_HBLANK | DMA_REPEAT 150 | movs r4, #0x22 151 | lsls r4, r4, #8 152 | orrs r3, r3, r4 153 | @ Check for DMA_32BIT and adjust byte count 154 | lsrs r4, r3, #11 155 | mov r4, r12 156 | bcs 1f 157 | lsls r2, r2, #1 158 | 1: 159 | lsls r2, r2, #14 160 | lsrs r2, r2, #16 161 | 162 | lsls r3, r3, #16 163 | orrs r2, r2, r3 164 | ldr r3, =REG_DMA0SRC 165 | stm r3!, {r0, r1, r2} 166 | bx lr 167 | endfn 168 | 169 | @ void dmaAtomicSet(u32 num, const void *src, void *dst, u32 len, u16 flags); 170 | @ 171 | @ r0 num 172 | @ r1 src 173 | @ r2 dst 174 | @ r3 len 175 | @ [sp] flags 176 | fn dmaAtomicSet thumb 177 | cmp r0, #3 178 | bgt .Lsout 179 | push {r4, r5} 180 | 181 | @ num *= 12 182 | lsls r4, r0, #3 183 | lsls r0, r0, #2 184 | adds r0, r0, r4 185 | 186 | @ Final Address 187 | ldr r4, =REG_DMA0SRC 188 | adds r0, r0, r4 189 | 190 | @ Load flags 191 | ldr r4, [sp] 192 | 193 | @ Adjust byte count 194 | lsrs r5, r4, #11 195 | bcs 1f 196 | lsls r3, r3, #1 197 | 1: 198 | lsls r3, r3, #14 199 | lsrs r3, r3, #16 200 | 201 | @ Merge Length and Flags 202 | lsls r4, r4, #16 203 | orrs r3, r3, r4 204 | 205 | stm r0!, {r1, r2, r3} 206 | 207 | pop {r4, r5} 208 | .Lsout: 209 | bx lr 210 | endfn 211 | 212 | @ vim: ft=armv4 et sta sw=4 sts=8 213 | -------------------------------------------------------------------------------- /src/hw/input.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | .include "seven/asm/hw/input.s" 12 | 13 | bss KEYINPUT 14 | .word 0 15 | endb 16 | 17 | fn inputPoll thumb 18 | @ ~REG_KEYINPUT & 0x3FFF 19 | ldr r0, =REG_KEYINPUT 20 | ldrh r0, [r0] 21 | mvns r0, r0 22 | lsls r0, r0, #22 23 | lsrs r0, r0, #22 24 | ldr r1, =KEYINPUT 25 | ldr r2, [r1] 26 | lsls r2, r2, #16 27 | orrs r2, r2, r0 28 | str r2, [r1] 29 | bx lr 30 | endfn 31 | 32 | fn inputState thumb 33 | ldr r0, =KEYINPUT 34 | ldr r0, [r0] 35 | bx lr 36 | endfn 37 | 38 | fn inputKeysPressed thumb 39 | ldr r1, =KEYINPUT 40 | ldr r1, [r1] 41 | lsrs r2, r1, #16 42 | bics r1, r1, r2 43 | ands r0, r0, r1 44 | bx lr 45 | endfn 46 | 47 | fn inputKeysReleased thumb 48 | ldr r1, =KEYINPUT 49 | ldr r1, [r1] 50 | lsrs r2, r1, #16 51 | bics r2, r2, r1 52 | ands r0, r0, r2 53 | bx lr 54 | endfn 55 | 56 | fn inputKeysDown thumb 57 | ldr r1, =KEYINPUT 58 | ldrh r1, [r1] 59 | ands r0, r0, r1 60 | bx lr 61 | endfn 62 | 63 | fn inputKeysUp thumb 64 | ldr r1, =KEYINPUT 65 | ldrh r1, [r1] 66 | bics r0, r0, r1 67 | bx lr 68 | endfn 69 | 70 | fn inputAxisX thumb 71 | ldr r1, =KEYINPUT 72 | ldrh r1, [r1] 73 | movs r0, #0 74 | lsls r1, r1, #31 - KEY_INDEX_LEFT 75 | @ C = RIGHT, r1 = -1 if LEFT, else 0 76 | asrs r1, r1, #31 77 | @ 0 - LEFT + RIGHT 78 | adcs r0, r0, r1 79 | bx lr 80 | endfn 81 | 82 | fn inputAxisY thumb 83 | ldr r1, =KEYINPUT 84 | ldrh r1, [r1] 85 | movs r0, #0 86 | @ C = UP, r1 = -1 if DOWN, else 0 87 | lsls r1, r1, #31 - KEY_INDEX_DOWN 88 | asrs r1, r1, #31 89 | @ r0 = 0 + -DOWN + UP 90 | adcs r0, r0, r1 91 | negs r0, r0 92 | bx lr 93 | endfn 94 | 95 | fn inputAxisLR thumb 96 | ldr r1, =KEYINPUT 97 | ldrh r1, [r1] 98 | movs r0, #0 99 | @ C = R, r1 = -1 if L, else 0 100 | lsls r1, r1, #31 - KEY_INDEX_L 101 | asrs r1, r1, #31 102 | @ r0 = 0 + -L + R 103 | adcs r0, r0, r1 104 | bx lr 105 | endfn 106 | 107 | fn inputAxisAB thumb 108 | ldr r1, =KEYINPUT 109 | ldrh r1, [r1] 110 | movs r0, #0 111 | @ C = A, r1 = -1 if B, else 0 112 | lsls r1, r1, #31 - KEY_INDEX_B 113 | asrs r1, r1, #31 114 | @ r0 = 0 + -B + A 115 | adcs r0, r0, r1 116 | bx lr 117 | endfn 118 | 119 | @ vim: ft=armv4 et sta sw=4 sts=8 120 | -------------------------------------------------------------------------------- /src/hw/sram.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | 12 | @ TODO: Make all functions return a byte count 13 | 14 | fn sramReadCore arm local 15 | ldrb r3, [r2], #1 16 | strb r3, [r0], #1 17 | cmp r2, r1 18 | bne sramReadCore 19 | bx lr 20 | endfn 21 | 22 | fn sramRead64 thumb 23 | movs r2, #0 24 | fn sramReadAt64 25 | movs r3, #64 26 | lsls r3, r3, #10 27 | b .Lsra_main 28 | fn sramRead 29 | movs r2, #0 30 | fn sramReadAt 31 | movs r3, #32 32 | lsls r3, r3, #10 33 | .Lsra_main: 34 | @ Length check 35 | cmp r1, #0 36 | beq .Lsra_ret 37 | @ Overflow check 38 | adds r1, r1, r2 39 | bcs .Lsra_ret 40 | @ Bounds check 41 | cmp r1, r3 42 | bgt .Lsra_ret 43 | 44 | @ r1 = sram_end 45 | @ r2 = sram_start 46 | movs r3, #0xE 47 | lsls r3, r3, #24 48 | 49 | adds r1, r1, r3 50 | adds r2, r2, r3 51 | 52 | ldr r3, =sramReadCore 53 | @ Tailcall 54 | bx r3 55 | .Lsra_ret: 56 | bx lr 57 | endfn 58 | 59 | @ void sramWriteAt(const void *src, u32 len, u32 off); 60 | fn sramWrite64 thumb 61 | movs r2, #0 62 | fn sramWriteAt64 63 | movs r3, #64 64 | lsls r3, r3, #10 65 | b .Lswa_main 66 | fn sramWrite 67 | movs r2, #0 68 | fn sramWriteAt 69 | movs r3, #64 70 | lsls r3, r3, #10 71 | .Lswa_main: 72 | @ Length check 73 | cmp r1, #0 74 | beq .Lswa_ret 75 | @ Overflow check 76 | adds r1, r1, r2 77 | bcs .Lswa_ret 78 | @ Bounds check 79 | cmp r1, r3 80 | bgt .Lswa_ret 81 | 82 | @ r1 = sram_end 83 | @ r2 = sram_start 84 | movs r3, #0xE 85 | lsls r3, r3, #24 86 | adds r1, r1, r3 87 | adds r2, r2, r3 88 | 89 | .Lswa_loop: 90 | ldrb r3, [r0] 91 | strb r3, [r2] 92 | adds r0, r0, #1 93 | adds r2, r2, #1 94 | cmp r2, r1 95 | bne .Lswa_loop 96 | 97 | .Lswa_ret: 98 | bx lr 99 | endfn 100 | 101 | @ src, offset, sram/temp2, counter, temp 102 | 103 | @ rX = src 104 | @ rY = sram 105 | 106 | @ rT = temp, end addr 107 | @ rU = temp2 108 | 109 | @ r0 = src 110 | @ r2 = sram_start 111 | @ r1 = length 112 | 113 | @ r3, r12 = temp 114 | @1: 115 | @ldrb temp1, [lhs, counter] 116 | @ldrb temp2, [rhs, counter] 117 | @cmp temp1, temp2 118 | @bxeq lr 119 | @add counter, #1 120 | @cmp counter, max 121 | @bne 1b 122 | 123 | 124 | fn sramCompareCore arm local 125 | .Lscc_loop: 126 | ldrb r3, [r0] 127 | ldrb r12, [r2] 128 | cmp r3, r12 129 | @ branch 130 | add r0, r0, #1 131 | add r2, r2, #1 132 | 133 | bx lr 134 | endfn 135 | 136 | @ void sramCompareAt(const void *src, u32 len, u32 off); 137 | fn sramCompare64 thumb 138 | movs r2, #0 139 | fn sramCompareAt64 140 | movs r3, #64 141 | lsls r3, r3, #10 142 | b .Lswc_main 143 | fn sramCompare 144 | movs r2, #0 145 | fn sramCompareAt 146 | movs r3, #64 147 | lsls r3, r3, #10 148 | .Lswc_main: 149 | @ Length check 150 | cmp r1, #0 151 | beq .Lswc_ret 152 | @ Overflow check 153 | adds r1, r1, r2 154 | bcs .Lswc_ret 155 | @ Bounds check 156 | cmp r1, r3 157 | bgt .Lswc_ret 158 | subs r1, r1, r2 159 | 160 | @ r0 = src 161 | @ r1 = sram_end 162 | @ r2 = sram_addr 163 | movs r3, #0xE 164 | lsls r3, r3, #24 165 | adds r1, r1, r3 166 | adds r2, r2, r3 167 | ldr r3, =sramCompareCore 168 | @ Tail call 169 | bx r3 170 | .Lswc_ret: 171 | bx lr 172 | endfn 173 | 174 | fn sramClear64 thumb 175 | movs r1, #0 176 | fn sramClearAt64 177 | movs r3, #64 178 | lsls r3, r3, #10 179 | b .Lsza_main 180 | fn sramClear 181 | movs r1, #0 182 | fn sramClearAt 183 | movs r3, #32 184 | lsls r3, r3, #10 185 | @ Length check 186 | cmp r0, #0 187 | beq .Lsza_oob 188 | @ Overflow check 189 | adds r2, r0, r1 190 | bcs .Lsza_oob 191 | @ Bounds check 192 | cmp r2, r3 193 | bgt .Lsza_oob 194 | 195 | @ r1 += MEM_SRAM 196 | movs r3, #0xE 197 | lsls r3, r3, #24 198 | adds r1, r1, r3 199 | 200 | @ r0 = len 201 | @ r1 = *dst 202 | @ r2 = 0 203 | @ r3 = count 204 | 205 | @ loop 206 | movs r2, #0 207 | movs r3, #0 208 | .Lsza_loop: 209 | strb r2, [r1, r3] 210 | adds r3, r3, #1 211 | cmp r3, r0 212 | bne .Lsza_loop 213 | 214 | .Lsza_oob: 215 | bx lr 216 | endfn 217 | 218 | @ vim: ft=armv4 et sta sw=4 sts=8 219 | -------------------------------------------------------------------------------- /src/hw/svc.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | .include "seven/asm/hw/svc.s" 12 | 13 | .macro svc_impl name:req num:req noreturn 14 | fn \name thumb 15 | svc #\num 16 | .ifnc \noreturn,noreturn 17 | bx lr 18 | .endif 19 | endfn 20 | .endm 21 | 22 | @ svc_impl svcSoftReset 0 noreturn 23 | svc_impl svcRegisterRamReset 1 24 | svc_impl svcHalt 2 25 | svc_impl svcStop 3 26 | svc_impl svcIntrWait 4 27 | svc_impl svcVBlankIntrWait 5 28 | svc_impl svcSqrt 8 29 | svc_impl svcArcTan 9 30 | svc_impl svcArcTan2 10 31 | svc_impl svcCpuSet 11 32 | svc_impl svcCpuFastSet 12 33 | svc_impl svcBiosChecksum 13 34 | svc_impl svcBgAffineSet 14 35 | svc_impl svcObjAffineSet 15 36 | svc_impl svcBitUnPack 16 37 | svc_impl svcLZ77UnCompWram 17 38 | svc_impl svcLZ77UnCompVram 18 39 | svc_impl svcHuffUnComp 19 40 | svc_impl svcRLUnCompWram 20 41 | svc_impl svcRLUnCompVram 21 42 | svc_impl svcDiff8bitUnFilterWram 22 43 | svc_impl svcDiff8bitUnFilterVram 23 44 | svc_impl svcDiff16bitUnFilter 24 45 | svc_impl svcSoundBiasChange 25 46 | svc_impl svcSoundDriverInit 26 47 | svc_impl svcSoundDriverMode 27 48 | svc_impl svcSoundDriverMain 28 49 | svc_impl svcSoundDriverVSync 29 50 | svc_impl svcSoundChannelClear 30 51 | svc_impl svcMidiKey2Freq 31 52 | svc_impl svcMusicPlayerOpen 32 53 | svc_impl svcMusicPlayerStart 33 54 | svc_impl svcMusicPlayerStop 34 55 | svc_impl svcMusicPlayerContinue 35 56 | svc_impl svcMusicPlayerFadeOut 36 57 | svc_impl svcMultiBoot 37 58 | @ svc_impl svcHardReset 38 noreturn 59 | svc_impl svcSoundDriverVSyncOff 40 60 | svc_impl svcSoundDriverVSyncOn 41 61 | 62 | @ We do not need to fix the stack pointer here. Even if the stacks are 63 | @ completely trashed, since SoftReset is noreturn, and the first thing it does 64 | @ is reset the stack pointers, it's fine even if the SVC handler couldn't 65 | @ correctly return. 66 | @ 67 | @ We do disable IRQs however, since during the time the BIOS clears the upper 68 | @ 512 bytes of IWRAM, including the IRQ handler pointer, interrupts aren't 69 | @ being masked in the CPSR. 70 | fn svcSoftReset thumb 71 | ldr r2, =0x40000208 72 | str r2, [r2] 73 | svc #SVC_SOFTRESET 74 | endfn 75 | 76 | @ Here we do fix the system mode stack pointer, since RegisterRamReset uses the 77 | @ the stack. We assume in good faith that the SVC-mode stack pointer is fine, 78 | @ since under any normal circumstances, including total program failure, 79 | @ it really shouldn't be modified from it's defaults. Maybe if minrt ever 80 | @ supports relocating the stack pointers. 81 | fn svcSoftResetEx thumb 82 | ldr r2, =0x04000208 83 | strh r2, [r2] 84 | movs r1, r1 85 | beq 1f 86 | movs r2, #1 87 | bics r0, r2 88 | 1: 89 | ldr r2, =0x03007FFA 90 | strb r1, [r2] 91 | subs r2, #0xFA 92 | mov sp, r2 93 | svc #SVC_REGISTERRAMRESET 94 | svc #SVC_SOFTRESET 95 | endfn 96 | 97 | fn svcHardReset thumb 98 | ldr r2, =0x04000208 99 | str r2, [r2] 100 | svc #SVC_HARDRESET 101 | endfn 102 | 103 | @ extern void svcIntrWaitEx(u8 op, u16 intr_flags); 104 | @ 105 | @ enum IntrWaitExFlags 106 | @ { 107 | @ IWE_WAIT_NEXT = BIT(0), 108 | @ IWE_CLEAR_IE = BIT(1), 109 | @ IEW_ADD_IE = !IWE_CLEAR_IE, 110 | @ } 111 | fn svcIntrWaitEx thumb 112 | @ Load IE 113 | ldr r3, =0x04000200 114 | @ if (!(op & 2 /* IWE_CLEAR */)) { intr_flags |= IE; } 115 | lsrs r2, r0, #2 116 | ldrh r2, [r3] 117 | bcs 1f 118 | orrs r1, r1, r2 119 | 1: 120 | @ Set IE 121 | mov r12, r3 122 | strh r1, [r3] 123 | svc #SVC_INTRWAIT 124 | mov r3, r12 125 | @ Restore IE 126 | strh r2, [r3] 127 | bx lr 128 | endfn 129 | 130 | fn svcDiv thumb 131 | movs r3, r0 132 | movs r0, r1 133 | movs r1, r2 134 | movs r2, r3 135 | svc #SVC_DIV 136 | stmia r2!, {r0, r1} 137 | bx lr 138 | endfn 139 | 140 | fn svcCpuSetFixed thumb 141 | sub sp, #8 142 | str r0, [sp] 143 | mov r0, sp 144 | movs r3, #1 145 | lsls r3, r3, #24 146 | orrs r2, r2, r3 147 | svc #SVC_CPUSET 148 | add sp, #8 149 | bx lr 150 | endfn 151 | 152 | fn svcCpuFastSetFixed thumb 153 | sub sp, #8 154 | str r0, [sp] 155 | mov r0, sp 156 | movs r3, #1 157 | lsls r3, r3, #24 158 | orrs r2, r2, r3 159 | svc #SVC_CPUFASTSET 160 | add sp, #8 161 | bx lr 162 | endfn 163 | 164 | @ vim: ft=armv4 et sta sw=4 sts=8 165 | -------------------------------------------------------------------------------- /src/hw/timer.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | .include "seven/asm/hw/timer.s" 12 | 13 | fn timerSet thumb 14 | cmp r0, #3 15 | bgt 1f 16 | 17 | lsls r0, r0, #2 18 | lsls r2, r2, #16 19 | orrs r2, r2, r1 20 | 21 | ldr r3, =REG_TM0VAL 22 | str r2, [r3, r0] 23 | 1: 24 | bx lr 25 | endfn 26 | 27 | fn timerEnable thumb 28 | cmp r0, #3 29 | bgt 1f 30 | 31 | lsls r0, r0, #2 32 | movs r1, TIMER_ENABLE 33 | ldr r2, =REG_TM0CNT 34 | 35 | ldrh r3, [r2, r0] 36 | orrs r3, r3, r1 37 | strh r3, [r2, r0] 38 | 1: 39 | bx lr 40 | endfn 41 | 42 | fn timerDisable thumb 43 | cmp r0, #3 44 | bgt 1f 45 | 46 | lsls r0, r0, #2 47 | movs r1, TIMER_ENABLE 48 | ldr r2, =REG_TM0CNT 49 | 50 | ldrh r3, [r2, r0] 51 | bics r3, r3, r1 52 | strh r3, [r2, r0] 53 | 1: 54 | bx lr 55 | endfn 56 | 57 | fn timerGetValue thumb 58 | cmp r0, #3 59 | bgt 1f 60 | 61 | lsls r0, r0, #2 62 | ldr r1, =REG_TM0VAL 63 | ldrh r0, [r1, r0] 64 | bx lr 65 | 1: 66 | movs r0, #0 67 | bx lr 68 | endfn 69 | 70 | @ vim: ft=armv4 et sta sw=4 sts=8 71 | -------------------------------------------------------------------------------- /src/macros.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .macro fn .name:req .code .linkage=global .section 8 | @ Setup 9 | .ifndef .L__FN__ 10 | .set .L__FN__, 0 11 | .endif 12 | 13 | @ Code type and section (only on parent function) 14 | .ifeq .L__FN__ 15 | .ifc \.code,arm 16 | .ifnb \.section 17 | .section \.section,"ax",%progbits 18 | .else 19 | @ Default to .iwram. for ARM 20 | .section .iwram.\.name,"ax",%progbits 21 | .endif 22 | .align 2 23 | .arm 24 | .else 25 | .ifc \.code,thumb 26 | .ifnb \.section 27 | .section \.section,"ax",%progbits 28 | .else 29 | @ Default to .text. for THUMB 30 | .section .text.\.name,"ax",%progbits 31 | .endif 32 | .align 1 33 | .thumb_func 34 | .else 35 | .error "please specify function \.name as `arm` or `thumb` code" 36 | .endif 37 | .endif 38 | .endif 39 | 40 | @ Symbol visibility 41 | .ifc \.linkage,global 42 | .global \.name 43 | .else 44 | .ifc \.linkage,local 45 | .local \.name 46 | .else 47 | .error "please specify function \.name as `local` or `global` linkage" 48 | .endif 49 | .endif 50 | 51 | @ Symbol type 52 | .type \.name STT_FUNC 53 | 54 | @ !!! 55 | .altmacro 56 | @ End function to set symbol size on `endfn` 57 | .macro __mkendfn n=%.L__FN__ 58 | .macro __endfn\n 59 | .size \.name,.-\.name 60 | .purgem __endfn\n 61 | .endm 62 | .purgem __mkendfn 63 | .endm 64 | .noaltmacro 65 | 66 | __mkendfn 67 | 68 | @ Increase nested function counter 69 | .set .L__FN__, .L__FN__ + 1 70 | 71 | @ Create label 72 | \.name : 73 | .endm 74 | 75 | .macro endfn 76 | @ !!! 77 | .altmacro 78 | .macro __endfn from=0 to=.L__FN__ 79 | .if \to-\from 80 | __endfn\from 81 | __endfn %(\from+1) 82 | .endif 83 | .endm 84 | 85 | __endfn 86 | .purgem __endfn 87 | .noaltmacro 88 | 89 | @ Reset nested function counter 90 | .set .L__FN__, 0 91 | .pool 92 | .previous 93 | .endm 94 | 95 | @ Declares read-writable data. 96 | .macro data .name:req .linkage=local .section .align 97 | .macro endd 98 | .size \.name,.-\.name 99 | .previous 100 | .purgem endd 101 | .endm 102 | 103 | .ifc \.linkage,global 104 | .global \.name 105 | .else 106 | .ifc \.linkage,local 107 | .local \.name 108 | .else 109 | .error "please specify linkage of \.name `local` or `global`" 110 | .endif 111 | .endif 112 | 113 | .ifnb \.section 114 | .section \.section,"aw",%progbits 115 | .else 116 | .section .data.\.name,"aw",%progbits 117 | .endif 118 | 119 | .ifnb \.align 120 | .align \.align 121 | .endif 122 | 123 | .type \.name STT_OBJECT 124 | \.name : 125 | .endm 126 | 127 | @ Declares read-only data. 128 | .macro rodata .name:req .linkage=local .section .align 129 | .macro endr 130 | .size \.name,.-\.name 131 | .previous 132 | .purgem endr 133 | .endm 134 | 135 | .ifc \.linkage,global 136 | .global \.name 137 | .else 138 | .ifc \.linkage,local 139 | .local \.name 140 | .else 141 | .error "please specify linkage of \.name `local` or `global`" 142 | .endif 143 | .endif 144 | 145 | .ifnb \.section 146 | .section \.section,"a",%progbits 147 | .else 148 | .section .rodata.\.name,"a",%progbits 149 | .endif 150 | 151 | .ifnb \.align 152 | .align \.align 153 | .endif 154 | 155 | .type \.name STT_OBJECT 156 | \.name : 157 | .endm 158 | 159 | @ Declares zero-initialized data. 160 | .macro bss .name:req .linkage=local .section .align 161 | .macro endb 162 | .size \.name,.-\.name 163 | .previous 164 | .purgem endb 165 | .endm 166 | 167 | .ifc \.linkage,global 168 | .global \.name 169 | .else 170 | .ifc \.linkage,local 171 | .local \.name 172 | .else 173 | .error "please specify linkage of \.name `local` or `global`" 174 | .endif 175 | .endif 176 | 177 | .ifnb \.section 178 | .section \.section,"aw",%nobits 179 | .else 180 | .section .bss.\.name,"aw",%nobits 181 | .endif 182 | 183 | .ifnb \.align 184 | .align \.align 185 | .endif 186 | 187 | .type \.name STT_OBJECT 188 | \.name : 189 | .endm 190 | 191 | @ vim: ft=armv4 et sta sw=4 sts=8 192 | -------------------------------------------------------------------------------- /src/util/debug.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | 12 | fn dbgRaiseException arm .section=.text 13 | cmp r0, #5 14 | movhs r0, #0 15 | bxhs lr 16 | 17 | adr r1, EXCEPT_TABLE 18 | ldr r1, [r1, r0, lsl #2] 19 | 20 | @ prepare new cpsr 21 | @ r2 = old psr 22 | @ r0 = new psr 23 | mrs r2, cpsr 24 | bic r0, r2, #0x1F 25 | orr r0, r0, r1 26 | 27 | msr cpsr_c, r0 28 | msr spsr, r2 29 | 30 | ands r3, r1, #0xFF00 31 | 32 | @ Calculate offset for trigger instruction 33 | add lr, pc, r1, lsr #16 34 | 35 | @ "Trigger Instruction" 36 | mov pc, r3, lsr #8 37 | 38 | @ All LR values must be relative to this instruction, since no instruction 39 | @ Actually gets truly interrupted 40 | mov r0, #1 41 | bx lr 42 | 43 | EXCEPT_TABLE: 44 | .word 0x00 << 16 | 0x04 << 8 | 0x1b | 0x80 @ undefined 45 | .word 0x04 << 16 | 0x0C << 8 | 0x17 | 0x80 @ prefetch abort 46 | .word 0x08 << 16 | 0x10 << 8 | 0x17 | 0x80 @ data abort 47 | .word 0x04 << 16 | 0x18 << 8 | 0x12 | 0x80 @ irq 48 | .word 0x04 << 16 | 0x1C << 8 | 0x11 | 0xC0 @ fiq 49 | endfn 50 | 51 | @ vim: ft=armv4 et sta sw=4 sts=8 52 | -------------------------------------------------------------------------------- /src/util/log.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | */ 6 | 7 | #include 8 | 9 | typedef bool LogInitFn(void); 10 | typedef void LogOutputFn(u8, const char *); 11 | 12 | static LogInitFn logInitNone; 13 | static LogInitFn logInitMgba; 14 | static LogInitFn logInitNocash; 15 | static LogInitFn logInitVba; 16 | 17 | static LogOutputFn logOutputNone; 18 | static LogOutputFn logOutputMgba; 19 | static LogOutputFn logOutputNocash; 20 | static LogOutputFn logOutputVba; 21 | 22 | static u8 LOG_MAX_LEVEL = LOG_OFF; 23 | 24 | struct LogInterfaceDescriptor 25 | { 26 | u32 id; 27 | LogInitFn *init; 28 | LogOutputFn *output; 29 | const char *name; 30 | }; 31 | 32 | // List of log interfaces to try initializing 33 | // VBA is not included due to the fact it has no dedicated presence check, 34 | // And using it without checking can crash hardware and emulators using 35 | // an official GBA BIOS ROM. 36 | // 37 | // FIXME: Would it be better to add flags to LogInterfaceDescriptor for "skip"? 38 | static const enum LogInterface INTERFACE_SEARCH_ORDER[] = 39 | { 40 | LOGIF_MGBA, 41 | LOGIF_NOCASH, 42 | 43 | // Sentinel 44 | LOGIF_NONE, 45 | }; 46 | 47 | static const struct LogInterfaceDescriptor INTERFACES[] = 48 | { 49 | { LOGIF_NONE, logInitNone, logOutputNone, "None" }, 50 | { LOGIF_MGBA, logInitMgba, logOutputMgba, "mGBA" }, 51 | { LOGIF_NOCASH, logInitNocash, logOutputNocash, "no$gba" }, 52 | { LOGIF_VBA, logInitVba, logOutputVba, "Visual Boy Advance" }, 53 | }; 54 | 55 | static struct LogInterfaceDescriptor CUSTOM_INTERFACE = 56 | { 57 | LOGIF_CUSTOM, 58 | NULL, 59 | NULL, 60 | "Custom", 61 | }; 62 | 63 | static const struct LogInterfaceDescriptor *LOG_INTERFACE = 64 | &INTERFACES[LOGIF_NONE]; 65 | 66 | extern u8 logInit(void) 67 | { 68 | const enum LogInterface *lid = INTERFACE_SEARCH_ORDER; 69 | const struct LogInterfaceDescriptor *lf; 70 | 71 | while ((lf = &INTERFACES[*lid]), !lf->init()) 72 | { 73 | lid++; 74 | } 75 | 76 | LOG_INTERFACE = lf; 77 | 78 | return lf->id; 79 | } 80 | 81 | extern bool logInitInterface(u8 interface) 82 | { 83 | if (interface >= (sizeof(INTERFACES) / sizeof(INTERFACES[0]))) 84 | { 85 | return false; 86 | } 87 | 88 | const struct LogInterfaceDescriptor *l = &INTERFACES[interface]; 89 | 90 | if (!l->init) 91 | { 92 | return false; 93 | } 94 | 95 | if (!l->init()) 96 | { 97 | return false; 98 | } 99 | 100 | LOG_INTERFACE = l; 101 | 102 | return true; 103 | } 104 | 105 | extern void logInitCustom(LogCustomOutputFunction *f) 106 | { 107 | CUSTOM_INTERFACE.output = f; 108 | LOG_INTERFACE = &CUSTOM_INTERFACE; 109 | } 110 | 111 | extern u8 logGetInterface(void) 112 | { 113 | return LOG_INTERFACE->id; 114 | } 115 | 116 | extern const char* logGetInterfaceName(void) 117 | { 118 | return LOG_INTERFACE->name; 119 | } 120 | 121 | extern u8 logGetMaxLevel(void) 122 | { 123 | return LOG_MAX_LEVEL; 124 | } 125 | 126 | extern void logSetMaxLevel(u8 level) 127 | { 128 | LOG_MAX_LEVEL = level; 129 | } 130 | 131 | extern void logOutput(u8 level, const char *message) 132 | { 133 | // Prevent silliness with logOutput(LOG_OFF, "Message"); 134 | if (!level || level > LOG_MAX_LEVEL) 135 | { 136 | return; 137 | } 138 | 139 | LOG_INTERFACE->output(level, message); 140 | } 141 | 142 | // Log interface implementations 143 | 144 | static bool logInitNone(void) 145 | { 146 | return true; 147 | } 148 | 149 | static void logOutputNone(u8 level, const char *message) 150 | { 151 | (void)level; 152 | (void)message; 153 | } 154 | 155 | #define REG_MGBA_ENABLE VOLADDR(0x04FFF780, u16) 156 | #define REG_MGBA_FLAGS VOLADDR(0x04FFF700, u16) 157 | #define MGBA_LOG_OUT ((char*)0x04FFF600) 158 | 159 | static bool logInitMgba(void) 160 | { 161 | REG_MGBA_ENABLE = 0xC0DE; 162 | 163 | return REG_MGBA_ENABLE == 0x1DEA; 164 | } 165 | 166 | static void logOutputMgba(u8 level, const char *message) 167 | { 168 | for (int i = 0; message[i] && i < 256; i++) 169 | { 170 | MGBA_LOG_OUT[i] = message[i]; 171 | } 172 | 173 | // FIXME: What if invalid level? Reject? Default to TRACE? 174 | REG_MGBA_FLAGS = (level - 1) | 0x100; 175 | } 176 | 177 | #define NOCASH_SIG ((char*)0x04FFFA00) 178 | #define REG_NOCASH_LOG VOLADDR(0x04FFFA1C, u8) 179 | 180 | static bool logInitNocash(void) 181 | { 182 | for (int i = 0; i < 6; i++) 183 | { 184 | if ("no$gba"[i] != NOCASH_SIG[i]) 185 | { 186 | return false; 187 | } 188 | } 189 | 190 | return true; 191 | } 192 | 193 | static void logOutputNocash(u8 level, const char *message) 194 | { 195 | (void)level; 196 | 197 | while (*message) 198 | { 199 | REG_NOCASH_LOG = *message++; 200 | } 201 | 202 | REG_NOCASH_LOG = '\n'; 203 | } 204 | 205 | static bool logInitVba(void) 206 | { 207 | return true; 208 | } 209 | 210 | static void logOutputVba(u8 level, const char *message) 211 | { 212 | (void)level; 213 | 214 | __asm__( 215 | "movs r0, %0\n" 216 | "svc #255" :: "r" (message) : "r0" 217 | ); 218 | 219 | __asm__( 220 | "movs r0, %0\n" 221 | "svc #255" :: "r" ("\n") : "r0" 222 | ); 223 | } 224 | -------------------------------------------------------------------------------- /src/util/mem.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | 12 | @ Memory Copy 13 | 14 | fn memCopy8 thumb 15 | bx lr 16 | endfn 17 | 18 | fn memCopy16 thumb 19 | bx lr 20 | endfn 21 | 22 | fn memCopy32 arm 23 | bx lr 24 | endfn 25 | 26 | @ Memory Move 27 | 28 | fn memMove8 thumb 29 | bx lr 30 | endfn 31 | 32 | fn memMove16 thumb 33 | bx lr 34 | endfn 35 | 36 | fn memMove32 arm 37 | bx lr 38 | endfn 39 | 40 | @ Memory Set 41 | 42 | fn memSet8 thumb 43 | bx lr 44 | endfn 45 | 46 | fn memSet16 thumb 47 | bx lr 48 | endfn 49 | 50 | fn memSet32 arm 51 | bx lr 52 | endfn 53 | 54 | @ Memory Zero 55 | 56 | fn memZero8 thumb 57 | movs r2, r1 58 | movs r1, #0 59 | b memSet8 60 | endfn 61 | 62 | fn memZero16 thumb 63 | movs r2, r1 64 | movs r1, #0 65 | b memSet16 66 | endfn 67 | 68 | fn memZero32 arm 69 | movs r2, r1 70 | movs r1, #0 71 | b memSet32 72 | endfn 73 | 74 | @ Memory Compare 75 | 76 | fn memCompare8 thumb 77 | bx lr 78 | endfn 79 | 80 | fn memCompare16 thumb 81 | bx lr 82 | endfn 83 | 84 | fn memCompare32 arm 85 | bx lr 86 | endfn 87 | 88 | @ vim: ft=armv4 et sta sw=4 sts=8 89 | -------------------------------------------------------------------------------- /src/util/profile.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | .include "seven/asm/hw/timer.s" 12 | 13 | @ u32 profileFunction(void (*f)(void), u32 *return_value); 14 | @ 15 | fn profileRun arm 16 | push {r4, r5, r6, lr} 17 | 18 | mov r4, r1 19 | 20 | @ Setup timers 21 | ldr r5, =REG_TM2VAL 22 | mov r6, #TIMER_FREQ_CASCADE << 16 23 | mov r1, #TIMER_ENABLE << 16 24 | stmia r5, {r1, r6} 25 | 26 | @ Call function 27 | mov lr, pc 28 | bx r0 29 | 30 | @ Stop timer 31 | strh r6, [r5, #2] 32 | 33 | @ Store return value? 34 | cmp r4, #0 35 | strne r0, [r4] 36 | 37 | @ Read timers 38 | ldrh r0, [r5] 39 | ldrh r1, [r5, #4] 40 | orr r0, r0, r1, lsl #16 41 | @ Subtract 3 Cycles of timer start/stop delay 42 | sub r0, r0, #3 43 | 44 | pop {r4, r5, r6, lr} 45 | bx lr 46 | endfn 47 | 48 | @ vim: ft=armv4 et sta sw=4 sts=8 49 | -------------------------------------------------------------------------------- /src/util/rand.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | 12 | @ state cannot be all zeroes, generator locks up 13 | @ set last state word as 1 to make sure SetSeed(0) is fine. 14 | data XOSHIRO_STATE 15 | .word 0, 0, 0, 1 16 | endd 17 | 18 | @ void randSetSeed(u32 seed) 19 | @ 20 | fn randSetSeed thumb 21 | ldr r1, =XOSHIRO_STATE 22 | str r0, [r1] 23 | bx lr 24 | endfn 25 | 26 | @ u32 randNext(void) 27 | @ 28 | @ Implementation of the Xoshiro128++ PRNG. 29 | @ 30 | @ Excellent speed, entropy, and periodicity. 31 | @ 32 | @ Code from https://xoshiro.di.unimi.it/xoshiro128plusplus.c 33 | @ 34 | @ r0 return 35 | @ r1 s[0] 36 | @ r2 s[1] 37 | @ r3 s[2] 38 | @ r4 s[3] 39 | @ r5 t 40 | @ r6 &XOSHIRO_STATE 41 | @ r7 ROR immediate 42 | fn randNext thumb 43 | push {r4, r5, r6, r7} 44 | 45 | ldr r6, =XOSHIRO_STATE 46 | ldmia r6!, {r1, r2, r3, r4} 47 | 48 | @ r = s[0] + s[3] 49 | adds r0, r1, r4 50 | @ r = rotl(r, 7) 51 | movs r7, #25 52 | rors r0, r0, r7 53 | @ r += s[0] 54 | adds r0, r0, r1 55 | 56 | @ t = s[1] << 9 57 | lsls r5, r2, #9 58 | 59 | @ s[2] ^= s[0] 60 | eors r3, r3, r1 61 | @ s[3] ^= s[1] 62 | eors r4, r4, r2 63 | @ s[1] ^= s[2] 64 | eors r2, r2, r3 65 | @ s[0] ^= s[3] 66 | eors r1, r1, r4 67 | @ s[2] ^= t 68 | eors r3, r3, r5 69 | 70 | @ s[3] = rotl(s[3], 11) 71 | movs r7, #21 72 | rors r4, r4, r7 73 | 74 | subs r6, r6, #16 75 | stmia r6!, {r1, r2, r3, r4} 76 | 77 | pop {r4, r5, r6, r7} 78 | bx lr 79 | endfn 80 | 81 | @ vim: ft=armv4 et sta sw=4 sts=8 82 | -------------------------------------------------------------------------------- /src/util/simd.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | 12 | @ r0 = mask 13 | @ r1 = dst 14 | @ r2 = src 15 | @ r3 = len 16 | @ r4 = a 17 | @ r5 = b 18 | @ r6 = s 19 | fn simdAdd thumb 20 | push {r4, r5, r6, r7} 21 | 22 | cmp r3, #0 23 | beq 2f 24 | 25 | adds r3, r1, r3 26 | 1: 27 | ldr r4, [r1] 28 | ldm r2!, {r5} 29 | @ s = (a ^ b) & m 30 | movs r6, r4 31 | eors r6, r6, r5 32 | ands r6, r6, r0 33 | @ a &= ~m 34 | @ b &= ~m 35 | bics r4, r4, r0 36 | bics r5, r5, r0 37 | @ a = (a + b) ^ s 38 | adds r4, r4, r5 39 | eors r4, r4, r6 40 | 41 | stm r1!, {r4} 42 | cmp r1, r3 43 | blt 1b 44 | 2: 45 | pop {r4, r5, r6, r7} 46 | bx lr 47 | endfn 48 | 49 | fn simdSub thumb 50 | push {r4, r5, r6, r7} 51 | 52 | cmp r3, #0 53 | beq 2f 54 | 55 | adds r3, r1, r3 56 | 57 | 1: 58 | ldr r4, [r1] 59 | ldm r2!, {r5} 60 | @ s = (~a ^ b) & m 61 | mvns r6, r4 62 | eors r6, r6, r5 63 | ands r6, r6, r0 64 | @ a |= m 65 | @ b &= ~m 66 | orrs r4, r4, r0 67 | bics r5, r5, r0 68 | @ a = (a - b) ^ s 69 | subs r4, r4, r5 70 | eors r4, r4, r6 71 | 72 | stm r1!, {r4} 73 | cmp r1, r3 74 | blt 1b 75 | 76 | 2: 77 | pop {r4, r5, r6, r7} 78 | bx lr 79 | endfn 80 | 81 | @ vim: ft=armv4 et sta sw=4 sts=8 82 | -------------------------------------------------------------------------------- /src/util/str.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | 12 | @ vim: ft=armv4 et sta sw=4 sts=8 13 | -------------------------------------------------------------------------------- /src/video/bmp.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | .include "seven/asm/hw/video.s" 12 | 13 | @ void* bmpSwapBuffers(void) 14 | @ 15 | fn bmpSwapBuffers thumb 16 | @ r0 = REG_DISCPNT 17 | @ r1 = LCD_FRAME_SELECT 18 | movs r1, LCD_FRAME_SELECT 19 | lsls r0, r1, #22 20 | 21 | @ r2 = DISPCNT ^ LCD_FRAME_SELECT 22 | ldrh r2, [r0] 23 | eors r2, r2, r1 24 | strh r2, [r0] 25 | 26 | @ r0 = MEM_VRAM 27 | movs r0, #6 28 | lsls r0, r0, #24 29 | 30 | @ Is flipped? 31 | tst r2, r1 32 | bne .Llsb_noadd 33 | 34 | @ r0 += 0xA000 35 | movs r2, #0xA 36 | lsls r2, r2, #12 37 | adds r0, r0, r2 38 | 39 | .Llsb_noadd: 40 | bx lr 41 | endfn 42 | 43 | @ void* bmpInitMode3(void) 44 | @ 45 | fn bmpInitMode3 thumb 46 | @ r0 = REG_DISCPNT 47 | @ r1 = LCD_BG2_ENABLE 48 | movs r0, #4 49 | lsls r1, r0, #8 50 | lsls r0, r0, #24 51 | 52 | @ r1 = LCD_BG2_ENABLE | LCD_MODE_3 53 | adds r1, r1, LCD_MODE_3 54 | 55 | @ REG_DISCPNT = r1 56 | strh r1, [r0] 57 | @ Reset REG_BG2CNT 58 | strh r0, [r0, #12] 59 | 60 | @ return MEM_VRAM; 61 | movs r0, #6 62 | lsls r0, r0, #24 63 | bx lr 64 | endfn 65 | 66 | @ void* bmpInitMode4(void) 67 | @ 68 | fn bmpInitMode4 thumb 69 | @ r0 = REG_DISCPNT 70 | @ r1 = LCD_BG2_ENABLE 71 | movs r0, #4 72 | lsls r1, r0, #8 73 | lsls r0, r0, #24 74 | 75 | @ r1 = LCD_BG2_ENABLE | LCD_MODE_4 76 | adds r1, r1, LCD_MODE_4 77 | 78 | @ REG_DISCPNT = r1 79 | strh r1, [r0] 80 | @ Reset REG_BG2CNT 81 | strh r0, [r0, #12] 82 | 83 | @ return MEM_VRAM; 84 | movs r0, #6 85 | lsls r0, r0, #24 86 | bx lr 87 | endfn 88 | 89 | @ void* bmpInitMode5(void) 90 | @ 91 | fn bmpInitMode5 thumb 92 | @ r0 = REG_DISCPNT 93 | @ r1 = LCD_BG2_ENABLE 94 | movs r0, #4 95 | lsls r1, r0, #8 96 | lsls r0, r0, #24 97 | 98 | @ r1 = LCD_BG2_ENABLE | LCD_MODE_5 99 | adds r1, r1, LCD_MODE_5 100 | 101 | @ REG_DISCPNT = r1 102 | strh r1, [r0] 103 | @ Reset REG_BG2CNT 104 | strh r0, [r0, #12] 105 | 106 | @ return MEM_VRAM; 107 | movs r0, #6 108 | lsls r0, r0, #24 109 | bx lr 110 | endfn 111 | 112 | @ vim: ft=armv4 et sta sw=4 sts=8 113 | -------------------------------------------------------------------------------- /src/video/oam.s: -------------------------------------------------------------------------------- 1 | @ 2 | @ This Source Code Form is subject to the terms of the Mozilla Public 3 | @ License, v. 2.0. If a copy of the MPL was not distributed with this 4 | @ file, You can obtain one at https://mozilla.org/MPL/2.0/. 5 | @ 6 | 7 | .syntax unified 8 | .cpu arm7tdmi 9 | 10 | .include "macros.s" 11 | 12 | @ usize oamWriteObjects(usize oam_index, struct Object *obj, usize n_objs) 13 | @ 14 | fn oamWriteObjects thumb 15 | adds r3, r0, r2 16 | bcs .Lo_truncate 17 | 18 | cmp r3, #128 19 | ble oamWriteObjectsUnchecked 20 | 21 | .Lo_truncate: 22 | movs r3, #128 23 | subs r2, r3, r0 24 | bmi .Lo_exit0 25 | 26 | fn oamWriteObjectsUnchecked 27 | push {r4, r5} 28 | 29 | movs r5, r2 30 | beq .Lo_exit 31 | 32 | lsls r0, r0, #3 33 | lsls r2, r2, #3 34 | 35 | @ r0 += MEM_OAM 36 | movs r3, #7 37 | lsls r3, r3, #24 38 | 39 | adds r0, r0, r3 40 | @ End pointer 41 | adds r2, r2, r1 42 | @ r0: dst, r1: src, r2: end 43 | 44 | .Lo_loop: 45 | ldm r1!, {r3, r4} 46 | str r3, [r0] 47 | strh r4, [r0, #4] 48 | adds r0, #8 49 | cmp r1, r2 50 | bne .Lo_loop 51 | 52 | .Lo_exit: 53 | movs r0, r5 54 | pop {r4, r5} 55 | bx lr 56 | .Lo_exit0: 57 | movs r0, #0 58 | bx lr 59 | endfn 60 | 61 | @ usize oamWriteMatrices(usize oam_index, struct Matrix *mat, usize n_mats) 62 | fn oamWriteMatrices thumb 63 | adds r3, r0, r2 64 | bcs .Lm_truncate 65 | 66 | cmp r3, #32 67 | ble oamWriteMatricesUnchecked 68 | 69 | .Lm_truncate: 70 | movs r3, #32 71 | subs r2, r3, r0 72 | bmi .Lm_exit0 73 | 74 | fn oamWriteMatricesUnchecked 75 | push {r4, r5} 76 | movs r5, r2 77 | beq .Lm_exit 78 | 79 | lsls r0, r0, #5 80 | lsls r2, r2, #3 81 | 82 | @ r0 += MEM_OAM 83 | movs r3, #7 84 | lsls r3, r3, #24 85 | 86 | @ OAM pointer 87 | adds r0, r0, r3 88 | @ End pointer 89 | adds r2, r2, r1 90 | @ r0: dst, r1: src, r2: end 91 | 92 | .Lm_loop: 93 | ldm r1!, {r3, r4} 94 | strh r3, [r0, #0+6] 95 | lsrs r3, r3, #16 96 | strh r3, [r0, #8+6] 97 | strh r4, [r0, #16+6] 98 | lsrs r4, r4, #16 99 | strh r4, [r0, #24+6] 100 | adds r0, #32 101 | cmp r1, r2 102 | bne .Lm_loop 103 | 104 | .Lm_exit: 105 | movs r0, r5 106 | pop {r4, r5} 107 | bx lr 108 | .Lm_exit0: 109 | movs r0, #0 110 | bx lr 111 | endfn 112 | 113 | @ vim: ft=armv4 et sta sw=4 sts=8 114 | --------------------------------------------------------------------------------