[HDLBits做题]Sequential Logic --- Finite State Machines (III)

(上接2.5Finite State Machine(II))

2.5.24 Q3a:FSM [Exams/2014 q3fsm]
问题描述

考虑一个输入为s和w的有限状态机。假设FSM从称为A的复位状态开始,如下所示,只要s=0,FSM就保持在状态A,并且当s=1时,A状态转换为B状态。一旦处于B状态,FSM在接下来的三个时钟周期中检查w的值,如果这三个时钟周期中有两个周期w=1,则FSM必须在下一个时钟周期中将输出z设置为1.否则,z必须为0.FSM继续检查接下来的三个时钟周期w,以此类推,下面的时序图说明了不同w值所输出的z值。
使用尽可能少的状态,请注意,s输入仅在状态A中使用,因此只需要考虑w的输入。
时序示例:
在这里插入图片描述

image

分析
状态转移从A到B的条件很简单,主要是当切换到B状态的时候要处理好对w的计数
这里设置两个变量 count用于计数3个时钟周期  num用于计数3个时钟周期中w为1的个数
z的输出则需要满足count=3且num=2的条件下。
代码
module top_module (
    input clk,
    input reset,   // Synchronous reset
    input s,
    input w,
    output z
);
	parameter A=0,B=1;
	integer count,num;
	reg state,next_state;
	
	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 | state==A) begin
			count <= 0 ;
			num <= 0;
		end
		else begin
			if(count <= 2) 
				count <= count + 1;
			else 
				count <= 1;
		
			if (count == 3) 
				num <= w;
			else
				num <= num + w;
		end
	end
	
	assign z = (num==2)&(count==3);

endmodule
2.5.25 Q3b:FSM [Exams/2014 q3bfsm]
问题描述

给定如下所示的状态分配表,实现有限状态机。复位应该将状态机复位为状态000.

Present state
y[2:0]
Next state Y[2:0]Output z
x=0x=1
000 000 000 0
001 001 100 0
010 010 001 0
011 001 010 1
100 011 100 1
代码
module top_module (
    input clk,
    input reset,   // Synchronous reset
    input x,
    output z
); 
	parameter s0=0,s1=1,s2=2,s3=3,s4=4;
	reg [2:0] state,next_state;
	
	always@(posedge clk) begin
		if(reset)
			state <= s0;
		else 
			state <= next_state;
	end
	
	always@(*) begin
		case (state)
			s0	: next_state = x ? s1 : s0;
			s1	: next_state = x ? s4 : s1;
			s2 : next_state = x ? s1 : s2;
			s3 : next_state = x ? s2 : s1;
			s4 : next_state = x ? s4 : s3;
		endcase
	end
	
	assign z = (state == s3) | (state == s4);

endmodule
2.5.26 Q3c:FSM logic [Exams/2014 q3c]
问题描述

给定如下所示状态分配表,实现逻辑函数Y[0]和z

Present state
y[2:0]
Next state Y[2:0]Output z
x=0x=1
000 000 001 0
001 001 100 0
010 010 001 0
011 001 010 1
100 011 100 1
分析

对题意的理解如下:在每个时钟根据y和x推出转移的下一个状态,并输出下一个状态Y[0]与z

代码
module top_module (
    input clk,
    input [2:0] y,
    input x,
    output Y0,
    output z
);
	parameter s0=0,s1=1,s2=2,s3=3,s4=4;
	reg [2:0] next_state;
		
	always@(*) begin
		case(y)
			s0 : next_state = x ? s1 : s0;
			s1 : next_state = x ? s4 : s1;
			s2 : next_state = x ? s1 : s2;
			s3 : next_state = x ? s2 : s1;
			s4 : next_state = x ? s4 : s3;
		endcase
	end
	
	assign Y0 = next_state[0];
	assign z = (y == s3) | (y == s4);

endmodule
2.5.27 Q6b:FSM next-state logic [Exams/m2014 q6b]
问题描述

考虑如下图所示的有限状态机,其有一个输入w和一个输出z:
image
假设您希望使用三个触发器和状态码y[3:1]=000,001,…,101用于状态A、B…F.显示此FSM的状态分配表并推导触发器y[2]的下一个状态的表达式。
只显示y[2]的下一个状态逻辑(这更像是一个有限状态机的问题,而不是一个编程问题)

状态转移表

根据状态转移图画出如下状态转移表:

