annotate system.c @ 2149:9209858b2f74

Fix a couple of bugs in the TOC file parser
author Michael Pavone <pavone@retrodev.com>
date Sat, 26 Mar 2022 23:05:08 -0700
parents 3414a4423de1
children 1e626d0ecf9c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1111
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <string.h>
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include "system.h"
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include "genesis.h"
1946
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
4 #include "gen_player.h"
1117
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
5 #include "sms.h"
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
6
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
7 uint8_t safe_cmp(char *str, long offset, uint8_t *buffer, long filesize)
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
8 {
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
9 long len = strlen(str);
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
10 return filesize >= offset+len && !memcmp(str, buffer + offset, len);
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
11 }
1111
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12
1140
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
13 system_type detect_system_type(system_media *media)
1111
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 {
1140
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
15 if (safe_cmp("SEGA", 0x100, media->buffer, media->size)) {
1503
a763523dadf4 Added code for initializing a combined Genesis + Sega CD system when a Sega CD ISO is loaded
Michael Pavone <pavone@retrodev.com>
parents: 1201
diff changeset
16 //TODO: support other bootable identifiers
a763523dadf4 Added code for initializing a combined Genesis + Sega CD system when a Sega CD ISO is loaded
Michael Pavone <pavone@retrodev.com>
parents: 1201
diff changeset
17 if (safe_cmp("SEGADISCSYSTEM", 0, media->buffer, media->size)) {
a763523dadf4 Added code for initializing a combined Genesis + Sega CD system when a Sega CD ISO is loaded
Michael Pavone <pavone@retrodev.com>
parents: 1201
diff changeset
18 return SYSTEM_SEGACD;
a763523dadf4 Added code for initializing a combined Genesis + Sega CD system when a Sega CD ISO is loaded
Michael Pavone <pavone@retrodev.com>
parents: 1201
diff changeset
19 }
a763523dadf4 Added code for initializing a combined Genesis + Sega CD system when a Sega CD ISO is loaded
Michael Pavone <pavone@retrodev.com>
parents: 1201
diff changeset
20 //TODO: Differentiate between vanilla Genesis and 32X games
1111
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 return SYSTEM_GENESIS;
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 }
1140
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
23 if (safe_cmp("TMR SEGA", 0x1FF0, media->buffer, media->size)
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
24 || safe_cmp("TMR SEGA", 0x3FF0, media->buffer, media->size)
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
25 || safe_cmp("TMR SEGA", 0x7FF0, media->buffer, media->size)
1117
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
26 ) {
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
27 return SYSTEM_SMS;
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
28 }
1946
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
29 if (safe_cmp("BLSTEL\x02", 0, media->buffer, media->size)) {
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
30 uint8_t *buffer = media->buffer;
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
31 if (media->size > 9 && buffer[7] == 0) {
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
32 return buffer[8] + 1;
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
33 }
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
34 }
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
35
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
36
1117
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
37 //TODO: Detect Jaguar ROMs here
1111
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38
1140
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
39 //Header based detection failed, examine filename for clues
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
40 if (media->extension) {
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
41 if (!strcmp("md", media->extension) || !strcmp("gen", media->extension)) {
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
42 return SYSTEM_GENESIS;
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
43 }
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
44 if (!strcmp("sms", media->extension)) {
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
45 return SYSTEM_SMS;
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
46 }
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
47 if (!strcmp("j64", media->extension)) {
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
48 return SYSTEM_JAGUAR;
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
49 }
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
50 }
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
51
1111
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 //More certain checks failed, look for a valid 68K reset vector
1140
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
53 if (media->size >= 8) {
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
54 char *rom = media->buffer;
1893
7b62e6805e6a Only look at low 24-bits of reset vector in ROM type detection heuristic
Michael Pavone <pavone@retrodev.com>
parents: 1595
diff changeset
55 uint32_t reset = rom[5] << 16 | rom[6] << 8 | rom[7];
1140
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
56 if (!(reset & 1) && reset < media->size) {
1111
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 //we have a valid looking reset vector, assume it's a Genesis ROM
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 return SYSTEM_GENESIS;
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 }
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 }
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 return SYSTEM_UNKNOWN;
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 }
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63
1595
360d5bab199f Update controller config when changed in UI without restart
Michael Pavone <pavone@retrodev.com>
parents: 1201
diff changeset
64 system_header *alloc_config_system(system_type stype, system_media *media, uint32_t opts, uint8_t force_region)
1111
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 {
1140
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
66 void *lock_on = NULL;
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
67 uint32_t lock_on_size = 0;
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
68 if (media->chain) {
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
69 lock_on = media->chain->buffer;
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
70 lock_on_size = media->chain->size;
4490c9c12272 Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents: 1117
diff changeset
71 }
1111
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 switch (stype)
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 {
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 case SYSTEM_GENESIS:
1595
360d5bab199f Update controller config when changed in UI without restart
Michael Pavone <pavone@retrodev.com>
parents: 1201
diff changeset
75 return &(alloc_config_genesis(media->buffer, media->size, lock_on, lock_on_size, opts, force_region))->header;
1946
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
76 case SYSTEM_GENESIS_PLAYER:
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
77 return &(alloc_config_gen_player(media->buffer, media->size))->header;
1503
a763523dadf4 Added code for initializing a combined Genesis + Sega CD system when a Sega CD ISO is loaded
Michael Pavone <pavone@retrodev.com>
parents: 1201
diff changeset
78 case SYSTEM_SEGACD:
1692
5dacaef602a7 Merge from default
Michael Pavone <pavone@retrodev.com>
parents: 1503 1595
diff changeset
79 return &(alloc_config_genesis_cdboot(media, opts, force_region))->header;
1117
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
80 #ifndef NO_Z80
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
81 case SYSTEM_SMS:
1595
360d5bab199f Update controller config when changed in UI without restart
Michael Pavone <pavone@retrodev.com>
parents: 1201
diff changeset
82 return &(alloc_configure_sms(media, opts, force_region))->header;
1117
928a65750345 Initial support for Genesis/Megadrive PBC mode. VDP still needs Mode 4 to be useful.
Michael Pavone <pavone@retrodev.com>
parents: 1111
diff changeset
83 #endif
1111
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 default:
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 return NULL;
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 }
2eb54e24914e Mostly working changes to allow support for multiple emulated system types in main blastem program
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
87 }
1946
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
88
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
89 system_header *alloc_config_player(system_type stype, event_reader *reader)
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
90 {
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
91 switch(stype)
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
92 {
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
93 case SYSTEM_GENESIS:
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
94 return &(alloc_config_gen_player_reader(reader))->header;
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
95 }
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
96 return NULL;
c3c62dbf1ceb WIP netplay support
Michael Pavone <pavone@retrodev.com>
parents: 1893
diff changeset
97 }
1980
81df9aa2de9b Less hacky run on audio thread mode
Michael Pavone <pavone@retrodev.com>
parents: 1946
diff changeset
98
81df9aa2de9b Less hacky run on audio thread mode
Michael Pavone <pavone@retrodev.com>
parents: 1946
diff changeset
99 void system_request_exit(system_header *system, uint8_t force_release)
81df9aa2de9b Less hacky run on audio thread mode
Michael Pavone <pavone@retrodev.com>
parents: 1946
diff changeset
100 {
81df9aa2de9b Less hacky run on audio thread mode
Michael Pavone <pavone@retrodev.com>
parents: 1946
diff changeset
101 system->force_release = force_release;
81df9aa2de9b Less hacky run on audio thread mode
Michael Pavone <pavone@retrodev.com>
parents: 1946
diff changeset
102 system->request_exit(system);
81df9aa2de9b Less hacky run on audio thread mode
Michael Pavone <pavone@retrodev.com>
parents: 1946
diff changeset
103 }