s12z-opc.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134
  1. /* s12z-decode.c -- Freescale S12Z disassembly
  2. Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU opcodes library.
  4. This library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. It is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include <stdio.h>
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include <assert.h>
  21. #include "opcode/s12z.h"
  22. #include "bfd.h"
  23. #include "s12z-opc.h"
  24. typedef int (*insn_bytes_f) (struct mem_read_abstraction_base *);
  25. typedef int (*operands_f) (struct mem_read_abstraction_base *,
  26. int *n_operands, struct operand **operand);
  27. typedef enum optr (*discriminator_f) (struct mem_read_abstraction_base *,
  28. enum optr hint);
  29. enum OPR_MODE
  30. {
  31. OPR_IMMe4,
  32. OPR_REG,
  33. OPR_OFXYS,
  34. OPR_XY_PRE_INC,
  35. OPR_XY_POST_INC,
  36. OPR_XY_PRE_DEC,
  37. OPR_XY_POST_DEC,
  38. OPR_S_PRE_DEC,
  39. OPR_S_POST_INC,
  40. OPR_REG_DIRECT,
  41. OPR_REG_INDIRECT,
  42. OPR_IDX_DIRECT,
  43. OPR_IDX_INDIRECT,
  44. OPR_EXT1,
  45. OPR_IDX2_REG,
  46. OPR_IDX3_DIRECT,
  47. OPR_IDX3_INDIRECT,
  48. OPR_EXT18,
  49. OPR_IDX3_DIRECT_REG,
  50. OPR_EXT3_DIRECT,
  51. OPR_EXT3_INDIRECT
  52. };
  53. struct opr_pb
  54. {
  55. uint8_t mask;
  56. uint8_t value;
  57. int n_operands;
  58. enum OPR_MODE mode;
  59. };
  60. static const struct opr_pb opr_pb[] = {
  61. {0xF0, 0x70, 1, OPR_IMMe4},
  62. {0xF8, 0xB8, 1, OPR_REG},
  63. {0xC0, 0x40, 1, OPR_OFXYS},
  64. {0xEF, 0xE3, 1, OPR_XY_PRE_INC},
  65. {0xEF, 0xE7, 1, OPR_XY_POST_INC},
  66. {0xEF, 0xC3, 1, OPR_XY_PRE_DEC},
  67. {0xEF, 0xC7, 1, OPR_XY_POST_DEC},
  68. {0xFF, 0xFB, 1, OPR_S_PRE_DEC},
  69. {0xFF, 0xFF, 1, OPR_S_POST_INC},
  70. {0xC8, 0x88, 1, OPR_REG_DIRECT},
  71. {0xE8, 0xC8, 1, OPR_REG_INDIRECT},
  72. {0xCE, 0xC0, 2, OPR_IDX_DIRECT},
  73. {0xCE, 0xC4, 2, OPR_IDX_INDIRECT},
  74. {0xC0, 0x00, 2, OPR_EXT1},
  75. {0xC8, 0x80, 3, OPR_IDX2_REG},
  76. {0xFA, 0xF8, 3, OPR_EXT18},
  77. {0xCF, 0xC2, 4, OPR_IDX3_DIRECT},
  78. {0xCF, 0xC6, 4, OPR_IDX3_INDIRECT},
  79. {0xF8, 0xE8, 4, OPR_IDX3_DIRECT_REG},
  80. {0xFF, 0xFA, 4, OPR_EXT3_DIRECT},
  81. {0xFF, 0xFE, 4, OPR_EXT3_INDIRECT},
  82. };
  83. /* Return the number of bytes in a OPR operand, including the XB postbyte.
  84. It does not include any preceeding opcodes. */
  85. static int
  86. x_opr_n_bytes (struct mem_read_abstraction_base *mra, int offset)
  87. {
  88. bfd_byte xb;
  89. int status = mra->read (mra, offset, 1, &xb);
  90. if (status < 0)
  91. return status;
  92. size_t i;
  93. for (i = 0; i < sizeof (opr_pb) / sizeof (opr_pb[0]); ++i)
  94. {
  95. const struct opr_pb *pb = opr_pb + i;
  96. if ((xb & pb->mask) == pb->value)
  97. {
  98. return pb->n_operands;
  99. }
  100. }
  101. return 1;
  102. }
  103. static int
  104. opr_n_bytes_p1 (struct mem_read_abstraction_base *mra)
  105. {
  106. int n = x_opr_n_bytes (mra, 0);
  107. if (n < 0)
  108. return n;
  109. return 1 + n;
  110. }
  111. static int
  112. opr_n_bytes2 (struct mem_read_abstraction_base *mra)
  113. {
  114. int s = x_opr_n_bytes (mra, 0);
  115. if (s < 0)
  116. return s;
  117. int n = x_opr_n_bytes (mra, s);
  118. if (n < 0)
  119. return n;
  120. return s + n + 1;
  121. }
  122. enum BB_MODE
  123. {
  124. BB_REG_REG_REG,
  125. BB_REG_REG_IMM,
  126. BB_REG_OPR_REG,
  127. BB_OPR_REG_REG,
  128. BB_REG_OPR_IMM,
  129. BB_OPR_REG_IMM
  130. };
  131. struct opr_bb
  132. {
  133. uint8_t mask;
  134. uint8_t value;
  135. int n_operands;
  136. bool opr;
  137. enum BB_MODE mode;
  138. };
  139. static const struct opr_bb bb_modes[] =
  140. {
  141. {0x60, 0x00, 2, false, BB_REG_REG_REG},
  142. {0x60, 0x20, 3, false, BB_REG_REG_IMM},
  143. {0x70, 0x40, 2, true, BB_REG_OPR_REG},
  144. {0x70, 0x50, 2, true, BB_OPR_REG_REG},
  145. {0x70, 0x60, 3, true, BB_REG_OPR_IMM},
  146. {0x70, 0x70, 3, true, BB_OPR_REG_IMM}
  147. };
  148. static int
  149. bfextins_n_bytes (struct mem_read_abstraction_base *mra)
  150. {
  151. bfd_byte bb;
  152. int status = mra->read (mra, 0, 1, &bb);
  153. if (status < 0)
  154. return status;
  155. size_t i;
  156. const struct opr_bb *bbs = 0;
  157. for (i = 0; i < sizeof (bb_modes) / sizeof (bb_modes[0]); ++i)
  158. {
  159. bbs = bb_modes + i;
  160. if ((bb & bbs->mask) == bbs->value)
  161. {
  162. break;
  163. }
  164. }
  165. int n = bbs->n_operands;
  166. if (bbs->opr)
  167. {
  168. int x = x_opr_n_bytes (mra, n - 1);
  169. if (x < 0)
  170. return x;
  171. n += x;
  172. }
  173. return n;
  174. }
  175. static int
  176. single (struct mem_read_abstraction_base *mra ATTRIBUTE_UNUSED)
  177. {
  178. return 1;
  179. }
  180. static int
  181. two (struct mem_read_abstraction_base *mra ATTRIBUTE_UNUSED)
  182. {
  183. return 2;
  184. }
  185. static int
  186. three (struct mem_read_abstraction_base *mra ATTRIBUTE_UNUSED)
  187. {
  188. return 3;
  189. }
  190. static int
  191. four (struct mem_read_abstraction_base *mra ATTRIBUTE_UNUSED)
  192. {
  193. return 4;
  194. }
  195. static int
  196. five (struct mem_read_abstraction_base *mra ATTRIBUTE_UNUSED)
  197. {
  198. return 5;
  199. }
  200. static int
  201. pcrel_15bit (struct mem_read_abstraction_base *mra)
  202. {
  203. bfd_byte byte;
  204. int status = mra->read (mra, 0, 1, &byte);
  205. if (status < 0)
  206. return status;
  207. return (byte & 0x80) ? 3 : 2;
  208. }
  209. static int
  210. xysp_reg_from_postbyte (uint8_t postbyte)
  211. {
  212. int reg = -1;
  213. switch ((postbyte & 0x30) >> 4)
  214. {
  215. case 0:
  216. reg = REG_X;
  217. break;
  218. case 1:
  219. reg = REG_Y;
  220. break;
  221. case 2:
  222. reg = REG_S;
  223. break;
  224. default:
  225. reg = REG_P;
  226. }
  227. return reg;
  228. }
  229. static struct operand *
  230. create_immediate_operand (int value)
  231. {
  232. struct immediate_operand *op = malloc (sizeof (*op));
  233. if (op != NULL)
  234. {
  235. op->parent.cl = OPND_CL_IMMEDIATE;
  236. op->parent.osize = -1;
  237. op->value = value;
  238. }
  239. return (struct operand *) op;
  240. }
  241. static struct operand *
  242. create_bitfield_operand (int width, int offset)
  243. {
  244. struct bitfield_operand *op = malloc (sizeof (*op));
  245. if (op != NULL)
  246. {
  247. op->parent.cl = OPND_CL_BIT_FIELD;
  248. op->parent.osize = -1;
  249. op->width = width;
  250. op->offset = offset;
  251. }
  252. return (struct operand *) op;
  253. }
  254. static struct operand *
  255. create_register_operand_with_size (int reg, short osize)
  256. {
  257. struct register_operand *op = malloc (sizeof (*op));
  258. if (op != NULL)
  259. {
  260. op->parent.cl = OPND_CL_REGISTER;
  261. op->parent.osize = osize;
  262. op->reg = reg;
  263. }
  264. return (struct operand *) op;
  265. }
  266. static struct operand *
  267. create_register_operand (int reg)
  268. {
  269. return create_register_operand_with_size (reg, -1);
  270. }
  271. static struct operand *
  272. create_register_all_operand (void)
  273. {
  274. struct register_operand *op = malloc (sizeof (*op));
  275. if (op != NULL)
  276. {
  277. op->parent.cl = OPND_CL_REGISTER_ALL;
  278. op->parent.osize = -1;
  279. }
  280. return (struct operand *) op;
  281. }
  282. static struct operand *
  283. create_register_all16_operand (void)
  284. {
  285. struct register_operand *op = malloc (sizeof (*op));
  286. if (op != NULL)
  287. {
  288. op->parent.cl = OPND_CL_REGISTER_ALL16;
  289. op->parent.osize = -1;
  290. }
  291. return (struct operand *) op;
  292. }
  293. static struct operand *
  294. create_simple_memory_operand (bfd_vma addr, bfd_vma base, bool relative)
  295. {
  296. struct simple_memory_operand *op;
  297. assert (relative || base == 0);
  298. op = malloc (sizeof (*op));
  299. if (op != NULL)
  300. {
  301. op->parent.cl = OPND_CL_SIMPLE_MEMORY;
  302. op->parent.osize = -1;
  303. op->addr = addr;
  304. op->base = base;
  305. op->relative = relative;
  306. }
  307. return (struct operand *) op;
  308. }
  309. static struct operand *
  310. create_memory_operand (bool indirect, int base, int n_regs, int reg0, int reg1)
  311. {
  312. struct memory_operand *op = malloc (sizeof (*op));
  313. if (op != NULL)
  314. {
  315. op->parent.cl = OPND_CL_MEMORY;
  316. op->parent.osize = -1;
  317. op->indirect = indirect;
  318. op->base_offset = base;
  319. op->mutation = OPND_RM_NONE;
  320. op->n_regs = n_regs;
  321. op->regs[0] = reg0;
  322. op->regs[1] = reg1;
  323. }
  324. return (struct operand *) op;
  325. }
  326. static struct operand *
  327. create_memory_auto_operand (enum op_reg_mutation mutation, int reg)
  328. {
  329. struct memory_operand *op = malloc (sizeof (*op));
  330. if (op != NULL)
  331. {
  332. op->parent.cl = OPND_CL_MEMORY;
  333. op->parent.osize = -1;
  334. op->indirect = false;
  335. op->base_offset = 0;
  336. op->mutation = mutation;
  337. op->n_regs = 1;
  338. op->regs[0] = reg;
  339. op->regs[1] = -1;
  340. }
  341. return (struct operand *) op;
  342. }
  343. static int
  344. z_ext24_decode (struct mem_read_abstraction_base *mra, int *n_operands,
  345. struct operand **operand)
  346. {
  347. struct operand *op;
  348. uint8_t buffer[3];
  349. int status = mra->read (mra, 0, 3, buffer);
  350. if (status < 0)
  351. return status;
  352. int i;
  353. uint32_t addr = 0;
  354. for (i = 0; i < 3; ++i)
  355. {
  356. addr <<= 8;
  357. addr |= buffer[i];
  358. }
  359. op = create_simple_memory_operand (addr, 0, false);
  360. if (op == NULL)
  361. return -1;
  362. operand[(*n_operands)++] = op;
  363. return 0;
  364. }
  365. static int
  366. z_decode_signed_value (struct mem_read_abstraction_base *mra, int offset,
  367. short size, uint32_t *result)
  368. {
  369. assert (size >0);
  370. assert (size <= 4);
  371. bfd_byte buffer[4];
  372. int status = mra->read (mra, offset, size, buffer);
  373. if (status < 0)
  374. return status;
  375. int i;
  376. uint32_t value = 0;
  377. for (i = 0; i < size; ++i)
  378. value = (value << 8) | buffer[i];
  379. if (buffer[0] & 0x80)
  380. {
  381. /* Deal with negative values */
  382. value -= 1u << (size * 4) << (size * 4);
  383. }
  384. *result = value;
  385. return 0;
  386. }
  387. static int
  388. decode_signed_value (struct mem_read_abstraction_base *mra, short size,
  389. uint32_t *result)
  390. {
  391. return z_decode_signed_value (mra, 0, size, result);
  392. }
  393. static int
  394. x_imm1 (struct mem_read_abstraction_base *mra,
  395. int offset,
  396. int *n_operands, struct operand **operand)
  397. {
  398. struct operand *op;
  399. bfd_byte byte;
  400. int status = mra->read (mra, offset, 1, &byte);
  401. if (status < 0)
  402. return status;
  403. op = create_immediate_operand (byte);
  404. if (op == NULL)
  405. return -1;
  406. operand[(*n_operands)++] = op;
  407. return 0;
  408. }
  409. /* An eight bit immediate operand. */
  410. static int
  411. imm1_decode (struct mem_read_abstraction_base *mra,
  412. int *n_operands, struct operand **operand)
  413. {
  414. return x_imm1 (mra, 0, n_operands, operand);
  415. }
  416. static int
  417. trap_decode (struct mem_read_abstraction_base *mra,
  418. int *n_operands, struct operand **operand)
  419. {
  420. return x_imm1 (mra, -1, n_operands, operand);
  421. }
  422. static struct operand *
  423. x_opr_decode_with_size (struct mem_read_abstraction_base *mra, int offset,
  424. short osize)
  425. {
  426. bfd_byte postbyte;
  427. int status = mra->read (mra, offset, 1, &postbyte);
  428. if (status < 0)
  429. return NULL;
  430. offset++;
  431. enum OPR_MODE mode = -1;
  432. size_t i;
  433. for (i = 0; i < sizeof (opr_pb) / sizeof (opr_pb[0]); ++i)
  434. {
  435. const struct opr_pb *pb = opr_pb + i;
  436. if ((postbyte & pb->mask) == pb->value)
  437. {
  438. mode = pb->mode;
  439. break;
  440. }
  441. }
  442. struct operand *operand = NULL;
  443. switch (mode)
  444. {
  445. case OPR_IMMe4:
  446. {
  447. int n;
  448. uint8_t x = (postbyte & 0x0F);
  449. if (x == 0)
  450. n = -1;
  451. else
  452. n = x;
  453. operand = create_immediate_operand (n);
  454. break;
  455. }
  456. case OPR_REG:
  457. {
  458. uint8_t x = (postbyte & 0x07);
  459. operand = create_register_operand (x);
  460. break;
  461. }
  462. case OPR_OFXYS:
  463. {
  464. operand = create_memory_operand (false, postbyte & 0x0F, 1,
  465. xysp_reg_from_postbyte (postbyte), -1);
  466. break;
  467. }
  468. case OPR_REG_DIRECT:
  469. {
  470. operand = create_memory_operand (false, 0, 2, postbyte & 0x07,
  471. xysp_reg_from_postbyte (postbyte));
  472. break;
  473. }
  474. case OPR_REG_INDIRECT:
  475. {
  476. operand = create_memory_operand (true, 0, 2, postbyte & 0x07,
  477. (postbyte & 0x10) ? REG_Y : REG_X);
  478. break;
  479. }
  480. case OPR_IDX_INDIRECT:
  481. {
  482. uint8_t x1;
  483. status = mra->read (mra, offset, 1, &x1);
  484. if (status < 0)
  485. return NULL;
  486. int idx = x1;
  487. if (postbyte & 0x01)
  488. {
  489. /* Deal with negative values */
  490. idx -= 0x1UL << 8;
  491. }
  492. operand = create_memory_operand (true, idx, 1,
  493. xysp_reg_from_postbyte (postbyte), -1);
  494. break;
  495. }
  496. case OPR_IDX3_DIRECT:
  497. {
  498. uint8_t x[3];
  499. status = mra->read (mra, offset, 3, x);
  500. if (status < 0)
  501. return NULL;
  502. int idx = x[0] << 16 | x[1] << 8 | x[2];
  503. if (x[0] & 0x80)
  504. {
  505. /* Deal with negative values */
  506. idx -= 0x1UL << 24;
  507. }
  508. operand = create_memory_operand (false, idx, 1,
  509. xysp_reg_from_postbyte (postbyte), -1);
  510. break;
  511. }
  512. case OPR_IDX3_DIRECT_REG:
  513. {
  514. uint8_t x[3];
  515. status = mra->read (mra, offset, 3, x);
  516. if (status < 0)
  517. return NULL;
  518. int idx = x[0] << 16 | x[1] << 8 | x[2];
  519. if (x[0] & 0x80)
  520. {
  521. /* Deal with negative values */
  522. idx -= 0x1UL << 24;
  523. }
  524. operand = create_memory_operand (false, idx, 1, postbyte & 0x07, -1);
  525. break;
  526. }
  527. case OPR_IDX3_INDIRECT:
  528. {
  529. uint8_t x[3];
  530. status = mra->read (mra, offset, 3, x);
  531. if (status < 0)
  532. return NULL;
  533. int idx = x[0] << 16 | x[1] << 8 | x[2];
  534. if (x[0] & 0x80)
  535. {
  536. /* Deal with negative values */
  537. idx -= 0x1UL << 24;
  538. }
  539. operand = create_memory_operand (true, idx, 1,
  540. xysp_reg_from_postbyte (postbyte), -1);
  541. break;
  542. }
  543. case OPR_IDX_DIRECT:
  544. {
  545. uint8_t x1;
  546. status = mra->read (mra, offset, 1, &x1);
  547. if (status < 0)
  548. return NULL;
  549. int idx = x1;
  550. if (postbyte & 0x01)
  551. {
  552. /* Deal with negative values */
  553. idx -= 0x1UL << 8;
  554. }
  555. operand = create_memory_operand (false, idx, 1,
  556. xysp_reg_from_postbyte (postbyte), -1);
  557. break;
  558. }
  559. case OPR_IDX2_REG:
  560. {
  561. uint8_t x[2];
  562. status = mra->read (mra, offset, 2, x);
  563. if (status < 0)
  564. return NULL;
  565. uint32_t idx = x[1] | x[0] << 8 ;
  566. idx |= (postbyte & 0x30) << 12;
  567. operand = create_memory_operand (false, idx, 1, postbyte & 0x07, -1);
  568. break;
  569. }
  570. case OPR_XY_PRE_INC:
  571. {
  572. operand = create_memory_auto_operand (OPND_RM_PRE_INC,
  573. (postbyte & 0x10) ? REG_Y: REG_X);
  574. break;
  575. }
  576. case OPR_XY_POST_INC:
  577. {
  578. operand = create_memory_auto_operand (OPND_RM_POST_INC,
  579. (postbyte & 0x10) ? REG_Y: REG_X);
  580. break;
  581. }
  582. case OPR_XY_PRE_DEC:
  583. {
  584. operand = create_memory_auto_operand (OPND_RM_PRE_DEC,
  585. (postbyte & 0x10) ? REG_Y: REG_X);
  586. break;
  587. }
  588. case OPR_XY_POST_DEC:
  589. {
  590. operand = create_memory_auto_operand (OPND_RM_POST_DEC,
  591. (postbyte & 0x10) ? REG_Y: REG_X);
  592. break;
  593. }
  594. case OPR_S_PRE_DEC:
  595. {
  596. operand = create_memory_auto_operand (OPND_RM_PRE_DEC, REG_S);
  597. break;
  598. }
  599. case OPR_S_POST_INC:
  600. {
  601. operand = create_memory_auto_operand (OPND_RM_POST_INC, REG_S);
  602. break;
  603. }
  604. case OPR_EXT18:
  605. {
  606. const size_t size = 2;
  607. bfd_byte buffer[4];
  608. status = mra->read (mra, offset, size, buffer);
  609. if (status < 0)
  610. return NULL;
  611. uint32_t ext18 = 0;
  612. for (i = 0; i < size; ++i)
  613. {
  614. ext18 <<= 8;
  615. ext18 |= buffer[i];
  616. }
  617. ext18 |= (postbyte & 0x01) << 16;
  618. ext18 |= (postbyte & 0x04) << 15;
  619. operand = create_simple_memory_operand (ext18, 0, false);
  620. break;
  621. }
  622. case OPR_EXT1:
  623. {
  624. uint8_t x1 = 0;
  625. status = mra->read (mra, offset, 1, &x1);
  626. if (status < 0)
  627. return NULL;
  628. int16_t addr;
  629. addr = x1;
  630. addr |= (postbyte & 0x3f) << 8;
  631. operand = create_simple_memory_operand (addr, 0, false);
  632. break;
  633. }
  634. case OPR_EXT3_DIRECT:
  635. {
  636. const size_t size = 3;
  637. bfd_byte buffer[4];
  638. status = mra->read (mra, offset, size, buffer);
  639. if (status < 0)
  640. return NULL;
  641. uint32_t ext24 = 0;
  642. for (i = 0; i < size; ++i)
  643. {
  644. ext24 |= buffer[i] << (8 * (size - i - 1));
  645. }
  646. operand = create_simple_memory_operand (ext24, 0, false);
  647. break;
  648. }
  649. case OPR_EXT3_INDIRECT:
  650. {
  651. const size_t size = 3;
  652. bfd_byte buffer[4];
  653. status = mra->read (mra, offset, size, buffer);
  654. if (status < 0)
  655. return NULL;
  656. uint32_t ext24 = 0;
  657. for (i = 0; i < size; ++i)
  658. {
  659. ext24 |= buffer[i] << (8 * (size - i - 1));
  660. }
  661. operand = create_memory_operand (true, ext24, 0, -1, -1);
  662. break;
  663. }
  664. default:
  665. printf ("Unknown OPR mode #0x%x (%d)", postbyte, mode);
  666. abort ();
  667. }
  668. if (operand != NULL)
  669. operand->osize = osize;
  670. return operand;
  671. }
  672. static struct operand *
  673. x_opr_decode (struct mem_read_abstraction_base *mra, int offset)
  674. {
  675. return x_opr_decode_with_size (mra, offset, -1);
  676. }
  677. static int
  678. z_opr_decode (struct mem_read_abstraction_base *mra,
  679. int *n_operands, struct operand **operand)
  680. {
  681. struct operand *op = x_opr_decode (mra, 0);
  682. if (op == NULL)
  683. return -1;
  684. operand[(*n_operands)++] = op;
  685. return 0;
  686. }
  687. static int
  688. z_opr_decode2 (struct mem_read_abstraction_base *mra,
  689. int *n_operands, struct operand **operand)
  690. {
  691. int n = x_opr_n_bytes (mra, 0);
  692. if (n < 0)
  693. return n;
  694. struct operand *op = x_opr_decode (mra, 0);
  695. if (op == NULL)
  696. return -1;
  697. operand[(*n_operands)++] = op;
  698. op = x_opr_decode (mra, n);
  699. if (op == NULL)
  700. return -1;
  701. operand[(*n_operands)++] = op;
  702. return 0;
  703. }
  704. static int
  705. imm1234 (struct mem_read_abstraction_base *mra, int base,
  706. int *n_operands, struct operand **operand)
  707. {
  708. struct operand *op;
  709. bfd_byte opcode;
  710. int status = mra->read (mra, -1, 1, &opcode);
  711. if (status < 0)
  712. return status;
  713. opcode -= base;
  714. int size = registers[opcode & 0xF].bytes;
  715. uint32_t imm;
  716. if (decode_signed_value (mra, size, &imm) < 0)
  717. return -1;
  718. op = create_immediate_operand (imm);
  719. if (op == NULL)
  720. return -1;
  721. operand[(*n_operands)++] = op;
  722. return 0;
  723. }
  724. /* Special case of LD and CMP with register S and IMM operand */
  725. static int
  726. reg_s_imm (struct mem_read_abstraction_base *mra, int *n_operands,
  727. struct operand **operand)
  728. {
  729. struct operand *op;
  730. op = create_register_operand (REG_S);
  731. if (op == NULL)
  732. return -1;
  733. operand[(*n_operands)++] = op;
  734. uint32_t imm;
  735. if (decode_signed_value (mra, 3, &imm) < 0)
  736. return -1;
  737. op = create_immediate_operand (imm);
  738. if (op == NULL)
  739. return -1;
  740. operand[(*n_operands)++] = op;
  741. return 0;
  742. }
  743. /* Special case of LD, CMP and ST with register S and OPR operand */
  744. static int
  745. reg_s_opr (struct mem_read_abstraction_base *mra, int *n_operands,
  746. struct operand **operand)
  747. {
  748. struct operand *op;
  749. op = create_register_operand (REG_S);
  750. if (op == NULL)
  751. return -1;
  752. operand[(*n_operands)++] = op;
  753. op = x_opr_decode (mra, 0);
  754. if (op == NULL)
  755. return -1;
  756. operand[(*n_operands)++] = op;
  757. return 0;
  758. }
  759. static int
  760. z_imm1234_8base (struct mem_read_abstraction_base *mra, int *n_operands,
  761. struct operand **operand)
  762. {
  763. return imm1234 (mra, 8, n_operands, operand);
  764. }
  765. static int
  766. z_imm1234_0base (struct mem_read_abstraction_base *mra, int *n_operands,
  767. struct operand **operand)
  768. {
  769. return imm1234 (mra, 0, n_operands, operand);
  770. }
  771. static int
  772. z_tfr (struct mem_read_abstraction_base *mra, int *n_operands,
  773. struct operand **operand)
  774. {
  775. struct operand *op;
  776. bfd_byte byte;
  777. int status = mra->read (mra, 0, 1, &byte);
  778. if (status < 0)
  779. return status;
  780. op = create_register_operand (byte >> 4);
  781. if (op == NULL)
  782. return -1;
  783. operand[(*n_operands)++] = op;
  784. op = create_register_operand (byte & 0x0F);
  785. if (op == NULL)
  786. return -1;
  787. operand[(*n_operands)++] = op;
  788. return 0;
  789. }
  790. static int
  791. z_reg (struct mem_read_abstraction_base *mra, int *n_operands,
  792. struct operand **operand)
  793. {
  794. struct operand *op;
  795. bfd_byte byte;
  796. int status = mra->read (mra, -1, 1, &byte);
  797. if (status < 0)
  798. return status;
  799. op = create_register_operand (byte & 0x07);
  800. if (op == NULL)
  801. return -1;
  802. operand[(*n_operands)++] = op;
  803. return 0;
  804. }
  805. static int
  806. reg_xy (struct mem_read_abstraction_base *mra,
  807. int *n_operands, struct operand **operand)
  808. {
  809. struct operand *op;
  810. bfd_byte byte;
  811. int status = mra->read (mra, -1, 1, &byte);
  812. if (status < 0)
  813. return status;
  814. op = create_register_operand ((byte & 0x01) ? REG_Y : REG_X);
  815. if (op == NULL)
  816. return -1;
  817. operand[(*n_operands)++] = op;
  818. return 0;
  819. }
  820. static int
  821. lea_reg_xys_opr (struct mem_read_abstraction_base *mra,
  822. int *n_operands, struct operand **operand)
  823. {
  824. struct operand *op;
  825. bfd_byte byte;
  826. int status = mra->read (mra, -1, 1, &byte);
  827. if (status < 0)
  828. return status;
  829. int reg_xys = -1;
  830. switch (byte & 0x03)
  831. {
  832. case 0x00:
  833. reg_xys = REG_X;
  834. break;
  835. case 0x01:
  836. reg_xys = REG_Y;
  837. break;
  838. case 0x02:
  839. reg_xys = REG_S;
  840. break;
  841. }
  842. op = create_register_operand (reg_xys);
  843. if (op == NULL)
  844. return -1;
  845. operand[(*n_operands)++] = op;
  846. op = x_opr_decode (mra, 0);
  847. if (op == NULL)
  848. return -1;
  849. operand[(*n_operands)++] = op;
  850. return 0;
  851. }
  852. static int
  853. lea_reg_xys (struct mem_read_abstraction_base *mra,
  854. int *n_operands, struct operand **operand)
  855. {
  856. struct operand *op;
  857. bfd_byte byte;
  858. int status = mra->read (mra, -1, 1, &byte);
  859. if (status < 0)
  860. return status;
  861. int reg_n = -1;
  862. switch (byte & 0x03)
  863. {
  864. case 0x00:
  865. reg_n = REG_X;
  866. break;
  867. case 0x01:
  868. reg_n = REG_Y;
  869. break;
  870. case 0x02:
  871. reg_n = REG_S;
  872. break;
  873. }
  874. status = mra->read (mra, 0, 1, &byte);
  875. if (status < 0)
  876. return status;
  877. op = create_register_operand (reg_n);
  878. if (op == NULL)
  879. return -1;
  880. operand[(*n_operands)++] = op;
  881. op = create_memory_operand (false, (int8_t) byte, 1, reg_n, -1);
  882. if (op == NULL)
  883. return -1;
  884. operand[(*n_operands)++] = op;
  885. return 0;
  886. }
  887. /* PC Relative offsets of size 15 or 7 bits */
  888. static int
  889. rel_15_7 (struct mem_read_abstraction_base *mra, int offset,
  890. int *n_operands, struct operand **operands)
  891. {
  892. struct operand *op;
  893. bfd_byte upper;
  894. int status = mra->read (mra, offset - 1, 1, &upper);
  895. if (status < 0)
  896. return status;
  897. bool rel_size = (upper & 0x80);
  898. int16_t addr = upper;
  899. if (rel_size)
  900. {
  901. /* 15 bits. Get the next byte */
  902. bfd_byte lower;
  903. status = mra->read (mra, offset, 1, &lower);
  904. if (status < 0)
  905. return status;
  906. addr <<= 8;
  907. addr |= lower;
  908. addr &= 0x7FFF;
  909. bool negative = (addr & 0x4000);
  910. addr &= 0x3FFF;
  911. if (negative)
  912. addr = addr - 0x4000;
  913. }
  914. else
  915. {
  916. /* 7 bits. */
  917. bool negative = (addr & 0x40);
  918. addr &= 0x3F;
  919. if (negative)
  920. addr = addr - 0x40;
  921. }
  922. op = create_simple_memory_operand (addr, mra->posn (mra) - 1, true);
  923. if (op == NULL)
  924. return -1;
  925. operands[(*n_operands)++] = op;
  926. return 0;
  927. }
  928. /* PC Relative offsets of size 15 or 7 bits */
  929. static int
  930. decode_rel_15_7 (struct mem_read_abstraction_base *mra,
  931. int *n_operands, struct operand **operand)
  932. {
  933. return rel_15_7 (mra, 1, n_operands, operand);
  934. }
  935. static int shift_n_bytes (struct mem_read_abstraction_base *);
  936. static int mov_imm_opr_n_bytes (struct mem_read_abstraction_base *);
  937. static int loop_prim_n_bytes (struct mem_read_abstraction_base *);
  938. static int bm_rel_n_bytes (struct mem_read_abstraction_base *);
  939. static int mul_n_bytes (struct mem_read_abstraction_base *);
  940. static int bm_n_bytes (struct mem_read_abstraction_base *);
  941. static int psh_pul_decode (struct mem_read_abstraction_base *mra, int *n_operands, struct operand **operand);
  942. static int shift_decode (struct mem_read_abstraction_base *mra, int *n_operands, struct operand **operand);
  943. static int mul_decode (struct mem_read_abstraction_base *mra, int *n_operands, struct operand **operand);
  944. static int bm_decode (struct mem_read_abstraction_base *mra, int *n_operands, struct operand **operand);
  945. static int bm_rel_decode (struct mem_read_abstraction_base *mra, int *n_operands, struct operand **operand);
  946. static int mov_imm_opr (struct mem_read_abstraction_base *mra, int *n_operands, struct operand **operand);
  947. static int loop_primitive_decode (struct mem_read_abstraction_base *mra, int *n_operands, struct operand **operands);
  948. static int bit_field_decode (struct mem_read_abstraction_base *mra, int *n_operands, struct operand **operands);
  949. static int exg_sex_decode (struct mem_read_abstraction_base *mra, int *n_operands, struct operand **operands);
  950. static enum optr shift_discrim (struct mem_read_abstraction_base *mra, enum optr hint);
  951. static enum optr psh_pul_discrim (struct mem_read_abstraction_base *mra, enum optr hint);
  952. static enum optr mul_discrim (struct mem_read_abstraction_base *mra, enum optr hint);
  953. static enum optr loop_primitive_discrim (struct mem_read_abstraction_base *mra, enum optr hint);
  954. static enum optr bit_field_discrim (struct mem_read_abstraction_base *mra, enum optr hint);
  955. static enum optr exg_sex_discrim (struct mem_read_abstraction_base *mra, enum optr hint);
  956. static int
  957. cmp_xy (struct mem_read_abstraction_base *mra ATTRIBUTE_UNUSED,
  958. int *n_operands, struct operand **operand)
  959. {
  960. struct operand *op;
  961. op = create_register_operand (REG_X);
  962. if (op == NULL)
  963. return -1;
  964. operand[(*n_operands)++] = op;
  965. op = create_register_operand (REG_Y);
  966. if (op == NULL)
  967. return -1;
  968. operand[(*n_operands)++] = op;
  969. return 0;
  970. }
  971. static int
  972. sub_d6_x_y (struct mem_read_abstraction_base *mra ATTRIBUTE_UNUSED,
  973. int *n_operands, struct operand **operand)
  974. {
  975. struct operand *op;
  976. op = create_register_operand (REG_D6);
  977. if (op == NULL)
  978. return -1;
  979. operand[(*n_operands)++] = op;
  980. op = create_register_operand (REG_X);
  981. if (op == NULL)
  982. return -1;
  983. operand[(*n_operands)++] = op;
  984. op = create_register_operand (REG_Y);
  985. if (op == NULL)
  986. return -1;
  987. operand[(*n_operands)++] = op;
  988. return 0;
  989. }
  990. static int
  991. sub_d6_y_x (struct mem_read_abstraction_base *mra ATTRIBUTE_UNUSED,
  992. int *n_operands, struct operand **operand)
  993. {
  994. struct operand *op;
  995. op = create_register_operand (REG_D6);
  996. if (op == NULL)
  997. return -1;
  998. operand[(*n_operands)++] = op;
  999. op = create_register_operand (REG_Y);
  1000. if (op == NULL)
  1001. return -1;
  1002. operand[(*n_operands)++] = op;
  1003. op = create_register_operand (REG_X);
  1004. if (op == NULL)
  1005. return -1;
  1006. operand[(*n_operands)++] = op;
  1007. return 0;
  1008. }
  1009. static int
  1010. ld_18bit_decode (struct mem_read_abstraction_base *mra, int *n_operands,
  1011. struct operand **operand);
  1012. static enum optr
  1013. mul_discrim (struct mem_read_abstraction_base *mra, enum optr hint)
  1014. {
  1015. uint8_t mb;
  1016. int status = mra->read (mra, 0, 1, &mb);
  1017. if (status < 0)
  1018. return OP_INVALID;
  1019. bool signed_op = (mb & 0x80);
  1020. switch (hint)
  1021. {
  1022. case OPBASE_mul:
  1023. return signed_op ? OP_muls : OP_mulu;
  1024. break;
  1025. case OPBASE_div:
  1026. return signed_op ? OP_divs : OP_divu;
  1027. break;
  1028. case OPBASE_mod:
  1029. return signed_op ? OP_mods : OP_modu;
  1030. break;
  1031. case OPBASE_mac:
  1032. return signed_op ? OP_macs : OP_macu;
  1033. break;
  1034. case OPBASE_qmul:
  1035. return signed_op ? OP_qmuls : OP_qmulu;
  1036. break;
  1037. default:
  1038. abort ();
  1039. }
  1040. return OP_INVALID;
  1041. }
  1042. struct opcode
  1043. {
  1044. /* The operation that this opcode performs. */
  1045. enum optr operator;
  1046. /* The size of this operation. May be -1 if it is implied
  1047. in the operands or if size is not applicable. */
  1048. short osize;
  1049. /* Some operations need this function to work out which operation
  1050. is intended. */
  1051. discriminator_f discriminator;
  1052. /* A function returning the number of bytes in this instruction. */
  1053. insn_bytes_f insn_bytes;
  1054. operands_f operands;
  1055. operands_f operands2;
  1056. };
  1057. static const struct opcode page2[] =
  1058. {
  1059. [0x00] = {OP_ld, -1, 0, opr_n_bytes_p1, reg_s_opr, 0},
  1060. [0x01] = {OP_st, -1, 0, opr_n_bytes_p1, reg_s_opr, 0},
  1061. [0x02] = {OP_cmp, -1, 0, opr_n_bytes_p1, reg_s_opr, 0},
  1062. [0x03] = {OP_ld, -1, 0, four, reg_s_imm, 0},
  1063. [0x04] = {OP_cmp, -1, 0, four, reg_s_imm, 0},
  1064. [0x05] = {OP_stop, -1, 0, single, 0, 0},
  1065. [0x06] = {OP_wai, -1, 0, single, 0, 0},
  1066. [0x07] = {OP_sys, -1, 0, single, 0, 0},
  1067. [0x08] = {0xFFFF, -1, bit_field_discrim, bfextins_n_bytes, bit_field_decode, 0}, /* BFEXT / BFINS */
  1068. [0x09] = {0xFFFF, -1, bit_field_discrim, bfextins_n_bytes, bit_field_decode, 0},
  1069. [0x0a] = {0xFFFF, -1, bit_field_discrim, bfextins_n_bytes, bit_field_decode, 0},
  1070. [0x0b] = {0xFFFF, -1, bit_field_discrim, bfextins_n_bytes, bit_field_decode, 0},
  1071. [0x0c] = {0xFFFF, -1, bit_field_discrim, bfextins_n_bytes, bit_field_decode, 0},
  1072. [0x0d] = {0xFFFF, -1, bit_field_discrim, bfextins_n_bytes, bit_field_decode, 0},
  1073. [0x0e] = {0xFFFF, -1, bit_field_discrim, bfextins_n_bytes, bit_field_decode, 0},
  1074. [0x0f] = {0xFFFF, -1, bit_field_discrim, bfextins_n_bytes, bit_field_decode, 0},
  1075. [0x10] = {OP_minu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1076. [0x11] = {OP_minu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1077. [0x12] = {OP_minu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1078. [0x13] = {OP_minu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1079. [0x14] = {OP_minu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1080. [0x15] = {OP_minu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1081. [0x16] = {OP_minu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1082. [0x17] = {OP_minu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1083. [0x18] = {OP_maxu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1084. [0x19] = {OP_maxu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1085. [0x1a] = {OP_maxu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1086. [0x1b] = {OP_maxu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1087. [0x1c] = {OP_maxu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1088. [0x1d] = {OP_maxu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1089. [0x1e] = {OP_maxu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1090. [0x1f] = {OP_maxu, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1091. [0x20] = {OP_mins, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1092. [0x21] = {OP_mins, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1093. [0x22] = {OP_mins, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1094. [0x23] = {OP_mins, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1095. [0x24] = {OP_mins, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1096. [0x25] = {OP_mins, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1097. [0x26] = {OP_mins, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1098. [0x27] = {OP_mins, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1099. [0x28] = {OP_maxs, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1100. [0x29] = {OP_maxs, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1101. [0x2a] = {OP_maxs, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1102. [0x2b] = {OP_maxs, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1103. [0x2c] = {OP_maxs, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1104. [0x2d] = {OP_maxs, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1105. [0x2e] = {OP_maxs, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1106. [0x2f] = {OP_maxs, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1107. [0x30] = {OPBASE_div, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1108. [0x31] = {OPBASE_div, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1109. [0x32] = {OPBASE_div, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1110. [0x33] = {OPBASE_div, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1111. [0x34] = {OPBASE_div, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1112. [0x35] = {OPBASE_div, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1113. [0x36] = {OPBASE_div, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1114. [0x37] = {OPBASE_div, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1115. [0x38] = {OPBASE_mod, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1116. [0x39] = {OPBASE_mod, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1117. [0x3a] = {OPBASE_mod, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1118. [0x3b] = {OPBASE_mod, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1119. [0x3c] = {OPBASE_mod, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1120. [0x3d] = {OPBASE_mod, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1121. [0x3e] = {OPBASE_mod, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1122. [0x3f] = {OPBASE_mod, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1123. [0x40] = {OP_abs, -1, 0, single, z_reg, 0},
  1124. [0x41] = {OP_abs, -1, 0, single, z_reg, 0},
  1125. [0x42] = {OP_abs, -1, 0, single, z_reg, 0},
  1126. [0x43] = {OP_abs, -1, 0, single, z_reg, 0},
  1127. [0x44] = {OP_abs, -1, 0, single, z_reg, 0},
  1128. [0x45] = {OP_abs, -1, 0, single, z_reg, 0},
  1129. [0x46] = {OP_abs, -1, 0, single, z_reg, 0},
  1130. [0x47] = {OP_abs, -1, 0, single, z_reg, 0},
  1131. [0x48] = {OPBASE_mac, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1132. [0x49] = {OPBASE_mac, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1133. [0x4a] = {OPBASE_mac, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1134. [0x4b] = {OPBASE_mac, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1135. [0x4c] = {OPBASE_mac, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1136. [0x4d] = {OPBASE_mac, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1137. [0x4e] = {OPBASE_mac, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1138. [0x4f] = {OPBASE_mac, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1139. [0x50] = {OP_adc, -1, 0, three, z_reg, z_imm1234_0base},
  1140. [0x51] = {OP_adc, -1, 0, three, z_reg, z_imm1234_0base},
  1141. [0x52] = {OP_adc, -1, 0, three, z_reg, z_imm1234_0base},
  1142. [0x53] = {OP_adc, -1, 0, three, z_reg, z_imm1234_0base},
  1143. [0x54] = {OP_adc, -1, 0, two, z_reg, z_imm1234_0base},
  1144. [0x55] = {OP_adc, -1, 0, two, z_reg, z_imm1234_0base},
  1145. [0x56] = {OP_adc, -1, 0, five, z_reg, z_imm1234_0base},
  1146. [0x57] = {OP_adc, -1, 0, five, z_reg, z_imm1234_0base},
  1147. [0x58] = {OP_bit, -1, 0, three, z_reg, z_imm1234_8base},
  1148. [0x59] = {OP_bit, -1, 0, three, z_reg, z_imm1234_8base},
  1149. [0x5a] = {OP_bit, -1, 0, three, z_reg, z_imm1234_8base},
  1150. [0x5b] = {OP_bit, -1, 0, three, z_reg, z_imm1234_8base},
  1151. [0x5c] = {OP_bit, -1, 0, two, z_reg, z_imm1234_8base},
  1152. [0x5d] = {OP_bit, -1, 0, two, z_reg, z_imm1234_8base},
  1153. [0x5e] = {OP_bit, -1, 0, five, z_reg, z_imm1234_8base},
  1154. [0x5f] = {OP_bit, -1, 0, five, z_reg, z_imm1234_8base},
  1155. [0x60] = {OP_adc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1156. [0x61] = {OP_adc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1157. [0x62] = {OP_adc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1158. [0x63] = {OP_adc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1159. [0x64] = {OP_adc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1160. [0x65] = {OP_adc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1161. [0x66] = {OP_adc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1162. [0x67] = {OP_adc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1163. [0x68] = {OP_bit, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1164. [0x69] = {OP_bit, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1165. [0x6a] = {OP_bit, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1166. [0x6b] = {OP_bit, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1167. [0x6c] = {OP_bit, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1168. [0x6d] = {OP_bit, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1169. [0x6e] = {OP_bit, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1170. [0x6f] = {OP_bit, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1171. [0x70] = {OP_sbc, -1, 0, three, z_reg, z_imm1234_0base},
  1172. [0x71] = {OP_sbc, -1, 0, three, z_reg, z_imm1234_0base},
  1173. [0x72] = {OP_sbc, -1, 0, three, z_reg, z_imm1234_0base},
  1174. [0x73] = {OP_sbc, -1, 0, three, z_reg, z_imm1234_0base},
  1175. [0x74] = {OP_sbc, -1, 0, two, z_reg, z_imm1234_0base},
  1176. [0x75] = {OP_sbc, -1, 0, two, z_reg, z_imm1234_0base},
  1177. [0x76] = {OP_sbc, -1, 0, five, z_reg, z_imm1234_0base},
  1178. [0x77] = {OP_sbc, -1, 0, five, z_reg, z_imm1234_0base},
  1179. [0x78] = {OP_eor, -1, 0, three, z_reg, z_imm1234_8base},
  1180. [0x79] = {OP_eor, -1, 0, three, z_reg, z_imm1234_8base},
  1181. [0x7a] = {OP_eor, -1, 0, three, z_reg, z_imm1234_8base},
  1182. [0x7b] = {OP_eor, -1, 0, three, z_reg, z_imm1234_8base},
  1183. [0x7c] = {OP_eor, -1, 0, two, z_reg, z_imm1234_8base},
  1184. [0x7d] = {OP_eor, -1, 0, two, z_reg, z_imm1234_8base},
  1185. [0x7e] = {OP_eor, -1, 0, five, z_reg, z_imm1234_8base},
  1186. [0x7f] = {OP_eor, -1, 0, five, z_reg, z_imm1234_8base},
  1187. [0x80] = {OP_sbc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1188. [0x81] = {OP_sbc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1189. [0x82] = {OP_sbc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1190. [0x83] = {OP_sbc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1191. [0x84] = {OP_sbc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1192. [0x85] = {OP_sbc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1193. [0x86] = {OP_sbc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1194. [0x87] = {OP_sbc, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1195. [0x88] = {OP_eor, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1196. [0x89] = {OP_eor, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1197. [0x8a] = {OP_eor, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1198. [0x8b] = {OP_eor, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1199. [0x8c] = {OP_eor, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1200. [0x8d] = {OP_eor, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1201. [0x8e] = {OP_eor, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1202. [0x8f] = {OP_eor, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1203. [0x90] = {OP_rti, -1, 0, single, 0, 0},
  1204. [0x91] = {OP_clb, -1, 0, two, z_tfr, 0},
  1205. [0x92] = {OP_trap, -1, 0, single, trap_decode, 0},
  1206. [0x93] = {OP_trap, -1, 0, single, trap_decode, 0},
  1207. [0x94] = {OP_trap, -1, 0, single, trap_decode, 0},
  1208. [0x95] = {OP_trap, -1, 0, single, trap_decode, 0},
  1209. [0x96] = {OP_trap, -1, 0, single, trap_decode, 0},
  1210. [0x97] = {OP_trap, -1, 0, single, trap_decode, 0},
  1211. [0x98] = {OP_trap, -1, 0, single, trap_decode, 0},
  1212. [0x99] = {OP_trap, -1, 0, single, trap_decode, 0},
  1213. [0x9a] = {OP_trap, -1, 0, single, trap_decode, 0},
  1214. [0x9b] = {OP_trap, -1, 0, single, trap_decode, 0},
  1215. [0x9c] = {OP_trap, -1, 0, single, trap_decode, 0},
  1216. [0x9d] = {OP_trap, -1, 0, single, trap_decode, 0},
  1217. [0x9e] = {OP_trap, -1, 0, single, trap_decode, 0},
  1218. [0x9f] = {OP_trap, -1, 0, single, trap_decode, 0},
  1219. [0xa0] = {OP_sat, -1, 0, single, z_reg, 0},
  1220. [0xa1] = {OP_sat, -1, 0, single, z_reg, 0},
  1221. [0xa2] = {OP_sat, -1, 0, single, z_reg, 0},
  1222. [0xa3] = {OP_sat, -1, 0, single, z_reg, 0},
  1223. [0xa4] = {OP_sat, -1, 0, single, z_reg, 0},
  1224. [0xa5] = {OP_sat, -1, 0, single, z_reg, 0},
  1225. [0xa6] = {OP_sat, -1, 0, single, z_reg, 0},
  1226. [0xa7] = {OP_sat, -1, 0, single, z_reg, 0},
  1227. [0xa8] = {OP_trap, -1, 0, single, trap_decode, 0},
  1228. [0xa9] = {OP_trap, -1, 0, single, trap_decode, 0},
  1229. [0xaa] = {OP_trap, -1, 0, single, trap_decode, 0},
  1230. [0xab] = {OP_trap, -1, 0, single, trap_decode, 0},
  1231. [0xac] = {OP_trap, -1, 0, single, trap_decode, 0},
  1232. [0xad] = {OP_trap, -1, 0, single, trap_decode, 0},
  1233. [0xae] = {OP_trap, -1, 0, single, trap_decode, 0},
  1234. [0xaf] = {OP_trap, -1, 0, single, trap_decode, 0},
  1235. [0xb0] = {OPBASE_qmul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1236. [0xb1] = {OPBASE_qmul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1237. [0xb2] = {OPBASE_qmul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1238. [0xb3] = {OPBASE_qmul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1239. [0xb4] = {OPBASE_qmul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1240. [0xb5] = {OPBASE_qmul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1241. [0xb6] = {OPBASE_qmul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1242. [0xb7] = {OPBASE_qmul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1243. [0xb8] = {OP_trap, -1, 0, single, trap_decode, 0},
  1244. [0xb9] = {OP_trap, -1, 0, single, trap_decode, 0},
  1245. [0xba] = {OP_trap, -1, 0, single, trap_decode, 0},
  1246. [0xbb] = {OP_trap, -1, 0, single, trap_decode, 0},
  1247. [0xbc] = {OP_trap, -1, 0, single, trap_decode, 0},
  1248. [0xbd] = {OP_trap, -1, 0, single, trap_decode, 0},
  1249. [0xbe] = {OP_trap, -1, 0, single, trap_decode, 0},
  1250. [0xbf] = {OP_trap, -1, 0, single, trap_decode, 0},
  1251. [0xc0] = {OP_trap, -1, 0, single, trap_decode, 0},
  1252. [0xc1] = {OP_trap, -1, 0, single, trap_decode, 0},
  1253. [0xc2] = {OP_trap, -1, 0, single, trap_decode, 0},
  1254. [0xc3] = {OP_trap, -1, 0, single, trap_decode, 0},
  1255. [0xc4] = {OP_trap, -1, 0, single, trap_decode, 0},
  1256. [0xc5] = {OP_trap, -1, 0, single, trap_decode, 0},
  1257. [0xc6] = {OP_trap, -1, 0, single, trap_decode, 0},
  1258. [0xc7] = {OP_trap, -1, 0, single, trap_decode, 0},
  1259. [0xc8] = {OP_trap, -1, 0, single, trap_decode, 0},
  1260. [0xc9] = {OP_trap, -1, 0, single, trap_decode, 0},
  1261. [0xca] = {OP_trap, -1, 0, single, trap_decode, 0},
  1262. [0xcb] = {OP_trap, -1, 0, single, trap_decode, 0},
  1263. [0xcc] = {OP_trap, -1, 0, single, trap_decode, 0},
  1264. [0xcd] = {OP_trap, -1, 0, single, trap_decode, 0},
  1265. [0xce] = {OP_trap, -1, 0, single, trap_decode, 0},
  1266. [0xcf] = {OP_trap, -1, 0, single, trap_decode, 0},
  1267. [0xd0] = {OP_trap, -1, 0, single, trap_decode, 0},
  1268. [0xd1] = {OP_trap, -1, 0, single, trap_decode, 0},
  1269. [0xd2] = {OP_trap, -1, 0, single, trap_decode, 0},
  1270. [0xd3] = {OP_trap, -1, 0, single, trap_decode, 0},
  1271. [0xd4] = {OP_trap, -1, 0, single, trap_decode, 0},
  1272. [0xd5] = {OP_trap, -1, 0, single, trap_decode, 0},
  1273. [0xd6] = {OP_trap, -1, 0, single, trap_decode, 0},
  1274. [0xd7] = {OP_trap, -1, 0, single, trap_decode, 0},
  1275. [0xd8] = {OP_trap, -1, 0, single, trap_decode, 0},
  1276. [0xd9] = {OP_trap, -1, 0, single, trap_decode, 0},
  1277. [0xda] = {OP_trap, -1, 0, single, trap_decode, 0},
  1278. [0xdb] = {OP_trap, -1, 0, single, trap_decode, 0},
  1279. [0xdc] = {OP_trap, -1, 0, single, trap_decode, 0},
  1280. [0xdd] = {OP_trap, -1, 0, single, trap_decode, 0},
  1281. [0xde] = {OP_trap, -1, 0, single, trap_decode, 0},
  1282. [0xdf] = {OP_trap, -1, 0, single, trap_decode, 0},
  1283. [0xe0] = {OP_trap, -1, 0, single, trap_decode, 0},
  1284. [0xe1] = {OP_trap, -1, 0, single, trap_decode, 0},
  1285. [0xe2] = {OP_trap, -1, 0, single, trap_decode, 0},
  1286. [0xe3] = {OP_trap, -1, 0, single, trap_decode, 0},
  1287. [0xe4] = {OP_trap, -1, 0, single, trap_decode, 0},
  1288. [0xe5] = {OP_trap, -1, 0, single, trap_decode, 0},
  1289. [0xe6] = {OP_trap, -1, 0, single, trap_decode, 0},
  1290. [0xe7] = {OP_trap, -1, 0, single, trap_decode, 0},
  1291. [0xe8] = {OP_trap, -1, 0, single, trap_decode, 0},
  1292. [0xe9] = {OP_trap, -1, 0, single, trap_decode, 0},
  1293. [0xea] = {OP_trap, -1, 0, single, trap_decode, 0},
  1294. [0xeb] = {OP_trap, -1, 0, single, trap_decode, 0},
  1295. [0xec] = {OP_trap, -1, 0, single, trap_decode, 0},
  1296. [0xed] = {OP_trap, -1, 0, single, trap_decode, 0},
  1297. [0xee] = {OP_trap, -1, 0, single, trap_decode, 0},
  1298. [0xef] = {OP_trap, -1, 0, single, trap_decode, 0},
  1299. [0xf0] = {OP_trap, -1, 0, single, trap_decode, 0},
  1300. [0xf1] = {OP_trap, -1, 0, single, trap_decode, 0},
  1301. [0xf2] = {OP_trap, -1, 0, single, trap_decode, 0},
  1302. [0xf3] = {OP_trap, -1, 0, single, trap_decode, 0},
  1303. [0xf4] = {OP_trap, -1, 0, single, trap_decode, 0},
  1304. [0xf5] = {OP_trap, -1, 0, single, trap_decode, 0},
  1305. [0xf6] = {OP_trap, -1, 0, single, trap_decode, 0},
  1306. [0xf7] = {OP_trap, -1, 0, single, trap_decode, 0},
  1307. [0xf8] = {OP_trap, -1, 0, single, trap_decode, 0},
  1308. [0xf9] = {OP_trap, -1, 0, single, trap_decode, 0},
  1309. [0xfa] = {OP_trap, -1, 0, single, trap_decode, 0},
  1310. [0xfb] = {OP_trap, -1, 0, single, trap_decode, 0},
  1311. [0xfc] = {OP_trap, -1, 0, single, trap_decode, 0},
  1312. [0xfd] = {OP_trap, -1, 0, single, trap_decode, 0},
  1313. [0xfe] = {OP_trap, -1, 0, single, trap_decode, 0},
  1314. [0xff] = {OP_trap, -1, 0, single, trap_decode, 0},
  1315. };
  1316. static const struct opcode page1[] =
  1317. {
  1318. [0x00] = {OP_bgnd, -1, 0, single, 0, 0},
  1319. [0x01] = {OP_nop, -1, 0, single, 0, 0},
  1320. [0x02] = {OP_brclr, -1, 0, bm_rel_n_bytes, bm_rel_decode, 0},
  1321. [0x03] = {OP_brset, -1, 0, bm_rel_n_bytes, bm_rel_decode, 0},
  1322. [0x04] = {0xFFFF, -1, psh_pul_discrim, two, psh_pul_decode, 0}, /* psh/pul */
  1323. [0x05] = {OP_rts, -1, 0, single, 0, 0},
  1324. [0x06] = {OP_lea, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1325. [0x07] = {OP_lea, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1326. [0x08] = {OP_lea, -1, 0, opr_n_bytes_p1, lea_reg_xys_opr, 0},
  1327. [0x09] = {OP_lea, -1, 0, opr_n_bytes_p1, lea_reg_xys_opr, 0},
  1328. [0x0a] = {OP_lea, -1, 0, opr_n_bytes_p1, lea_reg_xys_opr, 0},
  1329. [0x0b] = {0xFFFF, -1, loop_primitive_discrim, loop_prim_n_bytes, loop_primitive_decode, 0}, /* Loop primitives TBcc / DBcc */
  1330. [0x0c] = {OP_mov, 0, 0, mov_imm_opr_n_bytes, mov_imm_opr, 0},
  1331. [0x0d] = {OP_mov, 1, 0, mov_imm_opr_n_bytes, mov_imm_opr, 0},
  1332. [0x0e] = {OP_mov, 2, 0, mov_imm_opr_n_bytes, mov_imm_opr, 0},
  1333. [0x0f] = {OP_mov, 3, 0, mov_imm_opr_n_bytes, mov_imm_opr, 0},
  1334. [0x10] = {0xFFFF, -1, shift_discrim, shift_n_bytes, shift_decode, 0}, /* lsr/lsl/asl/asr/rol/ror */
  1335. [0x11] = {0xFFFF, -1, shift_discrim, shift_n_bytes, shift_decode, 0},
  1336. [0x12] = {0xFFFF, -1, shift_discrim, shift_n_bytes, shift_decode, 0},
  1337. [0x13] = {0xFFFF, -1, shift_discrim, shift_n_bytes, shift_decode, 0},
  1338. [0x14] = {0xFFFF, -1, shift_discrim, shift_n_bytes, shift_decode, 0},
  1339. [0x15] = {0xFFFF, -1, shift_discrim, shift_n_bytes, shift_decode, 0},
  1340. [0x16] = {0xFFFF, -1, shift_discrim, shift_n_bytes, shift_decode, 0},
  1341. [0x17] = {0xFFFF, -1, shift_discrim, shift_n_bytes, shift_decode, 0},
  1342. [0x18] = {OP_lea, -1, 0, two, lea_reg_xys, NULL},
  1343. [0x19] = {OP_lea, -1, 0, two, lea_reg_xys, NULL},
  1344. [0x1a] = {OP_lea, -1, 0, two, lea_reg_xys, NULL},
  1345. /* 0x1b PG2 */
  1346. [0x1c] = {OP_mov, 0, 0, opr_n_bytes2, z_opr_decode2, 0},
  1347. [0x1d] = {OP_mov, 1, 0, opr_n_bytes2, z_opr_decode2, 0},
  1348. [0x1e] = {OP_mov, 2, 0, opr_n_bytes2, z_opr_decode2, 0},
  1349. [0x1f] = {OP_mov, 3, 0, opr_n_bytes2, z_opr_decode2, 0},
  1350. [0x20] = {OP_bra, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1351. [0x21] = {OP_bsr, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1352. [0x22] = {OP_bhi, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1353. [0x23] = {OP_bls, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1354. [0x24] = {OP_bcc, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1355. [0x25] = {OP_bcs, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1356. [0x26] = {OP_bne, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1357. [0x27] = {OP_beq, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1358. [0x28] = {OP_bvc, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1359. [0x29] = {OP_bvs, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1360. [0x2a] = {OP_bpl, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1361. [0x2b] = {OP_bmi, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1362. [0x2c] = {OP_bge, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1363. [0x2d] = {OP_blt, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1364. [0x2e] = {OP_bgt, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1365. [0x2f] = {OP_ble, -1, 0, pcrel_15bit, decode_rel_15_7, 0},
  1366. [0x30] = {OP_inc, -1, 0, single, z_reg, 0},
  1367. [0x31] = {OP_inc, -1, 0, single, z_reg, 0},
  1368. [0x32] = {OP_inc, -1, 0, single, z_reg, 0},
  1369. [0x33] = {OP_inc, -1, 0, single, z_reg, 0},
  1370. [0x34] = {OP_inc, -1, 0, single, z_reg, 0},
  1371. [0x35] = {OP_inc, -1, 0, single, z_reg, 0},
  1372. [0x36] = {OP_inc, -1, 0, single, z_reg, 0},
  1373. [0x37] = {OP_inc, -1, 0, single, z_reg, 0},
  1374. [0x38] = {OP_clr, -1, 0, single, z_reg, 0},
  1375. [0x39] = {OP_clr, -1, 0, single, z_reg, 0},
  1376. [0x3a] = {OP_clr, -1, 0, single, z_reg, 0},
  1377. [0x3b] = {OP_clr, -1, 0, single, z_reg, 0},
  1378. [0x3c] = {OP_clr, -1, 0, single, z_reg, 0},
  1379. [0x3d] = {OP_clr, -1, 0, single, z_reg, 0},
  1380. [0x3e] = {OP_clr, -1, 0, single, z_reg, 0},
  1381. [0x3f] = {OP_clr, -1, 0, single, z_reg, 0},
  1382. [0x40] = {OP_dec, -1, 0, single, z_reg, 0},
  1383. [0x41] = {OP_dec, -1, 0, single, z_reg, 0},
  1384. [0x42] = {OP_dec, -1, 0, single, z_reg, 0},
  1385. [0x43] = {OP_dec, -1, 0, single, z_reg, 0},
  1386. [0x44] = {OP_dec, -1, 0, single, z_reg, 0},
  1387. [0x45] = {OP_dec, -1, 0, single, z_reg, 0},
  1388. [0x46] = {OP_dec, -1, 0, single, z_reg, 0},
  1389. [0x47] = {OP_dec, -1, 0, single, z_reg, 0},
  1390. [0x48] = {OPBASE_mul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1391. [0x49] = {OPBASE_mul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1392. [0x4a] = {OPBASE_mul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1393. [0x4b] = {OPBASE_mul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1394. [0x4c] = {OPBASE_mul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1395. [0x4d] = {OPBASE_mul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1396. [0x4e] = {OPBASE_mul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1397. [0x4f] = {OPBASE_mul, -1, mul_discrim, mul_n_bytes, mul_decode, 0},
  1398. [0x50] = {OP_add, -1, 0, three, z_reg, z_imm1234_0base},
  1399. [0x51] = {OP_add, -1, 0, three, z_reg, z_imm1234_0base},
  1400. [0x52] = {OP_add, -1, 0, three, z_reg, z_imm1234_0base},
  1401. [0x53] = {OP_add, -1, 0, three, z_reg, z_imm1234_0base},
  1402. [0x54] = {OP_add, -1, 0, two, z_reg, z_imm1234_0base},
  1403. [0x55] = {OP_add, -1, 0, two, z_reg, z_imm1234_0base},
  1404. [0x56] = {OP_add, -1, 0, five, z_reg, z_imm1234_0base},
  1405. [0x57] = {OP_add, -1, 0, five, z_reg, z_imm1234_0base},
  1406. [0x58] = {OP_and, -1, 0, three, z_reg, z_imm1234_8base},
  1407. [0x59] = {OP_and, -1, 0, three, z_reg, z_imm1234_8base},
  1408. [0x5a] = {OP_and, -1, 0, three, z_reg, z_imm1234_8base},
  1409. [0x5b] = {OP_and, -1, 0, three, z_reg, z_imm1234_8base},
  1410. [0x5c] = {OP_and, -1, 0, two, z_reg, z_imm1234_8base},
  1411. [0x5d] = {OP_and, -1, 0, two, z_reg, z_imm1234_8base},
  1412. [0x5e] = {OP_and, -1, 0, five, z_reg, z_imm1234_8base},
  1413. [0x5f] = {OP_and, -1, 0, five, z_reg, z_imm1234_8base},
  1414. [0x60] = {OP_add, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1415. [0x61] = {OP_add, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1416. [0x62] = {OP_add, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1417. [0x63] = {OP_add, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1418. [0x64] = {OP_add, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1419. [0x65] = {OP_add, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1420. [0x66] = {OP_add, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1421. [0x67] = {OP_add, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1422. [0x68] = {OP_and, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1423. [0x69] = {OP_and, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1424. [0x6a] = {OP_and, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1425. [0x6b] = {OP_and, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1426. [0x6c] = {OP_and, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1427. [0x6d] = {OP_and, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1428. [0x6e] = {OP_and, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1429. [0x6f] = {OP_and, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1430. [0x70] = {OP_sub, -1, 0, three, z_reg, z_imm1234_0base},
  1431. [0x71] = {OP_sub, -1, 0, three, z_reg, z_imm1234_0base},
  1432. [0x72] = {OP_sub, -1, 0, three, z_reg, z_imm1234_0base},
  1433. [0x73] = {OP_sub, -1, 0, three, z_reg, z_imm1234_0base},
  1434. [0x74] = {OP_sub, -1, 0, two, z_reg, z_imm1234_0base},
  1435. [0x75] = {OP_sub, -1, 0, two, z_reg, z_imm1234_0base},
  1436. [0x76] = {OP_sub, -1, 0, five, z_reg, z_imm1234_0base},
  1437. [0x77] = {OP_sub, -1, 0, five, z_reg, z_imm1234_0base},
  1438. [0x78] = {OP_or, -1, 0, three, z_reg, z_imm1234_8base},
  1439. [0x79] = {OP_or, -1, 0, three, z_reg, z_imm1234_8base},
  1440. [0x7a] = {OP_or, -1, 0, three, z_reg, z_imm1234_8base},
  1441. [0x7b] = {OP_or, -1, 0, three, z_reg, z_imm1234_8base},
  1442. [0x7c] = {OP_or, -1, 0, two, z_reg, z_imm1234_8base},
  1443. [0x7d] = {OP_or, -1, 0, two, z_reg, z_imm1234_8base},
  1444. [0x7e] = {OP_or, -1, 0, five, z_reg, z_imm1234_8base},
  1445. [0x7f] = {OP_or, -1, 0, five, z_reg, z_imm1234_8base},
  1446. [0x80] = {OP_sub, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1447. [0x81] = {OP_sub, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1448. [0x82] = {OP_sub, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1449. [0x83] = {OP_sub, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1450. [0x84] = {OP_sub, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1451. [0x85] = {OP_sub, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1452. [0x86] = {OP_sub, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1453. [0x87] = {OP_sub, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1454. [0x88] = {OP_or, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1455. [0x89] = {OP_or, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1456. [0x8a] = {OP_or, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1457. [0x8b] = {OP_or, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1458. [0x8c] = {OP_or, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1459. [0x8d] = {OP_or, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1460. [0x8e] = {OP_or, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1461. [0x8f] = {OP_or, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1462. [0x90] = {OP_ld, -1, 0, three, z_reg, z_imm1234_0base},
  1463. [0x91] = {OP_ld, -1, 0, three, z_reg, z_imm1234_0base},
  1464. [0x92] = {OP_ld, -1, 0, three, z_reg, z_imm1234_0base},
  1465. [0x93] = {OP_ld, -1, 0, three, z_reg, z_imm1234_0base},
  1466. [0x94] = {OP_ld, -1, 0, two, z_reg, z_imm1234_0base},
  1467. [0x95] = {OP_ld, -1, 0, two, z_reg, z_imm1234_0base},
  1468. [0x96] = {OP_ld, -1, 0, five, z_reg, z_imm1234_0base},
  1469. [0x97] = {OP_ld, -1, 0, five, z_reg, z_imm1234_0base},
  1470. [0x98] = {OP_ld, -1, 0, four, reg_xy, z_imm1234_0base},
  1471. [0x99] = {OP_ld, -1, 0, four, reg_xy, z_imm1234_0base},
  1472. [0x9a] = {OP_clr, -1, 0, single, reg_xy, 0},
  1473. [0x9b] = {OP_clr, -1, 0, single, reg_xy, 0},
  1474. [0x9c] = {OP_inc, 0, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1475. [0x9d] = {OP_inc, 1, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1476. [0x9e] = {OP_tfr, -1, 0, two, z_tfr, NULL},
  1477. [0x9f] = {OP_inc, 3, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1478. [0xa0] = {OP_ld, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1479. [0xa1] = {OP_ld, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1480. [0xa2] = {OP_ld, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1481. [0xa3] = {OP_ld, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1482. [0xa4] = {OP_ld, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1483. [0xa5] = {OP_ld, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1484. [0xa6] = {OP_ld, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1485. [0xa7] = {OP_ld, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1486. [0xa8] = {OP_ld, -1, 0, opr_n_bytes_p1, reg_xy, z_opr_decode},
  1487. [0xa9] = {OP_ld, -1, 0, opr_n_bytes_p1, reg_xy, z_opr_decode},
  1488. [0xaa] = {OP_jmp, -1, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1489. [0xab] = {OP_jsr, -1, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1490. [0xac] = {OP_dec, 0, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1491. [0xad] = {OP_dec, 1, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1492. [0xae] = {0xFFFF, -1, exg_sex_discrim, two, exg_sex_decode, 0}, /* EXG / SEX */
  1493. [0xaf] = {OP_dec, 3, 0, opr_n_bytes_p1, 0, z_opr_decode},
  1494. [0xb0] = {OP_ld, -1, 0, four, z_reg, z_ext24_decode},
  1495. [0xb1] = {OP_ld, -1, 0, four, z_reg, z_ext24_decode},
  1496. [0xb2] = {OP_ld, -1, 0, four, z_reg, z_ext24_decode},
  1497. [0xb3] = {OP_ld, -1, 0, four, z_reg, z_ext24_decode},
  1498. [0xb4] = {OP_ld, -1, 0, four, z_reg, z_ext24_decode},
  1499. [0xb5] = {OP_ld, -1, 0, four, z_reg, z_ext24_decode},
  1500. [0xb6] = {OP_ld, -1, 0, four, z_reg, z_ext24_decode},
  1501. [0xb7] = {OP_ld, -1, 0, four, z_reg, z_ext24_decode},
  1502. [0xb8] = {OP_ld, -1, 0, four, reg_xy, z_ext24_decode},
  1503. [0xb9] = {OP_ld, -1, 0, four, reg_xy, z_ext24_decode},
  1504. [0xba] = {OP_jmp, -1, 0, four, z_ext24_decode, 0},
  1505. [0xbb] = {OP_jsr, -1, 0, four, z_ext24_decode, 0},
  1506. [0xbc] = {OP_clr, 0, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1507. [0xbd] = {OP_clr, 1, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1508. [0xbe] = {OP_clr, 2, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1509. [0xbf] = {OP_clr, 3, 0, opr_n_bytes_p1, z_opr_decode, 0},
  1510. [0xc0] = {OP_st, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1511. [0xc1] = {OP_st, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1512. [0xc2] = {OP_st, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1513. [0xc3] = {OP_st, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1514. [0xc4] = {OP_st, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1515. [0xc5] = {OP_st, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1516. [0xc6] = {OP_st, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1517. [0xc7] = {OP_st, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1518. [0xc8] = {OP_st, -1, 0, opr_n_bytes_p1, reg_xy, z_opr_decode},
  1519. [0xc9] = {OP_st, -1, 0, opr_n_bytes_p1, reg_xy, z_opr_decode},
  1520. [0xca] = {OP_ld, -1, 0, three, reg_xy, ld_18bit_decode},
  1521. [0xcb] = {OP_ld, -1, 0, three, reg_xy, ld_18bit_decode},
  1522. [0xcc] = {OP_com, 0, 0, opr_n_bytes_p1, NULL, z_opr_decode},
  1523. [0xcd] = {OP_com, 1, 0, opr_n_bytes_p1, NULL, z_opr_decode},
  1524. [0xce] = {OP_andcc, -1, 0, two, imm1_decode, 0},
  1525. [0xcf] = {OP_com, 3, 0, opr_n_bytes_p1, NULL, z_opr_decode},
  1526. [0xd0] = {OP_st, -1, 0, four, z_reg, z_ext24_decode},
  1527. [0xd1] = {OP_st, -1, 0, four, z_reg, z_ext24_decode},
  1528. [0xd2] = {OP_st, -1, 0, four, z_reg, z_ext24_decode},
  1529. [0xd3] = {OP_st, -1, 0, four, z_reg, z_ext24_decode},
  1530. [0xd4] = {OP_st, -1, 0, four, z_reg, z_ext24_decode},
  1531. [0xd5] = {OP_st, -1, 0, four, z_reg, z_ext24_decode},
  1532. [0xd6] = {OP_st, -1, 0, four, z_reg, z_ext24_decode},
  1533. [0xd7] = {OP_st, -1, 0, four, z_reg, z_ext24_decode},
  1534. [0xd8] = {OP_st, -1, 0, four, reg_xy, z_ext24_decode},
  1535. [0xd9] = {OP_st, -1, 0, four, reg_xy, z_ext24_decode},
  1536. [0xda] = {OP_ld, -1, 0, three, reg_xy, ld_18bit_decode},
  1537. [0xdb] = {OP_ld, -1, 0, three, reg_xy, ld_18bit_decode},
  1538. [0xdc] = {OP_neg, 0, 0, opr_n_bytes_p1, NULL, z_opr_decode},
  1539. [0xdd] = {OP_neg, 1, 0, opr_n_bytes_p1, NULL, z_opr_decode},
  1540. [0xde] = {OP_orcc, -1, 0, two, imm1_decode, 0},
  1541. [0xdf] = {OP_neg, 3, 0, opr_n_bytes_p1, NULL, z_opr_decode},
  1542. [0xe0] = {OP_cmp, -1, 0, three, z_reg, z_imm1234_0base},
  1543. [0xe1] = {OP_cmp, -1, 0, three, z_reg, z_imm1234_0base},
  1544. [0xe2] = {OP_cmp, -1, 0, three, z_reg, z_imm1234_0base},
  1545. [0xe3] = {OP_cmp, -1, 0, three, z_reg, z_imm1234_0base},
  1546. [0xe4] = {OP_cmp, -1, 0, two, z_reg, z_imm1234_0base},
  1547. [0xe5] = {OP_cmp, -1, 0, two, z_reg, z_imm1234_0base},
  1548. [0xe6] = {OP_cmp, -1, 0, five, z_reg, z_imm1234_0base},
  1549. [0xe7] = {OP_cmp, -1, 0, five, z_reg, z_imm1234_0base},
  1550. [0xe8] = {OP_cmp, -1, 0, four, reg_xy, z_imm1234_0base},
  1551. [0xe9] = {OP_cmp, -1, 0, four, reg_xy, z_imm1234_0base},
  1552. [0xea] = {OP_ld, -1, 0, three, reg_xy, ld_18bit_decode},
  1553. [0xeb] = {OP_ld, -1, 0, three, reg_xy, ld_18bit_decode},
  1554. [0xec] = {OP_bclr, -1, 0, bm_n_bytes, bm_decode, 0},
  1555. [0xed] = {OP_bset, -1, 0, bm_n_bytes, bm_decode, 0},
  1556. [0xee] = {OP_btgl, -1, 0, bm_n_bytes, bm_decode, 0},
  1557. [0xef] = {OP_INVALID, -1, 0, NULL, NULL, NULL}, /* SPARE */
  1558. [0xf0] = {OP_cmp, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1559. [0xf1] = {OP_cmp, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1560. [0xf2] = {OP_cmp, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1561. [0xf3] = {OP_cmp, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1562. [0xf4] = {OP_cmp, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1563. [0xf5] = {OP_cmp, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1564. [0xf6] = {OP_cmp, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1565. [0xf7] = {OP_cmp, -1, 0, opr_n_bytes_p1, z_reg, z_opr_decode},
  1566. [0xf8] = {OP_cmp, -1, 0, opr_n_bytes_p1, reg_xy, z_opr_decode},
  1567. [0xf9] = {OP_cmp, -1, 0, opr_n_bytes_p1, reg_xy, z_opr_decode},
  1568. [0xfa] = {OP_ld, -1, 0, three, reg_xy, ld_18bit_decode},
  1569. [0xfb] = {OP_ld, -1, 0, three, reg_xy, ld_18bit_decode},
  1570. [0xfc] = {OP_cmp, -1, 0, single, cmp_xy, 0},
  1571. [0xfd] = {OP_sub, -1, 0, single, sub_d6_x_y, 0},
  1572. [0xfe] = {OP_sub, -1, 0, single, sub_d6_y_x, 0},
  1573. [0xff] = {OP_swi, -1, 0, single, 0, 0}
  1574. };
  1575. static const int oprregs1[] =
  1576. {
  1577. REG_D3, REG_D2, REG_D1, REG_D0, REG_CCL, REG_CCH
  1578. };
  1579. static const int oprregs2[] =
  1580. {
  1581. REG_Y, REG_X, REG_D7, REG_D6, REG_D5, REG_D4
  1582. };
  1583. enum MUL_MODE
  1584. {
  1585. MUL_REG_REG,
  1586. MUL_REG_OPR,
  1587. MUL_REG_IMM,
  1588. MUL_OPR_OPR
  1589. };
  1590. struct mb
  1591. {
  1592. uint8_t mask;
  1593. uint8_t value;
  1594. enum MUL_MODE mode;
  1595. };
  1596. static const struct mb mul_table[] = {
  1597. {0x40, 0x00, MUL_REG_REG},
  1598. {0x47, 0x40, MUL_REG_OPR},
  1599. {0x47, 0x41, MUL_REG_OPR},
  1600. {0x47, 0x43, MUL_REG_OPR},
  1601. {0x47, 0x44, MUL_REG_IMM},
  1602. {0x47, 0x45, MUL_REG_IMM},
  1603. {0x47, 0x47, MUL_REG_IMM},
  1604. {0x43, 0x42, MUL_OPR_OPR},
  1605. };
  1606. static int
  1607. mul_decode (struct mem_read_abstraction_base *mra,
  1608. int *n_operands, struct operand **operand)
  1609. {
  1610. uint8_t mb;
  1611. struct operand *op;
  1612. int status = mra->read (mra, 0, 1, &mb);
  1613. if (status < 0)
  1614. return status;
  1615. uint8_t byte;
  1616. status = mra->read (mra, -1, 1, &byte);
  1617. if (status < 0)
  1618. return status;
  1619. enum MUL_MODE mode = -1;
  1620. size_t i;
  1621. for (i = 0; i < sizeof (mul_table) / sizeof (mul_table[0]); ++i)
  1622. {
  1623. const struct mb *mm = mul_table + i;
  1624. if ((mb & mm->mask) == mm->value)
  1625. {
  1626. mode = mm->mode;
  1627. break;
  1628. }
  1629. }
  1630. op = create_register_operand (byte & 0x07);
  1631. if (op == NULL)
  1632. return -1;
  1633. operand[(*n_operands)++] = op;
  1634. switch (mode)
  1635. {
  1636. case MUL_REG_IMM:
  1637. {
  1638. int size = (mb & 0x3);
  1639. op = create_register_operand_with_size ((mb & 0x38) >> 3, size);
  1640. if (op == NULL)
  1641. return -1;
  1642. operand[(*n_operands)++] = op;
  1643. uint32_t imm;
  1644. if (z_decode_signed_value (mra, 1, size + 1, &imm) < 0)
  1645. return -1;
  1646. op = create_immediate_operand (imm);
  1647. if (op == NULL)
  1648. return -1;
  1649. operand[(*n_operands)++] = op;
  1650. }
  1651. break;
  1652. case MUL_REG_REG:
  1653. op = create_register_operand ((mb & 0x38) >> 3);
  1654. if (op == NULL)
  1655. return -1;
  1656. operand[(*n_operands)++] = op;
  1657. op = create_register_operand (mb & 0x07);
  1658. if (op == NULL)
  1659. return -1;
  1660. operand[(*n_operands)++] = op;
  1661. break;
  1662. case MUL_REG_OPR:
  1663. op = create_register_operand ((mb & 0x38) >> 3);
  1664. if (op == NULL)
  1665. return -1;
  1666. operand[(*n_operands)++] = op;
  1667. op = x_opr_decode_with_size (mra, 1, mb & 0x3);
  1668. if (op == NULL)
  1669. return -1;
  1670. operand[(*n_operands)++] = op;
  1671. break;
  1672. case MUL_OPR_OPR:
  1673. {
  1674. int first = x_opr_n_bytes (mra, 1);
  1675. if (first < 0)
  1676. return first;
  1677. op = x_opr_decode_with_size (mra, 1, (mb & 0x30) >> 4);
  1678. if (op == NULL)
  1679. return -1;
  1680. operand[(*n_operands)++] = op;
  1681. op = x_opr_decode_with_size (mra, first + 1, (mb & 0x0c) >> 2);
  1682. if (op == NULL)
  1683. return -1;
  1684. operand[(*n_operands)++] = op;
  1685. break;
  1686. }
  1687. }
  1688. return 0;
  1689. }
  1690. static int
  1691. mul_n_bytes (struct mem_read_abstraction_base *mra)
  1692. {
  1693. int nx = 2;
  1694. int first, second;
  1695. uint8_t mb;
  1696. int status = mra->read (mra, 0, 1, &mb);
  1697. if (status < 0)
  1698. return status;
  1699. enum MUL_MODE mode = -1;
  1700. size_t i;
  1701. for (i = 0; i < sizeof (mul_table) / sizeof (mul_table[0]); ++i)
  1702. {
  1703. const struct mb *mm = mul_table + i;
  1704. if ((mb & mm->mask) == mm->value)
  1705. {
  1706. mode = mm->mode;
  1707. break;
  1708. }
  1709. }
  1710. int size = (mb & 0x3) + 1;
  1711. switch (mode)
  1712. {
  1713. case MUL_REG_IMM:
  1714. nx += size;
  1715. break;
  1716. case MUL_REG_REG:
  1717. break;
  1718. case MUL_REG_OPR:
  1719. first = x_opr_n_bytes (mra, 1);
  1720. if (first < 0)
  1721. return first;
  1722. nx += first;
  1723. break;
  1724. case MUL_OPR_OPR:
  1725. first = x_opr_n_bytes (mra, nx - 1);
  1726. if (first < 0)
  1727. return first;
  1728. nx += first;
  1729. second = x_opr_n_bytes (mra, nx - 1);
  1730. if (second < 0)
  1731. return second;
  1732. nx += second;
  1733. break;
  1734. }
  1735. return nx;
  1736. }
  1737. /* The NXP documentation is vague about BM_RESERVED0 and BM_RESERVED1,
  1738. and contains obvious typos.
  1739. However the Freescale tools and experiments with the chip itself
  1740. seem to indicate that they behave like BM_REG_IMM and BM_OPR_REG
  1741. respectively. */
  1742. enum BM_MODE
  1743. {
  1744. BM_REG_IMM,
  1745. BM_RESERVED0,
  1746. BM_OPR_B,
  1747. BM_OPR_W,
  1748. BM_OPR_L,
  1749. BM_OPR_REG,
  1750. BM_RESERVED1
  1751. };
  1752. struct bm
  1753. {
  1754. uint8_t mask;
  1755. uint8_t value;
  1756. enum BM_MODE mode;
  1757. };
  1758. static const struct bm bm_table[] = {
  1759. { 0xC6, 0x04, BM_REG_IMM},
  1760. { 0x84, 0x00, BM_REG_IMM},
  1761. { 0x06, 0x06, BM_REG_IMM},
  1762. { 0xC6, 0x44, BM_RESERVED0},
  1763. { 0x8F, 0x80, BM_OPR_B},
  1764. { 0x8E, 0x82, BM_OPR_W},
  1765. { 0x8C, 0x88, BM_OPR_L},
  1766. { 0x83, 0x81, BM_OPR_REG},
  1767. { 0x87, 0x84, BM_RESERVED1},
  1768. };
  1769. static int
  1770. bm_decode (struct mem_read_abstraction_base *mra,
  1771. int *n_operands, struct operand **operand)
  1772. {
  1773. struct operand *op;
  1774. uint8_t bm;
  1775. int status = mra->read (mra, 0, 1, &bm);
  1776. if (status < 0)
  1777. return status;
  1778. size_t i;
  1779. enum BM_MODE mode = -1;
  1780. for (i = 0; i < sizeof (bm_table) / sizeof (bm_table[0]); ++i)
  1781. {
  1782. const struct bm *bme = bm_table + i;
  1783. if ((bm & bme->mask) == bme->value)
  1784. {
  1785. mode = bme->mode;
  1786. break;
  1787. }
  1788. }
  1789. switch (mode)
  1790. {
  1791. case BM_REG_IMM:
  1792. case BM_RESERVED0:
  1793. op = create_register_operand (bm & 0x07);
  1794. if (op == NULL)
  1795. return -1;
  1796. operand[(*n_operands)++] = op;
  1797. break;
  1798. case BM_OPR_B:
  1799. op = x_opr_decode_with_size (mra, 1, 0);
  1800. if (op == NULL)
  1801. return -1;
  1802. operand[(*n_operands)++] = op;
  1803. break;
  1804. case BM_OPR_W:
  1805. op = x_opr_decode_with_size (mra, 1, 1);
  1806. if (op == NULL)
  1807. return -1;
  1808. operand[(*n_operands)++] = op;
  1809. break;
  1810. case BM_OPR_L:
  1811. op = x_opr_decode_with_size (mra, 1, 3);
  1812. if (op == NULL)
  1813. return -1;
  1814. operand[(*n_operands)++] = op;
  1815. break;
  1816. case BM_OPR_REG:
  1817. case BM_RESERVED1:
  1818. {
  1819. uint8_t xb;
  1820. status = mra->read (mra, 1, 1, &xb);
  1821. if (status < 0)
  1822. return status;
  1823. /* Don't emit a size suffix for register operands */
  1824. if ((xb & 0xF8) != 0xB8)
  1825. op = x_opr_decode_with_size (mra, 1, (bm & 0x0c) >> 2);
  1826. else
  1827. op = x_opr_decode (mra, 1);
  1828. if (op == NULL)
  1829. return -1;
  1830. operand[(*n_operands)++] = op;
  1831. }
  1832. break;
  1833. }
  1834. uint8_t imm = 0;
  1835. switch (mode)
  1836. {
  1837. case BM_REG_IMM:
  1838. case BM_RESERVED0:
  1839. imm = (bm & 0x38) >> 3;
  1840. op = create_immediate_operand (imm);
  1841. if (op == NULL)
  1842. return -1;
  1843. operand[(*n_operands)++] = op;
  1844. break;
  1845. case BM_OPR_L:
  1846. imm |= (bm & 0x03) << 3;
  1847. /* fallthrough */
  1848. case BM_OPR_W:
  1849. imm |= (bm & 0x01) << 3;
  1850. /* fallthrough */
  1851. case BM_OPR_B:
  1852. imm |= (bm & 0x70) >> 4;
  1853. op = create_immediate_operand (imm);
  1854. if (op == NULL)
  1855. return -1;
  1856. operand[(*n_operands)++] = op;
  1857. break;
  1858. case BM_OPR_REG:
  1859. case BM_RESERVED1:
  1860. op = create_register_operand ((bm & 0x70) >> 4);
  1861. if (op == NULL)
  1862. return -1;
  1863. operand[(*n_operands)++] = op;
  1864. break;
  1865. }
  1866. return 0;
  1867. }
  1868. static int
  1869. bm_rel_decode (struct mem_read_abstraction_base *mra,
  1870. int *n_operands, struct operand **operand)
  1871. {
  1872. struct operand *op;
  1873. uint8_t bm;
  1874. int status = mra->read (mra, 0, 1, &bm);
  1875. if (status < 0)
  1876. return status;
  1877. size_t i;
  1878. enum BM_MODE mode = -1;
  1879. for (i = 0; i < sizeof (bm_table) / sizeof (bm_table[0]); ++i)
  1880. {
  1881. const struct bm *bme = bm_table + i;
  1882. if ((bm & bme->mask) == bme->value)
  1883. {
  1884. mode = bme->mode;
  1885. break;
  1886. }
  1887. }
  1888. int n = 1;
  1889. switch (mode)
  1890. {
  1891. case BM_REG_IMM:
  1892. case BM_RESERVED0:
  1893. op = create_register_operand (bm & 0x07);
  1894. if (op == NULL)
  1895. return -1;
  1896. operand[(*n_operands)++] = op;
  1897. break;
  1898. case BM_OPR_B:
  1899. op = x_opr_decode_with_size (mra, 1, 0);
  1900. if (op == NULL)
  1901. return -1;
  1902. operand[(*n_operands)++] = op;
  1903. n = x_opr_n_bytes (mra, 1);
  1904. if (n < 0)
  1905. return n;
  1906. n += 1;
  1907. break;
  1908. case BM_OPR_W:
  1909. op = x_opr_decode_with_size (mra, 1, 1);
  1910. if (op == NULL)
  1911. return -1;
  1912. operand[(*n_operands)++] = op;
  1913. n = x_opr_n_bytes (mra, 1);
  1914. if (n < 0)
  1915. return n;
  1916. n += 1;
  1917. break;
  1918. case BM_OPR_L:
  1919. op = x_opr_decode_with_size (mra, 1, 3);
  1920. if (op == NULL)
  1921. return -1;
  1922. operand[(*n_operands)++] = op;
  1923. n = x_opr_n_bytes (mra, 1);
  1924. if (n < 0)
  1925. return n;
  1926. n += 1;
  1927. break;
  1928. case BM_OPR_REG:
  1929. case BM_RESERVED1:
  1930. {
  1931. uint8_t xb;
  1932. status = mra->read (mra, +1, 1, &xb);
  1933. if (status < 0)
  1934. return status;
  1935. /* Don't emit a size suffix for register operands */
  1936. if ((xb & 0xF8) != 0xB8)
  1937. {
  1938. short os = (bm & 0x0c) >> 2;
  1939. op = x_opr_decode_with_size (mra, 1, os);
  1940. }
  1941. else
  1942. op = x_opr_decode (mra, 1);
  1943. if (op == NULL)
  1944. return -1;
  1945. operand[(*n_operands)++] = op;
  1946. }
  1947. break;
  1948. }
  1949. int x, imm = 0;
  1950. switch (mode)
  1951. {
  1952. case BM_OPR_L:
  1953. imm |= (bm & 0x02) << 3;
  1954. /* fall through */
  1955. case BM_OPR_W:
  1956. imm |= (bm & 0x01) << 3;
  1957. /* fall through */
  1958. case BM_OPR_B:
  1959. imm |= (bm & 0x70) >> 4;
  1960. op = create_immediate_operand (imm);
  1961. if (op == NULL)
  1962. return -1;
  1963. operand[(*n_operands)++] = op;
  1964. break;
  1965. case BM_RESERVED0:
  1966. imm = (bm & 0x38) >> 3;
  1967. op = create_immediate_operand (imm);
  1968. if (op == NULL)
  1969. return -1;
  1970. operand[(*n_operands)++] = op;
  1971. break;
  1972. case BM_REG_IMM:
  1973. imm = (bm & 0xF8) >> 3;
  1974. op = create_immediate_operand (imm);
  1975. if (op == NULL)
  1976. return -1;
  1977. operand[(*n_operands)++] = op;
  1978. break;
  1979. case BM_OPR_REG:
  1980. case BM_RESERVED1:
  1981. op = create_register_operand ((bm & 0x70) >> 4);
  1982. if (op == NULL)
  1983. return -1;
  1984. operand[(*n_operands)++] = op;
  1985. x = x_opr_n_bytes (mra, 1);
  1986. if (x < 0)
  1987. return x;
  1988. n += x;
  1989. break;
  1990. }
  1991. return rel_15_7 (mra, n + 1, n_operands, operand);
  1992. }
  1993. static int
  1994. bm_n_bytes (struct mem_read_abstraction_base *mra)
  1995. {
  1996. uint8_t bm;
  1997. int status = mra->read (mra, 0, 1, &bm);
  1998. if (status < 0)
  1999. return status;
  2000. size_t i;
  2001. enum BM_MODE mode = -1;
  2002. for (i = 0; i < sizeof (bm_table) / sizeof (bm_table[0]); ++i)
  2003. {
  2004. const struct bm *bme = bm_table + i;
  2005. if ((bm & bme->mask) == bme->value)
  2006. {
  2007. mode = bme->mode;
  2008. break;
  2009. }
  2010. }
  2011. int n = 0;
  2012. switch (mode)
  2013. {
  2014. case BM_REG_IMM:
  2015. case BM_RESERVED0:
  2016. break;
  2017. case BM_OPR_B:
  2018. case BM_OPR_W:
  2019. case BM_OPR_L:
  2020. case BM_OPR_REG:
  2021. case BM_RESERVED1:
  2022. n = x_opr_n_bytes (mra, 1);
  2023. if (n < 0)
  2024. return n;
  2025. break;
  2026. }
  2027. return n + 2;
  2028. }
  2029. static int
  2030. bm_rel_n_bytes (struct mem_read_abstraction_base *mra)
  2031. {
  2032. int n = 1 + bm_n_bytes (mra);
  2033. bfd_byte rb;
  2034. int status = mra->read (mra, n - 2, 1, &rb);
  2035. if (status != 0)
  2036. return status;
  2037. if (rb & 0x80)
  2038. n++;
  2039. return n;
  2040. }
  2041. /* shift direction */
  2042. enum SB_DIR
  2043. {
  2044. SB_LEFT,
  2045. SB_RIGHT
  2046. };
  2047. enum SB_TYPE
  2048. {
  2049. SB_ARITHMETIC,
  2050. SB_LOGICAL
  2051. };
  2052. enum SB_MODE
  2053. {
  2054. SB_REG_REG_N_EFF,
  2055. SB_REG_REG_N,
  2056. SB_REG_OPR_EFF,
  2057. SB_ROT,
  2058. SB_REG_OPR_OPR,
  2059. SB_OPR_N
  2060. };
  2061. struct sb
  2062. {
  2063. uint8_t mask;
  2064. uint8_t value;
  2065. enum SB_MODE mode;
  2066. };
  2067. static const struct sb sb_table[] = {
  2068. {0x30, 0x00, SB_REG_REG_N_EFF},
  2069. {0x30, 0x10, SB_REG_REG_N},
  2070. {0x34, 0x20, SB_REG_OPR_EFF},
  2071. {0x34, 0x24, SB_ROT},
  2072. {0x34, 0x30, SB_REG_OPR_OPR},
  2073. {0x34, 0x34, SB_OPR_N},
  2074. };
  2075. static int
  2076. shift_n_bytes (struct mem_read_abstraction_base *mra)
  2077. {
  2078. bfd_byte sb;
  2079. int opr1, opr2;
  2080. int status = mra->read (mra, 0, 1, &sb);
  2081. if (status != 0)
  2082. return status;
  2083. size_t i;
  2084. enum SB_MODE mode = -1;
  2085. for (i = 0; i < sizeof (sb_table) / sizeof (sb_table[0]); ++i)
  2086. {
  2087. const struct sb *sbe = sb_table + i;
  2088. if ((sb & sbe->mask) == sbe->value)
  2089. mode = sbe->mode;
  2090. }
  2091. switch (mode)
  2092. {
  2093. case SB_REG_REG_N_EFF:
  2094. return 2;
  2095. case SB_REG_OPR_EFF:
  2096. case SB_ROT:
  2097. opr1 = x_opr_n_bytes (mra, 1);
  2098. if (opr1 < 0)
  2099. return opr1;
  2100. return 2 + opr1;
  2101. case SB_REG_OPR_OPR:
  2102. opr1 = x_opr_n_bytes (mra, 1);
  2103. if (opr1 < 0)
  2104. return opr1;
  2105. opr2 = 0;
  2106. if ((sb & 0x30) != 0x20)
  2107. {
  2108. opr2 = x_opr_n_bytes (mra, opr1 + 1);
  2109. if (opr2 < 0)
  2110. return opr2;
  2111. }
  2112. return 2 + opr1 + opr2;
  2113. default:
  2114. return 3;
  2115. }
  2116. /* not reached */
  2117. return -1;
  2118. }
  2119. static int
  2120. mov_imm_opr_n_bytes (struct mem_read_abstraction_base *mra)
  2121. {
  2122. bfd_byte byte;
  2123. int status = mra->read (mra, -1, 1, &byte);
  2124. if (status < 0)
  2125. return status;
  2126. int size = byte - 0x0c + 1;
  2127. int n = x_opr_n_bytes (mra, size);
  2128. if (n < 0)
  2129. return n;
  2130. return size + n + 1;
  2131. }
  2132. static int
  2133. mov_imm_opr (struct mem_read_abstraction_base *mra,
  2134. int *n_operands, struct operand **operand)
  2135. {
  2136. struct operand *op;
  2137. bfd_byte byte;
  2138. int status = mra->read (mra, -1, 1, &byte);
  2139. if (status < 0)
  2140. return status;
  2141. int size = byte - 0x0c + 1;
  2142. uint32_t imm;
  2143. if (decode_signed_value (mra, size, &imm))
  2144. return -1;
  2145. op = create_immediate_operand (imm);
  2146. if (op == NULL)
  2147. return -1;
  2148. operand[(*n_operands)++] = op;
  2149. op = x_opr_decode (mra, size);
  2150. if (op == NULL)
  2151. return -1;
  2152. operand[(*n_operands)++] = op;
  2153. return 0;
  2154. }
  2155. static int
  2156. ld_18bit_decode (struct mem_read_abstraction_base *mra,
  2157. int *n_operands, struct operand **operand)
  2158. {
  2159. struct operand *op;
  2160. size_t size = 3;
  2161. bfd_byte buffer[3];
  2162. int status = mra->read (mra, 0, 2, buffer + 1);
  2163. if (status < 0)
  2164. return status;
  2165. status = mra->read (mra, -1, 1, buffer);
  2166. if (status < 0)
  2167. return status;
  2168. buffer[0] = (buffer[0] & 0x30) >> 4;
  2169. size_t i;
  2170. uint32_t imm = 0;
  2171. for (i = 0; i < size; ++i)
  2172. {
  2173. imm |= buffer[i] << (8 * (size - i - 1));
  2174. }
  2175. op = create_immediate_operand (imm);
  2176. if (op == NULL)
  2177. return -1;
  2178. operand[(*n_operands)++] = op;
  2179. return 0;
  2180. }
  2181. /* Loop Primitives */
  2182. enum LP_MODE {
  2183. LP_REG,
  2184. LP_XY,
  2185. LP_OPR
  2186. };
  2187. struct lp
  2188. {
  2189. uint8_t mask;
  2190. uint8_t value;
  2191. enum LP_MODE mode;
  2192. };
  2193. static const struct lp lp_mode[] = {
  2194. {0x08, 0x00, LP_REG},
  2195. {0x0C, 0x08, LP_XY},
  2196. {0x0C, 0x0C, LP_OPR},
  2197. };
  2198. static int
  2199. loop_prim_n_bytes (struct mem_read_abstraction_base *mra)
  2200. {
  2201. int mx = 0;
  2202. uint8_t lb;
  2203. int status = mra->read (mra, mx++, 1, &lb);
  2204. if (status < 0)
  2205. return status;
  2206. enum LP_MODE mode = -1;
  2207. size_t i;
  2208. for (i = 0; i < sizeof (lp_mode) / sizeof (lp_mode[0]); ++i)
  2209. {
  2210. const struct lp *pb = lp_mode + i;
  2211. if ((lb & pb->mask) == pb->value)
  2212. {
  2213. mode = pb->mode;
  2214. break;
  2215. }
  2216. }
  2217. if (mode == LP_OPR)
  2218. {
  2219. int n = x_opr_n_bytes (mra, mx);
  2220. if (n < 0)
  2221. return n;
  2222. mx += n;
  2223. }
  2224. uint8_t rb;
  2225. status = mra->read (mra, mx++, 1, &rb);
  2226. if (status < 0)
  2227. return status;
  2228. if (rb & 0x80)
  2229. mx++;
  2230. return mx + 1;
  2231. }
  2232. static enum optr
  2233. exg_sex_discrim (struct mem_read_abstraction_base *mra,
  2234. enum optr hint ATTRIBUTE_UNUSED)
  2235. {
  2236. uint8_t eb;
  2237. int status = mra->read (mra, 0, 1, &eb);
  2238. enum optr operator = OP_INVALID;
  2239. if (status < 0)
  2240. return operator;
  2241. struct operand *op0 = create_register_operand ((eb & 0xf0) >> 4);
  2242. if (op0 == NULL)
  2243. return -1;
  2244. struct operand *op1 = create_register_operand (eb & 0xf);
  2245. if (op1 == NULL)
  2246. return -1;
  2247. int reg0 = ((struct register_operand *) op0)->reg;
  2248. int reg1 = ((struct register_operand *) op1)->reg;
  2249. if (reg0 >= 0 && reg0 < S12Z_N_REGISTERS
  2250. && reg1 >= 0 && reg1 < S12Z_N_REGISTERS)
  2251. {
  2252. const struct reg *r0 = registers + reg0;
  2253. const struct reg *r1 = registers + reg1;
  2254. operator = r0->bytes < r1->bytes ? OP_sex : OP_exg;
  2255. }
  2256. free (op0);
  2257. free (op1);
  2258. return operator;
  2259. }
  2260. static int
  2261. exg_sex_decode (struct mem_read_abstraction_base *mra,
  2262. int *n_operands, struct operand **operands)
  2263. {
  2264. struct operand *op;
  2265. uint8_t eb;
  2266. int status = mra->read (mra, 0, 1, &eb);
  2267. if (status < 0)
  2268. return status;
  2269. /* Ship out the operands. */
  2270. op = create_register_operand ((eb & 0xf0) >> 4);
  2271. if (op == NULL)
  2272. return -1;
  2273. operands[(*n_operands)++] = op;
  2274. op = create_register_operand (eb & 0xf);
  2275. if (op == NULL)
  2276. return -1;
  2277. operands[(*n_operands)++] = op;
  2278. return 0;
  2279. }
  2280. static enum optr
  2281. loop_primitive_discrim (struct mem_read_abstraction_base *mra,
  2282. enum optr hint ATTRIBUTE_UNUSED)
  2283. {
  2284. uint8_t lb;
  2285. int status = mra->read (mra, 0, 1, &lb);
  2286. if (status < 0)
  2287. return OP_INVALID;
  2288. enum optr opbase = (lb & 0x80) ? OP_dbNE : OP_tbNE;
  2289. return opbase + ((lb & 0x70) >> 4);
  2290. }
  2291. static int
  2292. loop_primitive_decode (struct mem_read_abstraction_base *mra,
  2293. int *n_operands, struct operand **operands)
  2294. {
  2295. struct operand *op;
  2296. int n, offs = 1;
  2297. uint8_t lb;
  2298. int status = mra->read (mra, 0, 1, &lb);
  2299. if (status < 0)
  2300. return status;
  2301. enum LP_MODE mode = -1;
  2302. size_t i;
  2303. for (i = 0; i < sizeof (lp_mode) / sizeof (lp_mode[0]); ++i)
  2304. {
  2305. const struct lp *pb = lp_mode + i;
  2306. if ((lb & pb->mask) == pb->value)
  2307. {
  2308. mode = pb->mode;
  2309. break;
  2310. }
  2311. }
  2312. switch (mode)
  2313. {
  2314. case LP_REG:
  2315. op = create_register_operand (lb & 0x07);
  2316. if (op == NULL)
  2317. return -1;
  2318. operands[(*n_operands)++] = op;
  2319. break;
  2320. case LP_XY:
  2321. op = create_register_operand ((lb & 0x01) + REG_X);
  2322. if (op == NULL)
  2323. return -1;
  2324. operands[(*n_operands)++] = op;
  2325. break;
  2326. case LP_OPR:
  2327. n = x_opr_n_bytes (mra, 1);
  2328. if (n < 0)
  2329. return n;
  2330. offs += n;
  2331. op = x_opr_decode_with_size (mra, 1, lb & 0x03);
  2332. if (op == NULL)
  2333. return -1;
  2334. operands[(*n_operands)++] = op;
  2335. break;
  2336. }
  2337. return rel_15_7 (mra, offs + 1, n_operands, operands);
  2338. }
  2339. static enum optr
  2340. shift_discrim (struct mem_read_abstraction_base *mra,
  2341. enum optr hint ATTRIBUTE_UNUSED)
  2342. {
  2343. size_t i;
  2344. uint8_t sb;
  2345. int status = mra->read (mra, 0, 1, &sb);
  2346. if (status < 0)
  2347. return OP_INVALID;
  2348. enum SB_DIR dir = (sb & 0x40) ? SB_LEFT : SB_RIGHT;
  2349. enum SB_TYPE type = (sb & 0x80) ? SB_ARITHMETIC : SB_LOGICAL;
  2350. enum SB_MODE mode = -1;
  2351. for (i = 0; i < sizeof (sb_table) / sizeof (sb_table[0]); ++i)
  2352. {
  2353. const struct sb *sbe = sb_table + i;
  2354. if ((sb & sbe->mask) == sbe->value)
  2355. mode = sbe->mode;
  2356. }
  2357. if (mode == SB_ROT)
  2358. return (dir == SB_LEFT) ? OP_rol : OP_ror;
  2359. if (type == SB_LOGICAL)
  2360. return (dir == SB_LEFT) ? OP_lsl : OP_lsr;
  2361. return (dir == SB_LEFT) ? OP_asl : OP_asr;
  2362. }
  2363. static int
  2364. shift_decode (struct mem_read_abstraction_base *mra, int *n_operands,
  2365. struct operand **operands)
  2366. {
  2367. struct operand *op;
  2368. size_t i;
  2369. uint8_t byte;
  2370. int status = mra->read (mra, -1, 1, &byte);
  2371. if (status < 0)
  2372. return status;
  2373. uint8_t sb;
  2374. status = mra->read (mra, 0, 1, &sb);
  2375. if (status < 0)
  2376. return status;
  2377. enum SB_MODE mode = -1;
  2378. for (i = 0; i < sizeof (sb_table) / sizeof (sb_table[0]); ++i)
  2379. {
  2380. const struct sb *sbe = sb_table + i;
  2381. if ((sb & sbe->mask) == sbe->value)
  2382. mode = sbe->mode;
  2383. }
  2384. short osize = -1;
  2385. switch (mode)
  2386. {
  2387. case SB_REG_OPR_EFF:
  2388. case SB_ROT:
  2389. case SB_REG_OPR_OPR:
  2390. osize = sb & 0x03;
  2391. break;
  2392. case SB_OPR_N:
  2393. {
  2394. uint8_t xb;
  2395. status = mra->read (mra, 1, 1, &xb);
  2396. if (status < 0)
  2397. return status;
  2398. /* The size suffix is not printed if the OPR operand refers
  2399. directly to a register, because the size is implied by the
  2400. size of that register. */
  2401. if ((xb & 0xF8) != 0xB8)
  2402. osize = sb & 0x03;
  2403. }
  2404. break;
  2405. default:
  2406. break;
  2407. };
  2408. /* Destination register */
  2409. switch (mode)
  2410. {
  2411. case SB_REG_REG_N_EFF:
  2412. case SB_REG_REG_N:
  2413. op = create_register_operand (byte & 0x07);
  2414. if (op == NULL)
  2415. return -1;
  2416. operands[(*n_operands)++] = op;
  2417. break;
  2418. case SB_REG_OPR_EFF:
  2419. case SB_REG_OPR_OPR:
  2420. op = create_register_operand (byte & 0x07);
  2421. if (op == NULL)
  2422. return -1;
  2423. operands[(*n_operands)++] = op;
  2424. break;
  2425. case SB_ROT:
  2426. op = x_opr_decode_with_size (mra, 1, osize);
  2427. if (op == NULL)
  2428. return -1;
  2429. operands[(*n_operands)++] = op;
  2430. break;
  2431. default:
  2432. break;
  2433. }
  2434. /* Source register */
  2435. switch (mode)
  2436. {
  2437. case SB_REG_REG_N_EFF:
  2438. case SB_REG_REG_N:
  2439. op = create_register_operand_with_size (sb & 0x07, osize);
  2440. if (op == NULL)
  2441. return -1;
  2442. operands[(*n_operands)++] = op;
  2443. break;
  2444. case SB_REG_OPR_OPR:
  2445. op = x_opr_decode_with_size (mra, 1, osize);
  2446. if (op == NULL)
  2447. return -1;
  2448. operands[(*n_operands)++] = op;
  2449. break;
  2450. default:
  2451. break;
  2452. }
  2453. /* 3rd arg */
  2454. switch (mode)
  2455. {
  2456. case SB_REG_OPR_EFF:
  2457. case SB_OPR_N:
  2458. op = x_opr_decode_with_size (mra, 1, osize);
  2459. if (op == NULL)
  2460. return -1;
  2461. operands[(*n_operands)++] = op;
  2462. break;
  2463. case SB_REG_REG_N:
  2464. {
  2465. uint8_t xb;
  2466. status = mra->read (mra, 1, 1, &xb);
  2467. if (status < 0)
  2468. return status;
  2469. /* This case is slightly unusual.
  2470. If XB matches the binary pattern 0111XXXX, then instead of
  2471. interpreting this as a general OPR postbyte in the IMMe4 mode,
  2472. the XB byte is interpreted in s special way. */
  2473. if ((xb & 0xF0) == 0x70)
  2474. {
  2475. if (byte & 0x10)
  2476. {
  2477. int shift = ((sb & 0x08) >> 3) | ((xb & 0x0f) << 1);
  2478. op = create_immediate_operand (shift);
  2479. if (op == NULL)
  2480. return -1;
  2481. operands[(*n_operands)++] = op;
  2482. }
  2483. else
  2484. {
  2485. /* This should not happen. */
  2486. abort ();
  2487. }
  2488. }
  2489. else
  2490. {
  2491. op = x_opr_decode (mra, 1);
  2492. if (op == NULL)
  2493. return -1;
  2494. operands[(*n_operands)++] = op;
  2495. }
  2496. }
  2497. break;
  2498. case SB_REG_OPR_OPR:
  2499. {
  2500. uint8_t xb;
  2501. int n = x_opr_n_bytes (mra, 1);
  2502. if (n < 0)
  2503. return n;
  2504. status = mra->read (mra, 1 + n, 1, &xb);
  2505. if (status < 0)
  2506. return status;
  2507. if ((xb & 0xF0) == 0x70)
  2508. {
  2509. int imm = xb & 0x0F;
  2510. imm <<= 1;
  2511. imm |= (sb & 0x08) >> 3;
  2512. op = create_immediate_operand (imm);
  2513. if (op == NULL)
  2514. return -1;
  2515. operands[(*n_operands)++] = op;
  2516. }
  2517. else
  2518. {
  2519. op = x_opr_decode (mra, 1 + n);
  2520. if (op == NULL)
  2521. return -1;
  2522. operands[(*n_operands)++] = op;
  2523. }
  2524. }
  2525. break;
  2526. default:
  2527. break;
  2528. }
  2529. switch (mode)
  2530. {
  2531. case SB_REG_REG_N_EFF:
  2532. case SB_REG_OPR_EFF:
  2533. case SB_OPR_N:
  2534. {
  2535. int imm = (sb & 0x08) ? 2 : 1;
  2536. op = create_immediate_operand (imm);
  2537. if (op == NULL)
  2538. return -1;
  2539. operands[(*n_operands)++] = op;
  2540. }
  2541. break;
  2542. default:
  2543. break;
  2544. }
  2545. return 0;
  2546. }
  2547. static enum optr
  2548. psh_pul_discrim (struct mem_read_abstraction_base *mra,
  2549. enum optr hint ATTRIBUTE_UNUSED)
  2550. {
  2551. uint8_t byte;
  2552. int status = mra->read (mra, 0, 1, &byte);
  2553. if (status != 0)
  2554. return OP_INVALID;
  2555. return (byte & 0x80) ? OP_pull: OP_push;
  2556. }
  2557. static int
  2558. psh_pul_decode (struct mem_read_abstraction_base *mra,
  2559. int *n_operands, struct operand **operand)
  2560. {
  2561. struct operand *op;
  2562. uint8_t byte;
  2563. int status = mra->read (mra, 0, 1, &byte);
  2564. if (status != 0)
  2565. return status;
  2566. int bit;
  2567. if (byte & 0x40)
  2568. {
  2569. if ((byte & 0x3F) == 0)
  2570. {
  2571. op = create_register_all16_operand ();
  2572. if (op == NULL)
  2573. return -1;
  2574. operand[(*n_operands)++] = op;
  2575. }
  2576. else
  2577. for (bit = 5; bit >= 0; --bit)
  2578. {
  2579. if (byte & (0x1 << bit))
  2580. {
  2581. op = create_register_operand (oprregs2[bit]);
  2582. if (op == NULL)
  2583. return -1;
  2584. operand[(*n_operands)++] = op;
  2585. }
  2586. }
  2587. }
  2588. else
  2589. {
  2590. if ((byte & 0x3F) == 0)
  2591. {
  2592. op = create_register_all_operand ();
  2593. if (op == NULL)
  2594. return -1;
  2595. operand[(*n_operands)++] = op;
  2596. }
  2597. else
  2598. for (bit = 5; bit >= 0; --bit)
  2599. {
  2600. if (byte & (0x1 << bit))
  2601. {
  2602. op = create_register_operand (oprregs1[bit]);
  2603. if (op == NULL)
  2604. return -1;
  2605. operand[(*n_operands)++] = op;
  2606. }
  2607. }
  2608. }
  2609. return 0;
  2610. }
  2611. static enum optr
  2612. bit_field_discrim (struct mem_read_abstraction_base *mra,
  2613. enum optr hint ATTRIBUTE_UNUSED)
  2614. {
  2615. int status;
  2616. bfd_byte bb;
  2617. status = mra->read (mra, 0, 1, &bb);
  2618. if (status != 0)
  2619. return OP_INVALID;
  2620. return (bb & 0x80) ? OP_bfins : OP_bfext;
  2621. }
  2622. static int
  2623. bit_field_decode (struct mem_read_abstraction_base *mra,
  2624. int *n_operands, struct operand **operands)
  2625. {
  2626. struct operand *op;
  2627. int status;
  2628. bfd_byte byte2;
  2629. status = mra->read (mra, -1, 1, &byte2);
  2630. if (status != 0)
  2631. return status;
  2632. bfd_byte bb;
  2633. status = mra->read (mra, 0, 1, &bb);
  2634. if (status != 0)
  2635. return status;
  2636. enum BB_MODE mode = -1;
  2637. size_t i;
  2638. const struct opr_bb *bbs = 0;
  2639. for (i = 0; i < sizeof (bb_modes) / sizeof (bb_modes[0]); ++i)
  2640. {
  2641. bbs = bb_modes + i;
  2642. if ((bb & bbs->mask) == bbs->value)
  2643. {
  2644. mode = bbs->mode;
  2645. break;
  2646. }
  2647. }
  2648. int reg1 = byte2 & 0x07;
  2649. /* First operand */
  2650. switch (mode)
  2651. {
  2652. case BB_REG_REG_REG:
  2653. case BB_REG_REG_IMM:
  2654. case BB_REG_OPR_REG:
  2655. case BB_REG_OPR_IMM:
  2656. op = create_register_operand (reg1);
  2657. if (op == NULL)
  2658. return -1;
  2659. operands[(*n_operands)++] = op;
  2660. break;
  2661. case BB_OPR_REG_REG:
  2662. op = x_opr_decode_with_size (mra, 1, (bb >> 2) & 0x03);
  2663. if (op == NULL)
  2664. return -1;
  2665. operands[(*n_operands)++] = op;
  2666. break;
  2667. case BB_OPR_REG_IMM:
  2668. op = x_opr_decode_with_size (mra, 2, (bb >> 2) & 0x03);
  2669. if (op == NULL)
  2670. return -1;
  2671. operands[(*n_operands)++] = op;
  2672. break;
  2673. }
  2674. /* Second operand */
  2675. switch (mode)
  2676. {
  2677. case BB_REG_REG_REG:
  2678. case BB_REG_REG_IMM:
  2679. {
  2680. int reg_src = (bb >> 2) & 0x07;
  2681. op = create_register_operand (reg_src);
  2682. if (op == NULL)
  2683. return -1;
  2684. operands[(*n_operands)++] = op;
  2685. }
  2686. break;
  2687. case BB_OPR_REG_REG:
  2688. case BB_OPR_REG_IMM:
  2689. {
  2690. int reg_src = (byte2 & 0x07);
  2691. op = create_register_operand (reg_src);
  2692. if (op == NULL)
  2693. return -1;
  2694. operands[(*n_operands)++] = op;
  2695. }
  2696. break;
  2697. case BB_REG_OPR_REG:
  2698. op = x_opr_decode_with_size (mra, 1, (bb >> 2) & 0x03);
  2699. if (op == NULL)
  2700. return -1;
  2701. operands[(*n_operands)++] = op;
  2702. break;
  2703. case BB_REG_OPR_IMM:
  2704. op = x_opr_decode_with_size (mra, 2, (bb >> 2) & 0x03);
  2705. if (op == NULL)
  2706. return -1;
  2707. operands[(*n_operands)++] = op;
  2708. break;
  2709. }
  2710. /* Third operand */
  2711. switch (mode)
  2712. {
  2713. case BB_REG_REG_REG:
  2714. case BB_OPR_REG_REG:
  2715. case BB_REG_OPR_REG:
  2716. {
  2717. int reg_parm = bb & 0x03;
  2718. op = create_register_operand (reg_parm);
  2719. if (op == NULL)
  2720. return -1;
  2721. operands[(*n_operands)++] = op;
  2722. }
  2723. break;
  2724. case BB_REG_REG_IMM:
  2725. case BB_OPR_REG_IMM:
  2726. case BB_REG_OPR_IMM:
  2727. {
  2728. bfd_byte i1;
  2729. status = mra->read (mra, 1, 1, &i1);
  2730. if (status < 0)
  2731. return status;
  2732. int offset = i1 & 0x1f;
  2733. int width = bb & 0x03;
  2734. width <<= 3;
  2735. width |= i1 >> 5;
  2736. op = create_bitfield_operand (width, offset);
  2737. if (op == NULL)
  2738. return -1;
  2739. operands[(*n_operands)++] = op;
  2740. }
  2741. break;
  2742. }
  2743. return 0;
  2744. }
  2745. /* Decode the next instruction at MRA, according to OPC.
  2746. The operation to be performed is returned.
  2747. The number of operands, will be placed in N_OPERANDS.
  2748. The operands themselved into OPERANDS. */
  2749. static enum optr
  2750. decode_operation (const struct opcode *opc,
  2751. struct mem_read_abstraction_base *mra,
  2752. int *n_operands, struct operand **operands)
  2753. {
  2754. enum optr op = opc->operator;
  2755. if (opc->discriminator)
  2756. {
  2757. op = opc->discriminator (mra, opc->operator);
  2758. if (op == OP_INVALID)
  2759. return op;
  2760. }
  2761. if (opc->operands)
  2762. if (opc->operands (mra, n_operands, operands) < 0)
  2763. return OP_INVALID;
  2764. if (opc->operands2)
  2765. if (opc->operands2 (mra, n_operands, operands) < 0)
  2766. return OP_INVALID;
  2767. return op;
  2768. }
  2769. int
  2770. decode_s12z (enum optr *myoperator, short *osize,
  2771. int *n_operands, struct operand **operands,
  2772. struct mem_read_abstraction_base *mra)
  2773. {
  2774. int n_bytes = 0;
  2775. bfd_byte byte;
  2776. int status = mra->read (mra, 0, 1, &byte);
  2777. if (status < 0)
  2778. return status;
  2779. mra->advance (mra);
  2780. const struct opcode *opc = page1 + byte;
  2781. if (byte == PAGE2_PREBYTE)
  2782. {
  2783. /* Opcodes in page2 have an additional byte */
  2784. n_bytes++;
  2785. bfd_byte byte2;
  2786. status = mra->read (mra, 0, 1, &byte2);
  2787. if (status < 0)
  2788. return status;
  2789. mra->advance (mra);
  2790. opc = page2 + byte2;
  2791. }
  2792. *myoperator = decode_operation (opc, mra, n_operands, operands);
  2793. *osize = opc->osize;
  2794. /* Return the number of bytes in the instruction. */
  2795. if (*myoperator != OP_INVALID && opc->insn_bytes)
  2796. {
  2797. int n = opc->insn_bytes (mra);
  2798. if (n < 0)
  2799. return n;
  2800. n_bytes += n;
  2801. }
  2802. else
  2803. n_bytes += 1;
  2804. return n_bytes;
  2805. }