Update copyright year in gdbarch.sh doc/gdb.texinfo and doc/refcard.tex
[binutils-gdb.git] / sim / sh64 / sh64.c
blob7d5546d1c6bb13e0bbce4f0eeedcbebc19bc75c9
1 /* SH5 simulator support code
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3 Contributed by Red Hat, Inc.
5 This file is part of the GNU simulators.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #define WANT_CPU
21 #define WANT_CPU_SH64
23 #include "sim-main.h"
24 #include "sim-fpu.h"
25 #include "cgen-mem.h"
26 #include "cgen-ops.h"
28 #include "gdb/callback.h"
29 #include "defs-compact.h"
31 #include "bfd.h"
32 /* From include/gdb/. */
33 #include "gdb/sim-sh.h"
35 #define SYS_exit 1
36 #define SYS_read 3
37 #define SYS_write 4
38 #define SYS_open 5
39 #define SYS_close 6
40 #define SYS_lseek 19
41 #define SYS_time 23
42 #define SYS_argc 172
43 #define SYS_argnlen 173
44 #define SYS_argn 174
46 IDESC * sh64_idesc_media;
47 IDESC * sh64_idesc_compact;
50 sh64_endian (SIM_CPU *current_cpu)
52 return (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN);
56 sh64_fldi0 (SIM_CPU *current_cpu)
58 SF result;
59 sim_fpu_to32 (&result, &sim_fpu_zero);
60 return result;
64 sh64_fldi1 (SIM_CPU *current_cpu)
66 SF result;
67 sim_fpu_to32 (&result, &sim_fpu_one);
68 return result;
72 sh64_fabsd(SIM_CPU *current_cpu, DF drgh)
74 DF result;
75 sim_fpu f, fres;
77 sim_fpu_64to (&f, drgh);
78 sim_fpu_abs (&fres, &f);
79 sim_fpu_to64 (&result, &fres);
80 return result;
84 sh64_fabss(SIM_CPU *current_cpu, SF frgh)
86 SF result;
87 sim_fpu f, fres;
89 sim_fpu_32to (&f, frgh);
90 sim_fpu_abs (&fres, &f);
91 sim_fpu_to32 (&result, &fres);
92 return result;
96 sh64_faddd(SIM_CPU *current_cpu, DF drg, DF drh)
98 DF result;
99 sim_fpu f1, f2, fres;
101 sim_fpu_64to (&f1, drg);
102 sim_fpu_64to (&f2, drh);
103 sim_fpu_add (&fres, &f1, &f2);
104 sim_fpu_to64 (&result, &fres);
105 return result;
109 sh64_fadds(SIM_CPU *current_cpu, SF frg, SF frh)
111 SF result;
112 sim_fpu f1, f2, fres;
114 sim_fpu_32to (&f1, frg);
115 sim_fpu_32to (&f2, frh);
116 sim_fpu_add (&fres, &f1, &f2);
117 sim_fpu_to32 (&result, &fres);
118 return result;
122 sh64_fcmpeqd(SIM_CPU *current_cpu, DF drg, DF drh)
124 sim_fpu f1, f2;
126 sim_fpu_64to (&f1, drg);
127 sim_fpu_64to (&f2, drh);
128 return sim_fpu_is_eq (&f1, &f2);
132 sh64_fcmpeqs(SIM_CPU *current_cpu, SF frg, SF frh)
134 sim_fpu f1, f2;
136 sim_fpu_32to (&f1, frg);
137 sim_fpu_32to (&f2, frh);
138 return sim_fpu_is_eq (&f1, &f2);
142 sh64_fcmpged(SIM_CPU *current_cpu, DF drg, DF drh)
144 sim_fpu f1, f2;
146 sim_fpu_64to (&f1, drg);
147 sim_fpu_64to (&f2, drh);
148 return sim_fpu_is_ge (&f1, &f2);
152 sh64_fcmpges(SIM_CPU *current_cpu, SF frg, SF frh)
154 sim_fpu f1, f2;
156 sim_fpu_32to (&f1, frg);
157 sim_fpu_32to (&f2, frh);
158 return sim_fpu_is_ge (&f1, &f2);
162 sh64_fcmpgtd(SIM_CPU *current_cpu, DF drg, DF drh)
164 sim_fpu f1, f2;
166 sim_fpu_64to (&f1, drg);
167 sim_fpu_64to (&f2, drh);
168 return sim_fpu_is_gt (&f1, &f2);
172 sh64_fcmpgts(SIM_CPU *current_cpu, SF frg, SF frh)
174 sim_fpu f1, f2;
176 sim_fpu_32to (&f1, frg);
177 sim_fpu_32to (&f2, frh);
178 return sim_fpu_is_gt (&f1, &f2);
182 sh64_fcmpund(SIM_CPU *current_cpu, DF drg, DF drh)
184 sim_fpu f1, f2;
186 sim_fpu_64to (&f1, drg);
187 sim_fpu_64to (&f2, drh);
188 return (sim_fpu_is_nan (&f1) || sim_fpu_is_nan (&f2));
192 sh64_fcmpuns(SIM_CPU *current_cpu, SF frg, SF frh)
194 sim_fpu f1, f2;
196 sim_fpu_32to (&f1, frg);
197 sim_fpu_32to (&f2, frh);
198 return (sim_fpu_is_nan (&f1) || sim_fpu_is_nan (&f2));
202 sh64_fcnvds(SIM_CPU *current_cpu, DF drgh)
204 union {
205 unsigned long long ll;
206 double d;
207 } f1;
209 union {
210 unsigned long l;
211 float f;
212 } f2;
214 f1.ll = drgh;
215 f2.f = (float) f1.d;
217 return (SF) f2.l;
221 sh64_fcnvsd(SIM_CPU *current_cpu, SF frgh)
223 DF result;
224 sim_fpu f;
226 sim_fpu_32to (&f, frgh);
227 sim_fpu_to64 (&result, &f);
228 return result;
232 sh64_fdivd(SIM_CPU *current_cpu, DF drg, DF drh)
234 DF result;
235 sim_fpu f1, f2, fres;
237 sim_fpu_64to (&f1, drg);
238 sim_fpu_64to (&f2, drh);
239 sim_fpu_div (&fres, &f1, &f2);
240 sim_fpu_to64 (&result, &fres);
241 return result;
245 sh64_fdivs(SIM_CPU *current_cpu, SF frg, SF frh)
247 SF result;
248 sim_fpu f1, f2, fres;
250 sim_fpu_32to (&f1, frg);
251 sim_fpu_32to (&f2, frh);
252 sim_fpu_div (&fres, &f1, &f2);
253 sim_fpu_to32 (&result, &fres);
254 return result;
258 sh64_floatld(SIM_CPU *current_cpu, SF frgh)
260 DF result;
261 sim_fpu f;
263 sim_fpu_i32to (&f, frgh, sim_fpu_round_default);
264 sim_fpu_to64 (&result, &f);
265 return result;
269 sh64_floatls(SIM_CPU *current_cpu, SF frgh)
271 SF result;
272 sim_fpu f;
274 sim_fpu_i32to (&f, frgh, sim_fpu_round_default);
275 sim_fpu_to32 (&result, &f);
276 return result;
280 sh64_floatqd(SIM_CPU *current_cpu, DF drgh)
282 DF result;
283 sim_fpu f;
285 sim_fpu_i64to (&f, drgh, sim_fpu_round_default);
286 sim_fpu_to64 (&result, &f);
287 return result;
291 sh64_floatqs(SIM_CPU *current_cpu, DF drgh)
293 SF result;
294 sim_fpu f;
296 sim_fpu_i64to (&f, drgh, sim_fpu_round_default);
297 sim_fpu_to32 (&result, &f);
298 return result;
302 sh64_fmacs(SIM_CPU *current_cpu, SF fr0, SF frm, SF frn)
304 SF result;
305 sim_fpu m1, m2, a1, fres;
307 sim_fpu_32to (&m1, fr0);
308 sim_fpu_32to (&m2, frm);
309 sim_fpu_32to (&a1, frn);
311 sim_fpu_mul (&fres, &m1, &m2);
312 sim_fpu_add (&fres, &fres, &a1);
314 sim_fpu_to32 (&result, &fres);
315 return result;
319 sh64_fmuld(SIM_CPU *current_cpu, DF drg, DF drh)
321 DF result;
322 sim_fpu f1, f2, fres;
324 sim_fpu_64to (&f1, drg);
325 sim_fpu_64to (&f2, drh);
326 sim_fpu_mul (&fres, &f1, &f2);
327 sim_fpu_to64 (&result, &fres);
328 return result;
332 sh64_fmuls(SIM_CPU *current_cpu, SF frg, SF frh)
334 SF result;
335 sim_fpu f1, f2, fres;
337 sim_fpu_32to (&f1, frg);
338 sim_fpu_32to (&f2, frh);
339 sim_fpu_mul (&fres, &f1, &f2);
340 sim_fpu_to32 (&result, &fres);
341 return result;
345 sh64_fnegd(SIM_CPU *current_cpu, DF drgh)
347 DF result;
348 sim_fpu f1, f2;
350 sim_fpu_64to (&f1, drgh);
351 sim_fpu_neg (&f2, &f1);
352 sim_fpu_to64 (&result, &f2);
353 return result;
357 sh64_fnegs(SIM_CPU *current_cpu, SF frgh)
359 SF result;
360 sim_fpu f, fres;
362 sim_fpu_32to (&f, frgh);
363 sim_fpu_neg (&fres, &f);
364 sim_fpu_to32 (&result, &fres);
365 return result;
369 sh64_fsqrtd(SIM_CPU *current_cpu, DF drgh)
371 DF result;
372 sim_fpu f, fres;
374 sim_fpu_64to (&f, drgh);
375 sim_fpu_sqrt (&fres, &f);
376 sim_fpu_to64 (&result, &fres);
377 return result;
381 sh64_fsqrts(SIM_CPU *current_cpu, SF frgh)
383 SF result;
384 sim_fpu f, fres;
386 sim_fpu_32to (&f, frgh);
387 sim_fpu_sqrt (&fres, &f);
388 sim_fpu_to32 (&result, &fres);
389 return result;
393 sh64_fsubd(SIM_CPU *current_cpu, DF drg, DF drh)
395 DF result;
396 sim_fpu f1, f2, fres;
398 sim_fpu_64to (&f1, drg);
399 sim_fpu_64to (&f2, drh);
400 sim_fpu_sub (&fres, &f1, &f2);
401 sim_fpu_to64 (&result, &fres);
402 return result;
406 sh64_fsubs(SIM_CPU *current_cpu, SF frg, SF frh)
408 SF result;
409 sim_fpu f1, f2, fres;
411 sim_fpu_32to (&f1, frg);
412 sim_fpu_32to (&f2, frh);
413 sim_fpu_sub (&fres, &f1, &f2);
414 sim_fpu_to32 (&result, &fres);
415 return result;
419 sh64_ftrcdl(SIM_CPU *current_cpu, DF drgh)
421 SI result;
422 sim_fpu f;
424 sim_fpu_64to (&f, drgh);
425 sim_fpu_to32i (&result, &f, sim_fpu_round_zero);
426 return (SF) result;
430 sh64_ftrcsl(SIM_CPU *current_cpu, SF frgh)
432 SI result;
433 sim_fpu f;
435 sim_fpu_32to (&f, frgh);
436 sim_fpu_to32i (&result, &f, sim_fpu_round_zero);
437 return (SF) result;
441 sh64_ftrcdq(SIM_CPU *current_cpu, DF drgh)
443 DI result;
444 sim_fpu f;
446 sim_fpu_64to (&f, drgh);
447 sim_fpu_to64i (&result, &f, sim_fpu_round_zero);
448 return (DF) result;
452 sh64_ftrcsq(SIM_CPU *current_cpu, SF frgh)
454 DI result;
455 sim_fpu f;
457 sim_fpu_32to (&f, frgh);
458 sim_fpu_to64i (&result, &f, sim_fpu_round_zero);
459 return (DF) result;
462 VOID
463 sh64_ftrvs(SIM_CPU *cpu, unsigned g, unsigned h, unsigned f)
465 int i, j;
467 for (i = 0; i < 4; i++)
469 SF result;
470 sim_fpu sum;
471 sim_fpu_32to (&sum, 0);
473 for (j = 0; j < 4; j++)
475 sim_fpu f1, f2, temp;
476 sim_fpu_32to (&f1, sh64_h_fr_get (cpu, (g + i) + (j * 4)));
477 sim_fpu_32to (&f2, sh64_h_fr_get (cpu, h + j));
478 sim_fpu_mul (&temp, &f1, &f2);
479 sim_fpu_add (&sum, &sum, &temp);
481 sim_fpu_to32 (&result, &sum);
482 sh64_h_fr_set (cpu, f + i, result);
486 VOID
487 sh64_fipr (SIM_CPU *cpu, unsigned m, unsigned n)
489 SF result = sh64_fmuls (cpu, sh64_h_fvc_get (cpu, m), sh64_h_fvc_get (cpu, n));
490 result = sh64_fadds (cpu, result, sh64_fmuls (cpu, sh64_h_frc_get (cpu, m + 1), sh64_h_frc_get (cpu, n + 1)));
491 result = sh64_fadds (cpu, result, sh64_fmuls (cpu, sh64_h_frc_get (cpu, m + 2), sh64_h_frc_get (cpu, n + 2)));
492 result = sh64_fadds (cpu, result, sh64_fmuls (cpu, sh64_h_frc_get (cpu, m + 3), sh64_h_frc_get (cpu, n + 3)));
493 sh64_h_frc_set (cpu, n + 3, result);
497 sh64_fiprs (SIM_CPU *cpu, unsigned g, unsigned h)
499 SF temp = sh64_fmuls (cpu, sh64_h_fr_get (cpu, g), sh64_h_fr_get (cpu, h));
500 temp = sh64_fadds (cpu, temp, sh64_fmuls (cpu, sh64_h_fr_get (cpu, g + 1), sh64_h_fr_get (cpu, h + 1)));
501 temp = sh64_fadds (cpu, temp, sh64_fmuls (cpu, sh64_h_fr_get (cpu, g + 2), sh64_h_fr_get (cpu, h + 2)));
502 temp = sh64_fadds (cpu, temp, sh64_fmuls (cpu, sh64_h_fr_get (cpu, g + 3), sh64_h_fr_get (cpu, h + 3)));
503 return temp;
506 VOID
507 sh64_fldp (SIM_CPU *cpu, PCADDR pc, DI rm, DI rn, unsigned f)
509 sh64_h_fr_set (cpu, f, GETMEMSF (cpu, pc, rm + rn));
510 sh64_h_fr_set (cpu, f + 1, GETMEMSF (cpu, pc, rm + rn + 4));
513 VOID
514 sh64_fstp (SIM_CPU *cpu, PCADDR pc, DI rm, DI rn, unsigned f)
516 SETMEMSF (cpu, pc, rm + rn, sh64_h_fr_get (cpu, f));
517 SETMEMSF (cpu, pc, rm + rn + 4, sh64_h_fr_get (cpu, f + 1));
520 VOID
521 sh64_ftrv (SIM_CPU *cpu, UINT ignored)
523 /* TODO: Unimplemented. */
526 VOID
527 sh64_pref (SIM_CPU *cpu, SI addr)
529 /* TODO: Unimplemented. */
532 /* Read a null terminated string from memory, return in a buffer */
533 static char *
534 fetch_str (current_cpu, pc, addr)
535 SIM_CPU *current_cpu;
536 PCADDR pc;
537 DI addr;
539 char *buf;
540 int nr = 0;
541 while (sim_core_read_1 (current_cpu,
542 pc, read_map, addr + nr) != 0)
543 nr++;
544 buf = NZALLOC (char, nr + 1);
545 sim_read (CPU_STATE (current_cpu), addr, buf, nr);
546 return buf;
549 static void
550 trap_handler (SIM_CPU *current_cpu, int shmedia_abi_p, UQI trapnum, PCADDR pc)
552 char ch;
553 switch (trapnum)
555 case 1:
556 ch = GET_H_GRC (0);
557 sim_io_write_stdout (CPU_STATE (current_cpu), &ch, 1);
558 fflush (stdout);
559 break;
560 case 2:
561 sim_engine_halt (CPU_STATE (current_cpu), current_cpu, NULL, pc, sim_stopped, SIM_SIGTRAP);
562 break;
563 case 34:
565 int i;
566 int ret_reg = (shmedia_abi_p) ? 2 : 0;
567 char *buf;
568 DI PARM1 = GET_H_GR ((shmedia_abi_p) ? 3 : 5);
569 DI PARM2 = GET_H_GR ((shmedia_abi_p) ? 4 : 6);
570 DI PARM3 = GET_H_GR ((shmedia_abi_p) ? 5 : 7);
572 switch (GET_H_GR ((shmedia_abi_p) ? 2 : 4))
574 case SYS_write:
575 buf = zalloc (PARM3);
576 sim_read (CPU_STATE (current_cpu), PARM2, buf, PARM3);
577 SET_H_GR (ret_reg,
578 sim_io_write (CPU_STATE (current_cpu),
579 PARM1, buf, PARM3));
580 free (buf);
581 break;
583 case SYS_lseek:
584 SET_H_GR (ret_reg,
585 sim_io_lseek (CPU_STATE (current_cpu),
586 PARM1, PARM2, PARM3));
587 break;
589 case SYS_exit:
590 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
591 NULL, pc, sim_exited, PARM1);
592 break;
594 case SYS_read:
595 buf = zalloc (PARM3);
596 SET_H_GR (ret_reg,
597 sim_io_read (CPU_STATE (current_cpu),
598 PARM1, buf, PARM3));
599 sim_write (CPU_STATE (current_cpu), PARM2, buf, PARM3);
600 free (buf);
601 break;
603 case SYS_open:
604 buf = fetch_str (current_cpu, pc, PARM1);
605 SET_H_GR (ret_reg,
606 sim_io_open (CPU_STATE (current_cpu),
607 buf, PARM2));
608 free (buf);
609 break;
611 case SYS_close:
612 SET_H_GR (ret_reg,
613 sim_io_close (CPU_STATE (current_cpu), PARM1));
614 break;
616 case SYS_time:
617 SET_H_GR (ret_reg, time (0));
618 break;
620 case SYS_argc:
621 SET_H_GR (ret_reg, countargv (STATE_PROG_ARGV (CPU_STATE (current_cpu))));
622 break;
624 case SYS_argnlen:
625 if (PARM1 < countargv (STATE_PROG_ARGV (CPU_STATE (current_cpu))))
626 SET_H_GR (ret_reg,
627 strlen (STATE_PROG_ARGV (CPU_STATE (current_cpu)) [PARM1]));
628 else
629 SET_H_GR (ret_reg, -1);
630 break;
632 case SYS_argn:
633 if (PARM1 < countargv (STATE_PROG_ARGV (CPU_STATE (current_cpu))))
635 /* Include the NULL byte. */
636 i = strlen (STATE_PROG_ARGV (CPU_STATE (current_cpu)) [PARM1]) + 1;
637 sim_write (CPU_STATE (current_cpu),
638 PARM2,
639 STATE_PROG_ARGV (CPU_STATE (current_cpu)) [PARM1],
642 /* Just for good measure. */
643 SET_H_GR (ret_reg, i);
644 break;
646 else
647 SET_H_GR (ret_reg, -1);
648 break;
650 default:
651 SET_H_GR (ret_reg, -1);
654 break;
655 case 253:
656 puts ("pass");
657 exit (0);
658 case 254:
659 puts ("fail");
660 exit (1);
661 case 0xc3:
662 /* fall through. */
663 case 255:
664 sim_engine_halt (CPU_STATE (current_cpu), current_cpu, NULL, pc, sim_stopped, SIM_SIGTRAP);
665 break;
669 void
670 sh64_trapa (SIM_CPU *current_cpu, DI rm, PCADDR pc)
672 trap_handler (current_cpu, 1, (UQI) rm & 0xff, pc);
675 void
676 sh64_compact_trapa (SIM_CPU *current_cpu, UQI trapnum, PCADDR pc)
678 int mach_sh5_p;
680 /* If this is an SH5 executable, this is SHcompact code running in
681 the SHmedia ABI. */
683 mach_sh5_p =
684 (bfd_get_mach (STATE_PROG_BFD (CPU_STATE (current_cpu))) == bfd_mach_sh5);
686 trap_handler (current_cpu, mach_sh5_p, trapnum, pc);
690 sh64_nsb (SIM_CPU *current_cpu, DI rm)
692 int result = 0, count;
693 UDI source = (UDI) rm;
695 if ((source >> 63))
696 source = ~source;
697 source <<= 1;
699 for (count = 32; count; count >>= 1)
701 UDI newval = source << count;
703 if ((newval >> count) == source)
705 result |= count;
706 source = newval;
710 return result;
713 void
714 sh64_break (SIM_CPU *current_cpu, PCADDR pc)
716 SIM_DESC sd = CPU_STATE (current_cpu);
717 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGTRAP);
721 sh64_movua (SIM_CPU *current_cpu, PCADDR pc, SI rn)
723 SI v;
724 int i;
726 /* Move the data one byte at a time to avoid alignment problems.
727 Be aware of endianness. */
728 v = 0;
729 for (i = 0; i < 4; ++i)
730 v = (v << 8) | (GETMEMQI (current_cpu, pc, rn + i) & 0xff);
732 v = T2H_4 (v);
733 return v;
736 void
737 set_isa (SIM_CPU *current_cpu, int mode)
739 /* Do nothing. */
742 /* The semantic code invokes this for invalid (unrecognized) instructions. */
744 SEM_PC
745 sim_engine_invalid_insn (SIM_CPU *current_cpu, IADDR cia, SEM_PC vpc)
747 SIM_DESC sd = CPU_STATE (current_cpu);
748 sim_engine_halt (sd, current_cpu, NULL, cia, sim_stopped, SIM_SIGILL);
750 return vpc;
754 /* Process an address exception. */
756 void
757 sh64_core_signal (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia,
758 unsigned int map, int nr_bytes, address_word addr,
759 transfer_type transfer, sim_core_signals sig)
761 sim_core_signal (sd, current_cpu, cia, map, nr_bytes, addr,
762 transfer, sig);
766 /* Initialize cycle counting for an insn.
767 FIRST_P is non-zero if this is the first insn in a set of parallel
768 insns. */
770 void
771 sh64_compact_model_insn_before (SIM_CPU *cpu, int first_p)
773 /* Do nothing. */
776 void
777 sh64_media_model_insn_before (SIM_CPU *cpu, int first_p)
779 /* Do nothing. */
782 /* Record the cycles computed for an insn.
783 LAST_P is non-zero if this is the last insn in a set of parallel insns,
784 and we update the total cycle count.
785 CYCLES is the cycle count of the insn. */
787 void
788 sh64_compact_model_insn_after(SIM_CPU *cpu, int last_p, int cycles)
790 /* Do nothing. */
793 void
794 sh64_media_model_insn_after(SIM_CPU *cpu, int last_p, int cycles)
796 /* Do nothing. */
800 sh64_fetch_register (SIM_CPU *cpu, int nr, unsigned char *buf, int len)
802 /* Fetch general purpose registers. */
803 if (nr >= SIM_SH64_R0_REGNUM
804 && nr < (SIM_SH64_R0_REGNUM + SIM_SH64_NR_R_REGS)
805 && len == 8)
807 *((unsigned64*) buf) =
808 H2T_8 (sh64_h_gr_get (cpu, nr - SIM_SH64_R0_REGNUM));
809 return len;
812 /* Fetch PC. */
813 if (nr == SIM_SH64_PC_REGNUM && len == 8)
815 *((unsigned64*) buf) = H2T_8 (sh64_h_pc_get (cpu) | sh64_h_ism_get (cpu));
816 return len;
819 /* Fetch status register (SR). */
820 if (nr == SIM_SH64_SR_REGNUM && len == 8)
822 *((unsigned64*) buf) = H2T_8 (sh64_h_sr_get (cpu));
823 return len;
826 /* Fetch saved status register (SSR) and PC (SPC). */
827 if ((nr == SIM_SH64_SSR_REGNUM || nr == SIM_SH64_SPC_REGNUM)
828 && len == 8)
830 *((unsigned64*) buf) = 0;
831 return len;
834 /* Fetch target registers. */
835 if (nr >= SIM_SH64_TR0_REGNUM
836 && nr < (SIM_SH64_TR0_REGNUM + SIM_SH64_NR_TR_REGS)
837 && len == 8)
839 *((unsigned64*) buf) =
840 H2T_8 (sh64_h_tr_get (cpu, nr - SIM_SH64_TR0_REGNUM));
841 return len;
844 /* Fetch floating point registers. */
845 if (nr >= SIM_SH64_FR0_REGNUM
846 && nr < (SIM_SH64_FR0_REGNUM + SIM_SH64_NR_FP_REGS)
847 && len == 4)
849 *((unsigned32*) buf) =
850 H2T_4 (sh64_h_fr_get (cpu, nr - SIM_SH64_FR0_REGNUM));
851 return len;
854 /* We should never get here. */
855 return 0;
859 sh64_store_register (SIM_CPU *cpu, int nr, unsigned char *buf, int len)
861 /* Store general purpose registers. */
862 if (nr >= SIM_SH64_R0_REGNUM
863 && nr < (SIM_SH64_R0_REGNUM + SIM_SH64_NR_R_REGS)
864 && len == 8)
866 sh64_h_gr_set (cpu, nr - SIM_SH64_R0_REGNUM, T2H_8 (*((unsigned64*)buf)));
867 return len;
870 /* Store PC. */
871 if (nr == SIM_SH64_PC_REGNUM && len == 8)
873 unsigned64 new_pc = T2H_8 (*((unsigned64*)buf));
874 sh64_h_pc_set (cpu, new_pc);
875 return len;
878 /* Store status register (SR). */
879 if (nr == SIM_SH64_SR_REGNUM && len == 8)
881 sh64_h_sr_set (cpu, T2H_8 (*((unsigned64*)buf)));
882 return len;
885 /* Store saved status register (SSR) and PC (SPC). */
886 if (nr == SIM_SH64_SSR_REGNUM || nr == SIM_SH64_SPC_REGNUM)
888 /* Do nothing. */
889 return len;
892 /* Store target registers. */
893 if (nr >= SIM_SH64_TR0_REGNUM
894 && nr < (SIM_SH64_TR0_REGNUM + SIM_SH64_NR_TR_REGS)
895 && len == 8)
897 sh64_h_tr_set (cpu, nr - SIM_SH64_TR0_REGNUM, T2H_8 (*((unsigned64*)buf)));
898 return len;
901 /* Store floating point registers. */
902 if (nr >= SIM_SH64_FR0_REGNUM
903 && nr < (SIM_SH64_FR0_REGNUM + SIM_SH64_NR_FP_REGS)
904 && len == 4)
906 sh64_h_fr_set (cpu, nr - SIM_SH64_FR0_REGNUM, T2H_4 (*((unsigned32*)buf)));
907 return len;
910 /* We should never get here. */
911 return 0;
914 void
915 sh64_engine_run_full(SIM_CPU *cpu)
917 if (sh64_h_ism_get (cpu) == ISM_MEDIA)
919 if (!sh64_idesc_media)
921 sh64_media_init_idesc_table (cpu);
922 sh64_idesc_media = CPU_IDESC (cpu);
924 else
925 CPU_IDESC (cpu) = sh64_idesc_media;
926 sh64_media_engine_run_full (cpu);
928 else
930 if (!sh64_idesc_compact)
932 sh64_compact_init_idesc_table (cpu);
933 sh64_idesc_compact = CPU_IDESC (cpu);
935 else
936 CPU_IDESC (cpu) = sh64_idesc_compact;
937 sh64_compact_engine_run_full (cpu);
941 void
942 sh64_engine_run_fast (SIM_CPU *cpu)
944 if (sh64_h_ism_get (cpu) == ISM_MEDIA)
946 if (!sh64_idesc_media)
948 sh64_media_init_idesc_table (cpu);
949 sh64_idesc_media = CPU_IDESC (cpu);
951 else
952 CPU_IDESC (cpu) = sh64_idesc_media;
953 sh64_media_engine_run_fast (cpu);
955 else
957 if (!sh64_idesc_compact)
959 sh64_compact_init_idesc_table (cpu);
960 sh64_idesc_compact = CPU_IDESC (cpu);
962 else
963 CPU_IDESC (cpu) = sh64_idesc_compact;
964 sh64_compact_engine_run_fast (cpu);
968 static void
969 sh64_prepare_run (SIM_CPU *cpu)
971 /* Nothing. */
974 static const CGEN_INSN *
975 sh64_get_idata (SIM_CPU *cpu, int inum)
977 return CPU_IDESC (cpu) [inum].idata;
980 static void
981 sh64_init_cpu (SIM_CPU *cpu)
983 CPU_REG_FETCH (cpu) = sh64_fetch_register;
984 CPU_REG_STORE (cpu) = sh64_store_register;
985 CPU_PC_FETCH (cpu) = sh64_h_pc_get;
986 CPU_PC_STORE (cpu) = sh64_h_pc_set;
987 CPU_GET_IDATA (cpu) = sh64_get_idata;
988 /* Only used by profiling. 0 disables it. */
989 CPU_MAX_INSNS (cpu) = 0;
990 CPU_INSN_NAME (cpu) = cgen_insn_name;
991 CPU_FULL_ENGINE_FN (cpu) = sh64_engine_run_full;
992 #if WITH_FAST
993 CPU_FAST_ENGINE_FN (cpu) = sh64_engine_run_fast;
994 #else
995 CPU_FAST_ENGINE_FN (cpu) = sh64_engine_run_full;
996 #endif
999 static void
1000 shmedia_init_cpu (SIM_CPU *cpu)
1002 sh64_init_cpu (cpu);
1005 static void
1006 shcompact_init_cpu (SIM_CPU *cpu)
1008 sh64_init_cpu (cpu);
1011 static void
1012 sh64_model_init()
1014 /* Do nothing. */
1017 static const SIM_MODEL sh_models [] =
1019 { "sh2", & sh2_mach, MODEL_SH5, NULL, sh64_model_init },
1020 { "sh2e", & sh2e_mach, MODEL_SH5, NULL, sh64_model_init },
1021 { "sh2a", & sh2a_fpu_mach, MODEL_SH5, NULL, sh64_model_init },
1022 { "sh2a_nofpu", & sh2a_nofpu_mach, MODEL_SH5, NULL, sh64_model_init },
1023 { "sh3", & sh3_mach, MODEL_SH5, NULL, sh64_model_init },
1024 { "sh3e", & sh3_mach, MODEL_SH5, NULL, sh64_model_init },
1025 { "sh4", & sh4_mach, MODEL_SH5, NULL, sh64_model_init },
1026 { "sh4_nofpu", & sh4_nofpu_mach, MODEL_SH5, NULL, sh64_model_init },
1027 { "sh4a", & sh4a_mach, MODEL_SH5, NULL, sh64_model_init },
1028 { "sh4a_nofpu", & sh4a_nofpu_mach, MODEL_SH5, NULL, sh64_model_init },
1029 { "sh4al", & sh4al_mach, MODEL_SH5, NULL, sh64_model_init },
1030 { "sh5", & sh5_mach, MODEL_SH5, NULL, sh64_model_init },
1031 { 0 }
1034 static const SIM_MACH_IMP_PROPERTIES sh5_imp_properties =
1036 sizeof (SIM_CPU),
1037 #if WITH_SCACHE
1038 sizeof (SCACHE)
1039 #else
1041 #endif
1044 const SIM_MACH sh2_mach =
1046 "sh2", "sh2", MACH_SH5,
1047 16, 16, &sh_models[0], &sh5_imp_properties,
1048 shcompact_init_cpu,
1049 sh64_prepare_run
1052 const SIM_MACH sh2e_mach =
1054 "sh2e", "sh2e", MACH_SH5,
1055 16, 16, &sh_models[1], &sh5_imp_properties,
1056 shcompact_init_cpu,
1057 sh64_prepare_run
1060 const SIM_MACH sh2a_fpu_mach =
1062 "sh2a", "sh2a", MACH_SH5,
1063 16, 16, &sh_models[2], &sh5_imp_properties,
1064 shcompact_init_cpu,
1065 sh64_prepare_run
1068 const SIM_MACH sh2a_nofpu_mach =
1070 "sh2a_nofpu", "sh2a_nofpu", MACH_SH5,
1071 16, 16, &sh_models[3], &sh5_imp_properties,
1072 shcompact_init_cpu,
1073 sh64_prepare_run
1076 const SIM_MACH sh3_mach =
1078 "sh3", "sh3", MACH_SH5,
1079 16, 16, &sh_models[4], &sh5_imp_properties,
1080 shcompact_init_cpu,
1081 sh64_prepare_run
1084 const SIM_MACH sh3e_mach =
1086 "sh3e", "sh3e", MACH_SH5,
1087 16, 16, &sh_models[5], &sh5_imp_properties,
1088 shcompact_init_cpu,
1089 sh64_prepare_run
1092 const SIM_MACH sh4_mach =
1094 "sh4", "sh4", MACH_SH5,
1095 16, 16, &sh_models[6], &sh5_imp_properties,
1096 shcompact_init_cpu,
1097 sh64_prepare_run
1100 const SIM_MACH sh4_nofpu_mach =
1102 "sh4_nofpu", "sh4_nofpu", MACH_SH5,
1103 16, 16, &sh_models[7], &sh5_imp_properties,
1104 shcompact_init_cpu,
1105 sh64_prepare_run
1108 const SIM_MACH sh4a_mach =
1110 "sh4a", "sh4a", MACH_SH5,
1111 16, 16, &sh_models[8], &sh5_imp_properties,
1112 shcompact_init_cpu,
1113 sh64_prepare_run
1116 const SIM_MACH sh4a_nofpu_mach =
1118 "sh4a_nofpu", "sh4a_nofpu", MACH_SH5,
1119 16, 16, &sh_models[9], &sh5_imp_properties,
1120 shcompact_init_cpu,
1121 sh64_prepare_run
1124 const SIM_MACH sh4al_mach =
1126 "sh4al", "sh4al", MACH_SH5,
1127 16, 16, &sh_models[10], &sh5_imp_properties,
1128 shcompact_init_cpu,
1129 sh64_prepare_run
1132 const SIM_MACH sh5_mach =
1134 "sh5", "sh5", MACH_SH5,
1135 32, 32, &sh_models[11], &sh5_imp_properties,
1136 shmedia_init_cpu,
1137 sh64_prepare_run