interp.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. /* Simulator for Motorola's MCore processor
  2. Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions.
  4. This file is part of GDB, the GNU debugger.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include <signal.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/times.h>
  20. #include <sys/param.h>
  21. #include <unistd.h>
  22. #include "bfd.h"
  23. #include "gdb/callback.h"
  24. #include "libiberty.h"
  25. #include "gdb/remote-sim.h"
  26. #include "sim-main.h"
  27. #include "sim-base.h"
  28. #include "sim-syscall.h"
  29. #include "sim-options.h"
  30. #define target_big_endian (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN)
  31. static unsigned long
  32. mcore_extract_unsigned_integer (unsigned char *addr, int len)
  33. {
  34. unsigned long retval;
  35. unsigned char * p;
  36. unsigned char * startaddr = (unsigned char *)addr;
  37. unsigned char * endaddr = startaddr + len;
  38. if (len > (int) sizeof (unsigned long))
  39. printf ("That operation is not available on integers of more than %zu bytes.",
  40. sizeof (unsigned long));
  41. /* Start at the most significant end of the integer, and work towards
  42. the least significant. */
  43. retval = 0;
  44. if (! target_big_endian)
  45. {
  46. for (p = endaddr; p > startaddr;)
  47. retval = (retval << 8) | * -- p;
  48. }
  49. else
  50. {
  51. for (p = startaddr; p < endaddr;)
  52. retval = (retval << 8) | * p ++;
  53. }
  54. return retval;
  55. }
  56. static void
  57. mcore_store_unsigned_integer (unsigned char *addr, int len, unsigned long val)
  58. {
  59. unsigned char * p;
  60. unsigned char * startaddr = (unsigned char *)addr;
  61. unsigned char * endaddr = startaddr + len;
  62. if (! target_big_endian)
  63. {
  64. for (p = startaddr; p < endaddr;)
  65. {
  66. * p ++ = val & 0xff;
  67. val >>= 8;
  68. }
  69. }
  70. else
  71. {
  72. for (p = endaddr; p > startaddr;)
  73. {
  74. * -- p = val & 0xff;
  75. val >>= 8;
  76. }
  77. }
  78. }
  79. /* The machine state.
  80. This state is maintained in host byte order. The
  81. fetch/store register functions must translate between host
  82. byte order and the target processor byte order.
  83. Keeping this data in target byte order simplifies the register
  84. read/write functions. Keeping this data in native order improves
  85. the performance of the simulator. Simulation speed is deemed more
  86. important. */
  87. /* TODO: Should be moved to sim-main.h:sim_cpu. */
  88. /* The ordering of the mcore_regset structure is matched in the
  89. gdb/config/mcore/tm-mcore.h file in the REGISTER_NAMES macro. */
  90. struct mcore_regset
  91. {
  92. word gregs [16]; /* primary registers */
  93. word alt_gregs [16]; /* alt register file */
  94. word cregs [32]; /* control registers */
  95. int ticks;
  96. int stalls;
  97. int cycles;
  98. int insts;
  99. int exception;
  100. word * active_gregs;
  101. };
  102. union
  103. {
  104. struct mcore_regset asregs;
  105. word asints [1]; /* but accessed larger... */
  106. } cpu;
  107. #define LAST_VALID_CREG 32 /* only 0..12 implemented */
  108. #define NUM_MCORE_REGS (16 + 16 + LAST_VALID_CREG + 1)
  109. static int memcycles = 1;
  110. #define gr asregs.active_gregs
  111. #define cr asregs.cregs
  112. #define sr asregs.cregs[0]
  113. #define vbr asregs.cregs[1]
  114. #define esr asregs.cregs[2]
  115. #define fsr asregs.cregs[3]
  116. #define epc asregs.cregs[4]
  117. #define fpc asregs.cregs[5]
  118. #define ss0 asregs.cregs[6]
  119. #define ss1 asregs.cregs[7]
  120. #define ss2 asregs.cregs[8]
  121. #define ss3 asregs.cregs[9]
  122. #define ss4 asregs.cregs[10]
  123. #define gcr asregs.cregs[11]
  124. #define gsr asregs.cregs[12]
  125. /* maniuplate the carry bit */
  126. #define C_ON() (cpu.sr & 1)
  127. #define C_VALUE() (cpu.sr & 1)
  128. #define C_OFF() ((cpu.sr & 1) == 0)
  129. #define SET_C() {cpu.sr |= 1;}
  130. #define CLR_C() {cpu.sr &= 0xfffffffe;}
  131. #define NEW_C(v) {CLR_C(); cpu.sr |= ((v) & 1);}
  132. #define SR_AF() ((cpu.sr >> 1) & 1)
  133. #define TRAPCODE 1 /* r1 holds which function we want */
  134. #define PARM1 2 /* first parameter */
  135. #define PARM2 3
  136. #define PARM3 4
  137. #define PARM4 5
  138. #define RET1 2 /* register for return values. */
  139. /* Default to a 8 Mbyte (== 2^23) memory space. */
  140. #define DEFAULT_MEMORY_SIZE 0x800000
  141. static void
  142. set_initial_gprs (SIM_CPU *scpu)
  143. {
  144. int i;
  145. long space;
  146. /* Set up machine just out of reset. */
  147. CPU_PC_SET (scpu, 0);
  148. cpu.sr = 0;
  149. /* Clean out the GPRs and alternate GPRs. */
  150. for (i = 0; i < 16; i++)
  151. {
  152. cpu.asregs.gregs[i] = 0;
  153. cpu.asregs.alt_gregs[i] = 0;
  154. }
  155. /* Make our register set point to the right place. */
  156. if (SR_AF())
  157. cpu.asregs.active_gregs = &cpu.asregs.alt_gregs[0];
  158. else
  159. cpu.asregs.active_gregs = &cpu.asregs.gregs[0];
  160. /* ABI specifies initial values for these registers. */
  161. cpu.gr[0] = DEFAULT_MEMORY_SIZE - 4;
  162. /* dac fix, the stack address must be 8-byte aligned! */
  163. cpu.gr[0] = cpu.gr[0] - cpu.gr[0] % 8;
  164. cpu.gr[PARM1] = 0;
  165. cpu.gr[PARM2] = 0;
  166. cpu.gr[PARM3] = 0;
  167. cpu.gr[PARM4] = cpu.gr[0];
  168. }
  169. /* Simulate a monitor trap. */
  170. static void
  171. handle_trap1 (SIM_DESC sd)
  172. {
  173. /* XXX: We don't pass back the actual errno value. */
  174. cpu.gr[RET1] = sim_syscall (STATE_CPU (sd, 0), cpu.gr[TRAPCODE],
  175. cpu.gr[PARM1], cpu.gr[PARM2], cpu.gr[PARM3],
  176. cpu.gr[PARM4]);
  177. }
  178. static void
  179. process_stub (SIM_DESC sd, int what)
  180. {
  181. /* These values should match those in libgloss/mcore/syscalls.s. */
  182. switch (what)
  183. {
  184. case 3: /* _read */
  185. case 4: /* _write */
  186. case 5: /* _open */
  187. case 6: /* _close */
  188. case 10: /* _unlink */
  189. case 19: /* _lseek */
  190. case 43: /* _times */
  191. cpu.gr [TRAPCODE] = what;
  192. handle_trap1 (sd);
  193. break;
  194. default:
  195. if (STATE_VERBOSE_P (sd))
  196. fprintf (stderr, "Unhandled stub opcode: %d\n", what);
  197. break;
  198. }
  199. }
  200. static void
  201. util (SIM_DESC sd, unsigned what)
  202. {
  203. switch (what)
  204. {
  205. case 0: /* exit */
  206. cpu.asregs.exception = SIGQUIT;
  207. break;
  208. case 1: /* printf */
  209. if (STATE_VERBOSE_P (sd))
  210. fprintf (stderr, "WARNING: printf unimplemented\n");
  211. break;
  212. case 2: /* scanf */
  213. if (STATE_VERBOSE_P (sd))
  214. fprintf (stderr, "WARNING: scanf unimplemented\n");
  215. break;
  216. case 3: /* utime */
  217. cpu.gr[RET1] = cpu.asregs.insts;
  218. break;
  219. case 0xFF:
  220. process_stub (sd, cpu.gr[1]);
  221. break;
  222. default:
  223. if (STATE_VERBOSE_P (sd))
  224. fprintf (stderr, "Unhandled util code: %x\n", what);
  225. break;
  226. }
  227. }
  228. /* For figuring out whether we carried; addc/subc use this. */
  229. static int
  230. iu_carry (unsigned long a, unsigned long b, int cin)
  231. {
  232. unsigned long x;
  233. x = (a & 0xffff) + (b & 0xffff) + cin;
  234. x = (x >> 16) + (a >> 16) + (b >> 16);
  235. x >>= 16;
  236. return (x != 0);
  237. }
  238. /* TODO: Convert to common watchpoints. */
  239. #undef WATCHFUNCTIONS
  240. #ifdef WATCHFUNCTIONS
  241. #define MAXWL 80
  242. word WL[MAXWL];
  243. char * WLstr[MAXWL];
  244. int ENDWL=0;
  245. int WLincyc;
  246. int WLcyc[MAXWL];
  247. int WLcnts[MAXWL];
  248. int WLmax[MAXWL];
  249. int WLmin[MAXWL];
  250. word WLendpc;
  251. int WLbcyc;
  252. int WLW;
  253. #endif
  254. #define RD (inst & 0xF)
  255. #define RS ((inst >> 4) & 0xF)
  256. #define RX ((inst >> 8) & 0xF)
  257. #define IMM5 ((inst >> 4) & 0x1F)
  258. #define IMM4 ((inst) & 0xF)
  259. #define rbat(X) sim_core_read_1 (scpu, 0, read_map, X)
  260. #define rhat(X) sim_core_read_2 (scpu, 0, read_map, X)
  261. #define rlat(X) sim_core_read_4 (scpu, 0, read_map, X)
  262. #define wbat(X, D) sim_core_write_1 (scpu, 0, write_map, X, D)
  263. #define what(X, D) sim_core_write_2 (scpu, 0, write_map, X, D)
  264. #define wlat(X, D) sim_core_write_4 (scpu, 0, write_map, X, D)
  265. static int tracing = 0;
  266. void
  267. sim_resume (SIM_DESC sd, int step, int siggnal)
  268. {
  269. SIM_CPU *scpu = STATE_CPU (sd, 0);
  270. int needfetch;
  271. word ibuf;
  272. word pc;
  273. unsigned short inst;
  274. int memops;
  275. int bonus_cycles;
  276. int insts;
  277. int w;
  278. int cycs;
  279. #ifdef WATCHFUNCTIONS
  280. word WLhash;
  281. #endif
  282. cpu.asregs.exception = step ? SIGTRAP: 0;
  283. pc = CPU_PC_GET (scpu);
  284. /* Fetch the initial instructions that we'll decode. */
  285. ibuf = rlat (pc & 0xFFFFFFFC);
  286. needfetch = 0;
  287. memops = 0;
  288. bonus_cycles = 0;
  289. insts = 0;
  290. /* make our register set point to the right place */
  291. if (SR_AF ())
  292. cpu.asregs.active_gregs = & cpu.asregs.alt_gregs[0];
  293. else
  294. cpu.asregs.active_gregs = & cpu.asregs.gregs[0];
  295. #ifdef WATCHFUNCTIONS
  296. /* make a hash to speed exec loop, hope it's nonzero */
  297. WLhash = 0xFFFFFFFF;
  298. for (w = 1; w <= ENDWL; w++)
  299. WLhash = WLhash & WL[w];
  300. #endif
  301. do
  302. {
  303. word oldpc;
  304. insts ++;
  305. if (pc & 02)
  306. {
  307. if (! target_big_endian)
  308. inst = ibuf >> 16;
  309. else
  310. inst = ibuf & 0xFFFF;
  311. needfetch = 1;
  312. }
  313. else
  314. {
  315. if (! target_big_endian)
  316. inst = ibuf & 0xFFFF;
  317. else
  318. inst = ibuf >> 16;
  319. }
  320. #ifdef WATCHFUNCTIONS
  321. /* now scan list of watch addresses, if match, count it and
  322. note return address and count cycles until pc=return address */
  323. if ((WLincyc == 1) && (pc == WLendpc))
  324. {
  325. cycs = (cpu.asregs.cycles + (insts + bonus_cycles +
  326. (memops * memcycles)) - WLbcyc);
  327. if (WLcnts[WLW] == 1)
  328. {
  329. WLmax[WLW] = cycs;
  330. WLmin[WLW] = cycs;
  331. WLcyc[WLW] = 0;
  332. }
  333. if (cycs > WLmax[WLW])
  334. {
  335. WLmax[WLW] = cycs;
  336. }
  337. if (cycs < WLmin[WLW])
  338. {
  339. WLmin[WLW] = cycs;
  340. }
  341. WLcyc[WLW] += cycs;
  342. WLincyc = 0;
  343. WLendpc = 0;
  344. }
  345. /* Optimize with a hash to speed loop. */
  346. if (WLincyc == 0)
  347. {
  348. if ((WLhash == 0) || ((WLhash & pc) != 0))
  349. {
  350. for (w=1; w <= ENDWL; w++)
  351. {
  352. if (pc == WL[w])
  353. {
  354. WLcnts[w]++;
  355. WLbcyc = cpu.asregs.cycles + insts
  356. + bonus_cycles + (memops * memcycles);
  357. WLendpc = cpu.gr[15];
  358. WLincyc = 1;
  359. WLW = w;
  360. break;
  361. }
  362. }
  363. }
  364. }
  365. #endif
  366. if (tracing)
  367. fprintf (stderr, "%.4lx: inst = %.4x ", pc, inst);
  368. oldpc = pc;
  369. pc += 2;
  370. switch (inst >> 8)
  371. {
  372. case 0x00:
  373. switch RS
  374. {
  375. case 0x0:
  376. switch RD
  377. {
  378. case 0x0: /* bkpt */
  379. cpu.asregs.exception = SIGTRAP;
  380. pc -= 2;
  381. break;
  382. case 0x1: /* sync */
  383. break;
  384. case 0x2: /* rte */
  385. pc = cpu.epc;
  386. cpu.sr = cpu.esr;
  387. needfetch = 1;
  388. if (SR_AF ())
  389. cpu.asregs.active_gregs = & cpu.asregs.alt_gregs[0];
  390. else
  391. cpu.asregs.active_gregs = & cpu.asregs.gregs[0];
  392. break;
  393. case 0x3: /* rfi */
  394. pc = cpu.fpc;
  395. cpu.sr = cpu.fsr;
  396. needfetch = 1;
  397. if (SR_AF ())
  398. cpu.asregs.active_gregs = &cpu.asregs.alt_gregs[0];
  399. else
  400. cpu.asregs.active_gregs = &cpu.asregs.gregs[0];
  401. break;
  402. case 0x4: /* stop */
  403. if (STATE_VERBOSE_P (sd))
  404. fprintf (stderr, "WARNING: stop unimplemented\n");
  405. break;
  406. case 0x5: /* wait */
  407. if (STATE_VERBOSE_P (sd))
  408. fprintf (stderr, "WARNING: wait unimplemented\n");
  409. break;
  410. case 0x6: /* doze */
  411. if (STATE_VERBOSE_P (sd))
  412. fprintf (stderr, "WARNING: doze unimplemented\n");
  413. break;
  414. case 0x7:
  415. cpu.asregs.exception = SIGILL; /* illegal */
  416. break;
  417. case 0x8: /* trap 0 */
  418. case 0xA: /* trap 2 */
  419. case 0xB: /* trap 3 */
  420. cpu.asregs.exception = SIGTRAP;
  421. break;
  422. case 0xC: /* trap 4 */
  423. case 0xD: /* trap 5 */
  424. case 0xE: /* trap 6 */
  425. cpu.asregs.exception = SIGILL; /* illegal */
  426. break;
  427. case 0xF: /* trap 7 */
  428. cpu.asregs.exception = SIGTRAP; /* integer div-by-0 */
  429. break;
  430. case 0x9: /* trap 1 */
  431. handle_trap1 (sd);
  432. break;
  433. }
  434. break;
  435. case 0x1:
  436. cpu.asregs.exception = SIGILL; /* illegal */
  437. break;
  438. case 0x2: /* mvc */
  439. cpu.gr[RD] = C_VALUE();
  440. break;
  441. case 0x3: /* mvcv */
  442. cpu.gr[RD] = C_OFF();
  443. break;
  444. case 0x4: /* ldq */
  445. {
  446. word addr = cpu.gr[RD];
  447. int regno = 4; /* always r4-r7 */
  448. bonus_cycles++;
  449. memops += 4;
  450. do
  451. {
  452. cpu.gr[regno] = rlat(addr);
  453. addr += 4;
  454. regno++;
  455. }
  456. while ((regno&0x3) != 0);
  457. }
  458. break;
  459. case 0x5: /* stq */
  460. {
  461. word addr = cpu.gr[RD];
  462. int regno = 4; /* always r4-r7 */
  463. memops += 4;
  464. bonus_cycles++;
  465. do
  466. {
  467. wlat(addr, cpu.gr[regno]);
  468. addr += 4;
  469. regno++;
  470. }
  471. while ((regno & 0x3) != 0);
  472. }
  473. break;
  474. case 0x6: /* ldm */
  475. {
  476. word addr = cpu.gr[0];
  477. int regno = RD;
  478. /* bonus cycle is really only needed if
  479. the next insn shifts the last reg loaded.
  480. bonus_cycles++;
  481. */
  482. memops += 16-regno;
  483. while (regno <= 0xF)
  484. {
  485. cpu.gr[regno] = rlat(addr);
  486. addr += 4;
  487. regno++;
  488. }
  489. }
  490. break;
  491. case 0x7: /* stm */
  492. {
  493. word addr = cpu.gr[0];
  494. int regno = RD;
  495. /* this should be removed! */
  496. /* bonus_cycles ++; */
  497. memops += 16 - regno;
  498. while (regno <= 0xF)
  499. {
  500. wlat(addr, cpu.gr[regno]);
  501. addr += 4;
  502. regno++;
  503. }
  504. }
  505. break;
  506. case 0x8: /* dect */
  507. cpu.gr[RD] -= C_VALUE();
  508. break;
  509. case 0x9: /* decf */
  510. cpu.gr[RD] -= C_OFF();
  511. break;
  512. case 0xA: /* inct */
  513. cpu.gr[RD] += C_VALUE();
  514. break;
  515. case 0xB: /* incf */
  516. cpu.gr[RD] += C_OFF();
  517. break;
  518. case 0xC: /* jmp */
  519. pc = cpu.gr[RD];
  520. if (tracing && RD == 15)
  521. fprintf (stderr, "Func return, r2 = %lxx, r3 = %lx\n",
  522. cpu.gr[2], cpu.gr[3]);
  523. bonus_cycles++;
  524. needfetch = 1;
  525. break;
  526. case 0xD: /* jsr */
  527. cpu.gr[15] = pc;
  528. pc = cpu.gr[RD];
  529. bonus_cycles++;
  530. needfetch = 1;
  531. break;
  532. case 0xE: /* ff1 */
  533. {
  534. word tmp, i;
  535. tmp = cpu.gr[RD];
  536. for (i = 0; !(tmp & 0x80000000) && i < 32; i++)
  537. tmp <<= 1;
  538. cpu.gr[RD] = i;
  539. }
  540. break;
  541. case 0xF: /* brev */
  542. {
  543. word tmp;
  544. tmp = cpu.gr[RD];
  545. tmp = ((tmp & 0xaaaaaaaa) >> 1) | ((tmp & 0x55555555) << 1);
  546. tmp = ((tmp & 0xcccccccc) >> 2) | ((tmp & 0x33333333) << 2);
  547. tmp = ((tmp & 0xf0f0f0f0) >> 4) | ((tmp & 0x0f0f0f0f) << 4);
  548. tmp = ((tmp & 0xff00ff00) >> 8) | ((tmp & 0x00ff00ff) << 8);
  549. cpu.gr[RD] = ((tmp & 0xffff0000) >> 16) | ((tmp & 0x0000ffff) << 16);
  550. }
  551. break;
  552. }
  553. break;
  554. case 0x01:
  555. switch RS
  556. {
  557. case 0x0: /* xtrb3 */
  558. cpu.gr[1] = (cpu.gr[RD]) & 0xFF;
  559. NEW_C (cpu.gr[RD] != 0);
  560. break;
  561. case 0x1: /* xtrb2 */
  562. cpu.gr[1] = (cpu.gr[RD]>>8) & 0xFF;
  563. NEW_C (cpu.gr[RD] != 0);
  564. break;
  565. case 0x2: /* xtrb1 */
  566. cpu.gr[1] = (cpu.gr[RD]>>16) & 0xFF;
  567. NEW_C (cpu.gr[RD] != 0);
  568. break;
  569. case 0x3: /* xtrb0 */
  570. cpu.gr[1] = (cpu.gr[RD]>>24) & 0xFF;
  571. NEW_C (cpu.gr[RD] != 0);
  572. break;
  573. case 0x4: /* zextb */
  574. cpu.gr[RD] &= 0x000000FF;
  575. break;
  576. case 0x5: /* sextb */
  577. {
  578. long tmp;
  579. tmp = cpu.gr[RD];
  580. tmp <<= 24;
  581. tmp >>= 24;
  582. cpu.gr[RD] = tmp;
  583. }
  584. break;
  585. case 0x6: /* zexth */
  586. cpu.gr[RD] &= 0x0000FFFF;
  587. break;
  588. case 0x7: /* sexth */
  589. {
  590. long tmp;
  591. tmp = cpu.gr[RD];
  592. tmp <<= 16;
  593. tmp >>= 16;
  594. cpu.gr[RD] = tmp;
  595. }
  596. break;
  597. case 0x8: /* declt */
  598. --cpu.gr[RD];
  599. NEW_C ((long)cpu.gr[RD] < 0);
  600. break;
  601. case 0x9: /* tstnbz */
  602. {
  603. word tmp = cpu.gr[RD];
  604. NEW_C ((tmp & 0xFF000000) != 0 &&
  605. (tmp & 0x00FF0000) != 0 && (tmp & 0x0000FF00) != 0 &&
  606. (tmp & 0x000000FF) != 0);
  607. }
  608. break;
  609. case 0xA: /* decgt */
  610. --cpu.gr[RD];
  611. NEW_C ((long)cpu.gr[RD] > 0);
  612. break;
  613. case 0xB: /* decne */
  614. --cpu.gr[RD];
  615. NEW_C ((long)cpu.gr[RD] != 0);
  616. break;
  617. case 0xC: /* clrt */
  618. if (C_ON())
  619. cpu.gr[RD] = 0;
  620. break;
  621. case 0xD: /* clrf */
  622. if (C_OFF())
  623. cpu.gr[RD] = 0;
  624. break;
  625. case 0xE: /* abs */
  626. if (cpu.gr[RD] & 0x80000000)
  627. cpu.gr[RD] = ~cpu.gr[RD] + 1;
  628. break;
  629. case 0xF: /* not */
  630. cpu.gr[RD] = ~cpu.gr[RD];
  631. break;
  632. }
  633. break;
  634. case 0x02: /* movt */
  635. if (C_ON())
  636. cpu.gr[RD] = cpu.gr[RS];
  637. break;
  638. case 0x03: /* mult */
  639. /* consume 2 bits per cycle from rs, until rs is 0 */
  640. {
  641. unsigned int t = cpu.gr[RS];
  642. int ticks;
  643. for (ticks = 0; t != 0 ; t >>= 2)
  644. ticks++;
  645. bonus_cycles += ticks;
  646. }
  647. bonus_cycles += 2; /* min. is 3, so add 2, plus ticks above */
  648. if (tracing)
  649. fprintf (stderr, " mult %lx by %lx to give %lx",
  650. cpu.gr[RD], cpu.gr[RS], cpu.gr[RD] * cpu.gr[RS]);
  651. cpu.gr[RD] = cpu.gr[RD] * cpu.gr[RS];
  652. break;
  653. case 0x04: /* loopt */
  654. if (C_ON())
  655. {
  656. pc += (IMM4 << 1) - 32;
  657. bonus_cycles ++;
  658. needfetch = 1;
  659. }
  660. --cpu.gr[RS]; /* not RD! */
  661. NEW_C (((long)cpu.gr[RS]) > 0);
  662. break;
  663. case 0x05: /* subu */
  664. cpu.gr[RD] -= cpu.gr[RS];
  665. break;
  666. case 0x06: /* addc */
  667. {
  668. unsigned long tmp, a, b;
  669. a = cpu.gr[RD];
  670. b = cpu.gr[RS];
  671. cpu.gr[RD] = a + b + C_VALUE ();
  672. tmp = iu_carry (a, b, C_VALUE ());
  673. NEW_C (tmp);
  674. }
  675. break;
  676. case 0x07: /* subc */
  677. {
  678. unsigned long tmp, a, b;
  679. a = cpu.gr[RD];
  680. b = cpu.gr[RS];
  681. cpu.gr[RD] = a - b + C_VALUE () - 1;
  682. tmp = iu_carry (a,~b, C_VALUE ());
  683. NEW_C (tmp);
  684. }
  685. break;
  686. case 0x08: /* illegal */
  687. case 0x09: /* illegal*/
  688. cpu.asregs.exception = SIGILL;
  689. break;
  690. case 0x0A: /* movf */
  691. if (C_OFF())
  692. cpu.gr[RD] = cpu.gr[RS];
  693. break;
  694. case 0x0B: /* lsr */
  695. {
  696. unsigned long dst, src;
  697. dst = cpu.gr[RD];
  698. src = cpu.gr[RS];
  699. /* We must not rely solely upon the native shift operations, since they
  700. may not match the M*Core's behaviour on boundary conditions. */
  701. dst = src > 31 ? 0 : dst >> src;
  702. cpu.gr[RD] = dst;
  703. }
  704. break;
  705. case 0x0C: /* cmphs */
  706. NEW_C ((unsigned long )cpu.gr[RD] >=
  707. (unsigned long)cpu.gr[RS]);
  708. break;
  709. case 0x0D: /* cmplt */
  710. NEW_C ((long)cpu.gr[RD] < (long)cpu.gr[RS]);
  711. break;
  712. case 0x0E: /* tst */
  713. NEW_C ((cpu.gr[RD] & cpu.gr[RS]) != 0);
  714. break;
  715. case 0x0F: /* cmpne */
  716. NEW_C (cpu.gr[RD] != cpu.gr[RS]);
  717. break;
  718. case 0x10: case 0x11: /* mfcr */
  719. {
  720. unsigned r;
  721. r = IMM5;
  722. if (r <= LAST_VALID_CREG)
  723. cpu.gr[RD] = cpu.cr[r];
  724. else
  725. cpu.asregs.exception = SIGILL;
  726. }
  727. break;
  728. case 0x12: /* mov */
  729. cpu.gr[RD] = cpu.gr[RS];
  730. if (tracing)
  731. fprintf (stderr, "MOV %lx into reg %d", cpu.gr[RD], RD);
  732. break;
  733. case 0x13: /* bgenr */
  734. if (cpu.gr[RS] & 0x20)
  735. cpu.gr[RD] = 0;
  736. else
  737. cpu.gr[RD] = 1 << (cpu.gr[RS] & 0x1F);
  738. break;
  739. case 0x14: /* rsub */
  740. cpu.gr[RD] = cpu.gr[RS] - cpu.gr[RD];
  741. break;
  742. case 0x15: /* ixw */
  743. cpu.gr[RD] += cpu.gr[RS]<<2;
  744. break;
  745. case 0x16: /* and */
  746. cpu.gr[RD] &= cpu.gr[RS];
  747. break;
  748. case 0x17: /* xor */
  749. cpu.gr[RD] ^= cpu.gr[RS];
  750. break;
  751. case 0x18: case 0x19: /* mtcr */
  752. {
  753. unsigned r;
  754. r = IMM5;
  755. if (r <= LAST_VALID_CREG)
  756. cpu.cr[r] = cpu.gr[RD];
  757. else
  758. cpu.asregs.exception = SIGILL;
  759. /* we might have changed register sets... */
  760. if (SR_AF ())
  761. cpu.asregs.active_gregs = & cpu.asregs.alt_gregs[0];
  762. else
  763. cpu.asregs.active_gregs = & cpu.asregs.gregs[0];
  764. }
  765. break;
  766. case 0x1A: /* asr */
  767. /* We must not rely solely upon the native shift operations, since they
  768. may not match the M*Core's behaviour on boundary conditions. */
  769. if (cpu.gr[RS] > 30)
  770. cpu.gr[RD] = ((long) cpu.gr[RD]) < 0 ? -1 : 0;
  771. else
  772. cpu.gr[RD] = (long) cpu.gr[RD] >> cpu.gr[RS];
  773. break;
  774. case 0x1B: /* lsl */
  775. /* We must not rely solely upon the native shift operations, since they
  776. may not match the M*Core's behaviour on boundary conditions. */
  777. cpu.gr[RD] = cpu.gr[RS] > 31 ? 0 : cpu.gr[RD] << cpu.gr[RS];
  778. break;
  779. case 0x1C: /* addu */
  780. cpu.gr[RD] += cpu.gr[RS];
  781. break;
  782. case 0x1D: /* ixh */
  783. cpu.gr[RD] += cpu.gr[RS] << 1;
  784. break;
  785. case 0x1E: /* or */
  786. cpu.gr[RD] |= cpu.gr[RS];
  787. break;
  788. case 0x1F: /* andn */
  789. cpu.gr[RD] &= ~cpu.gr[RS];
  790. break;
  791. case 0x20: case 0x21: /* addi */
  792. cpu.gr[RD] =
  793. cpu.gr[RD] + (IMM5 + 1);
  794. break;
  795. case 0x22: case 0x23: /* cmplti */
  796. {
  797. int tmp = (IMM5 + 1);
  798. if (cpu.gr[RD] < tmp)
  799. {
  800. SET_C();
  801. }
  802. else
  803. {
  804. CLR_C();
  805. }
  806. }
  807. break;
  808. case 0x24: case 0x25: /* subi */
  809. cpu.gr[RD] =
  810. cpu.gr[RD] - (IMM5 + 1);
  811. break;
  812. case 0x26: case 0x27: /* illegal */
  813. cpu.asregs.exception = SIGILL;
  814. break;
  815. case 0x28: case 0x29: /* rsubi */
  816. cpu.gr[RD] =
  817. IMM5 - cpu.gr[RD];
  818. break;
  819. case 0x2A: case 0x2B: /* cmpnei */
  820. if (cpu.gr[RD] != IMM5)
  821. {
  822. SET_C();
  823. }
  824. else
  825. {
  826. CLR_C();
  827. }
  828. break;
  829. case 0x2C: case 0x2D: /* bmaski, divu */
  830. {
  831. unsigned imm = IMM5;
  832. if (imm == 1)
  833. {
  834. int exe;
  835. int rxnlz, r1nlz;
  836. unsigned int rx, r1;
  837. rx = cpu.gr[RD];
  838. r1 = cpu.gr[1];
  839. exe = 0;
  840. /* unsigned divide */
  841. cpu.gr[RD] = (word) ((unsigned int) cpu.gr[RD] / (unsigned int)cpu.gr[1] );
  842. /* compute bonus_cycles for divu */
  843. for (r1nlz = 0; ((r1 & 0x80000000) == 0) && (r1nlz < 32); r1nlz ++)
  844. r1 = r1 << 1;
  845. for (rxnlz = 0; ((rx & 0x80000000) == 0) && (rxnlz < 32); rxnlz ++)
  846. rx = rx << 1;
  847. if (r1nlz < rxnlz)
  848. exe += 4;
  849. else
  850. exe += 5 + r1nlz - rxnlz;
  851. if (exe >= (2 * memcycles - 1))
  852. {
  853. bonus_cycles += exe - (2 * memcycles) + 1;
  854. }
  855. }
  856. else if (imm == 0 || imm >= 8)
  857. {
  858. /* bmaski */
  859. if (imm == 0)
  860. cpu.gr[RD] = -1;
  861. else
  862. cpu.gr[RD] = (1 << imm) - 1;
  863. }
  864. else
  865. {
  866. /* illegal */
  867. cpu.asregs.exception = SIGILL;
  868. }
  869. }
  870. break;
  871. case 0x2E: case 0x2F: /* andi */
  872. cpu.gr[RD] = cpu.gr[RD] & IMM5;
  873. break;
  874. case 0x30: case 0x31: /* bclri */
  875. cpu.gr[RD] = cpu.gr[RD] & ~(1<<IMM5);
  876. break;
  877. case 0x32: case 0x33: /* bgeni, divs */
  878. {
  879. unsigned imm = IMM5;
  880. if (imm == 1)
  881. {
  882. int exe,sc;
  883. int rxnlz, r1nlz;
  884. signed int rx, r1;
  885. /* compute bonus_cycles for divu */
  886. rx = cpu.gr[RD];
  887. r1 = cpu.gr[1];
  888. exe = 0;
  889. if (((rx < 0) && (r1 > 0)) || ((rx >= 0) && (r1 < 0)))
  890. sc = 1;
  891. else
  892. sc = 0;
  893. rx = abs (rx);
  894. r1 = abs (r1);
  895. /* signed divide, general registers are of type int, so / op is OK */
  896. cpu.gr[RD] = cpu.gr[RD] / cpu.gr[1];
  897. for (r1nlz = 0; ((r1 & 0x80000000) == 0) && (r1nlz < 32) ; r1nlz ++ )
  898. r1 = r1 << 1;
  899. for (rxnlz = 0; ((rx & 0x80000000) == 0) && (rxnlz < 32) ; rxnlz ++ )
  900. rx = rx << 1;
  901. if (r1nlz < rxnlz)
  902. exe += 5;
  903. else
  904. exe += 6 + r1nlz - rxnlz + sc;
  905. if (exe >= (2 * memcycles - 1))
  906. {
  907. bonus_cycles += exe - (2 * memcycles) + 1;
  908. }
  909. }
  910. else if (imm >= 7)
  911. {
  912. /* bgeni */
  913. cpu.gr[RD] = (1 << IMM5);
  914. }
  915. else
  916. {
  917. /* illegal */
  918. cpu.asregs.exception = SIGILL;
  919. }
  920. break;
  921. }
  922. case 0x34: case 0x35: /* bseti */
  923. cpu.gr[RD] = cpu.gr[RD] | (1 << IMM5);
  924. break;
  925. case 0x36: case 0x37: /* btsti */
  926. NEW_C (cpu.gr[RD] >> IMM5);
  927. break;
  928. case 0x38: case 0x39: /* xsr, rotli */
  929. {
  930. unsigned imm = IMM5;
  931. unsigned long tmp = cpu.gr[RD];
  932. if (imm == 0)
  933. {
  934. word cbit;
  935. cbit = C_VALUE();
  936. NEW_C (tmp);
  937. cpu.gr[RD] = (cbit << 31) | (tmp >> 1);
  938. }
  939. else
  940. cpu.gr[RD] = (tmp << imm) | (tmp >> (32 - imm));
  941. }
  942. break;
  943. case 0x3A: case 0x3B: /* asrc, asri */
  944. {
  945. unsigned imm = IMM5;
  946. long tmp = cpu.gr[RD];
  947. if (imm == 0)
  948. {
  949. NEW_C (tmp);
  950. cpu.gr[RD] = tmp >> 1;
  951. }
  952. else
  953. cpu.gr[RD] = tmp >> imm;
  954. }
  955. break;
  956. case 0x3C: case 0x3D: /* lslc, lsli */
  957. {
  958. unsigned imm = IMM5;
  959. unsigned long tmp = cpu.gr[RD];
  960. if (imm == 0)
  961. {
  962. NEW_C (tmp >> 31);
  963. cpu.gr[RD] = tmp << 1;
  964. }
  965. else
  966. cpu.gr[RD] = tmp << imm;
  967. }
  968. break;
  969. case 0x3E: case 0x3F: /* lsrc, lsri */
  970. {
  971. unsigned imm = IMM5;
  972. unsigned long tmp = cpu.gr[RD];
  973. if (imm == 0)
  974. {
  975. NEW_C (tmp);
  976. cpu.gr[RD] = tmp >> 1;
  977. }
  978. else
  979. cpu.gr[RD] = tmp >> imm;
  980. }
  981. break;
  982. case 0x40: case 0x41: case 0x42: case 0x43:
  983. case 0x44: case 0x45: case 0x46: case 0x47:
  984. case 0x48: case 0x49: case 0x4A: case 0x4B:
  985. case 0x4C: case 0x4D: case 0x4E: case 0x4F:
  986. cpu.asregs.exception = SIGILL;
  987. break;
  988. case 0x50:
  989. util (sd, inst & 0xFF);
  990. break;
  991. case 0x51: case 0x52: case 0x53:
  992. case 0x54: case 0x55: case 0x56: case 0x57:
  993. case 0x58: case 0x59: case 0x5A: case 0x5B:
  994. case 0x5C: case 0x5D: case 0x5E: case 0x5F:
  995. cpu.asregs.exception = SIGILL;
  996. break;
  997. case 0x60: case 0x61: case 0x62: case 0x63: /* movi */
  998. case 0x64: case 0x65: case 0x66: case 0x67:
  999. cpu.gr[RD] = (inst >> 4) & 0x7F;
  1000. break;
  1001. case 0x68: case 0x69: case 0x6A: case 0x6B:
  1002. case 0x6C: case 0x6D: case 0x6E: case 0x6F: /* illegal */
  1003. cpu.asregs.exception = SIGILL;
  1004. break;
  1005. case 0x71: case 0x72: case 0x73:
  1006. case 0x74: case 0x75: case 0x76: case 0x77:
  1007. case 0x78: case 0x79: case 0x7A: case 0x7B:
  1008. case 0x7C: case 0x7D: case 0x7E: /* lrw */
  1009. cpu.gr[RX] = rlat ((pc + ((inst & 0xFF) << 2)) & 0xFFFFFFFC);
  1010. if (tracing)
  1011. fprintf (stderr, "LRW of 0x%x from 0x%lx to reg %d",
  1012. rlat ((pc + ((inst & 0xFF) << 2)) & 0xFFFFFFFC),
  1013. (pc + ((inst & 0xFF) << 2)) & 0xFFFFFFFC, RX);
  1014. memops++;
  1015. break;
  1016. case 0x7F: /* jsri */
  1017. cpu.gr[15] = pc;
  1018. if (tracing)
  1019. fprintf (stderr,
  1020. "func call: r2 = %lx r3 = %lx r4 = %lx r5 = %lx r6 = %lx r7 = %lx\n",
  1021. cpu.gr[2], cpu.gr[3], cpu.gr[4], cpu.gr[5], cpu.gr[6], cpu.gr[7]);
  1022. case 0x70: /* jmpi */
  1023. pc = rlat ((pc + ((inst & 0xFF) << 2)) & 0xFFFFFFFC);
  1024. memops++;
  1025. bonus_cycles++;
  1026. needfetch = 1;
  1027. break;
  1028. case 0x80: case 0x81: case 0x82: case 0x83:
  1029. case 0x84: case 0x85: case 0x86: case 0x87:
  1030. case 0x88: case 0x89: case 0x8A: case 0x8B:
  1031. case 0x8C: case 0x8D: case 0x8E: case 0x8F: /* ld */
  1032. cpu.gr[RX] = rlat (cpu.gr[RD] + ((inst >> 2) & 0x003C));
  1033. if (tracing)
  1034. fprintf (stderr, "load reg %d from 0x%lx with 0x%lx",
  1035. RX,
  1036. cpu.gr[RD] + ((inst >> 2) & 0x003C), cpu.gr[RX]);
  1037. memops++;
  1038. break;
  1039. case 0x90: case 0x91: case 0x92: case 0x93:
  1040. case 0x94: case 0x95: case 0x96: case 0x97:
  1041. case 0x98: case 0x99: case 0x9A: case 0x9B:
  1042. case 0x9C: case 0x9D: case 0x9E: case 0x9F: /* st */
  1043. wlat (cpu.gr[RD] + ((inst >> 2) & 0x003C), cpu.gr[RX]);
  1044. if (tracing)
  1045. fprintf (stderr, "store reg %d (containing 0x%lx) to 0x%lx",
  1046. RX, cpu.gr[RX],
  1047. cpu.gr[RD] + ((inst >> 2) & 0x003C));
  1048. memops++;
  1049. break;
  1050. case 0xA0: case 0xA1: case 0xA2: case 0xA3:
  1051. case 0xA4: case 0xA5: case 0xA6: case 0xA7:
  1052. case 0xA8: case 0xA9: case 0xAA: case 0xAB:
  1053. case 0xAC: case 0xAD: case 0xAE: case 0xAF: /* ld.b */
  1054. cpu.gr[RX] = rbat (cpu.gr[RD] + RS);
  1055. memops++;
  1056. break;
  1057. case 0xB0: case 0xB1: case 0xB2: case 0xB3:
  1058. case 0xB4: case 0xB5: case 0xB6: case 0xB7:
  1059. case 0xB8: case 0xB9: case 0xBA: case 0xBB:
  1060. case 0xBC: case 0xBD: case 0xBE: case 0xBF: /* st.b */
  1061. wbat (cpu.gr[RD] + RS, cpu.gr[RX]);
  1062. memops++;
  1063. break;
  1064. case 0xC0: case 0xC1: case 0xC2: case 0xC3:
  1065. case 0xC4: case 0xC5: case 0xC6: case 0xC7:
  1066. case 0xC8: case 0xC9: case 0xCA: case 0xCB:
  1067. case 0xCC: case 0xCD: case 0xCE: case 0xCF: /* ld.h */
  1068. cpu.gr[RX] = rhat (cpu.gr[RD] + ((inst >> 3) & 0x001E));
  1069. memops++;
  1070. break;
  1071. case 0xD0: case 0xD1: case 0xD2: case 0xD3:
  1072. case 0xD4: case 0xD5: case 0xD6: case 0xD7:
  1073. case 0xD8: case 0xD9: case 0xDA: case 0xDB:
  1074. case 0xDC: case 0xDD: case 0xDE: case 0xDF: /* st.h */
  1075. what (cpu.gr[RD] + ((inst >> 3) & 0x001E), cpu.gr[RX]);
  1076. memops++;
  1077. break;
  1078. case 0xE8: case 0xE9: case 0xEA: case 0xEB:
  1079. case 0xEC: case 0xED: case 0xEE: case 0xEF: /* bf */
  1080. if (C_OFF())
  1081. {
  1082. int disp;
  1083. disp = inst & 0x03FF;
  1084. if (inst & 0x0400)
  1085. disp |= 0xFFFFFC00;
  1086. pc += disp<<1;
  1087. bonus_cycles++;
  1088. needfetch = 1;
  1089. }
  1090. break;
  1091. case 0xE0: case 0xE1: case 0xE2: case 0xE3:
  1092. case 0xE4: case 0xE5: case 0xE6: case 0xE7: /* bt */
  1093. if (C_ON())
  1094. {
  1095. int disp;
  1096. disp = inst & 0x03FF;
  1097. if (inst & 0x0400)
  1098. disp |= 0xFFFFFC00;
  1099. pc += disp<<1;
  1100. bonus_cycles++;
  1101. needfetch = 1;
  1102. }
  1103. break;
  1104. case 0xF8: case 0xF9: case 0xFA: case 0xFB:
  1105. case 0xFC: case 0xFD: case 0xFE: case 0xFF: /* bsr */
  1106. cpu.gr[15] = pc;
  1107. case 0xF0: case 0xF1: case 0xF2: case 0xF3:
  1108. case 0xF4: case 0xF5: case 0xF6: case 0xF7: /* br */
  1109. {
  1110. int disp;
  1111. disp = inst & 0x03FF;
  1112. if (inst & 0x0400)
  1113. disp |= 0xFFFFFC00;
  1114. pc += disp<<1;
  1115. bonus_cycles++;
  1116. needfetch = 1;
  1117. }
  1118. break;
  1119. }
  1120. if (tracing)
  1121. fprintf (stderr, "\n");
  1122. if (needfetch)
  1123. {
  1124. ibuf = rlat (pc & 0xFFFFFFFC);
  1125. needfetch = 0;
  1126. }
  1127. }
  1128. while (!cpu.asregs.exception);
  1129. /* Hide away the things we've cached while executing. */
  1130. CPU_PC_SET (scpu, pc);
  1131. cpu.asregs.insts += insts; /* instructions done ... */
  1132. cpu.asregs.cycles += insts; /* and each takes a cycle */
  1133. cpu.asregs.cycles += bonus_cycles; /* and extra cycles for branches */
  1134. cpu.asregs.cycles += memops * memcycles; /* and memop cycle delays */
  1135. }
  1136. int
  1137. sim_store_register (SIM_DESC sd, int rn, unsigned char *memory, int length)
  1138. {
  1139. if (rn < NUM_MCORE_REGS && rn >= 0)
  1140. {
  1141. if (length == 4)
  1142. {
  1143. long ival;
  1144. /* misalignment safe */
  1145. ival = mcore_extract_unsigned_integer (memory, 4);
  1146. cpu.asints[rn] = ival;
  1147. }
  1148. return 4;
  1149. }
  1150. else
  1151. return 0;
  1152. }
  1153. int
  1154. sim_fetch_register (SIM_DESC sd, int rn, unsigned char *memory, int length)
  1155. {
  1156. if (rn < NUM_MCORE_REGS && rn >= 0)
  1157. {
  1158. if (length == 4)
  1159. {
  1160. long ival = cpu.asints[rn];
  1161. /* misalignment-safe */
  1162. mcore_store_unsigned_integer (memory, 4, ival);
  1163. }
  1164. return 4;
  1165. }
  1166. else
  1167. return 0;
  1168. }
  1169. void
  1170. sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
  1171. {
  1172. if (cpu.asregs.exception == SIGQUIT)
  1173. {
  1174. * reason = sim_exited;
  1175. * sigrc = cpu.gr[PARM1];
  1176. }
  1177. else
  1178. {
  1179. * reason = sim_stopped;
  1180. * sigrc = cpu.asregs.exception;
  1181. }
  1182. }
  1183. void
  1184. sim_info (SIM_DESC sd, int verbose)
  1185. {
  1186. #ifdef WATCHFUNCTIONS
  1187. int w, wcyc;
  1188. #endif
  1189. double virttime = cpu.asregs.cycles / 36.0e6;
  1190. host_callback *callback = STATE_CALLBACK (sd);
  1191. callback->printf_filtered (callback, "\n\n# instructions executed %10d\n",
  1192. cpu.asregs.insts);
  1193. callback->printf_filtered (callback, "# cycles %10d\n",
  1194. cpu.asregs.cycles);
  1195. callback->printf_filtered (callback, "# pipeline stalls %10d\n",
  1196. cpu.asregs.stalls);
  1197. callback->printf_filtered (callback, "# virtual time taken %10.4f\n",
  1198. virttime);
  1199. #ifdef WATCHFUNCTIONS
  1200. callback->printf_filtered (callback, "\nNumber of watched functions: %d\n",
  1201. ENDWL);
  1202. wcyc = 0;
  1203. for (w = 1; w <= ENDWL; w++)
  1204. {
  1205. callback->printf_filtered (callback, "WL = %s %8x\n",WLstr[w],WL[w]);
  1206. callback->printf_filtered (callback, " calls = %d, cycles = %d\n",
  1207. WLcnts[w],WLcyc[w]);
  1208. if (WLcnts[w] != 0)
  1209. callback->printf_filtered (callback,
  1210. " maxcpc = %d, mincpc = %d, avecpc = %d\n",
  1211. WLmax[w],WLmin[w],WLcyc[w]/WLcnts[w]);
  1212. wcyc += WLcyc[w];
  1213. }
  1214. callback->printf_filtered (callback,
  1215. "Total cycles for watched functions: %d\n",wcyc);
  1216. #endif
  1217. }
  1218. static sim_cia
  1219. mcore_pc_get (sim_cpu *cpu)
  1220. {
  1221. return cpu->pc;
  1222. }
  1223. static void
  1224. mcore_pc_set (sim_cpu *cpu, sim_cia pc)
  1225. {
  1226. cpu->pc = pc;
  1227. }
  1228. static void
  1229. free_state (SIM_DESC sd)
  1230. {
  1231. if (STATE_MODULES (sd) != NULL)
  1232. sim_module_uninstall (sd);
  1233. sim_cpu_free_all (sd);
  1234. sim_state_free (sd);
  1235. }
  1236. SIM_DESC
  1237. sim_open (SIM_OPEN_KIND kind, host_callback *cb, struct bfd *abfd, char **argv)
  1238. {
  1239. int i;
  1240. SIM_DESC sd = sim_state_alloc (kind, cb);
  1241. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  1242. /* The cpu data is kept in a separately allocated chunk of memory. */
  1243. if (sim_cpu_alloc_all (sd, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK)
  1244. {
  1245. free_state (sd);
  1246. return 0;
  1247. }
  1248. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  1249. {
  1250. free_state (sd);
  1251. return 0;
  1252. }
  1253. /* getopt will print the error message so we just have to exit if this fails.
  1254. FIXME: Hmmm... in the case of gdb we need getopt to call
  1255. print_filtered. */
  1256. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  1257. {
  1258. free_state (sd);
  1259. return 0;
  1260. }
  1261. /* Check for/establish the a reference program image. */
  1262. if (sim_analyze_program (sd,
  1263. (STATE_PROG_ARGV (sd) != NULL
  1264. ? *STATE_PROG_ARGV (sd)
  1265. : NULL), abfd) != SIM_RC_OK)
  1266. {
  1267. free_state (sd);
  1268. return 0;
  1269. }
  1270. /* Configure/verify the target byte order and other runtime
  1271. configuration options. */
  1272. if (sim_config (sd) != SIM_RC_OK)
  1273. {
  1274. sim_module_uninstall (sd);
  1275. return 0;
  1276. }
  1277. if (sim_post_argv_init (sd) != SIM_RC_OK)
  1278. {
  1279. /* Uninstall the modules to avoid memory leaks,
  1280. file descriptor leaks, etc. */
  1281. sim_module_uninstall (sd);
  1282. return 0;
  1283. }
  1284. /* CPU specific initialization. */
  1285. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  1286. {
  1287. SIM_CPU *cpu = STATE_CPU (sd, i);
  1288. CPU_PC_FETCH (cpu) = mcore_pc_get;
  1289. CPU_PC_STORE (cpu) = mcore_pc_set;
  1290. set_initial_gprs (cpu); /* Reset the GPR registers. */
  1291. }
  1292. /* Default to a 8 Mbyte (== 2^23) memory space. */
  1293. sim_do_commandf (sd, "memory-size %#x", DEFAULT_MEMORY_SIZE);
  1294. return sd;
  1295. }
  1296. void
  1297. sim_close (SIM_DESC sd, int quitting)
  1298. {
  1299. /* nothing to do */
  1300. }
  1301. SIM_RC
  1302. sim_create_inferior (SIM_DESC sd, struct bfd *prog_bfd, char **argv, char **env)
  1303. {
  1304. SIM_CPU *scpu = STATE_CPU (sd, 0);
  1305. char ** avp;
  1306. int nargs = 0;
  1307. int nenv = 0;
  1308. int s_length;
  1309. int l;
  1310. unsigned long strings;
  1311. unsigned long pointers;
  1312. unsigned long hi_stack;
  1313. /* Set the initial register set. */
  1314. set_initial_gprs (scpu);
  1315. hi_stack = DEFAULT_MEMORY_SIZE - 4;
  1316. CPU_PC_SET (scpu, bfd_get_start_address (prog_bfd));
  1317. /* Calculate the argument and environment strings. */
  1318. s_length = 0;
  1319. nargs = 0;
  1320. avp = argv;
  1321. while (avp && *avp)
  1322. {
  1323. l = strlen (*avp) + 1; /* include the null */
  1324. s_length += (l + 3) & ~3; /* make it a 4 byte boundary */
  1325. nargs++; avp++;
  1326. }
  1327. nenv = 0;
  1328. avp = env;
  1329. while (avp && *avp)
  1330. {
  1331. l = strlen (*avp) + 1; /* include the null */
  1332. s_length += (l + 3) & ~ 3;/* make it a 4 byte boundary */
  1333. nenv++; avp++;
  1334. }
  1335. /* Claim some memory for the pointers and strings. */
  1336. pointers = hi_stack - sizeof(word) * (nenv+1+nargs+1);
  1337. pointers &= ~3; /* must be 4-byte aligned */
  1338. cpu.gr[0] = pointers;
  1339. strings = cpu.gr[0] - s_length;
  1340. strings &= ~3; /* want to make it 4-byte aligned */
  1341. cpu.gr[0] = strings;
  1342. /* dac fix, the stack address must be 8-byte aligned! */
  1343. cpu.gr[0] = cpu.gr[0] - cpu.gr[0] % 8;
  1344. /* Loop through the arguments and fill them in. */
  1345. cpu.gr[PARM1] = nargs;
  1346. if (nargs == 0)
  1347. {
  1348. /* No strings to fill in. */
  1349. cpu.gr[PARM2] = 0;
  1350. }
  1351. else
  1352. {
  1353. cpu.gr[PARM2] = pointers;
  1354. avp = argv;
  1355. while (avp && *avp)
  1356. {
  1357. /* Save where we're putting it. */
  1358. wlat (pointers, strings);
  1359. /* Copy the string. */
  1360. l = strlen (* avp) + 1;
  1361. sim_core_write_buffer (sd, scpu, write_map, *avp, strings, l);
  1362. /* Bump the pointers. */
  1363. avp++;
  1364. pointers += 4;
  1365. strings += l+1;
  1366. }
  1367. /* A null to finish the list. */
  1368. wlat (pointers, 0);
  1369. pointers += 4;
  1370. }
  1371. /* Now do the environment pointers. */
  1372. if (nenv == 0)
  1373. {
  1374. /* No strings to fill in. */
  1375. cpu.gr[PARM3] = 0;
  1376. }
  1377. else
  1378. {
  1379. cpu.gr[PARM3] = pointers;
  1380. avp = env;
  1381. while (avp && *avp)
  1382. {
  1383. /* Save where we're putting it. */
  1384. wlat (pointers, strings);
  1385. /* Copy the string. */
  1386. l = strlen (* avp) + 1;
  1387. sim_core_write_buffer (sd, scpu, write_map, *avp, strings, l);
  1388. /* Bump the pointers. */
  1389. avp++;
  1390. pointers += 4;
  1391. strings += l+1;
  1392. }
  1393. /* A null to finish the list. */
  1394. wlat (pointers, 0);
  1395. pointers += 4;
  1396. }
  1397. return SIM_RC_OK;
  1398. }