| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#ifndef TSXPARSER_HH |
| 6 |
#define TSXPARSER_HH |
| 7 |
|
| 8 |
#include "endian.hh" |
| 9 |
|
| 10 |
#include <array> |
| 11 |
#include <cstdint> |
| 12 |
#include <optional> |
| 13 |
#include <span> |
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
class TsxParser { |
| 18 |
public: |
| 19 |
|
| 20 |
static constexpr unsigned TZX_Z80_FREQ = 3500000; |
| 21 |
static constexpr unsigned OUTPUT_FREQUENCY = 58900; |
| 22 |
static constexpr float T_STATES_MSX_PULSE = 238.f; |
| 23 |
|
| 24 |
enum class FileType : uint8_t { |
| 25 |
ASCII, BINARY, BASIC, UNKNOWN, |
| 26 |
}; |
| 27 |
|
| 28 |
public: |
| 29 |
explicit TsxParser(std::span<const uint8_t> file); |
| 30 |
|
| 31 |
[[nodiscard]] std::vector<int8_t>&& stealOutput() { return std::move(output); } |
| 32 |
[[nodiscard]] std::optional<FileType> getFirstFileType() const { return firstFileType; } |
| 33 |
[[nodiscard]] const std::vector<std::string>& getMessages() const { return messages; } |
| 34 |
|
| 35 |
private: |
| 36 |
struct Block10 { |
| 37 |
Endian::UA_L16 pauseMs; |
| 38 |
Endian::UA_L16 len; |
| 39 |
|
| 40 |
}; |
| 41 |
struct Block11 { |
| 42 |
Endian::UA_L16 pilot; |
| 43 |
Endian::UA_L16 sync1; |
| 44 |
Endian::UA_L16 sync2; |
| 45 |
Endian::UA_L16 zero; |
| 46 |
Endian::UA_L16 one; |
| 47 |
Endian::UA_L16 pilotLen; |
| 48 |
uint8_t lastBits; |
| 49 |
Endian::UA_L16 pauseMs; |
| 50 |
Endian::UA_L24 len; |
| 51 |
|
| 52 |
}; |
| 53 |
struct Block12 { |
| 54 |
Endian::UA_L16 len; |
| 55 |
Endian::UA_L16 pulses; |
| 56 |
}; |
| 57 |
struct Block13 { |
| 58 |
uint8_t num; |
| 59 |
|
| 60 |
}; |
| 61 |
struct Block15 { |
| 62 |
Endian::UA_L16 bitTstates; |
| 63 |
Endian::UA_L16 pauseMs; |
| 64 |
uint8_t lastBits; |
| 65 |
Endian::UA_L24 len; |
| 66 |
|
| 67 |
}; |
| 68 |
struct Block20 { |
| 69 |
Endian::UA_L16 pauseMs; |
| 70 |
}; |
| 71 |
struct Block21 { |
| 72 |
uint8_t len; |
| 73 |
|
| 74 |
}; |
| 75 |
struct Block30 { |
| 76 |
uint8_t len; |
| 77 |
|
| 78 |
}; |
| 79 |
struct Block32 { |
| 80 |
Endian::UA_L16 blockLen; |
| 81 |
uint8_t num; |
| 82 |
|
| 83 |
}; |
| 84 |
struct Block35 { |
| 85 |
std::array<char, 0x10> label; |
| 86 |
Endian::UA_L32 len; |
| 87 |
|
| 88 |
}; |
| 89 |
struct Block4B { |
| 90 |
Endian::UA_L32 blockLen; |
| 91 |
Endian::UA_L16 pauseMs; |
| 92 |
Endian::UA_L16 pilot; |
| 93 |
Endian::UA_L16 pulses; |
| 94 |
Endian::UA_L16 bit0len; |
| 95 |
Endian::UA_L16 bit1len; |
| 96 |
uint8_t bitCfg; |
| 97 |
uint8_t byteCfg; |
| 98 |
|
| 99 |
}; |
| 100 |
|
| 101 |
void processBlock10(const Block10& b); |
| 102 |
void processBlock11(const Block11& b); |
| 103 |
void processBlock12(const Block12& b); |
| 104 |
void processBlock13(const Block13& b); |
| 105 |
void processBlock15(const Block15& b); |
| 106 |
void processBlock20(const Block20& b); |
| 107 |
void processBlock21(const Block21& b); |
| 108 |
void processBlock30(const Block30& b); |
| 109 |
void processBlock32(const Block32& b); |
| 110 |
void processBlock35(const Block35& b); |
| 111 |
void processBlock4B(const Block4B& b); |
| 112 |
|
| 113 |
void writeSample(uint32_t tStates, int8_t value); |
| 114 |
void writePulse(uint32_t tStates); |
| 115 |
void writePulses(uint32_t count, uint32_t tStates); |
| 116 |
void writeSilence(int s); |
| 117 |
|
| 118 |
template<typename T> std::span<const T> get(size_t count); |
| 119 |
template<typename T> const T& get(); |
| 120 |
|
| 121 |
[[noreturn]] void error(std::string msg) const; |
| 122 |
|
| 123 |
private: |
| 124 |
|
| 125 |
std::vector<int8_t> output; |
| 126 |
std::vector<std::string> messages; |
| 127 |
std::optional<FileType> firstFileType; |
| 128 |
|
| 129 |
|
| 130 |
std::span<const uint8_t> buf; |
| 131 |
|
| 132 |
|
| 133 |
float accumBytes = 0.f; |
| 134 |
int8_t currentValue = 127; |
| 135 |
}; |
| 136 |
|
| 137 |
#endif |