Commit e5cfa45a authored by Peter's avatar Peter
Browse files

dev

parent 6c6804a7
Loading
Loading
Loading
Loading
Loading
+1
Changes for nb-configuration.xml: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -15,5 +15,6 @@ Any value defined here will override the pom.xml file value but is only applicab
-->
        <netbeans.hint.licensePath>${project.basedir}/licenseheader.txt</netbeans.hint.licensePath>
        <org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
        <netbeans.compile.on.save>none</netbeans.compile.on.save>
    </properties>
</project-shared-configuration>
+2 −2
Changes for pom.xml: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -8,8 +8,8 @@
	<name>Verilog Compiler</name>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>13</maven.compiler.source>
		<maven.compiler.target>13</maven.compiler.target>
		<maven.compiler.source>21</maven.compiler.source>
		<maven.compiler.target>21</maven.compiler.target>
		<build.date>${maven.build.timestamp}</build.date>
		<version>${project.version}</version>
	</properties>
+3 −3
Changes for src/main/java/hk/quantr/verilogcompiler/VerilogHelper.java: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ public class VerilogHelper {
	public static VerilogMacroDefinitionListener scanMacroDefinitions(File preferFile, File folder) throws FileNotFoundException, IOException {
//		logger.info(" --- scanMacroDefinitions " + preferFile + ", " + folder.getAbsolutePath());
		// scan all verilog for macro definitions
		logger.info(" --- listing folder " + folder.getAbsolutePath());
//		logger.info(" --- listing folder " + folder.getAbsolutePath());
		Collection<File> temp = FileUtils.listFiles(folder, new String[]{"v"}, true);
		ArrayList<File> files = new ArrayList<>();
		files.addAll(temp);
@@ -58,7 +58,7 @@ public class VerilogHelper {
		VerilogMacroDefinitionListener listener = new VerilogMacroDefinitionListener();
		for (File file : files) {
			try {
				logger.info(" --- reading " + file.getAbsolutePath());
//				logger.info(" --- reading " + file.getAbsolutePath());
				VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(IOUtils.toString(new FileInputStream(file), "utf-8")));
				CommonTokenStream tokenStream = new CommonTokenStream(lexer, VerilogLexer.DIRECTIVES);
				VerilogPreParser parser = new VerilogPreParser(tokenStream);
@@ -83,7 +83,7 @@ public class VerilogHelper {
	}

	public static String getContentReplacedWithMacro(int parseNo, File file, String content, VerilogMacroDefinitionListener verilogPreListener, ParseStructure ps) throws FileNotFoundException, IOException {
		System.out.println("file=" + file.getAbsolutePath() + ", parseNo=" + parseNo);
//		System.out.println("file=" + file.getAbsolutePath() + ", parseNo=" + parseNo);
		String lines[] = content.split("\r?\n");
		if (ps.macroListing.isEmpty()) {
			for (int x = 0; x < lines.length; x++) {
+259
Changes for src/main/java/hk/quantr/verilogcompiler/listener/WireListener.java: 259 added lines, 0 removed lines.
Original line number Diff line number Diff line
package hk.quantr.verilogcompiler.listener;

import hk.quantr.verilogcompiler.antlr.VerilogParser;
import hk.quantr.verilogcompiler.antlr.VerilogParserBaseListener;
import hk.quantr.verilogcompiler.structure.Module;
import hk.quantr.verilogcompiler.structure.Port;
import hk.quantr.verilogcompiler.structure.Wire;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

/**
 * Listener to extract wire declarations and their connections from Verilog modules
 * 
 * @author Peter <[email protected]>
 */
public class WireListener extends VerilogParserBaseListener {

	// Map of module name to list of wires in that module
	public HashMap<String, ArrayList<Wire>> moduleWires = new HashMap<>();
	
	// Map of wire name to Wire object for connection tracking
	private HashMap<String, Wire> wiresByName = new HashMap<>();
	
	// Available module definitions for port lookup
	private HashMap<String, Module> availableModules;
	
	private String currentModuleName = null;
	private ArrayList<Wire> currentWires = new ArrayList<>();
	private ArrayList<Port> currentModuleInputs = new ArrayList<>();
	private ArrayList<Port> currentModuleOutputs = new ArrayList<>();
	private static Logger logger = Logger.getLogger(WireListener.class.getName());
	
	public WireListener(HashMap<String, Module> availableModules) {
		this.availableModules = availableModules != null ? availableModules : new HashMap<>();
	}

	@Override
	public void enterModule_declaration(VerilogParser.Module_declarationContext ctx) {
		// Reset for new module
		currentWires = new ArrayList<>();
		wiresByName = new HashMap<>();
		currentModuleInputs = new ArrayList<>();
		currentModuleOutputs = new ArrayList<>();
		currentModuleName = null;
	}

	@Override
	public void exitModule_identifier(VerilogParser.Module_identifierContext ctx) {
		// Capture module name when we encounter the module identifier
		if (currentModuleName == null) {
			currentModuleName = ctx.getText();
		}
	}
	
	@Override
	public void exitInput_declaration(VerilogParser.Input_declarationContext ctx) {
		// Capture input ports of the current module
		String type = ctx.net_type() == null ? null : ctx.net_type().getText();
		String range = ctx.range_() == null ? null : ctx.range_().getText();
		
		List<VerilogParser.Port_identifierContext> list = ctx.list_of_port_identifiers().port_identifier();
		for (VerilogParser.Port_identifierContext portCtx : list) {
			Port port = new Port("input", type, range, portCtx.getText());
			currentModuleInputs.add(port);
			logger.info("Found input port in module " + currentModuleName + ": " + portCtx.getText());
		}
	}
	
	@Override
	public void exitOutput_declaration(VerilogParser.Output_declarationContext ctx) {
		// Capture output ports of the current module
		String type = ctx.net_type() == null ? null : ctx.net_type().getText();
		String range = ctx.range_() == null ? null : ctx.range_().getText();
		
		if (ctx.list_of_port_identifiers() != null) {
			List<VerilogParser.Port_identifierContext> list = ctx.list_of_port_identifiers().port_identifier();
			for (VerilogParser.Port_identifierContext portCtx : list) {
				Port port = new Port("output", type, range, portCtx.getText());
				currentModuleOutputs.add(port);
				logger.info("Found output port in module " + currentModuleName + ": " + portCtx.getText());
			}
		}
		if (ctx.list_of_variable_port_identifiers() != null) {
			List<VerilogParser.Var_port_idContext> list = ctx.list_of_variable_port_identifiers().var_port_id();
			for (VerilogParser.Var_port_idContext varPort : list) {
				Port port = new Port("output", type, range, varPort.port_identifier().getText());
				currentModuleOutputs.add(port);
				logger.info("Found output port in module " + currentModuleName + ": " + varPort.port_identifier().getText());
			}
		}
	}

	@Override
	public void exitNet_declaration(VerilogParser.Net_declarationContext ctx) {
		// Parse wire declarations
		String netType = ctx.net_type() != null ? ctx.net_type().getText() : "wire";
		String range = ctx.range_() != null ? ctx.range_().getText() : null;
		
		// Handle list_of_net_identifiers (wire declarations without assignments)
		if (ctx.list_of_net_identifiers() != null) {
			List<VerilogParser.Net_idContext> netIds = ctx.list_of_net_identifiers().net_id();
			for (VerilogParser.Net_idContext netId : netIds) {
				String wireName = netId.net_identifier().getText();
				Wire wire = new Wire(wireName, range);
				currentWires.add(wire);
				wiresByName.put(wireName, wire);
				logger.info("Found wire in module " + currentModuleName + ": " + wireName + 
					(range != null ? " " + range : ""));
			}
		}
		
		// Handle list_of_net_decl_assignments (wire declarations with assignments)
		if (ctx.list_of_net_decl_assignments() != null) {
			List<VerilogParser.Net_decl_assignmentContext> netDecls = 
				ctx.list_of_net_decl_assignments().net_decl_assignment();
			for (VerilogParser.Net_decl_assignmentContext netDecl : netDecls) {
				String wireName = netDecl.net_identifier().getText();
				Wire wire = new Wire(wireName, range);
				currentWires.add(wire);
				wiresByName.put(wireName, wire);
				logger.info("Found wire (with assignment) in module " + currentModuleName + 
					": " + wireName + (range != null ? " " + range : ""));
			}
		}
	}
	
	@Override
	public void exitModule_instantiation(VerilogParser.Module_instantiationContext ctx) {
		String instantiatedModuleName = ctx.module_identifier().getText();
		logger.info("Found module instantiation in " + currentModuleName + ": " + instantiatedModuleName);
		
		try {
			// Look up the module definition
			Module moduleDefinition = availableModules.get(instantiatedModuleName);
			if (moduleDefinition == null) {
				logger.info("Warning: Module definition '" + instantiatedModuleName + "' not found");
				return;
			}
			
			// Process each instance of this module
			for (VerilogParser.Module_instanceContext moduleInstance : ctx.module_instance()) {
				String instanceName = moduleInstance.name_of_module_instance().getText();
				
				// Process named port connections
				if (moduleInstance.list_of_port_connections() != null && 
					moduleInstance.list_of_port_connections().named_port_connection() != null) {
					
					for (VerilogParser.Named_port_connectionContext namedPort : 
						moduleInstance.list_of_port_connections().named_port_connection()) {
						
						String portName = namedPort.port_identifier().getText();
						
						// Get the expression (usually the wire name)
						if (namedPort.expression() != null) {
							String signalName = namedPort.expression().getText();
							
							// Find or create the wire/signal
							Wire wire = wiresByName.get(signalName);
							boolean isNewImplicitWire = false;
							Port topModulePort = null;
							
							if (wire == null) {
								// Check if it's an input/output port of the current module
								Port modulePort = findPortInModule(signalName);
								if (modulePort != null) {
									// Create an implicit wire for this module port
									wire = new Wire(signalName, modulePort.range);
									wiresByName.put(signalName, wire);
									currentWires.add(wire);
									isNewImplicitWire = true;
									topModulePort = modulePort;
									logger.info("  Created implicit wire for module port: " + signalName);
								} else {
									// Create an implicit wire for undeclared signal
									wire = new Wire(signalName, null);
									wiresByName.put(signalName, wire);
									currentWires.add(wire);
									logger.info("  Created implicit wire for undeclared signal: " + signalName);
								}
							}
							
							// If this is a new implicit wire from a top module port, add the top module port connection
							if (isNewImplicitWire && topModulePort != null) {
								// Create a pseudo-module to represent the current (parent) module
								Module currentModuleObj = availableModules.get(currentModuleName);
								if (currentModuleObj == null) {
									// Create a minimal module representation for the current module
									currentModuleObj = new Module(currentModuleName, currentModuleInputs, currentModuleOutputs);
								}
								
								// Create a copy of the port with module info
								Port topPortCopy = new Port(topModulePort.direction, topModulePort.type, topModulePort.range, topModulePort.name);
								topPortCopy.module = currentModuleObj;
								
								// Add the top module port connection
								wire.ports.add(topPortCopy);
								wire.modules.add(currentModuleObj);
								
								logger.info("  Added top module port connection for: " + signalName + " (" + topModulePort.direction + ")");
							}
							
							// Find the port in the instantiated module definition
							Port port = moduleDefinition.getPort(portName);
							if (port != null) {
								// Create a copy of the port with module info
								Port portCopy = new Port(port.direction, port.type, port.range, port.name);
								portCopy.module = moduleDefinition;
								
								// Add connection to wire
								wire.ports.add(portCopy);
								wire.modules.add(moduleDefinition);
								
								logger.info("  Connected signal '" + signalName + "' to " + 
									instantiatedModuleName + "." + portName + " (" + port.direction + ")");
							} else {
								logger.info("  Warning: Port '" + portName + "' not found in module '" + 
									instantiatedModuleName + "'");
							}
						}
					}
				}
			}
		} catch (Exception ex) {
			System.err.println("Error processing module instantiation: " + ex.getMessage());
			ex.printStackTrace();
		}
	}
	
	/**
	 * Find a port in the current module's input/output ports
	 */
	private Port findPortInModule(String portName) {
		for (Port port : currentModuleInputs) {
			if (port.name.equals(portName)) {
				return port;
			}
		}
		for (Port port : currentModuleOutputs) {
			if (port.name.equals(portName)) {
				return port;
			}
		}
		return null;
	}

	@Override
	public void exitModule_declaration(VerilogParser.Module_declarationContext ctx) {
		// Store wires for this module
		if (currentModuleName != null) {
			if (!moduleWires.containsKey(currentModuleName)) {
				moduleWires.put(currentModuleName, new ArrayList<>());
			}
			moduleWires.get(currentModuleName).addAll(currentWires);
			logger.info("Module " + currentModuleName + " has " + currentWires.size() + " wires");
		}
	}
}