⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sim-safe.c

📁 RISC处理器仿真分析程序。可以用于研究通用RISC处理器的指令和架构设计。在linux下编译
💻 C
字号:
/* * sim-safe.c - sample functional simulator implementation * * This file is a part of the SimpleScalar tool suite written by * Todd M. Austin as a part of the Multiscalar Research Project. *   * The tool suite is currently maintained by Doug Burger and Todd M. Austin. *  * Copyright (C) 1994, 1995, 1996, 1997 by Todd M. Austin * * This source file is distributed "as is" in the hope that it will be * useful.  The tool set comes with no warranty, and no author or * distributor accepts any responsibility for the consequences of its * use.  *  * Everyone is granted permission to copy, modify and redistribute * this tool set under the following conditions: *  *    This source code is distributed for non-commercial use only.  *    Please contact the maintainer for restrictions applying to  *    commercial use. * *    Permission is granted to anyone to make or distribute copies *    of this source code, either as received or modified, in any *    medium, provided that all copyright notices, permission and *    nonwarranty notices are preserved, and that the distributor *    grants the recipient permission for further redistribution as *    permitted by this document. * *    Permission is granted to distribute this file in compiled *    or executable form under the same conditions that apply for *    source code, provided that either: * *    A. it is accompanied by the corresponding machine-readable *       source code, *    B. it is accompanied by a written offer, with no time limit, *       to give anyone a machine-readable copy of the corresponding *       source code in return for reimbursement of the cost of *       distribution.  This written offer must permit verbatim *       duplication by anyone, or *    C. it is distributed by someone who received only the *       executable form, and is accompanied by a copy of the *       written offer of source code that they received concurrently. * * In other words, you are welcome to use, share and improve this * source file.  You are forbidden to forbid anyone else to use, share * and improve what you give them. * * INTERNET: dburger@cs.wisc.edu * US Mail:  1210 W. Dayton Street, Madison, WI 53706 * * $Id: sim-safe.c,v 1.5 1997/03/11 17:14:57 taustin Exp taustin $ * * $Log: sim-safe.c,v $ * Revision 1.5  1997/03/11  17:14:57  taustin * updated copyright * long/int tweaks made for ALPHA target support * supported added for non-GNU C compilers * * Revision 1.4  1997/01/06  16:06:28  taustin * updated comments * opt_reg_header() call now prints simulator overview message * access variable now generalized to is_write boolean flag * * Revision 1.3  1996/12/27  15:54:04  taustin * updated comments * integrated support for options and stats packages * added sim_init() code * * Revision 1.1  1996/12/05  18:52:32  taustin * Initial revision * * */#include <stdio.h>#include <stdlib.h>#include <math.h>#include "misc.h"#include "ss.h"#include "regs.h"#include "memory.h"#include "loader.h"#include "syscall.h"#include "dlite.h"#include "options.h"#include "stats.h"#include "sim.h"/* * This file implements a functional simulator.  This functional simulator is * the simplest, most user-friendly simulator in the simplescalar tool set. * Unlike sim-fast, this functional simulator checks for all instruction * errors, and the implementation is crafted for clarity rather than speed. *//* track number of insn and refs */static SS_COUNTER_TYPE sim_num_insn = 0;static SS_COUNTER_TYPE sim_num_refs = 0;/* register simulator-specific options */voidsim_reg_options(struct opt_odb_t *odb){  opt_reg_header(odb, "sim-safe: This simulator implements a functional simulator.  This\n""functional simulator is the simplest, most user-friendly simulator in the\n""simplescalar tool set.  Unlike sim-fast, this functional simulator checks\n""for all instruction errors, and the implementation is crafted for clarity\n""rather than speed.\n"		 );}/* check simulator-specific option values */voidsim_check_options(struct opt_odb_t *odb, int argc, char **argv){  /* nada */}/* register simulator-specific statistics */voidsim_reg_stats(struct stat_sdb_t *sdb){  stat_reg_counter(sdb, "sim_num_insn",		   "total number of instructions executed",		   &sim_num_insn, 0, NULL);  stat_reg_counter(sdb, "sim_num_refs",		   "total number of loads and stores executed",		   &sim_num_refs, 0, NULL);  stat_reg_int(sdb, "sim_elapsed_time",	       "total simulation time in seconds",	       (int *)&sim_elapsed_time, 0, NULL);  stat_reg_formula(sdb, "sim_inst_rate",		   "simulation speed (in insts/sec)",		   "sim_num_insn / sim_elapsed_time", NULL);}/* initialize the simulator */voidsim_init(void){  SS_INST_TYPE inst;  sim_num_insn = 0;  sim_num_refs = 0;  regs_PC = ld_prog_entry;  /* decode all instructions */  {    SS_ADDR_TYPE addr;    if (OP_MAX > 255)      fatal("cannot perform fast decoding, too many opcodes");    debug("sim: decoding text segment...");    for (addr=ld_text_base;	 addr < (ld_text_base+ld_text_size);	 addr += SS_INST_SIZE)      {	inst = __UNCHK_MEM_ACCESS(SS_INST_TYPE, addr);	inst.a = SWAP_WORD(inst.a);	inst.b = SWAP_WORD(inst.b);	inst.a = (inst.a & ~0xff) | (unsigned int)SS_OP_ENUM(SS_OPCODE(inst));	__UNCHK_MEM_ACCESS(SS_INST_TYPE, addr) = inst;      }  }  /* initialize the DLite debugger */  dlite_init(dlite_reg_obj, dlite_mem_obj, dlite_mstate_obj);}/* print simulator-specific configuration information */voidsim_aux_config(FILE *stream)		/* output stream */{  /* nothing currently */}/* dump simulator-specific auxiliary simulator statistics */voidsim_aux_stats(FILE *stream)		/* output stream */{  /* nada */}/* un-initialize simulator-specific state */voidsim_uninit(void){  /* nada */}/* * configure the execution engine *//* * precise architected register accessors *//* next program counter */#define SET_NPC(EXPR)		(next_PC = (EXPR))/* current program counter */#define CPC			(regs_PC)/* general purpose registers */#define GPR(N)			(regs_R[N])#define SET_GPR(N,EXPR)		(regs_R[N] = (EXPR))/* floating point registers, L->word, F->single-prec, D->double-prec */#define FPR_L(N)		(regs_F.l[(N)])#define SET_FPR_L(N,EXPR)	(regs_F.l[(N)] = (EXPR))#define FPR_F(N)		(regs_F.f[(N)])#define SET_FPR_F(N,EXPR)	(regs_F.f[(N)] = (EXPR))#define FPR_D(N)		(regs_F.d[(N) >> 1])#define SET_FPR_D(N,EXPR)	(regs_F.d[(N) >> 1] = (EXPR))/* miscellaneous register accessors */#define SET_HI(EXPR)		(regs_HI = (EXPR))#define HI			(regs_HI)#define SET_LO(EXPR)		(regs_LO = (EXPR))#define LO			(regs_LO)#define FCC			(regs_FCC)#define SET_FCC(EXPR)		(regs_FCC = (EXPR))/* precise architected memory state help functions */#define __READ_WORD(DST_T, SRC_T, SRC)					\  ((unsigned int)((DST_T)(SRC_T)MEM_READ_WORD(addr = (SRC))))#define __READ_HALF(DST_T, SRC_T, SRC)					\  ((unsigned int)((DST_T)(SRC_T)MEM_READ_HALF(addr = (SRC))))#define __READ_BYTE(DST_T, SRC_T, SRC)					\  ((unsigned int)((DST_T)(SRC_T)MEM_READ_BYTE(addr = (SRC))))/* precise architected memory state accessor macros */#define READ_WORD(SRC)							\  __READ_WORD(unsigned int, unsigned int, (SRC))#define READ_UNSIGNED_HALF(SRC)						\  __READ_HALF(unsigned int, unsigned short, (SRC))#define READ_SIGNED_HALF(SRC)						\  __READ_HALF(signed int, signed short, (SRC))#define READ_UNSIGNED_BYTE(SRC)						\  __READ_BYTE(unsigned int, unsigned char, (SRC))#define READ_SIGNED_BYTE(SRC)						\  __READ_BYTE(signed int, signed char, (SRC))#define WRITE_WORD(SRC, DST)						\  (MEM_WRITE_WORD(addr = (DST), (unsigned int)(SRC)))#define WRITE_HALF(SRC, DST)						\  (MEM_WRITE_HALF(addr = (DST), (unsigned short)(unsigned int)(SRC)))#define WRITE_BYTE(SRC, DST)						\  (MEM_WRITE_BYTE(addr = (DST), (unsigned char)(unsigned int)(SRC)))/* system call handler macro */#define SYSCALL(INST)		(ss_syscall(mem_access, INST))/* instantiate the helper functions in the '.def' file */#define DEFINST(OP,MSK,NAME,OPFORM,RES,CLASS,O1,O2,I1,I2,I3,EXPR)#define DEFLINK(OP,MSK,NAME,MASK,SHIFT)#define CONNECT(OP)#define IMPL#include "ss.def"#undef DEFINST#undef DEFLINK#undef CONNECT#undef IMPL/* start simulation, program loaded, processor precise state initialized */voidsim_main(void){  SS_INST_TYPE inst;  register SS_ADDR_TYPE next_PC;  register SS_ADDR_TYPE addr;  enum ss_opcode op;  register int is_write;  fprintf(stderr, "sim: ** starting functional simulation **\n");  /* set up initial default next PC */  next_PC = regs_PC + SS_INST_SIZE;  /* check for DLite debugger entry condition */  if (dlite_check_break(regs_PC, /* no access */0, /* addr */0, 0, 0))    dlite_main(regs_PC - SS_INST_SIZE, regs_PC, sim_num_insn);  while (TRUE)    {      /* maintain $r0 semantics */      regs_R[0] = 0;      /* keep an instruction count */      sim_num_insn++;      /* get the next instruction to execute */      inst = __UNCHK_MEM_ACCESS(SS_INST_TYPE, regs_PC);      /* set default reference address and access mode */      addr = 0; is_write = FALSE;      /* decode the instruction */      op = SS_OPCODE(inst);      switch (op)	{#define DEFINST(OP,MSK,NAME,OPFORM,RES,FLAGS,O1,O2,I1,I2,I3,EXPR)	\	case OP:                                                        \          EXPR;                                                         \          break;#define DEFLINK(OP,MSK,NAME,MASK,SHIFT)                                 \        case OP:                                                        \          panic("attempted to execute a linking opcode");#define CONNECT(OP)#include "ss.def"#undef DEFINST#undef DEFLINK#undef CONNECT	default:	  panic("bogus opcode");      }      if (SS_OP_FLAGS(op) & F_MEM)	{	  sim_num_refs++;	  if (SS_OP_FLAGS(op) & F_STORE)	    is_write = TRUE;	}      /* check for DLite debugger entry condition */      if (dlite_check_break(next_PC,			    is_write ? ACCESS_WRITE : ACCESS_READ,			    addr, sim_num_insn, sim_num_insn))	dlite_main(regs_PC, next_PC, sim_num_insn);      /* go to the next instruction */      regs_PC = next_PC;      next_PC += SS_INST_SIZE;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -