diff options
author | purringChaos <kitteh@kitteh.pw> | 2021-06-06 17:27:26 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-06 18:27:26 +0200 |
commit | e3b4350f44a51d0012457a556dc04dfd0ba267d4 (patch) | |
tree | 3ac02cfbe00ab13c787e77dbf7aa491a0e33ed1b /src | |
parent | 2494058a7190a4a673fc0d6c2e59740c896a74c6 (diff) |
Add PoroCYon's DSP code. (#1123)
* Add PoroCYon's DSP code.
* Remove some teakra iles that we dont need.
* make some requested changes.
* move DataMemoryOffset into namespace.
* use deault param.
* ad the switch change
* <Generic> forget about the default parameter
Diffstat (limited to 'src')
89 files changed, 29641 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c899745..5603943 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,6 +18,7 @@ add_library(core STATIC DSi.cpp DSi_AES.cpp DSi_Camera.cpp + DSi_DSP.cpp DSi_I2C.cpp DSi_NDMA.cpp DSi_NWifi.cpp @@ -100,6 +101,10 @@ if (ENABLE_JIT) endif() endif() +add_subdirectory(teakra EXCLUDE_FROM_ALL) +target_link_libraries(core teakra) + + if (ENABLE_OGLRENDERER) find_package(PkgConfig REQUIRED) pkg_check_modules(EPOXY REQUIRED epoxy) diff --git a/src/DSi.cpp b/src/DSi.cpp index 675d2cd..5954d4f 100644 --- a/src/DSi.cpp +++ b/src/DSi.cpp @@ -36,6 +36,7 @@ #include "DSi_I2C.h" #include "DSi_SD.h" #include "DSi_AES.h" +#include "DSi_DSP.h" #include "DSi_Camera.h" #include "tiny-AES-c/aes.hpp" @@ -51,6 +52,7 @@ u16 SCFG_Clock9; u16 SCFG_Clock7; u32 SCFG_EXT[2]; u32 SCFG_MC; +u16 SCFG_RST; u8 ARM9iBIOS[0x10000]; u8 ARM7iBIOS[0x10000]; @@ -95,6 +97,7 @@ bool Init() if (!DSi_I2C::Init()) return false; if (!DSi_AES::Init()) return false; + if (!DSi_DSP::Init()) return false; NDMAs[0] = new DSi_NDMA(0, 0); NDMAs[1] = new DSi_NDMA(0, 1); @@ -121,6 +124,7 @@ void DeInit() DSi_I2C::DeInit(); DSi_AES::DeInit(); + DSi_DSP::DeInit(); for (int i = 0; i < 8; i++) delete NDMAs[i]; @@ -146,6 +150,7 @@ void Reset() DSi_I2C::Reset(); DSi_AES::Reset(); + DSi_DSP::Reset(); SDMMC->Reset(); SDIO->Reset(); @@ -156,6 +161,8 @@ void Reset() SCFG_EXT[0] = 0x8307F100; SCFG_EXT[1] = 0x93FFFB06; SCFG_MC = 0x0010;//0x0011; + SCFG_RST = 0; + DSi_DSP::SetRstLine(false); // LCD init flag GPU::DispStat[0] |= (1<<6); @@ -202,6 +209,13 @@ void SoftReset() DSi_AES::Reset(); + + DSi_AES::Reset(); + // TODO: does the DSP get reset? NWRAM doesn't, so I'm assuming no + // *HOWEVER*, the bootrom (which does get rerun) does remap NWRAM, and thus + // the DSP most likely gets reset + DSi_DSP::Reset(); + LoadNAND(); SDMMC->Reset(); @@ -216,6 +230,10 @@ void SoftReset() SCFG_EXT[0] = 0x8307F100; SCFG_EXT[1] = 0x93FFFB06; SCFG_MC = 0x0010;//0x0011; + // TODO: is this actually reset? + SCFG_RST = 0; + DSi_DSP::SetRstLine(false); + // LCD init flag GPU::DispStat[0] |= (1<<6); @@ -602,6 +620,8 @@ void MapNWRAM_B(u32 num, u8 val) u8* ptr = &NWRAM_B[num << 15]; + DSi_DSP::OnMBKCfg('B', num, oldval, val, ptr); + if (oldval & 0x80) { if (oldval & 0x02) oldval &= 0xFE; @@ -641,6 +661,8 @@ void MapNWRAM_C(u32 num, u8 val) u8* ptr = &NWRAM_C[num << 15]; + DSi_DSP::OnMBKCfg('C', num, oldval, val, ptr); + if (oldval & 0x80) { if (oldval & 0x02) oldval &= 0xFE; @@ -1482,6 +1504,7 @@ u8 ARM9IORead8(u32 addr) switch (addr) { case 0x04004000: return SCFG_BIOS & 0xFF; + case 0x04004006: return SCFG_RST & 0xFF; CASE_READ8_32BIT(0x04004040, MBK[0][0]) CASE_READ8_32BIT(0x04004044, MBK[0][1]) @@ -1500,6 +1523,9 @@ u8 ARM9IORead8(u32 addr) return DSi_Camera::Read8(addr); } + if (addr >= 0x04004300 && addr <= 0x04004400) + return DSi_DSP::Read16(addr); + return NDS::ARM9IORead8(addr); } @@ -1509,6 +1535,7 @@ u16 ARM9IORead16(u32 addr) { case 0x04004000: return SCFG_BIOS & 0xFF; case 0x04004004: return SCFG_Clock9; + case 0x04004006: return SCFG_RST; case 0x04004010: return SCFG_MC & 0xFFFF; CASE_READ16_32BIT(0x04004040, MBK[0][0]) @@ -1528,6 +1555,9 @@ u16 ARM9IORead16(u32 addr) return DSi_Camera::Read16(addr); } + if (addr >= 0x04004300 && addr <= 0x04004400) + return DSi_DSP::Read32(addr); + return NDS::ARM9IORead16(addr); } @@ -1536,6 +1566,7 @@ u32 ARM9IORead32(u32 addr) switch (addr) { case 0x04004000: return SCFG_BIOS & 0xFF; + case 0x04004004: return SCFG_Clock9 | ((u32)SCFG_RST << 16); case 0x04004008: return SCFG_EXT[0]; case 0x04004010: return SCFG_MC & 0xFFFF; @@ -1603,6 +1634,11 @@ void ARM9IOWrite8(u32 addr, u8 val) //if (val == 0x80 && NDS::ARM9->R[15] == 0xFFFF0268) NDS::ARM9->Halt(1); return; + case 0x04004006: + SCFG_RST = (SCFG_RST & 0xFF00) | val; + DSi_DSP::SetRstLine(val & 1); + return; + case 0x04004040: MapNWRAM_A(0, val); return; case 0x04004041: MapNWRAM_A(1, val); return; case 0x04004042: MapNWRAM_A(2, val); return; @@ -1631,6 +1667,12 @@ void ARM9IOWrite8(u32 addr, u8 val) return DSi_Camera::Write8(addr, val); } + if (addr >= 0x04004300 && addr <= 0x04004400) + { + DSi_DSP::Write8(addr, val); + return; + } + return NDS::ARM9IOWrite8(addr, val); } @@ -1642,6 +1684,11 @@ void ARM9IOWrite16(u32 addr, u16 val) Set_SCFG_Clock9(val); return; + case 0x04004006: + SCFG_RST = val; + DSi_DSP::SetRstLine(val & 1); + return; + case 0x04004040: MapNWRAM_A(0, val & 0xFF); MapNWRAM_A(1, val >> 8); @@ -1690,6 +1737,12 @@ void ARM9IOWrite16(u32 addr, u16 val) return DSi_Camera::Write16(addr, val); } + if (addr >= 0x04004300 && addr <= 0x04004400) + { + DSi_DSP::Write16(addr, val); + return; + } + return NDS::ARM9IOWrite16(addr, val); } @@ -1697,6 +1750,12 @@ void ARM9IOWrite32(u32 addr, u32 val) { switch (addr) { + case 0x04004004: + Set_SCFG_Clock9(val & 0xFFFF); + SCFG_RST = val >> 16; + DSi_DSP::SetRstLine((val >> 16) & 1); + break; + case 0x04004008: { u32 oldram = (SCFG_EXT[0] >> 14) & 0x3; @@ -2106,6 +2165,13 @@ void ARM7IOWrite32(u32 addr, u32 val) return; } + + if (addr >= 0x04004300 && addr <= 0x04004400) + { + DSi_DSP::Write32(addr, val); + return; + } + return NDS::ARM7IOWrite32(addr, val); } @@ -26,6 +26,9 @@ namespace DSi { extern u16 SCFG_BIOS; +extern u16 SCFG_Clock9; +extern u32 SCFG_EXT[2]; + extern u8 ARM9iBIOS[0x10000]; extern u8 ARM7iBIOS[0x10000]; diff --git a/src/DSi_DSP.cpp b/src/DSi_DSP.cpp new file mode 100644 index 0000000..6b216f8 --- /dev/null +++ b/src/DSi_DSP.cpp @@ -0,0 +1,592 @@ +/* + Copyright 2020 PoroCYon + + This file is part of melonDS. + + melonDS is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + melonDS is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with melonDS. If not, see http://www.gnu.org/licenses/. +*/ + +#include "teakra/include/teakra/teakra.h" + +#include "DSi.h" +#include "DSi_DSP.h" +#include "FIFO.h" +#include "NDS.h" + + +namespace DSi_DSP +{ + +Teakra::Teakra* TeakraCore; + +bool SCFG_RST; + +u16 DSP_PADR; +u16 DSP_PCFG; +u16 DSP_PSTS; +u16 DSP_PSEM; +u16 DSP_PMASK; +u16 DSP_PCLEAR; +u16 DSP_CMD[3]; +u16 DSP_REP[3]; + +u64 DSPTimestamp; + +FIFO<u16, 16> PDATAReadFifo/*, *PDATAWriteFifo*/; +int PDataDMALen = 0; + +constexpr u32 DataMemoryOffset = 0x20000; // from Teakra memory_interface.h +// NOTE: ^ IS IN DSP WORDS, NOT IN BYTES! + +u16 GetPSTS() +{ + u16 r = DSP_PSTS & (1<<9); // this is the only sticky bit + //r &= ~((1<<2)|(1<<7)); // we support instant resets and wrfifo xfers + r |= (1<<8); // write fifo is always empty (inf. speed) + + if ( PDATAReadFifo.IsFull ()) r |= 1<<5; + if (!PDATAReadFifo.IsEmpty()) r |=(1<<6)|(1<<0); + + if (!TeakraCore->SendDataIsEmpty(0)) r |= 1<<13; + if (!TeakraCore->SendDataIsEmpty(1)) r |= 1<<14; + if (!TeakraCore->SendDataIsEmpty(2)) r |= 1<<15; + if ( TeakraCore->RecvDataIsReady(0)) r |= 1<<10; + if ( TeakraCore->RecvDataIsReady(1)) r |= 1<<11; + if ( TeakraCore->RecvDataIsReady(2)) r |= 1<<12; + + return r; +} + +void IrqRep0() +{ + if (DSP_PCFG & (1<< 9)) NDS::SetIRQ(0, NDS::IRQ_DSi_DSP); +} +void IrqRep1() +{ + if (DSP_PCFG & (1<<10)) NDS::SetIRQ(0, NDS::IRQ_DSi_DSP); +} +void IrqRep2() +{ + if (DSP_PCFG & (1<<11)) NDS::SetIRQ(0, NDS::IRQ_DSi_DSP); +} +void IrqSem() +{ + DSP_PSTS |= 1<<9; + // apparently these are always fired? + NDS::SetIRQ(0, NDS::IRQ_DSi_DSP); +} + +void AudioCb(std::array<s16, 2> frame) +{ + // TODO +} + +bool Init() +{ + TeakraCore = new Teakra::Teakra(); + SCFG_RST = false; + + if (!TeakraCore) return false; + + TeakraCore->SetRecvDataHandler(0, IrqRep0); + TeakraCore->SetRecvDataHandler(1, IrqRep1); + TeakraCore->SetRecvDataHandler(2, IrqRep2); + + TeakraCore->SetSemaphoreHandler(IrqSem); + + // these happen instantaneously and without too much regard for bus aribtration + // rules, so, this might have to be changed later on + Teakra::AHBMCallback cb; + cb.read8 = DSi::ARM9Read8; + cb.write8 = DSi::ARM9Write8; + cb.read16 = DSi::ARM9Read16; + cb.write16 = DSi::ARM9Write16; + cb.read32 = DSi::ARM9Read32; + cb.write32 = DSi::ARM9Write32; + TeakraCore->SetAHBMCallback(cb); + + TeakraCore->SetAudioCallback(AudioCb); + + //PDATAReadFifo = new FIFO<u16>(16); + //PDATAWriteFifo = new FIFO<u16>(16); + + return true; +} +void DeInit() +{ + //if (PDATAWriteFifo) delete PDATAWriteFifo; + if (TeakraCore) delete TeakraCore; + + //PDATAReadFifo = NULL; + //PDATAWriteFifo = NULL; + TeakraCore = NULL; +} + +void Reset() +{ + DSPTimestamp = 0; + + DSP_PADR = 0; + DSP_PCFG = 0; + DSP_PSTS = 0; + DSP_PSEM = 0; + DSP_PMASK = 0xff; + DSP_PCLEAR = 0; + DSP_CMD[2] = DSP_CMD[1] = DSP_CMD[0] = 0; + DSP_REP[2] = DSP_REP[1] = DSP_REP[0] = 0; + PDataDMALen = 0; + + PDATAReadFifo.Clear(); + //PDATAWriteFifo->Clear(); + TeakraCore->Reset(); + + NDS::CancelEvent(NDS::Event_DSi_DSP); +} + +bool IsRstReleased() +{ + return SCFG_RST; +} +void SetRstLine(bool release) +{ + SCFG_RST = release; + Reset(); + DSPTimestamp = NDS::ARM9Timestamp; // only start now! +} + +void OnMBKCfg(char bank, u32 slot, u8 oldcfg, u8 newcfg, u8* nwrambacking) +{ + if (bank != 'B' && bank != 'C') + { + printf("WTF?? (DSP MBK recfg, nonsense bank '%c')\n", bank); + return; + } + + bool olddsp = (oldcfg & 3) >= 2, // was mapped to the DSP + newdsp = (newcfg & 3) >= 2; // will be mapped to the DSP + + // nothing changes + if (olddsp == newdsp) + return; + + const u8* src; + u8* dst; + + if (newdsp) + { + // need to map stuff to DSP memory (== Teakra-owned memory) from NWRAM + src = nwrambacking; + dst = &TeakraCore->GetDspMemory()[((newcfg >> 2) & 7) << 15]; + + if (bank == 'C') // C: DSP data (B: DSP code) + dst += DataMemoryOffset*2; + } + else //if (olddsp) + { + // it was mapped to the DSP, but now we have to copy it out, back to NWRAM + src = &TeakraCore->GetDspMemory()[((oldcfg >> 2) & 7) << 15]; + dst = nwrambacking; + + if (bank == 'C') // C: DSP data (B: DSP code) + src += DataMemoryOffset*2; + } + + memcpy(dst, src, 1<<15); // 1 full slot +} + +inline bool IsDSPCoreEnabled() +{ + return (DSi::SCFG_Clock9 & (1<<1)) && SCFG_RST && (DSP_PCFG & (1<<0)); +} + +bool DSPCatchUp() +{ + //asm volatile("int3"); + if (!IsDSPCoreEnabled()) + { + // nothing to do, but advance the current time so that we don't do an + // unreasonable amount of cycles when rst is released + if (DSPTimestamp < NDS::ARM9Timestamp) + DSPTimestamp = NDS::ARM9Timestamp; + + return false; + } + + u64 curtime = NDS::ARM9Timestamp; + + if (DSPTimestamp >= curtime) return true; // ummmm?! + + u64 backlog = curtime - DSPTimestamp; + + while (backlog & (1uLL<<32)) // god I hope this never happens + { + Run((u32)(backlog & ((1uLL<<32)-1))); + backlog = curtime - DSPTimestamp; + } + Run((u32)backlog); + + return true; +} +void DSPCatchUpU32(u32 _) { DSPCatchUp(); } + +void PDataDMAWrite(u16 wrval) +{ + u32 addr = DSP_PADR; + + switch (DSP_PCFG & (7<<12)) // memory region select + { + case 0<<12: // data + addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16; + TeakraCore->DataWriteA32(addr, wrval); + break; + case 1<<12: // mmio + TeakraCore->MMIOWrite(addr & 0x7FF, wrval); + break; + case 5<<12: // program + addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16; + TeakraCore->ProgramWrite(addr, wrval); + break; + case 7<<12: + addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16; + // only do stuff when AHBM is configured correctly + if (TeakraCore->AHBMGetDmaChannel(0) == 0 && TeakraCore->AHBMGetDirection(0) == 1/*W*/) + { + switch (TeakraCore->AHBMGetUnitSize(0)) + { + case 0: /* 8bit */ DSi::ARM9Write8 (addr, (u8)wrval); break; + case 1: /* 16 b */ TeakraCore->AHBMWrite16(addr, wrval); break; + // does it work like this, or should it first buffer two u16's + // until it has enough data to write to the actual destination? + // -> this seems to be correct behavior! + case 2: /* 32 b */ TeakraCore->AHBMWrite32(addr, wrval); break; + } + } + break; + default: return; + } + + if (DSP_PCFG & (1<<1)) // auto-increment + ++DSP_PADR; // overflows and stays within a 64k 'page' // TODO: is this +1 or +2? + + NDS::SetIRQ(0, NDS::IRQ_DSi_DSP); // wrfifo empty +} +// TODO: FIFO interrupts! (rd full, nonempty) +u16 PDataDMARead() +{ + u16 r = 0; + u32 addr = DSP_PADR; + switch (DSP_PCFG & (7<<12)) // memory region select + { + case 0<<12: // data + addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16; + r = TeakraCore->DataReadA32(addr); + break; + case 1<<12: // mmio + r = TeakraCore->MMIORead(addr & 0x7FF); + break; + case 5<<12: // program + addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16; + r = TeakraCore->ProgramRead(addr); + break; + case 7<<12: + addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16; + // only do stuff when AHBM is configured correctly + if (TeakraCore->AHBMGetDmaChannel(0) == 0 && TeakraCore->AHBMGetDirection(0) == 0/*R*/) + { + switch (TeakraCore->AHBMGetUnitSize(0)) + { + case 0: /* 8bit */ r = DSi::ARM9Read8 (addr); break; + case 1: /* 16 b */ r = TeakraCore->AHBMRead16(addr); break; + case 2: /* 32 b */ r = (u16)TeakraCore->AHBMRead32(addr); break; + } + } + break; + default: return r; + } + + if (DSP_PCFG & (1<<1)) // auto-increment + ++DSP_PADR; // overflows and stays within a 64k 'page' // TODO: is this +1 or +2? + + return r; +} +void PDataDMAFetch() +{ + if (!PDataDMALen) return; + + PDATAReadFifo.Write(PDataDMARead()); + + if (PDataDMALen > 0) --PDataDMALen; +} +void PDataDMAStart() +{ + switch ((DSP_PSTS & (3<<2)) >> 2) + { + case 0: PDataDMALen = 1; break; + case 1: PDataDMALen = 8; break; + case 2: PDataDMALen =16; break; + case 3: PDataDMALen =-1; break; + } + + // fill a single fifo + int amt = PDataDMALen; + if (amt < 0) amt = 16; + for (int i = 0; i < amt; ++i) + PDataDMAFetch(); + + NDS::SetIRQ(0, NDS::IRQ_DSi_DSP); + +} +void PDataDMACancel() +{ + PDataDMALen = 0; + PDATAReadFifo.Clear(); + +} +u16 PDataDMAReadMMIO() +{ + u16 ret; + + if (!PDATAReadFifo.IsEmpty()) + ret = PDATAReadFifo.Read(); + + // aha, there's more to come + if (PDataDMALen != 0) + { + int left = 16 - PDATAReadFifo.Level(); + if (PDataDMALen > 0 && PDataDMALen < left) + left = PDataDMALen; + + for (int i = 0; i < left; ++i) + PDataDMAFetch(); + + ret = PDATAReadFifo.Read(); + } + else + { + // ah, crap + ret = 0; // TODO: is this actually 0, or just open bus? + } + + if (!PDATAReadFifo.IsEmpty() || PDATAReadFifo.IsFull()) + NDS::SetIRQ(0, NDS::IRQ_DSi_DSP); + + return ret; +} + +u8 Read8(u32 addr) +{ + if (!(DSi::SCFG_EXT[0] & (1<<18))) + return 0; + + if (!DSPCatchUp()) return 0; + + addr &= 0x3F; // mirroring wheee + + // ports are a bit weird, 16-bit regs in 32-bit spaces + switch (addr) + { + // no 8-bit PDATA read + // no DSP_PADR read + case 0x08: return DSP_PCFG & 0xFF; + case 0x09: return DSP_PCFG >> 8; + case 0x0C: return GetPSTS() & 0xFF; + case 0x0D: return GetPSTS() >> 8; + case 0x10: return DSP_PSEM & 0xFF; + case 0x11: return DSP_PSEM >> 8; + case 0x14: return DSP_PMASK & 0xFF; + case 0x15: return DSP_PMASK >> 8; + // no DSP_PCLEAR read + case 0x1C: return TeakraCore->GetSemaphore() & 0xFF; // SEM + case 0x1D: return TeakraCore->GetSemaphore() >> 8; + } + + return 0; +} +u16 Read16(u32 addr) +{ + if (!(DSi::SCFG_EXT[0] & (1<<18))) + return 0; + + if (!DSPCatchUp()) return 0; + + addr &= 0x3E; // mirroring wheee + + // ports are a bit weird, 16-bit regs in 32-bit spaces + switch (addr) + { + case 0x00: return PDataDMAReadMMIO(); + // no DSP_PADR read + case 0x08: return DSP_PCFG; + case 0x0C: return GetPSTS(); + case 0x10: return DSP_PSEM; + case 0x14: return DSP_PMASK; + // no DSP_PCLEAR read + case 0x1C: return TeakraCore->GetSemaphore(); // SEM + + case 0x20: return DSP_CMD[0]; + case 0x28: return DSP_CMD[1]; + case 0x30: return DSP_CMD[2]; + + case 0x24: + { + u16 r = TeakraCore->RecvData(0); + return r; + } + case 0x2C: + { + u16 r = TeakraCore->RecvData(1); + return r; + } + case 0x34: + { + u16 r = TeakraCore->RecvData(2); + return r; + } + } + + return 0; +} +u32 Read32(u32 addr) +{ + if (!(DSi::SCFG_EXT[0] & (1<<18))) return 0; + + addr &= 0x3C; + return Read16(addr); // *shrug* (doesn't do anything unintended due to the + // 4byte spacing between regs while they're all 16bit) +} + +void Write8(u32 addr, u8 val) +{ + if (!(DSi::SCFG_EXT[0] & (1<<18))) return; + + if (!DSPCatchUp()) return; + + addr &= 0x3F; + switch (addr) + { + // no 8-bit PDATA or PADR writes + case 0x08: + DSP_PCFG = (DSP_PCFG & 0xFF00) | (val << 0); + break; + case 0x09: + DSP_PCFG = (DSP_PCFG & 0x00FF) | (val << 8); + break; + // no PSTS writes + // no 8-bit semaphore writes + // no 8-bit CMDx writes + // no REPx writes + } +} +void Write16(u32 addr, u16 val) +{ + if (!(DSi::SCFG_EXT[0] & (1<<18))) return; + + if (!DSPCatchUp()) return; + + addr &= 0x3E; + switch (addr) + { + case 0x00: PDataDMAWrite(val); break; + case 0x04: DSP_PADR = val; break; + + case 0x08: + DSP_PCFG = val; + if (DSP_PCFG & (1<<4)) + PDataDMAStart(); + else + PDataDMACancel(); + break; + // no PSTS writes + case 0x10: + DSP_PSEM = val; + TeakraCore->SetSemaphore(val); + break; + case 0x14: + DSP_PMASK = val; + TeakraCore->MaskSemaphore(val); + break; + case 0x18: // PCLEAR + TeakraCore->ClearSemaphore(val); + if (TeakraCore->GetSemaphore() == 0) + DSP_PSTS &= ~(1<<9); + + break; + // SEM not writable + + case 0x20: // CMD0 + DSP_CMD[0] = val; + TeakraCore->SendData(0, val); + break; + case 0x28: // CMD1 + DSP_CMD[1] = val; + TeakraCore->SendData(1, val); + break; + case 0x30: // CMD2 + DSP_CMD[2] = val; + TeakraCore->SendData(2, val); + break; + + // no REPx writes + } +} + +void Write32(u32 addr, u32 val) +{ + if (!(DSi::SCFG_EXT[0] & (1<<18))) return; + + addr &= 0x3C; + Write16(addr, val & 0xFFFF); +} + +void Run(u32 cycles) +{ + if (!IsDSPCoreEnabled()) + { + DSPTimestamp += cycles; + return; + } + + TeakraCore->Run(cycles); + + DSPTimestamp += cycles; + + NDS::ScheduleEvent(NDS::Event_DSi_DSP, false, + 16384/*from citra (TeakraSlice)*/, DSPCatchUpU32, 0); +} + +void DoSavestate(Savestate* file) +{ + file->Section("DSPi"); + + PDATAReadFifo.DoSavestate(file); + + file->Var64(&DSPTimestamp); + file->Var32((u32*)&PDataDMALen); + + file->Var16(&DSP_PADR); + file->Var16(&DSP_PCFG); + file->Var16(&DSP_PSTS); + file->Var16(&DSP_PSEM); + file->Var16(&DSP_PMASK); + file->Var16(&DSP_PCLEAR); + file->Var16(&DSP_CMD[0]); + file->Var16(&DSP_CMD[1]); + file->Var16(&DSP_CMD[2]); + file->Var16(&DSP_REP[0]); + file->Var16(&DSP_REP[1]); + file->Var16(&DSP_REP[2]); + file->Var8((u8*)&SCFG_RST); +} + +} + diff --git a/src/DSi_DSP.h b/src/DSi_DSP.h new file mode 100644 index 0000000..f5264b4 --- /dev/null +++ b/src/DSi_DSP.h @@ -0,0 +1,71 @@ +/* + Copyright 2020 PoroCYon + + This file is part of melonDS. + + melonDS is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + melonDS is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with melonDS. If not, see http://www.gnu.org/licenses/. +*/ + +#ifndef DSI_DSP_H +#define DSI_DSP_H + +// TODO: for actual sound output +// * audio callbacks +// * SNDEXCNT + +namespace DSi_DSP +{ + +extern u16 DSP_PDATA; +extern u16 DSP_PADR; +extern u16 DSP_PCFG; +extern u16 DSP_PSTS; +extern u16 DSP_PSEM; +extern u16 DSP_PMASK; +extern u16 DSP_PCLEAR; +extern u16 DSP_SEM; +extern u16 DSP_CMD[3]; +extern u16 DSP_REP[3]; + +bool Init(); +void DeInit(); +void Reset(); + +// TODO: needs to be called! +// however, no DSi savestate stuff seems to be actually implemented?! +void DoSavestate(Savestate* file); + +// SCFG_RST bit0 +bool IsRstReleased(); +void SetRstLine(bool release); + +// apply NWRAM settings +void OnMBKCfg(char bank, u32 slot, u8 oldcfg, u8 newcfg, u8* nwrambacking); + +// DSP_* regs (0x040043xx) (NOTE: checks SCFG_EXT) +u8 Read8(u32 addr); +void Write8(u32 addr, u8 val); + +u16 Read16(u32 addr); +void Write16(u32 addr, u16 val); + +u32 Read32(u32 addr); +void Write32(u32 addr, u32 val); + +// NOTE: checks SCFG_CLK9 +void Run(u32 cycles); + +} + +#endif // DSI_DSP_H + @@ -50,6 +50,7 @@ enum Event_DSi_CamTransfer, Event_DSi_RAMSizeChange, + Event_DSi_DSP, Event_MAX }; diff --git a/src/teakra/.gitignore b/src/teakra/.gitignore new file mode 100644 index 0000000..11eb12c --- /dev/null +++ b/src/teakra/.gitignore @@ -0,0 +1,40 @@ +# Build directory +[Bb]uild/ +cmake-build/ +doc-build/ + +test/ + +# Generated source files +src/common/scm_rev.cpp +.travis.descriptor.json + +# Project/editor files +*.swp +.idea/ +.vs/ +.vscode/ + +# *nix related +# Common convention for backup or temporary files +*~ + +# Visual Studio CMake settings +CMakeSettings.json + +# OSX global filetypes +# Created by Finder or Spotlight in directories for various OS functionality (indexing, etc) +.DS_Store +.AppleDouble +.LSOverride +.Spotlight-V100 +.Trashes + +# Windows global filetypes +Thumbs.db + +teakra.out +teakra.out.* + +# cmake and ctest temporary files +/Testing diff --git a/src/teakra/.travis.yml b/src/teakra/.travis.yml new file mode 100644 index 0000000..bb2b62b --- /dev/null +++ b/src/teakra/.travis.yml @@ -0,0 +1,33 @@ +language: cpp + +matrix: + include: + - env: NAME="Linux Build" + os: linux + dist: xenial + sudo: false + cache: + directories: + - $HOME/assets + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-7 + - g++-7 + script: ./.travis/linux-build.sh + - env: NAME="macOS Build" + os: osx + sudo: false + osx_image: xcode10 + cache: + directories: + - $HOME/assets + script: ./.travis/macos-build.sh + - env: NAME="Windows Build" + os: windows + cache: + directories: + - $HOME/assets + script: ./.travis/windows-build.sh diff --git a/src/teakra/CMakeLists.txt b/src/teakra/CMakeLists.txt new file mode 100644 index 0000000..046f8c4 --- /dev/null +++ b/src/teakra/CMakeLists.txt @@ -0,0 +1,89 @@ +cmake_minimum_required(VERSION 3.8) +project(teakra CXX) + +# Determine if we're built as a subproject (using add_subdirectory) +# or if this is the master project. +set(MASTER_PROJECT OFF) +if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(MASTER_PROJECT ON) +endif() + +option(TEAKRA_WARNINGS_AS_ERRORS "Warnings as errors" ${MASTER_PROJECT}) +option(TEAKRA_BUILD_TOOLS "Build tools" ${MASTER_PROJECT}) +option(TEAKRA_BUILD_UNIT_TESTS "Build unit tests" ${MASTER_PROJECT}) +option(TEAKRA_RUN_TESTS "Run Teakra accuracy tests" OFF) + +# Set hard requirements for C++ +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +# Warn on CMake API deprecations +set(CMAKE_WARN_DEPRECATED ON) + +# Disable in-source builds +set(CMAKE_DISABLE_SOURCE_CHANGES ON) +set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) +if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}") + message(SEND_ERROR "In-source builds are not allowed.") +endif() + +# Add the module directory to the list of paths +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMakeModules") + +# Compiler flags +if (MSVC) + set(TEAKRA_CXX_FLAGS + /std:c++latest # CMAKE_CXX_STANDARD as no effect on MSVC until CMake 3.10. + /W3 + /permissive- # Stricter C++ standards conformance + /MP + /Zi + /Zo + /EHsc + /Zc:throwingNew # Assumes new never returns null + /Zc:inline # Omits inline functions from object-file output + /DNOMINMAX + /D_CRT_SECURE_NO_WARNINGS) + + if (TEAKRA_WARNINGS_AS_ERRORS) + list(APPEND TEAKRA_CXX_FLAGS + /WX) + endif() +else() + set(TEAKRA_CXX_FLAGS + -Wall + -Wextra + -Wcast-qual + -pedantic + -pedantic-errors + -Wfatal-errors + -Wno-missing-braces + -Wno-unused-parameter) + + if (TEAKRA_WARNINGS_AS_ERRORS) + list(APPEND TEAKRA_CXX_FLAGS + -Werror) + endif() +endif() + +# Prefer the -pthread flag on Linux. +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) + +enable_testing() + +if (NOT TEAKRA_TEST_ASSETS_DIR) + set(TEAKRA_TEST_ASSETS_DIR "${CMAKE_CURRENT_BINARY_DIR}") +endif() + +# External libraries +add_subdirectory(externals) + +# Teakra project files +add_subdirectory(src) + +# Teakra tests +if (TEAKRA_BUILD_UNIT_TESTS) + add_subdirectory(tests) +endif() diff --git a/src/teakra/CMakeModules/CreateDirectoryGroups.cmake b/src/teakra/CMakeModules/CreateDirectoryGroups.cmake new file mode 100644 index 0000000..175899e --- /dev/null +++ b/src/teakra/CMakeModules/CreateDirectoryGroups.cmake @@ -0,0 +1,17 @@ +# This function should be passed a name of an existing target. It will automatically generate +# file groups following the directory hierarchy, so that the layout of the files in IDEs matches the +# one in the filesystem. +function(create_target_directory_groups target_name) + # Place any files that aren't in the source list in a separate group so that they don't get in + # the way. + source_group("Other Files" REGULAR_EXPRESSION ".") + + get_target_property(target_sources "${target_name}" SOURCES) + + foreach(file_name IN LISTS target_sources) + get_filename_component(dir_name "${file_name}" PATH) + # Group names use '\' as a separator even though the entire rest of CMake uses '/'... + string(REPLACE "/" "\\" group_name "${dir_name}") + source_group("${group_name}" FILES "${file_name}") + endforeach() +endfunction() diff --git a/src/teakra/LICENSE b/src/teakra/LICENSE new file mode 100644 index 0000000..6208ca9 --- /dev/null +++ b/src/teakra/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Weiyi Wang + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/teakra/README.md b/src/teakra/README.md new file mode 100644 index 0000000..bbc4d60 --- /dev/null +++ b/src/teakra/README.md @@ -0,0 +1,21 @@ +# Teakra + +[![Build Status](https://travis-ci.com/wwylele/teakra.svg?branch=master)](https://travis-ci.com/wwylele/teakra) +[![Build status](https://ci.appveyor.com/api/projects/status/mxr5tg4v8dafyqec/branch/master?svg=true)](https://ci.appveyor.com/project/wwylele/teakra/branch/master) + +Emulator, (dis-)assembler, tools and documentation for XpertTeak, the DSP used by DSi/3DS. + +Many thanks to Martin Korth and many other contributers for their help and their [excellent GBATEK doc](http://problemkaputt.de/gbatek.htm#dsixpertteakdsp)! + +## Contents +Please refer to README.md in the following directories for their detail. + - `src` contains main source code for compiling portable libraries/tools. [Detailed documentation](src/README.md) for the Teak architecture and for peripherals is also here. + - `include` contains the header for the emulator and the disassembler libraries. + - `dsptester` contains the source code of a 3DS tool that tests processor instructions and registers + - `dspmemorytester` contains the source code of another 3DS tool that tests memory read/write, MMIO and DMA. + +## General Information of the XpertTeak + +The XpertTeak DSP consists of a Teak-family architecture processor, and peripheral components including DMA, interrupt controller and audio input/output ports etc. The exact architecture of the processor is still unclear. GBATEK states that the architecture is TeakLite II, the successor of the TeakLite architecture. Their evidence is the TeakLite II disassembler bundled in RealView Developer Suite. However, a Teak family debugger from [here](https://www.lauterbach.com) shows that the "TEAK(-REVA, -REVB, DEV-A0, -RTL2_0)" contains very similar registers and instruction set described in GBATEK, while the "TeakLite-II" contains very different registers and instructions. This shows that the architecture is likely the original Teak, introduced along with TeakLite as a "non-Lite" expansion to it. + +DSi and 3DS both include XpertTeak. However, their uses of XpertTeak are pretty different. Most DSi games don't use it at all. It's used by the "Nintendo DSi Sound" and "Nintendo Zone" system utilities, and by the "Cooking Coach" cartridge (according to GBATEK), where it appears to be intended for audio/video decoding. On the contrary, 3DS games all use XpertTeak for audio decoding and output. diff --git a/src/teakra/appveyor.yml b/src/teakra/appveyor.yml new file mode 100644 index 0000000..84e15c6 --- /dev/null +++ b/src/teakra/appveyor.yml @@ -0,0 +1,32 @@ +# shallow clone +clone_depth: 5 + +environment: + matrix: + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 + cmake_generator: "Visual Studio 15 2017 Win64" + +platform: + - x64 + +configuration: + - Release + +install: + - git submodule update --init --recursive + +before_build: + - mkdir build + - cd build + - cmake .. -G "%cmake_generator%" -DTEAKRA_TEST_ASSETS_DIR="%USERPROFILE%\assets" -DTEAKRA_RUN_TESTS=ON + - cd .. + +cache: + - '%USERPROFILE%\assets' + +build: + project: build/teakra.sln + parallel: true + +test_script: +- cd build && ctest -VV -C Release && cd .. diff --git a/src/teakra/externals/CMakeLists.txt b/src/teakra/externals/CMakeLists.txt new file mode 100644 index 0000000..ca73442 --- /dev/null +++ b/src/teakra/externals/CMakeLists.txt @@ -0,0 +1,5 @@ +if (TEAKRA_BUILD_UNIT_TESTS) + add_library(catch INTERFACE) + target_include_directories(catch INTERFACE + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/catch>) +endif() diff --git a/src/teakra/externals/catch/catch.hpp b/src/teakra/externals/catch/catch.hpp new file mode 100644 index 0000000..b1b2411 --- /dev/null +++ b/src/teakra/externals/catch/catch.hpp @@ -0,0 +1,14362 @@ +/* + * Catch v2.5.0 + * Generated: 2018-11-26 20:46:12.165372 + * ---------------------------------------------------------- + * This file has been merged from multiple headers. Please don't edit it directly + * Copyright (c) 2018 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ +#ifndef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED +#define TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED +// start catch.hpp + + +#define CATCH_VERSION_MAJOR 2 +#define CATCH_VERSION_MINOR 5 +#define CATCH_VERSION_PATCH 0 + +#ifdef __clang__ +# pragma clang system_header +#elif defined __GNUC__ +# pragma GCC system_header +#endif + +// start catch_suppress_warnings.h + +#ifdef __clang__ +# ifdef __ICC // icpc defines the __clang__ macro +# pragma warning(push) +# pragma warning(disable: 161 1682) +# else // __ICC +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wpadded" +# pragma clang diagnostic ignored "-Wswitch-enum" +# pragma clang diagnostic ignored "-Wcovered-switch-default" +# endif +#elif defined __GNUC__ + // GCC likes to warn on REQUIREs, and we cannot suppress them + // locally because g++'s support for _Pragma is lacking in older, + // still supported, versions +# pragma GCC diagnostic ignored "-Wparentheses" +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wunused-variable" +# pragma GCC diagnostic ignored "-Wpadded" +#endif +// end catch_suppress_warnings.h +#if defined(CATCH_CONFIG_MAIN) || defined(CATCH_CONFIG_RUNNER) +# define CATCH_IMPL +# define CATCH_CONFIG_ALL_PARTS +#endif + +// In the impl file, we want to have access to all parts of the headers +// Can also be used to sanely support PCHs +#if defined(CATCH_CONFIG_ALL_PARTS) +# define CATCH_CONFIG_EXTERNAL_INTERFACES +# if defined(CATCH_CONFIG_DISABLE_MATCHERS) +# undef CATCH_CONFIG_DISABLE_MATCHERS +# endif +# if !defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) +# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +# endif +#endif + +#if !defined(CATCH_CONFIG_IMPL_ONLY) +// start catch_platform.h + +#ifdef __APPLE__ +# include <TargetConditionals.h> +# if TARGET_OS_OSX == 1 +# define CATCH_PLATFORM_MAC +# elif TARGET_OS_IPHONE == 1 +# define CATCH_PLATFORM_IPHONE +# endif + +#elif defined(linux) || defined(__linux) || defined(__linux__) +# define CATCH_PLATFORM_LINUX + +#elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || defined(__MINGW32__) +# define CATCH_PLATFORM_WINDOWS +#endif + +// end catch_platform.h + +#ifdef CATCH_IMPL +# ifndef CLARA_CONFIG_MAIN +# define CLARA_CONFIG_MAIN_NOT_DEFINED +# define CLARA_CONFIG_MAIN +# endif +#endif + +// start catch_user_interfaces.h + +namespace Catch { + unsigned int rngSeed(); +} + +// end catch_user_interfaces.h +// start catch_tag_alias_autoregistrar.h + +// start catch_common.h + +// start catch_compiler_capabilities.h + +// Detect a number of compiler features - by compiler +// The following features are defined: +// +// CATCH_CONFIG_COUNTER : is the __COUNTER__ macro supported? +// CATCH_CONFIG_WINDOWS_SEH : is Windows SEH supported? +// CATCH_CONFIG_POSIX_SIGNALS : are POSIX signals supported? +// CATCH_CONFIG_DISABLE_EXCEPTIONS : Are exceptions enabled? +// **************** +// Note to maintainers: if new toggles are added please document them +// in configuration.md, too +// **************** + +// In general each macro has a _NO_<feature name> form +// (e.g. CATCH_CONFIG_NO_POSIX_SIGNALS) which disables the feature. +// Many features, at point of detection, define an _INTERNAL_ macro, so they +// can be combined, en-mass, with the _NO_ forms later. + +#ifdef __cplusplus + +# if (__cplusplus >= 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L) +# define CATCH_CPP14_OR_GREATER +# endif + +# if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) +# define CATCH_CPP17_OR_GREATER +# endif + +#endif + +#if defined(CATCH_CPP17_OR_GREATER) +# define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS +#endif + +#ifdef __clang__ + +# define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + _Pragma( "clang diagnostic push" ) \ + _Pragma( "clang diagnostic ignored \"-Wexit-time-destructors\"" ) \ + _Pragma( "clang diagnostic ignored \"-Wglobal-constructors\"") +# define CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ + _Pragma( "clang diagnostic pop" ) + +# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ + _Pragma( "clang diagnostic push" ) \ + _Pragma( "clang diagnostic ignored \"-Wparentheses\"" ) +# define CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \ + _Pragma( "clang diagnostic pop" ) + +# define CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS \ + _Pragma( "clang diagnostic push" ) \ + _Pragma( "clang diagnostic ignored \"-Wunused-variable\"" ) +# define CATCH_INTERNAL_UNSUPPRESS_UNUSED_WARNINGS \ + _Pragma( "clang diagnostic pop" ) + +#endif // __clang__ + +//////////////////////////////////////////////////////////////////////////////// +// Assume that non-Windows platforms support posix signals by default +#if !defined(CATCH_PLATFORM_WINDOWS) + #define CATCH_INTERNAL_CONFIG_POSIX_SIGNALS +#endif + +//////////////////////////////////////////////////////////////////////////////// +// We know some environments not to support full POSIX signals +#if defined(__CYGWIN__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(__DJGPP__) + #define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS +#endif + +#ifdef __OS400__ +# define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS +# define CATCH_CONFIG_COLOUR_NONE +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Android somehow still does not support std::to_string +#if defined(__ANDROID__) +# define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Not all Windows environments support SEH properly +#if defined(__MINGW32__) +# define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH +#endif + +//////////////////////////////////////////////////////////////////////////////// +// PS4 +#if defined(__ORBIS__) +# define CATCH_INTERNAL_CONFIG_NO_NEW_CAPTURE +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Cygwin +#ifdef __CYGWIN__ + +// Required for some versions of Cygwin to declare gettimeofday +// see: http://stackoverflow.com/questions/36901803/gettimeofday-not-declared-in-this-scope-cygwin +# define _BSD_SOURCE +// some versions of cygwin (most) do not support std::to_string. Use the libstd check. +// https://gcc.gnu.org/onlinedocs/gcc-4.8.2/libstdc++/api/a01053_source.html line 2812-2813 +# if !((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \ + && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)) + +# define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING + +# endif +#endif // __CYGWIN__ + +//////////////////////////////////////////////////////////////////////////////// +// Visual C++ +#ifdef _MSC_VER + +# if _MSC_VER >= 1900 // Visual Studio 2015 or newer +# define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS +# endif + +// Universal Windows platform does not support SEH +// Or console colours (or console at all...) +# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) +# define CATCH_CONFIG_COLOUR_NONE +# else +# define CATCH_INTERNAL_CONFIG_WINDOWS_SEH +# endif + +// MSVC traditional preprocessor needs some workaround for __VA_ARGS__ +// _MSVC_TRADITIONAL == 0 means new conformant preprocessor +// _MSVC_TRADITIONAL == 1 means old traditional non-conformant preprocessor +# if !defined(_MSVC_TRADITIONAL) || (defined(_MSVC_TRADITIONAL) && _MSVC_TRADITIONAL) +# define CATCH_INTERNAL_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +# endif + +#endif // _MSC_VER + +//////////////////////////////////////////////////////////////////////////////// +// Check if we are compiled with -fno-exceptions or equivalent +#if defined(__EXCEPTIONS) || defined(__cpp_exceptions) || defined(_CPPUNWIND) +# define CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED +#endif + +//////////////////////////////////////////////////////////////////////////////// +// DJGPP +#ifdef __DJGPP__ +# define CATCH_INTERNAL_CONFIG_NO_WCHAR +#endif // __DJGPP__ + +//////////////////////////////////////////////////////////////////////////////// +// Embarcadero C++Build +#if defined(__BORLANDC__) + #define CATCH_INTERNAL_CONFIG_POLYFILL_ISNAN +#endif + +//////////////////////////////////////////////////////////////////////////////// + +// Use of __COUNTER__ is suppressed during code analysis in +// CLion/AppCode 2017.2.x and former, because __COUNTER__ is not properly +// handled by it. +// Otherwise all supported compilers support COUNTER macro, +// but user still might want to turn it off +#if ( !defined(__JETBRAINS_IDE__) || __JETBRAINS_IDE__ >= 20170300L ) + #define CATCH_INTERNAL_CONFIG_COUNTER +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Check if string_view is available and usable +// The check is split apart to work around v140 (VS2015) preprocessor issue... +#if defined(__has_include) +#if __has_include(<string_view>) && defined(CATCH_CPP17_OR_GREATER) +# define CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW +#endif +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Check if variant is available and usable +#if defined(__has_include) +# if __has_include(<variant>) && defined(CATCH_CPP17_OR_GREATER) +# if defined(__clang__) && (__clang_major__ < 8) + // work around clang bug with libstdc++ https://bugs.llvm.org/show_bug.cgi?id=31852 + // fix should be in clang 8, workaround in libstdc++ 8.2 +# include <ciso646> +# if defined(__GLIBCXX__) && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE < 9) +# define CATCH_CONFIG_NO_CPP17_VARIANT +# else +# define CATCH_INTERNAL_CONFIG_CPP17_VARIANT +# endif // defined(__GLIBCXX__) && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE < 9) +# endif // defined(__clang__) && (__clang_major__ < 8) +# endif // __has_include(<variant>) && defined(CATCH_CPP17_OR_GREATER) +#endif // __has_include + +#if defined(CATCH_INTERNAL_CONFIG_COUNTER) && !defined(CATCH_CONFIG_NO_COUNTER) && !defined(CATCH_CONFIG_COUNTER) +# define CATCH_CONFIG_COUNTER +#endif +#if defined(CATCH_INTERNAL_CONFIG_WINDOWS_SEH) && !defined(CATCH_CONFIG_NO_WINDOWS_SEH) && !defined(CATCH_CONFIG_WINDOWS_SEH) && !defined(CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH) +# define CATCH_CONFIG_WINDOWS_SEH +#endif +// This is set by default, because we assume that unix compilers are posix-signal-compatible by default. +#if defined(CATCH_INTERNAL_CONFIG_POSIX_SIGNALS) && !defined(CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_POSIX_SIGNALS) +# define CATCH_CONFIG_POSIX_SIGNALS +#endif +// This is set by default, because we assume that compilers with no wchar_t support are just rare exceptions. +#if !defined(CATCH_INTERNAL_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_WCHAR) +# define CATCH_CONFIG_WCHAR +#endif + +#if !defined(CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_CPP11_TO_STRING) +# define CATCH_CONFIG_CPP11_TO_STRING +#endif + +#if defined(CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) +# define CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS +#endif + +#if defined(CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_NO_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_CPP17_STRING_VIEW) +# define CATCH_CONFIG_CPP17_STRING_VIEW +#endif + +#if defined(CATCH_INTERNAL_CONFIG_CPP17_VARIANT) && !defined(CATCH_CONFIG_NO_CPP17_VARIANT) && !defined(CATCH_CONFIG_CPP17_VARIANT) +# define CATCH_CONFIG_CPP17_VARIANT +#endif + +#if defined(CATCH_CONFIG_EXPERIMENTAL_REDIRECT) +# define CATCH_INTERNAL_CONFIG_NEW_CAPTURE +#endif + +#if defined(CATCH_INTERNAL_CONFIG_NEW_CAPTURE) && !defined(CATCH_INTERNAL_CONFIG_NO_NEW_CAPTURE) && !defined(CATCH_CONFIG_NO_NEW_CAPTURE) && !defined(CATCH_CONFIG_NEW_CAPTURE) +# define CATCH_CONFIG_NEW_CAPTURE +#endif + +#if !defined(CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED) && !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) +# define CATCH_CONFIG_DISABLE_EXCEPTIONS +#endif + +#if defined(CATCH_INTERNAL_CONFIG_POLYFILL_ISNAN) && !defined(CATCH_CONFIG_NO_POLYFILL_ISNAN) && !defined(CATCH_CONFIG_POLYFILL_ISNAN) +# define CATCH_CONFIG_POLYFILL_ISNAN +#endif + +#if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS +# define CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS +#endif +#if !defined(CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS +# define CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS +#endif +#if !defined(CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS +# define CATCH_INTERNAL_UNSUPPRESS_UNUSED_WARNINGS +#endif + +#if defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) +#define CATCH_TRY if ((true)) +#define CATCH_CATCH_ALL if ((false)) +#define CATCH_CATCH_ANON(type) if ((false)) +#else +#define CATCH_TRY try +#define CATCH_CATCH_ALL catch (...) +#define CATCH_CATCH_ANON(type) catch (type) +#endif + +#if defined(CATCH_INTERNAL_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR) && !defined(CATCH_CONFIG_NO_TRADITIONAL_MSVC_PREPROCESSOR) && !defined(CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR) +#define CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#endif + +// end catch_compiler_capabilities.h +#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line +#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) +#ifdef CATCH_CONFIG_COUNTER +# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ ) +#else +# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ ) +#endif + +#include <iosfwd> +#include <string> +#include <cstdint> + +// We need a dummy global operator<< so we can bring it into Catch namespace later +struct Catch_global_namespace_dummy {}; +std::ostream& operator<<(std::ostream&, Catch_global_namespace_dummy); + +namespace Catch { + + struct CaseSensitive { enum Choice { + Yes, + No + }; }; + + class NonCopyable { + NonCopyable( NonCopyable const& ) = delete; + NonCopyable( NonCopyable && ) = delete; + NonCopyable& operator = ( NonCopyable const& ) = delete; + NonCopyable& operator = ( NonCopyable && ) = delete; + + protected: + NonCopyable(); + virtual ~NonCopyable(); + }; + + struct SourceLineInfo { + + SourceLineInfo() = delete; + SourceLineInfo( char const* _file, std::size_t _line ) noexcept + : file( _file ), + line( _line ) + {} + + SourceLineInfo( SourceLineInfo const& other ) = default; + SourceLineInfo( SourceLineInfo && ) = default; + SourceLineInfo& operator = ( SourceLineInfo const& ) = default; + SourceLineInfo& operator = ( SourceLineInfo && ) = default; + + bool empty() const noexcept; + bool operator == ( SourceLineInfo const& other ) const noexcept; + bool operator < ( SourceLineInfo const& other ) const noexcept; + + char const* file; + std::size_t line; + }; + + std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ); + + // Bring in operator<< from global namespace into Catch namespace + // This is necessary because the overload of operator<< above makes + // lookup stop at namespace Catch + using ::operator<<; + + // Use this in variadic streaming macros to allow + // >> +StreamEndStop + // as well as + // >> stuff +StreamEndStop + struct StreamEndStop { + std::string operator+() const; + }; + template<typename T> + T const& operator + ( T const& value, StreamEndStop ) { + return value; + } +} + +#define CATCH_INTERNAL_LINEINFO \ + ::Catch::SourceLineInfo( __FILE__, static_cast<std::size_t>( __LINE__ ) ) + +// end catch_common.h +namespace Catch { + + struct RegistrarForTagAliases { + RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ); + }; + +} // end namespace Catch + +#define CATCH_REGISTER_TAG_ALIAS( alias, spec ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::RegistrarForTagAliases INTERNAL_CATCH_UNIQUE_NAME( AutoRegisterTagAlias )( alias, spec, CATCH_INTERNAL_LINEINFO ); } \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS + +// end catch_tag_alias_autoregistrar.h +// start catch_test_registry.h + +// start catch_interfaces_testcase.h + +#include <vector> +#include <memory> + +namespace Catch { + + class TestSpec; + + struct ITestInvoker { + virtual void invoke () const = 0; + virtual ~ITestInvoker(); + }; + + using ITestCasePtr = std::shared_ptr<ITestInvoker>; + + class TestCase; + struct IConfig; + + struct ITestCaseRegistry { + virtual ~ITestCaseRegistry(); + virtual std::vector<TestCase> const& getAllTests() const = 0; + virtual std::vector<TestCase> const& getAllTestsSorted( IConfig const& config ) const = 0; + }; + + bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ); + std::vector<TestCase> filterTests( std::vector<TestCase> const& testCases, TestSpec const& testSpec, IConfig const& config ); + std::vector<TestCase> const& getAllTestCasesSorted( IConfig const& config ); + +} + +// end catch_interfaces_testcase.h +// start catch_stringref.h + +#include <cstddef> +#include <string> +#include <iosfwd> + +namespace Catch { + + class StringData; + + /// A non-owning string class (similar to the forthcoming std::string_view) + /// Note that, because a StringRef may be a substring of another string, + /// it may not be null terminated. c_str() must return a null terminated + /// string, however, and so the StringRef will internally take ownership + /// (taking a copy), if necessary. In theory this ownership is not externally + /// visible - but it does mean (substring) StringRefs should not be shared between + /// threads. + class StringRef { + public: + using size_type = std::size_t; + + private: + friend struct StringRefTestAccess; + + char const* m_start; + size_type m_size; + + char* m_data = nullptr; + + void takeOwnership(); + + static constexpr char const* const s_empty = ""; + + public: // construction/ assignment + StringRef() noexcept + : StringRef( s_empty, 0 ) + {} + + StringRef( StringRef const& other ) noexcept + : m_start( other.m_start ), + m_size( other.m_size ) + {} + + StringRef( StringRef&& other ) noexcept + : m_start( other.m_start ), + m_size( other.m_size ), + m_data( other.m_data ) + { + other.m_data = nullptr; + } + + StringRef( char const* rawChars ) noexcept; + + StringRef( char const* rawChars, size_type size ) noexcept + : m_start( rawChars ), + m_size( size ) + {} + + StringRef( std::string const& stdString ) noexcept + : m_start( stdString.c_str() ), + m_size( stdString.size() ) + {} + + ~StringRef() noexcept { + delete[] m_data; + } + + auto operator = ( StringRef const &other ) noexcept -> StringRef& { + delete[] m_data; + m_data = nullptr; + m_start = other.m_start; + m_size = other.m_size; + return *this; + } + + operator std::string() const; + + void swap( StringRef& other ) noexcept; + + public: // operators + auto operator == ( StringRef const& other ) const noexcept -> bool; + auto operator != ( StringRef const& other ) const noexcept -> bool; + + auto operator[] ( size_type index ) const noexcept -> char; + + public: // named queries + auto empty() const noexcept -> bool { + return m_size == 0; + } + auto size() const noexcept -> size_type { + return m_size; + } + + auto numberOfCharacters() const noexcept -> size_type; + auto c_str() const -> char const*; + + public: // substrings and searches + auto substr( size_type start, size_type size ) const noexcept -> StringRef; + + // Returns the current start pointer. + // Note that the pointer can change when if the StringRef is a substring + auto currentData() const noexcept -> char const*; + + private: // ownership queries - may not be consistent between calls + auto isOwned() const noexcept -> bool; + auto isSubstring() const noexcept -> bool; + }; + + auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string; + auto operator + ( StringRef const& lhs, char const* rhs ) -> std::string; + auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string; + + auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&; + auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&; + + inline auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { + return StringRef( rawChars, size ); + } + +} // namespace Catch + +inline auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef { + return Catch::StringRef( rawChars, size ); +} + +// end catch_stringref.h +// start catch_type_traits.hpp + + +namespace Catch{ + +#ifdef CATCH_CPP17_OR_GREATER + template <typename...> + inline constexpr auto is_unique = std::true_type{}; + + template <typename T, typename... Rest> + inline constexpr auto is_unique<T, Rest...> = std::bool_constant< + (!std::is_same_v<T, Rest> && ...) && is_unique<Rest...> + >{}; +#else + +template <typename...> +struct is_unique : std::true_type{}; + +template <typename T0, typename T1, typename... Rest> +struct is_unique<T0, T1, Rest...> : std::integral_constant +<bool, + !std::is_same<T0, T1>::value + && is_unique<T0, Rest...>::value + && is_unique<T1, Rest...>::value +>{}; + +#endif +} + +// end catch_type_traits.hpp +// start catch_preprocessor.hpp + + +#define CATCH_RECURSION_LEVEL0(...) __VA_ARGS__ +#define CATCH_RECURSION_LEVEL1(...) CATCH_RECURSION_LEVEL0(CATCH_RECURSION_LEVEL0(CATCH_RECURSION_LEVEL0(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL2(...) CATCH_RECURSION_LEVEL1(CATCH_RECURSION_LEVEL1(CATCH_RECURSION_LEVEL1(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL3(...) CATCH_RECURSION_LEVEL2(CATCH_RECURSION_LEVEL2(CATCH_RECURSION_LEVEL2(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL4(...) CATCH_RECURSION_LEVEL3(CATCH_RECURSION_LEVEL3(CATCH_RECURSION_LEVEL3(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL5(...) CATCH_RECURSION_LEVEL4(CATCH_RECURSION_LEVEL4(CATCH_RECURSION_LEVEL4(__VA_ARGS__))) + +#ifdef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#define INTERNAL_CATCH_EXPAND_VARGS(...) __VA_ARGS__ +// MSVC needs more evaluations +#define CATCH_RECURSION_LEVEL6(...) CATCH_RECURSION_LEVEL5(CATCH_RECURSION_LEVEL5(CATCH_RECURSION_LEVEL5(__VA_ARGS__))) +#define CATCH_RECURSE(...) CATCH_RECURSION_LEVEL6(CATCH_RECURSION_LEVEL6(__VA_ARGS__)) +#else +#define CATCH_RECURSE(...) CATCH_RECURSION_LEVEL5(__VA_ARGS__) +#endif + +#define CATCH_REC_END(...) +#define CATCH_REC_OUT + +#define CATCH_EMPTY() +#define CATCH_DEFER(id) id CATCH_EMPTY() + +#define CATCH_REC_GET_END2() 0, CATCH_REC_END +#define CATCH_REC_GET_END1(...) CATCH_REC_GET_END2 +#define CATCH_REC_GET_END(...) CATCH_REC_GET_END1 +#define CATCH_REC_NEXT0(test, next, ...) next CATCH_REC_OUT +#define CATCH_REC_NEXT1(test, next) CATCH_DEFER ( CATCH_REC_NEXT0 ) ( test, next, 0) +#define CATCH_REC_NEXT(test, next) CATCH_REC_NEXT1(CATCH_REC_GET_END test, next) + +#define CATCH_REC_LIST0(f, x, peek, ...) , f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1) ) ( f, peek, __VA_ARGS__ ) +#define CATCH_REC_LIST1(f, x, peek, ...) , f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST0) ) ( f, peek, __VA_ARGS__ ) +#define CATCH_REC_LIST2(f, x, peek, ...) f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1) ) ( f, peek, __VA_ARGS__ ) + +#define CATCH_REC_LIST0_UD(f, userdata, x, peek, ...) , f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1_UD) ) ( f, userdata, peek, __VA_ARGS__ ) +#define CATCH_REC_LIST1_UD(f, userdata, x, peek, ...) , f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST0_UD) ) ( f, userdata, peek, __VA_ARGS__ ) +#define CATCH_REC_LIST2_UD(f, userdata, x, peek, ...) f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1_UD) ) ( f, userdata, peek, __VA_ARGS__ ) + +// Applies the function macro `f` to each of the remaining parameters, inserts commas between the results, +// and passes userdata as the first parameter to each invocation, +// e.g. CATCH_REC_LIST_UD(f, x, a, b, c) evaluates to f(x, a), f(x, b), f(x, c) +#define CATCH_REC_LIST_UD(f, userdata, ...) CATCH_RECURSE(CATCH_REC_LIST2_UD(f, userdata, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) + +#define CATCH_REC_LIST(f, ...) CATCH_RECURSE(CATCH_REC_LIST2(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) + +#define INTERNAL_CATCH_EXPAND1(param) INTERNAL_CATCH_EXPAND2(param) +#define INTERNAL_CATCH_EXPAND2(...) INTERNAL_CATCH_NO## __VA_ARGS__ +#define INTERNAL_CATCH_DEF(...) INTERNAL_CATCH_DEF __VA_ARGS__ +#define INTERNAL_CATCH_NOINTERNAL_CATCH_DEF + +#define INTERNAL_CATCH_REMOVE_PARENS(...) INTERNAL_CATCH_EXPAND1(INTERNAL_CATCH_DEF __VA_ARGS__) + +#define INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME2(Name, ...) INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME3(Name, __VA_ARGS__) +#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#define INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME3(Name,...) Name " - " #__VA_ARGS__ +#define INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME(Name,...) INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME2(Name, INTERNAL_CATCH_REMOVE_PARENS(__VA_ARGS__)) +#else +// MSVC is adding extra space and needs more calls to properly remove () +#define INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME3(Name,...) Name " -" #__VA_ARGS__ +#define INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME1(Name, ...) INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME2(Name, __VA_ARGS__) +#define INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME(Name, ...) INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME1(Name, INTERNAL_CATCH_EXPAND_VARGS(INTERNAL_CATCH_REMOVE_PARENS(__VA_ARGS__))) +#endif + +// end catch_preprocessor.hpp +namespace Catch { + +template<typename C> +class TestInvokerAsMethod : public ITestInvoker { + void (C::*m_testAsMethod)(); +public: + TestInvokerAsMethod( void (C::*testAsMethod)() ) noexcept : m_testAsMethod( testAsMethod ) {} + + void invoke() const override { + C obj; + (obj.*m_testAsMethod)(); + } +}; + +auto makeTestInvoker( void(*testAsFunction)() ) noexcept -> ITestInvoker*; + +template<typename C> +auto makeTestInvoker( void (C::*testAsMethod)() ) noexcept -> ITestInvoker* { + return new(std::nothrow) TestInvokerAsMethod<C>( testAsMethod ); +} + +struct NameAndTags { + NameAndTags( StringRef const& name_ = StringRef(), StringRef const& tags_ = StringRef() ) noexcept; + StringRef name; + StringRef tags; +}; + +struct AutoReg : NonCopyable { + AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef const& classOrMethod, NameAndTags const& nameAndTags ) noexcept; + ~AutoReg(); +}; + +} // end namespace Catch + +#if defined(CATCH_CONFIG_DISABLE) + #define INTERNAL_CATCH_TESTCASE_NO_REGISTRATION( TestName, ... ) \ + static void TestName() + #define INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION( TestName, ClassName, ... ) \ + namespace{ \ + struct TestName : INTERNAL_CATCH_REMOVE_PARENS(ClassName) { \ + void test(); \ + }; \ + } \ + void TestName::test() + #define INTERNAL_CATCH_TEMPLATE_TEST_CASE_NO_REGISTRATION( TestName, ... ) \ + template<typename TestType> \ + static void TestName() + #define INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD_NO_REGISTRATION( TestName, ClassName, ... ) \ + namespace{ \ + template<typename TestType> \ + struct TestName : INTERNAL_CATCH_REMOVE_PARENS(ClassName <TestType>) { \ + void test(); \ + }; \ + } \ + template<typename TestType> \ + void TestName::test() +#endif + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_TESTCASE2( TestName, ... ) \ + static void TestName(); \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( &TestName ), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ __VA_ARGS__ } ); } /* NOLINT */ \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ + static void TestName() + #define INTERNAL_CATCH_TESTCASE( ... ) \ + INTERNAL_CATCH_TESTCASE2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), __VA_ARGS__ ) + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( &QualifiedMethod ), CATCH_INTERNAL_LINEINFO, "&" #QualifiedMethod, Catch::NameAndTags{ __VA_ARGS__ } ); } /* NOLINT */ \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_TEST_CASE_METHOD2( TestName, ClassName, ... )\ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ \ + struct TestName : INTERNAL_CATCH_REMOVE_PARENS(ClassName) { \ + void test(); \ + }; \ + Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( Catch::makeTestInvoker( &TestName::test ), CATCH_INTERNAL_LINEINFO, #ClassName, Catch::NameAndTags{ __VA_ARGS__ } ); /* NOLINT */ \ + } \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ + void TestName::test() + #define INTERNAL_CATCH_TEST_CASE_METHOD( ClassName, ... ) \ + INTERNAL_CATCH_TEST_CASE_METHOD2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), ClassName, __VA_ARGS__ ) + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_REGISTER_TESTCASE( Function, ... ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( Function ), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ __VA_ARGS__ } ); /* NOLINT */ \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_TEMPLATE_TEST_CASE_2(TestName, TestFunc, Name, Tags, ... )\ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + template<typename TestType> \ + static void TestFunc();\ + namespace {\ + template<typename...Types> \ + struct TestName{\ + template<typename...Ts> \ + TestName(Ts...names){\ + CATCH_INTERNAL_CHECK_UNIQUE_TYPES(CATCH_REC_LIST(INTERNAL_CATCH_REMOVE_PARENS, __VA_ARGS__)) \ + using expander = int[];\ + (void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestFunc<Types> ), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ names, Tags } ), 0)... };/* NOLINT */ \ + }\ + };\ + INTERNAL_CATCH_TEMPLATE_REGISTRY_INITIATE(TestName, Name, __VA_ARGS__) \ + }\ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ + template<typename TestType> \ + static void TestFunc() + +#if defined(CATCH_CPP17_OR_GREATER) +#define CATCH_INTERNAL_CHECK_UNIQUE_TYPES(...) static_assert(Catch::is_unique<__VA_ARGS__>,"Duplicate type detected in declaration of template test case"); +#else +#define CATCH_INTERNAL_CHECK_UNIQUE_TYPES(...) static_assert(Catch::is_unique<__VA_ARGS__>::value,"Duplicate type detected in declaration of template test case"); +#endif + +#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR + #define INTERNAL_CATCH_TEMPLATE_TEST_CASE(Name, Tags, ...) \ + INTERNAL_CATCH_TEMPLATE_TEST_CASE_2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ), INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____F_U_N_C____ ), Name, Tags, __VA_ARGS__ ) +#else + #define INTERNAL_CATCH_TEMPLATE_TEST_CASE(Name, Tags, ...) \ + INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE_2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ), INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____F_U_N_C____ ), Name, Tags, __VA_ARGS__ ) ) +#endif + + #define INTERNAL_CATCH_TEMPLATE_REGISTRY_INITIATE(TestName, Name, ...)\ + static int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){\ + TestName<CATCH_REC_LIST(INTERNAL_CATCH_REMOVE_PARENS, __VA_ARGS__)>(CATCH_REC_LIST_UD(INTERNAL_CATCH_TEMPLATE_UNIQUE_NAME,Name, __VA_ARGS__));\ + return 0;\ + }(); + + #define INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD_2( TestNameClass, TestName, ClassName, Name, Tags, ... ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ \ + template<typename TestType> \ + struct TestName : INTERNAL_CATCH_REMOVE_PARENS(ClassName <TestType>) { \ + void test();\ + };\ + template<typename...Types> \ + struct TestNameClass{\ + template<typename...Ts> \ + TestNameClass(Ts...names){\ + CATCH_INTERNAL_CHECK_UNIQUE_TYPES(CATCH_REC_LIST(INTERNAL_CATCH_REMOVE_PARENS, __VA_ARGS__)) \ + using expander = int[];\ + (void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestName<Types>::test ), CATCH_INTERNAL_LINEINFO, #ClassName, Catch::NameAndTags{ names, Tags } ), 0)... };/* NOLINT */ \ + }\ + };\ + INTERNAL_CATCH_TEMPLATE_REGISTRY_INITIATE(TestNameClass, Name, __VA_ARGS__)\ + }\ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS\ + template<typename TestType> \ + void TestName<TestType>::test() + +#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR + #define INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD( ClassName, Name, Tags,... ) \ + INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD_2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____C_L_A_S_S____ ), INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ) , ClassName, Name, Tags, __VA_ARGS__ ) +#else + #define INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD( ClassName, Name, Tags,... ) \ + INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD_2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____C_L_A_S_S____ ), INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ) , ClassName, Name, Tags, __VA_ARGS__ ) ) +#endif + +// end catch_test_registry.h +// start catch_capture.hpp + +// start catch_assertionhandler.h + +// start catch_assertioninfo.h + +// start catch_result_type.h + +namespace Catch { + + // ResultWas::OfType enum + struct ResultWas { enum OfType { + Unknown = -1, + Ok = 0, + Info = 1, + Warning = 2, + + FailureBit = 0x10, + + ExpressionFailed = FailureBit | 1, + ExplicitFailure = FailureBit | 2, + + Exception = 0x100 | FailureBit, + + ThrewException = Exception | 1, + DidntThrowException = Exception | 2, + + FatalErrorCondition = 0x200 | FailureBit + + }; }; + + bool isOk( ResultWas::OfType resultType ); + bool isJustInfo( int flags ); + + // ResultDisposition::Flags enum + struct ResultDisposition { enum Flags { + Normal = 0x01, + + ContinueOnFailure = 0x02, // Failures fail test, but execution continues + FalseTest = 0x04, // Prefix expression with ! + SuppressFail = 0x08 // Failures are reported but do not fail the test + }; }; + + ResultDisposition::Flags operator | ( ResultDisposition::Flags lhs, ResultDisposition::Flags rhs ); + + bool shouldContinueOnFailure( int flags ); + inline bool isFalseTest( int flags ) { return ( flags & ResultDisposition::FalseTest ) != 0; } + bool shouldSuppressFailure( int flags ); + +} // end namespace Catch + +// end catch_result_type.h +namespace Catch { + + struct AssertionInfo + { + StringRef macroName; + SourceLineInfo lineInfo; + StringRef capturedExpression; + ResultDisposition::Flags resultDisposition; + + // We want to delete this constructor but a compiler bug in 4.8 means + // the struct is then treated as non-aggregate + //AssertionInfo() = delete; + }; + +} // end namespace Catch + +// end catch_assertioninfo.h +// start catch_decomposer.h + +// start catch_tostring.h + +#include <vector> +#include <cstddef> +#include <type_traits> +#include <string> +// start catch_stream.h + +#include <iosfwd> +#include <cstddef> +#include <ostream> + +namespace Catch { + + std::ostream& cout(); + std::ostream& cerr(); + std::ostream& clog(); + + class StringRef; + + struct IStream { + virtual ~IStream(); + virtual std::ostream& stream() const = 0; + }; + + auto makeStream( StringRef const &filename ) -> IStream const*; + + class ReusableStringStream { + std::size_t m_index; + std::ostream* m_oss; + public: + ReusableStringStream(); + ~ReusableStringStream(); + + auto str() const -> std::string; + + template<typename T> + auto operator << ( T const& value ) -> ReusableStringStream& { + *m_oss << value; + return *this; + } + auto get() -> std::ostream& { return *m_oss; } + }; +} + +// end catch_stream.h + +#ifdef CATCH_CONFIG_CPP17_STRING_VIEW +#include <string_view> +#endif + +#ifdef __OBJC__ +// start catch_objc_arc.hpp + +#import <Foundation/Foundation.h> + +#ifdef __has_feature +#define CATCH_ARC_ENABLED __has_feature(objc_arc) +#else +#define CATCH_ARC_ENABLED 0 +#endif + +void arcSafeRelease( NSObject* obj ); +id performOptionalSelector( id obj, SEL sel ); + +#if !CATCH_ARC_ENABLED +inline void arcSafeRelease( NSObject* obj ) { + [obj release]; +} +inline id performOptionalSelector( id obj, SEL sel ) { + if( [obj respondsToSelector: sel] ) + return [obj performSelector: sel]; + return nil; +} +#define CATCH_UNSAFE_UNRETAINED +#define CATCH_ARC_STRONG +#else +inline void arcSafeRelease( NSObject* ){} +inline id performOptionalSelector( id obj, SEL sel ) { +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" +#endif + if( [obj respondsToSelector: sel] ) + return [obj performSelector: sel]; +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + return nil; +} +#define CATCH_UNSAFE_UNRETAINED __unsafe_unretained +#define CATCH_ARC_STRONG __strong +#endif + +// end catch_objc_arc.hpp +#endif + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable:4180) // We attempt to stream a function (address) by const&, which MSVC complains about but is harmless +#endif + +namespace Catch { + namespace Detail { + + extern const std::string unprintableString; + + std::string rawMemoryToString( const void *object, std::size_t size ); + + template<typename T> + std::string rawMemoryToString( const T& object ) { + return rawMemoryToString( &object, sizeof(object) ); + } + + template<typename T> + class IsStreamInsertable { + template<typename SS, typename TT> + static auto test(int) + -> decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type()); + + template<typename, typename> + static auto test(...)->std::false_type; + + public: + static const bool value = decltype(test<std::ostream, const T&>(0))::value; + }; + + template<typename E> + std::string convertUnknownEnumToString( E e ); + + template<typename T> + typename std::enable_if< + !std::is_enum<T>::value && !std::is_base_of<std::exception, T>::value, + std::string>::type convertUnstreamable( T const& ) { + return Detail::unprintableString; + } + template<typename T> + typename std::enable_if< + !std::is_enum<T>::value && std::is_base_of<std::exception, T>::value, + std::string>::type convertUnstreamable(T const& ex) { + return ex.what(); + } + + template<typename T> + typename std::enable_if< + std::is_enum<T>::value + , std::string>::type convertUnstreamable( T const& value ) { + return convertUnknownEnumToString( value ); + } + +#if defined(_MANAGED) + //! Convert a CLR string to a utf8 std::string + template<typename T> + std::string clrReferenceToString( T^ ref ) { + if (ref == nullptr) + return std::string("null"); + auto bytes = System::Text::Encoding::UTF8->GetBytes(ref->ToString()); + cli::pin_ptr<System::Byte> p = &bytes[0]; + return std::string(reinterpret_cast<char const *>(p), bytes->Length); + } +#endif + + } // namespace Detail + + // If we decide for C++14, change these to enable_if_ts + template <typename T, typename = void> + struct StringMaker { + template <typename Fake = T> + static + typename std::enable_if<::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type + convert(const Fake& value) { + ReusableStringStream rss; + // NB: call using the function-like syntax to avoid ambiguity with + // user-defined templated operator<< under clang. + rss.operator<<(value); + return rss.str(); + } + + template <typename Fake = T> + static + typename std::enable_if<!::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type + convert( const Fake& value ) { +#if !defined(CATCH_CONFIG_FALLBACK_STRINGIFIER) + return Detail::convertUnstreamable(value); +#else + return CATCH_CONFIG_FALLBACK_STRINGIFIER(value); +#endif + } + }; + + namespace Detail { + + // This function dispatches all stringification requests inside of Catch. + // Should be preferably called fully qualified, like ::Catch::Detail::stringify + template <typename T> + std::string stringify(const T& e) { + return ::Catch::StringMaker<typename std::remove_cv<typename std::remove_reference<T>::type>::type>::convert(e); + } + + template<typename E> + std::string convertUnknownEnumToString( E e ) { + return ::Catch::Detail::stringify(static_cast<typename std::underlying_type<E>::type>(e)); + } + +#if defined(_MANAGED) + template <typename T> + std::string stringify( T^ e ) { + return ::Catch::StringMaker<T^>::convert(e); + } +#endif + + } // namespace Detail + + // Some predefined specializations + + template<> + struct StringMaker<std::string> { + static std::string convert(const std::string& str); + }; + +#ifdef CATCH_CONFIG_CPP17_STRING_VIEW + template<> + struct StringMaker<std::string_view> { + static std::string convert(std::string_view str); + }; +#endif + + template<> + struct StringMaker<char const *> { + static std::string convert(char const * str); + }; + template<> + struct StringMaker<char *> { + static std::string convert(char * str); + }; + +#ifdef CATCH_CONFIG_WCHAR + template<> + struct StringMaker<std::wstring> { + static std::string convert(const std::wstring& wstr); + }; + +# ifdef CATCH_CONFIG_CPP17_STRING_VIEW + template<> + struct StringMaker<std::wstring_view> { + static std::string convert(std::wstring_view str); + }; +# endif + + template<> + struct StringMaker<wchar_t const *> { + static std::string convert(wchar_t const * str); + }; + template<> + struct StringMaker<wchar_t *> { + static std::string convert(wchar_t * str); + }; +#endif + + // TBD: Should we use `strnlen` to ensure that we don't go out of the buffer, + // while keeping string semantics? + template<int SZ> + struct StringMaker<char[SZ]> { + static std::string convert(char const* str) { + return ::Catch::Detail::stringify(std::string{ str }); + } + }; + template<int SZ> + struct StringMaker<signed char[SZ]> { + static std::string convert(signed char const* str) { + return ::Catch::Detail::stringify(std::string{ reinterpret_cast<char const *>(str) }); + } + }; + template<int SZ> + struct StringMaker<unsigned char[SZ]> { + static std::string convert(unsigned char const* str) { + return ::Catch::Detail::stringify(std::string{ reinterpret_cast<char const *>(str) }); + } + }; + + template<> + struct StringMaker<int> { + static std::string convert(int value); + }; + template<> + struct StringMaker<long> { + static std::string convert(long value); + }; + template<> + struct StringMaker<long long> { + static std::string convert(long long value); + }; + template<> + struct StringMaker<unsigned int> { + static std::string convert(unsigned int value); + }; + template<> + struct StringMaker<unsigned long> { + static std::string convert(unsigned long value); + }; + template<> + struct StringMaker<unsigned long long> { + static std::string convert(unsigned long long value); + }; + + template<> + struct StringMaker<bool> { + static std::string convert(bool b); + }; + + template<> + struct StringMaker<char> { + static std::string convert(char c); + }; + template<> + struct StringMaker<signed char> { + static std::string convert(signed char c); + }; + template<> + struct StringMaker<unsigned char> { + static std::string convert(unsigned char c); + }; + + template<> + struct StringMaker<std::nullptr_t> { + static std::string convert(std::nullptr_t); + }; + + template<> + struct StringMaker<float> { + static std::string convert(float value); + }; + template<> + struct StringMaker<double> { + static std::string convert(double value); + }; + + template <typename T> + struct StringMaker<T*> { + template <typename U> + static std::string convert(U* p) { + if (p) { + return ::Catch::Detail::rawMemoryToString(p); + } else { + return "nullptr"; + } + } + }; + + template <typename R, typename C> + struct StringMaker<R C::*> { + static std::string convert(R C::* p) { + if (p) { + return ::Catch::Detail::rawMemoryToString(p); + } else { + return "nullptr"; + } + } + }; + +#if defined(_MANAGED) + template <typename T> + struct StringMaker<T^> { + static std::string convert( T^ ref ) { + return ::Catch::Detail::clrReferenceToString(ref); + } + }; +#endif + + namespace Detail { + template<typename InputIterator> + std::string rangeToString(InputIterator first, InputIterator last) { + ReusableStringStream rss; + rss << "{ "; + if (first != last) { + rss << ::Catch::Detail::stringify(*first); + for (++first; first != last; ++first) + rss << ", " << ::Catch::Detail::stringify(*first); + } + rss << " }"; + return rss.str(); + } + } + +#ifdef __OBJC__ + template<> + struct StringMaker<NSString*> { + static std::string convert(NSString * nsstring) { + if (!nsstring) + return "nil"; + return std::string("@") + [nsstring UTF8String]; + } + }; + template<> + struct StringMaker<NSObject*> { + static std::string convert(NSObject* nsObject) { + return ::Catch::Detail::stringify([nsObject description]); + } + + }; + namespace Detail { + inline std::string stringify( NSString* nsstring ) { + return StringMaker<NSString*>::convert( nsstring ); + } + + } // namespace Detail +#endif // __OBJC__ + +} // namespace Catch + +////////////////////////////////////////////////////// +// Separate std-lib types stringification, so it can be selectively enabled +// This means that we do not bring in + +#if defined(CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS) +# define CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER +# define CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER +# define CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER +# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +#endif + +// Separate std::pair specialization +#if defined(CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER) +#include <utility> +namespace Catch { + template<typename T1, typename T2> + struct StringMaker<std::pair<T1, T2> > { + static std::string convert(const std::pair<T1, T2>& pair) { + ReusableStringStream rss; + rss << "{ " + << ::Catch::Detail::stringify(pair.first) + << ", " + << ::Catch::Detail::stringify(pair.second) + << " }"; + return rss.str(); + } + }; +} +#endif // CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER + +// Separate std::tuple specialization +#if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER) +#include <tuple> +namespace Catch { + namespace Detail { + template< + typename Tuple, + std::size_t N = 0, + bool = (N < std::tuple_size<Tuple>::value) + > + struct TupleElementPrinter { + static void print(const Tuple& tuple, std::ostream& os) { + os << (N ? ", " : " ") + << ::Catch::Detail::stringify(std::get<N>(tuple)); + TupleElementPrinter<Tuple, N + 1>::print(tuple, os); + } + }; + + template< + typename Tuple, + std::size_t N + > + struct TupleElementPrinter<Tuple, N, false> { + static void print(const Tuple&, std::ostream&) {} + }; + + } + + template<typename ...Types> + struct StringMaker<std::tuple<Types...>> { + static std::string convert(const std::tuple<Types...>& tuple) { + ReusableStringStream rss; + rss << '{'; + Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, rss.get()); + rss << " }"; + return rss.str(); + } + }; +} +#endif // CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER + +#if defined(CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER) && defined(CATCH_CONFIG_CPP17_VARIANT) +#include <variant> +namespace Catch { + template<> + struct StringMaker<std::monostate> { + static std::string convert(const std::monostate&) { + return "{ }"; + } + }; + + template<typename... Elements> + struct StringMaker<std::variant<Elements...>> { + static std::string convert(const std::variant<Elements...>& variant) { + if (variant.valueless_by_exception()) { + return "{valueless variant}"; + } else { + return std::visit( + [](const auto& value) { + return ::Catch::Detail::stringify(value); + }, + variant + ); + } + } + }; +} +#endif // CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER + +namespace Catch { + struct not_this_one {}; // Tag type for detecting which begin/ end are being selected + + // Import begin/ end from std here so they are considered alongside the fallback (...) overloads in this namespace + using std::begin; + using std::end; + + not_this_one begin( ... ); + not_this_one end( ... ); + + template <typename T> + struct is_range { + static const bool value = + !std::is_same<decltype(begin(std::declval<T>())), not_this_one>::value && + !std::is_same<decltype(end(std::declval<T>())), not_this_one>::value; + }; + +#if defined(_MANAGED) // Managed types are never ranges + template <typename T> + struct is_range<T^> { + static const bool value = false; + }; +#endif + + template<typename Range> + std::string rangeToString( Range const& range ) { + return ::Catch::Detail::rangeToString( begin( range ), end( range ) ); + } + + // Handle vector<bool> specially + template<typename Allocator> + std::string rangeToString( std::vector<bool, Allocator> const& v ) { + ReusableStringStream rss; + rss << "{ "; + bool first = true; + for( bool b : v ) { + if( first ) + first = false; + else + rss << ", "; + rss << ::Catch::Detail::stringify( b ); + } + rss << " }"; + return rss.str(); + } + + template<typename R> + struct StringMaker<R, typename std::enable_if<is_range<R>::value && !::Catch::Detail::IsStreamInsertable<R>::value>::type> { + static std::string convert( R const& range ) { + return rangeToString( range ); + } + }; + + template <typename T, int SZ> + struct StringMaker<T[SZ]> { + static std::string convert(T const(&arr)[SZ]) { + return rangeToString(arr); + } + }; + +} // namespace Catch + +// Separate std::chrono::duration specialization +#if defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) +#include <ctime> +#include <ratio> +#include <chrono> + +namespace Catch { + +template <class Ratio> +struct ratio_string { + static std::string symbol(); +}; + +template <class Ratio> +std::string ratio_string<Ratio>::symbol() { + Catch::ReusableStringStream rss; + rss << '[' << Ratio::num << '/' + << Ratio::den << ']'; + return rss.str(); +} +template <> +struct ratio_string<std::atto> { + static std::string symbol(); +}; +template <> +struct ratio_string<std::femto> { + static std::string symbol(); +}; +template <> +struct ratio_string<std::pico> { + static std::string symbol(); +}; +template <> +struct ratio_string<std::nano> { + static std::string symbol(); +}; +template <> +struct ratio_string<std::micro> { + static std::string symbol(); +}; +template <> +struct ratio_string<std::milli> { + static std::string symbol(); +}; + + //////////// + // std::chrono::duration specializations + template<typename Value, typename Ratio> + struct StringMaker<std::chrono::duration<Value, Ratio>> { + static std::string convert(std::chrono::duration<Value, Ratio> const& duration) { + ReusableStringStream rss; + rss << duration.count() << ' ' << ratio_string<Ratio>::symbol() << 's'; + return rss.str(); + } + }; + template<typename Value> + struct StringMaker<std::chrono::duration<Value, std::ratio<1>>> { + static std::string convert(std::chrono::duration<Value, std::ratio<1>> const& duration) { + ReusableStringStream rss; + rss << duration.count() << " s"; + return rss.str(); + } + }; + template<typename Value> + struct StringMaker<std::chrono::duration<Value, std::ratio<60>>> { + static std::string convert(std::chrono::duration<Value, std::ratio<60>> const& duration) { + ReusableStringStream rss; + rss << duration.count() << " m"; + return rss.str(); + } + }; + template<typename Value> + struct StringMaker<std::chrono::duration<Value, std::ratio<3600>>> { + static std::string convert(std::chrono::duration<Value, std::ratio<3600>> const& duration) { + ReusableStringStream rss; + rss << duration.count() << " h"; + return rss.str(); + } + }; + + //////////// + // std::chrono::time_point specialization + // Generic time_point cannot be specialized, only std::chrono::time_point<system_clock> + template<typename Clock, typename Duration> + struct StringMaker<std::chrono::time_point<Clock, Duration>> { + static std::string convert(std::chrono::time_point<Clock, Duration> const& time_point) { + return ::Catch::Detail::stringify(time_point.time_since_epoch()) + " since epoch"; + } + }; + // std::chrono::time_point<system_clock> specialization + template<typename Duration> + struct StringMaker<std::chrono::time_point<std::chrono::system_clock, Duration>> { + static std::string convert(std::chrono::time_point<std::chrono::system_clock, Duration> const& time_point) { + auto converted = std::chrono::system_clock::to_time_t(time_point); + +#ifdef _MSC_VER + std::tm timeInfo = {}; + gmtime_s(&timeInfo, &converted); +#else + std::tm* timeInfo = std::gmtime(&converted); +#endif + + auto const timeStampSize = sizeof("2017-01-16T17:06:45Z"); + char timeStamp[timeStampSize]; + const char * const fmt = "%Y-%m-%dT%H:%M:%SZ"; + +#ifdef _MSC_VER + std::strftime(timeStamp, timeStampSize, fmt, &timeInfo); +#else + std::strftime(timeStamp, timeStampSize, fmt, timeInfo); +#endif + return std::string(timeStamp); + } + }; +} +#endif // CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +// end catch_tostring.h +#include <iosfwd> + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable:4389) // '==' : signed/unsigned mismatch +#pragma warning(disable:4018) // more "signed/unsigned mismatch" +#pragma warning(disable:4312) // Converting int to T* using reinterpret_cast (issue on x64 platform) +#pragma warning(disable:4180) // qualifier applied to function type has no meaning +#endif + +namespace Catch { + + struct ITransientExpression { + auto isBinaryExpression() const -> bool { return m_isBinaryExpression; } + auto getResult() const -> bool { return m_result; } + virtual void streamReconstructedExpression( std::ostream &os ) const = 0; + + ITransientExpression( bool isBinaryExpression, bool result ) + : m_isBinaryExpression( isBinaryExpression ), + m_result( result ) + {} + + // We don't actually need a virtual destructor, but many static analysers + // complain if it's not here :-( + virtual ~ITransientExpression(); + + bool m_isBinaryExpression; + bool m_result; + + }; + + void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ); + + template<typename LhsT, typename RhsT> + class BinaryExpr : public ITransientExpression { + LhsT m_lhs; + StringRef m_op; + RhsT m_rhs; + + void streamReconstructedExpression( std::ostream &os ) const override { + formatReconstructedExpression + ( os, Catch::Detail::stringify( m_lhs ), m_op, Catch::Detail::stringify( m_rhs ) ); + } + + public: + BinaryExpr( bool comparisonResult, LhsT lhs, StringRef op, RhsT rhs ) + : ITransientExpression{ true, comparisonResult }, + m_lhs( lhs ), + m_op( op ), + m_rhs( rhs ) + {} + }; + + template<typename LhsT> + class UnaryExpr : public ITransientExpression { + LhsT m_lhs; + + void streamReconstructedExpression( std::ostream &os ) const override { + os << Catch::Detail::stringify( m_lhs ); + } + + public: + explicit UnaryExpr( LhsT lhs ) + : ITransientExpression{ false, lhs ? true : false }, + m_lhs( lhs ) + {} + }; + + // Specialised comparison functions to handle equality comparisons between ints and pointers (NULL deduces as an int) + template<typename LhsT, typename RhsT> + auto compareEqual( LhsT const& lhs, RhsT const& rhs ) -> bool { return static_cast<bool>(lhs == rhs); } + template<typename T> + auto compareEqual( T* const& lhs, int rhs ) -> bool { return lhs == reinterpret_cast<void const*>( rhs ); } + template<typename T> + auto compareEqual( T* const& lhs, long rhs ) -> bool { return lhs == reinterpret_cast<void const*>( rhs ); } + template<typename T> + auto compareEqual( int lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) == rhs; } + template<typename T> + auto compareEqual( long lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) == rhs; } + + template<typename LhsT, typename RhsT> + auto compareNotEqual( LhsT const& lhs, RhsT&& rhs ) -> bool { return static_cast<bool>(lhs != rhs); } + template<typename T> + auto compareNotEqual( T* const& lhs, int rhs ) -> bool { return lhs != reinterpret_cast<void const*>( rhs ); } + template<typename T> + auto compareNotEqual( T* const& lhs, long rhs ) -> bool { return lhs != reinterpret_cast<void const*>( rhs ); } + template<typename T> + auto compareNotEqual( int lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) != rhs; } + template<typename T> + auto compareNotEqual( long lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) != rhs; } + + template<typename LhsT> + class ExprLhs { + LhsT m_lhs; + public: + explicit ExprLhs( LhsT lhs ) : m_lhs( lhs ) {} + + template<typename RhsT> + auto operator == ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { + return { compareEqual( m_lhs, rhs ), m_lhs, "==", rhs }; + } + auto operator == ( bool rhs ) -> BinaryExpr<LhsT, bool> const { + return { m_lhs == rhs, m_lhs, "==", rhs }; + } + + template<typename RhsT> + auto operator != ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { + return { compareNotEqual( m_lhs, rhs ), m_lhs, "!=", rhs }; + } + auto operator != ( bool rhs ) -> BinaryExpr<LhsT, bool> const { + return { m_lhs != rhs, m_lhs, "!=", rhs }; + } + + template<typename RhsT> + auto operator > ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { + return { static_cast<bool>(m_lhs > rhs), m_lhs, ">", rhs }; + } + template<typename RhsT> + auto operator < ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { + return { static_cast<bool>(m_lhs < rhs), m_lhs, "<", rhs }; + } + template<typename RhsT> + auto operator >= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { + return { static_cast<bool>(m_lhs >= rhs), m_lhs, ">=", rhs }; + } + template<typename RhsT> + auto operator <= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { + return { static_cast<bool>(m_lhs <= rhs), m_lhs, "<=", rhs }; + } + + auto makeUnaryExpr() const -> UnaryExpr<LhsT> { + return UnaryExpr<LhsT>{ m_lhs }; + } + }; + + void handleExpression( ITransientExpression const& expr ); + + template<typename T> + void handleExpression( ExprLhs<T> const& expr ) { + handleExpression( expr.makeUnaryExpr() ); + } + + struct Decomposer { + template<typename T> + auto operator <= ( T const& lhs ) -> ExprLhs<T const&> { + return ExprLhs<T const&>{ lhs }; + } + + auto operator <=( bool value ) -> ExprLhs<bool> { + return ExprLhs<bool>{ value }; + } + }; + +} // end namespace Catch + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +// end catch_decomposer.h +// start catch_interfaces_capture.h + +#include <string> + +namespace Catch { + + class AssertionResult; + struct AssertionInfo; + struct SectionInfo; + struct SectionEndInfo; + struct MessageInfo; + struct Counts; + struct BenchmarkInfo; + struct BenchmarkStats; + struct AssertionReaction; + struct SourceLineInfo; + + struct ITransientExpression; + struct IGeneratorTracker; + + struct IResultCapture { + + virtual ~IResultCapture(); + + virtual bool sectionStarted( SectionInfo const& sectionInfo, + Counts& assertions ) = 0; + virtual void sectionEnded( SectionEndInfo const& endInfo ) = 0; + virtual void sectionEndedEarly( SectionEndInfo const& endInfo ) = 0; + + virtual auto acquireGeneratorTracker( SourceLineInfo const& lineInfo ) -> IGeneratorTracker& = 0; + + virtual void benchmarkStarting( BenchmarkInfo const& info ) = 0; + virtual void benchmarkEnded( BenchmarkStats const& stats ) = 0; + + virtual void pushScopedMessage( MessageInfo const& message ) = 0; + virtual void popScopedMessage( MessageInfo const& message ) = 0; + + virtual void handleFatalErrorCondition( StringRef message ) = 0; + + virtual void handleExpr + ( AssertionInfo const& info, + ITransientExpression const& expr, + AssertionReaction& reaction ) = 0; + virtual void handleMessage + ( AssertionInfo const& info, + ResultWas::OfType resultType, + StringRef const& message, + AssertionReaction& reaction ) = 0; + virtual void handleUnexpectedExceptionNotThrown + ( AssertionInfo const& info, + AssertionReaction& reaction ) = 0; + virtual void handleUnexpectedInflightException + ( AssertionInfo const& info, + std::string const& message, + AssertionReaction& reaction ) = 0; + virtual void handleIncomplete + ( AssertionInfo const& info ) = 0; + virtual void handleNonExpr + ( AssertionInfo const &info, + ResultWas::OfType resultType, + AssertionReaction &reaction ) = 0; + + virtual bool lastAssertionPassed() = 0; + virtual void assertionPassed() = 0; + + // Deprecated, do not use: + virtual std::string getCurrentTestName() const = 0; + virtual const AssertionResult* getLastResult() const = 0; + virtual void exceptionEarlyReported() = 0; + }; + + IResultCapture& getResultCapture(); +} + +// end catch_interfaces_capture.h +namespace Catch { + + struct TestFailureException{}; + struct AssertionResultData; + struct IResultCapture; + class RunContext; + + class LazyExpression { + friend class AssertionHandler; + friend struct AssertionStats; + friend class RunContext; + + ITransientExpression const* m_transientExpression = nullptr; + bool m_isNegated; + public: + LazyExpression( bool isNegated ); + LazyExpression( LazyExpression const& other ); + LazyExpression& operator = ( LazyExpression const& ) = delete; + + explicit operator bool() const; + + friend auto operator << ( std::ostream& os, LazyExpression const& lazyExpr ) -> std::ostream&; + }; + + struct AssertionReaction { + bool shouldDebugBreak = false; + bool shouldThrow = false; + }; + + class AssertionHandler { + AssertionInfo m_assertionInfo; + AssertionReaction m_reaction; + bool m_completed = false; + IResultCapture& m_resultCapture; + + public: + AssertionHandler + ( StringRef const& macroName, + SourceLineInfo const& lineInfo, + StringRef capturedExpression, + ResultDisposition::Flags resultDisposition ); + ~AssertionHandler() { + if ( !m_completed ) { + m_resultCapture.handleIncomplete( m_assertionInfo ); + } + } + + template<typename T> + void handleExpr( ExprLhs<T> const& expr ) { + handleExpr( expr.makeUnaryExpr() ); + } + void handleExpr( ITransientExpression const& expr ); + + void handleMessage(ResultWas::OfType resultType, StringRef const& message); + + void handleExceptionThrownAsExpected(); + void handleUnexpectedExceptionNotThrown(); + void handleExceptionNotThrownAsExpected(); + void handleThrowingCallSkipped(); + void handleUnexpectedInflightException(); + + void complete(); + void setCompleted(); + + // query + auto allowThrows() const -> bool; + }; + + void handleExceptionMatchExpr( AssertionHandler& handler, std::string const& str, StringRef const& matcherString ); + +} // namespace Catch + +// end catch_assertionhandler.h +// start catch_message.h + +#include <string> +#include <vector> + +namespace Catch { + + struct MessageInfo { + MessageInfo( StringRef const& _macroName, + SourceLineInfo const& _lineInfo, + ResultWas::OfType _type ); + + StringRef macroName; + std::string message; + SourceLineInfo lineInfo; + ResultWas::OfType type; + unsigned int sequence; + + bool operator == ( MessageInfo const& other ) const; + bool operator < ( MessageInfo const& other ) const; + private: + static unsigned int globalCount; + }; + + struct MessageStream { + + template<typename T> + MessageStream& operator << ( T const& value ) { + m_stream << value; + return *this; + } + + ReusableStringStream m_stream; + }; + + struct MessageBuilder : MessageStream { + MessageBuilder( StringRef const& macroName, + SourceLineInfo const& lineInfo, + ResultWas::OfType type ); + + template<typename T> + MessageBuilder& operator << ( T const& value ) { + m_stream << value; + return *this; + } + + MessageInfo m_info; + }; + + class ScopedMessage { + public: + explicit ScopedMessage( MessageBuilder const& builder ); + ~ScopedMessage(); + + MessageInfo m_info; + }; + + class Capturer { + std::vector<MessageInfo> m_messages; + IResultCapture& m_resultCapture = getResultCapture(); + size_t m_captured = 0; + public: + Capturer( StringRef macroName, SourceLineInfo const& lineInfo, ResultWas::OfType resultType, StringRef names ); + ~Capturer(); + + void captureValue( size_t index, std::string const& value ); + + template<typename T> + void captureValues( size_t index, T const& value ) { + captureValue( index, Catch::Detail::stringify( value ) ); + } + + template<typename T, typename... Ts> + void captureValues( size_t index, T const& value, Ts const&... values ) { + captureValue( index, Catch::Detail::stringify(value) ); + captureValues( index+1, values... ); + } + }; + +} // end namespace Catch + +// end catch_message.h +#if !defined(CATCH_CONFIG_DISABLE) + +#if !defined(CATCH_CONFIG_DISABLE_STRINGIFICATION) + #define CATCH_INTERNAL_STRINGIFY(...) #__VA_ARGS__ +#else + #define CATCH_INTERNAL_STRINGIFY(...) "Disabled by CATCH_CONFIG_DISABLE_STRINGIFICATION" +#endif + +#if defined(CATCH_CONFIG_FAST_COMPILE) || defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) + +/////////////////////////////////////////////////////////////////////////////// +// Another way to speed-up compilation is to omit local try-catch for REQUIRE* +// macros. +#define INTERNAL_CATCH_TRY +#define INTERNAL_CATCH_CATCH( capturer ) + +#else // CATCH_CONFIG_FAST_COMPILE + +#define INTERNAL_CATCH_TRY try +#define INTERNAL_CATCH_CATCH( handler ) catch(...) { handler.handleUnexpectedInflightException(); } + +#endif + +#define INTERNAL_CATCH_REACT( handler ) handler.complete(); + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_TEST( macroName, resultDisposition, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \ + INTERNAL_CATCH_TRY { \ + CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ + catchAssertionHandler.handleExpr( Catch::Decomposer() <= __VA_ARGS__ ); \ + CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \ + } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( (void)0, false && static_cast<bool>( !!(__VA_ARGS__) ) ) // the expression here is never evaluated at runtime but it forces the compiler to give it a look + // The double negation silences MSVC's C4800 warning, the static_cast forces short-circuit evaluation if the type has overloaded &&. + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \ + INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \ + if( Catch::getResultCapture().lastAssertionPassed() ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_ELSE( macroName, resultDisposition, ... ) \ + INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \ + if( !Catch::getResultCapture().lastAssertionPassed() ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \ + try { \ + static_cast<void>(__VA_ARGS__); \ + catchAssertionHandler.handleExceptionNotThrownAsExpected(); \ + } \ + catch( ... ) { \ + catchAssertionHandler.handleUnexpectedInflightException(); \ + } \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_THROWS( macroName, resultDisposition, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition); \ + if( catchAssertionHandler.allowThrows() ) \ + try { \ + static_cast<void>(__VA_ARGS__); \ + catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ + } \ + catch( ... ) { \ + catchAssertionHandler.handleExceptionThrownAsExpected(); \ + } \ + else \ + catchAssertionHandler.handleThrowingCallSkipped(); \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(expr) ", " CATCH_INTERNAL_STRINGIFY(exceptionType), resultDisposition ); \ + if( catchAssertionHandler.allowThrows() ) \ + try { \ + static_cast<void>(expr); \ + catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ + } \ + catch( exceptionType const& ) { \ + catchAssertionHandler.handleExceptionThrownAsExpected(); \ + } \ + catch( ... ) { \ + catchAssertionHandler.handleUnexpectedInflightException(); \ + } \ + else \ + catchAssertionHandler.handleThrowingCallSkipped(); \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_MSG( macroName, messageType, resultDisposition, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), resultDisposition ); \ + catchAssertionHandler.handleMessage( messageType, ( Catch::MessageStream() << __VA_ARGS__ + ::Catch::StreamEndStop() ).m_stream.str() ); \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_CAPTURE( varName, macroName, ... ) \ + auto varName = Catch::Capturer( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info, #__VA_ARGS__ ); \ + varName.captureValues( 0, __VA_ARGS__ ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_INFO( macroName, log ) \ + Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage )( Catch::MessageBuilder( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log ); + +/////////////////////////////////////////////////////////////////////////////// +// Although this is matcher-based, it can be used with just a string +#define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ + if( catchAssertionHandler.allowThrows() ) \ + try { \ + static_cast<void>(__VA_ARGS__); \ + catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ + } \ + catch( ... ) { \ + Catch::handleExceptionMatchExpr( catchAssertionHandler, matcher, #matcher##_catch_sr ); \ + } \ + else \ + catchAssertionHandler.handleThrowingCallSkipped(); \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +#endif // CATCH_CONFIG_DISABLE + +// end catch_capture.hpp +// start catch_section.h + +// start catch_section_info.h + +// start catch_totals.h + +#include <cstddef> + +namespace Catch { + + struct Counts { + Counts operator - ( Counts const& other ) const; + Counts& operator += ( Counts const& other ); + + std::size_t total() const; + bool allPassed() const; + bool allOk() const; + + std::size_t passed = 0; + std::size_t failed = 0; + std::size_t failedButOk = 0; + }; + + struct Totals { + + Totals operator - ( Totals const& other ) const; + Totals& operator += ( Totals const& other ); + + Totals delta( Totals const& prevTotals ) const; + + int error = 0; + Counts assertions; + Counts testCases; + }; +} + +// end catch_totals.h +#include <string> + +namespace Catch { + + struct SectionInfo { + SectionInfo + ( SourceLineInfo const& _lineInfo, + std::string const& _name ); + + // Deprecated + SectionInfo + ( SourceLineInfo const& _lineInfo, + std::string const& _name, + std::string const& ) : SectionInfo( _lineInfo, _name ) {} + + std::string name; + std::string description; // !Deprecated: this will always be empty + SourceLineInfo lineInfo; + }; + + struct SectionEndInfo { + SectionInfo sectionInfo; + Counts prevAssertions; + double durationInSeconds; + }; + +} // end namespace Catch + +// end catch_section_info.h +// start catch_timer.h + +#include <cstdint> + +namespace Catch { + + auto getCurrentNanosecondsSinceEpoch() -> uint64_t; + auto getEstimatedClockResolution() -> uint64_t; + + class Timer { + uint64_t m_nanoseconds = 0; + public: + void start(); + auto getElapsedNanoseconds() const -> uint64_t; + auto getElapsedMicroseconds() const -> uint64_t; + auto getElapsedMilliseconds() const -> unsigned int; + auto getElapsedSeconds() const -> double; + }; + +} // namespace Catch + +// end catch_timer.h +#include <string> + +namespace Catch { + + class Section : NonCopyable { + public: + Section( SectionInfo const& info ); + ~Section(); + + // This indicates whether the section should be executed or not + explicit operator bool() const; + + private: + SectionInfo m_info; + + std::string m_name; + Counts m_assertions; + bool m_sectionIncluded; + Timer m_timer; + }; + +} // end namespace Catch + +#define INTERNAL_CATCH_SECTION( ... ) \ + CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS \ + if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( CATCH_INTERNAL_LINEINFO, __VA_ARGS__ ) ) \ + CATCH_INTERNAL_UNSUPPRESS_UNUSED_WARNINGS + +#define INTERNAL_CATCH_DYNAMIC_SECTION( ... ) \ + CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS \ + if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( CATCH_INTERNAL_LINEINFO, (Catch::ReusableStringStream() << __VA_ARGS__).str() ) ) \ + CATCH_INTERNAL_UNSUPPRESS_UNUSED_WARNINGS + +// end catch_section.h +// start catch_benchmark.h + +#include <cstdint> +#include <string> + +namespace Catch { + + class BenchmarkLooper { + + std::string m_name; + std::size_t m_count = 0; + std::size_t m_iterationsToRun = 1; + uint64_t m_resolution; + Timer m_timer; + + static auto getResolution() -> uint64_t; + public: + // Keep most of this inline as it's on the code path that is being timed + BenchmarkLooper( StringRef name ) + : m_name( name ), + m_resolution( getResolution() ) + { + reportStart(); + m_timer.start(); + } + + explicit operator bool() { + if( m_count < m_iterationsToRun ) + return true; + return needsMoreIterations(); + } + + void increment() { + ++m_count; + } + + void reportStart(); + auto needsMoreIterations() -> bool; + }; + +} // end namespace Catch + +#define BENCHMARK( name ) \ + for( Catch::BenchmarkLooper looper( name ); looper; looper.increment() ) + +// end catch_benchmark.h +// start catch_interfaces_exception.h + +// start catch_interfaces_registry_hub.h + +#include <string> +#include <memory> + +namespace Catch { + + class TestCase; + struct ITestCaseRegistry; + struct IExceptionTranslatorRegistry; + struct IExceptionTranslator; + struct IReporterRegistry; + struct IReporterFactory; + struct ITagAliasRegistry; + class StartupExceptionRegistry; + + using IReporterFactoryPtr = std::shared_ptr<IReporterFactory>; + + struct IRegistryHub { + virtual ~IRegistryHub(); + + virtual IReporterRegistry const& getReporterRegistry() const = 0; + virtual ITestCaseRegistry const& getTestCaseRegistry() const = 0; + virtual ITagAliasRegistry const& getTagAliasRegistry() const = 0; + + virtual IExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const = 0; + + virtual StartupExceptionRegistry const& getStartupExceptionRegistry() const = 0; + }; + + struct IMutableRegistryHub { + virtual ~IMutableRegistryHub(); + virtual void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) = 0; + virtual void registerListener( IReporterFactoryPtr const& factory ) = 0; + virtual void registerTest( TestCase const& testInfo ) = 0; + virtual void registerTranslator( const IExceptionTranslator* translator ) = 0; + virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0; + virtual void registerStartupException() noexcept = 0; + }; + + IRegistryHub const& getRegistryHub(); + IMutableRegistryHub& getMutableRegistryHub(); + void cleanUp(); + std::string translateActiveException(); + +} + +// end catch_interfaces_registry_hub.h +#if defined(CATCH_CONFIG_DISABLE) + #define INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( translatorName, signature) \ + static std::string translatorName( signature ) +#endif + +#include <exception> +#include <string> +#include <vector> + +namespace Catch { + using exceptionTranslateFunction = std::string(*)(); + + struct IExceptionTranslator; + using ExceptionTranslators = std::vector<std::unique_ptr<IExceptionTranslator const>>; + + struct IExceptionTranslator { + virtual ~IExceptionTranslator(); + virtual std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const = 0; + }; + + struct IExceptionTranslatorRegistry { + virtual ~IExceptionTranslatorRegistry(); + + virtual std::string translateActiveException() const = 0; + }; + + class ExceptionTranslatorRegistrar { + template<typename T> + class ExceptionTranslator : public IExceptionTranslator { + public: + + ExceptionTranslator( std::string(*translateFunction)( T& ) ) + : m_translateFunction( translateFunction ) + {} + + std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const override { + try { + if( it == itEnd ) + std::rethrow_exception(std::current_exception()); + else + return (*it)->translate( it+1, itEnd ); + } + catch( T& ex ) { + return m_translateFunction( ex ); + } + } + + protected: + std::string(*m_translateFunction)( T& ); + }; + + public: + template<typename T> + ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) { + getMutableRegistryHub().registerTranslator + ( new ExceptionTranslator<T>( translateFunction ) ); + } + }; +} + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \ + static std::string translatorName( signature ); \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ + static std::string translatorName( signature ) + +#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) + +// end catch_interfaces_exception.h +// start catch_approx.h + +#include <type_traits> + +namespace Catch { +namespace Detail { + + class Approx { + private: + bool equalityComparisonImpl(double other) const; + // Validates the new margin (margin >= 0) + // out-of-line to avoid including stdexcept in the header + void setMargin(double margin); + // Validates the new epsilon (0 < epsilon < 1) + // out-of-line to avoid including stdexcept in the header + void setEpsilon(double epsilon); + + public: + explicit Approx ( double value ); + + static Approx custom(); + + Approx operator-() const; + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + Approx operator()( T const& value ) { + Approx approx( static_cast<double>(value) ); + approx.m_epsilon = m_epsilon; + approx.m_margin = m_margin; + approx.m_scale = m_scale; + return approx; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + explicit Approx( T const& value ): Approx(static_cast<double>(value)) + {} + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator == ( const T& lhs, Approx const& rhs ) { + auto lhs_v = static_cast<double>(lhs); + return rhs.equalityComparisonImpl(lhs_v); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator == ( Approx const& lhs, const T& rhs ) { + return operator==( rhs, lhs ); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator != ( T const& lhs, Approx const& rhs ) { + return !operator==( lhs, rhs ); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator != ( Approx const& lhs, T const& rhs ) { + return !operator==( rhs, lhs ); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator <= ( T const& lhs, Approx const& rhs ) { + return static_cast<double>(lhs) < rhs.m_value || lhs == rhs; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator <= ( Approx const& lhs, T const& rhs ) { + return lhs.m_value < static_cast<double>(rhs) || lhs == rhs; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator >= ( T const& lhs, Approx const& rhs ) { + return static_cast<double>(lhs) > rhs.m_value || lhs == rhs; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator >= ( Approx const& lhs, T const& rhs ) { + return lhs.m_value > static_cast<double>(rhs) || lhs == rhs; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + Approx& epsilon( T const& newEpsilon ) { + double epsilonAsDouble = static_cast<double>(newEpsilon); + setEpsilon(epsilonAsDouble); + return *this; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + Approx& margin( T const& newMargin ) { + double marginAsDouble = static_cast<double>(newMargin); + setMargin(marginAsDouble); + return *this; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + Approx& scale( T const& newScale ) { + m_scale = static_cast<double>(newScale); + return *this; + } + + std::string toString() const; + + private: + double m_epsilon; + double m_margin; + double m_scale; + double m_value; + }; +} // end namespace Detail + +namespace literals { + Detail::Approx operator "" _a(long double val); + Detail::Approx operator "" _a(unsigned long long val); +} // end namespace literals + +template<> +struct StringMaker<Catch::Detail::Approx> { + static std::string convert(Catch::Detail::Approx const& value); +}; + +} // end namespace Catch + +// end catch_approx.h +// start catch_string_manip.h + +#include <string> +#include <iosfwd> + +namespace Catch { + + bool startsWith( std::string const& s, std::string const& prefix ); + bool startsWith( std::string const& s, char prefix ); + bool endsWith( std::string const& s, std::string const& suffix ); + bool endsWith( std::string const& s, char suffix ); + bool contains( std::string const& s, std::string const& infix ); + void toLowerInPlace( std::string& s ); + std::string toLower( std::string const& s ); + std::string trim( std::string const& str ); + bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ); + + struct pluralise { + pluralise( std::size_t count, std::string const& label ); + + friend std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ); + + std::size_t m_count; + std::string m_label; + }; +} + +// end catch_string_manip.h +#ifndef CATCH_CONFIG_DISABLE_MATCHERS +// start catch_capture_matchers.h + +// start catch_matchers.h + +#include <string> +#include <vector> + +namespace Catch { +namespace Matchers { + namespace Impl { + + template<typename ArgT> struct MatchAllOf; + template<typename ArgT> struct MatchAnyOf; + template<typename ArgT> struct MatchNotOf; + + class MatcherUntypedBase { + public: + MatcherUntypedBase() = default; + MatcherUntypedBase ( MatcherUntypedBase const& ) = default; + MatcherUntypedBase& operator = ( MatcherUntypedBase const& ) = delete; + std::string toString() const; + + protected: + virtual ~MatcherUntypedBase(); + virtual std::string describe() const = 0; + mutable std::string m_cachedToString; + }; + +#ifdef __clang__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wnon-virtual-dtor" +#endif + + template<typename ObjectT> + struct MatcherMethod { + virtual bool match( ObjectT const& arg ) const = 0; + }; + +#ifdef __clang__ +# pragma clang diagnostic pop +#endif + + template<typename T> + struct MatcherBase : MatcherUntypedBase, MatcherMethod<T> { + + MatchAllOf<T> operator && ( MatcherBase const& other ) const; + MatchAnyOf<T> operator || ( MatcherBase const& other ) const; + MatchNotOf<T> operator ! () const; + }; + + template<typename ArgT> + struct MatchAllOf : MatcherBase<ArgT> { + bool match( ArgT const& arg ) const override { + for( auto matcher : m_matchers ) { + if (!matcher->match(arg)) + return false; + } + return true; + } + std::string describe() const override { + std::string description; + description.reserve( 4 + m_matchers.size()*32 ); + description += "( "; + bool first = true; + for( auto matcher : m_matchers ) { + if( first ) + first = false; + else + description += " and "; + description += matcher->toString(); + } + description += " )"; + return description; + } + + MatchAllOf<ArgT>& operator && ( MatcherBase<ArgT> const& other ) { + m_matchers.push_back( &other ); + return *this; + } + + std::vector<MatcherBase<ArgT> const*> m_matchers; + }; + template<typename ArgT> + struct MatchAnyOf : MatcherBase<ArgT> { + + bool match( ArgT const& arg ) const override { + for( auto matcher : m_matchers ) { + if (matcher->match(arg)) + return true; + } + return false; + } + std::string describe() const override { + std::string description; + description.reserve( 4 + m_matchers.size()*32 ); + description += "( "; + bool first = true; + for( auto matcher : m_matchers ) { + if( first ) + first = false; + else + description += " or "; + description += matcher->toString(); + } + description += " )"; + return description; + } + + MatchAnyOf<ArgT>& operator || ( MatcherBase<ArgT> const& other ) { + m_matchers.push_back( &other ); + return *this; + } + + std::vector<MatcherBase<ArgT> const*> m_matchers; + }; + + template<typename ArgT> + struct MatchNotOf : MatcherBase<ArgT> { + + MatchNotOf( MatcherBase<ArgT> const& underlyingMatcher ) : m_underlyingMatcher( underlyingMatcher ) {} + + bool match( ArgT const& arg ) const override { + return !m_underlyingMatcher.match( arg ); + } + + std::string describe() const override { + return "not " + m_underlyingMatcher.toString(); + } + MatcherBase<ArgT> const& m_underlyingMatcher; + }; + + template<typename T> + MatchAllOf<T> MatcherBase<T>::operator && ( MatcherBase const& other ) const { + return MatchAllOf<T>() && *this && other; + } + template<typename T> + MatchAnyOf<T> MatcherBase<T>::operator || ( MatcherBase const& other ) const { + return MatchAnyOf<T>() || *this || other; + } + template<typename T> + MatchNotOf<T> MatcherBase<T>::operator ! () const { + return MatchNotOf<T>( *this ); + } + + } // namespace Impl + +} // namespace Matchers + +using namespace Matchers; +using Matchers::Impl::MatcherBase; + +} // namespace Catch + +// end catch_matchers.h +// start catch_matchers_floating.h + +#include <type_traits> +#include <cmath> + +namespace Catch { +namespace Matchers { + + namespace Floating { + + enum class FloatingPointKind : uint8_t; + + struct WithinAbsMatcher : MatcherBase<double> { + WithinAbsMatcher(double target, double margin); + bool match(double const& matchee) const override; + std::string describe() const override; + private: + double m_target; + double m_margin; + }; + + struct WithinUlpsMatcher : MatcherBase<double> { + WithinUlpsMatcher(double target, int ulps, FloatingPointKind baseType); + bool match(double const& matchee) const override; + std::string describe() const override; + private: + double m_target; + int m_ulps; + FloatingPointKind m_type; + }; + + } // namespace Floating + + // The following functions create the actual matcher objects. + // This allows the types to be inferred + Floating::WithinUlpsMatcher WithinULP(double target, int maxUlpDiff); + Floating::WithinUlpsMatcher WithinULP(float target, int maxUlpDiff); + Floating::WithinAbsMatcher WithinAbs(double target, double margin); + +} // namespace Matchers +} // namespace Catch + +// end catch_matchers_floating.h +// start catch_matchers_generic.hpp + +#include <functional> +#include <string> + +namespace Catch { +namespace Matchers { +namespace Generic { + +namespace Detail { + std::string finalizeDescription(const std::string& desc); +} + +template <typename T> +class PredicateMatcher : public MatcherBase<T> { + std::function<bool(T const&)> m_predicate; + std::string m_description; +public: + + PredicateMatcher(std::function<bool(T const&)> const& elem, std::string const& descr) + :m_predicate(std::move(elem)), + m_description(Detail::finalizeDescription(descr)) + {} + + bool match( T const& item ) const override { + return m_predicate(item); + } + + std::string describe() const override { + return m_description; + } +}; + +} // namespace Generic + + // The following functions create the actual matcher objects. + // The user has to explicitly specify type to the function, because + // infering std::function<bool(T const&)> is hard (but possible) and + // requires a lot of TMP. + template<typename T> + Generic::PredicateMatcher<T> Predicate(std::function<bool(T const&)> const& predicate, std::string const& description = "") { + return Generic::PredicateMatcher<T>(predicate, description); + } + +} // namespace Matchers +} // namespace Catch + +// end catch_matchers_generic.hpp +// start catch_matchers_string.h + +#include <string> + +namespace Catch { +namespace Matchers { + + namespace StdString { + + struct CasedString + { + CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ); + std::string adjustString( std::string const& str ) const; + std::string caseSensitivitySuffix() const; + + CaseSensitive::Choice m_caseSensitivity; + std::string m_str; + }; + + struct StringMatcherBase : MatcherBase<std::string> { + StringMatcherBase( std::string const& operation, CasedString const& comparator ); + std::string describe() const override; + + CasedString m_comparator; + std::string m_operation; + }; + + struct EqualsMatcher : StringMatcherBase { + EqualsMatcher( CasedString const& comparator ); + bool match( std::string const& source ) const override; + }; + struct ContainsMatcher : StringMatcherBase { + ContainsMatcher( CasedString const& comparator ); + bool match( std::string const& source ) const override; + }; + struct StartsWithMatcher : StringMatcherBase { + StartsWithMatcher( CasedString const& comparator ); + bool match( std::string const& source ) const override; + }; + struct EndsWithMatcher : StringMatcherBase { + EndsWithMatcher( CasedString const& comparator ); + bool match( std::string const& source ) const override; + }; + + struct RegexMatcher : MatcherBase<std::string> { + RegexMatcher( std::string regex, CaseSensitive::Choice caseSensitivity ); + bool match( std::string const& matchee ) const override; + std::string describe() const override; + + private: + std::string m_regex; + CaseSensitive::Choice m_caseSensitivity; + }; + + } // namespace StdString + + // The following functions create the actual matcher objects. + // This allows the types to be inferred + + StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::RegexMatcher Matches( std::string const& regex, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + +} // namespace Matchers +} // namespace Catch + +// end catch_matchers_string.h +// start catch_matchers_vector.h + +#include <algorithm> + +namespace Catch { +namespace Matchers { + + namespace Vector { + namespace Detail { + template <typename InputIterator, typename T> + size_t count(InputIterator first, InputIterator last, T const& item) { + size_t cnt = 0; + for (; first != last; ++first) { + if (*first == item) { + ++cnt; + } + } + return cnt; + } + template <typename InputIterator, typename T> + bool contains(InputIterator first, InputIterator last, T const& item) { + for (; first != last; ++first) { + if (*first == item) { + return true; + } + } + return false; + } + } + + template<typename T> + struct ContainsElementMatcher : MatcherBase<std::vector<T>> { + + ContainsElementMatcher(T const &comparator) : m_comparator( comparator) {} + + bool match(std::vector<T> const &v) const override { + for (auto const& el : v) { + if (el == m_comparator) { + return true; + } + } + return false; + } + + std::string describe() const override { + return "Contains: " + ::Catch::Detail::stringify( m_comparator ); + } + + T const& m_comparator; + }; + + template<typename T> + struct ContainsMatcher : MatcherBase<std::vector<T>> { + + ContainsMatcher(std::vector<T> const &comparator) : m_comparator( comparator ) {} + + bool match(std::vector<T> const &v) const override { + // !TBD: see note in EqualsMatcher + if (m_comparator.size() > v.size()) + return false; + for (auto const& comparator : m_comparator) { + auto present = false; + for (const auto& el : v) { + if (el == comparator) { + present = true; + break; + } + } + if (!present) { + return false; + } + } + return true; + } + std::string describe() const override { + return "Contains: " + ::Catch::Detail::stringify( m_comparator ); + } + + std::vector<T> const& m_comparator; + }; + + template<typename T> + struct EqualsMatcher : MatcherBase<std::vector<T>> { + + EqualsMatcher(std::vector<T> const &comparator) : m_comparator( comparator ) {} + + bool match(std::vector<T> const &v) const override { + // !TBD: This currently works if all elements can be compared using != + // - a more general approach would be via a compare template that defaults + // to using !=. but could be specialised for, e.g. std::vector<T> etc + // - then just call that directly + if (m_comparator.size() != v.size()) + return false; + for (std::size_t i = 0; i < v.size(); ++i) + if (m_comparator[i] != v[i]) + return false; + return true; + } + std::string describe() const override { + return "Equals: " + ::Catch::Detail::stringify( m_comparator ); + } + std::vector<T> const& m_comparator; + }; + + template<typename T> + struct UnorderedEqualsMatcher : MatcherBase<std::vector<T>> { + UnorderedEqualsMatcher(std::vector<T> const& target) : m_target(target) {} + bool match(std::vector<T> const& vec) const override { + // Note: This is a reimplementation of std::is_permutation, + // because I don't want to include <algorithm> inside the common path + if (m_target.size() != vec.size()) { + return false; + } + auto lfirst = m_target.begin(), llast = m_target.end(); + auto rfirst = vec.begin(), rlast = vec.end(); + // Cut common prefix to optimize checking of permuted parts + while (lfirst != llast && *lfirst == *rfirst) { + ++lfirst; ++rfirst; + } + if (lfirst == llast) { + return true; + } + + for (auto mid = lfirst; mid != llast; ++mid) { + // Skip already counted items + if (Detail::contains(lfirst, mid, *mid)) { + continue; + } + size_t num_vec = Detail::count(rfirst, rlast, *mid); + if (num_vec == 0 || Detail::count(lfirst, llast, *mid) != num_vec) { + return false; + } + } + + return true; + } + + std::string describe() const override { + return "UnorderedEquals: " + ::Catch::Detail::stringify(m_target); + } + private: + std::vector<T> const& m_target; + }; + + } // namespace Vector + + // The following functions create the actual matcher objects. + // This allows the types to be inferred + + template<typename T> + Vector::ContainsMatcher<T> Contains( std::vector<T> const& comparator ) { + return Vector::ContainsMatcher<T>( comparator ); + } + + template<typename T> + Vector::ContainsElementMatcher<T> VectorContains( T const& comparator ) { + return Vector::ContainsElementMatcher<T>( comparator ); + } + + template<typename T> + Vector::EqualsMatcher<T> Equals( std::vector<T> const& comparator ) { + return Vector::EqualsMatcher<T>( comparator ); + } + + template<typename T> + Vector::UnorderedEqualsMatcher<T> UnorderedEquals(std::vector<T> const& target) { + return Vector::UnorderedEqualsMatcher<T>(target); + } + +} // namespace Matchers +} // namespace Catch + +// end catch_matchers_vector.h +namespace Catch { + + template<typename ArgT, typename MatcherT> + class MatchExpr : public ITransientExpression { + ArgT const& m_arg; + MatcherT m_matcher; + StringRef m_matcherString; + public: + MatchExpr( ArgT const& arg, MatcherT const& matcher, StringRef const& matcherString ) + : ITransientExpression{ true, matcher.match( arg ) }, + m_arg( arg ), + m_matcher( matcher ), + m_matcherString( matcherString ) + {} + + void streamReconstructedExpression( std::ostream &os ) const override { + auto matcherAsString = m_matcher.toString(); + os << Catch::Detail::stringify( m_arg ) << ' '; + if( matcherAsString == Detail::unprintableString ) + os << m_matcherString; + else + os << matcherAsString; + } + }; + + using StringMatcher = Matchers::Impl::MatcherBase<std::string>; + + void handleExceptionMatchExpr( AssertionHandler& handler, StringMatcher const& matcher, StringRef const& matcherString ); + + template<typename ArgT, typename MatcherT> + auto makeMatchExpr( ArgT const& arg, MatcherT const& matcher, StringRef const& matcherString ) -> MatchExpr<ArgT, MatcherT> { + return MatchExpr<ArgT, MatcherT>( arg, matcher, matcherString ); + } + +} // namespace Catch + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CHECK_THAT( macroName, matcher, resultDisposition, arg ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(arg) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ + INTERNAL_CATCH_TRY { \ + catchAssertionHandler.handleExpr( Catch::makeMatchExpr( arg, matcher, #matcher##_catch_sr ) ); \ + } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_THROWS_MATCHES( macroName, exceptionType, resultDisposition, matcher, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(exceptionType) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ + if( catchAssertionHandler.allowThrows() ) \ + try { \ + static_cast<void>(__VA_ARGS__ ); \ + catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ + } \ + catch( exceptionType const& ex ) { \ + catchAssertionHandler.handleExpr( Catch::makeMatchExpr( ex, matcher, #matcher##_catch_sr ) ); \ + } \ + catch( ... ) { \ + catchAssertionHandler.handleUnexpectedInflightException(); \ + } \ + else \ + catchAssertionHandler.handleThrowingCallSkipped(); \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +// end catch_capture_matchers.h +#endif +// start catch_generators.hpp + +// start catch_interfaces_generatortracker.h + + +#include <memory> + +namespace Catch { + + namespace Generators { + class GeneratorBase { + protected: + size_t m_size = 0; + + public: + GeneratorBase( size_t size ) : m_size( size ) {} + virtual ~GeneratorBase(); + auto size() const -> size_t { return m_size; } + }; + using GeneratorBasePtr = std::unique_ptr<GeneratorBase>; + + } // namespace Generators + + struct IGeneratorTracker { + virtual ~IGeneratorTracker(); + virtual auto hasGenerator() const -> bool = 0; + virtual auto getGenerator() const -> Generators::GeneratorBasePtr const& = 0; + virtual void setGenerator( Generators::GeneratorBasePtr&& generator ) = 0; + virtual auto getIndex() const -> std::size_t = 0; + }; + +} // namespace Catch + +// end catch_interfaces_generatortracker.h +// start catch_enforce.h + +#include <stdexcept> + +namespace Catch { +#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) + template <typename Ex> + [[noreturn]] + void throw_exception(Ex const& e) { + throw e; + } +#else // ^^ Exceptions are enabled // Exceptions are disabled vv + [[noreturn]] + void throw_exception(std::exception const& e); +#endif +} // namespace Catch; + +#define CATCH_PREPARE_EXCEPTION( type, msg ) \ + type( ( Catch::ReusableStringStream() << msg ).str() ) +#define CATCH_INTERNAL_ERROR( msg ) \ + Catch::throw_exception(CATCH_PREPARE_EXCEPTION( std::logic_error, CATCH_INTERNAL_LINEINFO << ": Internal Catch error: " << msg)) +#define CATCH_ERROR( msg ) \ + Catch::throw_exception(CATCH_PREPARE_EXCEPTION( std::domain_error, msg )) +#define CATCH_RUNTIME_ERROR( msg ) \ + Catch::throw_exception(CATCH_PREPARE_EXCEPTION( std::runtime_error, msg )) +#define CATCH_ENFORCE( condition, msg ) \ + do{ if( !(condition) ) CATCH_ERROR( msg ); } while(false) + +// end catch_enforce.h +#include <memory> +#include <vector> +#include <cassert> + +#include <utility> + +namespace Catch { +namespace Generators { + + // !TBD move this into its own location? + namespace pf{ + template<typename T, typename... Args> + std::unique_ptr<T> make_unique( Args&&... args ) { + return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); + } + } + + template<typename T> + struct IGenerator { + virtual ~IGenerator() {} + virtual auto get( size_t index ) const -> T = 0; + }; + + template<typename T> + class SingleValueGenerator : public IGenerator<T> { + T m_value; + public: + SingleValueGenerator( T const& value ) : m_value( value ) {} + + auto get( size_t ) const -> T override { + return m_value; + } + }; + + template<typename T> + class FixedValuesGenerator : public IGenerator<T> { + std::vector<T> m_values; + + public: + FixedValuesGenerator( std::initializer_list<T> values ) : m_values( values ) {} + + auto get( size_t index ) const -> T override { + return m_values[index]; + } + }; + + template<typename T> + class RangeGenerator : public IGenerator<T> { + T const m_first; + T const m_last; + + public: + RangeGenerator( T const& first, T const& last ) : m_first( first ), m_last( last ) { + assert( m_last > m_first ); + } + + auto get( size_t index ) const -> T override { + // ToDo:: introduce a safe cast to catch potential overflows + return static_cast<T>(m_first+index); + } + }; + + template<typename T> + struct NullGenerator : IGenerator<T> { + auto get( size_t ) const -> T override { + CATCH_INTERNAL_ERROR("A Null Generator is always empty"); + } + }; + + template<typename T> + class Generator { + std::unique_ptr<IGenerator<T>> m_generator; + size_t m_size; + + public: + Generator( size_t size, std::unique_ptr<IGenerator<T>> generator ) + : m_generator( std::move( generator ) ), + m_size( size ) + {} + + auto size() const -> size_t { return m_size; } + auto operator[]( size_t index ) const -> T { + assert( index < m_size ); + return m_generator->get( index ); + } + }; + + std::vector<size_t> randomiseIndices( size_t selectionSize, size_t sourceSize ); + + template<typename T> + class GeneratorRandomiser : public IGenerator<T> { + Generator<T> m_baseGenerator; + + std::vector<size_t> m_indices; + public: + GeneratorRandomiser( Generator<T>&& baseGenerator, size_t numberOfItems ) + : m_baseGenerator( std::move( baseGenerator ) ), + m_indices( randomiseIndices( numberOfItems, m_baseGenerator.size() ) ) + {} + + auto get( size_t index ) const -> T override { + return m_baseGenerator[m_indices[index]]; + } + }; + + template<typename T> + struct RequiresASpecialisationFor; + + template<typename T> + auto all() -> Generator<T> { return RequiresASpecialisationFor<T>(); } + + template<> + auto all<int>() -> Generator<int>; + + template<typename T> + auto range( T const& first, T const& last ) -> Generator<T> { + return Generator<T>( (last-first), pf::make_unique<RangeGenerator<T>>( first, last ) ); + } + + template<typename T> + auto random( T const& first, T const& last ) -> Generator<T> { + auto gen = range( first, last ); + auto size = gen.size(); + + return Generator<T>( size, pf::make_unique<GeneratorRandomiser<T>>( std::move( gen ), size ) ); + } + template<typename T> + auto random( size_t size ) -> Generator<T> { + return Generator<T>( size, pf::make_unique<GeneratorRandomiser<T>>( all<T>(), size ) ); + } + + template<typename T> + auto values( std::initializer_list<T> values ) -> Generator<T> { + return Generator<T>( values.size(), pf::make_unique<FixedValuesGenerator<T>>( values ) ); + } + template<typename T> + auto value( T const& val ) -> Generator<T> { + return Generator<T>( 1, pf::make_unique<SingleValueGenerator<T>>( val ) ); + } + + template<typename T> + auto as() -> Generator<T> { + return Generator<T>( 0, pf::make_unique<NullGenerator<T>>() ); + } + + template<typename... Ts> + auto table( std::initializer_list<std::tuple<Ts...>>&& tuples ) -> Generator<std::tuple<Ts...>> { + return values<std::tuple<Ts...>>( std::forward<std::initializer_list<std::tuple<Ts...>>>( tuples ) ); + } + + template<typename T> + struct Generators : GeneratorBase { + std::vector<Generator<T>> m_generators; + + using type = T; + + Generators() : GeneratorBase( 0 ) {} + + void populate( T&& val ) { + m_size += 1; + m_generators.emplace_back( value( std::move( val ) ) ); + } + template<typename U> + void populate( U&& val ) { + populate( T( std::move( val ) ) ); + } + void populate( Generator<T>&& generator ) { + m_size += generator.size(); + m_generators.emplace_back( std::move( generator ) ); + } + + template<typename U, typename... Gs> + void populate( U&& valueOrGenerator, Gs... moreGenerators ) { + populate( std::forward<U>( valueOrGenerator ) ); + populate( std::forward<Gs>( moreGenerators )... ); + } + + auto operator[]( size_t index ) const -> T { + size_t sizes = 0; + for( auto const& gen : m_generators ) { + auto localIndex = index-sizes; + sizes += gen.size(); + if( index < sizes ) + return gen[localIndex]; + } + CATCH_INTERNAL_ERROR("Index '" << index << "' is out of range (" << sizes << ')'); + } + }; + + template<typename T, typename... Gs> + auto makeGenerators( Generator<T>&& generator, Gs... moreGenerators ) -> Generators<T> { + Generators<T> generators; + generators.m_generators.reserve( 1+sizeof...(Gs) ); + generators.populate( std::move( generator ), std::forward<Gs>( moreGenerators )... ); + return generators; + } + template<typename T> + auto makeGenerators( Generator<T>&& generator ) -> Generators<T> { + Generators<T> generators; + generators.populate( std::move( generator ) ); + return generators; + } + template<typename T, typename... Gs> + auto makeGenerators( T&& val, Gs... moreGenerators ) -> Generators<T> { + return makeGenerators( value( std::forward<T>( val ) ), std::forward<Gs>( moreGenerators )... ); + } + template<typename T, typename U, typename... Gs> + auto makeGenerators( U&& val, Gs... moreGenerators ) -> Generators<T> { + return makeGenerators( value( T( std::forward<U>( val ) ) ), std::forward<Gs>( moreGenerators )... ); + } + + auto acquireGeneratorTracker( SourceLineInfo const& lineInfo ) -> IGeneratorTracker&; + + template<typename L> + // Note: The type after -> is weird, because VS2015 cannot parse + // the expression used in the typedef inside, when it is in + // return type. Yeah, ¯\_(ツ)_/¯ + auto generate( SourceLineInfo const& lineInfo, L const& generatorExpression ) -> decltype(std::declval<decltype(generatorExpression())>()[0]) { + using UnderlyingType = typename decltype(generatorExpression())::type; + + IGeneratorTracker& tracker = acquireGeneratorTracker( lineInfo ); + if( !tracker.hasGenerator() ) + tracker.setGenerator( pf::make_unique<Generators<UnderlyingType>>( generatorExpression() ) ); + + auto const& generator = static_cast<Generators<UnderlyingType> const&>( *tracker.getGenerator() ); + return generator[tracker.getIndex()]; + } + +} // namespace Generators +} // namespace Catch + +#define GENERATE( ... ) \ + Catch::Generators::generate( CATCH_INTERNAL_LINEINFO, []{ using namespace Catch::Generators; return makeGenerators( __VA_ARGS__ ); } ) + +// end catch_generators.hpp + +// These files are included here so the single_include script doesn't put them +// in the conditionally compiled sections +// start catch_test_case_info.h + +#include <string> +#include <vector> +#include <memory> + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif + +namespace Catch { + + struct ITestInvoker; + + struct TestCaseInfo { + enum SpecialProperties{ + None = 0, + IsHidden = 1 << 1, + ShouldFail = 1 << 2, + MayFail = 1 << 3, + Throws = 1 << 4, + NonPortable = 1 << 5, + Benchmark = 1 << 6 + }; + + TestCaseInfo( std::string const& _name, + std::string const& _className, + std::string const& _description, + std::vector<std::string> const& _tags, + SourceLineInfo const& _lineInfo ); + + friend void setTags( TestCaseInfo& testCaseInfo, std::vector<std::string> tags ); + + bool isHidden() const; + bool throws() const; + bool okToFail() const; + bool expectedToFail() const; + + std::string tagsAsString() const; + + std::string name; + std::string className; + std::string description; + std::vector<std::string> tags; + std::vector<std::string> lcaseTags; + SourceLineInfo lineInfo; + SpecialProperties properties; + }; + + class TestCase : public TestCaseInfo { + public: + + TestCase( ITestInvoker* testCase, TestCaseInfo&& info ); + + TestCase withName( std::string const& _newName ) const; + + void invoke() const; + + TestCaseInfo const& getTestCaseInfo() const; + + bool operator == ( TestCase const& other ) const; + bool operator < ( TestCase const& other ) const; + + private: + std::shared_ptr<ITestInvoker> test; + }; + + TestCase makeTestCase( ITestInvoker* testCase, + std::string const& className, + NameAndTags const& nameAndTags, + SourceLineInfo const& lineInfo ); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// end catch_test_case_info.h +// start catch_interfaces_runner.h + +namespace Catch { + + struct IRunner { + virtual ~IRunner(); + virtual bool aborting() const = 0; + }; +} + +// end catch_interfaces_runner.h + +#ifdef __OBJC__ +// start catch_objc.hpp + +#import <objc/runtime.h> + +#include <string> + +// NB. Any general catch headers included here must be included +// in catch.hpp first to make sure they are included by the single +// header for non obj-usage + +/////////////////////////////////////////////////////////////////////////////// +// This protocol is really only here for (self) documenting purposes, since +// all its methods are optional. +@protocol OcFixture + +@optional + +-(void) setUp; +-(void) tearDown; + +@end + +namespace Catch { + + class OcMethod : public ITestInvoker { + + public: + OcMethod( Class cls, SEL sel ) : m_cls( cls ), m_sel( sel ) {} + + virtual void invoke() const { + id obj = [[m_cls alloc] init]; + + performOptionalSelector( obj, @selector(setUp) ); + performOptionalSelector( obj, m_sel ); + performOptionalSelector( obj, @selector(tearDown) ); + + arcSafeRelease( obj ); + } + private: + virtual ~OcMethod() {} + + Class m_cls; + SEL m_sel; + }; + + namespace Detail{ + + inline std::string getAnnotation( Class cls, + std::string const& annotationName, + std::string const& testCaseName ) { + NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()]; + SEL sel = NSSelectorFromString( selStr ); + arcSafeRelease( selStr ); + id value = performOptionalSelector( cls, sel ); + if( value ) + return [(NSString*)value UTF8String]; + return ""; + } + } + + inline std::size_t registerTestMethods() { + std::size_t noTestMethods = 0; + int noClasses = objc_getClassList( nullptr, 0 ); + + Class* classes = (CATCH_UNSAFE_UNRETAINED Class *)malloc( sizeof(Class) * noClasses); + objc_getClassList( classes, noClasses ); + + for( int c = 0; c < noClasses; c++ ) { + Class cls = classes[c]; + { + u_int count; + Method* methods = class_copyMethodList( cls, &count ); + for( u_int m = 0; m < count ; m++ ) { + SEL selector = method_getName(methods[m]); + std::string methodName = sel_getName(selector); + if( startsWith( methodName, "Catch_TestCase_" ) ) { + std::string testCaseName = methodName.substr( 15 ); + std::string name = Detail::getAnnotation( cls, "Name", testCaseName ); + std::string desc = Detail::getAnnotation( cls, "Description", testCaseName ); + const char* className = class_getName( cls ); + + getMutableRegistryHub().registerTest( makeTestCase( new OcMethod( cls, selector ), className, NameAndTags( name.c_str(), desc.c_str() ), SourceLineInfo("",0) ) ); + noTestMethods++; + } + } + free(methods); + } + } + return noTestMethods; + } + +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) + + namespace Matchers { + namespace Impl { + namespace NSStringMatchers { + + struct StringHolder : MatcherBase<NSString*>{ + StringHolder( NSString* substr ) : m_substr( [substr copy] ){} + StringHolder( StringHolder const& other ) : m_substr( [other.m_substr copy] ){} + StringHolder() { + arcSafeRelease( m_substr ); + } + + bool match( NSString* arg ) const override { + return false; + } + + NSString* CATCH_ARC_STRONG m_substr; + }; + + struct Equals : StringHolder { + Equals( NSString* substr ) : StringHolder( substr ){} + + bool match( NSString* str ) const override { + return (str != nil || m_substr == nil ) && + [str isEqualToString:m_substr]; + } + + std::string describe() const override { + return "equals string: " + Catch::Detail::stringify( m_substr ); + } + }; + + struct Contains : StringHolder { + Contains( NSString* substr ) : StringHolder( substr ){} + + bool match( NSString* str ) const { + return (str != nil || m_substr == nil ) && + [str rangeOfString:m_substr].location != NSNotFound; + } + + std::string describe() const override { + return "contains string: " + Catch::Detail::stringify( m_substr ); + } + }; + + struct StartsWith : StringHolder { + StartsWith( NSString* substr ) : StringHolder( substr ){} + + bool match( NSString* str ) const override { + return (str != nil || m_substr == nil ) && + [str rangeOfString:m_substr].location == 0; + } + + std::string describe() const override { + return "starts with: " + Catch::Detail::stringify( m_substr ); + } + }; + struct EndsWith : StringHolder { + EndsWith( NSString* substr ) : StringHolder( substr ){} + + bool match( NSString* str ) const override { + return (str != nil || m_substr == nil ) && + [str rangeOfString:m_substr].location == [str length] - [m_substr length]; + } + + std::string describe() const override { + return "ends with: " + Catch::Detail::stringify( m_substr ); + } + }; + + } // namespace NSStringMatchers + } // namespace Impl + + inline Impl::NSStringMatchers::Equals + Equals( NSString* substr ){ return Impl::NSStringMatchers::Equals( substr ); } + + inline Impl::NSStringMatchers::Contains + Contains( NSString* substr ){ return Impl::NSStringMatchers::Contains( substr ); } + + inline Impl::NSStringMatchers::StartsWith + StartsWith( NSString* substr ){ return Impl::NSStringMatchers::StartsWith( substr ); } + + inline Impl::NSStringMatchers::EndsWith + EndsWith( NSString* substr ){ return Impl::NSStringMatchers::EndsWith( substr ); } + + } // namespace Matchers + + using namespace Matchers; + +#endif // CATCH_CONFIG_DISABLE_MATCHERS + +} // namespace Catch + +/////////////////////////////////////////////////////////////////////////////// +#define OC_MAKE_UNIQUE_NAME( root, uniqueSuffix ) root##uniqueSuffix +#define OC_TEST_CASE2( name, desc, uniqueSuffix ) \ ++(NSString*) OC_MAKE_UNIQUE_NAME( Catch_Name_test_, uniqueSuffix ) \ +{ \ +return @ name; \ +} \ ++(NSString*) OC_MAKE_UNIQUE_NAME( Catch_Description_test_, uniqueSuffix ) \ +{ \ +return @ desc; \ +} \ +-(void) OC_MAKE_UNIQUE_NAME( Catch_TestCase_test_, uniqueSuffix ) + +#define OC_TEST_CASE( name, desc ) OC_TEST_CASE2( name, desc, __LINE__ ) + +// end catch_objc.hpp +#endif + +#ifdef CATCH_CONFIG_EXTERNAL_INTERFACES +// start catch_external_interfaces.h + +// start catch_reporter_bases.hpp + +// start catch_interfaces_reporter.h + +// start catch_config.hpp + +// start catch_test_spec_parser.h + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif + +// start catch_test_spec.h + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif + +// start catch_wildcard_pattern.h + +namespace Catch +{ + class WildcardPattern { + enum WildcardPosition { + NoWildcard = 0, + WildcardAtStart = 1, + WildcardAtEnd = 2, + WildcardAtBothEnds = WildcardAtStart | WildcardAtEnd + }; + + public: + + WildcardPattern( std::string const& pattern, CaseSensitive::Choice caseSensitivity ); + virtual ~WildcardPattern() = default; + virtual bool matches( std::string const& str ) const; + + private: + std::string adjustCase( std::string const& str ) const; + CaseSensitive::Choice m_caseSensitivity; + WildcardPosition m_wildcard = NoWildcard; + std::string m_pattern; + }; +} + +// end catch_wildcard_pattern.h +#include <string> +#include <vector> +#include <memory> + +namespace Catch { + + class TestSpec { + struct Pattern { + virtual ~Pattern(); + virtual bool matches( TestCaseInfo const& testCase ) const = 0; + }; + using PatternPtr = std::shared_ptr<Pattern>; + + class NamePattern : public Pattern { + public: + NamePattern( std::string const& name ); + virtual ~NamePattern(); + virtual bool matches( TestCaseInfo const& testCase ) const override; + private: + WildcardPattern m_wildcardPattern; + }; + + class TagPattern : public Pattern { + public: + TagPattern( std::string const& tag ); + virtual ~TagPattern(); + virtual bool matches( TestCaseInfo const& testCase ) const override; + private: + std::string m_tag; + }; + + class ExcludedPattern : public Pattern { + public: + ExcludedPattern( PatternPtr const& underlyingPattern ); + virtual ~ExcludedPattern(); + virtual bool matches( TestCaseInfo const& testCase ) const override; + private: + PatternPtr m_underlyingPattern; + }; + + struct Filter { + std::vector<PatternPtr> m_patterns; + + bool matches( TestCaseInfo const& testCase ) const; + }; + + public: + bool hasFilters() const; + bool matches( TestCaseInfo const& testCase ) const; + + private: + std::vector<Filter> m_filters; + + friend class TestSpecParser; + }; +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// end catch_test_spec.h +// start catch_interfaces_tag_alias_registry.h + +#include <string> + +namespace Catch { + + struct TagAlias; + + struct ITagAliasRegistry { + virtual ~ITagAliasRegistry(); + // Nullptr if not present + virtual TagAlias const* find( std::string const& alias ) const = 0; + virtual std::string expandAliases( std::string const& unexpandedTestSpec ) const = 0; + + static ITagAliasRegistry const& get(); + }; + +} // end namespace Catch + +// end catch_interfaces_tag_alias_registry.h +namespace Catch { + + class TestSpecParser { + enum Mode{ None, Name, QuotedName, Tag, EscapedName }; + Mode m_mode = None; + bool m_exclusion = false; + std::size_t m_start = std::string::npos, m_pos = 0; + std::string m_arg; + std::vector<std::size_t> m_escapeChars; + TestSpec::Filter m_currentFilter; + TestSpec m_testSpec; + ITagAliasRegistry const* m_tagAliases = nullptr; + + public: + TestSpecParser( ITagAliasRegistry const& tagAliases ); + + TestSpecParser& parse( std::string const& arg ); + TestSpec testSpec(); + + private: + void visitChar( char c ); + void startNewMode( Mode mode, std::size_t start ); + void escape(); + std::string subString() const; + + template<typename T> + void addPattern() { + std::string token = subString(); + for( std::size_t i = 0; i < m_escapeChars.size(); ++i ) + token = token.substr( 0, m_escapeChars[i]-m_start-i ) + token.substr( m_escapeChars[i]-m_start-i+1 ); + m_escapeChars.clear(); + if( startsWith( token, "exclude:" ) ) { + m_exclusion = true; + token = token.substr( 8 ); + } + if( !token.empty() ) { + TestSpec::PatternPtr pattern = std::make_shared<T>( token ); + if( m_exclusion ) + pattern = std::make_shared<TestSpec::ExcludedPattern>( pattern ); + m_currentFilter.m_patterns.push_back( pattern ); + } + m_exclusion = false; + m_mode = None; + } + + void addFilter(); + }; + TestSpec parseTestSpec( std::string const& arg ); + +} // namespace Catch + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// end catch_test_spec_parser.h +// start catch_interfaces_config.h + +#include <iosfwd> +#include <string> +#include <vector> +#include <memory> + +namespace Catch { + + enum class Verbosity { + Quiet = 0, + Normal, + High + }; + + struct WarnAbout { enum What { + Nothing = 0x00, + NoAssertions = 0x01, + NoTests = 0x02 + }; }; + + struct ShowDurations { enum OrNot { + DefaultForReporter, + Always, + Never + }; }; + struct RunTests { enum InWhatOrder { + InDeclarationOrder, + InLexicographicalOrder, + InRandomOrder + }; }; + struct UseColour { enum YesOrNo { + Auto, + Yes, + No + }; }; + struct WaitForKeypress { enum When { + Never, + BeforeStart = 1, + BeforeExit = 2, + BeforeStartAndExit = BeforeStart | BeforeExit + }; }; + + class TestSpec; + + struct IConfig : NonCopyable { + + virtual ~IConfig(); + + virtual bool allowThrows() const = 0; + virtual std::ostream& stream() const = 0; + virtual std::string name() const = 0; + virtual bool includeSuccessfulResults() const = 0; + virtual bool shouldDebugBreak() const = 0; + virtual bool warnAboutMissingAssertions() const = 0; + virtual bool warnAboutNoTests() const = 0; + virtual int abortAfter() const = 0; + virtual bool showInvisibles() const = 0; + virtual ShowDurations::OrNot showDurations() const = 0; + virtual TestSpec const& testSpec() const = 0; + virtual bool hasTestFilters() const = 0; + virtual RunTests::InWhatOrder runOrder() const = 0; + virtual unsigned int rngSeed() const = 0; + virtual int benchmarkResolutionMultiple() const = 0; + virtual UseColour::YesOrNo useColour() const = 0; + virtual std::vector<std::string> const& getSectionsToRun() const = 0; + virtual Verbosity verbosity() const = 0; + }; + + using IConfigPtr = std::shared_ptr<IConfig const>; +} + +// end catch_interfaces_config.h +// Libstdc++ doesn't like incomplete classes for unique_ptr + +#include <memory> +#include <vector> +#include <string> + +#ifndef CATCH_CONFIG_CONSOLE_WIDTH +#define CATCH_CONFIG_CONSOLE_WIDTH 80 +#endif + +namespace Catch { + + struct IStream; + + struct ConfigData { + bool listTests = false; + bool listTags = false; + bool listReporters = false; + bool listTestNamesOnly = false; + + bool showSuccessfulTests = false; + bool shouldDebugBreak = false; + bool noThrow = false; + bool showHelp = false; + bool showInvisibles = false; + bool filenamesAsTags = false; + bool libIdentify = false; + + int abortAfter = -1; + unsigned int rngSeed = 0; + int benchmarkResolutionMultiple = 100; + + Verbosity verbosity = Verbosity::Normal; + WarnAbout::What warnings = WarnAbout::Nothing; + ShowDurations::OrNot showDurations = ShowDurations::DefaultForReporter; + RunTests::InWhatOrder runOrder = RunTests::InDeclarationOrder; + UseColour::YesOrNo useColour = UseColour::Auto; + WaitForKeypress::When waitForKeypress = WaitForKeypress::Never; + + std::string outputFilename; + std::string name; + std::string processName; +#ifndef CATCH_CONFIG_DEFAULT_REPORTER +#define CATCH_CONFIG_DEFAULT_REPORTER "console" +#endif + std::string reporterName = CATCH_CONFIG_DEFAULT_REPORTER; +#undef CATCH_CONFIG_DEFAULT_REPORTER + + std::vector<std::string> testsOrTags; + std::vector<std::string> sectionsToRun; + }; + + class Config : public IConfig { + public: + + Config() = default; + Config( ConfigData const& data ); + virtual ~Config() = default; + + std::string const& getFilename() const; + + bool listTests() const; + bool listTestNamesOnly() const; + bool listTags() const; + bool listReporters() const; + + std::string getProcessName() const; + std::string const& getReporterName() const; + + std::vector<std::string> const& getTestsOrTags() const; + std::vector<std::string> const& getSectionsToRun() const override; + + virtual TestSpec const& testSpec() const override; + bool hasTestFilters() const override; + + bool showHelp() const; + + // IConfig interface + bool allowThrows() const override; + std::ostream& stream() const override; + std::string name() const override; + bool includeSuccessfulResults() const override; + bool warnAboutMissingAssertions() const override; + bool warnAboutNoTests() const override; + ShowDurations::OrNot showDurations() const override; + RunTests::InWhatOrder runOrder() const override; + unsigned int rngSeed() const override; + int benchmarkResolutionMultiple() const override; + UseColour::YesOrNo useColour() const override; + bool shouldDebugBreak() const override; + int abortAfter() const override; + bool showInvisibles() const override; + Verbosity verbosity() const override; + + private: + + IStream const* openStream(); + ConfigData m_data; + + std::unique_ptr<IStream const> m_stream; + TestSpec m_testSpec; + bool m_hasTestFilters = false; + }; + +} // end namespace Catch + +// end catch_config.hpp +// start catch_assertionresult.h + +#include <string> + +namespace Catch { + + struct AssertionResultData + { + AssertionResultData() = delete; + + AssertionResultData( ResultWas::OfType _resultType, LazyExpression const& _lazyExpression ); + + std::string message; + mutable std::string reconstructedExpression; + LazyExpression lazyExpression; + ResultWas::OfType resultType; + + std::string reconstructExpression() const; + }; + + class AssertionResult { + public: + AssertionResult() = delete; + AssertionResult( AssertionInfo const& info, AssertionResultData const& data ); + + bool isOk() const; + bool succeeded() const; + ResultWas::OfType getResultType() const; + bool hasExpression() const; + bool hasMessage() const; + std::string getExpression() const; + std::string getExpressionInMacro() const; + bool hasExpandedExpression() const; + std::string getExpandedExpression() const; + std::string getMessage() const; + SourceLineInfo getSourceInfo() const; + StringRef getTestMacroName() const; + + //protected: + AssertionInfo m_info; + AssertionResultData m_resultData; + }; + +} // end namespace Catch + +// end catch_assertionresult.h +// start catch_option.hpp + +namespace Catch { + + // An optional type + template<typename T> + class Option { + public: + Option() : nullableValue( nullptr ) {} + Option( T const& _value ) + : nullableValue( new( storage ) T( _value ) ) + {} + Option( Option const& _other ) + : nullableValue( _other ? new( storage ) T( *_other ) : nullptr ) + {} + + ~Option() { + reset(); + } + + Option& operator= ( Option const& _other ) { + if( &_other != this ) { + reset(); + if( _other ) + nullableValue = new( storage ) T( *_other ); + } + return *this; + } + Option& operator = ( T const& _value ) { + reset(); + nullableValue = new( storage ) T( _value ); + return *this; + } + + void reset() { + if( nullableValue ) + nullableValue->~T(); + nullableValue = nullptr; + } + + T& operator*() { return *nullableValue; } + T const& operator*() const { return *nullableValue; } + T* operator->() { return nullableValue; } + const T* operator->() const { return nullableValue; } + + T valueOr( T const& defaultValue ) const { + return nullableValue ? *nullableValue : defaultValue; + } + + bool some() const { return nullableValue != nullptr; } + bool none() const { return nullableValue == nullptr; } + + bool operator !() const { return nullableValue == nullptr; } + explicit operator bool() const { + return some(); + } + + private: + T *nullableValue; + alignas(alignof(T)) char storage[sizeof(T)]; + }; + +} // end namespace Catch + +// end catch_option.hpp +#include <string> +#include <iosfwd> +#include <map> +#include <set> +#include <memory> + +namespace Catch { + + struct ReporterConfig { + explicit ReporterConfig( IConfigPtr const& _fullConfig ); + + ReporterConfig( IConfigPtr const& _fullConfig, std::ostream& _stream ); + + std::ostream& stream() const; + IConfigPtr fullConfig() const; + + private: + std::ostream* m_stream; + IConfigPtr m_fullConfig; + }; + + struct ReporterPreferences { + bool shouldRedirectStdOut = false; + bool shouldReportAllAssertions = false; + }; + + template<typename T> + struct LazyStat : Option<T> { + LazyStat& operator=( T const& _value ) { + Option<T>::operator=( _value ); + used = false; + return *this; + } + void reset() { + Option<T>::reset(); + used = false; + } + bool used = false; + }; + + struct TestRunInfo { + TestRunInfo( std::string const& _name ); + std::string name; + }; + struct GroupInfo { + GroupInfo( std::string const& _name, + std::size_t _groupIndex, + std::size_t _groupsCount ); + + std::string name; + std::size_t groupIndex; + std::size_t groupsCounts; + }; + + struct AssertionStats { + AssertionStats( AssertionResult const& _assertionResult, + std::vector<MessageInfo> const& _infoMessages, + Totals const& _totals ); + + AssertionStats( AssertionStats const& ) = default; + AssertionStats( AssertionStats && ) = default; + AssertionStats& operator = ( AssertionStats const& ) = default; + AssertionStats& operator = ( AssertionStats && ) = default; + virtual ~AssertionStats(); + + AssertionResult assertionResult; + std::vector<MessageInfo> infoMessages; + Totals totals; + }; + + struct SectionStats { + SectionStats( SectionInfo const& _sectionInfo, + Counts const& _assertions, + double _durationInSeconds, + bool _missingAssertions ); + SectionStats( SectionStats const& ) = default; + SectionStats( SectionStats && ) = default; + SectionStats& operator = ( SectionStats const& ) = default; + SectionStats& operator = ( SectionStats && ) = default; + virtual ~SectionStats(); + + SectionInfo sectionInfo; + Counts assertions; + double durationInSeconds; + bool missingAssertions; + }; + + struct TestCaseStats { + TestCaseStats( TestCaseInfo const& _testInfo, + Totals const& _totals, + std::string const& _stdOut, + std::string const& _stdErr, + bool _aborting ); + + TestCaseStats( TestCaseStats const& ) = default; + TestCaseStats( TestCaseStats && ) = default; + TestCaseStats& operator = ( TestCaseStats const& ) = default; + TestCaseStats& operator = ( TestCaseStats && ) = default; + virtual ~TestCaseStats(); + + TestCaseInfo testInfo; + Totals totals; + std::string stdOut; + std::string stdErr; + bool aborting; + }; + + struct TestGroupStats { + TestGroupStats( GroupInfo const& _groupInfo, + Totals const& _totals, + bool _aborting ); + TestGroupStats( GroupInfo const& _groupInfo ); + + TestGroupStats( TestGroupStats const& ) = default; + TestGroupStats( TestGroupStats && ) = default; + TestGroupStats& operator = ( TestGroupStats const& ) = default; + TestGroupStats& operator = ( TestGroupStats && ) = default; + virtual ~TestGroupStats(); + + GroupInfo groupInfo; + Totals totals; + bool aborting; + }; + + struct TestRunStats { + TestRunStats( TestRunInfo const& _runInfo, + Totals const& _totals, + bool _aborting ); + + TestRunStats( TestRunStats const& ) = default; + TestRunStats( TestRunStats && ) = default; + TestRunStats& operator = ( TestRunStats const& ) = default; + TestRunStats& operator = ( TestRunStats && ) = default; + virtual ~TestRunStats(); + + TestRunInfo runInfo; + Totals totals; + bool aborting; + }; + + struct BenchmarkInfo { + std::string name; + }; + struct BenchmarkStats { + BenchmarkInfo info; + std::size_t iterations; + uint64_t elapsedTimeInNanoseconds; + }; + + struct IStreamingReporter { + virtual ~IStreamingReporter() = default; + + // Implementing class must also provide the following static methods: + // static std::string getDescription(); + // static std::set<Verbosity> getSupportedVerbosities() + + virtual ReporterPreferences getPreferences() const = 0; + + virtual void noMatchingTestCases( std::string const& spec ) = 0; + + virtual void testRunStarting( TestRunInfo const& testRunInfo ) = 0; + virtual void testGroupStarting( GroupInfo const& groupInfo ) = 0; + + virtual void testCaseStarting( TestCaseInfo const& testInfo ) = 0; + virtual void sectionStarting( SectionInfo const& sectionInfo ) = 0; + + // *** experimental *** + virtual void benchmarkStarting( BenchmarkInfo const& ) {} + + virtual void assertionStarting( AssertionInfo const& assertionInfo ) = 0; + + // The return value indicates if the messages buffer should be cleared: + virtual bool assertionEnded( AssertionStats const& assertionStats ) = 0; + + // *** experimental *** + virtual void benchmarkEnded( BenchmarkStats const& ) {} + + virtual void sectionEnded( SectionStats const& sectionStats ) = 0; + virtual void testCaseEnded( TestCaseStats const& testCaseStats ) = 0; + virtual void testGroupEnded( TestGroupStats const& testGroupStats ) = 0; + virtual void testRunEnded( TestRunStats const& testRunStats ) = 0; + + virtual void skipTest( TestCaseInfo const& testInfo ) = 0; + + // Default empty implementation provided + virtual void fatalErrorEncountered( StringRef name ); + + virtual bool isMulti() const; + }; + using IStreamingReporterPtr = std::unique_ptr<IStreamingReporter>; + + struct IReporterFactory { + virtual ~IReporterFactory(); + virtual IStreamingReporterPtr create( ReporterConfig const& config ) const = 0; + virtual std::string getDescription() const = 0; + }; + using IReporterFactoryPtr = std::shared_ptr<IReporterFactory>; + + struct IReporterRegistry { + using FactoryMap = std::map<std::string, IReporterFactoryPtr>; + using Listeners = std::vector<IReporterFactoryPtr>; + + virtual ~IReporterRegistry(); + virtual IStreamingReporterPtr create( std::string const& name, IConfigPtr const& config ) const = 0; + virtual FactoryMap const& getFactories() const = 0; + virtual Listeners const& getListeners() const = 0; + }; + +} // end namespace Catch + +// end catch_interfaces_reporter.h +#include <algorithm> +#include <cstring> +#include <cfloat> +#include <cstdio> +#include <cassert> +#include <memory> +#include <ostream> + +namespace Catch { + void prepareExpandedExpression(AssertionResult& result); + + // Returns double formatted as %.3f (format expected on output) + std::string getFormattedDuration( double duration ); + + template<typename DerivedT> + struct StreamingReporterBase : IStreamingReporter { + + StreamingReporterBase( ReporterConfig const& _config ) + : m_config( _config.fullConfig() ), + stream( _config.stream() ) + { + m_reporterPrefs.shouldRedirectStdOut = false; + if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) ) + CATCH_ERROR( "Verbosity level not supported by this reporter" ); + } + + ReporterPreferences getPreferences() const override { + return m_reporterPrefs; + } + + static std::set<Verbosity> getSupportedVerbosities() { + return { Verbosity::Normal }; + } + + ~StreamingReporterBase() override = default; + + void noMatchingTestCases(std::string const&) override {} + + void testRunStarting(TestRunInfo const& _testRunInfo) override { + currentTestRunInfo = _testRunInfo; + } + void testGroupStarting(GroupInfo const& _groupInfo) override { + currentGroupInfo = _groupInfo; + } + + void testCaseStarting(TestCaseInfo const& _testInfo) override { + currentTestCaseInfo = _testInfo; + } + void sectionStarting(SectionInfo const& _sectionInfo) override { + m_sectionStack.push_back(_sectionInfo); + } + + void sectionEnded(SectionStats const& /* _sectionStats */) override { + m_sectionStack.pop_back(); + } + void testCaseEnded(TestCaseStats const& /* _testCaseStats */) override { + currentTestCaseInfo.reset(); + } + void testGroupEnded(TestGroupStats const& /* _testGroupStats */) override { + currentGroupInfo.reset(); + } + void testRunEnded(TestRunStats const& /* _testRunStats */) override { + currentTestCaseInfo.reset(); + currentGroupInfo.reset(); + currentTestRunInfo.reset(); + } + + void skipTest(TestCaseInfo const&) override { + // Don't do anything with this by default. + // It can optionally be overridden in the derived class. + } + + IConfigPtr m_config; + std::ostream& stream; + + LazyStat<TestRunInfo> currentTestRunInfo; + LazyStat<GroupInfo> currentGroupInfo; + LazyStat<TestCaseInfo> currentTestCaseInfo; + + std::vector<SectionInfo> m_sectionStack; + ReporterPreferences m_reporterPrefs; + }; + + template<typename DerivedT> + struct CumulativeReporterBase : IStreamingReporter { + template<typename T, typename ChildNodeT> + struct Node { + explicit Node( T const& _value ) : value( _value ) {} + virtual ~Node() {} + + using ChildNodes = std::vector<std::shared_ptr<ChildNodeT>>; + T value; + ChildNodes children; + }; + struct SectionNode { + explicit SectionNode(SectionStats const& _stats) : stats(_stats) {} + virtual ~SectionNode() = default; + + bool operator == (SectionNode const& other) const { + return stats.sectionInfo.lineInfo == other.stats.sectionInfo.lineInfo; + } + bool operator == (std::shared_ptr<SectionNode> const& other) const { + return operator==(*other); + } + + SectionStats stats; + using ChildSections = std::vector<std::shared_ptr<SectionNode>>; + using Assertions = std::vector<AssertionStats>; + ChildSections childSections; + Assertions assertions; + std::string stdOut; + std::string stdErr; + }; + + struct BySectionInfo { + BySectionInfo( SectionInfo const& other ) : m_other( other ) {} + BySectionInfo( BySectionInfo const& other ) : m_other( other.m_other ) {} + bool operator() (std::shared_ptr<SectionNode> const& node) const { + return ((node->stats.sectionInfo.name == m_other.name) && + (node->stats.sectionInfo.lineInfo == m_other.lineInfo)); + } + void operator=(BySectionInfo const&) = delete; + + private: + SectionInfo const& m_other; + }; + + using TestCaseNode = Node<TestCaseStats, SectionNode>; + using TestGroupNode = Node<TestGroupStats, TestCaseNode>; + using TestRunNode = Node<TestRunStats, TestGroupNode>; + + CumulativeReporterBase( ReporterConfig const& _config ) + : m_config( _config.fullConfig() ), + stream( _config.stream() ) + { + m_reporterPrefs.shouldRedirectStdOut = false; + if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) ) + CATCH_ERROR( "Verbosity level not supported by this reporter" ); + } + ~CumulativeReporterBase() override = default; + + ReporterPreferences getPreferences() const override { + return m_reporterPrefs; + } + + static std::set<Verbosity> getSupportedVerbosities() { + return { Verbosity::Normal }; + } + + void testRunStarting( TestRunInfo const& ) override {} + void testGroupStarting( GroupInfo const& ) override {} + + void testCaseStarting( TestCaseInfo const& ) override {} + + void sectionStarting( SectionInfo const& sectionInfo ) override { + SectionStats incompleteStats( sectionInfo, Counts(), 0, false ); + std::shared_ptr<SectionNode> node; + if( m_sectionStack.empty() ) { + if( !m_rootSection ) + m_rootSection = std::make_shared<SectionNode>( incompleteStats ); + node = m_rootSection; + } + else { + SectionNode& parentNode = *m_sectionStack.back(); + auto it = + std::find_if( parentNode.childSections.begin(), + parentNode.childSections.end(), + BySectionInfo( sectionInfo ) ); + if( it == parentNode.childSections.end() ) { + node = std::make_shared<SectionNode>( incompleteStats ); + parentNode.childSections.push_back( node ); + } + else + node = *it; + } + m_sectionStack.push_back( node ); + m_deepestSection = std::move(node); + } + + void assertionStarting(AssertionInfo const&) override {} + + bool assertionEnded(AssertionStats const& assertionStats) override { + assert(!m_sectionStack.empty()); + // AssertionResult holds a pointer to a temporary DecomposedExpression, + // which getExpandedExpression() calls to build the expression string. + // Our section stack copy of the assertionResult will likely outlive the + // temporary, so it must be expanded or discarded now to avoid calling + // a destroyed object later. + prepareExpandedExpression(const_cast<AssertionResult&>( assertionStats.assertionResult ) ); + SectionNode& sectionNode = *m_sectionStack.back(); + sectionNode.assertions.push_back(assertionStats); + return true; + } + void sectionEnded(SectionStats const& sectionStats) override { + assert(!m_sectionStack.empty()); + SectionNode& node = *m_sectionStack.back(); + node.stats = sectionStats; + m_sectionStack.pop_back(); + } + void testCaseEnded(TestCaseStats const& testCaseStats) override { + auto node = std::make_shared<TestCaseNode>(testCaseStats); + assert(m_sectionStack.size() == 0); + node->children.push_back(m_rootSection); + m_testCases.push_back(node); + m_rootSection.reset(); + + assert(m_deepestSection); + m_deepestSection->stdOut = testCaseStats.stdOut; + m_deepestSection->stdErr = testCaseStats.stdErr; + } + void testGroupEnded(TestGroupStats const& testGroupStats) override { + auto node = std::make_shared<TestGroupNode>(testGroupStats); + node->children.swap(m_testCases); + m_testGroups.push_back(node); + } + void testRunEnded(TestRunStats const& testRunStats) override { + auto node = std::make_shared<TestRunNode>(testRunStats); + node->children.swap(m_testGroups); + m_testRuns.push_back(node); + testRunEndedCumulative(); + } + virtual void testRunEndedCumulative() = 0; + + void skipTest(TestCaseInfo const&) override {} + + IConfigPtr m_config; + std::ostream& stream; + std::vector<AssertionStats> m_assertions; + std::vector<std::vector<std::shared_ptr<SectionNode>>> m_sections; + std::vector<std::shared_ptr<TestCaseNode>> m_testCases; + std::vector<std::shared_ptr<TestGroupNode>> m_testGroups; + + std::vector<std::shared_ptr<TestRunNode>> m_testRuns; + + std::shared_ptr<SectionNode> m_rootSection; + std::shared_ptr<SectionNode> m_deepestSection; + std::vector<std::shared_ptr<SectionNode>> m_sectionStack; + ReporterPreferences m_reporterPrefs; + }; + + template<char C> + char const* getLineOfChars() { + static char line[CATCH_CONFIG_CONSOLE_WIDTH] = {0}; + if( !*line ) { + std::memset( line, C, CATCH_CONFIG_CONSOLE_WIDTH-1 ); + line[CATCH_CONFIG_CONSOLE_WIDTH-1] = 0; + } + return line; + } + + struct TestEventListenerBase : StreamingReporterBase<TestEventListenerBase> { + TestEventListenerBase( ReporterConfig const& _config ); + + static std::set<Verbosity> getSupportedVerbosities(); + + void assertionStarting(AssertionInfo const&) override; + bool assertionEnded(AssertionStats const&) override; + }; + +} // end namespace Catch + +// end catch_reporter_bases.hpp +// start catch_console_colour.h + +namespace Catch { + + struct Colour { + enum Code { + None = 0, + + White, + Red, + Green, + Blue, + Cyan, + Yellow, + Grey, + + Bright = 0x10, + + BrightRed = Bright | Red, + BrightGreen = Bright | Green, + LightGrey = Bright | Grey, + BrightWhite = Bright | White, + BrightYellow = Bright | Yellow, + + // By intention + FileName = LightGrey, + Warning = BrightYellow, + ResultError = BrightRed, + ResultSuccess = BrightGreen, + ResultExpectedFailure = Warning, + + Error = BrightRed, + Success = Green, + + OriginalExpression = Cyan, + ReconstructedExpression = BrightYellow, + + SecondaryText = LightGrey, + Headers = White + }; + + // Use constructed object for RAII guard + Colour( Code _colourCode ); + Colour( Colour&& other ) noexcept; + Colour& operator=( Colour&& other ) noexcept; + ~Colour(); + + // Use static method for one-shot changes + static void use( Code _colourCode ); + + private: + bool m_moved = false; + }; + + std::ostream& operator << ( std::ostream& os, Colour const& ); + +} // end namespace Catch + +// end catch_console_colour.h +// start catch_reporter_registrars.hpp + + +namespace Catch { + + template<typename T> + class ReporterRegistrar { + + class ReporterFactory : public IReporterFactory { + + virtual IStreamingReporterPtr create( ReporterConfig const& config ) const override { + return std::unique_ptr<T>( new T( config ) ); + } + + virtual std::string getDescription() const override { + return T::getDescription(); + } + }; + + public: + + explicit ReporterRegistrar( std::string const& name ) { + getMutableRegistryHub().registerReporter( name, std::make_shared<ReporterFactory>() ); + } + }; + + template<typename T> + class ListenerRegistrar { + + class ListenerFactory : public IReporterFactory { + + virtual IStreamingReporterPtr create( ReporterConfig const& config ) const override { + return std::unique_ptr<T>( new T( config ) ); + } + virtual std::string getDescription() const override { + return std::string(); + } + }; + + public: + + ListenerRegistrar() { + getMutableRegistryHub().registerListener( std::make_shared<ListenerFactory>() ); + } + }; +} + +#if !defined(CATCH_CONFIG_DISABLE) + +#define CATCH_REGISTER_REPORTER( name, reporterType ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::ReporterRegistrar<reporterType> catch_internal_RegistrarFor##reporterType( name ); } \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS + +#define CATCH_REGISTER_LISTENER( listenerType ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::ListenerRegistrar<listenerType> catch_internal_RegistrarFor##listenerType; } \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS +#else // CATCH_CONFIG_DISABLE + +#define CATCH_REGISTER_REPORTER(name, reporterType) +#define CATCH_REGISTER_LISTENER(listenerType) + +#endif // CATCH_CONFIG_DISABLE + +// end catch_reporter_registrars.hpp +// Allow users to base their work off existing reporters +// start catch_reporter_compact.h + +namespace Catch { + + struct CompactReporter : StreamingReporterBase<CompactReporter> { + + using StreamingReporterBase::StreamingReporterBase; + + ~CompactReporter() override; + + static std::string getDescription(); + + ReporterPreferences getPreferences() const override; + + void noMatchingTestCases(std::string const& spec) override; + + void assertionStarting(AssertionInfo const&) override; + + bool assertionEnded(AssertionStats const& _assertionStats) override; + + void sectionEnded(SectionStats const& _sectionStats) override; + + void testRunEnded(TestRunStats const& _testRunStats) override; + + }; + +} // end namespace Catch + +// end catch_reporter_compact.h +// start catch_reporter_console.h + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch + // Note that 4062 (not all labels are handled + // and default is missing) is enabled +#endif + +namespace Catch { + // Fwd decls + struct SummaryColumn; + class TablePrinter; + + struct ConsoleReporter : StreamingReporterBase<ConsoleReporter> { + std::unique_ptr<TablePrinter> m_tablePrinter; + + ConsoleReporter(ReporterConfig const& config); + ~ConsoleReporter() override; + static std::string getDescription(); + + void noMatchingTestCases(std::string const& spec) override; + + void assertionStarting(AssertionInfo const&) override; + + bool assertionEnded(AssertionStats const& _assertionStats) override; + + void sectionStarting(SectionInfo const& _sectionInfo) override; + void sectionEnded(SectionStats const& _sectionStats) override; + + void benchmarkStarting(BenchmarkInfo const& info) override; + void benchmarkEnded(BenchmarkStats const& stats) override; + + void testCaseEnded(TestCaseStats const& _testCaseStats) override; + void testGroupEnded(TestGroupStats const& _testGroupStats) override; + void testRunEnded(TestRunStats const& _testRunStats) override; + + private: + + void lazyPrint(); + + void lazyPrintWithoutClosingBenchmarkTable(); + void lazyPrintRunInfo(); + void lazyPrintGroupInfo(); + void printTestCaseAndSectionHeader(); + + void printClosedHeader(std::string const& _name); + void printOpenHeader(std::string const& _name); + + // if string has a : in first line will set indent to follow it on + // subsequent lines + void printHeaderString(std::string const& _string, std::size_t indent = 0); + + void printTotals(Totals const& totals); + void printSummaryRow(std::string const& label, std::vector<SummaryColumn> const& cols, std::size_t row); + + void printTotalsDivider(Totals const& totals); + void printSummaryDivider(); + + private: + bool m_headerPrinted = false; + }; + +} // end namespace Catch + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif + +// end catch_reporter_console.h +// start catch_reporter_junit.h + +// start catch_xmlwriter.h + +#include <vector> + +namespace Catch { + + class XmlEncode { + public: + enum ForWhat { ForTextNodes, ForAttributes }; + + XmlEncode( std::string const& str, ForWhat forWhat = ForTextNodes ); + + void encodeTo( std::ostream& os ) const; + + friend std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode ); + + private: + std::string m_str; + ForWhat m_forWhat; + }; + + class XmlWriter { + public: + + class ScopedElement { + public: + ScopedElement( XmlWriter* writer ); + + ScopedElement( ScopedElement&& other ) noexcept; + ScopedElement& operator=( ScopedElement&& other ) noexcept; + + ~ScopedElement(); + + ScopedElement& writeText( std::string const& text, bool indent = true ); + + template<typename T> + ScopedElement& writeAttribute( std::string const& name, T const& attribute ) { + m_writer->writeAttribute( name, attribute ); + return *this; + } + + private: + mutable XmlWriter* m_writer = nullptr; + }; + + XmlWriter( std::ostream& os = Catch::cout() ); + ~XmlWriter(); + + XmlWriter( XmlWriter const& ) = delete; + XmlWriter& operator=( XmlWriter const& ) = delete; + + XmlWriter& startElement( std::string const& name ); + + ScopedElement scopedElement( std::string const& name ); + + XmlWriter& endElement(); + + XmlWriter& writeAttribute( std::string const& name, std::string const& attribute ); + + XmlWriter& writeAttribute( std::string const& name, bool attribute ); + + template<typename T> + XmlWriter& writeAttribute( std::string const& name, T const& attribute ) { + ReusableStringStream rss; + rss << attribute; + return writeAttribute( name, rss.str() ); + } + + XmlWriter& writeText( std::string const& text, bool indent = true ); + + XmlWriter& writeComment( std::string const& text ); + + void writeStylesheetRef( std::string const& url ); + + XmlWriter& writeBlankLine(); + + void ensureTagClosed(); + + private: + + void writeDeclaration(); + + void newlineIfNecessary(); + + bool m_tagIsOpen = false; + bool m_needsNewline = false; + std::vector<std::string> m_tags; + std::string m_indent; + std::ostream& m_os; + }; + +} + +// end catch_xmlwriter.h +namespace Catch { + + class JunitReporter : public CumulativeReporterBase<JunitReporter> { + public: + JunitReporter(ReporterConfig const& _config); + + ~JunitReporter() override; + + static std::string getDescription(); + + void noMatchingTestCases(std::string const& /*spec*/) override; + + void testRunStarting(TestRunInfo const& runInfo) override; + + void testGroupStarting(GroupInfo const& groupInfo) override; + + void testCaseStarting(TestCaseInfo const& testCaseInfo) override; + bool assertionEnded(AssertionStats const& assertionStats) override; + + void testCaseEnded(TestCaseStats const& testCaseStats) override; + + void testGroupEnded(TestGroupStats const& testGroupStats) override; + + void testRunEndedCumulative() override; + + void writeGroup(TestGroupNode const& groupNode, double suiteTime); + + void writeTestCase(TestCaseNode const& testCaseNode); + + void writeSection(std::string const& className, + std::string const& rootName, + SectionNode const& sectionNode); + + void writeAssertions(SectionNode const& sectionNode); + void writeAssertion(AssertionStats const& stats); + + XmlWriter xml; + Timer suiteTimer; + std::string stdOutForSuite; + std::string stdErrForSuite; + unsigned int unexpectedExceptions = 0; + bool m_okToFail = false; + }; + +} // end namespace Catch + +// end catch_reporter_junit.h +// start catch_reporter_xml.h + +namespace Catch { + class XmlReporter : public StreamingReporterBase<XmlReporter> { + public: + XmlReporter(ReporterConfig const& _config); + + ~XmlReporter() override; + + static std::string getDescription(); + + virtual std::string getStylesheetRef() const; + + void writeSourceInfo(SourceLineInfo const& sourceInfo); + + public: // StreamingReporterBase + + void noMatchingTestCases(std::string const& s) override; + + void testRunStarting(TestRunInfo const& testInfo) override; + + void testGroupStarting(GroupInfo const& groupInfo) override; + + void testCaseStarting(TestCaseInfo const& testInfo) override; + + void sectionStarting(SectionInfo const& sectionInfo) override; + + void assertionStarting(AssertionInfo const&) override; + + bool assertionEnded(AssertionStats const& assertionStats) override; + + void sectionEnded(SectionStats const& sectionStats) override; + + void testCaseEnded(TestCaseStats const& testCaseStats) override; + + void testGroupEnded(TestGroupStats const& testGroupStats) override; + + void testRunEnded(TestRunStats const& testRunStats) override; + + private: + Timer m_testCaseTimer; + XmlWriter m_xml; + int m_sectionDepth = 0; + }; + +} // end namespace Catch + +// end catch_reporter_xml.h + +// end catch_external_interfaces.h +#endif + +#endif // ! CATCH_CONFIG_IMPL_ONLY + +#ifdef CATCH_IMPL +// start catch_impl.hpp + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wweak-vtables" +#endif + +// Keep these here for external reporters +// start catch_test_case_tracker.h + +#include <string> +#include <vector> +#include <memory> + +namespace Catch { +namespace TestCaseTracking { + + struct NameAndLocation { + std::string name; + SourceLineInfo location; + + NameAndLocation( std::string const& _name, SourceLineInfo const& _location ); + }; + + struct ITracker; + + using ITrackerPtr = std::shared_ptr<ITracker>; + + struct ITracker { + virtual ~ITracker(); + + // static queries + virtual NameAndLocation const& nameAndLocation() const = 0; + + // dynamic queries + virtual bool isComplete() const = 0; // Successfully completed or failed + virtual bool isSuccessfullyCompleted() const = 0; + virtual bool isOpen() const = 0; // Started but not complete + virtual bool hasChildren() const = 0; + + virtual ITracker& parent() = 0; + + // actions + virtual void close() = 0; // Successfully complete + virtual void fail() = 0; + virtual void markAsNeedingAnotherRun() = 0; + + virtual void addChild( ITrackerPtr const& child ) = 0; + virtual ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) = 0; + virtual void openChild() = 0; + + // Debug/ checking + virtual bool isSectionTracker() const = 0; + virtual bool isIndexTracker() const = 0; + }; + + class TrackerContext { + + enum RunState { + NotStarted, + Executing, + CompletedCycle + }; + + ITrackerPtr m_rootTracker; + ITracker* m_currentTracker = nullptr; + RunState m_runState = NotStarted; + + public: + + static TrackerContext& instance(); + + ITracker& startRun(); + void endRun(); + + void startCycle(); + void completeCycle(); + + bool completedCycle() const; + ITracker& currentTracker(); + void setCurrentTracker( ITracker* tracker ); + }; + + class TrackerBase : public ITracker { + protected: + enum CycleState { + NotStarted, + Executing, + ExecutingChildren, + NeedsAnotherRun, + CompletedSuccessfully, + Failed + }; + + using Children = std::vector<ITrackerPtr>; + NameAndLocation m_nameAndLocation; + TrackerContext& m_ctx; + ITracker* m_parent; + Children m_children; + CycleState m_runState = NotStarted; + + public: + TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ); + + NameAndLocation const& nameAndLocation() const override; + bool isComplete() const override; + bool isSuccessfullyCompleted() const override; + bool isOpen() const override; + bool hasChildren() const override; + + void addChild( ITrackerPtr const& child ) override; + + ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) override; + ITracker& parent() override; + + void openChild() override; + + bool isSectionTracker() const override; + bool isIndexTracker() const override; + + void open(); + + void close() override; + void fail() override; + void markAsNeedingAnotherRun() override; + + private: + void moveToParent(); + void moveToThis(); + }; + + class SectionTracker : public TrackerBase { + std::vector<std::string> m_filters; + public: + SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ); + + bool isSectionTracker() const override; + + static SectionTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ); + + void tryOpen(); + + void addInitialFilters( std::vector<std::string> const& filters ); + void addNextFilters( std::vector<std::string> const& filters ); + }; + + class IndexTracker : public TrackerBase { + int m_size; + int m_index = -1; + public: + IndexTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent, int size ); + + bool isIndexTracker() const override; + void close() override; + + static IndexTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation, int size ); + + int index() const; + + void moveNext(); + }; + +} // namespace TestCaseTracking + +using TestCaseTracking::ITracker; +using TestCaseTracking::TrackerContext; +using TestCaseTracking::SectionTracker; +using TestCaseTracking::IndexTracker; + +} // namespace Catch + +// end catch_test_case_tracker.h + +// start catch_leak_detector.h + +namespace Catch { + + struct LeakDetector { + LeakDetector(); + ~LeakDetector(); + }; + +} +// end catch_leak_detector.h +// Cpp files will be included in the single-header file here +// start catch_approx.cpp + +#include <cmath> +#include <limits> + +namespace { + +// Performs equivalent check of std::fabs(lhs - rhs) <= margin +// But without the subtraction to allow for INFINITY in comparison +bool marginComparison(double lhs, double rhs, double margin) { + return (lhs + margin >= rhs) && (rhs + margin >= lhs); +} + +} + +namespace Catch { +namespace Detail { + + Approx::Approx ( double value ) + : m_epsilon( std::numeric_limits<float>::epsilon()*100 ), + m_margin( 0.0 ), + m_scale( 0.0 ), + m_value( value ) + {} + + Approx Approx::custom() { + return Approx( 0 ); + } + + Approx Approx::operator-() const { + auto temp(*this); + temp.m_value = -temp.m_value; + return temp; + } + + std::string Approx::toString() const { + ReusableStringStream rss; + rss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )"; + return rss.str(); + } + + bool Approx::equalityComparisonImpl(const double other) const { + // First try with fixed margin, then compute margin based on epsilon, scale and Approx's value + // Thanks to Richard Harris for his help refining the scaled margin value + return marginComparison(m_value, other, m_margin) || marginComparison(m_value, other, m_epsilon * (m_scale + std::fabs(m_value))); + } + + void Approx::setMargin(double margin) { + CATCH_ENFORCE(margin >= 0, + "Invalid Approx::margin: " << margin << '.' + << " Approx::Margin has to be non-negative."); + m_margin = margin; + } + + void Approx::setEpsilon(double epsilon) { + CATCH_ENFORCE(epsilon >= 0 && epsilon <= 1.0, + "Invalid Approx::epsilon: " << epsilon << '.' + << " Approx::epsilon has to be in [0, 1]"); + m_epsilon = epsilon; + } + +} // end namespace Detail + +namespace literals { + Detail::Approx operator "" _a(long double val) { + return Detail::Approx(val); + } + Detail::Approx operator "" _a(unsigned long long val) { + return Detail::Approx(val); + } +} // end namespace literals + +std::string StringMaker<Catch::Detail::Approx>::convert(Catch::Detail::Approx const& value) { + return value.toString(); +} + +} // end namespace Catch +// end catch_approx.cpp +// start catch_assertionhandler.cpp + +// start catch_context.h + +#include <memory> + +namespace Catch { + + struct IResultCapture; + struct IRunner; + struct IConfig; + struct IMutableContext; + + using IConfigPtr = std::shared_ptr<IConfig const>; + + struct IContext + { + virtual ~IContext(); + + virtual IResultCapture* getResultCapture() = 0; + virtual IRunner* getRunner() = 0; + virtual IConfigPtr const& getConfig() const = 0; + }; + + struct IMutableContext : IContext + { + virtual ~IMutableContext(); + virtual void setResultCapture( IResultCapture* resultCapture ) = 0; + virtual void setRunner( IRunner* runner ) = 0; + virtual void setConfig( IConfigPtr const& config ) = 0; + + private: + static IMutableContext *currentContext; + friend IMutableContext& getCurrentMutableContext(); + friend void cleanUpContext(); + static void createContext(); + }; + + inline IMutableContext& getCurrentMutableContext() + { + if( !IMutableContext::currentContext ) + IMutableContext::createContext(); + return *IMutableContext::currentContext; + } + + inline IContext& getCurrentContext() + { + return getCurrentMutableContext(); + } + + void cleanUpContext(); +} + +// end catch_context.h +// start catch_debugger.h + +namespace Catch { + bool isDebuggerActive(); +} + +#ifdef CATCH_PLATFORM_MAC + + #define CATCH_TRAP() __asm__("int $3\n" : : ) /* NOLINT */ + +#elif defined(CATCH_PLATFORM_LINUX) + // If we can use inline assembler, do it because this allows us to break + // directly at the location of the failing check instead of breaking inside + // raise() called from it, i.e. one stack frame below. + #if defined(__GNUC__) && (defined(__i386) || defined(__x86_64)) + #define CATCH_TRAP() asm volatile ("int $3") /* NOLINT */ + #else // Fall back to the generic way. + #include <signal.h> + + #define CATCH_TRAP() raise(SIGTRAP) + #endif +#elif defined(_MSC_VER) + #define CATCH_TRAP() __debugbreak() +#elif defined(__MINGW32__) + extern "C" __declspec(dllimport) void __stdcall DebugBreak(); + #define CATCH_TRAP() DebugBreak() +#endif + +#ifdef CATCH_TRAP + #define CATCH_BREAK_INTO_DEBUGGER() if( Catch::isDebuggerActive() ) { CATCH_TRAP(); } +#else + namespace Catch { + inline void doNothing() {} + } + #define CATCH_BREAK_INTO_DEBUGGER() Catch::doNothing() +#endif + +// end catch_debugger.h +// start catch_run_context.h + +// start catch_fatal_condition.h + +// start catch_windows_h_proxy.h + + +#if defined(CATCH_PLATFORM_WINDOWS) + +#if !defined(NOMINMAX) && !defined(CATCH_CONFIG_NO_NOMINMAX) +# define CATCH_DEFINED_NOMINMAX +# define NOMINMAX +#endif +#if !defined(WIN32_LEAN_AND_MEAN) && !defined(CATCH_CONFIG_NO_WIN32_LEAN_AND_MEAN) +# define CATCH_DEFINED_WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +#endif + +#ifdef __AFXDLL +#include <AfxWin.h> +#else +#include <windows.h> +#endif + +#ifdef CATCH_DEFINED_NOMINMAX +# undef NOMINMAX +#endif +#ifdef CATCH_DEFINED_WIN32_LEAN_AND_MEAN +# undef WIN32_LEAN_AND_MEAN +#endif + +#endif // defined(CATCH_PLATFORM_WINDOWS) + +// end catch_windows_h_proxy.h +#if defined( CATCH_CONFIG_WINDOWS_SEH ) + +namespace Catch { + + struct FatalConditionHandler { + + static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo); + FatalConditionHandler(); + static void reset(); + ~FatalConditionHandler(); + + private: + static bool isSet; + static ULONG guaranteeSize; + static PVOID exceptionHandlerHandle; + }; + +} // namespace Catch + +#elif defined ( CATCH_CONFIG_POSIX_SIGNALS ) + +#include <signal.h> + +namespace Catch { + + struct FatalConditionHandler { + + static bool isSet; + static struct sigaction oldSigActions[]; + static stack_t oldSigStack; + static char altStackMem[]; + + static void handleSignal( int sig ); + + FatalConditionHandler(); + ~FatalConditionHandler(); + static void reset(); + }; + +} // namespace Catch + +#else + +namespace Catch { + struct FatalConditionHandler { + void reset(); + }; +} + +#endif + +// end catch_fatal_condition.h +#include <string> + +namespace Catch { + + struct IMutableContext; + + /////////////////////////////////////////////////////////////////////////// + + class RunContext : public IResultCapture, public IRunner { + + public: + RunContext( RunContext const& ) = delete; + RunContext& operator =( RunContext const& ) = delete; + + explicit RunContext( IConfigPtr const& _config, IStreamingReporterPtr&& reporter ); + + ~RunContext() override; + + void testGroupStarting( std::string const& testSpec, std::size_t groupIndex, std::size_t groupsCount ); + void testGroupEnded( std::string const& testSpec, Totals const& totals, std::size_t groupIndex, std::size_t groupsCount ); + + Totals runTest(TestCase const& testCase); + + IConfigPtr config() const; + IStreamingReporter& reporter() const; + + public: // IResultCapture + + // Assertion handlers + void handleExpr + ( AssertionInfo const& info, + ITransientExpression const& expr, + AssertionReaction& reaction ) override; + void handleMessage + ( AssertionInfo const& info, + ResultWas::OfType resultType, + StringRef const& message, + AssertionReaction& reaction ) override; + void handleUnexpectedExceptionNotThrown + ( AssertionInfo const& info, + AssertionReaction& reaction ) override; + void handleUnexpectedInflightException + ( AssertionInfo const& info, + std::string const& message, + AssertionReaction& reaction ) override; + void handleIncomplete + ( AssertionInfo const& info ) override; + void handleNonExpr + ( AssertionInfo const &info, + ResultWas::OfType resultType, + AssertionReaction &reaction ) override; + + bool sectionStarted( SectionInfo const& sectionInfo, Counts& assertions ) override; + + void sectionEnded( SectionEndInfo const& endInfo ) override; + void sectionEndedEarly( SectionEndInfo const& endInfo ) override; + + auto acquireGeneratorTracker( SourceLineInfo const& lineInfo ) -> IGeneratorTracker& override; + + void benchmarkStarting( BenchmarkInfo const& info ) override; + void benchmarkEnded( BenchmarkStats const& stats ) override; + + void pushScopedMessage( MessageInfo const& message ) override; + void popScopedMessage( MessageInfo const& message ) override; + + std::string getCurrentTestName() const override; + + const AssertionResult* getLastResult() const override; + + void exceptionEarlyReported() override; + + void handleFatalErrorCondition( StringRef message ) override; + + bool lastAssertionPassed() override; + + void assertionPassed() override; + + public: + // !TBD We need to do this another way! + bool aborting() const final; + + private: + + void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ); + void invokeActiveTestCase(); + + void resetAssertionInfo(); + bool testForMissingAssertions( Counts& assertions ); + + void assertionEnded( AssertionResult const& result ); + void reportExpr + ( AssertionInfo const &info, + ResultWas::OfType resultType, + ITransientExpression const *expr, + bool negated ); + + void populateReaction( AssertionReaction& reaction ); + + private: + + void handleUnfinishedSections(); + + TestRunInfo m_runInfo; + IMutableContext& m_context; + TestCase const* m_activeTestCase = nullptr; + ITracker* m_testCaseTracker; + Option<AssertionResult> m_lastResult; + + IConfigPtr m_config; + Totals m_totals; + IStreamingReporterPtr m_reporter; + std::vector<MessageInfo> m_messages; + AssertionInfo m_lastAssertionInfo; + std::vector<SectionEndInfo> m_unfinishedSections; + std::vector<ITracker*> m_activeSections; + TrackerContext m_trackerContext; + bool m_lastAssertionPassed = false; + bool m_shouldReportUnexpected = true; + bool m_includeSuccessfulResults; + }; + +} // end namespace Catch + +// end catch_run_context.h +namespace Catch { + + namespace { + auto operator <<( std::ostream& os, ITransientExpression const& expr ) -> std::ostream& { + expr.streamReconstructedExpression( os ); + return os; + } + } + + LazyExpression::LazyExpression( bool isNegated ) + : m_isNegated( isNegated ) + {} + + LazyExpression::LazyExpression( LazyExpression const& other ) : m_isNegated( other.m_isNegated ) {} + + LazyExpression::operator bool() const { + return m_transientExpression != nullptr; + } + + auto operator << ( std::ostream& os, LazyExpression const& lazyExpr ) -> std::ostream& { + if( lazyExpr.m_isNegated ) + os << "!"; + + if( lazyExpr ) { + if( lazyExpr.m_isNegated && lazyExpr.m_transientExpression->isBinaryExpression() ) + os << "(" << *lazyExpr.m_transientExpression << ")"; + else + os << *lazyExpr.m_transientExpression; + } + else { + os << "{** error - unchecked empty expression requested **}"; + } + return os; + } + + AssertionHandler::AssertionHandler + ( StringRef const& macroName, + SourceLineInfo const& lineInfo, + StringRef capturedExpression, + ResultDisposition::Flags resultDisposition ) + : m_assertionInfo{ macroName, lineInfo, capturedExpression, resultDisposition }, + m_resultCapture( getResultCapture() ) + {} + + void AssertionHandler::handleExpr( ITransientExpression const& expr ) { + m_resultCapture.handleExpr( m_assertionInfo, expr, m_reaction ); + } + void AssertionHandler::handleMessage(ResultWas::OfType resultType, StringRef const& message) { + m_resultCapture.handleMessage( m_assertionInfo, resultType, message, m_reaction ); + } + + auto AssertionHandler::allowThrows() const -> bool { + return getCurrentContext().getConfig()->allowThrows(); + } + + void AssertionHandler::complete() { + setCompleted(); + if( m_reaction.shouldDebugBreak ) { + + // If you find your debugger stopping you here then go one level up on the + // call-stack for the code that caused it (typically a failed assertion) + + // (To go back to the test and change execution, jump over the throw, next) + CATCH_BREAK_INTO_DEBUGGER(); + } + if (m_reaction.shouldThrow) { +#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) + throw Catch::TestFailureException(); +#else + CATCH_ERROR( "Test failure requires aborting test!" ); +#endif + } + } + void AssertionHandler::setCompleted() { + m_completed = true; + } + + void AssertionHandler::handleUnexpectedInflightException() { + m_resultCapture.handleUnexpectedInflightException( m_assertionInfo, Catch::translateActiveException(), m_reaction ); + } + + void AssertionHandler::handleExceptionThrownAsExpected() { + m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); + } + void AssertionHandler::handleExceptionNotThrownAsExpected() { + m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); + } + + void AssertionHandler::handleUnexpectedExceptionNotThrown() { + m_resultCapture.handleUnexpectedExceptionNotThrown( m_assertionInfo, m_reaction ); + } + + void AssertionHandler::handleThrowingCallSkipped() { + m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); + } + + // This is the overload that takes a string and infers the Equals matcher from it + // The more general overload, that takes any string matcher, is in catch_capture_matchers.cpp + void handleExceptionMatchExpr( AssertionHandler& handler, std::string const& str, StringRef const& matcherString ) { + handleExceptionMatchExpr( handler, Matchers::Equals( str ), matcherString ); + } + +} // namespace Catch +// end catch_assertionhandler.cpp +// start catch_assertionresult.cpp + +namespace Catch { + AssertionResultData::AssertionResultData(ResultWas::OfType _resultType, LazyExpression const & _lazyExpression): + lazyExpression(_lazyExpression), + resultType(_resultType) {} + + std::string AssertionResultData::reconstructExpression() const { + + if( reconstructedExpression.empty() ) { + if( lazyExpression ) { + ReusableStringStream rss; + rss << lazyExpression; + reconstructedExpression = rss.str(); + } + } + return reconstructedExpression; + } + + AssertionResult::AssertionResult( AssertionInfo const& info, AssertionResultData const& data ) + : m_info( info ), + m_resultData( data ) + {} + + // Result was a success + bool AssertionResult::succeeded() const { + return Catch::isOk( m_resultData.resultType ); + } + + // Result was a success, or failure is suppressed + bool AssertionResult::isOk() const { + return Catch::isOk( m_resultData.resultType ) || shouldSuppressFailure( m_info.resultDisposition ); + } + + ResultWas::OfType AssertionResult::getResultType() const { + return m_resultData.resultType; + } + + bool AssertionResult::hasExpression() const { + return m_info.capturedExpression[0] != 0; + } + + bool AssertionResult::hasMessage() const { + return !m_resultData.message.empty(); + } + + std::string AssertionResult::getExpression() const { + if( isFalseTest( m_info.resultDisposition ) ) + return "!(" + m_info.capturedExpression + ")"; + else + return m_info.capturedExpression; + } + + std::string AssertionResult::getExpressionInMacro() const { + std::string expr; + if( m_info.macroName[0] == 0 ) + expr = m_info.capturedExpression; + else { + expr.reserve( m_info.macroName.size() + m_info.capturedExpression.size() + 4 ); + expr += m_info.macroName; + expr += "( "; + expr += m_info.capturedExpression; + expr += " )"; + } + return expr; + } + + bool AssertionResult::hasExpandedExpression() const { + return hasExpression() && getExpandedExpression() != getExpression(); + } + + std::string AssertionResult::getExpandedExpression() const { + std::string expr = m_resultData.reconstructExpression(); + return expr.empty() + ? getExpression() + : expr; + } + + std::string AssertionResult::getMessage() const { + return m_resultData.message; + } + SourceLineInfo AssertionResult::getSourceInfo() const { + return m_info.lineInfo; + } + + StringRef AssertionResult::getTestMacroName() const { + return m_info.macroName; + } + +} // end namespace Catch +// end catch_assertionresult.cpp +// start catch_benchmark.cpp + +namespace Catch { + + auto BenchmarkLooper::getResolution() -> uint64_t { + return getEstimatedClockResolution() * getCurrentContext().getConfig()->benchmarkResolutionMultiple(); + } + + void BenchmarkLooper::reportStart() { + getResultCapture().benchmarkStarting( { m_name } ); + } + auto BenchmarkLooper::needsMoreIterations() -> bool { + auto elapsed = m_timer.getElapsedNanoseconds(); + + // Exponentially increasing iterations until we're confident in our timer resolution + if( elapsed < m_resolution ) { + m_iterationsToRun *= 10; + return true; + } + + getResultCapture().benchmarkEnded( { { m_name }, m_count, elapsed } ); + return false; + } + +} // end namespace Catch +// end catch_benchmark.cpp +// start catch_capture_matchers.cpp + +namespace Catch { + + using StringMatcher = Matchers::Impl::MatcherBase<std::string>; + + // This is the general overload that takes a any string matcher + // There is another overload, in catch_assertionhandler.h/.cpp, that only takes a string and infers + // the Equals matcher (so the header does not mention matchers) + void handleExceptionMatchExpr( AssertionHandler& handler, StringMatcher const& matcher, StringRef const& matcherString ) { + std::string exceptionMessage = Catch::translateActiveException(); + MatchExpr<std::string, StringMatcher const&> expr( exceptionMessage, matcher, matcherString ); + handler.handleExpr( expr ); + } + +} // namespace Catch +// end catch_capture_matchers.cpp +// start catch_commandline.cpp + +// start catch_commandline.h + +// start catch_clara.h + +// Use Catch's value for console width (store Clara's off to the side, if present) +#ifdef CLARA_CONFIG_CONSOLE_WIDTH +#define CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH +#undef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH +#endif +#define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH CATCH_CONFIG_CONSOLE_WIDTH-1 + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wweak-vtables" +#pragma clang diagnostic ignored "-Wexit-time-destructors" +#pragma clang diagnostic ignored "-Wshadow" +#endif + +// start clara.hpp +// Copyright 2017 Two Blue Cubes Ltd. All rights reserved. +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See https://github.com/philsquared/Clara for more details + +// Clara v1.1.5 + + +#ifndef CATCH_CLARA_CONFIG_CONSOLE_WIDTH +#define CATCH_CLARA_CONFIG_CONSOLE_WIDTH 80 +#endif + +#ifndef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH +#define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH CATCH_CLARA_CONFIG_CONSOLE_WIDTH +#endif + +#ifndef CLARA_CONFIG_OPTIONAL_TYPE +#ifdef __has_include +#if __has_include(<optional>) && __cplusplus >= 201703L +#include <optional> +#define CLARA_CONFIG_OPTIONAL_TYPE std::optional +#endif +#endif +#endif + +// ----------- #included from clara_textflow.hpp ----------- + +// TextFlowCpp +// +// A single-header library for wrapping and laying out basic text, by Phil Nash +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// This project is hosted at https://github.com/philsquared/textflowcpp + + +#include <cassert> +#include <ostream> +#include <sstream> +#include <vector> + +#ifndef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH +#define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH 80 +#endif + +namespace Catch { +namespace clara { +namespace TextFlow { + +inline auto isWhitespace(char c) -> bool { + static std::string chars = " \t\n\r"; + return chars.find(c) != std::string::npos; +} +inline auto isBreakableBefore(char c) -> bool { + static std::string chars = "[({<|"; + return chars.find(c) != std::string::npos; +} +inline auto isBreakableAfter(char c) -> bool { + static std::string chars = "])}>.,:;*+-=&/\\"; + return chars.find(c) != std::string::npos; +} + +class Columns; + +class Column { + std::vector<std::string> m_strings; + size_t m_width = CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH; + size_t m_indent = 0; + size_t m_initialIndent = std::string::npos; + +public: + class iterator { + friend Column; + + Column const& m_column; + size_t m_stringIndex = 0; + size_t m_pos = 0; + + size_t m_len = 0; + size_t m_end = 0; + bool m_suffix = false; + + iterator(Column const& column, size_t stringIndex) + : m_column(column), + m_stringIndex(stringIndex) {} + + auto line() const -> std::string const& { return m_column.m_strings[m_stringIndex]; } + + auto isBoundary(size_t at) const -> bool { + assert(at > 0); + assert(at <= line().size()); + + return at == line().size() || + (isWhitespace(line()[at]) && !isWhitespace(line()[at - 1])) || + isBreakableBefore(line()[at]) || + isBreakableAfter(line()[at - 1]); + } + + void calcLength() { + assert(m_stringIndex < m_column.m_strings.size()); + + m_suffix = false; + auto width = m_column.m_width - indent(); + m_end = m_pos; + while (m_end < line().size() && line()[m_end] != '\n') + ++m_end; + + if (m_end < m_pos + width) { + m_len = m_end - m_pos; + } else { + size_t len = width; + while (len > 0 && !isBoundary(m_pos + len)) + --len; + while (len > 0 && isWhitespace(line()[m_pos + len - 1])) + --len; + + if (len > 0) { + m_len = len; + } else { + m_suffix = true; + m_len = width - 1; + } + } + } + + auto indent() const -> size_t { + auto initial = m_pos == 0 && m_stringIndex == 0 ? m_column.m_initialIndent : std::string::npos; + return initial == std::string::npos ? m_column.m_indent : initial; + } + + auto addIndentAndSuffix(std::string const &plain) const -> std::string { + return std::string(indent(), ' ') + (m_suffix ? plain + "-" : plain); + } + + public: + using difference_type = std::ptrdiff_t; + using value_type = std::string; + using pointer = value_type * ; + using reference = value_type & ; + using iterator_category = std::forward_iterator_tag; + + explicit iterator(Column const& column) : m_column(column) { + assert(m_column.m_width > m_column.m_indent); + assert(m_column.m_initialIndent == std::string::npos || m_column.m_width > m_column.m_initialIndent); + calcLength(); + if (m_len == 0) + m_stringIndex++; // Empty string + } + + auto operator *() const -> std::string { + assert(m_stringIndex < m_column.m_strings.size()); + assert(m_pos <= m_end); + return addIndentAndSuffix(line().substr(m_pos, m_len)); + } + + auto operator ++() -> iterator& { + m_pos += m_len; + if (m_pos < line().size() && line()[m_pos] == '\n') + m_pos += 1; + else + while (m_pos < line().size() && isWhitespace(line()[m_pos])) + ++m_pos; + + if (m_pos == line().size()) { + m_pos = 0; + ++m_stringIndex; + } + if (m_stringIndex < m_column.m_strings.size()) + calcLength(); + return *this; + } + auto operator ++(int) -> iterator { + iterator prev(*this); + operator++(); + return prev; + } + + auto operator ==(iterator const& other) const -> bool { + return + m_pos == other.m_pos && + m_stringIndex == other.m_stringIndex && + &m_column == &other.m_column; + } + auto operator !=(iterator const& other) const -> bool { + return !operator==(other); + } + }; + using const_iterator = iterator; + + explicit Column(std::string const& text) { m_strings.push_back(text); } + + auto width(size_t newWidth) -> Column& { + assert(newWidth > 0); + m_width = newWidth; + return *this; + } + auto indent(size_t newIndent) -> Column& { + m_indent = newIndent; + return *this; + } + auto initialIndent(size_t newIndent) -> Column& { + m_initialIndent = newIndent; + return *this; + } + + auto width() const -> size_t { return m_width; } + auto begin() const -> iterator { return iterator(*this); } + auto end() const -> iterator { return { *this, m_strings.size() }; } + + inline friend std::ostream& operator << (std::ostream& os, Column const& col) { + bool first = true; + for (auto line : col) { + if (first) + first = false; + else + os << "\n"; + os << line; + } + return os; + } + + auto operator + (Column const& other)->Columns; + + auto toString() const -> std::string { + std::ostringstream oss; + oss << *this; + return oss.str(); + } +}; + +class Spacer : public Column { + +public: + explicit Spacer(size_t spaceWidth) : Column("") { + width(spaceWidth); + } +}; + +class Columns { + std::vector<Column> m_columns; + +public: + + class iterator { + friend Columns; + struct EndTag {}; + + std::vector<Column> const& m_columns; + std::vector<Column::iterator> m_iterators; + size_t m_activeIterators; + + iterator(Columns const& columns, EndTag) + : m_columns(columns.m_columns), + m_activeIterators(0) { + m_iterators.reserve(m_columns.size()); + + for (auto const& col : m_columns) + m_iterators.push_back(col.end()); + } + + public: + using difference_type = std::ptrdiff_t; + using value_type = std::string; + using pointer = value_type * ; + using reference = value_type & ; + using iterator_category = std::forward_iterator_tag; + + explicit iterator(Columns const& columns) + : m_columns(columns.m_columns), + m_activeIterators(m_columns.size()) { + m_iterators.reserve(m_columns.size()); + + for (auto const& col : m_columns) + m_iterators.push_back(col.begin()); + } + + auto operator ==(iterator const& other) const -> bool { + return m_iterators == other.m_iterators; + } + auto operator !=(iterator const& other) const -> bool { + return m_iterators != other.m_iterators; + } + auto operator *() const -> std::string { + std::string row, padding; + + for (size_t i = 0; i < m_columns.size(); ++i) { + auto width = m_columns[i].width(); + if (m_iterators[i] != m_columns[i].end()) { + std::string col = *m_iterators[i]; + row += padding + col; + if (col.size() < width) + padding = std::string(width - col.size(), ' '); + else + padding = ""; + } else { + padding += std::string(width, ' '); + } + } + return row; + } + auto operator ++() -> iterator& { + for (size_t i = 0; i < m_columns.size(); ++i) { + if (m_iterators[i] != m_columns[i].end()) + ++m_iterators[i]; + } + return *this; + } + auto operator ++(int) -> iterator { + iterator prev(*this); + operator++(); + return prev; + } + }; + using const_iterator = iterator; + + auto begin() const -> iterator { return iterator(*this); } + auto end() const -> iterator { return { *this, iterator::EndTag() }; } + + auto operator += (Column const& col) -> Columns& { + m_columns.push_back(col); + return *this; + } + auto operator + (Column const& col) -> Columns { + Columns combined = *this; + combined += col; + return combined; + } + + inline friend std::ostream& operator << (std::ostream& os, Columns const& cols) { + + bool first = true; + for (auto line : cols) { + if (first) + first = false; + else + os << "\n"; + os << line; + } + return os; + } + + auto toString() const -> std::string { + std::ostringstream oss; + oss << *this; + return oss.str(); + } +}; + +inline auto Column::operator + (Column const& other) -> Columns { + Columns cols; + cols += *this; + cols += other; + return cols; +} +} + +} +} + +// ----------- end of #include from clara_textflow.hpp ----------- +// ........... back in clara.hpp + +#include <string> +#include <memory> +#include <set> +#include <algorithm> + +#if !defined(CATCH_PLATFORM_WINDOWS) && ( defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) ) +#define CATCH_PLATFORM_WINDOWS +#endif + +namespace Catch { namespace clara { +namespace detail { + + // Traits for extracting arg and return type of lambdas (for single argument lambdas) + template<typename L> + struct UnaryLambdaTraits : UnaryLambdaTraits<decltype( &L::operator() )> {}; + + template<typename ClassT, typename ReturnT, typename... Args> + struct UnaryLambdaTraits<ReturnT( ClassT::* )( Args... ) const> { + static const bool isValid = false; + }; + + template<typename ClassT, typename ReturnT, typename ArgT> + struct UnaryLambdaTraits<ReturnT( ClassT::* )( ArgT ) const> { + static const bool isValid = true; + using ArgType = typename std::remove_const<typename std::remove_reference<ArgT>::type>::type; + using ReturnType = ReturnT; + }; + + class TokenStream; + + // Transport for raw args (copied from main args, or supplied via init list for testing) + class Args { + friend TokenStream; + std::string m_exeName; + std::vector<std::string> m_args; + + public: + Args( int argc, char const* const* argv ) + : m_exeName(argv[0]), + m_args(argv + 1, argv + argc) {} + + Args( std::initializer_list<std::string> args ) + : m_exeName( *args.begin() ), + m_args( args.begin()+1, args.end() ) + {} + + auto exeName() const -> std::string { + return m_exeName; + } + }; + + // Wraps a token coming from a token stream. These may not directly correspond to strings as a single string + // may encode an option + its argument if the : or = form is used + enum class TokenType { + Option, Argument + }; + struct Token { + TokenType type; + std::string token; + }; + + inline auto isOptPrefix( char c ) -> bool { + return c == '-' +#ifdef CATCH_PLATFORM_WINDOWS + || c == '/' +#endif + ; + } + + // Abstracts iterators into args as a stream of tokens, with option arguments uniformly handled + class TokenStream { + using Iterator = std::vector<std::string>::const_iterator; + Iterator it; + Iterator itEnd; + std::vector<Token> m_tokenBuffer; + + void loadBuffer() { + m_tokenBuffer.resize( 0 ); + + // Skip any empty strings + while( it != itEnd && it->empty() ) + ++it; + + if( it != itEnd ) { + auto const &next = *it; + if( isOptPrefix( next[0] ) ) { + auto delimiterPos = next.find_first_of( " :=" ); + if( delimiterPos != std::string::npos ) { + m_tokenBuffer.push_back( { TokenType::Option, next.substr( 0, delimiterPos ) } ); + m_tokenBuffer.push_back( { TokenType::Argument, next.substr( delimiterPos + 1 ) } ); + } else { + if( next[1] != '-' && next.size() > 2 ) { + std::string opt = "- "; + for( size_t i = 1; i < next.size(); ++i ) { + opt[1] = next[i]; + m_tokenBuffer.push_back( { TokenType::Option, opt } ); + } + } else { + m_tokenBuffer.push_back( { TokenType::Option, next } ); + } + } + } else { + m_tokenBuffer.push_back( { TokenType::Argument, next } ); + } + } + } + + public: + explicit TokenStream( Args const &args ) : TokenStream( args.m_args.begin(), args.m_args.end() ) {} + + TokenStream( Iterator it, Iterator itEnd ) : it( it ), itEnd( itEnd ) { + loadBuffer(); + } + + explicit operator bool() const { + return !m_tokenBuffer.empty() || it != itEnd; + } + + auto count() const -> size_t { return m_tokenBuffer.size() + (itEnd - it); } + + auto operator*() const -> Token { + assert( !m_tokenBuffer.empty() ); + return m_tokenBuffer.front(); + } + + auto operator->() const -> Token const * { + assert( !m_tokenBuffer.empty() ); + return &m_tokenBuffer.front(); + } + + auto operator++() -> TokenStream & { + if( m_tokenBuffer.size() >= 2 ) { + m_tokenBuffer.erase( m_tokenBuffer.begin() ); + } else { + if( it != itEnd ) + ++it; + loadBuffer(); + } + return *this; + } + }; + + class ResultBase { + public: + enum Type { + Ok, LogicError, RuntimeError + }; + + protected: + ResultBase( Type type ) : m_type( type ) {} + virtual ~ResultBase() = default; + + virtual void enforceOk() const = 0; + + Type m_type; + }; + + template<typename T> + class ResultValueBase : public ResultBase { + public: + auto value() const -> T const & { + enforceOk(); + return m_value; + } + + protected: + ResultValueBase( Type type ) : ResultBase( type ) {} + + ResultValueBase( ResultValueBase const &other ) : ResultBase( other ) { + if( m_type == ResultBase::Ok ) + new( &m_value ) T( other.m_value ); + } + + ResultValueBase( Type, T const &value ) : ResultBase( Ok ) { + new( &m_value ) T( value ); + } + + auto operator=( ResultValueBase const &other ) -> ResultValueBase & { + if( m_type == ResultBase::Ok ) + m_value.~T(); + ResultBase::operator=(other); + if( m_type == ResultBase::Ok ) + new( &m_value ) T( other.m_value ); + return *this; + } + + ~ResultValueBase() override { + if( m_type == Ok ) + m_value.~T(); + } + + union { + T m_value; + }; + }; + + template<> + class ResultValueBase<void> : public ResultBase { + protected: + using ResultBase::ResultBase; + }; + + template<typename T = void> + class BasicResult : public ResultValueBase<T> { + public: + template<typename U> + explicit BasicResult( BasicResult<U> const &other ) + : ResultValueBase<T>( other.type() ), + m_errorMessage( other.errorMessage() ) + { + assert( type() != ResultBase::Ok ); + } + + template<typename U> + static auto ok( U const &value ) -> BasicResult { return { ResultBase::Ok, value }; } + static auto ok() -> BasicResult { return { ResultBase::Ok }; } + static auto logicError( std::string const &message ) -> BasicResult { return { ResultBase::LogicError, message }; } + static auto runtimeError( std::string const &message ) -> BasicResult { return { ResultBase::RuntimeError, message }; } + + explicit operator bool() const { return m_type == ResultBase::Ok; } + auto type() const -> ResultBase::Type { return m_type; } + auto errorMessage() const -> std::string { return m_errorMessage; } + + protected: + void enforceOk() const override { + + // Errors shouldn't reach this point, but if they do + // the actual error message will be in m_errorMessage + assert( m_type != ResultBase::LogicError ); + assert( m_type != ResultBase::RuntimeError ); + if( m_type != ResultBase::Ok ) + std::abort(); + } + + std::string m_errorMessage; // Only populated if resultType is an error + + BasicResult( ResultBase::Type type, std::string const &message ) + : ResultValueBase<T>(type), + m_errorMessage(message) + { + assert( m_type != ResultBase::Ok ); + } + + using ResultValueBase<T>::ResultValueBase; + using ResultBase::m_type; + }; + + enum class ParseResultType { + Matched, NoMatch, ShortCircuitAll, ShortCircuitSame + }; + + class ParseState { + public: + + ParseState( ParseResultType type, TokenStream const &remainingTokens ) + : m_type(type), + m_remainingTokens( remainingTokens ) + {} + + auto type() const -> ParseResultType { return m_type; } + auto remainingTokens() const -> TokenStream { return m_remainingTokens; } + + private: + ParseResultType m_type; + TokenStream m_remainingTokens; + }; + + using Result = BasicResult<void>; + using ParserResult = BasicResult<ParseResultType>; + using InternalParseResult = BasicResult<ParseState>; + + struct HelpColumns { + std::string left; + std::string right; + }; + + template<typename T> + inline auto convertInto( std::string const &source, T& target ) -> ParserResult { + std::stringstream ss; + ss << source; + ss >> target; + if( ss.fail() ) + return ParserResult::runtimeError( "Unable to convert '" + source + "' to destination type" ); + else + return ParserResult::ok( ParseResultType::Matched ); + } + inline auto convertInto( std::string const &source, std::string& target ) -> ParserResult { + target = source; + return ParserResult::ok( ParseResultType::Matched ); + } + inline auto convertInto( std::string const &source, bool &target ) -> ParserResult { + std::string srcLC = source; + std::transform( srcLC.begin(), srcLC.end(), srcLC.begin(), []( char c ) { return static_cast<char>( ::tolower(c) ); } ); + if (srcLC == "y" || srcLC == "1" || srcLC == "true" || srcLC == "yes" || srcLC == "on") + target = true; + else if (srcLC == "n" || srcLC == "0" || srcLC == "false" || srcLC == "no" || srcLC == "off") + target = false; + else + return ParserResult::runtimeError( "Expected a boolean value but did not recognise: '" + source + "'" ); + return ParserResult::ok( ParseResultType::Matched ); + } +#ifdef CLARA_CONFIG_OPTIONAL_TYPE + template<typename T> + inline auto convertInto( std::string const &source, CLARA_CONFIG_OPTIONAL_TYPE<T>& target ) -> ParserResult { + T temp; + auto result = convertInto( source, temp ); + if( result ) + target = std::move(temp); + return result; + } +#endif // CLARA_CONFIG_OPTIONAL_TYPE + + struct NonCopyable { + NonCopyable() = default; + NonCopyable( NonCopyable const & ) = delete; + NonCopyable( NonCopyable && ) = delete; + NonCopyable &operator=( NonCopyable const & ) = delete; + NonCopyable &operator=( NonCopyable && ) = delete; + }; + + struct BoundRef : NonCopyable { + virtual ~BoundRef() = default; + virtual auto isContainer() const -> bool { return false; } + virtual auto isFlag() const -> bool { return false; } + }; + struct BoundValueRefBase : BoundRef { + virtual auto setValue( std::string const &arg ) -> ParserResult = 0; + }; + struct BoundFlagRefBase : BoundRef { + virtual auto setFlag( bool flag ) -> ParserResult = 0; + virtual auto isFlag() const -> bool { return true; } + }; + + template<typename T> + struct BoundValueRef : BoundValueRefBase { + T &m_ref; + + explicit BoundValueRef( T &ref ) : m_ref( ref ) {} + + auto setValue( std::string const &arg ) -> ParserResult override { + return convertInto( arg, m_ref ); + } + }; + + template<typename T> + struct BoundValueRef<std::vector<T>> : BoundValueRefBase { + std::vector<T> &m_ref; + + explicit BoundValueRef( std::vector<T> &ref ) : m_ref( ref ) {} + + auto isContainer() const -> bool override { return true; } + + auto setValue( std::string const &arg ) -> ParserResult override { + T temp; + auto result = convertInto( arg, temp ); + if( result ) + m_ref.push_back( temp ); + return result; + } + }; + + struct BoundFlagRef : BoundFlagRefBase { + bool &m_ref; + + explicit BoundFlagRef( bool &ref ) : m_ref( ref ) {} + + auto setFlag( bool flag ) -> ParserResult override { + m_ref = flag; + return ParserResult::ok( ParseResultType::Matched ); + } + }; + + template<typename ReturnType> + struct LambdaInvoker { + static_assert( std::is_same<ReturnType, ParserResult>::value, "Lambda must return void or clara::ParserResult" ); + + template<typename L, typename ArgType> + static auto invoke( L const &lambda, ArgType const &arg ) -> ParserResult { + return lambda( arg ); + } + }; + + template<> + struct LambdaInvoker<void> { + template<typename L, typename ArgType> + static auto invoke( L const &lambda, ArgType const &arg ) -> ParserResult { + lambda( arg ); + return ParserResult::ok( ParseResultType::Matched ); + } + }; + + template<typename ArgType, typename L> + inline auto invokeLambda( L const &lambda, std::string const &arg ) -> ParserResult { + ArgType temp{}; + auto result = convertInto( arg, temp ); + return !result + ? result + : LambdaInvoker<typename UnaryLambdaTraits<L>::ReturnType>::invoke( lambda, temp ); + } + + template<typename L> + struct BoundLambda : BoundValueRefBase { + L m_lambda; + + static_assert( UnaryLambdaTraits<L>::isValid, "Supplied lambda must take exactly one argument" ); + explicit BoundLambda( L const &lambda ) : m_lambda( lambda ) {} + + auto setValue( std::string const &arg ) -> ParserResult override { + return invokeLambda<typename UnaryLambdaTraits<L>::ArgType>( m_lambda, arg ); + } + }; + + template<typename L> + struct BoundFlagLambda : BoundFlagRefBase { + L m_lambda; + + static_assert( UnaryLambdaTraits<L>::isValid, "Supplied lambda must take exactly one argument" ); + static_assert( std::is_same<typename UnaryLambdaTraits<L>::ArgType, bool>::value, "flags must be boolean" ); + + explicit BoundFlagLambda( L const &lambda ) : m_lambda( lambda ) {} + + auto setFlag( bool flag ) -> ParserResult override { + return LambdaInvoker<typename UnaryLambdaTraits<L>::ReturnType>::invoke( m_lambda, flag ); + } + }; + + enum class Optionality { Optional, Required }; + + struct Parser; + + class ParserBase { + public: + virtual ~ParserBase() = default; + virtual auto validate() const -> Result { return Result::ok(); } + virtual auto parse( std::string const& exeName, TokenStream const &tokens) const -> InternalParseResult = 0; + virtual auto cardinality() const -> size_t { return 1; } + + auto parse( Args const &args ) const -> InternalParseResult { + return parse( args.exeName(), TokenStream( args ) ); + } + }; + + template<typename DerivedT> + class ComposableParserImpl : public ParserBase { + public: + template<typename T> + auto operator|( T const &other ) const -> Parser; + + template<typename T> + auto operator+( T const &other ) const -> Parser; + }; + + // Common code and state for Args and Opts + template<typename DerivedT> + class ParserRefImpl : public ComposableParserImpl<DerivedT> { + protected: + Optionality m_optionality = Optionality::Optional; + std::shared_ptr<BoundRef> m_ref; + std::string m_hint; + std::string m_description; + + explicit ParserRefImpl( std::shared_ptr<BoundRef> const &ref ) : m_ref( ref ) {} + + public: + template<typename T> + ParserRefImpl( T &ref, std::string const &hint ) + : m_ref( std::make_shared<BoundValueRef<T>>( ref ) ), + m_hint( hint ) + {} + + template<typename LambdaT> + ParserRefImpl( LambdaT const &ref, std::string const &hint ) + : m_ref( std::make_shared<BoundLambda<LambdaT>>( ref ) ), + m_hint(hint) + {} + + auto operator()( std::string const &description ) -> DerivedT & { + m_description = description; + return static_cast<DerivedT &>( *this ); + } + + auto optional() -> DerivedT & { + m_optionality = Optionality::Optional; + return static_cast<DerivedT &>( *this ); + }; + + auto required() -> DerivedT & { + m_optionality = Optionality::Required; + return static_cast<DerivedT &>( *this ); + }; + + auto isOptional() const -> bool { + return m_optionality == Optionality::Optional; + } + + auto cardinality() const -> size_t override { + if( m_ref->isContainer() ) + return 0; + else + return 1; + } + + auto hint() const -> std::string { return m_hint; } + }; + + class ExeName : public ComposableParserImpl<ExeName> { + std::shared_ptr<std::string> m_name; + std::shared_ptr<BoundValueRefBase> m_ref; + + template<typename LambdaT> + static auto makeRef(LambdaT const &lambda) -> std::shared_ptr<BoundValueRefBase> { + return std::make_shared<BoundLambda<LambdaT>>( lambda) ; + } + + public: + ExeName() : m_name( std::make_shared<std::string>( "<executable>" ) ) {} + + explicit ExeName( std::string &ref ) : ExeName() { + m_ref = std::make_shared<BoundValueRef<std::string>>( ref ); + } + + template<typename LambdaT> + explicit ExeName( LambdaT const& lambda ) : ExeName() { + m_ref = std::make_shared<BoundLambda<LambdaT>>( lambda ); + } + + // The exe name is not parsed out of the normal tokens, but is handled specially + auto parse( std::string const&, TokenStream const &tokens ) const -> InternalParseResult override { + return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, tokens ) ); + } + + auto name() const -> std::string { return *m_name; } + auto set( std::string const& newName ) -> ParserResult { + + auto lastSlash = newName.find_last_of( "\\/" ); + auto filename = ( lastSlash == std::string::npos ) + ? newName + : newName.substr( lastSlash+1 ); + + *m_name = filename; + if( m_ref ) + return m_ref->setValue( filename ); + else + return ParserResult::ok( ParseResultType::Matched ); + } + }; + + class Arg : public ParserRefImpl<Arg> { + public: + using ParserRefImpl::ParserRefImpl; + + auto parse( std::string const &, TokenStream const &tokens ) const -> InternalParseResult override { + auto validationResult = validate(); + if( !validationResult ) + return InternalParseResult( validationResult ); + + auto remainingTokens = tokens; + auto const &token = *remainingTokens; + if( token.type != TokenType::Argument ) + return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, remainingTokens ) ); + + assert( !m_ref->isFlag() ); + auto valueRef = static_cast<detail::BoundValueRefBase*>( m_ref.get() ); + + auto result = valueRef->setValue( remainingTokens->token ); + if( !result ) + return InternalParseResult( result ); + else + return InternalParseResult::ok( ParseState( ParseResultType::Matched, ++remainingTokens ) ); + } + }; + + inline auto normaliseOpt( std::string const &optName ) -> std::string { +#ifdef CATCH_PLATFORM_WINDOWS + if( optName[0] == '/' ) + return "-" + optName.substr( 1 ); + else +#endif + return optName; + } + + class Opt : public ParserRefImpl<Opt> { + protected: + std::vector<std::string> m_optNames; + + public: + template<typename LambdaT> + explicit Opt( LambdaT const &ref ) : ParserRefImpl( std::make_shared<BoundFlagLambda<LambdaT>>( ref ) ) {} + + explicit Opt( bool &ref ) : ParserRefImpl( std::make_shared<BoundFlagRef>( ref ) ) {} + + template<typename LambdaT> + Opt( LambdaT const &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {} + + template<typename T> + Opt( T &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {} + + auto operator[]( std::string const &optName ) -> Opt & { + m_optNames.push_back( optName ); + return *this; + } + + auto getHelpColumns() const -> std::vector<HelpColumns> { + std::ostringstream oss; + bool first = true; + for( auto const &opt : m_optNames ) { + if (first) + first = false; + else + oss << ", "; + oss << opt; + } + if( !m_hint.empty() ) + oss << " <" << m_hint << ">"; + return { { oss.str(), m_description } }; + } + + auto isMatch( std::string const &optToken ) const -> bool { + auto normalisedToken = normaliseOpt( optToken ); + for( auto const &name : m_optNames ) { + if( normaliseOpt( name ) == normalisedToken ) + return true; + } + return false; + } + + using ParserBase::parse; + + auto parse( std::string const&, TokenStream const &tokens ) const -> InternalParseResult override { + auto validationResult = validate(); + if( !validationResult ) + return InternalParseResult( validationResult ); + + auto remainingTokens = tokens; + if( remainingTokens && remainingTokens->type == TokenType::Option ) { + auto const &token = *remainingTokens; + if( isMatch(token.token ) ) { + if( m_ref->isFlag() ) { + auto flagRef = static_cast<detail::BoundFlagRefBase*>( m_ref.get() ); + auto result = flagRef->setFlag( true ); + if( !result ) + return InternalParseResult( result ); + if( result.value() == ParseResultType::ShortCircuitAll ) + return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) ); + } else { + auto valueRef = static_cast<detail::BoundValueRefBase*>( m_ref.get() ); + ++remainingTokens; + if( !remainingTokens ) + return InternalParseResult::runtimeError( "Expected argument following " + token.token ); + auto const &argToken = *remainingTokens; + if( argToken.type != TokenType::Argument ) + return InternalParseResult::runtimeError( "Expected argument following " + token.token ); + auto result = valueRef->setValue( argToken.token ); + if( !result ) + return InternalParseResult( result ); + if( result.value() == ParseResultType::ShortCircuitAll ) + return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) ); + } + return InternalParseResult::ok( ParseState( ParseResultType::Matched, ++remainingTokens ) ); + } + } + return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, remainingTokens ) ); + } + + auto validate() const -> Result override { + if( m_optNames.empty() ) + return Result::logicError( "No options supplied to Opt" ); + for( auto const &name : m_optNames ) { + if( name.empty() ) + return Result::logicError( "Option name cannot be empty" ); +#ifdef CATCH_PLATFORM_WINDOWS + if( name[0] != '-' && name[0] != '/' ) + return Result::logicError( "Option name must begin with '-' or '/'" ); +#else + if( name[0] != '-' ) + return Result::logicError( "Option name must begin with '-'" ); +#endif + } + return ParserRefImpl::validate(); + } + }; + + struct Help : Opt { + Help( bool &showHelpFlag ) + : Opt([&]( bool flag ) { + showHelpFlag = flag; + return ParserResult::ok( ParseResultType::ShortCircuitAll ); + }) + { + static_cast<Opt &>( *this ) + ("display usage information") + ["-?"]["-h"]["--help"] + .optional(); + } + }; + + struct Parser : ParserBase { + + mutable ExeName m_exeName; + std::vector<Opt> m_options; + std::vector<Arg> m_args; + + auto operator|=( ExeName const &exeName ) -> Parser & { + m_exeName = exeName; + return *this; + } + + auto operator|=( Arg const &arg ) -> Parser & { + m_args.push_back(arg); + return *this; + } + + auto operator|=( Opt const &opt ) -> Parser & { + m_options.push_back(opt); + return *this; + } + + auto operator|=( Parser const &other ) -> Parser & { + m_options.insert(m_options.end(), other.m_options.begin(), other.m_options.end()); + m_args.insert(m_args.end(), other.m_args.begin(), other.m_args.end()); + return *this; + } + + template<typename T> + auto operator|( T const &other ) const -> Parser { + return Parser( *this ) |= other; + } + + // Forward deprecated interface with '+' instead of '|' + template<typename T> + auto operator+=( T const &other ) -> Parser & { return operator|=( other ); } + template<typename T> + auto operator+( T const &other ) const -> Parser { return operator|( other ); } + + auto getHelpColumns() const -> std::vector<HelpColumns> { + std::vector<HelpColumns> cols; + for (auto const &o : m_options) { + auto childCols = o.getHelpColumns(); + cols.insert( cols.end(), childCols.begin(), childCols.end() ); + } + return cols; + } + + void writeToStream( std::ostream &os ) const { + if (!m_exeName.name().empty()) { + os << "usage:\n" << " " << m_exeName.name() << " "; + bool required = true, first = true; + for( auto const &arg : m_args ) { + if (first) + first = false; + else + os << " "; + if( arg.isOptional() && required ) { + os << "["; + required = false; + } + os << "<" << arg.hint() << ">"; + if( arg.cardinality() == 0 ) + os << " ... "; + } + if( !required ) + os << "]"; + if( !m_options.empty() ) + os << " options"; + os << "\n\nwhere options are:" << std::endl; + } + + auto rows = getHelpColumns(); + size_t consoleWidth = CATCH_CLARA_CONFIG_CONSOLE_WIDTH; + size_t optWidth = 0; + for( auto const &cols : rows ) + optWidth = (std::max)(optWidth, cols.left.size() + 2); + + optWidth = (std::min)(optWidth, consoleWidth/2); + + for( auto const &cols : rows ) { + auto row = + TextFlow::Column( cols.left ).width( optWidth ).indent( 2 ) + + TextFlow::Spacer(4) + + TextFlow::Column( cols.right ).width( consoleWidth - 7 - optWidth ); + os << row << std::endl; + } + } + + friend auto operator<<( std::ostream &os, Parser const &parser ) -> std::ostream& { + parser.writeToStream( os ); + return os; + } + + auto validate() const -> Result override { + for( auto const &opt : m_options ) { + auto result = opt.validate(); + if( !result ) + return result; + } + for( auto const &arg : m_args ) { + auto result = arg.validate(); + if( !result ) + return result; + } + return Result::ok(); + } + + using ParserBase::parse; + + auto parse( std::string const& exeName, TokenStream const &tokens ) const -> InternalParseResult override { + + struct ParserInfo { + ParserBase const* parser = nullptr; + size_t count = 0; + }; + const size_t totalParsers = m_options.size() + m_args.size(); + assert( totalParsers < 512 ); + // ParserInfo parseInfos[totalParsers]; // <-- this is what we really want to do + ParserInfo parseInfos[512]; + + { + size_t i = 0; + for (auto const &opt : m_options) parseInfos[i++].parser = &opt; + for (auto const &arg : m_args) parseInfos[i++].parser = &arg; + } + + m_exeName.set( exeName ); + + auto result = InternalParseResult::ok( ParseState( ParseResultType::NoMatch, tokens ) ); + while( result.value().remainingTokens() ) { + bool tokenParsed = false; + + for( size_t i = 0; i < totalParsers; ++i ) { + auto& parseInfo = parseInfos[i]; + if( parseInfo.parser->cardinality() == 0 || parseInfo.count < parseInfo.parser->cardinality() ) { + result = parseInfo.parser->parse(exeName, result.value().remainingTokens()); + if (!result) + return result; + if (result.value().type() != ParseResultType::NoMatch) { + tokenParsed = true; + ++parseInfo.count; + break; + } + } + } + + if( result.value().type() == ParseResultType::ShortCircuitAll ) + return result; + if( !tokenParsed ) + return InternalParseResult::runtimeError( "Unrecognised token: " + result.value().remainingTokens()->token ); + } + // !TBD Check missing required options + return result; + } + }; + + template<typename DerivedT> + template<typename T> + auto ComposableParserImpl<DerivedT>::operator|( T const &other ) const -> Parser { + return Parser() | static_cast<DerivedT const &>( *this ) | other; + } +} // namespace detail + +// A Combined parser +using detail::Parser; + +// A parser for options +using detail::Opt; + +// A parser for arguments +using detail::Arg; + +// Wrapper for argc, argv from main() +using detail::Args; + +// Specifies the name of the executable +using detail::ExeName; + +// Convenience wrapper for option parser that specifies the help option +using detail::Help; + +// enum of result types from a parse +using detail::ParseResultType; + +// Result type for parser operation +using detail::ParserResult; + +}} // namespace Catch::clara + +// end clara.hpp +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// Restore Clara's value for console width, if present +#ifdef CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH +#define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH +#undef CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH +#endif + +// end catch_clara.h +namespace Catch { + + clara::Parser makeCommandLineParser( ConfigData& config ); + +} // end namespace Catch + +// end catch_commandline.h +#include <fstream> +#include <ctime> + +namespace Catch { + + clara::Parser makeCommandLineParser( ConfigData& config ) { + + using namespace clara; + + auto const setWarning = [&]( std::string const& warning ) { + auto warningSet = [&]() { + if( warning == "NoAssertions" ) + return WarnAbout::NoAssertions; + + if ( warning == "NoTests" ) + return WarnAbout::NoTests; + + return WarnAbout::Nothing; + }(); + + if (warningSet == WarnAbout::Nothing) + return ParserResult::runtimeError( "Unrecognised warning: '" + warning + "'" ); + config.warnings = static_cast<WarnAbout::What>( config.warnings | warningSet ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const loadTestNamesFromFile = [&]( std::string const& filename ) { + std::ifstream f( filename.c_str() ); + if( !f.is_open() ) + return ParserResult::runtimeError( "Unable to load input file: '" + filename + "'" ); + + std::string line; + while( std::getline( f, line ) ) { + line = trim(line); + if( !line.empty() && !startsWith( line, '#' ) ) { + if( !startsWith( line, '"' ) ) + line = '"' + line + '"'; + config.testsOrTags.push_back( line + ',' ); + } + } + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setTestOrder = [&]( std::string const& order ) { + if( startsWith( "declared", order ) ) + config.runOrder = RunTests::InDeclarationOrder; + else if( startsWith( "lexical", order ) ) + config.runOrder = RunTests::InLexicographicalOrder; + else if( startsWith( "random", order ) ) + config.runOrder = RunTests::InRandomOrder; + else + return clara::ParserResult::runtimeError( "Unrecognised ordering: '" + order + "'" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setRngSeed = [&]( std::string const& seed ) { + if( seed != "time" ) + return clara::detail::convertInto( seed, config.rngSeed ); + config.rngSeed = static_cast<unsigned int>( std::time(nullptr) ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setColourUsage = [&]( std::string const& useColour ) { + auto mode = toLower( useColour ); + + if( mode == "yes" ) + config.useColour = UseColour::Yes; + else if( mode == "no" ) + config.useColour = UseColour::No; + else if( mode == "auto" ) + config.useColour = UseColour::Auto; + else + return ParserResult::runtimeError( "colour mode must be one of: auto, yes or no. '" + useColour + "' not recognised" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setWaitForKeypress = [&]( std::string const& keypress ) { + auto keypressLc = toLower( keypress ); + if( keypressLc == "start" ) + config.waitForKeypress = WaitForKeypress::BeforeStart; + else if( keypressLc == "exit" ) + config.waitForKeypress = WaitForKeypress::BeforeExit; + else if( keypressLc == "both" ) + config.waitForKeypress = WaitForKeypress::BeforeStartAndExit; + else + return ParserResult::runtimeError( "keypress argument must be one of: start, exit or both. '" + keypress + "' not recognised" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setVerbosity = [&]( std::string const& verbosity ) { + auto lcVerbosity = toLower( verbosity ); + if( lcVerbosity == "quiet" ) + config.verbosity = Verbosity::Quiet; + else if( lcVerbosity == "normal" ) + config.verbosity = Verbosity::Normal; + else if( lcVerbosity == "high" ) + config.verbosity = Verbosity::High; + else + return ParserResult::runtimeError( "Unrecognised verbosity, '" + verbosity + "'" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setReporter = [&]( std::string const& reporter ) { + IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories(); + + auto lcReporter = toLower( reporter ); + auto result = factories.find( lcReporter ); + + if( factories.end() != result ) + config.reporterName = lcReporter; + else + return ParserResult::runtimeError( "Unrecognized reporter, '" + reporter + "'. Check available with --list-reporters" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + + auto cli + = ExeName( config.processName ) + | Help( config.showHelp ) + | Opt( config.listTests ) + ["-l"]["--list-tests"] + ( "list all/matching test cases" ) + | Opt( config.listTags ) + ["-t"]["--list-tags"] + ( "list all/matching tags" ) + | Opt( config.showSuccessfulTests ) + ["-s"]["--success"] + ( "include successful tests in output" ) + | Opt( config.shouldDebugBreak ) + ["-b"]["--break"] + ( "break into debugger on failure" ) + | Opt( config.noThrow ) + ["-e"]["--nothrow"] + ( "skip exception tests" ) + | Opt( config.showInvisibles ) + ["-i"]["--invisibles"] + ( "show invisibles (tabs, newlines)" ) + | Opt( config.outputFilename, "filename" ) + ["-o"]["--out"] + ( "output filename" ) + | Opt( setReporter, "name" ) + ["-r"]["--reporter"] + ( "reporter to use (defaults to console)" ) + | Opt( config.name, "name" ) + ["-n"]["--name"] + ( "suite name" ) + | Opt( [&]( bool ){ config.abortAfter = 1; } ) + ["-a"]["--abort"] + ( "abort at first failure" ) + | Opt( [&]( int x ){ config.abortAfter = x; }, "no. failures" ) + ["-x"]["--abortx"] + ( "abort after x failures" ) + | Opt( setWarning, "warning name" ) + ["-w"]["--warn"] + ( "enable warnings" ) + | Opt( [&]( bool flag ) { config.showDurations = flag ? ShowDurations::Always : ShowDurations::Never; }, "yes|no" ) + ["-d"]["--durations"] + ( "show test durations" ) + | Opt( loadTestNamesFromFile, "filename" ) + ["-f"]["--input-file"] + ( "load test names to run from a file" ) + | Opt( config.filenamesAsTags ) + ["-#"]["--filenames-as-tags"] + ( "adds a tag for the filename" ) + | Opt( config.sectionsToRun, "section name" ) + ["-c"]["--section"] + ( "specify section to run" ) + | Opt( setVerbosity, "quiet|normal|high" ) + ["-v"]["--verbosity"] + ( "set output verbosity" ) + | Opt( config.listTestNamesOnly ) + ["--list-test-names-only"] + ( "list all/matching test cases names only" ) + | Opt( config.listReporters ) + ["--list-reporters"] + ( "list all reporters" ) + | Opt( setTestOrder, "decl|lex|rand" ) + ["--order"] + ( "test case order (defaults to decl)" ) + | Opt( setRngSeed, "'time'|number" ) + ["--rng-seed"] + ( "set a specific seed for random numbers" ) + | Opt( setColourUsage, "yes|no" ) + ["--use-colour"] + ( "should output be colourised" ) + | Opt( config.libIdentify ) + ["--libidentify"] + ( "report name and version according to libidentify standard" ) + | Opt( setWaitForKeypress, "start|exit|both" ) + ["--wait-for-keypress"] + ( "waits for a keypress before exiting" ) + | Opt( config.benchmarkResolutionMultiple, "multiplier" ) + ["--benchmark-resolution-multiple"] + ( "multiple of clock resolution to run benchmarks" ) + + | Arg( config.testsOrTags, "test name|pattern|tags" ) + ( "which test or tests to use" ); + + return cli; + } + +} // end namespace Catch +// end catch_commandline.cpp +// start catch_common.cpp + +#include <cstring> +#include <ostream> + +namespace Catch { + + bool SourceLineInfo::empty() const noexcept { + return file[0] == '\0'; + } + bool SourceLineInfo::operator == ( SourceLineInfo const& other ) const noexcept { + return line == other.line && (file == other.file || std::strcmp(file, other.file) == 0); + } + bool SourceLineInfo::operator < ( SourceLineInfo const& other ) const noexcept { + // We can assume that the same file will usually have the same pointer. + // Thus, if the pointers are the same, there is no point in calling the strcmp + return line < other.line || ( line == other.line && file != other.file && (std::strcmp(file, other.file) < 0)); + } + + std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) { +#ifndef __GNUG__ + os << info.file << '(' << info.line << ')'; +#else + os << info.file << ':' << info.line; +#endif + return os; + } + + std::string StreamEndStop::operator+() const { + return std::string(); + } + + NonCopyable::NonCopyable() = default; + NonCopyable::~NonCopyable() = default; + +} +// end catch_common.cpp +// start catch_config.cpp + +namespace Catch { + + Config::Config( ConfigData const& data ) + : m_data( data ), + m_stream( openStream() ) + { + TestSpecParser parser(ITagAliasRegistry::get()); + if (data.testsOrTags.empty()) { + parser.parse("~[.]"); // All not hidden tests + } + else { + m_hasTestFilters = true; + for( auto const& testOrTags : data.testsOrTags ) + parser.parse( testOrTags ); + } + m_testSpec = parser.testSpec(); + } + + std::string const& Config::getFilename() const { + return m_data.outputFilename ; + } + + bool Config::listTests() const { return m_data.listTests; } + bool Config::listTestNamesOnly() const { return m_data.listTestNamesOnly; } + bool Config::listTags() const { return m_data.listTags; } + bool Config::listReporters() const { return m_data.listReporters; } + + std::string Config::getProcessName() const { return m_data.processName; } + std::string const& Config::getReporterName() const { return m_data.reporterName; } + + std::vector<std::string> const& Config::getTestsOrTags() const { return m_data.testsOrTags; } + std::vector<std::string> const& Config::getSectionsToRun() const { return m_data.sectionsToRun; } + + TestSpec const& Config::testSpec() const { return m_testSpec; } + bool Config::hasTestFilters() const { return m_hasTestFilters; } + + bool Config::showHelp() const { return m_data.showHelp; } + + // IConfig interface + bool Config::allowThrows() const { return !m_data.noThrow; } + std::ostream& Config::stream() const { return m_stream->stream(); } + std::string Config::name() const { return m_data.name.empty() ? m_data.processName : m_data.name; } + bool Config::includeSuccessfulResults() const { return m_data.showSuccessfulTests; } + bool Config::warnAboutMissingAssertions() const { return !!(m_data.warnings & WarnAbout::NoAssertions); } + bool Config::warnAboutNoTests() const { return !!(m_data.warnings & WarnAbout::NoTests); } + ShowDurations::OrNot Config::showDurations() const { return m_data.showDurations; } + RunTests::InWhatOrder Config::runOrder() const { return m_data.runOrder; } + unsigned int Config::rngSeed() const { return m_data.rngSeed; } + int Config::benchmarkResolutionMultiple() const { return m_data.benchmarkResolutionMultiple; } + UseColour::YesOrNo Config::useColour() const { return m_data.useColour; } + bool Config::shouldDebugBreak() const { return m_data.shouldDebugBreak; } + int Config::abortAfter() const { return m_data.abortAfter; } + bool Config::showInvisibles() const { return m_data.showInvisibles; } + Verbosity Config::verbosity() const { return m_data.verbosity; } + + IStream const* Config::openStream() { + return Catch::makeStream(m_data.outputFilename); + } + +} // end namespace Catch +// end catch_config.cpp +// start catch_console_colour.cpp + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wexit-time-destructors" +#endif + +// start catch_errno_guard.h + +namespace Catch { + + class ErrnoGuard { + public: + ErrnoGuard(); + ~ErrnoGuard(); + private: + int m_oldErrno; + }; + +} + +// end catch_errno_guard.h +#include <sstream> + +namespace Catch { + namespace { + + struct IColourImpl { + virtual ~IColourImpl() = default; + virtual void use( Colour::Code _colourCode ) = 0; + }; + + struct NoColourImpl : IColourImpl { + void use( Colour::Code ) {} + + static IColourImpl* instance() { + static NoColourImpl s_instance; + return &s_instance; + } + }; + + } // anon namespace +} // namespace Catch + +#if !defined( CATCH_CONFIG_COLOUR_NONE ) && !defined( CATCH_CONFIG_COLOUR_WINDOWS ) && !defined( CATCH_CONFIG_COLOUR_ANSI ) +# ifdef CATCH_PLATFORM_WINDOWS +# define CATCH_CONFIG_COLOUR_WINDOWS +# else +# define CATCH_CONFIG_COLOUR_ANSI +# endif +#endif + +#if defined ( CATCH_CONFIG_COLOUR_WINDOWS ) ///////////////////////////////////////// + +namespace Catch { +namespace { + + class Win32ColourImpl : public IColourImpl { + public: + Win32ColourImpl() : stdoutHandle( GetStdHandle(STD_OUTPUT_HANDLE) ) + { + CONSOLE_SCREEN_BUFFER_INFO csbiInfo; + GetConsoleScreenBufferInfo( stdoutHandle, &csbiInfo ); + originalForegroundAttributes = csbiInfo.wAttributes & ~( BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY ); + originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY ); + } + + virtual void use( Colour::Code _colourCode ) override { + switch( _colourCode ) { + case Colour::None: return setTextAttribute( originalForegroundAttributes ); + case Colour::White: return setTextAttribute( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ); + case Colour::Red: return setTextAttribute( FOREGROUND_RED ); + case Colour::Green: return setTextAttribute( FOREGROUND_GREEN ); + case Colour::Blue: return setTextAttribute( FOREGROUND_BLUE ); + case Colour::Cyan: return setTextAttribute( FOREGROUND_BLUE | FOREGROUND_GREEN ); + case Colour::Yellow: return setTextAttribute( FOREGROUND_RED | FOREGROUND_GREEN ); + case Colour::Grey: return setTextAttribute( 0 ); + + case Colour::LightGrey: return setTextAttribute( FOREGROUND_INTENSITY ); + case Colour::BrightRed: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_RED ); + case Colour::BrightGreen: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN ); + case Colour::BrightWhite: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ); + case Colour::BrightYellow: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN ); + + case Colour::Bright: CATCH_INTERNAL_ERROR( "not a colour" ); + + default: + CATCH_ERROR( "Unknown colour requested" ); + } + } + + private: + void setTextAttribute( WORD _textAttribute ) { + SetConsoleTextAttribute( stdoutHandle, _textAttribute | originalBackgroundAttributes ); + } + HANDLE stdoutHandle; + WORD originalForegroundAttributes; + WORD originalBackgroundAttributes; + }; + + IColourImpl* platformColourInstance() { + static Win32ColourImpl s_instance; + + IConfigPtr config = getCurrentContext().getConfig(); + UseColour::YesOrNo colourMode = config + ? config->useColour() + : UseColour::Auto; + if( colourMode == UseColour::Auto ) + colourMode = UseColour::Yes; + return colourMode == UseColour::Yes + ? &s_instance + : NoColourImpl::instance(); + } + +} // end anon namespace +} // end namespace Catch + +#elif defined( CATCH_CONFIG_COLOUR_ANSI ) ////////////////////////////////////// + +#include <unistd.h> + +namespace Catch { +namespace { + + // use POSIX/ ANSI console terminal codes + // Thanks to Adam Strzelecki for original contribution + // (http://github.com/nanoant) + // https://github.com/philsquared/Catch/pull/131 + class PosixColourImpl : public IColourImpl { + public: + virtual void use( Colour::Code _colourCode ) override { + switch( _colourCode ) { + case Colour::None: + case Colour::White: return setColour( "[0m" ); + case Colour::Red: return setColour( "[0;31m" ); + case Colour::Green: return setColour( "[0;32m" ); + case Colour::Blue: return setColour( "[0;34m" ); + case Colour::Cyan: return setColour( "[0;36m" ); + case Colour::Yellow: return setColour( "[0;33m" ); + case Colour::Grey: return setColour( "[1;30m" ); + + case Colour::LightGrey: return setColour( "[0;37m" ); + case Colour::BrightRed: return setColour( "[1;31m" ); + case Colour::BrightGreen: return setColour( "[1;32m" ); + case Colour::BrightWhite: return setColour( "[1;37m" ); + case Colour::BrightYellow: return setColour( "[1;33m" ); + + case Colour::Bright: CATCH_INTERNAL_ERROR( "not a colour" ); + default: CATCH_INTERNAL_ERROR( "Unknown colour requested" ); + } + } + static IColourImpl* instance() { + static PosixColourImpl s_instance; + return &s_instance; + } + + private: + void setColour( const char* _escapeCode ) { + Catch::cout() << '\033' << _escapeCode; + } + }; + + bool useColourOnPlatform() { + return +#ifdef CATCH_PLATFORM_MAC + !isDebuggerActive() && +#endif +#if !(defined(__DJGPP__) && defined(__STRICT_ANSI__)) + isatty(STDOUT_FILENO) +#else + false +#endif + ; + } + IColourImpl* platformColourInstance() { + ErrnoGuard guard; + IConfigPtr config = getCurrentContext().getConfig(); + UseColour::YesOrNo colourMode = config + ? config->useColour() + : UseColour::Auto; + if( colourMode == UseColour::Auto ) + colourMode = useColourOnPlatform() + ? UseColour::Yes + : UseColour::No; + return colourMode == UseColour::Yes + ? PosixColourImpl::instance() + : NoColourImpl::instance(); + } + +} // end anon namespace +} // end namespace Catch + +#else // not Windows or ANSI /////////////////////////////////////////////// + +namespace Catch { + + static IColourImpl* platformColourInstance() { return NoColourImpl::instance(); } + +} // end namespace Catch + +#endif // Windows/ ANSI/ None + +namespace Catch { + + Colour::Colour( Code _colourCode ) { use( _colourCode ); } + Colour::Colour( Colour&& rhs ) noexcept { + m_moved = rhs.m_moved; + rhs.m_moved = true; + } + Colour& Colour::operator=( Colour&& rhs ) noexcept { + m_moved = rhs.m_moved; + rhs.m_moved = true; + return *this; + } + + Colour::~Colour(){ if( !m_moved ) use( None ); } + + void Colour::use( Code _colourCode ) { + static IColourImpl* impl = platformColourInstance(); + impl->use( _colourCode ); + } + + std::ostream& operator << ( std::ostream& os, Colour const& ) { + return os; + } + +} // end namespace Catch + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif + +// end catch_console_colour.cpp +// start catch_context.cpp + +namespace Catch { + + class Context : public IMutableContext, NonCopyable { + + public: // IContext + virtual IResultCapture* getResultCapture() override { + return m_resultCapture; + } + virtual IRunner* getRunner() override { + return m_runner; + } + + virtual IConfigPtr const& getConfig() const override { + return m_config; + } + + virtual ~Context() override; + + public: // IMutableContext + virtual void setResultCapture( IResultCapture* resultCapture ) override { + m_resultCapture = resultCapture; + } + virtual void setRunner( IRunner* runner ) override { + m_runner = runner; + } + virtual void setConfig( IConfigPtr const& config ) override { + m_config = config; + } + + friend IMutableContext& getCurrentMutableContext(); + + private: + IConfigPtr m_config; + IRunner* m_runner = nullptr; + IResultCapture* m_resultCapture = nullptr; + }; + + IMutableContext *IMutableContext::currentContext = nullptr; + + void IMutableContext::createContext() + { + currentContext = new Context(); + } + + void cleanUpContext() { + delete IMutableContext::currentContext; + IMutableContext::currentContext = nullptr; + } + IContext::~IContext() = default; + IMutableContext::~IMutableContext() = default; + Context::~Context() = default; +} +// end catch_context.cpp +// start catch_debug_console.cpp + +// start catch_debug_console.h + +#include <string> + +namespace Catch { + void writeToDebugConsole( std::string const& text ); +} + +// end catch_debug_console.h +#ifdef CATCH_PLATFORM_WINDOWS + + namespace Catch { + void writeToDebugConsole( std::string const& text ) { + ::OutputDebugStringA( text.c_str() ); + } + } + +#else + + namespace Catch { + void writeToDebugConsole( std::string const& text ) { + // !TBD: Need a version for Mac/ XCode and other IDEs + Catch::cout() << text; + } + } + +#endif // Platform +// end catch_debug_console.cpp +// start catch_debugger.cpp + +#ifdef CATCH_PLATFORM_MAC + +# include <assert.h> +# include <stdbool.h> +# include <sys/types.h> +# include <unistd.h> +# include <sys/sysctl.h> +# include <cstddef> +# include <ostream> + +namespace Catch { + + // The following function is taken directly from the following technical note: + // http://developer.apple.com/library/mac/#qa/qa2004/qa1361.html + + // Returns true if the current process is being debugged (either + // running under the debugger or has a debugger attached post facto). + bool isDebuggerActive(){ + + int mib[4]; + struct kinfo_proc info; + std::size_t size; + + // Initialize the flags so that, if sysctl fails for some bizarre + // reason, we get a predictable result. + + info.kp_proc.p_flag = 0; + + // Initialize mib, which tells sysctl the info we want, in this case + // we're looking for information about a specific process ID. + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PID; + mib[3] = getpid(); + + // Call sysctl. + + size = sizeof(info); + if( sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) != 0 ) { + Catch::cerr() << "\n** Call to sysctl failed - unable to determine if debugger is active **\n" << std::endl; + return false; + } + + // We're being debugged if the P_TRACED flag is set. + + return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); + } + } // namespace Catch + +#elif defined(CATCH_PLATFORM_LINUX) + #include <fstream> + #include <string> + + namespace Catch{ + // The standard POSIX way of detecting a debugger is to attempt to + // ptrace() the process, but this needs to be done from a child and not + // this process itself to still allow attaching to this process later + // if wanted, so is rather heavy. Under Linux we have the PID of the + // "debugger" (which doesn't need to be gdb, of course, it could also + // be strace, for example) in /proc/$PID/status, so just get it from + // there instead. + bool isDebuggerActive(){ + // Libstdc++ has a bug, where std::ifstream sets errno to 0 + // This way our users can properly assert over errno values + ErrnoGuard guard; + std::ifstream in("/proc/self/status"); + for( std::string line; std::getline(in, line); ) { + static const int PREFIX_LEN = 11; + if( line.compare(0, PREFIX_LEN, "TracerPid:\t") == 0 ) { + // We're traced if the PID is not 0 and no other PID starts + // with 0 digit, so it's enough to check for just a single + // character. + return line.length() > PREFIX_LEN && line[PREFIX_LEN] != '0'; + } + } + + return false; + } + } // namespace Catch +#elif defined(_MSC_VER) + extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); + namespace Catch { + bool isDebuggerActive() { + return IsDebuggerPresent() != 0; + } + } +#elif defined(__MINGW32__) + extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); + namespace Catch { + bool isDebuggerActive() { + return IsDebuggerPresent() != 0; + } + } +#else + namespace Catch { + bool isDebuggerActive() { return false; } + } +#endif // Platform +// end catch_debugger.cpp +// start catch_decomposer.cpp + +namespace Catch { + + ITransientExpression::~ITransientExpression() = default; + + void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ) { + if( lhs.size() + rhs.size() < 40 && + lhs.find('\n') == std::string::npos && + rhs.find('\n') == std::string::npos ) + os << lhs << " " << op << " " << rhs; + else + os << lhs << "\n" << op << "\n" << rhs; + } +} +// end catch_decomposer.cpp +// start catch_enforce.cpp + +namespace Catch { +#if defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) && !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS_CUSTOM_HANDLER) + [[noreturn]] + void throw_exception(std::exception const& e) { + Catch::cerr() << "Catch will terminate because it needed to throw an exception.\n" + << "The message was: " << e.what() << '\n'; + std::terminate(); + } +#endif +} // namespace Catch; +// end catch_enforce.cpp +// start catch_errno_guard.cpp + +#include <cerrno> + +namespace Catch { + ErrnoGuard::ErrnoGuard():m_oldErrno(errno){} + ErrnoGuard::~ErrnoGuard() { errno = m_oldErrno; } +} +// end catch_errno_guard.cpp +// start catch_exception_translator_registry.cpp + +// start catch_exception_translator_registry.h + +#include <vector> +#include <string> +#include <memory> + +namespace Catch { + + class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry { + public: + ~ExceptionTranslatorRegistry(); + virtual void registerTranslator( const IExceptionTranslator* translator ); + virtual std::string translateActiveException() const override; + std::string tryTranslators() const; + + private: + std::vector<std::unique_ptr<IExceptionTranslator const>> m_translators; + }; +} + +// end catch_exception_translator_registry.h +#ifdef __OBJC__ +#import "Foundation/Foundation.h" +#endif + +namespace Catch { + + ExceptionTranslatorRegistry::~ExceptionTranslatorRegistry() { + } + + void ExceptionTranslatorRegistry::registerTranslator( const IExceptionTranslator* translator ) { + m_translators.push_back( std::unique_ptr<const IExceptionTranslator>( translator ) ); + } + +#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) + std::string ExceptionTranslatorRegistry::translateActiveException() const { + try { +#ifdef __OBJC__ + // In Objective-C try objective-c exceptions first + @try { + return tryTranslators(); + } + @catch (NSException *exception) { + return Catch::Detail::stringify( [exception description] ); + } +#else + // Compiling a mixed mode project with MSVC means that CLR + // exceptions will be caught in (...) as well. However, these + // do not fill-in std::current_exception and thus lead to crash + // when attempting rethrow. + // /EHa switch also causes structured exceptions to be caught + // here, but they fill-in current_exception properly, so + // at worst the output should be a little weird, instead of + // causing a crash. + if (std::current_exception() == nullptr) { + return "Non C++ exception. Possibly a CLR exception."; + } + return tryTranslators(); +#endif + } + catch( TestFailureException& ) { + std::rethrow_exception(std::current_exception()); + } + catch( std::exception& ex ) { + return ex.what(); + } + catch( std::string& msg ) { + return msg; + } + catch( const char* msg ) { + return msg; + } + catch(...) { + return "Unknown exception"; + } + } + +#else // ^^ Exceptions are enabled // Exceptions are disabled vv + std::string ExceptionTranslatorRegistry::translateActiveException() const { + CATCH_INTERNAL_ERROR("Attempted to translate active exception under CATCH_CONFIG_DISABLE_EXCEPTIONS!"); + } +#endif + + std::string ExceptionTranslatorRegistry::tryTranslators() const { + if( m_translators.empty() ) + std::rethrow_exception(std::current_exception()); + else + return m_translators[0]->translate( m_translators.begin()+1, m_translators.end() ); + } +} +// end catch_exception_translator_registry.cpp +// start catch_fatal_condition.cpp + +#if defined(__GNUC__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +#if defined( CATCH_CONFIG_WINDOWS_SEH ) || defined( CATCH_CONFIG_POSIX_SIGNALS ) + +namespace { + // Report the error condition + void reportFatal( char const * const message ) { + Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( message ); + } +} + +#endif // signals/SEH handling + +#if defined( CATCH_CONFIG_WINDOWS_SEH ) + +namespace Catch { + struct SignalDefs { DWORD id; const char* name; }; + + // There is no 1-1 mapping between signals and windows exceptions. + // Windows can easily distinguish between SO and SigSegV, + // but SigInt, SigTerm, etc are handled differently. + static SignalDefs signalDefs[] = { + { EXCEPTION_ILLEGAL_INSTRUCTION, "SIGILL - Illegal instruction signal" }, + { EXCEPTION_STACK_OVERFLOW, "SIGSEGV - Stack overflow" }, + { EXCEPTION_ACCESS_VIOLATION, "SIGSEGV - Segmentation violation signal" }, + { EXCEPTION_INT_DIVIDE_BY_ZERO, "Divide by zero error" }, + }; + + LONG CALLBACK FatalConditionHandler::handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) { + for (auto const& def : signalDefs) { + if (ExceptionInfo->ExceptionRecord->ExceptionCode == def.id) { + reportFatal(def.name); + } + } + // If its not an exception we care about, pass it along. + // This stops us from eating debugger breaks etc. + return EXCEPTION_CONTINUE_SEARCH; + } + + FatalConditionHandler::FatalConditionHandler() { + isSet = true; + // 32k seems enough for Catch to handle stack overflow, + // but the value was found experimentally, so there is no strong guarantee + guaranteeSize = 32 * 1024; + exceptionHandlerHandle = nullptr; + // Register as first handler in current chain + exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException); + // Pass in guarantee size to be filled + SetThreadStackGuarantee(&guaranteeSize); + } + + void FatalConditionHandler::reset() { + if (isSet) { + RemoveVectoredExceptionHandler(exceptionHandlerHandle); + SetThreadStackGuarantee(&guaranteeSize); + exceptionHandlerHandle = nullptr; + isSet = false; + } + } + + FatalConditionHandler::~FatalConditionHandler() { + reset(); + } + +bool FatalConditionHandler::isSet = false; +ULONG FatalConditionHandler::guaranteeSize = 0; +PVOID FatalConditionHandler::exceptionHandlerHandle = nullptr; + +} // namespace Catch + +#elif defined( CATCH_CONFIG_POSIX_SIGNALS ) + +namespace Catch { + + struct SignalDefs { + int id; + const char* name; + }; + + // 32kb for the alternate stack seems to be sufficient. However, this value + // is experimentally determined, so that's not guaranteed. + constexpr static std::size_t sigStackSize = 32768 >= MINSIGSTKSZ ? 32768 : MINSIGSTKSZ; + + static SignalDefs signalDefs[] = { + { SIGINT, "SIGINT - Terminal interrupt signal" }, + { SIGILL, "SIGILL - Illegal instruction signal" }, + { SIGFPE, "SIGFPE - Floating point error signal" }, + { SIGSEGV, "SIGSEGV - Segmentation violation signal" }, + { SIGTERM, "SIGTERM - Termination request signal" }, + { SIGABRT, "SIGABRT - Abort (abnormal termination) signal" } + }; + + void FatalConditionHandler::handleSignal( int sig ) { + char const * name = "<unknown signal>"; + for (auto const& def : signalDefs) { + if (sig == def.id) { + name = def.name; + break; + } + } + reset(); + reportFatal(name); + raise( sig ); + } + + FatalConditionHandler::FatalConditionHandler() { + isSet = true; + stack_t sigStack; + sigStack.ss_sp = altStackMem; + sigStack.ss_size = sigStackSize; + sigStack.ss_flags = 0; + sigaltstack(&sigStack, &oldSigStack); + struct sigaction sa = { }; + + sa.sa_handler = handleSignal; + sa.sa_flags = SA_ONSTACK; + for (std::size_t i = 0; i < sizeof(signalDefs)/sizeof(SignalDefs); ++i) { + sigaction(signalDefs[i].id, &sa, &oldSigActions[i]); + } + } + + FatalConditionHandler::~FatalConditionHandler() { + reset(); + } + + void FatalConditionHandler::reset() { + if( isSet ) { + // Set signals back to previous values -- hopefully nobody overwrote them in the meantime + for( std::size_t i = 0; i < sizeof(signalDefs)/sizeof(SignalDefs); ++i ) { + sigaction(signalDefs[i].id, &oldSigActions[i], nullptr); + } + // Return the old stack + sigaltstack(&oldSigStack, nullptr); + isSet = false; + } + } + + bool FatalConditionHandler::isSet = false; + struct sigaction FatalConditionHandler::oldSigActions[sizeof(signalDefs)/sizeof(SignalDefs)] = {}; + stack_t FatalConditionHandler::oldSigStack = {}; + char FatalConditionHandler::altStackMem[sigStackSize] = {}; + +} // namespace Catch + +#else + +namespace Catch { + void FatalConditionHandler::reset() {} +} + +#endif // signals/SEH handling + +#if defined(__GNUC__) +# pragma GCC diagnostic pop +#endif +// end catch_fatal_condition.cpp +// start catch_generators.cpp + +// start catch_random_number_generator.h + +#include <algorithm> +#include <random> + +namespace Catch { + + struct IConfig; + + std::mt19937& rng(); + void seedRng( IConfig const& config ); + unsigned int rngSeed(); + +} + +// end catch_random_number_generator.h +#include <limits> +#include <set> + +namespace Catch { + +IGeneratorTracker::~IGeneratorTracker() {} + +namespace Generators { + + GeneratorBase::~GeneratorBase() {} + + std::vector<size_t> randomiseIndices( size_t selectionSize, size_t sourceSize ) { + + assert( selectionSize <= sourceSize ); + std::vector<size_t> indices; + indices.reserve( selectionSize ); + std::uniform_int_distribution<size_t> uid( 0, sourceSize-1 ); + + std::set<size_t> seen; + // !TBD: improve this algorithm + while( indices.size() < selectionSize ) { + auto index = uid( rng() ); + if( seen.insert( index ).second ) + indices.push_back( index ); + } + return indices; + } + + auto acquireGeneratorTracker( SourceLineInfo const& lineInfo ) -> IGeneratorTracker& { + return getResultCapture().acquireGeneratorTracker( lineInfo ); + } + + template<> + auto all<int>() -> Generator<int> { + return range( std::numeric_limits<int>::min(), std::numeric_limits<int>::max() ); + } + +} // namespace Generators +} // namespace Catch +// end catch_generators.cpp +// start catch_interfaces_capture.cpp + +namespace Catch { + IResultCapture::~IResultCapture() = default; +} +// end catch_interfaces_capture.cpp +// start catch_interfaces_config.cpp + +namespace Catch { + IConfig::~IConfig() = default; +} +// end catch_interfaces_config.cpp +// start catch_interfaces_exception.cpp + +namespace Catch { + IExceptionTranslator::~IExceptionTranslator() = default; + IExceptionTranslatorRegistry::~IExceptionTranslatorRegistry() = default; +} +// end catch_interfaces_exception.cpp +// start catch_interfaces_registry_hub.cpp + +namespace Catch { + IRegistryHub::~IRegistryHub() = default; + IMutableRegistryHub::~IMutableRegistryHub() = default; +} +// end catch_interfaces_registry_hub.cpp +// start catch_interfaces_reporter.cpp + +// start catch_reporter_listening.h + +namespace Catch { + + class ListeningReporter : public IStreamingReporter { + using Reporters = std::vector<IStreamingReporterPtr>; + Reporters m_listeners; + IStreamingReporterPtr m_reporter = nullptr; + ReporterPreferences m_preferences; + + public: + ListeningReporter(); + + void addListener( IStreamingReporterPtr&& listener ); + void addReporter( IStreamingReporterPtr&& reporter ); + + public: // IStreamingReporter + + ReporterPreferences getPreferences() const override; + + void noMatchingTestCases( std::string const& spec ) override; + + static std::set<Verbosity> getSupportedVerbosities(); + + void benchmarkStarting( BenchmarkInfo const& benchmarkInfo ) override; + void benchmarkEnded( BenchmarkStats const& benchmarkStats ) override; + + void testRunStarting( TestRunInfo const& testRunInfo ) override; + void testGroupStarting( GroupInfo const& groupInfo ) override; + void testCaseStarting( TestCaseInfo const& testInfo ) override; + void sectionStarting( SectionInfo const& sectionInfo ) override; + void assertionStarting( AssertionInfo const& assertionInfo ) override; + + // The return value indicates if the messages buffer should be cleared: + bool assertionEnded( AssertionStats const& assertionStats ) override; + void sectionEnded( SectionStats const& sectionStats ) override; + void testCaseEnded( TestCaseStats const& testCaseStats ) override; + void testGroupEnded( TestGroupStats const& testGroupStats ) override; + void testRunEnded( TestRunStats const& testRunStats ) override; + + void skipTest( TestCaseInfo const& testInfo ) override; + bool isMulti() const override; + + }; + +} // end namespace Catch + +// end catch_reporter_listening.h +namespace Catch { + + ReporterConfig::ReporterConfig( IConfigPtr const& _fullConfig ) + : m_stream( &_fullConfig->stream() ), m_fullConfig( _fullConfig ) {} + + ReporterConfig::ReporterConfig( IConfigPtr const& _fullConfig, std::ostream& _stream ) + : m_stream( &_stream ), m_fullConfig( _fullConfig ) {} + + std::ostream& ReporterConfig::stream() const { return *m_stream; } + IConfigPtr ReporterConfig::fullConfig() const { return m_fullConfig; } + + TestRunInfo::TestRunInfo( std::string const& _name ) : name( _name ) {} + + GroupInfo::GroupInfo( std::string const& _name, + std::size_t _groupIndex, + std::size_t _groupsCount ) + : name( _name ), + groupIndex( _groupIndex ), + groupsCounts( _groupsCount ) + {} + + AssertionStats::AssertionStats( AssertionResult const& _assertionResult, + std::vector<MessageInfo> const& _infoMessages, + Totals const& _totals ) + : assertionResult( _assertionResult ), + infoMessages( _infoMessages ), + totals( _totals ) + { + assertionResult.m_resultData.lazyExpression.m_transientExpression = _assertionResult.m_resultData.lazyExpression.m_transientExpression; + + if( assertionResult.hasMessage() ) { + // Copy message into messages list. + // !TBD This should have been done earlier, somewhere + MessageBuilder builder( assertionResult.getTestMacroName(), assertionResult.getSourceInfo(), assertionResult.getResultType() ); + builder << assertionResult.getMessage(); + builder.m_info.message = builder.m_stream.str(); + + infoMessages.push_back( builder.m_info ); + } + } + + AssertionStats::~AssertionStats() = default; + + SectionStats::SectionStats( SectionInfo const& _sectionInfo, + Counts const& _assertions, + double _durationInSeconds, + bool _missingAssertions ) + : sectionInfo( _sectionInfo ), + assertions( _assertions ), + durationInSeconds( _durationInSeconds ), + missingAssertions( _missingAssertions ) + {} + + SectionStats::~SectionStats() = default; + + TestCaseStats::TestCaseStats( TestCaseInfo const& _testInfo, + Totals const& _totals, + std::string const& _stdOut, + std::string const& _stdErr, + bool _aborting ) + : testInfo( _testInfo ), + totals( _totals ), + stdOut( _stdOut ), + stdErr( _stdErr ), + aborting( _aborting ) + {} + + TestCaseStats::~TestCaseStats() = default; + + TestGroupStats::TestGroupStats( GroupInfo const& _groupInfo, + Totals const& _totals, + bool _aborting ) + : groupInfo( _groupInfo ), + totals( _totals ), + aborting( _aborting ) + {} + + TestGroupStats::TestGroupStats( GroupInfo const& _groupInfo ) + : groupInfo( _groupInfo ), + aborting( false ) + {} + + TestGroupStats::~TestGroupStats() = default; + + TestRunStats::TestRunStats( TestRunInfo const& _runInfo, + Totals const& _totals, + bool _aborting ) + : runInfo( _runInfo ), + totals( _totals ), + aborting( _aborting ) + {} + + TestRunStats::~TestRunStats() = default; + + void IStreamingReporter::fatalErrorEncountered( StringRef ) {} + bool IStreamingReporter::isMulti() const { return false; } + + IReporterFactory::~IReporterFactory() = default; + IReporterRegistry::~IReporterRegistry() = default; + +} // end namespace Catch +// end catch_interfaces_reporter.cpp +// start catch_interfaces_runner.cpp + +namespace Catch { + IRunner::~IRunner() = default; +} +// end catch_interfaces_runner.cpp +// start catch_interfaces_testcase.cpp + +namespace Catch { + ITestInvoker::~ITestInvoker() = default; + ITestCaseRegistry::~ITestCaseRegistry() = default; +} +// end catch_interfaces_testcase.cpp +// start catch_leak_detector.cpp + +#ifdef CATCH_CONFIG_WINDOWS_CRTDBG +#include <crtdbg.h> + +namespace Catch { + + LeakDetector::LeakDetector() { + int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); + flag |= _CRTDBG_LEAK_CHECK_DF; + flag |= _CRTDBG_ALLOC_MEM_DF; + _CrtSetDbgFlag(flag); + _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); + _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); + // Change this to leaking allocation's number to break there + _CrtSetBreakAlloc(-1); + } +} + +#else + + Catch::LeakDetector::LeakDetector() {} + +#endif + +Catch::LeakDetector::~LeakDetector() { + Catch::cleanUp(); +} +// end catch_leak_detector.cpp +// start catch_list.cpp + +// start catch_list.h + +#include <set> + +namespace Catch { + + std::size_t listTests( Config const& config ); + + std::size_t listTestsNamesOnly( Config const& config ); + + struct TagInfo { + void add( std::string const& spelling ); + std::string all() const; + + std::set<std::string> spellings; + std::size_t count = 0; + }; + + std::size_t listTags( Config const& config ); + + std::size_t listReporters(); + + Option<std::size_t> list( Config const& config ); + +} // end namespace Catch + +// end catch_list.h +// start catch_text.h + +namespace Catch { + using namespace clara::TextFlow; +} + +// end catch_text.h +#include <limits> +#include <algorithm> +#include <iomanip> + +namespace Catch { + + std::size_t listTests( Config const& config ) { + TestSpec testSpec = config.testSpec(); + if( config.hasTestFilters() ) + Catch::cout() << "Matching test cases:\n"; + else { + Catch::cout() << "All available test cases:\n"; + } + + auto matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); + for( auto const& testCaseInfo : matchedTestCases ) { + Colour::Code colour = testCaseInfo.isHidden() + ? Colour::SecondaryText + : Colour::None; + Colour colourGuard( colour ); + + Catch::cout() << Column( testCaseInfo.name ).initialIndent( 2 ).indent( 4 ) << "\n"; + if( config.verbosity() >= Verbosity::High ) { + Catch::cout() << Column( Catch::Detail::stringify( testCaseInfo.lineInfo ) ).indent(4) << std::endl; + std::string description = testCaseInfo.description; + if( description.empty() ) + description = "(NO DESCRIPTION)"; + Catch::cout() << Column( description ).indent(4) << std::endl; + } + if( !testCaseInfo.tags.empty() ) + Catch::cout() << Column( testCaseInfo.tagsAsString() ).indent( 6 ) << "\n"; + } + + if( !config.hasTestFilters() ) + Catch::cout() << pluralise( matchedTestCases.size(), "test case" ) << '\n' << std::endl; + else + Catch::cout() << pluralise( matchedTestCases.size(), "matching test case" ) << '\n' << std::endl; + return matchedTestCases.size(); + } + + std::size_t listTestsNamesOnly( Config const& config ) { + TestSpec testSpec = config.testSpec(); + std::size_t matchedTests = 0; + std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); + for( auto const& testCaseInfo : matchedTestCases ) { + matchedTests++; + if( startsWith( testCaseInfo.name, '#' ) ) + Catch::cout() << '"' << testCaseInfo.name << '"'; + else + Catch::cout() << testCaseInfo.name; + if ( config.verbosity() >= Verbosity::High ) + Catch::cout() << "\t@" << testCaseInfo.lineInfo; + Catch::cout() << std::endl; + } + return matchedTests; + } + + void TagInfo::add( std::string const& spelling ) { + ++count; + spellings.insert( spelling ); + } + + std::string TagInfo::all() const { + std::string out; + for( auto const& spelling : spellings ) + out += "[" + spelling + "]"; + return out; + } + + std::size_t listTags( Config const& config ) { + TestSpec testSpec = config.testSpec(); + if( config.hasTestFilters() ) + Catch::cout() << "Tags for matching test cases:\n"; + else { + Catch::cout() << "All available tags:\n"; + } + + std::map<std::string, TagInfo> tagCounts; + + std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); + for( auto const& testCase : matchedTestCases ) { + for( auto const& tagName : testCase.getTestCaseInfo().tags ) { + std::string lcaseTagName = toLower( tagName ); + auto countIt = tagCounts.find( lcaseTagName ); + if( countIt == tagCounts.end() ) + countIt = tagCounts.insert( std::make_pair( lcaseTagName, TagInfo() ) ).first; + countIt->second.add( tagName ); + } + } + + for( auto const& tagCount : tagCounts ) { + ReusableStringStream rss; + rss << " " << std::setw(2) << tagCount.second.count << " "; + auto str = rss.str(); + auto wrapper = Column( tagCount.second.all() ) + .initialIndent( 0 ) + .indent( str.size() ) + .width( CATCH_CONFIG_CONSOLE_WIDTH-10 ); + Catch::cout() << str << wrapper << '\n'; + } + Catch::cout() << pluralise( tagCounts.size(), "tag" ) << '\n' << std::endl; + return tagCounts.size(); + } + + std::size_t listReporters() { + Catch::cout() << "Available reporters:\n"; + IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories(); + std::size_t maxNameLen = 0; + for( auto const& factoryKvp : factories ) + maxNameLen = (std::max)( maxNameLen, factoryKvp.first.size() ); + + for( auto const& factoryKvp : factories ) { + Catch::cout() + << Column( factoryKvp.first + ":" ) + .indent(2) + .width( 5+maxNameLen ) + + Column( factoryKvp.second->getDescription() ) + .initialIndent(0) + .indent(2) + .width( CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen-8 ) + << "\n"; + } + Catch::cout() << std::endl; + return factories.size(); + } + + Option<std::size_t> list( Config const& config ) { + Option<std::size_t> listedCount; + if( config.listTests() ) + listedCount = listedCount.valueOr(0) + listTests( config ); + if( config.listTestNamesOnly() ) + listedCount = listedCount.valueOr(0) + listTestsNamesOnly( config ); + if( config.listTags() ) + listedCount = listedCount.valueOr(0) + listTags( config ); + if( config.listReporters() ) + listedCount = listedCount.valueOr(0) + listReporters(); + return listedCount; + } + +} // end namespace Catch +// end catch_list.cpp +// start catch_matchers.cpp + +namespace Catch { +namespace Matchers { + namespace Impl { + + std::string MatcherUntypedBase::toString() const { + if( m_cachedToString.empty() ) + m_cachedToString = describe(); + return m_cachedToString; + } + + MatcherUntypedBase::~MatcherUntypedBase() = default; + + } // namespace Impl +} // namespace Matchers + +using namespace Matchers; +using Matchers::Impl::MatcherBase; + +} // namespace Catch +// end catch_matchers.cpp +// start catch_matchers_floating.cpp + +// start catch_polyfills.hpp + +namespace Catch { + bool isnan(float f); + bool isnan(double d); +} + +// end catch_polyfills.hpp +// start catch_to_string.hpp + +#include <string> + +namespace Catch { + template <typename T> + std::string to_string(T const& t) { +#if defined(CATCH_CONFIG_CPP11_TO_STRING) + return std::to_string(t); +#else + ReusableStringStream rss; + rss << t; + return rss.str(); +#endif + } +} // end namespace Catch + +// end catch_to_string.hpp +#include <cstdlib> +#include <cstdint> +#include <cstring> + +namespace Catch { +namespace Matchers { +namespace Floating { +enum class FloatingPointKind : uint8_t { + Float, + Double +}; +} +} +} + +namespace { + +template <typename T> +struct Converter; + +template <> +struct Converter<float> { + static_assert(sizeof(float) == sizeof(int32_t), "Important ULP matcher assumption violated"); + Converter(float f) { + std::memcpy(&i, &f, sizeof(f)); + } + int32_t i; +}; + +template <> +struct Converter<double> { + static_assert(sizeof(double) == sizeof(int64_t), "Important ULP matcher assumption violated"); + Converter(double d) { + std::memcpy(&i, &d, sizeof(d)); + } + int64_t i; +}; + +template <typename T> +auto convert(T t) -> Converter<T> { + return Converter<T>(t); +} + +template <typename FP> +bool almostEqualUlps(FP lhs, FP rhs, int maxUlpDiff) { + // Comparison with NaN should always be false. + // This way we can rule it out before getting into the ugly details + if (Catch::isnan(lhs) || Catch::isnan(rhs)) { + return false; + } + + auto lc = convert(lhs); + auto rc = convert(rhs); + + if ((lc.i < 0) != (rc.i < 0)) { + // Potentially we can have +0 and -0 + return lhs == rhs; + } + + auto ulpDiff = std::abs(lc.i - rc.i); + return ulpDiff <= maxUlpDiff; +} + +} + +namespace Catch { +namespace Matchers { +namespace Floating { + WithinAbsMatcher::WithinAbsMatcher(double target, double margin) + :m_target{ target }, m_margin{ margin } { + CATCH_ENFORCE(margin >= 0, "Invalid margin: " << margin << '.' + << " Margin has to be non-negative."); + } + + // Performs equivalent check of std::fabs(lhs - rhs) <= margin + // But without the subtraction to allow for INFINITY in comparison + bool WithinAbsMatcher::match(double const& matchee) const { + return (matchee + m_margin >= m_target) && (m_target + m_margin >= matchee); + } + + std::string WithinAbsMatcher::describe() const { + return "is within " + ::Catch::Detail::stringify(m_margin) + " of " + ::Catch::Detail::stringify(m_target); + } + + WithinUlpsMatcher::WithinUlpsMatcher(double target, int ulps, FloatingPointKind baseType) + :m_target{ target }, m_ulps{ ulps }, m_type{ baseType } { + CATCH_ENFORCE(ulps >= 0, "Invalid ULP setting: " << ulps << '.' + << " ULPs have to be non-negative."); + } + +#if defined(__clang__) +#pragma clang diagnostic push +// Clang <3.5 reports on the default branch in the switch below +#pragma clang diagnostic ignored "-Wunreachable-code" +#endif + + bool WithinUlpsMatcher::match(double const& matchee) const { + switch (m_type) { + case FloatingPointKind::Float: + return almostEqualUlps<float>(static_cast<float>(matchee), static_cast<float>(m_target), m_ulps); + case FloatingPointKind::Double: + return almostEqualUlps<double>(matchee, m_target, m_ulps); + default: + CATCH_INTERNAL_ERROR( "Unknown FloatingPointKind value" ); + } + } + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif + + std::string WithinUlpsMatcher::describe() const { + return "is within " + Catch::to_string(m_ulps) + " ULPs of " + ::Catch::Detail::stringify(m_target) + ((m_type == FloatingPointKind::Float)? "f" : ""); + } + +}// namespace Floating + +Floating::WithinUlpsMatcher WithinULP(double target, int maxUlpDiff) { + return Floating::WithinUlpsMatcher(target, maxUlpDiff, Floating::FloatingPointKind::Double); +} + +Floating::WithinUlpsMatcher WithinULP(float target, int maxUlpDiff) { + return Floating::WithinUlpsMatcher(target, maxUlpDiff, Floating::FloatingPointKind::Float); +} + +Floating::WithinAbsMatcher WithinAbs(double target, double margin) { + return Floating::WithinAbsMatcher(target, margin); +} + +} // namespace Matchers +} // namespace Catch + +// end catch_matchers_floating.cpp +// start catch_matchers_generic.cpp + +std::string Catch::Matchers::Generic::Detail::finalizeDescription(const std::string& desc) { + if (desc.empty()) { + return "matches undescribed predicate"; + } else { + return "matches predicate: \"" + desc + '"'; + } +} +// end catch_matchers_generic.cpp +// start catch_matchers_string.cpp + +#include <regex> + +namespace Catch { +namespace Matchers { + + namespace StdString { + + CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ) + : m_caseSensitivity( caseSensitivity ), + m_str( adjustString( str ) ) + {} + std::string CasedString::adjustString( std::string const& str ) const { + return m_caseSensitivity == CaseSensitive::No + ? toLower( str ) + : str; + } + std::string CasedString::caseSensitivitySuffix() const { + return m_caseSensitivity == CaseSensitive::No + ? " (case insensitive)" + : std::string(); + } + + StringMatcherBase::StringMatcherBase( std::string const& operation, CasedString const& comparator ) + : m_comparator( comparator ), + m_operation( operation ) { + } + + std::string StringMatcherBase::describe() const { + std::string description; + description.reserve(5 + m_operation.size() + m_comparator.m_str.size() + + m_comparator.caseSensitivitySuffix().size()); + description += m_operation; + description += ": \""; + description += m_comparator.m_str; + description += "\""; + description += m_comparator.caseSensitivitySuffix(); + return description; + } + + EqualsMatcher::EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {} + + bool EqualsMatcher::match( std::string const& source ) const { + return m_comparator.adjustString( source ) == m_comparator.m_str; + } + + ContainsMatcher::ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {} + + bool ContainsMatcher::match( std::string const& source ) const { + return contains( m_comparator.adjustString( source ), m_comparator.m_str ); + } + + StartsWithMatcher::StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {} + + bool StartsWithMatcher::match( std::string const& source ) const { + return startsWith( m_comparator.adjustString( source ), m_comparator.m_str ); + } + + EndsWithMatcher::EndsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "ends with", comparator ) {} + + bool EndsWithMatcher::match( std::string const& source ) const { + return endsWith( m_comparator.adjustString( source ), m_comparator.m_str ); + } + + RegexMatcher::RegexMatcher(std::string regex, CaseSensitive::Choice caseSensitivity): m_regex(std::move(regex)), m_caseSensitivity(caseSensitivity) {} + + bool RegexMatcher::match(std::string const& matchee) const { + auto flags = std::regex::ECMAScript; // ECMAScript is the default syntax option anyway + if (m_caseSensitivity == CaseSensitive::Choice::No) { + flags |= std::regex::icase; + } + auto reg = std::regex(m_regex, flags); + return std::regex_match(matchee, reg); + } + + std::string RegexMatcher::describe() const { + return "matches " + ::Catch::Detail::stringify(m_regex) + ((m_caseSensitivity == CaseSensitive::Choice::Yes)? " case sensitively" : " case insensitively"); + } + + } // namespace StdString + + StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) ); + } + StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) ); + } + StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) ); + } + StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) ); + } + + StdString::RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) { + return StdString::RegexMatcher(regex, caseSensitivity); + } + +} // namespace Matchers +} // namespace Catch +// end catch_matchers_string.cpp +// start catch_message.cpp + +// start catch_uncaught_exceptions.h + +namespace Catch { + bool uncaught_exceptions(); +} // end namespace Catch + +// end catch_uncaught_exceptions.h +#include <cassert> +#include <stack> + +namespace Catch { + + MessageInfo::MessageInfo( StringRef const& _macroName, + SourceLineInfo const& _lineInfo, + ResultWas::OfType _type ) + : macroName( _macroName ), + lineInfo( _lineInfo ), + type( _type ), + sequence( ++globalCount ) + {} + + bool MessageInfo::operator==( MessageInfo const& other ) const { + return sequence == other.sequence; + } + + bool MessageInfo::operator<( MessageInfo const& other ) const { + return sequence < other.sequence; + } + + // This may need protecting if threading support is added + unsigned int MessageInfo::globalCount = 0; + + //////////////////////////////////////////////////////////////////////////// + + Catch::MessageBuilder::MessageBuilder( StringRef const& macroName, + SourceLineInfo const& lineInfo, + ResultWas::OfType type ) + :m_info(macroName, lineInfo, type) {} + + //////////////////////////////////////////////////////////////////////////// + + ScopedMessage::ScopedMessage( MessageBuilder const& builder ) + : m_info( builder.m_info ) + { + m_info.message = builder.m_stream.str(); + getResultCapture().pushScopedMessage( m_info ); + } + + ScopedMessage::~ScopedMessage() { + if ( !uncaught_exceptions() ){ + getResultCapture().popScopedMessage(m_info); + } + } + + Capturer::Capturer( StringRef macroName, SourceLineInfo const& lineInfo, ResultWas::OfType resultType, StringRef names ) { + auto trimmed = [&] (size_t start, size_t end) { + while (names[start] == ',' || isspace(names[start])) { + ++start; + } + while (names[end] == ',' || isspace(names[end])) { + --end; + } + return names.substr(start, end - start + 1); + }; + + size_t start = 0; + std::stack<char> openings; + for (size_t pos = 0; pos < names.size(); ++pos) { + char c = names[pos]; + switch (c) { + case '[': + case '{': + case '(': + // It is basically impossible to disambiguate between + // comparison and start of template args in this context +// case '<': + openings.push(c); + break; + case ']': + case '}': + case ')': +// case '>': + openings.pop(); + break; + case ',': + if (start != pos && openings.size() == 0) { + m_messages.emplace_back(macroName, lineInfo, resultType); + m_messages.back().message = trimmed(start, pos); + m_messages.back().message += " := "; + start = pos; + } + } + } + assert(openings.size() == 0 && "Mismatched openings"); + m_messages.emplace_back(macroName, lineInfo, resultType); + m_messages.back().message = trimmed(start, names.size() - 1); + m_messages.back().message += " := "; + } + Capturer::~Capturer() { + if ( !uncaught_exceptions() ){ + assert( m_captured == m_messages.size() ); + for( size_t i = 0; i < m_captured; ++i ) + m_resultCapture.popScopedMessage( m_messages[i] ); + } + } + + void Capturer::captureValue( size_t index, std::string const& value ) { + assert( index < m_messages.size() ); + m_messages[index].message += value; + m_resultCapture.pushScopedMessage( m_messages[index] ); + m_captured++; + } + +} // end namespace Catch +// end catch_message.cpp +// start catch_output_redirect.cpp + +// start catch_output_redirect.h +#ifndef TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H +#define TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H + +#include <cstdio> +#include <iosfwd> +#include <string> + +namespace Catch { + + class RedirectedStream { + std::ostream& m_originalStream; + std::ostream& m_redirectionStream; + std::streambuf* m_prevBuf; + + public: + RedirectedStream( std::ostream& originalStream, std::ostream& redirectionStream ); + ~RedirectedStream(); + }; + + class RedirectedStdOut { + ReusableStringStream m_rss; + RedirectedStream m_cout; + public: + RedirectedStdOut(); + auto str() const -> std::string; + }; + + // StdErr has two constituent streams in C++, std::cerr and std::clog + // This means that we need to redirect 2 streams into 1 to keep proper + // order of writes + class RedirectedStdErr { + ReusableStringStream m_rss; + RedirectedStream m_cerr; + RedirectedStream m_clog; + public: + RedirectedStdErr(); + auto str() const -> std::string; + }; + +#if defined(CATCH_CONFIG_NEW_CAPTURE) + + // Windows's implementation of std::tmpfile is terrible (it tries + // to create a file inside system folder, thus requiring elevated + // privileges for the binary), so we have to use tmpnam(_s) and + // create the file ourselves there. + class TempFile { + public: + TempFile(TempFile const&) = delete; + TempFile& operator=(TempFile const&) = delete; + TempFile(TempFile&&) = delete; + TempFile& operator=(TempFile&&) = delete; + + TempFile(); + ~TempFile(); + + std::FILE* getFile(); + std::string getContents(); + + private: + std::FILE* m_file = nullptr; + #if defined(_MSC_VER) + char m_buffer[L_tmpnam] = { 0 }; + #endif + }; + + class OutputRedirect { + public: + OutputRedirect(OutputRedirect const&) = delete; + OutputRedirect& operator=(OutputRedirect const&) = delete; + OutputRedirect(OutputRedirect&&) = delete; + OutputRedirect& operator=(OutputRedirect&&) = delete; + + OutputRedirect(std::string& stdout_dest, std::string& stderr_dest); + ~OutputRedirect(); + + private: + int m_originalStdout = -1; + int m_originalStderr = -1; + TempFile m_stdoutFile; + TempFile m_stderrFile; + std::string& m_stdoutDest; + std::string& m_stderrDest; + }; + +#endif + +} // end namespace Catch + +#endif // TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H +// end catch_output_redirect.h +#include <cstdio> +#include <cstring> +#include <fstream> +#include <sstream> +#include <stdexcept> + +#if defined(CATCH_CONFIG_NEW_CAPTURE) + #if defined(_MSC_VER) + #include <io.h> //_dup and _dup2 + #define dup _dup + #define dup2 _dup2 + #define fileno _fileno + #else + #include <unistd.h> // dup and dup2 + #endif +#endif + +namespace Catch { + + RedirectedStream::RedirectedStream( std::ostream& originalStream, std::ostream& redirectionStream ) + : m_originalStream( originalStream ), + m_redirectionStream( redirectionStream ), + m_prevBuf( m_originalStream.rdbuf() ) + { + m_originalStream.rdbuf( m_redirectionStream.rdbuf() ); + } + + RedirectedStream::~RedirectedStream() { + m_originalStream.rdbuf( m_prevBuf ); + } + + RedirectedStdOut::RedirectedStdOut() : m_cout( Catch::cout(), m_rss.get() ) {} + auto RedirectedStdOut::str() const -> std::string { return m_rss.str(); } + + RedirectedStdErr::RedirectedStdErr() + : m_cerr( Catch::cerr(), m_rss.get() ), + m_clog( Catch::clog(), m_rss.get() ) + {} + auto RedirectedStdErr::str() const -> std::string { return m_rss.str(); } + +#if defined(CATCH_CONFIG_NEW_CAPTURE) + +#if defined(_MSC_VER) + TempFile::TempFile() { + if (tmpnam_s(m_buffer)) { + CATCH_RUNTIME_ERROR("Could not get a temp filename"); + } + if (fopen_s(&m_file, m_buffer, "w")) { + char buffer[100]; + if (strerror_s(buffer, errno)) { + CATCH_RUNTIME_ERROR("Could not translate errno to a string"); + } + CATCH_RUNTIME_ERROR("Coul dnot open the temp file: '" << m_buffer << "' because: " << buffer); + } + } +#else + TempFile::TempFile() { + m_file = std::tmpfile(); + if (!m_file) { + CATCH_RUNTIME_ERROR("Could not create a temp file."); + } + } + +#endif + + TempFile::~TempFile() { + // TBD: What to do about errors here? + std::fclose(m_file); + // We manually create the file on Windows only, on Linux + // it will be autodeleted +#if defined(_MSC_VER) + std::remove(m_buffer); +#endif + } + + FILE* TempFile::getFile() { + return m_file; + } + + std::string TempFile::getContents() { + std::stringstream sstr; + char buffer[100] = {}; + std::rewind(m_file); + while (std::fgets(buffer, sizeof(buffer), m_file)) { + sstr << buffer; + } + return sstr.str(); + } + + OutputRedirect::OutputRedirect(std::string& stdout_dest, std::string& stderr_dest) : + m_originalStdout(dup(1)), + m_originalStderr(dup(2)), + m_stdoutDest(stdout_dest), + m_stderrDest(stderr_dest) { + dup2(fileno(m_stdoutFile.getFile()), 1); + dup2(fileno(m_stderrFile.getFile()), 2); + } + + OutputRedirect::~OutputRedirect() { + Catch::cout() << std::flush; + fflush(stdout); + // Since we support overriding these streams, we flush cerr + // even though std::cerr is unbuffered + Catch::cerr() << std::flush; + Catch::clog() << std::flush; + fflush(stderr); + + dup2(m_originalStdout, 1); + dup2(m_originalStderr, 2); + + m_stdoutDest += m_stdoutFile.getContents(); + m_stderrDest += m_stderrFile.getContents(); + } + +#endif // CATCH_CONFIG_NEW_CAPTURE + +} // namespace Catch + +#if defined(CATCH_CONFIG_NEW_CAPTURE) + #if defined(_MSC_VER) + #undef dup + #undef dup2 + #undef fileno + #endif +#endif +// end catch_output_redirect.cpp +// start catch_polyfills.cpp + +#include <cmath> + +namespace Catch { + +#if !defined(CATCH_CONFIG_POLYFILL_ISNAN) + bool isnan(float f) { + return std::isnan(f); + } + bool isnan(double d) { + return std::isnan(d); + } +#else + // For now we only use this for embarcadero + bool isnan(float f) { + return std::_isnan(f); + } + bool isnan(double d) { + return std::_isnan(d); + } +#endif + +} // end namespace Catch +// end catch_polyfills.cpp +// start catch_random_number_generator.cpp + +namespace Catch { + + std::mt19937& rng() { + static std::mt19937 s_rng; + return s_rng; + } + + void seedRng( IConfig const& config ) { + if( config.rngSeed() != 0 ) { + std::srand( config.rngSeed() ); + rng().seed( config.rngSeed() ); + } + } + + unsigned int rngSeed() { + return getCurrentContext().getConfig()->rngSeed(); + } +} +// end catch_random_number_generator.cpp +// start catch_registry_hub.cpp + +// start catch_test_case_registry_impl.h + +#include <vector> +#include <set> +#include <algorithm> +#include <ios> + +namespace Catch { + + class TestCase; + struct IConfig; + + std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ); + bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ); + + void enforceNoDuplicateTestCases( std::vector<TestCase> const& functions ); + + std::vector<TestCase> filterTests( std::vector<TestCase> const& testCases, TestSpec const& testSpec, IConfig const& config ); + std::vector<TestCase> const& getAllTestCasesSorted( IConfig const& config ); + + class TestRegistry : public ITestCaseRegistry { + public: + virtual ~TestRegistry() = default; + + virtual void registerTest( TestCase const& testCase ); + + std::vector<TestCase> const& getAllTests() const override; + std::vector<TestCase> const& getAllTestsSorted( IConfig const& config ) const override; + + private: + std::vector<TestCase> m_functions; + mutable RunTests::InWhatOrder m_currentSortOrder = RunTests::InDeclarationOrder; + mutable std::vector<TestCase> m_sortedFunctions; + std::size_t m_unnamedCount = 0; + std::ios_base::Init m_ostreamInit; // Forces cout/ cerr to be initialised + }; + + /////////////////////////////////////////////////////////////////////////// + + class TestInvokerAsFunction : public ITestInvoker { + void(*m_testAsFunction)(); + public: + TestInvokerAsFunction( void(*testAsFunction)() ) noexcept; + + void invoke() const override; + }; + + std::string extractClassName( StringRef const& classOrQualifiedMethodName ); + + /////////////////////////////////////////////////////////////////////////// + +} // end namespace Catch + +// end catch_test_case_registry_impl.h +// start catch_reporter_registry.h + +#include <map> + +namespace Catch { + + class ReporterRegistry : public IReporterRegistry { + + public: + + ~ReporterRegistry() override; + + IStreamingReporterPtr create( std::string const& name, IConfigPtr const& config ) const override; + + void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ); + void registerListener( IReporterFactoryPtr const& factory ); + + FactoryMap const& getFactories() const override; + Listeners const& getListeners() const override; + + private: + FactoryMap m_factories; + Listeners m_listeners; + }; +} + +// end catch_reporter_registry.h +// start catch_tag_alias_registry.h + +// start catch_tag_alias.h + +#include <string> + +namespace Catch { + + struct TagAlias { + TagAlias(std::string const& _tag, SourceLineInfo _lineInfo); + + std::string tag; + SourceLineInfo lineInfo; + }; + +} // end namespace Catch + +// end catch_tag_alias.h +#include <map> + +namespace Catch { + + class TagAliasRegistry : public ITagAliasRegistry { + public: + ~TagAliasRegistry() override; + TagAlias const* find( std::string const& alias ) const override; + std::string expandAliases( std::string const& unexpandedTestSpec ) const override; + void add( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ); + + private: + std::map<std::string, TagAlias> m_registry; + }; + +} // end namespace Catch + +// end catch_tag_alias_registry.h +// start catch_startup_exception_registry.h + +#include <vector> +#include <exception> + +namespace Catch { + + class StartupExceptionRegistry { + public: + void add(std::exception_ptr const& exception) noexcept; + std::vector<std::exception_ptr> const& getExceptions() const noexcept; + private: + std::vector<std::exception_ptr> m_exceptions; + }; + +} // end namespace Catch + +// end catch_startup_exception_registry.h +// start catch_singletons.hpp + +namespace Catch { + + struct ISingleton { + virtual ~ISingleton(); + }; + + void addSingleton( ISingleton* singleton ); + void cleanupSingletons(); + + template<typename SingletonImplT, typename InterfaceT = SingletonImplT, typename MutableInterfaceT = InterfaceT> + class Singleton : SingletonImplT, public ISingleton { + + static auto getInternal() -> Singleton* { + static Singleton* s_instance = nullptr; + if( !s_instance ) { + s_instance = new Singleton; + addSingleton( s_instance ); + } + return s_instance; + } + + public: + static auto get() -> InterfaceT const& { + return *getInternal(); + } + static auto getMutable() -> MutableInterfaceT& { + return *getInternal(); + } + }; + +} // namespace Catch + +// end catch_singletons.hpp +namespace Catch { + + namespace { + + class RegistryHub : public IRegistryHub, public IMutableRegistryHub, + private NonCopyable { + + public: // IRegistryHub + RegistryHub() = default; + IReporterRegistry const& getReporterRegistry() const override { + return m_reporterRegistry; + } + ITestCaseRegistry const& getTestCaseRegistry() const override { + return m_testCaseRegistry; + } + IExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const override { + return m_exceptionTranslatorRegistry; + } + ITagAliasRegistry const& getTagAliasRegistry() const override { + return m_tagAliasRegistry; + } + StartupExceptionRegistry const& getStartupExceptionRegistry() const override { + return m_exceptionRegistry; + } + + public: // IMutableRegistryHub + void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) override { + m_reporterRegistry.registerReporter( name, factory ); + } + void registerListener( IReporterFactoryPtr const& factory ) override { + m_reporterRegistry.registerListener( factory ); + } + void registerTest( TestCase const& testInfo ) override { + m_testCaseRegistry.registerTest( testInfo ); + } + void registerTranslator( const IExceptionTranslator* translator ) override { + m_exceptionTranslatorRegistry.registerTranslator( translator ); + } + void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) override { + m_tagAliasRegistry.add( alias, tag, lineInfo ); + } + void registerStartupException() noexcept override { + m_exceptionRegistry.add(std::current_exception()); + } + + private: + TestRegistry m_testCaseRegistry; + ReporterRegistry m_reporterRegistry; + ExceptionTranslatorRegistry m_exceptionTranslatorRegistry; + TagAliasRegistry m_tagAliasRegistry; + StartupExceptionRegistry m_exceptionRegistry; + }; + } + + using RegistryHubSingleton = Singleton<RegistryHub, IRegistryHub, IMutableRegistryHub>; + + IRegistryHub const& getRegistryHub() { + return RegistryHubSingleton::get(); + } + IMutableRegistryHub& getMutableRegistryHub() { + return RegistryHubSingleton::getMutable(); + } + void cleanUp() { + cleanupSingletons(); + cleanUpContext(); + } + std::string translateActiveException() { + return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException(); + } + +} // end namespace Catch +// end catch_registry_hub.cpp +// start catch_reporter_registry.cpp + +namespace Catch { + + ReporterRegistry::~ReporterRegistry() = default; + + IStreamingReporterPtr ReporterRegistry::create( std::string const& name, IConfigPtr const& config ) const { + auto it = m_factories.find( name ); + if( it == m_factories.end() ) + return nullptr; + return it->second->create( ReporterConfig( config ) ); + } + + void ReporterRegistry::registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) { + m_factories.emplace(name, factory); + } + void ReporterRegistry::registerListener( IReporterFactoryPtr const& factory ) { + m_listeners.push_back( factory ); + } + + IReporterRegistry::FactoryMap const& ReporterRegistry::getFactories() const { + return m_factories; + } + IReporterRegistry::Listeners const& ReporterRegistry::getListeners() const { + return m_listeners; + } + +} +// end catch_reporter_registry.cpp +// start catch_result_type.cpp + +namespace Catch { + + bool isOk( ResultWas::OfType resultType ) { + return ( resultType & ResultWas::FailureBit ) == 0; + } + bool isJustInfo( int flags ) { + return flags == ResultWas::Info; + } + + ResultDisposition::Flags operator | ( ResultDisposition::Flags lhs, ResultDisposition::Flags rhs ) { + return static_cast<ResultDisposition::Flags>( static_cast<int>( lhs ) | static_cast<int>( rhs ) ); + } + + bool shouldContinueOnFailure( int flags ) { return ( flags & ResultDisposition::ContinueOnFailure ) != 0; } + bool shouldSuppressFailure( int flags ) { return ( flags & ResultDisposition::SuppressFail ) != 0; } + +} // end namespace Catch +// end catch_result_type.cpp +// start catch_run_context.cpp + +#include <cassert> +#include <algorithm> +#include <sstream> + +namespace Catch { + + namespace Generators { + struct GeneratorTracker : TestCaseTracking::TrackerBase, IGeneratorTracker { + size_t m_index = static_cast<size_t>( -1 ); + GeneratorBasePtr m_generator; + + GeneratorTracker( TestCaseTracking::NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ) + : TrackerBase( nameAndLocation, ctx, parent ) + {} + ~GeneratorTracker(); + + static GeneratorTracker& acquire( TrackerContext& ctx, TestCaseTracking::NameAndLocation const& nameAndLocation ) { + std::shared_ptr<GeneratorTracker> tracker; + + ITracker& currentTracker = ctx.currentTracker(); + if( TestCaseTracking::ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) { + assert( childTracker ); + assert( childTracker->isIndexTracker() ); + tracker = std::static_pointer_cast<GeneratorTracker>( childTracker ); + } + else { + tracker = std::make_shared<GeneratorTracker>( nameAndLocation, ctx, ¤tTracker ); + currentTracker.addChild( tracker ); + } + + if( !ctx.completedCycle() && !tracker->isComplete() ) { + if( tracker->m_runState != ExecutingChildren && tracker->m_runState != NeedsAnotherRun ) + tracker->moveNext(); + tracker->open(); + } + + return *tracker; + } + + void moveNext() { + m_index++; + m_children.clear(); + } + + // TrackerBase interface + bool isIndexTracker() const override { return true; } + auto hasGenerator() const -> bool override { + return !!m_generator; + } + void close() override { + TrackerBase::close(); + if( m_runState == CompletedSuccessfully && m_index < m_generator->size()-1 ) + m_runState = Executing; + } + + // IGeneratorTracker interface + auto getGenerator() const -> GeneratorBasePtr const& override { + return m_generator; + } + void setGenerator( GeneratorBasePtr&& generator ) override { + m_generator = std::move( generator ); + } + auto getIndex() const -> size_t override { + return m_index; + } + }; + GeneratorTracker::~GeneratorTracker() {} + } + + RunContext::RunContext(IConfigPtr const& _config, IStreamingReporterPtr&& reporter) + : m_runInfo(_config->name()), + m_context(getCurrentMutableContext()), + m_config(_config), + m_reporter(std::move(reporter)), + m_lastAssertionInfo{ StringRef(), SourceLineInfo("",0), StringRef(), ResultDisposition::Normal }, + m_includeSuccessfulResults( m_config->includeSuccessfulResults() || m_reporter->getPreferences().shouldReportAllAssertions ) + { + m_context.setRunner(this); + m_context.setConfig(m_config); + m_context.setResultCapture(this); + m_reporter->testRunStarting(m_runInfo); + } + + RunContext::~RunContext() { + m_reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, aborting())); + } + + void RunContext::testGroupStarting(std::string const& testSpec, std::size_t groupIndex, std::size_t groupsCount) { + m_reporter->testGroupStarting(GroupInfo(testSpec, groupIndex, groupsCount)); + } + + void RunContext::testGroupEnded(std::string const& testSpec, Totals const& totals, std::size_t groupIndex, std::size_t groupsCount) { + m_reporter->testGroupEnded(TestGroupStats(GroupInfo(testSpec, groupIndex, groupsCount), totals, aborting())); + } + + Totals RunContext::runTest(TestCase const& testCase) { + Totals prevTotals = m_totals; + + std::string redirectedCout; + std::string redirectedCerr; + + auto const& testInfo = testCase.getTestCaseInfo(); + + m_reporter->testCaseStarting(testInfo); + + m_activeTestCase = &testCase; + + ITracker& rootTracker = m_trackerContext.startRun(); + assert(rootTracker.isSectionTracker()); + static_cast<SectionTracker&>(rootTracker).addInitialFilters(m_config->getSectionsToRun()); + do { + m_trackerContext.startCycle(); + m_testCaseTracker = &SectionTracker::acquire(m_trackerContext, TestCaseTracking::NameAndLocation(testInfo.name, testInfo.lineInfo)); + runCurrentTest(redirectedCout, redirectedCerr); + } while (!m_testCaseTracker->isSuccessfullyCompleted() && !aborting()); + + Totals deltaTotals = m_totals.delta(prevTotals); + if (testInfo.expectedToFail() && deltaTotals.testCases.passed > 0) { + deltaTotals.assertions.failed++; + deltaTotals.testCases.passed--; + deltaTotals.testCases.failed++; + } + m_totals.testCases += deltaTotals.testCases; + m_reporter->testCaseEnded(TestCaseStats(testInfo, + deltaTotals, + redirectedCout, + redirectedCerr, + aborting())); + + m_activeTestCase = nullptr; + m_testCaseTracker = nullptr; + + return deltaTotals; + } + + IConfigPtr RunContext::config() const { + return m_config; + } + + IStreamingReporter& RunContext::reporter() const { + return *m_reporter; + } + + void RunContext::assertionEnded(AssertionResult const & result) { + if (result.getResultType() == ResultWas::Ok) { + m_totals.assertions.passed++; + m_lastAssertionPassed = true; + } else if (!result.isOk()) { + m_lastAssertionPassed = false; + if( m_activeTestCase->getTestCaseInfo().okToFail() ) + m_totals.assertions.failedButOk++; + else + m_totals.assertions.failed++; + } + else { + m_lastAssertionPassed = true; + } + + // We have no use for the return value (whether messages should be cleared), because messages were made scoped + // and should be let to clear themselves out. + static_cast<void>(m_reporter->assertionEnded(AssertionStats(result, m_messages, m_totals))); + + // Reset working state + resetAssertionInfo(); + m_lastResult = result; + } + void RunContext::resetAssertionInfo() { + m_lastAssertionInfo.macroName = StringRef(); + m_lastAssertionInfo.capturedExpression = "{Unknown expression after the reported line}"_sr; + } + + bool RunContext::sectionStarted(SectionInfo const & sectionInfo, Counts & assertions) { + ITracker& sectionTracker = SectionTracker::acquire(m_trackerContext, TestCaseTracking::NameAndLocation(sectionInfo.name, sectionInfo.lineInfo)); + if (!sectionTracker.isOpen()) + return false; + m_activeSections.push_back(§ionTracker); + + m_lastAssertionInfo.lineInfo = sectionInfo.lineInfo; + + m_reporter->sectionStarting(sectionInfo); + + assertions = m_totals.assertions; + + return true; + } + auto RunContext::acquireGeneratorTracker( SourceLineInfo const& lineInfo ) -> IGeneratorTracker& { + using namespace Generators; + GeneratorTracker& tracker = GeneratorTracker::acquire( m_trackerContext, TestCaseTracking::NameAndLocation( "generator", lineInfo ) ); + assert( tracker.isOpen() ); + m_lastAssertionInfo.lineInfo = lineInfo; + return tracker; + } + + bool RunContext::testForMissingAssertions(Counts& assertions) { + if (assertions.total() != 0) + return false; + if (!m_config->warnAboutMissingAssertions()) + return false; + if (m_trackerContext.currentTracker().hasChildren()) + return false; + m_totals.assertions.failed++; + assertions.failed++; + return true; + } + + void RunContext::sectionEnded(SectionEndInfo const & endInfo) { + Counts assertions = m_totals.assertions - endInfo.prevAssertions; + bool missingAssertions = testForMissingAssertions(assertions); + + if (!m_activeSections.empty()) { + m_activeSections.back()->close(); + m_activeSections.pop_back(); + } + + m_reporter->sectionEnded(SectionStats(endInfo.sectionInfo, assertions, endInfo.durationInSeconds, missingAssertions)); + m_messages.clear(); + } + + void RunContext::sectionEndedEarly(SectionEndInfo const & endInfo) { + if (m_unfinishedSections.empty()) + m_activeSections.back()->fail(); + else + m_activeSections.back()->close(); + m_activeSections.pop_back(); + + m_unfinishedSections.push_back(endInfo); + } + void RunContext::benchmarkStarting( BenchmarkInfo const& info ) { + m_reporter->benchmarkStarting( info ); + } + void RunContext::benchmarkEnded( BenchmarkStats const& stats ) { + m_reporter->benchmarkEnded( stats ); + } + + void RunContext::pushScopedMessage(MessageInfo const & message) { + m_messages.push_back(message); + } + + void RunContext::popScopedMessage(MessageInfo const & message) { + m_messages.erase(std::remove(m_messages.begin(), m_messages.end(), message), m_messages.end()); + } + + std::string RunContext::getCurrentTestName() const { + return m_activeTestCase + ? m_activeTestCase->getTestCaseInfo().name + : std::string(); + } + + const AssertionResult * RunContext::getLastResult() const { + return &(*m_lastResult); + } + + void RunContext::exceptionEarlyReported() { + m_shouldReportUnexpected = false; + } + + void RunContext::handleFatalErrorCondition( StringRef message ) { + // First notify reporter that bad things happened + m_reporter->fatalErrorEncountered(message); + + // Don't rebuild the result -- the stringification itself can cause more fatal errors + // Instead, fake a result data. + AssertionResultData tempResult( ResultWas::FatalErrorCondition, { false } ); + tempResult.message = message; + AssertionResult result(m_lastAssertionInfo, tempResult); + + assertionEnded(result); + + handleUnfinishedSections(); + + // Recreate section for test case (as we will lose the one that was in scope) + auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); + SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name); + + Counts assertions; + assertions.failed = 1; + SectionStats testCaseSectionStats(testCaseSection, assertions, 0, false); + m_reporter->sectionEnded(testCaseSectionStats); + + auto const& testInfo = m_activeTestCase->getTestCaseInfo(); + + Totals deltaTotals; + deltaTotals.testCases.failed = 1; + deltaTotals.assertions.failed = 1; + m_reporter->testCaseEnded(TestCaseStats(testInfo, + deltaTotals, + std::string(), + std::string(), + false)); + m_totals.testCases.failed++; + testGroupEnded(std::string(), m_totals, 1, 1); + m_reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, false)); + } + + bool RunContext::lastAssertionPassed() { + return m_lastAssertionPassed; + } + + void RunContext::assertionPassed() { + m_lastAssertionPassed = true; + ++m_totals.assertions.passed; + resetAssertionInfo(); + } + + bool RunContext::aborting() const { + return m_totals.assertions.failed >= static_cast<std::size_t>(m_config->abortAfter()); + } + + void RunContext::runCurrentTest(std::string & redirectedCout, std::string & redirectedCerr) { + auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); + SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name); + m_reporter->sectionStarting(testCaseSection); + Counts prevAssertions = m_totals.assertions; + double duration = 0; + m_shouldReportUnexpected = true; + m_lastAssertionInfo = { "TEST_CASE"_sr, testCaseInfo.lineInfo, StringRef(), ResultDisposition::Normal }; + + seedRng(*m_config); + + Timer timer; + CATCH_TRY { + if (m_reporter->getPreferences().shouldRedirectStdOut) { +#if !defined(CATCH_CONFIG_EXPERIMENTAL_REDIRECT) + RedirectedStdOut redirectedStdOut; + RedirectedStdErr redirectedStdErr; + + timer.start(); + invokeActiveTestCase(); + redirectedCout += redirectedStdOut.str(); + redirectedCerr += redirectedStdErr.str(); +#else + OutputRedirect r(redirectedCout, redirectedCerr); + timer.start(); + invokeActiveTestCase(); +#endif + } else { + timer.start(); + invokeActiveTestCase(); + } + duration = timer.getElapsedSeconds(); + } CATCH_CATCH_ANON (TestFailureException&) { + // This just means the test was aborted due to failure + } CATCH_CATCH_ALL { + // Under CATCH_CONFIG_FAST_COMPILE, unexpected exceptions under REQUIRE assertions + // are reported without translation at the point of origin. + if( m_shouldReportUnexpected ) { + AssertionReaction dummyReaction; + handleUnexpectedInflightException( m_lastAssertionInfo, translateActiveException(), dummyReaction ); + } + } + Counts assertions = m_totals.assertions - prevAssertions; + bool missingAssertions = testForMissingAssertions(assertions); + + m_testCaseTracker->close(); + handleUnfinishedSections(); + m_messages.clear(); + + SectionStats testCaseSectionStats(testCaseSection, assertions, duration, missingAssertions); + m_reporter->sectionEnded(testCaseSectionStats); + } + + void RunContext::invokeActiveTestCase() { + FatalConditionHandler fatalConditionHandler; // Handle signals + m_activeTestCase->invoke(); + fatalConditionHandler.reset(); + } + + void RunContext::handleUnfinishedSections() { + // If sections ended prematurely due to an exception we stored their + // infos here so we can tear them down outside the unwind process. + for (auto it = m_unfinishedSections.rbegin(), + itEnd = m_unfinishedSections.rend(); + it != itEnd; + ++it) + sectionEnded(*it); + m_unfinishedSections.clear(); + } + + void RunContext::handleExpr( + AssertionInfo const& info, + ITransientExpression const& expr, + AssertionReaction& reaction + ) { + m_reporter->assertionStarting( info ); + + bool negated = isFalseTest( info.resultDisposition ); + bool result = expr.getResult() != negated; + + if( result ) { + if (!m_includeSuccessfulResults) { + assertionPassed(); + } + else { + reportExpr(info, ResultWas::Ok, &expr, negated); + } + } + else { + reportExpr(info, ResultWas::ExpressionFailed, &expr, negated ); + populateReaction( reaction ); + } + } + void RunContext::reportExpr( + AssertionInfo const &info, + ResultWas::OfType resultType, + ITransientExpression const *expr, + bool negated ) { + + m_lastAssertionInfo = info; + AssertionResultData data( resultType, LazyExpression( negated ) ); + + AssertionResult assertionResult{ info, data }; + assertionResult.m_resultData.lazyExpression.m_transientExpression = expr; + + assertionEnded( assertionResult ); + } + + void RunContext::handleMessage( + AssertionInfo const& info, + ResultWas::OfType resultType, + StringRef const& message, + AssertionReaction& reaction + ) { + m_reporter->assertionStarting( info ); + + m_lastAssertionInfo = info; + + AssertionResultData data( resultType, LazyExpression( false ) ); + data.message = message; + AssertionResult assertionResult{ m_lastAssertionInfo, data }; + assertionEnded( assertionResult ); + if( !assertionResult.isOk() ) + populateReaction( reaction ); + } + void RunContext::handleUnexpectedExceptionNotThrown( + AssertionInfo const& info, + AssertionReaction& reaction + ) { + handleNonExpr(info, Catch::ResultWas::DidntThrowException, reaction); + } + + void RunContext::handleUnexpectedInflightException( + AssertionInfo const& info, + std::string const& message, + AssertionReaction& reaction + ) { + m_lastAssertionInfo = info; + + AssertionResultData data( ResultWas::ThrewException, LazyExpression( false ) ); + data.message = message; + AssertionResult assertionResult{ info, data }; + assertionEnded( assertionResult ); + populateReaction( reaction ); + } + + void RunContext::populateReaction( AssertionReaction& reaction ) { + reaction.shouldDebugBreak = m_config->shouldDebugBreak(); + reaction.shouldThrow = aborting() || (m_lastAssertionInfo.resultDisposition & ResultDisposition::Normal); + } + + void RunContext::handleIncomplete( + AssertionInfo const& info + ) { + m_lastAssertionInfo = info; + + AssertionResultData data( ResultWas::ThrewException, LazyExpression( false ) ); + data.message = "Exception translation was disabled by CATCH_CONFIG_FAST_COMPILE"; + AssertionResult assertionResult{ info, data }; + assertionEnded( assertionResult ); + } + void RunContext::handleNonExpr( + AssertionInfo const &info, + ResultWas::OfType resultType, + AssertionReaction &reaction + ) { + m_lastAssertionInfo = info; + + AssertionResultData data( resultType, LazyExpression( false ) ); + AssertionResult assertionResult{ info, data }; + assertionEnded( assertionResult ); + + if( !assertionResult.isOk() ) + populateReaction( reaction ); + } + + IResultCapture& getResultCapture() { + if (auto* capture = getCurrentContext().getResultCapture()) + return *capture; + else + CATCH_INTERNAL_ERROR("No result capture instance"); + } +} +// end catch_run_context.cpp +// start catch_section.cpp + +namespace Catch { + + Section::Section( SectionInfo const& info ) + : m_info( info ), + m_sectionIncluded( getResultCapture().sectionStarted( m_info, m_assertions ) ) + { + m_timer.start(); + } + + Section::~Section() { + if( m_sectionIncluded ) { + SectionEndInfo endInfo{ m_info, m_assertions, m_timer.getElapsedSeconds() }; + if( uncaught_exceptions() ) + getResultCapture().sectionEndedEarly( endInfo ); + else + getResultCapture().sectionEnded( endInfo ); + } + } + + // This indicates whether the section should be executed or not + Section::operator bool() const { + return m_sectionIncluded; + } + +} // end namespace Catch +// end catch_section.cpp +// start catch_section_info.cpp + +namespace Catch { + + SectionInfo::SectionInfo + ( SourceLineInfo const& _lineInfo, + std::string const& _name ) + : name( _name ), + lineInfo( _lineInfo ) + {} + +} // end namespace Catch +// end catch_section_info.cpp +// start catch_session.cpp + +// start catch_session.h + +#include <memory> + +namespace Catch { + + class Session : NonCopyable { + public: + + Session(); + ~Session() override; + + void showHelp() const; + void libIdentify(); + + int applyCommandLine( int argc, char const * const * argv ); + #if defined(CATCH_CONFIG_WCHAR) && defined(WIN32) && defined(UNICODE) + int applyCommandLine( int argc, wchar_t const * const * argv ); + #endif + + void useConfigData( ConfigData const& configData ); + + template<typename CharT> + int run(int argc, CharT const * const argv[]) { + if (m_startupExceptions) + return 1; + int returnCode = applyCommandLine(argc, argv); + if (returnCode == 0) + returnCode = run(); + return returnCode; + } + + int run(); + + clara::Parser const& cli() const; + void cli( clara::Parser const& newParser ); + ConfigData& configData(); + Config& config(); + private: + int runInternal(); + + clara::Parser m_cli; + ConfigData m_configData; + std::shared_ptr<Config> m_config; + bool m_startupExceptions = false; + }; + +} // end namespace Catch + +// end catch_session.h +// start catch_version.h + +#include <iosfwd> + +namespace Catch { + + // Versioning information + struct Version { + Version( Version const& ) = delete; + Version& operator=( Version const& ) = delete; + Version( unsigned int _majorVersion, + unsigned int _minorVersion, + unsigned int _patchNumber, + char const * const _branchName, + unsigned int _buildNumber ); + + unsigned int const majorVersion; + unsigned int const minorVersion; + unsigned int const patchNumber; + + // buildNumber is only used if branchName is not null + char const * const branchName; + unsigned int const buildNumber; + + friend std::ostream& operator << ( std::ostream& os, Version const& version ); + }; + + Version const& libraryVersion(); +} + +// end catch_version.h +#include <cstdlib> +#include <iomanip> + +namespace Catch { + + namespace { + const int MaxExitCode = 255; + + IStreamingReporterPtr createReporter(std::string const& reporterName, IConfigPtr const& config) { + auto reporter = Catch::getRegistryHub().getReporterRegistry().create(reporterName, config); + CATCH_ENFORCE(reporter, "No reporter registered with name: '" << reporterName << "'"); + + return reporter; + } + + IStreamingReporterPtr makeReporter(std::shared_ptr<Config> const& config) { + if (Catch::getRegistryHub().getReporterRegistry().getListeners().empty()) { + return createReporter(config->getReporterName(), config); + } + + auto multi = std::unique_ptr<ListeningReporter>(new ListeningReporter); + + auto const& listeners = Catch::getRegistryHub().getReporterRegistry().getListeners(); + for (auto const& listener : listeners) { + multi->addListener(listener->create(Catch::ReporterConfig(config))); + } + multi->addReporter(createReporter(config->getReporterName(), config)); + return std::move(multi); + } + + Catch::Totals runTests(std::shared_ptr<Config> const& config) { + auto reporter = makeReporter(config); + + RunContext context(config, std::move(reporter)); + + Totals totals; + + context.testGroupStarting(config->name(), 1, 1); + + TestSpec testSpec = config->testSpec(); + + auto const& allTestCases = getAllTestCasesSorted(*config); + for (auto const& testCase : allTestCases) { + if (!context.aborting() && matchTest(testCase, testSpec, *config)) + totals += context.runTest(testCase); + else + context.reporter().skipTest(testCase); + } + + if (config->warnAboutNoTests() && totals.testCases.total() == 0) { + ReusableStringStream testConfig; + + bool first = true; + for (const auto& input : config->getTestsOrTags()) { + if (!first) { testConfig << ' '; } + first = false; + testConfig << input; + } + + context.reporter().noMatchingTestCases(testConfig.str()); + totals.error = -1; + } + + context.testGroupEnded(config->name(), totals, 1, 1); + return totals; + } + + void applyFilenamesAsTags(Catch::IConfig const& config) { + auto& tests = const_cast<std::vector<TestCase>&>(getAllTestCasesSorted(config)); + for (auto& testCase : tests) { + auto tags = testCase.tags; + + std::string filename = testCase.lineInfo.file; + auto lastSlash = filename.find_last_of("\\/"); + if (lastSlash != std::string::npos) { + filename.erase(0, lastSlash); + filename[0] = '#'; + } + + auto lastDot = filename.find_last_of('.'); + if (lastDot != std::string::npos) { + filename.erase(lastDot); + } + + tags.push_back(std::move(filename)); + setTags(testCase, tags); + } + } + + } // anon namespace + + Session::Session() { + static bool alreadyInstantiated = false; + if( alreadyInstantiated ) { + CATCH_TRY { CATCH_INTERNAL_ERROR( "Only one instance of Catch::Session can ever be used" ); } + CATCH_CATCH_ALL { getMutableRegistryHub().registerStartupException(); } + } + + // There cannot be exceptions at startup in no-exception mode. +#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) + const auto& exceptions = getRegistryHub().getStartupExceptionRegistry().getExceptions(); + if ( !exceptions.empty() ) { + m_startupExceptions = true; + Colour colourGuard( Colour::Red ); + Catch::cerr() << "Errors occurred during startup!" << '\n'; + // iterate over all exceptions and notify user + for ( const auto& ex_ptr : exceptions ) { + try { + std::rethrow_exception(ex_ptr); + } catch ( std::exception const& ex ) { + Catch::cerr() << Column( ex.what() ).indent(2) << '\n'; + } + } + } +#endif + + alreadyInstantiated = true; + m_cli = makeCommandLineParser( m_configData ); + } + Session::~Session() { + Catch::cleanUp(); + } + + void Session::showHelp() const { + Catch::cout() + << "\nCatch v" << libraryVersion() << "\n" + << m_cli << std::endl + << "For more detailed usage please see the project docs\n" << std::endl; + } + void Session::libIdentify() { + Catch::cout() + << std::left << std::setw(16) << "description: " << "A Catch test executable\n" + << std::left << std::setw(16) << "category: " << "testframework\n" + << std::left << std::setw(16) << "framework: " << "Catch Test\n" + << std::left << std::setw(16) << "version: " << libraryVersion() << std::endl; + } + + int Session::applyCommandLine( int argc, char const * const * argv ) { + if( m_startupExceptions ) + return 1; + + auto result = m_cli.parse( clara::Args( argc, argv ) ); + if( !result ) { + Catch::cerr() + << Colour( Colour::Red ) + << "\nError(s) in input:\n" + << Column( result.errorMessage() ).indent( 2 ) + << "\n\n"; + Catch::cerr() << "Run with -? for usage\n" << std::endl; + return MaxExitCode; + } + + if( m_configData.showHelp ) + showHelp(); + if( m_configData.libIdentify ) + libIdentify(); + m_config.reset(); + return 0; + } + +#if defined(CATCH_CONFIG_WCHAR) && defined(WIN32) && defined(UNICODE) + int Session::applyCommandLine( int argc, wchar_t const * const * argv ) { + + char **utf8Argv = new char *[ argc ]; + + for ( int i = 0; i < argc; ++i ) { + int bufSize = WideCharToMultiByte( CP_UTF8, 0, argv[i], -1, NULL, 0, NULL, NULL ); + + utf8Argv[ i ] = new char[ bufSize ]; + + WideCharToMultiByte( CP_UTF8, 0, argv[i], -1, utf8Argv[i], bufSize, NULL, NULL ); + } + + int returnCode = applyCommandLine( argc, utf8Argv ); + + for ( int i = 0; i < argc; ++i ) + delete [] utf8Argv[ i ]; + + delete [] utf8Argv; + + return returnCode; + } +#endif + + void Session::useConfigData( ConfigData const& configData ) { + m_configData = configData; + m_config.reset(); + } + + int Session::run() { + if( ( m_configData.waitForKeypress & WaitForKeypress::BeforeStart ) != 0 ) { + Catch::cout() << "...waiting for enter/ return before starting" << std::endl; + static_cast<void>(std::getchar()); + } + int exitCode = runInternal(); + if( ( m_configData.waitForKeypress & WaitForKeypress::BeforeExit ) != 0 ) { + Catch::cout() << "...waiting for enter/ return before exiting, with code: " << exitCode << std::endl; + static_cast<void>(std::getchar()); + } + return exitCode; + } + + clara::Parser const& Session::cli() const { + return m_cli; + } + void Session::cli( clara::Parser const& newParser ) { + m_cli = newParser; + } + ConfigData& Session::configData() { + return m_configData; + } + Config& Session::config() { + if( !m_config ) + m_config = std::make_shared<Config>( m_configData ); + return *m_config; + } + + int Session::runInternal() { + if( m_startupExceptions ) + return 1; + + if (m_configData.showHelp || m_configData.libIdentify) { + return 0; + } + + CATCH_TRY { + config(); // Force config to be constructed + + seedRng( *m_config ); + + if( m_configData.filenamesAsTags ) + applyFilenamesAsTags( *m_config ); + + // Handle list request + if( Option<std::size_t> listed = list( config() ) ) + return static_cast<int>( *listed ); + + auto totals = runTests( m_config ); + // Note that on unices only the lower 8 bits are usually used, clamping + // the return value to 255 prevents false negative when some multiple + // of 256 tests has failed + return (std::min) (MaxExitCode, (std::max) (totals.error, static_cast<int>(totals.assertions.failed))); + } +#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) + catch( std::exception& ex ) { + Catch::cerr() << ex.what() << std::endl; + return MaxExitCode; + } +#endif + } + +} // end namespace Catch +// end catch_session.cpp +// start catch_singletons.cpp + +#include <vector> + +namespace Catch { + + namespace { + static auto getSingletons() -> std::vector<ISingleton*>*& { + static std::vector<ISingleton*>* g_singletons = nullptr; + if( !g_singletons ) + g_singletons = new std::vector<ISingleton*>(); + return g_singletons; + } + } + + ISingleton::~ISingleton() {} + + void addSingleton(ISingleton* singleton ) { + getSingletons()->push_back( singleton ); + } + void cleanupSingletons() { + auto& singletons = getSingletons(); + for( auto singleton : *singletons ) + delete singleton; + delete singletons; + singletons = nullptr; + } + +} // namespace Catch +// end catch_singletons.cpp +// start catch_startup_exception_registry.cpp + +namespace Catch { +void StartupExceptionRegistry::add( std::exception_ptr const& exception ) noexcept { + CATCH_TRY { + m_exceptions.push_back(exception); + } CATCH_CATCH_ALL { + // If we run out of memory during start-up there's really not a lot more we can do about it + std::terminate(); + } + } + + std::vector<std::exception_ptr> const& StartupExceptionRegistry::getExceptions() const noexcept { + return m_exceptions; + } + +} // end namespace Catch +// end catch_startup_exception_registry.cpp +// start catch_stream.cpp + +#include <cstdio> +#include <iostream> +#include <fstream> +#include <sstream> +#include <vector> +#include <memory> + +namespace Catch { + + Catch::IStream::~IStream() = default; + + namespace detail { namespace { + template<typename WriterF, std::size_t bufferSize=256> + class StreamBufImpl : public std::streambuf { + char data[bufferSize]; + WriterF m_writer; + + public: + StreamBufImpl() { + setp( data, data + sizeof(data) ); + } + + ~StreamBufImpl() noexcept { + StreamBufImpl::sync(); + } + + private: + int overflow( int c ) override { + sync(); + + if( c != EOF ) { + if( pbase() == epptr() ) + m_writer( std::string( 1, static_cast<char>( c ) ) ); + else + sputc( static_cast<char>( c ) ); + } + return 0; + } + + int sync() override { + if( pbase() != pptr() ) { + m_writer( std::string( pbase(), static_cast<std::string::size_type>( pptr() - pbase() ) ) ); + setp( pbase(), epptr() ); + } + return 0; + } + }; + + /////////////////////////////////////////////////////////////////////////// + + struct OutputDebugWriter { + + void operator()( std::string const&str ) { + writeToDebugConsole( str ); + } + }; + + /////////////////////////////////////////////////////////////////////////// + + class FileStream : public IStream { + mutable std::ofstream m_ofs; + public: + FileStream( StringRef filename ) { + m_ofs.open( filename.c_str() ); + CATCH_ENFORCE( !m_ofs.fail(), "Unable to open file: '" << filename << "'" ); + } + ~FileStream() override = default; + public: // IStream + std::ostream& stream() const override { + return m_ofs; + } + }; + + /////////////////////////////////////////////////////////////////////////// + + class CoutStream : public IStream { + mutable std::ostream m_os; + public: + // Store the streambuf from cout up-front because + // cout may get redirected when running tests + CoutStream() : m_os( Catch::cout().rdbuf() ) {} + ~CoutStream() override = default; + + public: // IStream + std::ostream& stream() const override { return m_os; } + }; + + /////////////////////////////////////////////////////////////////////////// + + class DebugOutStream : public IStream { + std::unique_ptr<StreamBufImpl<OutputDebugWriter>> m_streamBuf; + mutable std::ostream m_os; + public: + DebugOutStream() + : m_streamBuf( new StreamBufImpl<OutputDebugWriter>() ), + m_os( m_streamBuf.get() ) + {} + + ~DebugOutStream() override = default; + + public: // IStream + std::ostream& stream() const override { return m_os; } + }; + + }} // namespace anon::detail + + /////////////////////////////////////////////////////////////////////////// + + auto makeStream( StringRef const &filename ) -> IStream const* { + if( filename.empty() ) + return new detail::CoutStream(); + else if( filename[0] == '%' ) { + if( filename == "%debug" ) + return new detail::DebugOutStream(); + else + CATCH_ERROR( "Unrecognised stream: '" << filename << "'" ); + } + else + return new detail::FileStream( filename ); + } + + // This class encapsulates the idea of a pool of ostringstreams that can be reused. + struct StringStreams { + std::vector<std::unique_ptr<std::ostringstream>> m_streams; + std::vector<std::size_t> m_unused; + std::ostringstream m_referenceStream; // Used for copy state/ flags from + + auto add() -> std::size_t { + if( m_unused.empty() ) { + m_streams.push_back( std::unique_ptr<std::ostringstream>( new std::ostringstream ) ); + return m_streams.size()-1; + } + else { + auto index = m_unused.back(); + m_unused.pop_back(); + return index; + } + } + + void release( std::size_t index ) { + m_streams[index]->copyfmt( m_referenceStream ); // Restore initial flags and other state + m_unused.push_back(index); + } + }; + + ReusableStringStream::ReusableStringStream() + : m_index( Singleton<StringStreams>::getMutable().add() ), + m_oss( Singleton<StringStreams>::getMutable().m_streams[m_index].get() ) + {} + + ReusableStringStream::~ReusableStringStream() { + static_cast<std::ostringstream*>( m_oss )->str(""); + m_oss->clear(); + Singleton<StringStreams>::getMutable().release( m_index ); + } + + auto ReusableStringStream::str() const -> std::string { + return static_cast<std::ostringstream*>( m_oss )->str(); + } + + /////////////////////////////////////////////////////////////////////////// + +#ifndef CATCH_CONFIG_NOSTDOUT // If you #define this you must implement these functions + std::ostream& cout() { return std::cout; } + std::ostream& cerr() { return std::cerr; } + std::ostream& clog() { return std::clog; } +#endif +} +// end catch_stream.cpp +// start catch_string_manip.cpp + +#include <algorithm> +#include <ostream> +#include <cstring> +#include <cctype> + +namespace Catch { + + namespace { + char toLowerCh(char c) { + return static_cast<char>( std::tolower( c ) ); + } + } + + bool startsWith( std::string const& s, std::string const& prefix ) { + return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin()); + } + bool startsWith( std::string const& s, char prefix ) { + return !s.empty() && s[0] == prefix; + } + bool endsWith( std::string const& s, std::string const& suffix ) { + return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin()); + } + bool endsWith( std::string const& s, char suffix ) { + return !s.empty() && s[s.size()-1] == suffix; + } + bool contains( std::string const& s, std::string const& infix ) { + return s.find( infix ) != std::string::npos; + } + void toLowerInPlace( std::string& s ) { + std::transform( s.begin(), s.end(), s.begin(), toLowerCh ); + } + std::string toLower( std::string const& s ) { + std::string lc = s; + toLowerInPlace( lc ); + return lc; + } + std::string trim( std::string const& str ) { + static char const* whitespaceChars = "\n\r\t "; + std::string::size_type start = str.find_first_not_of( whitespaceChars ); + std::string::size_type end = str.find_last_not_of( whitespaceChars ); + + return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string(); + } + + bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) { + bool replaced = false; + std::size_t i = str.find( replaceThis ); + while( i != std::string::npos ) { + replaced = true; + str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() ); + if( i < str.size()-withThis.size() ) + i = str.find( replaceThis, i+withThis.size() ); + else + i = std::string::npos; + } + return replaced; + } + + pluralise::pluralise( std::size_t count, std::string const& label ) + : m_count( count ), + m_label( label ) + {} + + std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) { + os << pluraliser.m_count << ' ' << pluraliser.m_label; + if( pluraliser.m_count != 1 ) + os << 's'; + return os; + } + +} +// end catch_string_manip.cpp +// start catch_stringref.cpp + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wexit-time-destructors" +#endif + +#include <ostream> +#include <cstring> +#include <cstdint> + +namespace { + const uint32_t byte_2_lead = 0xC0; + const uint32_t byte_3_lead = 0xE0; + const uint32_t byte_4_lead = 0xF0; +} + +namespace Catch { + StringRef::StringRef( char const* rawChars ) noexcept + : StringRef( rawChars, static_cast<StringRef::size_type>(std::strlen(rawChars) ) ) + {} + + StringRef::operator std::string() const { + return std::string( m_start, m_size ); + } + + void StringRef::swap( StringRef& other ) noexcept { + std::swap( m_start, other.m_start ); + std::swap( m_size, other.m_size ); + std::swap( m_data, other.m_data ); + } + + auto StringRef::c_str() const -> char const* { + if( isSubstring() ) + const_cast<StringRef*>( this )->takeOwnership(); + return m_start; + } + auto StringRef::currentData() const noexcept -> char const* { + return m_start; + } + + auto StringRef::isOwned() const noexcept -> bool { + return m_data != nullptr; + } + auto StringRef::isSubstring() const noexcept -> bool { + return m_start[m_size] != '\0'; + } + + void StringRef::takeOwnership() { + if( !isOwned() ) { + m_data = new char[m_size+1]; + memcpy( m_data, m_start, m_size ); + m_data[m_size] = '\0'; + m_start = m_data; + } + } + auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef { + if( start < m_size ) + return StringRef( m_start+start, size ); + else + return StringRef(); + } + auto StringRef::operator == ( StringRef const& other ) const noexcept -> bool { + return + size() == other.size() && + (std::strncmp( m_start, other.m_start, size() ) == 0); + } + auto StringRef::operator != ( StringRef const& other ) const noexcept -> bool { + return !operator==( other ); + } + + auto StringRef::operator[](size_type index) const noexcept -> char { + return m_start[index]; + } + + auto StringRef::numberOfCharacters() const noexcept -> size_type { + size_type noChars = m_size; + // Make adjustments for uft encodings + for( size_type i=0; i < m_size; ++i ) { + char c = m_start[i]; + if( ( c & byte_2_lead ) == byte_2_lead ) { + noChars--; + if (( c & byte_3_lead ) == byte_3_lead ) + noChars--; + if( ( c & byte_4_lead ) == byte_4_lead ) + noChars--; + } + } + return noChars; + } + + auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string { + std::string str; + str.reserve( lhs.size() + rhs.size() ); + str += lhs; + str += rhs; + return str; + } + auto operator + ( StringRef const& lhs, const char* rhs ) -> std::string { + return std::string( lhs ) + std::string( rhs ); + } + auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string { + return std::string( lhs ) + std::string( rhs ); + } + + auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& { + return os.write(str.currentData(), str.size()); + } + + auto operator+=( std::string& lhs, StringRef const& rhs ) -> std::string& { + lhs.append(rhs.currentData(), rhs.size()); + return lhs; + } + +} // namespace Catch + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif +// end catch_stringref.cpp +// start catch_tag_alias.cpp + +namespace Catch { + TagAlias::TagAlias(std::string const & _tag, SourceLineInfo _lineInfo): tag(_tag), lineInfo(_lineInfo) {} +} +// end catch_tag_alias.cpp +// start catch_tag_alias_autoregistrar.cpp + +namespace Catch { + + RegistrarForTagAliases::RegistrarForTagAliases(char const* alias, char const* tag, SourceLineInfo const& lineInfo) { + CATCH_TRY { + getMutableRegistryHub().registerTagAlias(alias, tag, lineInfo); + } CATCH_CATCH_ALL { + // Do not throw when constructing global objects, instead register the exception to be processed later + getMutableRegistryHub().registerStartupException(); + } + } + +} +// end catch_tag_alias_autoregistrar.cpp +// start catch_tag_alias_registry.cpp + +#include <sstream> + +namespace Catch { + + TagAliasRegistry::~TagAliasRegistry() {} + + TagAlias const* TagAliasRegistry::find( std::string const& alias ) const { + auto it = m_registry.find( alias ); + if( it != m_registry.end() ) + return &(it->second); + else + return nullptr; + } + + std::string TagAliasRegistry::expandAliases( std::string const& unexpandedTestSpec ) const { + std::string expandedTestSpec = unexpandedTestSpec; + for( auto const& registryKvp : m_registry ) { + std::size_t pos = expandedTestSpec.find( registryKvp.first ); + if( pos != std::string::npos ) { + expandedTestSpec = expandedTestSpec.substr( 0, pos ) + + registryKvp.second.tag + + expandedTestSpec.substr( pos + registryKvp.first.size() ); + } + } + return expandedTestSpec; + } + + void TagAliasRegistry::add( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) { + CATCH_ENFORCE( startsWith(alias, "[@") && endsWith(alias, ']'), + "error: tag alias, '" << alias << "' is not of the form [@alias name].\n" << lineInfo ); + + CATCH_ENFORCE( m_registry.insert(std::make_pair(alias, TagAlias(tag, lineInfo))).second, + "error: tag alias, '" << alias << "' already registered.\n" + << "\tFirst seen at: " << find(alias)->lineInfo << "\n" + << "\tRedefined at: " << lineInfo ); + } + + ITagAliasRegistry::~ITagAliasRegistry() {} + + ITagAliasRegistry const& ITagAliasRegistry::get() { + return getRegistryHub().getTagAliasRegistry(); + } + +} // end namespace Catch +// end catch_tag_alias_registry.cpp +// start catch_test_case_info.cpp + +#include <cctype> +#include <exception> +#include <algorithm> +#include <sstream> + +namespace Catch { + + namespace { + TestCaseInfo::SpecialProperties parseSpecialTag( std::string const& tag ) { + if( startsWith( tag, '.' ) || + tag == "!hide" ) + return TestCaseInfo::IsHidden; + else if( tag == "!throws" ) + return TestCaseInfo::Throws; + else if( tag == "!shouldfail" ) + return TestCaseInfo::ShouldFail; + else if( tag == "!mayfail" ) + return TestCaseInfo::MayFail; + else if( tag == "!nonportable" ) + return TestCaseInfo::NonPortable; + else if( tag == "!benchmark" ) + return static_cast<TestCaseInfo::SpecialProperties>( TestCaseInfo::Benchmark | TestCaseInfo::IsHidden ); + else + return TestCaseInfo::None; + } + bool isReservedTag( std::string const& tag ) { + return parseSpecialTag( tag ) == TestCaseInfo::None && tag.size() > 0 && !std::isalnum( static_cast<unsigned char>(tag[0]) ); + } + void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) { + CATCH_ENFORCE( !isReservedTag(tag), + "Tag name: [" << tag << "] is not allowed.\n" + << "Tag names starting with non alpha-numeric characters are reserved\n" + << _lineInfo ); + } + } + + TestCase makeTestCase( ITestInvoker* _testCase, + std::string const& _className, + NameAndTags const& nameAndTags, + SourceLineInfo const& _lineInfo ) + { + bool isHidden = false; + + // Parse out tags + std::vector<std::string> tags; + std::string desc, tag; + bool inTag = false; + std::string _descOrTags = nameAndTags.tags; + for (char c : _descOrTags) { + if( !inTag ) { + if( c == '[' ) + inTag = true; + else + desc += c; + } + else { + if( c == ']' ) { + TestCaseInfo::SpecialProperties prop = parseSpecialTag( tag ); + if( ( prop & TestCaseInfo::IsHidden ) != 0 ) + isHidden = true; + else if( prop == TestCaseInfo::None ) + enforceNotReservedTag( tag, _lineInfo ); + + tags.push_back( tag ); + tag.clear(); + inTag = false; + } + else + tag += c; + } + } + if( isHidden ) { + tags.push_back( "." ); + } + + TestCaseInfo info( nameAndTags.name, _className, desc, tags, _lineInfo ); + return TestCase( _testCase, std::move(info) ); + } + + void setTags( TestCaseInfo& testCaseInfo, std::vector<std::string> tags ) { + std::sort(begin(tags), end(tags)); + tags.erase(std::unique(begin(tags), end(tags)), end(tags)); + testCaseInfo.lcaseTags.clear(); + + for( auto const& tag : tags ) { + std::string lcaseTag = toLower( tag ); + testCaseInfo.properties = static_cast<TestCaseInfo::SpecialProperties>( testCaseInfo.properties | parseSpecialTag( lcaseTag ) ); + testCaseInfo.lcaseTags.push_back( lcaseTag ); + } + testCaseInfo.tags = std::move(tags); + } + + TestCaseInfo::TestCaseInfo( std::string const& _name, + std::string const& _className, + std::string const& _description, + std::vector<std::string> const& _tags, + SourceLineInfo const& _lineInfo ) + : name( _name ), + className( _className ), + description( _description ), + lineInfo( _lineInfo ), + properties( None ) + { + setTags( *this, _tags ); + } + + bool TestCaseInfo::isHidden() const { + return ( properties & IsHidden ) != 0; + } + bool TestCaseInfo::throws() const { + return ( properties & Throws ) != 0; + } + bool TestCaseInfo::okToFail() const { + return ( properties & (ShouldFail | MayFail ) ) != 0; + } + bool TestCaseInfo::expectedToFail() const { + return ( properties & (ShouldFail ) ) != 0; + } + + std::string TestCaseInfo::tagsAsString() const { + std::string ret; + // '[' and ']' per tag + std::size_t full_size = 2 * tags.size(); + for (const auto& tag : tags) { + full_size += tag.size(); + } + ret.reserve(full_size); + for (const auto& tag : tags) { + ret.push_back('['); + ret.append(tag); + ret.push_back(']'); + } + + return ret; + } + + TestCase::TestCase( ITestInvoker* testCase, TestCaseInfo&& info ) : TestCaseInfo( std::move(info) ), test( testCase ) {} + + TestCase TestCase::withName( std::string const& _newName ) const { + TestCase other( *this ); + other.name = _newName; + return other; + } + + void TestCase::invoke() const { + test->invoke(); + } + + bool TestCase::operator == ( TestCase const& other ) const { + return test.get() == other.test.get() && + name == other.name && + className == other.className; + } + + bool TestCase::operator < ( TestCase const& other ) const { + return name < other.name; + } + + TestCaseInfo const& TestCase::getTestCaseInfo() const + { + return *this; + } + +} // end namespace Catch +// end catch_test_case_info.cpp +// start catch_test_case_registry_impl.cpp + +#include <sstream> + +namespace Catch { + + std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) { + + std::vector<TestCase> sorted = unsortedTestCases; + + switch( config.runOrder() ) { + case RunTests::InLexicographicalOrder: + std::sort( sorted.begin(), sorted.end() ); + break; + case RunTests::InRandomOrder: + seedRng( config ); + std::shuffle( sorted.begin(), sorted.end(), rng() ); + break; + case RunTests::InDeclarationOrder: + // already in declaration order + break; + } + return sorted; + } + bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ) { + return testSpec.matches( testCase ) && ( config.allowThrows() || !testCase.throws() ); + } + + void enforceNoDuplicateTestCases( std::vector<TestCase> const& functions ) { + std::set<TestCase> seenFunctions; + for( auto const& function : functions ) { + auto prev = seenFunctions.insert( function ); + CATCH_ENFORCE( prev.second, + "error: TEST_CASE( \"" << function.name << "\" ) already defined.\n" + << "\tFirst seen at " << prev.first->getTestCaseInfo().lineInfo << "\n" + << "\tRedefined at " << function.getTestCaseInfo().lineInfo ); + } + } + + std::vector<TestCase> filterTests( std::vector<TestCase> const& testCases, TestSpec const& testSpec, IConfig const& config ) { + std::vector<TestCase> filtered; + filtered.reserve( testCases.size() ); + for( auto const& testCase : testCases ) + if( matchTest( testCase, testSpec, config ) ) + filtered.push_back( testCase ); + return filtered; + } + std::vector<TestCase> const& getAllTestCasesSorted( IConfig const& config ) { + return getRegistryHub().getTestCaseRegistry().getAllTestsSorted( config ); + } + + void TestRegistry::registerTest( TestCase const& testCase ) { + std::string name = testCase.getTestCaseInfo().name; + if( name.empty() ) { + ReusableStringStream rss; + rss << "Anonymous test case " << ++m_unnamedCount; + return registerTest( testCase.withName( rss.str() ) ); + } + m_functions.push_back( testCase ); + } + + std::vector<TestCase> const& TestRegistry::getAllTests() const { + return m_functions; + } + std::vector<TestCase> const& TestRegistry::getAllTestsSorted( IConfig const& config ) const { + if( m_sortedFunctions.empty() ) + enforceNoDuplicateTestCases( m_functions ); + + if( m_currentSortOrder != config.runOrder() || m_sortedFunctions.empty() ) { + m_sortedFunctions = sortTests( config, m_functions ); + m_currentSortOrder = config.runOrder(); + } + return m_sortedFunctions; + } + + /////////////////////////////////////////////////////////////////////////// + TestInvokerAsFunction::TestInvokerAsFunction( void(*testAsFunction)() ) noexcept : m_testAsFunction( testAsFunction ) {} + + void TestInvokerAsFunction::invoke() const { + m_testAsFunction(); + } + + std::string extractClassName( StringRef const& classOrQualifiedMethodName ) { + std::string className = classOrQualifiedMethodName; + if( startsWith( className, '&' ) ) + { + std::size_t lastColons = className.rfind( "::" ); + std::size_t penultimateColons = className.rfind( "::", lastColons-1 ); + if( penultimateColons == std::string::npos ) + penultimateColons = 1; + className = className.substr( penultimateColons, lastColons-penultimateColons ); + } + return className; + } + +} // end namespace Catch +// end catch_test_case_registry_impl.cpp +// start catch_test_case_tracker.cpp + +#include <algorithm> +#include <cassert> +#include <stdexcept> +#include <memory> +#include <sstream> + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wexit-time-destructors" +#endif + +namespace Catch { +namespace TestCaseTracking { + + NameAndLocation::NameAndLocation( std::string const& _name, SourceLineInfo const& _location ) + : name( _name ), + location( _location ) + {} + + ITracker::~ITracker() = default; + + TrackerContext& TrackerContext::instance() { + static TrackerContext s_instance; + return s_instance; + } + + ITracker& TrackerContext::startRun() { + m_rootTracker = std::make_shared<SectionTracker>( NameAndLocation( "{root}", CATCH_INTERNAL_LINEINFO ), *this, nullptr ); + m_currentTracker = nullptr; + m_runState = Executing; + return *m_rootTracker; + } + + void TrackerContext::endRun() { + m_rootTracker.reset(); + m_currentTracker = nullptr; + m_runState = NotStarted; + } + + void TrackerContext::startCycle() { + m_currentTracker = m_rootTracker.get(); + m_runState = Executing; + } + void TrackerContext::completeCycle() { + m_runState = CompletedCycle; + } + + bool TrackerContext::completedCycle() const { + return m_runState == CompletedCycle; + } + ITracker& TrackerContext::currentTracker() { + return *m_currentTracker; + } + void TrackerContext::setCurrentTracker( ITracker* tracker ) { + m_currentTracker = tracker; + } + + TrackerBase::TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ) + : m_nameAndLocation( nameAndLocation ), + m_ctx( ctx ), + m_parent( parent ) + {} + + NameAndLocation const& TrackerBase::nameAndLocation() const { + return m_nameAndLocation; + } + bool TrackerBase::isComplete() const { + return m_runState == CompletedSuccessfully || m_runState == Failed; + } + bool TrackerBase::isSuccessfullyCompleted() const { + return m_runState == CompletedSuccessfully; + } + bool TrackerBase::isOpen() const { + return m_runState != NotStarted && !isComplete(); + } + bool TrackerBase::hasChildren() const { + return !m_children.empty(); + } + + void TrackerBase::addChild( ITrackerPtr const& child ) { + m_children.push_back( child ); + } + + ITrackerPtr TrackerBase::findChild( NameAndLocation const& nameAndLocation ) { + auto it = std::find_if( m_children.begin(), m_children.end(), + [&nameAndLocation]( ITrackerPtr const& tracker ){ + return + tracker->nameAndLocation().location == nameAndLocation.location && + tracker->nameAndLocation().name == nameAndLocation.name; + } ); + return( it != m_children.end() ) + ? *it + : nullptr; + } + ITracker& TrackerBase::parent() { + assert( m_parent ); // Should always be non-null except for root + return *m_parent; + } + + void TrackerBase::openChild() { + if( m_runState != ExecutingChildren ) { + m_runState = ExecutingChildren; + if( m_parent ) + m_parent->openChild(); + } + } + + bool TrackerBase::isSectionTracker() const { return false; } + bool TrackerBase::isIndexTracker() const { return false; } + + void TrackerBase::open() { + m_runState = Executing; + moveToThis(); + if( m_parent ) + m_parent->openChild(); + } + + void TrackerBase::close() { + + // Close any still open children (e.g. generators) + while( &m_ctx.currentTracker() != this ) + m_ctx.currentTracker().close(); + + switch( m_runState ) { + case NeedsAnotherRun: + break; + + case Executing: + m_runState = CompletedSuccessfully; + break; + case ExecutingChildren: + if( m_children.empty() || m_children.back()->isComplete() ) + m_runState = CompletedSuccessfully; + break; + + case NotStarted: + case CompletedSuccessfully: + case Failed: + CATCH_INTERNAL_ERROR( "Illogical state: " << m_runState ); + + default: + CATCH_INTERNAL_ERROR( "Unknown state: " << m_runState ); + } + moveToParent(); + m_ctx.completeCycle(); + } + void TrackerBase::fail() { + m_runState = Failed; + if( m_parent ) + m_parent->markAsNeedingAnotherRun(); + moveToParent(); + m_ctx.completeCycle(); + } + void TrackerBase::markAsNeedingAnotherRun() { + m_runState = NeedsAnotherRun; + } + + void TrackerBase::moveToParent() { + assert( m_parent ); + m_ctx.setCurrentTracker( m_parent ); + } + void TrackerBase::moveToThis() { + m_ctx.setCurrentTracker( this ); + } + + SectionTracker::SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ) + : TrackerBase( nameAndLocation, ctx, parent ) + { + if( parent ) { + while( !parent->isSectionTracker() ) + parent = &parent->parent(); + + SectionTracker& parentSection = static_cast<SectionTracker&>( *parent ); + addNextFilters( parentSection.m_filters ); + } + } + + bool SectionTracker::isSectionTracker() const { return true; } + + SectionTracker& SectionTracker::acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ) { + std::shared_ptr<SectionTracker> section; + + ITracker& currentTracker = ctx.currentTracker(); + if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) { + assert( childTracker ); + assert( childTracker->isSectionTracker() ); + section = std::static_pointer_cast<SectionTracker>( childTracker ); + } + else { + section = std::make_shared<SectionTracker>( nameAndLocation, ctx, ¤tTracker ); + currentTracker.addChild( section ); + } + if( !ctx.completedCycle() ) + section->tryOpen(); + return *section; + } + + void SectionTracker::tryOpen() { + if( !isComplete() && (m_filters.empty() || m_filters[0].empty() || m_filters[0] == m_nameAndLocation.name ) ) + open(); + } + + void SectionTracker::addInitialFilters( std::vector<std::string> const& filters ) { + if( !filters.empty() ) { + m_filters.push_back(""); // Root - should never be consulted + m_filters.push_back(""); // Test Case - not a section filter + m_filters.insert( m_filters.end(), filters.begin(), filters.end() ); + } + } + void SectionTracker::addNextFilters( std::vector<std::string> const& filters ) { + if( filters.size() > 1 ) + m_filters.insert( m_filters.end(), ++filters.begin(), filters.end() ); + } + + IndexTracker::IndexTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent, int size ) + : TrackerBase( nameAndLocation, ctx, parent ), + m_size( size ) + {} + + bool IndexTracker::isIndexTracker() const { return true; } + + IndexTracker& IndexTracker::acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation, int size ) { + std::shared_ptr<IndexTracker> tracker; + + ITracker& currentTracker = ctx.currentTracker(); + if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) { + assert( childTracker ); + assert( childTracker->isIndexTracker() ); + tracker = std::static_pointer_cast<IndexTracker>( childTracker ); + } + else { + tracker = std::make_shared<IndexTracker>( nameAndLocation, ctx, ¤tTracker, size ); + currentTracker.addChild( tracker ); + } + + if( !ctx.completedCycle() && !tracker->isComplete() ) { + if( tracker->m_runState != ExecutingChildren && tracker->m_runState != NeedsAnotherRun ) + tracker->moveNext(); + tracker->open(); + } + + return *tracker; + } + + int IndexTracker::index() const { return m_index; } + + void IndexTracker::moveNext() { + m_index++; + m_children.clear(); + } + + void IndexTracker::close() { + TrackerBase::close(); + if( m_runState == CompletedSuccessfully && m_index < m_size-1 ) + m_runState = Executing; + } + +} // namespace TestCaseTracking + +using TestCaseTracking::ITracker; +using TestCaseTracking::TrackerContext; +using TestCaseTracking::SectionTracker; +using TestCaseTracking::IndexTracker; + +} // namespace Catch + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif +// end catch_test_case_tracker.cpp +// start catch_test_registry.cpp + +namespace Catch { + + auto makeTestInvoker( void(*testAsFunction)() ) noexcept -> ITestInvoker* { + return new(std::nothrow) TestInvokerAsFunction( testAsFunction ); + } + + NameAndTags::NameAndTags( StringRef const& name_ , StringRef const& tags_ ) noexcept : name( name_ ), tags( tags_ ) {} + + AutoReg::AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef const& classOrMethod, NameAndTags const& nameAndTags ) noexcept { + CATCH_TRY { + getMutableRegistryHub() + .registerTest( + makeTestCase( + invoker, + extractClassName( classOrMethod ), + nameAndTags, + lineInfo)); + } CATCH_CATCH_ALL { + // Do not throw when constructing global objects, instead register the exception to be processed later + getMutableRegistryHub().registerStartupException(); + } + } + + AutoReg::~AutoReg() = default; +} +// end catch_test_registry.cpp +// start catch_test_spec.cpp + +#include <algorithm> +#include <string> +#include <vector> +#include <memory> + +namespace Catch { + + TestSpec::Pattern::~Pattern() = default; + TestSpec::NamePattern::~NamePattern() = default; + TestSpec::TagPattern::~TagPattern() = default; + TestSpec::ExcludedPattern::~ExcludedPattern() = default; + + TestSpec::NamePattern::NamePattern( std::string const& name ) + : m_wildcardPattern( toLower( name ), CaseSensitive::No ) + {} + bool TestSpec::NamePattern::matches( TestCaseInfo const& testCase ) const { + return m_wildcardPattern.matches( toLower( testCase.name ) ); + } + + TestSpec::TagPattern::TagPattern( std::string const& tag ) : m_tag( toLower( tag ) ) {} + bool TestSpec::TagPattern::matches( TestCaseInfo const& testCase ) const { + return std::find(begin(testCase.lcaseTags), + end(testCase.lcaseTags), + m_tag) != end(testCase.lcaseTags); + } + + TestSpec::ExcludedPattern::ExcludedPattern( PatternPtr const& underlyingPattern ) : m_underlyingPattern( underlyingPattern ) {} + bool TestSpec::ExcludedPattern::matches( TestCaseInfo const& testCase ) const { return !m_underlyingPattern->matches( testCase ); } + + bool TestSpec::Filter::matches( TestCaseInfo const& testCase ) const { + // All patterns in a filter must match for the filter to be a match + for( auto const& pattern : m_patterns ) { + if( !pattern->matches( testCase ) ) + return false; + } + return true; + } + + bool TestSpec::hasFilters() const { + return !m_filters.empty(); + } + bool TestSpec::matches( TestCaseInfo const& testCase ) const { + // A TestSpec matches if any filter matches + for( auto const& filter : m_filters ) + if( filter.matches( testCase ) ) + return true; + return false; + } +} +// end catch_test_spec.cpp +// start catch_test_spec_parser.cpp + +namespace Catch { + + TestSpecParser::TestSpecParser( ITagAliasRegistry const& tagAliases ) : m_tagAliases( &tagAliases ) {} + + TestSpecParser& TestSpecParser::parse( std::string const& arg ) { + m_mode = None; + m_exclusion = false; + m_start = std::string::npos; + m_arg = m_tagAliases->expandAliases( arg ); + m_escapeChars.clear(); + for( m_pos = 0; m_pos < m_arg.size(); ++m_pos ) + visitChar( m_arg[m_pos] ); + if( m_mode == Name ) + addPattern<TestSpec::NamePattern>(); + return *this; + } + TestSpec TestSpecParser::testSpec() { + addFilter(); + return m_testSpec; + } + + void TestSpecParser::visitChar( char c ) { + if( m_mode == None ) { + switch( c ) { + case ' ': return; + case '~': m_exclusion = true; return; + case '[': return startNewMode( Tag, ++m_pos ); + case '"': return startNewMode( QuotedName, ++m_pos ); + case '\\': return escape(); + default: startNewMode( Name, m_pos ); break; + } + } + if( m_mode == Name ) { + if( c == ',' ) { + addPattern<TestSpec::NamePattern>(); + addFilter(); + } + else if( c == '[' ) { + if( subString() == "exclude:" ) + m_exclusion = true; + else + addPattern<TestSpec::NamePattern>(); + startNewMode( Tag, ++m_pos ); + } + else if( c == '\\' ) + escape(); + } + else if( m_mode == EscapedName ) + m_mode = Name; + else if( m_mode == QuotedName && c == '"' ) + addPattern<TestSpec::NamePattern>(); + else if( m_mode == Tag && c == ']' ) + addPattern<TestSpec::TagPattern>(); + } + void TestSpecParser::startNewMode( Mode mode, std::size_t start ) { + m_mode = mode; + m_start = start; + } + void TestSpecParser::escape() { + if( m_mode == None ) + m_start = m_pos; + m_mode = EscapedName; + m_escapeChars.push_back( m_pos ); + } + std::string TestSpecParser::subString() const { return m_arg.substr( m_start, m_pos - m_start ); } + + void TestSpecParser::addFilter() { + if( !m_currentFilter.m_patterns.empty() ) { + m_testSpec.m_filters.push_back( m_currentFilter ); + m_currentFilter = TestSpec::Filter(); + } + } + + TestSpec parseTestSpec( std::string const& arg ) { + return TestSpecParser( ITagAliasRegistry::get() ).parse( arg ).testSpec(); + } + +} // namespace Catch +// end catch_test_spec_parser.cpp +// start catch_timer.cpp + +#include <chrono> + +static const uint64_t nanosecondsInSecond = 1000000000; + +namespace Catch { + + auto getCurrentNanosecondsSinceEpoch() -> uint64_t { + return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count(); + } + + namespace { + auto estimateClockResolution() -> uint64_t { + uint64_t sum = 0; + static const uint64_t iterations = 1000000; + + auto startTime = getCurrentNanosecondsSinceEpoch(); + + for( std::size_t i = 0; i < iterations; ++i ) { + + uint64_t ticks; + uint64_t baseTicks = getCurrentNanosecondsSinceEpoch(); + do { + ticks = getCurrentNanosecondsSinceEpoch(); + } while( ticks == baseTicks ); + + auto delta = ticks - baseTicks; + sum += delta; + + // If we have been calibrating for over 3 seconds -- the clock + // is terrible and we should move on. + // TBD: How to signal that the measured resolution is probably wrong? + if (ticks > startTime + 3 * nanosecondsInSecond) { + return sum / i; + } + } + + // We're just taking the mean, here. To do better we could take the std. dev and exclude outliers + // - and potentially do more iterations if there's a high variance. + return sum/iterations; + } + } + auto getEstimatedClockResolution() -> uint64_t { + static auto s_resolution = estimateClockResolution(); + return s_resolution; + } + + void Timer::start() { + m_nanoseconds = getCurrentNanosecondsSinceEpoch(); + } + auto Timer::getElapsedNanoseconds() const -> uint64_t { + return getCurrentNanosecondsSinceEpoch() - m_nanoseconds; + } + auto Timer::getElapsedMicroseconds() const -> uint64_t { + return getElapsedNanoseconds()/1000; + } + auto Timer::getElapsedMilliseconds() const -> unsigned int { + return static_cast<unsigned int>(getElapsedMicroseconds()/1000); + } + auto Timer::getElapsedSeconds() const -> double { + return getElapsedMicroseconds()/1000000.0; + } + +} // namespace Catch +// end catch_timer.cpp +// start catch_tostring.cpp + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wexit-time-destructors" +# pragma clang diagnostic ignored "-Wglobal-constructors" +#endif + +// Enable specific decls locally +#if !defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) +#define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +#endif + +#include <cmath> +#include <iomanip> + +namespace Catch { + +namespace Detail { + + const std::string unprintableString = "{?}"; + + namespace { + const int hexThreshold = 255; + + struct Endianness { + enum Arch { Big, Little }; + + static Arch which() { + union _{ + int asInt; + char asChar[sizeof (int)]; + } u; + + u.asInt = 1; + return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little; + } + }; + } + + std::string rawMemoryToString( const void *object, std::size_t size ) { + // Reverse order for little endian architectures + int i = 0, end = static_cast<int>( size ), inc = 1; + if( Endianness::which() == Endianness::Little ) { + i = end-1; + end = inc = -1; + } + + unsigned char const *bytes = static_cast<unsigned char const *>(object); + ReusableStringStream rss; + rss << "0x" << std::setfill('0') << std::hex; + for( ; i != end; i += inc ) + rss << std::setw(2) << static_cast<unsigned>(bytes[i]); + return rss.str(); + } +} + +template<typename T> +std::string fpToString( T value, int precision ) { + if (Catch::isnan(value)) { + return "nan"; + } + + ReusableStringStream rss; + rss << std::setprecision( precision ) + << std::fixed + << value; + std::string d = rss.str(); + std::size_t i = d.find_last_not_of( '0' ); + if( i != std::string::npos && i != d.size()-1 ) { + if( d[i] == '.' ) + i++; + d = d.substr( 0, i+1 ); + } + return d; +} + +//// ======================================================= //// +// +// Out-of-line defs for full specialization of StringMaker +// +//// ======================================================= //// + +std::string StringMaker<std::string>::convert(const std::string& str) { + if (!getCurrentContext().getConfig()->showInvisibles()) { + return '"' + str + '"'; + } + + std::string s("\""); + for (char c : str) { + switch (c) { + case '\n': + s.append("\\n"); + break; + case '\t': + s.append("\\t"); + break; + default: + s.push_back(c); + break; + } + } + s.append("\""); + return s; +} + +#ifdef CATCH_CONFIG_CPP17_STRING_VIEW +std::string StringMaker<std::string_view>::convert(std::string_view str) { + return ::Catch::Detail::stringify(std::string{ str }); +} +#endif + +std::string StringMaker<char const*>::convert(char const* str) { + if (str) { + return ::Catch::Detail::stringify(std::string{ str }); + } else { + return{ "{null string}" }; + } +} +std::string StringMaker<char*>::convert(char* str) { + if (str) { + return ::Catch::Detail::stringify(std::string{ str }); + } else { + return{ "{null string}" }; + } +} + +#ifdef CATCH_CONFIG_WCHAR +std::string StringMaker<std::wstring>::convert(const std::wstring& wstr) { + std::string s; + s.reserve(wstr.size()); + for (auto c : wstr) { + s += (c <= 0xff) ? static_cast<char>(c) : '?'; + } + return ::Catch::Detail::stringify(s); +} + +# ifdef CATCH_CONFIG_CPP17_STRING_VIEW +std::string StringMaker<std::wstring_view>::convert(std::wstring_view str) { + return StringMaker<std::wstring>::convert(std::wstring(str)); +} +# endif + +std::string StringMaker<wchar_t const*>::convert(wchar_t const * str) { + if (str) { + return ::Catch::Detail::stringify(std::wstring{ str }); + } else { + return{ "{null string}" }; + } +} +std::string StringMaker<wchar_t *>::convert(wchar_t * str) { + if (str) { + return ::Catch::Detail::stringify(std::wstring{ str }); + } else { + return{ "{null string}" }; + } +} +#endif + +std::string StringMaker<int>::convert(int value) { + return ::Catch::Detail::stringify(static_cast<long long>(value)); +} +std::string StringMaker<long>::convert(long value) { + return ::Catch::Detail::stringify(static_cast<long long>(value)); +} +std::string StringMaker<long long>::convert(long long value) { + ReusableStringStream rss; + rss << value; + if (value > Detail::hexThreshold) { + rss << " (0x" << std::hex << value << ')'; + } + return rss.str(); +} + +std::string StringMaker<unsigned int>::convert(unsigned int value) { + return ::Catch::Detail::stringify(static_cast<unsigned long long>(value)); +} +std::string StringMaker<unsigned long>::convert(unsigned long value) { + return ::Catch::Detail::stringify(static_cast<unsigned long long>(value)); +} +std::string StringMaker<unsigned long long>::convert(unsigned long long value) { + ReusableStringStream rss; + rss << value; + if (value > Detail::hexThreshold) { + rss << " (0x" << std::hex << value << ')'; + } + return rss.str(); +} + +std::string StringMaker<bool>::convert(bool b) { + return b ? "true" : "false"; +} + +std::string StringMaker<signed char>::convert(signed char value) { + if (value == '\r') { + return "'\\r'"; + } else if (value == '\f') { + return "'\\f'"; + } else if (value == '\n') { + return "'\\n'"; + } else if (value == '\t') { + return "'\\t'"; + } else if ('\0' <= value && value < ' ') { + return ::Catch::Detail::stringify(static_cast<unsigned int>(value)); + } else { + char chstr[] = "' '"; + chstr[1] = value; + return chstr; + } +} +std::string StringMaker<char>::convert(char c) { + return ::Catch::Detail::stringify(static_cast<signed char>(c)); +} +std::string StringMaker<unsigned char>::convert(unsigned char c) { + return ::Catch::Detail::stringify(static_cast<char>(c)); +} + +std::string StringMaker<std::nullptr_t>::convert(std::nullptr_t) { + return "nullptr"; +} + +std::string StringMaker<float>::convert(float value) { + return fpToString(value, 5) + 'f'; +} +std::string StringMaker<double>::convert(double value) { + return fpToString(value, 10); +} + +std::string ratio_string<std::atto>::symbol() { return "a"; } +std::string ratio_string<std::femto>::symbol() { return "f"; } +std::string ratio_string<std::pico>::symbol() { return "p"; } +std::string ratio_string<std::nano>::symbol() { return "n"; } +std::string ratio_string<std::micro>::symbol() { return "u"; } +std::string ratio_string<std::milli>::symbol() { return "m"; } + +} // end namespace Catch + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif + +// end catch_tostring.cpp +// start catch_totals.cpp + +namespace Catch { + + Counts Counts::operator - ( Counts const& other ) const { + Counts diff; + diff.passed = passed - other.passed; + diff.failed = failed - other.failed; + diff.failedButOk = failedButOk - other.failedButOk; + return diff; + } + + Counts& Counts::operator += ( Counts const& other ) { + passed += other.passed; + failed += other.failed; + failedButOk += other.failedButOk; + return *this; + } + + std::size_t Counts::total() const { + return passed + failed + failedButOk; + } + bool Counts::allPassed() const { + return failed == 0 && failedButOk == 0; + } + bool Counts::allOk() const { + return failed == 0; + } + + Totals Totals::operator - ( Totals const& other ) const { + Totals diff; + diff.assertions = assertions - other.assertions; + diff.testCases = testCases - other.testCases; + return diff; + } + + Totals& Totals::operator += ( Totals const& other ) { + assertions += other.assertions; + testCases += other.testCases; + return *this; + } + + Totals Totals::delta( Totals const& prevTotals ) const { + Totals diff = *this - prevTotals; + if( diff.assertions.failed > 0 ) + ++diff.testCases.failed; + else if( diff.assertions.failedButOk > 0 ) + ++diff.testCases.failedButOk; + else + ++diff.testCases.passed; + return diff; + } + +} +// end catch_totals.cpp +// start catch_uncaught_exceptions.cpp + +#include <exception> + +namespace Catch { + bool uncaught_exceptions() { +#if defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) + return std::uncaught_exceptions() > 0; +#else + return std::uncaught_exception(); +#endif + } +} // end namespace Catch +// end catch_uncaught_exceptions.cpp +// start catch_version.cpp + +#include <ostream> + +namespace Catch { + + Version::Version + ( unsigned int _majorVersion, + unsigned int _minorVersion, + unsigned int _patchNumber, + char const * const _branchName, + unsigned int _buildNumber ) + : majorVersion( _majorVersion ), + minorVersion( _minorVersion ), + patchNumber( _patchNumber ), + branchName( _branchName ), + buildNumber( _buildNumber ) + {} + + std::ostream& operator << ( std::ostream& os, Version const& version ) { + os << version.majorVersion << '.' + << version.minorVersion << '.' + << version.patchNumber; + // branchName is never null -> 0th char is \0 if it is empty + if (version.branchName[0]) { + os << '-' << version.branchName + << '.' << version.buildNumber; + } + return os; + } + + Version const& libraryVersion() { + static Version version( 2, 5, 0, "", 0 ); + return version; + } + +} +// end catch_version.cpp +// start catch_wildcard_pattern.cpp + +#include <sstream> + +namespace Catch { + + WildcardPattern::WildcardPattern( std::string const& pattern, + CaseSensitive::Choice caseSensitivity ) + : m_caseSensitivity( caseSensitivity ), + m_pattern( adjustCase( pattern ) ) + { + if( startsWith( m_pattern, '*' ) ) { + m_pattern = m_pattern.substr( 1 ); + m_wildcard = WildcardAtStart; + } + if( endsWith( m_pattern, '*' ) ) { + m_pattern = m_pattern.substr( 0, m_pattern.size()-1 ); + m_wildcard = static_cast<WildcardPosition>( m_wildcard | WildcardAtEnd ); + } + } + + bool WildcardPattern::matches( std::string const& str ) const { + switch( m_wildcard ) { + case NoWildcard: + return m_pattern == adjustCase( str ); + case WildcardAtStart: + return endsWith( adjustCase( str ), m_pattern ); + case WildcardAtEnd: + return startsWith( adjustCase( str ), m_pattern ); + case WildcardAtBothEnds: + return contains( adjustCase( str ), m_pattern ); + default: + CATCH_INTERNAL_ERROR( "Unknown enum" ); + } + } + + std::string WildcardPattern::adjustCase( std::string const& str ) const { + return m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str; + } +} +// end catch_wildcard_pattern.cpp +// start catch_xmlwriter.cpp + +#include <iomanip> + +using uchar = unsigned char; + +namespace Catch { + +namespace { + + size_t trailingBytes(unsigned char c) { + if ((c & 0xE0) == 0xC0) { + return 2; + } + if ((c & 0xF0) == 0xE0) { + return 3; + } + if ((c & 0xF8) == 0xF0) { + return 4; + } + CATCH_INTERNAL_ERROR("Invalid multibyte utf-8 start byte encountered"); + } + + uint32_t headerValue(unsigned char c) { + if ((c & 0xE0) == 0xC0) { + return c & 0x1F; + } + if ((c & 0xF0) == 0xE0) { + return c & 0x0F; + } + if ((c & 0xF8) == 0xF0) { + return c & 0x07; + } + CATCH_INTERNAL_ERROR("Invalid multibyte utf-8 start byte encountered"); + } + + void hexEscapeChar(std::ostream& os, unsigned char c) { + os << "\\x" + << std::uppercase << std::hex << std::setfill('0') << std::setw(2) + << static_cast<int>(c); + } + +} // anonymous namespace + + XmlEncode::XmlEncode( std::string const& str, ForWhat forWhat ) + : m_str( str ), + m_forWhat( forWhat ) + {} + + void XmlEncode::encodeTo( std::ostream& os ) const { + // Apostrophe escaping not necessary if we always use " to write attributes + // (see: http://www.w3.org/TR/xml/#syntax) + + for( std::size_t idx = 0; idx < m_str.size(); ++ idx ) { + uchar c = m_str[idx]; + switch (c) { + case '<': os << "<"; break; + case '&': os << "&"; break; + + case '>': + // See: http://www.w3.org/TR/xml/#syntax + if (idx > 2 && m_str[idx - 1] == ']' && m_str[idx - 2] == ']') + os << ">"; + else + os << c; + break; + + case '\"': + if (m_forWhat == ForAttributes) + os << """; + else + os << c; + break; + + default: + // Check for control characters and invalid utf-8 + + // Escape control characters in standard ascii + // see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0 + if (c < 0x09 || (c > 0x0D && c < 0x20) || c == 0x7F) { + hexEscapeChar(os, c); + break; + } + + // Plain ASCII: Write it to stream + if (c < 0x7F) { + os << c; + break; + } + + // UTF-8 territory + // Check if the encoding is valid and if it is not, hex escape bytes. + // Important: We do not check the exact decoded values for validity, only the encoding format + // First check that this bytes is a valid lead byte: + // This means that it is not encoded as 1111 1XXX + // Or as 10XX XXXX + if (c < 0xC0 || + c >= 0xF8) { + hexEscapeChar(os, c); + break; + } + + auto encBytes = trailingBytes(c); + // Are there enough bytes left to avoid accessing out-of-bounds memory? + if (idx + encBytes - 1 >= m_str.size()) { + hexEscapeChar(os, c); + break; + } + // The header is valid, check data + // The next encBytes bytes must together be a valid utf-8 + // This means: bitpattern 10XX XXXX and the extracted value is sane (ish) + bool valid = true; + uint32_t value = headerValue(c); + for (std::size_t n = 1; n < encBytes; ++n) { + uchar nc = m_str[idx + n]; + valid &= ((nc & 0xC0) == 0x80); + value = (value << 6) | (nc & 0x3F); + } + + if ( + // Wrong bit pattern of following bytes + (!valid) || + // Overlong encodings + (value < 0x80) || + (0x80 <= value && value < 0x800 && encBytes > 2) || + (0x800 < value && value < 0x10000 && encBytes > 3) || + // Encoded value out of range + (value >= 0x110000) + ) { + hexEscapeChar(os, c); + break; + } + + // If we got here, this is in fact a valid(ish) utf-8 sequence + for (std::size_t n = 0; n < encBytes; ++n) { + os << m_str[idx + n]; + } + idx += encBytes - 1; + break; + } + } + } + + std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode ) { + xmlEncode.encodeTo( os ); + return os; + } + + XmlWriter::ScopedElement::ScopedElement( XmlWriter* writer ) + : m_writer( writer ) + {} + + XmlWriter::ScopedElement::ScopedElement( ScopedElement&& other ) noexcept + : m_writer( other.m_writer ){ + other.m_writer = nullptr; + } + XmlWriter::ScopedElement& XmlWriter::ScopedElement::operator=( ScopedElement&& other ) noexcept { + if ( m_writer ) { + m_writer->endElement(); + } + m_writer = other.m_writer; + other.m_writer = nullptr; + return *this; + } + + XmlWriter::ScopedElement::~ScopedElement() { + if( m_writer ) + m_writer->endElement(); + } + + XmlWriter::ScopedElement& XmlWriter::ScopedElement::writeText( std::string const& text, bool indent ) { + m_writer->writeText( text, indent ); + return *this; + } + + XmlWriter::XmlWriter( std::ostream& os ) : m_os( os ) + { + writeDeclaration(); + } + + XmlWriter::~XmlWriter() { + while( !m_tags.empty() ) + endElement(); + } + + XmlWriter& XmlWriter::startElement( std::string const& name ) { + ensureTagClosed(); + newlineIfNecessary(); + m_os << m_indent << '<' << name; + m_tags.push_back( name ); + m_indent += " "; + m_tagIsOpen = true; + return *this; + } + + XmlWriter::ScopedElement XmlWriter::scopedElement( std::string const& name ) { + ScopedElement scoped( this ); + startElement( name ); + return scoped; + } + + XmlWriter& XmlWriter::endElement() { + newlineIfNecessary(); + m_indent = m_indent.substr( 0, m_indent.size()-2 ); + if( m_tagIsOpen ) { + m_os << "/>"; + m_tagIsOpen = false; + } + else { + m_os << m_indent << "</" << m_tags.back() << ">"; + } + m_os << std::endl; + m_tags.pop_back(); + return *this; + } + + XmlWriter& XmlWriter::writeAttribute( std::string const& name, std::string const& attribute ) { + if( !name.empty() && !attribute.empty() ) + m_os << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"'; + return *this; + } + + XmlWriter& XmlWriter::writeAttribute( std::string const& name, bool attribute ) { + m_os << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; + return *this; + } + + XmlWriter& XmlWriter::writeText( std::string const& text, bool indent ) { + if( !text.empty() ){ + bool tagWasOpen = m_tagIsOpen; + ensureTagClosed(); + if( tagWasOpen && indent ) + m_os << m_indent; + m_os << XmlEncode( text ); + m_needsNewline = true; + } + return *this; + } + + XmlWriter& XmlWriter::writeComment( std::string const& text ) { + ensureTagClosed(); + m_os << m_indent << "<!--" << text << "-->"; + m_needsNewline = true; + return *this; + } + + void XmlWriter::writeStylesheetRef( std::string const& url ) { + m_os << "<?xml-stylesheet type=\"text/xsl\" href=\"" << url << "\"?>\n"; + } + + XmlWriter& XmlWriter::writeBlankLine() { + ensureTagClosed(); + m_os << '\n'; + return *this; + } + + void XmlWriter::ensureTagClosed() { + if( m_tagIsOpen ) { + m_os << ">" << std::endl; + m_tagIsOpen = false; + } + } + + void XmlWriter::writeDeclaration() { + m_os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; + } + + void XmlWriter::newlineIfNecessary() { + if( m_needsNewline ) { + m_os << std::endl; + m_needsNewline = false; + } + } +} +// end catch_xmlwriter.cpp +// start catch_reporter_bases.cpp + +#include <cstring> +#include <cfloat> +#include <cstdio> +#include <cassert> +#include <memory> + +namespace Catch { + void prepareExpandedExpression(AssertionResult& result) { + result.getExpandedExpression(); + } + + // Because formatting using c++ streams is stateful, drop down to C is required + // Alternatively we could use stringstream, but its performance is... not good. + std::string getFormattedDuration( double duration ) { + // Max exponent + 1 is required to represent the whole part + // + 1 for decimal point + // + 3 for the 3 decimal places + // + 1 for null terminator + const std::size_t maxDoubleSize = DBL_MAX_10_EXP + 1 + 1 + 3 + 1; + char buffer[maxDoubleSize]; + + // Save previous errno, to prevent sprintf from overwriting it + ErrnoGuard guard; +#ifdef _MSC_VER + sprintf_s(buffer, "%.3f", duration); +#else + sprintf(buffer, "%.3f", duration); +#endif + return std::string(buffer); + } + + TestEventListenerBase::TestEventListenerBase(ReporterConfig const & _config) + :StreamingReporterBase(_config) {} + + std::set<Verbosity> TestEventListenerBase::getSupportedVerbosities() { + return { Verbosity::Quiet, Verbosity::Normal, Verbosity::High }; + } + + void TestEventListenerBase::assertionStarting(AssertionInfo const &) {} + + bool TestEventListenerBase::assertionEnded(AssertionStats const &) { + return false; + } + +} // end namespace Catch +// end catch_reporter_bases.cpp +// start catch_reporter_compact.cpp + +namespace { + +#ifdef CATCH_PLATFORM_MAC + const char* failedString() { return "FAILED"; } + const char* passedString() { return "PASSED"; } +#else + const char* failedString() { return "failed"; } + const char* passedString() { return "passed"; } +#endif + + // Colour::LightGrey + Catch::Colour::Code dimColour() { return Catch::Colour::FileName; } + + std::string bothOrAll( std::size_t count ) { + return count == 1 ? std::string() : + count == 2 ? "both " : "all " ; + } + +} // anon namespace + +namespace Catch { +namespace { +// Colour, message variants: +// - white: No tests ran. +// - red: Failed [both/all] N test cases, failed [both/all] M assertions. +// - white: Passed [both/all] N test cases (no assertions). +// - red: Failed N tests cases, failed M assertions. +// - green: Passed [both/all] N tests cases with M assertions. +void printTotals(std::ostream& out, const Totals& totals) { + if (totals.testCases.total() == 0) { + out << "No tests ran."; + } else if (totals.testCases.failed == totals.testCases.total()) { + Colour colour(Colour::ResultError); + const std::string qualify_assertions_failed = + totals.assertions.failed == totals.assertions.total() ? + bothOrAll(totals.assertions.failed) : std::string(); + out << + "Failed " << bothOrAll(totals.testCases.failed) + << pluralise(totals.testCases.failed, "test case") << ", " + "failed " << qualify_assertions_failed << + pluralise(totals.assertions.failed, "assertion") << '.'; + } else if (totals.assertions.total() == 0) { + out << + "Passed " << bothOrAll(totals.testCases.total()) + << pluralise(totals.testCases.total(), "test case") + << " (no assertions)."; + } else if (totals.assertions.failed) { + Colour colour(Colour::ResultError); + out << + "Failed " << pluralise(totals.testCases.failed, "test case") << ", " + "failed " << pluralise(totals.assertions.failed, "assertion") << '.'; + } else { + Colour colour(Colour::ResultSuccess); + out << + "Passed " << bothOrAll(totals.testCases.passed) + << pluralise(totals.testCases.passed, "test case") << + " with " << pluralise(totals.assertions.passed, "assertion") << '.'; + } +} + +// Implementation of CompactReporter formatting +class AssertionPrinter { +public: + AssertionPrinter& operator= (AssertionPrinter const&) = delete; + AssertionPrinter(AssertionPrinter const&) = delete; + AssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages) + : stream(_stream) + , result(_stats.assertionResult) + , messages(_stats.infoMessages) + , itMessage(_stats.infoMessages.begin()) + , printInfoMessages(_printInfoMessages) {} + + void print() { + printSourceInfo(); + + itMessage = messages.begin(); + + switch (result.getResultType()) { + case ResultWas::Ok: + printResultType(Colour::ResultSuccess, passedString()); + printOriginalExpression(); + printReconstructedExpression(); + if (!result.hasExpression()) + printRemainingMessages(Colour::None); + else + printRemainingMessages(); + break; + case ResultWas::ExpressionFailed: + if (result.isOk()) + printResultType(Colour::ResultSuccess, failedString() + std::string(" - but was ok")); + else + printResultType(Colour::Error, failedString()); + printOriginalExpression(); + printReconstructedExpression(); + printRemainingMessages(); + break; + case ResultWas::ThrewException: + printResultType(Colour::Error, failedString()); + printIssue("unexpected exception with message:"); + printMessage(); + printExpressionWas(); + printRemainingMessages(); + break; + case ResultWas::FatalErrorCondition: + printResultType(Colour::Error, failedString()); + printIssue("fatal error condition with message:"); + printMessage(); + printExpressionWas(); + printRemainingMessages(); + break; + case ResultWas::DidntThrowException: + printResultType(Colour::Error, failedString()); + printIssue("expected exception, got none"); + printExpressionWas(); + printRemainingMessages(); + break; + case ResultWas::Info: + printResultType(Colour::None, "info"); + printMessage(); + printRemainingMessages(); + break; + case ResultWas::Warning: + printResultType(Colour::None, "warning"); + printMessage(); + printRemainingMessages(); + break; + case ResultWas::ExplicitFailure: + printResultType(Colour::Error, failedString()); + printIssue("explicitly"); + printRemainingMessages(Colour::None); + break; + // These cases are here to prevent compiler warnings + case ResultWas::Unknown: + case ResultWas::FailureBit: + case ResultWas::Exception: + printResultType(Colour::Error, "** internal error **"); + break; + } + } + +private: + void printSourceInfo() const { + Colour colourGuard(Colour::FileName); + stream << result.getSourceInfo() << ':'; + } + + void printResultType(Colour::Code colour, std::string const& passOrFail) const { + if (!passOrFail.empty()) { + { + Colour colourGuard(colour); + stream << ' ' << passOrFail; + } + stream << ':'; + } + } + + void printIssue(std::string const& issue) const { + stream << ' ' << issue; + } + + void printExpressionWas() { + if (result.hasExpression()) { + stream << ';'; + { + Colour colour(dimColour()); + stream << " expression was:"; + } + printOriginalExpression(); + } + } + + void printOriginalExpression() const { + if (result.hasExpression()) { + stream << ' ' << result.getExpression(); + } + } + + void printReconstructedExpression() const { + if (result.hasExpandedExpression()) { + { + Colour colour(dimColour()); + stream << " for: "; + } + stream << result.getExpandedExpression(); + } + } + + void printMessage() { + if (itMessage != messages.end()) { + stream << " '" << itMessage->message << '\''; + ++itMessage; + } + } + + void printRemainingMessages(Colour::Code colour = dimColour()) { + if (itMessage == messages.end()) + return; + + // using messages.end() directly yields (or auto) compilation error: + std::vector<MessageInfo>::const_iterator itEnd = messages.end(); + const std::size_t N = static_cast<std::size_t>(std::distance(itMessage, itEnd)); + + { + Colour colourGuard(colour); + stream << " with " << pluralise(N, "message") << ':'; + } + + for (; itMessage != itEnd; ) { + // If this assertion is a warning ignore any INFO messages + if (printInfoMessages || itMessage->type != ResultWas::Info) { + stream << " '" << itMessage->message << '\''; + if (++itMessage != itEnd) { + Colour colourGuard(dimColour()); + stream << " and"; + } + } + } + } + +private: + std::ostream& stream; + AssertionResult const& result; + std::vector<MessageInfo> messages; + std::vector<MessageInfo>::const_iterator itMessage; + bool printInfoMessages; +}; + +} // anon namespace + + std::string CompactReporter::getDescription() { + return "Reports test results on a single line, suitable for IDEs"; + } + + ReporterPreferences CompactReporter::getPreferences() const { + return m_reporterPrefs; + } + + void CompactReporter::noMatchingTestCases( std::string const& spec ) { + stream << "No test cases matched '" << spec << '\'' << std::endl; + } + + void CompactReporter::assertionStarting( AssertionInfo const& ) {} + + bool CompactReporter::assertionEnded( AssertionStats const& _assertionStats ) { + AssertionResult const& result = _assertionStats.assertionResult; + + bool printInfoMessages = true; + + // Drop out if result was successful and we're not printing those + if( !m_config->includeSuccessfulResults() && result.isOk() ) { + if( result.getResultType() != ResultWas::Warning ) + return false; + printInfoMessages = false; + } + + AssertionPrinter printer( stream, _assertionStats, printInfoMessages ); + printer.print(); + + stream << std::endl; + return true; + } + + void CompactReporter::sectionEnded(SectionStats const& _sectionStats) { + if (m_config->showDurations() == ShowDurations::Always) { + stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl; + } + } + + void CompactReporter::testRunEnded( TestRunStats const& _testRunStats ) { + printTotals( stream, _testRunStats.totals ); + stream << '\n' << std::endl; + StreamingReporterBase::testRunEnded( _testRunStats ); + } + + CompactReporter::~CompactReporter() {} + + CATCH_REGISTER_REPORTER( "compact", CompactReporter ) + +} // end namespace Catch +// end catch_reporter_compact.cpp +// start catch_reporter_console.cpp + +#include <cfloat> +#include <cstdio> + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch + // Note that 4062 (not all labels are handled + // and default is missing) is enabled +#endif + +namespace Catch { + +namespace { + +// Formatter impl for ConsoleReporter +class ConsoleAssertionPrinter { +public: + ConsoleAssertionPrinter& operator= (ConsoleAssertionPrinter const&) = delete; + ConsoleAssertionPrinter(ConsoleAssertionPrinter const&) = delete; + ConsoleAssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages) + : stream(_stream), + stats(_stats), + result(_stats.assertionResult), + colour(Colour::None), + message(result.getMessage()), + messages(_stats.infoMessages), + printInfoMessages(_printInfoMessages) { + switch (result.getResultType()) { + case ResultWas::Ok: + colour = Colour::Success; + passOrFail = "PASSED"; + //if( result.hasMessage() ) + if (_stats.infoMessages.size() == 1) + messageLabel = "with message"; + if (_stats.infoMessages.size() > 1) + messageLabel = "with messages"; + break; + case ResultWas::ExpressionFailed: + if (result.isOk()) { + colour = Colour::Success; + passOrFail = "FAILED - but was ok"; + } else { + colour = Colour::Error; + passOrFail = "FAILED"; + } + if (_stats.infoMessages.size() == 1) + messageLabel = "with message"; + if (_stats.infoMessages.size() > 1) + messageLabel = "with messages"; + break; + case ResultWas::ThrewException: + colour = Colour::Error; + passOrFail = "FAILED"; + messageLabel = "due to unexpected exception with "; + if (_stats.infoMessages.size() == 1) + messageLabel += "message"; + if (_stats.infoMessages.size() > 1) + messageLabel += "messages"; + break; + case ResultWas::FatalErrorCondition: + colour = Colour::Error; + passOrFail = "FAILED"; + messageLabel = "due to a fatal error condition"; + break; + case ResultWas::DidntThrowException: + colour = Colour::Error; + passOrFail = "FAILED"; + messageLabel = "because no exception was thrown where one was expected"; + break; + case ResultWas::Info: + messageLabel = "info"; + break; + case ResultWas::Warning: + messageLabel = "warning"; + break; + case ResultWas::ExplicitFailure: + passOrFail = "FAILED"; + colour = Colour::Error; + if (_stats.infoMessages.size() == 1) + messageLabel = "explicitly with message"; + if (_stats.infoMessages.size() > 1) + messageLabel = "explicitly with messages"; + break; + // These cases are here to prevent compiler warnings + case ResultWas::Unknown: + case ResultWas::FailureBit: + case ResultWas::Exception: + passOrFail = "** internal error **"; + colour = Colour::Error; + break; + } + } + + void print() const { + printSourceInfo(); + if (stats.totals.assertions.total() > 0) { + printResultType(); + printOriginalExpression(); + printReconstructedExpression(); + } else { + stream << '\n'; + } + printMessage(); + } + +private: + void printResultType() const { + if (!passOrFail.empty()) { + Colour colourGuard(colour); + stream << passOrFail << ":\n"; + } + } + void printOriginalExpression() const { + if (result.hasExpression()) { + Colour colourGuard(Colour::OriginalExpression); + stream << " "; + stream << result.getExpressionInMacro(); + stream << '\n'; + } + } + void printReconstructedExpression() const { + if (result.hasExpandedExpression()) { + stream << "with expansion:\n"; + Colour colourGuard(Colour::ReconstructedExpression); + stream << Column(result.getExpandedExpression()).indent(2) << '\n'; + } + } + void printMessage() const { + if (!messageLabel.empty()) + stream << messageLabel << ':' << '\n'; + for (auto const& msg : messages) { + // If this assertion is a warning ignore any INFO messages + if (printInfoMessages || msg.type != ResultWas::Info) + stream << Column(msg.message).indent(2) << '\n'; + } + } + void printSourceInfo() const { + Colour colourGuard(Colour::FileName); + stream << result.getSourceInfo() << ": "; + } + + std::ostream& stream; + AssertionStats const& stats; + AssertionResult const& result; + Colour::Code colour; + std::string passOrFail; + std::string messageLabel; + std::string message; + std::vector<MessageInfo> messages; + bool printInfoMessages; +}; + +std::size_t makeRatio(std::size_t number, std::size_t total) { + std::size_t ratio = total > 0 ? CATCH_CONFIG_CONSOLE_WIDTH * number / total : 0; + return (ratio == 0 && number > 0) ? 1 : ratio; +} + +std::size_t& findMax(std::size_t& i, std::size_t& j, std::size_t& k) { + if (i > j && i > k) + return i; + else if (j > k) + return j; + else + return k; +} + +struct ColumnInfo { + enum Justification { Left, Right }; + std::string name; + int width; + Justification justification; +}; +struct ColumnBreak {}; +struct RowBreak {}; + +class Duration { + enum class Unit { + Auto, + Nanoseconds, + Microseconds, + Milliseconds, + Seconds, + Minutes + }; + static const uint64_t s_nanosecondsInAMicrosecond = 1000; + static const uint64_t s_nanosecondsInAMillisecond = 1000 * s_nanosecondsInAMicrosecond; + static const uint64_t s_nanosecondsInASecond = 1000 * s_nanosecondsInAMillisecond; + static const uint64_t s_nanosecondsInAMinute = 60 * s_nanosecondsInASecond; + + uint64_t m_inNanoseconds; + Unit m_units; + +public: + explicit Duration(uint64_t inNanoseconds, Unit units = Unit::Auto) + : m_inNanoseconds(inNanoseconds), + m_units(units) { + if (m_units == Unit::Auto) { + if (m_inNanoseconds < s_nanosecondsInAMicrosecond) + m_units = Unit::Nanoseconds; + else if (m_inNanoseconds < s_nanosecondsInAMillisecond) + m_units = Unit::Microseconds; + else if (m_inNanoseconds < s_nanosecondsInASecond) + m_units = Unit::Milliseconds; + else if (m_inNanoseconds < s_nanosecondsInAMinute) + m_units = Unit::Seconds; + else + m_units = Unit::Minutes; + } + + } + + auto value() const -> double { + switch (m_units) { + case Unit::Microseconds: + return m_inNanoseconds / static_cast<double>(s_nanosecondsInAMicrosecond); + case Unit::Milliseconds: + return m_inNanoseconds / static_cast<double>(s_nanosecondsInAMillisecond); + case Unit::Seconds: + return m_inNanoseconds / static_cast<double>(s_nanosecondsInASecond); + case Unit::Minutes: + return m_inNanoseconds / static_cast<double>(s_nanosecondsInAMinute); + default: + return static_cast<double>(m_inNanoseconds); + } + } + auto unitsAsString() const -> std::string { + switch (m_units) { + case Unit::Nanoseconds: + return "ns"; + case Unit::Microseconds: + return "µs"; + case Unit::Milliseconds: + return "ms"; + case Unit::Seconds: + return "s"; + case Unit::Minutes: + return "m"; + default: + return "** internal error **"; + } + + } + friend auto operator << (std::ostream& os, Duration const& duration) -> std::ostream& { + return os << duration.value() << " " << duration.unitsAsString(); + } +}; +} // end anon namespace + +class TablePrinter { + std::ostream& m_os; + std::vector<ColumnInfo> m_columnInfos; + std::ostringstream m_oss; + int m_currentColumn = -1; + bool m_isOpen = false; + +public: + TablePrinter( std::ostream& os, std::vector<ColumnInfo> columnInfos ) + : m_os( os ), + m_columnInfos( std::move( columnInfos ) ) {} + + auto columnInfos() const -> std::vector<ColumnInfo> const& { + return m_columnInfos; + } + + void open() { + if (!m_isOpen) { + m_isOpen = true; + *this << RowBreak(); + for (auto const& info : m_columnInfos) + *this << info.name << ColumnBreak(); + *this << RowBreak(); + m_os << Catch::getLineOfChars<'-'>() << "\n"; + } + } + void close() { + if (m_isOpen) { + *this << RowBreak(); + m_os << std::endl; + m_isOpen = false; + } + } + + template<typename T> + friend TablePrinter& operator << (TablePrinter& tp, T const& value) { + tp.m_oss << value; + return tp; + } + + friend TablePrinter& operator << (TablePrinter& tp, ColumnBreak) { + auto colStr = tp.m_oss.str(); + // This takes account of utf8 encodings + auto strSize = Catch::StringRef(colStr).numberOfCharacters(); + tp.m_oss.str(""); + tp.open(); + if (tp.m_currentColumn == static_cast<int>(tp.m_columnInfos.size() - 1)) { + tp.m_currentColumn = -1; + tp.m_os << "\n"; + } + tp.m_currentColumn++; + + auto colInfo = tp.m_columnInfos[tp.m_currentColumn]; + auto padding = (strSize + 2 < static_cast<std::size_t>(colInfo.width)) + ? std::string(colInfo.width - (strSize + 2), ' ') + : std::string(); + if (colInfo.justification == ColumnInfo::Left) + tp.m_os << colStr << padding << " "; + else + tp.m_os << padding << colStr << " "; + return tp; + } + + friend TablePrinter& operator << (TablePrinter& tp, RowBreak) { + if (tp.m_currentColumn > 0) { + tp.m_os << "\n"; + tp.m_currentColumn = -1; + } + return tp; + } +}; + +ConsoleReporter::ConsoleReporter(ReporterConfig const& config) + : StreamingReporterBase(config), + m_tablePrinter(new TablePrinter(config.stream(), + { + { "benchmark name", CATCH_CONFIG_CONSOLE_WIDTH - 32, ColumnInfo::Left }, + { "iters", 8, ColumnInfo::Right }, + { "elapsed ns", 14, ColumnInfo::Right }, + { "average", 14, ColumnInfo::Right } + })) {} +ConsoleReporter::~ConsoleReporter() = default; + +std::string ConsoleReporter::getDescription() { + return "Reports test results as plain lines of text"; +} + +void ConsoleReporter::noMatchingTestCases(std::string const& spec) { + stream << "No test cases matched '" << spec << '\'' << std::endl; +} + +void ConsoleReporter::assertionStarting(AssertionInfo const&) {} + +bool ConsoleReporter::assertionEnded(AssertionStats const& _assertionStats) { + AssertionResult const& result = _assertionStats.assertionResult; + + bool includeResults = m_config->includeSuccessfulResults() || !result.isOk(); + + // Drop out if result was successful but we're not printing them. + if (!includeResults && result.getResultType() != ResultWas::Warning) + return false; + + lazyPrint(); + + ConsoleAssertionPrinter printer(stream, _assertionStats, includeResults); + printer.print(); + stream << std::endl; + return true; +} + +void ConsoleReporter::sectionStarting(SectionInfo const& _sectionInfo) { + m_headerPrinted = false; + StreamingReporterBase::sectionStarting(_sectionInfo); +} +void ConsoleReporter::sectionEnded(SectionStats const& _sectionStats) { + m_tablePrinter->close(); + if (_sectionStats.missingAssertions) { + lazyPrint(); + Colour colour(Colour::ResultError); + if (m_sectionStack.size() > 1) + stream << "\nNo assertions in section"; + else + stream << "\nNo assertions in test case"; + stream << " '" << _sectionStats.sectionInfo.name << "'\n" << std::endl; + } + if (m_config->showDurations() == ShowDurations::Always) { + stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl; + } + if (m_headerPrinted) { + m_headerPrinted = false; + } + StreamingReporterBase::sectionEnded(_sectionStats); +} + +void ConsoleReporter::benchmarkStarting(BenchmarkInfo const& info) { + lazyPrintWithoutClosingBenchmarkTable(); + + auto nameCol = Column( info.name ).width( static_cast<std::size_t>( m_tablePrinter->columnInfos()[0].width - 2 ) ); + + bool firstLine = true; + for (auto line : nameCol) { + if (!firstLine) + (*m_tablePrinter) << ColumnBreak() << ColumnBreak() << ColumnBreak(); + else + firstLine = false; + + (*m_tablePrinter) << line << ColumnBreak(); + } +} +void ConsoleReporter::benchmarkEnded(BenchmarkStats const& stats) { + Duration average(stats.elapsedTimeInNanoseconds / stats.iterations); + (*m_tablePrinter) + << stats.iterations << ColumnBreak() + << stats.elapsedTimeInNanoseconds << ColumnBreak() + << average << ColumnBreak(); +} + +void ConsoleReporter::testCaseEnded(TestCaseStats const& _testCaseStats) { + m_tablePrinter->close(); + StreamingReporterBase::testCaseEnded(_testCaseStats); + m_headerPrinted = false; +} +void ConsoleReporter::testGroupEnded(TestGroupStats const& _testGroupStats) { + if (currentGroupInfo.used) { + printSummaryDivider(); + stream << "Summary for group '" << _testGroupStats.groupInfo.name << "':\n"; + printTotals(_testGroupStats.totals); + stream << '\n' << std::endl; + } + StreamingReporterBase::testGroupEnded(_testGroupStats); +} +void ConsoleReporter::testRunEnded(TestRunStats const& _testRunStats) { + printTotalsDivider(_testRunStats.totals); + printTotals(_testRunStats.totals); + stream << std::endl; + StreamingReporterBase::testRunEnded(_testRunStats); +} + +void ConsoleReporter::lazyPrint() { + + m_tablePrinter->close(); + lazyPrintWithoutClosingBenchmarkTable(); +} + +void ConsoleReporter::lazyPrintWithoutClosingBenchmarkTable() { + + if (!currentTestRunInfo.used) + lazyPrintRunInfo(); + if (!currentGroupInfo.used) + lazyPrintGroupInfo(); + + if (!m_headerPrinted) { + printTestCaseAndSectionHeader(); + m_headerPrinted = true; + } +} +void ConsoleReporter::lazyPrintRunInfo() { + stream << '\n' << getLineOfChars<'~'>() << '\n'; + Colour colour(Colour::SecondaryText); + stream << currentTestRunInfo->name + << " is a Catch v" << libraryVersion() << " host application.\n" + << "Run with -? for options\n\n"; + + if (m_config->rngSeed() != 0) + stream << "Randomness seeded to: " << m_config->rngSeed() << "\n\n"; + + currentTestRunInfo.used = true; +} +void ConsoleReporter::lazyPrintGroupInfo() { + if (!currentGroupInfo->name.empty() && currentGroupInfo->groupsCounts > 1) { + printClosedHeader("Group: " + currentGroupInfo->name); + currentGroupInfo.used = true; + } +} +void ConsoleReporter::printTestCaseAndSectionHeader() { + assert(!m_sectionStack.empty()); + printOpenHeader(currentTestCaseInfo->name); + + if (m_sectionStack.size() > 1) { + Colour colourGuard(Colour::Headers); + + auto + it = m_sectionStack.begin() + 1, // Skip first section (test case) + itEnd = m_sectionStack.end(); + for (; it != itEnd; ++it) + printHeaderString(it->name, 2); + } + + SourceLineInfo lineInfo = m_sectionStack.back().lineInfo; + + if (!lineInfo.empty()) { + stream << getLineOfChars<'-'>() << '\n'; + Colour colourGuard(Colour::FileName); + stream << lineInfo << '\n'; + } + stream << getLineOfChars<'.'>() << '\n' << std::endl; +} + +void ConsoleReporter::printClosedHeader(std::string const& _name) { + printOpenHeader(_name); + stream << getLineOfChars<'.'>() << '\n'; +} +void ConsoleReporter::printOpenHeader(std::string const& _name) { + stream << getLineOfChars<'-'>() << '\n'; + { + Colour colourGuard(Colour::Headers); + printHeaderString(_name); + } +} + +// if string has a : in first line will set indent to follow it on +// subsequent lines +void ConsoleReporter::printHeaderString(std::string const& _string, std::size_t indent) { + std::size_t i = _string.find(": "); + if (i != std::string::npos) + i += 2; + else + i = 0; + stream << Column(_string).indent(indent + i).initialIndent(indent) << '\n'; +} + +struct SummaryColumn { + + SummaryColumn( std::string _label, Colour::Code _colour ) + : label( std::move( _label ) ), + colour( _colour ) {} + SummaryColumn addRow( std::size_t count ) { + ReusableStringStream rss; + rss << count; + std::string row = rss.str(); + for (auto& oldRow : rows) { + while (oldRow.size() < row.size()) + oldRow = ' ' + oldRow; + while (oldRow.size() > row.size()) + row = ' ' + row; + } + rows.push_back(row); + return *this; + } + + std::string label; + Colour::Code colour; + std::vector<std::string> rows; + +}; + +void ConsoleReporter::printTotals( Totals const& totals ) { + if (totals.testCases.total() == 0) { + stream << Colour(Colour::Warning) << "No tests ran\n"; + } else if (totals.assertions.total() > 0 && totals.testCases.allPassed()) { + stream << Colour(Colour::ResultSuccess) << "All tests passed"; + stream << " (" + << pluralise(totals.assertions.passed, "assertion") << " in " + << pluralise(totals.testCases.passed, "test case") << ')' + << '\n'; + } else { + + std::vector<SummaryColumn> columns; + columns.push_back(SummaryColumn("", Colour::None) + .addRow(totals.testCases.total()) + .addRow(totals.assertions.total())); + columns.push_back(SummaryColumn("passed", Colour::Success) + .addRow(totals.testCases.passed) + .addRow(totals.assertions.passed)); + columns.push_back(SummaryColumn("failed", Colour::ResultError) + .addRow(totals.testCases.failed) + .addRow(totals.assertions.failed)); + columns.push_back(SummaryColumn("failed as expected", Colour::ResultExpectedFailure) + .addRow(totals.testCases.failedButOk) + .addRow(totals.assertions.failedButOk)); + + printSummaryRow("test cases", columns, 0); + printSummaryRow("assertions", columns, 1); + } +} +void ConsoleReporter::printSummaryRow(std::string const& label, std::vector<SummaryColumn> const& cols, std::size_t row) { + for (auto col : cols) { + std::string value = col.rows[row]; + if (col.label.empty()) { + stream << label << ": "; + if (value != "0") + stream << value; + else + stream << Colour(Colour::Warning) << "- none -"; + } else if (value != "0") { + stream << Colour(Colour::LightGrey) << " | "; + stream << Colour(col.colour) + << value << ' ' << col.label; + } + } + stream << '\n'; +} + +void ConsoleReporter::printTotalsDivider(Totals const& totals) { + if (totals.testCases.total() > 0) { + std::size_t failedRatio = makeRatio(totals.testCases.failed, totals.testCases.total()); + std::size_t failedButOkRatio = makeRatio(totals.testCases.failedButOk, totals.testCases.total()); + std::size_t passedRatio = makeRatio(totals.testCases.passed, totals.testCases.total()); + while (failedRatio + failedButOkRatio + passedRatio < CATCH_CONFIG_CONSOLE_WIDTH - 1) + findMax(failedRatio, failedButOkRatio, passedRatio)++; + while (failedRatio + failedButOkRatio + passedRatio > CATCH_CONFIG_CONSOLE_WIDTH - 1) + findMax(failedRatio, failedButOkRatio, passedRatio)--; + + stream << Colour(Colour::Error) << std::string(failedRatio, '='); + stream << Colour(Colour::ResultExpectedFailure) << std::string(failedButOkRatio, '='); + if (totals.testCases.allPassed()) + stream << Colour(Colour::ResultSuccess) << std::string(passedRatio, '='); + else + stream << Colour(Colour::Success) << std::string(passedRatio, '='); + } else { + stream << Colour(Colour::Warning) << std::string(CATCH_CONFIG_CONSOLE_WIDTH - 1, '='); + } + stream << '\n'; +} +void ConsoleReporter::printSummaryDivider() { + stream << getLineOfChars<'-'>() << '\n'; +} + +CATCH_REGISTER_REPORTER("console", ConsoleReporter) + +} // end namespace Catch + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif +// end catch_reporter_console.cpp +// start catch_reporter_junit.cpp + +#include <cassert> +#include <sstream> +#include <ctime> +#include <algorithm> + +namespace Catch { + + namespace { + std::string getCurrentTimestamp() { + // Beware, this is not reentrant because of backward compatibility issues + // Also, UTC only, again because of backward compatibility (%z is C++11) + time_t rawtime; + std::time(&rawtime); + auto const timeStampSize = sizeof("2017-01-16T17:06:45Z"); + +#ifdef _MSC_VER + std::tm timeInfo = {}; + gmtime_s(&timeInfo, &rawtime); +#else + std::tm* timeInfo; + timeInfo = std::gmtime(&rawtime); +#endif + + char timeStamp[timeStampSize]; + const char * const fmt = "%Y-%m-%dT%H:%M:%SZ"; + +#ifdef _MSC_VER + std::strftime(timeStamp, timeStampSize, fmt, &timeInfo); +#else + std::strftime(timeStamp, timeStampSize, fmt, timeInfo); +#endif + return std::string(timeStamp); + } + + std::string fileNameTag(const std::vector<std::string> &tags) { + auto it = std::find_if(begin(tags), + end(tags), + [] (std::string const& tag) {return tag.front() == '#'; }); + if (it != tags.end()) + return it->substr(1); + return std::string(); + } + } // anonymous namespace + + JunitReporter::JunitReporter( ReporterConfig const& _config ) + : CumulativeReporterBase( _config ), + xml( _config.stream() ) + { + m_reporterPrefs.shouldRedirectStdOut = true; + m_reporterPrefs.shouldReportAllAssertions = true; + } + + JunitReporter::~JunitReporter() {} + + std::string JunitReporter::getDescription() { + return "Reports test results in an XML format that looks like Ant's junitreport target"; + } + + void JunitReporter::noMatchingTestCases( std::string const& /*spec*/ ) {} + + void JunitReporter::testRunStarting( TestRunInfo const& runInfo ) { + CumulativeReporterBase::testRunStarting( runInfo ); + xml.startElement( "testsuites" ); + } + + void JunitReporter::testGroupStarting( GroupInfo const& groupInfo ) { + suiteTimer.start(); + stdOutForSuite.clear(); + stdErrForSuite.clear(); + unexpectedExceptions = 0; + CumulativeReporterBase::testGroupStarting( groupInfo ); + } + + void JunitReporter::testCaseStarting( TestCaseInfo const& testCaseInfo ) { + m_okToFail = testCaseInfo.okToFail(); + } + + bool JunitReporter::assertionEnded( AssertionStats const& assertionStats ) { + if( assertionStats.assertionResult.getResultType() == ResultWas::ThrewException && !m_okToFail ) + unexpectedExceptions++; + return CumulativeReporterBase::assertionEnded( assertionStats ); + } + + void JunitReporter::testCaseEnded( TestCaseStats const& testCaseStats ) { + stdOutForSuite += testCaseStats.stdOut; + stdErrForSuite += testCaseStats.stdErr; + CumulativeReporterBase::testCaseEnded( testCaseStats ); + } + + void JunitReporter::testGroupEnded( TestGroupStats const& testGroupStats ) { + double suiteTime = suiteTimer.getElapsedSeconds(); + CumulativeReporterBase::testGroupEnded( testGroupStats ); + writeGroup( *m_testGroups.back(), suiteTime ); + } + + void JunitReporter::testRunEndedCumulative() { + xml.endElement(); + } + + void JunitReporter::writeGroup( TestGroupNode const& groupNode, double suiteTime ) { + XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" ); + TestGroupStats const& stats = groupNode.value; + xml.writeAttribute( "name", stats.groupInfo.name ); + xml.writeAttribute( "errors", unexpectedExceptions ); + xml.writeAttribute( "failures", stats.totals.assertions.failed-unexpectedExceptions ); + xml.writeAttribute( "tests", stats.totals.assertions.total() ); + xml.writeAttribute( "hostname", "tbd" ); // !TBD + if( m_config->showDurations() == ShowDurations::Never ) + xml.writeAttribute( "time", "" ); + else + xml.writeAttribute( "time", suiteTime ); + xml.writeAttribute( "timestamp", getCurrentTimestamp() ); + + // Write test cases + for( auto const& child : groupNode.children ) + writeTestCase( *child ); + + xml.scopedElement( "system-out" ).writeText( trim( stdOutForSuite ), false ); + xml.scopedElement( "system-err" ).writeText( trim( stdErrForSuite ), false ); + } + + void JunitReporter::writeTestCase( TestCaseNode const& testCaseNode ) { + TestCaseStats const& stats = testCaseNode.value; + + // All test cases have exactly one section - which represents the + // test case itself. That section may have 0-n nested sections + assert( testCaseNode.children.size() == 1 ); + SectionNode const& rootSection = *testCaseNode.children.front(); + + std::string className = stats.testInfo.className; + + if( className.empty() ) { + className = fileNameTag(stats.testInfo.tags); + if ( className.empty() ) + className = "global"; + } + + if ( !m_config->name().empty() ) + className = m_config->name() + "." + className; + + writeSection( className, "", rootSection ); + } + + void JunitReporter::writeSection( std::string const& className, + std::string const& rootName, + SectionNode const& sectionNode ) { + std::string name = trim( sectionNode.stats.sectionInfo.name ); + if( !rootName.empty() ) + name = rootName + '/' + name; + + if( !sectionNode.assertions.empty() || + !sectionNode.stdOut.empty() || + !sectionNode.stdErr.empty() ) { + XmlWriter::ScopedElement e = xml.scopedElement( "testcase" ); + if( className.empty() ) { + xml.writeAttribute( "classname", name ); + xml.writeAttribute( "name", "root" ); + } + else { + xml.writeAttribute( "classname", className ); + xml.writeAttribute( "name", name ); + } + xml.writeAttribute( "time", ::Catch::Detail::stringify( sectionNode.stats.durationInSeconds ) ); + + writeAssertions( sectionNode ); + + if( !sectionNode.stdOut.empty() ) + xml.scopedElement( "system-out" ).writeText( trim( sectionNode.stdOut ), false ); + if( !sectionNode.stdErr.empty() ) + xml.scopedElement( "system-err" ).writeText( trim( sectionNode.stdErr ), false ); + } + for( auto const& childNode : sectionNode.childSections ) + if( className.empty() ) + writeSection( name, "", *childNode ); + else + writeSection( className, name, *childNode ); + } + + void JunitReporter::writeAssertions( SectionNode const& sectionNode ) { + for( auto const& assertion : sectionNode.assertions ) + writeAssertion( assertion ); + } + + void JunitReporter::writeAssertion( AssertionStats const& stats ) { + AssertionResult const& result = stats.assertionResult; + if( !result.isOk() ) { + std::string elementName; + switch( result.getResultType() ) { + case ResultWas::ThrewException: + case ResultWas::FatalErrorCondition: + elementName = "error"; + break; + case ResultWas::ExplicitFailure: + elementName = "failure"; + break; + case ResultWas::ExpressionFailed: + elementName = "failure"; + break; + case ResultWas::DidntThrowException: + elementName = "failure"; + break; + + // We should never see these here: + case ResultWas::Info: + case ResultWas::Warning: + case ResultWas::Ok: + case ResultWas::Unknown: + case ResultWas::FailureBit: + case ResultWas::Exception: + elementName = "internalError"; + break; + } + + XmlWriter::ScopedElement e = xml.scopedElement( elementName ); + + xml.writeAttribute( "message", result.getExpandedExpression() ); + xml.writeAttribute( "type", result.getTestMacroName() ); + + ReusableStringStream rss; + if( !result.getMessage().empty() ) + rss << result.getMessage() << '\n'; + for( auto const& msg : stats.infoMessages ) + if( msg.type == ResultWas::Info ) + rss << msg.message << '\n'; + + rss << "at " << result.getSourceInfo(); + xml.writeText( rss.str(), false ); + } + } + + CATCH_REGISTER_REPORTER( "junit", JunitReporter ) + +} // end namespace Catch +// end catch_reporter_junit.cpp +// start catch_reporter_listening.cpp + +#include <cassert> + +namespace Catch { + + ListeningReporter::ListeningReporter() { + // We will assume that listeners will always want all assertions + m_preferences.shouldReportAllAssertions = true; + } + + void ListeningReporter::addListener( IStreamingReporterPtr&& listener ) { + m_listeners.push_back( std::move( listener ) ); + } + + void ListeningReporter::addReporter(IStreamingReporterPtr&& reporter) { + assert(!m_reporter && "Listening reporter can wrap only 1 real reporter"); + m_reporter = std::move( reporter ); + m_preferences.shouldRedirectStdOut = m_reporter->getPreferences().shouldRedirectStdOut; + } + + ReporterPreferences ListeningReporter::getPreferences() const { + return m_preferences; + } + + std::set<Verbosity> ListeningReporter::getSupportedVerbosities() { + return std::set<Verbosity>{ }; + } + + void ListeningReporter::noMatchingTestCases( std::string const& spec ) { + for ( auto const& listener : m_listeners ) { + listener->noMatchingTestCases( spec ); + } + m_reporter->noMatchingTestCases( spec ); + } + + void ListeningReporter::benchmarkStarting( BenchmarkInfo const& benchmarkInfo ) { + for ( auto const& listener : m_listeners ) { + listener->benchmarkStarting( benchmarkInfo ); + } + m_reporter->benchmarkStarting( benchmarkInfo ); + } + void ListeningReporter::benchmarkEnded( BenchmarkStats const& benchmarkStats ) { + for ( auto const& listener : m_listeners ) { + listener->benchmarkEnded( benchmarkStats ); + } + m_reporter->benchmarkEnded( benchmarkStats ); + } + + void ListeningReporter::testRunStarting( TestRunInfo const& testRunInfo ) { + for ( auto const& listener : m_listeners ) { + listener->testRunStarting( testRunInfo ); + } + m_reporter->testRunStarting( testRunInfo ); + } + + void ListeningReporter::testGroupStarting( GroupInfo const& groupInfo ) { + for ( auto const& listener : m_listeners ) { + listener->testGroupStarting( groupInfo ); + } + m_reporter->testGroupStarting( groupInfo ); + } + + void ListeningReporter::testCaseStarting( TestCaseInfo const& testInfo ) { + for ( auto const& listener : m_listeners ) { + listener->testCaseStarting( testInfo ); + } + m_reporter->testCaseStarting( testInfo ); + } + + void ListeningReporter::sectionStarting( SectionInfo const& sectionInfo ) { + for ( auto const& listener : m_listeners ) { + listener->sectionStarting( sectionInfo ); + } + m_reporter->sectionStarting( sectionInfo ); + } + + void ListeningReporter::assertionStarting( AssertionInfo const& assertionInfo ) { + for ( auto const& listener : m_listeners ) { + listener->assertionStarting( assertionInfo ); + } + m_reporter->assertionStarting( assertionInfo ); + } + + // The return value indicates if the messages buffer should be cleared: + bool ListeningReporter::assertionEnded( AssertionStats const& assertionStats ) { + for( auto const& listener : m_listeners ) { + static_cast<void>( listener->assertionEnded( assertionStats ) ); + } + return m_reporter->assertionEnded( assertionStats ); + } + + void ListeningReporter::sectionEnded( SectionStats const& sectionStats ) { + for ( auto const& listener : m_listeners ) { + listener->sectionEnded( sectionStats ); + } + m_reporter->sectionEnded( sectionStats ); + } + + void ListeningReporter::testCaseEnded( TestCaseStats const& testCaseStats ) { + for ( auto const& listener : m_listeners ) { + listener->testCaseEnded( testCaseStats ); + } + m_reporter->testCaseEnded( testCaseStats ); + } + + void ListeningReporter::testGroupEnded( TestGroupStats const& testGroupStats ) { + for ( auto const& listener : m_listeners ) { + listener->testGroupEnded( testGroupStats ); + } + m_reporter->testGroupEnded( testGroupStats ); + } + + void ListeningReporter::testRunEnded( TestRunStats const& testRunStats ) { + for ( auto const& listener : m_listeners ) { + listener->testRunEnded( testRunStats ); + } + m_reporter->testRunEnded( testRunStats ); + } + + void ListeningReporter::skipTest( TestCaseInfo const& testInfo ) { + for ( auto const& listener : m_listeners ) { + listener->skipTest( testInfo ); + } + m_reporter->skipTest( testInfo ); + } + + bool ListeningReporter::isMulti() const { + return true; + } + +} // end namespace Catch +// end catch_reporter_listening.cpp +// start catch_reporter_xml.cpp + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch + // Note that 4062 (not all labels are handled + // and default is missing) is enabled +#endif + +namespace Catch { + XmlReporter::XmlReporter( ReporterConfig const& _config ) + : StreamingReporterBase( _config ), + m_xml(_config.stream()) + { + m_reporterPrefs.shouldRedirectStdOut = true; + m_reporterPrefs.shouldReportAllAssertions = true; + } + + XmlReporter::~XmlReporter() = default; + + std::string XmlReporter::getDescription() { + return "Reports test results as an XML document"; + } + + std::string XmlReporter::getStylesheetRef() const { + return std::string(); + } + + void XmlReporter::writeSourceInfo( SourceLineInfo const& sourceInfo ) { + m_xml + .writeAttribute( "filename", sourceInfo.file ) + .writeAttribute( "line", sourceInfo.line ); + } + + void XmlReporter::noMatchingTestCases( std::string const& s ) { + StreamingReporterBase::noMatchingTestCases( s ); + } + + void XmlReporter::testRunStarting( TestRunInfo const& testInfo ) { + StreamingReporterBase::testRunStarting( testInfo ); + std::string stylesheetRef = getStylesheetRef(); + if( !stylesheetRef.empty() ) + m_xml.writeStylesheetRef( stylesheetRef ); + m_xml.startElement( "Catch" ); + if( !m_config->name().empty() ) + m_xml.writeAttribute( "name", m_config->name() ); + if( m_config->rngSeed() != 0 ) + m_xml.scopedElement( "Randomness" ) + .writeAttribute( "seed", m_config->rngSeed() ); + } + + void XmlReporter::testGroupStarting( GroupInfo const& groupInfo ) { + StreamingReporterBase::testGroupStarting( groupInfo ); + m_xml.startElement( "Group" ) + .writeAttribute( "name", groupInfo.name ); + } + + void XmlReporter::testCaseStarting( TestCaseInfo const& testInfo ) { + StreamingReporterBase::testCaseStarting(testInfo); + m_xml.startElement( "TestCase" ) + .writeAttribute( "name", trim( testInfo.name ) ) + .writeAttribute( "description", testInfo.description ) + .writeAttribute( "tags", testInfo.tagsAsString() ); + + writeSourceInfo( testInfo.lineInfo ); + + if ( m_config->showDurations() == ShowDurations::Always ) + m_testCaseTimer.start(); + m_xml.ensureTagClosed(); + } + + void XmlReporter::sectionStarting( SectionInfo const& sectionInfo ) { + StreamingReporterBase::sectionStarting( sectionInfo ); + if( m_sectionDepth++ > 0 ) { + m_xml.startElement( "Section" ) + .writeAttribute( "name", trim( sectionInfo.name ) ); + writeSourceInfo( sectionInfo.lineInfo ); + m_xml.ensureTagClosed(); + } + } + + void XmlReporter::assertionStarting( AssertionInfo const& ) { } + + bool XmlReporter::assertionEnded( AssertionStats const& assertionStats ) { + + AssertionResult const& result = assertionStats.assertionResult; + + bool includeResults = m_config->includeSuccessfulResults() || !result.isOk(); + + if( includeResults || result.getResultType() == ResultWas::Warning ) { + // Print any info messages in <Info> tags. + for( auto const& msg : assertionStats.infoMessages ) { + if( msg.type == ResultWas::Info && includeResults ) { + m_xml.scopedElement( "Info" ) + .writeText( msg.message ); + } else if ( msg.type == ResultWas::Warning ) { + m_xml.scopedElement( "Warning" ) + .writeText( msg.message ); + } + } + } + + // Drop out if result was successful but we're not printing them. + if( !includeResults && result.getResultType() != ResultWas::Warning ) + return true; + + // Print the expression if there is one. + if( result.hasExpression() ) { + m_xml.startElement( "Expression" ) + .writeAttribute( "success", result.succeeded() ) + .writeAttribute( "type", result.getTestMacroName() ); + + writeSourceInfo( result.getSourceInfo() ); + + m_xml.scopedElement( "Original" ) + .writeText( result.getExpression() ); + m_xml.scopedElement( "Expanded" ) + .writeText( result.getExpandedExpression() ); + } + + // And... Print a result applicable to each result type. + switch( result.getResultType() ) { + case ResultWas::ThrewException: + m_xml.startElement( "Exception" ); + writeSourceInfo( result.getSourceInfo() ); + m_xml.writeText( result.getMessage() ); + m_xml.endElement(); + break; + case ResultWas::FatalErrorCondition: + m_xml.startElement( "FatalErrorCondition" ); + writeSourceInfo( result.getSourceInfo() ); + m_xml.writeText( result.getMessage() ); + m_xml.endElement(); + break; + case ResultWas::Info: + m_xml.scopedElement( "Info" ) + .writeText( result.getMessage() ); + break; + case ResultWas::Warning: + // Warning will already have been written + break; + case ResultWas::ExplicitFailure: + m_xml.startElement( "Failure" ); + writeSourceInfo( result.getSourceInfo() ); + m_xml.writeText( result.getMessage() ); + m_xml.endElement(); + break; + default: + break; + } + + if( result.hasExpression() ) + m_xml.endElement(); + + return true; + } + + void XmlReporter::sectionEnded( SectionStats const& sectionStats ) { + StreamingReporterBase::sectionEnded( sectionStats ); + if( --m_sectionDepth > 0 ) { + XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResults" ); + e.writeAttribute( "successes", sectionStats.assertions.passed ); + e.writeAttribute( "failures", sectionStats.assertions.failed ); + e.writeAttribute( "expectedFailures", sectionStats.assertions.failedButOk ); + + if ( m_config->showDurations() == ShowDurations::Always ) + e.writeAttribute( "durationInSeconds", sectionStats.durationInSeconds ); + + m_xml.endElement(); + } + } + + void XmlReporter::testCaseEnded( TestCaseStats const& testCaseStats ) { + StreamingReporterBase::testCaseEnded( testCaseStats ); + XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResult" ); + e.writeAttribute( "success", testCaseStats.totals.assertions.allOk() ); + + if ( m_config->showDurations() == ShowDurations::Always ) + e.writeAttribute( "durationInSeconds", m_testCaseTimer.getElapsedSeconds() ); + + if( !testCaseStats.stdOut.empty() ) + m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), false ); + if( !testCaseStats.stdErr.empty() ) + m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), false ); + + m_xml.endElement(); + } + + void XmlReporter::testGroupEnded( TestGroupStats const& testGroupStats ) { + StreamingReporterBase::testGroupEnded( testGroupStats ); + // TODO: Check testGroupStats.aborting and act accordingly. + m_xml.scopedElement( "OverallResults" ) + .writeAttribute( "successes", testGroupStats.totals.assertions.passed ) + .writeAttribute( "failures", testGroupStats.totals.assertions.failed ) + .writeAttribute( "expectedFailures", testGroupStats.totals.assertions.failedButOk ); + m_xml.endElement(); + } + + void XmlReporter::testRunEnded( TestRunStats const& testRunStats ) { + StreamingReporterBase::testRunEnded( testRunStats ); + m_xml.scopedElement( "OverallResults" ) + .writeAttribute( "successes", testRunStats.totals.assertions.passed ) + .writeAttribute( "failures", testRunStats.totals.assertions.failed ) + .writeAttribute( "expectedFailures", testRunStats.totals.assertions.failedButOk ); + m_xml.endElement(); + } + + CATCH_REGISTER_REPORTER( "xml", XmlReporter ) + +} // end namespace Catch + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif +// end catch_reporter_xml.cpp + +namespace Catch { + LeakDetector leakDetector; +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// end catch_impl.hpp +#endif + +#ifdef CATCH_CONFIG_MAIN +// start catch_default_main.hpp + +#ifndef __OBJC__ + +#if defined(CATCH_CONFIG_WCHAR) && defined(WIN32) && defined(_UNICODE) && !defined(DO_NOT_USE_WMAIN) +// Standard C/C++ Win32 Unicode wmain entry point +extern "C" int wmain (int argc, wchar_t * argv[], wchar_t * []) { +#else +// Standard C/C++ main entry point +int main (int argc, char * argv[]) { +#endif + + return Catch::Session().run( argc, argv ); +} + +#else // __OBJC__ + +// Objective-C entry point +int main (int argc, char * const argv[]) { +#if !CATCH_ARC_ENABLED + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; +#endif + + Catch::registerTestMethods(); + int result = Catch::Session().run( argc, (char**)argv ); + +#if !CATCH_ARC_ENABLED + [pool drain]; +#endif + + return result; +} + +#endif // __OBJC__ + +// end catch_default_main.hpp +#endif + +#if !defined(CATCH_CONFIG_IMPL_ONLY) + +#ifdef CLARA_CONFIG_MAIN_NOT_DEFINED +# undef CLARA_CONFIG_MAIN +#endif + +#if !defined(CATCH_CONFIG_DISABLE) +////// +// If this config identifier is defined then all CATCH macros are prefixed with CATCH_ +#ifdef CATCH_CONFIG_PREFIX_ALL + +#define CATCH_REQUIRE( ... ) INTERNAL_CATCH_TEST( "CATCH_REQUIRE", Catch::ResultDisposition::Normal, __VA_ARGS__ ) +#define CATCH_REQUIRE_FALSE( ... ) INTERNAL_CATCH_TEST( "CATCH_REQUIRE_FALSE", Catch::ResultDisposition::Normal | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) + +#define CATCH_REQUIRE_THROWS( ... ) INTERNAL_CATCH_THROWS( "CATCH_REQUIRE_THROWS", Catch::ResultDisposition::Normal, "", __VA_ARGS__ ) +#define CATCH_REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CATCH_REQUIRE_THROWS_AS", exceptionType, Catch::ResultDisposition::Normal, expr ) +#define CATCH_REQUIRE_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CATCH_REQUIRE_THROWS_WITH", Catch::ResultDisposition::Normal, matcher, expr ) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CATCH_REQUIRE_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::Normal, matcher, expr ) +#endif// CATCH_CONFIG_DISABLE_MATCHERS +#define CATCH_REQUIRE_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CATCH_REQUIRE_NOTHROW", Catch::ResultDisposition::Normal, __VA_ARGS__ ) + +#define CATCH_CHECK( ... ) INTERNAL_CATCH_TEST( "CATCH_CHECK", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CATCH_CHECK_FALSE( ... ) INTERNAL_CATCH_TEST( "CATCH_CHECK_FALSE", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) +#define CATCH_CHECKED_IF( ... ) INTERNAL_CATCH_IF( "CATCH_CHECKED_IF", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CATCH_CHECKED_ELSE( ... ) INTERNAL_CATCH_ELSE( "CATCH_CHECKED_ELSE", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CATCH_CHECK_NOFAIL( ... ) INTERNAL_CATCH_TEST( "CATCH_CHECK_NOFAIL", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::SuppressFail, __VA_ARGS__ ) + +#define CATCH_CHECK_THROWS( ... ) INTERNAL_CATCH_THROWS( "CATCH_CHECK_THROWS", Catch::ResultDisposition::ContinueOnFailure, "", __VA_ARGS__ ) +#define CATCH_CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CATCH_CHECK_THROWS_AS", exceptionType, Catch::ResultDisposition::ContinueOnFailure, expr ) +#define CATCH_CHECK_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CATCH_CHECK_THROWS_WITH", Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CATCH_CHECK_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define CATCH_CHECK_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CATCH_CHECK_NOTHROW", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) + +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_CHECK_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CATCH_CHECK_THAT", matcher, Catch::ResultDisposition::ContinueOnFailure, arg ) + +#define CATCH_REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CATCH_REQUIRE_THAT", matcher, Catch::ResultDisposition::Normal, arg ) +#endif // CATCH_CONFIG_DISABLE_MATCHERS + +#define CATCH_INFO( msg ) INTERNAL_CATCH_INFO( "CATCH_INFO", msg ) +#define CATCH_WARN( msg ) INTERNAL_CATCH_MSG( "CATCH_WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg ) +#define CATCH_CAPTURE( ... ) INTERNAL_CATCH_CAPTURE( INTERNAL_CATCH_UNIQUE_NAME(capturer), "CATCH_CAPTURE",__VA_ARGS__ ) + +#define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) +#define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) +#define CATCH_METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ ) +#define CATCH_REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ ) +#define CATCH_SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ ) +#define CATCH_DYNAMIC_SECTION( ... ) INTERNAL_CATCH_DYNAMIC_SECTION( __VA_ARGS__ ) +#define CATCH_FAIL( ... ) INTERNAL_CATCH_MSG( "CATCH_FAIL", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::Normal, __VA_ARGS__ ) +#define CATCH_FAIL_CHECK( ... ) INTERNAL_CATCH_MSG( "CATCH_FAIL_CHECK", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CATCH_SUCCEED( ... ) INTERNAL_CATCH_MSG( "CATCH_SUCCEED", Catch::ResultWas::Ok, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) + +#define CATCH_ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE() + +#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#define CATCH_TEMPLATE_TEST_CASE( ... ) INTERNAL_CATCH_TEMPLATE_TEST_CASE( __VA_ARGS__ ) +#define CATCH_TEMPLATE_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD( className, __VA_ARGS__ ) +#else +#define CATCH_TEMPLATE_TEST_CASE( ... ) INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE( __VA_ARGS__ ) ) +#define CATCH_TEMPLATE_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD( className, __VA_ARGS__ ) ) +#endif + +#if !defined(CATCH_CONFIG_RUNTIME_STATIC_REQUIRE) +#define CATCH_STATIC_REQUIRE( ... ) static_assert( __VA_ARGS__ , #__VA_ARGS__ ); CATCH_SUCCEED( #__VA_ARGS__ ) +#define CATCH_STATIC_REQUIRE_FALSE( ... ) static_assert( !(__VA_ARGS__), "!(" #__VA_ARGS__ ")" ); CATCH_SUCCEED( #__VA_ARGS__ ) +#else +#define CATCH_STATIC_REQUIRE( ... ) CATCH_REQUIRE( __VA_ARGS__ ) +#define CATCH_STATIC_REQUIRE_FALSE( ... ) CATCH_REQUIRE_FALSE( __VA_ARGS__ ) +#endif + +// "BDD-style" convenience wrappers +#define CATCH_SCENARIO( ... ) CATCH_TEST_CASE( "Scenario: " __VA_ARGS__ ) +#define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ ) +#define CATCH_GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Given: " << desc ) +#define CATCH_AND_GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( "And given: " << desc ) +#define CATCH_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " When: " << desc ) +#define CATCH_AND_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And when: " << desc ) +#define CATCH_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Then: " << desc ) +#define CATCH_AND_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And: " << desc ) + +// If CATCH_CONFIG_PREFIX_ALL is not defined then the CATCH_ prefix is not required +#else + +#define REQUIRE( ... ) INTERNAL_CATCH_TEST( "REQUIRE", Catch::ResultDisposition::Normal, __VA_ARGS__ ) +#define REQUIRE_FALSE( ... ) INTERNAL_CATCH_TEST( "REQUIRE_FALSE", Catch::ResultDisposition::Normal | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) + +#define REQUIRE_THROWS( ... ) INTERNAL_CATCH_THROWS( "REQUIRE_THROWS", Catch::ResultDisposition::Normal, __VA_ARGS__ ) +#define REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "REQUIRE_THROWS_AS", exceptionType, Catch::ResultDisposition::Normal, expr ) +#define REQUIRE_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "REQUIRE_THROWS_WITH", Catch::ResultDisposition::Normal, matcher, expr ) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "REQUIRE_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::Normal, matcher, expr ) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define REQUIRE_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "REQUIRE_NOTHROW", Catch::ResultDisposition::Normal, __VA_ARGS__ ) + +#define CHECK( ... ) INTERNAL_CATCH_TEST( "CHECK", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CHECK_FALSE( ... ) INTERNAL_CATCH_TEST( "CHECK_FALSE", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) +#define CHECKED_IF( ... ) INTERNAL_CATCH_IF( "CHECKED_IF", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CHECKED_ELSE( ... ) INTERNAL_CATCH_ELSE( "CHECKED_ELSE", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CHECK_NOFAIL( ... ) INTERNAL_CATCH_TEST( "CHECK_NOFAIL", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::SuppressFail, __VA_ARGS__ ) + +#define CHECK_THROWS( ... ) INTERNAL_CATCH_THROWS( "CHECK_THROWS", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CHECK_THROWS_AS", exceptionType, Catch::ResultDisposition::ContinueOnFailure, expr ) +#define CHECK_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CHECK_THROWS_WITH", Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CHECK_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define CHECK_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CHECK_NOTHROW", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) + +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CHECK_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CHECK_THAT", matcher, Catch::ResultDisposition::ContinueOnFailure, arg ) + +#define REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "REQUIRE_THAT", matcher, Catch::ResultDisposition::Normal, arg ) +#endif // CATCH_CONFIG_DISABLE_MATCHERS + +#define INFO( msg ) INTERNAL_CATCH_INFO( "INFO", msg ) +#define WARN( msg ) INTERNAL_CATCH_MSG( "WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg ) +#define CAPTURE( ... ) INTERNAL_CATCH_CAPTURE( INTERNAL_CATCH_UNIQUE_NAME(capturer), "CAPTURE",__VA_ARGS__ ) + +#define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) +#define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) +#define METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ ) +#define REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ ) +#define SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ ) +#define DYNAMIC_SECTION( ... ) INTERNAL_CATCH_DYNAMIC_SECTION( __VA_ARGS__ ) +#define FAIL( ... ) INTERNAL_CATCH_MSG( "FAIL", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::Normal, __VA_ARGS__ ) +#define FAIL_CHECK( ... ) INTERNAL_CATCH_MSG( "FAIL_CHECK", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define SUCCEED( ... ) INTERNAL_CATCH_MSG( "SUCCEED", Catch::ResultWas::Ok, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE() + +#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#define TEMPLATE_TEST_CASE( ... ) INTERNAL_CATCH_TEMPLATE_TEST_CASE( __VA_ARGS__ ) +#define TEMPLATE_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD( className, __VA_ARGS__ ) +#else +#define TEMPLATE_TEST_CASE( ... ) INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE( __VA_ARGS__ ) ) +#define TEMPLATE_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD( className, __VA_ARGS__ ) ) +#endif + +#if !defined(CATCH_CONFIG_RUNTIME_STATIC_REQUIRE) +#define STATIC_REQUIRE( ... ) static_assert( __VA_ARGS__, #__VA_ARGS__ ); SUCCEED( #__VA_ARGS__ ) +#define STATIC_REQUIRE_FALSE( ... ) static_assert( !(__VA_ARGS__), "!(" #__VA_ARGS__ ")" ); SUCCEED( "!(" #__VA_ARGS__ ")" ) +#else +#define STATIC_REQUIRE( ... ) REQUIRE( __VA_ARGS__ ) +#define STATIC_REQUIRE_FALSE( ... ) REQUIRE_FALSE( __VA_ARGS__ ) +#endif + +#endif + +#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) + +// "BDD-style" convenience wrappers +#define SCENARIO( ... ) TEST_CASE( "Scenario: " __VA_ARGS__ ) +#define SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ ) + +#define GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Given: " << desc ) +#define AND_GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( "And given: " << desc ) +#define WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " When: " << desc ) +#define AND_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And when: " << desc ) +#define THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Then: " << desc ) +#define AND_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And: " << desc ) + +using Catch::Detail::Approx; + +#else // CATCH_CONFIG_DISABLE + +////// +// If this config identifier is defined then all CATCH macros are prefixed with CATCH_ +#ifdef CATCH_CONFIG_PREFIX_ALL + +#define CATCH_REQUIRE( ... ) (void)(0) +#define CATCH_REQUIRE_FALSE( ... ) (void)(0) + +#define CATCH_REQUIRE_THROWS( ... ) (void)(0) +#define CATCH_REQUIRE_THROWS_AS( expr, exceptionType ) (void)(0) +#define CATCH_REQUIRE_THROWS_WITH( expr, matcher ) (void)(0) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) +#endif// CATCH_CONFIG_DISABLE_MATCHERS +#define CATCH_REQUIRE_NOTHROW( ... ) (void)(0) + +#define CATCH_CHECK( ... ) (void)(0) +#define CATCH_CHECK_FALSE( ... ) (void)(0) +#define CATCH_CHECKED_IF( ... ) if (__VA_ARGS__) +#define CATCH_CHECKED_ELSE( ... ) if (!(__VA_ARGS__)) +#define CATCH_CHECK_NOFAIL( ... ) (void)(0) + +#define CATCH_CHECK_THROWS( ... ) (void)(0) +#define CATCH_CHECK_THROWS_AS( expr, exceptionType ) (void)(0) +#define CATCH_CHECK_THROWS_WITH( expr, matcher ) (void)(0) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define CATCH_CHECK_NOTHROW( ... ) (void)(0) + +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_CHECK_THAT( arg, matcher ) (void)(0) + +#define CATCH_REQUIRE_THAT( arg, matcher ) (void)(0) +#endif // CATCH_CONFIG_DISABLE_MATCHERS + +#define CATCH_INFO( msg ) (void)(0) +#define CATCH_WARN( msg ) (void)(0) +#define CATCH_CAPTURE( msg ) (void)(0) + +#define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) +#define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) +#define CATCH_METHOD_AS_TEST_CASE( method, ... ) +#define CATCH_REGISTER_TEST_CASE( Function, ... ) (void)(0) +#define CATCH_SECTION( ... ) +#define CATCH_DYNAMIC_SECTION( ... ) +#define CATCH_FAIL( ... ) (void)(0) +#define CATCH_FAIL_CHECK( ... ) (void)(0) +#define CATCH_SUCCEED( ... ) (void)(0) + +#define CATCH_ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) + +#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#define CATCH_TEMPLATE_TEST_CASE( ... ) INTERNAL_CATCH_TEMPLATE_TEST_CASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ) ) +#define CATCH_TEMPLATE_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ), className ) +#else +#define CATCH_TEMPLATE_TEST_CASE( ... ) INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ) ) ) +#define CATCH_TEMPLATE_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ), className ) ) +#endif + +// "BDD-style" convenience wrappers +#define CATCH_SCENARIO( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) +#define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), className ) +#define CATCH_GIVEN( desc ) +#define CATCH_AND_GIVEN( desc ) +#define CATCH_WHEN( desc ) +#define CATCH_AND_WHEN( desc ) +#define CATCH_THEN( desc ) +#define CATCH_AND_THEN( desc ) + +#define CATCH_STATIC_REQUIRE( ... ) (void)(0) +#define CATCH_STATIC_REQUIRE_FALSE( ... ) (void)(0) + +// If CATCH_CONFIG_PREFIX_ALL is not defined then the CATCH_ prefix is not required +#else + +#define REQUIRE( ... ) (void)(0) +#define REQUIRE_FALSE( ... ) (void)(0) + +#define REQUIRE_THROWS( ... ) (void)(0) +#define REQUIRE_THROWS_AS( expr, exceptionType ) (void)(0) +#define REQUIRE_THROWS_WITH( expr, matcher ) (void)(0) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define REQUIRE_NOTHROW( ... ) (void)(0) + +#define CHECK( ... ) (void)(0) +#define CHECK_FALSE( ... ) (void)(0) +#define CHECKED_IF( ... ) if (__VA_ARGS__) +#define CHECKED_ELSE( ... ) if (!(__VA_ARGS__)) +#define CHECK_NOFAIL( ... ) (void)(0) + +#define CHECK_THROWS( ... ) (void)(0) +#define CHECK_THROWS_AS( expr, exceptionType ) (void)(0) +#define CHECK_THROWS_WITH( expr, matcher ) (void)(0) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define CHECK_NOTHROW( ... ) (void)(0) + +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CHECK_THAT( arg, matcher ) (void)(0) + +#define REQUIRE_THAT( arg, matcher ) (void)(0) +#endif // CATCH_CONFIG_DISABLE_MATCHERS + +#define INFO( msg ) (void)(0) +#define WARN( msg ) (void)(0) +#define CAPTURE( msg ) (void)(0) + +#define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) +#define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) +#define METHOD_AS_TEST_CASE( method, ... ) +#define REGISTER_TEST_CASE( Function, ... ) (void)(0) +#define SECTION( ... ) +#define DYNAMIC_SECTION( ... ) +#define FAIL( ... ) (void)(0) +#define FAIL_CHECK( ... ) (void)(0) +#define SUCCEED( ... ) (void)(0) +#define ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) + +#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#define TEMPLATE_TEST_CASE( ... ) INTERNAL_CATCH_TEMPLATE_TEST_CASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ) ) +#define TEMPLATE_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ), className ) +#else +#define TEMPLATE_TEST_CASE( ... ) INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ) ) ) +#define TEMPLATE_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_M_P_L_A_T_E____T_E_S_T____ ), className ) ) +#endif + +#define STATIC_REQUIRE( ... ) (void)(0) +#define STATIC_REQUIRE_FALSE( ... ) (void)(0) + +#endif + +#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) + +// "BDD-style" convenience wrappers +#define SCENARIO( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ) ) +#define SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), className ) + +#define GIVEN( desc ) +#define AND_GIVEN( desc ) +#define WHEN( desc ) +#define AND_WHEN( desc ) +#define THEN( desc ) +#define AND_THEN( desc ) + +using Catch::Detail::Approx; + +#endif + +#endif // ! CATCH_CONFIG_IMPL_ONLY + +// start catch_reenable_warnings.h + + +#ifdef __clang__ +# ifdef __ICC // icpc defines the __clang__ macro +# pragma warning(pop) +# else +# pragma clang diagnostic pop +# endif +#elif defined __GNUC__ +# pragma GCC diagnostic pop +#endif + +// end catch_reenable_warnings.h +// end catch.hpp +#endif // TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED + diff --git a/src/teakra/include/teakra/disassembler.h b/src/teakra/include/teakra/disassembler.h new file mode 100644 index 0000000..f6af37e --- /dev/null +++ b/src/teakra/include/teakra/disassembler.h @@ -0,0 +1,23 @@ +#pragma once + +#include <array> +#include <cstdint> +#include <optional> +#include <string> +#include <vector> + +namespace Teakra::Disassembler { + +struct ArArpSettings { + std::array<std::uint16_t, 2> ar; + std::array<std::uint16_t, 4> arp; +}; + +bool NeedExpansion(std::uint16_t opcode); +bool NeedExpansion(std::uint16_t opcode); +std::vector<std::string> GetTokenList(std::uint16_t opcode, std::uint16_t expansion = 0, + std::optional<ArArpSettings> ar_arp = std::nullopt); +std::string Do(std::uint16_t opcode, std::uint16_t expansion = 0, + std::optional<ArArpSettings> ar_arp = std::nullopt); + +} // namespace Teakra::Disassembler diff --git a/src/teakra/include/teakra/disassembler_c.h b/src/teakra/include/teakra/disassembler_c.h new file mode 100644 index 0000000..8222a74 --- /dev/null +++ b/src/teakra/include/teakra/disassembler_c.h @@ -0,0 +1,17 @@ +#pragma once + +#include <stdint.h> +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +bool Teakra_Disasm_NeedExpansion(uint16_t opcode); + +size_t Teakra_Disasm_Do(char* dst, size_t dstlen, + uint16_t opcode, uint16_t expansion /*= 0*/); + +#ifdef __cplusplus +} +#endif diff --git a/src/teakra/include/teakra/teakra.h b/src/teakra/include/teakra/teakra.h new file mode 100644 index 0000000..32b9064 --- /dev/null +++ b/src/teakra/include/teakra/teakra.h @@ -0,0 +1,81 @@ +#pragma once + +#include <array> +#include <cstdint> +#include <functional> +#include <memory> + +namespace Teakra { + +struct AHBMCallback { + std::function<std::uint8_t(std::uint32_t address)> read8; + std::function<void(std::uint32_t address, std::uint8_t value)> write8; + + std::function<std::uint16_t(std::uint32_t address)> read16; + std::function<void(std::uint32_t address, std::uint16_t value)> write16; + + std::function<std::uint32_t(std::uint32_t address)> read32; + std::function<void(std::uint32_t address, std::uint32_t value)> write32; +}; + +class Teakra { +public: + Teakra(); + ~Teakra(); + + void Reset(); + + std::array<std::uint8_t, 0x80000>& GetDspMemory(); + const std::array<std::uint8_t, 0x80000>& GetDspMemory() const; + + // APBP Data + bool SendDataIsEmpty(std::uint8_t index) const; + void SendData(std::uint8_t index, std::uint16_t value); + bool RecvDataIsReady(std::uint8_t index) const; + std::uint16_t RecvData(std::uint8_t index); + std::uint16_t PeekRecvData(std::uint8_t index); + void SetRecvDataHandler(std::uint8_t index, std::function<void()> handler); + + // APBP Semaphore + void SetSemaphore(std::uint16_t value); + void ClearSemaphore(std::uint16_t value); + void MaskSemaphore(std::uint16_t value); + void SetSemaphoreHandler(std::function<void()> handler); + std::uint16_t GetSemaphore() const; + + // for implementing DSP_PDATA/PADR DMA transfers + std::uint16_t ProgramRead(std::uint32_t address) const; + void ProgramWrite(std::uint32_t address, std::uint16_t value); + std::uint16_t DataRead(std::uint16_t address, bool bypass_mmio = false); + void DataWrite(std::uint16_t address, std::uint16_t value, bool bypass_mmio = false); + std::uint16_t DataReadA32(std::uint32_t address) const; + void DataWriteA32(std::uint32_t address, std::uint16_t value); + std::uint16_t MMIORead(std::uint16_t address); + void MMIOWrite(std::uint16_t address, std::uint16_t value); + + // DSP_PADR is only 16-bit, so this is where the DMA interface gets the + // upper 16-bits from + std::uint16_t DMAChan0GetSrcHigh(); + std::uint16_t DMAChan0GetDstHigh(); + + std::uint16_t AHBMGetUnitSize(std::uint16_t i) const; + std::uint16_t AHBMGetDirection(std::uint16_t i) const; + std::uint16_t AHBMGetDmaChannel(std::uint16_t i) const; + // we need these as AHBM does some weird stuff on unaligned accesses internally + std::uint16_t AHBMRead16(std::uint32_t addr); + void AHBMWrite16(std::uint32_t addr, std::uint16_t value); + std::uint16_t AHBMRead32(std::uint32_t addr); + void AHBMWrite32(std::uint32_t addr, std::uint32_t value); + + // core + void Run(unsigned cycle); + + void SetAHBMCallback(const AHBMCallback& callback); + + void SetAudioCallback(std::function<void(std::array<std::int16_t, 2>)> callback); + +private: + struct Impl; + std::unique_ptr<Impl> impl; +}; +} // namespace Teakra diff --git a/src/teakra/include/teakra/teakra_c.h b/src/teakra/include/teakra/teakra_c.h new file mode 100644 index 0000000..d52a9f1 --- /dev/null +++ b/src/teakra/include/teakra/teakra_c.h @@ -0,0 +1,79 @@ +#pragma once + +#include <stdint.h> +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +struct TeakraObject; +typedef struct TeakraObject TeakraContext; + +typedef void (*Teakra_InterruptCallback)(void* userdata); +typedef void (*Teakra_AudioCallback)(void* userdata, int16_t samples[2]); + +typedef uint8_t (*Teakra_AHBMReadCallback8)(void* userdata, uint32_t address); +typedef void (*Teakra_AHBMWriteCallback8)(void* userdata, uint32_t address, uint8_t value); + +typedef uint16_t (*Teakra_AHBMReadCallback16)(void* userdata, uint32_t address); +typedef void (*Teakra_AHBMWriteCallback16)(void* userdata, uint32_t address, uint16_t value); + +typedef uint32_t (*Teakra_AHBMReadCallback32)(void* userdata, uint32_t address); +typedef void (*Teakra_AHBMWriteCallback32)(void* userdata, uint32_t address, uint32_t value); + +TeakraContext* Teakra_Create(); +void Teakra_Destroy(TeakraContext* context); +void Teakra_Reset(TeakraContext* context); +uint8_t* Teakra_GetDspMemory(TeakraContext* context); + +int Teakra_SendDataIsEmpty(const TeakraContext* context, uint8_t index); +void Teakra_SendData(TeakraContext* context, uint8_t index, uint16_t value); +int Teakra_RecvDataIsReady(const TeakraContext* context, uint8_t index); +uint16_t Teakra_RecvData(TeakraContext* context, uint8_t index); +uint16_t Teakra_PeekRecvData(TeakraContext* context, uint8_t index); +void Teakra_SetRecvDataHandler(TeakraContext* context, uint8_t index, + Teakra_InterruptCallback handler, void* userdata); + +void Teakra_SetSemaphore(TeakraContext* context, uint16_t value); +void Teakra_ClearSemaphore(TeakraContext* context, uint16_t value); +void Teakra_MaskSemaphore(TeakraContext* context, uint16_t value); +void Teakra_SetSemaphoreHandler(TeakraContext* context, Teakra_InterruptCallback handler, + void* userdata); +uint16_t Teakra_GetSemaphore(const TeakraContext* context); + +uint16_t Teakra_ProgramRead(TeakraContext* context, uint32_t address); +void Teakra_ProgramWrite(TeakraContext* context, uint32_t address, uint16_t value); +uint16_t Teakra_DataRead(TeakraContext* context, uint16_t address, bool bypass_mmio); +void Teakra_DataWrite(TeakraContext* context, uint16_t address, uint16_t value, bool bypass_mmio); +uint16_t Teakra_DataReadA32(TeakraContext* context, uint32_t address); +void Teakra_DataWriteA32(TeakraContext* context, uint32_t address, uint16_t value); +uint16_t Teakra_MMIORead(TeakraContext* context, uint16_t address); +void Teakra_MMIOWrite(TeakraContext* context, uint16_t address, uint16_t value); + +uint16_t Teakra_DMAChan0GetSrcHigh(TeakraContext* context); +uint16_t Teakra_DMAChan0GetDstHigh(TeakraContext* context); + +uint16_t Teakra_AHBMGetUnitSize(TeakraContext* context, uint16_t i); +uint16_t Teakra_AHBMGetDirection(TeakraContext* context, uint16_t i); +uint16_t Teakra_AHBMGetDmaChannel(TeakraContext* context, uint16_t i); + +uint16_t Teakra_AHBMRead16(TeakraContext* context, uint32_t addr); +void Teakra_AHBMWrite16(TeakraContext* context, uint32_t addr, uint16_t value); +uint16_t Teakra_AHBMRead32(TeakraContext* context, uint32_t addr); +void Teakra_AHBMWrite32(TeakraContext* context, uint32_t addr, uint32_t value); + + +void Teakra_Run(TeakraContext* context, unsigned cycle); + +void Teakra_SetAHBMCallback(TeakraContext* context, + Teakra_AHBMReadCallback8 read8 , Teakra_AHBMWriteCallback8 write8 , + Teakra_AHBMReadCallback16 read16, Teakra_AHBMWriteCallback16 write16, + Teakra_AHBMReadCallback32 read32, Teakra_AHBMWriteCallback32 write32, + void* userdata); + + +void Teakra_SetAudioCallback(TeakraContext* context, Teakra_AudioCallback callback, void* userdata); +#ifdef __cplusplus +} +#endif diff --git a/src/teakra/src/CMakeLists.txt b/src/teakra/src/CMakeLists.txt new file mode 100644 index 0000000..b96c500 --- /dev/null +++ b/src/teakra/src/CMakeLists.txt @@ -0,0 +1,64 @@ +include(CreateDirectoryGroups) + +add_library(teakra + ../include/teakra/disassembler.h + ../include/teakra/teakra.h + ahbm.cpp + ahbm.h + apbp.cpp + apbp.h + bit.h + btdmp.cpp + btdmp.h + common_types.h + crash.h + decoder.h + disassembler.cpp + dma.cpp + dma.h + timer.cpp + timer.h + icu.h + interpreter.h + matcher.h + memory_interface.cpp + memory_interface.h + mmio.cpp + mmio.h + operand.h + parser.cpp + processor.cpp + processor.h + register.h + shared_memory.h + teakra.cpp + test.h + test_generator.cpp + test_generator.h +) + +create_target_directory_groups(teakra) + +target_link_libraries(teakra PRIVATE Threads::Threads) +target_include_directories(teakra + PUBLIC ../include + PRIVATE .) +target_compile_options(teakra PRIVATE ${TEAKRA_CXX_FLAGS}) + +add_library(teakra_c + ../include/teakra/disassembler_c.h + ../include/teakra/teakra_c.h + disassembler_c.cpp + teakra_c.cpp +) +target_link_libraries(teakra_c PRIVATE teakra) + +if (TEAKRA_BUILD_TOOLS) + add_subdirectory(coff_reader) + add_subdirectory(dsp1_reader) + add_subdirectory(test_generator) + add_subdirectory(test_verifier) + add_subdirectory(mod_test_generator) + add_subdirectory(step2_test_generator) + add_subdirectory(makedsp1) +endif() diff --git a/src/teakra/src/README.md b/src/teakra/src/README.md new file mode 100644 index 0000000..85f0873 --- /dev/null +++ b/src/teakra/src/README.md @@ -0,0 +1,29 @@ +## Content + + - main library + - [processor related](processor_general.md) + - operand: defines basic operand types used in instructions + - matcher and decoder: decodes binary instructions into opcodes and operands + - disassembler: translate binary instructions to (pseudo-)assembly. + - parser: translate (pseudo-)assembly to binary instructions + - interpreter: executes instructions + - [register](register.md): defines all register states in the processor + - processor: wrapper of interpreter and register as a processor emulator + - test_generator: generates test cases information for the instruction set + - peripherals + - [AHBM](ahbm.md): interface for accessing external memory (DSi/3DS main memory) + - [APBP](apbp.md): interface for communication with CPU (ARM in DSi/3DS) + - [BTDMP](btdmp.md): audio input/output ports + - [DMA](dma.md): engine for transferring large data between DSP memory and external memory + - [ICU](icu.md): interrupt controller unit + - [timer](timer.md) + - [MMIO](mmio.md): I/O ports for all peripherals + - shared_memory: the DSP working memory + - [memory_interface](miu.md): the memory space exposed to the processor and related control + - Tools + - coff_reader: disassembles and parses symbols COFF files leaked by some DSi applications + - dsp1_reader: disassembles DSP1 files, DSP binary for 3DS applications + - makedsp1: assembles DSP1 files + - test_generator: generate random test cases for processor instructions. + - mod_test_generator & step2_test_generator: similar to test_generator, but dedicated for mod/step2 related instructions + - test_verifier: verify test cases on the interpreter against the result generated from 3DS diff --git a/src/teakra/src/ahbm.cpp b/src/teakra/src/ahbm.cpp new file mode 100644 index 0000000..c51419b --- /dev/null +++ b/src/teakra/src/ahbm.cpp @@ -0,0 +1,159 @@ +#include <cstdio> +#include "ahbm.h" + +namespace Teakra { + +void Ahbm::Reset() { + busy_flag = 0; + channels = {}; +} + +unsigned Ahbm::Channel::GetBurstSize() { + switch (burst_size) { + case Ahbm::BurstSize::X1: + return 1; + case Ahbm::BurstSize::X4: + return 4; + case Ahbm::BurstSize::X8: + return 8; + default: + std::printf("Unknown burst size %04X\n", static_cast<u16>(burst_size)); + return 1; + } +} + +u16 Ahbm::Read16(u16 channel, u32 address) { + u32 value32 = Read32(channel, address); + if ((address & 1) == 0) { + return (u16)(value32 & 0xFFFF); + } else { + return (u16)(value32 >> 16); + } +} +u32 Ahbm::Read32(u16 channel, u32 address) { + if (channels[channel].direction != Direction::Read) { + std::printf("Wrong direction!\n"); + } + + if (channels[channel].burst_queue.empty()) { + u32 current = address; + unsigned size = channels[channel].GetBurstSize(); + for (unsigned i = 0; i < size; ++i) { + u32 value = 0; + switch (channels[channel].unit_size) { + case UnitSize::U8: + value = read_external8(current); + if ((current & 1) == 1) { + value <<= 8; // this weird bahiviour is hwtested + } + current += 1; + break; + case UnitSize::U16: { + u32 current_masked = current & 0xFFFFFFFE; + value = read_external16(current_masked); + current += 2; + break; + } + case UnitSize::U32: { + u32 current_masked = current & 0xFFFFFFFC; + value = read_external32(current_masked); + current += 4; + break; + } + default: + std::printf("Unknown unit size %04X\n", + static_cast<u16>(channels[channel].unit_size)); + break; + } + channels[channel].burst_queue.push(value); + } + } + + u32 value = channels[channel].burst_queue.front(); + channels[channel].burst_queue.pop(); + return value; +} +void Ahbm::Write16(u16 channel, u32 address, u16 value) { + WriteInternal(channel, address, value); +} +void Ahbm::Write32(u16 channel, u32 address, u32 value) { + if ((address & 1) == 1) { + value >>= 16; // this weird behaviour is hwtested + } + WriteInternal(channel, address, value); +} + +void Ahbm::WriteInternal(u16 channel, u32 address, u32 value) { + if (channels[channel].direction != Direction::Write) { + std::printf("Wrong direction!\n"); + } + + if (channels[channel].burst_queue.empty()) { + channels[channel].write_burst_start = address; + } + + channels[channel].burst_queue.push(value); + if (channels[channel].burst_queue.size() >= channels[channel].GetBurstSize()) { + u32 current = channels[channel].write_burst_start; + while (!channels[channel].burst_queue.empty()) { + u32 value32 = channels[channel].burst_queue.front(); + channels[channel].burst_queue.pop(); + switch (channels[channel].unit_size) { + case UnitSize::U8: { + // this weird behaviour is hwtested + u8 value8 = ((current & 1) == 1) ? (u8)(value32 >> 8) : (u8)value32; + write_external8(current, value8); + current += 1; + break; + } + case UnitSize::U16: { + u32 c0 = current & 0xFFFFFFFE; + u32 c1 = c0 + 1; + if (c0 >= current) { + write_external16(c0, (u16)value32); + } else { + write_external8(c1, (u8)(value32 >> 8)); + } + current += 2; + break; + } + case UnitSize::U32: { + u32 c0 = current & 0xFFFFFFFC; + u32 c1 = c0 + 1; + u32 c2 = c0 + 2; + u32 c3 = c0 + 3; + + if (c0 >= current && c1 >= current && c2 >= current) { + write_external32(c0, value32); + } else if (c2 >= current) { + if (c1 >= current) { + write_external8(c1, (u8)(value32 >> 8)); + } + write_external16(c2, (u16)(value32 >> 16)); + } else { + write_external8(c3, (u8)(value32 >> 24)); + } + + current += 4; + break; + } + default: + std::printf("Unknown unit size %04X\n", + static_cast<u16>(channels[channel].unit_size)); + break; + } + } + } +} + +u16 Ahbm::GetChannelForDma(u16 dma_channel) const { + for (u16 channel = 0; channel < channels.size(); ++channel) { + if ((channels[channel].dma_channel >> dma_channel) & 1) { + return channel; + } + } + std::printf("Could not find AHBM channel for DMA channel %04X\n", dma_channel); + return 0; +} + +} // namespace Teakra diff --git a/src/teakra/src/ahbm.h b/src/teakra/src/ahbm.h new file mode 100644 index 0000000..5d476fe --- /dev/null +++ b/src/teakra/src/ahbm.h @@ -0,0 +1,103 @@ +#pragma once +#include <array> +#include <functional> +#include <utility> +#include <queue> +#include "common_types.h" + +namespace Teakra { + +class Ahbm { +public: + enum class UnitSize : u16 { + U8 = 0, + U16 = 1, + U32 = 2, + }; + + enum class BurstSize : u16 { + X1 = 0, + X4 = 1, + X8 = 2, + }; + + enum class Direction : u16 { + Read = 0, + Write = 1, + }; + + void Reset(); + + u16 GetBusyFlag() const { + return busy_flag; + } + void SetUnitSize(u16 i, u16 value) { + channels[i].unit_size = static_cast<UnitSize>(value); + } + u16 GetUnitSize(u16 i) const { + return static_cast<u16>(channels[i].unit_size); + } + void SetBurstSize(u16 i, u16 value) { + channels[i].burst_size = static_cast<BurstSize>(value); + } + u16 GetBurstSize(u16 i) const { + return static_cast<u16>(channels[i].burst_size); + } + void SetDirection(u16 i, u16 value) { + channels[i].direction = static_cast<Direction>(value); + } + u16 GetDirection(u16 i) const { + return static_cast<u16>(channels[i].direction); + } + void SetDmaChannel(u16 i, u16 value) { + channels[i].dma_channel = value; + } + u16 GetDmaChannel(u16 i) const { + return channels[i].dma_channel; + } + + u16 Read16(u16 channel, u32 address); + u32 Read32(u16 channel, u32 address); + void Write16(u16 channel, u32 address, u16 value); + void Write32(u16 channel, u32 address, u32 value); + + u16 GetChannelForDma(u16 dma_channel) const; + + void SetExternalMemoryCallback( + std::function<u8 (u32)> read8 , std::function<void(u32, u8 )> write8 , + std::function<u16(u32)> read16, std::function<void(u32, u16)> write16, + std::function<u32(u32)> read32, std::function<void(u32, u32)> write32) { + + read_external8 = std::move(read8); + write_external8 = std::move(write8); + read_external16 = std::move(read16); + write_external16 = std::move(write16); + read_external32 = std::move(read32); + write_external32 = std::move(write32); + } + +private: + u16 busy_flag = 0; + struct Channel { + UnitSize unit_size = UnitSize::U8; + BurstSize burst_size = BurstSize::X1; + Direction direction = Direction::Read; + u16 dma_channel = 0; + + std::queue<u32> burst_queue; + u32 write_burst_start = 0; + unsigned GetBurstSize(); + }; + std::array<Channel, 3> channels; + + std::function<u8(u32)> read_external8; + std::function<void(u32, u8)> write_external8; + std::function<u16(u32)> read_external16; + std::function<void(u32, u16)> write_external16; + std::function<u32(u32)> read_external32; + std::function<void(u32, u32)> write_external32; + + void WriteInternal(u16 channel, u32 address, u32 value); +}; + +} // namespace Teakra diff --git a/src/teakra/src/ahbm.md b/src/teakra/src/ahbm.md new file mode 100644 index 0000000..783304a --- /dev/null +++ b/src/teakra/src/ahbm.md @@ -0,0 +1,37 @@ +# AHBM + +## MMIO Layout + +``` +Status register ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00E0 | | | | | | | | | | | | | |RNE| | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +Applications wait for all bits to be 0 before connecting AHBM to DMA +RNE: 1 when the burst queue is not empty + +Channel config (N = 0, 1, 2) ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00E2+N*6| - | | | | | | - | TYPE | | BURST | R | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00E4+N*6| - | E | W | | | | | | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00E6+N*6| - | D7| D6| D5| D4| D3| D2| D1| D0| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +R: Applications set this to 1 if BURST is non-zero +BURST: burst type + - 0: x1 + - 1: x4 + - 2: x8 + - 3: ? +TYPE: data type + - 0: 8 bit + - 1: 16 bit + - 2: 32 bit + - 3: ? +W: Transfer direction + - 0: read from external memory + - 1: write to external memory +E: Applications always set this. +D0..D7: Connects to DMA channel 0..7 if set to one +``` diff --git a/src/teakra/src/apbp.cpp b/src/teakra/src/apbp.cpp new file mode 100644 index 0000000..da30168 --- /dev/null +++ b/src/teakra/src/apbp.cpp @@ -0,0 +1,149 @@ +#include <array> +#include <atomic> +#include <mutex> +#include <utility> +#include "apbp.h" + +namespace Teakra { +class DataChannel { +public: + void Reset() { + ready = false; + data = 0; + } + + void Send(u16 data) { + { + std::lock_guard lock(mutex); + ready = true; + this->data = data; + if (disable_interrupt) + return; + } + if (handler) + handler(); + } + u16 Recv() { + std::lock_guard lock(mutex); + ready = false; + return data; + } + u16 Peek() const { + std::lock_guard lock(mutex); + return data; + } + bool IsReady() const { + std::lock_guard lock(mutex); + return ready; + } + u16 GetDisableInterrupt() const { + std::lock_guard lock(mutex); + return disable_interrupt; + } + void SetDisableInterrupt(u16 v) { + disable_interrupt = v; + } + + std::function<void()> handler; + +private: + bool ready = false; + u16 data = 0; + u16 disable_interrupt = 0; + mutable std::mutex mutex; +}; + +class Apbp::Impl { +public: + std::array<DataChannel, 3> data_channels; + u16 semaphore = 0; + u16 semaphore_mask = 0; + bool semaphore_master_signal = false; + mutable std::recursive_mutex semaphore_mutex; + std::function<void()> semaphore_handler; + + void Reset() { + for (auto& c : data_channels) + c.Reset(); + semaphore = 0; + semaphore_mask = 0; + semaphore_master_signal = false; + } +}; + +Apbp::Apbp() : impl(new Impl) {} +Apbp::~Apbp() = default; + +void Apbp::Reset() { + impl->Reset(); +} + +void Apbp::SendData(unsigned channel, u16 data) { + impl->data_channels[channel].Send(data); +} + +u16 Apbp::RecvData(unsigned channel) { + return impl->data_channels[channel].Recv(); +} + +u16 Apbp::PeekData(unsigned channel) const { + return impl->data_channels[channel].Peek(); +} + +bool Apbp::IsDataReady(unsigned channel) const { + return impl->data_channels[channel].IsReady(); +} + +u16 Apbp::GetDisableInterrupt(unsigned channel) const { + return impl->data_channels[channel].GetDisableInterrupt(); +} + +void Apbp::SetDisableInterrupt(unsigned channel, u16 v) { + impl->data_channels[channel].SetDisableInterrupt(v); +} + +void Apbp::SetDataHandler(unsigned channel, std::function<void()> handler) { + impl->data_channels[channel].handler = std::move(handler); +} + +void Apbp::SetSemaphore(u16 bits) { + std::lock_guard lock(impl->semaphore_mutex); + impl->semaphore |= bits; + bool new_signal = (impl->semaphore & ~impl->semaphore_mask) != 0; + if (new_signal && impl->semaphore_handler) { + impl->semaphore_handler(); + } + impl->semaphore_master_signal = impl->semaphore_master_signal || new_signal; +} + +void Apbp::ClearSemaphore(u16 bits) { + std::lock_guard lock(impl->semaphore_mutex); + impl->semaphore &= ~bits; + impl->semaphore_master_signal = (impl->semaphore & ~impl->semaphore_mask) != 0; +} + +u16 Apbp::GetSemaphore() const { + std::lock_guard lock(impl->semaphore_mutex); + return impl->semaphore; +} + +void Apbp::MaskSemaphore(u16 bits) { + std::lock_guard lock(impl->semaphore_mutex); + impl->semaphore_mask = bits; +} + +u16 Apbp::GetSemaphoreMask() const { + std::lock_guard lock(impl->semaphore_mutex); + return impl->semaphore_mask; +} + +void Apbp::SetSemaphoreHandler(std::function<void()> handler) { + std::lock_guard lock(impl->semaphore_mutex); + impl->semaphore_handler = std::move(handler); +} + +bool Apbp::IsSemaphoreSignaled() const { + std::lock_guard lock(impl->semaphore_mutex); + return impl->semaphore_master_signal; +} +} // namespace Teakra diff --git a/src/teakra/src/apbp.h b/src/teakra/src/apbp.h new file mode 100644 index 0000000..f9ac678 --- /dev/null +++ b/src/teakra/src/apbp.h @@ -0,0 +1,36 @@ +#pragma once +#include <array> +#include <functional> +#include <memory> +#include "common_types.h" + +namespace Teakra { +class Apbp { +public: + Apbp(); + ~Apbp(); + + void Reset(); + + void SendData(unsigned channel, u16 data); + u16 RecvData(unsigned channel); + u16 PeekData(unsigned channel) const; + bool IsDataReady(unsigned channel) const; + u16 GetDisableInterrupt(unsigned channel) const; + void SetDisableInterrupt(unsigned channel, u16 v); + void SetDataHandler(unsigned channel, std::function<void()> handler); + + void SetSemaphore(u16 bits); + void ClearSemaphore(u16 bits); + u16 GetSemaphore() const; + void MaskSemaphore(u16 bits); + u16 GetSemaphoreMask() const; + void SetSemaphoreHandler(std::function<void()> handler); + + bool IsSemaphoreSignaled() const; + +private: + class Impl; + std::unique_ptr<Impl> impl; +}; +} // namespace Teakra diff --git a/src/teakra/src/apbp.md b/src/teakra/src/apbp.md new file mode 100644 index 0000000..b26f2d0 --- /dev/null +++ b/src/teakra/src/apbp.md @@ -0,0 +1,110 @@ +# APBP + +## MMIO Layout + +``` +Command/Reply registers ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00C0 | REPLY0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00C2 | CMD0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00C4 | REPLY1 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00C6 | CMD1 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00C8 | REPLY2 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00CA | CMD2 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +REPLY0..2: data sent from DSP to CPU +CMD0..2: data received from CPU to DSP + +Semaphore registers ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00CC | SET_SEMAPHORE | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00CE | MASK_SEMAPHORE | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00D0 | ACK_SEMAPHORE | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00D2 | GET_SEMAPHORE | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +SET_SEMAPHORE: sets semaphore sent from DSP to CPU +MASK_SEMAPHORE: masks semaphore interrupt received from CPU to DSP +ACK_SEMAPHORE: acknowledges/clears semaphore received from CPU to DSP +GET_SEMAPHORE: semaphore received from CPU to DSP + +Config/status registers ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00D4 | |CI2|CI1| |CI0| |END| | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00D6 | | | C2| C1| | | S | C0| R2| R1| R0| | | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00D8 | C2| C1| C0| R2| R1| R0| S'|WEM|WFL|RNE|RFL| |PRS|WTU|RTU| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +END: CPU-side registers endianness. 1 to swap byte order of CPU-side registers +R0..R2: 1 when there is data in REPLY0..2 +C0..C2: 1 when there is data in CMD0..2 +CI0...CI2: 1 to disable interrupt when CMD0..2 is written by CPU +S: 1 when (GET_SEMAPHORE & ~MASK_SEMAPHORE) is non-zero +S': similar to S, but for CPU side +RTU: CPU-side read transfer underway flag +WTU: CPU-side write transfer underway flag +PRS: peripheral reset flag +RFL: CPU-side read FIFO full flag +RNE: CPU-side read FIFO non-empty flag +WFL: CPU-side write FIFO full flag +WEM: CPU-side write FIFO empty flag +* Note 0x00D8 is a mirror of CPU-side register DSP_PSTS +``` + +## CPU-DSP communication + +APBP is an important port for CPU and DSP to communicate with each other. It mainly contains 3 pairs of symmetrical data channels and a pair of 16-bit symmetrical semaphore channel. + +### Data channels + +When one side writes to a data channel, it sets the 1 to the "data ready" bit (`R0..R2` or `C0..C2`), and fires interrupt on the other side if enabled. When the othersides read from the channel register, it automatically clears the "data ready" bit. If new data is written before the previous one is read, the new data will overwrite the old data and fires another interrupt. + +### Semaphore channels + +There are two 16-bit semaphore channels for two directions, `CPU->DSP` and `DSP->CPU`. Writing to `SET_SEMAPHORE` from side A or `ACK_SEMAPHORE` from side B changes the semaphore value of direction `A->B`. Semaphore value change also changes the corresponding `S` bit (See the calculation above). Changing in mask is also reflected in the `S` bit immediately. Whenever a `0->1` transition for `S` is detected, interrupt is fired on B side. + +## CPU side MMIO + +``` +Command/Reply registers ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0000 | PDATA | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0004 | PADDR | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0008 | DATA_SPACE |RI2|RI1|RI0|IWE|IWF|IRE|IRF|STR| BURST |INC|RST| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x000C | C2| C1| C0| R2| R1| R0| S'|WEM|WFL|RNE|RFL| |PRS|WTU|RTU| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0010 | SET_SEMAPHORE' | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0014 | MASK_SEMAPHORE' | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0018 | ACK_SEMAPHORE' | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x001C | GET_SEMAPHORE' | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0020 | CMD0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0024 | REPLY0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0028 | CMD1 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x002C | REPLY1 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0030 | CMD2 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0034 | REPLY2 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +``` diff --git a/src/teakra/src/bit.h b/src/teakra/src/bit.h new file mode 100644 index 0000000..7379329 --- /dev/null +++ b/src/teakra/src/bit.h @@ -0,0 +1,26 @@ +#pragma once + +#include <limits> +#include <type_traits> +#ifdef _MSC_VER +#include <intrin.h> +#endif + +namespace std20 { + +// A simple (and not very correct) implementation of C++20's std::log2p1 +template <class T> +constexpr T log2p1(T x) noexcept { + static_assert(std::is_integral_v<T> && std::is_unsigned_v<T>); + if (x == 0) + return 0; +#ifdef _MSC_VER + unsigned long index = 0; + _BitScanReverse64(&index, x); + return static_cast<T>(index) + 1; +#else + return static_cast<T>(std::numeric_limits<unsigned long long>::digits - __builtin_clzll(x)); +#endif +} + +} // namespace std20 diff --git a/src/teakra/src/btdmp.cpp b/src/teakra/src/btdmp.cpp new file mode 100644 index 0000000..20a1f09 --- /dev/null +++ b/src/teakra/src/btdmp.cpp @@ -0,0 +1,97 @@ +#include <string> +#include "btdmp.h" +#include "crash.h" + +namespace Teakra { + +Btdmp::Btdmp(CoreTiming& core_timing) { + core_timing.RegisterCallbacks(this); +} + +Btdmp::~Btdmp() = default; + +void Btdmp::Reset() { + transmit_clock_config = 0; + transmit_period = 4096; + transmit_timer = 0; + transmit_enable = 0; + transmit_empty = true; + transmit_full = false; + transmit_queue = {}; +} + +void Btdmp::Tick() { + if (transmit_enable) { + ++transmit_timer; + if (transmit_timer >= transmit_period) { + transmit_timer = 0; + std::array<std::int16_t, 2> sample; + for (int i = 0; i < 2; ++i) { + if (transmit_queue.empty()) { + std::printf("BTDMP: transmit buffer underrun\n"); + sample[i] = 0; + } else { + sample[i] = static_cast<s16>(transmit_queue.front()); + transmit_queue.pop(); + transmit_empty = transmit_queue.empty(); + transmit_full = false; + if (transmit_empty) { + interrupt_handler(); + } + } + } + if (audio_callback) { + audio_callback(sample); + } + } + } +} + +u64 Btdmp::GetMaxSkip() const { + if (!transmit_enable || transmit_queue.empty()) { + return Infinity; + } + + u64 ticks = 0; + if (transmit_timer < transmit_period) { + // number of ticks before the tick of the next transmit + ticks += transmit_period - transmit_timer - 1; + } + + // number of ticks from the next transmit to the one just before the transmit that empties + // the buffer + ticks += ((transmit_queue.size() + 1) / 2 - 1) * transmit_period; + + return ticks; +} + +void Btdmp::Skip(u64 ticks) { + if (!transmit_enable) + return; + + if (transmit_timer >= transmit_period) + transmit_timer = 0; + + u64 future_timer = transmit_timer + ticks; + u64 cycles = future_timer / transmit_period; + transmit_timer = (u16)(future_timer % transmit_period); + + for (u64 c = 0; c < cycles; ++c) { + std::array<std::int16_t, 2> sample; + for (int i = 0; i < 2; ++i) { + if (transmit_queue.empty()) { + sample[i] = 0; + } else { + sample[i] = static_cast<s16>(transmit_queue.front()); + transmit_queue.pop(); + ASSERT(!transmit_queue.empty()); + transmit_full = false; + } + } + if (audio_callback) { + audio_callback(sample); + } + } +} + +} // namespace Teakra diff --git a/src/teakra/src/btdmp.h b/src/teakra/src/btdmp.h new file mode 100644 index 0000000..63cbb9b --- /dev/null +++ b/src/teakra/src/btdmp.h @@ -0,0 +1,99 @@ +#pragma once +#include <array> +#include <cstdio> +#include <functional> +#include <utility> +#include <queue> +#include "common_types.h" +#include "core_timing.h" + +namespace Teakra { + +class Btdmp : public CoreTiming::Callbacks { +public: + Btdmp(CoreTiming& core_timing); + ~Btdmp(); + + void Reset(); + + void SetTransmitClockConfig(u16 value) { + transmit_clock_config = value; + } + + u16 GetTransmitClockConfig() const { + return transmit_clock_config; + } + + void SetTransmitPeriod(u16 value) { + transmit_period = value; + } + + u16 GetTransmitPeriod() const { + return transmit_period; + } + + void SetTransmitEnable(u16 value) { + transmit_enable = value; + } + + u16 GetTransmitEnable() const { + return transmit_enable; + } + + u16 GetTransmitEmpty() const { + return transmit_empty; + } + + u16 GetTransmitFull() const { + return transmit_full; + } + + void Send(u16 value) { + if (transmit_queue.size() == 16) { + std::printf("BTDMP: transmit buffer overrun\n"); + } else { + transmit_queue.push(value); + transmit_empty = false; + transmit_full = transmit_queue.size() == 16; + } + } + + void SetTransmitFlush(u16 value) { + transmit_queue = {}; + transmit_empty = true; + transmit_full = false; + } + + u16 GetTransmitFlush() const { + return 0; + } + + void Tick() override; + u64 GetMaxSkip() const override; + void Skip(u64 ticks) override; + + void SetAudioCallback(std::function<void(std::array<std::int16_t, 2>)> callback) { + audio_callback = std::move(callback); + } + + void SetInterruptHandler(std::function<void()> handler) { + interrupt_handler = std::move(handler); + } + +private: + // TODO: figure out the relation between clock_config and period. + // Default to period = 4096 for now which every game uses + u16 transmit_clock_config = 0; + u16 transmit_period = 4096; + u16 transmit_timer = 0; + u16 transmit_enable = 0; + bool transmit_empty = true; + bool transmit_full = false; + std::queue<u16> transmit_queue; + std::function<void(std::array<std::int16_t, 2>)> audio_callback; + std::function<void()> interrupt_handler; + + class BtdmpTimingCallbacks; +}; + +} // namespace Teakra diff --git a/src/teakra/src/btdmp.md b/src/teakra/src/btdmp.md new file mode 100644 index 0000000..21d8daf --- /dev/null +++ b/src/teakra/src/btdmp.md @@ -0,0 +1,82 @@ +# BTDMP + +## MMIO Layout + +``` +Receive config registers ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0280 | | | | | | |RIR| | | | | | | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0282 | | | | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0284 | "0004"? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0286 | "0021"? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0288 | "0000"? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x028A | "0000"? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x028C | "0000"? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x028E | ? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0290 | ? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +... ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x029E |RE | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +RIR: enable IRQ for receive if 1 +RE: enable receive if 1 + +Transmit config registers ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02A0 | | | | | | |TIR| | | | | | | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02A2 | | | | | | | <- clock related, "1004" ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02A4 | "0004"? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02A6 | "0021"? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02A8 | "0000"? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02AA | "0000"? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02AC | "0000"? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02AE | ? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02B0 | ? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +... ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02BE |RT | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +TIR: enable IRQ for transmit if 1 +TE: enable transmit if 1 + +Receive/transmit status and data registers ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02C0 | |RF | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02C2 | |TE |TF | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02C4 | FIFO_RECEIVE | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02C6 | FIFO_TRANSMIT | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02C8 | ? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x02CA | |FL | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +RF: 1 if receive buffer is full +TF: 1 if receive buffer is full +TE: 1 if receive buffer is empty +FL: Application spin waits on this flag + +``` diff --git a/src/teakra/src/coff_reader/CMakeLists.txt b/src/teakra/src/coff_reader/CMakeLists.txt new file mode 100644 index 0000000..eabd181 --- /dev/null +++ b/src/teakra/src/coff_reader/CMakeLists.txt @@ -0,0 +1,10 @@ +include(CreateDirectoryGroups) + +add_executable(coff_reader + coff.h + main.cpp +) +create_target_directory_groups(coff_reader) +target_link_libraries(coff_reader PRIVATE teakra) +target_include_directories(coff_reader PRIVATE .) +target_compile_options(coff_reader PRIVATE ${TEAKRA_CXX_FLAGS}) diff --git a/src/teakra/src/coff_reader/coff.h b/src/teakra/src/coff_reader/coff.h new file mode 100644 index 0000000..4e857ce --- /dev/null +++ b/src/teakra/src/coff_reader/coff.h @@ -0,0 +1,367 @@ +#include <algorithm> +#include <cstdint> +#include <cstdio> +#include <cstring> +#include <string> +#include <unordered_map> +#include <variant> +#include <vector> + +using u8 = std::uint8_t; +using u16 = std::uint16_t; +using u32 = std::uint32_t; +using u64 = std::uint64_t; + +struct NameOrIndex { + u8 value[8]; + std::variant<std::string, u32> Get() { + u32 temp; + std::memcpy(&temp, value, 4); + if (temp == 0) { + std::memcpy(&temp, value + 4, 4); + return temp; + } + std::string ret(value, value + 8); + size_t pos = ret.find('\0'); + if (pos != std::string::npos) + ret.erase(pos); + return ret; + } + std::string GetString(std::FILE* file, u32 offset) { + auto v = Get(); + if (v.index() == 1) { + std::fseek(file, offset + std::get<1>(v), SEEK_SET); + char c; + std::string ret; + while (true) { + if (std::fread(&c, 1, 1, file) != 1) + throw "unexpected end"; + if (c == '\0') + break; + ret += c; + } + return ret; + } else { + return std::get<0>(v); + } + } +}; + +struct Header { + u16 magic; + u16 num_section; + u32 time; + u32 offset_symbol; + u32 num_symbol; + u16 optheader_size; + u16 flags; +}; +static_assert(sizeof(Header) == 20); + +struct SectionHeader { + NameOrIndex name; + u32 prog_addr; + u32 data_addr; + u32 size; + u32 offset_data; + u32 offset_rel; + u32 offset_line; + u16 num_rel; + u16 num_line; + u32 flags; +}; +static_assert(sizeof(SectionHeader) == 40); + +namespace SFlag { +constexpr u32 Exec = 0x0001; +constexpr u32 Unk2 = 0x0002; +constexpr u32 Prog = 0x0008; +constexpr u32 Data = 0x0010; +constexpr u32 RegionMask = Prog | Data; +constexpr u32 Moni = 0x0020; +constexpr u32 Un40 = 0x0040; +constexpr u32 Dupl = 0x0080; +constexpr u32 U200 = 0x0200; +} // namespace SFlag + +#pragma pack(push, 1) +struct Symbol { + NameOrIndex name; + u32 value; + u16 section_index; + u16 type; + u8 storage; + u8 num_aux; +}; +static_assert(sizeof(Symbol) == 18); + +inline const std::unordered_map<u32, const char*> storage_names{ + {0, "label"}, {1, "auto"}, {2, "external"}, {3, "static"}, + {4, "register"}, {8, "struct"}, {9, "arg"}, {10, "struct-tag"}, + {11, "union"}, {12, "union-tag"}, {13, "typedef"}, {15, "enum-tag"}, + {16, "enum"}, {18, "bitfield"}, {19, "auto-arg"}, {98, "start"}, + {99, "end"}, {100, "block"}, {101, "func"}, {102, "struct-size"}, + {103, "file"}, {107, "ar"}, {108, "ar"}, {109, "ar"}, + {110, "ar"}, {111, "ar"}, {112, "ar"}, {255, "physical-function-end"}, +}; + +/* +Storage: +0 labels? | section+address | type = 0 | no aux +1 auto variable | address, rel to sp? | variable type | variable aux +2 externals | section+address | variable type | variable aux +3 statics | section+address | variable type | variable aux +4 register variable | address, abs? | variable type | variable aux +8 structure member | address | variable type | variable aux +9 function argument | address, ? | variable type | variable aux +10 structure tag | debug/no address | type = 8 (structure) | aux = 1 +11 union member | address = 0? | type = 5 (long) or 8 (structure) | aux = 0 (long) or 1 (structure) +12 union tag | debug/no address | type = 9 (union) | aux = 1 +13 typedef | debug/no address | type = 5 (long) or 8 (structure) | aux = 0 (long) or 1 (structure) +15 enum tag | debug/no address | type = 10 (enum) | aux = 1 +16 enum member | abs value | type = 11 (enum member) | aux = 0 +18 bitfield | address | type = 5 (long) or 15 ulong | aux = 1 +19 auto arg | section+address | type = 17 | aux = 0 +98 function start | section + address | type = 0 | aux = 0 +99 function end | section + address | type = 0 | aux = 0 +100 .eb/.bb | section + address | type = 0 | aux = 1 +101 .ef/.bf | section + address | type = 0 | aux = 1 +102 struct end | address | type = 0 | aux = 1 +103 .file | debug value | type = 1 or 2 | aux = 1 +107 .ar | section + address | type = => ar0 value | aux = 0 +108 .ar | section + address | type = => ar1 value | aux = 0 +109 .ar | section + address | type = => arp0 value | aux = 0 +110 .ar | section + address | type = => arp1 value | aux = 0 +111 .ar | section + address | type = => arp2 value | aux = 0 +112 .ar | section + address | type = => arp3 value | aux = 0 +255 physical function end | section + address | type = 0 | aux = 0 +*/ + +struct Line { + u32 symbol_index_or_addr; + u16 line_number; +}; +static_assert(sizeof(Line) == 6); + +struct Relocation { + u32 addr; + u32 symbol; + u32 type; +}; +static_assert(sizeof(Relocation) == 12); // not 10 bytes! +#pragma pack(pop) + +class Coff { + +public: + Coff(std::FILE* in) { + fseek(in, 0, SEEK_SET); + Header header; + if (fread(&header, sizeof(header), 1, in) != 1) + throw "failed to read header"; + u32 string_offset = header.offset_symbol + header.num_symbol * sizeof(Symbol); + + sections.resize(header.num_section); + u32 expected = 0; + for (u32 i = 0; i < header.num_section; ++i) { + fseek(in, sizeof(header) + header.optheader_size + i * sizeof(SectionHeader), SEEK_SET); + SectionHeader sheader; + if (fread(&sheader, sizeof(sheader), 1, in) != 1) + throw "failed to read sheader"; + sections[i].name = sheader.name.GetString(in, string_offset); + sections[i].prog_addr = sheader.prog_addr; + sections[i].data_addr = sheader.data_addr; + if (expected == 0) + expected = sheader.offset_data; + if (expected != sheader.offset_data) { + throw ""; + } + sections[i].data.resize(sheader.size); + if ((sheader.flags & SFlag::Dupl) == 0) { + fseek(in, sheader.offset_data, SEEK_SET); + if (sheader.size != 0) + if (fread(sections[i].data.data(), sheader.size, 1, in) != 1) + throw "failed to read section"; + expected += sheader.size; + } + sections[i].num_rel = sheader.num_rel; + sections[i].flags = sheader.flags; + if ((sections[i].flags & SFlag::RegionMask) == 0) + throw "no region type"; + if ((sections[i].flags & SFlag::RegionMask) == SFlag::Prog) { + if (sections[i].data_addr != 0) + throw "prog section has data addr"; + } else if ((sections[i].flags & SFlag::RegionMask) == SFlag::Data) { + if (sections[i].prog_addr != 0) + throw "data section has prog addr"; + } + + fseek(in, sheader.offset_line, SEEK_SET); + u32 current_symbol = 0; + for (u16 j = 0; j < sheader.num_line; ++j) { + Line line; + if (fread(&line, sizeof(line), 1, in) != 1) + throw "failed to read line"; + if (line.line_number == 0) { + current_symbol = line.symbol_index_or_addr; + } else { + u32 addr = line.symbol_index_or_addr; + if (addr < sections[i].prog_addr || + addr >= sections[i].prog_addr + sections[i].data.size() / 2) { + if (addr != 0xFFFFFFFF && addr != 0xFFFFFFFE && + addr != 0xFFFFFFFD) // unknown magic + throw "line out of range"; + } + sections[i].line_numbers[addr].emplace_back( + Section::LineNumber{current_symbol, line.line_number}); + } + } + + fseek(in, sheader.offset_rel, SEEK_SET); + // printf("\n"); + for (u16 j = 0; j < sheader.num_rel; ++j) { + Relocation relocation; + if (fread(&relocation, sizeof(relocation), 1, in) != 1) + throw "failed to read relocation"; + // printf("%08X, %08X, %08X\n", relocation.addr, relocation.symbol, + // relocation.type); + sections[i].relocations[relocation.addr].push_back(relocation); + } + } + + for (u32 i = 0; i < header.num_symbol; ++i) { + Symbol symbol; + fseek(in, header.offset_symbol + i * sizeof(symbol), SEEK_SET); + if (fread(&symbol, sizeof(symbol), 1, in) != 1) + throw "failed to read symbol"; + i += symbol.num_aux; + printf("%s\n", symbol.name.GetString(in, string_offset).c_str()); + printf("value = %08X, section = %04X, type = %04X, storage = %02X, aux = %02X\n", + symbol.value, symbol.section_index, symbol.type, symbol.storage, symbol.num_aux); + SymbolEx symbol_ex; + symbol_ex.name = symbol.name.GetString(in, string_offset); + if (symbol.section_index > 0 && symbol.section_index < 0x7FFF) { + u16 section_index = symbol.section_index - 1; + symbol_ex.region = + sections[section_index].flags & SFlag::Prog ? SymbolEx::Prog : SymbolEx::Data; + symbol_ex.value = symbol.value + (sections[section_index].flags & SFlag::Prog + ? sections[section_index].prog_addr + : sections[section_index].data_addr); + + } else { + symbol_ex.region = SymbolEx::Absolute; + symbol_ex.value = symbol.value; + } + + symbol_ex.storage = symbol.storage; + symbol_ex.type = symbol.type; + + symbols.push_back(symbol_ex); + + if (symbols.back().region == SymbolEx::Prog || + symbols.back().region == SymbolEx::Data) { + symbols_lut[symbols.back().value].push_back(symbols.size() - 1); + } + + symbol_ex.region = SymbolEx::Aux; + for (u16 aux = 0; aux < symbol.num_aux; ++aux) { + symbols.push_back(symbol_ex); + } + } + + // Merge duplicated sections + std::sort(sections.begin(), sections.end(), [](const Section& left, const Section& right) { + u32 lm = left.flags & SFlag::Dupl; + u32 rm = right.flags & SFlag::Dupl; + if (lm == rm) { + return left.name < right.name; + } else { + return lm < rm; + } + }); + auto mid = sections.begin() + sections.size() / 2; + for (auto a = sections.begin(), b = mid; a != mid; ++a, ++b) { + if (a->name != b->name || a->prog_addr != b->prog_addr || + a->data_addr != b->data_addr || a->data.size() != b->data.size() || + (a->flags + SFlag::Dupl) != b->flags) + throw "mismatch"; + if (!b->line_numbers.empty()) + throw "dup has line numbers"; + if (b->num_rel != 0) + throw "dup has relocations"; + } + sections.resize(sections.size() / 2); + + // Break up multi-region section + auto both_begin = + std::partition(sections.begin(), sections.end(), [](const Section& section) { + return (section.flags & SFlag::RegionMask) != SFlag::RegionMask; + }); + std::vector<Section> copy(both_begin, sections.end()); + for (auto i = both_begin; i != sections.end(); ++i) { + i->flags &= ~SFlag::Prog; + i->prog_addr = 0; + } + for (auto& section : copy) { + section.flags &= ~SFlag::Data; + section.data_addr = 0; + } + sections.insert(sections.end(), std::make_move_iterator(copy.begin()), + std::make_move_iterator(copy.end())); + + // Sort according to address + std::sort(sections.begin(), sections.end(), [](const Section& left, const Section& right) { + if ((left.flags & SFlag::RegionMask) == SFlag::Prog) { + if ((right.flags & SFlag::RegionMask) == SFlag::Prog) { + if (left.prog_addr == right.prog_addr) { + return left.data.size() < right.data.size(); + } + return left.prog_addr < right.prog_addr; + } else { + return true; + } + } else { + if ((right.flags & SFlag::RegionMask) == SFlag::Prog) { + return false; + } else { + if (left.data_addr == right.data_addr) { + return left.data.size() < right.data.size(); + } + return left.data_addr < right.data_addr; + } + } + }); + } + + struct Section { + std::string name; + u32 prog_addr; + u32 data_addr; + std::vector<u8> data; + u16 num_rel; + u32 flags; + + struct LineNumber { + u32 symbol; + u16 line; + }; + std::unordered_map<u32 /*abs addr*/, std::vector<LineNumber>> line_numbers; + std::unordered_map<u32 /*rel addr*/, std::vector<Relocation>> relocations; + }; + std::vector<Section> sections; + + struct SymbolEx { + std::string name; + enum Region { + Aux, + Absolute, + Prog, + Data, + } region; + u32 value; + u16 type; + u8 storage; + }; + std::vector<SymbolEx> symbols; + std::unordered_map<u32 /*abs addr*/, std::vector<std::size_t /*index in symbols*/>> symbols_lut; +}; diff --git a/src/teakra/src/coff_reader/main.cpp b/src/teakra/src/coff_reader/main.cpp new file mode 100644 index 0000000..6b4316e --- /dev/null +++ b/src/teakra/src/coff_reader/main.cpp @@ -0,0 +1,132 @@ +#include <cstdio> +#include <string> +#include <teakra/disassembler.h> +#include "coff.h" + +int main(int argc, char** argv) { + if (argc < 2) { + printf("Please input a file\n"); + return -1; + } + std::FILE* in; + std::FILE* out; + in = std::fopen(argv[1], "rb"); + if (!in) { + printf("Failed to open input file\n"); + return -1; + } + out = std::fopen((argv[1] + std::string(".out")).c_str(), "wt"); + if (!out) { + printf("Failed to open output file\n"); + return -1; + } + + Coff coff(in); + + u32 prog_grow = 0, data_grow = 0; + for (const auto& section : coff.sections) { + u32 addr; + bool is_prog; + if ((section.flags & SFlag::RegionMask) == SFlag::Prog) { + if (section.prog_addr < prog_grow) { + throw "overlap"; + } + is_prog = true; + addr = section.prog_addr; + prog_grow = section.prog_addr + (u32)section.data.size() / 2; + } else { + if (section.data_addr < data_grow) { + throw "overlap"; + } + is_prog = false; + addr = section.data_addr; + data_grow = section.data_addr + (u32)section.data.size() / 2; + } + + fprintf(out, "\n===Section===\n"); + fprintf(out, "name: %s\n", section.name.c_str()); + fprintf(out, "addr: %s.%08X\n", is_prog ? "Prog" : "Data", addr); + fprintf(out, "flag: %08X\n\n", section.flags); + + std::vector<u16> data(section.data.size() / 2); + std::memcpy(data.data(), section.data.data(), section.data.size()); + + for (auto iter = data.begin(); iter != data.end(); ++iter) { + u32 current_rel_addr = (u32)(iter - data.begin()); + u32 current_addr = current_rel_addr + addr; + + if (coff.symbols_lut.count(current_addr)) { + const auto& symbol_list = coff.symbols_lut.at(current_addr); + auto begin = symbol_list.begin(); + while (true) { + auto symbol_index = + std::find_if(begin, symbol_list.end(), + [is_prog, current_addr, &coff](const auto symbol_index) { + const auto& symbol = coff.symbols[symbol_index]; + if (symbol.region == Coff::SymbolEx::Absolute || + symbol.region == Coff::SymbolEx::Aux) + throw "what"; + if (symbol.region == Coff::SymbolEx::Prog && !is_prog) + return false; + if (symbol.region == Coff::SymbolEx::Data && is_prog) + return false; + if (symbol.value != current_addr) + throw "what"; + return true; + }); + if (symbol_index != symbol_list.end()) { + const auto& symbol = coff.symbols[*symbol_index]; + fprintf(out, "$[%s]%s\n", storage_names.at(symbol.storage), + symbol.name.c_str()); + } else { + break; + } + begin = symbol_index + 1; + } + } + + if (section.line_numbers.count(current_addr)) { + for (const auto& line_number : section.line_numbers.at(current_addr)) { + const auto& symbol = coff.symbols[line_number.symbol]; + fprintf(out, "#%d + $[%s]%s\n", line_number.line, + storage_names.at(symbol.storage), symbol.name.c_str()); + } + } + + fprintf(out, ".%08X ", current_addr); + fprintf(out, "%04X", *iter); + + if (section.flags & SFlag::Exec) { + u16 opcode = *iter; + std::string dsm; + if (Teakra::Disassembler::NeedExpansion(opcode)) { + ++iter; + if (iter == data.end()) { + fprintf(out, "[broken expansion]\n"); + break; + } + u16 exp = *iter; + dsm = Teakra::Disassembler::Do(opcode, exp); + fprintf(out, " %04X ", exp); + } else { + dsm = Teakra::Disassembler::Do(opcode); + fprintf(out, " "); + } + fprintf(out, "%s ;", dsm.c_str()); + } + + if (section.relocations.count(current_rel_addr)) { + for (const auto& relocation : section.relocations.at(current_rel_addr)) { + const auto& symbol = coff.symbols[relocation.symbol]; + fprintf(out, "{@%08X + $[%s]%s}", relocation.type, + storage_names.at(symbol.storage), symbol.name.c_str()); + } + } + + fprintf(out, "\n"); + } + } + + std::fclose(in); + std::fclose(out); +} diff --git a/src/teakra/src/common_types.h b/src/teakra/src/common_types.h new file mode 100644 index 0000000..37a6c93 --- /dev/null +++ b/src/teakra/src/common_types.h @@ -0,0 +1,42 @@ +#pragma once + +#include <cstdint> + +using u8 = std::uint8_t; +using u16 = std::uint16_t; +using u32 = std::uint32_t; +using u64 = std::uint64_t; + +using s8 = std::int8_t; +using s16 = std::int16_t; +using s32 = std::int32_t; +using s64 = std::int64_t; + +template <typename T> +constexpr unsigned BitSize() { + return sizeof(T) * 8; // yeah I know I shouldn't use 8 here. +} + +template <typename T> +constexpr T SignExtend(const T value, unsigned bit_count) { + const T mask = static_cast<T>(1ULL << bit_count) - 1; + const bool sign_bit = ((value >> (bit_count - 1)) & 1) != 0; + if (sign_bit) { + return value | ~mask; + } + return value & mask; +} + +template <unsigned bit_count, typename T> +constexpr T SignExtend(const T value) { + static_assert(bit_count <= BitSize<T>(), "bit_count larger than bitsize of T"); + return SignExtend(value, bit_count); +} + +inline constexpr u16 BitReverse(u16 value) { + u16 result = 0; + for (u32 i = 0; i < 16; ++i) { + result |= ((value >> i) & 1) << (15 - i); + } + return result; +} diff --git a/src/teakra/src/core_timing.h b/src/teakra/src/core_timing.h new file mode 100644 index 0000000..80e8703 --- /dev/null +++ b/src/teakra/src/core_timing.h @@ -0,0 +1,46 @@ +#pragma once + +#include <algorithm> +#include <functional> +#include <limits> +#include <vector> +#include "common_types.h" + +namespace Teakra { + +class CoreTiming { +public: + class Callbacks { + public: + virtual ~Callbacks() = default; + virtual void Tick() = 0; + virtual u64 GetMaxSkip() const = 0; + virtual void Skip(u64) = 0; + static constexpr u64 Infinity = std::numeric_limits<u64>::max(); + }; + + void Tick() { + for (const auto& callbacks : registered_callbacks) { + callbacks->Tick(); + } + } + + u64 Skip(u64 maximum) { + u64 ticks = maximum; + for (const auto& callbacks : registered_callbacks) { + ticks = std::min(ticks, callbacks->GetMaxSkip()); + } + for (const auto& callbacks : registered_callbacks) { + callbacks->Skip(ticks); + } + return ticks; + } + + void RegisterCallbacks(Callbacks* callbacks) { + registered_callbacks.push_back(std::move(callbacks)); + } + +private: + std::vector<Callbacks*> registered_callbacks; +}; +} // namespace Teakra diff --git a/src/teakra/src/crash.h b/src/teakra/src/crash.h new file mode 100644 index 0000000..3d21aef --- /dev/null +++ b/src/teakra/src/crash.h @@ -0,0 +1,11 @@ +#pragma once +#include <cstdio> +#include <cstdlib> + +[[noreturn]] inline void Assert(const char* expression, const char* file, int line) { + std::fprintf(stderr, "Assertion '%s' failed, file '%s' line '%d'.", expression, file, line); + std::abort(); +} + +#define ASSERT(EXPRESSION) ((EXPRESSION) ? (void)0 : Assert(#EXPRESSION, __FILE__, __LINE__)) +#define UNREACHABLE() Assert("UNREACHABLE", __FILE__, __LINE__) diff --git a/src/teakra/src/cru.md b/src/teakra/src/cru.md new file mode 100644 index 0000000..12f287f --- /dev/null +++ b/src/teakra/src/cru.md @@ -0,0 +1,18 @@ +# CRU + +## MMIO Layout + +``` +(N = 0..14) ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0140+N*4| OFFSET_L | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0142+N*4| | | ? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x017C | OFFSET_L | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x017E | | | | ? | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +OFFSET_L, OFFSET_H: address of the program to replace +``` diff --git a/src/teakra/src/decoder.h b/src/teakra/src/decoder.h new file mode 100644 index 0000000..e8ecff3 --- /dev/null +++ b/src/teakra/src/decoder.h @@ -0,0 +1,722 @@ +#pragma once +#include <type_traits> +#include <vector> +#include "crash.h" +#include "matcher.h" +#include "operand.h" + +template <typename... OperandAtT> +struct OperandList { + template <typename OperandAtT0> + using prefix = OperandList<OperandAtT0, OperandAtT...>; +}; + +template <typename... OperandAtT> +struct FilterOperand; + +template <> +struct FilterOperand<> { + using result = OperandList<>; +}; + +template <bool keep, typename OperandAtT0, typename... OperandAtT> +struct FilterOperandHelper; + +template <typename OperandAtT0, typename... OperandAtT> +struct FilterOperandHelper<false, OperandAtT0, OperandAtT...> { + using result = typename FilterOperand<OperandAtT...>::result; +}; + +template <typename OperandAtT0, typename... OperandAtT> +struct FilterOperandHelper<true, OperandAtT0, OperandAtT...> { + using result = typename FilterOperand<OperandAtT...>::result ::template prefix<OperandAtT0>; +}; + +template <typename OperandAtT0, typename... OperandAtT> +struct FilterOperand<OperandAtT0, OperandAtT...> { + using result = + typename FilterOperandHelper<OperandAtT0::PassAsParameter, OperandAtT0, OperandAtT...>::result; +}; + +template <typename V, typename OperandListT> +struct VisitorFunctionWithoutFilter; + +template <typename V, typename... OperandAtT> +struct VisitorFunctionWithoutFilter<V, OperandList<OperandAtT...>> { + using type = typename V::instruction_return_type (V::*)(typename OperandAtT::FilterResult...); +}; + +template <typename V, typename... OperandAtT> +struct VisitorFunction { + using type = + typename VisitorFunctionWithoutFilter<V, typename FilterOperand<OperandAtT...>::result>::type; +}; + +template <typename V, u16 expected, typename... OperandAtT> +struct MatcherCreator { + template <typename OperandListT> + struct Proxy; + + using F = typename VisitorFunction<V, OperandAtT...>::type; + + template <typename... OperandAtTs> + struct Proxy<OperandList<OperandAtTs...>> { + F func; + auto operator()(V& visitor, [[maybe_unused]] u16 opcode, + [[maybe_unused]] u16 expansion) const { + return (visitor.*func)(OperandAtTs::Extract(opcode, expansion)...); + } + }; + + static Matcher<V> Create(const char* name, F func) { + // Operands shouldn't overlap each other, nor overlap with the expected ones + static_assert(NoOverlap<u16, expected, OperandAtT::Mask...>, "Error"); + + Proxy<typename FilterOperand<OperandAtT...>::result> proxy{func}; + + constexpr u16 mask = (~OperandAtT::Mask & ... & 0xFFFF); + constexpr bool expanded = (OperandAtT::NeedExpansion || ...); + return Matcher<V>(name, mask, expected, expanded, proxy); + } +}; + +template <typename... OperandAtConstT> +struct RejectorCreator { + static constexpr Rejector rejector{(OperandAtConstT::Mask | ...), (OperandAtConstT::Pad | ...)}; +}; + +// clang-format off + +template <typename V> +std::vector<Matcher<V>> GetDecodeTable() { + return { + +#define INST(name, ...) MatcherCreator<V, __VA_ARGS__>::Create(#name, &V::name) +#define EXCEPT(...) Except(RejectorCreator<__VA_ARGS__>::rejector) + + // <<< Misc >>> + INST(nop, 0x0000), + INST(norm, 0x94C0, At<Ax, 8>, At<Rn, 0>, At<StepZIDS, 3>), + INST(swap, 0x4980, At<SwapType, 0>), + INST(trap, 0x0020), + + // <<< ALM normal >>> + INST(alm, 0xA000, At<Alm, 9>, At<MemImm8, 0>, At<Ax, 8>), + INST(alm, 0x8080, At<Alm, 9>, At<Rn, 0>, At<StepZIDS, 3>, At<Ax, 8>), + INST(alm, 0x80A0, At<Alm, 9>, At<Register, 0>, At<Ax, 8>), + + // <<< ALM r6 >>> + INST(alm_r6, 0xD388, Const<Alm, 0>, At<Ax, 4>), + INST(alm_r6, 0xD389, Const<Alm, 1>, At<Ax, 4>), + INST(alm_r6, 0xD38A, Const<Alm, 2>, At<Ax, 4>), + INST(alm_r6, 0xD38B, Const<Alm, 3>, At<Ax, 4>), + INST(alm_r6, 0xD38C, Const<Alm, 4>, At<Ax, 4>), + INST(alm_r6, 0xD38D, Const<Alm, 5>, At<Ax, 4>), + INST(alm_r6, 0xD38E, Const<Alm, 6>, At<Ax, 4>), + INST(alm_r6, 0xD38F, Const<Alm, 7>, At<Ax, 4>), + INST(alm_r6, 0x9462, Const<Alm, 8>, At<Ax, 0>), + INST(alm_r6, 0x9464, Const<Alm, 9>, At<Ax, 0>), + INST(alm_r6, 0x9466, Const<Alm, 10>, At<Ax, 0>), + INST(alm_r6, 0x5E23, Const<Alm, 11>, At<Ax, 8>), + INST(alm_r6, 0x5E22, Const<Alm, 12>, At<Ax, 8>), + INST(alm_r6, 0x5F41, Const<Alm, 13>, Const<Ax, 0>), + INST(alm_r6, 0x9062, Const<Alm, 14>, At<Ax, 8>, Unused<0>), + INST(alm_r6, 0x8A63, Const<Alm, 15>, At<Ax, 3>), + + // <<< ALU normal >>> + INST(alu, 0xD4F8, At<Alu, 0>, At<MemImm16, 16>, At<Ax, 8>) + .EXCEPT(AtConst<Alu, 0, 4>).EXCEPT(AtConst<Alu, 0, 5>), + INST(alu, 0xD4D8, At<Alu, 0>, At<MemR7Imm16, 16>, At<Ax, 8>) + .EXCEPT(AtConst<Alu, 0, 4>).EXCEPT(AtConst<Alu, 0, 5>), + INST(alu, 0x80C0, At<Alu, 9>, At<Imm16, 16>, At<Ax, 8>) + .EXCEPT(AtConst<Alu, 9, 4>).EXCEPT(AtConst<Alu, 9, 5>), + INST(alu, 0xC000, At<Alu, 9>, At<Imm8, 0>, At<Ax, 8>) + .EXCEPT(AtConst<Alu, 9, 4>).EXCEPT(AtConst<Alu, 9, 5>), + INST(alu, 0x4000, At<Alu, 9>, At<MemR7Imm7s, 0>, At<Ax, 8>) + .EXCEPT(AtConst<Alu, 9, 4>).EXCEPT(AtConst<Alu, 9, 5>), + + // <<< OR Extra >>> + INST(or_, 0xD291, At<Ab, 10>, At<Ax, 6>, At<Ax, 5>), + INST(or_, 0xD4A4, At<Ax, 8>, At<Bx, 1>, At<Ax, 0>), + INST(or_, 0xD3C4, At<Bx, 10>, At<Bx, 1>, At<Ax, 0>), + + // <<< ALB normal >>> + INST(alb, 0xE100, At<Alb, 9>, At<Imm16, 16>, At<MemImm8, 0>), + INST(alb, 0x80E0, At<Alb, 9>, At<Imm16, 16>, At<Rn, 0>, At<StepZIDS, 3>), + INST(alb, 0x81E0, At<Alb, 9>, At<Imm16, 16>, At<Register, 0>), + INST(alb_r6, 0x47B8, At<Alb, 0>, At<Imm16, 16>), + + // <<< ALB SttMod >>> + INST(alb, 0x43C8, Const<Alb, 0>, At<Imm16, 16>, At<SttMod, 0>), + INST(alb, 0x4388, Const<Alb, 1>, At<Imm16, 16>, At<SttMod, 0>), + INST(alb, 0x0038, Const<Alb, 2>, At<Imm16, 16>, At<SttMod, 0>), + //INST(alb, 0x????, Const<Alb, 3>, At<Imm,16, 16>, At<SttMod, 0>), + INST(alb, 0x9470, Const<Alb, 4>, At<Imm16, 16>, At<SttMod, 0>), + INST(alb, 0x9478, Const<Alb, 5>, At<Imm16, 16>, At<SttMod, 0>), + //INST(alb, 0x????, Const<Alb, 6>, At<Imm,16, 16>, At<SttMod, 0>), + //INST(alb, 0x????, Const<Alb, 7>, At<Imm,16, 16>, At<SttMod, 0>), + + // <<< Add extra >>> + INST(add, 0xD2DA, At<Ab, 10>, At<Bx, 0>), + INST(add, 0x5DF0, At<Bx, 1>, At<Ax, 0>), + INST(add_p1, 0xD782, At<Ax, 0>), + INST(add, 0x5DF8, At<Px, 1>, At<Bx, 0>), + + // <<< Sub extra >>> + INST(sub, 0x8A61, At<Ab, 3>, At<Bx, 8>), + INST(sub, 0x8861, At<Bx, 4>, At<Ax, 3>), + INST(sub_p1, 0xD4B9, At<Ax, 8>), + INST(sub, 0x8FD0, At<Px, 1>, At<Bx, 0>), + + /// <<< addsub p0 p1 >>> + INST(app, 0x5DC0, At<Ab, 2>, BZr, Add, PP, Add, PP), + INST(app, 0x5DC1, At<Ab, 2>, BZr, Add, PP, Add, PA), + INST(app, 0x4590, At<Ab, 2>, BAc, Add, PP, Add, PP), + INST(app, 0x4592, At<Ab, 2>, BAc, Add, PP, Add, PA), + INST(app, 0x4593, At<Ab, 2>, BAc, Add, PA, Add, PA), + INST(app, 0x5DC2, At<Ab, 2>, BZr, Add, PP, Sub, PP), + INST(app, 0x5DC3, At<Ab, 2>, BZr, Add, PP, Sub, PA), + INST(app, 0x80C6, At<Ab, 10>, BAc, Sub, PP, Sub, PP), + INST(app, 0x82C6, At<Ab, 10>, BAc, Sub, PP, Sub, PA), + INST(app, 0x83C6, At<Ab, 10>, BAc, Sub, PA, Sub, PA), + INST(app, 0x906C, At<Ab, 0>, BAc, Add, PP, Sub, PP), + INST(app, 0x49C2, At<Ab, 4>, BAc, Sub, PP, Add, PP), + INST(app, 0x916C, At<Ab, 0>, BAc, Add, PP, Sub, PA), + INST(app, 0x49C3, At<Ab, 4>, BAc, Sub, PP, Add, PA), + + /// <<< add||sub >>> + INST(add_add, 0x6F80, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<Ab, 3>), + INST(add_sub, 0x6FA0, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<Ab, 3>), + INST(sub_add, 0x6FC0, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<Ab, 3>), + INST(sub_sub, 0x6FE0, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<Ab, 3>), + + /// <<< add||sub sv >>> + INST(add_sub_sv, 0x5DB0, At<ArRn1, 1>, At<ArStep1, 0>, At<Ab, 2>), + INST(sub_add_sv, 0x5DE0, At<ArRn1, 1>, At<ArStep1, 0>, At<Ab, 2>), + + /// <<< add||sub||mov sv >>> + INST(sub_add_i_mov_j_sv, 0x8064, At<ArpRn1, 8>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<Ab, 3>), + INST(sub_add_j_mov_i_sv, 0x5D80, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<Ab, 3>), + INST(add_sub_i_mov_j, 0x9070, At<ArpRn1, 8>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<Ab, 2>), + INST(add_sub_j_mov_i, 0x5E30, At<ArpRn1, 8>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<Ab, 2>), + + // <<< Mul >>> + INST(mul, 0x8000, At<Mul3, 8>, At<Rn, 0>, At<StepZIDS, 3>, At<Imm16, 16>, At<Ax, 11>), + INST(mul_y0, 0x8020, At<Mul3, 8>, At<Rn, 0>, At<StepZIDS, 3>, At<Ax, 11>), + INST(mul_y0, 0x8040, At<Mul3, 8>, At<Register, 0>, At<Ax, 11>), + INST(mul, 0xD000, At<Mul3, 8>, At<R45, 2>, At<StepZIDS, 5>, At<R0123, 0>, At<StepZIDS, 3>, At<Ax, 11>), + INST(mul_y0_r6, 0x5EA0, At<Mul3, 1>, At<Ax, 0>), + INST(mul_y0, 0xE000, At<Mul2, 9>, At<MemImm8, 0>, At<Ax, 11>), + + // <<< Mul Extra >>> + INST(mpyi, 0x0800, At<Imm8s, 0>), + INST(msu, 0xD080, At<R45, 2>, At<StepZIDS, 5>, At<R0123, 0>, At<StepZIDS, 3>, At<Ax, 8>), + INST(msu, 0x90C0, At<Rn, 0>, At<StepZIDS, 3>, At<Imm16, 16>, At<Ax, 8>), + INST(msusu, 0x8264, At<ArRn2, 3>, At<ArStep2, 0>, At<Ax, 8>), + INST(mac_x1to0, 0x4D84, At<Ax, 1>, Unused<0>), + INST(mac1, 0x5E28, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<Ax, 8>), + + // <<< MODA >>> + INST(moda4, 0x6700, At<Moda4, 4>, At<Ax, 12>, At<Cond, 0>) + .EXCEPT(AtConst<Moda4, 4, 7>), + INST(moda3, 0x6F00, At<Moda3, 4>, At<Bx, 12>, At<Cond, 0>), + INST(pacr1, 0xD7C2, At<Ax, 0>), + INST(clr, 0x8ED0, At<Ab, 2>, At<Ab, 0>), + INST(clrr, 0x8DD0, At<Ab, 2>, At<Ab, 0>), + + // <<< Block repeat >>> + INST(bkrep, 0x5C00, At<Imm8, 0>, At<Address16, 16>), + INST(bkrep, 0x5D00, At<Register, 0>, At<Address18_16, 16>, At<Address18_2, 5>), + INST(bkrep_r6, 0x8FDC, At<Address18_16, 16>, At<Address18_2, 0>), + INST(bkreprst, 0xDA9C, At<ArRn2, 0>), + INST(bkreprst_memsp, 0x5F48, Unused<0>, Unused<1>), + INST(bkrepsto, 0xDADC, At<ArRn2, 0>, Unused<10>), + INST(bkrepsto_memsp, 0x9468, Unused<0>, Unused<1>, Unused<2>), + + // <<< Bank >>> + INST(banke, 0x4B80, At<BankFlags, 0>), + INST(bankr, 0x8CDF), + INST(bankr, 0x8CDC, At<Ar, 0>), + INST(bankr, 0x8CD0, At<Ar, 2>, At<Arp, 0>), + INST(bankr, 0x8CD8, At<Arp, 0>), + + // <<< Bitrev >>> + INST(bitrev, 0x5EB8, At<Rn, 0>), + INST(bitrev_dbrv, 0xD7E8, At<Rn, 0>), + INST(bitrev_ebrv, 0xD7E0, At<Rn, 0>), + + // <<< Branching >>> + INST(br, 0x4180, At<Address18_16, 16>, At<Address18_2, 4>, At<Cond, 0>), + INST(brr, 0x5000, At<RelAddr7, 4>, At<Cond, 0>), + + // <<< Break >>> + INST(break_, 0xD3C0), + + // <<< Call >>> + INST(call, 0x41C0, At<Address18_16, 16>, At<Address18_2, 4>, At<Cond, 0>), + INST(calla, 0xD480, At<Axl, 8>), + INST(calla, 0xD381, At<Ax, 4>), + INST(callr, 0x1000, At<RelAddr7, 4>, At<Cond, 0>), + + // <<< Context >>> + INST(cntx_s, 0xD380), + INST(cntx_r, 0xD390), + + // <<< Return >>> + INST(ret, 0x4580, At<Cond, 0>), + INST(retd, 0xD780), + INST(reti, 0x45C0, At<Cond, 0>), + INST(retic, 0x45D0, At<Cond, 0>), + INST(retid, 0xD7C0), + INST(retidc, 0xD3C3), + INST(rets, 0x0900, At<Imm8, 0>), + + // <<< Load >>> + INST(load_ps, 0x4D80, At<Imm2, 0>), + INST(load_stepi, 0xDB80, At<Imm7s, 0>), + INST(load_stepj, 0xDF80, At<Imm7s, 0>), + INST(load_page, 0x0400, At<Imm8, 0>), + INST(load_modi, 0x0200, At<Imm9, 0>), + INST(load_modj, 0x0A00, At<Imm9, 0>), + INST(load_movpd, 0xD7D8, At<Imm2, 1>, Unused<0>), + INST(load_ps01, 0x0010, At<Imm4, 0>), + + // <<< Push >>> + INST(push, 0x5F40, At<Imm16, 16>), + INST(push, 0x5E40, At<Register, 0>), + INST(push, 0xD7C8, At<Abe, 1>, Unused<0>), + INST(push, 0xD3D0, At<ArArpSttMod, 0>), + INST(push_prpage, 0xD7FC, Unused<0>, Unused<1>), + INST(push, 0xD78C, At<Px, 1>, Unused<0>), + INST(push_r6, 0xD4D7, Unused<5>), + INST(push_repc, 0xD7F8, Unused<0>, Unused<1>), + INST(push_x0, 0xD4D4, Unused<5>), + INST(push_x1, 0xD4D5, Unused<5>), + INST(push_y1, 0xD4D6, Unused<5>), + INST(pusha, 0x4384, At<Ax, 6>, Unused<0>, Unused<1>), + INST(pusha, 0xD788, At<Bx, 1>, Unused<0>), + + // <<< Pop >>> + INST(pop, 0x5E60, At<Register, 0>), + INST(pop, 0x47B4, At<Abe, 0>), + INST(pop, 0x80C7, At<ArArpSttMod, 8>), + INST(pop, 0x0006, At<Bx, 5>, Unused<0>), + INST(pop_prpage, 0xD7F4, Unused<0>, Unused<1>), + INST(pop, 0xD496, At<Px, 0>), + INST(pop_r6, 0x0024, Unused<0>), + INST(pop_repc, 0xD7F0, Unused<0>, Unused<1>), + INST(pop_x0, 0xD494), + INST(pop_x1, 0xD495), + INST(pop_y1, 0x0004, Unused<0>), + INST(popa, 0x47B0, At<Ab, 0>), + + // <<< Repeat >>> + INST(rep, 0x0C00, At<Imm8, 0>), + INST(rep, 0x0D00, At<Register, 0>), + INST(rep_r6, 0x0002, Unused<0>), + + // <<< Shift >>> + INST(shfc, 0xD280, At<Ab, 10>, At<Ab, 5>, At<Cond, 0>), + INST(shfi, 0x9240, At<Ab, 10>, At<Ab, 7>, At<Imm6s, 0>), + + // <<< TSTB >>> + INST(tst4b, 0x80C1, At<ArRn2, 10>, At<ArStep2, 8>), + INST(tst4b, 0x4780, At<ArRn2, 2>, At<ArStep2, 0>, At<Ax, 4>), + INST(tstb, 0xF000, At<MemImm8, 0>, At<Imm4, 8>), + INST(tstb, 0x9020, At<Rn, 0>, At<StepZIDS, 3>, At<Imm4, 8>), + INST(tstb, 0x9000, At<Register, 0>, At<Imm4, 8>) + .EXCEPT(AtConst<Register, 0, 24>), // override by tstb_r6 + INST(tstb_r6, 0x9018, At<Imm4, 8>), + INST(tstb, 0x0028, At<SttMod, 0>, At<Imm16, 16>), // unused12@20 + + // <<< AND Extra >>> + INST(and_, 0x6770, At<Ab, 2>, At<Ab, 0>, At<Ax, 12>), + + // <<< Interrupt >>> + INST(dint, 0x43C0), + INST(eint, 0x4380), + + // <<< EXP >>> + INST(exp, 0x9460, At<Bx, 0>), + INST(exp, 0x9060, At<Bx, 0>, At<Ax, 8>), + INST(exp, 0x9C40, At<Rn, 0>, At<StepZIDS, 3>), + INST(exp, 0x9840, At<Rn, 0>, At<StepZIDS, 3>, At<Ax, 8>), + INST(exp, 0x9440, At<Register, 0>), + INST(exp, 0x9040, At<Register, 0>, At<Ax, 8>), + INST(exp_r6, 0xD7C1), + INST(exp_r6, 0xD382, At<Ax, 4>), + + // <<< MODR >>> + INST(modr, 0x0080, At<Rn, 0>, At<StepZIDS, 3>), + INST(modr_dmod, 0x00A0, At<Rn, 0>, At<StepZIDS, 3>), + INST(modr_i2, 0x4990, At<Rn, 0>), + INST(modr_i2_dmod, 0x4998, At<Rn, 0>), + INST(modr_d2, 0x5DA0, At<Rn, 0>), + INST(modr_d2_dmod, 0x5DA8, At<Rn, 0>), + INST(modr_eemod, 0xD294, At<ArpRn2, 10>, At<ArpStep2, 0>, At<ArpStep2, 5>), + INST(modr_edmod, 0x0D80, At<ArpRn2, 5>, At<ArpStep2, 1>, At<ArpStep2, 3>), + INST(modr_demod, 0x8464, At<ArpRn2, 8>, At<ArpStep2, 0>, At<ArpStep2, 3>), + INST(modr_ddmod, 0x0D81, At<ArpRn2, 5>, At<ArpStep2, 1>, At<ArpStep2, 3>), + + // <<< MOV >>> + INST(mov, 0xD290, At<Ab, 10>, At<Ab, 5>), + INST(mov_dvm, 0xD298, At<Abl, 10>), + INST(mov_x0, 0xD2D8, At<Abl, 10>), + INST(mov_x1, 0xD394, At<Abl, 0>), + INST(mov_y1, 0xD384, At<Abl, 0>), + + INST(mov, 0x3000, At<Ablh, 9>, At<MemImm8, 0>), + INST(mov, 0xD4BC, At<Axl, 8>, At<MemImm16, 16>), + INST(mov, 0xD49C, At<Axl, 8>, At<MemR7Imm16, 16>), + INST(mov, 0xDC80, At<Axl, 8>, At<MemR7Imm7s, 0>), + + INST(mov, 0xD4B8, At<MemImm16, 16>, At<Ax, 8>), + INST(mov, 0x6100, At<MemImm8, 0>, At<Ab, 11>), + INST(mov, 0x6200, At<MemImm8, 0>, At<Ablh, 10>), + INST(mov_eu, 0x6500, At<MemImm8, 0>, At<Axh, 12>), + INST(mov, 0x6000, At<MemImm8, 0>, At<RnOld, 10>), + INST(mov_sv, 0x6D00, At<MemImm8, 0>), + + INST(mov_dvm_to, 0xD491, At<Ab, 5>), + INST(mov_icr_to, 0xD492, At<Ab, 5>), + + INST(mov, 0x5E20, At<Imm16, 16>, At<Bx, 8>), + INST(mov, 0x5E00, At<Imm16, 16>, At<Register, 0>), + INST(mov_icr, 0x4F80, At<Imm5, 0>), + INST(mov, 0x2500, At<Imm8s, 0>, At<Axh, 12>), + INST(mov_ext0, 0x2900, At<Imm8s, 0>), + INST(mov_ext1, 0x2D00, At<Imm8s, 0>), + INST(mov_ext2, 0x3900, At<Imm8s, 0>), + INST(mov_ext3, 0x3D00, At<Imm8s, 0>), + INST(mov, 0x2300, At<Imm8s, 0>, At<RnOld, 10>), + INST(mov_sv, 0x0500, At<Imm8s, 0>), + INST(mov, 0x2100, At<Imm8, 0>, At<Axl, 12>), + + INST(mov, 0xD498, At<MemR7Imm16, 16>, At<Ax, 8>), + INST(mov, 0xD880, At<MemR7Imm7s, 0>, At<Ax, 8>), + INST(mov, 0x98C0, At<Rn, 0>, At<StepZIDS, 3>, At<Bx, 8>), + INST(mov, 0x1C00, At<Rn, 0>, At<StepZIDS, 3>, At<Register, 5>), + + INST(mov_memsp_to, 0x47E0, At<Register, 0>), + INST(mov_mixp_to, 0x47C0, At<Register, 0>), + INST(mov, 0x2000, At<RnOld, 9>, At<MemImm8, 0>), + INST(mov_icr, 0x4FC0, At<Register, 0>), + INST(mov_mixp, 0x5E80, At<Register, 0>), + INST(mov, 0x1800, At<Register, 5>, At<Rn, 0>, At<StepZIDS, 3>) + .EXCEPT(AtConst<Register, 5, 24>).EXCEPT(AtConst<Register, 5, 25>), // override by mov_r6(_to) + INST(mov, 0x5EC0, At<Register, 0>, At<Bx, 5>), + INST(mov, 0x5800, At<Register, 0>, At<Register, 5>) + .EXCEPT(AtConst<Register, 0, 24>).EXCEPT(AtConst<Register, 0, 25>), // override by mma_mov + INST(mov_repc_to, 0xD490, At<Ab, 5>), + INST(mov_sv_to, 0x7D00, At<MemImm8, 0>), + INST(mov_x0_to, 0xD493, At<Ab, 5>), + INST(mov_x1_to, 0x49C1, At<Ab, 4>), + INST(mov_y1_to, 0xD299, At<Ab, 10>), + + // <<< MOV load >>> + INST(mov, 0x0008, At<Imm16, 16>, At<ArArp, 0>), + INST(mov_r6, 0x0023, At<Imm16, 16>), + INST(mov_repc, 0x0001, At<Imm16, 16>), + INST(mov_stepi0, 0x8971, At<Imm16, 16>), + INST(mov_stepj0, 0x8979, At<Imm16, 16>), + INST(mov, 0x0030, At<Imm16, 16>, At<SttMod, 0>), + INST(mov_prpage, 0x5DD0, At<Imm4, 0>), + + // <<< <<< MOV p/d >>> + INST(movd, 0x5F80, At<R0123, 0>, At<StepZIDS, 3>, At<R45, 2>, At<StepZIDS, 5>), + INST(movp, 0x0040, At<Axl, 5>, At<Register, 0>), + INST(movp, 0x0D40, At<Ax, 5>, At<Register, 0>), + INST(movp, 0x0600, At<Rn, 0>, At<StepZIDS, 3>, At<R0123, 5>, At<StepZIDS, 7>), + INST(movpdw, 0xD499, At<Ax, 8>), + + // <<< MOV 2 >>> + INST(mov_a0h_stepi0, 0xD49B), + INST(mov_a0h_stepj0, 0xD59B), + INST(mov_stepi0_a0h, 0xD482), + INST(mov_stepj0_a0h, 0xD582), + + INST(mov_prpage, 0x9164, At<Abl, 0>), + INST(mov_repc, 0x9064, At<Abl, 0>), + INST(mov, 0x9540, At<Abl, 3>, At<ArArp, 0>), + INST(mov, 0x9C60, At<Abl, 3>, At<SttMod, 0>), + + INST(mov_prpage_to, 0x5EB0, At<Abl, 0>), + INST(mov_repc_to, 0xD2D9, At<Abl, 10>), + INST(mov, 0x9560, At<ArArp, 0>, At<Abl, 3>), + INST(mov, 0xD2F8, At<SttMod, 0>, At<Abl, 10>), + + INST(mov_repc_to, 0xD7D0, At<ArRn1, 1>, At<ArStep1, 0>), + INST(mov, 0xD488, At<ArArp, 0>, At<ArRn1, 8>, At<ArStep1, 5>), + INST(mov, 0x49A0, At<SttMod, 0>, At<ArRn1, 4>, At<ArStep1, 3>), + + INST(mov_repc, 0xD7D4, At<ArRn1, 1>, At<ArStep1, 0>), + INST(mov, 0x8062, At<ArRn1, 4>, At<ArStep1, 3>, At<ArArp, 8>), + INST(mov, 0x8063, At<ArRn1, 4>, At<ArStep1, 3>, At<SttMod, 8>), + + INST(mov_repc_to, 0xD3C8, At<MemR7Imm16, 16>, Unused<0>, Unused<1>, Unused<2>), + INST(mov, 0x5F50, At<ArArpSttMod, 0>, At<MemR7Imm16, 16>), + + INST(mov_repc, 0xD2DC, At<MemR7Imm16, 16>, Unused<0>, Unused<1>, Unused<10>), + INST(mov, 0x4D90, At<MemR7Imm16, 16>, At<ArArpSttMod, 0>), + + INST(mov_pc, 0x886B, At<Ax, 8>), + INST(mov_pc, 0x8863, At<Bx, 8>), + + INST(mov_mixp_to, 0x8A73, At<Bx, 3>), + INST(mov_mixp_r6, 0x4381), + INST(mov_p0h_to, 0x4382, At<Bx, 0>), + INST(mov_p0h_r6, 0xD3C2), + INST(mov_p0h_to, 0x4B60, At<Register, 0>), + INST(mov_p0, 0x8FD4, At<Ab, 0>), + INST(mov_p1_to, 0x8FD8, At<Ab, 0>), + + INST(mov2, 0x88D0, At<Px, 1>, At<ArRn2, 8>, At<ArStep2, 2>), + INST(mov2s, 0x88D1, At<Px, 1>, At<ArRn2, 8>, At<ArStep2, 2>), + INST(mov2, 0xD292, At<ArRn2, 10>, At<ArStep2, 5>, At<Px, 0>), + INST(mova, 0x4DC0, At<Ab, 4>, At<ArRn2, 2>, At<ArStep2, 0>), + INST(mova, 0x4BC0, At<ArRn2, 2>, At<ArStep2, 0>, At<Ab, 4>), + + INST(mov_r6_to, 0xD481, At<Bx, 8>), + INST(mov_r6_mixp, 0x43C1), + INST(mov_r6_to, 0x5F00, At<Register, 0>), + INST(mov_r6, 0x5F60, At<Register, 0>), + INST(mov_memsp_r6, 0xD29C, Unused<0>, Unused<1>, Unused<10>), + INST(mov_r6_to, 0x1B00, At<Rn, 0>, At<StepZIDS, 3>), + INST(mov_r6, 0x1B20, At<Rn, 0>, At<StepZIDS, 3>), + + INST(movs, 0x6300, At<MemImm8, 0>, At<Ab, 11>), + INST(movs, 0x0180, At<Rn, 0>, At<StepZIDS, 3>, At<Ab, 5>), + INST(movs, 0x0100, At<Register, 0>, At<Ab, 5>), + INST(movs_r6_to, 0x5F42, At<Ax, 0>), + INST(movsi, 0x4080, At<RnOld, 9>, At<Ab, 5>, At<Imm5s, 0>), + + // <<< MOV MOV >>> + INST(mov2_axh_m_y0_m, 0x4390, At<Axh, 6>, At<ArRn2, 2>, At<ArStep2, 0>), + INST(mov2_ax_mij, 0x43A0, At<Ab, 3>, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>), + INST(mov2_ax_mji, 0x43E0, At<Ab, 3>, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>), + INST(mov2_mij_ax, 0x80C4, At<ArpRn1, 9>, At<ArpStep1, 0>, At<ArpStep1, 8>, At<Ab, 10>), + INST(mov2_mji_ax, 0xD4C0, At<ArpRn1, 5>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<Ab, 2>), + INST(mov2_abh_m, 0x9D40, At<Abh, 4>, At<Abh, 2>, At<ArRn1, 1>, At<ArStep1, 0>), + INST(exchange_iaj, 0x8C60, At<Axh, 4>, At<ArpRn2, 8>, At<ArpStep2, 0>, At<ArpStep2, 2>), + INST(exchange_riaj, 0x7F80, At<Axh, 6>, At<ArpRn2, 4>, At<ArpStep2, 0>, At<ArpStep2, 2>), + INST(exchange_jai, 0x4900, At<Axh, 6>, At<ArpRn2, 4>, At<ArpStep2, 0>, At<ArpStep2, 2>), + INST(exchange_rjai, 0x4800, At<Axh, 6>, At<ArpRn2, 4>, At<ArpStep2, 0>, At<ArpStep2, 2>), + + // <<< MOVR >>> + INST(movr, 0x8864, At<ArRn2, 3>, At<ArStep2, 0>, At<Abh, 8>), + INST(movr, 0x9CE0, At<Rn, 0>, At<StepZIDS, 3>, At<Ax, 8>), + INST(movr, 0x9CC0, At<Register, 0>, At<Ax, 8>), + INST(movr, 0x5DF4, At<Bx, 1>, At<Ax, 0>), + INST(movr_r6_to, 0x8961, At<Ax, 3>), + + // <<< LIM >>> + INST(lim, 0x49C0, At<Ax, 5>, At<Ax, 4>), + + // <<< Viterbi >>> + INST(vtrclr0, 0x5F45), + INST(vtrclr1, 0x5F46), + INST(vtrclr, 0x5F47), + INST(vtrmov0, 0xD29A, At<Axl, 0>), + INST(vtrmov1, 0xD69A, At<Axl, 0>), + INST(vtrmov, 0xD383, At<Axl, 4>), + INST(vtrshr, 0xD781), + + // <<< CLRP >>> + INST(clrp0, 0x5DFE), + INST(clrp1, 0x5DFD), + INST(clrp, 0x5DFF), + + // <<< min/max >>> + INST(max_ge, 0x8460, At<Ax, 8>, At<StepZIDS, 3>), + INST(max_gt, 0x8660, At<Ax, 8>, At<StepZIDS, 3>), + INST(min_le, 0x8860, At<Ax, 8>, At<StepZIDS, 3>), + INST(min_lt, 0x8A60, At<Ax, 8>, At<StepZIDS, 3>), + INST(max_ge_r0, 0x8060, At<Ax, 8>, At<StepZIDS, 3>), + INST(max_gt_r0, 0x8260, At<Ax, 8>, At<StepZIDS, 3>), + INST(min_le_r0, 0x47A0, At<Ax, 3>, At<StepZIDS, 0>), + INST(min_lt_r0, 0x47A4, At<Ax, 3>, At<StepZIDS, 0>), + + // <<< Division Step >>> + INST(divs, 0x0E00, At<MemImm8, 0>, At<Ax, 8>), + + // <<< Sqr >>> + INST(sqr_sqr_add3, 0xD790, At<Ab, 2>, At<Ab, 0>), + INST(sqr_sqr_add3, 0x4B00, At<ArRn2, 4>, At<ArStep2, 2>, At<Ab, 0>), + INST(sqr_mpysu_add3a, 0x49C4, At<Ab, 4>, At<Ab, 0>), + + // <<< CMP Extra >>> + INST(cmp, 0x4D8C, At<Ax, 1>, At<Bx, 0>), + INST(cmp_b0_b1, 0xD483), + INST(cmp_b1_b0, 0xD583), + INST(cmp, 0xDA9A, At<Bx, 10>, At<Ax, 0>), + INST(cmp_p1_to, 0x8B63, At<Ax, 4>), + + // <<< min||max||vtrshr >>> + INST(max2_vtr, 0x5E21, At<Ax, 8>), + INST(min2_vtr, 0x43C2, At<Ax, 0>), + INST(max2_vtr, 0xD784, At<Ax, 1>, At<Bx, 0>), + INST(min2_vtr, 0xD4BA, At<Ax, 8>, At<Bx, 0>), + INST(max2_vtr_movl, 0x4A40, At<Ax, 3>, At<Bx, 4>, At<ArRn1, 1>, At<ArStep1, 0>), + INST(max2_vtr_movh, 0x4A44, At<Ax, 3>, At<Bx, 4>, At<ArRn1, 1>, At<ArStep1, 0>), + INST(max2_vtr_movl, 0x4A60, At<Bx, 4>, At<Ax, 3>, At<ArRn1, 1>, At<ArStep1, 0>), + INST(max2_vtr_movh, 0x4A64, At<Bx, 4>, At<Ax, 3>, At<ArRn1, 1>, At<ArStep1, 0>), + INST(min2_vtr_movl, 0x4A00, At<Ax, 3>, At<Bx, 4>, At<ArRn1, 1>, At<ArStep1, 0>), + INST(min2_vtr_movh, 0x4A04, At<Ax, 3>, At<Bx, 4>, At<ArRn1, 1>, At<ArStep1, 0>), + INST(min2_vtr_movl, 0x4A20, At<Bx, 4>, At<Ax, 3>, At<ArRn1, 1>, At<ArStep1, 0>), + INST(min2_vtr_movh, 0x4A24, At<Bx, 4>, At<Ax, 3>, At<ArRn1, 1>, At<ArStep1, 0>), + INST(max2_vtr_movij, 0xD590, At<Ax, 6>, At<Bx, 5>, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>), + INST(max2_vtr_movji, 0x45A0, At<Ax, 4>, At<Bx, 3>, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>), + INST(min2_vtr_movij, 0xD2B8, At<Ax, 11>, At<Bx, 10>, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>), + INST(min2_vtr_movji, 0x45E0, At<Ax, 4>, At<Bx, 3>, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>), + + // <<< MOV ADDSUB >>> + INST(mov_sv_app, 0x4B40, At<ArRn1, 3>, At<ArStep1, 2>, At<Bx, 0>, BSv, Sub, PP, Add, PP), + INST(mov_sv_app, 0x9960, At<ArRn1, 4>, At<ArStep1Alt, 3>, At<Bx, 2>, BSv, Sub, PP, Add, PP), + INST(mov_sv_app, 0x4B42, At<ArRn1, 3>, At<ArStep1, 2>, At<Bx, 0>, BSr, Sub, PP, Add, PP), + INST(mov_sv_app, 0x99E0, At<ArRn1, 4>, At<ArStep1Alt, 3>, At<Bx, 2>, BSr, Sub, PP, Add, PP), + INST(mov_sv_app, 0x5F4C, At<ArRn1, 1>, At<ArStep1, 0>, Const<Bx, 0>, BSv, Sub, PP, Sub, PP), + INST(mov_sv_app, 0x8873, At<ArRn1, 8>, At<ArStep1, 3>, Const<Bx, 1>, BSv, Sub, PP, Sub, PP), + INST(mov_sv_app, 0x9860, At<ArRn1, 4>, At<ArStep1Alt, 3>, At<Bx, 2>, BSv, Sub, PP, Sub, PP), + INST(mov_sv_app, 0xDE9C, At<ArRn1, 1>, At<ArStep1, 0>, Const<Bx, 0>, BSr, Sub, PP, Sub, PP), + INST(mov_sv_app, 0xD4B4, At<ArRn1, 1>, At<ArStep1, 0>, Const<Bx, 1>, BSr, Sub, PP, Sub, PP), + INST(mov_sv_app, 0x98E0, At<ArRn1, 4>, At<ArStep1Alt, 3>, At<Bx, 2>, BSr, Sub, PP, Sub, PP), + + // <<< CBS >>> + INST(cbs, 0x9068, At<Axh, 0>, At<CbsCond, 8>), + INST(cbs, 0xD49E, At<Axh, 8>, At<Bxh, 5>, At<CbsCond, 0>), + INST(cbs, 0xD5C0, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, At<CbsCond, 3>), + + // [[[XXX_xy_XXX_xy_XXX]]] + INST(mma, 0x4D88, AtNamed<Ax, 1>, SX, SY, SX, SY, BZr, Add, PP, Sub, PP), + INST(mma, 0xD49D, AtNamed<Bx, 5>, SX, SY, SX, SY, BZr, Add, PP, Sub, PP), + INST(mma, 0x5E24, AtNamed<Ab, 0>, SX, SY, SX, SY, BZr, Add, PP, Add, PP), + INST(mma, 0x8061, AtNamed<Ab, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PP), + INST(mma, 0x8071, AtNamed<Ab, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PA), + INST(mma, 0x8461, AtNamed<Ab, 8>, SX, SY, SX, SY, BAc, Sub, PP, Sub, PP), + INST(mma, 0x8471, AtNamed<Ab, 8>, SX, SY, SX, SY, BAc, Sub, PP, Sub, PA), + INST(mma, 0xD484, AtNamed<Ab, 0>, SX, SY, SX, SY, BAc, Add, PA, Add, PA), + INST(mma, 0xD4A0, AtNamed<Ab, 0>, SX, SY, SX, SY, BAc, Add, PP, Sub, PP), + INST(mma, 0x4D89, AtNamed<Ax, 1>, SX, SY, SX, UY, BZr, Add, PP, Sub, PP), + INST(mma, 0xD59D, AtNamed<Bx, 5>, SX, SY, SX, UY, BZr, Add, PP, Sub, PP), + INST(mma, 0x5F24, AtNamed<Ab, 0>, SX, SY, SX, UY, BZr, Add, PP, Add, PP), + INST(mma, 0x8069, AtNamed<Ab, 8>, SX, SY, SX, UY, BAc, Add, PP, Add, PP), + INST(mma, 0x8079, AtNamed<Ab, 8>, SX, SY, SX, UY, BAc, Add, PP, Add, PA), + INST(mma, 0x8469, AtNamed<Ab, 8>, SX, SY, SX, UY, BAc, Sub, PP, Sub, PP), + INST(mma, 0x8479, AtNamed<Ab, 8>, SX, SY, SX, UY, BAc, Sub, PP, Sub, PA), + INST(mma, 0xD584, AtNamed<Ab, 0>, SX, SY, SX, UY, BAc, Add, PA, Add, PA), + INST(mma, 0xD5A0, AtNamed<Ab, 0>, SX, SY, SX, UY, BAc, Add, PP, Sub, PP), + + // [[[XXX_mm_XXX_mm_XXX]]] + INST(mma, 0xCA00, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, UX, SY, UX, SY, BAc, Sub, PP, Sub, PA), + INST(mma, 0xCA01, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, UX, SY, SX, UY, BAc, Sub, PP, Sub, PA), + INST(mma, 0xCA02, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, UX, SY, UX, SY, BAc, Sub, PA, Sub, PA), + INST(mma, 0xCA03, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, UX, SY, SX, UY, BAc, Sub, PA, Sub, PA), + INST(mma, 0xCA04, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, UX, SY, UX, SY, BAc, Add, PP, Add, PA), + INST(mma, 0xCA05, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, UX, SY, SX, UY, BAc, Add, PP, Add, PA), + INST(mma, 0xCA06, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, UX, SY, UX, SY, BAc, Add, PA, Add, PA), + INST(mma, 0xCA07, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, UX, SY, SX, UY, BAc, Add, PA, Add, PA), + INST(mma, 0xCB00, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, SX, SY, UX, SY, BAc, Sub, PP, Sub, PP), + INST(mma, 0xCB01, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, SX, SY, SX, UY, BAc, Sub, PP, Sub, PP), + INST(mma, 0xCB02, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, SX, SY, UX, SY, BAc, Sub, PP, Sub, PA), + INST(mma, 0xCB03, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, SX, SY, SX, UY, BAc, Sub, PP, Sub, PA), + INST(mma, 0xCB04, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, SX, SY, UX, SY, BAc, Add, PP, Add, PP), + INST(mma, 0xCB05, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, SX, SY, SX, UY, BAc, Add, PP, Add, PP), + INST(mma, 0xCB06, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, SX, SY, UX, SY, BAc, Add, PP, Add, PA), + INST(mma, 0xCB07, At<ArpRn1, 5>, At<ArpStep1, 3>, At<ArpStep1, 4>, EMod, EMod, AtNamed<Ab, 6>, SX, SY, SX, UY, BAc, Add, PP, Add, PA), + + INST(mma, 0x0D30, At<ArpRn1, 3>, At<ArpStep1, 1>, At<ArpStep1, 2>, EMod, DMod, AtNamed<Ax, 0>, SX, SY, SX, UY, BAc, Add, PP, Add, PA), + INST(mma, 0x0D20, At<ArpRn1, 3>, At<ArpStep1, 1>, At<ArpStep1, 2>, DMod, EMod, AtNamed<Ax, 0>, SX, SY, SX, UY, BAc, Add, PP, Add, PA), + INST(mma, 0x4B50, At<ArpRn1, 3>, At<ArpStep1, 1>, At<ArpStep1, 2>, DMod, DMod, AtNamed<Ax, 0>, SX, SY, SX, UY, BAc, Add, PP, Add, PA), + + INST(mma, 0x9861, At<ArpRn1, 4>, At<ArpStep1, 2>, At<ArpStep1, 3>, EMod, DMod, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PP), + INST(mma, 0x9862, At<ArpRn1, 4>, At<ArpStep1, 2>, At<ArpStep1, 3>, DMod, EMod, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PP), + INST(mma, 0x9863, At<ArpRn1, 4>, At<ArpStep1, 2>, At<ArpStep1, 3>, DMod, DMod, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PP), + + INST(mma, 0x98E1, At<ArpRn1, 4>, At<ArpStep1, 2>, At<ArpStep1, 3>, EMod, DMod, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PA), + INST(mma, 0x98E2, At<ArpRn1, 4>, At<ArpStep1, 2>, At<ArpStep1, 3>, DMod, EMod, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PA), + INST(mma, 0x98E3, At<ArpRn1, 4>, At<ArpStep1, 2>, At<ArpStep1, 3>, DMod, DMod, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PA), + + INST(mma, 0x80C8, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, EMod, EMod, AtNamed<Ab, 10>, SX, SY, SX, SY, BAc, Add, PP, Sub, PP), + INST(mma, 0x81C8, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, EMod, EMod, AtNamed<Ab, 10>, SX, SY, SX, SY, BAc, Add, PP, Sub, PA), + INST(mma, 0x82C8, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, EMod, EMod, AtNamed<Ab, 10>, SX, SY, SX, SY, BZr, Add, PP, Add, PP), + INST(mma, 0x83C8, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, EMod, EMod, AtNamed<Ab, 10>, SX, SY, SX, SY, BZr, Add, PP, Add, PA), + + INST(mma, 0x80C2, At<ArpRn1, 0>, At<ArpStep1, 8>, At<ArpStep1, 9>, EMod, EMod, AtNamed<Ab, 10>, SX, SY, SX, SY, BAc, Add, PP, Add, PA), + INST(mma, 0x49C8, At<ArpRn1, 2>, At<ArpStep1, 0>, At<ArpStep1, 1>, EMod, EMod, AtNamed<Ab, 4>, SX, SY, SX, SY, BAc, Sub, PP, Sub, PA), + INST(mma, 0x00C0, At<ArpRn1, 3>, At<ArpStep1, 1>, At<ArpStep1, 2>, EMod, EMod, AtNamed<Ab, 4>, SX, SY, SX, SY, BZr, Add, PP, Sub, PP), + INST(mma, 0x00C1, At<ArpRn1, 3>, At<ArpStep1, 1>, At<ArpStep1, 2>, EMod, EMod, AtNamed<Ab, 4>, SX, SY, SX, SY, BZr, Add, PP, Sub, PA), + INST(mma, 0xD7A0, At<ArpRn1, 3>, At<ArpStep1, 1>, At<ArpStep1, 2>, EMod, EMod, AtNamed<Ax, 4>, SX, SY, SX, SY, BSv, Add, PP, Add, PP), + INST(mma, 0xD7A1, At<ArpRn1, 3>, At<ArpStep1, 1>, At<ArpStep1, 2>, EMod, EMod, AtNamed<Ax, 4>, SX, SY, SX, SY, BSr, Add, PP, Add, PP), + + INST(mma, 0xC800, At<ArpRn2, 4>, At<ArpStep2, 0>, At<ArpStep2, 2>, EMod, EMod, AtNamed<Ab, 6>, SX, SY, SX, SY, BAc, Add, PP, Add, PP), + INST(mma, 0xC900, At<ArpRn2, 4>, At<ArpStep2, 0>, At<ArpStep2, 2>, EMod, EMod, AtNamed<Ab, 6>, SX, SY, SX, SY, BAc, Sub, PP, Sub, PP), + + // [[[XXX_mx_XXX_xy_XXX]]] + INST(mma_mx_xy, 0xD5E0, At<ArRn1, 1>, At<ArStep1, 0>, AtNamed<Ax, 3>, SX, SY, SX, SY, BAc, Sub, PP, Sub, PP), + INST(mma_mx_xy, 0xD5E4, At<ArRn1, 1>, At<ArStep1, 0>, AtNamed<Ax, 3>, SX, SY, SX, SY, BAc, Add, PP, Add, PP), + + // [[[XXX_xy_XXX_mx_XXX]]] + INST(mma_xy_mx, 0x8862, At<ArRn1, 4>, At<ArStep1, 3>, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Sub, PP, Sub, PP), + INST(mma_xy_mx, 0x8A62, At<ArRn1, 4>, At<ArStep1, 3>, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PP), + + // [[[XXX_my_XXX_my_XXX]]] + INST(mma_my_my, 0x4DA0, At<ArRn1, 3>, At<ArStep1, 2>, AtNamed<Ax, 4>, SX, SY, SX, UY, BAc, Sub, PP, Sub, PP), + INST(mma_my_my, 0x4DA1, At<ArRn1, 3>, At<ArStep1, 2>, AtNamed<Ax, 4>, SX, SY, SX, UY, BAc, Sub, PP, Sub, PA), + INST(mma_my_my, 0x4DA2, At<ArRn1, 3>, At<ArStep1, 2>, AtNamed<Ax, 4>, SX, SY, SX, UY, BAc, Add, PP, Add, PP), + INST(mma_my_my, 0x4DA3, At<ArRn1, 3>, At<ArStep1, 2>, AtNamed<Ax, 4>, SX, SY, SX, UY, BAc, Add, PP, Add, PA), + + INST(mma_my_my, 0x94E0, At<ArRn1, 4>, At<ArStep1, 3>, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Sub, PP, Sub, PP), + INST(mma_my_my, 0x94E1, At<ArRn1, 4>, At<ArStep1, 3>, AtNamed<Ax, 8>, SX, SY, UX, SY, BAc, Sub, PP, Sub, PP), + INST(mma_my_my, 0x94E2, At<ArRn1, 4>, At<ArStep1, 3>, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Sub, PP, Sub, PA), + INST(mma_my_my, 0x94E3, At<ArRn1, 4>, At<ArStep1, 3>, AtNamed<Ax, 8>, SX, SY, UX, SY, BAc, Sub, PP, Sub, PA), + INST(mma_my_my, 0x94E4, At<ArRn1, 4>, At<ArStep1, 3>, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PP), + INST(mma_my_my, 0x94E5, At<ArRn1, 4>, At<ArStep1, 3>, AtNamed<Ax, 8>, SX, SY, UX, SY, BAc, Add, PP, Add, PP), + INST(mma_my_my, 0x94E6, At<ArRn1, 4>, At<ArStep1, 3>, AtNamed<Ax, 8>, SX, SY, SX, SY, BAc, Add, PP, Add, PA), + INST(mma_my_my, 0x94E7, At<ArRn1, 4>, At<ArStep1, 3>, AtNamed<Ax, 8>, SX, SY, UX, SY, BAc, Add, PP, Add, PA), + + // [[[XXX_xy_XXX_xy_XXX_mov]]] + INST(mma_mov, 0x4FA0, At<Axh, 6>, At<Bxh, 2>, At<ArRn1, 1>, At<ArStep1, 0>, AtNamed<Ab, 3>, SX, SY, SX, SY, BAc, Add, PP, Add, PP), + INST(mma_mov, 0xD3A0, At<Axh, 6>, At<Bxh, 2>, At<ArRn1, 1>, At<ArStep1, 0>, AtNamed<Ab, 3>, SX, SY, SX, SY, BAc, Add, PP, Sub, PP), + INST(mma_mov, 0x80D0, At<Axh, 9>, At<Bxh, 8>, At<ArRn1, 3>, At<ArStep1, 2>, AtNamed<Ax, 10>, SX, SY, SX, SY, BSv, Add, PP, Sub, PP), + INST(mma_mov, 0x80D1, At<Axh, 9>, At<Bxh, 8>, At<ArRn1, 3>, At<ArStep1, 2>, AtNamed<Ax, 10>, SX, SY, SX, SY, BSr, Add, PP, Sub, PP), + INST(mma_mov, 0x80D2, At<Axh, 9>, At<Bxh, 8>, At<ArRn1, 3>, At<ArStep1, 2>, AtNamed<Ax, 10>, SX, SY, SX, SY, BSv, Add, PP, Add, PP), + INST(mma_mov, 0x80D3, At<Axh, 9>, At<Bxh, 8>, At<ArRn1, 3>, At<ArStep1, 2>, AtNamed<Ax, 10>, SX, SY, SX, SY, BSr, Add, PP, Add, PP), + INST(mma_mov, 0x5818, At<ArRn2, 7>, At<ArStep1, 6>, AtNamed<Ax, 0>, SX, SY, SX, SY, BSv, Add, PP, Sub, PP), + INST(mma_mov, 0x5838, At<ArRn2, 7>, At<ArStep1, 6>, AtNamed<Ax, 0>, SX, SY, SX, SY, BSr, Add, PP, Sub, PP), + + INST(addhp, 0x90E0, At<ArRn2, 2>, At<ArStep2, 0>, At<Px, 4>, At<Ax, 8>), + }; + +#undef INST +#undef EXCEPT +} + +// clang-format on + +template <typename V> +Matcher<V> Decode(u16 instruction) { + static const auto table = GetDecodeTable<V>(); + + const auto matches_instruction = [instruction](const auto& matcher) { + return matcher.Matches(instruction); + }; + + auto iter = std::find_if(table.begin(), table.end(), matches_instruction); + if (iter == table.end()) { + return Matcher<V>::AllMatcher([](V& v, u16 opcode, u16) { return v.undefined(opcode); }); + } else { + auto other = std::find_if(iter + 1, table.end(), matches_instruction); + ASSERT(other == table.end()); + return *iter; + } +} + +template <typename V> +std::vector<Matcher<V>> GetDecoderTable() { + std::vector<Matcher<V>> table; + table.reserve(0x10000); + for (u32 i = 0; i < 0x10000; ++i) { + table.push_back(Decode<V>((u16)i)); + } + return table; +} diff --git a/src/teakra/src/decoder.md b/src/teakra/src/decoder.md new file mode 100644 index 0000000..79b98f7 --- /dev/null +++ b/src/teakra/src/decoder.md @@ -0,0 +1,68 @@ +# Teak Instruction Set Encoding + +Each opcode is one- or two-word wide. In this code base, two-word wide opcodes are generally referred as "expanded". The optional second word is used purely for parameters, so the opcode family can always be determined from the first word (which then determine whether there is a second word). The encoding is very messy. The general theme of the encoding are + - The encoding seems first designed for TeakLite (which is already a mess), then additional opcodes from Teak squeezed in formerly unused locations. + - While registers `r0`~`r7` are generally at equal status in the computation model, they are not always in the opcodes. Specifically, as `r6` doesn't present in TeakLite, and because of the opcode squeezing, many opcodes involving `r6` are encoded separately with seemingly random pattern. + - Similarly, there are also opcodes that are in the same family but have different pattern for different arguments. + - Some specific combination of arguments can be invalid for one opcode, and this specific pattern can be used for a totally different opcode. + - Some opcodes have unused bits, where setting either 0 or 1 has no effect. + +## Comparison between `decoder.h` and gbatek + +`decoder.h` contains the full table of the instruction set. It is derived from the table in gbatek, but rearranged a little according to the opcode families and patterns. The notation of the two table are comparable. For example: + +``` +teakra: INST(push, 0xD7C8, At<Abe, 1>, Unused<0>) +gbatek: D7C8h TL2 push Abe@1, Unused1@0 +``` +This means that the opcode has a parameter `Abe` at bit 1 (which is 2-bit long, defined by operand `Abe`), and an unused bit at bit 0. The rest bit of the opcode (bit 3~15) should match the pattern `0xD7C8`. The assembly of this opcode would be like `push <Abe>`. + +Sometimes there is a 18-bit address parameter that is split into two parts: + +``` +teakra: INST(br, 0x4180, At<Address18_16, 16>, At<Address18_2, 4>, At<Cond, 0>) +gbatek: 4180h TL br Address18@16and4, Cond@0 +``` +Note that the existence of `At<..., 16>` also indicates that this opcode is 2-word long. The pattern `0x4180` is only matched against the first word. + +Some opcodes that have the same pattern are merged into one opcode in teakra. For example +``` +teakra: INST(alm, 0xA000, At<Alm, 9>, At<MemImm8, 0>, At<Ax, 8>), +gbatek: { + A000h TL or MemImm8@0, Ax@8 + A200h TL and MemImm8@0, Ax@8 + ... + BE00h TL cmpu MemImm8@0, Ax@8 +} +``` +Here the operation name is also treated as an operand `Alm` in teakra. + +Opcodes with constants that has special encoding are marked in their opcode names: +``` +teakra: INST(sub_p1, 0xD4B9, At<Ax, 8>), +gbatek: D4B9h TL2 sub p1, Ax@8 +``` + +However, if several opcodes with constants have different encoding but similar semantics, the constants are placed in the parameter list and delegate to the same opcode +``` +teakra: { + INST(alm_r6, 0xD388, Const<Alm, 0>, At<Ax, 4>), + INST(alm_r6, 0xD389, Const<Alm, 1>, At<Ax, 4>), + ... + INST(alm_r6, 0x9462, Const<Alm, 8>, At<Ax, 0>), + ... + INST(alm_r6, 0x5F41, Const<Alm, 13>, Const<Ax, 0>), + INST(alm_r6, 0x9062, Const<Alm, 14>, At<Ax, 8>, Unused<0>), + INST(alm_r6, 0x8A63, Const<Alm, 15>, At<Ax, 3>), +} +gbatek: { + D388h TL2 or r6, Ax@4 + D389h TL2 and r6, Ax@4 + ... + 9462h TL2 msu y0, r6, Ax@0 // y0 is implied by msu + ... + 5F41h TL2 sqr r6 // Ax is implied/unused for sqr + 9062h TL2 sqra r6, Ax@8, Unused1@0 + 8A63h TL2 cmpu r6, Ax@3 +} +```
\ No newline at end of file diff --git a/src/teakra/src/disassembler.cpp b/src/teakra/src/disassembler.cpp new file mode 100644 index 0000000..7f3aa03 --- /dev/null +++ b/src/teakra/src/disassembler.cpp @@ -0,0 +1,1763 @@ +#include <iomanip> +#include <sstream> +#include <type_traits> +#include <vector> +#include "common_types.h" +#include "crash.h" +#include "decoder.h" +#include "operand.h" +#include "teakra/disassembler.h" + +namespace Teakra::Disassembler { + +template <typename T> +std::string ToHex(T i) { + u64 v = i; + std::stringstream stream; + stream << "0x" << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex << v; + return stream.str(); +} + +template <unsigned bits> +std::string Dsm(Imm<bits> a) { + std::string eight_mark = bits == 8 ? "u8" : ""; + return ToHex(a.Unsigned16()) + eight_mark; +} + +template <unsigned bits> +std::string Dsm(Imms<bits> a) { + u16 value = a.Signed16(); + bool negative = (value >> 15) != 0; + if (negative) { + value = (~value) + 1; + } + return (negative ? "-" : "+") + ToHex(value); +} + +std::string Dsm(MemImm8 a) { + return "[page:" + Dsm((Imm8)a) + "]"; +} + +std::string Dsm(MemImm16 a) { + return "[" + Dsm((Imm16)a) + "]"; +} + +std::string Dsm(MemR7Imm16 a) { + return "[r7+" + Dsm((Imm16)a) + "]"; +} +std::string Dsm(MemR7Imm7s a) { + return "[r7" + Dsm((Imm7s)a) + "s7]"; +} + +std::string DsmReg(RegName a) { + switch (a) { + case RegName::a0: + return "a0"; + case RegName::a0l: + return "a0l"; + case RegName::a0h: + return "a0h"; + case RegName::a0e: + return "a0e"; + case RegName::a1: + return "a1"; + case RegName::a1l: + return "a1l"; + case RegName::a1h: + return "a1h"; + case RegName::a1e: + return "a1e"; + case RegName::b0: + return "b0"; + case RegName::b0l: + return "b0l"; + case RegName::b0h: + return "b0h"; + case RegName::b0e: + return "b0e"; + case RegName::b1: + return "b1"; + case RegName::b1l: + return "b1l"; + case RegName::b1h: + return "b1h"; + case RegName::b1e: + return "b1e"; + + case RegName::r0: + return "r0"; + case RegName::r1: + return "r1"; + case RegName::r2: + return "r2"; + case RegName::r3: + return "r3"; + case RegName::r4: + return "r4"; + case RegName::r5: + return "r5"; + case RegName::r6: + return "r6"; + case RegName::r7: + return "r7"; + + case RegName::ar0: + return "ar0"; + case RegName::ar1: + return "ar1"; + case RegName::arp0: + return "arp0"; + case RegName::arp1: + return "arp1"; + case RegName::arp2: + return "arp2"; + case RegName::arp3: + return "arp3"; + case RegName::stt0: + return "stt0"; + case RegName::stt1: + return "stt1"; + case RegName::stt2: + return "stt2"; + case RegName::mod0: + return "mod0"; + case RegName::mod1: + return "mod1"; + case RegName::mod2: + return "mod2"; + case RegName::mod3: + return "mod3"; + case RegName::cfgi: + return "cfgi"; + case RegName::cfgj: + return "cfgj"; + + case RegName::y0: + return "y0"; + case RegName::p: + return "p*"; + case RegName::pc: + return "pc"; + case RegName::sp: + return "sp"; + case RegName::sv: + return "sv"; + case RegName::lc: + return "lc"; + + case RegName::st0: + return "st0"; + case RegName::st1: + return "st1"; + case RegName::st2: + return "st2"; + default: + return "[ERROR]" + std::to_string((int)a); + } +} + +template <typename RegT> +std::string R(RegT a) { + return DsmReg(a.GetName()); +} + +template <> +std::string R(Px a) { + return "p" + std::to_string(a.Index()); +} + +std::string Dsm(Alm alm) { + switch (alm.GetName()) { + case AlmOp::Or: + return "or"; + case AlmOp::And: + return "and"; + case AlmOp::Xor: + return "xor"; + case AlmOp::Add: + return "add"; + case AlmOp::Tst0: + return "tst0"; + case AlmOp::Tst1: + return "tst1"; + case AlmOp::Cmp: + return "cmp"; + case AlmOp::Sub: + return "sub"; + case AlmOp::Msu: + return "msu"; + case AlmOp::Addh: + return "addh"; + case AlmOp::Addl: + return "addl"; + case AlmOp::Subh: + return "subh"; + case AlmOp::Subl: + return "subl"; + case AlmOp::Sqr: + return "sqr"; + case AlmOp::Sqra: + return "sqra"; + case AlmOp::Cmpu: + return "cmpu"; + default: + return "[ERROR]"; + } +} + +std::string Dsm(Alu alu) { + switch (alu.GetName()) { + case AlmOp::Or: + return "or"; + case AlmOp::And: + return "and"; + case AlmOp::Xor: + return "xor"; + case AlmOp::Add: + return "add"; + case AlmOp::Cmp: + return "cmp"; + case AlmOp::Sub: + return "sub"; + default: + return "[ERROR]"; + } +} + +std::string Dsm(Alb alb) { + switch (alb.GetName()) { + case AlbOp::Set: + return "set"; + case AlbOp::Rst: + return "rst"; + case AlbOp::Chng: + return "chng"; + case AlbOp::Addv: + return "addv"; + case AlbOp::Tst0: + return "tst0"; + case AlbOp::Tst1: + return "tst1"; + case AlbOp::Cmpv: + return "cmpv"; + case AlbOp::Subv: + return "subv"; + default: + return "[ERROR]"; + } +} + +std::string Dsm(Moda4 moda4) { + switch (moda4.GetName()) { + case ModaOp::Shr: + return "shr"; + case ModaOp::Shr4: + return "shr4"; + case ModaOp::Shl: + return "shl"; + case ModaOp::Shl4: + return "shl4"; + case ModaOp::Ror: + return "ror"; + case ModaOp::Rol: + return "rol"; + case ModaOp::Clr: + return "clr"; + case ModaOp::Not: + return "not"; + case ModaOp::Neg: + return "neg"; + case ModaOp::Rnd: + return "rnd"; + case ModaOp::Pacr: + return "pacr"; + case ModaOp::Clrr: + return "clrr"; + case ModaOp::Inc: + return "inc"; + case ModaOp::Dec: + return "dec"; + case ModaOp::Copy: + return "copy"; + default: + return "[ERROR]"; + } +} + +std::string Dsm(Moda3 moda3) { + switch (moda3.GetName()) { + case ModaOp::Shr: + return "shr"; + case ModaOp::Shr4: + return "shr4"; + case ModaOp::Shl: + return "shl"; + case ModaOp::Shl4: + return "shl4"; + case ModaOp::Ror: + return "ror"; + case ModaOp::Rol: + return "rol"; + case ModaOp::Clr: + return "clr"; + case ModaOp::Clrr: + return "clrr"; + default: + return "[ERROR]"; + } +} + +std::string Dsm(Mul3 mul) { + switch (mul.GetName()) { + case MulOp::Mpy: + return "mpy"; + case MulOp::Mpysu: + return "mpysu"; + case MulOp::Mac: + return "mac"; + case MulOp::Macus: + return "macus"; + case MulOp::Maa: + return "maa"; + case MulOp::Macuu: + return "macuu"; + case MulOp::Macsu: + return "macsu"; + case MulOp::Maasu: + return "maasu"; + default: + return "[ERROR]"; + } +} + +std::string Dsm(Mul2 mul) { + switch (mul.GetName()) { + case MulOp::Mpy: + return "mpy"; + case MulOp::Mac: + return "mac"; + case MulOp::Maa: + return "maa"; + case MulOp::Macsu: + return "macsu"; + default: + return "[ERROR]"; + } +} + +std::string Dsm(Cond cond) { + switch (cond.GetName()) { + case CondValue::True: + return "always"; + case CondValue::Eq: + return "eq"; + case CondValue::Neq: + return "neq"; + case CondValue::Gt: + return "gt"; + case CondValue::Ge: + return "ge"; + case CondValue::Lt: + return "lt"; + case CondValue::Le: + return "le"; + case CondValue::Nn: + return "mn"; + case CondValue::C: + return "c"; + case CondValue::V: + return "v"; + case CondValue::E: + return "e"; + case CondValue::L: + return "l"; + case CondValue::Nr: + return "nr"; + case CondValue::Niu0: + return "niu0"; + case CondValue::Iu0: + return "iu0"; + case CondValue::Iu1: + return "iu1"; + default: + return "[ERROR]"; + } +} + +std::string Dsm(Address16 addr) { + return ToHex(addr.Address32()); +} + +std::string A18(Address18_16 addr_low, Address18_2 addr_high) { + return ToHex(Address32(addr_low, addr_high)); +} + +std::string Dsm(StepZIDS step) { + switch (step.GetName()) { + case StepValue::Zero: + return ""; + case StepValue::Increase: + return "++"; + case StepValue::Decrease: + return "--"; + case StepValue::PlusStep: + return "++s"; + default: + return "[ERROR]"; + } +} + +std::string Dsm(std::string t) { + return t; +} + +template <typename RegT> +std::string MemR(RegT reg, StepZIDS step) { + return "[" + R(reg) + Dsm(step) + "]"; +} + +template <typename Reg> +std::string MemG(Reg reg) { + return "[" + R(reg) + "]"; +} + +std::string Dsm(CbsCond c) { + switch (c.GetName()) { + case CbsCondValue::Ge: + return "ge"; + case CbsCondValue::Gt: + return "gt"; + default: + return "[ERROR]"; + } +} + +template <typename... T> +std::vector<std::string> D(T... t) { + return std::vector<std::string>{Dsm(t)...}; +} + +std::string Mul(bool x_sign, bool y_sign) { + return std::string("mpy") + (x_sign ? "sx" : "ux") + (y_sign ? "sy" : "uy"); +} + +std::string PA(SumBase base, bool sub_p0, bool p0_align, bool sub_p1, bool p1_align) { + std::string result; + switch (base) { + case SumBase::Zero: + result = "0"; + break; + case SumBase::Acc: + result = "acc"; + break; + case SumBase::Sv: + result = "sv"; + break; + case SumBase::SvRnd: + result = "svr"; + break; + } + result += sub_p0 ? "-" : "+"; + result += "p0"; + result += p0_align ? "a" : ""; + result += sub_p1 ? "-" : "+"; + result += "p1"; + result += p1_align ? "a" : ""; + return result; +} + +class Disassembler { +public: + using instruction_return_type = std::vector<std::string>; + + std::vector<std::string> undefined(u16 opcode) { + return D("[ERROR]"); + } + + std::vector<std::string> nop() { + return D("nop"); + } + + std::vector<std::string> norm(Ax a, Rn b, StepZIDS bs) { + return D("norm", R(a), MemR(b, bs)); + } + std::vector<std::string> swap(SwapType swap) { + std::string desc; + switch (swap.GetName()) { + case SwapTypeValue::a0b0: + desc = "a0<->b0"; + break; + case SwapTypeValue::a0b1: + desc = "a0<->b1"; + break; + case SwapTypeValue::a1b0: + desc = "a1<->b0"; + break; + case SwapTypeValue::a1b1: + desc = "a1<->b1"; + break; + case SwapTypeValue::a0b0a1b1: + desc = "a<->b"; + break; + case SwapTypeValue::a0b1a1b0: + desc = "a-x-b"; + break; + case SwapTypeValue::a0b0a1: + desc = "a0->b0->a1"; + break; + case SwapTypeValue::a0b1a1: + desc = "a0->b1->a1"; + break; + case SwapTypeValue::a1b0a0: + desc = "a1->b0->a0"; + break; + case SwapTypeValue::a1b1a0: + desc = "a1->b1->a0"; + break; + case SwapTypeValue::b0a0b1: + desc = "b0->a0->b1"; + break; + case SwapTypeValue::b0a1b1: + desc = "b0->a1->b1"; + break; + case SwapTypeValue::b1a0b0: + desc = "b1->a0->b0"; + break; + case SwapTypeValue::b1a1b0: + desc = "b1->a1->b0"; + break; + default: + desc = "[ERROR]"; + } + return D("swap", desc); + } + std::vector<std::string> trap() { + return D("trap"); + } + + std::vector<std::string> alm(Alm op, MemImm8 a, Ax b) { + return D(op, a, R(b)); + } + std::vector<std::string> alm(Alm op, Rn a, StepZIDS as, Ax b) { + return D(op, MemR(a, as), R(b)); + } + std::vector<std::string> alm(Alm op, Register a, Ax b) { + return D(op, R(a), R(b)); + } + std::vector<std::string> alm_r6(Alm op, Ax b) { + return D(op, "r6", R(b)); + } + + std::vector<std::string> alu(Alu op, MemImm16 a, Ax b) { + return D(op, a, R(b)); + } + std::vector<std::string> alu(Alu op, MemR7Imm16 a, Ax b) { + return D(op, a, R(b)); + } + std::vector<std::string> alu(Alu op, Imm16 a, Ax b) { + return D(op, a, R(b)); + } + std::vector<std::string> alu(Alu op, Imm8 a, Ax b) { + return D(op, a, R(b)); + } + std::vector<std::string> alu(Alu op, MemR7Imm7s a, Ax b) { + return D(op, a, R(b)); + } + + std::vector<std::string> or_(Ab a, Ax b, Ax c) { + return D("or", R(a), R(b), R(c)); + } + std::vector<std::string> or_(Ax a, Bx b, Ax c) { + return D("or", R(a), R(b), R(c)); + } + std::vector<std::string> or_(Bx a, Bx b, Ax c) { + return D("or", R(a), R(b), R(c)); + } + + std::vector<std::string> alb(Alb op, Imm16 a, MemImm8 b) { + return D(op, a, b); + } + std::vector<std::string> alb(Alb op, Imm16 a, Rn b, StepZIDS bs) { + return D(op, a, MemR(b, bs)); + } + std::vector<std::string> alb(Alb op, Imm16 a, Register b) { + return D(op, a, R(b)); + } + std::vector<std::string> alb_r6(Alb op, Imm16 a) { + return D(op, a, "r6"); + } + std::vector<std::string> alb(Alb op, Imm16 a, SttMod b) { + return D(op, a, R(b)); + } + + std::vector<std::string> add(Ab a, Bx b) { + return D("add", R(a), R(b)); + } + std::vector<std::string> add(Bx a, Ax b) { + return D("add", R(a), R(b)); + } + std::vector<std::string> add_p1(Ax b) { + return D("add", "p1", R(b)); + } + std::vector<std::string> add(Px a, Bx b) { + return D("add", R(a), R(b)); + } + + std::vector<std::string> sub(Ab a, Bx b) { + return D("sub", R(a), R(b)); + } + std::vector<std::string> sub(Bx a, Ax b) { + return D("sub", R(a), R(b)); + } + std::vector<std::string> sub_p1(Ax b) { + return D("sub", "p1", R(b)); + } + std::vector<std::string> sub(Px a, Bx b) { + return D("sub", R(a), R(b)); + } + + std::vector<std::string> app(Ab c, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + return D(PA(base, sub_p0, p0_align, sub_p1, p1_align), R(c)); + } + + std::vector<std::string> add_add(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return D("add||add", MemARPSJ(a, asj), MemARPSI(a, asi), R(b)); + } + std::vector<std::string> add_sub(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return D("add||sub", MemARPSJ(a, asj), MemARPSI(a, asi), R(b)); + } + std::vector<std::string> sub_add(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return D("sub||add", MemARPSJ(a, asj), MemARPSI(a, asi), R(b)); + } + std::vector<std::string> sub_sub(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return D("sub||sub", MemARPSJ(a, asj), MemARPSI(a, asi), R(b)); + } + + std::vector<std::string> add_sub_sv(ArRn1 a, ArStep1 as, Ab b) { + return D("add||sub", MemARS(a, as), "sv", R(b)); + } + std::vector<std::string> sub_add_sv(ArRn1 a, ArStep1 as, Ab b) { + return D("sub||add", MemARS(a, as), "sv", R(b)); + } + + std::vector<std::string> sub_add_i_mov_j_sv(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return D("sub||add", MemARPSI(a, asi), R(b), "||mov", MemARPSJ(a, asj), "sv"); + } + std::vector<std::string> sub_add_j_mov_i_sv(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return D("sub||add", MemARPSJ(a, asj), R(b), "||mov", MemARPSI(a, asi), "sv"); + } + std::vector<std::string> add_sub_i_mov_j(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return D("add_sub", MemARPSI(a, asi), R(b), "||mov", R(b), MemARPSJ(a, asj)); + } + std::vector<std::string> add_sub_j_mov_i(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return D("add_sub", MemARPSJ(a, asj), R(b), "||mov", R(b), MemARPSI(a, asi)); + } + + std::vector<std::string> moda4(Moda4 op, Ax a, Cond cond) { + return D(op, R(a), cond); + } + + std::vector<std::string> moda3(Moda3 op, Bx a, Cond cond) { + return D(op, R(a), cond); + } + + std::vector<std::string> pacr1(Ax a) { + return D("pacr p1", R(a)); + } + std::vector<std::string> clr(Ab a, Ab b) { + return D("clr", R(a), R(b)); + } + std::vector<std::string> clrr(Ab a, Ab b) { + return D("clrr", R(a), R(b)); + } + + std::vector<std::string> bkrep(Imm8 a, Address16 addr) { + return D("bkrep", a, addr); + } + std::vector<std::string> bkrep(Register a, Address18_16 addr_low, Address18_2 addr_high) { + return D("bkrep", R(a), A18(addr_low, addr_high)); + } + std::vector<std::string> bkrep_r6(Address18_16 addr_low, Address18_2 addr_high) { + return D("bkrep", "r6", A18(addr_low, addr_high)); + } + std::vector<std::string> bkreprst(ArRn2 a) { + return D("bkreprst", MemAR(a)); + } + std::vector<std::string> bkreprst_memsp() { + return D("bkreprst", "[sp]"); + } + std::vector<std::string> bkrepsto(ArRn2 a) { + return D("bkrepsto", MemAR(a)); + } + std::vector<std::string> bkrepsto_memsp() { + return D("bkrepsto", "[sp]"); + } + + std::vector<std::string> banke(BankFlags flags) { + std::vector<std::string> s{"banke"}; + if (flags.R0()) + s.push_back("r0"); + if (flags.R1()) + s.push_back("r1"); + if (flags.R4()) + s.push_back("r4"); + if (flags.Cfgi()) + s.push_back("cfgi"); + if (flags.R7()) + s.push_back("r7"); + if (flags.Cfgj()) + s.push_back("cfgj"); + return s; + } + std::vector<std::string> bankr() { + return D("bankr"); + } + std::vector<std::string> bankr(Ar a) { + return D("bankr", R(a)); + } + std::vector<std::string> bankr(Ar a, Arp b) { + return D("bankr", R(a), R(b)); + } + std::vector<std::string> bankr(Arp a) { + return D("bankr", R(a)); + } + + std::vector<std::string> bitrev(Rn a) { + return D("bitrev", R(a)); + } + std::vector<std::string> bitrev_dbrv(Rn a) { + return D("bitrev", R(a), "dbrv"); + } + std::vector<std::string> bitrev_ebrv(Rn a) { + return D("bitrev", R(a), "ebrv"); + } + + std::vector<std::string> br(Address18_16 addr_low, Address18_2 addr_high, Cond cond) { + return D("br", A18(addr_low, addr_high), cond); + } + + std::vector<std::string> brr(RelAddr7 addr, Cond cond) { + return D("brr", ToHex((u16)addr.Relative32()), cond); + } + + std::vector<std::string> break_() { + return D("break"); + } + + std::vector<std::string> call(Address18_16 addr_low, Address18_2 addr_high, Cond cond) { + return D("call", A18(addr_low, addr_high), cond); + } + std::vector<std::string> calla(Axl a) { + return D("calla", R(a)); + } + std::vector<std::string> calla(Ax a) { + return D("calla", R(a)); + } + std::vector<std::string> callr(RelAddr7 addr, Cond cond) { + return D("callr", ToHex((u16)addr.Relative32()), cond); + } + + std::vector<std::string> cntx_s() { + return D("cntx", "s"); + } + std::vector<std::string> cntx_r() { + return D("cntx", "r"); + } + + std::vector<std::string> ret(Cond c) { + return D("ret", c); + } + std::vector<std::string> retd() { + return D("retd"); + } + std::vector<std::string> reti(Cond c) { + return D("reti", c); + } + std::vector<std::string> retic(Cond c) { + return D("retic", c); + } + std::vector<std::string> retid() { + return D("retid"); + } + std::vector<std::string> retidc() { + return D("retidc"); + } + std::vector<std::string> rets(Imm8 a) { + return D("rets", a); + } + + std::vector<std::string> load_ps(Imm2 a) { + return D("load", a, "ps"); + } + std::vector<std::string> load_stepi(Imm7s a) { + return D("load", a, "stepi"); + } + std::vector<std::string> load_stepj(Imm7s a) { + return D("load", a, "stepj"); + } + std::vector<std::string> load_page(Imm8 a) { + return D("load", a, "page"); + } + std::vector<std::string> load_modi(Imm9 a) { + return D("load", a, "modi"); + } + std::vector<std::string> load_modj(Imm9 a) { + return D("load", a, "modj"); + } + std::vector<std::string> load_movpd(Imm2 a) { + return D("load", a, "movpd"); + } + std::vector<std::string> load_ps01(Imm4 a) { + return D("load", a, "ps01"); + } + + std::vector<std::string> push(Imm16 a) { + return D("push", a); + } + std::vector<std::string> push(Register a) { + return D("push", R(a)); + } + std::vector<std::string> push(Abe a) { + return D("push", R(a)); + } + std::vector<std::string> push(ArArpSttMod a) { + return D("push", R(a)); + } + std::vector<std::string> push_prpage() { + return D("push", "prpage"); + } + std::vector<std::string> push(Px a) { + return D("push", R(a)); + } + std::vector<std::string> push_r6() { + return D("push", "r6"); + } + std::vector<std::string> push_repc() { + return D("push", "repc"); + } + std::vector<std::string> push_x0() { + return D("push", "x0"); + } + std::vector<std::string> push_x1() { + return D("push", "x1"); + } + std::vector<std::string> push_y1() { + return D("push", "y1"); + } + std::vector<std::string> pusha(Ax a) { + return D("pusha", R(a)); + } + std::vector<std::string> pusha(Bx a) { + return D("pusha", R(a)); + } + + std::vector<std::string> pop(Register a) { + return D("pop", R(a)); + } + std::vector<std::string> pop(Abe a) { + return D("pop", R(a)); + } + std::vector<std::string> pop(ArArpSttMod a) { + return D("pop", R(a)); + } + std::vector<std::string> pop(Bx a) { + return D("pop", R(a)); + } + std::vector<std::string> pop_prpage() { + return D("pop", "prpage"); + } + std::vector<std::string> pop(Px a) { + return D("pop", R(a)); + } + std::vector<std::string> pop_r6() { + return D("pop", "r6"); + } + std::vector<std::string> pop_repc() { + return D("pop", "repc"); + } + std::vector<std::string> pop_x0() { + return D("pop", "x0"); + } + std::vector<std::string> pop_x1() { + return D("pop", "x1"); + } + std::vector<std::string> pop_y1() { + return D("pop", "y1"); + } + std::vector<std::string> popa(Ab a) { + return D("popa", R(a)); + } + + std::vector<std::string> rep(Imm8 a) { + return D("rep", a); + } + std::vector<std::string> rep(Register a) { + return D("rep", R(a)); + } + std::vector<std::string> rep_r6() { + return D("rep", "r6"); + } + + std::vector<std::string> shfc(Ab a, Ab b, Cond cond) { + return D("shfc", R(a), R(b), cond); + } + std::vector<std::string> shfi(Ab a, Ab b, Imm6s s) { + return D("shfi", R(a), R(b), s); + } + + std::vector<std::string> tst4b(ArRn2 b, ArStep2 bs) { + return D("tst4b", "a0l", MemARS(b, bs)); + } + std::vector<std::string> tst4b(ArRn2 b, ArStep2 bs, Ax c) { + return D("tst4b", "a0l", MemARS(b, bs), R(c)); + } + std::vector<std::string> tstb(MemImm8 a, Imm4 b) { + return D("tstb", a, b); + } + std::vector<std::string> tstb(Rn a, StepZIDS as, Imm4 b) { + return D("tstb", MemR(a, as), b); + } + std::vector<std::string> tstb(Register a, Imm4 b) { + return D("tstb", R(a), b); + } + std::vector<std::string> tstb_r6(Imm4 b) { + return D("tstb", "r6", b); + } + std::vector<std::string> tstb(SttMod a, Imm16 b) { + return D("tstb", R(a), b); + } + + std::vector<std::string> and_(Ab a, Ab b, Ax c) { + return D("and", R(a), R(b), R(c)); + } + + std::vector<std::string> dint() { + return D("dint"); + } + std::vector<std::string> eint() { + return D("eint"); + } + + std::vector<std::string> mul(Mul3 op, Rn y, StepZIDS ys, Imm16 x, Ax a) { + return D(op, MemR(y, ys), x, R(a)); + } + std::vector<std::string> mul_y0(Mul3 op, Rn x, StepZIDS xs, Ax a) { + return D(op, "y0", MemR(x, xs), R(a)); + } + std::vector<std::string> mul_y0(Mul3 op, Register x, Ax a) { + return D(op, "y0", R(x), R(a)); + } + std::vector<std::string> mul(Mul3 op, R45 y, StepZIDS ys, R0123 x, StepZIDS xs, Ax a) { + return D(op, MemR(y, ys), MemR(x, xs), R(a)); + } + std::vector<std::string> mul_y0_r6(Mul3 op, Ax a) { + return D(op, "y0", "r6", R(a)); + } + std::vector<std::string> mul_y0(Mul2 op, MemImm8 x, Ax a) { + return D(op, "y0", x, R(a)); + } + + std::vector<std::string> mpyi(Imm8s x) { + return D("mpyi", "y0", x); + } + + std::vector<std::string> msu(R45 y, StepZIDS ys, R0123 x, StepZIDS xs, Ax a) { + return D("msu", MemR(y, ys), MemR(x, xs), R(a)); + } + std::vector<std::string> msu(Rn y, StepZIDS ys, Imm16 x, Ax a) { + return D("msu", MemR(y, ys), x, R(a)); + } + std::vector<std::string> msusu(ArRn2 x, ArStep2 xs, Ax a) { + return D("msusu", "y0", MemARS(x, xs), R(a)); + } + std::vector<std::string> mac_x1to0(Ax a) { + return D("mac", "y0", "x1->x0", R(a)); + } + std::vector<std::string> mac1(ArpRn1 xy, ArpStep1 xis, ArpStep1 yjs, Ax a) { + return D("mac1", MemARPSJ(xy, yjs), MemARPSI(xy, xis), R(a)); + } + + std::vector<std::string> modr(Rn a, StepZIDS as) { + return D("modr", MemR(a, as)); + } + std::vector<std::string> modr_dmod(Rn a, StepZIDS as) { + return D("modr", MemR(a, as), "dmod"); + } + std::vector<std::string> modr_i2(Rn a) { + return D("modr", R(a), "+2"); + } + std::vector<std::string> modr_i2_dmod(Rn a) { + return D("modr", R(a), "+2", "dmod"); + } + std::vector<std::string> modr_d2(Rn a) { + return D("modr", R(a), "-2"); + } + std::vector<std::string> modr_d2_dmod(Rn a) { + return D("modr", R(a), "-2", "dmod"); + } + std::vector<std::string> modr_eemod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + return D("modr", MemARPSI(a, asi), MemARPSJ(a, asj), "eemod"); + } + std::vector<std::string> modr_edmod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + return D("modr", MemARPSI(a, asi), MemARPSJ(a, asj), "edmod"); + } + std::vector<std::string> modr_demod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + return D("modr", MemARPSI(a, asi), MemARPSJ(a, asj), "demod"); + } + std::vector<std::string> modr_ddmod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + return D("modr", MemARPSI(a, asi), MemARPSJ(a, asj), "ddmod"); + } + + std::vector<std::string> movd(R0123 a, StepZIDS as, R45 b, StepZIDS bs) { + return D("mov d->p", MemR(a, as), MemR(b, bs)); + } + std::vector<std::string> movp(Axl a, Register b) { + return D("mov p->r", MemG(a), R(b)); + } + std::vector<std::string> movp(Ax a, Register b) { + return D("mov p->r", MemG(a), R(b)); + } + std::vector<std::string> movp(Rn a, StepZIDS as, R0123 b, StepZIDS bs) { + return D("mov p->d", MemR(a, as), MemR(b, bs)); + } + std::vector<std::string> movpdw(Ax a) { + return D("mov p->pc", MemG(a)); + } + + std::vector<std::string> mov(Ab a, Ab b) { + return D("mov", R(a), R(b)); + } + std::vector<std::string> mov_dvm(Abl a) { + return D("mov", R(a), "dvm"); + } + std::vector<std::string> mov_x0(Abl a) { + return D("mov", R(a), "x0"); + } + std::vector<std::string> mov_x1(Abl a) { + return D("mov", R(a), "x1"); + } + std::vector<std::string> mov_y1(Abl a) { + return D("mov", R(a), "y1"); + } + std::vector<std::string> mov(Ablh a, MemImm8 b) { + return D("mov", R(a), b); + } + std::vector<std::string> mov(Axl a, MemImm16 b) { + return D("mov", R(a), b); + } + std::vector<std::string> mov(Axl a, MemR7Imm16 b) { + return D("mov", R(a), b); + } + std::vector<std::string> mov(Axl a, MemR7Imm7s b) { + return D("mov", R(a), b); + } + std::vector<std::string> mov(MemImm16 a, Ax b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov(MemImm8 a, Ab b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov(MemImm8 a, Ablh b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov_eu(MemImm8 a, Axh b) { + return D("mov", a, R(b), "eu"); + } + std::vector<std::string> mov(MemImm8 a, RnOld b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov_sv(MemImm8 a) { + return D("mov", a, "sv"); + } + std::vector<std::string> mov_dvm_to(Ab b) { + return D("mov", "dvm", R(b)); + } + std::vector<std::string> mov_icr_to(Ab b) { + return D("mov", "icr", R(b)); + } + std::vector<std::string> mov(Imm16 a, Bx b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov(Imm16 a, Register b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov_icr(Imm5 a) { + return D("mov", a, "icr"); + } + std::vector<std::string> mov(Imm8s a, Axh b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov(Imm8s a, RnOld b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov_sv(Imm8s a) { + return D("mov", a, "sv"); + } + std::vector<std::string> mov(Imm8 a, Axl b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov(MemR7Imm16 a, Ax b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov(MemR7Imm7s a, Ax b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov(Rn a, StepZIDS as, Bx b) { + return D("mov", MemR(a, as), R(b)); + } + std::vector<std::string> mov(Rn a, StepZIDS as, Register b) { + return D("mov", MemR(a, as), R(b)); + } + std::vector<std::string> mov_memsp_to(Register b) { + return D("mov", "[sp]", R(b)); + } + std::vector<std::string> mov_mixp_to(Register b) { + return D("mov", "mixp", R(b)); + } + std::vector<std::string> mov(RnOld a, MemImm8 b) { + return D("mov", R(a), b); + } + std::vector<std::string> mov_icr(Register a) { + return D("mov", R(a), "icr"); + } + std::vector<std::string> mov_mixp(Register a) { + return D("mov", R(a), "mixp"); + } + std::vector<std::string> mov(Register a, Rn b, StepZIDS bs) { + return D("mov", R(a), MemR(b, bs)); + } + std::vector<std::string> mov(Register a, Bx b) { + std::string a_mark; + if (a.GetName() == RegName::a0 || a.GetName() == RegName::a1) { + a_mark = "?"; + } + return D("mov" + a_mark, R(a), R(b)); + } + std::vector<std::string> mov(Register a, Register b) { + return D("mov", R(a), R(b)); + } + std::vector<std::string> mov_repc_to(Ab b) { + return D("mov", "repc", R(b)); + } + std::vector<std::string> mov_sv_to(MemImm8 b) { + return D("mov", "sv", b); + } + std::vector<std::string> mov_x0_to(Ab b) { + return D("mov", "x0", R(b)); + } + std::vector<std::string> mov_x1_to(Ab b) { + return D("mov", "x1", R(b)); + } + std::vector<std::string> mov_y1_to(Ab b) { + return D("mov", "y1", R(b)); + } + std::vector<std::string> mov(Imm16 a, ArArp b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov_r6(Imm16 a) { + return D("mov", a, "r6"); + } + std::vector<std::string> mov_repc(Imm16 a) { + return D("mov", a, "repc"); + } + std::vector<std::string> mov_stepi0(Imm16 a) { + return D("mov", a, "stepi0"); + } + std::vector<std::string> mov_stepj0(Imm16 a) { + return D("mov", a, "stepj0"); + } + std::vector<std::string> mov(Imm16 a, SttMod b) { + return D("mov", a, R(b)); + } + std::vector<std::string> mov_prpage(Imm4 a) { + return D("mov", a, "prpage"); + } + + std::vector<std::string> mov_a0h_stepi0() { + return D("mov", "a0h", "stepi0"); + } + std::vector<std::string> mov_a0h_stepj0() { + return D("mov", "a0h", "stepj0"); + } + std::vector<std::string> mov_stepi0_a0h() { + return D("mov", "stepi0", "a0h"); + } + std::vector<std::string> mov_stepj0_a0h() { + return D("mov", "stepj0", "a0h"); + } + + std::vector<std::string> mov_prpage(Abl a) { + return D("mov", R(a), "prpage"); + } + std::vector<std::string> mov_repc(Abl a) { + return D("mov", R(a), "repc"); + } + std::vector<std::string> mov(Abl a, ArArp b) { + return D("mov", R(a), R(b)); + } + std::vector<std::string> mov(Abl a, SttMod b) { + return D("mov", R(a), R(b)); + } + + std::vector<std::string> mov_prpage_to(Abl b) { + return D("mov", "prpage", R(b)); + } + std::vector<std::string> mov_repc_to(Abl b) { + return D("mov", "repc", R(b)); + } + std::vector<std::string> mov(ArArp a, Abl b) { + return D("mov", R(a), R(b)); + } + std::vector<std::string> mov(SttMod a, Abl b) { + return D("mov", R(a), R(b)); + } + + std::vector<std::string> mov_repc_to(ArRn1 b, ArStep1 bs) { + return D("mov", "repc", MemARS(b, bs)); + } + std::vector<std::string> mov(ArArp a, ArRn1 b, ArStep1 bs) { + return D("mov", R(a), MemARS(b, bs)); + } + std::vector<std::string> mov(SttMod a, ArRn1 b, ArStep1 bs) { + return D("mov", R(a), MemARS(b, bs)); + } + + std::vector<std::string> mov_repc(ArRn1 a, ArStep1 as) { + return D("mov", MemARS(a, as), "repc"); + } + std::vector<std::string> mov(ArRn1 a, ArStep1 as, ArArp b) { + return D("mov", MemARS(a, as), R(b)); + } + std::vector<std::string> mov(ArRn1 a, ArStep1 as, SttMod b) { + return D("mov", MemARS(a, as), R(b)); + } + + std::vector<std::string> mov_repc_to(MemR7Imm16 b) { + return D("mov", "repc", b); + } + std::vector<std::string> mov(ArArpSttMod a, MemR7Imm16 b) { + return D("mov", R(a), b); + } + + std::vector<std::string> mov_repc(MemR7Imm16 a) { + return D("mov", a, "repc"); + } + std::vector<std::string> mov(MemR7Imm16 a, ArArpSttMod b) { + return D("mov", a, R(b)); + } + + std::vector<std::string> mov_pc(Ax a) { + return D("mov", R(a), "pc"); + } + std::vector<std::string> mov_pc(Bx a) { + return D("mov", R(a), "pc"); + } + + std::vector<std::string> mov_mixp_to(Bx b) { + return D("mov", "mixp", R(b)); + } + std::vector<std::string> mov_mixp_r6() { + return D("mov", "mixp", "r6"); + } + std::vector<std::string> mov_p0h_to(Bx b) { + return D("mov", "p0h", R(b)); + } + std::vector<std::string> mov_p0h_r6() { + return D("mov", "p0h", "r6"); + } + std::vector<std::string> mov_p0h_to(Register b) { + return D("mov", "p0h", R(b)); + } + std::vector<std::string> mov_p0(Ab a) { + return D("mov", R(a), "p0"); + } + std::vector<std::string> mov_p1_to(Ab b) { + return D("mov", "p1", R(b)); + } + + std::vector<std::string> mov2(Px a, ArRn2 b, ArStep2 bs) { + return D("mov", R(a), MemARS(b, bs)); + } + std::vector<std::string> mov2s(Px a, ArRn2 b, ArStep2 bs) { + return D("mov s", R(a), MemARS(b, bs)); + } + std::vector<std::string> mov2(ArRn2 a, ArStep2 as, Px b) { + return D("mov", MemARS(a, as), R(b)); + } + std::vector<std::string> mova(Ab a, ArRn2 b, ArStep2 bs) { + return D("mov", R(a), MemARS(b, bs)); + } + std::vector<std::string> mova(ArRn2 a, ArStep2 as, Ab b) { + return D("mov", MemARS(a, as), R(b)); + } + + std::vector<std::string> mov_r6_to(Bx b) { + return D("mov", "r6", R(b)); + } + std::vector<std::string> mov_r6_mixp() { + return D("mov", "r6", "mixp"); + } + std::vector<std::string> mov_r6_to(Register b) { + return D("mov", "r6", R(b)); + } + std::vector<std::string> mov_r6(Register a) { + return D("mov", R(a), "r6"); + } + std::vector<std::string> mov_memsp_r6() { + return D("mov", "[sp]", "r6"); + } + std::vector<std::string> mov_r6_to(Rn b, StepZIDS bs) { + return D("mov", "r6", MemR(b, bs)); + } + std::vector<std::string> mov_r6(Rn a, StepZIDS as) { + return D("mov", MemR(a, as), "r6"); + } + + std::vector<std::string> movs(MemImm8 a, Ab b) { + return D("movs", a, R(b)); + } + std::vector<std::string> movs(Rn a, StepZIDS as, Ab b) { + return D("movs", MemR(a, as), R(b)); + } + std::vector<std::string> movs(Register a, Ab b) { + return D("movs", R(a), R(b)); + } + std::vector<std::string> movs_r6_to(Ax b) { + return D("movs", "r6", R(b)); + } + std::vector<std::string> movsi(RnOld a, Ab b, Imm5s s) { + return D("movsi", R(a), R(b), s); + } + + std::vector<std::string> mov2_axh_m_y0_m(Axh a, ArRn2 b, ArStep2 bs) { + return D("mov||mov", R(a), "y0", MemARS(b, bs)); + } + std::vector<std::string> mov2_ax_mij(Ab a, ArpRn1 b, ArpStep1 bsi, ArpStep1 bsj) { + return D("mov hilj", R(a), MemARPSI(b, bsi), MemARPSJ(b, bsj)); + } + std::vector<std::string> mov2_ax_mji(Ab a, ArpRn1 b, ArpStep1 bsi, ArpStep1 bsj) { + return D("mov lihj", R(a), MemARPSI(b, bsi), MemARPSJ(b, bsj)); + } + std::vector<std::string> mov2_mij_ax(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return D("mov hilj", MemARPSI(a, asi), MemARPSJ(a, asj), R(b)); + } + std::vector<std::string> mov2_mji_ax(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return D("mov lihj", MemARPSI(a, asi), MemARPSJ(a, asj), R(b)); + } + std::vector<std::string> mov2_abh_m(Abh ax, Abh ay, ArRn1 b, ArStep1 bs) { + return D("mov||mov", R(ax), R(ay), MemARS(b, bs)); + } + std::vector<std::string> exchange_iaj(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + return D("exchange i->a->j", R(a), MemARPSI(b, bsi), MemARPSJ(b, bsj)); + } + std::vector<std::string> exchange_riaj(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + return D("exchange ri->a->j", R(a), MemARPSI(b, bsi), MemARPSJ(b, bsj)); + } + std::vector<std::string> exchange_jai(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + return D("exchange j->a->i", R(a), MemARPSI(b, bsi), MemARPSJ(b, bsj)); + } + std::vector<std::string> exchange_rjai(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + return D("exchange rj->a->i", R(a), MemARPSI(b, bsi), MemARPSJ(b, bsj)); + } + + std::vector<std::string> movr(ArRn2 a, ArStep2 as, Abh b) { + return D("movr", MemARS(a, as), R(b)); + } + std::vector<std::string> movr(Rn a, StepZIDS as, Ax b) { + return D("movr", MemR(a, as), R(b)); + } + std::vector<std::string> movr(Register a, Ax b) { + return D("movr", R(a), R(b)); + } + std::vector<std::string> movr(Bx a, Ax b) { + return D("movr", R(a), R(b)); + } + std::vector<std::string> movr_r6_to(Ax b) { + return D("movr", "r6", R(b)); + } + + std::vector<std::string> exp(Bx a) { + return D("exp", R(a)); + } + std::vector<std::string> exp(Bx a, Ax b) { + return D("exp", R(a), R(b)); + } + std::vector<std::string> exp(Rn a, StepZIDS as) { + return D("exp", MemR(a, as)); + } + std::vector<std::string> exp(Rn a, StepZIDS as, Ax b) { + return D("exp", MemR(a, as), R(b)); + } + std::vector<std::string> exp(Register a) { + return D("exp", R(a)); + } + std::vector<std::string> exp(Register a, Ax b) { + return D("exp", R(a), R(b)); + } + std::vector<std::string> exp_r6() { + return D("exp", "r6"); + } + std::vector<std::string> exp_r6(Ax b) { + return D("exp", "r6", R(b)); + } + + std::vector<std::string> lim(Ax a, Ax b) { + return D("lim", R(a), R(b)); + } + + std::vector<std::string> vtrclr0() { + return D("vtrclr0"); + } + std::vector<std::string> vtrclr1() { + return D("vtrclr1"); + } + std::vector<std::string> vtrclr() { + return D("vtrclr"); + } + std::vector<std::string> vtrmov0(Axl a) { + return D("vtrmov0", R(a)); + } + std::vector<std::string> vtrmov1(Axl a) { + return D("vtrmov1", R(a)); + } + std::vector<std::string> vtrmov(Axl a) { + return D("vtrmov", R(a)); + } + std::vector<std::string> vtrshr() { + return D("vtrshr"); + } + + std::vector<std::string> clrp0() { + return D("clr0"); + } + std::vector<std::string> clrp1() { + return D("clr1"); + } + std::vector<std::string> clrp() { + return D("clr"); + } + + std::vector<std::string> max_ge(Ax a, StepZIDS bs) { + return D("max_ge", R(a), "^", "r0", bs); + } + std::vector<std::string> max_gt(Ax a, StepZIDS bs) { + return D("max_gt", R(a), "^", "r0", bs); + } + std::vector<std::string> min_le(Ax a, StepZIDS bs) { + return D("min_le", R(a), "^", "r0", bs); + } + std::vector<std::string> min_lt(Ax a, StepZIDS bs) { + return D("min_lt", R(a), "^", "r0", bs); + } + + std::vector<std::string> max_ge_r0(Ax a, StepZIDS bs) { + return D("max_ge", R(a), "[r0]", bs); + } + std::vector<std::string> max_gt_r0(Ax a, StepZIDS bs) { + return D("max_gt", R(a), "[r0]", bs); + } + std::vector<std::string> min_le_r0(Ax a, StepZIDS bs) { + return D("min_le", R(a), "[r0]", bs); + } + std::vector<std::string> min_lt_r0(Ax a, StepZIDS bs) { + return D("min_lt", R(a), "[r0]", bs); + } + std::vector<std::string> divs(MemImm8 a, Ax b) { + return D("divs", a, R(b)); + } + + std::vector<std::string> sqr_sqr_add3(Ab a, Ab b) { + return D("sqr h||l", R(a), "||add3", R(b)); + } + std::vector<std::string> sqr_sqr_add3(ArRn2 a, ArStep2 as, Ab b) { + return D("sqr h||l", MemARS(a, as), "||add3", R(b)); + } + std::vector<std::string> sqr_mpysu_add3a(Ab a, Ab b) { + return D("sqr h||mpysu hl", R(a), "||add3a", R(b)); + } + + std::vector<std::string> cmp(Ax a, Bx b) { + return D("cmp", R(a), R(b)); + } + std::vector<std::string> cmp_b0_b1() { + return D("cmp", "b0", "b1"); + } + std::vector<std::string> cmp_b1_b0() { + return D("cmp", "b1", "b0"); + } + std::vector<std::string> cmp(Bx a, Ax b) { + return D("cmp", R(a), R(b)); + } + std::vector<std::string> cmp_p1_to(Ax b) { + return D("cmp", "p1", R(b)); + } + + std::vector<std::string> max2_vtr(Ax a) { + return D("max h||l", R(a), "||vtrshr"); + } + std::vector<std::string> min2_vtr(Ax a) { + return D("min h||l", R(a), "||vtrshr"); + } + std::vector<std::string> max2_vtr(Ax a, Bx b) { + return D("max h||l", R(a), R(b), "||vtrshr"); + } + std::vector<std::string> min2_vtr(Ax a, Bx b) { + return D("min h||l", R(a), R(b), "||vtrshr"); + } + std::vector<std::string> max2_vtr_movl(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + return D("max h||l", R(a), R(b), "||vtrshr", "||mov^l", R(a), MemARS(c, cs)); + } + std::vector<std::string> max2_vtr_movh(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + return D("max h||l", R(a), R(b), "||vtrshr", "||mov^h", R(a), MemARS(c, cs)); + } + std::vector<std::string> max2_vtr_movl(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + return D("max h||l", R(a), R(b), "||vtrshr", "||mov^l", R(a), MemARS(c, cs)); + } + std::vector<std::string> max2_vtr_movh(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + return D("max h||l", R(a), R(b), "||vtrshr", "||mov^h", R(a), MemARS(c, cs)); + } + std::vector<std::string> min2_vtr_movl(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + return D("min h||l", R(a), R(b), "||vtrshr", "||mov^l", R(a), MemARS(c, cs)); + } + std::vector<std::string> min2_vtr_movh(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + return D("min h||l", R(a), R(b), "||vtrshr", "||mov^h", R(a), MemARS(c, cs)); + } + std::vector<std::string> min2_vtr_movl(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + return D("min h||l", R(a), R(b), "||vtrshr", "||mov^l", R(a), MemARS(c, cs)); + } + std::vector<std::string> min2_vtr_movh(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + return D("min h||l", R(a), R(b), "||vtrshr", "||mov^h", R(a), MemARS(c, cs)); + } + std::vector<std::string> max2_vtr_movij(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + return D("max h||l", R(a), R(b), "||vtrshr", "||mov^hilj", R(a), MemARPSI(c, csi), + MemARPSJ(c, csj)); + } + std::vector<std::string> max2_vtr_movji(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + return D("max h||l", R(a), R(b), "||vtrshr", "||mov^hjli", R(a), MemARPSI(c, csi), + MemARPSJ(c, csj)); + } + std::vector<std::string> min2_vtr_movij(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + return D("min h||l", R(a), R(b), "||vtrshr", "||mov^hilj", R(a), MemARPSI(c, csi), + MemARPSJ(c, csj)); + } + std::vector<std::string> min2_vtr_movji(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + return D("min h||l", R(a), R(b), "||vtrshr", "||mov^hjli", R(a), MemARPSI(c, csi), + MemARPSJ(c, csj)); + } + + template <typename ArpStepX> + std::vector<std::string> mov_sv_app(ArRn1 a, ArpStepX as, Bx b, SumBase base, bool sub_p0, + bool p0_align, bool sub_p1, bool p1_align) { + return D("mov", MemARS(a, as), "sv", PA(base, sub_p0, p0_align, sub_p1, p1_align), R(b)); + } + + std::vector<std::string> cbs(Axh a, CbsCond c) { + return D("cbs", R(a), "r0", c); + } + std::vector<std::string> cbs(Axh a, Bxh b, CbsCond c) { + return D("cbs", R(a), R(b), "r0", c); + } + std::vector<std::string> cbs(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, CbsCond c) { + return D("cbs", MemARPSI(a, asi), MemARPSJ(a, asj), c); + } + + std::vector<std::string> mma(RegName a, bool x0_sign, bool y0_sign, bool x1_sign, bool y1_sign, + SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + return D("x0<->x1", PA(base, sub_p0, p0_align, sub_p1, p1_align), DsmReg(a), + Mul(x0_sign, y0_sign), Mul(x1_sign, y1_sign)); + } + + template <typename ArpRnX, typename ArpStepX> + std::vector<std::string> mma(ArpRnX xy, ArpStepX i, ArpStepX j, bool dmodi, bool dmodj, + RegName a, bool x0_sign, bool y0_sign, bool x1_sign, bool y1_sign, + SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + return D("xy<-", MemARPSI(xy, i), MemARPSJ(xy, j), + PA(base, sub_p0, p0_align, sub_p1, p1_align), DsmReg(a), Mul(x0_sign, y0_sign), + Mul(x1_sign, y1_sign), dmodi ? "dmodi" : "", dmodj ? "dmodj" : ""); + } + + std::vector<std::string> mma_mx_xy(ArRn1 y, ArStep1 ys, RegName a, bool x0_sign, bool y0_sign, + bool x1_sign, bool y1_sign, SumBase base, bool sub_p0, + bool p0_align, bool sub_p1, bool p1_align) { + return D("x0<->x1, y0<-", MemARS(y, ys), PA(base, sub_p0, p0_align, sub_p1, p1_align), + DsmReg(a), Mul(x0_sign, y0_sign), Mul(x1_sign, y1_sign)); + } + + std::vector<std::string> mma_xy_mx(ArRn1 y, ArStep1 ys, RegName a, bool x0_sign, bool y0_sign, + bool x1_sign, bool y1_sign, SumBase base, bool sub_p0, + bool p0_align, bool sub_p1, bool p1_align) { + return D("x0<->x1, y1<-", MemARS(y, ys), PA(base, sub_p0, p0_align, sub_p1, p1_align), + DsmReg(a), Mul(x0_sign, y0_sign), Mul(x1_sign, y1_sign)); + } + + std::vector<std::string> mma_my_my(ArRn1 x, ArStep1 xs, RegName a, bool x0_sign, bool y0_sign, + bool x1_sign, bool y1_sign, SumBase base, bool sub_p0, + bool p0_align, bool sub_p1, bool p1_align) { + return D("x<-", MemARS(x, xs), PA(base, sub_p0, p0_align, sub_p1, p1_align), DsmReg(a), + Mul(x0_sign, y0_sign), Mul(x1_sign, y1_sign)); + } + + std::vector<std::string> mma_mov(Axh u, Bxh v, ArRn1 w, ArStep1 ws, RegName a, bool x0_sign, + bool y0_sign, bool x1_sign, bool y1_sign, SumBase base, + bool sub_p0, bool p0_align, bool sub_p1, bool p1_align) { + return D("mov", R(u), R(v), MemARS(w, ws), "x0<->x1", + PA(base, sub_p0, p0_align, sub_p1, p1_align), DsmReg(a), Mul(x0_sign, y0_sign), + Mul(x1_sign, y1_sign)); + } + + std::vector<std::string> mma_mov(ArRn2 w, ArStep1 ws, RegName a, bool x0_sign, bool y0_sign, + bool x1_sign, bool y1_sign, SumBase base, bool sub_p0, + bool p0_align, bool sub_p1, bool p1_align) { + return D("mov,^", DsmReg(a), MemARS(w, ws), "x0<->x1", + PA(base, sub_p0, p0_align, sub_p1, p1_align), DsmReg(a), Mul(x0_sign, y0_sign), + Mul(x1_sign, y1_sign)); + } + + std::vector<std::string> addhp(ArRn2 a, ArStep2 as, Px b, Ax c) { + return D("addhp", MemARS(a, as), R(b), R(c)); + } + + std::vector<std::string> mov_ext0(Imm8s a) { + return D("mov", a, "ext0"); + } + std::vector<std::string> mov_ext1(Imm8s a) { + return D("mov", a, "ext1"); + } + std::vector<std::string> mov_ext2(Imm8s a) { + return D("mov", a, "ext2"); + } + std::vector<std::string> mov_ext3(Imm8s a) { + return D("mov", a, "ext3"); + } + + void SetArArp(std::optional<ArArpSettings> ar_arp) { + this->ar_arp = ar_arp; + } + +private: + template <typename ArRn> + std::string DsmArRn(ArRn a) { + if (ar_arp) { + return "%r" + + std::to_string((ar_arp->ar[a.Index() / 2] >> (13 - 3 * (a.Index() % 2))) & 7); + } + return "arrn" + std::to_string(a.Index()); + } + + std::string ConvertArStepAndOffset(u16 v) { + static const std::array<std::string, 8> step_names{{ + "++0", + "++1", + "--1", + "++s", + "++2", + "--2", + "++2*", + "--2*", + }}; + + static const std::array<std::string, 4> offset_names{{ + "+0", + "+1", + "-1", + "-1*", + }}; + + return offset_names[v >> 3] + step_names[v & 7]; + } + + template <typename ArStep> + std::string DsmArStep(ArStep a) { + if (ar_arp) { + u16 s = (ar_arp->ar[a.Index() / 2] >> (5 - 5 * (a.Index() % 2))) & 31; + return ConvertArStepAndOffset(s); + } + return "+ars" + std::to_string(a.Index()); + } + + template <typename ArpRn> + std::string DsmArpRni(ArpRn a) { + if (ar_arp) { + return "%r" + std::to_string((ar_arp->arp[a.Index()] >> 10) & 3); + } + return "arprni" + std::to_string(a.Index()); + } + + template <typename ArpStep> + std::string DsmArpStepi(ArpStep a) { + if (ar_arp) { + u16 s = ar_arp->arp[a.Index()] & 31; + return ConvertArStepAndOffset(s); + } + return "+arpsi" + std::to_string(a.Index()); + } + + template <typename ArpRn> + std::string DsmArpRnj(ArpRn a) { + if (ar_arp) { + return "%r" + std::to_string(((ar_arp->arp[a.Index()] >> 13) & 3) + 4); + } + return "arprnj" + std::to_string(a.Index()); + } + + template <typename ArpStep> + std::string DsmArpStepj(ArpStep a) { + if (ar_arp) { + u16 s = (ar_arp->arp[a.Index()] >> 5) & 31; + return ConvertArStepAndOffset(s); + } + return "+arpsj" + std::to_string(a.Index()); + } + + template <typename ArRn, typename ArStep> + std::string MemARS(ArRn reg, ArStep step) { + return "[" + DsmArRn(reg) + DsmArStep(step) + "]"; + } + + template <typename ArpRn, typename ArpStep> + std::string MemARPSI(ArpRn reg, ArpStep step) { + return "[" + DsmArpRni(reg) + DsmArpStepi(step) + "]"; + } + + template <typename ArpRn, typename ArpStep> + std::string MemARPSJ(ArpRn reg, ArpStep step) { + return "[" + DsmArpRnj(reg) + DsmArpStepj(step) + "]"; + } + + template <typename ArRn> + std::string MemAR(ArRn reg) { + return "[" + DsmArRn(reg) + "]"; + } + + std::optional<ArArpSettings> ar_arp; +}; + +bool NeedExpansion(std::uint16_t opcode) { + auto decoder = Decode<Disassembler>(opcode); + return decoder.NeedExpansion(); +} + +std::vector<std::string> GetTokenList(std::uint16_t opcode, std::uint16_t expansion, + std::optional<ArArpSettings> ar_arp) { + Disassembler dsm; + dsm.SetArArp(ar_arp); + auto decoder = Decode<Disassembler>(opcode); + auto v = decoder.call(dsm, opcode, expansion); + return v; +} + +std::string Do(std::uint16_t opcode, std::uint16_t expansion, std::optional<ArArpSettings> ar_arp) { + auto v = GetTokenList(opcode, expansion, ar_arp); + std::string last = v.back(); + std::string result; + v.pop_back(); + for (const auto& s : v) { + result += s + " "; + } + return result + last; +} + +} // namespace Teakra::Disassembler diff --git a/src/teakra/src/disassembler_c.cpp b/src/teakra/src/disassembler_c.cpp new file mode 100644 index 0000000..88c7987 --- /dev/null +++ b/src/teakra/src/disassembler_c.cpp @@ -0,0 +1,23 @@ +#include "teakra/disassembler.h" +#include "teakra/disassembler_c.h" + +extern "C" { + bool Teakra_Disasm_NeedExpansion(uint16_t opcode) { + return Teakra::Disassembler::NeedExpansion(opcode); + } + + size_t Teakra_Disasm_Do(char* dst, size_t dstlen, + uint16_t opcode, uint16_t expansion /*= 0*/) { + std::string r = Teakra::Disassembler::Do(opcode, expansion); + + if (dst) { + size_t i = 0; + for (; i < (dstlen-1) && i < r.length(); ++i) { + dst[i] = r[i]; + } + dst[dstlen-1] = '\0'; + } + + return r.length(); + } +} diff --git a/src/teakra/src/dma.cpp b/src/teakra/src/dma.cpp new file mode 100644 index 0000000..6379e8f --- /dev/null +++ b/src/teakra/src/dma.cpp @@ -0,0 +1,137 @@ +#include <cstdio> +#include <cstring> +#include "ahbm.h" +#include "dma.h" +#include "shared_memory.h" + +namespace Teakra { + +void Dma::Reset() { + enable_channel = 0; + active_channel = 0; + channels = {}; +} + +void Dma::DoDma(u16 channel) { + channels[channel].Start(); + + channels[channel].ahbm_channel = ahbm.GetChannelForDma(channel); + + // TODO: actually Tick this according to global Tick; + while (channels[channel].running) + channels[channel].Tick(*this); + + interrupt_handler(); +} + +void Dma::Channel::Start() { + running = 1; + current_src = addr_src_low | ((u32)addr_src_high << 16); + current_dst = addr_dst_low | ((u32)addr_dst_high << 16); + counter0 = 0; + counter1 = 0; + counter2 = 0; +} + +void Dma::Channel::Tick(Dma& parent) { + static constexpr u32 DataMemoryOffset = 0x20000; + if (dword_mode) { + u32 value = 0; + switch (src_space) { + case 0: { + u32 l = current_src & 0xFFFFFFFE; + u32 h = current_src | 1; + value = parent.shared_memory.ReadWord(DataMemoryOffset + l) | + ((u32)parent.shared_memory.ReadWord(DataMemoryOffset + h) << 16); + break; + } + case 1: + std::printf("Unimplemented MMIO space"); + value = 0; + break; + case 7: + value = parent.ahbm.Read32(ahbm_channel, current_src); + break; + default: + std::printf("Unknown SrcSpace %04X\n", src_space); + } + + switch (dst_space) { + case 0: { + u32 l = current_dst & 0xFFFFFFFE; + u32 h = current_dst | 1; + parent.shared_memory.WriteWord(DataMemoryOffset + l, (u16)value); + parent.shared_memory.WriteWord(DataMemoryOffset + h, (u16)(value >> 16)); + break; + } + case 1: + std::printf("Unimplemented MMIO space"); + value = 0; + break; + case 7: + parent.ahbm.Write32(ahbm_channel, current_dst, value); + break; + default: + std::printf("Unknown DstSpace %04X\n", dst_space); + } + + counter0 += 2; + } else { + u16 value = 0; + switch (src_space) { + case 0: + value = parent.shared_memory.ReadWord(DataMemoryOffset + current_src); + break; + case 1: + std::printf("Unimplemented MMIO space"); + value = 0; + break; + case 7: + value = parent.ahbm.Read16(ahbm_channel, current_src); + break; + default: + std::printf("Unknown SrcSpace %04X\n", src_space); + } + + switch (dst_space) { + case 0: + parent.shared_memory.WriteWord(DataMemoryOffset + current_dst, value); + break; + case 1: + std::printf("Unimplemented MMIO space"); + value = 0; + break; + case 7: + parent.ahbm.Write16(ahbm_channel, current_dst, value); + break; + default: + std::printf("Unknown DstSpace %04X\n", dst_space); + } + + counter0 += 1; + } + + if (counter0 >= size0) { + counter0 = 0; + counter1 += 1; + if (counter1 >= size1) { + counter1 = 0; + counter2 += 1; + if (counter2 >= size2) { + running = 0; + return; + } else { + current_src += src_step2; + current_dst += dst_step2; + } + } else { + current_src += src_step1; + current_dst += dst_step1; + } + } else { + current_src += src_step0; + current_dst += dst_step0; + } +} + +} // namespace Teakra diff --git a/src/teakra/src/dma.h b/src/teakra/src/dma.h new file mode 100644 index 0000000..f5a8112 --- /dev/null +++ b/src/teakra/src/dma.h @@ -0,0 +1,201 @@ +#pragma once +#include <array> +#include <functional> +#include <utility> +#include "common_types.h" + +namespace Teakra { + +struct SharedMemory; +class Ahbm; + +class Dma { +public: + Dma(SharedMemory& shared_memory, Ahbm& ahbm) : shared_memory(shared_memory), ahbm(ahbm) {} + + void Reset(); + + void EnableChannel(u16 value) { + enable_channel = value; + } + u16 GetChannelEnabled() const { + return enable_channel; + } + + void ActivateChannel(u16 value) { + active_channel = value; + } + u16 GetActiveChannel() const { + return active_channel; + } + + void SetAddrSrcLow(u16 value) { + channels[active_channel].addr_src_low = value; + } + u16 GetAddrSrcLow() const { + return channels[active_channel].addr_src_low; + } + + void SetAddrSrcHigh(u16 value) { + channels[active_channel].addr_src_high = value; + } + u16 GetAddrSrcHigh() const { + return channels[active_channel].addr_src_high; + } + + void SetAddrDstLow(u16 value) { + channels[active_channel].addr_dst_low = value; + } + u16 GetAddrDstLow() const { + return channels[active_channel].addr_dst_low; + } + + void SetAddrDstHigh(u16 value) { + channels[active_channel].addr_dst_high = value; + } + u16 GetAddrDstHigh() const { + return channels[active_channel].addr_dst_high; + } + + void SetSize0(u16 value) { + channels[active_channel].size0 = value; + } + u16 GetSize0() const { + return channels[active_channel].size0; + } + + void SetSize1(u16 value) { + channels[active_channel].size1 = value; + } + u16 GetSize1() const { + return channels[active_channel].size1; + } + + void SetSize2(u16 value) { + channels[active_channel].size2 = value; + } + u16 GetSize2() const { + return channels[active_channel].size2; + } + + void SetSrcStep0(u16 value) { + channels[active_channel].src_step0 = value; + } + u16 GetSrcStep0() const { + return channels[active_channel].src_step0; + } + + void SetDstStep0(u16 value) { + channels[active_channel].dst_step0 = value; + } + u16 GetDstStep0() const { + return channels[active_channel].dst_step0; + } + + void SetSrcStep1(u16 value) { + channels[active_channel].src_step1 = value; + } + u16 GetSrcStep1() const { + return channels[active_channel].src_step1; + } + + void SetDstStep1(u16 value) { + channels[active_channel].dst_step1 = value; + } + u16 GetDstStep1() const { + return channels[active_channel].dst_step1; + } + + void SetSrcStep2(u16 value) { + channels[active_channel].src_step2 = value; + } + u16 GetSrcStep2() const { + return channels[active_channel].src_step2; + } + + void SetDstStep2(u16 value) { + channels[active_channel].dst_step2 = value; + } + u16 GetDstStep2() const { + return channels[active_channel].dst_step2; + } + + void SetSrcSpace(u16 value) { + channels[active_channel].src_space = value; + } + u16 GetSrcSpace() const { + return channels[active_channel].src_space; + } + + void SetDstSpace(u16 value) { + channels[active_channel].dst_space = value; + } + u16 GetDstSpace() const { + return channels[active_channel].dst_space; + } + + void SetDwordMode(u16 value) { + channels[active_channel].dword_mode = value; + } + u16 GetDwordMode() const { + return channels[active_channel].dword_mode; + } + + void SetY(u16 value) { + channels[active_channel].y = value; + } + u16 GetY() const { + return channels[active_channel].y; + } + + void SetZ(u16 value) { + channels[active_channel].z = value; + + if (value == 0x40C0) { + DoDma(active_channel); + } + } + u16 GetZ() const { + return channels[active_channel].z; + } + + void DoDma(u16 channel); + + void SetInterruptHandler(std::function<void()> handler) { + interrupt_handler = std::move(handler); + } + +private: + std::function<void()> interrupt_handler; + + u16 enable_channel = 0; + u16 active_channel = 0; + + struct Channel { + u16 addr_src_low = 0, addr_src_high = 0; + u16 addr_dst_low = 0, addr_dst_high = 0; + u16 size0 = 0, size1 = 0, size2 = 0; + u16 src_step0 = 0, dst_step0 = 0; + u16 src_step1 = 0, dst_step1 = 0; + u16 src_step2 = 0, dst_step2 = 0; + u16 src_space = 0, dst_space = 0; + u16 dword_mode = 0; + u16 y = 0; + u16 z = 0; + + u32 current_src = 0, current_dst = 0; + u16 counter0 = 0, counter1 = 0, counter2 = 0; + u16 running = 0; + u16 ahbm_channel = 0; + + void Start(); + void Tick(Dma& parent); + }; + + std::array<Channel, 8> channels; + + SharedMemory& shared_memory; + Ahbm& ahbm; +}; + +} // namespace Teakra diff --git a/src/teakra/src/dma.md b/src/teakra/src/dma.md new file mode 100644 index 0000000..4bf588e --- /dev/null +++ b/src/teakra/src/dma.md @@ -0,0 +1,99 @@ +# DMA + +## MMIO Layout + +``` ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0180 | | | | | | | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0182 | | | | | | | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0184 | | | | | | | | |C7 |C6 |C5 |C4 |C3 |C2 |C1 |C0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0186 | | | | | | | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0188 | | | | | | | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x018A | | | | | | | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x018C | |E7 |E6 |E5 |E4 |E3 |E2 |E1 |E0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x018E | | S3 | | S2 | | S1 | | S0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0190 | | S7 | | S6 | | S5 | | S4 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +C0..C7: enable channel 0..7? +E0..E7: end of transfer flag for channel 0..7? +S0..S7: some sort of slots for channel 0..7? are initialized as value 0..7 + ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01BE | | CHANNEL | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01C0 | SRC_ADDR_LOW | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01C2 | SRC_ADDR_HIGH | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01C4 | DST_ADDR_LOW | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01C6 | DST_ADDR_HIGH | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01C8 | SIZE0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01CA | SIZE1 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01CC | SIZE2 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01CE | SRC_STEP0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01D0 | DST_STEP0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01D2 | SRC_STEP1 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01D4 | DST_STEP1 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01D6 | SRC_STEP2 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01D8 | DST_STEP2 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01DA | | - |DWM| ? | | DST_SPACE | SRC_SPACE | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01DC | - | | | ? | ? | ? | | | | | - | | | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x01DE |RST|STR| | ? | ? | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +CHANNEL: set the channel to bind with register +0x01C0..+0x01DE +SRC_SPACE / DST_SPACE: the memory space associated with SRC_ADDR and DST_ADDR + - 0: DSP main memory + - 1: MMIO + - 5: Program memory (only for DST_SPACE) (untested) + - 7: connect to AHBM / external memory + +``` + +## Data Transfer Engine + +The engine is used for transferring data between memory locations. One transfer session is like copying a three-dimensional array, with configurable strides, from the specified source and destination. `SRC_ADDR` and `DST_ADDR` specify the starting address of data source and destination. `SIZE0`, `SIZE1` and `SIZE2` specify the element count for each dimension, where `SIZE0` specifies the finest dimension in the linear memory space. (`SRC_`/`DST_`)`STEP0`, `STEP1` and `STEP2` specify the address stepping between elements for each dimension. Below is an example showing how it works. + +With the source address configuration: + +``` +SRC_ADDR = 0 +SIZE0 = 3 +SIZE1 = 5 +SIZE2 = 2 +SRC_STEP0 = 2 +SRC_STEP1 = 1 +SRC_STEP2 = 7 +``` +The data transfer copies data from the following addresses, where `,` represents dimension 0 stepping, `/` for dimension 1 and `||` for dimension 2. +``` + 0, 2, 4/ 5, 7, 9/ 10, 12, 14/ 15, 17, 19/ 20, 22, 24|| +31, 33, 35/ 36, 38, 40/ 41, 43, 45/ 46, 48, 50/ 51, 53, 55 +``` +The same rule applies to the destination address as well. Note that `SIZEx` can be 0, in which case they have the same effect as being 1. + +One element can be either a 16-bit(`DWM = 0`) word or a 32-bit double word (`DWM = 1`). In the 32-bit double word mode, the address stepping method is the same as described above, except for `SIZE0`, where one 32-bit element is counted as 2 for the dimension 0 counter, so effectively `SIZE0` value is twice as the actual count of 32-bit elements copied in a dimension 0 stride. The double word mode also automatically aligns down the addresses to 32-bit boundaries. + +When the memory space is specified as 7 (AHBM), it performs data transfer from/to external memory. Take 3DS for example, the external memory is FCRAM, and the address is specified as FCRAM's physical address as-is. Because the external memory has 8-bit addressing, the address resolution mismatch between DSP memory and FCRAM can make it unintuitive. Just keep in mind that the `STEP` value is also added to the address as-is, so `STEP = 1` means stepping 8-bit for FCRAM, while stepping 16-bit for DSP memory. There are also more alignment requirement on the external memory address, but it is handled by AHBM. See [ahbm.md](ahbm.md) for detail. diff --git a/src/teakra/src/dsp1_reader/CMakeLists.txt b/src/teakra/src/dsp1_reader/CMakeLists.txt new file mode 100644 index 0000000..a36450c --- /dev/null +++ b/src/teakra/src/dsp1_reader/CMakeLists.txt @@ -0,0 +1,9 @@ +include(CreateDirectoryGroups) + +add_executable(dsp1_reader + main.cpp +) +create_target_directory_groups(dsp1_reader) +target_link_libraries(dsp1_reader PRIVATE teakra) +target_include_directories(dsp1_reader PRIVATE .) +target_compile_options(dsp1_reader PRIVATE ${TEAKRA_CXX_FLAGS}) diff --git a/src/teakra/src/dsp1_reader/main.cpp b/src/teakra/src/dsp1_reader/main.cpp new file mode 100644 index 0000000..f284c71 --- /dev/null +++ b/src/teakra/src/dsp1_reader/main.cpp @@ -0,0 +1,125 @@ +#include <cstdio> +#include <cstring> +#include <string> +#include <vector> + +#include <teakra/disassembler.h> +#include "../common_types.h" + +class Dsp1 { +public: + Dsp1(std::vector<u8> raw); + + struct Header { + u8 signature[0x100]; + u8 magic[4]; + u32 binary_size; + u16 memory_layout; + u16 padding; + u8 unknown; + u8 filter_segment_type; + u8 num_segments; + u8 flags; + u32 filter_segment_address; + u32 filter_segment_size; + u64 zero; + struct Segment { + u32 offset; + u32 address; + u32 size; + u8 pa, pb, pc; + u8 memory_type; + u8 sha256[0x20]; + } segments[10]; + }; + + static_assert(sizeof(Header) == 0x300); + + struct Segment { + std::vector<u8> data; + u8 memory_type; + u32 target; + }; + + std::vector<Segment> segments; + bool recv_data_on_start; +}; + +Dsp1::Dsp1(std::vector<u8> raw) { + Header header; + std::memcpy(&header, raw.data(), sizeof(header)); + + recv_data_on_start = (header.flags & 1) != 0; + + printf("Memory layout = %04X\n", header.memory_layout); + printf("Unk = %02X\n", header.unknown); + printf("Filter segment type = %d\n", header.filter_segment_type); + printf("Num segments = %d\n", header.num_segments); + printf("Flags = %d\n", header.flags); + printf("Filter address = 2 * %08X\n", header.filter_segment_address); + printf("Filter size = %08X\n", header.filter_segment_size); + + for (u32 i = 0; i < header.num_segments; ++i) { + Segment segment; + segment.data = + std::vector<u8>(raw.begin() + header.segments[i].offset, + raw.begin() + header.segments[i].offset + header.segments[i].size); + segment.memory_type = header.segments[i].memory_type; + segment.target = header.segments[i].address; /*header.segments[i].address * 2 + + (segment.memory_type == 2 ? 0x1FF40000 : 0x1FF00000);*/ + segments.push_back(segment); + + printf("[Segment %d]\n", i); + printf("memory_type = %d\n", segment.memory_type); + printf("target = %08X\n", segment.target); + printf("size = %08X\n", (u32)segment.data.size()); + } +} + +int main(int argc, char** argv) { + if (argc < 3) + return -1; + + FILE* file = fopen(argv[1], "rb"); + std::vector<u8> raw; + u8 ch; + while (fread(&ch, 1, 1, file) == 1) { + raw.push_back(ch); + } + fclose(file); + Dsp1 dsp(raw); + + file = fopen(argv[2], "wt"); + + for (const auto& segment : dsp.segments) { + if (segment.memory_type == 0 || segment.memory_type == 1) { + fprintf(file, "\n>>>>>>>> Segment <<<<<<<<\n\n"); + for (unsigned pos = 0; pos < segment.data.size(); pos += 2) { + u16 opcode = segment.data[pos] | (segment.data[pos + 1] << 8); + fprintf(file, "%08X %04X ", segment.target + pos / 2, opcode); + bool expand = false; + u16 expand_value = 0; + if (Teakra::Disassembler::NeedExpansion(opcode)) { + expand = true; + pos += 2; + expand_value = segment.data[pos] | (segment.data[pos + 1] << 8); + } + std::string result = Teakra::Disassembler::Do(opcode, expand_value); + fprintf(file, "%s\n", result.c_str()); + if (expand) { + fprintf(file, "%08X %04X ^^^\n", segment.target + pos / 2, expand_value); + } + } + } + + if (segment.memory_type == 2) { + fprintf(file, "\n>>>>>>>> Data Segment <<<<<<<<\n\n"); + for (unsigned pos = 0; pos < segment.data.size(); pos += 2) { + u16 opcode = segment.data[pos] | (segment.data[pos + 1] << 8); + fprintf(file, "%08X %04X\n", segment.target + pos / 2, opcode); + } + } + } + + fclose(file); +} diff --git a/src/teakra/src/icu.h b/src/teakra/src/icu.h new file mode 100644 index 0000000..060bb3d --- /dev/null +++ b/src/teakra/src/icu.h @@ -0,0 +1,89 @@ +#pragma once + +#include <array> +#include <bitset> +#include <functional> +#include <mutex> +#include <utility> +#include "common_types.h" + +namespace Teakra { + +class ICU { +public: + using IrqBits = std::bitset<16>; + u16 GetRequest() const { + std::lock_guard lock(mutex); + return (u16)request.to_ulong(); + } + void Acknowledge(u16 irq_bits) { + std::lock_guard lock(mutex); + request &= ~IrqBits(irq_bits); + } + u16 GetAcknowledge() { + return 0; + } + void Trigger(u16 irq_bits) { + std::lock_guard lock(mutex); + IrqBits bits(irq_bits); + request |= bits; + for (u32 irq = 0; irq < 16; ++irq) { + if (bits[irq]) { + for (u32 interrupt = 0; interrupt < enabled.size(); ++interrupt) { + if (enabled[interrupt][irq]) { + on_interrupt(interrupt); + } + } + if (vectored_enabled[irq]) { + on_vectored_interrupt(GetVector(irq), vector_context_switch[irq] != 0); + } + } + } + } + u16 GetTrigger() { + return 0; + } + void TriggerSingle(u32 irq) { + Trigger(1 << irq); + } + void SetEnable(u32 interrupt_index, u16 irq_bits) { + std::lock_guard lock(mutex); + enabled[interrupt_index] = IrqBits(irq_bits); + } + void SetEnableVectored(u16 irq_bits) { + std::lock_guard lock(mutex); + vectored_enabled = IrqBits(irq_bits); + } + u16 GetEnable(u32 interrupt_index) const { + std::lock_guard lock(mutex); + return (u16)enabled[interrupt_index].to_ulong(); + } + u16 GetEnableVectored() const { + std::lock_guard lock(mutex); + return (u16)vectored_enabled.to_ulong(); + } + + u32 GetVector(u32 irq) const { + return vector_low[irq] | ((u32)vector_high[irq] << 16); + } + + void SetInterruptHandler(std::function<void(u32)> interrupt, + std::function<void(u32, bool)> vectored_interrupt) { + on_interrupt = std::move(interrupt); + on_vectored_interrupt = std::move(vectored_interrupt); + } + + std::array<u16, 16> vector_low, vector_high; + std::array<u16, 16> vector_context_switch; + +private: + std::function<void(u32)> on_interrupt; + std::function<void(u32, bool)> on_vectored_interrupt; + + IrqBits request; + std::array<IrqBits, 3> enabled; + IrqBits vectored_enabled; + mutable std::mutex mutex; +}; + +} // namespace Teakra diff --git a/src/teakra/src/icu.md b/src/teakra/src/icu.md new file mode 100644 index 0000000..f29c410 --- /dev/null +++ b/src/teakra/src/icu.md @@ -0,0 +1,70 @@ +# ICU + +## MMIO Layout + +``` ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0200 |IPF|IPE|IPD|IPC|IPB|IPA|IP9|IP8|IP7|IP6|IP5|IP4|IP3|IP2|IP1|IP0| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0202 |IAF|IAE|IAD|IAC|IAB|IAA|IA9|IA8|IA7|IA6|IA5|IA4|IA3|IA2|IA1|IA0| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0204 |ITF|ITE|ITD|ITC|ITB|ITA|IT9|IT8|IT7|IT6|IT5|IT4|IT3|IT2|IT1|IT0| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0206 |I0F|I0E|I0D|I0C|I0B|I0A|I09|I08|I07|I06|I05|I04|I03|I02|I01|I00| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0208 |I1F|I1E|I1D|I1C|I1B|I1A|I19|I18|I17|I16|I15|I14|I13|I12|I11|I10| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x020A |I2F|I2E|I2D|I2C|I2B|I2A|I29|I28|I27|I26|I25|I24|I23|I22|I21|I20| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x020C |IVF|IVE|IVD|IVC|IVB|IVA|IV9|IV8|IV7|IV6|IV5|IV4|IV3|IV2|IV1|IV0| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x020E |TPF|TPE|TPD|TPC|TPB|TPA|TP9|TP8|TP7|TP6|TP5|TP4|TP3|TP2|TP1|TP0| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0210 |PLF|PLE|PLD|PLC|PLB|PLA|PL9|PL8|PL7|PL6|PL5|PL4|PL3|PL2|PL1|PL0| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +N = 0..15 ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0212+N*4|VIC| |VADDR_H| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0214+N*4| VADDR_L | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +IP0..IPF: IRQ pending flag +IA0..IAF: IRQ acknowledge, 1 to clears pending flag +IT0..ITF: triggers IRQ manually +I00..I0F: connects IRQ to core interrupt 0 +I10..I1F: connects IRQ to core interrupt 1 +I20..I2F: connects IRQ to core interrupt 2 +IV0..IVF: connects IRQ to core vectored interrupt +TP0..TPF: IRQ trigger mode? 0: pulse, 1: sticky +PL0..PLF: polarity of IRQ signal? + * I might have got PLx and TPx swapped +VADDR_H, VADDR_L: address of interrupt handler for vectored interrupt +VIC: 1 to enable context switch on vectored interrupt +``` + +## IRQ signal + +All bit fields in MMIO correspond to 16 IRQ signal. Some of them are known to connect to other peripherals as shown below. There might be more undiscovered components that have associated IRQ +``` ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +| | *F| *E| *D| *C| *B| *A| *9| *8| *7| *6| *5| *4| *3| *2| *1| *0| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + | | | | | | | +DMA --------* | | | | | | +APBP ------------* | | | | | +ICU? ----------------* | | | | + --------------------* | | | +BTDMP ------------------------* | | +TIMER0----------------------------* | +TIMER1--------------------------------* +``` + +## IRQ-to-interrupt translator + +The main job of ICU is to translate 16 IRQ signals to 4 processor interrupt signals (int0, int1, int2 and vint), specified by `I0x`, `I1x`, `I2x` and `IVx` registers. When an IRQ is signaled, ICU will generate interrupt signal and the processor starts to execute interrupt handling procedure if the core interrupt is enabled. The procedure is supposed check `IPx` reigster to know the exact IRQ index, and to set `IAx` registers to clear the IRQ signal. + +## Software interrupt + +The processor can writes to `ITx` to generate a software interrupt manually. A use case for this in many 3DS games is that IRQ 0 is used as the reschedule procedure for multithreading. When a thread wants to yield, it triggers IRQ 0, which switches thread context and resume another thread. diff --git a/src/teakra/src/interpreter.h b/src/teakra/src/interpreter.h new file mode 100644 index 0000000..72724ee --- /dev/null +++ b/src/teakra/src/interpreter.h @@ -0,0 +1,3632 @@ +#pragma once +#include <atomic> +#include <stdexcept> +#include <tuple> +#include <type_traits> +#include <unordered_map> +#include <unordered_set> +#include "bit.h" +#include "core_timing.h" +#include "crash.h" +#include "decoder.h" +#include "memory_interface.h" +#include "operand.h" +#include "register.h" + +namespace Teakra { + +class UnimplementedException : public std::runtime_error { +public: + UnimplementedException() : std::runtime_error("unimplemented") {} +}; + +class Interpreter { +public: + Interpreter(CoreTiming& core_timing, RegisterState& regs, MemoryInterface& mem) + : core_timing(core_timing), regs(regs), mem(mem) {} + + void PushPC() { + u16 l = (u16)(regs.pc & 0xFFFF); + u16 h = (u16)(regs.pc >> 16); + if (regs.cpc == 1) { + mem.DataWrite(--regs.sp, h); + mem.DataWrite(--regs.sp, l); + } else { + mem.DataWrite(--regs.sp, l); + mem.DataWrite(--regs.sp, h); + } + } + + void PopPC() { + u16 h, l; + if (regs.cpc == 1) { + l = mem.DataRead(regs.sp++); + h = mem.DataRead(regs.sp++); + } else { + h = mem.DataRead(regs.sp++); + l = mem.DataRead(regs.sp++); + } + SetPC(l | ((u32)h << 16)); + } + + void SetPC(u32 new_pc) { + ASSERT(new_pc < 0x40000); + regs.pc = new_pc; + } + + void undefined(u16 opcode) { + UNREACHABLE(); + } + + void Run(u64 cycles) { + idle = false; + for (u64 i = 0; i < cycles; ++i) { + if (idle) { + u64 skipped = core_timing.Skip(cycles - i - 1); + i += skipped; + + // Skip additional tick so to let components fire interrupts + if (i < cycles - 1) { + ++i; + core_timing.Tick(); + } + } + + for (std::size_t i = 0; i < 3; ++i) { + if (interrupt_pending[i].exchange(false)) { + regs.ip[i] = 1; + } + } + + if (vinterrupt_pending.exchange(false)) { + regs.ipv = 1; + } + + u16 opcode = mem.ProgramRead((regs.pc++) | (regs.prpage << 18)); + auto& decoder = decoders[opcode]; + u16 expand_value = 0; + if (decoder.NeedExpansion()) { + expand_value = mem.ProgramRead((regs.pc++) | (regs.prpage << 18)); + } + + if (regs.rep) { + if (regs.repc == 0) { + regs.rep = false; + } else { + --regs.repc; + --regs.pc; + } + } + + if (regs.lp && regs.bkrep_stack[regs.bcn - 1].end + 1 == regs.pc) { + if (regs.bkrep_stack[regs.bcn - 1].lc == 0) { + --regs.bcn; + regs.lp = regs.bcn != 0; + } else { + --regs.bkrep_stack[regs.bcn - 1].lc; + regs.pc = regs.bkrep_stack[regs.bcn - 1].start; + } + } + + decoder.call(*this, opcode, expand_value); + + // I am not sure if a single-instruction loop is interruptable and how it is handled, + // so just disable interrupt for it for now. + if (regs.ie && !regs.rep) { + bool interrupt_handled = false; + for (u32 i = 0; i < regs.im.size(); ++i) { + if (regs.im[i] && regs.ip[i]) { + regs.ip[i] = 0; + regs.ie = 0; + PushPC(); + regs.pc = 0x0006 + i * 8; + idle = false; + interrupt_handled = true; + if (regs.ic[i]) { + ContextStore(); + } + break; + } + } + if (!interrupt_handled && regs.imv && regs.ipv) { + regs.ipv = 0; + regs.ie = 0; + PushPC(); + regs.pc = vinterrupt_address; + idle = false; + if (vinterrupt_context_switch) { + ContextStore(); + } + } + } + + core_timing.Tick(); + } + } + + void SignalInterrupt(u32 i) { + interrupt_pending[i] = true; + } + void SignalVectoredInterrupt(u32 address, bool context_switch) { + vinterrupt_address = address; + vinterrupt_pending = true; + vinterrupt_context_switch = context_switch; + } + + using instruction_return_type = void; + + void nop() { + // literally nothing + } + + void norm(Ax a, Rn b, StepZIDS bs) { + if (regs.fn == 0) { + u64 value = GetAcc(a.GetName()); + regs.fv = value != SignExtend<39>(value); + if (regs.fv) { + regs.fvl = 1; + } + value <<= 1; + regs.fc0 = (value & ((u64)1 << 40)) != 0; + value = SignExtend<40>(value); + SetAccAndFlag(a.GetName(), value); + u32 unit = b.Index(); + RnAndModify(unit, bs.GetName()); + regs.fr = regs.r[unit] == 0; + } + } + void swap(SwapType swap) { + RegName s0, d0, s1, d1; + u64 u, v; + switch (swap.GetName()) { + case SwapTypeValue::a0b0: + s0 = d1 = RegName::a0; + s1 = d0 = RegName::b0; + break; + case SwapTypeValue::a0b1: + s0 = d1 = RegName::a0; + s1 = d0 = RegName::b1; + break; + case SwapTypeValue::a1b0: + s0 = d1 = RegName::a1; + s1 = d0 = RegName::b0; + break; + case SwapTypeValue::a1b1: + s0 = d1 = RegName::a1; + s1 = d0 = RegName::b1; + break; + case SwapTypeValue::a0b0a1b1: + u = GetAcc(RegName::a1); + v = GetAcc(RegName::b1); + SatAndSetAccAndFlag(RegName::a1, v); + SatAndSetAccAndFlag(RegName::b1, u); + s0 = d1 = RegName::a0; + s1 = d0 = RegName::b0; + break; + case SwapTypeValue::a0b1a1b0: + u = GetAcc(RegName::a1); + v = GetAcc(RegName::b0); + SatAndSetAccAndFlag(RegName::a1, v); + SatAndSetAccAndFlag(RegName::b0, u); + s0 = d1 = RegName::a0; + s1 = d0 = RegName::b1; + break; + case SwapTypeValue::a0b0a1: + s0 = RegName::a0; + d0 = s1 = RegName::b0; + d1 = RegName::a1; + break; + case SwapTypeValue::a0b1a1: + s0 = RegName::a0; + d0 = s1 = RegName::b1; + d1 = RegName::a1; + break; + case SwapTypeValue::a1b0a0: + s0 = RegName::a1; + d0 = s1 = RegName::b0; + d1 = RegName::a0; + break; + case SwapTypeValue::a1b1a0: + s0 = RegName::a1; + d0 = s1 = RegName::b1; + d1 = RegName::a0; + break; + case SwapTypeValue::b0a0b1: + s0 = d1 = RegName::a0; + d0 = RegName::b1; + s1 = RegName::b0; + break; + case SwapTypeValue::b0a1b1: + s0 = d1 = RegName::a1; + d0 = RegName::b1; + s1 = RegName::b0; + break; + case SwapTypeValue::b1a0b0: + s0 = d1 = RegName::a0; + d0 = RegName::b0; + s1 = RegName::b1; + break; + case SwapTypeValue::b1a1b0: + s0 = d1 = RegName::a1; + d0 = RegName::b0; + s1 = RegName::b1; + break; + default: + UNREACHABLE(); + } + u = GetAcc(s0); + v = GetAcc(s1); + SatAndSetAccAndFlag(d0, u); + SatAndSetAccAndFlag(d1, v); // only this one affects flags (except for fl) + } + void trap() { + throw UnimplementedException(); + } + + void DoMultiplication(u32 unit, bool x_sign, bool y_sign) { + u32 x = regs.x[unit]; + u32 y = regs.y[unit]; + if (regs.hwm == 1 || (regs.hwm == 3 && unit == 0)) { + y >>= 8; + } else if (regs.hwm == 2 || (regs.hwm == 3 && unit == 1)) { + y &= 0xFF; + } + if (x_sign) + x = SignExtend<16>(x); + if (y_sign) + y = SignExtend<16>(y); + regs.p[unit] = x * y; + if (x_sign || y_sign) + regs.pe[unit] = regs.p[unit] >> 31; + else + regs.pe[unit] = 0; + } + + u64 AddSub(u64 a, u64 b, bool sub) { + a &= 0xFF'FFFF'FFFF; + b &= 0xFF'FFFF'FFFF; + u64 result = sub ? a - b : a + b; + regs.fc0 = (result >> 40) & 1; + if (sub) + b = ~b; + regs.fv = ((~(a ^ b) & (a ^ result)) >> 39) & 1; + if (regs.fv) { + regs.fvl = 1; + } + return SignExtend<40>(result); + } + + void ProductSum(SumBase base, RegName acc, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + u64 value_a = ProductToBus40(Px{0}); + u64 value_b = ProductToBus40(Px{1}); + if (p0_align) { + value_a = SignExtend<24>(value_a >> 16); + } + if (p1_align) { + value_b = SignExtend<24>(value_b >> 16); + } + u64 value_c; + switch (base) { + case SumBase::Zero: + value_c = 0; + break; + case SumBase::Acc: + value_c = GetAcc(acc); + break; + case SumBase::Sv: + value_c = SignExtend<32, u64>((u64)regs.sv << 16); + break; + case SumBase::SvRnd: + value_c = SignExtend<32, u64>((u64)regs.sv << 16) | 0x8000; + break; + default: + UNREACHABLE(); + } + u64 result = AddSub(value_c, value_a, sub_p0); + u16 temp_c = regs.fc0; + u16 temp_v = regs.fv; + result = AddSub(result, value_b, sub_p1); + // Is this correct? + if (sub_p0 == sub_p1) { + regs.fc0 |= temp_c; + regs.fv |= temp_v; + } else { + regs.fc0 ^= temp_c; + regs.fv ^= temp_v; + } + SatAndSetAccAndFlag(acc, result); + } + + void AlmGeneric(AlmOp op, u64 a, Ax b) { + switch (op) { + case AlmOp::Or: { + u64 value = GetAcc(b.GetName()); + value |= a; + value = SignExtend<40>(value); + SetAccAndFlag(b.GetName(), value); + break; + } + case AlmOp::And: { + u64 value = GetAcc(b.GetName()); + value &= a; + value = SignExtend<40>(value); + SetAccAndFlag(b.GetName(), value); + break; + } + case AlmOp::Xor: { + u64 value = GetAcc(b.GetName()); + value ^= a; + value = SignExtend<40>(value); + SetAccAndFlag(b.GetName(), value); + break; + } + case AlmOp::Tst0: { + u64 value = GetAcc(b.GetName()) & 0xFFFF; + regs.fz = (value & a) == 0; + break; + } + case AlmOp::Tst1: { + u64 value = GetAcc(b.GetName()) & 0xFFFF; + regs.fz = (value & ~a) == 0; + break; + } + case AlmOp::Cmp: + case AlmOp::Cmpu: + case AlmOp::Sub: + case AlmOp::Subl: + case AlmOp::Subh: + case AlmOp::Add: + case AlmOp::Addl: + case AlmOp::Addh: { + u64 value = GetAcc(b.GetName()); + bool sub = !(op == AlmOp::Add || op == AlmOp::Addl || op == AlmOp::Addh); + u64 result = AddSub(value, a, sub); + if (op == AlmOp::Cmp || op == AlmOp::Cmpu) { + SetAccFlag(result); + } else { + SatAndSetAccAndFlag(b.GetName(), result); + } + break; + } + case AlmOp::Msu: { + u64 value = GetAcc(b.GetName()); + u64 product = ProductToBus40(Px{0}); + u64 result = AddSub(value, product, true); + SatAndSetAccAndFlag(b.GetName(), result); + + regs.x[0] = a & 0xFFFF; + DoMultiplication(0, true, true); + break; + } + case AlmOp::Sqra: { + u64 value = GetAcc(b.GetName()); + u64 product = ProductToBus40(Px{0}); + u64 result = AddSub(value, product, false); + SatAndSetAccAndFlag(b.GetName(), result); + } + [[fallthrough]]; + case AlmOp::Sqr: { + regs.y[0] = regs.x[0] = a & 0xFFFF; + DoMultiplication(0, true, true); + break; + } + + default: + UNREACHABLE(); + } + } + + u64 ExtendOperandForAlm(AlmOp op, u16 a) { + switch (op) { + case AlmOp::Cmp: + case AlmOp::Sub: + case AlmOp::Add: + return SignExtend<16, u64>(a); + case AlmOp::Addh: + case AlmOp::Subh: + return SignExtend<32, u64>((u64)a << 16); + default: + return a; + } + } + + void alm(Alm op, MemImm8 a, Ax b) { + u16 value = LoadFromMemory(a); + AlmGeneric(op.GetName(), ExtendOperandForAlm(op.GetName(), value), b); + } + void alm(Alm op, Rn a, StepZIDS as, Ax b) { + u16 address = RnAddressAndModify(a.Index(), as.GetName()); + u16 value = mem.DataRead(address); + AlmGeneric(op.GetName(), ExtendOperandForAlm(op.GetName(), value), b); + } + void alm(Alm op, Register a, Ax b) { + u64 value; + auto CheckBus40OperandAllowed = [op] { + static const std::unordered_set<AlmOp> allowed_instruction{ + AlmOp::Or, AlmOp::And, AlmOp::Xor, AlmOp::Add, AlmOp::Cmp, AlmOp::Sub, + }; + if (allowed_instruction.count(op.GetName()) == 0) + throw UnimplementedException(); // weird effect. probably undefined + }; + switch (a.GetName()) { + // need more test + case RegName::p: + CheckBus40OperandAllowed(); + value = ProductToBus40(Px{0}); + break; + case RegName::a0: + case RegName::a1: + CheckBus40OperandAllowed(); + value = GetAcc(a.GetName()); + break; + default: + value = ExtendOperandForAlm(op.GetName(), RegToBus16(a.GetName())); + break; + } + AlmGeneric(op.GetName(), value, b); + } + void alm_r6(Alm op, Ax b) { + u16 value = regs.r[6]; + AlmGeneric(op.GetName(), ExtendOperandForAlm(op.GetName(), value), b); + } + + void alu(Alu op, MemImm16 a, Ax b) { + u16 value = LoadFromMemory(a); + AlmGeneric(op.GetName(), ExtendOperandForAlm(op.GetName(), value), b); + } + void alu(Alu op, MemR7Imm16 a, Ax b) { + u16 value = LoadFromMemory(a); + AlmGeneric(op.GetName(), ExtendOperandForAlm(op.GetName(), value), b); + } + void alu(Alu op, Imm16 a, Ax b) { + u16 value = a.Unsigned16(); + AlmGeneric(op.GetName(), ExtendOperandForAlm(op.GetName(), value), b); + } + void alu(Alu op, Imm8 a, Ax b) { + u16 value = a.Unsigned16(); + u64 and_backup = 0; + if (op.GetName() == AlmOp::And) { + // AND instruction has a special treatment: + // bit 8~15 are unaffected in the accumulator, but the flags are set as if they are + // affected + and_backup = GetAcc(b.GetName()) & 0xFF00; + } + AlmGeneric(op.GetName(), ExtendOperandForAlm(op.GetName(), value), b); + if (op.GetName() == AlmOp::And) { + u64 and_new = GetAcc(b.GetName()) & 0xFFFF'FFFF'FFFF'00FF; + SetAcc(b.GetName(), and_backup | and_new); + } + } + void alu(Alu op, MemR7Imm7s a, Ax b) { + u16 value = LoadFromMemory(a); + AlmGeneric(op.GetName(), ExtendOperandForAlm(op.GetName(), value), b); + } + + void or_(Ab a, Ax b, Ax c) { + u64 value = GetAcc(a.GetName()) | GetAcc(b.GetName()); + SetAccAndFlag(c.GetName(), value); + } + void or_(Ax a, Bx b, Ax c) { + u64 value = GetAcc(a.GetName()) | GetAcc(b.GetName()); + SetAccAndFlag(c.GetName(), value); + } + void or_(Bx a, Bx b, Ax c) { + u64 value = GetAcc(a.GetName()) | GetAcc(b.GetName()); + SetAccAndFlag(c.GetName(), value); + } + + u16 GenericAlb(Alb op, u16 a, u16 b) { + u16 result; + switch (op.GetName()) { + case AlbOp::Set: { + result = a | b; + regs.fm = result >> 15; + break; + } + case AlbOp::Rst: { + result = ~a & b; + regs.fm = result >> 15; + break; + } + case AlbOp::Chng: { + result = a ^ b; + regs.fm = result >> 15; + break; + } + case AlbOp::Addv: { + u32 r = a + b; + regs.fc0 = (r >> 16) != 0; + regs.fm = (SignExtend<16, u32>(b) + SignExtend<16, u32>(a)) >> 31; // ! + result = r & 0xFFFF; + break; + } + case AlbOp::Tst0: { + result = (a & b) != 0; + break; + } + case AlbOp::Tst1: { + result = (a & ~b) != 0; + break; + } + case AlbOp::Cmpv: + case AlbOp::Subv: { + u32 r = b - a; + regs.fc0 = (r >> 16) != 0; + regs.fm = (SignExtend<16, u32>(b) - SignExtend<16, u32>(a)) >> 31; // ! + result = r & 0xFFFF; + break; + } + default: + UNREACHABLE(); + } + regs.fz = result == 0; + return result; + } + + static bool IsAlbModifying(Alb op) { + switch (op.GetName()) { + case AlbOp::Set: + case AlbOp::Rst: + case AlbOp::Chng: + case AlbOp::Addv: + case AlbOp::Subv: + return true; + case AlbOp::Tst0: + case AlbOp::Tst1: + case AlbOp::Cmpv: + return false; + default: + UNREACHABLE(); + } + } + + void alb(Alb op, Imm16 a, MemImm8 b) { + u16 bv = LoadFromMemory(b); + u16 result = GenericAlb(op, a.Unsigned16(), bv); + if (IsAlbModifying(op)) + StoreToMemory(b, result); + } + void alb(Alb op, Imm16 a, Rn b, StepZIDS bs) { + u16 address = RnAddressAndModify(b.Index(), bs.GetName()); + u16 bv = mem.DataRead(address); + u16 result = GenericAlb(op, a.Unsigned16(), bv); + if (IsAlbModifying(op)) + mem.DataWrite(address, result); + } + void alb(Alb op, Imm16 a, Register b) { + u16 bv; + if (b.GetName() == RegName::p) { + bv = (u16)(ProductToBus40(Px{0}) >> 16); + } else if (b.GetName() == RegName::a0 || b.GetName() == RegName::a1) { + throw UnimplementedException(); // weird effect; + } else { + bv = RegToBus16(b.GetName()); + } + u16 result = GenericAlb(op, a.Unsigned16(), bv); + if (IsAlbModifying(op)) { + switch (b.GetName()) { + case RegName::a0: + case RegName::a1: + UNREACHABLE(); + // operation on accumulators doesn't go through regular bus with flag and saturation + case RegName::a0l: + regs.a[0] = (regs.a[0] & 0xFFFF'FFFF'FFFF'0000) | result; + break; + case RegName::a1l: + regs.a[1] = (regs.a[1] & 0xFFFF'FFFF'FFFF'0000) | result; + break; + case RegName::b0l: + regs.b[0] = (regs.b[0] & 0xFFFF'FFFF'FFFF'0000) | result; + break; + case RegName::b1l: + regs.b[1] = (regs.b[1] & 0xFFFF'FFFF'FFFF'0000) | result; + break; + case RegName::a0h: + regs.a[0] = (regs.a[0] & 0xFFFF'FFFF'0000'FFFF) | ((u64)result << 16); + break; + case RegName::a1h: + regs.a[1] = (regs.a[1] & 0xFFFF'FFFF'0000'FFFF) | ((u64)result << 16); + break; + case RegName::b0h: + regs.b[0] = (regs.b[0] & 0xFFFF'FFFF'0000'FFFF) | ((u64)result << 16); + break; + case RegName::b1h: + regs.b[1] = (regs.b[1] & 0xFFFF'FFFF'0000'FFFF) | ((u64)result << 16); + break; + default: + RegFromBus16(b.GetName(), result); // including RegName:p (p0h) + } + } + } + void alb_r6(Alb op, Imm16 a) { + u16 bv = regs.r[6]; + u16 result = GenericAlb(op, a.Unsigned16(), bv); + if (IsAlbModifying(op)) + regs.r[6] = result; + } + void alb(Alb op, Imm16 a, SttMod b) { + u16 bv = RegToBus16(b.GetName()); + u16 result = GenericAlb(op, a.Unsigned16(), bv); + if (IsAlbModifying(op)) + RegFromBus16(b.GetName(), result); + } + + void add(Ab a, Bx b) { + u64 value_a = GetAcc(a.GetName()); + u64 value_b = GetAcc(b.GetName()); + u64 result = AddSub(value_b, value_a, false); + SatAndSetAccAndFlag(b.GetName(), result); + } + void add(Bx a, Ax b) { + u64 value_a = GetAcc(a.GetName()); + u64 value_b = GetAcc(b.GetName()); + u64 result = AddSub(value_b, value_a, false); + SatAndSetAccAndFlag(b.GetName(), result); + } + void add_p1(Ax b) { + u64 value_a = ProductToBus40(Px{1}); + u64 value_b = GetAcc(b.GetName()); + u64 result = AddSub(value_b, value_a, false); + SatAndSetAccAndFlag(b.GetName(), result); + } + void add(Px a, Bx b) { + u64 value_a = ProductToBus40(a); + u64 value_b = GetAcc(b.GetName()); + u64 result = AddSub(value_b, value_a, false); + SatAndSetAccAndFlag(b.GetName(), result); + } + + void sub(Ab a, Bx b) { + u64 value_a = GetAcc(a.GetName()); + u64 value_b = GetAcc(b.GetName()); + u64 result = AddSub(value_b, value_a, true); + SatAndSetAccAndFlag(b.GetName(), result); + } + void sub(Bx a, Ax b) { + u64 value_a = GetAcc(a.GetName()); + u64 value_b = GetAcc(b.GetName()); + u64 result = AddSub(value_b, value_a, true); + SatAndSetAccAndFlag(b.GetName(), result); + } + void sub_p1(Ax b) { + u64 value_a = ProductToBus40(Px{1}); + u64 value_b = GetAcc(b.GetName()); + u64 result = AddSub(value_b, value_a, true); + SatAndSetAccAndFlag(b.GetName(), result); + } + void sub(Px a, Bx b) { + u64 value_a = ProductToBus40(a); + u64 value_b = GetAcc(b.GetName()); + u64 result = AddSub(value_b, value_a, true); + SatAndSetAccAndFlag(b.GetName(), result); + } + + void app(Ab c, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, bool p1_align) { + ProductSum(base, c.GetName(), sub_p0, p0_align, sub_p1, p1_align); + } + + void add_add(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + auto [oi, oj] = GetArpOffset(asi, asj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 high = SignExtend<16, u64>(mem.DataRead(j)) + SignExtend<16, u64>(mem.DataRead(i)); + u16 low = mem.DataRead(OffsetAddress(uj, j, oj)) + mem.DataRead(OffsetAddress(ui, i, oi)); + u64 result = (high << 16) | low; + SetAcc(b.GetName(), result); + } + void add_sub(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + auto [oi, oj] = GetArpOffset(asi, asj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 high = SignExtend<16, u64>(mem.DataRead(j)) + SignExtend<16, u64>(mem.DataRead(i)); + u16 low = mem.DataRead(OffsetAddress(uj, j, oj)) - mem.DataRead(OffsetAddress(ui, i, oi)); + u64 result = (high << 16) | low; + SetAcc(b.GetName(), result); + } + void sub_add(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + auto [oi, oj] = GetArpOffset(asi, asj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 high = SignExtend<16, u64>(mem.DataRead(j)) - SignExtend<16, u64>(mem.DataRead(i)); + u16 low = mem.DataRead(OffsetAddress(uj, j, oj)) + mem.DataRead(OffsetAddress(ui, i, oi)); + u64 result = (high << 16) | low; + SetAcc(b.GetName(), result); + } + void sub_sub(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + auto [oi, oj] = GetArpOffset(asi, asj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 high = SignExtend<16, u64>(mem.DataRead(j)) - SignExtend<16, u64>(mem.DataRead(i)); + u16 low = mem.DataRead(OffsetAddress(uj, j, oj)) - mem.DataRead(OffsetAddress(ui, i, oi)); + u64 result = (high << 16) | low; + SetAcc(b.GetName(), result); + } + void add_sub_sv(ArRn1 a, ArStep1 as, Ab b) { + u16 u = GetArRnUnit(a); + auto s = GetArStep(as); + auto o = GetArOffset(as); + u16 address = RnAddressAndModify(u, s); + u64 high = SignExtend<16, u64>(mem.DataRead(address)) + SignExtend<16, u64>(regs.sv); + u16 low = mem.DataRead(OffsetAddress(u, address, o)) - regs.sv; + u64 result = (high << 16) | low; + SetAcc(b.GetName(), result); + } + void sub_add_sv(ArRn1 a, ArStep1 as, Ab b) { + u16 u = GetArRnUnit(a); + auto s = GetArStep(as); + auto o = GetArOffset(as); + u16 address = RnAddressAndModify(u, s); + u64 high = SignExtend<16, u64>(mem.DataRead(address)) - SignExtend<16, u64>(regs.sv); + u16 low = mem.DataRead(OffsetAddress(u, address, o)) + regs.sv; + u64 result = (high << 16) | low; + SetAcc(b.GetName(), result); + } + void sub_add_i_mov_j_sv(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + OffsetValue oi; + std::tie(oi, std::ignore) = GetArpOffset(asi, asj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 high = SignExtend<16, u64>(mem.DataRead(i)) - SignExtend<16, u64>(regs.sv); + u16 low = mem.DataRead(OffsetAddress(ui, i, oi)) + regs.sv; + u64 result = (high << 16) | low; + SetAcc(b.GetName(), result); + regs.sv = mem.DataRead(j); + } + void sub_add_j_mov_i_sv(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + OffsetValue oj; + std::tie(std::ignore, oj) = GetArpOffset(asi, asj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 high = SignExtend<16, u64>(mem.DataRead(j)) - SignExtend<16, u64>(regs.sv); + u16 low = mem.DataRead(OffsetAddress(uj, j, oj)) + regs.sv; + u64 result = (high << 16) | low; + SetAcc(b.GetName(), result); + regs.sv = mem.DataRead(i); + } + void add_sub_i_mov_j(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + OffsetValue oi; + std::tie(oi, std::ignore) = GetArpOffset(asi, asj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 high = SignExtend<16, u64>(mem.DataRead(i)) + SignExtend<16, u64>(regs.sv); + u16 low = mem.DataRead(OffsetAddress(ui, i, oi)) - regs.sv; + u64 result = (high << 16) | low; + u16 exchange = (u16)(GetAndSatAccNoFlag(b.GetName()) & 0xFFFF); + SetAcc(b.GetName(), result); + mem.DataWrite(j, exchange); + } + void add_sub_j_mov_i(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + OffsetValue oj; + std::tie(std::ignore, oj) = GetArpOffset(asi, asj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 high = SignExtend<16, u64>(mem.DataRead(j)) + SignExtend<16, u64>(regs.sv); + u16 low = mem.DataRead(OffsetAddress(uj, j, oj)) - regs.sv; + u64 result = (high << 16) | low; + u16 exchange = (u16)(GetAndSatAccNoFlag(b.GetName()) & 0xFFFF); + SetAcc(b.GetName(), result); + mem.DataWrite(i, exchange); + } + + void Moda(ModaOp op, RegName a, Cond cond) { + if (regs.ConditionPass(cond)) { + switch (op) { + case ModaOp::Shr: { + ShiftBus40(GetAcc(a), 0xFFFF, a); + break; + } + case ModaOp::Shr4: { + ShiftBus40(GetAcc(a), 0xFFFC, a); + break; + } + case ModaOp::Shl: { + ShiftBus40(GetAcc(a), 1, a); + break; + } + case ModaOp::Shl4: { + ShiftBus40(GetAcc(a), 4, a); + break; + } + case ModaOp::Ror: { + u64 value = GetAcc(a) & 0xFF'FFFF'FFFF; + u16 old_fc = regs.fc0; + regs.fc0 = value & 1; + value >>= 1; + value |= (u64)old_fc << 39; + value = SignExtend<40>(value); + SetAccAndFlag(a, value); + break; + } + case ModaOp::Rol: { + u64 value = GetAcc(a); + u16 old_fc = regs.fc0; + regs.fc0 = (value >> 39) & 1; + value <<= 1; + value |= old_fc; + value = SignExtend<40>(value); + SetAccAndFlag(a, value); + break; + } + case ModaOp::Clr: { + SatAndSetAccAndFlag(a, 0); + break; + } + case ModaOp::Not: { + u64 result = ~GetAcc(a); + SetAccAndFlag(a, result); + break; + } + case ModaOp::Neg: { + u64 value = GetAcc(a); + regs.fc0 = value != 0; // ? + regs.fv = value == 0xFFFF'FF80'0000'0000; // ? + if (regs.fv) + regs.fvl = 1; + u64 result = SignExtend<40, u64>(~GetAcc(a) + 1); + SatAndSetAccAndFlag(a, result); + break; + } + case ModaOp::Rnd: { + u64 value = GetAcc(a); + u64 result = AddSub(value, 0x8000, false); + SatAndSetAccAndFlag(a, result); + break; + } + case ModaOp::Pacr: { + u64 value = ProductToBus40(Px{0}); + u64 result = AddSub(value, 0x8000, false); + SatAndSetAccAndFlag(a, result); + break; + } + case ModaOp::Clrr: { + SatAndSetAccAndFlag(a, 0x8000); + break; + } + case ModaOp::Inc: { + u64 value = GetAcc(a); + u64 result = AddSub(value, 1, false); + SatAndSetAccAndFlag(a, result); + break; + } + case ModaOp::Dec: { + u64 value = GetAcc(a); + u64 result = AddSub(value, 1, true); + SatAndSetAccAndFlag(a, result); + break; + } + case ModaOp::Copy: { + // note: bX doesn't support + u64 value = GetAcc(a == RegName::a0 ? RegName::a1 : RegName::a0); + SatAndSetAccAndFlag(a, value); + break; + } + default: + UNREACHABLE(); + } + } + } + + void moda4(Moda4 op, Ax a, Cond cond) { + Moda(op.GetName(), a.GetName(), cond); + } + + void moda3(Moda3 op, Bx a, Cond cond) { + Moda(op.GetName(), a.GetName(), cond); + } + + void pacr1(Ax a) { + u64 value = ProductToBus40(Px{1}); + u64 result = AddSub(value, 0x8000, false); + SatAndSetAccAndFlag(a.GetName(), result); + } + + void FilterDoubleClr(RegName& a, RegName& b) { + if (a == RegName::b0) { + b = RegName::b1; + } else if (a == RegName::b1) { + b = RegName::b0; + } else if (a == RegName::a0) { + if (b == RegName::a0) + b = RegName::a1; + } else + b = b == RegName::b1 ? RegName::b1 : RegName::b0; + } + + void clr(Ab a, Ab b) { + RegName a_name = a.GetName(); + RegName b_name = b.GetName(); + FilterDoubleClr(a_name, b_name); + SatAndSetAccAndFlag(a_name, 0); + SatAndSetAccAndFlag(b_name, 0); + } + void clrr(Ab a, Ab b) { + RegName a_name = a.GetName(); + RegName b_name = b.GetName(); + FilterDoubleClr(a_name, b_name); + SatAndSetAccAndFlag(a_name, 0x8000); + SatAndSetAccAndFlag(b_name, 0x8000); + } + + void BlockRepeat(u16 lc, u32 address) { + ASSERT(regs.bcn <= 3); + regs.bkrep_stack[regs.bcn].start = regs.pc; + regs.bkrep_stack[regs.bcn].end = address; + regs.bkrep_stack[regs.bcn].lc = lc; + regs.lp = 1; + ++regs.bcn; + } + + void bkrep(Imm8 a, Address16 addr) { + u16 lc = a.Unsigned16(); + u32 address = addr.Address32() | (regs.pc & 0x30000); + BlockRepeat(lc, address); + } + void bkrep(Register a, Address18_16 addr_low, Address18_2 addr_high) { + u16 lc = RegToBus16(a.GetName()); + u32 address = Address32(addr_low, addr_high); + BlockRepeat(lc, address); + } + void bkrep_r6(Address18_16 addr_low, Address18_2 addr_high) { + u16 lc = regs.r[6]; + u32 address = Address32(addr_low, addr_high); + BlockRepeat(lc, address); + } + + void RestoreBlockRepeat(u16& address_reg) { + if (regs.lp) { + ASSERT(regs.bcn <= 3); + std::copy_backward(regs.bkrep_stack.begin(), regs.bkrep_stack.begin() + regs.bcn, + regs.bkrep_stack.begin() + regs.bcn + 1); + ++regs.bcn; + } + u32 flag = mem.DataRead(address_reg++); + u16 valid = flag >> 15; + if (regs.lp) { + ASSERT(valid); + } else { + if (valid) + regs.lp = regs.bcn = 1; + } + regs.bkrep_stack[0].end = mem.DataRead(address_reg++) | (((flag >> 8) & 3) << 16); + regs.bkrep_stack[0].start = mem.DataRead(address_reg++) | ((flag & 3) << 16); + regs.bkrep_stack[0].lc = mem.DataRead(address_reg++); + } + void StoreBlockRepeat(u16& address_reg) { + mem.DataWrite(--address_reg, regs.bkrep_stack[0].lc); + mem.DataWrite(--address_reg, regs.bkrep_stack[0].start & 0xFFFF); + mem.DataWrite(--address_reg, regs.bkrep_stack[0].end & 0xFFFF); + u16 flag = regs.lp << 15; + flag |= regs.bkrep_stack[0].start >> 16; + flag |= (regs.bkrep_stack[0].end >> 16) << 8; + mem.DataWrite(--address_reg, flag); + if (regs.lp) { + std::copy(regs.bkrep_stack.begin() + 1, regs.bkrep_stack.begin() + regs.bcn, + regs.bkrep_stack.begin()); + --regs.bcn; + if (regs.bcn == 0) + regs.lp = 0; + } + } + void bkreprst(ArRn2 a) { + RestoreBlockRepeat(regs.r[GetArRnUnit(a)]); + } + void bkreprst_memsp() { + RestoreBlockRepeat(regs.sp); + } + void bkrepsto(ArRn2 a) { + StoreBlockRepeat(regs.r[GetArRnUnit(a)]); + } + void bkrepsto_memsp() { + StoreBlockRepeat(regs.sp); + } + + void banke(BankFlags flags) { + if (flags.Cfgi()) { + std::swap(regs.stepi, regs.stepib); + std::swap(regs.modi, regs.modib); + if (regs.stp16) + std::swap(regs.stepi0, regs.stepi0b); + } + if (flags.R4()) { + std::swap(regs.r[4], regs.r4b); + } + if (flags.R1()) { + std::swap(regs.r[1], regs.r1b); + } + if (flags.R0()) { + std::swap(regs.r[0], regs.r0b); + } + if (flags.R7()) { + std::swap(regs.r[7], regs.r7b); + } + if (flags.Cfgj()) { + std::swap(regs.stepj, regs.stepjb); + std::swap(regs.modj, regs.modjb); + if (regs.stp16) + std::swap(regs.stepj0, regs.stepj0b); + } + } + void bankr() { + regs.SwapAllArArp(); + } + void bankr(Ar a) { + regs.SwapAr(a.Index()); + } + void bankr(Ar a, Arp b) { + regs.SwapAr(a.Index()); + regs.SwapArp(b.Index()); + } + void bankr(Arp a) { + regs.SwapArp(a.Index()); + } + + void bitrev(Rn a) { + u32 unit = a.Index(); + regs.r[unit] = BitReverse(regs.r[unit]); + } + void bitrev_dbrv(Rn a) { + u32 unit = a.Index(); + regs.r[unit] = BitReverse(regs.r[unit]); + regs.br[unit] = 0; + } + void bitrev_ebrv(Rn a) { + u32 unit = a.Index(); + regs.r[unit] = BitReverse(regs.r[unit]); + regs.br[unit] = 1; + } + + void br(Address18_16 addr_low, Address18_2 addr_high, Cond cond) { + if (regs.ConditionPass(cond)) { + SetPC(Address32(addr_low, addr_high)); + } + } + + void brr(RelAddr7 addr, Cond cond) { + if (regs.ConditionPass(cond)) { + regs.pc += addr.Relative32(); // note: pc is the address of the NEXT instruction + if (addr.Relative32() == 0xFFFFFFFF) { + idle = true; + } + } + } + + void break_() { + ASSERT(regs.lp); + --regs.bcn; + regs.lp = regs.bcn != 0; + // Note: unlike one would expect, the "break" instruction doesn't jump out of the block + } + + void call(Address18_16 addr_low, Address18_2 addr_high, Cond cond) { + if (regs.ConditionPass(cond)) { + PushPC(); + SetPC(Address32(addr_low, addr_high)); + } + } + void calla(Axl a) { + PushPC(); + SetPC(RegToBus16(a.GetName())); // use pcmhi? + } + void calla(Ax a) { + PushPC(); + SetPC(GetAcc(a.GetName()) & 0x3FFFF); // no saturation ? + } + void callr(RelAddr7 addr, Cond cond) { + if (regs.ConditionPass(cond)) { + PushPC(); + regs.pc += addr.Relative32(); + } + } + + void ContextStore() { + regs.ShadowStore(); + regs.ShadowSwap(); + if (!regs.crep) { + regs.repcs = regs.repc; + } + if (!regs.ccnta) { + regs.a1s = regs.a[1]; + regs.b1s = regs.b[1]; + } else { + u64 a = regs.a[1]; + u64 b = regs.b[1]; + regs.b[1] = a; + SetAccAndFlag(RegName::a1, b); // Flag set on b1->a1 + } + } + + void ContextRestore() { + regs.ShadowRestore(); + regs.ShadowSwap(); + if (!regs.crep) { + regs.repc = regs.repcs; + } + if (!regs.ccnta) { + regs.a[1] = regs.a1s; + regs.b[1] = regs.b1s; + } else { + std::swap(regs.a[1], regs.b[1]); + } + } + + void cntx_s() { + ContextStore(); + } + void cntx_r() { + ContextRestore(); + } + + void ret(Cond c) { + if (regs.ConditionPass(c)) { + PopPC(); + } + } + void retd() { + throw UnimplementedException(); + } + void reti(Cond c) { + if (regs.ConditionPass(c)) { + PopPC(); + regs.ie = 1; + } + } + void retic(Cond c) { + if (regs.ConditionPass(c)) { + PopPC(); + regs.ie = 1; + ContextRestore(); + } + } + void retid() { + UNREACHABLE(); + } + void retidc() { + UNREACHABLE(); + } + void rets(Imm8 a) { + PopPC(); + regs.sp += a.Unsigned16(); + } + + void load_ps(Imm2 a) { + regs.ps[0] = a.Unsigned16(); + } + void load_stepi(Imm7s a) { + // Although this is signed, we still only store the lower 7 bits + regs.stepi = a.Signed16() & 0x7F; + } + void load_stepj(Imm7s a) { + regs.stepj = a.Signed16() & 0x7F; + } + void load_page(Imm8 a) { + regs.page = a.Unsigned16(); + } + void load_modi(Imm9 a) { + regs.modi = a.Unsigned16(); + } + void load_modj(Imm9 a) { + regs.modj = a.Unsigned16(); + } + void load_movpd(Imm2 a) { + regs.pcmhi = a.Unsigned16(); + } + void load_ps01(Imm4 a) { + regs.ps[0] = a.Unsigned16() & 3; + regs.ps[1] = a.Unsigned16() >> 2; + } + + void push(Imm16 a) { + mem.DataWrite(--regs.sp, a.Unsigned16()); + } + void push(Register a) { + u16 value = RegToBus16(a.GetName(), true); + mem.DataWrite(--regs.sp, value); + } + void push(Abe a) { + u16 value = (GetAndSatAcc(a.GetName()) >> 32) & 0xFFFF; + mem.DataWrite(--regs.sp, value); + } + void push(ArArpSttMod a) { + u16 value = RegToBus16(a.GetName()); + mem.DataWrite(--regs.sp, value); + } + void push_prpage() { + mem.DataWrite(--regs.sp, regs.prpage); + } + void push(Px a) { + u32 value = (u32)ProductToBus40(a); + u16 h = value >> 16; + u16 l = value & 0xFFFF; + mem.DataWrite(--regs.sp, l); + mem.DataWrite(--regs.sp, h); + } + void push_r6() { + u16 value = regs.r[6]; + mem.DataWrite(--regs.sp, value); + } + void push_repc() { + u16 value = regs.repc; + mem.DataWrite(--regs.sp, value); + } + void push_x0() { + u16 value = regs.x[0]; + mem.DataWrite(--regs.sp, value); + } + void push_x1() { + u16 value = regs.x[1]; + mem.DataWrite(--regs.sp, value); + } + void push_y1() { + u16 value = regs.y[1]; + mem.DataWrite(--regs.sp, value); + } + void pusha(Ax a) { + u32 value = GetAndSatAcc(a.GetName()) & 0xFFFF'FFFF; + u16 h = value >> 16; + u16 l = value & 0xFFFF; + mem.DataWrite(--regs.sp, l); + mem.DataWrite(--regs.sp, h); + } + void pusha(Bx a) { + u32 value = GetAndSatAcc(a.GetName()) & 0xFFFF'FFFF; + u16 h = value >> 16; + u16 l = value & 0xFFFF; + mem.DataWrite(--regs.sp, l); + mem.DataWrite(--regs.sp, h); + } + + void pop(Register a) { + u16 value = mem.DataRead(regs.sp++); + RegFromBus16(a.GetName(), value); + } + void pop(Abe a) { + u32 value32 = SignExtend<8, u32>(mem.DataRead(regs.sp++) & 0xFF); + u64 acc = GetAcc(a.GetName()); + SetAccAndFlag(a.GetName(), (acc & 0xFFFFFFFF) | (u64)value32 << 32); + } + void pop(ArArpSttMod a) { + u16 value = mem.DataRead(regs.sp++); + RegFromBus16(a.GetName(), value); + } + void pop(Bx a) { + u16 value = mem.DataRead(regs.sp++); + RegFromBus16(a.GetName(), value); + } + void pop_prpage() { + regs.prpage = mem.DataRead(regs.sp++); + } + void pop(Px a) { + u16 h = mem.DataRead(regs.sp++); + u16 l = mem.DataRead(regs.sp++); + u32 value = ((u32)h << 16) | l; + ProductFromBus32(a, value); + } + void pop_r6() { + u16 value = mem.DataRead(regs.sp++); + regs.r[6] = value; + } + void pop_repc() { + u16 value = mem.DataRead(regs.sp++); + regs.repc = value; + } + void pop_x0() { + u16 value = mem.DataRead(regs.sp++); + regs.x[0] = value; + } + void pop_x1() { + u16 value = mem.DataRead(regs.sp++); + regs.x[1] = value; + } + void pop_y1() { + u16 value = mem.DataRead(regs.sp++); + regs.y[1] = value; + } + void popa(Ab a) { + u16 h = mem.DataRead(regs.sp++); + u16 l = mem.DataRead(regs.sp++); + u64 value = SignExtend<32, u64>(((u64)h << 16) | l); + SetAccAndFlag(a.GetName(), value); + } + + void Repeat(u16 repc) { + regs.repc = repc; + regs.rep = true; + } + + void rep(Imm8 a) { + Repeat(a.Unsigned16()); + } + void rep(Register a) { + Repeat(RegToBus16(a.GetName())); + } + void rep_r6() { + Repeat(regs.r[6]); + } + + void shfc(Ab a, Ab b, Cond cond) { + if (regs.ConditionPass(cond)) { + u64 value = GetAcc(a.GetName()); + u16 sv = regs.sv; + ShiftBus40(value, sv, b.GetName()); + } + } + void shfi(Ab a, Ab b, Imm6s s) { + u64 value = GetAcc(a.GetName()); + u16 sv = s.Signed16(); + ShiftBus40(value, sv, b.GetName()); + } + + void tst4b(ArRn2 b, ArStep2 bs) { + u16 address = RnAddressAndModify(GetArRnUnit(b), GetArStep(bs)); + u16 value = mem.DataRead(address); + u64 bit = GetAcc(RegName::a0) & 0xF; + // Is this correct? an why? + regs.fz = regs.fc0 = (value >> bit) & 1; + } + void tst4b(ArRn2 b, ArStep2 bs, Ax c) { + u64 a = GetAcc(RegName::a0); + u64 bit = a & 0xF; + u16 fv = regs.fv; + u16 fvl = regs.fvl; + u16 fm = regs.fm; + u16 fn = regs.fn; + u16 fe = regs.fe; + u16 sv = regs.sv; + ShiftBus40(a, sv, c.GetName()); + regs.fc1 = regs.fc0; + regs.fv = fv; + regs.fvl = fvl; + regs.fm = fm; + regs.fn = fn; + regs.fe = fe; + u16 address = RnAddressAndModify(GetArRnUnit(b), GetArStep(bs)); + u16 value = mem.DataRead(address); + regs.fz = regs.fc0 = (value >> bit) & 1; + } + void tstb(MemImm8 a, Imm4 b) { + u16 value = LoadFromMemory(a); + regs.fz = (value >> b.Unsigned16()) & 1; + } + void tstb(Rn a, StepZIDS as, Imm4 b) { + u16 address = RnAddressAndModify(a.Index(), as.GetName()); + u16 value = mem.DataRead(address); + regs.fz = (value >> b.Unsigned16()) & 1; + } + void tstb(Register a, Imm4 b) { + u16 value = RegToBus16(a.GetName()); + regs.fz = (value >> b.Unsigned16()) & 1; + } + void tstb_r6(Imm4 b) { + u16 value = regs.r[6]; + regs.fz = (value >> b.Unsigned16()) & 1; + } + void tstb(SttMod a, Imm16 b) { + u16 value = RegToBus16(a.GetName()); + regs.fz = (value >> b.Unsigned16()) & 1; + } + + void and_(Ab a, Ab b, Ax c) { + u64 value = GetAcc(a.GetName()) & GetAcc(b.GetName()); + SetAccAndFlag(c.GetName(), value); + } + + void dint() { + regs.ie = 0; + } + void eint() { + regs.ie = 1; + } + + void MulGeneric(MulOp op, Ax a) { + if (op != MulOp::Mpy && op != MulOp::Mpysu) { + u64 value = GetAcc(a.GetName()); + u64 product = ProductToBus40(Px{0}); + if (op == MulOp::Maa || op == MulOp::Maasu) { + product >>= 16; + product = SignExtend<24>(product); + } + u64 result = AddSub(value, product, false); + SatAndSetAccAndFlag(a.GetName(), result); + } + + switch (op) { + case MulOp::Mpy: + case MulOp::Mac: + case MulOp::Maa: + DoMultiplication(0, true, true); + break; + case MulOp::Mpysu: + case MulOp::Macsu: + case MulOp::Maasu: + // Note: the naming conventin of "mpysu" is "multiply signed *y* by unsigned *x*" + DoMultiplication(0, false, true); + break; + case MulOp::Macus: + DoMultiplication(0, true, false); + break; + case MulOp::Macuu: + DoMultiplication(0, false, false); + break; + } + } + + void mul(Mul3 op, Rn y, StepZIDS ys, Imm16 x, Ax a) { + u16 address = RnAddressAndModify(y.Index(), ys.GetName()); + regs.y[0] = mem.DataRead(address); + regs.x[0] = x.Unsigned16(); + MulGeneric(op.GetName(), a); + } + void mul_y0(Mul3 op, Rn x, StepZIDS xs, Ax a) { + u16 address = RnAddressAndModify(x.Index(), xs.GetName()); + regs.x[0] = mem.DataRead(address); + MulGeneric(op.GetName(), a); + } + void mul_y0(Mul3 op, Register x, Ax a) { + regs.x[0] = RegToBus16(x.GetName()); + MulGeneric(op.GetName(), a); + } + void mul(Mul3 op, R45 y, StepZIDS ys, R0123 x, StepZIDS xs, Ax a) { + u16 address_y = RnAddressAndModify(y.Index(), ys.GetName()); + u16 address_x = RnAddressAndModify(x.Index(), xs.GetName()); + regs.y[0] = mem.DataRead(address_y); + regs.x[0] = mem.DataRead(address_x); + MulGeneric(op.GetName(), a); + } + void mul_y0_r6(Mul3 op, Ax a) { + regs.x[0] = regs.r[6]; + MulGeneric(op.GetName(), a); + } + void mul_y0(Mul2 op, MemImm8 x, Ax a) { + regs.x[0] = LoadFromMemory(x); + MulGeneric(op.GetName(), a); + } + + void mpyi(Imm8s x) { + regs.x[0] = x.Signed16(); + DoMultiplication(0, true, true); + } + + void msu(R45 y, StepZIDS ys, R0123 x, StepZIDS xs, Ax a) { + u16 yi = RnAddressAndModify(y.Index(), ys.GetName()); + u16 xi = RnAddressAndModify(x.Index(), xs.GetName()); + u64 value = GetAcc(a.GetName()); + u64 product = ProductToBus40(Px{0}); + u64 result = AddSub(value, product, true); + SatAndSetAccAndFlag(a.GetName(), result); + regs.y[0] = mem.DataRead(yi); + regs.x[0] = mem.DataRead(xi); + DoMultiplication(0, true, true); + } + void msu(Rn y, StepZIDS ys, Imm16 x, Ax a) { + u16 yi = RnAddressAndModify(y.Index(), ys.GetName()); + u64 value = GetAcc(a.GetName()); + u64 product = ProductToBus40(Px{0}); + u64 result = AddSub(value, product, true); + SatAndSetAccAndFlag(a.GetName(), result); + regs.y[0] = mem.DataRead(yi); + regs.x[0] = x.Unsigned16(); + DoMultiplication(0, true, true); + } + void msusu(ArRn2 x, ArStep2 xs, Ax a) { + u16 xi = RnAddressAndModify(GetArRnUnit(x), GetArStep(xs)); + u64 value = GetAcc(a.GetName()); + u64 product = ProductToBus40(Px{0}); + u64 result = AddSub(value, product, true); + SatAndSetAccAndFlag(a.GetName(), result); + regs.x[0] = mem.DataRead(xi); + DoMultiplication(0, false, true); + } + void mac_x1to0(Ax a) { + u64 value = GetAcc(a.GetName()); + u64 product = ProductToBus40(Px{0}); + u64 result = AddSub(value, product, false); + SatAndSetAccAndFlag(a.GetName(), result); + regs.x[0] = regs.x[1]; + DoMultiplication(0, true, true); + } + void mac1(ArpRn1 xy, ArpStep1 xis, ArpStep1 yjs, Ax a) { + auto [ui, uj] = GetArpRnUnit(xy); + auto [si, sj] = GetArpStep(xis, yjs); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 value = GetAcc(a.GetName()); + u64 product = ProductToBus40(Px{1}); + u64 result = AddSub(value, product, false); + SatAndSetAccAndFlag(a.GetName(), result); + regs.x[1] = mem.DataRead(i); + regs.y[1] = mem.DataRead(j); + DoMultiplication(1, true, true); + } + + void modr(Rn a, StepZIDS as) { + u32 unit = a.Index(); + RnAndModify(unit, as.GetName()); + regs.fr = regs.r[unit] == 0; + } + void modr_dmod(Rn a, StepZIDS as) { + u32 unit = a.Index(); + RnAndModify(unit, as.GetName(), true); + regs.fr = regs.r[unit] == 0; + } + void modr_i2(Rn a) { + u32 unit = a.Index(); + RnAndModify(unit, StepValue::Increase2Mode1); + regs.fr = regs.r[unit] == 0; + } + void modr_i2_dmod(Rn a) { + u32 unit = a.Index(); + RnAndModify(unit, StepValue::Increase2Mode1, true); + regs.fr = regs.r[unit] == 0; + } + void modr_d2(Rn a) { + u32 unit = a.Index(); + RnAndModify(unit, StepValue::Decrease2Mode1); + regs.fr = regs.r[unit] == 0; + } + void modr_d2_dmod(Rn a) { + u32 unit = a.Index(); + RnAndModify(unit, StepValue::Decrease2Mode1, true); + regs.fr = regs.r[unit] == 0; + } + void modr_eemod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + u32 uniti, unitj; + StepValue stepi, stepj; + std::tie(uniti, unitj) = GetArpRnUnit(a); + std::tie(stepi, stepj) = GetArpStep(asi, asj); + RnAndModify(uniti, stepi); + RnAndModify(unitj, stepj); + } + void modr_edmod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + u32 uniti, unitj; + StepValue stepi, stepj; + std::tie(uniti, unitj) = GetArpRnUnit(a); + std::tie(stepi, stepj) = GetArpStep(asi, asj); + RnAndModify(uniti, stepi); + RnAndModify(unitj, stepj, true); + } + void modr_demod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + u32 uniti, unitj; + StepValue stepi, stepj; + std::tie(uniti, unitj) = GetArpRnUnit(a); + std::tie(stepi, stepj) = GetArpStep(asi, asj); + RnAndModify(uniti, stepi, true); + RnAndModify(unitj, stepj); + } + void modr_ddmod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + u32 uniti, unitj; + StepValue stepi, stepj; + std::tie(uniti, unitj) = GetArpRnUnit(a); + std::tie(stepi, stepj) = GetArpStep(asi, asj); + RnAndModify(uniti, stepi, true); + RnAndModify(unitj, stepj, true); + } + + void movd(R0123 a, StepZIDS as, R45 b, StepZIDS bs) { + u16 address_s = RnAddressAndModify(a.Index(), as.GetName()); + u32 address_d = RnAddressAndModify(b.Index(), bs.GetName()); + address_d |= (u32)regs.pcmhi << 16; + mem.ProgramWrite(address_d, mem.DataRead(address_s)); + } + void movp(Axl a, Register b) { + u32 address = RegToBus16(a.GetName()); + address |= (u32)regs.pcmhi << 16; + u16 value = mem.ProgramRead(address); + RegFromBus16(b.GetName(), value); + } + void movp(Ax a, Register b) { + u32 address = GetAcc(a.GetName()) & 0x3FFFF; // no saturation + u16 value = mem.ProgramRead(address); + RegFromBus16(b.GetName(), value); + } + void movp(Rn a, StepZIDS as, R0123 b, StepZIDS bs) { + u32 address_s = RnAddressAndModify(a.Index(), as.GetName()); + u16 address_d = RnAddressAndModify(b.Index(), bs.GetName()); + address_s |= (u32)regs.pcmhi << 16; + mem.DataWrite(address_d, mem.ProgramRead(address_s)); + } + void movpdw(Ax a) { + u32 address = GetAcc(a.GetName()) & 0x3FFFF; // no saturation + // the endianess doesn't seem to be affected by regs.cpc + u16 h = mem.ProgramRead(address); + u16 l = mem.ProgramRead(address + 1); + SetPC(l | ((u32)h << 16)); + } + + void mov(Ab a, Ab b) { + u64 value = GetAcc(a.GetName()); + SatAndSetAccAndFlag(b.GetName(), value); + } + void mov_dvm(Abl a) { + UNREACHABLE(); + } + void mov_x0(Abl a) { + u16 value16 = RegToBus16(a.GetName(), true); + regs.x[0] = value16; + } + void mov_x1(Abl a) { + u16 value16 = RegToBus16(a.GetName(), true); + regs.x[1] = value16; + } + void mov_y1(Abl a) { + u16 value16 = RegToBus16(a.GetName(), true); + regs.y[1] = value16; + } + + void StoreToMemory(MemImm8 addr, u16 value) { + mem.DataWrite(addr.Unsigned16() + (regs.page << 8), value); + } + void StoreToMemory(MemImm16 addr, u16 value) { + mem.DataWrite(addr.Unsigned16(), value); + } + void StoreToMemory(MemR7Imm16 addr, u16 value) { + mem.DataWrite(addr.Unsigned16() + regs.r[7], value); + } + void StoreToMemory(MemR7Imm7s addr, u16 value) { + mem.DataWrite(addr.Signed16() + regs.r[7], value); + } + + void mov(Ablh a, MemImm8 b) { + u16 value16 = RegToBus16(a.GetName(), true); + StoreToMemory(b, value16); + } + void mov(Axl a, MemImm16 b) { + u16 value16 = RegToBus16(a.GetName(), true); + StoreToMemory(b, value16); + } + void mov(Axl a, MemR7Imm16 b) { + u16 value16 = RegToBus16(a.GetName(), true); + StoreToMemory(b, value16); + } + void mov(Axl a, MemR7Imm7s b) { + u16 value16 = RegToBus16(a.GetName(), true); + StoreToMemory(b, value16); + } + + u16 LoadFromMemory(MemImm8 addr) { + return mem.DataRead(addr.Unsigned16() + (regs.page << 8)); + } + u16 LoadFromMemory(MemImm16 addr) { + return mem.DataRead(addr.Unsigned16()); + } + u16 LoadFromMemory(MemR7Imm16 addr) { + return mem.DataRead(addr.Unsigned16() + regs.r[7]); + } + u16 LoadFromMemory(MemR7Imm7s addr) { + return mem.DataRead(addr.Signed16() + regs.r[7]); + } + + void mov(MemImm16 a, Ax b) { + u16 value = LoadFromMemory(a); + RegFromBus16(b.GetName(), value); + } + void mov(MemImm8 a, Ab b) { + u16 value = LoadFromMemory(a); + RegFromBus16(b.GetName(), value); + } + void mov(MemImm8 a, Ablh b) { + u16 value = LoadFromMemory(a); + RegFromBus16(b.GetName(), value); + } + void mov_eu(MemImm8 a, Axh b) { + u16 value = LoadFromMemory(a); + u64 acc = GetAcc(b.GetName()); + acc &= 0xFFFF'FFFF'0000'0000; + acc |= (u64)value << 16; + SetAccAndFlag(b.GetName(), acc); // ? + } + void mov(MemImm8 a, RnOld b) { + u16 value = LoadFromMemory(a); + RegFromBus16(b.GetName(), value); + } + void mov_sv(MemImm8 a) { + u16 value = LoadFromMemory(a); + regs.sv = value; + } + void mov_dvm_to(Ab b) { + UNREACHABLE(); + } + void mov_icr_to(Ab b) { + u16 value = regs.Get<icr>(); + RegFromBus16(b.GetName(), value); + } + void mov(Imm16 a, Bx b) { + u16 value = a.Unsigned16(); + RegFromBus16(b.GetName(), value); + } + void mov(Imm16 a, Register b) { + u16 value = a.Unsigned16(); + RegFromBus16(b.GetName(), value); + } + void mov_icr(Imm5 a) { + u16 value = regs.Get<icr>(); + value &= ~0x1F; + value |= a.Unsigned16(); + regs.Set<icr>(value); + } + void mov(Imm8s a, Axh b) { + u16 value = a.Signed16(); + RegFromBus16(b.GetName(), value); + } + void mov(Imm8s a, RnOld b) { + u16 value = a.Signed16(); + RegFromBus16(b.GetName(), value); + } + void mov_sv(Imm8s a) { + u16 value = a.Signed16(); + regs.sv = value; + } + void mov(Imm8 a, Axl b) { + u16 value = a.Unsigned16(); + RegFromBus16(b.GetName(), value); + } + void mov(MemR7Imm16 a, Ax b) { + u16 value = LoadFromMemory(a); + RegFromBus16(b.GetName(), value); + } + void mov(MemR7Imm7s a, Ax b) { + u16 value = LoadFromMemory(a); + RegFromBus16(b.GetName(), value); + } + void mov(Rn a, StepZIDS as, Bx b) { + u16 address = RnAddressAndModify(a.Index(), as.GetName()); + u16 value = mem.DataRead(address); + RegFromBus16(b.GetName(), value); + } + void mov(Rn a, StepZIDS as, Register b) { + u16 address = RnAddressAndModify(a.Index(), as.GetName()); + u16 value = mem.DataRead(address); + RegFromBus16(b.GetName(), value); + } + void mov_memsp_to(Register b) { + u16 value = mem.DataRead(regs.sp); + RegFromBus16(b.GetName(), value); + } + void mov_mixp_to(Register b) { + u16 value = regs.mixp; + RegFromBus16(b.GetName(), value); + } + void mov(RnOld a, MemImm8 b) { + u16 value = RegToBus16(a.GetName()); + StoreToMemory(b, value); + } + void mov_icr(Register a) { + u16 value = RegToBus16(a.GetName(), true); + regs.Set<icr>(value); + } + void mov_mixp(Register a) { + u16 value = RegToBus16(a.GetName(), true); + regs.mixp = value; + } + void mov(Register a, Rn b, StepZIDS bs) { + // a = a0 or a1 is overrided + u16 value = RegToBus16(a.GetName(), true); + u16 address = RnAddressAndModify(b.Index(), bs.GetName()); + mem.DataWrite(address, value); + } + void mov(Register a, Bx b) { + if (a.GetName() == RegName::p) { + u64 value = ProductToBus40(Px{0}); + SatAndSetAccAndFlag(b.GetName(), value); + } else if (a.GetName() == RegName::a0 || a.GetName() == RegName::a1) { + // Is there any difference from the mov(Ab, Ab) instruction? + u64 value = GetAcc(a.GetName()); + SatAndSetAccAndFlag(b.GetName(), value); + } else { + u16 value = RegToBus16(a.GetName(), true); + RegFromBus16(b.GetName(), value); + } + } + void mov(Register a, Register b) { + // a = a0 or a1 is overrided + if (a.GetName() == RegName::p) { + // b loses its typical meaning in this case + RegName b_name = b.GetNameForMovFromP(); + u64 value = ProductToBus40(Px{0}); + SatAndSetAccAndFlag(b_name, value); + } else if (a.GetName() == RegName::pc) { + if (b.GetName() == RegName::a0 || b.GetName() == RegName::a1) { + SatAndSetAccAndFlag(b.GetName(), regs.pc); + } else { + RegFromBus16(b.GetName(), regs.pc & 0xFFFF); + } + } else { + u16 value = RegToBus16(a.GetName(), true); + RegFromBus16(b.GetName(), value); + } + } + void mov_repc_to(Ab b) { + u16 value = regs.repc; + RegFromBus16(b.GetName(), value); + } + void mov_sv_to(MemImm8 b) { + u16 value = regs.sv; + StoreToMemory(b, value); + } + void mov_x0_to(Ab b) { + u16 value = regs.x[0]; + RegFromBus16(b.GetName(), value); + } + void mov_x1_to(Ab b) { + u16 value = regs.x[1]; + RegFromBus16(b.GetName(), value); + } + void mov_y1_to(Ab b) { + u16 value = regs.y[1]; + RegFromBus16(b.GetName(), value); + } + void mov(Imm16 a, ArArp b) { + u16 value = a.Unsigned16(); + RegFromBus16(b.GetName(), value); + } + void mov_r6(Imm16 a) { + u16 value = a.Unsigned16(); + regs.r[6] = value; + } + void mov_repc(Imm16 a) { + u16 value = a.Unsigned16(); + regs.repc = value; + } + void mov_stepi0(Imm16 a) { + u16 value = a.Unsigned16(); + regs.stepi0 = value; + } + void mov_stepj0(Imm16 a) { + u16 value = a.Unsigned16(); + regs.stepj0 = value; + } + void mov(Imm16 a, SttMod b) { + u16 value = a.Unsigned16(); + RegFromBus16(b.GetName(), value); + } + void mov_prpage(Imm4 a) { + regs.prpage = a.Unsigned16(); + } + + void mov_a0h_stepi0() { + u16 value = RegToBus16(RegName::a0h, true); + regs.stepi0 = value; + } + void mov_a0h_stepj0() { + u16 value = RegToBus16(RegName::a0h, true); + regs.stepj0 = value; + } + void mov_stepi0_a0h() { + u16 value = regs.stepi0; + RegFromBus16(RegName::a0h, value); + } + void mov_stepj0_a0h() { + u16 value = regs.stepj0; + RegFromBus16(RegName::a0h, value); + } + + void mov_prpage(Abl a) { + regs.prpage = (u16)(GetAcc(a.GetName()) & 0xF); // ? + } + void mov_repc(Abl a) { + u16 value = RegToBus16(a.GetName(), true); + regs.repc = value; + } + void mov(Abl a, ArArp b) { + u16 value = RegToBus16(a.GetName(), true); + RegFromBus16(b.GetName(), value); + } + void mov(Abl a, SttMod b) { + u16 value = RegToBus16(a.GetName(), true); + RegFromBus16(b.GetName(), value); + } + + void mov_prpage_to(Abl b) { + RegFromBus16(b.GetName(), regs.prpage); // ? + } + void mov_repc_to(Abl b) { + u16 value = regs.repc; + RegFromBus16(b.GetName(), value); + } + void mov(ArArp a, Abl b) { + u16 value = RegToBus16(a.GetName()); + RegFromBus16(b.GetName(), value); + } + void mov(SttMod a, Abl b) { + u16 value = RegToBus16(a.GetName()); + RegFromBus16(b.GetName(), value); + } + + void mov_repc_to(ArRn1 b, ArStep1 bs) { + u16 address = RnAddressAndModify(GetArRnUnit(b), GetArStep(bs)); + u16 value = regs.repc; + mem.DataWrite(address, value); + } + void mov(ArArp a, ArRn1 b, ArStep1 bs) { + u16 address = RnAddressAndModify(GetArRnUnit(b), GetArStep(bs)); + u16 value = RegToBus16(a.GetName()); + mem.DataWrite(address, value); + } + void mov(SttMod a, ArRn1 b, ArStep1 bs) { + u16 address = RnAddressAndModify(GetArRnUnit(b), GetArStep(bs)); + u16 value = RegToBus16(a.GetName()); + mem.DataWrite(address, value); + } + + void mov_repc(ArRn1 a, ArStep1 as) { + u16 address = RnAddressAndModify(GetArRnUnit(a), GetArStep(as)); + u16 value = mem.DataRead(address); + regs.repc = value; + } + void mov(ArRn1 a, ArStep1 as, ArArp b) { + u16 address = RnAddressAndModify(GetArRnUnit(a), GetArStep(as)); + u16 value = mem.DataRead(address); + RegFromBus16(b.GetName(), value); + } + void mov(ArRn1 a, ArStep1 as, SttMod b) { + u16 address = RnAddressAndModify(GetArRnUnit(a), GetArStep(as)); + u16 value = mem.DataRead(address); + RegFromBus16(b.GetName(), value); + } + + void mov_repc_to(MemR7Imm16 b) { + u16 value = regs.repc; + StoreToMemory(b, value); + } + void mov(ArArpSttMod a, MemR7Imm16 b) { + u16 value = RegToBus16(a.GetName()); + StoreToMemory(b, value); + } + + void mov_repc(MemR7Imm16 a) { + u16 value = LoadFromMemory(a); + regs.repc = value; + } + void mov(MemR7Imm16 a, ArArpSttMod b) { + u16 value = LoadFromMemory(a); + RegFromBus16(b.GetName(), value); + } + + void mov_pc(Ax a) { + u64 value = GetAcc(a.GetName()); + SetPC(value & 0xFFFFFFFF); + } + void mov_pc(Bx a) { + u64 value = GetAcc(a.GetName()); + SetPC(value & 0xFFFFFFFF); + } + + void mov_mixp_to(Bx b) { + u16 value = regs.mixp; + RegFromBus16(b.GetName(), value); + } + void mov_mixp_r6() { + u16 value = regs.mixp; + regs.r[6] = value; + } + void mov_p0h_to(Bx b) { + u16 value = (ProductToBus40(Px{0}) >> 16) & 0xFFFF; + RegFromBus16(b.GetName(), value); + } + void mov_p0h_r6() { + u16 value = (ProductToBus40(Px{0}) >> 16) & 0xFFFF; + regs.r[6] = value; + } + void mov_p0h_to(Register b) { + u16 value = (ProductToBus40(Px{0}) >> 16) & 0xFFFF; + RegFromBus16(b.GetName(), value); + } + void mov_p0(Ab a) { + u32 value = GetAndSatAcc(a.GetName()) & 0xFFFFFFFF; + ProductFromBus32(Px{0}, value); + } + void mov_p1_to(Ab b) { + u64 value = ProductToBus40(Px{1}); + SatAndSetAccAndFlag(b.GetName(), value); + } + + void mov2(Px a, ArRn2 b, ArStep2 bs) { + u32 value = ProductToBus32_NoShift(a); + u16 l = value & 0xFFFF; + u16 h = (value >> 16) & 0xFFFF; + u16 unit = GetArRnUnit(b); + u16 address = RnAddressAndModify(unit, GetArStep(bs)); + u16 address2 = OffsetAddress(unit, address, GetArOffset(bs)); + // NOTE: keep the write order exactly like this. + mem.DataWrite(address2, l); + mem.DataWrite(address, h); + } + void mov2s(Px a, ArRn2 b, ArStep2 bs) { + u64 value = ProductToBus40(a); + u16 l = value & 0xFFFF; + u16 h = (value >> 16) & 0xFFFF; + u16 unit = GetArRnUnit(b); + u16 address = RnAddressAndModify(unit, GetArStep(bs)); + u16 address2 = OffsetAddress(unit, address, GetArOffset(bs)); + // NOTE: keep the write order exactly like this. + mem.DataWrite(address2, l); + mem.DataWrite(address, h); + } + void mov2(ArRn2 a, ArStep2 as, Px b) { + u16 unit = GetArRnUnit(a); + u16 address = RnAddressAndModify(unit, GetArStep(as)); + u16 address2 = OffsetAddress(unit, address, GetArOffset(as)); + u16 l = mem.DataRead(address2); + u16 h = mem.DataRead(address); + u32 value = ((u32)h << 16) | l; + ProductFromBus32(b, value); + } + void mova(Ab a, ArRn2 b, ArStep2 bs) { + u64 value = GetAndSatAcc(a.GetName()); + u16 l = value & 0xFFFF; + u16 h = (value >> 16) & 0xFFFF; + u16 unit = GetArRnUnit(b); + u16 address = RnAddressAndModify(unit, GetArStep(bs)); + u16 address2 = OffsetAddress(unit, address, GetArOffset(bs)); + // NOTE: keep the write order exactly like this. The second one overrides the first one if + // the offset is zero. + mem.DataWrite(address2, l); + mem.DataWrite(address, h); + } + void mova(ArRn2 a, ArStep2 as, Ab b) { + u16 unit = GetArRnUnit(a); + u16 address = RnAddressAndModify(unit, GetArStep(as)); + u16 address2 = OffsetAddress(unit, address, GetArOffset(as)); + u16 l = mem.DataRead(address2); + u16 h = mem.DataRead(address); + u64 value = SignExtend<32, u64>(((u64)h << 16) | l); + SatAndSetAccAndFlag(b.GetName(), value); + } + + void mov_r6_to(Bx b) { + u16 value = regs.r[6]; + RegFromBus16(b.GetName(), value); + } + void mov_r6_mixp() { + u16 value = regs.r[6]; + regs.mixp = value; + } + void mov_r6_to(Register b) { + u16 value = regs.r[6]; + RegFromBus16(b.GetName(), value); + } + void mov_r6(Register a) { + u16 value = RegToBus16(a.GetName(), true); + regs.r[6] = value; + } + void mov_memsp_r6() { + u16 value = mem.DataRead(regs.sp); + regs.r[6] = value; + } + void mov_r6_to(Rn b, StepZIDS bs) { + u16 value = regs.r[6]; + u16 address = RnAddressAndModify(b.Index(), bs.GetName()); + mem.DataWrite(address, value); + } + void mov_r6(Rn a, StepZIDS as) { + u16 address = RnAddressAndModify(a.Index(), as.GetName()); + u16 value = mem.DataRead(address); + regs.r[6] = value; + } + + void mov2_axh_m_y0_m(Axh a, ArRn2 b, ArStep2 bs) { + u16 u = (u16)((GetAndSatAccNoFlag(a.GetName()) >> 16) & 0xFFFF); + u16 v = regs.y[0]; + u16 unit = GetArRnUnit(b); + u16 ua = RnAddressAndModify(unit, GetArStep(bs)); + u16 va = OffsetAddress(unit, ua, GetArOffset(bs)); + // keep the order + mem.DataWrite(va, v); + mem.DataWrite(ua, u); + } + + void mov2_ax_mij(Ab a, ArpRn1 b, ArpStep1 bsi, ArpStep1 bsj) { + auto [ui, uj] = GetArpRnUnit(b); + auto [si, sj] = GetArpStep(bsi, bsj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 value = GetAndSatAccNoFlag(a.GetName()); + mem.DataWrite(i, (u16)((value >> 16) & 0xFFFF)); + mem.DataWrite(j, (u16)(value & 0xFFFF)); + } + void mov2_ax_mji(Ab a, ArpRn1 b, ArpStep1 bsi, ArpStep1 bsj) { + auto [ui, uj] = GetArpRnUnit(b); + auto [si, sj] = GetArpStep(bsi, bsj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 value = GetAndSatAccNoFlag(a.GetName()); + mem.DataWrite(j, (u16)((value >> 16) & 0xFFFF)); + mem.DataWrite(i, (u16)(value & 0xFFFF)); + } + void mov2_mij_ax(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + u16 h = mem.DataRead(RnAddressAndModify(ui, si)); + u16 l = mem.DataRead(RnAddressAndModify(uj, sj)); + u64 value = SignExtend<32, u64>(((u64)h << 16) | l); + SetAcc(b.GetName(), value); + } + void mov2_mji_ax(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + u16 l = mem.DataRead(RnAddressAndModify(ui, si)); + u16 h = mem.DataRead(RnAddressAndModify(uj, sj)); + u64 value = SignExtend<32, u64>(((u64)h << 16) | l); + SetAcc(b.GetName(), value); + } + void mov2_abh_m(Abh ax, Abh ay, ArRn1 b, ArStep1 bs) { + u16 u = (u16)((GetAndSatAccNoFlag(ax.GetName()) >> 16) & 0xFFFF); + u16 v = (u16)((GetAndSatAccNoFlag(ay.GetName()) >> 16) & 0xFFFF); + u16 unit = GetArRnUnit(b); + u16 ua = RnAddressAndModify(unit, GetArStep(bs)); + u16 va = OffsetAddress(unit, ua, GetArOffset(bs)); + // keep the order + mem.DataWrite(va, v); + mem.DataWrite(ua, u); + } + void exchange_iaj(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + auto [ui, uj] = GetArpRnUnit(b); + auto [si, sj] = GetArpStep(bsi, bsj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 value = GetAndSatAccNoFlag(a.GetName()); + mem.DataWrite(j, (u16)((value >> 16) & 0xFFFF)); + value = SignExtend<32, u64>((u64)mem.DataRead(i) << 16); + SetAcc(a.GetName(), value); + } + void exchange_riaj(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + auto [ui, uj] = GetArpRnUnit(b); + auto [si, sj] = GetArpStep(bsi, bsj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 value = GetAndSatAccNoFlag(a.GetName()); + mem.DataWrite(j, (u16)((value >> 16) & 0xFFFF)); + value = SignExtend<32, u64>(((u64)mem.DataRead(i) << 16) | 0x8000); + SetAcc(a.GetName(), value); + } + void exchange_jai(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + auto [ui, uj] = GetArpRnUnit(b); + auto [si, sj] = GetArpStep(bsi, bsj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 value = GetAndSatAccNoFlag(a.GetName()); + mem.DataWrite(i, (u16)((value >> 16) & 0xFFFF)); + value = SignExtend<32, u64>((u64)mem.DataRead(j) << 16); + SetAcc(a.GetName(), value); + } + void exchange_rjai(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + auto [ui, uj] = GetArpRnUnit(b); + auto [si, sj] = GetArpStep(bsi, bsj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + u64 value = GetAndSatAccNoFlag(a.GetName()); + mem.DataWrite(i, (u16)((value >> 16) & 0xFFFF)); + value = SignExtend<32, u64>(((u64)mem.DataRead(j) << 16) | 0x8000); + SetAcc(a.GetName(), value); + } + + void ShiftBus40(u64 value, u16 sv, RegName dest) { + value &= 0xFF'FFFF'FFFF; + u64 original_sign = value >> 39; + if ((sv >> 15) == 0) { + // left shift + if (sv >= 40) { + if (regs.s == 0) { + regs.fv = value != 0; + if (regs.fv) { + regs.fvl = 1; + } + } + value = 0; + regs.fc0 = 0; + } else { + if (regs.s == 0) { + regs.fv = SignExtend<40>(value) != SignExtend(value, 40 - sv); + if (regs.fv) { + regs.fvl = 1; + } + } + value <<= sv; + regs.fc0 = (value & ((u64)1 << 40)) != 0; + } + } else { + // right shift + u16 nsv = ~sv + 1; + if (nsv >= 40) { + if (regs.s == 0) { + regs.fc0 = (value >> 39) & 1; + value = regs.fc0 ? 0xFF'FFFF'FFFF : 0; + } else { + value = 0; + regs.fc0 = 0; + } + } else { + regs.fc0 = (value & ((u64)1 << (nsv - 1))) != 0; + value >>= nsv; + if (regs.s == 0) { + value = SignExtend(value, 40 - nsv); + } + } + + if (regs.s == 0) { + regs.fv = 0; + } + } + + value = SignExtend<40>(value); + SetAccFlag(value); + if (regs.s == 0 && regs.sata == 0) { + if (regs.fv || SignExtend<32>(value) != value) { + regs.flm = 1; + value = original_sign == 1 ? 0xFFFF'FFFF'8000'0000 : 0x7FFF'FFFF; + } + } + SetAcc(dest, value); + } + + void movs(MemImm8 a, Ab b) { + u64 value = SignExtend<16, u64>(LoadFromMemory(a)); + u16 sv = regs.sv; + ShiftBus40(value, sv, b.GetName()); + } + void movs(Rn a, StepZIDS as, Ab b) { + u16 address = RnAddressAndModify(a.Index(), as.GetName()); + u64 value = SignExtend<16, u64>(mem.DataRead(address)); + u16 sv = regs.sv; + ShiftBus40(value, sv, b.GetName()); + } + void movs(Register a, Ab b) { + u64 value = SignExtend<16, u64>(RegToBus16(a.GetName())); + u16 sv = regs.sv; + ShiftBus40(value, sv, b.GetName()); + } + void movs_r6_to(Ax b) { + u64 value = SignExtend<16, u64>(regs.r[6]); + u16 sv = regs.sv; + ShiftBus40(value, sv, b.GetName()); + } + void movsi(RnOld a, Ab b, Imm5s s) { + u64 value = SignExtend<16, u64>(RegToBus16(a.GetName())); + u16 sv = s.Signed16(); + ShiftBus40(value, sv, b.GetName()); + } + + void movr(ArRn2 a, ArStep2 as, Abh b) { + u16 value16 = mem.DataRead(RnAddressAndModify(GetArRnUnit(a), GetArStep(as))); + u64 value = SignExtend<32, u64>((u64)value16 << 16); + u64 result = AddSub(value, 0x8000, false); + SatAndSetAccAndFlag(b.GetName(), result); + } + void movr(Rn a, StepZIDS as, Ax b) { + u16 value16 = mem.DataRead(RnAddressAndModify(a.Index(), as.GetName())); + // Do 16-bit arithmetic. Flag C is set according to bit 16 but Flag V is always cleared + // Looks like a hardware bug to me + u64 result = (u64)value16 + 0x8000; + regs.fc0 = (u16)(result >> 16); + regs.fv = 0; + result &= 0xFFFF; + SatAndSetAccAndFlag(b.GetName(), result); + } + void movr(Register a, Ax b) { + u64 result; + if (a.GetName() == RegName::a0 || a.GetName() == RegName::a1) { + u64 value = GetAcc(a.GetName()); + result = AddSub(value, 0x8000, false); + } else if (a.GetName() == RegName::p) { + u64 value = ProductToBus40(Px{0}); + result = AddSub(value, 0x8000, false); + } else { + u16 value16 = RegToBus16(a.GetName()); + result = (u64)value16 + 0x8000; + regs.fc0 = (u16)(result >> 16); + regs.fv = 0; + result &= 0xFFFF; + } + SatAndSetAccAndFlag(b.GetName(), result); + } + void movr(Bx a, Ax b) { + u64 value = GetAcc(a.GetName()); + u64 result = AddSub(value, 0x8000, false); + SatAndSetAccAndFlag(b.GetName(), result); + } + void movr_r6_to(Ax b) { + u16 value16 = regs.r[6]; + u64 result = (u64)value16 + 0x8000; + regs.fc0 = (u16)(result >> 16); + regs.fv = 0; + result &= 0xFFFF; + SatAndSetAccAndFlag(b.GetName(), result); + } + + u16 Exp(u64 value) { + u64 sign = (value >> 39) & 1; + u16 bit = 38, count = 0; + while (true) { + if (((value >> bit) & 1) != sign) + break; + ++count; + if (bit == 0) + break; + --bit; + } + return count - 8; + } + + void ExpStore(Ax b) { + SetAcc(b.GetName(), SignExtend<16, u64>(regs.sv)); + } + + void exp(Bx a) { + u64 value = GetAcc(a.GetName()); + regs.sv = Exp(value); + } + void exp(Bx a, Ax b) { + exp(a); + ExpStore(b); + } + void exp(Rn a, StepZIDS as) { + u16 address = RnAddressAndModify(a.Index(), as.GetName()); + u64 value = SignExtend<32>((u64)mem.DataRead(address) << 16); + regs.sv = Exp(value); + } + void exp(Rn a, StepZIDS as, Ax b) { + exp(a, as); + ExpStore(b); + } + void exp(Register a) { + u64 value; + if (a.GetName() == RegName::a0 || a.GetName() == RegName::a1) { + value = GetAcc(a.GetName()); + } else { + // RegName::p follows the usual rule + value = SignExtend<32>((u64)RegToBus16(a.GetName()) << 16); + } + regs.sv = Exp(value); + } + void exp(Register a, Ax b) { + exp(a); + ExpStore(b); + } + void exp_r6() { + u64 value = SignExtend<32>((u64)RegToBus16(RegName::r6) << 16); + regs.sv = Exp(value); + } + void exp_r6(Ax b) { + exp_r6(); + ExpStore(b); + } + + void lim(Ax a, Ax b) { + u64 value = GetAcc(a.GetName()); + value = SaturateAcc(value); + SetAccAndFlag(b.GetName(), value); + } + + void vtrclr0() { + regs.vtr0 = 0; + } + void vtrclr1() { + regs.vtr1 = 0; + } + void vtrclr() { + regs.vtr0 = 0; + regs.vtr1 = 0; + } + void vtrmov0(Axl a) { + SatAndSetAccAndFlag(a.GetName(), regs.vtr0); + } + void vtrmov1(Axl a) { + SatAndSetAccAndFlag(a.GetName(), regs.vtr1); + } + void vtrmov(Axl a) { + SatAndSetAccAndFlag(a.GetName(), (regs.vtr1 & 0xFF00) | (regs.vtr0 >> 8)); + } + void vtrshr() { + // TODO: This instruction has one cycle delay on vtr0, but not on vtr1 + regs.vtr0 = (regs.vtr0 >> 1) | (regs.fc0 << 15); + regs.vtr1 = (regs.vtr1 >> 1) | (regs.fc1 << 15); + } + + void clrp0() { + ProductFromBus32(Px{0}, 0); + } + void clrp1() { + ProductFromBus32(Px{1}, 0); + } + void clrp() { + ProductFromBus32(Px{0}, 0); + ProductFromBus32(Px{1}, 0); + } + + void max_ge(Ax a, StepZIDS bs) { + u64 u = GetAcc(a.GetName()); + u64 v = GetAcc(CounterAcc(a.GetName())); + u64 d = v - u; + u16 r0 = RnAndModify(0, bs.GetName()); + if (((d >> 63) & 1) == 0) { + regs.fm = 1; + regs.mixp = r0; + SetAcc(a.GetName(), v); + } else { + regs.fm = 0; + } + } + void max_gt(Ax a, StepZIDS bs) { + u64 u = GetAcc(a.GetName()); + u64 v = GetAcc(CounterAcc(a.GetName())); + u64 d = v - u; + u16 r0 = RnAndModify(0, bs.GetName()); + if (((d >> 63) & 1) == 0 && d != 0) { + regs.fm = 1; + regs.mixp = r0; + SetAcc(a.GetName(), v); + } else { + regs.fm = 0; + } + } + void min_le(Ax a, StepZIDS bs) { + u64 u = GetAcc(a.GetName()); + u64 v = GetAcc(CounterAcc(a.GetName())); + u64 d = v - u; + u16 r0 = RnAndModify(0, bs.GetName()); + if (((d >> 63) & 1) == 1 || d == 0) { + regs.fm = 1; + regs.mixp = r0; + SetAcc(a.GetName(), v); + } else { + regs.fm = 0; + } + } + void min_lt(Ax a, StepZIDS bs) { + u64 u = GetAcc(a.GetName()); + u64 v = GetAcc(CounterAcc(a.GetName())); + u64 d = v - u; + u16 r0 = RnAndModify(0, bs.GetName()); + if (((d >> 63) & 1) == 1) { + regs.fm = 1; + regs.mixp = r0; + SetAcc(a.GetName(), v); + } else { + regs.fm = 0; + } + } + + void max_ge_r0(Ax a, StepZIDS bs) { + u64 u = GetAcc(a.GetName()); + u16 r0 = RnAndModify(0, bs.GetName()); + u64 v = SignExtend<16, u64>(mem.DataRead(RnAddress(0, r0))); + u64 d = v - u; + if (((d >> 63) & 1) == 0) { + regs.fm = 1; + regs.mixp = r0; + SetAcc(a.GetName(), v); + } else { + regs.fm = 0; + } + } + void max_gt_r0(Ax a, StepZIDS bs) { + u64 u = GetAcc(a.GetName()); + u16 r0 = RnAndModify(0, bs.GetName()); + u64 v = SignExtend<16, u64>(mem.DataRead(RnAddress(0, r0))); + u64 d = v - u; + if (((d >> 63) & 1) == 0 && d != 0) { + regs.fm = 1; + regs.mixp = r0; + SetAcc(a.GetName(), v); + } else { + regs.fm = 0; + } + } + void min_le_r0(Ax a, StepZIDS bs) { + u64 u = GetAcc(a.GetName()); + u16 r0 = RnAndModify(0, bs.GetName()); + u64 v = SignExtend<16, u64>(mem.DataRead(RnAddress(0, r0))); + u64 d = v - u; + if (((d >> 63) & 1) == 1 || d == 0) { + regs.fm = 1; + regs.mixp = r0; + SetAcc(a.GetName(), v); + } else { + regs.fm = 0; + } + } + void min_lt_r0(Ax a, StepZIDS bs) { + u64 u = GetAcc(a.GetName()); + u16 r0 = RnAndModify(0, bs.GetName()); + u64 v = SignExtend<16, u64>(mem.DataRead(RnAddress(0, r0))); + u64 d = v - u; + if (((d >> 63) & 1) == 1) { + regs.fm = 1; + regs.mixp = r0; + SetAcc(a.GetName(), v); + } else { + regs.fm = 0; + } + } + + void divs(MemImm8 a, Ax b) { + u16 da = LoadFromMemory(a); + u64 db = GetAcc(b.GetName()); + u64 value = db - ((u64)da << 15); + if (value >> 63) { + SetAccAndFlag(b.GetName(), SignExtend<40>(db << 1)); + } else { + SetAccAndFlag(b.GetName(), SignExtend<40>((value << 1) + 1)); + } + } + + void sqr_sqr_add3(Ab a, Ab b) { + u64 value = GetAcc(a.GetName()); + app(b, SumBase::Acc, false, false, false, false); + regs.x[0] = regs.y[0] = (u16)((value >> 16) & 0xFFFF); + regs.x[1] = regs.y[1] = (u16)(value & 0xFFFF); + DoMultiplication(0, true, true); + DoMultiplication(1, true, true); + } + + void sqr_sqr_add3(ArRn2 a, ArStep2 as, Ab b) { + app(b, SumBase::Acc, false, false, false, false); + u16 unit = GetArRnUnit(a); + u16 address0 = RnAddressAndModify(unit, GetArStep(as)); + u16 address1 = OffsetAddress(unit, address0, GetArOffset(as)); + regs.x[0] = regs.y[0] = mem.DataRead(address0); + regs.x[1] = regs.y[1] = mem.DataRead(address1); + DoMultiplication(0, true, true); + DoMultiplication(1, true, true); + } + + void sqr_mpysu_add3a(Ab a, Ab b) { + u64 value = GetAcc(a.GetName()); + app(b, SumBase::Acc, false, false, false, true); + regs.x[0] = regs.y[0] = regs.y[1] = (u16)((value >> 16) & 0xFFFF); + regs.x[1] = (u16)(value & 0xFFFF); + DoMultiplication(0, true, true); + DoMultiplication(1, false, true); + } + + void cmp(Ax a, Bx b) { + u64 va = GetAcc(a.GetName()); + u64 vb = GetAcc(b.GetName()); + SetAccFlag(AddSub(vb, va, true)); + } + void cmp_b0_b1() { + u64 va = GetAcc(RegName::b0); + u64 vb = GetAcc(RegName::b1); + SetAccFlag(AddSub(vb, va, true)); + } + void cmp_b1_b0() { + u64 va = GetAcc(RegName::b1); + u64 vb = GetAcc(RegName::b0); + SetAccFlag(AddSub(vb, va, true)); + } + void cmp(Bx a, Ax b) { + u64 va = GetAcc(a.GetName()); + u64 vb = GetAcc(b.GetName()); + SetAccFlag(AddSub(vb, va, true)); + } + void cmp_p1_to(Ax b) { + u64 va = ProductToBus40(Px{1}); + u64 vb = GetAcc(b.GetName()); + SetAccFlag(AddSub(vb, va, true)); + } + + void MinMaxVtr(RegName a, RegName b, bool min) { + u64 u = GetAcc(a); + u64 v = GetAcc(b); + u64 uh = SignExtend<24, u64>(u >> 16); + u64 ul = SignExtend<16, u64>(u & 0xFFFF); + u64 vh = SignExtend<24, u64>(v >> 16); + u64 vl = SignExtend<16, u64>(v & 0xFFFF); + u64 wh = min ? uh - vh : vh - uh; + u64 wl = min ? ul - vl : vl - ul; + + regs.fc0 = !(wh >> 63); + regs.fc1 = !(wl >> 63); + + wh = regs.fc0 != 0 ? vh : uh; + wl = regs.fc1 != 0 ? vl : ul; + + u64 w = (wh << 16) | (wl & 0xFFFF); + SetAcc(a, w); + vtrshr(); + } + + void max2_vtr(Ax a) { + MinMaxVtr(a.GetName(), CounterAcc(a.GetName()), false); + } + void min2_vtr(Ax a) { + MinMaxVtr(a.GetName(), CounterAcc(a.GetName()), true); + } + void max2_vtr(Ax a, Bx b) { + MinMaxVtr(a.GetName(), b.GetName(), false); + } + void min2_vtr(Ax a, Bx b) { + MinMaxVtr(a.GetName(), b.GetName(), true); + } + void max2_vtr_movl(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + MinMaxVtr(a.GetName(), b.GetName(), false); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 address = RnAddressAndModify(GetArRnUnit(c), GetArStep(cs)); + u16 value16 = (u16)(value & 0xFFFF); + mem.DataWrite(address, value16); + } + void max2_vtr_movh(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + MinMaxVtr(a.GetName(), b.GetName(), false); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 address = RnAddressAndModify(GetArRnUnit(c), GetArStep(cs)); + u16 value16 = (u16)((value >> 16) & 0xFFFF); + mem.DataWrite(address, value16); + } + void max2_vtr_movl(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + MinMaxVtr(a.GetName(), b.GetName(), false); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 address = RnAddressAndModify(GetArRnUnit(c), GetArStep(cs)); + u16 value16 = (u16)(value & 0xFFFF); + mem.DataWrite(address, value16); + } + void max2_vtr_movh(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + MinMaxVtr(a.GetName(), b.GetName(), false); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 address = RnAddressAndModify(GetArRnUnit(c), GetArStep(cs)); + u16 value16 = (u16)((value >> 16) & 0xFFFF); + mem.DataWrite(address, value16); + } + void min2_vtr_movl(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + MinMaxVtr(a.GetName(), b.GetName(), true); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 address = RnAddressAndModify(GetArRnUnit(c), GetArStep(cs)); + u16 value16 = (u16)(value & 0xFFFF); + mem.DataWrite(address, value16); + } + void min2_vtr_movh(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + MinMaxVtr(a.GetName(), b.GetName(), true); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 address = RnAddressAndModify(GetArRnUnit(c), GetArStep(cs)); + u16 value16 = (u16)((value >> 16) & 0xFFFF); + mem.DataWrite(address, value16); + } + void min2_vtr_movl(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + MinMaxVtr(a.GetName(), b.GetName(), true); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 address = RnAddressAndModify(GetArRnUnit(c), GetArStep(cs)); + u16 value16 = (u16)(value & 0xFFFF); + mem.DataWrite(address, value16); + } + void min2_vtr_movh(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + MinMaxVtr(a.GetName(), b.GetName(), true); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 address = RnAddressAndModify(GetArRnUnit(c), GetArStep(cs)); + u16 value16 = (u16)((value >> 16) & 0xFFFF); + mem.DataWrite(address, value16); + } + void max2_vtr_movij(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + MinMaxVtr(a.GetName(), b.GetName(), false); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 h = (u16)((value >> 16) & 0xFFFF); + u16 l = (u16)(value & 0xFFFF); + auto [ui, uj] = GetArpRnUnit(c); + auto [si, sj] = GetArpStep(csi, csj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + mem.DataWrite(i, h); + mem.DataWrite(j, l); + } + void max2_vtr_movji(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + MinMaxVtr(a.GetName(), b.GetName(), false); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 h = (u16)((value >> 16) & 0xFFFF); + u16 l = (u16)(value & 0xFFFF); + auto [ui, uj] = GetArpRnUnit(c); + auto [si, sj] = GetArpStep(csi, csj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + mem.DataWrite(i, l); + mem.DataWrite(j, h); + } + void min2_vtr_movij(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + MinMaxVtr(a.GetName(), b.GetName(), true); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 h = (u16)((value >> 16) & 0xFFFF); + u16 l = (u16)(value & 0xFFFF); + auto [ui, uj] = GetArpRnUnit(c); + auto [si, sj] = GetArpStep(csi, csj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + mem.DataWrite(i, h); + mem.DataWrite(j, l); + } + void min2_vtr_movji(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + MinMaxVtr(a.GetName(), b.GetName(), true); + u64 value = GetAndSatAccNoFlag(CounterAcc(a.GetName())); + u16 h = (u16)((value >> 16) & 0xFFFF); + u16 l = (u16)(value & 0xFFFF); + auto [ui, uj] = GetArpRnUnit(c); + auto [si, sj] = GetArpStep(csi, csj); + u16 i = RnAddressAndModify(ui, si); + u16 j = RnAddressAndModify(uj, sj); + mem.DataWrite(i, l); + mem.DataWrite(j, h); + } + + template <typename ArpStepX> + void mov_sv_app(ArRn1 a, ArpStepX as, Bx b, SumBase base, bool sub_p0, bool p0_align, + bool sub_p1, bool p1_align) { + regs.sv = mem.DataRead(RnAddressAndModify(GetArRnUnit(a), GetArStep(as))); + ProductSum(base, b.GetName(), sub_p0, p0_align, sub_p1, p1_align); + } + + void CodebookSearch(u16 u, u16 v, u16 r, CbsCond c) { + u64 diff = ProductToBus40(Px{0}) - ProductToBus40(Px{1}); + bool cond; + switch (c.GetName()) { + case CbsCondValue::Ge: + cond = !(diff >> 63); + break; + case CbsCondValue::Gt: + cond = !(diff >> 63) && diff != 0; + break; + default: + UNREACHABLE(); + } + if (cond) { + regs.x[1] = regs.p0h_cbs; + regs.x[0] = regs.y[1]; + regs.mixp = r; + } + regs.y[0] = u; + u16 x0 = std::exchange(regs.x[0], regs.y[0]); + DoMultiplication(0, true, true); + regs.p0h_cbs = regs.y[0] = (u16)((ProductToBus40(Px{0}) >> 16) & 0xFFFF); + regs.x[0] = x0; + regs.y[1] = v; + DoMultiplication(0, true, true); + DoMultiplication(1, true, true); + } + + void cbs(Axh a, CbsCond c) { + u16 u = (u16)((GetAcc(a.GetName()) >> 16) & 0xFFFF); + u16 v = (u16)((GetAcc(CounterAcc(a.GetName())) >> 16) & 0xFFFF); + u16 r = regs.r[0]; + CodebookSearch(u, v, r, c); + } + void cbs(Axh a, Bxh b, CbsCond c) { + u16 u = (u16)((GetAcc(a.GetName()) >> 16) & 0xFFFF); + u16 v = (u16)((GetAcc(b.GetName()) >> 16) & 0xFFFF); + u16 r = regs.r[0]; + CodebookSearch(u, v, r, c); + } + void cbs(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, CbsCond c) { + auto [ui, uj] = GetArpRnUnit(a); + auto [si, sj] = GetArpStep(asi, asj); + u16 aip = RnAndModify(ui, si); + u16 ai = RnAddress(ui, aip); + u16 aj = RnAddressAndModify(uj, sj); + u16 u = mem.DataRead(ai); + u16 v = mem.DataRead(aj); + u16 r = aip; + CodebookSearch(u, v, r, c); + } + + void mma(RegName a, bool x0_sign, bool y0_sign, bool x1_sign, bool y1_sign, SumBase base, + bool sub_p0, bool p0_align, bool sub_p1, bool p1_align) { + ProductSum(base, a, sub_p0, p0_align, sub_p1, p1_align); + std::swap(regs.x[0], regs.x[1]); + DoMultiplication(0, x0_sign, y0_sign); + DoMultiplication(1, x1_sign, y1_sign); + } + + template <typename ArpRnX, typename ArpStepX> + void mma(ArpRnX xy, ArpStepX i, ArpStepX j, bool dmodi, bool dmodj, RegName a, bool x0_sign, + bool y0_sign, bool x1_sign, bool y1_sign, SumBase base, bool sub_p0, bool p0_align, + bool sub_p1, bool p1_align) { + ProductSum(base, a, sub_p0, p0_align, sub_p1, p1_align); + auto [ui, uj] = GetArpRnUnit(xy); + auto [si, sj] = GetArpStep(i, j); + auto [oi, oj] = GetArpOffset(i, j); + u16 x = RnAddressAndModify(ui, si, dmodi); + u16 y = RnAddressAndModify(uj, sj, dmodj); + regs.x[0] = mem.DataRead(x); + regs.y[0] = mem.DataRead(y); + regs.x[1] = mem.DataRead(OffsetAddress(ui, x, oi, dmodi)); + regs.y[1] = mem.DataRead(OffsetAddress(uj, y, oj, dmodj)); + DoMultiplication(0, x0_sign, y0_sign); + DoMultiplication(1, x1_sign, y1_sign); + } + + void mma_mx_xy(ArRn1 y, ArStep1 ys, RegName a, bool x0_sign, bool y0_sign, bool x1_sign, + bool y1_sign, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + ProductSum(base, a, sub_p0, p0_align, sub_p1, p1_align); + std::swap(regs.x[0], regs.x[1]); + regs.y[0] = mem.DataRead(RnAddressAndModify(GetArRnUnit(y), GetArStep(ys))); + DoMultiplication(0, x0_sign, y0_sign); + DoMultiplication(1, x1_sign, y1_sign); + } + + void mma_xy_mx(ArRn1 y, ArStep1 ys, RegName a, bool x0_sign, bool y0_sign, bool x1_sign, + bool y1_sign, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + ProductSum(base, a, sub_p0, p0_align, sub_p1, p1_align); + std::swap(regs.x[0], regs.x[1]); + regs.y[1] = mem.DataRead(RnAddressAndModify(GetArRnUnit(y), GetArStep(ys))); + DoMultiplication(0, x0_sign, y0_sign); + DoMultiplication(1, x1_sign, y1_sign); + } + + void mma_my_my(ArRn1 x, ArStep1 xs, RegName a, bool x0_sign, bool y0_sign, bool x1_sign, + bool y1_sign, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + ProductSum(base, a, sub_p0, p0_align, sub_p1, p1_align); + u16 unit = GetArRnUnit(x); + u16 address = RnAddressAndModify(unit, GetArStep(xs)); + regs.x[0] = mem.DataRead(address); + regs.x[1] = mem.DataRead(OffsetAddress(unit, address, GetArOffset(xs))); + DoMultiplication(0, x0_sign, y0_sign); + DoMultiplication(1, x1_sign, y1_sign); + } + + void mma_mov(Axh u, Bxh v, ArRn1 w, ArStep1 ws, RegName a, bool x0_sign, bool y0_sign, + bool x1_sign, bool y1_sign, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + u16 unit = GetArRnUnit(w); + u16 address = RnAddressAndModify(unit, GetArStep(ws)); + u16 u_value = (u16)((GetAndSatAccNoFlag(u.GetName()) >> 16) & 0xFFFF); + u16 v_value = (u16)((GetAndSatAccNoFlag(v.GetName()) >> 16) & 0xFFFF); + // keep the order like this + mem.DataWrite(OffsetAddress(unit, address, GetArOffset(ws)), v_value); + mem.DataWrite(address, u_value); + ProductSum(base, a, sub_p0, p0_align, sub_p1, p1_align); + std::swap(regs.x[0], regs.x[1]); + DoMultiplication(0, x0_sign, y0_sign); + DoMultiplication(1, x1_sign, y1_sign); + } + + void mma_mov(ArRn2 w, ArStep1 ws, RegName a, bool x0_sign, bool y0_sign, bool x1_sign, + bool y1_sign, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + u16 unit = GetArRnUnit(w); + u16 address = RnAddressAndModify(unit, GetArStep(ws)); + u16 u_value = (u16)((GetAndSatAccNoFlag(a) >> 16) & 0xFFFF); + u16 v_value = (u16)((GetAndSatAccNoFlag(CounterAcc(a)) >> 16) & 0xFFFF); + // keep the order like this + mem.DataWrite(OffsetAddress(unit, address, GetArOffset(ws)), v_value); + mem.DataWrite(address, u_value); + ProductSum(base, a, sub_p0, p0_align, sub_p1, p1_align); + std::swap(regs.x[0], regs.x[1]); + DoMultiplication(0, x0_sign, y0_sign); + DoMultiplication(1, x1_sign, y1_sign); + } + + void addhp(ArRn2 a, ArStep2 as, Px b, Ax c) { + u16 address = RnAddressAndModify(GetArRnUnit(a), GetArStep(as)); + u64 value = SignExtend<32, u64>(((u64)mem.DataRead(address) << 16) | 0x8000); + u64 p = ProductToBus40(b); + u64 result = AddSub(value, p, false); + SatAndSetAccAndFlag(c.GetName(), result); + } + + void mov_ext0(Imm8s a) { + regs.ext[0] = a.Signed16(); + } + void mov_ext1(Imm8s a) { + regs.ext[1] = a.Signed16(); + } + void mov_ext2(Imm8s a) { + regs.ext[2] = a.Signed16(); + } + void mov_ext3(Imm8s a) { + regs.ext[3] = a.Signed16(); + } + +private: + CoreTiming& core_timing; + RegisterState& regs; + MemoryInterface& mem; + + std::array<std::atomic<bool>, 3> interrupt_pending{{false, false, false}}; + std::atomic<bool> vinterrupt_pending{false}; + std::atomic<bool> vinterrupt_context_switch; + std::atomic<u32> vinterrupt_address; + + bool idle = false; + + u64 GetAcc(RegName name) const { + switch (name) { + case RegName::a0: + case RegName::a0h: + case RegName::a0l: + case RegName::a0e: + return regs.a[0]; + case RegName::a1: + case RegName::a1h: + case RegName::a1l: + case RegName::a1e: + return regs.a[1]; + case RegName::b0: + case RegName::b0h: + case RegName::b0l: + case RegName::b0e: + return regs.b[0]; + case RegName::b1: + case RegName::b1h: + case RegName::b1l: + case RegName::b1e: + return regs.b[1]; + default: + UNREACHABLE(); + } + } + + u64 SaturateAccNoFlag(u64 value) const { + if (value != SignExtend<32>(value)) { + if ((value >> 39) != 0) + return 0xFFFF'FFFF'8000'0000; + else + return 0x0000'0000'7FFF'FFFF; + } + return value; + } + + u64 SaturateAcc(u64 value) { + if (value != SignExtend<32>(value)) { + regs.flm = 1; + if ((value >> 39) != 0) + return 0xFFFF'FFFF'8000'0000; + else + return 0x0000'0000'7FFF'FFFF; + } + // note: flm doesn't change value otherwise + return value; + } + + u64 GetAndSatAcc(RegName name) { + u64 value = GetAcc(name); + if (!regs.sat) { + return SaturateAcc(value); + } + return value; + } + + u64 GetAndSatAccNoFlag(RegName name) const { + u64 value = GetAcc(name); + if (!regs.sat) { + return SaturateAccNoFlag(value); + } + return value; + } + + u16 RegToBus16(RegName reg, bool enable_sat_for_mov = false) { + switch (reg) { + case RegName::a0: + case RegName::a1: + case RegName::b0: + case RegName::b1: + // get aXl, but unlike using RegName::aXl, this does never saturate. + // This only happen to insturctions using "Register" operand, + // and doesn't apply to all instructions. Need test and special check. + return GetAcc(reg) & 0xFFFF; + case RegName::a0l: + case RegName::a1l: + case RegName::b0l: + case RegName::b1l: + if (enable_sat_for_mov) { + return GetAndSatAcc(reg) & 0xFFFF; + } + return GetAcc(reg) & 0xFFFF; + case RegName::a0h: + case RegName::a1h: + case RegName::b0h: + case RegName::b1h: + if (enable_sat_for_mov) { + return (GetAndSatAcc(reg) >> 16) & 0xFFFF; + } + return (GetAcc(reg) >> 16) & 0xFFFF; + case RegName::a0e: + case RegName::a1e: + case RegName::b0e: + case RegName::b1e: + UNREACHABLE(); + + case RegName::r0: + return regs.r[0]; + case RegName::r1: + return regs.r[1]; + case RegName::r2: + return regs.r[2]; + case RegName::r3: + return regs.r[3]; + case RegName::r4: + return regs.r[4]; + case RegName::r5: + return regs.r[5]; + case RegName::r6: + return regs.r[6]; + case RegName::r7: + return regs.r[7]; + + case RegName::y0: + return regs.y[0]; + case RegName::p: + // This only happen to insturctions using "Register" operand, + // and doesn't apply to all instructions. Need test and special check. + return (ProductToBus40(Px{0}) >> 16) & 0xFFFF; + + case RegName::pc: + UNREACHABLE(); + case RegName::sp: + return regs.sp; + case RegName::sv: + return regs.sv; + case RegName::lc: + return regs.Lc(); + + case RegName::ar0: + return regs.Get<ar0>(); + case RegName::ar1: + return regs.Get<ar1>(); + + case RegName::arp0: + return regs.Get<arp0>(); + case RegName::arp1: + return regs.Get<arp1>(); + case RegName::arp2: + return regs.Get<arp2>(); + case RegName::arp3: + return regs.Get<arp3>(); + + case RegName::ext0: + return regs.ext[0]; + case RegName::ext1: + return regs.ext[1]; + case RegName::ext2: + return regs.ext[2]; + case RegName::ext3: + return regs.ext[3]; + + case RegName::stt0: + return regs.Get<stt0>(); + case RegName::stt1: + return regs.Get<stt1>(); + case RegName::stt2: + return regs.Get<stt2>(); + + case RegName::st0: + return regs.Get<st0>(); + case RegName::st1: + return regs.Get<st1>(); + case RegName::st2: + return regs.Get<st2>(); + + case RegName::cfgi: + return regs.Get<cfgi>(); + case RegName::cfgj: + return regs.Get<cfgj>(); + + case RegName::mod0: + return regs.Get<mod0>(); + case RegName::mod1: + return regs.Get<mod1>(); + case RegName::mod2: + return regs.Get<mod2>(); + case RegName::mod3: + return regs.Get<mod3>(); + default: + UNREACHABLE(); + } + } + + void SetAccFlag(u64 value) { + regs.fz = value == 0; + regs.fm = (value >> 39) != 0; + regs.fe = value != SignExtend<32>(value); + u64 bit31 = (value >> 31) & 1; + u64 bit30 = (value >> 30) & 1; + regs.fn = regs.fz || (!regs.fe && (bit31 ^ bit30) != 0); + } + + void SetAcc(RegName name, u64 value) { + switch (name) { + case RegName::a0: + case RegName::a0h: + case RegName::a0l: + case RegName::a0e: + regs.a[0] = value; + break; + case RegName::a1: + case RegName::a1h: + case RegName::a1l: + case RegName::a1e: + regs.a[1] = value; + break; + case RegName::b0: + case RegName::b0h: + case RegName::b0l: + case RegName::b0e: + regs.b[0] = value; + break; + case RegName::b1: + case RegName::b1h: + case RegName::b1l: + case RegName::b1e: + regs.b[1] = value; + break; + default: + UNREACHABLE(); + } + } + + void SatAndSetAccAndFlag(RegName name, u64 value) { + SetAccFlag(value); + if (!regs.sata) { + value = SaturateAcc(value); + } + SetAcc(name, value); + } + + void SetAccAndFlag(RegName name, u64 value) { + SetAccFlag(value); + SetAcc(name, value); + } + + void RegFromBus16(RegName reg, u16 value) { + switch (reg) { + case RegName::a0: + case RegName::a1: + case RegName::b0: + case RegName::b1: + SatAndSetAccAndFlag(reg, SignExtend<16, u64>(value)); + break; + case RegName::a0l: + case RegName::a1l: + case RegName::b0l: + case RegName::b1l: + SatAndSetAccAndFlag(reg, (u64)value); + break; + case RegName::a0h: + case RegName::a1h: + case RegName::b0h: + case RegName::b1h: + SatAndSetAccAndFlag(reg, SignExtend<32, u64>(value << 16)); + break; + case RegName::a0e: + case RegName::a1e: + case RegName::b0e: + case RegName::b1e: + UNREACHABLE(); + + case RegName::r0: + regs.r[0] = value; + break; + case RegName::r1: + regs.r[1] = value; + break; + case RegName::r2: + regs.r[2] = value; + break; + case RegName::r3: + regs.r[3] = value; + break; + case RegName::r4: + regs.r[4] = value; + break; + case RegName::r5: + regs.r[5] = value; + break; + case RegName::r6: + regs.r[6] = value; + break; + case RegName::r7: + regs.r[7] = value; + break; + + case RegName::y0: + regs.y[0] = value; + break; + case RegName::p: // p0h + regs.pe[0] = value > 0x7FFF; + regs.p[0] = (regs.p[0] & 0xFFFF) | (value << 16); + break; + + case RegName::pc: + UNREACHABLE(); + case RegName::sp: + regs.sp = value; + break; + case RegName::sv: + regs.sv = value; + break; + case RegName::lc: + regs.Lc() = value; + break; + + case RegName::ar0: + regs.Set<ar0>(value); + break; + case RegName::ar1: + regs.Set<ar1>(value); + break; + + case RegName::arp0: + regs.Set<arp0>(value); + break; + case RegName::arp1: + regs.Set<arp1>(value); + break; + case RegName::arp2: + regs.Set<arp2>(value); + break; + case RegName::arp3: + regs.Set<arp3>(value); + break; + + case RegName::ext0: + regs.ext[0] = value; + break; + case RegName::ext1: + regs.ext[1] = value; + break; + case RegName::ext2: + regs.ext[2] = value; + break; + case RegName::ext3: + regs.ext[3] = value; + break; + + case RegName::stt0: + regs.Set<stt0>(value); + break; + case RegName::stt1: + regs.Set<stt1>(value); + break; + case RegName::stt2: + regs.Set<stt2>(value); + break; + + case RegName::st0: + regs.Set<st0>(value); + break; + case RegName::st1: + regs.Set<st1>(value); + break; + case RegName::st2: + regs.Set<st2>(value); + break; + + case RegName::cfgi: + regs.Set<cfgi>(value); + break; + case RegName::cfgj: + regs.Set<cfgj>(value); + break; + + case RegName::mod0: + regs.Set<mod0>(value); + break; + case RegName::mod1: + regs.Set<mod1>(value); + break; + case RegName::mod2: + regs.Set<mod2>(value); + break; + case RegName::mod3: + regs.Set<mod3>(value); + break; + default: + UNREACHABLE(); + } + } + + template <typename ArRnX> + u16 GetArRnUnit(ArRnX arrn) const { + static_assert(std::is_same_v<ArRnX, ArRn1> || std::is_same_v<ArRnX, ArRn2>); + return regs.arrn[arrn.Index()]; + } + + template <typename ArpRnX> + std::tuple<u16, u16> GetArpRnUnit(ArpRnX arprn) const { + static_assert(std::is_same_v<ArpRnX, ArpRn1> || std::is_same_v<ArpRnX, ArpRn2>); + return std::make_tuple(regs.arprni[arprn.Index()], regs.arprnj[arprn.Index()] + 4); + } + + static StepValue ConvertArStep(u16 arvalue) { + switch (arvalue) { + case 0: + return StepValue::Zero; + case 1: + return StepValue::Increase; + case 2: + return StepValue::Decrease; + case 3: + return StepValue::PlusStep; + case 4: + return StepValue::Increase2Mode1; + case 5: + return StepValue::Decrease2Mode1; + case 6: + return StepValue::Increase2Mode2; + case 7: + return StepValue::Decrease2Mode2; + default: + UNREACHABLE(); + } + } + + template <typename ArStepX> + StepValue GetArStep(ArStepX arstep) const { + static_assert(std::is_same_v<ArStepX, ArStep1> || std::is_same_v<ArStepX, ArStep1Alt> || + std::is_same_v<ArStepX, ArStep2>); + return ConvertArStep(regs.arstep[arstep.Index()]); + } + + template <typename ArpStepX> + std::tuple<StepValue, StepValue> GetArpStep(ArpStepX arpstepi, ArpStepX arpstepj) const { + static_assert(std::is_same_v<ArpStepX, ArpStep1> || std::is_same_v<ArpStepX, ArpStep2>); + return std::make_tuple(ConvertArStep(regs.arpstepi[arpstepi.Index()]), + ConvertArStep(regs.arpstepj[arpstepj.Index()])); + } + + enum class OffsetValue : u16 { + Zero = 0, + PlusOne = 1, + MinusOne = 2, + MinusOneDmod = 3, + }; + + template <typename ArStepX> + OffsetValue GetArOffset(ArStepX arstep) const { + static_assert(std::is_same_v<ArStepX, ArStep1> || std::is_same_v<ArStepX, ArStep2>); + return (OffsetValue)regs.aroffset[arstep.Index()]; + } + + template <typename ArpStepX> + std::tuple<OffsetValue, OffsetValue> GetArpOffset(ArpStepX arpstepi, ArpStepX arpstepj) const { + static_assert(std::is_same_v<ArpStepX, ArpStep1> || std::is_same_v<ArpStepX, ArpStep2>); + return std::make_tuple((OffsetValue)regs.arpoffseti[arpstepi.Index()], + (OffsetValue)regs.arpoffsetj[arpstepj.Index()]); + } + + u16 RnAddress(unsigned unit, unsigned value) { + u16 ret = value; + if (regs.br[unit] && !regs.m[unit]) { + ret = BitReverse(ret); + } + return ret; + } + + u16 RnAddressAndModify(unsigned unit, StepValue step, bool dmod = false) { + return RnAddress(unit, RnAndModify(unit, step, dmod)); + } + + u16 OffsetAddress(unsigned unit, u16 address, OffsetValue offset, bool dmod = false) { + if (offset == OffsetValue::Zero) + return address; + if (offset == OffsetValue::MinusOneDmod) { + return address - 1; + } + bool emod = regs.m[unit] & !regs.br[unit] & !dmod; + u16 mod = unit < 4 ? regs.modi : regs.modj; + u16 mask = 1; // mod = 0 still have one bit mask + for (unsigned i = 0; i < 9; ++i) { + mask |= mod >> i; + } + if (offset == OffsetValue::PlusOne) { + if (!emod) + return address + 1; + if ((address & mask) == mod) + return address & ~mask; + return address + 1; + } else { // OffsetValue::MinusOne + if (!emod) + return address - 1; + throw UnimplementedException(); + // TODO: sometimes this would return two addresses, + // neither of which is the original Rn value. + // This only happens for memory writing, but not for memory reading. + // Might be some undefined behaviour. + if ((address & mask) == 0) + return address | mod; + return address - 1; + } + } + + u16 StepAddress(unsigned unit, u16 address, StepValue step, bool dmod = false) { + u16 s; + bool legacy = regs.cmd; + bool step2_mode1 = false; + bool step2_mode2 = false; + switch (step) { + case StepValue::Zero: + s = 0; + break; + case StepValue::Increase: + s = 1; + break; + case StepValue::Decrease: + s = 0xFFFF; + break; + // TODO: Increase/Decrease2Mode1/2 sometimes have wrong result if Offset=+/-1. + // This however never happens with modr instruction. + case StepValue::Increase2Mode1: + s = 2; + step2_mode1 = !legacy; + break; + case StepValue::Decrease2Mode1: + s = 0xFFFE; + step2_mode1 = !legacy; + break; + case StepValue::Increase2Mode2: + s = 2; + step2_mode2 = !legacy; + break; + case StepValue::Decrease2Mode2: + s = 0xFFFE; + step2_mode2 = !legacy; + break; + case StepValue::PlusStep: { + if (regs.br[unit] && !regs.m[unit]) { + s = unit < 4 ? regs.stepi0 : regs.stepj0; + } else { + s = unit < 4 ? regs.stepi : regs.stepj; + s = SignExtend<7>(s); + } + if (regs.stp16 == 1 && !legacy) { + s = unit < 4 ? regs.stepi0 : regs.stepj0; + if (regs.m[unit]) { + s = SignExtend<9>(s); + } + } + break; + } + default: + UNREACHABLE(); + } + + if (s == 0) + return address; + + if (!dmod && !regs.br[unit] && regs.m[unit]) { + u16 mod = unit < 4 ? regs.modi : regs.modj; + + if (mod == 0) { + return address; + } + + if (mod == 1 && step2_mode2) { + return address; + } + + unsigned iteration = 1; + if (step2_mode1) { + iteration = 2; + s = SignExtend<15, u16>(s >> 1); + } + + for (unsigned i = 0; i < iteration; ++i) { + if (legacy || step2_mode2) { + bool negative = false; + u16 m = mod; + if (s >> 15) { + negative = true; + m |= ~s; + } else { + m |= s; + } + + u16 mask = (1 << std20::log2p1(m)) - 1; + u16 next; + if (!negative) { + if ((address & mask) == mod && (!step2_mode2 || mod != mask)) { + next = 0; + } else { + next = (address + s) & mask; + } + } else { + if ((address & mask) == 0 && (!step2_mode2 || mod != mask)) { + next = mod; + } else { + next = (address + s) & mask; + } + } + address &= ~mask; + address |= next; + } else { + u16 mask = (1 << std20::log2p1(mod)) - 1; + u16 next; + if (s < 0x8000) { + next = (address + s) & mask; + if (next == ((mod + 1) & mask)) { + next = 0; + } + } else { + next = address & mask; + if (next == 0) { + next = mod + 1; + } + next += s; + next &= mask; + } + address &= ~mask; + address |= next; + } + } + } else { + address += s; + } + return address; + } + + u16 RnAndModify(unsigned unit, StepValue step, bool dmod = false) { + u16 ret = regs.r[unit]; + if ((unit == 3 && regs.epi) || (unit == 7 && regs.epj)) { + if (step != StepValue::Increase2Mode1 && step != StepValue::Decrease2Mode1 && + step != StepValue::Increase2Mode2 && step != StepValue::Decrease2Mode2) { + regs.r[unit] = 0; + return ret; + } + } + regs.r[unit] = StepAddress(unit, regs.r[unit], step, dmod); + return ret; + } + + u32 ProductToBus32_NoShift(Px reg) const { + return regs.p[reg.Index()]; + } + + u64 ProductToBus40(Px reg) const { + u16 unit = reg.Index(); + u64 value = regs.p[unit] | ((u64)regs.pe[unit] << 32); + switch (regs.ps[unit]) { + case 0: + value = SignExtend<33>(value); + break; + case 1: + value >>= 1; + value = SignExtend<32>(value); + break; + case 2: + value <<= 1; + value = SignExtend<34>(value); + break; + case 3: + value <<= 2; + value = SignExtend<35>(value); + break; + } + return value; + } + + void ProductFromBus32(Px reg, u32 value) { + u16 unit = reg.Index(); + regs.p[unit] = value; + regs.pe[unit] = value >> 31; + } + + static RegName CounterAcc(RegName in) { + static std::unordered_map<RegName, RegName> map{ + {RegName::a0, RegName::a1}, {RegName::a1, RegName::a0}, + {RegName::b0, RegName::b1}, {RegName::b1, RegName::b0}, + {RegName::a0l, RegName::a1l}, {RegName::a1l, RegName::a0l}, + {RegName::b0l, RegName::b1l}, {RegName::b1l, RegName::b0l}, + {RegName::a0h, RegName::a1h}, {RegName::a1h, RegName::a0h}, + {RegName::b0h, RegName::b1h}, {RegName::b1h, RegName::b0h}, + {RegName::a0e, RegName::a1e}, {RegName::a1e, RegName::a0e}, + {RegName::b0e, RegName::b1e}, {RegName::b1e, RegName::b0e}, + }; + return map.at(in); + } + + const std::vector<Matcher<Interpreter>> decoders = GetDecoderTable<Interpreter>(); +}; + +} // namespace Teakra diff --git a/src/teakra/src/makedsp1/CMakeLists.txt b/src/teakra/src/makedsp1/CMakeLists.txt new file mode 100644 index 0000000..302ed9a --- /dev/null +++ b/src/teakra/src/makedsp1/CMakeLists.txt @@ -0,0 +1,11 @@ +include(CreateDirectoryGroups) + +add_executable(makedsp1 + main.cpp + sha256.cpp + sha256.h +) +create_target_directory_groups(makedsp1) +target_link_libraries(makedsp1 PRIVATE teakra) +target_include_directories(makedsp1 PRIVATE .) +target_compile_options(makedsp1 PRIVATE ${TEAKRA_CXX_FLAGS}) diff --git a/src/teakra/src/makedsp1/main.cpp b/src/teakra/src/makedsp1/main.cpp new file mode 100644 index 0000000..50063ae --- /dev/null +++ b/src/teakra/src/makedsp1/main.cpp @@ -0,0 +1,172 @@ +#include <cstdio> +#include <fstream> +#include <iostream> +#include <string> +#include "../common_types.h" +#include "../parser.h" +#include "sha256.h" + +template <typename T> +std::vector<u8> Sha256(const std::vector<T>& data) { + SHA256_CTX ctx; + sha256_init(&ctx); + sha256_update(&ctx, (const BYTE*)data.data(), data.size() * sizeof(T)); + std::vector<u8> result(0x20); + sha256_final(&ctx, result.data()); + return result; +} + +struct Segment { + std::vector<u16> data; + u8 memory_type; + u32 target; +}; + +std::vector<std::string> StringToTokens(const std::string& in) { + std::vector<std::string> out; + bool need_new = true; + for (char c : in) { + if (c == ' ' || c == '\t') { + need_new = true; + } else { + if (need_new) { + need_new = false; + out.push_back(""); + } + out.back() += c; + } + } + return out; +} + +int main(int argc, char** argv) { + auto parser = Teakra::GenerateParser(); + + if (argc < 3) { + printf("Not enough parameters\n"); + return -1; + } + + std::ifstream in(argv[1]); + if (!in.is_open()) { + printf("Failed to open input file\n"); + return -1; + } + + std::string line; + std::vector<Segment> segments; + int line_number = 0; + while (std::getline(in, line)) { + ++line_number; + auto comment_pos = line.find("//"); + if (comment_pos != std::string::npos) { + line.erase(comment_pos); + } + + auto expansion_pos = line.find("$"); + bool has_expansion = expansion_pos != std::string::npos; + u16 expansion_data; + if (has_expansion) { + if (line.size() - expansion_pos < 5) { + printf("%d: unexpected line break in expansion data\n", line_number); + return -1; + } + expansion_data = (u16)std::stoi(line.substr(expansion_pos + 1, 4), 0, 16); + line = line.substr(0, expansion_pos) + "0000" + line.substr(expansion_pos + 5); + } + + auto tokens = StringToTokens(line); + if (tokens.size() == 0) + continue; + + if (tokens[0] == "segment") { + if (tokens.size() != 3) { + printf("%d: Wrong parameter count for 'segment'\n", line_number); + return -1; + } + Segment s; + if (tokens[1] == "p") { + s.memory_type = 0; + } else if (tokens[1] == "d") { + s.memory_type = 2; + } else { + printf("%d: Unknown segment type %s\n", line_number, tokens[1].c_str()); + return -1; + } + + s.target = std::stoi(tokens[2], 0, 16); + segments.push_back(s); + } else { + u16 v; + if (tokens[0] == "data") { + if (tokens.size() != 2) { + printf("%d: Wrong parameter count for 'data'\n", line_number); + return -1; + } + v = (u16)std::stoi(tokens[1], 0, 16); + segments.back().data.push_back(v); + } else { + auto maybe_v = parser->Parse(tokens); + if (maybe_v.status == Teakra::Parser::Opcode::Invalid) { + printf("%d: could not parse\n", line_number); + return -1; + } + v = maybe_v.opcode; + segments.back().data.push_back(v); + + if (maybe_v.status == Teakra::Parser::Opcode::ValidWithExpansion) { + if (!has_expansion) { + printf("%d: needs expansion\n", line_number); + return -1; + } + segments.back().data.push_back(expansion_data); + } else { + if (has_expansion) { + printf("%d: unexpected expansion\n", line_number); + return -1; + } + } + } + } + } + in.close(); + + FILE* out = fopen(argv[2], "wb"); + if (!out) { + printf("Failed to open output file\n"); + return -1; + } + + u32 data_ptr = 0x300; + + for (unsigned i = 0; i < segments.size(); ++i) { + fseek(out, 0x120 + i * 0x30, SEEK_SET); + fwrite(&data_ptr, 4, 1, out); + fwrite(&segments[i].target, 4, 1, out); + u32 size = (u32)segments[i].data.size() * 2; + fwrite(&size, 4, 1, out); + u32 memory_type = segments[i].memory_type << 24; + fwrite(&memory_type, 4, 1, out); + auto sha = Sha256(segments[i].data); + fwrite(sha.data(), 0x20, 1, out); + + fseek(out, data_ptr, SEEK_SET); + fwrite(segments[i].data.data(), size, 1, out); + data_ptr += size; + } + + fseek(out, 0x100, SEEK_SET); + fwrite("DSP1", 4, 1, out); + fwrite(&data_ptr, 4, 1, out); + u32 memory_layout = 0x0000FFFF; + fwrite(&memory_layout, 4, 1, out); + u32 misc = (u32)segments.size() << 16; + fwrite(&misc, 4, 1, out); + u32 zero = 0; + fwrite(&zero, 4, 1, out); + fwrite(&zero, 4, 1, out); + fwrite(&zero, 4, 1, out); + fwrite(&zero, 4, 1, out); + + fclose(out); +} diff --git a/src/teakra/src/makedsp1/sha256.cpp b/src/teakra/src/makedsp1/sha256.cpp new file mode 100644 index 0000000..70f9deb --- /dev/null +++ b/src/teakra/src/makedsp1/sha256.cpp @@ -0,0 +1,152 @@ +/********************************************************************* +* Filename: sha256.c +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Implementation of the SHA-256 hashing algorithm. + SHA-256 is one of the three algorithms in the SHA2 + specification. The others, SHA-384 and SHA-512, are not + offered in this implementation. + Algorithm specification can be found here: + * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf + This implementation uses little endian byte order. +*********************************************************************/ + +/*************************** HEADER FILES ***************************/ +#include <memory.h> +#include <stdlib.h> +#include "sha256.h" + +/****************************** MACROS ******************************/ +#define ROTLEFT(a, b) (((a) << (b)) | ((a) >> (32 - (b)))) +#define ROTRIGHT(a, b) (((a) >> (b)) | ((a) << (32 - (b)))) + +#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) +#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) +#define EP0(x) (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22)) +#define EP1(x) (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25)) +#define SIG0(x) (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3)) +#define SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10)) + +/**************************** VARIABLES *****************************/ +static const WORD k[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; + +/*********************** FUNCTION DEFINITIONS ***********************/ +void sha256_transform(SHA256_CTX* ctx, const BYTE data[]) { + WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; + + for (i = 0, j = 0; i < 16; ++i, j += 4) + m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]); + for (; i < 64; ++i) + m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; + + a = ctx->state[0]; + b = ctx->state[1]; + c = ctx->state[2]; + d = ctx->state[3]; + e = ctx->state[4]; + f = ctx->state[5]; + g = ctx->state[6]; + h = ctx->state[7]; + + for (i = 0; i < 64; ++i) { + t1 = h + EP1(e) + CH(e, f, g) + k[i] + m[i]; + t2 = EP0(a) + MAJ(a, b, c); + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + + ctx->state[0] += a; + ctx->state[1] += b; + ctx->state[2] += c; + ctx->state[3] += d; + ctx->state[4] += e; + ctx->state[5] += f; + ctx->state[6] += g; + ctx->state[7] += h; +} + +void sha256_init(SHA256_CTX* ctx) { + ctx->datalen = 0; + ctx->bitlen = 0; + ctx->state[0] = 0x6a09e667; + ctx->state[1] = 0xbb67ae85; + ctx->state[2] = 0x3c6ef372; + ctx->state[3] = 0xa54ff53a; + ctx->state[4] = 0x510e527f; + ctx->state[5] = 0x9b05688c; + ctx->state[6] = 0x1f83d9ab; + ctx->state[7] = 0x5be0cd19; +} + +void sha256_update(SHA256_CTX* ctx, const BYTE data[], size_t len) { + WORD i; + + for (i = 0; i < len; ++i) { + ctx->data[ctx->datalen] = data[i]; + ctx->datalen++; + if (ctx->datalen == 64) { + sha256_transform(ctx, ctx->data); + ctx->bitlen += 512; + ctx->datalen = 0; + } + } +} + +void sha256_final(SHA256_CTX* ctx, BYTE hash[]) { + WORD i; + + i = ctx->datalen; + + // Pad whatever data is left in the buffer. + if (ctx->datalen < 56) { + ctx->data[i++] = 0x80; + while (i < 56) + ctx->data[i++] = 0x00; + } else { + ctx->data[i++] = 0x80; + while (i < 64) + ctx->data[i++] = 0x00; + sha256_transform(ctx, ctx->data); + memset(ctx->data, 0, 56); + } + + // Append to the padding the total message's length in bits and transform. + ctx->bitlen += ctx->datalen * 8; + ctx->data[63] = (BYTE)ctx->bitlen; + ctx->data[62] = (BYTE)(ctx->bitlen >> 8); + ctx->data[61] = (BYTE)(ctx->bitlen >> 16); + ctx->data[60] = (BYTE)(ctx->bitlen >> 24); + ctx->data[59] = (BYTE)(ctx->bitlen >> 32); + ctx->data[58] = (BYTE)(ctx->bitlen >> 40); + ctx->data[57] = (BYTE)(ctx->bitlen >> 48); + ctx->data[56] = (BYTE)(ctx->bitlen >> 56); + sha256_transform(ctx, ctx->data); + + // Since this implementation uses little endian byte ordering and SHA uses big endian, + // reverse all the bytes when copying the final state to the output hash. + for (i = 0; i < 4; ++i) { + hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; + hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; + hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; + hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; + hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; + hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff; + hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff; + hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff; + } +} diff --git a/src/teakra/src/makedsp1/sha256.h b/src/teakra/src/makedsp1/sha256.h new file mode 100644 index 0000000..3c80787 --- /dev/null +++ b/src/teakra/src/makedsp1/sha256.h @@ -0,0 +1,34 @@ +/********************************************************************* + * Filename: sha256.h + * Author: Brad Conte (brad AT bradconte.com) + * Copyright: + * Disclaimer: This code is presented "as is" without any guarantees. + * Details: Defines the API for the corresponding SHA1 implementation. + *********************************************************************/ + +#ifndef SHA256_H +#define SHA256_H + +/*************************** HEADER FILES ***************************/ +#include <stddef.h> + +/****************************** MACROS ******************************/ +#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte +typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines + +typedef struct { + BYTE data[64]; + WORD datalen; + unsigned long long bitlen; + WORD state[8]; +} SHA256_CTX; + +/*********************** FUNCTION DECLARATIONS **********************/ +void sha256_init(SHA256_CTX* ctx); +void sha256_update(SHA256_CTX* ctx, const BYTE data[], size_t len); +void sha256_final(SHA256_CTX* ctx, BYTE hash[]); + +#endif // SHA256_H diff --git a/src/teakra/src/matcher.h b/src/teakra/src/matcher.h new file mode 100644 index 0000000..bb698ee --- /dev/null +++ b/src/teakra/src/matcher.h @@ -0,0 +1,65 @@ +#pragma once + +#include <algorithm> +#include <functional> +#include <vector> +#include "common_types.h" +#include "crash.h" + +struct Rejector { + u16 mask; + u16 unexpected; + bool Rejects(u16 instruction) const { + return (instruction & mask) == unexpected; + } +}; + +template <typename Visitor> +class Matcher { +public: + using visitor_type = Visitor; + using handler_return_type = typename Visitor::instruction_return_type; + using handler_function = std::function<handler_return_type(Visitor&, u16, u16)>; + + Matcher(const char* const name, u16 mask, u16 expected, bool expanded, handler_function func) + : name{name}, mask{mask}, expected{expected}, expanded{expanded}, fn{std::move(func)} {} + + static Matcher AllMatcher(handler_function func) { + return Matcher("*", 0, 0, false, std::move(func)); + } + + const char* GetName() const { + return name; + } + + bool NeedExpansion() const { + return expanded; + } + + bool Matches(u16 instruction) const { + return (instruction & mask) == expected && + std::none_of(rejectors.begin(), rejectors.end(), + [instruction](const Rejector& rejector) { + return rejector.Rejects(instruction); + }); + } + + Matcher Except(Rejector rejector) const { + Matcher new_matcher(*this); + new_matcher.rejectors.push_back(rejector); + return new_matcher; + } + + handler_return_type call(Visitor& v, u16 instruction, u16 instruction_expansion = 0) const { + ASSERT(Matches(instruction)); + return fn(v, instruction, instruction_expansion); + } + +private: + const char* name; + u16 mask; + u16 expected; + bool expanded; + handler_function fn; + std::vector<Rejector> rejectors; +}; diff --git a/src/teakra/src/memory_interface.cpp b/src/teakra/src/memory_interface.cpp new file mode 100644 index 0000000..ef30b95 --- /dev/null +++ b/src/teakra/src/memory_interface.cpp @@ -0,0 +1,57 @@ +#include "memory_interface.h" +#include "mmio.h" +#include "shared_memory.h" + +namespace Teakra { +MemoryInterface::MemoryInterface(SharedMemory& shared_memory, + MemoryInterfaceUnit& memory_interface_unit) + : shared_memory(shared_memory), memory_interface_unit(memory_interface_unit) {} + +void MemoryInterface::SetMMIO(MMIORegion& mmio) { + this->mmio = &mmio; +} + +u16 MemoryInterface::ProgramRead(u32 address) const { + return shared_memory.ReadWord(address); +} +void MemoryInterface::ProgramWrite(u32 address, u16 value) { + shared_memory.WriteWord(address, value); +} +u16 MemoryInterface::DataRead(u16 address, bool bypass_mmio) { + if (memory_interface_unit.InMMIO(address) && !bypass_mmio) { + ASSERT(mmio != nullptr); + return mmio->Read(memory_interface_unit.ToMMIO(address)); + } + u32 converted = memory_interface_unit.ConvertDataAddress(address); + u16 value = shared_memory.ReadWord(converted); + return value; +} +void MemoryInterface::DataWrite(u16 address, u16 value, bool bypass_mmio) { + if (memory_interface_unit.InMMIO(address) && !bypass_mmio) { + ASSERT(mmio != nullptr); + return mmio->Write(memory_interface_unit.ToMMIO(address), value); + } + u32 converted = memory_interface_unit.ConvertDataAddress(address); + shared_memory.WriteWord(converted, value); +} +u16 MemoryInterface::DataReadA32(u32 address) const { + u32 converted = (address & ((MemoryInterfaceUnit::DataMemoryBankSize*2)-1)) + + MemoryInterfaceUnit::DataMemoryOffset; + return shared_memory.ReadWord(converted); +} +void MemoryInterface::DataWriteA32(u32 address, u16 value) { + u32 converted = (address & ((MemoryInterfaceUnit::DataMemoryBankSize*2)-1)) + + MemoryInterfaceUnit::DataMemoryOffset; + shared_memory.WriteWord(converted, value); +} +u16 MemoryInterface::MMIORead(u16 address) { + ASSERT(mmio != nullptr); + // according to GBATek ("DSi Teak I/O Ports (on ARM9 Side)"), these are mirrored + return mmio->Read(address & (MemoryInterfaceUnit::MMIOSize - 1)); +} +void MemoryInterface::MMIOWrite(u16 address, u16 value) { + ASSERT(mmio != nullptr); + mmio->Write(address & (MemoryInterfaceUnit::MMIOSize - 1), value); +} + +} // namespace Teakra diff --git a/src/teakra/src/memory_interface.h b/src/teakra/src/memory_interface.h new file mode 100644 index 0000000..08c0ed5 --- /dev/null +++ b/src/teakra/src/memory_interface.h @@ -0,0 +1,73 @@ +#pragma once + +#include <array> +#include "common_types.h" +#include "crash.h" + +namespace Teakra { + +class MemoryInterfaceUnit { +public: + u16 x_page = 0, y_page = 0, z_page = 0; + static constexpr u16 XYSizeResolution = 0x400; + std::array<u16, 2> x_size{{0x20, 0x20}}; + std::array<u16, 2> y_size{{0x1E, 0x1E}}; + u16 page_mode = 0; + u16 mmio_base = 0x8000; + + static constexpr u16 MMIOSize = 0x0800; + static constexpr u32 DataMemoryOffset = 0x20000; + static constexpr u32 DataMemoryBankSize = 0x10000; + + void Reset() { + *this = MemoryInterfaceUnit(); + } + + bool InMMIO(u16 addr) const { + return addr >= mmio_base && addr < mmio_base + MMIOSize; + } + u16 ToMMIO(u16 addr) const { + ASSERT(z_page == 0); + // according to GBATek ("DSi Teak I/O Ports (on ARM9 Side)"), these are mirrored + return (addr - mmio_base) & (MMIOSize - 1); + } + + u32 ConvertDataAddress(u16 addr) const { + if (page_mode == 0) { + ASSERT(z_page < 2); + return DataMemoryOffset + addr + z_page * DataMemoryBankSize; + } else { + if (addr <= x_size[0] * XYSizeResolution) { + ASSERT(x_page < 2); + return DataMemoryOffset + addr + x_page * DataMemoryBankSize; + } else { + ASSERT(y_page < 2); + return DataMemoryOffset + addr + y_page * DataMemoryBankSize; + } + } + } +}; + +struct SharedMemory; +class MMIORegion; + +class MemoryInterface { +public: + MemoryInterface(SharedMemory& shared_memory, MemoryInterfaceUnit& memory_interface_unit); + void SetMMIO(MMIORegion& mmio); + u16 ProgramRead(u32 address) const; + void ProgramWrite(u32 address, u16 value); + u16 DataRead(u16 address, bool bypass_mmio = false); // not const because it can be a FIFO register + void DataWrite(u16 address, u16 value, bool bypass_mmio = false); + u16 DataReadA32(u32 address) const; + void DataWriteA32(u32 address, u16 value); + u16 MMIORead(u16 address); + void MMIOWrite(u16 address, u16 value); + +private: + SharedMemory& shared_memory; + MemoryInterfaceUnit& memory_interface_unit; + MMIORegion* mmio; +}; + +} // namespace Teakra diff --git a/src/teakra/src/miu.md b/src/teakra/src/miu.md new file mode 100644 index 0000000..28579ec --- /dev/null +++ b/src/teakra/src/miu.md @@ -0,0 +1,97 @@ +# MIU + +## MMIO Layout + +The following MMIO definition is extracted from Lauterbach's Teak debugger. Some of them are untested. +``` ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0100 | Z3WS | Z2WS | Z1WS | Z0WS | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0102 | | PRMWS | XYWS | ZDEFAULTWS | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0104 | Z0WSPAGE | Z0WSEA | Z0WSSA | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0106 | Z1WSPAGE | Z1WSEA | Z1WSSA | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0108 | Z2WSPAGE | Z2WSEA | Z2WSSA | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x010A | Z3WSPAGE | Z3WSEA | Z3WSSA | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x010C | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x010E | | XPAGE | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0110 | | YPAGE | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0112 | | ZPAGE | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0114 | | YPAGE0CFG | | XPAGE0CFG | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0116 | | YPAGE1CFG | | XPAGE1CFG | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0118 | | YOFFPAGECFG | | XOFFPAGECFG | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x011A | |PGM| |ZSP| |INP|TSP|PP | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x011C | |PDP| PDLPAGE |SDL|DLP| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x011E | MMIOBASE | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0120 | | OBSMOD |OBS| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0122 | |PRD|PZS|PXS|PXT|PZT|PZW|PZR| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +ZxWS: the number of wait-states for the x-th off-chip Z block +ZDEFAULTWS: the default number of wait-states for Z off-chip transactions +XYWS: the number of wait-states for X/Y off-chip memory transactions +PRMWS: the number of wait-states for off-chip program-memory transactions +ZxWSSA: the start address of the Z wait-states block #0 with a resolution of 1K-word. +ZxWSEA: the end address of the Z wait-states block #0 with a resolution of 1K-word +ZxWSPACE: the four LSBs of the Z wait-states block #0 page + +XPAGE: the X memory page when working in paging mode 1 +YPAGE: the Y memory page when working in paging mode 1 +ZPAGE: + - in paging mode 0: the absolute data memory page + - in paging mode 1: the Z memory page +XPAGE0CFG: + - in paging mode 0: the X memory size for page #0 + - in paging mode 1: the X memory size for all pages +YPAGE0CFG: + - in paging mode 0: the Y memory size for page #0 + - in paging mode 1: the Y memory size for all pages +XPAGE1CFG: in paging mode 0, the X memory size for page #1 +YPAGE1CFG: in paging mode 0, the Y memory size for page #1 +XOFFPAGECFG: in paging mode 0: the X memory size for all off-chip pages +YOFFPAGECFG: in paging mode 0: the Y memory size for all off-chip pages + +PP: 1 to enable the program protection mechanism +TSP: "Test Program", 1 to latch the value of the DAZXE[1]/TESTP strap pin during reset. It configures the entire program space as off-chip or based on the INTP bit +INP: "Internal Program", when a breakpoint interrupt occurs, the MIU forces a jump to page #1 (off-chip page) (when set to 0) or to page #0 (on-chip page) (when set to 1) and reads the Monitor program from this page +ZSP: 1 to enable sngle access mode, where the Z data memory space's Core/DMA addresses use only the Z-even address bus +PGM: paging mode + +DLP: "Download Memory Select", this bit is used to select between two, parallel Z-space data memories, including Regular and Download memories. 0 - ZRDEN, 1- ZBRDN +SDL: "Sticky Download Select", this bit is set one cycle after DLP is set. It is not cleared when clearing the DLP bit. It remains set until specifically writing low to it during a Trap routine +PDLPAGE: "Alternative Program Page", this field is an alternative paging register for program write (movd) and program read (movp) transactions +PDP: 1 to enable alternative program page + +MMIOBASE: MMIO base address, with resolution of 512-word. + +OBS: 1 to enable observability mode +OBSMOD: observability mode, defines which Core/DMA address/data buses are reflected on the off-chip XZ buses + - 0: Core XZ address/data buses + - 1: Core Y address/data buses + - 2: Core P address/data buses + - 3: DMA DST address/data buses + - 4: DMA SRC address/data buses + +PRD: Signal Polarity Bit - This bit defines the polarity of RD_WR +PZS: Z Select Polarity Bit - This bit defines the polarity of ZS +PXS: X Select Polarity Bit - This bit defines the polarity of XS +PXT: X Strobe Polarity Bit - This bit defines the polarity of XSTRB +PZT: Z Strobe Polarity Bit - This bit defines the polarity of ZSTRB +PZW: Z Write Polarity Bit - This bit defines the polarity of DWZON/DWZEN +PZR: Z Read Polarity Bit - This bit defines the polarity of DRZON/DRZEN +``` diff --git a/src/teakra/src/mmio.cpp b/src/teakra/src/mmio.cpp new file mode 100644 index 0000000..0a8e3bc --- /dev/null +++ b/src/teakra/src/mmio.cpp @@ -0,0 +1,361 @@ +#include <functional> +#include <string> +#include <type_traits> +#include <vector> +#include "ahbm.h" +#include "apbp.h" +#include "btdmp.h" +#include "dma.h" +#include "memory_interface.h" +#include "mmio.h" +#include "timer.h" + +namespace Teakra { + +auto NoSet(const std::string& debug_string) { + return [debug_string](u16) { printf("Warning: NoSet on %s\n", debug_string.data()); }; +} +auto NoGet(const std::string& debug_string) { + return [debug_string]() -> u16 { + printf("Warning: NoGet on %s\n", debug_string.data()); + return 0; + }; +} + +struct BitFieldSlot { + unsigned pos; + unsigned length; + std::function<void(u16)> set; + std::function<u16(void)> get; + + template <typename T> + static BitFieldSlot RefSlot(unsigned pos, unsigned length, T& var) { + static_assert( + std::is_same_v<u16, + typename std::conditional_t<std::is_enum_v<T>, std::underlying_type<T>, + std::enable_if<true, T>>::type>); + BitFieldSlot slot{pos, length, {}, {}}; + slot.set = [&var](u16 value) { var = static_cast<T>(value); }; + slot.get = [&var]() -> u16 { return static_cast<u16>(var); }; + return slot; + } +}; + +struct Cell { + std::function<void(u16)> set; + std::function<u16(void)> get; + u16 index = 0; + + Cell(std::function<void(u16)> set, std::function<u16(void)> get) + : set(std::move(set)), get(std::move(get)) {} + Cell() { + std::shared_ptr<u16> storage = std::make_shared<u16>(0); + set = [storage, this](u16 value) { + *storage = value; + std::printf("MMIO: cell %04X set = %04X\n", index, value); + }; + get = [storage, this]() -> u16 { + std::printf("MMIO: cell %04X get\n", index); + return *storage; + }; + } + static Cell ConstCell(u16 constant) { + Cell cell({}, {}); + cell.set = NoSet(""); + cell.get = [constant]() -> u16 { return constant; }; + return cell; + } + static Cell RefCell(u16& var) { + Cell cell({}, {}); + cell.set = [&var](u16 value) { var = value; }; + cell.get = [&var]() -> u16 { return var; }; + return cell; + } + + static Cell MirrorCell(Cell* mirror) { + Cell cell({}, {}); + cell.set = [mirror](u16 value) { mirror->set(value); }; + cell.get = [mirror]() -> u16 { return mirror->get(); }; + return cell; + } + + static Cell BitFieldCell(const std::vector<BitFieldSlot>& slots) { + Cell cell({}, {}); + std::shared_ptr<u16> storage = std::make_shared<u16>(0); + cell.set = [storage, slots](u16 value) { + for (const auto& slot : slots) { + if (slot.set) { + slot.set((value >> slot.pos) & ((1 << slot.length) - 1)); + } + } + *storage = value; + }; + cell.get = [storage, slots]() -> u16 { + u16 value = *storage; + for (const auto& slot : slots) { + if (slot.get) { + value &= ~(((1 << slot.length) - 1) << slot.pos); + value |= slot.get() << slot.pos; + } + } + return value; + }; + return cell; + } +}; + +class MMIORegion::Impl { +public: + std::array<Cell, 0x800> cells{}; + Impl() { + for (std::size_t i = 0; i < cells.size(); ++i) { + cells[i].index = (u16)i; + } + } +}; + +MMIORegion::MMIORegion(MemoryInterfaceUnit& miu, ICU& icu, Apbp& apbp_from_cpu, Apbp& apbp_from_dsp, + std::array<Timer, 2>& timer, Dma& dma, Ahbm& ahbm, + std::array<Btdmp, 2>& btdmp) + : impl(new Impl) { + using namespace std::placeholders; + + impl->cells[0x01A] = Cell::ConstCell(0xC902); // chip detect + + // Timer + for (unsigned i = 0; i < 2; ++i) { + impl->cells[0x20 + i * 0x10] = Cell::BitFieldCell({ + // TIMERx_CFG + BitFieldSlot::RefSlot(0, 2, timer[i].scale), // TS + BitFieldSlot::RefSlot(2, 3, timer[i].count_mode), // CM + BitFieldSlot{6, 1, {}, {}}, // TP + BitFieldSlot{7, 1, {}, {}}, // CT + BitFieldSlot::RefSlot(8, 1, timer[i].pause), // PC + BitFieldSlot::RefSlot(9, 1, timer[i].update_mmio), // MU + BitFieldSlot{10, 1, + [&timer, i](u16 v) { + if (v) + timer[i].Restart(); + }, + []() -> u16 { return 0; }}, // RES + BitFieldSlot{11, 1, {}, {}}, // BP + BitFieldSlot{12, 1, {}, {}}, // CS + BitFieldSlot{13, 1, {}, {}}, // GP + BitFieldSlot{14, 2, {}, {}}, // TM + }); + + impl->cells[0x22 + i * 0x10].set = [&timer, i](u16 v) { + if (v) + timer[i].TickEvent(); + }; // TIMERx_EW + impl->cells[0x22 + i * 0x10].get = []() -> u16 { return 0; }; + impl->cells[0x24 + i * 0x10] = Cell::RefCell(timer[i].start_low); // TIMERx_SCL + impl->cells[0x26 + i * 0x10] = Cell::RefCell(timer[i].start_high); // TIMERx_SCH + impl->cells[0x28 + i * 0x10] = Cell::RefCell(timer[i].counter_low); // TIMERx_CCL + impl->cells[0x2A + i * 0x10] = Cell::RefCell(timer[i].counter_high); // TIMERx_CCH + impl->cells[0x2C + i * 0x10] = Cell(); // TIMERx_SPWMCL + impl->cells[0x2E + i * 0x10] = Cell(); // TIMERx_SPWMCH + } + + // APBP + for (unsigned i = 0; i < 3; ++i) { + impl->cells[0x0C0 + i * 4].set = std::bind(&Apbp::SendData, &apbp_from_dsp, i, _1); + impl->cells[0x0C0 + i * 4].get = std::bind(&Apbp::PeekData, &apbp_from_dsp, i); + impl->cells[0x0C2 + i * 4].set = [](u16) {}; + impl->cells[0x0C2 + i * 4].get = std::bind(&Apbp::RecvData, &apbp_from_cpu, i); + } + impl->cells[0x0CC].set = std::bind(&Apbp::SetSemaphore, &apbp_from_dsp, _1); + impl->cells[0x0CC].get = std::bind(&Apbp::GetSemaphore, &apbp_from_dsp); + impl->cells[0x0CE].set = std::bind(&Apbp::MaskSemaphore, &apbp_from_cpu, _1); + impl->cells[0x0CE].get = std::bind(&Apbp::GetSemaphoreMask, &apbp_from_cpu); + impl->cells[0x0D0].set = std::bind(&Apbp::ClearSemaphore, &apbp_from_cpu, _1); + impl->cells[0x0D0].get = []() -> u16 { return 0; }; + impl->cells[0x0D2].set = [](u16) {}; + impl->cells[0x0D2].get = std::bind(&Apbp::GetSemaphore, &apbp_from_cpu); + impl->cells[0x0D4] = Cell::BitFieldCell({ + BitFieldSlot{2, 1, {}, {}}, // ARM side endianness flag + BitFieldSlot{8, 1, + [&apbp_from_cpu](u16 v) { return apbp_from_cpu.SetDisableInterrupt(0, v); }, + [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.GetDisableInterrupt(0); }}, + BitFieldSlot{12, 1, + [&apbp_from_cpu](u16 v) { return apbp_from_cpu.SetDisableInterrupt(1, v); }, + [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.GetDisableInterrupt(1); }}, + BitFieldSlot{13, 1, + [&apbp_from_cpu](u16 v) { return apbp_from_cpu.SetDisableInterrupt(2, v); }, + [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.GetDisableInterrupt(2); }}, + }); + impl->cells[0x0D6] = Cell::BitFieldCell({ + BitFieldSlot{5, 1, {}, [&apbp_from_dsp]() -> u16 { return apbp_from_dsp.IsDataReady(0); }}, + BitFieldSlot{6, 1, {}, [&apbp_from_dsp]() -> u16 { return apbp_from_dsp.IsDataReady(1); }}, + BitFieldSlot{7, 1, {}, [&apbp_from_dsp]() -> u16 { return apbp_from_dsp.IsDataReady(2); }}, + BitFieldSlot{8, 1, {}, [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.IsDataReady(0); }}, + BitFieldSlot{ + 9, 1, {}, [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.IsSemaphoreSignaled(); }}, + BitFieldSlot{12, 1, {}, [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.IsDataReady(1); }}, + BitFieldSlot{13, 1, {}, [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.IsDataReady(2); }}, + }); + + // This register is a mirror of CPU side register DSP_PSTS + impl->cells[0x0D8] = Cell::BitFieldCell({ + BitFieldSlot{ + 9, 1, {}, [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.IsSemaphoreSignaled(); }}, + BitFieldSlot{10, 1, {}, [&apbp_from_dsp]() -> u16 { return apbp_from_dsp.IsDataReady(0); }}, + BitFieldSlot{11, 1, {}, [&apbp_from_dsp]() -> u16 { return apbp_from_dsp.IsDataReady(1); }}, + BitFieldSlot{12, 1, {}, [&apbp_from_dsp]() -> u16 { return apbp_from_dsp.IsDataReady(2); }}, + BitFieldSlot{13, 1, {}, [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.IsDataReady(0); }}, + BitFieldSlot{14, 1, {}, [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.IsDataReady(1); }}, + BitFieldSlot{15, 1, {}, [&apbp_from_cpu]() -> u16 { return apbp_from_cpu.IsDataReady(2); }}, + }); + + // AHBM + impl->cells[0x0E0].set = NoSet("AHBM::BusyFlag"); + impl->cells[0x0E0].get = std::bind(&Ahbm::GetBusyFlag, &ahbm); + for (u16 i = 0; i < 3; ++i) { + impl->cells[0x0E2 + i * 6] = Cell::BitFieldCell({ + // BitFieldSlot{0, 1, ?, ?}, + BitFieldSlot{1, 2, std::bind(&Ahbm::SetBurstSize, &ahbm, i, _1), + std::bind(&Ahbm::GetBurstSize, &ahbm, i)}, + BitFieldSlot{4, 2, std::bind(&Ahbm::SetUnitSize, &ahbm, i, _1), + std::bind(&Ahbm::GetUnitSize, &ahbm, i)}, + }); + impl->cells[0x0E4 + i * 6] = Cell::BitFieldCell({ + BitFieldSlot{8, 1, std::bind(&Ahbm::SetDirection, &ahbm, i, _1), + std::bind(&Ahbm::GetDirection, &ahbm, i)}, + // BitFieldSlot{9, 1, ?, ?}, + }); + impl->cells[0x0E6 + i * 6].set = std::bind(&Ahbm::SetDmaChannel, &ahbm, i, _1); + impl->cells[0x0E6 + i * 6].get = std::bind(&Ahbm::GetDmaChannel, &ahbm, i); + } + + // MIU + // impl->cells[0x100]; // MIU_WSCFG0 + // impl->cells[0x102]; // MIU_WSCFG1 + // impl->cells[0x104]; // MIU_Z0WSCFG + // impl->cells[0x106]; // MIU_Z1WSCFG + // impl->cells[0x108]; // MIU_Z2WSCFG + // impl->cells[0x10C]; // MIU_Z3WSCFG + impl->cells[0x10E] = Cell::RefCell(miu.x_page); // MIU_XPAGE + impl->cells[0x110] = Cell::RefCell(miu.y_page); // MIU_YPAGE + impl->cells[0x112] = Cell::RefCell(miu.z_page); // MIU_ZPAGE + impl->cells[0x114] = Cell::BitFieldCell({ + // MIU_PAGE0CFG + BitFieldSlot::RefSlot(0, 6, miu.x_size[0]), + BitFieldSlot::RefSlot(8, 6, miu.y_size[0]), + }); + impl->cells[0x116] = Cell::BitFieldCell({ + // MIU_PAGE1CFG + BitFieldSlot::RefSlot(0, 6, miu.x_size[1]), + BitFieldSlot::RefSlot(8, 6, miu.y_size[1]), + }); + // impl->cells[0x118]; // MIU_OFFPAGECFG + impl->cells[0x11A] = Cell::BitFieldCell({ + BitFieldSlot{0, 1, {}, {}}, // PP + BitFieldSlot{1, 1, {}, {}}, // TESTP + BitFieldSlot{2, 1, {}, {}}, // INTP + BitFieldSlot{4, 1, {}, {}}, // ZSINGLEP + BitFieldSlot::RefSlot(6, 1, miu.page_mode), // PAGEMODE + }); + // impl->cells[0x11C]; // MIU_DLCFG + impl->cells[0x11E] = Cell::RefCell(miu.mmio_base); // MIU_MMIOBASE + // impl->cells[0x120]; // MIU_OBSCFG + // impl->cells[0x122]; // MIU_POLARITY + + // DMA + impl->cells[0x184].set = std::bind(&Dma::EnableChannel, &dma, _1); + impl->cells[0x184].get = std::bind(&Dma::GetChannelEnabled, &dma); + + impl->cells[0x18C].get = []() -> u16 { return 0xFFFF; }; // SEOX ? + + impl->cells[0x1BE].set = std::bind(&Dma::ActivateChannel, &dma, _1); + impl->cells[0x1BE].get = std::bind(&Dma::GetActiveChannel, &dma); + impl->cells[0x1C0].set = std::bind(&Dma::SetAddrSrcLow, &dma, _1); + impl->cells[0x1C0].get = std::bind(&Dma::GetAddrSrcLow, &dma); + impl->cells[0x1C2].set = std::bind(&Dma::SetAddrSrcHigh, &dma, _1); + impl->cells[0x1C2].get = std::bind(&Dma::GetAddrSrcHigh, &dma); + impl->cells[0x1C4].set = std::bind(&Dma::SetAddrDstLow, &dma, _1); + impl->cells[0x1C4].get = std::bind(&Dma::GetAddrDstLow, &dma); + impl->cells[0x1C6].set = std::bind(&Dma::SetAddrDstHigh, &dma, _1); + impl->cells[0x1C6].get = std::bind(&Dma::GetAddrDstHigh, &dma); + impl->cells[0x1C8].set = std::bind(&Dma::SetSize0, &dma, _1); + impl->cells[0x1C8].get = std::bind(&Dma::GetSize0, &dma); + impl->cells[0x1CA].set = std::bind(&Dma::SetSize1, &dma, _1); + impl->cells[0x1CA].get = std::bind(&Dma::GetSize1, &dma); + impl->cells[0x1CC].set = std::bind(&Dma::SetSize2, &dma, _1); + impl->cells[0x1CC].get = std::bind(&Dma::GetSize2, &dma); + impl->cells[0x1CE].set = std::bind(&Dma::SetSrcStep0, &dma, _1); + impl->cells[0x1CE].get = std::bind(&Dma::GetSrcStep0, &dma); + impl->cells[0x1D0].set = std::bind(&Dma::SetDstStep0, &dma, _1); + impl->cells[0x1D0].get = std::bind(&Dma::GetDstStep0, &dma); + impl->cells[0x1D2].set = std::bind(&Dma::SetSrcStep1, &dma, _1); + impl->cells[0x1D2].get = std::bind(&Dma::GetSrcStep1, &dma); + impl->cells[0x1D4].set = std::bind(&Dma::SetDstStep1, &dma, _1); + impl->cells[0x1D4].get = std::bind(&Dma::GetDstStep1, &dma); + impl->cells[0x1D6].set = std::bind(&Dma::SetSrcStep2, &dma, _1); + impl->cells[0x1D6].get = std::bind(&Dma::GetSrcStep2, &dma); + impl->cells[0x1D8].set = std::bind(&Dma::SetDstStep2, &dma, _1); + impl->cells[0x1D8].get = std::bind(&Dma::GetDstStep2, &dma); + impl->cells[0x1DA] = Cell::BitFieldCell({ + BitFieldSlot{0, 4, std::bind(&Dma::SetSrcSpace, &dma, _1), + std::bind(&Dma::GetSrcSpace, &dma)}, + BitFieldSlot{4, 4, std::bind(&Dma::SetDstSpace, &dma, _1), + std::bind(&Dma::GetDstSpace, &dma)}, + // BitFieldSlot{9, 1, ?, ?}, + BitFieldSlot{10, 1, std::bind(&Dma::SetDwordMode, &dma, _1), + std::bind(&Dma::GetDwordMode, &dma)}, + }); + impl->cells[0x1DC].set = std::bind(&Dma::SetY, &dma, _1); + impl->cells[0x1DC].get = std::bind(&Dma::GetY, &dma); + impl->cells[0x1DE].set = std::bind(&Dma::SetZ, &dma, _1); + impl->cells[0x1DE].get = std::bind(&Dma::GetZ, &dma); + + // ICU + impl->cells[0x200].set = NoSet("ICU::GetRequest"); + impl->cells[0x200].get = std::bind(&ICU::GetRequest, &icu); + impl->cells[0x202].set = std::bind(&ICU::Acknowledge, &icu, _1); + impl->cells[0x202].get = std::bind(&ICU::GetAcknowledge, &icu); + impl->cells[0x204].set = std::bind(&ICU::Trigger, &icu, _1); + impl->cells[0x204].get = std::bind(&ICU::GetTrigger, &icu); + impl->cells[0x206].set = std::bind(&ICU::SetEnable, &icu, 0, _1); + impl->cells[0x206].get = std::bind(&ICU::GetEnable, &icu, 0); + impl->cells[0x208].set = std::bind(&ICU::SetEnable, &icu, 1, _1); + impl->cells[0x208].get = std::bind(&ICU::GetEnable, &icu, 1); + impl->cells[0x20A].set = std::bind(&ICU::SetEnable, &icu, 2, _1); + impl->cells[0x20A].get = std::bind(&ICU::GetEnable, &icu, 2); + impl->cells[0x20C].set = std::bind(&ICU::SetEnableVectored, &icu, _1); + impl->cells[0x20C].get = std::bind(&ICU::GetEnableVectored, &icu); + // impl->cells[0x20E]; // polarity for each interrupt? + // impl->cells[0x210]; // source type for each interrupt? + for (unsigned i = 0; i < 16; ++i) { + impl->cells[0x212 + i * 4] = Cell::BitFieldCell({ + BitFieldSlot::RefSlot(0, 2, icu.vector_high[i]), + BitFieldSlot::RefSlot(15, 1, icu.vector_context_switch[i]), + }); + impl->cells[0x214 + i * 4] = Cell::RefCell(icu.vector_low[i]); + } + + // BTDMP + for (u16 i = 0; i < 2; ++i) { + impl->cells[0x2A2 + i * 0x80].set = std::bind(&Btdmp::SetTransmitClockConfig, &btdmp[i], _1); + impl->cells[0x2A2 + i * 0x80].get = std::bind(&Btdmp::GetTransmitClockConfig, &btdmp[i]); + impl->cells[0x2BE + i * 0x80].set = std::bind(&Btdmp::SetTransmitEnable, &btdmp[i], _1); + impl->cells[0x2BE + i * 0x80].get = std::bind(&Btdmp::GetTransmitEnable, &btdmp[i]); + impl->cells[0x2C2 + i * 0x80] = Cell::BitFieldCell({ + BitFieldSlot{3, 1, {}, std::bind(&Btdmp::GetTransmitFull, &btdmp[i])}, + BitFieldSlot{4, 1, {}, std::bind(&Btdmp::GetTransmitEmpty, &btdmp[i])}, + }); + impl->cells[0x2C6 + i * 0x80].set = std::bind(&Btdmp::Send, &btdmp[i], _1); + impl->cells[0x2CA + i * 0x80].set = std::bind(&Btdmp::SetTransmitFlush, &btdmp[i], _1); + impl->cells[0x2CA + i * 0x80].get = std::bind(&Btdmp::GetTransmitFlush, &btdmp[i]); + } +} + +MMIORegion::~MMIORegion() = default; + +u16 MMIORegion::Read(u16 addr) { + u16 value = impl->cells[addr].get(); + return value; +} +void MMIORegion::Write(u16 addr, u16 value) { + impl->cells[addr].set(value); +} +} // namespace Teakra diff --git a/src/teakra/src/mmio.h b/src/teakra/src/mmio.h new file mode 100644 index 0000000..e655b0a --- /dev/null +++ b/src/teakra/src/mmio.h @@ -0,0 +1,29 @@ +#pragma once +#include <array> +#include <memory> +#include "common_types.h" +#include "icu.h" + +namespace Teakra { + +class MemoryInterfaceUnit; +class Apbp; +class Timer; +class Dma; +class Ahbm; +class Btdmp; + +class MMIORegion { +public: + MMIORegion(MemoryInterfaceUnit& miu, ICU& icu, Apbp& apbp_from_cpu, Apbp& apbp_from_dsp, + std::array<Timer, 2>& timer, Dma& dma, Ahbm& ahbm, std::array<Btdmp, 2>& btdmp); + ~MMIORegion(); + u16 Read(u16 addr); // not const because it can be a FIFO register + void Write(u16 addr, u16 value); + +private: + class Impl; + std::unique_ptr<Impl> impl; +}; + +} // namespace Teakra diff --git a/src/teakra/src/mmio.md b/src/teakra/src/mmio.md new file mode 100644 index 0000000..6eee16a --- /dev/null +++ b/src/teakra/src/mmio.md @@ -0,0 +1,18 @@ +# MMIO + +The core processor communicates with peripherals (and indirectly external hardware such as CPU) via MMIO. The MMIO region occupies 0x0800-word region in the 16-bit address space, initially starting at 0x8000. The location of MMIO region can be configured in MIU (...yes, via MMIO). MMIO registers usually use even addresses only, which seems to be a hardware optimization as there is a register in MIU that enables forcing this rule. Each peripheral corresponds to a sub-region in MMIO, listed below. The detail of registers are in their corresponding peripheral documents. The register addresses in these documents are offsets to the start of the MMIO region. + + - `+0x0000` ? + - `+0x0004` JAM + - `+0x0010` GLUE + - `+0x0020` [Timer](timer.md) + - `+0x0050` [SIO](sio.md), Serial IO + - `+0x0060` [OCEM](ocem.md), On-chip Emulation Module + - `+0x0080` [PMU](pmu.md), Power Management Unit + - `+0x00C0` [APBP](apbp.md), Advanced Peripheral Bus Port? + - `+0x00E0` [AHBM](ahbm.md), Advanced High Performance Bus Master + - `+0x0100` [MIU](miu.md), Memory Interface Unit + - `+0x0140` [CRU](cru.md), Code Replacement Unit + - `+0x0180` [DMA](dma.md), Direct Memory Access + - `+0x0200` [ICU](icu.md), Interrupt Control Unit + - `+0x0280` [BTDMP](btdmp.md), Buffered Time Division Multiplexing Port diff --git a/src/teakra/src/mod_test_generator/CMakeLists.txt b/src/teakra/src/mod_test_generator/CMakeLists.txt new file mode 100644 index 0000000..794a153 --- /dev/null +++ b/src/teakra/src/mod_test_generator/CMakeLists.txt @@ -0,0 +1,9 @@ +include(CreateDirectoryGroups) + +add_executable(mod_test_generator + main.cpp +) +create_target_directory_groups(mod_test_generator) +target_link_libraries(mod_test_generator PRIVATE teakra) +target_include_directories(mod_test_generator PRIVATE .) +target_compile_options(mod_test_generator PRIVATE ${TEAKRA_CXX_FLAGS}) diff --git a/src/teakra/src/mod_test_generator/main.cpp b/src/teakra/src/mod_test_generator/main.cpp new file mode 100644 index 0000000..c4611c4 --- /dev/null +++ b/src/teakra/src/mod_test_generator/main.cpp @@ -0,0 +1,57 @@ +#include <cstdio> +#include <cstdlib> +#include <memory> +#include "../test.h" + +int main(int argc, char** argv) { + if (argc < 2) { + std::fprintf(stderr, "A file path argument must be provided. Exiting...\n"); + return -1; + } + + std::unique_ptr<std::FILE, decltype(&std::fclose)> f{std::fopen(argv[1], "wb"), std::fclose}; + if (!f) { + std::fprintf(stderr, "Unable to open file %s. Exiting...\n", argv[1]); + return -2; + } + + TestCase test_case{}; + test_case.opcode = 0x4DA0; // mpy y0, MemR04@3 || mpyus y1, MemR04@3offsZI@2 || sub3 p0, p1, + // Ax@4 || R04@3stepII2@2 + test_case.expand = 0; + test_case.before.mod2 = 1; // enable mod for r0; disable brv + for (u16 i = 0; i < TestSpaceSize; ++i) { + test_case.before.test_space_x[i] = TestSpaceX + i; + } + for (u16 i = 0; i < 0x20; ++i) { + test_case.before.r[0] = TestSpaceX + i + 0xF0; + for (u16 legacy = 0; legacy < 2; ++legacy) { + test_case.before.mod1 = legacy << 13; + for (u16 offset_mode = 0; offset_mode < 4; ++offset_mode) { + for (u16 step_mode = 0; step_mode < 8; ++step_mode) { + /*!!!*/ if (step_mode == 3) + continue; + test_case.before.ar[0] = (step_mode << 5) | (offset_mode << 8); + u16 step_min = 0, step_max = 0x20; + if (step_mode != 3) { + step_min = step_max = std::rand() % 0x20; + ++step_max; + } + for (u16 step = step_min; step < step_max; ++step) { + u16 step_true = SignExtend<5>(step) & 0x7F; + for (u16 mod = 0; mod < 0x10; ++mod) { + test_case.before.cfgi = step_true | (mod << 7); + if (std::fwrite(&test_case, sizeof(test_case), 1, f.get()) == 0) { + std::fprintf(stderr, + "Unable to completely write test case. Exiting...\n"); + return -3; + } + } + } + } + } + } + } + + return 0; +} diff --git a/src/teakra/src/ocem.md b/src/teakra/src/ocem.md new file mode 100644 index 0000000..7ab05ab --- /dev/null +++ b/src/teakra/src/ocem.md @@ -0,0 +1,87 @@ +# OCEM + +## MMIO Layout + +The following MMIO definition is derived from Lauterbach's Teak debugger with modification according to the Teak architecutre. The different layout around program address breakpoint is tested. Note that this is the only MMIO region that uses odd-address registers + +``` ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0060 | PFT | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0061 | | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0062 | PAB1_L | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0063 | | PAP1 | |PAB1_H | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0064 | PAB2_L | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0065 | | PAP2 | |PAB2_H | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0066 | PAB3_L | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0067 | | PAP3 | |PAB3_H | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0068 | | PACNT1 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0069 | | PACNT2 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x006A | | PACNT3 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x006B | DAM | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x006C | DAB | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x006D |SSE|ILE|BRE|TBE|INE|BRE|P3E|P2E|P1E|EXR|EXW|CDE|DAR|DAW|DVR|DVW| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x006E |DBG|BOT|ERR|MVD| |TRE| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x006F |SFT|ILL|TBF|INT|BR | |PA3|PA2|PA1|ABT|ERG|CD |DA |DV | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +PFT: Program Flow Trace +PAB1_L, PAB1_H: Program Address Break Point #1 +PAB2_L, PAB2_H: Program Address Break Point #2 +PAB3_L, PAB3_H: Program Address Break Point #3 +PAP1, PAP2, PAP3: Program page for Program Address Break Point #1/#2/#3 (?) +PACNT1, PACNT2, PACNT3: Program Address Break Point Counter #1/#2/#3 +DAM: Data Address Mask +DAB: Data Address Break Point + +DVW: 1 to enable data value break point on data write transaction +DVR: 1 to enable data value break point on data read transaction +DAW: 1 to enable data address break point as a result on data write transaction +DAR: 1 to enable data address break point as a result on data read transaction +CDE: 1 to enable break point as a result of simultaneous data address and data value match +EXW: 1 to enable break point as a result of external register write transaction +EXR: 1 to enable break point as a result of external register read transaction +P1E: 1 to enable program break point #1 +P2E: 1 to enable program break point #2 +P3E: 1 to enable program break point #3 +BRE: 1 to enable break point every time the program jumps instead of executing the next address +INE: 1 to enable break point upon detection of interrupt service routine +TBE: 1 to enable break point as a result of program flow trace buffer full +BRE: 1 to enable break point when returning to the beginning of block repeat loop +ILE: 1 to enable break point on illegal condition +SSE: 1 to enable single step + +DBG: 1 indicates the debug mode +BOT: 1 indicates the boot mode +ERR: 1 on detection of user reset that is being activated during execution of break point service routine +MVD: 1 on detection of MOVD instruction +TRE: 1 indicates that the current TRACE entry has to be combined with the next TRACE entry + +SFT: 1 on detection of software trap +ILL: 1 on detection of illegal break point +TBF: 1 indicates program flow Trace Buffer Full +INT: 1 on detection of interrupt break point +BR: 1 on detection of branch break point +PA3: 1 on detection of program address break point #3 +PA2: 1 on detection of program address break point #2 +PA1: 1 on detection of program address break point #1 +ABT: 1 on detection of break point due to an external event +ERG: 1 on detection of break point due to user defined register transaction +CD: 1 on detection of break point due to matched data value and matched data address +DA: 1 on detection of break point due to matched data address +DV: 1 on detection of break point due to matched data value +``` diff --git a/src/teakra/src/operand.h b/src/teakra/src/operand.h new file mode 100644 index 0000000..67900bd --- /dev/null +++ b/src/teakra/src/operand.h @@ -0,0 +1,698 @@ +#pragma once +#include "common_types.h" + +template <typename T, T... values> +inline constexpr bool NoOverlap = (values + ...) == (values | ...); + +template <unsigned bits> +struct Operand { + static_assert(bits > 0 && bits <= 16); + static constexpr unsigned Bits = bits; + +protected: + u16 storage{}; + + template <typename OperandT, unsigned pos> + friend struct At; + + template <typename OperandT, u16 value> + friend struct Const; +}; + +template <typename OperandT, unsigned pos> +struct At { + static constexpr unsigned Bits = OperandT::Bits; + static_assert((Bits < 16 && pos < 16 && Bits + pos <= 16) || (Bits == 16 && pos == 16)); + static constexpr u16 Mask = (((1 << Bits) - 1) << pos) & 0xFFFF; + static constexpr bool NeedExpansion = pos == 16; + static constexpr bool PassAsParameter = true; + using FilterResult = OperandT; + static constexpr OperandT Extract(u16 opcode, u16 expansion) { + OperandT operand{}; + if (NeedExpansion) + operand.storage = expansion; + else + operand.storage = (u16)((opcode & Mask) >> pos); + return operand; + } +}; + +template <typename OperandT, unsigned pos> +struct AtNamed { + using BaseType = At<OperandT, pos>; + static constexpr unsigned Bits = BaseType::Bits; + static constexpr u16 Mask = BaseType::Mask; + static constexpr bool NeedExpansion = BaseType::NeedExpansion; + static constexpr bool PassAsParameter = BaseType::PassAsParameter; + using FilterResult = typename BaseType::FilterResult::NameType; + static constexpr auto Extract(u16 opcode, u16 expansion) { + return BaseType::Extract(opcode, expansion).GetName(); + } +}; + +template <unsigned pos> +struct Unused { + static_assert(pos < 16); + static constexpr u16 Mask = 1 << pos; + static constexpr bool NeedExpansion = false; + static constexpr bool PassAsParameter = false; +}; + +template <typename OperandT, u16 value> +struct Const { + static constexpr u16 Mask = 0; + static constexpr bool NeedExpansion = false; + static constexpr bool PassAsParameter = true; + using FilterResult = OperandT; + static constexpr OperandT Extract(u16, u16) { + OperandT operand{}; + operand.storage = value; + return operand; + } +}; + +enum class SumBase { + Zero, + Acc, + Sv, + SvRnd, +}; + +template <typename T, T value> +struct Cn { + static constexpr u16 Mask = 0; + static constexpr bool NeedExpansion = false; + static constexpr bool PassAsParameter = true; + using FilterResult = T; + static constexpr T Extract(u16, u16) { + return value; + } +}; + +using SX = Cn<bool, true>; +using UX = Cn<bool, false>; +using SY = Cn<bool, true>; +using UY = Cn<bool, false>; +using BZr = Cn<SumBase, SumBase::Zero>; +using BAc = Cn<SumBase, SumBase::Acc>; +using BSv = Cn<SumBase, SumBase::Sv>; +using BSr = Cn<SumBase, SumBase::SvRnd>; +using PA = Cn<bool, true>; +using PP = Cn<bool, false>; +using Sub = Cn<bool, true>; +using Add = Cn<bool, false>; +using EMod = Cn<bool, false>; +using DMod = Cn<bool, true>; + +template <typename OperandT, unsigned pos, u16 value> +struct AtConst { + using Base = At<OperandT, pos>; + static_assert(Base::NeedExpansion == false, ""); + static constexpr u16 Mask = Base::Mask; + static constexpr u16 Pad = value << pos; +}; + +////////////////////////////////////////////////////////////////////////////// + +constexpr unsigned intlog2(unsigned n) { + if (n % 2 != 0) + throw "wtf"; + return (n == 2) ? 1 : 1 + intlog2(n / 2); +} + +template <typename EnumT, EnumT... names> +struct EnumOperand : Operand<intlog2(sizeof...(names))> { + using NameType = EnumT; + static constexpr EnumT values[] = {names...}; + constexpr EnumT GetName() const { + return values[this->storage]; + } +}; + +template <typename EnumT> +struct EnumAllOperand : Operand<intlog2((unsigned)EnumT::EnumEnd)> { + using NameType = EnumT; + constexpr EnumT GetName() const { + return (EnumT)this->storage; + } +}; + +// clang-format off + +enum class RegName { + a0, a0l, a0h, a0e, + a1, a1l, a1h, a1e, + b0, b0l, b0h, b0e, + b1, b1l, b1h, b1e, + + r0, r1, r2, r3, r4, r5, r6, r7, + + y0, p, + + pc, sp, sv, lc, + + ar0, ar1, + arp0, arp1, arp2, arp3, + + ext0, ext1, ext2, ext3, + + stt0, stt1, stt2, + st0, st1, st2, + cfgi, cfgj, + mod0, mod1, mod2, mod3, + + undefine, +}; + +template <RegName ... reg_names> +using RegOperand = EnumOperand <RegName, reg_names...>; + +struct Register : RegOperand< + RegName::r0, + RegName::r1, + RegName::r2, + RegName::r3, + RegName::r4, + RegName::r5, + RegName::r7, + RegName::y0, + RegName::st0, + RegName::st1, + RegName::st2, + RegName::p, // take special care of this as src operand + RegName::pc, + RegName::sp, + RegName::cfgi, + RegName::cfgj, + RegName::b0h, + RegName::b1h, + RegName::b0l, + RegName::b1l, + RegName::ext0, + RegName::ext1, + RegName::ext2, + RegName::ext3, + RegName::a0, // take special care of this as src operand + RegName::a1, // take special care of this as src operand + RegName::a0l, + RegName::a1l, + RegName::a0h, + RegName::a1h, + RegName::lc, + RegName::sv +> { + // only used in mov(Register, Register) + constexpr RegName GetNameForMovFromP() { + return (this->storage & 1) ? RegName::a1 : RegName::a0; + } +}; +struct Ax : RegOperand< + RegName::a0, + RegName::a1 +> {}; +struct Axl : RegOperand< + RegName::a0l, + RegName::a1l +> {}; +struct Axh : RegOperand< + RegName::a0h, + RegName::a1h +> {}; +struct Bx : RegOperand< + RegName::b0, + RegName::b1 +> {}; +struct Bxl : RegOperand< + RegName::b0l, + RegName::b1l +> {}; +struct Bxh : RegOperand< + RegName::b0h, + RegName::b1h +> {}; +struct Px : Operand<1> { + constexpr Px() = default; + constexpr Px(u16 index) { + this->storage = index; + } + constexpr u16 Index() const { + return this->storage; + } +}; +struct Ab : RegOperand< + RegName::b0, + RegName::b1, + RegName::a0, + RegName::a1 +> {}; +struct Abl : RegOperand< + RegName::b0l, + RegName::b1l, + RegName::a0l, + RegName::a1l +> {}; +struct Abh : RegOperand< + RegName::b0h, + RegName::b1h, + RegName::a0h, + RegName::a1h +> {}; +struct Abe : RegOperand< + RegName::b0e, + RegName::b1e, + RegName::a0e, + RegName::a1e +> {}; +struct Ablh : RegOperand< + RegName::b0l, + RegName::b0h, + RegName::b1l, + RegName::b1h, + RegName::a0l, + RegName::a0h, + RegName::a1l, + RegName::a1h +> {}; +struct RnOld : RegOperand< + RegName::r0, + RegName::r1, + RegName::r2, + RegName::r3, + RegName::r4, + RegName::r5, + RegName::r7, + RegName::y0 +> {}; +struct Rn : RegOperand< + RegName::r0, + RegName::r1, + RegName::r2, + RegName::r3, + RegName::r4, + RegName::r5, + RegName::r6, + RegName::r7 +> { + constexpr Rn() = default; + constexpr Rn(u16 index) { + this->storage = index; + } + constexpr u16 Index() const { + return this->storage; + } +}; + +struct R45 : RegOperand< + RegName::r4, + RegName::r5 +> { + constexpr u16 Index() const { + return this->storage + 4; + } +}; + +struct R0123 : RegOperand< + RegName::r0, + RegName::r1, + RegName::r2, + RegName::r3 +> { + constexpr u16 Index() const { + return this->storage; + } +}; + +struct ArArpSttMod : RegOperand< + RegName::ar0, + RegName::ar1, + RegName::arp0, + RegName::arp1, + RegName::arp2, + RegName::arp3, + RegName::undefine, + RegName::undefine, + RegName::stt0, + RegName::stt1, + RegName::stt2, + RegName::undefine, + RegName::mod0, + RegName::mod1, + RegName::mod2, + RegName::mod3 +> {}; +struct ArArp : RegOperand< + RegName::ar0, + RegName::ar1, + RegName::arp0, + RegName::arp1, + RegName::arp2, + RegName::arp3, + RegName::undefine, + RegName::undefine +> {}; +struct SttMod : RegOperand< + RegName::stt0, + RegName::stt1, + RegName::stt2, + RegName::undefine, + RegName::mod0, + RegName::mod1, + RegName::mod2, + RegName::mod3 +> {}; + +struct Ar : RegOperand< + RegName::ar0, + RegName::ar1 +> { + constexpr u16 Index() const { + return this->storage; + } +}; + +struct Arp : RegOperand< + RegName::arp0, + RegName::arp1, + RegName::arp2, + RegName::arp3 +> { + constexpr u16 Index() const { + return this->storage; + } +}; + +enum SwapTypeValue { + a0b0, + a0b1, + a1b0, + a1b1, + a0b0a1b1, + a0b1a1b0, + a0b0a1, + a0b1a1, + a1b0a0, + a1b1a0, + b0a0b1, + b0a1b1, + b1a0b0, + b1a1b0, + reserved0, + reserved1, + + EnumEnd, +}; + +using SwapType = EnumAllOperand<SwapTypeValue>; + +enum class StepValue { + Zero, + Increase, + Decrease, + PlusStep, + Increase2Mode1, + Decrease2Mode1, + Increase2Mode2, + Decrease2Mode2, +}; + +using StepZIDS = EnumOperand<StepValue, + StepValue::Zero, + StepValue::Increase, + StepValue::Decrease, + StepValue::PlusStep +>; + +template<unsigned bits, u16 offset = 0> +struct ArIndex : Operand<bits>{ + constexpr u16 Index() const { + return this->storage + offset; + } +}; + +struct ArRn1 : ArIndex<1> {}; +struct ArRn2 : ArIndex<2> {}; +struct ArStep1 : ArIndex<1> {}; +struct ArStep1Alt : ArIndex<1, 2> {}; +struct ArStep2 : ArIndex<2> {}; +struct ArpRn1 : ArIndex<1> {}; +struct ArpRn2 : ArIndex<2> {}; +struct ArpStep1 : ArIndex<1> {}; +struct ArpStep2 : ArIndex<2> {}; + +struct Address18_2 : Operand<2> { + constexpr u32 Address32() const { + return (u32)(this->storage) << 16; + } +}; +struct Address18_16 : Operand<16> { + constexpr u32 Address32() const { + return this->storage; + } +}; + +constexpr u32 Address32(Address18_16 low, Address18_2 high) { + return low.Address32() | high.Address32(); +} + +struct Address16 : Operand<16> { + constexpr u32 Address32() { + return this->storage; + } +}; + +struct RelAddr7 : Operand<7> { + constexpr u32 Relative32() { + return SignExtend<7, u32>(this->storage); + } +}; + +template <unsigned bits> +struct Imm : Operand<bits> { + constexpr u16 Unsigned16() const { + return this->storage; + } +}; + +template <unsigned bits> +struct Imms : Operand<bits> { + constexpr u16 Signed16() const { + return SignExtend<bits, u16>(this->storage); + } +}; + +struct Imm2 : Imm<2> {}; +struct Imm4 : Imm<4> {}; +struct Imm5 : Imm<5> {}; +struct Imm5s : Imms<5> {}; +struct Imm6s : Imms<6> {}; +struct Imm7s : Imms<7> {}; +struct Imm8 : Imm<8> {}; +struct Imm8s : Imms<8> {}; +struct Imm9 : Imm<9> {}; +struct Imm16 : Imm<16> {}; + +struct MemImm8 : Imm8 {}; +struct MemImm16 : Imm16 {}; +struct MemR7Imm7s : Imm7s {}; +struct MemR7Imm16 : Imm16 {}; + + +enum class AlmOp { + Or, + And, + Xor, + Add, + Tst0, + Tst1, + Cmp, + Sub, + Msu, + Addh, + Addl, + Subh, + Subl, + Sqr, + Sqra, + Cmpu, + + Reserved +}; + +using Alm = EnumOperand<AlmOp, + AlmOp::Or, + AlmOp::And, + AlmOp::Xor, + AlmOp::Add, + AlmOp::Tst0, + AlmOp::Tst1, + AlmOp::Cmp, + AlmOp::Sub, + AlmOp::Msu, + AlmOp::Addh, + AlmOp::Addl, + AlmOp::Subh, + AlmOp::Subl, + AlmOp::Sqr, + AlmOp::Sqra, + AlmOp::Cmpu +>; + +using Alu = EnumOperand<AlmOp, + AlmOp::Or, + AlmOp::And, + AlmOp::Xor, + AlmOp::Add, + AlmOp::Reserved, + AlmOp::Reserved, + AlmOp::Cmp, + AlmOp::Sub +>; + +enum class AlbOp { + Set, + Rst, + Chng, + Addv, + Tst0, + Tst1, + Cmpv, + Subv, + + EnumEnd +}; + +using Alb = EnumAllOperand<AlbOp>; + +enum class MulOp { + Mpy, + Mpysu, + Mac, + Macus, + Maa, + Macuu, + Macsu, + Maasu, +}; + +using Mul3 = EnumOperand<MulOp, + MulOp::Mpy, + MulOp::Mpysu, + MulOp::Mac, + MulOp::Macus, + MulOp::Maa, + MulOp::Macuu, + MulOp::Macsu, + MulOp::Maasu +>; + +using Mul2 = EnumOperand<MulOp, + MulOp::Mpy, + MulOp::Mac, + MulOp::Maa, + MulOp::Macsu +>; + +enum class ModaOp { + Shr, + Shr4, + Shl, + Shl4, + Ror, + Rol, + Clr, + Reserved, + Not, + Neg, + Rnd, + Pacr, + Clrr, + Inc, + Dec, + Copy, + +}; + +using Moda4 = EnumOperand<ModaOp, + ModaOp::Shr, + ModaOp::Shr4, + ModaOp::Shl, + ModaOp::Shl4, + ModaOp::Ror, + ModaOp::Rol, + ModaOp::Clr, + ModaOp::Reserved, + ModaOp::Not, + ModaOp::Neg, + ModaOp::Rnd, + ModaOp::Pacr, + ModaOp::Clrr, + ModaOp::Inc, + ModaOp::Dec, + ModaOp::Copy +>; + +using Moda3 = EnumOperand<ModaOp, + ModaOp::Shr, + ModaOp::Shr4, + ModaOp::Shl, + ModaOp::Shl4, + ModaOp::Ror, + ModaOp::Rol, + ModaOp::Clr, + ModaOp::Clrr +>; + +enum class CondValue { + True, + Eq, + Neq, + Gt, + Ge, + Lt, + Le, + Nn, + C, + V, + E, + L, + Nr, + Niu0, + Iu0, + Iu1, + + EnumEnd +}; + +using Cond = EnumAllOperand<CondValue>; + +struct BankFlags : Operand<6>{ + constexpr bool Cfgi() const { + return (this->storage & 1) != 0; + } + constexpr bool R4() const { + return (this->storage & 2) != 0; + } + constexpr bool R1() const { + return (this->storage & 4) != 0; + } + constexpr bool R0() const { + return (this->storage & 8) != 0; + } + constexpr bool R7() const { + return (this->storage & 16) != 0; + } + constexpr bool Cfgj() const { + return (this->storage & 32) != 0; + } +}; + +enum class CbsCondValue { + Ge, + Gt, + + EnumEnd +}; + +using CbsCond = EnumAllOperand<CbsCondValue>; + +// clang-format on diff --git a/src/teakra/src/parser.cpp b/src/teakra/src/parser.cpp new file mode 100644 index 0000000..895cef0 --- /dev/null +++ b/src/teakra/src/parser.cpp @@ -0,0 +1,92 @@ +#include <algorithm> +#include <functional> +#include <optional> +#include <unordered_map> +#include <variant> +#include "../include/teakra/disassembler.h" +#include "common_types.h" +#include "crash.h" +#include "parser.h" + +using NodeAsConst = std::string; +struct NodeAsExpansion {}; + +bool operator==(NodeAsExpansion, NodeAsExpansion) { + return true; +} + +namespace std { +template <> +struct hash<NodeAsExpansion> { + typedef NodeAsExpansion argument_type; + typedef std::size_t result_type; + result_type operator()(argument_type const& s) const { + return 0x12345678; + } +}; +} // namespace std + +namespace Teakra { +class ParserImpl : public Parser { +public: + using NodeKey = std::variant<NodeAsConst, NodeAsExpansion>; + + struct Node { + bool end = false; + u16 opcode = 0; + bool expansion = false; + std::unordered_map<NodeKey, std::unique_ptr<Node>> children; + }; + + Node root; + + Opcode Parse(const std::vector<std::string>& tokens) override { + Node* current = &root; + for (auto& token : tokens) { + auto const_find = current->children.find(token); + if (const_find != current->children.end()) { + current = const_find->second.get(); + } else { + return Opcode{Opcode::Invalid}; + } + } + if (!current->end) { + return Opcode{Opcode::Invalid}; + } + return Opcode{current->expansion ? Opcode::ValidWithExpansion : Opcode::Valid, + current->opcode}; + } +}; + +std::unique_ptr<Parser> GenerateParser() { + std::unique_ptr<ParserImpl> parser = std::make_unique<ParserImpl>(); + for (u32 opcode = 0; opcode < 0x10000; ++opcode) { + u16 o = (u16)opcode; + bool expansion = Disassembler::NeedExpansion(o); + auto tokens = Disassembler::GetTokenList(o); + + if (std::any_of(tokens.begin(), tokens.end(), [](const auto& token) { + return token.find("[ERROR]") != std::string::npos; + })) + continue; + + ParserImpl::Node* current = &parser->root; + for (const auto& token : tokens) { + auto& next = current->children[token]; + if (!next) + next = std::make_unique<ParserImpl::Node>(); + current = next.get(); + } + + if (current->end) { + ASSERT((current->opcode & (u16)(~o)) == 0); + continue; + } + current->end = true; + current->opcode = o; + current->expansion = expansion; + } + return parser; +} + +} // namespace Teakra diff --git a/src/teakra/src/parser.h b/src/teakra/src/parser.h new file mode 100644 index 0000000..d9b4f51 --- /dev/null +++ b/src/teakra/src/parser.h @@ -0,0 +1,26 @@ +#pragma once +#include <memory> +#include <string> +#include <vector> +#include "common_types.h" + +namespace Teakra { + +class Parser { +public: + virtual ~Parser() = default; + struct Opcode { + enum { + Invalid, + Valid, + ValidWithExpansion, + } status = Invalid; + u16 opcode = 0; + }; + + virtual Opcode Parse(const std::vector<std::string>& tokens) = 0; +}; + +std::unique_ptr<Parser> GenerateParser(); + +} // namespace Teakra diff --git a/src/teakra/src/pmu.md b/src/teakra/src/pmu.md new file mode 100644 index 0000000..326506a --- /dev/null +++ b/src/teakra/src/pmu.md @@ -0,0 +1,78 @@ +# PMU + +## MMIO Layout + +The following MMIO definition is extracted from Lauterbach's Teak debugger and is not tested at all. + +``` ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0080 | PLLMUL | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0082 | |PLO| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0084 |PLB| | CLKD | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0086 | |SDJ|SD1|SD0|SDO| |SDA|SDH|SDG|SDS|SDD|SDC| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0088 | |I0J|I01|I00|I0O| |I0A|I0H|I0G|I0S|I0D|I0C| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x008A | |I1J|I11|I10|I1O| |I1A|I1H|I1G|I1S|I1D|I1C| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x008C | |I2J|I21|I20|I2O| |I2A|I2H|I2G|I2S|I2D|I2C| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x008E | |IVJ|IV1|IV0|IVO| |IVA|IVH|IVG|IVS|IVD|IVC| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0090 | |T0J|T01| |T0O| |T0A|T0H|T0G|T0S|T0D|T0C| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0092 | |T1J| |T10|T1O| |T1A|T1H|T1G|T1S|T1D|T1C| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0094 | |NMJ|NM1|NM0|NMO| |NMA|NMH|NMG|NMS|NMD|NMC| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0096 | | | | | | | | | | |EXD| | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0098 | | |BM1|BM0| | | | | | |BMD| | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x009A | | SDB | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x009C | | I0B | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x009E | | I1B | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00A0 | | I2B | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00A2 | | IVB | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00A4 | | T0B | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x00A6 | | T1B | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +PLLMUL: Configuration of the PLL clock multiplication +PLO: PLL power-on configuration value for PLL use +CLKD: Clock Division Factor. When writing a value of 0 or 1 to CLKD, then the clock frequency is divided by 1 +PLB: 1 to bypass PLL + +SDx: 1 to shut down module x +I0x: 1 to enable module x recovery on interrupt 0 +I1x: 1 to enable module x recovery on interrupt 1 +I2x: 1 to enable module x recovery on interrupt 2 +IVx: 1 to enable module x recovery on vectored interrupt +T0x: 1 to enable module x recovery on timer 0 +T1x: 1 to enable module x recovery on timer 1 +NMx: 1 to enable module x recovery on non-maskable interrupt +EXx: 1 to enable module x recovery on external signal +BMx: 1 to enable module x breakpoint mask + +The module "x" in registers above means + - C: core + - D: DMA + - S: SIO + - G: GLUE + - H: APBP/HPI + - A: AHBM + - O: OCEM + - 0: Timer 0 + - 1: Timer 1 + - J: JAM + - B: BTDMP 0/1/2/3, for bit 0/1/2/3 respectively +``` diff --git a/src/teakra/src/processor.cpp b/src/teakra/src/processor.cpp new file mode 100644 index 0000000..c7517f1 --- /dev/null +++ b/src/teakra/src/processor.cpp @@ -0,0 +1,34 @@ +#include "interpreter.h" +#include "processor.h" +#include "register.h" + +namespace Teakra { + +struct Processor::Impl { + Impl(CoreTiming& core_timing, MemoryInterface& memory_interface) + : core_timing(core_timing), interpreter(core_timing, regs, memory_interface) {} + CoreTiming& core_timing; + RegisterState regs; + Interpreter interpreter; +}; + +Processor::Processor(CoreTiming& core_timing, MemoryInterface& memory_interface) + : impl(new Impl(core_timing, memory_interface)) {} +Processor::~Processor() = default; + +void Processor::Reset() { + impl->regs = RegisterState(); +} + +void Processor::Run(unsigned cycles) { + impl->interpreter.Run(cycles); +} + +void Processor::SignalInterrupt(u32 i) { + impl->interpreter.SignalInterrupt(i); +} +void Processor::SignalVectoredInterrupt(u32 address, bool context_switch) { + impl->interpreter.SignalVectoredInterrupt(address, context_switch); +} + +} // namespace Teakra diff --git a/src/teakra/src/processor.h b/src/teakra/src/processor.h new file mode 100644 index 0000000..d0f0b59 --- /dev/null +++ b/src/teakra/src/processor.h @@ -0,0 +1,25 @@ +#pragma once + +#include <memory> +#include "common_types.h" +#include "core_timing.h" + +namespace Teakra { + +class MemoryInterface; + +class Processor { +public: + Processor(CoreTiming& core_timing, MemoryInterface& memory_interface); + ~Processor(); + void Reset(); + void Run(unsigned cycles); + void SignalInterrupt(u32 i); + void SignalVectoredInterrupt(u32 address, bool context_switch); + +private: + struct Impl; + std::unique_ptr<Impl> impl; +}; + +} // namespace Teakra diff --git a/src/teakra/src/processor_general.md b/src/teakra/src/processor_general.md new file mode 100644 index 0000000..772f543 --- /dev/null +++ b/src/teakra/src/processor_general.md @@ -0,0 +1,11 @@ +## Features of the Architecture + +Most things in the processor is 16-bit. There is no 8-bit byte, and the smallest addressable memory unit is 16-bit. Many registers and data buses are also 16-bit. Exceptions are 40-bit accumulators, 33-bit multiplication results and 18-bit program counter. From now on, we call 16-bit "a word". + +Teak separate instruction and data address space. The instruction address is (18 + 4)-bit long, where 18 being the lower bits directly addressable by the program counter, and 4 being the higher bits specified by the `prpage` register. i.e. the program memory can be 16 pages with each page up to 0x40000 words. However, DSi/3DS seem to only support half of one program page (0x20000 words), and the register `prpage` is always 0. The data address is 16-bit long, meaning the data memory space contains 0x10000 words. However, DSi/3DS provides 0x20000-words long data memory to DSP. Accessing the larger space of data memory is achieved by memory bank switching. See [memory interface](miu.md) for details + +Although the program/data memory is viewed as arrays of 16-bit words from the Teak processor with no byte-order concept, it can be also accessed from the CPU (ARM) side and viewed as byte arrays. It appears that the 16-bit words is little-endian when represented in byte arrays. + +Each instruction is either one word or two words. The encoding of the instruction set is very messy. See [decoder](decoder.md) for details. + +The clock rate of the processor is unclear. Disassembly of 3DS DSP binary shows that it is likely about 134MHz (134,060,000 being the number used in the code). This also matches claimed sample rate from 3dbrew (32728Hz ≈ 134MHz / 4100, where 4100 is also a number for configuring audio output timer found in disassembly). Most instructions can complete in a single clock cycle, except for double-word instructions, multiple data read/write instructions and flow control instructions. diff --git a/src/teakra/src/register.h b/src/teakra/src/register.h new file mode 100644 index 0000000..713aed4 --- /dev/null +++ b/src/teakra/src/register.h @@ -0,0 +1,687 @@ +#pragma once + +#include <algorithm> +#include <array> +#include <memory> +#include <vector> +#include "common_types.h" +#include "crash.h" +#include "operand.h" + +namespace Teakra { + +struct RegisterState { + void Reset() { + *this = RegisterState(); + } + + /** Program control unit **/ + + u32 pc = 0; // 18-bit, program counter + u16 prpage = 0; // 4-bit, program page + u16 cpc = 1; // 1-bit, change word order when push/pop pc + + u16 repc = 0; // 16-bit rep loop counter + u16 repcs = 0; // repc shadow + bool rep = false; // true when in rep loop + u16 crep = 1; // 1-bit. If clear, store/restore repc to shadows on context switch + + u16 bcn = 0; // 3-bit, nest loop counter + u16 lp = 0; // 1-bit, set when in a loop + + struct BlockRepeatFrame { + u32 start = 0; + u32 end = 0; + u16 lc = 0; + }; + + std::array<BlockRepeatFrame, 4> bkrep_stack; + u16& Lc() { + if (lp) + return bkrep_stack[bcn - 1].lc; + return bkrep_stack[0].lc; + } + + /** Computation unit **/ + + // 40-bit 2's comp accumulators. + // Use 64-bit 2's comp here. The upper 24 bits are always sign extension + std::array<u64, 2> a{}; + std::array<u64, 2> b{}; + + u64 a1s = 0, b1s = 0; // shadows for a1 and b1 + u16 ccnta = 1; // 1-bit. If clear, store/restore a1/b1 to shadows on context switch + + u16 sat = 0; // 1-bit, disable saturation when moving from acc + u16 sata = 1; // 1-bit, disable saturation when moving to acc + u16 s = 0; // 1-bit, shift mode. 0 - arithmetic, 1 - logic + u16 sv = 0; // 16-bit two's complement shift value + + // 1-bit flags + u16 fz = 0; // zero flag + u16 fm = 0; // negative flag + u16 fn = 0; // normalized flag + u16 fv = 0; // overflow flag + u16 fe = 0; // extension flag + u16 fc0 = 0; // carry flag + u16 fc1 = 0; // another carry flag + u16 flm = 0; // set on saturation + u16 fvl = 0; // latching fv + u16 fr = 0; // Rn zero flag + + // Viterbi + u16 vtr0 = 0; + u16 vtr1 = 0; + + /** Multiplication unit **/ + + std::array<u16, 2> x{}; // factor + std::array<u16, 2> y{}; // factor + u16 hwm = 0; // 2-bit, half word mode, modify y on multiplication + std::array<u32, 2> p{}; // product + std::array<u16, 2> pe{}; // 1-bit product extension + std::array<u16, 2> ps{}; // 2-bit, product shift mode + u16 p0h_cbs = 0; // 16-bit hidden state for codebook search (CBS) opcode + + /** Address unit **/ + + std::array<u16, 8> r{}; // 16-bit general and address registers + u16 mixp = 0; // 16-bit, stores result of min/max instructions + u16 sp = 0; // 16-bit stack pointer + u16 page = 0; // 8-bit, higher part of MemImm8 address + u16 pcmhi = 0; // 2-bit, higher part of program address for movp/movd + + // shadows for bank exchange; + u16 r0b = 0, r1b = 0, r4b = 0, r7b = 0; + + /** Address step/mod unit **/ + + // step/modulo + u16 stepi = 0, stepj = 0; // 7-bit step + u16 modi = 0, modj = 0; // 9-bit mod + u16 stepi0 = 0, stepj0 = 0; // 16-bit step + + // shadows for bank exchange + u16 stepib = 0, stepjb = 0; + u16 modib = 0, modjb = 0; + u16 stepi0b = 0, stepj0b = 0; + + std::array<u16, 8> m{}; // 1-bit each, enable modulo arithmetic for Rn + std::array<u16, 8> br{}; // 1-bit each, use bit-reversed value from Rn as address + u16 stp16 = 0; // 1 bit. If set, stepi0/j0 will be exchanged along with cfgi/j in banke, and use + // stepi0/j0 for steping + u16 cmd = 1; // 1-bit, step/mod method. 0 - Teak; 1 - TeakLite + u16 epi = 0; // 1-bit. If set, cause r3 = 0 when steping r3 + u16 epj = 0; // 1-bit. If set, cause r7 = 0 when steping r7 + + /** Indirect address unit **/ + + // 3 bits each + // 0: +0 + // 1: +1 + // 2: -1 + // 3: +s + // 4: +2 + // 5: -2 + // 6: +2* + // 7: -2* + std::array<u16, 4> arstep{{1, 4, 5, 3}}, arpstepi{{1, 4, 5, 3}}, arpstepj{{1, 4, 5, 3}}; + + // 2 bits each + // 0: +0 + // 1: +1 + // 2: -1 + // 3: -1* + std::array<u16, 4> aroffset{{0, 1, 2, 0}}, arpoffseti{{0, 1, 2, 0}}, arpoffsetj{{0, 1, 2, 0}}; + + // 3 bits each, represent r0~r7 + std::array<u16, 4> arrn{{0, 4, 2, 6}}; + + // 2 bits each. for i represent r0~r3, for j represents r4~r7 + std::array<u16, 4> arprni{{0, 1, 2, 3}}, arprnj{{0, 1, 2, 3}}; + + /** Interrupt unit **/ + + // interrupt pending bit + std::array<u16, 3> ip{}; + u16 ipv = 0; + + // interrupt enable bit + std::array<u16, 3> im{}; + u16 imv = 0; + + // interrupt context switching bit + std::array<u16, 3> ic{}; + u16 nimc = 0; + + // interrupt enable master bit + u16 ie = 0; + + /** Extension unit **/ + + std::array<u16, 5> ou{}; // user output pins + std::array<u16, 2> iu{}; // user input pins + std::array<u16, 4> ext{}; + + u16 mod0_unk_const = 1; // 3-bit + + /** Shadow registers **/ + + template <u16 RegisterState::*origin> + class ShadowRegister { + public: + void Store(RegisterState* self) { + shadow = self->*origin; + } + void Restore(RegisterState* self) { + self->*origin = shadow; + } + + private: + u16 shadow = 0; + }; + + template <std::size_t size, std::array<u16, size> RegisterState::*origin> + class ShadowArrayRegister { + public: + void Store(RegisterState* self) { + shadow = self->*origin; + } + void Restore(RegisterState* self) { + self->*origin = shadow; + } + + private: + std::array<u16, size> shadow{}; + }; + + template <typename... ShadowRegisters> + class ShadowRegisterList : private ShadowRegisters... { + public: + void Store(RegisterState* self) { + (ShadowRegisters::Store(self), ...); + } + void Restore(RegisterState* self) { + (ShadowRegisters::Restore(self), ...); + } + }; + + template <u16 RegisterState::*origin> + class ShadowSwapRegister { + public: + void Swap(RegisterState* self) { + std::swap(self->*origin, shadow); + } + + private: + u16 shadow = 0; + }; + + template <std::size_t size, std::array<u16, size> RegisterState::*origin> + class ShadowSwapArrayRegister { + public: + void Swap(RegisterState* self) { + std::swap(self->*origin, shadow); + } + + private: + std::array<u16, size> shadow{}; + }; + + template <typename... ShadowSwapRegisters> + class ShadowSwapRegisterList : private ShadowSwapRegisters... { + public: + void Swap(RegisterState* self) { + (ShadowSwapRegisters::Swap(self), ...); + } + }; + + // clang-format off + + ShadowRegisterList< + ShadowRegister<&RegisterState::flm>, + ShadowRegister<&RegisterState::fvl>, + ShadowRegister<&RegisterState::fe>, + ShadowRegister<&RegisterState::fc0>, + ShadowRegister<&RegisterState::fc1>, + ShadowRegister<&RegisterState::fv>, + ShadowRegister<&RegisterState::fn>, + ShadowRegister<&RegisterState::fm>, + ShadowRegister<&RegisterState::fz>, + ShadowRegister<&RegisterState::fr> + > shadow_registers; + + ShadowSwapRegisterList< + ShadowSwapRegister<&RegisterState::pcmhi>, + ShadowSwapRegister<&RegisterState::sat>, + ShadowSwapRegister<&RegisterState::sata>, + ShadowSwapRegister<&RegisterState::hwm>, + ShadowSwapRegister<&RegisterState::s>, + ShadowSwapArrayRegister<2, &RegisterState::ps>, + ShadowSwapRegister<&RegisterState::page>, + ShadowSwapRegister<&RegisterState::stp16>, + ShadowSwapRegister<&RegisterState::cmd>, + ShadowSwapArrayRegister<8, &RegisterState::m>, + ShadowSwapArrayRegister<8, &RegisterState::br>, + ShadowSwapArrayRegister<3, &RegisterState::im>, // ? + ShadowSwapRegister<&RegisterState::imv>, // ? + ShadowSwapRegister<&RegisterState::epi>, + ShadowSwapRegister<&RegisterState::epj> + > shadow_swap_registers; + + // clang-format on + + template <unsigned index> + class ShadowSwapAr { + public: + void Swap(RegisterState* self) { + std::swap(self->arrn[index * 2], rni); + std::swap(self->arrn[index * 2 + 1], rnj); + std::swap(self->arstep[index * 2], stepi); + std::swap(self->arstep[index * 2 + 1], stepj); + std::swap(self->aroffset[index * 2], offseti); + std::swap(self->aroffset[index * 2 + 1], offsetj); + } + + private: + u16 rni, rnj, stepi, stepj, offseti, offsetj; + }; + + template <unsigned index> + class ShadowSwapArp { + public: + void Swap(RegisterState* self) { + std::swap(self->arprni[index], rni); + std::swap(self->arprnj[index], rnj); + std::swap(self->arpstepi[index], stepi); + std::swap(self->arpstepj[index], stepj); + std::swap(self->arpoffseti[index], offseti); + std::swap(self->arpoffsetj[index], offsetj); + } + + private: + u16 rni, rnj, stepi, stepj, offseti, offsetj; + }; + + ShadowSwapAr<0> shadow_swap_ar0; + ShadowSwapAr<1> shadow_swap_ar1; + ShadowSwapArp<0> shadow_swap_arp0; + ShadowSwapArp<1> shadow_swap_arp1; + ShadowSwapArp<2> shadow_swap_arp2; + ShadowSwapArp<3> shadow_swap_arp3; + + void ShadowStore() { + shadow_registers.Store(this); + } + + void ShadowRestore() { + shadow_registers.Restore(this); + } + + void SwapAllArArp() { + shadow_swap_ar0.Swap(this); + shadow_swap_ar1.Swap(this); + shadow_swap_arp0.Swap(this); + shadow_swap_arp1.Swap(this); + shadow_swap_arp2.Swap(this); + shadow_swap_arp3.Swap(this); + } + + void ShadowSwap() { + shadow_swap_registers.Swap(this); + SwapAllArArp(); + } + + void SwapAr(u16 index) { + switch (index) { + case 0: + shadow_swap_ar0.Swap(this); + break; + case 1: + shadow_swap_ar1.Swap(this); + break; + } + } + + void SwapArp(u16 index) { + switch (index) { + case 0: + shadow_swap_arp0.Swap(this); + break; + case 1: + shadow_swap_arp1.Swap(this); + break; + case 2: + shadow_swap_arp2.Swap(this); + break; + case 3: + shadow_swap_arp3.Swap(this); + break; + } + } + + bool ConditionPass(Cond cond) const { + switch (cond.GetName()) { + case CondValue::True: + return true; + case CondValue::Eq: + return fz == 1; + case CondValue::Neq: + return fz == 0; + case CondValue::Gt: + return fz == 0 && fm == 0; + case CondValue::Ge: + return fm == 0; + case CondValue::Lt: + return fm == 1; + case CondValue::Le: + return fm == 1 || fz == 1; + case CondValue::Nn: + return fn == 0; + case CondValue::C: + return fc0 == 1; + case CondValue::V: + return fv == 1; + case CondValue::E: + return fe == 1; + case CondValue::L: + return flm == 1 || fvl == 1; + case CondValue::Nr: + return fr == 0; + case CondValue::Niu0: + return iu[0] == 0; + case CondValue::Iu0: + return iu[0] == 1; + case CondValue::Iu1: + return iu[1] == 1; + default: + UNREACHABLE(); + } + } + + template <typename PseudoRegisterT> + u16 Get() const { + return PseudoRegisterT::Get(this); + } + + template <typename PseudoRegisterT> + void Set(u16 value) { + PseudoRegisterT::Set(this, value); + } +}; + +template <u16 RegisterState::*target> +struct Redirector { + static u16 Get(const RegisterState* self) { + return self->*target; + } + static void Set(RegisterState* self, u16 value) { + self->*target = value; + } +}; + +template <std::size_t size, std::array<u16, size> RegisterState::*target, std::size_t index> +struct ArrayRedirector { + static u16 Get(const RegisterState* self) { + return (self->*target)[index]; + } + static void Set(RegisterState* self, u16 value) { + (self->*target)[index] = value; + } +}; + +template <u16 RegisterState::*target0, u16 RegisterState::*target1> +struct DoubleRedirector { + static u16 Get(const RegisterState* self) { + return self->*target0 | self->*target1; + } + static void Set(RegisterState* self, u16 value) { + self->*target0 = self->*target1 = value; + } +}; + +template <u16 RegisterState::*target> +struct RORedirector { + static u16 Get(const RegisterState* self) { + return self->*target; + } + static void Set(RegisterState*, u16) { + // no + } +}; + +template <std::size_t size, std::array<u16, size> RegisterState::*target, std::size_t index> +struct ArrayRORedirector { + static u16 Get(const RegisterState* self) { + return (self->*target)[index]; + } + static void Set(RegisterState*, u16) { + // no + } +}; + +template <unsigned index> +struct AccEProxy { + static u16 Get(const RegisterState* self) { + return (u16)((self->a[index] >> 32) & 0xF); + } + static void Set(RegisterState* self, u16 value) { + u32 value32 = SignExtend<4>((u32)value); + self->a[index] &= 0xFFFFFFFF; + self->a[index] |= (u64)value32 << 32; + } +}; + +struct LPRedirector { + static u16 Get(const RegisterState* self) { + return self->lp; + } + static void Set(RegisterState* self, u16 value) { + if (value != 0) { + self->lp = 0; + self->bcn = 0; + } + } +}; + +template <typename Proxy, unsigned position, unsigned length> +struct ProxySlot { + using proxy = Proxy; + static constexpr unsigned pos = position; + static constexpr unsigned len = length; + static_assert(length < 16, "Error"); + static_assert(position + length <= 16, "Error"); + static constexpr u16 mask = ((1 << length) - 1) << position; +}; + +template <typename... ProxySlots> +struct PseudoRegister { + static_assert(NoOverlap<u16, ProxySlots::mask...>, "Error"); + static u16 Get(const RegisterState* self) { + return ((ProxySlots::proxy::Get(self) << ProxySlots::pos) | ...); + } + static void Set(RegisterState* self, u16 value) { + (ProxySlots::proxy::Set(self, (value >> ProxySlots::pos) & ((1 << ProxySlots::len) - 1)), + ...); + } +}; + +// clang-format off + +using cfgi = PseudoRegister< + ProxySlot<Redirector<&RegisterState::stepi>, 0, 7>, + ProxySlot<Redirector<&RegisterState::modi>, 7, 9> +>; + +using cfgj = PseudoRegister< + ProxySlot<Redirector<&RegisterState::stepj>, 0, 7>, + ProxySlot<Redirector<&RegisterState::modj>, 7, 9> +>; + +using stt0 = PseudoRegister< + ProxySlot<Redirector<&RegisterState::flm>, 0, 1>, + ProxySlot<Redirector<&RegisterState::fvl>, 1, 1>, + ProxySlot<Redirector<&RegisterState::fe>, 2, 1>, + ProxySlot<Redirector<&RegisterState::fc0>, 3, 1>, + ProxySlot<Redirector<&RegisterState::fv>, 4, 1>, + ProxySlot<Redirector<&RegisterState::fn>, 5, 1>, + ProxySlot<Redirector<&RegisterState::fm>, 6, 1>, + ProxySlot<Redirector<&RegisterState::fz>, 7, 1>, + ProxySlot<Redirector<&RegisterState::fc1>, 11, 1> +>; +using stt1 = PseudoRegister< + ProxySlot<Redirector<&RegisterState::fr>, 4, 1>, + ProxySlot<ArrayRORedirector<2, &RegisterState::iu, 0>, 10, 1>, + ProxySlot<ArrayRORedirector<2, &RegisterState::iu, 1>, 11, 1>, + ProxySlot<ArrayRedirector<2, &RegisterState::pe, 0>, 14, 1>, + ProxySlot<ArrayRedirector<2, &RegisterState::pe, 1>, 15, 1> +>; +using stt2 = PseudoRegister< + ProxySlot<ArrayRORedirector<3, &RegisterState::ip, 0>, 0, 1>, + ProxySlot<ArrayRORedirector<3, &RegisterState::ip, 1>, 1, 1>, + ProxySlot<ArrayRORedirector<3, &RegisterState::ip, 2>, 2, 1>, + ProxySlot<RORedirector<&RegisterState::ipv>, 3, 1>, + + ProxySlot<Redirector<&RegisterState::pcmhi>, 6, 2>, + + ProxySlot<RORedirector<&RegisterState::bcn>, 12, 3>, + ProxySlot<LPRedirector, 15, 1> +>; +using mod0 = PseudoRegister< + ProxySlot<Redirector<&RegisterState::sat>, 0, 1>, + ProxySlot<Redirector<&RegisterState::sata>, 1, 1>, + ProxySlot<RORedirector<&RegisterState::mod0_unk_const>, 2, 3>, + ProxySlot<Redirector<&RegisterState::hwm>, 5, 2>, + ProxySlot<Redirector<&RegisterState::s>, 7, 1>, + ProxySlot<ArrayRedirector<5, &RegisterState::ou, 0>, 8, 1>, + ProxySlot<ArrayRedirector<5, &RegisterState::ou, 1>, 9, 1>, + ProxySlot<ArrayRedirector<2, &RegisterState::ps, 0>, 10, 2>, + + ProxySlot<ArrayRedirector<2, &RegisterState::ps, 1>, 13, 2> +>; +using mod1 = PseudoRegister< + ProxySlot<Redirector<&RegisterState::page>, 0, 8>, + + ProxySlot<Redirector<&RegisterState::stp16>, 12, 1>, + ProxySlot<Redirector<&RegisterState::cmd>, 13, 1>, + ProxySlot<Redirector<&RegisterState::epi>, 14, 1>, + ProxySlot<Redirector<&RegisterState::epj>, 15, 1> +>; +using mod2 = PseudoRegister< + ProxySlot<ArrayRedirector<8, &RegisterState::m, 0>, 0, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 1>, 1, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 2>, 2, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 3>, 3, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 4>, 4, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 5>, 5, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 6>, 6, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 7>, 7, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::br, 0>, 8, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::br, 1>, 9, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::br, 2>, 10, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::br, 3>, 11, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::br, 4>, 12, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::br, 5>, 13, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::br, 6>, 14, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::br, 7>, 15, 1> +>; +using mod3 = PseudoRegister< + ProxySlot<Redirector<&RegisterState::nimc>, 0, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::ic, 0>, 1, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::ic, 1>, 2, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::ic, 2>, 3, 1>, + ProxySlot<ArrayRedirector<5, &RegisterState::ou, 2>, 4, 1>, + ProxySlot<ArrayRedirector<5, &RegisterState::ou, 3>, 5, 1>, + ProxySlot<ArrayRedirector<5, &RegisterState::ou, 4>, 6, 1>, + ProxySlot<Redirector<&RegisterState::ie>, 7, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::im, 0>, 8, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::im, 1>, 9, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::im, 2>, 10, 1>, + ProxySlot<Redirector<&RegisterState::imv>, 11, 1>, + + ProxySlot<Redirector<&RegisterState::ccnta>, 13, 1>, + ProxySlot<Redirector<&RegisterState::cpc>, 14, 1>, + ProxySlot<Redirector<&RegisterState::crep>, 15, 1> +>; + +using st0 = PseudoRegister< + ProxySlot<Redirector<&RegisterState::sat>, 0, 1>, + ProxySlot<Redirector<&RegisterState::ie>, 1, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::im, 0>, 2, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::im, 1>, 3, 1>, + ProxySlot<Redirector<&RegisterState::fr>, 4, 1>, + ProxySlot<DoubleRedirector<&RegisterState::flm, &RegisterState::fvl>, 5, 1>, + ProxySlot<Redirector<&RegisterState::fe>, 6, 1>, + ProxySlot<Redirector<&RegisterState::fc0>, 7, 1>, + ProxySlot<Redirector<&RegisterState::fv>, 8, 1>, + ProxySlot<Redirector<&RegisterState::fn>, 9, 1>, + ProxySlot<Redirector<&RegisterState::fm>, 10, 1>, + ProxySlot<Redirector<&RegisterState::fz>, 11, 1>, + ProxySlot<AccEProxy<0>, 12, 4> +>; +using st1 = PseudoRegister< + ProxySlot<Redirector<&RegisterState::page>, 0, 8>, + // 8, 9: reserved + ProxySlot<ArrayRedirector<2, &RegisterState::ps, 0>, 10, 2>, + ProxySlot<AccEProxy<1>, 12, 4> +>; +using st2 = PseudoRegister< + ProxySlot<ArrayRedirector<8, &RegisterState::m, 0>, 0, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 1>, 1, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 2>, 2, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 3>, 3, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 4>, 4, 1>, + ProxySlot<ArrayRedirector<8, &RegisterState::m, 5>, 5, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::im, 2>, 6, 1>, + ProxySlot<Redirector<&RegisterState::s>, 7, 1>, + ProxySlot<ArrayRedirector<5, &RegisterState::ou, 0>, 8, 1>, + ProxySlot<ArrayRedirector<5, &RegisterState::ou, 1>, 9, 1>, + ProxySlot<ArrayRORedirector<2, &RegisterState::iu, 0>, 10, 1>, + ProxySlot<ArrayRORedirector<2, &RegisterState::iu, 1>, 11, 1>, + // 12: reserved + ProxySlot<ArrayRORedirector<3, &RegisterState::ip, 2>, 13, 1>, // Note the index order! + ProxySlot<ArrayRORedirector<3, &RegisterState::ip, 0>, 14, 1>, + ProxySlot<ArrayRORedirector<3, &RegisterState::ip, 1>, 15, 1> +>; +using icr = PseudoRegister< + ProxySlot<Redirector<&RegisterState::nimc>, 0, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::ic, 0>, 1, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::ic, 1>, 2, 1>, + ProxySlot<ArrayRedirector<3, &RegisterState::ic, 2>, 3, 1>, + ProxySlot<LPRedirector, 4, 1>, + ProxySlot<RORedirector<&RegisterState::bcn>, 5, 3> +>; + +template<unsigned index> +using ar = PseudoRegister< + ProxySlot<ArrayRedirector<4, &RegisterState::arstep, index * 2 + 1>, 0, 3>, + ProxySlot<ArrayRedirector<4, &RegisterState::aroffset, index * 2 + 1>, 3, 2>, + ProxySlot<ArrayRedirector<4, &RegisterState::arstep, index * 2>, 5, 3>, + ProxySlot<ArrayRedirector<4, &RegisterState::aroffset, index * 2>, 8, 2>, + ProxySlot<ArrayRedirector<4, &RegisterState::arrn, index * 2 + 1>, 10, 3>, + ProxySlot<ArrayRedirector<4, &RegisterState::arrn, index * 2>, 13, 3> +>; + +using ar0 = ar<0>; +using ar1 = ar<1>; + +template<unsigned index> +using arp = PseudoRegister< + ProxySlot<ArrayRedirector<4, &RegisterState::arpstepi, index>, 0, 3>, + ProxySlot<ArrayRedirector<4, &RegisterState::arpoffseti, index>, 3, 2>, + ProxySlot<ArrayRedirector<4, &RegisterState::arpstepj, index>, 5, 3>, + ProxySlot<ArrayRedirector<4, &RegisterState::arpoffsetj, index>, 8, 2>, + ProxySlot<ArrayRedirector<4, &RegisterState::arprni, index>, 10, 2>, + // bit 12 reserved + ProxySlot<ArrayRedirector<4, &RegisterState::arprnj, index>, 13, 2> + // bit 15 reserved +>; + +using arp0 = arp<0>; +using arp1 = arp<1>; +using arp2 = arp<2>; +using arp3 = arp<3>; + +// clang-format on + +} // namespace Teakra diff --git a/src/teakra/src/register.md b/src/teakra/src/register.md new file mode 100644 index 0000000..10bd68c --- /dev/null +++ b/src/teakra/src/register.md @@ -0,0 +1,131 @@ +# Registers + +Related code: [register.h](register.h) + +## Basic Registers + +The `RegisterState` class includes registers that program can directly access individually, such as `r0`~`r7` and accumulators, and registers that are not directly exposed to the program but affect execution. These registers have various bit length in hardware, but they are all represented as `u16` for consistency if possible(which makes it easier to define pseudo-registers, explained below). All extra bits are zero-padded. Exceptions are + - program counter `pc`, as well as other program address registers, are 18-bit on hardware. they are zero-padded to `u32` here. + - accumulators `a0`, `a1`, `b0` and `b1` are 40-bit on hardware. They are sign-extended to `u64` here. + - Multiplication results `p0` and `p1` are 33-bit on hardware. They are stored in separate `u32` fields `p[*]` for lower bits and zero-padded one-bit `u16` fields `pe[*]` for the highest bit. + +Note that many registers have signed-integer semantics with two's complement representation, but we still store them as unsigned integer. This is to cooperate with the interpreter policy where signed integer arithmetic is avoided in order to avoid undefined or implementation-defined behaviours. + + +## Shadow Registers + +Many registers have "shadow" conterpart that are not directly accessible, but can be swapped with the main registers during context change. These shadow registers are classified into 4 groups: + - bank exchange registers + - Batch one-side shadow registers + - Batch two-side shadow registers + - shadow ar/arp + +Bank exchange registers swap with there counterparts by the `banke` opcode. In the `banke` opcode, program can specify which registers to swap. These bank exchange registers are suffixed with `b` in `RegisterState`. + +Batch shadow registers (both one-side and two-side) swap with coutnerparts by the `cntx r/s` opcode. They are always swapped together, so there is no need to provide ability to swap then individually. The difference of one-side and two-side is that for one-side registers, only main -> shadow transfer happens in `cntx s` and shadow -> main in `cntx r`, while the full swap between main <-> shadow happens in both `cntx s` and `cntx r` for two-side registers. In `RegisterState`, batch shadow registers are created using template `Shadow(Swap)(Array)Register`, where `Swap` templates are for two-side registers and `Array` templates are for register arrays. They are then added to the master templates `Shadow(Swap)RegisterList`, which includes all shadow register states by inheritance and handles value swapping together. + +Shadow ar/arp registers behaves much like Batch two-side shadow registers for ar/arp registers, but they can be also swapped with smaller batch pieces by opcode `bankr`, so they are defined separately from `ShadowSwapRegisterList` as `ShadowSwapAr(p)`. + +## Block Repeat Stack + +Teak supports block repeat nested in 4 levels. It has a small 4 frame stack for this, with each frame storing the loop counter, the start and the end address. The `lc` (loop counter) register visible to the program is always the one in the stack top. It is implemented as a function `Lc()` instead of a normal `u16 lc` registers in order to emulate this behaviour. + +## Pseudo-registers + +These registers works like bit fields and are combination of basic registers. Program can read and write them in 16-bit words, while each bit of them affects the corresponding basic register. However, they are not implemented as bit fields here for the following reasons: + - There are two sets of control/status pseudo-registers, many fields of which maps to the same basic registers but with different bit layout. One set is TeakLite-compatible and the other includes all Teak-exclusive registers. + - Some bits have special behaviour other than simply store the states. + + +These pseudo-registers are defined as some C++ types with the same names, such as `cfgi`, `stt0` and `arp2`, and `RegisterState` provides two template functions `u16 Get<T>()` and `void Set<T>(u16)`, where `T` should be one of the predefined type names, for read from and write to pseudo-registers. Here is an example +``` +u16 stt1_value = registers.Get<stt1>(); +``` +Internally, these predefined pseudo-register-types, using template, store pointers to basic registers and their bit position and length. + +## Details of all registers + +### Program control registers + +#### `pc`, program counter + +18-bit program counter, address for loading instructions from the program memory. It is increamented by 1 or 2, depending on the instruction length, on every instruction. It is post-incrementing from instructions' view, i.e. when referencing the `pc` value in an instruction, the value is always the address of the *next* instruction. + +The value of `pc` can theoretically be accessed by all opcodes that accept operand `Register` as source. However, most of these opcodes and other registers in `Register` use 16-bit data bus, so it is unclear how the convertion works, or whether it works at all. Only the opcode `mov pc a0/a1` is well tested and implemented: it is full 18-bit transfer. Other code path that access `pc` via `Register` is currently marked as unreachable. On a side node, the TeakLite architecture only has a 16-bit `pc` register, where transferring `pc` value via 16-bit data base makes more sense. + +`pc` value can be pushed to / popped from the stack as two words. The word order is specified by the `cpc` register. + +Aside from increamenting on every instruction, `pc` value can also be modified by the following instructions and circumstances. Please refer to their sections for detail. + - `mov a0/a1/b0/b1 pc` opcodes. + - subroutine call: `call` opcodes family. + - subroutine return: `ret` opcodes family. + - branching: `br` opcodes family. + - `movpdw [a0/a1] pc` opcode. + - hitting the end of a repeating block indicated by opcode `bkrep`. + - repeating instruction indicated by opcode `rep`. + - on interrupt. + +#### `prpage`, program page + +4-bit register to select program page in the program memory, extending the entire possible program memory space to `4 + 18 = 22` bit. However XpertTeak/DSi/3DS only support single program page, so this register should remain 0. + +#### `repc` repeat counter + +16-bit counter that is set to `repeat count - 1` on `rep` opcodes (i.e. exactly the value passed in the opcodes) and decrements on every repeat until 0 (inclusive). During the repeat, program can read the register value by `mov` opcodes. Out side repeat loop, this register can be used as a general register by using `mov` opcodes to read and write. It can also be `push`ed to or `pop`ed from the stack, and swap with its shadow counterpart `repcs` on context store / restore if the configuration register `crep` is clear. + +#### TODO: block repeat + + +### Arithmetic registers + +#### `a0`/`a1`/`b0`/`b1`, accumulators + +40-bit accumulators, stored as sign-extended 64-bit integer in Teakra. Accumulators each has three parts from the most significant bit to the least one: `8:16:16` as `e:h:l` (extension, high, and low). The `h` and `l` part are visible to many operations, while the `e` part can be only access by opcode `push/pop Abe`, and for `a0`/`a1`, the lower 4 bits of their `e` part is exposed in pseduo-registers `st0`/`st1`. + +#### Saturation + +#### Shifting + +#### Flags + +#### `vtr0`/`vtr1`, Viterbi registers + +These two 16-bit registers are dedicated for [Viterbi decoding](https://en.wikipedia.org/wiki/Viterbi_decoder),and for path recovery to be specific. For every opcodes that contain a `vtrshr` part, status bits `fc0` and `fc1` are pushed into `vtr0` and `vtr1`, respectively. `vtrmov` opcodes can move the value from these registers to accumulators. `vtrclr` can clear these registers to zero. There is no way to directly access these registers. + +### Multiplication registers + +#### `x0`/`x1`/`y0`/`y1`, factor registers + +16 bits each, used as factors for calculating multiplication, but can also be used for general purpose. + +#### `p0`/`p1`, product registers + +33 bits each, stores the result of multiplication operation when the instruction initiates one. `p0` stores `x0 * y0` and `p1` stores `x1 * y1`. The registers are split in to 32-bit parts `p[0]/p[1]` and sign bits `pe[0]/pe[1]` in the code. + +### Address registers + +#### `r0`..`r7`, general address registers + +These 16-bit registers can be used for general purpose or as memory pointer. Most opcodes that support indirect data access uses these registers as address operand (e.g. `[r0]`). Specifically `r7` also supports supplying an additional immediate address offset in some opcodes, and is often used as stack frame pointer. + +#### `sp`, stack pointer + +16-bit register as a pointer to data memory, decrements on `push` / `call` / `bksto [sp]` opcodes and increments on `pop` / `ret` / `bkrst [sp]` opcodes. Can be also read and modified as a general register via all opcodes that accept `Register` operand. + +#### `page`, data memory page + +Used to specify the higher 8-bits for opcodes that use 8-bit immediate data address. + +#### `pcmhi`, program memory transfer page + +Used to specify the higher 2-bits for opcodes that use 16-bit program address. + +### Advanced address registers + +#### Step/mod configuration registers + +#### Indirect address registers + +### Interrupt registers + +### Extension registers diff --git a/src/teakra/src/shared_memory.h b/src/teakra/src/shared_memory.h new file mode 100644 index 0000000..8dfcb05 --- /dev/null +++ b/src/teakra/src/shared_memory.h @@ -0,0 +1,23 @@ +#pragma once +#include <array> +#include <cstdio> +#include "common_types.h" + +namespace Teakra { +struct SharedMemory { + std::array<u8, 0x80000> raw{}; + u16 ReadWord(u32 word_address) const { + u32 byte_address = word_address * 2; + u8 low = raw[byte_address]; + u8 high = raw[byte_address + 1]; + return low | ((u16)high << 8); + } + void WriteWord(u32 word_address, u16 value) { + u8 low = value & 0xFF; + u8 high = value >> 8; + u32 byte_address = word_address * 2; + raw[byte_address] = low; + raw[byte_address + 1] = high; + } +}; +} // namespace Teakra diff --git a/src/teakra/src/sio.md b/src/teakra/src/sio.md new file mode 100644 index 0000000..3026779 --- /dev/null +++ b/src/teakra/src/sio.md @@ -0,0 +1,40 @@ +# SIO + +## MMIO Layout + +The following MMIO definition is extracted from Lauterbach's Teak debugger and is not tested at all. + +``` ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0050 | SIOSHIFT | | IM| PH| CP| MS|CSO|CSP| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0052 | | CLKD2SIO | | CLKD1SIO | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0054 | SIODATA | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0056 | | SE| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0058 | |ERR|STS| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +CSP: Chip Select Polarity +CSO: Chip Select Output +MS: Defines the operation mode of the SIO (Master or Slave). + - 0: Master mode, the SIO clock is the Core clock frequency (with or without division) + - 1: Slave mode, the clock is external +CP: Clock Polarity +PH: Phase, Determines whether SIODI/SIODO are sampled at the rising edge or at the falling edge + - 0: SIODI on rising edge, SIODO on falling edge + - 1: SIODI on falling edge, SIODO on rising edge +IM: Interrupt Mask Enable - Masks the SIO interrupt to the ICU. When set to 1, the SIO does not issue an interrupt at the end of the transfer +SIOSHIFT: Determines the length of bit shifts for every transfer (2 to 16). The value in the register is (shift - 1) + +CLKD1SIO: Clock Division #1 Factor - Defines the division value of divider #1, which can range from 2 to 128. Writing 0 or 1 to this field causes the divider to be bypassed, and no division is performed. +CLKD2SIO: Clock Division #2 Factor, similar behaviour to CLKD1SIO. + +SIODATA: The data register of the SIO. Writing to this register initiates a shift of SIOSHIFT + 1 bits into it and its data is output via SIODO. Data can be written to this register via the ZSI only, and only when no shift operation occurs. + +SE: 1 to enable SIO operation +ERR: SIO Error, 1 when an error occurred. A shift overwrites a previous shift value before the Core has read it. The indication is automatically cleared when the Core reads the SIO_STS register +STS: SIO Status, sticky indication set at the end of a transfer and cleared automatically when read. +``` diff --git a/src/teakra/src/step2_test_generator/CMakeLists.txt b/src/teakra/src/step2_test_generator/CMakeLists.txt new file mode 100644 index 0000000..7f70371 --- /dev/null +++ b/src/teakra/src/step2_test_generator/CMakeLists.txt @@ -0,0 +1,9 @@ +include(CreateDirectoryGroups) + +add_executable(step2_test_generator + main.cpp +) +create_target_directory_groups(step2_test_generator) +target_link_libraries(step2_test_generator PRIVATE teakra) +target_include_directories(step2_test_generator PRIVATE .) +target_compile_options(step2_test_generator PRIVATE ${TEAKRA_CXX_FLAGS}) diff --git a/src/teakra/src/step2_test_generator/main.cpp b/src/teakra/src/step2_test_generator/main.cpp new file mode 100644 index 0000000..8700a6c --- /dev/null +++ b/src/teakra/src/step2_test_generator/main.cpp @@ -0,0 +1,41 @@ +#include <cstdio> +#include <memory> +#include "../test.h" + +int main(int argc, char** argv) { + if (argc < 2) { + std::fprintf(stderr, "A file path argument must be specified. Exiting...\n"); + return -1; + } + + std::unique_ptr<std::FILE, decltype(&std::fclose)> f{std::fopen(argv[1], "wb"), std::fclose}; + if (!f) { + std::fprintf(stderr, "Unable to open file %s. Exiting...\n", argv[1]); + return -2; + } + + TestCase test_case{}; + u16 r0base = 0x4839; + test_case.opcode = 0xDFE9; // modr r0+arps0 + test_case.expand = 0; + test_case.before.mod2 = 1; // enable mod for r0; + + for (u16 legacy = 0; legacy < 2; ++legacy) { + test_case.before.mod1 = legacy << 13; + for (u16 step = 4; step < 8; ++step) { + test_case.before.arp[0] = step; + for (u16 mod = 0; mod < 0x200; ++mod) { + test_case.before.cfgi = mod << 7; + for (u16 r = 0; r < 0x20; ++r) { + test_case.before.r[0] = r + r0base; + if (std::fwrite(&test_case, sizeof(test_case), 1, f.get()) == 0) { + std::fprintf(stderr, "Unable to completely write test case. Exiting...\n"); + return -3; + } + } + } + } + } + + return 0; +} diff --git a/src/teakra/src/teakra.cpp b/src/teakra/src/teakra.cpp new file mode 100644 index 0000000..76bc79f --- /dev/null +++ b/src/teakra/src/teakra.cpp @@ -0,0 +1,193 @@ +#include <array> +#include <atomic> +#include "ahbm.h" +#include "apbp.h" +#include "btdmp.h" +#include "core_timing.h" +#include "dma.h" +#include "icu.h" +#include "memory_interface.h" +#include "mmio.h" +#include "processor.h" +#include "shared_memory.h" +#include "teakra/teakra.h" +#include "timer.h" + +namespace Teakra { + +struct Teakra::Impl { + CoreTiming core_timing; + SharedMemory shared_memory; + MemoryInterfaceUnit miu; + ICU icu; + Apbp apbp_from_cpu, apbp_from_dsp; + std::array<Timer, 2> timer{{{core_timing}, {core_timing}}}; + Ahbm ahbm; + Dma dma{shared_memory, ahbm}; + std::array<Btdmp, 2> btdmp{{{core_timing}, {core_timing}}}; + MMIORegion mmio{miu, icu, apbp_from_cpu, apbp_from_dsp, timer, dma, ahbm, btdmp}; + MemoryInterface memory_interface{shared_memory, miu}; + Processor processor{core_timing, memory_interface}; + + Impl() { + memory_interface.SetMMIO(mmio); + using namespace std::placeholders; + icu.SetInterruptHandler(std::bind(&Processor::SignalInterrupt, &processor, _1), + std::bind(&Processor::SignalVectoredInterrupt, &processor, _1, _2)); + + timer[0].SetInterruptHandler([this]() { icu.TriggerSingle(0xA); }); + timer[1].SetInterruptHandler([this]() { icu.TriggerSingle(0x9); }); + + apbp_from_cpu.SetDataHandler(0, [this]() { icu.TriggerSingle(0xE); }); + apbp_from_cpu.SetDataHandler(1, [this]() { icu.TriggerSingle(0xE); }); + apbp_from_cpu.SetDataHandler(2, [this]() { icu.TriggerSingle(0xE); }); + apbp_from_cpu.SetSemaphoreHandler([this]() { icu.TriggerSingle(0xE); }); + + btdmp[0].SetInterruptHandler([this]() { icu.TriggerSingle(0xB); }); + btdmp[1].SetInterruptHandler([this]() { icu.TriggerSingle(0xB); }); + + dma.SetInterruptHandler([this]() { icu.TriggerSingle(0xF); }); + } + + void Reset() { + shared_memory.raw.fill(0); + miu.Reset(); + apbp_from_cpu.Reset(); + apbp_from_dsp.Reset(); + timer[0].Reset(); + timer[1].Reset(); + ahbm.Reset(); + dma.Reset(); + btdmp[0].Reset(); + btdmp[1].Reset(); + processor.Reset(); + } +}; + +Teakra::Teakra() : impl(new Impl) {} +Teakra::~Teakra() = default; + +void Teakra::Reset() { + impl->Reset(); +} + +std::array<std::uint8_t, 0x80000>& Teakra::GetDspMemory() { + return impl->shared_memory.raw; +} + +const std::array<std::uint8_t, 0x80000>& Teakra::GetDspMemory() const { + return impl->shared_memory.raw; +} + +void Teakra::Run(unsigned cycle) { + impl->processor.Run(cycle); +} + +bool Teakra::SendDataIsEmpty(std::uint8_t index) const { + return !impl->apbp_from_cpu.IsDataReady(index); +} +void Teakra::SendData(std::uint8_t index, std::uint16_t value) { + impl->apbp_from_cpu.SendData(index, value); +} +bool Teakra::RecvDataIsReady(std::uint8_t index) const { + return impl->apbp_from_dsp.IsDataReady(index); +} +std::uint16_t Teakra::RecvData(std::uint8_t index) { + return impl->apbp_from_dsp.RecvData(index); +} +std::uint16_t Teakra::PeekRecvData(std::uint8_t index) { + return impl->apbp_from_dsp.PeekData(index); +} +void Teakra::SetRecvDataHandler(std::uint8_t index, std::function<void()> handler) { + impl->apbp_from_dsp.SetDataHandler(index, std::move(handler)); +} + +void Teakra::SetSemaphore(std::uint16_t value) { + impl->apbp_from_cpu.SetSemaphore(value); +} +void Teakra::SetSemaphoreHandler(std::function<void()> handler) { + impl->apbp_from_dsp.SetSemaphoreHandler(std::move(handler)); +} +std::uint16_t Teakra::GetSemaphore() const { + return impl->apbp_from_dsp.GetSemaphore(); +} +void Teakra::ClearSemaphore(std::uint16_t value) { + impl->apbp_from_dsp.ClearSemaphore(value); +} +void Teakra::MaskSemaphore(std::uint16_t value) { + impl->apbp_from_dsp.MaskSemaphore(value); +} +void Teakra::SetAHBMCallback(const AHBMCallback& callback) { + impl->ahbm.SetExternalMemoryCallback(callback.read8, callback.write8, + callback.read16, callback.write16, + callback.read32, callback.write32); +} + +std::uint16_t Teakra::AHBMGetUnitSize(std::uint16_t i) const { + return impl->ahbm.GetUnitSize(i); +} +std::uint16_t Teakra::AHBMGetDirection(std::uint16_t i) const { + return impl->ahbm.GetDirection(i); +} +std::uint16_t Teakra::AHBMGetDmaChannel(std::uint16_t i) const { + return impl->ahbm.GetDmaChannel(i); +} + +std::uint16_t Teakra::AHBMRead16(std::uint32_t addr) { + return impl->ahbm.Read16(0, addr); +} +void Teakra::AHBMWrite16(std::uint32_t addr, std::uint16_t value) { + impl->ahbm.Write16(0, addr, value); +} +std::uint16_t Teakra::AHBMRead32(std::uint32_t addr) { + return impl->ahbm.Read32(0, addr); +} +void Teakra::AHBMWrite32(std::uint32_t addr, std::uint32_t value) { + impl->ahbm.Write32(0, addr, value); +} + +void Teakra::SetAudioCallback(std::function<void(std::array<s16, 2>)> callback) { + impl->btdmp[0].SetAudioCallback(std::move(callback)); +} + +std::uint16_t Teakra::ProgramRead(std::uint32_t address) const { + return impl->memory_interface.ProgramRead(address); +} +void Teakra::ProgramWrite(std::uint32_t address, std::uint16_t value) { + impl->memory_interface.ProgramWrite(address, value); +} +std::uint16_t Teakra::DataRead(std::uint16_t address, bool bypass_mmio) { + return impl->memory_interface.DataRead(address, bypass_mmio); +} +void Teakra::DataWrite(std::uint16_t address, std::uint16_t value, bool bypass_mmio) { + impl->memory_interface.DataWrite(address, value, bypass_mmio); +} +std::uint16_t Teakra::DataReadA32(std::uint32_t address) const { + return impl->memory_interface.DataReadA32(address); +} +void Teakra::DataWriteA32(std::uint32_t address, std::uint16_t value) { + impl->memory_interface.DataWriteA32(address, value); +} +std::uint16_t Teakra::MMIORead(std::uint16_t address) { + return impl->memory_interface.MMIORead(address); +} +void Teakra::MMIOWrite(std::uint16_t address, std::uint16_t value) { + impl->memory_interface.MMIOWrite(address, value); +} + +std::uint16_t Teakra::DMAChan0GetSrcHigh() { + u16 active_bak = impl->dma.GetActiveChannel(); + impl->dma.ActivateChannel(0); + u16 ret = impl->dma.GetAddrSrcHigh(); + impl->dma.ActivateChannel(active_bak); + return ret; +} +std::uint16_t Teakra::DMAChan0GetDstHigh() { + u16 active_bak = impl->dma.GetActiveChannel(); + impl->dma.ActivateChannel(0); + u16 ret = impl->dma.GetAddrDstHigh(); + impl->dma.ActivateChannel(active_bak); + return ret; +} + +} // namespace Teakra diff --git a/src/teakra/src/teakra_c.cpp b/src/teakra/src/teakra_c.cpp new file mode 100644 index 0000000..531465f --- /dev/null +++ b/src/teakra/src/teakra_c.cpp @@ -0,0 +1,149 @@ +#include "teakra/teakra.h" +#include "teakra/teakra_c.h" + +extern "C" { + +struct TeakraObject { + Teakra::Teakra teakra; +}; + +TeakraContext* Teakra_Create() { + return new TeakraContext; +} + +void Teakra_Destroy(TeakraContext* context) { + delete context; +} + +void Teakra_Reset(TeakraContext* context) { + context->teakra.Reset(); +} + +uint8_t* Teakra_GetDspMemory(TeakraContext* context) { + return context->teakra.GetDspMemory().data(); +} + +int Teakra_SendDataIsEmpty(const TeakraContext* context, uint8_t index) { + return context->teakra.SendDataIsEmpty(index); +} + +void Teakra_SendData(TeakraContext* context, uint8_t index, uint16_t value) { + context->teakra.SendData(index, value); +} + +int Teakra_RecvDataIsReady(const TeakraContext* context, uint8_t index) { + return context->teakra.RecvDataIsReady(index); +} + +uint16_t Teakra_RecvData(TeakraContext* context, uint8_t index) { + return context->teakra.RecvData(index); +} +uint16_t Teakra_PeekRecvData(TeakraContext* context, uint8_t index) { + return context->teakra.PeekRecvData(index); +} + +void Teakra_SetRecvDataHandler(TeakraContext* context, uint8_t index, + Teakra_InterruptCallback handler, void* userdata) { + context->teakra.SetRecvDataHandler(index, [=]() { handler(userdata); }); +} + +void Teakra_SetSemaphore(TeakraContext* context, uint16_t value) { + context->teakra.SetSemaphore(value); +} +void Teakra_ClearSemaphore(TeakraContext* context, uint16_t value) { + context->teakra.ClearSemaphore(value); +} +void Teakra_MaskSemaphore(TeakraContext* context, uint16_t value) { + context->teakra.MaskSemaphore(value); +} + +void Teakra_SetSemaphoreHandler(TeakraContext* context, Teakra_InterruptCallback handler, + void* userdata) { + context->teakra.SetSemaphoreHandler([=]() { handler(userdata); }); +} + +uint16_t Teakra_GetSemaphore(const TeakraContext* context) { + return context->teakra.GetSemaphore(); +} + +uint16_t Teakra_ProgramRead(TeakraContext* context, uint32_t address) { + return context->teakra.ProgramRead(address); +} +void Teakra_ProgramWrite(TeakraContext* context, uint32_t address, uint16_t value) { + context->teakra.ProgramWrite(address, value); +} +uint16_t Teakra_DataRead(TeakraContext* context, uint16_t address, bool bypass_mmio) { + return context->teakra.DataRead(address, bypass_mmio); +} +void Teakra_DataWrite(TeakraContext* context, uint16_t address, uint16_t value, bool bypass_mmio) { + context->teakra.DataWrite(address, value, bypass_mmio); +} +uint16_t Teakra_DataReadA32(TeakraContext* context, uint32_t address) { + return context->teakra.DataReadA32(address); +} +void Teakra_DataWriteA32(TeakraContext* context, uint32_t address, uint16_t value) { + context->teakra.DataWriteA32(address, value); +} +uint16_t Teakra_MMIORead(TeakraContext* context, uint16_t address) { + return context->teakra.MMIORead(address); +} +void Teakra_MMIOWrite(TeakraContext* context, uint16_t address, uint16_t value) { + context->teakra.MMIOWrite(address, value); +} + +uint16_t Teakra_DMAChan0GetSrcHigh(TeakraContext* context) { + return context->teakra.DMAChan0GetSrcHigh(); +} +uint16_t Teakra_DMAChan0GetDstHigh(TeakraContext* context){ + return context->teakra.DMAChan0GetDstHigh(); +} + +uint16_t Teakra_AHBMGetUnitSize(TeakraContext* context, uint16_t i) { + return context->teakra.AHBMGetUnitSize(i); +} +uint16_t Teakra_AHBMGetDirection(TeakraContext* context, uint16_t i) { + return context->teakra.AHBMGetDirection(i); +} +uint16_t Teakra_AHBMGetDmaChannel(TeakraContext* context, uint16_t i) { + return context->teakra.AHBMGetDmaChannel(i); +} + +uint16_t Teakra_AHBMRead16(TeakraContext* context, uint32_t addr) { + return context->teakra.AHBMRead16(addr); +} +void Teakra_AHBMWrite16(TeakraContext* context, uint32_t addr, uint16_t value) { + context->teakra.AHBMWrite16(addr, value); +} +uint16_t Teakra_AHBMRead32(TeakraContext* context, uint32_t addr) { + return context->teakra.AHBMRead32(addr); +} +void Teakra_AHBMWrite32(TeakraContext* context, uint32_t addr, uint32_t value) { + context->teakra.AHBMWrite32(addr, value); +} + +void Teakra_Run(TeakraContext* context, unsigned cycle) { + context->teakra.Run(cycle); +} + +void Teakra_SetAHBMCallback(TeakraContext* context, + Teakra_AHBMReadCallback8 read8 , Teakra_AHBMWriteCallback8 write8 , + Teakra_AHBMReadCallback16 read16, Teakra_AHBMWriteCallback16 write16, + Teakra_AHBMReadCallback32 read32, Teakra_AHBMWriteCallback32 write32, + void* userdata) { + Teakra::AHBMCallback callback; + callback.read8 = [=](uint32_t address) { return read8(userdata, address); }; + callback.write8 = [=](uint32_t address, uint8_t value) { write8(userdata, address, value); }; + callback.read16 = [=](uint32_t address) { return read16(userdata, address); }; + callback.write16 = [=](uint32_t address, uint16_t value) { write16(userdata, address, value); }; + callback.read32 = [=](uint32_t address) { return read32(userdata, address); }; + callback.write32 = [=](uint32_t address, uint32_t value) { write32(userdata, address, value); }; + context->teakra.SetAHBMCallback(callback); +} + + +void Teakra_SetAudioCallback(TeakraContext* context, Teakra_AudioCallback callback, + void* userdata) { + context->teakra.SetAudioCallback( + [=](std::array<std::int16_t, 2> samples) { callback(userdata, samples.data()); }); +} +} diff --git a/src/teakra/src/test.h b/src/teakra/src/test.h new file mode 100644 index 0000000..e8c8742 --- /dev/null +++ b/src/teakra/src/test.h @@ -0,0 +1,38 @@ +#pragma once + +#include <array> +#include <type_traits> + +#ifndef COMMON_TYPE_3DS +#include "common_types.h" +#endif + +constexpr u16 TestSpaceX = 0x6400; +constexpr u16 TestSpaceY = 0xCC00; +constexpr u16 TestSpaceSize = 0x0200; + +struct State { + std::array<u64, 2> a, b; + std::array<u32, 2> p; + std::array<u16, 8> r; + std::array<u16, 2> x, y; + u16 stepi0, stepj0, mixp, sv, repc, lc; + u16 cfgi, cfgj; + u16 stt0, stt1, stt2; + u16 mod0, mod1, mod2; + std::array<u16, 2> ar; + std::array<u16, 4> arp; + + std::array<u16, TestSpaceSize> test_space_x; + std::array<u16, TestSpaceSize> test_space_y; +}; + +static_assert(std::is_trivially_copyable_v<State>); + +struct TestCase { + State before, after; + u16 opcode, expand; +}; + +static_assert(sizeof(TestCase) == 4312); +static_assert(std::is_trivially_copyable_v<TestCase>); diff --git a/src/teakra/src/test_generator.cpp b/src/teakra/src/test_generator.cpp new file mode 100644 index 0000000..324ec57 --- /dev/null +++ b/src/teakra/src/test_generator.cpp @@ -0,0 +1,1482 @@ +#include <algorithm> +#include <array> +#include <cstdio> +#include <memory> +#include <random> +#include <unordered_set> +#include "common_types.h" +#include "decoder.h" +#include "operand.h" +#include "test.h" + +namespace Teakra::Test { + +enum class RegConfig { Any = 0, Memory = 1 }; + +enum class ExpandConfig { + None = 0, // or Zero + Any = 1, + Memory = 2, +}; + +namespace Random { +namespace { +std::mt19937 gen(std::random_device{}()); + +u64 uniform(u64 a, u64 b) { + std::uniform_int_distribution<u64> dist(a, b); + return dist(gen); +} + +u64 bit40() { + static std::uniform_int_distribution<u64> dist(0, 0xFF'FFFF'FFFF); + static std::uniform_int_distribution<int> dist2(0, 4); + u64 v = dist(gen); + switch (dist2(gen)) { + case 0: + v = SignExtend<40>(v); + break; + case 1: + v &= 0xFFFF; + break; + case 2: + v &= 0xFFFF'FFFF; + break; + case 3: + v = SignExtend<32>(v); + break; + case 4: + v = SignExtend<16>(v); + break; + } + return v; +} + +u32 bit32() { + static std::uniform_int_distribution<u32> dist; + return dist(gen); +} + +u16 bit16() { + static std::uniform_int_distribution<u16> dist; + return dist(gen); +} +} // Anonymous namespace +} // namespace Random + +namespace { +struct Config { + bool enable = false; + bool lock_page = false; + bool lock_r7 = false; + std::array<RegConfig, 8> r{}; + std::array<RegConfig, 4> ar{}; + std::array<RegConfig, 4> arp{}; + + ExpandConfig expand = ExpandConfig::None; + + Config WithAnyExpand() { + Config copy = *this; + copy.expand = ExpandConfig::Any; + return copy; + } + + Config WithMemoryExpand() { + Config copy = *this; + copy.expand = ExpandConfig::Memory; + return copy; + } + + State GenerateRandomState() { + State state; + state.stepi0 = Random::bit16(); + state.stepj0 = Random::bit16(); + state.mixp = Random::bit16(); + state.sv = Random::bit16(); + if (Random::uniform(0, 1) == 1) { + state.sv &= 128; + state.sv = SignExtend<7>(state.sv); + } + state.repc = Random::bit16(); + state.lc = Random::bit16(); + state.cfgi = Random::bit16(); + state.cfgj = Random::bit16(); + state.stt0 = Random::bit16(); + state.stt1 = Random::bit16(); + state.stt2 = Random::bit16(); + state.mod0 = Random::bit16(); + state.mod1 = Random::bit16(); + state.mod2 = Random::bit16(); + if (lock_page) { + state.mod1 &= 0xFF00; + state.mod1 |= TestSpaceX >> 8; + } + std::generate(state.ar.begin(), state.ar.end(), Random::bit16); + std::generate(state.arp.begin(), state.arp.end(), Random::bit16); + std::generate(state.x.begin(), state.x.end(), Random::bit16); + std::generate(state.y.begin(), state.y.end(), Random::bit16); + std::generate(state.p.begin(), state.p.end(), Random::bit32); + std::generate(state.a.begin(), state.a.end(), Random::bit40); + std::generate(state.b.begin(), state.b.end(), Random::bit40); + std::generate(state.r.begin(), state.r.end(), Random::bit16); + std::generate(state.test_space_x.begin(), state.test_space_x.end(), Random::bit16); + std::generate(state.test_space_y.begin(), state.test_space_y.end(), Random::bit16); + + auto rp = r; + for (std::size_t i = 0; i < ar.size(); ++i) { + if (ar[i] == RegConfig::Memory) { + rp[(state.ar[i / 2] >> (13 - 3 * (i % 2))) & 7] = RegConfig::Memory; + } + } + for (std::size_t i = 0; i < arp.size(); ++i) { + if (arp[i] == RegConfig::Memory) { + rp[(state.arp[i] >> 10) & 3] = RegConfig::Memory; + rp[((state.arp[i] >> 13) & 3) + 4] = RegConfig::Memory; + } + } + for (std::size_t i = 0; i < rp.size(); ++i) { + if (rp[i] == RegConfig::Memory) { + state.r[i] = (i < 4 ? TestSpaceX : TestSpaceY) + + (u16)Random::uniform(10, TestSpaceSize - 10); + if (!((state.mod2 >> i) & 1) && (((state.mod2 >> (i + 8)) & 1))) { + state.r[i] = BitReverse(state.r[i]); + } + } + } + if (lock_r7) { + state.r[7] = TestSpaceY + (u16)Random::uniform(10, TestSpaceSize - 10); + } + return state; + } +}; + +Config DisabledConfig{false}; +Config AnyConfig{true}; + +Config ConfigWithAddress(Rn r) { + Config config{true}; + config.r[r.Index()] = RegConfig::Memory; + return config; +} + +template <typename ArRnX> +Config ConfigWithArAddress(ArRnX r) { + static_assert(std::is_same_v<ArRnX, ArRn1> || std::is_same_v<ArRnX, ArRn2>); + Config config{true}; + config.ar[r.Index()] = RegConfig::Memory; + return config; +} + +template <typename ArpRnX> +Config ConfigWithArpAddress(ArpRnX r) { + static_assert(std::is_same_v<ArpRnX, ArpRn1> || std::is_same_v<ArpRnX, ArpRn2>); + Config config{true}; + config.arp[r.Index()] = RegConfig::Memory; + return config; +} + +Config ConfigWithImm8(Imm8 imm) { + std::unordered_set<u16> random_pos = { + 0, + 35, + 0xE3, + }; + if (random_pos.count(imm.Unsigned16())) { + return AnyConfig; + } else { + return DisabledConfig; + } +} + +Config ConfigWithMemImm8(MemImm8 mem) { + std::unordered_set<u16> random_pos = { + 0, + 0x88, + 0xFF, + }; + if (random_pos.count(mem.Unsigned16())) { + Config c = AnyConfig; + c.lock_page = true; + return c; + } else { + return DisabledConfig; + } +} + +Config ConfigWithMemR7Imm16() { + Config c = AnyConfig; + c.lock_r7 = true; + return c; +} + +Config ConfigWithMemR7Imm7s(MemR7Imm7s mem) { + std::unordered_set<u16> random_pos = { + 0, + 4, + 0xFFFE, + }; + if (random_pos.count(mem.Signed16())) { + Config c = AnyConfig; + c.lock_r7 = true; + return c; + } else { + return DisabledConfig; + } +} + +class TestGenerator { +public: + bool IsUnimplementedRegister(RegName r) { + return r == RegName::pc || r == RegName::undefine || r == RegName::st0 || + r == RegName::st1 || r == RegName::st2 || r == RegName::stt2 || r == RegName::ext0 || + r == RegName::ext1 || r == RegName::ext2 || r == RegName::ext3 || + r == RegName::mod3 || r == RegName::sp; + } + + using instruction_return_type = Config; + + Config undefined(u16 opcode) { + return DisabledConfig; + } + + Config nop() { + return DisabledConfig; + } + + Config norm(Ax a, Rn b, StepZIDS bs) { + return AnyConfig; + } + Config swap(SwapType swap) { + if (swap.GetName() == SwapTypeValue::reserved0 || + swap.GetName() == SwapTypeValue::reserved1) + return DisabledConfig; + return AnyConfig; + } + Config trap() { + return DisabledConfig; + } + + Config alm(Alm op, MemImm8 a, Ax b) { + if (op.GetName() == AlmOp::Sqr && b.GetName() != RegName::a0) { + return DisabledConfig; + } + return ConfigWithMemImm8(a); + } + Config alm(Alm op, Rn a, StepZIDS as, Ax b) { + if (op.GetName() == AlmOp::Sqr && b.GetName() != RegName::a0) { + return DisabledConfig; + } + return ConfigWithAddress(a); + } + Config alm(Alm op, Register a, Ax b) { + if (op.GetName() == AlmOp::Sqr && b.GetName() != RegName::a0) { + return DisabledConfig; + } + + if (IsUnimplementedRegister(a.GetName())) { + return DisabledConfig; + } + + switch (a.GetName()) { + case RegName::p: + case RegName::a0: + case RegName::a1: { + static const std::unordered_set<AlmOp> allowed_instruction{ + AlmOp::Or, AlmOp::And, AlmOp::Xor, AlmOp::Add, AlmOp::Cmp, AlmOp::Sub, + }; + if (allowed_instruction.count(op.GetName()) != 0) + return AnyConfig; + else + return DisabledConfig; + } + default: + return AnyConfig; + } + } + Config alm_r6(Alm op, Ax b) { + // op = sqr, b = a1 is excluded by decoder + return AnyConfig; + } + + Config alu(Alu op, MemImm16 a, Ax b) { + // reserved op excluded by decoder + return AnyConfig.WithMemoryExpand(); + } + Config alu(Alu op, MemR7Imm16 a, Ax b) { + return ConfigWithMemR7Imm16(); + } + Config alu(Alu op, Imm16 a, Ax b) { + return AnyConfig.WithAnyExpand(); + } + Config alu(Alu op, Imm8 a, Ax b) { + return ConfigWithImm8(a); + } + Config alu(Alu op, MemR7Imm7s a, Ax b) { + return ConfigWithMemR7Imm7s(a); + } + + Config or_(Ab a, Ax b, Ax c) { + return AnyConfig; + } + Config or_(Ax a, Bx b, Ax c) { + return AnyConfig; + } + Config or_(Bx a, Bx b, Ax c) { + return AnyConfig; + } + + Config alb(Alb op, Imm16 a, MemImm8 b) { + return ConfigWithMemImm8(b).WithAnyExpand(); + } + Config alb(Alb op, Imm16 a, Rn b, StepZIDS bs) { + return ConfigWithAddress(b).WithAnyExpand(); + } + Config alb(Alb op, Imm16 a, Register b) { + if (IsUnimplementedRegister(b.GetName())) { + return DisabledConfig; + } + switch (b.GetName()) { + case RegName::a0: + case RegName::a1: + return DisabledConfig; + default: + return AnyConfig.WithAnyExpand(); + } + } + Config alb_r6(Alb op, Imm16 a) { + return AnyConfig.WithAnyExpand(); + } + Config alb(Alb op, Imm16 a, SttMod b) { + if (IsUnimplementedRegister(b.GetName())) { + return DisabledConfig; + } + return AnyConfig.WithAnyExpand(); + } + + Config add(Ab a, Bx b) { + return AnyConfig; + } + Config add(Bx a, Ax b) { + return AnyConfig; + } + Config add_p1(Ax b) { + return AnyConfig; + } + Config add(Px a, Bx b) { + return AnyConfig; + } + + Config sub(Ab a, Bx b) { + return AnyConfig; + } + Config sub(Bx a, Ax b) { + return AnyConfig; + } + Config sub_p1(Ax b) { + return AnyConfig; + } + Config sub(Px a, Bx b) { + return AnyConfig; + } + + Config app(Ab c, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, bool p1_align) { + return AnyConfig; + } + + Config add_add(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return ConfigWithArpAddress(a); + } + Config add_sub(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return ConfigWithArpAddress(a); + } + Config sub_add(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return ConfigWithArpAddress(a); + } + Config sub_sub(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return ConfigWithArpAddress(a); + } + + Config add_sub_sv(ArRn1 a, ArStep1 as, Ab b) { + return ConfigWithArAddress(a); + } + Config sub_add_sv(ArRn1 a, ArStep1 as, Ab b) { + return ConfigWithArAddress(a); + } + + Config sub_add_i_mov_j_sv(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return ConfigWithArpAddress(a); + } + Config sub_add_j_mov_i_sv(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return ConfigWithArpAddress(a); + } + Config add_sub_i_mov_j(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return ConfigWithArpAddress(a); + } + Config add_sub_j_mov_i(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return ConfigWithArpAddress(a); + } + + Config moda4(Moda4 op, Ax a, Cond cond) { + // reserved op excluded by decoder + return AnyConfig; + } + + Config moda3(Moda3 op, Bx a, Cond cond) { + return AnyConfig; + } + + Config pacr1(Ax a) { + return AnyConfig; + } + Config clr(Ab a, Ab b) { + return AnyConfig; + } + Config clrr(Ab a, Ab b) { + return AnyConfig; + } + + Config bkrep(Imm8 a, Address16 addr) { + return DisabledConfig; + } + Config bkrep(Register a, Address18_16 addr_low, Address18_2 addr_high) { + return DisabledConfig; + } + Config bkrep_r6(Address18_16 addr_low, Address18_2 addr_high) { + return DisabledConfig; + } + Config bkreprst(ArRn2 a) { + return DisabledConfig; + } + Config bkreprst_memsp() { + return DisabledConfig; + } + Config bkrepsto(ArRn2 a) { + return DisabledConfig; + } + Config bkrepsto_memsp() { + return DisabledConfig; + } + + Config banke(BankFlags flags) { + return DisabledConfig; + } + Config bankr() { + return DisabledConfig; + } + Config bankr(Ar a) { + return DisabledConfig; + } + Config bankr(Ar a, Arp b) { + return DisabledConfig; + } + Config bankr(Arp a) { + return DisabledConfig; + } + + Config bitrev(Rn a) { + return AnyConfig; + } + Config bitrev_dbrv(Rn a) { + return AnyConfig; + } + Config bitrev_ebrv(Rn a) { + return AnyConfig; + } + + Config br(Address18_16 addr_low, Address18_2 addr_high, Cond cond) { + return DisabledConfig; + } + + Config brr(RelAddr7 addr, Cond cond) { + return DisabledConfig; + } + + Config break_() { + return DisabledConfig; + } + + Config call(Address18_16 addr_low, Address18_2 addr_high, Cond cond) { + return DisabledConfig; + } + Config calla(Axl a) { + return DisabledConfig; + } + Config calla(Ax a) { + return DisabledConfig; + } + Config callr(RelAddr7 addr, Cond cond) { + return DisabledConfig; + } + + Config cntx_s() { + return DisabledConfig; + } + Config cntx_r() { + return DisabledConfig; + } + + Config ret(Cond c) { + return DisabledConfig; + } + Config retd() { + return DisabledConfig; + } + Config reti(Cond c) { + return DisabledConfig; + } + Config retic(Cond c) { + return DisabledConfig; + } + Config retid() { + return DisabledConfig; + } + Config retidc() { + return DisabledConfig; + } + Config rets(Imm8 a) { + return DisabledConfig; + } + + Config load_ps(Imm2 a) { + return AnyConfig; + } + Config load_stepi(Imm7s a) { + return AnyConfig; + } + Config load_stepj(Imm7s a) { + return AnyConfig; + } + Config load_page(Imm8 a) { + return AnyConfig; + } + Config load_modi(Imm9 a) { + return AnyConfig; + } + Config load_modj(Imm9 a) { + return AnyConfig; + } + Config load_movpd(Imm2 a) { + return DisabledConfig; + } + Config load_ps01(Imm4 a) { + return AnyConfig; + } + + Config push(Imm16 a) { + return DisabledConfig; + } + Config push(Register a) { + return DisabledConfig; + } + Config push(Abe a) { + return DisabledConfig; + } + Config push(ArArpSttMod a) { + return DisabledConfig; + } + Config push_prpage() { + return DisabledConfig; + } + Config push(Px a) { + return DisabledConfig; + } + Config push_r6() { + return DisabledConfig; + } + Config push_repc() { + return DisabledConfig; + } + Config push_x0() { + return DisabledConfig; + } + Config push_x1() { + return DisabledConfig; + } + Config push_y1() { + return DisabledConfig; + } + Config pusha(Ax a) { + return DisabledConfig; + } + Config pusha(Bx a) { + return DisabledConfig; + } + + Config pop(Register a) { + return DisabledConfig; + } + Config pop(Abe a) { + return DisabledConfig; + } + Config pop(ArArpSttMod a) { + return DisabledConfig; + } + Config pop(Bx a) { + return DisabledConfig; + } + Config pop_prpage() { + return DisabledConfig; + } + Config pop(Px a) { + return DisabledConfig; + } + Config pop_r6() { + return DisabledConfig; + } + Config pop_repc() { + return DisabledConfig; + } + Config pop_x0() { + return DisabledConfig; + } + Config pop_x1() { + return DisabledConfig; + } + Config pop_y1() { + return DisabledConfig; + } + Config popa(Ab a) { + return DisabledConfig; + } + + Config rep(Imm8 a) { + return DisabledConfig; + } + Config rep(Register a) { + return DisabledConfig; + } + Config rep_r6() { + return DisabledConfig; + } + + Config shfc(Ab a, Ab b, Cond cond) { + return AnyConfig; + } + Config shfi(Ab a, Ab b, Imm6s s) { + return AnyConfig; + } + + Config tst4b(ArRn2 b, ArStep2 bs) { + return ConfigWithArAddress(b); + } + Config tst4b(ArRn2 b, ArStep2 bs, Ax c) { + return ConfigWithArAddress(b); + } + Config tstb(MemImm8 a, Imm4 b) { + return ConfigWithMemImm8(a); + } + Config tstb(Rn a, StepZIDS as, Imm4 b) { + return ConfigWithAddress(a); + } + Config tstb(Register a, Imm4 b) { + if (IsUnimplementedRegister(a.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config tstb_r6(Imm4 b) { + return AnyConfig; + } + Config tstb(SttMod a, Imm16 b) { + // TODO deal with the expansion + return DisabledConfig; + } + + Config and_(Ab a, Ab b, Ax c) { + return AnyConfig; + } + + Config dint() { + return DisabledConfig; + } + Config eint() { + return DisabledConfig; + } + + Config mul(Mul3 op, Rn y, StepZIDS ys, Imm16 x, Ax a) { + return ConfigWithAddress(y); + } + Config mul_y0(Mul3 op, Rn x, StepZIDS xs, Ax a) { + return ConfigWithAddress(x); + } + Config mul_y0(Mul3 op, Register x, Ax a) { + if (IsUnimplementedRegister(x.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config mul(Mul3 op, R45 y, StepZIDS ys, R0123 x, StepZIDS xs, Ax a) { + return DisabledConfig; + } + Config mul_y0_r6(Mul3 op, Ax a) { + return AnyConfig; + } + Config mul_y0(Mul2 op, MemImm8 x, Ax a) { + return ConfigWithMemImm8(x); + } + + Config mpyi(Imm8s x) { + return AnyConfig; + } + + Config msu(R45 y, StepZIDS ys, R0123 x, StepZIDS xs, Ax a) { + return DisabledConfig; + } + Config msu(Rn y, StepZIDS ys, Imm16 x, Ax a) { + return ConfigWithAddress(y).WithAnyExpand(); + } + Config msusu(ArRn2 x, ArStep2 xs, Ax a) { + return ConfigWithArAddress(x); + } + Config mac_x1to0(Ax a) { + return AnyConfig; + } + Config mac1(ArpRn1 xy, ArpStep1 xis, ArpStep1 yjs, Ax a) { + return ConfigWithArpAddress(xy); + } + + Config modr(Rn a, StepZIDS as) { + return AnyConfig; + } + Config modr_dmod(Rn a, StepZIDS as) { + return AnyConfig; + } + Config modr_i2(Rn a) { + return AnyConfig; + } + Config modr_i2_dmod(Rn a) { + return AnyConfig; + } + Config modr_d2(Rn a) { + return AnyConfig; + } + Config modr_d2_dmod(Rn a) { + return AnyConfig; + } + Config modr_eemod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + return AnyConfig; + } + Config modr_edmod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + return AnyConfig; + } + Config modr_demod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + return AnyConfig; + } + Config modr_ddmod(ArpRn2 a, ArpStep2 asi, ArpStep2 asj) { + return AnyConfig; + } + + Config movd(R0123 a, StepZIDS as, R45 b, StepZIDS bs) { + return DisabledConfig; + } + Config movp(Axl a, Register b) { + return DisabledConfig; + } + Config movp(Ax a, Register b) { + return DisabledConfig; + } + Config movp(Rn a, StepZIDS as, R0123 b, StepZIDS bs) { + return DisabledConfig; + } + Config movpdw(Ax a) { + return DisabledConfig; + } + + Config mov(Ab a, Ab b) { + return AnyConfig; + } + Config mov_dvm(Abl a) { + return DisabledConfig; + } + Config mov_x0(Abl a) { + return AnyConfig; + } + Config mov_x1(Abl a) { + return AnyConfig; + } + Config mov_y1(Abl a) { + return AnyConfig; + } + Config mov(Ablh a, MemImm8 b) { + return ConfigWithMemImm8(b); + } + Config mov(Axl a, MemImm16 b) { + return AnyConfig.WithMemoryExpand(); + } + Config mov(Axl a, MemR7Imm16 b) { + return ConfigWithMemR7Imm16(); + } + Config mov(Axl a, MemR7Imm7s b) { + return ConfigWithMemR7Imm7s(b); + } + Config mov(MemImm16 a, Ax b) { + return AnyConfig.WithMemoryExpand(); + } + Config mov(MemImm8 a, Ab b) { + return ConfigWithMemImm8(a); + } + Config mov(MemImm8 a, Ablh b) { + return ConfigWithMemImm8(a); + } + Config mov_eu(MemImm8 a, Axh b) { + return DisabledConfig; + } + Config mov(MemImm8 a, RnOld b) { + return ConfigWithMemImm8(a); + } + Config mov_sv(MemImm8 a) { + return ConfigWithMemImm8(a); + } + Config mov_dvm_to(Ab b) { + return DisabledConfig; + } + Config mov_icr_to(Ab b) { + return DisabledConfig; + } + Config mov(Imm16 a, Bx b) { + return AnyConfig.WithAnyExpand(); + } + Config mov(Imm16 a, Register b) { + if (IsUnimplementedRegister(b.GetName())) + return DisabledConfig; + return AnyConfig.WithAnyExpand(); + } + Config mov_icr(Imm5 a) { + return DisabledConfig; + } + Config mov(Imm8s a, Axh b) { + return AnyConfig; + } + Config mov(Imm8s a, RnOld b) { + return AnyConfig; + } + Config mov_sv(Imm8s a) { + return AnyConfig; + } + Config mov(Imm8 a, Axl b) { + return AnyConfig; + } + Config mov(MemR7Imm16 a, Ax b) { + return ConfigWithMemR7Imm16(); + } + Config mov(MemR7Imm7s a, Ax b) { + return ConfigWithMemR7Imm7s(a); + } + Config mov(Rn a, StepZIDS as, Bx b) { + return ConfigWithAddress(a); + } + Config mov(Rn a, StepZIDS as, Register b) { + if (IsUnimplementedRegister(b.GetName())) + return DisabledConfig; + return ConfigWithAddress(a); + } + Config mov_memsp_to(Register b) { + return DisabledConfig; + } + Config mov_mixp_to(Register b) { + if (IsUnimplementedRegister(b.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config mov(RnOld a, MemImm8 b) { + return ConfigWithMemImm8(b); + } + Config mov_icr(Register a) { + return DisabledConfig; + } + Config mov_mixp(Register a) { + if (IsUnimplementedRegister(a.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config mov(Register a, Rn b, StepZIDS bs) { + if (IsUnimplementedRegister(a.GetName())) + return DisabledConfig; + return ConfigWithAddress(b); + } + Config mov(Register a, Bx b) { + if (IsUnimplementedRegister(a.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config mov(Register a, Register b) { + if (IsUnimplementedRegister(a.GetName())) + return DisabledConfig; + if (IsUnimplementedRegister(b.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config mov_repc_to(Ab b) { + return AnyConfig; + } + Config mov_sv_to(MemImm8 b) { + return ConfigWithMemImm8(b); + } + Config mov_x0_to(Ab b) { + return AnyConfig; + } + Config mov_x1_to(Ab b) { + return AnyConfig; + } + Config mov_y1_to(Ab b) { + return AnyConfig; + } + Config mov(Imm16 a, ArArp b) { + if (IsUnimplementedRegister(b.GetName())) { + return DisabledConfig; + } + return AnyConfig.WithAnyExpand(); + } + Config mov_r6(Imm16 a) { + return AnyConfig.WithAnyExpand(); + } + Config mov_repc(Imm16 a) { + return AnyConfig.WithAnyExpand(); + } + Config mov_stepi0(Imm16 a) { + return AnyConfig.WithAnyExpand(); + } + Config mov_stepj0(Imm16 a) { + return AnyConfig.WithAnyExpand(); + } + Config mov(Imm16 a, SttMod b) { + if (IsUnimplementedRegister(b.GetName())) { + return DisabledConfig; + } + return AnyConfig.WithAnyExpand(); + } + Config mov_prpage(Imm4 a) { + return DisabledConfig; + } + + Config mov_a0h_stepi0() { + return AnyConfig; + } + Config mov_a0h_stepj0() { + return AnyConfig; + } + Config mov_stepi0_a0h() { + return AnyConfig; + } + Config mov_stepj0_a0h() { + return AnyConfig; + } + + Config mov_prpage(Abl a) { + return DisabledConfig; + } + Config mov_repc(Abl a) { + return AnyConfig; + } + Config mov(Abl a, ArArp b) { + if (IsUnimplementedRegister(b.GetName())) { + return DisabledConfig; + } + return AnyConfig; + } + Config mov(Abl a, SttMod b) { + if (IsUnimplementedRegister(b.GetName())) { + return DisabledConfig; + } + return AnyConfig; + } + + Config mov_prpage_to(Abl b) { + return DisabledConfig; + } + Config mov_repc_to(Abl b) { + return AnyConfig; + } + Config mov(ArArp a, Abl b) { + if (IsUnimplementedRegister(a.GetName())) { + return DisabledConfig; + } + return AnyConfig; + } + Config mov(SttMod a, Abl b) { + if (IsUnimplementedRegister(a.GetName())) { + return DisabledConfig; + } + return AnyConfig; + } + + Config mov_repc_to(ArRn1 b, ArStep1 bs) { + return ConfigWithArAddress(b); + } + Config mov(ArArp a, ArRn1 b, ArStep1 bs) { + if (IsUnimplementedRegister(a.GetName())) { + return DisabledConfig; + } + return ConfigWithArAddress(b); + } + Config mov(SttMod a, ArRn1 b, ArStep1 bs) { + if (IsUnimplementedRegister(a.GetName())) { + return DisabledConfig; + } + return ConfigWithArAddress(b); + } + + Config mov_repc(ArRn1 a, ArStep1 as) { + return ConfigWithArAddress(a); + } + Config mov(ArRn1 a, ArStep1 as, ArArp b) { + if (IsUnimplementedRegister(b.GetName())) { + return DisabledConfig; + } + return ConfigWithArAddress(a); + } + Config mov(ArRn1 a, ArStep1 as, SttMod b) { + if (IsUnimplementedRegister(b.GetName())) { + return DisabledConfig; + } + return ConfigWithArAddress(a); + } + + Config mov_repc_to(MemR7Imm16 b) { + return ConfigWithMemR7Imm16(); + } + Config mov(ArArpSttMod a, MemR7Imm16 b) { + if (IsUnimplementedRegister(a.GetName())) { + return DisabledConfig; + } + return ConfigWithMemR7Imm16(); + } + + Config mov_repc(MemR7Imm16 a) { + return ConfigWithMemR7Imm16(); + } + Config mov(MemR7Imm16 a, ArArpSttMod b) { + if (IsUnimplementedRegister(b.GetName())) { + return DisabledConfig; + } + return ConfigWithMemR7Imm16(); + } + + Config mov_pc(Ax a) { + return DisabledConfig; + } + Config mov_pc(Bx a) { + return DisabledConfig; + } + + Config mov_mixp_to(Bx b) { + return AnyConfig; + } + Config mov_mixp_r6() { + return AnyConfig; + } + Config mov_p0h_to(Bx b) { + return AnyConfig; + } + Config mov_p0h_r6() { + return AnyConfig; + } + Config mov_p0h_to(Register b) { + if (IsUnimplementedRegister(b.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config mov_p0(Ab a) { + return AnyConfig; + } + Config mov_p1_to(Ab b) { + return AnyConfig; + } + + Config mov2(Px a, ArRn2 b, ArStep2 bs) { + return ConfigWithArAddress(b); + } + Config mov2s(Px a, ArRn2 b, ArStep2 bs) { + return ConfigWithArAddress(b); + } + Config mov2(ArRn2 a, ArStep2 as, Px b) { + return ConfigWithArAddress(a); + } + Config mova(Ab a, ArRn2 b, ArStep2 bs) { + return ConfigWithArAddress(b); + } + Config mova(ArRn2 a, ArStep2 as, Ab b) { + return ConfigWithArAddress(a); + } + + Config mov_r6_to(Bx b) { + return AnyConfig; + } + Config mov_r6_mixp() { + return AnyConfig; + } + Config mov_r6_to(Register b) { + if (IsUnimplementedRegister(b.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config mov_r6(Register a) { + if (IsUnimplementedRegister(a.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config mov_memsp_r6() { + return DisabledConfig; + } + Config mov_r6_to(Rn b, StepZIDS bs) { + return ConfigWithAddress(b); + } + Config mov_r6(Rn a, StepZIDS as) { + return ConfigWithAddress(a); + } + + Config movs(MemImm8 a, Ab b) { + return ConfigWithMemImm8(a); + } + Config movs(Rn a, StepZIDS as, Ab b) { + return ConfigWithAddress(a); + } + Config movs(Register a, Ab b) { + if (IsUnimplementedRegister(a.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config movs_r6_to(Ax b) { + return AnyConfig; + } + Config movsi(RnOld a, Ab b, Imm5s s) { + return AnyConfig; + } + + Config mov2_axh_m_y0_m(Axh a, ArRn2 b, ArStep2 bs) { + return ConfigWithArAddress(b); + } + Config mov2_ax_mij(Ab a, ArpRn1 b, ArpStep1 bsi, ArpStep1 bsj) { + return ConfigWithArpAddress(b); + } + Config mov2_ax_mji(Ab a, ArpRn1 b, ArpStep1 bsi, ArpStep1 bsj) { + return ConfigWithArpAddress(b); + } + Config mov2_mij_ax(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return ConfigWithArpAddress(a); + } + Config mov2_mji_ax(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, Ab b) { + return ConfigWithArpAddress(a); + } + Config mov2_abh_m(Abh ax, Abh ay, ArRn1 b, ArStep1 bs) { + return ConfigWithArAddress(b); + } + Config exchange_iaj(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + return ConfigWithArpAddress(b); + } + Config exchange_riaj(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + return ConfigWithArpAddress(b); + } + Config exchange_jai(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + return ConfigWithArpAddress(b); + } + Config exchange_rjai(Axh a, ArpRn2 b, ArpStep2 bsi, ArpStep2 bsj) { + return ConfigWithArpAddress(b); + } + + Config movr(ArRn2 a, ArStep2 as, Abh b) { + return ConfigWithArAddress(a); + } + Config movr(Rn a, StepZIDS as, Ax b) { + return ConfigWithAddress(a); + } + Config movr(Register a, Ax b) { + if (IsUnimplementedRegister(a.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config movr(Bx a, Ax b) { + return AnyConfig; + } + Config movr_r6_to(Ax b) { + return AnyConfig; + } + + Config exp(Bx a) { + return AnyConfig; + } + Config exp(Bx a, Ax b) { + return AnyConfig; + } + Config exp(Rn a, StepZIDS as) { + return ConfigWithAddress(a); + } + Config exp(Rn a, StepZIDS as, Ax b) { + return ConfigWithAddress(a); + } + Config exp(Register a) { + if (IsUnimplementedRegister(a.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config exp(Register a, Ax b) { + if (IsUnimplementedRegister(a.GetName())) + return DisabledConfig; + return AnyConfig; + } + Config exp_r6() { + return AnyConfig; + } + Config exp_r6(Ax b) { + return AnyConfig; + } + + Config lim(Ax a, Ax b) { + return AnyConfig; + } + + Config vtrclr0() { + return DisabledConfig; + } + Config vtrclr1() { + return DisabledConfig; + } + Config vtrclr() { + return DisabledConfig; + } + Config vtrmov0(Axl a) { + return DisabledConfig; + } + Config vtrmov1(Axl a) { + return DisabledConfig; + } + Config vtrmov(Axl a) { + return DisabledConfig; + } + Config vtrshr() { + return DisabledConfig; + } + + Config clrp0() { + return AnyConfig; + } + Config clrp1() { + return AnyConfig; + } + Config clrp() { + return AnyConfig; + } + + Config max_ge(Ax a, StepZIDS bs) { + return AnyConfig; + } + Config max_gt(Ax a, StepZIDS bs) { + return AnyConfig; + } + Config min_le(Ax a, StepZIDS bs) { + return AnyConfig; + } + Config min_lt(Ax a, StepZIDS bs) { + return AnyConfig; + } + + Config max_ge_r0(Ax a, StepZIDS bs) { + return ConfigWithAddress(Rn{0}); + } + Config max_gt_r0(Ax a, StepZIDS bs) { + return ConfigWithAddress(Rn{0}); + } + Config min_le_r0(Ax a, StepZIDS bs) { + return ConfigWithAddress(Rn{0}); + } + Config min_lt_r0(Ax a, StepZIDS bs) { + return ConfigWithAddress(Rn{0}); + } + Config divs(MemImm8 a, Ax b) { + return ConfigWithMemImm8(a); + } + + Config sqr_sqr_add3(Ab a, Ab b) { + return AnyConfig; + } + Config sqr_sqr_add3(ArRn2 a, ArStep2 as, Ab b) { + return ConfigWithArAddress(a); + } + Config sqr_mpysu_add3a(Ab a, Ab b) { + return AnyConfig; + } + + Config cmp(Ax a, Bx b) { + return AnyConfig; + } + Config cmp_b0_b1() { + return AnyConfig; + } + Config cmp_b1_b0() { + return AnyConfig; + } + Config cmp(Bx a, Ax b) { + return AnyConfig; + } + Config cmp_p1_to(Ax b) { + return AnyConfig; + } + + Config max2_vtr(Ax a) { + return AnyConfig; + } + Config min2_vtr(Ax a) { + return AnyConfig; + } + Config max2_vtr(Ax a, Bx b) { + return AnyConfig; + } + Config min2_vtr(Ax a, Bx b) { + return AnyConfig; + } + Config max2_vtr_movl(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + return ConfigWithArAddress(c); + } + Config max2_vtr_movh(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + return ConfigWithArAddress(c); + } + Config max2_vtr_movl(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + return ConfigWithArAddress(c); + } + Config max2_vtr_movh(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + return ConfigWithArAddress(c); + } + Config min2_vtr_movl(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + return ConfigWithArAddress(c); + } + Config min2_vtr_movh(Ax a, Bx b, ArRn1 c, ArStep1 cs) { + return ConfigWithArAddress(c); + } + Config min2_vtr_movl(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + return ConfigWithArAddress(c); + } + Config min2_vtr_movh(Bx a, Ax b, ArRn1 c, ArStep1 cs) { + return ConfigWithArAddress(c); + } + Config max2_vtr_movij(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + return ConfigWithArpAddress(c); + } + Config max2_vtr_movji(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + return ConfigWithArpAddress(c); + } + Config min2_vtr_movij(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + return ConfigWithArpAddress(c); + } + Config min2_vtr_movji(Ax a, Bx b, ArpRn1 c, ArpStep1 csi, ArpStep1 csj) { + return ConfigWithArpAddress(c); + } + + template <typename ArpStepX> + Config mov_sv_app(ArRn1 a, ArpStepX as, Bx b, SumBase base, bool sub_p0, bool p0_align, + bool sub_p1, bool p1_align) { + return ConfigWithArAddress(a); + } + + Config cbs(Axh a, CbsCond c) { + // return AnyConfig; + return DisabledConfig; + } + Config cbs(Axh a, Bxh b, CbsCond c) { + // return AnyConfig; + return DisabledConfig; + } + Config cbs(ArpRn1 a, ArpStep1 asi, ArpStep1 asj, CbsCond c) { + // return ConfigWithArpAddress(a); + return DisabledConfig; + } + + Config mma(RegName a, bool x0_sign, bool y0_sign, bool x1_sign, bool y1_sign, SumBase base, + bool sub_p0, bool p0_align, bool sub_p1, bool p1_align) { + return AnyConfig; + } + + template <typename ArpRnX, typename ArpStepX> + Config mma(ArpRnX xy, ArpStepX i, ArpStepX j, bool dmodi, bool dmodj, RegName a, bool x0_sign, + bool y0_sign, bool x1_sign, bool y1_sign, SumBase base, bool sub_p0, bool p0_align, + bool sub_p1, bool p1_align) { + return ConfigWithArpAddress(xy); + } + + Config mma_mx_xy(ArRn1 y, ArStep1 ys, RegName a, bool x0_sign, bool y0_sign, bool x1_sign, + bool y1_sign, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + return ConfigWithArAddress(y); + } + + Config mma_xy_mx(ArRn1 y, ArStep1 ys, RegName a, bool x0_sign, bool y0_sign, bool x1_sign, + bool y1_sign, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + return ConfigWithArAddress(y); + } + + Config mma_my_my(ArRn1 x, ArStep1 xs, RegName a, bool x0_sign, bool y0_sign, bool x1_sign, + bool y1_sign, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + return ConfigWithArAddress(x); + } + + Config mma_mov(Axh u, Bxh v, ArRn1 w, ArStep1 ws, RegName a, bool x0_sign, bool y0_sign, + bool x1_sign, bool y1_sign, SumBase base, bool sub_p0, bool p0_align, + bool sub_p1, bool p1_align) { + return ConfigWithArAddress(w); + } + + Config mma_mov(ArRn2 w, ArStep1 ws, RegName a, bool x0_sign, bool y0_sign, bool x1_sign, + bool y1_sign, SumBase base, bool sub_p0, bool p0_align, bool sub_p1, + bool p1_align) { + return ConfigWithArAddress(w); + } + + Config addhp(ArRn2 a, ArStep2 as, Px b, Ax c) { + return ConfigWithArAddress(a); + } + + Config mov_ext0(Imm8s a) { + return DisabledConfig; + } + Config mov_ext1(Imm8s a) { + return DisabledConfig; + } + Config mov_ext2(Imm8s a) { + return DisabledConfig; + } + Config mov_ext3(Imm8s a) { + return DisabledConfig; + } +}; +} // Anonymous namespace + +bool GenerateTestCasesToFile(const char* path) { + std::unique_ptr<std::FILE, decltype(&std::fclose)> f{std::fopen(path, "wb"), std::fclose}; + if (!f) { + return false; + } + + TestGenerator generator; + for (u32 i = 0; i < 0x10000; ++i) { + u16 opcode = (u16)i; + auto decoded = Decode<TestGenerator>(opcode); + Config config = decoded.call(generator, opcode, 0); + if (!config.enable) + continue; + + for (int j = 0; j < 4; ++j) { + TestCase test_case{}; + test_case.before = config.GenerateRandomState(); + test_case.opcode = opcode; + + switch (config.expand) { + case ExpandConfig::None: + test_case.expand = 0; + break; + case ExpandConfig::Any: + test_case.expand = Random::bit16(); + break; + case ExpandConfig::Memory: + test_case.expand = TestSpaceX + (u16)Random::uniform(10, TestSpaceSize - 10); + break; + } + + if (std::fwrite(&test_case, sizeof(test_case), 1, f.get()) == 0) { + return false; + } + } + } + + return true; +} + +} // namespace Teakra::Test diff --git a/src/teakra/src/test_generator.h b/src/teakra/src/test_generator.h new file mode 100644 index 0000000..b7e6651 --- /dev/null +++ b/src/teakra/src/test_generator.h @@ -0,0 +1,5 @@ +#pragma once + +namespace Teakra::Test { +bool GenerateTestCasesToFile(const char* path); +} diff --git a/src/teakra/src/test_generator/CMakeLists.txt b/src/teakra/src/test_generator/CMakeLists.txt new file mode 100644 index 0000000..a9b8312 --- /dev/null +++ b/src/teakra/src/test_generator/CMakeLists.txt @@ -0,0 +1,9 @@ +include(CreateDirectoryGroups) + +add_executable(test_generator + main.cpp +) +create_target_directory_groups(test_generator) +target_link_libraries(test_generator PRIVATE teakra) +target_include_directories(test_generator PRIVATE .) +target_compile_options(test_generator PRIVATE ${TEAKRA_CXX_FLAGS}) diff --git a/src/teakra/src/test_generator/main.cpp b/src/teakra/src/test_generator/main.cpp new file mode 100644 index 0000000..97cc626 --- /dev/null +++ b/src/teakra/src/test_generator/main.cpp @@ -0,0 +1,15 @@ +#include <cstdio> +#include "../test_generator.h" + +int main(int argc, char** argv) { + if (argc < 2) { + return -1; + } + + if (!Teakra::Test::GenerateTestCasesToFile(argv[1])) { + std::fprintf(stderr, "Unable to successfully generate all tests.\n"); + return -2; + } + + return 0; +} diff --git a/src/teakra/src/test_verifier/CMakeLists.txt b/src/teakra/src/test_verifier/CMakeLists.txt new file mode 100644 index 0000000..0ca2cfc --- /dev/null +++ b/src/teakra/src/test_verifier/CMakeLists.txt @@ -0,0 +1,36 @@ +include(CreateDirectoryGroups) + +add_executable(test_verifier + main.cpp +) +create_target_directory_groups(test_verifier) +target_link_libraries(test_verifier PRIVATE teakra) +target_include_directories(test_verifier PRIVATE .) +target_compile_options(test_verifier PRIVATE ${TEAKRA_CXX_FLAGS}) + + +# Test related stuff +set(ASSET_SHA256SUM "baffcd4f805a7480d969401792443a34aa39f813b4f0ae49c6365f1d1f3ce120") +if(TEAKRA_RUN_TESTS) + message(STATUS "Will run Teakra accuracy tests") + # download fixtures if there is none + if(NOT EXISTS "${TEAKRA_TEST_ASSETS_DIR}/teaklite2_tests_result") + message(STATUS "Downloading required samples...") + file(DOWNLOAD + "https://liushuyu.b-cdn.net/teaklite2_tests_result_20181208" + "${TEAKRA_TEST_ASSETS_DIR}/teaklite2_tests_result" + EXPECTED_HASH SHA256=${ASSET_SHA256SUM} + SHOW_PROGRESS + ) + else() + # check if provided fixtures are good + file(SHA256 "${TEAKRA_TEST_ASSETS_DIR}/teaklite2_tests_result" ASSET_CHECKSUM) + if(ASSET_SHA256SUM STREQUAL ASSET_CHECKSUM) + message(STATUS "Unit test sample looks good.") + else() + message(FATAL_ERROR "Unit test sample broken. Please remove the file and re-run CMake.") + endif() + endif() + + add_test(NAME tests COMMAND test_verifier "${TEAKRA_TEST_ASSETS_DIR}/teaklite2_tests_result") +endif(TEAKRA_RUN_TESTS) diff --git a/src/teakra/src/test_verifier/main.cpp b/src/teakra/src/test_verifier/main.cpp new file mode 100644 index 0000000..9ef55da --- /dev/null +++ b/src/teakra/src/test_verifier/main.cpp @@ -0,0 +1,253 @@ +#include <cinttypes> +#include <cstdio> +#include <iomanip> +#include <memory> +#include <teakra/disassembler.h> +#include "../core_timing.h" +#include "../interpreter.h" +#include "../memory_interface.h" +#include "../shared_memory.h" +#include "../test.h" + +std::string Flag16ToString(u16 value, const char* symbols) { + std::string result = symbols; + for (int i = 0; i < 16; ++i) { + if ((value >> i & 1) == 0) + result[15 - i] = '-'; + } + return result; +} + +int main(int argc, char** argv) { + if (argc < 2) { + std::fprintf(stderr, "A filename argument must be provided. Exiting...\n"); + return -1; + } + + std::unique_ptr<std::FILE, decltype(&std::fclose)> file{std::fopen(argv[1], "rb"), std::fclose}; + if (!file) { + std::fprintf(stderr, "Unable to open file %s. Exiting...\n", argv[1]); + return -2; + } + + Teakra::CoreTiming core_timing; + Teakra::SharedMemory shared_memory; + Teakra::MemoryInterfaceUnit miu; + Teakra::MemoryInterface memory_interface{shared_memory, miu}; + Teakra::RegisterState regs; + Teakra::Interpreter interpreter(core_timing, regs, memory_interface); + + int i = 0; + int passed = 0; + int total = 0; + int skipped = 0; + while (true) { + TestCase test_case; + if (std::fread(&test_case, sizeof(test_case), 1, file.get()) == 0) { + break; + } + regs.Reset(); + regs.a = test_case.before.a; + regs.b = test_case.before.b; + regs.p = test_case.before.p; + regs.r = test_case.before.r; + regs.x = test_case.before.x; + regs.y = test_case.before.y; + regs.stepi0 = test_case.before.stepi0; + regs.stepj0 = test_case.before.stepj0; + regs.mixp = test_case.before.mixp; + regs.sv = test_case.before.sv; + regs.repc = test_case.before.repc; + regs.Lc() = test_case.before.lc; + regs.Set<Teakra::cfgi>(test_case.before.cfgi); + regs.Set<Teakra::cfgj>(test_case.before.cfgj); + regs.Set<Teakra::stt0>(test_case.before.stt0); + regs.Set<Teakra::stt1>(test_case.before.stt1); + regs.Set<Teakra::stt2>(test_case.before.stt2); + regs.Set<Teakra::mod0>(test_case.before.mod0); + regs.Set<Teakra::mod1>(test_case.before.mod1); + regs.Set<Teakra::mod2>(test_case.before.mod2); + regs.Set<Teakra::ar0>(test_case.before.ar[0]); + regs.Set<Teakra::ar1>(test_case.before.ar[1]); + regs.Set<Teakra::arp0>(test_case.before.arp[0]); + regs.Set<Teakra::arp1>(test_case.before.arp[1]); + regs.Set<Teakra::arp2>(test_case.before.arp[2]); + regs.Set<Teakra::arp3>(test_case.before.arp[3]); + + for (u16 offset = 0; offset < TestSpaceSize; ++offset) { + memory_interface.DataWrite(TestSpaceX + offset, test_case.before.test_space_x[offset]); + memory_interface.DataWrite(TestSpaceY + offset, test_case.before.test_space_y[offset]); + } + + memory_interface.ProgramWrite(0, test_case.opcode); + memory_interface.ProgramWrite(1, test_case.expand); + + bool pass = true; + bool skip = false; + try { + interpreter.Run(1); + auto Check40 = [&](const char* name, u64 expected, u64 actual) { + if (expected != actual) { + std::printf("Mismatch: %s: %010" PRIx64 " != %010" PRIx64 "\n", name, + expected & 0xFF'FFFF'FFFF, actual & 0xFF'FFFF'FFFF); + pass = false; + } + }; + + auto Check32 = [&](const char* name, u32 expected, u32 actual) { + if (expected != actual) { + std::printf("Mismatch: %s: %08X != %08X\n", name, expected, actual); + pass = false; + } + }; + + auto Check = [&](const char* name, u16 expected, u16 actual) { + if (expected != actual) { + std::printf("Mismatch: %s: %04X != %04X\n", name, expected, actual); + pass = false; + } + }; + + auto CheckAddress = [&](const char* name, u16 address, u16 expected, u16 actual) { + if (expected != actual) { + std::printf("Mismatch: %s%04X: %04X != %04X\n", name, address, expected, + actual); + pass = false; + } + }; + + auto CheckFlag = [&](const char* name, u16 expected, u16 actual, const char* symbols) { + if (expected != actual) { + std::printf("Mismatch: %s: %s != %s\n", name, + Flag16ToString(expected, symbols).c_str(), + Flag16ToString(actual, symbols).c_str()); + pass = false; + } + }; + + Check40("a0", SignExtend<40>(test_case.after.a[0]), regs.a[0]); + Check40("a1", SignExtend<40>(test_case.after.a[1]), regs.a[1]); + Check40("b0", SignExtend<40>(test_case.after.b[0]), regs.b[0]); + Check40("b1", SignExtend<40>(test_case.after.b[1]), regs.b[1]); + Check32("p0", test_case.after.p[0], regs.p[0]); + Check32("p1", test_case.after.p[1], regs.p[1]); + Check("r0", test_case.after.r[0], regs.r[0]); + Check("r1", test_case.after.r[1], regs.r[1]); + Check("r2", test_case.after.r[2], regs.r[2]); + Check("r3", test_case.after.r[3], regs.r[3]); + Check("r4", test_case.after.r[4], regs.r[4]); + Check("r5", test_case.after.r[5], regs.r[5]); + Check("r6", test_case.after.r[6], regs.r[6]); + Check("r7", test_case.after.r[7], regs.r[7]); + Check("x0", test_case.after.x[0], regs.x[0]); + Check("x1", test_case.after.x[1], regs.x[1]); + Check("y0", test_case.after.y[0], regs.y[0]); + Check("y1", test_case.after.y[1], regs.y[1]); + Check("stepi0", test_case.after.stepi0, regs.stepi0); + Check("stepj0", test_case.after.stepj0, regs.stepj0); + Check("mixp", test_case.after.mixp, regs.mixp); + Check("sv", test_case.after.sv, regs.sv); + Check("repc", test_case.after.repc, regs.repc); + Check("lc", test_case.after.lc, regs.Lc()); + CheckFlag("cfgi", test_case.after.cfgi, regs.Get<Teakra::cfgi>(), "mmmmmmmmmsssssss"); + CheckFlag("cfgj", test_case.after.cfgj, regs.Get<Teakra::cfgj>(), "mmmmmmmmmsssssss"); + CheckFlag("stt0", test_case.after.stt0, regs.Get<Teakra::stt0>(), "####C###ZMNVCELL"); + CheckFlag("stt1", test_case.after.stt1, regs.Get<Teakra::stt1>(), "QP#########R####"); + CheckFlag("stt2", test_case.after.stt2, regs.Get<Teakra::stt2>(), "LBBB####mm##V21I"); + CheckFlag("mod0", test_case.after.mod0, regs.Get<Teakra::mod0>(), "#QQ#PPooSYY###SS"); + CheckFlag("mod1", test_case.after.mod1, regs.Get<Teakra::mod1>(), "???B####pppppppp"); + CheckFlag("mod2", test_case.after.mod2, regs.Get<Teakra::mod2>(), "7654321m7654321M"); + CheckFlag("ar0", test_case.after.ar[0], regs.Get<Teakra::ar0>(), "RRRRRRoosssoosss"); + CheckFlag("ar1", test_case.after.ar[1], regs.Get<Teakra::ar1>(), "RRRRRRoosssoosss"); + CheckFlag("arp0", test_case.after.arp[0], regs.Get<Teakra::arp0>(), "#RR#RRjjjjjiiiii"); + CheckFlag("arp1", test_case.after.arp[1], regs.Get<Teakra::arp1>(), "#RR#RRjjjjjiiiii"); + CheckFlag("arp2", test_case.after.arp[2], regs.Get<Teakra::arp2>(), "#RR#RRjjjjjiiiii"); + CheckFlag("arp3", test_case.after.arp[3], regs.Get<Teakra::arp3>(), "#RR#RRjjjjjiiiii"); + + for (u16 offset = 0; offset < TestSpaceSize; ++offset) { + CheckAddress("memory_", (TestSpaceX + offset), test_case.after.test_space_x[offset], + memory_interface.DataRead(TestSpaceX + offset)); + CheckAddress("memory_", (TestSpaceY + offset), test_case.after.test_space_y[offset], + memory_interface.DataRead(TestSpaceY + offset)); + } + ++total; + } catch (const Teakra::UnimplementedException&) { + std::printf("Skipped one unimplemented case\n"); + pass = false; + skip = true; + ++skipped; + } + + if (pass) { + ++passed; + } else { + Teakra::Disassembler::ArArpSettings ar_arp; + ar_arp.ar = test_case.before.ar; + ar_arp.arp = test_case.before.arp; + std::printf( + "Test case %d: %04X %04X %s\n", i, test_case.opcode, test_case.expand, + Teakra::Disassembler::Do(test_case.opcode, test_case.expand, ar_arp).c_str()); + if (!skip) { + std::printf("before:\n"); + std::printf("a0 = %010" PRIx64 "; a1 = %010" PRIx64 "\n", + test_case.before.a[0] & 0xFF'FFFF'FFFF, + test_case.before.a[1] & 0xFF'FFFF'FFFF); + std::printf("b0 = %010" PRIx64 "; b1 = %010" PRIx64 "\n", + test_case.before.b[0] & 0xFF'FFFF'FFFF, + test_case.before.b[1] & 0xFF'FFFF'FFFF); + std::printf("p0 = %08X; p1 = %08X\n", test_case.before.p[0], test_case.before.p[1]); + std::printf("x0 = %04X; x1 = %04X\n", test_case.before.x[0], test_case.before.x[1]); + std::printf("y0 = %04X; y1 = %04X\n", test_case.before.y[0], test_case.before.y[1]); + std::printf("r0 = %04X; r1 = %04X; r2 = %04X; r3 = %04X\n", test_case.before.r[0], + test_case.before.r[1], test_case.before.r[2], test_case.before.r[3]); + std::printf("r4 = %04X; r5 = %04X; r6 = %04X; r7 = %04X\n", test_case.before.r[4], + test_case.before.r[5], test_case.before.r[6], test_case.before.r[7]); + std::printf("stepi0 = %04X\n", test_case.before.stepi0); + std::printf("stepj0 = %04X\n", test_case.before.stepj0); + std::printf("mixp = %04X\n", test_case.before.mixp); + std::printf("sv = %04X\n", test_case.before.sv); + std::printf("repc = %04X\n", test_case.before.repc); + std::printf("lc = %04X\n", test_case.before.lc); + std::printf("cfgi = %s\n", + Flag16ToString(test_case.before.cfgi, "mmmmmmmmmsssssss").c_str()); + std::printf("cfgj = %s\n", + Flag16ToString(test_case.before.cfgj, "mmmmmmmmmsssssss").c_str()); + std::printf("stt0 = %s\n", + Flag16ToString(test_case.before.stt0, "####C###ZMNVCELL").c_str()); + std::printf("stt1 = %s\n", + Flag16ToString(test_case.before.stt1, "QP#########R####").c_str()); + std::printf("stt2 = %s\n", + Flag16ToString(test_case.before.stt2, "LBBB####mm##V21I").c_str()); + std::printf("mod0 = %s\n", + Flag16ToString(test_case.before.mod0, "#QQ#PPooSYY###SS").c_str()); + std::printf("mod1 = %s\n", + Flag16ToString(test_case.before.mod1, "jicB####pppppppp").c_str()); + std::printf("mod2 = %s\n", + Flag16ToString(test_case.before.mod2, "7654321m7654321M").c_str()); + std::printf("ar0 = %s\n", + Flag16ToString(test_case.before.ar[0], "RRRRRRoosssoosss").c_str()); + std::printf("ar1 = %s\n", + Flag16ToString(test_case.before.ar[1], "RRRRRRoosssoosss").c_str()); + std::printf("arp0 = %s\n", + Flag16ToString(test_case.before.arp[0], "#RR#RRiiiiijjjjj").c_str()); + std::printf("arp1 = %s\n", + Flag16ToString(test_case.before.arp[1], "#RR#RRiiiiijjjjj").c_str()); + std::printf("arp2 = %s\n", + Flag16ToString(test_case.before.arp[2], "#RR#RRiiiiijjjjj").c_str()); + std::printf("arp3 = %s\n", + Flag16ToString(test_case.before.arp[3], "#RR#RRiiiiijjjjj").c_str()); + std::printf("FAILED\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"); + } + } + + ++i; + } + + std::printf("%d / %d passed, %d skipped\n", passed, total, skipped); + + if (passed < total) { + return 1; + } + + return 0; +} diff --git a/src/teakra/src/timer.cpp b/src/teakra/src/timer.cpp new file mode 100644 index 0000000..434a33e --- /dev/null +++ b/src/teakra/src/timer.cpp @@ -0,0 +1,113 @@ +#include "crash.h" +#include "timer.h" + +namespace Teakra { + +void Timer::Reset() { + update_mmio = 0; + pause = 0; + count_mode = CountMode::Single; + scale = 0; + + start_high = 0; + start_low = 0; + counter = 0; + counter_high = 0; + counter_low = 0; +} + +void Timer::Restart() { + ASSERT(static_cast<u16>(count_mode) < 4); + if (count_mode != CountMode::FreeRunning) { + counter = ((u32)start_high << 16) | start_low; + UpdateMMIO(); + } +} + +void Timer::Tick() { + ASSERT(static_cast<u16>(count_mode) < 4); + ASSERT(scale == 0); + if (pause) + return; + if (count_mode == CountMode::EventCount) + return; + if (counter == 0) { + if (count_mode == CountMode::AutoRestart) { + Restart(); + } else if (count_mode == CountMode::FreeRunning) { + counter = 0xFFFFFFFF; + UpdateMMIO(); + } + } else { + --counter; + UpdateMMIO(); + if (counter == 0) + interrupt_handler(); + } +} + +void Timer::TickEvent() { + if (pause) + return; + if (count_mode != CountMode::EventCount) + return; + if (counter == 0) + return; + --counter; + UpdateMMIO(); + if (counter == 0) + interrupt_handler(); +} + +void Timer::UpdateMMIO() { + if (!update_mmio) + return; + counter_high = counter >> 16; + counter_low = counter & 0xFFFF; +} + +u64 Timer::GetMaxSkip() const { + if (pause || count_mode == CountMode::EventCount) + return Infinity; + + if (counter == 0) { + if (count_mode == CountMode::AutoRestart) { + return ((u32)start_high << 16) | start_low; + } else if (count_mode == CountMode::FreeRunning) { + return 0xFFFFFFFF; + } else /*Single*/ { + return Infinity; + } + } + + return counter - 1; +} + +void Timer::Skip(u64 ticks) { + if (pause || count_mode == CountMode::EventCount) + return; + + if (counter == 0) { + u32 reset; + if (count_mode == CountMode::AutoRestart) { + reset = ((u32)start_high << 16) | start_low; + } else if (count_mode == CountMode::FreeRunning) { + reset = 0xFFFFFFFF; + } else { + return; + } + ASSERT(reset >= ticks); + counter = reset - ((u32)ticks - 1); + } else { + ASSERT(counter > ticks); + counter -= (u32)ticks; + } + + UpdateMMIO(); +} + +Timer::Timer(CoreTiming& core_timing) { + core_timing.RegisterCallbacks(this); +} + +} // namespace Teakra diff --git a/src/teakra/src/timer.h b/src/teakra/src/timer.h new file mode 100644 index 0000000..8329eac --- /dev/null +++ b/src/teakra/src/timer.h @@ -0,0 +1,52 @@ +#pragma once + +#include <functional> +#include <utility> +#include "common_types.h" +#include "core_timing.h" + +namespace Teakra { + +class Timer : public CoreTiming::Callbacks { +public: + Timer(CoreTiming& core_timing); + + enum class CountMode : u16 { + Single = 0, + AutoRestart = 1, + FreeRunning = 2, + EventCount = 3, + }; + + void Reset(); + + void Restart(); + void Tick() override; + void TickEvent(); + u64 GetMaxSkip() const override; + void Skip(u64 ticks) override; + + u16 update_mmio = 0; + u16 pause = 0; + CountMode count_mode = CountMode::Single; + u16 scale = 0; + + u16 start_high = 0; + u16 start_low = 0; + u32 counter = 0; + u16 counter_high = 0; + u16 counter_low = 0; + + void SetInterruptHandler(std::function<void()> handler) { + interrupt_handler = std::move(handler); + } + +private: + std::function<void()> interrupt_handler; + + void UpdateMMIO(); + + class TimerTimingCallbacks; +}; + +} // namespace Teakra diff --git a/src/teakra/src/timer.md b/src/teakra/src/timer.md new file mode 100644 index 0000000..49e7da6 --- /dev/null +++ b/src/teakra/src/timer.md @@ -0,0 +1,66 @@ +# Timer + +## MMIO Layout + +The following MMIO definition is extracted from Lauterbach's Teak debugger. Some of them are untested. +``` +Timer 0 ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0020 | TM | GP| CS| BP|RES| MU| PC| CT| TP| | CM | TS | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0022 | | EW| ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0024 | START_COUNT_L | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0026 | START_COUNT_H | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x0028 | COUNTER_L | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x002A | COUNTER_H | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x002C | PWM_COUNTER_L | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +|+0x002E | PWM_COUNTER_H | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# + +TS: time scale + - 0: /1 + - 1: /2 + - 2: /4 + - 3: /16 +CM: count mode + - 0: single count + - 1: auto restart + - 2: free running + - 3: event count + - 4: watchdog mode 1 + - 5: watchdog mode 2 + - 6: watchdog mode 3 +TP: output signal polarity +CT: 1 to clear output signal +PC: 1 to pause the counter +MU: 1 to enable COUNTER_L/_H register update +RES: 1 to restart the counter +BP: 1 to enable breakpoint requests +CS: clock source + - 0: internal clock + - 1: external clock +GP: ? +TM: output signal clearing method + - 0: by setting CT manually + - 1: after two cycles + - 2: after four cycles + - 3: after eight cycles + +EW: 1 to decrement counter in event count mode / to reload watchdog in watch dog mode +START_COUNT_L, START_COUNT_H: the restart value for counter. Loaded to the counter on restarting +COUNTER_L, COUNTER_H: the value of the counter +PWM_COUNTER_L, PWM_COUNTER_H: the restart value for PWM counter. Loaded to the PWM counter on restarting + +Timer 1 ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +| +0x0030 | Same layout as Timer 0 | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +| ... | ++-----------#---+---+---+---#---+---+---+---#---+---+---+---#---+---+---+---# +``` |