题解 | 移位运算与乘法
`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 [1:0] count;
always @(posedge clk or negedge rst)begin
if(!rst)begin
count <= 2'b0;
end
else begin
count <= count + 1'b1;
end
end
reg [7:0] d_reg;
always @(posedge clk or negedge rst)begin
if(!rst)begin
out <= 11'b0;
input_grant <= 1'b0;
d_reg <= 8'b0;
end
else begin
case (count)
2'b00 : begin
out <= d;
d_reg <= d;
input_grant <= 1'b1;
end
2'b01 : begin
out <= d_reg + {d_reg , 1'b0};
input_grant <= 1'b0;
end
2'b10 : begin
out <= d_reg + {d_reg , 1'b0} + {d_reg , 2'b0};
input_grant <=1'b0;
end
2'b11 : begin
out <= {d_reg, 3'b0};
input_grant <= 1'b0;
end
default : begin
out <= d;
input_grant <= 1'b0;
end
endcase
end
end
//*************code***********//
endmodule
FPGA数字IC牛客网Verilog刷题04-移位拼接乘法
来源如上,本题目要求在每个时钟周期都输出数乘1/3/7/8,因此需要有一个储存模块来储存下个时钟周期想要发送的数据,此段代码使用了reg [7:0] d_reg来储存上一个时刻的数据。使用reg [1:0] count,来记录不同四个阶段的数据,相当于一个简易的状态机系统。每个阶段count+1,再通过case语句判断不同的count状态分别达到输出的四个状态。在每个case 00 ,01 , 10 ,11中使用拼接运算符做移位乘法
上海得物信息集团有限公司公司福利 1263人发布