Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Reading string and setting register bits in Verilog

Status
Not open for further replies.

prab97

Newbie level 2
Joined
Feb 7, 2012
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,299
I have a few lines of strings consisting of 0s and 1s only, in a file for example:

00000000010000100000001111101000
00000000100001000000010010110000
00010000010001000000000000000000
01111000000000000000000000000000

Each line is 32 chars in size. I have to read each line, and in a loop I have to set a 32 bit register according to the character encountered. This is what I wrote:


Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module readBIN;
    reg [8*32:1] data;
    reg [32:0] inst;
    integer file, n, i;
initial
begin
    file = $fopen("test.txt", "r");
    i=0;
    while(!$feof(file)) begin
        n=$fgets(data, file);
        if(data[(i + 1) * 8:i * 8 + 1] == "1") begin
            inst[i]=1;
        end
        else begin
            inst[i]=0;
        end
    end
    $write("%d", inst);
end
endmodule



The problem is that I get an error saying part-select must be constant. Now how do I solve this problem?
I should tell you that I am an absolute beginner in Verilog and know only 'Hello world' as of now.

Thanks!
 
Last edited:

permute

Advanced Member level 3
Joined
Jul 16, 2010
Messages
923
Helped
295
Reputation
590
Reaction score
268
Trophy points
1,343
Activity points
8,543
look into using readmemb.
 
  • Like
Reactions: prab97

    prab97

    Points: 2
    Helpful Answer Positive Rating

mrflibble

Advanced Member level 5
Joined
Apr 19, 2010
Messages
2,724
Helped
679
Reputation
1,360
Reaction score
651
Trophy points
1,393
Activity points
19,551
Well, the "part-select must be constant" is actually pretty descriptive. You are trying to do a bit select on a bit vector. You do this bit select from bit number A to bit number B. But these A and B values must be a constant value (as opposed to a runtime variable like the i
integer). As permute suggested, you may want to look into readmemb. See for example: Modeling Memories And FSM Part - I

Also, thanks for actually using code / syntax tags on a first post! You'd be amazed at how few people use that. :p Regarding the "[" etc stuff ... did that creep in after you entered it? Looks a bit weird...
 
  • Like
Reactions: prab97

    prab97

    Points: 2
    Helpful Answer Positive Rating

prab97

Newbie level 2
Joined
Feb 7, 2012
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,299
Also, thanks for actually using code / syntax tags on a first post! You'd be amazed at how few people use that. :p Regarding the "[" etc stuff ... did that creep in after you entered it? Looks a bit weird...

Yeah the square brackets got converted into #91 and #93 automagically.
Thanks permute and mrflibble. I'll try figuring out readmemb usage. I am good at C, C++ and Java but never ever worked with Verilog.
 

mrflibble

Advanced Member level 5
Joined
Apr 19, 2010
Messages
2,724
Helped
679
Reputation
1,360
Reaction score
651
Trophy points
1,393
Activity points
19,551
Funny, sounds like it's a bit buggy then. Because when I typed "[" and did a Preview Post, I noticed it changed it to a bracket. :p Must be a feature of the syntax tag...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top