FPGA Central - World's 1st FPGA / CPLD Portal

FPGA Central

World's 1st FPGA Portal

 

Go Back   FPGA Groups > NewsGroup > Verilog

Verilog comp.lang.verilog newsgroup / usenet

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-02-2007, 03:44 PM
Uwe Bonnes
Guest
 
Posts: n/a
Default Initializing Array from String

Hello,

is there an easier way to initialize an verilog array of registers with the
values from a string define.

At least the way I do it below compiles in cver and icarus:

`define CNT_RST_COOKIE "CNT_RST"
module test();

reg [7:0] rst_cookie[0:6];

reg [55:0] string;

integer i, j;
initial
begin
string = `CNT_RST_COOKIE;
for (i=0; i < 7; i = i+1)
begin
rst_cookie[i] = 8'h0;
for (j=0; j<8; j = j+1)
rst_cookie[i] =rst_cookie[i] <<1 |string[8*(i+i)-1+j];
end
end
endmodule // test

Thanks
--
Uwe Bonnes [email protected]

Institut fuer Kernphysik Schlossgartenstrasse 9 64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------
Reply With Quote
  #2 (permalink)  
Old 03-02-2007, 06:21 PM
Jonathan Bromley
Guest
 
Posts: n/a
Default Re: Initializing Array from String

On Fri, 2 Mar 2007 14:44:33 +0000 (UTC), Uwe Bonnes
<[email protected]> wrote:

>is there an easier way to initialize an verilog array of registers with the
>values from a string define.


Unless you use SystemVerilog, I'm not aware of a much
better way; but you definitely make life easier by copying
bytes at a time, not bits!!!

There are a couple of other things you can do to tidy it up:

(1) use parameters somewhat (although that's hard, since you
can't determine the length of a string constant);
(2) make the loop counters local to your begin...end block;
(3) use Verilog-2001 indexed part selects for the vector slicing;
(4) make the string's range ascending to simplify
left-to-right subscripting.


`define CNT_RST_COOKIE "CNT_RST"
`define N_CHARS (7)
module test();

reg [7:0] rst_cookie[0:`N_CHARS-1];

reg [08*`N_CHARS)-1] str; // not 'string'!!!!!
// it's a SystemVerilog keyword, best avoided
initial begin : str_copy
integer i;
string = `CNT_RST_COOKIE;
for (i=0; i < `N_CHARS; i = i+1)
rst_cookie[i] = string[8*i +: 8];
end

endmodule // test

If your "string" is indexed 55:0 then the last line of
code would have to be rewritten:

rst_cookie[i] = string [8*(`N_CHARS-i) - 1 -: 8];

or something similar.
--
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
[email protected]
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
Reply With Quote
  #3 (permalink)  
Old 03-02-2007, 06:46 PM
Jonathan Bromley
Guest
 
Posts: n/a
Default Re: Initializing Array from String

On Fri, 02 Mar 2007 17:21:42 +0000, I wrote:

> reg [08*`N_CHARS)-1] str; // not 'string'!!!!!
> // it's a SystemVerilog keyword, best avoided


Yes, I was right about that. It's a pity I then forgot to
edit the variable name in the rest of the code....

> string = `CNT_RST_COOKIE;


that'll be
str = `CNT_RST_COOKIE;

> rst_cookie[i] = string[8*i +: 8];


and again:
rst_cookie[i] = str[8*i +: 8];

Apologies for any confusion.
--
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
[email protected]
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
Reply With Quote
  #4 (permalink)  
Old 03-03-2007, 02:59 AM
Uwe Bonnes
Guest
 
Posts: n/a
Default Re: Initializing Array from String

Jonathan Bromley <[email protected]> wrote:
> On Fri, 2 Mar 2007 14:44:33 +0000 (UTC), Uwe Bonnes
> <[email protected]> wrote:


> >is there an easier way to initialize an verilog array of registers with the
> >values from a string define.


> Unless you use SystemVerilog, I'm not aware of a much
> better way; but you definitely make life easier by copying
> bytes at a time, not bits!!!


> There are a couple of other things you can do to tidy it up:


