OpenCores
URL https://opencores.org/ocsvn/System09/System09/trunk

Subversion Repositories System09

[/] [System09/] [trunk/] [Tools/] [as09/] [as.c] - Blame information for rev 218

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 83 davidgb
#include <stdlib.h>
2
#include <string.h>
3
 
4
char mapdn();
5
char *alloc();
6
/*
7
* as --- cross assembler main program
8
 
9
Note: Compile with define DEBUG for function module debug
10
statements.  Compile with define DEBUG2 for version 2 debug
11
statements only.  Compile with define IBM to use original, non-
12
Amiga, non-MAC, non-UNIX fgets() function.  Amiga version will
13
accept IBM generated source code but not the other way around.
14
Note added version (TER) 2.01 19 June 1989.
15
 
16
Additional modifications to cleanup output listing made by
17
Donald W. Coates 1/5/92. Version changed to 2.10
18
*/
19
main(argc,argv)
20
int argc;
21
char **argv;
22
{
23
        char **np;
24
        char *i;
25
        FILE *fopen();
26
        int j = 0;
27
 
28
        printf("Assembler release DWC_2.0 version 2.11\n");
29
        printf("May 6, 2004 (c) Motorola (free ware)\n");
30
 
31
        if(argc < 2){
32
                printf("Usage: %s [files]\n",argv[j]);
33
                exit(1);
34
        }
35
        Argv = argv;
36
        initialize();
37
        while ((j<argc) && (*argv[j] != '-'))
38
                j++;
39
        N_files = j-1;
40
        if (j < argc )
41
        {
42
                argv[j]++;
43
                while (j<argc)
44
                {
45
                        for (i = argv[j]; *i != 0; i++)
46
                                if ((*i <= 'Z') && (*i >= 'A'))
47
                                        *i = *i + 32;
48
                        if (strcmp(argv[j],"l")==0)
49
                                Lflag = 1;
50
                        else if (strcmp(argv[j],"nol")==0)
51
                                Lflag = 0;
52
                        else if (strcmp(argv[j],"c")==0)
53
                                Cflag = 1;
54
                        else if (strcmp(argv[j],"noc")==0)
55
                                Cflag = 0;
56
                        else if (strcmp(argv[j],"s")==0)
57
                                Sflag = 1;
58
                        else if (strcmp(argv[j],"cre")==0)
59
                                CREflag = 1;
60
                        else if (strcmp(argv[j],"crlf")==0) /* test for crlf option */
61
                                CRflag = 1;                       /* add <CR><LF> to S record */
62
                        /* ver TER_1.1 June 3, 1989 */
63
                        else if (strcmp(argv[j],"nnf")==0)  /* test for nnf option */
64
                                nfFlag=0;                         /* nfFlag=1 means number INCLUDE */
65
                        /* separately. ver TER_2.0 6/17/89 */
66
                        else if (strcmp(argv[j],"p50")==0){ /* page every 50 lines */
67
                                Pflag50 = 1;                      /* ver (TER) 2.01 19 Jun 89 */
68
                                Pflag75 = 0;
69
                        }                                                                       /* separately. ver TER_2.0 6/17/89 */
70
                        else if (strcmp(argv[j],"p75")==0){ /* page every 75 lines */
71
                                Pflag50 = 0;                      /* ver (DWC) 2.10 8 Oct 2001 */
72
                                Pflag75 = 1;
73
                        }
74
                        j++;
75
                }
76
        }
77
        root = NULL;
78
 
79
        Cfn = 0;
80
        np = argv;
81
        Line_num = 0; /* reset line number */
82
        while( ++Cfn <= N_files )
83
                if((Fd = fopen(*++np,"r")) == NULL)
84
                        printf("as: can't open %s\n",*np);
85
                else{
86
                        make_pass();
87
                        fclose(Fd);
88
                }
89
                if( Err_count == 0 ){
90
                        Pass++;
91
                        re_init();
92
                        Cfn = 0;
93
                        np = argv;
94
                        Line_num = 0;
95
                        FdCount=0;            /* Resets INCLUDE file nesting ver TER_2.0 6/17/89 */
96
                        while( ++Cfn <= N_files)
97
                                if((Fd = fopen(*++np,"r")) != NULL)
98
                                {
99
                                        make_pass();
100
                                        fclose(Fd);
101
                                }
102
                                printf ("Program + Init Data = %d bytes\n",F_total); /* print total bytes */
103
                                printf ("Error count = %d\n",Err_count);     /* rel TER_2.0 */
104
                                if (Sflag == 1)
105
                                {
106
                                        printf ("\n\f\n");
107
                                        stable (root);
108
                                }
109
                                if (CREflag == 1)
110
                                {
111
                                        printf ("\n\f\n");
112
                                        cross (root);
113
                                }
114
                                if (CRflag == 1)
115
                                        fprintf(Objfil,"S9030000FC%c\n",CR); /* ver TER_1.1 print w <CR> */
116
                                else
117
                                        fprintf(Objfil,"S9030000FC\n"); /* at least give a decent ending */
118
                                fclose(Objfil);                    /* close file correctly ver TER_1.1 */
119
                }
120
                else                                /* just note errors, TER_2.0 */
121
                {
122
                        printf ("Program + Init Data = %d bytes\n",F_total); /* print total bytes */
123
                        printf ("Error count = %d\n",Err_count);
124
                }
125
                fwd_done();
126
                exit(Err_count);        /* neat for UNIX cuz can test return value in script
127
                                                                but on other systems like Amiga, mostly just makes
128
                                                                further script statements fail if >10, else
129
                                                                nothing.  Therefore, printed out byte count
130
                                                                and error level. ver (TER) 2.02 19 Jun 89 */
131
}
132
 
