/*****************************************************************************
** $Source: /cvsroot/bluemsx/blueMSX/Src/Memory/romMapperF4device.c,v $
**
** $Revision: 1.6 $
**
** $Date: 2006/06/14 19:59:52 $
**
** More info: http://www.bluemsx.com
**
** Copyright (C) 2003-2004 Daniel Vik
**
**  This software is provided 'as-is', without any express or implied
**  warranty.  In no event will the authors be held liable for any damages
**  arising from the use of this software.
**
**  Permission is granted to anyone to use this software for any purpose,
**  including commercial applications, and to alter it and redistribute it
**  freely, subject to the following restrictions:
**
**  1. The origin of this software must not be misrepresented; you must not
**     claim that you wrote the original software. If you use this software
**     in a product, an acknowledgment in the product documentation would be
**     appreciated but is not required.
**  2. Altered source versions must be plainly marked as such, and must not be
**     misrepresented as being the original software.
**  3. This notice may not be removed or altered from any source distribution.
**
******************************************************************************
*/
#include "romMapperF4device.h"
#include "MediaDb.h"
#include "DeviceManager.h"
#include "DebugDeviceManager.h"
#include "SaveState.h"
#include "IoPort.h"
#include "Language.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct {
    int deviceHandle;
    int debugHandle;
    int inverted;
    int status;
} RomMapperF4device;

static void saveState(RomMapperF4device* rm)
{
    SaveState* state = saveStateOpenForWrite("mapperF4device");

    saveStateSet(state, "status", rm->status);

    saveStateClose(state);
}

static void loadState(RomMapperF4device* rm)
{
    SaveState* state = saveStateOpenForRead("mapperF4device");
    
    rm->status = saveStateGet(state, "status", 0);

    saveStateClose(state);
}

static void destroy(RomMapperF4device* rm)
{
    deviceManagerUnregister(rm->deviceHandle);
    debugDeviceUnregister(rm->debugHandle);

    ioPortUnregister(0xf4);

    free(rm);
}

static UInt8 read(RomMapperF4device* rm, UInt16 ioPort)
{
    return rm->status;
}

static void write(RomMapperF4device* rm, UInt16 ioPort, UInt8 value)
{	
	if (rm->inverted) {
		rm->status = value | 0x7f;
	} 
    else {
		rm->status = (rm->status & 0x20) | (value & 0xa0);
	}
}

static void reset(RomMapperF4device* rm)
{
    rm->status = rm->inverted ? 0xff : 0;
}

static void getDebugInfo(RomMapperF4device* rm, DbgDevice* dbgDevice)
{
    DbgIoPorts* ioPorts;

    ioPorts = dbgDeviceAddIoPorts(dbgDevice, langDbgDevF4Device(), 1);
    dbgIoPortsAddPort(ioPorts, 0, 0xf4, DBG_IO_READWRITE, read(rm, 0xe4));
}

int romMapperF4deviceCreate(int inverted) 
{
    DeviceCallbacks callbacks = { destroy, reset, saveState, loadState };
    DebugCallbacks dbgCallbacks = { getDebugInfo, NULL, NULL, NULL };
    RomMapperF4device* rm = malloc(sizeof(RomMapperF4device));

    rm->inverted   = inverted;
    rm->deviceHandle = deviceManagerRegister(inverted ? ROM_F4INVERTED : ROM_F4DEVICE, &callbacks, rm);
    rm->debugHandle = debugDeviceRegister(DBGTYPE_BIOS, langDbgDevF4Device(), &dbgCallbacks, rm);

    ioPortRegister(0xf4, read, write, rm);

    reset(rm);

    return 1;
}
