{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_timer_r called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_timer_r called with no CPU open\n"));
#endif

	/* only returns countdown */
	return ((h6280.timer_value>>10)&0x7F)|(h6280.io_buffer&0x80);
}

/*****************************************************************************

    h6280.c - Portable HuC6280 emulator

    This source code is based (with permission!) on the 6502 emulator by
    Juergen Buchmueller.  It is released as part of the Mame emulator project.
    Let me know if you intend to use this code in any other project.

    NOTICE:

    This code is around 99% complete!  Several things are unimplemented,
    some due to lack of time, some due to lack of documentation, mainly
    due to lack of programs using these features.

    csh, csl opcodes are not supported.

    I am unsure if flag B is set upon execution of rti.

    Cycle counts should be quite accurate.

    Changelog, version 1.02:
        JMP + indirect X (0x7c) opcode fixed.
        SMB + RMB opcodes fixed in disassembler.
        change_pc function calls removed.
        TSB & TRB now set flags properly.
        BIT opcode altered.

    Changelog, version 1.03:
        Swapped IRQ mask for IRQ1 & IRQ2 (thanks Yasuhiro)

    Changelog, version 1.04, 28/9/99-22/10/99:
        Adjusted RTI (thanks Karl)
        TST opcodes fixed in disassembler (missing break statements in a case!).
        TST behaviour fixed.
        SMB/RMB/BBS/BBR fixed in disassembler.

    Changelog, version 1.05, 8/12/99-16/12/99:
        Added CAB's timer implementation (note: irq ack & timer reload are changed).
        Fixed STA IDX.
        Fixed B flag setting on BRK.
        Assumed CSH & CSL to take 2 cycles each.

        Todo:  Performance could be improved by precalculating timer fire position.

    Changelog, version 1.06, 4/5/00 - last opcode bug found?
        JMP indirect was doing a EAL++; instead of EAD++; - Obviously causing
        a corrupt read when L = 0xff!  This fixes Bloody Wolf and Trio The Punch!

    Changelog, version 1.07, 3/9/00:
        Changed timer to be single shot - fixes Crude Buster music in level 1.

    Changelog, version 1.08, 8/11/05: (Charles MacDonald)

        Changed timer implementation, no longer single shot and reading the
        timer registers returns the count only. Fixes the following:
        - Mesopotamia: Music tempo & in-game timer
        - Dragon Saber: DDA effects
        - Magical Chase: Music tempo and speed regulation
        - Cadash: Allows the first level to start
        - Turrican: Allows the game to start

        Changed PLX and PLY to set NZ flags. Fixes:
        - Afterburner: Graphics unpacking
        - Aoi Blink: Collision detection with background

        Fixed the decimal version of ADC/SBC to *not* update the V flag,
        only the binary ones do.

        Fixed B flag handling so it is always set outside of an interrupt;
        even after being set by PLP and RTI.

        Fixed P state after reset to set I and B, leaving T, D cleared and
        NVZC randomized (cleared in this case).

        Fixed interrupt processing order (Timer has highest priority followed
        by IRQ1 and finally IRQ2).

    Changelog, version 1.09, 1/07/06: (Rob Bohms)

        Added emulation of the T flag, fixes PCE Ankuku Densetsu title screen

    Changelog, version 1.10, 5/09/07: (Wilbert Pol)

        - Taking of interrupts is delayed to respect a pending instruction already
          in the instruction pipeline; fixes After Burner.
        - Added 1 cycle for decimal mode ADC and SBC instructions.
        - Changed cycle counts for CSH and CSL instructions to 3.
        - Added T flag support to the SBC instruction.
        - Fixed ADC T flag to set the Z flag based on the value read.
        - Added 3 cycle penalty to ADC, AND, EOR, ORA, and SBC instructions
          when the T flag is set.
        - Fixed cycle count and support for 65536 byte blocks for the TAI, TDD,
          TIA, TII, and TIN instructions.
        - Fixed RDWORD macro in the disassembler.
        - Fixed setting of N and V flags in the TST instructions.
        - Removed unneeded debug_mmr code.
        - Fixed TSB and TRB instructions.
        - Added 1 delay when accessing the VDC or VCE areas.
        - Implemented low and high speed cpu modes.

    Changelog, version 1.11, 18/09/07: (Wilbert Pol)

        - Improvements to the handling of taking of delayed interrupts.

******************************************************************************/
#include "burnint.h"
#include "h6280.h"
#include "h6280_intf.h"

#define offs_t	unsigned int
#define change_pc(x) PCW = (x)

#define CLEAR_LINE	0
#define ASSERT_LINE	1
#define INPUT_LINE_NMI	0x20

#define H6280_INLINE static

extern INT32 nh6280CpuActive;

static int 	h6280_ICount = 0;

static h6280_Regs  h6280;

void h6280_set_irq_line(INT32 irqline, INT32 state);
#define set_irq_line h6280_set_irq_line

/* include the macros */
#include "h6280ops.h"

/* include the opcode macros, functions and function pointer tables */
#include "tblh6280.c"

#if 0
/*****************************************************************************/
static void h6280_init(int index, int clock, const void *config, int (*irqcallback)(int))
{
	state_save_register_item("h6280", index, h6280.ppc.w.l);
	state_save_register_item("h6280", index, h6280.pc.w.l);
	state_save_register_item("h6280", index, h6280.sp.w.l);
	state_save_register_item("h6280", index, h6280.zp.w.l);
	state_save_register_item("h6280", index, h6280.ea.w.l);
	state_save_register_item("h6280", index, h6280.a);
	state_save_register_item("h6280", index, h6280.x);
	state_save_register_item("h6280", index, h6280.y);
	state_save_register_item("h6280", index, h6280.p);
	state_save_register_item_array("h6280", index, h6280.mmr);
	state_save_register_item("h6280", index, h6280.irq_mask);
	state_save_register_item("h6280", index, h6280.timer_status);
	state_save_register_item("h6280", index, h6280.timer_ack);
	state_save_register_item("h6280", index, h6280.clocks_per_cycle);
	state_save_register_item("h6280", index, h6280.timer_value);
	state_save_register_item("h6280", index, h6280.timer_load);
	state_save_register_item("h6280", index, h6280.nmi_state);
	state_save_register_item("h6280", index, h6280.irq_state[0]);
	state_save_register_item("h6280", index, h6280.irq_state[1]);
	state_save_register_item("h6280", index, h6280.irq_state[2]);
	state_save_register_item("h6280", index, h6280.irq_pending);

	#if LAZY_FLAGS
	state_save_register_item("h6280", index, h6280.NZ);
	#endif
	state_save_register_item("h6280", index, h6280.io_buffer);

	h6280.irq_callback = irqcallback;
}
#endif

void h6280_irqcallback(int (*irqcallback)(int))
{
	h6280.irq_callback = irqcallback;
}

void h6280Reset(void)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280Reset called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280Reset called with no CPU open\n"));
#endif

	/* wipe out the h6280 structure */
	int (*save_irqcallback)(int) = h6280.irq_callback;
	memset(&h6280, 0, sizeof(h6280_Regs));
	h6280.irq_callback = save_irqcallback;

	/* set I and B flags */
	P = _fI | _fB;

    /* stack starts at 0x01ff */
	h6280.sp.d = 0x1ff;

    /* read the reset vector into PC */
	PCL = RDMEM(H6280_RESET_VEC);
	PCH = RDMEM((H6280_RESET_VEC+1));
	CHANGE_PC;

	/* CPU starts in low speed mode */
    h6280.clocks_per_cycle = 4;

	/* timer off by default */
	h6280.timer_status=0;
	h6280.timer_load = 128 * 1024;

    /* clear pending interrupts */
	for (INT32 i = 0; i < 3; i++)
		h6280.irq_state[i] = CLEAR_LINE;
	h6280.nmi_state = CLEAR_LINE;

	h6280.irq_pending = 0;

	h6280.h6280_totalcycles = 0;
}

#if 0
static void h6280_exit(void)
{
	/* nothing */
}
#endif

static int end_run = 0;

int h6280Run(int cycles)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280Run called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280Run called with no CPU open\n"));
#endif

	int in;
	h6280_ICount = cycles;
	h6280.h6280_iCycles = cycles;

	end_run = 0;

	if ( h6280.irq_pending == 2 ) {
		h6280.irq_pending--;
	}

	/* Execute instructions */
	do
    {
    	if ((h6280.ppc.w.l ^ h6280.pc.w.l) & 0xe000)
    		CHANGE_PC;
		h6280.ppc = h6280.pc;

		/* Execute 1 instruction */
		in=RDOP();
		PCW++;
		insnh6280[in]();

		if ( h6280.irq_pending ) {
			if ( h6280.irq_pending == 1 ) {
				if ( !(P & _fI) ) {
					h6280.irq_pending--;
					CHECK_AND_TAKE_IRQ_LINES;
				}
			} else {
				h6280.irq_pending--;
			}
		}

		/* Check internal timer */
		if(h6280.timer_status)
		{
			if(h6280.timer_value<=0)
			{
				if ( ! h6280.irq_pending )
					h6280.irq_pending = 1;
				while( h6280.timer_value <= 0 )
					h6280.timer_value += h6280.timer_load;
				set_irq_line(2,ASSERT_LINE);
			}
		}
	} while (h6280_ICount > 0 && !end_run);

	cycles = cycles - h6280_ICount;

	h6280.h6280_totalcycles += cycles;
	h6280_ICount = 0;
	h6280.h6280_iCycles = 0;

	return cycles;
}

void h6280_get_context(void *dst)
{
	if( dst )
		*(h6280_Regs*)dst = h6280;
}

void h6280_set_context(void *src)
{
	if( src )
		h6280 = *(h6280_Regs*)src;
	CHANGE_PC;
}

int h6280TotalCycles()
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280TotalCycles called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280TotalCycles called with no CPU open\n"));
#endif

	return h6280.h6280_totalcycles + (h6280.h6280_iCycles - h6280_ICount);
}

void h6280RunEnd()
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280RunEnd called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280RunEnd called with no CPU open\n"));
#endif

	end_run = 1;
}

INT32 h6280Idle(INT32 cycles)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280Idle called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280Idle called with no CPU open\n"));
#endif

	h6280.h6280_totalcycles += cycles;

	return cycles;
}

/*****************************************************************************/

void h6280_set_irq_line(int irqline, int state)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_set_irq_line called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_set_irq_line called with no CPU open\n"));
#endif

	if (irqline == INPUT_LINE_NMI)
	{
		if ( state != ASSERT_LINE ) return;
		h6280.nmi_state = state;
		CHECK_IRQ_LINES;
	}
	else if (irqline < 3)
	{
		if (state == CPU_IRQSTATUS_HOLD) {
			state = CPU_IRQSTATUS_ACK;
			h6280.irq_hold = 1;
		}
		/* If the state has not changed, just return */
		if ( h6280.irq_state[irqline] == state )
			return;

	    h6280.irq_state[irqline] = state;

		CHECK_IRQ_LINES;
	}
}

/*****************************************************************************/

unsigned char h6280_irq_status_r(unsigned int offset)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_irq_status_r called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_irq_status_r called with no CPU open\n"));
#endif

	int status;

	switch (offset&3)
	{
	default:return h6280.io_buffer;break;
	case 3:
		{
			status=0;
			if(h6280.irq_state[1]!=CLEAR_LINE) status|=1; /* IRQ 2 */
			if(h6280.irq_state[0]!=CLEAR_LINE) status|=2; /* IRQ 1 */
			if(h6280.irq_state[2]!=CLEAR_LINE) status|=4; /* TIMER */
			return status|(h6280.io_buffer&(~H6280_IRQ_MASK));
		}
	case 2: return h6280.irq_mask|(h6280.io_buffer&(~H6280_IRQ_MASK));break;
	}
}

void h6280_irq_status_w(unsigned int offset, unsigned char data)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_irq_status_w called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_irq_status_w called with no CPU open\n"));
#endif

	h6280.io_buffer=data;
	switch (offset&3)
	{
		default:h6280.io_buffer=data;break;
		case 2: /* Write irq mask */
			h6280.irq_mask=data&0x7;
			CHECK_IRQ_LINES;
			break;

		case 3: /* Timer irq ack */
			set_irq_line(2,CLEAR_LINE);
			break;
	}
}

unsigned char h6280_timer_r(unsigned int)

void h6280_timer_w(unsigned int offset, unsigned char data)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_timer_w called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_timer_w called with no CPU open\n"));
#endif

	h6280.io_buffer=data;
	switch (offset&1) {
		case 0: /* Counter preload */
			h6280.timer_load=((data&127)+1)*1024;
			return;

		case 1: /* Counter enable */
			if(data&1)
			{	/* stop -> start causes reload */
				if(h6280.timer_status==0) h6280.timer_value=h6280.timer_load;
			}
			h6280.timer_status=data&1;
			return;
	}
}

#if 0
static int h6280_translate(int space, int intention, offs_t *addr)
{
#if 0
	if (space == ADDRESS_SPACE_PROGRAM)
		*addr = TRANSLATED(*addr);
	return TRUE;
#else
	if (space == 0)
		*addr = TRANSLATED(*addr);
	return 1;
#endif
}
#endif

UINT8 h6280io_get_buffer()
{
	return h6280.io_buffer;
}
void h6280io_set_buffer(UINT8 data)
{
	h6280.io_buffer=data;
}

