On Mon, 23 Aug 2010 05:00:00 -0700 (PDT), hhanff wrote:
>The solution to your exercise ahould be:
>
> function put_Nbytes_f(data : std_logic_vector;
> first_adrs : natural;
> num_bytes : positive)
> return byte_array_t is
> variable result : byte_array_t(512 -1 downto 0);
> begin
> for i in 0 to num_bytes-1 loop
> result(first_adrs+i) := data(8*i+7 downto 8*i);
> --- puts 8 bit into rightmost rightmos byte
> end loop;
> return result(num_bytes-1 downto 0);
> end function;
Yes, but that's probably a little inefficient - it
constructs the whole of "result" even if you use
only a tiny part of it. More important, though,
it has unnecessary arguments.
As an example, consider
mySig: byte_array_t(511 downto 0);
...
--- now we update just 2 words of it
mySig(5 downto 4) <= put_Nbytes(some_data, 4, 2);
Note how you are tempted to repeat the "start address"
on both sides. Indeed, the '4' argument to put_Nbytes
doesn't really do anything useful at all, and the '2'
length argument is unnecessary too. You could
mightily simplify it...
function to_bytes(data:std_logic_vector)
return byte_array_t is
constant nBytes: integer := data'length/8;
variable result: byte_array_t(nBytes-1 downto 0);
constant normalised:
std_logic_vector(data'length-1 downto 0)
:= data;
begin
assert (data'length mod

= 0
report "Data should be a multiple of 8 bits"
severity error;
for i in 0 to nBytes-1 loop
result(i) := data(8*i+7 downto 8*i);
end loop;
return result;
end;
Now you could do
mySig(5 downto 4) <= to_bytes(X"CAFE");
mySig(13 downto 10) <= to_bytes(X"BAD4F00D");
Originally I was thinking, rather, of passing
the target array as an out or inout argument to
a procedure, so that it is passed by reference,
and then having the procedure update just the
necessary elements. However, this is less
convenient than the function because you need
two versions of the procedure - one to update
a signal result, and one to update a variable[*].
And you can't call any procedure from within
a function [**]. So, on balance, I like the function
version better.
[*] VHDL wishlist item: Some way of overloading
subprograms based on the class (signal vs other things)
of their arguments, so that you can write identical
procedures to update signals and variables. Today
you must create two procedures with different names.
[**] Another VHDL wishlist item: void functions to
encapsulate non-time-consuming activity whilst
permitting input, output and inout arguments.
Did that make it into VHDL-2008? I don't think so.
--
Jonathan Bromley