iverilog/tgt-vhdl/vhdl_type.cc

185 lines
4.2 KiB
C++
Raw Permalink Normal View History

/*
* VHDL variable and signal types.
*
* 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_type.hh"
2008-07-17 12:43:59 +02:00
#include <cassert>
#include <sstream>
#include <iostream>
vhdl_type *vhdl_type::std_logic()
{
return new vhdl_type(VHDL_TYPE_STD_LOGIC);
}
vhdl_type *vhdl_type::string()
{
return new vhdl_type(VHDL_TYPE_STRING);
}
vhdl_type *vhdl_type::line()
{
return new vhdl_type(VHDL_TYPE_LINE);
}
2008-06-12 11:36:38 +02:00
vhdl_type *vhdl_type::boolean()
{
return new vhdl_type(VHDL_TYPE_BOOLEAN);
}
2008-06-09 13:46:55 +02:00
vhdl_type *vhdl_type::integer()
{
return new vhdl_type(VHDL_TYPE_INTEGER);
}
2008-07-08 01:20:31 +02:00
vhdl_type *vhdl_type::nunsigned(int width, int lsb)
2008-06-14 18:09:31 +02:00
{
2008-07-08 01:20:31 +02:00
return new vhdl_type(VHDL_TYPE_UNSIGNED, width-1+lsb, lsb);
2008-06-14 18:09:31 +02:00
}
2008-07-08 01:20:31 +02:00
vhdl_type *vhdl_type::nsigned(int width, int lsb)
2008-06-14 18:09:31 +02:00
{
2008-07-08 01:20:31 +02:00
return new vhdl_type(VHDL_TYPE_SIGNED, width-1+lsb, lsb);
2008-06-14 18:09:31 +02:00
}
2008-06-19 13:16:19 +02:00
vhdl_type *vhdl_type::time()
{
return new vhdl_type(VHDL_TYPE_TIME);
}
2008-07-17 12:43:59 +02:00
vhdl_type *vhdl_type::get_base() const
{
assert(name_ == VHDL_TYPE_ARRAY);
return base_;
}
/*
* This is just the name of the type, without any parameters.
*/
std::string vhdl_type::get_string() const
{
switch (name_) {
case VHDL_TYPE_STD_LOGIC:
return std::string("std_logic");
case VHDL_TYPE_STD_LOGIC_VECTOR:
return std::string("std_logic_vector");
case VHDL_TYPE_STRING:
return std::string("String");
case VHDL_TYPE_LINE:
return std::string("Line");
case VHDL_TYPE_FILE:
return std::string("File");
2008-06-09 13:46:55 +02:00
case VHDL_TYPE_INTEGER:
return std::string("Integer");
case VHDL_TYPE_BOOLEAN:
return std::string("Boolean");
case VHDL_TYPE_SIGNED:
return std::string("signed");
case VHDL_TYPE_UNSIGNED:
return std::string("unsigned");
2008-07-17 12:43:59 +02:00
case VHDL_TYPE_ARRAY:
// Each array has its own type declaration
return array_name_;
default:
return std::string("BadType");
}
}
/*
* The is the qualified name of the type.
*/
std::string vhdl_type::get_decl_string() const
{
switch (name_) {
case VHDL_TYPE_STD_LOGIC_VECTOR:
case VHDL_TYPE_UNSIGNED:
case VHDL_TYPE_SIGNED:
{
std::ostringstream ss;
ss << get_string() << "(" << msb_;
ss << " downto " << lsb_ << ")";
return ss.str();
}
default:
return get_string();
}
}
/*
* Like get_decl_string but completely expands array declarations.
*/
std::string vhdl_type::get_type_decl_string() const
{
switch (name_) {
2008-07-17 12:43:59 +02:00
case VHDL_TYPE_ARRAY:
{
std::ostringstream ss;
ss << "array (" << msb_ << " downto "
<< lsb_ << ") of "
<< base_->get_decl_string();
return ss.str();
2008-07-17 12:43:59 +02:00
}
default:
return get_decl_string();
}
}
void vhdl_type::emit(std::ostream &of, int level) const
{
of << get_decl_string();
}
vhdl_type::vhdl_type(const vhdl_type &other)
: name_(other.name_), msb_(other.msb_), lsb_(other.lsb_),
array_name_(other.array_name_)
{
if (other.base_ != NULL)
base_ = new vhdl_type(*other.base_);
else
base_ = NULL;
}
vhdl_type::~vhdl_type()
{
if (base_ != NULL)
delete base_;
}
vhdl_type *vhdl_type::std_logic_vector(int msb, int lsb)
{
return new vhdl_type(VHDL_TYPE_STD_LOGIC_VECTOR, msb, lsb);
}
2008-07-07 22:45:27 +02:00
2008-07-08 01:20:31 +02:00
vhdl_type *vhdl_type::type_for(int width, bool issigned, int lsb)
2008-07-07 22:45:27 +02:00
{
2008-07-08 01:20:31 +02:00
if (width == 1)
2008-07-07 22:45:27 +02:00
return vhdl_type::std_logic();
else if (issigned)
2008-07-08 01:20:31 +02:00
return vhdl_type::nsigned(width, lsb);
2008-07-07 22:45:27 +02:00
else
2008-07-08 01:20:31 +02:00
return vhdl_type::nunsigned(width, lsb);
2008-07-07 22:45:27 +02:00
}
2008-07-17 12:43:59 +02:00
vhdl_type *vhdl_type::array_of(vhdl_type *b, std::string &n, int m, int l)
{
return new vhdl_type(b, n, m, l);
}