[keel,nostalgia] Make core calls take Context references instead of pointers

This commit is contained in:
2023-12-01 22:53:21 -06:00
parent 72c130d8a9
commit 453e08497d
16 changed files with 116 additions and 117 deletions

View File

@@ -11,12 +11,12 @@ namespace nostalgia::core {
GbaContext::GbaContext(turbine::Context *tctx) noexcept: turbineCtx(tctx) {
}
ox::Error initGfx(Context *ctx, InitParams const&) noexcept;
ox::Error initGfx(Context &ctx, InitParams const&) noexcept;
ox::Result<ox::UniquePtr<Context>> init(turbine::Context *tctx, InitParams const&params) noexcept {
auto ctx = ox::make_unique<GbaContext>(tctx);
oxReturnError(initGfx(ctx.get(), params));
return ox::UPtr<Context>(ctx.release());
ox::Result<ox::UniquePtr<Context>> init(turbine::Context &tctx, InitParams const&params) noexcept {
auto ctx = ox::make_unique<GbaContext>(&tctx);
oxReturnError(initGfx(*ctx, params));
return ox::UPtr<Context>(std::move(ctx));
}
}

View File

@@ -71,42 +71,43 @@ constexpr ox::Error model(auto *io, ox::CommonPtrWith<GbaTileMapTarget> auto *t)
return io->template field<uint8_t, decltype(handleTileMap)>("tileMap", handleTileMap);
}
ox::Error initGfx(Context*, InitParams const&) noexcept {
ox::Error initGfx(Context&, InitParams const&) noexcept {
for (auto bgCtl = &REG_BG0CTL; bgCtl <= &REG_BG3CTL; bgCtl += 2) {
teagba::bgSetSbb(bgCtl, 28);
}
return {};
}
void shutdownGfx(Context*) noexcept {
}
uint8_t bgStatus(Context*) noexcept {
uint8_t bgStatus(Context&) noexcept {
return (REG_DISPCTL >> 8u) & 0b1111u;
}
void setBgStatus(Context*, uint32_t status) noexcept {
void setBgStatus(Context&, uint32_t status) noexcept {
constexpr auto BgStatus = 8;
REG_DISPCTL = (REG_DISPCTL & ~0b111100000000u) | status << BgStatus;
}
bool bgStatus(Context*, unsigned bg) noexcept {
bool bgStatus(Context&, unsigned bg) noexcept {
return (REG_DISPCTL >> (8 + bg)) & 1;
}
void setBgStatus(Context*, unsigned bg, bool status) noexcept {
void setBgStatus(Context&, unsigned bg, bool status) noexcept {
constexpr auto Bg0Status = 8;
const auto mask = static_cast<uint32_t>(status) << (Bg0Status + bg);
REG_DISPCTL = REG_DISPCTL | ((REG_DISPCTL & ~mask) | mask);
}
void setBgCbb(Context*, unsigned bgIdx, unsigned cbb) noexcept {
static void setBgCbb(Context*, unsigned bgIdx, unsigned cbb) noexcept {
auto &bgCtl = regBgCtl(bgIdx);
const auto &cbbData = g_cbbData[cbb];
teagba::bgSetBpp(&bgCtl, cbbData.bpp);
teagba::bgSetCbb(&bgCtl, cbb);
}
void setBgCbb(Context&, unsigned bgIdx, unsigned cbb) noexcept {
setBgCbb(nullptr, bgIdx, cbb);
}
static ox::Error loadBgTileSheet(
const ox::MemFS &rom,
unsigned cbb,
@@ -136,20 +137,20 @@ static ox::Error loadBgTileSheet(
}
ox::Error loadBgTileSheet(
Context *ctx,
Context &ctx,
unsigned cbb,
ox::FileAddress const&tilesheetAddr,
ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(*ctx);
auto &gctx = static_cast<GbaContext&>(ctx);
auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
return loadBgTileSheet(rom, cbb, tilesheetAddr, paletteAddr);
}
ox::Error loadSpriteTileSheet(
Context *ctx,
Context &ctx,
ox::FileAddress const&tilesheetAddr,
ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(*ctx);
auto &gctx = static_cast<GbaContext&>(ctx);
auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
oxRequire(tsStat, gctx.rom().stat(tilesheetAddr));
oxRequire(ts, rom.directAccess(tilesheetAddr));
@@ -166,8 +167,8 @@ ox::Error loadSpriteTileSheet(
return {};
}
ox::Error loadBgPalette(Context *ctx, unsigned, ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(*ctx);
ox::Error loadBgPalette(Context &ctx, unsigned, ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(ctx);
auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
GbaPaletteTarget target;
target.palette = MEM_BG_PALETTE;
@@ -177,8 +178,8 @@ ox::Error loadBgPalette(Context *ctx, unsigned, ox::FileAddress const&paletteAdd
return {};
}
ox::Error loadSpritePalette(Context *ctx, unsigned cbb, ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(*ctx);
ox::Error loadSpritePalette(Context &ctx, unsigned cbb, ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(ctx);
auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
GbaPaletteTarget target;
target.palette = &MEM_SPRITE_PALETTE[cbb];
@@ -188,25 +189,16 @@ ox::Error loadSpritePalette(Context *ctx, unsigned cbb, ox::FileAddress const&pa
return {};
}
// Do NOT rely on Context in the GBA version of this function.
ox::Error initConsole(Context *ctx) noexcept {
ox::Error initConsole(Context &ctx) noexcept {
constexpr ox::FileAddress TilesheetAddr("/TileSheets/Charset.ng");
constexpr ox::FileAddress PaletteAddr("/Palettes/Charset.npal");
setBgStatus(ctx, 0b0001);
if (!ctx) {
oxRequire(rom, keel::loadRom());
const ox::FileSystem32 romFs(ox::FileStore32(rom, 32 * ox::units::MB));
oxReturnError(loadBgTileSheet(romFs, 0, TilesheetAddr, PaletteAddr));
setBgCbb(nullptr, 0, 0);
return {};
} else {
oxReturnError(loadBgTileSheet(ctx, 0, TilesheetAddr, PaletteAddr));
setBgCbb(ctx, 0, 0);
return {};
}
oxReturnError(loadBgTileSheet(ctx, 0, TilesheetAddr, PaletteAddr));
setBgCbb(ctx, 0, 0);
return {};
}
void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept {
void puts(Context &ctx, int column, int row, ox::CRStringView str) noexcept {
const auto col = static_cast<unsigned>(column);
for (auto i = 0u; i < str.bytes(); i++) {
const auto c = charMap[static_cast<std::size_t>(str[i])];
@@ -214,18 +206,18 @@ void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept {
}
}
void setTile(Context*, unsigned bgIdx, int column, int row, uint8_t tile) noexcept {
void setTile(Context&, unsigned bgIdx, int column, int row, uint8_t tile) noexcept {
const auto tileIdx = static_cast<std::size_t>(row * GbaTileColumns + column);
MEM_BG_MAP[bgIdx][tileIdx] = tile;
}
// Do NOT use Context in the GBA version of this function.
void clearTileLayer(Context*, unsigned bgIdx) noexcept {
void clearTileLayer(Context&, unsigned bgIdx) noexcept {
memset(MEM_BG_MAP[bgIdx].data(), 0, GbaTileRows * GbaTileColumns);
}
[[maybe_unused]]
void hideSprite(Context*, unsigned idx) noexcept {
void hideSprite(Context&, unsigned idx) noexcept {
//oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow");
teagba::GbaSpriteAttrUpdate oa;
oa.attr0 = 2 << 8;
@@ -233,7 +225,7 @@ void hideSprite(Context*, unsigned idx) noexcept {
teagba::addSpriteUpdate(oa);
}
void setSprite(Context*,
void setSprite(Context&,
unsigned idx,
int x,
int y,

View File

@@ -7,5 +7,5 @@
#include <nostalgia/core/context.hpp>
namespace nostalgia::core {
ox::Error initGfx(Context *ctx, InitParams const&) noexcept;
ox::Error initGfx(Context &ctx, InitParams const&) noexcept;
}

View File

@@ -3,33 +3,44 @@
*/
#include <nostalgia/core/core.hpp>
#include <ox/std/def.hpp>
#include <keel/media.hpp>
#include <turbine/turbine.hpp>
#include <teagba/addresses.hpp>
#include <teagba/bios.hpp>
#include <teagba/gfx.hpp>
#include <nostalgia/core/core.hpp>
#include "gfx.hpp"
#define HEAP_BEGIN (reinterpret_cast<char*>(MEM_EWRAM_BEGIN))
#define HEAP_SIZE ((MEM_EWRAM_END - MEM_EWRAM_BEGIN) / 2)
#define HEAP_END (reinterpret_cast<char*>(MEM_EWRAM_BEGIN + HEAP_SIZE))
namespace ox {
using namespace nostalgia::core;
void panic(const char *file, int line, const char *panicMsg, ox::Error const&err) noexcept {
oxIgnoreError(initGfx(nullptr, {}));
oxIgnoreError(initConsole(nullptr));
// enable only BG 0
REG_DISPCTL = teagba::DispCtl_Bg0;
clearTileLayer(nullptr, 0);
// reset heap to make sure we have enough memory to allocate context data
ox::heapmgr::initHeap(HEAP_BEGIN, HEAP_END);
auto tctx = turbine::init(keel::loadRomFs("").unwrap(), "Nostalgia").unwrap();
auto ctx = init(*tctx).unwrap();
oxIgnoreError(initGfx(*ctx, {}));
oxIgnoreError(initConsole(*ctx));
setBgStatus(*ctx, 0, true);
clearTileLayer(*ctx, 0);
ox::BString<23> serr = "Error code: ";
serr += static_cast<int64_t>(err);
puts(nullptr, 32 + 1, 1, "SADNESS...");
puts(nullptr, 32 + 1, 4, "UNEXPECTED STATE:");
puts(nullptr, 32 + 2, 6, panicMsg);
puts(*ctx, 32 + 1, 1, "SADNESS...");
puts(*ctx, 32 + 1, 4, "UNEXPECTED STATE:");
puts(*ctx, 32 + 2, 6, panicMsg);
if (err) {
puts(nullptr, 32 + 2, 8, serr);
puts(*ctx, 32 + 2, 8, serr);
}
puts(nullptr, 32 + 1, 15, "PLEASE RESTART THE SYSTEM");
puts(*ctx, 32 + 1, 15, "PLEASE RESTART THE SYSTEM");
// print to terminal if in mGBA
oxErrf("\033[31;1;1mPANIC:\033[0m [{}:{}]: {}\n", file, line, panicMsg);
if (err.msg) {