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

Subversion Repositories uart2bus_testbench

[/] [uart2bus_testbench/] [trunk/] [tb/] [uvm_src/] [dpi/] [uvm_hdl_vcs.c] - Blame information for rev 16

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 HanySalah
//----------------------------------------------------------------------
2
//   Copyright 2007-2011 Cadence Design Systems, Inc.
3
//   Copyright 2009-2010 Mentor Graphics Corporation
4
//   Copyright 2010-2011 Synopsys, Inc.
5
//   All Rights Reserved Worldwide
6
//
7
//   Licensed under the Apache License, Version 2.0 (the
8
//   "License"); you may not use this file except in
9
//   compliance with the License.  You may obtain a copy of
10
//   the License at
11
//
12
//       http://www.apache.org/licenses/LICENSE-2.0
13
//
14
//   Unless required by applicable law or agreed to in
15
//   writing, software distributed under the License is
16
//   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17
//   CONDITIONS OF ANY KIND, either express or implied.  See
18
//   the License for the specific language governing
19
//   permissions and limitations under the License.
20
//----------------------------------------------------------------------
21
 
22
#include "uvm_dpi.h"
23
#include <math.h>
24
 
25
#include "svdpi.h"
26
#include "vcsuser.h"
27
 
28
#ifdef VCSMX
29
#include "mhpi_user.h"
30
#include "vhpi_user.h"
31
#endif
32
 
33
 
34
/*
35
 * UVM HDL access C code.
36
 *
37
 */
38
 
39
/*
40
 * This C code checks to see if there is PLI handle
41
 * with a value set to define the maximum bit width.
42
 *
43
 * If no such variable is found, then the default
44
 * width of 1024 is used.
45
 *
46
 * This function should only get called once or twice,
47
 * its return value is cached in the caller.
48
 *
49
 */
50
static int uvm_hdl_max_width()
51
{
52
  vpiHandle ms;
53
  s_vpi_value value_s = { vpiIntVal, { 0 } };
54
  ms = vpi_handle_by_name((PLI_BYTE8*) "uvm_pkg::UVM_HDL_MAX_WIDTH", 0);
55
  if(ms == 0)
56
    return 1024;  /* If nothing else is defined,
57
                     this is the DEFAULT */
58
  vpi_get_value(ms, &value_s);
59
  return value_s.value.integer;
60
}
61
 
62
 
63
/*
64
 * Given a path, look the path name up using the PLI,
65
 * and set it to 'value'.
66
 */
67
static int uvm_hdl_set_vlog(char *path, p_vpi_vecval value, PLI_INT32 flag)
68
{
69
  static int maxsize = -1;
70
  vpiHandle r;
71
  s_vpi_value value_s = { vpiIntVal, { 0 } };
72
  s_vpi_time  time_s = { vpiSimTime, 0, 0, 0.0 };
73
 
74
  //vpi_printf("uvm_hdl_set_vlog(%s,%0x)\n",path,value[0].aval);
75
 
76
  r = vpi_handle_by_name(path, 0);
77
 
78
  if(r == 0)
79
  {
80
      const char * err_str = "set: unable to locate hdl path (%s)\n Either the name is incorrect, or you may not have PLI/ACC visibility to that name";
81
      char buffer[strlen(err_str) + strlen(path)];
82
      sprintf(buffer, err_str, path);
83
      m_uvm_report_dpi(M_UVM_ERROR,
84
                       (char*) "UVM/DPI/HDL_SET",
85
                       &buffer[0],
86
                       M_UVM_NONE,
87
                       (char*)__FILE__,
88
                       __LINE__);
89
    return 0;
90
  }
91
  else
92
  {
93
    if(maxsize == -1)
94
        maxsize = uvm_hdl_max_width();
95
 
96
    if (flag == vpiReleaseFlag) {
97
      //size = vpi_get(vpiSize, r);
98
      //value_p = (p_vpi_vecval)(malloc(((size-1)/32+1)*8*sizeof(s_vpi_vecval)));
99
      //value = &value_p;
100
    }
101
    value_s.format = vpiVectorVal;
102
    value_s.value.vector = value;
103
    vpi_put_value(r, &value_s, &time_s, flag);
104
    //if (value_p != NULL)
105
    //  free(value_p);
106
    if (value == NULL) {
107
      value = value_s.value.vector;
108
    }
109
  }
110
  return 1;
111
}
112
 
113
 
114
/*
115
 * Given a path, look the path name up using the PLI
116
 * and return its 'value'.
117
 */
118
static int uvm_hdl_get_vlog(char *path, p_vpi_vecval value, PLI_INT32 flag)
119
{
120
  static int maxsize = -1;
121
  int i, size, chunks;
122
  vpiHandle r;
123
  s_vpi_value value_s;
124
 
125
  r = vpi_handle_by_name(path, 0);
126
 
127
  if(r == 0)
128
  {
129
      const char * err_str = "get: unable to locate hdl path (%s)\n Either the name is incorrect, or you may not have PLI/ACC visibility to that name";
130
      char buffer[strlen(err_str) + strlen(path)];
131
      sprintf(buffer, err_str, path);
132
      m_uvm_report_dpi(M_UVM_ERROR,
133
                  (char*)"UVM/DPI/HDL_GET",
134
                       &buffer[0],
135
                       M_UVM_NONE,
136
                       (char*)__FILE__,
137
                       __LINE__);
138
    // Exiting is too harsh. Just return instead.
139
    // tf_dofinish();
140
    return 0;
141
  }
142
  else
143
  {
144
    if(maxsize == -1)
145
        maxsize = uvm_hdl_max_width();
146
 
147
    size = vpi_get(vpiSize, r);
148
    if(size > maxsize)
149
    {
150
      const char * err_str = "uvm_reg : hdl path '%s' is %0d bits, but the maximum size is %0d.  You can increase the maximum via a compile-time flag: +define+UVM_HDL_MAX_WIDTH=<value>";
151
      char buffer[strlen(err_str) + strlen(path) + (2*int_str_max(10))];
152
      sprintf(buffer, err_str, path, size, maxsize);
153
      m_uvm_report_dpi(M_UVM_ERROR,
154
                  (char*)"UVM/DPI/HDL_SET",
155
                       &buffer[0],
156
                       M_UVM_NONE,
157
                       (char*)__FILE__,
158
                       __LINE__);
159
      return 0;
160
    }
161
    chunks = (size-1)/32 + 1;
162
 
163
    value_s.format = vpiVectorVal;
164
    vpi_get_value(r, &value_s);
165
    /*dpi and vpi are reversed*/
166
    for(i=0;i<chunks; ++i)
167
    {
168
      value[i].aval = value_s.value.vector[i].aval;
169
      value[i].bval = value_s.value.vector[i].bval;
170
    }
171
  }
172
  //vpi_printf("uvm_hdl_get_vlog(%s,%0x)\n",path,value[0].aval);
173
  return 1;
174
}
175
 
176
 
177
/*
178
 * Given a path, look the path name up using the PLI,
179
 * but don't set or get. Just check.
180
 *
181
 * Return 0 if NOT found.
182
 * Return 1 if found.
183
 */
184
int uvm_hdl_check_path(char *path)
185
{
186
  vpiHandle r;
187
 
188
  r = vpi_handle_by_name(path, 0);
189
 
190
  if(r == 0)
191
      return 0;
192
  else
193
    return 1;
194
}
195
 
196
/*
197
 * convert binary to integer
198
 */
199
long int uvm_hdl_btoi(char *binVal) {
200
  long int remainder, dec=0, j = 0;
201
  unsigned long long int bin;
202
  int i;
203
  char tmp[2];
204
  tmp[1] = '\0';
205
 
206
  for(i= strlen(binVal) -1 ; i >= 0 ; i--) {
207
    tmp[0] = binVal[i];
208
    bin = atoi(tmp);
209
    dec = dec+(bin*(pow(2,j)));
210
    j++;
211
  }
212
  return(dec);
213
}
214
 
215
 
216
/*
217
 *decimal to hex conversion
218
 */
219
char *uvm_hdl_dtob(long int decimalNumber) {
220
   int remainder, quotient;
221
  int  i=0,j, length;
222
  int binN[65];
223
  static char binaryNumber[65];
224
  char *str = (char*) malloc(sizeof(char));
225
 
226
  quotient = decimalNumber;
227
 
228
  do {
229
    binN[i++] = quotient%2;
230
    quotient = quotient/2;
231
  } while (quotient!=0);
232
  length = i;
233
 
234
  for (i=length-1, j = 0; i>=0; i--) {
235
    binaryNumber[j++] = binN[i]?'1':'0';
236
  }
237
  binaryNumber[j] = '\0';
238
  return(binaryNumber);
239
}
240
 
241
 
242
/*
243
 * Mixed lanaguage API Get calls
244
 */
245
#ifdef VCSMX
246
int uvm_hdl_get_mhdl(char *path, p_vpi_vecval value) {
247
 
248
  long int value_int;
249
 
250
  char *binVal;
251
  int i = 0;
252
  vhpiValueT value1;
253
  p_vpi_vecval vecval;
254
  mhpi_initialize('/');
255
  mhpiHandleT mhpiH = mhpi_handle_by_name(path, 0);
256
  vhpiHandleT vhpiH = (long unsigned int *)mhpi_get_vhpi_handle(mhpiH);
257
  value1.format=vhpiStrVal;
258
  value1.bufSize = vhpi_get(vhpiSizeP, vhpiH);
259
  value1.value.str = (char*)malloc(value1.bufSize*sizeof(char)+1);
260
 
261
 
262
  if (vhpi_get_value(vhpiH, &value1) == 0) {
263
    binVal = value1.value.str;
264
 
265
    value_int = uvm_hdl_btoi(binVal);
266
    value->aval = (PLI_UINT32) value_int;
267
    value->bval = 0;
268
    mhpi_release_parent_handle(mhpiH);
269
    free(value1.value.str);
270
    return(1);
271
 
272
 
273
  } else {
274
    mhpi_release_parent_handle(mhpiH);
275
    free(value1.value.str);
276
    return (0);
277
  }
278
 
279
}
280
#endif
281
 
282
/*
283
 * Given a path, look the path name up using the PLI
284
 * or the VHPI, and return its 'value'.
285
 */
286
int uvm_hdl_read(char *path, p_vpi_vecval value)
287
{
288
#ifndef VCSMX
289
     return uvm_hdl_get_vlog(path, value, vpiNoDelay);
290
#else
291
    mhpi_initialize('/');
292
    mhpiHandleT h = mhpi_handle_by_name(path, 0);
293
    if (mhpi_get(mhpiPliP, h) == mhpiVpiPli) {
294
    mhpi_release_parent_handle(h);
295
      return uvm_hdl_get_vlog(path, value, vpiNoDelay);
296
    }
297
    else if (mhpi_get(mhpiPliP, h) == mhpiVhpiPli) {
298
 
299
    mhpi_release_parent_handle(h);
300
      return uvm_hdl_get_mhdl(path,value);
301
    }
302
#endif
303
}
304
 
305
 
306
/*
307
 * Mixed Language API Set calls
308
 */
309
#ifdef VCSMX
310
int uvm_hdl_set_mhdl(char *path, p_vpi_vecval value, mhpiPutValueFlagsT flags)
311
{
312
    mhpi_initialize('/');
313
    mhpiRealT forceDelay = 0;
314
    mhpiRealT cancelDelay = -1;
315
    mhpiReturnT ret;
316
    mhpiHandleT h = mhpi_handle_by_name(path, 0);
317
    mhpiHandleT mhpi_mhRegion = mhpi_handle(mhpiScope, h);
318
    int val = value->aval;
319
    char *force_value = uvm_hdl_dtob(val);
320
 
321
    ret = mhpi_force_value(path, mhpi_mhRegion, force_value, flags, forceDelay, cancelDelay);
322
    mhpi_release_parent_handle(h);
323
    if (ret == mhpiRetOk) {
324
      return(1);
325
    }
326
    else
327
      return(0);
328
}
329
#endif
330
 
