Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add setting for Verilog external code library path #1321

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@
<version>2.0.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>

</dependencies>

<scm>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/de/neemann/digital/core/element/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -1028,4 +1028,10 @@ private static File getATMISPPath() {
*/
public static final Key<Boolean> SKIP_HDL =
new Key<>("skipHDL", false).setSecondary();

/**
* verilog code lib path, find include and depends
*/
public static final Key<File> SETTINGS_VERILOG_LIB_DIR =
new Key.KeyFile("verilogLib", new File("")).setDirectoryOnly(true).setSecondary();
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ enum Type {
/**
* Icarus verilog interpreter
*/
IVERILOG
IVERILOG,
/**
* extern socket app interpreter
*/
Socket
}

/**
Expand All @@ -83,6 +87,8 @@ static Application create(Type type, ElementAttributes attr) {
return new ApplicationGHDL(attr);
case IVERILOG:
return new ApplicationIVerilog(attr);
case Socket:
return new ApplicationSocket();
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ApplicationIVerilog extends ApplicationVerilogStdIO {
private String iverilogFolder;
private String iverilog;
private String vvp;
private String verilogLibPath;

/**
* Initialize a new instance
Expand All @@ -39,6 +40,21 @@ public ApplicationIVerilog(ElementAttributes attr) {
this.attr = attr;
iverilogFolder = "";
hasIverilog = findIVerilog();
verilogLibPath = getVerilogLibPath();
}

/**
* verilog code lib path, find includes and depends code
* support config with env vars
* @return lib path
*/
private String getVerilogLibPath() {
String libPath = Settings.getInstance().get(Keys.SETTINGS_VERILOG_LIB_DIR).getAbsolutePath();
if (libPath == null || libPath.equals("")) {
return "";
}
libPath = "-y"+ApplicationIVerilog.replaceEnvVars(libPath);
return libPath;
}

@Override
Expand All @@ -60,6 +76,7 @@ public ProcessInterface start(String label, String code, PortDefinition inputs,
.add("-o")
.add(testOutputName)
.add(attr, Keys.IVERILOG_OPTIONS)
.addString(this.verilogLibPath)
.add(file.getName())
.getArray()
);
Expand Down Expand Up @@ -106,6 +123,7 @@ public String checkCode(String label, String code, PortDefinition inputs, PortDe
.add("-o")
.add(testOutputName)
.add(attr, Keys.IVERILOG_OPTIONS)
.addString(this.verilogLibPath)
.add(file.getName())
.getArray()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Ron Ren.
* Use of this source code is governed by the GPL v3 license
* that can be found in the LICENSE file.
*/
package de.neemann.digital.core.extern;

import de.neemann.digital.core.extern.handler.ProcessInterface;
import de.neemann.digital.core.extern.handler.SocketInterface;

import java.io.File;
import java.io.IOException;

/**
* application with shared mem map file comm
*/
public class ApplicationSocket implements Application {
@Override
public ProcessInterface start(String label, String code, PortDefinition inputs, PortDefinition outputs, File root) throws IOException {
String ipPort = "127.0.0.1:8009";
if (code.length() > 0 && code.indexOf(":") > 0) {
ipPort = code;
}
return new SocketInterface(ipPort, inputs, outputs);
}

@Override
public boolean checkSupported() {
return true;
}

@Override
public String checkCode(String label, String code, PortDefinition inputs, PortDefinition outputs, File root) throws IOException {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.io.*;
import java.nio.file.Files;
import java.util.NoSuchElementException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
Expand Down Expand Up @@ -208,6 +210,30 @@ private void scanPort(VerilogTokenizer st, PortDefinition in, PortDefinition out
}
}

/**
* replace env string in options
* @param input option string
* @return replace every ${xx} env vars
*/
public static String replaceEnvVars(String input) {
Pattern pattern = Pattern.compile("(?<!\\\\)\\$\\{([^}]+)\\}");
Matcher matcher = pattern.matcher(input);
StringBuffer result = new StringBuffer();

while (matcher.find()) {
String varName = matcher.group(1);
String envValue = System.getenv(varName);
if (envValue == null) {
envValue = "";
}
matcher.appendReplacement(result, envValue);
}

matcher.appendTail(result);

return result.toString().replace("\\$", "$").replace("\\", "\\\\");
}

private static final class ParseException extends Exception {
private ParseException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2024 Ron Ren.
* Use of this source code is governed by the GPL v3 license
* that can be found in the LICENSE file.
*/
package de.neemann.digital.core.extern.handler;

import de.neemann.digital.core.ObservableValue;
import de.neemann.digital.core.ObservableValues;
import de.neemann.digital.core.extern.Port;
import de.neemann.digital.core.extern.PortDefinition;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.BitSet;

/**
* signal comm out from extern app with shared mem map file
*/
public class SocketInterface implements ProcessInterface {

private TCPClient tcpClient;

private int inputBits;
private int outputBits;
private BitSet inSeta;
private BitSet inSetb;
private int inLen;
private BitSet outSet;
private ByteBuffer buf1;
private ByteBuffer buf2;


/**
* mem map init
* @param ipPort address and port for extern
* @param inputs signal inputs
* @param outputs signal outputs
*/
public SocketInterface(String ipPort, PortDefinition inputs, PortDefinition outputs) {
String[] net = ipPort.split(":");
this.tcpClient = new TCPClient(net[0], Integer.parseInt(net[1]));
this.inputBits = 0;
this.outputBits = 0;
for (Port input : inputs) {
this.inputBits += input.getBits();
this.inLen = this.inputBits;
// match the VPI t_vpi_value type,t_vpi_vecval use aval and bval with int32
while (this.inLen % 32 != 0) {
this.inLen++;
}
this.inSeta = new BitSet(this.inLen);
this.inSetb = new BitSet(this.inLen);
this.buf1 = ByteBuffer.allocate(this.inLen/8);
this.buf2 = ByteBuffer.allocate(this.inLen/8);
}
for (Port output : outputs) {
this.outputBits += output.getBits();
}
}

private void initSocket() throws IOException {
if (!this.tcpClient.getIsRunning()) {
this.tcpClient.start();
}
}

private static byte[] concatenateByteArrays(byte[] array1, byte[] array2) {
byte[] result = new byte[array1.length + array2.length]; // 新数组,长度为两个数组的总和

System.arraycopy(array1, 0, result, 0, array1.length); // 将array1复制到result
System.arraycopy(array2, 0, result, array1.length, array2.length); // 将array2复制到result

return result;
}

@Override
public void writeValues(ObservableValues values) throws IOException {
this.initSocket();
int pos = 0;
for (ObservableValue v : values) {
for (int i=0; i<v.getBits(); i++) {
long mask = (1L << i);
long value = v.getValue() & mask;
long hZ = v.getHighZ() & mask;
/* ab encoding: 00=0, 10=1, 11=X, 01=Z */
if (value > 0) {
this.inSeta.set(pos, true);
this.inSetb.set(pos, false);
} else if (hZ > 0) {
this.inSeta.set(pos, false);
this.inSetb.set(pos, true);
} else {
this.inSeta.set(pos, false);
this.inSetb.set(pos, false);
}
pos++;
}
}
this.buf1.clear();
this.buf2.clear();
this.buf1.put(this.inSeta.toByteArray());
this.buf2.put(this.inSetb.toByteArray());
while (this.buf1.position() < this.inLen/8) {
this.buf1.put((byte) 0);
}
while (this.buf2.position() < this.inLen/8) {
this.buf2.put((byte) 0);
}
try {
this.tcpClient.sendMessage(SocketInterface.concatenateByteArrays(this.buf1.array(), this.buf2.array()));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
public void readValues(ObservableValues values) throws IOException {
this.initSocket();
byte[] buf = this.tcpClient.receiveMessage();
if (buf == null) {
return;
}
int posAdd = (buf.length*8)/2;
this.outSet = BitSet.valueOf(buf);
int pos = 0;
for (ObservableValue v : values) {
long value = 0L;
long hZ = 0L;
for (int i=0; i<v.getBits(); i++) {
long mask = (1L << i);
/* ab encoding: 00=0, 10=1, 11=X, 01=Z */
boolean l = this.outSet.get(pos);
boolean r = this.outSet.get(pos + posAdd);
if (l && !r) {
value = value | mask;
} else if (!l && r) {
hZ = hZ | mask;
}
pos++;
}
v.set(value, hZ);
}
}

@Override
public void close() throws IOException {
this.tcpClient.close();
}
}
Loading