From 6224a0d283261f71f32f0a915b24c708b93d37bf Mon Sep 17 00:00:00 2001
From: Robin Krens <robin@robinkrens.nl>
Date: Sun, 12 Jun 2022 15:40:48 +0200
Subject: [PATCH] tools: reorganize dirs

---
 tools/romheader/Makefile    |  5 +++
 tools/romheader/romheader.c | 83 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+)
 create mode 100644 tools/romheader/Makefile
 create mode 100644 tools/romheader/romheader.c

diff --git a/tools/romheader/Makefile b/tools/romheader/Makefile
new file mode 100644
index 0000000..2d1a03a
--- /dev/null
+++ b/tools/romheader/Makefile
@@ -0,0 +1,5 @@
+all:
+	cc -O2 romheader.c -o romheader
+
+clean:
+	rm romheader
diff --git a/tools/romheader/romheader.c b/tools/romheader/romheader.c
new file mode 100644
index 0000000..0b23559
--- /dev/null
+++ b/tools/romheader/romheader.c
@@ -0,0 +1,83 @@
+/**
+ * File              : romheader.c
+ * Author            : Robin Krens <robin@robinkrens.nl>
+ * Date              : 04.06.2022
+ * Last Modified Date: 04.06.2022
+ * Last Modified By  : Robin Krens <robin@robinkrens.nl>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#define VERBOSE		1
+#define SEGMENT_SIZE	0x10000
+
+/* simple tool to pad 0xFF and to add ROM header
+ * romheader structure is as follows: */
+
+struct __attribute__((__packed__)) rom_header {
+	uint8_t jmpf;
+	uint16_t label;
+	uint16_t segment;
+	uint8_t padding;
+	uint8_t dev_id;
+	uint8_t ws_type;
+	uint8_t cart_nr;
+	uint8_t padding2;
+	uint8_t romsize;
+	uint8_t ramsize;
+	uint8_t wsspec;
+	uint8_t padding3;
+	uint16_t checksum;
+};
+
+static struct rom_header header = {
+	.jmpf = 0xEA,
+	.label = 0x0,
+	.segment = 0xF000,
+	.dev_id = 0x42,
+	.ws_type = 0x01,
+	.cart_nr = 0x01,
+	.romsize = 0x03,
+	.ramsize = 0x00,
+	.wsspec = 0x04,
+	.checksum = 0x0000
+};
+
+
+int main(void)
+{
+	FILE *fp;
+	/* open for appending, so seek postion is at end */
+	fp = fopen("test.wsc", "a");
+	if (fp == NULL) {
+		perror("can not open file");
+		exit(EXIT_FAILURE);
+	}
+
+	unsigned pos = ftell(fp);
+	int gap = (SEGMENT_SIZE - pos) - sizeof(struct rom_header);
+	if (gap <= 0)
+		goto cleanup;
+
+	char dummy[SEGMENT_SIZE + 1];
+	memset(dummy, 0xFF, SEGMENT_SIZE);
+
+	unsigned wc = fwrite(dummy, sizeof(char), gap, fp);
+	wc += fwrite(&header, sizeof(char), sizeof(struct rom_header), fp);
+	
+	if (VERBOSE) {
+		printf("size of struct: %ld\n", sizeof(struct rom_header));
+		printf("pos: %d, gap to fill: %d\n", pos, gap);
+		printf("written %d\n", wc);
+	}
+
+cleanup:
+
+	printf("done\n");
+	fflush(fp);
+	fclose(fp);
+}
+
-- 
2.7.4