#if 0
/*****************************************************************************/

/**************************************************************************
 * Generic set_info
 **************************************************************************/

static void h6280_set_info(UINT32 state, cpuinfo *info)
{
	switch (state)
	{
		/* --- the following bits of info are set as 64-bit signed integers --- */
		case CPUINFO_INT_INPUT_STATE + 0:			set_irq_line(0, info->i);					break;
		case CPUINFO_INT_INPUT_STATE + 1:			set_irq_line(1, info->i);					break;
		case CPUINFO_INT_INPUT_STATE + 2:			set_irq_line(2, info->i);					break;
		case CPUINFO_INT_INPUT_STATE + INPUT_LINE_NMI:set_irq_line(INPUT_LINE_NMI, info->i);	break;

		case CPUINFO_INT_PC:
		case CPUINFO_INT_REGISTER + H6280_PC:		PCW = info->i;								break;
		case CPUINFO_INT_SP:
		case CPUINFO_INT_REGISTER + H6280_S:		S = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_P:		P = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_A:		A = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_X:		X = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_Y:		Y = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_IRQ_MASK: h6280.irq_mask = info->i; CHECK_IRQ_LINES;	break;
		case CPUINFO_INT_REGISTER + H6280_TIMER_STATE: h6280.timer_status = info->i; 			break;
		case CPUINFO_INT_REGISTER + H6280_NMI_STATE: set_irq_line( INPUT_LINE_NMI, info->i ); 	break;
		case CPUINFO_INT_REGISTER + H6280_IRQ1_STATE: set_irq_line( 0, info->i ); 				break;
		case CPUINFO_INT_REGISTER + H6280_IRQ2_STATE: set_irq_line( 1, info->i ); 				break;
		case CPUINFO_INT_REGISTER + H6280_IRQT_STATE: set_irq_line( 2, info->i ); 				break;
		case CPUINFO_INT_REGISTER + H6280_M1:		h6280.mmr[0] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M2:		h6280.mmr[1] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M3:		h6280.mmr[2] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M4:		h6280.mmr[3] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M5:		h6280.mmr[4] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M6:		h6280.mmr[5] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M7:		h6280.mmr[6] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M8:		h6280.mmr[7] = info->i;						break;
	}
}

/**************************************************************************
 * Generic get_info
 **************************************************************************/

void h6280_get_info(UINT32 state, cpuinfo *info)
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case CPUINFO_INT_CONTEXT_SIZE:					info->i = sizeof(h6280);				break;
		case CPUINFO_INT_INPUT_LINES:					info->i = 3;							break;
		case CPUINFO_INT_DEFAULT_IRQ_VECTOR:			info->i = 0;							break;
		case CPUINFO_INT_ENDIANNESS:					info->i = CPU_IS_LE;					break;
		case CPUINFO_INT_CLOCK_MULTIPLIER:				info->i = 1;							break;
		case CPUINFO_INT_CLOCK_DIVIDER:					info->i = 1;							break;
		case CPUINFO_INT_MIN_INSTRUCTION_BYTES:			info->i = 1;							break;
		case CPUINFO_INT_MAX_INSTRUCTION_BYTES:			info->i = 7;							break;
		case CPUINFO_INT_MIN_CYCLES:					info->i = 2;							break;
		case CPUINFO_INT_MAX_CYCLES:					info->i = 17 + 6*65536;					break;

		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_PROGRAM:	info->i = 8;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 21;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_PROGRAM: info->i = 0;					break;
		case CPUINFO_INT_LOGADDR_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 16;					break;
		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_DATA:	info->i = 0;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_DATA: 	info->i = 0;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_DATA: 	info->i = 0;					break;
		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_IO:		info->i = 8;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_IO: 		info->i = 2;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_IO: 		info->i = 0;					break;

		case CPUINFO_INT_INPUT_STATE + 0:				info->i = h6280.irq_state[0];			break;
		case CPUINFO_INT_INPUT_STATE + 1:				info->i = h6280.irq_state[1];			break;
		case CPUINFO_INT_INPUT_STATE + 2:				info->i = h6280.irq_state[2];			break;
		case CPUINFO_INT_INPUT_STATE + INPUT_LINE_NMI:	info->i = h6280.nmi_state; 				break;

		case CPUINFO_INT_PREVIOUSPC:					info->i = h6280.ppc.d;					break;

		case CPUINFO_INT_PC:
		case CPUINFO_INT_REGISTER + H6280_PC:			info->i = PCD;							break;
		case CPUINFO_INT_SP:
		case CPUINFO_INT_REGISTER + H6280_S:			info->i = S;							break;
		case CPUINFO_INT_REGISTER + H6280_P:			info->i = P;							break;
		case CPUINFO_INT_REGISTER + H6280_A:			info->i = A;							break;
		case CPUINFO_INT_REGISTER + H6280_X:			info->i = X;							break;
		case CPUINFO_INT_REGISTER + H6280_Y:			info->i = Y;							break;
		case CPUINFO_INT_REGISTER + H6280_IRQ_MASK:		info->i = h6280.irq_mask;				break;
		case CPUINFO_INT_REGISTER + H6280_TIMER_STATE:	info->i = h6280.timer_status;			break;
		case CPUINFO_INT_REGISTER + H6280_NMI_STATE:	info->i = h6280.nmi_state;				break;
		case CPUINFO_INT_REGISTER + H6280_IRQ1_STATE:	info->i = h6280.irq_state[0];			break;
		case CPUINFO_INT_REGISTER + H6280_IRQ2_STATE:	info->i = h6280.irq_state[1];			break;
		case CPUINFO_INT_REGISTER + H6280_IRQT_STATE:	info->i = h6280.irq_state[2];			break;
		case CPUINFO_INT_REGISTER + H6280_M1:			info->i = h6280.mmr[0];					break;
		case CPUINFO_INT_REGISTER + H6280_M2:			info->i = h6280.mmr[1];					break;
		case CPUINFO_INT_REGISTER + H6280_M3:			info->i = h6280.mmr[2];					break;
		case CPUINFO_INT_REGISTER + H6280_M4:			info->i = h6280.mmr[3];					break;
		case CPUINFO_INT_REGISTER + H6280_M5:			info->i = h6280.mmr[4];					break;
		case CPUINFO_INT_REGISTER + H6280_M6:			info->i = h6280.mmr[5];					break;
		case CPUINFO_INT_REGISTER + H6280_M7:			info->i = h6280.mmr[6];					break;
		case CPUINFO_INT_REGISTER + H6280_M8:			info->i = h6280.mmr[7];					break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case CPUINFO_PTR_SET_INFO:						info->setinfo = h6280_set_info;			break;
		case CPUINFO_PTR_GET_CONTEXT:					info->getcontext = h6280_get_context;	break;
		case CPUINFO_PTR_SET_CONTEXT:					info->setcontext = h6280_set_context;	break;
		case CPUINFO_PTR_INIT:							info->init = h6280_init;				break;
		case CPUINFO_PTR_RESET:							info->reset = h6280_reset;				break;
		case CPUINFO_PTR_EXIT:							info->exit = h6280_exit;				break;
		case CPUINFO_PTR_EXECUTE:						info->execute = h6280_execute;			break;
		case CPUINFO_PTR_BURN:							info->burn = NULL;						break;
		case CPUINFO_PTR_DISASSEMBLE:					info->disassemble = h6280_dasm;			break;
		case CPUINFO_PTR_INSTRUCTION_COUNTER:			info->icount = &h6280_ICount;			break;
		case CPUINFO_PTR_TRANSLATE:						info->translate = h6280_translate;		break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case CPUINFO_STR_NAME:							strcpy(info->s, "HuC6280");				break;
		case CPUINFO_STR_CORE_FAMILY:					strcpy(info->s, "Hudsonsoft 6280");		break;
		case CPUINFO_STR_CORE_VERSION:					strcpy(info->s, "1.11");				break;
		case CPUINFO_STR_CORE_FILE:						strcpy(info->s, __FILE__);				break;
		case CPUINFO_STR_CORE_CREDITS:					strcpy(info->s, "Copyright Bryan McPhail, mish@tendril.co.uk"); break;

		case CPUINFO_STR_FLAGS:
			sprintf(info->s, "%c%c%c%c%c%c%c%c",
				h6280.p & 0x80 ? 'N':'.',
				h6280.p & 0x40 ? 'V':'.',
				h6280.p & 0x20 ? 'R':'.',
				h6280.p & 0x10 ? 'B':'.',
				h6280.p & 0x08 ? 'D':'.',
				h6280.p & 0x04 ? 'I':'.',
				h6280.p & 0x02 ? 'Z':'.',
				h6280.p & 0x01 ? 'C':'.');
			break;

		case CPUINFO_STR_REGISTER + H6280_PC:			sprintf(info->s, "PC:%04X", h6280.pc.d); break;
        case CPUINFO_STR_REGISTER + H6280_S:			sprintf(info->s, "S:%02X", h6280.sp.b.l); break;
        case CPUINFO_STR_REGISTER + H6280_P:			sprintf(info->s, "P:%02X", h6280.p); break;
        case CPUINFO_STR_REGISTER + H6280_A:			sprintf(info->s, "A:%02X", h6280.a); break;
		case CPUINFO_STR_REGISTER + H6280_X:			sprintf(info->s, "X:%02X", h6280.x); break;
		case CPUINFO_STR_REGISTER + H6280_Y:			sprintf(info->s, "Y:%02X", h6280.y); break;
		case CPUINFO_STR_REGISTER + H6280_IRQ_MASK:		sprintf(info->s, "IM:%02X", h6280.irq_mask); break;
		case CPUINFO_STR_REGISTER + H6280_TIMER_STATE:	sprintf(info->s, "TMR:%02X", h6280.timer_status); break;
		case CPUINFO_STR_REGISTER + H6280_NMI_STATE:	sprintf(info->s, "NMI:%X", h6280.nmi_state); break;
		case CPUINFO_STR_REGISTER + H6280_IRQ1_STATE:	sprintf(info->s, "IRQ1:%X", h6280.irq_state[0]); break;
		case CPUINFO_STR_REGISTER + H6280_IRQ2_STATE:	sprintf(info->s, "IRQ2:%X", h6280.irq_state[1]); break;
		case CPUINFO_STR_REGISTER + H6280_IRQT_STATE:	sprintf(info->s, "IRQT:%X", h6280.irq_state[2]); break;
		case CPUINFO_STR_REGISTER + H6280_M1:			sprintf(info->s, "M1:%02X", h6280.mmr[0]); break;
		case CPUINFO_STR_REGISTER + H6280_M2:			sprintf(info->s, "M2:%02X", h6280.mmr[1]); break;
		case CPUINFO_STR_REGISTER + H6280_M3:			sprintf(info->s, "M3:%02X", h6280.mmr[2]); break;
		case CPUINFO_STR_REGISTER + H6280_M4:			sprintf(info->s, "M4:%02X", h6280.mmr[3]); break;
		case CPUINFO_STR_REGISTER + H6280_M5:			sprintf(info->s, "M5:%02X", h6280.mmr[4]); break;
		case CPUINFO_STR_REGISTER + H6280_M6:			sprintf(info->s, "M6:%02X", h6280.mmr[5]); break;
		case CPUINFO_STR_REGISTER + H6280_M7:			sprintf(info->s, "M7:%02X", h6280.mmr[6]); break;
		case CPUINFO_STR_REGISTER + H6280_M8:			sprintf(info->s, "M8:%02X", h6280.mmr[7]); break;
	}
}
#endif


switch (offset&3)
	{
		default:h6280.io_buffer=data;break;
		case 2: /* Write irq mask */
			h6280.irq_mask=data&0x7;
			CHECK_IRQ_LINES;
			break;

		case 3: /* Timer irq ack */
			set_irq_line(2,CLEAR_LINE);
			break;
	}