> (1) use parameters somewhat (although that's hard, since you
> can't determine the length of a string constant);
> (2) make the loop counters local to your begin...end block;

Good hint
> (3) use Verilog-2001 indexed part selects for the vector slicing;

At least in ten way I coded it, neither cver not iverilog allowed
me to use bit-range selects in the for loop
....
> rst_cookie[i] = string[8*i +: 8];

^^
Any pointer where this syntax is explained?

Thanks
--
Uwe Bonnes [email protected]

Institut fuer Kernphysik Schlossgartenstrasse 9 64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------
Reply With Quote
  #5 (permalink)  
Old 03-03-2007, 09:23 AM
Jonathan Bromley
Guest
 
Posts: n/a
Default Re: Initializing Array from String

On Sat, 3 Mar 2007 01:59:36 +0000 (UTC), Uwe Bonnes
<[email protected]> wrote:

>> (3) use Verilog-2001 indexed part selects for the vector slicing;

>At least in ten way I coded it, neither cver not iverilog allowed
>me to use bit-range selects in the for loop


No; you can't use [start:end] syntax unless "start" and "end" are
both constants (unlike VHDL). But the "indexed part select" syntax,
using +: or -:, was added to deal with exactly this sort of problem.

>Any pointer where this [start +: width] syntax is explained?


It's Verilog-2001; explained in detail in the 1364-2001 or 1364-2005
LRM. The idea is simple:

[start_index +: width] or [start_index -: width]

width must be constant, so that Verilog can determine the bit-width
of the expression at compile time; start_index can be any expression.
The only surprising feature is the way this interacts with ascending
and descending subscripts. In the case of +:, the subscripts are
calculated like this:

[start_index +: width] =>
lowest-numbered subscript = start_index
highest_numbered subscript = start_index + width - 1

Now we must arrange the subscripts into the right order,
highest:lowest or lowest:highest, depending on the declaration
of the thing you are subscripting. Verilog does this automatically.
Similarly for -: where lowest-subscript = start_index,
highest-subscript = start_index + 1 - width.

All the commercial tools I use now fully support this, although
for some of them you need to enable V-2001 explicitly; I don't
know about icarus and cver.

>Thanks


You're welcome.

Cheap shameless advertising: Verilog-2001 constructs are fully
catalogued in our Golden Reference Guide
http://www.doulos.com/content/produc...nce_guides.php
--
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
[email protected]
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
Reply With Quote
  #6 (permalink)  
Old 03-03-2007, 04:54 PM
Stephen Williams
Guest
 
Posts: n/a
Default Re: Initializing Array from String

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jonathan Bromley wrote:

> It's Verilog-2001; explained in detail in the 1364-2001 or 1364-2005
> LRM. The idea is simple:
>
> [start_index +: width] or [start_index -: width]


[...]

> All the commercial tools I use now fully support this, although
> for some of them you need to enable V-2001 explicitly; I don't
> know about icarus and cver.


Icarus Verilog supports it in current snapshots. (I guess the v0.8
stable branch is getting pretty old, but I have specific milestones
I'm trying to reach for the 0.9 release.)

- --
Steve Williams "The woods are lovely, dark and deep.
steve at icarus.com But I have promises to keep,
http://www.icarus.com and lines to code before I sleep,
http://www.picturel.com And lines to code before I sleep."
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFF6ZpPrPt1Sc2b3ikRArQQAJ93nEnJf/7XXNZQSNPtjGv30qVZbgCffjcm
NUp9ZEfXLLq/RpM65eZZXg0=
=F2yb
-----END PGP SIGNATURE-----
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
initializing array of registers [email protected] Verilog 1 05-07-2006 12:22 AM
Initializing array of BlockRAM instances in verilog [email protected] Verilog 1 04-22-2006 09:09 AM
Problem initializing a RAM Giox Verilog 1 11-18-2005 05:29 PM
Initializing counters in Verilog Adam Verilog 4 08-28-2003 02:10 AM
Verilog VCD and string literals Jan Decaluwe Verilog 0 07-24-2003 11:30 AM


All times are GMT +1. The time now is 06:05 PM.


Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Copyright 2008 @ FPGA Central. All rights reserved