interp.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485
  1. /* Simulator for the Renesas (formerly Hitachi) / SuperH Inc. SH architecture.
  2. Written by Steve Chamberlain of Cygnus Support.
  3. sac@cygnus.com
  4. This file is part of SH sim
  5. THIS SOFTWARE IS NOT COPYRIGHTED
  6. Cygnus offers the following for use in the public domain. Cygnus
  7. makes no warranty with regard to the software or it's performance
  8. and the user accepts the software "AS IS" with all faults.
  9. CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
  10. THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  12. */
  13. /* This must come before any other includes. */
  14. #include "defs.h"
  15. #include <ctype.h>
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <signal.h>
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #ifdef HAVE_MMAP
  23. #include <sys/mman.h>
  24. # ifndef MAP_FAILED
  25. # define MAP_FAILED -1
  26. # endif
  27. # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
  28. # define MAP_ANONYMOUS MAP_ANON
  29. # endif
  30. #endif
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #ifdef HAVE_SYS_STAT_H
  34. #include <sys/stat.h>
  35. #endif
  36. #include <time.h>
  37. #include <sys/time.h>
  38. #ifdef HAVE_UTIME_H
  39. #include <utime.h>
  40. #endif
  41. #ifndef _WIN32
  42. #include <sys/wait.h>
  43. #endif
  44. #include "bfd.h"
  45. #include "sim/callback.h"
  46. #include "sim/sim.h"
  47. #include "gdb/sim-sh.h"
  48. #include "sim-main.h"
  49. #include "sim-base.h"
  50. #include "sim-options.h"
  51. #include "target-newlib-syscall.h"
  52. #include <math.h>
  53. #ifdef _WIN32
  54. #include <float.h> /* Needed for _isnan() */
  55. #ifndef isnan
  56. #define isnan _isnan
  57. #endif
  58. #endif
  59. #ifndef SIGBUS
  60. #define SIGBUS SIGSEGV
  61. #endif
  62. #ifndef SIGQUIT
  63. #define SIGQUIT SIGTERM
  64. #endif
  65. #ifndef SIGTRAP
  66. #define SIGTRAP 5
  67. #endif
  68. /* TODO: Stop using these names. */
  69. #undef SEXT
  70. #undef SEXT32
  71. extern unsigned short sh_jump_table[], sh_dsp_table[0x1000], ppi_table[];
  72. #define O_RECOMPILE 85
  73. #define DEFINE_TABLE
  74. #define DISASSEMBLER_TABLE
  75. /* Define the rate at which the simulator should poll the host
  76. for a quit. */
  77. #define POLL_QUIT_INTERVAL 0x60000
  78. /* TODO: Move into sim_cpu. */
  79. saved_state_type saved_state;
  80. struct loop_bounds { unsigned char *start, *end; };
  81. /* These variables are at file scope so that functions other than
  82. sim_resume can use the fetch/store macros */
  83. #define target_little_endian (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
  84. static int global_endianw, endianb;
  85. static int target_dsp;
  86. #define host_little_endian (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
  87. static int maskw = 0;
  88. static int maskl = 0;
  89. /* Short hand definitions of the registers */
  90. #define SBIT(x) ((x)&sbit)
  91. #define R0 saved_state.asregs.regs[0]
  92. #define Rn saved_state.asregs.regs[n]
  93. #define Rm saved_state.asregs.regs[m]
  94. #define UR0 (unsigned int) (saved_state.asregs.regs[0])
  95. #define UR (unsigned int) R
  96. #define UR (unsigned int) R
  97. #define SR0 saved_state.asregs.regs[0]
  98. #define CREG(n) (saved_state.asregs.cregs[(n)])
  99. #define GBR saved_state.asregs.gbr
  100. #define VBR saved_state.asregs.vbr
  101. #define DBR saved_state.asregs.dbr
  102. #define TBR saved_state.asregs.tbr
  103. #define IBCR saved_state.asregs.ibcr
  104. #define IBNR saved_state.asregs.ibnr
  105. #define BANKN (saved_state.asregs.ibnr & 0x1ff)
  106. #define ME ((saved_state.asregs.ibnr >> 14) & 0x3)
  107. #define SSR saved_state.asregs.ssr
  108. #define SPC saved_state.asregs.spc
  109. #define SGR saved_state.asregs.sgr
  110. #define SREG(n) (saved_state.asregs.sregs[(n)])
  111. #define MACH saved_state.asregs.mach
  112. #define MACL saved_state.asregs.macl
  113. #define PR saved_state.asregs.pr
  114. #define FPUL saved_state.asregs.fpul
  115. #define PC insn_ptr
  116. /* Alternate bank of registers r0-r7 */
  117. /* Note: code controling SR handles flips between BANK0 and BANK1 */
  118. #define Rn_BANK(n) (saved_state.asregs.bank[(n)])
  119. #define SET_Rn_BANK(n, EXP) do { saved_state.asregs.bank[(n)] = (EXP); } while (0)
  120. /* Manipulate SR */
  121. #define SR_MASK_BO (1 << 14)
  122. #define SR_MASK_CS (1 << 13)
  123. #define SR_MASK_DMY (1 << 11)
  124. #define SR_MASK_DMX (1 << 10)
  125. #define SR_MASK_M (1 << 9)
  126. #define SR_MASK_Q (1 << 8)
  127. #define SR_MASK_I (0xf << 4)
  128. #define SR_MASK_S (1 << 1)
  129. #define SR_MASK_T (1 << 0)
  130. #define SR_MASK_BL (1 << 28)
  131. #define SR_MASK_RB (1 << 29)
  132. #define SR_MASK_MD (1 << 30)
  133. #define SR_MASK_RC 0x0fff0000
  134. #define SR_RC_INCREMENT -0x00010000
  135. #define BO ((saved_state.asregs.sr & SR_MASK_BO) != 0)
  136. #define CS ((saved_state.asregs.sr & SR_MASK_CS) != 0)
  137. #define M ((saved_state.asregs.sr & SR_MASK_M) != 0)
  138. #define Q ((saved_state.asregs.sr & SR_MASK_Q) != 0)
  139. #define S ((saved_state.asregs.sr & SR_MASK_S) != 0)
  140. #define T ((saved_state.asregs.sr & SR_MASK_T) != 0)
  141. #define LDST ((saved_state.asregs.ldst) != 0)
  142. #define SR_BL ((saved_state.asregs.sr & SR_MASK_BL) != 0)
  143. #define SR_RB ((saved_state.asregs.sr & SR_MASK_RB) != 0)
  144. #define SR_MD ((saved_state.asregs.sr & SR_MASK_MD) != 0)
  145. #define SR_DMY ((saved_state.asregs.sr & SR_MASK_DMY) != 0)
  146. #define SR_DMX ((saved_state.asregs.sr & SR_MASK_DMX) != 0)
  147. #define SR_RC ((saved_state.asregs.sr & SR_MASK_RC))
  148. /* Note: don't use this for privileged bits */
  149. #define SET_SR_BIT(EXP, BIT) \
  150. do { \
  151. if ((EXP) & 1) \
  152. saved_state.asregs.sr |= (BIT); \
  153. else \
  154. saved_state.asregs.sr &= ~(BIT); \
  155. } while (0)
  156. #define SET_SR_BO(EXP) SET_SR_BIT ((EXP), SR_MASK_BO)
  157. #define SET_SR_CS(EXP) SET_SR_BIT ((EXP), SR_MASK_CS)
  158. #define SET_BANKN(EXP) \
  159. do { \
  160. IBNR = (IBNR & 0xfe00) | ((EXP) & 0x1f); \
  161. } while (0)
  162. #define SET_ME(EXP) \
  163. do { \
  164. IBNR = (IBNR & 0x3fff) | (((EXP) & 0x3) << 14); \
  165. } while (0)
  166. #define SET_SR_M(EXP) SET_SR_BIT ((EXP), SR_MASK_M)
  167. #define SET_SR_Q(EXP) SET_SR_BIT ((EXP), SR_MASK_Q)
  168. #define SET_SR_S(EXP) SET_SR_BIT ((EXP), SR_MASK_S)
  169. #define SET_SR_T(EXP) SET_SR_BIT ((EXP), SR_MASK_T)
  170. #define SET_LDST(EXP) (saved_state.asregs.ldst = ((EXP) != 0))
  171. /* stc currently relies on being able to read SR without modifications. */
  172. #define GET_SR() (saved_state.asregs.sr - 0)
  173. #define SET_SR(x) set_sr (x)
  174. #define SET_RC(x) \
  175. (saved_state.asregs.sr \
  176. = (saved_state.asregs.sr & 0xf000ffff) | ((x) & 0xfff) << 16)
  177. /* Manipulate FPSCR */
  178. #define FPSCR_MASK_FR (1 << 21)
  179. #define FPSCR_MASK_SZ (1 << 20)
  180. #define FPSCR_MASK_PR (1 << 19)
  181. #define FPSCR_FR ((GET_FPSCR () & FPSCR_MASK_FR) != 0)
  182. #define FPSCR_SZ ((GET_FPSCR () & FPSCR_MASK_SZ) != 0)
  183. #define FPSCR_PR ((GET_FPSCR () & FPSCR_MASK_PR) != 0)
  184. static void
  185. set_fpscr1 (int x)
  186. {
  187. int old = saved_state.asregs.fpscr;
  188. saved_state.asregs.fpscr = (x);
  189. /* swap the floating point register banks */
  190. if ((saved_state.asregs.fpscr ^ old) & FPSCR_MASK_FR
  191. /* Ignore bit change if simulating sh-dsp. */
  192. && ! target_dsp)
  193. {
  194. union fregs_u tmpf = saved_state.asregs.fregs[0];
  195. saved_state.asregs.fregs[0] = saved_state.asregs.fregs[1];
  196. saved_state.asregs.fregs[1] = tmpf;
  197. }
  198. }
  199. /* sts relies on being able to read fpscr directly. */
  200. #define GET_FPSCR() (saved_state.asregs.fpscr)
  201. #define SET_FPSCR(x) \
  202. do { \
  203. set_fpscr1 (x); \
  204. } while (0)
  205. #define DSR (saved_state.asregs.fpscr)
  206. #define RAISE_EXCEPTION(x) \
  207. (saved_state.asregs.exception = x, saved_state.asregs.insn_end = 0)
  208. #define RAISE_EXCEPTION_IF_IN_DELAY_SLOT() \
  209. if (in_delay_slot) RAISE_EXCEPTION (SIGILL)
  210. /* This function exists mainly for the purpose of setting a breakpoint to
  211. catch simulated bus errors when running the simulator under GDB. */
  212. static void
  213. raise_exception (int x)
  214. {
  215. RAISE_EXCEPTION (x);
  216. }
  217. static void
  218. raise_buserror (void)
  219. {
  220. raise_exception (SIGBUS);
  221. }
  222. #define PROCESS_SPECIAL_ADDRESS(addr, endian, ptr, bits_written, \
  223. forbidden_addr_bits, data, retval) \
  224. do { \
  225. if (addr & forbidden_addr_bits) \
  226. { \
  227. raise_buserror (); \
  228. return retval; \
  229. } \
  230. else if ((addr & saved_state.asregs.xyram_select) \
  231. == saved_state.asregs.xram_start) \
  232. ptr = (void *) &saved_state.asregs.xmem_offset[addr ^ endian]; \
  233. else if ((addr & saved_state.asregs.xyram_select) \
  234. == saved_state.asregs.yram_start) \
  235. ptr = (void *) &saved_state.asregs.ymem_offset[addr ^ endian]; \
  236. else if ((unsigned) addr >> 24 == 0xf0 \
  237. && bits_written == 32 && (data & 1) == 0) \
  238. /* This invalidates (if not associative) or might invalidate \
  239. (if associative) an instruction cache line. This is used for \
  240. trampolines. Since we don't simulate the cache, this is a no-op \
  241. as far as the simulator is concerned. */ \
  242. return retval; \
  243. else \
  244. { \
  245. if (bits_written == 8 && addr > 0x5000000) \
  246. IOMEM (addr, 1, data); \
  247. /* We can't do anything useful with the other stuff, so fail. */ \
  248. raise_buserror (); \
  249. return retval; \
  250. } \
  251. } while (0)
  252. /* FIXME: sim_resume should be renamed to sim_engine_run. sim_resume
  253. being implemented by ../common/sim_resume.c and the below should
  254. make a call to sim_engine_halt */
  255. #define BUSERROR(addr, mask) ((addr) & (mask))
  256. #define WRITE_BUSERROR(addr, mask, data, addr_func) \
  257. do \
  258. { \
  259. if (addr & mask) \
  260. { \
  261. addr_func (addr, data); \
  262. return; \
  263. } \
  264. } \
  265. while (0)
  266. #define READ_BUSERROR(addr, mask, addr_func) \
  267. do \
  268. { \
  269. if (addr & mask) \
  270. return addr_func (addr); \
  271. } \
  272. while (0)
  273. /* Define this to enable register lifetime checking.
  274. The compiler generates "add #0,rn" insns to mark registers as invalid,
  275. the simulator uses this info to call fail if it finds a ref to an invalid
  276. register before a def
  277. #define PARANOID
  278. */
  279. #ifdef PARANOID
  280. int valid[16];
  281. #define CREF(x) if (!valid[x]) fail ();
  282. #define CDEF(x) valid[x] = 1;
  283. #define UNDEF(x) valid[x] = 0;
  284. #else
  285. #define CREF(x)
  286. #define CDEF(x)
  287. #define UNDEF(x)
  288. #endif
  289. static void parse_and_set_memory_size (SIM_DESC sd, const char *str);
  290. static int IOMEM (int addr, int write, int value);
  291. static struct loop_bounds get_loop_bounds (int, int, unsigned char *,
  292. unsigned char *, int, int);
  293. static void process_wlat_addr (int, int);
  294. static void process_wwat_addr (int, int);
  295. static void process_wbat_addr (int, int);
  296. static int process_rlat_addr (int);
  297. static int process_rwat_addr (int);
  298. static int process_rbat_addr (int);
  299. /* Floating point registers */
  300. #define DR(n) (get_dr (n))
  301. static double
  302. get_dr (int n)
  303. {
  304. n = (n & ~1);
  305. if (host_little_endian)
  306. {
  307. union
  308. {
  309. int i[2];
  310. double d;
  311. } dr;
  312. dr.i[1] = saved_state.asregs.fregs[0].i[n + 0];
  313. dr.i[0] = saved_state.asregs.fregs[0].i[n + 1];
  314. return dr.d;
  315. }
  316. else
  317. return (saved_state.asregs.fregs[0].d[n >> 1]);
  318. }
  319. #define SET_DR(n, EXP) set_dr ((n), (EXP))
  320. static void
  321. set_dr (int n, double exp)
  322. {
  323. n = (n & ~1);
  324. if (host_little_endian)
  325. {
  326. union
  327. {
  328. int i[2];
  329. double d;
  330. } dr;
  331. dr.d = exp;
  332. saved_state.asregs.fregs[0].i[n + 0] = dr.i[1];
  333. saved_state.asregs.fregs[0].i[n + 1] = dr.i[0];
  334. }
  335. else
  336. saved_state.asregs.fregs[0].d[n >> 1] = exp;
  337. }
  338. #define SET_FI(n,EXP) (saved_state.asregs.fregs[0].i[(n)] = (EXP))
  339. #define FI(n) (saved_state.asregs.fregs[0].i[(n)])
  340. #define FR(n) (saved_state.asregs.fregs[0].f[(n)])
  341. #define SET_FR(n,EXP) (saved_state.asregs.fregs[0].f[(n)] = (EXP))
  342. #define XD_TO_XF(n) ((((n) & 1) << 5) | ((n) & 0x1e))
  343. #define XF(n) (saved_state.asregs.fregs[(n) >> 5].i[(n) & 0x1f])
  344. #define SET_XF(n,EXP) (saved_state.asregs.fregs[(n) >> 5].i[(n) & 0x1f] = (EXP))
  345. #define RS saved_state.asregs.rs
  346. #define RE saved_state.asregs.re
  347. #define MOD (saved_state.asregs.mod)
  348. #define SET_MOD(i) \
  349. (MOD = (i), \
  350. MOD_ME = (unsigned) MOD >> 16 | (SR_DMY ? ~0xffff : (SR_DMX ? 0 : 0x10000)), \
  351. MOD_DELTA = (MOD & 0xffff) - ((unsigned) MOD >> 16))
  352. #define DSP_R(n) saved_state.asregs.sregs[(n)]
  353. #define DSP_GRD(n) DSP_R ((n) + 8)
  354. #define GET_DSP_GRD(n) ((n | 2) == 7 ? SEXT (DSP_GRD (n)) : SIGN32 (DSP_R (n)))
  355. #define A1 DSP_R (5)
  356. #define A0 DSP_R (7)
  357. #define X0 DSP_R (8)
  358. #define X1 DSP_R (9)
  359. #define Y0 DSP_R (10)
  360. #define Y1 DSP_R (11)
  361. #define M0 DSP_R (12)
  362. #define A1G DSP_R (13)
  363. #define M1 DSP_R (14)
  364. #define A0G DSP_R (15)
  365. /* DSP_R (16) / DSP_GRD (16) are used as a fake destination for pcmp. */
  366. #define MOD_ME DSP_GRD (17)
  367. #define MOD_DELTA DSP_GRD (18)
  368. #define FP_OP(n, OP, m) \
  369. { \
  370. if (FPSCR_PR) \
  371. { \
  372. if (((n) & 1) || ((m) & 1)) \
  373. RAISE_EXCEPTION (SIGILL); \
  374. else \
  375. SET_DR (n, (DR (n) OP DR (m))); \
  376. } \
  377. else \
  378. SET_FR (n, (FR (n) OP FR (m))); \
  379. } while (0)
  380. #define FP_UNARY(n, OP) \
  381. { \
  382. if (FPSCR_PR) \
  383. { \
  384. if ((n) & 1) \
  385. RAISE_EXCEPTION (SIGILL); \
  386. else \
  387. SET_DR (n, (OP (DR (n)))); \
  388. } \
  389. else \
  390. SET_FR (n, (OP (FR (n)))); \
  391. } while (0)
  392. #define FP_CMP(n, OP, m) \
  393. { \
  394. if (FPSCR_PR) \
  395. { \
  396. if (((n) & 1) || ((m) & 1)) \
  397. RAISE_EXCEPTION (SIGILL); \
  398. else \
  399. SET_SR_T (DR (n) OP DR (m)); \
  400. } \
  401. else \
  402. SET_SR_T (FR (n) OP FR (m)); \
  403. } while (0)
  404. static void
  405. set_sr (int new_sr)
  406. {
  407. /* do we need to swap banks */
  408. int old_gpr = SR_MD && SR_RB;
  409. int new_gpr = (new_sr & SR_MASK_MD) && (new_sr & SR_MASK_RB);
  410. if (old_gpr != new_gpr)
  411. {
  412. int i, tmp;
  413. for (i = 0; i < 8; i++)
  414. {
  415. tmp = saved_state.asregs.bank[i];
  416. saved_state.asregs.bank[i] = saved_state.asregs.regs[i];
  417. saved_state.asregs.regs[i] = tmp;
  418. }
  419. }
  420. saved_state.asregs.sr = new_sr;
  421. SET_MOD (MOD);
  422. }
  423. static INLINE void
  424. wlat_fast (unsigned char *memory, int x, int value, int maskl)
  425. {
  426. int v = value;
  427. unsigned int *p = (unsigned int *) (memory + x);
  428. WRITE_BUSERROR (x, maskl, v, process_wlat_addr);
  429. *p = v;
  430. }
  431. static INLINE void
  432. wwat_fast (unsigned char *memory, int x, int value, int maskw, int endianw)
  433. {
  434. int v = value;
  435. unsigned short *p = (unsigned short *) (memory + (x ^ endianw));
  436. WRITE_BUSERROR (x, maskw, v, process_wwat_addr);
  437. *p = v;
  438. }
  439. static INLINE void
  440. wbat_fast (unsigned char *memory, int x, int value, int maskb)
  441. {
  442. unsigned char *p = memory + (x ^ endianb);
  443. WRITE_BUSERROR (x, maskb, value, process_wbat_addr);
  444. p[0] = value;
  445. }
  446. /* Read functions */
  447. static INLINE int
  448. rlat_fast (unsigned char *memory, int x, int maskl)
  449. {
  450. unsigned int *p = (unsigned int *) (memory + x);
  451. READ_BUSERROR (x, maskl, process_rlat_addr);
  452. return *p;
  453. }
  454. static INLINE int
  455. rwat_fast (unsigned char *memory, int x, int maskw, int endianw)
  456. {
  457. unsigned short *p = (unsigned short *) (memory + (x ^ endianw));
  458. READ_BUSERROR (x, maskw, process_rwat_addr);
  459. return *p;
  460. }
  461. static INLINE int
  462. riat_fast (unsigned char *insn_ptr, int endianw)
  463. {
  464. unsigned short *p = (unsigned short *) ((uintptr_t) insn_ptr ^ endianw);
  465. return *p;
  466. }
  467. static INLINE int
  468. rbat_fast (unsigned char *memory, int x, int maskb)
  469. {
  470. unsigned char *p = memory + (x ^ endianb);
  471. READ_BUSERROR (x, maskb, process_rbat_addr);
  472. return *p;
  473. }
  474. #define RWAT(x) (rwat_fast (memory, x, maskw, endianw))
  475. #define RLAT(x) (rlat_fast (memory, x, maskl))
  476. #define RBAT(x) (rbat_fast (memory, x, maskb))
  477. #define RIAT(p) (riat_fast ((p), endianw))
  478. #define WWAT(x,v) (wwat_fast (memory, x, v, maskw, endianw))
  479. #define WLAT(x,v) (wlat_fast (memory, x, v, maskl))
  480. #define WBAT(x,v) (wbat_fast (memory, x, v, maskb))
  481. #define RUWAT(x) (RWAT (x) & 0xffff)
  482. #define RSWAT(x) ((short) (RWAT (x)))
  483. #define RSLAT(x) ((long) (RLAT (x)))
  484. #define RSBAT(x) (SEXT (RBAT (x)))
  485. #define RDAT(x, n) (do_rdat (memory, (x), (n), (maskl)))
  486. static int
  487. do_rdat (unsigned char *memory, int x, int n, int maskl)
  488. {
  489. int f0;
  490. int f1;
  491. int i = (n & 1);
  492. int j = (n & ~1);
  493. f0 = rlat_fast (memory, x + 0, maskl);
  494. f1 = rlat_fast (memory, x + 4, maskl);
  495. saved_state.asregs.fregs[i].i[(j + 0)] = f0;
  496. saved_state.asregs.fregs[i].i[(j + 1)] = f1;
  497. return 0;
  498. }
  499. #define WDAT(x, n) (do_wdat (memory, (x), (n), (maskl)))
  500. static int
  501. do_wdat (unsigned char *memory, int x, int n, int maskl)
  502. {
  503. int f0;
  504. int f1;
  505. int i = (n & 1);
  506. int j = (n & ~1);
  507. f0 = saved_state.asregs.fregs[i].i[(j + 0)];
  508. f1 = saved_state.asregs.fregs[i].i[(j + 1)];
  509. wlat_fast (memory, (x + 0), f0, maskl);
  510. wlat_fast (memory, (x + 4), f1, maskl);
  511. return 0;
  512. }
  513. static void
  514. process_wlat_addr (int addr, int value)
  515. {
  516. unsigned int *ptr;
  517. PROCESS_SPECIAL_ADDRESS (addr, endianb, ptr, 32, 3, value, );
  518. *ptr = value;
  519. }
  520. static void
  521. process_wwat_addr (int addr, int value)
  522. {
  523. unsigned short *ptr;
  524. PROCESS_SPECIAL_ADDRESS (addr, endianb, ptr, 16, 1, value, );
  525. *ptr = value;
  526. }
  527. static void
  528. process_wbat_addr (int addr, int value)
  529. {
  530. unsigned char *ptr;
  531. PROCESS_SPECIAL_ADDRESS (addr, endianb, ptr, 8, 0, value, );
  532. *ptr = value;
  533. }
  534. static int
  535. process_rlat_addr (int addr)
  536. {
  537. unsigned char *ptr;
  538. PROCESS_SPECIAL_ADDRESS (addr, endianb, ptr, -32, 3, -1, 0);
  539. return *ptr;
  540. }
  541. static int
  542. process_rwat_addr (int addr)
  543. {
  544. unsigned char *ptr;
  545. PROCESS_SPECIAL_ADDRESS (addr, endianb, ptr, -16, 1, -1, 0);
  546. return *ptr;
  547. }
  548. static int
  549. process_rbat_addr (int addr)
  550. {
  551. unsigned char *ptr;
  552. PROCESS_SPECIAL_ADDRESS (addr, endianb, ptr, -8, 0, -1, 0);
  553. return *ptr;
  554. }
  555. #define SEXT(x) (((x & 0xff) ^ (~0x7f))+0x80)
  556. #define SEXT12(x) (((x & 0xfff) ^ 0x800) - 0x800)
  557. #define SEXTW(y) ((int) ((short) y))
  558. #if 0
  559. #define SEXT32(x) ((int) ((x & 0xffffffff) ^ 0x80000000U) - 0x7fffffff - 1)
  560. #else
  561. #define SEXT32(x) ((int) (x))
  562. #endif
  563. #define SIGN32(x) (SEXT32 (x) >> 31)
  564. /* convert pointer from target to host value. */
  565. #define PT2H(x) ((x) + memory)
  566. /* convert pointer from host to target value. */
  567. #define PH2T(x) ((x) - memory)
  568. #define SKIP_INSN(p) ((p) += ((RIAT (p) & 0xfc00) == 0xf800 ? 4 : 2))
  569. #define SET_NIP(x) nip = (x); CHECK_INSN_PTR (nip);
  570. static int in_delay_slot = 0;
  571. #define Delay_Slot(TEMPPC) iword = RIAT (TEMPPC); in_delay_slot = 1; goto top;
  572. #define CHECK_INSN_PTR(p) \
  573. do { \
  574. if (saved_state.asregs.exception || PH2T (p) & maskw) \
  575. saved_state.asregs.insn_end = 0; \
  576. else if (p < loop.end) \
  577. saved_state.asregs.insn_end = loop.end; \
  578. else \
  579. saved_state.asregs.insn_end = mem_end; \
  580. } while (0)
  581. #ifdef ACE_FAST
  582. #define MA(n)
  583. #define L(x)
  584. #define TL(x)
  585. #define TB(x)
  586. #else
  587. #define MA(n) \
  588. do { memstalls += ((((uintptr_t) PC & 3) != 0) ? (n) : ((n) - 1)); } while (0)
  589. #define L(x) thislock = x;
  590. #define TL(x) if ((x) == prevlock) stalls++;
  591. #define TB(x,y) if ((x) == prevlock || (y) == prevlock) stalls++;
  592. #endif
  593. #if defined(__GO32__)
  594. int sim_memory_size = 19;
  595. #else
  596. int sim_memory_size = 30;
  597. #endif
  598. static int sim_profile_size = 17;
  599. static int nsamples;
  600. #undef TB
  601. #define TB(x,y)
  602. #define SMR1 (0x05FFFEC8) /* Channel 1 serial mode register */
  603. #define BRR1 (0x05FFFEC9) /* Channel 1 bit rate register */
  604. #define SCR1 (0x05FFFECA) /* Channel 1 serial control register */
  605. #define TDR1 (0x05FFFECB) /* Channel 1 transmit data register */
  606. #define SSR1 (0x05FFFECC) /* Channel 1 serial status register */
  607. #define RDR1 (0x05FFFECD) /* Channel 1 receive data register */
  608. #define SCI_RDRF 0x40 /* Recieve data register full */
  609. #define SCI_TDRE 0x80 /* Transmit data register empty */
  610. static int
  611. IOMEM (int addr, int write, int value)
  612. {
  613. if (write)
  614. {
  615. switch (addr)
  616. {
  617. case TDR1:
  618. if (value != '\r')
  619. {
  620. putchar (value);
  621. fflush (stdout);
  622. }
  623. break;
  624. }
  625. }
  626. else
  627. {
  628. switch (addr)
  629. {
  630. case RDR1:
  631. return getchar ();
  632. }
  633. }
  634. return 0;
  635. }
  636. static int
  637. get_now (void)
  638. {
  639. return time (NULL);
  640. }
  641. static int
  642. now_persec (void)
  643. {
  644. return 1;
  645. }
  646. static FILE *profile_file;
  647. static INLINE unsigned
  648. swap (unsigned n)
  649. {
  650. if (endianb)
  651. n = (n << 24 | (n & 0xff00) << 8
  652. | (n & 0xff0000) >> 8 | (n & 0xff000000) >> 24);
  653. return n;
  654. }
  655. static INLINE unsigned short
  656. swap16 (unsigned short n)
  657. {
  658. if (endianb)
  659. n = n << 8 | (n & 0xff00) >> 8;
  660. return n;
  661. }
  662. static void
  663. swapout (int n)
  664. {
  665. if (profile_file)
  666. {
  667. union { char b[4]; int n; } u;
  668. u.n = swap (n);
  669. fwrite (u.b, 4, 1, profile_file);
  670. }
  671. }
  672. static void
  673. swapout16 (int n)
  674. {
  675. union { char b[4]; int n; } u;
  676. u.n = swap16 (n);
  677. fwrite (u.b, 2, 1, profile_file);
  678. }
  679. /* Turn a pointer in a register into a pointer into real memory. */
  680. static char *
  681. ptr (int x)
  682. {
  683. return (char *) (x + saved_state.asregs.memory);
  684. }
  685. /* STR points to a zero-terminated string in target byte order. Return
  686. the number of bytes that need to be converted to host byte order in order
  687. to use this string as a zero-terminated string on the host.
  688. (Not counting the rounding up needed to operate on entire words.) */
  689. static int
  690. strswaplen (int str)
  691. {
  692. unsigned char *memory = saved_state.asregs.memory;
  693. int start, end;
  694. int endian = endianb;
  695. if (! endian)
  696. return 0;
  697. end = str;
  698. for (end = str; memory[end ^ endian]; end++) ;
  699. return end - str + 1;
  700. }
  701. static void
  702. strnswap (int str, int len)
  703. {
  704. int *start, *end;
  705. if (! endianb || ! len)
  706. return;
  707. start = (int *) ptr (str & ~3);
  708. end = (int *) ptr (str + len);
  709. do
  710. {
  711. int old = *start;
  712. *start = (old << 24 | (old & 0xff00) << 8
  713. | (old & 0xff0000) >> 8 | (old & 0xff000000) >> 24);
  714. start++;
  715. }
  716. while (start < end);
  717. }
  718. /* Simulate a monitor trap, put the result into r0 and errno into r1
  719. return offset by which to adjust pc. */
  720. static int
  721. trap (SIM_DESC sd, int i, int *regs, unsigned char *insn_ptr,
  722. unsigned char *memory, int maskl, int maskw, int endianw)
  723. {
  724. host_callback *callback = STATE_CALLBACK (sd);
  725. char **prog_argv = STATE_PROG_ARGV (sd);
  726. switch (i)
  727. {
  728. case 1:
  729. printf ("%c", regs[0]);
  730. break;
  731. case 2:
  732. raise_exception (SIGQUIT);
  733. break;
  734. case 3: /* FIXME: for backwards compat, should be removed */
  735. case 33:
  736. {
  737. unsigned int countp = * (unsigned int *) (insn_ptr + 4);
  738. WLAT (countp, RLAT (countp) + 1);
  739. return 6;
  740. }
  741. case 34:
  742. {
  743. int perrno = errno;
  744. errno = 0;
  745. switch (regs[4])
  746. {
  747. #if !defined(__GO32__) && !defined(_WIN32)
  748. case TARGET_NEWLIB_SH_SYS_fork:
  749. regs[0] = fork ();
  750. break;
  751. /* This would work only if endianness matched between host and target.
  752. Besides, it's quite dangerous. */
  753. #if 0
  754. case TARGET_NEWLIB_SH_SYS_execve:
  755. regs[0] = execve (ptr (regs[5]), (char **) ptr (regs[6]),
  756. (char **) ptr (regs[7]));
  757. break;
  758. case TARGET_NEWLIB_SH_SYS_execv:
  759. regs[0] = execve (ptr (regs[5]), (char **) ptr (regs[6]), 0);
  760. break;
  761. #endif
  762. case TARGET_NEWLIB_SH_SYS_pipe:
  763. {
  764. regs[0] = (BUSERROR (regs[5], maskl)
  765. ? -EINVAL
  766. : pipe ((int *) ptr (regs[5])));
  767. }
  768. break;
  769. case TARGET_NEWLIB_SH_SYS_wait:
  770. regs[0] = wait ((int *) ptr (regs[5]));
  771. break;
  772. #endif /* !defined(__GO32__) && !defined(_WIN32) */
  773. case TARGET_NEWLIB_SH_SYS_read:
  774. strnswap (regs[6], regs[7]);
  775. regs[0]
  776. = callback->read (callback, regs[5], ptr (regs[6]), regs[7]);
  777. strnswap (regs[6], regs[7]);
  778. break;
  779. case TARGET_NEWLIB_SH_SYS_write:
  780. strnswap (regs[6], regs[7]);
  781. if (regs[5] == 1)
  782. regs[0] = (int) callback->write_stdout (callback,
  783. ptr (regs[6]), regs[7]);
  784. else
  785. regs[0] = (int) callback->write (callback, regs[5],
  786. ptr (regs[6]), regs[7]);
  787. strnswap (regs[6], regs[7]);
  788. break;
  789. case TARGET_NEWLIB_SH_SYS_lseek:
  790. regs[0] = callback->lseek (callback,regs[5], regs[6], regs[7]);
  791. break;
  792. case TARGET_NEWLIB_SH_SYS_close:
  793. regs[0] = callback->close (callback,regs[5]);
  794. break;
  795. case TARGET_NEWLIB_SH_SYS_open:
  796. {
  797. int len = strswaplen (regs[5]);
  798. strnswap (regs[5], len);
  799. regs[0] = callback->open (callback, ptr (regs[5]), regs[6]);
  800. strnswap (regs[5], len);
  801. break;
  802. }
  803. case TARGET_NEWLIB_SH_SYS_exit:
  804. /* EXIT - caller can look in r5 to work out the reason */
  805. raise_exception (SIGQUIT);
  806. regs[0] = regs[5];
  807. break;
  808. case TARGET_NEWLIB_SH_SYS_stat: /* added at hmsi */
  809. /* stat system call */
  810. {
  811. struct stat host_stat;
  812. int buf;
  813. int len = strswaplen (regs[5]);
  814. strnswap (regs[5], len);
  815. regs[0] = stat (ptr (regs[5]), &host_stat);
  816. strnswap (regs[5], len);
  817. buf = regs[6];
  818. WWAT (buf, host_stat.st_dev);
  819. buf += 2;
  820. WWAT (buf, host_stat.st_ino);
  821. buf += 2;
  822. WLAT (buf, host_stat.st_mode);
  823. buf += 4;
  824. WWAT (buf, host_stat.st_nlink);
  825. buf += 2;
  826. WWAT (buf, host_stat.st_uid);
  827. buf += 2;
  828. WWAT (buf, host_stat.st_gid);
  829. buf += 2;
  830. WWAT (buf, host_stat.st_rdev);
  831. buf += 2;
  832. WLAT (buf, host_stat.st_size);
  833. buf += 4;
  834. WLAT (buf, host_stat.st_atime);
  835. buf += 4;
  836. WLAT (buf, 0);
  837. buf += 4;
  838. WLAT (buf, host_stat.st_mtime);
  839. buf += 4;
  840. WLAT (buf, 0);
  841. buf += 4;
  842. WLAT (buf, host_stat.st_ctime);
  843. buf += 4;
  844. WLAT (buf, 0);
  845. buf += 4;
  846. WLAT (buf, 0);
  847. buf += 4;
  848. WLAT (buf, 0);
  849. buf += 4;
  850. }
  851. break;
  852. #ifndef _WIN32
  853. case TARGET_NEWLIB_SH_SYS_chown:
  854. {
  855. int len = strswaplen (regs[5]);
  856. strnswap (regs[5], len);
  857. regs[0] = chown (ptr (regs[5]), regs[6], regs[7]);
  858. strnswap (regs[5], len);
  859. break;
  860. }
  861. #endif /* _WIN32 */
  862. case TARGET_NEWLIB_SH_SYS_chmod:
  863. {
  864. int len = strswaplen (regs[5]);
  865. strnswap (regs[5], len);
  866. regs[0] = chmod (ptr (regs[5]), regs[6]);
  867. strnswap (regs[5], len);
  868. break;
  869. }
  870. case TARGET_NEWLIB_SH_SYS_utime:
  871. {
  872. /* Cast the second argument to void *, to avoid type mismatch
  873. if a prototype is present. */
  874. int len = strswaplen (regs[5]);
  875. strnswap (regs[5], len);
  876. #ifdef HAVE_UTIME_H
  877. regs[0] = utime (ptr (regs[5]), (void *) ptr (regs[6]));
  878. #else
  879. errno = ENOSYS;
  880. regs[0] = -1;
  881. #endif
  882. strnswap (regs[5], len);
  883. break;
  884. }
  885. case TARGET_NEWLIB_SH_SYS_argc:
  886. regs[0] = countargv (prog_argv);
  887. break;
  888. case TARGET_NEWLIB_SH_SYS_argnlen:
  889. if (regs[5] < countargv (prog_argv))
  890. regs[0] = strlen (prog_argv[regs[5]]);
  891. else
  892. regs[0] = -1;
  893. break;
  894. case TARGET_NEWLIB_SH_SYS_argn:
  895. if (regs[5] < countargv (prog_argv))
  896. {
  897. /* Include the termination byte. */
  898. int i = strlen (prog_argv[regs[5]]) + 1;
  899. regs[0] = sim_write (0, regs[6], (void *) prog_argv[regs[5]], i);
  900. }
  901. else
  902. regs[0] = -1;
  903. break;
  904. case TARGET_NEWLIB_SH_SYS_time:
  905. regs[0] = get_now ();
  906. break;
  907. case TARGET_NEWLIB_SH_SYS_ftruncate:
  908. regs[0] = callback->ftruncate (callback, regs[5], regs[6]);
  909. break;
  910. case TARGET_NEWLIB_SH_SYS_truncate:
  911. {
  912. int len = strswaplen (regs[5]);
  913. strnswap (regs[5], len);
  914. regs[0] = callback->truncate (callback, ptr (regs[5]), regs[6]);
  915. strnswap (regs[5], len);
  916. break;
  917. }
  918. default:
  919. regs[0] = -1;
  920. break;
  921. }
  922. regs[1] = callback->get_errno (callback);
  923. errno = perrno;
  924. }
  925. break;
  926. case 13: /* Set IBNR */
  927. IBNR = regs[0] & 0xffff;
  928. break;
  929. case 14: /* Set IBCR */
  930. IBCR = regs[0] & 0xffff;
  931. break;
  932. case 0xc3:
  933. case 255:
  934. raise_exception (SIGTRAP);
  935. if (i == 0xc3)
  936. return -2;
  937. break;
  938. }
  939. return 0;
  940. }
  941. static void
  942. div1 (int *R, int iRn2, int iRn1/*, int T*/)
  943. {
  944. unsigned long tmp0;
  945. unsigned char old_q, tmp1;
  946. old_q = Q;
  947. SET_SR_Q ((unsigned char) ((0x80000000 & R[iRn1]) != 0));
  948. R[iRn1] <<= 1;
  949. R[iRn1] |= (unsigned long) T;
  950. if (!old_q)
  951. {
  952. if (!M)
  953. {
  954. tmp0 = R[iRn1];
  955. R[iRn1] -= R[iRn2];
  956. tmp1 = (R[iRn1] > tmp0);
  957. if (!Q)
  958. SET_SR_Q (tmp1);
  959. else
  960. SET_SR_Q ((unsigned char) (tmp1 == 0));
  961. }
  962. else
  963. {
  964. tmp0 = R[iRn1];
  965. R[iRn1] += R[iRn2];
  966. tmp1 = (R[iRn1] < tmp0);
  967. if (!Q)
  968. SET_SR_Q ((unsigned char) (tmp1 == 0));
  969. else
  970. SET_SR_Q (tmp1);
  971. }
  972. }
  973. else
  974. {
  975. if (!M)
  976. {
  977. tmp0 = R[iRn1];
  978. R[iRn1] += R[iRn2];
  979. tmp1 = (R[iRn1] < tmp0);
  980. if (!Q)
  981. SET_SR_Q (tmp1);
  982. else
  983. SET_SR_Q ((unsigned char) (tmp1 == 0));
  984. }
  985. else
  986. {
  987. tmp0 = R[iRn1];
  988. R[iRn1] -= R[iRn2];
  989. tmp1 = (R[iRn1] > tmp0);
  990. if (!Q)
  991. SET_SR_Q ((unsigned char) (tmp1 == 0));
  992. else
  993. SET_SR_Q (tmp1);
  994. }
  995. }
  996. /*T = (Q == M);*/
  997. SET_SR_T (Q == M);
  998. /*return T;*/
  999. }
  1000. static void
  1001. dmul_s (uint32_t rm, uint32_t rn)
  1002. {
  1003. int64_t res = (int64_t)(int32_t)rm * (int64_t)(int32_t)rn;
  1004. MACH = (uint32_t)((uint64_t)res >> 32);
  1005. MACL = (uint32_t)res;
  1006. }
  1007. static void
  1008. dmul_u (uint32_t rm, uint32_t rn)
  1009. {
  1010. uint64_t res = (uint64_t)(uint32_t)rm * (uint64_t)(uint32_t)rn;
  1011. MACH = (uint32_t)(res >> 32);
  1012. MACL = (uint32_t)res;
  1013. }
  1014. static void
  1015. macw (int *regs, unsigned char *memory, int n, int m, int endianw)
  1016. {
  1017. long tempm, tempn;
  1018. long prod, macl, sum;
  1019. tempm=RSWAT (regs[m]); regs[m]+=2;
  1020. tempn=RSWAT (regs[n]); regs[n]+=2;
  1021. macl = MACL;
  1022. prod = (long) (short) tempm * (long) (short) tempn;
  1023. sum = prod + macl;
  1024. if (S)
  1025. {
  1026. if ((~(prod ^ macl) & (sum ^ prod)) < 0)
  1027. {
  1028. /* MACH's lsb is a sticky overflow bit. */
  1029. MACH |= 1;
  1030. /* Store the smallest negative number in MACL if prod is
  1031. negative, and the largest positive number otherwise. */
  1032. sum = 0x7fffffff + (prod < 0);
  1033. }
  1034. }
  1035. else
  1036. {
  1037. long mach;
  1038. /* Add to MACH the sign extended product, and carry from low sum. */
  1039. mach = MACH + (-(prod < 0)) + ((unsigned long) sum < prod);
  1040. /* Sign extend at 10:th bit in MACH. */
  1041. MACH = (mach & 0x1ff) | -(mach & 0x200);
  1042. }
  1043. MACL = sum;
  1044. }
  1045. static void
  1046. macl (int *regs, unsigned char *memory, int n, int m)
  1047. {
  1048. long tempm, tempn;
  1049. long macl, mach;
  1050. long long ans;
  1051. long long mac64;
  1052. tempm = RSLAT (regs[m]);
  1053. regs[m] += 4;
  1054. tempn = RSLAT (regs[n]);
  1055. regs[n] += 4;
  1056. mach = MACH;
  1057. macl = MACL;
  1058. mac64 = ((long long) macl & 0xffffffff) |
  1059. ((long long) mach & 0xffffffff) << 32;
  1060. ans = (long long) tempm * (long long) tempn; /* Multiply 32bit * 32bit */
  1061. mac64 += ans; /* Accumulate 64bit + 64 bit */
  1062. macl = (long) (mac64 & 0xffffffff);
  1063. mach = (long) ((mac64 >> 32) & 0xffffffff);
  1064. if (S) /* Store only 48 bits of the result */
  1065. {
  1066. if (mach < 0) /* Result is negative */
  1067. {
  1068. mach = mach & 0x0000ffff; /* Mask higher 16 bits */
  1069. mach |= 0xffff8000; /* Sign extend higher 16 bits */
  1070. }
  1071. else
  1072. mach = mach & 0x00007fff; /* Postive Result */
  1073. }
  1074. MACL = macl;
  1075. MACH = mach;
  1076. }
  1077. enum {
  1078. B_BCLR = 0,
  1079. B_BSET = 1,
  1080. B_BST = 2,
  1081. B_BLD = 3,
  1082. B_BAND = 4,
  1083. B_BOR = 5,
  1084. B_BXOR = 6,
  1085. B_BLDNOT = 11,
  1086. B_BANDNOT = 12,
  1087. B_BORNOT = 13,
  1088. MOVB_RM = 0x0000,
  1089. MOVW_RM = 0x1000,
  1090. MOVL_RM = 0x2000,
  1091. FMOV_RM = 0x3000,
  1092. MOVB_MR = 0x4000,
  1093. MOVW_MR = 0x5000,
  1094. MOVL_MR = 0x6000,
  1095. FMOV_MR = 0x7000,
  1096. MOVU_BMR = 0x8000,
  1097. MOVU_WMR = 0x9000,
  1098. };
  1099. /* Do extended displacement move instructions. */
  1100. static void
  1101. do_long_move_insn (int op, int disp12, int m, int n, int *thatlock)
  1102. {
  1103. int memstalls = 0;
  1104. int thislock = *thatlock;
  1105. int endianw = global_endianw;
  1106. int *R = &(saved_state.asregs.regs[0]);
  1107. unsigned char *memory = saved_state.asregs.memory;
  1108. int maskb = ~((saved_state.asregs.msize - 1) & ~0);
  1109. unsigned char *insn_ptr = PT2H (saved_state.asregs.pc);
  1110. switch (op) {
  1111. case MOVB_RM: /* signed */
  1112. WBAT (disp12 * 1 + R[n], R[m]);
  1113. break;
  1114. case MOVW_RM:
  1115. WWAT (disp12 * 2 + R[n], R[m]);
  1116. break;
  1117. case MOVL_RM:
  1118. WLAT (disp12 * 4 + R[n], R[m]);
  1119. break;
  1120. case FMOV_RM: /* floating point */
  1121. if (FPSCR_SZ)
  1122. {
  1123. MA (1);
  1124. WDAT (R[n] + 8 * disp12, m);
  1125. }
  1126. else
  1127. WLAT (R[n] + 4 * disp12, FI (m));
  1128. break;
  1129. case MOVB_MR:
  1130. R[n] = RSBAT (disp12 * 1 + R[m]);
  1131. L (n);
  1132. break;
  1133. case MOVW_MR:
  1134. R[n] = RSWAT (disp12 * 2 + R[m]);
  1135. L (n);
  1136. break;
  1137. case MOVL_MR:
  1138. R[n] = RLAT (disp12 * 4 + R[m]);
  1139. L (n);
  1140. break;
  1141. case FMOV_MR:
  1142. if (FPSCR_SZ) {
  1143. MA (1);
  1144. RDAT (R[m] + 8 * disp12, n);
  1145. }
  1146. else
  1147. SET_FI (n, RLAT (R[m] + 4 * disp12));
  1148. break;
  1149. case MOVU_BMR: /* unsigned */
  1150. R[n] = RBAT (disp12 * 1 + R[m]);
  1151. L (n);
  1152. break;
  1153. case MOVU_WMR:
  1154. R[n] = RWAT (disp12 * 2 + R[m]);
  1155. L (n);
  1156. break;
  1157. default:
  1158. RAISE_EXCEPTION (SIGINT);
  1159. exit (1);
  1160. }
  1161. saved_state.asregs.memstalls += memstalls;
  1162. *thatlock = thislock;
  1163. }
  1164. /* Do binary logical bit-manipulation insns. */
  1165. static void
  1166. do_blog_insn (int imm, int addr, int binop,
  1167. unsigned char *memory, int maskb)
  1168. {
  1169. int oldval = RBAT (addr);
  1170. switch (binop) {
  1171. case B_BCLR: /* bclr.b */
  1172. WBAT (addr, oldval & ~imm);
  1173. break;
  1174. case B_BSET: /* bset.b */
  1175. WBAT (addr, oldval | imm);
  1176. break;
  1177. case B_BST: /* bst.b */
  1178. if (T)
  1179. WBAT (addr, oldval | imm);
  1180. else
  1181. WBAT (addr, oldval & ~imm);
  1182. break;
  1183. case B_BLD: /* bld.b */
  1184. SET_SR_T ((oldval & imm) != 0);
  1185. break;
  1186. case B_BAND: /* band.b */
  1187. SET_SR_T (T && ((oldval & imm) != 0));
  1188. break;
  1189. case B_BOR: /* bor.b */
  1190. SET_SR_T (T || ((oldval & imm) != 0));
  1191. break;
  1192. case B_BXOR: /* bxor.b */
  1193. SET_SR_T (T ^ ((oldval & imm) != 0));
  1194. break;
  1195. case B_BLDNOT: /* bldnot.b */
  1196. SET_SR_T ((oldval & imm) == 0);
  1197. break;
  1198. case B_BANDNOT: /* bandnot.b */
  1199. SET_SR_T (T && ((oldval & imm) == 0));
  1200. break;
  1201. case B_BORNOT: /* bornot.b */
  1202. SET_SR_T (T || ((oldval & imm) == 0));
  1203. break;
  1204. }
  1205. }
  1206. static float
  1207. fsca_s (int in, double (*f) (double))
  1208. {
  1209. double rad = ldexp ((in & 0xffff), -15) * 3.141592653589793238462643383;
  1210. double result = (*f) (rad);
  1211. double error, upper, lower, frac;
  1212. int exp;
  1213. /* Search the value with the maximum error that is still within the
  1214. architectural spec. */
  1215. error = ldexp (1., -21);
  1216. /* compensate for calculation inaccuracy by reducing error. */
  1217. error = error - ldexp (1., -50);
  1218. upper = result + error;
  1219. frac = frexp (upper, &exp);
  1220. upper = ldexp (floor (ldexp (frac, 24)), exp - 24);
  1221. lower = result - error;
  1222. frac = frexp (lower, &exp);
  1223. lower = ldexp (ceil (ldexp (frac, 24)), exp - 24);
  1224. return abs (upper - result) >= abs (lower - result) ? upper : lower;
  1225. }
  1226. static float
  1227. fsrra_s (float in)
  1228. {
  1229. double result = 1. / sqrt (in);
  1230. int exp;
  1231. double frac, upper, lower, error, eps;
  1232. /* refine result */
  1233. result = result - (result * result * in - 1) * 0.5 * result;
  1234. /* Search the value with the maximum error that is still within the
  1235. architectural spec. */
  1236. frac = frexp (result, &exp);
  1237. frac = ldexp (frac, 24);
  1238. error = 4.0; /* 1 << 24-1-21 */
  1239. /* use eps to compensate for possible 1 ulp error in our 'exact' result. */
  1240. eps = ldexp (1., -29);
  1241. upper = floor (frac + error - eps);
  1242. if (upper > 16777216.)
  1243. upper = floor ((frac + error - eps) * 0.5) * 2.;
  1244. lower = ceil ((frac - error + eps) * 2) * .5;
  1245. if (lower > 8388608.)
  1246. lower = ceil (frac - error + eps);
  1247. upper = ldexp (upper, exp - 24);
  1248. lower = ldexp (lower, exp - 24);
  1249. return upper - result >= result - lower ? upper : lower;
  1250. }
  1251. /* GET_LOOP_BOUNDS {EXTENDED}
  1252. These two functions compute the actual starting and ending point
  1253. of the repeat loop, based on the RS and RE registers (repeat start,
  1254. repeat stop). The extended version is called for LDRC, and the
  1255. regular version is called for SETRC. The difference is that for
  1256. LDRC, the loop start and end instructions are literally the ones
  1257. pointed to by RS and RE -- for SETRC, they're not (see docs). */
  1258. static struct loop_bounds
  1259. get_loop_bounds_ext (int rs, int re, unsigned char *memory,
  1260. unsigned char *mem_end, int maskw, int endianw)
  1261. {
  1262. struct loop_bounds loop;
  1263. /* FIXME: should I verify RS < RE? */
  1264. loop.start = PT2H (RS); /* FIXME not using the params? */
  1265. loop.end = PT2H (RE & ~1); /* Ignore bit 0 of RE. */
  1266. SKIP_INSN (loop.end);
  1267. if (loop.end >= mem_end)
  1268. loop.end = PT2H (0);
  1269. return loop;
  1270. }
  1271. static struct loop_bounds
  1272. get_loop_bounds (int rs, int re, unsigned char *memory, unsigned char *mem_end,
  1273. int maskw, int endianw)
  1274. {
  1275. struct loop_bounds loop;
  1276. if (SR_RC)
  1277. {
  1278. if (RS >= RE)
  1279. {
  1280. loop.start = PT2H (RE - 4);
  1281. SKIP_INSN (loop.start);
  1282. loop.end = loop.start;
  1283. if (RS - RE == 0)
  1284. SKIP_INSN (loop.end);
  1285. if (RS - RE <= 2)
  1286. SKIP_INSN (loop.end);
  1287. SKIP_INSN (loop.end);
  1288. }
  1289. else
  1290. {
  1291. loop.start = PT2H (RS);
  1292. loop.end = PT2H (RE - 4);
  1293. SKIP_INSN (loop.end);
  1294. SKIP_INSN (loop.end);
  1295. SKIP_INSN (loop.end);
  1296. SKIP_INSN (loop.end);
  1297. }
  1298. if (loop.end >= mem_end)
  1299. loop.end = PT2H (0);
  1300. }
  1301. else
  1302. loop.end = PT2H (0);
  1303. return loop;
  1304. }
  1305. static void ppi_insn ();
  1306. #include "ppi.c"
  1307. /* Provide calloc / free versions that use an anonymous mmap. This can
  1308. significantly cut the start-up time when a large simulator memory is
  1309. required, because pages are only zeroed on demand. */
  1310. #ifdef MAP_ANONYMOUS
  1311. static void *
  1312. mcalloc (size_t nmemb, size_t size)
  1313. {
  1314. void *page;
  1315. if (nmemb != 1)
  1316. size *= nmemb;
  1317. return mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
  1318. -1, 0);
  1319. }
  1320. #define mfree(start,length) munmap ((start), (length))
  1321. #else
  1322. #define mcalloc calloc
  1323. #define mfree(start,length) free(start)
  1324. #endif
  1325. /* Set the memory size to the power of two provided. */
  1326. static void
  1327. sim_size (int power)
  1328. {
  1329. sim_memory_size = power;
  1330. if (saved_state.asregs.memory)
  1331. {
  1332. mfree (saved_state.asregs.memory, saved_state.asregs.msize);
  1333. }
  1334. saved_state.asregs.msize = 1 << power;
  1335. saved_state.asregs.memory =
  1336. (unsigned char *) mcalloc (1, saved_state.asregs.msize);
  1337. if (!saved_state.asregs.memory)
  1338. {
  1339. fprintf (stderr,
  1340. "Not enough VM for simulation of %d bytes of RAM\n",
  1341. saved_state.asregs.msize);
  1342. saved_state.asregs.msize = 1;
  1343. saved_state.asregs.memory = (unsigned char *) mcalloc (1, 1);
  1344. }
  1345. }
  1346. static void
  1347. init_dsp (struct bfd *abfd)
  1348. {
  1349. int was_dsp = target_dsp;
  1350. unsigned long mach = bfd_get_mach (abfd);
  1351. if (mach == bfd_mach_sh_dsp ||
  1352. mach == bfd_mach_sh4al_dsp ||
  1353. mach == bfd_mach_sh3_dsp)
  1354. {
  1355. int ram_area_size, xram_start, yram_start;
  1356. int new_select;
  1357. target_dsp = 1;
  1358. if (mach == bfd_mach_sh_dsp)
  1359. {
  1360. /* SH7410 (orig. sh-sdp):
  1361. 4KB each for X & Y memory;
  1362. On-chip X RAM 0x0800f000-0x0800ffff
  1363. On-chip Y RAM 0x0801f000-0x0801ffff */
  1364. xram_start = 0x0800f000;
  1365. ram_area_size = 0x1000;
  1366. }
  1367. if (mach == bfd_mach_sh3_dsp || mach == bfd_mach_sh4al_dsp)
  1368. {
  1369. /* SH7612:
  1370. 8KB each for X & Y memory;
  1371. On-chip X RAM 0x1000e000-0x1000ffff
  1372. On-chip Y RAM 0x1001e000-0x1001ffff */
  1373. xram_start = 0x1000e000;
  1374. ram_area_size = 0x2000;
  1375. }
  1376. yram_start = xram_start + 0x10000;
  1377. new_select = ~(ram_area_size - 1);
  1378. if (saved_state.asregs.xyram_select != new_select)
  1379. {
  1380. saved_state.asregs.xyram_select = new_select;
  1381. free (saved_state.asregs.xmem);
  1382. free (saved_state.asregs.ymem);
  1383. saved_state.asregs.xmem =
  1384. (unsigned char *) calloc (1, ram_area_size);
  1385. saved_state.asregs.ymem =
  1386. (unsigned char *) calloc (1, ram_area_size);
  1387. /* Disable use of X / Y mmeory if not allocated. */
  1388. if (! saved_state.asregs.xmem || ! saved_state.asregs.ymem)
  1389. {
  1390. saved_state.asregs.xyram_select = 0;
  1391. if (saved_state.asregs.xmem)
  1392. free (saved_state.asregs.xmem);
  1393. if (saved_state.asregs.ymem)
  1394. free (saved_state.asregs.ymem);
  1395. }
  1396. }
  1397. saved_state.asregs.xram_start = xram_start;
  1398. saved_state.asregs.yram_start = yram_start;
  1399. saved_state.asregs.xmem_offset = saved_state.asregs.xmem - xram_start;
  1400. saved_state.asregs.ymem_offset = saved_state.asregs.ymem - yram_start;
  1401. }
  1402. else
  1403. {
  1404. target_dsp = 0;
  1405. if (saved_state.asregs.xyram_select)
  1406. {
  1407. saved_state.asregs.xyram_select = 0;
  1408. free (saved_state.asregs.xmem);
  1409. free (saved_state.asregs.ymem);
  1410. }
  1411. }
  1412. if (! saved_state.asregs.xyram_select)
  1413. {
  1414. saved_state.asregs.xram_start = 1;
  1415. saved_state.asregs.yram_start = 1;
  1416. }
  1417. if (saved_state.asregs.regstack == NULL)
  1418. saved_state.asregs.regstack =
  1419. calloc (512, sizeof *saved_state.asregs.regstack);
  1420. if (target_dsp != was_dsp)
  1421. {
  1422. int i, tmp;
  1423. for (i = ARRAY_SIZE (sh_dsp_table) - 1; i >= 0; i--)
  1424. {
  1425. tmp = sh_jump_table[0xf000 + i];
  1426. sh_jump_table[0xf000 + i] = sh_dsp_table[i];
  1427. sh_dsp_table[i] = tmp;
  1428. }
  1429. }
  1430. }
  1431. static void
  1432. init_pointers (void)
  1433. {
  1434. if (saved_state.asregs.msize != 1 << sim_memory_size)
  1435. {
  1436. sim_size (sim_memory_size);
  1437. }
  1438. if (saved_state.asregs.profile && !profile_file)
  1439. {
  1440. profile_file = fopen ("gmon.out", "wb");
  1441. /* Seek to where to put the call arc data */
  1442. nsamples = (1 << sim_profile_size);
  1443. fseek (profile_file, nsamples * 2 + 12, 0);
  1444. if (!profile_file)
  1445. {
  1446. fprintf (stderr, "Can't open gmon.out\n");
  1447. }
  1448. else
  1449. {
  1450. saved_state.asregs.profile_hist =
  1451. (unsigned short *) calloc (64, (nsamples * sizeof (short) / 64));
  1452. }
  1453. }
  1454. }
  1455. static void
  1456. dump_profile (void)
  1457. {
  1458. unsigned int minpc;
  1459. unsigned int maxpc;
  1460. unsigned short *p;
  1461. int i;
  1462. p = saved_state.asregs.profile_hist;
  1463. minpc = 0;
  1464. maxpc = (1 << sim_profile_size);
  1465. fseek (profile_file, 0L, 0);
  1466. swapout (minpc << PROFILE_SHIFT);
  1467. swapout (maxpc << PROFILE_SHIFT);
  1468. swapout (nsamples * 2 + 12);
  1469. for (i = 0; i < nsamples; i++)
  1470. swapout16 (saved_state.asregs.profile_hist[i]);
  1471. }
  1472. static void
  1473. gotcall (int from, int to)
  1474. {
  1475. swapout (from);
  1476. swapout (to);
  1477. swapout (1);
  1478. }
  1479. #define MMASKB ((saved_state.asregs.msize -1) & ~0)
  1480. void
  1481. sim_resume (SIM_DESC sd, int step, int siggnal)
  1482. {
  1483. register unsigned char *insn_ptr;
  1484. unsigned char *mem_end;
  1485. struct loop_bounds loop;
  1486. register int cycles = 0;
  1487. register int stalls = 0;
  1488. register int memstalls = 0;
  1489. register int insts = 0;
  1490. register int prevlock;
  1491. #if 1
  1492. int thislock;
  1493. #else
  1494. register int thislock;
  1495. #endif
  1496. register unsigned int doprofile;
  1497. register int pollcount = 0;
  1498. /* endianw is used for every insn fetch, hence it makes sense to cache it.
  1499. endianb is used less often. */
  1500. register int endianw = global_endianw;
  1501. int tick_start = get_now ();
  1502. void (*prev_fpe) ();
  1503. register unsigned short *jump_table = sh_jump_table;
  1504. register int *R = &(saved_state.asregs.regs[0]);
  1505. /*register int T;*/
  1506. #ifndef PR
  1507. register int PR;
  1508. #endif
  1509. register int maskb = ~((saved_state.asregs.msize - 1) & ~0);
  1510. register int maskw = ~((saved_state.asregs.msize - 1) & ~1);
  1511. register int maskl = ~((saved_state.asregs.msize - 1) & ~3);
  1512. register unsigned char *memory;
  1513. register unsigned int sbit = ((unsigned int) 1 << 31);
  1514. prev_fpe = signal (SIGFPE, SIG_IGN);
  1515. init_pointers ();
  1516. saved_state.asregs.exception = 0;
  1517. memory = saved_state.asregs.memory;
  1518. mem_end = memory + saved_state.asregs.msize;
  1519. if (RE & 1)
  1520. loop = get_loop_bounds_ext (RS, RE, memory, mem_end, maskw, endianw);
  1521. else
  1522. loop = get_loop_bounds (RS, RE, memory, mem_end, maskw, endianw);
  1523. insn_ptr = PT2H (saved_state.asregs.pc);
  1524. CHECK_INSN_PTR (insn_ptr);
  1525. #ifndef PR
  1526. PR = saved_state.asregs.pr;
  1527. #endif
  1528. /*T = GET_SR () & SR_MASK_T;*/
  1529. prevlock = saved_state.asregs.prevlock;
  1530. thislock = saved_state.asregs.thislock;
  1531. doprofile = saved_state.asregs.profile;
  1532. /* If profiling not enabled, disable it by asking for
  1533. profiles infrequently. */
  1534. if (doprofile == 0)
  1535. doprofile = ~0;
  1536. loop:
  1537. if (step && insn_ptr < saved_state.asregs.insn_end)
  1538. {
  1539. if (saved_state.asregs.exception)
  1540. /* This can happen if we've already been single-stepping and
  1541. encountered a loop end. */
  1542. saved_state.asregs.insn_end = insn_ptr;
  1543. else
  1544. {
  1545. saved_state.asregs.exception = SIGTRAP;
  1546. saved_state.asregs.insn_end = insn_ptr + 2;
  1547. }
  1548. }
  1549. while (insn_ptr < saved_state.asregs.insn_end)
  1550. {
  1551. register unsigned int iword = RIAT (insn_ptr);
  1552. register unsigned int ult;
  1553. register unsigned char *nip = insn_ptr + 2;
  1554. #ifndef ACE_FAST
  1555. insts++;
  1556. #endif
  1557. top:
  1558. #include "code.c"
  1559. in_delay_slot = 0;
  1560. insn_ptr = nip;
  1561. if (--pollcount < 0)
  1562. {
  1563. host_callback *callback = STATE_CALLBACK (sd);
  1564. pollcount = POLL_QUIT_INTERVAL;
  1565. if ((*callback->poll_quit) != NULL
  1566. && (*callback->poll_quit) (callback))
  1567. {
  1568. sim_stop (sd);
  1569. }
  1570. }
  1571. #ifndef ACE_FAST
  1572. prevlock = thislock;
  1573. thislock = 30;
  1574. cycles++;
  1575. if (cycles >= doprofile)
  1576. {
  1577. saved_state.asregs.cycles += doprofile;
  1578. cycles -= doprofile;
  1579. if (saved_state.asregs.profile_hist)
  1580. {
  1581. int n = PH2T (insn_ptr) >> PROFILE_SHIFT;
  1582. if (n < nsamples)
  1583. {
  1584. int i = saved_state.asregs.profile_hist[n];
  1585. if (i < 65000)
  1586. saved_state.asregs.profile_hist[n] = i + 1;
  1587. }
  1588. }
  1589. }
  1590. #endif
  1591. }
  1592. if (saved_state.asregs.insn_end == loop.end)
  1593. {
  1594. saved_state.asregs.sr += SR_RC_INCREMENT;
  1595. if (SR_RC)
  1596. insn_ptr = loop.start;
  1597. else
  1598. {
  1599. saved_state.asregs.insn_end = mem_end;
  1600. loop.end = PT2H (0);
  1601. }
  1602. goto loop;
  1603. }
  1604. if (saved_state.asregs.exception == SIGILL
  1605. || saved_state.asregs.exception == SIGBUS)
  1606. {
  1607. insn_ptr -= 2;
  1608. }
  1609. /* Check for SIGBUS due to insn fetch. */
  1610. else if (! saved_state.asregs.exception)
  1611. saved_state.asregs.exception = SIGBUS;
  1612. saved_state.asregs.ticks += get_now () - tick_start;
  1613. saved_state.asregs.cycles += cycles;
  1614. saved_state.asregs.stalls += stalls;
  1615. saved_state.asregs.memstalls += memstalls;
  1616. saved_state.asregs.insts += insts;
  1617. saved_state.asregs.pc = PH2T (insn_ptr);
  1618. #ifndef PR
  1619. saved_state.asregs.pr = PR;
  1620. #endif
  1621. saved_state.asregs.prevlock = prevlock;
  1622. saved_state.asregs.thislock = thislock;
  1623. if (profile_file)
  1624. {
  1625. dump_profile ();
  1626. }
  1627. signal (SIGFPE, prev_fpe);
  1628. }
  1629. int
  1630. sim_write (SIM_DESC sd, SIM_ADDR addr, const unsigned char *buffer, int size)
  1631. {
  1632. int i;
  1633. init_pointers ();
  1634. for (i = 0; i < size; i++)
  1635. {
  1636. saved_state.asregs.memory[(MMASKB & (addr + i)) ^ endianb] = buffer[i];
  1637. }
  1638. return size;
  1639. }
  1640. int
  1641. sim_read (SIM_DESC sd, SIM_ADDR addr, unsigned char *buffer, int size)
  1642. {
  1643. int i;
  1644. init_pointers ();
  1645. for (i = 0; i < size; i++)
  1646. {
  1647. buffer[i] = saved_state.asregs.memory[(MMASKB & (addr + i)) ^ endianb];
  1648. }
  1649. return size;
  1650. }
  1651. static int gdb_bank_number;
  1652. enum {
  1653. REGBANK_MACH = 15,
  1654. REGBANK_IVN = 16,
  1655. REGBANK_PR = 17,
  1656. REGBANK_GBR = 18,
  1657. REGBANK_MACL = 19
  1658. };
  1659. static int
  1660. sh_reg_store (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
  1661. {
  1662. unsigned val;
  1663. init_pointers ();
  1664. val = swap (* (int *) memory);
  1665. switch (rn)
  1666. {
  1667. case SIM_SH_R0_REGNUM: case SIM_SH_R1_REGNUM: case SIM_SH_R2_REGNUM:
  1668. case SIM_SH_R3_REGNUM: case SIM_SH_R4_REGNUM: case SIM_SH_R5_REGNUM:
  1669. case SIM_SH_R6_REGNUM: case SIM_SH_R7_REGNUM: case SIM_SH_R8_REGNUM:
  1670. case SIM_SH_R9_REGNUM: case SIM_SH_R10_REGNUM: case SIM_SH_R11_REGNUM:
  1671. case SIM_SH_R12_REGNUM: case SIM_SH_R13_REGNUM: case SIM_SH_R14_REGNUM:
  1672. case SIM_SH_R15_REGNUM:
  1673. saved_state.asregs.regs[rn] = val;
  1674. break;
  1675. case SIM_SH_PC_REGNUM:
  1676. saved_state.asregs.pc = val;
  1677. break;
  1678. case SIM_SH_PR_REGNUM:
  1679. PR = val;
  1680. break;
  1681. case SIM_SH_GBR_REGNUM:
  1682. GBR = val;
  1683. break;
  1684. case SIM_SH_VBR_REGNUM:
  1685. VBR = val;
  1686. break;
  1687. case SIM_SH_MACH_REGNUM:
  1688. MACH = val;
  1689. break;
  1690. case SIM_SH_MACL_REGNUM:
  1691. MACL = val;
  1692. break;
  1693. case SIM_SH_SR_REGNUM:
  1694. SET_SR (val);
  1695. break;
  1696. case SIM_SH_FPUL_REGNUM:
  1697. FPUL = val;
  1698. break;
  1699. case SIM_SH_FPSCR_REGNUM:
  1700. SET_FPSCR (val);
  1701. break;
  1702. case SIM_SH_FR0_REGNUM: case SIM_SH_FR1_REGNUM: case SIM_SH_FR2_REGNUM:
  1703. case SIM_SH_FR3_REGNUM: case SIM_SH_FR4_REGNUM: case SIM_SH_FR5_REGNUM:
  1704. case SIM_SH_FR6_REGNUM: case SIM_SH_FR7_REGNUM: case SIM_SH_FR8_REGNUM:
  1705. case SIM_SH_FR9_REGNUM: case SIM_SH_FR10_REGNUM: case SIM_SH_FR11_REGNUM:
  1706. case SIM_SH_FR12_REGNUM: case SIM_SH_FR13_REGNUM: case SIM_SH_FR14_REGNUM:
  1707. case SIM_SH_FR15_REGNUM:
  1708. SET_FI (rn - SIM_SH_FR0_REGNUM, val);
  1709. break;
  1710. case SIM_SH_DSR_REGNUM:
  1711. DSR = val;
  1712. break;
  1713. case SIM_SH_A0G_REGNUM:
  1714. A0G = val;
  1715. break;
  1716. case SIM_SH_A0_REGNUM:
  1717. A0 = val;
  1718. break;
  1719. case SIM_SH_A1G_REGNUM:
  1720. A1G = val;
  1721. break;
  1722. case SIM_SH_A1_REGNUM:
  1723. A1 = val;
  1724. break;
  1725. case SIM_SH_M0_REGNUM:
  1726. M0 = val;
  1727. break;
  1728. case SIM_SH_M1_REGNUM:
  1729. M1 = val;
  1730. break;
  1731. case SIM_SH_X0_REGNUM:
  1732. X0 = val;
  1733. break;
  1734. case SIM_SH_X1_REGNUM:
  1735. X1 = val;
  1736. break;
  1737. case SIM_SH_Y0_REGNUM:
  1738. Y0 = val;
  1739. break;
  1740. case SIM_SH_Y1_REGNUM:
  1741. Y1 = val;
  1742. break;
  1743. case SIM_SH_MOD_REGNUM:
  1744. SET_MOD (val);
  1745. break;
  1746. case SIM_SH_RS_REGNUM:
  1747. RS = val;
  1748. break;
  1749. case SIM_SH_RE_REGNUM:
  1750. RE = val;
  1751. break;
  1752. case SIM_SH_SSR_REGNUM:
  1753. SSR = val;
  1754. break;
  1755. case SIM_SH_SPC_REGNUM:
  1756. SPC = val;
  1757. break;
  1758. /* The rn_bank idiosyncracies are not due to hardware differences, but to
  1759. a weird aliasing naming scheme for sh3 / sh3e / sh4. */
  1760. case SIM_SH_R0_BANK0_REGNUM: case SIM_SH_R1_BANK0_REGNUM:
  1761. case SIM_SH_R2_BANK0_REGNUM: case SIM_SH_R3_BANK0_REGNUM:
  1762. case SIM_SH_R4_BANK0_REGNUM: case SIM_SH_R5_BANK0_REGNUM:
  1763. case SIM_SH_R6_BANK0_REGNUM: case SIM_SH_R7_BANK0_REGNUM:
  1764. if (saved_state.asregs.bfd_mach == bfd_mach_sh2a)
  1765. {
  1766. rn -= SIM_SH_R0_BANK0_REGNUM;
  1767. saved_state.asregs.regstack[gdb_bank_number].regs[rn] = val;
  1768. }
  1769. else
  1770. if (SR_MD && SR_RB)
  1771. Rn_BANK (rn - SIM_SH_R0_BANK0_REGNUM) = val;
  1772. else
  1773. saved_state.asregs.regs[rn - SIM_SH_R0_BANK0_REGNUM] = val;
  1774. break;
  1775. case SIM_SH_R0_BANK1_REGNUM: case SIM_SH_R1_BANK1_REGNUM:
  1776. case SIM_SH_R2_BANK1_REGNUM: case SIM_SH_R3_BANK1_REGNUM:
  1777. case SIM_SH_R4_BANK1_REGNUM: case SIM_SH_R5_BANK1_REGNUM:
  1778. case SIM_SH_R6_BANK1_REGNUM: case SIM_SH_R7_BANK1_REGNUM:
  1779. if (saved_state.asregs.bfd_mach == bfd_mach_sh2a)
  1780. {
  1781. rn -= SIM_SH_R0_BANK1_REGNUM;
  1782. saved_state.asregs.regstack[gdb_bank_number].regs[rn + 8] = val;
  1783. }
  1784. else
  1785. if (SR_MD && SR_RB)
  1786. saved_state.asregs.regs[rn - SIM_SH_R0_BANK1_REGNUM] = val;
  1787. else
  1788. Rn_BANK (rn - SIM_SH_R0_BANK1_REGNUM) = val;
  1789. break;
  1790. case SIM_SH_R0_BANK_REGNUM: case SIM_SH_R1_BANK_REGNUM:
  1791. case SIM_SH_R2_BANK_REGNUM: case SIM_SH_R3_BANK_REGNUM:
  1792. case SIM_SH_R4_BANK_REGNUM: case SIM_SH_R5_BANK_REGNUM:
  1793. case SIM_SH_R6_BANK_REGNUM: case SIM_SH_R7_BANK_REGNUM:
  1794. SET_Rn_BANK (rn - SIM_SH_R0_BANK_REGNUM, val);
  1795. break;
  1796. case SIM_SH_TBR_REGNUM:
  1797. TBR = val;
  1798. break;
  1799. case SIM_SH_IBNR_REGNUM:
  1800. IBNR = val;
  1801. break;
  1802. case SIM_SH_IBCR_REGNUM:
  1803. IBCR = val;
  1804. break;
  1805. case SIM_SH_BANK_REGNUM:
  1806. /* This is a pseudo-register maintained just for gdb.
  1807. It tells us what register bank gdb would like to read/write. */
  1808. gdb_bank_number = val;
  1809. break;
  1810. case SIM_SH_BANK_MACL_REGNUM:
  1811. saved_state.asregs.regstack[gdb_bank_number].regs[REGBANK_MACL] = val;
  1812. break;
  1813. case SIM_SH_BANK_GBR_REGNUM:
  1814. saved_state.asregs.regstack[gdb_bank_number].regs[REGBANK_GBR] = val;
  1815. break;
  1816. case SIM_SH_BANK_PR_REGNUM:
  1817. saved_state.asregs.regstack[gdb_bank_number].regs[REGBANK_PR] = val;
  1818. break;
  1819. case SIM_SH_BANK_IVN_REGNUM:
  1820. saved_state.asregs.regstack[gdb_bank_number].regs[REGBANK_IVN] = val;
  1821. break;
  1822. case SIM_SH_BANK_MACH_REGNUM:
  1823. saved_state.asregs.regstack[gdb_bank_number].regs[REGBANK_MACH] = val;
  1824. break;
  1825. default:
  1826. return 0;
  1827. }
  1828. return length;
  1829. }
  1830. static int
  1831. sh_reg_fetch (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
  1832. {
  1833. int val;
  1834. init_pointers ();
  1835. switch (rn)
  1836. {
  1837. case SIM_SH_R0_REGNUM: case SIM_SH_R1_REGNUM: case SIM_SH_R2_REGNUM:
  1838. case SIM_SH_R3_REGNUM: case SIM_SH_R4_REGNUM: case SIM_SH_R5_REGNUM:
  1839. case SIM_SH_R6_REGNUM: case SIM_SH_R7_REGNUM: case SIM_SH_R8_REGNUM:
  1840. case SIM_SH_R9_REGNUM: case SIM_SH_R10_REGNUM: case SIM_SH_R11_REGNUM:
  1841. case SIM_SH_R12_REGNUM: case SIM_SH_R13_REGNUM: case SIM_SH_R14_REGNUM:
  1842. case SIM_SH_R15_REGNUM:
  1843. val = saved_state.asregs.regs[rn];
  1844. break;
  1845. case SIM_SH_PC_REGNUM:
  1846. val = saved_state.asregs.pc;
  1847. break;
  1848. case SIM_SH_PR_REGNUM:
  1849. val = PR;
  1850. break;
  1851. case SIM_SH_GBR_REGNUM:
  1852. val = GBR;
  1853. break;
  1854. case SIM_SH_VBR_REGNUM:
  1855. val = VBR;
  1856. break;
  1857. case SIM_SH_MACH_REGNUM:
  1858. val = MACH;
  1859. break;
  1860. case SIM_SH_MACL_REGNUM:
  1861. val = MACL;
  1862. break;
  1863. case SIM_SH_SR_REGNUM:
  1864. val = GET_SR ();
  1865. break;
  1866. case SIM_SH_FPUL_REGNUM:
  1867. val = FPUL;
  1868. break;
  1869. case SIM_SH_FPSCR_REGNUM:
  1870. val = GET_FPSCR ();
  1871. break;
  1872. case SIM_SH_FR0_REGNUM: case SIM_SH_FR1_REGNUM: case SIM_SH_FR2_REGNUM:
  1873. case SIM_SH_FR3_REGNUM: case SIM_SH_FR4_REGNUM: case SIM_SH_FR5_REGNUM:
  1874. case SIM_SH_FR6_REGNUM: case SIM_SH_FR7_REGNUM: case SIM_SH_FR8_REGNUM:
  1875. case SIM_SH_FR9_REGNUM: case SIM_SH_FR10_REGNUM: case SIM_SH_FR11_REGNUM:
  1876. case SIM_SH_FR12_REGNUM: case SIM_SH_FR13_REGNUM: case SIM_SH_FR14_REGNUM:
  1877. case SIM_SH_FR15_REGNUM:
  1878. val = FI (rn - SIM_SH_FR0_REGNUM);
  1879. break;
  1880. case SIM_SH_DSR_REGNUM:
  1881. val = DSR;
  1882. break;
  1883. case SIM_SH_A0G_REGNUM:
  1884. val = SEXT (A0G);
  1885. break;
  1886. case SIM_SH_A0_REGNUM:
  1887. val = A0;
  1888. break;
  1889. case SIM_SH_A1G_REGNUM:
  1890. val = SEXT (A1G);
  1891. break;
  1892. case SIM_SH_A1_REGNUM:
  1893. val = A1;
  1894. break;
  1895. case SIM_SH_M0_REGNUM:
  1896. val = M0;
  1897. break;
  1898. case SIM_SH_M1_REGNUM:
  1899. val = M1;
  1900. break;
  1901. case SIM_SH_X0_REGNUM:
  1902. val = X0;
  1903. break;
  1904. case SIM_SH_X1_REGNUM:
  1905. val = X1;
  1906. break;
  1907. case SIM_SH_Y0_REGNUM:
  1908. val = Y0;
  1909. break;
  1910. case SIM_SH_Y1_REGNUM:
  1911. val = Y1;
  1912. break;
  1913. case SIM_SH_MOD_REGNUM:
  1914. val = MOD;
  1915. break;
  1916. case SIM_SH_RS_REGNUM:
  1917. val = RS;
  1918. break;
  1919. case SIM_SH_RE_REGNUM:
  1920. val = RE;
  1921. break;
  1922. case SIM_SH_SSR_REGNUM:
  1923. val = SSR;
  1924. break;
  1925. case SIM_SH_SPC_REGNUM:
  1926. val = SPC;
  1927. break;
  1928. /* The rn_bank idiosyncracies are not due to hardware differences, but to
  1929. a weird aliasing naming scheme for sh3 / sh3e / sh4. */
  1930. case SIM_SH_R0_BANK0_REGNUM: case SIM_SH_R1_BANK0_REGNUM:
  1931. case SIM_SH_R2_BANK0_REGNUM: case SIM_SH_R3_BANK0_REGNUM:
  1932. case SIM_SH_R4_BANK0_REGNUM: case SIM_SH_R5_BANK0_REGNUM:
  1933. case SIM_SH_R6_BANK0_REGNUM: case SIM_SH_R7_BANK0_REGNUM:
  1934. if (saved_state.asregs.bfd_mach == bfd_mach_sh2a)
  1935. {
  1936. rn -= SIM_SH_R0_BANK0_REGNUM;
  1937. val = saved_state.asregs.regstack[gdb_bank_number].regs[rn];
  1938. }
  1939. else
  1940. val = (SR_MD && SR_RB
  1941. ? Rn_BANK (rn - SIM_SH_R0_BANK0_REGNUM)
  1942. : saved_state.asregs.regs[rn - SIM_SH_R0_BANK0_REGNUM]);
  1943. break;
  1944. case SIM_SH_R0_BANK1_REGNUM: case SIM_SH_R1_BANK1_REGNUM:
  1945. case SIM_SH_R2_BANK1_REGNUM: case SIM_SH_R3_BANK1_REGNUM:
  1946. case SIM_SH_R4_BANK1_REGNUM: case SIM_SH_R5_BANK1_REGNUM:
  1947. case SIM_SH_R6_BANK1_REGNUM: case SIM_SH_R7_BANK1_REGNUM:
  1948. if (saved_state.asregs.bfd_mach == bfd_mach_sh2a)
  1949. {
  1950. rn -= SIM_SH_R0_BANK1_REGNUM;
  1951. val = saved_state.asregs.regstack[gdb_bank_number].regs[rn + 8];
  1952. }
  1953. else
  1954. val = (! SR_MD || ! SR_RB
  1955. ? Rn_BANK (rn - SIM_SH_R0_BANK1_REGNUM)
  1956. : saved_state.asregs.regs[rn - SIM_SH_R0_BANK1_REGNUM]);
  1957. break;
  1958. case SIM_SH_R0_BANK_REGNUM: case SIM_SH_R1_BANK_REGNUM:
  1959. case SIM_SH_R2_BANK_REGNUM: case SIM_SH_R3_BANK_REGNUM:
  1960. case SIM_SH_R4_BANK_REGNUM: case SIM_SH_R5_BANK_REGNUM:
  1961. case SIM_SH_R6_BANK_REGNUM: case SIM_SH_R7_BANK_REGNUM:
  1962. val = Rn_BANK (rn - SIM_SH_R0_BANK_REGNUM);
  1963. break;
  1964. case SIM_SH_TBR_REGNUM:
  1965. val = TBR;
  1966. break;
  1967. case SIM_SH_IBNR_REGNUM:
  1968. val = IBNR;
  1969. break;
  1970. case SIM_SH_IBCR_REGNUM:
  1971. val = IBCR;
  1972. break;
  1973. case SIM_SH_BANK_REGNUM:
  1974. /* This is a pseudo-register maintained just for gdb.
  1975. It tells us what register bank gdb would like to read/write. */
  1976. val = gdb_bank_number;
  1977. break;
  1978. case SIM_SH_BANK_MACL_REGNUM:
  1979. val = saved_state.asregs.regstack[gdb_bank_number].regs[REGBANK_MACL];
  1980. break;
  1981. case SIM_SH_BANK_GBR_REGNUM:
  1982. val = saved_state.asregs.regstack[gdb_bank_number].regs[REGBANK_GBR];
  1983. break;
  1984. case SIM_SH_BANK_PR_REGNUM:
  1985. val = saved_state.asregs.regstack[gdb_bank_number].regs[REGBANK_PR];
  1986. break;
  1987. case SIM_SH_BANK_IVN_REGNUM:
  1988. val = saved_state.asregs.regstack[gdb_bank_number].regs[REGBANK_IVN];
  1989. break;
  1990. case SIM_SH_BANK_MACH_REGNUM:
  1991. val = saved_state.asregs.regstack[gdb_bank_number].regs[REGBANK_MACH];
  1992. break;
  1993. default:
  1994. return 0;
  1995. }
  1996. * (int *) memory = swap (val);
  1997. return length;
  1998. }
  1999. void
  2000. sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
  2001. {
  2002. /* The SH simulator uses SIGQUIT to indicate that the program has
  2003. exited, so we must check for it here and translate it to exit. */
  2004. if (saved_state.asregs.exception == SIGQUIT)
  2005. {
  2006. *reason = sim_exited;
  2007. *sigrc = saved_state.asregs.regs[5];
  2008. }
  2009. else
  2010. {
  2011. *reason = sim_stopped;
  2012. *sigrc = saved_state.asregs.exception;
  2013. }
  2014. }
  2015. void
  2016. sim_info (SIM_DESC sd, int verbose)
  2017. {
  2018. double timetaken =
  2019. (double) saved_state.asregs.ticks / (double) now_persec ();
  2020. double virttime = saved_state.asregs.cycles / 36.0e6;
  2021. sim_io_printf (sd, "\n\n# instructions executed %10d\n",
  2022. saved_state.asregs.insts);
  2023. sim_io_printf (sd, "# cycles %10d\n",
  2024. saved_state.asregs.cycles);
  2025. sim_io_printf (sd, "# pipeline stalls %10d\n",
  2026. saved_state.asregs.stalls);
  2027. sim_io_printf (sd, "# misaligned load/store %10d\n",
  2028. saved_state.asregs.memstalls);
  2029. sim_io_printf (sd, "# real time taken %10.4f\n", timetaken);
  2030. sim_io_printf (sd, "# virtual time taken %10.4f\n", virttime);
  2031. sim_io_printf (sd, "# profiling size %10d\n", sim_profile_size);
  2032. sim_io_printf (sd, "# profiling frequency %10d\n",
  2033. saved_state.asregs.profile);
  2034. sim_io_printf (sd, "# profile maxpc %10x\n",
  2035. (1 << sim_profile_size) << PROFILE_SHIFT);
  2036. if (timetaken != 0)
  2037. {
  2038. sim_io_printf (sd, "# cycles/second %10d\n",
  2039. (int) (saved_state.asregs.cycles / timetaken));
  2040. sim_io_printf (sd, "# simulation ratio %10.4f\n",
  2041. virttime / timetaken);
  2042. }
  2043. }
  2044. static sim_cia
  2045. sh_pc_get (sim_cpu *cpu)
  2046. {
  2047. return saved_state.asregs.pc;
  2048. }
  2049. static void
  2050. sh_pc_set (sim_cpu *cpu, sim_cia pc)
  2051. {
  2052. saved_state.asregs.pc = pc;
  2053. }
  2054. static void
  2055. free_state (SIM_DESC sd)
  2056. {
  2057. if (STATE_MODULES (sd) != NULL)
  2058. sim_module_uninstall (sd);
  2059. sim_cpu_free_all (sd);
  2060. sim_state_free (sd);
  2061. }
  2062. SIM_DESC
  2063. sim_open (SIM_OPEN_KIND kind, host_callback *cb,
  2064. struct bfd *abfd, char * const *argv)
  2065. {
  2066. char * const *p;
  2067. int i;
  2068. union
  2069. {
  2070. int i;
  2071. short s[2];
  2072. char c[4];
  2073. }
  2074. mem_word;
  2075. SIM_DESC sd = sim_state_alloc (kind, cb);
  2076. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  2077. /* Set default options before parsing user options. */
  2078. current_alignment = STRICT_ALIGNMENT;
  2079. cb->syscall_map = cb_sh_syscall_map;
  2080. /* The cpu data is kept in a separately allocated chunk of memory. */
  2081. if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
  2082. {
  2083. free_state (sd);
  2084. return 0;
  2085. }
  2086. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  2087. {
  2088. free_state (sd);
  2089. return 0;
  2090. }
  2091. /* The parser will print an error message for us, so we silently return. */
  2092. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  2093. {
  2094. free_state (sd);
  2095. return 0;
  2096. }
  2097. /* Check for/establish the a reference program image. */
  2098. if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
  2099. {
  2100. free_state (sd);
  2101. return 0;
  2102. }
  2103. /* Configure/verify the target byte order and other runtime
  2104. configuration options. */
  2105. if (sim_config (sd) != SIM_RC_OK)
  2106. {
  2107. sim_module_uninstall (sd);
  2108. return 0;
  2109. }
  2110. if (sim_post_argv_init (sd) != SIM_RC_OK)
  2111. {
  2112. /* Uninstall the modules to avoid memory leaks,
  2113. file descriptor leaks, etc. */
  2114. sim_module_uninstall (sd);
  2115. return 0;
  2116. }
  2117. /* CPU specific initialization. */
  2118. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  2119. {
  2120. SIM_CPU *cpu = STATE_CPU (sd, i);
  2121. CPU_REG_FETCH (cpu) = sh_reg_fetch;
  2122. CPU_REG_STORE (cpu) = sh_reg_store;
  2123. CPU_PC_FETCH (cpu) = sh_pc_get;
  2124. CPU_PC_STORE (cpu) = sh_pc_set;
  2125. }
  2126. for (p = argv + 1; *p != NULL; ++p)
  2127. {
  2128. if (isdigit (**p))
  2129. parse_and_set_memory_size (sd, *p);
  2130. }
  2131. if (abfd)
  2132. init_dsp (abfd);
  2133. for (i = 4; (i -= 2) >= 0; )
  2134. mem_word.s[i >> 1] = i;
  2135. global_endianw = mem_word.i >> (target_little_endian ? 0 : 16) & 0xffff;
  2136. for (i = 4; --i >= 0; )
  2137. mem_word.c[i] = i;
  2138. endianb = mem_word.i >> (target_little_endian ? 0 : 24) & 0xff;
  2139. return sd;
  2140. }
  2141. static void
  2142. parse_and_set_memory_size (SIM_DESC sd, const char *str)
  2143. {
  2144. int n;
  2145. n = strtol (str, NULL, 10);
  2146. if (n > 0 && n <= 31)
  2147. sim_memory_size = n;
  2148. else
  2149. sim_io_printf (sd, "Bad memory size %d; must be 1 to 31, inclusive\n", n);
  2150. }
  2151. SIM_RC
  2152. sim_create_inferior (SIM_DESC sd, struct bfd *prog_bfd,
  2153. char * const *argv, char * const *env)
  2154. {
  2155. /* Clear the registers. */
  2156. memset (&saved_state, 0,
  2157. (char*) &saved_state.asregs.end_of_registers - (char*) &saved_state);
  2158. /* Set the PC. */
  2159. if (prog_bfd != NULL)
  2160. saved_state.asregs.pc = bfd_get_start_address (prog_bfd);
  2161. /* Set the bfd machine type. */
  2162. if (prog_bfd != NULL)
  2163. saved_state.asregs.bfd_mach = bfd_get_mach (prog_bfd);
  2164. if (prog_bfd != NULL)
  2165. init_dsp (prog_bfd);
  2166. return SIM_RC_OK;
  2167. }
  2168. void
  2169. sim_do_command (SIM_DESC sd, const char *cmd)
  2170. {
  2171. const char *sms_cmd = "set-memory-size";
  2172. int cmdsize;
  2173. if (cmd == NULL || *cmd == '\0')
  2174. {
  2175. cmd = "help";
  2176. }
  2177. cmdsize = strlen (sms_cmd);
  2178. if (strncmp (cmd, sms_cmd, cmdsize) == 0
  2179. && strchr (" \t", cmd[cmdsize]) != NULL)
  2180. {
  2181. parse_and_set_memory_size (sd, cmd + cmdsize + 1);
  2182. }
  2183. else if (strcmp (cmd, "help") == 0)
  2184. {
  2185. sim_io_printf (sd, "List of SH simulator commands:\n\n");
  2186. sim_io_printf (sd, "set-memory-size <n> -- Set the number of address bits to use\n");
  2187. sim_io_printf (sd, "\n");
  2188. }
  2189. else
  2190. {
  2191. sim_io_printf (sd, "Error: \"%s\" is not a valid SH simulator command.\n", cmd);
  2192. }
  2193. }