/* Copyright (c) 2004, 2006 Pablo Bleyer Kocik. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ options { LOOKAHEAD = 2; STATIC = false; } PARSER_BEGIN(KCAsm) import java.util.Vector; import java.util.Properties; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; class KCAsm { /** Tracks the current line number. */ int lines = 1; /** Assembler expressions (ie, the parsed program). */ Vector expressions; public static void main(String[] args) throws ParseException, IOException { if (args.length < 2) { usage(); return; } Properties pr = System.getProperties(); String s; int kcpsm = -1, bram = -1; String mn = null; s = pr.getProperty("kcpsm"); if (s != null) kcpsm = Integer.parseInt(s); s = pr.getProperty("bram"); if (s != null) bram = Integer.parseInt(s); mn = pr.getProperty("module"); if (kcpsm < 1 || kcpsm > 3) { usage(); return; } if (bram != 16 && bram != 18) { usage(); return; } if (mn == null || mn.length() == 0) { usage(); return; } FileInputStream fi = new FileInputStream(args[0]); KCAsm p = new KCAsm(fi); p.expressions = new Vector(); p.Start(); fi.close(); Assembler asm; switch (kcpsm) { case 1: asm = new Assembler1(p.expressions); break; case 2: asm = new Assembler2(p.expressions); break; case 3: asm = new Assembler3(p.expressions); break; default: asm = null; break; } // FileOutputStream fo = new FileOutputStream(basename(args[0]) + ".rmh"); FileOutputStream fo = new FileOutputStream(args[1]); fo.write(asm.toString().getBytes()); fo.close(); fo = new FileOutputStream(mn + ".v"); fo.write(asm.toBlockRAM(mn).getBytes()); fo.close(); // asm.toVerilog(args[0]); } static void usage() { System.err.println( "Usage: java -Dkcpsm={1,2,3} -Dbram={16,18} -Dmodule={name} KCAsm {psm_file} {rmh_file}" ); } static String basename(String n) { int dp = n.indexOf('.'); return n.substring(0, dp-1).trim(); } } PARSER_END(KCAsm) SKIP : {" "|"\t"} // skip white spaces TOKEN : { <#CR:"\n"|"\r"|"\r\n"> // end of line characters | > | // valid word | // bounded valid word | // a binary number, eg %100101 | // an octal number, eg @756 | // a decimal number, eg &123 or 123 | // an hexadecimal number, eg $5a | // an ascii character | ":"> // valid label declaration - word followed by colon // | : IN_COMMENT | : IN_COMMENT // a semicolon starts a comment } TOKEN : { : DEFAULT // accept as comment until end of line } /** Valid program is a sequence of valid expressions followed by EOF. */ void Start() : {} { ( Expression() )* // { // ListIterator i = expressions.listIterator(); // while (i.hasNext()) { // Command c = (Command)i.next(); // ps.println(c.toString()); // } // } } /** Valid expressions are labels, commands and comments. */ void Expression() : { } { LabelExpression() ( { lines += 1; } )? // a label is optionally followed by an end of line | CommandExpression() ( CommentExpression() | { lines += 1; } ) // commands are ended by a comment or a new line | (CommentExpression())? // comments are always optional { lines += 1; } // valid expressions end with and end of line } /** Valid comment expressions start with a semicolon, which is not part of the comment content. */ void CommentExpression() : { Token t; } { t = { expressions.add(new Comment(lines, t.image)); } } /** Valid label expressions are words followed by a colon, which is not part of the label content. */ void LabelExpression() : { Token t; } { t =