1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia 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.
//
// Nestopia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
////////////////////////////////////////////////////////////////////////////////////////
#ifndef NST_IO_PORT_H
#define NST_IO_PORT_H
#ifdef NST_PRAGMA_ONCE
#pragma once
#endif
namespace Nes
{
namespace Core
{
namespace Io
{
#ifdef NST_FASTDELEGATE
class Port
{
class Component {};
typedef Data (NST_FASTCALL Component::*Reader)(Address);
typedef void (NST_FASTCALL Component::*Writer)(Address,Data);
Component* component;
Reader reader;
Writer writer;
NST_COMPILE_ASSERT
(
sizeof( Reader ) <= sizeof( Data (*)(void*,Address) ) &&
sizeof( Writer ) <= sizeof( Data (*)(void*,Address,Data) )
);
public:
Port() {}
template<typename T>
Port(T* c,Data (NST_FASTCALL T::*r)(Address),void (NST_FASTCALL T::*w)(Address,Data))
:
component ( reinterpret_cast<Component*>(c) ),
reader ( reinterpret_cast<Reader>(r) ),
writer ( reinterpret_cast<Writer>(w) )
{
NST_COMPILE_ASSERT( sizeof(reader) == sizeof(r) && sizeof(writer) == sizeof(w) );
}
template<typename T>
void Set(T* c,Data (NST_FASTCALL T::*r)(Address),void (NST_FASTCALL T::*w)(Address,Data))
{
NST_COMPILE_ASSERT( sizeof(reader) == sizeof(r) && sizeof(writer) == sizeof(w) );
component = reinterpret_cast<Component*>(c);
reader = reinterpret_cast<Reader>(r);
writer = reinterpret_cast<Writer>(w);
}
template<typename T>
void Set(Data (NST_FASTCALL T::*r)(Address))
{
NST_COMPILE_ASSERT( sizeof(reader) == sizeof(r) );
reader = reinterpret_cast<Reader>(r);
}
template<typename T>
void Set(void (NST_FASTCALL T::*w)(Address,Data))
{
NST_COMPILE_ASSERT( sizeof(writer) == sizeof(w) );
writer = reinterpret_cast<Writer>(w);
}
template<typename T>
void Set(Data (NST_FASTCALL T::*r)(Address),void (NST_FASTCALL T::*w)(Address,Data))
{
NST_COMPILE_ASSERT( sizeof(reader) == sizeof(r) && sizeof(writer) == sizeof(w) );
reader = reinterpret_cast<Reader>(r);
writer = reinterpret_cast<Writer>(w);
}
uint Peek(Address address) const
{
return (*component.*reader)( address );
}
void Poke(Address address,Data data) const
{
(*component.*writer)( address, data );
}
bool operator == (const Port& p) const
{
return component == p.component && reader == p.reader && writer == p.writer;
}
};
#define NES_DECL_PEEK(a_) Data NST_FASTCALL Peek_##a_(Address)
#define NES_DECL_POKE(a_) void NST_FASTCALL Poke_##a_(Address,Data)
#define NES_PEEK(o_,a_) Data NST_FASTCALL o_::Peek_##a_(Address)
#define NES_PEEK_A(o_,a_) Data NST_FASTCALL o_::Peek_##a_(Address address)
#define NES_POKE(o_,a_) void NST_FASTCALL o_::Poke_##a_(Address,Data)
#define NES_POKE_A(o_,a_) void NST_FASTCALL o_::Poke_##a_(Address address,Data)
#define NES_POKE_D(o_,a_) void NST_FASTCALL o_::Poke_##a_(Address,Data data)
#define NES_POKE_AD(o_,a_) void NST_FASTCALL o_::Poke_##a_(Address address,Data data)
#define NES_DO_POKE(a_,p_,d_) Poke_##a_(p_,d_)
#define NES_DO_PEEK(a_,p_) Peek_##a_(p_)
#else
class Port
{
typedef void* Component;
typedef Data (NST_REGCALL *Reader)(Component,Address);
typedef void (NST_REGCALL *Writer)(Component,Address,Data);
Component component;
Reader reader;
Writer writer;
public:
Port() {}
Port(Component c,Reader r,Writer w)
:
component ( c ),
reader ( r ),
writer ( w )
{}
void Set(Component c,Reader r,Writer w)
{
component = c;
reader = r;
writer = w;
}
void Set(Reader r)
{
reader = r;
}
void Set(Writer w)
{
writer = w;
}
void Set(Reader r,Writer w)
{
reader = r;
writer = w;
}
Data Peek(Address address) const
{
return reader( component, address );
}
void Poke(Address address,Data data) const
{
writer( component, address, data );
}
bool operator == (const Port& p) const
{
return component == p.component && reader == p.reader && writer == p.writer;
}
};
#define NES_DECL_PEEK(a_) \
\
NST_SINGLE_CALL Data NST_FASTCALL Peek_M_##a_(Address); \
static Data NST_REGCALL Peek_##a_(void*,Address)
#define NES_DECL_POKE(a_) \
\
NST_SINGLE_CALL void NST_FASTCALL Poke_M_##a_(Address,Data); \
static void NST_REGCALL Poke_##a_(void*,Address,Data)
#define NES_PEEK(o_,a_) \
\
Data NST_REGCALL o_::Peek_##a_(void* p_,Address i_) \
{ \
return static_cast<o_*>(p_)->Peek_M_##a_(i_); \
} \
\
NST_SINGLE_CALL Data NST_FASTCALL o_::Peek_M_##a_(Address)
#define NES_PEEK_A(o_,a_) \
\
Data NST_REGCALL o_::Peek_##a_(void* p_,Address i_) \
{ \
return static_cast<o_*>(p_)->Peek_M_##a_(i_); \
} \
\
NST_SINGLE_CALL Data NST_FASTCALL o_::Peek_M_##a_(Address address)
#define NES_POKE(o_,a_) \
\
void NST_REGCALL o_::Poke_##a_(void* p_,Address i_,Data j_) \
{ \
static_cast<o_*>(p_)->Poke_M_##a_(i_,j_); \
} \
\
NST_SINGLE_CALL void NST_FASTCALL o_::Poke_M_##a_(Address,Data)
#define NES_POKE_A(o_,a_) \
\
void NST_REGCALL o_::Poke_##a_(void* p_,Address i_,Data j_) \
{ \
static_cast<o_*>(p_)->Poke_M_##a_(i_,j_); \
} \
\
NST_SINGLE_CALL void NST_FASTCALL o_::Poke_M_##a_(Address address,Data)
#define NES_POKE_D(o_,a_) \
\
void NST_REGCALL o_::Poke_##a_(void* p_,Address i_,Data j_) \
{ \
static_cast<o_*>(p_)->Poke_M_##a_(i_,j_); \
} \
\
NST_SINGLE_CALL void NST_FASTCALL o_::Poke_M_##a_(Address,Data data)
#define NES_POKE_AD(o_,a_) \
\
void NST_REGCALL o_::Poke_##a_(void* p_,Address i_,Data j_) \
{ \
static_cast<o_*>(p_)->Poke_M_##a_(i_,j_); \
} \
\
NST_SINGLE_CALL void NST_FASTCALL o_::Poke_M_##a_(Address address,Data data)
#define NES_DO_POKE(a_,p_,d_) Poke_##a_(this,p_,d_)
#define NES_DO_PEEK(a_,p_) Peek_##a_(this,p_)
#endif
}
}
}
#endif
|