2012-04-13 01:08:20 +00:00
|
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Resolve module/signal name references
|
|
|
|
|
//
|
2008-04-25 12:14:27 +00:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2013-01-01 14:42:59 +00:00
|
|
|
|
// Copyright 2003-2013 by Wilson Snyder. This program is free software; you can
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 21:07:57 +00:00
|
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
// Verilator is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// LINKTOP TRANSFORMATIONS:
|
|
|
|
|
// Utility functions
|
|
|
|
|
// Sort cells by depth
|
|
|
|
|
// Create new MODULE TOP with connections to below signals
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2008-06-30 17:11:25 +00:00
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdarg>
|
2006-08-26 11:35:28 +00:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3LinkLevel.h"
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Levelizing class functions
|
|
|
|
|
|
|
|
|
|
struct CmpLevel {
|
2009-11-07 11:20:20 +00:00
|
|
|
|
inline bool operator () (const AstNodeModule* lhsp, const AstNodeModule* rhsp) const {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
return lhsp->level() < rhsp->level();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void V3LinkLevel::modSortByLevel() {
|
|
|
|
|
// Sort modules by levels, root down to lowest children
|
|
|
|
|
// Calculate levels again in case we added modules
|
|
|
|
|
UINFO(2,"modSortByLevel()\n");
|
2008-03-25 19:57:41 +00:00
|
|
|
|
|
2008-12-10 02:05:47 +00:00
|
|
|
|
// level() was computed for us in V3LinkCells
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
2009-11-07 11:20:20 +00:00
|
|
|
|
typedef vector<AstNodeModule*> ModVec;
|
|
|
|
|
|
|
|
|
|
ModVec vec;
|
|
|
|
|
AstNodeModule* topp = NULL;
|
|
|
|
|
for (AstNodeModule* nodep = v3Global.rootp()->modulesp(); nodep; nodep=nodep->nextp()->castNodeModule()) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (nodep->level()<=2) {
|
2008-03-25 19:57:41 +00:00
|
|
|
|
if (topp) {
|
2009-11-30 23:36:31 +00:00
|
|
|
|
nodep->v3warn(E_MULTITOP, "Unsupported: Multiple top level modules: "
|
2008-03-25 19:57:41 +00:00
|
|
|
|
<<nodep->prettyName()<<" and "<<topp->prettyName());
|
2009-11-30 23:36:31 +00:00
|
|
|
|
nodep->v3warn(E_MULTITOP, "Fix, or use --top-module option to select which you want.");
|
2008-03-25 19:57:41 +00:00
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
topp = nodep;
|
|
|
|
|
}
|
|
|
|
|
vec.push_back(nodep);
|
|
|
|
|
}
|
|
|
|
|
sort(vec.begin(), vec.end(), CmpLevel()); // Sort the vector
|
2009-11-07 11:20:20 +00:00
|
|
|
|
for (ModVec::iterator it = vec.begin(); it != vec.end(); ++it) {
|
|
|
|
|
AstNodeModule* nodep = *it;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
nodep->unlinkFrBack();
|
|
|
|
|
}
|
|
|
|
|
if (v3Global.rootp()->modulesp()) v3Global.rootp()->v3fatalSrc("Unlink didn't work");
|
2009-11-07 11:20:20 +00:00
|
|
|
|
for (ModVec::iterator it = vec.begin(); it != vec.end(); ++it) {
|
|
|
|
|
AstNodeModule* nodep = *it;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
v3Global.rootp()->addModulep(nodep);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Wrapping
|
|
|
|
|
|
|
|
|
|
void V3LinkLevel::wrapTop(AstNetlist* netlistp) {
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
|
|
|
|
// We do ONLY the top module
|
2009-11-07 11:20:20 +00:00
|
|
|
|
AstNodeModule* oldmodp = netlistp->modulesp();
|
2009-04-24 14:32:11 +00:00
|
|
|
|
if (!oldmodp) netlistp->v3fatalSrc("No module found to process");
|
2009-11-07 11:20:20 +00:00
|
|
|
|
AstNodeModule* newmodp = new AstModule(oldmodp->fileline(), (string)"TOP_"+oldmodp->name());
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Make the new module first in the list
|
|
|
|
|
oldmodp->unlinkFrBackWithNext();
|
|
|
|
|
newmodp->addNext(oldmodp);
|
|
|
|
|
newmodp->level(1);
|
|
|
|
|
newmodp->modPublic(true);
|
|
|
|
|
netlistp->addModulep(newmodp);
|
|
|
|
|
|
|
|
|
|
// Add instance
|
|
|
|
|
AstCell* cellp = new AstCell(newmodp->fileline(),
|
|
|
|
|
(v3Global.opt.l2Name() ? "v" : oldmodp->name()),
|
|
|
|
|
oldmodp->name(),
|
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
|
cellp->modp(oldmodp);
|
|
|
|
|
newmodp->addStmtp(cellp);
|
|
|
|
|
|
|
|
|
|
// Add pins
|
|
|
|
|
for (AstNode* subnodep=oldmodp->stmtsp(); subnodep; subnodep = subnodep->nextp()) {
|
|
|
|
|
if (AstVar* oldvarp=subnodep->castVar()) {
|
|
|
|
|
UINFO(8,"VARWRAP "<<oldvarp<<endl);
|
|
|
|
|
if (oldvarp->isIO()) {
|
2008-11-20 01:15:05 +00:00
|
|
|
|
AstVar* varp = oldvarp->cloneTree(false);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
newmodp->addStmtp(varp);
|
|
|
|
|
varp->sigPublic(true); // User needs to be able to get to it...
|
2006-09-25 20:40:52 +00:00
|
|
|
|
if (oldvarp->isIO()) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
oldvarp->primaryIO(true);
|
|
|
|
|
varp->primaryIO(true);
|
|
|
|
|
}
|
|
|
|
|
if (varp->isIO() && v3Global.opt.systemC()) varp->sc(true);
|
|
|
|
|
|
|
|
|
|
AstPin* pinp = new AstPin(oldvarp->fileline(),0,oldvarp->name(),
|
|
|
|
|
new AstVarRef(varp->fileline(),
|
|
|
|
|
varp, oldvarp->isOutput()));
|
|
|
|
|
// Skip length and width comp; we know it's a direct assignment
|
|
|
|
|
pinp->modVarp(oldvarp);
|
|
|
|
|
cellp->addPinsp(pinp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-11-08 02:05:02 +00:00
|
|
|
|
|
|
|
|
|
wrapTopPackages(netlistp, newmodp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void V3LinkLevel::wrapTopPackages(AstNetlist* netlistp, AstNodeModule* newmodp) {
|
|
|
|
|
// Instantiate all packages under the top wrapper
|
|
|
|
|
// This way all later SCOPE based optimizations can ignore packages
|
|
|
|
|
for (AstNodeModule* modp = netlistp->modulesp(); modp; modp=modp->nextp()->castNodeModule()) {
|
|
|
|
|
if (modp->castPackage()) {
|
|
|
|
|
AstCell* cellp = new AstCell(modp->fileline(),
|
|
|
|
|
// Could add __03a__03a="::" to prevent conflict
|
|
|
|
|
// with module names/"v"
|
|
|
|
|
modp->name(),
|
|
|
|
|
modp->name(),
|
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
|
cellp->modp(modp);
|
|
|
|
|
newmodp->addStmtp(cellp);
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|