interp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /* Simulator for the FT32 processor
  2. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. Contributed by FTDI <support@ftdichip.com>
  4. This file is part of simulators.
  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. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include <fcntl.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. #include <stdint.h>
  21. #include "bfd.h"
  22. #include "sim/callback.h"
  23. #include "libiberty.h"
  24. #include "sim/sim.h"
  25. #include "sim-main.h"
  26. #include "sim-options.h"
  27. #include "sim-signal.h"
  28. #include "opcode/ft32.h"
  29. /*
  30. * FT32 is a Harvard architecture: RAM and code occupy
  31. * different address spaces.
  32. *
  33. * sim and gdb model FT32 memory by adding 0x800000 to RAM
  34. * addresses. This means that sim/gdb can treat all addresses
  35. * similarly.
  36. *
  37. * The address space looks like:
  38. *
  39. * 00000 start of code memory
  40. * 3ffff end of code memory
  41. * 800000 start of RAM
  42. * 80ffff end of RAM
  43. */
  44. #define RAM_BIAS 0x800000 /* Bias added to RAM addresses. */
  45. static unsigned long
  46. ft32_extract_unsigned_integer (unsigned char *addr, int len)
  47. {
  48. unsigned long retval;
  49. unsigned char *p;
  50. unsigned char *startaddr = (unsigned char *) addr;
  51. unsigned char *endaddr = startaddr + len;
  52. /* Start at the most significant end of the integer, and work towards
  53. the least significant. */
  54. retval = 0;
  55. for (p = endaddr; p > startaddr;)
  56. retval = (retval << 8) | * -- p;
  57. return retval;
  58. }
  59. static void
  60. ft32_store_unsigned_integer (unsigned char *addr, int len, unsigned long val)
  61. {
  62. unsigned char *p;
  63. unsigned char *startaddr = (unsigned char *)addr;
  64. unsigned char *endaddr = startaddr + len;
  65. for (p = startaddr; p < endaddr; p++)
  66. {
  67. *p = val & 0xff;
  68. val >>= 8;
  69. }
  70. }
  71. /*
  72. * Align EA according to its size DW.
  73. * The FT32 ignores the low bit of a 16-bit addresss,
  74. * and the low two bits of a 32-bit address.
  75. */
  76. static uint32_t ft32_align (uint32_t dw, uint32_t ea)
  77. {
  78. switch (dw)
  79. {
  80. case 1:
  81. ea &= ~1;
  82. break;
  83. case 2:
  84. ea &= ~3;
  85. break;
  86. default:
  87. break;
  88. }
  89. return ea;
  90. }
  91. /* Read an item from memory address EA, sized DW. */
  92. static uint32_t
  93. ft32_read_item (SIM_DESC sd, int dw, uint32_t ea)
  94. {
  95. sim_cpu *cpu = STATE_CPU (sd, 0);
  96. address_word cia = CPU_PC_GET (cpu);
  97. uint8_t byte[4];
  98. uint32_t r;
  99. ea = ft32_align (dw, ea);
  100. switch (dw) {
  101. case 0:
  102. return sim_core_read_aligned_1 (cpu, cia, read_map, ea);
  103. case 1:
  104. return sim_core_read_aligned_2 (cpu, cia, read_map, ea);
  105. case 2:
  106. return sim_core_read_aligned_4 (cpu, cia, read_map, ea);
  107. default:
  108. abort ();
  109. }
  110. }
  111. /* Write item V to memory address EA, sized DW. */
  112. static void
  113. ft32_write_item (SIM_DESC sd, int dw, uint32_t ea, uint32_t v)
  114. {
  115. sim_cpu *cpu = STATE_CPU (sd, 0);
  116. address_word cia = CPU_PC_GET (cpu);
  117. uint8_t byte[4];
  118. ea = ft32_align (dw, ea);
  119. switch (dw) {
  120. case 0:
  121. sim_core_write_aligned_1 (cpu, cia, write_map, ea, v);
  122. break;
  123. case 1:
  124. sim_core_write_aligned_2 (cpu, cia, write_map, ea, v);
  125. break;
  126. case 2:
  127. sim_core_write_aligned_4 (cpu, cia, write_map, ea, v);
  128. break;
  129. default:
  130. abort ();
  131. }
  132. }
  133. #define ILLEGAL() \
  134. sim_engine_halt (sd, cpu, NULL, insnpc, sim_signalled, SIM_SIGILL)
  135. static uint32_t cpu_mem_read (SIM_DESC sd, uint32_t dw, uint32_t ea)
  136. {
  137. sim_cpu *cpu = STATE_CPU (sd, 0);
  138. uint32_t insnpc = cpu->state.pc;
  139. uint32_t r;
  140. uint8_t byte[4];
  141. ea &= 0x1ffff;
  142. if (ea & ~0xffff)
  143. {
  144. /* Simulate some IO devices */
  145. switch (ea)
  146. {
  147. case 0x10000:
  148. return getchar ();
  149. case 0x1fff4:
  150. /* Read the simulator cycle timer. */
  151. return cpu->state.cycles / 100;
  152. default:
  153. sim_io_eprintf (sd, "Illegal IO read address %08x, pc %#x\n",
  154. ea, insnpc);
  155. ILLEGAL ();
  156. }
  157. }
  158. return ft32_read_item (sd, dw, RAM_BIAS + ea);
  159. }
  160. static void cpu_mem_write (SIM_DESC sd, uint32_t dw, uint32_t ea, uint32_t d)
  161. {
  162. sim_cpu *cpu = STATE_CPU (sd, 0);
  163. ea &= 0x1ffff;
  164. if (ea & 0x10000)
  165. {
  166. /* Simulate some IO devices */
  167. switch (ea)
  168. {
  169. case 0x10000:
  170. /* Console output */
  171. putchar (d & 0xff);
  172. break;
  173. case 0x1fc80:
  174. /* Unlock the PM write port */
  175. cpu->state.pm_unlock = (d == 0x1337f7d1);
  176. break;
  177. case 0x1fc84:
  178. /* Set the PM write address register */
  179. cpu->state.pm_addr = d;
  180. break;
  181. case 0x1fc88:
  182. if (cpu->state.pm_unlock)
  183. {
  184. /* Write to PM. */
  185. ft32_write_item (sd, dw, cpu->state.pm_addr, d);
  186. cpu->state.pm_addr += 4;
  187. }
  188. break;
  189. case 0x1fffc:
  190. /* Normal exit. */
  191. sim_engine_halt (sd, cpu, NULL, cpu->state.pc, sim_exited, cpu->state.regs[0]);
  192. break;
  193. case 0x1fff8:
  194. sim_io_printf (sd, "Debug write %08x\n", d);
  195. break;
  196. default:
  197. sim_io_eprintf (sd, "Unknown IO write %08x to to %08x\n", d, ea);
  198. }
  199. }
  200. else
  201. ft32_write_item (sd, dw, RAM_BIAS + ea, d);
  202. }
  203. #define GET_BYTE(ea) cpu_mem_read (sd, 0, (ea))
  204. #define PUT_BYTE(ea, d) cpu_mem_write (sd, 0, (ea), (d))
  205. /* LSBS (n) is a mask of the least significant N bits. */
  206. #define LSBS(n) ((1U << (n)) - 1)
  207. static void ft32_push (SIM_DESC sd, uint32_t v)
  208. {
  209. sim_cpu *cpu = STATE_CPU (sd, 0);
  210. cpu->state.regs[FT32_HARD_SP] -= 4;
  211. cpu->state.regs[FT32_HARD_SP] &= 0xffff;
  212. cpu_mem_write (sd, 2, cpu->state.regs[FT32_HARD_SP], v);
  213. }
  214. static uint32_t ft32_pop (SIM_DESC sd)
  215. {
  216. sim_cpu *cpu = STATE_CPU (sd, 0);
  217. uint32_t r = cpu_mem_read (sd, 2, cpu->state.regs[FT32_HARD_SP]);
  218. cpu->state.regs[FT32_HARD_SP] += 4;
  219. cpu->state.regs[FT32_HARD_SP] &= 0xffff;
  220. return r;
  221. }
  222. /* Extract the low SIZ bits of N as an unsigned number. */
  223. static int nunsigned (int siz, int n)
  224. {
  225. return n & LSBS (siz);
  226. }
  227. /* Extract the low SIZ bits of N as a signed number. */
  228. static int nsigned (int siz, int n)
  229. {
  230. int shift = (sizeof (int) * 8) - siz;
  231. return (n << shift) >> shift;
  232. }
  233. /* Signed division N / D, matching hw behavior for (MIN_INT, -1). */
  234. static uint32_t ft32sdiv (uint32_t n, uint32_t d)
  235. {
  236. if (n == 0x80000000UL && d == 0xffffffffUL)
  237. return 0x80000000UL;
  238. else
  239. return (uint32_t)((int)n / (int)d);
  240. }
  241. /* Signed modulus N % D, matching hw behavior for (MIN_INT, -1). */
  242. static uint32_t ft32smod (uint32_t n, uint32_t d)
  243. {
  244. if (n == 0x80000000UL && d == 0xffffffffUL)
  245. return 0;
  246. else
  247. return (uint32_t)((int)n % (int)d);
  248. }
  249. /* Circular rotate right N by B bits. */
  250. static uint32_t ror (uint32_t n, uint32_t b)
  251. {
  252. b &= 31;
  253. return (n >> b) | (n << (32 - b));
  254. }
  255. /* Implement the BINS machine instruction.
  256. See FT32 Programmer's Reference for details. */
  257. static uint32_t bins (uint32_t d, uint32_t f, uint32_t len, uint32_t pos)
  258. {
  259. uint32_t bitmask = LSBS (len) << pos;
  260. return (d & ~bitmask) | ((f << pos) & bitmask);
  261. }
  262. /* Implement the FLIP machine instruction.
  263. See FT32 Programmer's Reference for details. */
  264. static uint32_t flip (uint32_t x, uint32_t b)
  265. {
  266. if (b & 1)
  267. x = (x & 0x55555555) << 1 | (x & 0xAAAAAAAA) >> 1;
  268. if (b & 2)
  269. x = (x & 0x33333333) << 2 | (x & 0xCCCCCCCC) >> 2;
  270. if (b & 4)
  271. x = (x & 0x0F0F0F0F) << 4 | (x & 0xF0F0F0F0) >> 4;
  272. if (b & 8)
  273. x = (x & 0x00FF00FF) << 8 | (x & 0xFF00FF00) >> 8;
  274. if (b & 16)
  275. x = (x & 0x0000FFFF) << 16 | (x & 0xFFFF0000) >> 16;
  276. return x;
  277. }
  278. static void
  279. step_once (SIM_DESC sd)
  280. {
  281. sim_cpu *cpu = STATE_CPU (sd, 0);
  282. address_word cia = CPU_PC_GET (cpu);
  283. uint32_t inst;
  284. uint32_t dw;
  285. uint32_t cb;
  286. uint32_t r_d;
  287. uint32_t cr;
  288. uint32_t cv;
  289. uint32_t bt;
  290. uint32_t r_1;
  291. uint32_t rimm;
  292. uint32_t r_2;
  293. uint32_t k20;
  294. uint32_t pa;
  295. uint32_t aa;
  296. uint32_t k16;
  297. uint32_t k15;
  298. uint32_t al;
  299. uint32_t r_1v;
  300. uint32_t rimmv;
  301. uint32_t bit_pos;
  302. uint32_t bit_len;
  303. uint32_t upper;
  304. uint32_t insnpc;
  305. unsigned int sc[2];
  306. int isize;
  307. inst = ft32_read_item (sd, 2, cpu->state.pc);
  308. cpu->state.cycles += 1;
  309. if ((STATE_ARCHITECTURE (sd)->mach == bfd_mach_ft32b)
  310. && ft32_decode_shortcode (cpu->state.pc, inst, sc))
  311. {
  312. if ((cpu->state.pc & 3) == 0)
  313. inst = sc[0];
  314. else
  315. inst = sc[1];
  316. isize = 2;
  317. }
  318. else
  319. isize = 4;
  320. /* Handle "call 8" (which is FT32's "break" equivalent) here. */
  321. if (inst == 0x00340002)
  322. {
  323. sim_engine_halt (sd, cpu, NULL,
  324. cpu->state.pc,
  325. sim_stopped, SIM_SIGTRAP);
  326. goto escape;
  327. }
  328. dw = (inst >> FT32_FLD_DW_BIT) & LSBS (FT32_FLD_DW_SIZ);
  329. cb = (inst >> FT32_FLD_CB_BIT) & LSBS (FT32_FLD_CB_SIZ);
  330. r_d = (inst >> FT32_FLD_R_D_BIT) & LSBS (FT32_FLD_R_D_SIZ);
  331. cr = (inst >> FT32_FLD_CR_BIT) & LSBS (FT32_FLD_CR_SIZ);
  332. cv = (inst >> FT32_FLD_CV_BIT) & LSBS (FT32_FLD_CV_SIZ);
  333. bt = (inst >> FT32_FLD_BT_BIT) & LSBS (FT32_FLD_BT_SIZ);
  334. r_1 = (inst >> FT32_FLD_R_1_BIT) & LSBS (FT32_FLD_R_1_SIZ);
  335. rimm = (inst >> FT32_FLD_RIMM_BIT) & LSBS (FT32_FLD_RIMM_SIZ);
  336. r_2 = (inst >> FT32_FLD_R_2_BIT) & LSBS (FT32_FLD_R_2_SIZ);
  337. k20 = nsigned (20, (inst >> FT32_FLD_K20_BIT) & LSBS (FT32_FLD_K20_SIZ));
  338. pa = (inst >> FT32_FLD_PA_BIT) & LSBS (FT32_FLD_PA_SIZ);
  339. aa = (inst >> FT32_FLD_AA_BIT) & LSBS (FT32_FLD_AA_SIZ);
  340. k16 = (inst >> FT32_FLD_K16_BIT) & LSBS (FT32_FLD_K16_SIZ);
  341. k15 = (inst >> FT32_FLD_K15_BIT) & LSBS (FT32_FLD_K15_SIZ);
  342. if (k15 & 0x80)
  343. k15 ^= 0x7f00;
  344. if (k15 & 0x4000)
  345. k15 -= 0x8000;
  346. al = (inst >> FT32_FLD_AL_BIT) & LSBS (FT32_FLD_AL_SIZ);
  347. r_1v = cpu->state.regs[r_1];
  348. rimmv = (rimm & 0x400) ? nsigned (10, rimm) : cpu->state.regs[rimm & 0x1f];
  349. bit_pos = rimmv & 31;
  350. bit_len = 0xf & (rimmv >> 5);
  351. if (bit_len == 0)
  352. bit_len = 16;
  353. upper = (inst >> 27);
  354. insnpc = cpu->state.pc;
  355. cpu->state.pc += isize;
  356. switch (upper)
  357. {
  358. case FT32_PAT_TOC:
  359. case FT32_PAT_TOCI:
  360. {
  361. int take = (cr == 3) || ((1 & (cpu->state.regs[28 + cr] >> cb)) == cv);
  362. if (take)
  363. {
  364. cpu->state.cycles += 1;
  365. if (bt)
  366. ft32_push (sd, cpu->state.pc); /* this is a call. */
  367. if (upper == FT32_PAT_TOC)
  368. cpu->state.pc = pa << 2;
  369. else
  370. cpu->state.pc = cpu->state.regs[r_2];
  371. if (cpu->state.pc == 0x8)
  372. goto escape;
  373. }
  374. }
  375. break;
  376. case FT32_PAT_ALUOP:
  377. case FT32_PAT_CMPOP:
  378. {
  379. uint32_t result;
  380. switch (al)
  381. {
  382. case 0x0: result = r_1v + rimmv; break;
  383. case 0x1: result = ror (r_1v, rimmv); break;
  384. case 0x2: result = r_1v - rimmv; break;
  385. case 0x3: result = (r_1v << 10) | (1023 & rimmv); break;
  386. case 0x4: result = r_1v & rimmv; break;
  387. case 0x5: result = r_1v | rimmv; break;
  388. case 0x6: result = r_1v ^ rimmv; break;
  389. case 0x7: result = ~(r_1v ^ rimmv); break;
  390. case 0x8: result = r_1v << rimmv; break;
  391. case 0x9: result = r_1v >> rimmv; break;
  392. case 0xa: result = (int32_t)r_1v >> rimmv; break;
  393. case 0xb: result = bins (r_1v, rimmv >> 10, bit_len, bit_pos); break;
  394. case 0xc: result = nsigned (bit_len, r_1v >> bit_pos); break;
  395. case 0xd: result = nunsigned (bit_len, r_1v >> bit_pos); break;
  396. case 0xe: result = flip (r_1v, rimmv); break;
  397. default:
  398. sim_io_eprintf (sd, "Unhandled alu %#x\n", al);
  399. ILLEGAL ();
  400. }
  401. if (upper == FT32_PAT_ALUOP)
  402. cpu->state.regs[r_d] = result;
  403. else
  404. {
  405. uint32_t dwmask = 0;
  406. int dwsiz = 0;
  407. int zero;
  408. int sign;
  409. int ahi;
  410. int bhi;
  411. int overflow;
  412. int carry;
  413. int bit;
  414. uint64_t ra;
  415. uint64_t rb;
  416. int above;
  417. int greater;
  418. int greatereq;
  419. switch (dw)
  420. {
  421. case 0: dwsiz = 7; dwmask = 0xffU; break;
  422. case 1: dwsiz = 15; dwmask = 0xffffU; break;
  423. case 2: dwsiz = 31; dwmask = 0xffffffffU; break;
  424. }
  425. zero = (0 == (result & dwmask));
  426. sign = 1 & (result >> dwsiz);
  427. ahi = 1 & (r_1v >> dwsiz);
  428. bhi = 1 & (rimmv >> dwsiz);
  429. overflow = (sign != ahi) & (ahi == !bhi);
  430. bit = (dwsiz + 1);
  431. ra = r_1v & dwmask;
  432. rb = rimmv & dwmask;
  433. switch (al)
  434. {
  435. case 0x0: carry = 1 & ((ra + rb) >> bit); break;
  436. case 0x2: carry = 1 & ((ra - rb) >> bit); break;
  437. default: carry = 0; break;
  438. }
  439. above = (!carry & !zero);
  440. greater = (sign == overflow) & !zero;
  441. greatereq = (sign == overflow);
  442. cpu->state.regs[r_d] = (
  443. (above << 6) |
  444. (greater << 5) |
  445. (greatereq << 4) |
  446. (sign << 3) |
  447. (overflow << 2) |
  448. (carry << 1) |
  449. (zero << 0));
  450. }
  451. }
  452. break;
  453. case FT32_PAT_LDK:
  454. cpu->state.regs[r_d] = k20;
  455. break;
  456. case FT32_PAT_LPM:
  457. cpu->state.regs[r_d] = ft32_read_item (sd, dw, pa << 2);
  458. cpu->state.cycles += 1;
  459. break;
  460. case FT32_PAT_LPMI:
  461. cpu->state.regs[r_d] = ft32_read_item (sd, dw, cpu->state.regs[r_1] + k15);
  462. cpu->state.cycles += 1;
  463. break;
  464. case FT32_PAT_STA:
  465. cpu_mem_write (sd, dw, aa, cpu->state.regs[r_d]);
  466. break;
  467. case FT32_PAT_STI:
  468. cpu_mem_write (sd, dw, cpu->state.regs[r_d] + k15, cpu->state.regs[r_1]);
  469. break;
  470. case FT32_PAT_LDA:
  471. cpu->state.regs[r_d] = cpu_mem_read (sd, dw, aa);
  472. cpu->state.cycles += 1;
  473. break;
  474. case FT32_PAT_LDI:
  475. cpu->state.regs[r_d] = cpu_mem_read (sd, dw, cpu->state.regs[r_1] + k15);
  476. cpu->state.cycles += 1;
  477. break;
  478. case FT32_PAT_EXA:
  479. {
  480. uint32_t tmp;
  481. tmp = cpu_mem_read (sd, dw, aa);
  482. cpu_mem_write (sd, dw, aa, cpu->state.regs[r_d]);
  483. cpu->state.regs[r_d] = tmp;
  484. cpu->state.cycles += 1;
  485. }
  486. break;
  487. case FT32_PAT_EXI:
  488. {
  489. uint32_t tmp;
  490. tmp = cpu_mem_read (sd, dw, cpu->state.regs[r_1] + k15);
  491. cpu_mem_write (sd, dw, cpu->state.regs[r_1] + k15, cpu->state.regs[r_d]);
  492. cpu->state.regs[r_d] = tmp;
  493. cpu->state.cycles += 1;
  494. }
  495. break;
  496. case FT32_PAT_PUSH:
  497. ft32_push (sd, r_1v);
  498. break;
  499. case FT32_PAT_LINK:
  500. ft32_push (sd, cpu->state.regs[r_d]);
  501. cpu->state.regs[r_d] = cpu->state.regs[FT32_HARD_SP];
  502. cpu->state.regs[FT32_HARD_SP] -= k16;
  503. cpu->state.regs[FT32_HARD_SP] &= 0xffff;
  504. break;
  505. case FT32_PAT_UNLINK:
  506. cpu->state.regs[FT32_HARD_SP] = cpu->state.regs[r_d];
  507. cpu->state.regs[FT32_HARD_SP] &= 0xffff;
  508. cpu->state.regs[r_d] = ft32_pop (sd);
  509. break;
  510. case FT32_PAT_POP:
  511. cpu->state.cycles += 1;
  512. cpu->state.regs[r_d] = ft32_pop (sd);
  513. break;
  514. case FT32_PAT_RETURN:
  515. cpu->state.pc = ft32_pop (sd);
  516. break;
  517. case FT32_PAT_FFUOP:
  518. switch (al)
  519. {
  520. case 0x0:
  521. cpu->state.regs[r_d] = r_1v / rimmv;
  522. break;
  523. case 0x1:
  524. cpu->state.regs[r_d] = r_1v % rimmv;
  525. break;
  526. case 0x2:
  527. cpu->state.regs[r_d] = ft32sdiv (r_1v, rimmv);
  528. break;
  529. case 0x3:
  530. cpu->state.regs[r_d] = ft32smod (r_1v, rimmv);
  531. break;
  532. case 0x4:
  533. {
  534. /* strcmp instruction. */
  535. uint32_t a = r_1v;
  536. uint32_t b = rimmv;
  537. uint32_t i = 0;
  538. while ((GET_BYTE (a + i) != 0) &&
  539. (GET_BYTE (a + i) == GET_BYTE (b + i)))
  540. i++;
  541. cpu->state.regs[r_d] = GET_BYTE (a + i) - GET_BYTE (b + i);
  542. }
  543. break;
  544. case 0x5:
  545. {
  546. /* memcpy instruction. */
  547. uint32_t src = r_1v;
  548. uint32_t dst = cpu->state.regs[r_d];
  549. uint32_t i;
  550. for (i = 0; i < (rimmv & 0x7fff); i++)
  551. PUT_BYTE (dst + i, GET_BYTE (src + i));
  552. }
  553. break;
  554. case 0x6:
  555. {
  556. /* strlen instruction. */
  557. uint32_t src = r_1v;
  558. uint32_t i;
  559. for (i = 0; GET_BYTE (src + i) != 0; i++)
  560. ;
  561. cpu->state.regs[r_d] = i;
  562. }
  563. break;
  564. case 0x7:
  565. {
  566. /* memset instruction. */
  567. uint32_t dst = cpu->state.regs[r_d];
  568. uint32_t i;
  569. for (i = 0; i < (rimmv & 0x7fff); i++)
  570. PUT_BYTE (dst + i, r_1v);
  571. }
  572. break;
  573. case 0x8:
  574. cpu->state.regs[r_d] = r_1v * rimmv;
  575. break;
  576. case 0x9:
  577. cpu->state.regs[r_d] = ((uint64_t)r_1v * (uint64_t)rimmv) >> 32;
  578. break;
  579. case 0xa:
  580. {
  581. /* stpcpy instruction. */
  582. uint32_t src = r_1v;
  583. uint32_t dst = cpu->state.regs[r_d];
  584. uint32_t i;
  585. for (i = 0; GET_BYTE (src + i) != 0; i++)
  586. PUT_BYTE (dst + i, GET_BYTE (src + i));
  587. PUT_BYTE (dst + i, 0);
  588. cpu->state.regs[r_d] = dst + i;
  589. }
  590. break;
  591. case 0xe:
  592. {
  593. /* streamout instruction. */
  594. uint32_t i;
  595. uint32_t src = cpu->state.regs[r_1];
  596. for (i = 0; i < rimmv; i += (1 << dw))
  597. {
  598. cpu_mem_write (sd,
  599. dw,
  600. cpu->state.regs[r_d],
  601. cpu_mem_read (sd, dw, src));
  602. src += (1 << dw);
  603. }
  604. }
  605. break;
  606. default:
  607. sim_io_eprintf (sd, "Unhandled ffu %#x at %08x\n", al, insnpc);
  608. ILLEGAL ();
  609. }
  610. break;
  611. default:
  612. sim_io_eprintf (sd, "Unhandled pattern %d at %08x\n", upper, insnpc);
  613. ILLEGAL ();
  614. }
  615. cpu->state.num_i++;
  616. escape:
  617. ;
  618. }
  619. void
  620. sim_engine_run (SIM_DESC sd,
  621. int next_cpu_nr, /* ignore */
  622. int nr_cpus, /* ignore */
  623. int siggnal) /* ignore */
  624. {
  625. sim_cpu *cpu;
  626. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  627. cpu = STATE_CPU (sd, 0);
  628. while (1)
  629. {
  630. step_once (sd);
  631. if (sim_events_tick (sd))
  632. sim_events_process (sd);
  633. }
  634. }
  635. static uint32_t *
  636. ft32_lookup_register (SIM_CPU *cpu, int nr)
  637. {
  638. /* Handle the register number translation here.
  639. * Sim registers are 0-31.
  640. * Other tools (gcc, gdb) use:
  641. * 0 - fp
  642. * 1 - sp
  643. * 2 - r0
  644. * 31 - cc
  645. */
  646. if ((nr < 0) || (nr > 32))
  647. {
  648. sim_io_eprintf (CPU_STATE (cpu), "unknown register %i\n", nr);
  649. abort ();
  650. }
  651. switch (nr)
  652. {
  653. case FT32_FP_REGNUM:
  654. return &cpu->state.regs[FT32_HARD_FP];
  655. case FT32_SP_REGNUM:
  656. return &cpu->state.regs[FT32_HARD_SP];
  657. case FT32_CC_REGNUM:
  658. return &cpu->state.regs[FT32_HARD_CC];
  659. case FT32_PC_REGNUM:
  660. return &cpu->state.pc;
  661. default:
  662. return &cpu->state.regs[nr - 2];
  663. }
  664. }
  665. static int
  666. ft32_reg_store (SIM_CPU *cpu,
  667. int rn,
  668. unsigned char *memory,
  669. int length)
  670. {
  671. if (0 <= rn && rn <= 32)
  672. {
  673. if (length == 4)
  674. *ft32_lookup_register (cpu, rn) = ft32_extract_unsigned_integer (memory, 4);
  675. return 4;
  676. }
  677. else
  678. return 0;
  679. }
  680. static int
  681. ft32_reg_fetch (SIM_CPU *cpu,
  682. int rn,
  683. unsigned char *memory,
  684. int length)
  685. {
  686. if (0 <= rn && rn <= 32)
  687. {
  688. if (length == 4)
  689. ft32_store_unsigned_integer (memory, 4, *ft32_lookup_register (cpu, rn));
  690. return 4;
  691. }
  692. else
  693. return 0;
  694. }
  695. static sim_cia
  696. ft32_pc_get (SIM_CPU *cpu)
  697. {
  698. return cpu->state.pc;
  699. }
  700. static void
  701. ft32_pc_set (SIM_CPU *cpu, sim_cia newpc)
  702. {
  703. cpu->state.pc = newpc;
  704. }
  705. /* Cover function of sim_state_free to free the cpu buffers as well. */
  706. static void
  707. free_state (SIM_DESC sd)
  708. {
  709. if (STATE_MODULES (sd) != NULL)
  710. sim_module_uninstall (sd);
  711. sim_cpu_free_all (sd);
  712. sim_state_free (sd);
  713. }
  714. SIM_DESC
  715. sim_open (SIM_OPEN_KIND kind,
  716. host_callback *cb,
  717. struct bfd *abfd,
  718. char * const *argv)
  719. {
  720. char c;
  721. size_t i;
  722. SIM_DESC sd = sim_state_alloc (kind, cb);
  723. /* Set default options before parsing user options. */
  724. current_alignment = STRICT_ALIGNMENT;
  725. current_target_byte_order = BFD_ENDIAN_LITTLE;
  726. /* The cpu data is kept in a separately allocated chunk of memory. */
  727. if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
  728. {
  729. free_state (sd);
  730. return 0;
  731. }
  732. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  733. {
  734. free_state (sd);
  735. return 0;
  736. }
  737. /* The parser will print an error message for us, so we silently return. */
  738. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  739. {
  740. free_state (sd);
  741. return 0;
  742. }
  743. /* Allocate external memory if none specified by user.
  744. Use address 4 here in case the user wanted address 0 unmapped. */
  745. if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0)
  746. {
  747. sim_do_command (sd, "memory region 0x00000000,0x40000");
  748. sim_do_command (sd, "memory region 0x800000,0x10000");
  749. }
  750. /* Check for/establish the reference program image. */
  751. if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
  752. {
  753. free_state (sd);
  754. return 0;
  755. }
  756. /* Configure/verify the target byte order and other runtime
  757. configuration options. */
  758. if (sim_config (sd) != SIM_RC_OK)
  759. {
  760. free_state (sd);
  761. return 0;
  762. }
  763. if (sim_post_argv_init (sd) != SIM_RC_OK)
  764. {
  765. free_state (sd);
  766. return 0;
  767. }
  768. /* CPU specific initialization. */
  769. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  770. {
  771. SIM_CPU *cpu = STATE_CPU (sd, i);
  772. CPU_REG_FETCH (cpu) = ft32_reg_fetch;
  773. CPU_REG_STORE (cpu) = ft32_reg_store;
  774. CPU_PC_FETCH (cpu) = ft32_pc_get;
  775. CPU_PC_STORE (cpu) = ft32_pc_set;
  776. }
  777. return sd;
  778. }
  779. SIM_RC
  780. sim_create_inferior (SIM_DESC sd,
  781. struct bfd *abfd,
  782. char * const *argv,
  783. char * const *env)
  784. {
  785. uint32_t addr;
  786. sim_cpu *cpu = STATE_CPU (sd, 0);
  787. host_callback *cb = STATE_CALLBACK (sd);
  788. /* Set the PC. */
  789. if (abfd != NULL)
  790. addr = bfd_get_start_address (abfd);
  791. else
  792. addr = 0;
  793. /* Standalone mode (i.e. `run`) will take care of the argv for us in
  794. sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
  795. with `gdb`), we need to handle it because the user can change the
  796. argv on the fly via gdb's 'run'. */
  797. if (STATE_PROG_ARGV (sd) != argv)
  798. {
  799. freeargv (STATE_PROG_ARGV (sd));
  800. STATE_PROG_ARGV (sd) = dupargv (argv);
  801. }
  802. if (STATE_PROG_ENVP (sd) != env)
  803. {
  804. freeargv (STATE_PROG_ENVP (sd));
  805. STATE_PROG_ENVP (sd) = dupargv (env);
  806. }
  807. cb->argv = STATE_PROG_ARGV (sd);
  808. cb->envp = STATE_PROG_ENVP (sd);
  809. cpu->state.regs[FT32_HARD_SP] = addr;
  810. cpu->state.num_i = 0;
  811. cpu->state.cycles = 0;
  812. cpu->state.next_tick_cycle = 100000;
  813. return SIM_RC_OK;
  814. }