-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcp3446.c
732 lines (650 loc) · 19.3 KB
/
cp3446.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
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*--------------------------------------------------------------------------
**
** Copyright (c) 2003-2011, Paul Koning, Tom Hunter
**
** Name: cp3446.c
**
** Description:
** Perform emulation of CDC 3446 card punch controller.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License version 3 as
** published by the Free Software Foundation.
**
** 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 version 3 for more details.
**
** You should have received a copy of the GNU General Public License
** version 3 along with this program in file "license-gpl-3.0.txt".
** If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
**
**--------------------------------------------------------------------------
*/
#define DEBUG 0
/*
** -------------
** Include Files
** -------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include "const.h"
#include "types.h"
#include "proto.h"
#include "dcc6681.h"
/*
** -----------------
** Private Constants
** -----------------
*/
#define CP_LC 0
/*
** CDC 3446 card punch function and status codes.
*/
#define FcCp3446Deselect 00000
#define FcCp3446Binary 00001
#define FcCp3446BCD 00002
#define FcCp3446SelectOffset 00003
#define FcCp3446CheckLastCard 00004
#define FcCp3446Clear 00005
#define FcCp3446IntReady 00020
#define FcCp3446NoIntReady 00021
#define FcCp3446IntEoi 00022
#define FcCp3446NoIntEoi 00023
#define FcCp3446IntError 00024
#define FcCp3446NoIntError 00025
/*
** Status reply flags
**
** 0001 = Ready
** 0002 = Busy
** 0100 = Failed to feed
** 0200 = Ready interrupt
** 0400 = EOI interrupt
** 1000 = Error interrupt
** 2000 = Compare error
** 4000 = Reserved by other controller (3644 only)
**
*/
#define StCp3446Ready 00201 // includes ReadyInt
#define StCp3446Busy 00002
#define StCp3446ReadyInt 00200
#define StCp3446EoiInt 00400
#define StCp3446ErrorInt 01000
#define StCp3446CompareErr 02000
#define StCp3446NonIntStatus 02177
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
typedef struct
{
bool binary;
bool rawcard;
int intmask;
int status;
int col;
int lastnbcol;
char convtable[4096];
u32 getcardcycle;
char card[322];
} CpContext;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static FcStatus cp3446Func(PpWord funcCode);
static void cp3446Io(void);
static void cp3446Activate(void);
static void cp3446Disconnect(void);
static void cp3446FlushCard(DevSlot *up, CpContext *cc);
static char *cp3446Func2String(PpWord funcCode);
/*
** ----------------
** Public Variables
** ----------------
*/
/*
** -----------------
** Private Variables
** -----------------
*/
#if DEBUG
static FILE *cp3446Log = NULL;
#endif
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise card punch.
**
** Parameters: Name Description.
** eqNo equipment number
** unitCount number of units to initialise
** channelNo channel number the device is attached to
** deviceName optional card output file name,
** may be followed by comma and"026" (default)
** or "029" to select translation mode
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cp3446Init(u8 eqNo, u8 unitNo, u8 channelNo, char *deviceName)
{
DevSlot *up;
char fname[80];
CpContext *cc;
const PpWord *charset;
PpWord hol;
int i;
#if DEBUG
if (cp3446Log == NULL)
{
cp3446Log = fopen("cp3446log.txt", "wt");
}
#endif
up = dcc6681Attach(channelNo, eqNo, 0, DtCp3446);
up->activate = cp3446Activate;
up->disconnect = cp3446Disconnect;
up->func = cp3446Func;
up->io = cp3446Io;
/*
** Only one card punch unit is possible per equipment.
*/
if (up->context[0] != NULL)
{
fprintf (stderr, "Only one CP3446 unit is possible per equipment\n");
exit (1);
}
cc = calloc (1, sizeof (CpContext));
if (cc == NULL)
{
fprintf (stderr, "Failed to allocate CP3446 context block\n");
exit (1);
}
up->context[0] = (void *)cc;
cc->lastnbcol = -1;
cc->col = 0;
cc->status = StCp3446Ready;
/*
** Open the device file.
*/
sprintf(fname, "CP3446_C%02o_E%o", channelNo, eqNo);
up->fcb[0] = fopen(fname, "w");
if (up->fcb[0] == NULL)
{
fprintf(stderr, "Failed to open %s\n", fname);
exit(1);
}
/*
** Setup character set translation table.
*/
charset = asciiTo026; // default translation table
if (deviceName != NULL)
{
if (strcmp (deviceName, "029") == 0)
{
charset = asciiTo029;
}
else if (strcmp (deviceName, "026") != 0)
{
fprintf (stderr, "Unrecognized card code name %s\n", deviceName);
exit (1);
}
}
memset(cc->convtable, ' ', sizeof(cc->convtable));
for (i = 040; i < 0177; i++)
{
hol = charset[i] & Mask12;
if (hol != 0)
{
cc->convtable[hol] = i;
}
}
/*
** Print a friendly message.
*/
printf("CP3446 initialised on channel %o equipment %o\n", channelNo, eqNo);
}
/*--------------------------------------------------------------------------
** Purpose: Remove cards from 3446 card punch.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cp3446RemoveCards(char *params)
{
CpContext *cc;
DevSlot *dp;
int numParam;
int channelNo;
int equipmentNo;
time_t currentTime;
struct tm t;
char fname[80];
char fnameNew[80];
static char msgBuf[80];
/*
** Operator wants to remove cards.
*/
numParam = sscanf(params,"%o,%o",&channelNo, &equipmentNo);
/*
** Check parameters.
*/
if (numParam != 2)
{
printf("Not enough or invalid parameters\n");
return;
}
if (channelNo < 0 || channelNo >= MaxChannels)
{
printf("Invalid channel no\n");
return;
}
if (equipmentNo < 0 || equipmentNo >= MaxEquipment)
{
printf("Invalid equipment no\n");
return;
}
/*
** Locate the device control block.
*/
dp = dcc6681FindDevice((u8)channelNo, (u8)equipmentNo, DtCp3446);
if (dp == NULL)
{
printf("No card punch on channel %o and equipment %o\n", channelNo, equipmentNo);
return;
}
/*
** Close the old device file.
*/
cc = (CpContext *) (dp->context[0]);
cp3446FlushCard (dp, cc);
fflush(dp->fcb[0]);
fclose(dp->fcb[0]);
dp->fcb[0] = NULL;
/*
** Rename the device file to the format "CP3446_yyyymmdd_hhmmss".
*/
sprintf(fname, "CP3446_C%02o_E%o", channelNo, equipmentNo);
time(¤tTime);
t = *localtime(¤tTime);
sprintf(fnameNew, "CP3446_%04d%02d%02d_%02d%02d%02d",
t.tm_year + 1900,
t.tm_mon + 1,
t.tm_mday,
t.tm_hour,
t.tm_min,
t.tm_sec);
if (rename(fname, fnameNew) != 0)
{
printf("Could not rename %s to %s - %s\n", fname, fnameNew, strerror(errno));
return;
}
/*
** Open the device file.
*/
dp->fcb[0] = fopen(fname, "w");
/*
** Check if the open succeeded.
*/
if (dp->fcb[0] == NULL)
{
printf("Failed to open %s\n", fname);
return;
}
printf("Punch cards removed from 3446 card puncher\n");
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Execute function code on 3446 card punch.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus cp3446Func(PpWord funcCode)
{
CpContext *cc;
FcStatus st;
#if DEBUG
fprintf(cp3446Log, "\n%06d PP:%02o CH:%02o f:%04o T:%-25s > ",
traceSequenceNo,
activePpu->id,
activeDevice->channel->id,
funcCode,
cp3446Func2String(funcCode));
#endif
cc = (CpContext *)active3000Device->context[0];
switch (funcCode)
{
default: // all unrecognized codes are NOPs
#if DEBUG
fprintf(cp3446Log, " FUNC not implemented & silently ignored!");
#endif
st = FcProcessed;
break;
case FcCp3446CheckLastCard:
case FcCp3446SelectOffset:
case Fc6681MasterClear:
st = FcProcessed;
break;
case Fc6681Output:
cc->status = StCp3446Ready;
active3000Device->fcode = funcCode;
st = FcAccepted;
break;
case Fc6681DevStatusReq:
active3000Device->fcode = funcCode;
st = FcAccepted;
break;
case FcCp3446Binary:
cc->binary = TRUE;
st = FcProcessed;
break;
case FcCp3446Deselect:
case FcCp3446Clear:
cc->intmask = 0;
cc->binary = FALSE;
st = FcProcessed;
break;
case FcCp3446BCD:
cc->binary = FALSE;
st = FcProcessed;
break;
case FcCp3446IntReady:
cc->intmask |= StCp3446ReadyInt;
cc->status &= ~StCp3446ReadyInt;
st = FcProcessed;
break;
case FcCp3446NoIntReady:
cc->intmask &= ~StCp3446ReadyInt;
cc->status &= ~StCp3446ReadyInt;
st = FcProcessed;
break;
case FcCp3446IntEoi:
cc->intmask |= StCp3446EoiInt;
cc->status &= ~StCp3446EoiInt;
st = FcProcessed;
break;
case FcCp3446NoIntEoi:
cc->intmask &= ~StCp3446EoiInt;
cc->status &= ~StCp3446EoiInt;
st = FcProcessed;
break;
case FcCp3446IntError:
cc->intmask |=StCp3446ErrorInt;
cc->status &= ~StCp3446ErrorInt;
st = FcProcessed;
break;
case FcCp3446NoIntError:
cc->intmask &= ~StCp3446ErrorInt;
cc->status &= ~StCp3446ErrorInt;
st = FcProcessed;
break;
}
dcc6681Interrupt((cc->status & cc->intmask) != 0);
return(st);
}
/*--------------------------------------------------------------------------
** Purpose: Perform I/O on 3446 card punch.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void cp3446Io(void)
{
CpContext *cc;
char c;
PpWord p;
cc = (CpContext *)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 | StCp3446NonIntStatus));
activeChannel->full = TRUE;
#if DEBUG
fprintf(cp3446Log, " %04o", activeChannel->data);
#endif
}
break;
case Fc6681Output:
/*
** 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 (!cc->rawcard && cc->col >= 80)
{
cp3446FlushCard (active3000Device, cc);
}
else if (cc->rawcard && cc->col >= (80 * 4))
{
cp3446FlushCard (active3000Device, cc);
}
else
{
p = activeChannel->data & Mask12;
activeChannel->full = FALSE;
#if DEBUG
fprintf(cp3446Log, " %04o", activeChannel->data);
#endif
/*
** If rows 7 and 9 in column 1 are set and we are in binary mode,
** then we have a raw binary card.
*/
if (cc->col == 0)
{
if (cc->binary && (p & Mask5) == 00005)
{
cc->rawcard = TRUE;
}
else
{
cc->rawcard = FALSE;
}
}
if (cc->rawcard)
{
sprintf(cc->card + cc->col, "%04o", p);
cc->col += 4;
}
else if (cc->binary)
{
c = cc->convtable[p];
#if (CP_LC == 1)
c = tolower(c);
#endif
if ((cc->card[cc->col] = c) != ' ')
{
cc->lastnbcol = cc->col;
}
cc->col++;
}
else
{
c = bcdToAscii[(p >> 6) & Mask6];
#if (CP_LC == 1)
c = tolower(c);
#endif
if ((cc->card[cc->col] = c) != ' ')
{
cc->lastnbcol = cc->col;
}
cc->col++;
c = bcdToAscii[(p >> 0) & Mask6];
#if (CP_LC == 1)
c = tolower(c);
#endif
if ((cc->card[cc->col] = c) != ' ')
{
cc->lastnbcol = cc->col;
}
cc->col++;
}
}
break;
}
dcc6681Interrupt((cc->status & cc->intmask) != 0);
}
/*--------------------------------------------------------------------------
** Purpose: Handle channel activation.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void cp3446Activate(void)
{
#if DEBUG
fprintf(cp3446Log, "\n%06d PP:%02o CH:%02o Activate",
traceSequenceNo,
activePpu->id,
activeDevice->channel->id);
#endif
}
/*--------------------------------------------------------------------------
** Purpose: Handle disconnecting of channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void cp3446Disconnect(void)
{
CpContext *cc;
#if DEBUG
fprintf(cp3446Log, "\n%06d PP:%02o CH:%02o Disconnect",
traceSequenceNo,
activePpu->id,
activeDevice->channel->id);
#endif
cc = (CpContext *)active3000Device->context[0];
if (cc != NULL)
{
cc->status |= StCp3446EoiInt;
dcc6681Interrupt((cc->status & cc->intmask) != 0);
if (active3000Device->fcb[0] != NULL && cc->col != 0)
{
cp3446FlushCard(active3000Device, cc);
}
}
}
/*--------------------------------------------------------------------------
** Purpose: Punch current card, update card punch status.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void cp3446FlushCard(DevSlot *up, CpContext *cc)
{
int lc;
if (cc->col == 0)
{
return;
}
/*
** Remember the cycle counter when the card punch started.
*/
cc->getcardcycle = cycles;
if (cc->binary && cc->rawcard)
{
fputs("~raw", up->fcb[0]);
lc = cc->col;
cc->card[lc++] = '\n';
}
else
{
/*
** Omit trailing blanks.
*/
lc = cc->lastnbcol + 1;
cc->card[lc++] = '\n';
}
/*
** Write the card and reset for next card.
*/
fwrite(cc->card, 1, lc, up->fcb[0]);
cc->col = 0;
cc->lastnbcol = -1;
}
/*--------------------------------------------------------------------------
** Purpose: Convert function code to string.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: String equivalent of function code.
**
**------------------------------------------------------------------------*/
static char *cp3446Func2String(PpWord funcCode)
{
static char buf[30];
#if DEBUG
switch(funcCode)
{
case FcCp3446Deselect : return "Deselect";
case FcCp3446Binary : return "Binary";
case FcCp3446BCD : return "BCD";
case FcCp3446SelectOffset : return "SelectOffset";
case FcCp3446CheckLastCard : return "CheckLastCard";
case FcCp3446Clear : return "Clear";
case FcCp3446IntReady : return "IntReady";
case FcCp3446NoIntReady : return "NoIntReady";
case FcCp3446IntEoi : return "IntEoi";
case FcCp3446NoIntEoi : return "NoIntEoi";
case FcCp3446IntError : return "IntError";
case FcCp3446NoIntError : return "NoIntError";
case Fc6681DevStatusReq : return "6681DevStatusReq";
case Fc6681Output : return "6681Output";
case Fc6681MasterClear : return "6681MasterClear";
}
#endif
sprintf(buf, "UNKNOWN: %04o", funcCode);
return(buf);
}
/*--------------------------- End Of File ------------------------------*/