NESe (pronounced "Nessie") is a NES emulator based on the e6502 emulator, also written in C with a focus on speed and portability for use on embedded platforms, especially ARM.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.8KB

  1. #ifndef NES_MAPPER_H_
  2. #define NES_MAPPER_H_
  3. #include <stdint.h>
  4. #ifdef DEBUG_MAPPER
  5. #define MAP_LOG(fmt, ...) printf("MAP: " fmt "\n" __VA_OPT__(,) __VA_ARGS__)
  6. #else
  7. #define MAP_LOG(...) do {} while (0)
  8. #endif
  9. struct nes_cart_t;
  10. typedef struct nes_mapper_t {
  11. void* data;
  12. int (*init)(struct nes_mapper_t*, struct nes_cart_t* cart);
  13. void (*reset)(struct nes_mapper_t*);
  14. void (*done)(struct nes_mapper_t*);
  15. uint8_t (*read)(struct nes_mapper_t*, uint16_t addr);
  16. void (*write)(struct nes_mapper_t*, uint16_t addr, uint8_t val);
  17. uint8_t* (*chr_addr)(struct nes_mapper_t*, uint16_t addr);
  18. uint8_t* (*vram_addr)(struct nes_mapper_t*, uint16_t addr);
  19. void (*chr_write)(struct nes_mapper_t*, uint16_t addr, uint8_t val);
  20. void (*scanline)(struct nes_mapper_t*);
  21. void (*irq_callback)(void*, int);
  22. void* irq_arg;
  23. } nes_mapper;
  24. static inline int nes_map_init(nes_mapper* map,
  25. struct nes_cart_t* cart) {
  26. return map->init(map, cart);
  27. }
  28. static inline void nes_map_reset(nes_mapper* map) {
  29. map->reset(map);
  30. }
  31. static inline void nes_map_done(nes_mapper* map) {
  32. map->done(map);
  33. }
  34. static inline void nes_map_set_irq(nes_mapper* map,
  35. void(*callback)(void*, int),
  36. void* arg) {
  37. map->irq_callback = callback;
  38. map->irq_arg = arg;
  39. }
  40. static inline void nes_map_trigger_irq(nes_mapper* map,
  41. int active) {
  42. map->irq_callback(map->irq_arg, active);
  43. }
  44. static inline uint8_t nes_map_read(nes_mapper* map,
  45. uint16_t addr) {
  46. return map->read(map, addr);
  47. }
  48. static inline void nes_map_write(nes_mapper* map,
  49. uint16_t addr,
  50. uint8_t val) {
  51. map->write(map, addr, val);
  52. }
  53. static inline uint8_t* nes_map_chr_addr(nes_mapper* map,
  54. uint16_t addr) {
  55. return map->chr_addr(map, addr);
  56. }
  57. static inline uint8_t nes_chr_read(nes_mapper* map,
  58. uint16_t addr) {
  59. return *(nes_map_chr_addr(map, addr));
  60. }
  61. static inline void nes_chr_write(nes_mapper* map,
  62. uint16_t addr, uint8_t val) {
  63. return map->chr_write(map, addr, val);
  64. }
  65. static inline uint8_t* nes_map_vram_addr(nes_mapper* map,
  66. uint16_t addr) {
  67. return map->vram_addr(map, addr & 0x1FFFU);
  68. }
  69. static inline uint8_t nes_vram_read(nes_mapper* map,
  70. uint16_t addr) {
  71. return *(nes_map_vram_addr(map, addr));
  72. }
  73. static inline void nes_vram_write(nes_mapper* map,
  74. uint16_t addr, uint8_t val) {
  75. *(nes_map_vram_addr(map, addr)) = val;
  76. }
  77. extern nes_mapper* nes_mappers[];
  78. #endif