1 /****************************************************************************
2     Copyright (C) 1987-2015 by Jeffery P. Hansen
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 ****************************************************************************/
18 #ifndef __value_h
19 #define __value_h
20 
21 /*****************************************************************************
22  *
23  * Define the real type we are going to use.
24  *
25  *****************************************************************************/
26 typedef float real_t;
27 
28 /*
29   State symbol codes
30  */
31 typedef enum state_symbol_en {
32   SYM_INVALID = -1,
33 #define SYM_INVALID SYM_INVALID
34   SYM_NUL1 = 0,
35 #define SYM_NUL1 SYM_NUL1
36   SYM_ZERO = 1,
37 #define SYM_ZERO SYM_ZERO
38   SYM_ONE = 2,
39 #define SYM_ONE	SYM_ONE
40   SYM_NUL2 = 3,
41 #define SYM_NUL2 SYM_NUL2
42   SYM_FLOAT = 4,
43 #define SYM_FLOAT SYM_FLOAT
44   SYM_LOW = 5,
45 #define SYM_LOW	SYM_LOW
46   SYM_HIGH = 6,
47 #define SYM_HIGH SYM_HIGH
48   SYM_UNKNOWN = 7
49 #define SYM_UNKNOWN SYM_UNKNOWN
50 } StateSymbol;
51 
52 /*
53  * Special property flags of Value
54  */
55 typedef enum value_flags_en {
56 	SF_NONE = 0,
57 #define SF_NONE SF_NONE
58 	SF_INT = 0x1,
59 #define SF_INT SF_INT				/* Declared as an integer */
60 	SF_DEC = 0x2,
61 #define SF_DEC SF_DEC				/* Declared as sized decimal */
62 	SF_HEX = 0x4,
63 #define SF_HEX SF_HEX				/* Declared as sized hex */
64 	SF_OCT = 0x8,
65 #define SF_OCT SF_OCT				/* Declaeed as sized octal */
66 	SF_BIN = 0x10,
67 #define SF_BIN SF_BIN				/* Declared as sized binary */
68 	SF_STRING = 0x20,
69 #define SF_STRING SF_STRING			/* Declared as string */
70 	SF_REAL = 0x40,
71 #define SF_REAL	SF_REAL				/* Declared as real */
72 	SF_STICKY_MASK = 0xffff,
73 #define SF_STICKY_MASK SF_STICKY_MASK		/* These flags are sticky and are propegated */
74 	SF_NETVAL = 0x10000
75 #define SF_NETVAL SF_NETVAL			/* Value is directly associated with a net */
76 } ValueFlags;
77 
78 /*
79  * Basic word size/byte size declarations.
80  */
81 #define SSWORDSIZE		TKGATE_WORDSIZE		/* # bits in an unsigned */
82 #define SSWORDMASK		((unsigned)~0)		/* Word with all bits set */
83 #define SSREALSIZE		(8*sizeof(real_t))	/* Size of real in bits */
84 #define SSREALBYTES		(sizeof(real_t))	/* Size of real in bytes */
85 #if (SSWORDSIZE == 32)
86 #define SSBITMASK		0x1f			/* Mask to get bit in word */
87 #define SSWORDSHIFT		5			/* Shift to get word index */
88 #define SSWORDBYTES		4			/* Number of bytes per word */
89 #define SSHALFWORDMASK		0xffff			/* Mask for a half word */
90 #elif (SSWORDSIZE == 64)
91 #define SSBITMASK		0x3f			/* Mask to get bit in word */
92 #define SSWORDSHIFT		6			/* Shift to get word index */
93 #define SSWORDBYTES		8			/* Number of bytes per word */
94 #define SSHALFWORDMASK		0xffffffff		/* Mask for a half word */
95 #else
96 #error Unsupported word size.
97 #endif
98 
99 /*
100   LMASK returns a mask with the low n bits set
101   HMASK returns a mask with the high n bits set
102 
103   HMASKZ returns a mask with all but the low n bits set
104   LMASKZ returns a mask with all but the high n bits set
105  */
106 #define LMASK(n)	(((n) == 0) ? 0 : (SSWORDMASK >> (SSWORDSIZE-(n))))
107 #define HMASK(n)	(((n) == 0) ? 0 : (SSWORDMASK << (SSWORDSIZE-(n))))
108 #define LMASKZ(n)	(((n) == SSWORDSIZE) ? 0 : (SSWORDMASK >> (n)))
109 #define HMASKZ(n)	(((n) == SSWORDSIZE) ? 0 : (SSWORDMASK << (n)))
110 
111 /*
112  * Number of words needed for b bits.
113  */
114 #define SSNUMWORDS(b)	(((b)>>SSWORDSHIFT) + (((b)&SSBITMASK)!=0))
115 
116 /*
117  * Bit position (in the high words) of the high bit in a b-bit word
118  */
119 #define SSHIGHBIT(b)	(((b)-1)&SSBITMASK)
120 
121 /*****************************************************************************
122  *
123  * Wire handler function
124  *
125  *****************************************************************************/
126 typedef void wirefunc_f(Value *R,Value *A,Value *B);
127 
128 
129 /*****************************************************************************
130  *
131  * transtype_t - transition type
132  *
133  *****************************************************************************/
134 typedef enum {
135   TT_NONE,		/* No transition */
136   TT_POSEDGE,		/* Rising edge transition */
137   TT_NEGEDGE,		/* Falling edge transition */
138   TT_EDGE,		/* Generic transition */
139 } transtype_t;
140 
141 /*
142          Logic values
143          0  1  x  z  L  H
144 -------------------------
145 zero     1  0  1  0  1  0
146 one      0  1  1  0  0  1
147 flt      0  0  1  1  1  1
148 */
149 #define DEBUG_VALUE_MEMMGR 0
150 
151 struct Value_str {
152 #if DEBUG_VALUE_MEMMGR
153   int		status;		/* Status code for memory management */
154 #endif
155   ValueFlags	flags;		/* Property flags */
156   unsigned	permFlags;	/* Perminant property flags */
157   short		nbits;		/* Number of bits in state */
158   short		nalloc;		/* Number of words allocated */
159   unsigned	*zero;		/* Bit indicating zero */
160   unsigned	*one;		/* Bit indicating one */
161   unsigned	*flt;		/* Bit indicating float */
162 };
163 
164 /*****************************************************************************
165  *
166  * Freelist for values
167  *
168  *****************************************************************************/
169 struct value_fl {
170   Value			state;	/* Actual state data */
171   struct value_fl	*next;	/* Next pointer for free list */
172 };
173 
174 Value *new_Value(int nbits);
175 void delete_Value(Value*);
176 
177 void Value_init(Value *S,int nbits);
178 void Value_uninit(Value *S);
179 void Value_reinit(Value *S,int nbits);
180 
181 void Value_zero(Value *S);
182 void Value_one(Value *S);
183 void Value_lone(Value *S);
184 void Value_unknown(Value *S);
185 void Value_float(Value *S);
186 
187 void Value_print(Value *S,FILE *f);
188 void Value_copy(Value *R,Value *A);
189 transtype_t Value_copyRange(Value *R,int rl,Value *A,int ah,int al);
190 void Value_resize(Value *R,int nbits);
191 void Value_makeSameSize(Value *A,Value *B);
192 void Value_makeSameSize3(Value *A,Value *B,Value *C);
193 
194 StateSymbol Value_getBitSym(Value*,int);
195 void Value_putBitSym(Value *S,int bit,StateSymbol p);
196 
197 void Value_wire(Value *R,Value *A,Value *B);
198 void Value_wand(Value *R,Value *A,Value *B);
199 void Value_wor(Value *R,Value *A,Value *B);
200 void Value_tri0(Value *R,Value *A,Value *B);
201 void Value_tri1(Value *R,Value *A,Value *B);
202 void Value_trireg(Value *R,Value *A,Value *B);
203 
204 /******************************************************************************
205  * Return non-zero if value has only 0 and 1 bits
206  ******************************************************************************/
207 int Value_isLogic(Value *);
208 
209 /******************************************************************************
210  * Return non-zero if value is logic zero
211  ******************************************************************************/
212 int Value_isZero(Value *);
213 
214 /******************************************************************************
215  * Return non-zero if all bits are at high-impedance value
216  ******************************************************************************/
217 int Value_isFloat(Value *);
218 
219 /******************************************************************************
220  * Return non-zero if all bits have unknown value
221  ******************************************************************************/
222 int Value_isUnknown(Value *);
223 
224 /******************************************************************************
225  * Return TRUE if some bits are unknown, FALSE otherwise
226  ******************************************************************************/
227 Boolean Value_hasUnknown(Value *);
228 
229 /******************************************************************************
230  * Return TRUE if some bits are high-impedance, FALSE otherwise
231  ******************************************************************************/
232 Boolean Value_hasFloat(Value *);
233 
234 int Value_isEqual(Value *A,Value *B);
235 
236 #define Value_nbits(S) (S)->nbits
237 void Value_w_roll(Value *R,Value *I,int shift);
238 void Value_w_shift(Value *R,Value *I,int n,int in1,int in0,int inZ);
239 void Value_shift(Value *R,Value *I,int n,int in1,int in0,int inZ);
240 transtype_t Value_transitionType(Value *A,Value *B);
241 #define Value_getAllFlags(S) ((S)->flags|(S)->permFlags)
242 #define Value_getTypeFlags(S) (S)->flags
243 #define Value_isReal(S) ((S)->flags & SF_REAL)
244 void Value_normalize(Value *r);
245 
246 /*****************************************************************************
247  * Value conversion methods - other to Value
248  *****************************************************************************/
249 int Value_convert(Value *S,const char *A);
250 int Value_convertStr(Value *S,const char *s);
251 int Value_convertI(Value *S,int A);
252 int Value_convertR(Value *S,real_t A);
253 int Value_convertNaN(Value *S);
254 int Value_convertTime(Value *S,simtime_t A);
255 int Value_convertFromInt(Value*,unsigned);
256 int Value_convertHex(Value *S, const char *p,int nbits);
257 int Value_convertBits(Value *S, const char *p,int nbits);
258 int Value_convertOct(Value *S,const char *A,int nbits);
259 int Value_convertDec(Value *S,const char *A,int nbits);
260 
261 int Value_format(Value *S,const char *fmt,char *p);
262 int Value_toReal(Value*,real_t*);
263 int Value_toInt(Value*,unsigned*);
264 int Value_toTime(Value*,simtime_t*);
265 int Value_toString(Value *S,char *p);
266 int Value_getstr(Value *S,char *p);
267 int Value_getvstr(Value *S,char *p);
268 
269 #endif
270