SCViewer/com.minres.scviewer.databas.../src/com/minres/scviewer/database/vcd/VCDDbLoader.java

207 lines
6.1 KiB
Java
Raw Normal View History

/*******************************************************************************
2015-10-22 00:25:12 +02:00
* Copyright (c) 2015 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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.Collections;
2015-01-06 17:14:16 +01:00
import java.util.List;
import java.util.NavigableMap;
2015-01-06 17:14:16 +01:00
import java.util.Stack;
2015-01-20 18:50:15 +01:00
import java.util.TreeMap;
2015-01-06 17:14:16 +01:00
import java.util.Vector;
2015-01-09 09:16:40 +01:00
import com.minres.scviewer.database.BitVector;
2015-01-06 17:14:16 +01:00
import com.minres.scviewer.database.ISignal;
import com.minres.scviewer.database.IWaveform;
2015-01-20 18:50:15 +01:00
import com.minres.scviewer.database.IWaveformDb;
import com.minres.scviewer.database.IWaveformDbLoader;
2015-01-06 17:14:16 +01:00
import com.minres.scviewer.database.InputFormatException;
import com.minres.scviewer.database.RelationType;
2015-01-06 17:14:16 +01:00
/**
* The Class VCDDb.
*/
public class VCDDbLoader implements IWaveformDbLoader, IVCDDatabaseBuilder {
2015-01-06 17:14:16 +01:00
/** The Constant TIME_RES. */
2015-01-20 18:50:15 +01:00
private static final Long TIME_RES = 1000L; // ps;
2015-01-06 17:14:16 +01:00
/** The db. */
private IWaveformDb db;
2015-01-06 17:14:16 +01:00
/** The module stack. */
private Stack<String> moduleStack;
/** The signals. */
private List<IWaveform> signals;
2015-01-06 17:14:16 +01:00
/** The max time. */
2015-01-06 17:14:16 +01:00
private long maxTime;
/**
* Instantiates a new VCD db.
*/
public VCDDbLoader() {
2015-01-06 17:14:16 +01:00
}
/** The date bytes. */
private byte[] dateBytes = "$date".getBytes();
2015-01-06 17:14:16 +01:00
/* (non-Javadoc)
* @see com.minres.scviewer.database.ITrDb#load(java.io.File)
*/
2015-01-20 18:50:15 +01:00
@SuppressWarnings("unchecked")
2015-01-06 17:14:16 +01:00
@Override
public boolean load(IWaveformDb db, File file) throws Exception {
this.db=db;
this.maxTime=0;
try {
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[dateBytes.length];
int read = fis.read(buffer, 0, dateBytes.length);
fis.close();
if (read == dateBytes.length)
for (int i = 0; i < dateBytes.length; i++)
if (buffer[i] != dateBytes[i])
return false;
} catch(FileNotFoundException e) {
return false;
}
signals = new Vector<IWaveform>();
2015-01-06 17:14:16 +01:00
moduleStack= new Stack<String>();
boolean res = new VCDFileParser(false).load(new FileInputStream(file), this);
2015-01-06 17:14:16 +01:00
moduleStack=null;
if(!res) throw new InputFormatException();
2015-01-20 18:50:15 +01:00
// calculate max time of database
for(IWaveform waveform:signals) {
NavigableMap<Long, ?> events =((ISignal<?>)waveform).getEvents();
if(events.size()>0)
maxTime= Math.max(maxTime, events.lastKey());
}
2015-01-20 18:50:15 +01:00
// extend signals to hav a last value set at max time
for(IWaveform s:signals){
if(s instanceof VCDSignal<?>) {
TreeMap<Long,?> events = (TreeMap<Long, ?>) ((VCDSignal<?>)s).getEvents();
if(events.size()>0 && events.lastKey()<maxTime){
Object val = events.lastEntry().getValue();
if(val instanceof BitVector) {
((VCDSignal<BitVector>)s).addSignalChange(maxTime, (BitVector) val);
} else if(val instanceof Double)
((VCDSignal<Double>)s).addSignalChange(maxTime, (Double) val);
}
2015-01-06 17:14:16 +01:00
}
}
return true;
2015-01-06 17:14:16 +01:00
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.ITrDb#getMaxTime()
*/
@Override
2015-01-20 18:50:15 +01:00
public Long getMaxTime() {
return maxTime;
2015-01-06 17:14:16 +01:00
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.ITrDb#getAllWaves()
*/
@Override
public List<IWaveform> getAllWaves() {
2015-01-06 17:14:16 +01:00
return signals;
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.vcd.ITraceBuilder#enterModule(java.lang.String)
*/
@Override
public void enterModule(String tokenString) {
2018-07-16 13:17:58 +02:00
if(moduleStack.isEmpty()) {
if("SystemC".compareTo(tokenString)!=0) moduleStack.push(tokenString);
} else
2015-01-06 17:14:16 +01:00
moduleStack.push(moduleStack.peek()+"."+tokenString);
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.vcd.ITraceBuilder#exitModule()
*/
@Override
public void exitModule() {
if(!moduleStack.isEmpty()) moduleStack.pop();
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.vcd.ITraceBuilder#newNet(java.lang.String, int, int)
*/
2015-01-20 18:50:15 +01:00
@SuppressWarnings("unchecked")
2015-01-06 17:14:16 +01:00
@Override
2018-07-16 13:17:58 +02:00
public Integer newNet(String name, int i, int width) {
String netName = moduleStack.empty()? name: moduleStack.lastElement()+"."+name;
2015-01-06 17:14:16 +01:00
int id = signals.size();
assert(width>=0);
if(width==0) {
signals.add( i<0 ? new VCDSignal<Double>(db, id, netName, width) :
new VCDSignal<Double>((VCDSignal<Double>)signals.get(i), id, netName));
} else if(width>0){
signals.add( i<0 ? new VCDSignal<BitVector>(db, id, netName, width) :
new VCDSignal<BitVector>((VCDSignal<BitVector>)signals.get(i), id, netName));
}
2015-01-06 17:14:16 +01:00
return id;
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.vcd.ITraceBuilder#getNetWidth(int)
*/
@Override
public int getNetWidth(int intValue) {
VCDSignal<?> signal = (VCDSignal<?>) signals.get(intValue);
2015-01-06 17:14:16 +01:00
return signal.getWidth();
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.vcd.ITraceBuilder#appendTransition(int, long, com.minres.scviewer.database.vcd.BitVector)
*/
@SuppressWarnings("unchecked")
@Override
public void appendTransition(int signalId, long currentTime, BitVector value) {
VCDSignal<BitVector> signal = (VCDSignal<BitVector>) signals.get(signalId);
Long time = currentTime* TIME_RES;
signal.getEvents().put(time, value);
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.vcd.ITraceBuilder#appendTransition(int, long, com.minres.scviewer.database.vcd.BitVector)
*/
@SuppressWarnings("unchecked")
@Override
public void appendTransition(int signalId, long currentTime, double value) {
VCDSignal<?> signal = (VCDSignal<?>) signals.get(signalId);
Long time = currentTime* TIME_RES;
if(signal.getWidth()==0){
((VCDSignal<Double>)signal).getEvents().put(time, value);
2015-01-06 17:14:16 +01:00
}
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.IWaveformDbLoader#getAllRelationTypes()
*/
@Override
public Collection<RelationType> getAllRelationTypes(){
return Collections.emptyList();
}
2015-01-06 17:14:16 +01:00
}