题解 | #位拆分与运算#

位拆分与运算

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 reg validout
);
//*************code***********//
reg [4:0] out_s;
/*
always @(posedge clk or negedge rst) begin
    if(!rst) validout <= 0;
    else if (sel == 0) begin
        validout <= 0;
    end else validout <= 1;
end
*/
reg [15:0] d1;
always@(posedge clk,negedge rst)
    if(!rst) d1<=16'b0;
    else if(sel==1'b0) d1<=d;


always @(*) begin
    case(sel)
        2'b00: begin
            out       = 5'b0;
            validout = 1'b10; 
        end 
        2'b01: begin
            out       = d1[3:0] + d1[7:4];
            validout = 1'b1;
        end
        2'b10: begin
            out       = d1[3:0] + d1[11:8] ;
            validout = 1'b1;
        end
        2'b11: begin
            out       = d1[3:0] + d1[15:12];
            validout = 1'b1;
        end 
    endcase
end
/*
always @(posedge clk or negedge rst) begin
    if(!rst) out <= 5'b0;
    else out <= out_s;
end
*/
//*************code***********//
endmodule

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务