A priority encoder is a combinational circuit that, when given an input bit vector, outputs the position of the first 1 bit in the vector. For example, a 8-bit priority encoder given the input 8'b10010000 would output 3'd4, because bit[4] is first bit that is high.
Build a 4-bit priority encoder.
For this problem, if none of the input bits are high (i.e., input is zero), output zero. Note that a 4-bit number has 16 possible combinations.
优先编码器是一个组合电路,当给定一个输入位向量时,输出向量中前1位的位置。例如,一个输入为8'b10010000的8位优先编码器将输出3'd4,因为比特[4]是第一个高的比特。
构建一个4位优先编码器。
对于这个问题,如果所有输入位都不高(即输入为零),则输出为零。注意,一个4位的数字有16种可能的组合。
这个问题主要是繁琐,就是判断所有的输入,然后给一个值输出
这里介绍两个新的case语句
casex和casez
语法和普通case是一样的,只是说
casex语句则将高阻值z和不定值都视为不必关心的情况
reg[3:0]state;
casex(state)
8'bxxx1: do1;
8'bxx1x: do2;
8'bx1xx: do3;
8'b1xxx: do4;
endcase
casez语句用来处理不考虑高阻值z的比较过程
reg[3:0]state;
casez(state)
8'b???1: do1;
8'b??1?: do2;
8'b?1??: do3;
8'b1???: do4;
endcase
这里就使用了casex的用法
// synthesis verilog_input_version verilog_2001
module top_module (
input [3:0] in,
output reg [1:0] pos );
always@(*)begin
casex(in)
4'bxxx1: pos <= 2'b00;
4'bxx1x: pos <= 2'b01;
4'bx1xx: pos <= 2'b10;
4'b1xxx: pos <= 2'b11;
default: pos <= 2'b00;
endcase
end
endmodule