Mercurial > repos > blastem
comparison realtec.c @ 1259:23c94f5266d1
Support for the Realtec mapper. Needs testing with games besides The Earth Defend
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 02 Mar 2017 21:08:33 -0800 |
parents | |
children | f13f51e9f9f2 |
comparison
equal
deleted
inserted
replaced
1258:1148567ab355 | 1259:23c94f5266d1 |
---|---|
1 #include <stdint.h> | |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include "romdb.h" | |
5 #include "genesis.h" | |
6 #include "util.h" | |
7 | |
8 typedef struct { | |
9 uint8_t rom_space[512*1024]; | |
10 uint8_t regs[3]; | |
11 } realtec; | |
12 | |
13 | |
14 uint8_t realtec_detect(uint8_t *rom, uint32_t rom_size) | |
15 { | |
16 //All Realtec mapper games are 512KB total | |
17 if (rom_size != 512*1024) { | |
18 return 0; | |
19 } | |
20 return memcmp(rom + 0x7E100, "SEGA", 4) == 0; | |
21 } | |
22 | |
23 static void *realtec_write_b(uint32_t address, void *context, uint8_t value) | |
24 { | |
25 if (address & 1) { | |
26 return context; | |
27 } | |
28 m68k_context *m68k = context; | |
29 genesis_context *gen = m68k->system; | |
30 if (!gen->extra) { | |
31 gen->extra = m68k->mem_pointers[0]; | |
32 } | |
33 realtec *r = gen->extra; | |
34 uint32_t offset = address >> 13; | |
35 uint8_t dirty = 0; | |
36 if (offset < 3 && r->regs[offset] != value) { | |
37 r->regs[offset] = value; | |
38 //other regs are only 3 bits, so assume 3 for this one too | |
39 uint32_t size = (r->regs[1] & 0x7) << 17; | |
40 uint32_t start = (r->regs[2] & 7) << 17 | (r->regs[0] & 6) << 19; | |
41 if (size > 512*1024) { | |
42 size = 512*1024; | |
43 } | |
44 for(uint32_t cur = 0; cur < 512*1024; cur += size) | |
45 { | |
46 if (start + size > 512*1024) { | |
47 memcpy(r->rom_space + cur, gen->cart + start/2, 512*1024-start); | |
48 //assume it wraps | |
49 memcpy(r->rom_space + cur + 512*1024-start, gen->cart, size - (512*1024-start)); | |
50 } else { | |
51 memcpy(r->rom_space + cur, gen->cart + start/2, size); | |
52 } | |
53 } | |
54 m68k_invalidate_code_range(gen->m68k, 0, 0x400000); | |
55 } | |
56 return context; | |
57 } | |
58 | |
59 static void *realtec_write_w(uint32_t address, void *context, uint16_t value) | |
60 { | |
61 return realtec_write_b(address, context, value >> 8); | |
62 } | |
63 | |
64 rom_info realtec_configure_rom(uint8_t *rom, uint32_t rom_size, memmap_chunk const *base_map, uint32_t base_chunks) | |
65 { | |
66 rom_info info; | |
67 realtec *r = calloc(sizeof(realtec), 1); | |
68 for (uint32_t i = 0; i < 512*1024; i += 8*1024) | |
69 { | |
70 memcpy(r->rom_space + i, rom + 0x7E000, 8*1024); | |
71 } | |
72 byteswap_rom(512*1024, (uint16_t *)r->rom_space); | |
73 | |
74 uint8_t *name_start = NULL, *name_end = NULL; | |
75 for (int i = 0x90; i < 0xE0; i++) | |
76 { | |
77 if (name_start) { | |
78 if (rom[i] < ' ' || rom[i] > 0x80 || !memcmp(rom+i, "ARE", 3)) { | |
79 name_end = rom+i; | |
80 break; | |
81 } | |
82 } else if (rom[i] > ' ' && rom[i] < 0x80) { | |
83 name_start = rom + i; | |
84 } | |
85 } | |
86 if (name_start && !name_end) { | |
87 name_end = rom + 0xE0; | |
88 } | |
89 if (name_end) { | |
90 while (name_end > name_start && name_end[-1] == ' ') | |
91 { | |
92 name_end--; | |
93 } | |
94 info.name = malloc(name_end-name_start+1); | |
95 memcpy(info.name, name_start, name_end-name_start); | |
96 info.name[name_end-name_start] = 0; | |
97 } else { | |
98 info.name = strdup("Realtec Game"); | |
99 } | |
100 info.save_type = SAVE_NONE; | |
101 info.save_size = 0; | |
102 info.save_buffer = NULL; | |
103 info.num_eeprom = 0; | |
104 info.eeprom_map = NULL; | |
105 info.port1_override = info.port2_override = info.ext_override = info.mouse_mode = NULL; | |
106 info.map_chunks = base_chunks + 2; | |
107 info.map = calloc(sizeof(memmap_chunk), info.map_chunks); | |
108 info.map[0].mask = sizeof(r->rom_space)-1; | |
109 info.map[0].flags = MMAP_READ|MMAP_CODE|MMAP_PTR_IDX; | |
110 info.map[0].start = 0; | |
111 info.map[0].end = 0x400000; | |
112 info.map[0].buffer = r->rom_space; | |
113 info.map[0].write_16 = NULL; | |
114 info.map[0].read_16 = NULL; | |
115 info.map[0].write_8 = NULL; | |
116 info.map[0].read_8 = NULL; | |
117 info.map[1].mask = 0x7FFF; | |
118 info.map[1].flags = 0; | |
119 info.map[1].start = 0x400000; | |
120 info.map[1].end = 0x800000; | |
121 info.map[1].write_16 = realtec_write_w; | |
122 info.map[1].write_8 = realtec_write_b; | |
123 info.map[1].read_16 = NULL; | |
124 info.map[1].read_8 = NULL; | |
125 memcpy(info.map + 2, base_map, base_chunks * sizeof(memmap_chunk)); | |
126 | |
127 return info; | |
128 } |