/*
** nester - NES emulator
** Copyright (C) 2000  Darren Ranalli
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful, 
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
** Library General Public License for more details.  To obtain a 
** copy of the GNU Library General Public License, write to the Free 
** Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** Any permitted reproduction of these routines, in whole or in part,
** must bear this legend.
*/

/*
** changes for NesterDC:
** changes are marked with KAF
** 
** added #include libc.h, stdlib.h
** commented out #include <stdio.h>
** type casted NULL values to get them to compile 
**
** - Ken Friece
*/


#include "libc.h" //KAF
#include "stdlib.h" //KAF
#include "NES_6502.h"
#include "NES.h"
//#include <stdio.h> // KAF
#include "debug.h"

// NOT SAFE FOR MULTIPLE NES_6502'S
static NES_6502 *NES_6502_nes = (NES_6502 *)NULL; //KAF

void NES_write(uint32 address, uint8 value)
{
  NES_6502_nes->MemoryWrite(address, value);
}

uint8 NES_read(uint32 address)
{
  return NES_6502_nes->MemoryRead(address);
}

static nes6502_memread NESReadHandler[] =
{
   /* $0 - $7FF is RAM */
   { 0x0800, 0xFFFF, NES_read },
   { -1,     -1,     (uint8 (*)(long unsigned int))NULL } //KAF
};

static nes6502_memwrite NESWriteHandler[] =
{
   /* $0 - $7FF is RAM */
   { 0x0800, 0xFFFF, NES_write },
   { -1,     -1,     (void (*)(long unsigned int, unsigned char))NULL} //KAF
};


NES_6502::NES_6502(NES* parent) : ParentNES(parent)
{
  if(NES_6502_nes) throw "error: multiple NES_6502's";

  try {
    NES_6502_nes = this;

    Init();
  } catch(...) {
    NES_6502_nes = (NES_6502 *)NULL; //KAF
    throw;
  }
}

NES_6502::~NES_6502()
{
  NES_6502_nes = (NES_6502 *)NULL; //KAF
}

// Context get/set
void NES_6502::SetContext(Context *cpu)
{
  ASSERT(0x00000000 == (cpu->pc_reg & 0xFFFF0000));
  cpu->read_handler = NESReadHandler;
  cpu->write_handler = NESWriteHandler;
  nes6502_setcontext(cpu);
}

void NES_6502::GetContext(Context *cpu)
{
  nes6502_getcontext(cpu);
  cpu->read_handler = NESReadHandler;
  cpu->write_handler = NESWriteHandler;
}

uint8 NES_6502::MemoryRead(uint32 addr)
{
  return ParentNES->MemoryRead(addr);
}

void NES_6502::MemoryWrite(uint32 addr, uint8 data)
{
  ParentNES->MemoryWrite(addr, data);
}
