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

Subversion Repositories System09

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

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

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

powered by: WebSVN 2.1.0

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