armemu.cpp 239 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555
  1. /* armemu.c -- Main instruction emulation: ARM7 Instruction Emulator.
  2. Copyright (C) 1994 Advanced RISC Machines Ltd.
  3. Modifications to add arch. v4 support by <jsmith@cygnus.com>.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. //#include <util.h> // DEBUG()
  16. #include "core/arm/skyeye_common/arm_regformat.h"
  17. #include "core/arm/skyeye_common/armdefs.h"
  18. #include "core/arm/skyeye_common/armemu.h"
  19. #include "core/hle/hle.h"
  20. //#include "svc.h"
  21. //ichfly
  22. //#define callstacker 1
  23. //#include "skyeye_callback.h"
  24. //#include "skyeye_bus.h"
  25. //#include "sim_control.h"
  26. //#include "skyeye_pref.h"
  27. //#include "skyeye.h"
  28. //#include "skyeye2gdb.h"
  29. //#include "code_cov.h"
  30. //#include "iwmmxt.h"
  31. //chy 2003-07-11: for debug instrs
  32. //extern int skyeye_instr_debug;
  33. extern FILE *skyeye_logfd;
  34. static ARMword GetDPRegRHS (ARMul_State *, ARMword);
  35. static ARMword GetDPSRegRHS (ARMul_State *, ARMword);
  36. static void WriteR15 (ARMul_State *, ARMword);
  37. static void WriteSR15 (ARMul_State *, ARMword);
  38. static void WriteR15Branch (ARMul_State *, ARMword);
  39. static ARMword GetLSRegRHS (ARMul_State *, ARMword);
  40. static ARMword GetLS7RHS (ARMul_State *, ARMword);
  41. static unsigned LoadWord (ARMul_State *, ARMword, ARMword);
  42. static unsigned LoadHalfWord (ARMul_State *, ARMword, ARMword, int);
  43. static unsigned LoadByte (ARMul_State *, ARMword, ARMword, int);
  44. static unsigned StoreWord (ARMul_State *, ARMword, ARMword);
  45. static unsigned StoreHalfWord (ARMul_State *, ARMword, ARMword);
  46. static unsigned StoreByte (ARMul_State *, ARMword, ARMword);
  47. static void LoadMult (ARMul_State *, ARMword, ARMword, ARMword);
  48. static void StoreMult (ARMul_State *, ARMword, ARMword, ARMword);
  49. static void LoadSMult (ARMul_State *, ARMword, ARMword, ARMword);
  50. static void StoreSMult (ARMul_State *, ARMword, ARMword, ARMword);
  51. static unsigned Multiply64 (ARMul_State *, ARMword, int, int);
  52. static unsigned MultiplyAdd64 (ARMul_State *, ARMword, int, int);
  53. static void Handle_Load_Double (ARMul_State *, ARMword);
  54. static void Handle_Store_Double (ARMul_State *, ARMword);
  55. static int
  56. handle_v6_insn (ARMul_State * state, ARMword instr);
  57. #define LUNSIGNED (0) /* unsigned operation */
  58. #define LSIGNED (1) /* signed operation */
  59. #define LDEFAULT (0) /* default : do nothing */
  60. #define LSCC (1) /* set condition codes on result */
  61. #ifdef NEED_UI_LOOP_HOOK
  62. /* How often to run the ui_loop update, when in use. */
  63. #define UI_LOOP_POLL_INTERVAL 0x32000
  64. /* Counter for the ui_loop_hook update. */
  65. static int ui_loop_hook_counter = UI_LOOP_POLL_INTERVAL;
  66. /* Actual hook to call to run through gdb's gui event loop. */
  67. extern int (*ui_loop_hook) (int);
  68. #endif /* NEED_UI_LOOP_HOOK */
  69. /* Short-hand macros for LDR/STR. */
  70. /* Store post decrement writeback. */
  71. #define SHDOWNWB() \
  72. lhs = LHS ; \
  73. if (StoreHalfWord (state, instr, lhs)) \
  74. LSBase = lhs - GetLS7RHS (state, instr);
  75. /* Store post increment writeback. */
  76. #define SHUPWB() \
  77. lhs = LHS ; \
  78. if (StoreHalfWord (state, instr, lhs)) \
  79. LSBase = lhs + GetLS7RHS (state, instr);
  80. /* Store pre decrement. */
  81. #define SHPREDOWN() \
  82. (void)StoreHalfWord (state, instr, LHS - GetLS7RHS (state, instr));
  83. /* Store pre decrement writeback. */
  84. #define SHPREDOWNWB() \
  85. temp = LHS - GetLS7RHS (state, instr); \
  86. if (StoreHalfWord (state, instr, temp)) \
  87. LSBase = temp;
  88. /* Store pre increment. */
  89. #define SHPREUP() \
  90. (void)StoreHalfWord (state, instr, LHS + GetLS7RHS (state, instr));
  91. /* Store pre increment writeback. */
  92. #define SHPREUPWB() \
  93. temp = LHS + GetLS7RHS (state, instr); \
  94. if (StoreHalfWord (state, instr, temp)) \
  95. LSBase = temp;
  96. /* Load post decrement writeback. */
  97. #define LHPOSTDOWN() \
  98. { \
  99. int done = 1; \
  100. lhs = LHS; \
  101. temp = lhs - GetLS7RHS (state, instr); \
  102. \
  103. switch (BITS (5, 6)) \
  104. { \
  105. case 1: /* H */ \
  106. if (LoadHalfWord (state, instr, lhs, LUNSIGNED)) \
  107. LSBase = temp; \
  108. break; \
  109. case 2: /* SB */ \
  110. if (LoadByte (state, instr, lhs, LSIGNED)) \
  111. LSBase = temp; \
  112. break; \
  113. case 3: /* SH */ \
  114. if (LoadHalfWord (state, instr, lhs, LSIGNED)) \
  115. LSBase = temp; \
  116. break; \
  117. case 0: /* SWP handled elsewhere. */ \
  118. default: \
  119. done = 0; \
  120. break; \
  121. } \
  122. if (done) \
  123. break; \
  124. }
  125. /* Load post increment writeback. */
  126. #define LHPOSTUP() \
  127. { \
  128. int done = 1; \
  129. lhs = LHS; \
  130. temp = lhs + GetLS7RHS (state, instr); \
  131. \
  132. switch (BITS (5, 6)) \
  133. { \
  134. case 1: /* H */ \
  135. if (LoadHalfWord (state, instr, lhs, LUNSIGNED)) \
  136. LSBase = temp; \
  137. break; \
  138. case 2: /* SB */ \
  139. if (LoadByte (state, instr, lhs, LSIGNED)) \
  140. LSBase = temp; \
  141. break; \
  142. case 3: /* SH */ \
  143. if (LoadHalfWord (state, instr, lhs, LSIGNED)) \
  144. LSBase = temp; \
  145. break; \
  146. case 0: /* SWP handled elsewhere. */ \
  147. default: \
  148. done = 0; \
  149. break; \
  150. } \
  151. if (done) \
  152. break; \
  153. }
  154. /* Load pre decrement. */
  155. #define LHPREDOWN() \
  156. { \
  157. int done = 1; \
  158. \
  159. temp = LHS - GetLS7RHS (state, instr); \
  160. switch (BITS (5, 6)) \
  161. { \
  162. case 1: /* H */ \
  163. (void) LoadHalfWord (state, instr, temp, LUNSIGNED); \
  164. break; \
  165. case 2: /* SB */ \
  166. (void) LoadByte (state, instr, temp, LSIGNED); \
  167. break; \
  168. case 3: /* SH */ \
  169. (void) LoadHalfWord (state, instr, temp, LSIGNED); \
  170. break; \
  171. case 0: \
  172. /* SWP handled elsewhere. */ \
  173. default: \
  174. done = 0; \
  175. break; \
  176. } \
  177. if (done) \
  178. break; \
  179. }
  180. /* Load pre decrement writeback. */
  181. #define LHPREDOWNWB() \
  182. { \
  183. int done = 1; \
  184. \
  185. temp = LHS - GetLS7RHS (state, instr); \
  186. switch (BITS (5, 6)) \
  187. { \
  188. case 1: /* H */ \
  189. if (LoadHalfWord (state, instr, temp, LUNSIGNED)) \
  190. LSBase = temp; \
  191. break; \
  192. case 2: /* SB */ \
  193. if (LoadByte (state, instr, temp, LSIGNED)) \
  194. LSBase = temp; \
  195. break; \
  196. case 3: /* SH */ \
  197. if (LoadHalfWord (state, instr, temp, LSIGNED)) \
  198. LSBase = temp; \
  199. break; \
  200. case 0: \
  201. /* SWP handled elsewhere. */ \
  202. default: \
  203. done = 0; \
  204. break; \
  205. } \
  206. if (done) \
  207. break; \
  208. }
  209. /* Load pre increment. */
  210. #define LHPREUP() \
  211. { \
  212. int done = 1; \
  213. \
  214. temp = LHS + GetLS7RHS (state, instr); \
  215. switch (BITS (5, 6)) \
  216. { \
  217. case 1: /* H */ \
  218. (void) LoadHalfWord (state, instr, temp, LUNSIGNED); \
  219. break; \
  220. case 2: /* SB */ \
  221. (void) LoadByte (state, instr, temp, LSIGNED); \
  222. break; \
  223. case 3: /* SH */ \
  224. (void) LoadHalfWord (state, instr, temp, LSIGNED); \
  225. break; \
  226. case 0: \
  227. /* SWP handled elsewhere. */ \
  228. default: \
  229. done = 0; \
  230. break; \
  231. } \
  232. if (done) \
  233. break; \
  234. }
  235. /* Load pre increment writeback. */
  236. #define LHPREUPWB() \
  237. { \
  238. int done = 1; \
  239. \
  240. temp = LHS + GetLS7RHS (state, instr); \
  241. switch (BITS (5, 6)) \
  242. { \
  243. case 1: /* H */ \
  244. if (LoadHalfWord (state, instr, temp, LUNSIGNED)) \
  245. LSBase = temp; \
  246. break; \
  247. case 2: /* SB */ \
  248. if (LoadByte (state, instr, temp, LSIGNED)) \
  249. LSBase = temp; \
  250. break; \
  251. case 3: /* SH */ \
  252. if (LoadHalfWord (state, instr, temp, LSIGNED)) \
  253. LSBase = temp; \
  254. break; \
  255. case 0: \
  256. /* SWP handled elsewhere. */ \
  257. default: \
  258. done = 0; \
  259. break; \
  260. } \
  261. if (done) \
  262. break; \
  263. }
  264. /*ywc 2005-03-31*/
  265. //teawater add for arm2x86 2005.02.17-------------------------------------------
  266. #ifdef DBCT
  267. #include "dbct/tb.h"
  268. #include "dbct/arm2x86_self.h"
  269. #endif
  270. //AJ2D--------------------------------------------------------------------------
  271. //Diff register
  272. unsigned int mirror_register_file[39];
  273. /* EMULATION of ARM6. */
  274. extern int debugmode;
  275. int ARMul_ICE_debug(ARMul_State *state,ARMword instr,ARMword addr);
  276. #ifdef MODE32
  277. //chy 2006-04-12, for ICE debug
  278. int ARMul_ICE_debug(ARMul_State *state,ARMword instr,ARMword addr)
  279. {
  280. return 0;
  281. }
  282. static int dump = 0;
  283. ARMword ARMul_Debug(ARMul_State * state, ARMword pc, ARMword instr)
  284. {
  285. /*printf("[%08x] ", pc);
  286. arm11_Disasm32(pc);*/
  287. /*if (pc >= 0x0010303C && pc <= 0x00103050)
  288. {
  289. printf("[%08x] = %08X = ", pc, instr);
  290. arm11_Disasm32(pc);
  291. arm11_Dump();
  292. }*/
  293. //fprintf(stderr,"[%08x]\n", pc);
  294. //if (pc == 0x00240C88)
  295. // arm11_Dump();
  296. /*if (pc == 0x188e04)
  297. {
  298. DEBUG("read %08X %08X %016X %08X %08X from %08X", state->Reg[0], state->Reg[1], state->Reg[2] | state->Reg[3] << 32, mem_Read32(state->Reg[13]), mem_Read32(state->Reg[13] + 4), state->Reg[14]);
  299. }
  300. if (pc == 0x21222c)
  301. {
  302. arm11_Dump();
  303. mem_Dbugdump();
  304. }*/
  305. /*if (pc == 0x0022D168)
  306. {
  307. int j = 0;
  308. }*/
  309. /*if (state->Reg[4] == 0x00105734)
  310. {
  311. printf("[%08x] ", pc);
  312. arm11_Disasm32(pc);
  313. }*/
  314. return 0;
  315. }
  316. /*
  317. void chy_debug()
  318. {
  319. printf("SkyEye chy_deubeg begin\n");
  320. }
  321. */
  322. ARMword
  323. ARMul_Emulate32 (ARMul_State * state)
  324. #else
  325. ARMword
  326. ARMul_Emulate26 (ARMul_State * state)
  327. #endif
  328. {
  329. /* The PC pipeline value depends on whether ARM
  330. or Thumb instructions are being
  331. d. */
  332. ARMword isize;
  333. ARMword instr; /* The current instruction. */
  334. ARMword dest = 0; /* Almost the DestBus. */
  335. ARMword temp; /* Ubiquitous third hand. */
  336. ARMword pc = 0; /* The address of the current instruction. */
  337. ARMword lhs; /* Almost the ABus and BBus. */
  338. ARMword rhs;
  339. ARMword decoded = 0; /* Instruction pipeline. */
  340. ARMword loaded = 0;
  341. ARMword decoded_addr=0;
  342. ARMword loaded_addr=0;
  343. ARMword have_bp=0;
  344. #ifdef callstacker
  345. char a[256];
  346. #endif
  347. /* shenoubang */
  348. static int instr_sum = 0;
  349. int reg_index = 0;
  350. #if DIFF_STATE
  351. //initialize all mirror register for follow mode
  352. for (reg_index = 0; reg_index < 16; reg_index ++) {
  353. mirror_register_file[reg_index] = state->Reg[reg_index];
  354. }
  355. mirror_register_file[CPSR_REG] = state->Cpsr;
  356. mirror_register_file[R13_SVC] = state->RegBank[SVCBANK][13];
  357. mirror_register_file[R14_SVC] = state->RegBank[SVCBANK][14];
  358. mirror_register_file[R13_ABORT] = state->RegBank[ABORTBANK][13];
  359. mirror_register_file[R14_ABORT] = state->RegBank[ABORTBANK][14];
  360. mirror_register_file[R13_UNDEF] = state->RegBank[UNDEFBANK][13];
  361. mirror_register_file[R14_UNDEF] = state->RegBank[UNDEFBANK][14];
  362. mirror_register_file[R13_IRQ] = state->RegBank[IRQBANK][13];
  363. mirror_register_file[R14_IRQ] = state->RegBank[IRQBANK][14];
  364. mirror_register_file[R8_FIRQ] = state->RegBank[FIQBANK][8];
  365. mirror_register_file[R9_FIRQ] = state->RegBank[FIQBANK][9];
  366. mirror_register_file[R10_FIRQ] = state->RegBank[FIQBANK][10];
  367. mirror_register_file[R11_FIRQ] = state->RegBank[FIQBANK][11];
  368. mirror_register_file[R12_FIRQ] = state->RegBank[FIQBANK][12];
  369. mirror_register_file[R13_FIRQ] = state->RegBank[FIQBANK][13];
  370. mirror_register_file[R14_FIRQ] = state->RegBank[FIQBANK][14];
  371. mirror_register_file[SPSR_SVC] = state->Spsr[SVCBANK];
  372. mirror_register_file[SPSR_ABORT] = state->Spsr[ABORTBANK];
  373. mirror_register_file[SPSR_UNDEF] = state->Spsr[UNDEFBANK];
  374. mirror_register_file[SPSR_IRQ] = state->Spsr[IRQBANK];
  375. mirror_register_file[SPSR_FIRQ] = state->Spsr[FIQBANK];
  376. #endif
  377. /* Execute the next instruction. */
  378. if (state->NextInstr < PRIMEPIPE) {
  379. decoded = state->decoded;
  380. loaded = state->loaded;
  381. pc = state->pc;
  382. //chy 2006-04-12, for ICE debug
  383. decoded_addr=state->decoded_addr;
  384. loaded_addr=state->loaded_addr;
  385. }
  386. do {
  387. //print_func_name(state->pc);
  388. /* Just keep going. */
  389. isize = INSN_SIZE;
  390. switch (state->NextInstr) {
  391. case SEQ:
  392. /* Advance the pipeline, and an S cycle. */
  393. state->Reg[15] += isize;
  394. pc += isize;
  395. instr = decoded;
  396. //chy 2006-04-12, for ICE debug
  397. have_bp = ARMul_ICE_debug(state,instr,decoded_addr);
  398. decoded = loaded;
  399. decoded_addr=loaded_addr;
  400. //loaded = ARMul_LoadInstrS (state, pc + (isize * 2),
  401. // isize);
  402. loaded_addr=pc + (isize * 2);
  403. if (have_bp) goto TEST_EMULATE;
  404. break;
  405. case NONSEQ:
  406. /* Advance the pipeline, and an N cycle. */
  407. state->Reg[15] += isize;
  408. pc += isize;
  409. instr = decoded;
  410. //chy 2006-04-12, for ICE debug
  411. have_bp=ARMul_ICE_debug(state,instr,decoded_addr);
  412. decoded = loaded;
  413. decoded_addr=loaded_addr;
  414. //loaded = ARMul_LoadInstrN (state, pc + (isize * 2),
  415. // isize);
  416. loaded_addr=pc + (isize * 2);
  417. NORMALCYCLE;
  418. if (have_bp) goto TEST_EMULATE;
  419. break;
  420. case PCINCEDSEQ:
  421. /* Program counter advanced, and an S cycle. */
  422. pc += isize;
  423. instr = decoded;
  424. //chy 2006-04-12, for ICE debug
  425. have_bp=ARMul_ICE_debug(state,instr,decoded_addr);
  426. decoded = loaded;
  427. decoded_addr=loaded_addr;
  428. //loaded = ARMul_LoadInstrS (state, pc + (isize * 2),
  429. // isize);
  430. loaded_addr=pc + (isize * 2);
  431. NORMALCYCLE;
  432. if (have_bp) goto TEST_EMULATE;
  433. break;
  434. case PCINCEDNONSEQ:
  435. /* Program counter advanced, and an N cycle. */
  436. pc += isize;
  437. instr = decoded;
  438. //chy 2006-04-12, for ICE debug
  439. have_bp=ARMul_ICE_debug(state,instr,decoded_addr);
  440. decoded = loaded;
  441. decoded_addr=loaded_addr;
  442. //loaded = ARMul_LoadInstrN (state, pc + (isize * 2),
  443. // isize);
  444. loaded_addr=pc + (isize * 2);
  445. NORMALCYCLE;
  446. if (have_bp) goto TEST_EMULATE;
  447. break;
  448. case RESUME:
  449. /* The program counter has been changed. */
  450. pc = state->Reg[15];
  451. #ifndef MODE32
  452. pc = pc & R15PCBITS;
  453. #endif
  454. state->Reg[15] = pc + (isize * 2);
  455. state->Aborted = 0;
  456. //chy 2004-05-25, fix bug provided by Carl van Schaik<cvansch@cse.unsw.EDU.AU>
  457. state->AbortAddr = 1;
  458. instr = ARMul_LoadInstrN (state, pc, isize);
  459. //instr = ARMul_ReLoadInstr (state, pc, isize);
  460. //chy 2006-04-12, for ICE debug
  461. have_bp=ARMul_ICE_debug(state,instr,pc);
  462. //decoded =
  463. // ARMul_ReLoadInstr (state, pc + isize, isize);
  464. decoded_addr=pc+isize;
  465. //loaded = ARMul_ReLoadInstr (state, pc + isize * 2,
  466. // isize);
  467. loaded_addr=pc + isize * 2;
  468. NORMALCYCLE;
  469. if (have_bp) goto TEST_EMULATE;
  470. break;
  471. default:
  472. /* The program counter has been changed. */
  473. pc = state->Reg[15];
  474. #ifndef MODE32
  475. pc = pc & R15PCBITS;
  476. #endif
  477. state->Reg[15] = pc + (isize * 2);
  478. state->Aborted = 0;
  479. //chy 2004-05-25, fix bug provided by Carl van Schaik<cvansch@cse.unsw.EDU.AU>
  480. state->AbortAddr = 1;
  481. instr = ARMul_LoadInstrN (state, pc, isize);
  482. //chy 2006-04-12, for ICE debug
  483. have_bp=ARMul_ICE_debug(state,instr,pc);
  484. #if 0
  485. decoded =
  486. ARMul_LoadInstrS (state, pc + (isize), isize);
  487. #endif
  488. decoded_addr=pc+isize;
  489. #if 0
  490. loaded = ARMul_LoadInstrS (state, pc + (isize * 2),
  491. isize);
  492. #endif
  493. loaded_addr=pc + isize * 2;
  494. NORMALCYCLE;
  495. if (have_bp) goto TEST_EMULATE;
  496. break;
  497. }
  498. #if 0
  499. int idx = 0;
  500. printf("pc:%x\n", pc);
  501. for (; idx < 17; idx ++) {
  502. printf("R%d:%x\t", idx, state->Reg[idx]);
  503. }
  504. printf("\n");
  505. #endif
  506. instr = ARMul_LoadInstrN (state, pc, isize);
  507. state->last_instr = state->CurrInstr;
  508. state->CurrInstr = instr;
  509. ARMul_Debug(state, pc, instr);
  510. #if 0
  511. if((state->NumInstrs % 10000000) == 0)
  512. printf("---|%p|--- %lld\n", pc, state->NumInstrs);
  513. if(state->NumInstrs > (3000000000)) {
  514. static int flag = 0;
  515. if(pc == 0x8032ccc4) {
  516. flag = 300;
  517. }
  518. if(flag) {
  519. int idx = 0;
  520. printf("------------------------------------\n");
  521. printf("pc:%x\n", pc);
  522. for (; idx < 17; idx ++) {
  523. printf("R%d:%x\t", idx, state->Reg[idx]);
  524. }
  525. printf("\nN:%d\t Z:%d\t C:%d\t V:%d\n", state->NFlag, state->ZFlag, state->CFlag, state->VFlag);
  526. printf("\n");
  527. printf("------------------------------------\n");
  528. flag--;
  529. }
  530. }
  531. #endif
  532. #if DIFF_STATE
  533. fprintf(state->state_log, "PC:0x%x\n", pc);
  534. if (pc && (pc + 8) != state->Reg[15]) {
  535. printf("lucky dog\n");
  536. printf("pc is %x, R15 is %x\n", pc, state->Reg[15]);
  537. //exit(-1);
  538. }
  539. for (reg_index = 0; reg_index < 16; reg_index ++) {
  540. if (state->Reg[reg_index] != mirror_register_file[reg_index]) {
  541. fprintf(state->state_log, "R%d:0x%x\n", reg_index, state->Reg[reg_index]);
  542. mirror_register_file[reg_index] = state->Reg[reg_index];
  543. }
  544. }
  545. if (state->Cpsr != mirror_register_file[CPSR_REG]) {
  546. fprintf(state->state_log, "Cpsr:0x%x\n", state->Cpsr);
  547. mirror_register_file[CPSR_REG] = state->Cpsr;
  548. }
  549. if (state->RegBank[SVCBANK][13] != mirror_register_file[R13_SVC]) {
  550. fprintf(state->state_log, "R13_SVC:0x%x\n", state->RegBank[SVCBANK][13]);
  551. mirror_register_file[R13_SVC] = state->RegBank[SVCBANK][13];
  552. }
  553. if (state->RegBank[SVCBANK][14] != mirror_register_file[R14_SVC]) {
  554. fprintf(state->state_log, "R14_SVC:0x%x\n", state->RegBank[SVCBANK][14]);
  555. mirror_register_file[R14_SVC] = state->RegBank[SVCBANK][14];
  556. }
  557. if (state->RegBank[ABORTBANK][13] != mirror_register_file[R13_ABORT]) {
  558. fprintf(state->state_log, "R13_ABORT:0x%x\n", state->RegBank[ABORTBANK][13]);
  559. mirror_register_file[R13_ABORT] = state->RegBank[ABORTBANK][13];
  560. }
  561. if (state->RegBank[ABORTBANK][14] != mirror_register_file[R14_ABORT]) {
  562. fprintf(state->state_log, "R14_ABORT:0x%x\n", state->RegBank[ABORTBANK][14]);
  563. mirror_register_file[R14_ABORT] = state->RegBank[ABORTBANK][14];
  564. }
  565. if (state->RegBank[UNDEFBANK][13] != mirror_register_file[R13_UNDEF]) {
  566. fprintf(state->state_log, "R13_UNDEF:0x%x\n", state->RegBank[UNDEFBANK][13]);
  567. mirror_register_file[R13_UNDEF] = state->RegBank[UNDEFBANK][13];
  568. }
  569. if (state->RegBank[UNDEFBANK][14] != mirror_register_file[R14_UNDEF]) {
  570. fprintf(state->state_log, "R14_UNDEF:0x%x\n", state->RegBank[UNDEFBANK][14]);
  571. mirror_register_file[R14_UNDEF] = state->RegBank[UNDEFBANK][14];
  572. }
  573. if (state->RegBank[IRQBANK][13] != mirror_register_file[R13_IRQ]) {
  574. fprintf(state->state_log, "R13_IRQ:0x%x\n", state->RegBank[IRQBANK][13]);
  575. mirror_register_file[R13_IRQ] = state->RegBank[IRQBANK][13];
  576. }
  577. if (state->RegBank[IRQBANK][14] != mirror_register_file[R14_IRQ]) {
  578. fprintf(state->state_log, "R14_IRQ:0x%x\n", state->RegBank[IRQBANK][14]);
  579. mirror_register_file[R14_IRQ] = state->RegBank[IRQBANK][14];
  580. }
  581. if (state->RegBank[FIQBANK][8] != mirror_register_file[R8_FIRQ]) {
  582. fprintf(state->state_log, "R8_FIRQ:0x%x\n", state->RegBank[FIQBANK][8]);
  583. mirror_register_file[R8_FIRQ] = state->RegBank[FIQBANK][8];
  584. }
  585. if (state->RegBank[FIQBANK][9] != mirror_register_file[R9_FIRQ]) {
  586. fprintf(state->state_log, "R9_FIRQ:0x%x\n", state->RegBank[FIQBANK][9]);
  587. mirror_register_file[R9_FIRQ] = state->RegBank[FIQBANK][9];
  588. }
  589. if (state->RegBank[FIQBANK][10] != mirror_register_file[R10_FIRQ]) {
  590. fprintf(state->state_log, "R10_FIRQ:0x%x\n", state->RegBank[FIQBANK][10]);
  591. mirror_register_file[R10_FIRQ] = state->RegBank[FIQBANK][10];
  592. }
  593. if (state->RegBank[FIQBANK][11] != mirror_register_file[R11_FIRQ]) {
  594. fprintf(state->state_log, "R11_FIRQ:0x%x\n", state->RegBank[FIQBANK][11]);
  595. mirror_register_file[R11_FIRQ] = state->RegBank[FIQBANK][11];
  596. }
  597. if (state->RegBank[FIQBANK][12] != mirror_register_file[R12_FIRQ]) {
  598. fprintf(state->state_log, "R12_FIRQ:0x%x\n", state->RegBank[FIQBANK][12]);
  599. mirror_register_file[R12_FIRQ] = state->RegBank[FIQBANK][12];
  600. }
  601. if (state->RegBank[FIQBANK][13] != mirror_register_file[R13_FIRQ]) {
  602. fprintf(state->state_log, "R13_FIRQ:0x%x\n", state->RegBank[FIQBANK][13]);
  603. mirror_register_file[R13_FIRQ] = state->RegBank[FIQBANK][13];
  604. }
  605. if (state->RegBank[FIQBANK][14] != mirror_register_file[R14_FIRQ]) {
  606. fprintf(state->state_log, "R14_FIRQ:0x%x\n", state->RegBank[FIQBANK][14]);
  607. mirror_register_file[R14_FIRQ] = state->RegBank[FIQBANK][14];
  608. }
  609. if (state->Spsr[SVCBANK] != mirror_register_file[SPSR_SVC]) {
  610. fprintf(state->state_log, "SPSR_SVC:0x%x\n", state->Spsr[SVCBANK]);
  611. mirror_register_file[SPSR_SVC] = state->RegBank[SVCBANK];
  612. }
  613. if (state->Spsr[ABORTBANK] != mirror_register_file[SPSR_ABORT]) {
  614. fprintf(state->state_log, "SPSR_ABORT:0x%x\n", state->Spsr[ABORTBANK]);
  615. mirror_register_file[SPSR_ABORT] = state->RegBank[ABORTBANK];
  616. }
  617. if (state->Spsr[UNDEFBANK] != mirror_register_file[SPSR_UNDEF]) {
  618. fprintf(state->state_log, "SPSR_UNDEF:0x%x\n", state->Spsr[UNDEFBANK]);
  619. mirror_register_file[SPSR_UNDEF] = state->RegBank[UNDEFBANK];
  620. }
  621. if (state->Spsr[IRQBANK] != mirror_register_file[SPSR_IRQ]) {
  622. fprintf(state->state_log, "SPSR_IRQ:0x%x\n", state->Spsr[IRQBANK]);
  623. mirror_register_file[SPSR_IRQ] = state->RegBank[IRQBANK];
  624. }
  625. if (state->Spsr[FIQBANK] != mirror_register_file[SPSR_FIRQ]) {
  626. fprintf(state->state_log, "SPSR_FIRQ:0x%x\n", state->Spsr[FIQBANK]);
  627. mirror_register_file[SPSR_FIRQ] = state->RegBank[FIQBANK];
  628. }
  629. #endif
  630. #if 0
  631. uint32_t alex = 0;
  632. static int flagged = 0;
  633. if ((flagged == 0) && (pc == 0xb224)) {
  634. flagged++;
  635. }
  636. if ((flagged == 1) && (pc == 0x1a800)) {
  637. flagged++;
  638. }
  639. if (flagged == 3) {
  640. printf("---|%p|--- %x\n", pc, state->NumInstrs);
  641. for (alex = 0; alex < 15; alex++) {
  642. printf("R%02d % 8x\n", alex, state->Reg[alex]);
  643. }
  644. printf("R%02d % 8x\n", alex, state->Reg[alex] - 8);
  645. printf("CPS %x%07x\n", (state->NFlag<<3 | state->ZFlag<<2 | state->CFlag<<1 | state->VFlag), state->Cpsr & 0xfffffff);
  646. } else {
  647. if (state->NumInstrs < 0x400000) {
  648. //exit(-1);
  649. }
  650. }
  651. #endif
  652. /*if (state->EventSet)
  653. ARMul_EnvokeEvent (state);*/
  654. #if 0
  655. /* do profiling for code coverage */
  656. if (skyeye_config.code_cov.prof_on)
  657. cov_prof(EXEC_FLAG, pc);
  658. #endif
  659. //2003-07-11 chy: for test
  660. #if 0
  661. if (skyeye_config.log.logon >= 1) {
  662. if (state->NumInstrs >= skyeye_config.log.start &&
  663. state->NumInstrs <= skyeye_config.log.end) {
  664. static int mybegin = 0;
  665. static int myinstrnum = 0;
  666. if (mybegin == 0)
  667. mybegin = 1;
  668. #if 0
  669. if (state->NumInstrs == 3695) {
  670. printf ("***********SKYEYE: numinstr = 3695\n");
  671. }
  672. static int mybeg2 = 0;
  673. static int mybeg3 = 0;
  674. static int mybeg4 = 0;
  675. static int mybeg5 = 0;
  676. if (pc == 0xa0008000) {
  677. //mybegin=1;
  678. printf ("************SKYEYE: real vmlinux begin now numinstr is %llu ****************\n", state->NumInstrs);
  679. }
  680. //chy 2003-09-02 test fiq
  681. if (state->NumInstrs == 67347000) {
  682. printf ("***********SKYEYE: numinstr = 67347000, begin log\n");
  683. mybegin = 1;
  684. }
  685. if (pc == 0xc00087b4) { //numinstr=67348714
  686. mybegin = 1;
  687. printf ("************SKYEYE: test irq now numinstr is %llu ****************\n", state->NumInstrs);
  688. }
  689. if (pc == 0xc00087b8) { //in start_kernel::sti()
  690. mybeg4 = 1;
  691. printf ("************SKYEYE: startkerenl: sti now numinstr is %llu ********\n", state->NumInstrs);
  692. }
  693. /*if (pc==0xc001e4f4||pc==0xc001e4f8||pc==0xc001e4fc||pc==0xc001e500||pc==0xffff0004) { //MRA instr */
  694. if (pc == 0xc001e500) { //MRA instr
  695. mybeg5 = 1;
  696. printf ("************SKYEYE: MRA instr now numinstr is %llu ********\n", state->NumInstrs);
  697. }
  698. if (pc >= 0xc0000000 && mybeg2 == 0) {
  699. mybeg2 = 1;
  700. printf ("************SKYEYE: enable mmu&cache, now numinstr is %llu **************\n", state->NumInstrs);
  701. SKYEYE_OUTREGS (stderr);
  702. printf ("************************************************************************\n");
  703. }
  704. //chy 2003-09-01 test after tlb-flush
  705. if (pc == 0xc00261ac) {
  706. //sleep(2);
  707. mybeg3 = 1;
  708. printf ("************SKYEYE: after tlb-flush numinstr is %llu ****************\n", state->NumInstrs);
  709. }
  710. if (mybeg3 == 1) {
  711. SKYEYE_OUTREGS (skyeye_logfd);
  712. SKYEYE_OUTMOREREGS (skyeye_logfd);
  713. fprintf (skyeye_logfd, "\n");
  714. }
  715. #endif
  716. if (mybegin == 1) {
  717. //fprintf(skyeye_logfd,"p %x,i %x,d %x,l %x,",pc,instr,decoded,loaded);
  718. //chy for test 20050729
  719. /*if (state->NumInstrs>=3302294) {
  720. if (pc==0x100c9d4 && instr==0xe1b0f00e){
  721. chy_debug();
  722. printf("*********************************************\n");
  723. printf("******SKYEYE N %llx :p %x,i %x\n SKYEYE******\n",state->NumInstrs,pc,instr);
  724. printf("*********************************************\n");
  725. }
  726. */
  727. if (skyeye_config.log.logon >= 1)
  728. /*
  729. fprintf (skyeye_logfd,
  730. "N %llx :p %x,i %x,",
  731. state->NumInstrs, pc,
  732. #ifdef MODET
  733. TFLAG ? instr & 0xffff : instr
  734. #else
  735. instr
  736. #endif
  737. );
  738. */
  739. fprintf(skyeye_logfd, "pc=0x%x,r3=0x%x\n", pc, state->Reg[3]);
  740. if (skyeye_config.log.logon >= 2)
  741. SKYEYE_OUTREGS (skyeye_logfd);
  742. if (skyeye_config.log.logon >= 3)
  743. SKYEYE_OUTMOREREGS
  744. (skyeye_logfd);
  745. //fprintf (skyeye_logfd, "\n");
  746. if (skyeye_config.log.length > 0) {
  747. myinstrnum++;
  748. if (myinstrnum >=
  749. skyeye_config.log.
  750. length) {
  751. myinstrnum = 0;
  752. fflush (skyeye_logfd);
  753. fseek (skyeye_logfd,
  754. 0L, SEEK_SET);
  755. }
  756. }
  757. }
  758. //SKYEYE_OUTREGS(skyeye_logfd);
  759. //SKYEYE_OUTMOREREGS(skyeye_logfd);
  760. }
  761. }
  762. #endif
  763. #if 0 /* Enable this for a helpful bit of debugging when tracing is needed. */
  764. fprintf (stderr, "pc: %x, instr: %x\n", pc & ~1, instr);
  765. if (instr == 0)
  766. abort ();
  767. #endif
  768. #if 0 /* Enable this code to help track down stack alignment bugs. */
  769. {
  770. static ARMword old_sp = -1;
  771. if (old_sp != state->Reg[13]) {
  772. old_sp = state->Reg[13];
  773. fprintf (stderr,
  774. "pc: %08x: SP set to %08x%s\n",
  775. pc & ~1, old_sp,
  776. (old_sp % 8) ? " [UNALIGNED!]" : "");
  777. }
  778. }
  779. #endif
  780. /* Any exceptions ? */
  781. if (state->NresetSig == LOW) {
  782. ARMul_Abort (state, ARMul_ResetV);
  783. /*added energy_prof statement by ksh in 2004-11-26 */
  784. //chy 2005-07-28 for standalone
  785. //ARMul_do_energy(state,instr,pc);
  786. break;
  787. } else if (!state->NfiqSig && !FFLAG) {
  788. ARMul_Abort (state, ARMul_FIQV);
  789. /*added energy_prof statement by ksh in 2004-11-26 */
  790. //chy 2005-07-28 for standalone
  791. //ARMul_do_energy(state,instr,pc);
  792. break;
  793. } else if (!state->NirqSig && !IFLAG) {
  794. ARMul_Abort (state, ARMul_IRQV);
  795. /*added energy_prof statement by ksh in 2004-11-26 */
  796. //chy 2005-07-28 for standalone
  797. //ARMul_do_energy(state,instr,pc);
  798. break;
  799. }
  800. //teawater add for arm2x86 2005.04.26-------------------------------------------
  801. #if 0
  802. // if (state->pc == 0xc011a868 || state->pc == 0xc011a86c) {
  803. if (state->NumInstrs == 1671574 || state->NumInstrs == 1671573 || state->NumInstrs == 1671572
  804. || state->NumInstrs == 1671575) {
  805. for (reg_index = 0; reg_index < 16; reg_index ++) {
  806. printf("R%d:%x\t", reg_index, state->Reg[reg_index]);
  807. }
  808. printf("\n");
  809. }
  810. #endif
  811. if (state->tea_pc) {
  812. int i;
  813. if (state->tea_reg_fd) {
  814. fprintf (state->tea_reg_fd, "\n");
  815. for (i = 0; i < 15; i++) {
  816. fprintf (state->tea_reg_fd, "%x,",
  817. state->Reg[i]);
  818. }
  819. fprintf (state->tea_reg_fd, "%x,", pc);
  820. state->Cpsr = ARMul_GetCPSR (state);
  821. fprintf (state->tea_reg_fd, "%x\n",
  822. state->Cpsr);
  823. } else {
  824. printf ("\n");
  825. for (i = 0; i < 15; i++) {
  826. printf ("%x,", state->Reg[i]);
  827. }
  828. printf ("%x,", pc);
  829. state->Cpsr = ARMul_GetCPSR (state);
  830. printf ("%x\n", state->Cpsr);
  831. }
  832. }
  833. //AJ2D--------------------------------------------------------------------------
  834. /*if (state->CallDebug > 0) {
  835. instr = ARMul_Debug (state, pc, instr);
  836. if (state->Emulate < ONCE) {
  837. state->NextInstr = RESUME;
  838. break;
  839. }
  840. if (state->Debug) {
  841. fprintf (stderr,
  842. "sim: At %08lx Instr %08lx Mode %02lx\n",
  843. pc, instr, state->Mode);
  844. (void) fgetc (stdin);
  845. }
  846. }
  847. else*/
  848. if (state->Emulate < ONCE) {
  849. state->NextInstr = RESUME;
  850. break;
  851. }
  852. //io_do_cycle (state);
  853. state->NumInstrs++;
  854. #if 0
  855. if (state->NumInstrs % 10000000 == 0) {
  856. printf("10 MIPS instr have been executed\n");
  857. }
  858. #endif
  859. #ifdef MODET
  860. /* Provide Thumb instruction decoding. If the processor is in Thumb
  861. mode, then we can simply decode the Thumb instruction, and map it
  862. to the corresponding ARM instruction (by directly loading the
  863. instr variable, and letting the normal ARM simulator
  864. execute). There are some caveats to ensure that the correct
  865. pipelined PC value is used when executing Thumb code, and also for
  866. dealing with the BL instruction. */
  867. if (TFLAG) {
  868. ARMword armOp = 0;
  869. /* Check if in Thumb mode. */
  870. switch (ARMul_ThumbDecode(state, pc, instr, &armOp)) {
  871. case t_undefined:
  872. /* This is a Thumb instruction. */
  873. ARMul_UndefInstr (state, instr);
  874. goto donext;
  875. case t_branch:
  876. /* Already processed. */
  877. //pc = state->Reg[15] - 2;
  878. //state->pc = state->Reg[15] - 2; //ichfly why do I need that
  879. goto donext;
  880. case t_decoded:
  881. /* ARM instruction available. */
  882. //printf("t decode %04lx -> %08lx\n", instr & 0xffff, armOp);
  883. if (armOp == 0xDEADC0DE) {
  884. LOG_ERROR(Core_ARM11, "Failed to decode thumb opcode %04X at %08X", instr, pc);
  885. }
  886. instr = armOp;
  887. /* So continue instruction decoding. */
  888. break;
  889. default:
  890. break;
  891. }
  892. }
  893. #endif
  894. /* Check the condition codes. */
  895. if ((temp = TOPBITS (28)) == AL) {
  896. /* Vile deed in the need for speed. */
  897. goto mainswitch;
  898. }
  899. /* Check the condition code. */
  900. switch ((int) TOPBITS (28)) {
  901. case AL:
  902. temp = TRUE;
  903. break;
  904. case NV:
  905. /* shenoubang add for armv7 instr dmb 2012-3-11 */
  906. if (state->is_v7) {
  907. if ((instr & 0x0fffff00) == 0x057ff000) {
  908. switch((instr >> 4) & 0xf) {
  909. case 4: /* dsb */
  910. case 5: /* dmb */
  911. case 6: /* isb */
  912. // TODO: do no implemented thes instr
  913. goto donext;
  914. }
  915. }
  916. }
  917. /* dyf add for armv6 instruct CPS 2010.9.17 */
  918. if (state->is_v6) {
  919. /* clrex do nothing here temporary */
  920. if (instr == 0xf57ff01f) {
  921. //printf("clrex \n");
  922. #if 0
  923. int i;
  924. for(i = 0; i < 128; i++) {
  925. state->exclusive_tag_array[i] = 0xffffffff;
  926. }
  927. #endif
  928. /* shenoubang 2012-3-14 refer the dyncom_interpreter */
  929. state->exclusive_tag_array[0] = 0xFFFFFFFF;
  930. state->exclusive_access_state = 0;
  931. goto donext;
  932. }
  933. if (BITS(20, 27) == 0x10) {
  934. if (BIT(19)) {
  935. if (BIT(8)) {
  936. if (BIT(18))
  937. state->Cpsr |= 1<<8;
  938. else
  939. state->Cpsr &= ~(1<<8);
  940. }
  941. if (BIT(7)) {
  942. if (BIT(18))
  943. state->Cpsr |= 1<<7;
  944. else
  945. state->Cpsr &= ~(1<<7);
  946. ASSIGNINT (state->Cpsr & INTBITS);
  947. }
  948. if (BIT(6)) {
  949. if (BIT(18))
  950. state->Cpsr |= 1<<6;
  951. else
  952. state->Cpsr &= ~(1<<6);
  953. ASSIGNINT (state->Cpsr & INTBITS);
  954. }
  955. }
  956. if (BIT(17)) {
  957. state->Cpsr |= BITS(0, 4);
  958. printf("skyeye test state->Mode\n");
  959. if (state->Mode != (state->Cpsr & MODEBITS)) {
  960. state->Mode = ARMul_SwitchMode (state, state->Mode, state->Cpsr & MODEBITS);
  961. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  962. }
  963. }
  964. goto donext;
  965. }
  966. }
  967. if (state->is_v5) {
  968. if (BITS (25, 27) == 5) { /* BLX(1) */
  969. ARMword dest;
  970. state->Reg[14] = pc + 4;
  971. /* Force entry into Thumb mode. */
  972. dest = pc + 8 + 1;
  973. if (BIT (23))
  974. dest += (NEGBRANCH +
  975. (BIT (24) << 1));
  976. else
  977. dest += POSBRANCH +
  978. (BIT (24) << 1);
  979. WriteR15Branch (state, dest);
  980. goto donext;
  981. } else if ((instr & 0xFC70F000) == 0xF450F000) {
  982. /* The PLD instruction. Ignored. */
  983. goto donext;
  984. } else if (((instr & 0xfe500f00) == 0xfc100100)
  985. || ((instr & 0xfe500f00) ==
  986. 0xfc000100)) {
  987. /* wldrw and wstrw are unconditional. */
  988. goto mainswitch;
  989. } else {
  990. /* UNDEFINED in v5, UNPREDICTABLE in v3, v4, non executed in v1, v2. */
  991. ARMul_UndefInstr (state, instr);
  992. }
  993. }
  994. temp = FALSE;
  995. break;
  996. case EQ:
  997. temp = ZFLAG;
  998. break;
  999. case NE:
  1000. temp = !ZFLAG;
  1001. break;
  1002. case VS:
  1003. temp = VFLAG;
  1004. break;
  1005. case VC:
  1006. temp = !VFLAG;
  1007. break;
  1008. case MI:
  1009. temp = NFLAG;
  1010. break;
  1011. case PL:
  1012. temp = !NFLAG;
  1013. break;
  1014. case CS:
  1015. temp = CFLAG;
  1016. break;
  1017. case CC:
  1018. temp = !CFLAG;
  1019. break;
  1020. case HI:
  1021. temp = (CFLAG && !ZFLAG);
  1022. break;
  1023. case LS:
  1024. temp = (!CFLAG || ZFLAG);
  1025. break;
  1026. case GE:
  1027. temp = ((!NFLAG && !VFLAG) || (NFLAG && VFLAG));
  1028. break;
  1029. case LT:
  1030. temp = ((NFLAG && !VFLAG) || (!NFLAG && VFLAG));
  1031. break;
  1032. case GT:
  1033. temp = ((!NFLAG && !VFLAG && !ZFLAG)
  1034. || (NFLAG && VFLAG && !ZFLAG));
  1035. break;
  1036. case LE:
  1037. temp = ((NFLAG && !VFLAG) || (!NFLAG && VFLAG))
  1038. || ZFLAG;
  1039. break;
  1040. } /* cc check */
  1041. //chy 2003-08-24 now #if 0 .... #endif process cp14, cp15.reg14, I disable it...
  1042. /* Actual execution of instructions begins here. */
  1043. /* If the condition codes don't match, stop here. */
  1044. if (temp) {
  1045. mainswitch:
  1046. /* shenoubang sbfx and ubfx instr 2012-3-16 */
  1047. if (state->is_v6) {
  1048. unsigned int m, lsb, width, Rd, Rn, data;
  1049. Rd = Rn = lsb = width = data = m = 0;
  1050. //printf("helloworld\n");
  1051. if ((((int) BITS (21, 27)) == 0x3f) && (((int) BITS (4, 6)) == 0x5)) {
  1052. m = (unsigned)BITS(7, 11);
  1053. width = (unsigned)BITS(16, 20);
  1054. Rd = (unsigned)BITS(12, 15);
  1055. Rn = (unsigned)BITS(0, 3);
  1056. if ((Rd == 15) || (Rn == 15)) {
  1057. ARMul_UndefInstr (state, instr);
  1058. } else if ((m + width) < 32) {
  1059. data = state->Reg[Rn];
  1060. state->Reg[Rd] ^= state->Reg[Rd];
  1061. state->Reg[Rd] = ((ARMword)(data << (31 -(m + width))) >> ((31 - (m + width)) + (m)));
  1062. //SKYEYE_LOG_IN_CLR(RED, "UBFX: In %s, line = %d, Reg_src[%d] = 0x%x, Reg_d[%d] = 0x%x, m = %d, width = %d, Rd = %d, Rn = %d\n",
  1063. // __FUNCTION__, __LINE__, Rn, data, Rd, state->Reg[Rd], m, width + 1, Rd, Rn);
  1064. goto donext;
  1065. }
  1066. } // ubfx instr
  1067. else if ((((int) BITS (21, 27)) == 0x3d) && (((int) BITS (4, 6)) == 0x5)) {
  1068. int tmp = 0;
  1069. Rd = BITS(12, 15);
  1070. Rn = BITS(0, 3);
  1071. lsb = BITS(7, 11);
  1072. width = BITS(16, 20);
  1073. if ((Rd == 15) || (Rn == 15)) {
  1074. ARMul_UndefInstr (state, instr);
  1075. } else if ((lsb + width) < 32) {
  1076. state->Reg[Rd] ^= state->Reg[Rd];
  1077. data = state->Reg[Rn];
  1078. tmp = (data << (32 - (lsb + width + 1)));
  1079. state->Reg[Rd] = (tmp >> (32 - (lsb + width + 1)));
  1080. //SKYEYE_LOG_IN_CLR(RED, "sbfx: In %s, line = %d, pc = 0x%x, instr = 0x%x,Rd = 0x%x, Rn = 0x%x, lsb = %d, width = %d, Rs[%d] = 0x%x, Rd[%d] = 0x%x\n",
  1081. // __func__, __LINE__, pc, instr, Rd, Rn, lsb, width + 1, Rn, state->Reg[Rn], Rd, state->Reg[Rd]);
  1082. goto donext;
  1083. }
  1084. } // sbfx instr
  1085. else if ((((int)BITS(21, 27)) == 0x3e) && ((int)BITS(4, 6) == 0x1)) {
  1086. //(ARMword)(instr<<(31-(n))) >> ((31-(n))+(m))
  1087. unsigned msb ,tmp_rn, tmp_rd, dst;
  1088. tmp_rd = tmp_rn = dst = 0;
  1089. Rd = BITS(12, 15);
  1090. Rn = BITS(0, 3);
  1091. lsb = BITS(7, 11);
  1092. msb = BITS(16, 20); //-V519
  1093. if ((Rd == 15)) {
  1094. ARMul_UndefInstr (state, instr);
  1095. } else if ((Rn == 15)) {
  1096. data = state->Reg[Rd];
  1097. tmp_rd = ((ARMword)(data << (31 - lsb)) >> (31 - lsb));
  1098. dst = ((data >> msb) << (msb - lsb));
  1099. dst = (dst << lsb) | tmp_rd;
  1100. goto donext;
  1101. } // bfc instr
  1102. else if (((msb >= lsb) && (msb < 32))) {
  1103. data = state->Reg[Rn];
  1104. tmp_rn = ((ARMword)(data << (31 - (msb - lsb))) >> (31 - (msb - lsb)));
  1105. data = state->Reg[Rd];
  1106. tmp_rd = ((ARMword)(data << (31 - lsb)) >> (31 - lsb));
  1107. dst = ((data >> msb) << (msb - lsb)) | tmp_rn;
  1108. dst = (dst << lsb) | tmp_rd;
  1109. goto donext;
  1110. } // bfi instr
  1111. }
  1112. }
  1113. switch ((int) BITS (20, 27)) {
  1114. /* Data Processing Register RHS Instructions. */
  1115. case 0x00: /* AND reg and MUL */
  1116. #ifdef MODET
  1117. if (BITS (4, 11) == 0xB) {
  1118. /* STRH register offset, no write-back, down, post indexed. */
  1119. SHDOWNWB ();
  1120. break;
  1121. }
  1122. if (BITS (4, 7) == 0xD) {
  1123. Handle_Load_Double (state, instr);
  1124. break;
  1125. }
  1126. if (BITS (4, 7) == 0xF) {
  1127. Handle_Store_Double (state, instr);
  1128. break;
  1129. }
  1130. #endif
  1131. if (BITS (4, 7) == 9) {
  1132. /* MUL */
  1133. rhs = state->Reg[MULRHSReg];
  1134. //if (MULLHSReg == MULDESTReg) {
  1135. if(0) { /* For armv6, the restriction is removed */
  1136. UNDEF_MULDestEQOp1;
  1137. state->Reg[MULDESTReg] = 0;
  1138. } else if (MULDESTReg != 15)
  1139. state->Reg[MULDESTReg] = state->Reg[MULLHSReg] * rhs;
  1140. else
  1141. UNDEF_MULPCDest;
  1142. for (dest = 0, temp = 0; dest < 32;
  1143. dest++)
  1144. if (rhs & (1L << dest))
  1145. temp = dest;
  1146. /* Mult takes this many/2 I cycles. */
  1147. ARMul_Icycles (state, ARMul_MultTable[temp], 0L);
  1148. } else {
  1149. /* AND reg. */
  1150. rhs = DPRegRHS;
  1151. dest = LHS & rhs;
  1152. WRITEDEST (dest);
  1153. }
  1154. break;
  1155. case 0x01: /* ANDS reg and MULS */
  1156. #ifdef MODET
  1157. if ((BITS (4, 11) & 0xF9) == 0x9)
  1158. /* LDR register offset, no write-back, down, post indexed. */
  1159. LHPOSTDOWN ();
  1160. /* Fall through to rest of decoding. */
  1161. #endif
  1162. if (BITS (4, 7) == 9) {
  1163. /* MULS */
  1164. rhs = state->Reg[MULRHSReg];
  1165. //if (MULLHSReg == MULDESTReg) {
  1166. if(0) {
  1167. printf("Something in %d line\n", __LINE__);
  1168. UNDEF_WARNING;
  1169. UNDEF_MULDestEQOp1;
  1170. state->Reg[MULDESTReg] = 0;
  1171. CLEARN;
  1172. SETZ;
  1173. } else if (MULDESTReg != 15) {
  1174. dest = state->Reg[MULLHSReg] * rhs;
  1175. ARMul_NegZero (state, dest);
  1176. state->Reg[MULDESTReg] = dest;
  1177. } else
  1178. UNDEF_MULPCDest;
  1179. for (dest = 0, temp = 0; dest < 32;
  1180. dest++)
  1181. if (rhs & (1L << dest))
  1182. temp = dest;
  1183. /* Mult takes this many/2 I cycles. */
  1184. ARMul_Icycles (state, ARMul_MultTable[temp], 0L);
  1185. } else {
  1186. /* ANDS reg. */
  1187. rhs = DPSRegRHS;
  1188. dest = LHS & rhs;
  1189. WRITESDEST (dest);
  1190. }
  1191. break;
  1192. case 0x02: /* EOR reg and MLA */
  1193. #ifdef MODET
  1194. if (BITS (4, 11) == 0xB) {
  1195. /* STRH register offset, write-back, down, post indexed. */
  1196. SHDOWNWB ();
  1197. break;
  1198. }
  1199. #endif
  1200. if (BITS (4, 7) == 9) { /* MLA */
  1201. rhs = state->Reg[MULRHSReg];
  1202. #if 0
  1203. if (MULLHSReg == MULDESTReg) {
  1204. UNDEF_MULDestEQOp1;
  1205. state->Reg[MULDESTReg] = state->Reg[MULACCReg];
  1206. } else if (MULDESTReg != 15) {
  1207. #endif
  1208. if (MULDESTReg != 15) {
  1209. state->Reg[MULDESTReg] = state->Reg[MULLHSReg] * rhs + state->Reg[MULACCReg];
  1210. } else
  1211. UNDEF_MULPCDest;
  1212. for (dest = 0, temp = 0; dest < 32;
  1213. dest++)
  1214. if (rhs & (1L << dest))
  1215. temp = dest;
  1216. /* Mult takes this many/2 I cycles. */
  1217. ARMul_Icycles (state, ARMul_MultTable[temp], 0L);
  1218. } else {
  1219. rhs = DPRegRHS;
  1220. dest = LHS ^ rhs;
  1221. WRITEDEST (dest);
  1222. }
  1223. break;
  1224. case 0x03: /* EORS reg and MLAS */
  1225. #ifdef MODET
  1226. if ((BITS (4, 11) & 0xF9) == 0x9)
  1227. /* LDR register offset, write-back, down, post-indexed. */
  1228. LHPOSTDOWN ();
  1229. /* Fall through to rest of the decoding. */
  1230. #endif
  1231. if (BITS (4, 7) == 9) {
  1232. /* MLAS */
  1233. rhs = state->Reg[MULRHSReg];
  1234. //if (MULLHSReg == MULDESTReg) {
  1235. if (0) {
  1236. UNDEF_MULDestEQOp1;
  1237. dest = state->Reg[MULACCReg];
  1238. ARMul_NegZero (state, dest);
  1239. state->Reg[MULDESTReg] = dest;
  1240. } else if (MULDESTReg != 15) {
  1241. dest = state->Reg[MULLHSReg] * rhs + state->Reg[MULACCReg];
  1242. ARMul_NegZero (state, dest);
  1243. state->Reg[MULDESTReg] = dest;
  1244. } else
  1245. UNDEF_MULPCDest;
  1246. for (dest = 0, temp = 0; dest < 32;
  1247. dest++)
  1248. if (rhs & (1L << dest))
  1249. temp = dest;
  1250. /* Mult takes this many/2 I cycles. */
  1251. ARMul_Icycles (state, ARMul_MultTable[temp], 0L);
  1252. } else {
  1253. /* EORS Reg. */
  1254. rhs = DPSRegRHS;
  1255. dest = LHS ^ rhs;
  1256. WRITESDEST (dest);
  1257. }
  1258. break;
  1259. case 0x04: /* SUB reg */
  1260. // Signifies UMAAL
  1261. if (state->is_v6 && BITS(4, 7) == 0x09) {
  1262. if (handle_v6_insn(state, instr))
  1263. break;
  1264. }
  1265. #ifdef MODET
  1266. if (BITS (4, 7) == 0xB) {
  1267. /* STRH immediate offset, no write-back, down, post indexed. */
  1268. SHDOWNWB ();
  1269. break;
  1270. }
  1271. if (BITS (4, 7) == 0xD) {
  1272. Handle_Load_Double (state, instr);
  1273. break;
  1274. }
  1275. if (BITS (4, 7) == 0xF) {
  1276. Handle_Store_Double (state, instr);
  1277. break;
  1278. }
  1279. #endif
  1280. rhs = DPRegRHS;
  1281. dest = LHS - rhs;
  1282. WRITEDEST (dest);
  1283. break;
  1284. case 0x05: /* SUBS reg */
  1285. #ifdef MODET
  1286. if ((BITS (4, 7) & 0x9) == 0x9)
  1287. /* LDR immediate offset, no write-back, down, post indexed. */
  1288. LHPOSTDOWN ();
  1289. /* Fall through to the rest of the instruction decoding. */
  1290. #endif
  1291. lhs = LHS;
  1292. rhs = DPRegRHS;
  1293. dest = lhs - rhs;
  1294. if ((lhs >= rhs) || ((rhs | lhs) >> 31)) {
  1295. ARMul_SubCarry (state, lhs, rhs, dest);
  1296. ARMul_SubOverflow (state, lhs, rhs, dest);
  1297. } else {
  1298. CLEARC;
  1299. CLEARV;
  1300. }
  1301. WRITESDEST (dest);
  1302. break;
  1303. case 0x06: /* RSB reg */
  1304. #ifdef MODET
  1305. if (BITS (4, 7) == 0xB) {
  1306. /* STRH immediate offset, write-back, down, post indexed. */
  1307. SHDOWNWB ();
  1308. break;
  1309. }
  1310. #endif
  1311. rhs = DPRegRHS;
  1312. dest = rhs - LHS;
  1313. WRITEDEST (dest);
  1314. break;
  1315. case 0x07: /* RSBS reg */
  1316. #ifdef MODET
  1317. if ((BITS (4, 7) & 0x9) == 0x9)
  1318. /* LDR immediate offset, write-back, down, post indexed. */
  1319. LHPOSTDOWN ();
  1320. /* Fall through to remainder of instruction decoding. */
  1321. #endif
  1322. lhs = LHS;
  1323. rhs = DPRegRHS;
  1324. dest = rhs - lhs;
  1325. if ((rhs >= lhs) || ((rhs | lhs) >> 31)) {
  1326. ARMul_SubCarry (state, rhs, lhs, dest);
  1327. ARMul_SubOverflow (state, rhs, lhs, dest);
  1328. } else {
  1329. CLEARC;
  1330. CLEARV;
  1331. }
  1332. WRITESDEST (dest);
  1333. break;
  1334. case 0x08: /* ADD reg */
  1335. #ifdef MODET
  1336. if (BITS (4, 11) == 0xB) {
  1337. /* STRH register offset, no write-back, up, post indexed. */
  1338. SHUPWB ();
  1339. break;
  1340. }
  1341. if (BITS (4, 7) == 0xD) {
  1342. Handle_Load_Double (state, instr);
  1343. break;
  1344. }
  1345. if (BITS (4, 7) == 0xF) {
  1346. Handle_Store_Double (state, instr);
  1347. break;
  1348. }
  1349. #endif
  1350. #ifdef MODET
  1351. if (BITS (4, 7) == 0x9) {
  1352. /* MULL */
  1353. /* 32x32 = 64 */
  1354. ARMul_Icycles (state, Multiply64 (state, instr, LUNSIGNED, LDEFAULT), 0L);
  1355. break;
  1356. }
  1357. #endif
  1358. rhs = DPRegRHS;
  1359. dest = LHS + rhs;
  1360. WRITEDEST (dest);
  1361. break;
  1362. case 0x09: /* ADDS reg */
  1363. #ifdef MODET
  1364. if ((BITS (4, 11) & 0xF9) == 0x9)
  1365. /* LDR register offset, no write-back, up, post indexed. */
  1366. LHPOSTUP ();
  1367. /* Fall through to remaining instruction decoding. */
  1368. #endif
  1369. #ifdef MODET
  1370. if (BITS (4, 7) == 0x9) {
  1371. /* MULL */
  1372. /* 32x32=64 */
  1373. ARMul_Icycles (state, Multiply64 (state, instr, LUNSIGNED, LSCC), 0L);
  1374. break;
  1375. }
  1376. #endif
  1377. lhs = LHS;
  1378. rhs = DPRegRHS;
  1379. dest = lhs + rhs;
  1380. ASSIGNZ (dest == 0);
  1381. if ((lhs | rhs) >> 30) {
  1382. /* Possible C,V,N to set. */
  1383. ASSIGNN (NEG (dest));
  1384. ARMul_AddCarry (state, lhs, rhs, dest);
  1385. ARMul_AddOverflow (state, lhs, rhs, dest);
  1386. } else {
  1387. CLEARN;
  1388. CLEARC;
  1389. CLEARV;
  1390. }
  1391. WRITESDEST (dest);
  1392. break;
  1393. case 0x0a: /* ADC reg */
  1394. #ifdef MODET
  1395. if (BITS (4, 11) == 0xB) {
  1396. /* STRH register offset, write-back, up, post-indexed. */
  1397. SHUPWB ();
  1398. break;
  1399. }
  1400. if (BITS (4, 7) == 0x9) {
  1401. /* MULL */
  1402. /* 32x32=64 */
  1403. ARMul_Icycles (state, MultiplyAdd64 (state, instr, LUNSIGNED, LDEFAULT), 0L);
  1404. break;
  1405. }
  1406. #endif
  1407. rhs = DPRegRHS;
  1408. dest = LHS + rhs + CFLAG;
  1409. WRITEDEST (dest);
  1410. break;
  1411. case 0x0b: /* ADCS reg */
  1412. #ifdef MODET
  1413. if ((BITS (4, 11) & 0xF9) == 0x9)
  1414. /* LDR register offset, write-back, up, post indexed. */
  1415. LHPOSTUP ();
  1416. /* Fall through to remaining instruction decoding. */
  1417. if (BITS (4, 7) == 0x9) {
  1418. /* MULL */
  1419. /* 32x32=64 */
  1420. ARMul_Icycles (state, MultiplyAdd64 (state, instr, LUNSIGNED, LSCC), 0L);
  1421. break;
  1422. }
  1423. #endif
  1424. lhs = LHS;
  1425. rhs = DPRegRHS;
  1426. dest = lhs + rhs + CFLAG;
  1427. ASSIGNZ (dest == 0);
  1428. if ((lhs | rhs) >> 30) {
  1429. /* Possible C,V,N to set. */
  1430. ASSIGNN (NEG (dest));
  1431. ARMul_AddCarry (state, lhs, rhs, dest);
  1432. ARMul_AddOverflow (state, lhs, rhs, dest);
  1433. } else {
  1434. CLEARN;
  1435. CLEARC;
  1436. CLEARV;
  1437. }
  1438. WRITESDEST (dest);
  1439. break;
  1440. case 0x0c: /* SBC reg */
  1441. #ifdef MODET
  1442. if (BITS (4, 7) == 0xB) {
  1443. /* STRH immediate offset, no write-back, up post indexed. */
  1444. SHUPWB ();
  1445. break;
  1446. }
  1447. if (BITS (4, 7) == 0xD) {
  1448. Handle_Load_Double (state, instr);
  1449. break;
  1450. }
  1451. if (BITS (4, 7) == 0xF) {
  1452. Handle_Store_Double (state, instr);
  1453. break;
  1454. }
  1455. if (BITS (4, 7) == 0x9) {
  1456. /* MULL */
  1457. /* 32x32=64 */
  1458. ARMul_Icycles (state, Multiply64 (state, instr, LSIGNED, LDEFAULT), 0L);
  1459. break;
  1460. }
  1461. #endif
  1462. rhs = DPRegRHS;
  1463. dest = LHS - rhs - !CFLAG;
  1464. WRITEDEST (dest);
  1465. break;
  1466. case 0x0d: /* SBCS reg */
  1467. #ifdef MODET
  1468. if ((BITS (4, 7) & 0x9) == 0x9)
  1469. /* LDR immediate offset, no write-back, up, post indexed. */
  1470. LHPOSTUP ();
  1471. if (BITS (4, 7) == 0x9) {
  1472. /* MULL */
  1473. /* 32x32=64 */
  1474. ARMul_Icycles (state, Multiply64 (state, instr, LSIGNED, LSCC), 0L);
  1475. break;
  1476. }
  1477. #endif
  1478. lhs = LHS;
  1479. rhs = DPRegRHS;
  1480. dest = lhs - rhs - !CFLAG;
  1481. if ((lhs >= rhs) || ((rhs | lhs) >> 31)) {
  1482. ARMul_SubCarry (state, lhs, rhs, dest);
  1483. ARMul_SubOverflow (state, lhs, rhs, dest);
  1484. } else {
  1485. CLEARC;
  1486. CLEARV;
  1487. }
  1488. WRITESDEST (dest);
  1489. break;
  1490. case 0x0e: /* RSC reg */
  1491. #ifdef MODET
  1492. if (BITS (4, 7) == 0xB) {
  1493. /* STRH immediate offset, write-back, up, post indexed. */
  1494. SHUPWB ();
  1495. break;
  1496. }
  1497. if (BITS (4, 7) == 0x9) {
  1498. /* MULL */
  1499. /* 32x32=64 */
  1500. ARMul_Icycles (state, MultiplyAdd64 (state, instr, LSIGNED, LDEFAULT), 0L);
  1501. break;
  1502. }
  1503. #endif
  1504. rhs = DPRegRHS;
  1505. dest = rhs - LHS - !CFLAG;
  1506. WRITEDEST (dest);
  1507. break;
  1508. case 0x0f: /* RSCS reg */
  1509. #ifdef MODET
  1510. if ((BITS (4, 7) & 0x9) == 0x9)
  1511. /* LDR immediate offset, write-back, up, post indexed. */
  1512. LHPOSTUP ();
  1513. /* Fall through to remaining instruction decoding. */
  1514. if (BITS (4, 7) == 0x9) {
  1515. /* MULL */
  1516. /* 32x32=64 */
  1517. ARMul_Icycles (state, MultiplyAdd64 (state, instr, LSIGNED, LSCC), 0L);
  1518. break;
  1519. }
  1520. #endif
  1521. lhs = LHS;
  1522. rhs = DPRegRHS;
  1523. dest = rhs - lhs - !CFLAG;
  1524. if ((rhs >= lhs) || ((rhs | lhs) >> 31)) {
  1525. ARMul_SubCarry (state, rhs, lhs, dest);
  1526. ARMul_SubOverflow (state, rhs, lhs, dest);
  1527. } else {
  1528. CLEARC;
  1529. CLEARV;
  1530. }
  1531. WRITESDEST (dest);
  1532. break;
  1533. case 0x10: /* TST reg and MRS CPSR and SWP word. */
  1534. if (state->is_v5e) {
  1535. if (BIT (4) == 0 && BIT (7) == 1) {
  1536. /* ElSegundo SMLAxy insn. */
  1537. ARMword op1 = state->Reg[BITS (0, 3)];
  1538. ARMword op2 = state->Reg[BITS (8, 11)];
  1539. ARMword Rn = state->Reg[BITS (12, 15)];
  1540. if (BIT (5))
  1541. op1 >>= 16;
  1542. if (BIT (6))
  1543. op2 >>= 16;
  1544. op1 &= 0xFFFF;
  1545. op2 &= 0xFFFF;
  1546. if (op1 & 0x8000)
  1547. op1 -= 65536;
  1548. if (op2 & 0x8000)
  1549. op2 -= 65536;
  1550. op1 *= op2;
  1551. //printf("SMLA_INST:BB,op1=0x%x, op2=0x%x. Rn=0x%x\n", op1, op2, Rn);
  1552. if (AddOverflow(op1, Rn, op1 + Rn))
  1553. SETQ;
  1554. state->Reg[BITS (16, 19)] = op1 + Rn;
  1555. break;
  1556. }
  1557. if (BITS (4, 11) == 5) {
  1558. /* ElSegundo QADD insn. */
  1559. ARMword op1 = state->Reg[BITS (0, 3)];
  1560. ARMword op2 = state->Reg[BITS (16, 19)];
  1561. ARMword result = op1 + op2;
  1562. if (AddOverflow(op1, op2, result)) {
  1563. result = POS (result) ? 0x80000000 : 0x7fffffff;
  1564. SETQ;
  1565. }
  1566. state->Reg[BITS (12, 15)] = result;
  1567. break;
  1568. }
  1569. }
  1570. #ifdef MODET
  1571. if (BITS (4, 11) == 0xB) {
  1572. /* STRH register offset, no write-back, down, pre indexed. */
  1573. SHPREDOWN ();
  1574. break;
  1575. }
  1576. if (BITS (4, 7) == 0xD) {
  1577. Handle_Load_Double (state, instr);
  1578. break;
  1579. }
  1580. if (BITS (4, 7) == 0xF) {
  1581. Handle_Store_Double (state, instr);
  1582. break;
  1583. }
  1584. #endif
  1585. if (BITS (4, 11) == 9) {
  1586. /* SWP */
  1587. UNDEF_SWPPC;
  1588. temp = LHS;
  1589. BUSUSEDINCPCS;
  1590. #ifndef MODE32
  1591. if (VECTORACCESS (temp) || ADDREXCEPT (temp)) {
  1592. INTERNALABORT (temp);
  1593. (void) ARMul_LoadWordN (state, temp);
  1594. (void) ARMul_LoadWordN (state, temp);
  1595. } else
  1596. #endif
  1597. dest = ARMul_SwapWord (state, temp, state->Reg[RHSReg]);
  1598. if (temp & 3)
  1599. DEST = ARMul_Align (state, temp, dest);
  1600. else
  1601. DEST = dest;
  1602. if (state->abortSig || state->Aborted)
  1603. TAKEABORT;
  1604. } else if ((BITS (0, 11) == 0) && (LHSReg == 15)) { /* MRS CPSR */
  1605. UNDEF_MRSPC;
  1606. DEST = ARMul_GetCPSR(state);
  1607. } else {
  1608. UNDEF_Test;
  1609. }
  1610. break;
  1611. case 0x11: /* TSTP reg */
  1612. #ifdef MODET
  1613. if ((BITS (4, 11) & 0xF9) == 0x9)
  1614. /* LDR register offset, no write-back, down, pre indexed. */
  1615. LHPREDOWN ();
  1616. /* Continue with remaining instruction decode. */
  1617. #endif
  1618. if (DESTReg == 15) {
  1619. /* TSTP reg */
  1620. #ifdef MODE32
  1621. //chy 2006-02-15 if in user mode, can not set cpsr 0:23
  1622. //from p165 of ARMARM book
  1623. state->Cpsr = GETSPSR (state->Bank);
  1624. ARMul_CPSRAltered (state);
  1625. #else
  1626. rhs = DPRegRHS;
  1627. temp = LHS & rhs;
  1628. SETR15PSR (temp);
  1629. #endif
  1630. } else {
  1631. /* TST reg */
  1632. rhs = DPSRegRHS;
  1633. dest = LHS & rhs;
  1634. ARMul_NegZero (state, dest);
  1635. }
  1636. break;
  1637. case 0x12: /* TEQ reg and MSR reg to CPSR (ARM6). */
  1638. if (state->is_v5) {
  1639. if (BITS (4, 7) == 3) {
  1640. /* BLX(2) */
  1641. ARMword temp;
  1642. if (TFLAG)
  1643. temp = (pc + 2) | 1;
  1644. else
  1645. temp = pc + 4;
  1646. WriteR15Branch (state, state->Reg[RHSReg]);
  1647. state->Reg[14] = temp;
  1648. break;
  1649. }
  1650. }
  1651. if (state->is_v5e) {
  1652. if (BIT (4) == 0 && BIT (7) == 1 && (BIT (5) == 0 || BITS (12, 15) == 0)) {
  1653. /* ElSegundo SMLAWy/SMULWy insn. */
  1654. unsigned long long op1 = state->Reg[BITS (0, 3)];
  1655. unsigned long long op2 = state->Reg[BITS (8, 11)];
  1656. unsigned long long result;
  1657. if (BIT (6))
  1658. op2 >>= 16;
  1659. if (op1 & 0x80000000)
  1660. op1 -= 1ULL << 32;
  1661. op2 &= 0xFFFF;
  1662. if (op2 & 0x8000)
  1663. op2 -= 65536;
  1664. result = (op1 * op2) >> 16;
  1665. if (BIT (5) == 0) {
  1666. ARMword Rn = state->Reg[BITS(12, 15)];
  1667. if (AddOverflow((ARMword)result, Rn, (ARMword)(result + Rn)))
  1668. SETQ;
  1669. result += Rn;
  1670. }
  1671. state->Reg[BITS (16, 19)] = (ARMword)result;
  1672. break;
  1673. }
  1674. if (BITS (4, 11) == 5) {
  1675. /* ElSegundo QSUB insn. */
  1676. ARMword op1 = state->Reg[BITS (0, 3)];
  1677. ARMword op2 = state->Reg[BITS (16, 19)];
  1678. ARMword result = op1 - op2;
  1679. if (SubOverflow
  1680. (op1, op2, result)) {
  1681. result = POS (result) ? 0x80000000 : 0x7fffffff;
  1682. SETQ;
  1683. }
  1684. state->Reg[BITS (12, 15)] = result;
  1685. break;
  1686. }
  1687. }
  1688. #ifdef MODET
  1689. if (BITS (4, 11) == 0xB) {
  1690. /* STRH register offset, write-back, down, pre indexed. */
  1691. SHPREDOWNWB ();
  1692. break;
  1693. }
  1694. if (BITS (4, 27) == 0x12FFF1) {
  1695. /* BX */
  1696. WriteR15Branch (state, state->Reg[RHSReg]);
  1697. break;
  1698. }
  1699. if (BITS (4, 7) == 0xD) {
  1700. Handle_Load_Double (state, instr);
  1701. break;
  1702. }
  1703. if (BITS (4, 7) == 0xF) {
  1704. Handle_Store_Double (state, instr);
  1705. break;
  1706. }
  1707. #endif
  1708. if (state->is_v5) {
  1709. if (BITS (4, 7) == 0x7) {
  1710. //ARMword value;
  1711. //extern int SWI_vector_installed;
  1712. /* Hardware is allowed to optionally override this
  1713. instruction and treat it as a breakpoint. Since
  1714. this is a simulator not hardware, we take the position
  1715. that if a SWI vector was not installed, then an Abort
  1716. vector was probably not installed either, and so
  1717. normally this instruction would be ignored, even if an
  1718. Abort is generated. This is a bad thing, since GDB
  1719. uses this instruction for its breakpoints (at least in
  1720. Thumb mode it does). So intercept the instruction here
  1721. and generate a breakpoint SWI instead. */
  1722. /* Force the next instruction to be refetched. */
  1723. state->NextInstr = RESUME;
  1724. break;
  1725. }
  1726. }
  1727. if (DESTReg == 15) {
  1728. /* MSR reg to CPSR. */
  1729. UNDEF_MSRPC;
  1730. temp = DPRegRHS;
  1731. #ifdef MODET
  1732. /* Don't allow TBIT to be set by MSR. */
  1733. temp &= ~TBIT;
  1734. #endif
  1735. ARMul_FixCPSR (state, instr, temp);
  1736. } else
  1737. UNDEF_Test;
  1738. break;
  1739. case 0x13: /* TEQP reg */
  1740. #ifdef MODET
  1741. if ((BITS (4, 11) & 0xF9) == 0x9)
  1742. /* LDR register offset, write-back, down, pre indexed. */
  1743. LHPREDOWNWB ();
  1744. /* Continue with remaining instruction decode. */
  1745. #endif
  1746. if (DESTReg == 15) {
  1747. /* TEQP reg */
  1748. #ifdef MODE32
  1749. state->Cpsr = GETSPSR (state->Bank);
  1750. ARMul_CPSRAltered (state);
  1751. #else
  1752. rhs = DPRegRHS;
  1753. temp = LHS ^ rhs;
  1754. SETR15PSR (temp);
  1755. #endif
  1756. } else {
  1757. /* TEQ Reg. */
  1758. rhs = DPSRegRHS;
  1759. dest = LHS ^ rhs;
  1760. ARMul_NegZero (state, dest);
  1761. }
  1762. break;
  1763. case 0x14: /* CMP reg and MRS SPSR and SWP byte. */
  1764. if (state->is_v5e) {
  1765. if (BIT (4) == 0 && BIT (7) == 1) {
  1766. /* ElSegundo SMLALxy insn. */
  1767. unsigned long long op1 = state->Reg[BITS (0, 3)];
  1768. unsigned long long op2 = state->Reg[BITS (8, 11)];
  1769. unsigned long long dest;
  1770. //unsigned long long result;
  1771. if (BIT (5))
  1772. op1 >>= 16;
  1773. if (BIT (6))
  1774. op2 >>= 16;
  1775. op1 &= 0xFFFF;
  1776. if (op1 & 0x8000)
  1777. op1 -= 65536;
  1778. op2 &= 0xFFFF;
  1779. if (op2 & 0x8000)
  1780. op2 -= 65536;
  1781. dest = (unsigned long long) state->Reg[BITS (16, 19)] << 32;
  1782. dest |= state->Reg[BITS (12, 15)];
  1783. dest += op1 * op2;
  1784. state->Reg[BITS(12, 15)] = (ARMword)dest;
  1785. state->Reg[BITS(16, 19)] = (ARMword)(dest >> 32);
  1786. break;
  1787. }
  1788. if (BITS (4, 11) == 5) {
  1789. /* ElSegundo QDADD insn. */
  1790. ARMword op1 = state->Reg[BITS (0, 3)];
  1791. ARMword op2 = state->Reg[BITS (16, 19)];
  1792. ARMword op2d = op2 + op2;
  1793. ARMword result;
  1794. if (AddOverflow
  1795. (op2, op2, op2d)) {
  1796. SETQ;
  1797. op2d = POS (op2d) ? 0x80000000 : 0x7fffffff;
  1798. }
  1799. result = op1 + op2d;
  1800. if (AddOverflow(op1, op2d, result)) {
  1801. SETQ;
  1802. result = POS (result) ? 0x80000000 : 0x7fffffff;
  1803. }
  1804. state->Reg[BITS (12, 15)] = result;
  1805. break;
  1806. }
  1807. }
  1808. #ifdef MODET
  1809. if (BITS (4, 7) == 0xB) {
  1810. /* STRH immediate offset, no write-back, down, pre indexed. */
  1811. SHPREDOWN ();
  1812. break;
  1813. }
  1814. if (BITS (4, 7) == 0xD) {
  1815. Handle_Load_Double (state, instr);
  1816. break;
  1817. }
  1818. if (BITS (4, 7) == 0xF) {
  1819. Handle_Store_Double (state, instr);
  1820. break;
  1821. }
  1822. #endif
  1823. if (BITS (4, 11) == 9) {
  1824. /* SWP */
  1825. UNDEF_SWPPC;
  1826. temp = LHS;
  1827. BUSUSEDINCPCS;
  1828. #ifndef MODE32
  1829. if (VECTORACCESS (temp) || ADDREXCEPT (temp)) {
  1830. INTERNALABORT (temp);
  1831. (void) ARMul_LoadByte (state, temp);
  1832. (void) ARMul_LoadByte (state, temp);
  1833. } else
  1834. #endif
  1835. DEST = ARMul_SwapByte (state, temp, state->Reg[RHSReg]);
  1836. if (state->abortSig || state->Aborted)
  1837. TAKEABORT;
  1838. } else if ((BITS (0, 11) == 0)
  1839. && (LHSReg == 15)) {
  1840. /* MRS SPSR */
  1841. UNDEF_MRSPC;
  1842. DEST = GETSPSR (state->Bank);
  1843. } else
  1844. UNDEF_Test;
  1845. break;
  1846. case 0x15: /* CMPP reg. */
  1847. #ifdef MODET
  1848. if ((BITS (4, 7) & 0x9) == 0x9)
  1849. /* LDR immediate offset, no write-back, down, pre indexed. */
  1850. LHPREDOWN ();
  1851. /* Continue with remaining instruction decode. */
  1852. #endif
  1853. if (DESTReg == 15) {
  1854. /* CMPP reg. */
  1855. #ifdef MODE32
  1856. state->Cpsr = GETSPSR (state->Bank);
  1857. ARMul_CPSRAltered (state);
  1858. #else
  1859. rhs = DPRegRHS;
  1860. temp = LHS - rhs;
  1861. SETR15PSR (temp);
  1862. #endif
  1863. } else {
  1864. /* CMP reg. */
  1865. lhs = LHS;
  1866. rhs = DPRegRHS;
  1867. dest = lhs - rhs;
  1868. ARMul_NegZero (state, dest);
  1869. if ((lhs >= rhs)
  1870. || ((rhs | lhs) >> 31)) {
  1871. ARMul_SubCarry (state, lhs, rhs, dest);
  1872. ARMul_SubOverflow (state, lhs, rhs, dest);
  1873. } else {
  1874. CLEARC;
  1875. CLEARV;
  1876. }
  1877. }
  1878. break;
  1879. case 0x16: /* CMN reg and MSR reg to SPSR */
  1880. if (state->is_v5e) {
  1881. if (BIT (4) == 0 && BIT (7) == 1 && BITS (12, 15) == 0) {
  1882. /* ElSegundo SMULxy insn. */
  1883. ARMword op1 = state->Reg[BITS (0, 3)];
  1884. ARMword op2 = state->Reg[BITS (8, 11)];
  1885. ARMword Rn = state->Reg[BITS (12, 15)];
  1886. if (BIT (5))
  1887. op1 >>= 16;
  1888. if (BIT (6))
  1889. op2 >>= 16;
  1890. op1 &= 0xFFFF;
  1891. op2 &= 0xFFFF;
  1892. if (op1 & 0x8000)
  1893. op1 -= 65536;
  1894. if (op2 & 0x8000)
  1895. op2 -= 65536;
  1896. state->Reg[BITS (16, 19)] = op1 * op2;
  1897. break;
  1898. }
  1899. if (BITS (4, 11) == 5) {
  1900. /* ElSegundo QDSUB insn. */
  1901. ARMword op1 = state->Reg[BITS (0, 3)];
  1902. ARMword op2 = state->Reg[BITS (16, 19)];
  1903. ARMword op2d = op2 + op2;
  1904. ARMword result;
  1905. if (AddOverflow(op2, op2, op2d)) {
  1906. SETQ;
  1907. op2d = POS (op2d) ? 0x80000000 : 0x7fffffff;
  1908. }
  1909. result = op1 - op2d;
  1910. if (SubOverflow(op1, op2d, result)) {
  1911. SETQ;
  1912. result = POS (result) ? 0x80000000 : 0x7fffffff;
  1913. }
  1914. state->Reg[BITS (12, 15)] = result;
  1915. break;
  1916. }
  1917. }
  1918. if (state->is_v5) {
  1919. if (BITS (4, 11) == 0xF1
  1920. && BITS (16, 19) == 0xF) {
  1921. /* ARM5 CLZ insn. */
  1922. ARMword op1 = state->Reg[BITS (0, 3)];
  1923. int result = 32;
  1924. if (op1)
  1925. for (result = 0; (op1 & 0x80000000) == 0; op1 <<= 1)
  1926. result++;
  1927. state->Reg[BITS (12, 15)] = result;
  1928. break;
  1929. }
  1930. }
  1931. #ifdef MODET
  1932. if (BITS (4, 7) == 0xB) {
  1933. /* STRH immediate offset, write-back, down, pre indexed. */
  1934. SHPREDOWNWB ();
  1935. break;
  1936. }
  1937. if (BITS (4, 7) == 0xD) {
  1938. Handle_Load_Double (state, instr);
  1939. break;
  1940. }
  1941. if (BITS (4, 7) == 0xF) {
  1942. Handle_Store_Double (state, instr);
  1943. break;
  1944. }
  1945. #endif
  1946. if (DESTReg == 15) {
  1947. /* MSR */
  1948. UNDEF_MSRPC;
  1949. /*ARMul_FixSPSR (state, instr,
  1950. DPRegRHS);*/
  1951. } else {
  1952. UNDEF_Test;
  1953. }
  1954. break;
  1955. case 0x17: /* CMNP reg */
  1956. #ifdef MODET
  1957. if ((BITS (4, 7) & 0x9) == 0x9)
  1958. /* LDR immediate offset, write-back, down, pre indexed. */
  1959. LHPREDOWNWB ();
  1960. /* Continue with remaining instruction decoding. */
  1961. #endif
  1962. if (DESTReg == 15) {
  1963. #ifdef MODE32
  1964. state->Cpsr = GETSPSR (state->Bank);
  1965. ARMul_CPSRAltered (state);
  1966. #else
  1967. rhs = DPRegRHS;
  1968. temp = LHS + rhs;
  1969. SETR15PSR (temp);
  1970. #endif
  1971. break;
  1972. } else {
  1973. /* CMN reg. */
  1974. lhs = LHS;
  1975. rhs = DPRegRHS;
  1976. dest = lhs + rhs;
  1977. ASSIGNZ (dest == 0);
  1978. if ((lhs | rhs) >> 30) {
  1979. /* Possible C,V,N to set. */
  1980. ASSIGNN (NEG (dest));
  1981. ARMul_AddCarry (state, lhs, rhs, dest);
  1982. ARMul_AddOverflow (state, lhs, rhs, dest);
  1983. } else {
  1984. CLEARN;
  1985. CLEARC;
  1986. CLEARV;
  1987. }
  1988. }
  1989. break;
  1990. case 0x18: /* ORR reg */
  1991. #ifdef MODET
  1992. /* dyf add armv6 instr strex 2010.9.17 */
  1993. if (state->is_v6) {
  1994. if (BITS (4, 7) == 0x9)
  1995. if (handle_v6_insn (state, instr))
  1996. break;
  1997. }
  1998. if (BITS (4, 11) == 0xB) {
  1999. /* STRH register offset, no write-back, up, pre indexed. */
  2000. SHPREUP ();
  2001. break;
  2002. }
  2003. if (BITS (4, 7) == 0xD) {
  2004. Handle_Load_Double (state, instr);
  2005. break;
  2006. }
  2007. if (BITS (4, 7) == 0xF) {
  2008. Handle_Store_Double (state, instr);
  2009. break;
  2010. }
  2011. #endif
  2012. rhs = DPRegRHS;
  2013. dest = LHS | rhs;
  2014. WRITEDEST (dest);
  2015. break;
  2016. case 0x19: /* ORRS reg */
  2017. #ifdef MODET
  2018. /* dyf add armv6 instr ldrex */
  2019. if (state->is_v6) {
  2020. if (BITS (4, 7) == 0x9) {
  2021. if (handle_v6_insn (state, instr))
  2022. break;
  2023. }
  2024. }
  2025. if ((BITS (4, 11) & 0xF9) == 0x9)
  2026. /* LDR register offset, no write-back, up, pre indexed. */
  2027. LHPREUP ();
  2028. /* Continue with remaining instruction decoding. */
  2029. #endif
  2030. rhs = DPSRegRHS;
  2031. dest = LHS | rhs;
  2032. WRITESDEST (dest);
  2033. break;
  2034. case 0x1a: /* MOV reg */
  2035. #ifdef MODET
  2036. if (BITS (4, 11) == 0xB) {
  2037. /* STRH register offset, write-back, up, pre indexed. */
  2038. SHPREUPWB ();
  2039. break;
  2040. }
  2041. if (BITS (4, 7) == 0xD) {
  2042. Handle_Load_Double (state, instr);
  2043. break;
  2044. }
  2045. if (BITS (4, 7) == 0xF) {
  2046. Handle_Store_Double (state, instr);
  2047. break;
  2048. }
  2049. if (BITS(4, 11) == 0xF9) { //strexd
  2050. u32 l = LHSReg;
  2051. bool enter = false;
  2052. if (state->currentexval == (u32)ARMul_ReadWord(state, state->currentexaddr)&&
  2053. state->currentexvald == (u32)ARMul_ReadWord(state, state->currentexaddr + 4))
  2054. enter = true;
  2055. //todo bug this and STREXD and LDREXD http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0360e/CHDGJGGC.html
  2056. if (enter) {
  2057. ARMul_StoreWordN(state, LHS, state->Reg[RHSReg]);
  2058. ARMul_StoreWordN(state,LHS + 4 , state->Reg[RHSReg + 1]);
  2059. state->Reg[DESTReg] = 0;
  2060. } else {
  2061. state->Reg[DESTReg] = 1;
  2062. }
  2063. break;
  2064. }
  2065. #endif
  2066. dest = DPRegRHS;
  2067. WRITEDEST (dest);
  2068. break;
  2069. case 0x1B: /* MOVS reg */
  2070. #ifdef MODET
  2071. /* ldrexd ichfly */
  2072. if (BITS(0, 11) == 0xF9F) { //strexd
  2073. lhs = LHS;
  2074. state->currentexaddr = lhs;
  2075. state->currentexval = (u32)ARMul_ReadWord(state, lhs);
  2076. state->currentexvald = (u32)ARMul_ReadWord(state, lhs + 4);
  2077. state->Reg[DESTReg] = ARMul_LoadWordN(state, lhs);
  2078. state->Reg[DESTReg] = ARMul_LoadWordN(state, lhs + 4);
  2079. break;
  2080. }
  2081. if ((BITS (4, 11) & 0xF9) == 0x9)
  2082. /* LDR register offset, write-back, up, pre indexed. */
  2083. LHPREUPWB ();
  2084. /* Continue with remaining instruction decoding. */
  2085. #endif
  2086. dest = DPSRegRHS;
  2087. WRITESDEST (dest);
  2088. break;
  2089. case 0x1c: /* BIC reg */
  2090. #ifdef MODET
  2091. /* dyf add for STREXB */
  2092. if (state->is_v6) {
  2093. if (BITS (4, 7) == 0x9) {
  2094. if (handle_v6_insn (state, instr))
  2095. break;
  2096. }
  2097. }
  2098. if (BITS (4, 7) == 0xB) {
  2099. /* STRH immediate offset, no write-back, up, pre indexed. */
  2100. SHPREUP ();
  2101. break;
  2102. }
  2103. if (BITS (4, 7) == 0xD) {
  2104. Handle_Load_Double (state, instr);
  2105. break;
  2106. } else if (BITS (4, 7) == 0xF) {
  2107. Handle_Store_Double (state, instr);
  2108. break;
  2109. }
  2110. #endif
  2111. rhs = DPRegRHS;
  2112. dest = LHS & ~rhs;
  2113. WRITEDEST (dest);
  2114. break;
  2115. case 0x1d: /* BICS reg */
  2116. #ifdef MODET
  2117. /* ladsh P=1 U=1 W=0 L=1 S=1 H=1 */
  2118. if (BITS(4, 7) == 0xF) {
  2119. temp = LHS + GetLS7RHS (state, instr);
  2120. LoadHalfWord (state, instr, temp, LSIGNED);
  2121. break;
  2122. }
  2123. if (BITS (4, 7) == 0xb) {
  2124. /* LDRH immediate offset, no write-back, up, pre indexed. */
  2125. temp = LHS + GetLS7RHS (state, instr);
  2126. LoadHalfWord (state, instr, temp, LUNSIGNED);
  2127. break;
  2128. }
  2129. if (BITS (4, 7) == 0xd) {
  2130. // alex-ykl fix: 2011-07-20 missing ldrsb instruction
  2131. temp = LHS + GetLS7RHS (state, instr);
  2132. LoadByte (state, instr, temp, LSIGNED);
  2133. break;
  2134. }
  2135. /* Continue with instruction decoding. */
  2136. /*if ((BITS (4, 7) & 0x9) == 0x9) */
  2137. if ((BITS (4, 7)) == 0x9) {
  2138. /* ldrexb */
  2139. if (state->is_v6) {
  2140. if (handle_v6_insn (state, instr))
  2141. break;
  2142. }
  2143. /* LDR immediate offset, no write-back, up, pre indexed. */
  2144. LHPREUP ();
  2145. }
  2146. #endif
  2147. rhs = DPSRegRHS;
  2148. dest = LHS & ~rhs;
  2149. WRITESDEST (dest);
  2150. break;
  2151. case 0x1e: /* MVN reg */
  2152. #ifdef MODET
  2153. if ((instr & 0x00000FF0) == 0x00000F90) { //if ((instr & 0x0FF00FF0) == 0x01e00f90) { //todo make that better ichfly
  2154. /* strexh ichfly */
  2155. u32 l = LHSReg;
  2156. u32 r = RHSReg;
  2157. lhs = LHS;
  2158. bool enter = false;
  2159. if (state->currentexval == (u32)ARMul_LoadHalfWord(state, state->currentexaddr))enter = true;
  2160. //StoreWord(state, lhs, RHS)
  2161. if (state->Aborted) {
  2162. TAKEABORT;
  2163. }
  2164. if (enter) {
  2165. ARMul_StoreHalfWord(state, lhs, RHS);
  2166. state->Reg[DESTReg] = 0;
  2167. } else {
  2168. state->Reg[DESTReg] = 1;
  2169. }
  2170. break;
  2171. }
  2172. if (BITS (4, 7) == 0xB) {
  2173. /* STRH immediate offset, write-back, up, pre indexed. */
  2174. SHPREUPWB ();
  2175. break;
  2176. }
  2177. if (BITS (4, 7) == 0xD) {
  2178. Handle_Load_Double (state, instr);
  2179. break;
  2180. }
  2181. if (BITS (4, 7) == 0xF) {
  2182. Handle_Store_Double (state, instr);
  2183. break;
  2184. }
  2185. #endif
  2186. dest = ~DPRegRHS;
  2187. WRITEDEST (dest);
  2188. break;
  2189. case 0x1f: /* MVNS reg */
  2190. #ifdef MODET
  2191. if ((instr & 0x00000FF0) == 0x00000F90) { //(instr & 0x0FF00FF0) == 0x01f00f90)//if ((instr & 0x0FF00FF0) == 0x01f00f90) {
  2192. /* ldrexh ichfly */
  2193. lhs = LHS;
  2194. state->currentexaddr = lhs;
  2195. state->currentexval = (u32)ARMul_LoadHalfWord(state, lhs);
  2196. LoadHalfWord(state, instr, lhs,0);
  2197. break;
  2198. }
  2199. if ((BITS (4, 7) & 0x9) == 0x9)
  2200. /* LDR immediate offset, write-back, up, pre indexed. */
  2201. LHPREUPWB ();
  2202. /* Continue instruction decoding. */
  2203. #endif
  2204. dest = ~DPSRegRHS;
  2205. WRITESDEST (dest);
  2206. break;
  2207. /* Data Processing Immediate RHS Instructions. */
  2208. case 0x20: /* AND immed */
  2209. dest = LHS & DPImmRHS;
  2210. WRITEDEST (dest);
  2211. break;
  2212. case 0x21: /* ANDS immed */
  2213. DPSImmRHS;
  2214. dest = LHS & rhs;
  2215. WRITESDEST (dest);
  2216. break;
  2217. case 0x22: /* EOR immed */
  2218. dest = LHS ^ DPImmRHS;
  2219. WRITEDEST (dest);
  2220. break;
  2221. case 0x23: /* EORS immed */
  2222. DPSImmRHS;
  2223. dest = LHS ^ rhs;
  2224. WRITESDEST (dest);
  2225. break;
  2226. case 0x24: /* SUB immed */
  2227. dest = LHS - DPImmRHS;
  2228. WRITEDEST (dest);
  2229. break;
  2230. case 0x25: /* SUBS immed */
  2231. lhs = LHS;
  2232. rhs = DPImmRHS;
  2233. dest = lhs - rhs;
  2234. if ((lhs >= rhs) || ((rhs | lhs) >> 31)) {
  2235. ARMul_SubCarry (state, lhs, rhs, dest);
  2236. ARMul_SubOverflow (state, lhs, rhs, dest);
  2237. } else {
  2238. CLEARC;
  2239. CLEARV;
  2240. }
  2241. WRITESDEST (dest);
  2242. break;
  2243. case 0x26: /* RSB immed */
  2244. dest = DPImmRHS - LHS;
  2245. WRITEDEST (dest);
  2246. break;
  2247. case 0x27: /* RSBS immed */
  2248. lhs = LHS;
  2249. rhs = DPImmRHS;
  2250. dest = rhs - lhs;
  2251. if ((rhs >= lhs) || ((rhs | lhs) >> 31)) {
  2252. ARMul_SubCarry (state, rhs, lhs, dest);
  2253. ARMul_SubOverflow (state, rhs, lhs, dest);
  2254. } else {
  2255. CLEARC;
  2256. CLEARV;
  2257. }
  2258. WRITESDEST (dest);
  2259. break;
  2260. case 0x28: /* ADD immed */
  2261. dest = LHS + DPImmRHS;
  2262. WRITEDEST (dest);
  2263. break;
  2264. case 0x29: /* ADDS immed */
  2265. lhs = LHS;
  2266. rhs = DPImmRHS;
  2267. dest = lhs + rhs;
  2268. ASSIGNZ (dest == 0);
  2269. if ((lhs | rhs) >> 30) {
  2270. /* Possible C,V,N to set. */
  2271. ASSIGNN (NEG (dest));
  2272. ARMul_AddCarry (state, lhs, rhs, dest);
  2273. ARMul_AddOverflow (state, lhs, rhs, dest);
  2274. } else {
  2275. CLEARN;
  2276. CLEARC;
  2277. CLEARV;
  2278. }
  2279. WRITESDEST (dest);
  2280. break;
  2281. case 0x2a: /* ADC immed */
  2282. dest = LHS + DPImmRHS + CFLAG;
  2283. WRITEDEST (dest);
  2284. break;
  2285. case 0x2b: /* ADCS immed */
  2286. lhs = LHS;
  2287. rhs = DPImmRHS;
  2288. dest = lhs + rhs + CFLAG;
  2289. ASSIGNZ (dest == 0);
  2290. if ((lhs | rhs) >> 30) {
  2291. /* Possible C,V,N to set. */
  2292. ASSIGNN (NEG (dest));
  2293. ARMul_AddCarry (state, lhs, rhs, dest);
  2294. ARMul_AddOverflow (state, lhs, rhs, dest);
  2295. } else {
  2296. CLEARN;
  2297. CLEARC;
  2298. CLEARV;
  2299. }
  2300. WRITESDEST (dest);
  2301. break;
  2302. case 0x2c: /* SBC immed */
  2303. dest = LHS - DPImmRHS - !CFLAG;
  2304. WRITEDEST (dest);
  2305. break;
  2306. case 0x2d: /* SBCS immed */
  2307. lhs = LHS;
  2308. rhs = DPImmRHS;
  2309. dest = lhs - rhs - !CFLAG;
  2310. if ((lhs >= rhs) || ((rhs | lhs) >> 31)) {
  2311. ARMul_SubCarry (state, lhs, rhs, dest);
  2312. ARMul_SubOverflow (state, lhs, rhs, dest);
  2313. } else {
  2314. CLEARC;
  2315. CLEARV;
  2316. }
  2317. WRITESDEST (dest);
  2318. break;
  2319. case 0x2e: /* RSC immed */
  2320. dest = DPImmRHS - LHS - !CFLAG;
  2321. WRITEDEST (dest);
  2322. break;
  2323. case 0x2f: /* RSCS immed */
  2324. lhs = LHS;
  2325. rhs = DPImmRHS;
  2326. dest = rhs - lhs - !CFLAG;
  2327. if ((rhs >= lhs) || ((rhs | lhs) >> 31)) {
  2328. ARMul_SubCarry (state, rhs, lhs, dest);
  2329. ARMul_SubOverflow (state, rhs, lhs, dest);
  2330. } else {
  2331. CLEARC;
  2332. CLEARV;
  2333. }
  2334. WRITESDEST (dest);
  2335. break;
  2336. case 0x30: /* TST immed */
  2337. /* shenoubang 2012-3-14*/
  2338. if (state->is_v6) { /* movw, ARMV6, ARMv7 */
  2339. dest ^= dest;
  2340. dest = BITS(16, 19);
  2341. dest = ((dest<<12) | BITS(0, 11));
  2342. WRITEDEST(dest);
  2343. break;
  2344. } else {
  2345. UNDEF_Test;
  2346. break;
  2347. }
  2348. case 0x31: /* TSTP immed */
  2349. if (DESTReg == 15) {
  2350. /* TSTP immed. */
  2351. #ifdef MODE32
  2352. state->Cpsr = GETSPSR (state->Bank);
  2353. ARMul_CPSRAltered (state);
  2354. #else
  2355. temp = LHS & DPImmRHS;
  2356. SETR15PSR (temp);
  2357. #endif
  2358. } else {
  2359. /* TST immed. */
  2360. DPSImmRHS;
  2361. dest = LHS & rhs;
  2362. ARMul_NegZero (state, dest);
  2363. }
  2364. break;
  2365. case 0x32: /* TEQ immed and MSR immed to CPSR */
  2366. if (DESTReg == 15)
  2367. /* MSR immed to CPSR. */
  2368. ARMul_FixCPSR (state, instr,
  2369. DPImmRHS);
  2370. else
  2371. UNDEF_Test;
  2372. break;
  2373. case 0x33: /* TEQP immed */
  2374. if (DESTReg == 15) {
  2375. /* TEQP immed. */
  2376. #ifdef MODE32
  2377. state->Cpsr = GETSPSR (state->Bank);
  2378. ARMul_CPSRAltered (state);
  2379. #else
  2380. temp = LHS ^ DPImmRHS;
  2381. SETR15PSR (temp);
  2382. #endif
  2383. } else {
  2384. DPSImmRHS; /* TEQ immed */
  2385. dest = LHS ^ rhs;
  2386. ARMul_NegZero (state, dest);
  2387. }
  2388. break;
  2389. case 0x34: /* CMP immed */
  2390. UNDEF_Test;
  2391. break;
  2392. case 0x35: /* CMPP immed */
  2393. if (DESTReg == 15) {
  2394. /* CMPP immed. */
  2395. #ifdef MODE32
  2396. state->Cpsr = GETSPSR (state->Bank);
  2397. ARMul_CPSRAltered (state);
  2398. #else
  2399. temp = LHS - DPImmRHS;
  2400. SETR15PSR (temp);
  2401. #endif
  2402. break;
  2403. } else {
  2404. /* CMP immed. */
  2405. lhs = LHS;
  2406. rhs = DPImmRHS;
  2407. dest = lhs - rhs;
  2408. ARMul_NegZero (state, dest);
  2409. if ((lhs >= rhs) || ((rhs | lhs) >> 31)) {
  2410. ARMul_SubCarry (state, lhs, rhs, dest);
  2411. ARMul_SubOverflow (state, lhs, rhs, dest);
  2412. } else {
  2413. CLEARC;
  2414. CLEARV;
  2415. }
  2416. }
  2417. break;
  2418. case 0x36: /* CMN immed and MSR immed to SPSR */
  2419. //if (DESTReg == 15)
  2420. /*ARMul0_FixSPSR (state, instr,
  2421. DPImmRHS);*/
  2422. //else
  2423. UNDEF_Test;
  2424. break;
  2425. case 0x37: /* CMNP immed. */
  2426. if (DESTReg == 15) {
  2427. /* CMNP immed. */
  2428. #ifdef MODE32
  2429. state->Cpsr = GETSPSR (state->Bank);
  2430. ARMul_CPSRAltered (state);
  2431. #else
  2432. temp = LHS + DPImmRHS;
  2433. SETR15PSR (temp);
  2434. #endif
  2435. break;
  2436. } else {
  2437. /* CMN immed. */
  2438. lhs = LHS;
  2439. rhs = DPImmRHS;
  2440. dest = lhs + rhs;
  2441. ASSIGNZ (dest == 0);
  2442. if ((lhs | rhs) >> 30) {
  2443. /* Possible C,V,N to set. */
  2444. ASSIGNN (NEG (dest));
  2445. ARMul_AddCarry (state, lhs, rhs, dest);
  2446. ARMul_AddOverflow (state, lhs, rhs, dest);
  2447. } else {
  2448. CLEARN;
  2449. CLEARC;
  2450. CLEARV;
  2451. }
  2452. }
  2453. break;
  2454. case 0x38: /* ORR immed. */
  2455. dest = LHS | DPImmRHS;
  2456. WRITEDEST (dest);
  2457. break;
  2458. case 0x39: /* ORRS immed. */
  2459. DPSImmRHS;
  2460. dest = LHS | rhs;
  2461. WRITESDEST (dest);
  2462. break;
  2463. case 0x3a: /* MOV immed. */
  2464. dest = DPImmRHS;
  2465. WRITEDEST (dest);
  2466. break;
  2467. case 0x3b: /* MOVS immed. */
  2468. DPSImmRHS;
  2469. WRITESDEST (rhs);
  2470. break;
  2471. case 0x3c: /* BIC immed. */
  2472. dest = LHS & ~DPImmRHS;
  2473. WRITEDEST (dest);
  2474. break;
  2475. case 0x3d: /* BICS immed. */
  2476. DPSImmRHS;
  2477. dest = LHS & ~rhs;
  2478. WRITESDEST (dest);
  2479. break;
  2480. case 0x3e: /* MVN immed. */
  2481. dest = ~DPImmRHS;
  2482. WRITEDEST (dest);
  2483. break;
  2484. case 0x3f: /* MVNS immed. */
  2485. DPSImmRHS;
  2486. WRITESDEST (~rhs);
  2487. break;
  2488. /* Single Data Transfer Immediate RHS Instructions. */
  2489. case 0x40: /* Store Word, No WriteBack, Post Dec, Immed. */
  2490. lhs = LHS;
  2491. if (StoreWord (state, instr, lhs))
  2492. LSBase = lhs - LSImmRHS;
  2493. break;
  2494. case 0x41: /* Load Word, No WriteBack, Post Dec, Immed. */
  2495. lhs = LHS;
  2496. if (LoadWord (state, instr, lhs))
  2497. LSBase = lhs - LSImmRHS;
  2498. break;
  2499. case 0x42: /* Store Word, WriteBack, Post Dec, Immed. */
  2500. UNDEF_LSRBaseEQDestWb;
  2501. UNDEF_LSRPCBaseWb;
  2502. lhs = LHS;
  2503. temp = lhs - LSImmRHS;
  2504. state->NtransSig = LOW;
  2505. if (StoreWord (state, instr, lhs))
  2506. LSBase = temp;
  2507. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2508. break;
  2509. case 0x43: /* Load Word, WriteBack, Post Dec, Immed. */
  2510. UNDEF_LSRBaseEQDestWb;
  2511. UNDEF_LSRPCBaseWb;
  2512. lhs = LHS;
  2513. state->NtransSig = LOW;
  2514. if (LoadWord (state, instr, lhs))
  2515. LSBase = lhs - LSImmRHS;
  2516. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2517. break;
  2518. case 0x44: /* Store Byte, No WriteBack, Post Dec, Immed. */
  2519. lhs = LHS;
  2520. if (StoreByte (state, instr, lhs))
  2521. LSBase = lhs - LSImmRHS;
  2522. break;
  2523. case 0x45: /* Load Byte, No WriteBack, Post Dec, Immed. */
  2524. lhs = LHS;
  2525. if (LoadByte (state, instr, lhs, LUNSIGNED))
  2526. LSBase = lhs - LSImmRHS;
  2527. break;
  2528. case 0x46: /* Store Byte, WriteBack, Post Dec, Immed. */
  2529. UNDEF_LSRBaseEQDestWb;
  2530. UNDEF_LSRPCBaseWb;
  2531. lhs = LHS;
  2532. state->NtransSig = LOW;
  2533. if (StoreByte (state, instr, lhs))
  2534. LSBase = lhs - LSImmRHS;
  2535. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2536. break;
  2537. case 0x47: /* Load Byte, WriteBack, Post Dec, Immed. */
  2538. UNDEF_LSRBaseEQDestWb;
  2539. UNDEF_LSRPCBaseWb;
  2540. lhs = LHS;
  2541. state->NtransSig = LOW;
  2542. if (LoadByte (state, instr, lhs, LUNSIGNED))
  2543. LSBase = lhs - LSImmRHS;
  2544. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2545. break;
  2546. case 0x48: /* Store Word, No WriteBack, Post Inc, Immed. */
  2547. lhs = LHS;
  2548. if (StoreWord (state, instr, lhs))
  2549. LSBase = lhs + LSImmRHS;
  2550. break;
  2551. case 0x49: /* Load Word, No WriteBack, Post Inc, Immed. */
  2552. lhs = LHS;
  2553. if (LoadWord (state, instr, lhs))
  2554. LSBase = lhs + LSImmRHS;
  2555. break;
  2556. case 0x4a: /* Store Word, WriteBack, Post Inc, Immed. */
  2557. UNDEF_LSRBaseEQDestWb;
  2558. UNDEF_LSRPCBaseWb;
  2559. lhs = LHS;
  2560. state->NtransSig = LOW;
  2561. if (StoreWord (state, instr, lhs))
  2562. LSBase = lhs + LSImmRHS;
  2563. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2564. break;
  2565. case 0x4b: /* Load Word, WriteBack, Post Inc, Immed. */
  2566. UNDEF_LSRBaseEQDestWb;
  2567. UNDEF_LSRPCBaseWb;
  2568. lhs = LHS;
  2569. state->NtransSig = LOW;
  2570. if (LoadWord (state, instr, lhs))
  2571. LSBase = lhs + LSImmRHS;
  2572. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2573. break;
  2574. case 0x4c: /* Store Byte, No WriteBack, Post Inc, Immed. */
  2575. lhs = LHS;
  2576. if (StoreByte (state, instr, lhs))
  2577. LSBase = lhs + LSImmRHS;
  2578. break;
  2579. case 0x4d: /* Load Byte, No WriteBack, Post Inc, Immed. */
  2580. lhs = LHS;
  2581. if (LoadByte (state, instr, lhs, LUNSIGNED))
  2582. LSBase = lhs + LSImmRHS;
  2583. break;
  2584. case 0x4e: /* Store Byte, WriteBack, Post Inc, Immed. */
  2585. UNDEF_LSRBaseEQDestWb;
  2586. UNDEF_LSRPCBaseWb;
  2587. lhs = LHS;
  2588. state->NtransSig = LOW;
  2589. if (StoreByte (state, instr, lhs))
  2590. LSBase = lhs + LSImmRHS;
  2591. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2592. break;
  2593. case 0x4f: /* Load Byte, WriteBack, Post Inc, Immed. */
  2594. UNDEF_LSRBaseEQDestWb;
  2595. UNDEF_LSRPCBaseWb;
  2596. lhs = LHS;
  2597. state->NtransSig = LOW;
  2598. if (LoadByte (state, instr, lhs, LUNSIGNED))
  2599. LSBase = lhs + LSImmRHS;
  2600. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2601. break;
  2602. case 0x50: /* Store Word, No WriteBack, Pre Dec, Immed. */
  2603. (void) StoreWord (state, instr, LHS - LSImmRHS);
  2604. break;
  2605. case 0x51: /* Load Word, No WriteBack, Pre Dec, Immed. */
  2606. (void) LoadWord (state, instr, LHS - LSImmRHS);
  2607. break;
  2608. case 0x52: /* Store Word, WriteBack, Pre Dec, Immed. */
  2609. UNDEF_LSRBaseEQDestWb;
  2610. UNDEF_LSRPCBaseWb;
  2611. temp = LHS - LSImmRHS;
  2612. if (StoreWord (state, instr, temp))
  2613. LSBase = temp;
  2614. break;
  2615. case 0x53: /* Load Word, WriteBack, Pre Dec, Immed. */
  2616. UNDEF_LSRBaseEQDestWb;
  2617. UNDEF_LSRPCBaseWb;
  2618. temp = LHS - LSImmRHS;
  2619. if (LoadWord (state, instr, temp))
  2620. LSBase = temp;
  2621. break;
  2622. case 0x54: /* Store Byte, No WriteBack, Pre Dec, Immed. */
  2623. (void) StoreByte (state, instr, LHS - LSImmRHS);
  2624. break;
  2625. case 0x55: /* Load Byte, No WriteBack, Pre Dec, Immed. */
  2626. (void) LoadByte (state, instr, LHS - LSImmRHS, LUNSIGNED);
  2627. break;
  2628. case 0x56: /* Store Byte, WriteBack, Pre Dec, Immed. */
  2629. UNDEF_LSRBaseEQDestWb;
  2630. UNDEF_LSRPCBaseWb;
  2631. temp = LHS - LSImmRHS;
  2632. if (StoreByte (state, instr, temp))
  2633. LSBase = temp;
  2634. break;
  2635. case 0x57: /* Load Byte, WriteBack, Pre Dec, Immed. */
  2636. UNDEF_LSRBaseEQDestWb;
  2637. UNDEF_LSRPCBaseWb;
  2638. temp = LHS - LSImmRHS;
  2639. if (LoadByte (state, instr, temp, LUNSIGNED))
  2640. LSBase = temp;
  2641. break;
  2642. case 0x58: /* Store Word, No WriteBack, Pre Inc, Immed. */
  2643. (void) StoreWord (state, instr, LHS + LSImmRHS);
  2644. break;
  2645. case 0x59: /* Load Word, No WriteBack, Pre Inc, Immed. */
  2646. (void) LoadWord (state, instr, LHS + LSImmRHS);
  2647. break;
  2648. case 0x5a: /* Store Word, WriteBack, Pre Inc, Immed. */
  2649. UNDEF_LSRBaseEQDestWb;
  2650. UNDEF_LSRPCBaseWb;
  2651. temp = LHS + LSImmRHS;
  2652. if (StoreWord (state, instr, temp))
  2653. LSBase = temp;
  2654. break;
  2655. case 0x5b: /* Load Word, WriteBack, Pre Inc, Immed. */
  2656. UNDEF_LSRBaseEQDestWb;
  2657. UNDEF_LSRPCBaseWb;
  2658. temp = LHS + LSImmRHS;
  2659. if (LoadWord (state, instr, temp))
  2660. LSBase = temp;
  2661. break;
  2662. case 0x5c: /* Store Byte, No WriteBack, Pre Inc, Immed. */
  2663. (void) StoreByte (state, instr, LHS + LSImmRHS);
  2664. break;
  2665. case 0x5d: /* Load Byte, No WriteBack, Pre Inc, Immed. */
  2666. (void) LoadByte (state, instr, LHS + LSImmRHS, LUNSIGNED);
  2667. break;
  2668. case 0x5e: /* Store Byte, WriteBack, Pre Inc, Immed. */
  2669. UNDEF_LSRBaseEQDestWb;
  2670. UNDEF_LSRPCBaseWb;
  2671. temp = LHS + LSImmRHS;
  2672. if (StoreByte (state, instr, temp))
  2673. LSBase = temp;
  2674. break;
  2675. case 0x5f: /* Load Byte, WriteBack, Pre Inc, Immed. */
  2676. UNDEF_LSRBaseEQDestWb;
  2677. UNDEF_LSRPCBaseWb;
  2678. temp = LHS + LSImmRHS;
  2679. if (LoadByte (state, instr, temp, LUNSIGNED))
  2680. LSBase = temp;
  2681. break;
  2682. /* Single Data Transfer Register RHS Instructions. */
  2683. case 0x60: /* Store Word, No WriteBack, Post Dec, Reg. */
  2684. if (BIT (4)) {
  2685. ARMul_UndefInstr (state, instr);
  2686. break;
  2687. }
  2688. UNDEF_LSRBaseEQOffWb;
  2689. UNDEF_LSRBaseEQDestWb;
  2690. UNDEF_LSRPCBaseWb;
  2691. UNDEF_LSRPCOffWb;
  2692. lhs = LHS;
  2693. if (StoreWord (state, instr, lhs))
  2694. LSBase = lhs - LSRegRHS;
  2695. break;
  2696. case 0x61: /* Load Word, No WriteBack, Post Dec, Reg. */
  2697. if (BIT (4)) {
  2698. #ifdef MODE32
  2699. if (state->is_v6 && handle_v6_insn (state, instr))
  2700. break;
  2701. #endif
  2702. ARMul_UndefInstr (state, instr);
  2703. break;
  2704. }
  2705. UNDEF_LSRBaseEQOffWb;
  2706. UNDEF_LSRBaseEQDestWb;
  2707. UNDEF_LSRPCBaseWb;
  2708. UNDEF_LSRPCOffWb;
  2709. lhs = LHS;
  2710. temp = lhs - LSRegRHS;
  2711. if (LoadWord (state, instr, lhs))
  2712. LSBase = temp;
  2713. break;
  2714. case 0x62: /* Store Word, WriteBack, Post Dec, Reg. */
  2715. if (BIT (4)) {
  2716. #ifdef MODE32
  2717. if (state->is_v6 && handle_v6_insn (state, instr))
  2718. break;
  2719. #endif
  2720. ARMul_UndefInstr (state, instr);
  2721. break;
  2722. }
  2723. UNDEF_LSRBaseEQOffWb;
  2724. UNDEF_LSRBaseEQDestWb;
  2725. UNDEF_LSRPCBaseWb;
  2726. UNDEF_LSRPCOffWb;
  2727. lhs = LHS;
  2728. state->NtransSig = LOW;
  2729. if (StoreWord (state, instr, lhs))
  2730. LSBase = lhs - LSRegRHS;
  2731. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2732. break;
  2733. case 0x63: /* Load Word, WriteBack, Post Dec, Reg. */
  2734. if (BIT (4)) {
  2735. #ifdef MODE32
  2736. if (state->is_v6 && handle_v6_insn (state, instr))
  2737. break;
  2738. #endif
  2739. ARMul_UndefInstr (state, instr);
  2740. break;
  2741. }
  2742. UNDEF_LSRBaseEQOffWb;
  2743. UNDEF_LSRBaseEQDestWb;
  2744. UNDEF_LSRPCBaseWb;
  2745. UNDEF_LSRPCOffWb;
  2746. lhs = LHS;
  2747. temp = lhs - LSRegRHS;
  2748. state->NtransSig = LOW;
  2749. if (LoadWord (state, instr, lhs))
  2750. LSBase = temp;
  2751. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2752. break;
  2753. case 0x64: /* Store Byte, No WriteBack, Post Dec, Reg. */
  2754. if (BIT (4)) {
  2755. ARMul_UndefInstr (state, instr);
  2756. break;
  2757. }
  2758. UNDEF_LSRBaseEQOffWb;
  2759. UNDEF_LSRBaseEQDestWb;
  2760. UNDEF_LSRPCBaseWb;
  2761. UNDEF_LSRPCOffWb;
  2762. lhs = LHS;
  2763. if (StoreByte (state, instr, lhs))
  2764. LSBase = lhs - LSRegRHS;
  2765. break;
  2766. case 0x65: /* Load Byte, No WriteBack, Post Dec, Reg. */
  2767. if (BIT (4)) {
  2768. #ifdef MODE32
  2769. if (state->is_v6 && handle_v6_insn (state, instr))
  2770. break;
  2771. #endif
  2772. ARMul_UndefInstr (state, instr);
  2773. break;
  2774. }
  2775. UNDEF_LSRBaseEQOffWb;
  2776. UNDEF_LSRBaseEQDestWb;
  2777. UNDEF_LSRPCBaseWb;
  2778. UNDEF_LSRPCOffWb;
  2779. lhs = LHS;
  2780. temp = lhs - LSRegRHS;
  2781. if (LoadByte (state, instr, lhs, LUNSIGNED))
  2782. LSBase = temp;
  2783. break;
  2784. case 0x66: /* Store Byte, WriteBack, Post Dec, Reg. */
  2785. if (BIT (4)) {
  2786. #ifdef MODE32
  2787. if (state->is_v6 && handle_v6_insn (state, instr))
  2788. break;
  2789. #endif
  2790. ARMul_UndefInstr (state, instr);
  2791. break;
  2792. }
  2793. UNDEF_LSRBaseEQOffWb;
  2794. UNDEF_LSRBaseEQDestWb;
  2795. UNDEF_LSRPCBaseWb;
  2796. UNDEF_LSRPCOffWb;
  2797. lhs = LHS;
  2798. state->NtransSig = LOW;
  2799. if (StoreByte (state, instr, lhs))
  2800. LSBase = lhs - LSRegRHS;
  2801. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2802. break;
  2803. case 0x67: /* Load Byte, WriteBack, Post Dec, Reg. */
  2804. if (BIT (4)) {
  2805. #ifdef MODE32
  2806. if (state->is_v6
  2807. && handle_v6_insn (state, instr))
  2808. break;
  2809. #endif
  2810. ARMul_UndefInstr (state, instr);
  2811. break;
  2812. }
  2813. UNDEF_LSRBaseEQOffWb;
  2814. UNDEF_LSRBaseEQDestWb;
  2815. UNDEF_LSRPCBaseWb;
  2816. UNDEF_LSRPCOffWb;
  2817. lhs = LHS;
  2818. temp = lhs - LSRegRHS;
  2819. state->NtransSig = LOW;
  2820. if (LoadByte (state, instr, lhs, LUNSIGNED))
  2821. LSBase = temp;
  2822. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2823. break;
  2824. case 0x68: /* Store Word, No WriteBack, Post Inc, Reg. */
  2825. if ((instr & 0x70) == 0x10) { //pkhbt
  2826. u8 idest = BITS(12, 15);
  2827. u8 rfis = BITS(16, 19);
  2828. u8 rlast = BITS(0, 3);
  2829. u8 ishi = BITS(7,11);
  2830. state->Reg[idest] = (state->Reg[rfis] & 0xFFFF) | ((state->Reg[rlast] << ishi) & 0xFFFF0000);
  2831. break;
  2832. } else if ((instr & 0x70) == 0x50) { //pkhtb
  2833. u8 rd_idx = BITS(12, 15);
  2834. u8 rn_idx = BITS(16, 19);
  2835. u8 rm_idx = BITS(0, 3);
  2836. u8 imm5 = BITS(7, 11) ? BITS(7, 11) : 31;
  2837. state->Reg[rd_idx] = ((static_cast<s32>(state->Reg[rm_idx]) >> imm5) & 0xFFFF) | ((state->Reg[rn_idx]) & 0xFFFF0000);
  2838. break;
  2839. } else if (BIT (4)) {
  2840. #ifdef MODE32
  2841. if (state->is_v6
  2842. && handle_v6_insn (state, instr))
  2843. break;
  2844. #endif
  2845. ARMul_UndefInstr (state, instr);
  2846. break;
  2847. }
  2848. UNDEF_LSRBaseEQOffWb;
  2849. UNDEF_LSRBaseEQDestWb;
  2850. UNDEF_LSRPCBaseWb;
  2851. UNDEF_LSRPCOffWb;
  2852. lhs = LHS;
  2853. if (StoreWord (state, instr, lhs))
  2854. LSBase = lhs + LSRegRHS;
  2855. break;
  2856. case 0x69: /* Load Word, No WriteBack, Post Inc, Reg. */
  2857. if (BIT (4)) {
  2858. ARMul_UndefInstr (state, instr);
  2859. break;
  2860. }
  2861. UNDEF_LSRBaseEQOffWb;
  2862. UNDEF_LSRBaseEQDestWb;
  2863. UNDEF_LSRPCBaseWb;
  2864. UNDEF_LSRPCOffWb;
  2865. lhs = LHS;
  2866. temp = lhs + LSRegRHS;
  2867. if (LoadWord (state, instr, lhs))
  2868. LSBase = temp;
  2869. break;
  2870. case 0x6a: /* Store Word, WriteBack, Post Inc, Reg. */
  2871. if (BIT (4)) {
  2872. #ifdef MODE32
  2873. if (state->is_v6
  2874. && handle_v6_insn (state, instr))
  2875. break;
  2876. #endif
  2877. ARMul_UndefInstr (state, instr);
  2878. break;
  2879. }
  2880. UNDEF_LSRBaseEQOffWb;
  2881. UNDEF_LSRBaseEQDestWb;
  2882. UNDEF_LSRPCBaseWb;
  2883. UNDEF_LSRPCOffWb;
  2884. lhs = LHS;
  2885. state->NtransSig = LOW;
  2886. if (StoreWord (state, instr, lhs))
  2887. LSBase = lhs + LSRegRHS;
  2888. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2889. break;
  2890. case 0x6b: /* Load Word, WriteBack, Post Inc, Reg. */
  2891. if (BIT (4)) {
  2892. #ifdef MODE32
  2893. if (state->is_v6
  2894. && handle_v6_insn (state, instr))
  2895. break;
  2896. #endif
  2897. ARMul_UndefInstr (state, instr);
  2898. break;
  2899. }
  2900. UNDEF_LSRBaseEQOffWb;
  2901. UNDEF_LSRBaseEQDestWb;
  2902. UNDEF_LSRPCBaseWb;
  2903. UNDEF_LSRPCOffWb;
  2904. lhs = LHS;
  2905. temp = lhs + LSRegRHS;
  2906. state->NtransSig = LOW;
  2907. if (LoadWord (state, instr, lhs))
  2908. LSBase = temp;
  2909. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2910. break;
  2911. case 0x6c: /* Store Byte, No WriteBack, Post Inc, Reg. */
  2912. if (BIT (4)) {
  2913. #ifdef MODE32
  2914. if (state->is_v6
  2915. && handle_v6_insn (state, instr))
  2916. break;
  2917. #endif
  2918. ARMul_UndefInstr (state, instr);
  2919. break;
  2920. }
  2921. UNDEF_LSRBaseEQOffWb;
  2922. UNDEF_LSRBaseEQDestWb;
  2923. UNDEF_LSRPCBaseWb;
  2924. UNDEF_LSRPCOffWb;
  2925. lhs = LHS;
  2926. if (StoreByte (state, instr, lhs))
  2927. LSBase = lhs + LSRegRHS;
  2928. break;
  2929. case 0x6d: /* Load Byte, No WriteBack, Post Inc, Reg. */
  2930. if (BIT (4)) {
  2931. ARMul_UndefInstr (state, instr);
  2932. break;
  2933. }
  2934. UNDEF_LSRBaseEQOffWb;
  2935. UNDEF_LSRBaseEQDestWb;
  2936. UNDEF_LSRPCBaseWb;
  2937. UNDEF_LSRPCOffWb;
  2938. lhs = LHS;
  2939. temp = lhs + LSRegRHS;
  2940. if (LoadByte (state, instr, lhs, LUNSIGNED))
  2941. LSBase = temp;
  2942. break;
  2943. case 0x6e: /* Store Byte, WriteBack, Post Inc, Reg. */
  2944. #if 0
  2945. if (state->is_v6) {
  2946. int Rm = 0;
  2947. /* utxb */
  2948. if (BITS(15, 19) == 0xf && BITS(4, 7) == 0x7) {
  2949. Rm = (RHS >> (8 * BITS(10, 11))) & 0xff;
  2950. DEST = Rm;
  2951. }
  2952. }
  2953. #endif
  2954. if (BIT (4)) {
  2955. #ifdef MODE32
  2956. if (state->is_v6
  2957. && handle_v6_insn (state, instr))
  2958. break;
  2959. #endif
  2960. ARMul_UndefInstr (state, instr);
  2961. break;
  2962. }
  2963. UNDEF_LSRBaseEQOffWb;
  2964. UNDEF_LSRBaseEQDestWb;
  2965. UNDEF_LSRPCBaseWb;
  2966. UNDEF_LSRPCOffWb;
  2967. lhs = LHS;
  2968. state->NtransSig = LOW;
  2969. if (StoreByte (state, instr, lhs))
  2970. LSBase = lhs + LSRegRHS;
  2971. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2972. break;
  2973. case 0x6f: /* Load Byte, WriteBack, Post Inc, Reg. */
  2974. if (BIT (4)) {
  2975. #ifdef MODE32
  2976. if (state->is_v6
  2977. && handle_v6_insn (state, instr))
  2978. break;
  2979. #endif
  2980. ARMul_UndefInstr (state, instr);
  2981. break;
  2982. }
  2983. UNDEF_LSRBaseEQOffWb;
  2984. UNDEF_LSRBaseEQDestWb;
  2985. UNDEF_LSRPCBaseWb;
  2986. UNDEF_LSRPCOffWb;
  2987. lhs = LHS;
  2988. temp = lhs + LSRegRHS;
  2989. state->NtransSig = LOW;
  2990. if (LoadByte (state, instr, lhs, LUNSIGNED))
  2991. LSBase = temp;
  2992. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  2993. break;
  2994. case 0x70: /* Store Word, No WriteBack, Pre Dec, Reg. */
  2995. if (BIT (4)) {
  2996. #ifdef MODE32
  2997. if (state->is_v6 && handle_v6_insn (state, instr))
  2998. break;
  2999. #endif
  3000. ARMul_UndefInstr (state, instr);
  3001. break;
  3002. }
  3003. (void) StoreWord (state, instr, LHS - LSRegRHS);
  3004. break;
  3005. case 0x71: /* Load Word, No WriteBack, Pre Dec, Reg. */
  3006. if (BIT (4)) {
  3007. ARMul_UndefInstr (state, instr);
  3008. break;
  3009. }
  3010. (void) LoadWord (state, instr, LHS - LSRegRHS);
  3011. break;
  3012. case 0x72: /* Store Word, WriteBack, Pre Dec, Reg. */
  3013. if (BIT (4)) {
  3014. ARMul_UndefInstr (state, instr);
  3015. break;
  3016. }
  3017. UNDEF_LSRBaseEQOffWb;
  3018. UNDEF_LSRBaseEQDestWb;
  3019. UNDEF_LSRPCBaseWb;
  3020. UNDEF_LSRPCOffWb;
  3021. temp = LHS - LSRegRHS;
  3022. if (StoreWord (state, instr, temp))
  3023. LSBase = temp;
  3024. break;
  3025. case 0x73: /* Load Word, WriteBack, Pre Dec, Reg. */
  3026. if (BIT (4)) {
  3027. ARMul_UndefInstr (state, instr);
  3028. break;
  3029. }
  3030. UNDEF_LSRBaseEQOffWb;
  3031. UNDEF_LSRBaseEQDestWb;
  3032. UNDEF_LSRPCBaseWb;
  3033. UNDEF_LSRPCOffWb;
  3034. temp = LHS - LSRegRHS;
  3035. if (LoadWord (state, instr, temp))
  3036. LSBase = temp;
  3037. break;
  3038. case 0x74: /* Store Byte, No WriteBack, Pre Dec, Reg. */
  3039. if (BIT (4)) {
  3040. #ifdef MODE32
  3041. if (state->is_v6 && handle_v6_insn (state, instr))
  3042. break;
  3043. #endif
  3044. ARMul_UndefInstr (state, instr);
  3045. break;
  3046. }
  3047. (void) StoreByte (state, instr, LHS - LSRegRHS);
  3048. break;
  3049. case 0x75: /* Load Byte, No WriteBack, Pre Dec, Reg. */
  3050. if (BIT (4)) {
  3051. #ifdef MODE32
  3052. if (state->is_v6 && handle_v6_insn (state, instr))
  3053. break;
  3054. #endif
  3055. ARMul_UndefInstr (state, instr);
  3056. break;
  3057. }
  3058. (void) LoadByte (state, instr, LHS - LSRegRHS, LUNSIGNED);
  3059. break;
  3060. case 0x76: /* Store Byte, WriteBack, Pre Dec, Reg. */
  3061. if (BIT (4)) {
  3062. ARMul_UndefInstr (state, instr);
  3063. break;
  3064. }
  3065. UNDEF_LSRBaseEQOffWb;
  3066. UNDEF_LSRBaseEQDestWb;
  3067. UNDEF_LSRPCBaseWb;
  3068. UNDEF_LSRPCOffWb;
  3069. temp = LHS - LSRegRHS;
  3070. if (StoreByte (state, instr, temp))
  3071. LSBase = temp;
  3072. break;
  3073. case 0x77: /* Load Byte, WriteBack, Pre Dec, Reg. */
  3074. if (BIT (4)) {
  3075. ARMul_UndefInstr (state, instr);
  3076. break;
  3077. }
  3078. UNDEF_LSRBaseEQOffWb;
  3079. UNDEF_LSRBaseEQDestWb;
  3080. UNDEF_LSRPCBaseWb;
  3081. UNDEF_LSRPCOffWb;
  3082. temp = LHS - LSRegRHS;
  3083. if (LoadByte (state, instr, temp, LUNSIGNED))
  3084. LSBase = temp;
  3085. break;
  3086. case 0x78: /* Store Word, No WriteBack, Pre Inc, Reg. */
  3087. if (BIT (4)) {
  3088. #ifdef MODE32
  3089. if (state->is_v6 && handle_v6_insn (state, instr))
  3090. break;
  3091. #endif
  3092. ARMul_UndefInstr (state, instr);
  3093. break;
  3094. }
  3095. (void) StoreWord (state, instr, LHS + LSRegRHS);
  3096. break;
  3097. case 0x79: /* Load Word, No WriteBack, Pre Inc, Reg. */
  3098. if (BIT (4)) {
  3099. ARMul_UndefInstr (state, instr);
  3100. break;
  3101. }
  3102. (void) LoadWord (state, instr, LHS + LSRegRHS);
  3103. break;
  3104. case 0x7a: /* Store Word, WriteBack, Pre Inc, Reg. */
  3105. if (BIT (4)) {
  3106. #ifdef MODE32
  3107. if (state->is_v6 && handle_v6_insn (state, instr))
  3108. break;
  3109. #endif
  3110. ARMul_UndefInstr (state, instr);
  3111. break;
  3112. }
  3113. UNDEF_LSRBaseEQOffWb;
  3114. UNDEF_LSRBaseEQDestWb;
  3115. UNDEF_LSRPCBaseWb;
  3116. UNDEF_LSRPCOffWb;
  3117. temp = LHS + LSRegRHS;
  3118. if (StoreWord (state, instr, temp))
  3119. LSBase = temp;
  3120. break;
  3121. case 0x7b: /* Load Word, WriteBack, Pre Inc, Reg. */
  3122. if (BIT (4)) {
  3123. ARMul_UndefInstr (state, instr);
  3124. break;
  3125. }
  3126. UNDEF_LSRBaseEQOffWb;
  3127. UNDEF_LSRBaseEQDestWb;
  3128. UNDEF_LSRPCBaseWb;
  3129. UNDEF_LSRPCOffWb;
  3130. temp = LHS + LSRegRHS;
  3131. if (LoadWord (state, instr, temp))
  3132. LSBase = temp;
  3133. break;
  3134. case 0x7c: /* Store Byte, No WriteBack, Pre Inc, Reg. */
  3135. if (BIT (4)) {
  3136. #ifdef MODE32
  3137. if (state->is_v6 && handle_v6_insn (state, instr))
  3138. break;
  3139. #endif
  3140. ARMul_UndefInstr (state, instr);
  3141. break;
  3142. }
  3143. (void) StoreByte (state, instr, LHS + LSRegRHS);
  3144. break;
  3145. case 0x7d: /* Load Byte, No WriteBack, Pre Inc, Reg. */
  3146. if (BIT (4)) {
  3147. ARMul_UndefInstr (state, instr);
  3148. break;
  3149. }
  3150. (void) LoadByte (state, instr, LHS + LSRegRHS, LUNSIGNED);
  3151. break;
  3152. case 0x7e: /* Store Byte, WriteBack, Pre Inc, Reg. */
  3153. if (BIT (4)) {
  3154. ARMul_UndefInstr (state, instr);
  3155. break;
  3156. }
  3157. UNDEF_LSRBaseEQOffWb;
  3158. UNDEF_LSRBaseEQDestWb;
  3159. UNDEF_LSRPCBaseWb;
  3160. UNDEF_LSRPCOffWb;
  3161. temp = LHS + LSRegRHS;
  3162. if (StoreByte (state, instr, temp))
  3163. LSBase = temp;
  3164. break;
  3165. case 0x7f: /* Load Byte, WriteBack, Pre Inc, Reg. */
  3166. if (BIT (4)) {
  3167. LOG_DEBUG(Core_ARM11, "got unhandled special breakpoint");
  3168. return 1;
  3169. }
  3170. UNDEF_LSRBaseEQOffWb;
  3171. UNDEF_LSRBaseEQDestWb;
  3172. UNDEF_LSRPCBaseWb;
  3173. UNDEF_LSRPCOffWb;
  3174. temp = LHS + LSRegRHS;
  3175. if (LoadByte (state, instr, temp, LUNSIGNED))
  3176. LSBase = temp;
  3177. break;
  3178. /* Multiple Data Transfer Instructions. */
  3179. case 0x80: /* Store, No WriteBack, Post Dec. */
  3180. STOREMULT (instr, LSBase - LSMNumRegs + 4L, 0L);
  3181. break;
  3182. case 0x81: /* Load, No WriteBack, Post Dec. */
  3183. LOADMULT (instr, LSBase - LSMNumRegs + 4L, 0L);
  3184. break;
  3185. case 0x82: /* Store, WriteBack, Post Dec. */
  3186. temp = LSBase - LSMNumRegs;
  3187. STOREMULT (instr, temp + 4L, temp);
  3188. break;
  3189. case 0x83: /* Load, WriteBack, Post Dec. */
  3190. temp = LSBase - LSMNumRegs;
  3191. LOADMULT (instr, temp + 4L, temp);
  3192. break;
  3193. case 0x84: /* Store, Flags, No WriteBack, Post Dec. */
  3194. STORESMULT (instr, LSBase - LSMNumRegs + 4L, 0L);
  3195. break;
  3196. case 0x85: /* Load, Flags, No WriteBack, Post Dec. */
  3197. LOADSMULT (instr, LSBase - LSMNumRegs + 4L, 0L);
  3198. break;
  3199. case 0x86: /* Store, Flags, WriteBack, Post Dec. */
  3200. temp = LSBase - LSMNumRegs;
  3201. STORESMULT (instr, temp + 4L, temp);
  3202. break;
  3203. case 0x87: /* Load, Flags, WriteBack, Post Dec. */
  3204. temp = LSBase - LSMNumRegs;
  3205. LOADSMULT (instr, temp + 4L, temp);
  3206. break;
  3207. case 0x88: /* Store, No WriteBack, Post Inc. */
  3208. STOREMULT (instr, LSBase, 0L);
  3209. break;
  3210. case 0x89: /* Load, No WriteBack, Post Inc. */
  3211. LOADMULT (instr, LSBase, 0L);
  3212. break;
  3213. case 0x8a: /* Store, WriteBack, Post Inc. */
  3214. temp = LSBase;
  3215. STOREMULT (instr, temp, temp + LSMNumRegs);
  3216. break;
  3217. case 0x8b: /* Load, WriteBack, Post Inc. */
  3218. temp = LSBase;
  3219. LOADMULT (instr, temp, temp + LSMNumRegs);
  3220. break;
  3221. case 0x8c: /* Store, Flags, No WriteBack, Post Inc. */
  3222. STORESMULT (instr, LSBase, 0L);
  3223. break;
  3224. case 0x8d: /* Load, Flags, No WriteBack, Post Inc. */
  3225. LOADSMULT (instr, LSBase, 0L);
  3226. break;
  3227. case 0x8e: /* Store, Flags, WriteBack, Post Inc. */
  3228. temp = LSBase;
  3229. STORESMULT (instr, temp, temp + LSMNumRegs);
  3230. break;
  3231. case 0x8f: /* Load, Flags, WriteBack, Post Inc. */
  3232. temp = LSBase;
  3233. LOADSMULT (instr, temp, temp + LSMNumRegs);
  3234. break;
  3235. case 0x90: /* Store, No WriteBack, Pre Dec. */
  3236. STOREMULT (instr, LSBase - LSMNumRegs, 0L);
  3237. break;
  3238. case 0x91: /* Load, No WriteBack, Pre Dec. */
  3239. LOADMULT (instr, LSBase - LSMNumRegs, 0L);
  3240. break;
  3241. case 0x92: /* Store, WriteBack, Pre Dec. */
  3242. temp = LSBase - LSMNumRegs;
  3243. STOREMULT (instr, temp, temp);
  3244. break;
  3245. case 0x93: /* Load, WriteBack, Pre Dec. */
  3246. temp = LSBase - LSMNumRegs;
  3247. LOADMULT (instr, temp, temp);
  3248. break;
  3249. case 0x94: /* Store, Flags, No WriteBack, Pre Dec. */
  3250. STORESMULT (instr, LSBase - LSMNumRegs, 0L);
  3251. break;
  3252. case 0x95: /* Load, Flags, No WriteBack, Pre Dec. */
  3253. LOADSMULT (instr, LSBase - LSMNumRegs, 0L);
  3254. break;
  3255. case 0x96: /* Store, Flags, WriteBack, Pre Dec. */
  3256. temp = LSBase - LSMNumRegs;
  3257. STORESMULT (instr, temp, temp);
  3258. break;
  3259. case 0x97: /* Load, Flags, WriteBack, Pre Dec. */
  3260. temp = LSBase - LSMNumRegs;
  3261. LOADSMULT (instr, temp, temp);
  3262. break;
  3263. case 0x98: /* Store, No WriteBack, Pre Inc. */
  3264. STOREMULT (instr, LSBase + 4L, 0L);
  3265. break;
  3266. case 0x99: /* Load, No WriteBack, Pre Inc. */
  3267. LOADMULT (instr, LSBase + 4L, 0L);
  3268. break;
  3269. case 0x9a: /* Store, WriteBack, Pre Inc. */
  3270. temp = LSBase;
  3271. STOREMULT (instr, temp + 4L, temp + LSMNumRegs);
  3272. break;
  3273. case 0x9b: /* Load, WriteBack, Pre Inc. */
  3274. temp = LSBase;
  3275. LOADMULT (instr, temp + 4L, temp + LSMNumRegs);
  3276. break;
  3277. case 0x9c: /* Store, Flags, No WriteBack, Pre Inc. */
  3278. STORESMULT (instr, LSBase + 4L, 0L);
  3279. break;
  3280. case 0x9d: /* Load, Flags, No WriteBack, Pre Inc. */
  3281. LOADSMULT (instr, LSBase + 4L, 0L);
  3282. break;
  3283. case 0x9e: /* Store, Flags, WriteBack, Pre Inc. */
  3284. temp = LSBase;
  3285. STORESMULT (instr, temp + 4L, temp + LSMNumRegs);
  3286. break;
  3287. case 0x9f: /* Load, Flags, WriteBack, Pre Inc. */
  3288. temp = LSBase;
  3289. LOADSMULT (instr, temp + 4L, temp + LSMNumRegs);
  3290. break;
  3291. /* Branch forward. */
  3292. case 0xa0:
  3293. case 0xa1:
  3294. case 0xa2:
  3295. case 0xa3:
  3296. case 0xa4:
  3297. case 0xa5:
  3298. case 0xa6:
  3299. case 0xa7:
  3300. state->Reg[15] = pc + 8 + POSBRANCH;
  3301. FLUSHPIPE;
  3302. break;
  3303. /* Branch backward. */
  3304. case 0xa8:
  3305. case 0xa9:
  3306. case 0xaa:
  3307. case 0xab:
  3308. case 0xac:
  3309. case 0xad:
  3310. case 0xae:
  3311. case 0xaf:
  3312. state->Reg[15] = pc + 8 + NEGBRANCH;
  3313. FLUSHPIPE;
  3314. break;
  3315. /* Branch and Link forward. */
  3316. case 0xb0:
  3317. case 0xb1:
  3318. case 0xb2:
  3319. case 0xb3:
  3320. case 0xb4:
  3321. case 0xb5:
  3322. case 0xb6:
  3323. case 0xb7:
  3324. /* Put PC into Link. */
  3325. #ifdef MODE32
  3326. state->Reg[14] = pc + 4;
  3327. #else
  3328. state->Reg[14] = (pc + 4) | ECC | ER15INT | EMODE;
  3329. #endif
  3330. state->Reg[15] = pc + 8 + POSBRANCH;
  3331. FLUSHPIPE;
  3332. #ifdef callstacker
  3333. memset(a, 0, 256);
  3334. aufloeser(a, state->Reg[15]);
  3335. printf("call %08X %08X %s(%08X %08X %08X %08X %08X %08X %08X)\n", state->Reg[14], state->Reg[15], a, state->Reg[0], state->Reg[1], state->Reg[2], state->Reg[3], mem_Read32(state->Reg[13]), mem_Read32(state->Reg[13] - 4),mem_Read32(state->Reg[13] - 8));
  3336. #endif
  3337. break;
  3338. /* Branch and Link backward. */
  3339. case 0xb8:
  3340. case 0xb9:
  3341. case 0xba:
  3342. case 0xbb:
  3343. case 0xbc:
  3344. case 0xbd:
  3345. case 0xbe:
  3346. case 0xbf:
  3347. /* Put PC into Link. */
  3348. #ifdef MODE32
  3349. state->Reg[14] = pc + 4;
  3350. #else
  3351. state->Reg[14] = (pc + 4) | ECC | ER15INT | EMODE;
  3352. #endif
  3353. state->Reg[15] = pc + 8 + NEGBRANCH;
  3354. FLUSHPIPE;
  3355. #ifdef callstacker
  3356. memset(a, 0, 256);
  3357. aufloeser(a, state->Reg[15]);
  3358. printf("call %08X %08X %s(%08X %08X %08X %08X %08X %08X %08X)\n", state->Reg[14], state->Reg[15], a, state->Reg[0], state->Reg[1], state->Reg[2], state->Reg[3], mem_Read32(state->Reg[13]), mem_Read32(state->Reg[13] - 4),mem_Read32(state->Reg[13] - 8));
  3359. #endif
  3360. break;
  3361. /* Co-Processor Data Transfers. */
  3362. case 0xc4:
  3363. if ((instr & 0x0FF00FF0) == 0xC400B10) { //vmov BIT(0-3), BIT(12-15), BIT(16-20), vmov d0, r0, r0
  3364. state->ExtReg[BITS(0, 3) << 1] = state->Reg[BITS(12, 15)];
  3365. state->ExtReg[(BITS(0, 3) << 1) + 1] = state->Reg[BITS(16, 20)];
  3366. break;
  3367. } else if (state->is_v5) {
  3368. /* Reading from R15 is UNPREDICTABLE. */
  3369. if (BITS (12, 15) == 15 || BITS (16, 19) == 15)
  3370. ARMul_UndefInstr (state, instr);
  3371. /* Is access to coprocessor 0 allowed ? */
  3372. else if (!CP_ACCESS_ALLOWED(state, CPNum))
  3373. ARMul_UndefInstr (state, instr);
  3374. else {
  3375. /* MCRR, ARMv5TE and up */
  3376. ARMul_MCRR (state, instr, DEST, state->Reg[LHSReg]);
  3377. break;
  3378. }
  3379. }
  3380. /* Drop through. */
  3381. case 0xc0: /* Store , No WriteBack , Post Dec. */
  3382. ARMul_STC (state, instr, LHS);
  3383. break;
  3384. case 0xc5:
  3385. if ((instr & 0x00000FF0) == 0xB10) { //vmov BIT(12-15), BIT(16-20), BIT(0-3) vmov r0, r0, d0
  3386. state->Reg[BITS(12, 15)] = state->ExtReg[BITS(0, 3) << 1];
  3387. state->Reg[BITS(16, 19)] = state->ExtReg[(BITS(0, 3) << 1) + 1];
  3388. break;
  3389. } else if (state->is_v5) {
  3390. /* Writes to R15 are UNPREDICATABLE. */
  3391. if (DESTReg == 15 || LHSReg == 15)
  3392. ARMul_UndefInstr (state, instr);
  3393. /* Is access to the coprocessor allowed ? */
  3394. else if (!CP_ACCESS_ALLOWED(state, CPNum)) {
  3395. ARMul_UndefInstr(state, instr);
  3396. } else {
  3397. /* MRRC, ARMv5TE and up */
  3398. ARMul_MRRC (state, instr, &DEST, &(state->Reg[LHSReg]));
  3399. break;
  3400. }
  3401. }
  3402. /* Drop through. */
  3403. case 0xc1: /* Load , No WriteBack , Post Dec. */
  3404. ARMul_LDC (state, instr, LHS);
  3405. break;
  3406. case 0xc2:
  3407. case 0xc6: /* Store , WriteBack , Post Dec. */
  3408. lhs = LHS;
  3409. state->Base = lhs - LSCOff;
  3410. ARMul_STC (state, instr, lhs);
  3411. break;
  3412. case 0xc3:
  3413. case 0xc7: /* Load , WriteBack , Post Dec. */
  3414. lhs = LHS;
  3415. state->Base = lhs - LSCOff;
  3416. ARMul_LDC (state, instr, lhs);
  3417. break;
  3418. case 0xc8:
  3419. case 0xcc: /* Store , No WriteBack , Post Inc. */
  3420. ARMul_STC (state, instr, LHS);
  3421. break;
  3422. case 0xc9:
  3423. case 0xcd: /* Load , No WriteBack , Post Inc. */
  3424. ARMul_LDC (state, instr, LHS);
  3425. break;
  3426. case 0xca:
  3427. case 0xce: /* Store , WriteBack , Post Inc. */
  3428. lhs = LHS;
  3429. state->Base = lhs + LSCOff;
  3430. ARMul_STC (state, instr, LHS);
  3431. break;
  3432. case 0xcb:
  3433. case 0xcf: /* Load , WriteBack , Post Inc. */
  3434. lhs = LHS;
  3435. state->Base = lhs + LSCOff;
  3436. ARMul_LDC (state, instr, LHS);
  3437. break;
  3438. case 0xd0:
  3439. case 0xd4: /* Store , No WriteBack , Pre Dec. */
  3440. ARMul_STC (state, instr, LHS - LSCOff);
  3441. break;
  3442. case 0xd1:
  3443. case 0xd5: /* Load , No WriteBack , Pre Dec. */
  3444. ARMul_LDC (state, instr, LHS - LSCOff);
  3445. break;
  3446. case 0xd2:
  3447. case 0xd6: /* Store , WriteBack , Pre Dec. */
  3448. lhs = LHS - LSCOff;
  3449. state->Base = lhs;
  3450. ARMul_STC (state, instr, lhs);
  3451. break;
  3452. case 0xd3:
  3453. case 0xd7: /* Load , WriteBack , Pre Dec. */
  3454. lhs = LHS - LSCOff;
  3455. state->Base = lhs;
  3456. ARMul_LDC (state, instr, lhs);
  3457. break;
  3458. case 0xd8:
  3459. case 0xdc: /* Store , No WriteBack , Pre Inc. */
  3460. ARMul_STC (state, instr, LHS + LSCOff);
  3461. break;
  3462. case 0xd9:
  3463. case 0xdd: /* Load , No WriteBack , Pre Inc. */
  3464. ARMul_LDC (state, instr, LHS + LSCOff);
  3465. break;
  3466. case 0xda:
  3467. case 0xde: /* Store , WriteBack , Pre Inc. */
  3468. lhs = LHS + LSCOff;
  3469. state->Base = lhs;
  3470. ARMul_STC (state, instr, lhs);
  3471. break;
  3472. case 0xdb:
  3473. case 0xdf: /* Load , WriteBack , Pre Inc. */
  3474. lhs = LHS + LSCOff;
  3475. state->Base = lhs;
  3476. ARMul_LDC (state, instr, lhs);
  3477. break;
  3478. /* Co-Processor Register Transfers (MCR) and Data Ops. */
  3479. case 0xe2:
  3480. /*if (!CP_ACCESS_ALLOWED (state, CPNum)) {
  3481. ARMul_UndefInstr (state, instr);
  3482. break;
  3483. }*/
  3484. case 0xe0:
  3485. case 0xe4:
  3486. case 0xe6:
  3487. case 0xe8:
  3488. case 0xea:
  3489. case 0xec:
  3490. case 0xee:
  3491. if (BIT (4)) {
  3492. /* MCR. */
  3493. if (DESTReg == 15) {
  3494. UNDEF_MCRPC;
  3495. #ifdef MODE32
  3496. ARMul_MCR (state, instr, state->Reg[15] + isize);
  3497. #else
  3498. ARMul_MCR (state, instr, ECC | ER15INT | EMODE | ((state->Reg[15] + isize) & R15PCBITS));
  3499. #endif
  3500. } else
  3501. ARMul_MCR (state, instr, DEST);
  3502. } else
  3503. /* CDP Part 1. */
  3504. ARMul_CDP (state, instr);
  3505. break;
  3506. /* Co-Processor Register Transfers (MRC) and Data Ops. */
  3507. case 0xe1:
  3508. case 0xe3:
  3509. case 0xe5:
  3510. case 0xe7:
  3511. case 0xe9:
  3512. case 0xeb:
  3513. case 0xed:
  3514. case 0xef:
  3515. if (BIT (4)) {
  3516. /* MRC */
  3517. temp = ARMul_MRC (state, instr);
  3518. if (DESTReg == 15) {
  3519. ASSIGNN ((temp & NBIT) != 0);
  3520. ASSIGNZ ((temp & ZBIT) != 0);
  3521. ASSIGNC ((temp & CBIT) != 0);
  3522. ASSIGNV ((temp & VBIT) != 0);
  3523. } else
  3524. DEST = temp;
  3525. } else
  3526. /* CDP Part 2. */
  3527. ARMul_CDP (state, instr);
  3528. break;
  3529. /* SWI instruction. */
  3530. case 0xf0:
  3531. case 0xf1:
  3532. case 0xf2:
  3533. case 0xf3:
  3534. case 0xf4:
  3535. case 0xf5:
  3536. case 0xf6:
  3537. case 0xf7:
  3538. case 0xf8:
  3539. case 0xf9:
  3540. case 0xfa:
  3541. case 0xfb:
  3542. case 0xfc:
  3543. case 0xfd:
  3544. case 0xfe:
  3545. case 0xff:
  3546. //svc_Execute(state, BITS(0, 23));
  3547. HLE::CallSVC(instr);
  3548. break;
  3549. }
  3550. }
  3551. #ifdef MODET
  3552. donext:
  3553. #endif
  3554. state->pc = pc;
  3555. #if 0
  3556. /* shenoubang */
  3557. instr_sum++;
  3558. int i, j;
  3559. i = j = 0;
  3560. if (instr_sum >= 7388648) {
  3561. //if (pc == 0xc0008ab4) {
  3562. // printf("instr_sum: %d\n", instr_sum);
  3563. // start_kernel : 0xc000895c
  3564. printf("--------------------------------------------------\n");
  3565. for (i = 0; i < 16; i++) {
  3566. printf("[R%02d]:[0x%08x]\t", i, state->Reg[i]);
  3567. if ((i % 3) == 2) {
  3568. printf("\n");
  3569. }
  3570. }
  3571. printf("[cpr]:[0x%08x]\t[spr0]:[0x%08x]\n", state->Cpsr, state->Spsr[0]);
  3572. for (j = 1; j < 7; j++) {
  3573. printf("[spr%d]:[0x%08x]\t", j, state->Spsr[j]);
  3574. if ((j % 4) == 3) {
  3575. printf("\n");
  3576. }
  3577. }
  3578. printf("\n[PC]:[0x%08x]\t[INST]:[0x%08x]\t[COUNT]:[%d]\n", pc, instr, instr_sum);
  3579. printf("--------------------------------------------------\n");
  3580. }
  3581. #endif
  3582. #if 0
  3583. fprintf(state->state_log, "PC:0x%x\n", pc);
  3584. for (reg_index = 0; reg_index < 16; reg_index ++) {
  3585. if (state->Reg[reg_index] != mirror_register_file[reg_index]) {
  3586. fprintf(state->state_log, "R%d:0x%x\n", reg_index, state->Reg[reg_index]);
  3587. mirror_register_file[reg_index] = state->Reg[reg_index];
  3588. }
  3589. }
  3590. if (state->Cpsr != mirror_register_file[CPSR_REG]) {
  3591. fprintf(state->state_log, "Cpsr:0x%x\n", state->Cpsr);
  3592. mirror_register_file[CPSR_REG] = state->Cpsr;
  3593. }
  3594. if (state->RegBank[SVCBANK][13] != mirror_register_file[R13_SVC]) {
  3595. fprintf(state->state_log, "R13_SVC:0x%x\n", state->RegBank[SVCBANK][13]);
  3596. mirror_register_file[R13_SVC] = state->RegBank[SVCBANK][13];
  3597. }
  3598. if (state->RegBank[SVCBANK][14] != mirror_register_file[R14_SVC]) {
  3599. fprintf(state->state_log, "R14_SVC:0x%x\n", state->RegBank[SVCBANK][14]);
  3600. mirror_register_file[R14_SVC] = state->RegBank[SVCBANK][14];
  3601. }
  3602. if (state->RegBank[ABORTBANK][13] != mirror_register_file[R13_ABORT]) {
  3603. fprintf(state->state_log, "R13_ABORT:0x%x\n", state->RegBank[ABORTBANK][13]);
  3604. mirror_register_file[R13_ABORT] = state->RegBank[ABORTBANK][13];
  3605. }
  3606. if (state->RegBank[ABORTBANK][14] != mirror_register_file[R14_ABORT]) {
  3607. fprintf(state->state_log, "R14_ABORT:0x%x\n", state->RegBank[ABORTBANK][14]);
  3608. mirror_register_file[R14_ABORT] = state->RegBank[ABORTBANK][14];
  3609. }
  3610. if (state->RegBank[UNDEFBANK][13] != mirror_register_file[R13_UNDEF]) {
  3611. fprintf(state->state_log, "R13_UNDEF:0x%x\n", state->RegBank[UNDEFBANK][13]);
  3612. mirror_register_file[R13_UNDEF] = state->RegBank[UNDEFBANK][13];
  3613. }
  3614. if (state->RegBank[UNDEFBANK][14] != mirror_register_file[R14_UNDEF]) {
  3615. fprintf(state->state_log, "R14_UNDEF:0x%x\n", state->RegBank[UNDEFBANK][14]);
  3616. mirror_register_file[R14_UNDEF] = state->RegBank[UNDEFBANK][14];
  3617. }
  3618. if (state->RegBank[IRQBANK][13] != mirror_register_file[R13_IRQ]) {
  3619. fprintf(state->state_log, "R13_IRQ:0x%x\n", state->RegBank[IRQBANK][13]);
  3620. mirror_register_file[R13_IRQ] = state->RegBank[IRQBANK][13];
  3621. }
  3622. if (state->RegBank[IRQBANK][14] != mirror_register_file[R14_IRQ]) {
  3623. fprintf(state->state_log, "R14_IRQ:0x%x\n", state->RegBank[IRQBANK][14]);
  3624. mirror_register_file[R14_IRQ] = state->RegBank[IRQBANK][14];
  3625. }
  3626. if (state->RegBank[FIQBANK][8] != mirror_register_file[R8_FIRQ]) {
  3627. fprintf(state->state_log, "R8_FIRQ:0x%x\n", state->RegBank[FIQBANK][8]);
  3628. mirror_register_file[R8_FIRQ] = state->RegBank[FIQBANK][8];
  3629. }
  3630. if (state->RegBank[FIQBANK][9] != mirror_register_file[R9_FIRQ]) {
  3631. fprintf(state->state_log, "R9_FIRQ:0x%x\n", state->RegBank[FIQBANK][9]);
  3632. mirror_register_file[R9_FIRQ] = state->RegBank[FIQBANK][9];
  3633. }
  3634. if (state->RegBank[FIQBANK][10] != mirror_register_file[R10_FIRQ]) {
  3635. fprintf(state->state_log, "R10_FIRQ:0x%x\n", state->RegBank[FIQBANK][10]);
  3636. mirror_register_file[R10_FIRQ] = state->RegBank[FIQBANK][10];
  3637. }
  3638. if (state->RegBank[FIQBANK][11] != mirror_register_file[R11_FIRQ]) {
  3639. fprintf(state->state_log, "R11_FIRQ:0x%x\n", state->RegBank[FIQBANK][11]);
  3640. mirror_register_file[R11_FIRQ] = state->RegBank[FIQBANK][11];
  3641. }
  3642. if (state->RegBank[FIQBANK][12] != mirror_register_file[R12_FIRQ]) {
  3643. fprintf(state->state_log, "R12_FIRQ:0x%x\n", state->RegBank[FIQBANK][12]);
  3644. mirror_register_file[R12_FIRQ] = state->RegBank[FIQBANK][12];
  3645. }
  3646. if (state->RegBank[FIQBANK][13] != mirror_register_file[R13_FIRQ]) {
  3647. fprintf(state->state_log, "R13_FIRQ:0x%x\n", state->RegBank[FIQBANK][13]);
  3648. mirror_register_file[R13_FIRQ] = state->RegBank[FIQBANK][13];
  3649. }
  3650. if (state->RegBank[FIQBANK][14] != mirror_register_file[R14_FIRQ]) {
  3651. fprintf(state->state_log, "R14_FIRQ:0x%x\n", state->RegBank[FIQBANK][14]);
  3652. mirror_register_file[R14_FIRQ] = state->RegBank[FIQBANK][14];
  3653. }
  3654. if (state->Spsr[SVCBANK] != mirror_register_file[SPSR_SVC]) {
  3655. fprintf(state->state_log, "SPSR_SVC:0x%x\n", state->Spsr[SVCBANK]);
  3656. mirror_register_file[SPSR_SVC] = state->RegBank[SVCBANK];
  3657. }
  3658. if (state->Spsr[ABORTBANK] != mirror_register_file[SPSR_ABORT]) {
  3659. fprintf(state->state_log, "SPSR_ABORT:0x%x\n", state->Spsr[ABORTBANK]);
  3660. mirror_register_file[SPSR_ABORT] = state->RegBank[ABORTBANK];
  3661. }
  3662. if (state->Spsr[UNDEFBANK] != mirror_register_file[SPSR_UNDEF]) {
  3663. fprintf(state->state_log, "SPSR_UNDEF:0x%x\n", state->Spsr[UNDEFBANK]);
  3664. mirror_register_file[SPSR_UNDEF] = state->RegBank[UNDEFBANK];
  3665. }
  3666. if (state->Spsr[IRQBANK] != mirror_register_file[SPSR_IRQ]) {
  3667. fprintf(state->state_log, "SPSR_IRQ:0x%x\n", state->Spsr[IRQBANK]);
  3668. mirror_register_file[SPSR_IRQ] = state->RegBank[IRQBANK];
  3669. }
  3670. if (state->Spsr[FIQBANK] != mirror_register_file[SPSR_FIRQ]) {
  3671. fprintf(state->state_log, "SPSR_FIRQ:0x%x\n", state->Spsr[FIQBANK]);
  3672. mirror_register_file[SPSR_FIRQ] = state->RegBank[FIQBANK];
  3673. }
  3674. #endif
  3675. #ifdef NEED_UI_LOOP_HOOK
  3676. if (ui_loop_hook != NULL && ui_loop_hook_counter-- < 0) {
  3677. ui_loop_hook_counter = UI_LOOP_POLL_INTERVAL;
  3678. ui_loop_hook (0);
  3679. }
  3680. #endif /* NEED_UI_LOOP_HOOK */
  3681. /*added energy_prof statement by ksh in 2004-11-26 */
  3682. //chy 2005-07-28 for standalone
  3683. //ARMul_do_energy(state,instr,pc);
  3684. //teawater add for record reg value to ./reg.txt 2005.07.10---------------------
  3685. if (state->tea_break_ok && pc == state->tea_break_addr) {
  3686. //ARMul_Debug (state, 0, 0);
  3687. state->tea_break_ok = 0;
  3688. } else {
  3689. state->tea_break_ok = 1;
  3690. }
  3691. //AJ2D--------------------------------------------------------------------------
  3692. //chy 2006-04-14 for ctrl-c debug
  3693. #if 0
  3694. if (debugmode) {
  3695. if (instr != ARMul_ABORTWORD) {
  3696. remote_interrupt_test_time++;
  3697. //chy 2006-04-14 2000 should be changed in skyeye_conf ???!!!
  3698. if (remote_interrupt_test_time >= 2000) {
  3699. remote_interrupt_test_time=0;
  3700. if (remote_interrupt()) {
  3701. //for test
  3702. //printf("SKYEYE: ICE_debug recv Ctrl_C\n");
  3703. state->EndCondition = 0;
  3704. state->Emulate = STOP;
  3705. }
  3706. }
  3707. }
  3708. }
  3709. #endif
  3710. /* jump out every time */
  3711. //state->EndCondition = 0;
  3712. //state->Emulate = STOP;
  3713. //chy 2006-04-12 for ICE debug
  3714. TEST_EMULATE:
  3715. if (state->Emulate == ONCE)
  3716. state->Emulate = STOP;
  3717. //chy: 2003-08-23: should not use CHANGEMODE !!!!
  3718. /* If we have changed mode, allow the PC to advance before stopping. */
  3719. // else if (state->Emulate == CHANGEMODE)
  3720. // continue;
  3721. else if (state->Emulate != RUN)
  3722. break;
  3723. }
  3724. while (state->NumInstrsToExecute);
  3725. exit:
  3726. state->decoded = decoded;
  3727. state->loaded = loaded;
  3728. state->pc = pc;
  3729. //chy 2006-04-12, for ICE debug
  3730. state->decoded_addr=decoded_addr;
  3731. state->loaded_addr=loaded_addr;
  3732. return pc;
  3733. }
  3734. //teawater add for arm2x86 2005.02.17-------------------------------------------
  3735. /*ywc 2005-04-01*/
  3736. //#include "tb.h"
  3737. //#include "arm2x86_self.h"
  3738. static volatile void (*gen_func) (void);
  3739. //static volatile ARMul_State *tmp_st;
  3740. //static volatile ARMul_State *save_st;
  3741. static volatile uint32_t tmp_st;
  3742. static volatile uint32_t save_st;
  3743. static volatile uint32_t save_T0;
  3744. static volatile uint32_t save_T1;
  3745. static volatile uint32_t save_T2;
  3746. #ifdef MODE32
  3747. #ifdef DBCT
  3748. //teawater change for debug function 2005.07.09---------------------------------
  3749. ARMword
  3750. ARMul_Emulate32_dbct (ARMul_State * state) {
  3751. static int init = 0;
  3752. static FILE *fd;
  3753. /*if (!init) {
  3754. fd = fopen("./pc.txt", "w");
  3755. if (!fd) {
  3756. exit(-1);
  3757. }
  3758. init = 1;
  3759. } */
  3760. state->Reg[15] += INSN_SIZE;
  3761. do {
  3762. /*if (skyeye_config.log.logon>=1) {
  3763. if (state->NumInstrs>=skyeye_config.log.start && state->NumInstrs<=skyeye_config.log.end) {
  3764. static int mybegin=0;
  3765. static int myinstrnum=0;
  3766. if (mybegin==0) mybegin=1;
  3767. if (mybegin==1) {
  3768. state->Reg[15] -= INSN_SIZE;
  3769. if (skyeye_config.log.logon>=1) fprintf(skyeye_logfd,"N %llx :p %x,i %x,",state->NumInstrs, (state->Reg[15] - INSN_SIZE), instr);
  3770. if (skyeye_config.log.logon>=2) SKYEYE_OUTREGS(skyeye_logfd);
  3771. if (skyeye_config.log.logon>=3) SKYEYE_OUTMOREREGS(skyeye_logfd);
  3772. fprintf(skyeye_logfd,"\n");
  3773. if (skyeye_config.log.length>0) {
  3774. myinstrnum++;
  3775. if (myinstrnum>=skyeye_config.log.length) {
  3776. myinstrnum=0;
  3777. fflush(skyeye_logfd);
  3778. fseek(skyeye_logfd,0L,SEEK_SET);
  3779. }
  3780. }
  3781. state->Reg[15] += INSN_SIZE;
  3782. }
  3783. }
  3784. } */
  3785. state->trap = 0;
  3786. gen_func =
  3787. (void *) tb_find (state, state->Reg[15] - INSN_SIZE);
  3788. if (!gen_func) {
  3789. //fprintf(stderr, "SKYEYE: tb_find: Error in find the translate block.\n");
  3790. //exit(-1);
  3791. //TRAP_INSN_ABORT
  3792. //TEA_OUT(printf("\n------------\npc:%x\n", state->Reg[15] - INSN_SIZE));
  3793. //TEA_OUT(printf("TRAP_INSN_ABORT\n"));
  3794. //teawater add for xscale(arm v5) 2005.09.01------------------------------------
  3795. /*XScale_set_fsr_far(state, ARMul_CP15_R5_MMU_EXCPT, state->Reg[15] - INSN_SIZE);
  3796. state->Reg[15] += INSN_SIZE;
  3797. ARMul_Abort(state, ARMul_PrefetchAbortV);
  3798. state->Reg[15] += INSN_SIZE;
  3799. goto next; */
  3800. state->trap = TRAP_INSN_ABORT;
  3801. goto check;
  3802. //AJ2D--------------------------------------------------------------------------
  3803. }
  3804. save_st = (uint32_t) st;
  3805. save_T0 = T0;
  3806. save_T1 = T1;
  3807. save_T2 = T2;
  3808. tmp_st = (uint32_t) state;
  3809. wmb ();
  3810. st = (ARMul_State *) tmp_st;
  3811. gen_func ();
  3812. st = (ARMul_State *) save_st;
  3813. T0 = save_T0;
  3814. T1 = save_T1;
  3815. T2 = save_T2;
  3816. /*if (state->trap != TRAP_OUT) {
  3817. state->tea_break_ok = 1;
  3818. }
  3819. if (state->trap <= TRAP_SET_R15) {
  3820. goto next;
  3821. } */
  3822. //TEA_OUT(printf("\n------------\npc:%x\n", state->Reg[15] - INSN_SIZE));
  3823. //teawater add check thumb 2005.07.21-------------------------------------------
  3824. /*if (TFLAG) {
  3825. state->Reg[15] -= 2;
  3826. return(state->Reg[15]);
  3827. } */
  3828. //AJ2D--------------------------------------------------------------------------
  3829. //teawater add for xscale(arm v5) 2005.09.01------------------------------------
  3830. check:
  3831. //AJ2D--------------------------------------------------------------------------
  3832. switch (state->trap) {
  3833. case TRAP_RESET: {
  3834. //TEA_OUT(printf("TRAP_RESET\n"));
  3835. ARMul_Abort (state, ARMul_ResetV);
  3836. state->Reg[15] += INSN_SIZE;
  3837. }
  3838. break;
  3839. case TRAP_UNPREDICTABLE: {
  3840. //ARMul_Debug (state, 0, 0);
  3841. }
  3842. break;
  3843. case TRAP_INSN_UNDEF: {
  3844. //TEA_OUT(printf("TRAP_INSN_UNDEF\n"));
  3845. state->Reg[15] += INSN_SIZE;
  3846. ARMul_UndefInstr (state, 0);
  3847. state->Reg[15] += INSN_SIZE;
  3848. }
  3849. break;
  3850. case TRAP_SWI: {
  3851. //TEA_OUT(printf("TRAP_SWI\n"));
  3852. state->Reg[15] += INSN_SIZE;
  3853. ARMul_Abort (state, ARMul_SWIV);
  3854. state->Reg[15] += INSN_SIZE;
  3855. }
  3856. break;
  3857. //teawater add for xscale(arm v5) 2005.09.01------------------------------------
  3858. case TRAP_INSN_ABORT: {
  3859. /*XScale_set_fsr_far (state,
  3860. ARMul_CP15_R5_MMU_EXCPT,
  3861. state->Reg[15] -
  3862. INSN_SIZE);*/
  3863. state->Reg[15] += INSN_SIZE;
  3864. ARMul_Abort (state, ARMul_PrefetchAbortV);
  3865. state->Reg[15] += INSN_SIZE;
  3866. }
  3867. break;
  3868. //AJ2D--------------------------------------------------------------------------
  3869. case TRAP_DATA_ABORT: {
  3870. //TEA_OUT(printf("TRAP_DATA_ABORT\n"));
  3871. state->Reg[15] += INSN_SIZE;
  3872. ARMul_Abort (state, ARMul_DataAbortV);
  3873. state->Reg[15] += INSN_SIZE;
  3874. }
  3875. break;
  3876. case TRAP_IRQ: {
  3877. //TEA_OUT(printf("TRAP_IRQ\n"));
  3878. state->Reg[15] += INSN_SIZE;
  3879. ARMul_Abort (state, ARMul_IRQV);
  3880. state->Reg[15] += INSN_SIZE;
  3881. }
  3882. break;
  3883. case TRAP_FIQ: {
  3884. //TEA_OUT(printf("TRAP_FIQ\n"));
  3885. state->Reg[15] += INSN_SIZE;
  3886. ARMul_Abort (state, ARMul_FIQV);
  3887. state->Reg[15] += INSN_SIZE;
  3888. }
  3889. break;
  3890. case TRAP_SETS_R15: {
  3891. //TEA_OUT(printf("TRAP_SETS_R15\n"));
  3892. /*if (state->Bank > 0) {
  3893. state->Cpsr = state->Spsr[state->Bank];
  3894. ARMul_CPSRAltered (state);
  3895. } */
  3896. WriteSR15 (state, state->Reg[15]);
  3897. }
  3898. break;
  3899. case TRAP_SET_CPSR: {
  3900. //TEA_OUT(printf("TRAP_SET_CPSR\n"));
  3901. //chy 2006-02-15 USERBANK=SYSTEMBANK=0
  3902. //chy 2006-02-16 should use Mode to test
  3903. //if (state->Bank > 0) {
  3904. if (state->Mode != USER26MODE && state->Mode != USER32MODE) {
  3905. //ARMul_CPSRAltered (state);
  3906. }
  3907. state->Reg[15] += INSN_SIZE;
  3908. }
  3909. break;
  3910. case TRAP_OUT: {
  3911. //TEA_OUT(printf("TRAP_OUT\n"));
  3912. goto out;
  3913. }
  3914. break;
  3915. case TRAP_BREAKPOINT: {
  3916. //TEA_OUT(printf("TRAP_BREAKPOINT\n"));
  3917. state->Reg[15] -= INSN_SIZE;
  3918. if (!ARMul_OSHandleSWI
  3919. (state, SWI_Breakpoint)) {
  3920. ARMul_Abort (state, ARMul_SWIV);
  3921. }
  3922. state->Reg[15] += INSN_SIZE;
  3923. }
  3924. break;
  3925. }
  3926. next:
  3927. if (state->Emulate == ONCE) {
  3928. state->Emulate = STOP;
  3929. break;
  3930. } else if (state->Emulate != RUN) {
  3931. break;
  3932. }
  3933. } while (!state->stop_simulator);
  3934. out:
  3935. state->Reg[15] -= INSN_SIZE;
  3936. return (state->Reg[15]);
  3937. }
  3938. #endif
  3939. //AJ2D--------------------------------------------------------------------------
  3940. #endif
  3941. //AJ2D--------------------------------------------------------------------------
  3942. /* This routine evaluates most Data Processing register RHS's with the S
  3943. bit clear. It is intended to be called from the macro DPRegRHS, which
  3944. filters the common case of an unshifted register with in line code. */
  3945. static ARMword
  3946. GetDPRegRHS (ARMul_State * state, ARMword instr) {
  3947. ARMword shamt, base;
  3948. base = RHSReg;
  3949. if (BIT (4)) {
  3950. /* Shift amount in a register. */
  3951. UNDEF_Shift;
  3952. INCPC;
  3953. #ifndef MODE32
  3954. if (base == 15)
  3955. base = ECC | ER15INT | R15PC | EMODE;
  3956. else
  3957. #endif
  3958. base = state->Reg[base];
  3959. ARMul_Icycles (state, 1, 0L);
  3960. shamt = state->Reg[BITS (8, 11)] & 0xff;
  3961. switch ((int) BITS (5, 6)) {
  3962. case LSL:
  3963. if (shamt == 0)
  3964. return (base);
  3965. else if (shamt >= 32)
  3966. return (0);
  3967. else
  3968. return (base << shamt);
  3969. case LSR:
  3970. if (shamt == 0)
  3971. return (base);
  3972. else if (shamt >= 32)
  3973. return (0);
  3974. else
  3975. return (base >> shamt);
  3976. case ASR:
  3977. if (shamt == 0)
  3978. return (base);
  3979. else if (shamt >= 32)
  3980. return ((ARMword) ((int) base >> 31L));
  3981. else
  3982. return ((ARMword)
  3983. (( int) base >> (int) shamt));
  3984. case ROR:
  3985. shamt &= 0x1f;
  3986. if (shamt == 0)
  3987. return (base);
  3988. else
  3989. return ((base << (32 - shamt)) |
  3990. (base >> shamt));
  3991. }
  3992. } else {
  3993. /* Shift amount is a constant. */
  3994. #ifndef MODE32
  3995. if (base == 15)
  3996. base = ECC | ER15INT | R15PC | EMODE;
  3997. else
  3998. #endif
  3999. base = state->Reg[base];
  4000. shamt = BITS (7, 11);
  4001. switch ((int) BITS (5, 6)) {
  4002. case LSL:
  4003. return (base << shamt);
  4004. case LSR:
  4005. if (shamt == 0)
  4006. return (0);
  4007. else
  4008. return (base >> shamt);
  4009. case ASR:
  4010. if (shamt == 0)
  4011. return ((ARMword) (( int) base >> 31L));
  4012. else
  4013. return ((ARMword)
  4014. (( int) base >> (int) shamt));
  4015. case ROR:
  4016. if (shamt == 0)
  4017. /* It's an RRX. */
  4018. return ((base >> 1) | (CFLAG << 31));
  4019. else
  4020. return ((base << (32 - shamt)) |
  4021. (base >> shamt));
  4022. }
  4023. }
  4024. return 0;
  4025. }
  4026. /* This routine evaluates most Logical Data Processing register RHS's
  4027. with the S bit set. It is intended to be called from the macro
  4028. DPSRegRHS, which filters the common case of an unshifted register
  4029. with in line code. */
  4030. static ARMword
  4031. GetDPSRegRHS (ARMul_State * state, ARMword instr) {
  4032. ARMword shamt, base;
  4033. base = RHSReg;
  4034. if (BIT (4)) {
  4035. /* Shift amount in a register. */
  4036. UNDEF_Shift;
  4037. INCPC;
  4038. #ifndef MODE32
  4039. if (base == 15)
  4040. base = ECC | ER15INT | R15PC | EMODE;
  4041. else
  4042. #endif
  4043. base = state->Reg[base];
  4044. ARMul_Icycles (state, 1, 0L);
  4045. shamt = state->Reg[BITS (8, 11)] & 0xff;
  4046. switch ((int) BITS (5, 6)) {
  4047. case LSL:
  4048. if (shamt == 0)
  4049. return (base);
  4050. else if (shamt == 32) {
  4051. ASSIGNC (base & 1);
  4052. return (0);
  4053. } else if (shamt > 32) {
  4054. CLEARC;
  4055. return (0);
  4056. } else {
  4057. ASSIGNC ((base >> (32 - shamt)) & 1);
  4058. return (base << shamt);
  4059. }
  4060. case LSR:
  4061. if (shamt == 0)
  4062. return (base);
  4063. else if (shamt == 32) {
  4064. ASSIGNC (base >> 31);
  4065. return (0);
  4066. } else if (shamt > 32) {
  4067. CLEARC;
  4068. return (0);
  4069. } else {
  4070. ASSIGNC ((base >> (shamt - 1)) & 1);
  4071. return (base >> shamt);
  4072. }
  4073. case ASR:
  4074. if (shamt == 0)
  4075. return (base);
  4076. else if (shamt >= 32) {
  4077. ASSIGNC (base >> 31L);
  4078. return ((ARMword) (( int) base >> 31L));
  4079. } else {
  4080. ASSIGNC ((ARMword)
  4081. (( int) base >>
  4082. (int) (shamt - 1)) & 1);
  4083. return ((ARMword)
  4084. ((int) base >> (int) shamt));
  4085. }
  4086. case ROR:
  4087. if (shamt == 0)
  4088. return (base);
  4089. shamt &= 0x1f;
  4090. if (shamt == 0) {
  4091. ASSIGNC (base >> 31);
  4092. return (base);
  4093. } else {
  4094. ASSIGNC ((base >> (shamt - 1)) & 1);
  4095. return ((base << (32 - shamt)) |
  4096. (base >> shamt));
  4097. }
  4098. }
  4099. } else {
  4100. /* Shift amount is a constant. */
  4101. #ifndef MODE32
  4102. if (base == 15)
  4103. base = ECC | ER15INT | R15PC | EMODE;
  4104. else
  4105. #endif
  4106. base = state->Reg[base];
  4107. shamt = BITS (7, 11);
  4108. switch ((int) BITS (5, 6)) {
  4109. case LSL:
  4110. ASSIGNC ((base >> (32 - shamt)) & 1);
  4111. return (base << shamt);
  4112. case LSR:
  4113. if (shamt == 0) {
  4114. ASSIGNC (base >> 31);
  4115. return (0);
  4116. } else {
  4117. ASSIGNC ((base >> (shamt - 1)) & 1);
  4118. return (base >> shamt);
  4119. }
  4120. case ASR:
  4121. if (shamt == 0) {
  4122. ASSIGNC (base >> 31L);
  4123. return ((ARMword) ((int) base >> 31L));
  4124. } else {
  4125. ASSIGNC ((ARMword)
  4126. ((int) base >>
  4127. (int) (shamt - 1)) & 1);
  4128. return ((ARMword)
  4129. (( int) base >> (int) shamt));
  4130. }
  4131. case ROR:
  4132. if (shamt == 0) {
  4133. /* It's an RRX. */
  4134. shamt = CFLAG;
  4135. ASSIGNC (base & 1);
  4136. return ((base >> 1) | (shamt << 31));
  4137. } else {
  4138. ASSIGNC ((base >> (shamt - 1)) & 1);
  4139. return ((base << (32 - shamt)) |
  4140. (base >> shamt));
  4141. }
  4142. }
  4143. }
  4144. return 0;
  4145. }
  4146. /* This routine handles writes to register 15 when the S bit is not set. */
  4147. static void
  4148. WriteR15 (ARMul_State * state, ARMword src) {
  4149. /* The ARM documentation states that the two least significant bits
  4150. are discarded when setting PC, except in the cases handled by
  4151. WriteR15Branch() below. It's probably an oversight: in THUMB
  4152. mode, the second least significant bit should probably not be
  4153. discarded. */
  4154. #ifdef MODET
  4155. if (TFLAG)
  4156. src &= 0xfffffffe;
  4157. else
  4158. #endif
  4159. src &= 0xfffffffc;
  4160. #ifdef MODE32
  4161. state->Reg[15] = src & PCBITS;
  4162. #else
  4163. state->Reg[15] = (src & R15PCBITS) | ECC | ER15INT | EMODE;
  4164. ARMul_R15Altered (state);
  4165. #endif
  4166. FLUSHPIPE;
  4167. }
  4168. /* This routine handles writes to register 15 when the S bit is set. */
  4169. static void
  4170. WriteSR15 (ARMul_State * state, ARMword src) {
  4171. #ifdef MODE32
  4172. if (state->Bank > 0) {
  4173. state->Cpsr = state->Spsr[state->Bank];
  4174. ARMul_CPSRAltered (state);
  4175. }
  4176. #ifdef MODET
  4177. if (TFLAG)
  4178. src &= 0xfffffffe;
  4179. else
  4180. #endif
  4181. src &= 0xfffffffc;
  4182. state->Reg[15] = src & PCBITS;
  4183. #else
  4184. #ifdef MODET
  4185. if (TFLAG)
  4186. /* ARMul_R15Altered would have to support it. */
  4187. abort ();
  4188. else
  4189. #endif
  4190. src &= 0xfffffffc;
  4191. if (state->Bank == USERBANK)
  4192. state->Reg[15] =
  4193. (src & (CCBITS | R15PCBITS)) | ER15INT | EMODE;
  4194. else
  4195. state->Reg[15] = src;
  4196. ARMul_R15Altered (state);
  4197. #endif
  4198. FLUSHPIPE;
  4199. }
  4200. /* In machines capable of running in Thumb mode, BX, BLX, LDR and LDM
  4201. will switch to Thumb mode if the least significant bit is set. */
  4202. static void
  4203. WriteR15Branch (ARMul_State * state, ARMword src) {
  4204. #ifdef MODET
  4205. if (src & 1) {
  4206. /* Thumb bit. */
  4207. SETT;
  4208. state->Reg[15] = src & 0xfffffffe;
  4209. } else {
  4210. CLEART;
  4211. state->Reg[15] = src & 0xfffffffc;
  4212. }
  4213. state->Cpsr = ARMul_GetCPSR (state);
  4214. FLUSHPIPE;
  4215. #else
  4216. WriteR15 (state, src);
  4217. #endif
  4218. }
  4219. /* This routine evaluates most Load and Store register RHS's. It is
  4220. intended to be called from the macro LSRegRHS, which filters the
  4221. common case of an unshifted register with in line code. */
  4222. static ARMword
  4223. GetLSRegRHS (ARMul_State * state, ARMword instr) {
  4224. ARMword shamt, base;
  4225. base = RHSReg;
  4226. #ifndef MODE32
  4227. if (base == 15)
  4228. /* Now forbidden, but ... */
  4229. base = ECC | ER15INT | R15PC | EMODE;
  4230. else
  4231. #endif
  4232. base = state->Reg[base];
  4233. shamt = BITS (7, 11);
  4234. switch ((int) BITS (5, 6)) {
  4235. case LSL:
  4236. return (base << shamt);
  4237. case LSR:
  4238. if (shamt == 0)
  4239. return (0);
  4240. else
  4241. return (base >> shamt);
  4242. case ASR:
  4243. if (shamt == 0)
  4244. return ((ARMword) (( int) base >> 31L));
  4245. else
  4246. return ((ARMword) (( int) base >> (int) shamt));
  4247. case ROR:
  4248. if (shamt == 0)
  4249. /* It's an RRX. */
  4250. return ((base >> 1) | (CFLAG << 31));
  4251. else
  4252. return ((base << (32 - shamt)) | (base >> shamt));
  4253. default:
  4254. break;
  4255. }
  4256. return 0;
  4257. }
  4258. /* This routine evaluates the ARM7T halfword and signed transfer RHS's. */
  4259. static ARMword
  4260. GetLS7RHS (ARMul_State * state, ARMword instr) {
  4261. if (BIT (22) == 0) {
  4262. /* Register. */
  4263. #ifndef MODE32
  4264. if (RHSReg == 15)
  4265. /* Now forbidden, but ... */
  4266. return ECC | ER15INT | R15PC | EMODE;
  4267. #endif
  4268. return state->Reg[RHSReg];
  4269. }
  4270. /* Immediate. */
  4271. return BITS (0, 3) | (BITS (8, 11) << 4);
  4272. }
  4273. /* This function does the work of loading a word for a LDR instruction. */
  4274. #define MEM_LOAD_LOG(description) if (skyeye_config.log.memlogon >= 1) { \
  4275. fprintf(skyeye_logfd, \
  4276. "m LOAD %s: N %llx :p %x :i %x :a %x :d %x\n", \
  4277. description, state->NumInstrs, state->pc, instr, \
  4278. address, dest); \
  4279. }
  4280. #define MEM_STORE_LOG(description) if (skyeye_config.log.memlogon >= 1) { \
  4281. fprintf(skyeye_logfd, \
  4282. "m STORE %s: N %llx :p %x :i %x :a %x :d %x\n", \
  4283. description, state->NumInstrs, state->pc, instr, \
  4284. address, DEST); \
  4285. }
  4286. static unsigned
  4287. LoadWord (ARMul_State * state, ARMword instr, ARMword address) {
  4288. ARMword dest;
  4289. BUSUSEDINCPCS;
  4290. #ifndef MODE32
  4291. if (ADDREXCEPT (address))
  4292. INTERNALABORT (address);
  4293. #endif
  4294. dest = ARMul_LoadWordN (state, address);
  4295. if (state->Aborted) {
  4296. TAKEABORT;
  4297. return state->lateabtSig;
  4298. }
  4299. if (address & 3)
  4300. dest = ARMul_Align (state, address, dest);
  4301. WRITEDESTB (dest);
  4302. ARMul_Icycles (state, 1, 0L);
  4303. //MEM_LOAD_LOG("WORD");
  4304. return (DESTReg != LHSReg);
  4305. }
  4306. #ifdef MODET
  4307. /* This function does the work of loading a halfword. */
  4308. static unsigned
  4309. LoadHalfWord (ARMul_State * state, ARMword instr, ARMword address,
  4310. int signextend) {
  4311. ARMword dest;
  4312. BUSUSEDINCPCS;
  4313. #ifndef MODE32
  4314. if (ADDREXCEPT (address))
  4315. INTERNALABORT (address);
  4316. #endif
  4317. dest = ARMul_LoadHalfWord (state, address);
  4318. if (state->Aborted) {
  4319. TAKEABORT;
  4320. return state->lateabtSig;
  4321. }
  4322. UNDEF_LSRBPC;
  4323. if (signextend)
  4324. if (dest & 1 << (16 - 1))
  4325. dest = (dest & ((1 << 16) - 1)) - (1 << 16);
  4326. WRITEDEST (dest);
  4327. ARMul_Icycles (state, 1, 0L);
  4328. //MEM_LOAD_LOG("HALFWORD");
  4329. return (DESTReg != LHSReg);
  4330. }
  4331. #endif /* MODET */
  4332. /* This function does the work of loading a byte for a LDRB instruction. */
  4333. static unsigned
  4334. LoadByte (ARMul_State * state, ARMword instr, ARMword address, int signextend) {
  4335. ARMword dest;
  4336. BUSUSEDINCPCS;
  4337. #ifndef MODE32
  4338. if (ADDREXCEPT (address))
  4339. INTERNALABORT (address);
  4340. #endif
  4341. dest = ARMul_LoadByte (state, address);
  4342. if (state->Aborted) {
  4343. TAKEABORT;
  4344. return state->lateabtSig;
  4345. }
  4346. UNDEF_LSRBPC;
  4347. if (signextend)
  4348. if (dest & 1 << (8 - 1))
  4349. dest = (dest & ((1 << 8) - 1)) - (1 << 8);
  4350. WRITEDEST (dest);
  4351. ARMul_Icycles (state, 1, 0L);
  4352. //MEM_LOAD_LOG("BYTE");
  4353. return (DESTReg != LHSReg);
  4354. }
  4355. /* This function does the work of loading two words for a LDRD instruction. */
  4356. static void
  4357. Handle_Load_Double (ARMul_State * state, ARMword instr) {
  4358. ARMword dest_reg;
  4359. ARMword addr_reg;
  4360. ARMword write_back = BIT (21);
  4361. ARMword immediate = BIT (22);
  4362. ARMword add_to_base = BIT (23);
  4363. ARMword pre_indexed = BIT (24);
  4364. ARMword offset;
  4365. ARMword addr;
  4366. ARMword sum;
  4367. ARMword base;
  4368. ARMword value1;
  4369. ARMword value2;
  4370. BUSUSEDINCPCS;
  4371. /* If the writeback bit is set, the pre-index bit must be clear. */
  4372. if (write_back && !pre_indexed) {
  4373. ARMul_UndefInstr (state, instr);
  4374. return;
  4375. }
  4376. /* Extract the base address register. */
  4377. addr_reg = LHSReg;
  4378. /* Extract the destination register and check it. */
  4379. dest_reg = DESTReg;
  4380. /* Destination register must be even. */
  4381. if ((dest_reg & 1)
  4382. /* Destination register cannot be LR. */
  4383. || (dest_reg == 14)) {
  4384. ARMul_UndefInstr (state, instr);
  4385. return;
  4386. }
  4387. /* Compute the base address. */
  4388. base = state->Reg[addr_reg];
  4389. /* Compute the offset. */
  4390. offset = immediate ? ((BITS (8, 11) << 4) | BITS (0, 3)) : state->
  4391. Reg[RHSReg];
  4392. /* Compute the sum of the two. */
  4393. if (add_to_base)
  4394. sum = base + offset;
  4395. else
  4396. sum = base - offset;
  4397. /* If this is a pre-indexed mode use the sum. */
  4398. if (pre_indexed)
  4399. addr = sum;
  4400. else
  4401. addr = base;
  4402. /* The address must be aligned on a 8 byte boundary. */
  4403. /*if (addr & 0x7) {
  4404. #ifdef ABORTS
  4405. ARMul_DATAABORT (addr);
  4406. #else
  4407. ARMul_UndefInstr (state, instr);
  4408. #endif
  4409. return;
  4410. }*/
  4411. /* Lets just forcibly align it for now */
  4412. //addr = (addr + 7) & ~7;
  4413. /* For pre indexed or post indexed addressing modes,
  4414. check that the destination registers do not overlap
  4415. the address registers. */
  4416. if ((!pre_indexed || write_back)
  4417. && (addr_reg == dest_reg || addr_reg == dest_reg + 1)) {
  4418. ARMul_UndefInstr (state, instr);
  4419. return;
  4420. }
  4421. /* Load the words. */
  4422. value1 = ARMul_LoadWordN (state, addr);
  4423. value2 = ARMul_LoadWordN (state, addr + 4);
  4424. /* Check for data aborts. */
  4425. if (state->Aborted) {
  4426. TAKEABORT;
  4427. return;
  4428. }
  4429. ARMul_Icycles (state, 2, 0L);
  4430. /* Store the values. */
  4431. state->Reg[dest_reg] = value1;
  4432. state->Reg[dest_reg + 1] = value2;
  4433. /* Do the post addressing and writeback. */
  4434. if (!pre_indexed)
  4435. addr = sum;
  4436. if (!pre_indexed || write_back)
  4437. state->Reg[addr_reg] = addr;
  4438. }
  4439. /* This function does the work of storing two words for a STRD instruction. */
  4440. static void
  4441. Handle_Store_Double (ARMul_State * state, ARMword instr) {
  4442. ARMword src_reg;
  4443. ARMword addr_reg;
  4444. ARMword write_back = BIT (21);
  4445. ARMword immediate = BIT (22);
  4446. ARMword add_to_base = BIT (23);
  4447. ARMword pre_indexed = BIT (24);
  4448. ARMword offset;
  4449. ARMword addr;
  4450. ARMword sum;
  4451. ARMword base;
  4452. BUSUSEDINCPCS;
  4453. /* If the writeback bit is set, the pre-index bit must be clear. */
  4454. if (write_back && !pre_indexed) {
  4455. ARMul_UndefInstr (state, instr);
  4456. return;
  4457. }
  4458. /* Extract the base address register. */
  4459. addr_reg = LHSReg;
  4460. /* Base register cannot be PC. */
  4461. if (addr_reg == 15) {
  4462. ARMul_UndefInstr (state, instr);
  4463. return;
  4464. }
  4465. /* Extract the source register. */
  4466. src_reg = DESTReg;
  4467. /* Source register must be even. */
  4468. if (src_reg & 1) {
  4469. ARMul_UndefInstr (state, instr);
  4470. return;
  4471. }
  4472. /* Compute the base address. */
  4473. base = state->Reg[addr_reg];
  4474. /* Compute the offset. */
  4475. offset = immediate ? ((BITS (8, 11) << 4) | BITS (0, 3)) : state->
  4476. Reg[RHSReg];
  4477. /* Compute the sum of the two. */
  4478. if (add_to_base)
  4479. sum = base + offset;
  4480. else
  4481. sum = base - offset;
  4482. /* If this is a pre-indexed mode use the sum. */
  4483. if (pre_indexed)
  4484. addr = sum;
  4485. else
  4486. addr = base;
  4487. /* The address must be aligned on a 8 byte boundary. */
  4488. /*if (addr & 0x7) {
  4489. #ifdef ABORTS
  4490. ARMul_DATAABORT (addr);
  4491. #else
  4492. ARMul_UndefInstr (state, instr);
  4493. #endif
  4494. return;
  4495. }*/
  4496. /* Lets just forcibly align it for now */
  4497. //addr = (addr + 7) & ~7;
  4498. /* For pre indexed or post indexed addressing modes,
  4499. check that the destination registers do not overlap
  4500. the address registers. */
  4501. if ((!pre_indexed || write_back)
  4502. && (addr_reg == src_reg || addr_reg == src_reg + 1)) {
  4503. ARMul_UndefInstr (state, instr);
  4504. return;
  4505. }
  4506. /* Load the words. */
  4507. ARMul_StoreWordN (state, addr, state->Reg[src_reg]);
  4508. ARMul_StoreWordN (state, addr + 4, state->Reg[src_reg + 1]);
  4509. if (state->Aborted) {
  4510. TAKEABORT;
  4511. return;
  4512. }
  4513. /* Do the post addressing and writeback. */
  4514. if (!pre_indexed)
  4515. addr = sum;
  4516. if (!pre_indexed || write_back)
  4517. state->Reg[addr_reg] = addr;
  4518. }
  4519. /* This function does the work of storing a word from a STR instruction. */
  4520. static unsigned
  4521. StoreWord (ARMul_State * state, ARMword instr, ARMword address) {
  4522. //MEM_STORE_LOG("WORD");
  4523. BUSUSEDINCPCN;
  4524. #ifndef MODE32
  4525. if (DESTReg == 15)
  4526. state->Reg[15] = ECC | ER15INT | R15PC | EMODE;
  4527. #endif
  4528. #ifdef MODE32
  4529. ARMul_StoreWordN (state, address, DEST);
  4530. #else
  4531. if (VECTORACCESS (address) || ADDREXCEPT (address)) {
  4532. INTERNALABORT (address);
  4533. (void) ARMul_LoadWordN (state, address);
  4534. } else
  4535. ARMul_StoreWordN (state, address, DEST);
  4536. #endif
  4537. if (state->Aborted) {
  4538. TAKEABORT;
  4539. return state->lateabtSig;
  4540. }
  4541. return TRUE;
  4542. }
  4543. #ifdef MODET
  4544. /* This function does the work of storing a byte for a STRH instruction. */
  4545. static unsigned
  4546. StoreHalfWord (ARMul_State * state, ARMword instr, ARMword address) {
  4547. //MEM_STORE_LOG("HALFWORD");
  4548. BUSUSEDINCPCN;
  4549. #ifndef MODE32
  4550. if (DESTReg == 15)
  4551. state->Reg[15] = ECC | ER15INT | R15PC | EMODE;
  4552. #endif
  4553. #ifdef MODE32
  4554. ARMul_StoreHalfWord (state, address, DEST);
  4555. #else
  4556. if (VECTORACCESS (address) || ADDREXCEPT (address)) {
  4557. INTERNALABORT (address);
  4558. (void) ARMul_LoadHalfWord (state, address);
  4559. } else
  4560. ARMul_StoreHalfWord (state, address, DEST);
  4561. #endif
  4562. if (state->Aborted) {
  4563. TAKEABORT;
  4564. return state->lateabtSig;
  4565. }
  4566. return TRUE;
  4567. }
  4568. #endif /* MODET */
  4569. /* This function does the work of storing a byte for a STRB instruction. */
  4570. static unsigned
  4571. StoreByte (ARMul_State * state, ARMword instr, ARMword address) {
  4572. //MEM_STORE_LOG("BYTE");
  4573. BUSUSEDINCPCN;
  4574. #ifndef MODE32
  4575. if (DESTReg == 15)
  4576. state->Reg[15] = ECC | ER15INT | R15PC | EMODE;
  4577. #endif
  4578. #ifdef MODE32
  4579. ARMul_StoreByte (state, address, DEST);
  4580. #else
  4581. if (VECTORACCESS (address) || ADDREXCEPT (address)) {
  4582. INTERNALABORT (address);
  4583. (void) ARMul_LoadByte (state, address);
  4584. } else
  4585. ARMul_StoreByte (state, address, DEST);
  4586. #endif
  4587. if (state->Aborted) {
  4588. TAKEABORT;
  4589. return state->lateabtSig;
  4590. }
  4591. //UNDEF_LSRBPC;
  4592. return TRUE;
  4593. }
  4594. /* This function does the work of loading the registers listed in an LDM
  4595. instruction, when the S bit is clear. The code here is always increment
  4596. after, it's up to the caller to get the input address correct and to
  4597. handle base register modification. */
  4598. static void
  4599. LoadMult (ARMul_State * state, ARMword instr, ARMword address, ARMword WBBase) {
  4600. ARMword dest, temp;
  4601. //UNDEF_LSMNoRegs;
  4602. //UNDEF_LSMPCBase;
  4603. //UNDEF_LSMBaseInListWb;
  4604. BUSUSEDINCPCS;
  4605. #ifndef MODE32
  4606. if (ADDREXCEPT (address))
  4607. INTERNALABORT (address);
  4608. #endif
  4609. /*chy 2004-05-23 may write twice
  4610. if (BIT (21) && LHSReg != 15)
  4611. LSBase = WBBase;
  4612. */
  4613. /* N cycle first. */
  4614. for (temp = 0; !BIT (temp); temp++);
  4615. dest = ARMul_LoadWordN (state, address);
  4616. if (!state->abortSig && !state->Aborted)
  4617. state->Reg[temp++] = dest;
  4618. else if (!state->Aborted) {
  4619. //XScale_set_fsr_far (state, ARMul_CP15_R5_ST_ALIGN, address);
  4620. state->Aborted = ARMul_DataAbortV;
  4621. }
  4622. /*chy 2004-05-23 chy goto end*/
  4623. if (state->Aborted)
  4624. goto L_ldm_makeabort;
  4625. /* S cycles from here on. */
  4626. for (; temp < 16; temp++)
  4627. if (BIT (temp)) {
  4628. /* Load this register. */
  4629. address += 4;
  4630. dest = ARMul_LoadWordS (state, address);
  4631. if (!state->abortSig && !state->Aborted)
  4632. state->Reg[temp] = dest;
  4633. else if (!state->Aborted) {
  4634. /*XScale_set_fsr_far (state,
  4635. ARMul_CP15_R5_ST_ALIGN,
  4636. address);*/
  4637. state->Aborted = ARMul_DataAbortV;
  4638. }
  4639. /*chy 2004-05-23 chy goto end */
  4640. if (state->Aborted)
  4641. goto L_ldm_makeabort;
  4642. }
  4643. if (BIT (15) && !state->Aborted)
  4644. /* PC is in the reg list. */
  4645. WriteR15Branch (state, PC);
  4646. /* To write back the final register. */
  4647. /* ARMul_Icycles (state, 1, 0L);*/
  4648. /*chy 2004-05-23, see below
  4649. if (state->Aborted)
  4650. {
  4651. if (BIT (21) && LHSReg != 15)
  4652. LSBase = WBBase;
  4653. TAKEABORT;
  4654. }
  4655. */
  4656. /*chy 2004-05-23 should compare the Abort Models*/
  4657. L_ldm_makeabort:
  4658. /* To write back the final register. */
  4659. ARMul_Icycles (state, 1, 0L);
  4660. /* chy 2005-11-24, bug found by benjl@cse.unsw.edu.au, etc */
  4661. /*
  4662. if (state->Aborted)
  4663. {
  4664. if (BIT (21) && LHSReg != 15)
  4665. if (!(state->abortSig && state->Aborted && state->lateabtSig == LOW))
  4666. LSBase = WBBase;
  4667. TAKEABORT;
  4668. }else if (BIT (21) && LHSReg != 15)
  4669. LSBase = WBBase;
  4670. */
  4671. if (state->Aborted) {
  4672. if (BIT (21) && LHSReg != 15) {
  4673. if (!(state->abortSig)) {
  4674. }
  4675. }
  4676. TAKEABORT;
  4677. } else if (BIT (21) && LHSReg != 15) {
  4678. LSBase = WBBase;
  4679. }
  4680. /* chy 2005-11-24, over */
  4681. }
  4682. /* This function does the work of loading the registers listed in an LDM
  4683. instruction, when the S bit is set. The code here is always increment
  4684. after, it's up to the caller to get the input address correct and to
  4685. handle base register modification. */
  4686. static void
  4687. LoadSMult (ARMul_State * state,
  4688. ARMword instr, ARMword address, ARMword WBBase) {
  4689. ARMword dest, temp;
  4690. //UNDEF_LSMNoRegs;
  4691. //UNDEF_LSMPCBase;
  4692. //UNDEF_LSMBaseInListWb;
  4693. BUSUSEDINCPCS;
  4694. #ifndef MODE32
  4695. if (ADDREXCEPT (address))
  4696. INTERNALABORT (address);
  4697. #endif
  4698. /* chy 2004-05-23, may write twice
  4699. if (BIT (21) && LHSReg != 15)
  4700. LSBase = WBBase;
  4701. */
  4702. if (!BIT (15) && state->Bank != USERBANK) {
  4703. /* Temporary reg bank switch. */
  4704. (void) ARMul_SwitchMode (state, state->Mode, USER26MODE);
  4705. UNDEF_LSMUserBankWb;
  4706. }
  4707. /* N cycle first. */
  4708. for (temp = 0; !BIT (temp); temp++);
  4709. dest = ARMul_LoadWordN (state, address);
  4710. if (!state->abortSig)
  4711. state->Reg[temp++] = dest;
  4712. else if (!state->Aborted) {
  4713. //XScale_set_fsr_far (state, ARMul_CP15_R5_ST_ALIGN, address);
  4714. state->Aborted = ARMul_DataAbortV;
  4715. }
  4716. /*chy 2004-05-23 chy goto end*/
  4717. if (state->Aborted)
  4718. goto L_ldm_s_makeabort;
  4719. /* S cycles from here on. */
  4720. for (; temp < 16; temp++)
  4721. if (BIT (temp)) {
  4722. /* Load this register. */
  4723. address += 4;
  4724. dest = ARMul_LoadWordS (state, address);
  4725. if (!state->abortSig && !state->Aborted)
  4726. state->Reg[temp] = dest;
  4727. else if (!state->Aborted) {
  4728. /*XScale_set_fsr_far (state,
  4729. ARMul_CP15_R5_ST_ALIGN,
  4730. address);*/
  4731. state->Aborted = ARMul_DataAbortV;
  4732. }
  4733. /*chy 2004-05-23 chy goto end */
  4734. if (state->Aborted)
  4735. goto L_ldm_s_makeabort;
  4736. }
  4737. /*chy 2004-05-23 label of ldm_s_makeabort*/
  4738. L_ldm_s_makeabort:
  4739. /*chy 2004-06-06 LSBase process should be here, not in the end of this function. Because ARMul_CPSRAltered maybe change R13(SP) R14(lr). If not, simulate INSTR ldmia sp!,[....pc]^ error.*/
  4740. /*chy 2004-05-23 should compare the Abort Models*/
  4741. if (state->Aborted) {
  4742. if (BIT (21) && LHSReg != 15)
  4743. if (!
  4744. (state->abortSig && state->Aborted
  4745. && state->lateabtSig == LOW))
  4746. LSBase = WBBase;
  4747. TAKEABORT;
  4748. } else if (BIT (21) && LHSReg != 15)
  4749. LSBase = WBBase;
  4750. if (BIT (15) && !state->Aborted) {
  4751. /* PC is in the reg list. */
  4752. #ifdef MODE32
  4753. //chy 2006-02-16 , should not consider system mode, don't conside 26bit mode
  4754. if (state->Mode != USER26MODE && state->Mode != USER32MODE ) {
  4755. state->Cpsr = GETSPSR (state->Bank);
  4756. ARMul_CPSRAltered (state);
  4757. }
  4758. WriteR15 (state, PC);
  4759. #else
  4760. //chy 2006-02-16 , should not consider system mode, don't conside 26bit mode
  4761. if (state->Mode == USER26MODE || state->Mode == USER32MODE ) {
  4762. /* Protect bits in user mode. */
  4763. ASSIGNN ((state->Reg[15] & NBIT) != 0);
  4764. ASSIGNZ ((state->Reg[15] & ZBIT) != 0);
  4765. ASSIGNC ((state->Reg[15] & CBIT) != 0);
  4766. ASSIGNV ((state->Reg[15] & VBIT) != 0);
  4767. } else
  4768. ARMul_R15Altered (state);
  4769. FLUSHPIPE;
  4770. #endif
  4771. }
  4772. //chy 2006-02-16 , should not consider system mode, don't conside 26bit mode
  4773. if (!BIT (15) && state->Mode != USER26MODE
  4774. && state->Mode != USER32MODE )
  4775. /* Restore the correct bank. */
  4776. (void) ARMul_SwitchMode (state, USER26MODE, state->Mode);
  4777. /* To write back the final register. */
  4778. ARMul_Icycles (state, 1, 0L);
  4779. /* chy 2004-05-23, see below
  4780. if (state->Aborted)
  4781. {
  4782. if (BIT (21) && LHSReg != 15)
  4783. LSBase = WBBase;
  4784. TAKEABORT;
  4785. }
  4786. */
  4787. }
  4788. /* This function does the work of storing the registers listed in an STM
  4789. instruction, when the S bit is clear. The code here is always increment
  4790. after, it's up to the caller to get the input address correct and to
  4791. handle base register modification. */
  4792. static void
  4793. StoreMult (ARMul_State * state,
  4794. ARMword instr, ARMword address, ARMword WBBase) {
  4795. ARMword temp;
  4796. UNDEF_LSMNoRegs;
  4797. UNDEF_LSMPCBase;
  4798. UNDEF_LSMBaseInListWb;
  4799. if (!TFLAG)
  4800. /* N-cycle, increment the PC and update the NextInstr state. */
  4801. BUSUSEDINCPCN;
  4802. #ifndef MODE32
  4803. if (VECTORACCESS (address) || ADDREXCEPT (address))
  4804. INTERNALABORT (address);
  4805. if (BIT (15))
  4806. PATCHR15;
  4807. #endif
  4808. /* N cycle first. */
  4809. for (temp = 0; !BIT (temp); temp++);
  4810. #ifdef MODE32
  4811. ARMul_StoreWordN (state, address, state->Reg[temp++]);
  4812. #else
  4813. if (state->Aborted) {
  4814. (void) ARMul_LoadWordN (state, address);
  4815. /* Fake the Stores as Loads. */
  4816. for (; temp < 16; temp++)
  4817. if (BIT (temp)) {
  4818. /* Save this register. */
  4819. address += 4;
  4820. (void) ARMul_LoadWordS (state, address);
  4821. }
  4822. if (BIT (21) && LHSReg != 15)
  4823. LSBase = WBBase;
  4824. TAKEABORT;
  4825. return;
  4826. } else
  4827. ARMul_StoreWordN (state, address, state->Reg[temp++]);
  4828. #endif
  4829. if (state->abortSig && !state->Aborted) {
  4830. //XScale_set_fsr_far (state, ARMul_CP15_R5_ST_ALIGN, address);
  4831. state->Aborted = ARMul_DataAbortV;
  4832. }
  4833. //chy 2004-05-23, needn't store other when aborted
  4834. if (state->Aborted)
  4835. goto L_stm_takeabort;
  4836. /* S cycles from here on. */
  4837. for (; temp < 16; temp++)
  4838. if (BIT (temp)) {
  4839. /* Save this register. */
  4840. address += 4;
  4841. ARMul_StoreWordS (state, address, state->Reg[temp]);
  4842. if (state->abortSig && !state->Aborted) {
  4843. /*XScale_set_fsr_far (state,
  4844. ARMul_CP15_R5_ST_ALIGN,
  4845. address);*/
  4846. state->Aborted = ARMul_DataAbortV;
  4847. }
  4848. //chy 2004-05-23, needn't store other when aborted
  4849. if (state->Aborted)
  4850. goto L_stm_takeabort;
  4851. }
  4852. //chy 2004-05-23,should compare the Abort Models
  4853. L_stm_takeabort:
  4854. if (BIT (21) && LHSReg != 15) {
  4855. if (!
  4856. (state->abortSig && state->Aborted
  4857. && state->lateabtSig == LOW))
  4858. LSBase = WBBase;
  4859. }
  4860. if (state->Aborted)
  4861. TAKEABORT;
  4862. }
  4863. /* This function does the work of storing the registers listed in an STM
  4864. instruction when the S bit is set. The code here is always increment
  4865. after, it's up to the caller to get the input address correct and to
  4866. handle base register modification. */
  4867. static void
  4868. StoreSMult (ARMul_State * state,
  4869. ARMword instr, ARMword address, ARMword WBBase) {
  4870. ARMword temp;
  4871. UNDEF_LSMNoRegs;
  4872. UNDEF_LSMPCBase;
  4873. UNDEF_LSMBaseInListWb;
  4874. BUSUSEDINCPCN;
  4875. #ifndef MODE32
  4876. if (VECTORACCESS (address) || ADDREXCEPT (address))
  4877. INTERNALABORT (address);
  4878. if (BIT (15))
  4879. PATCHR15;
  4880. #endif
  4881. if (state->Bank != USERBANK) {
  4882. /* Force User Bank. */
  4883. (void) ARMul_SwitchMode (state, state->Mode, USER26MODE);
  4884. UNDEF_LSMUserBankWb;
  4885. }
  4886. for (temp = 0; !BIT (temp); temp++); /* N cycle first. */
  4887. #ifdef MODE32
  4888. ARMul_StoreWordN (state, address, state->Reg[temp++]);
  4889. #else
  4890. if (state->Aborted) {
  4891. (void) ARMul_LoadWordN (state, address);
  4892. for (; temp < 16; temp++)
  4893. /* Fake the Stores as Loads. */
  4894. if (BIT (temp)) {
  4895. /* Save this register. */
  4896. address += 4;
  4897. (void) ARMul_LoadWordS (state, address);
  4898. }
  4899. if (BIT (21) && LHSReg != 15)
  4900. LSBase = WBBase;
  4901. TAKEABORT;
  4902. return;
  4903. } else
  4904. ARMul_StoreWordN (state, address, state->Reg[temp++]);
  4905. #endif
  4906. if (state->abortSig && !state->Aborted) {
  4907. //XScale_set_fsr_far (state, ARMul_CP15_R5_ST_ALIGN, address);
  4908. state->Aborted = ARMul_DataAbortV;
  4909. }
  4910. //chy 2004-05-23, needn't store other when aborted
  4911. if (state->Aborted)
  4912. goto L_stm_s_takeabort;
  4913. /* S cycles from here on. */
  4914. for (; temp < 16; temp++)
  4915. if (BIT (temp)) {
  4916. /* Save this register. */
  4917. address += 4;
  4918. ARMul_StoreWordS (state, address, state->Reg[temp]);
  4919. if (state->abortSig && !state->Aborted) {
  4920. /*XScale_set_fsr_far (state,
  4921. ARMul_CP15_R5_ST_ALIGN,
  4922. address);*/
  4923. state->Aborted = ARMul_DataAbortV;
  4924. }
  4925. //chy 2004-05-23, needn't store other when aborted
  4926. if (state->Aborted)
  4927. goto L_stm_s_takeabort;
  4928. }
  4929. //chy 2006-02-16 , should not consider system mode, don't conside 26bit mode
  4930. if (state->Mode != USER26MODE && state->Mode != USER32MODE )
  4931. /* Restore the correct bank. */
  4932. (void) ARMul_SwitchMode (state, USER26MODE, state->Mode);
  4933. //chy 2004-05-23,should compare the Abort Models
  4934. L_stm_s_takeabort:
  4935. if (BIT (21) && LHSReg != 15) {
  4936. if (!
  4937. (state->abortSig && state->Aborted
  4938. && state->lateabtSig == LOW))
  4939. LSBase = WBBase;
  4940. }
  4941. if (state->Aborted)
  4942. TAKEABORT;
  4943. }
  4944. /* This function does the work of adding two 32bit values
  4945. together, and calculating if a carry has occurred. */
  4946. static ARMword
  4947. Add32 (ARMword a1, ARMword a2, int *carry) {
  4948. ARMword result = (a1 + a2);
  4949. unsigned int uresult = (unsigned int) result;
  4950. unsigned int ua1 = (unsigned int) a1;
  4951. /* If (result == RdLo) and (state->Reg[nRdLo] == 0),
  4952. or (result > RdLo) then we have no carry. */
  4953. if ((uresult == ua1) ? (a2 != 0) : (uresult < ua1))
  4954. *carry = 1;
  4955. else
  4956. *carry = 0;
  4957. return result;
  4958. }
  4959. /* This function does the work of multiplying
  4960. two 32bit values to give a 64bit result. */
  4961. static unsigned
  4962. Multiply64 (ARMul_State * state, ARMword instr, int msigned, int scc) {
  4963. /* Operand register numbers. */
  4964. int nRdHi, nRdLo, nRs, nRm;
  4965. ARMword RdHi = 0, RdLo = 0, Rm;
  4966. /* Cycle count. */
  4967. int scount;
  4968. nRdHi = BITS (16, 19);
  4969. nRdLo = BITS (12, 15);
  4970. nRs = BITS (8, 11);
  4971. nRm = BITS (0, 3);
  4972. /* Needed to calculate the cycle count. */
  4973. Rm = state->Reg[nRm];
  4974. /* Check for illegal operand combinations first. */
  4975. if (nRdHi != 15
  4976. && nRdLo != 15
  4977. && nRs != 15
  4978. //&& nRm != 15 && nRdHi != nRdLo && nRdHi != nRm && nRdLo != nRm) {
  4979. && nRm != 15 && nRdHi != nRdLo ) {
  4980. /* Intermediate results. */
  4981. ARMword lo, mid1, mid2, hi;
  4982. int carry;
  4983. ARMword Rs = state->Reg[nRs];
  4984. int sign = 0;
  4985. if (msigned) {
  4986. /* Compute sign of result and adjust operands if necessary. */
  4987. sign = (Rm ^ Rs) & 0x80000000;
  4988. if (((signed int) Rm) < 0)
  4989. Rm = -Rm;
  4990. if (((signed int) Rs) < 0)
  4991. Rs = -Rs;
  4992. }
  4993. /* We can split the 32x32 into four 16x16 operations. This
  4994. ensures that we do not lose precision on 32bit only hosts. */
  4995. lo = ((Rs & 0xFFFF) * (Rm & 0xFFFF));
  4996. mid1 = ((Rs & 0xFFFF) * ((Rm >> 16) & 0xFFFF));
  4997. mid2 = (((Rs >> 16) & 0xFFFF) * (Rm & 0xFFFF));
  4998. hi = (((Rs >> 16) & 0xFFFF) * ((Rm >> 16) & 0xFFFF));
  4999. /* We now need to add all of these results together, taking
  5000. care to propogate the carries from the additions. */
  5001. RdLo = Add32 (lo, (mid1 << 16), &carry);
  5002. RdHi = carry;
  5003. RdLo = Add32 (RdLo, (mid2 << 16), &carry);
  5004. RdHi += (carry + ((mid1 >> 16) & 0xFFFF) +
  5005. ((mid2 >> 16) & 0xFFFF) + hi);
  5006. if (sign) {
  5007. /* Negate result if necessary. */
  5008. RdLo = ~RdLo;
  5009. RdHi = ~RdHi;
  5010. if (RdLo == 0xFFFFFFFF) {
  5011. RdLo = 0;
  5012. RdHi += 1;
  5013. } else
  5014. RdLo += 1;
  5015. }
  5016. state->Reg[nRdLo] = RdLo;
  5017. state->Reg[nRdHi] = RdHi;
  5018. } else {
  5019. fprintf (stderr, "sim: MULTIPLY64 - INVALID ARGUMENTS, instr=0x%x\n", instr);
  5020. }
  5021. if (scc)
  5022. /* Ensure that both RdHi and RdLo are used to compute Z,
  5023. but don't let RdLo's sign bit make it to N. */
  5024. ARMul_NegZero (state, RdHi | (RdLo >> 16) | (RdLo & 0xFFFF));
  5025. /* The cycle count depends on whether the instruction is a signed or
  5026. unsigned multiply, and what bits are clear in the multiplier. */
  5027. if (msigned && (Rm & ((unsigned) 1 << 31)))
  5028. /* Invert the bits to make the check against zero. */
  5029. Rm = ~Rm;
  5030. if ((Rm & 0xFFFFFF00) == 0)
  5031. scount = 1;
  5032. else if ((Rm & 0xFFFF0000) == 0)
  5033. scount = 2;
  5034. else if ((Rm & 0xFF000000) == 0)
  5035. scount = 3;
  5036. else
  5037. scount = 4;
  5038. return 2 + scount;
  5039. }
  5040. /* This function does the work of multiplying two 32bit
  5041. values and adding a 64bit value to give a 64bit result. */
  5042. static unsigned
  5043. MultiplyAdd64 (ARMul_State * state, ARMword instr, int msigned, int scc) {
  5044. unsigned scount;
  5045. ARMword RdLo, RdHi;
  5046. int nRdHi, nRdLo;
  5047. int carry = 0;
  5048. nRdHi = BITS (16, 19);
  5049. nRdLo = BITS (12, 15);
  5050. RdHi = state->Reg[nRdHi];
  5051. RdLo = state->Reg[nRdLo];
  5052. scount = Multiply64 (state, instr, msigned, LDEFAULT);
  5053. RdLo = Add32 (RdLo, state->Reg[nRdLo], &carry);
  5054. RdHi = (RdHi + state->Reg[nRdHi]) + carry;
  5055. state->Reg[nRdLo] = RdLo;
  5056. state->Reg[nRdHi] = RdHi;
  5057. if (scc)
  5058. /* Ensure that both RdHi and RdLo are used to compute Z,
  5059. but don't let RdLo's sign bit make it to N. */
  5060. ARMul_NegZero (state, RdHi | (RdLo >> 16) | (RdLo & 0xFFFF));
  5061. /* Extra cycle for addition. */
  5062. return scount + 1;
  5063. }
  5064. /* Attempt to emulate an ARMv6 instruction.
  5065. Returns non-zero upon success. */
  5066. static int handle_v6_insn(ARMul_State* state, ARMword instr) {
  5067. switch (BITS(20, 27)) {
  5068. case 0x03:
  5069. printf ("Unhandled v6 insn: ldr\n");
  5070. break;
  5071. case 0x04: // UMAAL
  5072. {
  5073. const u8 rm_idx = BITS(8, 11);
  5074. const u8 rn_idx = BITS(0, 3);
  5075. const u8 rd_lo_idx = BITS(12, 15);
  5076. const u8 rd_hi_idx = BITS(16, 19);
  5077. const u32 rm_val = state->Reg[rm_idx];
  5078. const u32 rn_val = state->Reg[rn_idx];
  5079. const u32 rd_lo_val = state->Reg[rd_lo_idx];
  5080. const u32 rd_hi_val = state->Reg[rd_hi_idx];
  5081. const u64 result = (rn_val * rm_val) + rd_lo_val + rd_hi_val;
  5082. state->Reg[rd_lo_idx] = (result & 0xFFFFFFFF);
  5083. state->Reg[rd_hi_idx] = ((result >> 32) & 0xFFFFFFFF);
  5084. return 1;
  5085. }
  5086. break;
  5087. case 0x06:
  5088. printf ("Unhandled v6 insn: mls/str\n");
  5089. break;
  5090. case 0x16:
  5091. printf ("Unhandled v6 insn: smi\n");
  5092. break;
  5093. case 0x18:
  5094. if (BITS(4, 7) == 0x9) {
  5095. /* strex */
  5096. u32 l = LHSReg;
  5097. u32 r = RHSReg;
  5098. u32 lhs = LHS;
  5099. bool enter = false;
  5100. if (state->currentexval == (u32)ARMul_ReadWord(state, state->currentexaddr))enter = true;
  5101. //StoreWord(state, lhs, RHS)
  5102. if (state->Aborted) {
  5103. TAKEABORT;
  5104. }
  5105. if (enter) {
  5106. ARMul_StoreWordS(state, lhs, RHS);
  5107. state->Reg[DESTReg] = 0;
  5108. }
  5109. else {
  5110. state->Reg[DESTReg] = 1;
  5111. }
  5112. return 1;
  5113. }
  5114. printf ("Unhandled v6 insn: strex\n");
  5115. break;
  5116. case 0x19:
  5117. /* ldrex */
  5118. if (BITS(4, 7) == 0x9) {
  5119. u32 lhs = LHS;
  5120. state->currentexaddr = lhs;
  5121. state->currentexval = ARMul_ReadWord(state, lhs);
  5122. LoadWord(state, instr, lhs);
  5123. return 1;
  5124. }
  5125. printf ("Unhandled v6 insn: ldrex\n");
  5126. break;
  5127. case 0x1a:
  5128. printf ("Unhandled v6 insn: strexd\n");
  5129. break;
  5130. case 0x1b:
  5131. printf ("Unhandled v6 insn: ldrexd\n");
  5132. break;
  5133. case 0x1c:
  5134. if (BITS(4, 7) == 0x9) {
  5135. /* strexb */
  5136. u32 lhs = LHS;
  5137. bool enter = false;
  5138. if (state->currentexval == (u32)ARMul_ReadByte(state, state->currentexaddr))enter = true;
  5139. BUSUSEDINCPCN;
  5140. if (state->Aborted) {
  5141. TAKEABORT;
  5142. }
  5143. if (enter) {
  5144. ARMul_StoreByte(state, lhs, RHS);
  5145. state->Reg[DESTReg] = 0;
  5146. }
  5147. else {
  5148. state->Reg[DESTReg] = 1;
  5149. }
  5150. //printf("In %s, strexb not implemented\n", __FUNCTION__);
  5151. UNDEF_LSRBPC;
  5152. /* WRITESDEST (dest); */
  5153. return 1;
  5154. }
  5155. printf ("Unhandled v6 insn: strexb\n");
  5156. break;
  5157. case 0x1d:
  5158. if ((BITS(4, 7)) == 0x9) {
  5159. /* ldrexb */
  5160. u32 lhs = LHS;
  5161. LoadByte(state, instr, lhs, LUNSIGNED);
  5162. state->currentexaddr = lhs;
  5163. state->currentexval = (u32)ARMul_ReadByte(state, lhs);
  5164. //state->Reg[BITS(12, 15)] = ARMul_LoadByte(state, state->Reg[BITS(16, 19)]);
  5165. //printf("ldrexb\n");
  5166. //printf("instr is %x rm is %d\n", instr, BITS(16, 19));
  5167. //exit(-1);
  5168. //printf("In %s, ldrexb not implemented\n", __FUNCTION__);
  5169. return 1;
  5170. }
  5171. printf ("Unhandled v6 insn: ldrexb\n");
  5172. break;
  5173. case 0x1e:
  5174. printf ("Unhandled v6 insn: strexh\n");
  5175. break;
  5176. case 0x1f:
  5177. printf ("Unhandled v6 insn: ldrexh\n");
  5178. break;
  5179. case 0x30:
  5180. printf ("Unhandled v6 insn: movw\n");
  5181. break;
  5182. case 0x32:
  5183. printf ("Unhandled v6 insn: nop/sev/wfe/wfi/yield\n");
  5184. break;
  5185. case 0x34:
  5186. printf ("Unhandled v6 insn: movt\n");
  5187. break;
  5188. case 0x3f:
  5189. printf ("Unhandled v6 insn: rbit\n");
  5190. break;
  5191. case 0x61: // SADD16, SASX, SSAX, and SSUB16
  5192. if ((instr & 0xFF0) == 0xf10 || (instr & 0xFF0) == 0xf30 ||
  5193. (instr & 0xFF0) == 0xf50 || (instr & 0xFF0) == 0xf70)
  5194. {
  5195. const u8 rd_idx = BITS(12, 15);
  5196. const u8 rm_idx = BITS(0, 3);
  5197. const u8 rn_idx = BITS(16, 19);
  5198. const s16 rn_lo = (state->Reg[rn_idx] & 0xFFFF);
  5199. const s16 rn_hi = ((state->Reg[rn_idx] >> 16) & 0xFFFF);
  5200. const s16 rm_lo = (state->Reg[rm_idx] & 0xFFFF);
  5201. const s16 rm_hi = ((state->Reg[rm_idx] >> 16) & 0xFFFF);
  5202. s32 lo_result;
  5203. s32 hi_result;
  5204. // SADD16
  5205. if ((instr & 0xFF0) == 0xf10) {
  5206. lo_result = (rn_lo + rm_lo);
  5207. hi_result = (rn_hi + rm_hi);
  5208. }
  5209. // SASX
  5210. else if ((instr & 0xFF0) == 0xf30) {
  5211. lo_result = (rn_lo - rm_hi);
  5212. hi_result = (rn_hi + rm_lo);
  5213. }
  5214. // SSAX
  5215. else if ((instr & 0xFF0) == 0xf50) {
  5216. lo_result = (rn_lo + rm_hi);
  5217. hi_result = (rn_hi - rm_lo);
  5218. }
  5219. // SSUB16
  5220. else {
  5221. lo_result = (rn_lo - rm_lo);
  5222. hi_result = (rn_hi - rm_hi);
  5223. }
  5224. state->Reg[rd_idx] = (lo_result & 0xFFFF) | ((hi_result & 0xFFFF) << 16);
  5225. if (lo_result >= 0) {
  5226. state->GEFlag |= (1 << 16);
  5227. state->GEFlag |= (1 << 17);
  5228. } else {
  5229. state->GEFlag &= ~(1 << 16);
  5230. state->GEFlag &= ~(1 << 17);
  5231. }
  5232. if (hi_result >= 0) {
  5233. state->GEFlag |= (1 << 18);
  5234. state->GEFlag |= (1 << 19);
  5235. } else {
  5236. state->GEFlag &= ~(1 << 18);
  5237. state->GEFlag &= ~(1 << 19);
  5238. }
  5239. return 1;
  5240. }
  5241. // SADD8/SSUB8
  5242. else if ((instr & 0xFF0) == 0xf90 || (instr & 0xFF0) == 0xff0)
  5243. {
  5244. const u8 rd_idx = BITS(12, 15);
  5245. const u8 rm_idx = BITS(0, 3);
  5246. const u8 rn_idx = BITS(16, 19);
  5247. const u32 rm_val = state->Reg[rm_idx];
  5248. const u32 rn_val = state->Reg[rn_idx];
  5249. s32 lo_val1, lo_val2;
  5250. s32 hi_val1, hi_val2;
  5251. // SADD8
  5252. if ((instr & 0xFF0) == 0xf90) {
  5253. lo_val1 = (s32)(s8)(rn_val & 0xFF) + (s32)(s8)(rm_val & 0xFF);
  5254. lo_val2 = (s32)(s8)((rn_val >> 8) & 0xFF) + (s32)(s8)((rm_val >> 8) & 0xFF);
  5255. hi_val1 = (s32)(s8)((rn_val >> 16) & 0xFF) + (s32)(s8)((rm_val >> 16) & 0xFF);
  5256. hi_val2 = (s32)(s8)((rn_val >> 24) & 0xFF) + (s32)(s8)((rm_val >> 24) & 0xFF);
  5257. }
  5258. // SSUB8
  5259. else {
  5260. lo_val1 = (s32)(s8)(rn_val & 0xFF) - (s32)(s8)(rm_val & 0xFF);
  5261. lo_val2 = (s32)(s8)((rn_val >> 8) & 0xFF) - (s32)(s8)((rm_val >> 8) & 0xFF);
  5262. hi_val1 = (s32)(s8)((rn_val >> 16) & 0xFF) - (s32)(s8)((rm_val >> 16) & 0xFF);
  5263. hi_val2 = (s32)(s8)((rn_val >> 24) & 0xFF) - (s32)(s8)((rm_val >> 24) & 0xFF);
  5264. }
  5265. if (lo_val1 >= 0)
  5266. state->GEFlag |= (1 << 16);
  5267. else
  5268. state->GEFlag &= ~(1 << 16);
  5269. if (lo_val2 >= 0)
  5270. state->GEFlag |= (1 << 17);
  5271. else
  5272. state->GEFlag &= ~(1 << 17);
  5273. if (hi_val1 >= 0)
  5274. state->GEFlag |= (1 << 18);
  5275. else
  5276. state->GEFlag &= ~(1 << 18);
  5277. if (hi_val2 >= 0)
  5278. state->GEFlag |= (1 << 19);
  5279. else
  5280. state->GEFlag &= ~(1 << 19);
  5281. state->Reg[rd_idx] = ((lo_val1 & 0xFF) | ((lo_val2 & 0xFF) << 8) | ((hi_val1 & 0xFF) << 16) | ((hi_val2 & 0xFF) << 24));
  5282. return 1;
  5283. }
  5284. else {
  5285. printf("Unhandled v6 insn: %08x", instr);
  5286. }
  5287. break;
  5288. case 0x62: // QADD16, QASX, QSAX, QSUB16, QADD8, and QSUB8
  5289. {
  5290. const u8 op2 = BITS(5, 7);
  5291. const u8 rd_idx = BITS(12, 15);
  5292. const u8 rn_idx = BITS(16, 19);
  5293. const u8 rm_idx = BITS(0, 3);
  5294. const u16 rm_lo = (state->Reg[rm_idx] & 0xFFFF);
  5295. const u16 rm_hi = ((state->Reg[rm_idx] >> 0x10) & 0xFFFF);
  5296. const u16 rn_lo = (state->Reg[rn_idx] & 0xFFFF);
  5297. const u16 rn_hi = ((state->Reg[rn_idx] >> 0x10) & 0xFFFF);
  5298. u16 lo_result = 0;
  5299. u16 hi_result = 0;
  5300. // QADD16
  5301. if (op2 == 0x00) {
  5302. lo_result = ARMul_SignedSaturatedAdd16(rn_lo, rm_lo);
  5303. hi_result = ARMul_SignedSaturatedAdd16(rn_hi, rm_hi);
  5304. }
  5305. // QASX
  5306. else if (op2 == 0x01) {
  5307. lo_result = ARMul_SignedSaturatedSub16(rn_lo, rm_hi);
  5308. hi_result = ARMul_SignedSaturatedAdd16(rn_hi, rm_lo);
  5309. }
  5310. // QSAX
  5311. else if (op2 == 0x02) {
  5312. lo_result = ARMul_SignedSaturatedAdd16(rn_lo, rm_hi);
  5313. hi_result = ARMul_SignedSaturatedSub16(rn_hi, rm_lo);
  5314. }
  5315. // QSUB16
  5316. else if (op2 == 0x03) {
  5317. lo_result = ARMul_SignedSaturatedSub16(rn_lo, rm_lo);
  5318. hi_result = ARMul_SignedSaturatedSub16(rn_hi, rm_hi);
  5319. }
  5320. // QADD8
  5321. else if (op2 == 0x04) {
  5322. lo_result = ARMul_SignedSaturatedAdd8(rn_lo & 0xFF, rm_lo & 0xFF) |
  5323. ARMul_SignedSaturatedAdd8(rn_lo >> 8, rm_lo >> 8) << 8;
  5324. hi_result = ARMul_SignedSaturatedAdd8(rn_hi & 0xFF, rm_hi & 0xFF) |
  5325. ARMul_SignedSaturatedAdd8(rn_hi >> 8, rm_hi >> 8) << 8;
  5326. }
  5327. // QSUB8
  5328. else if (op2 == 0x07) {
  5329. lo_result = ARMul_SignedSaturatedSub8(rn_lo & 0xFF, rm_lo & 0xFF) |
  5330. ARMul_SignedSaturatedSub8(rn_lo >> 8, rm_lo >> 8) << 8;
  5331. hi_result = ARMul_SignedSaturatedSub8(rn_hi & 0xFF, rm_hi & 0xFF) |
  5332. ARMul_SignedSaturatedSub8(rn_hi >> 8, rm_hi >> 8) << 8;
  5333. }
  5334. state->Reg[rd_idx] = (lo_result & 0xFFFF) | ((hi_result & 0xFFFF) << 16);
  5335. return 1;
  5336. }
  5337. break;
  5338. case 0x63:
  5339. printf ("Unhandled v6 insn: shadd/shsub\n");
  5340. break;
  5341. case 0x65:
  5342. {
  5343. u32 rd = (instr >> 12) & 0xF;
  5344. u32 rn = (instr >> 16) & 0xF;
  5345. u32 rm = (instr >> 0) & 0xF;
  5346. u32 from = state->Reg[rn];
  5347. u32 to = state->Reg[rm];
  5348. if ((instr & 0xFF0) == 0xF10 || (instr & 0xFF0) == 0xF70) { // UADD16/USUB16
  5349. u32 h1, h2;
  5350. state->Cpsr &= 0xfff0ffff;
  5351. if ((instr & 0x0F0) == 0x070) { // USUB16
  5352. h1 = ((u16)from - (u16)to);
  5353. h2 = ((u16)(from >> 16) - (u16)(to >> 16));
  5354. if (!(h1 & 0xffff0000))
  5355. state->GEFlag |= (3 << 16);
  5356. else
  5357. state->GEFlag &= ~(3 << 16);
  5358. if (!(h2 & 0xffff0000))
  5359. state->GEFlag |= (3 << 18);
  5360. else
  5361. state->GEFlag &= ~(3 << 18);
  5362. }
  5363. else { // UADD16
  5364. h1 = ((u16)from + (u16)to);
  5365. h2 = ((u16)(from >> 16) + (u16)(to >> 16));
  5366. if (h1 & 0xffff0000)
  5367. state->GEFlag |= (3 << 16);
  5368. else
  5369. state->GEFlag &= ~(3 << 16);
  5370. if (h2 & 0xffff0000)
  5371. state->GEFlag |= (3 << 18);
  5372. else
  5373. state->GEFlag &= ~(3 << 18);
  5374. }
  5375. state->Reg[rd] = (u32)((h1 & 0xffff) | ((h2 & 0xffff) << 16));
  5376. return 1;
  5377. }
  5378. else
  5379. if ((instr & 0xFF0) == 0xF90 || (instr & 0xFF0) == 0xFF0) { // UADD8/USUB8
  5380. u32 b1, b2, b3, b4;
  5381. state->Cpsr &= 0xfff0ffff;
  5382. if ((instr & 0x0F0) == 0x0F0) { // USUB8
  5383. b1 = ((u8)from - (u8)to);
  5384. b2 = ((u8)(from >> 8) - (u8)(to >> 8));
  5385. b3 = ((u8)(from >> 16) - (u8)(to >> 16));
  5386. b4 = ((u8)(from >> 24) - (u8)(to >> 24));
  5387. if (!(b1 & 0xffffff00))
  5388. state->GEFlag |= (1 << 16);
  5389. else
  5390. state->GEFlag &= ~(1 << 16);
  5391. if (!(b2 & 0xffffff00))
  5392. state->GEFlag |= (1 << 17);
  5393. else
  5394. state->GEFlag &= ~(1 << 17);
  5395. if (!(b3 & 0xffffff00))
  5396. state->GEFlag |= (1 << 18);
  5397. else
  5398. state->GEFlag &= ~(1 << 18);
  5399. if (!(b4 & 0xffffff00))
  5400. state->GEFlag |= (1 << 19);
  5401. else
  5402. state->GEFlag &= ~(1 << 19);
  5403. }
  5404. else { // UADD8
  5405. b1 = ((u8)from + (u8)to);
  5406. b2 = ((u8)(from >> 8) + (u8)(to >> 8));
  5407. b3 = ((u8)(from >> 16) + (u8)(to >> 16));
  5408. b4 = ((u8)(from >> 24) + (u8)(to >> 24));
  5409. if (b1 & 0xffffff00)
  5410. state->GEFlag |= (1 << 16);
  5411. else
  5412. state->GEFlag &= ~(1 << 16);
  5413. if (b2 & 0xffffff00)
  5414. state->GEFlag |= (1 << 17);
  5415. else
  5416. state->GEFlag &= ~(1 << 17);
  5417. if (b3 & 0xffffff00)
  5418. state->GEFlag |= (1 << 18);
  5419. else
  5420. state->GEFlag &= ~(1 << 18);
  5421. if (b4 & 0xffffff00)
  5422. state->GEFlag |= (1 << 19);
  5423. else
  5424. state->GEFlag &= ~(1 << 19);
  5425. }
  5426. state->Reg[rd] = (u32)(b1 | (b2 & 0xff) << 8 | (b3 & 0xff) << 16 | (b4 & 0xff) << 24);
  5427. return 1;
  5428. }
  5429. }
  5430. printf("Unhandled v6 insn: uasx/usax\n");
  5431. break;
  5432. case 0x66: // UQADD16, UQASX, UQSAX, UQSUB16, UQADD8, and UQSUB8
  5433. {
  5434. const u8 rd_idx = BITS(12, 15);
  5435. const u8 rm_idx = BITS(0, 3);
  5436. const u8 rn_idx = BITS(16, 19);
  5437. const u8 op2 = BITS(5, 7);
  5438. const u32 rm_val = state->Reg[rm_idx];
  5439. const u32 rn_val = state->Reg[rn_idx];
  5440. u16 lo_val = 0;
  5441. u16 hi_val = 0;
  5442. // UQADD16
  5443. if (op2 == 0x00) {
  5444. lo_val = ARMul_UnsignedSaturatedAdd16(rn_val & 0xFFFF, rm_val & 0xFFFF);
  5445. hi_val = ARMul_UnsignedSaturatedAdd16((rn_val >> 16) & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  5446. }
  5447. // UQASX
  5448. else if (op2 == 0x01) {
  5449. lo_val = ARMul_UnsignedSaturatedSub16(rn_val & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  5450. hi_val = ARMul_UnsignedSaturatedAdd16((rn_val >> 16) & 0xFFFF, rm_val & 0xFFFF);
  5451. }
  5452. // UQSAX
  5453. else if (op2 == 0x02) {
  5454. lo_val = ARMul_UnsignedSaturatedAdd16(rn_val & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  5455. hi_val = ARMul_UnsignedSaturatedSub16((rn_val >> 16) & 0xFFFF, rm_val & 0xFFFF);
  5456. }
  5457. // UQSUB16
  5458. else if (op2 == 0x03) {
  5459. lo_val = ARMul_UnsignedSaturatedSub16(rn_val & 0xFFFF, rm_val & 0xFFFF);
  5460. hi_val = ARMul_UnsignedSaturatedSub16((rn_val >> 16) & 0xFFFF, (rm_val >> 16) & 0xFFFF);
  5461. }
  5462. // UQADD8
  5463. else if (op2 == 0x04) {
  5464. lo_val = ARMul_UnsignedSaturatedAdd8(rn_val, rm_val) |
  5465. ARMul_UnsignedSaturatedAdd8(rn_val >> 8, rm_val >> 8) << 8;
  5466. hi_val = ARMul_UnsignedSaturatedAdd8(rn_val >> 16, rm_val >> 16) |
  5467. ARMul_UnsignedSaturatedAdd8(rn_val >> 24, rm_val >> 24) << 8;
  5468. }
  5469. // UQSUB8
  5470. else {
  5471. lo_val = ARMul_UnsignedSaturatedSub8(rn_val, rm_val) |
  5472. ARMul_UnsignedSaturatedSub8(rn_val >> 8, rm_val >> 8) << 8;
  5473. hi_val = ARMul_UnsignedSaturatedSub8(rn_val >> 16, rm_val >> 16) |
  5474. ARMul_UnsignedSaturatedSub8(rn_val >> 24, rm_val >> 24) << 8;
  5475. }
  5476. state->Reg[rd_idx] = ((lo_val & 0xFFFF) | hi_val << 16);
  5477. return 1;
  5478. }
  5479. break;
  5480. case 0x67: // UHADD16, UHASX, UHSAX, UHSUB16, UHADD8, and UHSUB8.
  5481. {
  5482. const u8 op2 = BITS(5, 7);
  5483. const u8 rm_idx = BITS(0, 3);
  5484. const u8 rn_idx = BITS(16, 19);
  5485. const u8 rd_idx = BITS(12, 15);
  5486. const u32 rm_val = state->Reg[rm_idx];
  5487. const u32 rn_val = state->Reg[rn_idx];
  5488. if (op2 == 0x00 || op2 == 0x01 || op2 == 0x02 || op2 == 0x03)
  5489. {
  5490. u32 lo_val = 0;
  5491. u32 hi_val = 0;
  5492. // UHADD16
  5493. if (op2 == 0x00) {
  5494. lo_val = (rn_val & 0xFFFF) + (rm_val & 0xFFFF);
  5495. hi_val = ((rn_val >> 16) & 0xFFFF) + ((rm_val >> 16) & 0xFFFF);
  5496. }
  5497. // UHASX
  5498. else if (op2 == 0x01) {
  5499. lo_val = (rn_val & 0xFFFF) - ((rm_val >> 16) & 0xFFFF);
  5500. hi_val = ((rn_val >> 16) & 0xFFFF) + (rm_val & 0xFFFF);
  5501. }
  5502. // UHSAX
  5503. else if (op2 == 0x02) {
  5504. lo_val = (rn_val & 0xFFFF) + ((rm_val >> 16) & 0xFFFF);
  5505. hi_val = ((rn_val >> 16) & 0xFFFF) - (rm_val & 0xFFFF);
  5506. }
  5507. // UHSUB16
  5508. else if (op2 == 0x03) {
  5509. lo_val = (rn_val & 0xFFFF) - (rm_val & 0xFFFF);
  5510. hi_val = ((rn_val >> 16) & 0xFFFF) - ((rm_val >> 16) & 0xFFFF);
  5511. }
  5512. lo_val >>= 1;
  5513. hi_val >>= 1;
  5514. state->Reg[rd_idx] = (lo_val & 0xFFFF) | ((hi_val & 0xFFFF) << 16);
  5515. return 1;
  5516. }
  5517. else if (op2 == 0x04 || op2 == 0x07) {
  5518. u32 sum1;
  5519. u32 sum2;
  5520. u32 sum3;
  5521. u32 sum4;
  5522. // UHADD8
  5523. if (op2 == 0x04) {
  5524. sum1 = (rn_val & 0xFF) + (rm_val & 0xFF);
  5525. sum2 = ((rn_val >> 8) & 0xFF) + ((rm_val >> 8) & 0xFF);
  5526. sum3 = ((rn_val >> 16) & 0xFF) + ((rm_val >> 16) & 0xFF);
  5527. sum4 = ((rn_val >> 24) & 0xFF) + ((rm_val >> 24) & 0xFF);
  5528. }
  5529. // UHSUB8
  5530. else {
  5531. sum1 = (rn_val & 0xFF) - (rm_val & 0xFF);
  5532. sum2 = ((rn_val >> 8) & 0xFF) - ((rm_val >> 8) & 0xFF);
  5533. sum3 = ((rn_val >> 16) & 0xFF) - ((rm_val >> 16) & 0xFF);
  5534. sum4 = ((rn_val >> 24) & 0xFF) - ((rm_val >> 24) & 0xFF);
  5535. }
  5536. sum1 >>= 1;
  5537. sum2 >>= 1;
  5538. sum3 >>= 1;
  5539. sum4 >>= 1;
  5540. state->Reg[rd_idx] = (sum1 & 0xFF) | ((sum2 & 0xFF) << 8) | ((sum3 & 0xFF) << 16) | ((sum4 & 0xFF) << 24);
  5541. return 1;
  5542. }
  5543. }
  5544. break;
  5545. case 0x68:
  5546. {
  5547. u32 rd = (instr >> 12) & 0xF;
  5548. u32 rn = (instr >> 16) & 0xF;
  5549. u32 rm = (instr >> 0) & 0xF;
  5550. u32 from = state->Reg[rn];
  5551. u32 to = state->Reg[rm];
  5552. u32 cpsr = ARMul_GetCPSR(state);
  5553. if ((instr & 0xFF0) == 0xFB0) { // SEL
  5554. u32 result;
  5555. if (cpsr & (1 << 16))
  5556. result = from & 0xff;
  5557. else
  5558. result = to & 0xff;
  5559. if (cpsr & (1 << 17))
  5560. result |= from & 0x0000ff00;
  5561. else
  5562. result |= to & 0x0000ff00;
  5563. if (cpsr & (1 << 18))
  5564. result |= from & 0x00ff0000;
  5565. else
  5566. result |= to & 0x00ff0000;
  5567. if (cpsr & (1 << 19))
  5568. result |= from & 0xff000000;
  5569. else
  5570. result |= to & 0xff000000;
  5571. state->Reg[rd] = result;
  5572. return 1;
  5573. }
  5574. }
  5575. printf("Unhandled v6 insn: pkh/sxtab/selsxtb\n");
  5576. break;
  5577. case 0x6a: // SSAT, SSAT16, SXTB, and SXTAB
  5578. {
  5579. const u8 op2 = BITS(5, 7);
  5580. // SSAT16
  5581. if (op2 == 0x01) {
  5582. const u8 rd_idx = BITS(12, 15);
  5583. const u8 rn_idx = BITS(0, 3);
  5584. const u8 num_bits = BITS(16, 19) + 1;
  5585. const s16 min = -(0x8000 >> (16 - num_bits));
  5586. const s16 max = (0x7FFF >> (16 - num_bits));
  5587. s16 rn_lo = (state->Reg[rn_idx]);
  5588. s16 rn_hi = (state->Reg[rn_idx] >> 16);
  5589. if (rn_lo > max) {
  5590. rn_lo = max;
  5591. SETQ;
  5592. } else if (rn_lo < min) {
  5593. rn_lo = min;
  5594. SETQ;
  5595. }
  5596. if (rn_hi > max) {
  5597. rn_hi = max;
  5598. SETQ;
  5599. } else if (rn_hi < min) {
  5600. rn_hi = min;
  5601. SETQ;
  5602. }
  5603. state->Reg[rd_idx] = (rn_lo & 0xFFFF) | ((rn_hi & 0xFFFF) << 16);
  5604. return 1;
  5605. }
  5606. else if (op2 == 0x03) {
  5607. const u8 rotation = BITS(10, 11) * 8;
  5608. u32 rm = ((state->Reg[BITS(0, 3)] >> rotation) & 0xFF) | (((state->Reg[BITS(0, 3)] << (32 - rotation)) & 0xFF) & 0xFF);
  5609. if (rm & 0x80)
  5610. rm |= 0xffffff00;
  5611. // SXTB, otherwise SXTAB
  5612. if (BITS(16, 19) == 0xf)
  5613. state->Reg[BITS(12, 15)] = rm;
  5614. else
  5615. state->Reg[BITS(12, 15)] = state->Reg[BITS(16, 19)] + rm;
  5616. return 1;
  5617. }
  5618. else {
  5619. printf("Unimplemented op: SSAT");
  5620. }
  5621. }
  5622. break;
  5623. case 0x6b: // REV, REV16, SXTH, and SXTAH
  5624. {
  5625. const u8 op2 = BITS(5, 7);
  5626. // REV
  5627. if (op2 == 0x01) {
  5628. DEST = ((RHS & 0xFF) << 24) | ((RHS & 0xFF00)) << 8 | ((RHS & 0xFF0000) >> 8) | ((RHS & 0xFF000000) >> 24);
  5629. return 1;
  5630. }
  5631. // REV16
  5632. else if (op2 == 0x05) {
  5633. DEST = ((RHS & 0xFF) << 8) | ((RHS & 0xFF00)) >> 8 | ((RHS & 0xFF0000) << 8) | ((RHS & 0xFF000000) >> 8);
  5634. return 1;
  5635. }
  5636. else if (op2 == 0x03) {
  5637. const u8 rotate = BITS(10, 11) * 8;
  5638. u32 rm = ((state->Reg[BITS(0, 3)] >> rotate) & 0xFFFF) | (((state->Reg[BITS(0, 3)] << (32 - rotate)) & 0xFFFF) & 0xFFFF);
  5639. if (rm & 0x8000)
  5640. rm |= 0xffff0000;
  5641. // SXTH, otherwise SXTAH
  5642. if (BITS(16, 19) == 15)
  5643. state->Reg[BITS(12, 15)] = rm;
  5644. else
  5645. state->Reg[BITS(12, 15)] = state->Reg[BITS(16, 19)] + rm;
  5646. return 1;
  5647. }
  5648. }
  5649. break;
  5650. case 0x6c: // UXTB16 and UXTAB16
  5651. {
  5652. const u8 rm_idx = BITS(0, 3);
  5653. const u8 rn_idx = BITS(16, 19);
  5654. const u8 rd_idx = BITS(12, 15);
  5655. const u32 rm_val = state->Reg[rm_idx];
  5656. const u32 rn_val = state->Reg[rn_idx];
  5657. const u32 rotation = BITS(10, 11) * 8;
  5658. const u32 rotated_rm = ((rm_val << (32 - rotation)) | (rm_val >> rotation));
  5659. // UXTB16
  5660. if ((instr & 0xf03f0) == 0xf0070) {
  5661. state->Reg[rd_idx] = rotated_rm & 0x00FF00FF;
  5662. }
  5663. else { // UXTAB16
  5664. const u8 lo_rotated = (rotated_rm & 0xFF);
  5665. const u16 lo_result = (rn_val & 0xFFFF) + (u16)lo_rotated;
  5666. const u8 hi_rotated = (rotated_rm >> 16) & 0xFF;
  5667. const u16 hi_result = (rn_val >> 16) + (u16)hi_rotated;
  5668. state->Reg[rd_idx] = ((hi_result << 16) | (lo_result & 0xFFFF));
  5669. }
  5670. return 1;
  5671. }
  5672. break;
  5673. case 0x6e: // USAT, USAT16, UXTB, and UXTAB
  5674. {
  5675. const u8 op2 = BITS(5, 7);
  5676. // USAT16
  5677. if (op2 == 0x01) {
  5678. const u8 rd_idx = BITS(12, 15);
  5679. const u8 rn_idx = BITS(0, 3);
  5680. const u8 num_bits = BITS(16, 19);
  5681. const s16 max = 0xFFFF >> (16 - num_bits);
  5682. s16 rn_lo = (state->Reg[rn_idx]);
  5683. s16 rn_hi = (state->Reg[rn_idx] >> 16);
  5684. if (max < rn_lo) {
  5685. rn_lo = max;
  5686. SETQ;
  5687. } else if (rn_lo < 0) {
  5688. rn_lo = 0;
  5689. SETQ;
  5690. }
  5691. if (max < rn_hi) {
  5692. rn_hi = max;
  5693. SETQ;
  5694. } else if (rn_hi < 0) {
  5695. rn_hi = 0;
  5696. SETQ;
  5697. }
  5698. state->Reg[rd_idx] = (rn_lo & 0xFFFF) | ((rn_hi << 16) & 0xFFFF);
  5699. return 1;
  5700. }
  5701. else if (op2 == 0x03) {
  5702. const u8 rotate = BITS(10, 11) * 8;
  5703. const u32 rm = ((state->Reg[BITS(0, 3)] >> rotate) & 0xFF) | (((state->Reg[BITS(0, 3)] << (32 - rotate)) & 0xFF) & 0xFF);
  5704. if (BITS(16, 19) == 0xf)
  5705. /* UXTB */
  5706. state->Reg[BITS(12, 15)] = rm;
  5707. else
  5708. /* UXTAB */
  5709. state->Reg[BITS(12, 15)] = state->Reg[BITS(16, 19)] + rm;
  5710. return 1;
  5711. }
  5712. else {
  5713. printf("Unimplemented op: USAT");
  5714. }
  5715. }
  5716. break;
  5717. case 0x6f: // UXTH, UXTAH, and REVSH.
  5718. {
  5719. const u8 op2 = BITS(5, 7);
  5720. // REVSH
  5721. if (op2 == 0x05) {
  5722. DEST = ((RHS & 0xFF) << 8) | ((RHS & 0xFF00) >> 8);
  5723. if (DEST & 0x8000)
  5724. DEST |= 0xffff0000;
  5725. return 1;
  5726. }
  5727. // UXTH and UXTAH
  5728. else if (op2 == 0x03) {
  5729. const u8 rotate = BITS(10, 11) * 8;
  5730. const ARMword rm = ((state->Reg[BITS(0, 3)] >> rotate) & 0xFFFF) | (((state->Reg[BITS(0, 3)] << (32 - rotate)) & 0xFFFF) & 0xFFFF);
  5731. // UXTH
  5732. if (BITS(16, 19) == 0xf) {
  5733. state->Reg[BITS(12, 15)] = rm;
  5734. }
  5735. // UXTAH
  5736. else {
  5737. state->Reg[BITS(12, 15)] = state->Reg[BITS(16, 19)] + rm;
  5738. }
  5739. return 1;
  5740. }
  5741. }
  5742. case 0x70:
  5743. // ichfly
  5744. // SMUAD, SMUSD, SMLAD, and SMLSD
  5745. if ((instr & 0xf0d0) == 0xf010 || (instr & 0xf0d0) == 0xf050 ||
  5746. (instr & 0xd0) == 0x10 || (instr & 0xd0) == 0x50)
  5747. {
  5748. const u8 rd_idx = BITS(16, 19);
  5749. const u8 rn_idx = BITS(0, 3);
  5750. const u8 rm_idx = BITS(8, 11);
  5751. const u8 ra_idx = BITS(12, 15);
  5752. const bool do_swap = (BIT(5) == 1);
  5753. u32 rm_val = state->Reg[rm_idx];
  5754. const u32 rn_val = state->Reg[rn_idx];
  5755. if (do_swap)
  5756. rm_val = (((rm_val & 0xFFFF) << 16) | (rm_val >> 16));
  5757. const s16 rm_lo = (rm_val & 0xFFFF);
  5758. const s16 rm_hi = ((rm_val >> 16) & 0xFFFF);
  5759. const s16 rn_lo = (rn_val & 0xFFFF);
  5760. const s16 rn_hi = ((rn_val >> 16) & 0xFFFF);
  5761. const u32 product1 = (rn_lo * rm_lo);
  5762. const u32 product2 = (rn_hi * rm_hi);
  5763. // SMUAD and SMLAD
  5764. if (BIT(6) == 0) {
  5765. state->Reg[rd_idx] = product1 + product2;
  5766. if (BITS(12, 15) != 15) {
  5767. state->Reg[rd_idx] += state->Reg[ra_idx];
  5768. if (ARMul_AddOverflowQ(product1 + product2, state->Reg[ra_idx]))
  5769. SETQ;
  5770. }
  5771. if (ARMul_AddOverflowQ(product1, product2))
  5772. SETQ;
  5773. }
  5774. // SMUSD and SMLSD
  5775. else {
  5776. state->Reg[rd_idx] = product1 - product2;
  5777. if (BITS(12, 15) != 15) {
  5778. state->Reg[rd_idx] += state->Reg[ra_idx];
  5779. if (ARMul_AddOverflowQ(product1 - product2, state->Reg[ra_idx]))
  5780. SETQ;
  5781. }
  5782. }
  5783. return 1;
  5784. }
  5785. break;
  5786. case 0x74: // SMLALD and SMLSLD
  5787. {
  5788. const u8 rm_idx = BITS(8, 11);
  5789. const u8 rn_idx = BITS(0, 3);
  5790. const u8 rdlo_idx = BITS(12, 15);
  5791. const u8 rdhi_idx = BITS(16, 19);
  5792. const bool do_swap = (BIT(5) == 1);
  5793. const u32 rdlo_val = state->Reg[rdlo_idx];
  5794. const u32 rdhi_val = state->Reg[rdhi_idx];
  5795. const u32 rn_val = state->Reg[rn_idx];
  5796. u32 rm_val = state->Reg[rm_idx];
  5797. if (do_swap)
  5798. rm_val = (((rm_val & 0xFFFF) << 16) | (rm_val >> 16));
  5799. const s32 product1 = (s16)(rn_val & 0xFFFF) * (s16)(rm_val & 0xFFFF);
  5800. const s32 product2 = (s16)((rn_val >> 16) & 0xFFFF) * (s16)((rm_val >> 16) & 0xFFFF);
  5801. s64 result;
  5802. // SMLALD
  5803. if (BIT(6) == 0) {
  5804. result = (product1 + product2) + (s64)(rdlo_val | ((s64)rdhi_val << 32));
  5805. }
  5806. // SMLSLD
  5807. else {
  5808. result = (product1 - product2) + (s64)(rdlo_val | ((s64)rdhi_val << 32));
  5809. }
  5810. state->Reg[rdlo_idx] = (result & 0xFFFFFFFF);
  5811. state->Reg[rdhi_idx] = ((result >> 32) & 0xFFFFFFFF);
  5812. return 1;
  5813. }
  5814. break;
  5815. case 0x75: // SMMLA, SMMUL, and SMMLS
  5816. {
  5817. const u8 rm_idx = BITS(8, 11);
  5818. const u8 rn_idx = BITS(0, 3);
  5819. const u8 ra_idx = BITS(12, 15);
  5820. const u8 rd_idx = BITS(16, 19);
  5821. const bool do_round = (BIT(5) == 1);
  5822. const u32 rm_val = state->Reg[rm_idx];
  5823. const u32 rn_val = state->Reg[rn_idx];
  5824. // Assume SMMUL by default.
  5825. s64 result = (s64)(s32)rn_val * (s64)(s32)rm_val;
  5826. if (ra_idx != 15) {
  5827. const u32 ra_val = state->Reg[ra_idx];
  5828. // SMMLA, otherwise SMMLS
  5829. if (BIT(6) == 0)
  5830. result += ((s64)ra_val << 32);
  5831. else
  5832. result = ((s64)ra_val << 32) - result;
  5833. }
  5834. if (do_round)
  5835. result += 0x80000000;
  5836. state->Reg[rd_idx] = ((result >> 32) & 0xFFFFFFFF);
  5837. return 1;
  5838. }
  5839. break;
  5840. case 0x78:
  5841. if (BITS(20, 24) == 0x18)
  5842. {
  5843. const u8 rm_idx = BITS(8, 11);
  5844. const u8 rn_idx = BITS(0, 3);
  5845. const u8 rd_idx = BITS(16, 19);
  5846. const u32 rm_val = state->Reg[rm_idx];
  5847. const u32 rn_val = state->Reg[rn_idx];
  5848. const u8 diff1 = ARMul_UnsignedAbsoluteDifference(rn_val & 0xFF, rm_val & 0xFF);
  5849. const u8 diff2 = ARMul_UnsignedAbsoluteDifference((rn_val >> 8) & 0xFF, (rm_val >> 8) & 0xFF);
  5850. const u8 diff3 = ARMul_UnsignedAbsoluteDifference((rn_val >> 16) & 0xFF, (rm_val >> 16) & 0xFF);
  5851. const u8 diff4 = ARMul_UnsignedAbsoluteDifference((rn_val >> 24) & 0xFF, (rm_val >> 24) & 0xFF);
  5852. u32 finalDif = (diff1 + diff2 + diff3 + diff4);
  5853. // Op is USADA8 if true.
  5854. const u8 ra_idx = BITS(12, 15);
  5855. if (ra_idx != 15)
  5856. finalDif += state->Reg[ra_idx];
  5857. state->Reg[rd_idx] = finalDif;
  5858. return 1;
  5859. }
  5860. break;
  5861. case 0x7a:
  5862. printf ("Unhandled v6 insn: usbfx\n");
  5863. break;
  5864. case 0x7c:
  5865. printf ("Unhandled v6 insn: bfc/bfi\n");
  5866. break;
  5867. case 0x84:
  5868. printf ("Unhandled v6 insn: srs\n");
  5869. break;
  5870. default:
  5871. break;
  5872. }
  5873. printf("Unhandled v6 insn: UNKNOWN: %08x %08X\n", instr, BITS(20, 27));
  5874. return 0;
  5875. }