1
0
mirror of https://github.com/ScrelliCopter/tmx2gba.git synced 2025-02-21 03:29:25 +11:00

rewrite assembly writer

This commit is contained in:
2024-03-21 10:14:17 +11:00
parent 417bc11fae
commit 5d5dda81c9
4 changed files with 178 additions and 72 deletions

View File

@@ -4,7 +4,7 @@ add_executable(tmx2gba
tmxobject.hpp
tmxreader.hpp tmxreader.cpp
tmxtileset.hpp
swriter.hpp
swriter.hpp swriter.cpp
headerwriter.hpp headerwriter.cpp
tmx2gba.cpp)

163
src/swriter.cpp Normal file
View File

@@ -0,0 +1,163 @@
/* swwriter.cpp - Copyright (C) 2024 a dinosaur (zlib, see COPYING.txt) */
#include "swriter.hpp"
#include <type_traits>
#include <limits>
#define GNU_STYLE 0
#define MASM_STYLE 1
#define HEX_STYLE GNU_STYLE
static inline constexpr char HexU(uint8_t h) { return "0123456789ABCDEF"[h >> 4]; }
static inline constexpr char HexL(uint8_t l) { return "0123456789ABCDEF"[l & 15]; }
#if HEX_STYLE == GNU_STYLE
template <typename T> static void CHex(std::ostream& s, T x);
template <> void CHex(std::ostream& s, uint8_t x)
{
if (x > 9) s << "0x";
if (x > 15) s << HexU(x);
s << HexL(x);
}
template <> void CHex(std::ostream& s, uint16_t x)
{
if (x > 9) s << "0x";
if (x > 4095) s << HexU(static_cast<uint8_t>(x >> 8));
if (x > 255) s << HexL(static_cast<uint8_t>(x >> 8));
if (x > 15) s << HexU(static_cast<uint8_t>(x));
s << HexL(static_cast<uint8_t>(x));
}
template <> void CHex(std::ostream& s, uint32_t x)
{
if (x > 9) s << "0x";
if (x > 0xFFFFFFF) s << HexU(static_cast<uint8_t>(x >> 24));
if (x > 0xFFFFFF) s << HexL(static_cast<uint8_t>(x >> 24));
if (x > 0xFFFFF) s << HexU(static_cast<uint8_t>(x >> 16));
if (x > 65535) s << HexL(static_cast<uint8_t>(x >> 16));
if (x > 4095) s << HexU(static_cast<uint8_t>(x >> 8));
if (x > 255) s << HexL(static_cast<uint8_t>(x >> 8));
if (x > 15) s << HexU(static_cast<uint8_t>(x));
s << HexL(static_cast<uint8_t>(x));
}
#elif HEX_STYLE == MASM_STYLE
template <typename T> static void MHex(std::ostream& s, T x);
template <> void MHex(std::ostream& s, uint8_t x)
{
if (x > 159) s << "0";
if (x > 15) s << HexU(x); else if (x > 9) s << "0";
s << HexL(x);
if (x > 9) s << "h";
}
template <> void MHex(std::ostream& s, uint16_t x)
{
if (x > 40959) s << "0";
if (x > 4095) s << HexU(static_cast<uint8_t>(x >> 8)); else if (x > 2559) s << "0";
if (x > 255) s << HexL(static_cast<uint8_t>(x >> 8)); else if (x > 159) s << "0";
if (x > 15) s << HexU(static_cast<uint8_t>(x)); else if (x > 9) s << "0";
s << HexL(static_cast<uint8_t>(x));
if (x > 9) s << "h";
}
template <> void MHex(std::ostream& s, uint32_t x)
{
if (x > 0x9FFFFFFF) s << "0";
if (x > 0xFFFFFFF) s << HexU(static_cast<uint8_t>(x >> 24)); else if (x > 0x9FFFFFF) s << "0";
if (x > 0xFFFFFF) s << HexL(static_cast<uint8_t>(x >> 24)); else if (x > 0x9FFFFF) s << "0";
if (x > 0xFFFFF) s << HexU(static_cast<uint8_t>(x >> 16)); else if (x > 655359) s << "0";
if (x > 65535) s << HexL(static_cast<uint8_t>(x >> 16)); else if (x > 40959) s << "0";
if (x > 4095) s << HexU(static_cast<uint8_t>(x >> 8)); else if (x > 2559) s << "0";
if (x > 255) s << HexL(static_cast<uint8_t>(x >> 8)); else if (x > 159) s << "0";
if (x > 15) s << HexU(static_cast<uint8_t>(x)); else if (x > 9) s << "0";
s << HexL(static_cast<uint8_t>(x));
if (x > 9) s << "h";
}
#else
# error "Unknown hex style"
#endif
template <typename T> static constexpr const std::string_view DataType();
template <> constexpr const std::string_view DataType<uint8_t>() { return ".byte"; }
template <> constexpr const std::string_view DataType<uint16_t>() { return ".hword"; }
template <> constexpr const std::string_view DataType<uint32_t>() { return ".word"; }
template <typename I>
static void WriteArrayDetail(std::ostream& s, const I beg, const I end, int perCol)
{
typedef typename std::iterator_traits<I>::value_type Element;
int col = 0;
for (auto it = beg;;)
{
if (col == 0)
s << "\t" << DataType<Element>() << " ";
const Element e = *it;
#if HEX_STYLE == MASM_STYLE
MHex(s, e);
#elif HEX_STYLE == GNU_STYLE
CHex(s, e);
#endif
if (++it == end)
break;
if (++col < perCol)
{
s << ",";
}
else
{
s << std::endl;
col = 0;
}
}
s << std::endl;
}
void SWriter::WriteSymbol(const std::string_view suffix)
{
if (writes++ != 0)
stream << std::endl;
stream << "\t.section .rodata" << std::endl;
stream << "\t.align 2" << std::endl;
stream << "\t.global " << mName << suffix << std::endl;
stream << "\t.hidden " << mName << suffix << std::endl;
stream << mName << suffix << ":" << std::endl;
}
void SWriter::WriteArray(const std::string_view suffix, std::span<uint8_t> data, int numCols)
{
WriteSymbol(suffix);
WriteArrayDetail(stream, data.begin(), data.end(), numCols);
}
void SWriter::WriteArray(const std::string_view suffix, std::span<uint16_t> data, int numCols)
{
WriteSymbol(suffix);
WriteArrayDetail(stream, data.begin(), data.end(), numCols);
}
void SWriter::WriteArray(const std::string_view suffix, std::span<uint32_t> data, int numCols)
{
WriteSymbol(suffix);
WriteArrayDetail(stream, data.begin(), data.end(), numCols);
}
SWriter::~SWriter()
{
if (stream.is_open())
{
stream.close();
}
}
bool SWriter::Open(const std::filesystem::path& path, const std::string_view name)
{
mName = name;
stream.open(path);
return stream.is_open();
}

