2016-08-15 23:44:41 +00:00
|
|
|
//
|
|
|
|
// TapePRG.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/08/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-08-15 23:44:41 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "TapePRG.hpp"
|
|
|
|
|
2016-08-19 15:04:59 +00:00
|
|
|
/*
|
|
|
|
My interpretation of Commodore's tape format is such that a PRG is encoded as:
|
|
|
|
|
|
|
|
[long block of lead-in tone]
|
|
|
|
[short block of lead-in tone]
|
|
|
|
[count down][header; 192 bytes fixed length]
|
|
|
|
[short block of lead-in tone]
|
|
|
|
[count down][copy of header; 192 bytes fixed length]
|
|
|
|
[gap]
|
|
|
|
[short block of lead-in tone]
|
|
|
|
[count down][data; length as in file]
|
|
|
|
[short block of lead-in tone]
|
|
|
|
[count down][copy of data]
|
|
|
|
... and repeat ...
|
|
|
|
|
|
|
|
Individual bytes are composed of:
|
|
|
|
|
|
|
|
word marker
|
|
|
|
least significant bit
|
|
|
|
...
|
|
|
|
most significant bit
|
|
|
|
parity bit
|
|
|
|
|
|
|
|
Both the header and data blocks additionally end with an end-of-block marker.
|
|
|
|
|
|
|
|
Encoding is via square-wave cycles of four lengths, in ascending order: lead-in, zero, one, marker.
|
|
|
|
|
|
|
|
Lead-in tone is always just repetitions of the lead-in wave.
|
|
|
|
A word marker is a marker wave followed by a one wave.
|
|
|
|
An end-of-block marker is a marker wave followed by a zero wave.
|
|
|
|
A zero bit is a zero wave followed by a one wave.
|
|
|
|
A one bit is a one wave followed by a zero wave.
|
|
|
|
|
|
|
|
Parity is 1 if there are an even number of bits in the byte; 0 otherwise.
|
|
|
|
*/
|
|
|
|
|
2016-08-15 23:44:41 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2016-08-27 21:18:12 +00:00
|
|
|
using namespace Storage::Tape;
|
2016-08-15 23:44:41 +00:00
|
|
|
|
2018-04-06 21:42:24 +00:00
|
|
|
PRG::PRG(const std::string &file_name) :
|
2017-11-03 02:32:00 +00:00
|
|
|
file_(file_name)
|
2016-08-15 23:44:41 +00:00
|
|
|
{
|
|
|
|
// There's really no way to validate other than that if this file is larger than 64kb,
|
|
|
|
// of if load address + length > 65536 then it's broken.
|
2017-11-03 02:32:00 +00:00
|
|
|
if(file_.stats().st_size >= 65538 || file_.stats().st_size < 3)
|
2016-08-15 23:44:41 +00:00
|
|
|
throw ErrorBadFormat;
|
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
load_address_ = file_.get16le();
|
|
|
|
length_ = static_cast<uint16_t>(file_.stats().st_size - 2);
|
2016-08-15 23:44:41 +00:00
|
|
|
|
2016-11-21 12:14:09 +00:00
|
|
|
if (load_address_ + length_ >= 65536)
|
2016-08-15 23:44:41 +00:00
|
|
|
throw ErrorBadFormat;
|
|
|
|
}
|
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
Storage::Tape::Tape::Pulse PRG::virtual_get_next_pulse() {
|
2016-08-17 01:09:50 +00:00
|
|
|
// these are all microseconds per pole
|
2016-08-16 00:08:50 +00:00
|
|
|
static const unsigned int leader_zero_length = 179;
|
|
|
|
static const unsigned int zero_length = 169;
|
|
|
|
static const unsigned int one_length = 247;
|
2016-08-16 02:40:05 +00:00
|
|
|
static const unsigned int marker_length = 328;
|
2016-08-16 00:08:50 +00:00
|
|
|
|
2016-11-21 12:14:09 +00:00
|
|
|
bit_phase_ = (bit_phase_+1)&3;
|
|
|
|
if(!bit_phase_) get_next_output_token();
|
2016-08-16 00:08:50 +00:00
|
|
|
|
2016-08-15 23:44:41 +00:00
|
|
|
Tape::Pulse pulse;
|
2016-08-19 14:58:42 +00:00
|
|
|
pulse.length.clock_rate = 1000000;
|
2016-11-21 12:14:09 +00:00
|
|
|
pulse.type = (bit_phase_&1) ? Tape::Pulse::High : Tape::Pulse::Low;
|
2017-11-03 02:32:00 +00:00
|
|
|
switch(output_token_) {
|
2016-08-16 02:40:05 +00:00
|
|
|
case Leader: pulse.length.length = leader_zero_length; break;
|
2016-11-21 12:14:09 +00:00
|
|
|
case Zero: pulse.length.length = (bit_phase_&2) ? one_length : zero_length; break;
|
|
|
|
case One: pulse.length.length = (bit_phase_&2) ? zero_length : one_length; break;
|
|
|
|
case WordMarker: pulse.length.length = (bit_phase_&2) ? one_length : marker_length; break;
|
|
|
|
case EndOfBlock: pulse.length.length = (bit_phase_&2) ? zero_length : marker_length; break;
|
2016-08-27 21:09:45 +00:00
|
|
|
case Silence: pulse.type = Tape::Pulse::Zero; pulse.length.length = 5000; break;
|
2016-08-16 00:08:50 +00:00
|
|
|
}
|
2016-08-15 23:44:41 +00:00
|
|
|
return pulse;
|
|
|
|
}
|
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
void PRG::virtual_reset() {
|
2016-11-21 12:14:09 +00:00
|
|
|
bit_phase_ = 3;
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.seek(2, SEEK_SET);
|
2016-11-21 12:14:09 +00:00
|
|
|
file_phase_ = FilePhaseLeadIn;
|
|
|
|
phase_offset_ = 0;
|
|
|
|
copy_mask_ = 0x80;
|
2016-08-16 00:08:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
bool PRG::is_at_end() {
|
2016-11-21 12:14:09 +00:00
|
|
|
return file_phase_ == FilePhaseAtEnd;
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
void PRG::get_next_output_token() {
|
2016-08-17 01:09:50 +00:00
|
|
|
static const int block_length = 192; // not counting the checksum
|
|
|
|
static const int countdown_bytes = 9;
|
2016-08-19 14:58:42 +00:00
|
|
|
static const int leadin_length = 20000;
|
|
|
|
static const int block_leadin_length = 5000;
|
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
if(file_phase_ == FilePhaseHeaderDataGap || file_phase_ == FilePhaseAtEnd) {
|
2016-11-21 12:14:09 +00:00
|
|
|
output_token_ = Silence;
|
|
|
|
if(file_phase_ != FilePhaseAtEnd) file_phase_ = FilePhaseData;
|
2016-08-19 14:58:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-08-17 01:09:50 +00:00
|
|
|
|
2016-08-16 02:10:53 +00:00
|
|
|
// the lead-in is 20,000 instances of the lead-in pair; every other phase begins with 5000
|
|
|
|
// before doing whatever it should be doing
|
2017-11-03 02:32:00 +00:00
|
|
|
if(file_phase_ == FilePhaseLeadIn || phase_offset_ < block_leadin_length) {
|
2016-11-21 12:14:09 +00:00
|
|
|
output_token_ = Leader;
|
|
|
|
phase_offset_++;
|
2017-11-03 02:32:00 +00:00
|
|
|
if(file_phase_ == FilePhaseLeadIn && phase_offset_ == leadin_length) {
|
2016-11-21 12:14:09 +00:00
|
|
|
phase_offset_ = 0;
|
|
|
|
file_phase_ = (file_phase_ == FilePhaseLeadIn) ? FilePhaseHeader : FilePhaseData;
|
2016-08-16 02:10:53 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine whether a new byte needs to be queued up
|
2016-11-21 12:14:09 +00:00
|
|
|
int block_offset = phase_offset_ - block_leadin_length;
|
2016-08-16 02:10:53 +00:00
|
|
|
int bit_offset = block_offset % 10;
|
|
|
|
int byte_offset = block_offset / 10;
|
2016-11-21 12:14:09 +00:00
|
|
|
phase_offset_++;
|
2016-08-16 02:10:53 +00:00
|
|
|
|
2016-08-19 14:58:42 +00:00
|
|
|
if(!bit_offset &&
|
|
|
|
(
|
2016-11-21 12:14:09 +00:00
|
|
|
(file_phase_ == FilePhaseHeader && byte_offset == block_length + countdown_bytes + 1) ||
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.eof()
|
2016-08-19 14:58:42 +00:00
|
|
|
)
|
2017-11-03 02:32:00 +00:00
|
|
|
) {
|
2016-11-21 12:14:09 +00:00
|
|
|
output_token_ = EndOfBlock;
|
|
|
|
phase_offset_ = 0;
|
2016-08-17 12:03:34 +00:00
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
switch(file_phase_) {
|
2016-08-17 12:03:34 +00:00
|
|
|
default: break;
|
|
|
|
case FilePhaseHeader:
|
2016-11-21 12:14:09 +00:00
|
|
|
copy_mask_ ^= 0x80;
|
|
|
|
if(copy_mask_) file_phase_ = FilePhaseHeaderDataGap;
|
2016-08-17 12:03:34 +00:00
|
|
|
break;
|
|
|
|
case FilePhaseData:
|
2016-11-21 12:14:09 +00:00
|
|
|
copy_mask_ ^= 0x80;
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.seek(2, SEEK_SET);
|
2016-11-21 12:14:09 +00:00
|
|
|
if(copy_mask_) file_phase_ = FilePhaseAtEnd;
|
2016-08-17 12:03:34 +00:00
|
|
|
break;
|
2016-08-17 02:17:08 +00:00
|
|
|
}
|
2016-08-16 02:40:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
if(bit_offset == 0) {
|
2016-08-16 02:10:53 +00:00
|
|
|
// the first nine bytes are countdown; the high bit is set if this is a header
|
2017-11-03 02:32:00 +00:00
|
|
|
if(byte_offset < countdown_bytes) {
|
2017-10-04 02:04:15 +00:00
|
|
|
output_byte_ = static_cast<uint8_t>(countdown_bytes - byte_offset) | copy_mask_;
|
2017-11-03 02:32:00 +00:00
|
|
|
} else {
|
|
|
|
if(file_phase_ == FilePhaseHeader) {
|
|
|
|
if(byte_offset == countdown_bytes + block_length) {
|
2016-11-21 12:14:09 +00:00
|
|
|
output_byte_ = check_digit_;
|
2017-11-03 02:32:00 +00:00
|
|
|
} else {
|
2016-11-21 12:14:09 +00:00
|
|
|
if(byte_offset == countdown_bytes) check_digit_ = 0;
|
2017-11-03 02:32:00 +00:00
|
|
|
if(file_phase_ == FilePhaseHeader) {
|
|
|
|
switch(byte_offset - countdown_bytes) {
|
2016-11-21 12:14:09 +00:00
|
|
|
case 0: output_byte_ = 0x03; break;
|
|
|
|
case 1: output_byte_ = load_address_ & 0xff; break;
|
|
|
|
case 2: output_byte_ = (load_address_ >> 8)&0xff; break;
|
|
|
|
case 3: output_byte_ = (load_address_ + length_) & 0xff; break;
|
|
|
|
case 4: output_byte_ = ((load_address_ + length_) >> 8) & 0xff; break;
|
|
|
|
|
|
|
|
case 5: output_byte_ = 0x50; break; // P
|
|
|
|
case 6: output_byte_ = 0x52; break; // R
|
|
|
|
case 7: output_byte_ = 0x47; break; // G
|
2016-08-19 14:58:42 +00:00
|
|
|
default:
|
2016-11-21 12:14:09 +00:00
|
|
|
output_byte_ = 0x20;
|
2016-08-19 14:58:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 02:40:05 +00:00
|
|
|
}
|
2017-11-03 02:32:00 +00:00
|
|
|
} else {
|
|
|
|
output_byte_ = file_.get8();
|
|
|
|
if(file_.eof()) {
|
2016-11-21 12:14:09 +00:00
|
|
|
output_byte_ = check_digit_;
|
2016-08-19 14:58:42 +00:00
|
|
|
}
|
2016-08-16 02:40:05 +00:00
|
|
|
}
|
2016-08-16 02:44:36 +00:00
|
|
|
|
2016-11-21 12:14:09 +00:00
|
|
|
check_digit_ ^= output_byte_;
|
2016-08-16 02:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
switch(bit_offset) {
|
2016-08-16 02:10:53 +00:00
|
|
|
case 0:
|
2016-11-21 12:14:09 +00:00
|
|
|
output_token_ = WordMarker;
|
2016-08-16 02:10:53 +00:00
|
|
|
break;
|
2018-05-13 19:43:03 +00:00
|
|
|
default: // i.e. 1-8
|
2016-11-21 12:14:09 +00:00
|
|
|
output_token_ = (output_byte_ & (1 << (bit_offset - 1))) ? One : Zero;
|
2016-08-16 02:10:53 +00:00
|
|
|
break;
|
2017-11-03 02:32:00 +00:00
|
|
|
case 9: {
|
2016-11-21 12:14:09 +00:00
|
|
|
uint8_t parity = output_byte_;
|
2016-08-16 02:10:53 +00:00
|
|
|
parity ^= (parity >> 4);
|
|
|
|
parity ^= (parity >> 2);
|
|
|
|
parity ^= (parity >> 1);
|
2016-11-21 12:14:09 +00:00
|
|
|
output_token_ = (parity&1) ? Zero : One;
|
2016-08-16 02:10:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-08-15 23:44:41 +00:00
|
|
|
}
|