arm_dyncom_interpreter.cpp 172 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578
  1. // Copyright 2012 Michael Kang, 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #define CITRA_IGNORE_EXIT(x)
  5. #include <algorithm>
  6. #include <cinttypes>
  7. #include <cstdio>
  8. #include "common/common_types.h"
  9. #include "common/logging/log.h"
  10. #include "common/microprofile.h"
  11. #include "core/arm/dyncom/arm_dyncom_dec.h"
  12. #include "core/arm/dyncom/arm_dyncom_interpreter.h"
  13. #include "core/arm/dyncom/arm_dyncom_run.h"
  14. #include "core/arm/dyncom/arm_dyncom_thumb.h"
  15. #include "core/arm/dyncom/arm_dyncom_trans.h"
  16. #include "core/arm/skyeye_common/armstate.h"
  17. #include "core/arm/skyeye_common/armsupp.h"
  18. #include "core/arm/skyeye_common/vfp/vfp.h"
  19. #include "core/gdbstub/gdbstub.h"
  20. #include "core/hle/svc.h"
  21. #include "core/memory.h"
  22. #define RM BITS(sht_oper, 0, 3)
  23. #define RS BITS(sht_oper, 8, 11)
  24. #define glue(x, y) x##y
  25. #define DPO(s) glue(DataProcessingOperands, s)
  26. #define ROTATE_RIGHT(n, i, l) ((n << (l - i)) | (n >> i))
  27. #define ROTATE_LEFT(n, i, l) ((n >> (l - i)) | (n << i))
  28. #define ROTATE_RIGHT_32(n, i) ROTATE_RIGHT(n, i, 32)
  29. #define ROTATE_LEFT_32(n, i) ROTATE_LEFT(n, i, 32)
  30. static bool CondPassed(const ARMul_State* cpu, unsigned int cond) {
  31. const bool n_flag = cpu->NFlag != 0;
  32. const bool z_flag = cpu->ZFlag != 0;
  33. const bool c_flag = cpu->CFlag != 0;
  34. const bool v_flag = cpu->VFlag != 0;
  35. switch (cond) {
  36. case ConditionCode::EQ:
  37. return z_flag;
  38. case ConditionCode::NE:
  39. return !z_flag;
  40. case ConditionCode::CS:
  41. return c_flag;
  42. case ConditionCode::CC:
  43. return !c_flag;
  44. case ConditionCode::MI:
  45. return n_flag;
  46. case ConditionCode::PL:
  47. return !n_flag;
  48. case ConditionCode::VS:
  49. return v_flag;
  50. case ConditionCode::VC:
  51. return !v_flag;
  52. case ConditionCode::HI:
  53. return (c_flag && !z_flag);
  54. case ConditionCode::LS:
  55. return (!c_flag || z_flag);
  56. case ConditionCode::GE:
  57. return (n_flag == v_flag);
  58. case ConditionCode::LT:
  59. return (n_flag != v_flag);
  60. case ConditionCode::GT:
  61. return (!z_flag && (n_flag == v_flag));
  62. case ConditionCode::LE:
  63. return (z_flag || (n_flag != v_flag));
  64. case ConditionCode::AL:
  65. case ConditionCode::NV: // Unconditional
  66. return true;
  67. }
  68. return false;
  69. }
  70. static unsigned int DPO(Immediate)(ARMul_State* cpu, unsigned int sht_oper) {
  71. unsigned int immed_8 = BITS(sht_oper, 0, 7);
  72. unsigned int rotate_imm = BITS(sht_oper, 8, 11);
  73. unsigned int shifter_operand = ROTATE_RIGHT_32(immed_8, rotate_imm * 2);
  74. if (rotate_imm == 0)
  75. cpu->shifter_carry_out = cpu->CFlag;
  76. else
  77. cpu->shifter_carry_out = BIT(shifter_operand, 31);
  78. return shifter_operand;
  79. }
  80. static unsigned int DPO(Register)(ARMul_State* cpu, unsigned int sht_oper) {
  81. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  82. unsigned int shifter_operand = rm;
  83. cpu->shifter_carry_out = cpu->CFlag;
  84. return shifter_operand;
  85. }
  86. static unsigned int DPO(LogicalShiftLeftByImmediate)(ARMul_State* cpu, unsigned int sht_oper) {
  87. int shift_imm = BITS(sht_oper, 7, 11);
  88. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  89. unsigned int shifter_operand;
  90. if (shift_imm == 0) {
  91. shifter_operand = rm;
  92. cpu->shifter_carry_out = cpu->CFlag;
  93. } else {
  94. shifter_operand = rm << shift_imm;
  95. cpu->shifter_carry_out = BIT(rm, 32 - shift_imm);
  96. }
  97. return shifter_operand;
  98. }
  99. static unsigned int DPO(LogicalShiftLeftByRegister)(ARMul_State* cpu, unsigned int sht_oper) {
  100. int shifter_operand;
  101. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  102. unsigned int rs = CHECK_READ_REG15(cpu, RS);
  103. if (BITS(rs, 0, 7) == 0) {
  104. shifter_operand = rm;
  105. cpu->shifter_carry_out = cpu->CFlag;
  106. } else if (BITS(rs, 0, 7) < 32) {
  107. shifter_operand = rm << BITS(rs, 0, 7);
  108. cpu->shifter_carry_out = BIT(rm, 32 - BITS(rs, 0, 7));
  109. } else if (BITS(rs, 0, 7) == 32) {
  110. shifter_operand = 0;
  111. cpu->shifter_carry_out = BIT(rm, 0);
  112. } else {
  113. shifter_operand = 0;
  114. cpu->shifter_carry_out = 0;
  115. }
  116. return shifter_operand;
  117. }
  118. static unsigned int DPO(LogicalShiftRightByImmediate)(ARMul_State* cpu, unsigned int sht_oper) {
  119. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  120. unsigned int shifter_operand;
  121. int shift_imm = BITS(sht_oper, 7, 11);
  122. if (shift_imm == 0) {
  123. shifter_operand = 0;
  124. cpu->shifter_carry_out = BIT(rm, 31);
  125. } else {
  126. shifter_operand = rm >> shift_imm;
  127. cpu->shifter_carry_out = BIT(rm, shift_imm - 1);
  128. }
  129. return shifter_operand;
  130. }
  131. static unsigned int DPO(LogicalShiftRightByRegister)(ARMul_State* cpu, unsigned int sht_oper) {
  132. unsigned int rs = CHECK_READ_REG15(cpu, RS);
  133. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  134. unsigned int shifter_operand;
  135. if (BITS(rs, 0, 7) == 0) {
  136. shifter_operand = rm;
  137. cpu->shifter_carry_out = cpu->CFlag;
  138. } else if (BITS(rs, 0, 7) < 32) {
  139. shifter_operand = rm >> BITS(rs, 0, 7);
  140. cpu->shifter_carry_out = BIT(rm, BITS(rs, 0, 7) - 1);
  141. } else if (BITS(rs, 0, 7) == 32) {
  142. shifter_operand = 0;
  143. cpu->shifter_carry_out = BIT(rm, 31);
  144. } else {
  145. shifter_operand = 0;
  146. cpu->shifter_carry_out = 0;
  147. }
  148. return shifter_operand;
  149. }
  150. static unsigned int DPO(ArithmeticShiftRightByImmediate)(ARMul_State* cpu, unsigned int sht_oper) {
  151. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  152. unsigned int shifter_operand;
  153. int shift_imm = BITS(sht_oper, 7, 11);
  154. if (shift_imm == 0) {
  155. if (BIT(rm, 31) == 0)
  156. shifter_operand = 0;
  157. else
  158. shifter_operand = 0xFFFFFFFF;
  159. cpu->shifter_carry_out = BIT(rm, 31);
  160. } else {
  161. shifter_operand = static_cast<int>(rm) >> shift_imm;
  162. cpu->shifter_carry_out = BIT(rm, shift_imm - 1);
  163. }
  164. return shifter_operand;
  165. }
  166. static unsigned int DPO(ArithmeticShiftRightByRegister)(ARMul_State* cpu, unsigned int sht_oper) {
  167. unsigned int rs = CHECK_READ_REG15(cpu, RS);
  168. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  169. unsigned int shifter_operand;
  170. if (BITS(rs, 0, 7) == 0) {
  171. shifter_operand = rm;
  172. cpu->shifter_carry_out = cpu->CFlag;
  173. } else if (BITS(rs, 0, 7) < 32) {
  174. shifter_operand = static_cast<int>(rm) >> BITS(rs, 0, 7);
  175. cpu->shifter_carry_out = BIT(rm, BITS(rs, 0, 7) - 1);
  176. } else {
  177. if (BIT(rm, 31) == 0)
  178. shifter_operand = 0;
  179. else
  180. shifter_operand = 0xffffffff;
  181. cpu->shifter_carry_out = BIT(rm, 31);
  182. }
  183. return shifter_operand;
  184. }
  185. static unsigned int DPO(RotateRightByImmediate)(ARMul_State* cpu, unsigned int sht_oper) {
  186. unsigned int shifter_operand;
  187. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  188. int shift_imm = BITS(sht_oper, 7, 11);
  189. if (shift_imm == 0) {
  190. shifter_operand = (cpu->CFlag << 31) | (rm >> 1);
  191. cpu->shifter_carry_out = BIT(rm, 0);
  192. } else {
  193. shifter_operand = ROTATE_RIGHT_32(rm, shift_imm);
  194. cpu->shifter_carry_out = BIT(rm, shift_imm - 1);
  195. }
  196. return shifter_operand;
  197. }
  198. static unsigned int DPO(RotateRightByRegister)(ARMul_State* cpu, unsigned int sht_oper) {
  199. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  200. unsigned int rs = CHECK_READ_REG15(cpu, RS);
  201. unsigned int shifter_operand;
  202. if (BITS(rs, 0, 7) == 0) {
  203. shifter_operand = rm;
  204. cpu->shifter_carry_out = cpu->CFlag;
  205. } else if (BITS(rs, 0, 4) == 0) {
  206. shifter_operand = rm;
  207. cpu->shifter_carry_out = BIT(rm, 31);
  208. } else {
  209. shifter_operand = ROTATE_RIGHT_32(rm, BITS(rs, 0, 4));
  210. cpu->shifter_carry_out = BIT(rm, BITS(rs, 0, 4) - 1);
  211. }
  212. return shifter_operand;
  213. }
  214. #define DEBUG_MSG \
  215. LOG_DEBUG(Core_ARM11, "inst is %x", inst); \
  216. CITRA_IGNORE_EXIT(0)
  217. #define LnSWoUB(s) glue(LnSWoUB, s)
  218. #define MLnS(s) glue(MLnS, s)
  219. #define LdnStM(s) glue(LdnStM, s)
  220. #define W_BIT BIT(inst, 21)
  221. #define U_BIT BIT(inst, 23)
  222. #define I_BIT BIT(inst, 25)
  223. #define P_BIT BIT(inst, 24)
  224. #define OFFSET_12 BITS(inst, 0, 11)
  225. static void LnSWoUB(ImmediateOffset)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr) {
  226. unsigned int Rn = BITS(inst, 16, 19);
  227. unsigned int addr;
  228. if (U_BIT)
  229. addr = CHECK_READ_REG15_WA(cpu, Rn) + OFFSET_12;
  230. else
  231. addr = CHECK_READ_REG15_WA(cpu, Rn) - OFFSET_12;
  232. virt_addr = addr;
  233. }
  234. static void LnSWoUB(RegisterOffset)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr) {
  235. unsigned int Rn = BITS(inst, 16, 19);
  236. unsigned int Rm = BITS(inst, 0, 3);
  237. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  238. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  239. unsigned int addr;
  240. if (U_BIT)
  241. addr = rn + rm;
  242. else
  243. addr = rn - rm;
  244. virt_addr = addr;
  245. }
  246. static void LnSWoUB(ImmediatePostIndexed)(ARMul_State* cpu, unsigned int inst,
  247. unsigned int& virt_addr) {
  248. unsigned int Rn = BITS(inst, 16, 19);
  249. unsigned int addr = CHECK_READ_REG15_WA(cpu, Rn);
  250. if (U_BIT)
  251. cpu->Reg[Rn] += OFFSET_12;
  252. else
  253. cpu->Reg[Rn] -= OFFSET_12;
  254. virt_addr = addr;
  255. }
  256. static void LnSWoUB(ImmediatePreIndexed)(ARMul_State* cpu, unsigned int inst,
  257. unsigned int& virt_addr) {
  258. unsigned int Rn = BITS(inst, 16, 19);
  259. unsigned int addr;
  260. if (U_BIT)
  261. addr = CHECK_READ_REG15_WA(cpu, Rn) + OFFSET_12;
  262. else
  263. addr = CHECK_READ_REG15_WA(cpu, Rn) - OFFSET_12;
  264. virt_addr = addr;
  265. if (CondPassed(cpu, BITS(inst, 28, 31)))
  266. cpu->Reg[Rn] = addr;
  267. }
  268. static void MLnS(RegisterPreIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr) {
  269. unsigned int addr;
  270. unsigned int Rn = BITS(inst, 16, 19);
  271. unsigned int Rm = BITS(inst, 0, 3);
  272. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  273. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  274. if (U_BIT)
  275. addr = rn + rm;
  276. else
  277. addr = rn - rm;
  278. virt_addr = addr;
  279. if (CondPassed(cpu, BITS(inst, 28, 31)))
  280. cpu->Reg[Rn] = addr;
  281. }
  282. static void LnSWoUB(RegisterPreIndexed)(ARMul_State* cpu, unsigned int inst,
  283. unsigned int& virt_addr) {
  284. unsigned int Rn = BITS(inst, 16, 19);
  285. unsigned int Rm = BITS(inst, 0, 3);
  286. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  287. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  288. unsigned int addr;
  289. if (U_BIT)
  290. addr = rn + rm;
  291. else
  292. addr = rn - rm;
  293. virt_addr = addr;
  294. if (CondPassed(cpu, BITS(inst, 28, 31))) {
  295. cpu->Reg[Rn] = addr;
  296. }
  297. }
  298. static void LnSWoUB(ScaledRegisterPreIndexed)(ARMul_State* cpu, unsigned int inst,
  299. unsigned int& virt_addr) {
  300. unsigned int shift = BITS(inst, 5, 6);
  301. unsigned int shift_imm = BITS(inst, 7, 11);
  302. unsigned int Rn = BITS(inst, 16, 19);
  303. unsigned int Rm = BITS(inst, 0, 3);
  304. unsigned int index = 0;
  305. unsigned int addr;
  306. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  307. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  308. switch (shift) {
  309. case 0:
  310. index = rm << shift_imm;
  311. break;
  312. case 1:
  313. if (shift_imm == 0) {
  314. index = 0;
  315. } else {
  316. index = rm >> shift_imm;
  317. }
  318. break;
  319. case 2:
  320. if (shift_imm == 0) { // ASR #32
  321. if (BIT(rm, 31) == 1)
  322. index = 0xFFFFFFFF;
  323. else
  324. index = 0;
  325. } else {
  326. index = static_cast<int>(rm) >> shift_imm;
  327. }
  328. break;
  329. case 3:
  330. if (shift_imm == 0) {
  331. index = (cpu->CFlag << 31) | (rm >> 1);
  332. } else {
  333. index = ROTATE_RIGHT_32(rm, shift_imm);
  334. }
  335. break;
  336. }
  337. if (U_BIT)
  338. addr = rn + index;
  339. else
  340. addr = rn - index;
  341. virt_addr = addr;
  342. if (CondPassed(cpu, BITS(inst, 28, 31)))
  343. cpu->Reg[Rn] = addr;
  344. }
  345. static void LnSWoUB(ScaledRegisterPostIndexed)(ARMul_State* cpu, unsigned int inst,
  346. unsigned int& virt_addr) {
  347. unsigned int shift = BITS(inst, 5, 6);
  348. unsigned int shift_imm = BITS(inst, 7, 11);
  349. unsigned int Rn = BITS(inst, 16, 19);
  350. unsigned int Rm = BITS(inst, 0, 3);
  351. unsigned int index = 0;
  352. unsigned int addr = CHECK_READ_REG15_WA(cpu, Rn);
  353. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  354. switch (shift) {
  355. case 0:
  356. index = rm << shift_imm;
  357. break;
  358. case 1:
  359. if (shift_imm == 0) {
  360. index = 0;
  361. } else {
  362. index = rm >> shift_imm;
  363. }
  364. break;
  365. case 2:
  366. if (shift_imm == 0) { // ASR #32
  367. if (BIT(rm, 31) == 1)
  368. index = 0xFFFFFFFF;
  369. else
  370. index = 0;
  371. } else {
  372. index = static_cast<int>(rm) >> shift_imm;
  373. }
  374. break;
  375. case 3:
  376. if (shift_imm == 0) {
  377. index = (cpu->CFlag << 31) | (rm >> 1);
  378. } else {
  379. index = ROTATE_RIGHT_32(rm, shift_imm);
  380. }
  381. break;
  382. }
  383. virt_addr = addr;
  384. if (CondPassed(cpu, BITS(inst, 28, 31))) {
  385. if (U_BIT)
  386. cpu->Reg[Rn] += index;
  387. else
  388. cpu->Reg[Rn] -= index;
  389. }
  390. }
  391. static void LnSWoUB(RegisterPostIndexed)(ARMul_State* cpu, unsigned int inst,
  392. unsigned int& virt_addr) {
  393. unsigned int Rn = BITS(inst, 16, 19);
  394. unsigned int Rm = BITS(inst, 0, 3);
  395. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  396. virt_addr = CHECK_READ_REG15_WA(cpu, Rn);
  397. if (CondPassed(cpu, BITS(inst, 28, 31))) {
  398. if (U_BIT) {
  399. cpu->Reg[Rn] += rm;
  400. } else {
  401. cpu->Reg[Rn] -= rm;
  402. }
  403. }
  404. }
  405. static void MLnS(ImmediateOffset)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr) {
  406. unsigned int immedL = BITS(inst, 0, 3);
  407. unsigned int immedH = BITS(inst, 8, 11);
  408. unsigned int Rn = BITS(inst, 16, 19);
  409. unsigned int addr;
  410. unsigned int offset_8 = (immedH << 4) | immedL;
  411. if (U_BIT)
  412. addr = CHECK_READ_REG15_WA(cpu, Rn) + offset_8;
  413. else
  414. addr = CHECK_READ_REG15_WA(cpu, Rn) - offset_8;
  415. virt_addr = addr;
  416. }
  417. static void MLnS(RegisterOffset)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr) {
  418. unsigned int addr;
  419. unsigned int Rn = BITS(inst, 16, 19);
  420. unsigned int Rm = BITS(inst, 0, 3);
  421. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  422. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  423. if (U_BIT)
  424. addr = rn + rm;
  425. else
  426. addr = rn - rm;
  427. virt_addr = addr;
  428. }
  429. static void MLnS(ImmediatePreIndexed)(ARMul_State* cpu, unsigned int inst,
  430. unsigned int& virt_addr) {
  431. unsigned int Rn = BITS(inst, 16, 19);
  432. unsigned int immedH = BITS(inst, 8, 11);
  433. unsigned int immedL = BITS(inst, 0, 3);
  434. unsigned int addr;
  435. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  436. unsigned int offset_8 = (immedH << 4) | immedL;
  437. if (U_BIT)
  438. addr = rn + offset_8;
  439. else
  440. addr = rn - offset_8;
  441. virt_addr = addr;
  442. if (CondPassed(cpu, BITS(inst, 28, 31)))
  443. cpu->Reg[Rn] = addr;
  444. }
  445. static void MLnS(ImmediatePostIndexed)(ARMul_State* cpu, unsigned int inst,
  446. unsigned int& virt_addr) {
  447. unsigned int Rn = BITS(inst, 16, 19);
  448. unsigned int immedH = BITS(inst, 8, 11);
  449. unsigned int immedL = BITS(inst, 0, 3);
  450. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  451. virt_addr = rn;
  452. if (CondPassed(cpu, BITS(inst, 28, 31))) {
  453. unsigned int offset_8 = (immedH << 4) | immedL;
  454. if (U_BIT)
  455. rn += offset_8;
  456. else
  457. rn -= offset_8;
  458. cpu->Reg[Rn] = rn;
  459. }
  460. }
  461. static void MLnS(RegisterPostIndexed)(ARMul_State* cpu, unsigned int inst,
  462. unsigned int& virt_addr) {
  463. unsigned int Rn = BITS(inst, 16, 19);
  464. unsigned int Rm = BITS(inst, 0, 3);
  465. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  466. virt_addr = CHECK_READ_REG15_WA(cpu, Rn);
  467. if (CondPassed(cpu, BITS(inst, 28, 31))) {
  468. if (U_BIT)
  469. cpu->Reg[Rn] += rm;
  470. else
  471. cpu->Reg[Rn] -= rm;
  472. }
  473. }
  474. static void LdnStM(DecrementBefore)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr) {
  475. unsigned int Rn = BITS(inst, 16, 19);
  476. unsigned int i = BITS(inst, 0, 15);
  477. int count = 0;
  478. while (i) {
  479. if (i & 1)
  480. count++;
  481. i = i >> 1;
  482. }
  483. virt_addr = CHECK_READ_REG15_WA(cpu, Rn) - count * 4;
  484. if (CondPassed(cpu, BITS(inst, 28, 31)) && BIT(inst, 21))
  485. cpu->Reg[Rn] -= count * 4;
  486. }
  487. static void LdnStM(IncrementBefore)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr) {
  488. unsigned int Rn = BITS(inst, 16, 19);
  489. unsigned int i = BITS(inst, 0, 15);
  490. int count = 0;
  491. while (i) {
  492. if (i & 1)
  493. count++;
  494. i = i >> 1;
  495. }
  496. virt_addr = CHECK_READ_REG15_WA(cpu, Rn) + 4;
  497. if (CondPassed(cpu, BITS(inst, 28, 31)) && BIT(inst, 21))
  498. cpu->Reg[Rn] += count * 4;
  499. }
  500. static void LdnStM(IncrementAfter)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr) {
  501. unsigned int Rn = BITS(inst, 16, 19);
  502. unsigned int i = BITS(inst, 0, 15);
  503. int count = 0;
  504. while (i) {
  505. if (i & 1)
  506. count++;
  507. i = i >> 1;
  508. }
  509. virt_addr = CHECK_READ_REG15_WA(cpu, Rn);
  510. if (CondPassed(cpu, BITS(inst, 28, 31)) && BIT(inst, 21))
  511. cpu->Reg[Rn] += count * 4;
  512. }
  513. static void LdnStM(DecrementAfter)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr) {
  514. unsigned int Rn = BITS(inst, 16, 19);
  515. unsigned int i = BITS(inst, 0, 15);
  516. int count = 0;
  517. while (i) {
  518. if (i & 1)
  519. count++;
  520. i = i >> 1;
  521. }
  522. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  523. unsigned int start_addr = rn - count * 4 + 4;
  524. virt_addr = start_addr;
  525. if (CondPassed(cpu, BITS(inst, 28, 31)) && BIT(inst, 21)) {
  526. cpu->Reg[Rn] -= count * 4;
  527. }
  528. }
  529. static void LnSWoUB(ScaledRegisterOffset)(ARMul_State* cpu, unsigned int inst,
  530. unsigned int& virt_addr) {
  531. unsigned int shift = BITS(inst, 5, 6);
  532. unsigned int shift_imm = BITS(inst, 7, 11);
  533. unsigned int Rn = BITS(inst, 16, 19);
  534. unsigned int Rm = BITS(inst, 0, 3);
  535. unsigned int index = 0;
  536. unsigned int addr;
  537. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  538. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  539. switch (shift) {
  540. case 0:
  541. index = rm << shift_imm;
  542. break;
  543. case 1:
  544. if (shift_imm == 0) {
  545. index = 0;
  546. } else {
  547. index = rm >> shift_imm;
  548. }
  549. break;
  550. case 2:
  551. if (shift_imm == 0) { // ASR #32
  552. if (BIT(rm, 31) == 1)
  553. index = 0xFFFFFFFF;
  554. else
  555. index = 0;
  556. } else {
  557. index = static_cast<int>(rm) >> shift_imm;
  558. }
  559. break;
  560. case 3:
  561. if (shift_imm == 0) {
  562. index = (cpu->CFlag << 31) | (rm >> 1);
  563. } else {
  564. index = ROTATE_RIGHT_32(rm, shift_imm);
  565. }
  566. break;
  567. }
  568. if (U_BIT) {
  569. addr = rn + index;
  570. } else
  571. addr = rn - index;
  572. virt_addr = addr;
  573. }
  574. shtop_fp_t GetShifterOp(unsigned int inst) {
  575. if (BIT(inst, 25)) {
  576. return DPO(Immediate);
  577. } else if (BITS(inst, 4, 11) == 0) {
  578. return DPO(Register);
  579. } else if (BITS(inst, 4, 6) == 0) {
  580. return DPO(LogicalShiftLeftByImmediate);
  581. } else if (BITS(inst, 4, 7) == 1) {
  582. return DPO(LogicalShiftLeftByRegister);
  583. } else if (BITS(inst, 4, 6) == 2) {
  584. return DPO(LogicalShiftRightByImmediate);
  585. } else if (BITS(inst, 4, 7) == 3) {
  586. return DPO(LogicalShiftRightByRegister);
  587. } else if (BITS(inst, 4, 6) == 4) {
  588. return DPO(ArithmeticShiftRightByImmediate);
  589. } else if (BITS(inst, 4, 7) == 5) {
  590. return DPO(ArithmeticShiftRightByRegister);
  591. } else if (BITS(inst, 4, 6) == 6) {
  592. return DPO(RotateRightByImmediate);
  593. } else if (BITS(inst, 4, 7) == 7) {
  594. return DPO(RotateRightByRegister);
  595. }
  596. return nullptr;
  597. }
  598. get_addr_fp_t GetAddressingOp(unsigned int inst) {
  599. if (BITS(inst, 24, 27) == 5 && BIT(inst, 21) == 0) {
  600. return LnSWoUB(ImmediateOffset);
  601. } else if (BITS(inst, 24, 27) == 7 && BIT(inst, 21) == 0 && BITS(inst, 4, 11) == 0) {
  602. return LnSWoUB(RegisterOffset);
  603. } else if (BITS(inst, 24, 27) == 7 && BIT(inst, 21) == 0 && BIT(inst, 4) == 0) {
  604. return LnSWoUB(ScaledRegisterOffset);
  605. } else if (BITS(inst, 24, 27) == 5 && BIT(inst, 21) == 1) {
  606. return LnSWoUB(ImmediatePreIndexed);
  607. } else if (BITS(inst, 24, 27) == 7 && BIT(inst, 21) == 1 && BITS(inst, 4, 11) == 0) {
  608. return LnSWoUB(RegisterPreIndexed);
  609. } else if (BITS(inst, 24, 27) == 7 && BIT(inst, 21) == 1 && BIT(inst, 4) == 0) {
  610. return LnSWoUB(ScaledRegisterPreIndexed);
  611. } else if (BITS(inst, 24, 27) == 4 && BIT(inst, 21) == 0) {
  612. return LnSWoUB(ImmediatePostIndexed);
  613. } else if (BITS(inst, 24, 27) == 6 && BIT(inst, 21) == 0 && BITS(inst, 4, 11) == 0) {
  614. return LnSWoUB(RegisterPostIndexed);
  615. } else if (BITS(inst, 24, 27) == 6 && BIT(inst, 21) == 0 && BIT(inst, 4) == 0) {
  616. return LnSWoUB(ScaledRegisterPostIndexed);
  617. } else if (BITS(inst, 24, 27) == 1 && BITS(inst, 21, 22) == 2 && BIT(inst, 7) == 1 &&
  618. BIT(inst, 4) == 1) {
  619. return MLnS(ImmediateOffset);
  620. } else if (BITS(inst, 24, 27) == 1 && BITS(inst, 21, 22) == 0 && BIT(inst, 7) == 1 &&
  621. BIT(inst, 4) == 1) {
  622. return MLnS(RegisterOffset);
  623. } else if (BITS(inst, 24, 27) == 1 && BITS(inst, 21, 22) == 3 && BIT(inst, 7) == 1 &&
  624. BIT(inst, 4) == 1) {
  625. return MLnS(ImmediatePreIndexed);
  626. } else if (BITS(inst, 24, 27) == 1 && BITS(inst, 21, 22) == 1 && BIT(inst, 7) == 1 &&
  627. BIT(inst, 4) == 1) {
  628. return MLnS(RegisterPreIndexed);
  629. } else if (BITS(inst, 24, 27) == 0 && BITS(inst, 21, 22) == 2 && BIT(inst, 7) == 1 &&
  630. BIT(inst, 4) == 1) {
  631. return MLnS(ImmediatePostIndexed);
  632. } else if (BITS(inst, 24, 27) == 0 && BITS(inst, 21, 22) == 0 && BIT(inst, 7) == 1 &&
  633. BIT(inst, 4) == 1) {
  634. return MLnS(RegisterPostIndexed);
  635. } else if (BITS(inst, 23, 27) == 0x11) {
  636. return LdnStM(IncrementAfter);
  637. } else if (BITS(inst, 23, 27) == 0x13) {
  638. return LdnStM(IncrementBefore);
  639. } else if (BITS(inst, 23, 27) == 0x10) {
  640. return LdnStM(DecrementAfter);
  641. } else if (BITS(inst, 23, 27) == 0x12) {
  642. return LdnStM(DecrementBefore);
  643. }
  644. return nullptr;
  645. }
  646. // Specialized for LDRT, LDRBT, STRT, and STRBT, which have specific addressing mode requirements
  647. get_addr_fp_t GetAddressingOpLoadStoreT(unsigned int inst) {
  648. if (BITS(inst, 25, 27) == 2) {
  649. return LnSWoUB(ImmediatePostIndexed);
  650. } else if (BITS(inst, 25, 27) == 3) {
  651. return LnSWoUB(ScaledRegisterPostIndexed);
  652. }
  653. // Reaching this would indicate the thumb version
  654. // of this instruction, however the 3DS CPU doesn't
  655. // support this variant (the 3DS CPU is only ARMv6K,
  656. // while this variant is added in ARMv6T2).
  657. // So it's sufficient for citra to not implement this.
  658. return nullptr;
  659. }
  660. enum { FETCH_SUCCESS, FETCH_FAILURE };
  661. static ThumbDecodeStatus DecodeThumbInstruction(u32 inst, u32 addr, u32* arm_inst, u32* inst_size,
  662. ARM_INST_PTR* ptr_inst_base) {
  663. // Check if in Thumb mode
  664. ThumbDecodeStatus ret = TranslateThumbInstruction(addr, inst, arm_inst, inst_size);
  665. if (ret == ThumbDecodeStatus::BRANCH) {
  666. int inst_index;
  667. int table_length = static_cast<int>(arm_instruction_trans_len);
  668. u32 tinstr = GetThumbInstruction(inst, addr);
  669. switch ((tinstr & 0xF800) >> 11) {
  670. case 26:
  671. case 27:
  672. if (((tinstr & 0x0F00) != 0x0E00) && ((tinstr & 0x0F00) != 0x0F00)) {
  673. inst_index = table_length - 4;
  674. *ptr_inst_base = arm_instruction_trans[inst_index](tinstr, inst_index);
  675. } else {
  676. LOG_ERROR(Core_ARM11, "thumb decoder error");
  677. }
  678. break;
  679. case 28:
  680. // Branch 2, unconditional branch
  681. inst_index = table_length - 5;
  682. *ptr_inst_base = arm_instruction_trans[inst_index](tinstr, inst_index);
  683. break;
  684. case 8:
  685. case 29:
  686. // For BLX 1 thumb instruction
  687. inst_index = table_length - 1;
  688. *ptr_inst_base = arm_instruction_trans[inst_index](tinstr, inst_index);
  689. break;
  690. case 30:
  691. // For BL 1 thumb instruction
  692. inst_index = table_length - 3;
  693. *ptr_inst_base = arm_instruction_trans[inst_index](tinstr, inst_index);
  694. break;
  695. case 31:
  696. // For BL 2 thumb instruction
  697. inst_index = table_length - 2;
  698. *ptr_inst_base = arm_instruction_trans[inst_index](tinstr, inst_index);
  699. break;
  700. default:
  701. ret = ThumbDecodeStatus::UNDEFINED;
  702. break;
  703. }
  704. }
  705. return ret;
  706. }
  707. enum { KEEP_GOING, FETCH_EXCEPTION };
  708. MICROPROFILE_DEFINE(DynCom_Decode, "DynCom", "Decode", MP_RGB(255, 64, 64));
  709. static unsigned int InterpreterTranslateInstruction(const ARMul_State* cpu, const u32 phys_addr,
  710. ARM_INST_PTR& inst_base) {
  711. u32 inst_size = 4;
  712. u32 inst = Memory::Read32(phys_addr & 0xFFFFFFFC);
  713. // If we are in Thumb mode, we'll translate one Thumb instruction to the corresponding ARM
  714. // instruction
  715. if (cpu->TFlag) {
  716. u32 arm_inst;
  717. ThumbDecodeStatus state =
  718. DecodeThumbInstruction(inst, phys_addr, &arm_inst, &inst_size, &inst_base);
  719. // We have translated the Thumb branch instruction in the Thumb decoder
  720. if (state == ThumbDecodeStatus::BRANCH) {
  721. return inst_size;
  722. }
  723. inst = arm_inst;
  724. }
  725. int idx;
  726. if (DecodeARMInstruction(inst, &idx) == ARMDecodeStatus::FAILURE) {
  727. LOG_ERROR(Core_ARM11, "Decode failure.\tPC: [0x%08" PRIX32 "]\tInstruction: %08" PRIX32,
  728. phys_addr, inst);
  729. LOG_ERROR(Core_ARM11, "cpsr=0x%" PRIX32 ", cpu->TFlag=%d, r15=0x%08" PRIX32, cpu->Cpsr,
  730. cpu->TFlag, cpu->Reg[15]);
  731. CITRA_IGNORE_EXIT(-1);
  732. }
  733. inst_base = arm_instruction_trans[idx](inst, idx);
  734. return inst_size;
  735. }
  736. static int InterpreterTranslateBlock(ARMul_State* cpu, std::size_t& bb_start, u32 addr) {
  737. MICROPROFILE_SCOPE(DynCom_Decode);
  738. // Decode instruction, get index
  739. // Allocate memory and init InsCream
  740. // Go on next, until terminal instruction
  741. // Save start addr of basicblock in CreamCache
  742. ARM_INST_PTR inst_base = nullptr;
  743. TransExtData ret = TransExtData::NON_BRANCH;
  744. int size = 0; // instruction size of basic block
  745. bb_start = trans_cache_buf_top;
  746. u32 phys_addr = addr;
  747. u32 pc_start = cpu->Reg[15];
  748. while (ret == TransExtData::NON_BRANCH) {
  749. unsigned int inst_size = InterpreterTranslateInstruction(cpu, phys_addr, inst_base);
  750. size++;
  751. phys_addr += inst_size;
  752. if ((phys_addr & 0xfff) == 0) {
  753. inst_base->br = TransExtData::END_OF_PAGE;
  754. }
  755. ret = inst_base->br;
  756. };
  757. cpu->instruction_cache[pc_start] = bb_start;
  758. return KEEP_GOING;
  759. }
  760. static int InterpreterTranslateSingle(ARMul_State* cpu, std::size_t& bb_start, u32 addr) {
  761. MICROPROFILE_SCOPE(DynCom_Decode);
  762. ARM_INST_PTR inst_base = nullptr;
  763. bb_start = trans_cache_buf_top;
  764. u32 phys_addr = addr;
  765. u32 pc_start = cpu->Reg[15];
  766. InterpreterTranslateInstruction(cpu, phys_addr, inst_base);
  767. if (inst_base->br == TransExtData::NON_BRANCH) {
  768. inst_base->br = TransExtData::SINGLE_STEP;
  769. }
  770. cpu->instruction_cache[pc_start] = bb_start;
  771. return KEEP_GOING;
  772. }
  773. static int clz(unsigned int x) {
  774. int n;
  775. if (x == 0)
  776. return (32);
  777. n = 1;
  778. if ((x >> 16) == 0) {
  779. n = n + 16;
  780. x = x << 16;
  781. }
  782. if ((x >> 24) == 0) {
  783. n = n + 8;
  784. x = x << 8;
  785. }
  786. if ((x >> 28) == 0) {
  787. n = n + 4;
  788. x = x << 4;
  789. }
  790. if ((x >> 30) == 0) {
  791. n = n + 2;
  792. x = x << 2;
  793. }
  794. n = n - (x >> 31);
  795. return n;
  796. }
  797. MICROPROFILE_DEFINE(DynCom_Execute, "DynCom", "Execute", MP_RGB(255, 0, 0));
  798. unsigned InterpreterMainLoop(ARMul_State* cpu) {
  799. MICROPROFILE_SCOPE(DynCom_Execute);
  800. GDBStub::BreakpointAddress breakpoint_data;
  801. #undef RM
  802. #undef RS
  803. #define CRn inst_cream->crn
  804. #define OPCODE_1 inst_cream->opcode_1
  805. #define OPCODE_2 inst_cream->opcode_2
  806. #define CRm inst_cream->crm
  807. #define RD cpu->Reg[inst_cream->Rd]
  808. #define RD2 cpu->Reg[inst_cream->Rd + 1]
  809. #define RN cpu->Reg[inst_cream->Rn]
  810. #define RM cpu->Reg[inst_cream->Rm]
  811. #define RS cpu->Reg[inst_cream->Rs]
  812. #define RDHI cpu->Reg[inst_cream->RdHi]
  813. #define RDLO cpu->Reg[inst_cream->RdLo]
  814. #define LINK_RTN_ADDR (cpu->Reg[14] = cpu->Reg[15] + 4)
  815. #define SET_PC (cpu->Reg[15] = cpu->Reg[15] + 8 + inst_cream->signed_immed_24)
  816. #define SHIFTER_OPERAND inst_cream->shtop_func(cpu, inst_cream->shifter_operand)
  817. #define FETCH_INST \
  818. if (inst_base->br != TransExtData::NON_BRANCH) \
  819. goto DISPATCH; \
  820. inst_base = (arm_inst*)&trans_cache_buf[ptr]
  821. #define INC_PC(l) ptr += sizeof(arm_inst) + l
  822. #define INC_PC_STUB ptr += sizeof(arm_inst)
  823. #define GDB_BP_CHECK \
  824. cpu->Cpsr &= ~(1 << 5); \
  825. cpu->Cpsr |= cpu->TFlag << 5; \
  826. if (GDBStub::IsServerEnabled()) { \
  827. if (GDBStub::IsMemoryBreak() || (breakpoint_data.type != GDBStub::BreakpointType::None && \
  828. PC == breakpoint_data.address)) { \
  829. GDBStub::Break(); \
  830. goto END; \
  831. } \
  832. }
  833. // GCC and Clang have a C++ extension to support a lookup table of labels. Otherwise, fallback to a
  834. // clunky switch statement.
  835. #if defined __GNUC__ || defined __clang__
  836. #define GOTO_NEXT_INST \
  837. GDB_BP_CHECK; \
  838. if (num_instrs >= cpu->NumInstrsToExecute) \
  839. goto END; \
  840. num_instrs++; \
  841. goto* InstLabel[inst_base->idx]
  842. #else
  843. #define GOTO_NEXT_INST \
  844. GDB_BP_CHECK; \
  845. if (num_instrs >= cpu->NumInstrsToExecute) \
  846. goto END; \
  847. num_instrs++; \
  848. switch (inst_base->idx) { \
  849. case 0: \
  850. goto VMLA_INST; \
  851. case 1: \
  852. goto VMLS_INST; \
  853. case 2: \
  854. goto VNMLA_INST; \
  855. case 3: \
  856. goto VNMLS_INST; \
  857. case 4: \
  858. goto VNMUL_INST; \
  859. case 5: \
  860. goto VMUL_INST; \
  861. case 6: \
  862. goto VADD_INST; \
  863. case 7: \
  864. goto VSUB_INST; \
  865. case 8: \
  866. goto VDIV_INST; \
  867. case 9: \
  868. goto VMOVI_INST; \
  869. case 10: \
  870. goto VMOVR_INST; \
  871. case 11: \
  872. goto VABS_INST; \
  873. case 12: \
  874. goto VNEG_INST; \
  875. case 13: \
  876. goto VSQRT_INST; \
  877. case 14: \
  878. goto VCMP_INST; \
  879. case 15: \
  880. goto VCMP2_INST; \
  881. case 16: \
  882. goto VCVTBDS_INST; \
  883. case 17: \
  884. goto VCVTBFF_INST; \
  885. case 18: \
  886. goto VCVTBFI_INST; \
  887. case 19: \
  888. goto VMOVBRS_INST; \
  889. case 20: \
  890. goto VMSR_INST; \
  891. case 21: \
  892. goto VMOVBRC_INST; \
  893. case 22: \
  894. goto VMRS_INST; \
  895. case 23: \
  896. goto VMOVBCR_INST; \
  897. case 24: \
  898. goto VMOVBRRSS_INST; \
  899. case 25: \
  900. goto VMOVBRRD_INST; \
  901. case 26: \
  902. goto VSTR_INST; \
  903. case 27: \
  904. goto VPUSH_INST; \
  905. case 28: \
  906. goto VSTM_INST; \
  907. case 29: \
  908. goto VPOP_INST; \
  909. case 30: \
  910. goto VLDR_INST; \
  911. case 31: \
  912. goto VLDM_INST; \
  913. case 32: \
  914. goto SRS_INST; \
  915. case 33: \
  916. goto RFE_INST; \
  917. case 34: \
  918. goto BKPT_INST; \
  919. case 35: \
  920. goto BLX_INST; \
  921. case 36: \
  922. goto CPS_INST; \
  923. case 37: \
  924. goto PLD_INST; \
  925. case 38: \
  926. goto SETEND_INST; \
  927. case 39: \
  928. goto CLREX_INST; \
  929. case 40: \
  930. goto REV16_INST; \
  931. case 41: \
  932. goto USAD8_INST; \
  933. case 42: \
  934. goto SXTB_INST; \
  935. case 43: \
  936. goto UXTB_INST; \
  937. case 44: \
  938. goto SXTH_INST; \
  939. case 45: \
  940. goto SXTB16_INST; \
  941. case 46: \
  942. goto UXTH_INST; \
  943. case 47: \
  944. goto UXTB16_INST; \
  945. case 48: \
  946. goto CPY_INST; \
  947. case 49: \
  948. goto UXTAB_INST; \
  949. case 50: \
  950. goto SSUB8_INST; \
  951. case 51: \
  952. goto SHSUB8_INST; \
  953. case 52: \
  954. goto SSUBADDX_INST; \
  955. case 53: \
  956. goto STREX_INST; \
  957. case 54: \
  958. goto STREXB_INST; \
  959. case 55: \
  960. goto SWP_INST; \
  961. case 56: \
  962. goto SWPB_INST; \
  963. case 57: \
  964. goto SSUB16_INST; \
  965. case 58: \
  966. goto SSAT16_INST; \
  967. case 59: \
  968. goto SHSUBADDX_INST; \
  969. case 60: \
  970. goto QSUBADDX_INST; \
  971. case 61: \
  972. goto SHADDSUBX_INST; \
  973. case 62: \
  974. goto SHADD8_INST; \
  975. case 63: \
  976. goto SHADD16_INST; \
  977. case 64: \
  978. goto SEL_INST; \
  979. case 65: \
  980. goto SADDSUBX_INST; \
  981. case 66: \
  982. goto SADD8_INST; \
  983. case 67: \
  984. goto SADD16_INST; \
  985. case 68: \
  986. goto SHSUB16_INST; \
  987. case 69: \
  988. goto UMAAL_INST; \
  989. case 70: \
  990. goto UXTAB16_INST; \
  991. case 71: \
  992. goto USUBADDX_INST; \
  993. case 72: \
  994. goto USUB8_INST; \
  995. case 73: \
  996. goto USUB16_INST; \
  997. case 74: \
  998. goto USAT16_INST; \
  999. case 75: \
  1000. goto USADA8_INST; \
  1001. case 76: \
  1002. goto UQSUBADDX_INST; \
  1003. case 77: \
  1004. goto UQSUB8_INST; \
  1005. case 78: \
  1006. goto UQSUB16_INST; \
  1007. case 79: \
  1008. goto UQADDSUBX_INST; \
  1009. case 80: \
  1010. goto UQADD8_INST; \
  1011. case 81: \
  1012. goto UQADD16_INST; \
  1013. case 82: \
  1014. goto SXTAB_INST; \
  1015. case 83: \
  1016. goto UHSUBADDX_INST; \
  1017. case 84: \
  1018. goto UHSUB8_INST; \
  1019. case 85: \
  1020. goto UHSUB16_INST; \
  1021. case 86: \
  1022. goto UHADDSUBX_INST; \
  1023. case 87: \
  1024. goto UHADD8_INST; \
  1025. case 88: \
  1026. goto UHADD16_INST; \
  1027. case 89: \
  1028. goto UADDSUBX_INST; \
  1029. case 90: \
  1030. goto UADD8_INST; \
  1031. case 91: \
  1032. goto UADD16_INST; \
  1033. case 92: \
  1034. goto SXTAH_INST; \
  1035. case 93: \
  1036. goto SXTAB16_INST; \
  1037. case 94: \
  1038. goto QADD8_INST; \
  1039. case 95: \
  1040. goto BXJ_INST; \
  1041. case 96: \
  1042. goto CLZ_INST; \
  1043. case 97: \
  1044. goto UXTAH_INST; \
  1045. case 98: \
  1046. goto BX_INST; \
  1047. case 99: \
  1048. goto REV_INST; \
  1049. case 100: \
  1050. goto BLX_INST; \
  1051. case 101: \
  1052. goto REVSH_INST; \
  1053. case 102: \
  1054. goto QADD_INST; \
  1055. case 103: \
  1056. goto QADD16_INST; \
  1057. case 104: \
  1058. goto QADDSUBX_INST; \
  1059. case 105: \
  1060. goto LDREX_INST; \
  1061. case 106: \
  1062. goto QDADD_INST; \
  1063. case 107: \
  1064. goto QDSUB_INST; \
  1065. case 108: \
  1066. goto QSUB_INST; \
  1067. case 109: \
  1068. goto LDREXB_INST; \
  1069. case 110: \
  1070. goto QSUB8_INST; \
  1071. case 111: \
  1072. goto QSUB16_INST; \
  1073. case 112: \
  1074. goto SMUAD_INST; \
  1075. case 113: \
  1076. goto SMMUL_INST; \
  1077. case 114: \
  1078. goto SMUSD_INST; \
  1079. case 115: \
  1080. goto SMLSD_INST; \
  1081. case 116: \
  1082. goto SMLSLD_INST; \
  1083. case 117: \
  1084. goto SMMLA_INST; \
  1085. case 118: \
  1086. goto SMMLS_INST; \
  1087. case 119: \
  1088. goto SMLALD_INST; \
  1089. case 120: \
  1090. goto SMLAD_INST; \
  1091. case 121: \
  1092. goto SMLAW_INST; \
  1093. case 122: \
  1094. goto SMULW_INST; \
  1095. case 123: \
  1096. goto PKHTB_INST; \
  1097. case 124: \
  1098. goto PKHBT_INST; \
  1099. case 125: \
  1100. goto SMUL_INST; \
  1101. case 126: \
  1102. goto SMLALXY_INST; \
  1103. case 127: \
  1104. goto SMLA_INST; \
  1105. case 128: \
  1106. goto MCRR_INST; \
  1107. case 129: \
  1108. goto MRRC_INST; \
  1109. case 130: \
  1110. goto CMP_INST; \
  1111. case 131: \
  1112. goto TST_INST; \
  1113. case 132: \
  1114. goto TEQ_INST; \
  1115. case 133: \
  1116. goto CMN_INST; \
  1117. case 134: \
  1118. goto SMULL_INST; \
  1119. case 135: \
  1120. goto UMULL_INST; \
  1121. case 136: \
  1122. goto UMLAL_INST; \
  1123. case 137: \
  1124. goto SMLAL_INST; \
  1125. case 138: \
  1126. goto MUL_INST; \
  1127. case 139: \
  1128. goto MLA_INST; \
  1129. case 140: \
  1130. goto SSAT_INST; \
  1131. case 141: \
  1132. goto USAT_INST; \
  1133. case 142: \
  1134. goto MRS_INST; \
  1135. case 143: \
  1136. goto MSR_INST; \
  1137. case 144: \
  1138. goto AND_INST; \
  1139. case 145: \
  1140. goto BIC_INST; \
  1141. case 146: \
  1142. goto LDM_INST; \
  1143. case 147: \
  1144. goto EOR_INST; \
  1145. case 148: \
  1146. goto ADD_INST; \
  1147. case 149: \
  1148. goto RSB_INST; \
  1149. case 150: \
  1150. goto RSC_INST; \
  1151. case 151: \
  1152. goto SBC_INST; \
  1153. case 152: \
  1154. goto ADC_INST; \
  1155. case 153: \
  1156. goto SUB_INST; \
  1157. case 154: \
  1158. goto ORR_INST; \
  1159. case 155: \
  1160. goto MVN_INST; \
  1161. case 156: \
  1162. goto MOV_INST; \
  1163. case 157: \
  1164. goto STM_INST; \
  1165. case 158: \
  1166. goto LDM_INST; \
  1167. case 159: \
  1168. goto LDRSH_INST; \
  1169. case 160: \
  1170. goto STM_INST; \
  1171. case 161: \
  1172. goto LDM_INST; \
  1173. case 162: \
  1174. goto LDRSB_INST; \
  1175. case 163: \
  1176. goto STRD_INST; \
  1177. case 164: \
  1178. goto LDRH_INST; \
  1179. case 165: \
  1180. goto STRH_INST; \
  1181. case 166: \
  1182. goto LDRD_INST; \
  1183. case 167: \
  1184. goto STRT_INST; \
  1185. case 168: \
  1186. goto STRBT_INST; \
  1187. case 169: \
  1188. goto LDRBT_INST; \
  1189. case 170: \
  1190. goto LDRT_INST; \
  1191. case 171: \
  1192. goto MRC_INST; \
  1193. case 172: \
  1194. goto MCR_INST; \
  1195. case 173: \
  1196. goto MSR_INST; \
  1197. case 174: \
  1198. goto MSR_INST; \
  1199. case 175: \
  1200. goto MSR_INST; \
  1201. case 176: \
  1202. goto MSR_INST; \
  1203. case 177: \
  1204. goto MSR_INST; \
  1205. case 178: \
  1206. goto LDRB_INST; \
  1207. case 179: \
  1208. goto STRB_INST; \
  1209. case 180: \
  1210. goto LDR_INST; \
  1211. case 181: \
  1212. goto LDRCOND_INST; \
  1213. case 182: \
  1214. goto STR_INST; \
  1215. case 183: \
  1216. goto CDP_INST; \
  1217. case 184: \
  1218. goto STC_INST; \
  1219. case 185: \
  1220. goto LDC_INST; \
  1221. case 186: \
  1222. goto LDREXD_INST; \
  1223. case 187: \
  1224. goto STREXD_INST; \
  1225. case 188: \
  1226. goto LDREXH_INST; \
  1227. case 189: \
  1228. goto STREXH_INST; \
  1229. case 190: \
  1230. goto NOP_INST; \
  1231. case 191: \
  1232. goto YIELD_INST; \
  1233. case 192: \
  1234. goto WFE_INST; \
  1235. case 193: \
  1236. goto WFI_INST; \
  1237. case 194: \
  1238. goto SEV_INST; \
  1239. case 195: \
  1240. goto SWI_INST; \
  1241. case 196: \
  1242. goto BBL_INST; \
  1243. case 197: \
  1244. goto B_2_THUMB; \
  1245. case 198: \
  1246. goto B_COND_THUMB; \
  1247. case 199: \
  1248. goto BL_1_THUMB; \
  1249. case 200: \
  1250. goto BL_2_THUMB; \
  1251. case 201: \
  1252. goto BLX_1_THUMB; \
  1253. case 202: \
  1254. goto DISPATCH; \
  1255. case 203: \
  1256. goto INIT_INST_LENGTH; \
  1257. case 204: \
  1258. goto END; \
  1259. }
  1260. #endif
  1261. #define UPDATE_NFLAG(dst) (cpu->NFlag = BIT(dst, 31) ? 1 : 0)
  1262. #define UPDATE_ZFLAG(dst) (cpu->ZFlag = dst ? 0 : 1)
  1263. #define UPDATE_CFLAG_WITH_SC (cpu->CFlag = cpu->shifter_carry_out)
  1264. #define SAVE_NZCVT \
  1265. cpu->Cpsr = (cpu->Cpsr & 0x0fffffdf) | (cpu->NFlag << 31) | (cpu->ZFlag << 30) | \
  1266. (cpu->CFlag << 29) | (cpu->VFlag << 28) | (cpu->TFlag << 5)
  1267. #define LOAD_NZCVT \
  1268. cpu->NFlag = (cpu->Cpsr >> 31); \
  1269. cpu->ZFlag = (cpu->Cpsr >> 30) & 1; \
  1270. cpu->CFlag = (cpu->Cpsr >> 29) & 1; \
  1271. cpu->VFlag = (cpu->Cpsr >> 28) & 1; \
  1272. cpu->TFlag = (cpu->Cpsr >> 5) & 1;
  1273. #define CurrentModeHasSPSR (cpu->Mode != SYSTEM32MODE) && (cpu->Mode != USER32MODE)
  1274. #define PC (cpu->Reg[15])
  1275. // GCC and Clang have a C++ extension to support a lookup table of labels. Otherwise, fallback
  1276. // to a clunky switch statement.
  1277. #if defined __GNUC__ || defined __clang__
  1278. void* InstLabel[] = {&&VMLA_INST,
  1279. &&VMLS_INST,
  1280. &&VNMLA_INST,
  1281. &&VNMLS_INST,
  1282. &&VNMUL_INST,
  1283. &&VMUL_INST,
  1284. &&VADD_INST,
  1285. &&VSUB_INST,
  1286. &&VDIV_INST,
  1287. &&VMOVI_INST,
  1288. &&VMOVR_INST,
  1289. &&VABS_INST,
  1290. &&VNEG_INST,
  1291. &&VSQRT_INST,
  1292. &&VCMP_INST,
  1293. &&VCMP2_INST,
  1294. &&VCVTBDS_INST,
  1295. &&VCVTBFF_INST,
  1296. &&VCVTBFI_INST,
  1297. &&VMOVBRS_INST,
  1298. &&VMSR_INST,
  1299. &&VMOVBRC_INST,
  1300. &&VMRS_INST,
  1301. &&VMOVBCR_INST,
  1302. &&VMOVBRRSS_INST,
  1303. &&VMOVBRRD_INST,
  1304. &&VSTR_INST,
  1305. &&VPUSH_INST,
  1306. &&VSTM_INST,
  1307. &&VPOP_INST,
  1308. &&VLDR_INST,
  1309. &&VLDM_INST,
  1310. &&SRS_INST,
  1311. &&RFE_INST,
  1312. &&BKPT_INST,
  1313. &&BLX_INST,
  1314. &&CPS_INST,
  1315. &&PLD_INST,
  1316. &&SETEND_INST,
  1317. &&CLREX_INST,
  1318. &&REV16_INST,
  1319. &&USAD8_INST,
  1320. &&SXTB_INST,
  1321. &&UXTB_INST,
  1322. &&SXTH_INST,
  1323. &&SXTB16_INST,
  1324. &&UXTH_INST,
  1325. &&UXTB16_INST,
  1326. &&CPY_INST,
  1327. &&UXTAB_INST,
  1328. &&SSUB8_INST,
  1329. &&SHSUB8_INST,
  1330. &&SSUBADDX_INST,
  1331. &&STREX_INST,
  1332. &&STREXB_INST,
  1333. &&SWP_INST,
  1334. &&SWPB_INST,
  1335. &&SSUB16_INST,
  1336. &&SSAT16_INST,
  1337. &&SHSUBADDX_INST,
  1338. &&QSUBADDX_INST,
  1339. &&SHADDSUBX_INST,
  1340. &&SHADD8_INST,
  1341. &&SHADD16_INST,
  1342. &&SEL_INST,
  1343. &&SADDSUBX_INST,
  1344. &&SADD8_INST,
  1345. &&SADD16_INST,
  1346. &&SHSUB16_INST,
  1347. &&UMAAL_INST,
  1348. &&UXTAB16_INST,
  1349. &&USUBADDX_INST,
  1350. &&USUB8_INST,
  1351. &&USUB16_INST,
  1352. &&USAT16_INST,
  1353. &&USADA8_INST,
  1354. &&UQSUBADDX_INST,
  1355. &&UQSUB8_INST,
  1356. &&UQSUB16_INST,
  1357. &&UQADDSUBX_INST,
  1358. &&UQADD8_INST,
  1359. &&UQADD16_INST,
  1360. &&SXTAB_INST,
  1361. &&UHSUBADDX_INST,
  1362. &&UHSUB8_INST,
  1363. &&UHSUB16_INST,
  1364. &&UHADDSUBX_INST,
  1365. &&UHADD8_INST,
  1366. &&UHADD16_INST,
  1367. &&UADDSUBX_INST,
  1368. &&UADD8_INST,
  1369. &&UADD16_INST,
  1370. &&SXTAH_INST,
  1371. &&SXTAB16_INST,
  1372. &&QADD8_INST,
  1373. &&BXJ_INST,
  1374. &&CLZ_INST,
  1375. &&UXTAH_INST,
  1376. &&BX_INST,
  1377. &&REV_INST,
  1378. &&BLX_INST,
  1379. &&REVSH_INST,
  1380. &&QADD_INST,
  1381. &&QADD16_INST,
  1382. &&QADDSUBX_INST,
  1383. &&LDREX_INST,
  1384. &&QDADD_INST,
  1385. &&QDSUB_INST,
  1386. &&QSUB_INST,
  1387. &&LDREXB_INST,
  1388. &&QSUB8_INST,
  1389. &&QSUB16_INST,
  1390. &&SMUAD_INST,
  1391. &&SMMUL_INST,
  1392. &&SMUSD_INST,
  1393. &&SMLSD_INST,
  1394. &&SMLSLD_INST,
  1395. &&SMMLA_INST,
  1396. &&SMMLS_INST,
  1397. &&SMLALD_INST,
  1398. &&SMLAD_INST,
  1399. &&SMLAW_INST,
  1400. &&SMULW_INST,
  1401. &&PKHTB_INST,
  1402. &&PKHBT_INST,
  1403. &&SMUL_INST,
  1404. &&SMLALXY_INST,
  1405. &&SMLA_INST,
  1406. &&MCRR_INST,
  1407. &&MRRC_INST,
  1408. &&CMP_INST,
  1409. &&TST_INST,
  1410. &&TEQ_INST,
  1411. &&CMN_INST,
  1412. &&SMULL_INST,
  1413. &&UMULL_INST,
  1414. &&UMLAL_INST,
  1415. &&SMLAL_INST,
  1416. &&MUL_INST,
  1417. &&MLA_INST,
  1418. &&SSAT_INST,
  1419. &&USAT_INST,
  1420. &&MRS_INST,
  1421. &&MSR_INST,
  1422. &&AND_INST,
  1423. &&BIC_INST,
  1424. &&LDM_INST,
  1425. &&EOR_INST,
  1426. &&ADD_INST,
  1427. &&RSB_INST,
  1428. &&RSC_INST,
  1429. &&SBC_INST,
  1430. &&ADC_INST,
  1431. &&SUB_INST,
  1432. &&ORR_INST,
  1433. &&MVN_INST,
  1434. &&MOV_INST,
  1435. &&STM_INST,
  1436. &&LDM_INST,
  1437. &&LDRSH_INST,
  1438. &&STM_INST,
  1439. &&LDM_INST,
  1440. &&LDRSB_INST,
  1441. &&STRD_INST,
  1442. &&LDRH_INST,
  1443. &&STRH_INST,
  1444. &&LDRD_INST,
  1445. &&STRT_INST,
  1446. &&STRBT_INST,
  1447. &&LDRBT_INST,
  1448. &&LDRT_INST,
  1449. &&MRC_INST,
  1450. &&MCR_INST,
  1451. &&MSR_INST,
  1452. &&MSR_INST,
  1453. &&MSR_INST,
  1454. &&MSR_INST,
  1455. &&MSR_INST,
  1456. &&LDRB_INST,
  1457. &&STRB_INST,
  1458. &&LDR_INST,
  1459. &&LDRCOND_INST,
  1460. &&STR_INST,
  1461. &&CDP_INST,
  1462. &&STC_INST,
  1463. &&LDC_INST,
  1464. &&LDREXD_INST,
  1465. &&STREXD_INST,
  1466. &&LDREXH_INST,
  1467. &&STREXH_INST,
  1468. &&NOP_INST,
  1469. &&YIELD_INST,
  1470. &&WFE_INST,
  1471. &&WFI_INST,
  1472. &&SEV_INST,
  1473. &&SWI_INST,
  1474. &&BBL_INST,
  1475. &&B_2_THUMB,
  1476. &&B_COND_THUMB,
  1477. &&BL_1_THUMB,
  1478. &&BL_2_THUMB,
  1479. &&BLX_1_THUMB,
  1480. &&DISPATCH,
  1481. &&INIT_INST_LENGTH,
  1482. &&END};
  1483. #endif
  1484. arm_inst* inst_base;
  1485. unsigned int addr;
  1486. unsigned int num_instrs = 0;
  1487. std::size_t ptr;
  1488. LOAD_NZCVT;
  1489. DISPATCH : {
  1490. if (!cpu->NirqSig) {
  1491. if (!(cpu->Cpsr & 0x80)) {
  1492. goto END;
  1493. }
  1494. }
  1495. if (cpu->TFlag)
  1496. cpu->Reg[15] &= 0xfffffffe;
  1497. else
  1498. cpu->Reg[15] &= 0xfffffffc;
  1499. // Find the cached instruction cream, otherwise translate it...
  1500. auto itr = cpu->instruction_cache.find(cpu->Reg[15]);
  1501. if (itr != cpu->instruction_cache.end()) {
  1502. ptr = itr->second;
  1503. } else if (cpu->NumInstrsToExecute != 1) {
  1504. if (InterpreterTranslateBlock(cpu, ptr, cpu->Reg[15]) == FETCH_EXCEPTION)
  1505. goto END;
  1506. } else {
  1507. if (InterpreterTranslateSingle(cpu, ptr, cpu->Reg[15]) == FETCH_EXCEPTION)
  1508. goto END;
  1509. }
  1510. // Find breakpoint if one exists within the block
  1511. if (GDBStub::IsConnected()) {
  1512. breakpoint_data =
  1513. GDBStub::GetNextBreakpointFromAddress(cpu->Reg[15], GDBStub::BreakpointType::Execute);
  1514. }
  1515. inst_base = (arm_inst*)&trans_cache_buf[ptr];
  1516. GOTO_NEXT_INST;
  1517. }
  1518. ADC_INST : {
  1519. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1520. adc_inst* const inst_cream = (adc_inst*)inst_base->component;
  1521. u32 rn_val = RN;
  1522. if (inst_cream->Rn == 15)
  1523. rn_val += 2 * cpu->GetInstructionSize();
  1524. bool carry;
  1525. bool overflow;
  1526. RD = AddWithCarry(rn_val, SHIFTER_OPERAND, cpu->CFlag, &carry, &overflow);
  1527. if (inst_cream->S && (inst_cream->Rd == 15)) {
  1528. if (CurrentModeHasSPSR) {
  1529. cpu->Cpsr = cpu->Spsr_copy;
  1530. cpu->ChangePrivilegeMode(cpu->Spsr_copy & 0x1F);
  1531. LOAD_NZCVT;
  1532. }
  1533. } else if (inst_cream->S) {
  1534. UPDATE_NFLAG(RD);
  1535. UPDATE_ZFLAG(RD);
  1536. cpu->CFlag = carry;
  1537. cpu->VFlag = overflow;
  1538. }
  1539. if (inst_cream->Rd == 15) {
  1540. INC_PC(sizeof(adc_inst));
  1541. goto DISPATCH;
  1542. }
  1543. }
  1544. cpu->Reg[15] += cpu->GetInstructionSize();
  1545. INC_PC(sizeof(adc_inst));
  1546. FETCH_INST;
  1547. GOTO_NEXT_INST;
  1548. }
  1549. ADD_INST : {
  1550. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1551. add_inst* const inst_cream = (add_inst*)inst_base->component;
  1552. u32 rn_val = CHECK_READ_REG15_WA(cpu, inst_cream->Rn);
  1553. bool carry;
  1554. bool overflow;
  1555. RD = AddWithCarry(rn_val, SHIFTER_OPERAND, 0, &carry, &overflow);
  1556. if (inst_cream->S && (inst_cream->Rd == 15)) {
  1557. if (CurrentModeHasSPSR) {
  1558. cpu->Cpsr = cpu->Spsr_copy;
  1559. cpu->ChangePrivilegeMode(cpu->Cpsr & 0x1F);
  1560. LOAD_NZCVT;
  1561. }
  1562. } else if (inst_cream->S) {
  1563. UPDATE_NFLAG(RD);
  1564. UPDATE_ZFLAG(RD);
  1565. cpu->CFlag = carry;
  1566. cpu->VFlag = overflow;
  1567. }
  1568. if (inst_cream->Rd == 15) {
  1569. INC_PC(sizeof(add_inst));
  1570. goto DISPATCH;
  1571. }
  1572. }
  1573. cpu->Reg[15] += cpu->GetInstructionSize();
  1574. INC_PC(sizeof(add_inst));
  1575. FETCH_INST;
  1576. GOTO_NEXT_INST;
  1577. }
  1578. AND_INST : {
  1579. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1580. and_inst* const inst_cream = (and_inst*)inst_base->component;
  1581. u32 lop = RN;
  1582. u32 rop = SHIFTER_OPERAND;
  1583. if (inst_cream->Rn == 15)
  1584. lop += 2 * cpu->GetInstructionSize();
  1585. RD = lop & rop;
  1586. if (inst_cream->S && (inst_cream->Rd == 15)) {
  1587. if (CurrentModeHasSPSR) {
  1588. cpu->Cpsr = cpu->Spsr_copy;
  1589. cpu->ChangePrivilegeMode(cpu->Cpsr & 0x1F);
  1590. LOAD_NZCVT;
  1591. }
  1592. } else if (inst_cream->S) {
  1593. UPDATE_NFLAG(RD);
  1594. UPDATE_ZFLAG(RD);
  1595. UPDATE_CFLAG_WITH_SC;
  1596. }
  1597. if (inst_cream->Rd == 15) {
  1598. INC_PC(sizeof(and_inst));
  1599. goto DISPATCH;
  1600. }
  1601. }
  1602. cpu->Reg[15] += cpu->GetInstructionSize();
  1603. INC_PC(sizeof(and_inst));
  1604. FETCH_INST;
  1605. GOTO_NEXT_INST;
  1606. }
  1607. BBL_INST : {
  1608. if ((inst_base->cond == ConditionCode::AL) || CondPassed(cpu, inst_base->cond)) {
  1609. bbl_inst* inst_cream = (bbl_inst*)inst_base->component;
  1610. if (inst_cream->L) {
  1611. LINK_RTN_ADDR;
  1612. }
  1613. SET_PC;
  1614. INC_PC(sizeof(bbl_inst));
  1615. goto DISPATCH;
  1616. }
  1617. cpu->Reg[15] += cpu->GetInstructionSize();
  1618. INC_PC(sizeof(bbl_inst));
  1619. goto DISPATCH;
  1620. }
  1621. BIC_INST : {
  1622. bic_inst* inst_cream = (bic_inst*)inst_base->component;
  1623. if ((inst_base->cond == ConditionCode::AL) || CondPassed(cpu, inst_base->cond)) {
  1624. u32 lop = RN;
  1625. if (inst_cream->Rn == 15) {
  1626. lop += 2 * cpu->GetInstructionSize();
  1627. }
  1628. u32 rop = SHIFTER_OPERAND;
  1629. RD = lop & (~rop);
  1630. if ((inst_cream->S) && (inst_cream->Rd == 15)) {
  1631. if (CurrentModeHasSPSR) {
  1632. cpu->Cpsr = cpu->Spsr_copy;
  1633. cpu->ChangePrivilegeMode(cpu->Spsr_copy & 0x1F);
  1634. LOAD_NZCVT;
  1635. }
  1636. } else if (inst_cream->S) {
  1637. UPDATE_NFLAG(RD);
  1638. UPDATE_ZFLAG(RD);
  1639. UPDATE_CFLAG_WITH_SC;
  1640. }
  1641. if (inst_cream->Rd == 15) {
  1642. INC_PC(sizeof(bic_inst));
  1643. goto DISPATCH;
  1644. }
  1645. }
  1646. cpu->Reg[15] += cpu->GetInstructionSize();
  1647. INC_PC(sizeof(bic_inst));
  1648. FETCH_INST;
  1649. GOTO_NEXT_INST;
  1650. }
  1651. BKPT_INST : {
  1652. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1653. bkpt_inst* const inst_cream = (bkpt_inst*)inst_base->component;
  1654. LOG_DEBUG(Core_ARM11, "Breakpoint instruction hit. Immediate: 0x%08X", inst_cream->imm);
  1655. }
  1656. cpu->Reg[15] += cpu->GetInstructionSize();
  1657. INC_PC(sizeof(bkpt_inst));
  1658. FETCH_INST;
  1659. GOTO_NEXT_INST;
  1660. }
  1661. BLX_INST : {
  1662. blx_inst* inst_cream = (blx_inst*)inst_base->component;
  1663. if ((inst_base->cond == ConditionCode::AL) || CondPassed(cpu, inst_base->cond)) {
  1664. unsigned int inst = inst_cream->inst;
  1665. if (BITS(inst, 20, 27) == 0x12 && BITS(inst, 4, 7) == 0x3) {
  1666. const u32 jump_address = cpu->Reg[inst_cream->val.Rm];
  1667. cpu->Reg[14] = (cpu->Reg[15] + cpu->GetInstructionSize());
  1668. if (cpu->TFlag)
  1669. cpu->Reg[14] |= 0x1;
  1670. cpu->Reg[15] = jump_address & 0xfffffffe;
  1671. cpu->TFlag = jump_address & 0x1;
  1672. } else {
  1673. cpu->Reg[14] = (cpu->Reg[15] + cpu->GetInstructionSize());
  1674. cpu->TFlag = 0x1;
  1675. int signed_int = inst_cream->val.signed_immed_24;
  1676. signed_int = (signed_int & 0x800000) ? (0x3F000000 | signed_int) : signed_int;
  1677. signed_int = signed_int << 2;
  1678. cpu->Reg[15] = cpu->Reg[15] + 8 + signed_int + (BIT(inst, 24) << 1);
  1679. }
  1680. INC_PC(sizeof(blx_inst));
  1681. goto DISPATCH;
  1682. }
  1683. cpu->Reg[15] += cpu->GetInstructionSize();
  1684. INC_PC(sizeof(blx_inst));
  1685. goto DISPATCH;
  1686. }
  1687. BX_INST:
  1688. BXJ_INST : {
  1689. // Note that only the 'fail' case of BXJ is emulated. This is because
  1690. // the facilities for Jazelle emulation are not implemented.
  1691. //
  1692. // According to the ARM documentation on BXJ, if setting the J bit in the APSR
  1693. // fails, then BXJ functions identically like a regular BX instruction.
  1694. //
  1695. // This is sufficient for citra, as the CPU for the 3DS does not implement Jazelle.
  1696. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1697. bx_inst* const inst_cream = (bx_inst*)inst_base->component;
  1698. u32 address = RM;
  1699. if (inst_cream->Rm == 15)
  1700. address += 2 * cpu->GetInstructionSize();
  1701. cpu->TFlag = address & 1;
  1702. cpu->Reg[15] = address & 0xfffffffe;
  1703. INC_PC(sizeof(bx_inst));
  1704. goto DISPATCH;
  1705. }
  1706. cpu->Reg[15] += cpu->GetInstructionSize();
  1707. INC_PC(sizeof(bx_inst));
  1708. goto DISPATCH;
  1709. }
  1710. CDP_INST : {
  1711. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1712. // Undefined instruction here
  1713. cpu->NumInstrsToExecute = 0;
  1714. return num_instrs;
  1715. }
  1716. cpu->Reg[15] += cpu->GetInstructionSize();
  1717. INC_PC(sizeof(cdp_inst));
  1718. FETCH_INST;
  1719. GOTO_NEXT_INST;
  1720. }
  1721. CLREX_INST : {
  1722. cpu->UnsetExclusiveMemoryAddress();
  1723. cpu->Reg[15] += cpu->GetInstructionSize();
  1724. INC_PC(sizeof(clrex_inst));
  1725. FETCH_INST;
  1726. GOTO_NEXT_INST;
  1727. }
  1728. CLZ_INST : {
  1729. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1730. clz_inst* inst_cream = (clz_inst*)inst_base->component;
  1731. RD = clz(RM);
  1732. }
  1733. cpu->Reg[15] += cpu->GetInstructionSize();
  1734. INC_PC(sizeof(clz_inst));
  1735. FETCH_INST;
  1736. GOTO_NEXT_INST;
  1737. }
  1738. CMN_INST : {
  1739. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1740. cmn_inst* const inst_cream = (cmn_inst*)inst_base->component;
  1741. u32 rn_val = RN;
  1742. if (inst_cream->Rn == 15)
  1743. rn_val += 2 * cpu->GetInstructionSize();
  1744. bool carry;
  1745. bool overflow;
  1746. u32 result = AddWithCarry(rn_val, SHIFTER_OPERAND, 0, &carry, &overflow);
  1747. UPDATE_NFLAG(result);
  1748. UPDATE_ZFLAG(result);
  1749. cpu->CFlag = carry;
  1750. cpu->VFlag = overflow;
  1751. }
  1752. cpu->Reg[15] += cpu->GetInstructionSize();
  1753. INC_PC(sizeof(cmn_inst));
  1754. FETCH_INST;
  1755. GOTO_NEXT_INST;
  1756. }
  1757. CMP_INST : {
  1758. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1759. cmp_inst* const inst_cream = (cmp_inst*)inst_base->component;
  1760. u32 rn_val = RN;
  1761. if (inst_cream->Rn == 15)
  1762. rn_val += 2 * cpu->GetInstructionSize();
  1763. bool carry;
  1764. bool overflow;
  1765. u32 result = AddWithCarry(rn_val, ~SHIFTER_OPERAND, 1, &carry, &overflow);
  1766. UPDATE_NFLAG(result);
  1767. UPDATE_ZFLAG(result);
  1768. cpu->CFlag = carry;
  1769. cpu->VFlag = overflow;
  1770. }
  1771. cpu->Reg[15] += cpu->GetInstructionSize();
  1772. INC_PC(sizeof(cmp_inst));
  1773. FETCH_INST;
  1774. GOTO_NEXT_INST;
  1775. }
  1776. CPS_INST : {
  1777. cps_inst* inst_cream = (cps_inst*)inst_base->component;
  1778. u32 aif_val = 0;
  1779. u32 aif_mask = 0;
  1780. if (cpu->InAPrivilegedMode()) {
  1781. if (inst_cream->imod1) {
  1782. if (inst_cream->A) {
  1783. aif_val |= (inst_cream->imod0 << 8);
  1784. aif_mask |= 1 << 8;
  1785. }
  1786. if (inst_cream->I) {
  1787. aif_val |= (inst_cream->imod0 << 7);
  1788. aif_mask |= 1 << 7;
  1789. }
  1790. if (inst_cream->F) {
  1791. aif_val |= (inst_cream->imod0 << 6);
  1792. aif_mask |= 1 << 6;
  1793. }
  1794. aif_mask = ~aif_mask;
  1795. cpu->Cpsr = (cpu->Cpsr & aif_mask) | aif_val;
  1796. }
  1797. if (inst_cream->mmod) {
  1798. cpu->Cpsr = (cpu->Cpsr & 0xffffffe0) | inst_cream->mode;
  1799. cpu->ChangePrivilegeMode(inst_cream->mode);
  1800. }
  1801. }
  1802. cpu->Reg[15] += cpu->GetInstructionSize();
  1803. INC_PC(sizeof(cps_inst));
  1804. FETCH_INST;
  1805. GOTO_NEXT_INST;
  1806. }
  1807. CPY_INST : {
  1808. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1809. mov_inst* inst_cream = (mov_inst*)inst_base->component;
  1810. RD = SHIFTER_OPERAND;
  1811. if (inst_cream->Rd == 15) {
  1812. INC_PC(sizeof(mov_inst));
  1813. goto DISPATCH;
  1814. }
  1815. }
  1816. cpu->Reg[15] += cpu->GetInstructionSize();
  1817. INC_PC(sizeof(mov_inst));
  1818. FETCH_INST;
  1819. GOTO_NEXT_INST;
  1820. }
  1821. EOR_INST : {
  1822. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1823. eor_inst* inst_cream = (eor_inst*)inst_base->component;
  1824. u32 lop = RN;
  1825. if (inst_cream->Rn == 15) {
  1826. lop += 2 * cpu->GetInstructionSize();
  1827. }
  1828. u32 rop = SHIFTER_OPERAND;
  1829. RD = lop ^ rop;
  1830. if (inst_cream->S && (inst_cream->Rd == 15)) {
  1831. if (CurrentModeHasSPSR) {
  1832. cpu->Cpsr = cpu->Spsr_copy;
  1833. cpu->ChangePrivilegeMode(cpu->Spsr_copy & 0x1F);
  1834. LOAD_NZCVT;
  1835. }
  1836. } else if (inst_cream->S) {
  1837. UPDATE_NFLAG(RD);
  1838. UPDATE_ZFLAG(RD);
  1839. UPDATE_CFLAG_WITH_SC;
  1840. }
  1841. if (inst_cream->Rd == 15) {
  1842. INC_PC(sizeof(eor_inst));
  1843. goto DISPATCH;
  1844. }
  1845. }
  1846. cpu->Reg[15] += cpu->GetInstructionSize();
  1847. INC_PC(sizeof(eor_inst));
  1848. FETCH_INST;
  1849. GOTO_NEXT_INST;
  1850. }
  1851. LDC_INST : {
  1852. // Instruction not implemented
  1853. // LOG_CRITICAL(Core_ARM11, "unimplemented instruction");
  1854. cpu->Reg[15] += cpu->GetInstructionSize();
  1855. INC_PC(sizeof(ldc_inst));
  1856. FETCH_INST;
  1857. GOTO_NEXT_INST;
  1858. }
  1859. LDM_INST : {
  1860. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1861. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  1862. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  1863. unsigned int inst = inst_cream->inst;
  1864. if (BIT(inst, 22) && !BIT(inst, 15)) {
  1865. for (int i = 0; i < 13; i++) {
  1866. if (BIT(inst, i)) {
  1867. cpu->Reg[i] = cpu->ReadMemory32(addr);
  1868. addr += 4;
  1869. }
  1870. }
  1871. if (BIT(inst, 13)) {
  1872. if (cpu->Mode == USER32MODE)
  1873. cpu->Reg[13] = cpu->ReadMemory32(addr);
  1874. else
  1875. cpu->Reg_usr[0] = cpu->ReadMemory32(addr);
  1876. addr += 4;
  1877. }
  1878. if (BIT(inst, 14)) {
  1879. if (cpu->Mode == USER32MODE)
  1880. cpu->Reg[14] = cpu->ReadMemory32(addr);
  1881. else
  1882. cpu->Reg_usr[1] = cpu->ReadMemory32(addr);
  1883. addr += 4;
  1884. }
  1885. } else if (!BIT(inst, 22)) {
  1886. for (int i = 0; i < 16; i++) {
  1887. if (BIT(inst, i)) {
  1888. unsigned int ret = cpu->ReadMemory32(addr);
  1889. // For armv5t, should enter thumb when bits[0] is non-zero.
  1890. if (i == 15) {
  1891. cpu->TFlag = ret & 0x1;
  1892. ret &= 0xFFFFFFFE;
  1893. }
  1894. cpu->Reg[i] = ret;
  1895. addr += 4;
  1896. }
  1897. }
  1898. } else if (BIT(inst, 22) && BIT(inst, 15)) {
  1899. for (int i = 0; i < 15; i++) {
  1900. if (BIT(inst, i)) {
  1901. cpu->Reg[i] = cpu->ReadMemory32(addr);
  1902. addr += 4;
  1903. }
  1904. }
  1905. if (CurrentModeHasSPSR) {
  1906. cpu->Cpsr = cpu->Spsr_copy;
  1907. cpu->ChangePrivilegeMode(cpu->Cpsr & 0x1F);
  1908. LOAD_NZCVT;
  1909. }
  1910. cpu->Reg[15] = cpu->ReadMemory32(addr);
  1911. }
  1912. if (BIT(inst, 15)) {
  1913. INC_PC(sizeof(ldst_inst));
  1914. goto DISPATCH;
  1915. }
  1916. }
  1917. cpu->Reg[15] += cpu->GetInstructionSize();
  1918. INC_PC(sizeof(ldst_inst));
  1919. FETCH_INST;
  1920. GOTO_NEXT_INST;
  1921. }
  1922. SXTH_INST : {
  1923. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1924. sxth_inst* inst_cream = (sxth_inst*)inst_base->component;
  1925. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate);
  1926. if (BIT(operand2, 15)) {
  1927. operand2 |= 0xffff0000;
  1928. } else {
  1929. operand2 &= 0xffff;
  1930. }
  1931. RD = operand2;
  1932. }
  1933. cpu->Reg[15] += cpu->GetInstructionSize();
  1934. INC_PC(sizeof(sxth_inst));
  1935. FETCH_INST;
  1936. GOTO_NEXT_INST;
  1937. }
  1938. LDR_INST : {
  1939. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  1940. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  1941. unsigned int value = cpu->ReadMemory32(addr);
  1942. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = value;
  1943. if (BITS(inst_cream->inst, 12, 15) == 15) {
  1944. // For armv5t, should enter thumb when bits[0] is non-zero.
  1945. cpu->TFlag = value & 0x1;
  1946. cpu->Reg[15] &= 0xFFFFFFFE;
  1947. INC_PC(sizeof(ldst_inst));
  1948. goto DISPATCH;
  1949. }
  1950. cpu->Reg[15] += cpu->GetInstructionSize();
  1951. INC_PC(sizeof(ldst_inst));
  1952. FETCH_INST;
  1953. GOTO_NEXT_INST;
  1954. }
  1955. LDRCOND_INST : {
  1956. if (CondPassed(cpu, inst_base->cond)) {
  1957. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  1958. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  1959. unsigned int value = cpu->ReadMemory32(addr);
  1960. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = value;
  1961. if (BITS(inst_cream->inst, 12, 15) == 15) {
  1962. // For armv5t, should enter thumb when bits[0] is non-zero.
  1963. cpu->TFlag = value & 0x1;
  1964. cpu->Reg[15] &= 0xFFFFFFFE;
  1965. INC_PC(sizeof(ldst_inst));
  1966. goto DISPATCH;
  1967. }
  1968. }
  1969. cpu->Reg[15] += cpu->GetInstructionSize();
  1970. INC_PC(sizeof(ldst_inst));
  1971. FETCH_INST;
  1972. GOTO_NEXT_INST;
  1973. }
  1974. UXTH_INST : {
  1975. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1976. uxth_inst* inst_cream = (uxth_inst*)inst_base->component;
  1977. RD = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xffff;
  1978. }
  1979. cpu->Reg[15] += cpu->GetInstructionSize();
  1980. INC_PC(sizeof(uxth_inst));
  1981. FETCH_INST;
  1982. GOTO_NEXT_INST;
  1983. }
  1984. UXTAH_INST : {
  1985. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1986. uxtah_inst* inst_cream = (uxtah_inst*)inst_base->component;
  1987. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xffff;
  1988. RD = RN + operand2;
  1989. }
  1990. cpu->Reg[15] += cpu->GetInstructionSize();
  1991. INC_PC(sizeof(uxtah_inst));
  1992. FETCH_INST;
  1993. GOTO_NEXT_INST;
  1994. }
  1995. LDRB_INST : {
  1996. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  1997. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  1998. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  1999. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = cpu->ReadMemory8(addr);
  2000. }
  2001. cpu->Reg[15] += cpu->GetInstructionSize();
  2002. INC_PC(sizeof(ldst_inst));
  2003. FETCH_INST;
  2004. GOTO_NEXT_INST;
  2005. }
  2006. LDRBT_INST : {
  2007. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2008. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  2009. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  2010. const u32 dest_index = BITS(inst_cream->inst, 12, 15);
  2011. const u32 previous_mode = cpu->Mode;
  2012. cpu->ChangePrivilegeMode(USER32MODE);
  2013. const u8 value = cpu->ReadMemory8(addr);
  2014. cpu->ChangePrivilegeMode(previous_mode);
  2015. cpu->Reg[dest_index] = value;
  2016. }
  2017. cpu->Reg[15] += cpu->GetInstructionSize();
  2018. INC_PC(sizeof(ldst_inst));
  2019. FETCH_INST;
  2020. GOTO_NEXT_INST;
  2021. }
  2022. LDRD_INST : {
  2023. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2024. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  2025. // Should check if RD is even-numbered, Rd != 14, addr[0:1] == 0, (CP15_reg1_U == 1 ||
  2026. // addr[2] == 0)
  2027. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  2028. // The 3DS doesn't have LPAE (Large Physical Access Extension), so it
  2029. // wouldn't do this as a single read.
  2030. cpu->Reg[BITS(inst_cream->inst, 12, 15) + 0] = cpu->ReadMemory32(addr);
  2031. cpu->Reg[BITS(inst_cream->inst, 12, 15) + 1] = cpu->ReadMemory32(addr + 4);
  2032. // No dispatch since this operation should not modify R15
  2033. }
  2034. cpu->Reg[15] += 4;
  2035. INC_PC(sizeof(ldst_inst));
  2036. FETCH_INST;
  2037. GOTO_NEXT_INST;
  2038. }
  2039. LDREX_INST : {
  2040. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2041. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  2042. unsigned int read_addr = RN;
  2043. cpu->SetExclusiveMemoryAddress(read_addr);
  2044. RD = cpu->ReadMemory32(read_addr);
  2045. }
  2046. cpu->Reg[15] += cpu->GetInstructionSize();
  2047. INC_PC(sizeof(generic_arm_inst));
  2048. FETCH_INST;
  2049. GOTO_NEXT_INST;
  2050. }
  2051. LDREXB_INST : {
  2052. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2053. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  2054. unsigned int read_addr = RN;
  2055. cpu->SetExclusiveMemoryAddress(read_addr);
  2056. RD = cpu->ReadMemory8(read_addr);
  2057. }
  2058. cpu->Reg[15] += cpu->GetInstructionSize();
  2059. INC_PC(sizeof(generic_arm_inst));
  2060. FETCH_INST;
  2061. GOTO_NEXT_INST;
  2062. }
  2063. LDREXH_INST : {
  2064. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2065. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  2066. unsigned int read_addr = RN;
  2067. cpu->SetExclusiveMemoryAddress(read_addr);
  2068. RD = cpu->ReadMemory16(read_addr);
  2069. }
  2070. cpu->Reg[15] += cpu->GetInstructionSize();
  2071. INC_PC(sizeof(generic_arm_inst));
  2072. FETCH_INST;
  2073. GOTO_NEXT_INST;
  2074. }
  2075. LDREXD_INST : {
  2076. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2077. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  2078. unsigned int read_addr = RN;
  2079. cpu->SetExclusiveMemoryAddress(read_addr);
  2080. RD = cpu->ReadMemory32(read_addr);
  2081. RD2 = cpu->ReadMemory32(read_addr + 4);
  2082. }
  2083. cpu->Reg[15] += cpu->GetInstructionSize();
  2084. INC_PC(sizeof(generic_arm_inst));
  2085. FETCH_INST;
  2086. GOTO_NEXT_INST;
  2087. }
  2088. LDRH_INST : {
  2089. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2090. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  2091. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  2092. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = cpu->ReadMemory16(addr);
  2093. }
  2094. cpu->Reg[15] += cpu->GetInstructionSize();
  2095. INC_PC(sizeof(ldst_inst));
  2096. FETCH_INST;
  2097. GOTO_NEXT_INST;
  2098. }
  2099. LDRSB_INST : {
  2100. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2101. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  2102. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  2103. unsigned int value = cpu->ReadMemory8(addr);
  2104. if (BIT(value, 7)) {
  2105. value |= 0xffffff00;
  2106. }
  2107. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = value;
  2108. }
  2109. cpu->Reg[15] += cpu->GetInstructionSize();
  2110. INC_PC(sizeof(ldst_inst));
  2111. FETCH_INST;
  2112. GOTO_NEXT_INST;
  2113. }
  2114. LDRSH_INST : {
  2115. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2116. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  2117. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  2118. unsigned int value = cpu->ReadMemory16(addr);
  2119. if (BIT(value, 15)) {
  2120. value |= 0xffff0000;
  2121. }
  2122. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = value;
  2123. }
  2124. cpu->Reg[15] += cpu->GetInstructionSize();
  2125. INC_PC(sizeof(ldst_inst));
  2126. FETCH_INST;
  2127. GOTO_NEXT_INST;
  2128. }
  2129. LDRT_INST : {
  2130. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2131. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  2132. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  2133. const u32 dest_index = BITS(inst_cream->inst, 12, 15);
  2134. const u32 previous_mode = cpu->Mode;
  2135. cpu->ChangePrivilegeMode(USER32MODE);
  2136. const u32 value = cpu->ReadMemory32(addr);
  2137. cpu->ChangePrivilegeMode(previous_mode);
  2138. cpu->Reg[dest_index] = value;
  2139. }
  2140. cpu->Reg[15] += cpu->GetInstructionSize();
  2141. INC_PC(sizeof(ldst_inst));
  2142. FETCH_INST;
  2143. GOTO_NEXT_INST;
  2144. }
  2145. MCR_INST : {
  2146. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2147. mcr_inst* inst_cream = (mcr_inst*)inst_base->component;
  2148. unsigned int inst = inst_cream->inst;
  2149. if (inst_cream->Rd == 15) {
  2150. DEBUG_MSG;
  2151. } else {
  2152. if (inst_cream->cp_num == 15)
  2153. cpu->WriteCP15Register(RD, CRn, OPCODE_1, CRm, OPCODE_2);
  2154. }
  2155. }
  2156. cpu->Reg[15] += cpu->GetInstructionSize();
  2157. INC_PC(sizeof(mcr_inst));
  2158. FETCH_INST;
  2159. GOTO_NEXT_INST;
  2160. }
  2161. MCRR_INST : {
  2162. // Stubbed, as the MPCore doesn't have any registers that are accessible
  2163. // through this instruction.
  2164. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2165. mcrr_inst* const inst_cream = (mcrr_inst*)inst_base->component;
  2166. LOG_ERROR(Core_ARM11, "MCRR executed | Coprocessor: %u, CRm %u, opc1: %u, Rt: %u, Rt2: %u",
  2167. inst_cream->cp_num, inst_cream->crm, inst_cream->opcode_1, inst_cream->rt,
  2168. inst_cream->rt2);
  2169. }
  2170. cpu->Reg[15] += cpu->GetInstructionSize();
  2171. INC_PC(sizeof(mcrr_inst));
  2172. FETCH_INST;
  2173. GOTO_NEXT_INST;
  2174. }
  2175. MLA_INST : {
  2176. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2177. mla_inst* inst_cream = (mla_inst*)inst_base->component;
  2178. u64 rm = RM;
  2179. u64 rs = RS;
  2180. u64 rn = RN;
  2181. RD = static_cast<u32>((rm * rs + rn) & 0xffffffff);
  2182. if (inst_cream->S) {
  2183. UPDATE_NFLAG(RD);
  2184. UPDATE_ZFLAG(RD);
  2185. }
  2186. }
  2187. cpu->Reg[15] += cpu->GetInstructionSize();
  2188. INC_PC(sizeof(mla_inst));
  2189. FETCH_INST;
  2190. GOTO_NEXT_INST;
  2191. }
  2192. MOV_INST : {
  2193. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2194. mov_inst* inst_cream = (mov_inst*)inst_base->component;
  2195. RD = SHIFTER_OPERAND;
  2196. if (inst_cream->S && (inst_cream->Rd == 15)) {
  2197. if (CurrentModeHasSPSR) {
  2198. cpu->Cpsr = cpu->Spsr_copy;
  2199. cpu->ChangePrivilegeMode(cpu->Spsr_copy & 0x1F);
  2200. LOAD_NZCVT;
  2201. }
  2202. } else if (inst_cream->S) {
  2203. UPDATE_NFLAG(RD);
  2204. UPDATE_ZFLAG(RD);
  2205. UPDATE_CFLAG_WITH_SC;
  2206. }
  2207. if (inst_cream->Rd == 15) {
  2208. INC_PC(sizeof(mov_inst));
  2209. goto DISPATCH;
  2210. }
  2211. }
  2212. cpu->Reg[15] += cpu->GetInstructionSize();
  2213. INC_PC(sizeof(mov_inst));
  2214. FETCH_INST;
  2215. GOTO_NEXT_INST;
  2216. }
  2217. MRC_INST : {
  2218. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2219. mrc_inst* inst_cream = (mrc_inst*)inst_base->component;
  2220. if (inst_cream->cp_num == 15) {
  2221. const uint32_t value = cpu->ReadCP15Register(CRn, OPCODE_1, CRm, OPCODE_2);
  2222. if (inst_cream->Rd == 15) {
  2223. cpu->Cpsr = (cpu->Cpsr & ~0xF0000000) | (value & 0xF0000000);
  2224. LOAD_NZCVT;
  2225. } else {
  2226. RD = value;
  2227. }
  2228. }
  2229. }
  2230. cpu->Reg[15] += cpu->GetInstructionSize();
  2231. INC_PC(sizeof(mrc_inst));
  2232. FETCH_INST;
  2233. GOTO_NEXT_INST;
  2234. }
  2235. MRRC_INST : {
  2236. // Stubbed, as the MPCore doesn't have any registers that are accessible
  2237. // through this instruction.
  2238. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2239. mcrr_inst* const inst_cream = (mcrr_inst*)inst_base->component;
  2240. LOG_ERROR(Core_ARM11, "MRRC executed | Coprocessor: %u, CRm %u, opc1: %u, Rt: %u, Rt2: %u",
  2241. inst_cream->cp_num, inst_cream->crm, inst_cream->opcode_1, inst_cream->rt,
  2242. inst_cream->rt2);
  2243. }
  2244. cpu->Reg[15] += cpu->GetInstructionSize();
  2245. INC_PC(sizeof(mcrr_inst));
  2246. FETCH_INST;
  2247. GOTO_NEXT_INST;
  2248. }
  2249. MRS_INST : {
  2250. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2251. mrs_inst* inst_cream = (mrs_inst*)inst_base->component;
  2252. if (inst_cream->R) {
  2253. RD = cpu->Spsr_copy;
  2254. } else {
  2255. SAVE_NZCVT;
  2256. RD = cpu->Cpsr;
  2257. }
  2258. }
  2259. cpu->Reg[15] += cpu->GetInstructionSize();
  2260. INC_PC(sizeof(mrs_inst));
  2261. FETCH_INST;
  2262. GOTO_NEXT_INST;
  2263. }
  2264. MSR_INST : {
  2265. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2266. msr_inst* inst_cream = (msr_inst*)inst_base->component;
  2267. const u32 UserMask = 0xf80f0200, PrivMask = 0x000001df, StateMask = 0x01000020;
  2268. unsigned int inst = inst_cream->inst;
  2269. unsigned int operand;
  2270. if (BIT(inst, 25)) {
  2271. int rot_imm = BITS(inst, 8, 11) * 2;
  2272. operand = ROTATE_RIGHT_32(BITS(inst, 0, 7), rot_imm);
  2273. } else {
  2274. operand = cpu->Reg[BITS(inst, 0, 3)];
  2275. }
  2276. u32 byte_mask = (BIT(inst, 16) ? 0xff : 0) | (BIT(inst, 17) ? 0xff00 : 0) |
  2277. (BIT(inst, 18) ? 0xff0000 : 0) | (BIT(inst, 19) ? 0xff000000 : 0);
  2278. u32 mask = 0;
  2279. if (!inst_cream->R) {
  2280. if (cpu->InAPrivilegedMode()) {
  2281. if ((operand & StateMask) != 0) {
  2282. /// UNPREDICTABLE
  2283. DEBUG_MSG;
  2284. } else
  2285. mask = byte_mask & (UserMask | PrivMask);
  2286. } else {
  2287. mask = byte_mask & UserMask;
  2288. }
  2289. SAVE_NZCVT;
  2290. cpu->Cpsr = (cpu->Cpsr & ~mask) | (operand & mask);
  2291. cpu->ChangePrivilegeMode(cpu->Cpsr & 0x1F);
  2292. LOAD_NZCVT;
  2293. } else {
  2294. if (CurrentModeHasSPSR) {
  2295. mask = byte_mask & (UserMask | PrivMask | StateMask);
  2296. cpu->Spsr_copy = (cpu->Spsr_copy & ~mask) | (operand & mask);
  2297. }
  2298. }
  2299. }
  2300. cpu->Reg[15] += cpu->GetInstructionSize();
  2301. INC_PC(sizeof(msr_inst));
  2302. FETCH_INST;
  2303. GOTO_NEXT_INST;
  2304. }
  2305. MUL_INST : {
  2306. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2307. mul_inst* inst_cream = (mul_inst*)inst_base->component;
  2308. u64 rm = RM;
  2309. u64 rs = RS;
  2310. RD = static_cast<u32>((rm * rs) & 0xffffffff);
  2311. if (inst_cream->S) {
  2312. UPDATE_NFLAG(RD);
  2313. UPDATE_ZFLAG(RD);
  2314. }
  2315. }
  2316. cpu->Reg[15] += cpu->GetInstructionSize();
  2317. INC_PC(sizeof(mul_inst));
  2318. FETCH_INST;
  2319. GOTO_NEXT_INST;
  2320. }
  2321. MVN_INST : {
  2322. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2323. mvn_inst* const inst_cream = (mvn_inst*)inst_base->component;
  2324. RD = ~SHIFTER_OPERAND;
  2325. if (inst_cream->S && (inst_cream->Rd == 15)) {
  2326. if (CurrentModeHasSPSR) {
  2327. cpu->Cpsr = cpu->Spsr_copy;
  2328. cpu->ChangePrivilegeMode(cpu->Spsr_copy & 0x1F);
  2329. LOAD_NZCVT;
  2330. }
  2331. } else if (inst_cream->S) {
  2332. UPDATE_NFLAG(RD);
  2333. UPDATE_ZFLAG(RD);
  2334. UPDATE_CFLAG_WITH_SC;
  2335. }
  2336. if (inst_cream->Rd == 15) {
  2337. INC_PC(sizeof(mvn_inst));
  2338. goto DISPATCH;
  2339. }
  2340. }
  2341. cpu->Reg[15] += cpu->GetInstructionSize();
  2342. INC_PC(sizeof(mvn_inst));
  2343. FETCH_INST;
  2344. GOTO_NEXT_INST;
  2345. }
  2346. ORR_INST : {
  2347. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2348. orr_inst* const inst_cream = (orr_inst*)inst_base->component;
  2349. u32 lop = RN;
  2350. u32 rop = SHIFTER_OPERAND;
  2351. if (inst_cream->Rn == 15)
  2352. lop += 2 * cpu->GetInstructionSize();
  2353. RD = lop | rop;
  2354. if (inst_cream->S && (inst_cream->Rd == 15)) {
  2355. if (CurrentModeHasSPSR) {
  2356. cpu->Cpsr = cpu->Spsr_copy;
  2357. cpu->ChangePrivilegeMode(cpu->Spsr_copy & 0x1F);
  2358. LOAD_NZCVT;
  2359. }
  2360. } else if (inst_cream->S) {
  2361. UPDATE_NFLAG(RD);
  2362. UPDATE_ZFLAG(RD);
  2363. UPDATE_CFLAG_WITH_SC;
  2364. }
  2365. if (inst_cream->Rd == 15) {
  2366. INC_PC(sizeof(orr_inst));
  2367. goto DISPATCH;
  2368. }
  2369. }
  2370. cpu->Reg[15] += cpu->GetInstructionSize();
  2371. INC_PC(sizeof(orr_inst));
  2372. FETCH_INST;
  2373. GOTO_NEXT_INST;
  2374. }
  2375. NOP_INST : {
  2376. cpu->Reg[15] += cpu->GetInstructionSize();
  2377. INC_PC_STUB;
  2378. FETCH_INST;
  2379. GOTO_NEXT_INST;
  2380. }
  2381. PKHBT_INST : {
  2382. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2383. pkh_inst* inst_cream = (pkh_inst*)inst_base->component;
  2384. RD = (RN & 0xFFFF) | ((RM << inst_cream->imm) & 0xFFFF0000);
  2385. }
  2386. cpu->Reg[15] += cpu->GetInstructionSize();
  2387. INC_PC(sizeof(pkh_inst));
  2388. FETCH_INST;
  2389. GOTO_NEXT_INST;
  2390. }
  2391. PKHTB_INST : {
  2392. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2393. pkh_inst* inst_cream = (pkh_inst*)inst_base->component;
  2394. int shift_imm = inst_cream->imm ? inst_cream->imm : 31;
  2395. RD = ((static_cast<s32>(RM) >> shift_imm) & 0xFFFF) | (RN & 0xFFFF0000);
  2396. }
  2397. cpu->Reg[15] += cpu->GetInstructionSize();
  2398. INC_PC(sizeof(pkh_inst));
  2399. FETCH_INST;
  2400. GOTO_NEXT_INST;
  2401. }
  2402. PLD_INST : {
  2403. // Not implemented. PLD is a hint instruction, so it's optional.
  2404. cpu->Reg[15] += cpu->GetInstructionSize();
  2405. INC_PC(sizeof(pld_inst));
  2406. FETCH_INST;
  2407. GOTO_NEXT_INST;
  2408. }
  2409. QADD_INST:
  2410. QDADD_INST:
  2411. QDSUB_INST:
  2412. QSUB_INST : {
  2413. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2414. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  2415. const u8 op1 = inst_cream->op1;
  2416. const u32 rm_val = RM;
  2417. const u32 rn_val = RN;
  2418. u32 result = 0;
  2419. // QADD
  2420. if (op1 == 0x00) {
  2421. result = rm_val + rn_val;
  2422. if (AddOverflow(rm_val, rn_val, result)) {
  2423. result = POS(result) ? 0x80000000 : 0x7FFFFFFF;
  2424. cpu->Cpsr |= (1 << 27);
  2425. }
  2426. }
  2427. // QSUB
  2428. else if (op1 == 0x01) {
  2429. result = rm_val - rn_val;
  2430. if (SubOverflow(rm_val, rn_val, result)) {
  2431. result = POS(result) ? 0x80000000 : 0x7FFFFFFF;
  2432. cpu->Cpsr |= (1 << 27);
  2433. }
  2434. }
  2435. // QDADD
  2436. else if (op1 == 0x02) {
  2437. u32 mul = (rn_val * 2);
  2438. if (AddOverflow(rn_val, rn_val, rn_val * 2)) {
  2439. mul = POS(mul) ? 0x80000000 : 0x7FFFFFFF;
  2440. cpu->Cpsr |= (1 << 27);
  2441. }
  2442. result = mul + rm_val;
  2443. if (AddOverflow(rm_val, mul, result)) {
  2444. result = POS(result) ? 0x80000000 : 0x7FFFFFFF;
  2445. cpu->Cpsr |= (1 << 27);
  2446. }
  2447. }
  2448. // QDSUB
  2449. else if (op1 == 0x03) {
  2450. u32 mul = (rn_val * 2);
  2451. if (AddOverflow(rn_val, rn_val, mul)) {
  2452. mul = POS(mul) ? 0x80000000 : 0x7FFFFFFF;
  2453. cpu->Cpsr |= (1 << 27);
  2454. }
  2455. result = rm_val - mul;
  2456. if (SubOverflow(rm_val, mul, result)) {
  2457. result = POS(result) ? 0x80000000 : 0x7FFFFFFF;
  2458. cpu->Cpsr |= (1 << 27);
  2459. }
  2460. }
  2461. RD = result;
  2462. }
  2463. cpu->Reg[15] += cpu->GetInstructionSize();
  2464. INC_PC(sizeof(generic_arm_inst));
  2465. FETCH_INST;
  2466. GOTO_NEXT_INST;
  2467. }
  2468. QADD8_INST:
  2469. QADD16_INST:
  2470. QADDSUBX_INST:
  2471. QSUB8_INST:
  2472. QSUB16_INST:
  2473. QSUBADDX_INST : {
  2474. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2475. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  2476. const u16 rm_lo = (RM & 0xFFFF);
  2477. const u16 rm_hi = ((RM >> 16) & 0xFFFF);
  2478. const u16 rn_lo = (RN & 0xFFFF);
  2479. const u16 rn_hi = ((RN >> 16) & 0xFFFF);
  2480. const u8 op2 = inst_cream->op2;
  2481. u16 lo_result = 0;
  2482. u16 hi_result = 0;
  2483. // QADD16
  2484. if (op2 == 0x00) {
  2485. lo_result = ARMul_SignedSaturatedAdd16(rn_lo, rm_lo);
  2486. hi_result = ARMul_SignedSaturatedAdd16(rn_hi, rm_hi);
  2487. }
  2488. // QASX
  2489. else if (op2 == 0x01) {
  2490. lo_result = ARMul_SignedSaturatedSub16(rn_lo, rm_hi);
  2491. hi_result = ARMul_SignedSaturatedAdd16(rn_hi, rm_lo);
  2492. }
  2493. // QSAX
  2494. else if (op2 == 0x02) {
  2495. lo_result = ARMul_SignedSaturatedAdd16(rn_lo, rm_hi);
  2496. hi_result = ARMul_SignedSaturatedSub16(rn_hi, rm_lo);
  2497. }
  2498. // QSUB16
  2499. else if (op2 == 0x03) {
  2500. lo_result = ARMul_SignedSaturatedSub16(rn_lo, rm_lo);
  2501. hi_result = ARMul_SignedSaturatedSub16(rn_hi, rm_hi);
  2502. }
  2503. // QADD8
  2504. else if (op2 == 0x04) {
  2505. lo_result = ARMul_SignedSaturatedAdd8(rn_lo & 0xFF, rm_lo & 0xFF) |
  2506. ARMul_SignedSaturatedAdd8(rn_lo >> 8, rm_lo >> 8) << 8;
  2507. hi_result = ARMul_SignedSaturatedAdd8(rn_hi & 0xFF, rm_hi & 0xFF) |
  2508. ARMul_SignedSaturatedAdd8(rn_hi >> 8, rm_hi >> 8) << 8;
  2509. }
  2510. // QSUB8
  2511. else if (op2 == 0x07) {
  2512. lo_result = ARMul_SignedSaturatedSub8(rn_lo & 0xFF, rm_lo & 0xFF) |
  2513. ARMul_SignedSaturatedSub8(rn_lo >> 8, rm_lo >> 8) << 8;
  2514. hi_result = ARMul_SignedSaturatedSub8(rn_hi & 0xFF, rm_hi & 0xFF) |
  2515. ARMul_SignedSaturatedSub8(rn_hi >> 8, rm_hi >> 8) << 8;
  2516. }
  2517. RD = (lo_result & 0xFFFF) | ((hi_result & 0xFFFF) << 16);
  2518. }
  2519. cpu->Reg[15] += cpu->GetInstructionSize();
  2520. INC_PC(sizeof(generic_arm_inst));
  2521. FETCH_INST;
  2522. GOTO_NEXT_INST;
  2523. }
  2524. REV_INST:
  2525. REV16_INST:
  2526. REVSH_INST : {
  2527. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2528. rev_inst* const inst_cream = (rev_inst*)inst_base->component;
  2529. const u8 op1 = inst_cream->op1;
  2530. const u8 op2 = inst_cream->op2;
  2531. // REV
  2532. if (op1 == 0x03 && op2 == 0x01) {
  2533. RD = ((RM & 0xFF) << 24) | (((RM >> 8) & 0xFF) << 16) | (((RM >> 16) & 0xFF) << 8) |
  2534. ((RM >> 24) & 0xFF);
  2535. }
  2536. // REV16
  2537. else if (op1 == 0x03 && op2 == 0x05) {
  2538. RD = ((RM & 0xFF) << 8) | ((RM & 0xFF00) >> 8) | ((RM & 0xFF0000) << 8) |
  2539. ((RM & 0xFF000000) >> 8);
  2540. }
  2541. // REVSH
  2542. else if (op1 == 0x07 && op2 == 0x05) {
  2543. RD = ((RM & 0xFF) << 8) | ((RM & 0xFF00) >> 8);
  2544. if (RD & 0x8000)
  2545. RD |= 0xffff0000;
  2546. }
  2547. }
  2548. cpu->Reg[15] += cpu->GetInstructionSize();
  2549. INC_PC(sizeof(rev_inst));
  2550. FETCH_INST;
  2551. GOTO_NEXT_INST;
  2552. }
  2553. RFE_INST : {
  2554. // RFE is unconditional
  2555. ldst_inst* const inst_cream = (ldst_inst*)inst_base->component;
  2556. u32 address = 0;
  2557. inst_cream->get_addr(cpu, inst_cream->inst, address);
  2558. cpu->Cpsr = cpu->ReadMemory32(address);
  2559. cpu->Reg[15] = cpu->ReadMemory32(address + 4);
  2560. INC_PC(sizeof(ldst_inst));
  2561. goto DISPATCH;
  2562. }
  2563. RSB_INST : {
  2564. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2565. rsb_inst* const inst_cream = (rsb_inst*)inst_base->component;
  2566. u32 rn_val = RN;
  2567. if (inst_cream->Rn == 15)
  2568. rn_val += 2 * cpu->GetInstructionSize();
  2569. bool carry;
  2570. bool overflow;
  2571. RD = AddWithCarry(~rn_val, SHIFTER_OPERAND, 1, &carry, &overflow);
  2572. if (inst_cream->S && (inst_cream->Rd == 15)) {
  2573. if (CurrentModeHasSPSR) {
  2574. cpu->Cpsr = cpu->Spsr_copy;
  2575. cpu->ChangePrivilegeMode(cpu->Spsr_copy & 0x1F);
  2576. LOAD_NZCVT;
  2577. }
  2578. } else if (inst_cream->S) {
  2579. UPDATE_NFLAG(RD);
  2580. UPDATE_ZFLAG(RD);
  2581. cpu->CFlag = carry;
  2582. cpu->VFlag = overflow;
  2583. }
  2584. if (inst_cream->Rd == 15) {
  2585. INC_PC(sizeof(rsb_inst));
  2586. goto DISPATCH;
  2587. }
  2588. }
  2589. cpu->Reg[15] += cpu->GetInstructionSize();
  2590. INC_PC(sizeof(rsb_inst));
  2591. FETCH_INST;
  2592. GOTO_NEXT_INST;
  2593. }
  2594. RSC_INST : {
  2595. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2596. rsc_inst* const inst_cream = (rsc_inst*)inst_base->component;
  2597. u32 rn_val = RN;
  2598. if (inst_cream->Rn == 15)
  2599. rn_val += 2 * cpu->GetInstructionSize();
  2600. bool carry;
  2601. bool overflow;
  2602. RD = AddWithCarry(~rn_val, SHIFTER_OPERAND, cpu->CFlag, &carry, &overflow);
  2603. if (inst_cream->S && (inst_cream->Rd == 15)) {
  2604. if (CurrentModeHasSPSR) {
  2605. cpu->Cpsr = cpu->Spsr_copy;
  2606. cpu->ChangePrivilegeMode(cpu->Spsr_copy & 0x1F);
  2607. LOAD_NZCVT;
  2608. }
  2609. } else if (inst_cream->S) {
  2610. UPDATE_NFLAG(RD);
  2611. UPDATE_ZFLAG(RD);
  2612. cpu->CFlag = carry;
  2613. cpu->VFlag = overflow;
  2614. }
  2615. if (inst_cream->Rd == 15) {
  2616. INC_PC(sizeof(rsc_inst));
  2617. goto DISPATCH;
  2618. }
  2619. }
  2620. cpu->Reg[15] += cpu->GetInstructionSize();
  2621. INC_PC(sizeof(rsc_inst));
  2622. FETCH_INST;
  2623. GOTO_NEXT_INST;
  2624. }
  2625. SADD8_INST:
  2626. SSUB8_INST:
  2627. SADD16_INST:
  2628. SADDSUBX_INST:
  2629. SSUBADDX_INST:
  2630. SSUB16_INST : {
  2631. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2632. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  2633. const u8 op2 = inst_cream->op2;
  2634. if (op2 == 0x00 || op2 == 0x01 || op2 == 0x02 || op2 == 0x03) {
  2635. const s16 rn_lo = (RN & 0xFFFF);
  2636. const s16 rn_hi = ((RN >> 16) & 0xFFFF);
  2637. const s16 rm_lo = (RM & 0xFFFF);
  2638. const s16 rm_hi = ((RM >> 16) & 0xFFFF);
  2639. s32 lo_result = 0;
  2640. s32 hi_result = 0;
  2641. // SADD16
  2642. if (inst_cream->op2 == 0x00) {
  2643. lo_result = (rn_lo + rm_lo);
  2644. hi_result = (rn_hi + rm_hi);
  2645. }
  2646. // SASX
  2647. else if (op2 == 0x01) {
  2648. lo_result = (rn_lo - rm_hi);
  2649. hi_result = (rn_hi + rm_lo);
  2650. }
  2651. // SSAX
  2652. else if (op2 == 0x02) {
  2653. lo_result = (rn_lo + rm_hi);
  2654. hi_result = (rn_hi - rm_lo);
  2655. }
  2656. // SSUB16
  2657. else if (op2 == 0x03) {
  2658. lo_result = (rn_lo - rm_lo);
  2659. hi_result = (rn_hi - rm_hi);
  2660. }
  2661. RD = (lo_result & 0xFFFF) | ((hi_result & 0xFFFF) << 16);
  2662. if (lo_result >= 0) {
  2663. cpu->Cpsr |= (1 << 16);
  2664. cpu->Cpsr |= (1 << 17);
  2665. } else {
  2666. cpu->Cpsr &= ~(1 << 16);
  2667. cpu->Cpsr &= ~(1 << 17);
  2668. }
  2669. if (hi_result >= 0) {
  2670. cpu->Cpsr |= (1 << 18);
  2671. cpu->Cpsr |= (1 << 19);
  2672. } else {
  2673. cpu->Cpsr &= ~(1 << 18);
  2674. cpu->Cpsr &= ~(1 << 19);
  2675. }
  2676. } else if (op2 == 0x04 || op2 == 0x07) {
  2677. s32 lo_val1, lo_val2;
  2678. s32 hi_val1, hi_val2;
  2679. // SADD8
  2680. if (op2 == 0x04) {
  2681. lo_val1 = (s32)(s8)(RN & 0xFF) + (s32)(s8)(RM & 0xFF);
  2682. lo_val2 = (s32)(s8)((RN >> 8) & 0xFF) + (s32)(s8)((RM >> 8) & 0xFF);
  2683. hi_val1 = (s32)(s8)((RN >> 16) & 0xFF) + (s32)(s8)((RM >> 16) & 0xFF);
  2684. hi_val2 = (s32)(s8)((RN >> 24) & 0xFF) + (s32)(s8)((RM >> 24) & 0xFF);
  2685. }
  2686. // SSUB8
  2687. else {
  2688. lo_val1 = (s32)(s8)(RN & 0xFF) - (s32)(s8)(RM & 0xFF);
  2689. lo_val2 = (s32)(s8)((RN >> 8) & 0xFF) - (s32)(s8)((RM >> 8) & 0xFF);
  2690. hi_val1 = (s32)(s8)((RN >> 16) & 0xFF) - (s32)(s8)((RM >> 16) & 0xFF);
  2691. hi_val2 = (s32)(s8)((RN >> 24) & 0xFF) - (s32)(s8)((RM >> 24) & 0xFF);
  2692. }
  2693. RD = ((lo_val1 & 0xFF) | ((lo_val2 & 0xFF) << 8) | ((hi_val1 & 0xFF) << 16) |
  2694. ((hi_val2 & 0xFF) << 24));
  2695. if (lo_val1 >= 0)
  2696. cpu->Cpsr |= (1 << 16);
  2697. else
  2698. cpu->Cpsr &= ~(1 << 16);
  2699. if (lo_val2 >= 0)
  2700. cpu->Cpsr |= (1 << 17);
  2701. else
  2702. cpu->Cpsr &= ~(1 << 17);
  2703. if (hi_val1 >= 0)
  2704. cpu->Cpsr |= (1 << 18);
  2705. else
  2706. cpu->Cpsr &= ~(1 << 18);
  2707. if (hi_val2 >= 0)
  2708. cpu->Cpsr |= (1 << 19);
  2709. else
  2710. cpu->Cpsr &= ~(1 << 19);
  2711. }
  2712. }
  2713. cpu->Reg[15] += cpu->GetInstructionSize();
  2714. INC_PC(sizeof(generic_arm_inst));
  2715. FETCH_INST;
  2716. GOTO_NEXT_INST;
  2717. }
  2718. SBC_INST : {
  2719. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2720. sbc_inst* const inst_cream = (sbc_inst*)inst_base->component;
  2721. u32 rn_val = RN;
  2722. if (inst_cream->Rn == 15)
  2723. rn_val += 2 * cpu->GetInstructionSize();
  2724. bool carry;
  2725. bool overflow;
  2726. RD = AddWithCarry(rn_val, ~SHIFTER_OPERAND, cpu->CFlag, &carry, &overflow);
  2727. if (inst_cream->S && (inst_cream->Rd == 15)) {
  2728. if (CurrentModeHasSPSR) {
  2729. cpu->Cpsr = cpu->Spsr_copy;
  2730. cpu->ChangePrivilegeMode(cpu->Spsr_copy & 0x1F);
  2731. LOAD_NZCVT;
  2732. }
  2733. } else if (inst_cream->S) {
  2734. UPDATE_NFLAG(RD);
  2735. UPDATE_ZFLAG(RD);
  2736. cpu->CFlag = carry;
  2737. cpu->VFlag = overflow;
  2738. }
  2739. if (inst_cream->Rd == 15) {
  2740. INC_PC(sizeof(sbc_inst));
  2741. goto DISPATCH;
  2742. }
  2743. }
  2744. cpu->Reg[15] += cpu->GetInstructionSize();
  2745. INC_PC(sizeof(sbc_inst));
  2746. FETCH_INST;
  2747. GOTO_NEXT_INST;
  2748. }
  2749. SEL_INST : {
  2750. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2751. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  2752. const u32 to = RM;
  2753. const u32 from = RN;
  2754. const u32 cpsr = cpu->Cpsr;
  2755. u32 result;
  2756. if (cpsr & (1 << 16))
  2757. result = from & 0xff;
  2758. else
  2759. result = to & 0xff;
  2760. if (cpsr & (1 << 17))
  2761. result |= from & 0x0000ff00;
  2762. else
  2763. result |= to & 0x0000ff00;
  2764. if (cpsr & (1 << 18))
  2765. result |= from & 0x00ff0000;
  2766. else
  2767. result |= to & 0x00ff0000;
  2768. if (cpsr & (1 << 19))
  2769. result |= from & 0xff000000;
  2770. else
  2771. result |= to & 0xff000000;
  2772. RD = result;
  2773. }
  2774. cpu->Reg[15] += cpu->GetInstructionSize();
  2775. INC_PC(sizeof(generic_arm_inst));
  2776. FETCH_INST;
  2777. GOTO_NEXT_INST;
  2778. }
  2779. SETEND_INST : {
  2780. // SETEND is unconditional
  2781. setend_inst* const inst_cream = (setend_inst*)inst_base->component;
  2782. const bool big_endian = (inst_cream->set_bigend == 1);
  2783. if (big_endian)
  2784. cpu->Cpsr |= (1 << 9);
  2785. else
  2786. cpu->Cpsr &= ~(1 << 9);
  2787. LOG_WARNING(Core_ARM11, "SETEND %s executed", big_endian ? "BE" : "LE");
  2788. cpu->Reg[15] += cpu->GetInstructionSize();
  2789. INC_PC(sizeof(setend_inst));
  2790. FETCH_INST;
  2791. GOTO_NEXT_INST;
  2792. }
  2793. SEV_INST : {
  2794. // Stubbed, as SEV is a hint instruction.
  2795. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2796. LOG_TRACE(Core_ARM11, "SEV executed.");
  2797. }
  2798. cpu->Reg[15] += cpu->GetInstructionSize();
  2799. INC_PC_STUB;
  2800. FETCH_INST;
  2801. GOTO_NEXT_INST;
  2802. }
  2803. SHADD8_INST:
  2804. SHADD16_INST:
  2805. SHADDSUBX_INST:
  2806. SHSUB8_INST:
  2807. SHSUB16_INST:
  2808. SHSUBADDX_INST : {
  2809. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2810. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  2811. const u8 op2 = inst_cream->op2;
  2812. const u32 rm_val = RM;
  2813. const u32 rn_val = RN;
  2814. if (op2 == 0x00 || op2 == 0x01 || op2 == 0x02 || op2 == 0x03) {
  2815. s32 lo_result = 0;
  2816. s32 hi_result = 0;
  2817. // SHADD16
  2818. if (op2 == 0x00) {
  2819. lo_result = ((s16)(rn_val & 0xFFFF) + (s16)(rm_val & 0xFFFF)) >> 1;
  2820. hi_result = ((s16)((rn_val >> 16) & 0xFFFF) + (s16)((rm_val >> 16) & 0xFFFF)) >> 1;
  2821. }
  2822. // SHASX
  2823. else if (op2 == 0x01) {
  2824. lo_result = ((s16)(rn_val & 0xFFFF) - (s16)((rm_val >> 16) & 0xFFFF)) >> 1;
  2825. hi_result = ((s16)((rn_val >> 16) & 0xFFFF) + (s16)(rm_val & 0xFFFF)) >> 1;
  2826. }
  2827. // SHSAX
  2828. else if (op2 == 0x02) {
  2829. lo_result = ((s16)(rn_val & 0xFFFF) + (s16)((rm_val >> 16) & 0xFFFF)) >> 1;
  2830. hi_result = ((s16)((rn_val >> 16) & 0xFFFF) - (s16)(rm_val & 0xFFFF)) >> 1;
  2831. }
  2832. // SHSUB16
  2833. else if (op2 == 0x03) {
  2834. lo_result = ((s16)(rn_val & 0xFFFF) - (s16)(rm_val & 0xFFFF)) >> 1;
  2835. hi_result = ((s16)((rn_val >> 16) & 0xFFFF) - (s16)((rm_val >> 16) & 0xFFFF)) >> 1;
  2836. }
  2837. RD = ((lo_result & 0xFFFF) | ((hi_result & 0xFFFF) << 16));
  2838. } else if (op2 == 0x04 || op2 == 0x07) {
  2839. s16 lo_val1, lo_val2;
  2840. s16 hi_val1, hi_val2;
  2841. // SHADD8
  2842. if (op2 == 0x04) {
  2843. lo_val1 = ((s8)(rn_val & 0xFF) + (s8)(rm_val & 0xFF)) >> 1;
  2844. lo_val2 = ((s8)((rn_val >> 8) & 0xFF) + (s8)((rm_val >> 8) & 0xFF)) >> 1;
  2845. hi_val1 = ((s8)((rn_val >> 16) & 0xFF) + (s8)((rm_val >> 16) & 0xFF)) >> 1;
  2846. hi_val2 = ((s8)((rn_val >> 24) & 0xFF) + (s8)((rm_val >> 24) & 0xFF)) >> 1;
  2847. }
  2848. // SHSUB8
  2849. else {
  2850. lo_val1 = ((s8)(rn_val & 0xFF) - (s8)(rm_val & 0xFF)) >> 1;
  2851. lo_val2 = ((s8)((rn_val >> 8) & 0xFF) - (s8)((rm_val >> 8) & 0xFF)) >> 1;
  2852. hi_val1 = ((s8)((rn_val >> 16) & 0xFF) - (s8)((rm_val >> 16) & 0xFF)) >> 1;
  2853. hi_val2 = ((s8)((rn_val >> 24) & 0xFF) - (s8)((rm_val >> 24) & 0xFF)) >> 1;
  2854. }
  2855. RD = (lo_val1 & 0xFF) | ((lo_val2 & 0xFF) << 8) | ((hi_val1 & 0xFF) << 16) |
  2856. ((hi_val2 & 0xFF) << 24);
  2857. }
  2858. }
  2859. cpu->Reg[15] += cpu->GetInstructionSize();
  2860. INC_PC(sizeof(generic_arm_inst));
  2861. FETCH_INST;
  2862. GOTO_NEXT_INST;
  2863. }
  2864. SMLA_INST : {
  2865. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2866. smla_inst* inst_cream = (smla_inst*)inst_base->component;
  2867. s32 operand1, operand2;
  2868. if (inst_cream->x == 0)
  2869. operand1 = (BIT(RM, 15)) ? (BITS(RM, 0, 15) | 0xffff0000) : BITS(RM, 0, 15);
  2870. else
  2871. operand1 = (BIT(RM, 31)) ? (BITS(RM, 16, 31) | 0xffff0000) : BITS(RM, 16, 31);
  2872. if (inst_cream->y == 0)
  2873. operand2 = (BIT(RS, 15)) ? (BITS(RS, 0, 15) | 0xffff0000) : BITS(RS, 0, 15);
  2874. else
  2875. operand2 = (BIT(RS, 31)) ? (BITS(RS, 16, 31) | 0xffff0000) : BITS(RS, 16, 31);
  2876. u32 product = operand1 * operand2;
  2877. u32 result = product + RN;
  2878. if (AddOverflow(product, RN, result))
  2879. cpu->Cpsr |= (1 << 27);
  2880. RD = result;
  2881. }
  2882. cpu->Reg[15] += cpu->GetInstructionSize();
  2883. INC_PC(sizeof(smla_inst));
  2884. FETCH_INST;
  2885. GOTO_NEXT_INST;
  2886. }
  2887. SMLAD_INST:
  2888. SMLSD_INST:
  2889. SMUAD_INST:
  2890. SMUSD_INST : {
  2891. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2892. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  2893. const u8 op2 = inst_cream->op2;
  2894. u32 rm_val = cpu->Reg[inst_cream->Rm];
  2895. const u32 rn_val = cpu->Reg[inst_cream->Rn];
  2896. if (inst_cream->m)
  2897. rm_val = (((rm_val & 0xFFFF) << 16) | (rm_val >> 16));
  2898. const s16 rm_lo = (rm_val & 0xFFFF);
  2899. const s16 rm_hi = ((rm_val >> 16) & 0xFFFF);
  2900. const s16 rn_lo = (rn_val & 0xFFFF);
  2901. const s16 rn_hi = ((rn_val >> 16) & 0xFFFF);
  2902. const u32 product1 = (rn_lo * rm_lo);
  2903. const u32 product2 = (rn_hi * rm_hi);
  2904. // SMUAD and SMLAD
  2905. if (BIT(op2, 1) == 0) {
  2906. u32 rd_val = (product1 + product2);
  2907. if (inst_cream->Ra != 15) {
  2908. rd_val += cpu->Reg[inst_cream->Ra];
  2909. if (ARMul_AddOverflowQ(product1 + product2, cpu->Reg[inst_cream->Ra]))
  2910. cpu->Cpsr |= (1 << 27);
  2911. }
  2912. RD = rd_val;
  2913. if (ARMul_AddOverflowQ(product1, product2))
  2914. cpu->Cpsr |= (1 << 27);
  2915. }
  2916. // SMUSD and SMLSD
  2917. else {
  2918. u32 rd_val = (product1 - product2);
  2919. if (inst_cream->Ra != 15) {
  2920. rd_val += cpu->Reg[inst_cream->Ra];
  2921. if (ARMul_AddOverflowQ(product1 - product2, cpu->Reg[inst_cream->Ra]))
  2922. cpu->Cpsr |= (1 << 27);
  2923. }
  2924. RD = rd_val;
  2925. }
  2926. }
  2927. cpu->Reg[15] += cpu->GetInstructionSize();
  2928. INC_PC(sizeof(smlad_inst));
  2929. FETCH_INST;
  2930. GOTO_NEXT_INST;
  2931. }
  2932. SMLAL_INST : {
  2933. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2934. umlal_inst* inst_cream = (umlal_inst*)inst_base->component;
  2935. long long int rm = RM;
  2936. long long int rs = RS;
  2937. if (BIT(rm, 31)) {
  2938. rm |= 0xffffffff00000000LL;
  2939. }
  2940. if (BIT(rs, 31)) {
  2941. rs |= 0xffffffff00000000LL;
  2942. }
  2943. long long int rst = rm * rs;
  2944. long long int rdhi32 = RDHI;
  2945. long long int hilo = (rdhi32 << 32) + RDLO;
  2946. rst += hilo;
  2947. RDLO = BITS(rst, 0, 31);
  2948. RDHI = BITS(rst, 32, 63);
  2949. if (inst_cream->S) {
  2950. cpu->NFlag = BIT(RDHI, 31);
  2951. cpu->ZFlag = (RDHI == 0 && RDLO == 0);
  2952. }
  2953. }
  2954. cpu->Reg[15] += cpu->GetInstructionSize();
  2955. INC_PC(sizeof(umlal_inst));
  2956. FETCH_INST;
  2957. GOTO_NEXT_INST;
  2958. }
  2959. SMLALXY_INST : {
  2960. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2961. smlalxy_inst* const inst_cream = (smlalxy_inst*)inst_base->component;
  2962. u64 operand1 = RN;
  2963. u64 operand2 = RM;
  2964. if (inst_cream->x != 0)
  2965. operand1 >>= 16;
  2966. if (inst_cream->y != 0)
  2967. operand2 >>= 16;
  2968. operand1 &= 0xFFFF;
  2969. if (operand1 & 0x8000)
  2970. operand1 -= 65536;
  2971. operand2 &= 0xFFFF;
  2972. if (operand2 & 0x8000)
  2973. operand2 -= 65536;
  2974. u64 dest = ((u64)RDHI << 32 | RDLO) + (operand1 * operand2);
  2975. RDLO = (dest & 0xFFFFFFFF);
  2976. RDHI = ((dest >> 32) & 0xFFFFFFFF);
  2977. }
  2978. cpu->Reg[15] += cpu->GetInstructionSize();
  2979. INC_PC(sizeof(smlalxy_inst));
  2980. FETCH_INST;
  2981. GOTO_NEXT_INST;
  2982. }
  2983. SMLAW_INST : {
  2984. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  2985. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  2986. const u32 rm_val = RM;
  2987. const u32 rn_val = RN;
  2988. const u32 ra_val = cpu->Reg[inst_cream->Ra];
  2989. const bool high = (inst_cream->m == 1);
  2990. const s16 operand2 = (high) ? ((rm_val >> 16) & 0xFFFF) : (rm_val & 0xFFFF);
  2991. const s64 result = (s64)(s32)rn_val * (s64)(s32)operand2 + ((s64)(s32)ra_val << 16);
  2992. RD = BITS(result, 16, 47);
  2993. if ((result >> 16) != (s32)RD)
  2994. cpu->Cpsr |= (1 << 27);
  2995. }
  2996. cpu->Reg[15] += cpu->GetInstructionSize();
  2997. INC_PC(sizeof(smlad_inst));
  2998. FETCH_INST;
  2999. GOTO_NEXT_INST;
  3000. }
  3001. SMLALD_INST:
  3002. SMLSLD_INST : {
  3003. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3004. smlald_inst* const inst_cream = (smlald_inst*)inst_base->component;
  3005. const bool do_swap = (inst_cream->swap == 1);
  3006. const u32 rdlo_val = RDLO;
  3007. const u32 rdhi_val = RDHI;
  3008. const u32 rn_val = RN;
  3009. u32 rm_val = RM;
  3010. if (do_swap)
  3011. rm_val = (((rm_val & 0xFFFF) << 16) | (rm_val >> 16));
  3012. const s32 product1 = (s16)(rn_val & 0xFFFF) * (s16)(rm_val & 0xFFFF);
  3013. const s32 product2 = (s16)((rn_val >> 16) & 0xFFFF) * (s16)((rm_val >> 16) & 0xFFFF);
  3014. s64 result;
  3015. // SMLALD
  3016. if (BIT(inst_cream->op2, 1) == 0) {
  3017. result = (product1 + product2) + (s64)(rdlo_val | ((s64)rdhi_val << 32));
  3018. }
  3019. // SMLSLD
  3020. else {
  3021. result = (product1 - product2) + (s64)(rdlo_val | ((s64)rdhi_val << 32));
  3022. }
  3023. RDLO = (result & 0xFFFFFFFF);
  3024. RDHI = ((result >> 32) & 0xFFFFFFFF);
  3025. }
  3026. cpu->Reg[15] += cpu->GetInstructionSize();
  3027. INC_PC(sizeof(smlald_inst));
  3028. FETCH_INST;
  3029. GOTO_NEXT_INST;
  3030. }
  3031. SMMLA_INST:
  3032. SMMLS_INST:
  3033. SMMUL_INST : {
  3034. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3035. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  3036. const u32 rm_val = RM;
  3037. const u32 rn_val = RN;
  3038. const bool do_round = (inst_cream->m == 1);
  3039. // Assume SMMUL by default.
  3040. s64 result = (s64)(s32)rn_val * (s64)(s32)rm_val;
  3041. if (inst_cream->Ra != 15) {
  3042. const u32 ra_val = cpu->Reg[inst_cream->Ra];
  3043. // SMMLA, otherwise SMMLS
  3044. if (BIT(inst_cream->op2, 1) == 0)
  3045. result += ((s64)ra_val << 32);
  3046. else
  3047. result = ((s64)ra_val << 32) - result;
  3048. }
  3049. if (do_round)
  3050. result += 0x80000000;
  3051. RD = ((result >> 32) & 0xFFFFFFFF);
  3052. }
  3053. cpu->Reg[15] += cpu->GetInstructionSize();
  3054. INC_PC(sizeof(smlad_inst));
  3055. FETCH_INST;
  3056. GOTO_NEXT_INST;
  3057. }
  3058. SMUL_INST : {
  3059. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3060. smul_inst* inst_cream = (smul_inst*)inst_base->component;
  3061. u32 operand1, operand2;
  3062. if (inst_cream->x == 0)
  3063. operand1 = (BIT(RM, 15)) ? (BITS(RM, 0, 15) | 0xffff0000) : BITS(RM, 0, 15);
  3064. else
  3065. operand1 = (BIT(RM, 31)) ? (BITS(RM, 16, 31) | 0xffff0000) : BITS(RM, 16, 31);
  3066. if (inst_cream->y == 0)
  3067. operand2 = (BIT(RS, 15)) ? (BITS(RS, 0, 15) | 0xffff0000) : BITS(RS, 0, 15);
  3068. else
  3069. operand2 = (BIT(RS, 31)) ? (BITS(RS, 16, 31) | 0xffff0000) : BITS(RS, 16, 31);
  3070. RD = operand1 * operand2;
  3071. }
  3072. cpu->Reg[15] += cpu->GetInstructionSize();
  3073. INC_PC(sizeof(smul_inst));
  3074. FETCH_INST;
  3075. GOTO_NEXT_INST;
  3076. }
  3077. SMULL_INST : {
  3078. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3079. umull_inst* inst_cream = (umull_inst*)inst_base->component;
  3080. s64 rm = RM;
  3081. s64 rs = RS;
  3082. if (BIT(rm, 31)) {
  3083. rm |= 0xffffffff00000000LL;
  3084. }
  3085. if (BIT(rs, 31)) {
  3086. rs |= 0xffffffff00000000LL;
  3087. }
  3088. s64 rst = rm * rs;
  3089. RDHI = BITS(rst, 32, 63);
  3090. RDLO = BITS(rst, 0, 31);
  3091. if (inst_cream->S) {
  3092. cpu->NFlag = BIT(RDHI, 31);
  3093. cpu->ZFlag = (RDHI == 0 && RDLO == 0);
  3094. }
  3095. }
  3096. cpu->Reg[15] += cpu->GetInstructionSize();
  3097. INC_PC(sizeof(umull_inst));
  3098. FETCH_INST;
  3099. GOTO_NEXT_INST;
  3100. }
  3101. SMULW_INST : {
  3102. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3103. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  3104. s16 rm = (inst_cream->m == 1) ? ((RM >> 16) & 0xFFFF) : (RM & 0xFFFF);
  3105. s64 result = (s64)rm * (s64)(s32)RN;
  3106. RD = BITS(result, 16, 47);
  3107. }
  3108. cpu->Reg[15] += cpu->GetInstructionSize();
  3109. INC_PC(sizeof(smlad_inst));
  3110. FETCH_INST;
  3111. GOTO_NEXT_INST;
  3112. }
  3113. SRS_INST : {
  3114. // SRS is unconditional
  3115. ldst_inst* const inst_cream = (ldst_inst*)inst_base->component;
  3116. u32 address = 0;
  3117. inst_cream->get_addr(cpu, inst_cream->inst, address);
  3118. cpu->WriteMemory32(address + 0, cpu->Reg[14]);
  3119. cpu->WriteMemory32(address + 4, cpu->Spsr_copy);
  3120. cpu->Reg[15] += cpu->GetInstructionSize();
  3121. INC_PC(sizeof(ldst_inst));
  3122. FETCH_INST;
  3123. GOTO_NEXT_INST;
  3124. }
  3125. SSAT_INST : {
  3126. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3127. ssat_inst* const inst_cream = (ssat_inst*)inst_base->component;
  3128. u8 shift_type = inst_cream->shift_type;
  3129. u8 shift_amount = inst_cream->imm5;
  3130. u32 rn_val = RN;
  3131. // 32-bit ASR is encoded as an amount of 0.
  3132. if (shift_type == 1 && shift_amount == 0)
  3133. shift_amount = 31;
  3134. if (shift_type == 0)
  3135. rn_val <<= shift_amount;
  3136. else if (shift_type == 1)
  3137. rn_val = ((s32)rn_val >> shift_amount);
  3138. bool saturated = false;
  3139. rn_val = ARMul_SignedSatQ(rn_val, inst_cream->sat_imm, &saturated);
  3140. if (saturated)
  3141. cpu->Cpsr |= (1 << 27);
  3142. RD = rn_val;
  3143. }
  3144. cpu->Reg[15] += cpu->GetInstructionSize();
  3145. INC_PC(sizeof(ssat_inst));
  3146. FETCH_INST;
  3147. GOTO_NEXT_INST;
  3148. }
  3149. SSAT16_INST : {
  3150. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3151. ssat_inst* const inst_cream = (ssat_inst*)inst_base->component;
  3152. const u8 saturate_to = inst_cream->sat_imm;
  3153. bool sat1 = false;
  3154. bool sat2 = false;
  3155. RD = (ARMul_SignedSatQ((s16)RN, saturate_to, &sat1) & 0xFFFF) |
  3156. ARMul_SignedSatQ((s32)RN >> 16, saturate_to, &sat2) << 16;
  3157. if (sat1 || sat2)
  3158. cpu->Cpsr |= (1 << 27);
  3159. }
  3160. cpu->Reg[15] += cpu->GetInstructionSize();
  3161. INC_PC(sizeof(ssat_inst));
  3162. FETCH_INST;
  3163. GOTO_NEXT_INST;
  3164. }
  3165. STC_INST : {
  3166. // Instruction not implemented
  3167. // LOG_CRITICAL(Core_ARM11, "unimplemented instruction");
  3168. cpu->Reg[15] += cpu->GetInstructionSize();
  3169. INC_PC(sizeof(stc_inst));
  3170. FETCH_INST;
  3171. GOTO_NEXT_INST;
  3172. }
  3173. STM_INST : {
  3174. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3175. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  3176. unsigned int inst = inst_cream->inst;
  3177. unsigned int Rn = BITS(inst, 16, 19);
  3178. unsigned int old_RN = cpu->Reg[Rn];
  3179. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  3180. if (BIT(inst_cream->inst, 22) == 1) {
  3181. for (int i = 0; i < 13; i++) {
  3182. if (BIT(inst_cream->inst, i)) {
  3183. cpu->WriteMemory32(addr, cpu->Reg[i]);
  3184. addr += 4;
  3185. }
  3186. }
  3187. if (BIT(inst_cream->inst, 13)) {
  3188. if (cpu->Mode == USER32MODE)
  3189. cpu->WriteMemory32(addr, cpu->Reg[13]);
  3190. else
  3191. cpu->WriteMemory32(addr, cpu->Reg_usr[0]);
  3192. addr += 4;
  3193. }
  3194. if (BIT(inst_cream->inst, 14)) {
  3195. if (cpu->Mode == USER32MODE)
  3196. cpu->WriteMemory32(addr, cpu->Reg[14]);
  3197. else
  3198. cpu->WriteMemory32(addr, cpu->Reg_usr[1]);
  3199. addr += 4;
  3200. }
  3201. if (BIT(inst_cream->inst, 15)) {
  3202. cpu->WriteMemory32(addr, cpu->Reg[15] + 8);
  3203. }
  3204. } else {
  3205. for (int i = 0; i < 15; i++) {
  3206. if (BIT(inst_cream->inst, i)) {
  3207. if (i == Rn)
  3208. cpu->WriteMemory32(addr, old_RN);
  3209. else
  3210. cpu->WriteMemory32(addr, cpu->Reg[i]);
  3211. addr += 4;
  3212. }
  3213. }
  3214. // Check PC reg
  3215. if (BIT(inst_cream->inst, 15)) {
  3216. cpu->WriteMemory32(addr, cpu->Reg[15] + 8);
  3217. }
  3218. }
  3219. }
  3220. cpu->Reg[15] += cpu->GetInstructionSize();
  3221. INC_PC(sizeof(ldst_inst));
  3222. FETCH_INST;
  3223. GOTO_NEXT_INST;
  3224. }
  3225. SXTB_INST : {
  3226. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3227. sxtb_inst* inst_cream = (sxtb_inst*)inst_base->component;
  3228. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate);
  3229. if (BIT(operand2, 7)) {
  3230. operand2 |= 0xffffff00;
  3231. } else {
  3232. operand2 &= 0xff;
  3233. }
  3234. RD = operand2;
  3235. }
  3236. cpu->Reg[15] += cpu->GetInstructionSize();
  3237. INC_PC(sizeof(sxtb_inst));
  3238. FETCH_INST;
  3239. GOTO_NEXT_INST;
  3240. }
  3241. STR_INST : {
  3242. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3243. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  3244. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  3245. unsigned int reg = BITS(inst_cream->inst, 12, 15);
  3246. unsigned int value = cpu->Reg[reg];
  3247. if (reg == 15)
  3248. value += 2 * cpu->GetInstructionSize();
  3249. cpu->WriteMemory32(addr, value);
  3250. }
  3251. cpu->Reg[15] += cpu->GetInstructionSize();
  3252. INC_PC(sizeof(ldst_inst));
  3253. FETCH_INST;
  3254. GOTO_NEXT_INST;
  3255. }
  3256. UXTB_INST : {
  3257. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3258. uxtb_inst* inst_cream = (uxtb_inst*)inst_base->component;
  3259. RD = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xff;
  3260. }
  3261. cpu->Reg[15] += cpu->GetInstructionSize();
  3262. INC_PC(sizeof(uxtb_inst));
  3263. FETCH_INST;
  3264. GOTO_NEXT_INST;
  3265. }
  3266. UXTAB_INST : {
  3267. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3268. uxtab_inst* inst_cream = (uxtab_inst*)inst_base->component;
  3269. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xff;
  3270. RD = RN + operand2;
  3271. }
  3272. cpu->Reg[15] += cpu->GetInstructionSize();
  3273. INC_PC(sizeof(uxtab_inst));
  3274. FETCH_INST;
  3275. GOTO_NEXT_INST;
  3276. }
  3277. STRB_INST : {
  3278. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3279. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  3280. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  3281. unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff;
  3282. cpu->WriteMemory8(addr, value);
  3283. }
  3284. cpu->Reg[15] += cpu->GetInstructionSize();
  3285. INC_PC(sizeof(ldst_inst));
  3286. FETCH_INST;
  3287. GOTO_NEXT_INST;
  3288. }
  3289. STRBT_INST : {
  3290. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3291. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  3292. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  3293. const u32 previous_mode = cpu->Mode;
  3294. const u32 value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff;
  3295. cpu->ChangePrivilegeMode(USER32MODE);
  3296. cpu->WriteMemory8(addr, value);
  3297. cpu->ChangePrivilegeMode(previous_mode);
  3298. }
  3299. cpu->Reg[15] += cpu->GetInstructionSize();
  3300. INC_PC(sizeof(ldst_inst));
  3301. FETCH_INST;
  3302. GOTO_NEXT_INST;
  3303. }
  3304. STRD_INST : {
  3305. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3306. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  3307. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  3308. // The 3DS doesn't have the Large Physical Access Extension (LPAE)
  3309. // so STRD wouldn't store these as a single write.
  3310. cpu->WriteMemory32(addr + 0, cpu->Reg[BITS(inst_cream->inst, 12, 15)]);
  3311. cpu->WriteMemory32(addr + 4, cpu->Reg[BITS(inst_cream->inst, 12, 15) + 1]);
  3312. }
  3313. cpu->Reg[15] += cpu->GetInstructionSize();
  3314. INC_PC(sizeof(ldst_inst));
  3315. FETCH_INST;
  3316. GOTO_NEXT_INST;
  3317. }
  3318. STREX_INST : {
  3319. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3320. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  3321. unsigned int write_addr = cpu->Reg[inst_cream->Rn];
  3322. if (cpu->IsExclusiveMemoryAccess(write_addr)) {
  3323. cpu->UnsetExclusiveMemoryAddress();
  3324. cpu->WriteMemory32(write_addr, RM);
  3325. RD = 0;
  3326. } else {
  3327. // Failed to write due to mutex access
  3328. RD = 1;
  3329. }
  3330. }
  3331. cpu->Reg[15] += cpu->GetInstructionSize();
  3332. INC_PC(sizeof(generic_arm_inst));
  3333. FETCH_INST;
  3334. GOTO_NEXT_INST;
  3335. }
  3336. STREXB_INST : {
  3337. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3338. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  3339. unsigned int write_addr = cpu->Reg[inst_cream->Rn];
  3340. if (cpu->IsExclusiveMemoryAccess(write_addr)) {
  3341. cpu->UnsetExclusiveMemoryAddress();
  3342. cpu->WriteMemory8(write_addr, cpu->Reg[inst_cream->Rm]);
  3343. RD = 0;
  3344. } else {
  3345. // Failed to write due to mutex access
  3346. RD = 1;
  3347. }
  3348. }
  3349. cpu->Reg[15] += cpu->GetInstructionSize();
  3350. INC_PC(sizeof(generic_arm_inst));
  3351. FETCH_INST;
  3352. GOTO_NEXT_INST;
  3353. }
  3354. STREXD_INST : {
  3355. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3356. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  3357. unsigned int write_addr = cpu->Reg[inst_cream->Rn];
  3358. if (cpu->IsExclusiveMemoryAccess(write_addr)) {
  3359. cpu->UnsetExclusiveMemoryAddress();
  3360. const u32 rt = cpu->Reg[inst_cream->Rm + 0];
  3361. const u32 rt2 = cpu->Reg[inst_cream->Rm + 1];
  3362. u64 value;
  3363. if (cpu->InBigEndianMode())
  3364. value = (((u64)rt << 32) | rt2);
  3365. else
  3366. value = (((u64)rt2 << 32) | rt);
  3367. cpu->WriteMemory64(write_addr, value);
  3368. RD = 0;
  3369. } else {
  3370. // Failed to write due to mutex access
  3371. RD = 1;
  3372. }
  3373. }
  3374. cpu->Reg[15] += cpu->GetInstructionSize();
  3375. INC_PC(sizeof(generic_arm_inst));
  3376. FETCH_INST;
  3377. GOTO_NEXT_INST;
  3378. }
  3379. STREXH_INST : {
  3380. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3381. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  3382. unsigned int write_addr = cpu->Reg[inst_cream->Rn];
  3383. if (cpu->IsExclusiveMemoryAccess(write_addr)) {
  3384. cpu->UnsetExclusiveMemoryAddress();
  3385. cpu->WriteMemory16(write_addr, RM);
  3386. RD = 0;
  3387. } else {
  3388. // Failed to write due to mutex access
  3389. RD = 1;
  3390. }
  3391. }
  3392. cpu->Reg[15] += cpu->GetInstructionSize();
  3393. INC_PC(sizeof(generic_arm_inst));
  3394. FETCH_INST;
  3395. GOTO_NEXT_INST;
  3396. }
  3397. STRH_INST : {
  3398. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3399. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  3400. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  3401. unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xffff;
  3402. cpu->WriteMemory16(addr, value);
  3403. }
  3404. cpu->Reg[15] += cpu->GetInstructionSize();
  3405. INC_PC(sizeof(ldst_inst));
  3406. FETCH_INST;
  3407. GOTO_NEXT_INST;
  3408. }
  3409. STRT_INST : {
  3410. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3411. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  3412. inst_cream->get_addr(cpu, inst_cream->inst, addr);
  3413. const u32 previous_mode = cpu->Mode;
  3414. const u32 rt_index = BITS(inst_cream->inst, 12, 15);
  3415. u32 value = cpu->Reg[rt_index];
  3416. if (rt_index == 15)
  3417. value += 2 * cpu->GetInstructionSize();
  3418. cpu->ChangePrivilegeMode(USER32MODE);
  3419. cpu->WriteMemory32(addr, value);
  3420. cpu->ChangePrivilegeMode(previous_mode);
  3421. }
  3422. cpu->Reg[15] += cpu->GetInstructionSize();
  3423. INC_PC(sizeof(ldst_inst));
  3424. FETCH_INST;
  3425. GOTO_NEXT_INST;
  3426. }
  3427. SUB_INST : {
  3428. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3429. sub_inst* const inst_cream = (sub_inst*)inst_base->component;
  3430. u32 rn_val = CHECK_READ_REG15_WA(cpu, inst_cream->Rn);
  3431. bool carry;
  3432. bool overflow;
  3433. RD = AddWithCarry(rn_val, ~SHIFTER_OPERAND, 1, &carry, &overflow);
  3434. if (inst_cream->S && (inst_cream->Rd == 15)) {
  3435. if (CurrentModeHasSPSR) {
  3436. cpu->Cpsr = cpu->Spsr_copy;
  3437. cpu->ChangePrivilegeMode(cpu->Spsr_copy & 0x1F);
  3438. LOAD_NZCVT;
  3439. }
  3440. } else if (inst_cream->S) {
  3441. UPDATE_NFLAG(RD);
  3442. UPDATE_ZFLAG(RD);
  3443. cpu->CFlag = carry;
  3444. cpu->VFlag = overflow;
  3445. }
  3446. if (inst_cream->Rd == 15) {
  3447. INC_PC(sizeof(sub_inst));
  3448. goto DISPATCH;
  3449. }
  3450. }
  3451. cpu->Reg[15] += cpu->GetInstructionSize();
  3452. INC_PC(sizeof(sub_inst));
  3453. FETCH_INST;
  3454. GOTO_NEXT_INST;
  3455. }
  3456. SWI_INST : {
  3457. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3458. swi_inst* const inst_cream = (swi_inst*)inst_base->component;
  3459. SVC::CallSVC(inst_cream->num & 0xFFFF);
  3460. }
  3461. cpu->Reg[15] += cpu->GetInstructionSize();
  3462. INC_PC(sizeof(swi_inst));
  3463. FETCH_INST;
  3464. GOTO_NEXT_INST;
  3465. }
  3466. SWP_INST : {
  3467. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3468. swp_inst* inst_cream = (swp_inst*)inst_base->component;
  3469. addr = RN;
  3470. unsigned int value = cpu->ReadMemory32(addr);
  3471. cpu->WriteMemory32(addr, RM);
  3472. RD = value;
  3473. }
  3474. cpu->Reg[15] += cpu->GetInstructionSize();
  3475. INC_PC(sizeof(swp_inst));
  3476. FETCH_INST;
  3477. GOTO_NEXT_INST;
  3478. }
  3479. SWPB_INST : {
  3480. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3481. swp_inst* inst_cream = (swp_inst*)inst_base->component;
  3482. addr = RN;
  3483. unsigned int value = cpu->ReadMemory8(addr);
  3484. cpu->WriteMemory8(addr, (RM & 0xFF));
  3485. RD = value;
  3486. }
  3487. cpu->Reg[15] += cpu->GetInstructionSize();
  3488. INC_PC(sizeof(swp_inst));
  3489. FETCH_INST;
  3490. GOTO_NEXT_INST;
  3491. }
  3492. SXTAB_INST : {
  3493. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3494. sxtab_inst* inst_cream = (sxtab_inst*)inst_base->component;
  3495. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xff;
  3496. // Sign extend for byte
  3497. operand2 = (0x80 & operand2) ? (0xFFFFFF00 | operand2) : operand2;
  3498. RD = RN + operand2;
  3499. }
  3500. cpu->Reg[15] += cpu->GetInstructionSize();
  3501. INC_PC(sizeof(uxtab_inst));
  3502. FETCH_INST;
  3503. GOTO_NEXT_INST;
  3504. }
  3505. SXTAB16_INST:
  3506. SXTB16_INST : {
  3507. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3508. sxtab_inst* const inst_cream = (sxtab_inst*)inst_base->component;
  3509. const u8 rotation = inst_cream->rotate * 8;
  3510. u32 rm_val = RM;
  3511. u32 rn_val = RN;
  3512. if (rotation)
  3513. rm_val = ((rm_val << (32 - rotation)) | (rm_val >> rotation));
  3514. // SXTB16
  3515. if (inst_cream->Rn == 15) {
  3516. u32 lo = (u32)(s8)rm_val;
  3517. u32 hi = (u32)(s8)(rm_val >> 16);
  3518. RD = (lo & 0xFFFF) | (hi << 16);
  3519. }
  3520. // SXTAB16
  3521. else {
  3522. u32 lo = rn_val + (u32)(s8)(rm_val & 0xFF);
  3523. u32 hi = (rn_val >> 16) + (u32)(s8)((rm_val >> 16) & 0xFF);
  3524. RD = (lo & 0xFFFF) | (hi << 16);
  3525. }
  3526. }
  3527. cpu->Reg[15] += cpu->GetInstructionSize();
  3528. INC_PC(sizeof(sxtab_inst));
  3529. FETCH_INST;
  3530. GOTO_NEXT_INST;
  3531. }
  3532. SXTAH_INST : {
  3533. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3534. sxtah_inst* inst_cream = (sxtah_inst*)inst_base->component;
  3535. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xffff;
  3536. // Sign extend for half
  3537. operand2 = (0x8000 & operand2) ? (0xFFFF0000 | operand2) : operand2;
  3538. RD = RN + operand2;
  3539. }
  3540. cpu->Reg[15] += cpu->GetInstructionSize();
  3541. INC_PC(sizeof(sxtah_inst));
  3542. FETCH_INST;
  3543. GOTO_NEXT_INST;
  3544. }
  3545. TEQ_INST : {
  3546. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3547. teq_inst* const inst_cream = (teq_inst*)inst_base->component;
  3548. u32 lop = RN;
  3549. u32 rop = SHIFTER_OPERAND;
  3550. if (inst_cream->Rn == 15)
  3551. lop += cpu->GetInstructionSize() * 2;
  3552. u32 result = lop ^ rop;
  3553. UPDATE_NFLAG(result);
  3554. UPDATE_ZFLAG(result);
  3555. UPDATE_CFLAG_WITH_SC;
  3556. }
  3557. cpu->Reg[15] += cpu->GetInstructionSize();
  3558. INC_PC(sizeof(teq_inst));
  3559. FETCH_INST;
  3560. GOTO_NEXT_INST;
  3561. }
  3562. TST_INST : {
  3563. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3564. tst_inst* const inst_cream = (tst_inst*)inst_base->component;
  3565. u32 lop = RN;
  3566. u32 rop = SHIFTER_OPERAND;
  3567. if (inst_cream->Rn == 15)
  3568. lop += cpu->GetInstructionSize() * 2;
  3569. u32 result = lop & rop;
  3570. UPDATE_NFLAG(result);
  3571. UPDATE_ZFLAG(result);
  3572. UPDATE_CFLAG_WITH_SC;
  3573. }
  3574. cpu->Reg[15] += cpu->GetInstructionSize();
  3575. INC_PC(sizeof(tst_inst));
  3576. FETCH_INST;
  3577. GOTO_NEXT_INST;
  3578. }
  3579. UADD8_INST:
  3580. UADD16_INST:
  3581. UADDSUBX_INST:
  3582. USUB8_INST:
  3583. USUB16_INST:
  3584. USUBADDX_INST : {
  3585. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3586. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  3587. const u8 op2 = inst_cream->op2;
  3588. const u32 rm_val = RM;
  3589. const u32 rn_val = RN;
  3590. s32 lo_result = 0;
  3591. s32 hi_result = 0;
  3592. // UADD16
  3593. if (op2 == 0x00) {
  3594. lo_result = (rn_val & 0xFFFF) + (rm_val & 0xFFFF);
  3595. hi_result = ((rn_val >> 16) & 0xFFFF) + ((rm_val >> 16) & 0xFFFF);
  3596. if (lo_result & 0xFFFF0000) {
  3597. cpu->Cpsr |= (1 << 16);
  3598. cpu->Cpsr |= (1 << 17);
  3599. } else {
  3600. cpu->Cpsr &= ~(1 << 16);
  3601. cpu->Cpsr &= ~(1 << 17);
  3602. }
  3603. if (hi_result & 0xFFFF0000) {
  3604. cpu->Cpsr |= (1 << 18);
  3605. cpu->Cpsr |= (1 << 19);
  3606. } else {
  3607. cpu->Cpsr &= ~(1 << 18);
  3608. cpu->Cpsr &= ~(1 << 19);
  3609. }
  3610. }
  3611. // UASX
  3612. else if (op2 == 0x01) {
  3613. lo_result = (rn_val & 0xFFFF) - ((rm_val >> 16) & 0xFFFF);
  3614. hi_result = ((rn_val >> 16) & 0xFFFF) + (rm_val & 0xFFFF);
  3615. if (lo_result >= 0) {
  3616. cpu->Cpsr |= (1 << 16);
  3617. cpu->Cpsr |= (1 << 17);
  3618. } else {
  3619. cpu->Cpsr &= ~(1 << 16);
  3620. cpu->Cpsr &= ~(1 << 17);
  3621. }
  3622. if (hi_result >= 0x10000) {
  3623. cpu->Cpsr |= (1 << 18);
  3624. cpu->Cpsr |= (1 << 19);
  3625. } else {
  3626. cpu->Cpsr &= ~(1 << 18);
  3627. cpu->Cpsr &= ~(1 << 19);
  3628. }
  3629. }
  3630. // USAX
  3631. else if (op2 == 0x02) {
  3632. lo_result = (rn_val & 0xFFFF) + ((rm_val >> 16) & 0xFFFF);
  3633. hi_result = ((rn_val >> 16) & 0xFFFF) - (rm_val & 0xFFFF);
  3634. if (lo_result >= 0x10000) {
  3635. cpu->Cpsr |= (1 << 16);
  3636. cpu->Cpsr |= (1 << 17);
  3637. } else {
  3638. cpu->Cpsr &= ~(1 << 16);
  3639. cpu->Cpsr &= ~(1 << 17);
  3640. }
  3641. if (hi_result >= 0) {
  3642. cpu->Cpsr |= (1 << 18);
  3643. cpu->Cpsr |= (1 << 19);
  3644. } else {
  3645. cpu->Cpsr &= ~(1 << 18);
  3646. cpu->Cpsr &= ~(1 << 19);
  3647. }
  3648. }
  3649. // USUB16
  3650. else if (op2 == 0x03) {
  3651. lo_result = (rn_val & 0xFFFF) - (rm_val & 0xFFFF);
  3652. hi_result = ((rn_val >> 16) & 0xFFFF) - ((rm_val >> 16) & 0xFFFF);
  3653. if ((lo_result & 0xFFFF0000) == 0) {
  3654. cpu->Cpsr |= (1 << 16);
  3655. cpu->Cpsr |= (1 << 17);
  3656. } else {
  3657. cpu->Cpsr &= ~(1 << 16);
  3658. cpu->Cpsr &= ~(1 << 17);
  3659. }
  3660. if ((hi_result & 0xFFFF0000) == 0) {
  3661. cpu->Cpsr |= (1 << 18);
  3662. cpu->Cpsr |= (1 << 19);
  3663. } else {
  3664. cpu->Cpsr &= ~(1 << 18);
  3665. cpu->Cpsr &= ~(1 << 19);
  3666. }
  3667. }
  3668. // UADD8
  3669. else if (op2 == 0x04) {
  3670. s16 sum1 = (rn_val & 0xFF) + (rm_val & 0xFF);
  3671. s16 sum2 = ((rn_val >> 8) & 0xFF) + ((rm_val >> 8) & 0xFF);
  3672. s16 sum3 = ((rn_val >> 16) & 0xFF) + ((rm_val >> 16) & 0xFF);
  3673. s16 sum4 = ((rn_val >> 24) & 0xFF) + ((rm_val >> 24) & 0xFF);
  3674. if (sum1 >= 0x100)
  3675. cpu->Cpsr |= (1 << 16);
  3676. else
  3677. cpu->Cpsr &= ~(1 << 16);
  3678. if (sum2 >= 0x100)
  3679. cpu->Cpsr |= (1 << 17);
  3680. else
  3681. cpu->Cpsr &= ~(1 << 17);
  3682. if (sum3 >= 0x100)
  3683. cpu->Cpsr |= (1 << 18);
  3684. else
  3685. cpu->Cpsr &= ~(1 << 18);
  3686. if (sum4 >= 0x100)
  3687. cpu->Cpsr |= (1 << 19);
  3688. else
  3689. cpu->Cpsr &= ~(1 << 19);
  3690. lo_result = ((sum1 & 0xFF) | (sum2 & 0xFF) << 8);
  3691. hi_result = ((sum3 & 0xFF) | (sum4 & 0xFF) << 8);
  3692. }
  3693. // USUB8
  3694. else if (op2 == 0x07) {
  3695. s16 diff1 = (rn_val & 0xFF) - (rm_val & 0xFF);
  3696. s16 diff2 = ((rn_val >> 8) & 0xFF) - ((rm_val >> 8) & 0xFF);
  3697. s16 diff3 = ((rn_val >> 16) & 0xFF) - ((rm_val >> 16) & 0xFF);
  3698. s16 diff4 = ((rn_val >> 24) & 0xFF) - ((rm_val >> 24) & 0xFF);
  3699. if (diff1 >= 0)
  3700. cpu->Cpsr |= (1 << 16);
  3701. else
  3702. cpu->Cpsr &= ~(1 << 16);
  3703. if (diff2 >= 0)
  3704. cpu->Cpsr |= (1 << 17);
  3705. else
  3706. cpu->Cpsr &= ~(1 << 17);
  3707. if (diff3 >= 0)
  3708. cpu->Cpsr |= (1 << 18);
  3709. else
  3710. cpu->Cpsr &= ~(1 << 18);
  3711. if (diff4 >= 0)
  3712. cpu->Cpsr |= (1 << 19);
  3713. else
  3714. cpu->Cpsr &= ~(1 << 19);
  3715. lo_result = (diff1 & 0xFF) | ((diff2 & 0xFF) << 8);
  3716. hi_result = (diff3 & 0xFF) | ((diff4 & 0xFF) << 8);
  3717. }
  3718. RD = (lo_result & 0xFFFF) | ((hi_result & 0xFFFF) << 16);
  3719. }
  3720. cpu->Reg[15] += cpu->GetInstructionSize();
  3721. INC_PC(sizeof(generic_arm_inst));
  3722. FETCH_INST;
  3723. GOTO_NEXT_INST;
  3724. }
  3725. UHADD8_INST:
  3726. UHADD16_INST:
  3727. UHADDSUBX_INST:
  3728. UHSUBADDX_INST:
  3729. UHSUB8_INST:
  3730. UHSUB16_INST : {
  3731. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3732. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  3733. const u32 rm_val = RM;
  3734. const u32 rn_val = RN;
  3735. const u8 op2 = inst_cream->op2;
  3736. if (op2 == 0x00 || op2 == 0x01 || op2 == 0x02 || op2 == 0x03) {
  3737. u32 lo_val = 0;
  3738. u32 hi_val = 0;
  3739. // UHADD16
  3740. if (op2 == 0x00) {
  3741. lo_val = (rn_val & 0xFFFF) + (rm_val & 0xFFFF);
  3742. hi_val = ((rn_val >> 16) & 0xFFFF) + ((rm_val >> 16) & 0xFFFF);
  3743. }
  3744. // UHASX
  3745. else if (op2 == 0x01) {
  3746. lo_val = (rn_val & 0xFFFF) - ((rm_val >> 16) & 0xFFFF);
  3747. hi_val = ((rn_val >> 16) & 0xFFFF) + (rm_val & 0xFFFF);
  3748. }
  3749. // UHSAX
  3750. else if (op2 == 0x02) {
  3751. lo_val = (rn_val & 0xFFFF) + ((rm_val >> 16) & 0xFFFF);
  3752. hi_val = ((rn_val >> 16) & 0xFFFF) - (rm_val & 0xFFFF);
  3753. }
  3754. // UHSUB16
  3755. else if (op2 == 0x03) {
  3756. lo_val = (rn_val & 0xFFFF) - (rm_val & 0xFFFF);
  3757. hi_val = ((rn_val >> 16) & 0xFFFF) - ((rm_val >> 16) & 0xFFFF);
  3758. }
  3759. lo_val >>= 1;
  3760. hi_val >>= 1;
  3761. RD = (lo_val & 0xFFFF) | ((hi_val & 0xFFFF) << 16);
  3762. } else if (op2 == 0x04 || op2 == 0x07) {
  3763. u32 sum1;
  3764. u32 sum2;
  3765. u32 sum3;
  3766. u32 sum4;
  3767. // UHADD8
  3768. if (op2 == 0x04) {
  3769. sum1 = (rn_val & 0xFF) + (rm_val & 0xFF);
  3770. sum2 = ((rn_val >> 8) & 0xFF) + ((rm_val >> 8) & 0xFF);
  3771. sum3 = ((rn_val >> 16) & 0xFF) + ((rm_val >> 16) & 0xFF);
  3772. sum4 = ((rn_val >> 24) & 0xFF) + ((rm_val >> 24) & 0xFF);
  3773. }
  3774. // UHSUB8
  3775. else {
  3776. sum1 = (rn_val & 0xFF) - (rm_val & 0xFF);
  3777. sum2 = ((rn_val >> 8) & 0xFF) - ((rm_val >> 8) & 0xFF);
  3778. sum3 = ((rn_val >> 16) & 0xFF) - ((rm_val >> 16) & 0xFF);
  3779. sum4 = ((rn_val >> 24) & 0xFF) - ((rm_val >> 24) & 0xFF);
  3780. }
  3781. sum1 >>= 1;
  3782. sum2 >>= 1;
  3783. sum3 >>= 1;
  3784. sum4 >>= 1;
  3785. RD = (sum1 & 0xFF) | ((sum2 & 0xFF) << 8) | ((sum3 & 0xFF) << 16) |
  3786. ((sum4 & 0xFF) << 24);
  3787. }
  3788. }
  3789. cpu->Reg[15] += cpu->GetInstructionSize();
  3790. INC_PC(sizeof(generic_arm_inst));
  3791. FETCH_INST;
  3792. GOTO_NEXT_INST;
  3793. }
  3794. UMAAL_INST : {
  3795. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3796. umaal_inst* const inst_cream = (umaal_inst*)inst_base->component;
  3797. const u64 rm = RM;
  3798. const u64 rn = RN;
  3799. const u64 rd_lo = RDLO;
  3800. const u64 rd_hi = RDHI;
  3801. const u64 result = (rm * rn) + rd_lo + rd_hi;
  3802. RDLO = (result & 0xFFFFFFFF);
  3803. RDHI = ((result >> 32) & 0xFFFFFFFF);
  3804. }
  3805. cpu->Reg[15] += cpu->GetInstructionSize();
  3806. INC_PC(sizeof(umaal_inst));
  3807. FETCH_INST;
  3808. GOTO_NEXT_INST;
  3809. }
  3810. UMLAL_INST : {
  3811. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3812. umlal_inst* inst_cream = (umlal_inst*)inst_base->component;
  3813. unsigned long long int rm = RM;
  3814. unsigned long long int rs = RS;
  3815. unsigned long long int rst = rm * rs;
  3816. unsigned long long int add = ((unsigned long long)RDHI) << 32;
  3817. add += RDLO;
  3818. rst += add;
  3819. RDLO = BITS(rst, 0, 31);
  3820. RDHI = BITS(rst, 32, 63);
  3821. if (inst_cream->S) {
  3822. cpu->NFlag = BIT(RDHI, 31);
  3823. cpu->ZFlag = (RDHI == 0 && RDLO == 0);
  3824. }
  3825. }
  3826. cpu->Reg[15] += cpu->GetInstructionSize();
  3827. INC_PC(sizeof(umlal_inst));
  3828. FETCH_INST;
  3829. GOTO_NEXT_INST;
  3830. }
  3831. UMULL_INST : {
  3832. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3833. umull_inst* inst_cream = (umull_inst*)inst_base->component;
  3834. unsigned long long int rm = RM;
  3835. unsigned long long int rs = RS;
  3836. unsigned long long int rst = rm * rs;
  3837. RDHI = BITS(rst, 32, 63);
  3838. RDLO = BITS(rst, 0, 31);
  3839. if (inst_cream->S) {
  3840. cpu->NFlag = BIT(RDHI, 31);
  3841. cpu->ZFlag = (RDHI == 0 && RDLO == 0);
  3842. }
  3843. }
  3844. cpu->Reg[15] += cpu->GetInstructionSize();
  3845. INC_PC(sizeof(umull_inst));
  3846. FETCH_INST;
  3847. GOTO_NEXT_INST;
  3848. }
  3849. B_2_THUMB : {
  3850. b_2_thumb* inst_cream = (b_2_thumb*)inst_base->component;
  3851. cpu->Reg[15] = cpu->Reg[15] + 4 + inst_cream->imm;
  3852. INC_PC(sizeof(b_2_thumb));
  3853. goto DISPATCH;
  3854. }
  3855. B_COND_THUMB : {
  3856. b_cond_thumb* inst_cream = (b_cond_thumb*)inst_base->component;
  3857. if (CondPassed(cpu, inst_cream->cond))
  3858. cpu->Reg[15] = cpu->Reg[15] + 4 + inst_cream->imm;
  3859. else
  3860. cpu->Reg[15] += 2;
  3861. INC_PC(sizeof(b_cond_thumb));
  3862. goto DISPATCH;
  3863. }
  3864. BL_1_THUMB : {
  3865. bl_1_thumb* inst_cream = (bl_1_thumb*)inst_base->component;
  3866. cpu->Reg[14] = cpu->Reg[15] + 4 + inst_cream->imm;
  3867. cpu->Reg[15] += cpu->GetInstructionSize();
  3868. INC_PC(sizeof(bl_1_thumb));
  3869. FETCH_INST;
  3870. GOTO_NEXT_INST;
  3871. }
  3872. BL_2_THUMB : {
  3873. bl_2_thumb* inst_cream = (bl_2_thumb*)inst_base->component;
  3874. int tmp = ((cpu->Reg[15] + 2) | 1);
  3875. cpu->Reg[15] = (cpu->Reg[14] + inst_cream->imm);
  3876. cpu->Reg[14] = tmp;
  3877. INC_PC(sizeof(bl_2_thumb));
  3878. goto DISPATCH;
  3879. }
  3880. BLX_1_THUMB : {
  3881. // BLX 1 for armv5t and above
  3882. u32 tmp = cpu->Reg[15];
  3883. blx_1_thumb* inst_cream = (blx_1_thumb*)inst_base->component;
  3884. cpu->Reg[15] = (cpu->Reg[14] + inst_cream->imm) & 0xFFFFFFFC;
  3885. cpu->Reg[14] = ((tmp + 2) | 1);
  3886. cpu->TFlag = 0;
  3887. INC_PC(sizeof(blx_1_thumb));
  3888. goto DISPATCH;
  3889. }
  3890. UQADD8_INST:
  3891. UQADD16_INST:
  3892. UQADDSUBX_INST:
  3893. UQSUB8_INST:
  3894. UQSUB16_INST:
  3895. UQSUBADDX_INST : {
  3896. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3897. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  3898. const u8 op2 = inst_cream->op2;
  3899. const u32 rm_val = RM;
  3900. const u32 rn_val = RN;
  3901. u16 lo_val = 0;
  3902. u16 hi_val = 0;
  3903. // UQADD16
  3904. if (op2 == 0x00) {
  3905. lo_val = ARMul_UnsignedSaturatedAdd16(rn_val & 0xFFFF, rm_val & 0xFFFF);
  3906. hi_val = ARMul_UnsignedSaturatedAdd16((rn_val >> 16) & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  3907. }
  3908. // UQASX
  3909. else if (op2 == 0x01) {
  3910. lo_val = ARMul_UnsignedSaturatedSub16(rn_val & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  3911. hi_val = ARMul_UnsignedSaturatedAdd16((rn_val >> 16) & 0xFFFF, rm_val & 0xFFFF);
  3912. }
  3913. // UQSAX
  3914. else if (op2 == 0x02) {
  3915. lo_val = ARMul_UnsignedSaturatedAdd16(rn_val & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  3916. hi_val = ARMul_UnsignedSaturatedSub16((rn_val >> 16) & 0xFFFF, rm_val & 0xFFFF);
  3917. }
  3918. // UQSUB16
  3919. else if (op2 == 0x03) {
  3920. lo_val = ARMul_UnsignedSaturatedSub16(rn_val & 0xFFFF, rm_val & 0xFFFF);
  3921. hi_val = ARMul_UnsignedSaturatedSub16((rn_val >> 16) & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  3922. }
  3923. // UQADD8
  3924. else if (op2 == 0x04) {
  3925. lo_val = ARMul_UnsignedSaturatedAdd8(rn_val, rm_val) |
  3926. ARMul_UnsignedSaturatedAdd8(rn_val >> 8, rm_val >> 8) << 8;
  3927. hi_val = ARMul_UnsignedSaturatedAdd8(rn_val >> 16, rm_val >> 16) |
  3928. ARMul_UnsignedSaturatedAdd8(rn_val >> 24, rm_val >> 24) << 8;
  3929. }
  3930. // UQSUB8
  3931. else {
  3932. lo_val = ARMul_UnsignedSaturatedSub8(rn_val, rm_val) |
  3933. ARMul_UnsignedSaturatedSub8(rn_val >> 8, rm_val >> 8) << 8;
  3934. hi_val = ARMul_UnsignedSaturatedSub8(rn_val >> 16, rm_val >> 16) |
  3935. ARMul_UnsignedSaturatedSub8(rn_val >> 24, rm_val >> 24) << 8;
  3936. }
  3937. RD = ((lo_val & 0xFFFF) | hi_val << 16);
  3938. }
  3939. cpu->Reg[15] += cpu->GetInstructionSize();
  3940. INC_PC(sizeof(generic_arm_inst));
  3941. FETCH_INST;
  3942. GOTO_NEXT_INST;
  3943. }
  3944. USAD8_INST:
  3945. USADA8_INST : {
  3946. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3947. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  3948. const u8 ra_idx = inst_cream->Ra;
  3949. const u32 rm_val = RM;
  3950. const u32 rn_val = RN;
  3951. const u8 diff1 = ARMul_UnsignedAbsoluteDifference(rn_val & 0xFF, rm_val & 0xFF);
  3952. const u8 diff2 =
  3953. ARMul_UnsignedAbsoluteDifference((rn_val >> 8) & 0xFF, (rm_val >> 8) & 0xFF);
  3954. const u8 diff3 =
  3955. ARMul_UnsignedAbsoluteDifference((rn_val >> 16) & 0xFF, (rm_val >> 16) & 0xFF);
  3956. const u8 diff4 =
  3957. ARMul_UnsignedAbsoluteDifference((rn_val >> 24) & 0xFF, (rm_val >> 24) & 0xFF);
  3958. u32 finalDif = (diff1 + diff2 + diff3 + diff4);
  3959. // Op is USADA8 if true.
  3960. if (ra_idx != 15)
  3961. finalDif += cpu->Reg[ra_idx];
  3962. RD = finalDif;
  3963. }
  3964. cpu->Reg[15] += cpu->GetInstructionSize();
  3965. INC_PC(sizeof(generic_arm_inst));
  3966. FETCH_INST;
  3967. GOTO_NEXT_INST;
  3968. }
  3969. USAT_INST : {
  3970. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3971. ssat_inst* const inst_cream = (ssat_inst*)inst_base->component;
  3972. u8 shift_type = inst_cream->shift_type;
  3973. u8 shift_amount = inst_cream->imm5;
  3974. u32 rn_val = RN;
  3975. // 32-bit ASR is encoded as an amount of 0.
  3976. if (shift_type == 1 && shift_amount == 0)
  3977. shift_amount = 31;
  3978. if (shift_type == 0)
  3979. rn_val <<= shift_amount;
  3980. else if (shift_type == 1)
  3981. rn_val = ((s32)rn_val >> shift_amount);
  3982. bool saturated = false;
  3983. rn_val = ARMul_UnsignedSatQ(rn_val, inst_cream->sat_imm, &saturated);
  3984. if (saturated)
  3985. cpu->Cpsr |= (1 << 27);
  3986. RD = rn_val;
  3987. }
  3988. cpu->Reg[15] += cpu->GetInstructionSize();
  3989. INC_PC(sizeof(ssat_inst));
  3990. FETCH_INST;
  3991. GOTO_NEXT_INST;
  3992. }
  3993. USAT16_INST : {
  3994. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  3995. ssat_inst* const inst_cream = (ssat_inst*)inst_base->component;
  3996. const u8 saturate_to = inst_cream->sat_imm;
  3997. bool sat1 = false;
  3998. bool sat2 = false;
  3999. RD = (ARMul_UnsignedSatQ((s16)RN, saturate_to, &sat1) & 0xFFFF) |
  4000. ARMul_UnsignedSatQ((s32)RN >> 16, saturate_to, &sat2) << 16;
  4001. if (sat1 || sat2)
  4002. cpu->Cpsr |= (1 << 27);
  4003. }
  4004. cpu->Reg[15] += cpu->GetInstructionSize();
  4005. INC_PC(sizeof(ssat_inst));
  4006. FETCH_INST;
  4007. GOTO_NEXT_INST;
  4008. }
  4009. UXTAB16_INST:
  4010. UXTB16_INST : {
  4011. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  4012. uxtab_inst* const inst_cream = (uxtab_inst*)inst_base->component;
  4013. const u8 rn_idx = inst_cream->Rn;
  4014. const u32 rm_val = RM;
  4015. const u32 rotation = inst_cream->rotate * 8;
  4016. const u32 rotated_rm = ((rm_val << (32 - rotation)) | (rm_val >> rotation));
  4017. // UXTB16, otherwise UXTAB16
  4018. if (rn_idx == 15) {
  4019. RD = rotated_rm & 0x00FF00FF;
  4020. } else {
  4021. const u32 rn_val = RN;
  4022. const u8 lo_rotated = (rotated_rm & 0xFF);
  4023. const u16 lo_result = (rn_val & 0xFFFF) + (u16)lo_rotated;
  4024. const u8 hi_rotated = (rotated_rm >> 16) & 0xFF;
  4025. const u16 hi_result = (rn_val >> 16) + (u16)hi_rotated;
  4026. RD = ((hi_result << 16) | (lo_result & 0xFFFF));
  4027. }
  4028. }
  4029. cpu->Reg[15] += cpu->GetInstructionSize();
  4030. INC_PC(sizeof(uxtab_inst));
  4031. FETCH_INST;
  4032. GOTO_NEXT_INST;
  4033. }
  4034. WFE_INST : {
  4035. // Stubbed, as WFE is a hint instruction.
  4036. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  4037. LOG_TRACE(Core_ARM11, "WFE executed.");
  4038. }
  4039. cpu->Reg[15] += cpu->GetInstructionSize();
  4040. INC_PC_STUB;
  4041. FETCH_INST;
  4042. GOTO_NEXT_INST;
  4043. }
  4044. WFI_INST : {
  4045. // Stubbed, as WFI is a hint instruction.
  4046. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  4047. LOG_TRACE(Core_ARM11, "WFI executed.");
  4048. }
  4049. cpu->Reg[15] += cpu->GetInstructionSize();
  4050. INC_PC_STUB;
  4051. FETCH_INST;
  4052. GOTO_NEXT_INST;
  4053. }
  4054. YIELD_INST : {
  4055. // Stubbed, as YIELD is a hint instruction.
  4056. if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
  4057. LOG_TRACE(Core_ARM11, "YIELD executed.");
  4058. }
  4059. cpu->Reg[15] += cpu->GetInstructionSize();
  4060. INC_PC_STUB;
  4061. FETCH_INST;
  4062. GOTO_NEXT_INST;
  4063. }
  4064. #define VFP_INTERPRETER_IMPL
  4065. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  4066. #undef VFP_INTERPRETER_IMPL
  4067. END : {
  4068. SAVE_NZCVT;
  4069. cpu->NumInstrsToExecute = 0;
  4070. return num_instrs;
  4071. }
  4072. INIT_INST_LENGTH : {
  4073. cpu->NumInstrsToExecute = 0;
  4074. return num_instrs;
  4075. }
  4076. }