📄 jopa.java
字号:
} else {//// do the assembling// int opcode = l.instr.opcode; if (l.nxt || l.opd) { bcfetbl.write("\t\twhen \""+bin(pc, ADDRBITS) + "\" => "); bcfetbl.write("nxt <= '"+(l.nxt ? 1 : 0)+"'; "); bcfetbl.write("opd <= '"+(l.opd ? 1 : 0)+"';\n"); } if (l.instr.hasOpd) { int opVal = 0; if (l.symVal!=null) { Integer i = (Integer) symMap.get(l.symVal); if (i==null) { error(in, "Symbol "+l.symVal+" not defined"); } else { opVal = i.intValue(); } } else { opVal = l.intVal; } if (l.instr.name.equals("ldi")) { Integer i = new Integer(opVal); Integer addr; if (constMap.containsKey(i)) { addr = (Integer) constMap.get(i); } else { addr = new Integer(constMap.size()); constMap.put(i, addr); constList.add(i); } opVal = addr.intValue(); } if (l.instr.isJmp) { // List of branch offsets Integer off = new Integer(opVal-pc-1); if (off.intValue()< -512 || off.intValue()>511) { error(in, "offset "+off+" wrong range"); } Integer addr; if (offMap.containsKey(off)) { addr = (Integer) offMap.get(off); } else { addr = new Integer(offMap.size()); offMap.put(off, addr); offList.add(off); } opVal = addr.intValue(); } if (opVal>31 || opVal<0) { error(in, "operand wrong: "+opVal); } opcode |= opVal & 0x1f; // use 5 bit operand } if (l.nxt) opcode |= 0x200; if (l.opd) opcode |= 0x100; romData[romLen] = opcode; ++romLen; line += hex(pc, 4)+" : "+hex(opcode, 3)+";\t"; ++pc; } line += "\t--\t"+inraw.readLine()+"\n"; rom.write( line );// System.out.print(line); } rom.write( "\nend;\n" ); rom.close(); line = "\n"; line += "\t\twhen others => addr <= \""+bin(noim_address, ADDRBITS)+ "\";\t\t--\t"+hex(noim_address,4)+"\tsys_noim\n"; line += "\tend case;\n"; line += "end process;\n"; line += "\n"; line += "process(int_pend, exc_pend, addr) begin\n"; line += "\n"; line += "\tq <= addr;\n"; line += "\tif exc_pend='1' then\n"; line += "\t\tq <= \""+bin(exc_address, ADDRBITS)+ "\";\t\t--\t"+hex(exc_address,4)+"\tsys_exc\n"; line += "\telsif int_pend='1' then\n"; line += "\t\tq <= \""+bin(int_address, ADDRBITS)+ "\";\t\t--\t"+hex(int_address,4)+"\tsys_int\n"; line += "\tend if;\n"; line += "end process;\n"; line += "\n"; line += "end rtl;\n"; jtbl.write(line); jtbl.close(); line = "\n"; line += "\t\twhen others => nxt <= '0'; opd <= '0'; \n"; line += "\tend case;\n"; line += "end process;\n"; line += "\n"; line += "end rtl;\n"; bcfetbl.write(line); bcfetbl.close();//// print ROM as generic VHDL file// FileWriter romvhd = new FileWriter(dstDir + "rom.vhd"); line = "--\n"; line += "--\trom.vhd\n"; line += "--\n"; line += "--\tgeneric VHDL version of ROM\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 rom is\n"; line += "generic (width : integer; addr_width : integer);\t-- for compatibility\n"; line += "port (\n"; line += "\tclk\t\t\t: in std_logic;\n"; line += "\taddress\t\t: in std_logic_vector("+(ADDRBITS-1)+" downto 0);\n"; line += "\tq\t\t\t: out std_logic_vector("+(DATABITS-1)+" downto 0)\n"; line += ");\n"; line += "end rom;\n"; line += "\n"; line += "architecture rtl of rom is\n"; line += "\n"; line += "\tsignal areg\t\t: std_logic_vector("+(ADDRBITS-1)+" downto 0);\n"; line += "\tsignal data\t\t: std_logic_vector("+(DATABITS-1)+" downto 0);\n"; line += "\n"; line += "begin\n"; line += "\n"; line += "process(clk) begin\n"; line += "\n";// line += "\tif falling_edge(clk) then\n";// line += "\t\tareg <= address;\n";// line += "\tend if;\n"; line += "\tif rising_edge(clk) then\n";// line += "\t\tq <= data;\n"; line += "\t\tareg <= address;\n"; line += "\tend if;\n"; line += "\n"; line += "end process;\n"; line += "\n"; line += "\tq <= data;\n"; line += "\n"; line += "process(areg) begin\n"; line += "\n"; line += "\tcase areg is\n"; line += "\n"; romvhd.write(line); for (int i=0; i<romLen; ++i) { romvhd.write("\t\twhen \""+bin(i, ADDRBITS) + "\" => data <= \""+bin(romData[i], DATABITS)+"\";"); romvhd.write("\t-- "+"TODO: comment"+"\n"); } line = "\n"; line += "\t\twhen others => data <= \""+bin(0, DATABITS)+"\";\n"; line += "\tend case;\n"; line += "end process;\n"; line += "\n"; line += "end rtl;\n"; romvhd.write(line); romvhd.close(); PrintStream rom_mem = new PrintStream(new FileOutputStream(dstDir + "mem_rom.dat")); for (int i=0; i<ROM_LEN; ++i) { rom_mem.println(romData[i]+" "); } rom_mem.close();//// print table of branch offsets// FileWriter offtbl = new FileWriter(dstDir + "offtbl.vhd"); line = "--\n"; line += "--\tofftbl.vhd\n"; line += "--\n"; line += "--\tnext bc or bc operand read for offtch.\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 offtbl is\n"; line += "port (\n"; line += "\tidx\t\t: in std_logic_vector("+(OPDBITS-1)+" downto 0);\n"; line += "\tq\t\t: out std_logic_vector("+(BRBITS-1)+" downto 0)\n"; line += ");\n"; line += "end offtbl;\n"; line += "\n"; line += "architecture rtl of offtbl is\n"; line += "\n"; line += "begin\n"; line += "\n"; line += "process(idx) begin\n"; line += "\n"; line += "\tcase idx is\n"; line += "\n"; offtbl.write( line ); for (int i=0; i<offList.size(); ++i) { Integer val = (Integer) offList.get(i); offtbl.write("\t\twhen \""+bin(i, OPDBITS) + "\" => q <= \""+bin(val.intValue(), BRBITS)+"\";"); offtbl.write("\t-- "+val.intValue()+"\n"); } line = "\n"; line += "\t\twhen others => q <= \""+bin(0, BRBITS)+"\";\n"; line += "\tend case;\n"; line += "end process;\n"; line += "\n"; line += "end rtl;\n"; offtbl.write( line ); offtbl.close();//// Print symbol table as ram.mif and data for the simulation.// FileWriter ram = new FileWriter(dstDir + "ram.mif"); for (int i=0; i<RAM_LEN; ++i) { ramData[i] = 0x12345678; } line = "--\n"; line += "--\tram.mif\n"; line += "--\n"; line += "depth = 256;\n"; line += "width = 32;\n"; line += "\n"; line += "content\n"; line += "\n"; line += "begin\n";// line += "\t[0..ff] : 00000000;\n"; line += "\t[0..ff] : 12345678;\n"; line += "\n"; ram.write( line ); line = "--\n"; line += "-- "+memcnt+" vars\n"; line += "--\n\n"; ram.write( line ); // // Variables // for (int i=0; i<varList.size(); ++i) { String s = (String) varList.get(i); ramData[i] = 0; line = "\t"; line += hex(i, 4) + " : " ; line += hex(0, 8) + ";\t--\t"; line += s + "\n"; ram.write( line ); } line = "--\n"; line += "-- "+constMap.size()+" consts\n"; line += "--\n\n"; ram.write( line ); // // Constants // for (int i=0; i<constList.size(); ++i) { Integer val = (Integer) constList.get(i); ramData[CONST_ADDR+i] = val.intValue(); line = "\t"; line += hex(CONST_ADDR+i, 4) + " : " ; line += hex(val.intValue(), 8) + ";\t--\t"; line += val + "\n"; ram.write( line ); } // check if version is set Integer ver = (Integer) symMap.get("version"); if (ver==null) { error(in, "version not set, setting to -1"); } else { version = ver.intValue(); } ramData[VER_ADDR] = version; ramData[VER_ADDR+1] = 0; ram.write("\n\n--\tVersion\n"); line = "\t"; line += hex(VER_ADDR, 4) + " : " ; line += hex(version, 8) + ";\t--\t"; line += version + "\n"; ram.write(line); line = "\t"; line = "\t"+hex(VER_ADDR+1, 4) + " : " ; line += hex(0, 8) + ";\t--\tfor future use - FPGA type?\n"; ram.write(line); ram.write( "\nend;\n" ); ram.close(); PrintStream ram_mem = new PrintStream(new FileOutputStream(dstDir + "mem_ram.dat")); for (int i=0; i<RAM_LEN; ++i) { ram_mem.println(ramData[i]+" "); } ram_mem.close(); System.out.println(ji_cnt+" Instructions implemented"); } catch (IOException e) { System.out.println(e.getMessage()); System.exit(-1); } } private boolean processOptions(String clist[]) { boolean success = true; for (int i = 0; i < clist.length; i++) { if (clist[i].equals("-s")) { srcDir = clist[++i]; } else if (clist[i].equals("-d")) { dstDir = clist[++i]; } else { fname = clist[i]; } } return success; }/*** Main for Jop assembler.*/ public static void main(String args[]) { if (args.length < 1) { System.out.println( "usage: java Jopa [-s srcDir] [-d dstDir] filename"); System.exit(-1); } Jopa j = new Jopa(args); j.pass1(); j.pass2(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -