题解 | #位拆分与运算#
位拆分与运算
http://www.nowcoder.com/practice/1649582a755a4fabb9763d07e62a9752
`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 [15:0] d_temp; reg out,validout; always@(posedge clk or negedge rst) begin if(!rst) begin out<=4'b0; validout<=0; end else begin case(sel) 0:begin d_temp<=d; out<=4'b0; validout<=0; end 2:begin out<=d_temp[3:0]+d_temp[11:8]; validout<=1; end 1:begin out<=d_temp[3:0]+d_temp[7:4]; validout<=1; end 3:begin out<=d_temp[3:0]+d_temp[15:12]; validout<=1; end default:begin out<=4'b0; validout<=0; end endcase end end
//**code// endmodule