4位优先编码器,可采用移位的方式来写,也可以用case语句列出所有的可能项,case语句写起来比较长。也可以用casez语句来写
移位的写法
module top_module (
input [3:0] in,
output reg [1:0] pos );
always@ (*)
if((in << 3) == 4'b1000) pos <= 2'd0;
else if((in << 2) == 4'b1100 || (in << 2) == 4'b1000) pos <= 2'd1;
else if((in >> 2) == 4'b0011 || (in >> 2) == 4'b0001) pos <= 2'd2;
else if((in >> 3) == 4'b0001) pos <= 2'd3;
else pos <= 2'd0;
endmodule
casez语句写法
module top_module (
input [3:0] in,
output reg [1:0] pos );
always @(*)
begin
casez (in[3:0])
4'bzzz1: out = 0;
4'bzz1z: out = 1;
4'bz1zz: out = 2;
4'b1zzz: out = 3;
default: out = 0;
endcase
end
endmodule
本文探讨了一种4位优先编码器的实现,通过比较移位运算和casez语句两种方式,介绍了如何简洁地使用这两种方法来表示所有输入映射到输出的状态。移位方法直观易懂,而casez语句虽然紧凑但可能阅读起来较复杂。
892

被折叠的 条评论
为什么被折叠?



