M6502.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /** M6502: portable 6502 emulator ****************************/
  2. /** **/
  3. /** M6502.h **/
  4. /** **/
  5. /** This file contains declarations relevant to emulation **/
  6. /** of 6502 CPU. **/
  7. /** **/
  8. /** Copyright (C) Marat Fayzullin 1996-2002 **/
  9. /** Alex Krasivsky 1996 **/
  10. /** You are not allowed to distribute this software **/
  11. /** commercially. Please, notify me, if you make any **/
  12. /** changes to this file. **/
  13. /*************************************************************/
  14. /*
  15. * $LastChangedDate: 2007-04-23 18:55:35 +0200 (lun, 23 avr 2007) $
  16. * $Author: mtrapier $
  17. * $HeadURL: file:///media/HD6G/SVNROOT/trunk/TI-NESulator/src/M6502.h $
  18. * $Revision: 45 $
  19. */
  20. #ifndef M6502_H
  21. #define M6502_H
  22. /* Loop6502() returns: */
  23. #define INT_NONE 0 /* No interrupt required */
  24. #define INT_IRQ 1 /* Standard IRQ interrupt */
  25. #define INT_NMI 2 /* Non-maskable interrupt */
  26. #define INT_QUIT 3 /* Exit the emulation */
  27. /* 6502 status flags: */
  28. #define C_FLAG 0x01 /* 1: Carry occured */
  29. #define Z_FLAG 0x02 /* 1: Result is zero */
  30. #define I_FLAG 0x04 /* 1: Interrupts disabled */
  31. #define D_FLAG 0x08 /* 1: Decimal mode */
  32. #define B_FLAG 0x10 /* Break [0 on stk after int] */
  33. #define R_FLAG 0x20 /* Always 1 */
  34. #define V_FLAG 0x40 /* 1: Overflow occured */
  35. #define N_FLAG 0x80 /* 1: Result is negative */
  36. /** Simple Datatypes *****************************************/
  37. /** NOTICE: sizeof(byte)=1 and sizeof(word)=2 **/
  38. /*************************************************************/
  39. #ifndef BYTE_TYPE_DEFINED
  40. #define BYTE_TYPE_DEFINED
  41. typedef unsigned char byte;
  42. #endif
  43. #ifndef WORD_TYPE_DEFINED
  44. #define WORD_TYPE_DEFINED
  45. typedef unsigned short word;
  46. #endif
  47. typedef signed char offset;
  48. /** Structured Datatypes *************************************/
  49. /** NOTICE: #define LSB_FIRST for machines where least **/
  50. /** signifcant byte goes first. **/
  51. /*************************************************************/
  52. typedef union
  53. {
  54. #ifdef LSB_FIRST
  55. struct { byte l,h; } B;
  56. #else
  57. struct { byte h,l; } B;
  58. #endif
  59. word W;
  60. } pair;
  61. typedef struct
  62. {
  63. byte A,P,X,Y,S; /* CPU registers and program counter */
  64. pair PC;
  65. int IPeriod,ICount; /* Set IPeriod to number of CPU cycles */
  66. /* between calls to Loop6502() */
  67. byte IRequest; /* Set to the INT_IRQ when pending IRQ */
  68. byte AfterCLI; /* Private, don't touch */
  69. int IBackup; /* Private, don't touch */
  70. byte IAutoReset; /* Set to 1 to autom. reset IRequest */
  71. byte TrapBadOps; /* Set to 1 to warn of illegal opcodes */
  72. word Trap; /* Set Trap to address to trace from */
  73. byte Trace; /* Set Trace=1 to start tracing */
  74. void *User; /* Arbitrary user data (ID,RAM*,etc.) */
  75. } M6502;
  76. /** Reset6502() **********************************************/
  77. /** This function can be used to reset the registers before **/
  78. /** starting execution with Run6502(). It sets registers to **/
  79. /** their initial values. **/
  80. /*************************************************************/
  81. void Reset6502(register M6502 *R);
  82. /** Exec6502() ***********************************************/
  83. /** This function will execute a single 6502 opcode. It **/
  84. /** will then return next PC, and current register values **/
  85. /** in R. **/
  86. /*************************************************************/
  87. word Exec6502(register M6502 *R);
  88. /** Int6502() ************************************************/
  89. /** This function will generate interrupt of a given type. **/
  90. /** INT_NMI will cause a non-maskable interrupt. INT_IRQ **/
  91. /** will cause a normal interrupt, unless I_FLAG set in R. **/
  92. /*************************************************************/
  93. void Int6502(register M6502 *R,register byte Type);
  94. /** Run6502() ************************************************/
  95. /** This function will run 6502 code until Loop6502() call **/
  96. /** returns INT_QUIT. It will return the PC at which **/
  97. /** emulation stopped, and current register values in R. **/
  98. /*************************************************************/
  99. word Run6502(register M6502 *R);
  100. /** Rd6502()/Wr6502/Op6502() *********************************/
  101. /** These functions are called when access to RAM occurs. **/
  102. /** They allow to control memory access. Op6502 is the same **/
  103. /** as Rd6502, but used to read *opcodes* only, when many **/
  104. /** checks can be skipped to make it fast. It is only **/
  105. /** required if there is a #define FAST_RDOP. **/
  106. /************************************ TO BE WRITTEN BY USER **/
  107. void Wr6502(register word Addr,register byte Value);
  108. byte Rd6502(register word Addr);
  109. byte Op6502(register word Addr);
  110. /** Debug6502() **********************************************/
  111. /** This function should exist if DEBUG is #defined. When **/
  112. /** Trace!=0, it is called after each command executed by **/
  113. /** the CPU, and given the 6502 registers. Emulation exits **/
  114. /** if Debug6502() returns 0. **/
  115. /*************************************************************/
  116. byte Debug6502(register M6502 *R);
  117. /** Loop6502() ***********************************************/
  118. /** 6502 emulation calls this function periodically to **/
  119. /** check if the system hardware requires any interrupts. **/
  120. /** This function must return one of following values: **/
  121. /** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/
  122. /** emulation loop. **/
  123. /************************************ TO BE WRITTEN BY USER **/
  124. byte Loop6502(register M6502 *R);
  125. /** Patch6502() **********************************************/
  126. /** Emulation calls this function when it encounters an **/
  127. /** unknown opcode. This can be used to patch the code to **/
  128. /** emulate BIOS calls, such as disk and tape access. The **/
  129. /** function should return 1 if the exception was handled, **/
  130. /** or 0 if the opcode was truly illegal. **/
  131. /************************************ TO BE WRITTEN BY USER **/
  132. byte Patch6502(register byte Op,register M6502 *R);
  133. #endif /* M6502_H */