/Nes/Mappers/Mapper051.cs

http://mynes.codeplex.com · C# · 113 lines · 90 code · 3 blank · 20 comment · 5 complexity · c63aa395b95ccdbd000f11cee02c7fb9 MD5 · raw file

  1. /*********************************************************************\
  2. *This file is part of My Nes *
  3. *A Nintendo Entertainment System Emulator. *
  4. * *
  5. *Copyright © Ala Hadid 2009 - 2011 *
  6. *E-mail: mailto:ahdsoftwares@hotmail.com *
  7. * *
  8. *My Nes is free software: you can redistribute it and/or modify *
  9. *it under the terms of the GNU General Public License as published by *
  10. *the Free Software Foundation, either version 3 of the License, or *
  11. *(at your option) any later version. *
  12. * *
  13. *My Nes is distributed in the hope that it will be useful, *
  14. *but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. *GNU General Public License for more details. *
  17. * *
  18. *You should have received a copy of the GNU General Public License *
  19. *along with this program. If not, see <http://www.gnu.org/licenses/>.*
  20. \*********************************************************************/
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Text;
  25. namespace MyNes.Nes
  26. {
  27. class Mapper51:Mapper
  28. {
  29. int bank = 0;
  30. int mode = 1;
  31. public Mapper51(NesSystem nes) : base(nes) { }
  32. public override void Poke(int addr, byte data)
  33. {
  34. if (addr < 08000)
  35. {
  36. mode = ((data & 0x10) >> 3) | ((data & 0x02) >> 1);
  37. SetBank_CPU();
  38. }
  39. else
  40. {
  41. bank = (data & 0x0f) << 2;
  42. if (0xC000 <= addr && addr <= 0xDFFF)
  43. {
  44. mode = (mode & 0x01) | ((data & 0x10) >> 3);
  45. }
  46. SetBank_CPU();
  47. }
  48. base.Poke(addr, data);
  49. }
  50. protected override void Initialize(bool initializing)
  51. {
  52. cpuMemory.Map(0x6000, 0x7FFF, Poke);
  53. SetBank_CPU();
  54. if (cartridge.HasCharRam)
  55. cpuMemory.FillChr(16);
  56. cpuMemory.Switch8kChrRom(0);
  57. base.Initialize(initializing);
  58. }
  59. void SetBank_CPU()
  60. {
  61. switch (mode)
  62. {
  63. case 0:
  64. cartridge.Mirroring = Mirroring.ModeVert;
  65. cpuMemory.Switch8kPrgRomToSRAM((bank | 0x2c | 3) * 2);
  66. cpuMemory.Switch8kPrgRom((bank | 0x00 | 0) * 2, 0);
  67. cpuMemory.Switch8kPrgRom((bank | 0x00 | 1) * 2, 1);
  68. cpuMemory.Switch8kPrgRom((bank | 0x0c | 2) * 2, 2);
  69. cpuMemory.Switch8kPrgRom((bank | 0x0c | 3) * 2, 3);
  70. break;
  71. case 1:
  72. cartridge.Mirroring = Mirroring.ModeVert;
  73. cpuMemory.Switch8kPrgRomToSRAM((bank | 0x20 | 3) * 2);
  74. cpuMemory.Switch8kPrgRom((bank | 0x00 | 0) * 2, 0);
  75. cpuMemory.Switch8kPrgRom((bank | 0x00 | 1) * 2, 1);
  76. cpuMemory.Switch8kPrgRom((bank | 0x00 | 2) * 2, 2);
  77. cpuMemory.Switch8kPrgRom((bank | 0x00 | 3) * 2, 3);
  78. break;
  79. case 2:
  80. cartridge.Mirroring = Mirroring.ModeVert;
  81. cpuMemory.Switch8kPrgRomToSRAM((bank | 0x2e | 3) * 2);
  82. cpuMemory.Switch8kPrgRom((bank | 0x02 | 0) * 2, 0);
  83. cpuMemory.Switch8kPrgRom((bank | 0x02 | 1) * 2, 1);
  84. cpuMemory.Switch8kPrgRom((bank | 0x0e | 2) * 2, 2);
  85. cpuMemory.Switch8kPrgRom((bank | 0x0e | 3) * 2, 3);
  86. break;
  87. case 3:
  88. cartridge.Mirroring = Mirroring.ModeHorz;
  89. cpuMemory.Switch8kPrgRomToSRAM((bank | 0x20 | 3) * 2);
  90. cpuMemory.Switch8kPrgRom((bank | 0x00 | 0) * 2, 0);
  91. cpuMemory.Switch8kPrgRom((bank | 0x00 | 1) * 2, 1);
  92. cpuMemory.Switch8kPrgRom((bank | 0x00 | 2) * 2, 2);
  93. cpuMemory.Switch8kPrgRom((bank | 0x00 | 3) * 2, 3);
  94. break;
  95. }
  96. }
  97. public override void SaveState(StateStream stateStream)
  98. {
  99. stateStream.Write(bank);
  100. stateStream.Write(mode);
  101. base.SaveState(stateStream);
  102. }
  103. public override void LoadState(StateStream stateStream)
  104. {
  105. bank = stateStream.ReadInt32();
  106. mode = stateStream.ReadInt32();
  107. base.LoadState(stateStream);
  108. }
  109. }
  110. }