forked from dhansel/Altair8800
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_teensy_tape.h
380 lines (344 loc) · 9.72 KB
/
host_teensy_tape.h
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// -----------------------------------------------------------------------------
// Altair 8800 Simulator
// Teensy Audio (Tape) interface
// Copyright (C) 2020 Dirk Herrendoerfer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------
// Audio Tape decoding and input - Simulates a serial port
IntervalTimer tapeTimer;
// Tape signal detect
static volatile uint16_t ain_last = 0;
static volatile uint16_t ain_current = 0;
static volatile uint32_t last_rise_millis = 0;
static volatile uint16_t rise_wave_period = 0;
static volatile uint8_t low_repeat = 0;
static volatile uint8_t high_repeat = 0;
// Tape-Serial char Buffer
static volatile uint8_t a_serial_buffer[8];
static volatile uint8_t a_serial_floor = 0;
static volatile uint8_t a_serial_ceil = 0;
// Tape-Serial decoder
static volatile uint8_t a_lastlevel = 0;
static volatile uint8_t a_level = 0;
static volatile uint8_t a_char = 0;
static volatile uint8_t a_state = 0;
static volatile uint32_t a_serial_state_start;// __attribute__ ((alligned(8)));
// Idle detect
static volatile uint16_t a_idle = 20001;
// Stats
static volatile uint32_t a_stat_read = 0;
static volatile uint32_t a_stat_timeout_err = 0;
static volatile uint32_t a_stat_frame_err = 0;
#define BITLEN 3333
#define BITVALID 900
#define TONE_REPEATS 2
#define MINSIGNAL 32
// Tape send (Yes I know this can be done a lot simpler!!!)
// For 1850Hz we use the entire wave, for 2400 we skip #2 and #8 from the waveform
static const int16_t a_waveform[] = {5,10,15,10,5,0,-5,-10,-15,-10,-5};
static volatile int8_t a_waveform_index = 0;
static volatile int8_t a_current_tone = 0;
static volatile int8_t a_send_index = 0;
static volatile int8_t a_send_char = '+';
static volatile uint32_t a_serial_send_start;
static volatile uint8_t a_send_idle = 0; //Idle has three states 2:tone off, 1:lead in, 0: data
static volatile uint32_t a_send_idle_counter = 80000;
static volatile uint8_t a_serial_write_buffer[8];
static volatile uint8_t a_serial_write_floor = 0 ;
static volatile uint8_t a_serial_write_ceil = 0;
static volatile uint8_t a_sending = 0;
// Virtual serial port code
//
static uint8_t a_serial_write_avail()
{
if (a_serial_write_ceil < a_serial_write_floor)
return(a_serial_write_floor - a_serial_write_ceil -1);
else
return(7 - a_serial_write_ceil + a_serial_write_floor);
}
static uint8_t a_serial_write(uint8_t a_char)
{
if (a_serial_write_avail() == 0)
return 0;
a_serial_write_buffer[a_serial_write_ceil++] = a_char;
a_serial_write_ceil &= 0x7;
return 1;
}
static void a_serial_store(uint8_t a_char)
{
a_serial_buffer[a_serial_ceil++] = a_char;
a_serial_ceil &= 0x7;
}
static uint8_t a_serial_available()
{
if (a_serial_ceil < a_serial_floor)
return(8 - a_serial_floor + a_serial_ceil);
else
return(a_serial_ceil - a_serial_floor);
}
static uint8_t a_serial_read()
{
if (a_serial_ceil != a_serial_floor) {
uint8_t ret;
ret = a_serial_buffer[a_serial_floor++];
a_serial_floor &= 0x7;
return ret;
}
return 0;
}
volatile uint8_t det = 0;
// Tape encoder/decoder timer interrupt routine
//
void tapeUpdate()
{
uint32_t utime;
ain_last=ain_current;
// ain_current = (ain_last + analogRead(A22) )/ 2;
ain_current = analogRead(A22);
utime=micros();
// Idle detection
if (!a_sending && ain_current < 512+MINSIGNAL && ain_current > 512-MINSIGNAL) {
if(a_idle > 20000){
tapeTimer.update(2000000);
}
else
a_idle++;
}
else {
if(a_idle > 20000){
tapeTimer.update(50);
}
a_idle = 0;
}
//detect every rising edge
if (ain_current < 512-MINSIGNAL*2)
det=0;
if (det == 0 && ain_current > 512) {
rise_wave_period = utime - last_rise_millis;
last_rise_millis = utime;
det=1;
}
else {
goto decode;
}
//Decoder: Frequencies
//There are 2 tones: 1850Hz and 2400Hz (541ms and 417ms period time)
if (rise_wave_period >= 350) {
//Valid Tone
if (rise_wave_period >= 500 && rise_wave_period < 650){
//Low Tone
low_repeat++;
high_repeat=0;
} else {
//High
high_repeat++;
low_repeat=0;
}
}
if (low_repeat == TONE_REPEATS ) {
a_level = 1;
}
else if (high_repeat == TONE_REPEATS ) {
a_level = 0;
}
decode:
// Hardcoded 300 baud 8N1 decoding
uint32_t mtime;
mtime = utime - a_serial_state_start;
//Find startbit
if (a_state == 0) {
if (a_lastlevel == 0 && a_level == 1) {
a_serial_state_start = utime;
a_state=1;
}
}
else if (a_state > 0 && mtime > 10*BITLEN ) {
//Timeout/invalid we really should never get here
if (a_state < 10)
a_stat_timeout_err++;
a_state = 0;
a_char = 0;
}
else if (a_state == 10 && mtime < 10*BITLEN ) {
//Time's up either we've got a char, or we didn't
a_state = 0;
a_char = 0;
}
else if (a_state == 9 && mtime > 9*BITLEN + BITVALID) {
//Stop bit (we test for that a couple of times)
if (a_level == 0) {
//store achar
a_serial_store(a_char);
//Serial.print((char)a_char);
a_stat_read++;
}
else {
//Stop Bit Error
a_stat_frame_err++;
}
a_state = 10;
}
else if (a_state < 9 && mtime > 8*BITLEN + BITVALID) {
//7 bit
if (a_level == 0) {
a_char |= 1<<7;
}
a_state = 9;
}
else if (a_state < 8 && mtime > 7*BITLEN + BITVALID) {
//6 bit
if (a_level == 0) {
a_char |= 1<<6;
}
a_state = 8;
}
else if (a_state < 7 && mtime > 6*BITLEN + BITVALID) {
//5 bit
if (a_level == 0) {
a_char |= 1<<5;
}
a_state = 7;
}
else if (a_state < 6 && mtime > 5*BITLEN + BITVALID) {
//4 bit
if (a_level == 0) {
a_char |= 1<<4;
}
a_state = 6;
}
else if (a_state < 5 && mtime > 4*BITLEN + BITVALID) {
//3 bit
if (a_level == 0) {
a_char |= 1<<3;
}
a_state = 5;
}
else if (a_state < 4 && mtime > 3*BITLEN + BITVALID) {
//2 bit
if (a_level == 0) {
a_char |= 1<<2;
}
a_state = 4;
}
else if (a_state < 3 && mtime > 2*BITLEN + BITVALID) {
//1 bit
if (a_level == 0) {
a_char |= 1<<1;
}
a_state = 3;
}
else if (a_state < 2 && mtime > 1*BITLEN + BITVALID) {
//0 bit
if (a_level == 0) {
a_char |= 1<<0;
}
a_state = 2;
}
a_lastlevel = a_level;
//---------------------------
//Send Part of the IRQ
if (a_sending) {
if (a_send_idle == 0) {
if (micros() > a_serial_send_start + BITLEN) {
if (a_send_index == 10) {
//restart (next char)
a_send_index = 0;
if (a_serial_write_ceil != a_serial_write_floor) {
a_send_char = a_serial_write_buffer[a_serial_write_floor++];
a_serial_write_floor &= 0x7;
}
else {
//no next char - go to idle timeout
a_send_idle = 2;
}
}
if (a_send_index == 9) {
//Stop_bit
a_current_tone = 1;
}
else if (a_send_index == 0) {
//Start_bit
a_current_tone = 0;
}
else {
//data
if (a_send_char & 1<<(a_send_index - 1))
a_current_tone = 1;
else
a_current_tone = 0;
}
a_send_index++;
a_serial_send_start = micros();
}
}
else if (a_send_idle == 1) {
// doing the lead-in tone
a_current_tone = 1;
a_send_idle_counter--;
if (a_send_idle_counter == 0)
a_send_idle=0;
}
else if (a_send_idle == 2) {
// doing the lead-out tone
a_current_tone = 1;
a_send_idle_counter++;
// Go back to sending if there is a char in the buffer
if (a_serial_write_ceil != a_serial_write_floor) {
a_send_char = a_serial_write_buffer[a_serial_write_floor++];
a_serial_write_floor &= 0x7;
a_send_idle_counter = 0;
a_send_idle = 0;
}
// If we time out, go to full idle mode
if (a_send_idle_counter == 80000)
a_send_idle=3;
}
// If we are not full idle then generate tones
if (a_send_idle != 3) {
//Do the waveform generation
if (a_waveform_index >= 11) {
a_waveform_index = 0;
}
else {
if (a_current_tone == 1) {
//For the high frequency waveform, we skip 3 elements
if(a_waveform_index == 2 || a_waveform_index == 5 || a_waveform_index == 8)
a_waveform_index++;
}
}
analogWrite(A21,2048+(a_waveform[a_waveform_index++] * 150));
}
else {
//Done with sending leave sending mode
a_sending = 0;
a_idle = 0;
analogWrite(A21,2048);
}
}
else {
//Not sending, if there is a new char in the buffer, get it, start sending
if (a_serial_write_ceil != a_serial_write_floor) {
a_send_char = a_serial_write_buffer[a_serial_write_floor++];
a_serial_write_floor &= 0x7;
a_send_index = 0;
a_sending = 1;
a_send_idle = 1;
a_send_idle_counter = 80000;
tapeTimer.update(50);
}
}
}