2008-05-28 18:17:39 +02:00
|
|
|
/*
|
|
|
|
|
* VHDL code generator for Icarus Verilog.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2008 Nick Gasson (nick@nickg.me.uk)
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program 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.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-11-19 03:28:45 +01:00
|
|
|
#include "version.h"
|
2008-05-28 18:17:39 +02:00
|
|
|
#include "vhdl_target.h"
|
|
|
|
|
#include "vhdl_element.hh"
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
2008-05-29 17:24:16 +02:00
|
|
|
#include <cstdarg>
|
|
|
|
|
#include <cstdio>
|
2008-05-31 16:28:25 +02:00
|
|
|
#include <cassert>
|
|
|
|
|
#include <cstring>
|
2008-12-14 14:10:43 +01:00
|
|
|
#include <cstdlib>
|
2008-06-03 20:14:47 +02:00
|
|
|
#include <list>
|
2008-06-12 21:26:23 +02:00
|
|
|
#include <map>
|
2008-07-29 20:33:40 +02:00
|
|
|
#include <set>
|
2008-05-29 17:24:16 +02:00
|
|
|
|
2008-11-19 03:28:45 +01:00
|
|
|
static const char*version_string =
|
|
|
|
|
"Icarus Verilog VHDL Code Generator " VERSION " (" VERSION_TAG ")\n\n"
|
|
|
|
|
"Copyright (C) 2008 Nick Gasson (nick@nickg.me.uk)\n\n"
|
|
|
|
|
" This program is free software; you can redistribute it and/or modify\n"
|
|
|
|
|
" it under the terms of the GNU General Public License as published by\n"
|
|
|
|
|
" the Free Software Foundation; either version 2 of the License, or\n"
|
|
|
|
|
" (at your option) any later version.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" This program is distributed in the hope that it will be useful,\n"
|
|
|
|
|
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
|
|
|
|
|
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
|
|
|
|
|
" GNU General Public License for more details.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" You should have received a copy of the GNU General Public License along\n"
|
|
|
|
|
" with this program; if not, write to the Free Software Foundation, Inc.,\n"
|
|
|
|
|
" 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
|
|
|
|
|
;
|
|
|
|
|
|
2008-06-12 21:26:23 +02:00
|
|
|
/*
|
2008-07-21 16:20:40 +02:00
|
|
|
* Maps a signal to the scope it is defined within. Also
|
2008-06-12 21:26:23 +02:00
|
|
|
* provides a mechanism for renaming signals -- i.e. when
|
|
|
|
|
* an output has the same name as register: valid in Verilog
|
|
|
|
|
* but not in VHDL, so two separate signals need to be
|
|
|
|
|
* defined.
|
|
|
|
|
*/
|
|
|
|
|
struct signal_defn_t {
|
2008-06-25 19:12:57 +02:00
|
|
|
std::string renamed; // The name of the VHDL signal
|
2008-07-29 13:00:26 +02:00
|
|
|
vhdl_scope *scope; // The scope where it is defined
|
2008-06-12 21:26:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef std::map<ivl_signal_t, signal_defn_t> signal_defn_map_t;
|
2008-05-29 17:24:16 +02:00
|
|
|
|
2008-06-12 21:26:23 +02:00
|
|
|
|
|
|
|
|
static int g_errors = 0; // Total number of errors encountered
|
2008-05-31 16:28:25 +02:00
|
|
|
static entity_list_t g_entities; // All entities to emit
|
2008-06-12 21:26:23 +02:00
|
|
|
static signal_defn_map_t g_known_signals;
|
2008-06-17 21:16:16 +02:00
|
|
|
static ivl_design_t g_design;
|
2008-05-31 16:28:25 +02:00
|
|
|
|
2008-06-03 20:14:47 +02:00
|
|
|
|
2008-05-29 17:24:16 +02:00
|
|
|
/*
|
|
|
|
|
* Called when an unrecoverable problem is encountered.
|
|
|
|
|
*/
|
|
|
|
|
void error(const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
std::va_list args;
|
|
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
std::printf("VHDL conversion error: "); // Source/line number?
|
|
|
|
|
std::vprintf(fmt, args);
|
|
|
|
|
std::putchar('\n');
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
g_errors++;
|
|
|
|
|
}
|
2008-05-28 18:17:39 +02:00
|
|
|
|
2008-11-29 23:16:46 +01:00
|
|
|
/*
|
|
|
|
|
* Print a message only if -pdebug was specified.
|
|
|
|
|
*/
|
|
|
|
|
void debug_msg(const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
std::va_list args;
|
|
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
|
|
if (std::strcmp(ivl_design_flag(g_design, "debug"), "")) {
|
|
|
|
|
std::fputs("[DEBUG] ", stdout);
|
|
|
|
|
std::vprintf(fmt, args);
|
|
|
|
|
std::putchar('\n');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-31 16:28:25 +02:00
|
|
|
/*
|
2008-07-29 20:33:40 +02:00
|
|
|
* Find an entity given a scope name.
|
2008-05-31 16:28:25 +02:00
|
|
|
*/
|
2008-07-29 20:33:40 +02:00
|
|
|
vhdl_entity *find_entity(const std::string &sname)
|
2008-05-30 02:04:47 +02:00
|
|
|
{
|
2008-05-31 16:28:25 +02:00
|
|
|
entity_list_t::const_iterator it;
|
|
|
|
|
for (it = g_entities.begin(); it != g_entities.end(); ++it) {
|
2008-07-29 20:33:40 +02:00
|
|
|
if ((*it)->get_derived_from() == sname)
|
2008-05-31 16:28:25 +02:00
|
|
|
return *it;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2008-05-30 02:04:47 +02:00
|
|
|
|
2008-05-31 16:28:25 +02:00
|
|
|
/*
|
|
|
|
|
* Add an entity/architecture pair to the list of entities
|
|
|
|
|
* to emit.
|
|
|
|
|
*/
|
|
|
|
|
void remember_entity(vhdl_entity* ent)
|
|
|
|
|
{
|
2008-07-29 20:33:40 +02:00
|
|
|
assert(find_entity(ent->get_derived_from()) == NULL);
|
2008-05-31 16:28:25 +02:00
|
|
|
g_entities.push_back(ent);
|
2008-05-30 02:04:47 +02:00
|
|
|
}
|
|
|
|
|
|
2008-06-30 18:47:45 +02:00
|
|
|
bool seen_signal_before(ivl_signal_t sig)
|
|
|
|
|
{
|
|
|
|
|
return g_known_signals.find(sig) != g_known_signals.end();
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-12 21:26:23 +02:00
|
|
|
/*
|
2008-09-09 18:30:47 +02:00
|
|
|
* Remember the association of signal to entity.
|
2008-06-12 21:26:23 +02:00
|
|
|
*/
|
2008-07-29 13:00:26 +02:00
|
|
|
void remember_signal(ivl_signal_t sig, vhdl_scope *scope)
|
2008-06-12 21:26:23 +02:00
|
|
|
{
|
2008-06-30 18:47:45 +02:00
|
|
|
assert(!seen_signal_before(sig));
|
2008-07-07 20:27:52 +02:00
|
|
|
|
2008-06-25 19:12:57 +02:00
|
|
|
signal_defn_t defn = { ivl_signal_basename(sig), scope };
|
2008-06-12 21:26:23 +02:00
|
|
|
g_known_signals[sig] = defn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Change the VHDL name of a Verilog signal.
|
|
|
|
|
*/
|
|
|
|
|
void rename_signal(ivl_signal_t sig, const std::string &renamed)
|
|
|
|
|
{
|
2008-06-30 18:47:45 +02:00
|
|
|
assert(seen_signal_before(sig));
|
2008-06-12 21:26:23 +02:00
|
|
|
|
|
|
|
|
g_known_signals[sig].renamed = renamed;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-29 13:00:26 +02:00
|
|
|
vhdl_scope *find_scope_for_signal(ivl_signal_t sig)
|
2008-06-12 21:26:23 +02:00
|
|
|
{
|
2009-01-17 13:00:32 +01:00
|
|
|
if (seen_signal_before(sig))
|
|
|
|
|
return g_known_signals[sig].scope;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
2008-06-12 21:26:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string &get_renamed_signal(ivl_signal_t sig)
|
|
|
|
|
{
|
2008-06-30 18:47:45 +02:00
|
|
|
assert(seen_signal_before(sig));
|
2008-06-12 21:26:23 +02:00
|
|
|
|
|
|
|
|
return g_known_signals[sig].renamed;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-13 13:41:02 +02:00
|
|
|
ivl_signal_t find_signal_named(const std::string &name, const vhdl_scope *scope)
|
|
|
|
|
{
|
|
|
|
|
signal_defn_map_t::const_iterator it;
|
|
|
|
|
for (it = g_known_signals.begin(); it != g_known_signals.end(); ++it) {
|
2008-07-14 20:13:11 +02:00
|
|
|
if (((*it).second.scope == scope
|
|
|
|
|
|| (*it).second.scope == scope->get_parent())
|
|
|
|
|
&& (*it).second.renamed == name)
|
2008-07-13 13:41:02 +02:00
|
|
|
return (*it).first;
|
|
|
|
|
}
|
|
|
|
|
assert(false);
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-17 21:16:16 +02:00
|
|
|
ivl_design_t get_vhdl_design()
|
|
|
|
|
{
|
|
|
|
|
return g_design;
|
|
|
|
|
}
|
2008-06-12 21:26:23 +02:00
|
|
|
|
2008-05-28 18:17:39 +02:00
|
|
|
extern "C" int target_design(ivl_design_t des)
|
|
|
|
|
{
|
|
|
|
|
ivl_scope_t *roots;
|
|
|
|
|
unsigned int nroots;
|
|
|
|
|
ivl_design_roots(des, &roots, &nroots);
|
|
|
|
|
|
2008-06-17 21:16:16 +02:00
|
|
|
g_design = des;
|
|
|
|
|
|
2008-05-31 16:28:25 +02:00
|
|
|
for (unsigned int i = 0; i < nroots; i++)
|
|
|
|
|
draw_scope(roots[i], NULL);
|
|
|
|
|
|
2008-09-06 11:56:52 +02:00
|
|
|
// Only generate processes if there were no errors generating entities
|
|
|
|
|
// (otherwise the necessary information won't be present)
|
|
|
|
|
if (0 == g_errors)
|
|
|
|
|
ivl_design_process(des, draw_process, NULL);
|
2008-05-31 16:28:25 +02:00
|
|
|
|
|
|
|
|
// Write the generated elements to the output file
|
2008-09-06 11:56:52 +02:00
|
|
|
// only if there were no errors generating entities or processes
|
2008-06-04 22:03:36 +02:00
|
|
|
if (0 == g_errors) {
|
|
|
|
|
const char *ofname = ivl_design_flag(des, "-o");
|
2008-07-29 20:33:40 +02:00
|
|
|
ofstream outfile(ofname);
|
2008-11-20 01:04:39 +01:00
|
|
|
outfile << "-- This VHDL was converted from Verilog using the" << endl
|
|
|
|
|
<< "-- Icarus Verilog VHDL Code Generator " VERSION
|
|
|
|
|
" (" VERSION_TAG ")" << endl << endl;
|
2008-07-29 20:33:40 +02:00
|
|
|
|
2008-12-14 14:10:43 +01:00
|
|
|
// If the user passed -pdepth=N then only emit entities with
|
|
|
|
|
// depth < N
|
|
|
|
|
// I.e. -pdepth=1 emits only the top-level entity
|
|
|
|
|
// If max_depth is zero then all entities will be emitted
|
|
|
|
|
// (This is handy since it means we can use atoi ;-)
|
|
|
|
|
int max_depth = std::atoi(ivl_design_flag(des, "depth"));
|
|
|
|
|
|
2008-07-29 20:33:40 +02:00
|
|
|
// Make sure we only emit one example of each type of entity
|
|
|
|
|
set<string> seen_entities;
|
2008-06-04 22:03:36 +02:00
|
|
|
|
|
|
|
|
for (entity_list_t::iterator it = g_entities.begin();
|
|
|
|
|
it != g_entities.end();
|
2008-07-29 20:33:40 +02:00
|
|
|
++it) {
|
2008-12-14 14:10:43 +01:00
|
|
|
if (seen_entities.find((*it)->get_name()) == seen_entities.end()
|
|
|
|
|
&& (max_depth == 0 || (*it)->depth < max_depth)) {
|
2008-07-29 20:33:40 +02:00
|
|
|
(*it)->emit(outfile);
|
|
|
|
|
seen_entities.insert((*it)->get_name());
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-06-04 22:03:36 +02:00
|
|
|
|
|
|
|
|
outfile.close();
|
|
|
|
|
}
|
2008-05-28 18:17:39 +02:00
|
|
|
|
2008-05-31 17:08:57 +02:00
|
|
|
// Clean up
|
2008-06-03 20:14:47 +02:00
|
|
|
for (entity_list_t::iterator it = g_entities.begin();
|
|
|
|
|
it != g_entities.end();
|
|
|
|
|
++it)
|
2008-05-31 17:08:57 +02:00
|
|
|
delete (*it);
|
|
|
|
|
g_entities.clear();
|
|
|
|
|
|
2008-05-29 17:24:16 +02:00
|
|
|
return g_errors;
|
2008-05-28 18:17:39 +02:00
|
|
|
}
|
2008-11-19 03:28:45 +01:00
|
|
|
|
|
|
|
|
extern "C" const char* target_query(const char*key)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp(key, "version") == 0) return version_string;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|