-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcr3447.c
More file actions
669 lines (599 loc) · 16.6 KB
/
cr3447.c
File metadata and controls
669 lines (599 loc) · 16.6 KB
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*--------------------------------------------------------------------------
**
** Copyright (c) 2003-2004, Paul Koning, Tom Hunter (see license.txt)
**
** Name: cr3447.c
**
** Description:
** Perform simulation of CDC 3447 card reader controller.
**
**--------------------------------------------------------------------------
*/
/*
** -------------
** Include Files
** -------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "const.h"
#include "types.h"
#include "proto.h"
/*
** -----------------
** Private Constants
** -----------------
*/
/*
** CDC 3447 card reader function and status codes.
*/
#define FcCr3447Deselect 00000
#define FcCr3447Binary 00001
#define FcCr3447BCD 00002
#define FcCr3447SetGateCard 00004
#define FcCr3447Clear 00005
#define FcCr3447IntReady 00020
#define FcCr3447NoIntReady 00021
#define FcCr3447IntEoi 00022
#define FcCr3447NoIntEoi 00023
#define FcCr3447IntError 00024
#define FcCr3447NoIntError 00025
#define Fc6681DevStatusReq 01300
#define Fc6681InputToEor 01400
#define Fc6681Input 01500
/*
** Status reply flags
**
** 0001 = Ready
** 0002 = Busy
** 0004 = Binary card (7/9 punch)
** 0010 = File card (7/8 punch)
** 0020 = Jam
** 0040 = Input tray empty
** 0100 = End of file
** 0200 = Ready interrupt
** 0400 = EOI interrupt
** 1000 = Error interrupt
** 2000 = Read compare error
** 4000 = Reserved by other controller (3649 only)
**
*/
#define StCr3447Ready 00201 /* includes ReadyInt */
#define StCr3447Busy 00002
#define StCr3447Binary 00004
#define StCr3447File 00010
#define StCr3447Empty 00040
#define StCr3447Eof 01540 /* includes Empty, EoiInt, ErrorInt */
#define StCr3447ReadyInt 00200
#define StCr3447EoiInt 00400
#define StCr3447ErrorInt 01000
#define StCr3447CompareErr 02000
#define StCr3447NonIntStatus 02177
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
typedef struct deckname_s
{
struct deckname_s *next;
char *name;
} DeckName;
typedef struct
{
void *statusBuf;
DeckName *waiting_decks;
DeckName *last_deck;
int intmask;
int status;
int col;
const u16 *table;
u32 getcardcycle;
char card[82];
bool binary;
bool bincard;
} CrContext;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static FcStatus cr3447Func(PpWord funcCode);
static void cr3447Io(void);
static void cr3447Load(DevSlot *, int, char *);
static void cr3447Activate(void);
static void cr3447Disconnect(void);
static void cr3447NextCard (DevSlot *up, CrContext *cc);
static void cr3447NextDeck (DevSlot *up, CrContext *cc);
/*
** ----------------
** Public Variables
** ----------------
*/
/*
** -----------------
** Private Variables
** -----------------
*/
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise card reader.
**
** Parameters: Name Description.
** eqNo equipment number
** unitCount number of units to initialise
** channelNo channel number the device is attached to
** deviceName optional card source file name,
** may be followed by comma and"026" (default)
** or "029" to select translation mode
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cr3447Init(u8 eqNo, u8 unitNo, u8 channelNo, char *deviceName)
{
DevSlot *up;
CrContext *cc;
FILE *fcb;
char *opt;
up = dcc6681Attach(channelNo, eqNo, 0, DtCr3447);
up->activate = cr3447Activate;
up->disconnect = cr3447Disconnect;
up->func = cr3447Func;
up->io = cr3447Io;
up->load = cr3447Load;
/*
** Only one card reader unit is possible per equipment.
*/
if (up->context[0] != NULL)
{
fprintf (stderr, "Only one CR3447 unit is possible per equipment\n");
exit (1);
}
cc = calloc (1, sizeof (CrContext));
if (cc == NULL)
{
fprintf (stderr, "Failed to allocate CR3447 context block\n");
exit (1);
}
up->context[0] = cc;
if (deviceName != NULL)
{
opt = strchr (deviceName, ',');
if (opt != NULL)
{
*opt++ = '\0';
}
fcb = fopen(deviceName, "r");
if (fcb == NULL)
{
fprintf(stderr, "Failed to open %s\n", deviceName);
exit(1);
}
up->fcb[0] = fcb;
cc->status = StCr3447Ready;
cr3447NextCard (up, cc); /* Read the first card */
}
else
{
opt = NULL;
up->fcb[0] = NULL;
cc->status = StCr3447Eof;
}
cc->table = asciiTo026; /* default translation table */
if (opt != NULL)
{
if (strcmp (opt, "029") == 0)
{
cc->table = asciiTo029;
}
else if (strcmp (opt, "026") != 0)
{
fprintf (stderr, "Unrecognized card code name %s\n", opt);
exit (1);
}
}
else
{
opt = "026";
}
/*
** Allocate the operator status buffer
*/
cc->statusBuf = opInitStatus ("CR3447", channelNo, eqNo);
/*
** Print a friendly message.
*/
printf("CR3447 initialised on channel %o equipment %o, default code %s\n",
channelNo, eqNo, opt);
}
/*--------------------------------------------------------------------------
** Purpose: Execute function code on 3447 card reader.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus cr3447Func(PpWord funcCode)
{
CrContext *cc;
FcStatus st;
cc = (CrContext *)active3000Device->context[0];
switch (funcCode)
{
default: /* all unrecognized codes are NOPs */
st = FcProcessed;
break;
case FcCr3447SetGateCard:
st = FcProcessed;
break;
case Fc6681InputToEor:
case Fc6681Input:
cc->status = StCr3447Ready;
/* fall through */
case Fc6681DevStatusReq:
st = FcAccepted;
active3000Device->fcode = funcCode;
break;
case FcCr3447Binary:
cc->binary = TRUE;
st = FcProcessed;
break;
case FcCr3447Deselect:
case FcCr3447Clear:
cc->intmask = 0;
/* fall through */
case FcCr3447BCD:
cc->binary = FALSE;
st = FcProcessed;
break;
case FcCr3447IntReady:
cc->intmask |= StCr3447ReadyInt;
cc->status &= ~StCr3447ReadyInt;
st = FcProcessed;
break;
case FcCr3447NoIntReady:
cc->intmask &= ~StCr3447ReadyInt;
cc->status &= ~StCr3447ReadyInt;
st = FcProcessed;
break;
case FcCr3447IntEoi:
cc->intmask |= StCr3447EoiInt;
cc->status &= ~StCr3447EoiInt;
st = FcProcessed;
break;
case FcCr3447NoIntEoi:
cc->intmask &= ~StCr3447EoiInt;
cc->status &= ~StCr3447EoiInt;
st = FcProcessed;
break;
case FcCr3447IntError:
cc->intmask |=StCr3447ErrorInt;
cc->status &= ~StCr3447ErrorInt;
st = FcProcessed;
break;
case FcCr3447NoIntError:
cc->intmask &= ~StCr3447ErrorInt;
cc->status &= ~StCr3447ErrorInt;
st = FcProcessed;
break;
}
dcc6681Interrupt((cc->status & cc->intmask) != 0);
return(st);
}
/*--------------------------------------------------------------------------
** Purpose: Perform I/O on 3447 card reader.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void cr3447Io(void)
{
CrContext *cc;
int c;
PpWord p;
cc = (CrContext *)active3000Device->context[0];
switch (active3000Device->fcode)
{
default:
printf("unexpected IO for function %04o\n", active3000Device->fcode);
break;
case 0:
break;
case Fc6681DevStatusReq:
if (!activeChannel->full)
{
activeChannel->data = (cc->status & (cc->intmask | StCr3447NonIntStatus));
activeChannel->full = TRUE;
}
break;
case Fc6681InputToEor:
case Fc6681Input:
/*
** Don't admit to having new data immediately after completing
** a card, otherwise 1CD may get stuck occasionally.
** So we simulate card in motion for 20 major cycles.
*/
if ( activeChannel->full
|| cycles - cc->getcardcycle < 20)
{
break;
}
if (active3000Device->fcb[0] == NULL)
{
cc->status = StCr3447Eof;
break;
}
if (cc->col >= 80)
{
/*
** Read the next card.
** If the function is input to EOR, disconnect to indicate EOR.
*/
cr3447NextCard (active3000Device, cc);
if (activeDevice->fcode == Fc6681InputToEor)
{
/* End of card but we're still ready */
cc->status |= StCr3447EoiInt | StCr3447Ready;
if (cc->status & StCr3447File)
cc->status |= StCr3447ErrorInt;
activeChannel->discAfterInput = TRUE;
}
}
else
{
c = cc->card[cc->col++];
if (cc->binary || cc->bincard)
{
p = cc->table[c];
}
else
{
p = asciiToBcd[c] << 6;
c = cc->card[cc->col++];
p += asciiToBcd[c];
}
activeChannel->data = p;
activeChannel->full = TRUE;
}
break;
}
dcc6681Interrupt((cc->status & cc->intmask) != 0);
}
/*--------------------------------------------------------------------------
** Purpose: Perform load/unload on 3447 card reader.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void cr3447Load(DevSlot *up, int eqNo, char *fn)
{
CrContext *cc;
char *fn2;
DeckName *d, *d2;
FILE *fcb;
static char msgBuf[80];
cc = (CrContext *) (up->context[0]);
if (fn != NULL)
{
fcb = fopen(fn, "r");
if (fcb == NULL)
{
sprintf (msgBuf, "$Open error: %s", strerror (errno));
opSetMsg(msgBuf);
return;
}
fclose (fcb);
fn2 = strdup (fn);
if (fn2 == NULL)
{
opSetMsg ("$malloc failure");
return;
}
d = (DeckName *) malloc (sizeof (*d));
if (fn2 == NULL)
{
free (fn2);
opSetMsg ("$malloc failure");
return;
}
d->next = NULL;
d->name = fn2;
if (cc->waiting_decks == NULL)
{
/* This is the first deck */
cc->waiting_decks = cc->last_deck = d;
}
else
{
cc->last_deck->next = d;
cc->last_deck = d;
}
if (up->fcb[0] == NULL)
{
cr3447NextDeck (up, cc);
}
sprintf (msgBuf, "CR3447 loaded with %s", fn);
opSetMsg (msgBuf);
}
else
{
if (up->fcb[0] != NULL)
fclose (up->fcb[0]);
up->fcb[0] = NULL;
cc->status = StCr3447Eof;
d = cc->waiting_decks;
while (d)
{
d2 = d;
d = d->next;
free (d2->name);
free (d2->next);
}
cc->waiting_decks = cc->last_deck = NULL;
opSetMsg ("CR3447 unloaded");
}
dcc6681InterruptDev(up, (cc->status & cc->intmask) != 0);
}
/*--------------------------------------------------------------------------
** Purpose: Handle channel activation.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void cr3447Activate(void)
{
}
/*--------------------------------------------------------------------------
** Purpose: Handle disconnecting of channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void cr3447Disconnect(void)
{
CrContext *cc;
cc = (CrContext *)active3000Device->context[0];
if (cc != NULL)
{
cc->status |= StCr3447EoiInt;
dcc6681Interrupt((cc->status & cc->intmask) != 0);
if (active3000Device->fcb[0] != NULL && cc->col != 0)
{
cr3447NextCard(active3000Device, cc);
}
}
}
/*--------------------------------------------------------------------------
** Purpose: Read next card, update card reader status.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void cr3447NextCard (DevSlot *up, CrContext *cc)
{
char *cp;
char c;
/*
** Remember the cycle counter when the card was called for.
*/
cc->getcardcycle = cycles;
/*
** Read the next card.
*/
cp = fgets (cc->card, sizeof (cc->card), up->fcb[0]);
if (cp == NULL)
{
/*
** If the last card wasn't a 6/7/8/9 card, fake one.
*/
if (cc->card[0] != '}')
{
strcpy (cc->card, "}\n");
}
else
{
cr3447NextDeck (up, cc);
return;
}
}
/*
** Set "this card is binary" if rows 7 and 9 are punched in column 1.
*/
cc->bincard = (cc->table[(u8) cc->card[0]] & 005) == 005;
/*
** Skip over any characters past column 80 (if line is longer).
*/
if ((cp = strchr (cc->card, '\n')) == NULL)
{
do
{
c = fgetc(up->fcb[0]);
} while (c != '\n' && c != EOF);
cp = &cc->card[80];
}
/*
** Blank fill line shorter then 80 characters.
*/
for ( ; cp <= &cc->card[80]; cp++)
{
*cp = ' ';
}
cc->col = 0;
if ( !cc->binary
&& (cc->table[(u8) cc->card[0]] & 006) == 006)
{
cc->status |= StCr3447File;
}
if (cc->bincard)
{
cc->status |= StCr3447Binary;
}
}
static void cr3447NextDeck (DevSlot *up, CrContext *cc)
{
DeckName *d;
FILE *fcb;
if (up->fcb[0] != NULL)
{
fclose (up->fcb[0]);
}
d = cc->waiting_decks;
if (d != NULL)
{
cc->waiting_decks = d->next;
if (d->next == NULL)
{
cc->last_deck = NULL;
}
fcb = fopen(d->name, "r");
if (fcb == NULL)
{
logError (LogErrorLocation, "cr3447: deck open failure, file %s, %s",
d->name, strerror (errno));
cr3447NextDeck (up, cc);
}
else
{
up->fcb[0] = fcb;
cc->status = StCr3447Ready;
cr3447NextCard (up, cc);
opSetStatus (cc->statusBuf, d->name);
}
free (d->name);
free (d);
}
else
{
/* No more decks */
up->fcb[0] = NULL;
cc->status = StCr3447Eof;
opSetStatus (cc->statusBuf, NULL);
}
}
/*--------------------------- End Of File ------------------------------*/