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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [cpu/] [common/] [parse.c] - Diff between revs 42 and 67

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

Rev 42 Rev 67
Line 23... Line 23...
#include <stdlib.h>
#include <stdlib.h>
 
 
#include "parse.h"
#include "parse.h"
#include "abstract.h"
#include "abstract.h"
#include "arch.h"
#include "arch.h"
 
#include "dmmu.h"
#include "coff.h"
#include "coff.h"
 
 
#define MAXLINE_LEN     18000
#define MAXLINE_LEN     18000
 
 
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. */
 
static unsigned long transl_table;
 
 
 
/* Used to signal whether during loading of programs a translation fault occured. */
 
static unsigned long transl_error;
 
 
 
 
int nonempty(char *line)
int nonempty(char *line)
{
{
        int i;
        int i;
 
 
        for(i = 0; i < strlen(line); i++)
        for(i = 0; i < strlen(line); i++)
Line 75... Line 83...
        if ((newline = strchr(out, '\r')))      /* get rid of CR */
        if ((newline = strchr(out, '\r')))      /* get rid of CR */
                newline[0] = '\0';
                newline[0] = '\0';
        return(out);
        return(out);
}
}
 
 
 
/* Used only by the simulator loader to translate logical addresses int ophysical.
 
   If loadcode() is called with valid virtphy_transl pointer to a table of
 
   translations then translate() performs translation otherwise phy address is
 
   equal to logical. */
 
static unsigned int translate(unsigned int laddr)
 
