Storing (0, 1, -1, U, X) in a variable

M

mahurshi

i'd like to store values of a signal into a variable called signal.

signal can take 0, 1, -1, U (where u = undefined, -1 = uninitialized)

is there a way i can store this without making the signal a string or
an int (in which case, i have to change the U to another number)

right now i am going with string, but i was wondering what you pros do
in such cases.

by the way, i am going to use these values to do some basic and/or/xor
type operations. (if i use string, i have to keep converting that stuff
before i do anything)

thanks.
 
V

Victor Bazarov

i'd like to store values of a signal into a variable called signal.

signal can take 0, 1, -1, U (where u = undefined, -1 = uninitialized)

is there a way i can store this without making the signal a string or
an int (in which case, i have to change the U to another number)

Do a char. Or a 'signed char'. Or a bit field of two bits. That's as
little as you can have. It will still probably take up as much as an int
in your computer memory. An array of them will probably be smaller...
right now i am going with string, but i was wondering what you pros do
in such cases.

Depends on what you need to do with it afterwards, how many of them you
need store at the same time, etc.
by the way, i am going to use these values to do some basic and/or/xor
type operations. (if i use string, i have to keep converting that stuff
before i do anything)

Same with any other type.

V
 
Z

Zara

i'd like to store values of a signal into a variable called signal.

signal can take 0, 1, -1, U (where u = undefined, -1 = uninitialized)

is there a way i can store this without making the signal a string or
an int (in which case, i have to change the U to another number)
(...)
by the way, i am going to use these values to do some basic and/or/xor
type operations. (if i use string, i have to keep converting that stuff
before i do anything)
Something like this:

struct Signal
{
enum signal_type {ZERO=0, ONE=1, UNDEF=2, UNINIT=-1} value;

Signal():value(UNINIT) {}

/* define operations */
Signal and(Signal& s) {...}
(...)
};
 
D

Dave Townsend

I'm guessing here, but this looks like the Verilog/VHDL logic
states which you can comfortably encode into a unsigned
char. Why are you using a -1 value, this is a bad choice since
you can do some very nice bit-wise operations to compute logical
expressions of these values which takes into account the "solid"
0,1 values and the undefined/unknown values U & X.

VHDL adopts the rule that the "Leftmost" value is the default (unassigned)
value for a enumeration type, so if you made U = 0, this would be convenient
to implement in C++, (especially with arrays of these babies)
Here's what I'd suggest, so that is in "Binary" you enum type would take
these values.

"U" = 00
"X" = 01
"0" = 00
"1" = 10

As you can see, this makes the logical evaluation of expressions of signals
relatively
easy, you can evaluate the expression using the "known" values ( its a bit
too much
to go into here, but you probably should evaluate the "unknown" part first
to see if
its worth evaluating the "known" part ).

Also in VHDL, an "U" value trumps other values when combined in expressions
( which means you have to do two pairs of bit wise ops on the two sets of
bit positions).


dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top