题解 | #位拆分与运算#

位拆分与运算

https://www.nowcoder.com/practice/1649582a755a4fabb9763d07e62a9752

`timescale 1ns/1ns

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

output  reg     [4:0]   out,
output  wire            validout
);
//*************code***********//

reg     [15:0]  d_reg;
wire    [3:0]   d0,d1,d2,d3;

assign  {d3,d2,d1,d0} = d_reg[15:0];
assign  validout = | sel;

always@(posedge clk or negedge rst)
    if(!rst)
        d_reg <= 16'b0;
    else if(!validout)
        d_reg <= d;
    else
        d_reg <= d_reg;

always@(*)
    if(!rst)
        out <= 5'b0;
    else
        case(sel)
        2'd0    :   out <= 5'b0;
        2'd1    :   out <= d0 + d1;
        2'd2    :   out <= d0 + d2;
        2'd3    :   out <= d0 + d3;
        default :   out <= 5'b0;
        endcase
        
//*************code***********//
endmodule

全部评论

相关推荐

09-21 21:14
门头沟学院
否极泰来来来来:和他说:这里不好骂你,我们加个微信聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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