/*
 *  Copyright (C) 2002-2019  The DOSBox Team
 *
 *  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.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

enum {
	L_N=0,
	L_SKIP,
	/* Grouped ones using MOD/RM */
	L_MODRM,L_MODRM_NVM,L_POPwRM,L_POPdRM,
	
	L_Ib,L_Iw,L_Id,
	L_Ibx,L_Iwx,L_Idx,				//Sign extend
	L_Ifw,L_Ifd,
	L_OP,

	L_REGb,L_REGw,L_REGd,
	L_REGbIb,L_REGwIw,L_REGdId,
	L_POPw,L_POPd,
	L_POPfw,L_POPfd,
	L_SEG,

	L_INTO,

	L_VAL,
	L_PRESEG,
	L_DOUBLE,
	L_PREOP,L_PREADD,L_PREREP,L_PREREPNE,
	L_STRING,

/* Direct ones */
	D_IRETw,D_IRETd,
	D_PUSHAw,D_PUSHAd,
	D_POPAw,D_POPAd,
	D_POPSEGw,D_POPSEGd,
	D_DAA,D_DAS,
	D_AAA,D_AAS,
	D_CBW,D_CWDE,
	D_CWD,D_CDQ,
	D_SETALC,
	D_XLAT,
	D_CLI,D_STI,D_STC,D_CLC,D_CMC,D_CLD,D_STD,
	D_NOP,D_WAIT,
	D_ENTERw,D_ENTERd,
	D_LEAVEw,D_LEAVEd,
	
	D_RETFw,D_RETFd,
	D_RETFwIw,D_RETFdIw,
	D_POPF,D_PUSHF,
	D_SAHF,D_LAHF,
	D_CPUID,
	D_HLT,D_CLTS,
	D_LOCK,D_ICEBP,
	D_RDTSC,
	L_ERROR
};


enum {
	O_N=t_LASTFLAG,
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Pw,
	S_AIPd,S_C_AIPd,

	S_IP,S_IPIw
};

enum {
	R_OUTSB,R_OUTSW,R_OUTSD,
	R_INSB,R_INSW,R_INSD,
	R_MOVSB,R_MOVSW,R_MOVSD,
	R_LODSB,R_LODSW,R_LODSD,
	R_STOSB,R_STOSW,R_STOSD,
	R_SCASB,R_SCASW,R_SCASD,
	R_CMPSB,R_CMPSW,R_CMPSD
};

enum {
	M_None=0,
	M_Ebx,M_Eb,M_Gb,M_EbGb,M_GbEb,
	M_Ewx,M_Ew,M_Gw,M_EwGw,M_GwEw,M_EwxGwx,M_EwGwt,
	M_Edx,M_Ed,M_Gd,M_EdGd,M_GdEd,M_EdxGdx,M_EdGdt,
	
	M_EbIb,M_EwIb,M_EdIb,
	M_EwIw,M_EwIbx,M_EwxIbx,M_EwxIwx,M_EwGwIb,M_EwGwCL,
	M_EdId,M_EdIbx,M_EdGdIb,M_EdGdCL,
	
	M_Efw,M_Efd,
	
	M_Ib,M_Iw,M_Id,


	M_SEG,M_EA,
	M_GRP,
	//Special shift groups
	M_SHIFT_1, M_SHIFT_Ib,M_SHIFT_CL,

	M_POPw,M_POPd
};

struct OpCode {
	Bit8u load,op,save,extra;
};

struct FullData {
	Bitu entry;
	Bitu rm;
	EAPoint rm_eaa;
	Bitu rm_off;
	Bitu rm_eai;
	Bitu rm_index;
	Bitu rm_mod;
	OpCode code;
	EAPoint cseip;
#ifdef WORDS_BIGENDIAN
	union {
		Bit32u dword[1];
		Bit32s dwords[1];
		Bit16u word[2];
		Bit16s words[2];
		Bit8u byte[4];
		Bit8s bytes[4];
		} blah1,blah2,blah_imm;
#else
	union {	
		Bit8u b;Bit8s bs;
		Bit16u w;Bit16s ws;
		Bit32u d;Bit32s ds;
	} op1,op2,imm;
#endif
	Bitu new_flags;
	struct {
		EAPoint base;
	} seg;
	Bitu cond;
	bool repz;
	Bitu prefix;
};

/* Some defines to get the names correct. */
#ifdef WORDS_BIGENDIAN

#define inst_op1_b  inst.blah1.byte[3]
#define inst_op1_bs inst.blah1.bytes[3]
#define inst_op1_w  inst.blah1.word[1]
#define inst_op1_ws inst.blah1.words[1]
#define inst_op1_d  inst.blah1.dword[0]
#define inst_op1_ds inst.blah1.dwords[0]

#define inst_op2_b  inst.blah2.byte[3]
#define inst_op2_bs inst.blah2.bytes[3]
#define inst_op2_w  inst.blah2.word[1]
#define inst_op2_ws inst.blah2.words[1]
#define inst_op2_d  inst.blah2.dword[0]
#define inst_op2_ds inst.blah2.dwords[0]

#define inst_imm_b  inst.blah_imm.byte[3]
#define inst_imm_bs inst.blah_imm.bytes[3]
#define inst_imm_w  inst.blah_imm.word[1]
#define inst_imm_ws inst.blah_imm.words[1]
#define inst_imm_d  inst.blah_imm.dword[0]
#define inst_imm_ds inst.blah_imm.dwords[0]

#else

#define inst_op1_b  inst.op1.b
#define inst_op1_bs inst.op1.bs
#define inst_op1_w  inst.op1.w
#define inst_op1_ws inst.op1.ws
#define inst_op1_d  inst.op1.d
#define inst_op1_ds inst.op1.ds

#define inst_op2_b  inst.op2.b
#define inst_op2_bs inst.op2.bs
#define inst_op2_w  inst.op2.w
#define inst_op2_ws inst.op2.ws
#define inst_op2_d  inst.op2.d
#define inst_op2_ds inst.op2.ds

#define inst_imm_b  inst.imm.b
#define inst_imm_bs inst.imm.bs
#define inst_imm_w  inst.imm.w
#define inst_imm_ws inst.imm.ws
#define inst_imm_d  inst.imm.d
#define inst_imm_ds inst.imm.ds

#endif


#define PREFIX_NONE		0x0
#define PREFIX_ADDR		0x1
#define PREFIX_SEG		0x2
#define PREFIX_REP		0x4

