dsp.igen 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. // -*- C -*-
  2. // Simulator definition for the MIPS DSP ASE.
  3. // Copyright (C) 2005-2022 Free Software Foundation, Inc.
  4. // Contributed by MIPS Technologies, Inc. Written by Chao-ying Fu.
  5. //
  6. // This file is part of the MIPS sim
  7. //
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation; either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. // op: 0 = ADD, 1 = SUB, 2 = MUL
  21. // sat: 0 = no saturation, 1 = saturation
  22. :function:::void:do_ph_op:int rd, int rs, int rt, int op, int sat
  23. {
  24. int i;
  25. int32_t h0 = 0;
  26. int16_t h1, h2;
  27. uint32_t v1 = GPR[rs];
  28. uint32_t v2 = GPR[rt];
  29. uint32_t result = 0;
  30. for (i = 0; i < 32; i += 16, v1 >>= 16, v2 >>= 16)
  31. {
  32. h1 = (int16_t)(v1 & 0xffff);
  33. h2 = (int16_t)(v2 & 0xffff);
  34. if (op == 0) // ADD
  35. h0 = (int32_t)h1 + (int32_t)h2;
  36. else if (op == 1) // SUB
  37. h0 = (int32_t)h1 - (int32_t)h2;
  38. else // MUL
  39. h0 = (int32_t)h1 * (int32_t)h2;
  40. if (h0 > (int32_t)0x7fff || h0 < (int32_t)0xffff8000)
  41. {
  42. if (op == 0 || op == 1) // ADD, SUB
  43. DSPCR |= DSPCR_OUFLAG4;
  44. else if (op == 2) // MUL
  45. DSPCR |= DSPCR_OUFLAG5;
  46. if (sat == 1)
  47. {
  48. if (h0 > (int32_t)0x7fff)
  49. h0 = 0x7fff;
  50. else
  51. h0 = 0x8000;
  52. }
  53. }
  54. result |= ((uint32_t)((uint16_t)h0) << i);
  55. }
  56. GPR[rd] = EXTEND32 (result);
  57. }
  58. // op: 0 = ADD, 1 = SUB
  59. :function:::void:do_w_op:int rd, int rs, int rt, int op
  60. {
  61. int64_t h0;
  62. int32_t h1, h2;
  63. uint32_t v1 = GPR[rs];
  64. uint32_t v2 = GPR[rt];
  65. uint32_t result = 0;
  66. h1 = (int32_t)v1;
  67. h2 = (int32_t)v2;
  68. if (op == 0) // ADD
  69. h0 = (int64_t)h1 + (int64_t)h2;
  70. else // SUB
  71. h0 = (int64_t)h1 - (int64_t)h2;
  72. if (((h0 & 0x100000000LL) >> 1) != (h0 & 0x80000000))
  73. {
  74. DSPCR |= DSPCR_OUFLAG4;
  75. if (h0 & 0x100000000LL)
  76. h0 = 0x80000000;
  77. else
  78. h0 = 0x7fffffff;
  79. }
  80. GPR[rd] = EXTEND32 (h0);
  81. }
  82. // op: 0 = ADD, 1 = SUB
  83. // sat: 0 = no saturation, 1 = saturation
  84. :function:::void:do_qb_op:int rd, int rs, int rt, int op, int sat
  85. {
  86. int i;
  87. uint32_t h0;
  88. uint8_t h1, h2;
  89. uint32_t v1 = GPR[rs];
  90. uint32_t v2 = GPR[rt];
  91. uint32_t result = 0;
  92. for (i = 0; i < 32; i += 8, v1 >>= 8, v2 >>= 8)
  93. {
  94. h1 = (uint8_t)(v1 & 0xff);
  95. h2 = (uint8_t)(v2 & 0xff);
  96. if (op == 0) // ADD
  97. h0 = (uint32_t)h1 + (uint32_t)h2;
  98. else // SUB
  99. h0 = (uint32_t)h1 - (uint32_t)h2;
  100. if (h0 & 0x100)
  101. {
  102. DSPCR |= DSPCR_OUFLAG4;
  103. if (sat == 1)
  104. {
  105. if (op == 0) // ADD
  106. h0 = 0xff;
  107. else // SUB
  108. h0 = 0;
  109. }
  110. }
  111. result |= ((uint32_t)((uint8_t)h0) << i);
  112. }
  113. GPR[rd] = EXTEND32 (result);
  114. }
  115. // op: 0 = left, 1 = right
  116. :function:::void:do_qb_shift:int rd, int rt, int shift, int op
  117. {
  118. int i, j;
  119. uint8_t h0;
  120. uint32_t v1 = GPR[rt];
  121. uint32_t result = 0;
  122. for (i = 0; i < 32; i += 8, v1 >>= 8)
  123. {
  124. h0 = (uint8_t)(v1 & 0xff);
  125. if (op == 0) // left
  126. {
  127. for (j = 7; j >= 8 - shift; j--)
  128. {
  129. if (h0 & (1<<j))
  130. {
  131. DSPCR |= DSPCR_OUFLAG6;
  132. break;
  133. }
  134. }
  135. h0 = h0 << shift;
  136. }
  137. else // right
  138. h0 = h0 >> shift;
  139. result |= ((uint32_t)h0 << i);
  140. }
  141. GPR[rd] = EXTEND32 (result);
  142. }
  143. // op: 0 = left, 1 = right
  144. // sat: 0 = no saturation/rounding, 1 = saturation/rounding
  145. :function:::void:do_ph_shift:int rd, int rt, int shift, int op, int sat
  146. {
  147. int i, j;
  148. int16_t h0;
  149. uint32_t v1 = GPR[rt];
  150. uint32_t result = 0;
  151. int setcond;
  152. for (i = 0; i < 32; i += 16, v1 >>= 16)
  153. {
  154. h0 = (int16_t)(v1 & 0xffff);
  155. if (op == 0) // left
  156. {
  157. setcond = 0;
  158. if (h0 & (1<<15))
  159. {
  160. for (j = 14; j >= 15 - shift; j--)
  161. {
  162. if (!(h0 & (1 << j)))
  163. {
  164. DSPCR |= DSPCR_OUFLAG6;
  165. setcond = 1;
  166. break;
  167. }
  168. }
  169. }
  170. else
  171. {
  172. for (j = 14; j >= 15 - shift; j--)
  173. {
  174. if (h0 & (1 << j))
  175. {
  176. DSPCR |= DSPCR_OUFLAG6;
  177. setcond = 2;
  178. break;
  179. }
  180. }
  181. }
  182. h0 = h0 << shift;
  183. if (sat == 1)
  184. {
  185. if (setcond == 2)
  186. h0 = 0x7fff;
  187. else if (setcond == 1)
  188. h0 = 0x8000;
  189. }
  190. }
  191. else // right
  192. {
  193. if (sat == 1 && shift != 0 && (h0 & (1 << (shift-1))))
  194. h0 = (h0 >> shift) + 1;
  195. else
  196. h0 = h0 >> shift;
  197. }
  198. result |= ((uint32_t)((uint16_t)h0) << i);
  199. }
  200. GPR[rd] = EXTEND32 (result);
  201. }
  202. :function:::void:do_w_shll:int rd, int rt, int shift
  203. {
  204. int i;
  205. uint32_t v1 = GPR[rt];
  206. uint32_t result = 0;
  207. int setcond = 0;
  208. if (v1 & (1 << 31))
  209. {
  210. for (i = 30; i >= 31 - shift; i--)
  211. {
  212. if (!(v1 & (1 << i)))
  213. {
  214. DSPCR |= DSPCR_OUFLAG6;
  215. setcond = 1;
  216. break;
  217. }
  218. }
  219. }
  220. else
  221. {
  222. for (i = 30; i >= 31 - shift; i--)
  223. {
  224. if (v1 & (1 << i))
  225. {
  226. DSPCR |= DSPCR_OUFLAG6;
  227. setcond = 2;
  228. break;
  229. }
  230. }
  231. }
  232. if (setcond == 2)
  233. result = 0x7fffffff;
  234. else if (setcond == 1)
  235. result = 0x80000000;
  236. else
  237. result = v1 << shift;
  238. GPR[rd] = EXTEND32 (result);
  239. }
  240. :function:::void:do_ph_s_absq:int rd, int rt
  241. {
  242. int i;
  243. int16_t h0;
  244. uint32_t v1 = GPR[rt];
  245. uint32_t result = 0;
  246. for (i = 0; i < 32; i += 16, v1 >>= 16)
  247. {
  248. h0 = (int16_t)(v1 & 0xffff);
  249. if (h0 == (int16_t)0x8000)
  250. {
  251. DSPCR |= DSPCR_OUFLAG4;
  252. h0 = 0x7fff;
  253. }
  254. else if (h0 & 0x8000)
  255. h0 = -h0;
  256. result |= ((uint32_t)((uint16_t)h0) << i);
  257. }
  258. GPR[rd] = EXTEND32 (result);
  259. }
  260. :function:::void:do_w_s_absq:int rd, int rt
  261. {
  262. uint32_t v1 = GPR[rt];
  263. int32_t h0 = (int32_t)v1;
  264. if (h0 == (int32_t)0x80000000)
  265. {
  266. DSPCR |= DSPCR_OUFLAG4;
  267. h0 = 0x7fffffff;
  268. }
  269. else if (h0 & 0x80000000)
  270. h0 = -h0;
  271. GPR[rd] = EXTEND32 (h0);
  272. }
  273. :function:::void:do_qb_s_absq:int rd, int rt
  274. {
  275. int i;
  276. int8_t q0;
  277. uint32_t v1 = GPR[rt];
  278. uint32_t result = 0;
  279. for (i = 0; i < 32; i += 8, v1 >>= 8)
  280. {
  281. q0 = (int8_t)(v1 & 0xff);
  282. if (q0 == (int8_t)0x80)
  283. {
  284. DSPCR |= DSPCR_OUFLAG4;
  285. q0 = 0x7f;
  286. }
  287. else if (q0 & 0x80)
  288. q0 = -q0;
  289. result |= ((uint32_t)((uint8_t)q0) << i);
  290. }
  291. GPR[rd] = EXTEND32 (result);
  292. }
  293. :function:::void:do_addsc:int rd, int rs, int rt
  294. {
  295. uint32_t v1 = GPR[rs];
  296. uint32_t v2 = GPR[rt];
  297. uint64_t h0;
  298. h0 = (uint64_t)v1 + (uint64_t)v2;
  299. if (h0 & 0x100000000LL)
  300. DSPCR |= DSPCR_CARRY;
  301. GPR[rd] = EXTEND32 (h0);
  302. }
  303. :function:::void:do_addwc:int rd, int rs, int rt
  304. {
  305. uint32_t v1 = GPR[rs];
  306. uint32_t v2 = GPR[rt];
  307. uint64_t h0;
  308. int32_t h1 = (int32_t) v1;
  309. int32_t h2 = (int32_t) v2;
  310. h0 = (int64_t)h1 + (int64_t)h2
  311. + (int64_t)((DSPCR >> DSPCR_CARRY_SHIFT) & DSPCR_CARRY_MASK);
  312. if (((h0 & 0x100000000LL) >> 1) != (h0 & 0x80000000))
  313. DSPCR |= DSPCR_OUFLAG4;
  314. GPR[rd] = EXTEND32 (h0);
  315. }
  316. :function:::void:do_bitrev:int rd, int rt
  317. {
  318. int i;
  319. uint32_t v1 = GPR[rt];
  320. uint32_t h1 = 0;
  321. for (i = 0; i < 16; i++)
  322. {
  323. if (v1 & (1 << i))
  324. h1 |= (1 << (15 - i));
  325. }
  326. GPR[rd] = EXTEND32 (h1);
  327. }
  328. // op: 0 = EXTPV, 1 = EXTPDPV
  329. :function:::void:do_extpv:int rt, int ac, int rs, int op
  330. {
  331. uint32_t size = GPR[rs] & 0x1f;
  332. do_extp (SD_, rt, ac, size, op);
  333. }
  334. // op: 0 = EXTRV, 1 = EXTRV_R, 2 = EXTRV_RS
  335. :function:::void:do_extrv:int rt, int ac, int rs, int op
  336. {
  337. uint32_t shift = GPR[rs] & 0x1f;
  338. do_w_extr (SD_, rt, ac, shift, op);
  339. }
  340. :function:::void:do_extrv_s_h:int rt, int ac, int rs
  341. {
  342. uint32_t shift = GPR[rs] & 0x1f;
  343. do_h_extr (SD_, rt, ac, shift);
  344. }
  345. :function:::void:do_insv:int rt, int rs
  346. {
  347. uint32_t v1 = GPR[rs];
  348. uint32_t v2 = GPR[rt];
  349. uint32_t pos = (DSPCR >> DSPCR_POS_SHIFT) & DSPCR_POS_MASK;
  350. uint32_t size = (DSPCR >> DSPCR_SCOUNT_SHIFT) & DSPCR_SCOUNT_MASK;
  351. uint32_t mask1, mask2, mask3, result;
  352. if (size < 32)
  353. mask1 = (1 << size) - 1;
  354. else
  355. mask1 = 0xffffffff;
  356. mask2 = (1 << pos) - 1;
  357. if (pos + size < 32)
  358. mask3 = ~((1 << (pos + size)) - 1);
  359. else
  360. mask3 = 0;
  361. result = (v2 & mask3) | ((v1 & mask1) << pos) | (v2 & mask2);
  362. GPR[rt] = EXTEND32 (result);
  363. }
  364. // op: 0 = NORMAL, 1 = EXTEND16, 2 = EXTEND32
  365. :function:::void:do_lxx:int rd, int base, int index, int op
  366. {
  367. if (op == 0)
  368. GPR[rd] = do_load (SD_, AccessLength_BYTE, GPR[base], GPR[index]);
  369. else if (op == 1)
  370. GPR[rd] = EXTEND16 (do_load (SD_, AccessLength_HALFWORD, GPR[base], GPR[index]));
  371. else if (op == 2)
  372. GPR[rd] = EXTEND32 (do_load (SD_, AccessLength_WORD, GPR[base], GPR[index]));
  373. }
  374. :function:::void:do_modsub:int rd, int rs, int rt
  375. {
  376. uint32_t result = 0;
  377. uint32_t v1 = GPR[rs];
  378. uint32_t v2 = GPR[rt];
  379. uint32_t decr = v2 & 0xff;
  380. uint32_t lastindex = (v2 & 0xffff00) >> 8;
  381. if (v1 == 0)
  382. result = lastindex;
  383. else
  384. result = v1 - decr;
  385. GPR[rd] = EXTEND32 (result);
  386. }
  387. :function:::void:do_mthlip:int rs, int ac
  388. {
  389. uint32_t pos = (DSPCR >> DSPCR_POS_SHIFT) & DSPCR_POS_MASK;
  390. DSPHI(ac) = DSPLO(ac);
  391. DSPLO(ac) = GPR[rs];
  392. if (pos >= 32)
  393. Unpredictable ();
  394. else
  395. pos += 32;
  396. DSPCR &= (~DSPCR_POS_SMASK);
  397. DSPCR |= (pos & DSPCR_POS_MASK) << DSPCR_POS_SHIFT;
  398. }
  399. :function:::void:do_mulsaq_s_w_ph:int ac, int rs, int rt
  400. {
  401. int i;
  402. uint32_t v1 = GPR[rs];
  403. uint32_t v2 = GPR[rt];
  404. int16_t h1, h2;
  405. int32_t result;
  406. uint32_t lo = DSPLO(ac);
  407. uint32_t hi = DSPHI(ac);
  408. int64_t prod = (int64_t)((((uint64_t)hi) << 32) + (uint64_t)lo);
  409. for (i = 0; i < 32; i += 16, v1 >>= 16, v2 >>= 16)
  410. {
  411. h1 = (int16_t)(v1 & 0xffff);
  412. h2 = (int16_t)(v2 & 0xffff);
  413. if (h1 == (int16_t)0x8000 && h2 == (int16_t)0x8000)
  414. {
  415. DSPCR |= (1 << (DSPCR_OUFLAG_SHIFT + ac));
  416. result = (int32_t) 0x7fffffff;
  417. }
  418. else
  419. result = ((int32_t)h1 * (int32_t)h2) << 1;
  420. if (i == 0)
  421. prod -= (int64_t) result;
  422. else
  423. prod += (int64_t) result;
  424. }
  425. DSPLO(ac) = EXTEND32 (prod);
  426. DSPHI(ac) = EXTEND32 (prod >> 32);
  427. }
  428. :function:::void:do_ph_packrl:int rd, int rs, int rt
  429. {
  430. uint32_t v1 = GPR[rs];
  431. uint32_t v2 = GPR[rt];
  432. GPR[rd] = EXTEND32 ((v1 << 16) + (v2 >> 16));
  433. }
  434. :function:::void:do_qb_pick:int rd, int rs, int rt
  435. {
  436. int i, j;
  437. uint32_t v1 = GPR[rs];
  438. uint32_t v2 = GPR[rt];
  439. uint8_t h1, h2;
  440. uint32_t result = 0;
  441. for (i = 0, j = 0; i < 32; i += 8, j++, v1 >>= 8, v2 >>= 8)
  442. {
  443. h1 = (uint8_t)(v1 & 0xff);
  444. h2 = (uint8_t)(v2 & 0xff);
  445. if (DSPCR & (1 << (DSPCR_CCOND_SHIFT + j)))
  446. result |= (uint32_t)(h1 << i);
  447. else
  448. result |= (uint32_t)(h2 << i);
  449. }
  450. GPR[rd] = EXTEND32 (result);
  451. }
  452. :function:::void:do_ph_pick:int rd, int rs, int rt
  453. {
  454. int i, j;
  455. uint32_t v1 = GPR[rs];
  456. uint32_t v2 = GPR[rt];
  457. uint16_t h1, h2;
  458. uint32_t result = 0;
  459. for (i = 0, j = 0; i < 32; i += 16, j++, v1 >>= 16, v2 >>= 16)
  460. {
  461. h1 = (uint16_t)(v1 & 0xffff);
  462. h2 = (uint16_t)(v2 & 0xffff);
  463. if (DSPCR & (1 << (DSPCR_CCOND_SHIFT + j)))
  464. result |= (uint32_t)(h1 << i);
  465. else
  466. result |= (uint32_t)(h2 << i);
  467. }
  468. GPR[rd] = EXTEND32 (result);
  469. }
  470. // op: 0 = QBR, 1 = QBRA, 2 = QBL, 3 = QBLA
  471. :function:::void:do_qb_ph_precequ:int rd, int rt, int op
  472. {
  473. uint32_t v1 = GPR[rt];
  474. if (op == 0)
  475. GPR[rd] = EXTEND32 ((v1 & 0xff00) << 15) | ((v1 & 0xff) << 7);
  476. else if (op == 1)
  477. GPR[rd] = EXTEND32 ((v1 & 0xff0000) << 7) | ((v1 & 0xff) << 7);
  478. else if (op == 2)
  479. GPR[rd] = EXTEND32 ((v1 & 0xff000000) >> 1) | ((v1 & 0xff0000) >> 9);
  480. else if (op == 3)
  481. GPR[rd] = EXTEND32 ((v1 & 0xff000000) >> 1) | ((v1 & 0xff00) >> 1);
  482. }
  483. // op: 0 = QBR, 1 = QBRA, 2 = QBL, 3 = QBLA
  484. :function:::void:do_qb_ph_preceu:int rd, int rt, int op
  485. {
  486. uint32_t v1 = GPR[rt];
  487. if (op == 0)
  488. GPR[rd] = EXTEND32 ((v1 & 0xff00) << 8) | (v1 & 0xff);
  489. else if (op == 1)
  490. GPR[rd] = EXTEND32 ((v1 & 0xff0000) | (v1 & 0xff));
  491. else if (op == 2)
  492. GPR[rd] = EXTEND32 ((v1 & 0xff000000) >> 8) | ((v1 & 0xff0000) >> 16);
  493. else if (op == 3)
  494. GPR[rd] = EXTEND32 ((v1 & 0xff000000) >> 8) | ((v1 & 0xff00) >> 8);
  495. }
  496. // op: 0 = .PHL, 1 = PHR
  497. :function:::void:do_w_preceq:int rd, int rt, int op
  498. {
  499. uint32_t v1 = GPR[rt];
  500. if (op == 0)
  501. GPR[rd] = EXTEND32 (v1 & 0xffff0000);
  502. else if (op == 1)
  503. GPR[rd] = EXTEND32 ((v1 & 0xffff) << 16);
  504. }
  505. :function:::void:do_w_ph_precrq:int rd, int rs, int rt
  506. {
  507. uint32_t v1 = GPR[rs];
  508. uint32_t v2 = GPR[rt];
  509. uint32_t tempu = (v1 & 0xffff0000) >> 16;
  510. uint32_t tempv = (v2 & 0xffff0000) >> 16;
  511. GPR[rd] = EXTEND32 ((tempu << 16) | tempv);
  512. }
  513. // sat: 0 = PRECRQ.QB.PH, 1 = PRECRQU_S.QB.PH
  514. :function:::void:do_ph_qb_precrq:int rd, int rs, int rt, int sat
  515. {
  516. uint32_t v1 = GPR[rs];
  517. uint32_t v2 = GPR[rt];
  518. uint32_t tempu = 0, tempv = 0, tempw = 0, tempx = 0;
  519. if (sat == 0)
  520. {
  521. tempu = (v1 & 0xff000000) >> 24;
  522. tempv = (v1 & 0xff00) >> 8;
  523. tempw = (v2 & 0xff000000) >> 24;
  524. tempx = (v2 & 0xff00) >> 8;
  525. }
  526. else if (sat == 1)
  527. {
  528. if (v1 & 0x80000000)
  529. {
  530. DSPCR |= DSPCR_OUFLAG6;
  531. tempu = 0;
  532. }
  533. else if (!(v1 & 0x80000000) && ((v1 >> 16) > (uint32_t)0x7f80))
  534. {
  535. DSPCR |= DSPCR_OUFLAG6;
  536. tempu = 0xff;
  537. }
  538. else
  539. tempu = (v1 & 0x7f800000) >> 23;
  540. if (v1 & 0x8000)
  541. {
  542. DSPCR |= DSPCR_OUFLAG6;
  543. tempv = 0;
  544. }
  545. else if (!(v1 & 0x8000) && ((v1 & 0xffff) > (uint32_t)0x7f80))
  546. {
  547. DSPCR |= DSPCR_OUFLAG6;
  548. tempv = 0xff;
  549. }
  550. else
  551. tempv = (v1 & 0x7f80) >> 7;
  552. if (v2 & 0x80000000)
  553. {
  554. DSPCR |= DSPCR_OUFLAG6;
  555. tempw = 0;
  556. }
  557. else if (!(v2 & 0x80000000) && ((v2 >> 16) > (uint32_t)0x7f80))
  558. {
  559. DSPCR |= DSPCR_OUFLAG6;
  560. tempw = 0xff;
  561. }
  562. else
  563. tempw = (v2 & 0x7f800000) >> 23;
  564. if (v2 & 0x8000)
  565. {
  566. DSPCR |= DSPCR_OUFLAG6;
  567. tempx = 0;
  568. }
  569. else if (!(v2 & 0x8000) && ((v2 & 0xffff) > (uint32_t)0x7f80))
  570. {
  571. DSPCR |= DSPCR_OUFLAG6;
  572. tempx = 0xff;
  573. }
  574. else
  575. tempx = (v2 & 0x7f80) >> 7;
  576. }
  577. GPR[rd] = EXTEND32 ((tempu << 24) | (tempv << 16) | (tempw << 8) | tempx);
  578. }
  579. :function:::void:do_w_ph_rs_precrq:int rd, int rs, int rt
  580. {
  581. uint32_t v1 = GPR[rs];
  582. uint32_t v2 = GPR[rt];
  583. int32_t h1 = (int32_t)v1;
  584. int32_t h2 = (int32_t)v2;
  585. int64_t temp1 = (int64_t)h1 + (int64_t)0x8000;
  586. int32_t temp2;
  587. int64_t temp3 = (int64_t)h2 + (int64_t)0x8000;
  588. int32_t temp4;
  589. if (((temp1 & 0x100000000LL) >> 1) != (temp1 & 0x80000000))
  590. {
  591. DSPCR |= DSPCR_OUFLAG6;
  592. temp2 = 0x7fff;
  593. }
  594. else
  595. temp2 = (int32_t)((temp1 & 0xffff0000) >> 16);
  596. if (((temp3 & 0x100000000LL) >> 1) != (temp3 & 0x80000000))
  597. {
  598. DSPCR |= DSPCR_OUFLAG6;
  599. temp4 = 0x7fff;
  600. }
  601. else
  602. temp4 = (int32_t)((temp3 & 0xffff0000) >> 16);
  603. GPR[rd] = EXTEND32 ((temp2 << 16) | temp4);
  604. }
  605. :function:::void:do_qb_w_raddu:int rd, int rs
  606. {
  607. int i;
  608. uint8_t h0;
  609. uint32_t v1 = GPR[rs];
  610. uint32_t result = 0;
  611. for (i = 0; i < 32; i += 8, v1 >>= 8)
  612. {
  613. h0 = (uint8_t)(v1 & 0xff);
  614. result += (uint32_t)h0;
  615. }
  616. GPR[rd] = EXTEND32 (result);
  617. }
  618. :function:::void:do_rddsp:int rd, int mask
  619. {
  620. uint32_t result = 0;
  621. if (mask & 0x1)
  622. {
  623. result &= (~DSPCR_POS_SMASK);
  624. result |= (DSPCR & DSPCR_POS_SMASK);
  625. }
  626. if (mask & 0x2)
  627. {
  628. result &= (~DSPCR_SCOUNT_SMASK);
  629. result |= (DSPCR & DSPCR_SCOUNT_SMASK);
  630. }
  631. if (mask & 0x4)
  632. {
  633. result &= (~DSPCR_CARRY_SMASK);
  634. result |= (DSPCR & DSPCR_CARRY_SMASK);
  635. }
  636. if (mask & 0x8)
  637. {
  638. result &= (~DSPCR_OUFLAG_SMASK);
  639. result |= (DSPCR & DSPCR_OUFLAG_SMASK);
  640. }
  641. if (mask & 0x10)
  642. {
  643. result &= (~DSPCR_CCOND_SMASK);
  644. result |= (DSPCR & DSPCR_CCOND_SMASK);
  645. }
  646. if (mask & 0x20)
  647. {
  648. result &= (~DSPCR_EFI_SMASK);
  649. result |= (DSPCR & DSPCR_EFI_SMASK);
  650. }
  651. GPR[rd] = EXTEND32 (result);
  652. }
  653. // op: 0 = REPL.QB, 1 = REPLV.QB, 2 = REPL.PH, 3 = REPLV.PH
  654. :function:::void:do_repl:int rd, int p2, int op
  655. {
  656. if (op == 0)
  657. GPR[rd] = EXTEND32 ((p2 << 24) | (p2 << 16) | (p2 << 8) | p2);
  658. else if (op == 1)
  659. {
  660. uint32_t v1 = GPR[p2] & 0xff;
  661. GPR[rd] = EXTEND32 ((v1 << 24) | (v1 << 16) | (v1 << 8) | v1);
  662. }
  663. else if (op == 2)
  664. {
  665. int32_t v1 = p2;
  666. if (v1 & 0x200)
  667. v1 |= 0xfffffc00;
  668. GPR[rd] = EXTEND32 ((v1 << 16) | (v1 & 0xffff));
  669. }
  670. else if (op == 3)
  671. {
  672. uint32_t v1 = GPR[p2];
  673. v1 = v1 & 0xffff;
  674. GPR[rd] = EXTEND32 ((v1 << 16) | v1);
  675. }
  676. }
  677. :function:::void:do_shilov:int ac, int rs
  678. {
  679. int32_t shift = GPR[rs] & 0x3f;
  680. do_shilo (SD_, ac, shift);
  681. }
  682. // op: 0 = SHLLV, 1 = SHRAV
  683. // sat: 0 = normal, 1 = saturate/rounding
  684. :function:::void:do_ph_shl:int rd, int rt, int rs, int op, int sat
  685. {
  686. uint32_t shift = GPR[rs] & 0xf;
  687. do_ph_shift (SD_, rd, rt, shift, op, sat);
  688. }
  689. // op: 0 = SHLLV, 1 = SHRLV
  690. :function:::void:do_qb_shl:int rd, int rt, int rs, int op
  691. {
  692. uint32_t shift = GPR[rs] & 0x7;
  693. do_qb_shift (SD_, rd, rt, shift, op);
  694. }
  695. :function:::void:do_w_s_shllv:int rd, int rt, int rs
  696. {
  697. uint32_t shift = GPR[rs] & 0x1f;
  698. do_w_shll (SD_, rd, rt, shift);
  699. }
  700. :function:::void:do_ph_shrlv:int rd, int rt, int rs
  701. {
  702. uint32_t shift = GPR[rs] & 0xf;
  703. do_ph_shrl (SD_, rd, rt, shift);
  704. }
  705. :function:::void:do_w_r_shrav:int rd, int rt, int rs
  706. {
  707. uint32_t shift = GPR[rs] & 0x1f;
  708. do_w_shra (SD_, rd, rt, shift);
  709. }
  710. :function:::void:do_wrdsp:int rs, int mask
  711. {
  712. uint32_t v1 = GPR[rs];
  713. if (mask & 0x1)
  714. {
  715. DSPCR &= (~DSPCR_POS_SMASK);
  716. DSPCR |= (v1 & DSPCR_POS_SMASK);
  717. }
  718. if (mask & 0x2)
  719. {
  720. DSPCR &= (~DSPCR_SCOUNT_SMASK);
  721. DSPCR |= (v1 & DSPCR_SCOUNT_SMASK);
  722. }
  723. if (mask & 0x4)
  724. {
  725. DSPCR &= (~DSPCR_CARRY_SMASK);
  726. DSPCR |= (v1 & DSPCR_CARRY_SMASK);
  727. }
  728. if (mask & 0x8)
  729. {
  730. DSPCR &= (~DSPCR_OUFLAG_SMASK);
  731. DSPCR |= (v1 & DSPCR_OUFLAG_SMASK);
  732. }
  733. if (mask & 0x10)
  734. {
  735. DSPCR &= (~DSPCR_CCOND_SMASK);
  736. DSPCR |= (v1 & DSPCR_CCOND_SMASK);
  737. }
  738. if (mask & 0x20)
  739. {
  740. DSPCR &= (~DSPCR_EFI_SMASK);
  741. DSPCR |= (v1 & DSPCR_EFI_SMASK);
  742. }
  743. }
  744. // round: 0 = no rounding, 1 = rounding
  745. :function:::void:do_qb_shrav:int rd, int rt, int rs, int round
  746. {
  747. uint32_t shift = GPR[rs] & 0x7;
  748. do_qb_shra (SD_, rd, rt, shift, round);
  749. }
  750. :function:::void:do_append:int rt, int rs, int sa
  751. {
  752. uint32_t v0 = GPR[rs];
  753. uint32_t v1 = GPR[rt];
  754. uint32_t result;
  755. uint32_t mask = (1 << sa) - 1;
  756. result = (v1 << sa) | (v0 & mask);
  757. GPR[rt] = EXTEND32 (result);
  758. }
  759. :function:::void:do_balign:int rt, int rs, int bp
  760. {
  761. uint32_t v0 = GPR[rs];
  762. uint32_t v1 = GPR[rt];
  763. uint32_t result;
  764. if (bp == 0)
  765. result = v1;
  766. else
  767. result = (v1 << 8 * bp) | (v0 >> 8 * (4 - bp));
  768. GPR[rt] = EXTEND32 (result);
  769. }
  770. :function:::void:do_ph_w_mulsa:int ac, int rs, int rt
  771. {
  772. int i;
  773. uint32_t v1 = GPR[rs];
  774. uint32_t v2 = GPR[rt];
  775. int16_t h1, h2;
  776. int32_t result;
  777. uint32_t lo = DSPLO(ac);
  778. uint32_t hi = DSPHI(ac);
  779. int64_t prod = (int64_t)((((uint64_t)hi) << 32) + (uint64_t)lo);
  780. for (i = 0; i < 32; i += 16, v1 >>= 16, v2 >>= 16)
  781. {
  782. h1 = (int16_t)(v1 & 0xffff);
  783. h2 = (int16_t)(v2 & 0xffff);
  784. result = (int32_t)h1 * (int32_t)h2;
  785. if (i == 0)
  786. prod -= (int64_t) result;
  787. else
  788. prod += (int64_t) result;
  789. }
  790. DSPLO(ac) = EXTEND32 (prod);
  791. DSPHI(ac) = EXTEND32 (prod >> 32);
  792. }
  793. :function:::void:do_ph_qb_precr:int rd, int rs, int rt
  794. {
  795. uint32_t v1 = GPR[rs];
  796. uint32_t v2 = GPR[rt];
  797. uint32_t tempu = (v1 & 0xff0000) >> 16;
  798. uint32_t tempv = (v1 & 0xff);
  799. uint32_t tempw = (v2 & 0xff0000) >> 16;
  800. uint32_t tempx = (v2 & 0xff);
  801. GPR[rd] = EXTEND32 ((tempu << 24) | (tempv << 16) | (tempw << 8) | tempx);
  802. }
  803. :function:::void:do_prepend:int rt, int rs, int sa
  804. {
  805. uint32_t v0 = GPR[rs];
  806. uint32_t v1 = GPR[rt];
  807. uint32_t result;
  808. if (sa == 0)
  809. result = v1;
  810. else
  811. result = (v0 << (32 - sa)) | (v1 >> sa);
  812. GPR[rt] = EXTEND32 (result);
  813. }
  814. :function:::void:do_w_shra:int rd, int rt, int shift
  815. {
  816. uint32_t result = GPR[rt];
  817. int32_t h0 = (int32_t)result;
  818. if (shift != 0 && (h0 & (1 << (shift-1))))
  819. h0 = (h0 >> shift) + 1;
  820. else
  821. h0 = h0 >> shift;
  822. GPR[rd] = EXTEND32 (h0);
  823. }
  824. 011111,5.RS,5.RT,5.RD,01010,010000:SPECIAL3:32::ADDQ.PH
  825. "addq.ph r<RD>, r<RS>, r<RT>"
  826. *dsp:
  827. {
  828. do_ph_op (SD_, RD, RS, RT, 0, 0);
  829. }
  830. 011111,5.RS,5.RT,5.RD,01110,010000:SPECIAL3:32::ADDQ_S.PH
  831. "addq_s.ph r<RD>, r<RS>, r<RT>"
  832. *dsp:
  833. {
  834. do_ph_op (SD_, RD, RS, RT, 0, 1);
  835. }
  836. 011111,5.RS,5.RT,5.RD,10110,010000:SPECIAL3:32::ADDQ_S.W
  837. "addq_s.w r<RD>, r<RS>, r<RT>"
  838. *dsp:
  839. {
  840. do_w_op (SD_, RD, RS, RT, 0);
  841. }
  842. 011111,5.RS,5.RT,5.RD,00000,010000:SPECIAL3:32::ADDU.QB
  843. "addu.qb r<RD>, r<RS>, r<RT>"
  844. *dsp:
  845. {
  846. do_qb_op (SD_, RD, RS, RT, 0, 0);
  847. }
  848. 011111,5.RS,5.RT,5.RD,00100,010000:SPECIAL3:32::ADDU_S.QB
  849. "addu_s.qb r<RD>, r<RS>, r<RT>"
  850. *dsp:
  851. {
  852. do_qb_op (SD_, RD, RS, RT, 0, 1);
  853. }
  854. 011111,5.RS,5.RT,5.RD,01011,010000:SPECIAL3:32::SUBQ.PH
  855. "subq.ph r<RD>, r<RS>, r<RT>"
  856. *dsp:
  857. {
  858. do_ph_op (SD_, RD, RS, RT, 1, 0);
  859. }
  860. 011111,5.RS,5.RT,5.RD,01111,010000:SPECIAL3:32::SUBQ_S.PH
  861. "subq_s.ph r<RD>, r<RS>, r<RT>"
  862. *dsp:
  863. {
  864. do_ph_op (SD_, RD, RS, RT, 1, 1);
  865. }
  866. 011111,5.RS,5.RT,5.RD,10111,010000:SPECIAL3:32::SUBQ_S.W
  867. "subq_s.w r<RD>, r<RS>, r<RT>"
  868. *dsp:
  869. {
  870. do_w_op (SD_, RD, RS, RT, 1);
  871. }
  872. 011111,5.RS,5.RT,5.RD,00001,010000:SPECIAL3:32::SUBU.QB
  873. "subu.qb r<RD>, r<RS>, r<RT>"
  874. *dsp:
  875. {
  876. do_qb_op (SD_, RD, RS, RT, 1, 0);
  877. }
  878. 011111,5.RS,5.RT,5.RD,00101,010000:SPECIAL3:32::SUBU_S.QB
  879. "subu_s.qb r<RD>, r<RS>, r<RT>"
  880. *dsp:
  881. {
  882. do_qb_op (SD_, RD, RS, RT, 1, 1);
  883. }
  884. 011111,5.RS,5.RT,5.RD,10000,010000:SPECIAL3:32::ADDSC
  885. "addsc r<RD>, r<RS>, r<RT>"
  886. *dsp:
  887. {
  888. do_addsc (SD_, RD, RS, RT);
  889. }
  890. 011111,5.RS,5.RT,5.RD,10001,010000:SPECIAL3:32::ADDWC
  891. "addwc r<RD>, r<RS>, r<RT>"
  892. *dsp:
  893. {
  894. do_addwc (SD_, RD, RS, RT);
  895. }
  896. 011111,5.RS,5.RT,5.RD,10010,010000:SPECIAL3:32::MODSUB
  897. "modsub r<RD>, r<RS>, r<RT>"
  898. *dsp:
  899. {
  900. do_modsub (SD_, RD, RS, RT);
  901. }
  902. 011111,5.RS,00000,5.RD,10100,010000:SPECIAL3:32::RADDU.W.QB
  903. "raddu.w.qb r<RD>, r<RS>"
  904. *dsp:
  905. {
  906. do_qb_w_raddu (SD_, RD, RS);
  907. }
  908. 011111,00000,5.RT,5.RD,01001,010010:SPECIAL3:32::ABSQ_S.PH
  909. "absq_s.ph r<RD>, r<RT>"
  910. *dsp:
  911. {
  912. do_ph_s_absq (SD_, RD, RT);
  913. }
  914. 011111,00000,5.RT,5.RD,10001,010010:SPECIAL3:32::ABSQ_S.W
  915. "absq_s.w r<RD>, r<RT>"
  916. *dsp:
  917. {
  918. do_w_s_absq (SD_, RD, RT);
  919. }
  920. 011111,5.RS,5.RT,5.RD,01100,010001:SPECIAL3:32::PRECRQ.QB.PH
  921. "precrq.qb.ph r<RD>, r<RS>, r<RT>"
  922. *dsp:
  923. {
  924. do_ph_qb_precrq (SD_, RD, RS, RT, 0);
  925. }
  926. 011111,5.RS,5.RT,5.RD,10100,010001:SPECIAL3:32::PRECRQ.PH.W
  927. "precrq.ph.w r<RD>, r<RS>, r<RT>"
  928. *dsp:
  929. {
  930. do_w_ph_precrq (SD_, RD, RS, RT);
  931. }
  932. 011111,5.RS,5.RT,5.RD,10101,010001:SPECIAL3:32::PRECRQ_RS.PH.W
  933. "precrq_rs.ph.w r<RD>, r<RS>, r<RT>"
  934. *dsp:
  935. {
  936. do_w_ph_rs_precrq (SD_, RD, RS, RT);
  937. }
  938. 011111,5.RS,5.RT,5.RD,01111,010001:SPECIAL3:32::PRECRQU_S.QB.PH
  939. "precrqu_s.qb.ph r<RD>, r<RS>, r<RT>"
  940. *dsp:
  941. {
  942. do_ph_qb_precrq (SD_, RD, RS, RT, 1);
  943. }
  944. 011111,00000,5.RT,5.RD,01100,010010:SPECIAL3:32::PRECEQ.W.PHL
  945. "preceq.w.phl r<RD>, r<RT>"
  946. *dsp:
  947. {
  948. do_w_preceq (SD_, RD, RT, 0);
  949. }
  950. 011111,00000,5.RT,5.RD,01101,010010:SPECIAL3:32::PRECEQ.W.PHR
  951. "preceq.w.phr r<RD>, r<RT>"
  952. *dsp:
  953. {
  954. do_w_preceq (SD_, RD, RT, 1);
  955. }
  956. 011111,00000,5.RT,5.RD,00100,010010:SPECIAL3:32::PRECEQU.PH.QBL
  957. "precequ.ph.qbl r<RD>, r<RT>"
  958. *dsp:
  959. {
  960. do_qb_ph_precequ (SD_, RD, RT, 2);
  961. }
  962. 011111,00000,5.RT,5.RD,00101,010010:SPECIAL3:32::PRECEQU.PH.QBR
  963. "precequ.ph.qbr r<RD>, r<RT>"
  964. *dsp:
  965. {
  966. do_qb_ph_precequ (SD_, RD, RT, 0);
  967. }
  968. 011111,00000,5.RT,5.RD,00110,010010:SPECIAL3:32::PRECEQU.PH.QBLA
  969. "precequ.ph.qbla r<RD>, r<RT>"
  970. *dsp:
  971. {
  972. do_qb_ph_precequ (SD_, RD, RT, 3);
  973. }
  974. 011111,00000,5.RT,5.RD,00111,010010:SPECIAL3:32::PRECEQU.PH.QBRA
  975. "precequ.ph.qbra r<RD>, r<RT>"
  976. *dsp:
  977. {
  978. do_qb_ph_precequ (SD_, RD, RT, 1);
  979. }
  980. 011111,00000,5.RT,5.RD,11100,010010:SPECIAL3:32::PRECEU.PH.QBL
  981. "preceu.ph.qbl r<RD>, r<RT>"
  982. *dsp:
  983. {
  984. do_qb_ph_preceu (SD_, RD, RT, 2);
  985. }
  986. 011111,00000,5.RT,5.RD,11101,010010:SPECIAL3:32::PRECEU.PH.QBR
  987. "preceu.ph.qbr r<RD>, r<RT>"
  988. *dsp:
  989. {
  990. do_qb_ph_preceu (SD_, RD, RT, 0);
  991. }
  992. 011111,00000,5.RT,5.RD,11110,010010:SPECIAL3:32::PRECEU.PH.QBLA
  993. "preceu.ph.qbla r<RD>, r<RT>"
  994. *dsp:
  995. {
  996. do_qb_ph_preceu (SD_, RD, RT, 3);
  997. }
  998. 011111,00000,5.RT,5.RD,11111,010010:SPECIAL3:32::PRECEU.PH.QBRA
  999. "preceu.ph.qbra r<RD>, r<RT>"
  1000. *dsp:
  1001. {
  1002. do_qb_ph_preceu (SD_, RD, RT, 1);
  1003. }
  1004. 011111,00,3.SHIFT3,5.RT,5.RD,00000,010011:SPECIAL3:32::SHLL.QB
  1005. "shll.qb r<RD>, r<RT>, <SHIFT3>"
  1006. *dsp:
  1007. {
  1008. do_qb_shift (SD_, RD, RT, SHIFT3, 0);
  1009. }
  1010. 011111,5.RS,5.RT,5.RD,00010,010011:SPECIAL3:32::SHLLV.QB
  1011. "shllv.qb r<RD>, r<RT>, r<RS>"
  1012. *dsp:
  1013. {
  1014. do_qb_shl (SD_, RD, RT, RS, 0);
  1015. }
  1016. 011111,0,4.SHIFT4,5.RT,5.RD,01000,010011:SPECIAL3:32::SHLL.PH
  1017. "shll.ph r<RD>, r<RT>, <SHIFT4>"
  1018. *dsp:
  1019. {
  1020. do_ph_shift (SD_, RD, RT, SHIFT4, 0, 0);
  1021. }
  1022. 011111,5.RS,5.RT,5.RD,01010,010011:SPECIAL3:32::SHLLV.PH
  1023. "shllv.ph r<RD>, r<RT>, r<RS>"
  1024. *dsp:
  1025. {
  1026. do_ph_shl (SD_, RD, RT, RS, 0, 0);
  1027. }
  1028. 011111,0,4.SHIFT4,5.RT,5.RD,01100,010011:SPECIAL3:32::SHLL_S.PH
  1029. "shll_s.ph r<RD>, r<RT>, <SHIFT4>"
  1030. *dsp:
  1031. {
  1032. do_ph_shift (SD_, RD, RT, SHIFT4, 0, 1);
  1033. }
  1034. 011111,5.RS,5.RT,5.RD,01110,010011:SPECIAL3:32::SHLLV_S.PH
  1035. "shllv_s.ph r<RD>, r<RT>, r<RS>"
  1036. *dsp:
  1037. {
  1038. do_ph_shl (SD_, RD, RT, RS, 0, 1);
  1039. }
  1040. 011111,5.SHIFT5,5.RT,5.RD,10100,010011:SPECIAL3:32::SHLL_S.W
  1041. "shll_s.w r<RD>, r<RT>, <SHIFT5>"
  1042. *dsp:
  1043. {
  1044. do_w_shll (SD_, RD, RT, SHIFT5);
  1045. }
  1046. 011111,5.RS,5.RT,5.RD,10110,010011:SPECIAL3:32::SHLLV_S.W
  1047. "shllv_s.w r<RD>, r<RT>, r<RS>"
  1048. *dsp:
  1049. {
  1050. do_w_s_shllv (SD_, RD, RT, RS);
  1051. }
  1052. 011111,00,3.SHIFT3,5.RT,5.RD,00001,010011:SPECIAL3:32::SHRL.QB
  1053. "shrl.qb r<RD>, r<RT>, <SHIFT3>"
  1054. *dsp:
  1055. {
  1056. do_qb_shift (SD_, RD, RT, SHIFT3, 1);
  1057. }
  1058. 011111,5.RS,5.RT,5.RD,00011,010011:SPECIAL3:32::SHRLV.QB
  1059. "shrlv.qb r<RD>, r<RT>, r<RS>"
  1060. *dsp:
  1061. {
  1062. do_qb_shl (SD_, RD, RT, RS, 1);
  1063. }
  1064. 011111,0,4.SHIFT4,5.RT,5.RD,01001,010011:SPECIAL3:32::SHRA.PH
  1065. "shra.ph r<RD>, r<RT>, <SHIFT4>"
  1066. *dsp:
  1067. {
  1068. do_ph_shift (SD_, RD, RT, SHIFT4, 1, 0);
  1069. }
  1070. 011111,5.RS,5.RT,5.RD,01011,010011:SPECIAL3:32::SHRAV.PH
  1071. "shrav.ph r<RD>, r<RT>, r<RS>"
  1072. *dsp:
  1073. {
  1074. do_ph_shl (SD_, RD, RT, RS, 1, 0);
  1075. }
  1076. 011111,0,4.SHIFT4,5.RT,5.RD,01101,010011:SPECIAL3:32::SHRA_R.PH
  1077. "shra_r.ph r<RD>, r<RT>, <SHIFT4>"
  1078. *dsp:
  1079. {
  1080. do_ph_shift (SD_, RD, RT, SHIFT4, 1, 1);
  1081. }
  1082. 011111,5.RS,5.RT,5.RD,01111,010011:SPECIAL3:32::SHRAV_R.PH
  1083. "shrav_r.ph r<RD>, r<RT>, r<RS>"
  1084. *dsp:
  1085. {
  1086. do_ph_shl (SD_, RD, RT, RS, 1, 1);
  1087. }
  1088. 011111,5.SHIFT5,5.RT,5.RD,10101,010011:SPECIAL3:32::SHRA_R.W
  1089. "shra_r.w r<RD>, r<RT>, <SHIFT5>"
  1090. *dsp:
  1091. {
  1092. do_w_shra (SD_, RD, RT, SHIFT5);
  1093. }
  1094. 011111,5.RS,5.RT,5.RD,10111,010011:SPECIAL3:32::SHRAV_R.W
  1095. "shrav_r.w r<RD>, r<RT>, r<RS>"
  1096. *dsp:
  1097. {
  1098. do_w_r_shrav (SD_, RD, RT, RS);
  1099. }
  1100. // loc: 0 = qhl, 1 = qhr
  1101. :function:::void:do_qb_muleu:int rd, int rs, int rt, int loc
  1102. {
  1103. int i;
  1104. uint32_t result = 0;
  1105. uint32_t v1 = GPR[rs];
  1106. uint32_t v2 = GPR[rt];
  1107. uint16_t h1, h2;
  1108. uint32_t prod;
  1109. if (loc == 0)
  1110. v1 >>= 16;
  1111. for (i = 0; i < 32; i += 16, v1 >>= 8, v2 >>= 16)
  1112. {
  1113. h1 = (uint16_t)(v1 & 0xff);
  1114. h2 = (uint16_t)(v2 & 0xffff);
  1115. prod = (uint32_t)h1 * (uint32_t)h2;
  1116. if (prod > 0xffff)
  1117. {
  1118. DSPCR |= DSPCR_OUFLAG5;
  1119. prod = 0xffff;
  1120. }
  1121. result |= ((uint32_t)prod << i);
  1122. }
  1123. GPR[rd] = EXTEND32 (result);
  1124. }
  1125. 011111,5.RS,5.RT,5.RD,00110,010000:SPECIAL3:32::MULEU_S.PH.QBL
  1126. "muleu_s.ph.qbl r<RD>, r<RS>, r<RT>"
  1127. *dsp:
  1128. {
  1129. do_qb_muleu (SD_, RD, RS, RT, 0);
  1130. }
  1131. 011111,5.RS,5.RT,5.RD,00111,010000:SPECIAL3:32::MULEU_S.PH.QBR
  1132. "muleu_s.ph.qbr r<RD>, r<RS>, r<RT>"
  1133. *dsp:
  1134. {
  1135. do_qb_muleu (SD_, RD, RS, RT, 1);
  1136. }
  1137. // round: 0 = no rounding, 1 = rounding
  1138. :function:::void:do_ph_mulq:int rd, int rs, int rt, int round
  1139. {
  1140. int i;
  1141. uint32_t result = 0;
  1142. uint32_t v1 = GPR[rs];
  1143. uint32_t v2 = GPR[rt];
  1144. int16_t h1, h2;
  1145. int32_t prod;
  1146. for (i = 0; i < 32; i += 16, v1 >>= 16, v2 >>= 16)
  1147. {
  1148. h1 = (int16_t)(v1 & 0xffff);
  1149. h2 = (int16_t)(v2 & 0xffff);
  1150. if (h1 == (int16_t)0x8000 && h2 == (int16_t)0x8000)
  1151. {
  1152. DSPCR |= DSPCR_OUFLAG5;
  1153. prod = 0x7fffffff;
  1154. }
  1155. else
  1156. {
  1157. prod = ((int32_t)h1 * (int32_t)h2) << 1;
  1158. if (round == 1)
  1159. prod += (int32_t)0x8000;
  1160. }
  1161. result |= (((uint32_t)prod >> 16) << i);
  1162. }
  1163. GPR[rd] = EXTEND32 (result);
  1164. }
  1165. 011111,5.RS,5.RT,5.RD,11111,010000:SPECIAL3:32::MULQ_RS.PH
  1166. "mulq_rs.ph r<RD>, r<RS>, r<RT>"
  1167. *dsp:
  1168. {
  1169. do_ph_mulq (SD_, RD, RS, RT, 1);
  1170. }
  1171. // loc: 0 = phl, 1 = phr
  1172. :function:::void:do_ph_muleq:int rd, int rs, int rt, int loc
  1173. {
  1174. uint32_t v1 = GPR[rs];
  1175. uint32_t v2 = GPR[rt];
  1176. int16_t h1, h2;
  1177. int32_t prod;
  1178. if (loc == 0)
  1179. {
  1180. h1 = (int16_t)(v1 >> 16);
  1181. h2 = (int16_t)(v2 >> 16);
  1182. }
  1183. else
  1184. {
  1185. h1 = (int16_t)(v1 & 0xffff);
  1186. h2 = (int16_t)(v2 & 0xffff);
  1187. }
  1188. if (h1 == (int16_t)0x8000 && h2 == (int16_t)0x8000)
  1189. {
  1190. DSPCR |= DSPCR_OUFLAG5;
  1191. prod = 0x7fffffff;
  1192. }
  1193. else
  1194. prod = ((int32_t)h1 * (int32_t)h2) << 1;
  1195. GPR[rd] = EXTEND32 (prod);
  1196. }
  1197. 011111,5.RS,5.RT,5.RD,11100,010000:SPECIAL3:32::MULEQ_S.W.PHL
  1198. "muleq_s.w.phl r<RD>, r<RS>, r<RT>"
  1199. *dsp:
  1200. {
  1201. do_ph_muleq (SD_, RD, RS, RT, 0);
  1202. }
  1203. 011111,5.RS,5.RT,5.RD,11101,010000:SPECIAL3:32::MULEQ_S.W.PHR
  1204. "muleq_s.w.phr r<RD>, r<RS>, r<RT>"
  1205. *dsp:
  1206. {
  1207. do_ph_muleq (SD_, RD, RS, RT, 1);
  1208. }
  1209. // op: 0 = DPAU 1 = DPSU
  1210. // loc: 0 = qbl, 1 = qbr
  1211. :function:::void:do_qb_dot_product:int ac, int rs, int rt, int op, int loc
  1212. {
  1213. int i;
  1214. uint32_t v1 = GPR[rs];
  1215. uint32_t v2 = GPR[rt];
  1216. uint8_t h1, h2;
  1217. uint32_t lo = DSPLO(ac);
  1218. uint32_t hi = DSPHI(ac);
  1219. uint64_t prod = (((uint64_t)hi) << 32) + (uint64_t)lo;
  1220. if (loc == 0)
  1221. {
  1222. v1 >>= 16;
  1223. v2 >>= 16;
  1224. }
  1225. for (i = 0; i < 16; i += 8, v1 >>= 8, v2 >>= 8)
  1226. {
  1227. h1 = (uint8_t)(v1 & 0xff);
  1228. h2 = (uint8_t)(v2 & 0xff);
  1229. if (op == 0) // DPAU
  1230. prod += (uint64_t)h1 * (uint64_t)h2;
  1231. else // DPSU
  1232. prod -= (uint64_t)h1 * (uint64_t)h2;
  1233. }
  1234. DSPLO(ac) = EXTEND32 (prod);
  1235. DSPHI(ac) = EXTEND32 (prod >> 32);
  1236. }
  1237. 011111,5.RS,5.RT,000,2.AC,00011,110000:SPECIAL3:32::DPAU.H.QBL
  1238. "dpau.h.qbl ac<AC>, r<RS>, r<RT>"
  1239. *dsp:
  1240. {
  1241. do_qb_dot_product (SD_, AC, RS, RT, 0, 0);
  1242. }
  1243. 011111,5.RS,5.RT,000,2.AC,00111,110000:SPECIAL3:32::DPAU.H.QBR
  1244. "dpau.h.qbr ac<AC>, r<RS>, r<RT>"
  1245. *dsp:
  1246. {
  1247. do_qb_dot_product (SD_, AC, RS, RT, 0, 1);
  1248. }
  1249. 011111,5.RS,5.RT,000,2.AC,01011,110000:SPECIAL3:32::DPSU.H.QBL
  1250. "dpsu.h.qbl ac<AC>, r<RS>, r<RT>"
  1251. *dsp:
  1252. {
  1253. do_qb_dot_product (SD_, AC, RS, RT, 1, 0);
  1254. }
  1255. 011111,5.RS,5.RT,000,2.AC,01111,110000:SPECIAL3:32::DPSU.H.QBR
  1256. "dpsu.h.qbr ac<AC>, r<RS>, r<RT>"
  1257. *dsp:
  1258. {
  1259. do_qb_dot_product (SD_, AC, RS, RT, 1, 1);
  1260. }
  1261. // op: 0 = DPAQ 1 = DPSQ
  1262. :function:::void:do_ph_dot_product:int ac, int rs, int rt, int op
  1263. {
  1264. int i;
  1265. uint32_t v1 = GPR[rs];
  1266. uint32_t v2 = GPR[rt];
  1267. int16_t h1, h2;
  1268. int32_t result;
  1269. uint32_t lo = DSPLO(ac);
  1270. uint32_t hi = DSPHI(ac);
  1271. int64_t prod = (int64_t)((((uint64_t)hi) << 32) + (uint64_t)lo);
  1272. for (i = 0; i < 32; i += 16, v1 >>= 16, v2 >>= 16)
  1273. {
  1274. h1 = (int16_t)(v1 & 0xffff);
  1275. h2 = (int16_t)(v2 & 0xffff);
  1276. if (h1 == (int16_t)0x8000 && h2 == (int16_t)0x8000)
  1277. {
  1278. DSPCR |= (1 << (DSPCR_OUFLAG_SHIFT + ac));
  1279. result = (int32_t)0x7fffffff;
  1280. }
  1281. else
  1282. result = ((int32_t)h1 * (int32_t)h2) << 1;
  1283. if (op == 0) // DPAQ
  1284. prod += (int64_t)result;
  1285. else // DPSQ
  1286. prod -= (int64_t)result;
  1287. }
  1288. DSPLO(ac) = EXTEND32 (prod);
  1289. DSPHI(ac) = EXTEND32 (prod >> 32);
  1290. }
  1291. 011111,5.RS,5.RT,000,2.AC,00100,110000:SPECIAL3:32::DPAQ_S.W.PH
  1292. "dpaq_s.w.ph ac<AC>, r<RS>, r<RT>"
  1293. *dsp:
  1294. {
  1295. do_ph_dot_product (SD_, AC, RS, RT, 0);
  1296. }
  1297. 011111,5.RS,5.RT,000,2.AC,00101,110000:SPECIAL3:32::DPSQ_S.W.PH
  1298. "dpsq_s.w.ph ac<AC>, r<RS>, r<RT>"
  1299. *dsp:
  1300. {
  1301. do_ph_dot_product (SD_, AC, RS, RT, 1);
  1302. }
  1303. 011111,5.RS,5.RT,000,2.AC,00110,110000:SPECIAL3:32::MULSAQ_S.W.PH
  1304. "mulsaq_s.w.ph ac<AC>, r<RS>, r<RT>"
  1305. *dsp:
  1306. {
  1307. do_mulsaq_s_w_ph (SD_, AC, RS, RT);
  1308. }
  1309. // op: 0 = DPAQ 1 = DPSQ
  1310. :function:::void:do_w_dot_product:int ac, int rs, int rt, int op
  1311. {
  1312. uint32_t v1 = GPR[rs];
  1313. uint32_t v2 = GPR[rt];
  1314. int32_t h1, h2;
  1315. int64_t result;
  1316. uint32_t lo = DSPLO(ac);
  1317. uint32_t hi = DSPHI(ac);
  1318. uint32_t resultlo;
  1319. uint32_t resulthi;
  1320. uint32_t carry;
  1321. uint64_t temp1;
  1322. int64_t temp2;
  1323. h1 = (int32_t) v1;
  1324. h2 = (int32_t) v2;
  1325. if (h1 == 0x80000000 && h2 == 0x80000000)
  1326. {
  1327. DSPCR |= (1 << (DSPCR_OUFLAG_SHIFT + ac));
  1328. result = (int64_t) 0x7fffffffffffffffLL;
  1329. }
  1330. else
  1331. result = ((int64_t)h1 * (int64_t)h2) << 1;
  1332. resultlo = (uint32_t)(result);
  1333. resulthi = (uint32_t)(result >> 32);
  1334. if (op ==0) // DPAQ
  1335. {
  1336. temp1 = (uint64_t)lo + (uint64_t)resultlo;
  1337. carry = (uint32_t)((temp1 >> 32) & 1);
  1338. temp2 = (int64_t)((int32_t)hi) + (int64_t)((int32_t)resulthi) +
  1339. (int64_t)((int32_t)carry);
  1340. }
  1341. else // DPSQ
  1342. {
  1343. temp1 = (uint64_t)lo - (uint64_t)resultlo;
  1344. carry = (uint32_t)((temp1 >> 32) & 1);
  1345. temp2 = (int64_t)((int32_t)hi) - (int64_t)((int32_t)resulthi) -
  1346. (int64_t)((int32_t)carry);
  1347. }
  1348. if (((temp2 & 0x100000000LL) >> 1) != (temp2 & 0x80000000LL))
  1349. {
  1350. DSPCR |= (1 << (DSPCR_OUFLAG_SHIFT + ac));
  1351. if (temp2 & 0x100000000LL)
  1352. {
  1353. DSPLO(ac) = EXTEND32 (0x00000000);
  1354. DSPHI(ac) = EXTEND32 (0x80000000);
  1355. }
  1356. else
  1357. {
  1358. DSPLO(ac) = EXTEND32 (0xffffffff);
  1359. DSPHI(ac) = EXTEND32 (0x7fffffff);
  1360. }
  1361. }
  1362. else
  1363. {
  1364. DSPLO(ac) = EXTEND32 (temp1);
  1365. DSPHI(ac) = EXTEND32 (temp2);
  1366. }
  1367. }
  1368. 011111,5.RS,5.RT,000,2.AC,01100,110000:SPECIAL3:32::DPAQ_SA.L.W
  1369. "dpaq_sa.l.w ac<AC>, r<RS>, r<RT>"
  1370. *dsp:
  1371. {
  1372. do_w_dot_product (SD_, AC, RS, RT, 0);
  1373. }
  1374. 011111,5.RS,5.RT,000,2.AC,01101,110000:SPECIAL3:32::DPSQ_SA.L.W
  1375. "dpsq_sa.l.w ac<AC>, r<RS>, r<RT>"
  1376. *dsp:
  1377. {
  1378. do_w_dot_product (SD_, AC, RS, RT, 1);
  1379. }
  1380. // op: 0 = MAQ_S 1 = MAQ_SA
  1381. // loc: 0 = phl, 1 = phr
  1382. :function:::void:do_ph_maq:int ac, int rs, int rt, int op, int loc
  1383. {
  1384. int i;
  1385. uint32_t v1 = GPR[rs];
  1386. uint32_t v2 = GPR[rt];
  1387. int16_t h1, h2;
  1388. int32_t result;
  1389. uint32_t lo = DSPLO(ac);
  1390. uint32_t hi = DSPHI(ac);
  1391. int64_t prod = (int64_t)((((uint64_t)hi) << 32) + (uint64_t)lo);
  1392. if (loc == 0)
  1393. {
  1394. h1 = (int16_t)(v1 >> 16);
  1395. h2 = (int16_t)(v2 >> 16);
  1396. }
  1397. else
  1398. {
  1399. h1 = (int16_t)(v1 & 0xffff);
  1400. h2 = (int16_t)(v2 & 0xffff);
  1401. }
  1402. if (h1 == (int16_t)0x8000 && h2 == (int16_t)0x8000)
  1403. {
  1404. DSPCR |= (1 << (DSPCR_OUFLAG_SHIFT + ac));
  1405. result = (int32_t)0x7fffffff;
  1406. }
  1407. else
  1408. result = ((int32_t)h1 * (int32_t)h2) << 1;
  1409. prod += (int64_t)result;
  1410. if (op == 1) // MAQ_SA
  1411. {
  1412. if (prod & 0x8000000000000000LL)
  1413. {
  1414. for (i = 62; i >= 31; i--)
  1415. {
  1416. if (!(prod & ((int64_t)1 << i)))
  1417. {
  1418. DSPCR |= (1 << (DSPCR_OUFLAG_SHIFT + ac));
  1419. prod = 0xffffffff80000000LL;
  1420. break;
  1421. }
  1422. }
  1423. }
  1424. else
  1425. {
  1426. for (i = 62; i >= 31; i--)
  1427. {
  1428. if (prod & ((int64_t)1 << i))
  1429. {
  1430. DSPCR |= (1 << (DSPCR_OUFLAG_SHIFT + ac));
  1431. prod = 0x7fffffff;
  1432. break;
  1433. }
  1434. }
  1435. }
  1436. }
  1437. DSPLO(ac) = EXTEND32 (prod);
  1438. DSPHI(ac) = EXTEND32 (prod >> 32);
  1439. }
  1440. 011111,5.RS,5.RT,000,2.AC,10100,110000:SPECIAL3:32::MAQ_S.W.PHL
  1441. "maq_s.w.phl ac<AC>, r<RS>, r<RT>"
  1442. *dsp:
  1443. {
  1444. do_ph_maq (SD_, AC, RS, RT, 0, 0);
  1445. }
  1446. 011111,5.RS,5.RT,000,2.AC,10110,110000:SPECIAL3:32::MAQ_S.W.PHR
  1447. "maq_s.w.phr ac<AC>, r<RS>, r<RT>"
  1448. *dsp:
  1449. {
  1450. do_ph_maq (SD_, AC, RS, RT, 0, 1);
  1451. }
  1452. 011111,5.RS,5.RT,000,2.AC,10000,110000:SPECIAL3:32::MAQ_SA.W.PHL
  1453. "maq_sa.w.phl ac<AC>, r<RS>, r<RT>"
  1454. *dsp:
  1455. {
  1456. do_ph_maq (SD_, AC, RS, RT, 1, 0);
  1457. }
  1458. 011111,5.RS,5.RT,000,2.AC,10010,110000:SPECIAL3:32::MAQ_SA.W.PHR
  1459. "maq_sa.w.phr ac<AC>, r<RS>, r<RT>"
  1460. *dsp:
  1461. {
  1462. do_ph_maq (SD_, AC, RS, RT, 1, 1);
  1463. }
  1464. 011111,00000,5.RT,5.RD,11011,010010:SPECIAL3:32::BITREV
  1465. "bitrev r<RD>, r<RT>"
  1466. *dsp:
  1467. {
  1468. do_bitrev (SD_, RD, RT);
  1469. }
  1470. 011111,5.RS,5.RT,00000,00000,001100:SPECIAL3:32::INSV
  1471. "insv r<RT>, r<RS>"
  1472. *dsp:
  1473. {
  1474. do_insv (SD_, RT, RS);
  1475. }
  1476. 011111,00,8.IMM8,5.RD,00010,010010:SPECIAL3:32::REPL.QB
  1477. "repl.qb r<RD>, <IMM8>"
  1478. *dsp:
  1479. {
  1480. do_repl (SD_, RD, IMM8, 0);
  1481. }
  1482. 011111,00000,5.RT,5.RD,00011,010010:SPECIAL3:32::REPLV.QB
  1483. "replv.qb r<RD>, r<RT>"
  1484. *dsp:
  1485. {
  1486. do_repl (SD_, RD, RT, 1);
  1487. }
  1488. 011111,10.IMM10,5.RD,01010,010010:SPECIAL3:32::REPL.PH
  1489. "repl.ph r<RD>, <IMM10>"
  1490. *dsp:
  1491. {
  1492. do_repl (SD_, RD, IMM10, 2);
  1493. }
  1494. 011111,00000,5.RT,5.RD,01011,010010:SPECIAL3:32::REPLV.PH
  1495. "replv.ph r<RD>, r<RT>"
  1496. *dsp:
  1497. {
  1498. do_repl (SD_, RD, RT, 3);
  1499. }
  1500. // op: 0 = EQ, 1 = LT, 2 = LE
  1501. :function:::void:do_qb_cmpu:int rs, int rt, int op
  1502. {
  1503. int i, j;
  1504. uint32_t v1 = GPR[rs];
  1505. uint32_t v2 = GPR[rt];
  1506. uint8_t h1, h2;
  1507. uint32_t mask;
  1508. for (i = 0, j = 0; i < 32; i += 8, j++, v1 >>= 8, v2 >>= 8)
  1509. {
  1510. h1 = (uint8_t)(v1 & 0xff);
  1511. h2 = (uint8_t)(v2 & 0xff);
  1512. mask = ~(1 << (DSPCR_CCOND_SHIFT + j));
  1513. DSPCR &= mask;
  1514. if (op == 0) // EQ
  1515. DSPCR |= ((h1 == h2) << (DSPCR_CCOND_SHIFT + j));
  1516. else if (op == 1) // LT
  1517. DSPCR |= ((h1 < h2) << (DSPCR_CCOND_SHIFT + j));
  1518. else // LE
  1519. DSPCR |= ((h1 <= h2) << (DSPCR_CCOND_SHIFT + j));
  1520. }
  1521. }
  1522. 011111,5.RS,5.RT,00000,00000,010001:SPECIAL3:32::CMPU.EQ.QB
  1523. "cmpu.eq.qb r<RS>, r<RT>"
  1524. *dsp:
  1525. {
  1526. do_qb_cmpu (SD_, RS, RT, 0);
  1527. }
  1528. 011111,5.RS,5.RT,00000,00001,010001:SPECIAL3:32::CMPU.LT.QB
  1529. "cmpu.lt.qb r<RS>, r<RT>"
  1530. *dsp:
  1531. {
  1532. do_qb_cmpu (SD_, RS, RT, 1);
  1533. }
  1534. 011111,5.RS,5.RT,00000,00010,010001:SPECIAL3:32::CMPU.LE.QB
  1535. "cmpu.le.qb r<RS>, r<RT>"
  1536. *dsp:
  1537. {
  1538. do_qb_cmpu (SD_, RS, RT, 2);
  1539. }
  1540. // op: 0 = EQ, 1 = LT, 2 = LE
  1541. :function:::void:do_qb_cmpgu:int rd, int rs, int rt, int op
  1542. {
  1543. int i, j;
  1544. uint32_t v1 = GPR[rs];
  1545. uint32_t v2 = GPR[rt];
  1546. uint8_t h1, h2;
  1547. uint32_t result = 0;
  1548. for (i = 0, j = 0; i < 32; i += 8, j++, v1 >>= 8, v2 >>= 8)
  1549. {
  1550. h1 = (uint8_t)(v1 & 0xff);
  1551. h2 = (uint8_t)(v2 & 0xff);
  1552. if (op == 0) // EQ
  1553. result |= ((h1 == h2) << j);
  1554. else if (op == 1) // LT
  1555. result |= ((h1 < h2) << j);
  1556. else // LE
  1557. result |= ((h1 <= h2) << j);
  1558. }
  1559. GPR[rd] = EXTEND32 (result);
  1560. }
  1561. 011111,5.RS,5.RT,5.RD,00100,010001:SPECIAL3:32::CMPGU.EQ.QB
  1562. "cmpgu.eq.qb r<RD>, r<RS>, r<RT>"
  1563. *dsp:
  1564. {
  1565. do_qb_cmpgu (SD_, RD, RS, RT, 0);
  1566. }
  1567. 011111,5.RS,5.RT,5.RD,00101,010001:SPECIAL3:32::CMPGU.LT.QB
  1568. "cmpgu.lt.qb r<RD>, r<RS>, r<RT>"
  1569. *dsp:
  1570. {
  1571. do_qb_cmpgu (SD_, RD, RS, RT, 1);
  1572. }
  1573. 011111,5.RS,5.RT,5.RD,00110,010001:SPECIAL3:32::CMPGU.LE.QB
  1574. "cmpgu.le.qb r<RD>, r<RS>, r<RT>"
  1575. *dsp:
  1576. {
  1577. do_qb_cmpgu (SD_, RD, RS, RT, 2);
  1578. }
  1579. // op: 0 = EQ, 1 = LT, 2 = LE
  1580. :function:::void:do_ph_cmpu:int rs, int rt, int op
  1581. {
  1582. int i, j;
  1583. uint32_t v1 = GPR[rs];
  1584. uint32_t v2 = GPR[rt];
  1585. int16_t h1, h2;
  1586. uint32_t mask;
  1587. for (i = 0, j = 0; i < 32; i += 16, j++, v1 >>= 16, v2 >>= 16)
  1588. {
  1589. h1 = (int16_t)(v1 & 0xffff);
  1590. h2 = (int16_t)(v2 & 0xffff);
  1591. mask = ~(1 << (DSPCR_CCOND_SHIFT + j));
  1592. DSPCR &= mask;
  1593. if (op == 0) // EQ
  1594. DSPCR |= ((h1 == h2) << (DSPCR_CCOND_SHIFT + j));
  1595. else if (op == 1) // LT
  1596. DSPCR |= ((h1 < h2) << (DSPCR_CCOND_SHIFT + j));
  1597. else // LE
  1598. DSPCR |= ((h1 <= h2) << (DSPCR_CCOND_SHIFT + j));
  1599. }
  1600. }
  1601. 011111,5.RS,5.RT,00000,01000,010001:SPECIAL3:32::CMP.EQ.PH
  1602. "cmp.eq.ph r<RS>, r<RT>"
  1603. *dsp:
  1604. {
  1605. do_ph_cmpu (SD_, RS, RT, 0);
  1606. }
  1607. 011111,5.RS,5.RT,00000,01001,010001:SPECIAL3:32::CMP.LT.PH
  1608. "cmp.lt.ph r<RS>, r<RT>"
  1609. *dsp:
  1610. {
  1611. do_ph_cmpu (SD_, RS, RT, 1);
  1612. }
  1613. 011111,5.RS,5.RT,00000,01010,010001:SPECIAL3:32::CMP.LE.PH
  1614. "cmp.le.ph r<RS>, r<RT>"
  1615. *dsp:
  1616. {
  1617. do_ph_cmpu (SD_, RS, RT, 2);
  1618. }
  1619. 011111,5.RS,5.RT,5.RD,00011,010001:SPECIAL3:32::PICK.QB
  1620. "pick.qb r<RD>, r<RS>, r<RT>"
  1621. *dsp:
  1622. {
  1623. do_qb_pick (SD_, RD, RS, RT);
  1624. }
  1625. 011111,5.RS,5.RT,5.RD,01011,010001:SPECIAL3:32::PICK.PH
  1626. "pick.ph r<RD>, r<RS>, r<RT>"
  1627. *dsp:
  1628. {
  1629. do_ph_pick (SD_, RD, RS, RT);
  1630. }
  1631. 011111,5.RS,5.RT,5.RD,01110,010001:SPECIAL3:32::PACKRL.PH
  1632. "packrl.ph r<RD>, r<RS>, r<RT>"
  1633. *dsp:
  1634. {
  1635. do_ph_packrl (SD_, RD, RS, RT);
  1636. }
  1637. // op: 0 = EXTR, 1 = EXTR_R, 2 = EXTR_RS
  1638. :function:::void:do_w_extr:int rt, int ac, int shift, int op
  1639. {
  1640. int i;
  1641. uint32_t lo = DSPLO(ac);
  1642. uint32_t hi = DSPHI(ac);
  1643. uint64_t prod = (((uint64_t)hi) << 32) + (uint64_t)lo;
  1644. int64_t result = (int64_t)prod;
  1645. int setcond = 0;
  1646. if (!(prod & 0x8000000000000000LL))
  1647. {
  1648. for (i = 62; i >= (shift + 31); i--)
  1649. {
  1650. if (prod & ((uint64_t)1 << i))
  1651. {
  1652. DSPCR |= DSPCR_OUFLAG7;
  1653. setcond = 1;
  1654. break;
  1655. }
  1656. }
  1657. if (((prod >> (shift - 1)) & 0xffffffffLL) == 0xffffffffLL)
  1658. {
  1659. DSPCR |= DSPCR_OUFLAG7;
  1660. setcond = 1;
  1661. }
  1662. }
  1663. else
  1664. {
  1665. for (i = 62; i >= (shift + 31); i--)
  1666. {
  1667. if (!(prod & ((uint64_t)1 << i)))
  1668. {
  1669. DSPCR |= DSPCR_OUFLAG7;
  1670. setcond = 2;
  1671. break;
  1672. }
  1673. }
  1674. }
  1675. if (op == 0) // EXTR
  1676. result = result >> shift;
  1677. else if (op == 1) // EXTR_R
  1678. {
  1679. if (shift != 0)
  1680. result = ((result >> (shift - 1)) + 1) >> 1;
  1681. else
  1682. result = result >> shift;
  1683. }
  1684. else // EXTR_RS
  1685. {
  1686. if (setcond == 1)
  1687. result = 0x7fffffff;
  1688. else if (setcond == 2)
  1689. result = 0x80000000;
  1690. else
  1691. {
  1692. if (shift != 0)
  1693. result = ((result >> (shift - 1)) + 1) >> 1;
  1694. else
  1695. result = result >> shift;
  1696. }
  1697. }
  1698. GPR[rt] = EXTEND32 (result);
  1699. }
  1700. 011111,5.SHIFT,5.RT,000,2.AC,00000,111000:SPECIAL3:32::EXTR.W
  1701. "extr.w r<RT>, ac<AC>, <SHIFT>"
  1702. *dsp:
  1703. {
  1704. do_w_extr (SD_, RT, AC, SHIFT, 0);
  1705. }
  1706. 011111,5.RS,5.RT,000,2.AC,00001,111000:SPECIAL3:32::EXTRV.W
  1707. "extrv.w r<RT>, ac<AC>, r<RS>"
  1708. *dsp:
  1709. {
  1710. do_extrv (SD_, RT, AC, RS, 0);
  1711. }
  1712. 011111,5.SHIFT,5.RT,000,2.AC,00100,111000:SPECIAL3:32::EXTR_R.W
  1713. "extr_r.w r<RT>, ac<AC>, <SHIFT>"
  1714. *dsp:
  1715. {
  1716. do_w_extr (SD_, RT, AC, SHIFT, 1);
  1717. }
  1718. 011111,5.RS,5.RT,000,2.AC,00101,111000:SPECIAL3:32::EXTRV_R.W
  1719. "extrv_r.w r<RT>, ac<AC>, r<RS>"
  1720. *dsp:
  1721. {
  1722. do_extrv (SD_, RT, AC, RS, 1);
  1723. }
  1724. 011111,5.SHIFT,5.RT,000,2.AC,00110,111000:SPECIAL3:32::EXTR_RS.W
  1725. "extr_rs.w r<RT>, ac<AC>, <SHIFT>"
  1726. *dsp:
  1727. {
  1728. do_w_extr (SD_, RT, AC, SHIFT, 2);
  1729. }
  1730. 011111,5.RS,5.RT,000,2.AC,00111,111000:SPECIAL3:32::EXTRV_RS.W
  1731. "extrv_rs.w r<RT>, ac<AC>, r<RS>"
  1732. *dsp:
  1733. {
  1734. do_extrv (SD_, RT, AC, RS, 2);
  1735. }
  1736. :function:::void:do_h_extr:int rt, int ac, int shift
  1737. {
  1738. int i;
  1739. uint32_t lo = DSPLO(ac);
  1740. uint32_t hi = DSPHI(ac);
  1741. uint64_t prod = (((uint64_t)hi) << 32) + (uint64_t)lo;
  1742. int64_t result = (int64_t)prod;
  1743. int64_t value = 0xffffffffffff8000LL;
  1744. result >>= shift;
  1745. if (result > 0x7fff)
  1746. {
  1747. result = 0x7fff;
  1748. DSPCR |= DSPCR_OUFLAG7;
  1749. }
  1750. else if (result < value)
  1751. {
  1752. result = value;
  1753. DSPCR |= DSPCR_OUFLAG7;
  1754. }
  1755. GPR[rt] = EXTEND32 (result);
  1756. }
  1757. 011111,5.SHIFT,5.RT,000,2.AC,01110,111000:SPECIAL3:32::EXTR_S.H
  1758. "extr_s.h r<RT>, ac<AC>, <SHIFT>"
  1759. *dsp:
  1760. {
  1761. do_h_extr (SD_, RT, AC, SHIFT);
  1762. }
  1763. 011111,5.RS,5.RT,000,2.AC,01111,111000:SPECIAL3:32::EXTRV_S.H
  1764. "extrv_s.h r<RT>, ac<AC>, r<RS>"
  1765. *dsp:
  1766. {
  1767. do_extrv_s_h (SD_, RT, AC, RS);
  1768. }
  1769. // op: 0 = EXTP, 1 = EXTPDP
  1770. :function:::void:do_extp:int rt, int ac, int size, int op
  1771. {
  1772. int32_t pos = (DSPCR >> DSPCR_POS_SHIFT) & DSPCR_POS_MASK;
  1773. uint32_t lo = DSPLO(ac);
  1774. uint32_t hi = DSPHI(ac);
  1775. uint64_t prod = (((uint64_t)hi) << 32) + (uint64_t)lo;
  1776. uint64_t result = 0;
  1777. if (pos - (size + 1) >= -1)
  1778. {
  1779. prod >>= (pos - size);
  1780. result = prod & (((uint64_t)1 << (size + 1)) - 1);
  1781. DSPCR &= (~DSPCR_EFI_SMASK);
  1782. if (op == 1) // EXTPDP
  1783. {
  1784. if (pos - (size + 1) >= 0)
  1785. {
  1786. DSPCR &= (~DSPCR_POS_SMASK);
  1787. DSPCR |= ((pos - (size + 1)) & DSPCR_POS_MASK) << DSPCR_POS_SHIFT;
  1788. }
  1789. else if (pos - (size + 1) == -1)
  1790. {
  1791. DSPCR |= DSPCR_POS_SMASK;
  1792. }
  1793. }
  1794. }
  1795. else
  1796. {
  1797. DSPCR |= DSPCR_EFI;
  1798. Unpredictable ();
  1799. }
  1800. GPR[rt] = EXTEND32 (result);
  1801. }
  1802. 011111,5.SIZE,5.RT,000,2.AC,00010,111000:SPECIAL3:32::EXTP
  1803. "extp r<RT>, ac<AC>, <SIZE>"
  1804. *dsp:
  1805. {
  1806. do_extp (SD_, RT, AC, SIZE, 0);
  1807. }
  1808. 011111,5.RS,5.RT,000,2.AC,00011,111000:SPECIAL3:32::EXTPV
  1809. "extpv r<RT>, ac<AC>, r<RS>"
  1810. *dsp:
  1811. {
  1812. do_extpv (SD_, RT, AC, RS, 0);
  1813. }
  1814. 011111,5.SIZE,5.RT,000,2.AC,01010,111000:SPECIAL3:32::EXTPDP
  1815. "extpdp r<RT>, ac<AC>, <SIZE>"
  1816. *dsp:
  1817. {
  1818. do_extp (SD_, RT, AC, SIZE, 1);
  1819. }
  1820. 011111,5.RS,5.RT,000,2.AC,01011,111000:SPECIAL3:32::EXTPDPV
  1821. "extpdpv r<RT>, ac<AC>, r<RS>"
  1822. *dsp:
  1823. {
  1824. do_extpv (SD_, RT, AC, RS, 1);
  1825. }
  1826. :function:::void:do_shilo:int ac, int shift
  1827. {
  1828. uint32_t lo = DSPLO(ac);
  1829. uint32_t hi = DSPHI(ac);
  1830. uint64_t prod = (((uint64_t)hi) << 32) + (uint64_t)lo;
  1831. if (shift > 31)
  1832. shift = shift - 64;
  1833. if (shift >= 0)
  1834. prod >>= shift;
  1835. else
  1836. prod <<= (-shift);
  1837. DSPLO(ac) = EXTEND32 (prod);
  1838. DSPHI(ac) = EXTEND32 (prod >> 32);
  1839. }
  1840. 011111,6.SHIFT6,0000,000,2.AC,11010,111000:SPECIAL3:32::SHILO
  1841. "shilo ac<AC>, <SHIFT6>"
  1842. *dsp:
  1843. {
  1844. do_shilo (SD_, AC, SHIFT6);
  1845. }
  1846. 011111,5.RS,00000,000,2.AC,11011,111000:SPECIAL3:32::SHILOV
  1847. "shilov ac<AC>, r<RS>"
  1848. *dsp:
  1849. {
  1850. do_shilov (SD_, AC, RS);
  1851. }
  1852. 011111,5.RS,00000,000,2.AC,11111,111000:SPECIAL3:32::MTHLIP
  1853. "mthlip r<RS>, ac<AC>"
  1854. *dsp:
  1855. {
  1856. do_mthlip (SD_, RS, AC);
  1857. }
  1858. 011111,5.RS,10.MASK10,10011,111000:SPECIAL3:32::WRDSP
  1859. "wrdsp r<RS>":MASK10 == 1111111111
  1860. "wrdsp r<RS>, <MASK10>"
  1861. *dsp:
  1862. {
  1863. do_wrdsp (SD_, RS, MASK10);
  1864. }
  1865. 011111,10.MASK10,5.RD,10010,111000:SPECIAL3:32::RDDSP
  1866. "rddsp r<RD>":MASK10 == 1111111111
  1867. "rddsp r<RD>, <MASK10>"
  1868. *dsp:
  1869. {
  1870. do_rddsp (SD_, RD, MASK10);
  1871. }
  1872. 011111,5.BASE,5.INDEX,5.RD,00110,001010:SPECIAL3:32::LBUX
  1873. "lbux r<RD>, r<INDEX>(r<BASE>)"
  1874. *dsp:
  1875. {
  1876. do_lxx (SD_, RD, BASE, INDEX, 0);
  1877. }
  1878. 011111,5.BASE,5.INDEX,5.RD,00100,001010:SPECIAL3:32::LHX
  1879. "lhx r<RD>, r<INDEX>(r<BASE>)"
  1880. *dsp:
  1881. {
  1882. do_lxx (SD_, RD, BASE, INDEX, 1);
  1883. }
  1884. 011111,5.BASE,5.INDEX,5.RD,00000,001010:SPECIAL3:32::LWX
  1885. "lwx r<RD>, r<INDEX>(r<BASE>)"
  1886. *dsp:
  1887. {
  1888. do_lxx (SD_, RD, BASE, INDEX, 2);
  1889. }
  1890. 000001,00000,11100,16.OFFSET:REGIMM:32::BPOSGE32
  1891. "bposge32 <OFFSET>"
  1892. *dsp:
  1893. {
  1894. uint32_t pos = (DSPCR >> DSPCR_POS_SHIFT) & DSPCR_POS_MASK;
  1895. address_word offset = EXTEND16 (OFFSET) << 2;
  1896. if (pos >= 32)
  1897. {
  1898. DELAY_SLOT (NIA + offset);
  1899. }
  1900. }