// idbg - IJVM debugger.
// Copyright 2000 Tom Rothamel <tom-idbg@onegeek.org>
//
// 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.  

#ifndef IJVM_H
#define IJVM_H

#include <stdio.h>

class Memory {
	unsigned char **mem;

	unsigned char *find(unsigned int);
	
public:
	char *load(FILE *);
	
	unsigned char getbyte(unsigned int);
	void setbyte(unsigned int, unsigned char);

	int getword(unsigned int);
	void setword(unsigned int, int);

	Memory();
};

class VM;

struct Opcode {
	char *name;

	int label;
	int variable;
	int constant;
	int method;
	int byte;

	int (*func)(VM *);
	
	Opcode(char *, int, int, int, int, int, int (*)(VM *));
};

struct Regs {
	unsigned int pc;
	unsigned int sp;
	unsigned int lv;
	unsigned int cpp;
	unsigned int base;
};

struct Parse {
	int wide;

	int opcode;
	int len;
		
	short label;
	unsigned short variable;
	unsigned short constant;
	unsigned short method;
	char byte;
};

class VM {
 public:
	Regs *regs;
	Memory *m;
	Opcode *ops[0x100];
	Parse *p;

	char *err;
	int depth;
	
	int decode(unsigned int);
	int execute();

	int getconst(unsigned int);
	
	int getvar(unsigned int);
	void setvar(unsigned int, unsigned int);
	
	void push(int);
	int pop();
	
	void load(FILE *f);
	
	VM();
	~VM();
};

	

#endif

