Using exceptions

This commit is contained in:
Sergio Martin 2024-02-06 18:38:15 +01:00
parent 393e51ebcb
commit bd737267ae
1 changed files with 86 additions and 106 deletions

View File

@ -15,6 +15,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
// Emu 0.7.0 // Emu 0.7.0
#include <stdexcept>
#include "cpu.hpp" #include "cpu.hpp"
#include "apu/apu.hpp" #include "apu/apu.hpp"
#include "mappers/mapper.hpp" #include "mappers/mapper.hpp"
@ -172,23 +173,11 @@ class Core : private Cpu
const size_t inputDataSize, const size_t inputDataSize,
uint8_t* __restrict__ outputData, uint8_t* __restrict__ outputData,
size_t* outputDataPos, size_t* outputDataPos,
const bool useDifferentialCompression = false,
const uint8_t* __restrict__ referenceData = nullptr, const uint8_t* __restrict__ referenceData = nullptr,
size_t* referenceDataPos = 0, size_t* referenceDataPos = 0,
const size_t outputMaxSize = 0, const size_t outputMaxSize = 0,
const bool useZlib = false const bool useZlib = false
) )
{
// If not using differential compression, process the entire input block
if (useDifferentialCompression == false)
{
// Only perform memcpy if the input block is not null
if (outputData != nullptr) memcpy(&outputData[*outputDataPos], inputData, inputDataSize);
*outputDataPos += inputDataSize;
}
// If using differential compression
if (useDifferentialCompression == true)
{ {
// Only perform compression if input is not null // Only perform compression if input is not null
if (outputData != nullptr) if (outputData != nullptr)
@ -203,7 +192,7 @@ class Core : private Cpu
if (*outputDataPos >= outputMaxSize) if (*outputDataPos >= outputMaxSize)
{ {
fprintf(stderr, "[Error] Maximum output data position reached before differential encode (%lu)\n", outputMaxSize); fprintf(stderr, "[Error] Maximum output data position reached before differential encode (%lu)\n", outputMaxSize);
exit(-1); std::runtime_error("Error while serializing");
} }
// Encoding differential // Encoding differential
@ -222,7 +211,7 @@ class Core : private Cpu
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "[Error] unexpected error while encoding differential compression. Diff count: %u\n", *diffCount); fprintf(stderr, "[Error] unexpected error while encoding differential compression. Diff count: %u\n", *diffCount);
exit(-1); std::runtime_error("Error while serializing");
} }
// Increasing output data position pointer // Increasing output data position pointer
@ -232,8 +221,7 @@ class Core : private Cpu
if ((usize_t)*outputDataPos > outputMaxSize) if ((usize_t)*outputDataPos > outputMaxSize)
{ {
fprintf(stderr, "[Error] Differential compression size (%u) exceeded output maximum size (%lu).\n", *diffCount, outputMaxSize); fprintf(stderr, "[Error] Differential compression size (%u) exceeded output maximum size (%lu).\n", *diffCount, outputMaxSize);
exit(-1); std::runtime_error("Error while serializing");
}
} }
} }
@ -246,22 +234,10 @@ class Core : private Cpu
const size_t outputDataSize, const size_t outputDataSize,
const uint8_t* __restrict__ inputData, const uint8_t* __restrict__ inputData,
size_t* inputDataPos, size_t* inputDataPos,
const bool useDifferentialCompression = false,
const uint8_t* __restrict__ referenceData = nullptr, const uint8_t* __restrict__ referenceData = nullptr,
size_t* referenceDataPos = 0, size_t* referenceDataPos = 0,
const bool useZlib = false const bool useZlib = false
) )
{
// If not using differential compression, process the entire input block
if (useDifferentialCompression == false)
{
// Only perform memcpy if the input block is not null
if (outputData != nullptr) memcpy(outputData, &inputData[*inputDataPos], outputDataSize);
*inputDataPos += outputDataSize;
}
// If using differential compression
if (useDifferentialCompression == true)
{ {
// Reading differential count // Reading differential count
usize_t diffCount; usize_t diffCount;
@ -287,12 +263,11 @@ class Core : private Cpu
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "[Error] unexpected error while decoding differential compression. Diff count: %u\n", diffCount); fprintf(stderr, "[Error] unexpected error while decoding differential compression. Diff count: %u\n", diffCount);
exit(-1); std::runtime_error("Error while deserializing");
} }
// Increasing output data position pointer // Increasing output data position pointer
*inputDataPos += diffCount; *inputDataPos += diffCount;
}
// Finally, increasing reference data position pointer // Finally, increasing reference data position pointer
*referenceDataPos += outputDataSize; *referenceDataPos += outputDataSize;
@ -383,8 +358,8 @@ class Core : private Cpu
outputDataPos += headerSize; referenceDataPos += headerSize; outputDataPos += headerSize; referenceDataPos += headerSize;
} }
if (useDifferentialCompression == true) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos); if (useDifferentialCompression == false) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos);
if (useDifferentialCompression == false) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, outputMaxSize, useZlib); if (useDifferentialCompression == true) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, referenceData, &referenceDataPos, outputMaxSize, useZlib);
} }
if (APURBlockEnabled == true) if (APURBlockEnabled == true)
@ -456,8 +431,8 @@ class Core : private Cpu
outputDataPos += headerSize; referenceDataPos += headerSize; outputDataPos += headerSize; referenceDataPos += headerSize;
} }
if (useDifferentialCompression == true) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos); if (useDifferentialCompression == false) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos);
if (useDifferentialCompression == false) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, outputMaxSize, useZlib); if (useDifferentialCompression == true) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, referenceData, &referenceDataPos, outputMaxSize, useZlib);
} }
if (SPRTBlockEnabled == true) if (SPRTBlockEnabled == true)
@ -474,14 +449,15 @@ class Core : private Cpu
outputDataPos += headerSize; referenceDataPos += headerSize; outputDataPos += headerSize; referenceDataPos += headerSize;
} }
if (useDifferentialCompression == true) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos); if (useDifferentialCompression == false) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos);
if (useDifferentialCompression == false) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, outputMaxSize, useZlib); if (useDifferentialCompression == true) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, referenceData, &referenceDataPos, outputMaxSize, useZlib);
} }
if (NTABBlockEnabled == true) if (NTABBlockEnabled == true)
{ {
size_t nametable_size = 0x800; size_t nametable_size = 0x800;
if (ppu.nt_banks[3] >= &ppu.impl->nt_ram[0xC00]) nametable_size = 0x1000; if (ppu.nt_banks[3] >= &ppu.impl->nt_ram[0xC00]) nametable_size = 0x1000;
blockSize = nametable_size; blockSize = nametable_size;
dataSource = (void *)ppu.impl->nt_ram; dataSource = (void *)ppu.impl->nt_ram;
@ -494,8 +470,8 @@ class Core : private Cpu
outputDataPos += headerSize; referenceDataPos += headerSize; outputDataPos += headerSize; referenceDataPos += headerSize;
} }
if (useDifferentialCompression == true) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos); if (useDifferentialCompression == false) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos);
if (useDifferentialCompression == false) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, outputMaxSize, useZlib); if (useDifferentialCompression == true) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, referenceData, &referenceDataPos, outputMaxSize, useZlib);
} }
if (CHRRBlockEnabled == true) if (CHRRBlockEnabled == true)
@ -514,8 +490,8 @@ class Core : private Cpu
outputDataPos += headerSize; referenceDataPos += headerSize; outputDataPos += headerSize; referenceDataPos += headerSize;
} }
if (useDifferentialCompression == true) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos); if (useDifferentialCompression == false) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos);
if (useDifferentialCompression == false) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, outputMaxSize, useZlib); if (useDifferentialCompression == true) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, referenceData, &referenceDataPos, outputMaxSize, useZlib);
} }
} }
@ -535,8 +511,8 @@ class Core : private Cpu
outputDataPos += headerSize; referenceDataPos += headerSize; outputDataPos += headerSize; referenceDataPos += headerSize;
} }
if (useDifferentialCompression == true) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos); if (useDifferentialCompression == false) serializeContiguousData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos);
if (useDifferentialCompression == false) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, outputMaxSize, useZlib); if (useDifferentialCompression == true) serializeDifferentialData((uint8_t*)dataSource, blockSize, outputData, &outputDataPos, referenceData, &referenceDataPos, outputMaxSize, useZlib);
} }
} }
@ -604,7 +580,7 @@ class Core : private Cpu
if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; } if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; }
if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos); if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos);
if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, useZlib); if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, referenceData, &referenceDataPos, useZlib);
} }
// APUR Block // APUR Block
@ -634,11 +610,15 @@ class Core : private Cpu
if (MAPRBlockEnabled == true) if (MAPRBlockEnabled == true)
{ {
mapper->default_reset_state(); mapper->default_reset_state();
auto outputData = (uint8_t *)&mapper->state; auto outputData = (uint8_t *)&mapper->state;
blockSize = mapper->state_size; blockSize = mapper->state_size;
if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; } if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; }
memcpy(outputData, &inputStateData[inputDataPos], blockSize); memcpy(outputData, &inputStateData[inputDataPos], blockSize);
inputDataPos += blockSize; inputDataPos += blockSize;
mapper->apply_mapping(); mapper->apply_mapping();
} }
@ -650,7 +630,7 @@ class Core : private Cpu
if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; } if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; }
if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos); if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos);
if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, useZlib); if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, referenceData, &referenceDataPos, useZlib);
} }
// SPRT Block // SPRT Block
@ -661,7 +641,7 @@ class Core : private Cpu
if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; } if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; }
if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos); if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos);
if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, useZlib); if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, referenceData, &referenceDataPos, useZlib);
} }
// NTAB Block // NTAB Block
@ -674,7 +654,7 @@ class Core : private Cpu
if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; } if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; }
if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos); if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos);
if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, useZlib); if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, referenceData, &referenceDataPos, useZlib);
} }
// CHRR Block // CHRR Block
@ -687,7 +667,7 @@ class Core : private Cpu
if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; } if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; }
if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos); if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos);
if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, useZlib); if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, referenceData, &referenceDataPos, useZlib);
ppu.all_tiles_modified(); ppu.all_tiles_modified();
} }
@ -703,7 +683,7 @@ class Core : private Cpu
if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; } if (HEADBlockEnabled == true) { inputDataPos += 2 * headerSize; referenceDataPos += 2 * headerSize; }
if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos); if (useDifferentialCompression == false) deserializeContiguousData(outputData, blockSize, inputStateData, &inputDataPos);
if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, useDifferentialCompression, referenceData, &referenceDataPos, useZlib); if (useDifferentialCompression == true) deserializeDifferentialData(outputData, blockSize, inputStateData, &inputDataPos, referenceData, &referenceDataPos, useZlib);
} }
} }