VL43. 根据状态转移写状态机-三段式
描述
如图所示为两种状态机中的一种,请根据状态转移图写出代码,状态转移线上的0/0等表示的意思是过程中data/flag的值。
要求:
1、 必须使用对应类型的状态机
2、 使用三段式描述方法,输出判断要求要用到对现态的判断
注意rst为低电平复位
信号示意图:
输入描述
输入信号 clk rst data输出描述
输出信号 flagVerilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06
`timescale 1ns/1ns module fsm1( input wire clk , input wire rst , input wire data , output reg flag ); //*************code***********// parameter S0 = 2'b00, S1 = 2'b01, S2 = 2'b10, S3 = 2'b11; reg [1:0] cs,ns; always@(posedge clk or negedge rst) if(!rst) cs <= S0; else cs <= ns; always@(*)begin if(!rst) ns <= S0; else case(cs) S0:ns <=data?S1:S0; S1:ns <=data?S2:S1; S2:ns <=data?S3:S2; S3:ns <=data?S0:S3; default:ns <= S0; endcase end always@(posedge clk or negedge rst) if(!rst) flag <= 1'b0; else if((cs == S3)&&(data == 1'b1)) flag <= 1'b1; else flag <= 1'b0; //*************code***********// endmodule
Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06
`timescale 1ns/1ns module fsm1( input wire clk , input wire rst , input wire data , output reg flag ); //*************code***********// parameter s0=4'b0001,s1=4'b0010,s2=4'b0100,s3=4'b1000; reg [3: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 case(c_s) s0:begin if(data) n_s = s1; else n_s = s0; end s1:begin if(data) n_s = s2; else n_s = s1; end s2:begin if(data) n_s = s3; else n_s = s2; end s3:begin if(data) n_s = s0; else n_s = s3; end default: begin n_s = s0; end endcase end always@(posedge clk or negedge rst) begin if(~rst) flag <= 'd0; else flag <= ((c_s==s3 )&(data==1'b1))?1'b1:1'b0; end //*************code***********// endmodule
Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06
`timescale 1ns/1ns module fsm1( input wire clk , input wire rst , input wire data , output reg flag ); //*************code***********// parameter S0 = 4'b0001; parameter S1 = 4'b0010; parameter S2 = 4'b0100; parameter S3 = 4'b1000; reg [3:0] cstate; reg [3:0] nstate; always @(posedge clk or negedge rst) begin if (rst == 1'b0) begin cstate <= S0; end else begin cstate <= nstate; end end always @(*) begin case(cstate) S0 : begin if (data == 1) begin nstate <= S1; end else begin nstate <= S0; end end S1 : begin if (data == 1) begin nstate <= S2; end else begin nstate <= S1; end end S2 : begin if (data == 1) begin nstate <= S3; end else begin nstate <= S2; end end S3 : begin if (data == 1) begin nstate <= S0; end else begin nstate <= S3; end end default : nstate <= S0; endcase end always @(posedge clk or negedge rst) begin if (rst == 1'b0) begin flag <= 1'b0; end else if (cstate == S3 && data == 1) begin flag <= 1'b1; end else begin flag <= 1'b0; end end //*************code***********// endmodule
Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06
`timescale 1ns/1ns module fsm1( input wire clk , input wire rst , input wire data , output reg flag ); //*************code***********// parameter S0 = 4'b0001, S1 = 4'b0010, S2 = 4'b0100, S3 = 4'b1000; reg [3:0] state,n_state; always@(posedge clk or negedge rst) if(!rst) state <= S0; else state <= n_state; always@(*) case(state) S0: n_state <= data?S1:S0; S1: n_state <= data?S2:S1; S2: n_state <= data?S3:S2; S3: n_state <= data?S0:S3; default: n_state <= S0; endcase always@(posedge clk or negedge rst) if(!rst) flag <= 1'b0; else if(state==S3 && data==1'b1) flag <= 1'b1; else flag <= 1'b0; //*************code***********// endmodule
Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06
`timescale 1ns/1ns module fsm1( input wire clk , input wire rst , input wire data , output reg flag ); //*************code***********// reg [2:0] cur_sta; reg [2:0] nex_sta; parameter S0 = 'd1, S1 = 'd2, S2 = 'd3, S3 = 'd4; //FMS1 always@(posedge clk or negedge rst) if (!rst) cur_sta <= S0; else cur_sta <= nex_sta; //FMS2 always@(*) begin case(cur_sta) S0: nex_sta = (data==1'b1)?S1:S0; S1: nex_sta = (data==1'b1)?S2:S1; S2: nex_sta = (data==1'b1)?S3:S2; S3: nex_sta = (data==1'b1)?S0:S3; default: nex_sta = S0; endcase end //FMS3 always@(posedge clk or negedge rst) begin if (!rst) flag <= 1'b0; else if (cur_sta==S3) begin if(data==1) flag <= 1'b1; else flag <= 1'b0; end else flag <= 1'b0; end //*************code***********// endmodule