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
相关推荐
夏莉莉iy6 分钟前
[MDM 2024]Spatial-Temporal Large Language Model for Traffic Prediction
人工智能·笔记·深度学习·机器学习·语言模型·自然语言处理·transformer
StickToForever14 分钟前
第4章 信息系统架构(三)
经验分享·笔记·学习·职场和发展
零星_AagT1 小时前
Apache-CC6链审计笔记
java·笔记·apache·代码审计
宇寒风暖3 小时前
侯捷 C++ 课程学习笔记:内存管理与工具应用
c++·笔记·学习
云缘若仙4 小时前
directx12 3d+vs2022游戏开发第六章 笔记十一
笔记·directx12 3d
电棍2334 小时前
在wsl环境中配置和开发verilog(一种比较新颖的verilog开发指南)
笔记
非 白5 小时前
【Java】单例模式
java·笔记·单例模式
ZxsLoves5 小时前
【【Systemverilog学习参考 简单的加法器验证-含覆盖率】】
学习·fpga开发
明阳mark5 小时前
Ansible 学习笔记
笔记·学习·ansible
Ronin-Lotus7 小时前
嵌入式硬件篇---数字电子技术中的触发器
嵌入式硬件·fpga开发·触发器·数字电子技术·上位机知识