File: s3_sys.c

package info (click to toggle)
simh 3.8.1-6.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,672 kB
  • sloc: ansic: 209,820; makefile: 326
file content (948 lines) | stat: -rw-r--r-- 34,584 bytes parent folder | download | duplicates (7)
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
/* s3_sys.c: IBM System/3 system interface

   Copyright (c) 2001-2005, Charles E. Owen

   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.

   Except as contained in this notice, the name of Charles E. Owen shall not be
   used in advertising or otherwise to promote the sale, use or other dealings
   in this Software without prior written authorization from Charles E. Owen.
*/

#include <ctype.h>
#include "s3_defs.h"

extern DEVICE cpu_dev;
extern DEVICE pkb_dev;
extern DEVICE cdr_dev;
extern DEVICE cdp_dev;
extern DEVICE stack_dev;
extern DEVICE lpt_dev;
extern DEVICE r1_dev;
extern DEVICE f1_dev;
extern DEVICE r2_dev;
extern DEVICE f2_dev;
extern UNIT cpu_unit;
extern REG cpu_reg[];
extern unsigned char M[];
extern int32 saved_PC, IAR[];
extern char ebcdic_to_ascii[256];
char *parse_addr(char *cptr,  char *gbuf, int32 *addr, int32 *addrtype);

int32 printf_sym (FILE *of, char *strg, int32 addr, uint32 *val,
    UNIT *uptr, int32 sw);

/* SCP data structures

   sim_name             simulator name string
   sim_PC               pointer to saved PC register descriptor
   sim_emax             number of words needed for examine
   sim_devices          array of pointers to simulated devices
   sim_stop_messages    array of pointers to stop messages
   sim_load             binary loader
*/

char sim_name[] = "System/3";

REG *sim_PC = &cpu_reg[0];

int32 sim_emax = 6;

DEVICE *sim_devices[] = {
    &cpu_dev, 
    &pkb_dev,
    &cdr_dev,
    &cdp_dev,
    &stack_dev,
    &lpt_dev,
    &r1_dev,
    &f1_dev,
    &r2_dev,
    &f2_dev,
    NULL
};

const char *sim_stop_messages[] = {
    "Unknown error",
    "Unknown I/O Instruction",
    "HALT instruction",
    "Breakpoint",
    "Invalid Opcode",
    "Invalid Qbyte",
    "Invalid Address",
    "Invalid Device Command",
    "ATTN Card Reader"
};

/* This is the opcode master defintion table.  Each possible opcode mnemonic
   is defined here, with enough information to translate to and from
   symbolic to binary machine code.  
   First field is the opcode's mnemonic
   Second field is the hex of the right nybble of the binary opcode
   Third field is the Q code for those with implicit Q codes
   Fourth field is the symbolic format of the operands:
        0 - (Q-byte),(R-byte)
        1 - (Q-byte),(Address)
        2 - (Address),(Address),(Qbyte)
        3 - (Address),(Qbyte)
        4 - (device),(modifier),(function) -- these 3 make up qbyte
        5 - (device),(modifier),(function),(control)
        6 - (device),(modifier),(function),(Address)
        7 - (displacement) -- Q byte is implicit in opcode
        8 - (address) -- Qbyte is implicit in opcode
        9 - (Address),(Address) -- Qbyte is implicit in opcode
   Fifth Field is the group number:
        0 - Command Group (left op nybble is F)
        1 - One Address Operations A (Left Nybble C, D, or E)
        2 - Two Address Operations (Left Nybble 0,1,2,4,5,6,8,9, or A)
        3 - One Address Operations B (left Nybble 3, 7, or B)

        There is duplication in this table -- IBM defines different opcodes
        that resolve to the same binary machine instruction -- e.g. JE and
        JZ.  On input this is no problem, on output, define the one you
        want to appear first, the second will never appear on output.
*/   

