-
Notifications
You must be signed in to change notification settings - Fork 1
/
outx.c
1392 lines (1338 loc) · 43.5 KB
/
outx.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
/*
outx.c - Part of macxx, a cross assembler family for various micro-processors
Copyright (C) 2008 David Shepperd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* OUTX
* A collection of output routines for producing extended TEKHEX and
* VLDA output.
* The symbol and object output routines each gather their own type
* of output until either the column outx_width would be exceeded, a type-
* specific break occurs (e.g. change of address in object output),
* or a call is made to flush...(). Symbol and object lines may be
* intermingled, but only at the line level. These are fairly special
* purpose routines, intended to be used with CHKFOR, a combination
* formatter and checksum generator.
*/
#include "outx.h"
#include <errno.h>
#ifdef VMS
#define NORMAL 1
#define ERROR 0x10000004
#else
#define NORMAL 0
#define ERROR 1
#endif
#define DEBUG 9
/*
DATA
*/
FILE *outxabs_fp;
FILE *outxsym_fp;
int outx_lineno;
int outx_width=78; /* object width */
int outx_swidth=78; /* symbol width */
int outx_debug;
int new_identifier=1; /* new identifiers */
/* line buffers for Ext-Tek output */
char *eline; /* pass2 builds expressions here */
static char *sline;
static char *oline;
/* pointers into lines and checksums for lines */
static char *symp=0,*op=0,*maxop=0;
static unsigned int symcs,objcs;
static unsigned long addr;
static unsigned char varcs;
static unsigned char namcs;
char hexdig[] = "0123456789ABCDEF";
static VLDA_abs *vlda_oline;
static VLDA_sym *vlda_sym; /* pointer to line area */
static VLDA_seg *vlda_seg; /* pointer to segment definition */
static struct my_desc *vid;
void outx_init( void )
{
eline = MEM_alloc(MAX_LINE);
sline = MEM_alloc(MAX_LINE);
oline = MEM_alloc(MAX_LINE);
vlda_sym = (VLDA_sym *)sline;
vlda_seg = (VLDA_seg *)sline;
vlda_oline = (VLDA_abs *)oline;
vid = (struct my_desc *)oline;
return;
}
static char *get_symid(SS_struct *sym_ptr,char *s)
{
if (sym_ptr->flg_ident)
{
sprintf(s," %%%d",sym_ptr->ss_ident);
}
else
{
sym_ptr->ss_ident = new_identifier++;
sym_ptr->flg_ident = 1;
#if 0
if (!sym_ptr->flg_global)
{
sprintf(s," {.L%05d}%%%d",sym_ptr->ss_ident,sym_ptr->ss_ident);
}
else
{
#endif
sprintf(s," {%s}%%%d",sym_ptr->ss_string,sym_ptr->ss_ident);
#if 0
}
#endif
}
while (*s++);
--s;
return(s);
}
static short rsize;
static void showBadExpression(const EXP_stk *topLevel)
{
/* We should never get here. But if we do, show something went wrong and prevent a segfault */
EXPR_struct *top_exp;
SS_struct *sym_ptr;
int ii, len;
top_exp = topLevel->stack;
len = snprintf(emsg,sizeof(emsg),"Expression loop. %d terms: ", topLevel->ptr);
for (ii=0; ii < topLevel->ptr; ++ii, ++top_exp)
{
long val = top_exp->expr_value;
switch(top_exp->expr_code)
{
case EXPR_B:
len += snprintf(emsg+len,sizeof(emsg)-len," B");
break;
case EXPR_L:
len += snprintf(emsg+len,sizeof(emsg)-len," L");
break;
case EXPR_SEG:
val += top_exp->expr_seg->rel_offset;
if (val != 0)
len += snprintf(emsg+len,sizeof(emsg)-len, " %%%d %ld +", top_exp->expr_seg->seg_ident, val);
else
len += snprintf(emsg+len, sizeof(emsg)-len," %%%d", top_exp->expr_seg->seg_ident);
break;
case EXPR_SYM:
sym_ptr = top_exp->expr_sym;
if (!sym_ptr->flg_defined || sym_ptr->flg_exprs)
{
char tIdent[32], *ts;
ts = get_symid(sym_ptr,tIdent);
*ts = 0;
len += snprintf(emsg+len,sizeof(emsg)-len," %s", tIdent);
break;
}
val = sym_ptr->ss_value;
/* Fall through to EXPR_VALUE */
case EXPR_VALUE:
len += snprintf(emsg+len,sizeof(emsg)-len," %ld", val);
break;
case EXPR_OPER:
len += snprintf(emsg+len,sizeof(emsg)-len," %c", (char)(val&0xFF));
if ((val&255) == EXPROPER_TST)
len += snprintf(emsg+len,sizeof(emsg)-len,"%c", (char)((val >> 8)&0xFF));
break;
case EXPR_LINK:
len += snprintf(emsg+len, sizeof(emsg)-len," <link to self>");
break;
default:
len += snprintf(emsg+len, sizeof(emsg)-len," <undefined code %d>", top_exp->expr_code);
break;
}
}
len += snprintf(emsg + len, sizeof(emsg) - len, ": Expression ignored");
err_msg(MSG_ERROR,emsg);
}
static char *do_outexp_vlda(EXP_stk *eps,char *s,int *len,const EXP_stk *topLevel)
{
int k;
SS_struct *sym_ptr;
EXPR_struct *exp;
union vexp ve;
k = eps->ptr; /* the length */
exp = eps->stack; /* the stack */
ve.vexp_chp = s; /* point to output array */
for (;k > 0; --k,++exp)
{
switch (exp->expr_code)
{
case EXPR_L: { /* segment length.. */
*ve.vexp_type++ = VLDA_EXPR_L;
*ve.vexp_ident++ = exp->expr_count;
continue;
}
case EXPR_B: { /* segment base */
*ve.vexp_type++ = VLDA_EXPR_B; /* insert type code */
*ve.vexp_ident++ = exp->expr_count;
continue;
}
case EXPR_SEG: { /* segment */
unsigned long cmp;
if (exp->expr_seg->seg_ident > 255)
{
*ve.vexp_type++ = VLDA_EXPR_SYM;
*ve.vexp_ident++ = exp->expr_seg->seg_ident;
}
else
{
*ve.vexp_type++ = VLDA_EXPR_CSYM;
*ve.vexp_byte++ = exp->expr_seg->seg_ident;
}
exp->expr_value += exp->expr_seg->rel_offset;
cmp = (exp->expr_value >= 0) ? exp->expr_value : -exp->expr_value;
if (cmp == 0)
{
continue;
}
else if (cmp < 128)
{
*ve.vexp_type++ = VLDA_EXPR_CVALUE;
*ve.vexp_byte++ = exp->expr_value;
}
else if (cmp < 32768)
{
*ve.vexp_type++ = VLDA_EXPR_WVALUE;
*ve.vexp_word++ = exp->expr_value;
}
else
{
*ve.vexp_type++ = VLDA_EXPR_VALUE;
*ve.vexp_long++ = exp->expr_value;
}
*ve.vexp_type++ = VLDA_EXPR_OPER;
*ve.vexp_oper++ = '+';
*len += 2; /* added 2 terms to expression */
continue;
}
case EXPR_SYM: { /* symbol */
sym_ptr = exp->expr_sym;
if (!sym_ptr->flg_defined || sym_ptr->flg_exprs)
{
if (!sym_ptr->flg_ident)
{
sym_ptr->ss_ident = new_identifier++;
sym_ptr->flg_ident = 1;
}
if (sym_ptr->ss_ident > 255)
{
*ve.vexp_type++ = VLDA_EXPR_SYM;
*ve.vexp_ident++ = sym_ptr->ss_ident;
}
else
{
*ve.vexp_type++ = VLDA_EXPR_CSYM;
*ve.vexp_byte++ = sym_ptr->ss_ident;
}
continue;
}
exp->expr_value = sym_ptr->ss_value;
} /* fall through to EXPR_VALUE */
case EXPR_VALUE: { /* constant */
unsigned long cmp;
cmp = (exp->expr_value >= 0) ? exp->expr_value : -exp->expr_value;
if (cmp == 0)
{
*ve.vexp_type++ = VLDA_EXPR_0VALUE;
continue;
}
else if (cmp < 128)
{
*ve.vexp_type++ = VLDA_EXPR_CVALUE;
*ve.vexp_byte++ = exp->expr_value;
}
else if (cmp < 32768)
{
*ve.vexp_type++ = VLDA_EXPR_WVALUE;
*ve.vexp_word++ = exp->expr_value;
}
else
{
*ve.vexp_type++ = VLDA_EXPR_VALUE;
*ve.vexp_long++ = exp->expr_value;
}
continue;
}
case EXPR_OPER: { /* operator */
*ve.vexp_type++ = VLDA_EXPR_OPER;
*ve.vexp_oper++ = exp->expr_value;
if ((exp->expr_value&255) == EXPROPER_TST)
{
*ve.vexp_oper++ = exp->expr_value >> 8;
}
continue;
}
case EXPR_LINK:
{ /* link to another expression */
EXP_stk *ep;
int ll;
ep = exp->expr_expr;
if ( ep != eps )
{
ll = ep->ptr;
ve.vexp_chp = do_outexp_vlda(ep,ve.vexp_chp,&ll,topLevel);
*len += ll-1; /* account for new items, but take out link */
}
else
{
/* We should never get here. But if we do, show something went wrong and prevent a segfault */
showBadExpression(topLevel);
*ve.vexp_type++ = VLDA_EXPR_VALUE;
*ve.vexp_long++ = 0;
}
continue;
}
default: {
sprintf(emsg,"OUTX (do_expr_vlda)-Undefined expression code: %d",exp->expr_code);
err_msg(MSG_ERROR,emsg);
continue;
} /* --default */
} /* --switch */
} /* --for */
if (eps->tag != 0)
{
unsigned long cmp;
cmp = eps->tag_len;
if (cmp < 2)
{
*ve.vexp_type++ = VLDA_EXPR_1TAG; /* insert tag code */
*ve.vexp_type++ = eps->tag; /* insert tag code */
}
else if (cmp < 128)
{
*ve.vexp_type++ = VLDA_EXPR_CTAG; /* insert tag code */
*ve.vexp_type++ = eps->tag; /* insert tag code */
*ve.vexp_byte++ = eps->tag_len;
}
else if (cmp < 32768)
{
*ve.vexp_type++ = VLDA_EXPR_WTAG; /* insert tag code */
*ve.vexp_type++ = eps->tag; /* insert tag code */
*ve.vexp_word++ = eps->tag_len;
}
else
{
*ve.vexp_type++ = VLDA_EXPR_TAG; /* insert tag code */
*ve.vexp_type++ = eps->tag; /* insert tag code */
*ve.vexp_long++ = eps->tag_len;
}
}
return(ve.vexp_chp); /* exit */
}
static char *do_outexp_ol(EXP_stk *eps,char *s, const EXP_stk *topLevel)
{
int k;
SS_struct *sym_ptr;
EXPR_struct *exp;
k = eps->ptr; /* the length */
exp = eps->stack; /* the stack */
*s = 0; /* null terminate the dst string */
for (;k > 0; --k,++exp)
{
switch (exp->expr_code)
{
char lb;
case EXPR_B:
lb = 'B';
goto lb_common1;
case EXPR_L:
lb = 'L';
lb_common1: sprintf(s," %c %%%d",lb,exp->expr_seg->seg_ident);
goto lb_common2;
case EXPR_SEG:
exp->expr_value += exp->expr_seg->rel_offset;
if (exp->expr_value != 0)
{
sprintf(s," %%%d %ld +",exp->expr_seg->seg_ident,exp->expr_value);
}
else
{
sprintf(s," %%%d",exp->expr_seg->seg_ident);
}
lb_common2: while (*s++);
--s;
continue;
case EXPR_SYM: { /* symbol or segment */
sym_ptr = exp->expr_sym;
if (!sym_ptr->flg_defined || sym_ptr->flg_exprs)
{
/* printf("Doing a getsymid on \"%s\". ident=%d\n",sym_ptr->ss_string,sym_ptr->ss_ident); */
s = get_symid(sym_ptr,s);
continue;
}
exp->expr_value = sym_ptr->ss_value;
}
case EXPR_VALUE: {
sprintf(s," %ld",exp->expr_value);
while (*s++);
--s;
continue;
}
case EXPR_OPER: {
*s++ = ' '; /* a space */
*s++ = exp->expr_value;
if ((exp->expr_value&255) == EXPROPER_TST)
{
*s++ = exp->expr_value >> 8;
}
*s = 0; /* move the null */
continue;
}
case EXPR_LINK:
{ /* link to another expression */
EXP_stk *new_eps = exp->expr_expr;
if ( new_eps != eps )
{
s = do_outexp_ol(new_eps, s, topLevel);
}
else
{
/* We should never get here. But if we do, show something went wrong and prevent a segfault */
showBadExpression(topLevel);
*s++ = '0';
}
*s = 0;
continue;
}
default: {
sprintf(emsg,"OUTX (do_exp_ol)-Undefined expression code: %d",exp->expr_code);
err_msg(MSG_ERROR,emsg);
continue;
} /* --default */
} /* --switch */
} /* --for */
if (eps->tag)
{
if (eps->tag_len == 1 || eps->tag_len == 0)
{
*s++ = ':'; /* follow with tag code */
*s++ = eps->tag; /* and tag */
}
else
{
sprintf(s,":%c %d",eps->tag,eps->tag_len); /* follow with tag and length */
while (*s++);
--s; /* backup to NULL */
}
}
return(s);
}
static int formvar(unsigned long num, char *where );
char *outexp(EXP_stk *eps, char *s, char *wrt, FILE *fp )
{
int len, evenodd;
flushobj(); /* flush the buffer */
if (options[QUAL_BINARY])
{
union vexp ve;
union vexp adj_len;
adj_len.vexp_chp = ve.vexp_chp = s; /* point to output array */
++ve.vexp_len; /* skip the length field */
len = eps->ptr;
ve.vexp_chp = do_outexp_vlda(eps,ve.vexp_chp,&len,eps);
*adj_len.vexp_len = len + ((eps->tag != 0)?1:0); /* expression size (in items) */
if (wrt==0) return(ve.vexp_chp); /* exit */
rsize = ve.vexp_chp-wrt;
#ifndef VMS
fwrite(&rsize,sizeof(short),1,fp);
evenodd = rsize&1;
#else
evenodd = 0;
#endif
fwrite(wrt,(int)rsize+evenodd,1,fp); /* write it */
return 0; /* exit */
}
else
{ /* --vlda */
*s = 0; /* null terminate the dst string */
len = eps->ptr;
s = do_outexp_ol(eps,s,eps);
if (wrt)
{
*s++ = '\n';
*s = 0;
fputs(wrt,fp);
}
} /* --vlda */
return(s);
}
char *outxfer(EXP_stk *eps, FILE *fp)
{
char *s;
eps->tag = 0; /* make sure there's no tag */
if (options[QUAL_BINARY])
{
char *kluge;
int evenodd;
union vexp ve;
ve.vexp_chp = kluge = eline; /* point to output array */
*ve.vexp_type++ = VLDA_XFER; /* set the type */
s = outexp(eps,ve.vexp_chp,(char *)0,(FILE *)0);
rsize = s-kluge;
#ifndef VMS
fwrite(&rsize,sizeof(short),1,fp);
evenodd = rsize&1;
#else
evenodd = 0;
#endif
fwrite(eline,(int)rsize+evenodd,1,fp); /* write it */
return 0; /* exit */
}
else
{ /* --vlda */
strcpy(eline,".start");
s = eline+strlen(eline);
s = outexp(eps,s,(char *)0,(FILE *)0);
*s++ = '\n';
*s = 0;
fputs(eline,fp);
} /* --vlda */
return(0);
}
/* outbstr(from,len)
* Output a byte string, accumulating the checksums for the output record
* and both the even and odd ROMs.
*/
int outbstr(char *from, int len )
{
register unsigned char *rop,*fop;
unsigned char c;
int k,limit,toeol=0;
fop = (unsigned char *)from;
if (!options[QUAL_BINARY])
{
len += len; /* double the input length if ASCII output */
}
else
{
toeol = oline + outx_width - op;
if (len >= toeol-2) flushobj();
}
while (len > 0)
{
if ( op != 0 )
{
toeol = oline + outx_width - op;
if (toeol < 2 )
{
flushobj();
}
}
if ( op == 0 )
{ /* After flushing, or when first... */
switch (output_mode)
{ /* dispatch on output mode */
case OUTPUT_HEX: {
strcpy(oline,"%ll6kk"); /* ...started, set up the buffer */
op = oline+6; /* with header and address */
op += formvar(addr,op);
objcs = varcs + 6;
break; /* out of switch */
}
case OUTPUT_OL: {
op = oline; /* point to start of buffer */
*op++ = '\''; /* start with a single quote */
break;
}
case OUTPUT_OBJ:
vlda_oline->vlda_type = VLDA_TXT;
vlda_oline->vlda_addr = addr; /* load address */
op = oline + sizeof(VLDA_abs);
break;
case OUTPUT_VLDA: {
vlda_oline->vlda_type = VLDA_ABS;
vlda_oline->vlda_addr = addr; /* load address */
op = oline + sizeof(VLDA_abs);
break;
}
}
toeol = oline + outx_width - op;
maxop = op;
}
limit = (toeol < len) ? toeol : len ;
if ((limit &= ~1) == 0) limit = 1;
if (outx_debug > 2)
{
fprintf(stderr,"Loading %d bytes at addr %08lx in rcd %08lX\n",
limit,addr,vlda_oline->vlda_addr);
}
rop = (unsigned char *)op;
len -= limit;
if (!options[QUAL_BINARY])
{
addr += limit/2; /* update address */
for ( ; limit > 0 ; limit -= 2)
{ /* first transfer the high nibble */
k = *fop++;
c = k >> 4;
objcs += c;
*rop++ = hexdig[c];
c = k & 0xF; /* now transfer the low one */
objcs += c;
*rop++ = hexdig[c];
} /* end for */
}
else
{
addr += limit;
for ( ; limit > 0 ; --limit) *rop++ = *fop++;
}
op = (char *)rop;
if (maxop < op) maxop = op;
} /* end while */
return 0;
}
/* outorg(exp_ptr)
* Sets the origin for following outbstr calls. If address
* supplied is different from current address (in addr), flushes object
* buffer.
*/
void outorg(EXP_stk *eps)
{
EXPR_struct *exp;
unsigned long taddr,laddr,address; /* address of end of txt + 1 */
char *s;
eps->tag = 0; /* make sure there's no tag */
exp = eps->stack; /* get ptr to stack */
address = exp->expr_value; /* assume value */
if (macxx_bytes_mau != 1)
{
exp += eps->ptr; /* point to end of stack */
exp->expr_value = macxx_bytes_mau; /* put on value */
(exp++)->expr_code = EXPR_VALUE;
exp->expr_value = '*'; /* put on multiplier */
(exp++)->expr_code = EXPR_OPER;
eps->ptr += 2;
exp = eps->stack;
address *= macxx_bytes_mau;
}
switch (output_mode)
{ /* how to encode it */
case OUTPUT_HEX: { /* absolute tekhex mode */
if ( address == addr) return; /* nothing to do */
flushobj(); /* flush out tekhex files */
break;
}
case OUTPUT_OL: { /* relative .OL output */
flushobj(); /* flush the buffer */
if (eps->ptr < 1)
{
err_msg(MSG_ERROR,"OUTX (outorg)-Too few terms in expression setting .org");
break;
}
strcpy(oline,".org");
s = oline + 4;
s = outexp(eps,s,(char *)0,(FILE *)0);
if (*(s-1) == '+')
{
--s;
}
else
{
*s++ = ' ';
*s++ = '0';
}
*s++ = '\n';
*s = 0;
fputs(oline,outxabs_fp);
break;
}
case OUTPUT_VLDA: { /* absolute VLDA mode */
if ( address == addr) return; /* nothing to do */
if ( op != 0 )
{ /* if there's something in the buffer */
taddr = maxop - oline + sizeof(VLDA_abs) +
(laddr=vlda_oline->vlda_addr);
if ( address > taddr || /* if off the end of the txt rcd */
address < laddr)
{ /* or he's backpatching too far */
flushobj(); /* flush out the current txt record */
}
else
{
op = oline + address - laddr + sizeof(VLDA_abs);
/* otherwise, just backup into txt */
}
}
break; /* fall out of switch */
} /* -- case 0,1 */
case OUTPUT_OBJ: { /* relative VLDA output */
flushobj(); /* flush the buffer */
vlda_oline->vlda_type = VLDA_ORG;
outexp(eps,(char *)&(vlda_oline->vlda_addr),oline,outxabs_fp);
break;
}
}
if (outx_debug > 2) fprintf(stderr,"Changing ORG from %08lX to %08lX\n",addr,address);
addr = address; /* set the new address */
return;
}
/* outbyt(num,where)
* "Output" a byte as two hex digits in buffer at where. return the
* checksum.
*/
int outbyt(unsigned int num, char *where)
{
int chk;
chk = (num >> 4) &0xF;
*where++ = hexdig[chk];
*where = hexdig[num &= 0xF];
return(chk+num);
}
/* formvar(num,where)
* Format a variable length number in buffer pointed to by where,
* returning the string length (number length + 1 for count digit).
* This preliminary is required to check if the number will fit on
* the current line. The variable varcs will contain the contribution
* of this string to the checksum.
*/
static int formvar(unsigned long num, char *where )
{
int count;
register char *vp;
register char c;
unsigned long msk;
vp = where;
/* First, predict where the string will end */
for (count = 0,msk = ~0 ; msk & num ; msk <<= 4) ++count;
if ( count == 0 ) ++count;
vp = &where[count+1];
*vp = 0;
varcs = 0;
/* Now, output characters from lsd to msd */
do
{
c = num & 0xF;
varcs += c;
*--vp = hexdig[(int)c];
} while ( (num >>= 4) != 0);
c = (count &= 0xF);
varcs += c;
*--vp = hexdig[(int)c];
if ( vp != where )
{
fprintf(stderr, "OUTX:formvar- bad estimate, %lX != %lX\n", (unsigned long)vp, (unsigned long)where);
}
return(count+1);
}
/* formsym(name,type,address,where)
* Format a symbol entry in buffer at where, returning the string length
* (name length + 2 for type and count digit + length of address from
* formvar(). This preliminary is required to check if the symbol will
* fit on the current line.
*/
static int formsym(char *name, int type, unsigned long address, char *where)
{
int i;
char *np,c;
namcs = type;
*where = type + '0';
np = &where[2];
for ( i = 0 ; i < 16 ; ++i)
{
if ( (c = name[i]) == 0 ) break;
*np++ = c;
#ifndef isdigit
if ( c >= '0' && c <= '9' ) c -= '0';
else if ( c >= 'A' && c <= 'Z' ) c -= ('A' - 10);
else if ( c >= 'a' && c <= 'z' ) c -= ('a' - 40);
#else
if ( isdigit(c) ) c -= '0';
else if ( isupper(c) ) c -= ('A' - 10);
else if ( islower(c) ) c -= ('a' - 40);
#endif
else if ( c == '.' ) c = 38;
else if ( c >= '_' ) c = 39;
namcs += c;
}
if ( i == 0 )
{
fprintf(stderr,"OUTX:formsym- line %d: Bad symbol %s\n",outx_lineno,name);
return(0);
}
namcs += ( i &= 0xF );
where[1] = hexdig[i];
i = formvar(address,np);
namcs += varcs;
np[i] = 0;
return((np-where) + i);
}
static char *sline_ptr;
int outsym(SS_struct *sym_ptr,int mode)
{
int len;
char save1,save2,*ssp;
/* If the first time through, set up the head of a block, and init
* checksum and length
*/
if ( symp == 0 )
{
sline_ptr = sline;
if (mode == OUTPUT_VLDA) *sline_ptr++ = VLDA_TPR;
strcpy(sline_ptr,"%ll3kk2S_"); /* length will overwrite ll, checksum kk */
symcs = 0x48;
symp = sline_ptr+9;
} /* --symp == 0 */
len = formsym(sym_ptr->ss_string,1,(unsigned long)sym_ptr->ss_value,symp);
if ( symp - sline + len >= outx_swidth)
{
/* We can't fit the current symbol on the line, so we stop here and output
* the line, then reset to initial conditions plus new symbol.
*/
symcs += outbyt((unsigned int)(symp - (sline_ptr+1)),sline_ptr+1);
outbyt(symcs,sline_ptr+4);
ssp = symp;
save1 = *ssp++;
save2 = *ssp;
if (mode == OUTPUT_VLDA)
{
char *kluge = sline;
int evenodd;
*symp = '\r';
*ssp = '\n';
#ifndef VMS
rsize = ssp-kluge+1;
fwrite(&rsize,sizeof(short),1,outxsym_fp);
evenodd = (int)(ssp-sline+1)&1;
#else
evenodd = 0;
#endif
fwrite(sline,(int)(ssp-sline+1)+evenodd,1,outxsym_fp); /* write a transparent rcd */
}
else
{
*symp = '\n';
*ssp = 0;
fputs(sline,outxsym_fp);
}
*symp = save1; /* replace our NULL (repair the patch)*/
*ssp = save2;
strcpy(sline_ptr+9,symp);
symcs = 0x48 + namcs;
symp = sline_ptr + 9 + len;
}
else
{ /* --symp < outx_swidth */
symcs += namcs;
symp += len;
} /* --symp < outx_swidth */
return 0; /* done */
}
void outsym_def(SS_struct *sym_ptr,int mode)
{
register char *s,*name=sym_ptr->ss_string;
switch (mode)
{
case OUTPUT_VLDA: /* vlda absolute */
case OUTPUT_HEX: { /* absolute output (tekhex/non-relative) */
outsym(sym_ptr,mode);
return; /* easy out */
}
case OUTPUT_OL: { /* relative output (.OL format) */
if (!sym_ptr->flg_defined)
{
strcpy(sline,".ext ");
}
else
{
strcpy(sline,sym_ptr->flg_global ? ".defg" : ".defl");
}
s = get_symid(sym_ptr,sline+5); /* get an ASCII ID */
if (!sym_ptr->flg_defined)
{
strcpy(s,"\n");
}
else
{
if (sym_ptr->flg_abs)
{
sprintf(s," %ld\n",sym_ptr->ss_value);
}
else
{
if (sym_ptr->flg_exprs)
{
/* printf("Doing %d term expression defining \"%s\"\n",sym_ptr->ss_exprs->ptr,sym_ptr->ss_string); */
s = outexp(sym_ptr->ss_exprs,s,(char *)0,(FILE *)0);
*s++ = '\n';
*s = 0;
}
else
{
sprintf(s," %%%d %ld +\n",sym_ptr->ss_seg->seg_ident,sym_ptr->ss_value);
}
}
}
fputs(sline,outxsym_fp);
return;
}
case OUTPUT_OBJ: { /* vlda relative */
char *kluge = sline;
int flg, evenodd;
vlda_sym->vsym_rectyp = VLDA_GSD; /* signal its a GSD record */
flg = VSYM_SYM; /* it's a symbol */
if (sym_ptr->flg_defined) flg |= VSYM_DEF; /* symbols may be defined */
if (sym_ptr->flg_exprs) flg |= VSYM_EXP;
if (sym_ptr->flg_abs) flg |= VSYM_ABS; /* may-be abs */
if (!sym_ptr->flg_global) flg |= VSYM_LCL; /* may be local */
vlda_sym->vsym_flags = flg;
if (!sym_ptr->flg_ident)
{
sym_ptr->ss_ident = new_identifier++;
sym_ptr->flg_ident = 1;
}
vlda_sym->vsym_ident = sym_ptr->ss_ident;
vlda_sym->vsym_value = sym_ptr->ss_value;
s = sline + sizeof(VLDA_sym);
if (flg&VSYM_DEF)
{
if (flg&VSYM_EXP)
{
vlda_sym->vsym_eoff = s - kluge;
s = outexp(sym_ptr->ss_exprs,s,(char *)0,(FILE *)0);
}
else
{
if ((flg&VSYM_ABS) == 0)
{
union vexp ve;
unsigned long cmp;
vlda_sym->vsym_flags |= VSYM_EXP;
vlda_sym->vsym_eoff = s - kluge;
ve.vexp_chp = s;
cmp = (sym_ptr->ss_value >= 0) ? sym_ptr->ss_value : -sym_ptr->ss_value;
*ve.vexp_len++ = (cmp != 0) ? 3 : 1;
if (sym_ptr->ss_seg->seg_ident > 255)
{
*ve.vexp_type++ = VLDA_EXPR_SYM;
*ve.vexp_ident++ = sym_ptr->ss_seg->seg_ident;
}
else
{
*ve.vexp_type++ = VLDA_EXPR_CSYM;
*ve.vexp_byte++ = sym_ptr->ss_seg->seg_ident;
}
if (cmp != 0)
{
if (cmp < 128)
{
*ve.vexp_type++ = VLDA_EXPR_CVALUE;
*ve.vexp_byte++ = sym_ptr->ss_value;
}
else if (cmp < 32768)
{
*ve.vexp_type++ = VLDA_EXPR_WVALUE;
*ve.vexp_word++ = sym_ptr->ss_value;
}
else
{
*ve.vexp_type++ = VLDA_EXPR_VALUE;
*ve.vexp_long++ = sym_ptr->ss_value;
}
*ve.vexp_type++ = VLDA_EXPR_OPER;
*ve.vexp_oper++ = '+';
}
s = ve.vexp_chp;
}
}
}
vlda_sym->vsym_noff = s-kluge;
while ((*s++ = *name++) != 0); /* copy in the symbol name string */
#ifndef VMS
rsize = s-kluge;
fwrite(&rsize,sizeof(short),1,outxsym_fp);
evenodd = rsize&1;
#else
evenodd = 0;
#endif
fwrite(sline,(int)(s-sline)+evenodd,1,outxsym_fp); /* write it */
return; /* done */
} /* --case 2,3 */