From afab2f687876515bbc8dbb64ba080d70c9763ee2 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 1 Feb 2020 00:47:08 -0600 Subject: [PATCH 01/18] [nostalgia/core] Add loadRomFs --- src/nostalgia/core/media.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/nostalgia/core/media.cpp diff --git a/src/nostalgia/core/media.cpp b/src/nostalgia/core/media.cpp new file mode 100644 index 000000000..51fd95468 --- /dev/null +++ b/src/nostalgia/core/media.cpp @@ -0,0 +1,23 @@ +/* + * 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 "media.hpp" +#include "ox/std/bit.hpp" + +namespace nostalgia::core { + +ox::FileSystem *loadRomFs(const char *path) { + auto rom = loadRom(path); + return new ox::FileSystem32(rom, 32 * ox::units::MB, [](uint8_t *buff) { + unloadRom(ox::bit_cast(buff)); + }); +} + +} From e59499b1273a965a707534d0cc07a02e6a1821f3 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 Feb 2020 00:07:37 -0600 Subject: [PATCH 02/18] [ox/std] Fix oxIgnoreError to not completely ignore the statement it encompases --- deps/ox/src/ox/std/error.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/deps/ox/src/ox/std/error.hpp b/deps/ox/src/ox/std/error.hpp index 6f157491d..9ab7a6faa 100644 --- a/deps/ox/src/ox/std/error.hpp +++ b/deps/ox/src/ox/std/error.hpp @@ -18,10 +18,6 @@ #define OxError(x) static_cast(x) #endif -#define oxIgnoreError(x) -#define oxReturnError(x) if (const auto _ox_error = ox::error::toError(x)) return _ox_error -#define oxThrowError(x) if (const auto _ox_error = ox::error::toError(x)) throw _ox_error - namespace ox { struct BaseError { @@ -95,3 +91,7 @@ template } +inline void oxIgnoreError(ox::Error) {} +#define oxReturnError(x) if (const auto _ox_error = ox::error::toError(x)) return _ox_error +#define oxThrowError(x) if (const auto _ox_error = ox::error::toError(x)) throw _ox_error + From ea14ccac3a5a57bc475c23d6d44f249411e8f648 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 Feb 2020 21:10:36 -0600 Subject: [PATCH 03/18] [ox/std] Leave panic unimplemented when C++ stdlib is absent --- deps/ox/src/ox/std/assert.cpp | 6 +++--- deps/ox/src/ox/std/assert.hpp | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/deps/ox/src/ox/std/assert.cpp b/deps/ox/src/ox/std/assert.cpp index e2713cc37..8cba5ffe2 100644 --- a/deps/ox/src/ox/std/assert.cpp +++ b/deps/ox/src/ox/std/assert.cpp @@ -34,12 +34,12 @@ void assertFunc([[maybe_unused]]const char *file, [[maybe_unused]]int line template<> void assertFunc(const char *file, int line, Error err, const char *msg) { if (err) { - panic(file, line, err, msg); + panic(file, line, msg, err); } } -void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]Error err, [[maybe_unused]]const char *msg) { #if defined(OX_USE_STDLIB) +void panic(const char *file, int line, const char *msg, Error err) { std::cerr << "\033[31;1;1mPANIC:\033[0m (" << file << ':' << line << "): " << msg << '\n'; std::cerr << "\tError Code:\t" << err << '\n'; if (err.file != nullptr) { @@ -48,7 +48,7 @@ void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_u printStackTrace(2); oxTrace("assert").del("") << "Failed assert: " << msg << " (" << file << ":" << line << ")"; std::abort(); -#endif } +#endif } diff --git a/deps/ox/src/ox/std/assert.hpp b/deps/ox/src/ox/std/assert.hpp index 424bf697f..fd7e19f89 100644 --- a/deps/ox/src/ox/std/assert.hpp +++ b/deps/ox/src/ox/std/assert.hpp @@ -23,14 +23,14 @@ void assertFunc(const char *file, int line, bool pass, const char *msg); template<> void assertFunc(const char *file, int line, Error err, const char*); -void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]Error err, [[maybe_unused]]const char *msg); +void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]const char *msg, [[maybe_unused]]Error err = OxError(0)); } +#define oxPanic(pass, msg) ox::panic(__FILE__, __LINE__, pass, msg) #ifndef NDEBUG #define oxAssert(pass, msg) ox::assertFunc(__FILE__, __LINE__, pass, msg) -#define oxPanic(pass, msg) ox::panic(__FILE__, __LINE__, pass, msg) #else -#define oxAssert(pass, msg) -#define oxPanic(pass, msg) +inline void oxAssert(bool, const char*) {} +inline void oxAssert(ox::Error, const char*) {} #endif From fffc66f18b84ea1771d9d9a3cf1fc532247fd94d Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 Feb 2020 21:17:10 -0600 Subject: [PATCH 04/18] [nostalgia/core/gba] Add test for malloc and unify panic systems --- src/nostalgia/core/gba/CMakeLists.txt | 56 +++++++++++++--------- src/nostalgia/core/gba/gfx.cpp | 3 +- src/nostalgia/core/gba/mem.cpp | 67 +++++++++++++++++---------- src/nostalgia/core/gba/panic.cpp | 12 +++-- src/nostalgia/core/gba/panic.hpp | 16 ------- src/nostalgia/core/gba/tests.cpp | 54 +++++++++++++++++++++ 6 files changed, 141 insertions(+), 67 deletions(-) delete mode 100644 src/nostalgia/core/gba/panic.hpp create mode 100644 src/nostalgia/core/gba/tests.cpp diff --git a/src/nostalgia/core/gba/CMakeLists.txt b/src/nostalgia/core/gba/CMakeLists.txt index e5a80d68b..a1a579420 100644 --- a/src/nostalgia/core/gba/CMakeLists.txt +++ b/src/nostalgia/core/gba/CMakeLists.txt @@ -1,23 +1,35 @@ -add_library( - NostalgiaCore-GBA - core.cpp - gfx.cpp - media.cpp - mem.cpp - panic.cpp -) - -target_link_libraries( - NostalgiaCore-GBA PUBLIC - NostalgiaCore - GbaStartup - OxFS - OxStd -) - -install( - TARGETS +if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA") + add_library( NostalgiaCore-GBA - DESTINATION - include/nostalgia/core -) + core.cpp + gfx.cpp + media.cpp + mem.cpp + panic.cpp + ) + + target_link_libraries( + NostalgiaCore-GBA PUBLIC + NostalgiaCore + GbaStartup + OxFS + OxStd + ) + + install( + TARGETS + NostalgiaCore-GBA + DESTINATION + include/nostalgia/core + ) +endif() + +# tests +if(NOSTALGIA_BUILD_TYPE STREQUAL "Native") + add_executable(NostalgiaCore-GBA_Test + tests.cpp + mem.cpp + ) + target_link_libraries(NostalgiaCore-GBA_Test NostalgiaCore) + add_test("NostalgiaCore-GBA\\ Test\\ malloc" NostalgiaCore-GBA_Test malloc) +endif() \ No newline at end of file diff --git a/src/nostalgia/core/gba/gfx.cpp b/src/nostalgia/core/gba/gfx.cpp index 81678c855..5dac22da7 100644 --- a/src/nostalgia/core/gba/gfx.cpp +++ b/src/nostalgia/core/gba/gfx.cpp @@ -14,7 +14,6 @@ #include #include "addresses.hpp" -#include "panic.hpp" namespace nostalgia::core { @@ -102,7 +101,7 @@ ox::Error shutdownGfx() { case 3: return REG_BG3CNT; default: - panic("Looking up non-existent register"); + oxPanic("Looking up non-existent register", OxError(1)); return REG_BG0CNT; } } diff --git a/src/nostalgia/core/gba/mem.cpp b/src/nostalgia/core/gba/mem.cpp index a2d351d39..33206430a 100644 --- a/src/nostalgia/core/gba/mem.cpp +++ b/src/nostalgia/core/gba/mem.cpp @@ -7,11 +7,9 @@ */ #include "addresses.hpp" -#include "ox/std/bit.hpp" -#include "ox/std/types.hpp" -#include "panic.hpp" -#include +#include +#include // this warning is too dumb to realize that it can actually confirm the hard // coded address aligns with the requirement of HeapSegment, so it must be @@ -25,6 +23,10 @@ namespace nostalgia::core { +static class HeapSegment *volatile g_heapBegin = nullptr; +static class HeapSegment *volatile g_heapEnd = nullptr; +static class HeapSegment *volatile heapIdx = nullptr; + static constexpr std::size_t alignedSize(std::size_t sz) { return sz + (sz & 7); } @@ -38,39 +40,35 @@ struct HeapSegment { std::size_t size; uint8_t inUse; - void init(std::size_t maxSize = ox::bit_cast(HEAP_END)) { - this->size = maxSize - reinterpret_cast(this); + void init(std::size_t maxSize = ox::bit_cast(g_heapEnd)) { + this->size = maxSize - ox::bit_cast(this); this->inUse = false; } template T *data() { - return reinterpret_cast(reinterpret_cast(this) + alignedSize(this)); + return ox::bit_cast(ox::bit_cast(this) + alignedSize(this)); } template T *end() { const auto size = alignedSize(this) + alignedSize(this->size); - auto e = reinterpret_cast(reinterpret_cast(this) + size); - return reinterpret_cast(e); + auto e = ox::bit_cast(ox::bit_cast(this) + size); + return ox::bit_cast(e); } }; -static HeapSegment *volatile heapIdx = nullptr; - -void initHeap() { - heapIdx = HEAP_BEGIN; - heapIdx->init(); +void initHeap(char *heapBegin, char *heapEnd) { + g_heapBegin = ox::bit_cast(heapBegin); + g_heapEnd = ox::bit_cast(heapEnd); + heapIdx = g_heapBegin; + heapIdx->size = ox::bit_cast(heapEnd) - ox::bit_cast(heapIdx); + heapIdx->inUse = false; } -static HeapSegment *findSegmentFor(std::size_t sz) { - for (auto s = HEAP_BEGIN; s + sz < HEAP_END; s = s->end()) { - if (s->size >= sz && !s->inUse) { - return s; - } - } - return nullptr; +void initHeap() { + initHeap(ox::bit_cast(HEAP_BEGIN), ox::bit_cast(HEAP_END)); } struct SegmentPair { @@ -90,6 +88,16 @@ static SegmentPair findSegmentOf(void *ptr) { return {}; } +static HeapSegment *findSegmentFor(std::size_t sz) { + for (auto s = g_heapBegin; s <= g_heapEnd; s = s->end()) { + if (s->size >= sz && !s->inUse) { + return s; + } + } + oxPanic("malloc: could not find segment", OxError(1)); + return nullptr; +} + [[nodiscard]] void *malloc(std::size_t allocSize) { const auto targetSize = alignedSize(sizeof(HeapSegment)) + alignedSize(allocSize); auto seg = findSegmentFor(targetSize); @@ -99,9 +107,8 @@ static SegmentPair findSegmentOf(void *ptr) { const auto bytesRemaining = seg->size - targetSize; seg->size = targetSize; seg->inUse = true; - auto out = seg->data(); seg->end()->init(bytesRemaining); - return out; + return seg->data(); } void free(void *ptr) { @@ -111,12 +118,14 @@ void free(void *ptr) { } else if (p.segment) { p.segment->inUse = false; } else { - panic("Bad heap free"); + oxPanic("Bad heap free", OxError(1)); } } } +#ifndef OX_USE_STDLIB + using namespace nostalgia; void *operator new(std::size_t allocSize) { @@ -142,3 +151,13 @@ void operator delete(void *ptr, unsigned) { void operator delete[](void *ptr, unsigned) { core::free(ptr); } + +void operator delete(void *ptr, unsigned long int) { + core::free(ptr); +} + +void operator delete[](void *ptr, unsigned long int) { + core::free(ptr); +} + +#endif \ No newline at end of file diff --git a/src/nostalgia/core/gba/panic.cpp b/src/nostalgia/core/gba/panic.cpp index 9f6b2d074..95ab46ba3 100644 --- a/src/nostalgia/core/gba/panic.cpp +++ b/src/nostalgia/core/gba/panic.cpp @@ -7,15 +7,21 @@ */ #include "../core.hpp" -#include "panic.hpp" +#include "../gfx.hpp" -namespace nostalgia::core { +namespace ox { -void panic(const char *msg) { +using namespace nostalgia::core; + +void panic(const char*, int, const char *msg, ox::Error err) { + oxIgnoreError(initGfx(nullptr)); oxIgnoreError(initConsole(nullptr)); + ox::BString<23> serr = "Error code: "; + serr += err; puts(nullptr, 32 + 0, 1, "SADNESS..."); puts(nullptr, 32 + 0, 4, "UNEXPECTED STATE:"); puts(nullptr, 32 + 2, 6, msg); + puts(nullptr, 32 + 2, 7, serr.c_str()); puts(nullptr, 32 + 0, 10, "PLEASE RESTART THE SYSTEM"); // TODO: properly end program execution, this wastes power while (1); diff --git a/src/nostalgia/core/gba/panic.hpp b/src/nostalgia/core/gba/panic.hpp deleted file mode 100644 index c4373ce6b..000000000 --- a/src/nostalgia/core/gba/panic.hpp +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright 2016 - 2019 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 - -namespace nostalgia::core { - -void panic(const char *msg); - -} - diff --git a/src/nostalgia/core/gba/tests.cpp b/src/nostalgia/core/gba/tests.cpp new file mode 100644 index 000000000..bd0e4ff19 --- /dev/null +++ b/src/nostalgia/core/gba/tests.cpp @@ -0,0 +1,54 @@ + +#include +#include +#include + +#include + +#include + +namespace nostalgia::core { + +[[nodiscard]] void *malloc(std::size_t allocSize); + +void free(void *ptr); + +void panic(const char *msg) { + oxPanic(msg, OxError(1)); +} + +void initHeap(char *heapBegin, char *heapEnd); + +} + +using namespace nostalgia; + +int testMalloc(std::string) { + std::vector buff(ox::units::MB); + core::initHeap(&buff.front(), &buff.back()); + oxAssert(core::malloc(5) != nullptr, "malloc is broken"); + oxAssert(core::malloc(5) != nullptr, "malloc is broken"); + return 0; +} + +std::map tests = { + { + { "malloc", testMalloc }, + } +}; + +int main(int argc, const char **args) { + int retval = -1; + if (argc > 1) { + auto testName = args[1]; + std::string testArg = ""; + if (args[2]) { + testArg = args[2]; + } + if (tests.find(testName) != tests.end()) { + retval = tests[testName](testArg); + } + } + return retval; +} + From 90f94dbfc2ae391cb92c74622b8c8ca693db7c99 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 Feb 2020 21:34:55 -0600 Subject: [PATCH 05/18] [nostalgia/core] Add PassthroughFS support to loadRomFs --- src/nostalgia/core/core.cpp | 5 +---- src/nostalgia/core/media.cpp | 22 +++++++++++++++------- src/nostalgia/core/media.hpp | 4 +++- src/nostalgia/player/main.cpp | 24 ++++-------------------- 4 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/nostalgia/core/core.cpp b/src/nostalgia/core/core.cpp index 131178ecd..f536680a4 100644 --- a/src/nostalgia/core/core.cpp +++ b/src/nostalgia/core/core.cpp @@ -12,10 +12,7 @@ namespace nostalgia::core { ox::Error init(Context *ctx) { - auto err = OxError(0); - err = initGfx(ctx); - initHeap(); // this does nothing in userland builds - return err; + return initGfx(ctx); } } diff --git a/src/nostalgia/core/media.cpp b/src/nostalgia/core/media.cpp index 51fd95468..8eff9466f 100644 --- a/src/nostalgia/core/media.cpp +++ b/src/nostalgia/core/media.cpp @@ -6,18 +6,26 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include - #include "media.hpp" -#include "ox/std/bit.hpp" namespace nostalgia::core { ox::FileSystem *loadRomFs(const char *path) { - auto rom = loadRom(path); - return new ox::FileSystem32(rom, 32 * ox::units::MB, [](uint8_t *buff) { - unloadRom(ox::bit_cast(buff)); - }); + const auto lastDot = ox_lastIndexOf(path, '.'); + const auto fsExt = lastDot != -1 ? path + lastDot : ""; + if (ox_strcmp(fsExt, ".oxfs") == 0) { + auto rom = core::loadRom(path); + if (!rom) { + return nullptr; + } + return new ox::FileSystem32(rom, 32 * ox::units::MB, unloadRom); + } else { +#ifdef OX_HAS_PASSTHROUGHFS + return new ox::PassThroughFS(path); +#else + return nullptr; +#endif + } } } diff --git a/src/nostalgia/core/media.hpp b/src/nostalgia/core/media.hpp index 06bac96d9..6d6ccb9cb 100644 --- a/src/nostalgia/core/media.hpp +++ b/src/nostalgia/core/media.hpp @@ -8,10 +8,12 @@ #pragma once -#include +#include namespace nostalgia::core { +ox::FileSystem *loadRomFs(const char *path); + char *loadRom(const char *path = ""); void unloadRom(char*); diff --git a/src/nostalgia/player/main.cpp b/src/nostalgia/player/main.cpp index d5ff48ed6..cb29cdea3 100644 --- a/src/nostalgia/player/main.cpp +++ b/src/nostalgia/player/main.cpp @@ -8,6 +8,7 @@ #include #include +#include #include using namespace nostalgia; @@ -28,29 +29,12 @@ ox::Error run(ox::FileSystem *fs) { int main(int argc, const char **argv) { if (argc > 1) { - ox::FileSystem *fs = nullptr; - char *rom = nullptr; auto path = argv[1]; - const auto lastDot = ox_lastIndexOf(path, '.'); - const auto fsExt = lastDot != -1 ? path + lastDot : ""; - if (ox_strcmp(fsExt, ".oxfs") == 0) { - rom = core::loadRom(path); - if (!rom) { - return 1; - } - fs = new (ox_alloca(sizeof(ox::FileStore32))) ox::FileSystem32(ox::FileStore32(rom, 32 * ox::units::MB)); - } else { -#ifdef OX_HAS_PASSTHROUGHFS - fs = new (ox_alloca(sizeof(ox::PassThroughFS))) ox::PassThroughFS(path); -#else - return 2; -#endif - } + auto fs = core::loadRomFs(path); auto err = run(fs); oxAssert(err, "Something went wrong..."); - fs->~FileSystem(); - core::unloadRom(rom); + delete fs; return err; } - return 3; + return 1; } From 553cde0d0c2b266166a78f193b8343ee4b401bdb Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 Feb 2020 21:42:09 -0600 Subject: [PATCH 06/18] [ox/fs] Replace uint8_t with char for buffers --- deps/ox/src/ox/fs/filestore/filestoretemplate.hpp | 6 +++--- deps/ox/src/ox/fs/filesystem/filesystem.hpp | 12 ++++++------ deps/ox/src/ox/fs/filesystem/passthroughfs.cpp | 2 +- deps/ox/src/ox/fs/filesystem/passthroughfs.hpp | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index 4d7ccdb46..ac1546e87 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -122,7 +122,7 @@ class FileStoreTemplate { [[nodiscard]] InodeId_t available(); - [[nodiscard]] uint8_t *buff(); + [[nodiscard]] char *buff(); [[nodiscard]] Error walk(Error(*cb)(uint8_t, uint64_t, uint64_t)); @@ -464,8 +464,8 @@ typename FileStoreTemplate::InodeId_t FileStoreTemplate::availab } template -uint8_t *FileStoreTemplate::buff() { - return reinterpret_cast(m_buffer); +char *FileStoreTemplate::buff() { + return reinterpret_cast(m_buffer); } template diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.hpp b/deps/ox/src/ox/fs/filesystem/filesystem.hpp index a14957d84..27c612bba 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.hpp @@ -70,7 +70,7 @@ class FileSystem { [[nodiscard]] virtual uint64_t size() const = 0; - [[nodiscard]] virtual uint8_t *buff() = 0; + [[nodiscard]] virtual char *buff() = 0; [[nodiscard]] virtual ox::Error walk(ox::Error(*cb)(uint8_t, uint64_t, uint64_t)) = 0; @@ -94,12 +94,12 @@ class FileSystemTemplate: public FileSystem { }; FileStore m_fs; - void(*m_freeBuffer)(uint8_t*) = nullptr; + void(*m_freeBuffer)(char*) = nullptr; public: FileSystemTemplate() = default; - FileSystemTemplate(void *buffer, uint64_t bufferSize, void(*freeBuffer)(uint8_t*) = [] (uint8_t *buff) { delete buff; }); + FileSystemTemplate(void *buffer, uint64_t bufferSize, void(*freeBuffer)(char*) = [] (char *buff) { delete buff; }); FileSystemTemplate(FileStore fs); @@ -147,7 +147,7 @@ class FileSystemTemplate: public FileSystem { uint64_t size() const override; - uint8_t *buff() override; + char *buff() override; [[nodiscard]] ox::Error walk(ox::Error(*cb)(uint8_t, uint64_t, uint64_t)) override; @@ -171,7 +171,7 @@ FileSystemTemplate::FileSystemTemplate(FileStore fs) { } template -FileSystemTemplate::FileSystemTemplate(void *buffer, uint64_t bufferSize, void(*freeBuffer)(uint8_t*)): +FileSystemTemplate::FileSystemTemplate(void *buffer, uint64_t bufferSize, void(*freeBuffer)(char*)): m_fs(buffer, bufferSize), m_freeBuffer(freeBuffer) { } @@ -363,7 +363,7 @@ uint64_t FileSystemTemplate::size() const { } template -uint8_t *FileSystemTemplate::buff() { +char *FileSystemTemplate::buff() { return m_fs.buff(); } diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp index f99f76dac..30c18ee32 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp @@ -140,7 +140,7 @@ uint64_t PassThroughFS::size() const { return s.capacity; } -uint8_t *PassThroughFS::buff() { +char *PassThroughFS::buff() { return nullptr; } diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp index 1c00313b9..c9366f102 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -72,7 +72,7 @@ class PassThroughFS: public FileSystem { uint64_t size() const override; - uint8_t *buff() override; + char *buff() override; ox::Error walk(Error(*cb)(uint8_t, uint64_t, uint64_t)) override; From 9d4d3cd331313e00c28fa355ec587f1a7e1a014a Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 Feb 2020 21:45:48 -0600 Subject: [PATCH 07/18] [nostalgia/core/gba] Move cstartup.cpp to NostalgiaCore-GBA --- deps/gbastartup/CMakeLists.txt | 1 - src/nostalgia/core/gba/CMakeLists.txt | 1 + .../nostalgia/core/gba}/cstartup.cpp | 15 +++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) rename {deps/gbastartup => src/nostalgia/core/gba}/cstartup.cpp (68%) diff --git a/deps/gbastartup/CMakeLists.txt b/deps/gbastartup/CMakeLists.txt index 203bbb72b..1adae538e 100644 --- a/deps/gbastartup/CMakeLists.txt +++ b/deps/gbastartup/CMakeLists.txt @@ -2,7 +2,6 @@ enable_language(C ASM) add_library( GbaStartup gba_crt0.s - cstartup.cpp ) target_link_libraries( diff --git a/src/nostalgia/core/gba/CMakeLists.txt b/src/nostalgia/core/gba/CMakeLists.txt index a1a579420..c112c0cdd 100644 --- a/src/nostalgia/core/gba/CMakeLists.txt +++ b/src/nostalgia/core/gba/CMakeLists.txt @@ -1,6 +1,7 @@ if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA") add_library( NostalgiaCore-GBA + cstartup.cpp core.cpp gfx.cpp media.cpp diff --git a/deps/gbastartup/cstartup.cpp b/src/nostalgia/core/gba/cstartup.cpp similarity index 68% rename from deps/gbastartup/cstartup.cpp rename to src/nostalgia/core/gba/cstartup.cpp index 22a1710df..80952fc2e 100644 --- a/deps/gbastartup/cstartup.cpp +++ b/src/nostalgia/core/gba/cstartup.cpp @@ -1,3 +1,10 @@ +/* + * 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 GCC diagnostic push #pragma GCC diagnostic ignored "-Wpedantic" @@ -7,6 +14,13 @@ extern void (*__preinit_array_end[]) (void); extern void (*__init_array_start[]) (void); extern void (*__init_array_end[]) (void); +namespace nostalgia::core { + +void initHeap(); + +} + + extern "C" { void __libc_init_array() { @@ -24,6 +38,7 @@ int main(int argc, const char **argv); int c_start() { const char *args[2] = {"", "rom.oxfs"}; + nostalgia::core::initHeap(); return main(2, args); } From 87968a87f0309936f95c80f3636b3599aee1cc18 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 Feb 2020 21:47:48 -0600 Subject: [PATCH 08/18] [nostalgia/core/gba] Fix memory range --- src/nostalgia/core/gba/addresses.hpp | 2 +- src/nostalgia/core/gba/media.cpp | 2 +- src/nostalgia/core/gba/tests.cpp | 4 ---- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/nostalgia/core/gba/addresses.hpp b/src/nostalgia/core/gba/addresses.hpp index 6cf22c395..e126cec3b 100644 --- a/src/nostalgia/core/gba/addresses.hpp +++ b/src/nostalgia/core/gba/addresses.hpp @@ -45,7 +45,7 @@ typedef uint16_t BgMapTile[1024]; #define MEM_BG_MAP reinterpret_cast(0x06000000) -#define MEM_ROM *reinterpret_cast(0x08000000) +#define MEM_ROM reinterpret_cast(0x08000000) #define MEM_WRAM_BEGIN reinterpret_cast(0x02000000) #define MEM_WRAM_END reinterpret_cast(0x0203FFFF) diff --git a/src/nostalgia/core/gba/media.cpp b/src/nostalgia/core/gba/media.cpp index 0719af06f..c8e50db6c 100644 --- a/src/nostalgia/core/gba/media.cpp +++ b/src/nostalgia/core/gba/media.cpp @@ -23,7 +23,7 @@ char *loadRom(const char*) { constexpr auto headerP2Len = 16; constexpr auto headerLen = headerP1Len + headerP2Len + 1; - for (auto current = &MEM_ROM; current < reinterpret_cast(0x0a000000); current += headerLen) { + for (auto current = MEM_ROM; current < reinterpret_cast(0x0a000000); current += headerLen) { if (ox_memcmp(current, headerP1, headerP1Len) == 0 && ox_memcmp(current + headerP1Len, headerP2, headerP2Len) == 0) { return current + headerLen; diff --git a/src/nostalgia/core/gba/tests.cpp b/src/nostalgia/core/gba/tests.cpp index bd0e4ff19..3d45eff98 100644 --- a/src/nostalgia/core/gba/tests.cpp +++ b/src/nostalgia/core/gba/tests.cpp @@ -13,10 +13,6 @@ namespace nostalgia::core { void free(void *ptr); -void panic(const char *msg) { - oxPanic(msg, OxError(1)); -} - void initHeap(char *heapBegin, char *heapEnd); } From 2c582617e33f8228e1958f65a28797852863c47d Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 Feb 2020 21:48:48 -0600 Subject: [PATCH 09/18] [nostalgia/core] Always run GBA CMake file and add media.cpp --- src/nostalgia/core/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nostalgia/core/CMakeLists.txt b/src/nostalgia/core/CMakeLists.txt index 41701a343..b7b6e6b3c 100644 --- a/src/nostalgia/core/CMakeLists.txt +++ b/src/nostalgia/core/CMakeLists.txt @@ -2,6 +2,7 @@ add_library( NostalgiaCore core.cpp gfx.cpp + media.cpp ) target_link_libraries( @@ -9,9 +10,8 @@ target_link_libraries( OxFS ) -if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA") - add_subdirectory(gba) -elseif(NOSTALGIA_BUILD_TYPE STREQUAL "Native") +add_subdirectory(gba) +if(NOSTALGIA_BUILD_TYPE STREQUAL "Native") add_subdirectory(sdl) add_subdirectory(userland) endif() From 29734d5ee1b3ff85099b550e3c20ba28564fe363 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 Feb 2020 21:58:24 -0600 Subject: [PATCH 10/18] [nostalgia/tools/pack] Fix oxAssert to use new parameter order --- src/nostalgia/tools/pack.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/nostalgia/tools/pack.cpp b/src/nostalgia/tools/pack.cpp index faa352b4b..707bf4460 100644 --- a/src/nostalgia/tools/pack.cpp +++ b/src/nostalgia/tools/pack.cpp @@ -6,8 +6,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include - #include #include #include @@ -54,7 +52,7 @@ int main(int argc, const char **args) { try { run(ox::ClArgs(argc, args)); } catch (const ox::Error &err) { - oxPanic(err, "pack failed"); + oxPanic("pack failed", err); std::cerr << "pack failed...\n"; return static_cast(err); } From a66f1499a4003957cf5690f5e4f3902716c3742d Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 Feb 2020 23:18:18 -0600 Subject: [PATCH 11/18] [nostalgia/core/gba] Move cstartup.cpp back to gbastartup --- deps/gbastartup/CMakeLists.txt | 1 + {src/nostalgia/core/gba => deps/gbastartup}/cstartup.cpp | 0 src/nostalgia/core/gba/CMakeLists.txt | 3 +-- 3 files changed, 2 insertions(+), 2 deletions(-) rename {src/nostalgia/core/gba => deps/gbastartup}/cstartup.cpp (100%) diff --git a/deps/gbastartup/CMakeLists.txt b/deps/gbastartup/CMakeLists.txt index 1adae538e..203bbb72b 100644 --- a/deps/gbastartup/CMakeLists.txt +++ b/deps/gbastartup/CMakeLists.txt @@ -2,6 +2,7 @@ enable_language(C ASM) add_library( GbaStartup gba_crt0.s + cstartup.cpp ) target_link_libraries( diff --git a/src/nostalgia/core/gba/cstartup.cpp b/deps/gbastartup/cstartup.cpp similarity index 100% rename from src/nostalgia/core/gba/cstartup.cpp rename to deps/gbastartup/cstartup.cpp diff --git a/src/nostalgia/core/gba/CMakeLists.txt b/src/nostalgia/core/gba/CMakeLists.txt index c112c0cdd..678f94341 100644 --- a/src/nostalgia/core/gba/CMakeLists.txt +++ b/src/nostalgia/core/gba/CMakeLists.txt @@ -1,7 +1,6 @@ if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA") add_library( NostalgiaCore-GBA - cstartup.cpp core.cpp gfx.cpp media.cpp @@ -33,4 +32,4 @@ if(NOSTALGIA_BUILD_TYPE STREQUAL "Native") ) target_link_libraries(NostalgiaCore-GBA_Test NostalgiaCore) add_test("NostalgiaCore-GBA\\ Test\\ malloc" NostalgiaCore-GBA_Test malloc) -endif() \ No newline at end of file +endif() From 4cbf1b9a9aef7de65109d835e28a0c621021c96c Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 23 Dec 2019 15:11:53 -0600 Subject: [PATCH 12/18] [ox] Get building in MSVC --- deps/ox/src/ox/fs/filesystem/passthroughfs.cpp | 3 ++- deps/ox/src/ox/fs/filesystem/passthroughfs.hpp | 4 +--- deps/ox/src/ox/mc/presenceindicator.hpp | 4 ++-- deps/ox/src/ox/std/hardware.hpp | 3 ++- deps/ox/src/ox/std/stddef.hpp | 2 +- deps/ox/src/ox/std/strops.hpp | 4 ++-- deps/ox/src/ox/std/trace.cpp | 1 - 7 files changed, 10 insertions(+), 11 deletions(-) diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp index 30c18ee32..17d0fc238 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp @@ -28,7 +28,8 @@ std::string PassThroughFS::basePath() { Error PassThroughFS::mkdir(const char *path, bool recursive) { bool success = false; const auto p = m_path / stripSlash(path); - oxTrace("ox::fs::PassThroughFS::mkdir") << p.c_str(); + const auto u8p = p.u8string(); + oxTrace("ox::fs::PassThroughFS::mkdir") << u8p.c_str(); if (recursive) { success = std::filesystem::create_directories(p); } else { diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp index c9366f102..d80398724 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -89,9 +89,7 @@ class PassThroughFS: public FileSystem { template ox::Error PassThroughFS::ls(const char *dir, F cb) { for (auto &p : std::filesystem::directory_iterator(m_path / stripSlash(dir))) { - if (auto err = cb(p.path().filename().c_str(), 0); err) { - return err; - } + oxReturnError(cb(p.path().filename().u8string(), 0)); } return OxError(0); } diff --git a/deps/ox/src/ox/mc/presenceindicator.hpp b/deps/ox/src/ox/mc/presenceindicator.hpp index 8e8c991a8..1b2c15c83 100644 --- a/deps/ox/src/ox/mc/presenceindicator.hpp +++ b/deps/ox/src/ox/mc/presenceindicator.hpp @@ -16,8 +16,8 @@ namespace ox { class FieldPresenceIndicator { private: uint8_t *m_mask = nullptr; - int m_maskLen = 0; - int m_fields = 0; + std::size_t m_maskLen = 0; + std::size_t m_fields = 0; public: FieldPresenceIndicator(uint8_t *mask, std::size_t maxLen); diff --git a/deps/ox/src/ox/std/hardware.hpp b/deps/ox/src/ox/std/hardware.hpp index 16d54a3fc..0aa270100 100644 --- a/deps/ox/src/ox/std/hardware.hpp +++ b/deps/ox/src/ox/std/hardware.hpp @@ -22,7 +22,8 @@ #else -#warn "Undefined hardware" +//TODO: fix warnning +//#warn "Undefined hardware" #endif diff --git a/deps/ox/src/ox/std/stddef.hpp b/deps/ox/src/ox/std/stddef.hpp index 1a41f50c7..d9c430350 100644 --- a/deps/ox/src/ox/std/stddef.hpp +++ b/deps/ox/src/ox/std/stddef.hpp @@ -8,7 +8,7 @@ #pragma once -#if OX_USE_STDLIB +#ifdef OX_USE_STDLIB #include #else #define offsetof(type, member) __builtin_offsetof(type, member) diff --git a/deps/ox/src/ox/std/strops.hpp b/deps/ox/src/ox/std/strops.hpp index c4537081d..4d17351fb 100644 --- a/deps/ox/src/ox/std/strops.hpp +++ b/deps/ox/src/ox/std/strops.hpp @@ -96,7 +96,7 @@ template int retval = -1; for (std::size_t i = 0; i < maxLen && str[i]; i++) { if (str[i] == character) { - retval = i; + retval = static_cast(i); } } return retval; @@ -106,7 +106,7 @@ template int retval = -1; for (std::size_t i = 0; i < maxLen && str[i]; i++) { if (str[i] == character) { - retval = i; + retval = static_cast(i); } } return retval; diff --git a/deps/ox/src/ox/std/trace.cpp b/deps/ox/src/ox/std/trace.cpp index 4811d167a..49386a5ed 100644 --- a/deps/ox/src/ox/std/trace.cpp +++ b/deps/ox/src/ox/std/trace.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #endif #include "trace.hpp" From cdf62cbb9871bb489aa4ff71ec866f8752e07cdd Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 23 Dec 2019 15:21:23 -0600 Subject: [PATCH 13/18] [nostalgia] Get building in MSVC --- src/nostalgia/CMakeLists.txt | 1 + src/nostalgia/tools/pack/pack.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/nostalgia/CMakeLists.txt b/src/nostalgia/CMakeLists.txt index 870af9e75..02b661407 100644 --- a/src/nostalgia/CMakeLists.txt +++ b/src/nostalgia/CMakeLists.txt @@ -2,6 +2,7 @@ #setup libraries if(NOSTALGIA_BUILD_TYPE STREQUAL "Native") + find_package(SDL2 CONFIG REQUIRED) if(NOSTALGIA_BUILD_STUDIO) find_package(Qt5Widgets REQUIRED) find_package(Qt5QuickWidgets REQUIRED) diff --git a/src/nostalgia/tools/pack/pack.cpp b/src/nostalgia/tools/pack/pack.cpp index 23270f901..8d51d99a9 100644 --- a/src/nostalgia/tools/pack/pack.cpp +++ b/src/nostalgia/tools/pack/pack.cpp @@ -78,7 +78,7 @@ struct VerificationPair { std::cout << "copying directory: " << path << '\n'; std::vector verficationPairs; // copy - oxReturnError(src->ls(path.c_str(), [&verficationPairs, src, dest, path](const char *name, ox::InodeId_t) { + oxReturnError(src->ls(path.c_str(), [&verficationPairs, src, dest, path](std::string name, ox::InodeId_t) { std::cout << "reading " << name << '\n'; auto currentFile = path + name; auto [stat, err] = src->stat((currentFile).c_str()); From 517551348aaab6857ff9fa8c1dbc2215a95114d4 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 31 Dec 2019 19:54:57 -0600 Subject: [PATCH 14/18] [ox/fs] Fix GCC/Clang incompatibility --- deps/ox/src/ox/fs/filesystem/passthroughfs.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp index d80398724..ff5464cef 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -89,7 +89,8 @@ class PassThroughFS: public FileSystem { template ox::Error PassThroughFS::ls(const char *dir, F cb) { for (auto &p : std::filesystem::directory_iterator(m_path / stripSlash(dir))) { - oxReturnError(cb(p.path().filename().u8string(), 0)); + auto u8p = p.path().filename().u8string(); + oxReturnError(cb(u8p.c_str(), 0)); } return OxError(0); } From c5f0825edb850ccb957ae81dd40823b0caa862af Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 14 Jan 2020 20:07:46 -0600 Subject: [PATCH 15/18] [ox/mc] Fix signed/unsigned comparison in field bitmap --- deps/ox/src/ox/mc/presenceindicator.cpp | 4 ++-- deps/ox/src/ox/mc/presenceindicator.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/deps/ox/src/ox/mc/presenceindicator.cpp b/deps/ox/src/ox/mc/presenceindicator.cpp index f9e4292c2..b3e7c3678 100644 --- a/deps/ox/src/ox/mc/presenceindicator.cpp +++ b/deps/ox/src/ox/mc/presenceindicator.cpp @@ -17,7 +17,7 @@ FieldPresenceIndicator::FieldPresenceIndicator(uint8_t *mask, std::size_t maxLen m_maskLen = maxLen; } -ValErr FieldPresenceIndicator::get(int i) const { +ValErr FieldPresenceIndicator::get(std::size_t i) const { if (i / 8 < m_maskLen) { return (m_mask[i / 8] >> (i % 8)) & 1; } else { @@ -25,7 +25,7 @@ ValErr FieldPresenceIndicator::get(int i) const { } } -Error FieldPresenceIndicator::set(int i, bool on) { +Error FieldPresenceIndicator::set(std::size_t i, bool on) { if (i / 8 < m_maskLen) { if (on) { m_mask[i / 8] |= 1 << (i % 8); diff --git a/deps/ox/src/ox/mc/presenceindicator.hpp b/deps/ox/src/ox/mc/presenceindicator.hpp index 1b2c15c83..1a9fbeb10 100644 --- a/deps/ox/src/ox/mc/presenceindicator.hpp +++ b/deps/ox/src/ox/mc/presenceindicator.hpp @@ -22,9 +22,9 @@ class FieldPresenceIndicator { public: FieldPresenceIndicator(uint8_t *mask, std::size_t maxLen); - ValErr get(int i) const; + ValErr get(std::size_t i) const; - Error set(int i, bool on); + Error set(std::size_t i, bool on); void setFields(int) noexcept; From f06806dafd05110536cee517931007ef2adc65eb Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 9 Feb 2020 21:43:56 -0600 Subject: [PATCH 16/18] [nostalgia] Remove overriding of input NOSTALIGA_BUILD_STUDIO setting --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd0c6e3e2..d1e2cfa45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,14 +5,13 @@ project(nostalgia) set(NOSTALGIA_BUILD_TYPE "Native" CACHE STRING "The type of build to produce(Native/GBA)") set(NOSTALGIA_IDE_BUILD ON CACHE STRING "Build for IDE's to run") set(NOSTALGIA_QT_PATH "" CACHE STRING "Path to Qt Libraries") -set(NOSTALGIA_BUILD_STUDIO ON CACHE STRING "Build Studio") +set(NOSTALGIA_BUILD_STUDIO ON CACHE BOOL "Build Studio") if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA") set(NOSTALGIA_BUILD_STUDIO OFF) set(OX_BARE_METAL ON) set(OX_USE_STDLIB OFF) else() - set(NOSTALGIA_BUILD_STUDIO ON) set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${NOSTALGIA_QT_PATH}) endif() From c2b4595c3375706611e754462219e39038aca301 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 12 Feb 2020 22:13:34 -0600 Subject: [PATCH 17/18] [ox/std] Make unrecognized hardware trigger compile error --- deps/ox/src/ox/std/hardware.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/deps/ox/src/ox/std/hardware.hpp b/deps/ox/src/ox/std/hardware.hpp index 0aa270100..a1426fca8 100644 --- a/deps/ox/src/ox/std/hardware.hpp +++ b/deps/ox/src/ox/std/hardware.hpp @@ -22,8 +22,7 @@ #else -//TODO: fix warnning -//#warn "Undefined hardware" +#error "Undefined hardware" #endif From 7df2244f650dff48ce1091b6ccd39ee7ac23a2e1 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 12 Feb 2020 22:34:49 -0600 Subject: [PATCH 18/18] [nostalgia] Add make conan to Jenkinsfile --- Jenkinsfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Jenkinsfile b/Jenkinsfile index cd61dd083..a2ec6e23e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,6 +3,7 @@ pipeline { stages { stage('Build') { steps { + sh 'make conan' sh 'make configure-debug configure-release' sh 'make' }