arm_dyncom_interpreter.cpp 218 KB

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