HDLBits:UWaterloo CS450

这篇博客介绍了使用Verilog HDL实现两种计数器模块。第一个是基于滑铁卢大学cs450课程的倒计时计时器,当load信号为1时开始倒数,计数到0时输出tc为1。第二个是具有上下限的计数器,根据train_valid和train_taken信号增减计数值。同时,还详细解析了一种历史移位寄存器的设计,当predict_valid或train_mispredicted有效时更新预测历史。
摘要由CSDN通过智能技术生成

最近发现HDLBits上面的题目有更新,貌似是滑铁卢大学cs450的题目,准备做一做,把答案整理在下面以供各位参考

  1. timer/计时器
    这个题目很好理解,就是当load为1的时候将数据读入并进行倒数,当倒数结束后输出tc为1,需要注意的地方为中间量counter的位数需要和data保持一致。
    在这里插入图片描述
module top_module(
	input clk, 
	input load, 
	input [9:0] data, 
	output tc
);
	reg [9:0]counter;
    
    always @(posedge clk) begin
        if(load) begin
           counter <= data;  
        end
        else if(counter!=10'd0) begin
           counter <= counter - 1'd1; 
        end
        else if (counter==10'd0) begin
           counter <= 10'd0; 
        end
    end
    
    assign tc = (counter==10'd0);
    
    
endmodule

2.counter 2bc/计数器
题目挺绕的,没怎么看懂,还好下面的description讲得听明白的,简单来说就是一个下限为0、上限为3的计数器,当train_valid = 1 与 train_taken = 1时计数器值增加,train_valid = 1 and train_taken = 0时计数器值减小,train_valid = 0时计数器值保持不变,自己画了一个有限状态机转移图:
在这里插入图片描述

module top_module(
    input clk,
    input areset,
    input train_valid,
    input train_taken,
    output [1:0] state
);
    parameter s0=2'd0,s1=2'd1,s2=2'd2,s3=2'd3;
    reg [1:0] current_state,next_state;
    
    always @(*) begin
        case(current_state)
            s0: if(!train_valid) next_state <= s0; //0?
            else if(train_taken) next_state <= s1; //11
            else next_state <= s0; //10
            s1: if(!train_valid) next_state <= s1; //0?
            else if(train_taken) next_state <= s2; //11
            else next_state <= s0; //10
            s2: if(!train_valid) next_state <= s2; //0?
            else if(train_taken) next_state <= s3; //11
            else next_state <= s1; //10
            s3: if(!train_valid) next_state <= s3; //0?
            else if(train_taken) next_state <= s3; //11
            else next_state <= s2; //10
        endcase
    end
    
    always @(posedge clk or posedge areset) begin
        if(areset) current_state <= 2'b01; 
        else current_state <= next_state;
    end
    
    assign state = current_state;

endmodule

  1. history shift/历史移位寄存器
    descprition里的大体意思是:当predict_valid = 1,即需要做预测时,predict_taken会作为预测的量从LSB中取出放入predict_history[0];当train_mispredicted=1,即出现错误时,需要将train_history和train_taken连接后再赋给输出predict_history,此外题目明确要求了train_mispredicted的优先级需要高于predict_valid。
module top_module(
    input clk,
    input areset,

    input predict_valid,
    input predict_taken,
    output [31:0] predict_history,

    input train_mispredicted,
    input train_taken,
    input [31:0] train_history
);
    
    always @(posedge clk or posedge areset) begin
        if (areset) begin
           predict_history <= 32'd0; 
        end
        else if(train_mispredicted) begin
            predict_history <= {train_history[30:0],train_taken};
        end
        else if(predict_valid) begin
            predict_history <= {predict_history[30:0],predict_taken};
        end
    end

endmodule

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值