From 67cbe6ef7f0af2c02e411c4484f71476e6d4a6db Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 21 Jan 2021 00:24:02 -0600 Subject: [PATCH] [nostalgia/core/gba] Fix C++20 compatibility issues --- src/nostalgia/core/gba/core.cpp | 4 +- src/nostalgia/core/gba/gfx.cpp | 69 +++++++++++++++++------------- src/nostalgia/core/gba/irq.arm.cpp | 12 +++--- src/nostalgia/core/gba/irq.hpp | 4 +- 4 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/nostalgia/core/gba/core.cpp b/src/nostalgia/core/gba/core.cpp index c96e5efd..8006b219 100644 --- a/src/nostalgia/core/gba/core.cpp +++ b/src/nostalgia/core/gba/core.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2020 gary@drinkingtea.net + * Copyright 2016 - 2021 gary@drinkingtea.net * * 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 @@ -37,7 +37,7 @@ static void initTimer() { REG_TIMER0 = TicksMs59ns; REG_TIMER0CTL = 0b11000000; // enable interrupt for timer0 - REG_IE |= Int_timer0; + REG_IE = REG_IE | Int_timer0; } ox::Error init(Context *ctx) { diff --git a/src/nostalgia/core/gba/gfx.cpp b/src/nostalgia/core/gba/gfx.cpp index 7b421a84..3ba76410 100644 --- a/src/nostalgia/core/gba/gfx.cpp +++ b/src/nostalgia/core/gba/gfx.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2020 gary@drinkingtea.net + * Copyright 2016 - 2021 gary@drinkingtea.net * * 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 @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -66,15 +65,15 @@ ox::Error modelRead(T *io, GbaTileMapTarget *t) { constexpr auto Bpp8 = 1 << 7; *t->bgCtl = (28 << 8) | 1; if (bpp == 4) { - *t->bgCtl |= (*t->bgCtl | Bpp8) ^ Bpp8; // set to use 4 bits per pixel + *t->bgCtl = *t->bgCtl | ((*t->bgCtl | Bpp8) ^ Bpp8); // set to use 4 bits per pixel } else { - *t->bgCtl |= Bpp8; // set to use 8 bits per pixel + *t->bgCtl = *t->bgCtl | Bpp8; // set to use 8 bits per pixel } oxReturnError(io->field("defaultPalette", &t->defaultPalette)); oxReturnError(io->field("pal", &t->pal)); uint16_t intermediate = 0; - auto handleTileMap = [t, &intermediate](std::size_t i, uint8_t *tile) { + const auto handleTileMap = [t, &intermediate](std::size_t i, uint8_t *tile) { if (i & 1) { // i is odd intermediate |= static_cast(*tile) << 8; t->tileMap[i / 2] = intermediate; @@ -92,9 +91,9 @@ ox::Error initGfx(Context*) { | DispCtl_Bg0 | DispCtl_Obj; // tell display to trigger vblank interrupts - REG_DISPSTAT |= DispStat_irq_vblank; + REG_DISPSTAT = REG_DISPSTAT | DispStat_irq_vblank; // enable vblank interrupt - REG_IE |= Int_vblank; + REG_IE = REG_IE | Int_vblank; return OxError(0); } @@ -138,9 +137,9 @@ ox::Error loadBgTileSheet(Context *ctx, int section, ox::FileAddress tilesheetAddr, ox::FileAddress paletteAddr) { - auto [tsStat, tsStatErr] = ctx->rom->stat(tilesheetAddr); + const auto [tsStat, tsStatErr] = ctx->rom->stat(tilesheetAddr); oxReturnError(tsStatErr); - auto [ts, tserr] = ctx->rom->read(tilesheetAddr); + const auto [ts, tserr] = ctx->rom->read(tilesheetAddr); oxReturnError(tserr); GbaTileMapTarget target; target.pal.palette = &MEM_BG_PALETTE[section]; @@ -162,9 +161,9 @@ ox::Error loadSpriteTileSheet(Context *ctx, int section, ox::FileAddress tilesheetAddr, ox::FileAddress paletteAddr) { - auto [tsStat, tsStatErr] = ctx->rom->stat(tilesheetAddr); + const auto [tsStat, tsStatErr] = ctx->rom->stat(tilesheetAddr); oxReturnError(tsStatErr); - auto [ts, tserr] = ctx->rom->read(tilesheetAddr); + const auto [ts, tserr] = ctx->rom->read(tilesheetAddr); oxReturnError(tserr); GbaTileMapTarget target; target.pal.palette = &MEM_SPRITE_PALETTE[section]; @@ -173,9 +172,9 @@ ox::Error loadSpriteTileSheet(Context *ctx, oxReturnError(ox::readMC(ts, tsStat.size, &target)); // load external palette if available if (paletteAddr) { - auto [palStat, palStatErr] = ctx->rom->stat(paletteAddr); + const auto [palStat, palStatErr] = ctx->rom->stat(paletteAddr); oxReturnError(palStatErr); - auto [pal, palErr] = ctx->rom->read(paletteAddr); + const auto [pal, palErr] = ctx->rom->read(paletteAddr); oxReturnError(palErr); oxReturnError(ox::readMC(pal, palStat.size, &target.pal)); } @@ -185,9 +184,9 @@ ox::Error loadSpriteTileSheet(Context *ctx, ox::Error loadBgPalette(Context *ctx, int section, ox::FileAddress paletteAddr) { GbaPaletteTarget target; target.palette = &MEM_BG_PALETTE[section]; - auto [palStat, palStatErr] = ctx->rom->stat(paletteAddr); + const auto [palStat, palStatErr] = ctx->rom->stat(paletteAddr); oxReturnError(palStatErr); - auto [pal, palErr] = ctx->rom->read(paletteAddr); + const auto [pal, palErr] = ctx->rom->read(paletteAddr); oxReturnError(palErr); oxReturnError(ox::readMC(pal, palStat.size, &target)); return OxError(0); @@ -196,9 +195,9 @@ ox::Error loadBgPalette(Context *ctx, int section, ox::FileAddress paletteAddr) ox::Error loadSpritePalette(Context *ctx, int section, ox::FileAddress paletteAddr) { GbaPaletteTarget target; target.palette = &MEM_SPRITE_PALETTE[section]; - auto [palStat, palStatErr] = ctx->rom->stat(paletteAddr); + const auto [palStat, palStatErr] = ctx->rom->stat(paletteAddr); oxReturnError(palStatErr); - auto [pal, palErr] = ctx->rom->read(paletteAddr); + const auto [pal, palErr] = ctx->rom->read(paletteAddr); oxReturnError(palErr); oxReturnError(ox::readMC(pal, palStat.size, &target)); return OxError(0); @@ -221,6 +220,7 @@ void clearTileLayer(Context*, int layer) { } void hideSprite(Context*, unsigned idx) { + oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow"); GbaSpriteAttrUpdate oa; oa.attr0 = 2 << 8; oa.idx = idx; @@ -229,18 +229,26 @@ void hideSprite(Context*, unsigned idx) { nostalgia_core_vblankintrwait(); } if constexpr(config::GbaEventLoopTimerBased) { - REG_IE &= ~Int_vblank; // disable vblank interrupt handler - g_spriteBuffer[g_spriteUpdates++] = oa; - REG_IE |= Int_vblank; // enable vblank interrupt handler + REG_IE = REG_IE & ~Int_vblank; // disable vblank interrupt handler + g_spriteBuffer[g_spriteUpdates] = oa; + REG_IE = REG_IE | Int_vblank; // enable vblank interrupt handler } else { - auto ie = REG_IE; // disable vblank interrupt handler - REG_IE &= ~Int_vblank; // disable vblank interrupt handler - g_spriteBuffer[g_spriteUpdates++] = oa; + const auto ie = REG_IE; // disable vblank interrupt handler + REG_IE = REG_IE & ~Int_vblank; // disable vblank interrupt handler + g_spriteBuffer[g_spriteUpdates] = oa; REG_IE = ie; // enable vblank interrupt handler } + g_spriteUpdates = g_spriteUpdates + 1; } -void setSprite(Context*, unsigned idx, unsigned x, unsigned y, unsigned tileIdx, unsigned spriteShape, unsigned spriteSize, unsigned flipX) { +void setSprite(Context*, + unsigned idx, + unsigned x, + unsigned y, + unsigned tileIdx, + unsigned spriteShape, + unsigned spriteSize, + unsigned flipX) { oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow"); GbaSpriteAttrUpdate oa; oa.attr0 = static_cast(y & ox::onMask(7)) @@ -256,15 +264,16 @@ void setSprite(Context*, unsigned idx, unsigned x, unsigned y, unsigned tileIdx, nostalgia_core_vblankintrwait(); } if constexpr(config::GbaEventLoopTimerBased) { - REG_IE &= ~Int_vblank; // disable vblank interrupt handler - g_spriteBuffer[g_spriteUpdates++] = oa; - REG_IE |= Int_vblank; // enable vblank interrupt handler + REG_IE = REG_IE & ~Int_vblank; // disable vblank interrupt handler + g_spriteBuffer[g_spriteUpdates] = oa; + REG_IE = REG_IE | Int_vblank; // enable vblank interrupt handler } else { - auto ie = REG_IE; // disable vblank interrupt handler - REG_IE &= ~Int_vblank; // disable vblank interrupt handler - g_spriteBuffer[g_spriteUpdates++] = oa; + const auto ie = REG_IE; // disable vblank interrupt handler + REG_IE = REG_IE & ~Int_vblank; // disable vblank interrupt handler + g_spriteBuffer[g_spriteUpdates] = oa; REG_IE = ie; // enable vblank interrupt handler } + g_spriteUpdates = g_spriteUpdates + 1; } } diff --git a/src/nostalgia/core/gba/irq.arm.cpp b/src/nostalgia/core/gba/irq.arm.cpp index caccb8f5..337429cf 100644 --- a/src/nostalgia/core/gba/irq.arm.cpp +++ b/src/nostalgia/core/gba/irq.arm.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2020 gary@drinkingtea.net + * Copyright 2016 - 2021 gary@drinkingtea.net * * 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 @@ -9,6 +9,8 @@ // NOTE: this file is compiled as ARM and not THUMB, so don't but too much in // here +#include + #include "addresses.hpp" #include "gfx.hpp" #include "irq.hpp" @@ -32,18 +34,18 @@ void nostalgia_core_isr_vblank() { // is volatile const auto updates = g_spriteUpdates; for (uint16_t i = 0; i < updates; ++i) { - auto &oa = g_spriteBuffer[i]; - MEM_OAM[oa.idx] = *reinterpret_cast(&oa); + const auto &oa = g_spriteBuffer[i]; + MEM_OAM[oa.idx] = *ox::bit_cast(&oa); } g_spriteUpdates = 0; if constexpr(config::GbaEventLoopTimerBased) { // disable vblank interrupt until it is needed again - REG_IE &= ~Int_vblank; + REG_IE = REG_IE & ~Int_vblank; } } void nostalgia_core_isr_timer0() { - ++g_timerMs; + g_timerMs = g_timerMs + 1; } } diff --git a/src/nostalgia/core/gba/irq.hpp b/src/nostalgia/core/gba/irq.hpp index e753e8db..bb8cd68c 100644 --- a/src/nostalgia/core/gba/irq.hpp +++ b/src/nostalgia/core/gba/irq.hpp @@ -1,11 +1,13 @@ /* - * Copyright 2016 - 2020 gary@drinkingtea.net + * Copyright 2016 - 2021 gary@drinkingtea.net * * 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 + namespace nostalgia::core { constexpr uint16_t Int_vblank = 1 << 0;