2008-06-16 20:41:01 +02:00
|
|
|
/*
|
|
|
|
|
* VHDL code generation for LPM devices.
|
|
|
|
|
*
|
|
|
|
|
* 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>
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
2008-07-07 22:45:27 +02:00
|
|
|
static vhdl_expr *draw_concat_lpm(vhdl_scope *scope, ivl_lpm_t lpm)
|
2008-06-16 20:41:01 +02:00
|
|
|
{
|
2008-07-07 22:45:27 +02:00
|
|
|
vhdl_type *result_type =
|
|
|
|
|
vhdl_type::type_for(ivl_lpm_width(lpm), ivl_lpm_signed(lpm) != 0);
|
|
|
|
|
vhdl_binop_expr *expr =
|
|
|
|
|
new vhdl_binop_expr(VHDL_BINOP_CONCAT, result_type);
|
|
|
|
|
|
|
|
|
|
for (int i = ivl_lpm_selects(lpm) - 1; i >= 0; i--) {
|
|
|
|
|
vhdl_expr *e = nexus_to_var_ref(scope, ivl_lpm_data(lpm, i));
|
|
|
|
|
if (NULL == e)
|
|
|
|
|
return NULL;
|
2008-06-16 20:41:01 +02:00
|
|
|
|
2008-07-07 22:45:27 +02:00
|
|
|
expr->add_expr(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return expr;
|
|
|
|
|
}
|
2008-06-16 20:41:01 +02:00
|
|
|
|
2008-07-07 22:45:27 +02:00
|
|
|
static vhdl_expr *draw_binop_lpm(vhdl_scope *scope, ivl_lpm_t lpm, vhdl_binop_t op)
|
|
|
|
|
{
|
|
|
|
|
vhdl_type *result_type =
|
|
|
|
|
vhdl_type::type_for(ivl_lpm_width(lpm), ivl_lpm_signed(lpm) != 0);
|
|
|
|
|
vhdl_binop_expr *expr = new vhdl_binop_expr(op, result_type);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
|
vhdl_expr *e = nexus_to_var_ref(scope, ivl_lpm_data(lpm, i));
|
|
|
|
|
if (NULL == e)
|
|
|
|
|
return NULL;
|
2008-06-16 20:49:24 +02:00
|
|
|
|
2008-07-07 22:45:27 +02:00
|
|
|
expr->add_expr(e);
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-16 20:49:24 +02:00
|
|
|
if (op == VHDL_BINOP_MULT) {
|
|
|
|
|
// Need to resize the output to the desired size,
|
|
|
|
|
// as this does not happen automatically in VHDL
|
|
|
|
|
|
|
|
|
|
unsigned out_width = ivl_lpm_width(lpm);
|
|
|
|
|
vhdl_fcall *resize =
|
|
|
|
|
new vhdl_fcall("Resize", vhdl_type::nsigned(out_width));
|
|
|
|
|
resize->add_expr(expr);
|
|
|
|
|
resize->add_expr(new vhdl_const_int(out_width));
|
|
|
|
|
|
2008-07-07 22:45:27 +02:00
|
|
|
return resize;
|
2008-06-16 20:49:24 +02:00
|
|
|
}
|
2008-07-07 22:45:27 +02:00
|
|
|
else
|
|
|
|
|
return expr;
|
2008-06-16 20:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-03 15:45:54 +02:00
|
|
|
/*
|
|
|
|
|
* Return the base of a part select.
|
|
|
|
|
*/
|
2008-07-07 20:46:18 +02:00
|
|
|
static vhdl_expr *part_select_base(vhdl_scope *scope, ivl_lpm_t lpm)
|
2008-06-27 15:58:03 +02:00
|
|
|
{
|
2008-07-03 15:45:54 +02:00
|
|
|
vhdl_expr *off;
|
2008-06-30 17:18:55 +02:00
|
|
|
ivl_nexus_t base = ivl_lpm_data(lpm, 1);
|
|
|
|
|
if (base != NULL)
|
2008-07-07 20:46:18 +02:00
|
|
|
off = nexus_to_var_ref(scope, base);
|
2008-06-30 17:18:55 +02:00
|
|
|
else
|
|
|
|
|
off = new vhdl_const_int(ivl_lpm_base(lpm));
|
2008-07-03 15:45:54 +02:00
|
|
|
|
|
|
|
|
// Array indexes must be integers
|
|
|
|
|
vhdl_type integer(VHDL_TYPE_INTEGER);
|
|
|
|
|
return off->cast(&integer);
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-07 20:46:18 +02:00
|
|
|
static vhdl_expr *draw_part_select_vp_lpm(vhdl_scope *scope, ivl_lpm_t lpm)
|
2008-07-03 15:45:54 +02:00
|
|
|
{
|
2008-07-07 20:46:18 +02:00
|
|
|
vhdl_var_ref *selfrom = nexus_to_var_ref(scope, ivl_lpm_data(lpm, 0));
|
2008-07-03 15:45:54 +02:00
|
|
|
if (NULL == selfrom)
|
2008-07-07 20:46:18 +02:00
|
|
|
return NULL;
|
2008-07-03 15:45:54 +02:00
|
|
|
|
2008-07-07 20:46:18 +02:00
|
|
|
vhdl_expr *off = part_select_base(scope, lpm);;
|
2008-06-27 15:58:03 +02:00
|
|
|
if (NULL == off)
|
2008-07-07 20:46:18 +02:00
|
|
|
return NULL;
|
2008-06-27 15:58:03 +02:00
|
|
|
|
2008-07-07 17:17:54 +02:00
|
|
|
selfrom->set_slice(off, ivl_lpm_width(lpm) - 1);
|
2008-07-07 20:46:18 +02:00
|
|
|
return selfrom;
|
2008-06-27 15:58:03 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-03 15:45:54 +02:00
|
|
|
static int draw_part_select_pv_lpm(vhdl_arch *arch, ivl_lpm_t lpm)
|
|
|
|
|
{
|
|
|
|
|
vhdl_var_ref *selfrom = nexus_to_var_ref(arch->get_scope(), ivl_lpm_data(lpm, 0));
|
|
|
|
|
if (NULL == selfrom)
|
|
|
|
|
return 1;
|
|
|
|
|
|
2008-07-07 20:46:18 +02:00
|
|
|
vhdl_expr *off = part_select_base(arch->get_scope(), lpm);;
|
2008-07-03 15:45:54 +02:00
|
|
|
if (NULL == off)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
vhdl_var_ref *out = nexus_to_var_ref(arch->get_scope(), ivl_lpm_q(lpm, 0));
|
|
|
|
|
if (NULL == out)
|
|
|
|
|
return 1;
|
|
|
|
|
|
2008-07-07 17:17:54 +02:00
|
|
|
out->set_slice(off, ivl_lpm_width(lpm) - 1);
|
2008-07-03 15:45:54 +02:00
|
|
|
arch->add_stmt(new vhdl_cassign_stmt(out, selfrom));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-07 20:46:18 +02:00
|
|
|
static vhdl_expr *draw_ufunc_lpm(vhdl_scope *scope, ivl_lpm_t lpm)
|
2008-06-30 17:35:29 +02:00
|
|
|
{
|
|
|
|
|
vhdl_fcall *fcall = new vhdl_fcall(ivl_lpm_basename(lpm), NULL);
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < ivl_lpm_size(lpm); i++) {
|
2008-07-07 20:46:18 +02:00
|
|
|
vhdl_var_ref *ref = nexus_to_var_ref(scope, ivl_lpm_data(lpm, i));
|
2008-06-30 17:35:29 +02:00
|
|
|
if (NULL == ref)
|
2008-07-07 20:46:18 +02:00
|
|
|
return NULL;
|
2008-06-30 17:35:29 +02:00
|
|
|
|
|
|
|
|
fcall->add_expr(ref);
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-07 20:46:18 +02:00
|
|
|
return fcall;
|
2008-06-30 17:35:29 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-07 20:46:18 +02:00
|
|
|
static vhdl_expr *draw_reduction_lpm(vhdl_scope *scope, ivl_lpm_t lpm,
|
|
|
|
|
const char *rfunc, bool invert)
|
2008-07-07 16:45:20 +02:00
|
|
|
{
|
|
|
|
|
vhdl_fcall *fcall = new vhdl_fcall(rfunc, vhdl_type::std_logic());
|
|
|
|
|
|
2008-07-07 20:46:18 +02:00
|
|
|
vhdl_var_ref *ref = nexus_to_var_ref(scope, ivl_lpm_data(lpm, 0));
|
2008-07-07 16:45:20 +02:00
|
|
|
if (NULL == ref)
|
2008-07-07 20:46:18 +02:00
|
|
|
return NULL;
|
2008-07-07 16:45:20 +02:00
|
|
|
|
|
|
|
|
fcall->add_expr(ref);
|
|
|
|
|
|
|
|
|
|
if (invert)
|
2008-07-07 20:46:18 +02:00
|
|
|
return new vhdl_unaryop_expr
|
|
|
|
|
(VHDL_UNARYOP_NOT, fcall, vhdl_type::std_logic());
|
2008-07-07 16:45:20 +02:00
|
|
|
else
|
2008-07-07 20:46:18 +02:00
|
|
|
return fcall;
|
2008-07-07 16:45:20 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-07 20:46:18 +02:00
|
|
|
static vhdl_expr *draw_sign_extend_lpm(vhdl_scope *scope, ivl_lpm_t lpm)
|
2008-07-07 20:27:52 +02:00
|
|
|
{
|
2008-07-07 20:46:18 +02:00
|
|
|
vhdl_expr *ref = nexus_to_var_ref(scope, ivl_lpm_data(lpm, 0));
|
|
|
|
|
if (ref)
|
|
|
|
|
return ref->resize(ivl_lpm_width(lpm));
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
2008-07-07 20:27:52 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-07 20:46:18 +02:00
|
|
|
vhdl_expr *lpm_to_expr(vhdl_scope *scope, ivl_lpm_t lpm)
|
2008-06-16 20:41:01 +02:00
|
|
|
{
|
|
|
|
|
switch (ivl_lpm_type(lpm)) {
|
|
|
|
|
case IVL_LPM_ADD:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_binop_lpm(scope, lpm, VHDL_BINOP_ADD);
|
2008-06-16 20:49:24 +02:00
|
|
|
case IVL_LPM_SUB:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_binop_lpm(scope, lpm, VHDL_BINOP_SUB);
|
2008-06-16 20:49:24 +02:00
|
|
|
case IVL_LPM_MULT:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_binop_lpm(scope, lpm, VHDL_BINOP_MULT);
|
2008-07-07 15:48:57 +02:00
|
|
|
case IVL_LPM_CONCAT:
|
2008-07-07 22:45:27 +02:00
|
|
|
return draw_concat_lpm(scope, lpm);
|
2008-06-27 15:58:03 +02:00
|
|
|
case IVL_LPM_PART_VP:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_part_select_vp_lpm(scope, lpm);
|
2008-06-30 17:35:29 +02:00
|
|
|
case IVL_LPM_UFUNC:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_ufunc_lpm(scope, lpm);
|
2008-07-07 16:45:20 +02:00
|
|
|
case IVL_LPM_RE_AND:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_reduction_lpm(scope, lpm, "Reduce_AND", false);
|
2008-07-07 16:45:20 +02:00
|
|
|
case IVL_LPM_RE_NAND:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_reduction_lpm(scope, lpm, "Reduce_AND", true);
|
2008-07-07 16:45:20 +02:00
|
|
|
case IVL_LPM_RE_NOR:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_reduction_lpm(scope, lpm, "Reduce_OR", true);
|
2008-07-07 16:45:20 +02:00
|
|
|
case IVL_LPM_RE_OR:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_reduction_lpm(scope, lpm, "Reduce_OR", false);
|
2008-07-07 16:45:20 +02:00
|
|
|
case IVL_LPM_RE_XOR:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_reduction_lpm(scope, lpm, "Reduce_XOR", false);
|
2008-07-07 16:45:20 +02:00
|
|
|
case IVL_LPM_RE_XNOR:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_reduction_lpm(scope, lpm, "Reduce_XNOR", false);
|
2008-07-07 20:27:52 +02:00
|
|
|
case IVL_LPM_SIGN_EXT:
|
2008-07-07 20:46:18 +02:00
|
|
|
return draw_sign_extend_lpm(scope, lpm);
|
2008-06-16 20:41:01 +02:00
|
|
|
default:
|
|
|
|
|
error("Unsupported LPM type: %d", ivl_lpm_type(lpm));
|
2008-07-07 20:46:18 +02:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int draw_lpm(vhdl_arch *arch, ivl_lpm_t lpm)
|
|
|
|
|
{
|
|
|
|
|
if (ivl_lpm_type(lpm) == IVL_LPM_PART_PV)
|
|
|
|
|
return draw_part_select_pv_lpm(arch, lpm);
|
|
|
|
|
else {
|
|
|
|
|
vhdl_expr *f = lpm_to_expr(arch->get_scope(), lpm);
|
|
|
|
|
if (NULL == f)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
vhdl_var_ref *out = nexus_to_var_ref(arch->get_scope(), ivl_lpm_q(lpm, 0));
|
2008-07-07 21:41:29 +02:00
|
|
|
if (out)
|
|
|
|
|
arch->add_stmt(new vhdl_cassign_stmt(out, f));
|
2008-07-07 20:46:18 +02:00
|
|
|
|
|
|
|
|
return 0;
|
2008-06-16 20:41:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|