题解 | #位拆分与运算#
位拆分与运算
http://www.nowcoder.com/practice/1649582a755a4fabb9763d07e62a9752
//注意仅在sel = 0时输入有效; `timescale 1ns/1ns
module data_cal( input clk, input rst, input [15:0]d, input [1:0]sel,
output [4:0]out, output validout ); //**code// reg [4:0] outputdata; reg [15:0] d_reg; reg valid ; always@(posedge clk or negedge rst)begin if(~rst)begin outputdata <= 5'd0; valid <= 1'd0; d_reg <= 16'd0; end else if(sel == 0)begin outputdata <= 5'd0; valid <= 1'd0; d_reg <= d; end else if(sel == 1)begin outputdata <= {1'b0,d_reg[3:0]} + {1'b0,d_reg[7:4]}; valid <= 1'd1; end else if(sel == 2)begin outputdata <= {1'b0,d_reg[3:0]} + {1'b0,d_reg[11:8]}; valid <= 1'd1; end else if(sel == 3)begin outputdata <= {1'b0,d_reg[3:0]} + {1'b0,d_reg[15:12]}; valid <= 1'd1; end end
assign out = outputdata;
assign validout = valid;
//**code// endmodule