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-03 19:44:17 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Generate VHDL for the $display system task.
|
2008-06-03 20:14:47 +02:00
|
|
|
* This is implemented using the functions in std.textio. Each
|
|
|
|
|
* parameter is written to a line variable in the process and
|
|
|
|
|
* then the line is written to the special variable `Output'
|
2008-06-03 20:46:10 +02:00
|
|
|
* (which represents the console). Subsequent $displays will
|
|
|
|
|
* use the same line variable.
|
2008-06-03 20:14:47 +02:00
|
|
|
*
|
|
|
|
|
* It's possible, although quite unlikely, that there will be
|
|
|
|
|
* name collision with an existing variable called
|
2008-06-03 20:46:10 +02:00
|
|
|
* `Verilog_Display_Line' -- do something about this?
|
2008-06-03 19:44:17 +02:00
|
|
|
*/
|
|
|
|
|
static int draw_stask_display(vhdl_process *proc, ivl_statement_t stmt)
|
|
|
|
|
{
|
2008-06-04 14:52:56 +02:00
|
|
|
// Add the package requirement to the containing entity
|
|
|
|
|
proc->get_parent()->get_parent()->requires_package("std.textio");
|
2008-06-03 20:46:10 +02:00
|
|
|
|
|
|
|
|
const char *display_line = "Verilog_Display_Line";
|
|
|
|
|
|
|
|
|
|
if (!proc->have_declared_var(display_line)) {
|
|
|
|
|
vhdl_type *line_type = new vhdl_scalar_type("Line");
|
|
|
|
|
vhdl_var_decl *line_var =
|
|
|
|
|
new vhdl_var_decl(display_line, line_type);
|
|
|
|
|
line_var->set_comment("For generating $display output");
|
|
|
|
|
proc->add_decl(line_var);
|
|
|
|
|
}
|
2008-06-04 16:19:44 +02:00
|
|
|
|
|
|
|
|
// Write the data into the line
|
|
|
|
|
int count = ivl_stmt_parm_count(stmt);
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
2008-06-04 21:57:15 +02:00
|
|
|
// $display may have an empty parameter, in which case
|
|
|
|
|
// the expression will be null
|
|
|
|
|
// The behaviour here seems to be to output a space
|
|
|
|
|
ivl_expr_t net = ivl_stmt_parm(stmt, i);
|
|
|
|
|
vhdl_expr *e = NULL;
|
|
|
|
|
if (net) {
|
|
|
|
|
// TODO: Need to add a call to Type'Image for types not
|
|
|
|
|
// supported by std.textio
|
|
|
|
|
e = translate_expr(net);
|
|
|
|
|
if (NULL == e)
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
e = new vhdl_const_string(" ");
|
2008-06-04 16:19:44 +02:00
|
|
|
|
|
|
|
|
vhdl_pcall_stmt *write = new vhdl_pcall_stmt("Write");
|
|
|
|
|
write->add_expr(new vhdl_var_ref(display_line));
|
|
|
|
|
write->add_expr(e);
|
|
|
|
|
|
|
|
|
|
proc->add_stmt(write);
|
|
|
|
|
}
|
2008-06-03 20:46:10 +02:00
|
|
|
|
2008-06-04 14:52:56 +02:00
|
|
|
// WriteLine(Output, Verilog_Display_Line)
|
|
|
|
|
vhdl_pcall_stmt *write_line = new vhdl_pcall_stmt("WriteLine");
|
2008-06-04 22:58:51 +02:00
|
|
|
write_line->add_expr(new vhdl_var_ref("std.textio.Output"));
|
2008-06-04 16:19:44 +02:00
|
|
|
write_line->add_expr(new vhdl_var_ref(display_line));
|
2008-06-04 14:27:42 +02:00
|
|
|
proc->add_stmt(write_line);
|
2008-06-03 20:14:47 +02:00
|
|
|
|
2008-06-03 19:44:17 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Generate VHDL for system tasks (like $display). Not all of
|
|
|
|
|
* these are supported.
|
|
|
|
|
*/
|
|
|
|
|
static int draw_stask(vhdl_process *proc, ivl_statement_t stmt)
|
|
|
|
|
{
|
|
|
|
|
const char *name = ivl_stmt_name(stmt);
|
|
|
|
|
|
|
|
|
|
if (strcmp(name, "$display") == 0)
|
|
|
|
|
return draw_stask_display(proc, stmt);
|
|
|
|
|
else {
|
|
|
|
|
error("No VHDL translation for system task %s", name);
|
|
|
|
|
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
|
|
|
|
|
* `begin' and `end process' function like a Verilog block.
|
|
|
|
|
*/
|
|
|
|
|
static int draw_block(vhdl_process *proc, ivl_statement_t stmt)
|
|
|
|
|
{
|
|
|
|
|
int count = ivl_stmt_block_count(stmt);
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
if (draw_stmt(proc, ivl_stmt_block_stmt(stmt, i)) != 0)
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-05 14:16:35 +02:00
|
|
|
/*
|
|
|
|
|
* A no-op statement. This corresponds to a `null' statement in
|
|
|
|
|
* VHDL.
|
|
|
|
|
*/
|
|
|
|
|
static int draw_noop(vhdl_process *proc, ivl_statement_t stmt)
|
|
|
|
|
{
|
|
|
|
|
proc->add_stmt(new vhdl_null_stmt());
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-06 17:55:45 +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_process *proc, ivl_statement_t stmt)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "draw_nbassign" << std::endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Delay statements are equivalent to the `wait for' form of the
|
|
|
|
|
* VHDL wait statement.
|
|
|
|
|
*/
|
|
|
|
|
static int draw_delay(vhdl_process *proc, ivl_statement_t stmt)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "draw_delay" << std::endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A wait statement waits for a level change on a @(..) list of
|
|
|
|
|
* signals. This needs to be implemented by an `if' statement
|
|
|
|
|
* inside the process (which the appropriate signals added to
|
|
|
|
|
* the sensitivity list).
|
|
|
|
|
*/
|
|
|
|
|
static int draw_wait(vhdl_process *proc, ivl_statement_t stmt)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "draw_wait" << std::endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-03 19:26:36 +02:00
|
|
|
/*
|
|
|
|
|
* Generate VHDL statements for the given Verilog statement and
|
|
|
|
|
* add them to the given VHDL process.
|
|
|
|
|
*/
|
|
|
|
|
int draw_stmt(vhdl_process *proc, ivl_statement_t stmt)
|
|
|
|
|
{
|
2008-06-03 19:44:17 +02:00
|
|
|
switch (ivl_statement_type(stmt)) {
|
|
|
|
|
case IVL_ST_STASK:
|
|
|
|
|
return draw_stask(proc, stmt);
|
2008-06-04 21:57:15 +02:00
|
|
|
case IVL_ST_BLOCK:
|
|
|
|
|
return draw_block(proc, stmt);
|
2008-06-05 14:16:35 +02:00
|
|
|
case IVL_ST_NOOP:
|
|
|
|
|
return draw_noop(proc, stmt);
|
2008-06-06 17:55:45 +02:00
|
|
|
case IVL_ST_ASSIGN_NB:
|
|
|
|
|
return draw_nbassign(proc, stmt);
|
|
|
|
|
case IVL_ST_DELAY:
|
|
|
|
|
return draw_delay(proc, stmt);
|
|
|
|
|
case IVL_ST_WAIT:
|
|
|
|
|
return draw_wait(proc, stmt);
|
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
|
|
|
}
|