/******************************************************************************* * 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 *******************************************************************************/ package com.minres.scviewer.database.vcd; import java.io.File; import java.io.FileInputStream; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Stack; import java.util.TreeMap; import java.util.Vector; import com.minres.scviewer.database.BitVector; import com.minres.scviewer.database.ISignal; import com.minres.scviewer.database.ISignalChange; import com.minres.scviewer.database.ISignalChangeMulti; import com.minres.scviewer.database.ISignalChangeSingle; import com.minres.scviewer.database.IWaveform; import com.minres.scviewer.database.IWaveformDb; import com.minres.scviewer.database.IWaveformDbLoader; import com.minres.scviewer.database.IWaveformEvent; import com.minres.scviewer.database.InputFormatException; import com.minres.scviewer.database.RelationType; // TODO: Auto-generated Javadoc /** * The Class VCDDb. */ public class VCDDbLoader implements IWaveformDbLoader, IVCDDatabaseBuilder { private static final Long TIME_RES = 1000L; // ps; private IWaveformDb db; /** The module stack. */ private Stack moduleStack; /** The signals. */ private List> signals; private long maxTime; /** * Instantiates a new VCD db. * * @param netName the net name */ public VCDDbLoader() { } private byte[] x = "$date".getBytes(); /* (non-Javadoc) * @see com.minres.scviewer.database.ITrDb#load(java.io.File) */ @SuppressWarnings("unchecked") @Override public boolean load(IWaveformDb db, File file) throws Exception { this.db=db; FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[x.length]; int read = fis.read(buffer, 0, x.length); fis.close(); if (read == x.length) for (int i = 0; i < x.length; i++) if (buffer[i] != x[i]) return false; signals = new Vector>(); moduleStack= new Stack(); boolean res = new VCDFileParser(false).load(new FileInputStream(file), this); moduleStack=null; if(!res) throw new InputFormatException(); // calculate max time of database for(IWaveform waveform:signals) maxTime= Math.max(maxTime, ((ISignal)waveform).getEvents().lastKey()); // extend signals to hav a last value set at max time for(IWaveform waveform:signals){ TreeMap events = ((VCDSignal)waveform).values; if(events.lastKey())waveform).values.put(maxTime, new VCDSignalChangeSingle(maxTime, ((ISignalChangeSingle)x).getValue())); else if(x instanceof ISignalChangeMulti) ((VCDSignal)waveform).values.put(maxTime, new VCDSignalChangeMulti(maxTime, ((ISignalChangeMulti)x).getValue())); } } return true; } /* (non-Javadoc) * @see com.minres.scviewer.database.ITrDb#getMaxTime() */ @Override public Long getMaxTime() { return maxTime; } /* (non-Javadoc) * @see com.minres.scviewer.database.ITrDb#getAllWaves() */ @Override public List> getAllWaves() { return signals; } /* (non-Javadoc) * @see com.minres.scviewer.database.vcd.ITraceBuilder#enterModule(java.lang.String) */ @Override public void enterModule(String tokenString) { if(moduleStack.isEmpty()) moduleStack.push(tokenString); else 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) */ @SuppressWarnings("unchecked") @Override public Integer newNet(String netName, int i, int width) { int id = signals.size(); VCDSignal signal; if(width==1){ signal = i<0 ? new VCDSignal(db, id, netName) : new VCDSignal((VCDSignal)signals.get(i), id, netName); } else { signal = i<0 ? new VCDSignal(db, id, netName, width) : new VCDSignal((VCDSignal)signals.get(i), id, netName); }; signals.add(signal); 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); 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 decodedValues) { VCDSignal signal = (VCDSignal) signals.get(signalId); Long time = currentTime* TIME_RES; if(signal.getWidth()==1){ ((VCDSignal)signal).values.put(time, new VCDSignalChangeSingle(time, decodedValues.getValue()[0])); } else { ((VCDSignal)signal).values.put(time, new VCDSignalChangeMulti(time, decodedValues)); } } @Override public Collection getAllRelationTypes(){ return Collections.emptyList(); } }