OpenCores
URL https://opencores.org/ocsvn/wf3d/wf3d/trunk

Subversion Repositories wf3d

[/] [wf3d/] [trunk/] [bin/] [vpi/] [vpi.c] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 specular
//=======================================================================
2
// Project Monophony
3
//   Wire-Frame 3D Graphics Accelerator IP Core
4
//
5
// File:
6
//   vpi.c
7
//
8
// Abstract:
9
//   VPI source code for Icarus Verilog
10
//
11
// Author:
12 9 specular
//   Kenji Ishimaru (info.info.wf3d@gmail.com)
13 2 specular
//
14
//======================================================================
15
//
16
// Copyright (c) 2015, Kenji Ishimaru
17
// All rights reserved.
18
//
19
// Redistribution and use in source and binary forms, with or without
20
// modification, are permitted provided that the following conditions are met:
21
//
22
//  -Redistributions of source code must retain the above copyright notice,
23
//   this list of conditions and the following disclaimer.
24
//  -Redistributions in binary form must reproduce the above copyright notice,
25
//   this list of conditions and the following disclaimer in the documentation
26
//   and/or other materials provided with the distribution.
27
//
28
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
35
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
37
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
38
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
//
40
// Revision History
41
 
42
#include <stdio.h>
43
#include <string.h>
44
 
45
#include "vpi_user.h"
46
 
47
#define TRUE 1
48
#define FALSE 0
49
 
50
/* local prototypes */
51
static int count_systf_args(vpiHandle);
52
 
53
/* global prototypes */
54
PLI_INT32 hello(void);
55
PLI_INT32 to_float32(void);
56
PLI_INT32 to_real32(void);
57
PLI_INT32 to_float22(void);
58
extern void register_my_systfs(void);
59
extern void register_my_systfs(void);
60
 
61
/*
62
 * possible pattern for register vpi_ system tasks and functions
63
 */
64
 
65
/*
66
 * this is routine to implement registered systf call back
67
 *
68
 * notice vpi_user.h requires function return int so using dummy 0
69
 */
70
PLI_INT32 hello(void)
71
{
72
 vpiHandle href, iref;
73
 
74
 href = vpi_handle(vpiSysTfCall, NULL);
75
 if (href == NULL)
76
  {
77
   vpi_printf("** ERR: $hello PLI 2.0 can not access systf call handle\n");
78
   return(0);
79
  }
80
 if ((iref = vpi_iterate(vpiArgument, href)) != NULL)
81
  {
82
   vpi_printf(
83
    "**ERR: $hello PLI 2.0 task called with %d arguments but none allowed\n",
84
    count_systf_args(iref));
85
   return(0);
86
  }
87
 vpi_printf("hello world\n");
88
 return(0);
89
}
90
 
91
 
92
PLI_INT32 to_float32(void)
93
{
94
  vpiHandle href, iref , ret_ref, in_ref;
95
  int numargs;
96
  s_vpi_value tmpval;
97
  union {
98
    float f;
99
    unsigned int u;
100
  } _uf;
101
 
102
 href = vpi_handle(vpiSysTfCall, NULL);
103
 if (href == NULL)
104
  {
105
   vpi_printf("** ERR: $to_float32 PLI 2.0 can not access systf call handle\n");
106
   return(0);
107
  }
108
 if ((iref = vpi_iterate(vpiArgument, href)) != NULL)
109
  {
110
   numargs = vpi_get(vpiSize, iref);
111
   /* vpi_printf("numargs = %d\n",numargs); */
112
   ret_ref = vpi_scan(iref);
113
 
114
   in_ref = vpi_scan(iref);
115
   tmpval.format = vpiRealVal;
116
   vpi_get_value(in_ref, &tmpval);
117
   /*
118
   vpi_printf(
119
    "$to_float32 float %f\n",tmpval.value.real);
120
   */
121
   _uf.f = tmpval.value.real;
122
   tmpval.format = vpiIntVal;
123
   tmpval.value.integer = _uf.u;
124
   vpi_put_value (ret_ref, &tmpval, NULL, vpiNoDelay);
125
   return(0);
126
  }
127
 return(0);
128
}
129
 
130
PLI_INT32 to_real32(void)
131
{
132
  vpiHandle href, iref , ret_ref, in_ref;
133
  int numargs;
134
  s_vpi_value tmpval;
135
  union {
136
    float f;
137
    unsigned int u;
138
  } _uf;
139
 
140
 href = vpi_handle(vpiSysTfCall, NULL);
141
 if (href == NULL)
142
  {
143
   vpi_printf("** ERR: $to_real32 PLI 2.0 can not access systf call handle\n");
144
   return(0);
145
  }
146
 if ((iref = vpi_iterate(vpiArgument, href)) != NULL)
147
  {
148
   numargs = vpi_get(vpiSize, iref);
149
   /* vpi_printf("numargs = %d\n",numargs); */
150
   ret_ref = vpi_scan(iref);
151
 
152
   in_ref = vpi_scan(iref);
153
   tmpval.format = vpiIntVal;
154
   vpi_get_value(in_ref, &tmpval);
155
   /*
156
   vpi_printf(
157
    "$to_float32 float %f\n",tmpval.value.real);
158
   */
159
   _uf.u = tmpval.value.integer;
160
   tmpval.format = vpiRealVal;
161
   tmpval.value.real = (double)_uf.f;
162
   vpi_put_value (ret_ref, &tmpval, NULL, vpiNoDelay);
163
   return(0);
164
  }
165
 return(0);
166
}
167
 
