URL
https://opencores.org/ocsvn/or1k/or1k/trunk
Subversion Repositories or1k
Compare Revisions
- This comparison shows the changes necessary to convert path
/or1k/tags/or1k-1_0/or1ksim/cpu/common
- from Rev 249 to Rev 1765
- ↔ Reverse comparison
Rev 249 → Rev 1765
/stats.c
0,0 → 1,188
/* stats.c -- Various statistics about instruction scheduling etc. |
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org |
|
This file is part of OpenRISC 1000 Architectural Simulator. |
|
This program is free software; you can redistribute it and/or modify |
it under the terms of the GNU General Public License as published by |
the Free Software Foundation; either version 2 of the License, or |
(at your option) any later version. |
|
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
|
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
#include <stdio.h> |
#include <ctype.h> |
#include <string.h> |
|
#include "abstract.h" |
#include "stats.h" |
|
const char func_unit_str[30][30] = { "unknown", "arith", "shift", "compare", |
"branch", "jump", "load", "store", "movimm", "move", "extend", "nop" }; |
|
struct dstats_entry dstats[DSTATS_LEN]; /* dependency stats */ |
struct sstats_entry sstats[SSTATS_LEN]; /* single stats */ |
struct fstats_entry fstats[FSTATS_LEN]; /* functional units stats */ |
struct mstats_entry mstats; /* misc units stats */ |
|
/* Dependency */ |
|
int check_depend() |
{ |
debug("check_depend"); |
if (depend_operands(icomplet[0].dependdst, iqueue[0].dependsrc1) + |
depend_operands(icomplet[0].dependdst, iqueue[0].dependsrc2)) |
return 1; |
else |
return 0; |
} |
|
void addsstats(char *item, int cnt_dynamic, int cnt_static) |
{ |
int i = 0; |
|
while(strcmp(sstats[i].insn, item) && (sstats[i].cnt_static > 0) && |
(sstats[i].cnt_static > 0) && (i < SSTATS_LEN)) |
i++; |
|
if (i >= SSTATS_LEN - 1) return; |
|
if (strcmp(sstats[i].insn, item) == 0) { |
sstats[i].cnt_dynamic += cnt_dynamic; |
sstats[i].cnt_static += cnt_static; |
} |
else { |
strcpy(sstats[i].insn, item); |
sstats[i].cnt_dynamic = cnt_dynamic; |
sstats[i].cnt_static = cnt_static; |
} |
} |
|
void adddstats(char *item1, char *item2, int cnt_dynamic, int depend) |
{ |
int i = 0; |
|
debug("adddstats start\n"); |
|
while((strcmp(dstats[i].insn1, item1) || strcmp(dstats[i].insn2, item2)) && |
(strlen(dstats[i].insn1)) && |
(i < DSTATS_LEN)) |
i++; |
|
if (i >= DSTATS_LEN - 1) return; |
|
if ((strcmp(dstats[i].insn1, item1) == 0) && |
(strcmp(dstats[i].insn2, item2) == 0)) { |
dstats[i].cnt_dynamic += cnt_dynamic; |
dstats[i].depend += depend; |
} |
else { |
strcpy(dstats[i].insn1, item1); |
strcpy(dstats[i].insn2, item2); |
dstats[i].cnt_dynamic = cnt_dynamic; |
dstats[i].depend = depend; |
} |
} |
|
void addfstats(enum insn_type item1, enum insn_type item2, int cnt_dynamic, int depend) |
{ |
int i = 0; |
|
while(((fstats[i].insn1 != item1) || (fstats[i].insn2 != item2)) && |
(fstats[i].insn1 != unknown) && |
(i < FSTATS_LEN)) |
i++; |
|
if (i >= FSTATS_LEN - 1) return; |
|
if ((fstats[i].insn1 == item1) && |
(fstats[i].insn2 == item2)) { |
fstats[i].cnt_dynamic += cnt_dynamic; |
fstats[i].depend += depend; |
} |
else { |
fstats[i].insn1 = item1; |
fstats[i].insn2 = item2; |
fstats[i].cnt_dynamic = cnt_dynamic; |
fstats[i].depend = depend; |
} |
} |
|
void initstats() |
{ |
memset(sstats, 0, sizeof(sstats)); |
memset(dstats, 0, sizeof(dstats)); |
memset(fstats, 0, sizeof(fstats)); |
memset(&mstats, 0, sizeof(mstats)); |
} |
|
void printstats() |
{ |
int i, all = 0, dependall = 0; |
|
for(i = 0; i < SSTATS_LEN; i++) |
all += sstats[i].cnt_static; |
|
for(i = 0; i < SSTATS_LEN; i++) |
if (sstats[i].cnt_static) |
printf(" %s\t\tused %6dx (%2d%%)\n", sstats[i].insn, sstats[i].cnt_static, (sstats[i].cnt_static * 100)/all); |
|
printf("SUM: %d instructions (static, single stats)\n", all); |
|
all = 0; |
for(i = 0; i < SSTATS_LEN; i++) |
all += sstats[i].cnt_dynamic; |
|
for(i = 0; i < SSTATS_LEN; i++) |
if (sstats[i].cnt_dynamic) |
printf(" %s\t\tused %6dx (%2d%%)\n", sstats[i].insn, sstats[i].cnt_dynamic, (sstats[i].cnt_dynamic * 100)/all); |
|
printf("SUM: %d instructions (dynamic, single stats)\n", all); |
|
all = 0; |
dependall = 0; |
for(i = 0; i < DSTATS_LEN; i++) { |
all += dstats[i].cnt_dynamic; |
dependall += dstats[i].depend; |
} |
|
for(i = 0; i < DSTATS_LEN; i++) |
if (dstats[i].cnt_dynamic) { |
printf(" %s, %s ", dstats[i].insn1, dstats[i].insn2); |
printf("\t\t\t%6dx (%2d%%)", dstats[i].cnt_dynamic, (dstats[i].cnt_dynamic * 100)/all); |
printf(" depend: %3d%%\n", (dstats[i].depend * 100) / dstats[i].cnt_dynamic); |
} |
|
printf("SUM: %d instructions (dynamic, dependency stats) depend: %d%%\n", all, (dependall * 100) / all); |
|
all = 0; |
dependall = 0; |
for(i = 0; i < FSTATS_LEN; i++) { |
all += fstats[i].cnt_dynamic; |
dependall += fstats[i].depend; |
} |
|
for(i = 0; i < FSTATS_LEN; i++) |
if (fstats[i].cnt_dynamic) { |
printf(" %s,", func_unit_str[fstats[i].insn1]); |
printf(" %s", func_unit_str[fstats[i].insn2]); |
printf("\t\t\t%6dx (%2d%%)", fstats[i].cnt_dynamic, (fstats[i].cnt_dynamic * 100)/all); |
printf(" depend: %3d%%\n", (fstats[i].depend * 100) / fstats[i].cnt_dynamic); |
} |
|
printf("SUM: %d instructions (dynamic, functional units stats) depend: %d%%\n", all, (dependall * 100) / all); |
printf("Byte ADD: %d instructions\n", mstats.byteadd); |
printf("BEQZ: %d (%d%%) taken,", mstats.beqz.taken, (mstats.beqz.taken * 100) / (mstats.beqz.taken + mstats.beqz.nottaken)); |
printf(" %d (%d%%) not taken\n", mstats.beqz.nottaken, (mstats.beqz.nottaken * 100) / (mstats.beqz.taken + mstats.beqz.nottaken)); |
printf("BNEZ: %d (%d%%) taken,", mstats.bnez.taken, (mstats.bnez.taken * 100) / (mstats.bnez.taken + mstats.bnez.nottaken)); |
printf(" %d (%d%%) not taken\n", mstats.bnez.nottaken, (mstats.bnez.nottaken * 100) / (mstats.bnez.taken + mstats.bnez.nottaken)); |
printf("BPB: hit %d (correct %d%%), miss %d\n", mstats.bpb.hit, (mstats.bpb.correct * 100) / mstats.bpb.hit, mstats.bpb.miss); |
printf("BTIC: hit %d(%d%%), miss %d\n", mstats.btic.hit, (mstats.btic.hit * 100) / (mstats.btic.hit + mstats.btic.miss), mstats.btic.miss); |
} |
/trace.c
0,0 → 1,52
/* trace.c -- Simulator breakpoints |
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org |
|
This file is part of OpenRISC 1000 Architectural Simulator. |
|
This program is free software; you can redistribute it and/or modify |
it under the terms of the GNU General Public License as published by |
the Free Software Foundation; either version 2 of the License, or |
(at your option) any later version. |
|
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
|
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
#include <stdio.h> |
#include <ctype.h> |
#include <string.h> |
#include <stdlib.h> |
#include <signal.h> |
#include <stdarg.h> |
|
#include "arch.h" |
#include "parse.h" |
#include "abstract.h" |
#include "trace.h" |
#include "execute.h" |
|
/* Set instruction execution breakpoint. */ |
|
void set_insnbrkpoint(unsigned long addr) |
{ |
addr &= 0xfffffffc; /* 32-bit aligned */ |
|
if (addr < MEMORY_LEN) |
if (mem[addr].brk) { |
mem[addr].brk = 0; |
printf("\nBreakpoint at 0x%.8lx cleared.\n", addr); |
} else { |
mem[addr].brk = 1; |
printf("\nBreakpoint at 0x%.8lx set.\n", addr); |
} |
else |
printf("ERROR: Can't set this breakpoint out of memory.\n"); |
|
return; |
} |
|
/parse.h
0,0 → 1,28
/* parse.h -- Header file for parse.c |
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org |
|
This file is part of OpenRISC 1000 Architectural Simulator. |
|
This program is free software; you can redistribute it and/or modify |
it under the terms of the GNU General Public License as published by |
the Free Software Foundation; either version 2 of the License, or |
(at your option) any later version. |
|
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
|
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
/* Here we define some often used caharcters in assembly files. |
This wil probably go into architecture dependent directory. */ |
|
#define COMMENT_CHAR ';' |
#define DIRECTIVE_CHAR '.' |
#define LABELEND_CHAR ":" |
#define OPERAND_DELIM "," |
|
extern int nonempty(char *line); |
/trace.h
0,0 → 1,26
/* trace.h -- Header file for trace.c |
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org |
|
This file is part of OpenRISC 1000 Architectural Simulator. |
|
This program is free software; you can redistribute it and/or modify |
it under the terms of the GNU General Public License as published by |
the Free Software Foundation; either version 2 of the License, or |
(at your option) any later version. |
|
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
|
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
/* Continuos run versus single step tracing. */ |
extern int cont_run; |
|
/* History of execution */ |
#define HISTEXEC_LEN 200 |
extern int histexec[HISTEXEC_LEN]; |
|
/abstract.c
0,0 → 1,190
/* abstract.c -- Abstract entities |
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org |
|
This file is part of OpenRISC 1000 Architectural Simulator. |
|
This program is free software; you can redistribute it and/or modify |
it under the terms of the GNU General Public License as published by |
the Free Software Foundation; either version 2 of the License, or |
(at your option) any later version. |
|
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
|
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
/* Abstract memory and routines that goes with this. I need to |
add all sorts of other abstract entities. Currently we have |
only memory. */ |
|
#include <stdio.h> |
#include <ctype.h> |
#include <string.h> |
|
#include "parse.h" |
#include "abstract.h" |
#include "arch.h" |
#include "trace.h" |
#include "execute.h" |
|
extern unsigned long reg[]; |
|
/* This is an abstract memory array rather than physical memory array */ |
struct mem_entry mem[MEMORY_LEN]; |
|
void dumpmemory(unsigned int from, unsigned int to) |
{ |
unsigned int i; |
|
for(i = from; i < to; i++) |
if (strlen(mem[i].insn)) { |
printf("\n%.4x: ", i); |
if (strlen(mem[i].label)) |
printf("%s%s\n", mem[i].label, LABELEND_CHAR); |
printf("\t\t%s\t%s", mem[i].insn, mem[i].op1); |
if (strlen(mem[i].op2)) |
printf("%s%s", OPERAND_DELIM, mem[i].op2); |
if (strlen(mem[i].op3)) |
printf("%s%s", OPERAND_DELIM, mem[i].op3); |
if (strlen(mem[i].op4)) |
printf("%s%s", OPERAND_DELIM, mem[i].op4); |
i += 3; /* insn long 4 bytes */ |
} else |
{ |
if (i % 8 == 0) |
printf("\n%.4x: ", i); |
|
/* don't print ascii chars below 0x20. */ |
if (mem[i].data < 0x20) |
printf("0x%.2x ", (unsigned char)mem[i].data); |
else |
printf("0x%.2x'%c' ", (unsigned char)mem[i].data, mem[i].data); |
|
} |
} |
|
/* Searches mem array for a particular label and returns label's address. |
If label does not exist, returns 0. */ |
|
unsigned long eval_label(char *label) |
{ |
int i; |
|
for(i = 0; i < MEMORY_LEN; i++) |
if (strcmp(label, mem[i].label) == 0) |
return i; |
|
printf("\nINTERNAL ERROR: undefined label %s\n", label); |
cont_run = 0; |
return 0; |
|
} |
|
/* Returns 32-bit values from mem array. Big endian version. */ |
|
unsigned long eval_mem32(unsigned long memaddr) |
{ |
unsigned long temp; |
|
if (memaddr < MEMORY_LEN) { |
/* temp = ((unsigned long)(mem[memaddr].data << 24) & 0xff000000); |
temp += ((unsigned long)(mem[memaddr + 1].data << 16) & 0x00ff0000); |
temp += ((unsigned long)(mem[memaddr + 2].data << 8) & 0x0000ff00); |
temp += ((unsigned long)mem[memaddr + 3].data & 0x000000ff); */ |
temp = mem[memaddr].data << 24; |
temp += mem[memaddr + 1].data << 16; |
temp += mem[memaddr + 2].data << 8; |
temp += mem[memaddr + 3].data; |
|
} else { |
printf("EXCEPTION: read out of memory (32-bit access to %.8lx)\n", memaddr); |
cont_run = 0; |
temp = ((unsigned long)(mem[0].data << 24) & 0xff000000); |
temp += ((unsigned long)(mem[1].data << 16) & 0x00ff0000); |
temp += ((unsigned long)(mem[2].data << 8) & 0x0000ff00); |
temp += ((unsigned long)mem[3].data & 0x000000ff); |
} |
return temp; |
} |
|
/* Returns 16-bit values from mem array. Big endian version. */ |
|
unsigned short eval_mem16(unsigned long memaddr) |
{ |
unsigned short temp; |
|
if (memaddr < MEMORY_LEN) { |
temp = ((unsigned short)(mem[memaddr].data << 8) & 0xff00); |
temp += ((unsigned short)mem[memaddr + 1].data & 0x00ff); |
} else { |
printf("EXCEPTION: read out of memory (16-bit access to %.8lx)\n", memaddr); |
cont_run = 0; |
temp = ((unsigned short)(mem[0].data << 8) & 0xff00); |
temp += ((unsigned short)mem[1].data & 0x00ff); |
} |
return temp; |
} |
|
|
/* Returns 8-bit values from mem array. Big endian version. */ |
|
unsigned char eval_mem8(unsigned long memaddr) |
{ |
if (memaddr < MEMORY_LEN) { |
return (unsigned char)mem[memaddr].data; |
} else { |
printf("EXCEPTION: read out of memory (16-bit access to %.8lx)\n", memaddr); |
cont_run = 0; |
return (unsigned char)mem[0].data; |
} |
} |
|
/* Set mem, 32-bit. Big endian version. */ |
|
void set_mem32(unsigned long memaddr, unsigned long value) |
{ |
if (memaddr < MEMORY_LEN) { |
mem[memaddr].data = (value >> 24); |
mem[memaddr + 1].data = (char)(value >> 16); |
mem[memaddr + 2].data = (char)(value >> 8); |
mem[memaddr + 3].data = (char)(value); |
} else { |
printf("EXCEPTION: write out of memory (32-bit access to %.8lx)\n", memaddr); |
cont_run = 0; |
} |
|
return; |
} |
|
/* Set mem, 16-bit. Big endian version. */ |
|
void set_mem16(unsigned long memaddr, unsigned short value) |
{ |
if (memaddr < MEMORY_LEN) { |
mem[memaddr].data = (value >> 8); |
mem[memaddr + 1].data = (char)(value); |
} else { |
printf("EXCEPTION: write out of memory (16-bit access to %.8lx)\n", memaddr); |
cont_run = 0; |
} |
|
return; |
} |
|
/* Set mem, 8-bit. Big endian version. */ |
|
void set_mem8(unsigned long memaddr, unsigned char value) |
{ |
if (memaddr < MEMORY_LEN) { |
mem[memaddr].data = value; |
} else { |
printf("EXCEPTION: write out of memory (8-bit access to %.8lx)\n", memaddr); |
cont_run = 0; |
} |
|
return; |
} |
/stats.h
0,0 → 1,79
/* stats.h -- Header file for stats.c |
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org |
|
This file is part of OpenRISC 1000 Architectural Simulator. |
|
This program is free software; you can redistribute it and/or modify |
it under the terms of the GNU General Public License as published by |
the Free Software Foundation; either version 2 of the License, or |
(at your option) any later version. |
|
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
|
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
#define DSTATS_LEN 3000 |
#define SSTATS_LEN 300 |
#define FSTATS_LEN 200 |
|
struct dstats_entry { |
char insn1[OPERANDNAME_LEN]; |
char insn2[OPERANDNAME_LEN]; |
int cnt_dynamic; |
int depend; |
}; |
|
struct sstats_entry { |
char insn[OPERANDNAME_LEN]; |
int cnt_static; |
int cnt_dynamic; |
}; /* single stats */ |
|
struct fstats_entry { |
enum insn_type insn1; |
enum insn_type insn2; |
int cnt_dynamic; |
int depend; |
}; /* functional units stats */ |
|
struct branchstat { |
int taken; |
int nottaken; |
}; |
|
struct bpbstat { |
int hit; |
int miss; |
int correct; |
int incorrect; |
}; |
|
struct bticstat { |
int hit; |
int miss; |
}; |
|
struct mstats_entry { |
int byteadd; |
struct branchstat beqz; |
struct branchstat bnez; |
struct bpbstat bpb; |
struct bticstat btic; |
}; /* misc units stats */ |
|
extern struct mstats_entry mstats; |
extern struct sstats_entry sstats[SSTATS_LEN]; |
extern struct dstats_entry dstats[DSTATS_LEN]; |
extern struct fstats_entry fstats[FSTATS_LEN]; |
|
extern int check_depend(); |
extern void addsstats(char *item, int cnt_dynamic, int cnt_static); |
extern void adddstats(char *item1, char *item2, int cnt_dynamic, int depend); |
extern void addfstats(enum insn_type item1, enum insn_type item2, int cnt_dynamic, int depend); |
extern void initstats(); |
extern void printstats(); |
/execute.h
0,0 → 1,27
/* execute.h -- Header file for architecture dependent execute.c |
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org |
|
This file is part of OpenRISC 1000 Architectural Simulator. |
|
This program is free software; you can redistribute it and/or modify |
it under the terms of the GNU General Public License as published by |
the Free Software Foundation; either version 2 of the License, or |
(at your option) any later version. |
|
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
|
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
/* This needs a lot of work. */ |
|
extern machword eval_operand(char *srcoperand); |
extern void set_operand(char *dstoperand, unsigned long value); |
extern void dumpreg(); |
extern void set_reg32(char *regstr, unsigned long value); |
|
extern unsigned long pctemp; |
/abstract.h
0,0 → 1,81
/* abstract.c -- Abstract entities header file |
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org |
|
This file is part of OpenRISC 1000 Architectural Simulator. |
|
This program is free software; you can redistribute it and/or modify |
it under the terms of the GNU General Public License as published by |
the Free Software Foundation; either version 2 of the License, or |
(at your option) any later version. |
|
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
|
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
#define MEMORY_LEN 100000 |
#define STACK_SIZE 10000 |
#define LABELNAME_LEN 30 |
#define INSNAME_LEN 13 |
#define OPERANDNAME_LEN 30 |
|
/* This is an abstract memory type rather than physical memory type */ |
struct mem_entry { |
unsigned char data; |
unsigned char brk; |
char label[LABELNAME_LEN]; /* label name (optinal) */ |
char insn[INSNAME_LEN]; |
char op1[OPERANDNAME_LEN]; |
char op2[OPERANDNAME_LEN]; |
char op3[OPERANDNAME_LEN]; |
char op4[OPERANDNAME_LEN]; |
}; |
|
enum insn_type { unknown, arith, shift, compare, branch, |
jump, load, store, movimm, move, extend, nop }; |
|
/* Instruction queue */ |
struct iqueue_entry { |
char insn[INSNAME_LEN]; |
enum insn_type func_unit; |
char op1[OPERANDNAME_LEN]; |
char op2[OPERANDNAME_LEN]; |
char op3[OPERANDNAME_LEN]; |
char op4[OPERANDNAME_LEN]; |
char *dependdst; |
char *dependsrc1; |
char *dependsrc2; |
unsigned long insn_addr; |
}; |
|
/* Completition queue */ |
struct icomplet_entry { |
char insn[INSNAME_LEN]; |
enum insn_type func_unit; |
char op1[OPERANDNAME_LEN]; |
char op2[OPERANDNAME_LEN]; |
char op3[OPERANDNAME_LEN]; |
char op4[OPERANDNAME_LEN]; |
char *dependdst; |
char *dependsrc1; |
char *dependsrc2; |
unsigned long insn_addr; |
}; |
|
extern struct iqueue_entry iqueue[20]; |
extern struct icomplet_entry icomplet[20]; |
extern unsigned long pc; |
|
extern struct mem_entry mem[MEMORY_LEN]; |
extern void dumpmemory(unsigned int from, unsigned int to); |
extern unsigned long eval_label(char *label); |
extern unsigned long eval_mem32(unsigned long memaddr); |
extern unsigned short eval_mem16(unsigned long memaddr); |
extern unsigned char eval_mem8(unsigned long memaddr); |
extern void set_mem32(unsigned long memaddr, unsigned long value); |
extern void set_mem16(unsigned long memaddr, unsigned short value); |
extern void set_mem8(unsigned long memaddr, unsigned char value); |
/parse.c
0,0 → 1,299
/* parce.c -- Architecture independent load and parsing of assembly |
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org |
|
This file is part of OpenRISC 1000 Architectural Simulator. |
|
This program is free software; you can redistribute it and/or modify |
it under the terms of the GNU General Public License as published by |
the Free Software Foundation; either version 2 of the License, or |
(at your option) any later version. |
|
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
|
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
#include <stdio.h> |
#include <ctype.h> |
#include <string.h> |
#include <stdlib.h> |
|
#include "parse.h" |
#include "abstract.h" |
|
#define MAXLINE_LEN 18000 |
|
/* Unused mem memory marker. It is used when allocating program and data memory |
during parsing */ |
unsigned int freemem; |
|
int nonempty(char *line) |
{ |
int i; |
|
for(i = 0; i < strlen(line); i++) |
if (!isspace(line[i])) |
return(1); |
return(0); |
} |
|
int nondigit(char *line) |
{ |
int i; |
|
for(i = 0; i < strlen(line); i++) |
if (!isdigit(line[i])) |
return(1); |
return(0); |
} |
|
char *strtoken(char *in, char *out, int which) |
{ |
char *super; |
char *sub; |
char *newline; |
|
super = strdup(in); |
sub = strtok(super, " \t"); |
while (sub && --which) |
sub = strtok(NULL, " \t"); |
if (sub && !which) { |
if ((newline = strchr(sub, '\n'))) |
newline[0] = '\0'; |
strcpy(out, sub); |
} else |
out[0] = '\0'; |
free(super); |
if ((newline = strchr(out, '\r'))) /* get rid of CR */ |
newline[0] = '\0'; |
return(out); |
} |
|
void adddatastr(char *str) |
{ |
if (str) |
str++; |
else |
return; |
|
for(; *str && *str != '\"'; str++, freemem++) |
if (*str == '\\') |
switch (*++str) { |
case 'n': mem[freemem].data = '\n'; |
break; |
case 't': mem[freemem].data = '\t'; |
break; |
case 'r': mem[freemem].data = '\r'; |
break; |
case '0': mem[freemem].data = '\0'; |
break; |
default: break; |
} |
else |
mem[freemem].data = *str; |
} |
|
void adddataword(char *num) |
{ |
mem[freemem].data = (char) (atol(num) >> 24); |
mem[freemem + 1].data = (char) (atol(num) >> 16); |
mem[freemem + 2].data = (char) (atol(num) >> 8); |
mem[freemem + 3].data = (char) (atol(num)); |
|
freemem += 4; |
} |
|
void adddatahalf(char *num) |
{ |
mem[freemem].data = (char) (atol(num) >> 8); |
mem[freemem + 1].data = (char) (atol(num)); |
|
freemem += 2; |
} |
|
void adddatabyte(char *num) |
{ |
mem[freemem].data = (char) (atol(num)); |
|
freemem++; |
} |
|
void adddataspace(char *num) |
{ |
freemem += atol(num); |
} |
|
void addlabel(char *label) |
{ |
if (strstr(label, LABELEND_CHAR)) { |
*strstr(label, LABELEND_CHAR) = '\0'; |
strcpy(mem[freemem].label, label); |
} |
|
return; |
} |
|
void addprogram(char *insn, char *operands) |
{ |
|
strcpy(mem[freemem].insn, insn); |
|
/* op1 */ |
if (*operands) |
strcpy(mem[freemem].op1, operands); |
if (strstr(mem[freemem].op1, OPERAND_DELIM)) { |
operands = strstr(mem[freemem].op1, OPERAND_DELIM); |
*operands = '\0'; |
operands++; |
} else { |
freemem += 4; |
return; |
} |
|
/* op2 */ |
if (*operands) |
strcpy(mem[freemem].op2, operands); |
if (strstr(mem[freemem].op2, OPERAND_DELIM)) { |
operands = strstr(mem[freemem].op2, OPERAND_DELIM); |
*operands = '\0'; |
operands++; |
} else { |
freemem += 4; |
return; |
} |
|
/* op3 */ |
if (*operands) |
strcpy(mem[freemem].op3, operands); |
if (strstr(mem[freemem].op3, OPERAND_DELIM)) { |
operands = strstr(mem[freemem].op3, OPERAND_DELIM); |
*operands = '\0'; |
operands++; |
} else { |
freemem += 4; |
return; |
} |
|
/* op4 */ |
if (*operands) |
strcpy(mem[freemem].op4, operands); |
if (strstr(mem[freemem].op4, OPERAND_DELIM)) { |
operands = strstr(mem[freemem].op4, OPERAND_DELIM); |
*operands = '\0'; |
operands++; |
} |
|
freemem += 4; |
return; |
} |
|
/* Non-architecture dependent parsing: stripping comments, filling |
abstract memory */ |
|
void parseline(char *inputline) |
{ |
char item[MAXLINE_LEN]; |
char item2[MAXLINE_LEN]; |
int i = 0; |
|
/* Strip comments: simply terminate line where |
the first comment character appears. */ |
|
debug("PARSING: %s", inputline); |
while (inputline[i] != '\0') |
if (inputline[i] == COMMENT_CHAR) { |
inputline[i] = '\0'; |
break; |
} else |
i++; |
|
/* Get the first item from this line */ |
strtoken(inputline, item, 1); |
strtoken(inputline, item2, 2); |
|
/* Is this item empty? Nothing to process, so return. */ |
if (strlen(item) == 0) |
return; |
|
/* Is this item a label? If yes, add it to the label |
table and return immediately. */ |
if (strstr(item, LABELEND_CHAR)) { |
addlabel(item); |
return; |
} |
|
/* Is this item a .directive? If yes, check for some supported |
and then return (even if unsupported found). */ |
if (item[0] == DIRECTIVE_CHAR) { |
if ((strcmp(item, ".align") == 0) && (freemem % 4)) { |
freemem &= -4; /* e.g. 0xfffffffc */ |
freemem += 4; /* always align to word */ |
return; |
} else |
if (strcmp(item, ".ascii") == 0) { |
adddatastr(strstr(inputline, "\"")); |
return; |
} else |
if (strcmp(item, ".word") == 0) { |
adddataword(item2); |
return; |
} else |
if (strcmp(item, ".half") == 0) { |
adddatahalf(item2); |
return; |
} else |
if (strcmp(item, ".byte") == 0) { |
adddatabyte(item2); |
return; |
} else |
if (strcmp(item, ".space") == 0) { |
adddataspace(item2); |
return; |
} else /* .directive but not one of the supported */ |
return; |
} |
|
/* This item can only be an instruction. Get all operands |
and add everything to mem array but as a program. */ |
addprogram(item, item2); |
|
/* Also do static, single stats. */ |
addsstats(item, 0, 1); |
|
return; |
|
} |
|
/* Load file and hand over every line to parse routine. */ |
|
void readfile(char *filename) |
{ |
FILE *inputfs; |
char inputbuf[MAXLINE_LEN]; |
char *status; |
|
if ((inputfs = fopen(filename, "r"))) { |
while ((status = fgets(inputbuf, sizeof(inputbuf), inputfs))) { |
if (nonempty(inputbuf)) |
parseline(inputbuf); |
} |
fclose(inputfs); |
} |
else |
perror("readfile"); |
|
return; |
} |
|
|
void loadcode(char *filename) |
{ |
freemem = 0; |
memset(mem, 0, sizeof(mem)); |
readfile(filename); |
return; |
} |