Mercurial > repos > blastem
comparison romdb.c @ 769:4638b88bc72d
Initial work on I2C EEPROM implementation
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 10 Jul 2015 18:46:18 -0700 |
parents | 2f48a3c187c6 |
children | a3b90f746dcf |
comparison
equal
deleted
inserted
replaced
768:2f48a3c187c6 | 769:4638b88bc72d |
---|---|
13 #define RAM_ID 0x1B0 | 13 #define RAM_ID 0x1B0 |
14 #define RAM_FLAGS 0x1B2 | 14 #define RAM_FLAGS 0x1B2 |
15 #define RAM_START 0x1B4 | 15 #define RAM_START 0x1B4 |
16 #define RAM_END 0x1B8 | 16 #define RAM_END 0x1B8 |
17 #define REGION_START 0x1F0 | 17 #define REGION_START 0x1F0 |
18 | |
19 void eeprom_init(eeprom_state *state) | |
20 { | |
21 state->slave_sda = 1; | |
22 state->host_sda = state->scl = 0; | |
23 } | |
24 | |
25 void set_host_sda(eeprom_state *state, uint8_t val) | |
26 { | |
27 if (state->scl) { | |
28 if (val & ~state->host_sda) { | |
29 //stop condition | |
30 } else if (~val & state->host_sda) { | |
31 //start condition | |
32 } | |
33 } | |
34 state->host_sda = val; | |
35 } | |
36 | |
37 void set_scl(eeprom_state *state, uint8_t val) | |
38 { | |
39 if (val & ~state->scl) { | |
40 //latch sda | |
41 } | |
42 state->scl = val; | |
43 } | |
44 | |
45 uint8_t get_sda(eeprom_state *state) | |
46 { | |
47 return state->host_sda & state->slave_sda; | |
48 } | |
18 | 49 |
19 uint16_t read_sram_w(uint32_t address, m68k_context * context) | 50 uint16_t read_sram_w(uint32_t address, m68k_context * context) |
20 { | 51 { |
21 genesis_context * gen = context->system; | 52 genesis_context * gen = context->system; |
22 address &= gen->save_ram_mask; | 53 address &= gen->save_ram_mask; |
133 context->mem_pointers[2] = cart + 0x200000/2; | 164 context->mem_pointers[2] = cart + 0x200000/2; |
134 } | 165 } |
135 } | 166 } |
136 } | 167 } |
137 return context; | 168 return context; |
169 } | |
170 eeprom_map *find_eeprom_map(uint32_t address, genesis_context *gen) | |
171 { | |
172 for (int i = 0; i < gen->num_eeprom; i++) | |
173 { | |
174 if (address >= gen->eeprom_map[i].start && address <= gen->eeprom_map[i].end) { | |
175 return gen->eeprom_map + i; | |
176 } | |
177 } | |
178 return NULL; | |
179 } | |
180 | |
181 void * write_eeprom_i2c_w(uint32_t address, void * context, uint16_t value) | |
182 { | |
183 genesis_context *gen = ((m68k_context *)context)->system; | |
184 eeprom_map *map = find_eeprom_map(address, gen); | |
185 if (!map) { | |
186 fprintf(stderr, "Could not find EEPROM map for address %X\n", address); | |
187 exit(1); | |
188 } | |
189 printf("EEPROM word write: %X - %X\n", address, value); | |
190 if (map->sda_write_mask) { | |
191 printf("sda: %d\n", (value & map->sda_write_mask) != 0); | |
192 set_host_sda(&gen->eeprom, (value & map->sda_write_mask) != 0); | |
193 } | |
194 if (map->scl_mask) { | |
195 printf("scl: %d\n", (value & map->scl_mask) != 0); | |
196 set_scl(&gen->eeprom, (value & map->sda_write_mask) != 0); | |
197 } | |
198 return context; | |
199 } | |
200 | |
201 void * write_eeprom_i2c_b(uint32_t address, void * context, uint8_t value) | |
202 { | |
203 genesis_context *gen = ((m68k_context *)context)->system; | |
204 eeprom_map *map = find_eeprom_map(address, gen); | |
205 if (!map) { | |
206 fprintf(stderr, "Could not find EEPROM map for address %X\n", address); | |
207 exit(1); | |
208 } | |
209 | |
210 uint16_t expanded, mask; | |
211 if (address & 1) { | |
212 expanded = value; | |
213 mask = 0xFF; | |
214 } else { | |
215 expanded = value << 8; | |
216 mask = 0xFF00; | |
217 } | |
218 printf("EEPROM byte write: %X - %X (using mask %X and expanded val %X)\n", address, value, mask, expanded); | |
219 if (map->sda_write_mask & mask) { | |
220 printf("sda: %d\n", (expanded & map->sda_write_mask) != 0); | |
221 set_host_sda(&gen->eeprom, (expanded & map->sda_write_mask) != 0); | |
222 } | |
223 if (map->scl_mask & mask) { | |
224 printf("scl: %d\n", (expanded & map->scl_mask) != 0); | |
225 set_scl(&gen->eeprom, (expanded & map->scl_mask) != 0); | |
226 } | |
227 return context; | |
228 } | |
229 | |
230 uint16_t read_eeprom_i2c_w(uint32_t address, void * context) | |
231 { | |
232 genesis_context *gen = ((m68k_context *)context)->system; | |
233 eeprom_map *map = find_eeprom_map(address, gen); | |
234 if (!map) { | |
235 fprintf(stderr, "Could not find EEPROM map for address %X\n", address); | |
236 exit(1); | |
237 } | |
238 uint16_t ret = 0; | |
239 if (map->sda_read_bit < 16) { | |
240 ret = get_sda(&gen->eeprom) << map->sda_read_bit; | |
241 } | |
242 printf("EEPROM word read: %X - %X\n", address, ret); | |
243 return ret; | |
244 } | |
245 | |
246 uint8_t read_eeprom_i2c_b(uint32_t address, void * context) | |
247 { | |
248 genesis_context *gen = ((m68k_context *)context)->system; | |
249 eeprom_map *map = find_eeprom_map(address, gen); | |
250 if (!map) { | |
251 fprintf(stderr, "Could not find EEPROM map for address %X\n", address); | |
252 exit(1); | |
253 } | |
254 uint8_t bit = address & 1 ? map->sda_read_bit : map->sda_read_bit - 8; | |
255 uint8_t ret = 0; | |
256 if (bit < 8) { | |
257 ret = get_sda(&gen->eeprom) << bit; | |
258 } | |
259 printf("EEPROM byte read: %X - %X\n", address, ret); | |
260 return ret; | |
138 } | 261 } |
139 | 262 |
140 tern_node *load_rom_db() | 263 tern_node *load_rom_db() |
141 { | 264 { |
142 char *exe_dir = get_exe_dir(); | 265 char *exe_dir = get_exe_dir(); |
314 rom_info *info; | 437 rom_info *info; |
315 uint8_t *rom; | 438 uint8_t *rom; |
316 tern_node *root; | 439 tern_node *root; |
317 uint32_t rom_size; | 440 uint32_t rom_size; |
318 int index; | 441 int index; |
442 int num_els; | |
319 } map_iter_state; | 443 } map_iter_state; |
444 | |
445 void eeprom_read_fun(char *key, tern_val val, void *data) | |
446 { | |
447 int bit = atoi(key); | |
448 if (bit < 0 || bit > 15) { | |
449 fprintf(stderr, "bit %s is out of range", key); | |
450 return; | |
451 } | |
452 char *pin = val.ptrval; | |
453 if (strcmp(pin, "sda")) { | |
454 fprintf(stderr, "bit %s is connected to unrecognized read pin %s", key, pin); | |
455 return; | |
456 } | |
457 eeprom_map *map = data; | |
458 map->sda_read_bit = bit; | |
459 } | |
460 | |
461 void eeprom_write_fun(char *key, tern_val val, void *data) | |
462 { | |
463 int bit = atoi(key); | |
464 if (bit < 0 || bit > 15) { | |
465 fprintf(stderr, "bit %s is out of range", key); | |
466 return; | |
467 } | |
468 char *pin = val.ptrval; | |
469 eeprom_map *map = data; | |
470 if (!strcmp(pin, "sda")) { | |
471 map->sda_write_mask = 1 << bit; | |
472 return; | |
473 } | |
474 if (!strcmp(pin, "scl")) { | |
475 map->scl_mask = 1 << bit; | |
476 return; | |
477 } | |
478 fprintf(stderr, "bit %s is connected to unrecognized write pin %s", key, pin); | |
479 } | |
320 | 480 |
321 void map_iter_fun(char *key, tern_val val, void *data) | 481 void map_iter_fun(char *key, tern_val val, void *data) |
322 { | 482 { |
323 map_iter_state *state = data; | 483 map_iter_state *state = data; |
324 tern_node *node = tern_get_node(val); | 484 tern_node *node = tern_get_node(val); |
340 if (!strcmp(dtype, "ROM")) { | 500 if (!strcmp(dtype, "ROM")) { |
341 map->buffer = state->rom + offset; | 501 map->buffer = state->rom + offset; |
342 map->flags = MMAP_READ; | 502 map->flags = MMAP_READ; |
343 map->mask = calc_mask(state->rom_size, start, end); | 503 map->mask = calc_mask(state->rom_size, start, end); |
344 } else if (!strcmp(dtype, "EEPROM")) { | 504 } else if (!strcmp(dtype, "EEPROM")) { |
345 | 505 if (!state->info->save_size) { |
346 | 506 char * size = tern_find_path(state->root, "EEPROM\0size\0").ptrval; |
507 if (!size) { | |
508 fprintf(stderr, "ROM DB map entry %d with address %s has device type EEPROM, but the EEPROM size is not defined\n", state->index, key); | |
509 exit(1); | |
510 } | |
511 state->info->save_size = atoi(size); | |
512 if (!state->info->save_size) { | |
513 fprintf(stderr, "EEPROM size %s is invalid\n", size); | |
514 exit(1); | |
515 } | |
516 char *etype = tern_find_path(state->root, "EEPROM\0type\0").ptrval; | |
517 if (!etype) { | |
518 etype = "i2c"; | |
519 } | |
520 if (!strcmp(etype, "i2c")) { | |
521 state->info->save_type = SAVE_I2C; | |
522 } else { | |
523 fprintf(stderr, "EEPROM type %s is invalid\n", etype); | |
524 exit(1); | |
525 } | |
526 state->info->save_buffer = malloc(state->info->save_size); | |
527 state->info->eeprom_map = malloc(sizeof(eeprom_map) * state->num_els); | |
528 memset(state->info->eeprom_map, 0, sizeof(eeprom_map) * state->num_els); | |
529 } | |
530 eeprom_map *eep_map = state->info->eeprom_map + state->info->num_eeprom; | |
531 eep_map->start = start; | |
532 eep_map->end = end; | |
533 eep_map->sda_read_bit = 0xFF; | |
534 tern_node * bits_read = tern_find_ptr(node, "bits_read"); | |
535 if (bits_read) { | |
536 tern_foreach(bits_read, eeprom_read_fun, eep_map); | |
537 } | |
538 tern_node * bits_write = tern_find_ptr(node, "bits_write"); | |
539 if (bits_write) { | |
540 tern_foreach(bits_write, eeprom_write_fun, eep_map); | |
541 } | |
542 printf("EEPROM address %X: sda read: %X, sda write: %X, scl: %X\n", start, eep_map->sda_read_bit, eep_map->sda_write_mask, eep_map->scl_mask); | |
543 state->info->num_eeprom++; | |
544 map->write_16 = write_eeprom_i2c_w; | |
545 map->write_8 = write_eeprom_i2c_b; | |
546 map->read_16 = read_eeprom_i2c_w; | |
547 map->read_8 = read_eeprom_i2c_b; | |
548 map->mask = 0xFFFFFF; | |
347 } else if (!strcmp(dtype, "SRAM")) { | 549 } else if (!strcmp(dtype, "SRAM")) { |
348 if (!state->info->save_size) { | 550 if (!state->info->save_size) { |
349 char * size = tern_find_path(state->root, "SRAM\0size\0").ptrval; | 551 char * size = tern_find_path(state->root, "SRAM\0size\0").ptrval; |
350 if (!size) { | 552 if (!size) { |
351 fprintf(stderr, "ROM DB map entry %d with address %s has device type SRAM, but the SRAM size is not defined\n", state->index, key); | 553 fprintf(stderr, "ROM DB map entry %d with address %s has device type SRAM, but the SRAM size is not defined\n", state->index, key); |
428 if (info.map_chunks) { | 630 if (info.map_chunks) { |
429 info.map_chunks += base_chunks; | 631 info.map_chunks += base_chunks; |
430 info.save_buffer = NULL; | 632 info.save_buffer = NULL; |
431 info.save_size = 0; | 633 info.save_size = 0; |
432 info.map = malloc(sizeof(memmap_chunk) * info.map_chunks); | 634 info.map = malloc(sizeof(memmap_chunk) * info.map_chunks); |
635 info.eeprom_map = NULL; | |
636 info.num_eeprom = 0; | |
433 memset(info.map, 0, sizeof(memmap_chunk) * (info.map_chunks - base_chunks)); | 637 memset(info.map, 0, sizeof(memmap_chunk) * (info.map_chunks - base_chunks)); |
434 map_iter_state state = {&info, rom, entry, rom_size, 0}; | 638 map_iter_state state = {&info, rom, entry, rom_size, 0, info.map_chunks - base_chunks}; |
435 tern_foreach(map, map_iter_fun, &state); | 639 tern_foreach(map, map_iter_fun, &state); |
436 memcpy(info.map + state.index, base_map, sizeof(memmap_chunk) * base_chunks); | 640 memcpy(info.map + state.index, base_map, sizeof(memmap_chunk) * base_chunks); |
437 } else { | 641 } else { |
438 add_memmap_header(&info, rom, rom_size, base_map, base_chunks); | 642 add_memmap_header(&info, rom, rom_size, base_map, base_chunks); |
439 } | 643 } |