2016-08-30 01:10:38 +00:00
|
|
|
//
|
|
|
|
// Tape.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 29/08/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Tape.hpp"
|
|
|
|
|
2016-09-01 12:35:28 +00:00
|
|
|
#include <deque>
|
2016-09-06 00:02:35 +00:00
|
|
|
#include "../TapeParser.hpp"
|
2016-09-18 22:35:50 +00:00
|
|
|
#include "../../NumberTheory/CRC.hpp"
|
2016-09-01 12:35:28 +00:00
|
|
|
|
2016-08-30 01:10:38 +00:00
|
|
|
using namespace StaticAnalyser::Acorn;
|
|
|
|
|
2016-09-05 21:17:52 +00:00
|
|
|
enum class WaveType {
|
|
|
|
Short, Long, Unrecognised
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class SymbolType {
|
|
|
|
One, Zero
|
|
|
|
};
|
|
|
|
|
2016-09-06 00:02:35 +00:00
|
|
|
class Acorn1200BaudTapeParser: public StaticAnalyer::TapeParser<WaveType, SymbolType> {
|
2016-09-05 21:17:52 +00:00
|
|
|
public:
|
2016-09-18 22:35:50 +00:00
|
|
|
Acorn1200BaudTapeParser(const std::shared_ptr<Storage::Tape::Tape> &tape) :
|
|
|
|
TapeParser(tape),
|
|
|
|
_crc(0x1021, 0x0000) {}
|
2016-08-30 01:53:06 +00:00
|
|
|
|
2016-09-05 21:17:52 +00:00
|
|
|
int get_next_bit()
|
|
|
|
{
|
2016-09-05 23:59:58 +00:00
|
|
|
SymbolType symbol = get_next_symbol();
|
|
|
|
return (symbol == SymbolType::One) ? 1 : 0;
|
2016-09-05 21:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int get_next_byte()
|
|
|
|
{
|
|
|
|
int value = 0;
|
|
|
|
int c = 8;
|
|
|
|
if(get_next_bit())
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
2016-09-09 01:09:05 +00:00
|
|
|
set_error_flag();
|
2016-09-05 21:17:52 +00:00
|
|
|
return -1;
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
2016-09-05 21:17:52 +00:00
|
|
|
while(c--)
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
2016-09-05 21:17:52 +00:00
|
|
|
value = (value >> 1) | (get_next_bit() << 7);
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
2016-09-05 21:17:52 +00:00
|
|
|
if(!get_next_bit())
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
2016-09-09 01:09:05 +00:00
|
|
|
set_error_flag();
|
2016-09-05 21:17:52 +00:00
|
|
|
return -1;
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
2016-09-18 22:35:50 +00:00
|
|
|
_crc.add((uint8_t)value);
|
2016-09-05 21:17:52 +00:00
|
|
|
return value;
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 21:17:52 +00:00
|
|
|
int get_next_short()
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
2016-09-05 21:17:52 +00:00
|
|
|
int result = get_next_byte();
|
|
|
|
result |= get_next_byte() << 8;
|
|
|
|
return result;
|
2016-08-30 11:38:08 +00:00
|
|
|
}
|
2016-09-05 21:17:52 +00:00
|
|
|
|
|
|
|
int get_next_word()
|
2016-08-30 11:38:08 +00:00
|
|
|
{
|
2016-09-05 21:17:52 +00:00
|
|
|
int result = get_next_short();
|
|
|
|
result |= get_next_short() << 8;
|
|
|
|
return result;
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 22:35:50 +00:00
|
|
|
void reset_crc() { _crc.reset(); }
|
|
|
|
uint16_t get_crc() { return _crc.get_value(); }
|
2016-08-30 11:38:08 +00:00
|
|
|
|
2016-09-05 21:17:52 +00:00
|
|
|
private:
|
2016-09-05 23:59:58 +00:00
|
|
|
void process_pulse(Storage::Tape::Tape::Pulse pulse)
|
2016-09-05 21:17:52 +00:00
|
|
|
{
|
|
|
|
switch(pulse.type)
|
|
|
|
{
|
|
|
|
default: break;
|
|
|
|
case Storage::Tape::Tape::Pulse::High:
|
|
|
|
case Storage::Tape::Tape::Pulse::Low:
|
|
|
|
float pulse_length = pulse.length.get_float();
|
2016-09-05 23:59:58 +00:00
|
|
|
if(pulse_length >= 0.35 / 2400.0 && pulse_length < 0.7 / 2400.0) { push_wave(WaveType::Short); return; }
|
|
|
|
if(pulse_length >= 0.35 / 1200.0 && pulse_length < 0.7 / 1200.0) { push_wave(WaveType::Long); return; }
|
2016-09-05 21:17:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-08-30 11:38:08 +00:00
|
|
|
|
2016-09-05 23:59:58 +00:00
|
|
|
push_wave(WaveType::Unrecognised);
|
2016-09-05 21:17:52 +00:00
|
|
|
}
|
2016-08-30 11:38:08 +00:00
|
|
|
|
2016-09-05 23:59:58 +00:00
|
|
|
void inspect_waves(const std::vector<WaveType> &waves)
|
2016-09-05 21:17:52 +00:00
|
|
|
{
|
2016-09-06 10:59:51 +00:00
|
|
|
if(waves.size() < 2) return;
|
2016-08-30 11:38:08 +00:00
|
|
|
|
2016-09-06 10:59:51 +00:00
|
|
|
if(waves[0] == WaveType::Long && waves[1] == WaveType::Long)
|
2016-09-05 21:17:52 +00:00
|
|
|
{
|
2016-09-05 23:59:58 +00:00
|
|
|
push_symbol(SymbolType::Zero, 2);
|
|
|
|
return;
|
2016-09-05 21:17:52 +00:00
|
|
|
}
|
2016-08-30 11:38:08 +00:00
|
|
|
|
2016-09-06 10:59:51 +00:00
|
|
|
if(waves.size() < 4) return;
|
|
|
|
|
|
|
|
if( waves[0] == WaveType::Short &&
|
|
|
|
waves[1] == WaveType::Short &&
|
|
|
|
waves[2] == WaveType::Short &&
|
|
|
|
waves[3] == WaveType::Short)
|
2016-09-05 21:17:52 +00:00
|
|
|
{
|
2016-09-06 10:59:51 +00:00
|
|
|
push_symbol(SymbolType::One, 4);
|
2016-09-05 23:59:58 +00:00
|
|
|
return;
|
2016-09-05 21:17:52 +00:00
|
|
|
}
|
2016-09-06 10:59:51 +00:00
|
|
|
|
|
|
|
remove_waves(1);
|
2016-09-05 21:17:52 +00:00
|
|
|
}
|
2016-08-30 11:38:08 +00:00
|
|
|
|
2016-09-18 22:35:50 +00:00
|
|
|
NumberTheory::CRC16 _crc;
|
2016-08-30 01:53:06 +00:00
|
|
|
};
|
|
|
|
|
2016-09-05 21:17:52 +00:00
|
|
|
static std::unique_ptr<File::Chunk> GetNextChunk(Acorn1200BaudTapeParser &parser)
|
2016-08-30 01:10:38 +00:00
|
|
|
{
|
2016-08-30 11:38:08 +00:00
|
|
|
std::unique_ptr<File::Chunk> new_chunk(new File::Chunk);
|
2016-08-30 01:53:06 +00:00
|
|
|
int shift_register = 0;
|
|
|
|
|
2016-08-30 12:13:40 +00:00
|
|
|
// TODO: move this into the parser
|
|
|
|
#define shift() shift_register = (shift_register >> 1) | (parser.get_next_bit() << 9)
|
2016-08-30 01:53:06 +00:00
|
|
|
|
|
|
|
// find next area of high tone
|
2016-08-30 11:38:08 +00:00
|
|
|
while(!parser.is_at_end() && (shift_register != 0x3ff))
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
|
|
|
shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
// find next 0x2a (swallowing stop bit)
|
2016-08-30 11:38:08 +00:00
|
|
|
while(!parser.is_at_end() && (shift_register != 0x254))
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
|
|
|
shift();
|
|
|
|
}
|
|
|
|
|
2016-08-30 12:13:40 +00:00
|
|
|
#undef shift
|
|
|
|
|
2016-08-30 11:38:08 +00:00
|
|
|
parser.reset_crc();
|
|
|
|
parser.reset_error_flag();
|
|
|
|
|
2016-08-30 01:53:06 +00:00
|
|
|
// read out name
|
2016-09-05 22:15:15 +00:00
|
|
|
char name[11];
|
2016-08-30 01:53:06 +00:00
|
|
|
int name_ptr = 0;
|
2016-09-05 22:28:43 +00:00
|
|
|
while(!parser.is_at_end() && name_ptr < sizeof(name))
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
2016-08-30 11:38:08 +00:00
|
|
|
name[name_ptr] = (char)parser.get_next_byte();
|
2016-08-30 01:53:06 +00:00
|
|
|
if(!name[name_ptr]) break;
|
|
|
|
name_ptr++;
|
|
|
|
}
|
2016-09-05 22:28:43 +00:00
|
|
|
name[sizeof(name)-1] = '\0';
|
2016-08-31 23:57:09 +00:00
|
|
|
new_chunk->name = name;
|
2016-08-30 01:53:06 +00:00
|
|
|
|
2016-08-30 11:38:08 +00:00
|
|
|
// addresses
|
|
|
|
new_chunk->load_address = (uint32_t)parser.get_next_word();
|
|
|
|
new_chunk->execution_address = (uint32_t)parser.get_next_word();
|
|
|
|
new_chunk->block_number = (uint16_t)parser.get_next_short();
|
|
|
|
new_chunk->block_length = (uint16_t)parser.get_next_short();
|
|
|
|
new_chunk->block_flag = (uint8_t)parser.get_next_byte();
|
|
|
|
new_chunk->next_address = (uint32_t)parser.get_next_word();
|
|
|
|
|
2016-08-30 12:13:40 +00:00
|
|
|
uint16_t calculated_header_crc = parser.get_crc();
|
|
|
|
uint16_t stored_header_crc = (uint16_t)parser.get_next_short();
|
|
|
|
stored_header_crc = (uint16_t)((stored_header_crc >> 8) | (stored_header_crc << 8));
|
|
|
|
new_chunk->header_crc_matched = stored_header_crc == calculated_header_crc;
|
2016-08-30 11:38:08 +00:00
|
|
|
|
2016-08-30 12:13:40 +00:00
|
|
|
parser.reset_crc();
|
|
|
|
new_chunk->data.reserve(new_chunk->block_length);
|
|
|
|
for(int c = 0; c < new_chunk->block_length; c++)
|
|
|
|
{
|
|
|
|
new_chunk->data.push_back((uint8_t)parser.get_next_byte());
|
|
|
|
}
|
2016-08-30 11:38:08 +00:00
|
|
|
|
2016-09-05 22:15:15 +00:00
|
|
|
if(new_chunk->block_length && !(new_chunk->block_flag&0x40))
|
2016-08-30 12:13:40 +00:00
|
|
|
{
|
|
|
|
uint16_t calculated_data_crc = parser.get_crc();
|
|
|
|
uint16_t stored_data_crc = (uint16_t)parser.get_next_short();
|
|
|
|
stored_data_crc = (uint16_t)((stored_data_crc >> 8) | (stored_data_crc << 8));
|
|
|
|
new_chunk->data_crc_matched = stored_data_crc == calculated_data_crc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_chunk->data_crc_matched = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parser.get_error_flag() ? nullptr : std::move(new_chunk);
|
2016-08-30 11:38:08 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 12:35:28 +00:00
|
|
|
std::unique_ptr<File> GetNextFile(std::deque<File::Chunk> &chunks)
|
2016-08-31 23:57:09 +00:00
|
|
|
{
|
|
|
|
// find next chunk with a block number of 0
|
2016-09-01 12:35:28 +00:00
|
|
|
while(chunks.size() && chunks.front().block_number)
|
2016-08-31 23:57:09 +00:00
|
|
|
{
|
2016-09-01 12:35:28 +00:00
|
|
|
chunks.pop_front();
|
2016-08-31 23:57:09 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 12:35:28 +00:00
|
|
|
if(!chunks.size()) return nullptr;
|
2016-09-01 00:24:13 +00:00
|
|
|
|
2016-08-31 23:57:09 +00:00
|
|
|
// accumulate chunks for as long as block number is sequential and the end-of-file bit isn't set
|
|
|
|
std::unique_ptr<File> file(new File);
|
2016-09-01 12:35:28 +00:00
|
|
|
|
2016-09-05 21:53:57 +00:00
|
|
|
uint16_t block_number = 0;
|
2016-09-01 12:35:28 +00:00
|
|
|
|
|
|
|
while(chunks.size())
|
2016-08-31 23:57:09 +00:00
|
|
|
{
|
2016-09-05 21:53:57 +00:00
|
|
|
if(chunks.front().block_number != block_number) return nullptr;
|
2016-09-01 12:35:28 +00:00
|
|
|
|
|
|
|
bool was_last = chunks.front().block_flag & 0x80;
|
|
|
|
file->chunks.push_back(chunks.front());
|
|
|
|
chunks.pop_front();
|
|
|
|
block_number++;
|
|
|
|
|
|
|
|
if(was_last) break;
|
2016-08-31 23:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// accumulate total data, copy flags appropriately
|
|
|
|
file->name = file->chunks.front().name;
|
|
|
|
file->load_address = file->chunks.front().load_address;
|
|
|
|
file->execution_address = file->chunks.front().execution_address;
|
|
|
|
file->is_protected = !!(file->chunks.back().block_flag & 0x01); // I think the last flags are the ones that count; TODO: check.
|
|
|
|
|
|
|
|
// copy all data into a single big block
|
|
|
|
for(File::Chunk chunk : file->chunks)
|
|
|
|
{
|
|
|
|
file->data.insert(file->data.end(), chunk.data.begin(), chunk.data.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<File> StaticAnalyser::Acorn::GetFiles(const std::shared_ptr<Storage::Tape::Tape> &tape)
|
2016-08-30 11:38:08 +00:00
|
|
|
{
|
2016-09-05 21:17:52 +00:00
|
|
|
Acorn1200BaudTapeParser parser(tape);
|
2016-08-30 11:38:08 +00:00
|
|
|
|
2016-09-01 12:35:28 +00:00
|
|
|
// populate chunk list
|
|
|
|
std::deque<File::Chunk> chunk_list;
|
2016-08-31 23:57:09 +00:00
|
|
|
while(!parser.is_at_end())
|
|
|
|
{
|
2016-09-01 12:35:28 +00:00
|
|
|
std::unique_ptr<File::Chunk> chunk = GetNextChunk(parser);
|
|
|
|
if(chunk)
|
|
|
|
{
|
|
|
|
chunk_list.push_back(*chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// decompose into file list
|
|
|
|
std::list<File> file_list;
|
|
|
|
|
|
|
|
while(chunk_list.size())
|
|
|
|
{
|
|
|
|
std::unique_ptr<File> next_file = GetNextFile(chunk_list);
|
2016-08-31 23:57:09 +00:00
|
|
|
if(next_file)
|
|
|
|
{
|
|
|
|
file_list.push_back(*next_file);
|
|
|
|
}
|
|
|
|
}
|
2016-08-30 11:38:08 +00:00
|
|
|
|
2016-08-31 23:57:09 +00:00
|
|
|
return file_list;
|
2016-08-30 01:10:38 +00:00
|
|
|
}
|