diff lwlink/output.c @ 187:857cb407229e

Added LWEX0 (LWOS simple binary) target to lwlink
author lost
date Sun, 22 Mar 2009 04:24:39 +0000
parents 302b8db5fd89
children bae1e3ecdce1
line wrap: on
line diff
--- a/lwlink/output.c	Sat Mar 21 19:49:01 2009 +0000
+++ b/lwlink/output.c	Sun Mar 22 04:24:39 2009 +0000
@@ -27,6 +27,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "lwlink.h"
 
@@ -37,6 +38,7 @@
 
 void do_output_decb(FILE *of);
 void do_output_raw(FILE *of);
+void do_output_lwex0(FILE *of);
 
 void do_output(void)
 {
@@ -59,6 +61,10 @@
 	case OUTPUT_RAW:
 		do_output_raw(of);
 		break;
+
+	case OUTPUT_LWEX0:
+		do_output_lwex0(of);
+		break;
 	
 	default:
 		fprintf(stderr, "Unknown output format doing output!\n");
@@ -152,3 +158,62 @@
 		writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
 	}
 }
+
+void do_output_lwex0(FILE *of)
+{
+	int nskips = 0;		// used to output blanks for BSS inline
+	int sn;
+	int codedatasize = 0;
+	unsigned char buf[32];
+	
+	// calculate items for the file header
+	for (sn = 0; sn < nsects; sn++)
+	{
+		if (sectlist[sn].ptr -> flags & SECTION_BSS)
+		{
+			// no output for a BSS section
+			nskips += sectlist[sn].ptr -> codesize;
+			continue;
+		}
+		codedatasize += nskips;
+		nskips = 0;
+		codedatasize += sectlist[sn].ptr -> codesize;
+	}
+
+	// output the file header
+	buf[0] = 'L';
+	buf[1] = 'W';
+	buf[2] = 'E';
+	buf[3] = 'X';
+	buf[4] = 0;		// version 0
+	buf[5] = 0;		// low stack
+	buf[6] = linkscript.stacksize / 256;
+	buf[7] = linkscript.stacksize & 0xff;
+	buf[8] = nskips / 256;
+	buf[9] = nskips & 0xff;
+	buf[10] = codedatasize / 256;
+	buf[11] = codedatasize & 0xff;
+	buf[12] = linkscript.execaddr / 256;
+	buf[13] = linkscript.execaddr & 0xff;
+	memset(buf + 14, 0, 18);
+	
+	writebytes(buf, 1, 32, of);
+	// output the data
+	// NOTE: disjoint load addresses will not work correctly!!!!!
+	for (sn = 0; sn < nsects; sn++)
+	{
+		if (sectlist[sn].ptr -> flags & SECTION_BSS)
+		{
+			// no output for a BSS section
+			nskips += sectlist[sn].ptr -> codesize;
+			continue;
+		}
+		while (nskips > 0)
+		{
+			// the "" is not an error - it turns into a single NUL byte!
+			writebytes("", 1, 1, of);
+			nskips--;
+		}
+		writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
+	}
+}