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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#import "GBMemoryByteArray.h"
#import "GBCompleteByteSlice.h"
@implementation GBMemoryByteArray
{
Document *_document;
}
- (instancetype) initWithDocument:(Document *)document
{
if ((self = [super init])) {
_document = document;
}
return self;
}
- (unsigned long long)length
{
switch (_mode) {
case GBMemoryEntireSpace:
return 0x10000;
case GBMemoryROM:
return 0x8000;
case GBMemoryVRAM:
return 0x2000;
case GBMemoryExternalRAM:
return 0x2000;
case GBMemoryRAM:
return 0x2000;
}
}
- (uint16_t)base
{
switch (_mode) {
case GBMemoryEntireSpace: return 0;
case GBMemoryROM: return 0;
case GBMemoryVRAM: return 0x8000;
case GBMemoryExternalRAM: return 0xA000;
case GBMemoryRAM: return 0xC000;
}
}
- (void)copyBytes:(unsigned char *)dst range:(HFRange)range
{
// Do everything in 0x1000 chunks, never cross a 0x1000 boundary
if ((range.location & 0xF000) != ((range.location + range.length) & 0xF000)) {
size_t partial = 0x1000 - (range.location & 0xFFF);
[self copyBytes:dst + partial range:HFRangeMake(range.location + partial, range.length - partial)];
range.length = partial;
}
range.location += self.base;
GB_gameboy_t *gb = _document.gameboy;
switch (range.location >> 12) {
case 0x0:
case 0x1:
case 0x2:
case 0x3: {
uint16_t bank;
uint8_t *data = GB_get_direct_access(gb, GB_DIRECT_ACCESS_ROM0, NULL, &bank);
memcpy(dst, data + bank * 0x4000 + range.location, range.length);
break;
}
case 0x4:
case 0x5:
case 0x6:
case 0x7: {
uint16_t bank;
size_t size;
uint8_t *data = GB_get_direct_access(gb, GB_DIRECT_ACCESS_ROM, &size, &bank);
if (_mode != GBMemoryEntireSpace) {
bank = self.selectedBank & (size / 0x4000 - 1);
}
memcpy(dst, data + bank * 0x4000 + range.location - 0x4000, range.length);
break;
}
case 0x8:
case 0x9: {
uint16_t bank;
size_t size;
uint8_t *data = GB_get_direct_access(gb, GB_DIRECT_ACCESS_VRAM, &size, &bank);
if (_mode != GBMemoryEntireSpace) {
bank = self.selectedBank & (size / 0x2000 - 1);
}
memcpy(dst, data + bank * 0x2000 + range.location - 0x8000, range.length);
break;
}
case 0xA:
case 0xB: {
// Some carts are special, use memory read directly in full mem mode
if (_mode == GBMemoryEntireSpace) {
case 0xF:
slow_path:
[_document performAtomicBlock:^{
for (unsigned i = 0; i < range.length; i++) {
dst[i] = GB_safe_read_memory(gb, range.location + i);
}
}];
break;
}
else {
uint16_t bank;
size_t size;
uint8_t *data = GB_get_direct_access(gb, GB_DIRECT_ACCESS_CART_RAM, &size, &bank);
bank = self.selectedBank & (size / 0x2000 - 1);
if (size == 0) {
memset(dst, 0xFF, range.length);
}
else if (range.location + range.length - 0xA000 > size) {
goto slow_path;
}
else {
memcpy(dst, data + bank * 0x2000 + range.location - 0xA000, range.length);
}
break;
}
}
case 0xC:
case 0xE: {
uint8_t *data = GB_get_direct_access(gb, GB_DIRECT_ACCESS_RAM, NULL, NULL);
memcpy(dst, data + (range.location & 0xFFF), range.length);
break;
}
case 0xD: {
uint16_t bank;
size_t size;
uint8_t *data = GB_get_direct_access(gb, GB_DIRECT_ACCESS_RAM, &size, &bank);
if (_mode != GBMemoryEntireSpace) {
bank = self.selectedBank & (size / 0x1000 - 1);
}
memcpy(dst, data + bank * 0x1000 + range.location - 0xD000, range.length);
break;
}
}
}
- (NSArray *)byteSlices
{
return @[[[GBCompleteByteSlice alloc] initWithByteArray:self]];
}
- (HFByteArray *)subarrayWithRange:(HFRange)range
{
unsigned char arr[range.length];
[self copyBytes:arr range:range];
HFByteArray *ret = [[HFBTreeByteArray alloc] init];
HFFullMemoryByteSlice *slice = [[HFFullMemoryByteSlice alloc] initWithData:[NSData dataWithBytes:arr length:range.length]];
[ret insertByteSlice:slice inRange:HFRangeMake(0, 0)];
return ret;
}
- (void)insertByteSlice:(HFByteSlice *)slice inRange:(HFRange)range
{
if (slice.length != range.length) return; /* Insertion is not allowed, only overwriting. */
// Do everything in 0x1000 chunks, never cross a 0x1000 boundary
if ((range.location & 0xF000) != ((range.location + range.length) & 0xF000)) {
size_t partial = 0x1000 - (range.location & 0xFFF);
if (slice.length - partial) {
[self insertByteSlice:[slice subsliceWithRange:HFRangeMake(partial, slice.length - partial)]
inRange:HFRangeMake(range.location + partial, range.length - partial)];
}
range.length = partial;
}
range.location += self.base;
GB_gameboy_t *gb = _document.gameboy;
switch (range.location >> 12) {
case 0x0:
case 0x1:
case 0x2:
case 0x3:
case 0x4:
case 0x5:
case 0x6:
case 0x7: {
return; // ROM not writeable
}
case 0x8:
case 0x9: {
uint16_t bank;
size_t size;
uint8_t *data = GB_get_direct_access(gb, GB_DIRECT_ACCESS_VRAM, &size, &bank);
if (_mode != GBMemoryEntireSpace) {
bank = self.selectedBank & (size / 0x2000 - 1);
}
uint8_t sliceData[range.length];
[slice copyBytes:sliceData range:HFRangeMake(0, range.length)];
memcpy(data + bank * 0x2000 + range.location - 0x8000, sliceData, range.length);
break;
}
case 0xA:
case 0xB: {
// Some carts are special, use memory write directly in full mem mode
if (_mode == GBMemoryEntireSpace) {
case 0xF:
slow_path:
[_document performAtomicBlock:^{
uint8_t sliceData[range.length];
[slice copyBytes:sliceData range:HFRangeMake(0, range.length)];
for (unsigned i = 0; i < range.length; i++) {
GB_write_memory(gb, range.location + i, sliceData[i]);
}
}];
break;
}
else {
uint16_t bank;
size_t size;
uint8_t *data = GB_get_direct_access(gb, GB_DIRECT_ACCESS_CART_RAM, &size, &bank);
bank = self.selectedBank & (size / 0x2000 - 1);
if (size == 0) {
// Nothing to write to
}
else if (range.location + range.length - 0xA000 > size) {
goto slow_path;
}
else {
uint8_t sliceData[range.length];
[slice copyBytes:sliceData range:HFRangeMake(0, range.length)];
memcpy(data + bank * 0x2000 + range.location - 0xA000, sliceData, range.length);
}
break;
}
}
case 0xC:
case 0xE: {
uint8_t *data = GB_get_direct_access(gb, GB_DIRECT_ACCESS_RAM, NULL, NULL);
uint8_t sliceData[range.length];
[slice copyBytes:sliceData range:HFRangeMake(0, range.length)];
memcpy(data + (range.location & 0xFFF), sliceData, range.length);
break;
}
case 0xD: {
uint16_t bank;
size_t size;
uint8_t *data = GB_get_direct_access(gb, GB_DIRECT_ACCESS_RAM, &size, &bank);
if (_mode != GBMemoryEntireSpace) {
bank = self.selectedBank & (size / 0x1000 - 1);
}
uint8_t sliceData[range.length];
[slice copyBytes:sliceData range:HFRangeMake(0, range.length)];
memcpy(data + bank * 0x1000 + range.location - 0xD000, sliceData, range.length);
break;
}
}
}
@end
|