vlog95: Add CA support, other enhancements, refactoring and bug fixes.

This patch add the following functionality to the vlog95 generator.

  Add support for most CAs. Selects still don't work correctly.
  Emitting a named event as an expression.
  Enhance the ability to find a signal in a nexus.
  Add code to emit a nexus driven by a constant.
  Rework the delay/strength code for logic gates/CAs
  Refactor some of the number code.
  Refactor the variable emitting code to support named blocks.
  Translate the compiler code back to <var> = <event> <value>
This commit is contained in:
Cary R 2011-01-30 23:53:53 -08:00 committed by Stephen Williams
parent 1e32ce5df8
commit 8744c4279d
7 changed files with 1002 additions and 294 deletions

View File

@ -44,7 +44,7 @@ CPPFLAGS = $(INCLUDE_PATH) @CPPFLAGS@ @DEFS@ @PICFLAG@
CFLAGS = @WARNING_FLAGS@ @CFLAGS@
LDFLAGS = @LDFLAGS@
O = vlog95.o event.o expr.o misc.o scope.o stmt.o
O = vlog95.o event.o expr.o misc.o numbers.o scope.o stmt.o
all: dep vlog95.tgt

View File

@ -204,99 +204,18 @@ static void emit_expr_delay(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
emit_scaled_delay(scope, ivl_expr_delay_val(expr));
}
static void emit_expr_number(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
/*
* An event in an expression context must be a named event.
*/
static void emit_expr_event(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
{
const char *bits = ivl_expr_bits(expr);
unsigned nbits = ivl_expr_width(expr);
/* A signed value can only be 32 bits long since it can only be
* represented as an integer. We can trim any matching MSB bits
* to make it fit. We do not support undefined bits. */
if (ivl_expr_signed(expr)) {
int rtype;
int32_t value = get_int32_from_number(expr, &rtype);
if (rtype > 0) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Signed number is "
"greater than 32 bits (%u) and cannot be "
"safely represented.\n",
ivl_expr_file(expr), ivl_expr_lineno(expr),
rtype);
vlog_errors += 1;
} else if (rtype == -1) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Signed number has "
"an undefined bit and cannot be "
"represented.\n",
ivl_expr_file(expr), ivl_expr_lineno(expr));
vlog_errors += 1;
return;
} else if (rtype == -2) {
fprintf(vlog_out, "'bz");
} else if (rtype == -3) {
fprintf(vlog_out, "'bx");
} else {
fprintf(vlog_out, "%"PRId32, value);
}
/* An unsigned number is represented in hex if all the bits are
* defined and it is more than a single bit otherwise it is
* represented in binary form to preserve all the information. */
} else {
int idx;
unsigned has_undef = 0;
for (idx = (int)nbits -1; idx >= 0; idx -= 1) {
if ((bits[idx] != '0') && (bits[idx] != '1')) {
has_undef = 1;
break;
}
}
if (has_undef || (nbits < 2)) {
fprintf(vlog_out, "%u'b", nbits);
for (idx = (int)nbits-1; idx >= 0; idx -= 1) {
fprintf(vlog_out, "%c", bits[idx]);
}
} else {
int start = 4*(nbits/4);
unsigned result = 0;
fprintf(vlog_out, "%u'h", nbits);
/* The first digit may not be a full hex digit. */
if (start < nbits) {
for (idx = start; idx < nbits; idx += 1) {
if (bits[idx] == '1') result |= 1U << (idx%4);
}
fprintf(vlog_out, "%1x", result);
}
/* Now print the full hex digits. */
for (idx = start-1; idx >= 0; idx -= 4) {
result = 0;
if (bits[idx] == '1') result |= 0x8;
if (bits[idx-1] == '1') result |= 0x4;
if (bits[idx-2] == '1') result |= 0x2;
if (bits[idx-3] == '1') result |= 0x1;
fprintf(vlog_out, "%1x", result);
}
}
}
}
static void emit_expr_real_number(ivl_scope_t scope, ivl_expr_t expr,
unsigned wid)
{
double value = ivl_expr_dvalue(expr);
/* Check for NaN. */
if (value != value) {
fprintf(vlog_out, "(0.0/0.0)");
return;
}
/* Check for the infinities. */
if (value && value == 0.5*value) {
if (value > 0) fprintf(vlog_out, "(1.0/0.0)");
else fprintf(vlog_out, "(-1.0/0.0)");
return;
}
// HERE: This needs to be reworked. We must have a trailing digit after the
// decimal point and we want to print all the significant digits.
// I think the will require our own printing routine.
fprintf(vlog_out, "%#.16g", ivl_expr_dvalue(expr));
ivl_event_t event = ivl_expr_event(expr);
ivl_scope_t ev_scope = ivl_event_scope(event);
assert(! ivl_event_nany(event));
assert(! ivl_event_npos(event));
assert(! ivl_event_nneg(event));
emit_scope_module_path(scope, ev_scope);
fprintf(vlog_out, "%s", ivl_event_basename(event));
}
static void emit_expr_scope(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
@ -309,12 +228,12 @@ static void emit_expr_select(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
ivl_expr_t sel_expr = ivl_expr_oper2(expr);
ivl_expr_t sig_expr = ivl_expr_oper1(expr);
if (sel_expr) {
ivl_signal_t sig = ivl_expr_signal(sig_expr);
int msb = 1;
int lsb = 0;
unsigned width = ivl_expr_width(expr);
assert(width > 0);
if (ivl_expr_type(sig_expr) == IVL_EX_SIGNAL) {
ivl_signal_t sig = ivl_expr_signal(sig_expr);
msb = ivl_signal_msb(sig);
lsb = ivl_signal_lsb(sig);
}
@ -385,8 +304,7 @@ static void emit_expr_ternary(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
static void emit_expr_ufunc(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
{
ivl_scope_t ufunc_def = ivl_expr_def(expr);
// HERE: I think we need to also consider the scope of the func relative
// to the calling scope to get the correct name.
emit_scope_module_path(scope, ufunc_def);
emit_expr_func(scope, expr, ivl_scope_tname(ufunc_def));
}
@ -449,11 +367,16 @@ void emit_expr(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
case IVL_EX_DELAY:
emit_expr_delay(scope, expr, wid);
break;
case IVL_EX_EVENT:
emit_expr_event(scope, expr, wid);
break;
case IVL_EX_NUMBER:
emit_expr_number(scope, expr, wid);
emit_number(ivl_expr_bits(expr), ivl_expr_width(expr),
ivl_expr_signed(expr), ivl_expr_file(expr),
ivl_expr_lineno(expr));
break;
case IVL_EX_REALNUM:
emit_expr_real_number(scope, expr, wid);
emit_real_number(ivl_expr_dvalue(expr));
break;
case IVL_EX_SCOPE:
emit_expr_scope(scope, expr, wid);

View File

@ -330,7 +330,8 @@ void emit_scaled_expr(ivl_scope_t scope, ivl_expr_t expr, int msb, int lsb)
static unsigned find_signal_in_nexus(ivl_scope_t scope, ivl_nexus_t nex)
{
ivl_signal_t sig, use_sig = 0;
ivl_signal_t use_sig = 0;
unsigned is_driver = 0;
unsigned is_array = 0;
int64_t array_idx = 0;
unsigned idx, count;
@ -338,12 +339,34 @@ static unsigned find_signal_in_nexus(ivl_scope_t scope, ivl_nexus_t nex)
count = ivl_nexus_ptrs(nex);
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
sig = ivl_nexus_ptr_sig(nex_ptr);
ivl_signal_t sig = ivl_nexus_ptr_sig(nex_ptr);
if (! sig) continue;
if (ivl_signal_local(sig)) continue;
if (ivl_signal_local(sig)) {
/* If the local signal is another receiver skip it. */
if ((ivl_nexus_ptr_drive1(nex_ptr) == IVL_DR_HiZ) &&
(ivl_nexus_ptr_drive0(nex_ptr) == IVL_DR_HiZ)) {
continue;
}
assert(0);
}
/* We have a signal that can be used to find the name. */
if (scope == ivl_signal_scope(sig)) {
if (use_sig) {
/* Swap a receiver for a driver. */
if (is_driver &&
(ivl_nexus_ptr_drive1(nex_ptr) == IVL_DR_HiZ) &&
(ivl_nexus_ptr_drive0(nex_ptr) == IVL_DR_HiZ)) {
use_sig = sig;
is_driver = 0;
if (ivl_signal_dimensions(sig) > 0) {
is_array = 1;
array_idx = ivl_nexus_ptr_pin(nex_ptr);
array_idx += ivl_signal_array_base(sig);
}
continue;
}
// HERE: Which one should we use? For now it's the first one found.
// I believe this needs to be solved (see the inout.v test).
fprintf(stderr, "%s:%u: vlog95 warning: Duplicate "
"name (%s",
ivl_signal_file(sig),
@ -360,6 +383,11 @@ static unsigned find_signal_in_nexus(ivl_scope_t scope, ivl_nexus_t nex)
fprintf(stderr, ")\n");
} else {
use_sig = sig;
/* This signal is a driver. */
if ((ivl_nexus_ptr_drive1(nex_ptr) != IVL_DR_HiZ) ||
(ivl_nexus_ptr_drive0(nex_ptr) != IVL_DR_HiZ)) {
is_driver = 1;
}
if (ivl_signal_dimensions(sig) > 0) {
is_array = 1;
array_idx = ivl_nexus_ptr_pin(nex_ptr);
@ -379,6 +407,50 @@ static unsigned find_signal_in_nexus(ivl_scope_t scope, ivl_nexus_t nex)
return 0;
}
void emit_const_nexus(ivl_scope_t scope, ivl_net_const_t const_net)
{
switch (ivl_const_type(const_net)) {
case IVL_VT_LOGIC:
case IVL_VT_BOOL:
emit_number(ivl_const_bits(const_net),
ivl_const_width(const_net),
ivl_const_signed(const_net),
ivl_const_file(const_net),
ivl_const_lineno(const_net));
break;
case IVL_VT_REAL:
emit_real_number(ivl_const_real(const_net));
break;
default:
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Unknown constant type "
"(%d).\n",
ivl_const_file(const_net),
ivl_const_lineno(const_net),
(int)ivl_const_type(const_net));
vlog_errors += 1;
break;
}
}
static unsigned find_const_nexus(ivl_scope_t scope, ivl_nexus_t nex)
{
unsigned idx, count;
count = ivl_nexus_ptrs(nex);
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_net_const_t const_net = ivl_nexus_ptr_con(nex_ptr);
// HERE: Do we need to check for duplicates?
if (const_net) {
assert(! ivl_nexus_ptr_pin(nex_ptr));
emit_const_nexus(scope, const_net);
return 1;
}
}
return 0;
}
// HERE: Does this work correctly with an array reference created from @*?
void emit_name_of_nexus(ivl_scope_t scope, ivl_nexus_t nex)
{
@ -389,9 +461,13 @@ void emit_name_of_nexus(ivl_scope_t scope, ivl_nexus_t nex)
* the module scope if the passed scope was not the module scope. */
if (find_signal_in_nexus(get_module_scope(scope), nex)) return;
/* If there is no signals driving this then look for a constant. */
if (find_const_nexus(scope, nex)) return;
// HERE: Need to check arr[var]? Can this be rebuilt?
// Then look for down scopes and then any scope. For all this warn if
// multiples are found in a given scope.
// multiples are found in a given scope. This all needs to be before
// the constant code.
fprintf(vlog_out, "<missing>");
}
@ -483,148 +559,3 @@ void emit_scope_path(ivl_scope_t scope, ivl_scope_t call_scope)
free(call_name);
}
}
/*
* Extract an uint64_t value from the given number expression. If the result
* type is 0 then the returned value is valid. If it is positive then the
* value was too large and if it is negative then the value had undefined
* bits.
*/
uint64_t get_uint64_from_number(ivl_expr_t expr, int *result_type)
{
unsigned nbits = ivl_expr_width(expr);
unsigned trim_wid = nbits - 1;
const char *bits = ivl_expr_bits(expr);
unsigned idx;
uint64_t value = 0;
assert(ivl_expr_type(expr) == IVL_EX_NUMBER);
assert(! ivl_expr_signed(expr));
/* Trim any '0' bits from the MSB. */
for (/* none */; trim_wid > 0; trim_wid -= 1) {
if ('0' != bits[trim_wid]) {
trim_wid += 1;
break;
}
}
if (trim_wid < nbits) trim_wid += 1;
/* Check to see if the value is too large. */
if (trim_wid > 64U) {
*result_type = trim_wid;
return 0;
}
/* Now build the value from the bits. */
for (idx = 0; idx < trim_wid; idx += 1) {
if (bits[idx] == '1') value |= (uint64_t)1 << idx;
else if (bits[idx] != '0') {
*result_type = -1;
/* If the value is entirely x/z then return -2 or -3. */
if ((idx == 0) && (trim_wid == 1)) {
if (bits[idx] == 'x') *result_type -= 1;
*result_type -= 1;
}
return 0;
}
}
*result_type = 0;
return value;
}
/*
* Extract an int64_t value from the given number expression. If the result
* type is 0 then the returned value is valid. If it is positive then the
* value was too large and if it is negative then the value had undefined
* bits. -2 is all bits z and -3 is all bits x.
*/
int64_t get_int64_from_number(ivl_expr_t expr, int *result_type)
{
unsigned is_signed = ivl_expr_signed(expr);
unsigned nbits = ivl_expr_width(expr);
unsigned trim_wid = nbits - 1;
const char *bits = ivl_expr_bits(expr);
const char msb = is_signed ? bits[trim_wid] : '0';
unsigned idx;
int64_t value = 0;
assert(ivl_expr_type(expr) == IVL_EX_NUMBER);
/* Trim any duplicate bits from the MSB. */
for (/* none */; trim_wid > 0; trim_wid -= 1) {
if (msb != bits[trim_wid]) {
trim_wid += 1;
break;
}
}
if (trim_wid < nbits) trim_wid += 1;
/* Check to see if the value is too large. */
if (trim_wid > 64U) {
*result_type = trim_wid;
return 0;
}
/* Now build the value from the bits. */
for (idx = 0; idx < trim_wid; idx += 1) {
if (bits[idx] == '1') value |= (int64_t)1 << idx;
else if (bits[idx] != '0') {
*result_type = -1;
/* If the value is entirely x/z then return -2 or -3. */
if ((idx == 0) && (trim_wid == 1)) {
if (bits[idx] == 'x') *result_type -= 1;
*result_type -= 1;
}
return 0;
}
}
/* Sign extend as needed. */
if (is_signed && (msb == '1') && (trim_wid < 64U)) {
value |= ~(((int64_t)1 << trim_wid) - (int64_t)1);
}
*result_type = 0;
return value;
}
/*
* Extract an int32_t value from the given number expression. If the result
* type is 0 then the returned value is valid. If it is positive then the
* value was too large and if it is negative then the value had undefined
* bits. -2 is all bits z and -3 is all bits x.
*/
int32_t get_int32_from_number(ivl_expr_t expr, int *result_type)
{
unsigned is_signed = ivl_expr_signed(expr);
unsigned nbits = ivl_expr_width(expr);
unsigned trim_wid = nbits - 1;
const char *bits = ivl_expr_bits(expr);
const char msb = is_signed ? bits[trim_wid] : '0';
unsigned idx;
int32_t value = 0;
assert(ivl_expr_type(expr) == IVL_EX_NUMBER);
/* Trim any duplicate bits from the MSB. */
for (/* none */; trim_wid > 0; trim_wid -= 1) {
if (msb != bits[trim_wid]) {
trim_wid += 1;
break;
}
}
if (trim_wid < nbits) trim_wid += 1;
/* Check to see if the value is too large. */
if (trim_wid > 32U) {
*result_type = trim_wid;
return 0;
}
/* Now build the value from the bits. */
for (idx = 0; idx < trim_wid; idx += 1) {
if (bits[idx] == '1') value |= (int32_t)1 << idx;
else if (bits[idx] != '0') {
*result_type = -1;
/* If the value is entirely x/z then return -2 or -3. */
if ((idx == 0) && (trim_wid == 1)) {
if (bits[idx] == 'x') *result_type -= 1;
*result_type -= 1;
}
return 0;
}
}
/* Sign extend as needed. */
if (is_signed && (msb == '1') && (trim_wid < 32U)) {
value |= ~(((int32_t)1 << trim_wid) - (int32_t)1);
}
*result_type = 0;
return value;
}

265
tgt-vlog95/numbers.c Normal file
View File

@ -0,0 +1,265 @@
/*
* Copyright (C) 2011 Cary R. (cygcary@yahoo.com)
*
* 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 "config.h"
# include "vlog95_priv.h"
/*
* Extract an int32_t value from the given bit information. If the result
* type is 0 then the returned value is valid. If it is positive then the
* value was too large and if it is negative then the value had undefined
* bits. -2 is all bits z and -3 is all bits x.
*/
static int32_t get_int32_from_bits(const char *bits, unsigned nbits,
unsigned is_signed, int *result_type)
{
unsigned trim_wid = nbits - 1;
const char msb = is_signed ? bits[trim_wid] : '0';
unsigned idx;
int32_t value = 0;
/* Trim any duplicate bits from the MSB. */
for (/* none */; trim_wid > 0; trim_wid -= 1) {
if (msb != bits[trim_wid]) {
trim_wid += 1;
break;
}
}
if (trim_wid < nbits) trim_wid += 1;
/* Check to see if the value is too large. */
if (trim_wid > 32U) {
*result_type = trim_wid;
return 0;
}
/* Now build the value from the bits. */
for (idx = 0; idx < trim_wid; idx += 1) {
if (bits[idx] == '1') value |= (int32_t)1 << idx;
else if (bits[idx] != '0') {
*result_type = -1;
/* If the value is entirely x/z then return -2 or -3. */
if ((idx == 0) && (trim_wid == 1)) {
if (bits[idx] == 'x') *result_type -= 1;
*result_type -= 1;
}
return 0;
}
}
/* Sign extend as needed. */
if (is_signed && (msb == '1') && (trim_wid < 32U)) {
value |= ~(((int32_t)1 << trim_wid) - (int32_t)1);
}
*result_type = 0;
return value;
}
void emit_number(const char *bits, unsigned nbits, unsigned is_signed,
const char *file, unsigned lineno)
{
/* A signed value can only be 32 bits long since it can only be
* represented as an integer. We can trim any matching MSB bits
* to make it fit. We do not support undefined bits. */
if (is_signed) {
int rtype;
int32_t value = get_int32_from_bits(bits, nbits, 1, &rtype);
if (rtype > 0) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Signed number is "
"greater than 32 bits (%u) and cannot be "
"safely represented.\n", file, lineno,
rtype);
vlog_errors += 1;
} else if (rtype == -1) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Signed number has "
"an undefined bit and cannot be "
"represented.\n", file, lineno);
vlog_errors += 1;
return;
} else if (rtype == -2) {
fprintf(vlog_out, "'bz");
} else if (rtype == -3) {
fprintf(vlog_out, "'bx");
} else {
fprintf(vlog_out, "%"PRId32, value);
}
/* An unsigned number is represented in hex if all the bits are
* defined and it is more than a single bit otherwise it is
* represented in binary form to preserve all the information. */
} else {
int idx;
unsigned has_undef = 0;
for (idx = (int)nbits -1; idx >= 0; idx -= 1) {
if ((bits[idx] != '0') && (bits[idx] != '1')) {
has_undef = 1;
break;
}
}
if (has_undef || (nbits < 2)) {
fprintf(vlog_out, "%u'b", nbits);
for (idx = (int)nbits-1; idx >= 0; idx -= 1) {
fprintf(vlog_out, "%c", bits[idx]);
}
} else {
int start = 4*(nbits/4);
unsigned result = 0;
fprintf(vlog_out, "%u'h", nbits);
/* The first digit may not be a full hex digit. */
if (start < nbits) {
for (idx = start; idx < nbits; idx += 1) {
if (bits[idx] == '1') result |= 1U << (idx%4);
}
fprintf(vlog_out, "%1x", result);
}
/* Now print the full hex digits. */
for (idx = start-1; idx >= 0; idx -= 4) {
result = 0;
if (bits[idx] == '1') result |= 0x8;
if (bits[idx-1] == '1') result |= 0x4;
if (bits[idx-2] == '1') result |= 0x2;
if (bits[idx-3] == '1') result |= 0x1;
fprintf(vlog_out, "%1x", result);
}
}
}
}
void emit_real_number(double value)
{
/* Check for NaN. */
if (value != value) {
fprintf(vlog_out, "(0.0/0.0)");
return;
}
/* Check for the infinities. */
if (value && value == 0.5*value) {
if (value > 0) fprintf(vlog_out, "(1.0/0.0)");
else fprintf(vlog_out, "(-1.0/0.0)");
return;
}
// HERE: This needs to be reworked. We must have a trailing digit after the
// decimal point and we want to print all the significant digits.
// I think the will require our own printing routine.
fprintf(vlog_out, "%#.16g", value);
}
/*
* Extract an uint64_t value from the given number expression. If the result
* type is 0 then the returned value is valid. If it is positive then the
* value was too large and if it is negative then the value had undefined
* bits.
*/
uint64_t get_uint64_from_number(ivl_expr_t expr, int *result_type)
{
unsigned nbits = ivl_expr_width(expr);
unsigned trim_wid = nbits - 1;
const char *bits = ivl_expr_bits(expr);
unsigned idx;
uint64_t value = 0;
assert(ivl_expr_type(expr) == IVL_EX_NUMBER);
assert(! ivl_expr_signed(expr));
/* Trim any '0' bits from the MSB. */
for (/* none */; trim_wid > 0; trim_wid -= 1) {
if ('0' != bits[trim_wid]) {
trim_wid += 1;
break;
}
}
if (trim_wid < nbits) trim_wid += 1;
/* Check to see if the value is too large. */
if (trim_wid > 64U) {
*result_type = trim_wid;
return 0;
}
/* Now build the value from the bits. */
for (idx = 0; idx < trim_wid; idx += 1) {
if (bits[idx] == '1') value |= (uint64_t)1 << idx;
else if (bits[idx] != '0') {
*result_type = -1;
/* If the value is entirely x/z then return -2 or -3. */
if ((idx == 0) && (trim_wid == 1)) {
if (bits[idx] == 'x') *result_type -= 1;
*result_type -= 1;
}
return 0;
}
}
*result_type = 0;
return value;
}
/*
* Extract an int64_t value from the given number expression. If the result
* type is 0 then the returned value is valid. If it is positive then the
* value was too large and if it is negative then the value had undefined
* bits. -2 is all bits z and -3 is all bits x.
*/
int64_t get_int64_from_number(ivl_expr_t expr, int *result_type)
{
unsigned is_signed = ivl_expr_signed(expr);
unsigned nbits = ivl_expr_width(expr);
unsigned trim_wid = nbits - 1;
const char *bits = ivl_expr_bits(expr);
const char msb = is_signed ? bits[trim_wid] : '0';
unsigned idx;
int64_t value = 0;
assert(ivl_expr_type(expr) == IVL_EX_NUMBER);
/* Trim any duplicate bits from the MSB. */
for (/* none */; trim_wid > 0; trim_wid -= 1) {
if (msb != bits[trim_wid]) {
trim_wid += 1;
break;
}
}
if (trim_wid < nbits) trim_wid += 1;
/* Check to see if the value is too large. */
if (trim_wid > 64U) {
*result_type = trim_wid;
return 0;
}
/* Now build the value from the bits. */
for (idx = 0; idx < trim_wid; idx += 1) {
if (bits[idx] == '1') value |= (int64_t)1 << idx;
else if (bits[idx] != '0') {
*result_type = -1;
/* If the value is entirely x/z then return -2 or -3. */
if ((idx == 0) && (trim_wid == 1)) {
if (bits[idx] == 'x') *result_type -= 1;
*result_type -= 1;
}
return 0;
}
}
/* Sign extend as needed. */
if (is_signed && (msb == '1') && (trim_wid < 64U)) {
value |= ~(((int64_t)1 << trim_wid) - (int64_t)1);
}
*result_type = 0;
return value;
}
/*
* Extract an int32_t value from the given number expression. If the result
* type is 0 then the returned value is valid. If it is positive then the
* value was too large and if it is negative then the value had undefined
* bits. -2 is all bits z and -3 is all bits x.
*/
int32_t get_int32_from_number(ivl_expr_t expr, int *result_type)
{
assert(ivl_expr_type(expr) == IVL_EX_NUMBER);
return get_int32_from_bits(ivl_expr_bits(expr), ivl_expr_width(expr),
ivl_expr_signed(expr), result_type);
}

View File

@ -200,7 +200,7 @@ static char *get_mangled_name(ivl_scope_t scope, unsigned root)
return name;
}
static void emit_gate_drive(ivl_net_logic_t nlogic, ivl_drive_t drive)
static unsigned emit_drive(ivl_drive_t drive)
{
switch (drive) {
case IVL_DR_HiZ:
@ -219,13 +219,10 @@ static void emit_gate_drive(ivl_net_logic_t nlogic, ivl_drive_t drive)
fprintf(vlog_out, "supply");
break;
default:
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Unsupported gate "
"drive (%d)\n", ivl_logic_file(nlogic),
ivl_logic_lineno(nlogic), (int)drive);
vlog_errors += 1;
return 1;
break;
}
return 0;
}
static void emit_gate_strength(ivl_net_logic_t nlogic)
@ -234,19 +231,54 @@ static void emit_gate_strength(ivl_net_logic_t nlogic)
ivl_drive_t drive0 = ivl_logic_drive0(nlogic);
if ((drive1 != IVL_DR_STRONG) || (drive0 != IVL_DR_STRONG)) {
fprintf(vlog_out, " (");
emit_gate_drive(nlogic, drive1);
if (emit_drive(drive1)) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Unsupported gate "
"1 drive (%d)\n", ivl_logic_file(nlogic),
ivl_logic_lineno(nlogic), (int)drive1);
vlog_errors += 1;
}
fprintf(vlog_out, "1, ");
emit_gate_drive(nlogic, drive0);
if (emit_drive(drive0)) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Unsupported gate "
"0 drive (%d)\n", ivl_logic_file(nlogic),
ivl_logic_lineno(nlogic), (int)drive0);
vlog_errors += 1;
}
fprintf(vlog_out, "0)");
}
}
static void emit_gate_delay(ivl_scope_t scope, ivl_net_logic_t nlogic,
unsigned dly_count)
static void emit_lpm_strength(ivl_lpm_t lpm)
{
ivl_drive_t drive1 = ivl_lpm_drive1(lpm);
ivl_drive_t drive0 = ivl_lpm_drive0(lpm);
if (drive0 != IVL_DR_STRONG || drive1 != IVL_DR_STRONG) {
fprintf(vlog_out, " (");
if (emit_drive(drive1)) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Unsupported LPM "
"1 drive (%d)\n", ivl_lpm_file(lpm),
ivl_lpm_lineno(lpm), (int)drive1);
vlog_errors += 1;
}
fprintf(vlog_out, "1, ");
emit_drive(drive0);
if (emit_drive(drive0)) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Unsupported LPM "
"0 drive (%d)\n", ivl_lpm_file(lpm),
ivl_lpm_lineno(lpm), (int)drive0);
vlog_errors += 1;
}
fprintf(vlog_out, "0)");
}
}
static void emit_delay(ivl_scope_t scope, ivl_expr_t rise, ivl_expr_t fall,
ivl_expr_t decay, unsigned dly_count)
{
ivl_expr_t rise = ivl_logic_delay(nlogic, 0);
ivl_expr_t fall = ivl_logic_delay(nlogic, 1);
ivl_expr_t decay = ivl_logic_delay(nlogic, 2);
assert((dly_count >= 2) && (dly_count <= 3));
/* No delays. */
@ -284,6 +316,445 @@ static void emit_gate_delay(ivl_scope_t scope, ivl_net_logic_t nlogic,
fprintf(vlog_out, ")");
}
unsigned is_local_nexus(ivl_scope_t scope, ivl_nexus_t nex)
{
unsigned idx, count = ivl_nexus_ptrs(nex);
unsigned is_local = 0;
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_signal_t sig = ivl_nexus_ptr_sig(nex_ptr);
if (! sig) continue;
if (scope != ivl_signal_scope(sig)) continue;
if ((ivl_nexus_ptr_drive1(nex_ptr) != IVL_DR_HiZ) ||
(ivl_nexus_ptr_drive0(nex_ptr) != IVL_DR_HiZ)) continue;
if (ivl_signal_local(sig)) {
// assert(! is_local);
is_local = 1;
} else {
is_local = 0;
break;
}
}
return is_local;
}
/*
* This returns the nexus of the LPM if it is not a local signal.
*/
ivl_nexus_t get_lpm_output(ivl_scope_t scope, ivl_lpm_t lpm)
{
ivl_nexus_t output = 0;
switch (ivl_lpm_type(lpm)) {
case IVL_LPM_ADD:
case IVL_LPM_ARRAY:
case IVL_LPM_CAST_INT:
case IVL_LPM_CAST_INT2:
case IVL_LPM_CAST_REAL:
case IVL_LPM_CMP_EEQ:
case IVL_LPM_CMP_EQ:
case IVL_LPM_CMP_GE:
case IVL_LPM_CMP_GT:
case IVL_LPM_CMP_NE:
case IVL_LPM_CMP_NEE:
case IVL_LPM_CONCAT:
case IVL_LPM_DIVIDE:
case IVL_LPM_MOD:
case IVL_LPM_MULT:
case IVL_LPM_MUX:
case IVL_LPM_PART_PV:
case IVL_LPM_PART_VP:
case IVL_LPM_RE_AND:
case IVL_LPM_RE_NAND:
case IVL_LPM_RE_NOR:
case IVL_LPM_RE_OR:
case IVL_LPM_RE_XOR:
case IVL_LPM_RE_XNOR:
case IVL_LPM_REPEAT:
case IVL_LPM_SFUNC:
case IVL_LPM_SHIFTL:
case IVL_LPM_SHIFTR:
case IVL_LPM_SIGN_EXT:
case IVL_LPM_SUB:
case IVL_LPM_UFUNC:
/* If the output of this LPM is a local signal then something
* else will request that this be emitted. */
output = ivl_lpm_q(lpm);
if (is_local_nexus(scope, output)) return 0;
break;
default:
// HERE: Can this be a simple assert at some point in time?
fprintf(vlog_out, "<unknown>");
fprintf(stderr, "%s:%u: vlog95 error: Unknown LPM type (%d).\n",
ivl_lpm_file(lpm), ivl_lpm_lineno(lpm),
(int)ivl_lpm_type(lpm));
vlog_errors += 1;
return 0;
}
return output;
}
static void emit_logic_as_ca(ivl_scope_t scope, ivl_net_logic_t nlogic);
static void emit_lpm_as_ca(ivl_scope_t scope, ivl_lpm_t lpm);
static void emit_nexus_as_ca(ivl_scope_t scope, ivl_nexus_t nex)
{
/* A local nexus only has a single driver. */
if (is_local_nexus(scope, nex)) {
unsigned idx, count = ivl_nexus_ptrs(nex);
ivl_lpm_t lpm = 0;
ivl_net_const_t net_const = 0;
ivl_net_logic_t net_logic = 0;
ivl_signal_t sig = 0;
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
if ((ivl_nexus_ptr_drive1(nex_ptr) == IVL_DR_HiZ) &&
(ivl_nexus_ptr_drive0(nex_ptr) == IVL_DR_HiZ)) continue;
ivl_lpm_t t_lpm = ivl_nexus_ptr_lpm(nex_ptr);
ivl_net_const_t t_net_const = ivl_nexus_ptr_con(nex_ptr);
ivl_net_logic_t t_net_logic = ivl_nexus_ptr_log(nex_ptr);
ivl_signal_t t_sig = ivl_nexus_ptr_sig(nex_ptr);
if (t_lpm) {
assert(! lpm);
lpm = t_lpm;
}
if (t_net_const) {
assert(! net_const);
net_const = t_net_const;
}
if (t_net_logic) {
assert(! net_logic);
net_logic = t_net_logic;
}
if (t_sig) {
assert(! sig);
sig = t_sig;
}
}
if (lpm) {
assert(! net_const);
assert(! net_logic);
assert(! sig);
fprintf(vlog_out, "(");
emit_lpm_as_ca(scope, lpm);
fprintf(vlog_out, ")");
} else if (net_const) {
assert( !net_logic);
assert(! sig);
emit_const_nexus(scope, net_const);
} else if (net_logic) {
assert(! sig);
fprintf(vlog_out, "(");
emit_logic_as_ca(scope, net_logic);
fprintf(vlog_out, ")");
} else if (sig) {
emit_name_of_nexus(scope, nex);
// HERE: The assert causes pr1703959 to fail.
// } else assert(0);
} else fprintf(vlog_out, "<missing>");
} else {
emit_name_of_nexus(scope, nex);
}
}
static void emit_logic_as_ca(ivl_scope_t scope, ivl_net_logic_t nlogic)
{
// HERE: Do we need to check that the pin count is correct for these?
switch (ivl_logic_type(nlogic)) {
case IVL_LO_AND:
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 0));
fprintf(vlog_out, " & ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1));
break;
case IVL_LO_BUF:
// case IVL_LO_BUFT:
case IVL_LO_BUFZ:
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 0));
break;
case IVL_LO_NAND:
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 0));
fprintf(vlog_out, " ~& ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1));
break;
case IVL_LO_NOR:
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 0));
fprintf(vlog_out, " ~| ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1));
break;
case IVL_LO_NOT:
fprintf(vlog_out, "~ ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 0));
case IVL_LO_OR:
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 0));
fprintf(vlog_out, " | ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1));
break;
case IVL_LO_XNOR:
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 0));
fprintf(vlog_out, " ~^ ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1));
break;
case IVL_LO_XOR:
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 0));
fprintf(vlog_out, " ^ ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1));
break;
default:
fprintf(vlog_out, "<unknown>");
fprintf(stderr, "%s:%u: vlog95 error: Unknown logic type (%d).\n",
ivl_logic_file(nlogic), ivl_logic_lineno(nlogic),
(int)ivl_logic_type(nlogic));
vlog_errors += 1;
}
}
static void emit_lpm_array(ivl_scope_t scope, ivl_lpm_t lpm)
{
ivl_signal_t sig = ivl_lpm_array(lpm);
emit_scope_module_path(scope, ivl_signal_scope(sig));
fprintf(vlog_out, "%s[", ivl_signal_basename(sig));
// HERE : Need to scale this to match array base.
emit_nexus_as_ca(scope, ivl_lpm_select(lpm));
fprintf(vlog_out, "]");
}
static void emit_lpm_concat(ivl_scope_t scope, ivl_lpm_t lpm)
{
unsigned idx, count= ivl_lpm_size(lpm);
fprintf(vlog_out, "{");
for (idx = count-1; idx > 0; idx -= 1) {
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, idx));
fprintf(vlog_out, ", ");
}
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, "}");
}
ivl_signal_t nexus_is_signal(ivl_scope_t scope, ivl_nexus_t nex)
{
return 0;
}
static void emit_lpm_part_select(ivl_scope_t scope, ivl_lpm_t lpm)
{
unsigned width = ivl_lpm_width(lpm);
unsigned base = ivl_lpm_base(lpm);
ivl_signal_t sig = nexus_is_signal(scope, ivl_lpm_data(lpm, 0));
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
if (! sig) return;
// HERE: We need the signal MSB/LSB to scale the select correctly.
fprintf(vlog_out, "[");
if (width == 1) {
ivl_nexus_t sel = ivl_lpm_data(lpm, 1);
if (sel) emit_nexus_as_ca(scope, sel);
else fprintf(vlog_out, "%u", base);
} else {
// HERE: No support for a variable select.
fprintf(vlog_out, "%u", base+width-1);
fprintf(vlog_out, ":");
fprintf(vlog_out, "%u", base);
}
fprintf(vlog_out, "]");
}
// HERE: No support for trigger. Is this actually needed?
static void emit_lpm_func(ivl_scope_t scope, ivl_lpm_t lpm, const char *name)
{
unsigned idx, count= ivl_lpm_size(lpm);
fprintf(vlog_out, "%s(", name);;
for (idx = count-1; idx > 0; idx -= 1) {
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, idx));
fprintf(vlog_out, ", ");
}
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, ")");
}
static void emit_lpm_as_ca(ivl_scope_t scope, ivl_lpm_t lpm)
{
switch (ivl_lpm_type(lpm)) {
case IVL_LPM_ADD:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " + ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_ARRAY:
emit_lpm_array(scope, lpm);
break;
case IVL_LPM_CAST_INT:
case IVL_LPM_CAST_INT2:
case IVL_LPM_CAST_REAL:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
break;
case IVL_LPM_CMP_EEQ:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " === ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_CMP_EQ:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " == ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_CMP_GE:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " >= ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_CMP_GT:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " > ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_CMP_NE:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " != ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_CMP_NEE:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " !== ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_CONCAT:
emit_lpm_concat(scope, lpm);
break;
case IVL_LPM_DIVIDE:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " / ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_MOD:
// HERE: Need to check if this LPM is IVL_VT_REAL.
if (0) {
fprintf(stderr, "%s:%u: vlog95 error: Real modulus operator "
"is not supported.\n",
ivl_lpm_file(lpm), ivl_lpm_lineno(lpm));
vlog_errors += 1;
}
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " %% ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_MULT:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " * ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_MUX:
emit_nexus_as_ca(scope, ivl_lpm_select(lpm));
fprintf(vlog_out, " ? ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
fprintf(vlog_out, " : ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
break;
case IVL_LPM_PART_PV:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
break;
case IVL_LPM_PART_VP:
emit_lpm_part_select(scope, lpm);
break;
case IVL_LPM_RE_AND:
fprintf(vlog_out, " & ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
break;
case IVL_LPM_RE_NAND:
fprintf(vlog_out, " ~& ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
break;
case IVL_LPM_RE_NOR:
fprintf(vlog_out, " ~| ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
break;
case IVL_LPM_RE_OR:
fprintf(vlog_out, " | ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
break;
case IVL_LPM_RE_XOR:
fprintf(vlog_out, " ^ ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
break;
case IVL_LPM_RE_XNOR:
fprintf(vlog_out, " ~^ ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
break;
case IVL_LPM_REPEAT:
fprintf(vlog_out, "{%u{", ivl_lpm_size(lpm));
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, "}}");
break;
case IVL_LPM_SFUNC:
emit_lpm_func(scope, lpm, ivl_lpm_string(lpm));
break;
case IVL_LPM_SHIFTL:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " << ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_SHIFTR:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " >> ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_SIGN_EXT:
// HERE: Do we need to extend here?
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
break;
case IVL_LPM_SUB:
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0));
fprintf(vlog_out, " - ");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 1));
break;
case IVL_LPM_UFUNC:
emit_lpm_func(scope, lpm, ivl_scope_name(ivl_lpm_define(lpm)));
break;
default:
fprintf(vlog_out, "<unknown>");
fprintf(stderr, "%s:%u: vlog95 error: Unknown LPM type (%d).\n",
ivl_lpm_file(lpm), ivl_lpm_lineno(lpm),
(int)ivl_lpm_type(lpm));
vlog_errors += 1;
}
}
static void emit_lpm(ivl_scope_t scope, ivl_lpm_t lpm)
{
ivl_nexus_t output = get_lpm_output(scope, lpm);
/* If the output is local then someone else will output this lpm. */
if (! output) return;
fprintf(vlog_out, "%*cassign", indent, ' ');
emit_lpm_strength(lpm);
emit_delay(scope,
ivl_lpm_delay(lpm, 0),
ivl_lpm_delay(lpm, 1),
ivl_lpm_delay(lpm, 2),
3);
fprintf(vlog_out, " ");
emit_name_of_nexus(scope, output);
fprintf(vlog_out, " = ");
emit_lpm_as_ca(scope, lpm);
fprintf(vlog_out, ";\n");
}
/*
* A BUFZ is a simple variable assignment possible with strength and/or delay.
*/
static void emit_bufz(ivl_scope_t scope, ivl_net_logic_t nlogic)
{
assert(ivl_logic_pins(nlogic) == 2);
fprintf(vlog_out, "assign");
emit_gate_strength(nlogic);
emit_delay(scope,
ivl_logic_delay(nlogic, 0),
ivl_logic_delay(nlogic, 1),
ivl_logic_delay(nlogic, 2),
3);
fprintf(vlog_out, " ");
emit_name_of_nexus(scope, ivl_logic_pin(nlogic, 0));
fprintf(vlog_out, " = ");
emit_name_of_nexus(scope, ivl_logic_pin(nlogic, 1));
fprintf(vlog_out, ";\n");
}
static void emit_logic(ivl_scope_t scope, ivl_net_logic_t nlogic)
{
unsigned idx, count, dly_count;
@ -305,6 +776,10 @@ static void emit_logic(ivl_scope_t scope, ivl_net_logic_t nlogic)
fprintf(vlog_out, "bufif1");
dly_count = 3;
break;
// case IVL_LO_BUFT:
case IVL_LO_BUFZ:
emit_bufz(scope, nlogic);
return;
case IVL_LO_CMOS:
fprintf(vlog_out, "cmos");
dly_count = 3;
@ -370,7 +845,7 @@ static void emit_logic(ivl_scope_t scope, ivl_net_logic_t nlogic)
dly_count = 2;
break;
default:
// HERE: Missing support for BUFT, BUFZ, UDP.
// HERE: Missing support for UDP.
fprintf(vlog_out, "<unknown>(");
fprintf(stderr, "%s:%u: vlog95 error: Unsupported logic type "
"(%d) named: %s.\n", ivl_logic_file(nlogic),
@ -381,10 +856,15 @@ static void emit_logic(ivl_scope_t scope, ivl_net_logic_t nlogic)
break;
}
emit_gate_strength(nlogic);
if (dly_count) emit_gate_delay(scope, nlogic, dly_count);
if (dly_count) emit_delay(scope,
ivl_logic_delay(nlogic, 0),
ivl_logic_delay(nlogic, 1),
ivl_logic_delay(nlogic, 2),
dly_count);
// HERE: The name has the location information encoded in it. We need to
// remove this and rebuild the instance array.
fprintf(vlog_out, " %s(", ivl_logic_basename(nlogic));
// remove this and rebuild the instance array. For now skip the name.
// fprintf(vlog_out, " %s(", ivl_logic_basename(nlogic));
fprintf(vlog_out, " (");
count = ivl_logic_pins(nlogic);
count -= 1;
for (idx = 0; idx < count; idx += 1) {
@ -405,6 +885,52 @@ static int find_process(ivl_process_t proc, ivl_scope_t scope)
return 0;
}
void emit_scope_variables(ivl_scope_t scope)
{
unsigned idx, count;
/* Output the parameters for this scope. */
count = ivl_scope_params(scope);
for (idx = 0; idx < count; idx += 1) {
ivl_parameter_t par = ivl_scope_param(scope, idx);
ivl_expr_t pex = ivl_parameter_expr(par);
fprintf(vlog_out, "%*cparameter %s = ", indent, ' ',
ivl_parameter_basename(par));
emit_expr(scope, pex, 0);
fprintf(vlog_out, ";\n");
}
if (count) fprintf(vlog_out, "\n");
/* Output the signals for this scope. */
count = ivl_scope_sigs(scope);
for (idx = 0; idx < count; idx += 1) {
ivl_signal_t sig = ivl_scope_sig(scope, idx);
if (ivl_signal_type(sig) == IVL_SIT_REG) {
/* Do not output the implicit function return register. */
if (ivl_scope_type(scope) == IVL_SCT_FUNCTION &&
strcmp(ivl_signal_basename(sig),
ivl_scope_tname(scope)) == 0) continue;
emit_var_def(sig);
} else {
emit_net_def(sig);
}
}
if (count) fprintf(vlog_out, "\n");
/* Output the named events for this scope. */
count = ivl_scope_events(scope);
for (idx = 0; idx < count; idx += 1) {
ivl_event_t event = ivl_scope_event(scope, idx);
/* If this event has any type of edge sensitivity then it is
* not a named event. */
if (ivl_event_nany(event)) continue;
if (ivl_event_npos(event)) continue;
if (ivl_event_nneg(event)) continue;
fprintf(vlog_out, "%*cevent %s;\n", indent, ' ',
ivl_event_basename(event));
}
if (count) fprintf(vlog_out, "\n");
}
/*
* This search method may be slow for a large structural design with a
* large number of gate types. That's not what this converter was built
@ -531,7 +1057,7 @@ int emit_scope(ivl_scope_t scope, ivl_scope_t parent)
fprintf(vlog_out, ";\n");
indent += indent_incr;
/* Output the scope ports. */
/* Output the scope ports for this scope. */
count = ivl_scope_ports(scope);
for (idx = start; idx < count; idx += 1) {
fprintf(vlog_out, "%*c", indent, ' ');
@ -560,36 +1086,15 @@ int emit_scope(ivl_scope_t scope, ivl_scope_t parent)
}
if (count) fprintf(vlog_out, "\n");
/* Output the scope parameters. */
count = ivl_scope_params(scope);
for (idx = 0; idx < count; idx += 1) {
ivl_parameter_t par = ivl_scope_param(scope, idx);
ivl_expr_t pex = ivl_parameter_expr(par);
fprintf(vlog_out, "%*cparameter %s = ", indent, ' ',
ivl_parameter_basename(par));
emit_expr(scope, pex, 0);
fprintf(vlog_out, ";\n");
}
if (count) fprintf(vlog_out, "\n");
/* Output the scope signals. */
count = ivl_scope_sigs(scope);
for (idx = 0; idx < count; idx += 1) {
ivl_signal_t sig = ivl_scope_sig(scope, idx);
if (ivl_signal_type(sig) == IVL_SIT_REG) {
/* Do not output the implicit function return register. */
if (sc_type == IVL_SCT_FUNCTION &&
strcmp(ivl_signal_basename(sig),
ivl_scope_tname(scope)) == 0) continue;
emit_var_def(sig);
} else {
emit_net_def(sig);
}
}
// HERE: Need to find and print any continuous assignments.
emit_scope_variables(scope);
if (sc_type == IVL_SCT_MODULE) {
/* Output the LPM devices. */
count = ivl_scope_lpms(scope);
for (idx = 0; idx < count; idx += 1) {
emit_lpm(scope, ivl_scope_lpm(scope, idx));
}
/* Output any logic devices. */
count = ivl_scope_logs(scope);
for (idx = 0; idx < count; idx += 1) {

View File

@ -35,7 +35,9 @@ static unsigned get_indent()
static void emit_stmt_block_body(ivl_scope_t scope, ivl_statement_t stmt)
{
unsigned idx, count = ivl_stmt_block_count(stmt);
ivl_scope_t my_scope = ivl_stmt_block_scope(stmt);
indent += indent_incr;
if (my_scope) emit_scope_variables(my_scope);
for (idx = 0; idx < count; idx += 1) {
emit_stmt(scope, ivl_stmt_block_stmt(stmt, idx));
}
@ -137,6 +139,81 @@ static unsigned emit_stmt_lval(ivl_scope_t scope, ivl_statement_t stmt)
return wid;
}
/*
* Icarus translated <var> = <event> <value> into
* begin
* <tmp> = <value>;
* <event>;
* <var> = <tmp>;
* end
* This routine looks for this pattern and turns it back into the
* appropriate blocking assignment.
*/
static unsigned find_event_assign(ivl_scope_t scope, ivl_statement_t stmt)
{
unsigned wid;
ivl_statement_t assign, event, event_assign, repeat = 0;
ivl_lval_t lval;
ivl_expr_t rval;
ivl_signal_t lsig, rsig;
/* We must have three block elements. */
if (ivl_stmt_block_count(stmt) != 3) return 0;
/* The first must be an assign. */
assign = ivl_stmt_block_stmt(stmt, 0);
if (ivl_statement_type(assign) != IVL_ST_ASSIGN) return 0;
/* The second must be a repeat with an event or an event. */
event = ivl_stmt_block_stmt(stmt, 1);
if (ivl_statement_type(event) != IVL_ST_REPEAT &&
ivl_statement_type(event) != IVL_ST_WAIT) return 0;
if (ivl_statement_type(event) == IVL_ST_REPEAT) {
repeat = event;
event = ivl_stmt_sub_stmt(repeat);
if (ivl_statement_type(event) != IVL_ST_WAIT) return 0;
}
/* The third must be an assign. */
event_assign = ivl_stmt_block_stmt(stmt, 2);
if (ivl_statement_type(event_assign) != IVL_ST_ASSIGN) return 0;
/* The L-value must be a single signal. */
if (ivl_stmt_lvals(assign) != 1) return 0;
lval = ivl_stmt_lval(assign, 0);
/* It must not have an array select. */
if (ivl_lval_idx(lval)) return 0;
/* It must not have a non-zero base. */
if (ivl_lval_part_off(lval)) return 0;
lsig = ivl_lval_sig(lval);
/* It must not be part of the signal. */
if (ivl_lval_width(lval) != ivl_signal_width(lsig)) return 0;
/* The R-value must be a single signal. */
rval = ivl_stmt_rval(event_assign);
if (ivl_expr_type(rval) != IVL_EX_SIGNAL) return 0;
/* It must not be an array word. */
if (ivl_expr_oper1(rval)) return 0;
rsig = ivl_expr_signal(rval);
/* And finally the two signals must be the same. */
if (lsig != rsig) return 0;
/* The pattern matched so generate the appropriate code. */
fprintf(vlog_out, "%*c", get_indent(), ' ');
// HERE: Do we need to calculate the width? The compiler should have already
// done this for us.
wid = emit_stmt_lval(scope, event_assign);
fprintf(vlog_out, " =");
single_indent = 1;
if (repeat) {
fprintf(vlog_out, " repeat (");
emit_expr(scope, ivl_stmt_cond_expr(repeat), 0);
fprintf(vlog_out, ")");
}
fprintf(vlog_out, " @(");
emit_event(scope, event);
fprintf(vlog_out, ") ");
emit_expr(scope, ivl_stmt_rval(assign), wid);
fprintf(vlog_out, ";\n");
return 1;
}
/*
* Icarus translated <var> = <delay> <value> into
* begin
@ -506,7 +583,7 @@ static void emit_stmt_condit(ivl_scope_t scope, ivl_statement_t stmt)
* condition that does not have a false clause then we need
* to add a begin/end pair to keep the else clause attached
* to this condition. */
if (false_stmt &&
if (false_stmt &&
(ivl_statement_type(true_stmt) == IVL_ST_CONDIT) &&
(! ivl_stmt_cond_false(true_stmt))) nest = 1;
if (nest) {
@ -697,6 +774,7 @@ void emit_stmt(ivl_scope_t scope, ivl_statement_t stmt)
emit_stmt_block_named(scope, stmt);
} else {
if (find_delayed_assign(scope, stmt)) break;
if (find_event_assign(scope, stmt)) break;
emit_stmt_block(scope, stmt);
}
break;

View File

@ -66,8 +66,14 @@ extern void emit_scaled_expr(ivl_scope_t scope, ivl_expr_t expr,
extern void emit_scaled_range(ivl_scope_t scope, ivl_expr_t expr,
unsigned width, int msb, int lsb);
extern void emit_scope_path(ivl_scope_t scope, ivl_scope_t call_scope);
extern void emit_scope_variables(ivl_scope_t scope);
extern void emit_scope_module_path(ivl_scope_t scope, ivl_scope_t call_scope);
extern void emit_name_of_nexus(ivl_scope_t scope, ivl_nexus_t nex);
extern void emit_const_nexus(ivl_scope_t scope, ivl_net_const_t const_net);
extern void emit_real_number(double value);
extern void emit_number(const char *bits, unsigned nbits, unsigned is_signed,
const char *file, unsigned lineno);
/*
* Find the enclosing module scope.