• Verilog刷题[hdlbits] :Always case2


    题目:Always case

    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.
    优先编码器是一种组合电路,当给定输入位矢量时,它输出矢量中第一个1位的位置。例如,给定输入8’b10010000的8位优先级编码器将输出3’d4,因为比特[4]是第一个高比特。

    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.
    构建一个4位优先编码器。对于这个问题,如果没有一个输入位是高的(即,输入为零),输出为零。请注意,一个4位数字有16种可能的组合。

    该题目要求输出给定的4位矢量中从右向左数,第一个出现高电平的位置。由于给定的4位矢量共有16种可能,在使用case语句时,需要将所有可能列出并写出对应输出。
    同时,如果取消case语句的限定,该题目可以通过if语句解决。

    // synthesis verilog_input_version verilog_2001
    module top_module (
        input [3:0] in,
        output reg [1:0] pos  );
    	
    	//case
        always@(*)
            begin
                case(in)
                    4'b0000:pos = 2'd00;
                    4'b0001:pos = 2'd00;
                    4'b0010:pos = 2'd01;
                    4'b0011:pos = 2'd00;
                    4'b0100:pos = 2'd02;
                    4'b0101:pos = 2'd00;
                    4'b0110:pos = 2'd01;
                    4'b0111:pos = 2'd00;
                    4'b1000:pos = 2'd03;
                    4'b1001:pos = 2'd00;
                    4'b1010:pos = 2'd01;
                    4'b1011:pos = 2'd00;
                    4'b1100:pos = 2'd02;
                    4'b1101:pos = 2'd00;
                    4'b1110:pos = 2'd01;
                    4'b1111:pos = 2'd00;
                    default:pos = 2'd00;
                endcase
            end
        
    endmodule
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    // synthesis verilog_input_version verilog_2001
    module top_module (
        input [3:0] in,
        output reg [1:0] pos  );
    	
    	//if
        always@(*)
            begin
                if(in[0] == 1'b1)
                     pos = 2'b00;
                else if(in[0] != 1'b1 && in[1] == 1'b1)
                     pos = 2'b01;
                else if(in[0] != 1'b1 && in[1] != 1'b1 && in[2] == 1'b1)
                     pos = 2'b10;
                else if(in[0] != 1'b1 && in[1] != 1'b1 && in[2] != 1'b1 && in[3] == 1'b1)
                     pos = 2'b11;
                else
                     pos = 2'b00;
            end
        
    endmodule
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    【ZooKeeper 】安装和使用,以及java客户端
    3d测试环境搭建
    USART串口协议
    web前端期末大作业实例 (1500套) 集合
    Spring Boot异步请求处理框架
    七通道NPN 达林顿管GC2003,专为符合标准 TTL 而制造,最高工作电压 50V,耐压 80V
    Python 潮流周刊#21:如何提升及测量 Python 代码的性能?
    学历低不能学编程?
    JDK的配置及运行过程
    Cpp/Qt-day030919Qt
  • 原文地址:https://blog.csdn.net/weixin_46585382/article/details/134297096