state
y[3:1]
Next state Y[3:1]Output z
w=0w=1
A B A 0
B C D 0
C E D 0
D F A 0
E E D 1
F C D 1
代码
module top_module (
    input [3:1] y,
    input w,
    output Y2);
	parameter A=0,B=1,C=2,D=3,E=4,F=5;
	reg [3:1] next_state;
	
	always@(*) begin
		case(y)
			A	: next_state = w ? A : B;
			B	: next_state = w ? D : C;
			C	: next_state = w ? D : E;
			D	: next_state = w ? A : F;
			E	: next_state = w ? D : E;
			F	: next_state = w ? D : C;
		endcase
	end
	
	assign Y2 = next_state[2];
	
endmodule
2.5.28 Q6c:FSM one-hot next-state logic [Exams/m2014 q6c]
问题描述

考虑如下图所示的有限状态机,其有一个输入w和一个输出z:
image
对于该题,假设状态分配y[6:1]采用独热码,编码方式为000001、000010、…、100000分别用于表示A、B、…、F。
写出next-state的Y2和Y4的逻辑表达式(假设独热编码,通过检查推导出逻辑方程。测试将使用非一个独热输入进行测试,以确保您没有尝试进行更复杂的操作)

代码
module top_module (
    input [6:1] y,
    input w,
    output Y2,
	 output Y4
	 );
	parameter A=6'b000001,B=6'b000010,C=6'b000100,D=6'b001000,E=6'b010000,F=6'b100000;
	wire [6:1] next_state;
	
	assign next_state[1] = (y[1] & w) | (y[4] & w);
	assign next_state[2] = (y[1] & ~w);
	assign next_state[3] = (y[2] & ~w) | (y[6] & ~w);
	assign next_state[4] = (y[2] & w) | (y[3] & w) | (y[5] & w) | (y[6] & w);
	assign next_state[5] = (y[3] & ~w) | (y[5] & ~w);
	assign next_state[6] = (y[4] & ~w);
	
	assign Y2 = next_state[2];
	assign Y4 = next_state[4];
	
endmodule
2.5.29 Q6:FSM [Exams/m2014 q6]
问题描述

考虑如下图所示的有限状态机,其有一个输入w和一个输出z:
image
实现该有限状态机。

代码
module top_module (
	 input clk,
    input reset,
    input w,
    output z);
	parameter A=0,B=1,C=2,D=3,E=4,F=5;
	reg [3:1] state,next_state;
	
	always@(posedge clk) begin
		if(reset)
			state <= A;
		else
			state <= next_state;
	end
	
	always@(*) begin
		case(state)
			A	: next_state = w ? A : B;
			B	: next_state = w ? D : C;
			C	: next_state = w ? D : E;
			D	: next_state = w ? A : F;
			E	: next_state = w ? D : E;
			F	: next_state = w ? D : C;
		endcase
	end
	
	assign z = (state == E) | (state == F);
	
endmodule
2.5.30 Q2a:FSM [Exams/2012 q2fsm]
问题描述

考虑下图所示状态图
image
编写完整的Verilog代码用于表示这个FSM,使用单独的always块表示状态表和状态触发。使用连续赋值语句或者always语句描述FSM的输出z。状态码按照你想使用的方式设置即可。

代码

直接根据 状态转移图 写即可:

module top_module (
    input clk,
    input reset,   // Synchronous active-high reset
    input w,
    output z
);
	parameter A=0,B=1,C=2,D=3,E=4,F=5;
	reg [3:0] state,next_state;
	
	always@(posedge clk) begin
		if(reset)
			state <= A;
		else
			state <= next_state;
	end

	always@(*) begin
		case(state)
			A	: next_state = w ? B : A;
			B   : next_state = w ? C : D;
			C	: next_state = w ? E : D;
			D	: next_state = w ? F : A;
			E	: next_state = w ? E : D;
			F	: next_state = w ? C : D;
		endcase
	end
	
	assign z = (state == E)|(state == F);
	
endmodule
2.5.31 Q2b:One-hot FSM equations [Exams/2012 q2b]
问题描述

考虑下图所示状态图
image
假设独热码与状态分配如下: y[5:0] = 000001(A), 000010(B), 000100©, 001000(D), 010000(E), 100000(F)
写出信号Y1和Y3的逻辑表达式,它们是状态触发器y[1]和y[3]的输入。

