1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-06-01 22:41:32 +00:00

Merge pull request #1320 from TomHarte/MoreTemplateElimination

Apply more macro elimination.
This commit is contained in:
Thomas Harte 2024-01-22 21:46:19 -05:00 committed by GitHub
commit f5800aa004
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 58 additions and 55 deletions

View File

@ -19,8 +19,10 @@ static std::unique_ptr<File::Chunk> GetNextChunk(const std::shared_ptr<Storage::
auto new_chunk = std::make_unique<File::Chunk>();
int shift_register = 0;
// TODO: move this into the parser
#define shift() shift_register = (shift_register >> 1) | (parser.get_next_bit(tape) << 9)
// TODO: move this into the parser
const auto shift = [&] {
shift_register = (shift_register >> 1) | (parser.get_next_bit(tape) << 9);
};
// find next area of high tone
while(!tape->is_at_end() && (shift_register != 0x3ff)) {
@ -32,8 +34,6 @@ static std::unique_ptr<File::Chunk> GetNextChunk(const std::shared_ptr<Storage::
shift();
}
#undef shift
parser.reset_crc();
parser.reset_error_flag();

View File

@ -38,12 +38,15 @@ struct Media {
}
Media &operator +=(const Media &rhs) {
#define append(name) name.insert(name.end(), rhs.name.begin(), rhs.name.end());
append(disks);
append(tapes);
append(cartridges);
append(mass_storage_devices);
#undef append
const auto append = [&](auto &destination, auto &source) {
destination.insert(destination.end(), source.begin(), source.end());
};
append(disks, rhs.disks);
append(tapes, rhs.tapes);
append(cartridges, rhs.cartridges);
append(mass_storage_devices, rhs.mass_storage_devices);
return *this;
}
};

View File

@ -15,6 +15,12 @@
using namespace Storage::Disk;
namespace {
constexpr uint32_t chunk(const char *str) {
return uint32_t(str[0] | (str[1] << 8) | (str[2] << 16) | (str[3] << 24));
}
}
WOZ::WOZ(const std::string &file_name) :
file_(file_name) {
@ -58,9 +64,8 @@ WOZ::WOZ(const std::string &file_name) :
long end_of_chunk = file_.tell() + long(chunk_size);
#define CK(str) (str[0] | (str[1] << 8) | (str[2] << 16) | (str[3] << 24))
switch(chunk_id) {
case CK("INFO"): {
case chunk("INFO"): {
const uint8_t version = file_.get8();
if(version > 2) break;
is_3_5_disk_ = file_.get8() == 2;
@ -81,12 +86,12 @@ WOZ::WOZ(const std::string &file_name) :
*/
} break;
case CK("TMAP"): {
case chunk("TMAP"): {
file_.read(track_map_, 160);
has_tmap = true;
} break;
case CK("TRKS"): {
case chunk("TRKS"): {
tracks_offset_ = file_.tell();
} break;
@ -95,7 +100,6 @@ WOZ::WOZ(const std::string &file_name) :
default:
break;
}
#undef CK
file_.seek(end_of_chunk, SEEK_SET);
}

View File

@ -74,16 +74,14 @@ Storage::Disk::PCMSegment AppleGCR::AppleII::header(uint8_t volume, uint8_t trac
data[1] = header_prologue[1];
data[2] = header_prologue[2];
#define WriteFM(index, value) \
data[index+0] = uint8_t(((value) >> 1) | 0xaa); \
data[index+1] = uint8_t((value) | 0xaa); \
WriteFM(3, volume);
WriteFM(5, track);
WriteFM(7, sector);
WriteFM(9, checksum);
#undef WriteFM
const auto write_fm = [&](std::size_t index, uint8_t value) {
data[index+0] = uint8_t(((value) >> 1) | 0xaa);
data[index+1] = uint8_t((value) | 0xaa);
};
write_fm(3, volume);
write_fm(5, track);
write_fm(7, sector);
write_fm(9, checksum);
data[11] = epilogue[0];
data[12] = epilogue[1];

View File

@ -205,9 +205,11 @@ std::unique_ptr<Sector> decode_appleii_sector(const std::array<uint_fast8_t, 8>
} else {
// Undo the 6 and 2 mapping.
constexpr uint8_t bit_reverse[] = {0, 2, 1, 3};
#define unmap(byte, nibble, shift) \
sector->data[86 + byte] = uint8_t(\
(sector->data[86 + byte] << 2) | bit_reverse[(sector->data[nibble] >> shift)&3]);
const auto unmap = [&](std::size_t byte, std::size_t nibble, int shift) {
sector->data[86 + byte] = uint8_t(
(sector->data[86 + byte] << 2) | bit_reverse[(sector->data[nibble] >> shift)&3]
);
};
for(std::size_t c = 0; c < 84; ++c) {
unmap(c, c, 0);
@ -220,8 +222,6 @@ std::unique_ptr<Sector> decode_appleii_sector(const std::array<uint_fast8_t, 8>
unmap(85, 85, 0);
unmap(171, 85, 2);
#undef unmap
// Throw away the collection of two-bit chunks from the start of the sector.
sector->data.erase(sector->data.begin(), sector->data.end() - 256);

View File

@ -170,14 +170,16 @@ template <typename Executor> void Target<Executor>::begin_command(uint8_t first_
}
}
namespace {
constexpr uint8_t G0(uint8_t opcode) { return 0x00 | opcode; }
constexpr uint8_t G1(uint8_t opcode) { return 0x20 | opcode; }
constexpr uint8_t G5(uint8_t opcode) { return 0xa0 | opcode; }
}
template <typename Executor> bool Target<Executor>::dispatch_command() {
CommandState arguments(command_, data_);
#define G0(x) x
#define G1(x) (0x20|x)
#define G5(x) (0xa0|x)
log_.info().append("---Command %02x---", command_[0]);
switch(command_[0]) {
@ -209,14 +211,9 @@ template <typename Executor> bool Target<Executor>::dispatch_command() {
case G1(0x1c): return executor_.read_buffer(arguments, *this);
case G1(0x15): return executor_.mode_select(arguments, *this);
case G5(0x09): return executor_.set_block_limits(arguments, *this);
}
#undef G0
#undef G1
#undef G5
return false;
}

View File

@ -88,22 +88,23 @@ void Parser::inspect_waves(const std::vector<WaveType> &waves) {
}
break;
case SlowData:
#define CHECK_RUN(length, type, symbol) \
if(waves.size() >= length) {\
std::size_t c;\
for(c = 0; c < length; c++) if(waves[c] != type) break;\
if(c == length) {\
push_symbol(symbol, length);\
return;\
}\
}
case SlowData: {
const auto check_run = [&](std::size_t length, WaveType type, SymbolType symbol) -> bool {
if(waves.size() >= length) {
std::size_t c;
for(c = 0; c < length; c++) if(waves[c] != type) break;
if(c == length) {
push_symbol(symbol, int(length));
return true;
}
}
return false;
};
CHECK_RUN(4, WaveType::Long, SymbolType::Zero);
CHECK_RUN(8, WaveType::Short, SymbolType::One);
#undef CHECK_RUN
if(check_run(4, WaveType::Long, SymbolType::Zero)) return;
if(check_run(8, WaveType::Short, SymbolType::One)) return;
if(waves.size() < 16) return; // TODO, maybe: if there are any inconsistencies in the first 8, don't return
break;
} break;
case Sync: {
// Sync is 0x16, either encoded fast or slow; i.e. 0 0110 1000 1