priority encoder

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.

Always case2 - HDLBits (01xz.net)

完成一个优先编码器,只不过这个优先编码器是倒着来从小位开始的,比如第0位出现了1,则编码结果为0;第0位为0,第1位出现了1,则编码结果为1;第0位为0,第1位为0,第2位为1,则编码结果为2,依此类推

所以这样下来这道题就容易理解了,编写代码即可

 1 // synthesis verilog_input_version verilog_2001
 2 module top_module (
 3     input [3:0] in,
 4     output reg [1:0] pos  );
 5  always @(*)begin 
 6         if(in[0])begin
 7             pos<=2'd0;
 8         end
 9         else if(in[1])begin
10             pos<=2'd1;
11         end
12         else if(in[2])begin
13             pos<=2'd2;
14         end
15         else if(in[3])begin
16             pos<=2'd3;
17         end
18         else begin
19             pos<=2'd0;
20         end
21     end   
22 
23 endmodule

 

posted @ 2023-04-20 00:27  江左子固  阅读(58)  评论(0)    收藏  举报