{
 
        int i;
 
 
 
        /* No translation (i.e. when loading kernel into simulator)
 
/*      printf("transl_table=%x  laddr=%x\n", transl_table, laddr);
 
        printf("laddr=%x\n", laddr);*/
 
        if (transl_table == 0)
 
                return laddr;
 
 
 
        /* Try to find our translation in the table. */
 
        for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
 
                if ((laddr & ~(PAGE_SIZE - 1)) == evalsim_mem32(transl_table + i)) {
 
                        setsim_mem32(transl_table + i + 8, -2); /* Page modified */
 
                        printf("found paddr=%x\n", evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
 
                        return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
 
                }
 
 
 
        /* Allocate new phy page for us. */
 
        for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
 
                if (evalsim_mem32(transl_table + i + 8) == 0) {
 
                        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 + 8, -2); /* Page modified */
 
                        printf("newly allocated ppn=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4));
 
                        printf("newly allocated .ppn=%x\n", (unsigned long)transl_table + i + 4);
 
                        printf("newly allocated ofs=%x\n", (unsigned long)(laddr & (PAGE_SIZE - 1)));
 
                        printf("newly allocated paddr=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
 
                        return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
 
                }
 
        /* If we come this far then all phy memory is used and we can't find our page
 
           nor allocate new page. */
 
        transl_error = 1;
 
 
 
        printf("can't translate\n", laddr);
 
        exit(1);
 
        return -1;
 
}
 
 
void adddatastr(char *str)
void adddatastr(char *str)
{
{
        if (str)
        if (str)
                str++;
                str++;
        else
        else
                return;
                return;
 
 
        for(; *str && *str != '\"'; str++, freemem++)
        for(; *str && *str != '\"'; str++, translate(freemem++))
                if (*str == '\\')
                if (*str == '\\')
                        switch (*++str) {
                        switch (*++str) {
                                case 'n': mem[freemem].data = '\n';
                                case 'n': mem[translate(freemem)].data = '\n';
                                        break;
                                        break;
                                case 't': mem[freemem].data = '\t';
                                case 't': mem[translate(freemem)].data = '\t';
                                        break;
                                        break;
                                case 'r': mem[freemem].data = '\r';
                                case 'r': mem[translate(freemem)].data = '\r';
                                        break;
                                        break;
                                case '0': mem[freemem].data = '\0';
                                case '0': mem[translate(freemem)].data = '\0';
                                        break;
                                        break;
                                default: break;
                                default: break;
                        }
                        }
                else
                else
                        mem[freemem].data = *str;
                        mem[translate(freemem)].data = *str;
}
}
 
 
void adddataword(char *item)
void adddataword(char *item)
{
{
        unsigned long num;
        unsigned long num;
Line 108... Line 159...
        if (isdigit(*item))
        if (isdigit(*item))
                num = strtoul(item, NULL, 0);
                num = strtoul(item, NULL, 0);
        else
        else
                num = eval_label(item);
                num = eval_label(item);
 
 
        debug("adddataword: [0x%x] <= %x\n", freemem, num);
        debug("adddataword: [0x%x] <= %x\n", translate(freemem), num);
        mem[freemem].data = (char) (num >> 24);
        mem[translate(freemem)].data = (char) (num >> 24);
        mem[freemem + 1].data = (char) (num >> 16);
        mem[translate(freemem + 1)].data = (char) (num >> 16);
        mem[freemem + 2].data = (char) (num >> 8);
        mem[translate(freemem + 2)].data = (char) (num >> 8);
        mem[freemem + 3].data = (char) (num);
        mem[translate(freemem + 3)].data = (char) (num);
 
 
        freemem += 4;
        freemem += 4;
}
}
 
 
void adddatahalf(char *item)
void adddatahalf(char *item)
Line 126... Line 177...
        if (isdigit(*item))
        if (isdigit(*item))
                num = strtoul(item, NULL, 0);
                num = strtoul(item, NULL, 0);
        else
        else
                num = eval_label(item);
                num = eval_label(item);
 
 
        mem[freemem].data = (char) (num >> 8);
        mem[translate(freemem)].data = (char) (num >> 8);
        mem[freemem + 1].data = (char) (num);
        mem[translate(freemem + 1)].data = (char) (num);
 
 
        freemem += 2;
        freemem += 2;
}
}
 
 
void adddatabyte(char *item)
void adddatabyte(char *item)
Line 141... Line 192...
        if (isdigit(*item))
        if (isdigit(*item))
                num = strtoul(item, NULL, 0);
                num = strtoul(item, NULL, 0);
        else
        else
                num = eval_label(item);
                num = eval_label(item);
 
 
        mem[freemem].data = (char) (num);
        mem[translate(freemem)].data = (char) (num);
 
 
        freemem++;
        freemem++;
}
}
 
 
void adddataspace(char *num)
void adddataspace(char *num)
Line 155... Line 206...
 
 
void addlabel(char *label, unsigned long freemem)
void addlabel(char *label, unsigned long freemem)
{
{
        struct label_entry **tmp;
        struct label_entry **tmp;
 
 
        debug("Adding label %s at 0x%x\n", label, freemem);
        debug("Adding label %s at 0x%x\n", label, translate(freemem));
        tmp = &mem[freemem].label;
        tmp = &mem[translate(freemem)].label;
        for (; *tmp; tmp = &((*tmp)->next));
        for (; *tmp; tmp = &((*tmp)->next));
        *tmp = malloc(sizeof(**tmp));
        *tmp = malloc(sizeof(**tmp));
        (*tmp)->name = malloc(strlen(label)+1);
        (*tmp)->name = malloc(strlen(label)+1);
        strcpy((*tmp)->name, label);
        strcpy((*tmp)->name, label);
        (*tmp)->next = NULL;
        (*tmp)->next = NULL;
Line 174... Line 225...
{
{
        int h_insn_is_word_flag=0;
        int h_insn_is_word_flag=0;
        char insn_first2_char[3];
        char insn_first2_char[3];
 
 
        debug("addprogram 1\n");
        debug("addprogram 1\n");
        if (!mem[freemem].insn) {
        if (!mem[translate(freemem)].insn) {
                mem[freemem].insn = malloc(sizeof(*mem[freemem].insn));
                mem[translate(freemem)].insn = malloc(sizeof(*mem[translate(freemem)].insn));
                mem[freemem].insn->insn = null_str;
                mem[translate(freemem)].insn->insn = null_str;
                mem[freemem].insn->op1 = null_str;
                mem[translate(freemem)].insn->op1 = null_str;
                mem[freemem].insn->op2 = null_str;
                mem[translate(freemem)].insn->op2 = null_str;
                mem[freemem].insn->op3 = null_str;
                mem[translate(freemem)].insn->op3 = null_str;
                mem[freemem].insn->op4 = null_str;
                mem[translate(freemem)].insn->op4 = null_str;
        } else {
        } else {
                printf("internal error: reloading the same location\n");
                printf("internal error: reloading the same location\n");
                exit(1);
                exit(1);
        }
        }
        debug("addprogram 2\n");
        debug("addprogram 2\n");
 
 
        mem[freemem].insn->insn = malloc(strlen(insn)+1);
        mem[translate(freemem)].insn->insn = malloc(strlen(insn)+1);
 
 
#ifdef  OR16
#ifdef  OR16
 
 
        strcpy(mem[freemem].insn->insn, insn);
        strcpy(mem[translate(freemem)].insn->insn, insn);
        printf("half:%s:\n", insn);
        printf("half:%s:\n", insn);
        insn_first2_char[0]=insn[0];
        insn_first2_char[0]=insn[0];
        insn_first2_char[1]=insn[1];
        insn_first2_char[1]=insn[1];
        insn_first2_char[2]='\0';
        insn_first2_char[2]='\0';
        debug("addprogram 3\n");
        debug("addprogram 3\n");
Line 225... Line 276...
        else {
        else {
                        h_insn_is_word_flag = 0; /* not h.xxx insn */
                        h_insn_is_word_flag = 0; /* not h.xxx insn */
        }
        }
#else
#else
        debug("addprogram 4\n");
        debug("addprogram 4\n");
        strcpy(mem[freemem].insn->insn, insn);
        strcpy(mem[translate(freemem)].insn->insn, insn);
        debug("addprogram 5\n");
        debug("addprogram 5\n");
#endif
#endif
 
 
        /* op1 */
        /* op1 */
        if (*operands) {
        if (*operands) {
                mem[freemem].insn->op1 = malloc(strlen(operands)+1);
                mem[translate(freemem)].insn->op1 = malloc(strlen(operands)+1);
                strcpy(mem[freemem].insn->op1, operands);
                strcpy(mem[translate(freemem)].insn->op1, operands);
        }
        }
 
 
        debug("addprogram 6\n");
        debug("addprogram 6\n");
        debug("operands:%s\n", operands);
        debug("operands:%s\n", operands);
        if (strstr(operands, OPERAND_DELIM)) {
        if (strstr(operands, OPERAND_DELIM)) {
                debug("addprogram 6a\n");
                debug("addprogram 6a\n");
                operands = strstr(mem[freemem].insn->op1, OPERAND_DELIM);
                operands = strstr(mem[translate(freemem)].insn->op1, OPERAND_DELIM);
                *operands = '\0';
                *operands = '\0';
                operands++;
                operands++;
        } else {
        } else {
                debug("addprogram 6b\n");
                debug("addprogram 6b\n");
#ifdef OR16
#ifdef OR16
Line 255... Line 306...
        }
        }
 
 
        debug("addprogram 7\n");
        debug("addprogram 7\n");
        /* op2 */
        /* op2 */
        if (*operands) {
        if (*operands) {
                mem[freemem].insn->op2 = malloc(strlen(operands)+1);
                mem[translate(freemem)].insn->op2 = malloc(strlen(operands)+1);
                strcpy(mem[freemem].insn->op2, operands);
                strcpy(mem[translate(freemem)].insn->op2, operands);
        }
        }
        if (strstr(operands, OPERAND_DELIM)) {
        if (strstr(operands, OPERAND_DELIM)) {
                operands = strstr(mem[freemem].insn->op2, OPERAND_DELIM);
                operands = strstr(mem[translate(freemem)].insn->op2, OPERAND_DELIM);
                *operands = '\0';
                *operands = '\0';
                operands++;
                operands++;
        } else {
        } else {
#ifdef OR16
#ifdef OR16
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
Line 274... Line 325...
        }
        }
 
 
        debug("addprogram 8\n");
        debug("addprogram 8\n");
        /* op3 */
        /* op3 */
        if (*operands) {
        if (*operands) {
                mem[freemem].insn->op3 = malloc(strlen(operands)+1);
                mem[translate(freemem)].insn->op3 = malloc(strlen(operands)+1);
                strcpy(mem[freemem].insn->op3, operands);
                strcpy(mem[translate(freemem)].insn->op3, operands);
        }
        }
        if (strstr(operands, OPERAND_DELIM)) {
        if (strstr(operands, OPERAND_DELIM)) {
                operands = strstr(mem[freemem].insn->op3, OPERAND_DELIM);
                operands = strstr(mem[translate(freemem)].insn->op3, OPERAND_DELIM);
                *operands = '\0';
                *operands = '\0';
                operands++;
                operands++;
        } else {
        } else {
#ifdef OR16
#ifdef OR16
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
Line 292... Line 343...
                return;
                return;
        }
        }
 
 
        /* op4 */
        /* op4 */
        if (*operands) {
        if (*operands) {
                mem[freemem].insn->op4 = malloc(strlen(operands)+1);
                mem[translate(freemem)].insn->op4 = malloc(strlen(operands)+1);
                strcpy(mem[freemem].insn->op4, operands);
                strcpy(mem[translate(freemem)].insn->op4, operands);
        }
        }
        if (strstr(operands, OPERAND_DELIM)) {
        if (strstr(operands, OPERAND_DELIM)) {
                operands = strstr(mem[freemem].insn->op4, OPERAND_DELIM);
                operands = strstr(mem[translate(freemem)].insn->op4, OPERAND_DELIM);
                *operands = '\0';
                *operands = '\0';
                operands++;
                operands++;
        }
        }
 
 
#ifdef OR16
#ifdef OR16
Line 340... Line 391...
                return;
                return;
 
 
        /* Is this item a label? If yes, add it to the label table and return immediately. */
        /* Is this item a label? If yes, add it to the label table and return immediately. */
        if (strstr(item, LABELEND_CHAR)) {
        if (strstr(item, LABELEND_CHAR)) {
                *strstr(item, LABELEND_CHAR) = '\0';
                *strstr(item, LABELEND_CHAR) = '\0';
                addlabel(item, freemem);
                addlabel(item, translate(freemem));
                return;
                return;
        }
        }
 
 
        /* Is this item a .directive? If yes, check for some supported
        /* Is this item a .directive? If yes, check for some supported
           and then return (even if unsupported found). */
           and then return (even if unsupported found). */
        if (item[0] == DIRECTIVE_CHAR) {
        if (item[0] == DIRECTIVE_CHAR) {
                if (strcmp(item, ".align") == 0) {
                if (strcmp(item, ".align") == 0) {
                        int align = strtoul(item2, NULL, 0);
                        int align = strtoul(item2, NULL, 0);
                        if (!(freemem % align))
                        if (!(translate(freemem) % align))
                                return;
                                return;
                        freemem &= -align;
                        freemem &= -align;
                        freemem += align;
                        freemem += align;
                        return;
                        return;
                } else
                } else
Line 385... Line 436...
                        return;
                        return;
        }
        }
 
 
        /* This item can only be an instruction. Get all operands
        /* This item can only be an instruction. Get all operands
           and add everything to mem array but as a program. */
           and add everything to mem array but as a program. */
        debug("%x: ", freemem);
        debug("%x: ", translate(freemem));
        addprogram(item, item2);
        addprogram(item, item2);
 
 
        /* Also do static, single stats. */
        /* Also do static, single stats. */
        addsstats(item, 0, 1);
        addsstats(item, 0, 1);
 
 
Line 447... Line 498...
                printf(" size: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_size));
                printf(" size: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_size));
                printf(" scnptr: 0x%.8x\n", COFF_LONG_H(coffscnhdr.s_scnptr));
                printf(" scnptr: 0x%.8x\n", COFF_LONG_H(coffscnhdr.s_scnptr));
 
 
                sectsize = COFF_LONG_H(coffscnhdr.s_size);
                sectsize = COFF_LONG_H(coffscnhdr.s_size);
                /* A couple of sanity checks. */
                /* A couple of sanity checks. */
                if (COFF_LONG_H(coffscnhdr.s_vaddr) < MEMORY_START) {
                if (translate(COFF_LONG_H(coffscnhdr.s_vaddr)) < MEMORY_START) {
                        printf("Section %s starts out of ", coffscnhdr.s_name);
                        printf("Section %s starts out of ", coffscnhdr.s_name);
                        printf("memory (at %x)\n", COFF_LONG_H(coffscnhdr.s_vaddr));
                        printf("memory (at %x)\n", COFF_LONG_H(coffscnhdr.s_vaddr));
                        exit(1);
                        exit(1);
                }
                }
                if (COFF_LONG_H(coffscnhdr.s_vaddr) + sectsize >
                if (translate(COFF_LONG_H(coffscnhdr.s_vaddr) + sectsize) >
                    MEMORY_START + MEMORY_LEN) {
                    MEMORY_START + MEMORY_LEN) {
                        printf("Section %s ends out of ", coffscnhdr.s_name);
                        printf("Section %s ends out of ", coffscnhdr.s_name);
                        printf("memory.\n");
                        printf("memory.\n");
                        exit(1);
                        exit(1);
                }
                }
Line 473... Line 524...
                        exit(1);
                        exit(1);
                }
                }
 
 
                /* loading section */
                /* loading section */
                freemem = COFF_LONG_H(coffscnhdr.s_vaddr);
                freemem = COFF_LONG_H(coffscnhdr.s_vaddr);
 
                debug("Starting to load at 0x%x", freemem);
                if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
                if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
                        fclose(inputfs);
                        fclose(inputfs);
                        perror("readfile_coff");
                        perror("readfile_coff");
                        exit(1);
                        exit(1);
                }
                }
Line 581... Line 633...
        FILE *inputfs;
        FILE *inputfs;
        struct COFF_filehdr coffhdr;
        struct COFF_filehdr coffhdr;
        size_t len;
        size_t len;
 
 
        if (!(inputfs = fopen(filename, "r"))) {
        if (!(inputfs = fopen(filename, "r"))) {
                perror("identifyfile");
                fprintf(stderr, "xx %s", filename);
 
                perror("identifyfile1");
 
                fflush(stdout);
 
                fflush(stderr);
                exit(1);
                exit(1);
        }
        }
 
 
        if (fread(&coffhdr, sizeof(coffhdr), 1, inputfs) == 1) {
        if (fread(&coffhdr, sizeof(coffhdr), 1, inputfs) == 1) {
                if (COFF_SHORT_H(coffhdr.f_magic) == 0x17a) {
                if (COFF_SHORT_H(coffhdr.f_magic) == 0x17a) {
Line 613... Line 668...
                        fclose(inputfs);
                        fclose(inputfs);
                        readfile_assembly(filename);
                        readfile_assembly(filename);
                        return;
                        return;
                }
                }
        }
        }
        else
        else {
                perror("identifyfile");
                printf("yy %s", filename);
 
                perror("identifyfile2");
 
        }
 
 
        fclose(inputfs);
        fclose(inputfs);
 
 
        return;
        return;
}
}
 
 
void loadcode(char *filename)
 
 
/* Loads file to memory starting at address startaddr and returns freemem. */
 
unsigned long loadcode(char *filename, unsigned long startaddr, unsigned long virtphy_transl)
{
{
        freemem = MEMORY_START;
        transl_error = 0;
        memset(mem, 0, sizeof(mem));
        transl_table = virtphy_transl;
 
        freemem = startaddr;
 
        printf("loadcode: filename %s  startaddr=%x  virtphy_transl=%x", filename, startaddr, virtphy_transl);
        identifyfile(filename);
        identifyfile(filename);
        return;
        if (transl_error)
 
                return -1;
 
        else
 
                return translate(freemem);
}
}
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

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