OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [cpu/] [common/] [parse.c] - Diff between revs 1323 and 1350

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 1323 Rev 1350
Line 20... Line 20...
#include <stdio.h>
#include <stdio.h>
#include <ctype.h>
#include <ctype.h>
#include <string.h>
#include <string.h>
#include <stdlib.h>
#include <stdlib.h>
 
 
#include "abstract.h"
#include "config.h"
 
 
 
#ifdef HAVE_INTTYPES_H
 
#include <inttypes.h>
 
#endif
 
 
 
#include "port.h"
#include "arch.h"
#include "arch.h"
 
#include "abstract.h"
#include "dmmu.h"
#include "dmmu.h"
#include "coff.h"
#include "coff.h"
#include "elf.h"
#include "elf.h"
#include "debug_unit.h"
#include "debug_unit.h"
#include "opcode/or32.h"
#include "opcode/or32.h"
Line 35... Line 42...
#include "debug.h"
#include "debug.h"
 
 
#define MEMORY_LEN 0x100000000
#define MEMORY_LEN 0x100000000
#define MAXLINE_LEN 18000
#define MAXLINE_LEN 18000
 
 
#ifndef HAVE_ISBLANK
 
/*
 
 *isblank() is a GNU extension
 
 *not available on Solaris, for example
 
 */
 
static int isblank(int c)
 
{
 
  return (c==' ') || (c=='\t');
 
}
 
#endif
 
 
 
/* Whether to do immediate statistics */
/* Whether to do immediate statistics */
#define IMM_STATS 0
#define IMM_STATS 0
 
 
extern char *disassembled;
extern char *disassembled;
 
 
/* Unused mem memory marker. It is used when allocating program and data memory
/* Unused mem memory marker. It is used when allocating program and data memory
   during parsing */
   during parsing */
unsigned int freemem;
unsigned int freemem;
 
 
/* Translation table provided by microkernel. Only used if simulating microkernel. */
/* Translation table provided by microkernel. Only used if simulating microkernel. */
static unsigned long transl_table;
static oraddr_t transl_table;
 
 
/* Used to signal whether during loading of programs a translation fault occured. */
/* Used to signal whether during loading of programs a translation fault occured. */
static unsigned long transl_error;
static unsigned long transl_error;
 
 
 
 
Line 104... Line 100...
  *++t = '\0';
  *++t = '\0';
 
 
  return s;
  return s;
}
}
 
 
char *
 
dupstr (s)
 
     char *s;
 
{
 
  char *r;
 
 
 
  r = (char *)malloc (strlen (s) + 1);
 
  strcpy (r, s);
 
  return (r);
 
}
 
 
 
