(7)Edgecapture

1.1 目录

(1)目录

(2)题目要求

(3)代码

(4)代码错误分析

1.2 题目要求

For each bit in a 32-bit vector, capture when the input signal changes from 1 in one clock cycle to 0 the next. "Capture" means that the output will remain 1 until the register is reset (synchronous reset).Each output bit behaves like a SR flip-flop: The output bit should be set (to 1) the cycle after a 1 to 0 transition occurs. The output bit should be reset (to 0) at the positive clock edge when reset is high. If both of the above events occur at the same time, reset has precedence. In the last 4 cycles of the example waveform below, the 'reset' event occurs one cycle earlier than the 'set' event, so there is no conflict here.

解释:通俗的意思就是输入一个in[31:0],每当输入的in中的某一位发生下降沿(就是位数据从1变为0)时就把out[31:0]的对应位置1。而且当reset为高电平时,要让out清零,但是只清零一个周期,然后就让out继续输出上一次的in[31:0].

对下图进行分析:当in[31:0]等于2(后四位为0010),然后下个周期变成e(后四位为1110)。此时0010变到1110,每一位都没有出现下降沿,所以out=0;然而当in[31:0]从e(后四位为1110)变成0(后四位为0000)时就有1、2、3位出现下降沿了,所以out的1、2、3位置1,输出out为e。当reset为高电平时对out置零一个周期,然后一个周期后out输出上一次in[31:0]的值即是2.

1.3 代码

//正确代码
module top_module (
    input clk,
    input reset,
    input [31:0] in,
    output [31:0] out
);
    reg [31:0]k;
    initial k=0;
    always@(posedge clk)
        k<=in;
    always@(posedge clk)
       if(reset)
           out<=0;
    	else
    		out<=~in&k|out;    
endmodule
//错误代码
module top_module (
    input clk,
    input reset,
    input [31:0] in,
    output [31:0] out
);
    reg [31:0]k;
    initial k=0;
    always@(posedge clk)
       if(reset)
           out<=0;
    	else
            begin
                 k<=in;   		
                 out<=~in&k|out;
    
            end
endmodule

1.4 代码错误分析

运行错位代码出来的仿真图片如下:

        我们可以从图中发现我们的out输出在红框处和参考输出不一致,具体原因就是我们的我们out值在reset后输出的是,上一个reset时的值即是10,而题目要求因该是输出上一个in[31:0]的值,上一个in[31:0]值为0,所以我们应该输出0才对。

没有输出零的原因:就是我们的代码中对值进行存储不是每个周期都进行的,而题目要求应该是每个周期都进行存储的。我们当reset第一次高电平时,我们的错误代码没有执行k<=in;这行代码。所以导致我们没有把0给k而是把10给k,所以就导致在reset变低电平时out为10;

解决办法就是把k<=in;这行代码,单独用clk时钟运行。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只准备起飞的小菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值