题解 | #移位运算与乘法#
移位运算与乘法
http://www.nowcoder.com/practice/1dd22852bcac42ce8f781737f84a3272
目录
代码
使用了状态机。
`timescale 1ns/1ns
module multi_sel(
input [7:0]d ,
input clk,
input rst,
output reg input_grant,
output reg [10:0]out
);
//*************code***********//
parameter TIMES1=0, TIMES3=1, TIMES7=2, TIMES8=3;
reg [1:0] state;
reg [7:0] d_r;
always @(posedge clk or negedge rst) begin
if(~rst) begin
input_grant <= 1'b0;
out <= 11'b0;
state <= TIMES1;
end
else begin
case(state)
TIMES1: begin
input_grant <= 1'b1;
out <= d;
state <= TIMES3;
d_r <= d;
end
TIMES3: begin
input_grant <= 1'b0;
out <= (d_r<<1) + d_r;
state <= TIMES7;
d_r <= d_r;
end
TIMES7: begin
input_grant <= 1'b0;
out <= (d_r<<3) - d_r;
state <= TIMES8;
d_r <= d_r;
end
TIMES8: begin
input_grant <= 1'b0;
out <= d_r<<3;
state <= TIMES1;
d_r <= d_r;
end
default: begin
input_grant <= 1'b0;
out <= 11'b0;
state <= TIMES1;
d_r <= 8'b0;
end
endcase
end
end
//*************code***********//
endmodule
简析
题目给出的波形图中,输入d==128
被忽略,也就是上一个输入的8倍输出之后,新的输入才是有效的。所以本状态机只在state=TIMES1
时更新d_r
。input_grant
只与当前的状态有关,而out
在state==TIMES1
时,与输入也有关,所以这个状态机严格上是Mealy状态机。
Verilog篇题解 文章被收录于专栏
本人对牛客网verilog篇题目一些理解