compile.c 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110
  1. /*
  2. * Simulator for the Renesas (formerly Hitachi) H8/300 architecture.
  3. *
  4. * Written by Steve Chamberlain of Cygnus Support. sac@cygnus.com
  5. *
  6. * This file is part of H8/300 sim
  7. *
  8. *
  9. * THIS SOFTWARE IS NOT COPYRIGHTED
  10. *
  11. * Cygnus offers the following for use in the public domain. Cygnus makes no
  12. * warranty with regard to the software or its performance and the user
  13. * accepts the software "AS IS" with all faults.
  14. *
  15. * CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS
  16. * SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
  17. * AND FITNESS FOR A PARTICULAR PURPOSE.
  18. */
  19. #include "config.h"
  20. #include <signal.h>
  21. #ifdef HAVE_TIME_H
  22. #include <time.h>
  23. #endif
  24. #ifdef HAVE_STDLIB_H
  25. #include <stdlib.h>
  26. #endif
  27. #ifdef HAVE_SYS_PARAM_H
  28. #include <sys/param.h>
  29. #endif
  30. #include "bfd.h"
  31. #include "sim-main.h"
  32. #include "gdb/sim-h8300.h"
  33. #include "sys/stat.h"
  34. #include "sys/types.h"
  35. #include "sim-options.h"
  36. #ifndef SIGTRAP
  37. # define SIGTRAP 5
  38. #endif
  39. int debug;
  40. host_callback *sim_callback;
  41. static SIM_OPEN_KIND sim_kind;
  42. static char *myname;
  43. /* FIXME: Needs to live in header file.
  44. This header should also include the things in remote-sim.h.
  45. One could move this to remote-sim.h but this function isn't needed
  46. by gdb. */
  47. static void set_simcache_size (SIM_DESC, int);
  48. #define X(op, size) (op * 4 + size)
  49. #define SP (h8300hmode && !h8300_normal_mode ? SL : SW)
  50. #define h8_opcodes ops
  51. #define DEFINE_TABLE
  52. #include "opcode/h8300.h"
  53. /* CPU data object: */
  54. static int
  55. sim_state_initialize (SIM_DESC sd, sim_cpu *cpu)
  56. {
  57. /* FIXME: not really necessary, since sim_cpu_alloc calls zalloc. */
  58. memset (&cpu->regs, 0, sizeof(cpu->regs));
  59. cpu->regs[SBR_REGNUM] = 0xFFFFFF00;
  60. cpu->pc = 0;
  61. cpu->delayed_branch = 0;
  62. cpu->memory = NULL;
  63. cpu->eightbit = NULL;
  64. cpu->mask = 0;
  65. /* Initialize local simulator state. */
  66. sd->sim_cache = NULL;
  67. sd->sim_cache_size = 0;
  68. sd->cache_idx = NULL;
  69. sd->cache_top = 0;
  70. sd->memory_size = 0;
  71. sd->compiles = 0;
  72. #ifdef ADEBUG
  73. memset (&cpu->stats, 0, sizeof (cpu->stats));
  74. #endif
  75. return 0;
  76. }
  77. static unsigned int
  78. h8_get_pc (SIM_DESC sd)
  79. {
  80. return (STATE_CPU (sd, 0)) -> pc;
  81. }
  82. static void
  83. h8_set_pc (SIM_DESC sd, unsigned int val)
  84. {
  85. (STATE_CPU (sd, 0)) -> pc = val;
  86. }
  87. static unsigned int
  88. h8_get_ccr (SIM_DESC sd)
  89. {
  90. return (STATE_CPU (sd, 0)) -> regs[CCR_REGNUM];
  91. }
  92. static void
  93. h8_set_ccr (SIM_DESC sd, unsigned int val)
  94. {
  95. (STATE_CPU (sd, 0)) -> regs[CCR_REGNUM] = val;
  96. }
  97. static unsigned int
  98. h8_get_exr (SIM_DESC sd)
  99. {
  100. return (STATE_CPU (sd, 0)) -> regs[EXR_REGNUM];
  101. }
  102. static void
  103. h8_set_exr (SIM_DESC sd, unsigned int val)
  104. {
  105. (STATE_CPU (sd, 0)) -> regs[EXR_REGNUM] = val;
  106. }
  107. static int
  108. h8_get_sbr (SIM_DESC sd)
  109. {
  110. return (STATE_CPU (sd, 0)) -> regs[SBR_REGNUM];
  111. }
  112. static void
  113. h8_set_sbr (SIM_DESC sd, int val)
  114. {
  115. (STATE_CPU (sd, 0)) -> regs[SBR_REGNUM] = val;
  116. }
  117. static int
  118. h8_get_vbr (SIM_DESC sd)
  119. {
  120. return (STATE_CPU (sd, 0)) -> regs[VBR_REGNUM];
  121. }
  122. static void
  123. h8_set_vbr (SIM_DESC sd, int val)
  124. {
  125. (STATE_CPU (sd, 0)) -> regs[VBR_REGNUM] = val;
  126. }
  127. static int
  128. h8_get_cache_top (SIM_DESC sd)
  129. {
  130. return sd -> cache_top;
  131. }
  132. static void
  133. h8_set_cache_top (SIM_DESC sd, int val)
  134. {
  135. sd -> cache_top = val;
  136. }
  137. static int
  138. h8_get_mask (SIM_DESC sd)
  139. {
  140. return (STATE_CPU (sd, 0)) -> mask;
  141. }
  142. static void
  143. h8_set_mask (SIM_DESC sd, int val)
  144. {
  145. (STATE_CPU (sd, 0)) -> mask = val;
  146. }
  147. #if 0
  148. static int
  149. h8_get_exception (SIM_DESC sd)
  150. {
  151. return (STATE_CPU (sd, 0)) -> exception;
  152. }
  153. static void
  154. h8_set_exception (SIM_DESC sd, int val)
  155. {
  156. (STATE_CPU (sd, 0)) -> exception = val;
  157. }
  158. static enum h8300_sim_state
  159. h8_get_state (SIM_DESC sd)
  160. {
  161. return sd -> state;
  162. }
  163. static void
  164. h8_set_state (SIM_DESC sd, enum h8300_sim_state val)
  165. {
  166. sd -> state = val;
  167. }
  168. #endif
  169. static unsigned int
  170. h8_get_cycles (SIM_DESC sd)
  171. {
  172. return (STATE_CPU (sd, 0)) -> regs[CYCLE_REGNUM];
  173. }
  174. static void
  175. h8_set_cycles (SIM_DESC sd, unsigned int val)
  176. {
  177. (STATE_CPU (sd, 0)) -> regs[CYCLE_REGNUM] = val;
  178. }
  179. static unsigned int
  180. h8_get_insts (SIM_DESC sd)
  181. {
  182. return (STATE_CPU (sd, 0)) -> regs[INST_REGNUM];
  183. }
  184. static void
  185. h8_set_insts (SIM_DESC sd, unsigned int val)
  186. {
  187. (STATE_CPU (sd, 0)) -> regs[INST_REGNUM] = val;
  188. }
  189. static unsigned int
  190. h8_get_ticks (SIM_DESC sd)
  191. {
  192. return (STATE_CPU (sd, 0)) -> regs[TICK_REGNUM];
  193. }
  194. static void
  195. h8_set_ticks (SIM_DESC sd, unsigned int val)
  196. {
  197. (STATE_CPU (sd, 0)) -> regs[TICK_REGNUM] = val;
  198. }
  199. static unsigned int
  200. h8_get_mach (SIM_DESC sd)
  201. {
  202. return (STATE_CPU (sd, 0)) -> regs[MACH_REGNUM];
  203. }
  204. static void
  205. h8_set_mach (SIM_DESC sd, unsigned int val)
  206. {
  207. (STATE_CPU (sd, 0)) -> regs[MACH_REGNUM] = val;
  208. }
  209. static unsigned int
  210. h8_get_macl (SIM_DESC sd)
  211. {
  212. return (STATE_CPU (sd, 0)) -> regs[MACL_REGNUM];
  213. }
  214. static void
  215. h8_set_macl (SIM_DESC sd, unsigned int val)
  216. {
  217. (STATE_CPU (sd, 0)) -> regs[MACL_REGNUM] = val;
  218. }
  219. static int
  220. h8_get_compiles (SIM_DESC sd)
  221. {
  222. return sd -> compiles;
  223. }
  224. static void
  225. h8_increment_compiles (SIM_DESC sd)
  226. {
  227. sd -> compiles ++;
  228. }
  229. static unsigned int *
  230. h8_get_reg_buf (SIM_DESC sd)
  231. {
  232. return &(((STATE_CPU (sd, 0)) -> regs)[0]);
  233. }
  234. static unsigned int
  235. h8_get_reg (SIM_DESC sd, int regnum)
  236. {
  237. return (STATE_CPU (sd, 0)) -> regs[regnum];
  238. }
  239. static void
  240. h8_set_reg (SIM_DESC sd, int regnum, int val)
  241. {
  242. (STATE_CPU (sd, 0)) -> regs[regnum] = val;
  243. }
  244. #ifdef ADEBUG
  245. static int
  246. h8_get_stats (SIM_DESC sd, int idx)
  247. {
  248. return sd -> stats[idx];
  249. }
  250. static void
  251. h8_increment_stats (SIM_DESC sd, int idx)
  252. {
  253. sd -> stats[idx] ++;
  254. }
  255. #endif /* ADEBUG */
  256. static unsigned short *
  257. h8_get_cache_idx_buf (SIM_DESC sd)
  258. {
  259. return sd -> cache_idx;
  260. }
  261. static void
  262. h8_set_cache_idx_buf (SIM_DESC sd, unsigned short *ptr)
  263. {
  264. sd -> cache_idx = ptr;
  265. }
  266. static unsigned short
  267. h8_get_cache_idx (SIM_DESC sd, unsigned int idx)
  268. {
  269. if (idx > sd->memory_size)
  270. return (unsigned short) -1;
  271. return sd -> cache_idx[idx];
  272. }
  273. static void
  274. h8_set_cache_idx (SIM_DESC sd, int idx, unsigned int val)
  275. {
  276. sd -> cache_idx[idx] = (unsigned short) val;
  277. }
  278. static unsigned char *
  279. h8_get_memory_buf (SIM_DESC sd)
  280. {
  281. return (STATE_CPU (sd, 0)) -> memory;
  282. }
  283. static void
  284. h8_set_memory_buf (SIM_DESC sd, unsigned char *ptr)
  285. {
  286. (STATE_CPU (sd, 0)) -> memory = ptr;
  287. }
  288. static unsigned char
  289. h8_get_memory (SIM_DESC sd, int idx)
  290. {
  291. return (STATE_CPU (sd, 0)) -> memory[idx];
  292. }
  293. static void
  294. h8_set_memory (SIM_DESC sd, int idx, unsigned int val)
  295. {
  296. (STATE_CPU (sd, 0)) -> memory[idx] = (unsigned char) val;
  297. }
  298. static unsigned char *
  299. h8_get_eightbit_buf (SIM_DESC sd)
  300. {
  301. return (STATE_CPU (sd, 0)) -> eightbit;
  302. }
  303. static void
  304. h8_set_eightbit_buf (SIM_DESC sd, unsigned char *ptr)
  305. {
  306. (STATE_CPU (sd, 0)) -> eightbit = ptr;
  307. }
  308. static unsigned char
  309. h8_get_eightbit (SIM_DESC sd, int idx)
  310. {
  311. return (STATE_CPU (sd, 0)) -> eightbit[idx];
  312. }
  313. static void
  314. h8_set_eightbit (SIM_DESC sd, int idx, unsigned int val)
  315. {
  316. (STATE_CPU (sd, 0)) -> eightbit[idx] = (unsigned char) val;
  317. }
  318. static unsigned int
  319. h8_get_delayed_branch (SIM_DESC sd)
  320. {
  321. return (STATE_CPU (sd, 0)) -> delayed_branch;
  322. }
  323. static void
  324. h8_set_delayed_branch (SIM_DESC sd, unsigned int dest)
  325. {
  326. (STATE_CPU (sd, 0)) -> delayed_branch = dest;
  327. }
  328. static char **
  329. h8_get_command_line (SIM_DESC sd)
  330. {
  331. return (STATE_CPU (sd, 0)) -> command_line;
  332. }
  333. static void
  334. h8_set_command_line (SIM_DESC sd, char ** val)
  335. {
  336. (STATE_CPU (sd, 0)) -> command_line = val;
  337. }
  338. static char *
  339. h8_get_cmdline_arg (SIM_DESC sd, int index)
  340. {
  341. return (STATE_CPU (sd, 0)) -> command_line[index];
  342. }
  343. static void
  344. h8_set_cmdline_arg (SIM_DESC sd, int index, char * val)
  345. {
  346. (STATE_CPU (sd, 0)) -> command_line[index] = val;
  347. }
  348. /* MAC Saturation Mode */
  349. static int
  350. h8_get_macS (SIM_DESC sd)
  351. {
  352. return (STATE_CPU (sd, 0)) -> macS;
  353. }
  354. static void
  355. h8_set_macS (SIM_DESC sd, int val)
  356. {
  357. (STATE_CPU (sd, 0)) -> macS = (val != 0);
  358. }
  359. /* MAC Zero Flag */
  360. static int
  361. h8_get_macZ (SIM_DESC sd)
  362. {
  363. return (STATE_CPU (sd, 0)) -> macZ;
  364. }
  365. static void
  366. h8_set_macZ (SIM_DESC sd, int val)
  367. {
  368. (STATE_CPU (sd, 0)) -> macZ = (val != 0);
  369. }
  370. /* MAC Negative Flag */
  371. static int
  372. h8_get_macN (SIM_DESC sd)
  373. {
  374. return (STATE_CPU (sd, 0)) -> macN;
  375. }
  376. static void
  377. h8_set_macN (SIM_DESC sd, int val)
  378. {
  379. (STATE_CPU (sd, 0)) -> macN = (val != 0);
  380. }
  381. /* MAC Overflow Flag */
  382. static int
  383. h8_get_macV (SIM_DESC sd)
  384. {
  385. return (STATE_CPU (sd, 0)) -> macV;
  386. }
  387. static void
  388. h8_set_macV (SIM_DESC sd, int val)
  389. {
  390. (STATE_CPU (sd, 0)) -> macV = (val != 0);
  391. }
  392. /* End CPU data object. */
  393. /* The rate at which to call the host's poll_quit callback. */
  394. enum { POLL_QUIT_INTERVAL = 0x80000 };
  395. #define LOW_BYTE(x) ((x) & 0xff)
  396. #define HIGH_BYTE(x) (((x) >> 8) & 0xff)
  397. #define P(X, Y) ((X << 8) | Y)
  398. #define C (c != 0)
  399. #define Z (nz == 0)
  400. #define V (v != 0)
  401. #define N (n != 0)
  402. #define U (u != 0)
  403. #define H (h != 0)
  404. #define UI (ui != 0)
  405. #define I (intMaskBit != 0)
  406. #define BUILDSR(SD) \
  407. h8_set_ccr (SD, (I << 7) | (UI << 6) | (H << 5) | (U << 4) \
  408. | (N << 3) | (Z << 2) | (V << 1) | C)
  409. #define GETSR(SD) \
  410. /* Get Status Register (flags). */ \
  411. c = (h8_get_ccr (sd) >> 0) & 1; \
  412. v = (h8_get_ccr (sd) >> 1) & 1; \
  413. nz = !((h8_get_ccr (sd) >> 2) & 1); \
  414. n = (h8_get_ccr (sd) >> 3) & 1; \
  415. u = (h8_get_ccr (sd) >> 4) & 1; \
  416. h = (h8_get_ccr (sd) >> 5) & 1; \
  417. ui = ((h8_get_ccr (sd) >> 6) & 1); \
  418. intMaskBit = (h8_get_ccr (sd) >> 7) & 1
  419. #ifdef __CHAR_IS_SIGNED__
  420. #define SEXTCHAR(x) ((char) (x))
  421. #endif
  422. #ifndef SEXTCHAR
  423. #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff) : x & 0xff)
  424. #endif
  425. #define UEXTCHAR(x) ((x) & 0xff)
  426. #define UEXTSHORT(x) ((x) & 0xffff)
  427. #define SEXTSHORT(x) ((short) (x))
  428. int h8300hmode = 0;
  429. int h8300smode = 0;
  430. int h8300_normal_mode = 0;
  431. int h8300sxmode = 0;
  432. static int memory_size;
  433. static int
  434. get_now (void)
  435. {
  436. return time (0); /* WinXX HAS UNIX like 'time', so why not use it? */
  437. }
  438. static int
  439. now_persec (void)
  440. {
  441. return 1;
  442. }
  443. static int
  444. bitfrom (int x)
  445. {
  446. switch (x & SIZE)
  447. {
  448. case L_8:
  449. return SB;
  450. case L_16:
  451. case L_16U:
  452. return SW;
  453. case L_32:
  454. return SL;
  455. case L_P:
  456. return (h8300hmode && !h8300_normal_mode)? SL : SW;
  457. }
  458. return 0;
  459. }
  460. /* Simulate an indirection / dereference.
  461. return 0 for success, -1 for failure.
  462. */
  463. static unsigned int
  464. lvalue (SIM_DESC sd, int x, int rn, unsigned int *val)
  465. {
  466. if (val == NULL) /* Paranoia. */
  467. return -1;
  468. switch (x / 4)
  469. {
  470. case OP_DISP:
  471. if (rn == ZERO_REGNUM)
  472. *val = X (OP_IMM, SP);
  473. else
  474. *val = X (OP_REG, SP);
  475. break;
  476. case OP_MEM:
  477. *val = X (OP_MEM, SP);
  478. break;
  479. default:
  480. sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
  481. return -1;
  482. }
  483. return 0;
  484. }
  485. static int
  486. cmdline_location()
  487. {
  488. if (h8300smode && !h8300_normal_mode)
  489. return 0xffff00L;
  490. else if (h8300hmode && !h8300_normal_mode)
  491. return 0x2ff00L;
  492. else
  493. return 0xff00L;
  494. }
  495. static void
  496. decode (SIM_DESC sd, int addr, unsigned char *data, decoded_inst *dst)
  497. {
  498. int cst[3] = {0, 0, 0};
  499. int reg[3] = {0, 0, 0};
  500. int rdisp[3] = {0, 0, 0};
  501. int opnum;
  502. const struct h8_opcode *q;
  503. dst->dst.type = -1;
  504. dst->src.type = -1;
  505. /* Find the exact opcode/arg combo. */
  506. for (q = h8_opcodes; q->name; q++)
  507. {
  508. const op_type *nib = q->data.nib;
  509. unsigned int len = 0;
  510. if ((q->available == AV_H8SX && !h8300sxmode) ||
  511. (q->available == AV_H8S && !h8300smode) ||
  512. (q->available == AV_H8H && !h8300hmode))
  513. continue;
  514. cst[0] = cst[1] = cst[2] = 0;
  515. reg[0] = reg[1] = reg[2] = 0;
  516. rdisp[0] = rdisp[1] = rdisp[2] = 0;
  517. while (1)
  518. {
  519. op_type looking_for = *nib;
  520. int thisnib = data[len / 2];
  521. thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
  522. opnum = ((looking_for & OP3) ? 2 :
  523. (looking_for & DST) ? 1 : 0);
  524. if (looking_for < 16 && looking_for >= 0)
  525. {
  526. if (looking_for != thisnib)
  527. goto fail;
  528. }
  529. else
  530. {
  531. if (looking_for & B31)
  532. {
  533. if (!((thisnib & 0x8) != 0))
  534. goto fail;
  535. looking_for = (op_type) (looking_for & ~B31);
  536. thisnib &= 0x7;
  537. }
  538. else if (looking_for & B30)
  539. {
  540. if (!((thisnib & 0x8) == 0))
  541. goto fail;
  542. looking_for = (op_type) (looking_for & ~B30);
  543. }
  544. if (looking_for & B21)
  545. {
  546. if (!((thisnib & 0x4) != 0))
  547. goto fail;
  548. looking_for = (op_type) (looking_for & ~B21);
  549. thisnib &= 0xb;
  550. }
  551. else if (looking_for & B20)
  552. {
  553. if (!((thisnib & 0x4) == 0))
  554. goto fail;
  555. looking_for = (op_type) (looking_for & ~B20);
  556. }
  557. if (looking_for & B11)
  558. {
  559. if (!((thisnib & 0x2) != 0))
  560. goto fail;
  561. looking_for = (op_type) (looking_for & ~B11);
  562. thisnib &= 0xd;
  563. }
  564. else if (looking_for & B10)
  565. {
  566. if (!((thisnib & 0x2) == 0))
  567. goto fail;
  568. looking_for = (op_type) (looking_for & ~B10);
  569. }
  570. if (looking_for & B01)
  571. {
  572. if (!((thisnib & 0x1) != 0))
  573. goto fail;
  574. looking_for = (op_type) (looking_for & ~B01);
  575. thisnib &= 0xe;
  576. }
  577. else if (looking_for & B00)
  578. {
  579. if (!((thisnib & 0x1) == 0))
  580. goto fail;
  581. looking_for = (op_type) (looking_for & ~B00);
  582. }
  583. if (looking_for & IGNORE)
  584. {
  585. /* Hitachi has declared that IGNORE must be zero. */
  586. if (thisnib != 0)
  587. goto fail;
  588. }
  589. else if ((looking_for & MODE) == DATA)
  590. {
  591. ; /* Skip embedded data. */
  592. }
  593. else if ((looking_for & MODE) == DBIT)
  594. {
  595. /* Exclude adds/subs by looking at bit 0 and 2, and
  596. make sure the operand size, either w or l,
  597. matches by looking at bit 1. */
  598. if ((looking_for & 7) != (thisnib & 7))
  599. goto fail;
  600. cst[opnum] = (thisnib & 0x8) ? 2 : 1;
  601. }
  602. else if ((looking_for & MODE) == REG ||
  603. (looking_for & MODE) == LOWREG ||
  604. (looking_for & MODE) == IND ||
  605. (looking_for & MODE) == PREINC ||
  606. (looking_for & MODE) == POSTINC ||
  607. (looking_for & MODE) == PREDEC ||
  608. (looking_for & MODE) == POSTDEC)
  609. {
  610. reg[opnum] = thisnib;
  611. }
  612. else if (looking_for & CTRL)
  613. {
  614. thisnib &= 7;
  615. if (((looking_for & MODE) == CCR && (thisnib != C_CCR)) ||
  616. ((looking_for & MODE) == EXR && (thisnib != C_EXR)) ||
  617. ((looking_for & MODE) == MACH && (thisnib != C_MACH)) ||
  618. ((looking_for & MODE) == MACL && (thisnib != C_MACL)) ||
  619. ((looking_for & MODE) == VBR && (thisnib != C_VBR)) ||
  620. ((looking_for & MODE) == SBR && (thisnib != C_SBR)))
  621. goto fail;
  622. if (((looking_for & MODE) == CCR_EXR &&
  623. (thisnib != C_CCR && thisnib != C_EXR)) ||
  624. ((looking_for & MODE) == VBR_SBR &&
  625. (thisnib != C_VBR && thisnib != C_SBR)) ||
  626. ((looking_for & MODE) == MACREG &&
  627. (thisnib != C_MACH && thisnib != C_MACL)))
  628. goto fail;
  629. if (((looking_for & MODE) == CC_EX_VB_SB &&
  630. (thisnib != C_CCR && thisnib != C_EXR &&
  631. thisnib != C_VBR && thisnib != C_SBR)))
  632. goto fail;
  633. reg[opnum] = thisnib;
  634. }
  635. else if ((looking_for & MODE) == ABS)
  636. {
  637. /* Absolute addresses are unsigned. */
  638. switch (looking_for & SIZE)
  639. {
  640. case L_8:
  641. cst[opnum] = UEXTCHAR (data[len / 2]);
  642. break;
  643. case L_16:
  644. case L_16U:
  645. cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
  646. break;
  647. case L_32:
  648. cst[opnum] =
  649. (data[len / 2 + 0] << 24) +
  650. (data[len / 2 + 1] << 16) +
  651. (data[len / 2 + 2] << 8) +
  652. (data[len / 2 + 3]);
  653. break;
  654. default:
  655. printf ("decode: bad size ABS: %d\n",
  656. (looking_for & SIZE));
  657. goto end;
  658. }
  659. }
  660. else if ((looking_for & MODE) == DISP ||
  661. (looking_for & MODE) == PCREL ||
  662. (looking_for & MODE) == INDEXB ||
  663. (looking_for & MODE) == INDEXW ||
  664. (looking_for & MODE) == INDEXL)
  665. {
  666. switch (looking_for & SIZE)
  667. {
  668. case L_2:
  669. cst[opnum] = thisnib & 3;
  670. break;
  671. case L_8:
  672. cst[opnum] = SEXTCHAR (data[len / 2]);
  673. break;
  674. case L_16:
  675. cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
  676. cst[opnum] = (short) cst[opnum]; /* Sign extend. */
  677. break;
  678. case L_16U:
  679. cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
  680. break;
  681. case L_32:
  682. cst[opnum] =
  683. (data[len / 2 + 0] << 24) +
  684. (data[len / 2 + 1] << 16) +
  685. (data[len / 2 + 2] << 8) +
  686. (data[len / 2 + 3]);
  687. break;
  688. default:
  689. printf ("decode: bad size DISP/PCREL/INDEX: %d\n",
  690. (looking_for & SIZE));
  691. goto end;
  692. }
  693. }
  694. else if ((looking_for & SIZE) == L_16 ||
  695. (looking_for & SIZE) == L_16U)
  696. {
  697. cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
  698. /* Immediates are always unsigned. */
  699. if ((looking_for & SIZE) != L_16U &&
  700. (looking_for & MODE) != IMM)
  701. cst[opnum] = (short) cst[opnum]; /* Sign extend. */
  702. }
  703. else if (looking_for & ABSJMP)
  704. {
  705. switch (looking_for & SIZE) {
  706. case L_24:
  707. cst[opnum] = (data[1] << 16) | (data[2] << 8) | (data[3]);
  708. break;
  709. case L_32:
  710. cst[opnum] =
  711. (data[len / 2 + 0] << 24) +
  712. (data[len / 2 + 1] << 16) +
  713. (data[len / 2 + 2] << 8) +
  714. (data[len / 2 + 3]);
  715. break;
  716. default:
  717. printf ("decode: bad size ABSJMP: %d\n",
  718. (looking_for & SIZE));
  719. goto end;
  720. }
  721. }
  722. else if ((looking_for & MODE) == MEMIND)
  723. {
  724. cst[opnum] = data[1];
  725. }
  726. else if ((looking_for & MODE) == VECIND)
  727. {
  728. if(h8300_normal_mode)
  729. cst[opnum] = ((data[1] & 0x7f) + 0x80) * 2;
  730. else
  731. cst[opnum] = ((data[1] & 0x7f) + 0x80) * 4;
  732. cst[opnum] += h8_get_vbr (sd); /* Add vector base reg. */
  733. }
  734. else if ((looking_for & SIZE) == L_32)
  735. {
  736. int i = len / 2;
  737. cst[opnum] =
  738. (data[i + 0] << 24) |
  739. (data[i + 1] << 16) |
  740. (data[i + 2] << 8) |
  741. (data[i + 3]);
  742. }
  743. else if ((looking_for & SIZE) == L_24)
  744. {
  745. int i = len / 2;
  746. cst[opnum] =
  747. (data[i + 0] << 16) |
  748. (data[i + 1] << 8) |
  749. (data[i + 2]);
  750. }
  751. else if (looking_for & DISPREG)
  752. {
  753. rdisp[opnum] = thisnib & 0x7;
  754. }
  755. else if ((looking_for & MODE) == KBIT)
  756. {
  757. switch (thisnib)
  758. {
  759. case 9:
  760. cst[opnum] = 4;
  761. break;
  762. case 8:
  763. cst[opnum] = 2;
  764. break;
  765. case 0:
  766. cst[opnum] = 1;
  767. break;
  768. default:
  769. goto fail;
  770. }
  771. }
  772. else if ((looking_for & SIZE) == L_8)
  773. {
  774. if ((looking_for & MODE) == ABS)
  775. {
  776. /* Will be combined with contents of SBR_REGNUM
  777. by fetch (). For all modes except h8sx, this
  778. will always contain the value 0xFFFFFF00. */
  779. cst[opnum] = data[len / 2] & 0xff;
  780. }
  781. else
  782. {
  783. cst[opnum] = data[len / 2] & 0xff;
  784. }
  785. }
  786. else if ((looking_for & SIZE) == L_2)
  787. {
  788. cst[opnum] = thisnib & 3;
  789. }
  790. else if ((looking_for & SIZE) == L_3 ||
  791. (looking_for & SIZE) == L_3NZ)
  792. {
  793. cst[opnum] = thisnib & 7;
  794. if (cst[opnum] == 0 && (looking_for & SIZE) == L_3NZ)
  795. goto fail;
  796. }
  797. else if ((looking_for & SIZE) == L_4)
  798. {
  799. cst[opnum] = thisnib & 15;
  800. }
  801. else if ((looking_for & SIZE) == L_5)
  802. {
  803. cst[opnum] = data[len / 2] & 0x1f;
  804. }
  805. else if (looking_for == E)
  806. {
  807. #ifdef ADEBUG
  808. dst->op = q;
  809. #endif
  810. /* Fill in the args. */
  811. {
  812. const op_type *args = q->args.nib;
  813. int hadone = 0;
  814. int nargs;
  815. for (nargs = 0;
  816. nargs < 3 && *args != E;
  817. nargs++)
  818. {
  819. int x = *args;
  820. ea_type *p;
  821. opnum = ((x & OP3) ? 2 :
  822. (x & DST) ? 1 : 0);
  823. if (x & DST)
  824. p = &dst->dst;
  825. else if (x & OP3)
  826. p = &dst->op3;
  827. else
  828. p = &dst->src;
  829. if ((x & MODE) == IMM ||
  830. (x & MODE) == KBIT ||
  831. (x & MODE) == DBIT)
  832. {
  833. /* Use the instruction to determine
  834. the operand size. */
  835. p->type = X (OP_IMM, OP_SIZE (q->how));
  836. p->literal = cst[opnum];
  837. }
  838. else if ((x & MODE) == CONST_2 ||
  839. (x & MODE) == CONST_4 ||
  840. (x & MODE) == CONST_8 ||
  841. (x & MODE) == CONST_16)
  842. {
  843. /* Use the instruction to determine
  844. the operand size. */
  845. p->type = X (OP_IMM, OP_SIZE (q->how));
  846. switch (x & MODE) {
  847. case CONST_2: p->literal = 2; break;
  848. case CONST_4: p->literal = 4; break;
  849. case CONST_8: p->literal = 8; break;
  850. case CONST_16: p->literal = 16; break;
  851. }
  852. }
  853. else if ((x & MODE) == REG)
  854. {
  855. p->type = X (OP_REG, bitfrom (x));
  856. p->reg = reg[opnum];
  857. }
  858. else if ((x & MODE) == LOWREG)
  859. {
  860. p->type = X (OP_LOWREG, bitfrom (x));
  861. p->reg = reg[opnum];
  862. }
  863. else if ((x & MODE) == PREINC)
  864. {
  865. /* Use the instruction to determine
  866. the operand size. */
  867. p->type = X (OP_PREINC, OP_SIZE (q->how));
  868. p->reg = reg[opnum] & 0x7;
  869. }
  870. else if ((x & MODE) == POSTINC)
  871. {
  872. /* Use the instruction to determine
  873. the operand size. */
  874. p->type = X (OP_POSTINC, OP_SIZE (q->how));
  875. p->reg = reg[opnum] & 0x7;
  876. }
  877. else if ((x & MODE) == PREDEC)
  878. {
  879. /* Use the instruction to determine
  880. the operand size. */
  881. p->type = X (OP_PREDEC, OP_SIZE (q->how));
  882. p->reg = reg[opnum] & 0x7;
  883. }
  884. else if ((x & MODE) == POSTDEC)
  885. {
  886. /* Use the instruction to determine
  887. the operand size. */
  888. p->type = X (OP_POSTDEC, OP_SIZE (q->how));
  889. p->reg = reg[opnum] & 0x7;
  890. }
  891. else if ((x & MODE) == IND)
  892. {
  893. /* Note: an indirect is transformed into
  894. a displacement of zero.
  895. */
  896. /* Use the instruction to determine
  897. the operand size. */
  898. p->type = X (OP_DISP, OP_SIZE (q->how));
  899. p->reg = reg[opnum] & 0x7;
  900. p->literal = 0;
  901. if (OP_KIND (q->how) == O_JSR ||
  902. OP_KIND (q->how) == O_JMP)
  903. if (lvalue (sd, p->type, p->reg, (unsigned int *)&p->type))
  904. goto end;
  905. }
  906. else if ((x & MODE) == ABS)
  907. {
  908. /* Note: a 16 or 32 bit ABS is transformed into a
  909. displacement from pseudo-register ZERO_REGNUM,
  910. which is always zero. An 8 bit ABS becomes
  911. a displacement from SBR_REGNUM.
  912. */
  913. /* Use the instruction to determine
  914. the operand size. */
  915. p->type = X (OP_DISP, OP_SIZE (q->how));
  916. p->literal = cst[opnum];
  917. /* 8-bit ABS is displacement from SBR.
  918. 16 and 32-bit ABS are displacement from ZERO.
  919. (SBR will always be zero except for h8/sx)
  920. */
  921. if ((x & SIZE) == L_8)
  922. p->reg = SBR_REGNUM;
  923. else
  924. p->reg = ZERO_REGNUM;;
  925. }
  926. else if ((x & MODE) == MEMIND ||
  927. (x & MODE) == VECIND)
  928. {
  929. /* Size doesn't matter. */
  930. p->type = X (OP_MEM, SB);
  931. p->literal = cst[opnum];
  932. if (OP_KIND (q->how) == O_JSR ||
  933. OP_KIND (q->how) == O_JMP)
  934. if (lvalue (sd, p->type, p->reg, (unsigned int *)&p->type))
  935. goto end;
  936. }
  937. else if ((x & MODE) == PCREL)
  938. {
  939. /* Size doesn't matter. */
  940. p->type = X (OP_PCREL, SB);
  941. p->literal = cst[opnum];
  942. }
  943. else if (x & ABSJMP)
  944. {
  945. p->type = X (OP_IMM, SP);
  946. p->literal = cst[opnum];
  947. }
  948. else if ((x & MODE) == INDEXB)
  949. {
  950. p->type = X (OP_INDEXB, OP_SIZE (q->how));
  951. p->literal = cst[opnum];
  952. p->reg = rdisp[opnum];
  953. }
  954. else if ((x & MODE) == INDEXW)
  955. {
  956. p->type = X (OP_INDEXW, OP_SIZE (q->how));
  957. p->literal = cst[opnum];
  958. p->reg = rdisp[opnum];
  959. }
  960. else if ((x & MODE) == INDEXL)
  961. {
  962. p->type = X (OP_INDEXL, OP_SIZE (q->how));
  963. p->literal = cst[opnum];
  964. p->reg = rdisp[opnum];
  965. }
  966. else if ((x & MODE) == DISP)
  967. {
  968. /* Yuck -- special for mova args. */
  969. if (strncmp (q->name, "mova", 4) == 0 &&
  970. (x & SIZE) == L_2)
  971. {
  972. /* Mova can have a DISP2 dest, with an
  973. INDEXB or INDEXW src. The multiplier
  974. for the displacement value is determined
  975. by the src operand, not by the insn. */
  976. switch (OP_KIND (dst->src.type))
  977. {
  978. case OP_INDEXB:
  979. p->type = X (OP_DISP, SB);
  980. p->literal = cst[opnum];
  981. break;
  982. case OP_INDEXW:
  983. p->type = X (OP_DISP, SW);
  984. p->literal = cst[opnum] * 2;
  985. break;
  986. default:
  987. goto fail;
  988. }
  989. }
  990. else
  991. {
  992. p->type = X (OP_DISP, OP_SIZE (q->how));
  993. p->literal = cst[opnum];
  994. /* DISP2 is special. */
  995. if ((x & SIZE) == L_2)
  996. switch (OP_SIZE (q->how))
  997. {
  998. case SB: break;
  999. case SW: p->literal *= 2; break;
  1000. case SL: p->literal *= 4; break;
  1001. }
  1002. }
  1003. p->reg = rdisp[opnum];
  1004. }
  1005. else if (x & CTRL)
  1006. {
  1007. switch (reg[opnum])
  1008. {
  1009. case C_CCR:
  1010. p->type = X (OP_CCR, SB);
  1011. break;
  1012. case C_EXR:
  1013. p->type = X (OP_EXR, SB);
  1014. break;
  1015. case C_MACH:
  1016. p->type = X (OP_MACH, SL);
  1017. break;
  1018. case C_MACL:
  1019. p->type = X (OP_MACL, SL);
  1020. break;
  1021. case C_VBR:
  1022. p->type = X (OP_VBR, SL);
  1023. break;
  1024. case C_SBR:
  1025. p->type = X (OP_SBR, SL);
  1026. break;
  1027. }
  1028. }
  1029. else if ((x & MODE) == CCR)
  1030. {
  1031. p->type = OP_CCR;
  1032. }
  1033. else if ((x & MODE) == EXR)
  1034. {
  1035. p->type = OP_EXR;
  1036. }
  1037. else
  1038. printf ("Hmmmm 0x%x...\n", x);
  1039. args++;
  1040. }
  1041. }
  1042. /* Unary operators: treat src and dst as equivalent. */
  1043. if (dst->dst.type == -1)
  1044. dst->dst = dst->src;
  1045. if (dst->src.type == -1)
  1046. dst->src = dst->dst;
  1047. dst->opcode = q->how;
  1048. dst->cycles = q->time;
  1049. /* And jsr's to these locations are turned into
  1050. magic traps. */
  1051. if (OP_KIND (dst->opcode) == O_JSR)
  1052. {
  1053. switch (dst->src.literal)
  1054. {
  1055. case 0xc5:
  1056. dst->opcode = O (O_SYS_OPEN, SB);
  1057. break;
  1058. case 0xc6:
  1059. dst->opcode = O (O_SYS_READ, SB);
  1060. break;
  1061. case 0xc7:
  1062. dst->opcode = O (O_SYS_WRITE, SB);
  1063. break;
  1064. case 0xc8:
  1065. dst->opcode = O (O_SYS_LSEEK, SB);
  1066. break;
  1067. case 0xc9:
  1068. dst->opcode = O (O_SYS_CLOSE, SB);
  1069. break;
  1070. case 0xca:
  1071. dst->opcode = O (O_SYS_STAT, SB);
  1072. break;
  1073. case 0xcb:
  1074. dst->opcode = O (O_SYS_FSTAT, SB);
  1075. break;
  1076. case 0xcc:
  1077. dst->opcode = O (O_SYS_CMDLINE, SB);
  1078. break;
  1079. }
  1080. /* End of Processing for system calls. */
  1081. }
  1082. dst->next_pc = addr + len / 2;
  1083. return;
  1084. }
  1085. else
  1086. printf ("Don't understand 0x%x \n", looking_for);
  1087. }
  1088. len++;
  1089. nib++;
  1090. }
  1091. fail:
  1092. ;
  1093. }
  1094. end:
  1095. /* Fell off the end. */
  1096. dst->opcode = O (O_ILL, SB);
  1097. }
  1098. static void
  1099. compile (SIM_DESC sd, int pc)
  1100. {
  1101. int idx;
  1102. /* Find the next cache entry to use. */
  1103. idx = h8_get_cache_top (sd) + 1;
  1104. h8_increment_compiles (sd);
  1105. if (idx >= sd->sim_cache_size)
  1106. {
  1107. idx = 1;
  1108. }
  1109. h8_set_cache_top (sd, idx);
  1110. /* Throw away its old meaning. */
  1111. h8_set_cache_idx (sd, sd->sim_cache[idx].oldpc, 0);
  1112. /* Set to new address. */
  1113. sd->sim_cache[idx].oldpc = pc;
  1114. /* Fill in instruction info. */
  1115. decode (sd, pc, h8_get_memory_buf (sd) + pc, sd->sim_cache + idx);
  1116. /* Point to new cache entry. */
  1117. h8_set_cache_idx (sd, pc, idx);
  1118. }
  1119. static unsigned char *breg[32];
  1120. static unsigned short *wreg[16];
  1121. static unsigned int *lreg[18];
  1122. #define GET_B_REG(X) *(breg[X])
  1123. #define SET_B_REG(X, Y) (*(breg[X])) = (Y)
  1124. #define GET_W_REG(X) *(wreg[X])
  1125. #define SET_W_REG(X, Y) (*(wreg[X])) = (Y)
  1126. #define GET_L_REG(X) h8_get_reg (sd, X)
  1127. #define SET_L_REG(X, Y) h8_set_reg (sd, X, Y)
  1128. #define GET_MEMORY_L(X) \
  1129. ((X) < memory_size \
  1130. ? ((h8_get_memory (sd, (X)+0) << 24) | (h8_get_memory (sd, (X)+1) << 16) \
  1131. | (h8_get_memory (sd, (X)+2) << 8) | (h8_get_memory (sd, (X)+3) << 0)) \
  1132. : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 24) \
  1133. | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 16) \
  1134. | (h8_get_eightbit (sd, ((X)+2) & 0xff) << 8) \
  1135. | (h8_get_eightbit (sd, ((X)+3) & 0xff) << 0)))
  1136. #define GET_MEMORY_W(X) \
  1137. ((X) < memory_size \
  1138. ? ((h8_get_memory (sd, (X)+0) << 8) \
  1139. | (h8_get_memory (sd, (X)+1) << 0)) \
  1140. : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 8) \
  1141. | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 0)))
  1142. #define GET_MEMORY_B(X) \
  1143. ((X) < memory_size ? (h8_get_memory (sd, (X))) \
  1144. : (h8_get_eightbit (sd, (X) & 0xff)))
  1145. #define SET_MEMORY_L(X, Y) \
  1146. { register unsigned char *_p; register int __y = (Y); \
  1147. _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
  1148. h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
  1149. _p[0] = __y >> 24; _p[1] = __y >> 16; \
  1150. _p[2] = __y >> 8; _p[3] = __y >> 0; \
  1151. }
  1152. #define SET_MEMORY_W(X, Y) \
  1153. { register unsigned char *_p; register int __y = (Y); \
  1154. _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
  1155. h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
  1156. _p[0] = __y >> 8; _p[1] = __y; \
  1157. }
  1158. #define SET_MEMORY_B(X, Y) \
  1159. ((X) < memory_size ? (h8_set_memory (sd, (X), (Y))) \
  1160. : (h8_set_eightbit (sd, (X) & 0xff, (Y))))
  1161. /* Simulate a memory fetch.
  1162. Return 0 for success, -1 for failure.
  1163. */
  1164. static int
  1165. fetch_1 (SIM_DESC sd, ea_type *arg, int *val, int twice)
  1166. {
  1167. int rn = arg->reg;
  1168. int abs = arg->literal;
  1169. int r;
  1170. int t;
  1171. if (val == NULL)
  1172. return -1; /* Paranoia. */
  1173. switch (arg->type)
  1174. {
  1175. /* Indexed register plus displacement mode:
  1176. This new family of addressing modes are similar to OP_DISP
  1177. (register plus displacement), with two differences:
  1178. 1) INDEXB uses only the least significant byte of the register,
  1179. INDEXW uses only the least significant word, and
  1180. INDEXL uses the entire register (just like OP_DISP).
  1181. and
  1182. 2) The displacement value in abs is multiplied by two
  1183. for SW-sized operations, and by four for SL-size.
  1184. This gives nine possible variations.
  1185. */
  1186. case X (OP_INDEXB, SB):
  1187. case X (OP_INDEXB, SW):
  1188. case X (OP_INDEXB, SL):
  1189. case X (OP_INDEXW, SB):
  1190. case X (OP_INDEXW, SW):
  1191. case X (OP_INDEXW, SL):
  1192. case X (OP_INDEXL, SB):
  1193. case X (OP_INDEXL, SW):
  1194. case X (OP_INDEXL, SL):
  1195. t = GET_L_REG (rn);
  1196. switch (OP_KIND (arg->type)) {
  1197. case OP_INDEXB: t &= 0xff; break;
  1198. case OP_INDEXW: t &= 0xffff; break;
  1199. case OP_INDEXL:
  1200. default: break;
  1201. }
  1202. switch (OP_SIZE (arg->type)) {
  1203. case SB:
  1204. *val = GET_MEMORY_B ((t * 1 + abs) & h8_get_mask (sd));
  1205. break;
  1206. case SW:
  1207. *val = GET_MEMORY_W ((t * 2 + abs) & h8_get_mask (sd));
  1208. break;
  1209. case SL:
  1210. *val = GET_MEMORY_L ((t * 4 + abs) & h8_get_mask (sd));
  1211. break;
  1212. }
  1213. break;
  1214. case X (OP_LOWREG, SB):
  1215. *val = GET_L_REG (rn) & 0xff;
  1216. break;
  1217. case X (OP_LOWREG, SW):
  1218. *val = GET_L_REG (rn) & 0xffff;
  1219. break;
  1220. case X (OP_REG, SB): /* Register direct, byte. */
  1221. *val = GET_B_REG (rn);
  1222. break;
  1223. case X (OP_REG, SW): /* Register direct, word. */
  1224. *val = GET_W_REG (rn);
  1225. break;
  1226. case X (OP_REG, SL): /* Register direct, long. */
  1227. *val = GET_L_REG (rn);
  1228. break;
  1229. case X (OP_IMM, SB): /* Immediate, byte. */
  1230. case X (OP_IMM, SW): /* Immediate, word. */
  1231. case X (OP_IMM, SL): /* Immediate, long. */
  1232. *val = abs;
  1233. break;
  1234. case X (OP_POSTINC, SB): /* Register indirect w/post-incr: byte. */
  1235. t = GET_L_REG (rn);
  1236. r = GET_MEMORY_B (t & h8_get_mask (sd));
  1237. if (!twice)
  1238. t += 1;
  1239. SET_L_REG (rn, t);
  1240. *val = r;
  1241. break;
  1242. case X (OP_POSTINC, SW): /* Register indirect w/post-incr: word. */
  1243. t = GET_L_REG (rn);
  1244. r = GET_MEMORY_W (t & h8_get_mask (sd));
  1245. if (!twice)
  1246. t += 2;
  1247. SET_L_REG (rn, t);
  1248. *val = r;
  1249. break;
  1250. case X (OP_POSTINC, SL): /* Register indirect w/post-incr: long. */
  1251. t = GET_L_REG (rn);
  1252. r = GET_MEMORY_L (t & h8_get_mask (sd));
  1253. if (!twice)
  1254. t += 4;
  1255. SET_L_REG (rn, t);
  1256. *val = r;
  1257. break;
  1258. case X (OP_POSTDEC, SB): /* Register indirect w/post-decr: byte. */
  1259. t = GET_L_REG (rn);
  1260. r = GET_MEMORY_B (t & h8_get_mask (sd));
  1261. if (!twice)
  1262. t -= 1;
  1263. SET_L_REG (rn, t);
  1264. *val = r;
  1265. break;
  1266. case X (OP_POSTDEC, SW): /* Register indirect w/post-decr: word. */
  1267. t = GET_L_REG (rn);
  1268. r = GET_MEMORY_W (t & h8_get_mask (sd));
  1269. if (!twice)
  1270. t -= 2;
  1271. SET_L_REG (rn, t);
  1272. *val = r;
  1273. break;
  1274. case X (OP_POSTDEC, SL): /* Register indirect w/post-decr: long. */
  1275. t = GET_L_REG (rn);
  1276. r = GET_MEMORY_L (t & h8_get_mask (sd));
  1277. if (!twice)
  1278. t -= 4;
  1279. SET_L_REG (rn, t);
  1280. *val = r;
  1281. break;
  1282. case X (OP_PREDEC, SB): /* Register indirect w/pre-decr: byte. */
  1283. t = GET_L_REG (rn) - 1;
  1284. SET_L_REG (rn, t);
  1285. t &= h8_get_mask (sd);
  1286. *val = GET_MEMORY_B (t);
  1287. break;
  1288. case X (OP_PREDEC, SW): /* Register indirect w/pre-decr: word. */
  1289. t = GET_L_REG (rn) - 2;
  1290. SET_L_REG (rn, t);
  1291. t &= h8_get_mask (sd);
  1292. *val = GET_MEMORY_W (t);
  1293. break;
  1294. case X (OP_PREDEC, SL): /* Register indirect w/pre-decr: long. */
  1295. t = GET_L_REG (rn) - 4;
  1296. SET_L_REG (rn, t);
  1297. t &= h8_get_mask (sd);
  1298. *val = GET_MEMORY_L (t);
  1299. break;
  1300. case X (OP_PREINC, SB): /* Register indirect w/pre-incr: byte. */
  1301. t = GET_L_REG (rn) + 1;
  1302. SET_L_REG (rn, t);
  1303. t &= h8_get_mask (sd);
  1304. *val = GET_MEMORY_B (t);
  1305. break;
  1306. case X (OP_PREINC, SW): /* Register indirect w/pre-incr: long. */
  1307. t = GET_L_REG (rn) + 2;
  1308. SET_L_REG (rn, t);
  1309. t &= h8_get_mask (sd);
  1310. *val = GET_MEMORY_W (t);
  1311. break;
  1312. case X (OP_PREINC, SL): /* Register indirect w/pre-incr: long. */
  1313. t = GET_L_REG (rn) + 4;
  1314. SET_L_REG (rn, t);
  1315. t &= h8_get_mask (sd);
  1316. *val = GET_MEMORY_L (t);
  1317. break;
  1318. case X (OP_DISP, SB): /* Register indirect w/displacement: byte. */
  1319. t = GET_L_REG (rn) + abs;
  1320. t &= h8_get_mask (sd);
  1321. *val = GET_MEMORY_B (t);
  1322. break;
  1323. case X (OP_DISP, SW): /* Register indirect w/displacement: word. */
  1324. t = GET_L_REG (rn) + abs;
  1325. t &= h8_get_mask (sd);
  1326. *val = GET_MEMORY_W (t);
  1327. break;
  1328. case X (OP_DISP, SL): /* Register indirect w/displacement: long. */
  1329. t = GET_L_REG (rn) + abs;
  1330. t &= h8_get_mask (sd);
  1331. *val =GET_MEMORY_L (t);
  1332. break;
  1333. case X (OP_MEM, SL): /* Absolute memory address, long. */
  1334. t = GET_MEMORY_L (abs);
  1335. t &= h8_get_mask (sd);
  1336. *val = t;
  1337. break;
  1338. case X (OP_MEM, SW): /* Absolute memory address, word. */
  1339. t = GET_MEMORY_W (abs);
  1340. t &= h8_get_mask (sd);
  1341. *val = t;
  1342. break;
  1343. case X (OP_PCREL, SB): /* PC relative (for jump, branch etc). */
  1344. case X (OP_PCREL, SW):
  1345. case X (OP_PCREL, SL):
  1346. case X (OP_PCREL, SN):
  1347. *val = abs;
  1348. break;
  1349. case X (OP_MEM, SB): /* Why isn't this implemented? */
  1350. default:
  1351. sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
  1352. return -1;
  1353. }
  1354. return 0; /* Success. */
  1355. }
  1356. /* Normal fetch. */
  1357. static int
  1358. fetch (SIM_DESC sd, ea_type *arg, int *val)
  1359. {
  1360. return fetch_1 (sd, arg, val, 0);
  1361. }
  1362. /* Fetch which will be followed by a store to the same location.
  1363. The difference being that we don't want to do a post-increment
  1364. or post-decrement at this time: we'll do it when we store. */
  1365. static int
  1366. fetch2 (SIM_DESC sd, ea_type *arg, int *val)
  1367. {
  1368. return fetch_1 (sd, arg, val, 1);
  1369. }
  1370. /* Simulate a memory store.
  1371. Return 0 for success, -1 for failure.
  1372. */
  1373. static int
  1374. store_1 (SIM_DESC sd, ea_type *arg, int n, int twice)
  1375. {
  1376. int rn = arg->reg;
  1377. int abs = arg->literal;
  1378. int t;
  1379. switch (arg->type)
  1380. {
  1381. /* Indexed register plus displacement mode:
  1382. This new family of addressing modes are similar to OP_DISP
  1383. (register plus displacement), with two differences:
  1384. 1) INDEXB uses only the least significant byte of the register,
  1385. INDEXW uses only the least significant word, and
  1386. INDEXL uses the entire register (just like OP_DISP).
  1387. and
  1388. 2) The displacement value in abs is multiplied by two
  1389. for SW-sized operations, and by four for SL-size.
  1390. This gives nine possible variations.
  1391. */
  1392. case X (OP_INDEXB, SB):
  1393. case X (OP_INDEXB, SW):
  1394. case X (OP_INDEXB, SL):
  1395. case X (OP_INDEXW, SB):
  1396. case X (OP_INDEXW, SW):
  1397. case X (OP_INDEXW, SL):
  1398. case X (OP_INDEXL, SB):
  1399. case X (OP_INDEXL, SW):
  1400. case X (OP_INDEXL, SL):
  1401. t = GET_L_REG (rn);
  1402. switch (OP_KIND (arg->type)) {
  1403. case OP_INDEXB: t &= 0xff; break;
  1404. case OP_INDEXW: t &= 0xffff; break;
  1405. case OP_INDEXL:
  1406. default: break;
  1407. }
  1408. switch (OP_SIZE (arg->type)) {
  1409. case SB:
  1410. SET_MEMORY_B ((t * 1 + abs) & h8_get_mask (sd), n);
  1411. break;
  1412. case SW:
  1413. SET_MEMORY_W ((t * 2 + abs) & h8_get_mask (sd), n);
  1414. break;
  1415. case SL:
  1416. SET_MEMORY_L ((t * 4 + abs) & h8_get_mask (sd), n);
  1417. break;
  1418. }
  1419. break;
  1420. case X (OP_REG, SB): /* Register direct, byte. */
  1421. SET_B_REG (rn, n);
  1422. break;
  1423. case X (OP_REG, SW): /* Register direct, word. */
  1424. SET_W_REG (rn, n);
  1425. break;
  1426. case X (OP_REG, SL): /* Register direct, long. */
  1427. SET_L_REG (rn, n);
  1428. break;
  1429. case X (OP_PREDEC, SB): /* Register indirect w/pre-decr, byte. */
  1430. t = GET_L_REG (rn);
  1431. if (!twice)
  1432. t -= 1;
  1433. SET_L_REG (rn, t);
  1434. t &= h8_get_mask (sd);
  1435. SET_MEMORY_B (t, n);
  1436. break;
  1437. case X (OP_PREDEC, SW): /* Register indirect w/pre-decr, word. */
  1438. t = GET_L_REG (rn);
  1439. if (!twice)
  1440. t -= 2;
  1441. SET_L_REG (rn, t);
  1442. t &= h8_get_mask (sd);
  1443. SET_MEMORY_W (t, n);
  1444. break;
  1445. case X (OP_PREDEC, SL): /* Register indirect w/pre-decr, long. */
  1446. t = GET_L_REG (rn);
  1447. if (!twice)
  1448. t -= 4;
  1449. SET_L_REG (rn, t);
  1450. t &= h8_get_mask (sd);
  1451. SET_MEMORY_L (t, n);
  1452. break;
  1453. case X (OP_PREINC, SB): /* Register indirect w/pre-incr, byte. */
  1454. t = GET_L_REG (rn);
  1455. if (!twice)
  1456. t += 1;
  1457. SET_L_REG (rn, t);
  1458. t &= h8_get_mask (sd);
  1459. SET_MEMORY_B (t, n);
  1460. break;
  1461. case X (OP_PREINC, SW): /* Register indirect w/pre-incr, word. */
  1462. t = GET_L_REG (rn);
  1463. if (!twice)
  1464. t += 2;
  1465. SET_L_REG (rn, t);
  1466. t &= h8_get_mask (sd);
  1467. SET_MEMORY_W (t, n);
  1468. break;
  1469. case X (OP_PREINC, SL): /* Register indirect w/pre-incr, long. */
  1470. t = GET_L_REG (rn);
  1471. if (!twice)
  1472. t += 4;
  1473. SET_L_REG (rn, t);
  1474. t &= h8_get_mask (sd);
  1475. SET_MEMORY_L (t, n);
  1476. break;
  1477. case X (OP_POSTDEC, SB): /* Register indirect w/post-decr, byte. */
  1478. t = GET_L_REG (rn);
  1479. SET_L_REG (rn, t - 1);
  1480. t &= h8_get_mask (sd);
  1481. SET_MEMORY_B (t, n);
  1482. break;
  1483. case X (OP_POSTDEC, SW): /* Register indirect w/post-decr, word. */
  1484. t = GET_L_REG (rn);
  1485. SET_L_REG (rn, t - 2);
  1486. t &= h8_get_mask (sd);
  1487. SET_MEMORY_W (t, n);
  1488. break;
  1489. case X (OP_POSTDEC, SL): /* Register indirect w/post-decr, long. */
  1490. t = GET_L_REG (rn);
  1491. SET_L_REG (rn, t - 4);
  1492. t &= h8_get_mask (sd);
  1493. SET_MEMORY_L (t, n);
  1494. break;
  1495. case X (OP_POSTINC, SB): /* Register indirect w/post-incr, byte. */
  1496. t = GET_L_REG (rn);
  1497. SET_L_REG (rn, t + 1);
  1498. t &= h8_get_mask (sd);
  1499. SET_MEMORY_B (t, n);
  1500. break;
  1501. case X (OP_POSTINC, SW): /* Register indirect w/post-incr, word. */
  1502. t = GET_L_REG (rn);
  1503. SET_L_REG (rn, t + 2);
  1504. t &= h8_get_mask (sd);
  1505. SET_MEMORY_W (t, n);
  1506. break;
  1507. case X (OP_POSTINC, SL): /* Register indirect w/post-incr, long. */
  1508. t = GET_L_REG (rn);
  1509. SET_L_REG (rn, t + 4);
  1510. t &= h8_get_mask (sd);
  1511. SET_MEMORY_L (t, n);
  1512. break;
  1513. case X (OP_DISP, SB): /* Register indirect w/displacement, byte. */
  1514. t = GET_L_REG (rn) + abs;
  1515. t &= h8_get_mask (sd);
  1516. SET_MEMORY_B (t, n);
  1517. break;
  1518. case X (OP_DISP, SW): /* Register indirect w/displacement, word. */
  1519. t = GET_L_REG (rn) + abs;
  1520. t &= h8_get_mask (sd);
  1521. SET_MEMORY_W (t, n);
  1522. break;
  1523. case X (OP_DISP, SL): /* Register indirect w/displacement, long. */
  1524. t = GET_L_REG (rn) + abs;
  1525. t &= h8_get_mask (sd);
  1526. SET_MEMORY_L (t, n);
  1527. break;
  1528. case X (OP_MEM, SB): /* Why isn't this implemented? */
  1529. case X (OP_MEM, SW): /* Why isn't this implemented? */
  1530. case X (OP_MEM, SL): /* Why isn't this implemented? */
  1531. default:
  1532. sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
  1533. return -1;
  1534. }
  1535. return 0;
  1536. }
  1537. /* Normal store. */
  1538. static int
  1539. store (SIM_DESC sd, ea_type *arg, int n)
  1540. {
  1541. return store_1 (sd, arg, n, 0);
  1542. }
  1543. /* Store which follows a fetch from the same location.
  1544. The difference being that we don't want to do a pre-increment
  1545. or pre-decrement at this time: it was already done when we fetched. */
  1546. static int
  1547. store2 (SIM_DESC sd, ea_type *arg, int n)
  1548. {
  1549. return store_1 (sd, arg, n, 1);
  1550. }
  1551. static union
  1552. {
  1553. short int i;
  1554. struct
  1555. {
  1556. char low;
  1557. char high;
  1558. }
  1559. u;
  1560. } littleendian;
  1561. /* Flag to be set whenever a new SIM_DESC object is created. */
  1562. static int init_pointers_needed = 1;
  1563. static void
  1564. init_pointers (SIM_DESC sd)
  1565. {
  1566. if (init_pointers_needed)
  1567. {
  1568. int i;
  1569. littleendian.i = 1;
  1570. if (h8300smode && !h8300_normal_mode)
  1571. memory_size = H8300S_MSIZE;
  1572. else if (h8300hmode && !h8300_normal_mode)
  1573. memory_size = H8300H_MSIZE;
  1574. else
  1575. memory_size = H8300_MSIZE;
  1576. /* `msize' must be a power of two. */
  1577. if ((memory_size & (memory_size - 1)) != 0)
  1578. {
  1579. (*sim_callback->printf_filtered)
  1580. (sim_callback,
  1581. "init_pointers: bad memory size %d, defaulting to %d.\n",
  1582. memory_size, memory_size = H8300S_MSIZE);
  1583. }
  1584. if (h8_get_memory_buf (sd))
  1585. free (h8_get_memory_buf (sd));
  1586. if (h8_get_cache_idx_buf (sd))
  1587. free (h8_get_cache_idx_buf (sd));
  1588. if (h8_get_eightbit_buf (sd))
  1589. free (h8_get_eightbit_buf (sd));
  1590. h8_set_memory_buf (sd, (unsigned char *)
  1591. calloc (sizeof (char), memory_size));
  1592. h8_set_cache_idx_buf (sd, (unsigned short *)
  1593. calloc (sizeof (short), memory_size));
  1594. sd->memory_size = memory_size;
  1595. h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
  1596. h8_set_mask (sd, memory_size - 1);
  1597. memset (h8_get_reg_buf (sd), 0, sizeof (((STATE_CPU (sd, 0))->regs)));
  1598. for (i = 0; i < 8; i++)
  1599. {
  1600. /* FIXME: rewrite using local buffer. */
  1601. unsigned char *p = (unsigned char *) (h8_get_reg_buf (sd) + i);
  1602. unsigned char *e = (unsigned char *) (h8_get_reg_buf (sd) + i + 1);
  1603. unsigned short *q = (unsigned short *) (h8_get_reg_buf (sd) + i);
  1604. unsigned short *u = (unsigned short *) (h8_get_reg_buf (sd) + i + 1);
  1605. h8_set_reg (sd, i, 0x00112233);
  1606. while (p < e)
  1607. {
  1608. if (*p == 0x22)
  1609. breg[i] = p;
  1610. if (*p == 0x33)
  1611. breg[i + 8] = p;
  1612. if (*p == 0x11)
  1613. breg[i + 16] = p;
  1614. if (*p == 0x00)
  1615. breg[i + 24] = p;
  1616. p++;
  1617. }
  1618. wreg[i] = wreg[i + 8] = 0;
  1619. while (q < u)
  1620. {
  1621. if (*q == 0x2233)
  1622. {
  1623. wreg[i] = q;
  1624. }
  1625. if (*q == 0x0011)
  1626. {
  1627. wreg[i + 8] = q;
  1628. }
  1629. q++;
  1630. }
  1631. if (wreg[i] == 0 || wreg[i + 8] == 0)
  1632. (*sim_callback->printf_filtered) (sim_callback,
  1633. "init_pointers: internal error.\n");
  1634. h8_set_reg (sd, i, 0);
  1635. lreg[i] = h8_get_reg_buf (sd) + i;
  1636. }
  1637. /* Note: sim uses pseudo-register ZERO as a zero register. */
  1638. lreg[ZERO_REGNUM] = h8_get_reg_buf (sd) + ZERO_REGNUM;
  1639. init_pointers_needed = 0;
  1640. /* Initialize the seg registers. */
  1641. if (!sd->sim_cache)
  1642. set_simcache_size (sd, CSIZE);
  1643. }
  1644. }
  1645. int
  1646. sim_stop (SIM_DESC sd)
  1647. {
  1648. /* FIXME: use a real signal value. */
  1649. sim_engine_set_run_state (sd, sim_stopped, SIGINT);
  1650. return 1;
  1651. }
  1652. #define OBITOP(name, f, s, op) \
  1653. case O (name, SB): \
  1654. { \
  1655. int m, tmp; \
  1656. \
  1657. if (f) \
  1658. if (fetch (sd, &code->dst, &ea)) \
  1659. goto end; \
  1660. if (fetch (sd, &code->src, &tmp)) \
  1661. goto end; \
  1662. m = 1 << (tmp & 7); \
  1663. op; \
  1664. if (s) \
  1665. if (store (sd, &code->dst,ea)) \
  1666. goto end; \
  1667. goto next; \
  1668. }
  1669. void
  1670. sim_resume (SIM_DESC sd, int step, int siggnal)
  1671. {
  1672. static int init1;
  1673. int cycles = 0;
  1674. int insts = 0;
  1675. int tick_start = get_now ();
  1676. int poll_count = 0;
  1677. int res;
  1678. int tmp;
  1679. int rd;
  1680. int ea;
  1681. int bit;
  1682. int pc;
  1683. int c, nz, v, n, u, h, ui, intMaskBit;
  1684. int trace, intMask;
  1685. int oldmask;
  1686. enum sim_stop reason;
  1687. int sigrc;
  1688. init_pointers (sd);
  1689. if (step)
  1690. {
  1691. sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
  1692. }
  1693. else
  1694. {
  1695. sim_engine_set_run_state (sd, sim_running, 0);
  1696. }
  1697. pc = h8_get_pc (sd);
  1698. /* The PC should never be odd. */
  1699. if (pc & 0x1)
  1700. {
  1701. sim_engine_set_run_state (sd, sim_stopped, SIGBUS);
  1702. return;
  1703. }
  1704. /* Get Status Register (flags). */
  1705. GETSR (sd);
  1706. if (h8300smode) /* Get exr. */
  1707. {
  1708. trace = (h8_get_exr (sd) >> 7) & 1;
  1709. intMask = h8_get_exr (sd) & 7;
  1710. }
  1711. oldmask = h8_get_mask (sd);
  1712. if (!h8300hmode || h8300_normal_mode)
  1713. h8_set_mask (sd, 0xffff);
  1714. do
  1715. {
  1716. unsigned short cidx;
  1717. decoded_inst *code;
  1718. top:
  1719. cidx = h8_get_cache_idx (sd, pc);
  1720. if (cidx == (unsigned short) -1 ||
  1721. cidx >= sd->sim_cache_size)
  1722. goto illegal;
  1723. code = sd->sim_cache + cidx;
  1724. #if ADEBUG
  1725. if (debug)
  1726. {
  1727. printf ("%x %d %s\n", pc, code->opcode,
  1728. code->op ? code->op->name : "**");
  1729. }
  1730. h8_increment_stats (sd, code->opcode);
  1731. #endif
  1732. if (code->opcode)
  1733. {
  1734. cycles += code->cycles;
  1735. insts++;
  1736. }
  1737. switch (code->opcode)
  1738. {
  1739. case 0:
  1740. /*
  1741. * This opcode is a fake for when we get to an
  1742. * instruction which hasnt been compiled
  1743. */
  1744. compile (sd, pc);
  1745. goto top;
  1746. break;
  1747. case O (O_MOVAB, SL):
  1748. case O (O_MOVAW, SL):
  1749. case O (O_MOVAL, SL):
  1750. /* 1) Evaluate 2nd argument (dst).
  1751. 2) Mask / zero extend according to whether 1st argument (src)
  1752. is INDEXB, INDEXW, or INDEXL.
  1753. 3) Left-shift the result by 0, 1 or 2, according to size of mova
  1754. (mova/b, mova/w, mova/l).
  1755. 4) Add literal value of 1st argument (src).
  1756. 5) Store result in 3rd argument (op3).
  1757. */
  1758. /* Alas, since this is the only instruction with 3 arguments,
  1759. decode doesn't handle them very well. Some fix-up is required.
  1760. a) The size of dst is determined by whether src is
  1761. INDEXB or INDEXW. */
  1762. if (OP_KIND (code->src.type) == OP_INDEXB)
  1763. code->dst.type = X (OP_KIND (code->dst.type), SB);
  1764. else if (OP_KIND (code->src.type) == OP_INDEXW)
  1765. code->dst.type = X (OP_KIND (code->dst.type), SW);
  1766. /* b) If op3 == null, then this is the short form of the insn.
  1767. Dst is the dispreg of src, and op3 is the 32-bit form
  1768. of the same register.
  1769. */
  1770. if (code->op3.type == 0)
  1771. {
  1772. /* Short form: src == INDEXB/INDEXW, dst == op3 == 0.
  1773. We get to compose dst and op3 as follows:
  1774. op3 is a 32-bit register, ID == src.reg.
  1775. dst is the same register, but 8 or 16 bits
  1776. depending on whether src is INDEXB or INDEXW.
  1777. */
  1778. code->op3.type = X (OP_REG, SL);
  1779. code->op3.reg = code->src.reg;
  1780. code->op3.literal = 0;
  1781. if (OP_KIND (code->src.type) == OP_INDEXB)
  1782. {
  1783. code->dst.type = X (OP_REG, SB);
  1784. code->dst.reg = code->op3.reg + 8;
  1785. }
  1786. else
  1787. code->dst.type = X (OP_REG, SW);
  1788. }
  1789. if (fetch (sd, &code->dst, &ea))
  1790. goto end;
  1791. switch (OP_KIND (code->src.type)) {
  1792. case OP_INDEXB: ea = ea & 0xff; break;
  1793. case OP_INDEXW: ea = ea & 0xffff; break;
  1794. case OP_INDEXL: break;
  1795. default: goto illegal;
  1796. }
  1797. switch (code->opcode) {
  1798. case O (O_MOVAB, SL): break;
  1799. case O (O_MOVAW, SL): ea = ea << 1; break;
  1800. case O (O_MOVAL, SL): ea = ea << 2; break;
  1801. default: goto illegal;
  1802. }
  1803. ea = ea + code->src.literal;
  1804. if (store (sd, &code->op3, ea))
  1805. goto end;
  1806. goto next;
  1807. case O (O_SUBX, SB): /* subx, extended sub */
  1808. if (fetch2 (sd, &code->dst, &rd))
  1809. goto end;
  1810. if (fetch (sd, &code->src, &ea))
  1811. goto end;
  1812. ea = -(ea + C);
  1813. res = rd + ea;
  1814. goto alu8;
  1815. case O (O_SUBX, SW): /* subx, extended sub */
  1816. if (fetch2 (sd, &code->dst, &rd))
  1817. goto end;
  1818. if (fetch (sd, &code->src, &ea))
  1819. goto end;
  1820. ea = -(ea + C);
  1821. res = rd + ea;
  1822. goto alu16;
  1823. case O (O_SUBX, SL): /* subx, extended sub */
  1824. if (fetch2 (sd, &code->dst, &rd))
  1825. goto end;
  1826. if (fetch (sd, &code->src, &ea))
  1827. goto end;
  1828. ea = -(ea + C);
  1829. res = rd + ea;
  1830. goto alu32;
  1831. case O (O_ADDX, SB): /* addx, extended add */
  1832. if (fetch2 (sd, &code->dst, &rd))
  1833. goto end;
  1834. if (fetch (sd, &code->src, &ea))
  1835. goto end;
  1836. ea = ea + C;
  1837. res = rd + ea;
  1838. goto alu8;
  1839. case O (O_ADDX, SW): /* addx, extended add */
  1840. if (fetch2 (sd, &code->dst, &rd))
  1841. goto end;
  1842. if (fetch (sd, &code->src, &ea))
  1843. goto end;
  1844. ea = ea + C;
  1845. res = rd + ea;
  1846. goto alu16;
  1847. case O (O_ADDX, SL): /* addx, extended add */
  1848. if (fetch2 (sd, &code->dst, &rd))
  1849. goto end;
  1850. if (fetch (sd, &code->src, &ea))
  1851. goto end;
  1852. ea = ea + C;
  1853. res = rd + ea;
  1854. goto alu32;
  1855. case O (O_SUB, SB): /* sub.b */
  1856. /* Fetch rd and ea. */
  1857. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1858. goto end;
  1859. ea = -ea;
  1860. res = rd + ea;
  1861. goto alu8;
  1862. case O (O_SUB, SW): /* sub.w */
  1863. /* Fetch rd and ea. */
  1864. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1865. goto end;
  1866. ea = -ea;
  1867. res = rd + ea;
  1868. goto alu16;
  1869. case O (O_SUB, SL): /* sub.l */
  1870. /* Fetch rd and ea. */
  1871. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1872. goto end;
  1873. ea = -ea;
  1874. res = rd + ea;
  1875. goto alu32;
  1876. case O (O_NEG, SB): /* neg.b */
  1877. /* Fetch ea. */
  1878. if (fetch2 (sd, &code->src, &ea))
  1879. goto end;
  1880. ea = -ea;
  1881. rd = 0;
  1882. res = rd + ea;
  1883. goto alu8;
  1884. case O (O_NEG, SW): /* neg.w */
  1885. /* Fetch ea. */
  1886. if (fetch2 (sd, &code->src, &ea))
  1887. goto end;
  1888. ea = -ea;
  1889. rd = 0;
  1890. res = rd + ea;
  1891. goto alu16;
  1892. case O (O_NEG, SL): /* neg.l */
  1893. /* Fetch ea. */
  1894. if (fetch2 (sd, &code->src, &ea))
  1895. goto end;
  1896. ea = -ea;
  1897. rd = 0;
  1898. res = rd + ea;
  1899. goto alu32;
  1900. case O (O_ADD, SB): /* add.b */
  1901. if (fetch2 (sd, &code->dst, &rd))
  1902. goto end;
  1903. if (fetch (sd, &code->src, &ea))
  1904. goto end;
  1905. res = rd + ea;
  1906. goto alu8;
  1907. case O (O_ADD, SW): /* add.w */
  1908. if (fetch2 (sd, &code->dst, &rd))
  1909. goto end;
  1910. if (fetch (sd, &code->src, &ea))
  1911. goto end;
  1912. res = rd + ea;
  1913. goto alu16;
  1914. case O (O_ADD, SL): /* add.l */
  1915. if (fetch2 (sd, &code->dst, &rd))
  1916. goto end;
  1917. if (fetch (sd, &code->src, &ea))
  1918. goto end;
  1919. res = rd + ea;
  1920. goto alu32;
  1921. case O (O_AND, SB): /* and.b */
  1922. /* Fetch rd and ea. */
  1923. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1924. goto end;
  1925. res = rd & ea;
  1926. goto log8;
  1927. case O (O_AND, SW): /* and.w */
  1928. /* Fetch rd and ea. */
  1929. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1930. goto end;
  1931. res = rd & ea;
  1932. goto log16;
  1933. case O (O_AND, SL): /* and.l */
  1934. /* Fetch rd and ea. */
  1935. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1936. goto end;
  1937. res = rd & ea;
  1938. goto log32;
  1939. case O (O_OR, SB): /* or.b */
  1940. /* Fetch rd and ea. */
  1941. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1942. goto end;
  1943. res = rd | ea;
  1944. goto log8;
  1945. case O (O_OR, SW): /* or.w */
  1946. /* Fetch rd and ea. */
  1947. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1948. goto end;
  1949. res = rd | ea;
  1950. goto log16;
  1951. case O (O_OR, SL): /* or.l */
  1952. /* Fetch rd and ea. */
  1953. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1954. goto end;
  1955. res = rd | ea;
  1956. goto log32;
  1957. case O (O_XOR, SB): /* xor.b */
  1958. /* Fetch rd and ea. */
  1959. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1960. goto end;
  1961. res = rd ^ ea;
  1962. goto log8;
  1963. case O (O_XOR, SW): /* xor.w */
  1964. /* Fetch rd and ea. */
  1965. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1966. goto end;
  1967. res = rd ^ ea;
  1968. goto log16;
  1969. case O (O_XOR, SL): /* xor.l */
  1970. /* Fetch rd and ea. */
  1971. if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
  1972. goto end;
  1973. res = rd ^ ea;
  1974. goto log32;
  1975. case O (O_MOV, SB):
  1976. if (fetch (sd, &code->src, &res))
  1977. goto end;
  1978. if (store (sd, &code->dst, res))
  1979. goto end;
  1980. goto just_flags_log8;
  1981. case O (O_MOV, SW):
  1982. if (fetch (sd, &code->src, &res))
  1983. goto end;
  1984. if (store (sd, &code->dst, res))
  1985. goto end;
  1986. goto just_flags_log16;
  1987. case O (O_MOV, SL):
  1988. if (fetch (sd, &code->src, &res))
  1989. goto end;
  1990. if (store (sd, &code->dst, res))
  1991. goto end;
  1992. goto just_flags_log32;
  1993. case O (O_MOVMD, SB): /* movmd.b */
  1994. ea = GET_W_REG (4);
  1995. if (ea == 0)
  1996. ea = 0x10000;
  1997. while (ea--)
  1998. {
  1999. rd = GET_MEMORY_B (GET_L_REG (5));
  2000. SET_MEMORY_B (GET_L_REG (6), rd);
  2001. SET_L_REG (5, GET_L_REG (5) + 1);
  2002. SET_L_REG (6, GET_L_REG (6) + 1);
  2003. SET_W_REG (4, ea);
  2004. }
  2005. goto next;
  2006. case O (O_MOVMD, SW): /* movmd.w */
  2007. ea = GET_W_REG (4);
  2008. if (ea == 0)
  2009. ea = 0x10000;
  2010. while (ea--)
  2011. {
  2012. rd = GET_MEMORY_W (GET_L_REG (5));
  2013. SET_MEMORY_W (GET_L_REG (6), rd);
  2014. SET_L_REG (5, GET_L_REG (5) + 2);
  2015. SET_L_REG (6, GET_L_REG (6) + 2);
  2016. SET_W_REG (4, ea);
  2017. }
  2018. goto next;
  2019. case O (O_MOVMD, SL): /* movmd.l */
  2020. ea = GET_W_REG (4);
  2021. if (ea == 0)
  2022. ea = 0x10000;
  2023. while (ea--)
  2024. {
  2025. rd = GET_MEMORY_L (GET_L_REG (5));
  2026. SET_MEMORY_L (GET_L_REG (6), rd);
  2027. SET_L_REG (5, GET_L_REG (5) + 4);
  2028. SET_L_REG (6, GET_L_REG (6) + 4);
  2029. SET_W_REG (4, ea);
  2030. }
  2031. goto next;
  2032. case O (O_MOVSD, SB): /* movsd.b */
  2033. /* This instruction implements strncpy, with a conditional branch.
  2034. r4 contains n, r5 contains src, and r6 contains dst.
  2035. The 16-bit displacement operand is added to the pc
  2036. if and only if the end of string is reached before
  2037. n bytes are transferred. */
  2038. ea = GET_L_REG (4) & 0xffff;
  2039. if (ea == 0)
  2040. ea = 0x10000;
  2041. while (ea--)
  2042. {
  2043. rd = GET_MEMORY_B (GET_L_REG (5));
  2044. SET_MEMORY_B (GET_L_REG (6), rd);
  2045. SET_L_REG (5, GET_L_REG (5) + 1);
  2046. SET_L_REG (6, GET_L_REG (6) + 1);
  2047. SET_W_REG (4, ea);
  2048. if (rd == 0)
  2049. goto condtrue;
  2050. }
  2051. goto next;
  2052. case O (O_EEPMOV, SB): /* eepmov.b */
  2053. case O (O_EEPMOV, SW): /* eepmov.w */
  2054. if (h8300hmode || h8300smode)
  2055. {
  2056. register unsigned char *_src, *_dst;
  2057. unsigned int count = ((code->opcode == O (O_EEPMOV, SW))
  2058. ? h8_get_reg (sd, R4_REGNUM) & 0xffff
  2059. : h8_get_reg (sd, R4_REGNUM) & 0xff);
  2060. _src = (h8_get_reg (sd, R5_REGNUM) < memory_size
  2061. ? h8_get_memory_buf (sd) + h8_get_reg (sd, R5_REGNUM)
  2062. : h8_get_eightbit_buf (sd) +
  2063. (h8_get_reg (sd, R5_REGNUM) & 0xff));
  2064. if ((_src + count) >= (h8_get_memory_buf (sd) + memory_size))
  2065. {
  2066. if ((_src + count) >= (h8_get_eightbit_buf (sd) + 0x100))
  2067. goto illegal;
  2068. }
  2069. _dst = (h8_get_reg (sd, R6_REGNUM) < memory_size
  2070. ? h8_get_memory_buf (sd) + h8_get_reg (sd, R6_REGNUM)
  2071. : h8_get_eightbit_buf (sd) +
  2072. (h8_get_reg (sd, R6_REGNUM) & 0xff));
  2073. if ((_dst + count) >= (h8_get_memory_buf (sd) + memory_size))
  2074. {
  2075. if ((_dst + count) >= (h8_get_eightbit_buf (sd) + 0x100))
  2076. goto illegal;
  2077. }
  2078. memcpy (_dst, _src, count);
  2079. h8_set_reg (sd, R5_REGNUM, h8_get_reg (sd, R5_REGNUM) + count);
  2080. h8_set_reg (sd, R6_REGNUM, h8_get_reg (sd, R6_REGNUM) + count);
  2081. h8_set_reg (sd, R4_REGNUM, h8_get_reg (sd, R4_REGNUM) &
  2082. ((code->opcode == O (O_EEPMOV, SW))
  2083. ? (~0xffff) : (~0xff)));
  2084. cycles += 2 * count;
  2085. goto next;
  2086. }
  2087. goto illegal;
  2088. case O (O_ADDS, SL): /* adds (.l) */
  2089. /* FIXME fetch.
  2090. * This insn only uses register operands, but still
  2091. * it would be cleaner to use fetch and store... */
  2092. SET_L_REG (code->dst.reg,
  2093. GET_L_REG (code->dst.reg)
  2094. + code->src.literal);
  2095. goto next;
  2096. case O (O_SUBS, SL): /* subs (.l) */
  2097. /* FIXME fetch.
  2098. * This insn only uses register operands, but still
  2099. * it would be cleaner to use fetch and store... */
  2100. SET_L_REG (code->dst.reg,
  2101. GET_L_REG (code->dst.reg)
  2102. - code->src.literal);
  2103. goto next;
  2104. case O (O_CMP, SB): /* cmp.b */
  2105. if (fetch (sd, &code->dst, &rd))
  2106. goto end;
  2107. if (fetch (sd, &code->src, &ea))
  2108. goto end;
  2109. ea = -ea;
  2110. res = rd + ea;
  2111. goto just_flags_alu8;
  2112. case O (O_CMP, SW): /* cmp.w */
  2113. if (fetch (sd, &code->dst, &rd))
  2114. goto end;
  2115. if (fetch (sd, &code->src, &ea))
  2116. goto end;
  2117. ea = -ea;
  2118. res = rd + ea;
  2119. goto just_flags_alu16;
  2120. case O (O_CMP, SL): /* cmp.l */
  2121. if (fetch (sd, &code->dst, &rd))
  2122. goto end;
  2123. if (fetch (sd, &code->src, &ea))
  2124. goto end;
  2125. ea = -ea;
  2126. res = rd + ea;
  2127. goto just_flags_alu32;
  2128. case O (O_DEC, SB): /* dec.b */
  2129. /* FIXME fetch.
  2130. * This insn only uses register operands, but still
  2131. * it would be cleaner to use fetch and store... */
  2132. rd = GET_B_REG (code->src.reg);
  2133. ea = -1;
  2134. res = rd + ea;
  2135. SET_B_REG (code->src.reg, res);
  2136. goto just_flags_inc8;
  2137. case O (O_DEC, SW): /* dec.w */
  2138. /* FIXME fetch.
  2139. * This insn only uses register operands, but still
  2140. * it would be cleaner to use fetch and store... */
  2141. rd = GET_W_REG (code->dst.reg);
  2142. ea = -code->src.literal;
  2143. res = rd + ea;
  2144. SET_W_REG (code->dst.reg, res);
  2145. goto just_flags_inc16;
  2146. case O (O_DEC, SL): /* dec.l */
  2147. /* FIXME fetch.
  2148. * This insn only uses register operands, but still
  2149. * it would be cleaner to use fetch and store... */
  2150. rd = GET_L_REG (code->dst.reg);
  2151. ea = -code->src.literal;
  2152. res = rd + ea;
  2153. SET_L_REG (code->dst.reg, res);
  2154. goto just_flags_inc32;
  2155. case O (O_INC, SB): /* inc.b */
  2156. /* FIXME fetch.
  2157. * This insn only uses register operands, but still
  2158. * it would be cleaner to use fetch and store... */
  2159. rd = GET_B_REG (code->src.reg);
  2160. ea = 1;
  2161. res = rd + ea;
  2162. SET_B_REG (code->src.reg, res);
  2163. goto just_flags_inc8;
  2164. case O (O_INC, SW): /* inc.w */
  2165. /* FIXME fetch.
  2166. * This insn only uses register operands, but still
  2167. * it would be cleaner to use fetch and store... */
  2168. rd = GET_W_REG (code->dst.reg);
  2169. ea = code->src.literal;
  2170. res = rd + ea;
  2171. SET_W_REG (code->dst.reg, res);
  2172. goto just_flags_inc16;
  2173. case O (O_INC, SL): /* inc.l */
  2174. /* FIXME fetch.
  2175. * This insn only uses register operands, but still
  2176. * it would be cleaner to use fetch and store... */
  2177. rd = GET_L_REG (code->dst.reg);
  2178. ea = code->src.literal;
  2179. res = rd + ea;
  2180. SET_L_REG (code->dst.reg, res);
  2181. goto just_flags_inc32;
  2182. case O (O_LDC, SB): /* ldc.b */
  2183. if (fetch (sd, &code->src, &res))
  2184. goto end;
  2185. goto setc;
  2186. case O (O_LDC, SW): /* ldc.w */
  2187. if (fetch (sd, &code->src, &res))
  2188. goto end;
  2189. /* Word operand, value from MSB, must be shifted. */
  2190. res >>= 8;
  2191. goto setc;
  2192. case O (O_LDC, SL): /* ldc.l */
  2193. if (fetch (sd, &code->src, &res))
  2194. goto end;
  2195. switch (code->dst.type) {
  2196. case X (OP_SBR, SL):
  2197. h8_set_sbr (sd, res);
  2198. break;
  2199. case X (OP_VBR, SL):
  2200. h8_set_vbr (sd, res);
  2201. break;
  2202. default:
  2203. goto illegal;
  2204. }
  2205. goto next;
  2206. case O (O_STC, SW): /* stc.w */
  2207. case O (O_STC, SB): /* stc.b */
  2208. if (code->src.type == X (OP_CCR, SB))
  2209. {
  2210. BUILDSR (sd);
  2211. res = h8_get_ccr (sd);
  2212. }
  2213. else if (code->src.type == X (OP_EXR, SB) && h8300smode)
  2214. {
  2215. if (h8300smode)
  2216. h8_set_exr (sd, (trace << 7) | intMask);
  2217. res = h8_get_exr (sd);
  2218. }
  2219. else
  2220. goto illegal;
  2221. /* Word operand, value to MSB, must be shifted. */
  2222. if (code->opcode == X (O_STC, SW))
  2223. res <<= 8;
  2224. if (store (sd, &code->dst, res))
  2225. goto end;
  2226. goto next;
  2227. case O (O_STC, SL): /* stc.l */
  2228. switch (code->src.type) {
  2229. case X (OP_SBR, SL):
  2230. res = h8_get_sbr (sd);
  2231. break;
  2232. case X (OP_VBR, SL):
  2233. res = h8_get_vbr (sd);
  2234. break;
  2235. default:
  2236. goto illegal;
  2237. }
  2238. if (store (sd, &code->dst, res))
  2239. goto end;
  2240. goto next;
  2241. case O (O_ANDC, SB): /* andc.b */
  2242. if (code->dst.type == X (OP_CCR, SB))
  2243. {
  2244. BUILDSR (sd);
  2245. rd = h8_get_ccr (sd);
  2246. }
  2247. else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
  2248. {
  2249. if (h8300smode)
  2250. h8_set_exr (sd, (trace << 7) | intMask);
  2251. rd = h8_get_exr (sd);
  2252. }
  2253. else
  2254. goto illegal;
  2255. ea = code->src.literal;
  2256. res = rd & ea;
  2257. goto setc;
  2258. case O (O_ORC, SB): /* orc.b */
  2259. if (code->dst.type == X (OP_CCR, SB))
  2260. {
  2261. BUILDSR (sd);
  2262. rd = h8_get_ccr (sd);
  2263. }
  2264. else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
  2265. {
  2266. if (h8300smode)
  2267. h8_set_exr (sd, (trace << 7) | intMask);
  2268. rd = h8_get_exr (sd);
  2269. }
  2270. else
  2271. goto illegal;
  2272. ea = code->src.literal;
  2273. res = rd | ea;
  2274. goto setc;
  2275. case O (O_XORC, SB): /* xorc.b */
  2276. if (code->dst.type == X (OP_CCR, SB))
  2277. {
  2278. BUILDSR (sd);
  2279. rd = h8_get_ccr (sd);
  2280. }
  2281. else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
  2282. {
  2283. if (h8300smode)
  2284. h8_set_exr (sd, (trace << 7) | intMask);
  2285. rd = h8_get_exr (sd);
  2286. }
  2287. else
  2288. goto illegal;
  2289. ea = code->src.literal;
  2290. res = rd ^ ea;
  2291. goto setc;
  2292. case O (O_BRAS, SB): /* bra/s */
  2293. /* This is basically an ordinary branch, with a delay slot. */
  2294. if (fetch (sd, &code->src, &res))
  2295. goto end;
  2296. if ((res & 1) == 0)
  2297. goto illegal;
  2298. res -= 1;
  2299. /* Execution continues at next instruction, but
  2300. delayed_branch is set up for next cycle. */
  2301. h8_set_delayed_branch (sd, code->next_pc + res);
  2302. pc = code->next_pc;
  2303. goto end;
  2304. case O (O_BRAB, SB): /* bra rd.b */
  2305. case O (O_BRAW, SW): /* bra rd.w */
  2306. case O (O_BRAL, SL): /* bra erd.l */
  2307. if (fetch (sd, &code->src, &rd))
  2308. goto end;
  2309. switch (OP_SIZE (code->opcode)) {
  2310. case SB: rd &= 0xff; break;
  2311. case SW: rd &= 0xffff; break;
  2312. case SL: rd &= 0xffffffff; break;
  2313. }
  2314. pc = code->next_pc + rd;
  2315. goto end;
  2316. case O (O_BRABC, SB): /* bra/bc, branch if bit clear */
  2317. case O (O_BRABS, SB): /* bra/bs, branch if bit set */
  2318. case O (O_BSRBC, SB): /* bsr/bc, call if bit clear */
  2319. case O (O_BSRBS, SB): /* bsr/bs, call if bit set */
  2320. if (fetch (sd, &code->dst, &rd) ||
  2321. fetch (sd, &code->src, &bit))
  2322. goto end;
  2323. if (code->opcode == O (O_BRABC, SB) || /* branch if clear */
  2324. code->opcode == O (O_BSRBC, SB)) /* call if clear */
  2325. {
  2326. if ((rd & (1 << bit))) /* no branch */
  2327. goto next;
  2328. }
  2329. else /* branch/call if set */
  2330. {
  2331. if (!(rd & (1 << bit))) /* no branch */
  2332. goto next;
  2333. }
  2334. if (fetch (sd, &code->op3, &res)) /* branch */
  2335. goto end;
  2336. pc = code->next_pc + res;
  2337. if (code->opcode == O (O_BRABC, SB) ||
  2338. code->opcode == O (O_BRABS, SB)) /* branch */
  2339. goto end;
  2340. else /* call */
  2341. goto call;
  2342. case O (O_BRA, SN):
  2343. case O (O_BRA, SL):
  2344. case O (O_BRA, SW):
  2345. case O (O_BRA, SB): /* bra, branch always */
  2346. if (1)
  2347. goto condtrue;
  2348. goto next;
  2349. case O (O_BRN, SB): /* brn, ;-/ branch never? */
  2350. if (0)
  2351. goto condtrue;
  2352. goto next;
  2353. case O (O_BHI, SB): /* bhi */
  2354. if ((C || Z) == 0)
  2355. goto condtrue;
  2356. goto next;
  2357. case O (O_BLS, SB): /* bls */
  2358. if ((C || Z))
  2359. goto condtrue;
  2360. goto next;
  2361. case O (O_BCS, SB): /* bcs, branch if carry set */
  2362. if ((C == 1))
  2363. goto condtrue;
  2364. goto next;
  2365. case O (O_BCC, SB): /* bcc, branch if carry clear */
  2366. if ((C == 0))
  2367. goto condtrue;
  2368. goto next;
  2369. case O (O_BEQ, SB): /* beq, branch if zero set */
  2370. if (Z)
  2371. goto condtrue;
  2372. goto next;
  2373. case O (O_BGT, SB): /* bgt */
  2374. if (((Z || (N ^ V)) == 0))
  2375. goto condtrue;
  2376. goto next;
  2377. case O (O_BLE, SB): /* ble */
  2378. if (((Z || (N ^ V)) == 1))
  2379. goto condtrue;
  2380. goto next;
  2381. case O (O_BGE, SB): /* bge */
  2382. if ((N ^ V) == 0)
  2383. goto condtrue;
  2384. goto next;
  2385. case O (O_BLT, SB): /* blt */
  2386. if ((N ^ V))
  2387. goto condtrue;
  2388. goto next;
  2389. case O (O_BMI, SB): /* bmi */
  2390. if ((N))
  2391. goto condtrue;
  2392. goto next;
  2393. case O (O_BNE, SB): /* bne, branch if zero clear */
  2394. if ((Z == 0))
  2395. goto condtrue;
  2396. goto next;
  2397. case O (O_BPL, SB): /* bpl */
  2398. if (N == 0)
  2399. goto condtrue;
  2400. goto next;
  2401. case O (O_BVC, SB): /* bvc */
  2402. if ((V == 0))
  2403. goto condtrue;
  2404. goto next;
  2405. case O (O_BVS, SB): /* bvs */
  2406. if ((V == 1))
  2407. goto condtrue;
  2408. goto next;
  2409. /* Trap for Command Line setup. */
  2410. case O (O_SYS_CMDLINE, SB):
  2411. {
  2412. int i = 0; /* Loop counter. */
  2413. int j = 0; /* Loop counter. */
  2414. int ind_arg_len = 0; /* Length of each argument. */
  2415. int no_of_args = 0; /* The no. or cmdline args. */
  2416. int current_location = 0; /* Location of string. */
  2417. int old_sp = 0; /* The Initial Stack Pointer. */
  2418. int no_of_slots = 0; /* No. of slots required on the stack
  2419. for storing cmdline args. */
  2420. int sp_move = 0; /* No. of locations by which the stack needs
  2421. to grow. */
  2422. int new_sp = 0; /* The final stack pointer location passed
  2423. back. */
  2424. int *argv_ptrs; /* Pointers of argv strings to be stored. */
  2425. int argv_ptrs_location = 0; /* Location of pointers to cmdline
  2426. args on the stack. */
  2427. int char_ptr_size = 0; /* Size of a character pointer on
  2428. target machine. */
  2429. int addr_cmdline = 0; /* Memory location where cmdline has
  2430. to be stored. */
  2431. int size_cmdline = 0; /* Size of cmdline. */
  2432. /* Set the address of 256 free locations where command line is
  2433. stored. */
  2434. addr_cmdline = cmdline_location();
  2435. h8_set_reg (sd, 0, addr_cmdline);
  2436. /* Counting the no. of commandline arguments. */
  2437. for (i = 0; h8_get_cmdline_arg (sd, i) != NULL; i++)
  2438. continue;
  2439. /* No. of arguments in the command line. */
  2440. no_of_args = i;
  2441. /* Current location is just a temporary variable,which we are
  2442. setting to the point to the start of our commandline string. */
  2443. current_location = addr_cmdline;
  2444. /* Allocating space for storing pointers of the command line
  2445. arguments. */
  2446. argv_ptrs = (int *) malloc (sizeof (int) * no_of_args);
  2447. /* Setting char_ptr_size to the sizeof (char *) on the different
  2448. architectures. */
  2449. if ((h8300hmode || h8300smode) && !h8300_normal_mode)
  2450. {
  2451. char_ptr_size = 4;
  2452. }
  2453. else
  2454. {
  2455. char_ptr_size = 2;
  2456. }
  2457. for (i = 0; i < no_of_args; i++)
  2458. {
  2459. ind_arg_len = 0;
  2460. /* The size of the commandline argument. */
  2461. ind_arg_len = strlen (h8_get_cmdline_arg (sd, i)) + 1;
  2462. /* The total size of the command line string. */
  2463. size_cmdline += ind_arg_len;
  2464. /* As we have only 256 bytes, we need to provide a graceful
  2465. exit. Anyways, a program using command line arguments
  2466. where we cannot store all the command line arguments
  2467. given may behave unpredictably. */
  2468. if (size_cmdline >= 256)
  2469. {
  2470. h8_set_reg (sd, 0, 0);
  2471. goto next;
  2472. }
  2473. else
  2474. {
  2475. /* current_location points to the memory where the next
  2476. commandline argument is stored. */
  2477. argv_ptrs[i] = current_location;
  2478. for (j = 0; j < ind_arg_len; j++)
  2479. {
  2480. SET_MEMORY_B ((current_location +
  2481. (sizeof (char) * j)),
  2482. *(h8_get_cmdline_arg (sd, i) +
  2483. sizeof (char) * j));
  2484. }
  2485. /* Setting current_location to the starting of next
  2486. argument. */
  2487. current_location += ind_arg_len;
  2488. }
  2489. }
  2490. /* This is the original position of the stack pointer. */
  2491. old_sp = h8_get_reg (sd, SP_REGNUM);
  2492. /* We need space from the stack to store the pointers to argvs. */
  2493. /* As we will infringe on the stack, we need to shift the stack
  2494. pointer so that the data is not overwritten. We calculate how
  2495. much space is required. */
  2496. sp_move = (no_of_args) * (char_ptr_size);
  2497. /* The final position of stack pointer, we have thus taken some
  2498. space from the stack. */
  2499. new_sp = old_sp - sp_move;
  2500. /* Temporary variable holding value where the argv pointers need
  2501. to be stored. */
  2502. argv_ptrs_location = new_sp;
  2503. /* The argv pointers are stored at sequential locations. As per
  2504. the H8300 ABI. */
  2505. for (i = 0; i < no_of_args; i++)
  2506. {
  2507. /* Saving the argv pointer. */
  2508. if ((h8300hmode || h8300smode) && !h8300_normal_mode)
  2509. {
  2510. SET_MEMORY_L (argv_ptrs_location, argv_ptrs[i]);
  2511. }
  2512. else
  2513. {
  2514. SET_MEMORY_W (argv_ptrs_location, argv_ptrs[i]);
  2515. }
  2516. /* The next location where the pointer to the next argv
  2517. string has to be stored. */
  2518. argv_ptrs_location += char_ptr_size;
  2519. }
  2520. /* Required by POSIX, Setting 0x0 at the end of the list of argv
  2521. pointers. */
  2522. if ((h8300hmode || h8300smode) && !h8300_normal_mode)
  2523. {
  2524. SET_MEMORY_L (old_sp, 0x0);
  2525. }
  2526. else
  2527. {
  2528. SET_MEMORY_W (old_sp, 0x0);
  2529. }
  2530. /* Freeing allocated memory. */
  2531. free (argv_ptrs);
  2532. for (i = 0; i <= no_of_args; i++)
  2533. {
  2534. free (h8_get_cmdline_arg (sd, i));
  2535. }
  2536. free (h8_get_command_line (sd));
  2537. /* The no. of argv arguments are returned in Reg 0. */
  2538. h8_set_reg (sd, 0, no_of_args);
  2539. /* The Pointer to argv in Register 1. */
  2540. h8_set_reg (sd, 1, new_sp);
  2541. /* Setting the stack pointer to the new value. */
  2542. h8_set_reg (sd, SP_REGNUM, new_sp);
  2543. }
  2544. goto next;
  2545. /* System call processing starts. */
  2546. case O (O_SYS_OPEN, SB):
  2547. {
  2548. int len = 0; /* Length of filename. */
  2549. char *filename; /* Filename would go here. */
  2550. char temp_char; /* Temporary character */
  2551. int mode = 0; /* Mode bits for the file. */
  2552. int open_return; /* Return value of open, file descriptor. */
  2553. int i; /* Loop counter */
  2554. int filename_ptr; /* Pointer to filename in cpu memory. */
  2555. /* Setting filename_ptr to first argument of open, */
  2556. /* and trying to get mode. */
  2557. if ((h8300sxmode || h8300hmode || h8300smode) && !h8300_normal_mode)
  2558. {
  2559. filename_ptr = GET_L_REG (0);
  2560. mode = GET_MEMORY_L (h8_get_reg (sd, SP_REGNUM) + 4);
  2561. }
  2562. else
  2563. {
  2564. filename_ptr = GET_W_REG (0);
  2565. mode = GET_MEMORY_W (h8_get_reg (sd, SP_REGNUM) + 2);
  2566. }
  2567. /* Trying to find the length of the filename. */
  2568. temp_char = GET_MEMORY_B (h8_get_reg (sd, 0));
  2569. len = 1;
  2570. while (temp_char != '\0')
  2571. {
  2572. temp_char = GET_MEMORY_B (filename_ptr + len);
  2573. len++;
  2574. }
  2575. /* Allocating space for the filename. */
  2576. filename = (char *) malloc (sizeof (char) * len);
  2577. /* String copying the filename from memory. */
  2578. for (i = 0; i < len; i++)
  2579. {
  2580. temp_char = GET_MEMORY_B (filename_ptr + i);
  2581. filename[i] = temp_char;
  2582. }
  2583. /* Callback to open and return the file descriptor. */
  2584. open_return = sim_callback->open (sim_callback, filename, mode);
  2585. /* Return value in register 0. */
  2586. h8_set_reg (sd, 0, open_return);
  2587. /* Freeing memory used for filename. */
  2588. free (filename);
  2589. }
  2590. goto next;
  2591. case O (O_SYS_READ, SB):
  2592. {
  2593. char *char_ptr; /* Where characters read would be stored. */
  2594. int fd; /* File descriptor */
  2595. int buf_size; /* BUF_SIZE parameter in read. */
  2596. int i = 0; /* Temporary Loop counter */
  2597. int read_return = 0; /* Return value from callback to
  2598. read. */
  2599. fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
  2600. buf_size = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (2) : GET_W_REG (2);
  2601. char_ptr = (char *) malloc (sizeof (char) * buf_size);
  2602. /* Callback to read and return the no. of characters read. */
  2603. read_return =
  2604. sim_callback->read (sim_callback, fd, char_ptr, buf_size);
  2605. /* The characters read are stored in cpu memory. */
  2606. for (i = 0; i < buf_size; i++)
  2607. {
  2608. SET_MEMORY_B ((h8_get_reg (sd, 1) + (sizeof (char) * i)),
  2609. *(char_ptr + (sizeof (char) * i)));
  2610. }
  2611. /* Return value in Register 0. */
  2612. h8_set_reg (sd, 0, read_return);
  2613. /* Freeing memory used as buffer. */
  2614. free (char_ptr);
  2615. }
  2616. goto next;
  2617. case O (O_SYS_WRITE, SB):
  2618. {
  2619. int fd; /* File descriptor */
  2620. char temp_char; /* Temporary character */
  2621. int len; /* Length of write, Parameter II to write. */
  2622. int char_ptr; /* Character Pointer, Parameter I of write. */
  2623. char *ptr; /* Where characters to be written are stored.
  2624. */
  2625. int write_return; /* Return value from callback to write. */
  2626. int i = 0; /* Loop counter */
  2627. fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
  2628. char_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
  2629. len = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (2) : GET_W_REG (2);
  2630. /* Allocating space for the characters to be written. */
  2631. ptr = (char *) malloc (sizeof (char) * len);
  2632. /* Fetching the characters from cpu memory. */
  2633. for (i = 0; i < len; i++)
  2634. {
  2635. temp_char = GET_MEMORY_B (char_ptr + i);
  2636. ptr[i] = temp_char;
  2637. }
  2638. /* Callback write and return the no. of characters written. */
  2639. write_return = sim_callback->write (sim_callback, fd, ptr, len);
  2640. /* Return value in Register 0. */
  2641. h8_set_reg (sd, 0, write_return);
  2642. /* Freeing memory used as buffer. */
  2643. free (ptr);
  2644. }
  2645. goto next;
  2646. case O (O_SYS_LSEEK, SB):
  2647. {
  2648. int fd; /* File descriptor */
  2649. int offset; /* Offset */
  2650. int origin; /* Origin */
  2651. int lseek_return; /* Return value from callback to lseek. */
  2652. fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
  2653. offset = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
  2654. origin = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (2) : GET_W_REG (2);
  2655. /* Callback lseek and return offset. */
  2656. lseek_return =
  2657. sim_callback->lseek (sim_callback, fd, offset, origin);
  2658. /* Return value in register 0. */
  2659. h8_set_reg (sd, 0, lseek_return);
  2660. }
  2661. goto next;
  2662. case O (O_SYS_CLOSE, SB):
  2663. {
  2664. int fd; /* File descriptor */
  2665. int close_return; /* Return value from callback to close. */
  2666. fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
  2667. /* Callback close and return. */
  2668. close_return = sim_callback->close (sim_callback, fd);
  2669. /* Return value in register 0. */
  2670. h8_set_reg (sd, 0, close_return);
  2671. }
  2672. goto next;
  2673. case O (O_SYS_FSTAT, SB):
  2674. {
  2675. int fd; /* File descriptor */
  2676. struct stat stat_rec; /* Stat record */
  2677. int fstat_return; /* Return value from callback to stat. */
  2678. int stat_ptr; /* Pointer to stat record. */
  2679. char *temp_stat_ptr; /* Temporary stat_rec pointer. */
  2680. fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
  2681. /* Setting stat_ptr to second argument of stat. */
  2682. stat_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
  2683. /* Callback stat and return. */
  2684. fstat_return = sim_callback->to_fstat (sim_callback, fd,
  2685. &stat_rec);
  2686. /* Have stat_ptr point to starting of stat_rec. */
  2687. temp_stat_ptr = (char *) (&stat_rec);
  2688. /* Setting up the stat structure returned. */
  2689. SET_MEMORY_W (stat_ptr, stat_rec.st_dev);
  2690. stat_ptr += 2;
  2691. SET_MEMORY_W (stat_ptr, stat_rec.st_ino);
  2692. stat_ptr += 2;
  2693. SET_MEMORY_L (stat_ptr, stat_rec.st_mode);
  2694. stat_ptr += 4;
  2695. SET_MEMORY_W (stat_ptr, stat_rec.st_nlink);
  2696. stat_ptr += 2;
  2697. SET_MEMORY_W (stat_ptr, stat_rec.st_uid);
  2698. stat_ptr += 2;
  2699. SET_MEMORY_W (stat_ptr, stat_rec.st_gid);
  2700. stat_ptr += 2;
  2701. SET_MEMORY_W (stat_ptr, stat_rec.st_rdev);
  2702. stat_ptr += 2;
  2703. SET_MEMORY_L (stat_ptr, stat_rec.st_size);
  2704. stat_ptr += 4;
  2705. SET_MEMORY_L (stat_ptr, stat_rec.st_atime);
  2706. stat_ptr += 8;
  2707. SET_MEMORY_L (stat_ptr, stat_rec.st_mtime);
  2708. stat_ptr += 8;
  2709. SET_MEMORY_L (stat_ptr, stat_rec.st_ctime);
  2710. /* Return value in register 0. */
  2711. h8_set_reg (sd, 0, fstat_return);
  2712. }
  2713. goto next;
  2714. case O (O_SYS_STAT, SB):
  2715. {
  2716. int len = 0; /* Length of filename. */
  2717. char *filename; /* Filename would go here. */
  2718. char temp_char; /* Temporary character */
  2719. int filename_ptr; /* Pointer to filename in cpu memory. */
  2720. struct stat stat_rec; /* Stat record */
  2721. int stat_return; /* Return value from callback to stat */
  2722. int stat_ptr; /* Pointer to stat record. */
  2723. char *temp_stat_ptr; /* Temporary stat_rec pointer. */
  2724. int i = 0; /* Loop Counter */
  2725. /* Setting filename_ptr to first argument of open. */
  2726. filename_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
  2727. /* Trying to find the length of the filename. */
  2728. temp_char = GET_MEMORY_B (h8_get_reg (sd, 0));
  2729. len = 1;
  2730. while (temp_char != '\0')
  2731. {
  2732. temp_char = GET_MEMORY_B (filename_ptr + len);
  2733. len++;
  2734. }
  2735. /* Allocating space for the filename. */
  2736. filename = (char *) malloc (sizeof (char) * len);
  2737. /* String copying the filename from memory. */
  2738. for (i = 0; i < len; i++)
  2739. {
  2740. temp_char = GET_MEMORY_B (filename_ptr + i);
  2741. filename[i] = temp_char;
  2742. }
  2743. /* Setting stat_ptr to second argument of stat. */
  2744. /* stat_ptr = h8_get_reg (sd, 1); */
  2745. stat_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
  2746. /* Callback stat and return. */
  2747. stat_return =
  2748. sim_callback->to_stat (sim_callback, filename, &stat_rec);
  2749. /* Have stat_ptr point to starting of stat_rec. */
  2750. temp_stat_ptr = (char *) (&stat_rec);
  2751. /* Freeing memory used for filename. */
  2752. free (filename);
  2753. /* Setting up the stat structure returned. */
  2754. SET_MEMORY_W (stat_ptr, stat_rec.st_dev);
  2755. stat_ptr += 2;
  2756. SET_MEMORY_W (stat_ptr, stat_rec.st_ino);
  2757. stat_ptr += 2;
  2758. SET_MEMORY_L (stat_ptr, stat_rec.st_mode);
  2759. stat_ptr += 4;
  2760. SET_MEMORY_W (stat_ptr, stat_rec.st_nlink);
  2761. stat_ptr += 2;
  2762. SET_MEMORY_W (stat_ptr, stat_rec.st_uid);
  2763. stat_ptr += 2;
  2764. SET_MEMORY_W (stat_ptr, stat_rec.st_gid);
  2765. stat_ptr += 2;
  2766. SET_MEMORY_W (stat_ptr, stat_rec.st_rdev);
  2767. stat_ptr += 2;
  2768. SET_MEMORY_L (stat_ptr, stat_rec.st_size);
  2769. stat_ptr += 4;
  2770. SET_MEMORY_L (stat_ptr, stat_rec.st_atime);
  2771. stat_ptr += 8;
  2772. SET_MEMORY_L (stat_ptr, stat_rec.st_mtime);
  2773. stat_ptr += 8;
  2774. SET_MEMORY_L (stat_ptr, stat_rec.st_ctime);
  2775. /* Return value in register 0. */
  2776. h8_set_reg (sd, 0, stat_return);
  2777. }
  2778. goto next;
  2779. /* End of system call processing. */
  2780. case O (O_NOT, SB): /* not.b */
  2781. if (fetch2 (sd, &code->src, &rd))
  2782. goto end;
  2783. rd = ~rd;
  2784. v = 0;
  2785. goto shift8;
  2786. case O (O_NOT, SW): /* not.w */
  2787. if (fetch2 (sd, &code->src, &rd))
  2788. goto end;
  2789. rd = ~rd;
  2790. v = 0;
  2791. goto shift16;
  2792. case O (O_NOT, SL): /* not.l */
  2793. if (fetch2 (sd, &code->src, &rd))
  2794. goto end;
  2795. rd = ~rd;
  2796. v = 0;
  2797. goto shift32;
  2798. case O (O_SHLL, SB): /* shll.b */
  2799. case O (O_SHLR, SB): /* shlr.b */
  2800. if (fetch2 (sd, &code->dst, &rd))
  2801. goto end;
  2802. if (memcmp (&code->src, &code->dst, sizeof (code->src)) == 0)
  2803. ea = 1; /* unary op */
  2804. else /* binary op */
  2805. fetch (sd, &code->src, &ea);
  2806. if (code->opcode == O (O_SHLL, SB))
  2807. {
  2808. v = (ea > 8);
  2809. c = rd & (0x80 >> (ea - 1));
  2810. rd <<= ea;
  2811. }
  2812. else
  2813. {
  2814. v = 0;
  2815. c = rd & (1 << (ea - 1));
  2816. rd = (unsigned char) rd >> ea;
  2817. }
  2818. goto shift8;
  2819. case O (O_SHLL, SW): /* shll.w */
  2820. case O (O_SHLR, SW): /* shlr.w */
  2821. if (fetch2 (sd, &code->dst, &rd))
  2822. goto end;
  2823. if (memcmp (&code->src, &code->dst, sizeof (code->src)) == 0)
  2824. ea = 1; /* unary op */
  2825. else
  2826. fetch (sd, &code->src, &ea);
  2827. if (code->opcode == O (O_SHLL, SW))
  2828. {
  2829. v = (ea > 16);
  2830. c = rd & (0x8000 >> (ea - 1));
  2831. rd <<= ea;
  2832. }
  2833. else
  2834. {
  2835. v = 0;
  2836. c = rd & (1 << (ea - 1));
  2837. rd = (unsigned short) rd >> ea;
  2838. }
  2839. goto shift16;
  2840. case O (O_SHLL, SL): /* shll.l */
  2841. case O (O_SHLR, SL): /* shlr.l */
  2842. if (fetch2 (sd, &code->dst, &rd))
  2843. goto end;
  2844. if (memcmp (&code->src, &code->dst, sizeof (code->src)) == 0)
  2845. ea = 1; /* unary op */
  2846. else
  2847. fetch (sd, &code->src, &ea);
  2848. if (code->opcode == O (O_SHLL, SL))
  2849. {
  2850. v = (ea > 32);
  2851. c = rd & (0x80000000 >> (ea - 1));
  2852. rd <<= ea;
  2853. }
  2854. else
  2855. {
  2856. v = 0;
  2857. c = rd & (1 << (ea - 1));
  2858. rd = (unsigned int) rd >> ea;
  2859. }
  2860. goto shift32;
  2861. case O (O_SHAL, SB):
  2862. case O (O_SHAR, SB):
  2863. if (fetch2 (sd, &code->dst, &rd))
  2864. goto end;
  2865. if (code->src.type == X (OP_IMM, SB))
  2866. fetch (sd, &code->src, &ea);
  2867. else
  2868. ea = 1;
  2869. if (code->opcode == O (O_SHAL, SB))
  2870. {
  2871. c = rd & (0x80 >> (ea - 1));
  2872. res = rd >> (7 - ea);
  2873. v = ((res & 1) && !(res & 2))
  2874. || (!(res & 1) && (res & 2));
  2875. rd <<= ea;
  2876. }
  2877. else
  2878. {
  2879. c = rd & (1 << (ea - 1));
  2880. v = 0;
  2881. rd = ((signed char) rd) >> ea;
  2882. }
  2883. goto shift8;
  2884. case O (O_SHAL, SW):
  2885. case O (O_SHAR, SW):
  2886. if (fetch2 (sd, &code->dst, &rd))
  2887. goto end;
  2888. if (code->src.type == X (OP_IMM, SW))
  2889. fetch (sd, &code->src, &ea);
  2890. else
  2891. ea = 1;
  2892. if (code->opcode == O (O_SHAL, SW))
  2893. {
  2894. c = rd & (0x8000 >> (ea - 1));
  2895. res = rd >> (15 - ea);
  2896. v = ((res & 1) && !(res & 2))
  2897. || (!(res & 1) && (res & 2));
  2898. rd <<= ea;
  2899. }
  2900. else
  2901. {
  2902. c = rd & (1 << (ea - 1));
  2903. v = 0;
  2904. rd = ((signed short) rd) >> ea;
  2905. }
  2906. goto shift16;
  2907. case O (O_SHAL, SL):
  2908. case O (O_SHAR, SL):
  2909. if (fetch2 (sd, &code->dst, &rd))
  2910. goto end;
  2911. if (code->src.type == X (OP_IMM, SL))
  2912. fetch (sd, &code->src, &ea);
  2913. else
  2914. ea = 1;
  2915. if (code->opcode == O (O_SHAL, SL))
  2916. {
  2917. c = rd & (0x80000000 >> (ea - 1));
  2918. res = rd >> (31 - ea);
  2919. v = ((res & 1) && !(res & 2))
  2920. || (!(res & 1) && (res & 2));
  2921. rd <<= ea;
  2922. }
  2923. else
  2924. {
  2925. c = rd & (1 << (ea - 1));
  2926. v = 0;
  2927. rd = ((signed int) rd) >> ea;
  2928. }
  2929. goto shift32;
  2930. case O (O_ROTL, SB):
  2931. case O (O_ROTR, SB):
  2932. if (fetch2 (sd, &code->dst, &rd))
  2933. goto end;
  2934. if (code->src.type == X (OP_IMM, SB))
  2935. fetch (sd, &code->src, &ea);
  2936. else
  2937. ea = 1;
  2938. while (ea--)
  2939. if (code->opcode == O (O_ROTL, SB))
  2940. {
  2941. c = rd & 0x80;
  2942. rd <<= 1;
  2943. if (c)
  2944. rd |= 1;
  2945. }
  2946. else
  2947. {
  2948. c = rd & 1;
  2949. rd = ((unsigned char) rd) >> 1;
  2950. if (c)
  2951. rd |= 0x80;
  2952. }
  2953. v = 0;
  2954. goto shift8;
  2955. case O (O_ROTL, SW):
  2956. case O (O_ROTR, SW):
  2957. if (fetch2 (sd, &code->dst, &rd))
  2958. goto end;
  2959. if (code->src.type == X (OP_IMM, SW))
  2960. fetch (sd, &code->src, &ea);
  2961. else
  2962. ea = 1;
  2963. while (ea--)
  2964. if (code->opcode == O (O_ROTL, SW))
  2965. {
  2966. c = rd & 0x8000;
  2967. rd <<= 1;
  2968. if (c)
  2969. rd |= 1;
  2970. }
  2971. else
  2972. {
  2973. c = rd & 1;
  2974. rd = ((unsigned short) rd) >> 1;
  2975. if (c)
  2976. rd |= 0x8000;
  2977. }
  2978. v = 0;
  2979. goto shift16;
  2980. case O (O_ROTL, SL):
  2981. case O (O_ROTR, SL):
  2982. if (fetch2 (sd, &code->dst, &rd))
  2983. goto end;
  2984. if (code->src.type == X (OP_IMM, SL))
  2985. fetch (sd, &code->src, &ea);
  2986. else
  2987. ea = 1;
  2988. while (ea--)
  2989. if (code->opcode == O (O_ROTL, SL))
  2990. {
  2991. c = rd & 0x80000000;
  2992. rd <<= 1;
  2993. if (c)
  2994. rd |= 1;
  2995. }
  2996. else
  2997. {
  2998. c = rd & 1;
  2999. rd = ((unsigned int) rd) >> 1;
  3000. if (c)
  3001. rd |= 0x80000000;
  3002. }
  3003. v = 0;
  3004. goto shift32;
  3005. case O (O_ROTXL, SB):
  3006. case O (O_ROTXR, SB):
  3007. if (fetch2 (sd, &code->dst, &rd))
  3008. goto end;
  3009. if (code->src.type == X (OP_IMM, SB))
  3010. fetch (sd, &code->src, &ea);
  3011. else
  3012. ea = 1;
  3013. while (ea--)
  3014. if (code->opcode == O (O_ROTXL, SB))
  3015. {
  3016. res = rd & 0x80;
  3017. rd <<= 1;
  3018. if (C)
  3019. rd |= 1;
  3020. c = res;
  3021. }
  3022. else
  3023. {
  3024. res = rd & 1;
  3025. rd = ((unsigned char) rd) >> 1;
  3026. if (C)
  3027. rd |= 0x80;
  3028. c = res;
  3029. }
  3030. v = 0;
  3031. goto shift8;
  3032. case O (O_ROTXL, SW):
  3033. case O (O_ROTXR, SW):
  3034. if (fetch2 (sd, &code->dst, &rd))
  3035. goto end;
  3036. if (code->src.type == X (OP_IMM, SW))
  3037. fetch (sd, &code->src, &ea);
  3038. else
  3039. ea = 1;
  3040. while (ea--)
  3041. if (code->opcode == O (O_ROTXL, SW))
  3042. {
  3043. res = rd & 0x8000;
  3044. rd <<= 1;
  3045. if (C)
  3046. rd |= 1;
  3047. c = res;
  3048. }
  3049. else
  3050. {
  3051. res = rd & 1;
  3052. rd = ((unsigned short) rd) >> 1;
  3053. if (C)
  3054. rd |= 0x8000;
  3055. c = res;
  3056. }
  3057. v = 0;
  3058. goto shift16;
  3059. case O (O_ROTXL, SL):
  3060. case O (O_ROTXR, SL):
  3061. if (fetch2 (sd, &code->dst, &rd))
  3062. goto end;
  3063. if (code->src.type == X (OP_IMM, SL))
  3064. fetch (sd, &code->src, &ea);
  3065. else
  3066. ea = 1;
  3067. while (ea--)
  3068. if (code->opcode == O (O_ROTXL, SL))
  3069. {
  3070. res = rd & 0x80000000;
  3071. rd <<= 1;
  3072. if (C)
  3073. rd |= 1;
  3074. c = res;
  3075. }
  3076. else
  3077. {
  3078. res = rd & 1;
  3079. rd = ((unsigned int) rd) >> 1;
  3080. if (C)
  3081. rd |= 0x80000000;
  3082. c = res;
  3083. }
  3084. v = 0;
  3085. goto shift32;
  3086. case O (O_JMP, SN):
  3087. case O (O_JMP, SL):
  3088. case O (O_JMP, SB): /* jmp */
  3089. case O (O_JMP, SW):
  3090. fetch (sd, &code->src, &pc);
  3091. goto end;
  3092. case O (O_JSR, SN):
  3093. case O (O_JSR, SL):
  3094. case O (O_JSR, SB): /* jsr, jump to subroutine */
  3095. case O (O_JSR, SW):
  3096. if (fetch (sd, &code->src, &pc))
  3097. goto end;
  3098. call:
  3099. tmp = h8_get_reg (sd, SP_REGNUM);
  3100. if (h8300hmode && !h8300_normal_mode)
  3101. {
  3102. tmp -= 4;
  3103. SET_MEMORY_L (tmp, code->next_pc);
  3104. }
  3105. else
  3106. {
  3107. tmp -= 2;
  3108. SET_MEMORY_W (tmp, code->next_pc);
  3109. }
  3110. h8_set_reg (sd, SP_REGNUM, tmp);
  3111. goto end;
  3112. case O (O_BSR, SW):
  3113. case O (O_BSR, SL):
  3114. case O (O_BSR, SB): /* bsr, branch to subroutine */
  3115. if (fetch (sd, &code->src, &res))
  3116. goto end;
  3117. pc = code->next_pc + res;
  3118. goto call;
  3119. case O (O_RTE, SN): /* rte, return from exception */
  3120. rte:
  3121. /* Pops exr and ccr before pc -- otherwise identical to rts. */
  3122. tmp = h8_get_reg (sd, SP_REGNUM);
  3123. if (h8300smode) /* pop exr */
  3124. {
  3125. h8_set_exr (sd, GET_MEMORY_L (tmp));
  3126. tmp += 4;
  3127. }
  3128. if (h8300hmode && !h8300_normal_mode)
  3129. {
  3130. h8_set_ccr (sd, GET_MEMORY_L (tmp));
  3131. tmp += 4;
  3132. pc = GET_MEMORY_L (tmp);
  3133. tmp += 4;
  3134. }
  3135. else
  3136. {
  3137. h8_set_ccr (sd, GET_MEMORY_W (tmp));
  3138. tmp += 2;
  3139. pc = GET_MEMORY_W (tmp);
  3140. tmp += 2;
  3141. }
  3142. GETSR (sd);
  3143. h8_set_reg (sd, SP_REGNUM, tmp);
  3144. goto end;
  3145. case O (O_RTS, SN): /* rts, return from subroutine */
  3146. rts:
  3147. tmp = h8_get_reg (sd, SP_REGNUM);
  3148. if (h8300hmode && !h8300_normal_mode)
  3149. {
  3150. pc = GET_MEMORY_L (tmp);
  3151. tmp += 4;
  3152. }
  3153. else
  3154. {
  3155. pc = GET_MEMORY_W (tmp);
  3156. tmp += 2;
  3157. }
  3158. h8_set_reg (sd, SP_REGNUM, tmp);
  3159. goto end;
  3160. case O (O_ILL, SB): /* illegal */
  3161. sim_engine_set_run_state (sd, sim_stopped, SIGILL);
  3162. goto end;
  3163. case O (O_SLEEP, SN): /* sleep */
  3164. /* Check for magic numbers in r1 and r2. */
  3165. if ((h8_get_reg (sd, R1_REGNUM) & 0xffff) == LIBC_EXIT_MAGIC1 &&
  3166. (h8_get_reg (sd, R2_REGNUM) & 0xffff) == LIBC_EXIT_MAGIC2 &&
  3167. SIM_WIFEXITED (h8_get_reg (sd, 0)))
  3168. {
  3169. /* This trap comes from _exit, not from gdb. */
  3170. sim_engine_set_run_state (sd, sim_exited,
  3171. SIM_WEXITSTATUS (h8_get_reg (sd, 0)));
  3172. }
  3173. #if 0
  3174. /* Unfortunately this won't really work, because
  3175. when we take a breakpoint trap, R0 has a "random",
  3176. user-defined value. Don't see any immediate solution. */
  3177. else if (SIM_WIFSTOPPED (h8_get_reg (sd, 0)))
  3178. {
  3179. /* Pass the stop signal up to gdb. */
  3180. sim_engine_set_run_state (sd, sim_stopped,
  3181. SIM_WSTOPSIG (h8_get_reg (sd, 0)));
  3182. }
  3183. #endif
  3184. else
  3185. {
  3186. /* Treat it as a sigtrap. */
  3187. sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
  3188. }
  3189. goto end;
  3190. case O (O_TRAPA, SB): /* trapa */
  3191. if (fetch (sd, &code->src, &res))
  3192. goto end; /* res is vector number. */
  3193. tmp = h8_get_reg (sd, SP_REGNUM);
  3194. if(h8300_normal_mode)
  3195. {
  3196. tmp -= 2;
  3197. SET_MEMORY_W (tmp, code->next_pc);
  3198. tmp -= 2;
  3199. SET_MEMORY_W (tmp, h8_get_ccr (sd));
  3200. }
  3201. else
  3202. {
  3203. tmp -= 4;
  3204. SET_MEMORY_L (tmp, code->next_pc);
  3205. tmp -= 4;
  3206. SET_MEMORY_L (tmp, h8_get_ccr (sd));
  3207. }
  3208. intMaskBit = 1;
  3209. BUILDSR (sd);
  3210. if (h8300smode)
  3211. {
  3212. tmp -= 4;
  3213. SET_MEMORY_L (tmp, h8_get_exr (sd));
  3214. }
  3215. h8_set_reg (sd, SP_REGNUM, tmp);
  3216. if(h8300_normal_mode)
  3217. pc = GET_MEMORY_L (0x10 + res * 2); /* Vector addresses are 0x10,0x12,0x14 and 0x16 */
  3218. else
  3219. pc = GET_MEMORY_L (0x20 + res * 4);
  3220. goto end;
  3221. case O (O_BPT, SN):
  3222. sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
  3223. goto end;
  3224. case O (O_BSETEQ, SB):
  3225. if (Z)
  3226. goto bset;
  3227. goto next;
  3228. case O (O_BSETNE, SB):
  3229. if (!Z)
  3230. goto bset;
  3231. goto next;
  3232. case O (O_BCLREQ, SB):
  3233. if (Z)
  3234. goto bclr;
  3235. goto next;
  3236. case O (O_BCLRNE, SB):
  3237. if (!Z)
  3238. goto bclr;
  3239. goto next;
  3240. OBITOP (O_BNOT, 1, 1, ea ^= m); /* bnot */
  3241. OBITOP (O_BTST, 1, 0, nz = ea & m); /* btst */
  3242. bset:
  3243. OBITOP (O_BSET, 1, 1, ea |= m); /* bset */
  3244. bclr:
  3245. OBITOP (O_BCLR, 1, 1, ea &= ~m); /* bclr */
  3246. OBITOP (O_BLD, 1, 0, c = ea & m); /* bld */
  3247. OBITOP (O_BILD, 1, 0, c = !(ea & m)); /* bild */
  3248. OBITOP (O_BST, 1, 1, ea &= ~m;
  3249. if (C) ea |= m); /* bst */
  3250. OBITOP (O_BIST, 1, 1, ea &= ~m;
  3251. if (!C) ea |= m); /* bist */
  3252. OBITOP (O_BSTZ, 1, 1, ea &= ~m;
  3253. if (Z) ea |= m); /* bstz */
  3254. OBITOP (O_BISTZ, 1, 1, ea &= ~m;
  3255. if (!Z) ea |= m); /* bistz */
  3256. OBITOP (O_BAND, 1, 0, c = (ea & m) && C); /* band */
  3257. OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C); /* biand */
  3258. OBITOP (O_BOR, 1, 0, c = (ea & m) || C); /* bor */
  3259. OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C); /* bior */
  3260. OBITOP (O_BXOR, 1, 0, c = ((ea & m) != 0)!= C); /* bxor */
  3261. OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C); /* bixor */
  3262. case O (O_BFLD, SB): /* bfld */
  3263. /* bitfield load */
  3264. ea = 0;
  3265. if (fetch (sd, &code->src, &bit))
  3266. goto end;
  3267. if (bit != 0)
  3268. {
  3269. if (fetch (sd, &code->dst, &ea))
  3270. goto end;
  3271. ea &= bit;
  3272. while (!(bit & 1))
  3273. {
  3274. ea >>= 1;
  3275. bit >>= 1;
  3276. }
  3277. }
  3278. if (store (sd, &code->op3, ea))
  3279. goto end;
  3280. goto next;
  3281. case O(O_BFST, SB): /* bfst */
  3282. /* bitfield store */
  3283. /* NOTE: the imm8 value is in dst, and the ea value
  3284. (which is actually the destination) is in op3.
  3285. It has to be that way, to avoid breaking the assembler. */
  3286. if (fetch (sd, &code->dst, &bit)) /* imm8 */
  3287. goto end;
  3288. if (bit == 0) /* noop -- nothing to do. */
  3289. goto next;
  3290. if (fetch (sd, &code->src, &rd)) /* reg8 src */
  3291. goto end;
  3292. if (fetch2 (sd, &code->op3, &ea)) /* ea dst */
  3293. goto end;
  3294. /* Left-shift the register data into position. */
  3295. for (tmp = bit; !(tmp & 1); tmp >>= 1)
  3296. rd <<= 1;
  3297. /* Combine it with the neighboring bits. */
  3298. ea = (ea & ~bit) | (rd & bit);
  3299. /* Put it back. */
  3300. if (store2 (sd, &code->op3, ea))
  3301. goto end;
  3302. goto next;
  3303. case O (O_CLRMAC, SN): /* clrmac */
  3304. h8_set_mach (sd, 0);
  3305. h8_set_macl (sd, 0);
  3306. h8_set_macZ (sd, 1);
  3307. h8_set_macV (sd, 0);
  3308. h8_set_macN (sd, 0);
  3309. goto next;
  3310. case O (O_STMAC, SL): /* stmac, 260 */
  3311. switch (code->src.type) {
  3312. case X (OP_MACH, SL):
  3313. res = h8_get_mach (sd);
  3314. if (res & 0x200) /* sign extend */
  3315. res |= 0xfffffc00;
  3316. break;
  3317. case X (OP_MACL, SL):
  3318. res = h8_get_macl (sd);
  3319. break;
  3320. default: goto illegal;
  3321. }
  3322. nz = !h8_get_macZ (sd);
  3323. n = h8_get_macN (sd);
  3324. v = h8_get_macV (sd);
  3325. if (store (sd, &code->dst, res))
  3326. goto end;
  3327. goto next;
  3328. case O (O_LDMAC, SL): /* ldmac, 179 */
  3329. if (fetch (sd, &code->src, &rd))
  3330. goto end;
  3331. switch (code->dst.type) {
  3332. case X (OP_MACH, SL):
  3333. rd &= 0x3ff; /* Truncate to 10 bits */
  3334. h8_set_mach (sd, rd);
  3335. break;
  3336. case X (OP_MACL, SL):
  3337. h8_set_macl (sd, rd);
  3338. break;
  3339. default: goto illegal;
  3340. }
  3341. h8_set_macV (sd, 0);
  3342. goto next;
  3343. case O (O_MAC, SW):
  3344. if (fetch (sd, &code->src, &rd) ||
  3345. fetch (sd, &code->dst, &res))
  3346. goto end;
  3347. /* Ye gods, this is non-portable!
  3348. However, the existing mul/div code is similar. */
  3349. res = SEXTSHORT (res) * SEXTSHORT (rd);
  3350. if (h8_get_macS (sd)) /* Saturating mode */
  3351. {
  3352. long long mac = h8_get_macl (sd);
  3353. if (mac & 0x80000000) /* sign extend */
  3354. mac |= 0xffffffff00000000LL;
  3355. mac += res;
  3356. if (mac > 0x7fffffff || mac < 0xffffffff80000000LL)
  3357. h8_set_macV (sd, 1);
  3358. h8_set_macZ (sd, (mac == 0));
  3359. h8_set_macN (sd, (mac < 0));
  3360. h8_set_macl (sd, (int) mac);
  3361. }
  3362. else /* "Less Saturating" mode */
  3363. {
  3364. long long mac = h8_get_mach (sd);
  3365. mac <<= 32;
  3366. mac += h8_get_macl (sd);
  3367. if (mac & 0x20000000000LL) /* sign extend */
  3368. mac |= 0xfffffc0000000000LL;
  3369. mac += res;
  3370. if (mac > 0x1ffffffffffLL ||
  3371. mac < (long long) 0xfffffe0000000000LL)
  3372. h8_set_macV (sd, 1);
  3373. h8_set_macZ (sd, (mac == 0));
  3374. h8_set_macN (sd, (mac < 0));
  3375. h8_set_macl (sd, (int) mac);
  3376. mac >>= 32;
  3377. h8_set_mach (sd, (int) (mac & 0x3ff));
  3378. }
  3379. goto next;
  3380. case O (O_MULS, SW): /* muls.w */
  3381. if (fetch (sd, &code->src, &ea) ||
  3382. fetch (sd, &code->dst, &rd))
  3383. goto end;
  3384. ea = SEXTSHORT (ea);
  3385. res = SEXTSHORT (ea * SEXTSHORT (rd));
  3386. n = res & 0x8000;
  3387. nz = res & 0xffff;
  3388. if (store (sd, &code->dst, res))
  3389. goto end;
  3390. goto next;
  3391. case O (O_MULS, SL): /* muls.l */
  3392. if (fetch (sd, &code->src, &ea) ||
  3393. fetch (sd, &code->dst, &rd))
  3394. goto end;
  3395. res = ea * rd;
  3396. n = res & 0x80000000;
  3397. nz = res & 0xffffffff;
  3398. if (store (sd, &code->dst, res))
  3399. goto end;
  3400. goto next;
  3401. case O (O_MULSU, SL): /* muls/u.l */
  3402. if (fetch (sd, &code->src, &ea) ||
  3403. fetch (sd, &code->dst, &rd))
  3404. goto end;
  3405. /* Compute upper 32 bits of the 64-bit result. */
  3406. res = (((long long) ea) * ((long long) rd)) >> 32;
  3407. n = res & 0x80000000;
  3408. nz = res & 0xffffffff;
  3409. if (store (sd, &code->dst, res))
  3410. goto end;
  3411. goto next;
  3412. case O (O_MULU, SW): /* mulu.w */
  3413. if (fetch (sd, &code->src, &ea) ||
  3414. fetch (sd, &code->dst, &rd))
  3415. goto end;
  3416. res = UEXTSHORT ((UEXTSHORT (ea) * UEXTSHORT (rd)));
  3417. /* Don't set Z or N. */
  3418. if (store (sd, &code->dst, res))
  3419. goto end;
  3420. goto next;
  3421. case O (O_MULU, SL): /* mulu.l */
  3422. if (fetch (sd, &code->src, &ea) ||
  3423. fetch (sd, &code->dst, &rd))
  3424. goto end;
  3425. res = ea * rd;
  3426. /* Don't set Z or N. */
  3427. if (store (sd, &code->dst, res))
  3428. goto end;
  3429. goto next;
  3430. case O (O_MULUU, SL): /* mulu/u.l */
  3431. if (fetch (sd, &code->src, &ea) ||
  3432. fetch (sd, &code->dst, &rd))
  3433. goto end;
  3434. /* Compute upper 32 bits of the 64-bit result. */
  3435. res = (((unsigned long long) (unsigned) ea) *
  3436. ((unsigned long long) (unsigned) rd)) >> 32;
  3437. /* Don't set Z or N. */
  3438. if (store (sd, &code->dst, res))
  3439. goto end;
  3440. goto next;
  3441. case O (O_MULXS, SB): /* mulxs.b */
  3442. if (fetch (sd, &code->src, &ea) ||
  3443. fetch (sd, &code->dst, &rd))
  3444. goto end;
  3445. ea = SEXTCHAR (ea);
  3446. res = ea * SEXTCHAR (rd);
  3447. n = res & 0x8000;
  3448. nz = res & 0xffff;
  3449. if (store (sd, &code->dst, res))
  3450. goto end;
  3451. goto next;
  3452. case O (O_MULXS, SW): /* mulxs.w */
  3453. if (fetch (sd, &code->src, &ea) ||
  3454. fetch (sd, &code->dst, &rd))
  3455. goto end;
  3456. ea = SEXTSHORT (ea);
  3457. res = ea * SEXTSHORT (rd & 0xffff);
  3458. n = res & 0x80000000;
  3459. nz = res & 0xffffffff;
  3460. if (store (sd, &code->dst, res))
  3461. goto end;
  3462. goto next;
  3463. case O (O_MULXU, SB): /* mulxu.b */
  3464. if (fetch (sd, &code->src, &ea) ||
  3465. fetch (sd, &code->dst, &rd))
  3466. goto end;
  3467. res = UEXTCHAR (ea) * UEXTCHAR (rd);
  3468. if (store (sd, &code->dst, res))
  3469. goto end;
  3470. goto next;
  3471. case O (O_MULXU, SW): /* mulxu.w */
  3472. if (fetch (sd, &code->src, &ea) ||
  3473. fetch (sd, &code->dst, &rd))
  3474. goto end;
  3475. res = UEXTSHORT (ea) * UEXTSHORT (rd);
  3476. if (store (sd, &code->dst, res))
  3477. goto end;
  3478. goto next;
  3479. case O (O_TAS, SB): /* tas (test and set) */
  3480. if (!h8300sxmode) /* h8sx can use any register. */
  3481. switch (code->src.reg)
  3482. {
  3483. case R0_REGNUM:
  3484. case R1_REGNUM:
  3485. case R4_REGNUM:
  3486. case R5_REGNUM:
  3487. break;
  3488. default:
  3489. goto illegal;
  3490. }
  3491. if (fetch (sd, &code->src, &res))
  3492. goto end;
  3493. if (store (sd, &code->src, res | 0x80))
  3494. goto end;
  3495. goto just_flags_log8;
  3496. case O (O_DIVU, SW): /* divu.w */
  3497. if (fetch (sd, &code->src, &ea) ||
  3498. fetch (sd, &code->dst, &rd))
  3499. goto end;
  3500. n = ea & 0x8000;
  3501. nz = ea & 0xffff;
  3502. if (ea)
  3503. res = (unsigned) (UEXTSHORT (rd) / UEXTSHORT (ea));
  3504. else
  3505. res = 0;
  3506. if (store (sd, &code->dst, res))
  3507. goto end;
  3508. goto next;
  3509. case O (O_DIVU, SL): /* divu.l */
  3510. if (fetch (sd, &code->src, &ea) ||
  3511. fetch (sd, &code->dst, &rd))
  3512. goto end;
  3513. n = ea & 0x80000000;
  3514. nz = ea & 0xffffffff;
  3515. if (ea)
  3516. res = (unsigned) rd / ea;
  3517. else
  3518. res = 0;
  3519. if (store (sd, &code->dst, res))
  3520. goto end;
  3521. goto next;
  3522. case O (O_DIVS, SW): /* divs.w */
  3523. if (fetch (sd, &code->src, &ea) ||
  3524. fetch (sd, &code->dst, &rd))
  3525. goto end;
  3526. if (ea)
  3527. {
  3528. res = SEXTSHORT (rd) / SEXTSHORT (ea);
  3529. nz = 1;
  3530. }
  3531. else
  3532. {
  3533. res = 0;
  3534. nz = 0;
  3535. }
  3536. n = res & 0x8000;
  3537. if (store (sd, &code->dst, res))
  3538. goto end;
  3539. goto next;
  3540. case O (O_DIVS, SL): /* divs.l */
  3541. if (fetch (sd, &code->src, &ea) ||
  3542. fetch (sd, &code->dst, &rd))
  3543. goto end;
  3544. if (ea)
  3545. {
  3546. res = rd / ea;
  3547. nz = 1;
  3548. }
  3549. else
  3550. {
  3551. res = 0;
  3552. nz = 0;
  3553. }
  3554. n = res & 0x80000000;
  3555. if (store (sd, &code->dst, res))
  3556. goto end;
  3557. goto next;
  3558. case O (O_DIVXU, SB): /* divxu.b */
  3559. if (fetch (sd, &code->src, &ea) ||
  3560. fetch (sd, &code->dst, &rd))
  3561. goto end;
  3562. rd = UEXTSHORT (rd);
  3563. ea = UEXTCHAR (ea);
  3564. n = ea & 0x80;
  3565. nz = ea & 0xff;
  3566. if (ea)
  3567. {
  3568. tmp = (unsigned) rd % ea;
  3569. res = (unsigned) rd / ea;
  3570. }
  3571. else
  3572. {
  3573. tmp = 0;
  3574. res = 0;
  3575. }
  3576. if (store (sd, &code->dst, (res & 0xff) | (tmp << 8)))
  3577. goto end;
  3578. goto next;
  3579. case O (O_DIVXU, SW): /* divxu.w */
  3580. if (fetch (sd, &code->src, &ea) ||
  3581. fetch (sd, &code->dst, &rd))
  3582. goto end;
  3583. ea = UEXTSHORT (ea);
  3584. n = ea & 0x8000;
  3585. nz = ea & 0xffff;
  3586. if (ea)
  3587. {
  3588. tmp = (unsigned) rd % ea;
  3589. res = (unsigned) rd / ea;
  3590. }
  3591. else
  3592. {
  3593. tmp = 0;
  3594. res = 0;
  3595. }
  3596. if (store (sd, &code->dst, (res & 0xffff) | (tmp << 16)))
  3597. goto end;
  3598. goto next;
  3599. case O (O_DIVXS, SB): /* divxs.b */
  3600. if (fetch (sd, &code->src, &ea) ||
  3601. fetch (sd, &code->dst, &rd))
  3602. goto end;
  3603. rd = SEXTSHORT (rd);
  3604. ea = SEXTCHAR (ea);
  3605. if (ea)
  3606. {
  3607. tmp = (int) rd % (int) ea;
  3608. res = (int) rd / (int) ea;
  3609. nz = 1;
  3610. }
  3611. else
  3612. {
  3613. tmp = 0;
  3614. res = 0;
  3615. nz = 0;
  3616. }
  3617. n = res & 0x8000;
  3618. if (store (sd, &code->dst, (res & 0xff) | (tmp << 8)))
  3619. goto end;
  3620. goto next;
  3621. case O (O_DIVXS, SW): /* divxs.w */
  3622. if (fetch (sd, &code->src, &ea) ||
  3623. fetch (sd, &code->dst, &rd))
  3624. goto end;
  3625. ea = SEXTSHORT (ea);
  3626. if (ea)
  3627. {
  3628. tmp = (int) rd % (int) ea;
  3629. res = (int) rd / (int) ea;
  3630. nz = 1;
  3631. }
  3632. else
  3633. {
  3634. tmp = 0;
  3635. res = 0;
  3636. nz = 0;
  3637. }
  3638. n = res & 0x80000000;
  3639. if (store (sd, &code->dst, (res & 0xffff) | (tmp << 16)))
  3640. goto end;
  3641. goto next;
  3642. case O (O_EXTS, SW): /* exts.w, signed extend */
  3643. if (fetch2 (sd, &code->dst, &rd))
  3644. goto end;
  3645. ea = rd & 0x80 ? -256 : 0;
  3646. res = (rd & 0xff) + ea;
  3647. goto log16;
  3648. case O (O_EXTS, SL): /* exts.l, signed extend */
  3649. if (fetch2 (sd, &code->dst, &rd))
  3650. goto end;
  3651. if (code->src.type == X (OP_IMM, SL))
  3652. {
  3653. if (fetch (sd, &code->src, &ea))
  3654. goto end;
  3655. if (ea == 2) /* exts.l #2, nn */
  3656. {
  3657. /* Sign-extend from 8-bit to 32-bit. */
  3658. ea = rd & 0x80 ? -256 : 0;
  3659. res = (rd & 0xff) + ea;
  3660. goto log32;
  3661. }
  3662. }
  3663. /* Sign-extend from 16-bit to 32-bit. */
  3664. ea = rd & 0x8000 ? -65536 : 0;
  3665. res = (rd & 0xffff) + ea;
  3666. goto log32;
  3667. case O (O_EXTU, SW): /* extu.w, unsigned extend */
  3668. if (fetch2 (sd, &code->dst, &rd))
  3669. goto end;
  3670. ea = 0;
  3671. res = (rd & 0xff) + ea;
  3672. goto log16;
  3673. case O (O_EXTU, SL): /* extu.l, unsigned extend */
  3674. if (fetch2 (sd, &code->dst, &rd))
  3675. goto end;
  3676. if (code->src.type == X (OP_IMM, SL))
  3677. {
  3678. if (fetch (sd, &code->src, &ea))
  3679. goto end;
  3680. if (ea == 2) /* extu.l #2, nn */
  3681. {
  3682. /* Zero-extend from 8-bit to 32-bit. */
  3683. ea = 0;
  3684. res = (rd & 0xff) + ea;
  3685. goto log32;
  3686. }
  3687. }
  3688. /* Zero-extend from 16-bit to 32-bit. */
  3689. ea = 0;
  3690. res = (rd & 0xffff) + ea;
  3691. goto log32;
  3692. case O (O_NOP, SN): /* nop */
  3693. goto next;
  3694. case O (O_STM, SL): /* stm, store to memory */
  3695. {
  3696. int nregs, firstreg, i;
  3697. nregs = GET_MEMORY_B (pc + 1);
  3698. nregs >>= 4;
  3699. nregs &= 0xf;
  3700. firstreg = code->src.reg;
  3701. firstreg &= 0xf;
  3702. for (i = firstreg; i <= firstreg + nregs; i++)
  3703. {
  3704. h8_set_reg (sd, SP_REGNUM, h8_get_reg (sd, SP_REGNUM) - 4);
  3705. SET_MEMORY_L (h8_get_reg (sd, SP_REGNUM), h8_get_reg (sd, i));
  3706. }
  3707. }
  3708. goto next;
  3709. case O (O_LDM, SL): /* ldm, load from memory */
  3710. case O (O_RTEL, SN): /* rte/l, ldm plus rte */
  3711. case O (O_RTSL, SN): /* rts/l, ldm plus rts */
  3712. {
  3713. int nregs, firstreg, i;
  3714. nregs = ((GET_MEMORY_B (pc + 1) >> 4) & 0xf);
  3715. firstreg = code->dst.reg & 0xf;
  3716. for (i = firstreg; i >= firstreg - nregs; i--)
  3717. {
  3718. h8_set_reg (sd, i, GET_MEMORY_L (h8_get_reg (sd, SP_REGNUM)));
  3719. h8_set_reg (sd, SP_REGNUM, h8_get_reg (sd, SP_REGNUM) + 4);
  3720. }
  3721. }
  3722. switch (code->opcode) {
  3723. case O (O_RTEL, SN):
  3724. goto rte;
  3725. case O (O_RTSL, SN):
  3726. goto rts;
  3727. case O (O_LDM, SL):
  3728. goto next;
  3729. default:
  3730. goto illegal;
  3731. }
  3732. case O (O_DAA, SB):
  3733. /* Decimal Adjust Addition. This is for BCD arithmetic. */
  3734. res = GET_B_REG (code->src.reg); /* FIXME fetch? */
  3735. if (!c && (0 <= (res >> 4) && (res >> 4) <= 9) &&
  3736. !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
  3737. res = res; /* Value added == 0. */
  3738. else if (!c && (0 <= (res >> 4) && (res >> 4) <= 8) &&
  3739. !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
  3740. res = res + 0x6; /* Value added == 6. */
  3741. else if (!c && (0 <= (res >> 4) && (res >> 4) <= 9) &&
  3742. h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
  3743. res = res + 0x6; /* Value added == 6. */
  3744. else if (!c && (10 <= (res >> 4) && (res >> 4) <= 15) &&
  3745. !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
  3746. res = res + 0x60; /* Value added == 60. */
  3747. else if (!c && (9 <= (res >> 4) && (res >> 4) <= 15) &&
  3748. !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
  3749. res = res + 0x66; /* Value added == 66. */
  3750. else if (!c && (10 <= (res >> 4) && (res >> 4) <= 15) &&
  3751. h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
  3752. res = res + 0x66; /* Value added == 66. */
  3753. else if ( c && (1 <= (res >> 4) && (res >> 4) <= 2) &&
  3754. !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
  3755. res = res + 0x60; /* Value added == 60. */
  3756. else if ( c && (1 <= (res >> 4) && (res >> 4) <= 2) &&
  3757. !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
  3758. res = res + 0x66; /* Value added == 66. */
  3759. else if (c && (1 <= (res >> 4) && (res >> 4) <= 3) &&
  3760. h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
  3761. res = res + 0x66; /* Value added == 66. */
  3762. goto alu8;
  3763. case O (O_DAS, SB):
  3764. /* Decimal Adjust Subtraction. This is for BCD arithmetic. */
  3765. res = GET_B_REG (code->src.reg); /* FIXME fetch, fetch2... */
  3766. if (!c && (0 <= (res >> 4) && (res >> 4) <= 9) &&
  3767. !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
  3768. res = res; /* Value added == 0. */
  3769. else if (!c && (0 <= (res >> 4) && (res >> 4) <= 8) &&
  3770. h && (6 <= (res & 0xf) && (res & 0xf) <= 15))
  3771. res = res + 0xfa; /* Value added == 0xfa. */
  3772. else if ( c && (7 <= (res >> 4) && (res >> 4) <= 15) &&
  3773. !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
  3774. res = res + 0xa0; /* Value added == 0xa0. */
  3775. else if (c && (6 <= (res >> 4) && (res >> 4) <= 15) &&
  3776. h && (6 <= (res & 0xf) && (res & 0xf) <= 15))
  3777. res = res + 0x9a; /* Value added == 0x9a. */
  3778. goto alu8;
  3779. default:
  3780. illegal:
  3781. sim_engine_set_run_state (sd, sim_stopped, SIGILL);
  3782. goto end;
  3783. }
  3784. (*sim_callback->printf_filtered) (sim_callback,
  3785. "sim_resume: internal error.\n");
  3786. sim_engine_set_run_state (sd, sim_stopped, SIGILL);
  3787. goto end;
  3788. setc:
  3789. if (code->dst.type == X (OP_CCR, SB) ||
  3790. code->dst.type == X (OP_CCR, SW))
  3791. {
  3792. h8_set_ccr (sd, res);
  3793. GETSR (sd);
  3794. }
  3795. else if (h8300smode &&
  3796. (code->dst.type == X (OP_EXR, SB) ||
  3797. code->dst.type == X (OP_EXR, SW)))
  3798. {
  3799. h8_set_exr (sd, res);
  3800. if (h8300smode) /* Get exr. */
  3801. {
  3802. trace = (h8_get_exr (sd) >> 7) & 1;
  3803. intMask = h8_get_exr (sd) & 7;
  3804. }
  3805. }
  3806. else
  3807. goto illegal;
  3808. goto next;
  3809. condtrue:
  3810. /* When a branch works */
  3811. if (fetch (sd, &code->src, &res))
  3812. goto end;
  3813. if (res & 1) /* bad address */
  3814. goto illegal;
  3815. pc = code->next_pc + res;
  3816. goto end;
  3817. /* Set the cond codes from res */
  3818. bitop:
  3819. /* Set the flags after an 8 bit inc/dec operation */
  3820. just_flags_inc8:
  3821. n = res & 0x80;
  3822. nz = res & 0xff;
  3823. v = (rd & 0x7f) == 0x7f;
  3824. goto next;
  3825. /* Set the flags after an 16 bit inc/dec operation */
  3826. just_flags_inc16:
  3827. n = res & 0x8000;
  3828. nz = res & 0xffff;
  3829. v = (rd & 0x7fff) == 0x7fff;
  3830. goto next;
  3831. /* Set the flags after an 32 bit inc/dec operation */
  3832. just_flags_inc32:
  3833. n = res & 0x80000000;
  3834. nz = res & 0xffffffff;
  3835. v = (rd & 0x7fffffff) == 0x7fffffff;
  3836. goto next;
  3837. shift8:
  3838. /* Set flags after an 8 bit shift op, carry,overflow set in insn */
  3839. n = (rd & 0x80);
  3840. nz = rd & 0xff;
  3841. if (store2 (sd, &code->dst, rd))
  3842. goto end;
  3843. goto next;
  3844. shift16:
  3845. /* Set flags after an 16 bit shift op, carry,overflow set in insn */
  3846. n = (rd & 0x8000);
  3847. nz = rd & 0xffff;
  3848. if (store2 (sd, &code->dst, rd))
  3849. goto end;
  3850. goto next;
  3851. shift32:
  3852. /* Set flags after an 32 bit shift op, carry,overflow set in insn */
  3853. n = (rd & 0x80000000);
  3854. nz = rd & 0xffffffff;
  3855. if (store2 (sd, &code->dst, rd))
  3856. goto end;
  3857. goto next;
  3858. log32:
  3859. if (store2 (sd, &code->dst, res))
  3860. goto end;
  3861. just_flags_log32:
  3862. /* flags after a 32bit logical operation */
  3863. n = res & 0x80000000;
  3864. nz = res & 0xffffffff;
  3865. v = 0;
  3866. goto next;
  3867. log16:
  3868. if (store2 (sd, &code->dst, res))
  3869. goto end;
  3870. just_flags_log16:
  3871. /* flags after a 16bit logical operation */
  3872. n = res & 0x8000;
  3873. nz = res & 0xffff;
  3874. v = 0;
  3875. goto next;
  3876. log8:
  3877. if (store2 (sd, &code->dst, res))
  3878. goto end;
  3879. just_flags_log8:
  3880. n = res & 0x80;
  3881. nz = res & 0xff;
  3882. v = 0;
  3883. goto next;
  3884. alu8:
  3885. if (store2 (sd, &code->dst, res))
  3886. goto end;
  3887. just_flags_alu8:
  3888. n = res & 0x80;
  3889. nz = res & 0xff;
  3890. c = (res & 0x100);
  3891. switch (code->opcode / 4)
  3892. {
  3893. case O_ADD:
  3894. case O_ADDX:
  3895. v = ((rd & 0x80) == (ea & 0x80)
  3896. && (rd & 0x80) != (res & 0x80));
  3897. break;
  3898. case O_SUB:
  3899. case O_SUBX:
  3900. case O_CMP:
  3901. v = ((rd & 0x80) != (-ea & 0x80)
  3902. && (rd & 0x80) != (res & 0x80));
  3903. break;
  3904. case O_NEG:
  3905. v = (rd == 0x80);
  3906. break;
  3907. case O_DAA:
  3908. case O_DAS:
  3909. break; /* No effect on v flag. */
  3910. }
  3911. goto next;
  3912. alu16:
  3913. if (store2 (sd, &code->dst, res))
  3914. goto end;
  3915. just_flags_alu16:
  3916. n = res & 0x8000;
  3917. nz = res & 0xffff;
  3918. c = (res & 0x10000);
  3919. switch (code->opcode / 4)
  3920. {
  3921. case O_ADD:
  3922. case O_ADDX:
  3923. v = ((rd & 0x8000) == (ea & 0x8000)
  3924. && (rd & 0x8000) != (res & 0x8000));
  3925. break;
  3926. case O_SUB:
  3927. case O_SUBX:
  3928. case O_CMP:
  3929. v = ((rd & 0x8000) != (-ea & 0x8000)
  3930. && (rd & 0x8000) != (res & 0x8000));
  3931. break;
  3932. case O_NEG:
  3933. v = (rd == 0x8000);
  3934. break;
  3935. }
  3936. goto next;
  3937. alu32:
  3938. if (store2 (sd, &code->dst, res))
  3939. goto end;
  3940. just_flags_alu32:
  3941. n = res & 0x80000000;
  3942. nz = res & 0xffffffff;
  3943. switch (code->opcode / 4)
  3944. {
  3945. case O_ADD:
  3946. case O_ADDX:
  3947. v = ((rd & 0x80000000) == (ea & 0x80000000)
  3948. && (rd & 0x80000000) != (res & 0x80000000));
  3949. c = ((unsigned) res < (unsigned) rd) ||
  3950. ((unsigned) res < (unsigned) ea);
  3951. break;
  3952. case O_SUB:
  3953. case O_SUBX:
  3954. case O_CMP:
  3955. v = ((rd & 0x80000000) != (-ea & 0x80000000)
  3956. && (rd & 0x80000000) != (res & 0x80000000));
  3957. c = (unsigned) rd < (unsigned) -ea;
  3958. break;
  3959. case O_NEG:
  3960. v = (rd == 0x80000000);
  3961. c = res != 0;
  3962. break;
  3963. }
  3964. goto next;
  3965. next:
  3966. if ((res = h8_get_delayed_branch (sd)) != 0)
  3967. {
  3968. pc = res;
  3969. h8_set_delayed_branch (sd, 0);
  3970. }
  3971. else
  3972. pc = code->next_pc;
  3973. end:
  3974. if (--poll_count < 0)
  3975. {
  3976. poll_count = POLL_QUIT_INTERVAL;
  3977. if ((*sim_callback->poll_quit) != NULL
  3978. && (*sim_callback->poll_quit) (sim_callback))
  3979. sim_engine_set_run_state (sd, sim_stopped, SIGINT);
  3980. }
  3981. sim_engine_get_run_state (sd, &reason, &sigrc);
  3982. } while (reason == sim_running);
  3983. h8_set_ticks (sd, h8_get_ticks (sd) + get_now () - tick_start);
  3984. h8_set_cycles (sd, h8_get_cycles (sd) + cycles);
  3985. h8_set_insts (sd, h8_get_insts (sd) + insts);
  3986. h8_set_pc (sd, pc);
  3987. BUILDSR (sd);
  3988. if (h8300smode)
  3989. h8_set_exr (sd, (trace<<7) | intMask);
  3990. h8_set_mask (sd, oldmask);
  3991. }
  3992. int
  3993. sim_write (SIM_DESC sd, SIM_ADDR addr, const unsigned char *buffer, int size)
  3994. {
  3995. int i;
  3996. init_pointers (sd);
  3997. if (addr < 0)
  3998. return 0;
  3999. for (i = 0; i < size; i++)
  4000. {
  4001. if (addr < memory_size)
  4002. {
  4003. h8_set_memory (sd, addr + i, buffer[i]);
  4004. h8_set_cache_idx (sd, addr + i, 0);
  4005. }
  4006. else
  4007. {
  4008. h8_set_eightbit (sd, (addr + i) & 0xff, buffer[i]);
  4009. }
  4010. }
  4011. return size;
  4012. }
  4013. int
  4014. sim_read (SIM_DESC sd, SIM_ADDR addr, unsigned char *buffer, int size)
  4015. {
  4016. init_pointers (sd);
  4017. if (addr < 0)
  4018. return 0;
  4019. if (addr < memory_size)
  4020. memcpy (buffer, h8_get_memory_buf (sd) + addr, size);
  4021. else
  4022. memcpy (buffer, h8_get_eightbit_buf (sd) + (addr & 0xff), size);
  4023. return size;
  4024. }
  4025. int
  4026. sim_store_register (SIM_DESC sd, int rn, unsigned char *value, int length)
  4027. {
  4028. int longval;
  4029. int shortval;
  4030. int intval;
  4031. longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
  4032. shortval = (value[0] << 8) | (value[1]);
  4033. intval = h8300hmode ? longval : shortval;
  4034. init_pointers (sd);
  4035. switch (rn)
  4036. {
  4037. case PC_REGNUM:
  4038. if(h8300_normal_mode)
  4039. h8_set_pc (sd, shortval); /* PC for Normal mode is 2 bytes */
  4040. else
  4041. h8_set_pc (sd, intval);
  4042. break;
  4043. default:
  4044. (*sim_callback->printf_filtered) (sim_callback,
  4045. "sim_store_register: bad regnum %d.\n",
  4046. rn);
  4047. case R0_REGNUM:
  4048. case R1_REGNUM:
  4049. case R2_REGNUM:
  4050. case R3_REGNUM:
  4051. case R4_REGNUM:
  4052. case R5_REGNUM:
  4053. case R6_REGNUM:
  4054. case R7_REGNUM:
  4055. h8_set_reg (sd, rn, intval);
  4056. break;
  4057. case CCR_REGNUM:
  4058. h8_set_ccr (sd, intval);
  4059. break;
  4060. case EXR_REGNUM:
  4061. h8_set_exr (sd, intval);
  4062. break;
  4063. case SBR_REGNUM:
  4064. h8_set_sbr (sd, intval);
  4065. break;
  4066. case VBR_REGNUM:
  4067. h8_set_vbr (sd, intval);
  4068. break;
  4069. case MACH_REGNUM:
  4070. h8_set_mach (sd, intval);
  4071. break;
  4072. case MACL_REGNUM:
  4073. h8_set_macl (sd, intval);
  4074. break;
  4075. case CYCLE_REGNUM:
  4076. h8_set_cycles (sd, longval);
  4077. break;
  4078. case INST_REGNUM:
  4079. h8_set_insts (sd, longval);
  4080. break;
  4081. case TICK_REGNUM:
  4082. h8_set_ticks (sd, longval);
  4083. break;
  4084. }
  4085. return length;
  4086. }
  4087. int
  4088. sim_fetch_register (SIM_DESC sd, int rn, unsigned char *buf, int length)
  4089. {
  4090. int v;
  4091. int longreg = 0;
  4092. init_pointers (sd);
  4093. if (!h8300smode && rn >= EXR_REGNUM)
  4094. rn++;
  4095. switch (rn)
  4096. {
  4097. default:
  4098. (*sim_callback->printf_filtered) (sim_callback,
  4099. "sim_fetch_register: bad regnum %d.\n",
  4100. rn);
  4101. v = 0;
  4102. break;
  4103. case CCR_REGNUM:
  4104. v = h8_get_ccr (sd);
  4105. break;
  4106. case EXR_REGNUM:
  4107. v = h8_get_exr (sd);
  4108. break;
  4109. case PC_REGNUM:
  4110. v = h8_get_pc (sd);
  4111. break;
  4112. case SBR_REGNUM:
  4113. v = h8_get_sbr (sd);
  4114. break;
  4115. case VBR_REGNUM:
  4116. v = h8_get_vbr (sd);
  4117. break;
  4118. case MACH_REGNUM:
  4119. v = h8_get_mach (sd);
  4120. break;
  4121. case MACL_REGNUM:
  4122. v = h8_get_macl (sd);
  4123. break;
  4124. case R0_REGNUM:
  4125. case R1_REGNUM:
  4126. case R2_REGNUM:
  4127. case R3_REGNUM:
  4128. case R4_REGNUM:
  4129. case R5_REGNUM:
  4130. case R6_REGNUM:
  4131. case R7_REGNUM:
  4132. v = h8_get_reg (sd, rn);
  4133. break;
  4134. case CYCLE_REGNUM:
  4135. v = h8_get_cycles (sd);
  4136. longreg = 1;
  4137. break;
  4138. case TICK_REGNUM:
  4139. v = h8_get_ticks (sd);
  4140. longreg = 1;
  4141. break;
  4142. case INST_REGNUM:
  4143. v = h8_get_insts (sd);
  4144. longreg = 1;
  4145. break;
  4146. }
  4147. /* In Normal mode PC is 2 byte, but other registers are 4 byte */
  4148. if ((h8300hmode || longreg) && !(rn == PC_REGNUM && h8300_normal_mode))
  4149. {
  4150. buf[0] = v >> 24;
  4151. buf[1] = v >> 16;
  4152. buf[2] = v >> 8;
  4153. buf[3] = v >> 0;
  4154. }
  4155. else
  4156. {
  4157. buf[0] = v >> 8;
  4158. buf[1] = v;
  4159. }
  4160. return -1;
  4161. }
  4162. void
  4163. sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
  4164. {
  4165. sim_engine_get_run_state (sd, reason, sigrc);
  4166. }
  4167. static void
  4168. set_simcache_size (SIM_DESC sd, int n)
  4169. {
  4170. if (sd->sim_cache)
  4171. free (sd->sim_cache);
  4172. if (n < 2)
  4173. n = 2;
  4174. sd->sim_cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
  4175. memset (sd->sim_cache, 0, sizeof (decoded_inst) * n);
  4176. sd->sim_cache_size = n;
  4177. }
  4178. void
  4179. sim_info (SIM_DESC sd, int verbose)
  4180. {
  4181. double timetaken = (double) h8_get_ticks (sd) / (double) now_persec ();
  4182. double virttime = h8_get_cycles (sd) / 10.0e6;
  4183. (*sim_callback->printf_filtered) (sim_callback,
  4184. "\n\n#instructions executed %10d\n",
  4185. h8_get_insts (sd));
  4186. (*sim_callback->printf_filtered) (sim_callback,
  4187. "#cycles (v approximate) %10d\n",
  4188. h8_get_cycles (sd));
  4189. (*sim_callback->printf_filtered) (sim_callback,
  4190. "#real time taken %10.4f\n",
  4191. timetaken);
  4192. (*sim_callback->printf_filtered) (sim_callback,
  4193. "#virtual time taken %10.4f\n",
  4194. virttime);
  4195. if (timetaken != 0.0)
  4196. (*sim_callback->printf_filtered) (sim_callback,
  4197. "#simulation ratio %10.4f\n",
  4198. virttime / timetaken);
  4199. (*sim_callback->printf_filtered) (sim_callback,
  4200. "#compiles %10d\n",
  4201. h8_get_compiles (sd));
  4202. (*sim_callback->printf_filtered) (sim_callback,
  4203. "#cache size %10d\n",
  4204. sd->sim_cache_size);
  4205. #ifdef ADEBUG
  4206. /* This to be conditional on `what' (aka `verbose'),
  4207. however it was never passed as non-zero. */
  4208. if (1)
  4209. {
  4210. int i;
  4211. for (i = 0; i < O_LAST; i++)
  4212. {
  4213. if (h8_get_stats (sd, i))
  4214. (*sim_callback->printf_filtered) (sim_callback, "%d: %d\n",
  4215. i, h8_get_stats (sd, i));
  4216. }
  4217. }
  4218. #endif
  4219. }
  4220. /* Indicate whether the cpu is an H8/300 or H8/300H.
  4221. FLAG is non-zero for the H8/300H. */
  4222. void
  4223. set_h8300h (unsigned long machine)
  4224. {
  4225. /* FIXME: Much of the code in sim_load can be moved to sim_open.
  4226. This function being replaced by a sim_open:ARGV configuration
  4227. option. */
  4228. h8300hmode = h8300smode = h8300sxmode = h8300_normal_mode = 0;
  4229. if (machine == bfd_mach_h8300sx || machine == bfd_mach_h8300sxn)
  4230. h8300sxmode = 1;
  4231. if (machine == bfd_mach_h8300s || machine == bfd_mach_h8300sn || h8300sxmode)
  4232. h8300smode = 1;
  4233. if (machine == bfd_mach_h8300h || machine == bfd_mach_h8300hn || h8300smode)
  4234. h8300hmode = 1;
  4235. if(machine == bfd_mach_h8300hn || machine == bfd_mach_h8300sn || machine == bfd_mach_h8300sxn)
  4236. h8300_normal_mode = 1;
  4237. }
  4238. static sim_cia
  4239. h8300_pc_get (sim_cpu *cpu)
  4240. {
  4241. return cpu->pc;
  4242. }
  4243. static void
  4244. h8300_pc_set (sim_cpu *cpu, sim_cia pc)
  4245. {
  4246. cpu->pc = pc;
  4247. }
  4248. /* Cover function of sim_state_free to free the cpu buffers as well. */
  4249. static void
  4250. free_state (SIM_DESC sd)
  4251. {
  4252. if (STATE_MODULES (sd) != NULL)
  4253. sim_module_uninstall (sd);
  4254. /* Fixme: free buffers in _sim_cpu. */
  4255. sim_state_free (sd);
  4256. }
  4257. SIM_DESC
  4258. sim_open (SIM_OPEN_KIND kind,
  4259. struct host_callback_struct *callback,
  4260. struct bfd *abfd,
  4261. char **argv)
  4262. {
  4263. int i;
  4264. SIM_DESC sd;
  4265. sim_cpu *cpu;
  4266. sd = sim_state_alloc (kind, callback);
  4267. /* The cpu data is kept in a separately allocated chunk of memory. */
  4268. if (sim_cpu_alloc_all (sd, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK)
  4269. {
  4270. free_state (sd);
  4271. return 0;
  4272. }
  4273. cpu = STATE_CPU (sd, 0);
  4274. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  4275. sim_state_initialize (sd, cpu);
  4276. /* sim_cpu object is new, so some initialization is needed. */
  4277. init_pointers_needed = 1;
  4278. /* For compatibility (FIXME: is this right?). */
  4279. current_alignment = NONSTRICT_ALIGNMENT;
  4280. current_target_byte_order = BIG_ENDIAN;
  4281. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  4282. {
  4283. free_state (sd);
  4284. return 0;
  4285. }
  4286. /* getopt will print the error message so we just have to exit if
  4287. this fails. FIXME: Hmmm... in the case of gdb we need getopt
  4288. to call print_filtered. */
  4289. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  4290. {
  4291. /* Uninstall the modules to avoid memory leaks,
  4292. file descriptor leaks, etc. */
  4293. free_state (sd);
  4294. return 0;
  4295. }
  4296. /* Check for/establish the a reference program image. */
  4297. if (sim_analyze_program (sd,
  4298. (STATE_PROG_ARGV (sd) != NULL
  4299. ? *STATE_PROG_ARGV (sd)
  4300. : NULL), abfd) != SIM_RC_OK)
  4301. {
  4302. free_state (sd);
  4303. return 0;
  4304. }
  4305. /* Establish any remaining configuration options. */
  4306. if (sim_config (sd) != SIM_RC_OK)
  4307. {
  4308. free_state (sd);
  4309. return 0;
  4310. }
  4311. if (sim_post_argv_init (sd) != SIM_RC_OK)
  4312. {
  4313. /* Uninstall the modules to avoid memory leaks,
  4314. file descriptor leaks, etc. */
  4315. free_state (sd);
  4316. return 0;
  4317. }
  4318. /* CPU specific initialization. */
  4319. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  4320. {
  4321. SIM_CPU *cpu = STATE_CPU (sd, i);
  4322. CPU_PC_FETCH (cpu) = h8300_pc_get;
  4323. CPU_PC_STORE (cpu) = h8300_pc_set;
  4324. }
  4325. /* sim_hw_configure (sd); */
  4326. /* FIXME: Much of the code in sim_load can be moved here. */
  4327. sim_kind = kind;
  4328. myname = argv[0];
  4329. sim_callback = callback;
  4330. return sd;
  4331. }
  4332. void
  4333. sim_close (SIM_DESC sd, int quitting)
  4334. {
  4335. /* Nothing to do. */
  4336. }
  4337. /* Called by gdb to load a program into memory. */
  4338. SIM_RC
  4339. sim_load (SIM_DESC sd, const char *prog, bfd *abfd, int from_tty)
  4340. {
  4341. bfd *prog_bfd;
  4342. /* FIXME: The code below that sets a specific variant of the H8/300
  4343. being simulated should be moved to sim_open(). */
  4344. /* See if the file is for the H8/300 or H8/300H. */
  4345. /* ??? This may not be the most efficient way. The z8k simulator
  4346. does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
  4347. if (abfd != NULL)
  4348. prog_bfd = abfd;
  4349. else
  4350. prog_bfd = bfd_openr (prog, NULL);
  4351. if (prog_bfd != NULL)
  4352. {
  4353. /* Set the cpu type. We ignore failure from bfd_check_format
  4354. and bfd_openr as sim_load_file checks too. */
  4355. if (bfd_check_format (prog_bfd, bfd_object))
  4356. {
  4357. set_h8300h (bfd_get_mach (prog_bfd));
  4358. }
  4359. }
  4360. /* If we're using gdb attached to the simulator, then we have to
  4361. reallocate memory for the simulator.
  4362. When gdb first starts, it calls fetch_registers (among other
  4363. functions), which in turn calls init_pointers, which allocates
  4364. simulator memory.
  4365. The problem is when we do that, we don't know whether we're
  4366. debugging an H8/300 or H8/300H program.
  4367. This is the first point at which we can make that determination,
  4368. so we just reallocate memory now; this will also allow us to handle
  4369. switching between H8/300 and H8/300H programs without exiting
  4370. gdb. */
  4371. if (h8300smode && !h8300_normal_mode)
  4372. memory_size = H8300S_MSIZE;
  4373. else if (h8300hmode && !h8300_normal_mode)
  4374. memory_size = H8300H_MSIZE;
  4375. else
  4376. memory_size = H8300_MSIZE;
  4377. if (h8_get_memory_buf (sd))
  4378. free (h8_get_memory_buf (sd));
  4379. if (h8_get_cache_idx_buf (sd))
  4380. free (h8_get_cache_idx_buf (sd));
  4381. if (h8_get_eightbit_buf (sd))
  4382. free (h8_get_eightbit_buf (sd));
  4383. h8_set_memory_buf (sd, (unsigned char *)
  4384. calloc (sizeof (char), memory_size));
  4385. h8_set_cache_idx_buf (sd, (unsigned short *)
  4386. calloc (sizeof (short), memory_size));
  4387. sd->memory_size = memory_size;
  4388. h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
  4389. /* `msize' must be a power of two. */
  4390. if ((memory_size & (memory_size - 1)) != 0)
  4391. {
  4392. (*sim_callback->printf_filtered) (sim_callback,
  4393. "sim_load: bad memory size.\n");
  4394. return SIM_RC_FAIL;
  4395. }
  4396. h8_set_mask (sd, memory_size - 1);
  4397. if (sim_load_file (sd, myname, sim_callback, prog, prog_bfd,
  4398. sim_kind == SIM_OPEN_DEBUG,
  4399. 0, sim_write)
  4400. == NULL)
  4401. {
  4402. /* Close the bfd if we opened it. */
  4403. if (abfd == NULL && prog_bfd != NULL)
  4404. bfd_close (prog_bfd);
  4405. return SIM_RC_FAIL;
  4406. }
  4407. /* Close the bfd if we opened it. */
  4408. if (abfd == NULL && prog_bfd != NULL)
  4409. bfd_close (prog_bfd);
  4410. return SIM_RC_OK;
  4411. }
  4412. SIM_RC
  4413. sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env)
  4414. {
  4415. int i = 0;
  4416. int len_arg = 0;
  4417. int no_of_args = 0;
  4418. if (abfd != NULL)
  4419. h8_set_pc (sd, bfd_get_start_address (abfd));
  4420. else
  4421. h8_set_pc (sd, 0);
  4422. /* Command Line support. */
  4423. if (argv != NULL)
  4424. {
  4425. /* Counting the no. of commandline arguments. */
  4426. for (no_of_args = 0; argv[no_of_args] != NULL; no_of_args++)
  4427. continue;
  4428. /* Allocating memory for the argv pointers. */
  4429. h8_set_command_line (sd, (char **) malloc ((sizeof (char *))
  4430. * (no_of_args + 1)));
  4431. for (i = 0; i < no_of_args; i++)
  4432. {
  4433. /* Copying the argument string. */
  4434. h8_set_cmdline_arg (sd, i, (char *) strdup (argv[i]));
  4435. }
  4436. h8_set_cmdline_arg (sd, i, NULL);
  4437. }
  4438. return SIM_RC_OK;
  4439. }