SCViewer/plugins/com.minres.scviewer.databas.../src/com/minres/scviewer/database/vcd/VCDFileParser.java

246 lines
7.3 KiB
Java
Raw Normal View History

/*******************************************************************************
2021-01-09 14:26:49 +01:00
* Copyright (c) 2015-2021 MINRES Technologies GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* MINRES Technologies GmbH - initial API and implementation
*******************************************************************************/
2015-01-06 17:14:16 +01:00
package com.minres.scviewer.database.vcd;
import java.io.*;
import java.text.ParseException;
2015-01-06 17:14:16 +01:00
import java.util.*;
import com.minres.scviewer.database.BitValue;
2015-01-09 09:16:40 +01:00
import com.minres.scviewer.database.BitVector;
2023-03-04 12:33:22 +01:00
import com.minres.scviewer.database.IWaveformDb;
2015-01-09 09:16:40 +01:00
2015-01-06 17:14:16 +01:00
class VCDFileParser {
private StreamTokenizer tokenizer;
private IVCDDatabaseBuilder traceBuilder;
private HashMap<String, Integer> nameToNetMap = new HashMap<>();
2023-03-04 12:33:22 +01:00
private long timeScaleFactor;
2015-01-06 17:14:16 +01:00
private boolean stripNetWidth;
private boolean replaceColon;
2015-01-06 17:14:16 +01:00
long currentTime;
public VCDFileParser(boolean stripNetWidth) {
this.stripNetWidth=stripNetWidth;
this.replaceColon=false;
2015-01-06 17:14:16 +01:00
}
public boolean load(InputStream is, IVCDDatabaseBuilder builder) {
tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(is)));
tokenizer.resetSyntax();
tokenizer.wordChars(33, 126);
tokenizer.whitespaceChars('\r', '\r');
tokenizer.whitespaceChars('\n', '\n');
tokenizer.whitespaceChars(' ', ' ');
tokenizer.whitespaceChars('\t', '\t');
try {
traceBuilder = builder;
currentTime=0;
while (parseDefinition());
while (parseTransition());
return true;
} catch (Exception exc) {
exc.printStackTrace();
return false;
}
}
private void parseScope() throws IOException, ParseException {
2015-01-06 17:14:16 +01:00
nextToken(); // Scope type (ignore)
nextToken();
traceBuilder.enterModule(tokenizer.sval);
match("$end");
}
private void parseUpscope() throws IOException, ParseException {
2015-01-06 17:14:16 +01:00
match("$end");
traceBuilder.exitModule();
}
private void parseVar() throws IOException {
2015-01-06 17:14:16 +01:00
nextToken(); // type
String type = tokenizer.sval;
2015-01-06 17:14:16 +01:00
nextToken(); // size
int width = Integer.parseInt(tokenizer.sval);
if("real".equals(type))
width=0;
2015-01-06 17:14:16 +01:00
nextToken();
String id = tokenizer.sval;
nextToken();
StringBuilder sb = new StringBuilder();
sb.append(tokenizer.sval);
2015-01-06 17:14:16 +01:00
while (nextToken() && !tokenizer.sval.equals("$end")) {
sb.append(tokenizer.sval);
2015-01-06 17:14:16 +01:00
}
String netName = sb.toString();
2015-01-06 17:14:16 +01:00
Integer net = nameToNetMap.get(id);
if (net == null) { // We've never seen this net before
int openBracket = netName.indexOf('[');
2015-01-06 17:14:16 +01:00
if(stripNetWidth){
if (openBracket != -1) netName = netName.substring(0, openBracket);
openBracket = -1;
}
if(replaceColon) {
if (openBracket != -1) {
netName = netName.substring(0, openBracket).replace(":", ".")+netName.substring(openBracket);
} else
netName=netName.replace(":", ".");
2015-01-06 17:14:16 +01:00
}
nameToNetMap.put(id, traceBuilder.newNet(netName, -1, width));
} else {
// Shares data with existing net. Add as clone.
traceBuilder.newNet(netName, net, width);
}
}
private void parseComment() throws IOException {
nextToken();
StringBuilder s = new StringBuilder();
s.append(tokenizer.sval);
nextToken();
while(!tokenizer.sval.equals("$end")){
s.append(" ").append(tokenizer.sval);
nextToken();
}
replaceColon|=s.toString().contains("ARTERIS Architecture");
}
private void parseTimescale() throws IOException {
2015-01-06 17:14:16 +01:00
nextToken();
StringBuilder sb = new StringBuilder();
sb.append(tokenizer.sval);
2015-01-06 17:14:16 +01:00
nextToken();
while(!tokenizer.sval.equals("$end")){
sb.append(" ").append(tokenizer.sval);
2015-01-06 17:14:16 +01:00
nextToken();
}
String s = sb.toString();
2023-03-04 12:33:22 +01:00
int fac =1;
2015-01-06 17:14:16 +01:00
switch (s.charAt(s.length() - 2)){
2023-03-04 12:33:22 +01:00
case 'f': // Nano-seconds
fac = -15;
s = s.substring(0, s.length() - 2).trim();
break;
2015-01-06 17:14:16 +01:00
case 'p': // Nano-seconds
2023-03-04 12:33:22 +01:00
fac = -12;
2015-01-06 17:14:16 +01:00
s = s.substring(0, s.length() - 2).trim();
break;
case 'n': // Nano-seconds
2023-03-04 12:33:22 +01:00
fac = -9;
2015-01-06 17:14:16 +01:00
s = s.substring(0, s.length() - 2).trim();
break;
case 'u': // Microseconds
2023-03-04 12:33:22 +01:00
fac = -6;
2015-01-06 17:14:16 +01:00
s = s.substring(0, s.length() - 2).trim();
break;
case 'm': // Microseconds
2023-03-04 12:33:22 +01:00
fac = -3;
2015-01-06 17:14:16 +01:00
s = s.substring(0, s.length() - 2).trim();
break;
default: // Seconds
2023-03-04 12:33:22 +01:00
fac = 1;
2015-01-06 17:14:16 +01:00
s = s.substring(0, s.length() - 1);
break;
}
2023-03-04 12:33:22 +01:00
timeScaleFactor = 1;
for(int i = 1; i<= fac-IWaveformDb.databaseTimeScale; i++)
timeScaleFactor *= 10;
timeScaleFactor = Long.parseLong(s);
2015-01-06 17:14:16 +01:00
}
private boolean parseDefinition() throws IOException, ParseException {
2015-01-06 17:14:16 +01:00
nextToken();
if (tokenizer.sval.equals("$scope"))
parseScope();
else if (tokenizer.sval.equals("$var"))
parseVar();
else if (tokenizer.sval.equals("$upscope"))
parseUpscope();
else if (tokenizer.sval.equals("$timescale"))
parseTimescale();
else if (tokenizer.sval.equals("$comment"))
parseComment();
2015-01-06 17:14:16 +01:00
else if (tokenizer.sval.equals("$enddefinitions")) {
match("$end");
return false;
2021-01-09 20:10:58 +01:00
} else do {
if (!nextToken()) return false;
} while (!tokenizer.sval.equals("$end"));
2015-01-06 17:14:16 +01:00
return true;
}
private boolean parseTransition() throws IOException {
2015-01-06 17:14:16 +01:00
if (!nextToken()) return false;
if (tokenizer.sval.charAt(0) == '#') { // If the line begins with a #, this is a timestamp.
2023-03-04 12:33:22 +01:00
currentTime = Long.parseLong(tokenizer.sval.substring(1)) * timeScaleFactor;
2015-01-06 17:14:16 +01:00
} else {
if(tokenizer.sval.equals("$comment")){
do {
if (!nextToken()) return false;
} while (!tokenizer.sval.equals("$end"));
return true;
}
if (tokenizer.sval.equals("$dumpvars") || tokenizer.sval.equals("$end"))
return true;
String value;
String id;
if (tokenizer.sval.charAt(0) == 'b' || tokenizer.sval.charAt(0) == 'r') {
// Multiple value net. Value appears first, followed by space, then identifier
2015-01-06 17:14:16 +01:00
value = tokenizer.sval.substring(1);
nextToken();
id = tokenizer.sval;
} else {
// Single value net. identifier first, then value, no space.
value = tokenizer.sval.substring(0, 1);
id = tokenizer.sval.substring(1);
}
Integer net = nameToNetMap.get(id);
if (net == null)
2015-01-06 17:14:16 +01:00
return true;
int netWidth = traceBuilder.getNetWidth(net);
if(netWidth==0) {
if("nan".equals(value))
traceBuilder.appendTransition(net, currentTime, Double.NaN);
else
traceBuilder.appendTransition(net, currentTime, Double.parseDouble(value));
2015-01-06 17:14:16 +01:00
} else {
2023-02-27 13:07:10 +01:00
BitVector decodedValues;
if (value.equals("z") && netWidth > 1) {
2023-02-27 13:07:10 +01:00
decodedValues = new BitVector(netWidth);
for (int i = 0; i < netWidth; i++)
decodedValues.setValue(i, BitValue.Z);
} else if (value.equals("x") && netWidth > 1) {
2023-02-27 13:07:10 +01:00
decodedValues = new BitVector(netWidth);
for (int i = 0; i < netWidth; i++)
decodedValues.setValue(i, BitValue.X);
} else {
2023-02-27 13:07:10 +01:00
decodedValues = BitVector.fromString(netWidth, value);
2015-01-06 17:14:16 +01:00
}
traceBuilder.appendTransition(net, currentTime, decodedValues);
2015-01-06 17:14:16 +01:00
}
}
return true;
}
private void match(String value) throws ParseException, IOException {
2015-01-06 17:14:16 +01:00
nextToken();
if (!tokenizer.sval.equals(value))
throw new ParseException("Line "+tokenizer.lineno()+": parse error, expected "+value+" got "+tokenizer.sval, tokenizer.lineno());
2015-01-06 17:14:16 +01:00
}
private boolean nextToken() throws IOException {
return tokenizer.nextToken() != StreamTokenizer.TT_EOF;
}
}