summaryrefslogtreecommitdiff
path: root/mknes_ines2.c
diff options
context:
space:
mode:
authorPeter Fors <peter.fors@mindkiller.com>2025-10-25 21:56:37 +0200
committerPeter Fors <peter.fors@mindkiller.com>2025-10-25 21:56:37 +0200
commit396b10dd5e206462ebeab5fed368ba3ae25c6a51 (patch)
tree9d3c759298de673fb6d778b8bbeae4bec2174a9b /mknes_ines2.c
parenta4c261c6ee3940099e653a6f448dc952dfd5899f (diff)
Better benchmarking, some small optimizations
Diffstat (limited to 'mknes_ines2.c')
-rw-r--r--mknes_ines2.c59
1 files changed, 37 insertions, 22 deletions
diff --git a/mknes_ines2.c b/mknes_ines2.c
index 29ceb71..686eb38 100644
--- a/mknes_ines2.c
+++ b/mknes_ines2.c
@@ -1,9 +1,11 @@
+#ifndef BENCHMARK
#define USE_LIBARCHIVE
#ifdef USE_LIBARCHIVE
#include <archive.h>
#include <archive_entry.h>
#endif
+#endif
// iNES header fields
#define INES_HEADER_SIZE 16
@@ -30,7 +32,7 @@
#define MIRROR_VERTICAL 1
#define MIRROR_FOUR_SCREEN 2
-
+#ifndef BENCHMARK
static uint8_t *ines2_read_entire_file(const char *path, size_t *out_size) {
FILE *f = fopen(path, "rb");
if(!f) return 0;
@@ -99,32 +101,16 @@ uint8_t *ines2_unzip_file_to_memory(const char *zip_path, size_t *out_size) {
return buffer;
}
#endif
-
-static int ines2_load(struct nes_state *state, const char *path) {
- uint8_t *data = 0;
- size_t size = 0;
-
-#ifdef USE_LIBARCHIVE
- if(strstr(path, ".zip")) {
- data = ines2_unzip_file_to_memory(path, &size);
- } else {
- data = ines2_read_entire_file(path, &size);
- }
-#else
- if(strstr(path, ".zip")) {
- fprintf(stderr, "ZIP support not compiled in. Please use .nes files directly.\n");
- return -1;
- }
- data = ines2_read_entire_file(path, &size);
#endif
+// Load ROM from memory buffer (for embedded ROMs)
+static int ines2_load_from_memory(struct nes_state *state, const uint8_t *data, size_t size) {
if(!data || size < INES_HEADER_SIZE) {
- free(data);
return -1;
}
- uint8_t *ptr = data;
- uint8_t *header = ptr; ptr += INES_HEADER_SIZE;
+ const uint8_t *ptr = data;
+ const uint8_t *header = ptr; ptr += INES_HEADER_SIZE;
uint8_t prg_lsb = header[INES_PRG_SIZE_LSB];
uint8_t chr_lsb = header[INES_CHR_SIZE_LSB];
@@ -168,9 +154,38 @@ static int ines2_load(struct nes_state *state, const char *path) {
memcpy(state->chr_rom, ptr, chr_size);
}
- free(data);
return 0;
}
+#ifndef BENCHMARK
+static int ines2_load(struct nes_state *state, const char *path) {
+ uint8_t *data = 0;
+ size_t size = 0;
+
+#ifdef USE_LIBARCHIVE
+ if(strstr(path, ".zip")) {
+ data = ines2_unzip_file_to_memory(path, &size);
+ } else {
+ data = ines2_read_entire_file(path, &size);
+ }
+#else
+ if(strstr(path, ".zip")) {
+ fprintf(stderr, "ZIP support not compiled in. Please use .nes files directly.\n");
+ return -1;
+ }
+ data = ines2_read_entire_file(path, &size);
+#endif
+
+ if(!data || size < INES_HEADER_SIZE) {
+ free(data);
+ return -1;
+ }
+
+ int result = ines2_load_from_memory(state, data, size);
+ free(data);
+ return result;
+}
+#endif
+