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

📄 jopvm.c

📁 ALTERA的NIOS处理器!文件直接可以打开直接选择器件重新编译!
💻 C
📖 第 1 页 / 共 3 页
字号:
	}
	c[0]=a;c[1]=b;
	return t;
}
#endif

#ifdef __GNUC__
long long readTSC (void)
{

  long long t;
  __asm__ __volatile__ (".byte 0x0f, 0x31" : "=A" (t));

  return t;      
}
#endif


int usCntClock() {
	return (int) (clock()*(1000000/CLOCKS_PER_SEC));
}

static long divide;

int getMHz() {

	int i, tim;
#ifdef _MSC_VER
	_int64 tsc1, tsc2; 
#endif
#ifdef __GNUC__
	long long tsc1, tsc2;
#endif

	i = usCntClock();
	while (i == (tim=usCntClock()))		// wait for a tick
		;
	tsc1 = readTSC();

	tim +=1000000;
	while (tim-usCntClock() > 0)		// wait one second
		;
	tsc2 = readTSC();
	tsc2 -= tsc1;
	tim = (tsc2+500000)/1000000;

	divide = tim;

	return tim;
}

int usCnt() {

	return (int) (readTSC()/divide);
}

#endif // __nios__

void sysRd() 
{
	int addr = stack[sp];
	switch(addr) 
	{
		case Native_IO_STATUS:
			stack[sp] = -1;
			break;
		case Native_IO_UART:
			stack[sp] = 'u';
			break;
		case Native_IO_CNT:
#ifdef __nios__
			stack[sp] = usCnt()*20;
#else
			stack[sp] = readTSC(); // processor clock
#endif
			break;
		case Native_IO_US_CNT:
			stack[sp] = usCnt();
			break;
		case 11:
			stack[sp] = 0 /* ioCnt/24000 */;
			break;
		default:
			stack[sp] = 0;
	}
}

void sysWr() {

	int addr = stack[sp--];
	int val = stack[sp--];
	switch(addr) {

		case Native_IO_UART:
			// printf("\t->%c<-\n", val);
			printf("%c", val);
			fflush(stdout);
			break;
		case Native_IO_INT_ENA:
			intEna = val;
			break;
		case Native_IO_TIMER:
			intPend = 0;		// reset pending interrupt
			interrupt = 0;		// for shure ???
			nextTimerInt = val;
			break;
		case Native_IO_SWINT:
			if (!intPend) {
				interrupt = 1;
				intPend = 1;
			}
			break;
	}
}

/**
 *	start of JVM :-)
 */

void invokestatic();
	
void invokespecial() 
{
	invokestatic();				/* what's the difference? */
}

void invokevirtual() 
{
	int idx = readOpd16u();
	unsigned int off = readMem(cp+idx);			/* index in vt and arg count (-1) */
	int args = off & 0xff;
	int ref = stack[sp-args];
	int vt = readMem(ref-1);
	off = (unsigned int) off >> 8;
	
DEBUG(printf("invokevirtual: off = %d, args = %d, ref = %d, vt = %d, addr = %d\n",
		off, args, ref, vt, vt + off);)

	invoke(vt + off);
}

	void invokeinterface() {

		int idx, args, ref, vt, it, mp;
		unsigned int off;

		idx = readOpd16u();
		readOpd16u();				// read historical argument count and 0

		off = readMem(cp+idx);			// index in interface table

		args = off & 0xff;				// this is args count without obj-ref
		off = (unsigned int) off >> 8;
		ref = stack[sp-args];

		vt = readMem(ref-1);			// pointer to virtual table in obj-1
		it = readMem(vt-1);				// pointer to interface table one befor vt

		mp = readMem(it+off);
// System.out.println("invint: off: "+off+" args: "+args+" ref: "+ref+" vt: "+vt+" mp: "+(mp));
		invoke(mp);
	}

void invokestatic()
{
	int idx = readOpd16u();
	invokestaticAddr(cp + idx);
}

void invokestaticAddr(int ptr)
{
	invoke(readMem(ptr));
}

/**
 *	return wie es sein sollte (oder doch nicht?)
 */

void vreturn() 
{
	unsigned int start;
	int len;
DEBUG(dump();)
	
	mp = stack[sp--];
	cp = stack[sp--];
	vp = stack[sp--];
	pc = stack[sp--];
	sp = stack[sp]; /* Nils */

DEBUG(dump();)
	
	start = readMem(mp);
	len = start & 0x03ff;
	start = (unsigned int) start >> 10;
	// cp = readMem(mp+1)>>>10;

DEBUG(printf("start = %d, len = %d\n", start, len);)
	loadBc(start, len);
}

void ireturn() 
{
	int val = stack[sp--];
	vreturn();
	stack[++sp] = val;
}

	void lreturn() {

		int val1 = stack[sp--];
		int val2 = stack[sp--];
		vreturn();
		stack[++sp] = val2;
		stack[++sp] = val1;
	}

/**
/**
 *
 */

