Add creation of .dat file by writing header and few machine codes
[zilutils.git] / zilasm / main.c
index f0f335b61e8c5fb3768d1eb1c53cb836dbaae1de..b60233930a32f83ea2b8c7132654f14157909aa8 100644 (file)
@@ -2,7 +2,7 @@
  * main.c
  *
  * Copyright (C) 2015 Alexander Andrejevic <theflash AT sdf DOT lonestar DOT org>
- * Copyright (C) 2015 Jason Self <j@jxself.org>
+ * Copyright (C) 2015, 2019 Jason Self <j@jxself.org>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
@@ -30,6 +30,8 @@
 
 #include "config.h"
 
+#include "header.h"
+
 const int DEFAULT_ZVERSION = 6;
 
 enum { ZVERSION = 11, ZORKID, ZSERIAL };
@@ -176,9 +178,41 @@ int init_assembly(void)
        return OK;
 }
 
-int assembly(void)
+int assembly(char *output_file)
 {
        /* TODO */
+       FILE *file = fopen(output_file, "w");
+       if (file)
+       {
+               program_header_reset(6);
+
+               int size = sizeof(Program_header );
+               //Program_header.
+
+               Program_header.mode = 0; // DISPLAY_AVAILABLE | MONOSPACE_AVAILABLE;
+               Program_header.release = 1; // game version
+
+               Program_header.startPC = 64;
+
+               Program_header.dynamic_size = 128;
+               Program_header.h_file_size = 9;  //sizeof(Program_header) + zmb->used_size;
+
+               ZMemblock *zmb = zmem_init(Program_header.h_file_size * 8);
+
+               //fprintf(file, "%c", 178); // printi command
+               //fprintf(file, "hello, world!");
+
+               zmem_putbyte(zmb, 186); // quit command
+
+
+
+               outputToFile(&Program_header, file);
+
+               //fprintf(file, "%c", 186);
+               fwrite(zmb->contents, zmb->allocated_size, 1, file);
+               fclose(file);
+       }
+
        return OK;
 }
 
@@ -227,7 +261,7 @@ int main(int argc, char *argv[], char *envp[])
 
        init_opcodes(Config.zversion, 0);
 
-       while(init_assembly() == OK && assembly() == NEED_RESTART);
+       while(init_assembly() == OK && assembly(output_file) == NEED_RESTART);
 
        /* TODO! List global symbols */
        /* TODO! Find abbreviations */