corecpu.h 4.6 KB

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