1 module nes.mapper7;
2 
3 import std.conv;
4 import std.format;
5 
6 import nes.cartridge;
7 import nes.mapper;
8 import nes.memory;
9 
10 class Mapper7 : Mapper {
11     this(Cartridge cartridge) {
12         this.cart = cartridge;
13         this.prgBank = 0;
14     }
15 
16     void step() {
17     }
18 
19     ubyte read(ushort address) {
20         if (address < 0x2000) {
21             return this.cart.chr[address];
22         }
23         else if (address >= 0x8000) {
24             auto index = this.prgBank * 0x8000 + cast(int)(address - 0x8000);
25             return this.cart.prg[index];
26         }
27         else if (address >= 0x6000) {
28             auto index = cast(int)address - 0x6000;
29             return this.cart.sram[index];
30         }
31         else {
32             throw new MapperException(format("unhandled mapper7 read at address: 0x%04X", address));
33         }
34     }
35 
36     void write(ushort address, ubyte value) {
37         if (address < 0x2000) {
38             this.cart.chr[address] = value;
39         }
40         else if (address >= 0x8000) {
41             this.prgBank = cast(int)(value & 7);
42             switch (value & 0x10) {
43                 case 0x00:
44                     this.cart.mirror = MirrorSingle0;
45                     break;
46                 case 0x10:
47                     this.cart.mirror = MirrorSingle1;
48                     break;
49                 default:
50                     break;
51             }
52         }
53         else if (address >= 0x6000) {
54             auto index = cast(int)address - 0x6000;
55             this.cart.sram[index] = value;
56         }
57         else {
58             throw new MapperException(format("unhandled mapper7 write at address: 0x%04X", address));
59         }
60     }
61 
62     void save(string[string] state) {
63         state["mapper7.prgBank"] = to!string(this.prgBank);
64     }
65 
66     void load(string[string] state) {
67         this.prgBank = to!int(state["mapper7.prgBank"]);
68     }
69 
70     private:
71         Cartridge cart;
72         int prgBank;
73 }