data/full_repos/permissive/120024308/src/LCDController/command_fsm.v
data/verilator_xmls/733807d7-5317-45b0-a1b0-cbf2d99b2ddc.xml
module CmdFSM(
                   input wire 	     clk,
                   input wire 	     reset,
                   input wire 	     enable,
                   input wire [19:0] count,
                   input wire [9:0]  buffer,
                   input wire 	     next_command,
                   output reg 	     req_command,
                   output reg 	     count_reset,
                   output reg [3:0]  sf_d,
                   output reg 	     lcd_e,
                   output wire 	     lcd_rs,
		   output wire 	     lcd_rw,
                   output wire 	     sf_ce0
                   );

   assign sf_ce0 = 0;
   reg [2:0]                         state, next_state;

   assign lcd_rs = buffer[8];
   assign lcd_rw = buffer[9];
   

   always @(posedge clk)
     if(!enable)
       begin
          state <=`CMD_DEFAULT;
       end
     else
       begin
          begin
             if(reset)
               begin
                  state <= `HALT;
               end
             else
               begin
                  state <= next_state;
               end
          end
     end

   always @(*)
     begin
        count_reset = 0;
        req_command = 0;
        lcd_e = 0;
        case(state)
          `HALT:
            begin
	       sf_d = buffer[7:4];
               if(next_command)
                 begin


                    count_reset = 1;
                    next_state = `INITIAL_WAIT;
                 end
               else
                 begin
                    next_state = `HALT;
                 end
            end
	  `INITIAL_WAIT:
	    begin
	       sf_d = buffer[7:4];
	       if (count >4)
		 begin
		    next_state = `UPPER;
		    count_reset = 1;
		 end
	       else
		 begin
		    next_state = `INITIAL_WAIT;
		 end
	    end

	       
          `UPPER:
            begin
               lcd_e = 1;
               sf_d = buffer[7:4];
               if(count > 13)
                 begin
                    next_state = `SMALL_WAIT;
                    count_reset = 1;
                 end
               else
                 begin
                    next_state = `UPPER;
                 end
            end
          `SMALL_WAIT:
            begin
               if(count > 49)
                 begin
		    sf_d = buffer[3:0];
                    count_reset = 1;
                    next_state = `LOWER;
                 end
               else
                 begin
		    if (count > 35)
		      begin
			 sf_d = buffer[3:0];	 
		      end
		    else
		      begin
			 sf_d = buffer[7:4];
		      end
                    next_state = `SMALL_WAIT;
                 end
            end
          `LOWER:
            begin
	       sf_d = buffer[3:0];
               lcd_e = 1;
               if(count > 13)
                 begin
		    if(buffer != 10'b1)
		      begin
			 count_reset = 1;
			 req_command = 1;
			 next_state = `BIG_WAIT;
		      end
		    else
		      begin 
			 count_reset = 1;
			 req_command = 1;
			 next_state = `GLORIOUS_WAIT;
		      end
                 end
               else
                 begin
                    next_state = `LOWER;
                 end
            end
          `BIG_WAIT:
            begin
               if(count > 1999)
                 begin
		    sf_d = buffer[7:4];
                    if(next_command)
                      begin
                         count_reset = 1;
                         next_state = `UPPER;
                      end
                    else
                      begin
                         next_state = `HALT;
                      end
                 end
               else
                 begin
		    if (count > 1980)
		      begin
			 sf_d = buffer[7:4];
		      end
		    else begin
		       sf_d = buffer[3:0];
		    end
                    next_state = `BIG_WAIT;
                 end
            end
	  `GLORIOUS_WAIT:
            begin
               if(count > 820000)
                 begin
		    sf_d = buffer[7:4];
                    if(next_command)
                      begin
                         count_reset = 1;
                         next_state = `UPPER;
                      end
                    else
                      begin
                         next_state = `HALT;
                      end
                 end
               else
                 begin
		    if (count > 819200)
		      begin
			 sf_d = buffer[7:4];
		      end
		    else begin
		       sf_d = buffer[3:0];
		    end
		    
                    next_state = `GLORIOUS_WAIT;
                 end
            end
          `CMD_DEFAULT:
            begin
               next_state = `HALT;
	       sf_d = 0;
            end
          default:
            begin
               next_state = `CMD_DEFAULT;
	       sf_d = 0;
            end
        endcase
     end
endmodule
module CmdFSM(
                   input wire 	     clk,
                   input wire 	     reset,
                   input wire 	     enable,
                   input wire [19:0] count,
                   input wire [9:0]  buffer,
                   input wire 	     next_command,
                   output reg 	     req_command,
                   output reg 	     count_reset,
                   output reg [3:0]  sf_d,
                   output reg 	     lcd_e,
                   output wire 	     lcd_rs,
		   output wire 	     lcd_rw,
                   output wire 	     sf_ce0
                   );


   assign sf_ce0 = 0;
   reg [2:0]                         state, next_state;

   assign lcd_rs = buffer[8];
   assign lcd_rw = buffer[9];
   

   always @(posedge clk)
     if(!enable)
       begin
          state <=`CMD_DEFAULT;
       end
     else
       begin
          begin
             if(reset)
               begin
                  state <= `HALT;
               end
             else
               begin
                  state <= next_state;
               end
          end
     end

   always @(*)
     begin
        count_reset = 0;
        req_command = 0;
        lcd_e = 0;
        case(state)
          `HALT:
            begin
	       sf_d = buffer[7:4];
               if(next_command)
                 begin


                    count_reset = 1;
                    next_state = `INITIAL_WAIT;
                 end
               else
                 begin
                    next_state = `HALT;
                 end
            end
	  `INITIAL_WAIT:
	    begin
	       sf_d = buffer[7:4];
	       if (count >4)
		 begin
		    next_state = `UPPER;
		    count_reset = 1;
		 end
	       else
		 begin
		    next_state = `INITIAL_WAIT;
		 end
	    end

	       
          `UPPER:
            begin
               lcd_e = 1;
               sf_d = buffer[7:4];
               if(count > 13)
                 begin
                    next_state = `SMALL_WAIT;
                    count_reset = 1;
                 end
               else
                 begin
                    next_state = `UPPER;
                 end
            end
          `SMALL_WAIT:
            begin
               if(count > 49)
                 begin
		    sf_d = buffer[3:0];
                    count_reset = 1;
                    next_state = `LOWER;
                 end
               else
                 begin
		    if (count > 35)
		      begin
			 sf_d = buffer[3:0];	 
		      end
		    else
		      begin
			 sf_d = buffer[7:4];
		      end
                    next_state = `SMALL_WAIT;
                 end
            end
          `LOWER:
            begin
	       sf_d = buffer[3:0];
               lcd_e = 1;
               if(count > 13)
                 begin
		    if(buffer != 10'b1)
		      begin
			 count_reset = 1;
			 req_command = 1;
			 next_state = `BIG_WAIT;
		      end
		    else
		      begin 
			 count_reset = 1;
			 req_command = 1;
			 next_state = `GLORIOUS_WAIT;
		      end
                 end
               else
                 begin
                    next_state = `LOWER;
                 end
            end
          `BIG_WAIT:
            begin
               if(count > 1999)
                 begin
		    sf_d = buffer[7:4];
                    if(next_command)
                      begin
                         count_reset = 1;
                         next_state = `UPPER;
                      end
                    else
                      begin
                         next_state = `HALT;
                      end
                 end
               else
                 begin
		    if (count > 1980)
		      begin
			 sf_d = buffer[7:4];
		      end
		    else begin
		       sf_d = buffer[3:0];
		    end
                    next_state = `BIG_WAIT;
                 end
            end
	  `GLORIOUS_WAIT:
            begin
               if(count > 820000)
                 begin
		    sf_d = buffer[7:4];
                    if(next_command)
                      begin
                         count_reset = 1;
                         next_state = `UPPER;
                      end
                    else
                      begin
                         next_state = `HALT;
                      end
                 end
               else
                 begin
		    if (count > 819200)
		      begin
			 sf_d = buffer[7:4];
		      end
		    else begin
		       sf_d = buffer[3:0];
		    end
		    
                    next_state = `GLORIOUS_WAIT;
                 end
            end
          `CMD_DEFAULT:
            begin
               next_state = `HALT;
	       sf_d = 0;
            end
          default:
            begin
               next_state = `CMD_DEFAULT;
	       sf_d = 0;
            end
        endcase
     end
endmodule