331
/*
332
 * Given a path, look the path name up using the PLI
333
 * or the VHPI, and set it to 'value'.
334
 */
335
int uvm_hdl_deposit(char *path, p_vpi_vecval value)
336
{
337
#ifndef VCSMX
338
     return uvm_hdl_set_vlog(path, value, vpiNoDelay);
339
#else
340
    mhpi_initialize('/');
341
    mhpiHandleT h = mhpi_handle_by_name(path, 0);
342
 
343
    if (mhpi_get(mhpiPliP, h) == mhpiVpiPli) {
344
    mhpi_release_parent_handle(h);
345
      return uvm_hdl_set_vlog(path, value, vpiNoDelay);
346
    }
347
    else if (mhpi_get(mhpiPliP, h) == mhpiVhpiPli) {
348
      mhpi_release_parent_handle(h);
349
      return uvm_hdl_set_mhdl(path, value, mhpiNoDelay);
350
     }
351
    else
352
      return (0);
353
#endif
354
}
355
 
356
 
357
/*
358
 * Given a path, look the path name up using the PLI
359
 * or the VHPI, and set it to 'value'.
360
 */
361
int uvm_hdl_force(char *path, p_vpi_vecval value)
362
{
363
#ifndef VCSMX
364
      return uvm_hdl_set_vlog(path, value, vpiForceFlag);
365
#else
366
    mhpi_initialize('/');
367
    mhpiHandleT h = mhpi_handle_by_name(path, 0);
368
 
369
    if (mhpi_get(mhpiPliP, h) == mhpiVpiPli) {
370
    mhpi_release_parent_handle(h);
371
      return uvm_hdl_set_vlog(path, value, vpiForceFlag);
372
    }
373
    else if (mhpi_get(mhpiPliP, h) == mhpiVhpiPli) {
374
 
375
    mhpi_release_parent_handle(h);
376
      return uvm_hdl_set_mhdl(path, value, mhpiForce);
377
 
378
      }
379
    else
380
      return (0);
381
#endif
382
}
383
 
384
 
385
/*
386
 * Given a path, look the path name up using the PLI
387
 * or the VHPI, and release it.
388
 */
389
int uvm_hdl_release_and_read(char *path, p_vpi_vecval value)
390
{
391
    return uvm_hdl_set_vlog(path, value, vpiReleaseFlag);
392
}
393
 
394
/*
395
 * Given a path, look the path name up using the PLI
396
 * or the VHPI, and release it.
397
 */
398
int uvm_hdl_release(char *path)
399
{
400
    s_vpi_vecval value;
401
    p_vpi_vecval valuep = &value;
402
#ifndef VCSMX
403
     return uvm_hdl_set_vlog(path, valuep, vpiReleaseFlag);
404
#else
405
    mhpi_initialize('/');
406
    mhpiHandleT h = mhpi_handle_by_name(path, 0);
407
    mhpiReturnT ret;
408
 
409
    if (mhpi_get(mhpiPliP, h) == mhpiVpiPli) {
410
      return uvm_hdl_set_vlog(path, valuep, vpiReleaseFlag);
411
    }
412
    else if (mhpi_get(mhpiPliP, h) == mhpiVhpiPli) {
413
      mhpiHandleT mhpi_mhRegion = mhpi_handle(mhpiScope, h);
414
      ret = mhpi_release_force(path, mhpi_mhRegion);
415
      if (ret == mhpiRetOk) {
416
        return(1);
417
      }
418
      else
419
        return(0);
420
 
421
      }
422
    else
423
      return (0);
424
#endif
425
}
426
 

powered by: WebSVN 2.1.0

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