133
initialize()
134
{
135
        FILE *fopen();
136
        int i = 0;
137
 
138
#ifdef DEBUG
139
        printf("Initializing\n");
140
#endif
141
        Err_count = 0;
142
        Pc   = 0;
143
        Pass   = 1;
144
        Lflag   = 0;
145
        Cflag   = 0;
146
        Ctotal   = 0;
147
        Sflag   = 0;
148
        CREflag   = 0;
149
        N_page   = 0;
150
        Line[MAXBUF-1] = NEWLINE;
151
 
152
        strcpy(Obj_name,Argv[1]); /* copy first file name into array */
153
        do {
154
                if (Obj_name[i]=='.')
155
                        Obj_name[i]=0;
156
        }
157
        while (Obj_name[i++] != 0);
158
        strcat(Obj_name,".s19");  /* append .out to file name. */
159
        if( (Objfil = fopen(Obj_name,"w")) == NULL)
160
                fatal("Can't create object file");
161
        fwdinit();                /* forward ref init */
162
        localinit();              /* target machine specific init. */
163
}
164
 
165
re_init()
166
{
167
#ifdef DEBUG
168
        printf("Reinitializing\n");
169
#endif
170
        Pc = 0;
171
        E_total = 0;
172
        P_total = 0;
173
        Ctotal = 0;
174
        N_page = 0;
175
        fwdreinit();
176
}
177
 
