arm_dyncom_interpreter.cpp 172 KB

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