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

📄 func.c

📁 leon2的指令模拟器。leon是应用于航天领域的一款高可靠性的sparc v7指令集的处理器。
💻 C
📖 第 1 页 / 共 2 页
字号:
	    daddr = sregs->pc;	    sim_halt();	} else if (strncmp(cmd1, "tcont", clen) == 0) {	    sregs->tlimit = limcalc(sregs->freq);	    stat = run_sim(sregs, -1, 0);	    daddr = sregs->pc;	    sim_halt();	} else if (strncmp(cmd1, "tgo", clen) == 0) {	    if ((cmd1 = strtok(NULL, " \t\n\r")) == NULL) {		len = last_load_addr;	    } else {		len = VAL(cmd1);	        sregs->tlimit = limcalc(sregs->freq);	    }	    sregs->pc = len & ~3;	    sregs->npc = sregs->pc + 4;	    printf("resuming at 0x%08x\n",sregs->pc);	    stat = run_sim(sregs, -1, 0);	    daddr = sregs->pc;	    sim_halt();	} else if (strncmp(cmd1, "tlimit", clen) == 0) {	   sregs->tlimit = limcalc(sregs->freq);	   if (sregs->tlimit != (uint32) -1)              printf("simulation limit = %u (%.3f ms)\n",(uint32) sregs->tlimit,		sregs->tlimit / sregs->freq / 1000);	} else if (strncmp(cmd1, "tra", clen) == 0) {	    if ((cmd1 = strtok(NULL, " \t\n\r")) == NULL) {		stat = run_sim(sregs, -1, 1);	    } else {		stat = run_sim(sregs, VAL(cmd1), 1);	    }	    printf("\n");	    daddr = sregs->pc;	    sim_halt();	} else if (strncmp(cmd1, "trun", clen) == 0) {	    ebase.simtime = 0;	    reset_all();	    reset_stat(sregs);	    sregs->tlimit = limcalc(sregs->freq);	    stat = run_sim(sregs, -1, 0);	    daddr = sregs->pc;	    sim_halt();	} else	    printf("syntax error\n");    }    if (cmdsave != NULL)	free(cmdsave);    return (stat);}voidreset_stat(sregs)    struct pstate  *sregs;{    sregs->tottime = 0;    sregs->pwdtime = 0;    sregs->ninst = 0;    sregs->fholdt = 0;    sregs->holdt = 0;    sregs->icntt = 0;    sregs->finst = 0;    sregs->nstore = 0;    sregs->nload = 0;    sregs->nbranch = 0;    sregs->simstart = ebase.simtime;}voidshow_stat(sregs)    struct pstate  *sregs;{    uint32          iinst;    uint32          stime, tottime;    if (sregs->tottime == 0) tottime = 1; else tottime = sregs->tottime;    stime = ebase.simtime - sregs->simstart;	/* Total simulated time */#ifdef STAT    iinst = sregs->ninst - sregs->finst - sregs->nload - sregs->nstore -	sregs->nbranch;#endif    printf("\n Cycles       : %9d\n\r", ebase.simtime - sregs->simstart);    printf(" Instructions : %9d\n", sregs->ninst);#ifdef STAT    printf("   integer    : %9.2f %%\n", 100.0 * (float) iinst / (float) sregs->ninst);    printf("   load       : %9.2f %%\n",	   100.0 * (float) sregs->nload / (float) sregs->ninst);    printf("   store      : %9.2f %%\n",	   100.0 * (float) sregs->nstore / (float) sregs->ninst);    printf("   branch     : %9.2f %%\n",	   100.0 * (float) sregs->nbranch / (float) sregs->ninst);    printf("   float      : %9.2f %%\n",	   100.0 * (float) sregs->finst / (float) sregs->ninst);    printf(" Integer CPI  : %9.2f\n",	   ((float) (stime - sregs->pwdtime - sregs->fholdt - sregs->finst))	   /	   (float) (sregs->ninst - sregs->finst));    printf(" Float CPI    : %9.2f\n",	   ((float) sregs->fholdt / (float) sregs->finst) + 1.0);#endif    printf(" Overall CPI  : %9.2f\n",	   (float) (stime - sregs->pwdtime) / (float) sregs->ninst);    printf("\n ERC32 performance (%4.1f MHz): %5.2f MOPS (%5.2f MIPS, %5.2f MFLOPS)\n",	   sregs->freq, sregs->freq * (float) sregs->ninst / (float) (stime - sregs->pwdtime),	   sregs->freq * (float) (sregs->ninst - sregs->finst) /	   (float) (stime - sregs->pwdtime),     sregs->freq * (float) sregs->finst / (float) (stime - sregs->pwdtime));    printf(" Simulated ERC32 time        : %5.2f ms\n", (float) (ebase.simtime - sregs->simstart) / 1000.0 / sregs->freq);    printf(" Processor utilisation       : %5.2f %%\n", 100.0 * (1.0 - ((float) sregs->pwdtime / (float) stime)));    printf(" Real-time / simulator-time  : 1/%.2f \n",      ((float) sregs->tottime) / ((float) (stime) / (sregs->freq * 1.0E6)));    printf(" Simulator performance       : %d KIPS\n",sregs->ninst/tottime/1000);    printf(" Used time (sys + user)      : %3d s\n\n", sregs->tottime);}voidinit_bpt(sregs)    struct pstate  *sregs;{    sregs->bptnum = 0;    sregs->histlen = 0;    sregs->histind = 0;    sregs->histbuf = NULL;    sregs->tlimit = -1;}static voidint_handler(sig)    int32           sig;{    if (sig != 2)	printf("\n\n Signal handler error  (%d)\n\n", sig);    ctrl_c = 1;}voidinit_signals(){    typedef void    (*PFI) ();    static PFI      int_tab[2];    int_tab[0] = signal(SIGTERM, int_handler);    int_tab[1] = signal(SIGINT, int_handler);}extern struct disassemble_info dinfo;struct estate   ebase;struct evcell   evbuf[EVENT_MAX];struct irqcell  irqarr[16];static intdisp_fpu(sregs)    struct pstate  *sregs;{    int         i;    float	t;    printf("\n fsr: %08X\n\n", sregs->fsr);#ifdef HOST_LITTLE_ENDIAN_FLOAT    for (i = 0; i < 32; i++)      sregs->fdp[i ^ 1] = sregs->fs[i];#endif    for (i = 0; i < 32; i++) {	t = sregs->fs[i];	printf(" f%02d  %08x  %14e  ", i, sregs->fsi[i], sregs->fs[i]);	if (!(i & 1))	    printf("%14e\n", sregs->fd[i >> 1]);	else	    printf("\n");    }    printf("\n");    return (OK);}static voiddisp_regs(sregs,cwp)    struct pstate  *sregs;    int cwp;{    int           i;    cwp = ((cwp & 0x7) << 4);    printf("\n\t  INS       LOCALS      OUTS     GLOBALS\n");    for (i = 0; i < 8; i++) {	printf("   %d:  %08X   %08X   %08X   %08X\n", i,	       sregs->r[(cwp + i + 24) & 0x7f],	    sregs->r[(cwp + i + 16) & 0x7f], sregs->r[(cwp + i + 8) & 0x7f],	       sregs->g[i]);    }}static voiddisp_ctrl(sregs)    struct pstate  *sregs;{    unsigned char           i[4];    printf("\n psr: %08X   wim: %08X   tbr: %08X   y: %08X\n",	   sregs->psr, sregs->wim, sregs->tbr, sregs->y);    sis_memory_read(sregs->pc, i, 4);    printf("\n  pc: %08X = %02X%02X%02X%02X    ", sregs->pc,i[0],i[1],i[2],i[3]);    print_insn_sparc(sregs->pc, &dinfo);    sis_memory_read(sregs->npc, i, 4);    printf("\n npc: %08X = %02X%02X%02X%02X    ",sregs->npc,i[0],i[1],i[2],i[3]);    print_insn_sparc(sregs->npc, &dinfo);    if (sregs->err_mode)	printf("\n IU in error mode");    printf("\n\n");}static voiddisp_mem(addr, len)    uint32          addr;    uint32          len;{    uint32          i;    unsigned char   data[4];    uint32          mem[4], j;    char           *p;    for (i = addr & ~3; i < ((addr + len) & ~3); i += 16) {	printf("\n %8X  ", i);	for (j = 0; j < 4; j++) {	    sis_memory_read((i + (j * 4)), data, 4);	    printf("%02x%02x%02x%02x  ", data[0],data[1],data[2],data[3]);	    mem[j] = *((int *) &data);	}	printf("  ");	p = (char *) mem;	for (j = 0; j < 16; j++) {	    if (isprint(p[j]))		putchar(p[j]);	    else		putchar('.');	}    }    printf("\n\n");}voiddis_mem(addr, len, info)    uint32          addr;    uint32          len;    struct disassemble_info *info;{    uint32          i;    unsigned char   data[4];    for (i = addr & -3; i < ((addr & -3) + (len << 2)); i += 4) {	sis_memory_read(i, data, 4);	printf(" %08x  %02x%02x%02x%02x  ", i, data[0],data[1],data[2],data[3]);	print_insn_sparc(i, info);        if (i >= 0xfffffffc) break;	printf("\n");    }}/* Add event to event queue */voidevent(cfunc, arg, delta)    void            (*cfunc) ();    int32           arg;    uint32          delta;{    struct evcell  *ev1, *evins;    if (ebase.freeq == NULL) {	printf("Error, too many events in event queue\n");	return;    }    ev1 = &ebase.eq;    delta += ebase.simtime;    while ((ev1->nxt != NULL) && (ev1->nxt->time <= delta)) {	ev1 = ev1->nxt;    }    if (ev1->nxt == NULL) {	ev1->nxt = ebase.freeq;	ebase.freeq = ebase.freeq->nxt;	ev1->nxt->nxt = NULL;    } else {	evins = ebase.freeq;	ebase.freeq = ebase.freeq->nxt;	evins->nxt = ev1->nxt;	ev1->nxt = evins;    }    ev1->nxt->time = delta;    ev1->nxt->cfunc = cfunc;    ev1->nxt->arg = arg;}#if 0	/* apparently not used */voidstop_event(){}#endifvoidinit_event(){    int32           i;    ebase.eq.nxt = NULL;    ebase.freeq = evbuf;    for (i = 0; i < EVENT_MAX; i++) {	evbuf[i].nxt = &evbuf[i + 1];    }    evbuf[EVENT_MAX - 1].nxt = NULL;}voidset_int(level, callback, arg)    int32           level;    void            (*callback) ();    int32           arg;{    irqarr[level & 0x0f].callback = callback;    irqarr[level & 0x0f].arg = arg;}/* Advance simulator time */voidadvance_time(sregs)    struct pstate  *sregs;{    struct evcell  *evrem;    void            (*cfunc) ();    uint32          arg, endtime;#ifdef STAT    sregs->fholdt += sregs->fhold;    sregs->holdt += sregs->hold;    sregs->icntt += sregs->icnt;#endif    endtime = ebase.simtime + sregs->icnt + sregs->hold + sregs->fhold;    while ((ebase.eq.nxt->time <= (endtime)) && (ebase.eq.nxt != NULL)) {	ebase.simtime = ebase.eq.nxt->time;	cfunc = ebase.eq.nxt->cfunc;	arg = ebase.eq.nxt->arg;	evrem = ebase.eq.nxt;	ebase.eq.nxt = ebase.eq.nxt->nxt;	evrem->nxt = ebase.freeq;	ebase.freeq = evrem;	cfunc(arg);    }    ebase.simtime = endtime;}uint32now(){    return(ebase.simtime);}/* Advance time until an external interrupt is seen */intwait_for_irq(){    struct evcell  *evrem;    void            (*cfunc) ();    int32           arg, endtime;    if (ebase.eq.nxt == NULL)	printf("Warning: event queue empty - power-down mode not entered\n");    endtime = ebase.simtime;    while (!ext_irl && (ebase.eq.nxt != NULL)) {	ebase.simtime = ebase.eq.nxt->time;	cfunc = ebase.eq.nxt->cfunc;	arg = ebase.eq.nxt->arg;	evrem = ebase.eq.nxt;	ebase.eq.nxt = ebase.eq.nxt->nxt;	evrem->nxt = ebase.freeq;	ebase.freeq = evrem;	cfunc(arg);	if (ctrl_c) {	    printf("\bwarning: power-down mode interrupted\n");	    break;	}    }    sregs.pwdtime += ebase.simtime - endtime;    return (ebase.simtime - endtime);}intcheck_bpt(sregs)    struct pstate  *sregs;{    int32           i;    if ((sregs->bphit) || (sregs->annul))	return (0);    for (i = 0; i < (int32) sregs->bptnum; i++) {	if (sregs->pc == sregs->bpts[i])	    return (BPT_HIT);    }    return (0);}voidreset_all(){    init_event();		/* Clear event queue */    init_regs(&sregs);    reset();#ifdef ERRINJ    errinjstart();#endif}voidsys_reset(){    reset_all();    sregs.trap = 256;		/* Force fake reset trap */}voidsys_halt(){    sregs.trap = 257;           /* Force fake halt trap */}#include "ansidecl.h"#include <stdarg.h>#include "libiberty.h"#include "bfd.h"#define min(A, B) (((A) < (B)) ? (A) : (B))#define LOAD_ADDRESS 0intbfd_load(fname)    char           *fname;{    asection       *section;    bfd            *pbfd;    const bfd_arch_info_type *arch;    pbfd = bfd_openr(fname, 0);    if (pbfd == NULL) {	printf("open of %s failed\n", fname);	return (-1);    }    if (!bfd_check_format(pbfd, bfd_object)) {	printf("file %s  doesn't seem to be an object file\n", fname);	return (-1);    }    arch = bfd_get_arch_info (pbfd);    if (bfd_little_endian (pbfd) || arch->mach == bfd_mach_sparc_sparclite_le)        current_target_byte_order = LITTLE_ENDIAN;    else	current_target_byte_order = BIG_ENDIAN;    if (sis_verbose)	printf("file %s is %s-endian.\n", fname,	       current_target_byte_order == BIG_ENDIAN ? "big" : "little");    if (sis_verbose)	printf("loading %s:", fname);    for (section = pbfd->sections; section; section = section->next) {	if (bfd_get_section_flags(pbfd, section) & SEC_ALLOC) {	    bfd_vma         section_address;	    unsigned long   section_size;	    const char     *section_name;	    section_name = bfd_get_section_name(pbfd, section);	    section_address = bfd_get_section_vma(pbfd, section);	    /*	     * Adjust sections from a.out files, since they don't carry their	     * addresses with.	     */	    if (bfd_get_flavour(pbfd) == bfd_target_aout_flavour) {		if (strcmp (section_name, ".text") == 0)		    section_address = bfd_get_start_address (pbfd);		else if (strcmp (section_name, ".data") == 0) {		    /* Read the first 8 bytes of the data section.		       There should be the string 'DaTa' followed by		       a word containing the actual section address. */		    struct data_marker		    {			char signature[4];	/* 'DaTa' */			unsigned char sdata[4];	/* &sdata */		    } marker;		    bfd_get_section_contents (pbfd, section, &marker, 0,					      sizeof (marker));		    if (strncmp (marker.signature, "DaTa", 4) == 0)		      {			if (current_target_byte_order == BIG_ENDIAN)			  section_address = bfd_getb32 (marker.sdata);			else			  section_address = bfd_getl32 (marker.sdata);		      }		}	    }	    section_size = bfd_section_size(pbfd, section);	    if (sis_verbose)		printf("\nsection %s at 0x%08lx (0x%lx bytes)",		       section_name, section_address, section_size);	    /* Text, data or lit */	    if (bfd_get_section_flags(pbfd, section) & SEC_LOAD) {		file_ptr        fptr;		fptr = 0;		while (section_size > 0) {		    char            buffer[1024];		    int             count;		    count = min(section_size, 1024);		    bfd_get_section_contents(pbfd, section, buffer, fptr, count);		    sis_memory_write(section_address, buffer, count);		    section_address += count;		    fptr += count;		    section_size -= count;		}	    } else		/* BSS */		if (sis_verbose)		    printf("(not loaded)");	}    }    if (sis_verbose)	printf("\n");    return(bfd_get_start_address (pbfd));}

⌨️ 快捷键说明

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