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"
|
|
|
|
|
|
|
|
using namespace StaticAnalyser::Acorn;
|
|
|
|
|
2016-08-30 01:53:06 +00:00
|
|
|
struct TapeParser {
|
|
|
|
|
2016-08-30 11:38:08 +00:00
|
|
|
TapeParser(const std::shared_ptr<Storage::Tape::Tape> &tape) : _wave_length_pointer(0), _tape(tape) {}
|
2016-08-30 01:53:06 +00:00
|
|
|
|
2016-08-30 11:38:08 +00:00
|
|
|
int get_next_bit()
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
2016-08-30 11:38:08 +00:00
|
|
|
while(!_tape->is_at_end())
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
|
|
|
// skip any gaps
|
2016-08-30 11:38:08 +00:00
|
|
|
Storage::Tape::Tape::Pulse next_pulse = _tape->get_next_pulse();
|
2016-08-30 01:53:06 +00:00
|
|
|
while(next_pulse.type == Storage::Tape::Tape::Pulse::Zero)
|
|
|
|
{
|
2016-08-30 11:38:08 +00:00
|
|
|
next_pulse = _tape->get_next_pulse();
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 11:38:08 +00:00
|
|
|
_wave_lengths[_wave_length_pointer] = next_pulse.length.get_float();
|
|
|
|
_wave_length_pointer++;
|
2016-08-30 01:53:06 +00:00
|
|
|
|
|
|
|
// if first wave is too short or too long, drop it
|
2016-08-30 11:38:08 +00:00
|
|
|
if(_wave_lengths[0] < 1.0f / 4800.0f || _wave_lengths[0] >= 5.0f / 4800.0f)
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
|
|
|
rotate(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if first two waves add up to a correct-length cycle, pop them and this is a 0
|
2016-08-30 11:38:08 +00:00
|
|
|
if(_wave_length_pointer >= 2)
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
2016-08-30 11:38:08 +00:00
|
|
|
float length = _wave_lengths[0] + _wave_lengths[1];
|
2016-08-30 01:53:06 +00:00
|
|
|
if(length >= 3.0f / 4800.0f && length < 5.0f / 4800.0f)
|
|
|
|
{
|
|
|
|
rotate(2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if all four waves add up to a correct-length cycle, pop them and this is a 1
|
2016-08-30 11:38:08 +00:00
|
|
|
if(_wave_length_pointer == 4)
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
2016-08-30 11:38:08 +00:00
|
|
|
float length = _wave_lengths[0] + _wave_lengths[1] + _wave_lengths[2] + _wave_lengths[3];
|
2016-08-30 01:53:06 +00:00
|
|
|
if(length >= 3.0f / 4800.0f && length < 5.0f / 4800.0f)
|
|
|
|
{
|
|
|
|
rotate(4);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-30 11:38:08 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rotate(1);
|
|
|
|
}
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-30 11:38:08 +00:00
|
|
|
int get_next_byte()
|
2016-08-30 01:53:06 +00:00
|
|
|
{
|
|
|
|
int value = 0;
|
2016-08-30 02:05:06 +00:00
|
|
|
int c = 8;
|
2016-08-30 11:38:08 +00:00
|
|
|
if(get_next_bit())
|
|
|
|
{
|
|
|
|
_error_flag = true;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-08-30 01:53:06 +00:00
|
|
|
while(c--)
|
|
|
|
{
|
2016-08-30 11:38:08 +00:00
|
|
|
value = (value >> 1) | (get_next_bit() << 7);
|
|
|
|
}
|
|
|
|
if(!get_next_bit())
|
|
|
|
{
|
|
|
|
_error_flag = true;
|
|
|
|
return -1;
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
2016-08-30 11:38:08 +00:00
|
|
|
add_to_crc((uint8_t)value);
|
2016-08-30 02:05:06 +00:00
|
|
|
return value;
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 11:38:08 +00:00
|
|
|
int get_next_short()
|
|
|
|
{
|
|
|
|
int result = get_next_byte();
|
|
|
|
result |= get_next_byte() << 8;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_next_word()
|
|
|
|
{
|
|
|
|
int result = get_next_short();
|
|
|
|
result |= get_next_short() << 8;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_error_flag()
|
|
|
|
{
|
|
|
|
_error_flag = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_error_flag()
|
|
|
|
{
|
|
|
|
return _error_flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_crc()
|
|
|
|
{
|
|
|
|
_crc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_to_crc(uint8_t value)
|
|
|
|
{
|
|
|
|
_crc ^= (uint16_t)value << 8;
|
|
|
|
for(int c = 0; c < 8; c++)
|
|
|
|
{
|
|
|
|
uint16_t exclusive_or = (_crc&0x8000) ? 0x1021 : 0x0000;
|
|
|
|
_crc = (uint16_t)(_crc << 1) ^ exclusive_or;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t get_crc()
|
|
|
|
{
|
|
|
|
return _crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_at_end()
|
|
|
|
{
|
|
|
|
return _tape->is_at_end();
|
|
|
|
}
|
|
|
|
|
2016-08-30 01:53:06 +00:00
|
|
|
private:
|
|
|
|
void rotate(int places)
|
|
|
|
{
|
2016-08-30 11:38:08 +00:00
|
|
|
_wave_length_pointer -= places;
|
|
|
|
if(places < 4) memmove(_wave_lengths, &_wave_lengths[places], (size_t)(4 - places) * sizeof(float));
|
2016-08-30 01:53:06 +00:00
|
|
|
}
|
2016-08-30 11:38:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
uint16_t _crc;
|
|
|
|
bool _error_flag;
|
|
|
|
|
|
|
|
float _wave_lengths[4];
|
|
|
|
int _wave_length_pointer;
|
|
|
|
std::shared_ptr<Storage::Tape::Tape> _tape;
|
2016-08-30 01:53:06 +00:00
|
|
|
};
|
|
|
|
|
2016-08-30 11:38:08 +00:00
|
|
|
static std::unique_ptr<File::Chunk> GetNextChunk(TapeParser &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;
|
|
|
|
|
|
|
|
#define shift() \
|
2016-08-30 11:38:08 +00:00
|
|
|
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 11:38:08 +00:00
|
|
|
parser.reset_crc();
|
|
|
|
parser.reset_error_flag();
|
|
|
|
|
2016-08-30 01:53:06 +00:00
|
|
|
// read out name
|
|
|
|
char name[10];
|
|
|
|
int name_ptr = 0;
|
2016-08-30 11:38:08 +00:00
|
|
|
while(!parser.is_at_end() && name_ptr < 10)
|
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-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();
|
|
|
|
|
|
|
|
uint16_t calculated_crc = parser.get_crc();
|
|
|
|
uint16_t stored_crc = (uint16_t)parser.get_next_short();
|
|
|
|
|
|
|
|
printf("%04x v %04x", calculated_crc, stored_crc);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<File> StaticAnalyser::Acorn::GetNextFile(const std::shared_ptr<Storage::Tape::Tape> &tape)
|
|
|
|
{
|
|
|
|
TapeParser parser(tape);
|
|
|
|
|
|
|
|
GetNextChunk(parser);
|
|
|
|
|
2016-08-30 01:10:38 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|