data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v
1: b'%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:16: Cannot find include file: common_defines.v\n`include "common_defines.v" \n         ^~~~~~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source,data/full_repos/permissive/114714167/common_defines.v\n             data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source,data/full_repos/permissive/114714167/common_defines.v.v\n             data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source,data/full_repos/permissive/114714167/common_defines.v.sv\n             common_defines.v\n             common_defines.v.v\n             common_defines.v.sv\n             obj_dir/common_defines.v\n             obj_dir/common_defines.v.v\n             obj_dir/common_defines.v.sv\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:20: Define or directive not defined: \'`REC_VAL_BIT_WIDTH\'\n    output reg [`REC_VAL_BIT_WIDTH-1:0] throttle_pwm_value_out,\n                ^~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:23: Define or directive not defined: \'`REC_VAL_BIT_WIDTH\'\n    input  wire [`REC_VAL_BIT_WIDTH-1:0] throttle_pwm_value_in,\n                 ^~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:32: Define or directive not defined: \'`REC_VAL_BIT_WIDTH\'\n    reg [`REC_VAL_BIT_WIDTH-1:0]    latched_throttle;\n         ^~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:33: Define or directive not defined: \'`OPS_BIT_WIDTH\'\n    reg [`OPS_BIT_WIDTH-1:0]        debounced_throttle;\n         ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:34: Define or directive not defined: \'`OPS_BIT_WIDTH\'\n    reg [`OPS_BIT_WIDTH-1:0]        scaled_throttle;\n         ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:35: Define or directive not defined: \'`OPS_BIT_WIDTH\'\n    reg signed [`OPS_BIT_WIDTH-1:0] limited_throttle;\n                ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:36: Define or directive not defined: \'`OPS_BIT_WIDTH\'\n    reg [`OPS_BIT_WIDTH-`REC_VAL_BIT_WIDTH-1:0] trash_bits;\n         ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:36: Define or directive not defined: \'`REC_VAL_BIT_WIDTH\'\n    reg [`OPS_BIT_WIDTH-`REC_VAL_BIT_WIDTH-1:0] trash_bits;\n                        ^~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:37: Define or directive not defined: \'`REC_VAL_BIT_WIDTH\'\n    reg [`REC_VAL_BIT_WIDTH-1:0]    prev_latched_throttle;\n         ^~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:38: Define or directive not defined: \'`REC_VAL_BIT_WIDTH\'\n    reg [`REC_VAL_BIT_WIDTH-1:0]    prev_throttle_pwm_value_out;\n         ^~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:53: Define or directive not defined: \'`FALSE\'\n    reg start_flag = `FALSE;\n                     ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:53: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n    reg start_flag = `FALSE;\n                           ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:55: Define or directive not defined: \'`OPS_BIT_WIDTH\'\n    localparam signed [`OPS_BIT_WIDTH-1:0]\n                       ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:63: Define or directive not defined: \'`FALSE\'\n            start_flag     <= `FALSE;\n                              ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:63: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n            start_flag     <= `FALSE;\n                                    ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:65: Define or directive not defined: \'`TRUE\'\n            start_flag     <= `TRUE;\n                              ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:65: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n            start_flag     <= `TRUE;\n                                   ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:68: Define or directive not defined: \'`FALSE\'\n                start_flag <= `FALSE;\n                              ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:68: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                start_flag <= `FALSE;\n                                    ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:91: Define or directive not defined: \'`TRUE\'\n                    if(imu_ready == `TRUE)\n                                    ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:91: syntax error, unexpected \')\', expecting TYPE-IDENTIFIER\n                    if(imu_ready == `TRUE)\n                                         ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:128: Define or directive not defined: \'`BYTE_ALL_ZERO\'\n            throttle_pwm_value_out      <= `BYTE_ALL_ZERO;\n                                           ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:128: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n            throttle_pwm_value_out      <= `BYTE_ALL_ZERO;\n                                                         ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:129: Define or directive not defined: \'`BYTE_ALL_ZERO\'\n            latched_throttle            <= `BYTE_ALL_ZERO;\n                                           ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:129: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n            latched_throttle            <= `BYTE_ALL_ZERO;\n                                                         ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:132: Define or directive not defined: \'`BYTE_ALL_ZERO\'\n            prev_latched_throttle       <= `BYTE_ALL_ZERO;\n                                           ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:132: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n            prev_latched_throttle       <= `BYTE_ALL_ZERO;\n                                                         ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:133: Define or directive not defined: \'`BYTE_ALL_ZERO\'\n            prev_throttle_pwm_value_out <= `BYTE_ALL_ZERO;\n                                           ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:133: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n            prev_throttle_pwm_value_out <= `BYTE_ALL_ZERO;\n                                                         ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:139: Define or directive not defined: \'`TRUE\'\n                    complete_signal         <= `TRUE;  \n                                               ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:139: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                    complete_signal         <= `TRUE;  \n                                                    ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:140: Define or directive not defined: \'`FALSE\'\n                    active_signal           <= `FALSE;\n                                               ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:140: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                    active_signal           <= `FALSE;\n                                                     ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:144: Define or directive not defined: \'`FALSE\'\n                    complete_signal         <= `FALSE;\n                                               ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:144: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                    complete_signal         <= `FALSE;\n                                                     ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:145: Define or directive not defined: \'`FALSE\'\n                    active_signal           <= `FALSE;\n                                               ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:145: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                    active_signal           <= `FALSE;\n                                                     ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:156: Define or directive not defined: \'`FALSE\'\n                    complete_signal <= `FALSE;\n                                       ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:156: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                    complete_signal <= `FALSE;\n                                             ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:157: Define or directive not defined: \'`TRUE\'\n                    active_signal   <= `TRUE;\n                                       ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:157: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                    active_signal   <= `TRUE;\n                                            ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:182: Define or directive not defined: \'`FALSE\'\n                    complete_signal <= `FALSE; \n                                       ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:182: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                    complete_signal <= `FALSE; \n                                             ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:183: Define or directive not defined: \'`TRUE\'\n                    active_signal   <= `TRUE;\n                                       ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:183: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                    active_signal   <= `TRUE;\n                                            ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:206: Define or directive not defined: \'`FALSE\'\n                    complete_signal          <= `FALSE;\n                                                ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:206: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                    complete_signal          <= `FALSE;\n                                                      ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:207: Define or directive not defined: \'`TRUE\'\n                    active_signal            <= `TRUE;\n                                                ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/throttle_controller.v:207: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n                    active_signal            <= `TRUE;\n                                                     ^\n%Error: Exiting due to too many errors encountered; --error-limit=50\n'
module throttle_controller
    (
    output reg [`REC_VAL_BIT_WIDTH-1:0] throttle_pwm_value_out,
    output reg complete_signal,
    output reg active_signal,
    input  wire [`REC_VAL_BIT_WIDTH-1:0] throttle_pwm_value_in,
    input  wire tc_enable_n,
    input  wire [2:0] switch_a,
    input  wire imu_ready,
    input  wire start_signal,
    input  wire resetn,
    input  wire us_clk);


    reg [`REC_VAL_BIT_WIDTH-1:0]    latched_throttle;
    reg [`OPS_BIT_WIDTH-1:0]        debounced_throttle;
    reg [`OPS_BIT_WIDTH-1:0]        scaled_throttle;
    reg signed [`OPS_BIT_WIDTH-1:0] limited_throttle;
    reg [`OPS_BIT_WIDTH-`REC_VAL_BIT_WIDTH-1:0] trash_bits;
    reg [`REC_VAL_BIT_WIDTH-1:0]    prev_latched_throttle;
    reg [`REC_VAL_BIT_WIDTH-1:0]    prev_throttle_pwm_value_out;


    localparam
        STATE_INIT          = 7'b0000001,
        STATE_WAITING       = 7'b0000010,
        STATE_DEGLITCH      = 7'b0000100,
        STATE_LINEAR_SCALE  = 7'b0001000,
        STATE_CHANGE_LIMIT  = 7'b0010000,
        STATE_ASSIGN_OUTPUT = 7'b0100000,
        STATE_COMPLETE      = 7'b1000000;


    reg [6:0] state, next_state;

    reg start_flag = `FALSE;
    
    localparam signed [`OPS_BIT_WIDTH-1:0]
        THROTTLE_MID_RANGE_LOW_END = 42,
        THROTTLE_MID_RANGE_HIGH_END = 209,
        THROTTLE_CHANGE_LIMIT = 5;


    always @(posedge us_clk or negedge resetn) begin
        if(!resetn)
            start_flag     <= `FALSE;
        else if(start_signal && !start_flag)
            start_flag     <= `TRUE;
        else if(!start_signal && start_flag) begin
            if(state != STATE_WAITING)
                start_flag <= `FALSE;
        end
        else
            start_flag     <= start_flag;
    end


    always @(posedge us_clk or negedge resetn) begin
        if(!resetn) begin
            state  <= STATE_INIT;
        end
        else begin
            state  <= next_state;
        end
    end


    always @(state or start_flag or imu_ready or resetn) begin
        if (!resetn)
            next_state = STATE_INIT;
        else begin
            case(state)
                STATE_INIT: begin
                    if(imu_ready == `TRUE)
                        next_state = STATE_WAITING;
                    else
                        next_state = STATE_INIT;
                end
                STATE_WAITING: begin
                    if(start_flag)
                        next_state = STATE_DEGLITCH;
                    else
                        next_state = STATE_WAITING;
                end
                STATE_DEGLITCH: begin
                    next_state = STATE_LINEAR_SCALE;
                end
                STATE_LINEAR_SCALE: begin
                    next_state = STATE_CHANGE_LIMIT;
                end
                STATE_CHANGE_LIMIT: begin
                    next_state = STATE_ASSIGN_OUTPUT;
                end
                STATE_ASSIGN_OUTPUT: begin
                    next_state = STATE_COMPLETE;
                end
                STATE_COMPLETE: begin
                    next_state = STATE_WAITING;
                end
                default: begin
                    next_state = STATE_WAITING;
                end
            endcase
        end
    end


    always @(posedge us_clk or negedge resetn) begin
        if(!resetn) begin

            throttle_pwm_value_out      <= `BYTE_ALL_ZERO;
            latched_throttle            <= `BYTE_ALL_ZERO;
            debounced_throttle          <= 0;
            limited_throttle            <= 0;
            prev_latched_throttle       <= `BYTE_ALL_ZERO;
            prev_throttle_pwm_value_out <= `BYTE_ALL_ZERO;

        end
        else begin
            case(state)
                STATE_INIT: begin
                    complete_signal         <= `TRUE;
                    active_signal           <= `FALSE;
                    throttle_pwm_value_out  <=  0;
                end
                STATE_WAITING: begin
                    complete_signal         <= `FALSE;
                    active_signal           <= `FALSE;
                    if(next_state == STATE_DEGLITCH) begin
                        if(throttle_pwm_value_in < 8'd10)
                            latched_throttle <= 8'd0;
                        else if(throttle_pwm_value_in > 8'd250)
                            latched_throttle <= 8'd250;
                        else
                            latched_throttle <= throttle_pwm_value_in;
                    end
                end
                STATE_DEGLITCH: begin
                    complete_signal <= `FALSE;
                    active_signal   <= `TRUE;
                    if(latched_throttle <= 8'd10) begin

                        if(prev_latched_throttle <= 8'd10) begin
                            debounced_throttle    <= latched_throttle;
                            prev_latched_throttle <= latched_throttle;
                        end

                        else if (prev_latched_throttle > 8'd10) begin
                            debounced_throttle    <= prev_latched_throttle;
                            prev_latched_throttle <= latched_throttle;
                        end

                        else begin
                            debounced_throttle    <= latched_throttle;
                            prev_latched_throttle <= latched_throttle;
                        end
                    end

                    else begin
                        prev_latched_throttle <= latched_throttle;
                        debounced_throttle    <= latched_throttle;
                    end
                end
                STATE_LINEAR_SCALE: begin 
                    complete_signal <= `FALSE; 
                    active_signal   <= `TRUE;
                    if (switch_a[0]) begin

                        if(debounced_throttle < THROTTLE_MID_RANGE_LOW_END) 
                            scaled_throttle  <= (8'd2*debounced_throttle); 

                        else if(debounced_throttle > THROTTLE_MID_RANGE_HIGH_END) 
                            scaled_throttle  <= ((8'd2*debounced_throttle)-8'd252); 

                        else 
                            scaled_throttle  <= ((debounced_throttle>>>1)+8'd61); 
                    end
                    else begin

                        if(debounced_throttle < 8'd35)
                            scaled_throttle  <= (8'd4*debounced_throttle); 
                        else


                            scaled_throttle  <= ((debounced_throttle>>2)+8'd128); 
                    end
                end
                STATE_CHANGE_LIMIT: begin
                    complete_signal          <= `FALSE;
                    active_signal            <= `TRUE;

                    if(scaled_throttle < 8'd10)
                        limited_throttle     <= 16'd0;

                    else if(scaled_throttle > (prev_throttle_pwm_value_out + THROTTLE_CHANGE_LIMIT)) begin
                        if((prev_throttle_pwm_value_out + THROTTLE_CHANGE_LIMIT) > 'd250)
                            limited_throttle <= 16'd250;
                        else
                            limited_throttle <= prev_throttle_pwm_value_out + THROTTLE_CHANGE_LIMIT;
                    end

                    else if( prev_throttle_pwm_value_out > (scaled_throttle + THROTTLE_CHANGE_LIMIT)) begin
                        if((prev_throttle_pwm_value_out - THROTTLE_CHANGE_LIMIT) < 'd10)
                            limited_throttle <= 16'd0;
                        else
                            limited_throttle <= prev_throttle_pwm_value_out - THROTTLE_CHANGE_LIMIT;
                    end

                    else begin
                        if(scaled_throttle > 250)
                            limited_throttle <= 16'd250;
                        if(scaled_throttle < 10)
                            limited_throttle <= 16'd0;
                        else
                            limited_throttle <= scaled_throttle;
                    end
                end
                STATE_ASSIGN_OUTPUT: begin
                    complete_signal <= `FALSE;
                    active_signal   <= `TRUE;
                    if(tc_enable_n)
                        throttle_pwm_value_out  <= latched_throttle;
                    else
                        {trash_bits, throttle_pwm_value_out} <= limited_throttle;
                end
                STATE_COMPLETE: begin
                    complete_signal             <= `TRUE;
                    active_signal               <= `FALSE;
                    prev_throttle_pwm_value_out <= throttle_pwm_value_out;
                end
                default: begin
                    throttle_pwm_value_out      <= `BYTE_ALL_ZERO;
                end
            endcase
        end
    end

endmodule
module throttle_controller
    (
    output reg [`REC_VAL_BIT_WIDTH-1:0] throttle_pwm_value_out,
    output reg complete_signal,
    output reg active_signal,
    input  wire [`REC_VAL_BIT_WIDTH-1:0] throttle_pwm_value_in,
    input  wire tc_enable_n,
    input  wire [2:0] switch_a,
    input  wire imu_ready,
    input  wire start_signal,
    input  wire resetn,
    input  wire us_clk);



    reg [`REC_VAL_BIT_WIDTH-1:0]    latched_throttle;
    reg [`OPS_BIT_WIDTH-1:0]        debounced_throttle;
    reg [`OPS_BIT_WIDTH-1:0]        scaled_throttle;
    reg signed [`OPS_BIT_WIDTH-1:0] limited_throttle;
    reg [`OPS_BIT_WIDTH-`REC_VAL_BIT_WIDTH-1:0] trash_bits;
    reg [`REC_VAL_BIT_WIDTH-1:0]    prev_latched_throttle;
    reg [`REC_VAL_BIT_WIDTH-1:0]    prev_throttle_pwm_value_out;


    localparam
        STATE_INIT          = 7'b0000001,
        STATE_WAITING       = 7'b0000010,
        STATE_DEGLITCH      = 7'b0000100,
        STATE_LINEAR_SCALE  = 7'b0001000,
        STATE_CHANGE_LIMIT  = 7'b0010000,
        STATE_ASSIGN_OUTPUT = 7'b0100000,
        STATE_COMPLETE      = 7'b1000000;


    reg [6:0] state, next_state;

    reg start_flag = `FALSE;
    
    localparam signed [`OPS_BIT_WIDTH-1:0]
        THROTTLE_MID_RANGE_LOW_END = 42,
        THROTTLE_MID_RANGE_HIGH_END = 209,
        THROTTLE_CHANGE_LIMIT = 5;


    always @(posedge us_clk or negedge resetn) begin
        if(!resetn)
            start_flag     <= `FALSE;
        else if(start_signal && !start_flag)
            start_flag     <= `TRUE;
        else if(!start_signal && start_flag) begin
            if(state != STATE_WAITING)
                start_flag <= `FALSE;
        end
        else
            start_flag     <= start_flag;
    end


    always @(posedge us_clk or negedge resetn) begin
        if(!resetn) begin
            state  <= STATE_INIT;
        end
        else begin
            state  <= next_state;
        end
    end


    always @(state or start_flag or imu_ready or resetn) begin
        if (!resetn)
            next_state = STATE_INIT;
        else begin
            case(state)
                STATE_INIT: begin
                    if(imu_ready == `TRUE)
                        next_state = STATE_WAITING;
                    else
                        next_state = STATE_INIT;
                end
                STATE_WAITING: begin
                    if(start_flag)
                        next_state = STATE_DEGLITCH;
                    else
                        next_state = STATE_WAITING;
                end
                STATE_DEGLITCH: begin
                    next_state = STATE_LINEAR_SCALE;
                end
                STATE_LINEAR_SCALE: begin
                    next_state = STATE_CHANGE_LIMIT;
                end
                STATE_CHANGE_LIMIT: begin
                    next_state = STATE_ASSIGN_OUTPUT;
                end
                STATE_ASSIGN_OUTPUT: begin
                    next_state = STATE_COMPLETE;
                end
                STATE_COMPLETE: begin
                    next_state = STATE_WAITING;
                end
                default: begin
                    next_state = STATE_WAITING;
                end
            endcase
        end
    end


    always @(posedge us_clk or negedge resetn) begin
        if(!resetn) begin

            throttle_pwm_value_out      <= `BYTE_ALL_ZERO;
            latched_throttle            <= `BYTE_ALL_ZERO;
            debounced_throttle          <= 0;
            limited_throttle            <= 0;
            prev_latched_throttle       <= `BYTE_ALL_ZERO;
            prev_throttle_pwm_value_out <= `BYTE_ALL_ZERO;

        end
        else begin
            case(state)
                STATE_INIT: begin
                    complete_signal         <= `TRUE;
                    active_signal           <= `FALSE;
                    throttle_pwm_value_out  <=  0;
                end
                STATE_WAITING: begin
                    complete_signal         <= `FALSE;
                    active_signal           <= `FALSE;
                    if(next_state == STATE_DEGLITCH) begin
                        if(throttle_pwm_value_in < 8'd10)
                            latched_throttle <= 8'd0;
                        else if(throttle_pwm_value_in > 8'd250)
                            latched_throttle <= 8'd250;
                        else
                            latched_throttle <= throttle_pwm_value_in;
                    end
                end
                STATE_DEGLITCH: begin
                    complete_signal <= `FALSE;
                    active_signal   <= `TRUE;
                    if(latched_throttle <= 8'd10) begin

                        if(prev_latched_throttle <= 8'd10) begin
                            debounced_throttle    <= latched_throttle;
                            prev_latched_throttle <= latched_throttle;
                        end

                        else if (prev_latched_throttle > 8'd10) begin
                            debounced_throttle    <= prev_latched_throttle;
                            prev_latched_throttle <= latched_throttle;
                        end

                        else begin
                            debounced_throttle    <= latched_throttle;
                            prev_latched_throttle <= latched_throttle;
                        end
                    end

                    else begin
                        prev_latched_throttle <= latched_throttle;
                        debounced_throttle    <= latched_throttle;
                    end
                end
                STATE_LINEAR_SCALE: begin 
                    complete_signal <= `FALSE; 
                    active_signal   <= `TRUE;
                    if (switch_a[0]) begin

                        if(debounced_throttle < THROTTLE_MID_RANGE_LOW_END) 
                            scaled_throttle  <= (8'd2*debounced_throttle); 

                        else if(debounced_throttle > THROTTLE_MID_RANGE_HIGH_END) 
                            scaled_throttle  <= ((8'd2*debounced_throttle)-8'd252); 

                        else 
                            scaled_throttle  <= ((debounced_throttle>>>1)+8'd61); 
                    end
                    else begin

                        if(debounced_throttle < 8'd35)
                            scaled_throttle  <= (8'd4*debounced_throttle); 
                        else


                            scaled_throttle  <= ((debounced_throttle>>2)+8'd128); 
                    end
                end
                STATE_CHANGE_LIMIT: begin
                    complete_signal          <= `FALSE;
                    active_signal            <= `TRUE;

                    if(scaled_throttle < 8'd10)
                        limited_throttle     <= 16'd0;

                    else if(scaled_throttle > (prev_throttle_pwm_value_out + THROTTLE_CHANGE_LIMIT)) begin
                        if((prev_throttle_pwm_value_out + THROTTLE_CHANGE_LIMIT) > 'd250)
                            limited_throttle <= 16'd250;
                        else
                            limited_throttle <= prev_throttle_pwm_value_out + THROTTLE_CHANGE_LIMIT;
                    end

                    else if( prev_throttle_pwm_value_out > (scaled_throttle + THROTTLE_CHANGE_LIMIT)) begin
                        if((prev_throttle_pwm_value_out - THROTTLE_CHANGE_LIMIT) < 'd10)
                            limited_throttle <= 16'd0;
                        else
                            limited_throttle <= prev_throttle_pwm_value_out - THROTTLE_CHANGE_LIMIT;
                    end

                    else begin
                        if(scaled_throttle > 250)
                            limited_throttle <= 16'd250;
                        if(scaled_throttle < 10)
                            limited_throttle <= 16'd0;
                        else
                            limited_throttle <= scaled_throttle;
                    end
                end
                STATE_ASSIGN_OUTPUT: begin
                    complete_signal <= `FALSE;
                    active_signal   <= `TRUE;
                    if(tc_enable_n)
                        throttle_pwm_value_out  <= latched_throttle;
                    else
                        {trash_bits, throttle_pwm_value_out} <= limited_throttle;
                end
                STATE_COMPLETE: begin
                    complete_signal             <= `TRUE;
                    active_signal               <= `FALSE;
                    prev_throttle_pwm_value_out <= throttle_pwm_value_out;
                end
                default: begin
                    throttle_pwm_value_out      <= `BYTE_ALL_ZERO;
                end
            endcase
        end
    end

endmodule