-
Notifications
You must be signed in to change notification settings - Fork 17
/
trs_stringy.c
583 lines (513 loc) · 14.2 KB
/
trs_stringy.c
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
* Copyright (c) 2011-2020, Timothy P. Mann
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Emulate Exatron stringy floppy.
*
* Still needs some cleanup; see XXX comments below.
*
* XXX Check if I am exactly duplicating TRS32 output now. However,
* TRS32 seems to drop one bit on wrap, which might be a bug in
* TRS32 that I don't need to duplicate.
*/
#define _XOPEN_SOURCE 500 /* string.h: strdup() */
#include "z80.h"
#include "trs.h"
#include "trs_disk.h"
#include "trs_stringy.h"
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <assert.h>
#define STRINGYDEBUG_IN 0
#define STRINGYDEBUG_OUT 0
#define STRINGYDEBUG_STATE 0
/* Input port bits */
#define STRINGY_WRITE_PROT 0x01
#define STRINGY_END_OF_TAPE 0x04
#define STRINGY_NO_WAFER 0x08 /* is that what this bit means? */
#define STRINGY_FLUX 0x80
#define STRINGY_NO_UNIT 0xff
/* Output port bits */
#define STRINGY_MOTOR_ON 0x01
#define STRINGY_WRITE_GATE 0x04
/*#define STRINGY_FLUX 0x80*/
#define STRINGY_MAX_UNITS 8
typedef struct {
char *name;
FILE *file;
stringy_pos_t length;
stringy_pos_t eotWidth;
stringy_pos_t pos;
tstate_t pos_time;
stringy_pos_t flux_change_pos;
int flux_change_to;
Uchar in_port;
Uchar out_port;
Uchar format;
// for esf format:
long esf_bytelen;
long esf_bytepos;
Uchar esf_bytebuf;
Uchar esf_bitpos;
#if STRINGYDEBUG_IN
int prev_in_port;
#endif
} stringy_info_t;
stringy_info_t stringy_info[STRINGY_MAX_UNITS];
#define STRINGY_STOPPED 0
#define STRINGY_READING 1
#define STRINGY_WRITING 2
static int
stringy_state(int out_port)
{
if ((out_port & STRINGY_MOTOR_ON) == 0) return STRINGY_STOPPED;
if ((out_port & STRINGY_WRITE_GATE) != 0) return STRINGY_WRITING;
return STRINGY_READING;
}
/*
* Create a blank virtual stringy floppy wafer with default parameters.
* Returns 0 if OK, errno value otherwise.
*/
int
stringy_create(const char *name)
{
FILE *f;
int ires;
f = fopen(name, "w");
if (f == NULL) {
return errno;
}
/*
* Default parameters
*/
ires = stringy_init_with(f,
STRINGY_FMT_DEFAULT,
STRINGY_LEN_DEFAULT,
STRINGY_EOT_DEFAULT,
FALSE);
if (ires < 0) return errno;
ires = fclose(f);
if (ires < 0) return errno;
return 0;
}
static int
stringy_read_esf_header(stringy_info_t *s)
{
char magic[sizeof(stringy_esf_magic)];
Uchar headerLength, flags;
Ushort eotWidth;
size_t sres;
int ires;
Uint len;
rewind(s->file);
sres = fread(magic, sizeof(stringy_esf_magic), 1, s->file);
if (sres < 1) return -1;
if (memcmp(magic, stringy_esf_magic,
sizeof(stringy_esf_magic)) != 0) return -1;
ires = fgetc(s->file);
if (ires < 0) return ires;
headerLength = ires;
if (headerLength != stringy_esf_header_length) return -1;
ires = fgetc(s->file);
if (ires < 0) return ires;
flags = ires;
ires = get_twobyte(&eotWidth, s->file);
if (ires < 0) return ires;
ires = get_fourbyte(&len, s->file);
if (ires < 0) return ires;
s->format = STRINGY_FMT_ESF;
s->length = len * STRINGY_CELL_WIDTH * 8;
s->eotWidth = eotWidth * STRINGY_CELL_WIDTH;
s->in_port = (s->in_port & ~STRINGY_WRITE_PROT) |
((flags & stringy_esf_write_protected) ? STRINGY_WRITE_PROT : 0);
fseek(s->file, 0, SEEK_CUR);
s->esf_bytelen = len;
s->esf_bytepos = 0;
s->esf_bytebuf = 0;
s->esf_bitpos = 0;
return 0;
}
static int
stringy_read_debug_header(stringy_info_t *s)
{
int ires;
stringy_pos_t len, eotw;
int wprot;
rewind(s->file);
ires = fscanf(s->file, stringy_debug_header, &len, &eotw, &wprot);
if (ires < 3) return -1;
s->format = STRINGY_FMT_DEBUG;
s->length = len;
s->eotWidth = eotw;
s->in_port = (s->in_port & ~STRINGY_WRITE_PROT) |
(wprot ? STRINGY_WRITE_PROT : 0);
fseek(s->file, 0, SEEK_CUR);
return 0;
}
static int
stringy_read_header(stringy_info_t *s)
{
int ires;
ires = stringy_read_esf_header(s);
if (ires >= 0) return ires;
ires = stringy_read_debug_header(s);
return ires;
}
/* Returns 0 if OK, -1 if invalid header, errno value otherwise. */
static int
stringy_change(int unit)
{
stringy_info_t *s = &stringy_info[unit];
int ires;
if (s->file) {
fclose(s->file);
s->file = NULL;
}
if (s->name == NULL) {
s->in_port = STRINGY_NO_WAFER;
return 0;
}
s->file = fopen(s->name, "r+");
if (s->file == NULL) {
if (errno == EACCES || errno == EROFS) {
s->file = fopen(s->name, "r");
}
if (s->file == NULL) {
s->in_port = STRINGY_NO_WAFER;
return errno;
}
s->in_port = STRINGY_WRITE_PROT;
} else {
s->in_port = 0;
}
s->out_port = 0;
ires = stringy_read_header(s);
s->pos = 0;
s->pos_time = z80_state.t_count;
//XXX is the following right?
s->flux_change_pos = 0;
s->flux_change_to = 1;
return ires;
}
void
stringy_change_all(void)
{
int i;
for (i = 0; i < STRINGY_MAX_UNITS; i++) {
stringy_change(i);
}
}
const char *
stringy_get_name(int drive)
{
return stringy_info[drive].name;
}
/* Returns 0 if OK, -1 if invalid header, errno value otherwise. */
int
stringy_set_name(int drive, const char *name)
{
stringy_info_t *s = &stringy_info[drive];
if (s->name) free(s->name);
s->name = name ? strdup(name) : NULL;
return stringy_change(drive);
}
/* One-time initialization */
void
stringy_init(void)
{
int i;
for (i = 0; i < STRINGY_MAX_UNITS; i++) {
stringy_info[i].name = (char *) malloc(strlen(trs_disk_dir) + 13);
/*
* Stringy really only works for Model I, but I'm keeping the
* generality for now anyway.
*/
if (trs_model == 5) {
sprintf(stringy_info[i].name, "%s/stringy4p-%d", trs_disk_dir, i);
} else {
sprintf(stringy_info[i].name, "%s/stringy%d-%d",
trs_disk_dir, trs_model, i);
}
}
}
/* Stringy controller hardware reset */
void
stringy_reset(void)
{
/* Nothing to do (?) */
}
static void
stringy_byte_flush(stringy_info_t *s)
{
int ires;
Uchar mask;
if (s->format != STRINGY_FMT_ESF ||
stringy_state(s->out_port) != STRINGY_WRITING ||
s->esf_bitpos == 0) return;
fseek(s->file, 0, SEEK_CUR);
ires = fgetc(s->file);
if (ires == EOF) {
ires = 0;
}
fseek(s->file, -1, SEEK_CUR);
mask = 0xff << s->esf_bitpos;
s->esf_bytebuf = (ires & mask) | (s->esf_bytebuf & ~mask);
fputc(s->esf_bytebuf, s->file); //XXX handle errors
fseek(s->file, -1, SEEK_CUR);
}
static void
stringy_bit_write(stringy_info_t *s, int flux)
{
s->esf_bytebuf &= ~(1 << s->esf_bitpos);
s->esf_bytebuf |= flux << s->esf_bitpos;
s->esf_bitpos++;
if (s->esf_bitpos == 8) {
fputc(s->esf_bytebuf, s->file); //XXX handle errors
if (++s->esf_bytepos >= s->esf_bytelen) {
fseek(s->file, stringy_esf_header_length, SEEK_SET);
s->esf_bytepos = 0;
}
s->esf_bitpos = 0;
s->esf_bytebuf = 0;
}
}
/*
* flux = new flux state
* delta = time in *previous* state
*/
static void
stringy_flux_write(stringy_info_t *s, int flux, stringy_pos_t delta)
{
int cells;
stringy_pos_t adjustment;
switch (s->format) {
case STRINGY_FMT_DEBUG:
fprintf(s->file, "%u %lu\n", flux, delta);
break;
case STRINGY_FMT_ESF:
cells = (delta + 1) / STRINGY_CELL_WIDTH;
if (cells > 3) {
/*
* XXX Why is this needed? Otherwise we get a huge run of cells
* right at the start, and some later too -- see .debug format
* output. Did the real hardware/firmware leave gaps there
* (maybe for motor startup?), or is this a bug in xtrs
* emulation? TRS32 shows no such gaps in its output format, so
* we avoid them too by crushing them out here.
*/
cells = 3;
}
adjustment = delta - cells * STRINGY_CELL_WIDTH;
while (cells--) {
stringy_bit_write(s, flux);
}
/*
* Adjust the position to exactly match the nominal size of the
* number of cells written. This is necessary so that the end of
* tape mark will be seen at exactly the same place when reading
* the tape back as it was seen when writing the tape out.
*/
s->pos -= adjustment;
break;
}
}
static int
stringy_bit_read(stringy_info_t *s, int *bit)
{
int ires;
if (s->esf_bitpos == 0) {
if (s->esf_bytepos++ >= s->esf_bytelen) {
fseek(s->file, stringy_esf_header_length, SEEK_SET);
s->esf_bytepos = 0;
}
ires = fgetc(s->file); //XXX handle errors (but do ignore EOF)
if (ires < 0) {
ires = 0;
}
s->esf_bytebuf = ires;
}
*bit = (s->esf_bytebuf & (1 << s->esf_bitpos)) != 0;
s->esf_bitpos = (s->esf_bitpos + 1) % 8;
return TRUE;
}
static int
stringy_flux_read(stringy_info_t *s, int *flux, stringy_pos_t *delta)
{
int bres;
int bit;
switch(s->format) {
case STRINGY_FMT_DEBUG:
bres = fscanf(s->file, "%u %lu\n", flux, delta);
if (bres == EOF) {
if (ferror(s->file)) return FALSE;
stringy_read_debug_header(s);
bres = fscanf(s->file, "%u %lu\n", flux, delta);
if (bres == EOF && ferror(s->file)) return FALSE;
}
return TRUE;
case STRINGY_FMT_ESF:
bres = stringy_bit_read(s, &bit);
if (!bres) return bres;
/*
* This calls for some explanation. Our caller wants the delta to
* the next flux change and the resulting flux value, as in "xtrs
* stringy debug" format. We don't really know either; we only
* know the current flux value and that it will continue for
* STRINGY_CELL_WIDTH. So we fib and say that the value will
* change to the opposite of the current value after
* STRINGY_CELL_WIDTH. This is actually good enough for our
* caller, which will request enough additional samples to get
* correct information before using the information.
*/
*flux = !bit;
*delta = STRINGY_CELL_WIDTH;
return TRUE;
}
return FALSE;
}
static void
stringy_update_pos(stringy_info_t *s)
{
if (stringy_state(s->out_port) == STRINGY_STOPPED) {
return;
}
s->pos += z80_state.t_count - s->pos_time;
s->pos_time = z80_state.t_count;
if (s->pos >= s->length) {
s->pos -= s->length;
s->flux_change_pos -= s->length;
}
if (s->pos >= s->length - s->eotWidth) {
s->in_port |= STRINGY_END_OF_TAPE;
} else {
s->in_port &= ~STRINGY_END_OF_TAPE;
}
}
int
stringy_in(int unit)
{
stringy_info_t *s = &stringy_info[unit];
int ret;
if (s->in_port & STRINGY_NO_WAFER) {
ret = s->in_port;
goto done;
}
stringy_update_pos(s);
if (stringy_state(s->out_port) == STRINGY_READING) {
while (s->pos >= s->flux_change_pos) {
int flux;
stringy_pos_t delta;
s->in_port = (s->in_port & ~STRINGY_FLUX) |
(s->flux_change_to ? 0 : STRINGY_FLUX);
if (!stringy_flux_read(s, &flux, &delta)) {
break;
}
s->flux_change_to = flux;
s->flux_change_pos += delta;
}
}
ret = s->in_port;
done:
#if STRINGYDEBUG_IN
if (ret != s->prev_in_port) {
debug("stringy_in(%d) -> %d\n", unit, ret);
s->prev_in_port = ret;
}
#endif
return ret;
}
void
stringy_out(int unit, int value)
{
stringy_info_t *s = &stringy_info[unit];
int old_state, new_state;
int res;
if (s->in_port & STRINGY_NO_WAFER) return;
old_state = stringy_state(s->out_port);
new_state = stringy_state(value);
stringy_update_pos(s);
#if STRINGYDEBUG_STATE
if (old_state != new_state) {
debug("stringy state %d -> %d, new pos %ld\n",
old_state, new_state, s->pos);
}
#endif
if (old_state == STRINGY_STOPPED &&
new_state != STRINGY_STOPPED) {
if (1 /*s->in_port & STRINGY_END_OF_TAPE*/) {
/*
* Start at the beginning after motor off/on. This surely
* doesn't emulate real hardware accurately, but I get "tape
* too short" errors on @SAVE unless I do it at least in the
* case where STRINGY_END_OF_TAPE is currently set. Maybe the
* ROM does its "rewind" thing, shuts off the motor, and relies
* on tape coasting past the EOT marker during the off/on
* transition. XXX Read ROM code to try to understand this. As
* long as I'm doing this sometimes, doing it always is no
* greater compromise. And doing it always makes the state
* change positions more reproducible, which is better for
* regression testing and such.
*
*/
s->pos = 0;
s->in_port &= ~STRINGY_END_OF_TAPE;
stringy_read_header(s);
}
s->pos_time = z80_state.t_count;
// XXX Is the following right?
s->in_port &= ~STRINGY_FLUX;
s->flux_change_pos = s->pos;
s->flux_change_to = 1;
}
if (old_state != STRINGY_WRITING &&
new_state == STRINGY_WRITING) {
if (s->format == STRINGY_FMT_DEBUG) {
// Debug format can't handle overwriting
fflush(s->file);
res = ftruncate(fileno(s->file), ftell(s->file));
assert(res == 0);
}
fseek(s->file, 0, SEEK_CUR);
stringy_flux_write(s, 1, 0); //XXX needed? bad?
}
if (old_state == STRINGY_WRITING) {
if (((s->out_port ^ value) & STRINGY_FLUX) != 0 ||
new_state != STRINGY_WRITING) {
stringy_flux_write(s, (value & STRINGY_FLUX) != 0,
s->pos - s->flux_change_pos);
s->flux_change_pos = s->pos;
}
if (new_state != STRINGY_WRITING) {
stringy_byte_flush(s);
fflush(s->file);
}
}
s->out_port = value;
#if STRINGYDEBUG_OUT
debug("stringy_out(%d, %d)\n", unit, value);
s->prev_in_port = -1;
#endif
}