Home | History | Annotate | Line # | Download | only in siop
ncr53cxxx.c revision 1.11
      1 /*	$NetBSD: ncr53cxxx.c,v 1.11 2003/04/06 09:50:29 tsutsui Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995,1999 Michael L. Hitch
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Michael L. Hitch.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*	ncr53cxxx.c	- SCSI SCRIPTS Assembler		*/
     34 
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 #include <time.h>
     39 
     40 #ifndef AMIGA
     41 #define strcmpi	strcasecmp
     42 #endif
     43 
     44 #define	MAXTOKENS	16
     45 #define	MAXINST		1024
     46 #define	MAXSYMBOLS	128
     47 
     48 struct {
     49 	int	type;
     50 	char	*name;
     51 } tokens[MAXTOKENS];
     52 int	ntokens;
     53 int	tokenix;
     54 
     55 void	f_proc (void);
     56 void	f_pass (void);
     57 void	f_list (void);		/* ENTRY, EXTERNAL label list */
     58 void	f_define (void);	/* ABSOLUTE, RELATIVE label list */
     59 void	f_move (void);
     60 void	f_jump (void);
     61 void	f_call (void);
     62 void	f_return (void);
     63 void	f_int (void);
     64 void	f_intfly (void);
     65 void	f_select (void);
     66 void	f_reselect (void);
     67 void	f_wait (void);
     68 void	f_disconnect (void);
     69 void	f_set (void);
     70 void	f_clear (void);
     71 void	f_load (void);
     72 void	f_store (void);
     73 void	f_nop (void);
     74 void	f_arch (void);
     75 
     76 struct {
     77 	char	*name;
     78 	void	(*func)(void);
     79 } directives[] = {
     80 	{"PROC",	f_proc},
     81 	{"PASS",	f_pass},
     82 	{"ENTRY",	f_list},
     83 	{"ABSOLUTE",	f_define},
     84 	{"EXTERN",	f_list},
     85 	{"EXTERNAL",	f_list},
     86 	{"RELATIVE",	f_define},
     87 	{"MOVE",	f_move},
     88 	{"JUMP",	f_jump},
     89 	{"CALL",	f_call},
     90 	{"RETURN",	f_return},
     91 	{"INT",		f_int},
     92 	{"INTFLY",	f_intfly},
     93 	{"SELECT",	f_select},
     94 	{"RESELECT",	f_reselect},
     95 	{"WAIT",	f_wait},
     96 	{"DISCONNECT",	f_disconnect},
     97 	{"SET",		f_set},
     98 	{"CLEAR",	f_clear},
     99 	{"LOAD",	f_load},
    100 	{"STORE",	f_store},
    101 	{"NOP",		f_nop},
    102 	{"ARCH",	f_arch},
    103 	{NULL, NULL}};
    104 
    105 u_int32_t script[MAXINST];
    106 int	dsps;
    107 char	*script_name = "SCRIPT";
    108 u_int32_t inst0, inst1, inst2;
    109 unsigned int	ninsts;
    110 unsigned int	npatches;
    111 
    112 struct patchlist {
    113 	struct patchlist *next;
    114 	unsigned	offset;
    115 } *patches;
    116 
    117 #define	S_LABEL		0x0000
    118 #define	S_ABSOLUTE	0x0001
    119 #define	S_RELATIVE	0x0002
    120 #define	S_EXTERNAL	0x0003
    121 #define	F_DEFINED	0x0001
    122 #define	F_ENTRY		0x0002
    123 struct {
    124 	short	type;
    125 	short	flags;
    126 	u_int32_t value;
    127 	struct patchlist *patchlist;
    128 	char	*name;
    129 } symbols[MAXSYMBOLS];
    130 int nsymbols;
    131 
    132 char	*stypes[] = {"Label", "Absolute", "Relative", "External"};
    133 
    134 char	*phases[] = {
    135 	"data_out", "data_in", "cmd", "status",
    136 	"res4", "res5", "msg_out", "msg_in"
    137 };
    138 
    139 struct ncrregs {
    140 	char *name;
    141 	int addr[5];
    142 };
    143 #define ARCH700 1
    144 #define ARCH710 2
    145 #define ARCH720 3
    146 #define ARCH810 4
    147 #define ARCH825 5
    148 
    149 struct ncrregs 	regs[] = {
    150 	{"scntl0",	{0x00, 0x00, 0x00, 0x00, 0x00}},
    151 	{"scntl1",	{0x01, 0x01, 0x01, 0x01, 0x01}},
    152 	{"sdid",	{0x02, 0x02,   -1,   -1,   -1}},
    153 	{"sien",	{0x03, 0x03,   -1,   -1,   -1}},
    154 	{"scid",	{0x04, 0x04,   -1,   -1,   -1}},
    155 	{"scntl2",	{  -1,   -1, 0x02, 0x02, 0x02}},
    156 	{"scntl3",	{  -1,   -1, 0x03, 0x03, 0x03}},
    157 	{"scid", 	{  -1,   -1, 0x04, 0x04, 0x04}},
    158 	{"sxfer",	{0x05, 0x05, 0x05, 0x05, 0x05}},
    159 	{"sodl",	{0x06, 0x06,   -1,   -1,   -1}},
    160 	{"socl",	{0x07, 0x07,   -1,   -1,   -1}},
    161 	{"sdid",	{  -1,   -1, 0x06, 0x06, 0x06}},
    162 	{"gpreg",	{  -1,   -1, 0x07, 0x07, 0x07}},
    163 	{"sfbr",	{0x08, 0x08, 0x08, 0x08, 0x08}},
    164 	{"sidl",	{0x09, 0x09,   -1,   -1,   -1}},
    165 	{"sbdl",	{0x0a, 0x0a,   -1,   -1,   -1}},
    166 	{"socl",	{  -1,   -1, 0x09, 0x09, 0x09}},
    167 	{"ssid", 	{  -1,   -1, 0x0a, 0x0a, 0x0a}},
    168 	{"sbcl",	{0x0b, 0x0b, 0x0b, 0x0b, 0x0b}},
    169 	{"dstat",	{0x0c, 0x0c, 0x0c, 0x0c, 0x0c}},
    170 	{"sstat0",	{0x0d, 0x0d, 0x0d, 0x0d, 0x0d}},
    171 	{"sstat1",	{0x0e, 0x0e, 0x0e, 0x0e, 0x0e}},
    172 	{"sstat2",	{0x0f, 0x0f, 0x0f, 0x0f, 0x0f}},
    173 	{"dsa0",	{  -1, 0x10, 0x10, 0x10, 0x10}},
    174 	{"dsa1",	{  -1, 0x11, 0x11, 0x11, 0x11}},
    175 	{"dsa2",	{  -1, 0x12, 0x12, 0x12, 0x12}},
    176 	{"dsa3",	{  -1, 0x13, 0x13, 0x13, 0x13}},
    177 	{"ctest0",	{0x14, 0x14, 0x18, 0x18, 0x18}},
    178 	{"ctest1",	{0x15, 0x15, 0x19, 0x19, 0x19}},
    179 	{"ctest2",	{0x16, 0x16, 0x1a, 0x1a, 0x1a}},
    180 	{"ctest3",	{0x17, 0x17, 0x1b, 0x1b, 0x1b}},
    181 	{"ctest4",	{0x18, 0x18, 0x21, 0x21, 0x21}},
    182 	{"ctest5",	{0x19, 0x19, 0x22, 0x22, 0x22}},
    183 	{"ctest6",	{0x1a, 0x1a, 0x23, 0x23, 0x23}},
    184 	{"ctest7",	{0x1b, 0x1b,   -1,   -1,   -1}},
    185 	{"temp0",	{0x1c, 0x1c, 0x1c, 0x1c, 0x1c}},
    186 	{"temp1",	{0x1d, 0x1d, 0x1d, 0x1d, 0x1d}},
    187 	{"temp2", 	{0x1e, 0x1e, 0x1e, 0x1e, 0x1e}},
    188 	{"temp3",	{0x1f, 0x1f, 0x1f, 0x1f, 0x1f}},
    189 	{"dfifo",	{0x20, 0x20, 0x20, 0x20, 0x20}},
    190 	{"istat", 	{0x21, 0x21, 0x14, 0x14, 0x14}},
    191 	{"ctest8",	{0x22, 0x22,   -1,   -1,   -1}},
    192 	{"lcrc",	{  -1, 0x23,   -1,   -1,   -1}},
    193 	{"ctest9",	{0x23,   -1,   -1,   -1,   -1}},
    194 	{"dbc0",	{0x24, 0x24, 0x24, 0x24, 0x24}},
    195 	{"dbc1",	{0x25, 0x25, 0x25, 0x25, 0x25}},
    196 	{"dbc2",	{0x26, 0x26, 0x26, 0x26, 0x26}},
    197 	{"dcmd",	{0x27, 0x27, 0x27, 0x27, 0x27}},
    198 	{"dnad0",	{0x28, 0x28, 0x28, 0x28, 0x28}},
    199 	{"dnad1",	{0x29, 0x29, 0x29, 0x29, 0x29}},
    200 	{"dnad2",	{0x2a, 0x2a, 0x2a, 0x2a, 0x2a}},
    201 	{"dnad3",	{0x2b, 0x2b, 0x2b, 0x2b, 0x2b}},
    202 	{"dsp0",	{0x2c, 0x2c, 0x2c, 0x2c, 0x2c}},
    203 	{"dsp1",	{0x2d, 0x2d, 0x2d, 0x2d, 0x2d}},
    204 	{"dsp2",	{0x2e, 0x2e, 0x2e, 0x2e, 0x2e}},
    205 	{"dsp3",	{0x2f, 0x2f, 0x2f, 0x2f, 0x2f}},
    206 	{"dsps0",	{0x30, 0x30, 0x30, 0x30, 0x30}},
    207 	{"dsps1",	{0x31, 0x31, 0x31, 0x31, 0x31}},
    208 	{"dsps2",	{0x32, 0x32, 0x32, 0x32, 0x32}},
    209 	{"dsps3",	{0x33, 0x33, 0x33, 0x33, 0x33}},
    210 	{"scratch0",	{  -1, 0x34,   -1,   -1,   -1}},
    211 	{"scratch1",	{  -1, 0x35,   -1,   -1,   -1}},
    212 	{"scratch2",	{  -1, 0x36,   -1,   -1,   -1}},
    213 	{"scratch3",	{  -1, 0x37,   -1,   -1,   -1}},
    214 	{"scratcha0",	{0x10,   -1, 0x34, 0x34, 0x34}},
    215 	{"scratcha1",	{0x11,   -1, 0x35, 0x35, 0x35}},
    216 	{"scratcha2",	{0x12,   -1, 0x36, 0x36, 0x36}},
    217 	{"scratcha3",	{0x13,   -1, 0x37, 0x37, 0x37}},
    218 	{"dmode",	{0x34, 0x38, 0x38, 0x38, 0x38}},
    219 	{"dien",	{0x39, 0x39, 0x39, 0x39, 0x39}},
    220 	{"dwt",		{0x3a, 0x3a, 0x3a,   -1,   -1}},
    221 	{"sbr",		{  -1,   -1,   -1, 0x3a, 0x3a}},
    222 	{"dcntl",	{0x3b, 0x3b, 0x3b, 0x3b, 0x3b}},
    223 	{"addr0",	{  -1, 0x3c, 0x3c, 0x3c, 0x3c}},
    224 	{"addr1",	{  -1, 0x3d, 0x3d, 0x3d, 0x3d}},
    225 	{"addr2",	{  -1, 0x3e, 0x3e, 0x3e, 0x3e}},
    226 	{"addr3",	{  -1, 0x3f, 0x3f, 0x3f, 0x3f}},
    227 	{"sien0",	{  -1,   -1, 0x40, 0x40, 0x40}},
    228 	{"sien1",	{  -1,   -1, 0x41, 0x41, 0x41}},
    229 	{"sist0",	{  -1,   -1, 0x42, 0x42, 0x42}},
    230 	{"sist1",	{  -1,   -1, 0x43, 0x43, 0x43}},
    231 	{"slpar",	{  -1,   -1, 0x44, 0x44, 0x44}},
    232 	{"swide",	{  -1,   -1, 0x45,   -1, 0x45}},
    233 	{"macntl",	{  -1,   -1, 0x46, 0x46, 0x46}},
    234 	{"gpcntl",	{  -1,   -1, 0x47, 0x47, 0x47}},
    235 	{"stime0",	{  -1,   -1, 0x48, 0x48, 0x48}},
    236 	{"stime1",	{  -1,   -1, 0x49, 0x49, 0x49}},
    237 	{"respid0",	{  -1,   -1, 0x4a, 0x4a, 0x4a}},
    238 	{"respid1",	{  -1,   -1, 0x4b,   -1, 0x4b}},
    239 	{"stest0",	{  -1,   -1, 0x4c, 0x4c, 0x4c}},
    240 	{"stest1",	{  -1,   -1, 0x4d, 0x4d, 0x4d}},
    241 	{"stest2",	{  -1,   -1, 0x4e, 0x4e, 0x4e}},
    242 	{"stest3",	{  -1,   -1, 0x4f, 0x4f, 0x4f}},
    243 	{"sidl0",	{  -1,   -1, 0x50, 0x50, 0x50}},
    244 	{"sidl1",	{  -1,   -1, 0x51,   -1, 0x51}},
    245 	{"sodl0",	{  -1,   -1, 0x54, 0x54, 0x54}},
    246 	{"sodl1",	{  -1,   -1, 0x55,   -1, 0x55}},
    247 	{"sbdl0",	{  -1,   -1, 0x58, 0x58, 0x58}},
    248 	{"sbdl1",	{  -1,   -1, 0x59,   -1, 0x59}},
    249 	{"scratchb0",	{0x3c,   -1, 0x5c, 0x5c, 0x5c}},
    250 	{"scratchb1",	{0x3d,   -1, 0x5d, 0x5d, 0x5d}},
    251 	{"scratchb2",	{0x3e,   -1, 0x5e, 0x5e, 0x5e}},
    252 	{"scratchb3",	{0x3f,   -1, 0x5f, 0x5f, 0x5f}},
    253 	{"scratchc0",	{  -1,   -1,   -1,   -1, 0x60}},
    254 	{"scratchc1",	{  -1,   -1,   -1,   -1, 0x61}},
    255 	{"scratchc2",	{  -1,   -1,   -1,   -1, 0x62}},
    256 	{"scratchc3",	{  -1,   -1,   -1,   -1, 0x63}},
    257 	{"scratchd0",	{  -1,   -1,   -1,   -1, 0x64}},
    258 	{"scratchd1",	{  -1,   -1,   -1,   -1, 0x65}},
    259 	{"scratchd2",	{  -1,   -1,   -1,   -1, 0x66}},
    260 	{"scratchd3",	{  -1,   -1,   -1,   -1, 0x67}},
    261 	{"scratche0",	{  -1,   -1,   -1,   -1, 0x68}},
    262 	{"scratche1",	{  -1,   -1,   -1,   -1, 0x69}},
    263 	{"scratche2",	{  -1,   -1,   -1,   -1, 0x6a}},
    264 	{"scratche3",	{  -1,   -1,   -1,   -1, 0x6b}},
    265 	{"scratchf0",	{  -1,   -1,   -1,   -1, 0x6c}},
    266 	{"scratchf1",	{  -1,   -1,   -1,   -1, 0x6d}},
    267 	{"scratchf2",	{  -1,   -1,   -1,   -1, 0x6e}},
    268 	{"scratchf3",	{  -1,   -1,   -1,   -1, 0x6f}},
    269 	{"scratchg0",	{  -1,   -1,   -1,   -1, 0x70}},
    270 	{"scratchg1",	{  -1,   -1,   -1,   -1, 0x71}},
    271 	{"scratchg2",	{  -1,   -1,   -1,   -1, 0x72}},
    272 	{"scratchg3",	{  -1,   -1,   -1,   -1, 0x73}},
    273 	{"scratchh0",	{  -1,   -1,   -1,   -1, 0x74}},
    274 	{"scratchh1",	{  -1,   -1,   -1,   -1, 0x75}},
    275 	{"scratchh2",	{  -1,   -1,   -1,   -1, 0x7e}},
    276 	{"scratchh3",	{  -1,   -1,   -1,   -1, 0x77}},
    277 	{"scratchi0",	{  -1,   -1,   -1,   -1, 0x78}},
    278 	{"scratchi1",	{  -1,   -1,   -1,   -1, 0x79}},
    279 	{"scratchi2",	{  -1,   -1,   -1,   -1, 0x7a}},
    280 	{"scratchi3",	{  -1,   -1,   -1,   -1, 0x7b}},
    281 	{"scratchj0",	{  -1,   -1,   -1,   -1, 0x7c}},
    282 	{"scratchj1",	{  -1,   -1,   -1,   -1, 0x7d}},
    283 	{"scratchj2",	{  -1,   -1,   -1,   -1, 0x7e}},
    284 	{"scratchj3",	{  -1,   -1,   -1,   -1, 0x7f}},
    285 };
    286 
    287 int	lineno;
    288 int	err_listed;
    289 int	arch;
    290 int	partial_flag;
    291 
    292 char	inbuf[128];
    293 
    294 char	*sourcefile;
    295 char	*outputfile;
    296 char	*listfile;
    297 char	*errorfile;
    298 
    299 FILE	*infp;
    300 FILE	*outfp;
    301 FILE	*listfp;
    302 FILE	*errfp;
    303 
    304 void	setarch(char *);
    305 void	parse (void);
    306 void	process (void);
    307 void	emit_symbols (void);
    308 void	list_symbols (void);
    309 void	errout (char *);
    310 void	define_symbol (char *, u_int32_t, short, short);
    311 void	patch_label (void);
    312 void	close_script (void);
    313 void	new_script (char *);
    314 void	store_inst (void);
    315 int	expression (int *);
    316 int	evaluate (int);
    317 int	number (char *);
    318 int	lookup (char *);
    319 int	reserved (char *, int);
    320 int	CheckPhase (int);
    321 int	CheckRegister (int);
    322 void	transfer (int, int);
    323 void	select_reselect (int);
    324 void	set_clear (u_int32_t);
    325 void	block_move (void);
    326 void	register_write (void);
    327 void	memory_to_memory (void);
    328 void	loadstore (int);
    329 void	error_line(void);
    330 char	*makefn(char *, char *);
    331 void	usage(void);
    332 
    333 int
    334 main (int argc, char *argv[])
    335 {
    336 	int	i;
    337 	struct patchlist *p;
    338 
    339 	if (argc < 2 || argv[1][0] == '-')
    340 		usage();
    341 	sourcefile = argv[1];
    342 	infp = fopen (sourcefile, "r");
    343 	if (infp == NULL) {
    344 		perror ("open source");
    345 		fprintf (stderr, "scc: error opening source file %s\n", argv[1]);
    346 		exit (1);
    347 	}
    348 	/*
    349 	 * process options
    350 	 * -l [listfile]
    351 	 * -o [outputfile]
    352 	 * -p [outputfile]
    353 	 * -z [debugfile]
    354 	 * -e [errorfile]
    355 	 * -a arch
    356 	 * -v
    357 	 * -u
    358 	 */
    359 	for (i = 2; i < argc; ++i) {
    360 		if (argv[i][0] != '-')
    361 			usage();
    362 		switch (argv[i][1]) {
    363 		case 'o':
    364 		case 'p':
    365 			partial_flag = argv[i][1] == 'p';
    366 			if (i + 1 >= argc || argv[i + 1][0] == '-')
    367 				outputfile = makefn (sourcefile, "out");
    368 			else {
    369 				outputfile = argv[i + 1];
    370 				++i;
    371 			}
    372 			break;
    373 		case 'l':
    374 			if (i + 1 >= argc || argv[i + 1][0] == '-')
    375 				listfile = makefn (sourcefile, "lis");
    376 			else {
    377 				listfile = argv[i + 1];
    378 				++i;
    379 			}
    380 			break;
    381 		case 'e':
    382 			if (i + 1 >= argc || argv[i + 1][0] == '-')
    383 				errorfile = makefn (sourcefile, "err");
    384 			else {
    385 				errorfile = argv[i + 1];
    386 				++i;
    387 			}
    388 			break;
    389 		case 'a':
    390 			if (i + 1 == argc)
    391 				usage();
    392 			setarch(argv[i +1]);
    393 			if (arch == 0) {
    394 				fprintf(stderr,"%s: bad arch '%s'\n",
    395 					argv[0], argv[i +1]);
    396 				exit(1);
    397 			}
    398 			++i;
    399 			break;
    400 		default:
    401 			fprintf (stderr, "scc: unrecognized option '%c'\n",
    402 			    argv[i][1]);
    403 			usage();
    404 		}
    405 	}
    406 	if (outputfile)
    407 		outfp = fopen (outputfile, "w");
    408 	if (listfile)
    409 		listfp = fopen (listfile, "w");
    410 	if (errorfile)
    411 		errfp = fopen (errorfile, "w");
    412 	else
    413 		errfp = stderr;
    414 
    415 	if (outfp) {
    416 		time_t cur_time;
    417 
    418 		fprintf(outfp, "/*\t$NetBSD: ncr53cxxx.c,v 1.11 2003/04/06 09:50:29 tsutsui Exp $\t*/\n");
    419 		fprintf(outfp, "/*\n");
    420 		fprintf(outfp, " *\tDO NOT EDIT - this file is automatically generated.\n");
    421 		time(&cur_time);
    422 		fprintf(outfp, " *\tcreated from %s on %s", sourcefile, ctime(&cur_time));
    423 		fprintf(outfp, " */\n");
    424 	}
    425 
    426 	while (fgets (inbuf, sizeof (inbuf), infp)) {
    427 		++lineno;
    428 		if (listfp)
    429 			fprintf (listfp, "%3d:  %s", lineno, inbuf);
    430 		err_listed = 0;
    431 		parse ();
    432 		if (ntokens) {
    433 #ifdef DUMP_TOKENS
    434 			int	i;
    435 
    436 			fprintf (listfp, "      %d tokens\n", ntokens);
    437 			for (i = 0; i < ntokens; ++i) {
    438 				fprintf (listfp, "      %d: ", i);
    439 				if (tokens[i].type)
    440 					fprintf (listfp,"'%c'\n", tokens[i].type);
    441 				else
    442 					fprintf (listfp, "%s\n", tokens[i].name);
    443 			}
    444 #endif
    445 			if (ntokens >= 2 && tokens[0].type == 0 &&
    446 			    tokens[1].type == ':') {
    447 			    	define_symbol (tokens[0].name, dsps, S_LABEL, F_DEFINED);
    448 				tokenix += 2;
    449 			}
    450 			if (tokenix < ntokens)
    451 				process ();
    452 		}
    453 
    454 	}
    455 	close_script ();
    456 	emit_symbols ();
    457 	if (outfp && !partial_flag) {
    458 		fprintf (outfp, "\nu_int32_t INSTRUCTIONS = 0x%08x;\n", ninsts);
    459 		fprintf (outfp, "u_int32_t PATCHES = 0x%08x;\n", npatches);
    460 		fprintf (outfp, "u_int32_t LABELPATCHES[] = {\n");
    461 		p = patches;
    462 		while (p) {
    463 			fprintf (outfp, "\t0x%08x,\n", p->offset / 4);
    464 			p = p->next;
    465 		}
    466 		fprintf (outfp, "};\n\n");
    467 	}
    468 	list_symbols ();
    469 	exit(0);
    470 }
    471 
    472 void setarch(char *val)
    473 {
    474 	switch (atoi(val)) {
    475 	case 700:
    476 		arch = ARCH700;
    477 		break;
    478 	case 710:
    479 		arch = ARCH710;
    480 		break;
    481 	case 720:
    482 		arch = ARCH720;
    483 		break;
    484 	case 810:
    485 		arch = ARCH810;
    486 		break;
    487 	case 825:
    488 		arch = ARCH825;
    489 		break;
    490 	default:
    491 		arch = 0;
    492 	}
    493 }
    494 
    495 void emit_symbols ()
    496 {
    497 	int	i;
    498 	struct	patchlist *p;
    499 
    500 	if (nsymbols == 0 || outfp == NULL)
    501 		return;
    502 
    503 	for (i = 0; i < nsymbols; ++i) {
    504 		char	*code;
    505 		if ((symbols[i].flags & F_DEFINED) == 0 &&
    506 		    symbols[i].type != S_EXTERNAL) {
    507 			fprintf(stderr, "warning: symbol %s undefined\n",
    508 			    symbols[i].name);
    509 		}
    510 		if (symbols[i].type == S_ABSOLUTE)
    511 			code = "A_";
    512 		else if (symbols[i].type == S_RELATIVE)
    513 			code = "R_";
    514 		else if (symbols[i].type == S_EXTERNAL)
    515 			code = "E_";
    516 		else if (symbols[i].flags & F_ENTRY)
    517 			code = "Ent_";
    518 		else
    519 			continue;
    520 		fprintf (outfp, "#define\t%s%s\t0x%08x\n", code, symbols[i].name,
    521 			symbols[i].value);
    522 		if (symbols[i].flags & F_ENTRY || symbols[i].patchlist == NULL)
    523 			continue;
    524 		fprintf (outfp, "u_int32_t %s%s_Used[] = {\n", code, symbols[i].name);
    525 #if 1
    526 		p = symbols[i].patchlist;
    527 		while (p) {
    528 			fprintf (outfp, "\t0x%08x,\n", p->offset / 4);
    529 			p = p->next;
    530 		}
    531 #endif
    532 		fprintf (outfp, "};\n\n");
    533 	}
    534 	/* patches ? */
    535 }
    536 
    537 void list_symbols ()
    538 {
    539 	int	i;
    540 
    541 	if (nsymbols == 0 || listfp == NULL)
    542 		return;
    543 	fprintf (listfp, "\n\nValue     Type     Symbol\n");
    544 	for (i = 0; i < nsymbols; ++i) {
    545 		fprintf (listfp, "%08x: %-8s %s\n", symbols[i].value,
    546 			stypes[symbols[i].type], symbols[i].name);
    547 	}
    548 }
    549 
    550 void errout (char *text)
    551 {
    552 	error_line();
    553 	fprintf (errfp, "*** %s ***\n", text);
    554 }
    555 
    556 void parse ()
    557 {
    558 	char *p = inbuf;
    559 	char c;
    560 	char string[64];
    561 	char *s;
    562 
    563 	ntokens = tokenix = 0;
    564 	while (1) {
    565 		while ((c = *p++) && c != '\n' && (c <= ' ' || c == '\t'))
    566 			;
    567 		if (c == '\n' || c == 0 || c == ';')
    568 			break;
    569 		if (ntokens >= MAXTOKENS) {
    570 			errout ("Token table full");
    571 			break;
    572 		}
    573 		if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
    574 		    (c >= 'A' && c <= 'Z') || c == '$' || c == '_') {
    575 		    	s = string;
    576 		    	*s++ = c;
    577 		    	while (((c = *p) >= '0' && c <= '9') ||
    578 		    	    (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
    579 		    	    c == '_' || c == '$') {
    580 		    	    	*s++ = *p++;
    581 		    	}
    582 		    	*s = 0;
    583 		    	tokens[ntokens].name = malloc (strlen (string) + 1);
    584 		    	strcpy (tokens[ntokens].name, string);
    585 		    	tokens[ntokens].type = 0;
    586 		}
    587 		else {
    588 			tokens[ntokens].type = c;
    589 		}
    590 		++ntokens;
    591 	}
    592 	return;
    593 }
    594 
    595 void	process ()
    596 {
    597 	int	i;
    598 
    599 	if (tokens[tokenix].type) {
    600 		error_line();
    601 		fprintf (errfp, "Error: expected directive, found '%c'\n",
    602 			tokens[tokenix].type);
    603 		return;
    604 	}
    605 	for (i = 0; directives[i].name; ++i) {
    606 		if (strcmpi (directives[i].name, tokens[tokenix].name) == 0)
    607 			break;
    608 	}
    609 	if (directives[i].name == NULL) {
    610 		error_line();
    611 		fprintf (errfp, "Error: expected directive, found \"%s\"\n",
    612 			tokens[tokenix].name);
    613 		return;
    614 	}
    615 	if (directives[i].func == NULL) {
    616 		error_line();
    617 		fprintf (errfp, "No function for directive \"%s\"\n", tokens[tokenix].name);
    618 	} else {
    619 #if 0
    620 		fprintf (listfp, "Processing directive \"%s\"\n", directives[i].name);
    621 #endif
    622 		++tokenix;
    623 		(*directives[i].func) ();
    624 	}
    625 }
    626 
    627 void define_symbol (char *name, u_int32_t value, short type, short flags)
    628 {
    629 	int	i;
    630 	struct patchlist *p;
    631 
    632 	for (i = 0; i < nsymbols; ++i) {
    633 		if (symbols[i].type == type && strcmp (symbols[i].name, name) == 0) {
    634 			if (symbols[i].flags & F_DEFINED) {
    635 				error_line();
    636 				fprintf (errfp, "*** Symbol \"%s\" multiply defined\n",
    637 					name);
    638 			} else {
    639 				symbols[i].flags |= flags;
    640 				symbols[i].value = value;
    641 				p = symbols[i].patchlist;
    642 				while (p) {
    643 					if (p->offset > dsps)
    644 						errout ("Whoops\007");
    645 					else
    646 						script[p->offset / 4] += dsps;
    647 					p = p->next;
    648 				}
    649 			}
    650 			return;
    651 		}
    652 	}
    653 	if (nsymbols >= MAXSYMBOLS) {
    654 		errout ("Symbol table full");
    655 		return;
    656 	}
    657 	symbols[nsymbols].type = type;
    658 	symbols[nsymbols].flags = flags;
    659 	symbols[nsymbols].value = value;
    660 	symbols[nsymbols].patchlist = NULL;
    661 	symbols[nsymbols].name = malloc (strlen (name) + 1);
    662 	strcpy (symbols[nsymbols].name, name);
    663 	++nsymbols;
    664 }
    665 
    666 void patch_label (void)
    667 {
    668 	struct patchlist *p, **h;
    669 
    670 	h = &patches;
    671 	while(*h)
    672 		h = &(*h)->next;
    673 	p = (struct patchlist *) malloc (sizeof (struct patchlist));
    674 	*h = p;
    675 	p->next = NULL;
    676 	p->offset = dsps + 4;
    677 	npatches++;
    678 }
    679 
    680 void close_script ()
    681 {
    682 	int	i;
    683 
    684 	if (dsps == 0)
    685 		return;
    686 	if (outfp) {
    687 		fprintf (outfp, "const u_int32_t %s[] = {\n", script_name);
    688 		for (i = 0; i < dsps / 4; i += 2) {
    689 			fprintf (outfp, "\t0x%08x, 0x%08x", script[i],
    690 				script[i + 1]);
    691 			/* check for memory move instruction */
    692 			if ((script[i] & 0xe0000000) == 0xc0000000)
    693 				fprintf (outfp, ", 0x%08x,", script[i + 2]);
    694 			else
    695 				if ((i + 2) <= dsps / 4) fprintf (outfp, ",\t\t");
    696 			fprintf (outfp, "\t/* %03x - %3d */\n", i * 4, i * 4);
    697 			if ((script[i] & 0xe0000000) == 0xc0000000)
    698 				++i;
    699 		}
    700 		fprintf (outfp, "};\n\n");
    701 	}
    702 	dsps = 0;
    703 }
    704 
    705 void new_script (char *name)
    706 {
    707 	close_script ();
    708 	script_name = malloc (strlen (name) + 1);
    709 	strcpy (script_name, name);
    710 }
    711 
    712 int	reserved (char *string, int t)
    713 {
    714 	if (tokens[t].type == 0 && strcmpi (tokens[t].name, string) == 0)
    715 		return (1);
    716 	return (0);
    717 }
    718 
    719 int	CheckPhase (int t)
    720 {
    721 	int	i;
    722 
    723 	for (i = 0; i < 8; ++i) {
    724 		if (reserved (phases[i], t)) {
    725 			inst0 |= i << 24;
    726 			return (1);
    727 		}
    728 	}
    729 	return (0);
    730 }
    731 
    732 int	CheckRegister (int t)
    733 {
    734 	int	i;
    735 
    736 	if (arch <= 0) {
    737 		errout("'ARCH' statement missing");
    738 		return -1;
    739 	}
    740 	for (i = 0; i < (sizeof(regs) / sizeof(regs[0])); i++) {
    741 		if (regs[i].addr[arch - 1] >= 0 && reserved(regs[i].name, t))
    742 			return regs[i].addr[arch-1];
    743 	}
    744 	return (-1);
    745 }
    746 
    747 int	expression (int *t)
    748 {
    749 	int	value;
    750 	int	i = *t;
    751 
    752 	value = evaluate (i++);
    753 	while (i < ntokens) {
    754 		if (tokens[i].type == '+')
    755 			value += evaluate (i + 1);
    756 		else if (tokens[i].type == '-')
    757 			value -= evaluate (i + 1);
    758 		else
    759 			errout ("Unknown identifier");
    760 		i += 2;
    761 	}
    762 	*t = i;
    763 	return (value);
    764 }
    765 
    766 int	evaluate (t)
    767 {
    768 	int	value;
    769 	char	*name;
    770 
    771 	if (tokens[t].type) {
    772 		errout ("Expected an identifier");
    773 		return (0);
    774 	}
    775 	name = tokens[t].name;
    776 	if (*name >= '0' && *name <= '9')
    777 		value = number (name);
    778 	else
    779 		value = lookup (name);
    780 	return (value);
    781 }
    782 
    783 int	number (char *s)
    784 {
    785 	int	value;
    786 	int	n;
    787 	int	radix;
    788 
    789 	radix = 10;
    790 	if (*s == '0') {
    791 		++s;
    792 		radix = 8;
    793 		switch (*s) {
    794 		case 'x':
    795 		case 'X':
    796 			radix = 16;
    797 			break;
    798 		case 'b':
    799 		case 'B':
    800 			radix = 2;
    801 		}
    802 		if (radix != 8)
    803 			++s;
    804 	}
    805 	value = 0;
    806 	while (*s) {
    807 		n = *s++;
    808 		if (n >= '0' && n <= '9')
    809 			n -= '0';
    810 		else if (n >= 'a' && n <= 'f')
    811 			n -= 'a' - 10;
    812 		else if (n >= 'A' && n <= 'F')
    813 			n -= 'A' - 10;
    814 		else {
    815 			error_line();
    816 			fprintf (errfp, "*** Expected digit\n");
    817 			n = 0;
    818 		}
    819 		if (n >= radix)
    820 			errout ("Expected digit");
    821 		else
    822 			value = value * radix + n;
    823 	}
    824 	return (value);
    825 }
    826 
    827 int	lookup (char *name)
    828 {
    829 	int	i;
    830 	struct patchlist *p;
    831 
    832 	for (i = 0; i < nsymbols; ++i) {
    833 		if (strcmp (name, symbols[i].name) == 0) {
    834 			if ((symbols[i].flags & F_DEFINED) == 0) {
    835 				p = (struct patchlist *) &symbols[i].patchlist;
    836 				while (p->next)
    837 					p = p->next;
    838 				p->next = (struct patchlist *) malloc (sizeof (struct patchlist));
    839 				p = p->next;
    840 				p->next = NULL;
    841 				p->offset = dsps + 4;
    842 			}
    843 			return ((int) symbols[i].value);
    844 		}
    845 	}
    846 	if (nsymbols >= MAXSYMBOLS) {
    847 		errout ("Symbol table full");
    848 		return (0);
    849 	}
    850 	symbols[nsymbols].type = S_LABEL;	/* assume forward reference */
    851 	symbols[nsymbols].flags = 0;
    852 	symbols[nsymbols].value = 0;
    853 	p = (struct patchlist *) malloc (sizeof (struct patchlist));
    854 	symbols[nsymbols].patchlist = p;
    855 	p->next = NULL;
    856 	p->offset = dsps + 4;
    857 	symbols[nsymbols].name = malloc (strlen (name) + 1);
    858 	strcpy (symbols[nsymbols].name, name);
    859 	++nsymbols;
    860 	return (0);
    861 }
    862 
    863 void	f_arch (void)
    864 {
    865 	int i, archsave;
    866 
    867 	i = tokenix;
    868 
    869 	archsave = arch;
    870 	setarch(tokens[i].name);
    871 	if( arch == 0) {
    872 		errout("Unrecognized ARCH");
    873 		arch = archsave;
    874 	}
    875 }
    876 
    877 void	f_proc (void)
    878 {
    879 	if (tokens[tokenix].type != 0 || tokens[tokenix + 1].type != ':')
    880 		errout ("Invalid PROC statement");
    881 	else
    882 		new_script (tokens[tokenix].name);
    883 }
    884 
    885 void	f_pass (void)
    886 {
    887 	errout ("PASS option not implemented");
    888 }
    889 
    890 /*
    891  *	f_list:  process list of symbols for the ENTRY and EXTERNAL directive
    892  */
    893 
    894 void	f_list (void)
    895 {
    896 	int	i;
    897 	short	type;
    898 	short	flags;
    899 
    900 	type = strcmpi (tokens[tokenix-1].name, "ENTRY") ? S_EXTERNAL : S_LABEL;
    901 	flags = type == S_LABEL ? F_ENTRY : 0;
    902 	for (i = tokenix; i < ntokens; ++i) {
    903 		if (tokens[i].type != 0) {
    904 			errout ("Expected an identifier");
    905 			return;
    906 		}
    907 		define_symbol (tokens[i].name, 0, type, flags);
    908 		if (i + 1 < ntokens) {
    909 			if (tokens[++i].type == ',')
    910 				continue;
    911 			errout ("Expected a separator");
    912 			return;
    913 		}
    914 	}
    915 }
    916 
    917 /*
    918  *	f_define:	process list of definitions for ABSOLUTE and RELATIVE directive
    919  */
    920 
    921 void	f_define (void)
    922 {
    923 	int	i;
    924 	char	*name;
    925 	u_int32_t value;
    926 	int	type;
    927 
    928 	type = strcmpi (tokens[tokenix-1].name, "ABSOLUTE") ? S_RELATIVE : S_ABSOLUTE;
    929 	i = tokenix;
    930 	while (i < ntokens) {
    931 		if (tokens[i].type) {
    932 			errout ("Expected an identifier");
    933 			return;
    934 		}
    935 		if (tokens[i + 1].type != '=') {
    936 			errout ("Expected a separator");
    937 			return;
    938 		}
    939 		name = tokens[i].name;
    940 		i += 2;
    941 		value = expression (&i);
    942 		define_symbol (name, value, type, F_DEFINED);
    943 	}
    944 }
    945 
    946 void	store_inst ()
    947 {
    948 	int	i = dsps / 4;
    949 	int	l = 8;
    950 
    951 	if ((inst0 & 0xe0000000) == 0xc0000000)
    952 		l = 12;			/* Memory to memory move is 12 bytes */
    953 	if ((dsps + l) / 4 > MAXINST) {
    954 		errout ("Instruction table overflow");
    955 		return;
    956 	}
    957 	script[i++] = inst0;
    958 	script[i++] = inst1;
    959 	if (l == 12)
    960 		script[i++] = inst2;
    961 	if (listfp) {
    962 		fprintf (listfp, "\t%04x: %08x %08x", dsps, inst0, inst1);
    963 		if (l == 12)
    964 			fprintf (listfp, " %08x", inst2);
    965 		fprintf (listfp, "\n");
    966 	}
    967 	dsps += l;
    968 	inst0 = inst1 = inst2 = 0;
    969 	++ninsts;
    970 }
    971 
    972 void	f_move (void)
    973 {
    974 	if (reserved ("memory", tokenix))
    975 		memory_to_memory ();
    976 	else if (reserved ("from", tokenix) || tokens[tokenix+1].type == ',')
    977 		block_move ();
    978 	else
    979 		register_write ();
    980 	store_inst ();
    981 }
    982 
    983 void	f_jump (void)
    984 {
    985 	transfer (0x80000000, 0);
    986 }
    987 
    988 void	f_call (void)
    989 {
    990 	transfer (0x88000000, 0);
    991 }
    992 
    993 void	f_return (void)
    994 {
    995 	transfer (0x90000000, 1);
    996 }
    997 
    998 void	f_int (void)
    999 {
   1000 	transfer (0x98000000, 2);
   1001 }
   1002 
   1003 void	f_intfly (void)
   1004 {
   1005 	transfer (0x98100000, 2);
   1006 }
   1007 
   1008 void	f_select (void)
   1009 {
   1010 	int	t = tokenix;
   1011 
   1012 	if (reserved ("atn", t)) {
   1013 		inst0 = 0x01000000;
   1014 		++t;
   1015 	}
   1016 	select_reselect (t);
   1017 }
   1018 
   1019 void	f_reselect (void)
   1020 {
   1021 	select_reselect (tokenix);
   1022 }
   1023 
   1024 void	f_wait (void)
   1025 {
   1026 	int	i = tokenix;
   1027 
   1028 	inst1 = 0;
   1029 	if (reserved ("disconnect", i)) {
   1030 		inst0 = 0x48000000;
   1031 	}
   1032 	else {
   1033 		if (reserved ("reselect", i))
   1034 			inst0 = 0x50000000;
   1035 		else if (reserved ("select", i))
   1036 			inst0 = 0x50000000;
   1037 		else
   1038 			errout ("Expected SELECT or RESELECT");
   1039 		++i;
   1040 		if (reserved ("rel", i)) {
   1041 #if 0 /* driver will fix relative dsps to absolute */
   1042 			if (arch < ARCH710) {
   1043 				errout ("Wrong arch for relative dsps");
   1044 			}
   1045 #endif
   1046 			i += 2;
   1047 			inst1 = evaluate (i) - dsps - 8;
   1048 			inst0 |= 0x04000000;
   1049 		}
   1050 		else {
   1051 			inst1 = evaluate (i);
   1052 			patch_label();
   1053 		}
   1054 	}
   1055 	store_inst ();
   1056 }
   1057 
   1058 void	f_disconnect (void)
   1059 {
   1060 	inst0 = 0x48000000;
   1061 	store_inst ();
   1062 }
   1063 
   1064 void	f_set (void)
   1065 {
   1066 	set_clear (0x58000000);
   1067 }
   1068 
   1069 void	f_clear (void)
   1070 {
   1071 	set_clear (0x60000000);
   1072 }
   1073 
   1074 void	f_load (void)
   1075 {
   1076 	inst0 = 0xe1000000;
   1077 	if (arch < ARCH810) {
   1078 		errout ("Wrong arch for load/store");
   1079 		return;
   1080 	}
   1081 	loadstore(tokenix);
   1082 }
   1083 
   1084 void	f_store (void)
   1085 {
   1086 	int i;
   1087 	inst0 = 0xe0000000;
   1088 	if (arch < ARCH810) {
   1089 		errout ("Wrong arch for load/store");
   1090 		return;
   1091 	}
   1092 	i = tokenix;
   1093 	if (reserved("noflush", i)) {
   1094 		inst0 |= 0x2000000;
   1095 		i++;
   1096 	}
   1097 	loadstore(i);
   1098 }
   1099 
   1100 void	f_nop (void)
   1101 {
   1102 	inst0 = 0x80000000;
   1103 	inst1 = 0x00000000;
   1104 	store_inst ();
   1105 }
   1106 
   1107 void loadstore(int i)
   1108 {
   1109 	int reg, size;
   1110 
   1111 	reg = CheckRegister(i);
   1112 	if (reg < 0)
   1113 		errout ("Expected register");
   1114 	else
   1115 		inst0 |= reg <<  16;
   1116 	if (reg == 8)
   1117 		errout ("Register can't be SFBR");
   1118 	i++;
   1119 	if (tokens[i].type == ',')
   1120 		i++;
   1121 	else
   1122 		errout ("expected ','");
   1123 	size = evaluate(i);
   1124 	if (i < 1 || i > 4)
   1125 		errout("wrong size");
   1126 	if ((reg & 0x3) + size > 4)
   1127 		errout("size too big for register");
   1128 	inst0 |= size;
   1129 	i++;
   1130 	if (tokens[i].type == ',')
   1131 		i++;
   1132 	else
   1133 		errout ("expected ','");
   1134 	if (reserved("from", i) || reserved("dsarel", i)) {
   1135 		if (arch < ARCH710) {
   1136 			errout ("Wrong arch for table indirect");
   1137 			return;
   1138 		}
   1139 		i++;
   1140 		inst0 |= 0x10000000;
   1141 	}
   1142 	inst1 = evaluate(i);
   1143 	store_inst ();
   1144 }
   1145 
   1146 void	transfer (int word0, int type)
   1147 {
   1148 	int	i;
   1149 
   1150 	i = tokenix;
   1151 	inst0 = word0;
   1152 	if (type == 0 && reserved ("rel", i)) {
   1153 #if 0 /* driver will fix relative dsps to absolute */
   1154 		if (arch < ARCH710) {
   1155 			errout ("Wrong arch for relative dsps");
   1156 		}
   1157 #endif
   1158 		inst1 = evaluate (i + 2) - dsps - 8;
   1159 		i += 4;
   1160 		inst0 |= 0x00800000;
   1161 	}
   1162 	else if (type != 1) {
   1163 		inst1 = evaluate (i);
   1164 		++i;
   1165 		if (type == 0)
   1166 			patch_label();
   1167 	}
   1168 	if (i >= ntokens) {
   1169 		inst0 |= 0x00080000;
   1170 		store_inst ();
   1171 		return;
   1172 	}
   1173 	if (tokens[i].type != ',')
   1174 		errout ("Expected a separator, ',' assumed");
   1175 	else
   1176 		++i;
   1177 	if (reserved("when", i))
   1178 		inst0 |= 0x00010000;
   1179 	else if (reserved ("if", i) == 0) {
   1180 		errout ("Expected a reserved word");
   1181 		store_inst ();
   1182 		return;
   1183 	}
   1184 	i++;
   1185 	if (reserved("false", i)) {
   1186 		store_inst ();
   1187 		return;
   1188 	}
   1189 	if (reserved ("not", i))
   1190 		++i;
   1191 	else
   1192 		inst0 |= 0x00080000;
   1193 	if (reserved ("atn", i)) {
   1194 		inst0 |= 0x00020000;
   1195 		++i;
   1196 	} else if (CheckPhase (i)) {
   1197 		inst0 |= 0x00020000;
   1198 		++i;
   1199 	}
   1200 	if (i < ntokens && tokens[i].type != ',') {
   1201 		if (inst0 & 0x00020000) {
   1202 			if (inst0 & 0x00080000 && reserved ("and", i)) {
   1203 				++i;
   1204 			}
   1205 			else if ((inst0 & 0x00080000) == 0 && reserved ("or", i)) {
   1206 				++i;
   1207 			}
   1208 			else
   1209 				errout ("Expected a reserved word");
   1210 		}
   1211 		inst0 |= 0x00040000 + (evaluate (i++) & 0xff);
   1212 	}
   1213 	if (i < ntokens) {
   1214 		if (tokens[i].type == ',')
   1215 			++i;
   1216 		else
   1217 			errout ("Expected a separator, ',' assumed");
   1218 		if (reserved ("and", i) && reserved ("mask", i + 1))
   1219 			inst0 |= ((evaluate (i + 2) & 0xff) << 8);
   1220 		else
   1221 			errout ("Expected , AND MASK");
   1222 	}
   1223 	store_inst ();
   1224 }
   1225 
   1226 void 	select_reselect (int t)
   1227 {
   1228 	inst0 |= 0x40000000;		/* ATN may be set from SELECT */
   1229 	if (reserved ("from", t)) {
   1230 		if (arch < ARCH710) {
   1231 			errout ("Wrong arch for table indirect");
   1232 			return;
   1233 		}
   1234 		++t;
   1235 		inst0 |= 0x02000000 | evaluate (t++);
   1236 	}
   1237 	else
   1238 		inst0 |= (evaluate (t++) & 0xff) << 16;
   1239 	if (tokens[t++].type == ',') {
   1240 		if (reserved ("rel", t)) {
   1241 #if 0 /* driver will fix relative dsps to absolute */
   1242 			if (arch < ARCH710) {
   1243 				errout ("Wrong arch for relative dsps");
   1244 			}
   1245 #endif
   1246 			inst0 |= 0x04000000;
   1247 			inst1 = evaluate (t + 2) - dsps - 8;
   1248 		}
   1249 		else {
   1250 			inst1 = evaluate (t);
   1251 			patch_label();
   1252 		}
   1253 	}
   1254 	else
   1255 		errout ("Expected separator");
   1256 	store_inst ();
   1257 }
   1258 
   1259 void	set_clear (u_int32_t code)
   1260 {
   1261 	int	i = tokenix;
   1262 	short	need_and = 0;
   1263 
   1264 	inst0 = code;
   1265 	while (i < ntokens) {
   1266 		if (need_and) {
   1267 			if (reserved ("and", i))
   1268 				++i;
   1269 			else
   1270 				errout ("Expected AND");
   1271 		}
   1272 		if (reserved ("atn", i)) {
   1273 			inst0 |= 0x0008;
   1274 			++i;
   1275 		}
   1276 		else if (reserved ("ack", i)) {
   1277 			inst0 |= 0x0040;
   1278 			++i;
   1279 		}
   1280 		else if (reserved ("target", i)) {
   1281 			inst0 |= 0x0200;
   1282 			++i;
   1283 		}
   1284 		else if (reserved ("carry", i)) {
   1285 			inst0 |= 0x0400;
   1286 			++i;
   1287 		}
   1288 		else
   1289 			errout ("Expected ATN, ACK, TARGET or CARRY");
   1290 		need_and = 1;
   1291 	}
   1292 	store_inst ();
   1293 }
   1294 
   1295 void	block_move ()
   1296 {
   1297 	if (reserved ("from", tokenix)) {
   1298 		if (arch < ARCH710) {
   1299 			errout ("Wrong arch for table indirect");
   1300 			return;
   1301 		}
   1302 		inst1 = evaluate (tokenix+1);
   1303 		inst0 |= 0x10000000 | inst1;	/*** ??? to match Zeus script */
   1304 		tokenix += 2;
   1305 	}
   1306 	else {
   1307 		inst0 |= evaluate (tokenix++);	/* count */
   1308 		tokenix++;			/* skip ',' */
   1309 		if (reserved ("ptr", tokenix)) {
   1310 			++ tokenix;
   1311 			inst0 |= 0x20000000;
   1312 		}
   1313 		inst1 = evaluate (tokenix++);	/* address */
   1314 	}
   1315 	if (tokens[tokenix].type != ',')
   1316 		errout ("Expected separator");
   1317 	if (reserved ("when", tokenix + 1)) {
   1318 		inst0 |= 0x08000000;
   1319 		CheckPhase (tokenix + 2);
   1320 	}
   1321 	else if (reserved ("with", tokenix + 1)) {
   1322 		CheckPhase (tokenix + 2);
   1323 	}
   1324 	else
   1325 		errout ("Expected WITH or WHEN");
   1326 }
   1327 
   1328 void	register_write ()
   1329 {
   1330 	/*
   1331 	 * MOVE reg/data8 TO reg			register write
   1332 	 * MOVE reg <op> data8 TO reg			register write
   1333 	 * MOVE reg + data8 TO reg WITH CARRY		register write
   1334 	 */
   1335 	int	op;
   1336 	int	reg;
   1337 	int	data;
   1338 
   1339 	if (reserved ("to", tokenix+1))
   1340 		op = 0;
   1341 	else if (reserved ("shl", tokenix+1))
   1342 		op = 1;
   1343 	else if (reserved ("shr", tokenix+1))
   1344 		op = 5;
   1345 	else if (tokens[tokenix+1].type == '|')
   1346 		op = 2;
   1347 	else if (reserved ("xor", tokenix+1))
   1348 		op = 3;
   1349 	else if (tokens[tokenix+1].type == '&')
   1350 		op = 4;
   1351 	else if (tokens[tokenix+1].type == '+')
   1352 		op = 6;
   1353 	else if (tokens[tokenix+1].type == '-')
   1354 		op = 8;
   1355 	else
   1356 		errout ("Unknown register operator");
   1357 	switch (op) {
   1358 	case 2:
   1359 	case 3:
   1360 	case 4:
   1361 	case 6:
   1362 	case 8:
   1363 		if (reserved ("to", tokenix+3) == 0)
   1364 			errout ("Register command expected TO");
   1365 	default:
   1366 	}
   1367 	reg = CheckRegister (tokenix);
   1368 	if (reg < 0) {			/* Not register, must be data */
   1369 		data = evaluate (tokenix);
   1370 		if (op)
   1371 			errout ("Register operator not move");
   1372 		reg = CheckRegister (tokenix+2);
   1373 		if (reg < 0)
   1374 			errout ("Expected register");
   1375 		inst0 = 0x78000000 | (data << 8) | reg << 16;
   1376 #if 0
   1377 fprintf (listfp, "Move data to register: %02x %d\n", data, reg);
   1378 #endif
   1379 	} else if (op) {
   1380 		switch (op) {
   1381 		case 2:
   1382 		case 3:
   1383 		case 4:
   1384 		case 6:
   1385 		case 8:
   1386 			inst0 = 0;
   1387 			/* A register read/write operator */
   1388 			if (reserved("sfbr", tokenix+2)) {
   1389 				if (arch < ARCH825)
   1390 					errout("wrong arch for add with SFBR");
   1391 				if (op == 8)
   1392 					errout("can't substract SFBR");
   1393 				inst0 |= 0x00800000;
   1394 				data = 0;
   1395 			} else
   1396 				data = evaluate (tokenix+2);
   1397 			if (tokenix+5 < ntokens) {
   1398 				if (!reserved("with", tokenix+5) ||
   1399 				    !reserved("carry", tokenix+6)) {
   1400 					errout("Expected 'WITH CARRY'");
   1401 				} else if (op != 6) {
   1402 					errout("'WITH CARRY' only valide "
   1403 					    "with '+'");
   1404 				}
   1405 				op = 7;
   1406 			}
   1407 			if (op == 8) {
   1408 				data = -data;
   1409 				op = 6;
   1410 			}
   1411 			inst0 |= (data & 0xff) << 8;
   1412 			data = CheckRegister (tokenix+4);
   1413 			break;
   1414 		default:
   1415 			data = CheckRegister (tokenix+2);
   1416 			break;
   1417 		}
   1418 		if (data < 0)
   1419 			errout ("Expected register");
   1420 		if (reg != data && reg != 8 && data != 8)
   1421 			errout ("One register MUST be SBFR");
   1422 		if (reg == data) {	/* A register read/modify/write */
   1423 #if 0
   1424 fprintf (listfp, "Read/modify register: %02x %d %d\n", inst0 >> 8, op, reg);
   1425 #endif
   1426 			inst0 |= 0x78000000 | (op << 24) | (reg << 16);
   1427 		}
   1428 		else {			/* A move to/from SFBR */
   1429 			if (reg == 8) {	/* MOVE SFBR <> TO reg */
   1430 #if 0
   1431 fprintf (listfp, "Move SFBR to register: %02x %d %d\n", inst0 >> 8, op, data);
   1432 #endif
   1433 				inst0 |= 0x68000000 | (op << 24) | (data << 16);
   1434 			}
   1435 			else {
   1436 #if 0
   1437 fprintf (listfp, "Move register to SFBR: %02x %d %d\n", inst0 >> 8, op, reg);
   1438 #endif
   1439 				inst0 |= 0x70000000 | (op << 24) | (reg << 16);
   1440 			}
   1441 		}
   1442 	} else {				/* register to register */
   1443 		data = CheckRegister (tokenix+2);
   1444 		if (data < 0)
   1445 			errout ("Expected register");
   1446 		if (reg == 8)		/* move SFBR to reg */
   1447 			inst0 = 0x6a000000 | (data << 16);
   1448 		else if (data == 8)	/* move reg to SFBR */
   1449 			inst0 = 0x72000000 | (reg << 16);
   1450 		else
   1451 			errout ("One register must be SFBR");
   1452 	}
   1453 }
   1454 
   1455 void	memory_to_memory ()
   1456 {
   1457 	inst0 = 0xc0000000 + evaluate (tokenix+1);
   1458 	inst1 = evaluate (tokenix+3);
   1459 	/*
   1460 	 * need to hack dsps, otherwise patch offset will be wrong for
   1461 	 * second pointer
   1462 	 */
   1463 	dsps += 4;
   1464 	inst2 = evaluate (tokenix+5);
   1465 	dsps -= 4;
   1466 }
   1467 
   1468 void	error_line()
   1469 {
   1470 	if (errfp != listfp && errfp && err_listed == 0) {
   1471 		fprintf (errfp, "%3d:  %s", lineno, inbuf);
   1472 		err_listed = 1;
   1473 	}
   1474 }
   1475 
   1476 char *	makefn (base, sub)
   1477 	char *base;
   1478 	char *sub;
   1479 {
   1480 	char *fn;
   1481 
   1482 	fn = malloc (strlen (base) + strlen (sub) + 2);
   1483 	strcpy (fn, base);
   1484 	base = strrchr(fn, '.');
   1485 	if (base)
   1486 		*base = 0;
   1487 	strcat (fn, ".");
   1488 	strcat (fn, sub);
   1489 	return (fn);
   1490 }
   1491 
   1492 void	usage()
   1493 {
   1494 	fprintf (stderr, "usage: scc sourcfile [options]\n");
   1495 	exit(1);
   1496 }
   1497