bsx_cart.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifdef BSX_CPP
  2. void BSXCart::init() {
  3. }
  4. void BSXCart::enable() {
  5. for(uint16 i = 0x5000; i <= 0x5fff; i++) memory::mmio.map(i, *this);
  6. }
  7. void BSXCart::power() {
  8. reset();
  9. }
  10. void BSXCart::reset() {
  11. for(unsigned i = 0; i < 16; i++) regs.r[i] = 0x00;
  12. regs.r[0x07] = 0x80;
  13. regs.r[0x08] = 0x80;
  14. update_memory_map();
  15. }
  16. void BSXCart::update_memory_map() {
  17. Memory &cart = (regs.r[0x01] & 0x80) == 0x00 ? (Memory&)bsxflash : (Memory&)psram;
  18. if((regs.r[0x02] & 0x80) == 0x00) {
  19. //LoROM mapping
  20. bus.map(Bus::MapLinear, 0x00, 0x7d, 0x8000, 0xffff, cart);
  21. bus.map(Bus::MapLinear, 0x80, 0xff, 0x8000, 0xffff, cart);
  22. } else {
  23. //HiROM mapping
  24. bus.map(Bus::MapShadow, 0x00, 0x3f, 0x8000, 0xffff, cart);
  25. bus.map(Bus::MapLinear, 0x40, 0x7d, 0x0000, 0xffff, cart);
  26. bus.map(Bus::MapShadow, 0x80, 0xbf, 0x8000, 0xffff, cart);
  27. bus.map(Bus::MapLinear, 0xc0, 0xff, 0x0000, 0xffff, cart);
  28. }
  29. if(regs.r[0x03] & 0x80) {
  30. bus.map(Bus::MapLinear, 0x60, 0x6f, 0x0000, 0xffff, psram);
  31. //bus.map(Bus::MapLinear, 0x70, 0x77, 0x0000, 0xffff, psram);
  32. }
  33. if((regs.r[0x05] & 0x80) == 0x00) {
  34. bus.map(Bus::MapLinear, 0x40, 0x4f, 0x0000, 0xffff, psram);
  35. }
  36. if((regs.r[0x06] & 0x80) == 0x00) {
  37. bus.map(Bus::MapLinear, 0x50, 0x5f, 0x0000, 0xffff, psram);
  38. }
  39. if(regs.r[0x07] & 0x80) {
  40. bus.map(Bus::MapLinear, 0x00, 0x1f, 0x8000, 0xffff, memory::cartrom);
  41. }
  42. if(regs.r[0x08] & 0x80) {
  43. bus.map(Bus::MapLinear, 0x80, 0x9f, 0x8000, 0xffff, memory::cartrom);
  44. }
  45. bus.map(Bus::MapShadow, 0x20, 0x3f, 0x6000, 0x7fff, psram);
  46. bus.map(Bus::MapLinear, 0x70, 0x77, 0x0000, 0xffff, psram);
  47. }
  48. uint8 BSXCart::mmio_read(unsigned addr) {
  49. if((addr & 0xf0ffff) == 0x005000) { //$[00-0f]:5000 MMIO
  50. uint8 n = (addr >> 16) & 15;
  51. return regs.r[n];
  52. }
  53. if((addr & 0xf8f000) == 0x105000) { //$[10-17]:[5000-5fff] SRAM
  54. return sram.read(((addr >> 16) & 7) * 0x1000 + (addr & 0xfff));
  55. }
  56. return 0x00;
  57. }
  58. void BSXCart::mmio_write(unsigned addr, uint8 data) {
  59. if((addr & 0xf0ffff) == 0x005000) { //$[00-0f]:5000 MMIO
  60. uint8 n = (addr >> 16) & 15;
  61. regs.r[n] = data;
  62. if(n == 0x0e && data & 0x80) update_memory_map();
  63. return;
  64. }
  65. if((addr & 0xf8f000) == 0x105000) { //$[10-17]:[5000-5fff] SRAM
  66. return sram.write(((addr >> 16) & 7) * 0x1000 + (addr & 0xfff), data);
  67. }
  68. }
  69. BSXCart::BSXCart() {
  70. sram_data = new uint8_t[ 32 * 1024];
  71. psram_data = new uint8_t[512 * 1024];
  72. sram.map (sram_data, 32 * 1024);
  73. psram.map(psram_data, 512 * 1024);
  74. }
  75. BSXCart::~BSXCart() {
  76. delete[] sram_data;
  77. delete[] psram_data;
  78. }
  79. #endif