网上看到一道NVIDIA笔试的Verilog代码查错题,小弟才疏学浅,试着自己改了改,也不知对不对,欢迎大家拍砖指正~~
-------------------------------------------------------------------------------------------------------------------------------------------
原贴地址:http://bbs.yingjiesheng.com/thread-33829-1-1.html
3。Verilog代码查错。原代码基本如下:
always @( sel_and and sel_or and source_a and source_b )
begin
if( sel_and )
begin
data_0 = source_a & source_b;
sel_0 = 1'b1;
end
else if( sel_or )
begin
data_1 = source_a | source_b;
sel_0 = 1'b1;
end
end
assign sel_1 = sel_xor | sel_nxor;
assign data_1 = sel_1 ? source_c ^ source_d : ~( source_c ^ source_d );
always @( posedge clk )
begin
if( !rst_ )
data <= 1'b0;
else begin
if( sel_0 )
data <= data_0;
if( sel_1 )
data <= data_1;
end
(代码记不清了,大概就这样)
-------------------------------------------------------------------------------------------------------------------------------------------
我修改后是这样:
always @( sel_and or sel_or or source_a or source_b ) //看后面代码估计应该是or才对
begin
if( sel_and )
begin
data_0 = source_a & source_b;
sel_0 = 1'b1;
end
if( sel_or ) //把else去掉了,其实我更想改成两个always过程块,但是这样改应该也可以?
begin
data_1 = source_a | source_b;
sel_0 = 1'b1;
end
end
assign sel_1 = sel_xor | sel_nxor; //搞不明白,照这个语句,sel_1=1是必然发生的了,应该是原帖的问题
assign data_1 = sel_1 ? source_c ^ source_d : ~( source_c ^ source_d );
always @( posedge clk or negedge rst_n) //rst_n的下降沿也是敏感事件
begin
if( !rst_n) //估计原帖作者笔误少了个n
data <= 1'b0;
else begin
if( sel_0 )
data <= data_0;
else if( sel_1 ) //加了else,有了判断优先级,不然sel_0和sel_1都为1时..?
data <= data_1;
end //缺了个end
end
//也是因为原帖程序本来就残缺,不知这个程序到底要实现的是啥... 反正我修改了的地方,应该是可以的吧?呵呵,语法书我没怎么看~~
|