iverilog/tgt-vhdl/vhdl.cc

110 lines
2.7 KiB
C++
Raw Normal View History

/*
* 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.
*/
#include "vhdl_target.h"
#include "vhdl_element.hh"
#include <iostream>
#include <fstream>
2008-05-29 17:24:16 +02:00
#include <cstdarg>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <list>
2008-05-29 17:24:16 +02:00
static int g_errors = 0; // Total number of errors encountered
static entity_list_t g_entities; // All entities to emit
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++;
}
/*
* Find an entity given a type name.
*/
vhdl_entity *find_entity(const std::string &tname)
2008-05-30 02:04:47 +02:00
{
entity_list_t::const_iterator it;
for (it = g_entities.begin(); it != g_entities.end(); ++it) {
if ((*it)->get_name() == tname)
return *it;
}
return NULL;
}
2008-05-30 02:04:47 +02:00
/*
* Add an entity/architecture pair to the list of entities
* to emit.
*/
void remember_entity(vhdl_entity* ent)
{
assert(find_entity(ent->get_name()) == NULL);
g_entities.push_back(ent);
2008-05-30 02:04:47 +02:00
}
extern "C" int target_design(ivl_design_t des)
{
ivl_scope_t *roots;
unsigned int nroots;
ivl_design_roots(des, &roots, &nroots);
for (unsigned int i = 0; i < nroots; i++)
draw_scope(roots[i], NULL);
ivl_design_process(des, draw_process, NULL);
// Write the generated elements to the output file
// only if there are no errors
if (0 == g_errors) {
const char *ofname = ivl_design_flag(des, "-o");
std::ofstream outfile(ofname);
for (entity_list_t::iterator it = g_entities.begin();
it != g_entities.end();
++it)
(*it)->emit(outfile);
outfile.close();
}
2008-05-31 17:08:57 +02:00
// Clean up
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;
}