2017-07-17 01:33:11 +00:00
|
|
|
//
|
|
|
|
// TZX.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 16/07/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-07-17 01:33:11 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "TZX.hpp"
|
|
|
|
|
2018-02-23 02:28:12 +00:00
|
|
|
#include "CSW.hpp"
|
2019-03-02 23:07:05 +00:00
|
|
|
#include "../../../Outputs/Log.hpp"
|
2018-02-23 02:28:12 +00:00
|
|
|
|
2017-07-17 01:33:11 +00:00
|
|
|
using namespace Storage::Tape;
|
|
|
|
|
2017-07-17 02:40:38 +00:00
|
|
|
namespace {
|
|
|
|
const unsigned int StandardTZXClock = 3500000;
|
2017-07-21 22:21:12 +00:00
|
|
|
const unsigned int TZXClockMSMultiplier = 3500;
|
2017-07-17 02:40:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 21:42:24 +00:00
|
|
|
TZX::TZX(const std::string &file_name) :
|
2017-11-03 02:32:00 +00:00
|
|
|
file_(file_name),
|
2017-07-21 22:21:12 +00:00
|
|
|
current_level_(false) {
|
2017-07-17 01:33:11 +00:00
|
|
|
|
|
|
|
// Check for signature followed by a 0x1a
|
2017-11-03 02:32:00 +00:00
|
|
|
if(!file_.check_signature("ZXTape!")) throw ErrorNotTZX;
|
|
|
|
if(file_.get8() != 0x1a) throw ErrorNotTZX;
|
2017-07-17 01:33:11 +00:00
|
|
|
|
|
|
|
// Get version number
|
2017-11-03 02:32:00 +00:00
|
|
|
uint8_t major_version = file_.get8();
|
|
|
|
uint8_t minor_version = file_.get8();
|
2017-07-17 01:33:11 +00:00
|
|
|
|
|
|
|
// Reject if an incompatible version
|
2017-12-20 03:17:42 +00:00
|
|
|
if(major_version != 1 || minor_version > 21) throw ErrorNotTZX;
|
2017-07-20 01:28:33 +00:00
|
|
|
|
|
|
|
virtual_reset();
|
2017-07-17 01:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::virtual_reset() {
|
2017-07-17 02:06:56 +00:00
|
|
|
clear();
|
2017-07-21 22:21:12 +00:00
|
|
|
set_is_at_end(false);
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.seek(0x0a, SEEK_SET);
|
2017-07-20 01:28:33 +00:00
|
|
|
|
|
|
|
// This is a workaround for arguably dodgy ZX80/ZX81 TZXs; they launch straight
|
|
|
|
// into data but both machines require a gap before data begins. So impose
|
|
|
|
// an initial gap, in the form of a very long wave.
|
2017-07-21 22:21:12 +00:00
|
|
|
current_level_ = false;
|
|
|
|
post_gap(500);
|
2017-07-17 01:33:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 02:06:56 +00:00
|
|
|
void TZX::get_next_pulses() {
|
2017-07-17 02:40:38 +00:00
|
|
|
while(empty()) {
|
2017-11-03 02:32:00 +00:00
|
|
|
uint8_t chunk_id = file_.get8();
|
|
|
|
if(file_.eof()) {
|
2017-07-17 02:40:38 +00:00
|
|
|
set_is_at_end(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(chunk_id) {
|
2017-07-17 23:38:15 +00:00
|
|
|
case 0x10: get_standard_speed_data_block(); break;
|
|
|
|
case 0x11: get_turbo_speed_data_block(); break;
|
|
|
|
case 0x12: get_pure_tone_data_block(); break;
|
|
|
|
case 0x13: get_pulse_sequence(); break;
|
2017-08-03 16:13:41 +00:00
|
|
|
case 0x14: get_pure_data_block(); break;
|
2018-02-23 02:28:12 +00:00
|
|
|
case 0x15: get_direct_recording_block(); break;
|
|
|
|
case 0x18: get_csw_recording_block(); break;
|
2017-07-17 23:38:15 +00:00
|
|
|
case 0x19: get_generalised_data_block(); break;
|
2017-08-02 18:12:34 +00:00
|
|
|
case 0x20: get_pause(); break;
|
2017-07-17 02:40:38 +00:00
|
|
|
|
2017-08-03 16:13:41 +00:00
|
|
|
case 0x21: ignore_group_start(); break;
|
|
|
|
case 0x22: ignore_group_end(); break;
|
|
|
|
case 0x23: ignore_jump_to_block(); break;
|
|
|
|
case 0x24: ignore_loop_start(); break;
|
|
|
|
case 0x25: ignore_loop_end(); break;
|
|
|
|
case 0x26: ignore_call_sequence(); break;
|
|
|
|
case 0x27: ignore_return_from_sequence(); break;
|
|
|
|
case 0x28: ignore_select_block(); break;
|
2018-02-23 02:28:12 +00:00
|
|
|
case 0x2a: ignore_stop_tape_if_in_48kb_mode(); break;
|
|
|
|
|
|
|
|
case 0x2b: get_set_signal_level(); break;
|
2017-08-03 16:13:41 +00:00
|
|
|
|
|
|
|
case 0x30: ignore_text_description(); break;
|
|
|
|
case 0x31: ignore_message_block(); break;
|
2018-02-23 02:28:12 +00:00
|
|
|
case 0x32: ignore_archive_info(); break;
|
2017-08-03 16:13:41 +00:00
|
|
|
case 0x33: get_hardware_type(); break;
|
2018-02-23 02:28:12 +00:00
|
|
|
case 0x35: ignore_custom_info_block(); break;
|
2017-07-17 02:40:38 +00:00
|
|
|
|
2017-12-20 02:53:04 +00:00
|
|
|
case 0x4b: get_kansas_city_block(); break;
|
|
|
|
|
2018-02-23 02:28:12 +00:00
|
|
|
case 0x5a: ignore_glue_block(); break;
|
|
|
|
|
2017-07-17 02:40:38 +00:00
|
|
|
default:
|
|
|
|
// In TZX each chunk has a different way of stating or implying its length,
|
|
|
|
// so there is no route past an unimplemented chunk.
|
2019-03-02 23:07:05 +00:00
|
|
|
LOG("Unknown TZX chunk: " << PADHEX(4) << chunk_id);
|
2017-07-17 02:40:38 +00:00
|
|
|
set_is_at_end(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 02:28:12 +00:00
|
|
|
void TZX::get_csw_recording_block() {
|
|
|
|
const uint32_t block_length = file_.get32le();
|
|
|
|
const uint16_t pause_after_block = file_.get16le();
|
|
|
|
const uint32_t sampling_rate = file_.get24le();
|
|
|
|
const uint8_t compression_type = file_.get8();
|
|
|
|
const uint32_t number_of_compressed_pulses = file_.get32le();
|
|
|
|
|
|
|
|
std::vector<uint8_t> raw_block = file_.read(block_length - 10);
|
|
|
|
|
|
|
|
CSW csw(std::move(raw_block), (compression_type == 2) ? CSW::CompressionType::ZRLE : CSW::CompressionType::RLE, current_level_, sampling_rate);
|
|
|
|
while(!csw.is_at_end()) {
|
|
|
|
Tape::Pulse next_pulse = csw.get_next_pulse();
|
|
|
|
current_level_ = (next_pulse.type == Tape::Pulse::High);
|
|
|
|
emplace_back(std::move(next_pulse));
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)number_of_compressed_pulses;
|
|
|
|
post_gap(pause_after_block);
|
|
|
|
}
|
|
|
|
|
2017-07-17 02:40:38 +00:00
|
|
|
void TZX::get_generalised_data_block() {
|
2017-11-03 02:32:00 +00:00
|
|
|
uint32_t block_length = file_.get32le();
|
2020-05-10 03:00:39 +00:00
|
|
|
long endpoint = file_.tell() + long(block_length);
|
2017-11-03 02:32:00 +00:00
|
|
|
uint16_t pause_after_block = file_.get16le();
|
2017-07-17 02:40:38 +00:00
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
uint32_t total_pilot_symbols = file_.get32le();
|
|
|
|
uint8_t maximum_pulses_per_pilot_symbol = file_.get8();
|
|
|
|
uint8_t symbols_in_pilot_table = file_.get8();
|
2017-07-17 02:40:38 +00:00
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
uint32_t total_data_symbols = file_.get32le();
|
|
|
|
uint8_t maximum_pulses_per_data_symbol = file_.get8();
|
|
|
|
uint8_t symbols_in_data_table = file_.get8();
|
2017-07-17 02:40:38 +00:00
|
|
|
|
2017-07-17 11:34:10 +00:00
|
|
|
get_generalised_segment(total_pilot_symbols, maximum_pulses_per_pilot_symbol, symbols_in_pilot_table, false);
|
|
|
|
get_generalised_segment(total_data_symbols, maximum_pulses_per_data_symbol, symbols_in_data_table, true);
|
2017-07-21 22:21:12 +00:00
|
|
|
post_gap(pause_after_block);
|
2017-07-17 11:35:53 +00:00
|
|
|
|
|
|
|
// This should be unnecessary, but intends to preserve sanity.
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.seek(endpoint, SEEK_SET);
|
2017-07-17 02:40:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 11:34:10 +00:00
|
|
|
void TZX::get_generalised_segment(uint32_t output_symbols, uint8_t max_pulses_per_symbol, uint8_t number_of_symbols, bool is_data) {
|
2017-07-17 02:40:38 +00:00
|
|
|
if(!output_symbols) return;
|
|
|
|
|
|
|
|
// Construct the symbol table.
|
|
|
|
struct Symbol {
|
|
|
|
uint8_t flags;
|
|
|
|
std::vector<uint16_t> pulse_lengths;
|
|
|
|
};
|
|
|
|
std::vector<Symbol> symbol_table;
|
|
|
|
for(int c = 0; c < number_of_symbols; c++) {
|
|
|
|
Symbol symbol;
|
2017-11-03 02:32:00 +00:00
|
|
|
symbol.flags = file_.get8();
|
2017-07-17 02:40:38 +00:00
|
|
|
for(int ic = 0; ic < max_pulses_per_symbol; ic++) {
|
2017-11-03 02:32:00 +00:00
|
|
|
symbol.pulse_lengths.push_back(file_.get16le());
|
2017-07-17 02:40:38 +00:00
|
|
|
}
|
2017-07-17 11:34:10 +00:00
|
|
|
symbol_table.push_back(symbol);
|
2017-07-17 02:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hence produce the output.
|
2017-11-03 02:32:00 +00:00
|
|
|
FileHolder::BitStream stream = file_.get_bitstream(false);
|
2017-07-17 11:34:10 +00:00
|
|
|
int base = 2;
|
|
|
|
int bits = 1;
|
|
|
|
while(base < number_of_symbols) {
|
|
|
|
base <<= 1;
|
|
|
|
bits++;
|
|
|
|
}
|
2017-11-11 20:28:40 +00:00
|
|
|
for(std::size_t c = 0; c < output_symbols; c++) {
|
2017-07-17 11:34:10 +00:00
|
|
|
uint8_t symbol_value;
|
|
|
|
int count;
|
|
|
|
if(is_data) {
|
|
|
|
symbol_value = stream.get_bits(bits);
|
|
|
|
count = 1;
|
|
|
|
} else {
|
2017-11-03 02:32:00 +00:00
|
|
|
symbol_value = file_.get8();
|
|
|
|
count = file_.get16le();
|
2017-07-17 02:40:38 +00:00
|
|
|
}
|
2017-07-17 11:34:10 +00:00
|
|
|
if(symbol_value > number_of_symbols) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Symbol &symbol = symbol_table[symbol_value];
|
2017-07-17 02:40:38 +00:00
|
|
|
|
2017-07-17 11:34:10 +00:00
|
|
|
while(count--) {
|
|
|
|
// Mutate initial output level.
|
|
|
|
switch(symbol.flags & 3) {
|
|
|
|
case 0: break;
|
2017-07-21 22:21:12 +00:00
|
|
|
case 1: current_level_ ^= true; break;
|
|
|
|
case 2: current_level_ = true; break;
|
|
|
|
case 3: current_level_ = false; break;
|
2017-07-17 11:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output waves.
|
2018-05-01 02:23:57 +00:00
|
|
|
for(const auto length : symbol.pulse_lengths) {
|
2017-07-17 11:34:10 +00:00
|
|
|
if(!length) break;
|
2017-07-17 23:38:15 +00:00
|
|
|
post_pulse(length);
|
2017-07-17 11:34:10 +00:00
|
|
|
}
|
2017-07-17 02:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-17 01:33:11 +00:00
|
|
|
}
|
2017-07-17 23:38:15 +00:00
|
|
|
|
|
|
|
void TZX::get_standard_speed_data_block() {
|
2017-08-02 18:36:47 +00:00
|
|
|
DataBlock data_block;
|
|
|
|
data_block.length_of_pilot_pulse = 2168;
|
|
|
|
data_block.length_of_sync_first_pulse = 667;
|
|
|
|
data_block.length_of_sync_second_pulse = 735;
|
2017-08-03 16:13:41 +00:00
|
|
|
data_block.data.length_of_zero_bit_pulse = 855;
|
|
|
|
data_block.data.length_of_one_bit_pulse = 1710;
|
|
|
|
data_block.data.number_of_bits_in_final_byte = 8;
|
2017-08-02 18:36:47 +00:00
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
data_block.data.pause_after_block = file_.get16le();
|
|
|
|
data_block.data.data_length = file_.get16le();
|
2017-08-03 16:13:41 +00:00
|
|
|
if(!data_block.data.data_length) return;
|
2017-07-17 23:38:15 +00:00
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
uint8_t first_byte = file_.get8();
|
2017-08-02 18:36:47 +00:00
|
|
|
data_block.length_of_pilot_tone = (first_byte < 128) ? 8063 : 3223;
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.seek(-1, SEEK_CUR);
|
2017-07-17 23:38:15 +00:00
|
|
|
|
2017-08-02 18:36:47 +00:00
|
|
|
get_data_block(data_block);
|
2017-07-17 23:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::get_turbo_speed_data_block() {
|
2017-08-02 18:36:47 +00:00
|
|
|
DataBlock data_block;
|
2017-11-03 02:32:00 +00:00
|
|
|
data_block.length_of_pilot_pulse = file_.get16le();
|
|
|
|
data_block.length_of_sync_first_pulse = file_.get16le();
|
|
|
|
data_block.length_of_sync_second_pulse = file_.get16le();
|
|
|
|
data_block.data.length_of_zero_bit_pulse = file_.get16le();
|
|
|
|
data_block.data.length_of_one_bit_pulse = file_.get16le();
|
|
|
|
data_block.length_of_pilot_tone = file_.get16le();
|
|
|
|
data_block.data.number_of_bits_in_final_byte = file_.get8();
|
|
|
|
data_block.data.pause_after_block = file_.get16le();
|
|
|
|
data_block.data.data_length = file_.get24le();
|
2017-08-02 18:36:47 +00:00
|
|
|
|
|
|
|
get_data_block(data_block);
|
|
|
|
}
|
2017-08-02 18:12:34 +00:00
|
|
|
|
2017-08-02 18:36:47 +00:00
|
|
|
void TZX::get_data_block(const DataBlock &data_block) {
|
2017-08-02 18:24:34 +00:00
|
|
|
// Output pilot tone.
|
2017-12-20 02:53:04 +00:00
|
|
|
post_pulses(data_block.length_of_pilot_tone, data_block.length_of_pilot_pulse);
|
2017-08-02 18:24:34 +00:00
|
|
|
|
|
|
|
// Output sync pulses.
|
2017-08-02 18:36:47 +00:00
|
|
|
post_pulse(data_block.length_of_sync_first_pulse);
|
|
|
|
post_pulse(data_block.length_of_sync_second_pulse);
|
2017-07-17 23:38:15 +00:00
|
|
|
|
2017-08-03 16:13:41 +00:00
|
|
|
get_data(data_block.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::get_data(const Data &data) {
|
2017-08-02 18:24:34 +00:00
|
|
|
// Output data.
|
2020-07-25 01:55:33 +00:00
|
|
|
for(decltype(data.data_length) c = 0; c < data.data_length; c++) {
|
2017-11-03 02:32:00 +00:00
|
|
|
uint8_t next_byte = file_.get8();
|
2017-08-02 18:24:34 +00:00
|
|
|
|
2020-07-25 01:55:33 +00:00
|
|
|
auto bits = (c != data.data_length-1) ? 8 : data.number_of_bits_in_final_byte;
|
2017-08-02 18:36:47 +00:00
|
|
|
while(bits--) {
|
2017-08-03 16:13:41 +00:00
|
|
|
unsigned int pulse_length = (next_byte & 0x80) ? data.length_of_one_bit_pulse : data.length_of_zero_bit_pulse;
|
2017-08-02 18:24:34 +00:00
|
|
|
next_byte <<= 1;
|
|
|
|
|
|
|
|
post_pulse(pulse_length);
|
|
|
|
post_pulse(pulse_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output gap.
|
2017-08-03 16:13:41 +00:00
|
|
|
post_gap(data.pause_after_block);
|
2017-07-17 23:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::get_pure_tone_data_block() {
|
2017-11-03 02:32:00 +00:00
|
|
|
uint16_t length_of_pulse = file_.get16le();
|
|
|
|
uint16_t nunber_of_pulses = file_.get16le();
|
2017-07-17 23:38:15 +00:00
|
|
|
|
2017-12-20 02:53:04 +00:00
|
|
|
post_pulses(nunber_of_pulses, length_of_pulse);
|
2017-07-17 23:38:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 16:13:41 +00:00
|
|
|
void TZX::get_pure_data_block() {
|
|
|
|
Data data;
|
2017-11-03 02:32:00 +00:00
|
|
|
data.length_of_zero_bit_pulse = file_.get16le();
|
|
|
|
data.length_of_one_bit_pulse = file_.get16le();
|
|
|
|
data.number_of_bits_in_final_byte = file_.get8();
|
|
|
|
data.pause_after_block = file_.get16le();
|
|
|
|
data.data_length = file_.get24le();
|
2017-08-03 16:13:41 +00:00
|
|
|
|
|
|
|
get_data(data);
|
|
|
|
}
|
|
|
|
|
2018-02-23 02:28:12 +00:00
|
|
|
void TZX::get_direct_recording_block() {
|
2020-05-10 03:00:39 +00:00
|
|
|
const Storage::Time length_per_sample(unsigned(file_.get16le()), StandardTZXClock);
|
2018-02-23 02:28:12 +00:00
|
|
|
const uint16_t pause_after_block = file_.get16le();
|
|
|
|
uint8_t used_bits_in_final_byte = file_.get8();
|
|
|
|
const uint32_t length_of_data = file_.get24le();
|
|
|
|
|
|
|
|
if(used_bits_in_final_byte < 1) used_bits_in_final_byte = 1;
|
|
|
|
if(used_bits_in_final_byte > 8) used_bits_in_final_byte = 8;
|
|
|
|
|
|
|
|
uint8_t byte = 0;
|
|
|
|
unsigned int bits_at_level = 0;
|
|
|
|
uint8_t level = 0;
|
|
|
|
for(std::size_t bit = 0; bit < (length_of_data - 1) * 8 + used_bits_in_final_byte; ++bit) {
|
|
|
|
if(!(bit&7)) byte = file_.get8();
|
|
|
|
if(!bit) level = byte&0x80;
|
|
|
|
|
|
|
|
if((byte&0x80) != level) {
|
|
|
|
emplace_back(level ? Tape::Pulse::High : Tape::Pulse::Low, length_per_sample * bits_at_level);
|
|
|
|
bits_at_level = 0;
|
|
|
|
level = byte&0x80;
|
|
|
|
}
|
|
|
|
bits_at_level++;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_level_ = !!(level);
|
|
|
|
emplace_back(level ? Tape::Pulse::High : Tape::Pulse::Low, length_per_sample * bits_at_level);
|
|
|
|
|
|
|
|
post_gap(pause_after_block);
|
|
|
|
}
|
|
|
|
|
2017-07-17 23:38:15 +00:00
|
|
|
void TZX::get_pulse_sequence() {
|
2017-11-03 02:32:00 +00:00
|
|
|
uint8_t number_of_pulses = file_.get8();
|
2017-07-17 23:38:15 +00:00
|
|
|
while(number_of_pulses--) {
|
2017-11-03 02:32:00 +00:00
|
|
|
post_pulse(file_.get16le());
|
2017-07-17 23:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-21 22:21:12 +00:00
|
|
|
|
2017-08-02 18:12:34 +00:00
|
|
|
void TZX::get_pause() {
|
2017-11-03 02:32:00 +00:00
|
|
|
uint16_t duration = file_.get16le();
|
2017-08-02 18:12:34 +00:00
|
|
|
if(!duration) {
|
|
|
|
// TODO (maybe): post a 'pause the tape' suggestion
|
|
|
|
} else {
|
|
|
|
post_gap(duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 02:28:12 +00:00
|
|
|
void TZX::get_set_signal_level() {
|
|
|
|
file_.seek(4, SEEK_CUR);
|
|
|
|
const uint8_t level = file_.get8();
|
|
|
|
current_level_ = !!level;
|
|
|
|
}
|
|
|
|
|
2017-12-20 02:53:04 +00:00
|
|
|
void TZX::get_kansas_city_block() {
|
|
|
|
uint32_t block_length = file_.get32le();
|
|
|
|
|
|
|
|
const uint16_t pause_after_block = file_.get16le();
|
|
|
|
const uint16_t pilot_pulse_duration = file_.get16le();
|
|
|
|
const uint16_t pilot_length = file_.get16le();
|
|
|
|
uint16_t pulse_durations[2];
|
|
|
|
pulse_durations[0] = file_.get16le();
|
|
|
|
pulse_durations[1] = file_.get16le();
|
|
|
|
const uint8_t packed_pulse_counts = file_.get8();
|
|
|
|
const unsigned int pulse_counts[2] = {
|
2020-05-10 03:00:39 +00:00
|
|
|
unsigned((((packed_pulse_counts >> 4) - 1) & 15) + 1),
|
|
|
|
unsigned((((packed_pulse_counts & 15) - 1) & 15) + 1)
|
2017-12-20 02:53:04 +00:00
|
|
|
};
|
|
|
|
const uint8_t padding_flags = file_.get8();
|
|
|
|
|
|
|
|
const unsigned int number_of_leading_pulses = ((padding_flags >> 6)&3) * pulse_counts[(padding_flags >> 5) & 1];
|
|
|
|
const unsigned int leading_pulse_length = pulse_durations[(padding_flags >> 5) & 1];
|
|
|
|
|
|
|
|
const unsigned int number_of_trailing_pulses = ((padding_flags >> 3)&3) * pulse_counts[(padding_flags >> 2) & 1];
|
|
|
|
const unsigned int trailing_pulse_length = pulse_durations[(padding_flags >> 2) & 1];
|
|
|
|
|
|
|
|
block_length -= 12;
|
|
|
|
|
|
|
|
// Output pilot tone.
|
|
|
|
post_pulses(pilot_length, pilot_pulse_duration);
|
|
|
|
|
|
|
|
// Output data.
|
|
|
|
while(block_length--) {
|
|
|
|
post_pulses(number_of_leading_pulses, leading_pulse_length);
|
|
|
|
|
|
|
|
uint8_t new_byte = file_.get8();
|
|
|
|
int bits = 8;
|
|
|
|
if(padding_flags & 1) {
|
|
|
|
// Output MSB first.
|
|
|
|
while(bits--) {
|
|
|
|
int bit = (new_byte >> 7) & 1;
|
|
|
|
new_byte <<= 1;
|
|
|
|
post_pulses(pulse_counts[bit], pulse_durations[bit]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Output LSB first.
|
|
|
|
while(bits--) {
|
|
|
|
int bit = new_byte & 1;
|
|
|
|
new_byte >>= 1;
|
|
|
|
post_pulses(pulse_counts[bit], pulse_durations[bit]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
post_pulses(number_of_trailing_pulses, trailing_pulse_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output gap.
|
|
|
|
post_gap(pause_after_block);
|
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Output
|
2017-07-21 22:21:12 +00:00
|
|
|
|
2017-12-20 02:53:04 +00:00
|
|
|
void TZX::post_pulses(unsigned int count, unsigned int length) {
|
|
|
|
while(count--) post_pulse(length);
|
|
|
|
}
|
|
|
|
|
2017-07-21 22:21:12 +00:00
|
|
|
void TZX::post_pulse(unsigned int length) {
|
|
|
|
post_pulse(Storage::Time(length, StandardTZXClock));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::post_gap(unsigned int milliseconds) {
|
|
|
|
if(!milliseconds) return;
|
|
|
|
if(milliseconds > 1 && !current_level_) {
|
|
|
|
post_pulse(Storage::Time(TZXClockMSMultiplier, StandardTZXClock));
|
|
|
|
post_pulse(Storage::Time((milliseconds - 1u)*TZXClockMSMultiplier, StandardTZXClock));
|
|
|
|
} else {
|
|
|
|
post_pulse(Storage::Time(milliseconds*TZXClockMSMultiplier, StandardTZXClock));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::post_pulse(const Storage::Time &time) {
|
|
|
|
emplace_back(current_level_ ? Tape::Pulse::High : Tape::Pulse::Low, time);
|
|
|
|
current_level_ ^= true;
|
|
|
|
}
|
2017-08-03 16:13:41 +00:00
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Flow control; currently ignored
|
2017-08-03 16:13:41 +00:00
|
|
|
|
|
|
|
void TZX::ignore_group_start() {
|
2017-11-03 02:32:00 +00:00
|
|
|
uint8_t length = file_.get8();
|
|
|
|
file_.seek(length, SEEK_CUR);
|
2017-08-03 16:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_group_end() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_jump_to_block() {
|
2017-11-11 02:56:53 +00:00
|
|
|
uint16_t target = file_.get16le();
|
|
|
|
(void)target;
|
2017-08-03 16:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_loop_start() {
|
2017-11-11 02:56:53 +00:00
|
|
|
uint16_t number_of_repetitions = file_.get16le();
|
|
|
|
(void)number_of_repetitions;
|
2017-08-03 16:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_loop_end() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_call_sequence() {
|
2017-11-11 02:56:53 +00:00
|
|
|
uint16_t number_of_entries = file_.get16le();
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.seek(number_of_entries * sizeof(uint16_t), SEEK_CUR);
|
2017-08-03 16:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_return_from_sequence() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_select_block() {
|
2017-11-11 02:56:53 +00:00
|
|
|
uint16_t length_of_block = file_.get16le();
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.seek(length_of_block, SEEK_CUR);
|
2018-02-23 02:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_stop_tape_if_in_48kb_mode() {
|
|
|
|
file_.seek(4, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_custom_info_block() {
|
2018-07-12 02:21:35 +00:00
|
|
|
file_.seek(0x10, SEEK_CUR);
|
2018-02-23 02:28:12 +00:00
|
|
|
uint32_t length = file_.get32le();
|
|
|
|
file_.seek(length, SEEK_CUR);
|
2017-08-03 16:13:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Messaging
|
2017-08-03 16:13:41 +00:00
|
|
|
|
|
|
|
void TZX::ignore_text_description() {
|
2017-11-03 02:32:00 +00:00
|
|
|
uint8_t length = file_.get8();
|
|
|
|
file_.seek(length, SEEK_CUR);
|
2017-08-03 16:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_message_block() {
|
2017-11-11 02:56:53 +00:00
|
|
|
uint8_t time_for_display = file_.get8();
|
2017-11-03 02:32:00 +00:00
|
|
|
uint8_t length = file_.get8();
|
|
|
|
file_.seek(length, SEEK_CUR);
|
2017-11-11 02:56:53 +00:00
|
|
|
(void)time_for_display;
|
2018-02-23 02:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_archive_info() {
|
|
|
|
uint16_t length = file_.get16le();
|
|
|
|
file_.seek(length, SEEK_CUR);
|
2017-08-03 16:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::get_hardware_type() {
|
|
|
|
// TODO: pick a way to retain and communicate this.
|
2017-11-03 02:32:00 +00:00
|
|
|
uint8_t number_of_machines = file_.get8();
|
|
|
|
file_.seek(number_of_machines * 3, SEEK_CUR);
|
2018-02-23 02:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TZX::ignore_glue_block() {
|
|
|
|
file_.seek(9, SEEK_CUR);
|
2017-08-03 16:13:41 +00:00
|
|
|
}
|