列表

详情


VL44. 根据状态转移写状态机-二段式

描述


题目描述:   
 

如图所示为两种状态机中的一种,请根据状态转移图写出代码,状态转移线上的0/0等表示的意思是过程中data/flag的值。

要求:

1、 必须使用对应类型的状态机

2、 使用二段式描述方法

注意rst低电平复位

信号示意图:

波形示意图:

激励描述如下:


输入描述

输入信号 clk rst data 
类型 wire

输出描述

输出信号  flag
类型  reg

原站题解

Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06

`timescale 1ns/1ns

module fsm2(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);

//*************code***********//
 parameter s0=5'b00001,s1=5'b00010,s2=5'b00100,s3=5'b01000,s4=5'b10000;
    reg [4:0]c_s,n_s;
    always@(posedge clk or negedge rst)
        begin
            if(~rst)
                c_s <= s0;
            else
                c_s <= n_s;
        end
    always@(*)
       begin
           if(~rst)
               begin
               flag = 1'b0;
               n_s = s0;
               end
           else
               begin
           case(c_s)
            s0:begin
                if(data)
                    n_s = s1;
                else
                    n_s = s0;
                flag = 1'b0;
            end
            s1:begin
                if(data)
                    n_s = s2;
                else
                    n_s = s1;
                flag = 1'b0;
            end
            s2:begin
                if(data)
                    n_s = s3;
                else
                    n_s = s2;
                flag = 1'b0;
            end
            s3:begin
                if(data)
                    n_s = s4;
                else
                    n_s = s3;
                flag = 1'b0;
            end   
            s4:begin
                if(data)
                    n_s = s1;
                else
                    n_s = s0;
                flag = 1'b1;
            end    
               
            default: begin
                n_s = s0;
                flag = 1'b0;
            end
           endcase
               end
       end
  

//*************code***********//
endmodule

Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06

`timescale 1ns/1ns

module fsm2(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);

//*************code***********//
parameter	S0 = 5'b0_0001;
	    parameter	S1 = 5'b0_0010;
	    parameter	S2 = 5'b0_0100;
	    parameter	S3 = 5'b0_1000;
	    parameter	S4 = 5'b1_0000;

	    reg 	[4:0]	state;
	    reg		[4:0]	nstate;
	    always @(posedge clk or posedge rst) begin
	    	if (rst == 1'b0) begin
	    		state <= S0;
	    	end
	    	else begin
	    		state <= nstate;
	    	end
	    end

    always @(*) begin
	    	if (rst == 1'b0) begin
	    		nstate <= S0;
                flag <= 0;
	    	end
	    	else begin
	    		case(state)
	    			S0 : begin
	    				nstate <= (data == 1) ? S1 : S0;
	    				flag <= 1'b0;
	    			end
	    			S1 : begin
	    				nstate <= (data == 1) ? S2 : S1;
	    				flag <= 1'b0;
	    			end
	    			S2 : begin
	    				nstate <= (data == 1) ? S3 : S2;
	    				flag <= 1'b0;
	    			end
	    			S3 : begin
	    				nstate <= (data == 1) ? S4 : S3;
	    				flag <= 1'b0;
	    			end
	    			S4 : begin
	    				nstate <= (data == 1) ? S1 : S0;
	    				flag <= 1'b1;
	    			end

	    		default : begin
	    				nstate <= S0;
	    				flag <= 1'b0;
	    			end
	    		endcase
	    	end
	    end
//*************code***********//
endmodule

Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06

`timescale 1ns/1ns

module fsm2(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);

//*************code***********//
    parameter    S0    =    5'b00001;
    parameter    S1    =    5'b00010;
    parameter    S2    =    5'b00100;
    parameter    S3    =    5'b01000;
    parameter    S4    =    5'b10000;
    
    reg    [4:0]    state;
    reg    [4:0]    n_state;
    
    always@(posedge clk or negedge rst)
        if(!rst)
            state <= S0;
        else 
            state <= n_state;
    
    
    always@(*)
        if(!rst) begin
            n_state <= S0;
            flag <= 1'b0;
        end
    else if(state==S0) begin
        n_state <= data?S1:S0;    
        flag <= 1'b0;
    end
    else if(state==S1) begin
        n_state <= data?S2:S1;    
        flag <= 1'b0;
    end
    else if(state==S2) begin
        n_state <= data?S3:S2;    
        flag <= 1'b0;
    end
    else if(state==S3) begin
        n_state <= data?S4:S3;    
        flag <= 1'b0;
    end
    else if(state==S4) begin
        n_state <= data?S1:S0;    
        flag <= 1'b1;
    end


//*************code***********//
endmodule

Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06

`timescale 1ns/1ns

module fsm2(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);

//*************code***********//
    reg [3:0]state;
    reg [3:0]next_state;
    parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b100,s4=3'b101;
    always@(posedge clk or negedge rst)
        if(!rst)
            state<=s0;
    else state<=next_state;
    always@(*)begin
        case(state)
            s0:begin
                next_state<=(data==1)?s1:s0;
                flag<=0;
            end
            s1:begin
                next_state<=(data==1)?s2:s1; 
                flag<=0;
            end
            s2:begin
                next_state<=(data==1)?s3:s2;
                flag<=0;
            end
            s3:begin
                next_state<=(data==1)?s4:s3;
                flag<=0;
            end
            s4:begin
                next_state<=(data==1)?s1:s0;
                flag<=1;
            end
            default:begin
                next_state<=s0;
                flag<=0;
            end
        endcase
    end
//*************code***********//
endmodule

Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06

`timescale 1ns/1ns


module fsm2(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);

    parameter S0 = 'd0, S1 = 'd1, S2 = 'd2, S3 = 'd3 ,S4 = 'd4 ;
    reg [2:0]	current_state;
    reg [2:0]	next_state;
	
    always@(posedge clk or negedge rst)begin
        if(rst == 1'b0)begin
            current_state <= S0;
        end
        else begin
            current_state <= next_state;
        end
    end   
    
    always@(*)begin
        case(current_state)
            S0:begin
                next_state = data ? S1 : S0;
				flag = 1'b0; 
            end
            S1:begin
                next_state = data ? S2 : S1;
				flag = 1'b0;
            end
            S2:begin
                next_state = data ? S3 : S2;
				flag = 1'b0;
            end
            S3:begin
                next_state = data ? S4 : S3;
				flag = 1'b0;
            end
			S4:begin
			    next_state = data ? S1 : S0;
				flag = 1'b1;
			end
            default:begin  
				next_state = S0;
				flag = 1'b0;				
			end
        endcase
    end

endmodule

上一题