📄 jopa.java
字号:
package com.jopdesign.tools;/** Assemler for JOP3 Author: Martin Schoeberl martin@good-ear.com instruction coding:revision: 2001-09-22 creation 2001-10-24 working version 2001-12-08 intruction set change (16->8 bit) 2005-01-17 interrupt mux in jtbl.vhd 2005-02-06 JOP version in stack RAM at address 64 2005-02-20 Generate memory data for the simulation*/import java.io.*;import java.util.*;public class Jopa { private String fname; static final int ADDRBITS = 10; static final int DATABITS = 10; static final int BRBITS = 10; static final int OPDBITS = 5; static final int CONST_ADDR = 32; static final int VER_ADDR = 64; static final int RAM_LEN = 256; static final int ROM_LEN = 1<<ADDRBITS; private String srcDir; private String dstDir; public Jopa(String fn) { srcDir = System.getProperty("user.dir"); dstDir = System.getProperty("user.dir"); fname = fn; if (!srcDir.endsWith(File.separator)) srcDir += File.separator; if (!dstDir.endsWith(File.separator)) dstDir += File.separator; } public Jopa(String[] clist) { srcDir = System.getProperty("user.dir"); dstDir = System.getProperty("user.dir"); processOptions(clist); if (!srcDir.endsWith(File.separator)) srcDir += File.separator; if (!dstDir.endsWith(File.separator)) dstDir += File.separator; } void error(StreamTokenizer in, String s) { System.out.println((in.lineno()-1)+" error: "+s); } private StreamTokenizer getSt() { try { FileReader fileIn = new FileReader(srcDir + fname); StreamTokenizer in = new StreamTokenizer(fileIn); in.wordChars( '_', '_' ); in.wordChars( ':', ':' ); in.eolIsSignificant(true); in.slashStarComments(true); in.slashSlashComments(true); in.lowerCaseMode(true); return in; } catch (IOException e) { System.out.println(e.getMessage()); System.exit(-1); return null; } } private class Line { int jinstr; String label; Instruction instr; int special; int intVal; String symVal; boolean nxt; boolean opd; } private Line getLine(StreamTokenizer in) { Line l = new Line(); l.jinstr = -1; try { for (int cnt=0; in.nextToken()!=StreamTokenizer.TT_EOL; ++cnt) { if (in.ttype == StreamTokenizer.TT_WORD) { int pos = in.sval.indexOf(":"); if (pos!=-1) { String s = in.sval.substring(0, pos); l.jinstr = JopInstr.get(s); if (l.jinstr==-1) { l.label = s; } } else { if (in.sval.equals("nxt")) { l.nxt = true; } else if (in.sval.equals("opd")) { l.opd = true; } else { Instruction i = Instruction.get(in.sval); if (i==null) { l.symVal = in.sval; } else if (l.instr==null) { l.instr = i; } } } } else if (in.ttype == StreamTokenizer.TT_NUMBER) { l.intVal = (int) in.nval; } else if (in.ttype == '=') { l.special = in.ttype; } else if (in.ttype == '?') { l.special = in.ttype; } else { error(in, "'"+(char) in.ttype+"' syntax"); } } // EOL } catch (IOException e) { System.out.println(e.getMessage()); System.exit(-1); } return l; } static String bin(int val, int bits) { String s = ""; for (int i=0; i<bits; ++i) { s += (val & (1<<(bits-i-1))) != 0 ? "1" : "0"; } return s; } private Map symMap = new HashMap(); private int memcnt = 0; private List varList = new LinkedList(); private int version = -1;/*** first pass.* get values for all symbols*/ public void pass1() { StreamTokenizer in = getSt(); int pc = 0; try { while (in.nextToken() != StreamTokenizer.TT_EOF) { in.pushBack(); Line l = getLine(in);//System.out.println(l.jinstr+" "+l.label+" "+l.instr+" '"+(char) l.special+"' "+l.intVal+" "+l.symVal); if (l.jinstr==-1) { if (l.label!=null) { if (symMap.containsKey(l.label)) { error(in, "symbol "+l.label+" allready defined"); } else { symMap.put(l.label, new Integer(pc)); } } if (l.special=='=') { if (l.symVal==null) { error(in, "missing symbol for '='"); } else { if (symMap.containsKey(l.symVal)) { error(in, "symbol "+l.symVal+" allready defined"); } else { symMap.put(l.symVal, new Integer(l.intVal)); } } } else if (l.special=='?') { if (symMap.containsKey(l.symVal)) { error(in, "symbol "+l.symVal+" allready defined"); } else { symMap.put(l.symVal, new Integer(memcnt++)); varList.add(l.symVal); } } } if (l.instr!=null) { ++pc; } } } catch (IOException e) { System.out.println(e.getMessage()); System.exit(-1); }//System.out.println(symMap); } static String hex(int i, int len) { String s = Integer.toHexString(i); int cnt = len-s.length(); for (int k=0; k<cnt; ++k) s = "0"+s; return s; } private Map constMap = new HashMap(); private List constList = new LinkedList(); private Map offMap = new HashMap(); private List offList = new LinkedList(); private int[] romData = new int[ROM_LEN]; private int romLen = 0; private int[] ramData = new int[RAM_LEN];/*** second pass.* generate code and write rom.mif and ram.mif.*/ public void pass2() { StreamTokenizer in = getSt(); int pc = 0; int ji_cnt = 0; try { FileWriter rom = new FileWriter(dstDir + "rom.mif"); FileWriter jtbl = new FileWriter(dstDir + "jtbl.vhd"); FileWriter bcfetbl = new FileWriter(dstDir + "bcfetbl.vhd"); BufferedReader inraw = new BufferedReader(new FileReader(srcDir + fname)); String line;//// print rom.mif head// line = "--\n"; line += "--\trom.mif\n"; line += "--\n"; line += "depth = 1024;\n"; line += "width = "+DATABITS+";\n"; line += "\n"; line += "content\n"; line += "\n"; line += "begin\n"; line += "\n"; line += "\t[0..1ff] : 080; -- nop\n\n"; rom.write( line );//// print jtbl.vhd head// line = "--\n"; line += "--\tjtbl.vhd\n"; line += "--\n"; line += "--\tjump table for java bc to jvm address\n"; line += "--\n"; line += "--\t\tDONT edit this file!\n"; line += "--\t\tgenerated by Jopa.java\n"; line += "--\n"; line += "\n"; line += "library ieee;\n"; line += "use ieee.std_logic_1164.all;\n"; line += "use ieee.std_logic_arith.all;\n"; line += "use ieee.std_logic_unsigned.all;\n"; line += "\n"; line += "entity jtbl is\n"; line += "port (\n"; line += "\tbcode\t: in std_logic_vector(7 downto 0);\n"; line += "\tint_pend\t: in std_logic;\n"; line += "\texc_pend\t: in std_logic;\n"; line += "\tq\t\t: out std_logic_vector("+(ADDRBITS-1)+" downto 0)\n"; line += ");\n"; line += "end jtbl;\n"; line += "\n"; line += "--\n"; line += "--\tunregistered rdbcode\n"; line += "--\tunregistered dout\n"; line += "--\n"; line += "architecture rtl of jtbl is\n"; line += "\n"; line += "\tsignal\taddr\t: std_logic_vector("+(ADDRBITS-1)+" downto 0);\n"; line += "\n"; line += "begin\n"; line += "\n"; line += "process(bcode) begin\n"; line += "\n"; line += "\tcase bcode is\n"; line += "\n"; jtbl.write( line );//// print bcfetbl.vhd head// line = "--\n"; line += "--\tbcfetbl.vhd\n"; line += "--\n"; line += "--\tnext bc or bc operand read for bcfetch.\n"; line += "--\n"; line += "--\t\tDONT edit this file!\n"; line += "--\t\tgenerated by Jopa.java\n"; line += "--\n"; line += "\n"; line += "library ieee;\n"; line += "use ieee.std_logic_1164.all;\n"; line += "use ieee.std_logic_arith.all;\n"; line += "use ieee.std_logic_unsigned.all;\n"; line += "\n"; line += "entity bcfetbl is\n"; line += "port (\n"; line += "\taddr\t\t: in std_logic_vector("+(ADDRBITS-1)+" downto 0);\n"; line += "\tnxt, opd\t: out std_logic\n"; line += ");\n"; line += "end bcfetbl;\n"; line += "\n"; line += "architecture rtl of bcfetbl is\n"; line += "\n"; line += "begin\n"; line += "\n"; line += "process(addr) begin\n"; line += "\n"; line += "\tcase addr is\n"; line += "\n"; bcfetbl.write( line ); int noim_address = 0; int int_address = 0; int exc_address = 0; while (in.nextToken() != StreamTokenizer.TT_EOF) { in.pushBack(); Line l = getLine(in); if (l.jinstr!=-1) { ++ji_cnt; if (JopInstr.name(l.jinstr).equals("sys_int")) { int_address = pc; } else if (JopInstr.name(l.jinstr).equals("sys_exc")) { exc_address = pc; } else if (JopInstr.name(l.jinstr).equals("sys_noim")) { noim_address = pc; } else { jtbl.write("\t\twhen \""+bin(l.jinstr, 8) + "\" => addr <= \""+bin(pc, ADDRBITS)+"\";" + "\t--\t"+hex(pc,4)+"\t"+JopInstr.name(l.jinstr)+"\n"); } } line = "\t"; if (l.instr==null) { line += " ";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -