arm_dyncom_interpreter.cpp 219 KB

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