comparison blastem.c @ 1696:956c1cce05e2 mame_interp

Merge from default
author Michael Pavone <pavone@retrodev.com>
date Thu, 24 Jan 2019 19:15:59 -0800
parents b7ecd0d6a77b f6bd4962b8f5
children 8fe162bdb038
comparison
equal deleted inserted replaced
1648:b7ecd0d6a77b 1696:956c1cce05e2
32 #include "zip.h" 32 #include "zip.h"
33 #ifndef DISABLE_NUKLEAR 33 #ifndef DISABLE_NUKLEAR
34 #include "nuklear_ui/blastem_nuklear.h" 34 #include "nuklear_ui/blastem_nuklear.h"
35 #endif 35 #endif
36 36
37 #define BLASTEM_VERSION "0.6.0-pre" 37 #define BLASTEM_VERSION "0.6.2-pre"
38 38
39 #ifdef __ANDROID__ 39 #ifdef __ANDROID__
40 #define FULLSCREEN_DEFAULT 1 40 #define FULLSCREEN_DEFAULT 1
41 #else 41 #else
42 #define FULLSCREEN_DEFAULT 0 42 #define FULLSCREEN_DEFAULT 0
71 #define romseek gzseek 71 #define romseek gzseek
72 #define romgetc gzgetc 72 #define romgetc gzgetc
73 #define romclose gzclose 73 #define romclose gzclose
74 #endif 74 #endif
75 75
76 uint16_t *process_smd_block(uint16_t *dst, uint8_t *src, size_t bytes)
77 {
78 for (uint8_t *low = src, *high = (src+bytes/2), *end = src+bytes; high < end; high++, low++) {
79 *(dst++) = *low << 8 | *high;
80 }
81 return dst;
82 }
83
76 int load_smd_rom(ROMFILE f, void **buffer) 84 int load_smd_rom(ROMFILE f, void **buffer)
77 { 85 {
78 uint8_t block[SMD_BLOCK_SIZE]; 86 uint8_t block[SMD_BLOCK_SIZE];
79 romseek(f, SMD_HEADER_SIZE, SEEK_SET); 87 romseek(f, SMD_HEADER_SIZE, SEEK_SET);
80 88
81 size_t filesize = 512 * 1024; 89 size_t filesize = 512 * 1024;
82 size_t readsize = 0; 90 size_t readsize = 0;
83 uint16_t *dst = malloc(filesize); 91 uint16_t *dst, *buf;
92 dst = buf = malloc(filesize);
84 93
85 94
86 size_t read; 95 size_t read;
87 do { 96 do {
88 if ((readsize + SMD_BLOCK_SIZE > filesize)) { 97 if ((readsize + SMD_BLOCK_SIZE > filesize)) {
89 filesize *= 2; 98 filesize *= 2;
90 dst = realloc(dst, filesize); 99 buf = realloc(buf, filesize);
100 dst = buf + readsize/sizeof(uint16_t);
91 } 101 }
92 read = romread(block, 1, SMD_BLOCK_SIZE, f); 102 read = romread(block, 1, SMD_BLOCK_SIZE, f);
93 if (read > 0) { 103 if (read > 0) {
94 for (uint8_t *low = block, *high = (block+read/2), *end = block+read; high < end; high++, low++) { 104 dst = process_smd_block(dst, block, read);
95 *(dst++) = *low << 8 | *high;
96 }
97 readsize += read; 105 readsize += read;
98 } 106 }
99 } while(read > 0); 107 } while(read > 0);
100 romclose(f); 108 romclose(f);
101 109
102 *buffer = dst; 110 *buffer = buf;
103 111
104 return readsize; 112 return readsize;
105 } 113 }
106 114
115 uint8_t is_smd_format(const char *filename, uint8_t *header)
116 {
117 if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) {
118 int i;
119 for (i = 3; i < 8; i++) {
120 if (header[i] != 0) {
121 return 0;
122 }
123 }
124 if (i == 8) {
125 if (header[2]) {
126 fatal_error("%s is a split SMD ROM which is not currently supported", filename);
127 }
128 return 1;
129 }
130 }
131 return 0;
132 }
133
107 uint32_t load_rom_zip(const char *filename, void **dst) 134 uint32_t load_rom_zip(const char *filename, void **dst)
108 { 135 {
109 static const char *valid_exts[] = {"bin", "md", "gen", "sms", "rom"}; 136 static const char *valid_exts[] = {"bin", "md", "gen", "sms", "rom", "smd"};
110 const uint32_t num_exts = sizeof(valid_exts)/sizeof(*valid_exts); 137 const uint32_t num_exts = sizeof(valid_exts)/sizeof(*valid_exts);
111 zip_file *z = zip_open(filename); 138 zip_file *z = zip_open(filename);
112 if (!z) { 139 if (!z) {
113 return 0; 140 return 0;
114 } 141 }
123 { 150 {
124 if (!strcasecmp(ext, valid_exts[j])) { 151 if (!strcasecmp(ext, valid_exts[j])) {
125 size_t out_size = nearest_pow2(z->entries[i].size); 152 size_t out_size = nearest_pow2(z->entries[i].size);
126 *dst = zip_read(z, i, &out_size); 153 *dst = zip_read(z, i, &out_size);
127 if (*dst) { 154 if (*dst) {
155 if (is_smd_format(z->entries[i].name, *dst)) {
156 size_t offset;
157 for (offset = 0; offset + SMD_BLOCK_SIZE + SMD_HEADER_SIZE < out_size; offset += SMD_BLOCK_SIZE)
158 {
159 uint8_t tmp[SMD_BLOCK_SIZE];
160 memcpy(tmp, *dst + offset + SMD_HEADER_SIZE, SMD_BLOCK_SIZE);
161 process_smd_block(*dst + offset, tmp, SMD_BLOCK_SIZE);
162 }
163 out_size = offset;
164 }
128 free(ext); 165 free(ext);
129 zip_close(z); 166 zip_close(z);
130 return out_size; 167 return out_size;
131 } 168 }
132 } 169 }
139 176
140 uint32_t load_rom(const char * filename, void **dst, system_type *stype) 177 uint32_t load_rom(const char * filename, void **dst, system_type *stype)
141 { 178 {
142 uint8_t header[10]; 179 uint8_t header[10];
143 char *ext = path_extension(filename); 180 char *ext = path_extension(filename);
144 if (!strcasecmp(ext, "zip")) { 181 if (ext && !strcasecmp(ext, "zip")) {
145 free(ext); 182 free(ext);
146 return load_rom_zip(filename, dst); 183 return load_rom_zip(filename, dst);
147 } 184 }
148 free(ext); 185 free(ext);
149 ROMFILE f = romopen(filename, "rb"); 186 ROMFILE f = romopen(filename, "rb");
152 } 189 }
153 if (sizeof(header) != romread(header, 1, sizeof(header), f)) { 190 if (sizeof(header) != romread(header, 1, sizeof(header), f)) {
154 fatal_error("Error reading from %s\n", filename); 191 fatal_error("Error reading from %s\n", filename);
155 } 192 }
156 193
157 if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) { 194 if (is_smd_format(filename, header)) {
158 int i; 195 if (stype) {
159 for (i = 3; i < 8; i++) { 196 *stype = SYSTEM_GENESIS;
160 if (header[i] != 0) { 197 }
161 break; 198 return load_smd_rom(f, dst);
162 }
163 }
164 if (i == 8) {
165 if (header[2]) {
166 fatal_error("%s is a split SMD ROM which is not currently supported", filename);
167 }
168 if (stype) {
169 *stype = SYSTEM_GENESIS;
170 }
171 return load_smd_rom(f, dst);
172 }
173 } 199 }
174 200
175 size_t filesize = 512 * 1024; 201 size_t filesize = 512 * 1024;
176 size_t readsize = sizeof(header); 202 size_t readsize = sizeof(header);
177 203
579 set_bindings(); 605 set_bindings();
580 606
581 uint8_t menu = !loaded; 607 uint8_t menu = !loaded;
582 uint8_t use_nuklear = 0; 608 uint8_t use_nuklear = 0;
583 #ifndef DISABLE_NUKLEAR 609 #ifndef DISABLE_NUKLEAR
584 use_nuklear = is_nuklear_available(); 610 use_nuklear = !headless && is_nuklear_available();
585 #endif 611 #endif
586 if (!loaded && !use_nuklear) { 612 if (!loaded && !use_nuklear) {
587 //load menu 613 //load menu
588 romfname = tern_find_path(config, "ui\0rom\0", TVAL_PTR).ptrval; 614 romfname = tern_find_path(config, "ui\0rom\0", TVAL_PTR).ptrval;
589 if (!romfname) { 615 if (!romfname) {