xref: /linux/drivers/scsi/script_asm.pl (revision 74ba9207)
11da177e4SLinus Torvalds#!/usr/bin/perl -s
2*74ba9207SThomas Gleixner# SPDX-License-Identifier: GPL-2.0-or-later
31da177e4SLinus Torvalds
41da177e4SLinus Torvalds# NCR 53c810 script assembler
51da177e4SLinus Torvalds# Sponsored by
61da177e4SLinus Torvalds#       iX Multiuser Multitasking Magazine
71da177e4SLinus Torvalds#
81da177e4SLinus Torvalds# Copyright 1993, Drew Eckhardt
91da177e4SLinus Torvalds#      Visionary Computing
101da177e4SLinus Torvalds#      (Unix and Linux consulting and custom programming)
111da177e4SLinus Torvalds#      drew@Colorado.EDU
121da177e4SLinus Torvalds#      +1 (303) 786-7975
131da177e4SLinus Torvalds#
141da177e4SLinus Torvalds#   Support for 53c710 (via -ncr7x0_family switch) added by Richard
151da177e4SLinus Torvalds#   Hirst <richard@sleepie.demon.co.uk> - 15th March 1997
161da177e4SLinus Torvalds#
171da177e4SLinus Torvalds# TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
181da177e4SLinus Torvalds#
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds#
211da177e4SLinus Torvalds# Basically, I follow the NCR syntax documented in the NCR53c710
221da177e4SLinus Torvalds# Programmer's guide, with the new instructions, registers, etc.
231da177e4SLinus Torvalds# from the NCR53c810.
241da177e4SLinus Torvalds#
251da177e4SLinus Torvalds# Differences between this assembler and NCR's are that
261da177e4SLinus Torvalds# 1.  PASS, REL (data, JUMPs work fine), and the option to start a new
271da177e4SLinus Torvalds#	script,  are unimplemented, since I didn't use them in my scripts.
281da177e4SLinus Torvalds#
291da177e4SLinus Torvalds# 2.  I also emit a script_u.h file, which will undefine all of
301da177e4SLinus Torvalds# 	the A_*, E_*, etc. symbols defined in the script.  This
311da177e4SLinus Torvalds#	makes including multiple scripts in one program easier
321da177e4SLinus Torvalds#
331da177e4SLinus Torvalds# 3.  This is a single pass assembler, which only emits
341da177e4SLinus Torvalds#	.h files.
351da177e4SLinus Torvalds#
361da177e4SLinus Torvalds
371da177e4SLinus Torvalds
381da177e4SLinus Torvalds# XXX - set these with command line options
391da177e4SLinus Torvalds$debug = 0;		# Print general debugging messages
401da177e4SLinus Torvalds$debug_external = 0;	# Print external/forward reference messages
411da177e4SLinus Torvalds$list_in_array = 1;	# Emit original SCRIPTS assembler in comments in
421da177e4SLinus Torvalds			# script.h
431da177e4SLinus Torvalds#$prefix;		# (set by perl -s)
441da177e4SLinus Torvalds                        # define all arrays having this prefix so we
451da177e4SLinus Torvalds			# don't have name space collisions after
461da177e4SLinus Torvalds			# assembling this file in different ways for
471da177e4SLinus Torvalds			# different host adapters
481da177e4SLinus Torvalds
491da177e4SLinus Torvalds# Constants
501da177e4SLinus Torvalds
511da177e4SLinus Torvalds
521da177e4SLinus Torvalds# Table of the SCSI phase encodings
531da177e4SLinus Torvalds%scsi_phases = (
541da177e4SLinus Torvalds    'DATA_OUT', 0x00_00_00_00, 'DATA_IN', 0x01_00_00_00, 'CMD', 0x02_00_00_00,
551da177e4SLinus Torvalds    'STATUS', 0x03_00_00_00, 'MSG_OUT', 0x06_00_00_00, 'MSG_IN', 0x07_00_00_00
561da177e4SLinus Torvalds);
571da177e4SLinus Torvalds
581da177e4SLinus Torvalds# XXX - replace references to the *_810 constants with general constants
591da177e4SLinus Torvalds# assigned at compile time based on chip type.
601da177e4SLinus Torvalds
611da177e4SLinus Torvalds# Table of operator encodings
621da177e4SLinus Torvalds# XXX - NCR53c710 only implements
631da177e4SLinus Torvalds# 	move (nop) = 0x00_00_00_00
641da177e4SLinus Torvalds#	or = 0x02_00_00_00
651da177e4SLinus Torvalds# 	and = 0x04_00_00_00
661da177e4SLinus Torvalds# 	add = 0x06_00_00_00
671da177e4SLinus Torvalds
681da177e4SLinus Torvaldsif ($ncr7x0_family) {
691da177e4SLinus Torvalds  %operators = (
701da177e4SLinus Torvalds    '|', 0x02_00_00_00, 'OR', 0x02_00_00_00,
711da177e4SLinus Torvalds    '&', 0x04_00_00_00, 'AND', 0x04_00_00_00,
721da177e4SLinus Torvalds    '+', 0x06_00_00_00
731da177e4SLinus Torvalds  );
741da177e4SLinus Torvalds}
751da177e4SLinus Torvaldselse {
761da177e4SLinus Torvalds  %operators = (
771da177e4SLinus Torvalds    'SHL',  0x01_00_00_00,
781da177e4SLinus Torvalds    '|', 0x02_00_00_00, 'OR', 0x02_00_00_00,
791da177e4SLinus Torvalds    'XOR', 0x03_00_00_00,
801da177e4SLinus Torvalds    '&', 0x04_00_00_00, 'AND', 0x04_00_00_00,
811da177e4SLinus Torvalds    'SHR', 0x05_00_00_00,
821da177e4SLinus Torvalds    # Note : low bit of the operator bit should be set for add with
831da177e4SLinus Torvalds    # carry.
841da177e4SLinus Torvalds    '+', 0x06_00_00_00
851da177e4SLinus Torvalds  );
861da177e4SLinus Torvalds}
871da177e4SLinus Torvalds
881da177e4SLinus Torvalds# Table of register addresses
891da177e4SLinus Torvalds
901da177e4SLinus Torvaldsif ($ncr7x0_family) {
911da177e4SLinus Torvalds  %registers = (
921da177e4SLinus Torvalds    'SCNTL0', 0, 'SCNTL1', 1, 'SDID', 2, 'SIEN', 3,
931da177e4SLinus Torvalds    'SCID', 4, 'SXFER', 5, 'SODL', 6, 'SOCL', 7,
941da177e4SLinus Torvalds    'SFBR', 8, 'SIDL', 9, 'SBDL', 10, 'SBCL', 11,
951da177e4SLinus Torvalds    'DSTAT', 12, 'SSTAT0', 13, 'SSTAT1', 14, 'SSTAT2', 15,
961da177e4SLinus Torvalds    'DSA0', 16, 'DSA1', 17, 'DSA2', 18, 'DSA3', 19,
971da177e4SLinus Torvalds    'CTEST0', 20, 'CTEST1', 21, 'CTEST2', 22, 'CTEST3', 23,
981da177e4SLinus Torvalds    'CTEST4', 24, 'CTEST5', 25, 'CTEST6', 26, 'CTEST7', 27,
991da177e4SLinus Torvalds    'TEMP0', 28, 'TEMP1', 29, 'TEMP2', 30, 'TEMP3', 31,
1001da177e4SLinus Torvalds    'DFIFO', 32, 'ISTAT', 33, 'CTEST8', 34, 'LCRC', 35,
1011da177e4SLinus Torvalds    'DBC0', 36, 'DBC1', 37, 'DBC2', 38, 'DCMD', 39,
1021da177e4SLinus Torvalds    'DNAD0', 40, 'DNAD1', 41, 'DNAD2', 42, 'DNAD3', 43,
1031da177e4SLinus Torvalds    'DSP0', 44, 'DSP1', 45, 'DSP2', 46, 'DSP3', 47,
1041da177e4SLinus Torvalds    'DSPS0', 48, 'DSPS1', 49, 'DSPS2', 50, 'DSPS3', 51,
1051da177e4SLinus Torvalds    'SCRATCH0', 52, 'SCRATCH1', 53, 'SCRATCH2', 54, 'SCRATCH3', 55,
1061da177e4SLinus Torvalds    'DMODE', 56, 'DIEN', 57, 'DWT', 58, 'DCNTL', 59,
1071da177e4SLinus Torvalds    'ADDER0', 60, 'ADDER1', 61, 'ADDER2', 62, 'ADDER3', 63,
1081da177e4SLinus Torvalds  );
1091da177e4SLinus Torvalds}
1101da177e4SLinus Torvaldselse {
1111da177e4SLinus Torvalds  %registers = (
1121da177e4SLinus Torvalds    'SCNTL0', 0, 'SCNTL1', 1, 'SCNTL2', 2, 'SCNTL3', 3,
1131da177e4SLinus Torvalds    'SCID', 4, 'SXFER', 5, 'SDID', 6, 'GPREG', 7,
1141da177e4SLinus Torvalds    'SFBR', 8, 'SOCL', 9, 'SSID', 10, 'SBCL', 11,
1151da177e4SLinus Torvalds    'DSTAT', 12, 'SSTAT0', 13, 'SSTAT1', 14, 'SSTAT2', 15,
1161da177e4SLinus Torvalds    'DSA0', 16, 'DSA1', 17, 'DSA2', 18, 'DSA3', 19,
1171da177e4SLinus Torvalds    'ISTAT', 20,
1181da177e4SLinus Torvalds    'CTEST0', 24, 'CTEST1', 25, 'CTEST2', 26, 'CTEST3', 27,
1191da177e4SLinus Torvalds    'TEMP0', 28, 'TEMP1', 29, 'TEMP2', 30, 'TEMP3', 31,
1201da177e4SLinus Torvalds    'DFIFO', 32, 'CTEST4', 33, 'CTEST5', 34, 'CTEST6', 35,
1211da177e4SLinus Torvalds    'DBC0', 36, 'DBC1', 37, 'DBC2', 38, 'DCMD', 39,
1221da177e4SLinus Torvalds    'DNAD0', 40, 'DNAD1', 41, 'DNAD2', 42, 'DNAD3', 43,
1231da177e4SLinus Torvalds    'DSP0', 44, 'DSP1', 45, 'DSP2', 46, 'DSP3', 47,
1241da177e4SLinus Torvalds    'DSPS0', 48, 'DSPS1', 49, 'DSPS2', 50, 'DSPS3', 51,
1251da177e4SLinus Torvalds    'SCRATCH0', 52, 'SCRATCH1', 53, 'SCRATCH2', 54, 'SCRATCH3', 55,
1261da177e4SLinus Torvalds    'SCRATCHA0', 52, 'SCRATCHA1', 53, 'SCRATCHA2', 54, 'SCRATCHA3', 55,
1271da177e4SLinus Torvalds    'DMODE', 56, 'DIEN', 57, 'DWT', 58, 'DCNTL', 59,
1281da177e4SLinus Torvalds    'ADDER0', 60, 'ADDER1', 61, 'ADDER2', 62, 'ADDER3', 63,
1291da177e4SLinus Torvalds    'SIEN0', 64, 'SIEN1', 65, 'SIST0', 66, 'SIST1', 67,
1301da177e4SLinus Torvalds    'SLPAR', 68, 	      'MACNTL', 70, 'GPCNTL', 71,
1311da177e4SLinus Torvalds    'STIME0', 72, 'STIME1', 73, 'RESPID', 74,
1321da177e4SLinus Torvalds    'STEST0', 76, 'STEST1', 77, 'STEST2', 78, 'STEST3', 79,
1331da177e4SLinus Torvalds    'SIDL', 80,
1341da177e4SLinus Torvalds    'SODL', 84,
1351da177e4SLinus Torvalds    'SBDL', 88,
1361da177e4SLinus Torvalds    'SCRATCHB0', 92, 'SCRATCHB1', 93, 'SCRATCHB2', 94, 'SCRATCHB3', 95
1371da177e4SLinus Torvalds  );
1381da177e4SLinus Torvalds}
1391da177e4SLinus Torvalds
1401da177e4SLinus Torvalds# Parsing regular expressions
1411da177e4SLinus Torvalds$identifier = '[A-Za-z_][A-Za-z_0-9]*';
1421da177e4SLinus Torvalds$decnum = '-?\\d+';
1431da177e4SLinus Torvalds$hexnum = '0[xX][0-9A-Fa-f]+';
1441da177e4SLinus Torvalds$constant = "$hexnum|$decnum";
1451da177e4SLinus Torvalds
1461da177e4SLinus Torvalds# yucky - since we can't control grouping of # $constant, we need to
1471da177e4SLinus Torvalds# expand out each alternative for $value.
1481da177e4SLinus Torvalds
1491da177e4SLinus Torvalds$value = "$identifier|$identifier\\s*[+\-]\\s*$decnum|".
1501da177e4SLinus Torvalds    "$identifier\\s*[+-]\s*$hexnum|$constant";
1511da177e4SLinus Torvalds
1521da177e4SLinus Torvaldsprint STDERR "value regex = $value\n" if ($debug);
1531da177e4SLinus Torvalds
1541da177e4SLinus Torvalds$phase = join ('|', keys %scsi_phases);
1551da177e4SLinus Torvaldsprint STDERR "phase regex = $phase\n" if ($debug);
1561da177e4SLinus Torvalds$register = join ('|', keys %registers);
1571da177e4SLinus Torvalds
1581da177e4SLinus Torvalds# yucky - since %operators includes meta-characters which must
1591da177e4SLinus Torvalds# be escaped, I can't use the join() trick I used for the register
1601da177e4SLinus Torvalds# regex
1611da177e4SLinus Torvalds
1621da177e4SLinus Torvaldsif ($ncr7x0_family) {
1631da177e4SLinus Torvalds  $operator = '\||OR|AND|\&|\+';
1641da177e4SLinus Torvalds}
1651da177e4SLinus Torvaldselse {
1661da177e4SLinus Torvalds  $operator = '\||OR|AND|XOR|\&|\+';
1671da177e4SLinus Torvalds}
1681da177e4SLinus Torvalds
1691da177e4SLinus Torvalds# Global variables
1701da177e4SLinus Torvalds
1711da177e4SLinus Torvalds%symbol_values = (%registers) ;		# Traditional symbol table
1721da177e4SLinus Torvalds
1731da177e4SLinus Torvalds%symbol_references = () ;		# Table of symbol references, where
1741da177e4SLinus Torvalds					# the index is the symbol name,
1751da177e4SLinus Torvalds					# and the contents a white space
1761da177e4SLinus Torvalds					# delimited list of address,size
1771da177e4SLinus Torvalds					# tuples where size is in bytes.
1781da177e4SLinus Torvalds
1791da177e4SLinus Torvalds@code = ();				# Array of 32 bit words for SIOP
1801da177e4SLinus Torvalds
1811da177e4SLinus Torvalds@entry = ();				# Array of entry point names
1821da177e4SLinus Torvalds
1831da177e4SLinus Torvalds@label = ();				# Array of label names
1841da177e4SLinus Torvalds
1851da177e4SLinus Torvalds@absolute = ();				# Array of absolute names
1861da177e4SLinus Torvalds
1871da177e4SLinus Torvalds@relative = ();				# Array of relative names
1881da177e4SLinus Torvalds
1891da177e4SLinus Torvalds@external = ();				# Array of external names
1901da177e4SLinus Torvalds
1911da177e4SLinus Torvalds$address = 0;				# Address of current instruction
1921da177e4SLinus Torvalds
1931da177e4SLinus Torvalds$lineno = 0;				# Line number we are parsing
1941da177e4SLinus Torvalds
1951da177e4SLinus Torvalds$output = 'script.h';			# Output file
1961da177e4SLinus Torvalds$outputu = 'scriptu.h';
1971da177e4SLinus Torvalds
1981da177e4SLinus Torvalds# &patch ($address, $offset, $length, $value) patches $code[$address]
1991da177e4SLinus Torvalds# 	so that the $length bytes at $offset have $value added to
2001da177e4SLinus Torvalds# 	them.
2011da177e4SLinus Torvalds
2021da177e4SLinus Torvalds@inverted_masks = (0x00_00_00_00, 0x00_00_00_ff, 0x00_00_ff_ff, 0x00_ff_ff_ff,
2031da177e4SLinus Torvalds    0xff_ff_ff_ff);
2041da177e4SLinus Torvalds
2051da177e4SLinus Torvaldssub patch {
2061da177e4SLinus Torvalds    local ($address, $offset, $length, $value) = @_;
2071da177e4SLinus Torvalds    if ($debug) {
2081da177e4SLinus Torvalds	print STDERR "Patching $address at offset $offset, length $length to $value\n";
2091da177e4SLinus Torvalds	printf STDERR "Old code : %08x\n", $code[$address];
2101da177e4SLinus Torvalds     }
2111da177e4SLinus Torvalds
2121da177e4SLinus Torvalds    $mask = ($inverted_masks[$length] << ($offset * 8));
2131da177e4SLinus Torvalds
2141da177e4SLinus Torvalds    $code[$address] = ($code[$address] & ~$mask) |
2151da177e4SLinus Torvalds	(($code[$address] & $mask) + ($value << ($offset * 8)) &
2161da177e4SLinus Torvalds	$mask);
2171da177e4SLinus Torvalds
2181da177e4SLinus Torvalds    printf STDERR "New code : %08x\n", $code[$address] if ($debug);
2191da177e4SLinus Torvalds}
2201da177e4SLinus Torvalds
2211da177e4SLinus Torvalds# &parse_value($value, $word, $offset, $length) where $value is
2221da177e4SLinus Torvalds# 	an identifier or constant, $word is the word offset relative to
2231da177e4SLinus Torvalds#	$address, $offset is the starting byte within that word, and
2241da177e4SLinus Torvalds#	$length is the length of the field in bytes.
2251da177e4SLinus Torvalds#
2261da177e4SLinus Torvalds# Side effects are that the bytes are combined into the @code array
2271da177e4SLinus Torvalds#	relative to $address, and that the %symbol_references table is
2281da177e4SLinus Torvalds# 	updated as appropriate.
2291da177e4SLinus Torvalds
2301da177e4SLinus Torvaldssub parse_value {
2311da177e4SLinus Torvalds    local ($value, $word, $offset, $length) = @_;
2321da177e4SLinus Torvalds    local ($tmp);
2331da177e4SLinus Torvalds
2341da177e4SLinus Torvalds    $symbol = '';
2351da177e4SLinus Torvalds
2361da177e4SLinus Torvalds    if ($value =~ /^REL\s*\(\s*($identifier)\s*\)\s*(.*)/i) {
2371da177e4SLinus Torvalds	$relative = 'REL';
2381da177e4SLinus Torvalds	$symbol = $1;
2391da177e4SLinus Torvalds	$value = $2;
2401da177e4SLinus Torvaldsprint STDERR "Relative reference $symbol\n" if ($debug);
2411da177e4SLinus Torvalds    } elsif ($value =~ /^($identifier)\s*(.*)/) {
2421da177e4SLinus Torvalds	$relative = 'ABS';
2431da177e4SLinus Torvalds	$symbol = $1;
2441da177e4SLinus Torvalds	$value = $2;
2451da177e4SLinus Torvaldsprint STDERR "Absolute reference $symbol\n" if ($debug);
2461da177e4SLinus Torvalds    }
2471da177e4SLinus Torvalds
2481da177e4SLinus Torvalds    if ($symbol ne '') {
2491da177e4SLinus Torvaldsprint STDERR "Referencing symbol $1, length = $length in $_\n" if ($debug);
2501da177e4SLinus Torvalds     	$tmp = ($address + $word) * 4 + $offset;
2511da177e4SLinus Torvalds	if ($symbol_references{$symbol} ne undef) {
2521da177e4SLinus Torvalds	    $symbol_references{$symbol} =
2531da177e4SLinus Torvalds		"$symbol_references{$symbol} $relative,$tmp,$length";
2541da177e4SLinus Torvalds	} else {
2551da177e4SLinus Torvalds	    if (!defined($symbol_values{$symbol})) {
2561da177e4SLinus Torvaldsprint STDERR "forward $1\n" if ($debug_external);
2571da177e4SLinus Torvalds		$forward{$symbol} = "line $lineno : $_";
2581da177e4SLinus Torvalds	    }
2591da177e4SLinus Torvalds	    $symbol_references{$symbol} = "$relative,$tmp,$length";
2601da177e4SLinus Torvalds	}
2611da177e4SLinus Torvalds    }
2621da177e4SLinus Torvalds
2631da177e4SLinus Torvalds    $value = eval $value;
2641da177e4SLinus Torvalds    &patch ($address + $word, $offset, $length, $value);
2651da177e4SLinus Torvalds}
2661da177e4SLinus Torvalds
2671da177e4SLinus Torvalds# &parse_conditional ($conditional) where $conditional is the conditional
2681da177e4SLinus Torvalds# clause from a transfer control instruction (RETURN, CALL, JUMP, INT).
2691da177e4SLinus Torvalds
2701da177e4SLinus Torvaldssub parse_conditional {
2711da177e4SLinus Torvalds    local ($conditional) = @_;
2721da177e4SLinus Torvalds    if ($conditional =~ /^\s*(IF|WHEN)\s*(.*)/i) {
2731da177e4SLinus Torvalds	$if = $1;
2741da177e4SLinus Torvalds	$conditional = $2;
2751da177e4SLinus Torvalds	if ($if =~ /WHEN/i) {
2761da177e4SLinus Torvalds	    $allow_atn = 0;
2771da177e4SLinus Torvalds	    $code[$address] |= 0x00_01_00_00;
2781da177e4SLinus Torvalds	    $allow_atn = 0;
2791da177e4SLinus Torvalds	    print STDERR "$0 : parsed WHEN\n" if ($debug);
2801da177e4SLinus Torvalds	} else {
2811da177e4SLinus Torvalds	    $allow_atn = 1;
2821da177e4SLinus Torvalds	    print STDERR "$0 : parsed IF\n" if ($debug);
2831da177e4SLinus Torvalds	}
2841da177e4SLinus Torvalds    } else {
2851da177e4SLinus Torvalds	    die "$0 : syntax error in line $lineno : $_
2861da177e4SLinus Torvalds	expected IF or WHEN
2871da177e4SLinus Torvalds";
2881da177e4SLinus Torvalds    }
2891da177e4SLinus Torvalds
2901da177e4SLinus Torvalds    if ($conditional =~ /^NOT\s+(.*)$/i) {
2911da177e4SLinus Torvalds	$not = 'NOT ';
2921da177e4SLinus Torvalds	$other = 'OR';
2931da177e4SLinus Torvalds	$conditional = $1;
2941da177e4SLinus Torvalds	print STDERR "$0 : parsed NOT\n" if ($debug);
2951da177e4SLinus Torvalds    } else {
2961da177e4SLinus Torvalds	$code[$address] |= 0x00_08_00_00;
2971da177e4SLinus Torvalds	$not = '';
2981da177e4SLinus Torvalds	$other = 'AND'
2991da177e4SLinus Torvalds    }
3001da177e4SLinus Torvalds
3011da177e4SLinus Torvalds    $need_data = 0;
3021da177e4SLinus Torvalds    if ($conditional =~ /^ATN\s*(.*)/i) {#
3031da177e4SLinus Torvalds	die "$0 : syntax error in line $lineno : $_
3041da177e4SLinus Torvalds	WHEN conditional is incompatible with ATN
3051da177e4SLinus Torvalds" if (!$allow_atn);
3061da177e4SLinus Torvalds	$code[$address] |= 0x00_02_00_00;
3071da177e4SLinus Torvalds	$conditional = $1;
3081da177e4SLinus Torvalds	print STDERR "$0 : parsed ATN\n" if ($debug);
3091da177e4SLinus Torvalds    } elsif ($conditional =~ /^($phase)\s*(.*)/i) {
3101da177e4SLinus Torvalds	$phase_index = "\U$1\E";
3111da177e4SLinus Torvalds	$p = $scsi_phases{$phase_index};
3121da177e4SLinus Torvalds	$code[$address] |= $p | 0x00_02_00_00;
3131da177e4SLinus Torvalds	$conditional = $2;
3141da177e4SLinus Torvalds	print STDERR "$0 : parsed phase $phase_index\n" if ($debug);
3151da177e4SLinus Torvalds    } else {
3161da177e4SLinus Torvalds	$other = '';
3171da177e4SLinus Torvalds	$need_data = 1;
3181da177e4SLinus Torvalds    }
3191da177e4SLinus Torvalds
3201da177e4SLinus Torvaldsprint STDERR "Parsing conjunction, expecting $other\n" if ($debug);
3211da177e4SLinus Torvalds    if ($conditional =~ /^(AND|OR)\s*(.*)/i) {
3221da177e4SLinus Torvalds	$conjunction = $1;
3231da177e4SLinus Torvalds	$conditional = $2;
3241da177e4SLinus Torvalds	$need_data = 1;
3251da177e4SLinus Torvalds	die "$0 : syntax error in line $lineno : $_
3261da177e4SLinus Torvalds	    Illegal use of $1.  Valid uses are
3271da177e4SLinus Torvalds	    ".$not."<phase> $1 data
3281da177e4SLinus Torvalds	    ".$not."ATN $1 data
3291da177e4SLinus Torvalds" if ($other eq '');
3301da177e4SLinus Torvalds	die "$0 : syntax error in line $lineno : $_
3311da177e4SLinus Torvalds	Illegal use of $conjunction.  Valid syntaxes are
3321da177e4SLinus Torvalds		NOT <phase>|ATN OR data
3331da177e4SLinus Torvalds		<phase>|ATN AND data
3341da177e4SLinus Torvalds" if ($conjunction !~ /\s*$other\s*/i);
3351da177e4SLinus Torvalds	print STDERR "$0 : parsed $1\n" if ($debug);
3361da177e4SLinus Torvalds    }
3371da177e4SLinus Torvalds
3381da177e4SLinus Torvalds    if ($need_data) {
3391da177e4SLinus Torvaldsprint STDERR "looking for data in $conditional\n" if ($debug);
3401da177e4SLinus Torvalds	if ($conditional=~ /^($value)\s*(.*)/i) {
3411da177e4SLinus Torvalds	    $code[$address] |= 0x00_04_00_00;
3421da177e4SLinus Torvalds	    $conditional = $2;
3431da177e4SLinus Torvalds	    &parse_value($1, 0, 0, 1);
3441da177e4SLinus Torvalds	    print STDERR "$0 : parsed data\n" if ($debug);
3451da177e4SLinus Torvalds	} else {
3461da177e4SLinus Torvalds	die "$0 : syntax error in line $lineno : $_
3471da177e4SLinus Torvalds	expected <data>.
3481da177e4SLinus Torvalds";
3491da177e4SLinus Torvalds	}
3501da177e4SLinus Torvalds    }
3511da177e4SLinus Torvalds
3521da177e4SLinus Torvalds    if ($conditional =~ /^\s*,\s*(.*)/) {
3531da177e4SLinus Torvalds	$conditional = $1;
3541da177e4SLinus Torvalds	if ($conditional =~ /^AND\s\s*MASK\s\s*($value)\s*(.*)/i) {
3551da177e4SLinus Torvalds	    &parse_value ($1, 0, 1, 1);
3561da177e4SLinus Torvalds	    print STDERR "$0 parsed AND MASK $1\n" if ($debug);
3571da177e4SLinus Torvalds	    die "$0 : syntax error in line $lineno : $_
3581da177e4SLinus Torvalds	expected end of line, not \"$2\"
3591da177e4SLinus Torvalds" if ($2 ne '');
3601da177e4SLinus Torvalds	} else {
3611da177e4SLinus Torvalds	    die "$0 : syntax error in line $lineno : $_
3621da177e4SLinus Torvalds	expected \",AND MASK <data>\", not \"$2\"
3631da177e4SLinus Torvalds";
3641da177e4SLinus Torvalds	}
3651da177e4SLinus Torvalds    } elsif ($conditional !~ /^\s*$/) {
3661da177e4SLinus Torvalds	die "$0 : syntax error in line $lineno : $_
3671da177e4SLinus Torvalds	expected end of line" . (($need_data) ? " or \"AND MASK <data>\"" : "") . "
3681da177e4SLinus Torvalds	not \"$conditional\"
3691da177e4SLinus Torvalds";
3701da177e4SLinus Torvalds    }
3711da177e4SLinus Torvalds}
3721da177e4SLinus Torvalds
3731da177e4SLinus Torvalds# Parse command line
3741da177e4SLinus Torvalds$output = shift;
3751da177e4SLinus Torvalds$outputu = shift;
3761da177e4SLinus Torvalds
3771da177e4SLinus Torvalds
3781da177e4SLinus Torvalds# Main loop
3791da177e4SLinus Torvaldswhile (<STDIN>) {
3801da177e4SLinus Torvalds    $lineno = $lineno + 1;
3811da177e4SLinus Torvalds    $list[$address] = $list[$address].$_;
3821da177e4SLinus Torvalds    s/;.*$//;				# Strip comments
3831da177e4SLinus Torvalds
3841da177e4SLinus Torvalds
3851da177e4SLinus Torvalds    chop;				# Leave new line out of error messages
3861da177e4SLinus Torvalds
3871da177e4SLinus Torvalds# Handle symbol definitions of the form label:
3881da177e4SLinus Torvalds    if (/^\s*($identifier)\s*:(.*)/) {
3891da177e4SLinus Torvalds	if (!defined($symbol_values{$1})) {
3901da177e4SLinus Torvalds	    $symbol_values{$1} = $address * 4;	# Address is an index into
3911da177e4SLinus Torvalds	    delete $forward{$1};		# an array of longs
3921da177e4SLinus Torvalds	    push (@label, $1);
3931da177e4SLinus Torvalds	    $_ = $2;
3941da177e4SLinus Torvalds	} else {
3951da177e4SLinus Torvalds	    die "$0 : redefinition of symbol $1 in line $lineno : $_\n";
3961da177e4SLinus Torvalds	}
3971da177e4SLinus Torvalds    }
3981da177e4SLinus Torvalds
3991da177e4SLinus Torvalds# Handle symbol definitions of the form ABSOLUTE or RELATIVE identifier =
4001da177e4SLinus Torvalds# value
4011da177e4SLinus Torvalds    if (/^\s*(ABSOLUTE|RELATIVE)\s+(.*)/i) {
4021da177e4SLinus Torvalds	$is_absolute = $1;
4031da177e4SLinus Torvalds	$rest = $2;
4041da177e4SLinus Torvalds	foreach $rest (split (/\s*,\s*/, $rest)) {
4051da177e4SLinus Torvalds	    if ($rest =~ /^($identifier)\s*=\s*($constant)\s*$/) {
4061da177e4SLinus Torvalds	        local ($id, $cnst) = ($1, $2);
4071da177e4SLinus Torvalds		if ($symbol_values{$id} eq undef) {
4081da177e4SLinus Torvalds		    $symbol_values{$id} = eval $cnst;
4091da177e4SLinus Torvalds		    delete $forward{$id};
4101da177e4SLinus Torvalds		    if ($is_absolute =~ /ABSOLUTE/i) {
4111da177e4SLinus Torvalds			push (@absolute , $id);
4121da177e4SLinus Torvalds		    } else {
4131da177e4SLinus Torvalds			push (@relative, $id);
4141da177e4SLinus Torvalds		    }
4151da177e4SLinus Torvalds		} else {
4161da177e4SLinus Torvalds		    die "$0 : redefinition of symbol $id in line $lineno : $_\n";
4171da177e4SLinus Torvalds		}
4181da177e4SLinus Torvalds	    } else {
4191da177e4SLinus Torvalds		die
4201da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
4211da177e4SLinus Torvalds	    expected <identifier> = <value>
4221da177e4SLinus Torvalds";
4231da177e4SLinus Torvalds	    }
4241da177e4SLinus Torvalds	}
4251da177e4SLinus Torvalds    } elsif (/^\s*EXTERNAL\s+(.*)/i) {
4261da177e4SLinus Torvalds	$externals = $1;
4271da177e4SLinus Torvalds	foreach $external (split (/,/,$externals)) {
4281da177e4SLinus Torvalds	    if ($external =~ /\s*($identifier)\s*$/) {
4291da177e4SLinus Torvalds		$external = $1;
4301da177e4SLinus Torvalds		push (@external, $external);
4311da177e4SLinus Torvalds		delete $forward{$external};
4321da177e4SLinus Torvalds		if (defined($symbol_values{$external})) {
4331da177e4SLinus Torvalds			die "$0 : redefinition of symbol $1 in line $lineno : $_\n";
4341da177e4SLinus Torvalds		}
4351da177e4SLinus Torvalds		$symbol_values{$external} = $external;
4361da177e4SLinus Torvaldsprint STDERR "defined external $1 to $external\n" if ($debug_external);
4371da177e4SLinus Torvalds	    } else {
4381da177e4SLinus Torvalds		die
4391da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
4401da177e4SLinus Torvalds	expected <identifier>, got $external
4411da177e4SLinus Torvalds";
4421da177e4SLinus Torvalds	    }
4431da177e4SLinus Torvalds	}
4441da177e4SLinus Torvalds# Process ENTRY identifier declarations
4451da177e4SLinus Torvalds    } elsif (/^\s*ENTRY\s+(.*)/i) {
4461da177e4SLinus Torvalds	if ($1 =~ /^($identifier)\s*$/) {
4471da177e4SLinus Torvalds	    push (@entry, $1);
4481da177e4SLinus Torvalds	} else {
4491da177e4SLinus Torvalds	    die
4501da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
4511da177e4SLinus Torvalds	expected ENTRY <identifier>
4521da177e4SLinus Torvalds";
4531da177e4SLinus Torvalds	}
4541da177e4SLinus Torvalds# Process MOVE length, address, WITH|WHEN phase instruction
4551da177e4SLinus Torvalds    } elsif (/^\s*MOVE\s+(.*)/i) {
4561da177e4SLinus Torvalds	$rest = $1;
4571da177e4SLinus Torvalds	if ($rest =~ /^FROM\s+($value)\s*,\s*(WITH|WHEN)\s+($phase)\s*$/i) {
4581da177e4SLinus Torvalds	    $transfer_addr = $1;
4591da177e4SLinus Torvalds	    $with_when = $2;
4601da177e4SLinus Torvalds	    $scsi_phase = $3;
4611da177e4SLinus Torvaldsprint STDERR "Parsing MOVE FROM $transfer_addr, $with_when $3\n" if ($debug);
4621da177e4SLinus Torvalds	    $code[$address] = 0x18_00_00_00 | (($with_when =~ /WITH/i) ?
4631da177e4SLinus Torvalds		0x00_00_00_00 : 0x08_00_00_00) | $scsi_phases{$scsi_phase};
4641da177e4SLinus Torvalds	    &parse_value ($transfer_addr, 1, 0, 4);
4651da177e4SLinus Torvalds	    $address += 2;
4661da177e4SLinus Torvalds	} elsif ($rest =~ /^($value)\s*,\s*(PTR\s+|)($value)\s*,\s*(WITH|WHEN)\s+($phase)\s*$/i) {
4671da177e4SLinus Torvalds	    $transfer_len = $1;
4681da177e4SLinus Torvalds	    $ptr = $2;
4691da177e4SLinus Torvalds	    $transfer_addr = $3;
4701da177e4SLinus Torvalds	    $with_when = $4;
4711da177e4SLinus Torvalds	    $scsi_phase = $5;
4721da177e4SLinus Torvalds	    $code[$address] = (($with_when =~ /WITH/i) ? 0x00_00_00_00 :
4731da177e4SLinus Torvalds		0x08_00_00_00)  | (($ptr =~ /PTR/i) ? (1 << 29) : 0) |
4741da177e4SLinus Torvalds		$scsi_phases{$scsi_phase};
4751da177e4SLinus Torvalds	    &parse_value ($transfer_len, 0, 0, 3);
4761da177e4SLinus Torvalds	    &parse_value ($transfer_addr, 1, 0, 4);
4771da177e4SLinus Torvalds	    $address += 2;
4781da177e4SLinus Torvalds	} elsif ($rest =~ /^MEMORY\s+(.*)/i) {
4791da177e4SLinus Torvalds	    $rest = $1;
4801da177e4SLinus Torvalds	    $code[$address] = 0xc0_00_00_00;
4811da177e4SLinus Torvalds	    if ($rest =~ /^($value)\s*,\s*($value)\s*,\s*($value)\s*$/) {
4821da177e4SLinus Torvalds		$count = $1;
4831da177e4SLinus Torvalds		$source = $2;
4841da177e4SLinus Torvalds		$dest =  $3;
4851da177e4SLinus Torvaldsprint STDERR "Parsing MOVE MEMORY $count, $source, $dest\n" if ($debug);
4861da177e4SLinus Torvalds		&parse_value ($count, 0, 0, 3);
4871da177e4SLinus Torvalds		&parse_value ($source, 1, 0, 4);
4881da177e4SLinus Torvalds		&parse_value ($dest, 2, 0, 4);
4891da177e4SLinus Torvaldsprintf STDERR "Move memory instruction = %08x,%08x,%08x\n",
4901da177e4SLinus Torvalds		$code[$address], $code[$address+1], $code[$address +2] if
4911da177e4SLinus Torvalds		($debug);
4921da177e4SLinus Torvalds		$address += 3;
4931da177e4SLinus Torvalds
4941da177e4SLinus Torvalds	    } else {
4951da177e4SLinus Torvalds		die
4961da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
4971da177e4SLinus Torvalds	expected <count>, <source>, <destination>
4981da177e4SLinus Torvalds"
4991da177e4SLinus Torvalds	    }
5001da177e4SLinus Torvalds	} elsif ($1 =~ /^(.*)\s+(TO|SHL|SHR)\s+(.*)/i) {
5011da177e4SLinus Torvaldsprint STDERR "Parsing register to register move\n" if ($debug);
5021da177e4SLinus Torvalds	    $src = $1;
5031da177e4SLinus Torvalds	    $op = "\U$2\E";
5041da177e4SLinus Torvalds	    $rest = $3;
5051da177e4SLinus Torvalds
5061da177e4SLinus Torvalds	    $code[$address] = 0x40_00_00_00;
5071da177e4SLinus Torvalds
5081da177e4SLinus Torvalds	    $force = ($op !~ /TO/i);
5091da177e4SLinus Torvalds
5101da177e4SLinus Torvalds
5111da177e4SLinus Torvaldsprint STDERR "Forcing register source \n" if ($force && $debug);
5121da177e4SLinus Torvalds
5131da177e4SLinus Torvalds	    if (!$force && $src =~
5141da177e4SLinus Torvalds		/^($register)\s+(-|$operator)\s+($value)\s*$/i) {
5151da177e4SLinus Torvaldsprint STDERR "register operand  data8 source\n" if ($debug);
5161da177e4SLinus Torvalds		$src_reg = "\U$1\E";
5171da177e4SLinus Torvalds		$op = "\U$2\E";
5181da177e4SLinus Torvalds		if ($op ne '-') {
5191da177e4SLinus Torvalds		    $data8 = $3;
5201da177e4SLinus Torvalds		} else {
5211da177e4SLinus Torvalds		    die "- is not implemented yet.\n"
5221da177e4SLinus Torvalds		}
5231da177e4SLinus Torvalds	    } elsif ($src =~ /^($register)\s*$/i) {
5241da177e4SLinus Torvaldsprint STDERR "register source\n" if ($debug);
5251da177e4SLinus Torvalds		$src_reg = "\U$1\E";
5261da177e4SLinus Torvalds		# Encode register to register move as a register | 0
5271da177e4SLinus Torvalds		# move to register.
5281da177e4SLinus Torvalds		if (!$force) {
5291da177e4SLinus Torvalds		    $op = '|';
5301da177e4SLinus Torvalds		}
5311da177e4SLinus Torvalds		$data8 = 0;
5321da177e4SLinus Torvalds	    } elsif (!$force && $src =~ /^($value)\s*$/i) {
5331da177e4SLinus Torvaldsprint STDERR "data8 source\n" if ($debug);
5341da177e4SLinus Torvalds		$src_reg = undef;
5351da177e4SLinus Torvalds		$op = 'NONE';
5361da177e4SLinus Torvalds		$data8 = $1;
5371da177e4SLinus Torvalds	    } else {
5381da177e4SLinus Torvalds		if (!$force) {
5391da177e4SLinus Torvalds		    die
5401da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
5411da177e4SLinus Torvalds	expected <register>
5421da177e4SLinus Torvalds		<data8>
5431da177e4SLinus Torvalds		<register> <operand> <data8>
5441da177e4SLinus Torvalds";
5451da177e4SLinus Torvalds		} else {
5461da177e4SLinus Torvalds		    die
5471da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
5481da177e4SLinus Torvalds	expected <register>
5491da177e4SLinus Torvalds";
5501da177e4SLinus Torvalds		}
5511da177e4SLinus Torvalds	    }
5521da177e4SLinus Torvalds	    if ($rest =~ /^($register)\s*(.*)$/i) {
5531da177e4SLinus Torvalds		$dst_reg = "\U$1\E";
5541da177e4SLinus Torvalds		$rest = $2;
5551da177e4SLinus Torvalds	    } else {
5561da177e4SLinus Torvalds	    die
5571da177e4SLinus Torvalds"$0 : syntax error in $lineno : $_
5581da177e4SLinus Torvalds	expected <register>, got $rest
5591da177e4SLinus Torvalds";
5601da177e4SLinus Torvalds	    }
5611da177e4SLinus Torvalds
5621da177e4SLinus Torvalds	    if ($rest =~ /^WITH\s+CARRY\s*(.*)/i) {
5631da177e4SLinus Torvalds		$rest = $1;
5641da177e4SLinus Torvalds		if ($op eq '+') {
5651da177e4SLinus Torvalds		    $code[$address] |= 0x01_00_00_00;
5661da177e4SLinus Torvalds		} else {
5671da177e4SLinus Torvalds		    die
5681da177e4SLinus Torvalds"$0 : syntax error in $lineno : $_
5691da177e4SLinus Torvalds	WITH CARRY option is incompatible with the $op operator.
5701da177e4SLinus Torvalds";
5711da177e4SLinus Torvalds		}
5721da177e4SLinus Torvalds	    }
5731da177e4SLinus Torvalds
5741da177e4SLinus Torvalds	    if ($rest !~ /^\s*$/) {
5751da177e4SLinus Torvalds		die
5761da177e4SLinus Torvalds"$0 : syntax error in $lineno : $_
5771da177e4SLinus Torvalds	Expected end of line, got $rest
5781da177e4SLinus Torvalds";
5791da177e4SLinus Torvalds	    }
5801da177e4SLinus Torvalds
5811da177e4SLinus Torvalds	    print STDERR "source = $src_reg, data = $data8 , destination = $dst_reg\n"
5821da177e4SLinus Torvalds		if ($debug);
5831da177e4SLinus Torvalds	    # Note that Move data8 to reg is encoded as a read-modify-write
5841da177e4SLinus Torvalds	    # instruction.
5851da177e4SLinus Torvalds	    if (($src_reg eq undef) || ($src_reg eq $dst_reg)) {
5861da177e4SLinus Torvalds		$code[$address] |= 0x38_00_00_00 |
5871da177e4SLinus Torvalds		    ($registers{$dst_reg} << 16);
5881da177e4SLinus Torvalds	    } elsif ($dst_reg =~ /SFBR/i) {
5891da177e4SLinus Torvalds		$code[$address] |= 0x30_00_00_00 |
5901da177e4SLinus Torvalds		    ($registers{$src_reg} << 16);
5911da177e4SLinus Torvalds	    } elsif ($src_reg =~ /SFBR/i) {
5921da177e4SLinus Torvalds		$code[$address] |= 0x28_00_00_00 |
5931da177e4SLinus Torvalds		    ($registers{$dst_reg} << 16);
5941da177e4SLinus Torvalds	    } else {
5951da177e4SLinus Torvalds		die
5961da177e4SLinus Torvalds"$0 : Illegal combination of registers in line $lineno : $_
5971da177e4SLinus Torvalds	Either source and destination registers must be the same,
5981da177e4SLinus Torvalds	or either source or destination register must be SFBR.
5991da177e4SLinus Torvalds";
6001da177e4SLinus Torvalds	    }
6011da177e4SLinus Torvalds
6021da177e4SLinus Torvalds	    $code[$address] |= $operators{$op};
6031da177e4SLinus Torvalds
6041da177e4SLinus Torvalds	    &parse_value ($data8, 0, 1, 1);
6051da177e4SLinus Torvalds	    $code[$address] |= $operators{$op};
6061da177e4SLinus Torvalds	    $code[$address + 1] = 0x00_00_00_00;# Reserved
6071da177e4SLinus Torvalds	    $address += 2;
6081da177e4SLinus Torvalds	} else {
6091da177e4SLinus Torvalds	    die
6101da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
6111da177e4SLinus Torvalds	expected (initiator) <length>, <address>, WHEN <phase>
6121da177e4SLinus Torvalds		 (target) <length>, <address>, WITH <phase>
6131da177e4SLinus Torvalds		 MEMORY <length>, <source>, <destination>
6141da177e4SLinus Torvalds		 <expression> TO <register>
6151da177e4SLinus Torvalds";
6161da177e4SLinus Torvalds	}
6171da177e4SLinus Torvalds# Process SELECT {ATN|} id, fail_address
6181da177e4SLinus Torvalds    } elsif (/^\s*(SELECT|RESELECT)\s+(.*)/i) {
6191da177e4SLinus Torvalds	$rest = $2;
6201da177e4SLinus Torvalds	if ($rest =~ /^(ATN|)\s*($value)\s*,\s*($identifier)\s*$/i) {
6211da177e4SLinus Torvalds	    $atn = $1;
6221da177e4SLinus Torvalds	    $id = $2;
6231da177e4SLinus Torvalds	    $alt_addr = $3;
6241da177e4SLinus Torvalds	    $code[$address] = 0x40_00_00_00 |
6251da177e4SLinus Torvalds		(($atn =~ /ATN/i) ? 0x01_00_00_00 : 0);
6261da177e4SLinus Torvalds	    $code[$address + 1] = 0x00_00_00_00;
6271da177e4SLinus Torvalds	    &parse_value($id, 0, 2, 1);
6281da177e4SLinus Torvalds	    &parse_value($alt_addr, 1, 0, 4);
6291da177e4SLinus Torvalds	    $address += 2;
6301da177e4SLinus Torvalds	} elsif ($rest =~ /^(ATN|)\s*FROM\s+($value)\s*,\s*($identifier)\s*$/i) {
6311da177e4SLinus Torvalds	    $atn = $1;
6321da177e4SLinus Torvalds	    $addr = $2;
6331da177e4SLinus Torvalds	    $alt_addr = $3;
6341da177e4SLinus Torvalds	    $code[$address] = 0x42_00_00_00 |
6351da177e4SLinus Torvalds		(($atn =~ /ATN/i) ? 0x01_00_00_00 : 0);
6361da177e4SLinus Torvalds	    $code[$address + 1] = 0x00_00_00_00;
6371da177e4SLinus Torvalds	    &parse_value($addr, 0, 0, 3);
6381da177e4SLinus Torvalds	    &parse_value($alt_addr, 1, 0, 4);
6391da177e4SLinus Torvalds	    $address += 2;
6401da177e4SLinus Torvalds        } else {
6411da177e4SLinus Torvalds	    die
6421da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
6431da177e4SLinus Torvalds	expected SELECT id, alternate_address or
6441da177e4SLinus Torvalds		SELECT FROM address, alternate_address or
6451da177e4SLinus Torvalds		RESELECT id, alternate_address or
6461da177e4SLinus Torvalds		RESELECT FROM address, alternate_address
6471da177e4SLinus Torvalds";
6481da177e4SLinus Torvalds	}
6491da177e4SLinus Torvalds    } elsif (/^\s*WAIT\s+(.*)/i) {
6501da177e4SLinus Torvalds	    $rest = $1;
6511da177e4SLinus Torvaldsprint STDERR "Parsing WAIT $rest\n" if ($debug);
6521da177e4SLinus Torvalds	if ($rest =~ /^DISCONNECT\s*$/i) {
6531da177e4SLinus Torvalds	    $code[$address] = 0x48_00_00_00;
6541da177e4SLinus Torvalds	    $code[$address + 1] = 0x00_00_00_00;
6551da177e4SLinus Torvalds	    $address += 2;
6561da177e4SLinus Torvalds	} elsif ($rest =~ /^(RESELECT|SELECT)\s+($identifier)\s*$/i) {
6571da177e4SLinus Torvalds	    $alt_addr = $2;
6581da177e4SLinus Torvalds	    $code[$address] = 0x50_00_00_00;
6591da177e4SLinus Torvalds	    &parse_value ($alt_addr, 1, 0, 4);
6601da177e4SLinus Torvalds	    $address += 2;
6611da177e4SLinus Torvalds	} else {
6621da177e4SLinus Torvalds	    die
6631da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
6641da177e4SLinus Torvalds	expected (initiator) WAIT DISCONNECT or
6651da177e4SLinus Torvalds		 (initiator) WAIT RESELECT alternate_address or
6661da177e4SLinus Torvalds		 (target) WAIT SELECT alternate_address
6671da177e4SLinus Torvalds";
6681da177e4SLinus Torvalds	}
6691da177e4SLinus Torvalds# Handle SET and CLEAR instructions.  Note that we should also do something
6701da177e4SLinus Torvalds# with this syntax to set target mode.
6711da177e4SLinus Torvalds    } elsif (/^\s*(SET|CLEAR)\s+(.*)/i) {
6721da177e4SLinus Torvalds	$set = $1;
6731da177e4SLinus Torvalds	$list = $2;
6741da177e4SLinus Torvalds	$code[$address] = ($set =~ /SET/i) ?  0x58_00_00_00 :
6751da177e4SLinus Torvalds	    0x60_00_00_00;
6761da177e4SLinus Torvalds	foreach $arg (split (/\s+AND\s+/i,$list)) {
6771da177e4SLinus Torvalds	    if ($arg =~ /ATN/i) {
6781da177e4SLinus Torvalds		$code[$address] |= 0x00_00_00_08;
6791da177e4SLinus Torvalds	    } elsif ($arg =~ /ACK/i) {
6801da177e4SLinus Torvalds		$code[$address] |= 0x00_00_00_40;
6811da177e4SLinus Torvalds	    } elsif ($arg =~ /TARGET/i) {
6821da177e4SLinus Torvalds		$code[$address] |= 0x00_00_02_00;
6831da177e4SLinus Torvalds	    } elsif ($arg =~ /CARRY/i) {
6841da177e4SLinus Torvalds		$code[$address] |= 0x00_00_04_00;
6851da177e4SLinus Torvalds	    } else {
6861da177e4SLinus Torvalds		die
6871da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
6881da177e4SLinus Torvalds	expected $set followed by a AND delimited list of one or
6891da177e4SLinus Torvalds	more strings from the list ACK, ATN, CARRY, TARGET.
6901da177e4SLinus Torvalds";
6911da177e4SLinus Torvalds	    }
6921da177e4SLinus Torvalds	}
6931da177e4SLinus Torvalds	$code[$address + 1] = 0x00_00_00_00;
6941da177e4SLinus Torvalds	$address += 2;
6951da177e4SLinus Torvalds    } elsif (/^\s*(JUMP|CALL|INT)\s+(.*)/i) {
6961da177e4SLinus Torvalds	$instruction = $1;
6971da177e4SLinus Torvalds	$rest = $2;
6981da177e4SLinus Torvalds	if ($instruction =~ /JUMP/i) {
6991da177e4SLinus Torvalds	    $code[$address] = 0x80_00_00_00;
7001da177e4SLinus Torvalds	} elsif ($instruction =~ /CALL/i) {
7011da177e4SLinus Torvalds	    $code[$address] = 0x88_00_00_00;
7021da177e4SLinus Torvalds	} else {
7031da177e4SLinus Torvalds	    $code[$address] = 0x98_00_00_00;
7041da177e4SLinus Torvalds	}
7051da177e4SLinus Torvaldsprint STDERR "parsing JUMP, rest = $rest\n" if ($debug);
7061da177e4SLinus Torvalds
7071da177e4SLinus Torvalds# Relative jump.
7081da177e4SLinus Torvalds	if ($rest =~ /^(REL\s*\(\s*$identifier\s*\))\s*(.*)/i) {
7091da177e4SLinus Torvalds	    $addr = $1;
7101da177e4SLinus Torvalds	    $rest = $2;
7111da177e4SLinus Torvaldsprint STDERR "parsing JUMP REL, addr = $addr, rest = $rest\n" if ($debug);
7121da177e4SLinus Torvalds	    $code[$address]  |= 0x00_80_00_00;
7131da177e4SLinus Torvalds	    &parse_value($addr, 1, 0, 4);
7141da177e4SLinus Torvalds# Absolute jump, requires no more gunk
7151da177e4SLinus Torvalds	} elsif ($rest =~ /^($value)\s*(.*)/) {
7161da177e4SLinus Torvalds	    $addr = $1;
7171da177e4SLinus Torvalds	    $rest = $2;
7181da177e4SLinus Torvalds	    &parse_value($addr, 1, 0, 4);
7191da177e4SLinus Torvalds	} else {
7201da177e4SLinus Torvalds	    die
7211da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
7221da177e4SLinus Torvalds	expected <address> or REL (address)
7231da177e4SLinus Torvalds";
7241da177e4SLinus Torvalds	}
7251da177e4SLinus Torvalds
7261da177e4SLinus Torvalds	if ($rest =~ /^,\s*(.*)/) {
7271da177e4SLinus Torvalds	    &parse_conditional($1);
7281da177e4SLinus Torvalds	} elsif ($rest =~ /^\s*$/) {
7291da177e4SLinus Torvalds	    $code[$address] |= (1 << 19);
7301da177e4SLinus Torvalds	} else {
7311da177e4SLinus Torvalds	    die
7321da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
7331da177e4SLinus Torvalds	expected , <conditional> or end of line, got $1
7341da177e4SLinus Torvalds";
7351da177e4SLinus Torvalds	}
7361da177e4SLinus Torvalds
7371da177e4SLinus Torvalds	$address += 2;
7381da177e4SLinus Torvalds    } elsif (/^\s*(RETURN|INTFLY)\s*(.*)/i) {
7391da177e4SLinus Torvalds	$instruction = $1;
7401da177e4SLinus Torvalds	$conditional = $2;
7411da177e4SLinus Torvaldsprint STDERR "Parsing $instruction\n" if ($debug);
7421da177e4SLinus Torvalds	$code[$address] = ($instruction =~ /RETURN/i) ? 0x90_00_00_00 :
7431da177e4SLinus Torvalds	    0x98_10_00_00;
7441da177e4SLinus Torvalds	if ($conditional =~ /^,\s*(.*)/) {
7451da177e4SLinus Torvalds	    $conditional = $1;
7461da177e4SLinus Torvalds	    &parse_conditional ($conditional);
7471da177e4SLinus Torvalds	} elsif ($conditional !~ /^\s*$/) {
7481da177e4SLinus Torvalds	    die
7491da177e4SLinus Torvalds"$0 : syntax error in line $lineno : $_
7501da177e4SLinus Torvalds	expected , <conditional>
7511da177e4SLinus Torvalds";
7521da177e4SLinus Torvalds	} else {
7531da177e4SLinus Torvalds	    $code[$address] |= 0x00_08_00_00;
7541da177e4SLinus Torvalds	}
7551da177e4SLinus Torvalds
7561da177e4SLinus Torvalds	$code[$address + 1] = 0x00_00_00_00;
7571da177e4SLinus Torvalds	$address += 2;
7581da177e4SLinus Torvalds    } elsif (/^\s*DISCONNECT\s*$/) {
7591da177e4SLinus Torvalds	$code[$address] = 0x48_00_00_00;
7601da177e4SLinus Torvalds	$code[$address + 1] = 0x00_00_00_00;
7611da177e4SLinus Torvalds	$address += 2;
7621da177e4SLinus Torvalds# I'm not sure that I should be including this extension, but
7631da177e4SLinus Torvalds# what the hell?
7641da177e4SLinus Torvalds    } elsif (/^\s*NOP\s*$/i) {
7651da177e4SLinus Torvalds	$code[$address] = 0x80_88_00_00;
7661da177e4SLinus Torvalds	$code[$address + 1] = 0x00_00_00_00;
7671da177e4SLinus Torvalds	$address += 2;
7681da177e4SLinus Torvalds# Ignore lines consisting entirely of white space
7691da177e4SLinus Torvalds    } elsif (/^\s*$/) {
7701da177e4SLinus Torvalds    } else {
7711da177e4SLinus Torvalds	die
7721da177e4SLinus Torvalds"$0 : syntax error in line $lineno: $_
7731da177e4SLinus Torvalds	expected label:, ABSOLUTE, CLEAR, DISCONNECT, EXTERNAL, MOVE, RESELECT,
7741da177e4SLinus Torvalds	    SELECT SET, or WAIT
7751da177e4SLinus Torvalds";
7761da177e4SLinus Torvalds    }
7771da177e4SLinus Torvalds}
7781da177e4SLinus Torvalds
7791da177e4SLinus Torvalds# Fill in label references
7801da177e4SLinus Torvalds
7811da177e4SLinus Torvalds@undefined = keys %forward;
7821da177e4SLinus Torvaldsif ($#undefined >= 0) {
7831da177e4SLinus Torvalds    print STDERR "Undefined symbols : \n";
7841da177e4SLinus Torvalds    foreach $undef (@undefined) {
7851da177e4SLinus Torvalds	print STDERR "$undef in $forward{$undef}\n";
7861da177e4SLinus Torvalds    }
7871da177e4SLinus Torvalds    exit 1;
7881da177e4SLinus Torvalds}
7891da177e4SLinus Torvalds
7901da177e4SLinus Torvalds@label_patches = ();
7911da177e4SLinus Torvalds
7921da177e4SLinus Torvalds@external_patches = ();
7931da177e4SLinus Torvalds
7941da177e4SLinus Torvalds@absolute = sort @absolute;
7951da177e4SLinus Torvalds
7961da177e4SLinus Torvaldsforeach $i (@absolute) {
7971da177e4SLinus Torvalds    foreach $j (split (/\s+/,$symbol_references{$i})) {
7981da177e4SLinus Torvalds	$j =~ /(REL|ABS),(.*),(.*)/;
7991da177e4SLinus Torvalds	$type = $1;
8001da177e4SLinus Torvalds	$address = $2;
8011da177e4SLinus Torvalds	$length = $3;
8021da177e4SLinus Torvalds	die
8031da177e4SLinus Torvalds"$0 : $symbol $i has invalid relative reference at address $address,
8041da177e4SLinus Torvalds    size $length\n"
8051da177e4SLinus Torvalds	if ($type eq 'REL');
8061da177e4SLinus Torvalds
8071da177e4SLinus Torvalds	&patch ($address / 4, $address % 4, $length, $symbol_values{$i});
8081da177e4SLinus Torvalds    }
8091da177e4SLinus Torvalds}
8101da177e4SLinus Torvalds
8111da177e4SLinus Torvaldsforeach $external (@external) {
8121da177e4SLinus Torvaldsprint STDERR "checking external $external \n" if ($debug_external);
8131da177e4SLinus Torvalds    if ($symbol_references{$external} ne undef) {
8141da177e4SLinus Torvalds	for $reference (split(/\s+/,$symbol_references{$external})) {
8151da177e4SLinus Torvalds	    $reference =~ /(REL|ABS),(.*),(.*)/;
8161da177e4SLinus Torvalds	    $type = $1;
8171da177e4SLinus Torvalds	    $address = $2;
8181da177e4SLinus Torvalds	    $length = $3;
8191da177e4SLinus Torvalds
8201da177e4SLinus Torvalds	    die
8211da177e4SLinus Torvalds"$0 : symbol $label is external, has invalid relative reference at $address,
8221da177e4SLinus Torvalds    size $length\n"
8231da177e4SLinus Torvalds		if ($type eq 'REL');
8241da177e4SLinus Torvalds
8251da177e4SLinus Torvalds	    die
8261da177e4SLinus Torvalds"$0 : symbol $label has invalid reference at $address, size $length\n"
8271da177e4SLinus Torvalds		if ((($address % 4) !=0) || ($length != 4));
8281da177e4SLinus Torvalds
8291da177e4SLinus Torvalds	    $symbol = $symbol_values{$external};
8301da177e4SLinus Torvalds	    $add = $code[$address / 4];
8311da177e4SLinus Torvalds	    if ($add eq 0) {
8321da177e4SLinus Torvalds		$code[$address / 4] = $symbol;
8331da177e4SLinus Torvalds	    } else {
8341da177e4SLinus Torvalds		$add = sprintf ("0x%08x", $add);
8351da177e4SLinus Torvalds		$code[$address / 4] = "$symbol + $add";
8361da177e4SLinus Torvalds	    }
8371da177e4SLinus Torvalds
8381da177e4SLinus Torvaldsprint STDERR "referenced external $external at $1\n" if ($debug_external);
8391da177e4SLinus Torvalds	}
8401da177e4SLinus Torvalds    }
8411da177e4SLinus Torvalds}
8421da177e4SLinus Torvalds
8431da177e4SLinus Torvaldsforeach $label (@label) {
8441da177e4SLinus Torvalds    if ($symbol_references{$label} ne undef) {
8451da177e4SLinus Torvalds	for $reference (split(/\s+/,$symbol_references{$label})) {
8461da177e4SLinus Torvalds	    $reference =~ /(REL|ABS),(.*),(.*)/;
8471da177e4SLinus Torvalds	    $type = $1;
8481da177e4SLinus Torvalds	    $address = $2;
8491da177e4SLinus Torvalds	    $length = $3;
8501da177e4SLinus Torvalds
8511da177e4SLinus Torvalds	    if ((($address % 4) !=0) || ($length != 4)) {
8521da177e4SLinus Torvalds		die "$0 : symbol $label has invalid reference at $1, size $2\n";
8531da177e4SLinus Torvalds	    }
8541da177e4SLinus Torvalds
8551da177e4SLinus Torvalds	    if ($type eq 'ABS') {
8561da177e4SLinus Torvalds		$code[$address / 4] += $symbol_values{$label};
8571da177e4SLinus Torvalds		push (@label_patches, $address / 4);
8581da177e4SLinus Torvalds	    } else {
8591da177e4SLinus Torvalds#
8601da177e4SLinus Torvalds# - The address of the reference should be in the second and last word
8611da177e4SLinus Torvalds#	of an instruction
8621da177e4SLinus Torvalds# - Relative jumps, etc. are relative to the DSP of the _next_ instruction
8631da177e4SLinus Torvalds#
8641da177e4SLinus Torvalds# So, we need to add four to the address of the reference, to get
8651da177e4SLinus Torvalds# the address of the next instruction, when computing the reference.
8661da177e4SLinus Torvalds
8671da177e4SLinus Torvalds		$tmp = $symbol_values{$label} -
8681da177e4SLinus Torvalds		    ($address + 4);
8691da177e4SLinus Torvalds		die
8701da177e4SLinus Torvalds# Relative addressing is limited to 24 bits.
8711da177e4SLinus Torvalds"$0 : symbol $label is too far ($tmp) from $address to reference as
8721da177e4SLinus Torvalds    relative/\n" if (($tmp >= 0x80_00_00) || ($tmp < -0x80_00_00));
8731da177e4SLinus Torvalds		$code[$address / 4] = $tmp & 0x00_ff_ff_ff;
8741da177e4SLinus Torvalds	    }
8751da177e4SLinus Torvalds	}
8761da177e4SLinus Torvalds    }
8771da177e4SLinus Torvalds}
8781da177e4SLinus Torvalds
8791da177e4SLinus Torvalds# Output SCRIPT[] array, one instruction per line.  Optionally
8801da177e4SLinus Torvalds# print the original code too.
8811da177e4SLinus Torvalds
8821da177e4SLinus Torvaldsopen (OUTPUT, ">$output") || die "$0 : can't open $output for writing\n";
8831da177e4SLinus Torvaldsopen (OUTPUTU, ">$outputu") || die "$0 : can't open $outputu for writing\n";
8841da177e4SLinus Torvalds
8851da177e4SLinus Torvalds($_ = $0) =~ s:.*/::;
8861da177e4SLinus Torvaldsprint OUTPUT "/* DO NOT EDIT - Generated automatically by ".$_." */\n";
8871da177e4SLinus Torvaldsprint OUTPUT "static u32 ".$prefix."SCRIPT[] = {\n";
8881da177e4SLinus Torvalds$instructions = 0;
8891da177e4SLinus Torvaldsfor ($i = 0; $i < $#code; ) {
8901da177e4SLinus Torvalds    if ($list_in_array) {
8911da177e4SLinus Torvalds	printf OUTPUT "/*\n$list[$i]\nat 0x%08x : */", $i;
8921da177e4SLinus Torvalds    }
8931da177e4SLinus Torvalds    printf OUTPUT "\t0x%08x,", $code[$i];
8941da177e4SLinus Torvalds    printf STDERR "Address $i = %x\n", $code[$i] if ($debug);
8951da177e4SLinus Torvalds    if ($code[$i + 1] =~ /\s*($identifier)(.*)$/) {
8961da177e4SLinus Torvalds	push (@external_patches, $i+1, $1);
8971da177e4SLinus Torvalds	printf OUTPUT "0%s,", $2
8981da177e4SLinus Torvalds    } else {
8991da177e4SLinus Torvalds	printf OUTPUT "0x%08x,",$code[$i+1];
9001da177e4SLinus Torvalds    }
9011da177e4SLinus Torvalds
9021da177e4SLinus Torvalds    if (($code[$i] & 0xff_00_00_00) == 0xc0_00_00_00) {
9031da177e4SLinus Torvalds	if ($code[$i + 2] =~ /$identifier/) {
9041da177e4SLinus Torvalds	    push (@external_patches, $i+2, $code[$i+2]);
9051da177e4SLinus Torvalds	    printf OUTPUT "0,\n";
9061da177e4SLinus Torvalds	} else {
9071da177e4SLinus Torvalds	    printf OUTPUT "0x%08x,\n",$code[$i+2];
9081da177e4SLinus Torvalds	}
9091da177e4SLinus Torvalds	$i += 3;
9101da177e4SLinus Torvalds    } else {
9111da177e4SLinus Torvalds	printf OUTPUT "\n";
9121da177e4SLinus Torvalds	$i += 2;
9131da177e4SLinus Torvalds    }
9141da177e4SLinus Torvalds    $instructions += 1;
9151da177e4SLinus Torvalds}
9161da177e4SLinus Torvaldsprint OUTPUT "};\n\n";
9171da177e4SLinus Torvalds
9181da177e4SLinus Torvaldsforeach $i (@absolute) {
9191da177e4SLinus Torvalds    printf OUTPUT "#define A_$i\t0x%08x\n", $symbol_values{$i};
9201da177e4SLinus Torvalds    if (defined($prefix) && $prefix ne '') {
9211da177e4SLinus Torvalds	printf OUTPUT "#define A_".$i."_used ".$prefix."A_".$i."_used\n";
9221da177e4SLinus Torvalds	printf OUTPUTU "#undef A_".$i."_used\n";
9231da177e4SLinus Torvalds    }
9241da177e4SLinus Torvalds    printf OUTPUTU "#undef A_$i\n";
9251da177e4SLinus Torvalds
9261da177e4SLinus Torvalds    printf OUTPUT "static u32 A_".$i."_used\[\] __attribute((unused)) = {\n";
9271da177e4SLinus Torvaldsprintf STDERR "$i is used $symbol_references{$i}\n" if ($debug);
9281da177e4SLinus Torvalds    foreach $j (split (/\s+/,$symbol_references{$i})) {
9291da177e4SLinus Torvalds	$j =~ /(ABS|REL),(.*),(.*)/;
9301da177e4SLinus Torvalds	if ($1 eq 'ABS') {
9311da177e4SLinus Torvalds	    $address = $2;
9321da177e4SLinus Torvalds	    $length = $3;
9331da177e4SLinus Torvalds	    printf OUTPUT "\t0x%08x,\n", $address / 4;
9341da177e4SLinus Torvalds	}
9351da177e4SLinus Torvalds    }
9361da177e4SLinus Torvalds    printf OUTPUT "};\n\n";
9371da177e4SLinus Torvalds}
9381da177e4SLinus Torvalds
9391da177e4SLinus Torvaldsforeach $i (sort @entry) {
9401da177e4SLinus Torvalds    printf OUTPUT "#define Ent_$i\t0x%08x\n", $symbol_values{$i};
9411da177e4SLinus Torvalds    printf OUTPUTU "#undef Ent_$i\n", $symbol_values{$i};
9421da177e4SLinus Torvalds}
9431da177e4SLinus Torvalds
9441da177e4SLinus Torvalds#
9451da177e4SLinus Torvalds# NCR assembler outputs label patches in the form of indices into
9461da177e4SLinus Torvalds# the code.
9471da177e4SLinus Torvalds#
9481da177e4SLinus Torvaldsprintf OUTPUT "static u32 ".$prefix."LABELPATCHES[] __attribute((unused)) = {\n";
9491da177e4SLinus Torvaldsfor $patch (sort {$a <=> $b} @label_patches) {
9501da177e4SLinus Torvalds    printf OUTPUT "\t0x%08x,\n", $patch;
9511da177e4SLinus Torvalds}
9521da177e4SLinus Torvaldsprintf OUTPUT "};\n\n";
9531da177e4SLinus Torvalds
9541da177e4SLinus Torvalds$num_external_patches = 0;
9551da177e4SLinus Torvaldsprintf OUTPUT "static struct {\n\tu32\toffset;\n\tvoid\t\t*address;\n".
9561da177e4SLinus Torvalds    "} ".$prefix."EXTERNAL_PATCHES[] __attribute((unused)) = {\n";
9571da177e4SLinus Torvaldswhile ($ident = pop(@external_patches)) {
9581da177e4SLinus Torvalds    $off = pop(@external_patches);
9591da177e4SLinus Torvalds    printf OUTPUT "\t{0x%08x, &%s},\n", $off, $ident;
9601da177e4SLinus Torvalds    ++$num_external_patches;
9611da177e4SLinus Torvalds}
9621da177e4SLinus Torvaldsprintf OUTPUT "};\n\n";
9631da177e4SLinus Torvalds
9641da177e4SLinus Torvaldsprintf OUTPUT "static u32 ".$prefix."INSTRUCTIONS __attribute((unused))\t= %d;\n",
9651da177e4SLinus Torvalds    $instructions;
9661da177e4SLinus Torvaldsprintf OUTPUT "static u32 ".$prefix."PATCHES __attribute((unused))\t= %d;\n",
9671da177e4SLinus Torvalds    $#label_patches+1;
9681da177e4SLinus Torvaldsprintf OUTPUT "static u32 ".$prefix."EXTERNAL_PATCHES_LEN __attribute((unused))\t= %d;\n",
9691da177e4SLinus Torvalds    $num_external_patches;
9701da177e4SLinus Torvaldsclose OUTPUT;
9711da177e4SLinus Torvaldsclose OUTPUTU;
972