SV: Return an unpacked array of integer!

A

Amal

Guest
How does declare a function to return an unpacked array of integers?
The following code the header of the function declares a packed
array. How does one change it to an unpacked array?!

function integer [0:9] f();
integer result[0:9]
for( int i=0; i<10; i++ )
result = i;
end
return result;
endfunction f;

Thanks,
-- Amal
 
On Jan 12, 1:08 pm, Amal <akhailt...@gmail.com> wrote:
How does declare a function to return an unpacked array of integers?
The following code the header of the function declares a packed
array.  How does one change it to an unpacked array?!

  function integer [0:9] f();
    integer result[0:9]
    for( int i=0; i<10; i++ )
      result = i;
    end
    return result;
  endfunction f;

Thanks,
-- Amal
Sorry, the correct code is:

function integer [0:9] f();
integer result[0:9];
for( int i=0; i<10; i++ ) begin
result = i;
end
return result;
endfunction f;

What should the function header ( function integer [0:9] f(); ) be?
-- Amal
 
On Jan 12, 1:09 pm, Amal <akhailt...@gmail.com> wrote:
What should the function header ( function integer [0:9] f(); ) be?
The syntax analogous to a variable declaration would be

function integer f [0:9] ();

But that does not seem to be legal syntax. So you will need to use a
typedef:

typedef integer array_type [0:9];

function array_type f();
 
SystemVerilog does not allow you to specify an anonymous unpacked type
when you are not declaring a variable. You must use a typedef.

In general, it's a good idea to use a typedef for anything that is
strongly typed because you will need to ensure matching types on the
right and left sides of assignments anyways.

Dave Rich


On Jan 12, 11:19 am, sh...@cadence.com wrote:
On Jan 12, 1:09 pm, Amal <akhailt...@gmail.com> wrote:



What should the function header ( function integer [0:9] f(); ) be?

The syntax analogous to a variable declaration would be

  function integer f [0:9] ();

But that does not seem to be legal syntax.  So you will need to use a
typedef:

  typedef integer array_type [0:9];

  function array_type f();
 
On Jan 12, 8:30 pm, Dave Rich <dave.nospam.r...@gmail.com> wrote:
SystemVerilog does not allow you to specify an anonymous unpacked type
when you are not declaring a variable. You must use a typedef.

In general, it's a good idea to use a typedef for anything that is
strongly typed because you will need to ensure matching types on the
right and left sides of assignments anyways.

Dave Rich

On Jan 12, 11:19 am, sh...@cadence.com wrote:

On Jan 12, 1:09 pm, Amal <akhailt...@gmail.com> wrote:

What should the function header ( function integer [0:9] f(); ) be?

The syntax analogous to a variable declaration would be

  function integer f [0:9] ();

But that does not seem to be legal syntax.  So you will need to use a
typedef:

  typedef integer array_type [0:9];

  function array_type f();
Another question on the subject of arrays. How can I declare a
function that returns an array of objects? And on the same note, use
a mailbox to pass an array of object?

I tried typedef for this and it does not seem to like the declaration.

Basically the following does not work.

------------------------------
class dummy;
endclass : dummy

typedef dummy[] dummy-array;

mailbox #(dummy_array) mbx;

function dummy_array foo();
endfunction : dummy_array
------------------------------

Cheers,
-- Amal
 
On Thu, 12 Mar 2009 07:01:43 -0700 (PDT), Amal wrote:

Another question on the subject of arrays. How can I declare a
function that returns an array of objects? And on the same note, use
a mailbox to pass an array of object?

I tried typedef for this and it does not seem to like the declaration.

Basically the following does not work.
[snip]

Once I got rid of your various typos and made a complete
example, it all worked just fine:

module m;
class dummy;
int x;
endclass : dummy

typedef dummy dummy_array[]; // unpacked, please

function dummy_array foo();
// construct a result
foo = new[5];
// construct the individual objects
foreach (foo) begin
foo = new;
foo.x = i;
end
endfunction : foo

initial begin
dummy_array d,e;
mailbox #(dummy_array) mbx;

d = foo();
foreach (d) $display("d[%0d].x=%0d", i, d.x);
mbx = new;
mbx.put(d);
mbx.get(e);
foreach (e) $display("e[%0d].x=%0d", i, e.x);
end

endmodule
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 
On Mar 16, 2:44 pm, Jonathan Bromley <jonathan.brom...@MYCOMPANY.com>
wrote:
On Thu, 12 Mar 2009 07:01:43 -0700 (PDT), Amal wrote:
Another question on the subject of arrays.  How can I declare a
function that returns an array of objects?  And on the same note, use
a mailbox to pass an array of object?

I tried typedef for this and it does not seem to like the declaration.

Basically the following does not work.

[snip]

Once I got rid of your various typos and made a complete
example, it all worked just fine:

module m;
class dummy;
int x;
endclass : dummy

typedef dummy dummy_array[];  // unpacked, please

function dummy_array foo();
  // construct a result
  foo = new[5];
  // construct the individual objects
  foreach (foo) begin
    foo = new;
    foo.x = i;
  end
endfunction : foo

initial begin
  dummy_array d,e;
  mailbox #(dummy_array) mbx;

  d = foo();
  foreach (d) $display("d[%0d].x=%0d", i, d.x);
  mbx = new;
  mbx.put(d);
  mbx.get(e);
  foreach (e) $display("e[%0d].x=%0d", i, e.x);
end

endmodule
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.brom...@MYCOMPANY.comhttp://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.

Yes, I had a lot of typos. But thanks for the clarification.

-- Amal
 

Welcome to EDABoard.com

Sponsor

Back
Top