module的参数例化
module async_fifo #(parameter FIFO_PTR = 4, FIFO_WIDTH = 32)([port_list])
格雷码转换与generate结构
generate语句可以配合genvar实现将assign语句进行并行化处理。否则有有以下提示:
Generate for loop index variable must be a genvar.
Please refer to LRM (1364-2005), section 12.4.1 "Loop generate constructs".
一段错误的代码
格雷码转换代码与测试:
代码中若不延时1个时间单位(i赋值后),则导致输出无法按时获得。
这个代码似乎是将二进制转换为了格雷码,
module grey2bin #(parameter PTR=6)(grey_value, bin_value);
input [PTR:0] grey_value;
output [PTR:0] bin_value;
/* wire [PTR:0] bin_value; */
assign bin_value[PTR] = grey_value[PTR];
generate
genvar i;
for (i = 0; i < PTR; i=i+1) begin
assign bin_value[i] = grey_value[i+1] ^ grey_value[i];
end
endgenerate
endmodule
module test_case ();
reg [3:0] input_data;
wire [3:0] output_data;
grey2bin #(3) g2b(input_data, output_data);
initial begin
bit [4:0] i;
bit [3:0] arr[$];
assign input_data = i;
for (i = 0; i < 16 ; i=i+1) begin
#1;
$display("%04b, output is grey :%04b",i, output_data);
arr.push_back(output_data);
end
$display("-----------------------------------------------");
foreach (arr[j]) begin
i = arr[j];
#1;
$display("%04b, output is binary :%04b",i, output_data);
end
$finish();
end
endmodule
输出结果:
0000, output is grey :0000
0001, output is grey :0001
0010, output is grey :0011
0011, output is grey :0010
0100, output is grey :0110
0101, output is grey :0111
0110, output is grey :0101
0111, output is grey :0100
1000, output is grey :1100
1001, output is grey :1101
1010, output is grey :1111
1011, output is grey :1110
1100, output is grey :1010
1101, output is grey :1011
1110, output is grey :1001
1111, output is grey :1000
-----------------------------------------------
0000, output is binary :0000
0001, output is binary :0001
0011, output is binary :0010
0010, output is binary :0011
0110, output is binary :0101
0111, output is binary :0100
0101, output is binary :0111
0100, output is binary :0110
1100, output is binary :1010
1101, output is binary :1011
1111, output is binary :1000
1110, output is binary :1001
1010, output is binary :1111
1011, output is binary :1110
1001, output is binary :1101
1000, output is binary :1100