2008-06-03 19:26:36 +02:00
|
|
|
/*
|
|
|
|
|
* VHDL code generation for statements.
|
|
|
|
|
*
|
|
|
|
|
* 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 <iostream>
|
2008-06-03 19:44:17 +02:00
|
|
|
#include <cstring>
|
2008-06-04 21:57:15 +02:00
|
|
|
#include <cassert>
|
2008-06-07 15:21:50 +02:00
|
|
|
#include <sstream>
|
2008-06-24 11:55:45 +02:00
|
|
|
#include <typeinfo>
|
2008-06-03 19:44:17 +02:00
|
|
|
|
2008-06-11 14:37:21 +02:00
|
|
|
/*
|
|
|
|
|
* VHDL has no real equivalent of Verilog's $finish task. The
|
|
|
|
|
* current solution is to use `assert false ...' to terminate
|
|
|
|
|
* the simulator. This isn't great, as the simulator will
|
|
|
|
|
* return a failure exit code when in fact it completed
|
|
|
|
|
* successfully.
|
2008-06-17 21:16:16 +02:00
|
|
|
*
|
|
|
|
|
* An alternative is to use the VHPI interface supported by
|
|
|
|
|
* some VHDL simulators and implement the $finish funcitonality
|
|
|
|
|
* in C. This function can be enabled with the flag
|
|
|
|
|
* -puse-vhpi-finish=1.
|
2008-06-11 14:37:21 +02:00
|
|
|
*/
|
2008-06-24 21:01:06 +02:00
|
|
|
static int draw_stask_finish(vhdl_procedural *proc, stmt_container *container,
|
2008-06-11 15:11:37 +02:00
|
|
|
ivl_statement_t stmt)
|
2008-06-11 14:37:21 +02:00
|
|
|
{
|
2008-06-17 21:16:16 +02:00
|
|
|
const char *use_vhpi = ivl_design_flag(get_vhdl_design(), "use-vhpi-finish");
|
|
|
|
|
if (strcmp(use_vhpi, "1") == 0) {
|
2008-06-24 19:52:25 +02:00
|
|
|
//get_active_entity()->requires_package("work.Verilog_Support");
|
2008-06-17 21:16:16 +02:00
|
|
|
container->add_stmt(new vhdl_pcall_stmt("work.Verilog_Support.Finish"));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
container->add_stmt(new vhdl_assert_stmt("SIMULATION FINISHED"));
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-11 14:37:21 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-03 19:44:17 +02:00
|
|
|
/*
|
|
|
|
|
* Generate VHDL for system tasks (like $display). Not all of
|
|
|
|
|
* these are supported.
|
|
|
|
|
*/
|
2008-06-24 21:01:06 +02:00
|
|
|
static int draw_stask(vhdl_procedural *proc, stmt_container *container,
|
2008-06-11 15:11:37 +02:00
|
|
|
ivl_statement_t stmt)
|
2008-06-03 19:44:17 +02:00
|
|
|
{
|
|
|
|
|
const char *name = ivl_stmt_name(stmt);
|
|
|
|
|
|
|
|
|
|
if (strcmp(name, "$display") == 0)
|
2008-06-20 20:00:07 +02:00
|
|
|
return draw_stask_display(proc, container, stmt, true);
|
|
|
|
|
else if (strcmp(name, "$write") == 0)
|
|
|
|
|
return draw_stask_display(proc, container, stmt, false);
|
2008-06-11 14:37:21 +02:00
|
|
|
else if (strcmp(name, "$finish") == 0)
|
2008-06-11 15:11:37 +02:00
|
|
|
return draw_stask_finish(proc, container, stmt);
|
2008-06-03 19:44:17 +02:00
|
|
|
else {
|
2008-09-06 13:06:01 +02:00
|
|
|
vhdl_seq_stmt *result = new vhdl_null_stmt();
|
|
|
|
|
ostringstream ss;
|
|
|
|
|
ss << "Unsupported system task " << name << " omitted here ("
|
|
|
|
|
<< ivl_stmt_file(stmt) << ":" << ivl_stmt_lineno(stmt) << ")";
|
|
|
|
|
result->set_comment(ss.str());
|
|
|
|
|
container->add_stmt(result);
|
|
|
|
|
cerr << "Warning: no VHDL translation for system task " << name << endl;
|
2008-06-03 19:44:17 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-06-03 19:26:36 +02:00
|
|
|
|
2008-06-04 21:57:15 +02:00
|
|
|
/*
|
|
|
|
|
* Generate VHDL for a block of Verilog statements. This doesn't
|
|
|
|
|
* actually do anything, other than recursively translate the
|
|
|
|
|
* block's statements and add them to the process. This is OK as
|
2008-06-11 15:11:37 +02:00
|
|
|
* the stmt_container class behaves like a Verilog block.
|
2008-06-04 21:57:15 +02:00
|
|
|
*/
|
2008-06-24 21:01:06 +02:00
|
|
|
static int draw_block(vhdl_procedural *proc, stmt_container *container,
|
2008-08-05 12:02:36 +02:00
|
|
|
ivl_statement_t stmt, bool is_last)
|
2008-06-04 21:57:15 +02:00
|
|
|
{
|
|
|
|
|
int count = ivl_stmt_block_count(stmt);
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
2008-08-05 12:02:36 +02:00
|
|
|
ivl_statement_t stmt_i = ivl_stmt_block_stmt(stmt, i);
|
|
|
|
|
if (draw_stmt(proc, container, stmt_i, is_last && i == count - 1) != 0)
|
2008-06-04 21:57:15 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-05 14:16:35 +02:00
|
|
|
/*
|
2008-08-02 19:20:18 +02:00
|
|
|
* A no-op statement. This corresponds to a `null' statement in VHDL.
|
2008-06-05 14:16:35 +02:00
|
|
|
*/
|
2008-06-24 21:01:06 +02:00
|
|
|
static int draw_noop(vhdl_procedural *proc, stmt_container *container,
|
2008-06-11 15:11:37 +02:00
|
|
|
ivl_statement_t stmt)
|
2008-06-05 14:16:35 +02:00
|
|
|
{
|
2008-06-11 15:11:37 +02:00
|
|
|
container->add_stmt(new vhdl_null_stmt());
|
2008-06-05 14:16:35 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-03 16:47:32 +02:00
|
|
|
static vhdl_var_ref *make_assign_lhs(ivl_lval_t lval, vhdl_scope *scope)
|
2008-07-14 22:04:09 +02:00
|
|
|
{
|
2008-08-03 16:47:32 +02:00
|
|
|
ivl_signal_t sig = ivl_lval_sig(lval);
|
|
|
|
|
if (!sig) {
|
|
|
|
|
error("Only signals as lvals supported at the moment");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vhdl_expr *base = NULL;
|
|
|
|
|
ivl_expr_t e_off = ivl_lval_part_off(lval);
|
|
|
|
|
if (NULL == e_off)
|
|
|
|
|
e_off = ivl_lval_idx(lval);
|
|
|
|
|
if (e_off) {
|
|
|
|
|
if ((base = translate_expr(e_off)) == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
vhdl_type integer(VHDL_TYPE_INTEGER);
|
|
|
|
|
base = base->cast(&integer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned lval_width = ivl_lval_width(lval);
|
|
|
|
|
|
2008-07-14 22:34:48 +02:00
|
|
|
string signame(get_renamed_signal(sig));
|
2008-07-14 22:27:21 +02:00
|
|
|
vhdl_decl *decl = scope->get_decl(signame);
|
2008-08-12 10:47:03 +02:00
|
|
|
assert(decl);
|
2008-07-14 22:04:09 +02:00
|
|
|
|
2008-07-14 22:27:21 +02:00
|
|
|
vhdl_type *ltype = new vhdl_type(*decl->get_type());
|
|
|
|
|
vhdl_var_ref *lval_ref = new vhdl_var_ref(signame.c_str(), ltype);
|
2008-07-17 14:41:44 +02:00
|
|
|
if (base) {
|
|
|
|
|
if (decl->get_type()->get_name() == VHDL_TYPE_ARRAY)
|
|
|
|
|
lval_ref->set_slice(base, 0);
|
|
|
|
|
else
|
|
|
|
|
lval_ref->set_slice(base, lval_width - 1);
|
|
|
|
|
}
|
2008-07-07 22:19:59 +02:00
|
|
|
|
2008-07-14 22:27:21 +02:00
|
|
|
return lval_ref;
|
2008-07-04 22:55:51 +02:00
|
|
|
}
|
|
|
|
|
|
2008-08-02 19:20:18 +02:00
|
|
|
static bool assignment_lvals(ivl_statement_t stmt, vhdl_procedural *proc,
|
|
|
|
|
list<vhdl_var_ref*> &lvals)
|
|
|
|
|
{
|
|
|
|
|
int nlvals = ivl_stmt_lvals(stmt);
|
|
|
|
|
for (int i = 0; i < nlvals; i++) {
|
|
|
|
|
ivl_lval_t lval = ivl_stmt_lval(stmt, i);
|
2008-08-03 16:47:32 +02:00
|
|
|
vhdl_var_ref *lhs = make_assign_lhs(lval, proc->get_scope());
|
|
|
|
|
if (NULL == lhs)
|
2008-08-02 19:20:18 +02:00
|
|
|
return false;
|
|
|
|
|
|
2008-08-03 16:47:32 +02:00
|
|
|
lvals.push_back(lhs);
|
|
|
|
|
}
|
2008-08-02 19:20:18 +02:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-04 22:55:51 +02:00
|
|
|
/*
|
|
|
|
|
* Generate an assignment of type T for the Verilog statement stmt.
|
|
|
|
|
*/
|
2008-07-04 21:07:38 +02:00
|
|
|
template <class T>
|
2008-08-02 17:38:44 +02:00
|
|
|
void make_assignment(vhdl_procedural *proc, stmt_container *container,
|
2008-08-03 16:47:32 +02:00
|
|
|
ivl_statement_t stmt, bool blocking)
|
2008-06-06 17:55:45 +02:00
|
|
|
{
|
2008-08-02 19:20:18 +02:00
|
|
|
list<vhdl_var_ref*> lvals;
|
|
|
|
|
if (!assignment_lvals(stmt, proc, lvals))
|
2008-08-02 17:38:44 +02:00
|
|
|
return;
|
2008-06-07 15:54:00 +02:00
|
|
|
|
2008-08-03 16:47:32 +02:00
|
|
|
vhdl_expr *rhs, *rhs2 = NULL;
|
|
|
|
|
ivl_expr_t rval = ivl_stmt_rval(stmt);
|
|
|
|
|
if (ivl_expr_type(rval) == IVL_EX_TERNARY) {
|
|
|
|
|
rhs = translate_expr(ivl_expr_oper2(rval));
|
|
|
|
|
rhs2 = translate_expr(ivl_expr_oper3(rval));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
rhs = translate_expr(rval);
|
|
|
|
|
if (rhs == NULL)
|
2008-08-02 17:38:44 +02:00
|
|
|
return;
|
2008-07-07 17:11:45 +02:00
|
|
|
|
2008-08-02 19:20:18 +02:00
|
|
|
if (lvals.size() == 1) {
|
|
|
|
|
vhdl_var_ref *lhs = lvals.front();
|
|
|
|
|
rhs = rhs->cast(lhs->get_type());
|
2008-08-03 16:47:32 +02:00
|
|
|
|
|
|
|
|
ivl_expr_t i_delay;
|
|
|
|
|
vhdl_expr *after = NULL;
|
|
|
|
|
if ((i_delay = ivl_stmt_delay_expr(stmt)) != NULL)
|
|
|
|
|
after = translate_time_expr(i_delay);
|
|
|
|
|
|
|
|
|
|
// A small optimisation is to expand ternary RHSs into an
|
|
|
|
|
// if statement (eliminates a function call and produces
|
|
|
|
|
// more idiomatic code)
|
|
|
|
|
if (ivl_expr_type(rval) == IVL_EX_TERNARY) {
|
|
|
|
|
rhs2 = rhs2->cast(lhs->get_type());
|
|
|
|
|
vhdl_var_ref *lhs2 =
|
|
|
|
|
make_assign_lhs(ivl_stmt_lval(stmt, 0), proc->get_scope());
|
|
|
|
|
|
|
|
|
|
vhdl_expr *test = translate_expr(ivl_expr_oper1(rval));
|
|
|
|
|
if (NULL == test)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
vhdl_if_stmt *vhdif = new vhdl_if_stmt(test);
|
|
|
|
|
|
|
|
|
|
// True part
|
|
|
|
|
{
|
|
|
|
|
T *a = new T(lhs, rhs);
|
|
|
|
|
if (after)
|
|
|
|
|
a->set_after(after);
|
|
|
|
|
vhdif->get_then_container()->add_stmt(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// False part
|
|
|
|
|
{
|
|
|
|
|
T *a = new T(lhs2, rhs2);
|
|
|
|
|
if (after)
|
|
|
|
|
a->set_after(translate_time_expr(i_delay));
|
|
|
|
|
vhdif->get_else_container()->add_stmt(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container->add_stmt(vhdif);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-08-02 19:20:18 +02:00
|
|
|
|
|
|
|
|
// Where possible, move constant assignments into the
|
|
|
|
|
// declaration as initializers. This optimisation is only
|
|
|
|
|
// performed on assignments of constant values to prevent
|
|
|
|
|
// ordering problems.
|
2008-08-02 16:46:36 +02:00
|
|
|
|
2008-08-02 19:20:18 +02:00
|
|
|
// This also has another application: If this is an `inital'
|
|
|
|
|
// process and we haven't yet generated a `wait' statement then
|
|
|
|
|
// moving the assignment to the initialization preserves the
|
|
|
|
|
// expected Verilog behaviour: VHDL does not distinguish
|
|
|
|
|
// `initial' and `always' processes so an `always' process might
|
|
|
|
|
// be activatated before an `initial' process at time 0. The
|
|
|
|
|
// `always' process may then use the uninitialized signal value.
|
|
|
|
|
// The second test ensures that we only try to initialise
|
|
|
|
|
// internal signals not ports
|
|
|
|
|
ivl_lval_t lval = ivl_stmt_lval(stmt, 0);
|
|
|
|
|
vhdl_decl *decl = proc->get_scope()->get_decl(lhs->get_name());
|
|
|
|
|
if (proc->get_scope()->initializing()
|
|
|
|
|
&& ivl_signal_port(ivl_lval_sig(lval)) == IVL_SIP_NONE
|
|
|
|
|
&& !decl->has_initial()
|
|
|
|
|
&& rhs->constant()
|
|
|
|
|
&& decl->get_type()->get_name() != VHDL_TYPE_ARRAY) {
|
2008-08-02 17:38:44 +02:00
|
|
|
|
2008-08-02 19:20:18 +02:00
|
|
|
// If this assignment is not in the top-level container
|
|
|
|
|
// it will not be made on all paths through the code
|
|
|
|
|
// This precludes any future extraction of an initialiser
|
|
|
|
|
if (container != proc->get_container())
|
|
|
|
|
decl->set_initial(NULL); // Default initial value
|
|
|
|
|
else {
|
|
|
|
|
decl->set_initial(rhs);
|
|
|
|
|
delete lhs;
|
|
|
|
|
return;
|
2008-08-02 17:38:44 +02:00
|
|
|
}
|
2008-08-02 19:20:18 +02:00
|
|
|
}
|
2008-08-20 23:54:53 +02:00
|
|
|
|
2008-08-02 19:20:18 +02:00
|
|
|
T *a = new T(lhs, rhs);
|
|
|
|
|
container->add_stmt(a);
|
2008-08-03 16:47:32 +02:00
|
|
|
|
2008-08-02 19:20:18 +02:00
|
|
|
if (after != NULL)
|
|
|
|
|
a->set_after(after);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2008-08-02 19:40:24 +02:00
|
|
|
// Multiple lvals are implemented by first assigning the complete
|
|
|
|
|
// RHS to a temporary, and then assigning each lval in turn as
|
|
|
|
|
// bit-selects of the temporary
|
|
|
|
|
|
|
|
|
|
static int tmp_count = 0;
|
|
|
|
|
ostringstream ss;
|
|
|
|
|
ss << "Verilog_Assign_Tmp_" << tmp_count++;
|
|
|
|
|
string tmpname = ss.str();
|
|
|
|
|
|
|
|
|
|
proc->get_scope()->add_decl
|
|
|
|
|
(new vhdl_var_decl(tmpname.c_str(), new vhdl_type(*rhs->get_type())));
|
|
|
|
|
|
|
|
|
|
vhdl_var_ref *tmp_ref =
|
|
|
|
|
new vhdl_var_ref(tmpname.c_str(), new vhdl_type(*rhs->get_type()));
|
|
|
|
|
container->add_stmt(new vhdl_assign_stmt(tmp_ref, rhs));
|
|
|
|
|
|
|
|
|
|
list<vhdl_var_ref*>::iterator it;
|
|
|
|
|
int width_so_far = 0;
|
|
|
|
|
for (it = lvals.begin(); it != lvals.end(); ++it) {
|
|
|
|
|
vhdl_var_ref *tmp_rhs =
|
|
|
|
|
new vhdl_var_ref(tmpname.c_str(), new vhdl_type(*rhs->get_type()));
|
|
|
|
|
|
|
|
|
|
int lval_width = (*it)->get_type()->get_width();
|
|
|
|
|
vhdl_expr *slice_base = new vhdl_const_int(width_so_far);
|
|
|
|
|
tmp_rhs->set_slice(slice_base, lval_width - 1);
|
2008-08-03 16:47:32 +02:00
|
|
|
|
|
|
|
|
ivl_expr_t i_delay;
|
|
|
|
|
vhdl_expr *after = NULL;
|
|
|
|
|
if ((i_delay = ivl_stmt_delay_expr(stmt)) != NULL)
|
|
|
|
|
after = translate_time_expr(i_delay);
|
|
|
|
|
|
|
|
|
|
T *a = new T(*it, tmp_rhs);
|
|
|
|
|
if (after)
|
|
|
|
|
a->set_after(after);
|
2008-08-02 19:40:24 +02:00
|
|
|
|
2008-08-03 16:47:32 +02:00
|
|
|
container->add_stmt(a);
|
2008-08-02 19:40:24 +02:00
|
|
|
|
|
|
|
|
width_so_far += lval_width;
|
|
|
|
|
}
|
2008-06-18 14:06:27 +02:00
|
|
|
}
|
2008-07-04 21:07:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A non-blocking assignment inside a process. The semantics for
|
|
|
|
|
* this are essentially the same as VHDL's non-blocking signal
|
|
|
|
|
* assignment.
|
|
|
|
|
*/
|
|
|
|
|
static int draw_nbassign(vhdl_procedural *proc, stmt_container *container,
|
2008-08-03 16:47:32 +02:00
|
|
|
ivl_statement_t stmt)
|
2008-07-04 21:07:38 +02:00
|
|
|
{
|
|
|
|
|
assert(proc->get_scope()->allow_signal_assignment());
|
|
|
|
|
|
2008-08-03 16:47:32 +02:00
|
|
|
make_assignment<vhdl_nbassign_stmt>(proc, container, stmt, false);
|
2008-07-14 21:29:49 +02:00
|
|
|
|
2008-07-04 21:07:38 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int draw_assign(vhdl_procedural *proc, stmt_container *container,
|
2008-08-05 12:02:36 +02:00
|
|
|
ivl_statement_t stmt, bool is_last)
|
2008-07-04 21:07:38 +02:00
|
|
|
{
|
2008-07-14 20:54:45 +02:00
|
|
|
if (proc->get_scope()->allow_signal_assignment()) {
|
2008-07-15 15:09:24 +02:00
|
|
|
// Blocking assignment is implemented as non-blocking assignment
|
|
|
|
|
// followed by a zero-time wait
|
|
|
|
|
// This follows the Verilog semantics fairly closely.
|
2008-07-14 20:54:45 +02:00
|
|
|
|
2008-08-03 16:47:32 +02:00
|
|
|
make_assignment<vhdl_nbassign_stmt>(proc, container, stmt, false);
|
2008-08-05 12:02:36 +02:00
|
|
|
|
|
|
|
|
// Don't generate a zero-wait if this is the last statement in
|
|
|
|
|
// the process
|
|
|
|
|
if (!is_last)
|
|
|
|
|
container->add_stmt
|
|
|
|
|
(new vhdl_wait_stmt(VHDL_WAIT_FOR, new vhdl_const_time(0)));
|
2008-07-14 20:54:45 +02:00
|
|
|
}
|
|
|
|
|
else
|
2008-08-03 16:47:32 +02:00
|
|
|
make_assignment<vhdl_assign_stmt>(proc, container, stmt, true);
|
2008-07-14 20:54:45 +02:00
|
|
|
|
2008-06-18 13:51:11 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-06 17:55:45 +02:00
|
|
|
/*
|
|
|
|
|
* Delay statements are equivalent to the `wait for' form of the
|
|
|
|
|
* VHDL wait statement.
|
|
|
|
|
*/
|
2008-06-24 21:01:06 +02:00
|
|
|
static int draw_delay(vhdl_procedural *proc, stmt_container *container,
|
2008-06-11 15:11:37 +02:00
|
|
|
ivl_statement_t stmt)
|
2008-06-06 17:55:45 +02:00
|
|
|
{
|
2008-06-09 13:40:59 +02:00
|
|
|
// This currently ignores the time units and precision
|
|
|
|
|
// of the enclosing scope
|
|
|
|
|
// A neat way to do this would be to make these values
|
|
|
|
|
// constants in the scope (type is Time), and have the
|
|
|
|
|
// VHDL wait statement compute the value from that.
|
|
|
|
|
// The other solution is to add them as parameters to
|
|
|
|
|
// the vhdl_process class
|
2008-06-19 13:16:19 +02:00
|
|
|
vhdl_expr *time;
|
|
|
|
|
if (ivl_statement_type(stmt) == IVL_ST_DELAY) {
|
|
|
|
|
uint64_t value = ivl_stmt_delay_val(stmt);
|
|
|
|
|
time = new vhdl_const_time(value, TIME_UNIT_NS);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2008-07-23 17:18:49 +02:00
|
|
|
time = translate_time_expr(ivl_stmt_delay_expr(stmt));
|
2008-06-19 13:16:19 +02:00
|
|
|
if (NULL == time)
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2008-06-12 12:24:43 +02:00
|
|
|
|
2008-06-09 17:40:32 +02:00
|
|
|
ivl_statement_t sub_stmt = ivl_stmt_sub_stmt(stmt);
|
2008-08-03 16:47:32 +02:00
|
|
|
vhdl_wait_stmt *wait =
|
|
|
|
|
new vhdl_wait_stmt(VHDL_WAIT_FOR, time);
|
|
|
|
|
container->add_stmt(wait);
|
|
|
|
|
|
|
|
|
|
// Expand the sub-statement as well
|
|
|
|
|
// Often this would result in a useless `null' statement which
|
|
|
|
|
// is caught here instead
|
|
|
|
|
if (ivl_statement_type(sub_stmt) != IVL_ST_NOOP)
|
|
|
|
|
draw_stmt(proc, container, sub_stmt);
|
2008-06-09 13:40:59 +02:00
|
|
|
|
2008-06-13 14:59:48 +02:00
|
|
|
// Any further assignments occur after simulation time 0
|
|
|
|
|
// so they cannot be used to initialize signal declarations
|
2008-06-24 20:06:06 +02:00
|
|
|
// (if this scope is an initial process)
|
|
|
|
|
proc->get_scope()->set_initializing(false);
|
2008-06-13 14:59:48 +02:00
|
|
|
|
2008-06-06 17:55:45 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A wait statement waits for a level change on a @(..) list of
|
2008-07-17 18:36:42 +02:00
|
|
|
* signals. Purely combinatorial processes (i.e. no posedge/negedge
|
|
|
|
|
* events) produce a `wait on' statement at the end of the process.
|
|
|
|
|
* Sequential processes produce a `wait until' statement at the
|
|
|
|
|
* start of the process.
|
2008-06-06 17:55:45 +02:00
|
|
|
*/
|
2008-06-24 21:01:06 +02:00
|
|
|
static int draw_wait(vhdl_procedural *_proc, stmt_container *container,
|
2008-06-11 15:11:37 +02:00
|
|
|
ivl_statement_t stmt)
|
2008-06-06 17:55:45 +02:00
|
|
|
{
|
2008-06-24 21:01:06 +02:00
|
|
|
// Wait statements only occur in processes
|
|
|
|
|
vhdl_process *proc = dynamic_cast<vhdl_process*>(_proc);
|
|
|
|
|
assert(proc); // Catch not process
|
2008-07-04 12:58:33 +02:00
|
|
|
|
2008-07-14 21:29:49 +02:00
|
|
|
vhdl_binop_expr *test =
|
|
|
|
|
new vhdl_binop_expr(VHDL_BINOP_OR, vhdl_type::boolean());
|
2008-07-17 18:36:42 +02:00
|
|
|
|
2008-06-06 18:36:15 +02:00
|
|
|
int nevents = ivl_stmt_nevent(stmt);
|
2008-07-17 18:36:42 +02:00
|
|
|
|
|
|
|
|
bool combinatorial = true; // True if no negedge/posedge events
|
2008-06-06 18:36:15 +02:00
|
|
|
for (int i = 0; i < nevents; i++) {
|
|
|
|
|
ivl_event_t event = ivl_stmt_events(stmt, i);
|
2008-07-17 18:36:42 +02:00
|
|
|
if (ivl_event_npos(event) > 0 || ivl_event_nneg(event) > 0)
|
|
|
|
|
combinatorial = false;
|
|
|
|
|
}
|
2008-06-06 18:36:15 +02:00
|
|
|
|
2008-07-17 18:36:42 +02:00
|
|
|
if (combinatorial) {
|
|
|
|
|
vhdl_wait_stmt *wait = new vhdl_wait_stmt(VHDL_WAIT_ON);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < nevents; i++) {
|
|
|
|
|
ivl_event_t event = ivl_stmt_events(stmt, i);
|
|
|
|
|
|
|
|
|
|
int nany = ivl_event_nany(event);
|
|
|
|
|
for (int i = 0; i < nany; i++) {
|
|
|
|
|
ivl_nexus_t nexus = ivl_event_any(event, i);
|
|
|
|
|
vhdl_var_ref *ref = nexus_to_var_ref(proc->get_scope(), nexus);
|
2008-06-06 18:36:15 +02:00
|
|
|
|
2008-07-17 18:36:42 +02:00
|
|
|
wait->add_sensitivity(ref->get_name());
|
|
|
|
|
delete ref;
|
|
|
|
|
}
|
2008-06-12 11:36:38 +02:00
|
|
|
}
|
|
|
|
|
|
2008-08-05 12:02:36 +02:00
|
|
|
draw_stmt(proc, container, ivl_stmt_sub_stmt(stmt), true);
|
2008-07-17 18:36:42 +02:00
|
|
|
container->add_stmt(wait);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (int i = 0; i < nevents; i++) {
|
|
|
|
|
ivl_event_t event = ivl_stmt_events(stmt, i);
|
2008-06-12 11:36:38 +02:00
|
|
|
|
2008-07-17 18:36:42 +02:00
|
|
|
int nany = ivl_event_nany(event);
|
|
|
|
|
for (int i = 0; i < nany; i++) {
|
|
|
|
|
ivl_nexus_t nexus = ivl_event_any(event, i);
|
|
|
|
|
vhdl_var_ref *ref = nexus_to_var_ref(proc->get_scope(), nexus);
|
|
|
|
|
|
|
|
|
|
ref->set_name(ref->get_name() + "'Event");
|
|
|
|
|
test->add_expr(ref);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int nneg = ivl_event_nneg(event);
|
|
|
|
|
for (int i = 0; i < nneg; i++) {
|
|
|
|
|
ivl_nexus_t nexus = ivl_event_neg(event, i);
|
|
|
|
|
vhdl_var_ref *ref = nexus_to_var_ref(proc->get_scope(), nexus);
|
|
|
|
|
vhdl_fcall *detect =
|
|
|
|
|
new vhdl_fcall("falling_edge", vhdl_type::boolean());
|
|
|
|
|
detect->add_expr(ref);
|
|
|
|
|
|
|
|
|
|
test->add_expr(detect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int npos = ivl_event_npos(event);
|
|
|
|
|
for (int i = 0; i < npos; i++) {
|
|
|
|
|
ivl_nexus_t nexus = ivl_event_pos(event, i);
|
|
|
|
|
vhdl_var_ref *ref = nexus_to_var_ref(proc->get_scope(), nexus);
|
|
|
|
|
vhdl_fcall *detect =
|
|
|
|
|
new vhdl_fcall("rising_edge", vhdl_type::boolean());
|
|
|
|
|
detect->add_expr(ref);
|
2008-07-14 21:29:49 +02:00
|
|
|
|
2008-07-17 18:36:42 +02:00
|
|
|
test->add_expr(detect);
|
|
|
|
|
}
|
2008-06-12 11:36:38 +02:00
|
|
|
}
|
2008-07-17 18:36:42 +02:00
|
|
|
|
|
|
|
|
container->add_stmt(new vhdl_wait_stmt(VHDL_WAIT_UNTIL, test));
|
2008-08-05 12:02:36 +02:00
|
|
|
draw_stmt(proc, container, ivl_stmt_sub_stmt(stmt), true);
|
2008-06-06 18:36:15 +02:00
|
|
|
}
|
|
|
|
|
|
2008-06-06 17:55:45 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-24 21:01:06 +02:00
|
|
|
static int draw_if(vhdl_procedural *proc, stmt_container *container,
|
2008-08-05 12:09:51 +02:00
|
|
|
ivl_statement_t stmt, bool is_last)
|
2008-06-11 15:11:37 +02:00
|
|
|
{
|
2008-06-11 15:20:05 +02:00
|
|
|
vhdl_expr *test = translate_expr(ivl_stmt_cond_expr(stmt));
|
|
|
|
|
if (NULL == test)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
vhdl_if_stmt *vhdif = new vhdl_if_stmt(test);
|
|
|
|
|
|
2008-07-15 15:26:19 +02:00
|
|
|
ivl_statement_t cond_true_stmt = ivl_stmt_cond_true(stmt);
|
|
|
|
|
if (cond_true_stmt)
|
2008-08-05 12:09:51 +02:00
|
|
|
draw_stmt(proc, vhdif->get_then_container(), cond_true_stmt, is_last);
|
2008-06-16 13:13:01 +02:00
|
|
|
|
|
|
|
|
ivl_statement_t cond_false_stmt = ivl_stmt_cond_false(stmt);
|
|
|
|
|
if (cond_false_stmt)
|
2008-08-05 12:09:51 +02:00
|
|
|
draw_stmt(proc, vhdif->get_else_container(), cond_false_stmt, is_last);
|
2008-06-11 15:20:05 +02:00
|
|
|
|
|
|
|
|
container->add_stmt(vhdif);
|
|
|
|
|
|
2008-06-11 15:11:37 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-24 21:01:06 +02:00
|
|
|
static int draw_case(vhdl_procedural *proc, stmt_container *container,
|
2008-08-05 12:09:51 +02:00
|
|
|
ivl_statement_t stmt, bool is_last)
|
2008-06-21 16:03:36 +02:00
|
|
|
{
|
|
|
|
|
vhdl_expr *test = translate_expr(ivl_stmt_cond_expr(stmt));
|
|
|
|
|
if (NULL == test)
|
|
|
|
|
return 1;
|
2008-06-23 12:36:12 +02:00
|
|
|
|
|
|
|
|
// VHDL case expressions are required to be quite simple: variable
|
|
|
|
|
// references or slices. So we may need to create a temporary
|
|
|
|
|
// variable to hold the result of the expression evaluation
|
2008-07-03 17:27:36 +02:00
|
|
|
if (typeid(*test) != typeid(vhdl_var_ref)) {
|
2008-06-23 12:36:12 +02:00
|
|
|
const char *tmp_name = "Verilog_Case_Ex";
|
|
|
|
|
vhdl_type *test_type = new vhdl_type(*test->get_type());
|
2008-07-04 12:15:34 +02:00
|
|
|
|
|
|
|
|
if (!proc->get_scope()->have_declared(tmp_name)) {
|
|
|
|
|
proc->get_scope()->add_decl
|
|
|
|
|
(new vhdl_var_decl(tmp_name, new vhdl_type(*test_type)));
|
|
|
|
|
}
|
2008-06-23 12:36:12 +02:00
|
|
|
|
|
|
|
|
vhdl_var_ref *tmp_ref = new vhdl_var_ref(tmp_name, NULL);
|
|
|
|
|
container->add_stmt(new vhdl_assign_stmt(tmp_ref, test));
|
|
|
|
|
|
2008-07-04 12:15:34 +02:00
|
|
|
test = new vhdl_var_ref(tmp_name, test_type);
|
2008-06-23 12:36:12 +02:00
|
|
|
}
|
2008-06-21 16:03:36 +02:00
|
|
|
|
|
|
|
|
vhdl_case_stmt *vhdlcase = new vhdl_case_stmt(test);
|
|
|
|
|
container->add_stmt(vhdlcase);
|
2008-07-03 17:17:56 +02:00
|
|
|
|
|
|
|
|
// VHDL is more strict than Verilog about covering every
|
|
|
|
|
// possible case. So make sure we add an 'others' branch
|
|
|
|
|
// if there isn't a default one.
|
|
|
|
|
bool have_others = false;
|
2008-06-21 16:03:36 +02:00
|
|
|
|
|
|
|
|
int nbranches = ivl_stmt_case_count(stmt);
|
|
|
|
|
for (int i = 0; i < nbranches; i++) {
|
2008-07-04 12:10:20 +02:00
|
|
|
vhdl_expr *when;
|
2008-06-24 11:55:45 +02:00
|
|
|
ivl_expr_t net = ivl_stmt_case_expr(stmt, i);
|
|
|
|
|
if (net) {
|
2008-07-04 12:10:20 +02:00
|
|
|
when = translate_expr(net)->cast(test->get_type());
|
2008-06-24 11:55:45 +02:00
|
|
|
if (NULL == when)
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2008-07-03 17:17:56 +02:00
|
|
|
else {
|
2008-07-04 12:10:20 +02:00
|
|
|
when = new vhdl_var_ref("others", NULL);
|
2008-07-03 17:17:56 +02:00
|
|
|
have_others = true;
|
|
|
|
|
}
|
2008-06-24 11:55:45 +02:00
|
|
|
|
2008-07-04 12:10:20 +02:00
|
|
|
vhdl_case_branch *branch = new vhdl_case_branch(when);
|
|
|
|
|
vhdlcase->add_branch(branch);
|
2008-08-05 12:09:51 +02:00
|
|
|
|
|
|
|
|
ivl_statement_t stmt_i = ivl_stmt_case_stmt(stmt, i);
|
|
|
|
|
draw_stmt(proc, branch->get_container(), stmt_i, is_last);
|
2008-06-21 16:03:36 +02:00
|
|
|
}
|
2008-07-03 17:17:56 +02:00
|
|
|
|
2008-07-04 12:10:20 +02:00
|
|
|
if (!have_others) {
|
|
|
|
|
vhdl_case_branch *others =
|
|
|
|
|
new vhdl_case_branch(new vhdl_var_ref("others", NULL));
|
|
|
|
|
others->get_container()->add_stmt(new vhdl_null_stmt());
|
|
|
|
|
vhdlcase->add_branch(others);
|
|
|
|
|
}
|
2008-06-21 16:03:36 +02:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-24 21:01:06 +02:00
|
|
|
int draw_while(vhdl_procedural *proc, stmt_container *container,
|
2008-06-21 16:13:44 +02:00
|
|
|
ivl_statement_t stmt)
|
|
|
|
|
{
|
|
|
|
|
vhdl_expr *test = translate_expr(ivl_stmt_cond_expr(stmt));
|
|
|
|
|
if (NULL == test)
|
|
|
|
|
return 1;
|
|
|
|
|
|
2008-07-27 19:39:16 +02:00
|
|
|
// The test must be a Boolean (and std_logic and (un)signed types
|
|
|
|
|
// must be explicitly cast unlike in Verilog)
|
|
|
|
|
vhdl_type boolean(VHDL_TYPE_BOOLEAN);
|
|
|
|
|
test = test->cast(&boolean);
|
|
|
|
|
|
2008-06-21 16:13:44 +02:00
|
|
|
vhdl_while_stmt *loop = new vhdl_while_stmt(test);
|
|
|
|
|
container->add_stmt(loop);
|
|
|
|
|
|
|
|
|
|
draw_stmt(proc, loop->get_container(), ivl_stmt_sub_stmt(stmt));
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-24 15:30:10 +02:00
|
|
|
int draw_forever(vhdl_procedural *proc, stmt_container *container,
|
|
|
|
|
ivl_statement_t stmt)
|
|
|
|
|
{
|
|
|
|
|
vhdl_loop_stmt *loop = new vhdl_loop_stmt;
|
|
|
|
|
container->add_stmt(loop);
|
|
|
|
|
|
|
|
|
|
draw_stmt(proc, loop->get_container(), ivl_stmt_sub_stmt(stmt));
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-24 15:52:06 +02:00
|
|
|
int draw_repeat(vhdl_procedural *proc, stmt_container *container,
|
|
|
|
|
ivl_statement_t stmt)
|
|
|
|
|
{
|
|
|
|
|
vhdl_expr *times = translate_expr(ivl_stmt_cond_expr(stmt));
|
|
|
|
|
if (NULL == times)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
vhdl_type integer(VHDL_TYPE_INTEGER);
|
|
|
|
|
times = times->cast(&integer);
|
|
|
|
|
|
|
|
|
|
const char *it_name = "Verilog_Repeat";
|
|
|
|
|
vhdl_for_stmt *loop =
|
|
|
|
|
new vhdl_for_stmt(it_name, new vhdl_const_int(1), times);
|
|
|
|
|
container->add_stmt(loop);
|
|
|
|
|
|
|
|
|
|
draw_stmt(proc, loop->get_container(), ivl_stmt_sub_stmt(stmt));
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-07 11:54:39 +02:00
|
|
|
/*
|
|
|
|
|
* Tasks are difficult to translate to VHDL since they allow things
|
|
|
|
|
* not allowed by VHDL's corresponding procedures (e.g. updating
|
|
|
|
|
* global variables. The solution here is to expand tasks in-line.
|
|
|
|
|
*/
|
|
|
|
|
int draw_utask(vhdl_procedural *proc, stmt_container *container,
|
|
|
|
|
ivl_statement_t stmt)
|
|
|
|
|
{
|
|
|
|
|
ivl_scope_t tscope = ivl_stmt_call(stmt);
|
|
|
|
|
|
|
|
|
|
// TODO: adding some comments to the output would be helpful
|
|
|
|
|
|
|
|
|
|
// TOOD: this completely ignores paremeters!
|
|
|
|
|
draw_stmt(proc, container, ivl_scope_def(tscope), false);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-03 19:26:36 +02:00
|
|
|
/*
|
|
|
|
|
* Generate VHDL statements for the given Verilog statement and
|
2008-06-11 15:11:37 +02:00
|
|
|
* add them to the given VHDL process. The container is the
|
|
|
|
|
* location to add statements: e.g. the process body, a branch
|
|
|
|
|
* of an if statement, etc.
|
2008-08-05 12:02:36 +02:00
|
|
|
*
|
|
|
|
|
* The flag is_last should be set if this is the final statement
|
|
|
|
|
* in a block or process. It avoids generating useless `wait for 0ns'
|
|
|
|
|
* statements if the next statement would be a wait anyway.
|
2008-06-03 19:26:36 +02:00
|
|
|
*/
|
2008-06-24 21:01:06 +02:00
|
|
|
int draw_stmt(vhdl_procedural *proc, stmt_container *container,
|
2008-08-05 12:02:36 +02:00
|
|
|
ivl_statement_t stmt, bool is_last)
|
2008-06-03 19:26:36 +02:00
|
|
|
{
|
2008-06-21 16:03:36 +02:00
|
|
|
assert(stmt);
|
|
|
|
|
|
2008-06-03 19:44:17 +02:00
|
|
|
switch (ivl_statement_type(stmt)) {
|
|
|
|
|
case IVL_ST_STASK:
|
2008-06-11 15:11:37 +02:00
|
|
|
return draw_stask(proc, container, stmt);
|
2008-06-04 21:57:15 +02:00
|
|
|
case IVL_ST_BLOCK:
|
2008-08-05 12:02:36 +02:00
|
|
|
return draw_block(proc, container, stmt, is_last);
|
2008-06-05 14:16:35 +02:00
|
|
|
case IVL_ST_NOOP:
|
2008-06-11 15:11:37 +02:00
|
|
|
return draw_noop(proc, container, stmt);
|
2008-06-18 13:51:11 +02:00
|
|
|
case IVL_ST_ASSIGN:
|
2008-08-05 12:02:36 +02:00
|
|
|
return draw_assign(proc, container, stmt, is_last);
|
2008-06-06 17:55:45 +02:00
|
|
|
case IVL_ST_ASSIGN_NB:
|
2008-06-11 15:11:37 +02:00
|
|
|
return draw_nbassign(proc, container, stmt);
|
2008-06-06 17:55:45 +02:00
|
|
|
case IVL_ST_DELAY:
|
2008-06-19 13:16:19 +02:00
|
|
|
case IVL_ST_DELAYX:
|
2008-06-11 15:11:37 +02:00
|
|
|
return draw_delay(proc, container, stmt);
|
2008-06-06 17:55:45 +02:00
|
|
|
case IVL_ST_WAIT:
|
2008-06-11 15:11:37 +02:00
|
|
|
return draw_wait(proc, container, stmt);
|
|
|
|
|
case IVL_ST_CONDIT:
|
2008-08-05 12:09:51 +02:00
|
|
|
return draw_if(proc, container, stmt, is_last);
|
2008-06-21 16:03:36 +02:00
|
|
|
case IVL_ST_CASE:
|
2008-08-05 12:09:51 +02:00
|
|
|
return draw_case(proc, container, stmt, is_last);
|
2008-06-21 16:13:44 +02:00
|
|
|
case IVL_ST_WHILE:
|
|
|
|
|
return draw_while(proc, container, stmt);
|
2008-07-24 15:30:10 +02:00
|
|
|
case IVL_ST_FOREVER:
|
|
|
|
|
return draw_forever(proc, container, stmt);
|
2008-07-24 15:52:06 +02:00
|
|
|
case IVL_ST_REPEAT:
|
|
|
|
|
return draw_repeat(proc, container, stmt);
|
2008-08-07 11:54:39 +02:00
|
|
|
case IVL_ST_UTASK:
|
|
|
|
|
return draw_utask(proc, container, stmt);
|
2008-08-18 16:29:30 +02:00
|
|
|
case IVL_ST_FORCE:
|
|
|
|
|
case IVL_ST_RELEASE:
|
|
|
|
|
error("force/release statements cannot be translated to VHDL");
|
|
|
|
|
return 1;
|
|
|
|
|
case IVL_ST_DISABLE:
|
|
|
|
|
error("disable statement cannot be translated to VHDL");
|
|
|
|
|
return 1;
|
2008-08-08 21:09:40 +02:00
|
|
|
case IVL_ST_CASEX:
|
|
|
|
|
error("casex statement cannot be translated to VHDL");
|
|
|
|
|
return 1;
|
2008-08-22 21:25:58 +02:00
|
|
|
case IVL_ST_CASEZ:
|
|
|
|
|
error("casez statement cannot be translated to VHDL");
|
|
|
|
|
return 1;
|
2008-08-18 16:24:38 +02:00
|
|
|
case IVL_ST_FORK:
|
|
|
|
|
error("fork statement cannot be translated to VHDL");
|
|
|
|
|
return 1;
|
|
|
|
|
case IVL_ST_CASSIGN:
|
|
|
|
|
case IVL_ST_DEASSIGN:
|
|
|
|
|
error("continuous procedural assignment cannot be translated to VHDL");
|
|
|
|
|
return 1;
|
2008-06-03 19:44:17 +02:00
|
|
|
default:
|
|
|
|
|
error("No VHDL translation for statement at %s:%d (type = %d)",
|
|
|
|
|
ivl_stmt_file(stmt), ivl_stmt_lineno(stmt),
|
|
|
|
|
ivl_statement_type(stmt));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2008-06-03 19:26:36 +02:00
|
|
|
}
|