`timescale 1ns / 1ps `define space 2'b00 `define caseBegin 2'b01 `define caseEnd 2'b10 `define caseNo 2'b11 `define ini 3'b000 `define B 3'b001 `define BE 3'b010 `define BEG 3'b011 `define BEGI 3'b100 `define BEGIN 3'b101 `define E 3'b001 `define EN 3'b010 `define END 3'b011 `define NO 3'b111 module BlockChecker( input clk, input reset, input [7:0] in, output result ); reg [2:0]tem1; //judge begin reg [2:0]tem2; //judge end reg [1:0]control; reg [31:0]stack; reg judge; assign result = stack == 32'd0 ? 1'b1 : 1'b0; always@(posedge clk or posedge reset)begin if(reset == 1)begin control <= `space; tem1 <= `ini; tem2 <= `ini; judge <= 1'b0; stack <= 32'd0; end else begin if(in == " ")begin if(control == `caseEnd && tem2 == `END && $signed(stack) < 0)begin judge <= 1'b1; control <= `caseNo; end else begin control <= `space; end end else if(judge == 1'b1)begin control <= `caseNo; end else if(control == `space)begin if(in == "B" || in == "b")begin control <= `caseBegin; tem1 <= `B; end if(in == "E" || in == "e")begin control <= `caseEnd; tem2 <= `E; end end else if(control == `caseBegin)begin case(tem1) `B:begin if(in == "E" || in == "e")begin tem1 <= `BE; end else begin tem1 <= `NO; control <= `caseNo; end end `BE:begin if(in == "G" || in == "g")begin tem1 <= `BEG; end else begin tem1 <= `NO; control <= `caseNo; end end `BEG:begin if(in == "I" || in == "i")begin tem1 <= `BEGI; end else begin tem1 <= `NO; control <= `caseNo; end end `BEGI:begin if(in == "N" || in == "n")begin tem1 <= `BEGIN; if(judge == 1'b0) stack <= stack + 1; end else begin tem1 <= `NO; control <= `caseNo; end end `BEGIN:begin if(in != " ")begin stack <= stack - 1; tem1 <= `NO; control <= `caseNo; end end `NO:begin tem1 <= `NO; control <= `caseNo; end endcase end else if (control == `caseEnd)begin case(tem2) `E:begin if(in == "N" || in == "n")begin tem2 <= `EN; end else begin tem2 <= `NO; control <= `caseNo; end end `EN:begin if(in == "D" || in == "d")begin tem2 <= `END; stack <= stack - 1; end else begin tem2 <= `NO; control <= `caseNo; end end `END:begin if(in != " ")begin stack <= stack + 1; tem2 <= `NO; control <= `caseNo; end end `NO:begin tem2 <= `NO; control <= `caseNo; end endcase end else if(control == `caseNo)begin end end end endmodule