/*****************************************************************************

    h6280.c - Portable HuC6280 emulator

    This source code is based (with permission!) on the 6502 emulator by
    Juergen Buchmueller.  It is released as part of the Mame emulator project.
    Let me know if you intend to use this code in any other project.

    NOTICE:

    This code is around 99% complete!  Several things are unimplemented,
    some due to lack of time, some due to lack of documentation, mainly
    due to lack of programs using these features.

    csh, csl opcodes are not supported.

    I am unsure if flag B is set upon execution of rti.

    Cycle counts should be quite accurate.

    Changelog, version 1.02:
        JMP + indirect X (0x7c) opcode fixed.
        SMB + RMB opcodes fixed in disassembler.
        change_pc function calls removed.
        TSB & TRB now set flags properly.
        BIT opcode altered.

    Changelog, version 1.03:
        Swapped IRQ mask for IRQ1 & IRQ2 (thanks Yasuhiro)

    Changelog, version 1.04, 28/9/99-22/10/99:
        Adjusted RTI (thanks Karl)
        TST opcodes fixed in disassembler (missing break statements in a case!).
        TST behaviour fixed.
        SMB/RMB/BBS/BBR fixed in disassembler.

    Changelog, version 1.05, 8/12/99-16/12/99:
        Added CAB's timer implementation (note: irq ack & timer reload are changed).
        Fixed STA IDX.
        Fixed B flag setting on BRK.
        Assumed CSH & CSL to take 2 cycles each.

        Todo:  Performance could be improved by precalculating timer fire position.

    Changelog, version 1.06, 4/5/00 - last opcode bug found?
        JMP indirect was doing a EAL++; instead of EAD++; - Obviously causing
        a corrupt read when L = 0xff!  This fixes Bloody Wolf and Trio The Punch!

    Changelog, version 1.07, 3/9/00:
        Changed timer to be single shot - fixes Crude Buster music in level 1.

    Changelog, version 1.08, 8/11/05: (Charles MacDonald)

        Changed timer implementation, no longer single shot and reading the
        timer registers returns the count only. Fixes the following:
        - Mesopotamia: Music tempo & in-game timer
        - Dragon Saber: DDA effects
        - Magical Chase: Music tempo and speed regulation
        - Cadash: Allows the first level to start
        - Turrican: Allows the game to start

        Changed PLX and PLY to set NZ flags. Fixes:
        - Afterburner: Graphics unpacking
        - Aoi Blink: Collision detection with background

        Fixed the decimal version of ADC/SBC to *not* update the V flag,
        only the binary ones do.

        Fixed B flag handling so it is always set outside of an interrupt;
        even after being set by PLP and RTI.

        Fixed P state after reset to set I and B, leaving T, D cleared and
        NVZC randomized (cleared in this case).

        Fixed interrupt processing order (Timer has highest priority followed
        by IRQ1 and finally IRQ2).

    Changelog, version 1.09, 1/07/06: (Rob Bohms)

        Added emulation of the T flag, fixes PCE Ankuku Densetsu title screen

    Changelog, version 1.10, 5/09/07: (Wilbert Pol)

        - Taking of interrupts is delayed to respect a pending instruction already
          in the instruction pipeline; fixes After Burner.
        - Added 1 cycle for decimal mode ADC and SBC instructions.
        - Changed cycle counts for CSH and CSL instructions to 3.
        - Added T flag support to the SBC instruction.
        - Fixed ADC T flag to set the Z flag based on the value read.
        - Added 3 cycle penalty to ADC, AND, EOR, ORA, and SBC instructions
          when the T flag is set.
        - Fixed cycle count and support for 65536 byte blocks for the TAI, TDD,
          TIA, TII, and TIN instructions.
        - Fixed RDWORD macro in the disassembler.
        - Fixed setting of N and V flags in the TST instructions.
        - Removed unneeded debug_mmr code.
        - Fixed TSB and TRB instructions.
        - Added 1 delay when accessing the VDC or VCE areas.
        - Implemented low and high speed cpu modes.

    Changelog, version 1.11, 18/09/07: (Wilbert Pol)

        - Improvements to the handling of taking of delayed interrupts.

******************************************************************************/
#include "burnint.h"
#include "h6280.h"
#include "h6280_intf.h"

#define offs_t	unsigned int
#define change_pc(x) PCW = (x)

#define CLEAR_LINE	0
#define ASSERT_LINE	1
#define INPUT_LINE_NMI	0x20

#define H6280_INLINE static

extern INT32 nh6280CpuActive;

static int 	h6280_ICount = 0;

static h6280_Regs  h6280;

void h6280_set_irq_line(INT32 irqline, INT32 state);
#define set_irq_line h6280_set_irq_line

/* include the macros */
#include "h6280ops.h"

/* include the opcode macros, functions and function pointer tables */
#include "tblh6280.c"

#if 0
/*****************************************************************************/
static void h6280_init(int index, int clock, const void *config, int (*irqcallback)(int))
{
	state_save_register_item("h6280", index, h6280.ppc.w.l);
	state_save_register_item("h6280", index, h6280.pc.w.l);
	state_save_register_item("h6280", index, h6280.sp.w.l);
	state_save_register_item("h6280", index, h6280.zp.w.l);
	state_save_register_item("h6280", index, h6280.ea.w.l);
	state_save_register_item("h6280", index, h6280.a);
	state_save_register_item("h6280", index, h6280.x);
	state_save_register_item("h6280", index, h6280.y);
	state_save_register_item("h6280", index, h6280.p);
	state_save_register_item_array("h6280", index, h6280.mmr);
	state_save_register_item("h6280", index, h6280.irq_mask);
	state_save_register_item("h6280", index, h6280.timer_status);
	state_save_register_item("h6280", index, h6280.timer_ack);
	state_save_register_item("h6280", index, h6280.clocks_per_cycle);
	state_save_register_item("h6280", index, h6280.timer_value);
	state_save_register_item("h6280", index, h6280.timer_load);
	state_save_register_item("h6280", index, h6280.nmi_state);
	state_save_register_item("h6280", index, h6280.irq_state[0]);
	state_save_register_item("h6280", index, h6280.irq_state[1]);
	state_save_register_item("h6280", index, h6280.irq_state[2]);
	state_save_register_item("h6280", index, h6280.irq_pending);

	#if LAZY_FLAGS
	state_save_register_item("h6280", index, h6280.NZ);
	#endif
	state_save_register_item("h6280", index, h6280.io_buffer);

	h6280.irq_callback = irqcallback;
}
#endif

void h6280_irqcallback(int (*irqcallback)(int))
{
	h6280.irq_callback = irqcallback;
}

void h6280Reset(void)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280Reset called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280Reset called with no CPU open\n"));
#endif

	/* wipe out the h6280 structure */
	int (*save_irqcallback)(int) = h6280.irq_callback;
	memset(&h6280, 0, sizeof(h6280_Regs));
	h6280.irq_callback = save_irqcallback;

	/* set I and B flags */
	P = _fI | _fB;

    /* stack starts at 0x01ff */
	h6280.sp.d = 0x1ff;

    /* read the reset vector into PC */
	PCL = RDMEM(H6280_RESET_VEC);
	PCH = RDMEM((H6280_RESET_VEC+1));
	CHANGE_PC;

	/* CPU starts in low speed mode */
    h6280.clocks_per_cycle = 4;

	/* timer off by default */
	h6280.timer_status=0;
	h6280.timer_load = 128 * 1024;

    /* clear pending interrupts */
	for (INT32 i = 0; i < 3; i++)
		h6280.irq_state[i] = CLEAR_LINE;
	h6280.nmi_state = CLEAR_LINE;

	h6280.irq_pending = 0;

	h6280.h6280_totalcycles = 0;
}

#if 0
static void h6280_exit(void)
{
	/* nothing */
}
#endif

static int end_run = 0;

int h6280Run(int cycles)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280Run called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280Run called with no CPU open\n"));
#endif

	int in;
	h6280_ICount = cycles;
	h6280.h6280_iCycles = cycles;

	end_run = 0;

	if ( h6280.irq_pending == 2 ) {
		h6280.irq_pending--;
	}

	/* Execute instructions */
	do
    {
    	if ((h6280.ppc.w.l ^ h6280.pc.w.l) & 0xe000)
    		CHANGE_PC;
		h6280.ppc = h6280.pc;

		/* Execute 1 instruction */
		in=RDOP();
		PCW++;
		insnh6280[in]();

		if ( h6280.irq_pending ) {
			if ( h6280.irq_pending == 1 ) {
				if ( !(P & _fI) ) {
					h6280.irq_pending--;
					CHECK_AND_TAKE_IRQ_LINES;
				}
			} else {
				h6280.irq_pending--;
			}
		}

		/* Check internal timer */
		if(h6280.timer_status)
		{
			if(h6280.timer_value<=0)
			{
				if ( ! h6280.irq_pending )
					h6280.irq_pending = 1;
				while( h6280.timer_value <= 0 )
					h6280.timer_value += h6280.timer_load;
				set_irq_line(2,ASSERT_LINE);
			}
		}
	} while (h6280_ICount > 0 && !end_run);

	cycles = cycles - h6280_ICount;

	h6280.h6280_totalcycles += cycles;
	h6280_ICount = 0;
	h6280.h6280_iCycles = 0;

	return cycles;
}

void h6280_get_context(void *dst)
{
	if( dst )
		*(h6280_Regs*)dst = h6280;
}

void h6280_set_context(void *src)
{
	if( src )
		h6280 = *(h6280_Regs*)src;
	CHANGE_PC;
}

int h6280TotalCycles()
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280TotalCycles called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280TotalCycles called with no CPU open\n"));
#endif

	return h6280.h6280_totalcycles + (h6280.h6280_iCycles - h6280_ICount);
}

void h6280RunEnd()
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280RunEnd called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280RunEnd called with no CPU open\n"));
#endif

	end_run = 1;
}

INT32 h6280Idle(INT32 cycles)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280Idle called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280Idle called with no CPU open\n"));
#endif

	h6280.h6280_totalcycles += cycles;

	return cycles;
}

/*****************************************************************************/

void h6280_set_irq_line(int irqline, int state)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_set_irq_line called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_set_irq_line called with no CPU open\n"));
#endif

	if (irqline == INPUT_LINE_NMI)
	{
		if ( state != ASSERT_LINE ) return;
		h6280.nmi_state = state;
		CHECK_IRQ_LINES;
	}
	else if (irqline < 3)
	{
		if (state == CPU_IRQSTATUS_HOLD) {
			state = CPU_IRQSTATUS_ACK;
			h6280.irq_hold = 1;
		}
		/* If the state has not changed, just return */
		if ( h6280.irq_state[irqline] == state )
			return;

	    h6280.irq_state[irqline] = state;

		CHECK_IRQ_LINES;
	}
}

/*****************************************************************************/

unsigned char h6280_irq_status_r(unsigned int offset)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_irq_status_r called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_irq_status_r called with no CPU open\n"));
#endif

	int status;

	switch (offset&3)
	{
	default:return h6280.io_buffer;break;
	case 3:
		{
			status=0;
			if(h6280.irq_state[1]!=CLEAR_LINE) status|=1; /* IRQ 2 */
			if(h6280.irq_state[0]!=CLEAR_LINE) status|=2; /* IRQ 1 */
			if(h6280.irq_state[2]!=CLEAR_LINE) status|=4; /* TIMER */
			return status|(h6280.io_buffer&(~H6280_IRQ_MASK));
		}
	case 2: return h6280.irq_mask|(h6280.io_buffer&(~H6280_IRQ_MASK));break;
	}
}

void h6280_irq_status_w(unsigned int offset, unsigned char data)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_irq_status_w called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_irq_status_w called with no CPU open\n"));
#endif

	h6280.io_buffer=data;

}

unsigned char h6280_timer_r(unsigned int)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_timer_r called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_timer_r called with no CPU open\n"));
#endif

	/* only returns countdown */
	return ((h6280.timer_value>>10)&0x7F)|(h6280.io_buffer&0x80);
}

void h6280_timer_w(unsigned int offset, unsigned char data)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_timer_w called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_timer_w called with no CPU open\n"));
#endif

	h6280.io_buffer=data;
	switch (offset&1) {
		case 0: /* Counter preload */
			h6280.timer_load=((data&127)+1)*1024;
			return;

		case 1: /* Counter enable */
			if(data&1)
			{	/* stop -> start causes reload */
				if(h6280.timer_status==0) h6280.timer_value=h6280.timer_load;
			}
			h6280.timer_status=data&1;
			return;
	}
}

#if 0
static int h6280_translate(int space, int intention, offs_t *addr)
{
#if 0
	if (space == ADDRESS_SPACE_PROGRAM)
		*addr = TRANSLATED(*addr);
	return TRUE;
#else
	if (space == 0)
		*addr = TRANSLATED(*addr);
	return 1;
#endif
}
#endif

UINT8 h6280io_get_buffer()
{
	return h6280.io_buffer;
}
void h6280io_set_buffer(UINT8 data)
{
	h6280.io_buffer=data;
}

#if 0
/*****************************************************************************/

/**************************************************************************
 * Generic set_info
 **************************************************************************/

static void h6280_set_info(UINT32 state, cpuinfo *info)
{
	switch (state)
	{
		/* --- the following bits of info are set as 64-bit signed integers --- */
		case CPUINFO_INT_INPUT_STATE + 0:			set_irq_line(0, info->i);					break;
		case CPUINFO_INT_INPUT_STATE + 1:			set_irq_line(1, info->i);					break;
		case CPUINFO_INT_INPUT_STATE + 2:			set_irq_line(2, info->i);					break;
		case CPUINFO_INT_INPUT_STATE + INPUT_LINE_NMI:set_irq_line(INPUT_LINE_NMI, info->i);	break;

		case CPUINFO_INT_PC:
		case CPUINFO_INT_REGISTER + H6280_PC:		PCW = info->i;								break;
		case CPUINFO_INT_SP:
		case CPUINFO_INT_REGISTER + H6280_S:		S = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_P:		P = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_A:		A = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_X:		X = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_Y:		Y = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_IRQ_MASK: h6280.irq_mask = info->i; CHECK_IRQ_LINES;	break;
		case CPUINFO_INT_REGISTER + H6280_TIMER_STATE: h6280.timer_status = info->i; 			break;
		case CPUINFO_INT_REGISTER + H6280_NMI_STATE: set_irq_line( INPUT_LINE_NMI, info->i ); 	break;
		case CPUINFO_INT_REGISTER + H6280_IRQ1_STATE: set_irq_line( 0, info->i ); 				break;
		case CPUINFO_INT_REGISTER + H6280_IRQ2_STATE: set_irq_line( 1, info->i ); 				break;
		case CPUINFO_INT_REGISTER + H6280_IRQT_STATE: set_irq_line( 2, info->i ); 				break;
		case CPUINFO_INT_REGISTER + H6280_M1:		h6280.mmr[0] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M2:		h6280.mmr[1] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M3:		h6280.mmr[2] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M4:		h6280.mmr[3] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M5:		h6280.mmr[4] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M6:		h6280.mmr[5] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M7:		h6280.mmr[6] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M8:		h6280.mmr[7] = info->i;						break;
	}
}

