From 90c9a148a94ed92c93d42110e45d8787f7ac1eff Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Mon, 12 Dec 2011 00:11:34 +0100 Subject: initial import --- assembler.vala | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 assembler.vala (limited to 'assembler.vala') diff --git a/assembler.vala b/assembler.vala new file mode 100644 index 0000000..e44ceb8 --- /dev/null +++ b/assembler.vala @@ -0,0 +1,140 @@ +/* assembler.vala + * Copyright 2011, Sebastian Reichel + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +const OptionEntry[] option_entries = { + {"assemble", 'a', OptionFlags.NO_ARG, OptionArg.CALLBACK, (void*) parse_mode, "use assemble mode", null}, + {"disassemble", 'd', OptionFlags.NO_ARG, OptionArg.CALLBACK, (void*) parse_mode, "use disassemble mode", null}, + { "", 0, 0, OptionArg.FILENAME_ARRAY, ref files, "input file", "FILE" }, + {null} +}; + +enum mode { + ASSEMBLE, + DISASSEMBLE +} + +static string[] files; +static mode m; + +bool parse_mode(string key, string? val) throws OptionError { + switch(key) { + case "--assemble": + case "-a": + m = mode.ASSEMBLE; + return true; + case "--disassemble": + case "-d": + m = mode.DISASSEMBLE; + return true; + default: + throw new OptionError.UNKNOWN_OPTION("Unknown Option " + key); + } +} + +string assemble(FileStream stream) throws lp5523.ParsingError { + string line; + string result = ""; + + /* read until EOF */ + while((line = stream.read_line()) != null) { + /* skip comment lines */ + line = line.strip(); + if(line.has_prefix("#")) + continue; + + /* remove inline comments */ + line = line.split("#", 2)[0]; + line = line.strip(); + + result += "%04hx".printf(lp5523.assemble(line)); + } + + /* size check */ + if(result.length/4 > lp5523.PROGSPACE_SIZE) + warning("this does not fit into lp5523 progspace"); + + result += "\n"; + + return result; +} + +public string disassemble(FileStream stream) throws lp5523.ParsingError { + string line; + string result = ""; + + line = stream.read_line(); + + if(line == null) + return ""; + + /* remove trailing newline */ + if(line.length % 2 == 1 && line.has_suffix("\n")) + line = line.substring(0, line.length-1); + + /* disassemble */ + for(int i=0; i