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

📄 writecode.c

📁 gdb-6.0 linux 下的调试工具
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Code generator for the Hitachi H8/300 architecture simulator.   Written by Steve Chamberlain of Cygnus Support.   sac@cygnus.com   This file is part of H8/300 sim		THIS SOFTWARE IS NOT COPYRIGHTED   Cygnus offers the following for use in the public domain.  Cygnus   makes no warranty with regard to the software or it's performance   and the user accepts the software "AS IS" with all faults.   CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO   THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.*//* This program reads the H8/300 opcode table and writes out   a large switch statement to understand the opcodes (with ifs if   there is more than one opcode per case) and code to do the stuff  */#include <stdio.h>#define DEFINE_TABLE#define INSIM#include"opcode/h8300.h"#define MAXSAME 140#define PTWO 256static struct h8_opcode *h8_opcodes_sorted[PTWO][MAXSAME];char *cs = "/*";char *ce = "*/";/* How to get at nibble n from the instruction */char *nibs[] ={  "foo",  "(b0&0xf)",  "((b1>>4)&0xf)",  "((b1)&0xf)",  "((pc[1]>>12)&0xf)",  "((pc[1]>>8)&0xf)",  "((pc[1]>>4)&0xf)",  "((pc[1])&0xf)",  0, 0};/* how to get at the 3 bit immediate in the instruction */char *imm3[] ={"foo", "foo", "((b1>>4)&0x7)", "foo", "foo", "foo", "(pc[1]>>4)&0x7"};/* How to get at a byte register from an index in the instruction at   nibble n */char *breg[] ={"foo", "*(blow[b0])", "*(bhigh[b1])", "*(blow[b1])", 0, 0, "*(bhigh[pc[1]>>8])"};/* How to get at a word register from an index in the instruction at   nibble n */char *wreg[] ={"foo", "*(wlow[b0])", "*(whigh[b1])", "*(wlow[b1])"};#define sorted_key noperands/* sort the opcode table into h8_opcodes_sorted[0..255] */static voidinit (){  unsigned int i;  struct h8_opcode *p;  for (p = h8_opcodes; p->name; p++)    {      int n1 = 0;      int n2 = 0;      int j;#if 0      for (j = 0; p->data.nib[j] != E; j++)	{	  if ((int) p->data.nib[j] == ABS16ORREL8SRC)	    p->data.nib[j] = ABS16SRC;	  if ((int) p->data.nib[j] == ABS16OR8SRC)	    p->data.nib[j] = ABS16SRC;	  if ((int) p->data.nib[j] == ABS16OR8DST)	    p->data.nib[j] = ABS16DST;	}#endif      if ((int) p->data.nib[0] < 16)	{	  n1 = (int) p->data.nib[0];	}      else	n1 = 0;      if ((int) p->data.nib[1] < 16)	{	  n2 = (int) p->data.nib[1];	}      else	n2 = 0;      for (i = 0; i < MAXSAME; i++)	{	  int j = /* ((n3 >> 3) * 512) + ((n4 >> 3) * 256) + */ n1 * 16 + n2;	  if (h8_opcodes_sorted[j][i] == (struct h8_opcode *) NULL)	    {	      h8_opcodes_sorted[j][i] = p;	      p->sorted_key = j;	      break;	    }	}      if (i == MAXSAME)	abort ();      /* Just make sure there are an even number of nibbles in it, and       that the count is the same s the length */      for (i = 0; p->data.nib[i] != E; i++)	/*EMPTY*/ ;      if (i & 1)	abort ();      p->length = i / 2;    }  for (i = 0; i < PTWO; i++)    {      if (h8_opcodes_sorted[i][0])	p = h8_opcodes_sorted[i][0];      else	h8_opcodes_sorted[i][0] = p;    }}/* decode the lvalues, creating a pointer in real space to object - remember if the thing has to be swapped out of where it is */int swap[2];lval (p)     struct h8_opcode *p;{  int i;  for (i = 0; p->data.nib[i] != E; i++)    {      int x = p->data.nib[i];      int size;      int op;      op = (x & DST) ? 1 : 0;      switch (x & SIZE)	{	case L_32:	  size = 32;	  break;	case L_16:	  size = 16;	  break;	case L_8:	  size = 8;	  break;	default:	  size = 1234;	}      if (x & REG)	{	  printf ("ir%d = GET_LVAL_%d_REG(%d);\n", op, size, i);	}      else if (x & IMM)	{	  printf ("/* Imm has no lvalue */\n");	}    }}voiddecode (p, fetch, size)     struct h8_opcode *p;     int fetch;     int size;{  if (fetch)    {      lval (p);    }}static voidesleep (){  printf ("saved_state.exception = SIGSTOP;\n");}static voidmov (p, s, sz)     struct h8_opcode *p;     char *s;     int sz;{  printf ("dst = srca;\n");}static voidandc (p)     struct h8_opcode *p;{  printf ("SET_CCR(GET_CCR() & srca);\n");}static voidaddx (p)     struct h8_opcode *p;{  printf ("dst = srca + srcb+ (c != 0);\n");}static voidsubx (p)     struct h8_opcode *p;{  printf ("dst = srcb - srca - (c != 0);\n");}static voidadd (p, s, sz)     struct h8_opcode *p;     char *s;     int sz;{  printf ("%s;\n", s);}static voidadds (p, s)     struct h8_opcode *p;     char *s;{  printf ("%s;\n", s);}static voidbra (p, a)     struct h8_opcode *p;     char *a;{  printf ("if (%s) npc += ((char )b1)>>1;\n", a);}static voidbsr (p, a)     struct h8_opcode *p;     char *a;{  printf ("reg[7]-=2;\n");  printf ("tmp = reg[7];\n");  printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");  printf ("npc += ((char)b1)>>1;\n");}static voidcmp (p, a, s)     struct h8_opcode *p;     char *a;     int s;{  decode (p, 1, s);  printf ("srca = -srca;\n");  printf ("dst = srca + srcb;\n");}staticvoidjsr (p, a, s)     struct h8_opcode *p;     char *a;     int s;{  printf ("if (b1 == 0xc4) {\n");  printf ("printf(\"%%c\", reg[2]);\n");  printf ("}\n");  printf ("else {\n");  printf ("reg[7]-=2;\n");  printf ("tmp = reg[7];\n");  printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");  printf ("npc = (lval>>1) + saved_state.mem;\n");  printf ("}");}static voidjmp (p, a, s)     struct h8_opcode *p;     char *a;     int s;{  printf ("npc = (lval>>1) + saved_state.mem;\n");}static voidrts (p, a, s)     struct h8_opcode *p;     char *a;     int s;{  printf ("tmp = reg[7];\n");  printf ("reg[7]+=2;\n");  printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");}static voidrte (p, a, s)     struct h8_opcode *p;     char *a;     int s;{  printf ("reg[7]+=2;\n");  printf ("tmp = reg[7];\n");  printf ("reg[7]+=2;\n");  printf ("SET_CCR(tmp);\n");  printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");}static voidsetf (p, a, s)     struct h8_opcode *p;     char *a;     int s;{  printf ("tmp = GET_CCR();\n");  printf ("tmp %s= srca;\n", a);}static voidbpt (p, a, s)     struct h8_opcode *p;     char *a;     int s;{  printf ("saved_state.exception = SIGTRAP;\n");  printf ("npc = pc;\n");}static voidlog (p, a, s)     struct h8_opcode *p;     char *a;     int s;{  printf ("dst = srcb %s srca;\n", a);}static voidulog (p, a, s)     struct h8_opcode *p;     char *a;     int s;{  printf ("dst = %s srcb ;\n", a);}static voidnop (){}static voidrotl (){  printf ("c = srcb & 0x80;\n");  printf ("dst = srcb << 1;\n");  printf ("if (c) dst|=1;\n");}static voidrotr (){  printf ("c = srcb & 1;\n");  printf ("dst = srcb >> 1;\n");  printf ("if (c) dst|=0x80;\n");}static voidrotxl (){  printf ("tmp = srcb & 0x80;\n");  printf ("dst = srcb << 1;\n");  printf ("if (c) dst|=1;\n");  printf ("c = tmp;\n");}static voidrotxr (){  printf ("tmp = srcb & 1;\n");  printf ("dst = srcb >> 1;\n");  printf ("if (c) dst|=0x80;\n");  printf ("c = tmp;\n");}static voidshal (){  printf ("c = srcb&0x80;\n");  printf ("dst = srcb << 1;\n");}staticvoidshar (){  printf ("c = srcb&0x1;\n");  printf ("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n");  printf ("else  dst = (srcb>>1) &~ 0x80;\n");}staticvoidshll (){  printf ("c = srcb&0x80;\n");  printf ("dst = srcb << 1;\n");}staticvoidshlr (){  printf ("c = srcb&0x1;\n");  printf ("dst = (srcb>>1) &~ 0x80;\n");}staticvoiddivxu (){  printf ("srca = %s;\n", breg[2]);  printf ("srcb = %s;\n", wreg[3]);  printf ("n = srca & 0x80;\n");  printf ("z = !srca;\n");  printf ("if (srca) dst = srcb / srca;tmp = srcb %% srca;\n");  printf ("%s = (dst & 0xff) | (tmp << 8);\n", wreg[3]);}staticvoidmulxu (){  printf ("srca = %s;\n", breg[2]);  printf ("srcb = %s;\n", wreg[3]);  printf ("dst = (srcb&0xff) * srca;\n");  printf ("%s = dst;\n", wreg[3]);}staticvoidinc (){  printf ("dst = %s;\n", breg[3]);  printf ("v = (dst==0x7f);\n");  printf ("dst++;\n");  printf ("%s= dst;\n", breg[3]);}staticvoidbit (p, a, s)     struct h8_opcode *p;     char *a;     int s;{  printf ("%s\n", a);}staticvoiddec (){  printf ("dst = %s;\n", breg[3]);  printf ("v = (dst==0x80);\n");  printf ("dst--;\n");  printf ("%s = dst;\n", breg[3]);}char saf[] = "goto setflags;";char sf[] = "goto shiftflags;";char af8[] = "goto aluflags8;";char af16[] = "goto aluflags16;";char lf[] = "goto logflags;";char icf[] = "goto incflags;";char mf8[] = "goto movflags8;";char mf16[] = "goto movflags16;";

⌨️ 快捷键说明

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