Conversion of std_logic to integer

This commit is contained in:
Nick Gasson 2008-07-28 22:46:39 +01:00
parent 1250010696
commit f88415b1d7
3 changed files with 19 additions and 1 deletions

View File

@ -70,7 +70,15 @@ vhdl_expr *vhdl_expr::cast(const vhdl_type *to)
}
}
else if (to->get_name() == VHDL_TYPE_INTEGER) {
vhdl_fcall *conv = new vhdl_fcall("To_Integer", new vhdl_type(*to));
vhdl_fcall *conv;
if (type_->get_name() == VHDL_TYPE_STD_LOGIC) {
require_support_function(SF_LOGIC_TO_INTEGER);
conv = new vhdl_fcall(support_function::function_name(SF_LOGIC_TO_INTEGER),
vhdl_type::integer());
}
else
conv = new vhdl_fcall("To_Integer", new vhdl_type(*to));
conv->add_expr(this);
return conv;

View File

@ -43,6 +43,7 @@ const char *support_function::function_name(support_function_t type)
case SF_TERNARY_LOGIC: return "Ternary_Logic";
case SF_TERNARY_UNSIGNED: return "Ternary_Unsigned";
case SF_TERNARY_SIGNED: return "Ternary_Signed";
case SF_LOGIC_TO_INTEGER: return "Logic_To_Integer";
default:
assert(false);
}
@ -60,6 +61,8 @@ vhdl_type *support_function::function_type(support_function_t type)
case SF_REDUCE_XOR:
case SF_TERNARY_LOGIC:
return vhdl_type::std_logic();
case SF_LOGIC_TO_INTEGER:
return vhdl_type::integer();
default:
assert(false);
}
@ -141,6 +144,12 @@ void support_function::emit(std::ostream &of, int level) const
<< nl_string(level);
emit_ternary(of, level);
break;
case SF_LOGIC_TO_INTEGER:
of << "(X : std_logic) return integer is" << nl_string(level)
<< "begin" << nl_string(indent(level))
<< "if X = '1' then return 1; else return 0; end if;"
<< nl_string(level);
break;
default:
assert(false);
}

View File

@ -33,6 +33,7 @@ enum support_function_t {
SF_TERNARY_LOGIC,
SF_TERNARY_UNSIGNED,
SF_TERNARY_SIGNED,
SF_LOGIC_TO_INTEGER,
};
class support_function : public vhdl_function {