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.1k views
in Technique[技术] by (71.8m points)

verilog - I don't understand this define macro with replication

I have SystemVerilog code in which replication is used that I don't understand. Please be thorough with your answer.

parameter WIDTH = 6;
logic [WIDTH-1:0] flag, flag2;

`define ZERO_X(n, m) {{m-$bits(n){1'b0}}, (n)}

assign flag = flag2 - `ZERO_X(1'b1, WIDTH);
question from:https://stackoverflow.com/questions/65836185/i-dont-understand-this-define-macro-with-replication

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

1 Reply

0 votes
by (71.8m points)

The first step is to perform simple text substitution in this expression. Replace n with 1'b1 and m with WIDTH. So this:

`ZERO_X(1'b1, WIDTH)

becomes:

{{WIDTH-$bits(1'b1){1'b0}}, (1'b1)}

Replace WIDTH with 6:

{{6-$bits(1'b1){1'b0}}, 1'b1}

$bits(1'b1) evaluates to 1:

{{(6-1){1'b0}}, 1'b1}

6-1 is just 5:

{{5{1'b0}}, 1'b1}

{5{1'b0}} replicates 1'b0 out to 5 0's:

{5'b0_0000, 1'b1}

Then simple concatenation:

6'b00_0001

Thus, this line:

assign flag = flag2 - `ZERO_X(1'b1, WIDTH);

evaluates to:

assign flag = flag2 - 6'b00_0001;

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

...