changeset 29:5a8b5f9fc50a

Added incbin directive to assembler
author Michael Pavone <pavone@retrodev.com>
date Sun, 03 Apr 2016 18:37:01 -0700
parents c677507682e3
children 78068060313a
files src/asm.c
diffstat 1 files changed, 30 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/asm.c	Sun Apr 03 00:33:54 2016 -0700
+++ b/src/asm.c	Sun Apr 03 18:37:01 2016 -0700
@@ -395,6 +395,30 @@
 	return 1;
 }
 
+uint8_t handle_incbin(char *cur, uint8_t *outbuf, uint16_t *pc)
+{
+	char * end = strchr(cur, ';');
+	if (end) {
+		*end = 0;
+	} else {
+		end = cur + strlen(cur);
+	}
+	while (end - cur > 1 && isspace(*(end-1)))
+	{
+		end--;
+	}
+	*end = 0;
+	FILE *incfile = fopen(cur, "rb");
+	if (!incfile) {
+		fprintf(stderr, "Failed to open inclued file %s for reading\n", cur);
+		return 0;
+	}
+	size_t read = fread(outbuf + *pc, 1, 48*1024-*pc, incfile);
+	fclose(incfile);
+	*pc += read;
+	return 1;
+}
+
 uint8_t assemble_file(FILE *input, FILE *output)
 {
 	//fixed size buffers are lame, but so are lines longer than 4K characters
@@ -476,6 +500,12 @@
 			}
 			continue;
 		}
+		if (!strcmp(mnemonic, "incbin")) {
+			if (!handle_incbin(cur, outbuf, &pc)) {
+				goto error;
+			}
+			continue;
+		}
 		//automatically align to word boundary
 		if (pc & 1) {
 			outbuf[pc] = 0;