2020-10-03 15:22:44 +00:00
|
|
|
#include "global.h"
|
2020-03-17 04:31:30 +00:00
|
|
|
|
|
|
|
u8 sYaz0DataBuffer[0x400];
|
2022-06-11 07:42:05 +00:00
|
|
|
u8* sYaz0DataBufferEnd;
|
2022-07-12 16:47:25 +00:00
|
|
|
uintptr_t sYaz0CurRomStart;
|
2022-11-17 02:57:02 +00:00
|
|
|
size_t sYaz0CurSize;
|
2022-06-11 07:42:05 +00:00
|
|
|
u8* sYaz0MaxPtr;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
void* Yaz0_FirstDMA(void) {
|
2022-06-11 07:42:05 +00:00
|
|
|
s32 pad[2];
|
2022-11-17 02:57:02 +00:00
|
|
|
size_t dmaSize;
|
|
|
|
size_t bufferSize;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
2022-06-11 07:42:05 +00:00
|
|
|
sYaz0MaxPtr = sYaz0DataBufferEnd - 0x19;
|
2020-03-22 21:19:43 +00:00
|
|
|
|
2022-06-11 07:42:05 +00:00
|
|
|
bufferSize = sYaz0DataBufferEnd - sYaz0DataBuffer;
|
|
|
|
dmaSize = (bufferSize > sYaz0CurSize) ? sYaz0CurSize : bufferSize;
|
2020-03-22 21:19:43 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
DmaMgr_DmaRomToRam(sYaz0CurRomStart, sYaz0DataBuffer, dmaSize);
|
2020-03-17 04:31:30 +00:00
|
|
|
sYaz0CurRomStart += dmaSize;
|
|
|
|
sYaz0CurSize -= dmaSize;
|
|
|
|
return sYaz0DataBuffer;
|
|
|
|
}
|
|
|
|
|
2022-06-11 07:42:05 +00:00
|
|
|
void* Yaz0_NextDMA(u8* curSrcPos) {
|
2020-03-17 04:31:30 +00:00
|
|
|
u8* dst;
|
2022-11-17 02:57:02 +00:00
|
|
|
size_t restSize;
|
|
|
|
size_t dmaSize;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
2022-06-11 07:42:05 +00:00
|
|
|
restSize = sYaz0DataBufferEnd - curSrcPos;
|
2020-03-22 21:19:43 +00:00
|
|
|
dst = (restSize & 7) ? (sYaz0DataBuffer - (restSize & 7)) + 8 : sYaz0DataBuffer;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
|
|
|
bcopy(curSrcPos, dst, restSize);
|
2022-06-11 07:42:05 +00:00
|
|
|
dmaSize = (sYaz0DataBufferEnd - dst) - restSize;
|
2020-03-22 21:19:43 +00:00
|
|
|
if (sYaz0CurSize < dmaSize) {
|
2020-03-17 04:31:30 +00:00
|
|
|
dmaSize = sYaz0CurSize;
|
2020-03-22 21:19:43 +00:00
|
|
|
}
|
2020-03-17 04:31:30 +00:00
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
if (dmaSize != 0) {
|
2022-06-11 07:42:05 +00:00
|
|
|
DmaMgr_DmaRomToRam(sYaz0CurRomStart, dst + restSize, dmaSize);
|
2020-03-17 04:31:30 +00:00
|
|
|
sYaz0CurRomStart += dmaSize;
|
|
|
|
sYaz0CurSize -= dmaSize;
|
2022-06-11 07:42:05 +00:00
|
|
|
if (sYaz0CurSize == 0) {
|
|
|
|
sYaz0MaxPtr = dst + restSize + dmaSize;
|
2020-03-22 21:19:43 +00:00
|
|
|
}
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2024-01-29 23:14:30 +00:00
|
|
|
void Yaz0_DecompressImpl(u8* src, u8* dst) {
|
|
|
|
Yaz0Header* header = (Yaz0Header*)src;
|
2021-02-14 00:49:40 +00:00
|
|
|
u32 bitIdx = 0;
|
2024-01-29 23:14:30 +00:00
|
|
|
u8* dstEnd = dst + header->decSize;
|
2020-03-17 04:31:30 +00:00
|
|
|
u32 chunkHeader;
|
|
|
|
u32 nibble;
|
|
|
|
u8* backPtr;
|
2021-02-14 00:49:40 +00:00
|
|
|
u32 chunkSize;
|
2020-03-17 04:31:30 +00:00
|
|
|
u32 off;
|
|
|
|
|
2024-01-29 23:14:30 +00:00
|
|
|
src += sizeof(Yaz0Header);
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
do {
|
|
|
|
if (bitIdx == 0) {
|
2022-06-11 07:42:05 +00:00
|
|
|
if ((sYaz0MaxPtr < src) && (sYaz0CurSize != 0)) {
|
2020-03-17 04:31:30 +00:00
|
|
|
src = Yaz0_NextDMA(src);
|
2020-03-23 10:35:43 +00:00
|
|
|
}
|
2020-03-17 04:31:30 +00:00
|
|
|
|
|
|
|
chunkHeader = *src++;
|
|
|
|
bitIdx = 8;
|
|
|
|
}
|
|
|
|
|
2020-03-23 10:35:43 +00:00
|
|
|
if (chunkHeader & (1 << 7)) { // uncompressed
|
|
|
|
*dst = *src;
|
|
|
|
dst++;
|
|
|
|
src++;
|
2020-03-22 21:19:43 +00:00
|
|
|
} else { // compressed
|
2020-03-23 10:35:43 +00:00
|
|
|
off = ((*src & 0xF) << 8 | *(src + 1));
|
2020-03-17 04:31:30 +00:00
|
|
|
nibble = *src >> 4;
|
2020-03-23 10:35:43 +00:00
|
|
|
backPtr = dst - off;
|
2020-03-17 04:31:30 +00:00
|
|
|
src += 2;
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
chunkSize = (nibble == 0) // N = chunkSize; B = back offset
|
|
|
|
? (u32)(*src++ + 0x12) // 3 bytes 0B BB NN
|
|
|
|
: nibble + 2; // 2 bytes NB BB
|
2020-03-17 04:31:30 +00:00
|
|
|
|
2020-03-23 10:35:43 +00:00
|
|
|
do {
|
2020-03-17 04:31:30 +00:00
|
|
|
*dst++ = *(backPtr++ - 1);
|
2020-03-23 10:35:43 +00:00
|
|
|
chunkSize--;
|
|
|
|
} while (chunkSize != 0);
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
2020-03-23 10:35:43 +00:00
|
|
|
chunkHeader <<= 1;
|
2020-03-17 04:31:30 +00:00
|
|
|
bitIdx--;
|
|
|
|
} while (dst != dstEnd);
|
|
|
|
}
|
|
|
|
|
2022-11-17 02:57:02 +00:00
|
|
|
void Yaz0_Decompress(uintptr_t romStart, u8* dst, size_t size) {
|
2020-03-17 04:31:30 +00:00
|
|
|
sYaz0CurRomStart = romStart;
|
|
|
|
sYaz0CurSize = size;
|
2022-06-11 07:42:05 +00:00
|
|
|
sYaz0DataBufferEnd = sYaz0DataBuffer + sizeof(sYaz0DataBuffer);
|
2020-03-17 04:31:30 +00:00
|
|
|
Yaz0_DecompressImpl(Yaz0_FirstDMA(), dst);
|
|
|
|
}
|