/* This function is very similar to strncpy, except it null terminates the string */
/* This function is very similar to strncpy, except it null terminates the string */
char *strstrip (char *dst, const char *src, int n)
char *strstrip (char *dst, const char *src, int n)
{
{
  strncpy (dst, src, n);
  strncpy (dst, src, n);
  *(dst + n) = '\0';
  *(dst + n) = '\0';
Line 146... Line 131...
 
 
/* Used only by the simulator loader to translate logical addresses into physical.
/* Used only by the simulator loader to translate logical addresses into physical.
   If loadcode() is called with valid virtphy_transl pointer to a table of
   If loadcode() is called with valid virtphy_transl pointer to a table of
   translations then translate() performs translation otherwise phy address is
   translations then translate() performs translation otherwise phy address is
   equal to logical. */
   equal to logical. */
static unsigned int translate(unsigned int laddr,int* breakpoint)
static oraddr_t translate(oraddr_t laddr,int* breakpoint)
{
{
  int i;
  int i;
 
 
  /* No translation (i.e. when loading kernel into simulator) */
  /* No translation (i.e. when loading kernel into simulator) */
/*  PRINTF("transl_table=%x  laddr=%x\n", transl_table, laddr);
/*  PRINTF("transl_table=%x  laddr=%x\n", transl_table, laddr);
Line 160... Line 145...
 
 
  /* Try to find our translation in the table. */
  /* Try to find our translation in the table. */
  for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
  for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
    if ((laddr & ~(PAGE_SIZE - 1)) == evalsim_mem32(transl_table + i)) {
    if ((laddr & ~(PAGE_SIZE - 1)) == evalsim_mem32(transl_table + i)) {
      setsim_mem32(transl_table + i + 8, -2); /* Page modified */
      setsim_mem32(transl_table + i + 8, -2); /* Page modified */
      PRINTF("found paddr=%lx\n", evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
      PRINTF("found paddr=%"PRIx32"\n",
      return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
             evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
 
      return (oraddr_t)evalsim_mem32(transl_table + i + 4) | (laddr & (oraddr_t)(PAGE_SIZE - 1));
    }
    }
 
 
  /* Allocate new phy page for us. */
  /* Allocate new phy page for us. */
  for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
  for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
    if (evalsim_mem32(transl_table + i + 8) == 0) {
    if (evalsim_mem32(transl_table + i + 8) == 0) {
      setsim_mem32(transl_table + i, laddr & ~(PAGE_SIZE - 1)); /* VPN */
      setsim_mem32(transl_table + i, laddr & ~(PAGE_SIZE - 1)); /* VPN */
      setsim_mem32(transl_table + i + 4, (i/16) * PAGE_SIZE); /* PPN */
      setsim_mem32(transl_table + i + 4, (i/16) * PAGE_SIZE); /* PPN */
      setsim_mem32(transl_table + i + 8, -2); /* Page modified */
      setsim_mem32(transl_table + i + 8, -2); /* Page modified */
      PRINTF("newly allocated ppn=%lx\n", (unsigned long)evalsim_mem32(transl_table + i + 4));
      PRINTF("newly allocated ppn=%"PRIx32"\n",
      PRINTF("newly allocated .ppn=%lx\n", (unsigned long)transl_table + i + 4);
             evalsim_mem32(transl_table + i + 4));
      PRINTF("newly allocated ofs=%lx\n", (unsigned long)(laddr & (PAGE_SIZE - 1)));
      PRINTF("newly allocated .ppn=%"PRIxADDR"\n", transl_table + i + 4);
      PRINTF("newly allocated paddr=%lx\n", (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
      PRINTF("newly allocated ofs=%"PRIxADDR"\n", (laddr & (PAGE_SIZE - 1)));
      return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
      PRINTF("newly allocated paddr=%"PRIx32"\n",
 
             evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
 
      return (oraddr_t)evalsim_mem32(transl_table + i + 4) | (laddr & (oraddr_t)(PAGE_SIZE - 1));
    }
    }
  /* If we come this far then all phy memory is used and we can't find our page
  /* If we come this far then all phy memory is used and we can't find our page
     nor allocate new page. */
     nor allocate new page. */
  transl_error = 1;
  transl_error = 1;
 
 
  PRINTF("can't translate %x\n", laddr);
  PRINTF("can't translate %"PRIxADDR"\n", laddr);
  exit(1);
  exit(1);
  return -1;
  return -1;
}
}
 
 
#if IMM_STATS
#if IMM_STATS
Line 197... Line 185...
  if (!val) return 0;
  if (!val) return 0;
  while (val != 0 && (signed long)val != -1) {i++; val = (signed long)val >> 1;}
  while (val != 0 && (signed long)val != -1) {i++; val = (signed long)val >> 1;}
  return i;
  return i;
}
}
 
 
void check_insn (unsigned long insn) {
void check_insn (uint32_t insn) {
  int insn_index = insn_decode (insn);
  int insn_index = insn_decode (insn);
  struct insn_op_struct *opd = op_start[insn_index];
  struct insn_op_struct *opd = op_start[insn_index];
  unsigned long data = 0;
  uint32_t data = 0;
  int dis = 0;
  int dis = 0;
  const char *name;
  const char *name;
  if (!insn || insn_index < 0) return;
  if (!insn || insn_index < 0) return;
  name = insn_name (insn_index);
  name = insn_name (insn_index);
  if (strcmp (name, "l.nop") == 0 || strcmp (name, "l.sys") == 0) return;
  if (strcmp (name, "l.nop") == 0 || strcmp (name, "l.sys") == 0) return;
 
 
  while (1)
  while (1)
    {
    {
      unsigned long tmp = 0, nbits = 0;
      uint32_t tmp = 0
 
      unsigned int nbits = 0;
      while (1)
      while (1)
        {
        {
          tmp |= ((insn  >> (opd->type & OPTYPE_SHR)) & ((1 << opd->data) - 1)) << nbits;
          tmp |= ((insn  >> (opd->type & OPTYPE_SHR)) & ((1 << opd->data) - 1)) << nbits;
          nbits += opd->data;
          nbits += opd->data;
          if (opd->type & OPTYPE_OP)
          if (opd->type & OPTYPE_OP)
Line 261... Line 250...
      opd++;
      opd++;
    }
    }
}
}
#endif
#endif
 
 
char null_str[1] = "\0";
 
 
 
/* Replaced several calls to translate(freemem) with vaddr */
/* Replaced several calls to translate(freemem) with vaddr */
/* Added new mode execution code */
/* Added new mode execution code */
/* Changed parameters so address can be passed as argument */
/* Changed parameters so address can be passed as argument */
void addprogram(unsigned long address, unsigned long insn, int* breakpoint)
void addprogram(oraddr_t address, uint32_t insn, int* breakpoint)
{
{
  int vaddr = (!runtime.sim.filename) ? translate(address,breakpoint) : translate(freemem,breakpoint);
  int vaddr = (!runtime.sim.filename) ? translate(address,breakpoint) : translate(freemem,breakpoint);
 
 
  setsim_mem32 (vaddr, insn);
  setsim_mem32 (vaddr, insn);
#if IMM_STATS
#if IMM_STATS
Line 284... Line 271...
 
 
void readfile_coff(char *filename, short sections)
void readfile_coff(char *filename, short sections)
{
{
  FILE *inputfs;
  FILE *inputfs;
  char inputbuf[4];
  char inputbuf[4];
  unsigned long insn;
  uint32_t insn;
  signed long sectsize;
  signed long sectsize;
  COFF_AOUTHDR coffaouthdr;
  COFF_AOUTHDR coffaouthdr;
  struct COFF_scnhdr coffscnhdr;
  struct COFF_scnhdr coffscnhdr;
  int  len;
  int  len;
  int  firstthree = 0;
  int  firstthree = 0;
Line 374... Line 361...
      insn = COFF_LONG_H(inputbuf);
      insn = COFF_LONG_H(inputbuf);
      len = insn_len (insn_decode (insn));
      len = insn_len (insn_decode (insn));
      if (len == 2)
      if (len == 2)
        {
        {
          fseek(inputfs, -2, SEEK_CUR);
          fseek(inputfs, -2, SEEK_CUR);
          debug(8,"readfile_coff: %x 0x%x   \n", sectsize, insn >> 16);
          debug(8,"readfile_coff: %lx 0x%x   \n", sectsize, insn >> 16);
        }
        }
      else
      else
        debug(8,"readfile_coff: %x 0x%x   \n", sectsize, insn);
        debug(8,"readfile_coff: %lx 0x%x   \n", sectsize, insn);
      addprogram (freemem, insn, &breakpoint);
      addprogram (freemem, insn, &breakpoint);
      sectsize -= len;
      sectsize -= len;
    }
    }
  }
  }
  if (firstthree < 3) {
  if (firstthree < 3) {
Line 451... Line 438...
          debug(8, "[%i] Symbol: %s,", count++, &tmp[0]);
          debug(8, "[%i] Symbol: %s,", count++, &tmp[0]);
        }
        }
        fseek(inputfs, fpos, SEEK_SET);
        fseek(inputfs, fpos, SEEK_SET);
      }
      }
 
 
      debug(9, " val: 0x%.8x,", COFF_LONG_H(coffsymhdr.e_value));
      debug(9, " val: 0x%.8lx,", COFF_LONG_H(coffsymhdr.e_value));
      debug(9, " type: %x, %x, auxs: %i\n", COFF_SHORT_H(coffsymhdr.e_type), *((unsigned short *)coffsymhdr.e_type), n);
      debug(9, " type: %x, %x, auxs: %i\n", COFF_SHORT_H(coffsymhdr.e_type), *((unsigned short *)coffsymhdr.e_type), n);
    }
    }
 
 
    for (i = 0; i < n; i++)
    for (i = 0; i < n; i++)
      if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
      if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
Line 483... Line 470...
  unsigned long syms = 0;
  unsigned long syms = 0;
  char *str_tbl = (char *)0;
  char *str_tbl = (char *)0;
  char *s_str = (char *)0;
  char *s_str = (char *)0;
  int breakpoint = 0;
  int breakpoint = 0;
  unsigned long inputbuf;
  unsigned long inputbuf;
  unsigned long insn, padd;
  unsigned long padd;
 
  uint32_t insn;
  int i, j, sectsize, len;
  int i, j, sectsize, len;
 
 
  if (!(inputfs = fopen(filename, "r"))) {
  if (!(inputfs = fopen(filename, "r"))) {
    perror("readfile_elf");
    perror("readfile_elf");
    exit(1);
    exit(1);
Line 641... Line 629...
  if (str_tbl) {
  if (str_tbl) {
    i = 0;
    i = 0;
    while(syms--) {
    while(syms--) {
      if (sym_tbl[i].st_name && sym_tbl[i].st_info && ELF_SHORT_H(sym_tbl[i].st_shndx) < 0x8000) {
      if (sym_tbl[i].st_name && sym_tbl[i].st_info && ELF_SHORT_H(sym_tbl[i].st_shndx) < 0x8000) {
        add_label(ELF_LONG_H(sym_tbl[i].st_value), &str_tbl[ELF_LONG_H(sym_tbl[i].st_name)]);
        add_label(ELF_LONG_H(sym_tbl[i].st_value), &str_tbl[ELF_LONG_H(sym_tbl[i].st_name)]);
        debug (8, "%08x(%s): %x %x %x\n", ELF_LONG_H(sym_tbl[i].st_value), &str_tbl[ELF_LONG_H(sym_tbl[i].st_name)], sym_tbl[i].st_info, sym_tbl[i].st_other, ELF_SHORT_H(sym_tbl[i].st_shndx));
        debug (8, "%08lx(%s): %x %x %x\n", ELF_LONG_H(sym_tbl[i].st_value), &str_tbl[ELF_LONG_H(sym_tbl[i].st_name)], sym_tbl[i].st_info, sym_tbl[i].st_other, ELF_SHORT_H(sym_tbl[i].st_shndx));
      }
      }
      i++;
      i++;
    }
    }
  }
  }
}
}
Line 718... Line 706...
  return;
  return;
}
}
 
 
 
 
/* Loads file to memory starting at address startaddr and returns freemem. */
/* Loads file to memory starting at address startaddr and returns freemem. */
unsigned long loadcode(char *filename, unsigned long startaddr, unsigned long virtphy_transl)
unsigned long loadcode(char *filename, oraddr_t startaddr, oraddr_t virtphy_transl)
{
{
  int breakpoint = 0;
  int breakpoint = 0;
 
 
  transl_error = 0;
  transl_error = 0;
  transl_table = virtphy_transl;
  transl_table = virtphy_transl;
  freemem = startaddr;
  freemem = startaddr;
  PRINTF("loadcode: filename %s  startaddr=%lx  virtphy_transl=%lx\n", filename, startaddr, virtphy_transl);
  PRINTF("loadcode: filename %s  startaddr=%"PRIxADDR"  virtphy_transl=%"PRIxADDR"\n",
 
         filename, startaddr, virtphy_transl);
  identifyfile(filename);
  identifyfile(filename);
 
 
#if IMM_STATS  
#if IMM_STATS  
  {
  {
    int i = 0, a = 0, b = 0, c = 0;
    int i = 0, a = 0, b = 0, c = 0;
    PRINTF ("index:arith/branch/jump\n");
    PRINTF ("index:arith/branch/jump\n");
    for (i = 0; i < 33; i++) PRINTF ("%2i:\t%3.0f%% / %3.0f%%/ %3.0f%%\t%5i / %5i / %5i\n", i, 100.* (a += bcnt[i][0])/bsum[0],
    for (i = 0; i < 33; i++)
        100.* (b += bcnt[i][1])/bsum[1], 100.* (c += bcnt[i][2])/bsum[2], bcnt[i][0], bcnt[i][1], bcnt[i][2]);
      PRINTF ("%2i:\t%3.0f%% / %3.0f%%/ %3.0f%%\t%5i / %5i / %5i\n", i,
 
              100.* (a += bcnt[i][0])/bsum[0], 100.* (b += bcnt[i][1])/bsum[1],
 
              100.* (c += bcnt[i][2])/bsum[2], bcnt[i][0], bcnt[i][1], bcnt[i][2]);
    PRINTF ("\nsum %i %i %i\n", bsum[0], bsum[1], bsum[2]);
    PRINTF ("\nsum %i %i %i\n", bsum[0], bsum[1], bsum[2]);
  }
  }
#endif
#endif
 
 
  if (transl_error)
  if (transl_error)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.