Mercurial > repos > blastem
comparison blastem.c @ 1529:f7fe240a7da6
Updated fibonacci benchmark code to work with current test harness
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 23 Mar 2018 22:23:39 -0700 |
parents | 4929325c3ce0 |
children | 092675db4f37 |
comparison
equal
deleted
inserted
replaced
1519:1f745318f10a | 1529:f7fe240a7da6 |
---|---|
45 #define SMD_MAGIC1 0x03 | 45 #define SMD_MAGIC1 0x03 |
46 #define SMD_MAGIC2 0xAA | 46 #define SMD_MAGIC2 0xAA |
47 #define SMD_MAGIC3 0xBB | 47 #define SMD_MAGIC3 0xBB |
48 #define SMD_BLOCK_SIZE 0x4000 | 48 #define SMD_BLOCK_SIZE 0x4000 |
49 | 49 |
50 int load_smd_rom(long filesize, FILE * f, void **buffer) | 50 #ifdef DISABLE_ZLIB |
51 #define ROMFILE FILE* | |
52 #define romopen fopen | |
53 #define romread fread | |
54 #define romseek fseek | |
55 #define romgetc fgetc | |
56 #define romclose fclose | |
57 #else | |
58 #include "zlib/zlib.h" | |
59 #define ROMFILE gzFile | |
60 #define romopen gzopen | |
61 #define romread gzfread | |
62 #define romseek gzseek | |
63 #define romgetc gzgetc | |
64 #define romclose gzclose | |
65 #endif | |
66 | |
67 int load_smd_rom(ROMFILE f, void **buffer) | |
51 { | 68 { |
52 uint8_t block[SMD_BLOCK_SIZE]; | 69 uint8_t block[SMD_BLOCK_SIZE]; |
53 filesize -= SMD_HEADER_SIZE; | 70 romseek(f, SMD_HEADER_SIZE, SEEK_SET); |
54 fseek(f, SMD_HEADER_SIZE, SEEK_SET); | 71 |
55 | 72 size_t filesize = 512 * 1024; |
56 uint16_t *dst = *buffer = malloc(nearest_pow2(filesize)); | 73 size_t readsize = 0; |
57 int rom_size = filesize; | 74 uint16_t *dst = malloc(filesize); |
58 while (filesize > 0) { | 75 |
59 fread(block, 1, SMD_BLOCK_SIZE, f); | 76 |
60 for (uint8_t *low = block, *high = (block+SMD_BLOCK_SIZE/2), *end = block+SMD_BLOCK_SIZE; high < end; high++, low++) { | 77 size_t read; |
61 *(dst++) = *low << 8 | *high; | 78 do { |
62 } | 79 if ((readsize + SMD_BLOCK_SIZE > filesize)) { |
63 filesize -= SMD_BLOCK_SIZE; | 80 filesize *= 2; |
64 } | 81 dst = realloc(dst, filesize); |
65 return rom_size; | 82 } |
83 read = romread(block, 1, SMD_BLOCK_SIZE, f); | |
84 if (read > 0) { | |
85 for (uint8_t *low = block, *high = (block+read/2), *end = block+read; high < end; high++, low++) { | |
86 *(dst++) = *low << 8 | *high; | |
87 } | |
88 readsize += read; | |
89 } | |
90 } while(read > 0); | |
91 romclose(f); | |
92 | |
93 *buffer = dst; | |
94 | |
95 return readsize; | |
66 } | 96 } |
67 | 97 |
68 uint32_t load_rom(char * filename, void **dst, system_type *stype) | 98 uint32_t load_rom(char * filename, void **dst, system_type *stype) |
69 { | 99 { |
70 uint8_t header[10]; | 100 uint8_t header[10]; |
71 FILE * f = fopen(filename, "rb"); | 101 ROMFILE f = romopen(filename, "rb"); |
72 if (!f) { | 102 if (!f) { |
73 return 0; | 103 return 0; |
74 } | 104 } |
75 if (sizeof(header) != fread(header, 1, sizeof(header), f)) { | 105 if (sizeof(header) != romread(header, 1, sizeof(header), f)) { |
76 fatal_error("Error reading from %s\n", filename); | 106 fatal_error("Error reading from %s\n", filename); |
77 } | 107 } |
78 fseek(f, 0, SEEK_END); | 108 |
79 long filesize = ftell(f); | |
80 fseek(f, 0, SEEK_SET); | |
81 if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) { | 109 if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) { |
82 int i; | 110 int i; |
83 for (i = 3; i < 8; i++) { | 111 for (i = 3; i < 8; i++) { |
84 if (header[i] != 0) { | 112 if (header[i] != 0) { |
85 break; | 113 break; |
90 fatal_error("%s is a split SMD ROM which is not currently supported", filename); | 118 fatal_error("%s is a split SMD ROM which is not currently supported", filename); |
91 } | 119 } |
92 if (stype) { | 120 if (stype) { |
93 *stype = SYSTEM_GENESIS; | 121 *stype = SYSTEM_GENESIS; |
94 } | 122 } |
95 return load_smd_rom(filesize, f, dst); | 123 return load_smd_rom(f, dst); |
96 } | 124 } |
97 } | 125 } |
98 *dst = malloc(nearest_pow2(filesize)); | 126 |
99 if (filesize != fread(*dst, 1, filesize, f)) { | 127 size_t filesize = 512 * 1024; |
100 fatal_error("Error reading from %s\n", filename); | 128 size_t readsize = sizeof(header); |
101 } | 129 |
102 fclose(f); | 130 char *buf = malloc(filesize); |
103 return filesize; | 131 memcpy(buf, header, readsize); |
132 | |
133 size_t read; | |
134 do { | |
135 read = romread(buf + readsize, 1, filesize - readsize, f); | |
136 if (read > 0) { | |
137 readsize += read; | |
138 if (readsize == filesize) { | |
139 int one_more = romgetc(f); | |
140 if (one_more >= 0) { | |
141 filesize *= 2; | |
142 buf = realloc(buf, filesize); | |
143 buf[readsize++] = one_more; | |
144 } else { | |
145 read = 0; | |
146 } | |
147 } | |
148 } | |
149 } while (read > 0); | |
150 | |
151 *dst = buf; | |
152 | |
153 romclose(f); | |
154 return readsize; | |
104 } | 155 } |
105 | 156 |
106 | 157 |
107 | 158 |
108 int break_on_sync = 0; | 159 int break_on_sync = 0; |