diff 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
line wrap: on
line diff
--- a/blastem.c	Tue Dec 25 11:12:26 2018 -0800
+++ b/blastem.c	Thu Jan 24 19:15:59 2019 -0800
@@ -34,7 +34,7 @@
 #include "nuklear_ui/blastem_nuklear.h"
 #endif
 
-#define BLASTEM_VERSION "0.6.0-pre"
+#define BLASTEM_VERSION "0.6.2-pre"
 
 #ifdef __ANDROID__
 #define FULLSCREEN_DEFAULT 1
@@ -73,6 +73,14 @@
 #define romclose gzclose
 #endif
 
+uint16_t *process_smd_block(uint16_t *dst, uint8_t *src, size_t bytes)
+{
+	for (uint8_t *low = src, *high = (src+bytes/2), *end = src+bytes; high < end; high++, low++) {
+		*(dst++) = *low << 8 | *high;
+	}
+	return dst;
+}
+
 int load_smd_rom(ROMFILE f, void **buffer)
 {
 	uint8_t block[SMD_BLOCK_SIZE];
@@ -80,33 +88,52 @@
 
 	size_t filesize = 512 * 1024;
 	size_t readsize = 0;
-	uint16_t *dst = malloc(filesize);
+	uint16_t *dst, *buf;
+	dst = buf = malloc(filesize);
 	
 
 	size_t read;
 	do {
 		if ((readsize + SMD_BLOCK_SIZE > filesize)) {
 			filesize *= 2;
-			dst = realloc(dst, filesize);
+			buf = realloc(buf, filesize);
+			dst = buf + readsize/sizeof(uint16_t);
 		}
 		read = romread(block, 1, SMD_BLOCK_SIZE, f);
 		if (read > 0) {
-			for (uint8_t *low = block, *high = (block+read/2), *end = block+read; high < end; high++, low++) {
-				*(dst++) = *low << 8 | *high;
-			}
+			dst = process_smd_block(dst, block, read);
 			readsize += read;
 		}
 	} while(read > 0);
 	romclose(f);
 	
-	*buffer = dst;
+	*buffer = buf;
 	
 	return readsize;
 }
 
+uint8_t is_smd_format(const char *filename, uint8_t *header)
+{
+	if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) {
+		int i;
+		for (i = 3; i < 8; i++) {
+			if (header[i] != 0) {
+				return 0;
+			}
+		}
+		if (i == 8) {
+			if (header[2]) {
+				fatal_error("%s is a split SMD ROM which is not currently supported", filename);
+			}
+			return 1;
+		}
+	}
+	return 0;
+}
+
 uint32_t load_rom_zip(const char *filename, void **dst)
 {
-	static const char *valid_exts[] = {"bin", "md", "gen", "sms", "rom"};
+	static const char *valid_exts[] = {"bin", "md", "gen", "sms", "rom", "smd"};
 	const uint32_t num_exts = sizeof(valid_exts)/sizeof(*valid_exts);
 	zip_file *z = zip_open(filename);
 	if (!z) {
@@ -125,6 +152,16 @@
 				size_t out_size = nearest_pow2(z->entries[i].size);
 				*dst = zip_read(z, i, &out_size);
 				if (*dst) {
+					if (is_smd_format(z->entries[i].name, *dst)) {
+						size_t offset;
+						for (offset = 0; offset + SMD_BLOCK_SIZE + SMD_HEADER_SIZE < out_size; offset += SMD_BLOCK_SIZE)
+						{
+							uint8_t tmp[SMD_BLOCK_SIZE];
+							memcpy(tmp, *dst + offset + SMD_HEADER_SIZE, SMD_BLOCK_SIZE);
+							process_smd_block(*dst + offset, tmp, SMD_BLOCK_SIZE);
+						}
+						out_size = offset;
+					}
 					free(ext);
 					zip_close(z);
 					return out_size;
@@ -141,7 +178,7 @@
 {
 	uint8_t header[10];
 	char *ext = path_extension(filename);
-	if (!strcasecmp(ext, "zip")) {
+	if (ext && !strcasecmp(ext, "zip")) {
 		free(ext);
 		return load_rom_zip(filename, dst);
 	}
@@ -154,22 +191,11 @@
 		fatal_error("Error reading from %s\n", filename);
 	}
 	
-	if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) {
-		int i;
-		for (i = 3; i < 8; i++) {
-			if (header[i] != 0) {
-				break;
-			}
+	if (is_smd_format(filename, header)) {
+		if (stype) {
+			*stype = SYSTEM_GENESIS;
 		}
-		if (i == 8) {
-			if (header[2]) {
-				fatal_error("%s is a split SMD ROM which is not currently supported", filename);
-			}
-			if (stype) {
-				*stype = SYSTEM_GENESIS;
-			}
-			return load_smd_rom(f, dst);
-		}
+		return load_smd_rom(f, dst);
 	}
 	
 	size_t filesize = 512 * 1024;
@@ -581,7 +607,7 @@
 	uint8_t menu = !loaded;
 	uint8_t use_nuklear = 0;
 #ifndef DISABLE_NUKLEAR
-	use_nuklear = is_nuklear_available();
+	use_nuklear = !headless && is_nuklear_available();
 #endif
 	if (!loaded && !use_nuklear) {
 		//load menu