Files
fluxengine/encoder.c
David Given 607bb141b3 Radically rework decoding: there's now a simple statistical clock guesser, and
then we read the pulsetrain into nice tidy bits with a proper clock, which
makes the decoder's job way easier. We can actually get rid of the entire MFM
decoder state machine. Also, after sorting out the magic bit patterns at the
beginning of records, we can now reliably pull them out of the bitstream
without needing to know anything about the records themselves.
2018-10-18 00:27:48 +02:00

52 lines
1.4 KiB
C

#include "globals.h"
#define MAX_INTERVAL_US (128/(TICK_FREQUENCY/1000000))
struct encoding_buffer* create_encoding_buffer(int pulselength_ns, int length_pulses)
{
struct encoding_buffer* buffer = calloc(1, sizeof(*buffer));
buffer->pulselength_ns = pulselength_ns;
buffer->length_pulses = length_pulses;
buffer->bitmap = calloc(1, length_pulses);
return buffer;
}
void free_encoding_buffer(struct encoding_buffer* buffer)
{
free(buffer->bitmap);
free(buffer);
}
void encoding_buffer_pulse(struct encoding_buffer* buffer, int timestamp_ns)
{
int pulse = timestamp_ns / buffer->pulselength_ns;
if (pulse < buffer->length_pulses)
buffer->bitmap[pulse] = 1;
}
struct fluxmap* encoding_buffer_encode(const struct encoding_buffer* buffer)
{
struct fluxmap* fluxmap = create_fluxmap();
int lastpulse = 0;
int cursor = 1;
while (cursor < buffer->length_pulses)
{
if ((buffer->bitmap[cursor]) || (cursor-lastpulse == MAX_INTERVAL_US))
{
int delta_ticks = (cursor - lastpulse) / TICKS_PER_NS;
while (delta_ticks >= 0xfe)
{
fluxmap_append_interval(fluxmap, 0xfe);
delta_ticks -= 0xfe;
}
fluxmap_append_interval(fluxmap, delta_ticks);
lastpulse = cursor;
}
cursor++;
}
return fluxmap;
}