arm_dyncom_interpreter.cpp 218 KB

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