From 89aadfb6064ca181cbd098daa823d2c957e52833 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 21 Jun 2020 11:48:18 -0500 Subject: [PATCH] [nostalgia/core/gba] Add IRQ system --- src/nostalgia/core/CMakeLists.txt | 1 - src/nostalgia/core/gba/CMakeLists.txt | 4 +++ src/nostalgia/core/gba/addresses.hpp | 19 +++++++++--- src/nostalgia/core/gba/core.cpp | 8 +++++ .../core/{core.cpp => gba/irq.arm.cpp} | 12 ++++---- src/nostalgia/core/gba/irq.cpp | 29 +++++++++++++++++++ src/nostalgia/core/gba/irq.hpp | 27 +++++++++++++++++ src/nostalgia/core/sdl/core.cpp | 7 ++++- 8 files changed, 96 insertions(+), 11 deletions(-) rename src/nostalgia/core/{core.cpp => gba/irq.arm.cpp} (57%) create mode 100644 src/nostalgia/core/gba/irq.cpp create mode 100644 src/nostalgia/core/gba/irq.hpp diff --git a/src/nostalgia/core/CMakeLists.txt b/src/nostalgia/core/CMakeLists.txt index c666166d..b378b5b7 100644 --- a/src/nostalgia/core/CMakeLists.txt +++ b/src/nostalgia/core/CMakeLists.txt @@ -1,6 +1,5 @@ add_library( NostalgiaCore - core.cpp gfx.cpp media.cpp ) diff --git a/src/nostalgia/core/gba/CMakeLists.txt b/src/nostalgia/core/gba/CMakeLists.txt index 89b25ad2..770e42d7 100644 --- a/src/nostalgia/core/gba/CMakeLists.txt +++ b/src/nostalgia/core/gba/CMakeLists.txt @@ -3,10 +3,14 @@ if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA") NostalgiaCore-GBA core.cpp gfx.cpp + irq.arm.cpp + irq.cpp media.cpp panic.cpp ) + set_source_files_properties(irq.arm.cpp PROPERTIES COMPILE_FLAGS -marm) + target_link_libraries( NostalgiaCore-GBA PUBLIC NostalgiaCore diff --git a/src/nostalgia/core/gba/addresses.hpp b/src/nostalgia/core/gba/addresses.hpp index e126cec3..a0b0be1a 100644 --- a/src/nostalgia/core/gba/addresses.hpp +++ b/src/nostalgia/core/gba/addresses.hpp @@ -1,18 +1,30 @@ /* - * Copyright 2016 - 2019 gtalent2@gmail.com + * Copyright 2016 - 2020 gtalent2@gmail.com * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + #pragma once #include ///////////////////////////////////////////////////////////////// -// I/O Registers +// Interrupt Handler -#define REG_DISPCNT *reinterpret_cast(0x04000000) +using interrupt_handler = void (*)(void); +#define REG_ISR *reinterpret_cast(0x03007FFC) +#define REG_IE *reinterpret_cast(0x04000200) +#define REG_IF *reinterpret_cast(0x04000202) +#define REG_IME *reinterpret_cast(0x04000208) + +///////////////////////////////////////////////////////////////// +// Display Registers + +#define REG_DISPCNT *reinterpret_cast(0x04000000) +#define REG_DISPSTAT *reinterpret_cast(0x04000004) +#define REG_VCOUNT *reinterpret_cast(0x04000006) ///////////////////////////////////////////////////////////////// // background registers @@ -35,7 +47,6 @@ #define REG_BG2VOFS *reinterpret_cast(0x0400001a) #define REG_BG3VOFS *reinterpret_cast(0x0400001e) - ///////////////////////////////////////////////////////////////// // Memory Addresses diff --git a/src/nostalgia/core/gba/core.cpp b/src/nostalgia/core/gba/core.cpp index f611f4be..46f9a7f7 100644 --- a/src/nostalgia/core/gba/core.cpp +++ b/src/nostalgia/core/gba/core.cpp @@ -8,8 +8,16 @@ #include +#include "irq.hpp" + namespace nostalgia::core { +ox::Error init(Context *ctx) { + oxReturnError(initGfx(ctx)); + oxReturnError(initIrq(ctx)); + return OxError(0); +} + ox::Error run(Context*) { while (1) { } diff --git a/src/nostalgia/core/core.cpp b/src/nostalgia/core/gba/irq.arm.cpp similarity index 57% rename from src/nostalgia/core/core.cpp rename to src/nostalgia/core/gba/irq.arm.cpp index f536680a..6b35b98b 100644 --- a/src/nostalgia/core/core.cpp +++ b/src/nostalgia/core/gba/irq.arm.cpp @@ -1,18 +1,20 @@ /* - * Copyright 2016 - 2019 gtalent2@gmail.com + * Copyright 2016 - 2020 gtalent2@gmail.com * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "mem.hpp" -#include "core.hpp" +// NOTE: this file is compiled as ARM and not THUMB, so don't but too much in +// here + +#include "irq.hpp" namespace nostalgia::core { -ox::Error init(Context *ctx) { - return initGfx(ctx); +void isr() { + REG_IF = IntId_vblank; } } diff --git a/src/nostalgia/core/gba/irq.cpp b/src/nostalgia/core/gba/irq.cpp new file mode 100644 index 00000000..9082542c --- /dev/null +++ b/src/nostalgia/core/gba/irq.cpp @@ -0,0 +1,29 @@ +/* + * Copyright 2016 - 2020 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "irq.hpp" + +namespace nostalgia::core { + +constexpr auto DispStat_irq_vblank = static_cast(1) << 3; +constexpr auto DispStat_irq_hblank = static_cast(1) << 4; +constexpr auto DispStat_irq_vcount = static_cast(1) << 5; + +void isr(); + +ox::Error initIrq(Context*) { + REG_ISR = isr; + // tell display to trigger vblank interrupts + REG_DISPSTAT |= DispStat_irq_vblank; + // tell proc which interrupts to handle + REG_IE |= IntId_vblank; + REG_IME = 1; + return OxError(0); +} + +} diff --git a/src/nostalgia/core/gba/irq.hpp b/src/nostalgia/core/gba/irq.hpp new file mode 100644 index 00000000..b56201a7 --- /dev/null +++ b/src/nostalgia/core/gba/irq.hpp @@ -0,0 +1,27 @@ +/* + * Copyright 2016 - 2020 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include "addresses.hpp" + +namespace nostalgia::core { + +constexpr uint16_t IntId_vblank = static_cast(1) << 0; +constexpr uint16_t IntId_hblank = static_cast(1) << 1; +constexpr uint16_t IntId_vcount = static_cast(1) << 2; +constexpr uint16_t IntId_timer0 = static_cast(1) << 3; +constexpr uint16_t IntId_timer1 = static_cast(1) << 4; +constexpr uint16_t IntId_timer2 = static_cast(1) << 5; +constexpr uint16_t IntId_serial = static_cast(1) << 6; // link cable +constexpr uint16_t IntId_input = static_cast(1) << 14; // gamepad +constexpr uint16_t IntId_cart = static_cast(1) << 15; // cartridge removed + +[[nodiscard]] ox::Error initIrq(Context *ctx); + +} diff --git a/src/nostalgia/core/sdl/core.cpp b/src/nostalgia/core/sdl/core.cpp index d7e52f30..ee583a14 100644 --- a/src/nostalgia/core/sdl/core.cpp +++ b/src/nostalgia/core/sdl/core.cpp @@ -8,12 +8,17 @@ #include -#include +#include namespace nostalgia::core { void draw(Context *ctx); +ox::Error init(Context *ctx) { + oxReturnError(initGfx(ctx)); + return OxError(0); +} + ox::Error run(Context *ctx) { for (auto running = true; running;) { SDL_Event event;