nes_cpu.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2022 Dozingfiretruck
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifndef _NES_CPU_
  25. #define _NES_CPU_
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. // https://www.nesdev.org/wiki/Controller_reading
  30. #define NES_CPU_RAM_SIZE 0x800 /* 2KB */
  31. struct nes;
  32. typedef struct nes nes_t;
  33. #define NES_VERCTOR_NMI 0xFFFA /* NMI vector (NMI=not maskable interupts) */
  34. #define NES_VERCTOR_RESET 0xFFFC /* Reset vector */
  35. #define NES_VERCTOR_IRQBRK 0xFFFE /* IRQ vector */
  36. /*
  37. Bit No. 15 14 13 12 11 10 9 8
  38. A1 B1 Select1 Start1 Up1 Down1 Left1 Right1
  39. Bit No. 7 6 5 4 3 2 1 0
  40. A2 B2 Select2 Start2 Up2 Down2 Left2 Right2
  41. */
  42. typedef struct nes_joypad{
  43. uint8_t offset1;
  44. uint8_t offset2;
  45. uint8_t mask;
  46. union {
  47. struct {
  48. uint8_t R2:1;
  49. uint8_t L2:1;
  50. uint8_t D2:1;
  51. uint8_t U2:1;
  52. uint8_t ST2:1;
  53. uint8_t SE2:1;
  54. uint8_t B2:1;
  55. uint8_t A2:1;
  56. uint8_t R1:1;
  57. uint8_t L1:1;
  58. uint8_t D1:1;
  59. uint8_t U1:1;
  60. uint8_t ST1:1;
  61. uint8_t SE1:1;
  62. uint8_t B1:1;
  63. uint8_t A1:1;
  64. };
  65. uint16_t joypad;
  66. };
  67. } nes_joypad_t;
  68. // https://www.nesdev.org/wiki/CPU_registers
  69. typedef struct nes_cpu{
  70. /* CPU registers */
  71. uint8_t A; /* Accumulator */
  72. uint8_t X; /* Indexes X */
  73. uint8_t Y; /* Indexes Y */
  74. uint16_t PC; /* Program Counter */
  75. uint8_t SP; /* Stack Pointer */
  76. union {
  77. struct {
  78. uint8_t C:1; /* carry flag (1 on unsigned overflow) */
  79. uint8_t Z:1; /* zero flag (1 when all bits of a result are 0) */
  80. uint8_t I:1; /* IRQ flag (when 1, no interupts will occur (exceptions are IRQs forced by BRK and NMIs)) */
  81. uint8_t D:1; /* decimal flag (1 when CPU in BCD mode) */
  82. uint8_t B:1; /* break flag (1 when interupt was caused by a BRK) */
  83. uint8_t U:1; /* unused (always 1) */
  84. uint8_t V:1; /* overflow flag (1 on signed overflow) */
  85. uint8_t N:1; /* negative flag (1 when result is negative) */
  86. };
  87. uint8_t P; /* Status Register */
  88. };
  89. uint32_t cycles;
  90. uint8_t opcode;
  91. uint8_t cpu_ram[NES_CPU_RAM_SIZE];
  92. uint8_t* prg_banks[4]; /* 4 bank ( 8Kb * 4 ) = 32KB */
  93. nes_joypad_t joypad;
  94. } nes_cpu_t;
  95. void nes_cpu_init(nes_t *nes);
  96. void nes_cpu_reset(nes_t* nes);
  97. void nes_nmi(nes_t* nes);
  98. void nes_opcode(nes_t* nes,uint16_t ticks);
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif// _NES_CPU_