int32 nopcode = 75;
struct opdef opcode[75] = {
    "HPL",  0x00,0,0,0,                                 /* Halt Program Level */
    "A",    0x06,0,1,3,                                 /* Add to Register: A R,AADD */
    "ST",   0x04,0,1,3,                                 /* Store Register */
    "L",    0x05,0,1,3,                                 /* Load Register */
    "LA",   0x02,0,1,1,                                 /* Load Address */
    "ZAZ",  0x04,0,2,2,                                 /* Zero and Add Zoned */
    "AZ",   0x06,0,2,2,                                 /* Add Zoned Decimal */
    "SZ",   0x07,0,2,2,                                 /* Subtract Zoned Decimal */
    "ALC",  0x0E,0,2,2,                                 /* Add Logical:  ALC BADD,AADD,LEN */
    "SLC",  0x0F,0,2,2,                                 /* Sub Logical:  SLC BADD,AADD,LEN */
    "MVC",  0x0C,0,2,2,                                 /* Move Chars MVX BADD,AADD,LEN */
    "ED",   0x0A,0,2,2,                                 /* Edit: ED BADD,AADD,LEN */
    "ITC",  0x0B,0,2,2,                                 /* Insert Chars: ITC BADD,AADD,LEN */
    "CLC",  0x0D,0,2,2,                                 /* Compare Logical: CLC BADD,AADD,LEN */
    "MVI",  0x0C,0,3,3,                                 /* Move Immediate */
    "SBN",  0x0A,0,3,3,                                 /* Set Bits On */
    "SBF",  0x0B,0,3,3,                                 /* Set Bits Off */
    "CLI",  0x0D,0,3,3,                                 /* Compare Immediate */
    "TBN",  0x08,0,3,3,                                 /* Test Bits On */
    "TBF",  0x09,0,3,3,                                 /* Test Bits Off */
    "APL",  0x01,0,4,0,                                 /* Advance Program Level */
    "SIO",  0x03,0,5,0,                                 /* Start I/O */
    "SNS",  0x00,0,6,3,                                 /* Sense I/O */
    "LIO",  0x01,0,6,3,                                 /* Load I/O */
    "TIO",  0x01,0,6,1,                                 /* Test I/O */
    "J",    0x02,0,7,0,                                 /* Jump Unconditional */
    "J",    0x02,0x87,7,0,                              /* Alternate J */
    "JH",   0x02,132,7,0,                               /* Jump if High */
    "JL",   0x02,130,7,0,                               /* Jump if Low */
    "JE",   0x02,129,7,0,                               /* Jump if Equal */
    "JNH",  0x02,4,7,0,                                 /* Jump if Not High */
    "JNL",  0x02,2,7,0,                                 /* Jump if Not Low */
    "JNE",  0x02,1,7,0,                                 /* Jump if Not Equal */
    "JOZ",  0x02,136,7,0,                               /* Jump if Overflow Zoned */
    "JOL",  0x02,160,7,0,                               /* Jump if Overflow Logical */
    "JNOZ", 0x02,8,7,0,                                 /* Jump if No Overflow Zoned */
    "JNOL", 0x02,32,7,0,                                /* Jump if No Overflow Logical */
    "JT",   0x02,16,7,0,                                /* Jump if True */
    "JF",   0x02,144,7,0,                               /* Jump if False */
    "JP",   0x02,132,7,0,                               /* Jump if Plus */
    "JM",   0x02,130,7,0,                               /* Jump if Minus */
    "JZ",   0x02,129,7,0,                               /* Jump if Zero */
    "JNP",  0x02,4,7,0,                                 /* Jump if Not Plus */
    "JNM",  0x02,2,7,0,                                 /* Jump if Not Minus */
    "JNZ",  0x02,1,7,0,                                 /* Jump if Not Zero */
    "NOPJ", 0x02,0x80,7,0,                              /* Never Jump - NOP */
    "B",    0x00,0x00,8,1,                              /* Branch Unconditional */
    "B",    0x00,0x87,8,1,                              /* Alternate B */
    "BH",   0x00,0x84,8,1,                              /* Branch if High */
    "BL",   0x00,0x82,8,1,                              /* Branch if Low */
    "BE",   0x00,0x81,8,1,                              /* Branch if Equal */
    "BNH",  0x00,0x04,8,1,                              /* Branch if Not High */
    "BNL",  0x00,0x02,8,1,                              /* Branch if Not Low */
    "BNE",  0x00,0x01,8,1,                              /* Branch if Not Equal */
    "BOZ",  0x00,0x88,8,1,                              /* Branch if Overflow Zoned */
    "BOL",  0x00,0xA0,8,1,                              /* Branch if Overflow Logical */
    "BNOZ", 0x00,0x08,8,1,                              /* Branch if No Overflow Zoned */
    "BNOL", 0x00,0x20,8,1,                              /* Branch if No Overflow Logical */
    "BT",   0x00,0x10,8,1,                              /* Branch if True */
    "BF",   0x00,0x90,8,1,                              /* Branch if False */
    "BP",   0x00,0x84,8,1,                              /* Branch if Plus */
    "BM",   0x00,0x82,8,1,                              /* Branch if Minus */
    "BZ",   0x00,0x81,8,1,                              /* Branch if Zero */
    "BNP",  0x00,0x04,8,1,                              /* Branch if Not Plus */
    "BNM",  0x00,0x02,8,1,                              /* Branch if Not Minus */
    "BNZ",  0x00,0x01,8,1,                              /* Branch if Not Zero */
    "NOPB", 0x00,0x80,8,1,                              /* Never Branch - NOP */
    "MZZ",  0x08,0,9,2,                                 /* Move Zone to Zone */
    "MNZ",  0x08,1,9,2,                                 /* Move Numeric to Zone */
    "MZN",  0x08,2,9,2,                                 /* Move Zone to Numeric */
    "MNN",  0x08,3,9,2,                                 /* Move Numeric to Numeric */
    "MVX",  0x08,0,2,2,                                 /* Move Hex: MVX BADD,AADD,CODE */
    "JC",   0x02,0,3,0,                                 /* Jump on Specified Condition bits */
    "BC",   0x00,0,3,1,                                 /* Branch on Specified Condition */
    "***",  0x00,0,0,0
};

