arm_dyncom_interpreter.cpp 221 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999
  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/logging/log.h"
  8. #include "common/profiler.h"
  9. #include "core/mem_map.h"
  10. #include "core/hle/svc.h"
  11. #include "core/arm/disassembler/arm_disasm.h"
  12. #include "core/arm/dyncom/arm_dyncom_interpreter.h"
  13. #include "core/arm/dyncom/arm_dyncom_thumb.h"
  14. #include "core/arm/dyncom/arm_dyncom_run.h"
  15. #include "core/arm/skyeye_common/armdefs.h"
  16. #include "core/arm/skyeye_common/armmmu.h"
  17. #include "core/arm/skyeye_common/vfp/vfp.h"
  18. Common::Profiling::TimingCategory profile_execute("DynCom::Execute");
  19. Common::Profiling::TimingCategory profile_decode("DynCom::Decode");
  20. enum {
  21. COND = (1 << 0),
  22. NON_BRANCH = (1 << 1),
  23. DIRECT_BRANCH = (1 << 2),
  24. INDIRECT_BRANCH = (1 << 3),
  25. CALL = (1 << 4),
  26. RET = (1 << 5),
  27. END_OF_PAGE = (1 << 6),
  28. THUMB = (1 << 7)
  29. };
  30. #define RM BITS(sht_oper, 0, 3)
  31. #define RS BITS(sht_oper, 8, 11)
  32. #define glue(x, y) x ## y
  33. #define DPO(s) glue(DataProcessingOperands, s)
  34. #define ROTATE_RIGHT(n, i, l) ((n << (l - i)) | (n >> i))
  35. #define ROTATE_LEFT(n, i, l) ((n >> (l - i)) | (n << i))
  36. #define ROTATE_RIGHT_32(n, i) ROTATE_RIGHT(n, i, 32)
  37. #define ROTATE_LEFT_32(n, i) ROTATE_LEFT(n, i, 32)
  38. typedef unsigned int (*shtop_fp_t)(ARMul_State* cpu, unsigned int sht_oper);
  39. // Defines a reservation granule of 2 words, which protects the first 2 words starting at the tag.
  40. // This is the smallest granule allowed by the v7 spec, and is coincidentally just large enough to
  41. // support LDR/STREXD.
  42. static const ARMword RESERVATION_GRANULE_MASK = 0xFFFFFFF8;
  43. // Exclusive memory access
  44. static int exclusive_detect(ARMul_State* state, ARMword addr) {
  45. if(state->exclusive_tag == (addr & RESERVATION_GRANULE_MASK))
  46. return 0;
  47. else
  48. return -1;
  49. }
  50. static void add_exclusive_addr(ARMul_State* state, ARMword addr){
  51. state->exclusive_tag = addr & RESERVATION_GRANULE_MASK;
  52. return;
  53. }
  54. static void remove_exclusive(ARMul_State* state, ARMword addr){
  55. state->exclusive_tag = 0xFFFFFFFF;
  56. }
  57. static unsigned int DPO(Immediate)(ARMul_State* cpu, unsigned int sht_oper) {
  58. unsigned int immed_8 = BITS(sht_oper, 0, 7);
  59. unsigned int rotate_imm = BITS(sht_oper, 8, 11);
  60. unsigned int shifter_operand = ROTATE_RIGHT_32(immed_8, rotate_imm * 2);
  61. if (rotate_imm == 0)
  62. cpu->shifter_carry_out = cpu->CFlag;
  63. else
  64. cpu->shifter_carry_out = BIT(shifter_operand, 31);
  65. return shifter_operand;
  66. }
  67. static unsigned int DPO(Register)(ARMul_State* cpu, unsigned int sht_oper) {
  68. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  69. unsigned int shifter_operand = rm;
  70. cpu->shifter_carry_out = cpu->CFlag;
  71. return shifter_operand;
  72. }
  73. static unsigned int DPO(LogicalShiftLeftByImmediate)(ARMul_State* cpu, unsigned int sht_oper) {
  74. int shift_imm = BITS(sht_oper, 7, 11);
  75. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  76. unsigned int shifter_operand;
  77. if (shift_imm == 0) {
  78. shifter_operand = rm;
  79. cpu->shifter_carry_out = cpu->CFlag;
  80. } else {
  81. shifter_operand = rm << shift_imm;
  82. cpu->shifter_carry_out = BIT(rm, 32 - shift_imm);
  83. }
  84. return shifter_operand;
  85. }
  86. static unsigned int DPO(LogicalShiftLeftByRegister)(ARMul_State* cpu, unsigned int sht_oper) {
  87. int shifter_operand;
  88. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  89. unsigned int rs = CHECK_READ_REG15(cpu, RS);
  90. if (BITS(rs, 0, 7) == 0) {
  91. shifter_operand = rm;
  92. cpu->shifter_carry_out = cpu->CFlag;
  93. } else if (BITS(rs, 0, 7) < 32) {
  94. shifter_operand = rm << BITS(rs, 0, 7);
  95. cpu->shifter_carry_out = BIT(rm, 32 - BITS(rs, 0, 7));
  96. } else if (BITS(rs, 0, 7) == 32) {
  97. shifter_operand = 0;
  98. cpu->shifter_carry_out = BIT(rm, 0);
  99. } else {
  100. shifter_operand = 0;
  101. cpu->shifter_carry_out = 0;
  102. }
  103. return shifter_operand;
  104. }
  105. static unsigned int DPO(LogicalShiftRightByImmediate)(ARMul_State* cpu, unsigned int sht_oper) {
  106. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  107. unsigned int shifter_operand;
  108. int shift_imm = BITS(sht_oper, 7, 11);
  109. if (shift_imm == 0) {
  110. shifter_operand = 0;
  111. cpu->shifter_carry_out = BIT(rm, 31);
  112. } else {
  113. shifter_operand = rm >> shift_imm;
  114. cpu->shifter_carry_out = BIT(rm, shift_imm - 1);
  115. }
  116. return shifter_operand;
  117. }
  118. static unsigned int DPO(LogicalShiftRightByRegister)(ARMul_State* cpu, unsigned int sht_oper) {
  119. unsigned int rs = CHECK_READ_REG15(cpu, RS);
  120. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  121. unsigned int shifter_operand;
  122. if (BITS(rs, 0, 7) == 0) {
  123. shifter_operand = rm;
  124. cpu->shifter_carry_out = cpu->CFlag;
  125. } else if (BITS(rs, 0, 7) < 32) {
  126. shifter_operand = rm >> BITS(rs, 0, 7);
  127. cpu->shifter_carry_out = BIT(rm, BITS(rs, 0, 7) - 1);
  128. } else if (BITS(rs, 0, 7) == 32) {
  129. shifter_operand = 0;
  130. cpu->shifter_carry_out = BIT(rm, 31);
  131. } else {
  132. shifter_operand = 0;
  133. cpu->shifter_carry_out = 0;
  134. }
  135. return shifter_operand;
  136. }
  137. static unsigned int DPO(ArithmeticShiftRightByImmediate)(ARMul_State* cpu, unsigned int sht_oper) {
  138. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  139. unsigned int shifter_operand;
  140. int shift_imm = BITS(sht_oper, 7, 11);
  141. if (shift_imm == 0) {
  142. if (BIT(rm, 31) == 0)
  143. shifter_operand = 0;
  144. else
  145. shifter_operand = 0xFFFFFFFF;
  146. cpu->shifter_carry_out = BIT(rm, 31);
  147. } else {
  148. shifter_operand = static_cast<int>(rm) >> shift_imm;
  149. cpu->shifter_carry_out = BIT(rm, shift_imm - 1);
  150. }
  151. return shifter_operand;
  152. }
  153. static unsigned int DPO(ArithmeticShiftRightByRegister)(ARMul_State* cpu, unsigned int sht_oper) {
  154. unsigned int rs = CHECK_READ_REG15(cpu, RS);
  155. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  156. unsigned int shifter_operand;
  157. if (BITS(rs, 0, 7) == 0) {
  158. shifter_operand = rm;
  159. cpu->shifter_carry_out = cpu->CFlag;
  160. } else if (BITS(rs, 0, 7) < 32) {
  161. shifter_operand = static_cast<int>(rm) >> BITS(rs, 0, 7);
  162. cpu->shifter_carry_out = BIT(rm, BITS(rs, 0, 7) - 1);
  163. } else {
  164. if (BIT(rm, 31) == 0)
  165. shifter_operand = 0;
  166. else
  167. shifter_operand = 0xffffffff;
  168. cpu->shifter_carry_out = BIT(rm, 31);
  169. }
  170. return shifter_operand;
  171. }
  172. static unsigned int DPO(RotateRightByImmediate)(ARMul_State* cpu, unsigned int sht_oper) {
  173. unsigned int shifter_operand;
  174. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  175. int shift_imm = BITS(sht_oper, 7, 11);
  176. if (shift_imm == 0) {
  177. shifter_operand = (cpu->CFlag << 31) | (rm >> 1);
  178. cpu->shifter_carry_out = BIT(rm, 0);
  179. } else {
  180. shifter_operand = ROTATE_RIGHT_32(rm, shift_imm);
  181. cpu->shifter_carry_out = BIT(rm, shift_imm - 1);
  182. }
  183. return shifter_operand;
  184. }
  185. static unsigned int DPO(RotateRightByRegister)(ARMul_State* cpu, unsigned int sht_oper) {
  186. unsigned int rm = CHECK_READ_REG15(cpu, RM);
  187. unsigned int rs = CHECK_READ_REG15(cpu, RS);
  188. unsigned int shifter_operand;
  189. if (BITS(rs, 0, 7) == 0) {
  190. shifter_operand = rm;
  191. cpu->shifter_carry_out = cpu->CFlag;
  192. } else if (BITS(rs, 0, 4) == 0) {
  193. shifter_operand = rm;
  194. cpu->shifter_carry_out = BIT(rm, 31);
  195. } else {
  196. shifter_operand = ROTATE_RIGHT_32(rm, BITS(rs, 0, 4));
  197. cpu->shifter_carry_out = BIT(rm, BITS(rs, 0, 4) - 1);
  198. }
  199. return shifter_operand;
  200. }
  201. typedef void (*get_addr_fp_t)(ARMul_State *cpu, unsigned int inst, unsigned int &virt_addr, unsigned int rw);
  202. typedef struct _ldst_inst {
  203. unsigned int inst;
  204. get_addr_fp_t get_addr;
  205. } ldst_inst;
  206. #define DEBUG_MSG LOG_DEBUG(Core_ARM11, "inst is %x", inst); CITRA_IGNORE_EXIT(0)
  207. int CondPassed(ARMul_State* cpu, unsigned int cond);
  208. #define LnSWoUB(s) glue(LnSWoUB, s)
  209. #define MLnS(s) glue(MLnS, s)
  210. #define LdnStM(s) glue(LdnStM, s)
  211. #define W_BIT BIT(inst, 21)
  212. #define U_BIT BIT(inst, 23)
  213. #define I_BIT BIT(inst, 25)
  214. #define P_BIT BIT(inst, 24)
  215. #define OFFSET_12 BITS(inst, 0, 11)
  216. static void LnSWoUB(ImmediateOffset)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  217. unsigned int Rn = BITS(inst, 16, 19);
  218. unsigned int addr;
  219. if (U_BIT)
  220. addr = CHECK_READ_REG15_WA(cpu, Rn) + OFFSET_12;
  221. else
  222. addr = CHECK_READ_REG15_WA(cpu, Rn) - OFFSET_12;
  223. virt_addr = addr;
  224. }
  225. static void LnSWoUB(RegisterOffset)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  226. unsigned int Rn = BITS(inst, 16, 19);
  227. unsigned int Rm = BITS(inst, 0, 3);
  228. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  229. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  230. unsigned int addr;
  231. if (U_BIT)
  232. addr = rn + rm;
  233. else
  234. addr = rn - rm;
  235. virt_addr = addr;
  236. }
  237. static void LnSWoUB(ImmediatePostIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  238. unsigned int Rn = BITS(inst, 16, 19);
  239. unsigned int addr = CHECK_READ_REG15_WA(cpu, Rn);
  240. if (U_BIT)
  241. cpu->Reg[Rn] += OFFSET_12;
  242. else
  243. cpu->Reg[Rn] -= OFFSET_12;
  244. virt_addr = addr;
  245. }
  246. static void LnSWoUB(ImmediatePreIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  247. unsigned int Rn = BITS(inst, 16, 19);
  248. unsigned int addr;
  249. if (U_BIT)
  250. addr = CHECK_READ_REG15_WA(cpu, Rn) + OFFSET_12;
  251. else
  252. addr = CHECK_READ_REG15_WA(cpu, Rn) - OFFSET_12;
  253. virt_addr = addr;
  254. if (CondPassed(cpu, BITS(inst, 28, 31)))
  255. cpu->Reg[Rn] = addr;
  256. }
  257. static void MLnS(RegisterPreIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  258. unsigned int addr;
  259. unsigned int Rn = BITS(inst, 16, 19);
  260. unsigned int Rm = BITS(inst, 0, 3);
  261. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  262. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  263. if (U_BIT)
  264. addr = rn + rm;
  265. else
  266. addr = rn - rm;
  267. virt_addr = addr;
  268. if (CondPassed(cpu, BITS(inst, 28, 31)))
  269. cpu->Reg[Rn] = addr;
  270. }
  271. static void LnSWoUB(RegisterPreIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  272. unsigned int Rn = BITS(inst, 16, 19);
  273. unsigned int Rm = BITS(inst, 0, 3);
  274. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  275. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  276. unsigned int addr;
  277. if (U_BIT)
  278. addr = rn + rm;
  279. else
  280. addr = rn - rm;
  281. virt_addr = addr;
  282. if (CondPassed(cpu, BITS(inst, 28, 31))) {
  283. cpu->Reg[Rn] = addr;
  284. }
  285. }
  286. static void LnSWoUB(ScaledRegisterPreIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  287. unsigned int shift = BITS(inst, 5, 6);
  288. unsigned int shift_imm = BITS(inst, 7, 11);
  289. unsigned int Rn = BITS(inst, 16, 19);
  290. unsigned int Rm = BITS(inst, 0, 3);
  291. unsigned int index = 0;
  292. unsigned int addr;
  293. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  294. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  295. switch (shift) {
  296. case 0:
  297. index = rm << shift_imm;
  298. break;
  299. case 1:
  300. if (shift_imm == 0) {
  301. index = 0;
  302. } else {
  303. index = rm >> shift_imm;
  304. }
  305. break;
  306. case 2:
  307. if (shift_imm == 0) { // ASR #32
  308. if (BIT(rm, 31) == 1)
  309. index = 0xFFFFFFFF;
  310. else
  311. index = 0;
  312. } else {
  313. index = static_cast<int>(rm) >> shift_imm;
  314. }
  315. break;
  316. case 3:
  317. if (shift_imm == 0) {
  318. index = (cpu->CFlag << 31) | (rm >> 1);
  319. } else {
  320. index = ROTATE_RIGHT_32(rm, shift_imm);
  321. }
  322. break;
  323. }
  324. if (U_BIT)
  325. addr = rn + index;
  326. else
  327. addr = rn - index;
  328. virt_addr = addr;
  329. if (CondPassed(cpu, BITS(inst, 28, 31)))
  330. cpu->Reg[Rn] = addr;
  331. }
  332. static void LnSWoUB(ScaledRegisterPostIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  333. unsigned int shift = BITS(inst, 5, 6);
  334. unsigned int shift_imm = BITS(inst, 7, 11);
  335. unsigned int Rn = BITS(inst, 16, 19);
  336. unsigned int Rm = BITS(inst, 0, 3);
  337. unsigned int index = 0;
  338. unsigned int addr = CHECK_READ_REG15_WA(cpu, Rn);
  339. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  340. switch (shift) {
  341. case 0:
  342. index = rm << shift_imm;
  343. break;
  344. case 1:
  345. if (shift_imm == 0) {
  346. index = 0;
  347. } else {
  348. index = rm >> shift_imm;
  349. }
  350. break;
  351. case 2:
  352. if (shift_imm == 0) { // ASR #32
  353. if (BIT(rm, 31) == 1)
  354. index = 0xFFFFFFFF;
  355. else
  356. index = 0;
  357. } else {
  358. index = static_cast<int>(rm) >> shift_imm;
  359. }
  360. break;
  361. case 3:
  362. if (shift_imm == 0) {
  363. index = (cpu->CFlag << 31) | (rm >> 1);
  364. } else {
  365. index = ROTATE_RIGHT_32(rm, shift_imm);
  366. }
  367. break;
  368. }
  369. virt_addr = addr;
  370. if (CondPassed(cpu, BITS(inst, 28, 31))) {
  371. if (U_BIT)
  372. cpu->Reg[Rn] += index;
  373. else
  374. cpu->Reg[Rn] -= index;
  375. }
  376. }
  377. static void LnSWoUB(RegisterPostIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  378. unsigned int Rn = BITS(inst, 16, 19);
  379. unsigned int Rm = BITS(inst, 0, 3);
  380. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  381. virt_addr = CHECK_READ_REG15_WA(cpu, Rn);
  382. if (CondPassed(cpu, BITS(inst, 28, 31))) {
  383. if (U_BIT) {
  384. cpu->Reg[Rn] += rm;
  385. } else {
  386. cpu->Reg[Rn] -= rm;
  387. }
  388. }
  389. }
  390. static void MLnS(ImmediateOffset)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  391. unsigned int immedL = BITS(inst, 0, 3);
  392. unsigned int immedH = BITS(inst, 8, 11);
  393. unsigned int Rn = BITS(inst, 16, 19);
  394. unsigned int addr;
  395. unsigned int offset_8 = (immedH << 4) | immedL;
  396. if (U_BIT)
  397. addr = CHECK_READ_REG15_WA(cpu, Rn) + offset_8;
  398. else
  399. addr = CHECK_READ_REG15_WA(cpu, Rn) - offset_8;
  400. virt_addr = addr;
  401. }
  402. static void MLnS(RegisterOffset)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  403. unsigned int addr;
  404. unsigned int Rn = BITS(inst, 16, 19);
  405. unsigned int Rm = BITS(inst, 0, 3);
  406. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  407. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  408. if (U_BIT)
  409. addr = rn + rm;
  410. else
  411. addr = rn - rm;
  412. virt_addr = addr;
  413. }
  414. static void MLnS(ImmediatePreIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  415. unsigned int Rn = BITS(inst, 16, 19);
  416. unsigned int immedH = BITS(inst, 8, 11);
  417. unsigned int immedL = BITS(inst, 0, 3);
  418. unsigned int addr;
  419. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  420. unsigned int offset_8 = (immedH << 4) | immedL;
  421. if (U_BIT)
  422. addr = rn + offset_8;
  423. else
  424. addr = rn - offset_8;
  425. virt_addr = addr;
  426. if (CondPassed(cpu, BITS(inst, 28, 31)))
  427. cpu->Reg[Rn] = addr;
  428. }
  429. static void MLnS(ImmediatePostIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  430. unsigned int Rn = BITS(inst, 16, 19);
  431. unsigned int immedH = BITS(inst, 8, 11);
  432. unsigned int immedL = BITS(inst, 0, 3);
  433. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  434. virt_addr = rn;
  435. if (CondPassed(cpu, BITS(inst, 28, 31))) {
  436. unsigned int offset_8 = (immedH << 4) | immedL;
  437. if (U_BIT)
  438. rn += offset_8;
  439. else
  440. rn -= offset_8;
  441. cpu->Reg[Rn] = rn;
  442. }
  443. }
  444. static void MLnS(RegisterPostIndexed)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  445. unsigned int Rn = BITS(inst, 16, 19);
  446. unsigned int Rm = BITS(inst, 0, 3);
  447. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  448. virt_addr = CHECK_READ_REG15_WA(cpu, Rn);
  449. if (CondPassed(cpu, BITS(inst, 28, 31))) {
  450. if (U_BIT)
  451. cpu->Reg[Rn] += rm;
  452. else
  453. cpu->Reg[Rn] -= rm;
  454. }
  455. }
  456. static void LdnStM(DecrementBefore)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  457. unsigned int Rn = BITS(inst, 16, 19);
  458. unsigned int i = BITS(inst, 0, 15);
  459. int count = 0;
  460. while (i) {
  461. if (i & 1) count++;
  462. i = i >> 1;
  463. }
  464. virt_addr = CHECK_READ_REG15_WA(cpu, Rn) - count * 4;
  465. if (CondPassed(cpu, BITS(inst, 28, 31)) && BIT(inst, 21))
  466. cpu->Reg[Rn] -= count * 4;
  467. }
  468. static void LdnStM(IncrementBefore)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  469. unsigned int Rn = BITS(inst, 16, 19);
  470. unsigned int i = BITS(inst, 0, 15);
  471. int count = 0;
  472. while (i) {
  473. if (i & 1) count++;
  474. i = i >> 1;
  475. }
  476. virt_addr = CHECK_READ_REG15_WA(cpu, Rn) + 4;
  477. if (CondPassed(cpu, BITS(inst, 28, 31)) && BIT(inst, 21))
  478. cpu->Reg[Rn] += count * 4;
  479. }
  480. static void LdnStM(IncrementAfter)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  481. unsigned int Rn = BITS(inst, 16, 19);
  482. unsigned int i = BITS(inst, 0, 15);
  483. int count = 0;
  484. while(i) {
  485. if (i & 1) count++;
  486. i = i >> 1;
  487. }
  488. virt_addr = CHECK_READ_REG15_WA(cpu, Rn);
  489. if (CondPassed(cpu, BITS(inst, 28, 31)) && BIT(inst, 21))
  490. cpu->Reg[Rn] += count * 4;
  491. }
  492. static void LdnStM(DecrementAfter)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  493. unsigned int Rn = BITS(inst, 16, 19);
  494. unsigned int i = BITS(inst, 0, 15);
  495. int count = 0;
  496. while(i) {
  497. if(i & 1) count++;
  498. i = i >> 1;
  499. }
  500. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  501. unsigned int start_addr = rn - count * 4 + 4;
  502. virt_addr = start_addr;
  503. if (CondPassed(cpu, BITS(inst, 28, 31)) && BIT(inst, 21)) {
  504. cpu->Reg[Rn] -= count * 4;
  505. }
  506. }
  507. static void LnSWoUB(ScaledRegisterOffset)(ARMul_State* cpu, unsigned int inst, unsigned int& virt_addr, unsigned int rw) {
  508. unsigned int shift = BITS(inst, 5, 6);
  509. unsigned int shift_imm = BITS(inst, 7, 11);
  510. unsigned int Rn = BITS(inst, 16, 19);
  511. unsigned int Rm = BITS(inst, 0, 3);
  512. unsigned int index = 0;
  513. unsigned int addr;
  514. unsigned int rm = CHECK_READ_REG15_WA(cpu, Rm);
  515. unsigned int rn = CHECK_READ_REG15_WA(cpu, Rn);
  516. switch (shift) {
  517. case 0:
  518. index = rm << shift_imm;
  519. break;
  520. case 1:
  521. if (shift_imm == 0) {
  522. index = 0;
  523. } else {
  524. index = rm >> shift_imm;
  525. }
  526. break;
  527. case 2:
  528. if (shift_imm == 0) { // ASR #32
  529. if (BIT(rm, 31) == 1)
  530. index = 0xFFFFFFFF;
  531. else
  532. index = 0;
  533. } else {
  534. index = static_cast<int>(rm) >> shift_imm;
  535. }
  536. break;
  537. case 3:
  538. if (shift_imm == 0) {
  539. index = (cpu->CFlag << 31) | (rm >> 1);
  540. } else {
  541. index = ROTATE_RIGHT_32(rm, shift_imm);
  542. }
  543. break;
  544. }
  545. if (U_BIT) {
  546. addr = rn + index;
  547. } else
  548. addr = rn - index;
  549. virt_addr = addr;
  550. }
  551. typedef struct _arm_inst {
  552. unsigned int idx;
  553. unsigned int cond;
  554. int br;
  555. int load_r15;
  556. char component[0];
  557. } arm_inst;
  558. typedef struct generic_arm_inst {
  559. u32 Ra;
  560. u32 Rm;
  561. u32 Rn;
  562. u32 Rd;
  563. u8 op1;
  564. u8 op2;
  565. } generic_arm_inst;
  566. typedef struct _adc_inst {
  567. unsigned int I;
  568. unsigned int S;
  569. unsigned int Rn;
  570. unsigned int Rd;
  571. unsigned int shifter_operand;
  572. shtop_fp_t shtop_func;
  573. } adc_inst;
  574. typedef struct _add_inst {
  575. unsigned int I;
  576. unsigned int S;
  577. unsigned int Rn;
  578. unsigned int Rd;
  579. unsigned int shifter_operand;
  580. shtop_fp_t shtop_func;
  581. } add_inst;
  582. typedef struct _orr_inst {
  583. unsigned int I;
  584. unsigned int S;
  585. unsigned int Rn;
  586. unsigned int Rd;
  587. unsigned int shifter_operand;
  588. shtop_fp_t shtop_func;
  589. } orr_inst;
  590. typedef struct _and_inst {
  591. unsigned int I;
  592. unsigned int S;
  593. unsigned int Rn;
  594. unsigned int Rd;
  595. unsigned int shifter_operand;
  596. shtop_fp_t shtop_func;
  597. } and_inst;
  598. typedef struct _eor_inst {
  599. unsigned int I;
  600. unsigned int S;
  601. unsigned int Rn;
  602. unsigned int Rd;
  603. unsigned int shifter_operand;
  604. shtop_fp_t shtop_func;
  605. } eor_inst;
  606. typedef struct _bbl_inst {
  607. unsigned int L;
  608. int signed_immed_24;
  609. unsigned int next_addr;
  610. unsigned int jmp_addr;
  611. } bbl_inst;
  612. typedef struct _bx_inst {
  613. unsigned int Rm;
  614. } bx_inst;
  615. typedef struct _blx_inst {
  616. union {
  617. int32_t signed_immed_24;
  618. uint32_t Rm;
  619. } val;
  620. unsigned int inst;
  621. } blx_inst;
  622. typedef struct _clz_inst {
  623. unsigned int Rm;
  624. unsigned int Rd;
  625. } clz_inst;
  626. typedef struct _cps_inst {
  627. unsigned int imod0;
  628. unsigned int imod1;
  629. unsigned int mmod;
  630. unsigned int A, I, F;
  631. unsigned int mode;
  632. } cps_inst;
  633. typedef struct _clrex_inst {
  634. } clrex_inst;
  635. typedef struct _cpy_inst {
  636. unsigned int Rm;
  637. unsigned int Rd;
  638. } cpy_inst;
  639. typedef struct _bic_inst {
  640. unsigned int I;
  641. unsigned int S;
  642. unsigned int Rn;
  643. unsigned int Rd;
  644. unsigned int shifter_operand;
  645. shtop_fp_t shtop_func;
  646. } bic_inst;
  647. typedef struct _sub_inst {
  648. unsigned int I;
  649. unsigned int S;
  650. unsigned int Rn;
  651. unsigned int Rd;
  652. unsigned int shifter_operand;
  653. shtop_fp_t shtop_func;
  654. } sub_inst;
  655. typedef struct _tst_inst {
  656. unsigned int I;
  657. unsigned int S;
  658. unsigned int Rn;
  659. unsigned int Rd;
  660. unsigned int shifter_operand;
  661. shtop_fp_t shtop_func;
  662. } tst_inst;
  663. typedef struct _cmn_inst {
  664. unsigned int I;
  665. unsigned int Rn;
  666. unsigned int shifter_operand;
  667. shtop_fp_t shtop_func;
  668. } cmn_inst;
  669. typedef struct _teq_inst {
  670. unsigned int I;
  671. unsigned int Rn;
  672. unsigned int shifter_operand;
  673. shtop_fp_t shtop_func;
  674. } teq_inst;
  675. typedef struct _stm_inst {
  676. unsigned int inst;
  677. } stm_inst;
  678. struct bkpt_inst {
  679. u32 imm;
  680. };
  681. struct blx1_inst {
  682. unsigned int addr;
  683. };
  684. struct blx2_inst {
  685. unsigned int Rm;
  686. };
  687. typedef struct _stc_inst {
  688. } stc_inst;
  689. typedef struct _ldc_inst {
  690. } ldc_inst;
  691. typedef struct _swi_inst {
  692. unsigned int num;
  693. } swi_inst;
  694. typedef struct _cmp_inst {
  695. unsigned int I;
  696. unsigned int Rn;
  697. unsigned int shifter_operand;
  698. shtop_fp_t shtop_func;
  699. } cmp_inst;
  700. typedef struct _mov_inst {
  701. unsigned int I;
  702. unsigned int S;
  703. unsigned int Rd;
  704. unsigned int shifter_operand;
  705. shtop_fp_t shtop_func;
  706. } mov_inst;
  707. typedef struct _mvn_inst {
  708. unsigned int I;
  709. unsigned int S;
  710. unsigned int Rd;
  711. unsigned int shifter_operand;
  712. shtop_fp_t shtop_func;
  713. } mvn_inst;
  714. typedef struct _rev_inst {
  715. unsigned int Rd;
  716. unsigned int Rm;
  717. unsigned int op1;
  718. unsigned int op2;
  719. } rev_inst;
  720. typedef struct _rsb_inst {
  721. unsigned int I;
  722. unsigned int S;
  723. unsigned int Rn;
  724. unsigned int Rd;
  725. unsigned int shifter_operand;
  726. shtop_fp_t shtop_func;
  727. } rsb_inst;
  728. typedef struct _rsc_inst {
  729. unsigned int I;
  730. unsigned int S;
  731. unsigned int Rn;
  732. unsigned int Rd;
  733. unsigned int shifter_operand;
  734. shtop_fp_t shtop_func;
  735. } rsc_inst;
  736. typedef struct _sbc_inst {
  737. unsigned int I;
  738. unsigned int S;
  739. unsigned int Rn;
  740. unsigned int Rd;
  741. unsigned int shifter_operand;
  742. shtop_fp_t shtop_func;
  743. } sbc_inst;
  744. typedef struct _mul_inst {
  745. unsigned int S;
  746. unsigned int Rd;
  747. unsigned int Rs;
  748. unsigned int Rm;
  749. } mul_inst;
  750. typedef struct _smul_inst {
  751. unsigned int Rd;
  752. unsigned int Rs;
  753. unsigned int Rm;
  754. unsigned int x;
  755. unsigned int y;
  756. } smul_inst;
  757. typedef struct _umull_inst {
  758. unsigned int S;
  759. unsigned int RdHi;
  760. unsigned int RdLo;
  761. unsigned int Rs;
  762. unsigned int Rm;
  763. } umull_inst;
  764. typedef struct _smlad_inst {
  765. unsigned int m;
  766. unsigned int Rm;
  767. unsigned int Rd;
  768. unsigned int Ra;
  769. unsigned int Rn;
  770. unsigned int op1;
  771. unsigned int op2;
  772. } smlad_inst;
  773. typedef struct _smla_inst {
  774. unsigned int x;
  775. unsigned int y;
  776. unsigned int Rm;
  777. unsigned int Rd;
  778. unsigned int Rs;
  779. unsigned int Rn;
  780. } smla_inst;
  781. typedef struct smlalxy_inst {
  782. unsigned int x;
  783. unsigned int y;
  784. unsigned int RdLo;
  785. unsigned int RdHi;
  786. unsigned int Rm;
  787. unsigned int Rn;
  788. } smlalxy_inst;
  789. typedef struct ssat_inst {
  790. unsigned int Rn;
  791. unsigned int Rd;
  792. unsigned int imm5;
  793. unsigned int sat_imm;
  794. unsigned int shift_type;
  795. } ssat_inst;
  796. typedef struct umaal_inst {
  797. unsigned int Rn;
  798. unsigned int Rm;
  799. unsigned int RdHi;
  800. unsigned int RdLo;
  801. } umaal_inst;
  802. typedef struct _umlal_inst {
  803. unsigned int S;
  804. unsigned int Rm;
  805. unsigned int Rs;
  806. unsigned int RdHi;
  807. unsigned int RdLo;
  808. } umlal_inst;
  809. typedef struct _smlal_inst {
  810. unsigned int S;
  811. unsigned int Rm;
  812. unsigned int Rs;
  813. unsigned int RdHi;
  814. unsigned int RdLo;
  815. } smlal_inst;
  816. typedef struct smlald_inst {
  817. unsigned int RdLo;
  818. unsigned int RdHi;
  819. unsigned int Rm;
  820. unsigned int Rn;
  821. unsigned int swap;
  822. unsigned int op1;
  823. unsigned int op2;
  824. } smlald_inst;
  825. typedef struct _mla_inst {
  826. unsigned int S;
  827. unsigned int Rn;
  828. unsigned int Rd;
  829. unsigned int Rs;
  830. unsigned int Rm;
  831. } mla_inst;
  832. typedef struct _mrc_inst {
  833. unsigned int opcode_1;
  834. unsigned int opcode_2;
  835. unsigned int cp_num;
  836. unsigned int crn;
  837. unsigned int crm;
  838. unsigned int Rd;
  839. unsigned int inst;
  840. } mrc_inst;
  841. typedef struct _mcr_inst {
  842. unsigned int opcode_1;
  843. unsigned int opcode_2;
  844. unsigned int cp_num;
  845. unsigned int crn;
  846. unsigned int crm;
  847. unsigned int Rd;
  848. unsigned int inst;
  849. } mcr_inst;
  850. typedef struct mcrr_inst {
  851. unsigned int opcode_1;
  852. unsigned int cp_num;
  853. unsigned int crm;
  854. unsigned int rt;
  855. unsigned int rt2;
  856. } mcrr_inst;
  857. typedef struct _mrs_inst {
  858. unsigned int R;
  859. unsigned int Rd;
  860. } mrs_inst;
  861. typedef struct _msr_inst {
  862. unsigned int field_mask;
  863. unsigned int R;
  864. unsigned int inst;
  865. } msr_inst;
  866. typedef struct _pld_inst {
  867. } pld_inst;
  868. typedef struct _sxtb_inst {
  869. unsigned int Rd;
  870. unsigned int Rm;
  871. unsigned int rotate;
  872. } sxtb_inst;
  873. typedef struct _sxtab_inst {
  874. unsigned int Rd;
  875. unsigned int Rn;
  876. unsigned int Rm;
  877. unsigned rotate;
  878. } sxtab_inst;
  879. typedef struct _sxtah_inst {
  880. unsigned int Rd;
  881. unsigned int Rn;
  882. unsigned int Rm;
  883. unsigned int rotate;
  884. } sxtah_inst;
  885. typedef struct _sxth_inst {
  886. unsigned int Rd;
  887. unsigned int Rm;
  888. unsigned int rotate;
  889. } sxth_inst;
  890. typedef struct _uxtab_inst {
  891. unsigned int Rn;
  892. unsigned int Rd;
  893. unsigned int rotate;
  894. unsigned int Rm;
  895. } uxtab_inst;
  896. typedef struct _uxtah_inst {
  897. unsigned int Rn;
  898. unsigned int Rd;
  899. unsigned int rotate;
  900. unsigned int Rm;
  901. } uxtah_inst;
  902. typedef struct _uxth_inst {
  903. unsigned int Rd;
  904. unsigned int Rm;
  905. unsigned int rotate;
  906. } uxth_inst;
  907. typedef struct _cdp_inst {
  908. unsigned int opcode_1;
  909. unsigned int CRn;
  910. unsigned int CRd;
  911. unsigned int cp_num;
  912. unsigned int opcode_2;
  913. unsigned int CRm;
  914. unsigned int inst;
  915. }cdp_inst;
  916. typedef struct _uxtb_inst {
  917. unsigned int Rd;
  918. unsigned int Rm;
  919. unsigned int rotate;
  920. } uxtb_inst;
  921. typedef struct _swp_inst {
  922. unsigned int Rn;
  923. unsigned int Rd;
  924. unsigned int Rm;
  925. } swp_inst;
  926. typedef struct setend_inst {
  927. unsigned int set_bigend;
  928. } setend_inst;
  929. typedef struct _b_2_thumb {
  930. unsigned int imm;
  931. }b_2_thumb;
  932. typedef struct _b_cond_thumb {
  933. unsigned int imm;
  934. unsigned int cond;
  935. }b_cond_thumb;
  936. typedef struct _bl_1_thumb {
  937. unsigned int imm;
  938. }bl_1_thumb;
  939. typedef struct _bl_2_thumb {
  940. unsigned int imm;
  941. }bl_2_thumb;
  942. typedef struct _blx_1_thumb {
  943. unsigned int imm;
  944. unsigned int instr;
  945. }blx_1_thumb;
  946. typedef struct _pkh_inst {
  947. unsigned int Rm;
  948. unsigned int Rn;
  949. unsigned int Rd;
  950. unsigned char imm;
  951. } pkh_inst;
  952. typedef arm_inst * ARM_INST_PTR;
  953. #define CACHE_BUFFER_SIZE (64 * 1024 * 2000)
  954. char inst_buf[CACHE_BUFFER_SIZE];
  955. int top = 0;
  956. inline void *AllocBuffer(unsigned int size) {
  957. int start = top;
  958. top += size;
  959. if (top > CACHE_BUFFER_SIZE) {
  960. LOG_ERROR(Core_ARM11, "inst_buf is full");
  961. CITRA_IGNORE_EXIT(-1);
  962. }
  963. return (void *)&inst_buf[start];
  964. }
  965. int CondPassed(ARMul_State* cpu, unsigned int cond) {
  966. #define NFLAG cpu->NFlag
  967. #define ZFLAG cpu->ZFlag
  968. #define CFLAG cpu->CFlag
  969. #define VFLAG cpu->VFlag
  970. int temp = 0;
  971. switch (cond) {
  972. case 0x0:
  973. temp = ZFLAG;
  974. break;
  975. case 0x1: // NE
  976. temp = !ZFLAG;
  977. break;
  978. case 0x6: // VS
  979. temp = VFLAG;
  980. break;
  981. case 0x7: // VC
  982. temp = !VFLAG;
  983. break;
  984. case 0x4: // MI
  985. temp = NFLAG;
  986. break;
  987. case 0x5: // PL
  988. temp = !NFLAG;
  989. break;
  990. case 0x2: // CS
  991. temp = CFLAG;
  992. break;
  993. case 0x3: // CC
  994. temp = !CFLAG;
  995. break;
  996. case 0x8: // HI
  997. temp = (CFLAG && !ZFLAG);
  998. break;
  999. case 0x9: // LS
  1000. temp = (!CFLAG || ZFLAG);
  1001. break;
  1002. case 0xa: // GE
  1003. temp = ((!NFLAG && !VFLAG) || (NFLAG && VFLAG));
  1004. break;
  1005. case 0xb: // LT
  1006. temp = ((NFLAG && !VFLAG) || (!NFLAG && VFLAG));
  1007. break;
  1008. case 0xc: // GT
  1009. temp = ((!NFLAG && !VFLAG && !ZFLAG) || (NFLAG && VFLAG && !ZFLAG));
  1010. break;
  1011. case 0xd: // LE
  1012. temp = ((NFLAG && !VFLAG) || (!NFLAG && VFLAG)) || ZFLAG;
  1013. break;
  1014. case 0xe: // AL
  1015. temp = 1;
  1016. break;
  1017. case 0xf:
  1018. temp = 1;
  1019. break;
  1020. }
  1021. return temp;
  1022. }
  1023. enum DECODE_STATUS {
  1024. DECODE_SUCCESS,
  1025. DECODE_FAILURE
  1026. };
  1027. int decode_arm_instr(uint32_t instr, int32_t *idx);
  1028. static shtop_fp_t get_shtop(unsigned int inst) {
  1029. if (BIT(inst, 25)) {
  1030. return DPO(Immediate);
  1031. } else if (BITS(inst, 4, 11) == 0) {
  1032. return DPO(Register);
  1033. } else if (BITS(inst, 4, 6) == 0) {
  1034. return DPO(LogicalShiftLeftByImmediate);
  1035. } else if (BITS(inst, 4, 7) == 1) {
  1036. return DPO(LogicalShiftLeftByRegister);
  1037. } else if (BITS(inst, 4, 6) == 2) {
  1038. return DPO(LogicalShiftRightByImmediate);
  1039. } else if (BITS(inst, 4, 7) == 3) {
  1040. return DPO(LogicalShiftRightByRegister);
  1041. } else if (BITS(inst, 4, 6) == 4) {
  1042. return DPO(ArithmeticShiftRightByImmediate);
  1043. } else if (BITS(inst, 4, 7) == 5) {
  1044. return DPO(ArithmeticShiftRightByRegister);
  1045. } else if (BITS(inst, 4, 6) == 6) {
  1046. return DPO(RotateRightByImmediate);
  1047. } else if (BITS(inst, 4, 7) == 7) {
  1048. return DPO(RotateRightByRegister);
  1049. }
  1050. return nullptr;
  1051. }
  1052. static get_addr_fp_t get_calc_addr_op(unsigned int inst) {
  1053. if (BITS(inst, 24, 27) == 5 && BIT(inst, 21) == 0) {
  1054. return LnSWoUB(ImmediateOffset);
  1055. } else if (BITS(inst, 24, 27) == 7 && BIT(inst, 21) == 0 && BITS(inst, 4, 11) == 0) {
  1056. return LnSWoUB(RegisterOffset);
  1057. } else if (BITS(inst, 24, 27) == 7 && BIT(inst, 21) == 0 && BIT(inst, 4) == 0) {
  1058. return LnSWoUB(ScaledRegisterOffset);
  1059. } else if (BITS(inst, 24, 27) == 5 && BIT(inst, 21) == 1) {
  1060. return LnSWoUB(ImmediatePreIndexed);
  1061. } else if (BITS(inst, 24, 27) == 7 && BIT(inst, 21) == 1 && BITS(inst, 4, 11) == 0) {
  1062. return LnSWoUB(RegisterPreIndexed);
  1063. } else if (BITS(inst, 24, 27) == 7 && BIT(inst, 21) == 1 && BIT(inst, 4) == 0) {
  1064. return LnSWoUB(ScaledRegisterPreIndexed);
  1065. } else if (BITS(inst, 24, 27) == 4 && BIT(inst, 21) == 0) {
  1066. return LnSWoUB(ImmediatePostIndexed);
  1067. } else if (BITS(inst, 24, 27) == 6 && BIT(inst, 21) == 0 && BITS(inst, 4, 11) == 0) {
  1068. return LnSWoUB(RegisterPostIndexed);
  1069. } else if (BITS(inst, 24, 27) == 6 && BIT(inst, 21) == 0 && BIT(inst, 4) == 0) {
  1070. return LnSWoUB(ScaledRegisterPostIndexed);
  1071. } else if (BITS(inst, 24, 27) == 1 && BITS(inst, 21, 22) == 2 && BIT(inst, 7) == 1 && BIT(inst, 4) == 1) {
  1072. return MLnS(ImmediateOffset);
  1073. } else if (BITS(inst, 24, 27) == 1 && BITS(inst, 21, 22) == 0 && BIT(inst, 7) == 1 && BIT(inst, 4) == 1) {
  1074. return MLnS(RegisterOffset);
  1075. } else if (BITS(inst, 24, 27) == 1 && BITS(inst, 21, 22) == 3 && BIT(inst, 7) == 1 && BIT(inst, 4) == 1) {
  1076. return MLnS(ImmediatePreIndexed);
  1077. } else if (BITS(inst, 24, 27) == 1 && BITS(inst, 21, 22) == 1 && BIT(inst, 7) == 1 && BIT(inst, 4) == 1) {
  1078. return MLnS(RegisterPreIndexed);
  1079. } else if (BITS(inst, 24, 27) == 0 && BITS(inst, 21, 22) == 2 && BIT(inst, 7) == 1 && BIT(inst, 4) == 1) {
  1080. return MLnS(ImmediatePostIndexed);
  1081. } else if (BITS(inst, 24, 27) == 0 && BITS(inst, 21, 22) == 0 && BIT(inst, 7) == 1 && BIT(inst, 4) == 1) {
  1082. return MLnS(RegisterPostIndexed);
  1083. } else if (BITS(inst, 23, 27) == 0x11) {
  1084. return LdnStM(IncrementAfter);
  1085. } else if (BITS(inst, 23, 27) == 0x13) {
  1086. return LdnStM(IncrementBefore);
  1087. } else if (BITS(inst, 23, 27) == 0x10) {
  1088. return LdnStM(DecrementAfter);
  1089. } else if (BITS(inst, 23, 27) == 0x12) {
  1090. return LdnStM(DecrementBefore);
  1091. }
  1092. return nullptr;
  1093. }
  1094. #define INTERPRETER_TRANSLATE(s) glue(InterpreterTranslate_, s)
  1095. #define CHECK_RN (inst_cream->Rn == 15)
  1096. #define CHECK_RM (inst_cream->Rm == 15)
  1097. #define CHECK_RS (inst_cream->Rs == 15)
  1098. static ARM_INST_PTR INTERPRETER_TRANSLATE(adc)(unsigned int inst, int index)
  1099. {
  1100. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(adc_inst));
  1101. adc_inst *inst_cream = (adc_inst *)inst_base->component;
  1102. inst_base->cond = BITS(inst, 28, 31);
  1103. inst_base->idx = index;
  1104. inst_base->br = NON_BRANCH;
  1105. inst_base->load_r15 = 0;
  1106. inst_cream->I = BIT(inst, 25);
  1107. inst_cream->S = BIT(inst, 20);
  1108. inst_cream->Rn = BITS(inst, 16, 19);
  1109. inst_cream->Rd = BITS(inst, 12, 15);
  1110. if (CHECK_RN)
  1111. inst_base->load_r15 = 1;
  1112. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1113. inst_cream->shtop_func = get_shtop(inst);
  1114. if (inst_cream->Rd == 15) {
  1115. inst_base->br = INDIRECT_BRANCH;
  1116. }
  1117. return inst_base;
  1118. }
  1119. static ARM_INST_PTR INTERPRETER_TRANSLATE(add)(unsigned int inst, int index)
  1120. {
  1121. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(add_inst));
  1122. add_inst *inst_cream = (add_inst *)inst_base->component;
  1123. inst_base->cond = BITS(inst, 28, 31);
  1124. inst_base->idx = index;
  1125. inst_base->br = NON_BRANCH;
  1126. inst_base->load_r15 = 0;
  1127. inst_cream->I = BIT(inst, 25);
  1128. inst_cream->S = BIT(inst, 20);
  1129. inst_cream->Rn = BITS(inst, 16, 19);
  1130. inst_cream->Rd = BITS(inst, 12, 15);
  1131. if (CHECK_RN)
  1132. inst_base->load_r15 = 1;
  1133. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1134. inst_cream->shtop_func = get_shtop(inst);
  1135. if (inst_cream->Rd == 15) {
  1136. inst_base->br = INDIRECT_BRANCH;
  1137. }
  1138. return inst_base;
  1139. }
  1140. static ARM_INST_PTR INTERPRETER_TRANSLATE(and)(unsigned int inst, int index)
  1141. {
  1142. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(and_inst));
  1143. and_inst *inst_cream = (and_inst *)inst_base->component;
  1144. inst_base->cond = BITS(inst, 28, 31);
  1145. inst_base->idx = index;
  1146. inst_base->br = NON_BRANCH;
  1147. inst_base->load_r15 = 0;
  1148. inst_cream->I = BIT(inst, 25);
  1149. inst_cream->S = BIT(inst, 20);
  1150. inst_cream->Rn = BITS(inst, 16, 19);
  1151. inst_cream->Rd = BITS(inst, 12, 15);
  1152. if (CHECK_RN)
  1153. inst_base->load_r15 = 1;
  1154. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1155. inst_cream->shtop_func = get_shtop(inst);
  1156. if (inst_cream->Rd == 15)
  1157. inst_base->br = INDIRECT_BRANCH;
  1158. return inst_base;
  1159. }
  1160. static ARM_INST_PTR INTERPRETER_TRANSLATE(bbl)(unsigned int inst, int index)
  1161. {
  1162. #define POSBRANCH ((inst & 0x7fffff) << 2)
  1163. #define NEGBRANCH ((0xff000000 |(inst & 0xffffff)) << 2)
  1164. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(bbl_inst));
  1165. bbl_inst *inst_cream = (bbl_inst *)inst_base->component;
  1166. inst_base->cond = BITS(inst, 28, 31);
  1167. inst_base->idx = index;
  1168. inst_base->br = DIRECT_BRANCH;
  1169. if (BIT(inst, 24))
  1170. inst_base->br = CALL;
  1171. if (BITS(inst, 28, 31) <= 0xe)
  1172. inst_base->br |= COND;
  1173. inst_cream->L = BIT(inst, 24);
  1174. inst_cream->signed_immed_24 = BIT(inst, 23) ? NEGBRANCH : POSBRANCH;
  1175. return inst_base;
  1176. }
  1177. static ARM_INST_PTR INTERPRETER_TRANSLATE(bic)(unsigned int inst, int index)
  1178. {
  1179. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(bic_inst));
  1180. bic_inst *inst_cream = (bic_inst *)inst_base->component;
  1181. inst_base->cond = BITS(inst, 28, 31);
  1182. inst_base->idx = index;
  1183. inst_base->br = NON_BRANCH;
  1184. inst_base->load_r15 = 0;
  1185. inst_cream->I = BIT(inst, 25);
  1186. inst_cream->S = BIT(inst, 20);
  1187. inst_cream->Rn = BITS(inst, 16, 19);
  1188. inst_cream->Rd = BITS(inst, 12, 15);
  1189. if (CHECK_RN)
  1190. inst_base->load_r15 = 1;
  1191. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1192. inst_cream->shtop_func = get_shtop(inst);
  1193. if (inst_cream->Rd == 15)
  1194. inst_base->br = INDIRECT_BRANCH;
  1195. return inst_base;
  1196. }
  1197. static ARM_INST_PTR INTERPRETER_TRANSLATE(bkpt)(unsigned int inst, int index)
  1198. {
  1199. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(bkpt_inst));
  1200. bkpt_inst* const inst_cream = (bkpt_inst*)inst_base->component;
  1201. inst_base->cond = BITS(inst, 28, 31);
  1202. inst_base->idx = index;
  1203. inst_base->br = NON_BRANCH;
  1204. inst_base->load_r15 = 0;
  1205. inst_cream->imm = (BITS(inst, 8, 19) << 4) | BITS(inst, 0, 3);
  1206. return inst_base;
  1207. }
  1208. static ARM_INST_PTR INTERPRETER_TRANSLATE(blx)(unsigned int inst, int index)
  1209. {
  1210. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(blx_inst));
  1211. blx_inst *inst_cream = (blx_inst *)inst_base->component;
  1212. inst_base->cond = BITS(inst, 28, 31);
  1213. inst_base->idx = index;
  1214. inst_base->br = INDIRECT_BRANCH;
  1215. inst_cream->inst = inst;
  1216. if (BITS(inst, 20, 27) == 0x12 && BITS(inst, 4, 7) == 0x3) {
  1217. inst_cream->val.Rm = BITS(inst, 0, 3);
  1218. } else {
  1219. inst_cream->val.signed_immed_24 = BITS(inst, 0, 23);
  1220. }
  1221. return inst_base;
  1222. }
  1223. static ARM_INST_PTR INTERPRETER_TRANSLATE(bx)(unsigned int inst, int index)
  1224. {
  1225. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(bx_inst));
  1226. bx_inst *inst_cream = (bx_inst *)inst_base->component;
  1227. inst_base->cond = BITS(inst, 28, 31);
  1228. inst_base->idx = index;
  1229. inst_base->br = INDIRECT_BRANCH;
  1230. inst_cream->Rm = BITS(inst, 0, 3);
  1231. return inst_base;
  1232. }
  1233. static ARM_INST_PTR INTERPRETER_TRANSLATE(bxj)(unsigned int inst, int index)
  1234. {
  1235. return INTERPRETER_TRANSLATE(bx)(inst, index);
  1236. }
  1237. static ARM_INST_PTR INTERPRETER_TRANSLATE(cdp)(unsigned int inst, int index) {
  1238. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(cdp_inst));
  1239. cdp_inst *inst_cream = (cdp_inst *)inst_base->component;
  1240. inst_base->cond = BITS(inst, 28, 31);
  1241. inst_base->idx = index;
  1242. inst_base->br = NON_BRANCH;
  1243. inst_base->load_r15 = 0;
  1244. inst_cream->CRm = BITS(inst, 0, 3);
  1245. inst_cream->CRd = BITS(inst, 12, 15);
  1246. inst_cream->CRn = BITS(inst, 16, 19);
  1247. inst_cream->cp_num = BITS(inst, 8, 11);
  1248. inst_cream->opcode_2 = BITS(inst, 5, 7);
  1249. inst_cream->opcode_1 = BITS(inst, 20, 23);
  1250. inst_cream->inst = inst;
  1251. LOG_TRACE(Core_ARM11, "inst %x index %x", inst, index);
  1252. return inst_base;
  1253. }
  1254. static ARM_INST_PTR INTERPRETER_TRANSLATE(clrex)(unsigned int inst, int index)
  1255. {
  1256. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(clrex_inst));
  1257. inst_base->cond = BITS(inst, 28, 31);
  1258. inst_base->idx = index;
  1259. inst_base->br = NON_BRANCH;
  1260. return inst_base;
  1261. }
  1262. static ARM_INST_PTR INTERPRETER_TRANSLATE(clz)(unsigned int inst, int index)
  1263. {
  1264. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(clz_inst));
  1265. clz_inst *inst_cream = (clz_inst *)inst_base->component;
  1266. inst_base->cond = BITS(inst, 28, 31);
  1267. inst_base->idx = index;
  1268. inst_base->br = NON_BRANCH;
  1269. inst_base->load_r15 = 0;
  1270. inst_cream->Rm = BITS(inst, 0, 3);
  1271. inst_cream->Rd = BITS(inst, 12, 15);
  1272. if (CHECK_RM)
  1273. inst_base->load_r15 = 1;
  1274. return inst_base;
  1275. }
  1276. static ARM_INST_PTR INTERPRETER_TRANSLATE(cmn)(unsigned int inst, int index)
  1277. {
  1278. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(cmn_inst));
  1279. cmn_inst *inst_cream = (cmn_inst *)inst_base->component;
  1280. inst_base->cond = BITS(inst, 28, 31);
  1281. inst_base->idx = index;
  1282. inst_base->br = NON_BRANCH;
  1283. inst_base->load_r15 = 0;
  1284. inst_cream->I = BIT(inst, 25);
  1285. inst_cream->Rn = BITS(inst, 16, 19);
  1286. if (CHECK_RN)
  1287. inst_base->load_r15 = 1;
  1288. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1289. inst_cream->shtop_func = get_shtop(inst);
  1290. return inst_base;
  1291. }
  1292. static ARM_INST_PTR INTERPRETER_TRANSLATE(cmp)(unsigned int inst, int index)
  1293. {
  1294. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(cmp_inst));
  1295. cmp_inst *inst_cream = (cmp_inst *)inst_base->component;
  1296. inst_base->cond = BITS(inst, 28, 31);
  1297. inst_base->idx = index;
  1298. inst_base->br = NON_BRANCH;
  1299. inst_base->load_r15 = 0;
  1300. inst_cream->I = BIT(inst, 25);
  1301. inst_cream->Rn = BITS(inst, 16, 19);
  1302. if (CHECK_RN)
  1303. inst_base->load_r15 = 1;
  1304. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1305. inst_cream->shtop_func = get_shtop(inst);
  1306. return inst_base;
  1307. }
  1308. static ARM_INST_PTR INTERPRETER_TRANSLATE(cps)(unsigned int inst, int index)
  1309. {
  1310. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(cps_inst));
  1311. cps_inst *inst_cream = (cps_inst *)inst_base->component;
  1312. inst_base->cond = BITS(inst, 28, 31);
  1313. inst_base->idx = index;
  1314. inst_base->br = NON_BRANCH;
  1315. inst_cream->imod0 = BIT(inst, 18);
  1316. inst_cream->imod1 = BIT(inst, 19);
  1317. inst_cream->mmod = BIT(inst, 17);
  1318. inst_cream->A = BIT(inst, 8);
  1319. inst_cream->I = BIT(inst, 7);
  1320. inst_cream->F = BIT(inst, 6);
  1321. inst_cream->mode = BITS(inst, 0, 4);
  1322. return inst_base;
  1323. }
  1324. static ARM_INST_PTR INTERPRETER_TRANSLATE(cpy)(unsigned int inst, int index)
  1325. {
  1326. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(mov_inst));
  1327. mov_inst *inst_cream = (mov_inst *)inst_base->component;
  1328. inst_base->cond = BITS(inst, 28, 31);
  1329. inst_base->idx = index;
  1330. inst_base->br = NON_BRANCH;
  1331. inst_cream->I = BIT(inst, 25);
  1332. inst_cream->S = BIT(inst, 20);
  1333. inst_cream->Rd = BITS(inst, 12, 15);
  1334. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1335. inst_cream->shtop_func = get_shtop(inst);
  1336. if (inst_cream->Rd == 15) {
  1337. inst_base->br = INDIRECT_BRANCH;
  1338. }
  1339. return inst_base;
  1340. }
  1341. static ARM_INST_PTR INTERPRETER_TRANSLATE(eor)(unsigned int inst, int index)
  1342. {
  1343. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(eor_inst));
  1344. eor_inst *inst_cream = (eor_inst *)inst_base->component;
  1345. inst_base->cond = BITS(inst, 28, 31);
  1346. inst_base->idx = index;
  1347. inst_base->br = NON_BRANCH;
  1348. inst_base->load_r15 = 0;
  1349. inst_cream->I = BIT(inst, 25);
  1350. inst_cream->S = BIT(inst, 20);
  1351. inst_cream->Rn = BITS(inst, 16, 19);
  1352. inst_cream->Rd = BITS(inst, 12, 15);
  1353. if (CHECK_RN)
  1354. inst_base->load_r15 = 1;
  1355. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1356. inst_cream->shtop_func = get_shtop(inst);
  1357. if (inst_cream->Rd == 15) {
  1358. inst_base->br = INDIRECT_BRANCH;
  1359. }
  1360. return inst_base;
  1361. }
  1362. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldc)(unsigned int inst, int index)
  1363. {
  1364. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldc_inst));
  1365. inst_base->cond = BITS(inst, 28, 31);
  1366. inst_base->idx = index;
  1367. inst_base->br = NON_BRANCH;
  1368. return inst_base;
  1369. }
  1370. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldm)(unsigned int inst, int index)
  1371. {
  1372. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1373. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  1374. inst_base->cond = BITS(inst, 28, 31);
  1375. inst_base->idx = index;
  1376. inst_base->br = NON_BRANCH;
  1377. inst_cream->inst = inst;
  1378. inst_cream->get_addr = get_calc_addr_op(inst);
  1379. if (BIT(inst, 15)) {
  1380. inst_base->br = INDIRECT_BRANCH;
  1381. }
  1382. return inst_base;
  1383. }
  1384. static ARM_INST_PTR INTERPRETER_TRANSLATE(sxth)(unsigned int inst, int index)
  1385. {
  1386. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(sxtb_inst));
  1387. sxtb_inst *inst_cream = (sxtb_inst *)inst_base->component;
  1388. inst_base->cond = BITS(inst, 28, 31);
  1389. inst_base->idx = index;
  1390. inst_base->br = NON_BRANCH;
  1391. inst_base->load_r15 = 0;
  1392. inst_cream->Rd = BITS(inst, 12, 15);
  1393. inst_cream->Rm = BITS(inst, 0, 3);
  1394. inst_cream->rotate = BITS(inst, 10, 11);
  1395. if (CHECK_RM)
  1396. inst_base->load_r15 = 1;
  1397. return inst_base;
  1398. }
  1399. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldr)(unsigned int inst, int index)
  1400. {
  1401. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1402. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  1403. inst_base->cond = BITS(inst, 28, 31);
  1404. inst_base->idx = index;
  1405. inst_base->br = NON_BRANCH;
  1406. inst_base->load_r15 = 0;
  1407. inst_cream->inst = inst;
  1408. inst_cream->get_addr = get_calc_addr_op(inst);
  1409. if (BITS(inst, 12, 15) == 15) {
  1410. inst_base->br = INDIRECT_BRANCH;
  1411. }
  1412. return inst_base;
  1413. }
  1414. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrcond)(unsigned int inst, int index)
  1415. {
  1416. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1417. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  1418. inst_base->cond = BITS(inst, 28, 31);
  1419. inst_base->idx = index;
  1420. inst_base->br = NON_BRANCH;
  1421. inst_base->load_r15 = 0;
  1422. inst_cream->inst = inst;
  1423. inst_cream->get_addr = get_calc_addr_op(inst);
  1424. if (BITS(inst, 12, 15) == 15) {
  1425. inst_base->br = INDIRECT_BRANCH;
  1426. }
  1427. return inst_base;
  1428. }
  1429. static ARM_INST_PTR INTERPRETER_TRANSLATE(uxth)(unsigned int inst, int index)
  1430. {
  1431. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(uxth_inst));
  1432. uxth_inst *inst_cream = (uxth_inst *)inst_base->component;
  1433. inst_base->cond = BITS(inst, 28, 31);
  1434. inst_base->idx = index;
  1435. inst_base->br = NON_BRANCH;
  1436. inst_base->load_r15 = 0;
  1437. inst_cream->Rd = BITS(inst, 12, 15);
  1438. inst_cream->rotate = BITS(inst, 10, 11);
  1439. inst_cream->Rm = BITS(inst, 0, 3);
  1440. if (CHECK_RM)
  1441. inst_base->load_r15 = 1;
  1442. return inst_base;
  1443. }
  1444. static ARM_INST_PTR INTERPRETER_TRANSLATE(uxtah)(unsigned int inst, int index)
  1445. {
  1446. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(uxtah_inst));
  1447. uxtah_inst *inst_cream = (uxtah_inst *)inst_base->component;
  1448. inst_base->cond = BITS(inst, 28, 31);
  1449. inst_base->idx = index;
  1450. inst_base->br = NON_BRANCH;
  1451. inst_base->load_r15 = 0;
  1452. inst_cream->Rn = BITS(inst, 16, 19);
  1453. inst_cream->Rd = BITS(inst, 12, 15);
  1454. inst_cream->rotate = BITS(inst, 10, 11);
  1455. inst_cream->Rm = BITS(inst, 0, 3);
  1456. if (CHECK_RM || CHECK_RN)
  1457. inst_base->load_r15 = 1;
  1458. return inst_base;
  1459. }
  1460. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrb)(unsigned int inst, int index)
  1461. {
  1462. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1463. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  1464. inst_base->cond = BITS(inst, 28, 31);
  1465. inst_base->idx = index;
  1466. inst_base->br = NON_BRANCH;
  1467. inst_cream->inst = inst;
  1468. inst_cream->get_addr = get_calc_addr_op(inst);
  1469. if (BITS(inst, 12, 15) == 15) {
  1470. inst_base->br = INDIRECT_BRANCH;
  1471. }
  1472. return inst_base;
  1473. }
  1474. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrbt)(unsigned int inst, int index)
  1475. {
  1476. arm_inst* inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1477. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  1478. inst_base->cond = BITS(inst, 28, 31);
  1479. inst_base->idx = index;
  1480. inst_base->br = NON_BRANCH;
  1481. inst_cream->inst = inst;
  1482. if (BITS(inst, 25, 27) == 2) {
  1483. inst_cream->get_addr = LnSWoUB(ImmediatePostIndexed);
  1484. } else if (BITS(inst, 25, 27) == 3) {
  1485. inst_cream->get_addr = LnSWoUB(ScaledRegisterPostIndexed);
  1486. } else {
  1487. DEBUG_MSG;
  1488. }
  1489. if (BITS(inst, 12, 15) == 15) {
  1490. inst_base->br = INDIRECT_BRANCH;
  1491. }
  1492. return inst_base;
  1493. }
  1494. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrd)(unsigned int inst, int index)
  1495. {
  1496. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1497. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  1498. inst_base->cond = BITS(inst, 28, 31);
  1499. inst_base->idx = index;
  1500. inst_base->br = NON_BRANCH;
  1501. inst_cream->inst = inst;
  1502. inst_cream->get_addr = get_calc_addr_op(inst);
  1503. return inst_base;
  1504. }
  1505. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrex)(unsigned int inst, int index)
  1506. {
  1507. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  1508. generic_arm_inst *inst_cream = (generic_arm_inst *)inst_base->component;
  1509. inst_base->cond = BITS(inst, 28, 31);
  1510. inst_base->idx = index;
  1511. inst_base->br = (BITS(inst, 12, 15) == 15) ? INDIRECT_BRANCH : NON_BRANCH; // Branch if dest is R15
  1512. inst_cream->Rn = BITS(inst, 16, 19);
  1513. inst_cream->Rd = BITS(inst, 12, 15);
  1514. return inst_base;
  1515. }
  1516. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrexb)(unsigned int inst, int index)
  1517. {
  1518. return INTERPRETER_TRANSLATE(ldrex)(inst, index);
  1519. }
  1520. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrexh)(unsigned int inst, int index)
  1521. {
  1522. return INTERPRETER_TRANSLATE(ldrex)(inst, index);
  1523. }
  1524. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrexd)(unsigned int inst, int index)
  1525. {
  1526. return INTERPRETER_TRANSLATE(ldrex)(inst, index);
  1527. }
  1528. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrh)(unsigned int inst, int index)
  1529. {
  1530. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1531. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  1532. inst_base->cond = BITS(inst, 28, 31);
  1533. inst_base->idx = index;
  1534. inst_base->br = NON_BRANCH;
  1535. inst_cream->inst = inst;
  1536. inst_cream->get_addr = get_calc_addr_op(inst);
  1537. if (BITS(inst, 12, 15) == 15) {
  1538. inst_base->br = INDIRECT_BRANCH;
  1539. }
  1540. return inst_base;
  1541. }
  1542. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrsb)(unsigned int inst, int index)
  1543. {
  1544. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1545. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  1546. inst_base->cond = BITS(inst, 28, 31);
  1547. inst_base->idx = index;
  1548. inst_base->br = NON_BRANCH;
  1549. inst_cream->inst = inst;
  1550. inst_cream->get_addr = get_calc_addr_op(inst);
  1551. if (BITS(inst, 12, 15) == 15) {
  1552. inst_base->br = INDIRECT_BRANCH;
  1553. }
  1554. return inst_base;
  1555. }
  1556. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrsh)(unsigned int inst, int index)
  1557. {
  1558. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1559. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  1560. inst_base->cond = BITS(inst, 28, 31);
  1561. inst_base->idx = index;
  1562. inst_base->br = NON_BRANCH;
  1563. inst_cream->inst = inst;
  1564. inst_cream->get_addr = get_calc_addr_op(inst);
  1565. if (BITS(inst, 12, 15) == 15) {
  1566. inst_base->br = INDIRECT_BRANCH;
  1567. }
  1568. return inst_base;
  1569. }
  1570. static ARM_INST_PTR INTERPRETER_TRANSLATE(ldrt)(unsigned int inst, int index)
  1571. {
  1572. arm_inst* inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1573. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  1574. inst_base->cond = BITS(inst, 28, 31);
  1575. inst_base->idx = index;
  1576. inst_base->br = NON_BRANCH;
  1577. inst_cream->inst = inst;
  1578. if (BITS(inst, 25, 27) == 2) {
  1579. inst_cream->get_addr = LnSWoUB(ImmediatePostIndexed);
  1580. } else if (BITS(inst, 25, 27) == 3) {
  1581. inst_cream->get_addr = LnSWoUB(ScaledRegisterPostIndexed);
  1582. } else {
  1583. // Reaching this would indicate the thumb version
  1584. // of this instruction, however the 3DS CPU doesn't
  1585. // support this variant (the 3DS CPU is only ARMv6K,
  1586. // while this variant is added in ARMv6T2).
  1587. // So it's sufficient for citra to not implement this.
  1588. DEBUG_MSG;
  1589. }
  1590. if (BITS(inst, 12, 15) == 15) {
  1591. inst_base->br = INDIRECT_BRANCH;
  1592. }
  1593. return inst_base;
  1594. }
  1595. static ARM_INST_PTR INTERPRETER_TRANSLATE(mcr)(unsigned int inst, int index)
  1596. {
  1597. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(mcr_inst));
  1598. mcr_inst *inst_cream = (mcr_inst *)inst_base->component;
  1599. inst_base->cond = BITS(inst, 28, 31);
  1600. inst_base->idx = index;
  1601. inst_base->br = NON_BRANCH;
  1602. inst_cream->crn = BITS(inst, 16, 19);
  1603. inst_cream->crm = BITS(inst, 0, 3);
  1604. inst_cream->opcode_1 = BITS(inst, 21, 23);
  1605. inst_cream->opcode_2 = BITS(inst, 5, 7);
  1606. inst_cream->Rd = BITS(inst, 12, 15);
  1607. inst_cream->cp_num = BITS(inst, 8, 11);
  1608. inst_cream->inst = inst;
  1609. return inst_base;
  1610. }
  1611. static ARM_INST_PTR INTERPRETER_TRANSLATE(mcrr)(unsigned int inst, int index)
  1612. {
  1613. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(mcrr_inst));
  1614. mcrr_inst* const inst_cream = (mcrr_inst*)inst_base->component;
  1615. inst_base->cond = BITS(inst, 28, 31);
  1616. inst_base->idx = index;
  1617. inst_base->br = NON_BRANCH;
  1618. inst_base->load_r15 = 0;
  1619. inst_cream->crm = BITS(inst, 0, 3);
  1620. inst_cream->opcode_1 = BITS(inst, 4, 7);
  1621. inst_cream->cp_num = BITS(inst, 8, 11);
  1622. inst_cream->rt = BITS(inst, 12, 15);
  1623. inst_cream->rt2 = BITS(inst, 16, 19);
  1624. return inst_base;
  1625. }
  1626. static ARM_INST_PTR INTERPRETER_TRANSLATE(mla)(unsigned int inst, int index)
  1627. {
  1628. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(mla_inst));
  1629. mla_inst *inst_cream = (mla_inst *)inst_base->component;
  1630. inst_base->cond = BITS(inst, 28, 31);
  1631. inst_base->idx = index;
  1632. inst_base->br = NON_BRANCH;
  1633. inst_base->load_r15 = 0;
  1634. inst_cream->S = BIT(inst, 20);
  1635. inst_cream->Rn = BITS(inst, 12, 15);
  1636. inst_cream->Rd = BITS(inst, 16, 19);
  1637. inst_cream->Rs = BITS(inst, 8, 11);
  1638. inst_cream->Rm = BITS(inst, 0, 3);
  1639. if (CHECK_RM || CHECK_RN || CHECK_RS)
  1640. inst_base->load_r15 = 1;
  1641. return inst_base;
  1642. }
  1643. static ARM_INST_PTR INTERPRETER_TRANSLATE(mov)(unsigned int inst, int index)
  1644. {
  1645. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(mov_inst));
  1646. mov_inst *inst_cream = (mov_inst *)inst_base->component;
  1647. inst_base->cond = BITS(inst, 28, 31);
  1648. inst_base->idx = index;
  1649. inst_base->br = NON_BRANCH;
  1650. inst_cream->I = BIT(inst, 25);
  1651. inst_cream->S = BIT(inst, 20);
  1652. inst_cream->Rd = BITS(inst, 12, 15);
  1653. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1654. inst_cream->shtop_func = get_shtop(inst);
  1655. if (inst_cream->Rd == 15) {
  1656. inst_base->br = INDIRECT_BRANCH;
  1657. }
  1658. return inst_base;
  1659. }
  1660. static ARM_INST_PTR INTERPRETER_TRANSLATE(mrc)(unsigned int inst, int index)
  1661. {
  1662. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(mrc_inst));
  1663. mrc_inst *inst_cream = (mrc_inst *)inst_base->component;
  1664. inst_base->cond = BITS(inst, 28, 31);
  1665. inst_base->idx = index;
  1666. inst_base->br = NON_BRANCH;
  1667. inst_cream->crn = BITS(inst, 16, 19);
  1668. inst_cream->crm = BITS(inst, 0, 3);
  1669. inst_cream->opcode_1 = BITS(inst, 21, 23);
  1670. inst_cream->opcode_2 = BITS(inst, 5, 7);
  1671. inst_cream->Rd = BITS(inst, 12, 15);
  1672. inst_cream->cp_num = BITS(inst, 8, 11);
  1673. inst_cream->inst = inst;
  1674. return inst_base;
  1675. }
  1676. static ARM_INST_PTR INTERPRETER_TRANSLATE(mrrc)(unsigned int inst, int index)
  1677. {
  1678. return INTERPRETER_TRANSLATE(mcrr)(inst, index);
  1679. }
  1680. static ARM_INST_PTR INTERPRETER_TRANSLATE(mrs)(unsigned int inst, int index)
  1681. {
  1682. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(mrs_inst));
  1683. mrs_inst *inst_cream = (mrs_inst *)inst_base->component;
  1684. inst_base->cond = BITS(inst, 28, 31);
  1685. inst_base->idx = index;
  1686. inst_base->br = NON_BRANCH;
  1687. inst_cream->Rd = BITS(inst, 12, 15);
  1688. inst_cream->R = BIT(inst, 22);
  1689. return inst_base;
  1690. }
  1691. static ARM_INST_PTR INTERPRETER_TRANSLATE(msr)(unsigned int inst, int index)
  1692. {
  1693. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(msr_inst));
  1694. msr_inst *inst_cream = (msr_inst *)inst_base->component;
  1695. inst_base->cond = BITS(inst, 28, 31);
  1696. inst_base->idx = index;
  1697. inst_base->br = NON_BRANCH;
  1698. inst_cream->field_mask = BITS(inst, 16, 19);
  1699. inst_cream->R = BIT(inst, 22);
  1700. inst_cream->inst = inst;
  1701. return inst_base;
  1702. }
  1703. static ARM_INST_PTR INTERPRETER_TRANSLATE(mul)(unsigned int inst, int index)
  1704. {
  1705. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(mul_inst));
  1706. mul_inst *inst_cream = (mul_inst *)inst_base->component;
  1707. inst_base->cond = BITS(inst, 28, 31);
  1708. inst_base->idx = index;
  1709. inst_base->br = NON_BRANCH;
  1710. inst_base->load_r15 = 0;
  1711. inst_cream->S = BIT(inst, 20);
  1712. inst_cream->Rm = BITS(inst, 0, 3);
  1713. inst_cream->Rs = BITS(inst, 8, 11);
  1714. inst_cream->Rd = BITS(inst, 16, 19);
  1715. if (CHECK_RM || CHECK_RS)
  1716. inst_base->load_r15 = 1;
  1717. return inst_base;
  1718. }
  1719. static ARM_INST_PTR INTERPRETER_TRANSLATE(mvn)(unsigned int inst, int index)
  1720. {
  1721. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(mvn_inst));
  1722. mvn_inst *inst_cream = (mvn_inst *)inst_base->component;
  1723. inst_base->cond = BITS(inst, 28, 31);
  1724. inst_base->idx = index;
  1725. inst_base->br = NON_BRANCH;
  1726. inst_cream->I = BIT(inst, 25);
  1727. inst_cream->S = BIT(inst, 20);
  1728. inst_cream->Rd = BITS(inst, 12, 15);
  1729. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1730. inst_cream->shtop_func = get_shtop(inst);
  1731. if (inst_cream->Rd == 15) {
  1732. inst_base->br = INDIRECT_BRANCH;
  1733. }
  1734. return inst_base;
  1735. }
  1736. static ARM_INST_PTR INTERPRETER_TRANSLATE(orr)(unsigned int inst, int index)
  1737. {
  1738. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(orr_inst));
  1739. orr_inst *inst_cream = (orr_inst *)inst_base->component;
  1740. inst_base->cond = BITS(inst, 28, 31);
  1741. inst_base->idx = index;
  1742. inst_base->br = NON_BRANCH;
  1743. inst_base->load_r15 = 0;
  1744. inst_cream->I = BIT(inst, 25);
  1745. inst_cream->S = BIT(inst, 20);
  1746. inst_cream->Rd = BITS(inst, 12, 15);
  1747. inst_cream->Rn = BITS(inst, 16, 19);
  1748. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1749. inst_cream->shtop_func = get_shtop(inst);
  1750. if (CHECK_RN)
  1751. inst_base->load_r15 = 1;
  1752. if (inst_cream->Rd == 15) {
  1753. inst_base->br = INDIRECT_BRANCH;
  1754. }
  1755. return inst_base;
  1756. }
  1757. static ARM_INST_PTR INTERPRETER_TRANSLATE(pkhbt)(unsigned int inst, int index)
  1758. {
  1759. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(pkh_inst));
  1760. pkh_inst *inst_cream = (pkh_inst *)inst_base->component;
  1761. inst_base->cond = BITS(inst, 28, 31);
  1762. inst_base->idx = index;
  1763. inst_base->br = NON_BRANCH;
  1764. inst_base->load_r15 = 0;
  1765. inst_cream->Rd = BITS(inst, 12, 15);
  1766. inst_cream->Rn = BITS(inst, 16, 19);
  1767. inst_cream->Rm = BITS(inst, 0, 3);
  1768. inst_cream->imm = BITS(inst, 7, 11);
  1769. return inst_base;
  1770. }
  1771. static ARM_INST_PTR INTERPRETER_TRANSLATE(pkhtb)(unsigned int inst, int index)
  1772. {
  1773. return INTERPRETER_TRANSLATE(pkhbt)(inst, index);
  1774. }
  1775. static ARM_INST_PTR INTERPRETER_TRANSLATE(pld)(unsigned int inst, int index)
  1776. {
  1777. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(pld_inst));
  1778. inst_base->cond = BITS(inst, 28, 31);
  1779. inst_base->idx = index;
  1780. inst_base->br = NON_BRANCH;
  1781. inst_base->load_r15 = 0;
  1782. return inst_base;
  1783. }
  1784. static ARM_INST_PTR INTERPRETER_TRANSLATE(qadd)(unsigned int inst, int index)
  1785. {
  1786. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  1787. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  1788. inst_base->cond = BITS(inst, 28, 31);
  1789. inst_base->idx = index;
  1790. inst_base->br = NON_BRANCH;
  1791. inst_base->load_r15 = 0;
  1792. inst_cream->op1 = BITS(inst, 21, 22);
  1793. inst_cream->Rm = BITS(inst, 0, 3);
  1794. inst_cream->Rn = BITS(inst, 16, 19);
  1795. inst_cream->Rd = BITS(inst, 12, 15);
  1796. return inst_base;
  1797. }
  1798. static ARM_INST_PTR INTERPRETER_TRANSLATE(qdadd)(unsigned int inst, int index)
  1799. {
  1800. return INTERPRETER_TRANSLATE(qadd)(inst, index);
  1801. }
  1802. static ARM_INST_PTR INTERPRETER_TRANSLATE(qdsub)(unsigned int inst, int index)
  1803. {
  1804. return INTERPRETER_TRANSLATE(qadd)(inst, index);
  1805. }
  1806. static ARM_INST_PTR INTERPRETER_TRANSLATE(qsub)(unsigned int inst, int index)
  1807. {
  1808. return INTERPRETER_TRANSLATE(qadd)(inst, index);
  1809. }
  1810. static ARM_INST_PTR INTERPRETER_TRANSLATE(qadd8)(unsigned int inst, int index)
  1811. {
  1812. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  1813. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  1814. inst_base->cond = BITS(inst, 28, 31);
  1815. inst_base->idx = index;
  1816. inst_base->br = NON_BRANCH;
  1817. inst_base->load_r15 = 0;
  1818. inst_cream->Rm = BITS(inst, 0, 3);
  1819. inst_cream->Rn = BITS(inst, 16, 19);
  1820. inst_cream->Rd = BITS(inst, 12, 15);
  1821. inst_cream->op1 = BITS(inst, 20, 21);
  1822. inst_cream->op2 = BITS(inst, 5, 7);
  1823. return inst_base;
  1824. }
  1825. static ARM_INST_PTR INTERPRETER_TRANSLATE(qadd16)(unsigned int inst, int index)
  1826. {
  1827. return INTERPRETER_TRANSLATE(qadd8)(inst, index);
  1828. }
  1829. static ARM_INST_PTR INTERPRETER_TRANSLATE(qaddsubx)(unsigned int inst, int index)
  1830. {
  1831. return INTERPRETER_TRANSLATE(qadd8)(inst, index);
  1832. }
  1833. static ARM_INST_PTR INTERPRETER_TRANSLATE(qsub8)(unsigned int inst, int index)
  1834. {
  1835. return INTERPRETER_TRANSLATE(qadd8)(inst, index);
  1836. }
  1837. static ARM_INST_PTR INTERPRETER_TRANSLATE(qsub16)(unsigned int inst, int index)
  1838. {
  1839. return INTERPRETER_TRANSLATE(qadd8)(inst, index);
  1840. }
  1841. static ARM_INST_PTR INTERPRETER_TRANSLATE(qsubaddx)(unsigned int inst, int index)
  1842. {
  1843. return INTERPRETER_TRANSLATE(qadd8)(inst, index);
  1844. }
  1845. static ARM_INST_PTR INTERPRETER_TRANSLATE(rev)(unsigned int inst, int index)
  1846. {
  1847. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(rev_inst));
  1848. rev_inst* const inst_cream = (rev_inst*)inst_base->component;
  1849. inst_base->cond = BITS(inst, 28, 31);
  1850. inst_base->idx = index;
  1851. inst_base->br = NON_BRANCH;
  1852. inst_base->load_r15 = 0;
  1853. inst_cream->Rm = BITS(inst, 0, 3);
  1854. inst_cream->Rd = BITS(inst, 12, 15);
  1855. inst_cream->op1 = BITS(inst, 20, 22);
  1856. inst_cream->op2 = BITS(inst, 5, 7);
  1857. return inst_base;
  1858. }
  1859. static ARM_INST_PTR INTERPRETER_TRANSLATE(rev16)(unsigned int inst, int index)
  1860. {
  1861. return INTERPRETER_TRANSLATE(rev)(inst, index);
  1862. }
  1863. static ARM_INST_PTR INTERPRETER_TRANSLATE(revsh)(unsigned int inst, int index)
  1864. {
  1865. return INTERPRETER_TRANSLATE(rev)(inst, index);
  1866. }
  1867. static ARM_INST_PTR INTERPRETER_TRANSLATE(rfe)(unsigned int inst, int index)
  1868. {
  1869. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  1870. ldst_inst* const inst_cream = (ldst_inst*)inst_base->component;
  1871. inst_base->cond = AL;
  1872. inst_base->idx = index;
  1873. inst_base->br = INDIRECT_BRANCH;
  1874. inst_base->load_r15 = 0;
  1875. inst_cream->inst = inst;
  1876. inst_cream->get_addr = get_calc_addr_op(inst);
  1877. return inst_base;
  1878. }
  1879. static ARM_INST_PTR INTERPRETER_TRANSLATE(rsb)(unsigned int inst, int index)
  1880. {
  1881. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(rsb_inst));
  1882. rsb_inst *inst_cream = (rsb_inst *)inst_base->component;
  1883. inst_base->cond = BITS(inst, 28, 31);
  1884. inst_base->idx = index;
  1885. inst_base->br = NON_BRANCH;
  1886. inst_base->load_r15 = 0;
  1887. inst_cream->I = BIT(inst, 25);
  1888. inst_cream->S = BIT(inst, 20);
  1889. inst_cream->Rn = BITS(inst, 16, 19);
  1890. inst_cream->Rd = BITS(inst, 12, 15);
  1891. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1892. inst_cream->shtop_func = get_shtop(inst);
  1893. if (CHECK_RN)
  1894. inst_base->load_r15 = 1;
  1895. if (inst_cream->Rd == 15) {
  1896. inst_base->br = INDIRECT_BRANCH;
  1897. }
  1898. return inst_base;
  1899. }
  1900. static ARM_INST_PTR INTERPRETER_TRANSLATE(rsc)(unsigned int inst, int index)
  1901. {
  1902. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(rsc_inst));
  1903. rsc_inst *inst_cream = (rsc_inst *)inst_base->component;
  1904. inst_base->cond = BITS(inst, 28, 31);
  1905. inst_base->idx = index;
  1906. inst_base->br = NON_BRANCH;
  1907. inst_base->load_r15 = 0;
  1908. inst_cream->I = BIT(inst, 25);
  1909. inst_cream->S = BIT(inst, 20);
  1910. inst_cream->Rn = BITS(inst, 16, 19);
  1911. inst_cream->Rd = BITS(inst, 12, 15);
  1912. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1913. inst_cream->shtop_func = get_shtop(inst);
  1914. if (CHECK_RN)
  1915. inst_base->load_r15 = 1;
  1916. if (inst_cream->Rd == 15) {
  1917. inst_base->br = INDIRECT_BRANCH;
  1918. }
  1919. return inst_base;
  1920. }
  1921. static ARM_INST_PTR INTERPRETER_TRANSLATE(sadd8)(unsigned int inst, int index)
  1922. {
  1923. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  1924. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  1925. inst_base->cond = BITS(inst, 28, 31);
  1926. inst_base->idx = index;
  1927. inst_base->br = NON_BRANCH;
  1928. inst_base->load_r15 = 0;
  1929. inst_cream->Rm = BITS(inst, 0, 3);
  1930. inst_cream->Rn = BITS(inst, 16, 19);
  1931. inst_cream->Rd = BITS(inst, 12, 15);
  1932. inst_cream->op1 = BITS(inst, 20, 21);
  1933. inst_cream->op2 = BITS(inst, 5, 7);
  1934. return inst_base;
  1935. }
  1936. static ARM_INST_PTR INTERPRETER_TRANSLATE(sadd16)(unsigned int inst, int index)
  1937. {
  1938. return INTERPRETER_TRANSLATE(sadd8)(inst, index);
  1939. }
  1940. static ARM_INST_PTR INTERPRETER_TRANSLATE(saddsubx)(unsigned int inst, int index)
  1941. {
  1942. return INTERPRETER_TRANSLATE(sadd8)(inst, index);
  1943. }
  1944. static ARM_INST_PTR INTERPRETER_TRANSLATE(ssub8)(unsigned int inst, int index)
  1945. {
  1946. return INTERPRETER_TRANSLATE(sadd8)(inst, index);
  1947. }
  1948. static ARM_INST_PTR INTERPRETER_TRANSLATE(ssub16)(unsigned int inst, int index)
  1949. {
  1950. return INTERPRETER_TRANSLATE(sadd8)(inst, index);
  1951. }
  1952. static ARM_INST_PTR INTERPRETER_TRANSLATE(ssubaddx)(unsigned int inst, int index)
  1953. {
  1954. return INTERPRETER_TRANSLATE(sadd8)(inst, index);
  1955. }
  1956. static ARM_INST_PTR INTERPRETER_TRANSLATE(sbc)(unsigned int inst, int index)
  1957. {
  1958. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(sbc_inst));
  1959. sbc_inst *inst_cream = (sbc_inst *)inst_base->component;
  1960. inst_base->cond = BITS(inst, 28, 31);
  1961. inst_base->idx = index;
  1962. inst_base->br = NON_BRANCH;
  1963. inst_base->load_r15 = 0;
  1964. inst_cream->I = BIT(inst, 25);
  1965. inst_cream->S = BIT(inst, 20);
  1966. inst_cream->Rn = BITS(inst, 16, 19);
  1967. inst_cream->Rd = BITS(inst, 12, 15);
  1968. inst_cream->shifter_operand = BITS(inst, 0, 11);
  1969. inst_cream->shtop_func = get_shtop(inst);
  1970. if (CHECK_RN)
  1971. inst_base->load_r15 = 1;
  1972. if (inst_cream->Rd == 15) {
  1973. inst_base->br = INDIRECT_BRANCH;
  1974. }
  1975. return inst_base;
  1976. }
  1977. static ARM_INST_PTR INTERPRETER_TRANSLATE(sel)(unsigned int inst, int index)
  1978. {
  1979. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  1980. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  1981. inst_base->cond = BITS(inst, 28, 31);
  1982. inst_base->idx = index;
  1983. inst_base->br = NON_BRANCH;
  1984. inst_base->load_r15 = 0;
  1985. inst_cream->Rm = BITS(inst, 0, 3);
  1986. inst_cream->Rn = BITS(inst, 16, 19);
  1987. inst_cream->Rd = BITS(inst, 12, 15);
  1988. inst_cream->op1 = BITS(inst, 20, 22);
  1989. inst_cream->op2 = BITS(inst, 5, 7);
  1990. return inst_base;
  1991. }
  1992. static ARM_INST_PTR INTERPRETER_TRANSLATE(setend)(unsigned int inst, int index)
  1993. {
  1994. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(setend_inst));
  1995. setend_inst* const inst_cream = (setend_inst*)inst_base->component;
  1996. inst_base->cond = AL;
  1997. inst_base->idx = index;
  1998. inst_base->br = NON_BRANCH;
  1999. inst_base->load_r15 = 0;
  2000. inst_cream->set_bigend = BIT(inst, 9);
  2001. return inst_base;
  2002. }
  2003. static ARM_INST_PTR INTERPRETER_TRANSLATE(shadd8)(unsigned int inst, int index)
  2004. {
  2005. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  2006. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  2007. inst_base->cond = BITS(inst, 28, 31);
  2008. inst_base->idx = index;
  2009. inst_base->br = NON_BRANCH;
  2010. inst_base->load_r15 = 0;
  2011. inst_cream->op1 = BITS(inst, 20, 21);
  2012. inst_cream->op2 = BITS(inst, 5, 7);
  2013. inst_cream->Rm = BITS(inst, 0, 3);
  2014. inst_cream->Rn = BITS(inst, 16, 19);
  2015. inst_cream->Rd = BITS(inst, 12, 15);
  2016. return inst_base;
  2017. }
  2018. static ARM_INST_PTR INTERPRETER_TRANSLATE(shadd16)(unsigned int inst, int index)
  2019. {
  2020. return INTERPRETER_TRANSLATE(shadd8)(inst, index);
  2021. }
  2022. static ARM_INST_PTR INTERPRETER_TRANSLATE(shaddsubx)(unsigned int inst, int index)
  2023. {
  2024. return INTERPRETER_TRANSLATE(shadd8)(inst, index);
  2025. }
  2026. static ARM_INST_PTR INTERPRETER_TRANSLATE(shsub8)(unsigned int inst, int index)
  2027. {
  2028. return INTERPRETER_TRANSLATE(shadd8)(inst, index);
  2029. }
  2030. static ARM_INST_PTR INTERPRETER_TRANSLATE(shsub16)(unsigned int inst, int index)
  2031. {
  2032. return INTERPRETER_TRANSLATE(shadd8)(inst, index);
  2033. }
  2034. static ARM_INST_PTR INTERPRETER_TRANSLATE(shsubaddx)(unsigned int inst, int index)
  2035. {
  2036. return INTERPRETER_TRANSLATE(shadd8)(inst, index);
  2037. }
  2038. static ARM_INST_PTR INTERPRETER_TRANSLATE(smla)(unsigned int inst, int index)
  2039. {
  2040. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(smla_inst));
  2041. smla_inst *inst_cream = (smla_inst *)inst_base->component;
  2042. inst_base->cond = BITS(inst, 28, 31);
  2043. inst_base->idx = index;
  2044. inst_base->br = NON_BRANCH;
  2045. inst_base->load_r15 = 0;
  2046. inst_cream->x = BIT(inst, 5);
  2047. inst_cream->y = BIT(inst, 6);
  2048. inst_cream->Rm = BITS(inst, 0, 3);
  2049. inst_cream->Rs = BITS(inst, 8, 11);
  2050. inst_cream->Rd = BITS(inst, 16, 19);
  2051. inst_cream->Rn = BITS(inst, 12, 15);
  2052. return inst_base;
  2053. }
  2054. static ARM_INST_PTR INTERPRETER_TRANSLATE(smlad)(unsigned int inst, int index)
  2055. {
  2056. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(smlad_inst));
  2057. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  2058. inst_base->cond = BITS(inst, 28, 31);
  2059. inst_base->idx = index;
  2060. inst_base->br = NON_BRANCH;
  2061. inst_base->load_r15 = 0;
  2062. inst_cream->m = BIT(inst, 5);
  2063. inst_cream->Rn = BITS(inst, 0, 3);
  2064. inst_cream->Rm = BITS(inst, 8, 11);
  2065. inst_cream->Rd = BITS(inst, 16, 19);
  2066. inst_cream->Ra = BITS(inst, 12, 15);
  2067. inst_cream->op1 = BITS(inst, 20, 22);
  2068. inst_cream->op2 = BITS(inst, 5, 7);
  2069. return inst_base;
  2070. }
  2071. static ARM_INST_PTR INTERPRETER_TRANSLATE(smuad)(unsigned int inst, int index)
  2072. {
  2073. return INTERPRETER_TRANSLATE(smlad)(inst, index);
  2074. }
  2075. static ARM_INST_PTR INTERPRETER_TRANSLATE(smusd)(unsigned int inst, int index)
  2076. {
  2077. return INTERPRETER_TRANSLATE(smlad)(inst, index);
  2078. }
  2079. static ARM_INST_PTR INTERPRETER_TRANSLATE(smlsd)(unsigned int inst, int index)
  2080. {
  2081. return INTERPRETER_TRANSLATE(smlad)(inst, index);
  2082. }
  2083. static ARM_INST_PTR INTERPRETER_TRANSLATE(smlal)(unsigned int inst, int index)
  2084. {
  2085. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(umlal_inst));
  2086. umlal_inst *inst_cream = (umlal_inst *)inst_base->component;
  2087. inst_base->cond = BITS(inst, 28, 31);
  2088. inst_base->idx = index;
  2089. inst_base->br = NON_BRANCH;
  2090. inst_base->load_r15 = 0;
  2091. inst_cream->S = BIT(inst, 20);
  2092. inst_cream->Rm = BITS(inst, 0, 3);
  2093. inst_cream->Rs = BITS(inst, 8, 11);
  2094. inst_cream->RdHi = BITS(inst, 16, 19);
  2095. inst_cream->RdLo = BITS(inst, 12, 15);
  2096. if (CHECK_RM || CHECK_RS)
  2097. inst_base->load_r15 = 1;
  2098. return inst_base;
  2099. }
  2100. static ARM_INST_PTR INTERPRETER_TRANSLATE(smlalxy)(unsigned int inst, int index)
  2101. {
  2102. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(smlalxy_inst));
  2103. smlalxy_inst* const inst_cream = (smlalxy_inst*)inst_base->component;
  2104. inst_base->cond = BITS(inst, 28, 31);
  2105. inst_base->idx = index;
  2106. inst_base->br = NON_BRANCH;
  2107. inst_base->load_r15 = 0;
  2108. inst_cream->x = BIT(inst, 5);
  2109. inst_cream->y = BIT(inst, 6);
  2110. inst_cream->RdLo = BITS(inst, 12, 15);
  2111. inst_cream->RdHi = BITS(inst, 16, 19);
  2112. inst_cream->Rn = BITS(inst, 0, 4);
  2113. inst_cream->Rm = BITS(inst, 8, 11);
  2114. return inst_base;
  2115. }
  2116. static ARM_INST_PTR INTERPRETER_TRANSLATE(smlaw)(unsigned int inst, int index)
  2117. {
  2118. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(smlad_inst));
  2119. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  2120. inst_base->cond = BITS(inst, 28, 31);
  2121. inst_base->idx = index;
  2122. inst_base->br = NON_BRANCH;
  2123. inst_base->load_r15 = 0;
  2124. inst_cream->Ra = BITS(inst, 12, 15);
  2125. inst_cream->Rm = BITS(inst, 8, 11);
  2126. inst_cream->Rn = BITS(inst, 0, 3);
  2127. inst_cream->Rd = BITS(inst, 16, 19);
  2128. inst_cream->m = BIT(inst, 6);
  2129. return inst_base;
  2130. }
  2131. static ARM_INST_PTR INTERPRETER_TRANSLATE(smlald)(unsigned int inst, int index)
  2132. {
  2133. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(smlald_inst));
  2134. smlald_inst* const inst_cream = (smlald_inst*)inst_base->component;
  2135. inst_base->cond = BITS(inst, 28, 31);
  2136. inst_base->idx = index;
  2137. inst_base->br = NON_BRANCH;
  2138. inst_base->load_r15 = 0;
  2139. inst_cream->Rm = BITS(inst, 8, 11);
  2140. inst_cream->Rn = BITS(inst, 0, 3);
  2141. inst_cream->RdLo = BITS(inst, 12, 15);
  2142. inst_cream->RdHi = BITS(inst, 16, 19);
  2143. inst_cream->swap = BIT(inst, 5);
  2144. inst_cream->op1 = BITS(inst, 20, 22);
  2145. inst_cream->op2 = BITS(inst, 5, 7);
  2146. return inst_base;
  2147. }
  2148. static ARM_INST_PTR INTERPRETER_TRANSLATE(smlsld)(unsigned int inst, int index)
  2149. {
  2150. return INTERPRETER_TRANSLATE(smlald)(inst, index);
  2151. }
  2152. static ARM_INST_PTR INTERPRETER_TRANSLATE(smmla)(unsigned int inst, int index)
  2153. {
  2154. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(smlad_inst));
  2155. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  2156. inst_base->cond = BITS(inst, 28, 31);
  2157. inst_base->idx = index;
  2158. inst_base->br = NON_BRANCH;
  2159. inst_base->load_r15 = 0;
  2160. inst_cream->m = BIT(inst, 5);
  2161. inst_cream->Ra = BITS(inst, 12, 15);
  2162. inst_cream->Rm = BITS(inst, 8, 11);
  2163. inst_cream->Rn = BITS(inst, 0, 3);
  2164. inst_cream->Rd = BITS(inst, 16, 19);
  2165. inst_cream->op1 = BITS(inst, 20, 22);
  2166. inst_cream->op2 = BITS(inst, 5, 7);
  2167. return inst_base;
  2168. }
  2169. static ARM_INST_PTR INTERPRETER_TRANSLATE(smmls)(unsigned int inst, int index)
  2170. {
  2171. return INTERPRETER_TRANSLATE(smmla)(inst, index);
  2172. }
  2173. static ARM_INST_PTR INTERPRETER_TRANSLATE(smmul)(unsigned int inst, int index)
  2174. {
  2175. return INTERPRETER_TRANSLATE(smmla)(inst, index);
  2176. }
  2177. static ARM_INST_PTR INTERPRETER_TRANSLATE(smul)(unsigned int inst, int index)
  2178. {
  2179. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(smul_inst));
  2180. smul_inst *inst_cream = (smul_inst *)inst_base->component;
  2181. inst_base->cond = BITS(inst, 28, 31);
  2182. inst_base->idx = index;
  2183. inst_base->br = NON_BRANCH;
  2184. inst_base->load_r15 = 0;
  2185. inst_cream->Rd = BITS(inst, 16, 19);
  2186. inst_cream->Rs = BITS(inst, 8, 11);
  2187. inst_cream->Rm = BITS(inst, 0, 3);
  2188. inst_cream->x = BIT(inst, 5);
  2189. inst_cream->y = BIT(inst, 6);
  2190. if (CHECK_RM || CHECK_RS)
  2191. inst_base->load_r15 = 1;
  2192. return inst_base;
  2193. }
  2194. static ARM_INST_PTR INTERPRETER_TRANSLATE(smull)(unsigned int inst, int index)
  2195. {
  2196. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(umull_inst));
  2197. umull_inst *inst_cream = (umull_inst *)inst_base->component;
  2198. inst_base->cond = BITS(inst, 28, 31);
  2199. inst_base->idx = index;
  2200. inst_base->br = NON_BRANCH;
  2201. inst_base->load_r15 = 0;
  2202. inst_cream->S = BIT(inst, 20);
  2203. inst_cream->Rm = BITS(inst, 0, 3);
  2204. inst_cream->Rs = BITS(inst, 8, 11);
  2205. inst_cream->RdHi = BITS(inst, 16, 19);
  2206. inst_cream->RdLo = BITS(inst, 12, 15);
  2207. if (CHECK_RM || CHECK_RS)
  2208. inst_base->load_r15 = 1;
  2209. return inst_base;
  2210. }
  2211. static ARM_INST_PTR INTERPRETER_TRANSLATE(smulw)(unsigned int inst, int index)
  2212. {
  2213. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(smlad_inst));
  2214. smlad_inst *inst_cream = (smlad_inst *)inst_base->component;
  2215. inst_base->cond = BITS(inst, 28, 31);
  2216. inst_base->idx = index;
  2217. inst_base->br = NON_BRANCH;
  2218. inst_base->load_r15 = 0;
  2219. inst_cream->m = BIT(inst, 6);
  2220. inst_cream->Rm = BITS(inst, 8, 11);
  2221. inst_cream->Rn = BITS(inst, 0, 3);
  2222. inst_cream->Rd = BITS(inst, 16, 19);
  2223. if (CHECK_RM || CHECK_RN)
  2224. inst_base->load_r15 = 1;
  2225. return inst_base;
  2226. }
  2227. static ARM_INST_PTR INTERPRETER_TRANSLATE(srs)(unsigned int inst, int index)
  2228. {
  2229. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  2230. ldst_inst* const inst_cream = (ldst_inst*)inst_base->component;
  2231. inst_base->cond = AL;
  2232. inst_base->idx = index;
  2233. inst_base->br = NON_BRANCH;
  2234. inst_base->load_r15 = 0;
  2235. inst_cream->inst = inst;
  2236. inst_cream->get_addr = get_calc_addr_op(inst);
  2237. return inst_base;
  2238. }
  2239. static ARM_INST_PTR INTERPRETER_TRANSLATE(ssat)(unsigned int inst, int index)
  2240. {
  2241. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(ssat_inst));
  2242. ssat_inst* const inst_cream = (ssat_inst*)inst_base->component;
  2243. inst_base->cond = BITS(inst, 28, 31);
  2244. inst_base->idx = index;
  2245. inst_base->br = NON_BRANCH;
  2246. inst_base->load_r15 = 0;
  2247. inst_cream->Rn = BITS(inst, 0, 3);
  2248. inst_cream->Rd = BITS(inst, 12, 15);
  2249. inst_cream->imm5 = BITS(inst, 7, 11);
  2250. inst_cream->sat_imm = BITS(inst, 16, 20);
  2251. inst_cream->shift_type = BIT(inst, 6);
  2252. return inst_base;
  2253. }
  2254. static ARM_INST_PTR INTERPRETER_TRANSLATE(ssat16)(unsigned int inst, int index)
  2255. {
  2256. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(ssat_inst));
  2257. ssat_inst* const inst_cream = (ssat_inst*)inst_base->component;
  2258. inst_base->cond = BITS(inst, 28, 31);
  2259. inst_base->idx = index;
  2260. inst_base->br = NON_BRANCH;
  2261. inst_base->load_r15 = 0;
  2262. inst_cream->Rn = BITS(inst, 0, 3);
  2263. inst_cream->Rd = BITS(inst, 12, 15);
  2264. inst_cream->sat_imm = BITS(inst, 16, 19);
  2265. return inst_base;
  2266. }
  2267. static ARM_INST_PTR INTERPRETER_TRANSLATE(stc)(unsigned int inst, int index)
  2268. {
  2269. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(stc_inst));
  2270. inst_base->cond = BITS(inst, 28, 31);
  2271. inst_base->idx = index;
  2272. inst_base->br = NON_BRANCH;
  2273. return inst_base;
  2274. }
  2275. static ARM_INST_PTR INTERPRETER_TRANSLATE(stm)(unsigned int inst, int index)
  2276. {
  2277. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  2278. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  2279. inst_base->cond = BITS(inst, 28, 31);
  2280. inst_base->idx = index;
  2281. inst_base->br = NON_BRANCH;
  2282. inst_cream->inst = inst;
  2283. inst_cream->get_addr = get_calc_addr_op(inst);
  2284. return inst_base;
  2285. }
  2286. static ARM_INST_PTR INTERPRETER_TRANSLATE(sxtb)(unsigned int inst, int index)
  2287. {
  2288. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(sxtb_inst));
  2289. sxtb_inst *inst_cream = (sxtb_inst *)inst_base->component;
  2290. inst_base->cond = BITS(inst, 28, 31);
  2291. inst_base->idx = index;
  2292. inst_base->br = NON_BRANCH;
  2293. inst_base->load_r15 = 0;
  2294. inst_cream->Rd = BITS(inst, 12, 15);
  2295. inst_cream->Rm = BITS(inst, 0, 3);
  2296. inst_cream->rotate = BITS(inst, 10, 11);
  2297. if (CHECK_RM)
  2298. inst_base->load_r15 = 1;
  2299. return inst_base;
  2300. }
  2301. static ARM_INST_PTR INTERPRETER_TRANSLATE(str)(unsigned int inst, int index)
  2302. {
  2303. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  2304. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  2305. inst_base->cond = BITS(inst, 28, 31);
  2306. inst_base->idx = index;
  2307. inst_base->br = NON_BRANCH;
  2308. inst_cream->inst = inst;
  2309. inst_cream->get_addr = get_calc_addr_op(inst);
  2310. if (BITS(inst, 12, 15) == 15) {
  2311. inst_base->br = INDIRECT_BRANCH;
  2312. }
  2313. return inst_base;
  2314. }
  2315. static ARM_INST_PTR INTERPRETER_TRANSLATE(uxtb)(unsigned int inst, int index)
  2316. {
  2317. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(uxth_inst));
  2318. uxth_inst *inst_cream = (uxth_inst *)inst_base->component;
  2319. inst_base->cond = BITS(inst, 28, 31);
  2320. inst_base->idx = index;
  2321. inst_base->br = NON_BRANCH;
  2322. inst_base->load_r15 = 0;
  2323. inst_cream->Rd = BITS(inst, 12, 15);
  2324. inst_cream->rotate = BITS(inst, 10, 11);
  2325. inst_cream->Rm = BITS(inst, 0, 3);
  2326. if (CHECK_RM)
  2327. inst_base->load_r15 = 1;
  2328. return inst_base;
  2329. }
  2330. static ARM_INST_PTR INTERPRETER_TRANSLATE(uxtab)(unsigned int inst, int index)
  2331. {
  2332. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(uxtab_inst));
  2333. uxtab_inst *inst_cream = (uxtab_inst *)inst_base->component;
  2334. inst_base->cond = BITS(inst, 28, 31);
  2335. inst_base->idx = index;
  2336. inst_base->br = NON_BRANCH;
  2337. inst_base->load_r15 = 0;
  2338. inst_cream->Rd = BITS(inst, 12, 15);
  2339. inst_cream->rotate = BITS(inst, 10, 11);
  2340. inst_cream->Rm = BITS(inst, 0, 3);
  2341. inst_cream->Rn = BITS(inst, 16, 19);
  2342. return inst_base;
  2343. }
  2344. static ARM_INST_PTR INTERPRETER_TRANSLATE(strb)(unsigned int inst, int index)
  2345. {
  2346. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  2347. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  2348. inst_base->cond = BITS(inst, 28, 31);
  2349. inst_base->idx = index;
  2350. inst_base->br = NON_BRANCH;
  2351. inst_cream->inst = inst;
  2352. inst_cream->get_addr = get_calc_addr_op(inst);
  2353. if (BITS(inst, 12, 15) == 15) {
  2354. inst_base->br = INDIRECT_BRANCH;
  2355. }
  2356. return inst_base;
  2357. }
  2358. static ARM_INST_PTR INTERPRETER_TRANSLATE(strbt)(unsigned int inst, int index)
  2359. {
  2360. arm_inst* inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  2361. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  2362. inst_base->cond = BITS(inst, 28, 31);
  2363. inst_base->idx = index;
  2364. inst_base->br = NON_BRANCH;
  2365. inst_cream->inst = inst;
  2366. if (BITS(inst, 25, 27) == 2) {
  2367. inst_cream->get_addr = LnSWoUB(ImmediatePostIndexed);
  2368. } else if (BITS(inst, 25, 27) == 3) {
  2369. inst_cream->get_addr = LnSWoUB(ScaledRegisterPostIndexed);
  2370. } else {
  2371. DEBUG_MSG;
  2372. }
  2373. if (BITS(inst, 12, 15) == 15) {
  2374. inst_base->br = INDIRECT_BRANCH;
  2375. }
  2376. return inst_base;
  2377. }
  2378. static ARM_INST_PTR INTERPRETER_TRANSLATE(strd)(unsigned int inst, int index){
  2379. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  2380. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  2381. inst_base->cond = BITS(inst, 28, 31);
  2382. inst_base->idx = index;
  2383. inst_base->br = NON_BRANCH;
  2384. inst_cream->inst = inst;
  2385. inst_cream->get_addr = get_calc_addr_op(inst);
  2386. if (BITS(inst, 12, 15) == 15) {
  2387. inst_base->br = INDIRECT_BRANCH;
  2388. }
  2389. return inst_base;
  2390. }
  2391. static ARM_INST_PTR INTERPRETER_TRANSLATE(strex)(unsigned int inst, int index)
  2392. {
  2393. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  2394. generic_arm_inst *inst_cream = (generic_arm_inst *)inst_base->component;
  2395. inst_base->cond = BITS(inst, 28, 31);
  2396. inst_base->idx = index;
  2397. inst_base->br = NON_BRANCH;
  2398. inst_cream->Rn = BITS(inst, 16, 19);
  2399. inst_cream->Rd = BITS(inst, 12, 15);
  2400. inst_cream->Rm = BITS(inst, 0, 3);
  2401. return inst_base;
  2402. }
  2403. static ARM_INST_PTR INTERPRETER_TRANSLATE(strexb)(unsigned int inst, int index)
  2404. {
  2405. return INTERPRETER_TRANSLATE(strex)(inst, index);
  2406. }
  2407. static ARM_INST_PTR INTERPRETER_TRANSLATE(strexh)(unsigned int inst, int index)
  2408. {
  2409. return INTERPRETER_TRANSLATE(strex)(inst, index);
  2410. }
  2411. static ARM_INST_PTR INTERPRETER_TRANSLATE(strexd)(unsigned int inst, int index)
  2412. {
  2413. return INTERPRETER_TRANSLATE(strex)(inst, index);
  2414. }
  2415. static ARM_INST_PTR INTERPRETER_TRANSLATE(strh)(unsigned int inst, int index)
  2416. {
  2417. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  2418. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  2419. inst_base->cond = BITS(inst, 28, 31);
  2420. inst_base->idx = index;
  2421. inst_base->br = NON_BRANCH;
  2422. inst_cream->inst = inst;
  2423. inst_cream->get_addr = get_calc_addr_op(inst);
  2424. if (BITS(inst, 12, 15) == 15) {
  2425. inst_base->br = INDIRECT_BRANCH;
  2426. }
  2427. return inst_base;
  2428. }
  2429. static ARM_INST_PTR INTERPRETER_TRANSLATE(strt)(unsigned int inst, int index)
  2430. {
  2431. arm_inst* inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(ldst_inst));
  2432. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  2433. inst_base->cond = BITS(inst, 28, 31);
  2434. inst_base->idx = index;
  2435. inst_base->br = NON_BRANCH;
  2436. inst_cream->inst = inst;
  2437. if (BITS(inst, 25, 27) == 2) {
  2438. inst_cream->get_addr = LnSWoUB(ImmediatePostIndexed);
  2439. } else if (BITS(inst, 25, 27) == 3) {
  2440. inst_cream->get_addr = LnSWoUB(ScaledRegisterPostIndexed);
  2441. } else {
  2442. // Reaching this would indicate the thumb version
  2443. // of this instruction, however the 3DS CPU doesn't
  2444. // support this variant (the 3DS CPU is only ARMv6K,
  2445. // while this variant is added in ARMv6T2).
  2446. // So it's sufficient for citra to not implement this.
  2447. DEBUG_MSG;
  2448. }
  2449. if (BITS(inst, 12, 15) == 15) {
  2450. inst_base->br = INDIRECT_BRANCH;
  2451. }
  2452. return inst_base;
  2453. }
  2454. static ARM_INST_PTR INTERPRETER_TRANSLATE(sub)(unsigned int inst, int index)
  2455. {
  2456. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(sub_inst));
  2457. sub_inst *inst_cream = (sub_inst *)inst_base->component;
  2458. inst_base->cond = BITS(inst, 28, 31);
  2459. inst_base->idx = index;
  2460. inst_base->br = NON_BRANCH;
  2461. inst_base->load_r15 = 0;
  2462. inst_cream->I = BIT(inst, 25);
  2463. inst_cream->S = BIT(inst, 20);
  2464. inst_cream->Rn = BITS(inst, 16, 19);
  2465. inst_cream->Rd = BITS(inst, 12, 15);
  2466. inst_cream->shifter_operand = BITS(inst, 0, 11);
  2467. inst_cream->shtop_func = get_shtop(inst);
  2468. if (inst_cream->Rd == 15) {
  2469. inst_base->br = INDIRECT_BRANCH;
  2470. }
  2471. if (CHECK_RN)
  2472. inst_base->load_r15 = 1;
  2473. return inst_base;
  2474. }
  2475. static ARM_INST_PTR INTERPRETER_TRANSLATE(swi)(unsigned int inst, int index)
  2476. {
  2477. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(swi_inst));
  2478. swi_inst *inst_cream = (swi_inst *)inst_base->component;
  2479. inst_base->cond = BITS(inst, 28, 31);
  2480. inst_base->idx = index;
  2481. inst_base->br = NON_BRANCH;
  2482. inst_cream->num = BITS(inst, 0, 23);
  2483. return inst_base;
  2484. }
  2485. static ARM_INST_PTR INTERPRETER_TRANSLATE(swp)(unsigned int inst, int index)
  2486. {
  2487. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(swp_inst));
  2488. swp_inst *inst_cream = (swp_inst *)inst_base->component;
  2489. inst_base->cond = BITS(inst, 28, 31);
  2490. inst_base->idx = index;
  2491. inst_base->br = NON_BRANCH;
  2492. inst_cream->Rn = BITS(inst, 16, 19);
  2493. inst_cream->Rd = BITS(inst, 12, 15);
  2494. inst_cream->Rm = BITS(inst, 0, 3);
  2495. if (inst_cream->Rd == 15) {
  2496. inst_base->br = INDIRECT_BRANCH;
  2497. }
  2498. return inst_base;
  2499. }
  2500. static ARM_INST_PTR INTERPRETER_TRANSLATE(swpb)(unsigned int inst, int index){
  2501. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(swp_inst));
  2502. swp_inst *inst_cream = (swp_inst *)inst_base->component;
  2503. inst_base->cond = BITS(inst, 28, 31);
  2504. inst_base->idx = index;
  2505. inst_base->br = NON_BRANCH;
  2506. inst_cream->Rn = BITS(inst, 16, 19);
  2507. inst_cream->Rd = BITS(inst, 12, 15);
  2508. inst_cream->Rm = BITS(inst, 0, 3);
  2509. if (inst_cream->Rd == 15) {
  2510. inst_base->br = INDIRECT_BRANCH;
  2511. }
  2512. return inst_base;
  2513. }
  2514. static ARM_INST_PTR INTERPRETER_TRANSLATE(sxtab)(unsigned int inst, int index){
  2515. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(sxtab_inst));
  2516. sxtab_inst *inst_cream = (sxtab_inst *)inst_base->component;
  2517. inst_base->cond = BITS(inst, 28, 31);
  2518. inst_base->idx = index;
  2519. inst_base->br = NON_BRANCH;
  2520. inst_base->load_r15 = 0;
  2521. inst_cream->Rd = BITS(inst, 12, 15);
  2522. inst_cream->rotate = BITS(inst, 10, 11);
  2523. inst_cream->Rm = BITS(inst, 0, 3);
  2524. inst_cream->Rn = BITS(inst, 16, 19);
  2525. return inst_base;
  2526. }
  2527. static ARM_INST_PTR INTERPRETER_TRANSLATE(sxtab16)(unsigned int inst, int index)
  2528. {
  2529. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(sxtab_inst));
  2530. sxtab_inst* const inst_cream = (sxtab_inst*)inst_base->component;
  2531. inst_base->cond = BITS(inst, 28, 31);
  2532. inst_base->idx = index;
  2533. inst_base->br = NON_BRANCH;
  2534. inst_base->load_r15 = 0;
  2535. inst_cream->Rm = BITS(inst, 0, 3);
  2536. inst_cream->Rn = BITS(inst, 16, 19);
  2537. inst_cream->Rd = BITS(inst, 12, 15);
  2538. inst_cream->rotate = BITS(inst, 10, 11);
  2539. return inst_base;
  2540. }
  2541. static ARM_INST_PTR INTERPRETER_TRANSLATE(sxtb16)(unsigned int inst, int index)
  2542. {
  2543. return INTERPRETER_TRANSLATE(sxtab16)(inst, index);
  2544. }
  2545. static ARM_INST_PTR INTERPRETER_TRANSLATE(sxtah)(unsigned int inst, int index) {
  2546. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(sxtah_inst));
  2547. sxtah_inst *inst_cream = (sxtah_inst *)inst_base->component;
  2548. inst_base->cond = BITS(inst, 28, 31);
  2549. inst_base->idx = index;
  2550. inst_base->br = NON_BRANCH;
  2551. inst_base->load_r15 = 0;
  2552. inst_cream->Rd = BITS(inst, 12, 15);
  2553. inst_cream->rotate = BITS(inst, 10, 11);
  2554. inst_cream->Rm = BITS(inst, 0, 3);
  2555. inst_cream->Rn = BITS(inst, 16, 19);
  2556. return inst_base;
  2557. }
  2558. static ARM_INST_PTR INTERPRETER_TRANSLATE(teq)(unsigned int inst, int index)
  2559. {
  2560. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(teq_inst));
  2561. teq_inst *inst_cream = (teq_inst *)inst_base->component;
  2562. inst_base->cond = BITS(inst, 28, 31);
  2563. inst_base->idx = index;
  2564. inst_base->br = NON_BRANCH;
  2565. inst_base->load_r15 = 0;
  2566. inst_cream->I = BIT(inst, 25);
  2567. inst_cream->Rn = BITS(inst, 16, 19);
  2568. inst_cream->shifter_operand = BITS(inst, 0, 11);
  2569. inst_cream->shtop_func = get_shtop(inst);
  2570. if (CHECK_RN)
  2571. inst_base->load_r15 = 1;
  2572. return inst_base;
  2573. }
  2574. static ARM_INST_PTR INTERPRETER_TRANSLATE(tst)(unsigned int inst, int index)
  2575. {
  2576. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(tst_inst));
  2577. tst_inst *inst_cream = (tst_inst *)inst_base->component;
  2578. inst_base->cond = BITS(inst, 28, 31);
  2579. inst_base->idx = index;
  2580. inst_base->br = NON_BRANCH;
  2581. inst_base->load_r15 = 0;
  2582. inst_cream->I = BIT(inst, 25);
  2583. inst_cream->S = BIT(inst, 20);
  2584. inst_cream->Rn = BITS(inst, 16, 19);
  2585. inst_cream->Rd = BITS(inst, 12, 15);
  2586. inst_cream->shifter_operand = BITS(inst, 0, 11);
  2587. inst_cream->shtop_func = get_shtop(inst);
  2588. if (inst_cream->Rd == 15) {
  2589. inst_base->br = INDIRECT_BRANCH;
  2590. }
  2591. if (CHECK_RN)
  2592. inst_base->load_r15 = 1;
  2593. return inst_base;
  2594. }
  2595. static ARM_INST_PTR INTERPRETER_TRANSLATE(uadd8)(unsigned int inst, int index)
  2596. {
  2597. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  2598. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  2599. inst_base->cond = BITS(inst, 28, 31);
  2600. inst_base->idx = index;
  2601. inst_base->br = NON_BRANCH;
  2602. inst_base->load_r15 = 0;
  2603. inst_cream->op1 = BITS(inst, 20, 21);
  2604. inst_cream->op2 = BITS(inst, 5, 7);
  2605. inst_cream->Rm = BITS(inst, 0, 3);
  2606. inst_cream->Rn = BITS(inst, 16, 19);
  2607. inst_cream->Rd = BITS(inst, 12, 15);
  2608. return inst_base;
  2609. }
  2610. static ARM_INST_PTR INTERPRETER_TRANSLATE(uadd16)(unsigned int inst, int index)
  2611. {
  2612. return INTERPRETER_TRANSLATE(uadd8)(inst, index);
  2613. }
  2614. static ARM_INST_PTR INTERPRETER_TRANSLATE(uaddsubx)(unsigned int inst, int index)
  2615. {
  2616. return INTERPRETER_TRANSLATE(uadd8)(inst, index);
  2617. }
  2618. static ARM_INST_PTR INTERPRETER_TRANSLATE(usub8)(unsigned int inst, int index)
  2619. {
  2620. return INTERPRETER_TRANSLATE(uadd8)(inst, index);
  2621. }
  2622. static ARM_INST_PTR INTERPRETER_TRANSLATE(usub16)(unsigned int inst, int index)
  2623. {
  2624. return INTERPRETER_TRANSLATE(uadd8)(inst, index);
  2625. }
  2626. static ARM_INST_PTR INTERPRETER_TRANSLATE(usubaddx)(unsigned int inst, int index)
  2627. {
  2628. return INTERPRETER_TRANSLATE(uadd8)(inst, index);
  2629. }
  2630. static ARM_INST_PTR INTERPRETER_TRANSLATE(uhadd8)(unsigned int inst, int index)
  2631. {
  2632. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  2633. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  2634. inst_base->cond = BITS(inst, 28, 31);
  2635. inst_base->idx = index;
  2636. inst_base->br = NON_BRANCH;
  2637. inst_base->load_r15 = 0;
  2638. inst_cream->op1 = BITS(inst, 20, 21);
  2639. inst_cream->op2 = BITS(inst, 5, 7);
  2640. inst_cream->Rm = BITS(inst, 0, 3);
  2641. inst_cream->Rn = BITS(inst, 16, 19);
  2642. inst_cream->Rd = BITS(inst, 12, 15);
  2643. return inst_base;
  2644. }
  2645. static ARM_INST_PTR INTERPRETER_TRANSLATE(uhadd16)(unsigned int inst, int index)
  2646. {
  2647. return INTERPRETER_TRANSLATE(uhadd8)(inst, index);
  2648. }
  2649. static ARM_INST_PTR INTERPRETER_TRANSLATE(uhaddsubx)(unsigned int inst, int index)
  2650. {
  2651. return INTERPRETER_TRANSLATE(uhadd8)(inst, index);
  2652. }
  2653. static ARM_INST_PTR INTERPRETER_TRANSLATE(uhsub8)(unsigned int inst, int index)
  2654. {
  2655. return INTERPRETER_TRANSLATE(uhadd8)(inst, index);
  2656. }
  2657. static ARM_INST_PTR INTERPRETER_TRANSLATE(uhsub16)(unsigned int inst, int index)
  2658. {
  2659. return INTERPRETER_TRANSLATE(uhadd8)(inst, index);
  2660. }
  2661. static ARM_INST_PTR INTERPRETER_TRANSLATE(uhsubaddx)(unsigned int inst, int index)
  2662. {
  2663. return INTERPRETER_TRANSLATE(uhadd8)(inst, index);
  2664. }
  2665. static ARM_INST_PTR INTERPRETER_TRANSLATE(umaal)(unsigned int inst, int index)
  2666. {
  2667. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(umaal_inst));
  2668. umaal_inst* const inst_cream = (umaal_inst*)inst_base->component;
  2669. inst_base->cond = BITS(inst, 28, 31);
  2670. inst_base->idx = index;
  2671. inst_base->br = NON_BRANCH;
  2672. inst_base->load_r15 = 0;
  2673. inst_cream->Rm = BITS(inst, 8, 11);
  2674. inst_cream->Rn = BITS(inst, 0, 3);
  2675. inst_cream->RdLo = BITS(inst, 12, 15);
  2676. inst_cream->RdHi = BITS(inst, 16, 19);
  2677. if (CHECK_RM || CHECK_RN)
  2678. inst_base->load_r15 = 1;
  2679. return inst_base;
  2680. }
  2681. static ARM_INST_PTR INTERPRETER_TRANSLATE(umlal)(unsigned int inst, int index)
  2682. {
  2683. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(umlal_inst));
  2684. umlal_inst *inst_cream = (umlal_inst *)inst_base->component;
  2685. inst_base->cond = BITS(inst, 28, 31);
  2686. inst_base->idx = index;
  2687. inst_base->br = NON_BRANCH;
  2688. inst_base->load_r15 = 0;
  2689. inst_cream->S = BIT(inst, 20);
  2690. inst_cream->Rm = BITS(inst, 0, 3);
  2691. inst_cream->Rs = BITS(inst, 8, 11);
  2692. inst_cream->RdHi = BITS(inst, 16, 19);
  2693. inst_cream->RdLo = BITS(inst, 12, 15);
  2694. if (CHECK_RM || CHECK_RS)
  2695. inst_base->load_r15 = 1;
  2696. return inst_base;
  2697. }
  2698. static ARM_INST_PTR INTERPRETER_TRANSLATE(umull)(unsigned int inst, int index)
  2699. {
  2700. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(umull_inst));
  2701. umull_inst *inst_cream = (umull_inst *)inst_base->component;
  2702. inst_base->cond = BITS(inst, 28, 31);
  2703. inst_base->idx = index;
  2704. inst_base->br = NON_BRANCH;
  2705. inst_base->load_r15 = 0;
  2706. inst_cream->S = BIT(inst, 20);
  2707. inst_cream->Rm = BITS(inst, 0, 3);
  2708. inst_cream->Rs = BITS(inst, 8, 11);
  2709. inst_cream->RdHi = BITS(inst, 16, 19);
  2710. inst_cream->RdLo = BITS(inst, 12, 15);
  2711. if (CHECK_RM || CHECK_RS)
  2712. inst_base->load_r15 = 1;
  2713. return inst_base;
  2714. }
  2715. static ARM_INST_PTR INTERPRETER_TRANSLATE(b_2_thumb)(unsigned int tinst, int index)
  2716. {
  2717. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(b_2_thumb));
  2718. b_2_thumb *inst_cream = (b_2_thumb *)inst_base->component;
  2719. inst_cream->imm = ((tinst & 0x3FF) << 1) | ((tinst & (1 << 10)) ? 0xFFFFF800 : 0);
  2720. inst_base->idx = index;
  2721. inst_base->br = DIRECT_BRANCH;
  2722. return inst_base;
  2723. }
  2724. static ARM_INST_PTR INTERPRETER_TRANSLATE(b_cond_thumb)(unsigned int tinst, int index)
  2725. {
  2726. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(b_cond_thumb));
  2727. b_cond_thumb *inst_cream = (b_cond_thumb *)inst_base->component;
  2728. inst_cream->imm = (((tinst & 0x7F) << 1) | ((tinst & (1 << 7)) ? 0xFFFFFF00 : 0));
  2729. inst_cream->cond = ((tinst >> 8) & 0xf);
  2730. inst_base->idx = index;
  2731. inst_base->br = DIRECT_BRANCH;
  2732. return inst_base;
  2733. }
  2734. static ARM_INST_PTR INTERPRETER_TRANSLATE(bl_1_thumb)(unsigned int tinst, int index)
  2735. {
  2736. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(bl_1_thumb));
  2737. bl_1_thumb *inst_cream = (bl_1_thumb *)inst_base->component;
  2738. inst_cream->imm = (((tinst & 0x07FF) << 12) | ((tinst & (1 << 10)) ? 0xFF800000 : 0));
  2739. inst_base->idx = index;
  2740. inst_base->br = NON_BRANCH;
  2741. return inst_base;
  2742. }
  2743. static ARM_INST_PTR INTERPRETER_TRANSLATE(bl_2_thumb)(unsigned int tinst, int index)
  2744. {
  2745. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(bl_2_thumb));
  2746. bl_2_thumb *inst_cream = (bl_2_thumb *)inst_base->component;
  2747. inst_cream->imm = (tinst & 0x07FF) << 1;
  2748. inst_base->idx = index;
  2749. inst_base->br = DIRECT_BRANCH;
  2750. return inst_base;
  2751. }
  2752. static ARM_INST_PTR INTERPRETER_TRANSLATE(blx_1_thumb)(unsigned int tinst, int index)
  2753. {
  2754. arm_inst *inst_base = (arm_inst *)AllocBuffer(sizeof(arm_inst) + sizeof(blx_1_thumb));
  2755. blx_1_thumb *inst_cream = (blx_1_thumb *)inst_base->component;
  2756. inst_cream->imm = (tinst & 0x07FF) << 1;
  2757. inst_cream->instr = tinst;
  2758. inst_base->idx = index;
  2759. inst_base->br = DIRECT_BRANCH;
  2760. return inst_base;
  2761. }
  2762. static ARM_INST_PTR INTERPRETER_TRANSLATE(uqadd8)(unsigned int inst, int index)
  2763. {
  2764. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  2765. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  2766. inst_base->cond = BITS(inst, 28, 31);
  2767. inst_base->idx = index;
  2768. inst_base->br = NON_BRANCH;
  2769. inst_base->load_r15 = 0;
  2770. inst_cream->Rm = BITS(inst, 0, 3);
  2771. inst_cream->Rn = BITS(inst, 16, 19);
  2772. inst_cream->Rd = BITS(inst, 12, 15);
  2773. inst_cream->op1 = BITS(inst, 20, 21);
  2774. inst_cream->op2 = BITS(inst, 5, 7);
  2775. return inst_base;
  2776. }
  2777. static ARM_INST_PTR INTERPRETER_TRANSLATE(uqadd16)(unsigned int inst, int index)
  2778. {
  2779. return INTERPRETER_TRANSLATE(uqadd8)(inst, index);
  2780. }
  2781. static ARM_INST_PTR INTERPRETER_TRANSLATE(uqaddsubx)(unsigned int inst, int index)
  2782. {
  2783. return INTERPRETER_TRANSLATE(uqadd8)(inst, index);
  2784. }
  2785. static ARM_INST_PTR INTERPRETER_TRANSLATE(uqsub8)(unsigned int inst, int index)
  2786. {
  2787. return INTERPRETER_TRANSLATE(uqadd8)(inst, index);
  2788. }
  2789. static ARM_INST_PTR INTERPRETER_TRANSLATE(uqsub16)(unsigned int inst, int index)
  2790. {
  2791. return INTERPRETER_TRANSLATE(uqadd8)(inst, index);
  2792. }
  2793. static ARM_INST_PTR INTERPRETER_TRANSLATE(uqsubaddx)(unsigned int inst, int index)
  2794. {
  2795. return INTERPRETER_TRANSLATE(uqadd8)(inst, index);
  2796. }
  2797. static ARM_INST_PTR INTERPRETER_TRANSLATE(usada8)(unsigned int inst, int index)
  2798. {
  2799. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(generic_arm_inst));
  2800. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  2801. inst_base->cond = BITS(inst, 28, 31);
  2802. inst_base->idx = index;
  2803. inst_base->br = NON_BRANCH;
  2804. inst_base->load_r15 = 0;
  2805. inst_cream->op1 = BITS(inst, 20, 24);
  2806. inst_cream->op2 = BITS(inst, 5, 7);
  2807. inst_cream->Rd = BITS(inst, 16, 19);
  2808. inst_cream->Rm = BITS(inst, 8, 11);
  2809. inst_cream->Rn = BITS(inst, 0, 3);
  2810. inst_cream->Ra = BITS(inst, 12, 15);
  2811. return inst_base;
  2812. }
  2813. static ARM_INST_PTR INTERPRETER_TRANSLATE(usad8)(unsigned int inst, int index)
  2814. {
  2815. return INTERPRETER_TRANSLATE(usada8)(inst, index);
  2816. }
  2817. static ARM_INST_PTR INTERPRETER_TRANSLATE(usat)(unsigned int inst, int index)
  2818. {
  2819. return INTERPRETER_TRANSLATE(ssat)(inst, index);
  2820. }
  2821. static ARM_INST_PTR INTERPRETER_TRANSLATE(usat16)(unsigned int inst, int index)
  2822. {
  2823. return INTERPRETER_TRANSLATE(ssat16)(inst, index);
  2824. }
  2825. static ARM_INST_PTR INTERPRETER_TRANSLATE(uxtab16)(unsigned int inst, int index)
  2826. {
  2827. arm_inst* const inst_base = (arm_inst*)AllocBuffer(sizeof(arm_inst) + sizeof(uxtab_inst));
  2828. uxtab_inst* const inst_cream = (uxtab_inst*)inst_base->component;
  2829. inst_base->cond = BITS(inst, 28, 31);
  2830. inst_base->idx = index;
  2831. inst_base->br = NON_BRANCH;
  2832. inst_base->load_r15 = 0;
  2833. inst_cream->Rm = BITS(inst, 0, 3);
  2834. inst_cream->Rn = BITS(inst, 16, 19);
  2835. inst_cream->Rd = BITS(inst, 12, 15);
  2836. inst_cream->rotate = BITS(inst, 10, 11);
  2837. return inst_base;
  2838. }
  2839. static ARM_INST_PTR INTERPRETER_TRANSLATE(uxtb16)(unsigned int inst, int index)
  2840. {
  2841. return INTERPRETER_TRANSLATE(uxtab16)(inst, index);
  2842. }
  2843. // Floating point VFPv3 structures and instructions
  2844. #define VFP_INTERPRETER_STRUCT
  2845. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  2846. #undef VFP_INTERPRETER_STRUCT
  2847. #define VFP_INTERPRETER_TRANS
  2848. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  2849. #undef VFP_INTERPRETER_TRANS
  2850. typedef ARM_INST_PTR (*transop_fp_t)(unsigned int, int);
  2851. const transop_fp_t arm_instruction_trans[] = {
  2852. INTERPRETER_TRANSLATE(vmla),
  2853. INTERPRETER_TRANSLATE(vmls),
  2854. INTERPRETER_TRANSLATE(vnmla),
  2855. INTERPRETER_TRANSLATE(vnmla),
  2856. INTERPRETER_TRANSLATE(vnmls),
  2857. INTERPRETER_TRANSLATE(vnmul),
  2858. INTERPRETER_TRANSLATE(vmul),
  2859. INTERPRETER_TRANSLATE(vadd),
  2860. INTERPRETER_TRANSLATE(vsub),
  2861. INTERPRETER_TRANSLATE(vdiv),
  2862. INTERPRETER_TRANSLATE(vmovi),
  2863. INTERPRETER_TRANSLATE(vmovr),
  2864. INTERPRETER_TRANSLATE(vabs),
  2865. INTERPRETER_TRANSLATE(vneg),
  2866. INTERPRETER_TRANSLATE(vsqrt),
  2867. INTERPRETER_TRANSLATE(vcmp),
  2868. INTERPRETER_TRANSLATE(vcmp2),
  2869. INTERPRETER_TRANSLATE(vcvtbds),
  2870. INTERPRETER_TRANSLATE(vcvtbff),
  2871. INTERPRETER_TRANSLATE(vcvtbfi),
  2872. INTERPRETER_TRANSLATE(vmovbrs),
  2873. INTERPRETER_TRANSLATE(vmsr),
  2874. INTERPRETER_TRANSLATE(vmovbrc),
  2875. INTERPRETER_TRANSLATE(vmrs),
  2876. INTERPRETER_TRANSLATE(vmovbcr),
  2877. INTERPRETER_TRANSLATE(vmovbrrss),
  2878. INTERPRETER_TRANSLATE(vmovbrrd),
  2879. INTERPRETER_TRANSLATE(vstr),
  2880. INTERPRETER_TRANSLATE(vpush),
  2881. INTERPRETER_TRANSLATE(vstm),
  2882. INTERPRETER_TRANSLATE(vpop),
  2883. INTERPRETER_TRANSLATE(vldr),
  2884. INTERPRETER_TRANSLATE(vldm),
  2885. INTERPRETER_TRANSLATE(srs),
  2886. INTERPRETER_TRANSLATE(rfe),
  2887. INTERPRETER_TRANSLATE(bkpt),
  2888. INTERPRETER_TRANSLATE(blx),
  2889. INTERPRETER_TRANSLATE(cps),
  2890. INTERPRETER_TRANSLATE(pld),
  2891. INTERPRETER_TRANSLATE(setend),
  2892. INTERPRETER_TRANSLATE(clrex),
  2893. INTERPRETER_TRANSLATE(rev16),
  2894. INTERPRETER_TRANSLATE(usad8),
  2895. INTERPRETER_TRANSLATE(sxtb),
  2896. INTERPRETER_TRANSLATE(uxtb),
  2897. INTERPRETER_TRANSLATE(sxth),
  2898. INTERPRETER_TRANSLATE(sxtb16),
  2899. INTERPRETER_TRANSLATE(uxth),
  2900. INTERPRETER_TRANSLATE(uxtb16),
  2901. INTERPRETER_TRANSLATE(cpy),
  2902. INTERPRETER_TRANSLATE(uxtab),
  2903. INTERPRETER_TRANSLATE(ssub8),
  2904. INTERPRETER_TRANSLATE(shsub8),
  2905. INTERPRETER_TRANSLATE(ssubaddx),
  2906. INTERPRETER_TRANSLATE(strex),
  2907. INTERPRETER_TRANSLATE(strexb),
  2908. INTERPRETER_TRANSLATE(swp),
  2909. INTERPRETER_TRANSLATE(swpb),
  2910. INTERPRETER_TRANSLATE(ssub16),
  2911. INTERPRETER_TRANSLATE(ssat16),
  2912. INTERPRETER_TRANSLATE(shsubaddx),
  2913. INTERPRETER_TRANSLATE(qsubaddx),
  2914. INTERPRETER_TRANSLATE(shaddsubx),
  2915. INTERPRETER_TRANSLATE(shadd8),
  2916. INTERPRETER_TRANSLATE(shadd16),
  2917. INTERPRETER_TRANSLATE(sel),
  2918. INTERPRETER_TRANSLATE(saddsubx),
  2919. INTERPRETER_TRANSLATE(sadd8),
  2920. INTERPRETER_TRANSLATE(sadd16),
  2921. INTERPRETER_TRANSLATE(shsub16),
  2922. INTERPRETER_TRANSLATE(umaal),
  2923. INTERPRETER_TRANSLATE(uxtab16),
  2924. INTERPRETER_TRANSLATE(usubaddx),
  2925. INTERPRETER_TRANSLATE(usub8),
  2926. INTERPRETER_TRANSLATE(usub16),
  2927. INTERPRETER_TRANSLATE(usat16),
  2928. INTERPRETER_TRANSLATE(usada8),
  2929. INTERPRETER_TRANSLATE(uqsubaddx),
  2930. INTERPRETER_TRANSLATE(uqsub8),
  2931. INTERPRETER_TRANSLATE(uqsub16),
  2932. INTERPRETER_TRANSLATE(uqaddsubx),
  2933. INTERPRETER_TRANSLATE(uqadd8),
  2934. INTERPRETER_TRANSLATE(uqadd16),
  2935. INTERPRETER_TRANSLATE(sxtab),
  2936. INTERPRETER_TRANSLATE(uhsubaddx),
  2937. INTERPRETER_TRANSLATE(uhsub8),
  2938. INTERPRETER_TRANSLATE(uhsub16),
  2939. INTERPRETER_TRANSLATE(uhaddsubx),
  2940. INTERPRETER_TRANSLATE(uhadd8),
  2941. INTERPRETER_TRANSLATE(uhadd16),
  2942. INTERPRETER_TRANSLATE(uaddsubx),
  2943. INTERPRETER_TRANSLATE(uadd8),
  2944. INTERPRETER_TRANSLATE(uadd16),
  2945. INTERPRETER_TRANSLATE(sxtah),
  2946. INTERPRETER_TRANSLATE(sxtab16),
  2947. INTERPRETER_TRANSLATE(qadd8),
  2948. INTERPRETER_TRANSLATE(bxj),
  2949. INTERPRETER_TRANSLATE(clz),
  2950. INTERPRETER_TRANSLATE(uxtah),
  2951. INTERPRETER_TRANSLATE(bx),
  2952. INTERPRETER_TRANSLATE(rev),
  2953. INTERPRETER_TRANSLATE(blx),
  2954. INTERPRETER_TRANSLATE(revsh),
  2955. INTERPRETER_TRANSLATE(qadd),
  2956. INTERPRETER_TRANSLATE(qadd16),
  2957. INTERPRETER_TRANSLATE(qaddsubx),
  2958. INTERPRETER_TRANSLATE(ldrex),
  2959. INTERPRETER_TRANSLATE(qdadd),
  2960. INTERPRETER_TRANSLATE(qdsub),
  2961. INTERPRETER_TRANSLATE(qsub),
  2962. INTERPRETER_TRANSLATE(ldrexb),
  2963. INTERPRETER_TRANSLATE(qsub8),
  2964. INTERPRETER_TRANSLATE(qsub16),
  2965. INTERPRETER_TRANSLATE(smuad),
  2966. INTERPRETER_TRANSLATE(smmul),
  2967. INTERPRETER_TRANSLATE(smusd),
  2968. INTERPRETER_TRANSLATE(smlsd),
  2969. INTERPRETER_TRANSLATE(smlsld),
  2970. INTERPRETER_TRANSLATE(smmla),
  2971. INTERPRETER_TRANSLATE(smmls),
  2972. INTERPRETER_TRANSLATE(smlald),
  2973. INTERPRETER_TRANSLATE(smlad),
  2974. INTERPRETER_TRANSLATE(smlaw),
  2975. INTERPRETER_TRANSLATE(smulw),
  2976. INTERPRETER_TRANSLATE(pkhtb),
  2977. INTERPRETER_TRANSLATE(pkhbt),
  2978. INTERPRETER_TRANSLATE(smul),
  2979. INTERPRETER_TRANSLATE(smlalxy),
  2980. INTERPRETER_TRANSLATE(smla),
  2981. INTERPRETER_TRANSLATE(mcrr),
  2982. INTERPRETER_TRANSLATE(mrrc),
  2983. INTERPRETER_TRANSLATE(cmp),
  2984. INTERPRETER_TRANSLATE(tst),
  2985. INTERPRETER_TRANSLATE(teq),
  2986. INTERPRETER_TRANSLATE(cmn),
  2987. INTERPRETER_TRANSLATE(smull),
  2988. INTERPRETER_TRANSLATE(umull),
  2989. INTERPRETER_TRANSLATE(umlal),
  2990. INTERPRETER_TRANSLATE(smlal),
  2991. INTERPRETER_TRANSLATE(mul),
  2992. INTERPRETER_TRANSLATE(mla),
  2993. INTERPRETER_TRANSLATE(ssat),
  2994. INTERPRETER_TRANSLATE(usat),
  2995. INTERPRETER_TRANSLATE(mrs),
  2996. INTERPRETER_TRANSLATE(msr),
  2997. INTERPRETER_TRANSLATE(and),
  2998. INTERPRETER_TRANSLATE(bic),
  2999. INTERPRETER_TRANSLATE(ldm),
  3000. INTERPRETER_TRANSLATE(eor),
  3001. INTERPRETER_TRANSLATE(add),
  3002. INTERPRETER_TRANSLATE(rsb),
  3003. INTERPRETER_TRANSLATE(rsc),
  3004. INTERPRETER_TRANSLATE(sbc),
  3005. INTERPRETER_TRANSLATE(adc),
  3006. INTERPRETER_TRANSLATE(sub),
  3007. INTERPRETER_TRANSLATE(orr),
  3008. INTERPRETER_TRANSLATE(mvn),
  3009. INTERPRETER_TRANSLATE(mov),
  3010. INTERPRETER_TRANSLATE(stm),
  3011. INTERPRETER_TRANSLATE(ldm),
  3012. INTERPRETER_TRANSLATE(ldrsh),
  3013. INTERPRETER_TRANSLATE(stm),
  3014. INTERPRETER_TRANSLATE(ldm),
  3015. INTERPRETER_TRANSLATE(ldrsb),
  3016. INTERPRETER_TRANSLATE(strd),
  3017. INTERPRETER_TRANSLATE(ldrh),
  3018. INTERPRETER_TRANSLATE(strh),
  3019. INTERPRETER_TRANSLATE(ldrd),
  3020. INTERPRETER_TRANSLATE(strt),
  3021. INTERPRETER_TRANSLATE(strbt),
  3022. INTERPRETER_TRANSLATE(ldrbt),
  3023. INTERPRETER_TRANSLATE(ldrt),
  3024. INTERPRETER_TRANSLATE(mrc),
  3025. INTERPRETER_TRANSLATE(mcr),
  3026. INTERPRETER_TRANSLATE(msr),
  3027. INTERPRETER_TRANSLATE(ldrb),
  3028. INTERPRETER_TRANSLATE(strb),
  3029. INTERPRETER_TRANSLATE(ldr),
  3030. INTERPRETER_TRANSLATE(ldrcond),
  3031. INTERPRETER_TRANSLATE(str),
  3032. INTERPRETER_TRANSLATE(cdp),
  3033. INTERPRETER_TRANSLATE(stc),
  3034. INTERPRETER_TRANSLATE(ldc),
  3035. INTERPRETER_TRANSLATE(ldrexd),
  3036. INTERPRETER_TRANSLATE(strexd),
  3037. INTERPRETER_TRANSLATE(ldrexh),
  3038. INTERPRETER_TRANSLATE(strexh),
  3039. INTERPRETER_TRANSLATE(swi),
  3040. INTERPRETER_TRANSLATE(bbl),
  3041. // All the thumb instructions should be placed the end of table
  3042. INTERPRETER_TRANSLATE(b_2_thumb),
  3043. INTERPRETER_TRANSLATE(b_cond_thumb),
  3044. INTERPRETER_TRANSLATE(bl_1_thumb),
  3045. INTERPRETER_TRANSLATE(bl_2_thumb),
  3046. INTERPRETER_TRANSLATE(blx_1_thumb)
  3047. };
  3048. enum {
  3049. FETCH_SUCCESS,
  3050. FETCH_FAILURE
  3051. };
  3052. static tdstate decode_thumb_instr(ARMul_State* cpu, u32 inst, u32 addr, u32* arm_inst, u32* inst_size, ARM_INST_PTR* ptr_inst_base) {
  3053. // Check if in Thumb mode
  3054. tdstate ret = thumb_translate (addr, inst, arm_inst, inst_size);
  3055. if(ret == t_branch){
  3056. // TODO: FIXME, endian should be judged
  3057. u32 tinstr;
  3058. if((addr & 0x3) != 0)
  3059. tinstr = inst >> 16;
  3060. else
  3061. tinstr = inst & 0xFFFF;
  3062. int inst_index;
  3063. int table_length = sizeof(arm_instruction_trans) / sizeof(transop_fp_t);
  3064. switch((tinstr & 0xF800) >> 11){
  3065. case 26:
  3066. case 27:
  3067. if (((tinstr & 0x0F00) != 0x0E00) && ((tinstr & 0x0F00) != 0x0F00)){
  3068. inst_index = table_length - 4;
  3069. *ptr_inst_base = arm_instruction_trans[inst_index](tinstr, inst_index);
  3070. } else {
  3071. LOG_ERROR(Core_ARM11, "thumb decoder error");
  3072. }
  3073. break;
  3074. case 28:
  3075. // Branch 2, unconditional branch
  3076. inst_index = table_length - 5;
  3077. *ptr_inst_base = arm_instruction_trans[inst_index](tinstr, inst_index);
  3078. break;
  3079. case 8:
  3080. case 29:
  3081. // For BLX 1 thumb instruction
  3082. inst_index = table_length - 1;
  3083. *ptr_inst_base = arm_instruction_trans[inst_index](tinstr, inst_index);
  3084. break;
  3085. case 30:
  3086. // For BL 1 thumb instruction
  3087. inst_index = table_length - 3;
  3088. *ptr_inst_base = arm_instruction_trans[inst_index](tinstr, inst_index);
  3089. break;
  3090. case 31:
  3091. // For BL 2 thumb instruction
  3092. inst_index = table_length - 2;
  3093. *ptr_inst_base = arm_instruction_trans[inst_index](tinstr, inst_index);
  3094. break;
  3095. default:
  3096. ret = t_undefined;
  3097. break;
  3098. }
  3099. }
  3100. return ret;
  3101. }
  3102. enum {
  3103. KEEP_GOING,
  3104. FETCH_EXCEPTION
  3105. };
  3106. typedef struct instruction_set_encoding_item ISEITEM;
  3107. extern const ISEITEM arm_instruction[];
  3108. static int InterpreterTranslate(ARMul_State* cpu, int& bb_start, u32 addr) {
  3109. Common::Profiling::ScopeTimer timer_decode(profile_decode);
  3110. // Decode instruction, get index
  3111. // Allocate memory and init InsCream
  3112. // Go on next, until terminal instruction
  3113. // Save start addr of basicblock in CreamCache
  3114. ARM_INST_PTR inst_base = nullptr;
  3115. unsigned int inst, inst_size = 4;
  3116. int idx;
  3117. int ret = NON_BRANCH;
  3118. int thumb = 0;
  3119. int size = 0; // instruction size of basic block
  3120. bb_start = top;
  3121. if (cpu->TFlag)
  3122. thumb = THUMB;
  3123. u32 phys_addr = addr;
  3124. u32 pc_start = cpu->Reg[15];
  3125. while(ret == NON_BRANCH) {
  3126. inst = Memory::Read32(phys_addr & 0xFFFFFFFC);
  3127. size++;
  3128. // If we are in thumb instruction, we will translate one thumb to one corresponding arm instruction
  3129. if (cpu->TFlag) {
  3130. uint32_t arm_inst;
  3131. tdstate state;
  3132. state = decode_thumb_instr(cpu, inst, phys_addr, &arm_inst, &inst_size, &inst_base);
  3133. // We have translated the branch instruction of thumb in thumb decoder
  3134. if(state == t_branch){
  3135. goto translated;
  3136. }
  3137. inst = arm_inst;
  3138. }
  3139. ret = decode_arm_instr(inst, &idx);
  3140. if (ret == DECODE_FAILURE) {
  3141. std::string disasm = ARM_Disasm::Disassemble(phys_addr, inst);
  3142. LOG_ERROR(Core_ARM11, "Decode failure.\tPC : [0x%x]\tInstruction : %s [%x]", phys_addr, disasm.c_str(), inst);
  3143. LOG_ERROR(Core_ARM11, "cpsr=0x%x, cpu->TFlag=%d, r15=0x%x", cpu->Cpsr, cpu->TFlag, cpu->Reg[15]);
  3144. CITRA_IGNORE_EXIT(-1);
  3145. }
  3146. inst_base = arm_instruction_trans[idx](inst, idx);
  3147. translated:
  3148. phys_addr += inst_size;
  3149. if ((phys_addr & 0xfff) == 0) {
  3150. inst_base->br = END_OF_PAGE;
  3151. }
  3152. ret = inst_base->br;
  3153. };
  3154. cpu->instruction_cache[pc_start] = bb_start;
  3155. return KEEP_GOING;
  3156. }
  3157. static int clz(unsigned int x) {
  3158. int n;
  3159. if (x == 0) return (32);
  3160. n = 1;
  3161. if ((x >> 16) == 0) { n = n + 16; x = x << 16;}
  3162. if ((x >> 24) == 0) { n = n + 8; x = x << 8;}
  3163. if ((x >> 28) == 0) { n = n + 4; x = x << 4;}
  3164. if ((x >> 30) == 0) { n = n + 2; x = x << 2;}
  3165. n = n - (x >> 31);
  3166. return n;
  3167. }
  3168. unsigned InterpreterMainLoop(ARMul_State* cpu) {
  3169. Common::Profiling::ScopeTimer timer_execute(profile_execute);
  3170. #undef RM
  3171. #undef RS
  3172. #define CRn inst_cream->crn
  3173. #define OPCODE_1 inst_cream->opcode_1
  3174. #define OPCODE_2 inst_cream->opcode_2
  3175. #define CRm inst_cream->crm
  3176. #define RD cpu->Reg[inst_cream->Rd]
  3177. #define RD2 cpu->Reg[inst_cream->Rd + 1]
  3178. #define RN cpu->Reg[inst_cream->Rn]
  3179. #define RM cpu->Reg[inst_cream->Rm]
  3180. #define RS cpu->Reg[inst_cream->Rs]
  3181. #define RDHI cpu->Reg[inst_cream->RdHi]
  3182. #define RDLO cpu->Reg[inst_cream->RdLo]
  3183. #define LINK_RTN_ADDR (cpu->Reg[14] = cpu->Reg[15] + 4)
  3184. #define SET_PC (cpu->Reg[15] = cpu->Reg[15] + 8 + inst_cream->signed_immed_24)
  3185. #define SHIFTER_OPERAND inst_cream->shtop_func(cpu, inst_cream->shifter_operand)
  3186. #define FETCH_INST if (inst_base->br != NON_BRANCH) goto DISPATCH; \
  3187. inst_base = (arm_inst *)&inst_buf[ptr]
  3188. #define INC_PC(l) ptr += sizeof(arm_inst) + l
  3189. // GCC and Clang have a C++ extension to support a lookup table of labels. Otherwise, fallback to a
  3190. // clunky switch statement.
  3191. #if defined __GNUC__ || defined __clang__
  3192. #define GOTO_NEXT_INST \
  3193. if (num_instrs >= cpu->NumInstrsToExecute) goto END; \
  3194. num_instrs++; \
  3195. goto *InstLabel[inst_base->idx]
  3196. #else
  3197. #define GOTO_NEXT_INST \
  3198. if (num_instrs >= cpu->NumInstrsToExecute) goto END; \
  3199. num_instrs++; \
  3200. switch(inst_base->idx) { \
  3201. case 0: goto VMLA_INST; \
  3202. case 1: goto VMLS_INST; \
  3203. case 2: goto VNMLA_INST; \
  3204. case 3: goto VNMLA_INST; \
  3205. case 4: goto VNMLS_INST; \
  3206. case 5: goto VNMUL_INST; \
  3207. case 6: goto VMUL_INST; \
  3208. case 7: goto VADD_INST; \
  3209. case 8: goto VSUB_INST; \
  3210. case 9: goto VDIV_INST; \
  3211. case 10: goto VMOVI_INST; \
  3212. case 11: goto VMOVR_INST; \
  3213. case 12: goto VABS_INST; \
  3214. case 13: goto VNEG_INST; \
  3215. case 14: goto VSQRT_INST; \
  3216. case 15: goto VCMP_INST; \
  3217. case 16: goto VCMP2_INST; \
  3218. case 17: goto VCVTBDS_INST; \
  3219. case 18: goto VCVTBFF_INST; \
  3220. case 19: goto VCVTBFI_INST; \
  3221. case 20: goto VMOVBRS_INST; \
  3222. case 21: goto VMSR_INST; \
  3223. case 22: goto VMOVBRC_INST; \
  3224. case 23: goto VMRS_INST; \
  3225. case 24: goto VMOVBCR_INST; \
  3226. case 25: goto VMOVBRRSS_INST; \
  3227. case 26: goto VMOVBRRD_INST; \
  3228. case 27: goto VSTR_INST; \
  3229. case 28: goto VPUSH_INST; \
  3230. case 29: goto VSTM_INST; \
  3231. case 30: goto VPOP_INST; \
  3232. case 31: goto VLDR_INST; \
  3233. case 32: goto VLDM_INST ; \
  3234. case 33: goto SRS_INST; \
  3235. case 34: goto RFE_INST; \
  3236. case 35: goto BKPT_INST; \
  3237. case 36: goto BLX_INST; \
  3238. case 37: goto CPS_INST; \
  3239. case 38: goto PLD_INST; \
  3240. case 39: goto SETEND_INST; \
  3241. case 40: goto CLREX_INST; \
  3242. case 41: goto REV16_INST; \
  3243. case 42: goto USAD8_INST; \
  3244. case 43: goto SXTB_INST; \
  3245. case 44: goto UXTB_INST; \
  3246. case 45: goto SXTH_INST; \
  3247. case 46: goto SXTB16_INST; \
  3248. case 47: goto UXTH_INST; \
  3249. case 48: goto UXTB16_INST; \
  3250. case 49: goto CPY_INST; \
  3251. case 50: goto UXTAB_INST; \
  3252. case 51: goto SSUB8_INST; \
  3253. case 52: goto SHSUB8_INST; \
  3254. case 53: goto SSUBADDX_INST; \
  3255. case 54: goto STREX_INST; \
  3256. case 55: goto STREXB_INST; \
  3257. case 56: goto SWP_INST; \
  3258. case 57: goto SWPB_INST; \
  3259. case 58: goto SSUB16_INST; \
  3260. case 59: goto SSAT16_INST; \
  3261. case 60: goto SHSUBADDX_INST; \
  3262. case 61: goto QSUBADDX_INST; \
  3263. case 62: goto SHADDSUBX_INST; \
  3264. case 63: goto SHADD8_INST; \
  3265. case 64: goto SHADD16_INST; \
  3266. case 65: goto SEL_INST; \
  3267. case 66: goto SADDSUBX_INST; \
  3268. case 67: goto SADD8_INST; \
  3269. case 68: goto SADD16_INST; \
  3270. case 69: goto SHSUB16_INST; \
  3271. case 70: goto UMAAL_INST; \
  3272. case 71: goto UXTAB16_INST; \
  3273. case 72: goto USUBADDX_INST; \
  3274. case 73: goto USUB8_INST; \
  3275. case 74: goto USUB16_INST; \
  3276. case 75: goto USAT16_INST; \
  3277. case 76: goto USADA8_INST; \
  3278. case 77: goto UQSUBADDX_INST; \
  3279. case 78: goto UQSUB8_INST; \
  3280. case 79: goto UQSUB16_INST; \
  3281. case 80: goto UQADDSUBX_INST; \
  3282. case 81: goto UQADD8_INST; \
  3283. case 82: goto UQADD16_INST; \
  3284. case 83: goto SXTAB_INST; \
  3285. case 84: goto UHSUBADDX_INST; \
  3286. case 85: goto UHSUB8_INST; \
  3287. case 86: goto UHSUB16_INST; \
  3288. case 87: goto UHADDSUBX_INST; \
  3289. case 88: goto UHADD8_INST; \
  3290. case 89: goto UHADD16_INST; \
  3291. case 90: goto UADDSUBX_INST; \
  3292. case 91: goto UADD8_INST; \
  3293. case 92: goto UADD16_INST; \
  3294. case 93: goto SXTAH_INST; \
  3295. case 94: goto SXTAB16_INST; \
  3296. case 95: goto QADD8_INST; \
  3297. case 96: goto BXJ_INST; \
  3298. case 97: goto CLZ_INST; \
  3299. case 98: goto UXTAH_INST; \
  3300. case 99: goto BX_INST; \
  3301. case 100: goto REV_INST; \
  3302. case 101: goto BLX_INST; \
  3303. case 102: goto REVSH_INST; \
  3304. case 103: goto QADD_INST; \
  3305. case 104: goto QADD16_INST; \
  3306. case 105: goto QADDSUBX_INST; \
  3307. case 106: goto LDREX_INST; \
  3308. case 107: goto QDADD_INST; \
  3309. case 108: goto QDSUB_INST; \
  3310. case 109: goto QSUB_INST; \
  3311. case 110: goto LDREXB_INST; \
  3312. case 111: goto QSUB8_INST; \
  3313. case 112: goto QSUB16_INST; \
  3314. case 113: goto SMUAD_INST; \
  3315. case 114: goto SMMUL_INST; \
  3316. case 115: goto SMUSD_INST; \
  3317. case 116: goto SMLSD_INST; \
  3318. case 117: goto SMLSLD_INST; \
  3319. case 118: goto SMMLA_INST; \
  3320. case 119: goto SMMLS_INST; \
  3321. case 120: goto SMLALD_INST; \
  3322. case 121: goto SMLAD_INST; \
  3323. case 122: goto SMLAW_INST; \
  3324. case 123: goto SMULW_INST; \
  3325. case 124: goto PKHTB_INST; \
  3326. case 125: goto PKHBT_INST; \
  3327. case 126: goto SMUL_INST; \
  3328. case 127: goto SMLALXY_INST; \
  3329. case 128: goto SMLA_INST; \
  3330. case 129: goto MCRR_INST; \
  3331. case 130: goto MRRC_INST; \
  3332. case 131: goto CMP_INST; \
  3333. case 132: goto TST_INST; \
  3334. case 133: goto TEQ_INST; \
  3335. case 134: goto CMN_INST; \
  3336. case 135: goto SMULL_INST; \
  3337. case 136: goto UMULL_INST; \
  3338. case 137: goto UMLAL_INST; \
  3339. case 138: goto SMLAL_INST; \
  3340. case 139: goto MUL_INST; \
  3341. case 140: goto MLA_INST; \
  3342. case 141: goto SSAT_INST; \
  3343. case 142: goto USAT_INST; \
  3344. case 143: goto MRS_INST; \
  3345. case 144: goto MSR_INST; \
  3346. case 145: goto AND_INST; \
  3347. case 146: goto BIC_INST; \
  3348. case 147: goto LDM_INST; \
  3349. case 148: goto EOR_INST; \
  3350. case 149: goto ADD_INST; \
  3351. case 150: goto RSB_INST; \
  3352. case 151: goto RSC_INST; \
  3353. case 152: goto SBC_INST; \
  3354. case 153: goto ADC_INST; \
  3355. case 154: goto SUB_INST; \
  3356. case 155: goto ORR_INST; \
  3357. case 156: goto MVN_INST; \
  3358. case 157: goto MOV_INST; \
  3359. case 158: goto STM_INST; \
  3360. case 159: goto LDM_INST; \
  3361. case 160: goto LDRSH_INST; \
  3362. case 161: goto STM_INST; \
  3363. case 162: goto LDM_INST; \
  3364. case 163: goto LDRSB_INST; \
  3365. case 164: goto STRD_INST; \
  3366. case 165: goto LDRH_INST; \
  3367. case 166: goto STRH_INST; \
  3368. case 167: goto LDRD_INST; \
  3369. case 168: goto STRT_INST; \
  3370. case 169: goto STRBT_INST; \
  3371. case 170: goto LDRBT_INST; \
  3372. case 171: goto LDRT_INST; \
  3373. case 172: goto MRC_INST; \
  3374. case 173: goto MCR_INST; \
  3375. case 174: goto MSR_INST; \
  3376. case 175: goto LDRB_INST; \
  3377. case 176: goto STRB_INST; \
  3378. case 177: goto LDR_INST; \
  3379. case 178: goto LDRCOND_INST ; \
  3380. case 179: goto STR_INST; \
  3381. case 180: goto CDP_INST; \
  3382. case 181: goto STC_INST; \
  3383. case 182: goto LDC_INST; \
  3384. case 183: goto LDREXD_INST; \
  3385. case 184: goto STREXD_INST; \
  3386. case 185: goto LDREXH_INST; \
  3387. case 186: goto STREXH_INST; \
  3388. case 187: goto SWI_INST; \
  3389. case 188: goto BBL_INST; \
  3390. case 189: goto B_2_THUMB ; \
  3391. case 190: goto B_COND_THUMB ; \
  3392. case 191: goto BL_1_THUMB ; \
  3393. case 192: goto BL_2_THUMB ; \
  3394. case 193: goto BLX_1_THUMB ; \
  3395. case 194: goto DISPATCH; \
  3396. case 195: goto INIT_INST_LENGTH; \
  3397. case 196: goto END; \
  3398. }
  3399. #endif
  3400. #define UPDATE_NFLAG(dst) (cpu->NFlag = BIT(dst, 31) ? 1 : 0)
  3401. #define UPDATE_ZFLAG(dst) (cpu->ZFlag = dst ? 0 : 1)
  3402. #define UPDATE_CFLAG_WITH_SC (cpu->CFlag = cpu->shifter_carry_out)
  3403. #define SAVE_NZCVT cpu->Cpsr = (cpu->Cpsr & 0x0fffffdf) | \
  3404. (cpu->NFlag << 31) | \
  3405. (cpu->ZFlag << 30) | \
  3406. (cpu->CFlag << 29) | \
  3407. (cpu->VFlag << 28) | \
  3408. (cpu->TFlag << 5)
  3409. #define LOAD_NZCVT cpu->NFlag = (cpu->Cpsr >> 31); \
  3410. cpu->ZFlag = (cpu->Cpsr >> 30) & 1; \
  3411. cpu->CFlag = (cpu->Cpsr >> 29) & 1; \
  3412. cpu->VFlag = (cpu->Cpsr >> 28) & 1; \
  3413. cpu->TFlag = (cpu->Cpsr >> 5) & 1;
  3414. #define CurrentModeHasSPSR (cpu->Mode != SYSTEM32MODE) && (cpu->Mode != USER32MODE)
  3415. #define PC (cpu->Reg[15])
  3416. #define CHECK_EXT_INT if (!cpu->NirqSig && !(cpu->Cpsr & 0x80)) goto END;
  3417. // GCC and Clang have a C++ extension to support a lookup table of labels. Otherwise, fallback
  3418. // to a clunky switch statement.
  3419. #if defined __GNUC__ || defined __clang__
  3420. void *InstLabel[] = {
  3421. &&VMLA_INST, &&VMLS_INST, &&VNMLA_INST, &&VNMLA_INST, &&VNMLS_INST, &&VNMUL_INST, &&VMUL_INST, &&VADD_INST, &&VSUB_INST,
  3422. &&VDIV_INST, &&VMOVI_INST, &&VMOVR_INST, &&VABS_INST, &&VNEG_INST, &&VSQRT_INST, &&VCMP_INST, &&VCMP2_INST, &&VCVTBDS_INST,
  3423. &&VCVTBFF_INST, &&VCVTBFI_INST, &&VMOVBRS_INST, &&VMSR_INST, &&VMOVBRC_INST, &&VMRS_INST, &&VMOVBCR_INST, &&VMOVBRRSS_INST,
  3424. &&VMOVBRRD_INST, &&VSTR_INST, &&VPUSH_INST, &&VSTM_INST, &&VPOP_INST, &&VLDR_INST, &&VLDM_INST,
  3425. &&SRS_INST,&&RFE_INST,&&BKPT_INST,&&BLX_INST,&&CPS_INST,&&PLD_INST,&&SETEND_INST,&&CLREX_INST,&&REV16_INST,&&USAD8_INST,&&SXTB_INST,
  3426. &&UXTB_INST,&&SXTH_INST,&&SXTB16_INST,&&UXTH_INST,&&UXTB16_INST,&&CPY_INST,&&UXTAB_INST,&&SSUB8_INST,&&SHSUB8_INST,&&SSUBADDX_INST,
  3427. &&STREX_INST,&&STREXB_INST,&&SWP_INST,&&SWPB_INST,&&SSUB16_INST,&&SSAT16_INST,&&SHSUBADDX_INST,&&QSUBADDX_INST,&&SHADDSUBX_INST,
  3428. &&SHADD8_INST,&&SHADD16_INST,&&SEL_INST,&&SADDSUBX_INST,&&SADD8_INST,&&SADD16_INST,&&SHSUB16_INST,&&UMAAL_INST,&&UXTAB16_INST,
  3429. &&USUBADDX_INST,&&USUB8_INST,&&USUB16_INST,&&USAT16_INST,&&USADA8_INST,&&UQSUBADDX_INST,&&UQSUB8_INST,&&UQSUB16_INST,
  3430. &&UQADDSUBX_INST,&&UQADD8_INST,&&UQADD16_INST,&&SXTAB_INST,&&UHSUBADDX_INST,&&UHSUB8_INST,&&UHSUB16_INST,&&UHADDSUBX_INST,&&UHADD8_INST,
  3431. &&UHADD16_INST,&&UADDSUBX_INST,&&UADD8_INST,&&UADD16_INST,&&SXTAH_INST,&&SXTAB16_INST,&&QADD8_INST,&&BXJ_INST,&&CLZ_INST,&&UXTAH_INST,
  3432. &&BX_INST,&&REV_INST,&&BLX_INST,&&REVSH_INST,&&QADD_INST,&&QADD16_INST,&&QADDSUBX_INST,&&LDREX_INST,&&QDADD_INST,&&QDSUB_INST,
  3433. &&QSUB_INST,&&LDREXB_INST,&&QSUB8_INST,&&QSUB16_INST,&&SMUAD_INST,&&SMMUL_INST,&&SMUSD_INST,&&SMLSD_INST,&&SMLSLD_INST,&&SMMLA_INST,
  3434. &&SMMLS_INST,&&SMLALD_INST,&&SMLAD_INST,&&SMLAW_INST,&&SMULW_INST,&&PKHTB_INST,&&PKHBT_INST,&&SMUL_INST,&&SMLALXY_INST,&&SMLA_INST,
  3435. &&MCRR_INST,&&MRRC_INST,&&CMP_INST,&&TST_INST,&&TEQ_INST,&&CMN_INST,&&SMULL_INST,&&UMULL_INST,&&UMLAL_INST,&&SMLAL_INST,&&MUL_INST,
  3436. &&MLA_INST,&&SSAT_INST,&&USAT_INST,&&MRS_INST,&&MSR_INST,&&AND_INST,&&BIC_INST,&&LDM_INST,&&EOR_INST,&&ADD_INST,&&RSB_INST,&&RSC_INST,
  3437. &&SBC_INST,&&ADC_INST,&&SUB_INST,&&ORR_INST,&&MVN_INST,&&MOV_INST,&&STM_INST,&&LDM_INST,&&LDRSH_INST,&&STM_INST,&&LDM_INST,&&LDRSB_INST,
  3438. &&STRD_INST,&&LDRH_INST,&&STRH_INST,&&LDRD_INST,&&STRT_INST,&&STRBT_INST,&&LDRBT_INST,&&LDRT_INST,&&MRC_INST,&&MCR_INST,&&MSR_INST,
  3439. &&LDRB_INST,&&STRB_INST,&&LDR_INST,&&LDRCOND_INST, &&STR_INST,&&CDP_INST,&&STC_INST,&&LDC_INST, &&LDREXD_INST,
  3440. &&STREXD_INST,&&LDREXH_INST,&&STREXH_INST, &&SWI_INST,&&BBL_INST,
  3441. &&B_2_THUMB, &&B_COND_THUMB,&&BL_1_THUMB, &&BL_2_THUMB, &&BLX_1_THUMB, &&DISPATCH,
  3442. &&INIT_INST_LENGTH,&&END
  3443. };
  3444. #endif
  3445. arm_inst* inst_base;
  3446. unsigned int addr;
  3447. unsigned int phys_addr;
  3448. unsigned int num_instrs = 0;
  3449. int ptr;
  3450. LOAD_NZCVT;
  3451. DISPATCH:
  3452. {
  3453. if (!cpu->NirqSig) {
  3454. if (!(cpu->Cpsr & 0x80)) {
  3455. goto END;
  3456. }
  3457. }
  3458. if (cpu->TFlag)
  3459. cpu->Reg[15] &= 0xfffffffe;
  3460. else
  3461. cpu->Reg[15] &= 0xfffffffc;
  3462. phys_addr = cpu->Reg[15];
  3463. // Find the cached instruction cream, otherwise translate it...
  3464. auto itr = cpu->instruction_cache.find(cpu->Reg[15]);
  3465. if (itr != cpu->instruction_cache.end()) {
  3466. ptr = itr->second;
  3467. } else {
  3468. if (InterpreterTranslate(cpu, ptr, cpu->Reg[15]) == FETCH_EXCEPTION)
  3469. goto END;
  3470. }
  3471. inst_base = (arm_inst *)&inst_buf[ptr];
  3472. GOTO_NEXT_INST;
  3473. }
  3474. ADC_INST:
  3475. {
  3476. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3477. adc_inst* const inst_cream = (adc_inst*)inst_base->component;
  3478. bool carry;
  3479. bool overflow;
  3480. RD = AddWithCarry(RN, SHIFTER_OPERAND, cpu->CFlag, &carry, &overflow);
  3481. if (inst_cream->S && (inst_cream->Rd == 15)) {
  3482. if (CurrentModeHasSPSR) {
  3483. cpu->Cpsr = cpu->Spsr_copy;
  3484. switch_mode(cpu, cpu->Spsr_copy & 0x1f);
  3485. LOAD_NZCVT;
  3486. }
  3487. } else if (inst_cream->S) {
  3488. UPDATE_NFLAG(RD);
  3489. UPDATE_ZFLAG(RD);
  3490. cpu->CFlag = carry;
  3491. cpu->VFlag = overflow;
  3492. }
  3493. if (inst_cream->Rd == 15) {
  3494. INC_PC(sizeof(adc_inst));
  3495. goto DISPATCH;
  3496. }
  3497. }
  3498. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3499. INC_PC(sizeof(adc_inst));
  3500. FETCH_INST;
  3501. GOTO_NEXT_INST;
  3502. }
  3503. ADD_INST:
  3504. {
  3505. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3506. add_inst* const inst_cream = (add_inst*)inst_base->component;
  3507. u32 rn_val = RN;
  3508. if (inst_cream->Rn == 15)
  3509. rn_val += 2 * GET_INST_SIZE(cpu);
  3510. bool carry;
  3511. bool overflow;
  3512. RD = AddWithCarry(rn_val, SHIFTER_OPERAND, 0, &carry, &overflow);
  3513. if (inst_cream->S && (inst_cream->Rd == 15)) {
  3514. if (CurrentModeHasSPSR) {
  3515. cpu->Cpsr = cpu->Spsr_copy;
  3516. switch_mode(cpu, cpu->Cpsr & 0x1f);
  3517. LOAD_NZCVT;
  3518. }
  3519. } else if (inst_cream->S) {
  3520. UPDATE_NFLAG(RD);
  3521. UPDATE_ZFLAG(RD);
  3522. cpu->CFlag = carry;
  3523. cpu->VFlag = overflow;
  3524. }
  3525. if (inst_cream->Rd == 15) {
  3526. INC_PC(sizeof(add_inst));
  3527. goto DISPATCH;
  3528. }
  3529. }
  3530. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3531. INC_PC(sizeof(add_inst));
  3532. FETCH_INST;
  3533. GOTO_NEXT_INST;
  3534. }
  3535. AND_INST:
  3536. {
  3537. and_inst *inst_cream = (and_inst *)inst_base->component;
  3538. if ((inst_base->cond == 0xe) || CondPassed(cpu, inst_base->cond)) {
  3539. u32 lop = RN;
  3540. u32 rop = SHIFTER_OPERAND;
  3541. RD = lop & rop;
  3542. if (inst_cream->S && (inst_cream->Rd == 15)) {
  3543. if (CurrentModeHasSPSR) {
  3544. cpu->Cpsr = cpu->Spsr_copy;
  3545. switch_mode(cpu, cpu->Cpsr & 0x1f);
  3546. LOAD_NZCVT;
  3547. }
  3548. } else if (inst_cream->S) {
  3549. UPDATE_NFLAG(RD);
  3550. UPDATE_ZFLAG(RD);
  3551. UPDATE_CFLAG_WITH_SC;
  3552. }
  3553. if (inst_cream->Rd == 15) {
  3554. INC_PC(sizeof(and_inst));
  3555. goto DISPATCH;
  3556. }
  3557. }
  3558. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3559. INC_PC(sizeof(and_inst));
  3560. FETCH_INST;
  3561. GOTO_NEXT_INST;
  3562. }
  3563. BBL_INST:
  3564. {
  3565. if ((inst_base->cond == 0xe) || CondPassed(cpu, inst_base->cond)) {
  3566. bbl_inst *inst_cream = (bbl_inst *)inst_base->component;
  3567. if (inst_cream->L) {
  3568. LINK_RTN_ADDR;
  3569. }
  3570. SET_PC;
  3571. INC_PC(sizeof(bbl_inst));
  3572. goto DISPATCH;
  3573. }
  3574. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3575. INC_PC(sizeof(bbl_inst));
  3576. goto DISPATCH;
  3577. }
  3578. BIC_INST:
  3579. {
  3580. bic_inst *inst_cream = (bic_inst *)inst_base->component;
  3581. if ((inst_base->cond == 0xe) || CondPassed(cpu, inst_base->cond)) {
  3582. u32 lop = RN;
  3583. if (inst_cream->Rn == 15) {
  3584. lop += 2 * GET_INST_SIZE(cpu);
  3585. }
  3586. u32 rop = SHIFTER_OPERAND;
  3587. RD = lop & (~rop);
  3588. if ((inst_cream->S) && (inst_cream->Rd == 15)) {
  3589. if (CurrentModeHasSPSR) {
  3590. cpu->Cpsr = cpu->Spsr_copy;
  3591. switch_mode(cpu, cpu->Spsr_copy & 0x1f);
  3592. LOAD_NZCVT;
  3593. }
  3594. } else if (inst_cream->S) {
  3595. UPDATE_NFLAG(RD);
  3596. UPDATE_ZFLAG(RD);
  3597. UPDATE_CFLAG_WITH_SC;
  3598. }
  3599. if (inst_cream->Rd == 15) {
  3600. INC_PC(sizeof(bic_inst));
  3601. goto DISPATCH;
  3602. }
  3603. }
  3604. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3605. INC_PC(sizeof(bic_inst));
  3606. FETCH_INST;
  3607. GOTO_NEXT_INST;
  3608. }
  3609. BKPT_INST:
  3610. {
  3611. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3612. bkpt_inst* const inst_cream = (bkpt_inst*)inst_base->component;
  3613. LOG_DEBUG(Core_ARM11, "Breakpoint instruction hit. Immediate: 0x%08X", inst_cream->imm);
  3614. }
  3615. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3616. INC_PC(sizeof(bkpt_inst));
  3617. FETCH_INST;
  3618. GOTO_NEXT_INST;
  3619. }
  3620. BLX_INST:
  3621. {
  3622. blx_inst *inst_cream = (blx_inst *)inst_base->component;
  3623. if ((inst_base->cond == 0xe) || CondPassed(cpu, inst_base->cond)) {
  3624. unsigned int inst = inst_cream->inst;
  3625. if (BITS(inst, 20, 27) == 0x12 && BITS(inst, 4, 7) == 0x3) {
  3626. cpu->Reg[14] = (cpu->Reg[15] + GET_INST_SIZE(cpu));
  3627. if(cpu->TFlag)
  3628. cpu->Reg[14] |= 0x1;
  3629. cpu->Reg[15] = cpu->Reg[inst_cream->val.Rm] & 0xfffffffe;
  3630. cpu->TFlag = cpu->Reg[inst_cream->val.Rm] & 0x1;
  3631. } else {
  3632. cpu->Reg[14] = (cpu->Reg[15] + GET_INST_SIZE(cpu));
  3633. cpu->TFlag = 0x1;
  3634. int signed_int = inst_cream->val.signed_immed_24;
  3635. signed_int = (signed_int & 0x800000) ? (0x3F000000 | signed_int) : signed_int;
  3636. signed_int = signed_int << 2;
  3637. cpu->Reg[15] = cpu->Reg[15] + 8 + signed_int + (BIT(inst, 24) << 1);
  3638. }
  3639. INC_PC(sizeof(blx_inst));
  3640. goto DISPATCH;
  3641. }
  3642. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3643. INC_PC(sizeof(blx_inst));
  3644. goto DISPATCH;
  3645. }
  3646. BX_INST:
  3647. BXJ_INST:
  3648. {
  3649. // Note that only the 'fail' case of BXJ is emulated. This is because
  3650. // the facilities for Jazelle emulation are not implemented.
  3651. //
  3652. // According to the ARM documentation on BXJ, if setting the J bit in the APSR
  3653. // fails, then BXJ functions identically like a regular BX instruction.
  3654. //
  3655. // This is sufficient for citra, as the CPU for the 3DS does not implement Jazelle.
  3656. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3657. bx_inst* const inst_cream = (bx_inst*)inst_base->component;
  3658. if (inst_cream->Rm == 15)
  3659. LOG_WARNING(Core_ARM11, "BX at pc %x: use of Rm = R15 is discouraged", cpu->Reg[15]);
  3660. cpu->TFlag = cpu->Reg[inst_cream->Rm] & 0x1;
  3661. cpu->Reg[15] = cpu->Reg[inst_cream->Rm] & 0xfffffffe;
  3662. INC_PC(sizeof(bx_inst));
  3663. goto DISPATCH;
  3664. }
  3665. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3666. INC_PC(sizeof(bx_inst));
  3667. goto DISPATCH;
  3668. }
  3669. CDP_INST:
  3670. {
  3671. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3672. // Undefined instruction here
  3673. cpu->NumInstrsToExecute = 0;
  3674. return num_instrs;
  3675. }
  3676. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3677. INC_PC(sizeof(cdp_inst));
  3678. FETCH_INST;
  3679. GOTO_NEXT_INST;
  3680. }
  3681. CLREX_INST:
  3682. {
  3683. remove_exclusive(cpu, 0);
  3684. cpu->exclusive_state = 0;
  3685. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3686. INC_PC(sizeof(clrex_inst));
  3687. FETCH_INST;
  3688. GOTO_NEXT_INST;
  3689. }
  3690. CLZ_INST:
  3691. {
  3692. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3693. clz_inst* inst_cream = (clz_inst*)inst_base->component;
  3694. RD = clz(RM);
  3695. }
  3696. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3697. INC_PC(sizeof(clz_inst));
  3698. FETCH_INST;
  3699. GOTO_NEXT_INST;
  3700. }
  3701. CMN_INST:
  3702. {
  3703. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3704. cmn_inst* const inst_cream = (cmn_inst*)inst_base->component;
  3705. bool carry;
  3706. bool overflow;
  3707. u32 result = AddWithCarry(RN, SHIFTER_OPERAND, 0, &carry, &overflow);
  3708. UPDATE_NFLAG(result);
  3709. UPDATE_ZFLAG(result);
  3710. cpu->CFlag = carry;
  3711. cpu->VFlag = overflow;
  3712. }
  3713. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3714. INC_PC(sizeof(cmn_inst));
  3715. FETCH_INST;
  3716. GOTO_NEXT_INST;
  3717. }
  3718. CMP_INST:
  3719. {
  3720. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3721. cmp_inst* const inst_cream = (cmp_inst*)inst_base->component;
  3722. u32 rn_val = RN;
  3723. if (inst_cream->Rn == 15)
  3724. rn_val += 2 * GET_INST_SIZE(cpu);
  3725. bool carry;
  3726. bool overflow;
  3727. u32 result = AddWithCarry(rn_val, ~SHIFTER_OPERAND, 1, &carry, &overflow);
  3728. UPDATE_NFLAG(result);
  3729. UPDATE_ZFLAG(result);
  3730. cpu->CFlag = carry;
  3731. cpu->VFlag = overflow;
  3732. }
  3733. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3734. INC_PC(sizeof(cmp_inst));
  3735. FETCH_INST;
  3736. GOTO_NEXT_INST;
  3737. }
  3738. CPS_INST:
  3739. {
  3740. cps_inst *inst_cream = (cps_inst *)inst_base->component;
  3741. uint32_t aif_val = 0;
  3742. uint32_t aif_mask = 0;
  3743. if (InAPrivilegedMode(cpu)) {
  3744. if (inst_cream->imod1) {
  3745. if (inst_cream->A) {
  3746. aif_val |= (inst_cream->imod0 << 8);
  3747. aif_mask |= 1 << 8;
  3748. }
  3749. if (inst_cream->I) {
  3750. aif_val |= (inst_cream->imod0 << 7);
  3751. aif_mask |= 1 << 7;
  3752. }
  3753. if (inst_cream->F) {
  3754. aif_val |= (inst_cream->imod0 << 6);
  3755. aif_mask |= 1 << 6;
  3756. }
  3757. aif_mask = ~aif_mask;
  3758. cpu->Cpsr = (cpu->Cpsr & aif_mask) | aif_val;
  3759. }
  3760. if (inst_cream->mmod) {
  3761. cpu->Cpsr = (cpu->Cpsr & 0xffffffe0) | inst_cream->mode;
  3762. switch_mode(cpu, inst_cream->mode);
  3763. }
  3764. }
  3765. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3766. INC_PC(sizeof(cps_inst));
  3767. FETCH_INST;
  3768. GOTO_NEXT_INST;
  3769. }
  3770. CPY_INST:
  3771. {
  3772. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3773. mov_inst* inst_cream = (mov_inst*)inst_base->component;
  3774. RD = SHIFTER_OPERAND;
  3775. if (inst_cream->Rd == 15) {
  3776. INC_PC(sizeof(mov_inst));
  3777. goto DISPATCH;
  3778. }
  3779. }
  3780. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3781. INC_PC(sizeof(mov_inst));
  3782. FETCH_INST;
  3783. GOTO_NEXT_INST;
  3784. }
  3785. EOR_INST:
  3786. {
  3787. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3788. eor_inst* inst_cream = (eor_inst*)inst_base->component;
  3789. u32 lop = RN;
  3790. if (inst_cream->Rn == 15) {
  3791. lop += 2 * GET_INST_SIZE(cpu);
  3792. }
  3793. u32 rop = SHIFTER_OPERAND;
  3794. RD = lop ^ rop;
  3795. if (inst_cream->S && (inst_cream->Rd == 15)) {
  3796. if (CurrentModeHasSPSR) {
  3797. cpu->Cpsr = cpu->Spsr_copy;
  3798. switch_mode(cpu, cpu->Spsr_copy & 0x1f);
  3799. LOAD_NZCVT;
  3800. }
  3801. } else if (inst_cream->S) {
  3802. UPDATE_NFLAG(RD);
  3803. UPDATE_ZFLAG(RD);
  3804. UPDATE_CFLAG_WITH_SC;
  3805. }
  3806. if (inst_cream->Rd == 15) {
  3807. INC_PC(sizeof(eor_inst));
  3808. goto DISPATCH;
  3809. }
  3810. }
  3811. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3812. INC_PC(sizeof(eor_inst));
  3813. FETCH_INST;
  3814. GOTO_NEXT_INST;
  3815. }
  3816. LDC_INST:
  3817. {
  3818. // Instruction not implemented
  3819. //LOG_CRITICAL(Core_ARM11, "unimplemented instruction");
  3820. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3821. INC_PC(sizeof(ldc_inst));
  3822. FETCH_INST;
  3823. GOTO_NEXT_INST;
  3824. }
  3825. LDM_INST:
  3826. {
  3827. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3828. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  3829. inst_cream->get_addr(cpu, inst_cream->inst, addr, 1);
  3830. unsigned int inst = inst_cream->inst;
  3831. if (BIT(inst, 22) && !BIT(inst, 15)) {
  3832. for (int i = 0; i < 13; i++) {
  3833. if(BIT(inst, i)) {
  3834. cpu->Reg[i] = ReadMemory32(cpu, addr);
  3835. addr += 4;
  3836. }
  3837. }
  3838. if (BIT(inst, 13)) {
  3839. if (cpu->Mode == USER32MODE)
  3840. cpu->Reg[13] = ReadMemory32(cpu, addr);
  3841. else
  3842. cpu->Reg_usr[0] = ReadMemory32(cpu, addr);
  3843. addr += 4;
  3844. }
  3845. if (BIT(inst, 14)) {
  3846. if (cpu->Mode == USER32MODE)
  3847. cpu->Reg[14] = ReadMemory32(cpu, addr);
  3848. else
  3849. cpu->Reg_usr[1] = ReadMemory32(cpu, addr);
  3850. addr += 4;
  3851. }
  3852. } else if (!BIT(inst, 22)) {
  3853. for(int i = 0; i < 16; i++ ){
  3854. if(BIT(inst, i)){
  3855. unsigned int ret = ReadMemory32(cpu, addr);
  3856. // For armv5t, should enter thumb when bits[0] is non-zero.
  3857. if(i == 15){
  3858. cpu->TFlag = ret & 0x1;
  3859. ret &= 0xFFFFFFFE;
  3860. }
  3861. cpu->Reg[i] = ret;
  3862. addr += 4;
  3863. }
  3864. }
  3865. } else if (BIT(inst, 22) && BIT(inst, 15)) {
  3866. for(int i = 0; i < 15; i++ ){
  3867. if(BIT(inst, i)){
  3868. cpu->Reg[i] = ReadMemory32(cpu, addr);
  3869. addr += 4;
  3870. }
  3871. }
  3872. if (CurrentModeHasSPSR) {
  3873. cpu->Cpsr = cpu->Spsr_copy;
  3874. switch_mode(cpu, cpu->Cpsr & 0x1f);
  3875. LOAD_NZCVT;
  3876. }
  3877. cpu->Reg[15] = ReadMemory32(cpu, addr);
  3878. }
  3879. if (BIT(inst, 15)) {
  3880. INC_PC(sizeof(ldst_inst));
  3881. goto DISPATCH;
  3882. }
  3883. }
  3884. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3885. INC_PC(sizeof(ldst_inst));
  3886. FETCH_INST;
  3887. GOTO_NEXT_INST;
  3888. }
  3889. SXTH_INST:
  3890. {
  3891. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3892. sxth_inst* inst_cream = (sxth_inst*)inst_base->component;
  3893. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate);
  3894. if (BIT(operand2, 15)) {
  3895. operand2 |= 0xffff0000;
  3896. } else {
  3897. operand2 &= 0xffff;
  3898. }
  3899. RD = operand2;
  3900. }
  3901. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3902. INC_PC(sizeof(sxth_inst));
  3903. FETCH_INST;
  3904. GOTO_NEXT_INST;
  3905. }
  3906. LDR_INST:
  3907. {
  3908. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  3909. inst_cream->get_addr(cpu, inst_cream->inst, addr, 1);
  3910. unsigned int value = ReadMemory32(cpu, addr);
  3911. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = value;
  3912. if (BITS(inst_cream->inst, 12, 15) == 15) {
  3913. // For armv5t, should enter thumb when bits[0] is non-zero.
  3914. cpu->TFlag = value & 0x1;
  3915. cpu->Reg[15] &= 0xFFFFFFFE;
  3916. INC_PC(sizeof(ldst_inst));
  3917. goto DISPATCH;
  3918. }
  3919. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3920. INC_PC(sizeof(ldst_inst));
  3921. FETCH_INST;
  3922. GOTO_NEXT_INST;
  3923. }
  3924. LDRCOND_INST:
  3925. {
  3926. if (CondPassed(cpu, inst_base->cond)) {
  3927. ldst_inst *inst_cream = (ldst_inst *)inst_base->component;
  3928. inst_cream->get_addr(cpu, inst_cream->inst, addr, 1);
  3929. unsigned int value = ReadMemory32(cpu, addr);
  3930. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = value;
  3931. if (BITS(inst_cream->inst, 12, 15) == 15) {
  3932. // For armv5t, should enter thumb when bits[0] is non-zero.
  3933. cpu->TFlag = value & 0x1;
  3934. cpu->Reg[15] &= 0xFFFFFFFE;
  3935. INC_PC(sizeof(ldst_inst));
  3936. goto DISPATCH;
  3937. }
  3938. }
  3939. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3940. INC_PC(sizeof(ldst_inst));
  3941. FETCH_INST;
  3942. GOTO_NEXT_INST;
  3943. }
  3944. UXTH_INST:
  3945. {
  3946. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3947. uxth_inst* inst_cream = (uxth_inst*)inst_base->component;
  3948. RD = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xffff;
  3949. }
  3950. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3951. INC_PC(sizeof(uxth_inst));
  3952. FETCH_INST;
  3953. GOTO_NEXT_INST;
  3954. }
  3955. UXTAH_INST:
  3956. {
  3957. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3958. uxtah_inst* inst_cream = (uxtah_inst*)inst_base->component;
  3959. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xffff;
  3960. RD = RN + operand2;
  3961. }
  3962. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3963. INC_PC(sizeof(uxtah_inst));
  3964. FETCH_INST;
  3965. GOTO_NEXT_INST;
  3966. }
  3967. LDRB_INST:
  3968. {
  3969. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3970. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  3971. inst_cream->get_addr(cpu, inst_cream->inst, addr, 1);
  3972. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = Memory::Read8(addr);
  3973. if (BITS(inst_cream->inst, 12, 15) == 15) {
  3974. INC_PC(sizeof(ldst_inst));
  3975. goto DISPATCH;
  3976. }
  3977. }
  3978. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3979. INC_PC(sizeof(ldst_inst));
  3980. FETCH_INST;
  3981. GOTO_NEXT_INST;
  3982. }
  3983. LDRBT_INST:
  3984. {
  3985. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  3986. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  3987. inst_cream->get_addr(cpu, inst_cream->inst, addr, 1);
  3988. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = Memory::Read8(addr);
  3989. if (BITS(inst_cream->inst, 12, 15) == 15) {
  3990. INC_PC(sizeof(ldst_inst));
  3991. goto DISPATCH;
  3992. }
  3993. }
  3994. cpu->Reg[15] += GET_INST_SIZE(cpu);
  3995. INC_PC(sizeof(ldst_inst));
  3996. FETCH_INST;
  3997. GOTO_NEXT_INST;
  3998. }
  3999. LDRD_INST:
  4000. {
  4001. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4002. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  4003. // Should check if RD is even-numbered, Rd != 14, addr[0:1] == 0, (CP15_reg1_U == 1 || addr[2] == 0)
  4004. inst_cream->get_addr(cpu, inst_cream->inst, addr, 1);
  4005. // The 3DS doesn't have LPAE (Large Physical Access Extension), so it
  4006. // wouldn't do this as a single read.
  4007. cpu->Reg[BITS(inst_cream->inst, 12, 15) + 0] = ReadMemory32(cpu, addr);
  4008. cpu->Reg[BITS(inst_cream->inst, 12, 15) + 1] = ReadMemory32(cpu, addr + 4);
  4009. // No dispatch since this operation should not modify R15
  4010. }
  4011. cpu->Reg[15] += 4;
  4012. INC_PC(sizeof(ldst_inst));
  4013. FETCH_INST;
  4014. GOTO_NEXT_INST;
  4015. }
  4016. LDREX_INST:
  4017. {
  4018. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4019. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  4020. unsigned int read_addr = RN;
  4021. add_exclusive_addr(cpu, read_addr);
  4022. cpu->exclusive_state = 1;
  4023. RD = ReadMemory32(cpu, read_addr);
  4024. if (inst_cream->Rd == 15) {
  4025. INC_PC(sizeof(generic_arm_inst));
  4026. goto DISPATCH;
  4027. }
  4028. }
  4029. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4030. INC_PC(sizeof(generic_arm_inst));
  4031. FETCH_INST;
  4032. GOTO_NEXT_INST;
  4033. }
  4034. LDREXB_INST:
  4035. {
  4036. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4037. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  4038. unsigned int read_addr = RN;
  4039. add_exclusive_addr(cpu, read_addr);
  4040. cpu->exclusive_state = 1;
  4041. RD = Memory::Read8(read_addr);
  4042. if (inst_cream->Rd == 15) {
  4043. INC_PC(sizeof(generic_arm_inst));
  4044. goto DISPATCH;
  4045. }
  4046. }
  4047. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4048. INC_PC(sizeof(generic_arm_inst));
  4049. FETCH_INST;
  4050. GOTO_NEXT_INST;
  4051. }
  4052. LDREXH_INST:
  4053. {
  4054. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4055. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  4056. unsigned int read_addr = RN;
  4057. add_exclusive_addr(cpu, read_addr);
  4058. cpu->exclusive_state = 1;
  4059. RD = ReadMemory16(cpu, read_addr);
  4060. if (inst_cream->Rd == 15) {
  4061. INC_PC(sizeof(generic_arm_inst));
  4062. goto DISPATCH;
  4063. }
  4064. }
  4065. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4066. INC_PC(sizeof(generic_arm_inst));
  4067. FETCH_INST;
  4068. GOTO_NEXT_INST;
  4069. }
  4070. LDREXD_INST:
  4071. {
  4072. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4073. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  4074. unsigned int read_addr = RN;
  4075. add_exclusive_addr(cpu, read_addr);
  4076. cpu->exclusive_state = 1;
  4077. RD = ReadMemory32(cpu, read_addr);
  4078. RD2 = ReadMemory32(cpu, read_addr + 4);
  4079. if (inst_cream->Rd == 15) {
  4080. INC_PC(sizeof(generic_arm_inst));
  4081. goto DISPATCH;
  4082. }
  4083. }
  4084. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4085. INC_PC(sizeof(generic_arm_inst));
  4086. FETCH_INST;
  4087. GOTO_NEXT_INST;
  4088. }
  4089. LDRH_INST:
  4090. {
  4091. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4092. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  4093. inst_cream->get_addr(cpu, inst_cream->inst, addr, 1);
  4094. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = ReadMemory16(cpu, addr);
  4095. if (BITS(inst_cream->inst, 12, 15) == 15) {
  4096. INC_PC(sizeof(ldst_inst));
  4097. goto DISPATCH;
  4098. }
  4099. }
  4100. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4101. INC_PC(sizeof(ldst_inst));
  4102. FETCH_INST;
  4103. GOTO_NEXT_INST;
  4104. }
  4105. LDRSB_INST:
  4106. {
  4107. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4108. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  4109. inst_cream->get_addr(cpu, inst_cream->inst, addr, 1);
  4110. unsigned int value = Memory::Read8(addr);
  4111. if (BIT(value, 7)) {
  4112. value |= 0xffffff00;
  4113. }
  4114. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = value;
  4115. if (BITS(inst_cream->inst, 12, 15) == 15) {
  4116. INC_PC(sizeof(ldst_inst));
  4117. goto DISPATCH;
  4118. }
  4119. }
  4120. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4121. INC_PC(sizeof(ldst_inst));
  4122. FETCH_INST;
  4123. GOTO_NEXT_INST;
  4124. }
  4125. LDRSH_INST:
  4126. {
  4127. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4128. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  4129. inst_cream->get_addr(cpu, inst_cream->inst, addr, 1);
  4130. unsigned int value = ReadMemory16(cpu, addr);
  4131. if (BIT(value, 15)) {
  4132. value |= 0xffff0000;
  4133. }
  4134. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = value;
  4135. if (BITS(inst_cream->inst, 12, 15) == 15) {
  4136. INC_PC(sizeof(ldst_inst));
  4137. goto DISPATCH;
  4138. }
  4139. }
  4140. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4141. INC_PC(sizeof(ldst_inst));
  4142. FETCH_INST;
  4143. GOTO_NEXT_INST;
  4144. }
  4145. LDRT_INST:
  4146. {
  4147. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4148. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  4149. inst_cream->get_addr(cpu, inst_cream->inst, addr, 1);
  4150. unsigned int value = ReadMemory32(cpu, addr);
  4151. cpu->Reg[BITS(inst_cream->inst, 12, 15)] = value;
  4152. if (BITS(inst_cream->inst, 12, 15) == 15) {
  4153. INC_PC(sizeof(ldst_inst));
  4154. goto DISPATCH;
  4155. }
  4156. }
  4157. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4158. INC_PC(sizeof(ldst_inst));
  4159. FETCH_INST;
  4160. GOTO_NEXT_INST;
  4161. }
  4162. MCR_INST:
  4163. {
  4164. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4165. mcr_inst* inst_cream = (mcr_inst*)inst_base->component;
  4166. unsigned int inst = inst_cream->inst;
  4167. if (inst_cream->Rd == 15) {
  4168. DEBUG_MSG;
  4169. } else {
  4170. if (inst_cream->cp_num == 15)
  4171. WriteCP15Register(cpu, RD, CRn, OPCODE_1, CRm, OPCODE_2);
  4172. }
  4173. }
  4174. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4175. INC_PC(sizeof(mcr_inst));
  4176. FETCH_INST;
  4177. GOTO_NEXT_INST;
  4178. }
  4179. MCRR_INST:
  4180. {
  4181. // Stubbed, as the MPCore doesn't have any registers that are accessible
  4182. // through this instruction.
  4183. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4184. mcrr_inst* const inst_cream = (mcrr_inst*)inst_base->component;
  4185. LOG_ERROR(Core_ARM11, "MCRR executed | Coprocessor: %u, CRm %u, opc1: %u, Rt: %u, Rt2: %u",
  4186. inst_cream->cp_num, inst_cream->crm, inst_cream->opcode_1, inst_cream->rt, inst_cream->rt2);
  4187. }
  4188. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4189. INC_PC(sizeof(mcrr_inst));
  4190. FETCH_INST;
  4191. GOTO_NEXT_INST;
  4192. }
  4193. MLA_INST:
  4194. {
  4195. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4196. mla_inst* inst_cream = (mla_inst*)inst_base->component;
  4197. uint64_t rm = RM;
  4198. uint64_t rs = RS;
  4199. uint64_t rn = RN;
  4200. RD = static_cast<uint32_t>((rm * rs + rn) & 0xffffffff);
  4201. if (inst_cream->S) {
  4202. UPDATE_NFLAG(RD);
  4203. UPDATE_ZFLAG(RD);
  4204. }
  4205. if (inst_cream->Rd == 15) {
  4206. INC_PC(sizeof(mla_inst));
  4207. goto DISPATCH;
  4208. }
  4209. }
  4210. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4211. INC_PC(sizeof(mla_inst));
  4212. FETCH_INST;
  4213. GOTO_NEXT_INST;
  4214. }
  4215. MOV_INST:
  4216. {
  4217. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4218. mov_inst* inst_cream = (mov_inst*)inst_base->component;
  4219. RD = SHIFTER_OPERAND;
  4220. if (inst_cream->S && (inst_cream->Rd == 15)) {
  4221. if (CurrentModeHasSPSR) {
  4222. cpu->Cpsr = cpu->Spsr_copy;
  4223. switch_mode(cpu, cpu->Spsr_copy & 0x1f);
  4224. LOAD_NZCVT;
  4225. }
  4226. } else if (inst_cream->S) {
  4227. UPDATE_NFLAG(RD);
  4228. UPDATE_ZFLAG(RD);
  4229. UPDATE_CFLAG_WITH_SC;
  4230. }
  4231. if (inst_cream->Rd == 15) {
  4232. INC_PC(sizeof(mov_inst));
  4233. goto DISPATCH;
  4234. }
  4235. }
  4236. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4237. INC_PC(sizeof(mov_inst));
  4238. FETCH_INST;
  4239. GOTO_NEXT_INST;
  4240. }
  4241. MRC_INST:
  4242. {
  4243. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4244. mrc_inst* inst_cream = (mrc_inst*)inst_base->component;
  4245. unsigned int inst = inst_cream->inst;
  4246. if (inst_cream->Rd == 15) {
  4247. DEBUG_MSG;
  4248. }
  4249. if (inst_cream->inst == 0xeef04a10) {
  4250. // Undefined instruction fmrx
  4251. RD = 0x20000000;
  4252. CITRA_IGNORE_EXIT(-1);
  4253. goto END;
  4254. } else {
  4255. if (inst_cream->cp_num == 15)
  4256. RD = ReadCP15Register(cpu, CRn, OPCODE_1, CRm, OPCODE_2);
  4257. }
  4258. }
  4259. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4260. INC_PC(sizeof(mrc_inst));
  4261. FETCH_INST;
  4262. GOTO_NEXT_INST;
  4263. }
  4264. MRRC_INST:
  4265. {
  4266. // Stubbed, as the MPCore doesn't have any registers that are accessible
  4267. // through this instruction.
  4268. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4269. mcrr_inst* const inst_cream = (mcrr_inst*)inst_base->component;
  4270. LOG_ERROR(Core_ARM11, "MRRC executed | Coprocessor: %u, CRm %u, opc1: %u, Rt: %u, Rt2: %u",
  4271. inst_cream->cp_num, inst_cream->crm, inst_cream->opcode_1, inst_cream->rt, inst_cream->rt2);
  4272. }
  4273. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4274. INC_PC(sizeof(mcrr_inst));
  4275. FETCH_INST;
  4276. GOTO_NEXT_INST;
  4277. }
  4278. MRS_INST:
  4279. {
  4280. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4281. mrs_inst* inst_cream = (mrs_inst*)inst_base->component;
  4282. if (inst_cream->R) {
  4283. RD = cpu->Spsr_copy;
  4284. } else {
  4285. SAVE_NZCVT;
  4286. RD = cpu->Cpsr;
  4287. }
  4288. }
  4289. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4290. INC_PC(sizeof(mrs_inst));
  4291. FETCH_INST;
  4292. GOTO_NEXT_INST;
  4293. }
  4294. MSR_INST:
  4295. {
  4296. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4297. msr_inst* inst_cream = (msr_inst*)inst_base->component;
  4298. const uint32_t UnallocMask = 0x06f0fc00, UserMask = 0xf80f0200, PrivMask = 0x000001df, StateMask = 0x01000020;
  4299. unsigned int inst = inst_cream->inst;
  4300. unsigned int operand;
  4301. if (BIT(inst, 25)) {
  4302. int rot_imm = BITS(inst, 8, 11) * 2;
  4303. operand = ROTATE_RIGHT_32(BITS(inst, 0, 7), rot_imm);
  4304. } else {
  4305. operand = cpu->Reg[BITS(inst, 0, 3)];
  4306. }
  4307. uint32_t byte_mask = (BIT(inst, 16) ? 0xff : 0) | (BIT(inst, 17) ? 0xff00 : 0)
  4308. | (BIT(inst, 18) ? 0xff0000 : 0) | (BIT(inst, 19) ? 0xff000000 : 0);
  4309. uint32_t mask = 0;
  4310. if (!inst_cream->R) {
  4311. if (InAPrivilegedMode(cpu)) {
  4312. if ((operand & StateMask) != 0) {
  4313. /// UNPREDICTABLE
  4314. DEBUG_MSG;
  4315. } else
  4316. mask = byte_mask & (UserMask | PrivMask);
  4317. } else {
  4318. mask = byte_mask & UserMask;
  4319. }
  4320. SAVE_NZCVT;
  4321. cpu->Cpsr = (cpu->Cpsr & ~mask) | (operand & mask);
  4322. switch_mode(cpu, cpu->Cpsr & 0x1f);
  4323. LOAD_NZCVT;
  4324. } else {
  4325. if (CurrentModeHasSPSR) {
  4326. mask = byte_mask & (UserMask | PrivMask | StateMask);
  4327. cpu->Spsr_copy = (cpu->Spsr_copy & ~mask) | (operand & mask);
  4328. }
  4329. }
  4330. }
  4331. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4332. INC_PC(sizeof(msr_inst));
  4333. FETCH_INST;
  4334. GOTO_NEXT_INST;
  4335. }
  4336. MUL_INST:
  4337. {
  4338. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4339. mul_inst* inst_cream = (mul_inst*)inst_base->component;
  4340. uint64_t rm = RM;
  4341. uint64_t rs = RS;
  4342. RD = static_cast<uint32_t>((rm * rs) & 0xffffffff);
  4343. if (inst_cream->S) {
  4344. UPDATE_NFLAG(RD);
  4345. UPDATE_ZFLAG(RD);
  4346. }
  4347. if (inst_cream->Rd == 15) {
  4348. INC_PC(sizeof(mul_inst));
  4349. goto DISPATCH;
  4350. }
  4351. }
  4352. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4353. INC_PC(sizeof(mul_inst));
  4354. FETCH_INST;
  4355. GOTO_NEXT_INST;
  4356. }
  4357. MVN_INST:
  4358. {
  4359. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4360. mvn_inst* const inst_cream = (mvn_inst*)inst_base->component;
  4361. RD = ~SHIFTER_OPERAND;
  4362. if (inst_cream->S && (inst_cream->Rd == 15)) {
  4363. if (CurrentModeHasSPSR) {
  4364. cpu->Cpsr = cpu->Spsr_copy;
  4365. switch_mode(cpu, cpu->Spsr_copy & 0x1f);
  4366. LOAD_NZCVT;
  4367. }
  4368. } else if (inst_cream->S) {
  4369. UPDATE_NFLAG(RD);
  4370. UPDATE_ZFLAG(RD);
  4371. UPDATE_CFLAG_WITH_SC;
  4372. }
  4373. if (inst_cream->Rd == 15) {
  4374. INC_PC(sizeof(mvn_inst));
  4375. goto DISPATCH;
  4376. }
  4377. }
  4378. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4379. INC_PC(sizeof(mvn_inst));
  4380. FETCH_INST;
  4381. GOTO_NEXT_INST;
  4382. }
  4383. ORR_INST:
  4384. {
  4385. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4386. orr_inst* const inst_cream = (orr_inst*)inst_base->component;
  4387. u32 lop = RN;
  4388. u32 rop = SHIFTER_OPERAND;
  4389. RD = lop | rop;
  4390. if (inst_cream->S && (inst_cream->Rd == 15)) {
  4391. if (CurrentModeHasSPSR) {
  4392. cpu->Cpsr = cpu->Spsr_copy;
  4393. switch_mode(cpu, cpu->Spsr_copy & 0x1f);
  4394. LOAD_NZCVT;
  4395. }
  4396. } else if (inst_cream->S) {
  4397. UPDATE_NFLAG(RD);
  4398. UPDATE_ZFLAG(RD);
  4399. UPDATE_CFLAG_WITH_SC;
  4400. }
  4401. if (inst_cream->Rd == 15) {
  4402. INC_PC(sizeof(orr_inst));
  4403. goto DISPATCH;
  4404. }
  4405. }
  4406. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4407. INC_PC(sizeof(orr_inst));
  4408. FETCH_INST;
  4409. GOTO_NEXT_INST;
  4410. }
  4411. PKHBT_INST:
  4412. {
  4413. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4414. pkh_inst *inst_cream = (pkh_inst *)inst_base->component;
  4415. RD = (RN & 0xFFFF) | ((RM << inst_cream->imm) & 0xFFFF0000);
  4416. }
  4417. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4418. INC_PC(sizeof(pkh_inst));
  4419. FETCH_INST;
  4420. GOTO_NEXT_INST;
  4421. }
  4422. PKHTB_INST:
  4423. {
  4424. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4425. pkh_inst *inst_cream = (pkh_inst *)inst_base->component;
  4426. int shift_imm = inst_cream->imm ? inst_cream->imm : 31;
  4427. RD = ((static_cast<s32>(RM) >> shift_imm) & 0xFFFF) | (RN & 0xFFFF0000);
  4428. }
  4429. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4430. INC_PC(sizeof(pkh_inst));
  4431. FETCH_INST;
  4432. GOTO_NEXT_INST;
  4433. }
  4434. PLD_INST:
  4435. {
  4436. // Not implemented. PLD is a hint instruction, so it's optional.
  4437. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4438. INC_PC(sizeof(pld_inst));
  4439. FETCH_INST;
  4440. GOTO_NEXT_INST;
  4441. }
  4442. QADD_INST:
  4443. QDADD_INST:
  4444. QDSUB_INST:
  4445. QSUB_INST:
  4446. {
  4447. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4448. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  4449. const u8 op1 = inst_cream->op1;
  4450. const u32 rm_val = RM;
  4451. const u32 rn_val = RN;
  4452. u32 result = 0;
  4453. // QADD
  4454. if (op1 == 0x00) {
  4455. result = rm_val + rn_val;
  4456. if (AddOverflow(rm_val, rn_val, result)) {
  4457. result = POS(result) ? 0x80000000 : 0x7FFFFFFF;
  4458. cpu->Cpsr |= (1 << 27);
  4459. }
  4460. }
  4461. // QSUB
  4462. else if (op1 == 0x01) {
  4463. result = rm_val - rn_val;
  4464. if (SubOverflow(rm_val, rn_val, result)) {
  4465. result = POS(result) ? 0x80000000 : 0x7FFFFFFF;
  4466. cpu->Cpsr |= (1 << 27);
  4467. }
  4468. }
  4469. // QDADD
  4470. else if (op1 == 0x02) {
  4471. u32 mul = (rn_val * 2);
  4472. if (AddOverflow(rn_val, rn_val, rn_val * 2)) {
  4473. mul = POS(mul) ? 0x80000000 : 0x7FFFFFFF;
  4474. cpu->Cpsr |= (1 << 27);
  4475. }
  4476. result = mul + rm_val;
  4477. if (AddOverflow(rm_val, mul, result)) {
  4478. result = POS(result) ? 0x80000000 : 0x7FFFFFFF;
  4479. cpu->Cpsr |= (1 << 27);
  4480. }
  4481. }
  4482. // QDSUB
  4483. else if (op1 == 0x03) {
  4484. u32 mul = (rn_val * 2);
  4485. if (AddOverflow(rn_val, rn_val, mul)) {
  4486. mul = POS(mul) ? 0x80000000 : 0x7FFFFFFF;
  4487. cpu->Cpsr |= (1 << 27);
  4488. }
  4489. result = rm_val - mul;
  4490. if (SubOverflow(rm_val, mul, result)) {
  4491. result = POS(result) ? 0x80000000 : 0x7FFFFFFF;
  4492. cpu->Cpsr |= (1 << 27);
  4493. }
  4494. }
  4495. RD = result;
  4496. }
  4497. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4498. INC_PC(sizeof(generic_arm_inst));
  4499. FETCH_INST;
  4500. GOTO_NEXT_INST;
  4501. }
  4502. QADD8_INST:
  4503. QADD16_INST:
  4504. QADDSUBX_INST:
  4505. QSUB8_INST:
  4506. QSUB16_INST:
  4507. QSUBADDX_INST:
  4508. {
  4509. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4510. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  4511. const u16 rm_lo = (RM & 0xFFFF);
  4512. const u16 rm_hi = ((RM >> 16) & 0xFFFF);
  4513. const u16 rn_lo = (RN & 0xFFFF);
  4514. const u16 rn_hi = ((RN >> 16) & 0xFFFF);
  4515. const u8 op2 = inst_cream->op2;
  4516. u16 lo_result = 0;
  4517. u16 hi_result = 0;
  4518. // QADD16
  4519. if (op2 == 0x00) {
  4520. lo_result = ARMul_SignedSaturatedAdd16(rn_lo, rm_lo);
  4521. hi_result = ARMul_SignedSaturatedAdd16(rn_hi, rm_hi);
  4522. }
  4523. // QASX
  4524. else if (op2 == 0x01) {
  4525. lo_result = ARMul_SignedSaturatedSub16(rn_lo, rm_hi);
  4526. hi_result = ARMul_SignedSaturatedAdd16(rn_hi, rm_lo);
  4527. }
  4528. // QSAX
  4529. else if (op2 == 0x02) {
  4530. lo_result = ARMul_SignedSaturatedAdd16(rn_lo, rm_hi);
  4531. hi_result = ARMul_SignedSaturatedSub16(rn_hi, rm_lo);
  4532. }
  4533. // QSUB16
  4534. else if (op2 == 0x03) {
  4535. lo_result = ARMul_SignedSaturatedSub16(rn_lo, rm_lo);
  4536. hi_result = ARMul_SignedSaturatedSub16(rn_hi, rm_hi);
  4537. }
  4538. // QADD8
  4539. else if (op2 == 0x04) {
  4540. lo_result = ARMul_SignedSaturatedAdd8(rn_lo & 0xFF, rm_lo & 0xFF) |
  4541. ARMul_SignedSaturatedAdd8(rn_lo >> 8, rm_lo >> 8) << 8;
  4542. hi_result = ARMul_SignedSaturatedAdd8(rn_hi & 0xFF, rm_hi & 0xFF) |
  4543. ARMul_SignedSaturatedAdd8(rn_hi >> 8, rm_hi >> 8) << 8;
  4544. }
  4545. // QSUB8
  4546. else if (op2 == 0x07) {
  4547. lo_result = ARMul_SignedSaturatedSub8(rn_lo & 0xFF, rm_lo & 0xFF) |
  4548. ARMul_SignedSaturatedSub8(rn_lo >> 8, rm_lo >> 8) << 8;
  4549. hi_result = ARMul_SignedSaturatedSub8(rn_hi & 0xFF, rm_hi & 0xFF) |
  4550. ARMul_SignedSaturatedSub8(rn_hi >> 8, rm_hi >> 8) << 8;
  4551. }
  4552. RD = (lo_result & 0xFFFF) | ((hi_result & 0xFFFF) << 16);
  4553. }
  4554. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4555. INC_PC(sizeof(generic_arm_inst));
  4556. FETCH_INST;
  4557. GOTO_NEXT_INST;
  4558. }
  4559. REV_INST:
  4560. REV16_INST:
  4561. REVSH_INST:
  4562. {
  4563. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4564. rev_inst* const inst_cream = (rev_inst*)inst_base->component;
  4565. const u8 op1 = inst_cream->op1;
  4566. const u8 op2 = inst_cream->op2;
  4567. // REV
  4568. if (op1 == 0x03 && op2 == 0x01) {
  4569. RD = ((RM & 0xFF) << 24) | (((RM >> 8) & 0xFF) << 16) | (((RM >> 16) & 0xFF) << 8) | ((RM >> 24) & 0xFF);
  4570. }
  4571. // REV16
  4572. else if (op1 == 0x03 && op2 == 0x05) {
  4573. RD = ((RM & 0xFF) << 8) | ((RM & 0xFF00) >> 8) | ((RM & 0xFF0000) << 8) | ((RM & 0xFF000000) >> 8);
  4574. }
  4575. // REVSH
  4576. else if (op1 == 0x07 && op2 == 0x05) {
  4577. RD = ((RM & 0xFF) << 8) | ((RM & 0xFF00) >> 8);
  4578. if (RD & 0x8000)
  4579. RD |= 0xffff0000;
  4580. }
  4581. }
  4582. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4583. INC_PC(sizeof(rev_inst));
  4584. FETCH_INST;
  4585. GOTO_NEXT_INST;
  4586. }
  4587. RFE_INST:
  4588. {
  4589. // RFE is unconditional
  4590. ldst_inst* const inst_cream = (ldst_inst*)inst_base->component;
  4591. u32 address = 0;
  4592. inst_cream->get_addr(cpu, inst_cream->inst, address, 1);
  4593. cpu->Cpsr = ReadMemory32(cpu, address);
  4594. cpu->Reg[15] = ReadMemory32(cpu, address + 4);
  4595. INC_PC(sizeof(ldst_inst));
  4596. goto DISPATCH;
  4597. }
  4598. RSB_INST:
  4599. {
  4600. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4601. rsb_inst* const inst_cream = (rsb_inst*)inst_base->component;
  4602. u32 rn_val = RN;
  4603. if (inst_cream->Rn == 15)
  4604. rn_val += 2 * GET_INST_SIZE(cpu);
  4605. bool carry;
  4606. bool overflow;
  4607. RD = AddWithCarry(~rn_val, SHIFTER_OPERAND, 1, &carry, &overflow);
  4608. if (inst_cream->S && (inst_cream->Rd == 15)) {
  4609. if (CurrentModeHasSPSR) {
  4610. cpu->Cpsr = cpu->Spsr_copy;
  4611. switch_mode(cpu, cpu->Spsr_copy & 0x1f);
  4612. LOAD_NZCVT;
  4613. }
  4614. } else if (inst_cream->S) {
  4615. UPDATE_NFLAG(RD);
  4616. UPDATE_ZFLAG(RD);
  4617. cpu->CFlag = carry;
  4618. cpu->VFlag = overflow;
  4619. }
  4620. if (inst_cream->Rd == 15) {
  4621. INC_PC(sizeof(rsb_inst));
  4622. goto DISPATCH;
  4623. }
  4624. }
  4625. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4626. INC_PC(sizeof(rsb_inst));
  4627. FETCH_INST;
  4628. GOTO_NEXT_INST;
  4629. }
  4630. RSC_INST:
  4631. {
  4632. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4633. rsc_inst* const inst_cream = (rsc_inst*)inst_base->component;
  4634. bool carry;
  4635. bool overflow;
  4636. RD = AddWithCarry(~RN, SHIFTER_OPERAND, cpu->CFlag, &carry, &overflow);
  4637. if (inst_cream->S && (inst_cream->Rd == 15)) {
  4638. if (CurrentModeHasSPSR) {
  4639. cpu->Cpsr = cpu->Spsr_copy;
  4640. switch_mode(cpu, cpu->Spsr_copy & 0x1f);
  4641. LOAD_NZCVT;
  4642. }
  4643. } else if (inst_cream->S) {
  4644. UPDATE_NFLAG(RD);
  4645. UPDATE_ZFLAG(RD);
  4646. cpu->CFlag = carry;
  4647. cpu->VFlag = overflow;
  4648. }
  4649. if (inst_cream->Rd == 15) {
  4650. INC_PC(sizeof(rsc_inst));
  4651. goto DISPATCH;
  4652. }
  4653. }
  4654. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4655. INC_PC(sizeof(rsc_inst));
  4656. FETCH_INST;
  4657. GOTO_NEXT_INST;
  4658. }
  4659. SADD8_INST:
  4660. SSUB8_INST:
  4661. SADD16_INST:
  4662. SADDSUBX_INST:
  4663. SSUBADDX_INST:
  4664. SSUB16_INST:
  4665. {
  4666. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4667. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  4668. const u8 op2 = inst_cream->op2;
  4669. if (op2 == 0x00 || op2 == 0x01 || op2 == 0x02 || op2 == 0x03) {
  4670. const s16 rn_lo = (RN & 0xFFFF);
  4671. const s16 rn_hi = ((RN >> 16) & 0xFFFF);
  4672. const s16 rm_lo = (RM & 0xFFFF);
  4673. const s16 rm_hi = ((RM >> 16) & 0xFFFF);
  4674. s32 lo_result = 0;
  4675. s32 hi_result = 0;
  4676. // SADD16
  4677. if (inst_cream->op2 == 0x00) {
  4678. lo_result = (rn_lo + rm_lo);
  4679. hi_result = (rn_hi + rm_hi);
  4680. }
  4681. // SASX
  4682. else if (op2 == 0x01) {
  4683. lo_result = (rn_lo - rm_hi);
  4684. hi_result = (rn_hi + rm_lo);
  4685. }
  4686. // SSAX
  4687. else if (op2 == 0x02) {
  4688. lo_result = (rn_lo + rm_hi);
  4689. hi_result = (rn_hi - rm_lo);
  4690. }
  4691. // SSUB16
  4692. else if (op2 == 0x03) {
  4693. lo_result = (rn_lo - rm_lo);
  4694. hi_result = (rn_hi - rm_hi);
  4695. }
  4696. RD = (lo_result & 0xFFFF) | ((hi_result & 0xFFFF) << 16);
  4697. if (lo_result >= 0) {
  4698. cpu->Cpsr |= (1 << 16);
  4699. cpu->Cpsr |= (1 << 17);
  4700. } else {
  4701. cpu->Cpsr &= ~(1 << 16);
  4702. cpu->Cpsr &= ~(1 << 17);
  4703. }
  4704. if (hi_result >= 0) {
  4705. cpu->Cpsr |= (1 << 18);
  4706. cpu->Cpsr |= (1 << 19);
  4707. } else {
  4708. cpu->Cpsr &= ~(1 << 18);
  4709. cpu->Cpsr &= ~(1 << 19);
  4710. }
  4711. }
  4712. else if (op2 == 0x04 || op2 == 0x07) {
  4713. s32 lo_val1, lo_val2;
  4714. s32 hi_val1, hi_val2;
  4715. // SADD8
  4716. if (op2 == 0x04) {
  4717. lo_val1 = (s32)(s8)(RN & 0xFF) + (s32)(s8)(RM & 0xFF);
  4718. lo_val2 = (s32)(s8)((RN >> 8) & 0xFF) + (s32)(s8)((RM >> 8) & 0xFF);
  4719. hi_val1 = (s32)(s8)((RN >> 16) & 0xFF) + (s32)(s8)((RM >> 16) & 0xFF);
  4720. hi_val2 = (s32)(s8)((RN >> 24) & 0xFF) + (s32)(s8)((RM >> 24) & 0xFF);
  4721. }
  4722. // SSUB8
  4723. else {
  4724. lo_val1 = (s32)(s8)(RN & 0xFF) - (s32)(s8)(RM & 0xFF);
  4725. lo_val2 = (s32)(s8)((RN >> 8) & 0xFF) - (s32)(s8)((RM >> 8) & 0xFF);
  4726. hi_val1 = (s32)(s8)((RN >> 16) & 0xFF) - (s32)(s8)((RM >> 16) & 0xFF);
  4727. hi_val2 = (s32)(s8)((RN >> 24) & 0xFF) - (s32)(s8)((RM >> 24) & 0xFF);
  4728. }
  4729. RD = ((lo_val1 & 0xFF) | ((lo_val2 & 0xFF) << 8) | ((hi_val1 & 0xFF) << 16) | ((hi_val2 & 0xFF) << 24));
  4730. if (lo_val1 >= 0)
  4731. cpu->Cpsr |= (1 << 16);
  4732. else
  4733. cpu->Cpsr &= ~(1 << 16);
  4734. if (lo_val2 >= 0)
  4735. cpu->Cpsr |= (1 << 17);
  4736. else
  4737. cpu->Cpsr &= ~(1 << 17);
  4738. if (hi_val1 >= 0)
  4739. cpu->Cpsr |= (1 << 18);
  4740. else
  4741. cpu->Cpsr &= ~(1 << 18);
  4742. if (hi_val2 >= 0)
  4743. cpu->Cpsr |= (1 << 19);
  4744. else
  4745. cpu->Cpsr &= ~(1 << 19);
  4746. }
  4747. }
  4748. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4749. INC_PC(sizeof(generic_arm_inst));
  4750. FETCH_INST;
  4751. GOTO_NEXT_INST;
  4752. }
  4753. SBC_INST:
  4754. {
  4755. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4756. sbc_inst* const inst_cream = (sbc_inst*)inst_base->component;
  4757. bool carry;
  4758. bool overflow;
  4759. RD = AddWithCarry(RN, ~SHIFTER_OPERAND, cpu->CFlag, &carry, &overflow);
  4760. if (inst_cream->S && (inst_cream->Rd == 15)) {
  4761. if (CurrentModeHasSPSR) {
  4762. cpu->Cpsr = cpu->Spsr_copy;
  4763. switch_mode(cpu, cpu->Spsr_copy & 0x1f);
  4764. LOAD_NZCVT;
  4765. }
  4766. } else if (inst_cream->S) {
  4767. UPDATE_NFLAG(RD);
  4768. UPDATE_ZFLAG(RD);
  4769. cpu->CFlag = carry;
  4770. cpu->VFlag = overflow;
  4771. }
  4772. if (inst_cream->Rd == 15) {
  4773. INC_PC(sizeof(sbc_inst));
  4774. goto DISPATCH;
  4775. }
  4776. }
  4777. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4778. INC_PC(sizeof(sbc_inst));
  4779. FETCH_INST;
  4780. GOTO_NEXT_INST;
  4781. }
  4782. SEL_INST:
  4783. {
  4784. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4785. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  4786. const u32 to = RM;
  4787. const u32 from = RN;
  4788. const u32 cpsr = cpu->Cpsr;
  4789. u32 result;
  4790. if (cpsr & (1 << 16))
  4791. result = from & 0xff;
  4792. else
  4793. result = to & 0xff;
  4794. if (cpsr & (1 << 17))
  4795. result |= from & 0x0000ff00;
  4796. else
  4797. result |= to & 0x0000ff00;
  4798. if (cpsr & (1 << 18))
  4799. result |= from & 0x00ff0000;
  4800. else
  4801. result |= to & 0x00ff0000;
  4802. if (cpsr & (1 << 19))
  4803. result |= from & 0xff000000;
  4804. else
  4805. result |= to & 0xff000000;
  4806. RD = result;
  4807. }
  4808. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4809. INC_PC(sizeof(generic_arm_inst));
  4810. FETCH_INST;
  4811. GOTO_NEXT_INST;
  4812. }
  4813. SETEND_INST:
  4814. {
  4815. // SETEND is unconditional
  4816. setend_inst* const inst_cream = (setend_inst*)inst_base->component;
  4817. const bool big_endian = (inst_cream->set_bigend == 1);
  4818. if (big_endian)
  4819. cpu->Cpsr |= (1 << 9);
  4820. else
  4821. cpu->Cpsr &= ~(1 << 9);
  4822. LOG_WARNING(Core_ARM11, "SETEND %s executed", big_endian ? "BE" : "LE");
  4823. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4824. INC_PC(sizeof(setend_inst));
  4825. FETCH_INST;
  4826. GOTO_NEXT_INST;
  4827. }
  4828. SHADD8_INST:
  4829. SHADD16_INST:
  4830. SHADDSUBX_INST:
  4831. SHSUB8_INST:
  4832. SHSUB16_INST:
  4833. SHSUBADDX_INST:
  4834. {
  4835. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4836. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  4837. const u8 op2 = inst_cream->op2;
  4838. const u32 rm_val = RM;
  4839. const u32 rn_val = RN;
  4840. if (op2 == 0x00 || op2 == 0x01 || op2 == 0x02 || op2 == 0x03) {
  4841. s32 lo_result = 0;
  4842. s32 hi_result = 0;
  4843. // SHADD16
  4844. if (op2 == 0x00) {
  4845. lo_result = ((s16)(rn_val & 0xFFFF) + (s16)(rm_val & 0xFFFF)) >> 1;
  4846. hi_result = ((s16)((rn_val >> 16) & 0xFFFF) + (s16)((rm_val >> 16) & 0xFFFF)) >> 1;
  4847. }
  4848. // SHASX
  4849. else if (op2 == 0x01) {
  4850. lo_result = ((s16)(rn_val & 0xFFFF) - (s16)((rm_val >> 16) & 0xFFFF)) >> 1;
  4851. hi_result = ((s16)((rn_val >> 16) & 0xFFFF) + (s16)(rm_val & 0xFFFF)) >> 1;
  4852. }
  4853. // SHSAX
  4854. else if (op2 == 0x02) {
  4855. lo_result = ((s16)(rn_val & 0xFFFF) + (s16)((rm_val >> 16) & 0xFFFF)) >> 1;
  4856. hi_result = ((s16)((rn_val >> 16) & 0xFFFF) - (s16)(rm_val & 0xFFFF)) >> 1;
  4857. }
  4858. // SHSUB16
  4859. else if (op2 == 0x03) {
  4860. lo_result = ((s16)(rn_val & 0xFFFF) - (s16)(rm_val & 0xFFFF)) >> 1;
  4861. hi_result = ((s16)((rn_val >> 16) & 0xFFFF) - (s16)((rm_val >> 16) & 0xFFFF)) >> 1;
  4862. }
  4863. RD = ((lo_result & 0xFFFF) | ((hi_result & 0xFFFF) << 16));
  4864. }
  4865. else if (op2 == 0x04 || op2 == 0x07) {
  4866. s16 lo_val1, lo_val2;
  4867. s16 hi_val1, hi_val2;
  4868. // SHADD8
  4869. if (op2 == 0x04) {
  4870. lo_val1 = ((s8)(rn_val & 0xFF) + (s8)(rm_val & 0xFF)) >> 1;
  4871. lo_val2 = ((s8)((rn_val >> 8) & 0xFF) + (s8)((rm_val >> 8) & 0xFF)) >> 1;
  4872. hi_val1 = ((s8)((rn_val >> 16) & 0xFF) + (s8)((rm_val >> 16) & 0xFF)) >> 1;
  4873. hi_val2 = ((s8)((rn_val >> 24) & 0xFF) + (s8)((rm_val >> 24) & 0xFF)) >> 1;
  4874. }
  4875. // SHSUB8
  4876. else {
  4877. lo_val1 = ((s8)(rn_val & 0xFF) - (s8)(rm_val & 0xFF)) >> 1;
  4878. lo_val2 = ((s8)((rn_val >> 8) & 0xFF) - (s8)((rm_val >> 8) & 0xFF)) >> 1;
  4879. hi_val1 = ((s8)((rn_val >> 16) & 0xFF) - (s8)((rm_val >> 16) & 0xFF)) >> 1;
  4880. hi_val2 = ((s8)((rn_val >> 24) & 0xFF) - (s8)((rm_val >> 24) & 0xFF)) >> 1;
  4881. }
  4882. RD = (lo_val1 & 0xFF) | ((lo_val2 & 0xFF) << 8) | ((hi_val1 & 0xFF) << 16) | ((hi_val2 & 0xFF) << 24);
  4883. }
  4884. }
  4885. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4886. INC_PC(sizeof(generic_arm_inst));
  4887. FETCH_INST;
  4888. GOTO_NEXT_INST;
  4889. }
  4890. SMLA_INST:
  4891. {
  4892. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4893. smla_inst* inst_cream = (smla_inst*)inst_base->component;
  4894. int32_t operand1, operand2;
  4895. if (inst_cream->x == 0)
  4896. operand1 = (BIT(RM, 15)) ? (BITS(RM, 0, 15) | 0xffff0000) : BITS(RM, 0, 15);
  4897. else
  4898. operand1 = (BIT(RM, 31)) ? (BITS(RM, 16, 31) | 0xffff0000) : BITS(RM, 16, 31);
  4899. if (inst_cream->y == 0)
  4900. operand2 = (BIT(RS, 15)) ? (BITS(RS, 0, 15) | 0xffff0000) : BITS(RS, 0, 15);
  4901. else
  4902. operand2 = (BIT(RS, 31)) ? (BITS(RS, 16, 31) | 0xffff0000) : BITS(RS, 16, 31);
  4903. RD = operand1 * operand2 + RN;
  4904. if (AddOverflow(operand1 * operand2, RN, RD))
  4905. cpu->Cpsr |= (1 << 27);
  4906. }
  4907. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4908. INC_PC(sizeof(smla_inst));
  4909. FETCH_INST;
  4910. GOTO_NEXT_INST;
  4911. }
  4912. SMLAD_INST:
  4913. SMLSD_INST:
  4914. SMUAD_INST:
  4915. SMUSD_INST:
  4916. {
  4917. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4918. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  4919. const u8 op2 = inst_cream->op2;
  4920. u32 rm_val = cpu->Reg[inst_cream->Rm];
  4921. const u32 rn_val = cpu->Reg[inst_cream->Rn];
  4922. if (inst_cream->m)
  4923. rm_val = (((rm_val & 0xFFFF) << 16) | (rm_val >> 16));
  4924. const s16 rm_lo = (rm_val & 0xFFFF);
  4925. const s16 rm_hi = ((rm_val >> 16) & 0xFFFF);
  4926. const s16 rn_lo = (rn_val & 0xFFFF);
  4927. const s16 rn_hi = ((rn_val >> 16) & 0xFFFF);
  4928. const u32 product1 = (rn_lo * rm_lo);
  4929. const u32 product2 = (rn_hi * rm_hi);
  4930. // SMUAD and SMLAD
  4931. if (BIT(op2, 1) == 0) {
  4932. RD = (product1 + product2);
  4933. if (inst_cream->Ra != 15) {
  4934. RD += cpu->Reg[inst_cream->Ra];
  4935. if (ARMul_AddOverflowQ(product1 + product2, cpu->Reg[inst_cream->Ra]))
  4936. cpu->Cpsr |= (1 << 27);
  4937. }
  4938. if (ARMul_AddOverflowQ(product1, product2))
  4939. cpu->Cpsr |= (1 << 27);
  4940. }
  4941. // SMUSD and SMLSD
  4942. else {
  4943. RD = (product1 - product2);
  4944. if (inst_cream->Ra != 15) {
  4945. RD += cpu->Reg[inst_cream->Ra];
  4946. if (ARMul_AddOverflowQ(product1 - product2, cpu->Reg[inst_cream->Ra]))
  4947. cpu->Cpsr |= (1 << 27);
  4948. }
  4949. }
  4950. }
  4951. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4952. INC_PC(sizeof(smlad_inst));
  4953. FETCH_INST;
  4954. GOTO_NEXT_INST;
  4955. }
  4956. SMLAL_INST:
  4957. {
  4958. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4959. umlal_inst* inst_cream = (umlal_inst*)inst_base->component;
  4960. long long int rm = RM;
  4961. long long int rs = RS;
  4962. if (BIT(rm, 31)) {
  4963. rm |= 0xffffffff00000000LL;
  4964. }
  4965. if (BIT(rs, 31)) {
  4966. rs |= 0xffffffff00000000LL;
  4967. }
  4968. long long int rst = rm * rs;
  4969. long long int rdhi32 = RDHI;
  4970. long long int hilo = (rdhi32 << 32) + RDLO;
  4971. rst += hilo;
  4972. RDLO = BITS(rst, 0, 31);
  4973. RDHI = BITS(rst, 32, 63);
  4974. if (inst_cream->S) {
  4975. cpu->NFlag = BIT(RDHI, 31);
  4976. cpu->ZFlag = (RDHI == 0 && RDLO == 0);
  4977. }
  4978. }
  4979. cpu->Reg[15] += GET_INST_SIZE(cpu);
  4980. INC_PC(sizeof(umlal_inst));
  4981. FETCH_INST;
  4982. GOTO_NEXT_INST;
  4983. }
  4984. SMLALXY_INST:
  4985. {
  4986. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  4987. smlalxy_inst* const inst_cream = (smlalxy_inst*)inst_base->component;
  4988. u64 operand1 = RN;
  4989. u64 operand2 = RM;
  4990. if (inst_cream->x != 0)
  4991. operand1 >>= 16;
  4992. if (inst_cream->y != 0)
  4993. operand2 >>= 16;
  4994. operand1 &= 0xFFFF;
  4995. if (operand1 & 0x8000)
  4996. operand1 -= 65536;
  4997. operand2 &= 0xFFFF;
  4998. if (operand2 & 0x8000)
  4999. operand2 -= 65536;
  5000. u64 dest = ((u64)RDHI << 32 | RDLO) + (operand1 * operand2);
  5001. RDLO = (dest & 0xFFFFFFFF);
  5002. RDHI = ((dest >> 32) & 0xFFFFFFFF);
  5003. }
  5004. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5005. INC_PC(sizeof(smlalxy_inst));
  5006. FETCH_INST;
  5007. GOTO_NEXT_INST;
  5008. }
  5009. SMLAW_INST:
  5010. {
  5011. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5012. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  5013. const u32 rm_val = RM;
  5014. const u32 rn_val = RN;
  5015. const u32 ra_val = cpu->Reg[inst_cream->Ra];
  5016. const bool high = (inst_cream->m == 1);
  5017. const s16 operand2 = (high) ? ((rm_val >> 16) & 0xFFFF) : (rm_val & 0xFFFF);
  5018. const s64 result = (s64)(s32)rn_val * (s64)(s32)operand2 + ((s64)(s32)ra_val << 16);
  5019. RD = (result & (0xFFFFFFFFFFFFFFFFLL >> 15)) >> 16;
  5020. if ((result >> 16) != (s32)RD)
  5021. cpu->Cpsr |= (1 << 27);
  5022. }
  5023. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5024. INC_PC(sizeof(smlad_inst));
  5025. FETCH_INST;
  5026. GOTO_NEXT_INST;
  5027. }
  5028. SMLALD_INST:
  5029. SMLSLD_INST:
  5030. {
  5031. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5032. smlald_inst* const inst_cream = (smlald_inst*)inst_base->component;
  5033. const bool do_swap = (inst_cream->swap == 1);
  5034. const u32 rdlo_val = RDLO;
  5035. const u32 rdhi_val = RDHI;
  5036. const u32 rn_val = RN;
  5037. u32 rm_val = RM;
  5038. if (do_swap)
  5039. rm_val = (((rm_val & 0xFFFF) << 16) | (rm_val >> 16));
  5040. const s32 product1 = (s16)(rn_val & 0xFFFF) * (s16)(rm_val & 0xFFFF);
  5041. const s32 product2 = (s16)((rn_val >> 16) & 0xFFFF) * (s16)((rm_val >> 16) & 0xFFFF);
  5042. s64 result;
  5043. // SMLALD
  5044. if (BIT(inst_cream->op2, 1) == 0) {
  5045. result = (product1 + product2) + (s64)(rdlo_val | ((s64)rdhi_val << 32));
  5046. }
  5047. // SMLSLD
  5048. else {
  5049. result = (product1 - product2) + (s64)(rdlo_val | ((s64)rdhi_val << 32));
  5050. }
  5051. RDLO = (result & 0xFFFFFFFF);
  5052. RDHI = ((result >> 32) & 0xFFFFFFFF);
  5053. }
  5054. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5055. INC_PC(sizeof(smlald_inst));
  5056. FETCH_INST;
  5057. GOTO_NEXT_INST;
  5058. }
  5059. SMMLA_INST:
  5060. SMMLS_INST:
  5061. SMMUL_INST:
  5062. {
  5063. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5064. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  5065. const u32 rm_val = RM;
  5066. const u32 rn_val = RN;
  5067. const bool do_round = (inst_cream->m == 1);
  5068. // Assume SMMUL by default.
  5069. s64 result = (s64)(s32)rn_val * (s64)(s32)rm_val;
  5070. if (inst_cream->Ra != 15) {
  5071. const u32 ra_val = cpu->Reg[inst_cream->Ra];
  5072. // SMMLA, otherwise SMMLS
  5073. if (BIT(inst_cream->op2, 1) == 0)
  5074. result += ((s64)ra_val << 32);
  5075. else
  5076. result = ((s64)ra_val << 32) - result;
  5077. }
  5078. if (do_round)
  5079. result += 0x80000000;
  5080. RD = ((result >> 32) & 0xFFFFFFFF);
  5081. }
  5082. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5083. INC_PC(sizeof(smlad_inst));
  5084. FETCH_INST;
  5085. GOTO_NEXT_INST;
  5086. }
  5087. SMUL_INST:
  5088. {
  5089. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5090. smul_inst* inst_cream = (smul_inst*)inst_base->component;
  5091. uint32_t operand1, operand2;
  5092. if (inst_cream->x == 0)
  5093. operand1 = (BIT(RM, 15)) ? (BITS(RM, 0, 15) | 0xffff0000) : BITS(RM, 0, 15);
  5094. else
  5095. operand1 = (BIT(RM, 31)) ? (BITS(RM, 16, 31) | 0xffff0000) : BITS(RM, 16, 31);
  5096. if (inst_cream->y == 0)
  5097. operand2 = (BIT(RS, 15)) ? (BITS(RS, 0, 15) | 0xffff0000) : BITS(RS, 0, 15);
  5098. else
  5099. operand2 = (BIT(RS, 31)) ? (BITS(RS, 16, 31) | 0xffff0000) : BITS(RS, 16, 31);
  5100. RD = operand1 * operand2;
  5101. }
  5102. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5103. INC_PC(sizeof(smul_inst));
  5104. FETCH_INST;
  5105. GOTO_NEXT_INST;
  5106. }
  5107. SMULL_INST:
  5108. {
  5109. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5110. umull_inst* inst_cream = (umull_inst*)inst_base->component;
  5111. int64_t rm = RM;
  5112. int64_t rs = RS;
  5113. if (BIT(rm, 31)) {
  5114. rm |= 0xffffffff00000000LL;
  5115. }
  5116. if (BIT(rs, 31)) {
  5117. rs |= 0xffffffff00000000LL;
  5118. }
  5119. int64_t rst = rm * rs;
  5120. RDHI = BITS(rst, 32, 63);
  5121. RDLO = BITS(rst, 0, 31);
  5122. if (inst_cream->S) {
  5123. cpu->NFlag = BIT(RDHI, 31);
  5124. cpu->ZFlag = (RDHI == 0 && RDLO == 0);
  5125. }
  5126. }
  5127. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5128. INC_PC(sizeof(umull_inst));
  5129. FETCH_INST;
  5130. GOTO_NEXT_INST;
  5131. }
  5132. SMULW_INST:
  5133. {
  5134. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5135. smlad_inst* const inst_cream = (smlad_inst*)inst_base->component;
  5136. s16 rm = (inst_cream->m == 1) ? ((RM >> 16) & 0xFFFF) : (RM & 0xFFFF);
  5137. s64 result = (s64)rm * (s64)(s32)RN;
  5138. RD = BITS(result, 16, 47);
  5139. }
  5140. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5141. INC_PC(sizeof(smlad_inst));
  5142. FETCH_INST;
  5143. GOTO_NEXT_INST;
  5144. }
  5145. SRS_INST:
  5146. {
  5147. // SRS is unconditional
  5148. ldst_inst* const inst_cream = (ldst_inst*)inst_base->component;
  5149. u32 address = 0;
  5150. inst_cream->get_addr(cpu, inst_cream->inst, address, 1);
  5151. WriteMemory32(cpu, address + 0, cpu->Reg[14]);
  5152. WriteMemory32(cpu, address + 4, cpu->Spsr_copy);
  5153. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5154. INC_PC(sizeof(ldst_inst));
  5155. FETCH_INST;
  5156. GOTO_NEXT_INST;
  5157. }
  5158. SSAT_INST:
  5159. {
  5160. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5161. ssat_inst* const inst_cream = (ssat_inst*)inst_base->component;
  5162. u8 shift_type = inst_cream->shift_type;
  5163. u8 shift_amount = inst_cream->imm5;
  5164. u32 rn_val = RN;
  5165. // 32-bit ASR is encoded as an amount of 0.
  5166. if (shift_type == 1 && shift_amount == 0)
  5167. shift_amount = 31;
  5168. if (shift_type == 0)
  5169. rn_val <<= shift_amount;
  5170. else if (shift_type == 1)
  5171. rn_val = ((s32)rn_val >> shift_amount);
  5172. bool saturated = false;
  5173. rn_val = ARMul_SignedSatQ(rn_val, inst_cream->sat_imm, &saturated);
  5174. if (saturated)
  5175. cpu->Cpsr |= (1 << 27);
  5176. RD = rn_val;
  5177. }
  5178. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5179. INC_PC(sizeof(ssat_inst));
  5180. FETCH_INST;
  5181. GOTO_NEXT_INST;
  5182. }
  5183. SSAT16_INST:
  5184. {
  5185. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5186. ssat_inst* const inst_cream = (ssat_inst*)inst_base->component;
  5187. const u8 saturate_to = inst_cream->sat_imm;
  5188. bool sat1 = false;
  5189. bool sat2 = false;
  5190. RD = (ARMul_SignedSatQ((s16)RN, saturate_to, &sat1) & 0xFFFF) |
  5191. ARMul_SignedSatQ((s32)RN >> 16, saturate_to, &sat2) << 16;
  5192. if (sat1 || sat2)
  5193. cpu->Cpsr |= (1 << 27);
  5194. }
  5195. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5196. INC_PC(sizeof(ssat_inst));
  5197. FETCH_INST;
  5198. GOTO_NEXT_INST;
  5199. }
  5200. STC_INST:
  5201. {
  5202. // Instruction not implemented
  5203. //LOG_CRITICAL(Core_ARM11, "unimplemented instruction");
  5204. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5205. INC_PC(sizeof(stc_inst));
  5206. FETCH_INST;
  5207. GOTO_NEXT_INST;
  5208. }
  5209. STM_INST:
  5210. {
  5211. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5212. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  5213. unsigned int inst = inst_cream->inst;
  5214. unsigned int Rn = BITS(inst, 16, 19);
  5215. unsigned int old_RN = cpu->Reg[Rn];
  5216. inst_cream->get_addr(cpu, inst_cream->inst, addr, 0);
  5217. if (BIT(inst_cream->inst, 22) == 1) {
  5218. for (int i = 0; i < 13; i++) {
  5219. if (BIT(inst_cream->inst, i)) {
  5220. WriteMemory32(cpu, addr, cpu->Reg[i]);
  5221. addr += 4;
  5222. }
  5223. }
  5224. if (BIT(inst_cream->inst, 13)) {
  5225. if (cpu->Mode == USER32MODE)
  5226. WriteMemory32(cpu, addr, cpu->Reg[13]);
  5227. else
  5228. WriteMemory32(cpu, addr, cpu->Reg_usr[0]);
  5229. addr += 4;
  5230. }
  5231. if (BIT(inst_cream->inst, 14)) {
  5232. if (cpu->Mode == USER32MODE)
  5233. WriteMemory32(cpu, addr, cpu->Reg[14]);
  5234. else
  5235. WriteMemory32(cpu, addr, cpu->Reg_usr[1]);
  5236. addr += 4;
  5237. }
  5238. if (BIT(inst_cream->inst, 15)) {
  5239. WriteMemory32(cpu, addr, cpu->Reg_usr[1] + 8);
  5240. }
  5241. } else {
  5242. for (int i = 0; i < 15; i++) {
  5243. if (BIT(inst_cream->inst, i)) {
  5244. if (i == Rn)
  5245. WriteMemory32(cpu, addr, old_RN);
  5246. else
  5247. WriteMemory32(cpu, addr, cpu->Reg[i]);
  5248. addr += 4;
  5249. }
  5250. }
  5251. // Check PC reg
  5252. if (BIT(inst_cream->inst, 15))
  5253. WriteMemory32(cpu, addr, cpu->Reg_usr[1] + 8);
  5254. }
  5255. }
  5256. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5257. INC_PC(sizeof(ldst_inst));
  5258. FETCH_INST;
  5259. GOTO_NEXT_INST;
  5260. }
  5261. SXTB_INST:
  5262. {
  5263. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5264. sxtb_inst* inst_cream = (sxtb_inst*)inst_base->component;
  5265. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate);
  5266. if (BIT(operand2, 7)) {
  5267. operand2 |= 0xffffff00;
  5268. } else {
  5269. operand2 &= 0xff;
  5270. }
  5271. RD = operand2;
  5272. }
  5273. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5274. INC_PC(sizeof(sxtb_inst));
  5275. FETCH_INST;
  5276. GOTO_NEXT_INST;
  5277. }
  5278. STR_INST:
  5279. {
  5280. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5281. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  5282. inst_cream->get_addr(cpu, inst_cream->inst, addr, 0);
  5283. unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)];
  5284. WriteMemory32(cpu, addr, value);
  5285. }
  5286. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5287. INC_PC(sizeof(ldst_inst));
  5288. FETCH_INST;
  5289. GOTO_NEXT_INST;
  5290. }
  5291. UXTB_INST:
  5292. {
  5293. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5294. uxtb_inst* inst_cream = (uxtb_inst*)inst_base->component;
  5295. RD = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xff;
  5296. }
  5297. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5298. INC_PC(sizeof(uxtb_inst));
  5299. FETCH_INST;
  5300. GOTO_NEXT_INST;
  5301. }
  5302. UXTAB_INST:
  5303. {
  5304. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5305. uxtab_inst* inst_cream = (uxtab_inst*)inst_base->component;
  5306. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xff;
  5307. RD = RN + operand2;
  5308. }
  5309. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5310. INC_PC(sizeof(uxtab_inst));
  5311. FETCH_INST;
  5312. GOTO_NEXT_INST;
  5313. }
  5314. STRB_INST:
  5315. {
  5316. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5317. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  5318. inst_cream->get_addr(cpu, inst_cream->inst, addr, 0);
  5319. unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff;
  5320. Memory::Write8(addr, value);
  5321. }
  5322. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5323. INC_PC(sizeof(ldst_inst));
  5324. FETCH_INST;
  5325. GOTO_NEXT_INST;
  5326. }
  5327. STRBT_INST:
  5328. {
  5329. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5330. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  5331. inst_cream->get_addr(cpu, inst_cream->inst, addr, 0);
  5332. unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff;
  5333. Memory::Write8(addr, value);
  5334. }
  5335. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5336. INC_PC(sizeof(ldst_inst));
  5337. FETCH_INST;
  5338. GOTO_NEXT_INST;
  5339. }
  5340. STRD_INST:
  5341. {
  5342. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5343. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  5344. inst_cream->get_addr(cpu, inst_cream->inst, addr, 0);
  5345. // The 3DS doesn't have the Large Physical Access Extension (LPAE)
  5346. // so STRD wouldn't store these as a single write.
  5347. WriteMemory32(cpu, addr + 0, cpu->Reg[BITS(inst_cream->inst, 12, 15)]);
  5348. WriteMemory32(cpu, addr + 4, cpu->Reg[BITS(inst_cream->inst, 12, 15) + 1]);
  5349. }
  5350. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5351. INC_PC(sizeof(ldst_inst));
  5352. FETCH_INST;
  5353. GOTO_NEXT_INST;
  5354. }
  5355. STREX_INST:
  5356. {
  5357. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5358. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  5359. unsigned int write_addr = cpu->Reg[inst_cream->Rn];
  5360. if ((exclusive_detect(cpu, write_addr) == 0) && (cpu->exclusive_state == 1)) {
  5361. remove_exclusive(cpu, write_addr);
  5362. cpu->exclusive_state = 0;
  5363. WriteMemory32(cpu, write_addr, RM);
  5364. RD = 0;
  5365. } else {
  5366. // Failed to write due to mutex access
  5367. RD = 1;
  5368. }
  5369. }
  5370. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5371. INC_PC(sizeof(generic_arm_inst));
  5372. FETCH_INST;
  5373. GOTO_NEXT_INST;
  5374. }
  5375. STREXB_INST:
  5376. {
  5377. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5378. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  5379. unsigned int write_addr = cpu->Reg[inst_cream->Rn];
  5380. if ((exclusive_detect(cpu, write_addr) == 0) && (cpu->exclusive_state == 1)) {
  5381. remove_exclusive(cpu, write_addr);
  5382. cpu->exclusive_state = 0;
  5383. Memory::Write8(write_addr, cpu->Reg[inst_cream->Rm]);
  5384. RD = 0;
  5385. } else {
  5386. // Failed to write due to mutex access
  5387. RD = 1;
  5388. }
  5389. }
  5390. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5391. INC_PC(sizeof(generic_arm_inst));
  5392. FETCH_INST;
  5393. GOTO_NEXT_INST;
  5394. }
  5395. STREXD_INST:
  5396. {
  5397. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5398. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  5399. unsigned int write_addr = cpu->Reg[inst_cream->Rn];
  5400. if ((exclusive_detect(cpu, write_addr) == 0) && (cpu->exclusive_state == 1)) {
  5401. remove_exclusive(cpu, write_addr);
  5402. cpu->exclusive_state = 0;
  5403. const u32 rt = cpu->Reg[inst_cream->Rm + 0];
  5404. const u32 rt2 = cpu->Reg[inst_cream->Rm + 1];
  5405. u64 value;
  5406. if (InBigEndianMode(cpu))
  5407. value = (((u64)rt << 32) | rt2);
  5408. else
  5409. value = (((u64)rt2 << 32) | rt);
  5410. WriteMemory64(cpu, write_addr, value);
  5411. RD = 0;
  5412. }
  5413. else {
  5414. // Failed to write due to mutex access
  5415. RD = 1;
  5416. }
  5417. }
  5418. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5419. INC_PC(sizeof(generic_arm_inst));
  5420. FETCH_INST;
  5421. GOTO_NEXT_INST;
  5422. }
  5423. STREXH_INST:
  5424. {
  5425. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5426. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  5427. unsigned int write_addr = cpu->Reg[inst_cream->Rn];
  5428. if ((exclusive_detect(cpu, write_addr) == 0) && (cpu->exclusive_state == 1)) {
  5429. remove_exclusive(cpu, write_addr);
  5430. cpu->exclusive_state = 0;
  5431. WriteMemory16(cpu, write_addr, RM);
  5432. RD = 0;
  5433. } else {
  5434. // Failed to write due to mutex access
  5435. RD = 1;
  5436. }
  5437. }
  5438. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5439. INC_PC(sizeof(generic_arm_inst));
  5440. FETCH_INST;
  5441. GOTO_NEXT_INST;
  5442. }
  5443. STRH_INST:
  5444. {
  5445. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5446. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  5447. inst_cream->get_addr(cpu, inst_cream->inst, addr, 0);
  5448. unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xffff;
  5449. WriteMemory16(cpu, addr, value);
  5450. }
  5451. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5452. INC_PC(sizeof(ldst_inst));
  5453. FETCH_INST;
  5454. GOTO_NEXT_INST;
  5455. }
  5456. STRT_INST:
  5457. {
  5458. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5459. ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
  5460. inst_cream->get_addr(cpu, inst_cream->inst, addr, 0);
  5461. unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)];
  5462. WriteMemory32(cpu, addr, value);
  5463. }
  5464. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5465. INC_PC(sizeof(ldst_inst));
  5466. FETCH_INST;
  5467. GOTO_NEXT_INST;
  5468. }
  5469. SUB_INST:
  5470. {
  5471. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5472. sub_inst* const inst_cream = (sub_inst*)inst_base->component;
  5473. u32 rn_val = RN;
  5474. if (inst_cream->Rn == 15)
  5475. rn_val += 8;
  5476. bool carry;
  5477. bool overflow;
  5478. RD = AddWithCarry(rn_val, ~SHIFTER_OPERAND, 1, &carry, &overflow);
  5479. if (inst_cream->S && (inst_cream->Rd == 15)) {
  5480. if (CurrentModeHasSPSR) {
  5481. cpu->Cpsr = cpu->Spsr_copy;
  5482. switch_mode(cpu, cpu->Spsr_copy & 0x1f);
  5483. LOAD_NZCVT;
  5484. }
  5485. } else if (inst_cream->S) {
  5486. UPDATE_NFLAG(RD);
  5487. UPDATE_ZFLAG(RD);
  5488. cpu->CFlag = carry;
  5489. cpu->VFlag = overflow;
  5490. }
  5491. if (inst_cream->Rd == 15) {
  5492. INC_PC(sizeof(sub_inst));
  5493. goto DISPATCH;
  5494. }
  5495. }
  5496. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5497. INC_PC(sizeof(sub_inst));
  5498. FETCH_INST;
  5499. GOTO_NEXT_INST;
  5500. }
  5501. SWI_INST:
  5502. {
  5503. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5504. SVC::CallSVC(Memory::Read32(cpu->Reg[15]));
  5505. }
  5506. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5507. INC_PC(sizeof(swi_inst));
  5508. FETCH_INST;
  5509. GOTO_NEXT_INST;
  5510. }
  5511. SWP_INST:
  5512. {
  5513. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5514. swp_inst* inst_cream = (swp_inst*)inst_base->component;
  5515. addr = RN;
  5516. unsigned int value = ReadMemory32(cpu, addr);
  5517. WriteMemory32(cpu, addr, RM);
  5518. RD = value;
  5519. }
  5520. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5521. INC_PC(sizeof(swp_inst));
  5522. FETCH_INST;
  5523. GOTO_NEXT_INST;
  5524. }
  5525. SWPB_INST:
  5526. {
  5527. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5528. swp_inst* inst_cream = (swp_inst*)inst_base->component;
  5529. addr = RN;
  5530. unsigned int value = Memory::Read8(addr);
  5531. Memory::Write8(addr, (RM & 0xFF));
  5532. RD = value;
  5533. }
  5534. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5535. INC_PC(sizeof(swp_inst));
  5536. FETCH_INST;
  5537. GOTO_NEXT_INST;
  5538. }
  5539. SXTAB_INST:
  5540. {
  5541. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5542. sxtab_inst* inst_cream = (sxtab_inst*)inst_base->component;
  5543. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xff;
  5544. // Sign extend for byte
  5545. operand2 = (0x80 & operand2)? (0xFFFFFF00 | operand2):operand2;
  5546. RD = RN + operand2;
  5547. }
  5548. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5549. INC_PC(sizeof(uxtab_inst));
  5550. FETCH_INST;
  5551. GOTO_NEXT_INST;
  5552. }
  5553. SXTAB16_INST:
  5554. SXTB16_INST:
  5555. {
  5556. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5557. sxtab_inst* const inst_cream = (sxtab_inst*)inst_base->component;
  5558. const u8 rotation = inst_cream->rotate * 8;
  5559. u32 rm_val = RM;
  5560. u32 rn_val = RN;
  5561. if (rotation)
  5562. rm_val = ((rm_val << (32 - rotation)) | (rm_val >> rotation));
  5563. // SXTB16
  5564. if (inst_cream->Rn == 15) {
  5565. u32 lo = (u32)(s8)rm_val;
  5566. u32 hi = (u32)(s8)(rm_val >> 16);
  5567. RD = (lo | (hi << 16));
  5568. }
  5569. // SXTAB16
  5570. else {
  5571. u32 lo = (rn_val & 0xFFFF) + (u32)(s8)(rm_val & 0xFF);
  5572. u32 hi = ((rn_val >> 16) & 0xFFFF) + (u32)(s8)((rm_val >> 16) & 0xFF);
  5573. RD = (lo | (hi << 16));
  5574. }
  5575. }
  5576. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5577. INC_PC(sizeof(sxtab_inst));
  5578. FETCH_INST;
  5579. GOTO_NEXT_INST;
  5580. }
  5581. SXTAH_INST:
  5582. {
  5583. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5584. sxtah_inst* inst_cream = (sxtah_inst*)inst_base->component;
  5585. unsigned int operand2 = ROTATE_RIGHT_32(RM, 8 * inst_cream->rotate) & 0xffff;
  5586. // Sign extend for half
  5587. operand2 = (0x8000 & operand2) ? (0xFFFF0000 | operand2) : operand2;
  5588. RD = RN + operand2;
  5589. }
  5590. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5591. INC_PC(sizeof(sxtah_inst));
  5592. FETCH_INST;
  5593. GOTO_NEXT_INST;
  5594. }
  5595. TEQ_INST:
  5596. {
  5597. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5598. teq_inst* const inst_cream = (teq_inst*)inst_base->component;
  5599. u32 lop = RN;
  5600. u32 rop = SHIFTER_OPERAND;
  5601. if (inst_cream->Rn == 15)
  5602. lop += GET_INST_SIZE(cpu) * 2;
  5603. u32 result = lop ^ rop;
  5604. UPDATE_NFLAG(result);
  5605. UPDATE_ZFLAG(result);
  5606. UPDATE_CFLAG_WITH_SC;
  5607. }
  5608. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5609. INC_PC(sizeof(teq_inst));
  5610. FETCH_INST;
  5611. GOTO_NEXT_INST;
  5612. }
  5613. TST_INST:
  5614. {
  5615. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5616. tst_inst* const inst_cream = (tst_inst*)inst_base->component;
  5617. u32 lop = RN;
  5618. u32 rop = SHIFTER_OPERAND;
  5619. if (inst_cream->Rn == 15)
  5620. lop += GET_INST_SIZE(cpu) * 2;
  5621. u32 result = lop & rop;
  5622. UPDATE_NFLAG(result);
  5623. UPDATE_ZFLAG(result);
  5624. UPDATE_CFLAG_WITH_SC;
  5625. }
  5626. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5627. INC_PC(sizeof(tst_inst));
  5628. FETCH_INST;
  5629. GOTO_NEXT_INST;
  5630. }
  5631. UADD8_INST:
  5632. UADD16_INST:
  5633. UADDSUBX_INST:
  5634. USUB8_INST:
  5635. USUB16_INST:
  5636. USUBADDX_INST:
  5637. {
  5638. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5639. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  5640. const u8 op2 = inst_cream->op2;
  5641. const u32 rm_val = RM;
  5642. const u32 rn_val = RN;
  5643. s32 lo_result = 0;
  5644. s32 hi_result = 0;
  5645. // UADD16
  5646. if (op2 == 0x00) {
  5647. lo_result = (rn_val & 0xFFFF) + (rm_val & 0xFFFF);
  5648. hi_result = ((rn_val >> 16) & 0xFFFF) + ((rm_val >> 16) & 0xFFFF);
  5649. if (lo_result & 0xFFFF0000) {
  5650. cpu->Cpsr |= (1 << 16);
  5651. cpu->Cpsr |= (1 << 17);
  5652. } else {
  5653. cpu->Cpsr &= ~(1 << 16);
  5654. cpu->Cpsr &= ~(1 << 17);
  5655. }
  5656. if (hi_result & 0xFFFF0000) {
  5657. cpu->Cpsr |= (1 << 18);
  5658. cpu->Cpsr |= (1 << 19);
  5659. } else {
  5660. cpu->Cpsr &= ~(1 << 18);
  5661. cpu->Cpsr &= ~(1 << 19);
  5662. }
  5663. }
  5664. // UASX
  5665. else if (op2 == 0x01) {
  5666. lo_result = (rn_val & 0xFFFF) - ((rm_val >> 16) & 0xFFFF);
  5667. hi_result = ((rn_val >> 16) & 0xFFFF) + (rm_val & 0xFFFF);
  5668. if (lo_result >= 0) {
  5669. cpu->Cpsr |= (1 << 16);
  5670. cpu->Cpsr |= (1 << 17);
  5671. } else {
  5672. cpu->Cpsr &= ~(1 << 16);
  5673. cpu->Cpsr &= ~(1 << 17);
  5674. }
  5675. if (hi_result >= 0x10000) {
  5676. cpu->Cpsr |= (1 << 18);
  5677. cpu->Cpsr |= (1 << 19);
  5678. } else {
  5679. cpu->Cpsr &= ~(1 << 18);
  5680. cpu->Cpsr &= ~(1 << 19);
  5681. }
  5682. }
  5683. // USAX
  5684. else if (op2 == 0x02) {
  5685. lo_result = (rn_val & 0xFFFF) + ((rm_val >> 16) & 0xFFFF);
  5686. hi_result = ((rn_val >> 16) & 0xFFFF) - (rm_val & 0xFFFF);
  5687. if (lo_result >= 0x10000) {
  5688. cpu->Cpsr |= (1 << 16);
  5689. cpu->Cpsr |= (1 << 17);
  5690. } else {
  5691. cpu->Cpsr &= ~(1 << 16);
  5692. cpu->Cpsr &= ~(1 << 17);
  5693. }
  5694. if (hi_result >= 0) {
  5695. cpu->Cpsr |= (1 << 18);
  5696. cpu->Cpsr |= (1 << 19);
  5697. } else {
  5698. cpu->Cpsr &= ~(1 << 18);
  5699. cpu->Cpsr &= ~(1 << 19);
  5700. }
  5701. }
  5702. // USUB16
  5703. else if (op2 == 0x03) {
  5704. lo_result = (rn_val & 0xFFFF) - (rm_val & 0xFFFF);
  5705. hi_result = ((rn_val >> 16) & 0xFFFF) - ((rm_val >> 16) & 0xFFFF);
  5706. if ((lo_result & 0xFFFF0000) == 0) {
  5707. cpu->Cpsr |= (1 << 16);
  5708. cpu->Cpsr |= (1 << 17);
  5709. } else {
  5710. cpu->Cpsr &= ~(1 << 16);
  5711. cpu->Cpsr &= ~(1 << 17);
  5712. }
  5713. if ((hi_result & 0xFFFF0000) == 0) {
  5714. cpu->Cpsr |= (1 << 18);
  5715. cpu->Cpsr |= (1 << 19);
  5716. } else {
  5717. cpu->Cpsr &= ~(1 << 18);
  5718. cpu->Cpsr &= ~(1 << 19);
  5719. }
  5720. }
  5721. // UADD8
  5722. else if (op2 == 0x04) {
  5723. s16 sum1 = (rn_val & 0xFF) + (rm_val & 0xFF);
  5724. s16 sum2 = ((rn_val >> 8) & 0xFF) + ((rm_val >> 8) & 0xFF);
  5725. s16 sum3 = ((rn_val >> 16) & 0xFF) + ((rm_val >> 16) & 0xFF);
  5726. s16 sum4 = ((rn_val >> 24) & 0xFF) + ((rm_val >> 24) & 0xFF);
  5727. if (sum1 >= 0x100)
  5728. cpu->Cpsr |= (1 << 16);
  5729. else
  5730. cpu->Cpsr &= ~(1 << 16);
  5731. if (sum2 >= 0x100)
  5732. cpu->Cpsr |= (1 << 17);
  5733. else
  5734. cpu->Cpsr &= ~(1 << 17);
  5735. if (sum3 >= 0x100)
  5736. cpu->Cpsr |= (1 << 18);
  5737. else
  5738. cpu->Cpsr &= ~(1 << 18);
  5739. if (sum4 >= 0x100)
  5740. cpu->Cpsr |= (1 << 19);
  5741. else
  5742. cpu->Cpsr &= ~(1 << 19);
  5743. lo_result = ((sum1 & 0xFF) | (sum2 & 0xFF) << 8);
  5744. hi_result = ((sum3 & 0xFF) | (sum4 & 0xFF) << 8);
  5745. }
  5746. // USUB8
  5747. else if (op2 == 0x07) {
  5748. s16 diff1 = (rn_val & 0xFF) - (rm_val & 0xFF);
  5749. s16 diff2 = ((rn_val >> 8) & 0xFF) - ((rm_val >> 8) & 0xFF);
  5750. s16 diff3 = ((rn_val >> 16) & 0xFF) - ((rm_val >> 16) & 0xFF);
  5751. s16 diff4 = ((rn_val >> 24) & 0xFF) - ((rm_val >> 24) & 0xFF);
  5752. if (diff1 >= 0)
  5753. cpu->Cpsr |= (1 << 16);
  5754. else
  5755. cpu->Cpsr &= ~(1 << 16);
  5756. if (diff2 >= 0)
  5757. cpu->Cpsr |= (1 << 17);
  5758. else
  5759. cpu->Cpsr &= ~(1 << 17);
  5760. if (diff3 >= 0)
  5761. cpu->Cpsr |= (1 << 18);
  5762. else
  5763. cpu->Cpsr &= ~(1 << 18);
  5764. if (diff4 >= 0)
  5765. cpu->Cpsr |= (1 << 19);
  5766. else
  5767. cpu->Cpsr &= ~(1 << 19);
  5768. lo_result = (diff1 & 0xFF) | ((diff2 & 0xFF) << 8);
  5769. hi_result = (diff3 & 0xFF) | ((diff4 & 0xFF) << 8);
  5770. }
  5771. RD = (lo_result & 0xFFFF) | ((hi_result & 0xFFFF) << 16);
  5772. }
  5773. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5774. INC_PC(sizeof(generic_arm_inst));
  5775. FETCH_INST;
  5776. GOTO_NEXT_INST;
  5777. }
  5778. UHADD8_INST:
  5779. UHADD16_INST:
  5780. UHADDSUBX_INST:
  5781. UHSUBADDX_INST:
  5782. UHSUB8_INST:
  5783. UHSUB16_INST:
  5784. {
  5785. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5786. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  5787. const u32 rm_val = RM;
  5788. const u32 rn_val = RN;
  5789. const u8 op2 = inst_cream->op2;
  5790. if (op2 == 0x00 || op2 == 0x01 || op2 == 0x02 || op2 == 0x03)
  5791. {
  5792. u32 lo_val = 0;
  5793. u32 hi_val = 0;
  5794. // UHADD16
  5795. if (op2 == 0x00) {
  5796. lo_val = (rn_val & 0xFFFF) + (rm_val & 0xFFFF);
  5797. hi_val = ((rn_val >> 16) & 0xFFFF) + ((rm_val >> 16) & 0xFFFF);
  5798. }
  5799. // UHASX
  5800. else if (op2 == 0x01) {
  5801. lo_val = (rn_val & 0xFFFF) - ((rm_val >> 16) & 0xFFFF);
  5802. hi_val = ((rn_val >> 16) & 0xFFFF) + (rm_val & 0xFFFF);
  5803. }
  5804. // UHSAX
  5805. else if (op2 == 0x02) {
  5806. lo_val = (rn_val & 0xFFFF) + ((rm_val >> 16) & 0xFFFF);
  5807. hi_val = ((rn_val >> 16) & 0xFFFF) - (rm_val & 0xFFFF);
  5808. }
  5809. // UHSUB16
  5810. else if (op2 == 0x03) {
  5811. lo_val = (rn_val & 0xFFFF) - (rm_val & 0xFFFF);
  5812. hi_val = ((rn_val >> 16) & 0xFFFF) - ((rm_val >> 16) & 0xFFFF);
  5813. }
  5814. lo_val >>= 1;
  5815. hi_val >>= 1;
  5816. RD = (lo_val & 0xFFFF) | ((hi_val & 0xFFFF) << 16);
  5817. }
  5818. else if (op2 == 0x04 || op2 == 0x07) {
  5819. u32 sum1;
  5820. u32 sum2;
  5821. u32 sum3;
  5822. u32 sum4;
  5823. // UHADD8
  5824. if (op2 == 0x04) {
  5825. sum1 = (rn_val & 0xFF) + (rm_val & 0xFF);
  5826. sum2 = ((rn_val >> 8) & 0xFF) + ((rm_val >> 8) & 0xFF);
  5827. sum3 = ((rn_val >> 16) & 0xFF) + ((rm_val >> 16) & 0xFF);
  5828. sum4 = ((rn_val >> 24) & 0xFF) + ((rm_val >> 24) & 0xFF);
  5829. }
  5830. // UHSUB8
  5831. else {
  5832. sum1 = (rn_val & 0xFF) - (rm_val & 0xFF);
  5833. sum2 = ((rn_val >> 8) & 0xFF) - ((rm_val >> 8) & 0xFF);
  5834. sum3 = ((rn_val >> 16) & 0xFF) - ((rm_val >> 16) & 0xFF);
  5835. sum4 = ((rn_val >> 24) & 0xFF) - ((rm_val >> 24) & 0xFF);
  5836. }
  5837. sum1 >>= 1;
  5838. sum2 >>= 1;
  5839. sum3 >>= 1;
  5840. sum4 >>= 1;
  5841. RD = (sum1 & 0xFF) | ((sum2 & 0xFF) << 8) | ((sum3 & 0xFF) << 16) | ((sum4 & 0xFF) << 24);
  5842. }
  5843. }
  5844. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5845. INC_PC(sizeof(generic_arm_inst));
  5846. FETCH_INST;
  5847. GOTO_NEXT_INST;
  5848. }
  5849. UMAAL_INST:
  5850. {
  5851. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5852. umaal_inst* const inst_cream = (umaal_inst*)inst_base->component;
  5853. const u64 rm = RM;
  5854. const u64 rn = RN;
  5855. const u64 rd_lo = RDLO;
  5856. const u64 rd_hi = RDHI;
  5857. const u64 result = (rm * rn) + rd_lo + rd_hi;
  5858. RDLO = (result & 0xFFFFFFFF);
  5859. RDHI = ((result >> 32) & 0xFFFFFFFF);
  5860. }
  5861. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5862. INC_PC(sizeof(umaal_inst));
  5863. FETCH_INST;
  5864. GOTO_NEXT_INST;
  5865. }
  5866. UMLAL_INST:
  5867. {
  5868. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5869. umlal_inst* inst_cream = (umlal_inst*)inst_base->component;
  5870. unsigned long long int rm = RM;
  5871. unsigned long long int rs = RS;
  5872. unsigned long long int rst = rm * rs;
  5873. unsigned long long int add = ((unsigned long long) RDHI)<<32;
  5874. add += RDLO;
  5875. rst += add;
  5876. RDLO = BITS(rst, 0, 31);
  5877. RDHI = BITS(rst, 32, 63);
  5878. if (inst_cream->S) {
  5879. cpu->NFlag = BIT(RDHI, 31);
  5880. cpu->ZFlag = (RDHI == 0 && RDLO == 0);
  5881. }
  5882. }
  5883. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5884. INC_PC(sizeof(umlal_inst));
  5885. FETCH_INST;
  5886. GOTO_NEXT_INST;
  5887. }
  5888. UMULL_INST:
  5889. {
  5890. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5891. umull_inst* inst_cream = (umull_inst*)inst_base->component;
  5892. unsigned long long int rm = RM;
  5893. unsigned long long int rs = RS;
  5894. unsigned long long int rst = rm * rs;
  5895. RDHI = BITS(rst, 32, 63);
  5896. RDLO = BITS(rst, 0, 31);
  5897. if (inst_cream->S) {
  5898. cpu->NFlag = BIT(RDHI, 31);
  5899. cpu->ZFlag = (RDHI == 0 && RDLO == 0);
  5900. }
  5901. }
  5902. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5903. INC_PC(sizeof(umull_inst));
  5904. FETCH_INST;
  5905. GOTO_NEXT_INST;
  5906. }
  5907. B_2_THUMB:
  5908. {
  5909. b_2_thumb* inst_cream = (b_2_thumb*)inst_base->component;
  5910. cpu->Reg[15] = cpu->Reg[15] + 4 + inst_cream->imm;
  5911. INC_PC(sizeof(b_2_thumb));
  5912. goto DISPATCH;
  5913. }
  5914. B_COND_THUMB:
  5915. {
  5916. b_cond_thumb* inst_cream = (b_cond_thumb*)inst_base->component;
  5917. if(CondPassed(cpu, inst_cream->cond))
  5918. cpu->Reg[15] = cpu->Reg[15] + 4 + inst_cream->imm;
  5919. else
  5920. cpu->Reg[15] += 2;
  5921. INC_PC(sizeof(b_cond_thumb));
  5922. goto DISPATCH;
  5923. }
  5924. BL_1_THUMB:
  5925. {
  5926. bl_1_thumb* inst_cream = (bl_1_thumb*)inst_base->component;
  5927. cpu->Reg[14] = cpu->Reg[15] + 4 + inst_cream->imm;
  5928. cpu->Reg[15] += GET_INST_SIZE(cpu);
  5929. INC_PC(sizeof(bl_1_thumb));
  5930. FETCH_INST;
  5931. GOTO_NEXT_INST;
  5932. }
  5933. BL_2_THUMB:
  5934. {
  5935. bl_2_thumb* inst_cream = (bl_2_thumb*)inst_base->component;
  5936. int tmp = ((cpu->Reg[15] + 2) | 1);
  5937. cpu->Reg[15] = (cpu->Reg[14] + inst_cream->imm);
  5938. cpu->Reg[14] = tmp;
  5939. INC_PC(sizeof(bl_2_thumb));
  5940. goto DISPATCH;
  5941. }
  5942. BLX_1_THUMB:
  5943. {
  5944. // BLX 1 for armv5t and above
  5945. u32 tmp = cpu->Reg[15];
  5946. blx_1_thumb* inst_cream = (blx_1_thumb*)inst_base->component;
  5947. cpu->Reg[15] = (cpu->Reg[14] + inst_cream->imm) & 0xFFFFFFFC;
  5948. cpu->Reg[14] = ((tmp + 2) | 1);
  5949. cpu->TFlag = 0;
  5950. INC_PC(sizeof(blx_1_thumb));
  5951. goto DISPATCH;
  5952. }
  5953. UQADD8_INST:
  5954. UQADD16_INST:
  5955. UQADDSUBX_INST:
  5956. UQSUB8_INST:
  5957. UQSUB16_INST:
  5958. UQSUBADDX_INST:
  5959. {
  5960. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  5961. generic_arm_inst* const inst_cream = (generic_arm_inst*)inst_base->component;
  5962. const u8 op2 = inst_cream->op2;
  5963. const u32 rm_val = RM;
  5964. const u32 rn_val = RN;
  5965. u16 lo_val = 0;
  5966. u16 hi_val = 0;
  5967. // UQADD16
  5968. if (op2 == 0x00) {
  5969. lo_val = ARMul_UnsignedSaturatedAdd16(rn_val & 0xFFFF, rm_val & 0xFFFF);
  5970. hi_val = ARMul_UnsignedSaturatedAdd16((rn_val >> 16) & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  5971. }
  5972. // UQASX
  5973. else if (op2 == 0x01) {
  5974. lo_val = ARMul_UnsignedSaturatedSub16(rn_val & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  5975. hi_val = ARMul_UnsignedSaturatedAdd16((rn_val >> 16) & 0xFFFF, rm_val & 0xFFFF);
  5976. }
  5977. // UQSAX
  5978. else if (op2 == 0x02) {
  5979. lo_val = ARMul_UnsignedSaturatedAdd16(rn_val & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  5980. hi_val = ARMul_UnsignedSaturatedSub16((rn_val >> 16) & 0xFFFF, rm_val & 0xFFFF);
  5981. }
  5982. // UQSUB16
  5983. else if (op2 == 0x03) {
  5984. lo_val = ARMul_UnsignedSaturatedSub16(rn_val & 0xFFFF, rm_val & 0xFFFF);
  5985. hi_val = ARMul_UnsignedSaturatedSub16((rn_val >> 16) & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  5986. }
  5987. // UQADD8
  5988. else if (op2 == 0x04) {
  5989. lo_val = ARMul_UnsignedSaturatedAdd8(rn_val, rm_val) |
  5990. ARMul_UnsignedSaturatedAdd8(rn_val >> 8, rm_val >> 8) << 8;
  5991. hi_val = ARMul_UnsignedSaturatedAdd8(rn_val >> 16, rm_val >> 16) |
  5992. ARMul_UnsignedSaturatedAdd8(rn_val >> 24, rm_val >> 24) << 8;
  5993. }
  5994. // UQSUB8
  5995. else {
  5996. lo_val = ARMul_UnsignedSaturatedSub8(rn_val, rm_val) |
  5997. ARMul_UnsignedSaturatedSub8(rn_val >> 8, rm_val >> 8) << 8;
  5998. hi_val = ARMul_UnsignedSaturatedSub8(rn_val >> 16, rm_val >> 16) |
  5999. ARMul_UnsignedSaturatedSub8(rn_val >> 24, rm_val >> 24) << 8;
  6000. }
  6001. RD = ((lo_val & 0xFFFF) | hi_val << 16);
  6002. }
  6003. cpu->Reg[15] += GET_INST_SIZE(cpu);
  6004. INC_PC(sizeof(generic_arm_inst));
  6005. FETCH_INST;
  6006. GOTO_NEXT_INST;
  6007. }
  6008. USAD8_INST:
  6009. USADA8_INST:
  6010. {
  6011. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  6012. generic_arm_inst* inst_cream = (generic_arm_inst*)inst_base->component;
  6013. const u8 ra_idx = inst_cream->Ra;
  6014. const u32 rm_val = RM;
  6015. const u32 rn_val = RN;
  6016. const u8 diff1 = ARMul_UnsignedAbsoluteDifference(rn_val & 0xFF, rm_val & 0xFF);
  6017. const u8 diff2 = ARMul_UnsignedAbsoluteDifference((rn_val >> 8) & 0xFF, (rm_val >> 8) & 0xFF);
  6018. const u8 diff3 = ARMul_UnsignedAbsoluteDifference((rn_val >> 16) & 0xFF, (rm_val >> 16) & 0xFF);
  6019. const u8 diff4 = ARMul_UnsignedAbsoluteDifference((rn_val >> 24) & 0xFF, (rm_val >> 24) & 0xFF);
  6020. u32 finalDif = (diff1 + diff2 + diff3 + diff4);
  6021. // Op is USADA8 if true.
  6022. if (ra_idx != 15)
  6023. finalDif += cpu->Reg[ra_idx];
  6024. RD = finalDif;
  6025. }
  6026. cpu->Reg[15] += GET_INST_SIZE(cpu);
  6027. INC_PC(sizeof(generic_arm_inst));
  6028. FETCH_INST;
  6029. GOTO_NEXT_INST;
  6030. }
  6031. USAT_INST:
  6032. {
  6033. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  6034. ssat_inst* const inst_cream = (ssat_inst*)inst_base->component;
  6035. u8 shift_type = inst_cream->shift_type;
  6036. u8 shift_amount = inst_cream->imm5;
  6037. u32 rn_val = RN;
  6038. // 32-bit ASR is encoded as an amount of 0.
  6039. if (shift_type == 1 && shift_amount == 0)
  6040. shift_amount = 31;
  6041. if (shift_type == 0)
  6042. rn_val <<= shift_amount;
  6043. else if (shift_type == 1)
  6044. rn_val = ((s32)rn_val >> shift_amount);
  6045. bool saturated = false;
  6046. rn_val = ARMul_UnsignedSatQ(rn_val, inst_cream->sat_imm, &saturated);
  6047. if (saturated)
  6048. cpu->Cpsr |= (1 << 27);
  6049. RD = rn_val;
  6050. }
  6051. cpu->Reg[15] += GET_INST_SIZE(cpu);
  6052. INC_PC(sizeof(ssat_inst));
  6053. FETCH_INST;
  6054. GOTO_NEXT_INST;
  6055. }
  6056. USAT16_INST:
  6057. {
  6058. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  6059. ssat_inst* const inst_cream = (ssat_inst*)inst_base->component;
  6060. const u8 saturate_to = inst_cream->sat_imm;
  6061. bool sat1 = false;
  6062. bool sat2 = false;
  6063. RD = (ARMul_UnsignedSatQ((s16)RN, saturate_to, &sat1) & 0xFFFF) |
  6064. ARMul_UnsignedSatQ((s32)RN >> 16, saturate_to, &sat2) << 16;
  6065. if (sat1 || sat2)
  6066. cpu->Cpsr |= (1 << 27);
  6067. }
  6068. cpu->Reg[15] += GET_INST_SIZE(cpu);
  6069. INC_PC(sizeof(ssat_inst));
  6070. FETCH_INST;
  6071. GOTO_NEXT_INST;
  6072. }
  6073. UXTAB16_INST:
  6074. UXTB16_INST:
  6075. {
  6076. if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
  6077. uxtab_inst* const inst_cream = (uxtab_inst*)inst_base->component;
  6078. const u8 rn_idx = inst_cream->Rn;
  6079. const u32 rm_val = RM;
  6080. const u32 rotation = inst_cream->rotate * 8;
  6081. const u32 rotated_rm = ((rm_val << (32 - rotation)) | (rm_val >> rotation));
  6082. // UXTB16, otherwise UXTAB16
  6083. if (rn_idx == 15) {
  6084. RD = rotated_rm & 0x00FF00FF;
  6085. } else {
  6086. const u32 rn_val = RN;
  6087. const u8 lo_rotated = (rotated_rm & 0xFF);
  6088. const u16 lo_result = (rn_val & 0xFFFF) + (u16)lo_rotated;
  6089. const u8 hi_rotated = (rotated_rm >> 16) & 0xFF;
  6090. const u16 hi_result = (rn_val >> 16) + (u16)hi_rotated;
  6091. RD = ((hi_result << 16) | (lo_result & 0xFFFF));
  6092. }
  6093. }
  6094. cpu->Reg[15] += GET_INST_SIZE(cpu);
  6095. INC_PC(sizeof(uxtab_inst));
  6096. FETCH_INST;
  6097. GOTO_NEXT_INST;
  6098. }
  6099. #define VFP_INTERPRETER_IMPL
  6100. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  6101. #undef VFP_INTERPRETER_IMPL
  6102. END:
  6103. {
  6104. SAVE_NZCVT;
  6105. cpu->NumInstrsToExecute = 0;
  6106. return num_instrs;
  6107. }
  6108. INIT_INST_LENGTH:
  6109. {
  6110. cpu->NumInstrsToExecute = 0;
  6111. return num_instrs;
  6112. }
  6113. }