corecpu.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * CoreCPU - The Quick6502 Project
  3. * corecpu.h
  4. *
  5. * Created by Manoel Trapier on 24/02/08
  6. * Copyright 2008 986 Corp. All rights reserved.
  7. *
  8. * $LastChangedDate$
  9. * $Author$
  10. * $HeadURL$
  11. * $Revision$
  12. *
  13. */
  14. #ifndef _QUICK6502_CORECPU_H_
  15. #define _QUICK6502_CORECPU_H_
  16. /* M6502 configuration
  17. *
  18. * Supported DEFINEs :
  19. * NO_DECIMAL Quick6502 will not support BDC arithemtic (used for NES)
  20. * CMOS_6502 Quick6502 will act as a CMOS 6502 (Not actually used)
  21. *
  22. */
  23. #ifdef CMOS_6502
  24. //#warning Quick6502 CMOS support is actually desactivated, desactivate it
  25. #undef CMOS_6502
  26. #endif
  27. #ifndef NO_DECIMAL
  28. //#warning Quick6502 have actually no BCD support, fallback to no NO_DECIMAL
  29. #define NO_DECIMAL
  30. #endif
  31. #include "types.h"
  32. typedef byte (*quick6502_MemoryReadFunction)(unsigned short addr);
  33. typedef void (*quick6502_MemoryWriteFunction)(unsigned short addr, byte value);
  34. typedef struct quick6502_cpu_
  35. {
  36. /* 6502 registers */
  37. byte reg_A, reg_X, reg_Y;
  38. byte reg_P, reg_S;
  39. unsigned short reg_PC;
  40. /* Read/Write memory functions */
  41. quick6502_MemoryReadFunction memory_read;
  42. quick6502_MemoryWriteFunction memory_write;
  43. quick6502_MemoryReadFunction memory_page0_read;
  44. quick6502_MemoryWriteFunction memory_page0_write;
  45. quick6502_MemoryReadFunction memory_stack_read;
  46. quick6502_MemoryWriteFunction memory_stack_write;
  47. quick6502_MemoryReadFunction memory_opcode_read;
  48. /* Timing related */
  49. long cycle_done;
  50. byte exit_loop;
  51. byte int_pending;
  52. /* Other config options */
  53. byte running; /* This field is used to prevent cpu free if this cpu is running */
  54. byte page_crossed;
  55. } quick6502_cpu;
  56. typedef struct quick6502_cpuconfig_
  57. {
  58. /* Read/Write memory functions */
  59. quick6502_MemoryReadFunction memory_read;
  60. quick6502_MemoryWriteFunction memory_write;
  61. quick6502_MemoryReadFunction memory_page0_read;
  62. quick6502_MemoryWriteFunction memory_page0_write;
  63. quick6502_MemoryReadFunction memory_stack_read;
  64. quick6502_MemoryWriteFunction memory_stack_write;
  65. quick6502_MemoryReadFunction memory_opcode_read;
  66. } quick6502_cpuconfig;
  67. /*** Signal that we can send to the CPU ***/
  68. typedef enum
  69. {
  70. Q6502_NO_SIGNAL = 0,
  71. Q6502_IRQ_SIGNAL,
  72. Q6502_NMI_SIGNAL,
  73. Q6502_STOPLOOP_SIGNAL
  74. } quick6502_signal;
  75. /*** Some 6502 related definitions ***/
  76. /*** P register flags ***/
  77. #define Q6502_N_FLAG 0x80 /* Negavite flag */
  78. #define Q6502_V_FLAG 0x40 /* oVerflow flag */
  79. #define Q6502_R_FLAG 0x20 /* Not a real flag, but need to be to 1 on PHP */
  80. #define Q6502_B_FLAG 0x10 /* Break flag */
  81. #define Q6502_D_FLAG 0x08 /* BCD flag */
  82. #define Q6502_I_FLAG 0x04 /* IRQ/BRK flag */
  83. #define Q6502_Z_FLAG 0x02 /* Zero flag */
  84. #define Q6502_C_FLAG 0x01 /* Carry flag */
  85. /*** Interuption Vectors ***/
  86. #define Q6502_NMI_LOW 0xFFFA
  87. #define Q6502_NMI_HIGH 0xFFFB
  88. #define Q6502_RESET_LOW 0xFFFC
  89. #define Q6502_RESET_HIGH 0xFFFD
  90. #define Q6502_IRQ_LOW 0xFFFE
  91. #define Q6502_IRQ_HIGH 0xFFFF
  92. /**
  93. * Initialise the CPU
  94. *
  95. * Inputs:
  96. *
  97. * - CPU Init structure:
  98. * +- Memory Read function pointer
  99. * +- Memory Write function pointer
  100. * +- Fast memory read function pointer (for opcodes read)
  101. * +- Fast page 0 function / Read/Write
  102. * +- Fast page 1 function / Read/Write
  103. *
  104. * Output:
  105. *
  106. * (void *): An opaque pointer to the internal structure of the CPU
  107. *
  108. */
  109. quick6502_cpu *quick6502_init(quick6502_cpuconfig *config);
  110. /* Reset the CPU (must be done after init) */
  111. void quick6502_reset(quick6502_cpu *cpu);
  112. /**
  113. * Run cpu for at least X cycles
  114. *
  115. * Output:
  116. *
  117. * int: (Number of cycle really done) - (Number of cycle asked)
  118. */
  119. int quick6502_run(quick6502_cpu *cpu, int cycles);
  120. /** Loop CPU until explicit quit */
  121. void quick6502_loop(quick6502_cpu *cpu);
  122. /** Run CPU for one instruction */
  123. void quick6502_exec(quick6502_cpu *cpu);
  124. /** Send IRQ/NMI/EXITLOOP signal to CPU */
  125. void quick6502_int(quick6502_cpu *cpu, quick6502_signal signal);
  126. /** Dump CPU State to the given file */
  127. void quick6502_dump(quick6502_cpu *cpu, FILE * fp);
  128. /** Get current instruction name at specified address and put it into buffer */
  129. void quick6502_getinstruction(quick6502_cpu *cpu, unsigned short addr, char *buffer);
  130. /**
  131. * Free the CPU
  132. *
  133. * This function will free the CPU only if it's not currently used, it will
  134. * return !0 if everything goes well and 0 if the free is impossible
  135. */
  136. int quick6502_free(quick6502_cpu *cpu);
  137. #endif /* _QUICK6502_CORECPU_H_ */