178
make_pass()
179
{
180
#ifdef IBM
181
        char *fgets();         /* the original line */
182
#else
183
        char *FGETS();         /* use own FGETS which is rewrite of lib function */
184
        /* such that it discards <CR> so can read code */
185
        /* generated on IBM */
186
        /* June 3, 1989 rev TER_1.1 */
187
#endif
188
 
189
#ifdef DEBUG
190
        printf("Pass %d\n",Pass);
191
#endif
192
 
193
#ifndef IBM
194
        while( FGETS(Line,MAXBUF-1,Fd) != (char *)NULL ){  /* changed to FGETS */
195
                /* which does not pass on <CR>  June 3, 1989 */
196
                /* rev TER_1.1 */
197
#else
198
        while( fgets(Line,MAXBUF,Fd) != (char *)NULL ){
199
 
200
#endif
201
 
202
                Line_num++;
203
                P_force = 0;          /* No force unless bytes emitted */
204
                N_page = 0;
205
                if(parse_line())
206
                        process();
207
                if(Pass == 2 && Lflag && !N_page)
208
                        print_line();
209
                P_total = 0;          /* reset byte count */
210
                Cycles = 0;           /* and per instruction cycle count */
211
        }
212
        f_record();
213
}
214
 
215
 
216
/*
217
* parse_line --- split input line into label, op and operand
218
*/
219
parse_line()
220
{
221
        register char *ptrfrm = Line;
222
        register char *ptrto = Label;
223
        char *skip_white();
224
 
225
        if( *ptrfrm == '*' || *ptrfrm == '\n' || *ptrfrm == ';' )
226
                /* added check for ';' ver TER_1.1 */
227
                /* June 3, 1989 */
228
                return(0);                    /* a comment line */
229
 
230
        while( delim(*ptrfrm)== NO )   /* parsing Label */
231
                *ptrto++ = *ptrfrm++;
232
        if(*--ptrto != ':')ptrto++;    /* allow trailing : */
233
        *ptrto = EOS;
234
 
235
        ptrfrm = skip_white(ptrfrm);
236
        if (*ptrfrm == ';') {          /* intercept comment after label, ver TER_2.0 */
237
                *Op = *Operand = EOS;   /* comment, no Opcode or Operand */
238
                return(1); }
239
        ptrto = Op;
240
 
241
        while( delim(*ptrfrm) == NO)   /* parsing Opcode */
242
                *ptrto++ = mapdn(*ptrfrm++);
243
        *ptrto = EOS;
244
 
245
        ptrfrm = skip_white(ptrfrm);
246
        if (*ptrfrm == ';') {          /* intercept comment, ver TER_2.0 */
247
                *Operand = EOS;         /* comment, no Operand */
248
                return(1); }
249
 
250
        ptrto = Operand;
251
        while( (*ptrfrm != NEWLINE) && (*ptrfrm != ';')) /* ver TER_2.0 */
252
                *ptrto++ = *ptrfrm++; /* kill comments */
253
        *ptrto = EOS;
254
 
255
#ifdef DEBUG
256
        printf("Label-%s-\n",Label);
257
        printf("Op----%s-\n",Op);
258
        printf("Operand-%s-\n",Operand);
259
#endif
260
        return(1);
261
}
262
 
263
/*
264
* process --- determine mnemonic class and act on it
265
*/
266
process()
267
{
268
        register struct oper *i;
269
        struct oper *mne_look();
270
 
271
        Old_pc = Pc;                   /* setup `old' program counter */
272
        Optr = Operand;                /* point to beginning of operand field */
273
 
274
        if(*Op==EOS){                  /* no mnemonic */
275
                if(*Label != EOS)
276
                        install(Label,Pc);
277
        }
278
        else if( (i = mne_look(Op))== NULL)
279 142 davidgb
                printf("Unrecognized Mnemonic '%s'\n",Op);
280 83 davidgb
        else if( i->class == PSEUDO )
281
                do_pseudo(i->opcode);
282
        else{
283
                if( *Label )install(Label,Pc);
284
                if(Cflag)Cycles = i->cycles;
285
                do_op(i->opcode,i->class);
286
                if(Cflag)Ctotal += Cycles;
287
        }
288
}
289
 
290
#ifndef IBM
291
char *FGETS(s, n, iop)  /* get at most n chars from iop */
292
/* Added rev TER_1.1 June 3, 1989 */
293
/* Adapted from Kernighan & Ritchie */
294
/* An fgets() that is IBM proof. Not needed if
295
this IS an IBM */
296
 
297
char *s;
298
int n;
299
register FILE *iop;
300
{
301
        register int c;
302
        register char *cs;
303
 
304
        cs=s;
305
        while (--n > 0 && (c = getc(iop)) != EOF) /* read chars if not file end */
306
        {
307
                if ((*cs = c) != CR) cs++; /* incr buffer pointer if not CR */
308
                /* If CR, leave to be written over */
309
                if (c == '\n')
310
                        break;
311
        }
312
        *cs = '\0';   /* replace NEWLINE with NULL as in standard fgets() */
313
        return((c == EOF && cs == s) ? NULL : s);  /* return NULL if this is EOF */
314
}
315
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.