-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsega_mapper.c
148 lines (140 loc) · 3.79 KB
/
sega_mapper.c
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
#include "genesis.h"
uint16_t read_sram_w(uint32_t address, m68k_context * context)
{
genesis_context * gen = context->system;
address &= gen->save_ram_mask;
switch(gen->save_type)
{
case RAM_FLAG_BOTH:
return gen->save_storage[address] << 8 | gen->save_storage[address+1];
case RAM_FLAG_EVEN:
return gen->save_storage[address >> 1] << 8 | 0xFF;
case RAM_FLAG_ODD:
return gen->save_storage[address >> 1] | 0xFF00;
}
return 0xFFFF;//We should never get here
}
uint8_t read_sram_b(uint32_t address, m68k_context * context)
{
genesis_context * gen = context->system;
address &= gen->save_ram_mask;
switch(gen->save_type)
{
case RAM_FLAG_BOTH:
return gen->save_storage[address];
case RAM_FLAG_EVEN:
if (address & 1) {
return 0xFF;
} else {
return gen->save_storage[address >> 1];
}
case RAM_FLAG_ODD:
if (address & 1) {
return gen->save_storage[address >> 1];
} else {
return 0xFF;
}
}
return 0xFF;//We should never get here
}
m68k_context * write_sram_area_w(uint32_t address, m68k_context * context, uint16_t value)
{
genesis_context * gen = context->system;
if ((gen->bank_regs[0] & 0x3) == 1) {
address &= gen->save_ram_mask;
switch(gen->save_type)
{
case RAM_FLAG_BOTH:
gen->save_storage[address] = value >> 8;
gen->save_storage[address+1] = value;
break;
case RAM_FLAG_EVEN:
gen->save_storage[address >> 1] = value >> 8;
break;
case RAM_FLAG_ODD:
gen->save_storage[address >> 1] = value;
break;
}
}
return context;
}
m68k_context * write_sram_area_b(uint32_t address, m68k_context * context, uint8_t value)
{
genesis_context * gen = context->system;
if ((gen->bank_regs[0] & 0x3) == 1) {
address &= gen->save_ram_mask;
switch(gen->save_type)
{
case RAM_FLAG_BOTH:
gen->save_storage[address] = value;
break;
case RAM_FLAG_EVEN:
if (!(address & 1)) {
gen->save_storage[address >> 1] = value;
}
break;
case RAM_FLAG_ODD:
if (address & 1) {
gen->save_storage[address >> 1] = value;
}
break;
}
}
return context;
}
m68k_context * write_bank_reg_w(uint32_t address, m68k_context * context, uint16_t value)
{
genesis_context * gen = context->system;
address &= 0xE;
address >>= 1;
gen->bank_regs[address] = value;
if (!address) {
if (value & 1) {
//Used for games that only use the mapper for SRAM
if (context->mem_pointers[gen->mapper_start_index]) {
gen->mapper_temp = context->mem_pointers[gen->mapper_start_index];
}
context->mem_pointers[gen->mapper_start_index] = NULL;
//For games that need more than 4MB
for (int i = 4; i < 8; i++)
{
context->mem_pointers[gen->mapper_start_index + i] = NULL;
}
} else {
//Used for games that only use the mapper for SRAM
if (!context->mem_pointers[gen->mapper_start_index]) {
context->mem_pointers[gen->mapper_start_index] = gen->mapper_temp;
}
//For games that need more than 4MB
for (int i = 4; i < 8; i++)
{
context->mem_pointers[gen->mapper_start_index + i] = gen->cart + 0x40000*gen->bank_regs[i];
}
}
} else {
void *new_ptr = gen->cart + 0x40000*value;
if (context->mem_pointers[gen->mapper_start_index + address] != new_ptr) {
m68k_invalidate_code_range(gen->m68k, address * 0x80000, (address + 1) * 0x80000);
context->mem_pointers[gen->mapper_start_index + address] = new_ptr;
}
}
return context;
}
m68k_context * write_bank_reg_b(uint32_t address, m68k_context * context, uint8_t value)
{
if (address & 1) {
write_bank_reg_w(address, context, value);
}
return context;
}
void sega_mapper_serialize(genesis_context *gen, serialize_buffer *buf)
{
save_buffer8(buf, gen->bank_regs, sizeof(gen->bank_regs));
}
void sega_mapper_deserialize(deserialize_buffer *buf, genesis_context *gen)
{
for (int i = 0; i < sizeof(gen->bank_regs); i++)
{
write_bank_reg_w(i * 2, gen->m68k, load_int8(buf));
}
}