comparison blastem.c @ 1534:c59adc305e46 nuklear_ui

Merge
author Michael Pavone <pavone@retrodev.com>
date Sat, 24 Mar 2018 22:18:23 -0700
parents 637fbc3b5063 092675db4f37
children b525491b4e5b
comparison
equal deleted inserted replaced
1528:855210dca5b9 1534:c59adc305e46
22 #include "romdb.h" 22 #include "romdb.h"
23 #include "terminal.h" 23 #include "terminal.h"
24 #include "arena.h" 24 #include "arena.h"
25 #include "config.h" 25 #include "config.h"
26 #include "menu.h" 26 #include "menu.h"
27 #include "zip.h"
27 #ifndef DISABLE_NUKLEAR 28 #ifndef DISABLE_NUKLEAR
28 #include "nuklear_ui/blastem_nuklear.h" 29 #include "nuklear_ui/blastem_nuklear.h"
29 #endif 30 #endif
30 31
31 #define BLASTEM_VERSION "0.5.2-pre" 32 #define BLASTEM_VERSION "0.5.2-pre"
48 #define SMD_MAGIC1 0x03 49 #define SMD_MAGIC1 0x03
49 #define SMD_MAGIC2 0xAA 50 #define SMD_MAGIC2 0xAA
50 #define SMD_MAGIC3 0xBB 51 #define SMD_MAGIC3 0xBB
51 #define SMD_BLOCK_SIZE 0x4000 52 #define SMD_BLOCK_SIZE 0x4000
52 53
53 int load_smd_rom(long filesize, FILE * f, void **buffer) 54 #ifdef DISABLE_ZLIB
55 #define ROMFILE FILE*
56 #define romopen fopen
57 #define romread fread
58 #define romseek fseek
59 #define romgetc fgetc
60 #define romclose fclose
61 #else
62 #include "zlib/zlib.h"
63 #define ROMFILE gzFile
64 #define romopen gzopen
65 #define romread gzfread
66 #define romseek gzseek
67 #define romgetc gzgetc
68 #define romclose gzclose
69 #endif
70
71 int load_smd_rom(ROMFILE f, void **buffer)
54 { 72 {
55 uint8_t block[SMD_BLOCK_SIZE]; 73 uint8_t block[SMD_BLOCK_SIZE];
56 filesize -= SMD_HEADER_SIZE; 74 romseek(f, SMD_HEADER_SIZE, SEEK_SET);
57 fseek(f, SMD_HEADER_SIZE, SEEK_SET); 75
58 76 size_t filesize = 512 * 1024;
59 uint16_t *dst = *buffer = malloc(nearest_pow2(filesize)); 77 size_t readsize = 0;
60 int rom_size = filesize; 78 uint16_t *dst = malloc(filesize);
61 while (filesize > 0) { 79
62 fread(block, 1, SMD_BLOCK_SIZE, f); 80
63 for (uint8_t *low = block, *high = (block+SMD_BLOCK_SIZE/2), *end = block+SMD_BLOCK_SIZE; high < end; high++, low++) { 81 size_t read;
64 *(dst++) = *low << 8 | *high; 82 do {
65 } 83 if ((readsize + SMD_BLOCK_SIZE > filesize)) {
66 filesize -= SMD_BLOCK_SIZE; 84 filesize *= 2;
67 } 85 dst = realloc(dst, filesize);
68 return rom_size; 86 }
87 read = romread(block, 1, SMD_BLOCK_SIZE, f);
88 if (read > 0) {
89 for (uint8_t *low = block, *high = (block+read/2), *end = block+read; high < end; high++, low++) {
90 *(dst++) = *low << 8 | *high;
91 }
92 readsize += read;
93 }
94 } while(read > 0);
95 romclose(f);
96
97 *buffer = dst;
98
99 return readsize;
100 }
101
102 uint32_t load_rom_zip(char *filename, void **dst)
103 {
104 static const char *valid_exts[] = {"bin", "md", "gen", "sms", "rom"};
105 const uint32_t num_exts = sizeof(valid_exts)/sizeof(*valid_exts);
106 zip_file *z = zip_open(filename);
107 if (!z) {
108 return 0;
109 }
110
111 for (uint32_t i = 0; i < z->num_entries; i++)
112 {
113 char *ext = path_extension(z->entries[i].name);
114 if (!ext) {
115 continue;
116 }
117 for (uint32_t j = 0; j < num_exts; j++)
118 {
119 if (!strcasecmp(ext, valid_exts[j])) {
120 size_t out_size = nearest_pow2(z->entries[i].size);
121 *dst = zip_read(z, i, &out_size);
122 if (*dst) {
123 free(ext);
124 zip_close(z);
125 return out_size;
126 }
127 }
128 }
129 free(ext);
130 }
131 zip_close(z);
132 return 0;
69 } 133 }
70 134
71 uint32_t load_rom(char * filename, void **dst, system_type *stype) 135 uint32_t load_rom(char * filename, void **dst, system_type *stype)
72 { 136 {
73 uint8_t header[10]; 137 uint8_t header[10];
74 FILE * f = fopen(filename, "rb"); 138 char *ext = path_extension(filename);
139 if (!strcasecmp(ext, "zip")) {
140 free(ext);
141 return load_rom_zip(filename, dst);
142 }
143 free(ext);
144 ROMFILE f = romopen(filename, "rb");
75 if (!f) { 145 if (!f) {
76 return 0; 146 return 0;
77 } 147 }
78 if (sizeof(header) != fread(header, 1, sizeof(header), f)) { 148 if (sizeof(header) != romread(header, 1, sizeof(header), f)) {
79 fatal_error("Error reading from %s\n", filename); 149 fatal_error("Error reading from %s\n", filename);
80 } 150 }
81 fseek(f, 0, SEEK_END); 151
82 long filesize = ftell(f);
83 fseek(f, 0, SEEK_SET);
84 if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) { 152 if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) {
85 int i; 153 int i;
86 for (i = 3; i < 8; i++) { 154 for (i = 3; i < 8; i++) {
87 if (header[i] != 0) { 155 if (header[i] != 0) {
88 break; 156 break;
93 fatal_error("%s is a split SMD ROM which is not currently supported", filename); 161 fatal_error("%s is a split SMD ROM which is not currently supported", filename);
94 } 162 }
95 if (stype) { 163 if (stype) {
96 *stype = SYSTEM_GENESIS; 164 *stype = SYSTEM_GENESIS;
97 } 165 }
98 return load_smd_rom(filesize, f, dst); 166 return load_smd_rom(f, dst);
99 } 167 }
100 } 168 }
101 *dst = malloc(nearest_pow2(filesize)); 169
102 if (filesize != fread(*dst, 1, filesize, f)) { 170 size_t filesize = 512 * 1024;
103 fatal_error("Error reading from %s\n", filename); 171 size_t readsize = sizeof(header);
104 } 172
105 fclose(f); 173 char *buf = malloc(filesize);
106 return filesize; 174 memcpy(buf, header, readsize);
175
176 size_t read;
177 do {
178 read = romread(buf + readsize, 1, filesize - readsize, f);
179 if (read > 0) {
180 readsize += read;
181 if (readsize == filesize) {
182 int one_more = romgetc(f);
183 if (one_more >= 0) {
184 filesize *= 2;
185 buf = realloc(buf, filesize);
186 buf[readsize++] = one_more;
187 } else {
188 read = 0;
189 }
190 }
191 }
192 } while (read > 0);
193
194 *dst = buf;
195
196 romclose(f);
197 return readsize;
107 } 198 }
108 199
109 200
110 201
111 int break_on_sync = 0; 202 int break_on_sync = 0;