Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

verilog - For loop with binary numbers

I want to use a for loop in Verilog to get from binary 0000000 to 0011111. I have a problem with the increment part of the for loop. How should I exactly move to get to the 0011111?

I tried the following code, but it gives me an error.

for(DATA_IN=7'b0000000; DATA_IN<=7'b0011111; 1<<DATA_IN);
question from:https://stackoverflow.com/questions/65927030/for-loop-with-binary-numbers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To increment by 1 each time through the loop, use DATA_IN=DATA_IN+1. Here is a self-contained example:

module tb;

reg [7:0] DATA_IN;

initial begin
    for (DATA_IN=7'b0000000; DATA_IN<=7'b0011111; DATA_IN=DATA_IN+1) begin
        $displayb(DATA_IN);
    end
end

endmodule

Output:

00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
00001011
00001100
00001101
00001110
00001111
00010000
00010001
00010010
00010011
00010100
00010101
00010110
00010111
00011000
00011001
00011010
00011011
00011100
00011101
00011110
00011111

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...