上期答案
[172] 编写verilog代码,检测序列10110
首先设计状态机。
- 没有检测到序列
- 检测到1
- 检测到10
- 检测到101
- 检测到1011
状态机如下
代码语言:javascript复制module seq_detector(z,x,clock,reset);
output z;
input x,clock;
input reset; //active high
reg [2:0] state,nextstate;
parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100;
always @ (posedge clock) begin
if(reset) begin
state <=s0;
nextstate<=s0;
end else begin
state<=nextstate;
end
end
always @ (x or state)
case(state)
s0: if(x) nextstate=s1; else nextstate=s0;
s1: if(x) nextstate=s1; else nextstate=s2;
s2: if(x) nextstate=s3; else nextstate=s0;
s3: if(x) nextstate=s4; else nextstate=s2;
s4: if(x) nextstate=s1; else nextstate=s2;
endcase
always @ (x or state)
case(state)
s4: if(x) z=1'b0; else z=1'b1;
s0,s1,s2,s3: z=1'b0;
endcase
endmodule
[173] 写一段verilog代码,根据输入的n计算斐波那契数列
斐波那契数列是一种数列,每一项是通过将前两项相加而得到的。从0和1开始,顺序为0、1、1、2、3、5、8、13、21、34,依此类推。通常,表达式为xn = xn-1 xn-2。假设最大值n = 256,以下代码将生成第n个斐波那契数。值“n”作为输入传递给模块(nth_number)
代码语言:javascript复制module fibonacci(input clock, reset, input [7:0] nth_number, output [19:0] fibonacci_number, output number_ready);
reg [19:0] previous_value, current_value;
reg [7:0] internal_counter;
reg number_ready_r;
always @(posedge clock or posedge reset) begin
if(reset) begin
previous_value <='d0; //1st Fibonacci Number
current_value <='d1; //2nd Fibonacci Number
internal_counter <='d1;
number_ready_r <= 0;
end else begin
if (internal_counter == (nth_number-2)) begin
number_ready_r <= 1;
end else begin
internal_counter <= internal_counter 1;
current_value <= current_value previous_value;
previous_value <= current_value;
number_ready_r <= 0;
end
end
end
assign fibonacci_number = current_value;
assign number_ready = number_ready_r
endmodule
你答对了吗
本期题目
[174] 写一段verilog代码,用半加器组成全加器
[175] verilog中的task和function有什么区别?
欢迎在留言区给出你的答案,正确答案将在下一期公布,或者到下面的文章获取答案