diff --git a/PExpr.h b/PExpr.h index 004c619a7..e8e07f7e4 100644 --- a/PExpr.h +++ b/PExpr.h @@ -917,8 +917,10 @@ class PECallFunction : public PExpr { virtual bool has_aa_term(Design*des, NetScope*scope) const; virtual NetExpr*elaborate_expr(Design*des, NetScope*scope, - unsigned expr_wid, - unsigned flags) const; + ivl_type_t type, unsigned flags) const; + + virtual NetExpr*elaborate_expr(Design*des, NetScope*scope, + unsigned expr_wid, unsigned flags) const; virtual unsigned test_width(Design*des, NetScope*scope, width_mode_t&mode); diff --git a/elab_expr.cc b/elab_expr.cc index 1a2aebe80..367093a30 100644 --- a/elab_expr.cc +++ b/elab_expr.cc @@ -2181,6 +2181,14 @@ NetExpr* PECallFunction::elaborate_expr(Design*des, NetScope*scope, return elaborate_base_(des, scope, dscope, expr_wid, flags); } +NetExpr* PECallFunction::elaborate_expr(Design*des, NetScope*scope, + ivl_type_t type, unsigned flags) const +{ + const netdarray_t*darray = dynamic_cast(type); + assert(darray); + return elaborate_expr(des, scope, darray->element_type()->packed_width(), flags); +} + NetExpr* PECallFunction::elaborate_base_(Design*des, NetScope*scope, NetScope*dscope, unsigned expr_wid, unsigned flags) const { @@ -2262,6 +2270,9 @@ NetExpr* PECallFunction::elaborate_base_(Design*des, NetScope*scope, NetScope*ds NetEUFunc*func = new NetEUFunc(scope, dscope, eres, parms, need_const); func->set_line(*this); + if(res->darray_type()) + return func; + NetExpr*tmp = pad_to_width(func, expr_wid, *this); tmp->cast_signed(signed_flag_); diff --git a/elab_sig.cc b/elab_sig.cc index 46ba52071..01871f622 100644 --- a/elab_sig.cc +++ b/elab_sig.cc @@ -1194,7 +1194,7 @@ NetNet* PWire::elaborate_sig(Design*des, NetScope*scope) const cerr << " in scope " << scope_path(scope) << endl; } - sig = new NetNet(scope, name_, wtype, use_type); + sig = new NetNet(scope, name_, wtype, unpacked_dimensions, use_type); } else if (enum_type_t*enum_type = dynamic_cast(set_data_type_)) { list::const_iterator sample_name = enum_type->names->begin(); diff --git a/elaborate.cc b/elaborate.cc index 9a99826a2..645d846e5 100644 --- a/elaborate.cc +++ b/elaborate.cc @@ -2282,11 +2282,18 @@ NetAssign_* PAssign_::elaborate_lval(Design*des, NetScope*scope) const PExpr::width_mode_t mode = PExpr::SIZED; rval_->test_width(des, scope, mode); // Create a L-value that matches the function return type. + NetNet*tmp; netvector_t*tmp_vec = new netvector_t(rval_->expr_type(), rval_->expr_width()-1, 0, rval_->has_sign()); - NetNet*tmp = new NetNet(scope, scope->local_symbol(), - NetNet::REG, tmp_vec); + + if(rval_->expr_type() == IVL_VT_DARRAY) { + netdarray_t*darray = new netdarray_t(tmp_vec); + tmp = new NetNet(scope, scope->local_symbol(), NetNet::REG, darray); + } else { + tmp = new NetNet(scope, scope->local_symbol(), NetNet::REG, tmp_vec); + } + tmp->set_file(rval_->get_file()); tmp->set_lineno(rval_->get_lineno()); NetAssign_*lv = new NetAssign_(tmp); diff --git a/parse.y b/parse.y index dd12a9837..edd0fc47b 100644 --- a/parse.y +++ b/parse.y @@ -750,7 +750,7 @@ class_identifier perm_string name = lex_strings.make($1); class_type_t*tmp = new class_type_t(name); FILE_NAME(tmp, @1); - pform_set_typedef(name, tmp); + pform_set_typedef(name, tmp, NULL); delete[]$1; $$ = tmp; } @@ -2358,9 +2358,9 @@ block_item_decls_opt /* Type declarations are parsed here. The rule actions call pform functions that add the declaration to the current lexical scope. */ type_declaration - : K_typedef data_type IDENTIFIER ';' + : K_typedef data_type IDENTIFIER dimensions_opt ';' { perm_string name = lex_strings.make($3); - pform_set_typedef(name, $2); + pform_set_typedef(name, $2, $4); delete[]$3; } @@ -2373,7 +2373,7 @@ type_declaration yyerror(@3, "error: Typedef identifier \"%s\" is already a type name.", $3.text); } else { - pform_set_typedef(name, $2); + pform_set_typedef(name, $2, NULL); } delete[]$3.text; } @@ -2386,7 +2386,7 @@ type_declaration perm_string name = lex_strings.make($3); class_type_t*tmp = new class_type_t(name); FILE_NAME(tmp, @3); - pform_set_typedef(name, tmp); + pform_set_typedef(name, tmp, NULL); delete[]$3; } | K_typedef K_enum IDENTIFIER ';' @@ -2401,7 +2401,7 @@ type_declaration perm_string name = lex_strings.make($2); class_type_t*tmp = new class_type_t(name); FILE_NAME(tmp, @2); - pform_set_typedef(name, tmp); + pform_set_typedef(name, tmp, NULL); delete[]$2; } diff --git a/pform.cc b/pform.cc index 1e42fa0ad..b8318c792 100644 --- a/pform.cc +++ b/pform.cc @@ -593,8 +593,11 @@ PWire*pform_get_make_wire_in_scope(perm_string name, NetNet::Type net_type, NetN return cur; } -void pform_set_typedef(perm_string name, data_type_t*data_type) +void pform_set_typedef(perm_string name, data_type_t*data_type, std::list*unp_ranges) { + if(unp_ranges) + data_type = new uarray_type_t(data_type, unp_ranges); + // If we are in a lexical scope (i.e. a package or module) // then put the typedef into that scope. Otherwise, put it // into the $root scope. @@ -1939,7 +1942,6 @@ static void pform_set_net_range(list*names, pform_set_net_range(txt, net_type, range, signed_flag, dt, SR_NET, attr); } - delete names; } /* @@ -3171,7 +3173,6 @@ static void pform_set_integer_2atom(uint64_t width, bool signed_flag, list static void pform_set2_data_type(const struct vlltype&li, T*data_type, perm_string name, NetNet::Type net_type, list*attr) @@ -3247,7 +3248,6 @@ static void pform_set_enum(const struct vlltype&li, enum_type_t*enum_type, pform_set_enum(li, enum_type, txt, net_type, attr); } - delete names; } /* @@ -3256,58 +3256,68 @@ static void pform_set_enum(const struct vlltype&li, enum_type_t*enum_type, */ void pform_set_data_type(const struct vlltype&li, data_type_t*data_type, list*names, NetNet::Type net_type, list*attr) { + const std::list*unpacked_dims = NULL; + if (data_type == 0) { VLerror(li, "internal error: data_type==0."); assert(0); } + if(uarray_type_t*uarray_type = dynamic_cast (data_type)) { + unpacked_dims = uarray_type->dims.get(); + data_type = uarray_type->base_type; + } + if (atom2_type_t*atom2_type = dynamic_cast (data_type)) { pform_set_integer_2atom(atom2_type->type_code, atom2_type->signed_flag, names, net_type, attr); - return; } - if (struct_type_t*struct_type = dynamic_cast (data_type)) { + else if (struct_type_t*struct_type = dynamic_cast (data_type)) { pform_set_struct_type(struct_type, names, net_type, attr); - return; } - if (enum_type_t*enum_type = dynamic_cast (data_type)) { + else if (enum_type_t*enum_type = dynamic_cast (data_type)) { pform_set_enum(li, enum_type, names, net_type, attr); - return; } - if (vector_type_t*vec_type = dynamic_cast (data_type)) { + else if (vector_type_t*vec_type = dynamic_cast (data_type)) { if (net_type==NetNet::REG && vec_type->integer_flag) net_type=NetNet::INTEGER; pform_set_net_range(names, vec_type->pdims.get(), vec_type->signed_flag, vec_type->base_type, net_type, attr); - return; } - if (/*real_type_t*real_type =*/ dynamic_cast (data_type)) { + else if (/*real_type_t*real_type =*/ dynamic_cast (data_type)) { pform_set_net_range(names, 0, true, IVL_VT_REAL, net_type, attr); - return; } - if (class_type_t*class_type = dynamic_cast (data_type)) { + else if (class_type_t*class_type = dynamic_cast (data_type)) { pform_set_class_type(class_type, names, net_type, attr); - return; } - if (parray_type_t*array_type = dynamic_cast (data_type)) { + else if (parray_type_t*array_type = dynamic_cast (data_type)) { pform_set2_data_type(li, array_type, names, net_type, attr); - return; } - if (string_type_t*string_type = dynamic_cast (data_type)) { + else if (string_type_t*string_type = dynamic_cast (data_type)) { pform_set_string_type(string_type, names, net_type, attr); - return; + + } else { + VLerror(li, "internal error: Unexpected data_type."); + assert(0); } - VLerror(li, "internal error: Unexpected data_type."); - assert(0); + if(unpacked_dims) { + for (list::iterator cur = names->begin() + ; cur != names->end() ; ++ cur ) { + PWire*wire = pform_get_wire_in_scope(*cur); + wire->set_unpacked_idx(*unpacked_dims); + } + } + + delete names; } vector* pform_make_udp_input_ports(list*names) diff --git a/pform.h b/pform.h index 5ea113c37..db36e73b4 100644 --- a/pform.h +++ b/pform.h @@ -287,7 +287,8 @@ extern void pform_endgenerate(); */ extern PGenerate* pform_parent_generate(void); -extern void pform_set_typedef(perm_string name, data_type_t*data_type); +extern void pform_set_typedef(perm_string name, data_type_t*data_type, + std::list*unp_ranges); /* * This function makes a PECallFunction of the named function. Decide diff --git a/tgt-vvp/eval_object.c b/tgt-vvp/eval_object.c index 64308e5f5..d24a034cf 100644 --- a/tgt-vvp/eval_object.c +++ b/tgt-vvp/eval_object.c @@ -50,6 +50,7 @@ static int eval_darray_new(ivl_expr_t ex) fprintf(vvp_out, " %%new/darray %u, \"S\";\n", size_reg); break; case IVL_VT_BOOL: + case IVL_VT_LOGIC: // bool objects are vectorable, but for now only support // a single dimensions. assert(ivl_type_packed_dimensions(element_type) == 1); diff --git a/vpi/sys_darray.c b/vpi/sys_darray.c index 7038ab05e..ccb31c391 100644 --- a/vpi/sys_darray.c +++ b/vpi/sys_darray.c @@ -20,6 +20,9 @@ # include "sys_priv.h" # include +# include +# include +# include static PLI_INT32 one_darray_arg_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name) { @@ -76,6 +79,291 @@ static PLI_INT32 size_calltf(ICARUS_VPI_CONST PLI_BYTE8*name) return 0; } +static PLI_INT32 to_vec_compiletf(ICARUS_VPI_CONST PLI_BYTE8*user_data) +{ + (void) user_data; /* Parameter is not used. */ + + vpiHandle systf_handle, arg_iterator, arg_handle; + PLI_INT32 arg_type[2]; + + /* obtain a handle to the system task instance */ + systf_handle = vpi_handle(vpiSysTfCall, NULL); + if (systf_handle == NULL) { + vpi_printf("ERROR: $ivl_darray_method$to_vec failed to obtain systf handle\n"); + vpi_control(vpiFinish,0); /* abort simulation */ + return 0; + } + + /* obtain handles to system task arguments */ + arg_iterator = vpi_iterate(vpiArgument, systf_handle); + if (arg_iterator == NULL) { + vpi_printf("ERROR: $ivl_darray_method$to_vec requires 2 arguments\n"); + vpi_control(vpiFinish, 0); + return 0; + } + + /* check the type of object in system task arguments */ + arg_handle = vpi_scan(arg_iterator); + for(int i = 0; i < 2; ++i) { + arg_type[i] = vpi_get(vpiType, arg_handle); + arg_handle = vpi_scan(arg_iterator); + } + + if (arg_handle != NULL) { /* are there more arguments? */ + vpi_printf("ERROR: $ivl_darray_method$to_vec can only have 2 arguments\n"); + vpi_free_object(arg_iterator); + vpi_control(vpiFinish, 0); + return 0; + } + + if ((arg_type[0] != vpiRegArray) || + (arg_type[1] != vpiNet && arg_type[1] != vpiReg && arg_type[1] != vpiBitVar)) { + vpi_printf("ERROR: $ivl_darray_method$to_vec value arguments must be a dynamic array and a net or reg\n"); + vpi_free_object(arg_iterator); + vpi_control(vpiFinish, 0); + return 0; + } + + return 0; +} + +static PLI_INT32 to_vec_calltf(ICARUS_VPI_CONST PLI_BYTE8*name) +{ + (void)name; /* Parameter is not used. */ + const unsigned int PLI_INT32_bits = sizeof(PLI_INT32) * 8; + + vpiHandle callh = vpi_handle(vpiSysTfCall, 0); + vpiHandle argv, darr, darr_word, vec; + s_vpi_value darr_val; + s_vpi_vecval*vec_val; + + /* Fetch arguments */ + argv = vpi_iterate(vpiArgument, callh); + assert(argv); + darr = vpi_scan(argv); + assert(darr); + vec = vpi_scan(argv); + assert(vec); + vpi_free_object(argv); + + int darr_length = vpi_get(vpiSize, darr); + darr_word = vpi_handle_by_index(darr, 0); + int darr_word_bit_size = vpi_get(vpiSize, darr_word); + int darr_bit_size = darr_length * darr_word_bit_size; + + int vec_size = vpi_get(vpiSize, vec); + if(darr_length <= 0) { + vpi_printf("ERROR: Cannot cast empty dynamic array"); + vpi_control(vpiFinish, 0); + return 0; + } + + if(vec_size != darr_bit_size) { + vpi_printf("ERROR: Dynamic array and vector size do not match"); + vpi_control(vpiFinish, 0); + return 0; + } + + /* Conversion part */ + int vec_number = ceil((double)darr_bit_size / PLI_INT32_bits); + vec_val = calloc(vec_number, sizeof(s_vpi_vecval)); + + int darr_number = ceil((double)darr_word_bit_size / PLI_INT32_bits); + + darr_val.format = vpiVectorVal; + unsigned int offset = 0; + s_vpi_vecval*vec_val_ptr = vec_val; + vec_val_ptr->aval = 0; + vec_val_ptr->bval = 0; + + /* We have to reverse the order of the dynamic array, no memcpy here */ + for(int i = darr_length - 1; i >= 0; --i) { + unsigned int bits_to_copy = darr_word_bit_size; + darr_word = vpi_handle_by_index(darr, i); + vpi_get_value(darr_word, &darr_val); + assert(darr_val.value.vector); + + for(int j = 0; j < darr_number; ++j) { + PLI_INT32 aval = darr_val.value.vector->aval; + PLI_INT32 bval = darr_val.value.vector->bval; + + if(offset < PLI_INT32_bits) { + vec_val_ptr->aval |= (aval << offset); + vec_val_ptr->bval |= (bval << offset); + } + + offset += bits_to_copy > PLI_INT32_bits ? PLI_INT32_bits : bits_to_copy; + + if(offset >= PLI_INT32_bits) { + ++vec_val_ptr; + vec_val_ptr->aval = 0; + vec_val_ptr->bval = 0; + + // is the current word crossing the s_vpi_vecval boundary? + if(offset > PLI_INT32_bits) { + // this assert is to warn you, that the following + // part could not be tested at the moment of writing + // (dynamic arrays work with vectors of 8, 16, 32, 64 + // bits, so there is no chance that one of the vectors + // will cross the s_vpi_vecval boundary) + // it *may* work, but it is better to check first + assert(0); + + // copy the remainder that did not fit in the previous s_vpi_vecval + offset -= PLI_INT32_bits; + vec_val_ptr->aval |= (aval >> (darr_word_bit_size - offset)); + vec_val_ptr->bval |= (bval >> (darr_word_bit_size - offset)); + } else { + offset = 0; + } + } + + bits_to_copy -= PLI_INT32_bits; + darr_val.value.vector++; + } + } + + darr_val.format = vpiVectorVal; + darr_val.value.vector = vec_val; + vpi_put_value(vec, &darr_val, 0, vpiNoDelay); + + free(vec_val); + + return 0; +} + +static PLI_INT32 from_vec_compiletf(ICARUS_VPI_CONST PLI_BYTE8*user_data) +{ + (void) user_data; /* Parameter is not used. */ + + vpiHandle systf_handle, arg_iterator, arg_handle; + PLI_INT32 arg_type[2]; + + /* obtain a handle to the system task instance */ + systf_handle = vpi_handle(vpiSysTfCall, NULL); + if (systf_handle == NULL) { + vpi_printf("ERROR: $ivl_darray_method$from_vec failed to obtain systf handle\n"); + vpi_control(vpiFinish,0); /* abort simulation */ + return 0; + } + + /* obtain handles to system task arguments */ + arg_iterator = vpi_iterate(vpiArgument, systf_handle); + if (arg_iterator == NULL) { + vpi_printf("ERROR: $ivl_darray_method$from_vec requires 2 arguments\n"); + vpi_control(vpiFinish, 0); + return 0; + } + + /* check the type of object in system task arguments */ + arg_handle = vpi_scan(arg_iterator); + for(int i = 0; i < 2; ++i) { + arg_type[i] = vpi_get(vpiType, arg_handle); + arg_handle = vpi_scan(arg_iterator); + } + + if (arg_handle != NULL) { /* are there more arguments? */ + vpi_printf("ERROR: $ivl_darray_method$from_vec can only have 2 arguments\n"); + vpi_free_object(arg_iterator); + vpi_control(vpiFinish, 0); + return 0; + } + + if ((arg_type[1] != vpiNet && arg_type[1] != vpiReg && arg_type[1] != vpiBitVar) || + (arg_type[0] != vpiRegArray)) { + vpi_printf("ERROR: $ivl_darray_method$from_vec value arguments must be "\ + "a net or reg and a dynamic array\n"); + vpi_free_object(arg_iterator); + vpi_control(vpiFinish, 0); + return 0; + } + + return 0; +} + +static PLI_INT32 from_vec_calltf(ICARUS_VPI_CONST PLI_BYTE8*name) +{ + (void)name; /* Parameter is not used. */ + const int PLI_INT32_bits = sizeof(PLI_INT32) * 8; + + vpiHandle callh = vpi_handle(vpiSysTfCall, 0); + vpiHandle argv, darr, darr_word, vec; + s_vpi_value darr_val, vec_val; + s_vpi_vecval*vector; + + /* Fetch arguments */ + argv = vpi_iterate(vpiArgument, callh); + assert(argv); + darr = vpi_scan(argv); + assert(darr); + vec = vpi_scan(argv); + assert(vec); + vpi_free_object(argv); + + int darr_length = vpi_get(vpiSize, darr); + darr_word = vpi_handle_by_index(darr, 0); + int darr_word_bit_size = vpi_get(vpiSize, darr_word); + int darr_bit_size = darr_length * darr_word_bit_size; + + int vec_size = vpi_get(vpiSize, vec); + if(vec_size <= 0) { + vpi_printf("ERROR: Cannot cast empty vector"); + vpi_control(vpiFinish, 0); + return 0; + } + + if(vec_size != darr_bit_size) { + vpi_printf("ERROR: Dynamic array and vector size do not match"); + vpi_control(vpiFinish, 0); + return 0; + } + + /* Conversion part */ + int darr_number = ceil((double)darr_word_bit_size / PLI_INT32_bits); + vector = calloc(darr_number, sizeof(s_vpi_vecval)); + + vec_val.format = vpiVectorVal; + vpi_get_value(vec, &vec_val); + s_vpi_vecval*darr_val_ptr; + int offset = 0; // offset in bits + + /* We have to reverse the order of the dynamic array, no memcpy here */ + for(int i = darr_length - 1; i >= 0; --i) { + int bits_to_copy = darr_word_bit_size; + darr_word = vpi_handle_by_index(darr, i); + assert(darr_val.value.vector); + darr_val_ptr = vector; + + while(bits_to_copy > 0) { + int copied_bits = bits_to_copy > PLI_INT32_bits ? PLI_INT32_bits : bits_to_copy; + PLI_INT32 aval = vec_val.value.vector[offset / PLI_INT32_bits].aval; + PLI_INT32 bval = vec_val.value.vector[offset / PLI_INT32_bits].bval; + + if(offset % PLI_INT32_bits != 0) { + unsigned int remainder = offset % 32; + aval >>= remainder; + aval |= vec_val.value.vector[offset / PLI_INT32_bits + 1].aval << (PLI_INT32_bits - remainder); + bval >>= remainder; + bval |= vec_val.value.vector[offset / PLI_INT32_bits + 1].bval << (PLI_INT32_bits - remainder); + } + + offset += copied_bits; + darr_val_ptr->aval = aval; + darr_val_ptr->bval = bval; + darr_val_ptr++; + bits_to_copy -= copied_bits; + } + + darr_val.format = vpiVectorVal; + darr_val.value.vector = vector; + vpi_put_value(darr_word, &darr_val, 0, vpiNoDelay); + } + + free(vector); + + return 0; +} + void sys_darray_register(void) { s_vpi_systf_data tf_data; @@ -90,4 +378,25 @@ void sys_darray_register(void) tf_data.user_data = "$size"; res = vpi_register_systf(&tf_data); vpip_make_systf_system_defined(res); + + tf_data.type = vpiSysTask; + tf_data.sysfunctype = 0; + tf_data.tfname = "$ivl_darray_method$to_vec"; + tf_data.calltf = to_vec_calltf; + tf_data.compiletf = to_vec_compiletf; + tf_data.sizetf = 0; + tf_data.user_data = "$ivl_darray_method$to_vec"; + res = vpi_register_systf(&tf_data); + vpip_make_systf_system_defined(res); + + tf_data.type = vpiSysTask; + tf_data.sysfunctype = 0; + tf_data.tfname = "$ivl_darray_method$from_vec"; + tf_data.calltf = from_vec_calltf; + tf_data.compiletf = from_vec_compiletf; + tf_data.sizetf = 0; + tf_data.user_data = "$ivl_darray_method$from_vec"; + res = vpi_register_systf(&tf_data); + vpip_make_systf_system_defined(res); + } diff --git a/vpi/v2009.sft b/vpi/v2009.sft index 78424f454..e2a84620b 100644 --- a/vpi/v2009.sft +++ b/vpi/v2009.sft @@ -11,3 +11,6 @@ $low vpiSysFuncInt $high vpiSysFuncInt $increment vpiSysFuncInt $size vpiSysFuncInt + +$ivl_array_method$to_vec vpiSysFuncVoid +$ivl_array_method$from_vec vpiSysFuncVoid diff --git a/vpi/v2009_array.c b/vpi/v2009_array.c index 805773e63..0262f5f28 100644 --- a/vpi/v2009_array.c +++ b/vpi/v2009_array.c @@ -1,6 +1,8 @@ /* * Copyright (C) 2013 Cary R. (cygcary@yahoo.com) * Copyright (C) 2014 Stephen Williams (steve@icarus.com) + * Copyright (C) 2014 CERN + * @author Maciej Suminski (maciej.suminski@cern.ch) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -50,6 +52,47 @@ static PLI_INT32 one_array_arg_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name) return 0; } +// Checks if a function is passed an array and optionally an integer. +static PLI_INT32 array_int_opt_arg_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name) +{ + const int MAX_ARGC = 3; // one more is to verify there are at most 2 args + vpiHandle callh = vpi_handle(vpiSysTfCall, 0); + vpiHandle argv, arg[MAX_ARGC]; + PLI_INT32 arg_type[MAX_ARGC]; + int argc; + + argv = vpi_iterate(vpiArgument, callh); + argc = 0; + + if(argv) { + for(int i = 0; i < MAX_ARGC; ++i) { + arg[i] = vpi_scan(argv); + if(arg[i]) { + arg_type[i] = vpi_get(vpiType, arg[i]); + ++argc; + } else { + break; + } + } + } + + if (!argv || argc == MAX_ARGC || (arg_type[0] != vpiRegArray && arg_type[0] != vpiStringVar) || + (argc == 2 && arg_type[1] != vpiIntegerVar && + !(arg_type[1] == vpiConstant && vpi_get(vpiConstType, arg[1]) == vpiBinaryConst))) { + vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh), + (int)vpi_get(vpiLineNo, callh)); + vpi_printf("%s expects an array and optionally an integer.\n", name); + + if(argc == MAX_ARGC) + vpi_free_object(argv); + + vpi_control(vpiFinish, 0); + return 0; + } + + return 0; +} + static PLI_INT32 func_not_implemented_compiletf(ICARUS_VPI_CONST PLI_BYTE8* name) { vpiHandle callh = vpi_handle(vpiSysTfCall, 0); @@ -61,6 +104,47 @@ static PLI_INT32 func_not_implemented_compiletf(ICARUS_VPI_CONST PLI_BYTE8* name return 0; } +static PLI_INT32 array_get_property(int property, ICARUS_VPI_CONST PLI_BYTE8*name) +{ + vpiHandle callh = vpi_handle(vpiSysTfCall, 0); + vpiHandle argv, array, dim; + s_vpi_value value; + + argv = vpi_iterate(vpiArgument, callh); + if (argv == 0) { + vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh), + (int)vpi_get(vpiLineNo, callh)); + vpi_printf("%s requires an array argument.\n", name); + vpi_control(vpiFinish, 1); + return 0; + } + + array = vpi_scan(argv); + dim = vpi_scan(argv); + + if(dim != 0) { + vpi_printf("SORRY: %s:%d: multiple dimensions are not handled yet.\n", + vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh)); + vpi_control(vpiFinish, 1); + } + + value.format = vpiIntVal; + value.value.integer = vpi_get(property, array); + vpi_put_value(callh, &value, 0, vpiNoDelay); + + return 0; +} + +static PLI_INT32 left_calltf(ICARUS_VPI_CONST PLI_BYTE8*name) +{ + return array_get_property(vpiLeftRange, name); +} + +static PLI_INT32 right_calltf(ICARUS_VPI_CONST PLI_BYTE8*name) +{ + return array_get_property(vpiRightRange, name); +} + static void high_array(const char*name, vpiHandle callh, vpiHandle arg) { s_vpi_value value; @@ -176,32 +260,6 @@ void v2009_array_register(void) tf_data.compiletf = func_not_implemented_compiletf;; tf_data.sizetf = 0; - /* These functions are not currently implemented. */ - tf_data.tfname = "$dimensions"; - tf_data.user_data = "$dimensions"; - res = vpi_register_systf(&tf_data); - vpip_make_systf_system_defined(res); - - tf_data.tfname = "$unpacked_dimensions"; - tf_data.user_data = "$unpacked_dimensions"; - res = vpi_register_systf(&tf_data); - vpip_make_systf_system_defined(res); - - tf_data.tfname = "$left"; - tf_data.user_data = "$left"; - res = vpi_register_systf(&tf_data); - vpip_make_systf_system_defined(res); - - tf_data.tfname = "$right"; - tf_data.user_data = "$right"; - res = vpi_register_systf(&tf_data); - vpip_make_systf_system_defined(res); - - tf_data.tfname = "$increment"; - tf_data.user_data = "$increment"; - res = vpi_register_systf(&tf_data); - vpip_make_systf_system_defined(res); - tf_data.tfname = "$high"; tf_data.user_data = "$high"; tf_data.compiletf = one_array_arg_compiletf; @@ -215,4 +273,34 @@ void v2009_array_register(void) tf_data.calltf = low_calltf; res = vpi_register_systf(&tf_data); vpip_make_systf_system_defined(res); + + tf_data.tfname = "$left"; + tf_data.user_data = "$left"; + tf_data.compiletf = array_int_opt_arg_compiletf; + tf_data.calltf = left_calltf; + res = vpi_register_systf(&tf_data); + vpip_make_systf_system_defined(res); + + tf_data.tfname = "$right"; + tf_data.user_data = "$right"; + tf_data.compiletf = array_int_opt_arg_compiletf; + tf_data.calltf = right_calltf; + res = vpi_register_systf(&tf_data); + vpip_make_systf_system_defined(res); + + /* These functions are not currently implemented. */ + tf_data.tfname = "$dimensions"; + tf_data.user_data = "$dimensions"; + res = vpi_register_systf(&tf_data); + vpip_make_systf_system_defined(res); + + tf_data.tfname = "$unpacked_dimensions"; + tf_data.user_data = "$unpacked_dimensions"; + res = vpi_register_systf(&tf_data); + vpip_make_systf_system_defined(res); + + tf_data.tfname = "$increment"; + tf_data.user_data = "$increment"; + res = vpi_register_systf(&tf_data); + vpip_make_systf_system_defined(res); } diff --git a/vvp/vpi_darray.cc b/vvp/vpi_darray.cc index 6e4cf3402..a747df2bf 100644 --- a/vvp/vpi_darray.cc +++ b/vvp/vpi_darray.cc @@ -221,10 +221,16 @@ int __vpiDarrayVar::vpi_get(int code) switch (code) { case vpiArrayType: return vpiDynamicArray; + case vpiLeftRange: + return 0; + case vpiRightRange: + return get_size() - 1; case vpiSize: return get_size(); default: + fprintf(stderr, "vpi sorry: property is not implemented"); + assert(false); return 0; } } diff --git a/vvp/vpi_string.cc b/vvp/vpi_string.cc index 58aa6a664..1f7bb1851 100644 --- a/vvp/vpi_string.cc +++ b/vvp/vpi_string.cc @@ -51,7 +51,13 @@ int __vpiStringVar::vpi_get(int code) // The vpiSize of a string variable is the number of // bytes (characters) in that string. return str.size(); + case vpiLeftRange: + return 0; + case vpiRightRange: + return str.size() - 1; default: + fprintf(stderr, "vpi sorry: property is not implemented"); + assert(false); return 0; } }