void putstatic() 
{
	int idx = readOpd16u();
	int addr = readMem(cp + idx);	// not now
// System.out.println("putstatic address: "+addr+" TOS: "+stack[sp]);
	writeMem(addr, stack[sp--]);
}

void getstatic() 
{
	int idx = readOpd16u();
	int addr = readMem(cp + idx);	// not now
// System.out.println("getstatic address: "+addr+" TOS: "+stack[sp]);
	stack[++sp] = readMem(addr);
}

void putfield() 
{
	int idx = readOpd16u();
	int off = readMem(cp+idx);
	int val = stack[sp--];
	writeMem(stack[sp--] + off, val);
}

void getfield() 
{
	int idx = readOpd16u();
	int off = readMem(cp + idx);
	stack[sp] = readMem(stack[sp] + off);
}

void interprete() 
{
	int new_pc;
	int ref, val, idx;
	int old_pc = -1;
	int old_mp = -1;

	int a, b, c, d;

	for (;;) 
	{
		int instr;
		
		/*
		 *	check for endless loop and stop
		 */
/*
		if(pc == old_pc && mp == old_mp) 
		{
			printf("\nendless loop\n");
			break;
		}
		
		old_pc = pc;
		old_mp = mp;
*/
		
		if(maxInstr != 0 && instrCnt >= maxInstr)
			break;

		/**
		 *	statistic
		 */
		STATISTICS(++instrBytesCnt;)
		++instrCnt;
		STATISTICS(if(sp > maxSp) maxSp = sp;)

		instr = bc[pc++] & 0x0ff;

//
//	interrupt handling
//
		if ((nextTimerInt-usCnt()<0) && !intPend) {
			intPend = 1;
			interrupt = 1;
		}
		if (interrupt && intEna) {
			instr = SYS_INT;
			interrupt = 0;		// reset int
		}

		STATISTICS(bcStat[instr]++;)
		STATISTICS(ioCnt += JopInstr.cnt(instr);)

		DEBUG(printf("%4d %s\n", pc-1, jopInstruction[instr].name);)

		switch(instr) {

			case 0:		// nop
				break;
			case 1:		// aconst_null
				stack[++sp] = 0;
				break;
			case 2:		// iconst_m1
				stack[++sp] = -1;
				break;
			case 3:		// iconst_0
				stack[++sp] = 0;
				break;
			case 4:		// iconst_1
				stack[++sp] = 1;
				break;
			case 5:		// iconst_2
				stack[++sp] = 2;
				break;
			case 6:		// iconst_3
				stack[++sp] = 3;
				break;
			case 7:		// iconst_4
				stack[++sp] = 4;
				break;
			case 8:		// iconst_5
				stack[++sp] = 5;
				break;
			case 9 :		// lconst_0
				stack[++sp] = 0;
				stack[++sp] = 0;
				break;
			case 10 :		// lconst_1
				stack[++sp] = 0;
				stack[++sp] = 1;
				break;
			case 11 :		// fconst_0
				noim(11);
				break;
			case 12 :		// fconst_1
				noim(12);
				break;
			case 13 :		// fconst_2
				noim(13);
				break;
			case 14 :		// dconst_0
				noim(14);
				break;
			case 15 :		// dconst_1
				noim(15);
				break;
			case 16 :		// bipush
				stack[++sp] = readOpd8s();
				break;
			case 17 :		// sipush
				stack[++sp] = readOpd16s();
				break;
			case 18 :		// ldc
				stack[++sp] = readMem(cp+readOpd8u());
				break;
			case 19 :		// ldc_w
				stack[++sp] = readMem(cp+readOpd16u());
				break;
			case 20 :		// ldc2_w
				idx = readOpd16u();
				stack[++sp] = readMem(cp+idx);
				stack[++sp] = readMem(cp+idx+1);
				break;
			case 25 :		// aload
			case 23 :		// fload
			case 21 :		// iload
				idx = readOpd8u();
				stack[++sp] = stack[vp+idx];
				break;
			case 22 :		// lload
				idx = readOpd8u();
				stack[++sp] = stack[vp+idx];
				stack[++sp] = stack[vp+idx+1];
				break;
			case 24 :		// dload
				idx = readOpd8u();
				stack[++sp] = stack[vp+idx];
				stack[++sp] = stack[vp+idx+1];
				break;
			case 42 :		// aload_0
			case 34 :		// fload_0
			case 26 :		// iload_0
				stack[++sp] = stack[vp];
				break;
			case 43 :		// aload_1
			case 35 :		// fload_1
			case 27 :		// iload_1
				stack[++sp] = stack[vp+1];
				break;
			case 44 :		// aload_2
			case 36 :		// fload_2
			case 28 :		// iload_2
				stack[++sp] = stack[vp+2];
				break;
			case 45 :		// aload_3
			case 37 :		// fload_3
			case 29 :		// iload_3
				stack[++sp] = stack[vp+3];
				break;
			case 30 :		// lload_0
				stack[++sp] = stack[vp];
				stack[++sp] = stack[vp+1];
				break;
			case 31 :		// lload_1
				stack[++sp] = stack[vp+1];
				stack[++sp] = stack[vp+2];
				break;
			case 32 :		// lload_2
				stack[++sp] = stack[vp+2];
				stack[++sp] = stack[vp+3];
				break;
			case 33 :		// lload_3
				stack[++sp] = stack[vp+3];
				stack[++sp] = stack[vp+4];
				break;
			case 38 :		// dload_0
				noim(38);
				break;
			case 39 :		// dload_1
				noim(39);
				break;
			case 40 :		// dload_2
				noim(40);
				break;
			case 41 :		// dload_3
				noim(41);
				break;
			case 50 :		// aaload
			case 51 :		// baload
			case 52 :		// caload
			case 48 :		// faload
			case 46 :		// iaload
			case 53 :		// saload
				ref = stack[sp--];	// index
				ref += stack[sp--];	// ref
				stack[++sp] = readMem(ref);
				break;
			case 47 :		// laload
				noim(47);
				break;
			case 49 :		// daload
				noim(49);
				break;
			case 58 :		// astore
			case 56 :		// fstore
			case 54 :		// istore
				idx = readOpd8u();
				stack[vp+idx] = stack[sp--];
				break;
			case 55 :		// lstore
				idx = readOpd8u();
				stack[vp+idx+1] = stack[sp--];
				stack[vp+idx] = stack[sp--];
				break;
			case 57 :		// dstore
				idx = readOpd8u();
				stack[vp+idx+1] = stack[sp--];
				stack[vp+idx] = stack[sp--];
				break;
			case 75 :		// astore_0
			case 67 :		// fstore_0
			case 59 :		// istore_0
				stack[vp] = stack[sp--];
				break;
			case 76 :		// astore_1
			case 68 :		// fstore_1
			case 60 :		// istore_1
				stack[vp+1] = stack[sp--];
				break;
			case 77 :		// astore_2
			case 69 :		// fstore_2
			case 61 :		// istore_2
				stack[vp+2] = stack[sp--];
				break;
			case 78 :		// astore_3
			case 70 :		// fstore_3
			case 62 :		// istore_3
				stack[vp+3] = stack[sp--];
				break;
			case 63 :		// lstore_0
				stack[vp+1] = stack[sp--];
				stack[vp] = stack[sp--];
				break;
			case 64 :		// lstore_1
				stack[vp+2] = stack[sp--];
				stack[vp+1] = stack[sp--];
				break;
			case 65 :		// lstore_2
				stack[vp+3] = stack[sp--];
				stack[vp+2] = stack[sp--];
				break;
			case 66 :		// lstore_3
				stack[vp+4] = stack[sp--];
				stack[vp+3] = stack[sp--];
				break;
			case 71 :		// dstore_0
				noim(71);
				break;
			case 72 :		// dstore_1
				noim(72);
				break;
			case 73 :		// dstore_2
				noim(73);
				break;
			case 74 :		// dstore_3
				noim(74);
				break;
			case 83 :		// aastore
			case 84 :		// bastore
			case 85 :		// castore
			case 81 :		// fastore
			case 79 :		// iastore
			case 86 :		// sastore
				val = stack[sp--];	// value
				ref = stack[sp--];	// index
				ref += stack[sp--];	// ref
				writeMem(ref, val);
				break;
			case 80 :		// lastore
				noim(80);
				break;
			case 82 :		// dastore
				noim(82);
				break;
			case 87 :		// pop
				sp--;
				break;
			case 88 :		// pop2
				sp--;
				sp--;
				break;
			case 89 :		// dup
				val = stack[sp];
				stack[++sp] = val;
				break;
			case 90 :		// dup_x1
				a = stack[sp--];
				b = stack[sp--];
				stack[++sp] = a;
				stack[++sp] = b;
				stack[++sp] = a;
				break;
			case 91 :		// dup_x2
				noim(91);
				break;
			case 92 :		// dup2
				a = stack[sp--];
				b = stack[sp--];
				stack[++sp] = b;
				stack[++sp] = a;
				stack[++sp] = b;
				stack[++sp] = a;
				break;
			case 93 :		// dup2_x1
				noim(93);
				break;
			case 94 :		// dup2_x2
				noim(94);
				break;
			case 95 :		// swap
				noim(95);
				break;
			case 96 :		// iadd
				val = stack[sp-1] + stack[sp];
				stack[--sp] = val;
				break;
			case 97 :		// ladd
				noim(97);
				break;
			case 98 :		// fadd
				noim(98);
				break;
			case 99 :		// dadd
				noim(99);
				break;
			case 100 :		// isub
				val = stack[sp-1] - stack[sp];
				stack[--sp] = val;
				break;
			case 101 :		// lsub
				noim(101);
				break;
			case 102 :		// fsub
				noim(102);
				break;
			case 103 :		// dsub
				noim(103);
				break;
			case 104 :		// imul

⌨️ 快捷键说明

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