HDLBits训练6
时间:2024.12.25
Rotate100
错误代码
module top_module(
input clk,
input load,
input [1:0] ena,
input [99:0] data,
output reg [99:0] q);
always@(posedge clk)
begin
if(load) q<=data;
else begin
case(ena)
2'b01:q=q>>1;
2'b10:q=q<<1;
default:q<=q;
endcase
end
end
endmodule
运行结果
正确代码
module top_module(
input clk,
input load,
input [1:0] ena,
input [99:0] data,
output reg [99:0] q);
always@(posedge clk)begin
if(load)begin
q<=data;
end else begin
case(ena)
2'b00:q<=q;
2'b01:q<={q[0],q[99:1]};
2'b10:q<={q[98:0],q[99]};
2'b11:q<=q;
default:q<=q;
endcase
end
end
endmodule
运行结果
Fsm1
代码
module top_module(
input clk,
input areset, // Asynchronous reset to state B
input in,
output out);//
parameter A=0, B=1;
reg state, next_state;
always @(*) begin // This is a combinational always block
case(state) // State transition logic
A:next_state=in?A:B;
B:next_state=in?B:A;
endcase
end
always @(posedge clk, posedge areset) begin // This is a sequential always block
if(areset) state<=B;// State flip-flops with asynchronous reset
else state<=next_state;
end
assign out=(state==B)?1'b1:1'b0;
// Output logic
// assign out = (state == ...);
endmodule
按照Fsm1的逻辑书写也可以
module top_module(
input clk,
input reset,
input in,
output out);//
parameter A=0, B=1;
reg state, next_state;
always @(*) begin // This is a combinational always block
case(state) // State transition logic
A:next_state=in?A:B;
B:next_state=in?B:A;
endcase
end
always @(posedge clk) begin // This is a sequential always block
if(reset) state<=B;// State flip-flops with asynchronous reset
else state<=next_state;
end
assign out=(state==B)?1'b1:1'b0;
// Output logic
// assign out = (state == ...);
endmodule
运行结果
Alwaysblock2
代码
// synthesis verilog_input_version verilog_2001
module top_module(
input clk,
input a,
input b,
output wire out_assign,
output reg out_always_comb,
output reg out_always_ff );
assign out_assign=a^b;
always@(posedge clk)
begin
out_always_ff=a^b;
end
always@(*)
out_always_comb=a^b;
endmodule
运行结果
Fsm1s
代码
// Note the Verilog-1995 module declaration syntax here:
module top_module(clk, reset, in, out);
input clk;
input reset; // Synchronous reset to state B
input in;
output out;
reg out;
parameter A=1'b0, B=1'b1;
reg present_state, next_state;
//状态转移
always @(posedge clk) begin
if (reset) begin
present_state <= B;
end else begin
present_state <= next_state;
end
end
//状态转移逻辑
always @(*) begin
case( {in,present_state} )
{1'b0,A}: next_state <= B;
{1'b0,B}: next_state <= A;
{1'b1,A}: next_state <= A;
{1'b1,B}: next_state <= B;
default: next_state <= present_state;
endcase
end
//状态输出
always @(*) begin
out = present_state;
end
endmodule
运行结果
Fsm2
代码
module top_module(
input clk,
input areset, // Asynchronous reset to OFF
input j,
input k,
output out); //
parameter OFF=0, ON=1;
reg state, next_state;
always @(*) begin
if(state==OFF)
next_state<=j?ON:OFF;
else next_state<=k?OFF:ON;
end
always @(posedge clk, posedge areset) begin
if(areset) state<=OFF;
else state<=next_state;
end
assign out=state;
endmodule
module top_module(
input clk,
input areset, // Asynchronous reset to OFF
input j,
input k,
output out);
parameter OFF=1'b0, ON=1'b1;
reg state, next_state;
//状态转移
always @(posedge clk or posedge areset) begin
if(areset)begin
state <= OFF;
end else begin
state <= next_state;
end
end
//状态转移逻辑
always @(*) begin
if(j==1 && state==OFF) begin
next_state = ON; //OFF→ON
end else if(k==1 && state==ON) begin
next_state = OFF; //ON→OFF
end else begin
next_state = state; //状态不变
end
end
//状态输出
always @(*) begin
out = state;
end
endmodule
运行结果
Fsm3comb
代码
module top_module(
input in,
input [1:0] state,
output [1:0] next_state,
output out); //
parameter A=0, B=1, C=2, D=3;
always@(*)
begin
case(state)
A:next_state<=in?B:A;
B:next_state<=in?B:C;
C:next_state<=in?D:A;
D:next_state<=in?B:C;
default:next_state<=A;
endcase
end
// State transition logic: next_state = f(state, in)
assign out=(state==D)? 1'b1:1'b0;
// Output logic: out = f(state) for a Moore state machine
endmodule
运行结果
Fsm3onehot
代码
module top_module(
input in,
input [3:0] state,
output [3:0] next_state,
output out);
parameter A=0, B=1, C=2, D=3;
//状态转移逻辑:采用独热码,写出下一状态每个位的表达式
assign next_state[A] = state[A]&(~in) | state[C]&(~in);
assign next_state[B] = state[A]&in | state[B]&in | state[D]∈
assign next_state[C] = state[B]&(~in) | state[D]&(~in);
assign next_state[D] = state[C]∈
//状态输出
assign out = state[D];
endmodule
整体功能概述
这段代码实现了一个简单的有限状态机(FSM)的逻辑部分,根据输入in
以及当前状态state
来确定下一状态next_state
,同时根据当前状态产生相应的输出out
。该有限状态机使用了独热码(One-Hot Encoding)来对状态进行编码,也就是每个状态用一个单独的位来表示,在任意时刻只有一位为1
,其余位为0
。
状态转移逻辑
-
next_state[A]
的逻辑:assign next_state[A] = state[A]&(~in) | state[C]&(~in);
这行代码确定了在下一时刻状态转移到状态A
(也就是next_state
中表示A
的位变为1
)的条件。从逻辑表达式来看,有两种情况会使得下一状态变为A
:- 当当前状态就是
A
(即state[A]
为1
),并且输入in
为0
(~in
为1
)时,意味着在当前处于A
状态且接收到特定输入(这里是低电平)时,下一时刻保持在A
状态或者跳转到A
状态。 - 当当前状态是
C
(即state[C]
为1
),并且输入in
为0
时,会从C
状态跳转到A
状态。
- 当当前状态就是
-
next_state[B]
的逻辑:assign next_state[B] = state[A]&in | state[B]&in | state[D]∈
对于状态B
,以下三种情况会使得下一时刻状态变为B
:- 当前状态是
A
(state[A]
为1
),并且输入in
为1
时,会从A
状态跳转到B
状态。 - 当前状态已经是
B
(state[B]
为1
),同时输入in
为1
,则保持在B
状态。 - 当前状态是
D
(state[D]
为1
),并且输入in
为1
,会从D
状态跳转到B
状态。
- 当前状态是
-
next_state[C]
的逻辑:assign next_state[C] = state[B]&(~in) | state[D]&(~in);
要使下一状态变为C
,存在两种可能:- 当前状态是
B
(state[B]
为1
),且输入in
为0
,会从B
状态跳转到C
状态。 - 当前状态是
D
(state[D]
为1
),并且输入in
为0
,会从D
状态跳转到C
状态。
- 当前状态是
-
next_state[D]
的逻辑:assign next_state[D] = state[C]∈
仅当当前状态是C
(state[C]
为1
),并且输入in
为1
时,下一时刻状态会变为D
。
状态输出逻辑
assign out = state[D];
这行代码定义了输出逻辑,即输出out
的值取决于当前状态中表示D
的那一位。当当前状态为D
(也就是state[D]
为1
,其他位为0
,因为是独热码表示)时,输出out
为1
,否则为0
。意味着只有在有限状态机处于状态D
时,才会产生有效的输出(这里设为高电平),其他状态下输出为低电平。
总体而言,这段代码通过布尔逻辑表达式清晰地描述了有限状态机的状态转移规则以及输出与状态之间的对应关系,是一种常见的基于 Verilog 语言实现有限状态机逻辑的方式。
运行结果
Fsm3
代码
module top_module(
input clk,
input in,
input areset,
output out);
/*//方法一:非独热码编码,四个state分别为:A=2'd0,B=2'd1,C=2'd2,D=2'd3
reg [1:0]state, next_state;
parameter A=2'd0, B=2'd1, C=2'd2, D=2'd3;
//状态转移
always@(posedge clk or posedge areset)begin
if(areset)begin
state <= A;
end else begin
state <= next_state;
end
end
//状态转移逻辑
always@(*)begin
if(in==0)begin
case(state)
A: next_state = A;
B: next_state = C;
C: next_state = A;
D: next_state = C;
default: next_state = state; //保持当前状态不变
endcase
end else begin
case(state)
A: next_state = B;
B: next_state = B;
C: next_state = D;
D: next_state = B;
default: next_state = state; //保持当前状态不变
endcase
end
end
//状态输出
always@(*)begin
case(state)
A: out = 1'b0;
B: out = 1'b0;
C: out = 1'b0;
D: out = 1'b1;
default: out = out; //保持当前状态不变
endcase
end*/
//方法二:独热码编码,四个state分别为:4'b0001,4'b0010,4'b0100,4'b1000
reg [3:0]state,next_state;
parameter A=4'd0, B=4'd1, C=4'd2, D=4'd3;
//状态转移
always@(posedge clk or posedge areset)begin
if(areset)begin
state <= 4'b0001; //注意复位到state的第一个状态4'b0001
end else begin
state <= next_state;
end
end
//状态转移逻辑
always@(*)begin
next_state[A] = state[A]&(~in) | state[C]&(~in);
next_state[B] = state[A]&in | state[B]&in | state[D]∈
next_state[C] = state[B]&(~in) | state[D]&(~in);
next_state[D] = state[C]∈
end
//状态输出
always@(*)begin
out = state[D];
end
endmodule