View File

@@ -1,4 +1,4 @@
/* swwriter.hpp - Copyright (C) 2015-2024 a dinosaur (zlib, see COPYING.txt) */
/* swwriter.hpp - Copyright (C) 2024 a dinosaur (zlib, see COPYING.txt) */
#ifndef SWRITER_HPP
#define SWRITER_HPP
@@ -7,83 +7,26 @@
#include <cstdint>
#include <string>
#include <string_view>
#include <span>
#include <fstream>
#include <vector>
namespace GccIsDumb
{
template <typename T> constexpr const char* DatType();
template <> constexpr const char* DatType<uint8_t>() { return ".byte"; }
template <> constexpr const char* DatType<uint16_t>() { return ".hword"; }
template <> constexpr const char* DatType<uint32_t>() { return ".word"; }
}
#include <filesystem>
class SWriter
{
std::ofstream stream;
std::string mName;
int writes = 0;
template <typename T>
static void WriteArray(std::ostream& aOut, const std::vector<T>& aDat, int aPerCol = 16)
{
int col = 0;
aOut.setf(std::ios::hex, std::ios::basefield);
aOut.setf(std::ios::showbase);
size_t i = 0;
for (T element : aDat)
{
if (col == 0)
aOut << "\t" << GccIsDumb::DatType<T>() << " ";
aOut << std::hex << static_cast<int>(element);
if (i < aDat.size() - 1)
{
if (++col < aPerCol)
{
aOut << ",";
}
else
{
aOut << "" << std::endl;
col = 0;
}
}
++i;
}
}
void WriteSymbol(const std::string_view suffix);
public:
[[nodiscard]] bool Open(const std::filesystem::path& path)
{
stream.open(path);
return stream.is_open();
}
~SWriter();
~SWriter()
{
if (stream.is_open())
{
stream.close();
}
}
bool Open(const std::filesystem::path& path, const std::string_view name);
template <typename T>
void WriteArray(const std::string_view name, T data)
{
if (writes++ != 0)
stream << std::endl;
stream << "\t.section .rodata" << std::endl;
stream << "\t.align 2" << std::endl;
stream << "\t.global " << name << "Tiles" << std::endl;
stream << "\t.hidden " << name << "Tiles" << std::endl;
stream << name << "Tiles" << ":" << std::endl;
WriteArray(stream, data);
stream << std::endl;
}
void WriteArray(const std::string_view suffix, std::span<uint8_t> data, int numCols = 16);
void WriteArray(const std::string_view suffix, std::span<uint16_t> data, int numCols = 16);
void WriteArray(const std::string_view suffix, std::span<uint32_t> data, int numCols = 16);
};
#endif//SWRITER_HPP

View File

@@ -204,7 +204,7 @@ int main(int argc, char** argv)
// Open output files
SWriter outS; HeaderWriter outH;
if (!outS.Open(p.outPath + ".s"))
if (!outS.Open(p.outPath + ".s", name))
{
std::cerr << "Failed to create output file \"" << p.outPath << ".s\".";
return 1;
@@ -219,13 +219,13 @@ int main(int argc, char** argv)
const uint32_t* gfxTiles = layerGfx->GetData();
const uint32_t* palTiles = (layerPal == nullptr) ? nullptr : layerPal->GetData();
std::vector<uint16_t> charDat;
size_t numTiles = static_cast<size_t>(layerGfx->GetWidth()) * static_cast<size_t>(layerGfx->GetHeight());
const size_t numTiles = static_cast<size_t>(layerGfx->GetWidth()) * static_cast<size_t>(layerGfx->GetHeight());
charDat.reserve(numTiles);
for (size_t i = 0; i < numTiles; ++i)
{
uint32_t read = (*gfxTiles++);
uint16_t tile = std::max<uint16_t>(0, tmx.LidFromGid(read & ~TmxLayer::FLIP_MASK) + p.offset);
uint16_t tile = std::max(0, static_cast<int>(tmx.LidFromGid(read & ~TmxLayer::FLIP_MASK)) + p.offset);
uint8_t flags = 0x0;
// Get flipped!
@@ -271,7 +271,7 @@ int main(int argc, char** argv)
// Write collision
outH.WriteCollision(collisionDat);
outS.WriteArray("Collision", collisionDat);
outS.WriteArray("Collision", collisionDat, 32);
}
if (!p.objMappings.empty())