牛客刷题verilog

VL5 位拆分与运算

点击进入题目
这是通过的代码,但总觉得波形图不对,根据波形图seloutvalidout中间应该没有寄存器才对
题目给的波形图

`timescale 1ns/1ns

module data_cal(
input clk,
input rst,
input [15:0]d,
input [1:0]sel,

output reg[4:0]out,//注意out和validout都是reg型变量 
output reg validout
);
//*************code***********//
reg [15:0] din;//用来暂存,sel=0时才有效的输入d

always @(posedge clk or negedge rst)begin
    if(!rst)begin
        out<=0;
        validout<=0;
        din<=0;
    end
    else begin
        case(sel)
        0:begin
            din<=d;
            out<=0;
            validout<=0;
        end
        1:begin
            out<=din[3:0]+din[7:4];
            validout<=1;
        end
        2:begin
            out<=din[3:0]+din[11:8];
            validout<=1;
        end
        3:begin
            out<=din[3:0]+din[15:12];
            validout<=1;
        end
        endcase
    end
end

//*************code***********//
endmodule

通过的代码

`timescale 1ns/1ns

module data_cal(
input clk,
input rst,
input [15:0]d,
input [1:0]sel,

output reg[4:0]out,//注意out和validout都是reg!
output reg validout
);
//*************code***********//
reg [15:0] din;//用来暂存,sel=0时才有效的输入d

always @(posedge clk or negedge rst)begin
    if(!rst)begin
        out<=0;
    end
    else if(!sel)begin
       din<=d;
    end
end
    
always @(sel)  begin
    case(sel)
        0:begin
            out =0;
            validout =0;
        end
        1:begin
            out =din[3:0]+din[7:4];
            validout =1;
        end
        2:begin
            out =din[3:0]+din[11:8];
            validout =1;
        end
        3:begin
            out =din[3:0]+din[15:12];
            validout =1;
        end
    endcase
end
 
//*************code***********//
endmodule

可是为啥两种写法都通过呢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值