File: assemble.c

package info (click to toggle)
alsa-tools 1.0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,464 kB
  • ctags: 5,787
  • sloc: ansic: 23,609; cpp: 15,057; sh: 12,305; pascal: 1,140; asm: 1,053; xml: 814; makefile: 733; python: 250
file content (438 lines) | stat: -rw-r--r-- 13,382 bytes parent folder | download | duplicates (8)
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
/***************************************************************************
                          assemble.c  -  Assembles the parsed lines
                             -------------------
    Date                : May 24 2000
    Copyright            : (C) 2000 by Daniel Bertrand
    Email                : d.bertrand@ieee.ca
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include <ctype.h>


#include"types.h"
#include"proto.h"

extern int dbg_opt;
extern FILE *listfile;
extern char *listing;
char type_strings[GPR_TYPE_EQUATE+1][20]={
"Input",
"Output",
"Constant",
"Static",
"Dynamic",
"Control",
"Tram Data Reg",
"Tram Address/Read",
"Tram Address/Write",
"Macro arg",
"Equate"
};

void op(int op, int z,int  w,int  x,int  y)
{
	int  w0, w1;
        extern int dsp_code[DSP_CODE_SIZE];
        extern int ip;
        extern char op_codes[35][9];
	extern char listtemp[60];
	if (ip >= 0x200)
		as_exit("to many instructions");
	if (op >= 0x10 || op < 0x00)
		as_exit("illegal op code");
        
        //check if gpr is valid, optional do additional modifications
	z=declared(z,1);
	declared(w,2);
	declared(x,3);
	declared(y,4);

        if ( (dbg_opt & DBG_INSTR) !=0  )
		printf( "0x%03x\t%s(%d)  \t0x%03x,0x%03x,0x%03x,0x%03x\n",2*ip,op_codes[op],op,z,w,x,y);
	if(listing)
		sprintf(listtemp, "0x%03x   %-9s(%02d)   0x%03x,0x%03x,0x%03x,0x%03x",2*ip,op_codes[op],op,z,w,x,y);
	
	w0 = (x << 10) | y;
	w1 = (op << 20) | (z << 10) | w;
	dsp_code[ip * 2] = w0;
	dsp_code[ip * 2 + 1] = w1;
	ip++;

}

int declared(int operand,int i){

        struct sym *sym;
        extern struct list_head sym_head;
        struct list_head *entry;
 
        
       
        if ((operand < 0x040)||(operand >= 0x400)){
                printf("** Assembler Error with Operand %d:0x%x\n",i,operand);
                as_exit("Operand has value out of range");
        }
        
        if((operand < 0x400) && operand >= 0x100)
        {
                list_for_each(entry,&sym_head){
                        sym=list_entry(entry,struct sym,list);
                        if( (sym->data.address == operand ) && sym->type!=GPR_TYPE_EQUATE){
				if( ( sym->type==GPR_TYPE_CONSTANT) && (i==1) ){
					printf("** Assembler Error with Operand %d:0x%x\n",i,operand);
					as_exit("** Error: Destination register is a read-only constant");
				}
				
				else if(sym->type!=GPR_TYPE_INPUT)
                                        return(operand);
                                else
                                        return( i==1? operand + 1 : operand);
                        }
                }
                
               
        }
        else if(operand<0x100)
                return(operand);
        
         printf("** Assembler Error with Operand %d:0x%x\n",i,operand);
         as_exit("Operand address is undeclared");
         return(0);
}

//each operand will be something like :  ,  sym1 + sym2 * sym3 ,
//we will recursively decode each symbol and perform proper operations:
int arg_decode(char *operand, int prev_val)
{
        int value;
        char oper='0';

        
	//Nothing: 
        if(operand==NULL)
                as_exit("Parse Error: missing operand(s)");

        
        if(*operand==','||*operand=='\n'||*operand=='\0')
                return(prev_val);

 
        //skip over leading blanks(if any):
        advance_over_whites(operand);
        
        if(*operand==','||*operand=='\n' ||*operand=='\0')
                return(prev_val);
        
        //get the operator:
        if(*operand=='+' || *operand=='-' || *operand=='/' || *operand== '*'){
                oper=*operand;
                operand++;
        }
        
        //skip over any blanks after the oper
        advance_over_whites(operand);
        
        //decode the symbol/value:
        value=arg_decode2(operand);

        //advance to next symbol
        while( *operand!='+' && *operand!='-' && *operand!='/' && *operand!= '*'  && *operand != '\0'  &&*operand!=',' &&*operand!='\n')
                operand++;
        
        switch (oper){
                
        case '+':
                return(arg_decode(operand,prev_val+value));
        case '-':
                return(arg_decode(operand,prev_val-value));
        case '/':
                return(arg_decode(operand,prev_val/value));
        case '*':
                return(arg_decode(operand,prev_val*value));       
        default:
                return(arg_decode(operand,value));
        
        }
        
}

//this function does argument decoding
int arg_decode2(char *operand)
{
        extern int ip,ds_addr;
        extern unsigned int macro_depth;
        struct sym *sym;
        extern struct list_head sym_head;
        struct list_head *entry;
        //printf("operand:%s\n",operand);

	
        if(operand[0]=='.' &&isalpha(operand[1])){
                add_symbol(operand,GPR_TYPE_STATIC, ds_addr++, -(long)ip);
                return(ds_addr-1);
         }

        
        // Hex
        if((char)(*operand)=='$')
                return((int)strtol(operand+1,NULL,16));
	// Octal 
        if((char)(*operand)=='@')
                return((int)strtol(operand+1,NULL,8));
        // Binary:
        if((char)(*operand)=='%')
                return((int)strtol(operand+1,NULL,2));
	// Decimal:
        if( (operand[0] >= '0' && operand[0] <='9') ||operand[0]=='-')
                return((int)strtol(operand,NULL,10));

        
        
        //Symbol:
        list_for_each(entry,&sym_head){
                sym=list_entry(entry,struct sym,list);
                if(symcmp(sym->data.name,operand)==0){
                        if(sym->type!=TYPE_MACRO_ARG)
                                return(sym->data.address);
                        else if(sym->data.value==(macro_depth))
                                return(sym->data.address);     
                        
                }
        }
               
        
        
        printf("Parse error with operand: \"");
        while(!symend(operand))
                printf("%c",*(operand++));
        printf("\"\n");
        as_exit("Bad operand");
        
        //printf("** Parse error with operand: \"%s\"\n",operand);
        as_exit("\"\nOperand isn't a defined symbol or value");
        return(0);
}


#define FACTOR 0x7fffffff
#define SAMP_FREQ 48000
//used by the DC operation to get a long int:
long arg2long(char *operand){


	//Nothing: 	
        if(operand==NULL)
                as_exit("Parse Error: missing operand(s)");
	
        advance(operand);

        //Fractional ( -1 <= operand <= 1 )
        if(operand[0]=='#')
                return((long)(atof(operand+1)*FACTOR));
        // Time value
        if(operand[0]=='&')
                 return((long)(atof(operand+1)*48000));
        // Hex:
        if((char)(*operand)=='$')
                return(strtoul(operand+1,NULL,16));
        // Binary:
        if((char)(*operand)=='%')
                return(strtoul(operand+1,NULL,2));
	// Octal:
        if((char)(*operand)=='@')
                return(strtoul(operand+1,NULL,8));
	// Decimal:
        if( (operand[0] >= '0' && operand[0] <='9') || operand[0]=='-' || operand[0]=='.'){
                if(atof(operand)<1 && atof(operand)>-1)
                        return((long)(atof(operand)*FACTOR));
                else
                        return(strtol(operand,NULL,10));
        }
        
        
        printf("Parse error with operand:\"%s\"\n",operand);
        //        while(!symend(operand))
        //      printf("%c",*operand);
        //printf("\"\n");
        as_exit("Bad operand");
        
        return(0);
}
void update_symbol(char *name,u16 type,u16 address,u32 value){
        struct sym *sym;
        
        
        switch(type){
                
        case TYPE_MACRO_ARG:
               
                if( issymbol(name,&sym) == -1 ){
                        add_symbol(name,type,address,value);
                        return;
                }

                if(sym->type!=TYPE_MACRO_ARG){
                        printf("Error: with argument:%s",name);
                        as_exit("Error:symbol is already defined");
                }
                sym->data.address=address;
                break;
        default:
                if( issymbol(name,&sym) == -1 ){
                        add_symbol(name,type,address,value);
                        return;
        }
                break;  
        }
        
}




void add_symbol(char *name, u16 type, u16 address, u32 value)
{
       
        extern int gpr_input_count,gpr_output_count,gpr_static_count,gpr_dynamic_count,gpr_control_count,gpr_constant_count;
        struct sym *sym;
        struct tram *tmp_ptr;
        extern struct list_head sym_head;
        extern struct delay tram_delay[MAX_TANK_ADDR];
        extern struct lookup tram_lookup[MAX_TANK_ADDR];
	int tmp;
	
        
        if(name==NULL)
                as_exit("Parse Error: This directive requires a label");

        if(symcmp(name,NO_SYM)!=0 &&type== GPR_TYPE_CONSTANT){
		if(issymbol(name,&sym)==0){	
			if(sym->data.value != value)
				as_exit("Error: Constant redeclared as another value");
			else
				
				return;
			}
	}
	
	
        if(symcmp(name,NO_SYM)!=0 && type!=TYPE_MACRO_ARG)
        {
                if(issymbol(name,&sym)!=-1)
                        as_exit("Parse Error: Symbol is already defined");
                if(ismacro(name)!=-1)
                        as_exit("Parse Error: Symbol is already defined as a macro");
                if(isalpha(*name)==0 && name[0]!='.')
                        as_exit("Parse Error: Symbol must start with a alpha character (a-z)");
        }
       
        switch(type){
        case GPR_TYPE_CONTROL:
                sym=(struct sym *)malloc(sizeof(struct control));
                list_add_tail(&sym->list, &sym_head);
                break;
        case TYPE_TRAM_ADDR_READ:
        case TYPE_TRAM_ADDR_WRITE:
                sym=(struct sym *)malloc(sizeof(struct tram));
                list_add_tail(&sym->list, &sym_head);
                
                //if ID is that of a delay:
                if((tmp=((struct sym * ) sym->list.prev)->data.value)>0xff){
                        tmp=tmp-0x100;
                        list_add_tail(&(((struct tram *)sym)->tram) , &(tram_delay[tmp].tram) );
                        if(type== TYPE_TRAM_ADDR_READ)
                                tram_delay[tmp].read++;   
                        else
                                tram_delay[tmp].write++;
                }else{
                        tmp_ptr=(struct tram *)sym;
                        list_add_tail(&(((struct tram *)sym)->tram) , &(tram_lookup[tmp].tram) );
			tmp_ptr=(struct tram *)sym;
                                if(type== TYPE_TRAM_ADDR_READ)
                                tram_lookup[tmp].read++;   
                        else
                                tram_lookup[tmp].write++;
                }
                break;
        default:
		
		sym=(struct sym *)malloc(sizeof(struct sym));
                list_add_tail(&sym->list, &sym_head);
		
        }
        
        
	
        symcpy(sym->data.name,name);
        sym->data.address=address;
        sym->type=type;
        sym->data.value=value;	
	//GPR debugging:
	if((dbg_opt&DBG_GPR) && type<=GPR_TYPE_CONTROL)
		printf("GPR:    %-16s 0x%03x Value=0x%08x, Type: %s\n",name,address,value,type_strings[type] );
	
	//tram debugging:
	else if((dbg_opt&DBG_TRAM && type ==  TYPE_TRAM_DATA))
		printf("TRAM Access: %-16s",name);
	else if((dbg_opt&DBG_TRAM && type ==  TYPE_TRAM_ADDR_WRITE))
		printf(", type: Write, using 0x%03x/0x%03x, offset:0x%07x",address,address-0x100,value );
	else if((dbg_opt&DBG_TRAM && type ==  TYPE_TRAM_ADDR_READ))
		printf(", type: Read,  using 0x%03x/0x%03x, offset:0x%07x",address,address-0x100,value );
	//General Symbol debugging:		
	else if((dbg_opt&DBG_SYM )){
		printf("symbol: %-16s 0x%03x Type: %s\n",name,address,type_strings[type]);
	}
	

	switch(type){
        case TYPE_MACRO_ARG:
                return;
        case GPR_TYPE_INPUT:
                gpr_input_count++;
                return;
        case  GPR_TYPE_OUTPUT:
                gpr_output_count++;
                return;
        case GPR_TYPE_STATIC:
                gpr_static_count++;
                return;
        case GPR_TYPE_DYNAMIC:
                gpr_dynamic_count++;
                return;
        case GPR_TYPE_CONTROL:
                gpr_control_count++;
                return;
	case GPR_TYPE_CONSTANT:
		gpr_constant_count++;
		return;
        default:
                return;
        }
        
}