1 /* Simulator for the moxie processor
2 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
3 Contributed by Anthony Green
5 This file is part of GDB, the GNU debugger.
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/>. */
23 #include <sys/times.h>
24 #include <sys/param.h>
25 #include <netinet/in.h> /* for byte ordering macros */
27 #include "gdb/callback.h"
28 #include "libiberty.h"
29 #include "gdb/remote-sim.h"
35 typedef unsigned int uword;
37 host_callback * callback;
41 /* Extract the signed 10-bit offset from a 16-bit branch
43 #define INST2OFFSET(o) ((((signed short)((o & ((1<<10)-1))<<6))>>6)<<1)
45 #define EXTRACT_WORD(addr) \
46 ((sim_core_read_aligned_1 (scpu, cia, read_map, addr) << 24) \
47 + (sim_core_read_aligned_1 (scpu, cia, read_map, addr+1) << 16) \
48 + (sim_core_read_aligned_1 (scpu, cia, read_map, addr+2) << 8) \
49 + (sim_core_read_aligned_1 (scpu, cia, read_map, addr+3)))
52 moxie_extract_unsigned_integer (addr, len)
58 unsigned char * startaddr = (unsigned char *)addr;
59 unsigned char * endaddr = startaddr + len;
61 if (len > (int) sizeof (unsigned long))
62 printf ("That operation is not available on integers of more than %d bytes.",
63 sizeof (unsigned long));
65 /* Start at the most significant end of the integer, and work towards
66 the least significant. */
69 for (p = endaddr; p > startaddr;)
70 retval = (retval << 8) | * -- p;
76 moxie_store_unsigned_integer (addr, len, val)
82 unsigned char * startaddr = (unsigned char *)addr;
83 unsigned char * endaddr = startaddr + len;
85 for (p = endaddr; p > startaddr;)
92 /* moxie register names. */
93 static const char *reg_names[16] =
94 { "$fp", "$sp", "$r0", "$r1", "$r2", "$r3", "$r4", "$r5",
95 "$r6", "$r7", "$r8", "$r9", "$r10", "$r11", "$r12", "$r13" };
99 This state is maintained in host byte order. The fetch/store
100 register functions must translate between host byte order and the
101 target processor byte order. Keeping this data in target byte
102 order simplifies the register read/write functions. Keeping this
103 data in native order improves the performance of the simulator.
104 Simulation speed is deemed more important. */
106 #define NUM_MOXIE_REGS 17 /* Including PC */
107 #define NUM_MOXIE_SREGS 256 /* The special registers */
110 /* The ordering of the moxie_regset structure is matched in the
111 gdb/config/moxie/tm-moxie.h file in the REGISTER_NAMES macro. */
114 word regs[NUM_MOXIE_REGS + 1]; /* primary registers */
115 word sregs[256]; /* special registers */
116 word cc; /* the condition code reg */
118 unsigned long long insts; /* instruction counter */
129 struct moxie_regset asregs;
130 word asints [1]; /* but accessed larger... */
134 static SIM_OPEN_KIND sim_kind;
135 static int issue_messages = 0;
148 /* Set up machine just out of reset. */
149 cpu.asregs.regs[PC_REGNO] = 0;
151 /* Clean out the register contents. */
152 for (i = 0; i < NUM_MOXIE_REGS; i++)
153 cpu.asregs.regs[i] = 0;
154 for (i = 0; i < NUM_MOXIE_SREGS; i++)
155 cpu.asregs.sregs[i] = 0;
161 cpu.asregs.exception = SIGINT;
164 /* Write a 1 byte value to memory. */
167 wbat (sim_cpu *scpu, word pc, word x, word v)
169 address_word cia = CIA_GET (scpu);
171 sim_core_write_aligned_1 (scpu, cia, write_map, x, v);
174 /* Write a 2 byte value to memory. */
177 wsat (sim_cpu *scpu, word pc, word x, word v)
179 address_word cia = CIA_GET (scpu);
181 sim_core_write_aligned_2 (scpu, cia, write_map, x, v);
184 /* Write a 4 byte value to memory. */
187 wlat (sim_cpu *scpu, word pc, word x, word v)
189 address_word cia = CIA_GET (scpu);
191 sim_core_write_aligned_4 (scpu, cia, write_map, x, v);
194 /* Read 2 bytes from memory. */
197 rsat (sim_cpu *scpu, word pc, word x)
199 address_word cia = CIA_GET (scpu);
201 return (sim_core_read_aligned_2 (scpu, cia, read_map, x));
204 /* Read 1 byte from memory. */
207 rbat (sim_cpu *scpu, word pc, word x)
209 address_word cia = CIA_GET (scpu);
211 return (sim_core_read_aligned_1 (scpu, cia, read_map, x));
214 /* Read 4 bytes from memory. */
217 rlat (sim_cpu *scpu, word pc, word x)
219 address_word cia = CIA_GET (scpu);
221 return (sim_core_read_aligned_4 (scpu, cia, read_map, x));
224 #define CHECK_FLAG(T,H) if (tflags & T) { hflags |= H; tflags ^= T; }
227 convert_target_flags (unsigned int tflags)
229 unsigned int hflags = 0x0;
231 CHECK_FLAG(0x0001, O_WRONLY);
232 CHECK_FLAG(0x0002, O_RDWR);
233 CHECK_FLAG(0x0008, O_APPEND);
234 CHECK_FLAG(0x0200, O_CREAT);
235 CHECK_FLAG(0x0400, O_TRUNC);
236 CHECK_FLAG(0x0800, O_EXCL);
237 CHECK_FLAG(0x2000, O_SYNC);
241 "Simulator Error: problem converting target open flags for host. 0x%x\n",
247 #define TRACE(str) if (tracing) fprintf(tracefile,"0x%08x, %s, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", opc, str, cpu.asregs.regs[0], cpu.asregs.regs[1], cpu.asregs.regs[2], cpu.asregs.regs[3], cpu.asregs.regs[4], cpu.asregs.regs[5], cpu.asregs.regs[6], cpu.asregs.regs[7], cpu.asregs.regs[8], cpu.asregs.regs[9], cpu.asregs.regs[10], cpu.asregs.regs[11], cpu.asregs.regs[12], cpu.asregs.regs[13], cpu.asregs.regs[14], cpu.asregs.regs[15]);
249 static int tracing = 0;
252 sim_resume (sd, step, siggnal)
257 unsigned long long insts;
260 sim_cpu *scpu = STATE_CPU (sd, 0); /* FIXME */
261 address_word cia = CIA_GET (scpu);
263 sigsave = signal (SIGINT, interrupt);
264 cpu.asregs.exception = step ? SIGTRAP: 0;
265 pc = cpu.asregs.regs[PC_REGNO];
266 insts = cpu.asregs.insts;
268 /* Run instructions here. */
273 /* Fetch the instruction at pc. */
274 inst = (sim_core_read_aligned_1 (scpu, cia, read_map, pc) << 8)
275 + sim_core_read_aligned_1 (scpu, cia, read_map, pc+1);
277 /* Decode instruction. */
278 if (inst & (1 << 15))
280 if (inst & (1 << 14))
282 /* This is a Form 3 instruction. */
283 int opcode = (inst >> 10 & 0xf);
290 if (cpu.asregs.cc & CC_EQ)
291 pc += INST2OFFSET(inst) - 2;
297 if (! (cpu.asregs.cc & CC_EQ))
298 pc += INST2OFFSET(inst) - 2;
304 if (cpu.asregs.cc & CC_LT)
305 pc += INST2OFFSET(inst) - 2;
310 if (cpu.asregs.cc & CC_GT)
311 pc += INST2OFFSET(inst) - 2;
314 case 0x04: /* bltu */
317 if (cpu.asregs.cc & CC_LTU)
318 pc += INST2OFFSET(inst) - 2;
321 case 0x05: /* bgtu */
324 if (cpu.asregs.cc & CC_GTU)
325 pc += INST2OFFSET(inst) - 2;
331 if (cpu.asregs.cc & (CC_GT | CC_EQ))
332 pc += INST2OFFSET(inst) - 2;
338 if (cpu.asregs.cc & (CC_LT | CC_EQ))
339 pc += INST2OFFSET(inst) - 2;
342 case 0x08: /* bgeu */
345 if (cpu.asregs.cc & (CC_GTU | CC_EQ))
346 pc += INST2OFFSET(inst) - 2;
349 case 0x09: /* bleu */
352 if (cpu.asregs.cc & (CC_LTU | CC_EQ))
353 pc += INST2OFFSET(inst) - 2;
359 cpu.asregs.exception = SIGILL;
366 /* This is a Form 2 instruction. */
367 int opcode = (inst >> 12 & 0x3);
372 int a = (inst >> 8) & 0xf;
373 unsigned av = cpu.asregs.regs[a];
374 unsigned v = (inst & 0xff);
376 cpu.asregs.regs[a] = av + v;
381 int a = (inst >> 8) & 0xf;
382 unsigned av = cpu.asregs.regs[a];
383 unsigned v = (inst & 0xff);
385 cpu.asregs.regs[a] = av - v;
390 int a = (inst >> 8) & 0xf;
391 unsigned v = (inst & 0xff);
393 cpu.asregs.regs[a] = cpu.asregs.sregs[v];
398 int a = (inst >> 8) & 0xf;
399 unsigned v = (inst & 0xff);
401 cpu.asregs.sregs[v] = cpu.asregs.regs[a];
406 cpu.asregs.exception = SIGILL;
413 /* This is a Form 1 instruction. */
414 int opcode = inst >> 8;
419 case 0x01: /* ldi.l (immediate) */
421 int reg = (inst >> 4) & 0xf;
423 unsigned int val = EXTRACT_WORD(pc+2);
424 cpu.asregs.regs[reg] = val;
428 case 0x02: /* mov (register-to-register) */
430 int dest = (inst >> 4) & 0xf;
431 int src = (inst ) & 0xf;
433 cpu.asregs.regs[dest] = cpu.asregs.regs[src];
436 case 0x03: /* jsra */
438 unsigned int fn = EXTRACT_WORD(pc+2);
439 unsigned int sp = cpu.asregs.regs[1];
441 /* Save a slot for the static chain. */
444 /* Push the return address. */
446 wlat (scpu, opc, sp, pc + 6);
448 /* Push the current frame pointer. */
450 wlat (scpu, opc, sp, cpu.asregs.regs[0]);
452 /* Uncache the stack pointer and set the pc and $fp. */
453 cpu.asregs.regs[1] = sp;
454 cpu.asregs.regs[0] = sp;
460 unsigned int sp = cpu.asregs.regs[0];
464 /* Pop the frame pointer. */
465 cpu.asregs.regs[0] = rlat (scpu, opc, sp);
468 /* Pop the return address. */
469 pc = rlat (scpu, opc, sp) - 2;
472 /* Skip over the static chain slot. */
475 /* Uncache the stack pointer. */
476 cpu.asregs.regs[1] = sp;
479 case 0x05: /* add.l */
481 int a = (inst >> 4) & 0xf;
483 unsigned av = cpu.asregs.regs[a];
484 unsigned bv = cpu.asregs.regs[b];
486 cpu.asregs.regs[a] = av + bv;
489 case 0x06: /* push */
491 int a = (inst >> 4) & 0xf;
493 int sp = cpu.asregs.regs[a] - 4;
495 wlat (scpu, opc, sp, cpu.asregs.regs[b]);
496 cpu.asregs.regs[a] = sp;
501 int a = (inst >> 4) & 0xf;
503 int sp = cpu.asregs.regs[a];
505 cpu.asregs.regs[b] = rlat (scpu, opc, sp);
506 cpu.asregs.regs[a] = sp + 4;
509 case 0x08: /* lda.l */
511 int reg = (inst >> 4) & 0xf;
512 unsigned int addr = EXTRACT_WORD(pc+2);
514 cpu.asregs.regs[reg] = rlat (scpu, opc, addr);
518 case 0x09: /* sta.l */
520 int reg = (inst >> 4) & 0xf;
521 unsigned int addr = EXTRACT_WORD(pc+2);
523 wlat (scpu, opc, addr, cpu.asregs.regs[reg]);
527 case 0x0a: /* ld.l (register indirect) */
529 int src = inst & 0xf;
530 int dest = (inst >> 4) & 0xf;
533 xv = cpu.asregs.regs[src];
534 cpu.asregs.regs[dest] = rlat (scpu, opc, xv);
537 case 0x0b: /* st.l */
539 int dest = (inst >> 4) & 0xf;
540 int val = inst & 0xf;
542 wlat (scpu, opc, cpu.asregs.regs[dest], cpu.asregs.regs[val]);
545 case 0x0c: /* ldo.l */
547 unsigned int addr = EXTRACT_WORD(pc+2);
548 int a = (inst >> 4) & 0xf;
551 addr += cpu.asregs.regs[b];
552 cpu.asregs.regs[a] = rlat (scpu, opc, addr);
556 case 0x0d: /* sto.l */
558 unsigned int addr = EXTRACT_WORD(pc+2);
559 int a = (inst >> 4) & 0xf;
562 addr += cpu.asregs.regs[a];
563 wlat (scpu, opc, addr, cpu.asregs.regs[b]);
569 int a = (inst >> 4) & 0xf;
572 int va = cpu.asregs.regs[a];
573 int vb = cpu.asregs.regs[b];
581 cc |= (va < vb ? CC_LT : 0);
582 cc |= (va > vb ? CC_GT : 0);
583 cc |= ((unsigned int) va < (unsigned int) vb ? CC_LTU : 0);
584 cc |= ((unsigned int) va > (unsigned int) vb ? CC_GTU : 0);
603 cpu.asregs.exception = SIGILL;
608 unsigned int fn = cpu.asregs.regs[(inst >> 4) & 0xf];
609 unsigned int sp = cpu.asregs.regs[1];
613 /* Save a slot for the static chain. */
616 /* Push the return address. */
618 wlat (scpu, opc, sp, pc + 2);
620 /* Push the current frame pointer. */
622 wlat (scpu, opc, sp, cpu.asregs.regs[0]);
624 /* Uncache the stack pointer and set the fp & pc. */
625 cpu.asregs.regs[1] = sp;
626 cpu.asregs.regs[0] = sp;
630 case 0x1a: /* jmpa */
632 unsigned int tgt = EXTRACT_WORD(pc+2);
637 case 0x1b: /* ldi.b (immediate) */
639 int reg = (inst >> 4) & 0xf;
641 unsigned int val = EXTRACT_WORD(pc+2);
643 cpu.asregs.regs[reg] = val;
647 case 0x1c: /* ld.b (register indirect) */
649 int src = inst & 0xf;
650 int dest = (inst >> 4) & 0xf;
653 xv = cpu.asregs.regs[src];
654 cpu.asregs.regs[dest] = rbat (scpu, opc, xv);
657 case 0x1d: /* lda.b */
659 int reg = (inst >> 4) & 0xf;
660 unsigned int addr = EXTRACT_WORD(pc+2);
662 cpu.asregs.regs[reg] = rbat (scpu, opc, addr);
666 case 0x1e: /* st.b */
668 int dest = (inst >> 4) & 0xf;
669 int val = inst & 0xf;
671 wbat (scpu, opc, cpu.asregs.regs[dest], cpu.asregs.regs[val]);
674 case 0x1f: /* sta.b */
676 int reg = (inst >> 4) & 0xf;
677 unsigned int addr = EXTRACT_WORD(pc+2);
679 wbat (scpu, opc, addr, cpu.asregs.regs[reg]);
683 case 0x20: /* ldi.s (immediate) */
685 int reg = (inst >> 4) & 0xf;
687 unsigned int val = EXTRACT_WORD(pc+2);
689 cpu.asregs.regs[reg] = val;
693 case 0x21: /* ld.s (register indirect) */
695 int src = inst & 0xf;
696 int dest = (inst >> 4) & 0xf;
699 xv = cpu.asregs.regs[src];
700 cpu.asregs.regs[dest] = rsat (scpu, opc, xv);
703 case 0x22: /* lda.s */
705 int reg = (inst >> 4) & 0xf;
706 unsigned int addr = EXTRACT_WORD(pc+2);
708 cpu.asregs.regs[reg] = rsat (scpu, opc, addr);
712 case 0x23: /* st.s */
714 int dest = (inst >> 4) & 0xf;
715 int val = inst & 0xf;
717 wsat (scpu, opc, cpu.asregs.regs[dest], cpu.asregs.regs[val]);
720 case 0x24: /* sta.s */
722 int reg = (inst >> 4) & 0xf;
723 unsigned int addr = EXTRACT_WORD(pc+2);
725 wsat (scpu, opc, addr, cpu.asregs.regs[reg]);
731 int reg = (inst >> 4) & 0xf;
733 pc = cpu.asregs.regs[reg] - 2;
738 int a = (inst >> 4) & 0xf;
742 av = cpu.asregs.regs[a];
743 bv = cpu.asregs.regs[b];
744 cpu.asregs.regs[a] = av & bv;
747 case 0x27: /* lshr */
749 int a = (inst >> 4) & 0xf;
751 int av = cpu.asregs.regs[a];
752 int bv = cpu.asregs.regs[b];
754 cpu.asregs.regs[a] = (unsigned) ((unsigned) av >> bv);
757 case 0x28: /* ashl */
759 int a = (inst >> 4) & 0xf;
761 int av = cpu.asregs.regs[a];
762 int bv = cpu.asregs.regs[b];
764 cpu.asregs.regs[a] = av << bv;
767 case 0x29: /* sub.l */
769 int a = (inst >> 4) & 0xf;
771 unsigned av = cpu.asregs.regs[a];
772 unsigned bv = cpu.asregs.regs[b];
774 cpu.asregs.regs[a] = av - bv;
779 int a = (inst >> 4) & 0xf;
781 int bv = cpu.asregs.regs[b];
783 cpu.asregs.regs[a] = - bv;
788 int a = (inst >> 4) & 0xf;
792 av = cpu.asregs.regs[a];
793 bv = cpu.asregs.regs[b];
794 cpu.asregs.regs[a] = av | bv;
799 int a = (inst >> 4) & 0xf;
801 int bv = cpu.asregs.regs[b];
803 cpu.asregs.regs[a] = 0xffffffff ^ bv;
806 case 0x2d: /* ashr */
808 int a = (inst >> 4) & 0xf;
810 int av = cpu.asregs.regs[a];
811 int bv = cpu.asregs.regs[b];
813 cpu.asregs.regs[a] = av >> bv;
818 int a = (inst >> 4) & 0xf;
822 av = cpu.asregs.regs[a];
823 bv = cpu.asregs.regs[b];
824 cpu.asregs.regs[a] = av ^ bv;
827 case 0x2f: /* mul.l */
829 int a = (inst >> 4) & 0xf;
831 unsigned av = cpu.asregs.regs[a];
832 unsigned bv = cpu.asregs.regs[b];
834 cpu.asregs.regs[a] = av * bv;
839 unsigned int inum = EXTRACT_WORD(pc+2);
841 /* Set the special registers appropriately. */
842 cpu.asregs.sregs[2] = 3; /* MOXIE_EX_SWI */
843 cpu.asregs.sregs[3] = inum;
846 case 0x1: /* SYS_exit */
848 cpu.asregs.exception = SIGQUIT;
851 case 0x2: /* SYS_open */
854 int mode = (int) convert_target_flags ((unsigned) cpu.asregs.regs[3]);
855 int perm = (int) cpu.asregs.regs[4];
856 int fd = open (fname, mode, perm);
857 sim_core_read_buffer (sd, scpu, read_map, fname,
858 cpu.asregs.regs[2], 1024);
859 /* FIXME - set errno */
860 cpu.asregs.regs[2] = fd;
863 case 0x4: /* SYS_read */
865 int fd = cpu.asregs.regs[2];
866 unsigned len = (unsigned) cpu.asregs.regs[4];
867 char *buf = malloc (len);
868 cpu.asregs.regs[2] = read (fd, buf, len);
869 sim_core_write_buffer (sd, scpu, write_map, buf,
870 cpu.asregs.regs[3], len);
874 case 0x5: /* SYS_write */
877 /* String length is at 0x12($fp) */
878 unsigned count, len = (unsigned) cpu.asregs.regs[4];
880 sim_core_read_buffer (sd, scpu, read_map, str,
881 cpu.asregs.regs[3], len);
882 count = write (cpu.asregs.regs[2], str, len);
884 cpu.asregs.regs[2] = count;
887 case 0xffffffff: /* Linux System Call */
889 unsigned int handler = cpu.asregs.sregs[1];
890 unsigned int sp = cpu.asregs.regs[1];
892 /* Save a slot for the static chain. */
895 /* Push the return address. */
897 wlat (scpu, opc, sp, pc + 6);
899 /* Push the current frame pointer. */
901 wlat (scpu, opc, sp, cpu.asregs.regs[0]);
903 /* Uncache the stack pointer and set the fp & pc. */
904 cpu.asregs.regs[1] = sp;
905 cpu.asregs.regs[0] = sp;
914 case 0x31: /* div.l */
916 int a = (inst >> 4) & 0xf;
918 int av = cpu.asregs.regs[a];
919 int bv = cpu.asregs.regs[b];
921 cpu.asregs.regs[a] = av / bv;
924 case 0x32: /* udiv.l */
926 int a = (inst >> 4) & 0xf;
928 unsigned int av = cpu.asregs.regs[a];
929 unsigned int bv = cpu.asregs.regs[b];
931 cpu.asregs.regs[a] = (av / bv);
934 case 0x33: /* mod.l */
936 int a = (inst >> 4) & 0xf;
938 int av = cpu.asregs.regs[a];
939 int bv = cpu.asregs.regs[b];
941 cpu.asregs.regs[a] = av % bv;
944 case 0x34: /* umod.l */
946 int a = (inst >> 4) & 0xf;
948 unsigned int av = cpu.asregs.regs[a];
949 unsigned int bv = cpu.asregs.regs[b];
951 cpu.asregs.regs[a] = (av % bv);
956 cpu.asregs.exception = SIGTRAP;
957 pc -= 2; /* Adjust pc */
959 case 0x36: /* ldo.b */
961 unsigned int addr = EXTRACT_WORD(pc+2);
962 int a = (inst >> 4) & 0xf;
965 addr += cpu.asregs.regs[b];
966 cpu.asregs.regs[a] = rbat (scpu, opc, addr);
970 case 0x37: /* sto.b */
972 unsigned int addr = EXTRACT_WORD(pc+2);
973 int a = (inst >> 4) & 0xf;
976 addr += cpu.asregs.regs[a];
977 wbat (scpu, opc, addr, cpu.asregs.regs[b]);
981 case 0x38: /* ldo.s */
983 unsigned int addr = EXTRACT_WORD(pc+2);
984 int a = (inst >> 4) & 0xf;
987 addr += cpu.asregs.regs[b];
988 cpu.asregs.regs[a] = rsat (scpu, opc, addr);
992 case 0x39: /* sto.s */
994 unsigned int addr = EXTRACT_WORD(pc+2);
995 int a = (inst >> 4) & 0xf;
998 addr += cpu.asregs.regs[a];
999 wsat (scpu, opc, addr, cpu.asregs.regs[b]);
1006 cpu.asregs.exception = SIGILL;
1014 } while (!cpu.asregs.exception);
1016 /* Hide away the things we've cached while executing. */
1017 cpu.asregs.regs[PC_REGNO] = pc;
1018 cpu.asregs.insts += insts; /* instructions done ... */
1020 signal (SIGINT, sigsave);
1024 sim_write (sd, addr, buffer, size)
1027 unsigned char * buffer;
1030 sim_cpu *scpu = STATE_CPU (sd, 0); /* FIXME */
1032 sim_core_write_buffer (sd, scpu, write_map, buffer, addr, size);
1038 sim_read (sd, addr, buffer, size)
1041 unsigned char * buffer;
1044 sim_cpu *scpu = STATE_CPU (sd, 0); /* FIXME */
1046 sim_core_read_buffer (sd, scpu, read_map, buffer, addr, size);
1053 sim_store_register (sd, rn, memory, length)
1056 unsigned char * memory;
1059 if (rn < NUM_MOXIE_REGS && rn >= 0)
1065 /* misalignment safe */
1066 ival = moxie_extract_unsigned_integer (memory, 4);
1067 cpu.asints[rn] = ival;
1077 sim_fetch_register (sd, rn, memory, length)
1080 unsigned char * memory;
1083 if (rn < NUM_MOXIE_REGS && rn >= 0)
1087 long ival = cpu.asints[rn];
1089 /* misalignment-safe */
1090 moxie_store_unsigned_integer (memory, 4, ival);
1105 tracefile = fopen("trace.csv", "wb");
1109 sim_resume (sd, 0, 0);
1117 sim_stop_reason (sd, reason, sigrc)
1119 enum sim_stop * reason;
1122 if (cpu.asregs.exception == SIGQUIT)
1124 * reason = sim_exited;
1125 * sigrc = cpu.asregs.regs[2];
1129 * reason = sim_stopped;
1130 * sigrc = cpu.asregs.exception;
1139 cpu.asregs.exception = SIGINT;
1145 sim_info (sd, verbose)
1149 callback->printf_filtered (callback, "\n\n# instructions executed %llu\n",
1155 sim_open (kind, cb, abfd, argv)
1161 SIM_DESC sd = sim_state_alloc (kind, cb);
1162 printf ("0x%x 0x%x\n", sd, STATE_MAGIC(sd));
1163 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
1165 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
1168 sim_do_command(sd," memory region 0x00000000,0x4000000") ;
1169 sim_do_command(sd," memory region 0xE0000000,0x10000") ;
1174 if (kind == SIM_OPEN_STANDALONE)
1177 set_initial_gprs (); /* Reset the GPR registers. */
1183 sim_close (sd, quitting)
1191 /* Load the device tree blob. */
1194 load_dtb (SIM_DESC sd, const char *filename)
1197 FILE *f = fopen (filename, "rb");
1199 sim_cpu *scpu = STATE_CPU (sd, 0); /* FIXME */
1202 printf ("WARNING: ``%s'' could not be opened.\n", filename);
1205 fseek (f, 0, SEEK_END);
1207 fseek (f, 0, SEEK_SET);
1208 buf = alloca (size);
1209 if (size != fread (buf, 1, size, f))
1211 printf ("ERROR: error reading ``%s''.\n", filename);
1214 sim_core_write_buffer (sd, scpu, write_map, buf, 0xE0000000, size);
1215 cpu.asregs.sregs[9] = 0xE0000000;
1220 sim_load (sd, prog, abfd, from_tty)
1227 /* Do the right thing for ELF executables; this turns out to be
1228 just about the right thing for any object format that:
1229 - we crack using BFD routines
1230 - follows the traditional UNIX text/data/bss layout
1231 - calls the bss section ".bss". */
1233 extern bfd * sim_load_file (); /* ??? Don't know where this should live. */
1238 handle = bfd_openr (prog, 0); /* could be "moxie" */
1242 printf("``%s'' could not be opened.\n", prog);
1246 /* Makes sure that we have an object file, also cleans gets the
1247 section headers in place. */
1248 if (!bfd_check_format (handle, bfd_object))
1250 /* wasn't an object file */
1252 printf ("``%s'' is not appropriate object file.\n", prog);
1256 /* Clean up after ourselves. */
1260 /* from sh -- dac */
1261 prog_bfd = sim_load_file (sd, myname, callback, prog, abfd,
1262 sim_kind == SIM_OPEN_DEBUG,
1264 if (prog_bfd == NULL)
1268 bfd_close (prog_bfd);
1274 sim_create_inferior (sd, prog_bfd, argv, env)
1276 struct bfd * prog_bfd;
1282 sim_cpu *scpu = STATE_CPU (sd, 0); /* FIXME */
1284 /* Set the initial register set. */
1287 set_initial_gprs ();
1290 cpu.asregs.regs[PC_REGNO] = bfd_get_start_address (prog_bfd);
1292 /* Copy args into target memory. */
1294 for (argc = 0; *avp; avp++)
1297 /* Target memory looks like this:
1298 0x00000000 zero word
1299 0x00000004 argc word
1300 0x00000008 start of argv
1302 0x0000???? end of argv
1303 0x0000???? zero word
1304 0x0000???? start of data pointed to by argv */
1306 wlat (scpu, 0, 0, 0);
1307 wlat (scpu, 0, 4, argc);
1309 /* tp is the offset of our first argv data. */
1310 tp = 4 + 4 + argc * 4 + 4;
1312 for (i = 0; i < argc; i++)
1314 /* Set the argv value. */
1315 wlat (scpu, 0, 4 + 4 + i * 4, tp);
1317 /* Store the string. */
1318 sim_core_write_buffer (sd, scpu, write_map, argv[i],
1319 tp, strlen(argv[i])+1);
1320 tp += strlen (argv[i]) + 1;
1323 wlat (scpu, 0, 4 + 4 + i * 4, 0);
1339 sim_do_command (sd, cmd)
1343 if (sim_args_command (sd, cmd) != SIM_RC_OK)
1345 "Error: \"%s\" is not a valid moxie simulator command.\n",
1350 sim_set_callbacks (ptr)
1351 host_callback * ptr;