题解2 | #移位运算与乘法#
移位运算与乘法
https://www.nowcoder.com/practice/1dd22852bcac42ce8f781737f84a3272
将cnt/d_temp/移位运算拆分成多个写
注意:第一次计算d*1 时,out 并不能写为 out <= d_temp ,此时d_temp 是上一拍的值,当cnt = 0 && rst 时才将d存到d_temp 里。
`timescale 1ns/1ns module multi_sel( input [7:0]d , input clk, input rst, output reg input_grant, output reg [10:0]out ); //*************code***********// reg [2:0] cnt; reg [7:0] d_temp; always @(posedge clk or negedge rst) begin if (!rst) cnt <= 0; else if (cnt == 3'd3) cnt <= 0; else cnt <= cnt +1; end always @(posedge clk or negedge rst) begin if (!rst) begin d_temp <= 0; input_grant <= 0; end else if (cnt == 0) begin d_temp <= d; input_grant <= 1; end else begin d_temp <= d_temp; input_grant <= 0; end end always @(posedge clk or negedge rst) begin if (!rst) out <= 0; else case(cnt) 3'd0: out <= d; 3'd1: out <= (d_temp << 2) - d_temp; 3'd2: out <= (d_temp << 3) - d_temp; 3'd3: out <= d_temp << 3; default: out <= 0; endcase end //*************code***********// endmodule