/**************************************************************************
 * Generic get_info
 **************************************************************************/

void h6280_get_info(UINT32 state, cpuinfo *info)
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case CPUINFO_INT_CONTEXT_SIZE:					info->i = sizeof(h6280);				break;
		case CPUINFO_INT_INPUT_LINES:					info->i = 3;							break;
		case CPUINFO_INT_DEFAULT_IRQ_VECTOR:			info->i = 0;							break;
		case CPUINFO_INT_ENDIANNESS:					info->i = CPU_IS_LE;					break;
		case CPUINFO_INT_CLOCK_MULTIPLIER:				info->i = 1;							break;
		case CPUINFO_INT_CLOCK_DIVIDER:					info->i = 1;							break;
		case CPUINFO_INT_MIN_INSTRUCTION_BYTES:			info->i = 1;							break;
		case CPUINFO_INT_MAX_INSTRUCTION_BYTES:			info->i = 7;							break;
		case CPUINFO_INT_MIN_CYCLES:					info->i = 2;							break;
		case CPUINFO_INT_MAX_CYCLES:					info->i = 17 + 6*65536;					break;

		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_PROGRAM:	info->i = 8;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 21;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_PROGRAM: info->i = 0;					break;
		case CPUINFO_INT_LOGADDR_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 16;					break;
		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_DATA:	info->i = 0;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_DATA: 	info->i = 0;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_DATA: 	info->i = 0;					break;
		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_IO:		info->i = 8;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_IO: 		info->i = 2;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_IO: 		info->i = 0;					break;

		case CPUINFO_INT_INPUT_STATE + 0:				info->i = h6280.irq_state[0];			break;
		case CPUINFO_INT_INPUT_STATE + 1:				info->i = h6280.irq_state[1];			break;
		case CPUINFO_INT_INPUT_STATE + 2:				info->i = h6280.irq_state[2];			break;
		case CPUINFO_INT_INPUT_STATE + INPUT_LINE_NMI:	info->i = h6280.nmi_state; 				break;

		case CPUINFO_INT_PREVIOUSPC:					info->i = h6280.ppc.d;					break;

		case CPUINFO_INT_PC:
		case CPUINFO_INT_REGISTER + H6280_PC:			info->i = PCD;							break;
		case CPUINFO_INT_SP:
		case CPUINFO_INT_REGISTER + H6280_S:			info->i = S;							break;
		case CPUINFO_INT_REGISTER + H6280_P:			info->i = P;							break;
		case CPUINFO_INT_REGISTER + H6280_A:			info->i = A;							break;
		case CPUINFO_INT_REGISTER + H6280_X:			info->i = X;							break;
		case CPUINFO_INT_REGISTER + H6280_Y:			info->i = Y;							break;
		case CPUINFO_INT_REGISTER + H6280_IRQ_MASK:		info->i = h6280.irq_mask;				break;
		case CPUINFO_INT_REGISTER + H6280_TIMER_STATE:	info->i = h6280.timer_status;			break;
		case CPUINFO_INT_REGISTER + H6280_NMI_STATE:	info->i = h6280.nmi_state;				break;
		case CPUINFO_INT_REGISTER + H6280_IRQ1_STATE:	info->i = h6280.irq_state[0];			break;
		case CPUINFO_INT_REGISTER + H6280_IRQ2_STATE:	info->i = h6280.irq_state[1];			break;
		case CPUINFO_INT_REGISTER + H6280_IRQT_STATE:	info->i = h6280.irq_state[2];			break;
		case CPUINFO_INT_REGISTER + H6280_M1:			info->i = h6280.mmr[0];					break;
		case CPUINFO_INT_REGISTER + H6280_M2:			info->i = h6280.mmr[1];					break;
		case CPUINFO_INT_REGISTER + H6280_M3:			info->i = h6280.mmr[2];					break;
		case CPUINFO_INT_REGISTER + H6280_M4:			info->i = h6280.mmr[3];					break;
		case CPUINFO_INT_REGISTER + H6280_M5:			info->i = h6280.mmr[4];					break;
		case CPUINFO_INT_REGISTER + H6280_M6:			info->i = h6280.mmr[5];					break;
		case CPUINFO_INT_REGISTER + H6280_M7:			info->i = h6280.mmr[6];					break;
		case CPUINFO_INT_REGISTER + H6280_M8:			info->i = h6280.mmr[7];					break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case CPUINFO_PTR_SET_INFO:						info->setinfo = h6280_set_info;			break;
		case CPUINFO_PTR_GET_CONTEXT:					info->getcontext = h6280_get_context;	break;
		case CPUINFO_PTR_SET_CONTEXT:					info->setcontext = h6280_set_context;	break;
		case CPUINFO_PTR_INIT:							info->init = h6280_init;				break;
		case CPUINFO_PTR_RESET:							info->reset = h6280_reset;				break;
		case CPUINFO_PTR_EXIT:							info->exit = h6280_exit;				break;
		case CPUINFO_PTR_EXECUTE:						info->execute = h6280_execute;			break;
		case CPUINFO_PTR_BURN:							info->burn = NULL;						break;
		case CPUINFO_PTR_DISASSEMBLE:					info->disassemble = h6280_dasm;			break;
		case CPUINFO_PTR_INSTRUCTION_COUNTER:			info->icount = &h6280_ICount;			break;
		case CPUINFO_PTR_TRANSLATE:						info->translate = h6280_translate;		break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case CPUINFO_STR_NAME:							strcpy(info->s, "HuC6280");				break;
		case CPUINFO_STR_CORE_FAMILY:					strcpy(info->s, "Hudsonsoft 6280");		break;
		case CPUINFO_STR_CORE_VERSION:					strcpy(info->s, "1.11");				break;
		case CPUINFO_STR_CORE_FILE:						strcpy(info->s, __FILE__);				break;
		case CPUINFO_STR_CORE_CREDITS:					strcpy(info->s, "Copyright Bryan McPhail, mish@tendril.co.uk"); break;

		case CPUINFO_STR_FLAGS:
			sprintf(info->s, "%c%c%c%c%c%c%c%c",
				h6280.p & 0x80 ? 'N':'.',
				h6280.p & 0x40 ? 'V':'.',
				h6280.p & 0x20 ? 'R':'.',
				h6280.p & 0x10 ? 'B':'.',
				h6280.p & 0x08 ? 'D':'.',
				h6280.p & 0x04 ? 'I':'.',
				h6280.p & 0x02 ? 'Z':'.',
				h6280.p & 0x01 ? 'C':'.');
			break;

		case CPUINFO_STR_REGISTER + H6280_PC:			sprintf(info->s, "PC:%04X", h6280.pc.d); break;
        case CPUINFO_STR_REGISTER + H6280_S:			sprintf(info->s, "S:%02X", h6280.sp.b.l); break;
        case CPUINFO_STR_REGISTER + H6280_P:			sprintf(info->s, "P:%02X", h6280.p); break;
        case CPUINFO_STR_REGISTER + H6280_A:			sprintf(info->s, "A:%02X", h6280.a); break;
		case CPUINFO_STR_REGISTER + H6280_X:			sprintf(info->s, "X:%02X", h6280.x); break;
		case CPUINFO_STR_REGISTER + H6280_Y:			sprintf(info->s, "Y:%02X", h6280.y); break;
		case CPUINFO_STR_REGISTER + H6280_IRQ_MASK:		sprintf(info->s, "IM:%02X", h6280.irq_mask); break;
		case CPUINFO_STR_REGISTER + H6280_TIMER_STATE:	sprintf(info->s, "TMR:%02X", h6280.timer_status); break;
		case CPUINFO_STR_REGISTER + H6280_NMI_STATE:	sprintf(info->s, "NMI:%X", h6280.nmi_state); break;
		case CPUINFO_STR_REGISTER + H6280_IRQ1_STATE:	sprintf(info->s, "IRQ1:%X", h6280.irq_state[0]); break;
		case CPUINFO_STR_REGISTER + H6280_IRQ2_STATE:	sprintf(info->s, "IRQ2:%X", h6280.irq_state[1]); break;
		case CPUINFO_STR_REGISTER + H6280_IRQT_STATE:	sprintf(info->s, "IRQT:%X", h6280.irq_state[2]); break;
		case CPUINFO_STR_REGISTER + H6280_M1:			sprintf(info->s, "M1:%02X", h6280.mmr[0]); break;
		case CPUINFO_STR_REGISTER + H6280_M2:			sprintf(info->s, "M2:%02X", h6280.mmr[1]); break;
		case CPUINFO_STR_REGISTER + H6280_M3:			sprintf(info->s, "M3:%02X", h6280.mmr[2]); break;
		case CPUINFO_STR_REGISTER + H6280_M4:			sprintf(info->s, "M4:%02X", h6280.mmr[3]); break;
		case CPUINFO_STR_REGISTER + H6280_M5:			sprintf(info->s, "M5:%02X", h6280.mmr[4]); break;
		case CPUINFO_STR_REGISTER + H6280_M6:			sprintf(info->s, "M6:%02X", h6280.mmr[5]); break;
		case CPUINFO_STR_REGISTER + H6280_M7:			sprintf(info->s, "M7:%02X", h6280.mmr[6]); break;
		case CPUINFO_STR_REGISTER + H6280_M8:			sprintf(info->s, "M8:%02X", h6280.mmr[7]); break;
	}
}
#endif


while( h6280.timer_value <= 0 )
					h6280.timer_value += h6280.timer_load;

/*****************************************************************************

    h6280.c - Portable HuC6280 emulator

    This source code is based (with permission!) on the 6502 emulator by
    Juergen Buchmueller.  It is released as part of the Mame emulator project.
    Let me know if you intend to use this code in any other project.

    NOTICE:

    This code is around 99% complete!  Several things are unimplemented,
    some due to lack of time, some due to lack of documentation, mainly
    due to lack of programs using these features.

    csh, csl opcodes are not supported.

    I am unsure if flag B is set upon execution of rti.

    Cycle counts should be quite accurate.

    Changelog, version 1.02:
        JMP + indirect X (0x7c) opcode fixed.
        SMB + RMB opcodes fixed in disassembler.
        change_pc function calls removed.
        TSB & TRB now set flags properly.
        BIT opcode altered.

    Changelog, version 1.03:
        Swapped IRQ mask for IRQ1 & IRQ2 (thanks Yasuhiro)

    Changelog, version 1.04, 28/9/99-22/10/99:
        Adjusted RTI (thanks Karl)
        TST opcodes fixed in disassembler (missing break statements in a case!).
        TST behaviour fixed.
        SMB/RMB/BBS/BBR fixed in disassembler.

    Changelog, version 1.05, 8/12/99-16/12/99:
        Added CAB's timer implementation (note: irq ack & timer reload are changed).
        Fixed STA IDX.
        Fixed B flag setting on BRK.
        Assumed CSH & CSL to take 2 cycles each.

        Todo:  Performance could be improved by precalculating timer fire position.

    Changelog, version 1.06, 4/5/00 - last opcode bug found?
        JMP indirect was doing a EAL++; instead of EAD++; - Obviously causing
        a corrupt read when L = 0xff!  This fixes Bloody Wolf and Trio The Punch!

    Changelog, version 1.07, 3/9/00:
        Changed timer to be single shot - fixes Crude Buster music in level 1.

    Changelog, version 1.08, 8/11/05: (Charles MacDonald)

        Changed timer implementation, no longer single shot and reading the
        timer registers returns the count only. Fixes the following:
        - Mesopotamia: Music tempo & in-game timer
        - Dragon Saber: DDA effects
        - Magical Chase: Music tempo and speed regulation
        - Cadash: Allows the first level to start
        - Turrican: Allows the game to start

        Changed PLX and PLY to set NZ flags. Fixes:
        - Afterburner: Graphics unpacking
        - Aoi Blink: Collision detection with background

        Fixed the decimal version of ADC/SBC to *not* update the V flag,
        only the binary ones do.

        Fixed B flag handling so it is always set outside of an interrupt;
        even after being set by PLP and RTI.

        Fixed P state after reset to set I and B, leaving T, D cleared and
        NVZC randomized (cleared in this case).

        Fixed interrupt processing order (Timer has highest priority followed
        by IRQ1 and finally IRQ2).

    Changelog, version 1.09, 1/07/06: (Rob Bohms)

        Added emulation of the T flag, fixes PCE Ankuku Densetsu title screen

    Changelog, version 1.10, 5/09/07: (Wilbert Pol)

        - Taking of interrupts is delayed to respect a pending instruction already
          in the instruction pipeline; fixes After Burner.
        - Added 1 cycle for decimal mode ADC and SBC instructions.
        - Changed cycle counts for CSH and CSL instructions to 3.
        - Added T flag support to the SBC instruction.
        - Fixed ADC T flag to set the Z flag based on the value read.
        - Added 3 cycle penalty to ADC, AND, EOR, ORA, and SBC instructions
          when the T flag is set.
        - Fixed cycle count and support for 65536 byte blocks for the TAI, TDD,
          TIA, TII, and TIN instructions.
        - Fixed RDWORD macro in the disassembler.
        - Fixed setting of N and V flags in the TST instructions.
        - Removed unneeded debug_mmr code.
        - Fixed TSB and TRB instructions.
        - Added 1 delay when accessing the VDC or VCE areas.
        - Implemented low and high speed cpu modes.

    Changelog, version 1.11, 18/09/07: (Wilbert Pol)

        - Improvements to the handling of taking of delayed interrupts.

******************************************************************************/
#include "burnint.h"
#include "h6280.h"
#include "h6280_intf.h"

#define offs_t	unsigned int
#define change_pc(x) PCW = (x)

#define CLEAR_LINE	0
#define ASSERT_LINE	1
#define INPUT_LINE_NMI	0x20

#define H6280_INLINE static

extern INT32 nh6280CpuActive;

static int 	h6280_ICount = 0;

static h6280_Regs  h6280;

void h6280_set_irq_line(INT32 irqline, INT32 state);
#define set_irq_line h6280_set_irq_line

/* include the macros */
#include "h6280ops.h"

/* include the opcode macros, functions and function pointer tables */
#include "tblh6280.c"

#if 0
/*****************************************************************************/
static void h6280_init(int index, int clock, const void *config, int (*irqcallback)(int))
{
	state_save_register_item("h6280", index, h6280.ppc.w.l);
	state_save_register_item("h6280", index, h6280.pc.w.l);
	state_save_register_item("h6280", index, h6280.sp.w.l);
	state_save_register_item("h6280", index, h6280.zp.w.l);
	state_save_register_item("h6280", index, h6280.ea.w.l);
	state_save_register_item("h6280", index, h6280.a);
	state_save_register_item("h6280", index, h6280.x);
	state_save_register_item("h6280", index, h6280.y);
	state_save_register_item("h6280", index, h6280.p);
	state_save_register_item_array("h6280", index, h6280.mmr);
	state_save_register_item("h6280", index, h6280.irq_mask);
	state_save_register_item("h6280", index, h6280.timer_status);
	state_save_register_item("h6280", index, h6280.timer_ack);
	state_save_register_item("h6280", index, h6280.clocks_per_cycle);
	state_save_register_item("h6280", index, h6280.timer_value);
	state_save_register_item("h6280", index, h6280.timer_load);
	state_save_register_item("h6280", index, h6280.nmi_state);
	state_save_register_item("h6280", index, h6280.irq_state[0]);
	state_save_register_item("h6280", index, h6280.irq_state[1]);
	state_save_register_item("h6280", index, h6280.irq_state[2]);
	state_save_register_item("h6280", index, h6280.irq_pending);

	#if LAZY_FLAGS
	state_save_register_item("h6280", index, h6280.NZ);
	#endif
	state_save_register_item("h6280", index, h6280.io_buffer);

	h6280.irq_callback = irqcallback;
}
#endif

void h6280_irqcallback(int (*irqcallback)(int))
{
	h6280.irq_callback = irqcallback;
}

void h6280Reset(void)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280Reset called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280Reset called with no CPU open\n"));
#endif

	/* wipe out the h6280 structure */
	int (*save_irqcallback)(int) = h6280.irq_callback;
	memset(&h6280, 0, sizeof(h6280_Regs));
	h6280.irq_callback = save_irqcallback;

	/* set I and B flags */
	P = _fI | _fB;

    /* stack starts at 0x01ff */
	h6280.sp.d = 0x1ff;

    /* read the reset vector into PC */
	PCL = RDMEM(H6280_RESET_VEC);
	PCH = RDMEM((H6280_RESET_VEC+1));
	CHANGE_PC;

	/* CPU starts in low speed mode */
    h6280.clocks_per_cycle = 4;

	/* timer off by default */
	h6280.timer_status=0;
	h6280.timer_load = 128 * 1024;

    /* clear pending interrupts */
	for (INT32 i = 0; i < 3; i++)
		h6280.irq_state[i] = CLEAR_LINE;
	h6280.nmi_state = CLEAR_LINE;

	h6280.irq_pending = 0;

	h6280.h6280_totalcycles = 0;
}

#if 0
static void h6280_exit(void)
{
	/* nothing */
}
#endif

static int end_run = 0;

int h6280Run(int cycles)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280Run called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280Run called with no CPU open\n"));
#endif

	int in;
	h6280_ICount = cycles;
	h6280.h6280_iCycles = cycles;

	end_run = 0;

	if ( h6280.irq_pending == 2 ) {
		h6280.irq_pending--;
	}

	/* Execute instructions */
	do
    {
    	if ((h6280.ppc.w.l ^ h6280.pc.w.l) & 0xe000)
    		CHANGE_PC;
		h6280.ppc = h6280.pc;

		/* Execute 1 instruction */
		in=RDOP();
		PCW++;
		insnh6280[in]();

		if ( h6280.irq_pending ) {
			if ( h6280.irq_pending == 1 ) {
				if ( !(P & _fI) ) {
					h6280.irq_pending--;
					CHECK_AND_TAKE_IRQ_LINES;
				}
			} else {
				h6280.irq_pending--;
			}
		}

		/* Check internal timer */
		if(h6280.timer_status)
		{
			if(h6280.timer_value<=0)
			{
				if ( ! h6280.irq_pending )
					h6280.irq_pending = 1;

				set_irq_line(2,ASSERT_LINE);
			}
		}
	} while (h6280_ICount > 0 && !end_run);

	cycles = cycles - h6280_ICount;

	h6280.h6280_totalcycles += cycles;
	h6280_ICount = 0;
	h6280.h6280_iCycles = 0;

	return cycles;
}

void h6280_get_context(void *dst)
{
	if( dst )
		*(h6280_Regs*)dst = h6280;
}

void h6280_set_context(void *src)
{
	if( src )
		h6280 = *(h6280_Regs*)src;
	CHANGE_PC;
}

int h6280TotalCycles()
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280TotalCycles called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280TotalCycles called with no CPU open\n"));
#endif

	return h6280.h6280_totalcycles + (h6280.h6280_iCycles - h6280_ICount);
}

void h6280RunEnd()
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280RunEnd called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280RunEnd called with no CPU open\n"));
#endif

	end_run = 1;
}

INT32 h6280Idle(INT32 cycles)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280Idle called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280Idle called with no CPU open\n"));
#endif

	h6280.h6280_totalcycles += cycles;

	return cycles;
}

/*****************************************************************************/

void h6280_set_irq_line(int irqline, int state)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_set_irq_line called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_set_irq_line called with no CPU open\n"));
#endif

	if (irqline == INPUT_LINE_NMI)
	{
		if ( state != ASSERT_LINE ) return;
		h6280.nmi_state = state;
		CHECK_IRQ_LINES;
	}
	else if (irqline < 3)
	{
		if (state == CPU_IRQSTATUS_HOLD) {
			state = CPU_IRQSTATUS_ACK;
			h6280.irq_hold = 1;
		}
		/* If the state has not changed, just return */
		if ( h6280.irq_state[irqline] == state )
			return;

	    h6280.irq_state[irqline] = state;

		CHECK_IRQ_LINES;
	}
}

/*****************************************************************************/

unsigned char h6280_irq_status_r(unsigned int offset)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_irq_status_r called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_irq_status_r called with no CPU open\n"));
#endif

	int status;

	switch (offset&3)
	{
	default:return h6280.io_buffer;break;
	case 3:
		{
			status=0;
			if(h6280.irq_state[1]!=CLEAR_LINE) status|=1; /* IRQ 2 */
			if(h6280.irq_state[0]!=CLEAR_LINE) status|=2; /* IRQ 1 */
			if(h6280.irq_state[2]!=CLEAR_LINE) status|=4; /* TIMER */
			return status|(h6280.io_buffer&(~H6280_IRQ_MASK));
		}
	case 2: return h6280.irq_mask|(h6280.io_buffer&(~H6280_IRQ_MASK));break;
	}
}

void h6280_irq_status_w(unsigned int offset, unsigned char data)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_irq_status_w called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_irq_status_w called with no CPU open\n"));
#endif

	h6280.io_buffer=data;
	switch (offset&3)
	{
		default:h6280.io_buffer=data;break;
		case 2: /* Write irq mask */
			h6280.irq_mask=data&0x7;
			CHECK_IRQ_LINES;
			break;

		case 3: /* Timer irq ack */
			set_irq_line(2,CLEAR_LINE);
			break;
	}
}

unsigned char h6280_timer_r(unsigned int)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_timer_r called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_timer_r called with no CPU open\n"));
#endif

	/* only returns countdown */
	return ((h6280.timer_value>>10)&0x7F)|(h6280.io_buffer&0x80);
}

void h6280_timer_w(unsigned int offset, unsigned char data)
{
#if defined FBNEO_DEBUG
	if (!DebugCPU_H6280Initted) bprintf(PRINT_ERROR, _T("h6280_timer_w called without init\n"));
	if (nh6280CpuActive == -1) bprintf(PRINT_ERROR, _T("h6280_timer_w called with no CPU open\n"));
#endif

	h6280.io_buffer=data;
	switch (offset&1) {
		case 0: /* Counter preload */
			h6280.timer_load=((data&127)+1)*1024;
			return;

		case 1: /* Counter enable */
			if(data&1)
			{	/* stop -> start causes reload */
				if(h6280.timer_status==0) h6280.timer_value=h6280.timer_load;
			}
			h6280.timer_status=data&1;
			return;
	}
}

#if 0
static int h6280_translate(int space, int intention, offs_t *addr)
{
#if 0
	if (space == ADDRESS_SPACE_PROGRAM)
		*addr = TRANSLATED(*addr);
	return TRUE;
#else
	if (space == 0)
		*addr = TRANSLATED(*addr);
	return 1;
#endif
}
#endif

UINT8 h6280io_get_buffer()
{
	return h6280.io_buffer;
}
void h6280io_set_buffer(UINT8 data)
{
	h6280.io_buffer=data;
}

#if 0
/*****************************************************************************/

/**************************************************************************
 * Generic set_info
 **************************************************************************/

static void h6280_set_info(UINT32 state, cpuinfo *info)
{
	switch (state)
	{
		/* --- the following bits of info are set as 64-bit signed integers --- */
		case CPUINFO_INT_INPUT_STATE + 0:			set_irq_line(0, info->i);					break;
		case CPUINFO_INT_INPUT_STATE + 1:			set_irq_line(1, info->i);					break;
		case CPUINFO_INT_INPUT_STATE + 2:			set_irq_line(2, info->i);					break;
		case CPUINFO_INT_INPUT_STATE + INPUT_LINE_NMI:set_irq_line(INPUT_LINE_NMI, info->i);	break;

		case CPUINFO_INT_PC:
		case CPUINFO_INT_REGISTER + H6280_PC:		PCW = info->i;								break;
		case CPUINFO_INT_SP:
		case CPUINFO_INT_REGISTER + H6280_S:		S = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_P:		P = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_A:		A = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_X:		X = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_Y:		Y = info->i;								break;
		case CPUINFO_INT_REGISTER + H6280_IRQ_MASK: h6280.irq_mask = info->i; CHECK_IRQ_LINES;	break;
		case CPUINFO_INT_REGISTER + H6280_TIMER_STATE: h6280.timer_status = info->i; 			break;
		case CPUINFO_INT_REGISTER + H6280_NMI_STATE: set_irq_line( INPUT_LINE_NMI, info->i ); 	break;
		case CPUINFO_INT_REGISTER + H6280_IRQ1_STATE: set_irq_line( 0, info->i ); 				break;
		case CPUINFO_INT_REGISTER + H6280_IRQ2_STATE: set_irq_line( 1, info->i ); 				break;
		case CPUINFO_INT_REGISTER + H6280_IRQT_STATE: set_irq_line( 2, info->i ); 				break;
		case CPUINFO_INT_REGISTER + H6280_M1:		h6280.mmr[0] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M2:		h6280.mmr[1] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M3:		h6280.mmr[2] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M4:		h6280.mmr[3] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M5:		h6280.mmr[4] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M6:		h6280.mmr[5] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M7:		h6280.mmr[6] = info->i;						break;
		case CPUINFO_INT_REGISTER + H6280_M8:		h6280.mmr[7] = info->i;						break;
	}
}

/**************************************************************************
 * Generic get_info
 **************************************************************************/

void h6280_get_info(UINT32 state, cpuinfo *info)
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case CPUINFO_INT_CONTEXT_SIZE:					info->i = sizeof(h6280);				break;
		case CPUINFO_INT_INPUT_LINES:					info->i = 3;							break;
		case CPUINFO_INT_DEFAULT_IRQ_VECTOR:			info->i = 0;							break;
		case CPUINFO_INT_ENDIANNESS:					info->i = CPU_IS_LE;					break;
		case CPUINFO_INT_CLOCK_MULTIPLIER:				info->i = 1;							break;
		case CPUINFO_INT_CLOCK_DIVIDER:					info->i = 1;							break;
		case CPUINFO_INT_MIN_INSTRUCTION_BYTES:			info->i = 1;							break;
		case CPUINFO_INT_MAX_INSTRUCTION_BYTES:			info->i = 7;							break;
		case CPUINFO_INT_MIN_CYCLES:					info->i = 2;							break;
		case CPUINFO_INT_MAX_CYCLES:					info->i = 17 + 6*65536;					break;

		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_PROGRAM:	info->i = 8;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 21;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_PROGRAM: info->i = 0;					break;
		case CPUINFO_INT_LOGADDR_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 16;					break;
		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_DATA:	info->i = 0;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_DATA: 	info->i = 0;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_DATA: 	info->i = 0;					break;
		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_IO:		info->i = 8;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_IO: 		info->i = 2;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_IO: 		info->i = 0;					break;

		case CPUINFO_INT_INPUT_STATE + 0:				info->i = h6280.irq_state[0];			break;
		case CPUINFO_INT_INPUT_STATE + 1:				info->i = h6280.irq_state[1];			break;
		case CPUINFO_INT_INPUT_STATE + 2:				info->i = h6280.irq_state[2];			break;
		case CPUINFO_INT_INPUT_STATE + INPUT_LINE_NMI:	info->i = h6280.nmi_state; 				break;

		case CPUINFO_INT_PREVIOUSPC:					info->i = h6280.ppc.d;					break;

		case CPUINFO_INT_PC:
		case CPUINFO_INT_REGISTER + H6280_PC:			info->i = PCD;							break;
		case CPUINFO_INT_SP:
		case CPUINFO_INT_REGISTER + H6280_S:			info->i = S;							break;
		case CPUINFO_INT_REGISTER + H6280_P:			info->i = P;							break;
		case CPUINFO_INT_REGISTER + H6280_A:			info->i = A;							break;
		case CPUINFO_INT_REGISTER + H6280_X:			info->i = X;							break;
		case CPUINFO_INT_REGISTER + H6280_Y:			info->i = Y;							break;
		case CPUINFO_INT_REGISTER + H6280_IRQ_MASK:		info->i = h6280.irq_mask;				break;
		case CPUINFO_INT_REGISTER + H6280_TIMER_STATE:	info->i = h6280.timer_status;			break;
		case CPUINFO_INT_REGISTER + H6280_NMI_STATE:	info->i = h6280.nmi_state;				break;
		case CPUINFO_INT_REGISTER + H6280_IRQ1_STATE:	info->i = h6280.irq_state[0];			break;
		case CPUINFO_INT_REGISTER + H6280_IRQ2_STATE:	info->i = h6280.irq_state[1];			break;
		case CPUINFO_INT_REGISTER + H6280_IRQT_STATE:	info->i = h6280.irq_state[2];			break;
		case CPUINFO_INT_REGISTER + H6280_M1:			info->i = h6280.mmr[0];					break;
		case CPUINFO_INT_REGISTER + H6280_M2:			info->i = h6280.mmr[1];					break;
		case CPUINFO_INT_REGISTER + H6280_M3:			info->i = h6280.mmr[2];					break;
		case CPUINFO_INT_REGISTER + H6280_M4:			info->i = h6280.mmr[3];					break;
		case CPUINFO_INT_REGISTER + H6280_M5:			info->i = h6280.mmr[4];					break;
		case CPUINFO_INT_REGISTER + H6280_M6:			info->i = h6280.mmr[5];					break;
		case CPUINFO_INT_REGISTER + H6280_M7:			info->i = h6280.mmr[6];					break;
		case CPUINFO_INT_REGISTER + H6280_M8:			info->i = h6280.mmr[7];					break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case CPUINFO_PTR_SET_INFO:						info->setinfo = h6280_set_info;			break;
		case CPUINFO_PTR_GET_CONTEXT:					info->getcontext = h6280_get_context;	break;
		case CPUINFO_PTR_SET_CONTEXT:					info->setcontext = h6280_set_context;	break;
		case CPUINFO_PTR_INIT:							info->init = h6280_init;				break;
		case CPUINFO_PTR_RESET:							info->reset = h6280_reset;				break;
		case CPUINFO_PTR_EXIT:							info->exit = h6280_exit;				break;
		case CPUINFO_PTR_EXECUTE:						info->execute = h6280_execute;			break;
		case CPUINFO_PTR_BURN:							info->burn = NULL;						break;
		case CPUINFO_PTR_DISASSEMBLE:					info->disassemble = h6280_dasm;			break;
		case CPUINFO_PTR_INSTRUCTION_COUNTER:			info->icount = &h6280_ICount;			break;
		case CPUINFO_PTR_TRANSLATE:						info->translate = h6280_translate;		break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case CPUINFO_STR_NAME:							strcpy(info->s, "HuC6280");				break;
		case CPUINFO_STR_CORE_FAMILY:					strcpy(info->s, "Hudsonsoft 6280");		break;
		case CPUINFO_STR_CORE_VERSION:					strcpy(info->s, "1.11");				break;
		case CPUINFO_STR_CORE_FILE:						strcpy(info->s, __FILE__);				break;
		case CPUINFO_STR_CORE_CREDITS:					strcpy(info->s, "Copyright Bryan McPhail, mish@tendril.co.uk"); break;

		case CPUINFO_STR_FLAGS:
			sprintf(info->s, "%c%c%c%c%c%c%c%c",
				h6280.p & 0x80 ? 'N':'.',
				h6280.p & 0x40 ? 'V':'.',
				h6280.p & 0x20 ? 'R':'.',
				h6280.p & 0x10 ? 'B':'.',
				h6280.p & 0x08 ? 'D':'.',
				h6280.p & 0x04 ? 'I':'.',
				h6280.p & 0x02 ? 'Z':'.',
				h6280.p & 0x01 ? 'C':'.');
			break;

		case CPUINFO_STR_REGISTER + H6280_PC:			sprintf(info->s, "PC:%04X", h6280.pc.d); break;
        case CPUINFO_STR_REGISTER + H6280_S:			sprintf(info->s, "S:%02X", h6280.sp.b.l); break;
        case CPUINFO_STR_REGISTER + H6280_P:			sprintf(info->s, "P:%02X", h6280.p); break;
        case CPUINFO_STR_REGISTER + H6280_A:			sprintf(info->s, "A:%02X", h6280.a); break;
		case CPUINFO_STR_REGISTER + H6280_X:			sprintf(info->s, "X:%02X", h6280.x); break;
		case CPUINFO_STR_REGISTER + H6280_Y:			sprintf(info->s, "Y:%02X", h6280.y); break;
		case CPUINFO_STR_REGISTER + H6280_IRQ_MASK:		sprintf(info->s, "IM:%02X", h6280.irq_mask); break;
		case CPUINFO_STR_REGISTER + H6280_TIMER_STATE:	sprintf(info->s, "TMR:%02X", h6280.timer_status); break;
		case CPUINFO_STR_REGISTER + H6280_NMI_STATE:	sprintf(info->s, "NMI:%X", h6280.nmi_state); break;
		case CPUINFO_STR_REGISTER + H6280_IRQ1_STATE:	sprintf(info->s, "IRQ1:%X", h6280.irq_state[0]); break;
		case CPUINFO_STR_REGISTER + H6280_IRQ2_STATE:	sprintf(info->s, "IRQ2:%X", h6280.irq_state[1]); break;
		case CPUINFO_STR_REGISTER + H6280_IRQT_STATE:	sprintf(info->s, "IRQT:%X", h6280.irq_state[2]); break;
		case CPUINFO_STR_REGISTER + H6280_M1:			sprintf(info->s, "M1:%02X", h6280.mmr[0]); break;
		case CPUINFO_STR_REGISTER + H6280_M2:			sprintf(info->s, "M2:%02X", h6280.mmr[1]); break;
		case CPUINFO_STR_REGISTER + H6280_M3:			sprintf(info->s, "M3:%02X", h6280.mmr[2]); break;
		case CPUINFO_STR_REGISTER + H6280_M4:			sprintf(info->s, "M4:%02X", h6280.mmr[3]); break;
		case CPUINFO_STR_REGISTER + H6280_M5:			sprintf(info->s, "M5:%02X", h6280.mmr[4]); break;
		case CPUINFO_STR_REGISTER + H6280_M6:			sprintf(info->s, "M6:%02X", h6280.mmr[5]); break;
		case CPUINFO_STR_REGISTER + H6280_M7:			sprintf(info->s, "M7:%02X", h6280.mmr[6]); break;
		case CPUINFO_STR_REGISTER + H6280_M8:			sprintf(info->s, "M8:%02X", h6280.mmr[7]); break;
	}
}
#endif


{
	string line;
	for(;getline(cin,line);){
		cout<<"CRIME TIME"<<endl;
	}
}

#include <iostream>
#include <string>
using namespace std;

int main()


{
  llvm::MD5::MD5Result res;
  llvm::MD5 MD5;
  MD5.update(llvm::makeArrayRef((uint8_t *)onnxModel, onnxModelSize));
  MD5.final(res);
  llvm::MD5::stringifyResult(res, str);
}

/*
 *
 */

#include "InlineOnnxifi.h"

#include "glow/Flags/Flags.h"
#include "glow/Optimizer/GraphOptimizer/GraphOptimizer.h"
#include "glow/Quantization/Quantization.h"
#include "glow/Quantization/Serialization.h"
#include "glow/Support/Support.h"

#include "llvm/Support/MD5.h"

namespace glow {
namespace onnxifi {

namespace {
std::string getProfileFile(llvm::StringRef hash) {
  return strFormat("/tmp/glow-profile-%s.yaml", hash.str().c_str());
}

void computeModelHash(const void *onnxModel, size_t onnxModelSize,
                      llvm::SmallString<32> &str)

} // namespace

onnxStatus InlineGraph::initGraph(
    const void *onnxModel, size_t onnxModelSize, uint32_t weightCount,
    const onnxTensorDescriptorV1 *weightDescriptors, uint32_t maxSeqLength,
    void * /*unused */, bool /*unused*/) {
  Module &mod = executionEngine_.getModule();
  // Note: Pass in a nullptr for PPC here because we do not currently support
  // pre-partitioned models here.
  std::unique_ptr<ONNXIFIModelLoader> loader;
  PlaceholderBindings dummyBindings;
  CompilationContext cctx{&dummyBindings, &loweredMap_};
  auto loaderOrErr = ONNXIFIModelLoader::parse(
      onnxModel, onnxModelSize, weightCount, weightDescriptors, mod, "function",
      cctx, /* staticPlaceholderTypes */ nullptr,
      true /*loadInputsAsPlaceholdersForOnnx*/, backendPtr_->getUseOnnx());
  if (loaderOrErr) {
    loader = std::move(*loaderOrErr);
  } else {
    LOG(ERROR) << "Error when loading model: "
               << ERR_TO_STRING(loaderOrErr.takeError());
    return ONNXIFI_STATUS_INVALID_MODEL;
  }

  CHECK_EQ(mod.getFunctions().size(), 1) << "Should have exactly one Function.";
  function_ = *mod.getFunctions().begin();

  bindPlaceholders(*loader);
  if (flags::SaveModel) {
    saveOnnxifiModel(function_);
  }

  setZeroLengthSequence(maxSeqLength);
  computeModelHash(onnxModel, onnxModelSize, modelHash_);
  optimize(function_, CompilationMode::Infer);

  PrecisionConfiguration &precConfig = cctx.precisionConfig;
  precConfig.quantMode = quantizationMode_;

  // If quantizing, load quantization infos and setup the schema.
  if (quantizationMode_ == QuantizationMode::Quantize) {
    auto fileExists = deserializeProfilingInfosFromYaml(
        getProfileFile(modelHash_), precConfig.quantConfig.graphPreLowerHash,
        precConfig.quantConfig.infos);
    if (!fileExists) {
      return ONNXIFI_STATUS_UNIDENTIFIED_NAME;
    }
    precConfig.quantConfig.schema = quantization::Schema::Symmetric;
  }

  executionEngine_.compile(CompilationMode::Infer);

  return ONNXIFI_STATUS_SUCCESS;
}

onnxStatus InlineGraph::run(std::unique_ptr<ExecutionContext> ctx,
                            EventPtr outputEvent,
                            onnxTraceEventList *traceEvents) {
  executionEngine_.run(*ctx);

  // Dump profile if requested.
  if (quantizationMode_ == QuantizationMode::Profile) {
    auto PI = quantization::generateNodeProfilingInfos(
        *(ctx->getPlaceholderBindings()), function_, loweredMap_);
    serializeProfilingInfosToYaml(getProfileFile(modelHash_),
                                  /* graphPreLowerHash */ 0, PI);
  }

  if (auto *traceContext = ctx->getTraceContext()) {
    setTraceEvents(traceEvents, traceContext);
  }

  outputEvent->signal(ONNXIFI_STATUS_SUCCESS);
  return ONNXIFI_STATUS_SUCCESS;
}

} // namespace onnxifi
} // namespace glow


/**
 */

#include <aws/cloudfront/model/LoggingConfig.h>
#include <aws/core/utils/xml/XmlSerializer.h>
#include <aws/core/utils/StringUtils.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>

#include <utility>

using namespace Aws::Utils::Xml;
using namespace Aws::Utils;

namespace Aws
{
namespace CloudFront
{
namespace Model
{

LoggingConfig::LoggingConfig() :
    m_enabled(false),
    m_enabledHasBeenSet(false),
    m_includeCookies(false),
    m_includeCookiesHasBeenSet(false),
    m_bucketHasBeenSet(false),
    m_prefixHasBeenSet(false)
{
}

LoggingConfig::LoggingConfig(const XmlNode& xmlNode) :
    m_enabled(false),
    m_enabledHasBeenSet(false),
    m_includeCookies(false),
    m_includeCookiesHasBeenSet(false),
    m_bucketHasBeenSet(false),
    m_prefixHasBeenSet(false)

LoggingConfig& LoggingConfig::operator =(const XmlNode& xmlNode)
{
  XmlNode resultNode = xmlNode;

  if(!resultNode.IsNull())
  {
    XmlNode enabledNode = resultNode.FirstChild("Enabled");
    if(!enabledNode.IsNull())
    {
      m_enabled = StringUtils::ConvertToBool(StringUtils::Trim(Aws::Utils::Xml::DecodeEscapedXmlText(enabledNode.GetText()).c_str()).c_str());
      m_enabledHasBeenSet = true;
    }
    XmlNode includeCookiesNode = resultNode.FirstChild("IncludeCookies");
    if(!includeCookiesNode.IsNull())
    {
      m_includeCookies = StringUtils::ConvertToBool(StringUtils::Trim(Aws::Utils::Xml::DecodeEscapedXmlText(includeCookiesNode.GetText()).c_str()).c_str());
      m_includeCookiesHasBeenSet = true;
    }
    XmlNode bucketNode = resultNode.FirstChild("Bucket");
    if(!bucketNode.IsNull())
    {
      m_bucket = Aws::Utils::Xml::DecodeEscapedXmlText(bucketNode.GetText());
      m_bucketHasBeenSet = true;
    }
    XmlNode prefixNode = resultNode.FirstChild("Prefix");
    if(!prefixNode.IsNull())
    {
      m_prefix = Aws::Utils::Xml::DecodeEscapedXmlText(prefixNode.GetText());
      m_prefixHasBeenSet = true;
    }
  }

  return *this;
}

void LoggingConfig::AddToNode(XmlNode& parentNode) const
{
  Aws::StringStream ss;
  if(m_enabledHasBeenSet)
  {
   XmlNode enabledNode = parentNode.CreateChildElement("Enabled");
   ss << std::boolalpha << m_enabled;
   enabledNode.SetText(ss.str());
   ss.str("");
  }

  if(m_includeCookiesHasBeenSet)
  {
   XmlNode includeCookiesNode = parentNode.CreateChildElement("IncludeCookies");
   ss << std::boolalpha << m_includeCookies;
   includeCookiesNode.SetText(ss.str());
   ss.str("");
  }

  if(m_bucketHasBeenSet)
  {
   XmlNode bucketNode = parentNode.CreateChildElement("Bucket");
   bucketNode.SetText(m_bucket);
  }

  if(m_prefixHasBeenSet)
  {
   XmlNode prefixNode = parentNode.CreateChildElement("Prefix");
   prefixNode.SetText(m_prefix);
  }

}

} // namespace Model
} // namespace CloudFront
} // namespace Aws


{
    try {
        ARG_ASSERT(0, (pPlot != 0));

        getPlot(pPlot)->setColor(pRed, pGreen, pBlue, pAlpha);
    }
    CATCHALL

    return FG_ERR_NONE;
}

/*******************************************************
 * All rights reserved.
 *
 ********************************************************/

#include <common/chart_renderables.hpp>
#include <common/handle.hpp>
#include <fg/plot.h>

using namespace forge;

using forge::common::getPlot;

fg_err fg_create_plot(fg_plot* pPlot, const unsigned pNPoints,
                      const fg_dtype pType, const fg_chart_type pChartType,
                      const fg_plot_type pPlotType,
                      const fg_marker_type pMarkerType) {
    try {
        ARG_ASSERT(1, (pNPoints > 0));

        *pPlot = getHandle(new common::Plot(
            pNPoints, (forge::dtype)pType, pPlotType, pMarkerType, pChartType));
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_retain_plot(fg_plot* pOut, fg_plot pIn) {
    try {
        ARG_ASSERT(1, (pIn != 0));

        common::Plot* temp = new common::Plot(getPlot(pIn));
        *pOut              = getHandle(temp);
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_release_plot(fg_plot pPlot) {
    try {
        ARG_ASSERT(0, (pPlot != 0));

        delete getPlot(pPlot);
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_set_plot_color(fg_plot pPlot, const float pRed, const float pGreen,
                         const float pBlue, const float pAlpha)

fg_err fg_set_plot_legend(fg_plot pPlot, const char* pLegend) {
    try {
        ARG_ASSERT(0, (pPlot != 0));
        ARG_ASSERT(1, (pLegend != 0));

        getPlot(pPlot)->setLegend(pLegend);
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_set_plot_marker_size(fg_plot pPlot, const float pMarkerSize) {
    try {
        ARG_ASSERT(1, (pPlot != 0));

        getPlot(pPlot)->setMarkerSize(pMarkerSize);
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_get_plot_vertex_buffer(unsigned* pOut, const fg_plot pPlot) {
    try {
        ARG_ASSERT(1, (pPlot != 0));

        *pOut = getPlot(pPlot)->vbo();
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_get_plot_color_buffer(unsigned* pOut, const fg_plot pPlot) {
    try {
        ARG_ASSERT(1, (pPlot != 0));

        *pOut = getPlot(pPlot)->cbo();
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_get_plot_alpha_buffer(unsigned* pOut, const fg_plot pPlot) {
    try {
        ARG_ASSERT(1, (pPlot != 0));

        *pOut = getPlot(pPlot)->abo();
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_get_plot_radii_buffer(unsigned* pOut, const fg_plot pPlot) {
    try {
        ARG_ASSERT(1, (pPlot != 0));

        *pOut = getPlot(pPlot)->mbo();
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_get_plot_vertex_buffer_size(unsigned* pOut, const fg_plot pPlot) {
    try {
        ARG_ASSERT(1, (pPlot != 0));

        *pOut = (unsigned)getPlot(pPlot)->vboSize();
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_get_plot_color_buffer_size(unsigned* pOut, const fg_plot pPlot) {
    try {
        ARG_ASSERT(1, (pPlot != 0));

        *pOut = (unsigned)getPlot(pPlot)->cboSize();
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_get_plot_alpha_buffer_size(unsigned* pOut, const fg_plot pPlot) {
    try {
        ARG_ASSERT(1, (pPlot != 0));

        *pOut = (unsigned)getPlot(pPlot)->aboSize();
    }
    CATCHALL

    return FG_ERR_NONE;
}

fg_err fg_get_plot_radii_buffer_size(unsigned* pOut, const fg_plot pPlot) {
    try {
        ARG_ASSERT(1, (pPlot != 0));

        *pOut = (unsigned)getPlot(pPlot)->mboSize();
    }
    CATCHALL

    return FG_ERR_NONE;
}


{
	return MonitorPluginInterfaceFactory::Create(service.c_str());
}

/***************************************************************************
 *                                                                         *
 ***************************************************************************/

#define SEISCOMP_COMPONENT ScMonitor
#include <seiscomp3/logging/log.h>

#include "monitorplugininterface.h"
#include "monitorfilter.h"

#include <iostream>
#include <boost/lexical_cast.hpp>

#include <seiscomp3/core/interfacefactory.ipp>

IMPLEMENT_INTERFACE_FACTORY(Seiscomp::Applications::MonitorPluginInterface, SC_MPLUGIN_API);

namespace Seiscomp {
namespace Applications {

IMPLEMENT_SC_ABSTRACT_CLASS(MonitorPluginInterface, "MonitorPluginInterface");

bool findName(ClientInfoData clientData, std::string name) {
	ClientInfoData::const_iterator it = clientData.find(Communication::CLIENTNAME_TAG);
	if ( it == clientData.end() )
		return false;
	if ( name != it->second )
		return false;
	return true;
}

MonitorPluginInterface::MonitorPluginInterface(const std::string& name)
	: _filterMeanInterval(10*60),
	  _filterMeanTimeMark(Core::Time::GMT()),
	  _name(name),
	  _operational(false),
	  _isFilteringEnabled(false),
	  _mFilterParser(NULL),
	  _filter(NULL) {
}

MonitorPluginInterface::~MonitorPluginInterface() {
	if ( _mFilterParser ) delete _mFilterParser;
	if ( _filter ) delete _filter;
}

MonitorPluginInterface* MonitorPluginInterface::Create(const std::string& service)

bool MonitorPluginInterface::init(const Config::Config &cfg) {
	setOperational(initFilter(cfg));
	return operational();
}

bool MonitorPluginInterface::initFilter(const Config::Config &cfg) {
	try {
		_filterStr = cfg.getString(_name + ".filter");
		SEISCOMP_DEBUG("Filter expression: %s", _filterStr.c_str());
		_mFilterParser = new MFilterParser;
		bs::tree_parse_info<> info = bs::ast_parse(_filterStr.c_str(), *_mFilterParser, bs::space_p);
		if ( info.full ) {
			SEISCOMP_DEBUG("Parsing filter expression succeed");
			MOperatorFactory factory;
			_filter = evalParseTree(info.trees.begin(), factory);
		}
		else
			SEISCOMP_ERROR("Parsing filter expression: %s failed at token: %c", _filterStr.c_str(), *info.stop);

		if ( !_filter ) {
			SEISCOMP_ERROR("Message Filter could not be instantiated.");
			return false;
		}
		_isFilteringEnabled = true;
	}
	catch ( Config::OptionNotFoundException& ) {
		SEISCOMP_DEBUG("Filter option for %s not set. Message filtering disabled.", _name.c_str());
	}
	return true;
}

bool MonitorPluginInterface::operational() const {
	return _operational;
}

bool MonitorPluginInterface::isFilteringEnabled() const {
	return _isFilteringEnabled;
}

void MonitorPluginInterface::setOperational(bool val) {
	_operational = val;
}

const std::string& MonitorPluginInterface::filterString() const {
	return _filterStr;
}

const ClientTable* MonitorPluginInterface::filter(const ClientTable& clientTable) {
	if ( !_filter )
		return NULL;

	_match.clear();
	for ( ClientTable::const_iterator it = clientTable.begin(); it != clientTable.end(); ++it ) {
		if ( _filter->eval(*it) )
			_match.push_back(*it);
	}
	return match();
}

const ClientTable* MonitorPluginInterface::filterMean(const ClientTable& clientTable) {
	// Delete already disconnected clients
	ClientTable::iterator fmIt = _filterMeanClientTable.begin();
	while ( fmIt != _filterMeanClientTable.end() ) {
		ClientTable::const_iterator found = std::find_if(
				clientTable.begin(),
				clientTable.end(),
				std::bind2nd(std::ptr_fun(findName), fmIt->info[Communication::CLIENTNAME_TAG])
		);
		if ( found == clientTable.end() ) {
			_filterMeanMessageCount.erase(fmIt->info[Communication::CLIENTNAME_TAG]);
			fmIt = _filterMeanClientTable.erase(fmIt);
		}
		else {
			++fmIt;
		}
	}

	// Add newly connected clients
	ClientTable::const_iterator ctIt = clientTable.begin();
	for ( ; ctIt != clientTable.end(); ++ctIt ) {
		ClientTable::iterator found = std::find_if(
				_filterMeanClientTable.begin(),
				_filterMeanClientTable.end(),
				std::bind2nd(std::ptr_fun(findName), ctIt->info.find(Communication::CLIENTNAME_TAG)->second)
		);

		if ( found != _filterMeanClientTable.end() ) {
			// Update data
			sumData<Communication::TOTAL_MEMORY_TAG>(*found, *ctIt);
			sumData<Communication::CLIENT_MEMORY_USAGE_TAG>(*found, *ctIt);
			sumData<Communication::MEMORY_USAGE_TAG>(*found, *ctIt);
			sumData<Communication::CPU_USAGE_TAG>(*found, *ctIt);
			sumData<Communication::MESSAGE_QUEUE_SIZE_TAG>(*found, *ctIt);
			sumData<Communication::AVERAGE_MESSAGE_QUEUE_SIZE_TAG>(*found, *ctIt);
			found->info[Communication::UPTIME_TAG] = ctIt->info.find(Communication::UPTIME_TAG)->second;
			found->info[Communication::RESPONSE_TIME_TAG] = ctIt->info.find(Communication::RESPONSE_TIME_TAG)->second;

			++(_filterMeanMessageCount[ctIt->info.find(Communication::CLIENTNAME_TAG)->second]);
		}
		else {
			_filterMeanClientTable.push_back(*ctIt);
			_filterMeanMessageCount[ctIt->info.find(Communication::CLIENTNAME_TAG)->second] = 1;
		}
	}

	if ( Core::Time::GMT() - _filterMeanTimeMark >= _filterMeanInterval ) {
		_filterMeanTimeMark = Core::Time::GMT();
		// Calculate mean
		for ( ClientTable::iterator it = _filterMeanClientTable.begin();
			 it != _filterMeanClientTable.end(); ++it ) {
			size_t count = _filterMeanMessageCount[it->info[Communication::CLIENTNAME_TAG]];
			calculateMean<Communication::TOTAL_MEMORY_TAG>(*it, count);
			calculateMean<Communication::CLIENT_MEMORY_USAGE_TAG>(*it, count);
			calculateMean<Communication::MEMORY_USAGE_TAG>(*it, count);
			calculateMean<Communication::CPU_USAGE_TAG>(*it, count);
			calculateMean<Communication::MESSAGE_QUEUE_SIZE_TAG>(*it, count);
			calculateMean<Communication::AVERAGE_MESSAGE_QUEUE_SIZE_TAG>(*it, count);
		}
		return filter(_filterMeanClientTable);
	}
	return NULL;
}

void MonitorPluginInterface::setFilterMeanInterval(double interval) {
	_filterMeanInterval = interval * 60;
}

const ClientTable* MonitorPluginInterface::match() const {
	return _match.size() > 0 ? &_match : NULL;
}

const std::string& MonitorPluginInterface::name() const {
	return _name;
}

template <Communication::EConnectionInfoTag tag>
void MonitorPluginInterface::sumData(ClientInfoData& lhs, const ClientInfoData& rhs) {
	typedef typename Communication::ConnectionInfoT<tag>::Type Type;
	ClientInfoData::const_iterator found = rhs.find(tag);
	if ( found == rhs.end() ) {
		SEISCOMP_ERROR("Incompatible data found. Tag %s could not be found in ClientInfoData",
				Communication::ConnectionInfoTag(tag).toString());
	}

	try {
		Type result = boost::lexical_cast<Type>(lhs[tag]) +
			          boost::lexical_cast<Type>(found->second);
		lhs[tag] = boost::lexical_cast<std::string>(result);
	}
	catch ( boost::bad_lexical_cast& e ) {
		SEISCOMP_ERROR("%s", e.what());
		SEISCOMP_ERROR(
				"[%s, %s@%s] Datatypes could not be converted",
				rhs.find(Communication::CLIENTNAME_TAG)->second.c_str(),
				rhs.find(Communication::PROGRAMNAME_TAG)->second.c_str(),
				rhs.find(Communication::HOSTNAME_TAG)->second.c_str()
		);
		SEISCOMP_ERROR(
				"tag = %s, lhs[tag] = %s, found->second = %s",
				Communication::ConnectionInfoTag(tag).toString(),
				lhs[tag].c_str(),
				found->second.c_str()
		);
	}
}

template <Communication::EConnectionInfoTag tag>
void MonitorPluginInterface::calculateMean(ClientInfoData& lhs, size_t count) {
	typedef typename Communication::ConnectionInfoT<tag>::Type Type;
	try {
		Type result = boost::lexical_cast<Type>(lhs[tag]) / count;
		lhs[tag] = boost::lexical_cast<std::string>(result);
	}
	catch ( boost::bad_lexical_cast& e ) {
		SEISCOMP_ERROR("%s", e.what());
		SEISCOMP_ERROR(
				"[%s, %s@%s] Datatypes could not be converted",
				lhs.find(Communication::CLIENTNAME_TAG)->second.c_str(),
				lhs.find(Communication::PROGRAMNAME_TAG)->second.c_str(),
				lhs.find(Communication::HOSTNAME_TAG)->second.c_str()
		);
		SEISCOMP_ERROR(
				"tag = %s, lhs[tag] = %s",
				Communication::ConnectionInfoTag(tag).toString(),
				lhs[tag].c_str()
		);
	}
}

} // namespace Applications
} // namespace Seiscomp


while ( fmIt != _filterMeanClientTable.end() ) {
		ClientTable::const_iterator found = std::find_if(
				clientTable.begin(),
				clientTable.end(),
				std::bind2nd(std::ptr_fun(findName), fmIt->info[Communication::CLIENTNAME_TAG])
		);
		if ( found == clientTable.end() ) {
			_filterMeanMessageCount.erase(fmIt->info[Communication::CLIENTNAME_TAG]);
			fmIt = _filterMeanClientTable.erase(fmIt);
		}
		else {
			++fmIt;
		}
	}

/***************************************************************************
 *                                                                         *
 ***************************************************************************/

#define SEISCOMP_COMPONENT ScMonitor
#include <seiscomp3/logging/log.h>

#include "monitorplugininterface.h"
#include "monitorfilter.h"

#include <iostream>
#include <boost/lexical_cast.hpp>

#include <seiscomp3/core/interfacefactory.ipp>

IMPLEMENT_INTERFACE_FACTORY(Seiscomp::Applications::MonitorPluginInterface, SC_MPLUGIN_API);

namespace Seiscomp {
namespace Applications {

IMPLEMENT_SC_ABSTRACT_CLASS(MonitorPluginInterface, "MonitorPluginInterface");

bool findName(ClientInfoData clientData, std::string name) {
	ClientInfoData::const_iterator it = clientData.find(Communication::CLIENTNAME_TAG);
	if ( it == clientData.end() )
		return false;
	if ( name != it->second )
		return false;
	return true;
}

MonitorPluginInterface::MonitorPluginInterface(const std::string& name)
	: _filterMeanInterval(10*60),
	  _filterMeanTimeMark(Core::Time::GMT()),
	  _name(name),
	  _operational(false),
	  _isFilteringEnabled(false),
	  _mFilterParser(NULL),
	  _filter(NULL) {
}

MonitorPluginInterface::~MonitorPluginInterface() {
	if ( _mFilterParser ) delete _mFilterParser;
	if ( _filter ) delete _filter;
}

MonitorPluginInterface* MonitorPluginInterface::Create(const std::string& service) {
	return MonitorPluginInterfaceFactory::Create(service.c_str());
}

bool MonitorPluginInterface::init(const Config::Config &cfg) {
	setOperational(initFilter(cfg));
	return operational();
}

bool MonitorPluginInterface::initFilter(const Config::Config &cfg) {
	try {
		_filterStr = cfg.getString(_name + ".filter");
		SEISCOMP_DEBUG("Filter expression: %s", _filterStr.c_str());
		_mFilterParser = new MFilterParser;
		bs::tree_parse_info<> info = bs::ast_parse(_filterStr.c_str(), *_mFilterParser, bs::space_p);
		if ( info.full ) {
			SEISCOMP_DEBUG("Parsing filter expression succeed");
			MOperatorFactory factory;
			_filter = evalParseTree(info.trees.begin(), factory);
		}
		else
			SEISCOMP_ERROR("Parsing filter expression: %s failed at token: %c", _filterStr.c_str(), *info.stop);

		if ( !_filter ) {
			SEISCOMP_ERROR("Message Filter could not be instantiated.");
			return false;
		}
		_isFilteringEnabled = true;
	}
	catch ( Config::OptionNotFoundException& ) {
		SEISCOMP_DEBUG("Filter option for %s not set. Message filtering disabled.", _name.c_str());
	}
	return true;
}

bool MonitorPluginInterface::operational() const {
	return _operational;
}

bool MonitorPluginInterface::isFilteringEnabled() const {
	return _isFilteringEnabled;
}

void MonitorPluginInterface::setOperational(bool val) {
	_operational = val;
}

const std::string& MonitorPluginInterface::filterString() const {
	return _filterStr;
}

const ClientTable* MonitorPluginInterface::filter(const ClientTable& clientTable) {
	if ( !_filter )
		return NULL;

	_match.clear();
	for ( ClientTable::const_iterator it = clientTable.begin(); it != clientTable.end(); ++it ) {
		if ( _filter->eval(*it) )
			_match.push_back(*it);
	}
	return match();
}

const ClientTable* MonitorPluginInterface::filterMean(const ClientTable& clientTable) {
	// Delete already disconnected clients
	ClientTable::iterator fmIt = _filterMeanClientTable.begin();

	// Add newly connected clients
	ClientTable::const_iterator ctIt = clientTable.begin();
	for ( ; ctIt != clientTable.end(); ++ctIt ) {
		ClientTable::iterator found = std::find_if(
				_filterMeanClientTable.begin(),
				_filterMeanClientTable.end(),
				std::bind2nd(std::ptr_fun(findName), ctIt->info.find(Communication::CLIENTNAME_TAG)->second)
		);

		if ( found != _filterMeanClientTable.end() ) {
			// Update data
			sumData<Communication::TOTAL_MEMORY_TAG>(*found, *ctIt);
			sumData<Communication::CLIENT_MEMORY_USAGE_TAG>(*found, *ctIt);
			sumData<Communication::MEMORY_USAGE_TAG>(*found, *ctIt);
			sumData<Communication::CPU_USAGE_TAG>(*found, *ctIt);
			sumData<Communication::MESSAGE_QUEUE_SIZE_TAG>(*found, *ctIt);
			sumData<Communication::AVERAGE_MESSAGE_QUEUE_SIZE_TAG>(*found, *ctIt);
			found->info[Communication::UPTIME_TAG] = ctIt->info.find(Communication::UPTIME_TAG)->second;
			found->info[Communication::RESPONSE_TIME_TAG] = ctIt->info.find(Communication::RESPONSE_TIME_TAG)->second;

			++(_filterMeanMessageCount[ctIt->info.find(Communication::CLIENTNAME_TAG)->second]);
		}
		else {
			_filterMeanClientTable.push_back(*ctIt);
			_filterMeanMessageCount[ctIt->info.find(Communication::CLIENTNAME_TAG)->second] = 1;
		}
	}

	if ( Core::Time::GMT() - _filterMeanTimeMark >= _filterMeanInterval ) {
		_filterMeanTimeMark = Core::Time::GMT();
		// Calculate mean
		for ( ClientTable::iterator it = _filterMeanClientTable.begin();
			 it != _filterMeanClientTable.end(); ++it ) {
			size_t count = _filterMeanMessageCount[it->info[Communication::CLIENTNAME_TAG]];
			calculateMean<Communication::TOTAL_MEMORY_TAG>(*it, count);
			calculateMean<Communication::CLIENT_MEMORY_USAGE_TAG>(*it, count);
			calculateMean<Communication::MEMORY_USAGE_TAG>(*it, count);
			calculateMean<Communication::CPU_USAGE_TAG>(*it, count);
			calculateMean<Communication::MESSAGE_QUEUE_SIZE_TAG>(*it, count);
			calculateMean<Communication::AVERAGE_MESSAGE_QUEUE_SIZE_TAG>(*it, count);
		}
		return filter(_filterMeanClientTable);
	}
	return NULL;
}

void MonitorPluginInterface::setFilterMeanInterval(double interval) {
	_filterMeanInterval = interval * 60;
}

const ClientTable* MonitorPluginInterface::match() const {
	return _match.size() > 0 ? &_match : NULL;
}

const std::string& MonitorPluginInterface::name() const {
	return _name;
}

template <Communication::EConnectionInfoTag tag>
void MonitorPluginInterface::sumData(ClientInfoData& lhs, const ClientInfoData& rhs) {
	typedef typename Communication::ConnectionInfoT<tag>::Type Type;
	ClientInfoData::const_iterator found = rhs.find(tag);
	if ( found == rhs.end() ) {
		SEISCOMP_ERROR("Incompatible data found. Tag %s could not be found in ClientInfoData",
				Communication::ConnectionInfoTag(tag).toString());
	}

	try {
		Type result = boost::lexical_cast<Type>(lhs[tag]) +
			          boost::lexical_cast<Type>(found->second);
		lhs[tag] = boost::lexical_cast<std::string>(result);
	}
	catch ( boost::bad_lexical_cast& e ) {
		SEISCOMP_ERROR("%s", e.what());
		SEISCOMP_ERROR(
				"[%s, %s@%s] Datatypes could not be converted",
				rhs.find(Communication::CLIENTNAME_TAG)->second.c_str(),
				rhs.find(Communication::PROGRAMNAME_TAG)->second.c_str(),
				rhs.find(Communication::HOSTNAME_TAG)->second.c_str()
		);
		SEISCOMP_ERROR(
				"tag = %s, lhs[tag] = %s, found->second = %s",
				Communication::ConnectionInfoTag(tag).toString(),
				lhs[tag].c_str(),
				found->second.c_str()
		);
	}
}

template <Communication::EConnectionInfoTag tag>
void MonitorPluginInterface::calculateMean(ClientInfoData& lhs, size_t count) {
	typedef typename Communication::ConnectionInfoT<tag>::Type Type;
	try {
		Type result = boost::lexical_cast<Type>(lhs[tag]) / count;
		lhs[tag] = boost::lexical_cast<std::string>(result);
	}
	catch ( boost::bad_lexical_cast& e ) {
		SEISCOMP_ERROR("%s", e.what());
		SEISCOMP_ERROR(
				"[%s, %s@%s] Datatypes could not be converted",
				lhs.find(Communication::CLIENTNAME_TAG)->second.c_str(),
				lhs.find(Communication::PROGRAMNAME_TAG)->second.c_str(),
				lhs.find(Communication::HOSTNAME_TAG)->second.c_str()
		);
		SEISCOMP_ERROR(
				"tag = %s, lhs[tag] = %s",
				Communication::ConnectionInfoTag(tag).toString(),
				lhs[tag].c_str()
		);
	}
}

} // namespace Applications
} // namespace Seiscomp


{
    strcpy(fullname, "Noname");
    handicap = 0;
}

#include <cstring>
#include <iostream>
#include "golf.h"

Golf::Golf()

Golf::Golf(const char * name, int hc)
{
    strcpy(fullname, name);
    handicap = hc;
}

const Golf & Golf::setgolf(const Golf & g)
{
    strcpy(fullname, g.fullname);
    handicap = g.handicap;

    return *this;
}

void Golf::showgolf() const
{
    std::cout << "fullname: " << fullname << std::endl;
    std::cout << "handicap: " << handicap << std::endl;
}