From 0d172cc2c42f301c0afff70ab122bbd08ef0f443 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sat, 10 Oct 2020 13:28:36 -0400 Subject: [PATCH] tools: add smsrom Running a ROM on an everdrive is one thing, but running a ROM directly is another: my hacked up sega.bin didn't have a proper checksum, so the ROM didn't run. This new tool transforms a binary into a properly-headered ROM. Has been tested on an actual SMS. --- recipes/sms/Makefile | 6 +++-- recipes/sms/sega.bin | Bin 16 -> 0 bytes tools/.gitignore | 5 +--- tools/Makefile | 4 +++- tools/smsrom.c | 53 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 7 deletions(-) delete mode 100644 recipes/sms/sega.bin create mode 100644 tools/smsrom.c diff --git a/recipes/sms/Makefile b/recipes/sms/Makefile index f3d661c..3910b2d 100644 --- a/recipes/sms/Makefile +++ b/recipes/sms/Makefile @@ -2,6 +2,7 @@ TARGET = os.bin BASE = ../../ STAGE = $(BASE)/cvm/stage BLKPACK = $(BASE)/tools/blkpack +SMSROM = $(BASE)/tools/smsrom EMUL = $(BASE)/emul/hw/sms/sms .PHONY: all @@ -9,6 +10,7 @@ all: $(TARGET) $(TARGET): xcomp.fs $(STAGE) blkfs cat xcomp.fs | $(STAGE) blkfs > $@ +$(SMSROM): $(BLKPACK): $(MAKE) -C ../tools @@ -18,8 +20,8 @@ blkfs: $(BLKPACK) $(STAGE): $(MAKE) -C $(BASE)/cvm stage -os.sms: $(TARGET) $(STAGE) - dd if=$(TARGET) bs=32752 conv=sync | cat - sega.bin > $@ +os.sms: $(TARGET) $(STAGE) $(SMSROM) + $(SMSROM) $(TARGET) > $@ $(EMUL): $(MAKE) -C ${@:%/sms=%} diff --git a/recipes/sms/sega.bin b/recipes/sms/sega.bin deleted file mode 100644 index 9db7c7c94024620bb4a01d12b4949ba5f1127571..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16 XcmWIX4N?epb$4W7_?^MPz~BP_Bo71Q diff --git a/tools/.gitignore b/tools/.gitignore index f8ae9ea..a335747 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -1,13 +1,10 @@ /memdump /blkdump /upload -/fontcompile /ttysafe /pingpong -/slatest -/stripfc -/bin2c /exec /blkpack /blkunpack /blkup +/smsrom diff --git a/tools/Makefile b/tools/Makefile index 853a681..063ac7b 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -6,9 +6,10 @@ EXEC_TGT = exec BLKPACK_TGT = blkpack BLKUNPACK_TGT = blkunpack BLKUP_TGT = blkup +SMSROM_TGT = smsrom TARGETS = $(MEMDUMP_TGT) $(UPLOAD_TGT) \ $(TTYSAFE_TGT) $(PINGPONG_TGT) $(EXEC_TGT) $(BLKPACK_TGT) \ - $(BLKUNPACK_TGT) $(BLKUP_TGT) + $(BLKUNPACK_TGT) $(BLKUP_TGT) $(SMSROM_TGT) OBJS = common.o all: $(TARGETS) @@ -25,6 +26,7 @@ $(EXEC_TGT): $(EXEC_TGT).c $(BLKPACK_TGT): $(BLKPACK_TGT).c $(BLKUNPACK_TGT): $(BLKUNPACK_TGT).c $(BLKUP_TGT): $(BLKUP_TGT).c +$(SMSROM_TGT): $(SMSROM_TGT).c $(TARGETS): $(OBJS) $(CC) $(CFLAGS) $@.c $(OBJS) -o $@ diff --git a/tools/smsrom.c b/tools/smsrom.c new file mode 100644 index 0000000..77136d0 --- /dev/null +++ b/tools/smsrom.c @@ -0,0 +1,53 @@ +/* ./smsrom fname + +Transforms binary at fname into an 8K, 16K or 32K Sega Master System ROM with +a header fit for an Export SMS. The resulting ROM is spit to stdout. + +Whether the ROM is 8, 16 or 32K depends on the size of binary at fname. +*/ +#include +#include + +int main(int argc, char **argv) +{ + if (argc != 2) { + fprintf(stderr, "Usage: ./smsrom fname\n"); + return 1; + } + FILE *fp = fopen(argv[1], "r"); + if (!fp) { + fprintf(stderr, "Can't open %s.\n", argv[1]); + return 1; + } + fseek(fp, 0, SEEK_END); + long fsize = ftell(fp); + fseek(fp, 0, SEEK_SET); + uint8_t hdsz = 0x4a; // size flag in header. either 4a, 4b or 4c. + int romsize = 0x2000; + while (romsize-16 < fsize) { + romsize *= 2; + hdsz++; + if (romsize > 0x8000) { + fprintf(stderr, "binary too big\n"); + return 1; + } + } + uint16_t chksum = 0; + for (int i=0; i> 8); + putchar(0); + putchar(0); + putchar(0); + putchar(hdsz); + return 0; +}