Verilog刷题笔记56

题目:

Exams/2014 q3fsm

Consider a finite state machine with inputs s and w. Assume that the FSM begins in a reset state called A, as depicted below. The FSM remains in state A as long as s = 0, and it moves to state B when s = 1. Once in state B the FSM examines the value of the input w in the next three clock cycles. If w = 1 in exactly two of these clock cycles, then the FSM has to set an output z to 1 in the following clock cycle. Otherwise z has to be 0. The FSM continues checking w for the next three clock cycles, and so on. The timing diagram below illustrates the required values of z for different values of w.

Use as few states as possible. Note that the s input is used only in state A, so you need to consider just the w input.

解题:

cpp 复制代码
module top_module (
    input clk,
    input reset,   // Synchronous reset
    input s,
    input w,
    output z
);

    parameter A=0,B=1,B1=2,B2=3;
    reg [2:0]state,next_state;
    reg [2:0]cnt;
    always@(posedge clk)begin
        if(reset)
            state=A;
        else
            state=next_state;
    end
    always@(*)begin
        case(state)
            A:next_state=s?B:A;
            B:next_state=B1;
            B1:next_state=B2;
            B2:next_state=B;
            default:next_state=A;
        endcase
    end
    always@(posedge clk)begin
        if(reset)begin
            cnt=0;
        end
        else begin
            case(state)
                A:cnt=0;
                B: cnt=w;
                B1:cnt=w?cnt+1:cnt;
                B2:cnt=w?cnt+1:cnt;
                default:cnt=0;
            endcase
        end
    end
    assign z=((state==B)&(cnt==2))?1:0;

                
endmodule

结果正确:

网上看到另一种解法,记录一下,类似FIFO思想,先进先出。

cpp 复制代码
module top_module (
    input clk,
    input reset,   // Synchronous reset
    input s,
    input w,
    output z
);

	localparam  A  = 1'b0,
			    B  = 1'b1;	
	reg [1:0] state,next_state;
	reg [2:0] data;
	reg [1:0] cnt;
	
	always@(posedge clk)begin
		if(reset)
			state <= A;
		else 
			state <= next_state; 
	end
	
	always@(*)begin
		case (state)
			A: next_state = s?B:A;
			B: next_state = B;
        endcase
	end
	
	always@(posedge clk)begin
		if(reset)
		data <= 3'b0;
		else case (next_state)		
		A: data <= 3'b0;
		B: begin
				data[0] <= w;
				data[1] <= data[0];
				data[2] <= data[1];	
		   end	
        endcase
	end
	
	always@(posedge clk)begin
		if(reset)
			cnt <= 2'h0;
		else if(state == A)
			cnt <= 2'h0;
		else if(cnt==2'h3&&(state == B))
			cnt <= 2'h1;
		else 
			cnt <= cnt+2'h1;
	end
	
	assign z = ((data==3'b011)|(data==3'b101)|(data==3'b110))&&(cnt==2'h3);  
	
endmodule
相关推荐
深圳老胡1 小时前
STM32C5A3ZG 网络调试总结:从启动失败到连续 Ping 稳定
笔记·stm32·单片机·代码规范
职豚求职小程序刷题1 小时前
宁德时代社招高岗晋升测评笔试改版了
笔记
CarIise1 小时前
JDBC 与数据库事务核心
数据库·笔记
鬼手点金2 小时前
KV Cache 未必是一次性的:可编辑、可组合的“笔记”
java·人工智能·笔记·学习·自动化·1024程序员节
摇滚侠2 小时前
《On Java 中文版 基础卷》阅读笔记 初始化和清理 08
java·开发语言·笔记
dire_7773 小时前
TH26027A开尔文夹子线缆问题导致同惠TH2822电桥测量不准
笔记
宝绿特塑业再生科技3 小时前
板材收缩率不稳,户外家具装配尺寸为何对不上:从切割尺寸变化到接缝不一的系统性排查与对策
笔记·科技·学习·生活·户外家具·hdpe再生颗粒·hdpe
~城南花已开~4 小时前
Intel E810 SR-IOV VF Spoofcheck 防伪造测试-KVM3
服务器·网络·笔记·学习·ubuntu·etl工程师
hss123456784 小时前
天津婚房装修怎么选:从整装、定制到全案落地的技术决策框架
笔记
职豚求职小程序刷题4 小时前
中国平安社招IQ测评+16PF怎么准备?
笔记