int32 regcode[15] = {   0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01,
            0x80, 0xC0, 0xA0, 0x90, 0x88, 0x84, 0x82, 0x81
};
            
char regname[15][8] =  {    "(P2IAR)",
                "(P1IAR)",
                "(IAR)",
                "(ARR)",
                "(PSR)",
                "(XR2)",
                "(XR1)",
                "(IAR0)",
                "(IAR1)",
                "(IAR2)",
                "(IAR3)",
                "(IAR4)",
                "(IAR5)",
                "(IAR6)",
                "(IAR7)"
};             

/* This is the binary loader.  The input file is considered to be
   a string of literal bytes with no special format. The
   load starts at the current value of the P1IAR.
*/

int32 sim_load (FILE *fileref, char *cptr, char *fnam, int flag)
{
int32 i, addr = 0, cnt = 0;

if ((*cptr != 0) || (flag != 0)) return SCPE_ARG;
addr = IAR[8];
while ((i = getc (fileref)) != EOF) {
    M[addr] = i & 0xff;
    addr++;
    cnt++;
}   /* end while */
printf ("%d Bytes loaded.\n", cnt);
return (SCPE_OK);
}

/* Symbolic output

   Inputs:
        *of   = output stream
        addr    =       current PC
        *val    =       pointer to values
        *uptr   =       pointer to unit
        sw      =       switches
   Outputs:
        status  =       error code
*/

int32 fprint_sym (FILE *of, int32 addr, uint32 *val,
    UNIT *uptr, int32 sw)
{
    int32 r;
    char strg[256];
    
    strcpy(strg, "");
    r = printf_sym(of, strg, addr, val, uptr, sw);
    if (sw & SWMASK ('A'))
        strcpy(strg, "");
        else
        fprintf(of, "%s", strg);
    return (r);
}

