kcasm.java
来自「和picoblaze完全兼容的mcu ip core」· Java 代码 · 共 817 行 · 第 1/2 页
JAVA
817 行
/* Generated By:JavaCC: Do not edit this line. KCAsm.java */
import java.util.Vector;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
class KCAsm implements KCAsmConstants {
/** 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();
}
/** Valid program is a sequence of valid expressions followed by EOF. */
final public void Start() throws ParseException {
label_1:
while (true) {
if (jj_2_1(2)) {
;
} else {
break label_1;
}
Expression();
}
jj_consume_token(0);
}
/** Valid expressions are labels, commands and comments. */
final public void Expression() throws ParseException {
if (jj_2_6(2)) {
LabelExpression();
if (jj_2_2(2)) {
jj_consume_token(EOL);
lines += 1;
} else {
;
}
} else if (jj_2_7(2)) {
CommandExpression();
if (jj_2_3(2)) {
CommentExpression();
} else if (jj_2_4(2)) {
jj_consume_token(EOL);
lines += 1;
} else {
jj_consume_token(-1);
throw new ParseException();
}
} else if (jj_2_8(2)) {
if (jj_2_5(2)) {
CommentExpression();
} else {
;
}
jj_consume_token(EOL);
lines += 1;
} else {
jj_consume_token(-1);
throw new ParseException();
}
}
/** Valid comment expressions start with a semicolon, which is not part of the
comment content.
*/
final public void CommentExpression() throws ParseException {
Token t;
jj_consume_token(SEMICOLON);
t = jj_consume_token(COMMENT);
expressions.add(new Comment(lines, t.image));
}
/** Valid label expressions are words followed by a colon, which is not part of
the label content.
*/
final public void LabelExpression() throws ParseException {
Token t;
t = jj_consume_token(LABEL);
expressions.add(new Label(lines, t.image.replace(':', ' ').trim()));
}
/** Valid command expressions consist of an initial word (command name),
followed by an optional, comma separated argument list.
*/
final public void CommandExpression() throws ParseException {
Token t;
Command c;
Vector l = null;
t = jj_consume_token(WORD);
if (jj_2_9(2)) {
l = Arguments();
} else {
;
}
expressions.add(new Command(lines, t.image, l));
}
/** Valid numbers are binary, octal, decimal, hexadecimal and ascii character
values.
*/
final public Integer Number() throws ParseException {
Token t;
if (jj_2_10(2)) {
t = jj_consume_token(BINARY);
{if (true) return Integer.valueOf(t.image.replace('%', ' ').trim(), 2);}
} else if (jj_2_11(2)) {
t = jj_consume_token(OCTAL);
{if (true) return Integer.valueOf(t.image.replace('@', ' ').trim(), 8);}
} else if (jj_2_12(2)) {
t = jj_consume_token(DECIMAL);
{if (true) return Integer.valueOf(t.image.replace('&', ' ').trim(), 10);}
} else if (jj_2_13(2)) {
t = jj_consume_token(HEXADECIMAL);
{if (true) return Integer.valueOf(t.image.replace('$', ' ').trim(), 16);}
} else if (jj_2_14(2)) {
t = jj_consume_token(ASCII);
{if (true) return new Integer((int)t.image.charAt(1));}
} else {
jj_consume_token(-1);
throw new ParseException();
}
throw new Error("Missing return statement in function");
}
/* A literal is a word (symbol name) or a number. */
final public Object Literal() throws ParseException {
Token t;
Integer n;
if (jj_2_15(2)) {
t = jj_consume_token(WORD);
{if (true) return t.image;}
} else if (jj_2_16(2)) {
t = jj_consume_token(PWORD);
{if (true) return t.image.substring(1, t.image.length()-1);}
} else if (jj_2_17(2)) {
n = Number();
{if (true) return n;}
} else {
jj_consume_token(-1);
throw new ParseException();
}
throw new Error("Missing return statement in function");
}
/* An argument list is a comma separated sequence of literals that are packed
into a vector.
*/
final public Vector Arguments() throws ParseException {
Vector l = new Vector();
Object o;
o = Literal();
l.add(o);
label_2:
while (true) {
if (jj_2_18(2)) {
;
} else {
break label_2;
}
jj_consume_token(15);
o = Literal();
l.add(o);
}
{if (true) return l;}
throw new Error("Missing return statement in function");
}
final private boolean jj_2_1(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_1(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(0, xla); }
}
final private boolean jj_2_2(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_2(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(1, xla); }
}
final private boolean jj_2_3(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_3(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(2, xla); }
}
final private boolean jj_2_4(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_4(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(3, xla); }
}
final private boolean jj_2_5(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_5(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(4, xla); }
}
final private boolean jj_2_6(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_6(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(5, xla); }
}
final private boolean jj_2_7(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_7(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(6, xla); }
}
final private boolean jj_2_8(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_8(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(7, xla); }
}
final private boolean jj_2_9(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_9(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(8, xla); }
}
final private boolean jj_2_10(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_10(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(9, xla); }
}
final private boolean jj_2_11(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_11(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(10, xla); }
}
final private boolean jj_2_12(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_12(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(11, xla); }
}
final private boolean jj_2_13(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_13(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(12, xla); }
}
final private boolean jj_2_14(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_14(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(13, xla); }
}
final private boolean jj_2_15(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_15(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(14, xla); }
}
final private boolean jj_2_16(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_16(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(15, xla); }
}
final private boolean jj_2_17(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_17(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(16, xla); }
}
final private boolean jj_2_18(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_18(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(17, xla); }
}
final private boolean jj_3_4() {
if (jj_scan_token(EOL)) return true;
return false;
}
final private boolean jj_3_3() {
if (jj_3R_4()) return true;
return false;
}
final private boolean jj_3_5() {
if (jj_3R_4()) return true;
return false;
}
final private boolean jj_3_8() {
Token xsp;
xsp = jj_scanpos;
if (jj_3_5()) jj_scanpos = xsp;
if (jj_scan_token(EOL)) return true;
return false;
}
final private boolean jj_3R_7() {
if (jj_3R_9()) return true;
Token xsp;
while (true) {
xsp = jj_scanpos;
if (jj_3_18()) { jj_scanpos = xsp; break; }
}
return false;
}
final private boolean jj_3_2() {
if (jj_scan_token(EOL)) return true;
return false;
}
final private boolean jj_3R_6() {
if (jj_scan_token(WORD)) return true;
Token xsp;
xsp = jj_scanpos;
if (jj_3_9()) jj_scanpos = xsp;
return false;
}
final private boolean jj_3_7() {
if (jj_3R_6()) return true;
Token xsp;
xsp = jj_scanpos;
if (jj_3_3()) {
jj_scanpos = xsp;
if (jj_3_4()) return true;
}
return false;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?