题解 | #序列发生器#

序列发生器

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];

               

   

       


全部评论

相关推荐

11-11 22:08
佛山大学 Java
点赞 评论 收藏
分享
渴望wlb的牛油果很...:直说卡第一学历不就行了 非得拐弯抹角
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
6
收藏
分享

创作者周榜

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