int32 printf_sym (FILE *of, char *strg, int32 addr, uint32 *val,
    UNIT *uptr, int32 sw)
{
int32 cflag, c1, c2, group, len1, len2, inst, aaddr, baddr;
int32 oplen, groupno, i, j, vpos, qbyte, da, m, n;
char bld[128], bldaddr[32], boperand[32], aoperand[32];
int32 blk[16], blt[16];
int32 blkadd;

cflag = (uptr == NULL) || (uptr == &cpu_unit);
c1 = val[0] & 0xff;
if (sw & SWMASK ('A')) {
    for (i = 0; i < 16; i++) {
        blkadd = addr + (i*16);
        for (j = 0; j < 16; j++) {
            blk[j] = M[blkadd+j] & 0xff;
            c2 = ebcdic_to_ascii[blk[j]];
            if (c2 < 040 || c2 > 0177 || blk[j] == 07) {
                blt[j] = '.';
            } else {    
                blt[j] = c2;
            }
        }
        if (i == 0) {
            fprintf(of, "%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X  [%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n ",
                    blk[0], blk[1], blk[2], blk[3], blk[4], blk[5], blk[6], blk[7],
                    blk[8], blk[9], blk[10], blk[11], blk[12], blk[13], blk[14], blk[15],
                    blt[0], blt[1], blt[2], blt[3], blt[4], blt[5], blt[6], blt[7],
                    blt[8], blt[9], blt[10], blt[11], blt[12], blt[13], blt[14], blt[15]);
        } else {
            fprintf(of, "%X\t%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X  [%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n ",
                    blkadd, blk[0], blk[1], blk[2], blk[3], blk[4], blk[5], blk[6], blk[7],
                    blk[8], blk[9], blk[10], blk[11], blk[12], blk[13], blk[14], blk[15],
                    blt[0], blt[1], blt[2], blt[3], blt[4], blt[5], blt[6], blt[7],
                    blt[8], blt[9], blt[10], blt[11], blt[12], blt[13], blt[14], blt[15]);
        }           
    }       
    return SCPE_OK;  }
if (sw & SWMASK ('C')) {
    c2 = ebcdic_to_ascii[c1];
    if (c2 < 040 || c2 > 0177) {
        sprintf(strg, "<%02X>", c1 & 0xff);
    } else {    
        sprintf (strg, "%c", c2 & 0xff);
    }
    return SCPE_OK;  }
if (!(sw & SWMASK ('M'))) return SCPE_ARG;

inst = val[0] & 0x0f;
len1 = (val[0] >> 6) & 3;
len2 = (val[0] >> 4) & 3;
group = (val[0] >> 4) & 0x0f;
qbyte = val[1];

/* Get total length of instruction */

if (group == 0x0f) {
    oplen = 3;
} else {
    oplen = 2;    
    if (len1 == 0) oplen += 2;
    if (len1 == 1 || len1 == 2) oplen++;
    if (len2 == 0) oplen += 2;
    if (len2 == 1 || len2 == 2) oplen++;
}

/* Find which group it belongs to */

switch (group) {
    case 0x0f:  
        groupno = 0;
        break;
    case 0x0c:
    case 0x0d:
    case 0x0e:
        groupno = 1;
        break;
    case 0x03:
    case 0x07:
    case 0x0b:
        groupno = 3;
        break;
    default:
        groupno = 2;
        break;
}                   

/* find the table entry */

for (i = 0; i < nopcode; i++) {
    if (opcode[i].form < 7) {                           /* Explicit Q */
        if (opcode[i].group == groupno &&
            opcode[i].opmask == inst) break;
        } else {                                        /* Implicit Q */
        if (opcode[i].group == groupno &&
            opcode[i].opmask == inst &&
            opcode[i].q == qbyte) break;
        }       
}

/* print the opcode */

if (i >= nopcode) {
    sprintf(strg, "%02X", val[0]);
    oplen = 1;
} else {
    sprintf(bld, "%s ", opcode[i].op);

    /* Extract the addresses into aaddr and baddr */

    strcpy(aoperand, "ERROR");
    strcpy(boperand, "ERROR");
    vpos = 2;
    aaddr = baddr = 0;
    switch (len1) {
        case 0:
            baddr = ((val[vpos] << 8) & 0xff00) | (val[vpos + 1] & 0x00ff);
            sprintf(boperand, "%04X", baddr);
            vpos = 4;
            break;
        case 1:
            baddr = val[vpos] & 255;
            sprintf(boperand, "(%02X,XR1)", baddr);
            vpos = 3;
            break;
        case 2:
            baddr = val[vpos] & 255;
            sprintf(boperand, "(%02X,XR2)", baddr);
            vpos = 3;
            break;
        default:
            baddr = 0;
            break;
    }           
    switch (len2) {
        case 0:
            aaddr = ((val[vpos] << 8) & 0xff00) | (val[vpos + 1] & 0x00ff);
            if (group == 0x0C || group == 0x0D || group == 0x0E)
                sprintf(boperand, "%04X", aaddr);
                else
                sprintf(aoperand, "%04X", aaddr);
            break;  
        case 1:
            aaddr = val[vpos] & 255;
            if (group == 0x0C || group == 0x0D || group == 0x0E)
                sprintf(boperand, "(%02X,XR1)", aaddr);
                else
                sprintf(aoperand, "(%02X,XR1)", aaddr);
            break;
        case 2:
            aaddr = val[vpos] & 255;
            if (group == 0x0C || group == 0x0D || group == 0x0E)
                sprintf(boperand, "(%02X,XR2)", aaddr);
                else
                sprintf(aoperand, "(%02X,XR2)", aaddr);
            break;
        default:
            aaddr = 0;
            break;
    }           

    /* Display the operands in the correct format */

    da = (qbyte >> 4) & 0x0f;   
    m = (qbyte >> 3) & 0x01;
    n = (qbyte) & 0x07;

    switch (opcode[i].form) {
        case 0:
            sprintf(bldaddr, "%02X,%02X", qbyte, val[2]);
            break;
        case 1:
            if (inst == 2 || inst == 4 || inst == 5 || inst == 6) {
                for (i = 0; i < 16; i++) {
                    if (regcode[i] == qbyte)
                        break;
                }
                if (i < 16) {
                    sprintf(bldaddr, "%s,%s", regname[i], boperand);
                } else {
                    sprintf(bldaddr, "%02X,%s", qbyte, boperand);
                }           
            } else {
                sprintf(bldaddr, "%02X,%s", qbyte, boperand);
            }
            break;
        case 2:
            if (inst > 9 || inst == 4 || inst == 6 || inst == 7)
                 qbyte++;                               /* special +1 for length display */
            sprintf(bldaddr, "%s,%s,%d", boperand, aoperand, qbyte);
            break;
        case 3:
            if (strcmp(opcode[i].op, "JC") == 0) {
                sprintf(bldaddr, "%04X,%02X", addr+oplen+val[2], qbyte);
            } else {    
                sprintf(bldaddr, "%s,%02X", boperand, qbyte);
            }   
            break;
        case 4: 
            sprintf(bldaddr, "%d,%d,%d", da, m, n);
            break;
        case 5:
            sprintf(bldaddr, "%d,%d,%d,%02X", da, m, n, val[2]);
            break;
        case 6:
            sprintf(bldaddr, "%d,%d,%d,%s", da, m, n, boperand);
            break;
        case 7:
            sprintf(bldaddr, "%04X", addr+oplen+val[2]);
            break;
        case 8:
            sprintf(bldaddr, "%s", boperand);   
            break;
        default:
            sprintf(bldaddr, "%s,%s", boperand, aoperand);
            break;
    }                                               
    sprintf(strg, "%s%s", bld, bldaddr);
} 

return -(oplen - 1);
}

