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

📄 sim-cache.c

📁 RISC处理器仿真分析程序。可以用于研究通用RISC处理器的指令和架构设计。在linux下编译
💻 C
📖 第 1 页 / 共 2 页
字号:
			       /* usize */0, assoc, cache_char2policy(c),			       il1_access_fn, /* hit latency */1);      /* is the level 2 D-cache defined? */      if (!mystricmp(cache_il2_opt, "none"))	cache_il2 = NULL;      else if (!mystricmp(cache_il2_opt, "dl2"))	{	  if (!cache_dl2)	    fatal("I-cache l2 cannot access D-cache l2 as it's undefined");	  cache_il2 = cache_dl2;	}      else	{	  if (sscanf(cache_il2_opt, "%[^:]:%d:%d:%d:%c",		     name, &nsets, &bsize, &assoc, &c) != 5)	    fatal("bad l2 I-cache parms: "		  "<name>:<nsets>:<bsize>:<assoc>:<repl>");	  cache_il2 = cache_create(name, nsets, bsize, /* balloc */FALSE,				   /* usize */0, assoc, cache_char2policy(c),				   il2_access_fn, /* hit latency */1);	}    }  /* use an I-TLB? */  if (!mystricmp(itlb_opt, "none"))    itlb = NULL;  else    {      if (sscanf(itlb_opt, "%[^:]:%d:%d:%d:%c",		 name, &nsets, &bsize, &assoc, &c) != 5)	fatal("bad TLB parms: <name>:<nsets>:<page_size>:<assoc>:<repl>");      itlb = cache_create(name, nsets, bsize, /* balloc */FALSE,			  /* usize */sizeof(SS_ADDR_TYPE), assoc,			  cache_char2policy(c), itlb_access_fn,			  /* hit latency */1);    }  /* use a D-TLB? */  if (!mystricmp(dtlb_opt, "none"))    dtlb = NULL;  else    {      if (sscanf(dtlb_opt, "%[^:]:%d:%d:%d:%c",		 name, &nsets, &bsize, &assoc, &c) != 5)	fatal("bad TLB parms: <name>:<nsets>:<page_size>:<assoc>:<repl>");      dtlb = cache_create(name, nsets, bsize, /* balloc */FALSE,			  /* usize */sizeof(SS_ADDR_TYPE), assoc,			  cache_char2policy(c), dtlb_access_fn,			  /* hit latency */1);    }}/* print simulator-specific configuration information */voidsim_aux_config(FILE *stream)		/* output stream */{  /* nada */}/* register simulator-specific statistics */voidsim_reg_stats(struct stat_sdb_t *sdb)	/* stats database */{  int i;  /* register baseline stats */  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);  /* register cache stats */  if (cache_il1      && (cache_il1 != cache_dl1 && cache_il1 != cache_dl2))    cache_reg_stats(cache_il1, sdb);  if (cache_il2      && (cache_il2 != cache_dl1 && cache_il2 != cache_dl2))    cache_reg_stats(cache_il2, sdb);  if (cache_dl1)    cache_reg_stats(cache_dl1, sdb);  if (cache_dl2)    cache_reg_stats(cache_dl2, sdb);  if (itlb)    cache_reg_stats(itlb, sdb);  if (dtlb)    cache_reg_stats(dtlb, sdb);  for (i=0; i<pcstat_nelt; i++)    {      char buf[512], buf1[512];      struct stat_stat_t *stat;      /* track the named statistical variable by text address */      /* find it... */      stat = stat_find_stat(sdb, pcstat_vars[i]);      if (!stat)	fatal("cannot locate any statistic named `%s'", pcstat_vars[i]);      /* stat must be an integral type */      if (stat->sc != sc_int && stat->sc != sc_uint && stat->sc != sc_counter)	fatal("`-pcstat' statistical variable `%s' is not an integral type",	      stat->name);      /* register this stat */      pcstat_stats[i] = stat;      pcstat_lastvals[i] = STATVAL(stat);      /* declare the sparce text distribution */      sprintf(buf, "%s_by_pc", stat->name);      sprintf(buf1, "%s (by text address)", stat->desc);      pcstat_sdists[i] = stat_reg_sdist(sdb, buf, buf1,					/* initial value */0,					/* print fmt */(PF_COUNT|PF_PDF),					/* format */"0x%lx %lu %.2f",					/* print fn */NULL);    }}/* local machine state accessor */static char *					/* err str, NULL for no err */cache_mstate_obj(FILE *stream,			/* output stream */		 char *cmd)			/* optional command string */{  /* just dump intermediate stats */  sim_print_stats(stream);  /* no error */  return 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 do 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 = (inst.a & ~0xff) | (unsigned int)SS_OP_ENUM(SS_OPCODE(inst));	__UNCHK_MEM_ACCESS(SS_INST_TYPE, addr) = inst;      }  }  /* initialize the DLite debugger (NOTE: mem is always precise) */  dlite_init(dlite_reg_obj, dlite_mem_obj, cache_mstate_obj);}/* dump simulator-specific auxiliary simulator statistics */voidsim_aux_stats(FILE *stream)		/* output stream */{  /* nada */}/* un-initialize the simulator */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_CACHE(addr, SRC_T)					\  ((dtlb								\    ? cache_access(dtlb, Read, (addr), NULL, sizeof(SRC_T), 0, NULL, NULL)\    : 0),								\   (cache_dl1								\    ? cache_access(cache_dl1, Read, (addr), NULL, sizeof(SRC_T), 0, NULL, NULL)\    : 0))#define __READ_WORD(DST_T, SRC_T, SRC)					\  (addr = (SRC),							\   __READ_CACHE(addr, SRC_T),						\   ((unsigned int)((DST_T)(SRC_T)MEM_READ_WORD(addr))))#define __READ_HALF(DST_T, SRC_T, SRC)					\  (addr = (SRC),							\   __READ_CACHE(addr, SRC_T),						\   (unsigned int)((DST_T)(SRC_T)MEM_READ_HALF(addr)))#define __READ_BYTE(DST_T, SRC_T, SRC)					\  (addr = (SRC),							\   __READ_CACHE(addr, SRC_T),						\   (unsigned int)((DST_T)(SRC_T)MEM_READ_BYTE(addr)))/* 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))/* precise architected memory state help functions */#define __WRITE_CACHE(addr, DST_T)					\  ((dtlb								\    ? cache_access(dtlb, Write, (addr), NULL, sizeof(DST_T), 0, NULL, NULL)\    : 0),								\   (cache_dl1								\    ? cache_access(cache_dl1, Write, (addr), NULL, sizeof(DST_T),	\		   0, NULL, NULL)					\    : 0))#define WRITE_WORD(SRC, DST)						\  (addr = (DST),							\   __WRITE_CACHE(addr, unsigned int),					\   MEM_WRITE_WORD(addr, (unsigned int)(SRC)))#define WRITE_HALF(SRC, DST)						\  (addr = (DST),							\   __WRITE_CACHE(addr, unsigned short),					\   MEM_WRITE_HALF(addr, (unsigned short)(unsigned int)(SRC)))#define WRITE_BYTE(SRC, DST)						\  (addr = (DST),							\   __WRITE_CACHE(addr, unsigned char),					\   MEM_WRITE_BYTE(addr, (unsigned char)(unsigned int)(SRC)))/* system call memory access function */voiddcache_access_fn(enum mem_cmd cmd,	/* memory access cmd, Read or Write */		 SS_ADDR_TYPE addr,	/* data address to access */		 void *p,		/* data input/output buffer */		 int nbytes)		/* number of bytes to access */{  if (dtlb)    cache_access(dtlb, cmd, addr, NULL, nbytes, 0, NULL, NULL);  if (cache_dl1)    cache_access(cache_dl1, cmd, addr, NULL, nbytes, 0, NULL, NULL);  mem_access(cmd, addr, p, nbytes);}/* system call handler macro */#define SYSCALL(INST)							\  (flush_on_syscalls							\   ? ((dtlb ? cache_flush(dtlb, 0) : 0),				\      (cache_dl1 ? cache_flush(cache_dl1, 0) : 0),			\      (cache_dl2 ? cache_flush(cache_dl2, 0) : 0),			\      ss_syscall(mem_access, INST))					\   : (ss_syscall(dcache_access_fn, INST)))/* instantiate the helper functions in the '.def' file */#define DEFICLASS(ICLASS,DESC)#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 DEFICLASS#undef DEFINST#undef DEFLINK#undef CONNECT#undef IMPL/* start simulation, program loaded, processor precise state initialized */voidsim_main(void){  int i;  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 w/ caches **\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 */      if (itlb)	cache_access(itlb, Read, IACOMPRESS(regs_PC),		     NULL, ISCOMPRESS(SS_INST_SIZE), 0, NULL, NULL);      if (cache_il1)	cache_access(cache_il1, Read, IACOMPRESS(regs_PC),		     NULL, ISCOMPRESS(SS_INST_SIZE), 0, NULL, NULL);      mem_access(Read, regs_PC, &inst, SS_INST_SIZE);      /* 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("attempted to execute a bogus opcode");	}      if (SS_OP_FLAGS(op) & F_MEM)	{	  sim_num_refs++;	  if (SS_OP_FLAGS(op) & F_STORE)	    is_write = TRUE;	}      /* update any stats tracked by PC */      for (i=0; i<pcstat_nelt; i++)	{	  SS_COUNTER_TYPE newval;	  int delta;	  /* check if any tracked stats changed */	  newval = STATVAL(pcstat_stats[i]);	  delta = newval - pcstat_lastvals[i];	  if (delta != 0)	    {	      stat_add_samples(pcstat_sdists[i], regs_PC, delta);	      pcstat_lastvals[i] = newval;	    }	}      /* 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 + -