1998-11-04 00:28:49 +01:00
|
|
|
#ifndef __verinum_H
|
|
|
|
|
#define __verinum_H
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 1998 Stephen Williams (steve@icarus.com)
|
|
|
|
|
*
|
|
|
|
|
* This source code is free software; you can redistribute it
|
|
|
|
|
* and/or modify it in source code form 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
2002-08-12 03:34:58 +02:00
|
|
|
#ifdef HAVE_CVS_IDENT
|
2005-12-07 05:04:23 +01:00
|
|
|
#ident "$Id: verinum.h,v 1.28 2005/12/07 04:04:24 steve Exp $"
|
1998-11-04 00:28:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
# include <string>
|
|
|
|
|
|
2003-01-30 05:23:25 +01:00
|
|
|
# include "config.h"
|
2001-01-16 03:44:17 +01:00
|
|
|
#ifdef HAVE_IOSFWD
|
|
|
|
|
# include <iosfwd>
|
|
|
|
|
#else
|
|
|
|
|
class ostream;
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-06-14 21:13:43 +02:00
|
|
|
using namespace std;
|
|
|
|
|
|
1998-11-04 00:28:49 +01:00
|
|
|
/*
|
2003-01-30 17:23:07 +01:00
|
|
|
* Numbers in Verilog are multibit strings, where each bit has 4
|
1998-11-04 00:28:49 +01:00
|
|
|
* possible values: 0, 1, x or z. The verinum number is store in
|
|
|
|
|
* little-endian format. This means that if the long value is 2b'10,
|
|
|
|
|
* get(0) is 0 and get(1) is 1.
|
|
|
|
|
*/
|
|
|
|
|
class verinum {
|
|
|
|
|
|
|
|
|
|
public:
|
2002-06-03 06:04:24 +02:00
|
|
|
enum V { V0 = 0, V1, Vx, Vz };
|
1998-11-04 00:28:49 +01:00
|
|
|
|
|
|
|
|
verinum();
|
|
|
|
|
verinum(const string&str);
|
1999-05-13 06:02:09 +02:00
|
|
|
verinum(const V*v, unsigned nbits, bool has_len =true);
|
2001-02-07 03:46:31 +01:00
|
|
|
verinum(V, unsigned nbits =1, bool has_len =true);
|
1998-11-04 00:28:49 +01:00
|
|
|
verinum(unsigned long val, unsigned bits);
|
|
|
|
|
verinum(const verinum&);
|
1999-05-13 06:02:09 +02:00
|
|
|
|
2000-12-10 23:01:35 +01:00
|
|
|
// Create a signed number, with an unspecified number of bits.
|
|
|
|
|
explicit verinum(long val);
|
|
|
|
|
|
1999-05-13 06:02:09 +02:00
|
|
|
// Copy only the specified number of bits from the
|
|
|
|
|
// source. Also mark this number as has_len.
|
|
|
|
|
verinum(const verinum&, unsigned bits);
|
|
|
|
|
|
1998-11-04 00:28:49 +01:00
|
|
|
~verinum();
|
1999-05-13 06:02:09 +02:00
|
|
|
verinum& operator= (const verinum&);
|
1998-11-04 00:28:49 +01:00
|
|
|
|
|
|
|
|
// Number of significant bits in this number.
|
|
|
|
|
unsigned len() const { return nbits_; }
|
|
|
|
|
|
1999-05-13 06:02:09 +02:00
|
|
|
// A number "has a length" if the length was specified fixed
|
|
|
|
|
// in some way.
|
|
|
|
|
bool has_len() const { return has_len_; }
|
|
|
|
|
|
2000-01-07 04:45:49 +01:00
|
|
|
bool has_sign(bool flag) { has_sign_ = flag; return has_sign_; }
|
|
|
|
|
bool has_sign() const { return has_sign_; }
|
|
|
|
|
|
1999-05-13 06:02:09 +02:00
|
|
|
// A number is "defined" if there are no x or z bits in its value.
|
|
|
|
|
bool is_defined() const;
|
2003-04-03 06:30:00 +02:00
|
|
|
bool is_zero() const;
|
1999-05-13 06:02:09 +02:00
|
|
|
|
|
|
|
|
// A number is "a string" if its value came directly from
|
2003-01-30 17:23:07 +01:00
|
|
|
// an ASCII description instead of a number value.
|
1999-05-13 06:02:09 +02:00
|
|
|
bool is_string() const { return string_flag_; }
|
|
|
|
|
|
1999-11-06 17:00:17 +01:00
|
|
|
// Comparison for use in sorting algorithms.
|
|
|
|
|
bool is_before(const verinum&that) const;
|
|
|
|
|
|
1998-11-04 00:28:49 +01:00
|
|
|
// Individual bits can be accessed with the get and set
|
|
|
|
|
// methods.
|
|
|
|
|
V get(unsigned idx) const;
|
|
|
|
|
V set(unsigned idx, V val);
|
|
|
|
|
|
|
|
|
|
V operator[] (unsigned idx) const { return get(idx); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long as_ulong() const;
|
|
|
|
|
signed long as_long() const;
|
|
|
|
|
string as_string() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
V* bits_;
|
|
|
|
|
unsigned nbits_;
|
1999-05-13 06:02:09 +02:00
|
|
|
bool has_len_;
|
2000-01-07 04:45:49 +01:00
|
|
|
bool has_sign_;
|
1998-11-04 00:28:49 +01:00
|
|
|
|
|
|
|
|
// These are some convenience flags that help us do a better
|
|
|
|
|
// job of pretty-printing values.
|
|
|
|
|
bool string_flag_;
|
|
|
|
|
};
|
|
|
|
|
|
2003-04-14 05:40:21 +02:00
|
|
|
/* Return a verinum that is minimal. That is, it has only the length
|
|
|
|
|
needed to accurately represent the contained value, signed or not. */
|
|
|
|
|
extern verinum trim_vnum(const verinum&);
|
1998-11-04 00:28:49 +01:00
|
|
|
|
1998-11-11 01:01:51 +01:00
|
|
|
extern ostream& operator<< (ostream&, const verinum&);
|
|
|
|
|
extern ostream& operator<< (ostream&, verinum::V);
|
|
|
|
|
|
2001-01-02 04:23:40 +01:00
|
|
|
extern verinum::V operator | (verinum::V l, verinum::V r);
|
|
|
|
|
extern verinum::V operator & (verinum::V l, verinum::V r);
|
2003-10-26 05:54:56 +01:00
|
|
|
extern verinum::V operator ^ (verinum::V l, verinum::V r);
|
2001-01-02 04:23:40 +01:00
|
|
|
|
1999-10-23 01:57:53 +02:00
|
|
|
extern verinum::V operator == (const verinum&left, const verinum&right);
|
|
|
|
|
extern verinum::V operator <= (const verinum&left, const verinum&right);
|
2001-02-09 06:44:23 +01:00
|
|
|
extern verinum::V operator < (const verinum&left, const verinum&right);
|
1998-11-04 00:28:49 +01:00
|
|
|
|
2001-02-10 21:29:39 +01:00
|
|
|
inline verinum::V operator > (const verinum&left, const verinum&right)
|
|
|
|
|
{ return right < left; }
|
|
|
|
|
|
|
|
|
|
inline verinum::V operator >= (const verinum&left, const verinum&right)
|
|
|
|
|
{ return right <= left; }
|
|
|
|
|
|
2002-06-03 06:04:24 +02:00
|
|
|
inline verinum::V operator != (const verinum&left, const verinum&right)
|
|
|
|
|
{ return (left == right)? verinum::V0 : verinum::V1; }
|
|
|
|
|
|
2003-04-14 05:40:21 +02:00
|
|
|
|
|
|
|
|
/* These are arithmetic operators. These generally work to produce
|
|
|
|
|
results that do not overflow. That means the result may expand or
|
|
|
|
|
contract to hold the bits needed to hold the operation results
|
|
|
|
|
accurately. It is up to the caller to truncate or pad if a specific
|
|
|
|
|
width is expected. */
|
|
|
|
|
extern verinum operator + (const verinum&left, const verinum&right);
|
|
|
|
|
extern verinum operator - (const verinum&left, const verinum&right);
|
|
|
|
|
extern verinum operator * (const verinum&left, const verinum&right);
|
|
|
|
|
extern verinum operator / (const verinum&left, const verinum&right);
|
|
|
|
|
extern verinum operator % (const verinum&left, const verinum&right);
|
|
|
|
|
|
2004-02-17 07:52:55 +01:00
|
|
|
extern verinum operator<< (const verinum&left, unsigned shift);
|
|
|
|
|
extern verinum operator>> (const verinum&left, unsigned shift);
|
|
|
|
|
|
2005-12-07 05:04:23 +01:00
|
|
|
extern verinum concat(const verinum&left, const verinum&right);
|
|
|
|
|
|
2003-04-14 05:40:21 +02:00
|
|
|
/* Bitwise not returns the ones complement. */
|
2000-02-23 05:43:43 +01:00
|
|
|
extern verinum v_not(const verinum&left);
|
|
|
|
|
|
1998-11-04 00:28:49 +01:00
|
|
|
/*
|
|
|
|
|
* $Log: verinum.h,v $
|
2005-12-07 05:04:23 +01:00
|
|
|
* Revision 1.28 2005/12/07 04:04:24 steve
|
|
|
|
|
* Allow constant concat expressions.
|
|
|
|
|
*
|
2005-06-14 21:13:43 +02:00
|
|
|
* Revision 1.27 2005/06/14 19:13:43 steve
|
|
|
|
|
* gcc3/4 compile errors.
|
|
|
|
|
*
|
2004-02-17 07:52:55 +01:00
|
|
|
* Revision 1.26 2004/02/17 06:52:55 steve
|
|
|
|
|
* Support unsigned divide of huge numbers.
|
|
|
|
|
*
|
2003-10-26 05:54:56 +01:00
|
|
|
* Revision 1.25 2003/10/26 04:54:56 steve
|
|
|
|
|
* Support constant evaluation of binary ^ operator.
|
|
|
|
|
*
|
2003-04-14 05:40:21 +02:00
|
|
|
* Revision 1.24 2003/04/14 03:40:21 steve
|
|
|
|
|
* Make some effort to preserve bits while
|
|
|
|
|
* operating on constant values.
|
|
|
|
|
*
|
2003-04-03 06:30:00 +02:00
|
|
|
* Revision 1.23 2003/04/03 04:30:00 steve
|
|
|
|
|
* Prevent overrun comparing verinums to zero.
|
|
|
|
|
*
|
2003-01-30 17:23:07 +01:00
|
|
|
* Revision 1.22 2003/01/30 16:23:08 steve
|
|
|
|
|
* Spelling fixes.
|
|
|
|
|
*
|
2003-01-30 05:23:25 +01:00
|
|
|
* Revision 1.21 2003/01/30 04:23:25 steve
|
|
|
|
|
* include config.h to get iosfwd flags.
|
|
|
|
|
*
|
2002-08-12 03:34:58 +02:00
|
|
|
* Revision 1.20 2002/08/12 01:35:01 steve
|
|
|
|
|
* conditional ident string using autoconfig.
|
|
|
|
|
*
|
2002-06-03 06:04:24 +02:00
|
|
|
* Revision 1.19 2002/06/03 04:04:24 steve
|
|
|
|
|
* Add verinum != operator.
|
|
|
|
|
*
|
2001-02-10 21:29:39 +01:00
|
|
|
* Revision 1.18 2001/02/10 20:29:39 steve
|
|
|
|
|
* In the context of range declarations, use elab_and_eval instead
|
|
|
|
|
* of the less robust eval_const methods.
|
|
|
|
|
*
|
2001-02-09 06:44:23 +01:00
|
|
|
* Revision 1.17 2001/02/09 05:44:23 steve
|
|
|
|
|
* support evaluation of constant < in expressions.
|
|
|
|
|
*
|
2001-02-07 03:46:31 +01:00
|
|
|
* Revision 1.16 2001/02/07 02:46:31 steve
|
|
|
|
|
* Support constant evaluation of / and % (PR#124)
|
|
|
|
|
*
|
2001-01-16 03:44:17 +01:00
|
|
|
* Revision 1.15 2001/01/16 02:44:18 steve
|
|
|
|
|
* Use the iosfwd header if available.
|
|
|
|
|
*
|
2001-01-02 04:23:40 +01:00
|
|
|
* Revision 1.14 2001/01/02 03:23:40 steve
|
|
|
|
|
* Evaluate constant &, | and unary ~.
|
|
|
|
|
*
|
2000-12-10 23:01:35 +01:00
|
|
|
* Revision 1.13 2000/12/10 22:01:36 steve
|
|
|
|
|
* Support decimal constants in behavioral delays.
|
|
|
|
|
*
|
2000-09-27 20:28:37 +02:00
|
|
|
* Revision 1.12 2000/09/27 18:28:37 steve
|
|
|
|
|
* multiply in parameter expressions.
|
|
|
|
|
*
|
2000-02-23 05:43:43 +01:00
|
|
|
* Revision 1.11 2000/02/23 04:43:43 steve
|
|
|
|
|
* Some compilers do not accept the not symbol.
|
|
|
|
|
*
|
2000-02-23 03:56:53 +01:00
|
|
|
* Revision 1.10 2000/02/23 02:56:56 steve
|
|
|
|
|
* Macintosh compilers do not support ident.
|
|
|
|
|
*
|
2000-01-07 04:45:49 +01:00
|
|
|
* Revision 1.9 2000/01/07 03:45:49 steve
|
|
|
|
|
* Initial support for signed constants.
|
|
|
|
|
*
|
1999-11-06 17:00:17 +01:00
|
|
|
* Revision 1.8 1999/11/06 16:00:17 steve
|
|
|
|
|
* Put number constants into a static table.
|
|
|
|
|
*
|
1999-10-23 01:57:53 +02:00
|
|
|
* Revision 1.7 1999/10/22 23:57:53 steve
|
|
|
|
|
* do the <= in bits, not numbers.
|
|
|
|
|
*
|
1999-10-11 01:29:37 +02:00
|
|
|
* Revision 1.6 1999/10/10 23:29:37 steve
|
|
|
|
|
* Support evaluating + operator at compile time.
|
|
|
|
|
*
|
1999-05-13 06:02:09 +02:00
|
|
|
* Revision 1.5 1999/05/13 04:02:09 steve
|
|
|
|
|
* More precise handling of verinum bit lengths.
|
|
|
|
|
*
|
1998-12-20 03:05:41 +01:00
|
|
|
* Revision 1.4 1998/12/20 02:05:41 steve
|
|
|
|
|
* Function to calculate wire initial value.
|
|
|
|
|
*
|
1998-11-11 01:01:51 +01:00
|
|
|
* Revision 1.3 1998/11/11 00:01:51 steve
|
|
|
|
|
* Check net ranges in declarations.
|
|
|
|
|
*
|
1998-11-09 19:55:33 +01:00
|
|
|
* Revision 1.2 1998/11/09 18:55:35 steve
|
|
|
|
|
* Add procedural while loops,
|
|
|
|
|
* Parse procedural for loops,
|
|
|
|
|
* Add procedural wait statements,
|
|
|
|
|
* Add constant nodes,
|
|
|
|
|
* Add XNOR logic gate,
|
|
|
|
|
* Make vvm output look a bit prettier.
|
|
|
|
|
*
|
1998-11-04 00:28:49 +01:00
|
|
|
* Revision 1.1 1998/11/03 23:29:08 steve
|
|
|
|
|
* Introduce verilog to CVS.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#endif
|