script_asm.pl 28 KB

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