2008-05-31 16:31:48 +02:00
|
|
|
/*
|
|
|
|
|
* VHDL code generation for processes.
|
|
|
|
|
*
|
|
|
|
|
* 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>
|
2008-06-02 17:17:01 +02:00
|
|
|
#include <cassert>
|
|
|
|
|
#include <sstream>
|
2008-06-18 14:49:03 +02:00
|
|
|
#include <map>
|
2008-05-31 16:31:48 +02:00
|
|
|
|
2008-06-18 13:51:11 +02:00
|
|
|
/*
|
2008-06-18 15:04:16 +02:00
|
|
|
* Implementing blocking assignment is a little tricky since
|
|
|
|
|
* the semantics are a little different to VHDL:
|
|
|
|
|
*
|
|
|
|
|
* In Verilog a blocking assignment (=) can be used anywhere
|
|
|
|
|
* non-blocking assignment (<=) can be. In VHDL blocking
|
|
|
|
|
* assignment (:=) can only be used with variables, and
|
|
|
|
|
* non-blocking assignment (<=) can only be used with signals.
|
|
|
|
|
* All Verilog variables are translated into signals in the
|
|
|
|
|
* VHDL architecture. This means we cannot use the VHDL :=
|
|
|
|
|
* operator directly. Furthermore, VHDL variables can only
|
|
|
|
|
* be declared within processes, so it wouldn't help to
|
|
|
|
|
* make all Verilog variables VHDL variables.
|
|
|
|
|
*
|
|
|
|
|
* The solution is to generate a VHDL variable in a process
|
|
|
|
|
* whenever a blocking assignment is made to a signal. The
|
|
|
|
|
* assignment is made to this variable instead, and
|
|
|
|
|
* g_assign_vars below remembers the temporary variables
|
|
|
|
|
* that have been generated. Any subsequent blocking assignments
|
|
|
|
|
* are made to the same variable. At either the end of the
|
|
|
|
|
* process or a `wait' statement, the temporaries are assigned
|
|
|
|
|
* back to the signals, and the temporaries are forgotten. This
|
|
|
|
|
* has exactly the same (external) behaviour as the Verilog
|
|
|
|
|
* blocking assignment, since no external process will be able
|
|
|
|
|
* to observe that the assignment wasn't made immediately.
|
|
|
|
|
*
|
|
|
|
|
* For example:
|
|
|
|
|
*
|
|
|
|
|
* initial begin
|
|
|
|
|
* a = 5;
|
|
|
|
|
* b = a + 3;
|
|
|
|
|
* end
|
|
|
|
|
*
|
|
|
|
|
* Is translated to:
|
|
|
|
|
*
|
|
|
|
|
* process is
|
|
|
|
|
* variable a_Var : Some_Type;
|
|
|
|
|
* variable b_Var : Some_Type;
|
|
|
|
|
* begin
|
|
|
|
|
* a_Var := 5;
|
|
|
|
|
* b_Var := a_Var + 3;
|
|
|
|
|
* a <= a_Var;
|
|
|
|
|
* b <= b_Var;
|
|
|
|
|
* end process;
|
2008-06-18 13:51:11 +02:00
|
|
|
*/
|
2008-06-18 14:49:03 +02:00
|
|
|
typedef std::map<std::string, ivl_signal_t> var_temp_set_t;
|
|
|
|
|
static var_temp_set_t g_assign_vars;
|
2008-06-18 13:51:11 +02:00
|
|
|
|
2008-06-18 15:04:16 +02:00
|
|
|
/*
|
|
|
|
|
* Called whenever a blocking assignment is made to sig.
|
|
|
|
|
*/
|
2008-06-18 14:30:19 +02:00
|
|
|
void blocking_assign_to(vhdl_process *proc, ivl_signal_t sig)
|
2008-06-18 13:51:11 +02:00
|
|
|
{
|
2008-06-18 14:30:19 +02:00
|
|
|
std::string var(get_renamed_signal(sig));
|
2008-06-18 15:04:16 +02:00
|
|
|
std::string tmpname(var + "_Var");
|
|
|
|
|
|
2008-06-18 14:30:19 +02:00
|
|
|
if (g_assign_vars.find(var) == g_assign_vars.end()) {
|
|
|
|
|
// This is the first time a non-blocking assignment
|
|
|
|
|
// has been made to this signal: create a variable
|
|
|
|
|
// to shadow it.
|
2008-06-18 15:04:16 +02:00
|
|
|
if (!proc->have_declared_var(tmpname)) {
|
|
|
|
|
vhdl_decl *decl = proc->get_parent()->get_decl(var);
|
|
|
|
|
assert(decl);
|
|
|
|
|
vhdl_type *type = new vhdl_type(*decl->get_type());
|
|
|
|
|
|
|
|
|
|
proc->add_decl(new vhdl_var_decl(tmpname.c_str(), type));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rename_signal(sig, tmpname);
|
|
|
|
|
g_assign_vars[tmpname] = sig;
|
2008-06-18 14:30:19 +02:00
|
|
|
}
|
2008-06-18 13:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
2008-06-18 14:49:03 +02:00
|
|
|
/*
|
|
|
|
|
* Assign all _Var variables to the corresponding signals. This makes
|
|
|
|
|
* the new values visible outside the current process. This should be
|
|
|
|
|
* called before any `wait' statement or the end of the process.
|
|
|
|
|
*/
|
|
|
|
|
void draw_blocking_assigns(vhdl_process *proc)
|
|
|
|
|
{
|
|
|
|
|
var_temp_set_t::const_iterator it;
|
|
|
|
|
for (it = g_assign_vars.begin(); it != g_assign_vars.end(); ++it) {
|
|
|
|
|
std::string stripped(strip_var((*it).first));
|
|
|
|
|
|
|
|
|
|
vhdl_decl *decl = proc->get_decl(stripped);
|
|
|
|
|
assert(decl);
|
|
|
|
|
vhdl_type *type = new vhdl_type(*decl->get_type());
|
|
|
|
|
|
|
|
|
|
vhdl_var_ref *lhs = new vhdl_var_ref(stripped.c_str(), NULL);
|
|
|
|
|
vhdl_expr *rhs = new vhdl_var_ref((*it).first.c_str(), type);
|
|
|
|
|
|
|
|
|
|
// TODO: I'm not sure this will work properly if, e.g., the delay
|
|
|
|
|
// is inside a `if' statement
|
|
|
|
|
proc->get_container()->add_stmt(new vhdl_nbassign_stmt(lhs, rhs));
|
|
|
|
|
|
|
|
|
|
// Undo the renaming (since the temporary is no longer needed)
|
|
|
|
|
rename_signal((*it).second, stripped);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_assign_vars.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-18 14:30:19 +02:00
|
|
|
/*
|
|
|
|
|
* Remove _Var from the end of a string, if it is present.
|
|
|
|
|
*/
|
|
|
|
|
std::string strip_var(const std::string &str)
|
|
|
|
|
{
|
|
|
|
|
std::string result(str);
|
|
|
|
|
size_t pos = result.find("_Var");
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
|
result.erase(pos, 4);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2008-06-18 13:51:11 +02:00
|
|
|
|
2008-06-02 17:17:01 +02:00
|
|
|
/*
|
|
|
|
|
* Convert a Verilog process to VHDL and add it to the architecture
|
|
|
|
|
* of the given entity.
|
|
|
|
|
*/
|
|
|
|
|
static int generate_vhdl_process(vhdl_entity *ent, ivl_process_t proc)
|
2008-05-31 16:31:48 +02:00
|
|
|
{
|
2008-06-04 14:52:56 +02:00
|
|
|
// Create a new process and store it in the entity's
|
|
|
|
|
// architecture. This needs to be done first or the
|
|
|
|
|
// parent link won't be valid (and draw_stmt needs this
|
|
|
|
|
// to add information to the architecture)
|
2008-06-02 17:17:01 +02:00
|
|
|
vhdl_process *vhdl_proc = new vhdl_process();
|
2008-06-13 14:59:48 +02:00
|
|
|
ent->get_arch()->add_stmt(vhdl_proc);
|
|
|
|
|
|
|
|
|
|
// If this is an initial process, push signal initialisation
|
|
|
|
|
// into the declarations
|
|
|
|
|
if (ivl_process_type(proc) == IVL_PR_INITIAL)
|
|
|
|
|
vhdl_proc->set_initial(true);
|
2008-06-04 14:52:56 +02:00
|
|
|
|
2008-06-03 19:26:36 +02:00
|
|
|
ivl_statement_t stmt = ivl_process_stmt(proc);
|
2008-06-11 15:11:37 +02:00
|
|
|
int rc = draw_stmt(vhdl_proc, vhdl_proc->get_container(), stmt);
|
2008-06-03 19:26:36 +02:00
|
|
|
if (rc != 0)
|
|
|
|
|
return rc;
|
|
|
|
|
|
2008-06-03 18:39:24 +02:00
|
|
|
// Initial processes are translated to VHDL processes with
|
|
|
|
|
// no sensitivity list and and indefinite wait statement at
|
|
|
|
|
// the end
|
2008-06-13 15:17:24 +02:00
|
|
|
// However, if no statements were added to the container
|
|
|
|
|
// by draw_stmt, don't bother adding a wait as `emit'
|
|
|
|
|
// will optimise the process out of the output
|
|
|
|
|
if (ivl_process_type(proc) == IVL_PR_INITIAL
|
|
|
|
|
&& !vhdl_proc->get_container()->empty()) {
|
2008-06-03 18:39:24 +02:00
|
|
|
vhdl_wait_stmt *wait = new vhdl_wait_stmt();
|
2008-06-11 15:11:37 +02:00
|
|
|
vhdl_proc->get_container()->add_stmt(wait);
|
2008-06-03 18:39:24 +02:00
|
|
|
}
|
|
|
|
|
|
2008-06-02 17:17:01 +02:00
|
|
|
// Add a comment indicating where it came from
|
2008-05-31 16:31:48 +02:00
|
|
|
ivl_scope_t scope = ivl_process_scope(proc);
|
2008-06-02 17:17:01 +02:00
|
|
|
const char *type = ivl_process_type(proc) == IVL_PR_INITIAL
|
|
|
|
|
? "initial" : "always";
|
|
|
|
|
std::ostringstream ss;
|
2008-06-02 21:24:25 +02:00
|
|
|
ss << "Generated from " << type << " process in ";
|
|
|
|
|
ss << ivl_scope_tname(scope);
|
2008-06-02 17:17:01 +02:00
|
|
|
vhdl_proc->set_comment(ss.str());
|
2008-05-31 16:31:48 +02:00
|
|
|
|
2008-06-18 14:49:03 +02:00
|
|
|
// Output any remaning blocking assignments
|
|
|
|
|
draw_blocking_assigns(vhdl_proc);
|
|
|
|
|
|
2008-05-31 16:31:48 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2008-06-02 17:17:01 +02:00
|
|
|
|
|
|
|
|
int draw_process(ivl_process_t proc, void *cd)
|
|
|
|
|
{
|
|
|
|
|
ivl_scope_t scope = ivl_process_scope(proc);
|
|
|
|
|
const char *scope_name = ivl_scope_name(scope);
|
|
|
|
|
|
|
|
|
|
// A process should occur in a module scope, therefore it
|
|
|
|
|
// should have already been assigned a VHDL entity
|
|
|
|
|
assert(ivl_scope_type(scope) == IVL_SCT_MODULE);
|
|
|
|
|
vhdl_entity *ent = find_entity(ivl_scope_tname(scope));
|
|
|
|
|
assert(ent != NULL);
|
2008-06-09 13:40:59 +02:00
|
|
|
|
2008-06-02 17:17:01 +02:00
|
|
|
// If the scope this process belongs to is the same as the
|
|
|
|
|
// VHDL entity was generated from, then create a VHDL process
|
|
|
|
|
// from this Verilog process. This ensures that each process
|
|
|
|
|
// is translated at most once, no matter how many times it
|
|
|
|
|
// appears in the hierarchy.
|
2008-06-04 22:07:50 +02:00
|
|
|
if (ent->get_derived_from() == scope_name)
|
2008-06-02 17:17:01 +02:00
|
|
|
return generate_vhdl_process(ent, proc);
|
2008-06-04 22:07:50 +02:00
|
|
|
else
|
2008-06-02 17:17:01 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|