script_asm.pl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. #!/usr/bin/perl -s
  2. # NCR 53c810 script assembler
  3. # Sponsored by
  4. # iX Multiuser Multitasking Magazine
  5. #
  6. # Copyright 1993, Drew Eckhardt
  7. # Visionary Computing
  8. # (Unix and Linux consulting and custom programming)
  9. # drew@Colorado.EDU
  10. # +1 (303) 786-7975
  11. #
  12. # Support for 53c710 (via -ncr7x0_family switch) added by Richard
  13. # Hirst <richard@sleepie.demon.co.uk> - 15th March 1997
  14. #
  15. # This program is free software; you can redistribute it and/or modify
  16. # it under the terms of the GNU General Public License as published by
  17. # the Free Software Foundation; either version 2 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program; if not, write to the Free Software
  27. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. #
  29. # TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
  30. #
  31. #
  32. # Basically, I follow the NCR syntax documented in the NCR53c710
  33. # Programmer's guide, with the new instructions, registers, etc.
  34. # from the NCR53c810.
  35. #
  36. # Differences between this assembler and NCR's are that
  37. # 1. PASS, REL (data, JUMPs work fine), and the option to start a new
  38. # script, are unimplemented, since I didn't use them in my scripts.
  39. #
  40. # 2. I also emit a script_u.h file, which will undefine all of
  41. # the A_*, E_*, etc. symbols defined in the script. This
  42. # makes including multiple scripts in one program easier
  43. #
  44. # 3. This is a single pass assembler, which only emits
  45. # .h files.
  46. #
  47. # XXX - set these with command line options
  48. $debug = 0; # Print general debugging messages
  49. $debug_external = 0; # Print external/forward reference messages
  50. $list_in_array = 1; # Emit original SCRIPTS assembler in comments in
  51. # script.h
  52. #$prefix; # (set by perl -s)
  53. # define all arrays having this prefix so we
  54. # don't have name space collisions after
  55. # assembling this file in different ways for
  56. # different host adapters
  57. # Constants
  58. # Table of the SCSI phase encodings
  59. %scsi_phases = (
  60. 'DATA_OUT', 0x00_00_00_00, 'DATA_IN', 0x01_00_00_00, 'CMD', 0x02_00_00_00,
  61. 'STATUS', 0x03_00_00_00, 'MSG_OUT', 0x06_00_00_00, 'MSG_IN', 0x07_00_00_00
  62. );
  63. # XXX - replace references to the *_810 constants with general constants
  64. # assigned at compile time based on chip type.
  65. # Table of operator encodings
  66. # XXX - NCR53c710 only implements
  67. # move (nop) = 0x00_00_00_00
  68. # or = 0x02_00_00_00
  69. # and = 0x04_00_00_00
  70. # add = 0x06_00_00_00
  71. if ($ncr7x0_family) {
  72. %operators = (
  73. '|', 0x02_00_00_00, 'OR', 0x02_00_00_00,
  74. '&', 0x04_00_00_00, 'AND', 0x04_00_00_00,
  75. '+', 0x06_00_00_00
  76. );
  77. }
  78. else {
  79. %operators = (
  80. 'SHL', 0x01_00_00_00,
  81. '|', 0x02_00_00_00, 'OR', 0x02_00_00_00,
  82. 'XOR', 0x03_00_00_00,
  83. '&', 0x04_00_00_00, 'AND', 0x04_00_00_00,
  84. 'SHR', 0x05_00_00_00,
  85. # Note : low bit of the operator bit should be set for add with
  86. # carry.
  87. '+', 0x06_00_00_00
  88. );
  89. }
  90. # Table of register addresses
  91. if ($ncr7x0_family) {
  92. %registers = (
  93. 'SCNTL0', 0, 'SCNTL1', 1, 'SDID', 2, 'SIEN', 3,
  94. 'SCID', 4, 'SXFER', 5, 'SODL', 6, 'SOCL', 7,
  95. 'SFBR', 8, 'SIDL', 9, 'SBDL', 10, 'SBCL', 11,
  96. 'DSTAT', 12, 'SSTAT0', 13, 'SSTAT1', 14, 'SSTAT2', 15,
  97. 'DSA0', 16, 'DSA1', 17, 'DSA2', 18, 'DSA3', 19,
  98. 'CTEST0', 20, 'CTEST1', 21, 'CTEST2', 22, 'CTEST3', 23,
  99. 'CTEST4', 24, 'CTEST5', 25, 'CTEST6', 26, 'CTEST7', 27,
  100. 'TEMP0', 28, 'TEMP1', 29, 'TEMP2', 30, 'TEMP3', 31,
  101. 'DFIFO', 32, 'ISTAT', 33, 'CTEST8', 34, 'LCRC', 35,
  102. 'DBC0', 36, 'DBC1', 37, 'DBC2', 38, 'DCMD', 39,
  103. 'DNAD0', 40, 'DNAD1', 41, 'DNAD2', 42, 'DNAD3', 43,
  104. 'DSP0', 44, 'DSP1', 45, 'DSP2', 46, 'DSP3', 47,
  105. 'DSPS0', 48, 'DSPS1', 49, 'DSPS2', 50, 'DSPS3', 51,
  106. 'SCRATCH0', 52, 'SCRATCH1', 53, 'SCRATCH2', 54, 'SCRATCH3', 55,
  107. 'DMODE', 56, 'DIEN', 57, 'DWT', 58, 'DCNTL', 59,
  108. 'ADDER0', 60, 'ADDER1', 61, 'ADDER2', 62, 'ADDER3', 63,
  109. );
  110. }
  111. else {
  112. %registers = (
  113. 'SCNTL0', 0, 'SCNTL1', 1, 'SCNTL2', 2, 'SCNTL3', 3,
  114. 'SCID', 4, 'SXFER', 5, 'SDID', 6, 'GPREG', 7,
  115. 'SFBR', 8, 'SOCL', 9, 'SSID', 10, 'SBCL', 11,
  116. 'DSTAT', 12, 'SSTAT0', 13, 'SSTAT1', 14, 'SSTAT2', 15,
  117. 'DSA0', 16, 'DSA1', 17, 'DSA2', 18, 'DSA3', 19,
  118. 'ISTAT', 20,
  119. 'CTEST0', 24, 'CTEST1', 25, 'CTEST2', 26, 'CTEST3', 27,
  120. 'TEMP0', 28, 'TEMP1', 29, 'TEMP2', 30, 'TEMP3', 31,
  121. 'DFIFO', 32, 'CTEST4', 33, 'CTEST5', 34, 'CTEST6', 35,
  122. 'DBC0', 36, 'DBC1', 37, 'DBC2', 38, 'DCMD', 39,
  123. 'DNAD0', 40, 'DNAD1', 41, 'DNAD2', 42, 'DNAD3', 43,
  124. 'DSP0', 44, 'DSP1', 45, 'DSP2', 46, 'DSP3', 47,
  125. 'DSPS0', 48, 'DSPS1', 49, 'DSPS2', 50, 'DSPS3', 51,
  126. 'SCRATCH0', 52, 'SCRATCH1', 53, 'SCRATCH2', 54, 'SCRATCH3', 55,
  127. 'SCRATCHA0', 52, 'SCRATCHA1', 53, 'SCRATCHA2', 54, 'SCRATCHA3', 55,
  128. 'DMODE', 56, 'DIEN', 57, 'DWT', 58, 'DCNTL', 59,
  129. 'ADDER0', 60, 'ADDER1', 61, 'ADDER2', 62, 'ADDER3', 63,
  130. 'SIEN0', 64, 'SIEN1', 65, 'SIST0', 66, 'SIST1', 67,
  131. 'SLPAR', 68, 'MACNTL', 70, 'GPCNTL', 71,
  132. 'STIME0', 72, 'STIME1', 73, 'RESPID', 74,
  133. 'STEST0', 76, 'STEST1', 77, 'STEST2', 78, 'STEST3', 79,
  134. 'SIDL', 80,
  135. 'SODL', 84,
  136. 'SBDL', 88,
  137. 'SCRATCHB0', 92, 'SCRATCHB1', 93, 'SCRATCHB2', 94, 'SCRATCHB3', 95
  138. );
  139. }
  140. # Parsing regular expressions
  141. $identifier = '[A-Za-z_][A-Za-z_0-9]*';
  142. $decnum = '-?\\d+';
  143. $hexnum = '0[xX][0-9A-Fa-f]+';
  144. $constant = "$hexnum|$decnum";
  145. # yucky - since we can't control grouping of # $constant, we need to
  146. # expand out each alternative for $value.
  147. $value = "$identifier|$identifier\\s*[+\-]\\s*$decnum|".
  148. "$identifier\\s*[+-]\s*$hexnum|$constant";
  149. print STDERR "value regex = $value\n" if ($debug);
  150. $phase = join ('|', keys %scsi_phases);
  151. print STDERR "phase regex = $phase\n" if ($debug);
  152. $register = join ('|', keys %registers);
  153. # yucky - since %operators includes meta-characters which must
  154. # be escaped, I can't use the join() trick I used for the register
  155. # regex
  156. if ($ncr7x0_family) {
  157. $operator = '\||OR|AND|\&|\+';
  158. }
  159. else {
  160. $operator = '\||OR|AND|XOR|\&|\+';
  161. }
  162. # Global variables
  163. %symbol_values = (%registers) ; # Traditional symbol table
  164. %symbol_references = () ; # Table of symbol references, where
  165. # the index is the symbol name,
  166. # and the contents a white space
  167. # delimited list of address,size
  168. # tuples where size is in bytes.
  169. @code = (); # Array of 32 bit words for SIOP
  170. @entry = (); # Array of entry point names
  171. @label = (); # Array of label names
  172. @absolute = (); # Array of absolute names
  173. @relative = (); # Array of relative names
  174. @external = (); # Array of external names
  175. $address = 0; # Address of current instruction
  176. $lineno = 0; # Line number we are parsing
  177. $output = 'script.h'; # Output file
  178. $outputu = 'scriptu.h';
  179. # &patch ($address, $offset, $length, $value) patches $code[$address]
  180. # so that the $length bytes at $offset have $value added to
  181. # them.
  182. @inverted_masks = (0x00_00_00_00, 0x00_00_00_ff, 0x00_00_ff_ff, 0x00_ff_ff_ff,
  183. 0xff_ff_ff_ff);
  184. sub patch {
  185. local ($address, $offset, $length, $value) = @_;
  186. if ($debug) {
  187. print STDERR "Patching $address at offset $offset, length $length to $value\n";
  188. printf STDERR "Old code : %08x\n", $code[$address];
  189. }
  190. $mask = ($inverted_masks[$length] << ($offset * 8));
  191. $code[$address] = ($code[$address] & ~$mask) |
  192. (($code[$address] & $mask) + ($value << ($offset * 8)) &
  193. $mask);
  194. printf STDERR "New code : %08x\n", $code[$address] if ($debug);
  195. }
  196. # &parse_value($value, $word, $offset, $length) where $value is
  197. # an identifier or constant, $word is the word offset relative to
  198. # $address, $offset is the starting byte within that word, and
  199. # $length is the length of the field in bytes.
  200. #
  201. # Side effects are that the bytes are combined into the @code array
  202. # relative to $address, and that the %symbol_references table is
  203. # updated as appropriate.
  204. sub parse_value {
  205. local ($value, $word, $offset, $length) = @_;
  206. local ($tmp);
  207. $symbol = '';
  208. if ($value =~ /^REL\s*\(\s*($identifier)\s*\)\s*(.*)/i) {
  209. $relative = 'REL';
  210. $symbol = $1;
  211. $value = $2;
  212. print STDERR "Relative reference $symbol\n" if ($debug);
  213. } elsif ($value =~ /^($identifier)\s*(.*)/) {
  214. $relative = 'ABS';
  215. $symbol = $1;
  216. $value = $2;
  217. print STDERR "Absolute reference $symbol\n" if ($debug);
  218. }
  219. if ($symbol ne '') {
  220. print STDERR "Referencing symbol $1, length = $length in $_\n" if ($debug);
  221. $tmp = ($address + $word) * 4 + $offset;
  222. if ($symbol_references{$symbol} ne undef) {
  223. $symbol_references{$symbol} =
  224. "$symbol_references{$symbol} $relative,$tmp,$length";
  225. } else {
  226. if (!defined($symbol_values{$symbol})) {
  227. print STDERR "forward $1\n" if ($debug_external);
  228. $forward{$symbol} = "line $lineno : $_";
  229. }
  230. $symbol_references{$symbol} = "$relative,$tmp,$length";
  231. }
  232. }
  233. $value = eval $value;
  234. &patch ($address + $word, $offset, $length, $value);
  235. }
  236. # &parse_conditional ($conditional) where $conditional is the conditional
  237. # clause from a transfer control instruction (RETURN, CALL, JUMP, INT).
  238. sub parse_conditional {
  239. local ($conditional) = @_;
  240. if ($conditional =~ /^\s*(IF|WHEN)\s*(.*)/i) {
  241. $if = $1;
  242. $conditional = $2;
  243. if ($if =~ /WHEN/i) {
  244. $allow_atn = 0;
  245. $code[$address] |= 0x00_01_00_00;
  246. $allow_atn = 0;
  247. print STDERR "$0 : parsed WHEN\n" if ($debug);
  248. } else {
  249. $allow_atn = 1;
  250. print STDERR "$0 : parsed IF\n" if ($debug);
  251. }
  252. } else {
  253. die "$0 : syntax error in line $lineno : $_
  254. expected IF or WHEN
  255. ";
  256. }
  257. if ($conditional =~ /^NOT\s+(.*)$/i) {
  258. $not = 'NOT ';
  259. $other = 'OR';
  260. $conditional = $1;
  261. print STDERR "$0 : parsed NOT\n" if ($debug);
  262. } else {
  263. $code[$address] |= 0x00_08_00_00;
  264. $not = '';
  265. $other = 'AND'
  266. }
  267. $need_data = 0;
  268. if ($conditional =~ /^ATN\s*(.*)/i) {#
  269. die "$0 : syntax error in line $lineno : $_
  270. WHEN conditional is incompatible with ATN
  271. " if (!$allow_atn);
  272. $code[$address] |= 0x00_02_00_00;
  273. $conditional = $1;
  274. print STDERR "$0 : parsed ATN\n" if ($debug);
  275. } elsif ($conditional =~ /^($phase)\s*(.*)/i) {
  276. $phase_index = "\U$1\E";
  277. $p = $scsi_phases{$phase_index};
  278. $code[$address] |= $p | 0x00_02_00_00;
  279. $conditional = $2;
  280. print STDERR "$0 : parsed phase $phase_index\n" if ($debug);
  281. } else {
  282. $other = '';
  283. $need_data = 1;
  284. }
  285. print STDERR "Parsing conjunction, expecting $other\n" if ($debug);
  286. if ($conditional =~ /^(AND|OR)\s*(.*)/i) {
  287. $conjunction = $1;
  288. $conditional = $2;
  289. $need_data = 1;
  290. die "$0 : syntax error in line $lineno : $_
  291. Illegal use of $1. Valid uses are
  292. ".$not."<phase> $1 data
  293. ".$not."ATN $1 data
  294. " if ($other eq '');
  295. die "$0 : syntax error in line $lineno : $_
  296. Illegal use of $conjunction. Valid syntaxes are
  297. NOT <phase>|ATN OR data
  298. <phase>|ATN AND data
  299. " if ($conjunction !~ /\s*$other\s*/i);
  300. print STDERR "$0 : parsed $1\n" if ($debug);
  301. }
  302. if ($need_data) {
  303. print STDERR "looking for data in $conditional\n" if ($debug);
  304. if ($conditional=~ /^($value)\s*(.*)/i) {
  305. $code[$address] |= 0x00_04_00_00;
  306. $conditional = $2;
  307. &parse_value($1, 0, 0, 1);
  308. print STDERR "$0 : parsed data\n" if ($debug);
  309. } else {
  310. die "$0 : syntax error in line $lineno : $_
  311. expected <data>.
  312. ";
  313. }
  314. }
  315. if ($conditional =~ /^\s*,\s*(.*)/) {
  316. $conditional = $1;
  317. if ($conditional =~ /^AND\s\s*MASK\s\s*($value)\s*(.*)/i) {
  318. &parse_value ($1, 0, 1, 1);
  319. print STDERR "$0 parsed AND MASK $1\n" if ($debug);
  320. die "$0 : syntax error in line $lineno : $_
  321. expected end of line, not \"$2\"
  322. " if ($2 ne '');
  323. } else {
  324. die "$0 : syntax error in line $lineno : $_
  325. expected \",AND MASK <data>\", not \"$2\"
  326. ";
  327. }
  328. } elsif ($conditional !~ /^\s*$/) {
  329. die "$0 : syntax error in line $lineno : $_
  330. expected end of line" . (($need_data) ? " or \"AND MASK <data>\"" : "") . "
  331. not \"$conditional\"
  332. ";
  333. }
  334. }
  335. # Parse command line
  336. $output = shift;
  337. $outputu = shift;
  338. # Main loop
  339. while (<STDIN>) {
  340. $lineno = $lineno + 1;
  341. $list[$address] = $list[$address].$_;
  342. s/;.*$//; # Strip comments
  343. chop; # Leave new line out of error messages
  344. # Handle symbol definitions of the form label:
  345. if (/^\s*($identifier)\s*:(.*)/) {
  346. if (!defined($symbol_values{$1})) {
  347. $symbol_values{$1} = $address * 4; # Address is an index into
  348. delete $forward{$1}; # an array of longs
  349. push (@label, $1);
  350. $_ = $2;
  351. } else {
  352. die "$0 : redefinition of symbol $1 in line $lineno : $_\n";
  353. }
  354. }
  355. # Handle symbol definitions of the form ABSOLUTE or RELATIVE identifier =
  356. # value
  357. if (/^\s*(ABSOLUTE|RELATIVE)\s+(.*)/i) {
  358. $is_absolute = $1;
  359. $rest = $2;
  360. foreach $rest (split (/\s*,\s*/, $rest)) {
  361. if ($rest =~ /^($identifier)\s*=\s*($constant)\s*$/) {
  362. local ($id, $cnst) = ($1, $2);
  363. if ($symbol_values{$id} eq undef) {
  364. $symbol_values{$id} = eval $cnst;
  365. delete $forward{$id};
  366. if ($is_absolute =~ /ABSOLUTE/i) {
  367. push (@absolute , $id);
  368. } else {
  369. push (@relative, $id);
  370. }
  371. } else {
  372. die "$0 : redefinition of symbol $id in line $lineno : $_\n";
  373. }
  374. } else {
  375. die
  376. "$0 : syntax error in line $lineno : $_
  377. expected <identifier> = <value>
  378. ";
  379. }
  380. }
  381. } elsif (/^\s*EXTERNAL\s+(.*)/i) {
  382. $externals = $1;
  383. foreach $external (split (/,/,$externals)) {
  384. if ($external =~ /\s*($identifier)\s*$/) {
  385. $external = $1;
  386. push (@external, $external);
  387. delete $forward{$external};
  388. if (defined($symbol_values{$external})) {
  389. die "$0 : redefinition of symbol $1 in line $lineno : $_\n";
  390. }
  391. $symbol_values{$external} = $external;
  392. print STDERR "defined external $1 to $external\n" if ($debug_external);
  393. } else {
  394. die
  395. "$0 : syntax error in line $lineno : $_
  396. expected <identifier>, got $external
  397. ";
  398. }
  399. }
  400. # Process ENTRY identifier declarations
  401. } elsif (/^\s*ENTRY\s+(.*)/i) {
  402. if ($1 =~ /^($identifier)\s*$/) {
  403. push (@entry, $1);
  404. } else {
  405. die
  406. "$0 : syntax error in line $lineno : $_
  407. expected ENTRY <identifier>
  408. ";
  409. }
  410. # Process MOVE length, address, WITH|WHEN phase instruction
  411. } elsif (/^\s*MOVE\s+(.*)/i) {
  412. $rest = $1;
  413. if ($rest =~ /^FROM\s+($value)\s*,\s*(WITH|WHEN)\s+($phase)\s*$/i) {
  414. $transfer_addr = $1;
  415. $with_when = $2;
  416. $scsi_phase = $3;
  417. print STDERR "Parsing MOVE FROM $transfer_addr, $with_when $3\n" if ($debug);
  418. $code[$address] = 0x18_00_00_00 | (($with_when =~ /WITH/i) ?
  419. 0x00_00_00_00 : 0x08_00_00_00) | $scsi_phases{$scsi_phase};
  420. &parse_value ($transfer_addr, 1, 0, 4);
  421. $address += 2;
  422. } elsif ($rest =~ /^($value)\s*,\s*(PTR\s+|)($value)\s*,\s*(WITH|WHEN)\s+($phase)\s*$/i) {
  423. $transfer_len = $1;
  424. $ptr = $2;
  425. $transfer_addr = $3;
  426. $with_when = $4;
  427. $scsi_phase = $5;
  428. $code[$address] = (($with_when =~ /WITH/i) ? 0x00_00_00_00 :
  429. 0x08_00_00_00) | (($ptr =~ /PTR/i) ? (1 << 29) : 0) |
  430. $scsi_phases{$scsi_phase};
  431. &parse_value ($transfer_len, 0, 0, 3);
  432. &parse_value ($transfer_addr, 1, 0, 4);
  433. $address += 2;
  434. } elsif ($rest =~ /^MEMORY\s+(.*)/i) {
  435. $rest = $1;
  436. $code[$address] = 0xc0_00_00_00;
  437. if ($rest =~ /^($value)\s*,\s*($value)\s*,\s*($value)\s*$/) {
  438. $count = $1;
  439. $source = $2;
  440. $dest = $3;
  441. print STDERR "Parsing MOVE MEMORY $count, $source, $dest\n" if ($debug);
  442. &parse_value ($count, 0, 0, 3);
  443. &parse_value ($source, 1, 0, 4);
  444. &parse_value ($dest, 2, 0, 4);
  445. printf STDERR "Move memory instruction = %08x,%08x,%08x\n",
  446. $code[$address], $code[$address+1], $code[$address +2] if
  447. ($debug);
  448. $address += 3;
  449. } else {
  450. die
  451. "$0 : syntax error in line $lineno : $_
  452. expected <count>, <source>, <destination>
  453. "
  454. }
  455. } elsif ($1 =~ /^(.*)\s+(TO|SHL|SHR)\s+(.*)/i) {
  456. print STDERR "Parsing register to register move\n" if ($debug);
  457. $src = $1;
  458. $op = "\U$2\E";
  459. $rest = $3;
  460. $code[$address] = 0x40_00_00_00;
  461. $force = ($op !~ /TO/i);
  462. print STDERR "Forcing register source \n" if ($force && $debug);
  463. if (!$force && $src =~
  464. /^($register)\s+(-|$operator)\s+($value)\s*$/i) {
  465. print STDERR "register operand data8 source\n" if ($debug);
  466. $src_reg = "\U$1\E";
  467. $op = "\U$2\E";
  468. if ($op ne '-') {
  469. $data8 = $3;
  470. } else {
  471. die "- is not implemented yet.\n"
  472. }
  473. } elsif ($src =~ /^($register)\s*$/i) {
  474. print STDERR "register source\n" if ($debug);
  475. $src_reg = "\U$1\E";
  476. # Encode register to register move as a register | 0
  477. # move to register.
  478. if (!$force) {
  479. $op = '|';
  480. }
  481. $data8 = 0;
  482. } elsif (!$force && $src =~ /^($value)\s*$/i) {
  483. print STDERR "data8 source\n" if ($debug);
  484. $src_reg = undef;
  485. $op = 'NONE';
  486. $data8 = $1;
  487. } else {
  488. if (!$force) {
  489. die
  490. "$0 : syntax error in line $lineno : $_
  491. expected <register>
  492. <data8>
  493. <register> <operand> <data8>
  494. ";
  495. } else {
  496. die
  497. "$0 : syntax error in line $lineno : $_
  498. expected <register>
  499. ";
  500. }
  501. }
  502. if ($rest =~ /^($register)\s*(.*)$/i) {
  503. $dst_reg = "\U$1\E";
  504. $rest = $2;
  505. } else {
  506. die
  507. "$0 : syntax error in $lineno : $_
  508. expected <register>, got $rest
  509. ";
  510. }
  511. if ($rest =~ /^WITH\s+CARRY\s*(.*)/i) {
  512. $rest = $1;
  513. if ($op eq '+') {
  514. $code[$address] |= 0x01_00_00_00;
  515. } else {
  516. die
  517. "$0 : syntax error in $lineno : $_
  518. WITH CARRY option is incompatible with the $op operator.
  519. ";
  520. }
  521. }
  522. if ($rest !~ /^\s*$/) {
  523. die
  524. "$0 : syntax error in $lineno : $_
  525. Expected end of line, got $rest
  526. ";
  527. }
  528. print STDERR "source = $src_reg, data = $data8 , destination = $dst_reg\n"
  529. if ($debug);
  530. # Note that Move data8 to reg is encoded as a read-modify-write
  531. # instruction.
  532. if (($src_reg eq undef) || ($src_reg eq $dst_reg)) {
  533. $code[$address] |= 0x38_00_00_00 |
  534. ($registers{$dst_reg} << 16);
  535. } elsif ($dst_reg =~ /SFBR/i) {
  536. $code[$address] |= 0x30_00_00_00 |
  537. ($registers{$src_reg} << 16);
  538. } elsif ($src_reg =~ /SFBR/i) {
  539. $code[$address] |= 0x28_00_00_00 |
  540. ($registers{$dst_reg} << 16);
  541. } else {
  542. die
  543. "$0 : Illegal combination of registers in line $lineno : $_
  544. Either source and destination registers must be the same,
  545. or either source or destination register must be SFBR.
  546. ";
  547. }
  548. $code[$address] |= $operators{$op};
  549. &parse_value ($data8, 0, 1, 1);
  550. $code[$address] |= $operators{$op};
  551. $code[$address + 1] = 0x00_00_00_00;# Reserved
  552. $address += 2;
  553. } else {
  554. die
  555. "$0 : syntax error in line $lineno : $_
  556. expected (initiator) <length>, <address>, WHEN <phase>
  557. (target) <length>, <address>, WITH <phase>
  558. MEMORY <length>, <source>, <destination>
  559. <expression> TO <register>
  560. ";
  561. }
  562. # Process SELECT {ATN|} id, fail_address
  563. } elsif (/^\s*(SELECT|RESELECT)\s+(.*)/i) {
  564. $rest = $2;
  565. if ($rest =~ /^(ATN|)\s*($value)\s*,\s*($identifier)\s*$/i) {
  566. $atn = $1;
  567. $id = $2;
  568. $alt_addr = $3;
  569. $code[$address] = 0x40_00_00_00 |
  570. (($atn =~ /ATN/i) ? 0x01_00_00_00 : 0);
  571. $code[$address + 1] = 0x00_00_00_00;
  572. &parse_value($id, 0, 2, 1);
  573. &parse_value($alt_addr, 1, 0, 4);
  574. $address += 2;
  575. } elsif ($rest =~ /^(ATN|)\s*FROM\s+($value)\s*,\s*($identifier)\s*$/i) {
  576. $atn = $1;
  577. $addr = $2;
  578. $alt_addr = $3;
  579. $code[$address] = 0x42_00_00_00 |
  580. (($atn =~ /ATN/i) ? 0x01_00_00_00 : 0);
  581. $code[$address + 1] = 0x00_00_00_00;
  582. &parse_value($addr, 0, 0, 3);
  583. &parse_value($alt_addr, 1, 0, 4);
  584. $address += 2;
  585. } else {
  586. die
  587. "$0 : syntax error in line $lineno : $_
  588. expected SELECT id, alternate_address or
  589. SELECT FROM address, alternate_address or
  590. RESELECT id, alternate_address or
  591. RESELECT FROM address, alternate_address
  592. ";
  593. }
  594. } elsif (/^\s*WAIT\s+(.*)/i) {
  595. $rest = $1;
  596. print STDERR "Parsing WAIT $rest\n" if ($debug);
  597. if ($rest =~ /^DISCONNECT\s*$/i) {
  598. $code[$address] = 0x48_00_00_00;
  599. $code[$address + 1] = 0x00_00_00_00;
  600. $address += 2;
  601. } elsif ($rest =~ /^(RESELECT|SELECT)\s+($identifier)\s*$/i) {
  602. $alt_addr = $2;
  603. $code[$address] = 0x50_00_00_00;
  604. &parse_value ($alt_addr, 1, 0, 4);
  605. $address += 2;
  606. } else {
  607. die
  608. "$0 : syntax error in line $lineno : $_
  609. expected (initiator) WAIT DISCONNECT or
  610. (initiator) WAIT RESELECT alternate_address or
  611. (target) WAIT SELECT alternate_address
  612. ";
  613. }
  614. # Handle SET and CLEAR instructions. Note that we should also do something
  615. # with this syntax to set target mode.
  616. } elsif (/^\s*(SET|CLEAR)\s+(.*)/i) {
  617. $set = $1;
  618. $list = $2;
  619. $code[$address] = ($set =~ /SET/i) ? 0x58_00_00_00 :
  620. 0x60_00_00_00;
  621. foreach $arg (split (/\s+AND\s+/i,$list)) {
  622. if ($arg =~ /ATN/i) {
  623. $code[$address] |= 0x00_00_00_08;
  624. } elsif ($arg =~ /ACK/i) {
  625. $code[$address] |= 0x00_00_00_40;
  626. } elsif ($arg =~ /TARGET/i) {
  627. $code[$address] |= 0x00_00_02_00;
  628. } elsif ($arg =~ /CARRY/i) {
  629. $code[$address] |= 0x00_00_04_00;
  630. } else {
  631. die
  632. "$0 : syntax error in line $lineno : $_
  633. expected $set followed by a AND delimited list of one or
  634. more strings from the list ACK, ATN, CARRY, TARGET.
  635. ";
  636. }
  637. }
  638. $code[$address + 1] = 0x00_00_00_00;
  639. $address += 2;
  640. } elsif (/^\s*(JUMP|CALL|INT)\s+(.*)/i) {
  641. $instruction = $1;
  642. $rest = $2;
  643. if ($instruction =~ /JUMP/i) {
  644. $code[$address] = 0x80_00_00_00;
  645. } elsif ($instruction =~ /CALL/i) {
  646. $code[$address] = 0x88_00_00_00;
  647. } else {
  648. $code[$address] = 0x98_00_00_00;
  649. }
  650. print STDERR "parsing JUMP, rest = $rest\n" if ($debug);
  651. # Relative jump.
  652. if ($rest =~ /^(REL\s*\(\s*$identifier\s*\))\s*(.*)/i) {
  653. $addr = $1;
  654. $rest = $2;
  655. print STDERR "parsing JUMP REL, addr = $addr, rest = $rest\n" if ($debug);
  656. $code[$address] |= 0x00_80_00_00;
  657. &parse_value($addr, 1, 0, 4);
  658. # Absolute jump, requires no more gunk
  659. } elsif ($rest =~ /^($value)\s*(.*)/) {
  660. $addr = $1;
  661. $rest = $2;
  662. &parse_value($addr, 1, 0, 4);
  663. } else {
  664. die
  665. "$0 : syntax error in line $lineno : $_
  666. expected <address> or REL (address)
  667. ";
  668. }
  669. if ($rest =~ /^,\s*(.*)/) {
  670. &parse_conditional($1);
  671. } elsif ($rest =~ /^\s*$/) {
  672. $code[$address] |= (1 << 19);
  673. } else {
  674. die
  675. "$0 : syntax error in line $lineno : $_
  676. expected , <conditional> or end of line, got $1
  677. ";
  678. }
  679. $address += 2;
  680. } elsif (/^\s*(RETURN|INTFLY)\s*(.*)/i) {
  681. $instruction = $1;
  682. $conditional = $2;
  683. print STDERR "Parsing $instruction\n" if ($debug);
  684. $code[$address] = ($instruction =~ /RETURN/i) ? 0x90_00_00_00 :
  685. 0x98_10_00_00;
  686. if ($conditional =~ /^,\s*(.*)/) {
  687. $conditional = $1;
  688. &parse_conditional ($conditional);
  689. } elsif ($conditional !~ /^\s*$/) {
  690. die
  691. "$0 : syntax error in line $lineno : $_
  692. expected , <conditional>
  693. ";
  694. } else {
  695. $code[$address] |= 0x00_08_00_00;
  696. }
  697. $code[$address + 1] = 0x00_00_00_00;
  698. $address += 2;
  699. } elsif (/^\s*DISCONNECT\s*$/) {
  700. $code[$address] = 0x48_00_00_00;
  701. $code[$address + 1] = 0x00_00_00_00;
  702. $address += 2;
  703. # I'm not sure that I should be including this extension, but
  704. # what the hell?
  705. } elsif (/^\s*NOP\s*$/i) {
  706. $code[$address] = 0x80_88_00_00;
  707. $code[$address + 1] = 0x00_00_00_00;
  708. $address += 2;
  709. # Ignore lines consisting entirely of white space
  710. } elsif (/^\s*$/) {
  711. } else {
  712. die
  713. "$0 : syntax error in line $lineno: $_
  714. expected label:, ABSOLUTE, CLEAR, DISCONNECT, EXTERNAL, MOVE, RESELECT,
  715. SELECT SET, or WAIT
  716. ";
  717. }
  718. }
  719. # Fill in label references
  720. @undefined = keys %forward;
  721. if ($#undefined >= 0) {
  722. print STDERR "Undefined symbols : \n";
  723. foreach $undef (@undefined) {
  724. print STDERR "$undef in $forward{$undef}\n";
  725. }
  726. exit 1;
  727. }
  728. @label_patches = ();
  729. @external_patches = ();
  730. @absolute = sort @absolute;
  731. foreach $i (@absolute) {
  732. foreach $j (split (/\s+/,$symbol_references{$i})) {
  733. $j =~ /(REL|ABS),(.*),(.*)/;
  734. $type = $1;
  735. $address = $2;
  736. $length = $3;
  737. die
  738. "$0 : $symbol $i has invalid relative reference at address $address,
  739. size $length\n"
  740. if ($type eq 'REL');
  741. &patch ($address / 4, $address % 4, $length, $symbol_values{$i});
  742. }
  743. }
  744. foreach $external (@external) {
  745. print STDERR "checking external $external \n" if ($debug_external);
  746. if ($symbol_references{$external} ne undef) {
  747. for $reference (split(/\s+/,$symbol_references{$external})) {
  748. $reference =~ /(REL|ABS),(.*),(.*)/;
  749. $type = $1;
  750. $address = $2;
  751. $length = $3;
  752. die
  753. "$0 : symbol $label is external, has invalid relative reference at $address,
  754. size $length\n"
  755. if ($type eq 'REL');
  756. die
  757. "$0 : symbol $label has invalid reference at $address, size $length\n"
  758. if ((($address % 4) !=0) || ($length != 4));
  759. $symbol = $symbol_values{$external};
  760. $add = $code[$address / 4];
  761. if ($add eq 0) {
  762. $code[$address / 4] = $symbol;
  763. } else {
  764. $add = sprintf ("0x%08x", $add);
  765. $code[$address / 4] = "$symbol + $add";
  766. }
  767. print STDERR "referenced external $external at $1\n" if ($debug_external);
  768. }
  769. }
  770. }
  771. foreach $label (@label) {
  772. if ($symbol_references{$label} ne undef) {
  773. for $reference (split(/\s+/,$symbol_references{$label})) {
  774. $reference =~ /(REL|ABS),(.*),(.*)/;
  775. $type = $1;
  776. $address = $2;
  777. $length = $3;
  778. if ((($address % 4) !=0) || ($length != 4)) {
  779. die "$0 : symbol $label has invalid reference at $1, size $2\n";
  780. }
  781. if ($type eq 'ABS') {
  782. $code[$address / 4] += $symbol_values{$label};
  783. push (@label_patches, $address / 4);
  784. } else {
  785. #
  786. # - The address of the reference should be in the second and last word
  787. # of an instruction
  788. # - Relative jumps, etc. are relative to the DSP of the _next_ instruction
  789. #
  790. # So, we need to add four to the address of the reference, to get
  791. # the address of the next instruction, when computing the reference.
  792. $tmp = $symbol_values{$label} -
  793. ($address + 4);
  794. die
  795. # Relative addressing is limited to 24 bits.
  796. "$0 : symbol $label is too far ($tmp) from $address to reference as
  797. relative/\n" if (($tmp >= 0x80_00_00) || ($tmp < -0x80_00_00));
  798. $code[$address / 4] = $tmp & 0x00_ff_ff_ff;
  799. }
  800. }
  801. }
  802. }
  803. # Output SCRIPT[] array, one instruction per line. Optionally
  804. # print the original code too.
  805. open (OUTPUT, ">$output") || die "$0 : can't open $output for writing\n";
  806. open (OUTPUTU, ">$outputu") || die "$0 : can't open $outputu for writing\n";
  807. ($_ = $0) =~ s:.*/::;
  808. print OUTPUT "/* DO NOT EDIT - Generated automatically by ".$_." */\n";
  809. print OUTPUT "static u32 ".$prefix."SCRIPT[] = {\n";
  810. $instructions = 0;
  811. for ($i = 0; $i < $#code; ) {
  812. if ($list_in_array) {
  813. printf OUTPUT "/*\n$list[$i]\nat 0x%08x : */", $i;
  814. }
  815. printf OUTPUT "\t0x%08x,", $code[$i];
  816. printf STDERR "Address $i = %x\n", $code[$i] if ($debug);
  817. if ($code[$i + 1] =~ /\s*($identifier)(.*)$/) {
  818. push (@external_patches, $i+1, $1);
  819. printf OUTPUT "0%s,", $2
  820. } else {
  821. printf OUTPUT "0x%08x,",$code[$i+1];
  822. }
  823. if (($code[$i] & 0xff_00_00_00) == 0xc0_00_00_00) {
  824. if ($code[$i + 2] =~ /$identifier/) {
  825. push (@external_patches, $i+2, $code[$i+2]);
  826. printf OUTPUT "0,\n";
  827. } else {
  828. printf OUTPUT "0x%08x,\n",$code[$i+2];
  829. }
  830. $i += 3;
  831. } else {
  832. printf OUTPUT "\n";
  833. $i += 2;
  834. }
  835. $instructions += 1;
  836. }
  837. print OUTPUT "};\n\n";
  838. foreach $i (@absolute) {
  839. printf OUTPUT "#define A_$i\t0x%08x\n", $symbol_values{$i};
  840. if (defined($prefix) && $prefix ne '') {
  841. printf OUTPUT "#define A_".$i."_used ".$prefix."A_".$i."_used\n";
  842. printf OUTPUTU "#undef A_".$i."_used\n";
  843. }
  844. printf OUTPUTU "#undef A_$i\n";
  845. printf OUTPUT "static u32 A_".$i."_used\[\] __attribute((unused)) = {\n";
  846. printf STDERR "$i is used $symbol_references{$i}\n" if ($debug);
  847. foreach $j (split (/\s+/,$symbol_references{$i})) {
  848. $j =~ /(ABS|REL),(.*),(.*)/;
  849. if ($1 eq 'ABS') {
  850. $address = $2;
  851. $length = $3;
  852. printf OUTPUT "\t0x%08x,\n", $address / 4;
  853. }
  854. }
  855. printf OUTPUT "};\n\n";
  856. }
  857. foreach $i (sort @entry) {
  858. printf OUTPUT "#define Ent_$i\t0x%08x\n", $symbol_values{$i};
  859. printf OUTPUTU "#undef Ent_$i\n", $symbol_values{$i};
  860. }
  861. #
  862. # NCR assembler outputs label patches in the form of indices into
  863. # the code.
  864. #
  865. printf OUTPUT "static u32 ".$prefix."LABELPATCHES[] __attribute((unused)) = {\n";
  866. for $patch (sort {$a <=> $b} @label_patches) {
  867. printf OUTPUT "\t0x%08x,\n", $patch;
  868. }
  869. printf OUTPUT "};\n\n";
  870. $num_external_patches = 0;
  871. printf OUTPUT "static struct {\n\tu32\toffset;\n\tvoid\t\t*address;\n".
  872. "} ".$prefix."EXTERNAL_PATCHES[] __attribute((unused)) = {\n";
  873. while ($ident = pop(@external_patches)) {
  874. $off = pop(@external_patches);
  875. printf OUTPUT "\t{0x%08x, &%s},\n", $off, $ident;
  876. ++$num_external_patches;
  877. }
  878. printf OUTPUT "};\n\n";
  879. printf OUTPUT "static u32 ".$prefix."INSTRUCTIONS __attribute((unused))\t= %d;\n",
  880. $instructions;
  881. printf OUTPUT "static u32 ".$prefix."PATCHES __attribute((unused))\t= %d;\n",
  882. $#label_patches+1;
  883. printf OUTPUT "static u32 ".$prefix."EXTERNAL_PATCHES_LEN __attribute((unused))\t= %d;\n",
  884. $num_external_patches;
  885. close OUTPUT;
  886. close OUTPUTU;