-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_card.c
1544 lines (1384 loc) · 54.8 KB
/
sim_card.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Generic Card read/punch routines for simulators.
Copyright (c) 2005, Richard Cornwell
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
ROBERT M SUPNIK 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.
This is the standard card reader.
This is the standard card punch.
Input formats are accepted in a variaty of formats:
Standard ASCII: one record per line.
returns are ignored.
tabs are expanded to modules 8 characters.
~ in first column is treated as a EOF.
Binary Card format:
Each record 160 characters.
First characters 6789----
Second character 21012345
111
Top 4 bits of second character are 0.
It is unlikely that any other format could
look like this.
BCD Format:
Each record variable length (80 chars or less).
Record mark has bit 7 set.
Bit 6 is even parity.
Bits 5-0 are character.
CBN Format:
Each record 160 charaters.
First char has bit 7 set. Rest set to 0.
Bit 6 is odd parity.
Bit 5-0 of first character are top 6 bits
of card.
Bit 5-0 of second character are lower 6 bits
of card.
ASCII mode recognizes some additional forms of input which allows the
intermixing of binary cards with text cards.
Lines beginning with ~raw are taken as a number of 4 digit octal values
with represent each column of the card from 12 row down to 9 row. If there
is not enough octal numbers to span a full card the remainder of the
card will not be punched.
Also ~eor, will generate a 7/8/9 punch card. An ~eof will gernerate a
6/7/9 punch card, and a ~eoi will generate a 6/7/8/9 punch.
A single line of ~ will set the EOF flag when that card is read.
For autodetection of card format, there can be no parity errors.
All undeterminate formats are treated as ASCII.
Auto output format is ASCII if card has only printable characters
or card format binary.
The card module uses up7 to hold a buffer for the card being translated
and the backward translation table. Which is generated from the table.
*/
#include <ctype.h>
#include "sim_defs.h"
#include "sim_card.h"
#if defined(USE_SIM_CARD)
#define card_ctx up8
#define CARD_EOF 0x1000 /* This card is end of file card. */
#define CARD_ERR 0x2000 /* Return error for this card */
#define DECK_SIZE 1000 /* Number of cards to allocate at a time */
struct card_context
{
t_addr punch_count; /* Number of cards punched */
char cbuff[1024]; /* Read in buffer for cards */
uint8 hol_to_ascii[4096]; /* Back conversion table */
t_addr hopper_size; /* Size of hopper */
t_addr hopper_cards; /* Number of cards in hopper */
uint16 (*images)[1][80];
};
/* Character conversion tables */
const char sim_six_to_ascii[64] = {
' ', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '0', '=', '\'', ':', '>', '%', /* 17 = box */
'_', '/', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', '@', ',', '(', '~', '\\', '#',
'-', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', '!', '$', '*', ']', ';', '^', /* 57 = triangle */
'+', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', '?', '.', ')', '[', '<', '@', /* 37 = stop code */
}; /* 72 = rec mark */
/* 75 = squiggle, 77 = del */
static const uint16 ascii_to_hol_026[128] = {
/* Control */
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000, /*0-37*/
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/* sp ! " # $ % & ' */
/* none Y28 78 T28 Y38 T48 XT 48 */
0x000, 0x600, 0x006, 0x282, 0x442, 0x222, 0xA00, 0x022, /* 40 - 77 */
/* ( ) * + , - . / */
/* T48 X48 Y48 X T38 T X38 T1 */
0x222, 0x822, 0x422, 0x800, 0x242, 0x400, 0x842, 0x300,
/* 0 1 2 3 4 5 6 7 */
/* T 1 2 3 4 5 6 7 */
0x200, 0x100, 0x080, 0x040, 0x020, 0x010, 0x008, 0x004,
/* 8 9 : ; < = > ? */
/* 8 9 58 Y68 X68 38 68 X28 */
0x002, 0x001, 0x012, 0x40A, 0x80A, 0x042, 0x00A, 0x882,
/* @ A B C D E F G */
/* 82 X1 X2 X3 X4 X5 X6 X7 */
0x022, 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, /* 100 - 137 */
/* H I J K L M N O */
/* X8 X9 Y1 Y2 Y3 Y4 Y5 Y6 */
0x802, 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408,
/* P Q R S T U V W */
/* Y7 Y8 Y9 T2 T3 T4 T5 T6 */
0x404, 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208,
/* X Y Z [ \ ] ^ _ */
/* T7 T8 T9 X58 X68 T58 T78 28 */
0x204, 0x202, 0x201, 0x812, 0x20A, 0x412, 0x406, 0x082,
/* ` a b c d e f g */
0x212, 0xB00, 0xA80, 0xA40, 0xA20, 0xA10, 0xA08, 0xA04, /* 140 - 177 */
/* h i j k l m n o */
0xA02, 0xA01, 0xD00, 0xC80, 0xC40, 0xC20, 0xC10, 0xC08,
/* p q r s t u v w */
0xC04, 0xC02, 0xC01, 0x680, 0x640, 0x620, 0x610, 0x608,
/* x y z { | } ~ del */
/* T79 X78 Y79 79 */
0x604, 0x602, 0x601, 0x406, 0x806, 0x805, 0x005, 0xf000
};
/* Set for Burrough codes */
static const uint16 ascii_to_hol_029[128] = {
/* Control */
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000, /*0-37*/
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/* sp ! " # $ % & ' */
/* none X28 78 38 Y38 T48 X 58 */
0x000, 0x482, 0x006, 0x042, 0x442, 0x222, 0x800, 0x012, /* 40 - 77 */
/* ( ) * + , - . / */
/* X58 Y58 Y48 X68 T38 Y X38 T1 */
0x812, 0x412, 0x422, 0x80A, 0x242, 0x400, 0x842, 0x300,
/* 0 1 2 3 4 5 6 7 */
/* T 1 2 3 4 5 6 7 */
0x200, 0x100, 0x080, 0x040, 0x020, 0x010, 0x008, 0x004,
/* 8 9 : ; < = > ? */
/* 8 9 28 Y68 X48 68 T68 T78 */
0x002, 0x001, 0x082, 0x40A, 0x822, 0x00A, 0x20A, 0x206,
/* @ A B C D E F G */
/* 48 X1 X2 X3 X4 X5 X6 X7 */
0x022, 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, /* 100 - 137 */
/* H I J K L M N O */
/* X8 X9 Y1 Y2 Y3 Y4 Y5 Y6 */
0x802, 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408,
/* P Q R S T U V W */
/* Y7 Y8 Y9 T2 T3 T4 T5 T6 */
0x404, 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208,
/* X Y Z [ \ ] ^ _ */
/* T7 T8 T9 TY028 T28 TY038 Y78 T58 */
0x204, 0x202, 0x201, 0xE82, 0x282, 0xE42, 0x406, 0x212,
/* ` a b c d e f g */
0x102 ,0xB00, 0xA80, 0xA40, 0xA20, 0xA10, 0xA08, 0xA04, /* 140 - 177 */
/* h i j k l m n o */
0xA02, 0xA01, 0xD00, 0xC80, 0xC40, 0xC20, 0xC10, 0xC08,
/* p q r s t u v w */
0xC04, 0xC02, 0xC01, 0x680, 0x640, 0x620, 0x610, 0x608,
/* x y z { | } ~ del */
/* Y78 X78 X79 XTY18 */
0x604, 0x602, 0x601, 0x406, 0x806, 0x805, 0xF02,0xf000
};
/* Set for DEC 029 codes */
static const uint16 ascii_to_dec_029[128] = {
/* Control */
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000, /*0-37*/
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/* sp ! " # $ % & ' */
/* none Y28 78 38 Y38 T48 X 58 */
0x000, 0x482, 0x006, 0x042, 0x442, 0x222, 0x800, 0x012, /* 40 - 77 */
/* ( ) * + , - . / */
/* X58 Y58 Y48 X68 T38 Y X38 T1 */
0x812, 0x412, 0x422, 0x80A, 0x242, 0x400, 0x842, 0x300,
/* 0 1 2 3 4 5 6 7 */
/* T 1 2 3 4 5 6 7 */
0x200, 0x100, 0x080, 0x040, 0x020, 0x010, 0x008, 0x004,
/* 8 9 : ; < = > ? */
/* 8 9 28 Y68 X48 68 T68 T78 */
0x002, 0x001, 0x082, 0x40A, 0x822, 0x00A, 0x20A, 0x206,
/* @ A B C D E F G */
/* 48 X1 X2 X3 X4 X5 X6 X7 */
0x022, 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, /* 100 - 137 */
/* H I J K L M N O */
/* X8 X9 Y1 Y2 Y3 Y4 Y5 Y6 */
0x802, 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408,
/* P Q R S T U V W */
/* Y7 Y8 Y9 T2 T3 T4 T5 T6 */
0x404, 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208,
/* X Y Z [ \ ] ^ _ */
/* T7 T8 T9 X28 Y78 T28 X78 T58 */
0x204, 0x202, 0x201, 0x882, 0x406, 0x282, 0x806, 0x212,
/* ` a b c d e f g */
0x102 ,0xB00, 0xA80, 0xA40, 0xA20, 0xA10, 0xA08, 0xA04, /* 140 - 177 */
/* h i j k l m n o */
0xA02, 0xA01, 0xD00, 0xC80, 0xC40, 0xC20, 0xC10, 0xC08,
/* p q r s t u v w */
0xC04, 0xC02, 0xC01, 0x680, 0x640, 0x620, 0x610, 0x608,
/* x y z { | } ~ del */
/* XT XY YT YT1 */
0x604, 0x602, 0x601, 0xA00, 0xC00, 0x600, 0x700,0xf000
};
static const uint16 ascii_to_hol_ebcdic[128] = {
/* Control */
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000, /*0-37*/
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/*Control*/
0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
/* sp ! " # $ % & ' */
/* none Y28 78 38 Y38 T48 X 58 */
0x000, 0x482, 0x006, 0x042, 0x442, 0x222, 0x800, 0x012, /* 40 - 77 */
/* ( ) * + , - . / */
/* X58 Y58 Y48 X T38 Y X38 T1 */
0x812, 0x412, 0x422, 0x800, 0x242, 0x400, 0x842, 0x300,
/* 0 1 2 3 4 5 6 7 */
/* T 1 2 3 4 5 6 7 */
0x200, 0x100, 0x080, 0x040, 0x020, 0x010, 0x008, 0x004,
/* 8 9 : ; < = > ? */
/* 8 9 28 Y68 X48 68 T68 T78 */
0x002, 0x001, 0x082, 0x40A, 0x822, 0x00A, 0x20A, 0x206,
/* @ A B C D E F G */
/* 48 X1 X2 X3 X4 X5 X6 X7 */
0x022, 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, /* 100 - 137 */
/* H I J K L M N O */
/* X8 X9 Y1 Y2 Y3 Y4 Y5 Y6 */
0x802, 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408,
/* P Q R S T U V W */
/* Y7 Y8 Y9 T2 T3 T4 T5 T6 */
0x404, 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208,
/* X Y Z [ \ ] ^ _ */
/* T7 T8 T9 X28 X68 Y28 Y78 X58 */
0x204, 0x202, 0x201, 0x882, 0x20A, 0x482, 0x406, 0x212,
/* ` a b c d e f g */
0x102, 0xB00, 0xA80, 0xA40, 0xA20, 0xA10, 0xA08, 0xA04, /* 140 - 177 */
/* h i j k l m n o */
0xA02, 0xA01, 0xD00, 0xC80, 0xC40, 0xC20, 0xC10, 0xC08,
/* p q r s t u v w */
0xC04, 0xC02, 0xC01, 0x680, 0x640, 0x620, 0x610, 0x608,
/* x y z { | } ~ del */
/* X18 X78 Y18 XYT18 */
0x604, 0x602, 0x601, 0x902, 0x806, 0x502, 0xF02,0xf000
};
const char sim_ascii_to_six[128] = {
/* Control */
-1, -1, -1, -1, -1, -1, -1, -1, /* 0 - 37 */
/* Control */
-1, -1, -1, -1, -1, -1, -1, -1,
/* Control */
-1, -1, -1, -1, -1, -1, -1, -1,
/* Control */
-1, -1, -1, -1, -1, -1, -1, -1,
/*sp ! " # $ % & ' */
000, 052, -1, 032, 053, 017, 060, 014, /* 40 - 77 */
/* ( ) * + , - . / */
034, 074, 054, 060, 033, 040, 073, 021,
/* 0 1 2 3 4 5 6 7 */
012, 001, 002, 003, 004, 005, 006, 007,
/* 8 9 : ; < = > ? */
010, 011, 015, 056, 076, 013, 016, 072,
/* @ A B C D E F G */
014, 061, 062, 063, 064, 065, 066, 067, /* 100 - 137 */
/* H I J K L M N O */
070, 071, 041, 042, 043, 044, 045, 046,
/* P Q R S T U V W */
047, 050, 051, 022, 023, 024, 025, 026,
/* X Y Z [ \ ] ^ _ */
027, 030, 031, 075, 036, 055, 057, 020,
/* ` a b c d e f g */
035, 061, 062, 063, 064, 065, 066, 067, /* 140 - 177 */
/* h i j k l m n o */
070, 071, 041, 042, 043, 044, 045, 046,
/* p q r s t u v w */
047, 050, 051, 022, 023, 024, 025, 026,
/* x y z { | } ~ del*/
027, 030, 031, 057, 077, 017, -1, -1
};
static uint16 ebcdic_to_hol[256] = {
/* T918 T91 T92 T93 T94 T95 T96 T97 0x0x */
0xB03, 0x901, 0x881, 0x841, 0x821, 0x811, 0x809, 0x805,
/* T98, T189 , T289, T389, T489, T589, T689, T789 */
0x803, 0x903, 0x883, 0x843, 0x823, 0x813, 0x80B, 0x807,
/* TE189 E91 E92 E93 E94 E95 E96 E97 0x1x */
0xD03, 0x501, 0x481, 0x441, 0x421, 0x411, 0x409, 0x405,
/* E98 E918 E928 E938 E948 E958 E968 E978 */
0x403, 0x503, 0x483, 0x443, 0x423, 0x413, 0x40B, 0x407,
/* E0918 091 092 093 094 095 096 097 0x2x */
0x703, 0x301, 0x281, 0x241, 0x221, 0x211, 0x209, 0x205,
/* 098 0918 0928 0938 0948 0958 0968 0978 */
0x203, 0x303, 0x283, 0x243, 0x223, 0x213, 0x20B, 0x207,
/* TE0918 91 92 93 94 95 96 97 0x3x */
0xF03, 0x101, 0x081, 0x041, 0x021, 0x011, 0x009, 0x005,
/* 98 189 289 389 489 589 689 789 */
0x003, 0x103, 0x083, 0x043, 0x023, 0x013, 0x00B, 0x007,
/* T091 T092 T093 T094 T095 T096 T097 0x4x */
0x000, 0xB01, 0xA81, 0xA41, 0xA21, 0xA11, 0xA09, 0xA05,
/* T098 T18 T28 T38 T48 T58 T68 T78 */
0xA03, 0x902, 0x882, 0x842, 0x822, 0x812, 0x80A, 0x806,
/* T TE91 TE92 TE93 TE94 TE95 TE96 TE97 0x5x */
0x800, 0xD01, 0xC81, 0xC41, 0xC21, 0xC11, 0xC09, 0xC05,
/* TE98 E18 E28 E38 E48 E58 E68 E78 */
0xC03, 0x502, 0x482, 0x442, 0x422, 0x412, 0x40A, 0x406,
/* E 01 E092 E093 E094 E095 E096 E097 0x6x */
0x400, 0x300, 0x681, 0x641, 0x621, 0x611, 0x609, 0x605,
/* E098 018 TE 038 048 68 068 078 */
0x603, 0x302, 0xC00, 0x242, 0x222, 0x212, 0x20A, 0x206,
/* TE0 TE091 TE092 TE093 TE094 TE095 TE096 TE097 0x7x */
0xE00, 0xF01, 0xE81, 0xE41, 0xE21, 0xE11, 0xE09, 0xE05,
/* TE098 18 28 38 48 58 68 78 */
0xE03, 0x102, 0x082, 0x042, 0x022, 0x012, 0x00A, 0x006,
/* T018 T01 T02 T03 T04 T05 T06 T07 0x8x */
0xB02, 0xB00, 0xA80, 0xA40, 0xA20, 0xA10, 0xA08, 0xA04,
/* T08 T09 T028 T038 T048 T058 T068 T078 */
0xA02, 0xA01, 0xA82, 0xA42, 0xA22, 0xA12, 0xA0A, 0xA06,
/* TE18 TE1 TE2 TE3 TE4 TE5 TE6 TE7 0x9x */
0xD02, 0xD00, 0xC80, 0xC40, 0xC20, 0xC10, 0xC08, 0xC04,
/* TE8 TE9 TE28 TE38 TE48 TE58 TE68 TE78 */
0xC02, 0xC01, 0xC82, 0xC42, 0xC22, 0xC12, 0xC0A, 0xC06,
/* E018 E01 E02 E03 E04 E05 E06 E07 0xax */
0x702, 0x700, 0x680, 0x640, 0x620, 0x610, 0x608, 0x604,
/* E08 E09 E028 E038 E048 E058 E068 E078 */
0x602, 0x601, 0x682, 0x642, 0x622, 0x612, 0x60A, 0x606,
/* TE018 TE01 TE02 TE03 TE04 TE05 TE06 TE07 0xbx */
0xF02, 0xF00, 0xE80, 0xE40, 0xE20, 0xE10, 0xE08, 0xE04,
/* TE08 TE09 TE028 TE038 TE048 TE058 TE068 TE078 */
0xE02, 0xE01, 0xE82, 0xE42, 0xE22, 0xE12, 0xE0A, 0xE06,
/* T0 T1 T2 T3 T4 T5 T6 T7 0xcx */
0xA00, 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804,
/* T8 T9 T0928 T0938 T0948 T0958 T0968 T0978 */
0x802, 0x801, 0xA83, 0xA43, 0xA23, 0xA13, 0xA0B, 0xA07,
/* E0 E1 E2 E3 E4 E5 E6 E7 0xdx */
0x600, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408, 0x404,
/* E8 E9 TE928 TE938 TE948 TE958 TE968 TE978 */
0x402, 0x401, 0xC83, 0xC43, 0xC23, 0xC13, 0xC0B, 0xC07,
/* 028 E091 02 03 04 05 06 07 0xex */
0x282, 0x701, 0x280, 0x240, 0x220, 0x210, 0x208, 0x204,
/* 08 09 E0928 E0938 E0948 E0958 E0968 E0978 */
0x202, 0x201, 0x683, 0x643, 0x623, 0x613, 0x60B, 0x607,
/* 0 1 2 3 4 5 6 7 0xfx */
0x200, 0x100, 0x080, 0x040, 0x020, 0x010, 0x008, 0x004,
/* 8 9 TE0928 TE0938 TE0948 TE0958 TE0968 TE0978 */
0x002, 0x001, 0xE83, 0xE43, 0xE23, 0xE13, 0xE0B, 0xE07
};
static uint16 hol_to_ebcdic[4096];
const uint8 sim_parity_table[64] = {
/* 0 1 2 3 4 5 6 7 */
0000, 0100, 0100, 0000, 0100, 0000, 0000, 0100,
0100, 0000, 0000, 0100, 0000, 0100, 0100, 0000,
0100, 0000, 0000, 0100, 0000, 0100, 0100, 0000,
0000, 0100, 0100, 0000, 0100, 0000, 0000, 0100,
0100, 0000, 0000, 0100, 0000, 0100, 0100, 0000,
0000, 0100, 0100, 0000, 0100, 0000, 0000, 0100,
0000, 0100, 0100, 0000, 0100, 0000, 0000, 0100,
0100, 0000, 0000, 0100, 0000, 0100, 0100, 0000
};
struct card_formats {
uint32 mode;
const char *name;
};
static struct card_formats fmts[] = {
{MODE_AUTO, "AUTO"},
{MODE_BIN, "BIN"},
{MODE_TEXT, "TEXT"},
{MODE_BCD, "BCD"},
{MODE_CBN, "CBN"},
{MODE_EBCDIC,"EBCDIC"},
{MODE_OCTAL, "OCTAL"},
{0, 0},
};
/* Conversion routines */
/* Convert BCD character into hollerith code */
uint16
sim_bcd_to_hol(uint8 bcd) {
uint16 hol;
/* Handle space correctly */
if (bcd == 0) /* 0 to 82 punch */
return 0x82;
if (bcd == 020) /* 20 no punch */
return 0;
/* Convert to top column */
switch (bcd & 060) {
default:
case 000:
hol = 0x000;
break;
case 020:
hol = 0x200;
break;
case 040:
hol = 0x400;
break;
case 060:
hol = 0x800;
break;
}
/* Handle case of 10 special */
/* Only 032 is punched as 8-2 */
if ((bcd & 017) == 10 && (bcd & 060) != 020) {
hol |= 1 << 9;
return hol;
}
/* Convert to 0-9 row */
bcd &= 017;
if (bcd > 9) {
hol |= 0x2; /* Col 8 */
bcd -= 8;
}
if (bcd != 0)
hol |= 1 << (9 - bcd);
return hol;
}
/* Returns the BCD of the hollerith code or 0x7f if error */
uint8
sim_hol_to_bcd(uint16 hol) {
uint8 bcd;
/* Convert 10,11,12 rows */
switch (hol & 0xe00) {
case 0x000:
bcd = 0;
break;
case 0x200:
if ((hol & 0x1ff) == 0)
return 10;
bcd = 020;
break;
case 0x400:
bcd = 040;
break;
case 0x600: /* 11-10 Punch */
bcd = 052;
break;
case 0x800:
bcd = 060;
break;
case 0xA00: /* 12-10 Punch */
bcd = 072;
break;
default: /* Double punch in 10,11,12 rows */
return 0x7f;
}
hol &= 0x1ff; /* Mask rows 0-9 */
/* Check row 8 punched */
if (hol & 0x2) {
bcd += 8;
hol &= ~0x2;
}
/* Convert rows 0-9 */
while (hol != 0 && (hol & 0x200) == 0) {
bcd++;
hol <<= 1;
}
/* Any more columns punched? */
if ((hol & 0x1ff) != 0)
return 0x7f;
return bcd;
}
/* Convert EBCDIC character into hollerith code */
uint16
sim_ebcdic_to_hol(uint8 ebcdic) {
return ebcdic_to_hol[ebcdic];
}
/* Returns the BCD of the hollerith code or 0x7f if error */
uint16
sim_hol_to_ebcdic(uint16 hol) {
return hol_to_ebcdic[hol];
}
t_addr
sim_hopper_size(UNIT * uptr) {
struct card_context *data = (struct card_context *)uptr->card_ctx;
if (data == NULL)
return 0;
return data->hopper_cards;
}
t_addr
sim_punch_count(UNIT * uptr) {
struct card_context *data = (struct card_context *)uptr->card_ctx;
if (data == NULL)
return 0;
return data->punch_count;
}
t_addr
sim_card_input_hopper_count(UNIT *uptr) {
struct card_context *data = (struct card_context *)uptr->card_ctx;
uint16 col;
if (data == NULL || data->images == NULL)
return 0; /* attached? */
if (uptr->pos >= data->hopper_cards)
return 0;
col = (*data->images)[data->hopper_cards-1][0];
return (int)((data->hopper_cards - uptr->pos) - ((col & CARD_EOF) ? 1 : 0));
}
t_addr
sim_card_output_hopper_count(UNIT *uptr) {
struct card_context *data = (struct card_context *)uptr->card_ctx;
if (data == NULL)
return 0; /* attached? */
return (int)data->punch_count;
}
t_cdstat
sim_read_card(UNIT * uptr, uint16 image[80])
{
int i;
struct card_context *data = (struct card_context *)uptr->card_ctx;
DEVICE *dptr;
uint16 (*img)[80];
t_stat r = CDSE_OK;
if (data == NULL || (uptr->flags & UNIT_ATT) == 0)
return CDSE_EMPTY; /* attached? */
if (data->hopper_cards == 0 || uptr->pos >= data->hopper_cards)
return CDSE_EMPTY;
dptr = find_dev_from_unit( uptr);
img = &(*data->images)[uptr->pos];
if (sim_deb && dptr && ((dptr)->dctrl & DEBUG_CARD)) {
if (image[0] & CARD_EOF) {
sim_debug(DEBUG_CARD, dptr, "Read hopper EOF\n");
} else if (image[0] & CARD_ERR) {
sim_debug(DEBUG_CARD, dptr, "Read hopper ERR\n");
} else {
uint8 out[81];
int ok = 1;
for (i = 0; i < 80; i++) {
out[i] = data->hol_to_ascii[(int)(*img)[i]];
if (out[i] == 0xff) {
ok = 0;
}
}
if (ok) {
sim_debug(DEBUG_CARD, dptr, "Read hopper: [");
for (i = 0; i < 80; i++) {
sim_debug(DEBUG_CARD, dptr, "%c", out[i]);
}
sim_debug(DEBUG_CARD, dptr, "]\n");
} else {
sim_debug(DEBUG_CARD, dptr, "Read hopper binary\n");
}
}
}
if ((*img)[0] & CARD_EOF)
r = CDSE_EOF;
else if ((*img)[0] & CARD_ERR)
r = CDSE_ERROR;
uptr->pos++;
data->punch_count++;
memcpy(image, img, 80 * sizeof(uint16));
image[0] &= 0xfff; /* Remove any CARD_EOF and CARD_ERR Flags */
return r;
}
/*
* Check if reader is at last card.
*/
int
sim_card_eof(UNIT *uptr)
{
struct card_context *data = (struct card_context *)uptr->card_ctx;
uint16 col;
if (data == NULL || data->images == NULL)
return SCPE_UNATT; /* attached? */
if (uptr->pos >= data->hopper_cards)
return SCPE_UNATT;
col = (*data->images)[uptr->pos][0];
if (col & CARD_EOF)
return 1;
return 0;
}
struct _card_buffer {
uint8 buffer[8192+500]; /* Buffer data */
int len; /* Amount of data in buffer */
int size; /* Size of last card read */
};
static int _cmpcard(const uint8 *p, const char *s) {
int i;
if (p[0] != '~')
return 0;
for(i = 0; i < 3; i++) {
if (tolower(p[i+1]) != s[i])
return 0;
}
return 1;
}
t_stat
_sim_parse_card(UNIT *uptr, DEVICE *dptr, struct _card_buffer *buf, uint16 (*image)[80]) {
unsigned int mode;
uint16 temp;
int i;
char c;
int col;
sim_debug(DEBUG_CARD, dptr, "Read card ");
if ((uptr->flags & UNIT_CARD_MODE) == MODE_AUTO) {
mode = MODE_TEXT; /* Default is text */
/* Check buffer to see if binary card in it. */
for (i = 0, temp = 0; i < 160 && i <buf->len; i+=2)
temp |= (uint16)(buf->buffer[i] & 0xFF);
/* Check if every other char < 16 & full buffer */
if ((temp & 0x0f) == 0 && i == 160)
mode = MODE_BIN; /* Probably binary */
/* Check if maybe BCD or CBN */
if (buf->buffer[0] & 0x80) {
int odd = 0;
int even = 0;
/* Clear record mark */
buf->buffer[0] &= 0x7f;
/* Check all chars for correct parity */
for(i = 0, temp = 0; i < buf->len; i++) {
uint8 ch = buf->buffer[i];
/* Stop at EOR */
if (ch & 0x80)
break;
/* Try matching parity */
if (sim_parity_table[(ch & 077)] == (ch & 0100))
even++;
else
odd++;
}
/* Restore it */
buf->buffer[0] |= 0x80;
if (i == 160 && odd == i)
mode = MODE_CBN;
else if (i < 80 && even == i)
mode = MODE_BCD;
}
/* Check if modes match */
if ((uptr->flags & UNIT_CARD_MODE) != MODE_AUTO &&
(uptr->flags & UNIT_CARD_MODE) != mode) {
(*image)[0] = CARD_ERR;
sim_debug(DEBUG_CARD, dptr, "invalid mode\n");
return SCPE_OPENERR;
}
} else
mode = uptr->flags & UNIT_CARD_MODE;
switch(mode) {
default:
case MODE_TEXT:
sim_debug(DEBUG_CARD, dptr, "text: [");
/* Check for special codes */
if (buf->buffer[0] == '~') {
int f = 1;
for(col = i = 1; col < 80 && f && i < buf->len; i++) {
c = buf->buffer[i];
switch (c) {
case '\n':
case '\0':
case '\r':
col = 80;
case ' ':
break; /* Ignore these */
case '\t':
col = (col | 7) + 1; /* Mult of 8 */
break;
default:
f = 0;
break;
}
}
if (f) {
(*image)[0] |= CARD_EOF;
goto end_card;
}
}
if (_cmpcard(&buf->buffer[0], "raw")) {
int j = 0;
sim_debug(DEBUG_CARD, dptr, "-octal-");
for(col = 0, i = 4; col < 80 && i < buf->len; i++) {
if (buf->buffer[i] >= '0' && buf->buffer[i] <= '7') {
(*image)[col] = ((*image)[col] << 3) | (buf->buffer[i] - '0');
j++;
} else if (buf->buffer[i] == '\n' || buf->buffer[i] == '\r') {
break;
} else {
(*image)[0] = CARD_ERR;
break;
}
if (j == 4) {
col++;
j = 0;
}
}
} else if (_cmpcard(&buf->buffer[0], "eor")) {
sim_debug(DEBUG_CARD, dptr, "-eor-");
(*image)[0] = 07; /* 7/8/9 punch */
i = 4;
} else if (_cmpcard(&buf->buffer[0], "eof")) {
sim_debug(DEBUG_CARD, dptr, "-eof-");
(*image)[0] = 015; /* 6/7/9 punch */
i = 4;
} else if (_cmpcard(&buf->buffer[0], "eoi")) {
sim_debug(DEBUG_CARD, dptr, "-eoi-");
(*image)[0] = 017; /* 6/7/8/9 punch */
i = 4;
} else {
/* Convert text line into card image */
for (col = 0, i = 0; col < 80 && i < buf->len; i++) {
c = buf->buffer[i];
switch (c) {
case '\0':
case '\r':
break; /* Ignore these */
case '\t':
col = (col | 7) + 1; /* Mult of 8 */
break;
case '\n':
col = 80;
i--;
break;
default:
sim_debug(DEBUG_CARD, dptr, "%c", c);
if ((uptr->flags & MODE_LOWER) == 0)
c = toupper(c);
switch(uptr->flags & MODE_CHAR) {
default:
case MODE_026:
temp = ascii_to_hol_026[(int)c];
break;
case MODE_029:
temp = ascii_to_hol_029[(int)c];
break;
case MODE_DEC29:
temp = ascii_to_dec_029[(int)c];
break;
}
if (temp & 0xf000)
(*image)[0] |= CARD_ERR;
(*image)[col++] = temp & 0xfff;
}
}
}
end_card:
sim_debug(DEBUG_CARD, dptr, "-%d-", i);
/* Scan to end of line, ignore anything after last column */
while (buf->buffer[i] != '\n' && buf->buffer[i] != '\r' && i < buf->len) {
i++;
}
if (buf->buffer[i] == '\r')
i++;
if (buf->buffer[i] == '\n')
i++;
sim_debug(DEBUG_CARD, dptr, "]\n");
break;
case MODE_BIN:
temp = 0;
sim_debug(DEBUG_CARD, dptr, "bin\n");
if (buf->len < 160) {
(*image)[0] = CARD_ERR;
return SCPE_OPENERR;
}
/* Move data to buffer */
for (col = i = 0; i < 160;) {
temp |= (uint16)(buf->buffer[i] & 0xff);
(*image)[col] = (buf->buffer[i++] >> 4) & 0xF;
(*image)[col++] |= ((uint16)buf->buffer[i++] & 0xff) << 4;
}
/* Check if format error */
if (temp & 0xF)
(*image)[0] |= CARD_ERR;
break;
case MODE_CBN:
sim_debug(DEBUG_CARD, dptr, "cbn\n");
/* Check if first character is a tape mark */
if (buf->buffer[0] == 0217 &&
(buf->len == 1 || (buf->buffer[1] & 0200) != 0)) {
i = 1;
(*image)[0] |= CARD_EOF;
break;
}
/* Clear record mark */
buf->buffer[0] &= 0x7f;
/* Convert card and check for errors */
for (col = i = 0; i < buf->len && col < 80;) {
uint8 c;
if (buf->buffer[i] & 0x80)
break;
c = buf->buffer[i] & 077;
if (sim_parity_table[(int)c] == (buf->buffer[i++] & 0100))
(*image)[0] |= CARD_ERR;
(*image)[col] = ((uint16)c) << 6;
if (buf->buffer[i] & 0x80)
break;
c = buf->buffer[i] & 077;
if (sim_parity_table[(int)c] == (buf->buffer[i++] & 0100))
(*image)[0] |= CARD_ERR;
(*image)[col++] |= c;
}
if (i < buf->len && col >= 80 && (buf->buffer[i] & 0x80) == 0) {
(*image)[0] |= CARD_ERR;
}
/* Record over length of card, skip until next */
while ((buf->buffer[i] & 0x80) == 0) {
if (i > buf->len)
break;
i++;
}
break;
case MODE_BCD:
sim_debug(DEBUG_CARD, dptr, "bcd [");
/* Check if first character is a tape mark */
if (buf->buffer[0] == 0217 && (buf->buffer[1] & 0200) != 0) {
i = 1;
(*image)[0] |= CARD_EOF;
break;
}
/* Clear record mark */
buf->buffer[0] &= 0x7f;
/* Convert text line into card image */
for (col = 0, i = 0; col < 80 && i < buf->len; i++) {
if (buf->buffer[i] & 0x80)
break;
c = buf->buffer[i] & 077;
if (sim_parity_table[(int)c] != (buf->buffer[i] & 0100))
(*image)[0] |= CARD_ERR;
sim_debug(DEBUG_CARD, dptr, "%c", sim_six_to_ascii[(int)c]);
/* Convert to top column */
(*image)[col++] = sim_bcd_to_hol(c);
}
if (i < buf->len && col >= 80 && (buf->buffer[i] & 0x80) == 0) {
(*image)[0] |= CARD_ERR;
}
/* Record over length of card, skip until next */
while ((buf->buffer[i] & 0x80) == 0) {
if (i > buf->len)
break;
i++;
}
sim_debug(DEBUG_CARD, dptr, "]\n");
break;
case MODE_EBCDIC:
sim_debug(DEBUG_CARD, dptr, "ebcdic\n");
if (buf->len < 80)
(*image)[0] |= CARD_ERR;
/* Move data to buffer */
for (i = 0; i < 80 && i < buf->len; i++) {
temp = (uint16)(buf->buffer[i]) & 0xFF;
(*image)[i] = ebcdic_to_hol[temp];
}
break;
}
buf->size = i;
return SCPE_OK;
}
t_stat
_sim_read_deck(UNIT * uptr, int eof)
{
struct _card_buffer buf;
struct card_context *data;
DEVICE *dptr;
int i;
int j;
int l;
int cards = 0;
t_stat r = SCPE_OK;
if ((uptr->flags & UNIT_ATT) == 0)
return SCPE_UNATT; /* attached? */
dptr = find_dev_from_unit( uptr);
data = (struct card_context *)uptr->card_ctx;
buf.len = 0;
buf.size = 0;
buf.buffer[0] = 0; /* Initialize bufer to empty */
/* Slurp up current file */
do {
if (buf.len < 500 && !feof(uptr->fileref)) {
l = sim_fread(&buf.buffer[buf.len], 1, 8192, uptr->fileref);
if (l < 0)
r = SCPE_OPENERR;
else
buf.len += l;
}
/* Allocate space for some more cards if needed */
if (data->hopper_cards >= data->hopper_size) {
data->hopper_size += DECK_SIZE;
data->images = (uint16 (*)[1][80])realloc(data->images,
(size_t)data->hopper_size * sizeof(*(data->images)));
memset(&data->images[data->hopper_cards], 0,
(size_t)(data->hopper_size - data->hopper_cards) *
sizeof(*(data->images)));
}
/* Process one card */
cards++;