summaryrefslogtreecommitdiffstats
path: root/mfmdecode.c
blob: 87d19b39a4e32717461c82f296c63d91a6dec40e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Decode ABC80 cassette files
 *
 * Convert to single channel 16-bit with appropriate endianness first:
 *
 * sox muzak.wav --endian little -c 1 -s muzak.raw
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <locale.h>
#include <inttypes.h>

static unsigned int xtime;

static const wchar_t my_tolower[256] =
  L"\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
  L"\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
  L" !\"#¤%&'()*+,-./0123456789:;<=>?"
  L"éabcdefghijklmnopqrstuvwxyzäöåü_"
  L"éabcdefghijklmnopqrstuvwxyzäöåü\377"
  L"\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217"
  L"\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237"
  L"\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257"
  L"\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277"
  L"\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317"
  L"\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337"
  L"\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357"
  L"\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377";

static void unmangle_filename(char *out, const uint8_t *in)
{
	int i;

	wctomb(NULL, 0);

	for (i = 0; i < 8; i++) {
		if (*in != ' ')
			out += wctomb(out, my_tolower[*in]);
		in++;
	}

	if (memcmp(in, "   ", 3)) {
		out += wctomb(out, L'.');
		for (i = 0; i < 3; i++) {
			if (*in != ' ')
				out += wctomb(out, my_tolower[*in]);
			in++;
		}
	}
	*out = '\0';
}

static void output_block(uint8_t *buf, uint8_t *csum)
{
	uint16_t csum1, csum2;
	int i;
	static FILE *f;
	static uint16_t block;
	static char filename[64];
	int err = 0;

	if (!buf) {
		err = 1;	/* Notification of a bad block */
	} else {
		csum2 = csum[0] + (csum[1] << 8);

		csum1 = 0x03;		/* Checksum includes ETX */
		for (i = 0; i < 256; i++)
			csum1 += buf[i];
		
		if (csum1 == csum2) {
			if (!memcmp(buf, "\xff\xff\xff", 3)) {
				if (f)
					fclose(f);
				
				unmangle_filename(filename, buf+3);
				f = fopen(filename, "wb");
				if (!f) {
					perror(filename);
					exit(1);
				}
				block = 0;
				printf("File: %s\n", filename);
			} else if (!f || buf[0] != 0x00 ||
				   (buf[1] + (buf[2] << 8)) != block) {
				printf("Bad block ID: %02x %02x %02x\n",
					buf[0], buf[1], buf[2]);
				err = 1;
			} else {
				block++;
				fwrite(buf+3, 1, 253, f);
			}
		} else {
			printf("Checksum error: got %04x data %04x\n",
				csum2, csum1);
			err = 1;
		}
	}

	if (err && f) {
		fclose(f);
		remove(filename);
		f = NULL;
	}
}

static void output_bit(unsigned int bit)
{
	enum state {
		st_leader,
		st_sync,
		st_stx,
		st_data,
		st_etx,
		st_csum
	};
	static enum state st = st_leader;
	static unsigned int ctr = 0;
	static unsigned int bits = 0;
	static uint8_t data = 0;
	static uint8_t buf[256];
	static uint8_t csum[2];

	data = (data >> 1) + (bit << 7); /* Bigendian bit order... */
	bits++;

	switch (st) {
	case st_leader:
		ctr++;
		if (bit == 1)
			ctr = 0;
		if (ctr > 128)	/* After 128 zeroes start looking for sync */
			st = st_sync;
		break;

	case st_sync:
		if (data == 0x16) {
			st = st_stx; /* Sync acquired */
			bits = 0;
		}
		break;

	case st_stx:
		if (bits < 8)
			break;
		bits = 0;
		ctr = 0;
		if (data == 0x02) {
			st = st_data;
		} else if (data == 0x16) {
			/* Got another SYNC */
		} else {
			printf("Got %02x when expecting SYNC or STX\n", data);
			output_block(NULL, NULL);
			st = st_leader; /* ERROR */
		}
		break;

	case st_data:
		if (bits < 8)
			break;
		bits = 0;
		buf[ctr++] = data;
		if (ctr == 256)
			st = st_etx;
		break;

	case st_etx:
		if (bits < 8)
			break;
		bits = 0;
		ctr = 0;
		if (data == 0x03)
			st = st_csum;
		else {
			printf("Got %02x when expecting ETX\n", data);
			output_block(NULL, NULL);
			st = st_leader; /* ERROR */
		}
		break;

	case st_csum:
		if (bits < 8)
			break;
		bits = 0;
		csum[ctr++] = data;
		if (ctr == 2) {
			output_block(buf, csum);
			ctr = 0;
			st = st_leader;
		}
		break;
	}
}

static void process_flank(unsigned int time)
{
	static unsigned int since_start = 0;
	static unsigned int captured = 0;

	since_start += time;

	if (since_start < xtime/4) {
		printf("Quick flank: %u:%u\n", since_start, xtime);
	} else if (since_start < xtime*3/4) {
		captured = 1;
	} else if (since_start < xtime*5/4) {
		output_bit(captured);
		since_start = captured = 0;
	} else {
		printf("Slow flank: %u:%u\n", since_start, xtime);
		since_start = 0;
	}
}

int main(int argc, char *argv[])
{
	unsigned int frequency;
	FILE *f;
	int16_t samp, osamp;
	double v, k, cutoff, thresh;
	unsigned int state;
	unsigned int time, ptime;

	setlocale(LC_ALL, "");

	f = fopen(argv[1], "rb");
	
	frequency = (argc > 2) ? strtoul(argv[2], NULL, 0) : 44100;
	thresh = (argc > 3) ? atof(argv[3]) : 5300.0;
	cutoff = (argc > 4) ? atof(argv[4]) : 0.0;
	
	k = exp(-cutoff/frequency);
	xtime = frequency/700;	/* Samples per baud */

	state = time = ptime = 0;
	samp = osamp = 0;
	v = 0.0;

	while (fread(&samp, 1, 2, f) == 2) {
		/* Emulate high pass filter in ABC80 */
		v = (v * k) + (samp - osamp);
		osamp = samp;

		if (v >= thresh) {
			if (!state) {
				state = 1;
				process_flank(time - ptime);
				ptime = time;
			}
		} else if (v <= -thresh) {
			if (state) {
				state = 0;
				process_flank(time - ptime);
				ptime = time;
			}
		}
		time++;
	}

	fclose(f);

	return 0;
}