forked from macifom/macifom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNESVRC2bCartridge.m
151 lines (117 loc) · 4.84 KB
/
NESVRC2bCartridge.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// NESVRC2bCartridge.m
// Macifom
//
// Created by Auston Stewart on 9/23/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "NESVRC2bCartridge.h"
#import "NESPPUEmulator.h"
@implementation NESVRC2bCartridge
- (void)_switch1KBCHRROMBank:(uint_fast32_t)bank toBank:(uint_fast32_t)index
{
uint_fast32_t bankCounter;
uint_fast32_t selected1KBBank = (index & _chrromIndexMask) * BANK_SIZE_1KB / CHRROM_BANK_SIZE;
// Rebuild CHRROM indices
for (bankCounter = 0; bankCounter < (BANK_SIZE_1KB / CHRROM_BANK_SIZE); bankCounter++) {
_chrromBankIndices[bankCounter + (bank * BANK_SIZE_1KB / CHRROM_BANK_SIZE)] = selected1KBBank + bankCounter;
}
}
- (void)_switch8KBPRGROMBank:(uint_fast32_t)bank toBank:(uint_fast32_t)index
{
uint_fast32_t bankCounter;
uint_fast32_t selected8KBBank = (index & _prgromIndexMask) * (BANK_SIZE_8KB / PRGROM_BANK_SIZE);
// Establish PRGROM indices
for (bankCounter = 0; bankCounter < (BANK_SIZE_8KB / PRGROM_BANK_SIZE); bankCounter++) {
_prgromBankIndices[bankCounter + (bank * BANK_SIZE_8KB / PRGROM_BANK_SIZE)] = selected8KBBank + bankCounter;
}
}
- (void)_switchVRC2CHRROMBankWithByte:(uint8_t)byte andCPUAddress:(uint16_t)address
{
uint8_t chrromBankToSwitch = (((address - 0xB000) >> 12) * 2) + ((address & 0x2) >> 1);
if (address & 0x1) {
// High nibble
_vrc2CHRROMBankIndices[chrromBankToSwitch] = (_vrc2CHRROMBankIndices[chrromBankToSwitch] & 0xF) | ((byte & 0xF) << 4);
}
else {
// Low nibble
_vrc2CHRROMBankIndices[chrromBankToSwitch] = (_vrc2CHRROMBankIndices[chrromBankToSwitch] & 0xF0) | (byte & 0xF);
}
[self _switch1KBCHRROMBank:chrromBankToSwitch toBank:_vrc2CHRROMBankIndices[chrromBankToSwitch]];
}
- (void)writeByte:(uint8_t)byte toPRGROMwithCPUAddress:(uint16_t)address onCycle:(uint_fast32_t)cycle
{
if (address < 0x9000) {
if (address < 0x8004) {
// $8000-$8003: [.... PPPP] PRG Reg 0 (select 8k @ $8000)
[self _switch8KBPRGROMBank:0 toBank:byte];
[self rebuildPRGROMPointers];
}
}
else if (address < 0xA000) {
if (address < 0x9004) {
/* $9000-$9003: [.... ..MM] Mirroring:
%00 = Vert
%01 = Horz
%10 = 1ScA
%11 = 1ScB
*/
// Change mirroring
switch (byte & 0x3) {
case 0:
[_ppu changeMirroringTypeTo:NESVerticalMirroring onCycle:cycle];
break;
case 1:
[_ppu changeMirroringTypeTo:NESHorizontalMirroring onCycle:cycle];
break;
case 2:
[_ppu changeMirroringTypeTo:NESSingleScreenLowerMirroring onCycle:cycle];
break;
case 3:
[_ppu changeMirroringTypeTo:NESSingleScreenUpperMirroring onCycle:cycle];
break;
default:
break;
}
}
}
else if (address < 0xB000) {
if (address < 0xA004) {
// $A000-$A003: [.... PPPP] PRG Reg 1 (select 8k @ $A000)
[self _switch8KBPRGROMBank:1 toBank:byte];
[self rebuildPRGROMPointers];
}
}
else {
// $B000-$E003: [.... CCCC] CHR Regs (see CHR Setup)
[_ppu runPPUUntilCPUCycle:cycle];
[self _switchVRC2CHRROMBankWithByte:byte andCPUAddress:address];
[self rebuildCHRROMPointers];
}
}
- (void)setInitialROMPointers
{
uint_fast32_t bankCounter;
uint_fast32_t secondToLast8KBBankIndex = (_iNesFlags->prgromSize - BANK_SIZE_16KB) / PRGROM_BANK_SIZE;
// Establish PRGROM pointers for the first two 8KB banks, which are swappable
for (bankCounter = 0; bankCounter < (BANK_SIZE_16KB / PRGROM_BANK_SIZE); bankCounter++) {
_prgromBankIndices[bankCounter] = bankCounter;
}
// Establish PRGROM pointers for the last two 8KB banks, which are fixed
for (bankCounter = 0; bankCounter < (BANK_SIZE_16KB / PRGROM_BANK_SIZE); bankCounter++) {
_prgromBankIndices[bankCounter + (BANK_SIZE_16KB / PRGROM_BANK_SIZE)] = secondToLast8KBBankIndex + bankCounter;
}
[self rebuildPRGROMPointers];
// Establish Initial VRC2 CHRROM Index Registers
for (bankCounter = 0; bankCounter < (CHRROM_APERTURE_SIZE / BANK_SIZE_1KB); bankCounter++) {
_vrc2CHRROMBankIndices[bankCounter] = bankCounter;
}
// Establish CHRROM pointers
for (bankCounter = 0; bankCounter < (CHRROM_APERTURE_SIZE / CHRROM_BANK_SIZE); bankCounter++) {
_chrromBankIndices[bankCounter] = bankCounter;
}
[self rebuildCHRROMPointers];
_prgromIndexMask = (_iNesFlags->prgromSize / BANK_SIZE_8KB) - 1;
_chrromIndexMask = (_iNesFlags->chrromSize / BANK_SIZE_1KB) - 1;
}
@end