/* Symbolic input

   Inputs:
        *cptr   =       pointer to input string
        addr    =       current PC
        *uptr   =       pointer to unit
        *val    =       pointer to output values
        sw      =       switches
   Outputs:
        status  =       error status
*/

int32 parse_sym (char *cptr, int32 addr, UNIT *uptr, uint32 *val, int32 sw)
{
int32 cflag, i = 0, j, r, oplen, addtyp, saveaddr, vptr;
char gbuf[CBUFSIZE];

cflag = (uptr == NULL) || (uptr == &cpu_unit);
while (isspace (*cptr)) cptr++;                         /* absorb spaces */
if ((sw & SWMASK ('A')) || ((*cptr == '\'') && cptr++)) { /* ASCII char? */
    if (cptr[0] == 0) return SCPE_ARG;                  /* must have 1 char */
    val[0] = (unsigned int) cptr[0];
    return SCPE_OK;
}
if ((sw & SWMASK ('C')) || ((*cptr == '"') && cptr++)) { /* ASCII string? */
    if (cptr[0] == 0) return SCPE_ARG;                  /* must have 1 char */
    val[0] = ((unsigned int) cptr[0] << 8) + (unsigned int) cptr[1];
    return SCPE_OK;
}

/* An instruction: get opcode (all characters until null, comma, left paren,
   or numeric (including spaces).
*/

while (1) {
    if (*cptr == ',' || *cptr == '\0' || *cptr == '(' ||
         isdigit(*cptr))
            break;
    gbuf[i] = toupper(*cptr);
    cptr++;
    i++;
}

/* kill trailing spaces if any */
gbuf[i] = '\0';
for (j = i - 1; gbuf[j] == ' '; j--) {
    gbuf[j] = '\0';
}

/* find opcode in table */
for (j = 0; j < nopcode; j++) {
    if (strcmp(gbuf, opcode[j].op) == 0)
        break;
}
if (j >= nopcode)                                       /* not found */
    return SCPE_ARG;

oplen = 2;                                              /* start with op & q */

val[0] = opcode[j].opmask;                              /* store opcode right nybble */

switch (opcode[j].form) {                               /* Get operands based on operand format */
    case 0:                                             /* Single Byte Operand */
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, ',');              /* Get Q Byte */
        sscanf(gbuf, "%x", &r);
        val[1] = r;
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, 0);                /* Get R Byte */
        sscanf(gbuf, "%x", &r);
        val[2] = r;
        oplen = 3;
        val[0] = 0xf0 | opcode[j].opmask;
        break;
    case 1:
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, ',');
        if (opcode[j].opmask == 2 ||
            opcode[j].opmask == 4 ||
            opcode[j].opmask == 5 ||
            opcode[j].opmask == 6) {
            if (isdigit(gbuf[0])) {
                sscanf(gbuf, "%x", &r);
            } else {
                for (i = 0; i < 16; i++) {
                    if (strcmp(gbuf, regname[i]) == 0)
                        break;
                }
                if (i < 16) {
                    r = regcode[i];
                } else {
                    return SCPE_ARG;
                }       
            }   
        } else {
            sscanf(gbuf, "%x", &r);
        }   
        if (r > 255) return SCPE_ARG;
        val[1] = r;
        if (*cptr == ',') cptr++;
        cptr = parse_addr(cptr, gbuf, &addr, &addtyp);
        switch(addtyp) {
            case 0: 
                val[2] = (addr >> 8) & 0x00ff;
                val[3] = addr & 0xff;
                oplen = 4;
                if (opcode[j].group == 1)
                    val[0] = 0xC0 | opcode[j].opmask;
                    else
                    val[0] = 0x30 | opcode[j].opmask;
                break;
            case 1: 
                val[2] = addr & 0xff;
                oplen = 3;
                if (opcode[j].group == 1)
                    val[0] = 0xD0 | opcode[j].opmask;
                    else 
                    val[0] = 0x70 | opcode[j].opmask;
                break;
            case 2:
                val[2] = addr & 0xff;
                oplen = 3;
                if (opcode[j].group == 1)
                    val[0] = 0xE0 | opcode[j].opmask;
                    else
                    val[0] = 0xB0 | opcode[j].opmask;
                break;
            default:
                return SCPE_ARG;
                break;
        }               
        break;
    case 2:
        oplen = 2;
        cptr = parse_addr(cptr, gbuf, &addr, &addtyp);
        switch(addtyp) {
            case 0: 
                val[2] = (addr >> 8) & 0xff;
                val[3] = addr & 0xff;
                oplen += 2;
                vptr = 4;
                val[0] = 0x00 | opcode[j].opmask;
                break;
            case 1: 
                val[2] = addr & 0xff;
                oplen += 1;
                vptr = 3;
                val[0] = 0x40 | opcode[j].opmask;
                break;
            case 2:
                val[2] = addr & 0xff;
                oplen += 1;
                vptr = 3;
                val[0] = 0x80 | opcode[j].opmask;
                break;
            default:
                return SCPE_ARG;
                break;
        }
        if (*cptr == ',') cptr++;
        cptr = parse_addr(cptr, gbuf, &addr, &addtyp);
        switch(addtyp) {
            case 0: 
                val[vptr] = (addr >> 8) & 0xff;
                val[vptr+1] = addr & 0xff;
                oplen += 2;
                break;
            case 1: 
                val[vptr] = addr & 0xff;
                oplen += 1;
                val[0] = 0x10 | val[0];
                break;
            case 2:
                val[vptr] = addr & 0xff;
                oplen += 1;
                val[0] = 0x20 | val[0];
                break;
            default:
                return SCPE_ARG;
                break;
        }                   
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, 0);
        sscanf(gbuf, "%d", &r);
        if (opcode[j].opmask > 9 ||
            opcode[j].opmask == 4 ||
            opcode[j].opmask == 6 ||
            opcode[j].opmask == 7) r--;                 /* special: length -1 */
        val[1] = r;
        if (*cptr == ',') cptr++;
        break;
    case 3:
        saveaddr = addr;
        if (*cptr == ',') cptr++;
        cptr = parse_addr(cptr, gbuf, &addr, &addtyp);
        switch(addtyp) {
            case 0:
                if (opcode[j].group == 0) {             /* Group 0 form 3 is JC with explicit Q */
                    if (*cptr == ',') cptr++;
                    cptr = get_glyph(cptr, gbuf, 0);
                    sscanf(gbuf, "%x", &r);
                    if ((addr - (saveaddr+3)) > 255 || (addr - (saveaddr+3)) < 1)
                         return SCPE_ARG;
                    val[2] = addr - (saveaddr+3);
                    val[1] = r;
                    val[0] = 0xf0 | opcode[j].opmask;
                    oplen = 3;
                    
                } else {
                    val[2] = (addr >> 8) & 0x00ff;
                    val[3] = addr & 0xff;
                    oplen = 4;
                    if (opcode[j].group == 1)
                        val[0] = 0xC0 | opcode[j].opmask;
                        else
                        val[0] = 0x30 | opcode[j].opmask;
                }       
                break;
            case 1: 
                val[2] = addr & 0xff;
                oplen = 3;
                if (opcode[j].group == 1)
                    val[0] = 0xD0 | opcode[j].opmask;
                    else
                    val[0] = 0x70 | opcode[j].opmask;
                break;
            case 2:
                val[2] = addr & 0xff;
                oplen = 3;
                if (opcode[j].group == 1)
                    val[0] = 0xE0 | opcode[j].opmask;
                    else
                    val[0] = 0xB0 | opcode[j].opmask;
                break;
            default:
                return SCPE_ARG;
                break;
        }                   
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, 0);
        sscanf(gbuf, "%x", &r);
        if (r > 255) return SCPE_ARG;
        val[1] = r;
        break;
    case 4:
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, ',');
        sscanf(gbuf, "%d", &r);
        if (r > 15) return SCPE_ARG;
        val[1] = (r << 4) & 0xf0;
        val[0] = 0xf0 | opcode[j].opmask;
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, ',');
        sscanf(gbuf, "%d", &r);
        if (r > 1) return SCPE_ARG;
        val[1] |= (r << 3) & 0x08;
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, 0);
        sscanf(gbuf, "%d", &r);
        if (r > 7) return SCPE_ARG;
        val[1] |= r & 0x07;
        val[2] = 0;
        oplen = 3;
        break;
    case 5:
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, ',');
        sscanf(gbuf, "%d", &r);
        if (r > 15) return SCPE_ARG;
        val[1] = (r << 4) & 0xf0;
        val[0] = 0xf0 | opcode[j].opmask;
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, ',');
        sscanf(gbuf, "%d", &r);
        if (r > 1) return SCPE_ARG;
        val[1] |= (r << 3) & 0x08;
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, ',');
        sscanf(gbuf, "%d", &r);
        if (r > 7) return SCPE_ARG;
        val[1] |= r & 0x07;
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, 0);
        sscanf(gbuf, "%x", &r);
        if (r > 255) return SCPE_ARG;
        val[2] = r;
        oplen = 3;
        break;
    case 6:
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, ',');
        sscanf(gbuf, "%d", &r);
        if (r > 15) return SCPE_ARG;
        val[1] = (r << 4) & 0xf0;
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, ',');
        sscanf(gbuf, "%d", &r);
        if (r > 1) return SCPE_ARG;
        val[1] |= (r << 3) & 0x08;
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, ',');
        sscanf(gbuf, "%d", &r);
        if (r > 7) return SCPE_ARG;
        val[1] |= r & 0x07;
        if (*cptr == ',') cptr++;
        cptr = parse_addr(cptr, gbuf, &addr, &addtyp);
        switch(addtyp) {
            case 0: 
                val[2] = (addr >> 8) & 0x00ff;
                val[3] = addr & 0xff;
                oplen = 4;
                if (opcode[j].group == 1)
                    val[0] = 0xC0 | opcode[j].opmask;
                    else
                    val[0] = 0x30 | opcode[j].opmask;
                break;
            case 1: 
                val[2] = addr & 0xff;
                oplen = 3;
                if (opcode[j].group == 1)
                    val[0] = 0xD0 | opcode[j].opmask;
                    else
                    val[0] = 0x70 | opcode[j].opmask;
                break;
            case 2:
                val[2] = addr & 0xff;
                oplen = 3;
                if (opcode[j].group == 1)
                    val[0] = 0xE0 | opcode[j].opmask;
                    else
                    val[0] = 0xB0 | opcode[j].opmask;
                break;
            default:
                return SCPE_ARG;
                break;
        }                   
        break;
    case 7:
        if (*cptr == ',') cptr++;
        cptr = get_glyph(cptr, gbuf, 0);
        sscanf(gbuf, "%x", &r);
        if ((r - (addr+3)) > 255 || (r - (addr+3)) < 1) return SCPE_ARG;
        val[2] = r - (addr+3);
        val[1] = opcode[j].q;
        val[0] = 0xf0 | opcode[j].opmask;
        oplen = 3;
        break;
        
    case 8:
        if (*cptr == ',') cptr++;
        cptr = parse_addr(cptr, gbuf, &addr, &addtyp);
        switch(addtyp) {
            case 0: 
                val[2] = (addr >> 8) & 0x00ff;
                val[3] = addr & 0xff;
                oplen = 4;
                val[0] = 0xC0 | opcode[j].opmask;
                break;
            case 1: 
                val[2] = addr & 0xff;
                oplen = 3;
                val[0] = 0xD0 | opcode[j].opmask;
                break;
            case 2:
                val[2] = addr & 0xff;
                oplen = 3;
                val[0] = 0xE0 | opcode[j].opmask;
                break;
            default:
                return SCPE_ARG;
                break;
        }                   
        val[1] = opcode[j].q;
        break;
    case 9:
        oplen = 2;
        val[0] = 0;
        cptr = parse_addr(cptr, gbuf, &addr, &addtyp);
        switch(addtyp) {
            case 0: 
                val[2] = (addr >> 8) & 0xff;
                val[3] = addr & 0xff;
                oplen += 2;
                vptr = 4;
                val[0] = 0x00 | opcode[j].opmask;
                break;
            case 1: 
                val[2] = addr & 0xff;
                oplen += 1;
                vptr = 3;
                val[0] = 0x40 | opcode[j].opmask;
                break;
            case 2:
                val[2] = addr & 0xff;
                oplen += 1;
                vptr = 3;
                val[0] = 0x80 | opcode[j].opmask;
                break;
            default:
                return SCPE_ARG;
                break;
        }
        if (*cptr == ',') cptr++;
        cptr = parse_addr(cptr, gbuf, &addr, &addtyp);
        switch(addtyp) {
            case 0: 
                val[vptr] = (addr >> 8) & 0xff;
                val[vptr+1] = addr & 0xff;
                oplen += 2;
                break;
            case 1: 
                val[vptr] = addr & 0xff;
                oplen += 1;
                val[0] = 0x10 | val[0];
                break;
            case 2:
                val[vptr] = addr & 0xff;
                oplen += 1;
                val[0] = 0x20 | val[0];
                break;
            default:
                return SCPE_ARG;
                break;
        }                   
        val[1] = opcode[j].q;
        break;
    default:
        break;
}


return (-(oplen-1));
}

char *parse_addr(char *cptr,  char *gbuf, int32 *addr, int32 *addrtype)
{
int32 nybble = 0;
char temp[32];

cptr = get_glyph(cptr, gbuf, ',');
if (gbuf[0] == '(') {                                   /* XR relative */
    strcpy(temp, gbuf+1);
    sscanf(temp, "%x", addr);
    if (*cptr == ',') cptr++;
    cptr = get_glyph(cptr, gbuf, ',');
    nybble = -1;
    if (strcmp(gbuf, "XR1)") == 0)
        nybble = 1;
    if (strcmp(gbuf, "XR2)") == 0)
        nybble = 2;
} else {                                                /* Direct */
    sscanf(gbuf, "%x", addr);
    nybble = 0; 
}
*addrtype = nybble;
return cptr;
}