168
 
169
// float32 to float 22
170
//   float32 : s = 1bit, e = 8bit, m = 23bit (bias:127)
171
//   float22 : s = 1bit, e = 5bit, m = 16bit (bias:15)
172
unsigned int cnv_f32_to_f22(unsigned int a) {
173
  // extract sign
174
  unsigned int tmp_s = (a >> 31) & 1;
175
  // extract exp
176
  int tmp_e = (a >> 23) & 0xff;
177
  tmp_e -= 127;
178
  tmp_e += 15;
179
  if (tmp_e < 0) tmp_e = 0;
180
  // extract fraction
181
  unsigned int rbit = (a >> 7) & 1;// bit 7
182
  unsigned int tmp_m = (a >> 8)& 0x7fff;
183
  if (rbit) tmp_m++;
184
  if (tmp_e != 0) tmp_m |= 0x8000;
185
  return  (tmp_s << 21) | (tmp_e << 16) | tmp_m;
186
}
187
 
188
 
189
PLI_INT32 to_float22(void)
190
{
191
  vpiHandle href, iref , ret_ref, in_ref;
192
  int numargs;
193
  s_vpi_value tmpval;
194
  union {
195
    float f;
196
    unsigned int u;
197
  } _uf;
198
 
199
 href = vpi_handle(vpiSysTfCall, NULL);
200
 if (href == NULL)
201
  {
202
   vpi_printf("** ERR: $to_float22 PLI 2.0 can not access systf call handle\n");
203
   return(0);
204
  }
205
 if ((iref = vpi_iterate(vpiArgument, href)) != NULL)
206
  {
207
   numargs = vpi_get(vpiSize, iref);
208
   /* vpi_printf("numargs = %d\n",numargs); */
209
   ret_ref = vpi_scan(iref);
210
 
211
   in_ref = vpi_scan(iref);
212
   tmpval.format = vpiRealVal;
213
   vpi_get_value(in_ref, &tmpval);
214
   /*
215
   vpi_printf(
216
    "$to_float32 float %f\n",tmpval.value.real);
217
   */
218
   _uf.f = tmpval.value.real;
219
   tmpval.format = vpiIntVal;
220
   tmpval.value.integer =  cnv_f32_to_f22(_uf.u);
221
   vpi_put_value (ret_ref, &tmpval, NULL, vpiNoDelay);
222
   return(0);
223
  }
224
 return(0);
225
}
226
 
227
 
228
/*
229
 * count arguments
230
 */
231
static int count_systf_args(vpiHandle iref)
232
{
233
 int anum = 0;
234
 
235
 while (vpi_scan(iref) != NULL) anum++;
236
 return(anum);
237
}
238
 
239
/* Template functin table for added user systf tasks and functions.
240
   See file vpi_user.h for structure definition
241
   Note only vpi_register_systf and vpi_ or tf_ utility routines that
242
   do not access the simulation data base may be called from these routines
243
*/
244
 
245
/* all routines are called to register system tasks */
246
/* called just after all PLI 1.0 tf_ veriusertfs table routines are set up */
247
/* before source is read */
248
void (*vlog_startup_routines[]) () =
249
{
250
 register_my_systfs,
251
 
252
};
253
 
254
/* routine to do the systf registering - probably should go in other file */
255
/* usually only vpi_ PLI 2.0 systf registering is done here */
256
 
257
/*
258
 * register all vpi_ PLI 2.0 style user system tasks and functions
259
 */
260
void register_my_systfs(void)
261
{
262
 p_vpi_systf_data systf_data_p;
263
 
264
 /* use predefined table form - could fill systf_data_list dynamically */
265
 static s_vpi_systf_data systf_data_list[] = {
266
  { vpiSysTask, 0, "$hello", hello, NULL, NULL, NULL },
267
  { vpiSysTask, 0, "$to_float32", to_float32, NULL, NULL, NULL },
268
  { vpiSysTask, 0, "$to_real32", to_real32, NULL, NULL, NULL },
269
  { vpiSysTask, 0, "$to_float22", to_float22, NULL, NULL, NULL },
270
  { 0, 0, NULL, NULL, NULL, NULL, NULL }
271
 };
272
 
273
 systf_data_p = &(systf_data_list[0]);
274
 while (systf_data_p->type != 0) vpi_register_systf(systf_data_p++);
275
}
276
 
277
void vpi_compat_bootstrap(void)
278
{
279
 int i;
280
 
281
 for (i = 0;; i++)
282
  {
283
   if (vlog_startup_routines[i] == NULL) break;
284
   vlog_startup_routines[i]();
285
  }
286
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.