📄 verilogreader.java
字号:
package com.sun.electric.tool.io.input.verilog;import com.sun.electric.database.text.TextUtils;import com.sun.electric.database.hierarchy.Cell;import com.sun.electric.database.hierarchy.Library;import com.sun.electric.database.hierarchy.View;import com.sun.electric.database.hierarchy.Export;import com.sun.electric.database.topology.NodeInst;import com.sun.electric.database.topology.ArcInst;import com.sun.electric.database.topology.PortInst;import com.sun.electric.database.geometry.Orientation;import com.sun.electric.database.prototype.PortProto;import com.sun.electric.database.prototype.PortCharacteristic;import com.sun.electric.technology.technologies.Schematics;import com.sun.electric.technology.technologies.Generic;import com.sun.electric.technology.PrimitiveNode;import com.sun.electric.technology.ArcProto;import com.sun.electric.tool.user.ViewChanges;import com.sun.electric.tool.Job;import com.sun.electric.tool.io.input.Input;import java.net.URL;import java.io.IOException;import java.util.*;import java.awt.geom.Point2D;/** * User: gg151869 * Date: Oct 23, 2006 */public class VerilogReader extends Input{ List<NodeInst> transistors = new ArrayList<NodeInst>(); double maxWidth = 100, nodeWidth = 10; double primitiveHeight = 0.5, primitiveWidth = 0.5; Map<Cell, Point2D.Double> locationMap = new HashMap<Cell, Point2D.Double>(); PrimitiveNode essentialBounds = Generic.tech().findNodeProto("Essential-Bounds"); Cell topCell = null; Map<String, NodeInst> pinsMap = new HashMap<String, NodeInst>(); private String typicalSkipStrings = "\t\\"; // strings that should be ignored by the StringTokenizer() private String readCellHeader(List<String> inputs) throws IOException { for (;;) { String key = getAKeyword(); StringTokenizer parse = new StringTokenizer(key, "( ),\t", false); while (parse.hasMoreTokens()) { String value = parse.nextToken(); if (value.equals(";")) // done with header return null; inputs.add(value); } } } private static class CellInstance { String name; List<PortInfo> list = new ArrayList<PortInfo>(); CellInstance(String n) { this.name = TextUtils.correctName(n, false, true); } static class PortInfo { String local; boolean isBus; PortProto ex; PortInfo(String local, boolean isBus, PortProto ex) { // Doesn't correct name if it is a bus this.local = (isBus) ? local : TextUtils.correctName(local, false, true); this.isBus = isBus; this.ex = ex; } } void addConnection(String local, boolean isBus, PortProto ex) { PortInfo port = new PortInfo(local, isBus, ex); list.add(port); } }// private void createInstance(Cell parent, VerilogData verilogData,// VerilogData.VerilogModule module, Cell icon, CellInstance info)// {// NodeInst cellInst = NodeInst.newInstance(icon, getNextLocation(parent), 10, 10, parent,// Orientation.IDENT, info.name, 0);//// List<String> localPorts = new ArrayList<String>();//// for (CellInstance.PortInfo port : info.list)// {// localPorts.clear();//// String portLocal = port.local;//// // It is unknown how many pins are coming in the stream// if (portLocal.contains("{"))// {// StringTokenizer parse = new StringTokenizer(portLocal, "{,}", false); // extracting pins// while (parse.hasMoreTokens())// {// String name = parse.nextToken();// name = name.replaceAll(" ", "");// localPorts.add(name);// }// }// else// localPorts.add(portLocal);//// for (String s : localPorts)// {// NodeInst pin = pinsMap.get(s);//// if (pin == null)// {// int index = s.indexOf("[");// if (index != -1)// {// s = s.substring(0, index);// pin = pinsMap.get(s);// }// }//// if (pin == null)// {// if (s.equals("vss")) // ground// {// pin = readSupply(module, false, s);// }// else// {// if (Job.getDebug())// System.out.println("Unknown signal " + s + " in cell " + parent.describe(false));// PrimitiveNode primitive = (port.isBus) ? Schematics.tech().busPinNode : Schematics.tech().wirePinNode;// pin = NodeInst.newInstance(primitive, getNextLocation(parent),// primitiveWidth, primitiveHeight,//// primitive.getDefWidth(), primitive.getDefHeight(),// parent, Orientation.IDENT, null/*s*/, 0);// pinsMap.put(s, pin);// }// }////// ArcProto node = (port.isBus) ? Schematics.tech.bus_arc : Schematics.tech.wire_arc;// ArcProto node = (pin.getProto() == Schematics.tech().busPinNode) ? Schematics.tech().bus_arc : Schematics.tech().wire_arc;// PortInst ex = cellInst.findPortInst(port.ex.getName());// ArcInst ai = ArcInst.makeInstanceBase(node, 0.0,//// ArcInst ai = ArcInst.makeInstanceFull(node, 0.0 /*node.getDefaultLambdaFullWidth()*/,// pin.getOnlyPortInst(), ex, null, null, s);// assert(ai != null);// ai.setFixedAngle(false);// }// }// } private NodeInst readSupply(VerilogData.VerilogModule module, boolean power, String name) { VerilogData.VerilogPort supply = module.addPort(name, false); supply.type = (power) ? PortCharacteristic.PWR : PortCharacteristic.GND; return null; } private CellInstance readInstance(VerilogData.VerilogModule module, VerilogData.VerilogModule element) throws IOException { StringBuffer signature = new StringBuffer(); List<String> exports = new ArrayList<String>(); List<String> pins = new ArrayList<String>(); for (;;) { String key = getRestOfLine(); if (key.contains("//")) continue; // comment signature.append(key); if (key.contains(";")) // found end of signature { String line = signature.toString(); int index = line.indexOf("("); // searching for first ( String instanceName = element.getName() + "-instance"; // if index==0, no name provided -> name is null assert(index > -1); // do we have cases with -1? if (index > 0) { instanceName = line.substring(0, index); } line = line.substring(index+1, line.length()); StringTokenizer parse = new StringTokenizer(line, ")", false); //typicalSkipStrings can't be used exports.clear(); pins.clear(); while (parse.hasMoreTokens()) { String value = parse.nextToken(); value = value.replaceAll(" ", ""); index = value.indexOf("."); // look for first . if (index == -1) // end of tokens continue; // or break? int index2 = value.indexOf("("); // look for first ( assert(index2 != -1); String n = value.substring(index+1, index2); n = TextUtils.correctName(n, false, true);// int index3 = n.indexOf("\\"); // those \ are a problem!// if (index3 != -1)// n = n.substring(index3+1); exports.add(n); n = value.substring(index2+1); n = TextUtils.correctName(n, false, false); if (n.contains(" ")) assert(false); // get rid of those empty sapces?// pins.add(value.substring(index2+1)); pins.add(n); } // remove extra white spaces instanceName = TextUtils.correctName(instanceName, false, true); instanceName = instanceName.replaceAll(" ", ""); CellInstance localCell = new CellInstance(instanceName); VerilogData.VerilogInstance verilogInst = null; verilogInst = module.addInstance(instanceName, element); for (int i = 0; i < exports.size(); i++) { String export = exports.get(i); String pin = pins.get(i); pin = pin.replaceAll(" ", ""); export = export.replaceAll(" ", ""); VerilogData.VerilogPort exp = element.findPort(export); // fixing original export if not found if (exp == null) {// System.out.println("Warning: port " + export + " not found in module " + element.name + " yet"); exp = element.addPort(export, false); } verilogInst.addPortInstance(pin, exp); } return localCell; } } // never reach this point } private String readWiresAndSupplies(VerilogData.VerilogModule module, boolean readWires, boolean power) throws IOException { List<String> values = new ArrayList<String>(2); for (;;) { String input = getRestOfLine(); StringTokenizer parse = new StringTokenizer(input, ",;", true); // net1, net2, [9:0] net4; while (parse.hasMoreTokens()) { String net = parse.nextToken(); if (net.equals(",")) continue; if (net.equals(";")) { return null; // done } if (readWires) // wires { StringTokenizer p = new StringTokenizer(net, typicalSkipStrings+" ", false); values.clear(); // clean reset while (p.hasMoreTokens()) { values.add(p.nextToken()); } int size = values.size(); if (size == 0) continue; assert(size == 1 || size == 2); PrimitiveNode primitive = Schematics.tech().wirePinNode; String pinName = values.get(size-1); int[] vals = {0, 0}; int count = 0; if (values.size() == 2) { p = new StringTokenizer(values.get(0), "[:]", false); while (p.hasMoreTokens()) { String s = p.nextToken(); if (TextUtils.isANumber(s)) vals[count++] = Integer.parseInt(s); } if (count == 2 && vals[0] != vals[1]) // only if it is a real bus { // pinName += values.get(0); primitive = Schematics.tech().busPinNode; } else System.out.println(net + " is not a bus wire"); } pinName = TextUtils.correctName(pinName, false, true); // also considering [x:x]. Not doing the exception here as above module.addWire(pinName, (values.size() == 2) ? values.get(0) : null); } else // supplies { StringTokenizer p = new StringTokenizer(net, "\t ", false); String name = p.nextToken(); name = TextUtils.correctName(name, false, true); readSupply(module, power, name); // supply1 -> vdd, supply0 -> gnd or vss } } } // never reach this point } /** * Method to ignore certain amount of lines. Useful for begin/end blocks and tables * @param endString * @throws IOException */ private void ignoreUntilEndOfStatement(String endString) throws IOException { String key = (endString != null) ? endString : ";"; // endString != null for table for example for (;;) { String input = getRestOfLine(); if (endString == null && input.contains("begin")) // swtch to end only if it is not a table
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -