NstIoLine.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Nestopia - NES/Famicom emulator written in C++
  4. //
  5. // Copyright (C) 2003-2008 Martin Freij
  6. //
  7. // This file is part of Nestopia.
  8. //
  9. // Nestopia is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation; either version 2 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // Nestopia is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with Nestopia; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. //
  23. ////////////////////////////////////////////////////////////////////////////////////////
  24. #ifndef NST_IO_LINE_H
  25. #define NST_IO_LINE_H
  26. #ifdef NST_PRAGMA_ONCE
  27. #pragma once
  28. #endif
  29. namespace Nes
  30. {
  31. namespace Core
  32. {
  33. namespace Io
  34. {
  35. #ifdef NST_FASTDELEGATE
  36. class Line : public ImplicitBool<Line>
  37. {
  38. class Component {};
  39. typedef void (NST_FASTCALL Component::*Toggler)(Address,Cycle);
  40. Component* component;
  41. Toggler toggler;
  42. NST_COMPILE_ASSERT( sizeof(Toggler) <= sizeof(void (*)(void*,Address,Cycle)) );
  43. public:
  44. Line() {}
  45. template<typename T>
  46. Line(T* c,void (NST_FASTCALL T::*t)(Address,Cycle))
  47. :
  48. component ( reinterpret_cast<Component*>(c) ),
  49. toggler ( reinterpret_cast<Toggler>(t) )
  50. {
  51. NST_COMPILE_ASSERT( sizeof(toggler) == sizeof(t) );
  52. }
  53. template<typename T>
  54. void Set(T* c,void (NST_FASTCALL T::*t)(Address,Cycle))
  55. {
  56. NST_COMPILE_ASSERT( sizeof(toggler) == sizeof(t) );
  57. component = reinterpret_cast<Component*>(c);
  58. toggler = reinterpret_cast<Toggler>(t);
  59. }
  60. void Unset()
  61. {
  62. component = NULL;
  63. toggler = NULL;
  64. }
  65. bool operator ! () const
  66. {
  67. return component == NULL;
  68. }
  69. void Toggle(Address address,Cycle cycle) const
  70. {
  71. (*component.*toggler)( address, cycle );
  72. }
  73. };
  74. #define NES_DECL_LINE(a_) void NST_FASTCALL Line_##a_(Address,Cycle)
  75. #define NES_LINE(o_,a_) void NST_FASTCALL o_::Line_##a_(Address address,Cycle cycle)
  76. #define NES_LINE_T(t_,o_,a_) t_ void NST_FASTCALL o_::Line_##a_(Address address,Cycle cycle)
  77. #else
  78. class Line : public ImplicitBool<Line>
  79. {
  80. typedef void* Component;
  81. typedef void (NST_REGCALL *Toggler)(Component,Address,Cycle);
  82. Component component;
  83. Toggler toggler;
  84. public:
  85. Line() {}
  86. Line(Component c,Toggler t)
  87. :
  88. component ( c ),
  89. toggler ( t )
  90. {}
  91. void Set(Component c,Toggler t)
  92. {
  93. component = c;
  94. toggler = t;
  95. }
  96. void Unset()
  97. {
  98. component = NULL;
  99. toggler = NULL;
  100. }
  101. bool operator ! () const
  102. {
  103. return component == NULL;
  104. }
  105. void Toggle(Address address,Cycle cycle) const
  106. {
  107. toggler( component, address, cycle );
  108. }
  109. };
  110. #define NES_DECL_LINE(a_) \
  111. \
  112. NST_FORCE_INLINE void NST_FASTCALL Line_M_##a_(Address,Cycle); \
  113. static NST_NO_INLINE void NST_REGCALL Line_##a_(void*,Address,Cycle)
  114. #define NES_LINE(o_,a_) \
  115. \
  116. NST_NO_INLINE void NST_REGCALL o_::Line_##a_(void* p_,Address a_,Cycle c_) \
  117. { \
  118. static_cast<o_*>(p_)->Line_M_##a_(a_,c_); \
  119. } \
  120. \
  121. NST_FORCE_INLINE void NST_FASTCALL o_::Line_M_##a_(Address address,Cycle cycle)
  122. #define NES_LINE_T(t_,o_,a_) \
  123. \
  124. t_ NST_NO_INLINE void NST_REGCALL o_::Line_##a_(void* p_,Address a_,Cycle c_) \
  125. { \
  126. static_cast<o_*>(p_)->Line_M_##a_(a_,c_); \
  127. } \
  128. \
  129. t_ NST_FORCE_INLINE void NST_FASTCALL o_::Line_M_##a_(Address address,Cycle cycle)
  130. #endif
  131. }
  132. }
  133. }
  134. #endif