SayoriOS 0.3.3

mbc.c
1 #include "mbc.h"
2 #include "mem.h"
3 #include "rom.h"
4
5 enum {
6 NO_FILTER_WRITE,
7 FILTER_WRITE
8 };
9
10 static unsigned int bank_upper_bits;
11 static unsigned int ram_select;
12
13 /* Unfinished, no clock etc */
14 unsigned int MBC3_write_byte(unsigned short d, unsigned char i)
15 {
16 int bank;
17
18 if(d < 0x2000)
19 {
20 return FILTER_WRITE;
21 }
22
23 if(d < 0x4000)
24 {
25 bank = i & 0x7F;
26
27 if(bank == 0)
28 bank++;
29
30 mem_bank_switch(bank);
31
32 return FILTER_WRITE;
33 }
34
35 if(d < 0x8000)
36 return FILTER_WRITE;
37
38 return NO_FILTER_WRITE;
39 }
40 unsigned int MBC1_write_byte(unsigned short d, unsigned char i)
41 {
42 int bank;
43
44 if(d < 0x2000)
45 {
46 return FILTER_WRITE;
47 /* TODO: Enable/disable SRAM */
48 }
49
50 /* Switch rom bank at 4000-7fff */
51 if(d >= 0x2000 && d < 0x4000)
52 {
53 /* Bits 0-4 come from the value written to memory here,
54 * bits 5-6 come from a seperate write to 4000-5fff if
55 * RAM select is 1.
56 */
57 bank = i & 0x1F;
58 if(!ram_select)
59 bank |= bank_upper_bits;
60
61 /* "Writing to this address space selects the lower 5 bits of the
62 * ROM Bank Number (in range 01-1Fh). When 00h is written, the MBC
63 * translates that to bank 01h also."
64 * http://nocash.emubase.de/pandocs.htm#mbc1max2mbyteromandor32kbyteram
65 */
66
67 if(bank == 0 || bank == 0x20 || bank == 0x40 || bank == 0x60)
68 bank++;
69
70 mem_bank_switch(bank);
71
72 return FILTER_WRITE;
73 }
74
75 /* Bit 5 and 6 of the bank selection */
76 if(d >= 0x4000 && d < 0x6000)
77 {
78 bank_upper_bits = (i & 0x3)<<5;
79 return FILTER_WRITE;
80 }
81
82 if(d >= 0x6000 && d <= 0x7FFF)
83 {
84 ram_select = i&1;
85 return FILTER_WRITE;
86 }
87 return NO_FILTER_WRITE;
88 }