//
//  EXPStackMachine.h
//  StackMachine
//
//  Created by Ashley on 23/04/2007.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "EXPAssemblerConstants.h"
#import "history_protocol.h"

#define M 2
#define N 4
#define MM 1
#define NN 2

#define MAXFILES 16

/*typedef union
{
	unsigned int i;
	float x;
} intfloat; */
struct intfloat_s
{
	unsigned int i;
	float x;
};
typedef struct intfloat_s intfloat;

@interface EXPVirtualMachine : NSObject {
	unsigned short int *memory;
	unsigned int memorySize;
	
	int nConstants;
	int nAuxiliaries;
	int nVariables;
	
	int nTables;
	
	NSMutableArray *_filenames;
	NSMutableArray *_filetypes;
	int nextFile;
	FILE **files;
	
	int exitCode;
	
	BOOL echo;
//	'DMA' pointer
	int ptr;

//	Registers.
	unsigned int pc;								// Programme counter
	unsigned int sp;								// Data stack ointer
	unsigned int lp;								// Local variable base pointer
	unsigned int gp;								// Global variable base pointer
	unsigned int rp;								// Return stack pointer
//	unsigned int index[NUMINDEXREGS];				// Index registers
	intfloat acc;										// Acumulator

	unsigned int lastpc;

//	double _startTime;
//	double _stopTime;
//	double _time;
	NSMutableArray *trapCalls;

	int _nHistory;
	id <history, NSObject> _history;

	int _outputCounter;
}

- (id) initWithMemory:(unsigned int)newMemorySize; //store:(unsigned int)newStoreSize

- (unsigned short int *) memory;

- (void) setNTables:(int) newNTables;
- (int) nTables;
- (void) setNHistory:(int)nHistory;

- (void) setHistory:(id <history, NSObject>)history;
- (id) history;

- (BOOL) echo;
- (void) setEcho:(BOOL)newEcho;

- (void) putOpcode:(unsigned short int)opcode mode:(unsigned short int)mode;
- (void) putUInt:(unsigned int)val;
- (void) putUShInt:(unsigned short int)val;
- (void) putUInt:(unsigned int)val at:(unsigned int)location;
- (void) putDouble:(double)val;
- (void) putString:(NSString *)string withNewLine:(BOOL)newLine;

- (unsigned int) memorySize;

- (int) registerTrapCall:(SEL)sel forObject:(id) object;

- (void) runFromPc;

- (int) exitCode;

- (int) nFiles;
- (int) attachFile:(NSString *)filename type:(NSString *)filetype;
- (BOOL) openFiles;
- (void) closeFiles;

- (unsigned int)ptr;
- (void) setPtr:(unsigned int)newPtr;

- (unsigned int)pc;
- (void) setPc:(unsigned int)newPc;
- (unsigned int)sp;
- (void) setSp:(unsigned int)newSp;
- (unsigned int)gp;
- (void) setGp:(unsigned int)newGp;
- (void) setRp:(unsigned int)newRp;
- (unsigned int)rp;
- (double)acc_fp;
- (void) setAccFP:(double)newAcc;
- (int) accInt;
- (void) setAccInt:(int)newAcc;

- (void) dumpRegisters;
- (void) dump:(unsigned int)start to:(unsigned int)stop;
- (void) dumpDoubles:(unsigned int)start to:(unsigned int)stop;

//- (BOOL) writeToFile:(NSString *)

@end