2 * Copyright (c) 2003 Michael Ruff (mruff at chiaro.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 #ident "$Id: sys_convert.c,v 1.7 2007/03/14 04:05:51 steve Exp $"
23 # include "vpi_config.h"
24 # include "vpi_user.h"
29 static double bits2double(PLI_UINT32 bits
[2])
33 unsigned char bval
[sizeof(double)];
36 #ifdef WORDS_BIGENDIAN
37 conv
.bval
[7] = (bits
[0] >> 0) & 0xff;
38 conv
.bval
[6] = (bits
[0] >> 8) & 0xff;
39 conv
.bval
[5] = (bits
[0] >> 16) & 0xff;
40 conv
.bval
[4] = (bits
[0] >> 24) & 0xff;
41 conv
.bval
[3] = (bits
[1] >> 0) & 0xff;
42 conv
.bval
[2] = (bits
[1] >> 8) & 0xff;
43 conv
.bval
[1] = (bits
[1] >> 16) & 0xff;
44 conv
.bval
[0] = (bits
[1] >> 24) & 0xff;
46 conv
.bval
[0] = (bits
[0] >> 0) & 0xff;
47 conv
.bval
[1] = (bits
[0] >> 8) & 0xff;
48 conv
.bval
[2] = (bits
[0] >> 16) & 0xff;
49 conv
.bval
[3] = (bits
[0] >> 24) & 0xff;
50 conv
.bval
[4] = (bits
[1] >> 0) & 0xff;
51 conv
.bval
[5] = (bits
[1] >> 8) & 0xff;
52 conv
.bval
[6] = (bits
[1] >> 16) & 0xff;
53 conv
.bval
[7] = (bits
[1] >> 24) & 0xff;
59 static void double2bits(double real
, PLI_UINT32 bits
[2])
63 unsigned char bval
[sizeof(double)];
68 #ifdef WORDS_BIGENDIAN
69 bits
[0] = conv
.bval
[7]
72 | (conv
.bval
[4] <<24);
73 bits
[1] = conv
.bval
[3]
76 | (conv
.bval
[0] <<24);
78 bits
[0] = conv
.bval
[0]
81 | (conv
.bval
[3] <<24);
82 bits
[1] = conv
.bval
[4]
85 | (conv
.bval
[7] <<24);
89 static PLI_INT32
sizetf_32 (PLI_BYTE8
*x
) { return 32; }
90 static PLI_INT32
sizetf_64 (PLI_BYTE8
*x
) { return 64; }
92 static PLI_INT32
sys_convert_compiletf(PLI_BYTE8
*name
)
94 vpiHandle call_hand
, argv
, arg
;
97 call_hand
= vpi_handle(vpiSysTfCall
, 0);
98 argv
= vpi_iterate(vpiArgument
, call_hand
);
102 vpi_printf("ERROR: %s requires a parameter.\n",
103 vpi_get_str(vpiName
, call_hand
));
107 if (!strcmp("$bitstoreal", name
) && vpi_get(vpiSize
, arg
) != 64) {
108 vpi_printf("ERROR: %s requires 64-bit argument.\n",
109 vpi_get_str(vpiName
, call_hand
));
114 vpi_free_object(argv
);
119 static PLI_INT32
sys_bitstoreal_calltf(PLI_BYTE8
*user
)
121 vpiHandle sys
, argv
, arg
;
126 /* find argument handle */
127 sys
= vpi_handle(vpiSysTfCall
, 0);
128 argv
= vpi_iterate(vpiArgument
, sys
);
129 arg
= vpi_scan(argv
);
132 value
.format
= vpiVectorVal
;
133 vpi_get_value(arg
, &value
);
136 bits
[0] = (value
.value
.vector
[0]).aval
;
137 bits
[1] = (value
.value
.vector
[1]).aval
;
138 value
.value
.real
= bits2double(bits
);
139 value
.format
= vpiRealVal
;
141 /* return converted value */
142 vpi_put_value(sys
, &value
, 0, vpiNoDelay
);
145 vpi_free_object(argv
);
150 static PLI_INT32
sys_itor_calltf(PLI_BYTE8
*user
)
152 vpiHandle sys
, argv
, arg
;
155 /* find argument handle */
156 sys
= vpi_handle(vpiSysTfCall
, 0);
157 argv
= vpi_iterate(vpiArgument
, sys
);
158 arg
= vpi_scan(argv
);
161 value
.format
= vpiIntVal
;
162 vpi_get_value(arg
, &value
);
165 value
.value
.real
= (PLI_INT32
)value
.value
.integer
;
166 value
.format
= vpiRealVal
;
168 /* return converted value */
169 vpi_put_value(sys
, &value
, 0, vpiNoDelay
);
172 vpi_free_object(argv
);
177 static PLI_INT32
sys_realtobits_calltf(PLI_BYTE8
*user
)
179 vpiHandle sys
, argv
, arg
;
181 static struct t_vpi_vecval res
[2];
185 /* find argument handle */
186 sys
= vpi_handle(vpiSysTfCall
, 0);
187 argv
= vpi_iterate(vpiArgument
, sys
);
188 arg
= vpi_scan(argv
);
191 value
.format
= vpiRealVal
;
192 vpi_get_value(arg
, &value
);
195 double2bits(value
.value
.real
, bits
);
197 res
[0].aval
= bits
[0];
199 res
[1].aval
= bits
[1];
202 value
.format
= vpiVectorVal
;
203 value
.value
.vector
= res
;
205 /* return converted value */
206 vpi_put_value(sys
, &value
, 0, vpiNoDelay
);
209 vpi_free_object(argv
);
214 static PLI_INT32
sys_rtoi_calltf(PLI_BYTE8
*user
)
216 vpiHandle sys
, argv
, arg
;
218 static struct t_vpi_vecval res
;
220 /* find argument handle */
221 sys
= vpi_handle(vpiSysTfCall
, 0);
222 argv
= vpi_iterate(vpiArgument
, sys
);
223 arg
= vpi_scan(argv
);
226 value
.format
= vpiRealVal
;
227 vpi_get_value(arg
, &value
);
230 res
.aval
= (unsigned)value
.value
.real
;
233 value
.format
= vpiVectorVal
;
234 value
.value
.vector
= &res
;
236 /* return converted value */
237 vpi_put_value(sys
, &value
, 0, vpiNoDelay
);
240 vpi_free_object(argv
);
245 void sys_convert_register()
247 s_vpi_systf_data tf_data
;
249 tf_data
.type
= vpiSysFunc
;
250 tf_data
.user_data
= "$bitstoreal";
251 tf_data
.tfname
= tf_data
.user_data
;
252 tf_data
.sizetf
= sizetf_64
;
253 tf_data
.compiletf
= sys_convert_compiletf
;
254 tf_data
.calltf
= sys_bitstoreal_calltf
;
255 vpi_register_systf(&tf_data
);
257 tf_data
.type
= vpiSysFunc
;
258 tf_data
.user_data
= "$itor";
259 tf_data
.tfname
= tf_data
.user_data
;
260 tf_data
.sizetf
= sizetf_64
;
261 tf_data
.compiletf
= sys_convert_compiletf
;
262 tf_data
.calltf
= sys_itor_calltf
;
263 vpi_register_systf(&tf_data
);
265 tf_data
.type
= vpiSysFunc
;
266 tf_data
.user_data
= "$realtobits";
267 tf_data
.tfname
= tf_data
.user_data
;
268 tf_data
.sizetf
= sizetf_64
;
269 tf_data
.compiletf
= sys_convert_compiletf
;
270 tf_data
.calltf
= sys_realtobits_calltf
;
271 vpi_register_systf(&tf_data
);
273 tf_data
.type
= vpiSysFunc
;
274 tf_data
.user_data
= "$rtoi";
275 tf_data
.tfname
= tf_data
.user_data
;
276 tf_data
.sizetf
= sizetf_32
;
277 tf_data
.compiletf
= sys_convert_compiletf
;
278 tf_data
.calltf
= sys_rtoi_calltf
;
279 vpi_register_systf(&tf_data
);
283 * $Log: sys_convert.c,v $
284 * Revision 1.7 2007/03/14 04:05:51 steve
285 * VPI tasks take PLI_BYTE* by the standard.
287 * Revision 1.6 2006/10/30 22:45:37 steve
288 * Updates for Cygwin portability (pr1585922)
290 * Revision 1.5 2004/02/15 18:03:30 steve
291 * Cleanup of warnings.
293 * Revision 1.4 2004/01/21 01:22:53 steve
294 * Give the vip directory its own configure and vpi_config.h
296 * Revision 1.3 2003/03/17 21:59:54 steve
297 * Implement $itor and $bitstoreal
299 * Revision 1.2 2003/03/10 23:40:10 steve
300 * Add support for $rtoi
302 * Revision 1.1 2003/03/07 02:44:34 steve
303 * Implement $realtobits.