代码
module top_module (
    input [5:0] y,
    input w,
    output Y1,
    output Y3
);
	parameter 	A=6'b000001,
					B=6'b000010,
					C=6'b000100,
					D=6'b001000,
					E=6'b010000,
					F=6'b100000;
	reg [5:0] state,next_state;
	
	assign next_state[0] = (y[0] & ~w) | (y[3] & ~w);
	assign next_state[1] = (y[0] & w);
	assign next_state[2] = (y[1] & w) | (y[5] & w);
	assign next_state[3] = (y[1] & ~w) | (y[2] & ~w) | (y[4] & ~w) | (y[5] & ~w);
	assign next_state[4] = (y[4] & w) | (y[2] & w);
	assign next_state[5] = (y[3] & w);
	
	assign Y1 = next_state[1];
	assign Y3 = next_state[3];

endmodule
2.5.32 Q2a:FSM [Exams/2013 q2afsm]
问题描述

考虑下图所示的状态转移图所描述的FSM:
image
该FSM充当仲裁器电路,其控制三个请求设备对某种类型的资源的访问。每个设备通过设置信号r[i]=1来请求资源,其中每个r[i]都是FSM的输入信号,代表三个器件中的一个。只要没有请求,FSM都保持在A状态。当一个或多个请求发生时,FSM决定哪个设备接收到使用资源的授权,并改变该设备的g[i]信号设置为1的状态。每个g[i]都是FSM的输出。存在优先级系统,其中设备1比设备2有更高的优先级,设备3的优先级最低。因此,如果设备3时FSM处于状态A时发出请求的唯一设备,则设备3将仅接收许可,一旦设备i被FSM给予授权,则只要其请求r[i]=1,该设备就继续接收授权。
编写完整的Verilog代码来表示这个FSM,使用单独的always块描述状态表和状态触发器,使用连续赋值语句或always块描述FSM的输出g[i]。

分析

题意机翻的有点混乱,实际就是描述这个状态转移图

代码
module top_module (
    input clk,
    input resetn,    // active-low synchronous reset
    input [3:1] r,   // request
    output [3:1] g   // grant
); 

	parameter A=0,B=1,C=2,D=3;
	reg [1:0] state , next_state;
	
	always@(posedge clk) begin
		if (!resetn)
			state <= A;
		else 
			state <= next_state;
	end	
	
	always@(*) begin
		case (state)
			A	: begin
				if (r == 3'b000) next_state = A;
				if (r[1] == 1) next_state = B;
				if (r[2:1] == 2'b10) next_state = C;
				if (r == 3'b100) next_state = D;
			end
			B	:	next_state = (r[1] == 1) ? B : A;
			C	:	next_state = (r[2] == 1) ? C : A;
			D 	:  next_state = (r[3] == 1) ? D : A;
		endcase
	end
	
	assign g[3] = (state == D);
	assign g[2] = (state == C);
	assign g[1] = (state == B);
	
endmodule
2.5.33 Q2b:Another FSM [Exams/2013 q2bfsm]
问题描述

考虑一个有限状态机,用于控制某种类型的电机。FSM具有来自电机的输入x和y,并产生控制电机的输出f和g。还有一个时钟输入clk和一个低电平复位输入resetn。
该FSM按照以下方式工作。只要复位输入置位,FSM就停止在起始位置,称为状态A。当复位信号解除时,在下一个时钟沿后,FSM必须在一个时钟周期内将输出f设置为1.然后FSM必须监视x输入。当在三个连续的时钟周期中产生的值为101时,下一个周期中g的输出应设置为1.当g=1时,FSM则监视y输入。如果y在最多两个时钟周期的值为1时,则g将一直保持为高电平直到复位信号。但是如果y在两个时钟周期内没有变为1,则FSM应当永久被设置g=0直到复位信号。

状态转移图

在这里插入图片描述

代码
module top_module (
    input clk,
    input resetn,    // active-low synchronous reset
    input x,
    input y,
    output f,
    output g
); 
	parameter IDLE=0,START=1,A=2,B=3,C=4,D=5,E=6,F=7,G=8;
	reg [3:0] state,next_state;
	
	always@(posedge clk) begin
		if (!resetn)
			state <= IDLE;
		else
			state <= next_state;
	end
	
	always@(*) begin
	case(state)
		IDLE 	: next_state = START;
        START   : next_state = A;
		A		: next_state = x ? B : A;
		B		: next_state = x ? B : C;
		C		: next_state = x ? D : A;
		D		: next_state = y ? F : E;
		E		: next_state = y ? F : G;
		F		: next_state = F;
		G		: next_state = G;
	endcase
	end
    assign f = (state == START);
	assign g = (state == D) | (state == E) | (state == F);
endmodule


这个系列主要是记录一下自己的学习过程和简单的思考过程,参考了许多他人的思路。题目均为HDLBits上的题目,借助翻译器与自己的理解组织了题目描述,如果有问题欢迎批评指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值