📄 interp.c
字号:
#include <signal.h>#if WITH_COMMON#include "sim-main.h"#include "sim-options.h"#include "sim-hw.h"#else#include "mn10300_sim.h"#endif#include "sysdep.h"#include "bfd.h"#include "sim-assert.h"#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_STRING_H#include <string.h>#else#ifdef HAVE_STRINGS_H#include <strings.h>#endif#endif#include "bfd.h"#ifndef INLINE#ifdef __GNUC__#define INLINE inline#else#define INLINE#endif#endifhost_callback *mn10300_callback;int mn10300_debug;struct _state State;/* simulation target board. NULL=default configuration */static char* board = NULL;static DECLARE_OPTION_HANDLER (mn10300_option_handler);enum { OPTION_BOARD = OPTION_START,};static SIM_RCmn10300_option_handler (sd, cpu, opt, arg, is_command) SIM_DESC sd; sim_cpu *cpu; int opt; char *arg; int is_command;{ int cpu_nr; switch (opt) { case OPTION_BOARD: { if (arg) { board = zalloc(strlen(arg) + 1); strcpy(board, arg); } return SIM_RC_OK; } } return SIM_RC_OK;}static const OPTION mn10300_options[] = {#define BOARD_AM32 "stdeval1" { {"board", required_argument, NULL, OPTION_BOARD}, '\0', "none" /* rely on compile-time string concatenation for other options */ "|" BOARD_AM32 , "Customize simulation for a particular board.", mn10300_option_handler }, { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }};#if WITH_COMMON#elsestatic void dispatch PARAMS ((uint32, uint32, int));static long hash PARAMS ((long));static void init_system PARAMS ((void));static SIM_OPEN_KIND sim_kind;static char *myname;#define MAX_HASH 127struct hash_entry{ struct hash_entry *next; long opcode; long mask; struct simops *ops;#ifdef HASH_STAT unsigned long count;#endif};static int max_mem = 0;struct hash_entry hash_table[MAX_HASH+1];/* This probably doesn't do a very good job at bucket filling, but it's simple... */static INLINE long hash(insn) long insn;{ /* These are one byte insns, we special case these since, in theory, they should be the most heavily used. */ if ((insn & 0xffffff00) == 0) { switch (insn & 0xf0) { case 0x00: return 0x70; case 0x40: return 0x71; case 0x10: return 0x72; case 0x30: return 0x73; case 0x50: return 0x74; case 0x60: return 0x75; case 0x70: return 0x76; case 0x80: return 0x77; case 0x90: return 0x78; case 0xa0: return 0x79; case 0xb0: return 0x7a; case 0xe0: return 0x7b; default: return 0x7c; } } /* These are two byte insns */ if ((insn & 0xffff0000) == 0) { if ((insn & 0xf000) == 0x2000 || (insn & 0xf000) == 0x5000) return ((insn & 0xfc00) >> 8) & 0x7f; if ((insn & 0xf000) == 0x4000) return ((insn & 0xf300) >> 8) & 0x7f; if ((insn & 0xf000) == 0x8000 || (insn & 0xf000) == 0x9000 || (insn & 0xf000) == 0xa000 || (insn & 0xf000) == 0xb000) return ((insn & 0xf000) >> 8) & 0x7f; if ((insn & 0xff00) == 0xf000 || (insn & 0xff00) == 0xf100 || (insn & 0xff00) == 0xf200 || (insn & 0xff00) == 0xf500 || (insn & 0xff00) == 0xf600) return ((insn & 0xfff0) >> 4) & 0x7f; if ((insn & 0xf000) == 0xc000) return ((insn & 0xff00) >> 8) & 0x7f; return ((insn & 0xffc0) >> 6) & 0x7f; } /* These are three byte insns. */ if ((insn & 0xff000000) == 0) { if ((insn & 0xf00000) == 0x000000) return ((insn & 0xf30000) >> 16) & 0x7f; if ((insn & 0xf00000) == 0x200000 || (insn & 0xf00000) == 0x300000) return ((insn & 0xfc0000) >> 16) & 0x7f; if ((insn & 0xff0000) == 0xf80000) return ((insn & 0xfff000) >> 12) & 0x7f; if ((insn & 0xff0000) == 0xf90000) return ((insn & 0xfffc00) >> 10) & 0x7f; return ((insn & 0xff0000) >> 16) & 0x7f; } /* These are four byte or larger insns. */ if ((insn & 0xf0000000) == 0xf0000000) return ((insn & 0xfff00000) >> 20) & 0x7f; return ((insn & 0xff000000) >> 24) & 0x7f;}static INLINE voiddispatch (insn, extension, length) uint32 insn; uint32 extension; int length;{ struct hash_entry *h; h = &hash_table[hash(insn)]; while ((insn & h->mask) != h->opcode || (length != h->ops->length)) { if (!h->next) { (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR looking up hash for 0x%x, PC=0x%x\n", insn, PC); exit(1); } h = h->next; }#ifdef HASH_STAT h->count++;#endif /* Now call the right function. */ (h->ops->func)(insn, extension); PC += length;}voidsim_size (power) int power;{ if (State.mem) free (State.mem); max_mem = 1 << power; State.mem = (uint8 *) calloc (1, 1 << power); if (!State.mem) { (*mn10300_callback->printf_filtered) (mn10300_callback, "Allocation of main memory failed.\n"); exit (1); }}static voidinit_system (){ if (!State.mem) sim_size(19);}intsim_write (sd, addr, buffer, size) SIM_DESC sd; SIM_ADDR addr; unsigned char *buffer; int size;{ int i; init_system (); for (i = 0; i < size; i++) store_byte (addr + i, buffer[i]); return size;}/* Compare two opcode table entries for qsort. */static intcompare_simops (arg1, arg2) const PTR arg1; const PTR arg2;{ unsigned long code1 = ((struct simops *)arg1)->opcode; unsigned long code2 = ((struct simops *)arg2)->opcode; if (code1 < code2) return -1; if (code2 < code1) return 1; return 0;}SIM_DESCsim_open (kind, cb, abfd, argv) SIM_OPEN_KIND kind; host_callback *cb; struct bfd *abfd; char **argv;{ struct simops *s; struct hash_entry *h; char **p; int i; mn10300_callback = cb; /* Sort the opcode array from smallest opcode to largest. This will generally improve simulator performance as the smaller opcodes are generally preferred to the larger opcodes. */ for (i = 0, s = Simops; s->func; s++, i++) ; qsort (Simops, i, sizeof (Simops[0]), compare_simops); sim_kind = kind; myname = argv[0]; for (p = argv + 1; *p; ++p) { if (strcmp (*p, "-E") == 0) ++p; /* ignore endian spec */ else#ifdef DEBUG if (strcmp (*p, "-t") == 0) mn10300_debug = DEBUG; else#endif (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",*p); } /* put all the opcodes in the hash table */ for (s = Simops; s->func; s++) { h = &hash_table[hash(s->opcode)]; /* go to the last entry in the chain */ while (h->next) { /* Don't insert the same opcode more than once. */ if (h->opcode == s->opcode && h->mask == s->mask && h->ops == s) break; else h = h->next; } /* Don't insert the same opcode more than once. */ if (h->opcode == s->opcode && h->mask == s->mask && h->ops == s) continue; if (h->ops) { h->next = calloc(1,sizeof(struct hash_entry)); h = h->next; } h->ops = s; h->mask = s->mask; h->opcode = s->opcode;#if HASH_STAT h->count = 0;#endif } /* fudge our descriptor for now */ return (SIM_DESC) 1;}voidsim_close (sd, quitting) SIM_DESC sd; int quitting;{ /* nothing to do */}voidsim_set_profile (n) int n;{ (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n);}voidsim_set_profile_size (n) int n;{ (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n);}intsim_stop (sd) SIM_DESC sd;{ return 0;}voidsim_resume (sd, step, siggnal) SIM_DESC sd; int step, siggnal;{ uint32 inst; reg_t oldpc; struct hash_entry *h; if (step) State.exception = SIGTRAP; else State.exception = 0; State.exited = 0; do { unsigned long insn, extension; /* Fetch the current instruction. */ inst = load_mem_big (PC, 2); oldpc = PC; /* Using a giant case statement may seem like a waste because of the code/rodata size the table itself will consume. However, using a giant case statement speeds up the simulator by 10-15% by avoiding cascading if/else statements or cascading case statements. */ switch ((inst >> 8) & 0xff) { /* All the single byte insns except 0x80, 0x90, 0xa0, 0xb0 which must be handled specially. */ case 0x00: case 0x04: case 0x08: case 0x0c: case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17: case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f: case 0x3c: case 0x3d: case 0x3e: case 0x3f:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -