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

  machine.c

  Functions to emulate general aspects of the machine (RAM, ROM, interrupts,
  I/O ports)

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

#include "driver.h"
#include "vidhrdw/generic.h"
#include "cpu/z80/z80.h"

// this prototype will move to the driver
void stfight_bank_w( int offset, int data );


/*

Encryption PAL 16R4 on CPU board

          +---U---+
     CP --|       |-- VCC
 ROM D1 --|       |-- ROM D0          M1 = 0                M1 = 1
 ROM D3 --|       |-- (NC)
 ROM D4 --|       |-- D6         D6 = D1 ^^ D3          D6 = / ( D1 ^^ D0 )
 ROM D6 --|       |-- D4         D4 = / ( D6 ^^ A7 )    D4 = D3 ^^ A0
     A0 --|       |-- D3         D3 = / ( D0 ^^ A1 )    D3 = D4 ^^ A4
     A1 --|       |-- D0         D0 = D1 ^^ D4          D0 = / ( D6 ^^ A0 )
     A4 --|       |-- (NC)
     A7 --|       |-- /M1
    GND --|       |-- /OE
          +-------+

*/

void stfight_decode(void)
{
    int             A;
	unsigned char   *RAM = Machine->memory_region[Machine->drv->cpu[0].memory_region];

    for( A=0; A<0x8000; A++ )
    {
        unsigned char src = RAM[A];

        // decode opcode
        ROM[A] = ( src & 0xA6 ) |
                 ( ( ( ( src << 2 ) ^ src ) << 3 ) & 0x40 ) |
                 ( ~( ( src ^ ( A >> 1 ) ) >> 2 ) & 0x10 ) |
                 ( ~( ( ( src << 1 ) ^ A ) << 2 ) & 0x08 ) |
                 ( ( ( src ^ ( src >> 3 ) ) >> 1 ) & 0x01 );

        // decode operand
        RAM[A] = ( src & 0xA6 ) |
                 ( ~( ( src ^ ( src << 1 ) ) << 5 ) & 0x40 ) |
                 ( ( ( src ^ ( A << 3 ) ) << 1 ) & 0x10 ) |
                 ( ( ( src ^ A ) >> 1 ) & 0x08 ) |
                 ( ~( ( src >> 6 ) ^ A ) & 0x01 );
    }
}

void stfight_init_machine( void )
{
    static int patch_0[] =
    {
        0x00B1, 0x00B2, 0x00B3,     // wait for NMI after power-on
        0x00B4, 0x00B5,             // fix stack

        //0x00C1, 0x00C2, 0x00C3,     // check coin circuit

        //0x2CBB, 0x2CBC, 0x2CBD,     // read coin mechs

        //0x2858, 0x2859,             // wait for 30Hz interrupt

        0x0000
    };

	unsigned char *RAM = Machine->memory_region[Machine->drv->cpu[0].memory_region];
    int             i;

    // Insert NOPs into ROM code
    for( i=0; patch_0[i]; i++ )
    {
        // patch opcode space
        ROM[patch_0[i]] = 0x00;
        // patch operand space
        RAM[patch_0[i]] = 0x00;
    }

    // initialise rom bank
    stfight_bank_w( 0, 0 );
}

// It's entirely possible that this bank is never switched out
// - in fact I don't even know how/where it's switched in!
void stfight_bank_w( int offset, int data )
{
	unsigned char   *ROM2 = Machine->memory_region[Machine->drv->cpu[0].memory_region] + 0x10000;

	cpu_setbank( 1, &ROM2[data<<14] );
}

int stfight_vb_interrupt( void )
{
    // Do a RST10
    interrupt_vector_w( 0, 0xd7 );

	return( interrupt() );
}

/*
 *      CPU 1 timed interrupt - 30Hz???
 */

int stfight_interrupt_1( void )
{
    // Do a RST08
    interrupt_vector_w( 0, 0xcf );

	return( interrupt() );
}

/*
 *      CPU 2 timed interrupt - 120Hz
 */

int stfight_interrupt_2( void )
{
	return( interrupt() );
}

/*
 *      Hardware handlers
 */

// Perhaps define dipswitches as active low?
int stfight_dsw_r( int offset )
{
    return( ~readinputport( 3+offset ) );
}

static int stfight_coin_mech_query_active = 0;
static int stfight_coin_mech_query;

int stfight_coin_r( int offset )
{
    static int coin_mech_latch[2] = { 0x02, 0x01 };

    int coin_mech_data;
    int i;

    // Was the coin mech queried by software?
    if( stfight_coin_mech_query_active )
    {
        stfight_coin_mech_query_active = 0;
        return( (~stfight_coin_mech_query) & 0x03 );
    }

    /*
     *      Is this really necessary?
     *      - we can control impulse length so that the port is
     *        never strobed twice within the impulse period
     *        since it's read by the 30Hz interrupt ISR
     */

    coin_mech_data = readinputport( 5 );

    for( i=0; i<2; i++ )
    {
        /* Only valid on signal edge */
        if( ( coin_mech_data & (1<<i) ) != coin_mech_latch[i] )
            coin_mech_latch[i] = coin_mech_data & (1<<i);
        else
            coin_mech_data |= coin_mech_data & (1<<i);
    }

    return( coin_mech_data );
}

void stfight_coin_w( int offset, int data )
{
    // interrogate coin mech
    stfight_coin_mech_query_active = 1;
    stfight_coin_mech_query = data;
}

/*
 *      Machine hardware for MSM5205 ADPCM sound control
 */

static int sampleLimits[] =
{
    0x0000,     // machine gun fire?
    0x1000,     // player getting shot
    0x2C00,     // player shooting
    0x3C00,     // girl screaming
    0x5400,     // girl getting shot
    0x7200      // (end of samples)
};
static int adpcm_data_offs;
static int adpcm_data_end;

void stfight_adpcm_int( int data )
{
	static int toggle;
	unsigned char *SAMPLES = Machine->memory_region[2];
	int adpcm_data = SAMPLES[adpcm_data_offs & 0x7fff];

    // finished playing sample?
    if( adpcm_data_offs == adpcm_data_end )
    {
        MSM5205_reset_w( 0, 1 );
        return;
    }

	if( toggle == 0 )
		MSM5205_data_w( 0, ( adpcm_data >> 4 ) & 0x0f );
	else
	{
		MSM5205_data_w( 0, adpcm_data & 0x0f );
		adpcm_data_offs++;
	}

	toggle ^= 1;
}

void stfight_adpcm_control_w( int offset, int data )
{
    if( data < 0x08 )
    {
        adpcm_data_offs = sampleLimits[data];
        adpcm_data_end = sampleLimits[data+1];
    }

    MSM5205_reset_w( 0, data & 0x08 ? 1 : 0 );
}

void stfight_e800_w( int offset, int data )
{
}

/*
 *      Machine hardware for YM2303 fm sound control
 */

static unsigned char fm_data;

void stfight_fm_w( int offset, int data )
{
    // the sound cpu ignores any fm data without bit 7 set
    fm_data = 0x80 | data;
}

int stfight_fm_r( int offset )
{
    int data = fm_data;

    // clear the latch?!?
    fm_data &= 0x7f;

    return( data );
}
