From: Jason Self Date: Thu, 23 May 2019 02:00:21 +0000 (-0700) Subject: Add creation of .dat file by writing header and few machine codes X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=37d32bd49e745a5c1686b6495f60172b24222361;p=zilutils.git Add creation of .dat file by writing header and few machine codes --- diff --git a/zilasm/header.c b/zilasm/header.c index 54ce75e..e5f350f 100644 --- a/zilasm/header.c +++ b/zilasm/header.c @@ -1,7 +1,7 @@ /* * header.c -- part of ZilUtils/ZilAsm * - * Copyright (C) 2016 Jason Self + * Copyright (C) 2016, 2019 Jason Self * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -21,6 +21,7 @@ #include /* bzero */ #include +#include #include "header.h" @@ -41,3 +42,20 @@ ZMemblock *program_header_build(void) /* TODO */ return zmb; } + + +Word swapBytes(Word w) +{ + return ((w & 255) << 8) + (w >> 8); +} + + +void outputToFile(Program_header_struct *pHeader, FILE* file) +{ + Program_header_struct h; + memcpy(&h, pHeader, sizeof(Program_header_struct)); + + h.startPC = swapBytes(h.startPC); + h.h_file_size = swapBytes(h.h_file_size); + fwrite(&h, sizeof(Program_header_struct), 1, file); +} \ No newline at end of file diff --git a/zilasm/header.h b/zilasm/header.h index 28ab595..41f82fb 100644 --- a/zilasm/header.h +++ b/zilasm/header.h @@ -1,7 +1,7 @@ /* * header.h -- part of ZilUtils/ZilAsm * - * Copyright (C) 2016 Jason Self + * Copyright (C) 2016, 2019 Jason Self * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -28,67 +28,113 @@ typedef unsigned long Byte_address; typedef unsigned long Word_address; typedef unsigned long Packed_address; typedef unsigned long Offset8; -typedef unsigned int Word; -typedef signed char Byte; +typedef unsigned short Word; +typedef unsigned char Byte; typedef int Bool; +//typedef struct { +// unsigned version; // 1..6 +// // [$01] V1..V3: Flags1 +// Bool statusline_type; // bit1: 0=score/turns, 1=hh:mm +// Bool split_two_discs; // bit2 +// Bool statusline_notavail; // bit4 +// Bool screensplit_avail; // bit5 +// Bool varpitchfont_default; // bit6 +// // [$01] V4: Flags1 +// Bool colors_avail; // v5: bit0 +// Bool pics_avail; // v6: bit1 +// Bool bold_avail; // v4: bit2 +// Bool italic_avail; // v4: bit3 +// Bool fixedspace_avail; // v4: bit4 +// Bool sound_avail; // v6: bit5 +// Bool timedkeyb_avail; // v4: bit7 +// // Addresses +// Byte_address highmem_base; // [$04] +// Byte_address start_pc; // [$06], v1 +// Packed_address start_routine; // [$06], v6 +// Byte_address dictionary; // [$08] +// Byte_address objects; // [$0A] +// Byte_address globals; // [$0C] +// Byte_address static_base; // [$0E] +// // [$10] Flags2 +// Bool transcript_on; // v1: bit0 +// Bool print_fixedfont; // v3: bit1 +// Bool request_redraw; // v6: bit2 +// Bool want_pics; // v5: bit3 +// Bool want_undo; // v5: bit4 +// Bool want_mouse; // v5: bit5 +// Bool want_colors; // v5: bit6 +// Bool want_sound; // v5: bit7 +// Bool want_menus; // v5: bit8 +// // +// Byte_address abbrevs; // [$18], v2 +// Word file_length; // [$1A], v3 +// Word checksum; // [$1C], v3 +// Byte interpreter_number; // [$1E], v4 +// Byte interpreter_version; // [$1F], v4 +// // Screen +// Byte screen_lines; // [$20], v4 ($FF = infinite) +// Byte screen_chars; // [$21], v4 +// Word screen_width; // [$22], v5 +// Word screen_height; // [$24], v5 +// Byte font_width; // [$26], v5/v6 +// Byte font_height; // [$27], v5/v6 +// // Tables +// Offset8 routines; // [$28], v6 +// Offset8 strings; // [$2A], v6 +// Byte backcolor; // [$2C], v5 +// Byte forecolor; // [$2D], v5 +// Byte_address term_chartable; // [$2E], v5 +// Word width3; // [$30], v6 +// Word revnumber; // [$32], v1 +// Byte_address alphabet; // [$34], v5 +// Byte_address header_ext; // [$36], v5 +//} Program_header_struct; + + +enum MODE_FLAGS +{ + COLOR_AVAILABLE = 0, + DISPLAY_AVAILABLE = 1, + BOLD_AVAILABLE = 2, + ITALIC_AVAILABLE = 4, + MONOSPACE_AVAILABLE = 8 +}; + typedef struct { - unsigned version; // 1..6 - // [$01] V1..V3: Flags1 - Bool statusline_type; // bit1: 0=score/turns, 1=hh:mm - Bool split_two_discs; // bit2 - Bool statusline_notavail; // bit4 - Bool screensplit_avail; // bit5 - Bool varpitchfont_default; // bit6 - // [$01] V4: Flags1 - Bool colors_avail; // v5: bit0 - Bool pics_avail; // v6: bit1 - Bool bold_avail; // v4: bit2 - Bool italic_avail; // v4: bit3 - Bool fixedspace_avail; // v4: bit4 - Bool sound_avail; // v6: bit5 - Bool timedkeyb_avail; // v4: bit7 - // Addresses - Byte_address highmem_base; // [$04] - Byte_address start_pc; // [$06], v1 - Packed_address start_routine; // [$06], v6 - Byte_address dictionary; // [$08] - Byte_address objects; // [$0A] - Byte_address globals; // [$0C] - Byte_address static_base; // [$0E] - // [$10] Flags2 - Bool transcript_on; // v1: bit0 - Bool print_fixedfont; // v3: bit1 - Bool request_redraw; // v6: bit2 - Bool want_pics; // v5: bit3 - Bool want_undo; // v5: bit4 - Bool want_mouse; // v5: bit5 - Bool want_colors; // v5: bit6 - Bool want_sound; // v5: bit7 - Bool want_menus; // v5: bit8 - // - Byte_address abbrevs; // [$18], v2 - Word file_length; // [$1A], v3 - Word checksum; // [$1C], v3 - Byte interpreter_number; // [$1E], v4 - Byte interpreter_version; // [$1F], v4 - // Screen - Byte screen_lines; // [$20], v4 ($FF = infinite) - Byte screen_chars; // [$21], v4 - Word screen_width; // [$22], v5 - Word screen_height; // [$24], v5 - Byte font_width; // [$26], v5/v6 - Byte font_height; // [$27], v5/v6 - // Tables - Offset8 routines; // [$28], v6 - Offset8 strings; // [$2A], v6 - Byte backcolor; // [$2C], v5 - Byte forecolor; // [$2D], v5 - Byte_address term_chartable; // [$2E], v5 - Word width3; // [$30], v6 - Word revnumber; // [$32], v1 - Byte_address alphabet; // [$34], v5 - Byte_address header_ext; // [$36], v5 + Byte version; // 1..6 + Byte mode; + Word release; + Word resident_size; + Word startPC; + Word dictionaryTableOffset; + Word objectsTableOffset; + Word globalsTableOffset; + Word dynamic_size; + Word flags; + unsigned char serial[6]; + Word h_abbreviations; + Word h_file_size; + Word checkSum; + unsigned char interpreterNumber; + unsigned char interpreterVersion; + unsigned char H_SCREEN_ROWS; + unsigned char H_SCREEN_COLS; + Word H_SCREEN_WIDTH; + Word H_SCREEN_HEIGHT; + unsigned char H_FONT_HEIGHT; /* this is the font width in V5 */ + unsigned char H_FONT_WIDTH; /* this is the font height in V5 */ + Word H_FUNCTIONS_OFFSET; + Word H_STRINGS_OFFSET; + unsigned char H_DEFAULT_BACKGROUND; + unsigned char H_DEFAULT_FOREGROUND; + Word H_TERMINATING_KEYS; + Word H_LINE_WIDTH; + unsigned char H_STANDARD_HIGH; + unsigned char H_STANDARD_LOW; + Word H_ALPHABET; + Word H_EXTENSION_TABLE; + char USER_NAME[8]; } Program_header_struct; extern Program_header_struct Program_header; @@ -96,4 +142,15 @@ extern Program_header_struct Program_header; void program_header_reset(unsigned zversion); ZMemblock *program_header_build(void); +typedef struct { + Byte numberOfSelfinseringCharacters; + Byte *pSymbols; + Byte numberOfBytesInEachEntry; + +} Program_dictionary_struct ; + + + +void outputToFile(Program_header_struct *h, FILE* file); + #endif /* ifndef ZILASM_HEADER */ diff --git a/zilasm/main.c b/zilasm/main.c index f0f335b..b602339 100644 --- a/zilasm/main.c +++ b/zilasm/main.c @@ -2,7 +2,7 @@ * main.c * * Copyright (C) 2015 Alexander Andrejevic - * Copyright (C) 2015 Jason Self + * Copyright (C) 2015, 2019 Jason Self * * 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 */ diff --git a/zilasm/parser.c b/zilasm/parser.c index 61038a4..81713cc 100644 --- a/zilasm/parser.c +++ b/zilasm/parser.c @@ -1,7 +1,7 @@ /* * parser.c -- part of ZilUtils/ZilAsm * - * Copyright (C) 2016 Jason Self + * Copyright (C) 2016, 2019 Jason Self * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -124,12 +124,14 @@ int parse_line(const char *p) return 0; } +#define MAX_LINESIZE 1024 + int parse_file(const char *filename) { FILE *fp = fopen(filename, "r"); if (!fp) fatal_error("wrong file"); - const int MAX_LINESIZE = 1024; + //const int MAX_LINESIZE = 1024; char line[MAX_LINESIZE]; int newline_missing = 0;