题解 | #序列发生器#

序列发生器

https://www.nowcoder.com/practice/1fe78a981bd640edb35b91d467341061

方法一:用计数器和Case语句实现
    reg [2:0] cnt;
    always@(posedge clk or negedge rst_n)begin
        if(!rst_n) cnt<=3'b0;
        else begin
            if(cnt==3'd5)
                cnt<=3'b0;
            else    cnt<=cnt+1'b1;
        end        
    end
    always@(posedge clk or negedge rst_n)begin
        if(!rst_n) data<=1'b0;
        else begin
            case(cnt)
                3'd0:data<=1'b0;
                3'd1:data<=1'b0;
                3'd2:data<=1'b1;
                3'd3:data<=1'b0;
                3'd4:data<=1'b1;
                3'd5:data<=1'b1;
            endcase
        end        
    end
方法二:循环移位实现(左移)

reg            [5:0]           data_reg;

 

always@(posedge clk or negedge rst_n)

    if(rst_n == 1'b0)

        data_reg <= 6'b001011;

    else if(data_reg == 6'b100000)

        data_reg <= 6'b001011;

    else

        data_reg <= (data_reg << 1);

 

always@(posedge clk or negedge rst_n)

    if(rst_n == 1'b0)

        data <= 1'b0;

    else

        data <= data_reg[5];

               

   

       


全部评论

相关推荐

用微笑面对困难:这里面最强的是驾驶证了,可以入职美团大厂,然后直接开启黄马褂人生
点赞 评论 收藏
分享
我不行了,我真过不了第二关
码农索隆:嘿,哥们连界面都进去去,更别提玩了
点赞 评论 收藏
分享
评论
4
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务