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