data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v
 line:266: before: "signed"
1: b'%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:19: 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/pid.v:31: Define or directive not defined: \'`PID_RATE_BIT_WIDTH\'\n     output reg signed  [`PID_RATE_BIT_WIDTH-1:0]  rate_out,\n                         ^~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:32: Define or directive not defined: \'`DEBUG_WIRE_BIT_WIDTH\'\n     output wire    [`DEBUG_WIRE_BIT_WIDTH-1:0]DEBUG_WIRE,  \n                     ^~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:33: Define or directive not defined: \'`RATE_BIT_WIDTH\'\n     input  wire signed [`RATE_BIT_WIDTH-1:0]     angle_error,\n                         ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:34: Define or directive not defined: \'`RATE_BIT_WIDTH\'\n     input  wire signed [`RATE_BIT_WIDTH-1:0]     target_rotation,\n                         ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:35: Define or directive not defined: \'`IMU_VAL_BIT_WIDTH\'\n      input  wire signed [`IMU_VAL_BIT_WIDTH-1:0]   actual_rotation,\n                          ^~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:42: Define or directive not defined: \'`OPS_BIT_WIDTH\'\n reg signed [`OPS_BIT_WIDTH-1:0]\n             ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:66: Define or directive not defined: \'`DEBUG_WIRE_BIT_WIDTH\'\n assign DEBUG_WIRE = rotation_total[`DEBUG_WIRE_BIT_WIDTH-1:0];\n                                    ^~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:79: Define or directive not defined: \'`FALSE\'\n   pid_active   <= `FALSE;\n                   ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:79: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n   pid_active   <= `FALSE;\n                         ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:80: Define or directive not defined: \'`FALSE\'\n   pid_complete  <= `FALSE;\n                    ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:80: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n   pid_complete  <= `FALSE;\n                          ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:81: Define or directive not defined: \'`ALL_ZERO_2BYTE\'\n   rate_out   <= `ALL_ZERO_2BYTE;\n                 ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:81: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n   rate_out   <= `ALL_ZERO_2BYTE;\n                                ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:86: Define or directive not defined: \'`FALSE\'\n     pid_active    <= `FALSE;\n                      ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:86: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_active    <= `FALSE;\n                            ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:87: Define or directive not defined: \'`TRUE\'\n     pid_complete   <= `TRUE;\n                       ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:87: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_complete   <= `TRUE;\n                            ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:90: Define or directive not defined: \'`TRUE\'\n     pid_active     <= `TRUE;\n                       ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:90: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_active     <= `TRUE;\n                            ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:91: Define or directive not defined: \'`FALSE\'\n     pid_complete    <= `FALSE;\n                        ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:91: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_complete    <= `FALSE;\n                              ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:97: Define or directive not defined: \'`TRUE\'\n     pid_active     <= `TRUE;\n                       ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:97: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_active     <= `TRUE;\n                            ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:98: Define or directive not defined: \'`FALSE\'\n     pid_complete    <= `FALSE;\n                        ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:98: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_complete    <= `FALSE;\n                              ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:103: Define or directive not defined: \'`TRUE\'\n     pid_active     <= `TRUE;\n                       ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:103: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_active     <= `TRUE;\n                            ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:104: Define or directive not defined: \'`FALSE\'\n     pid_complete    <= `FALSE;\n                        ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:104: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_complete    <= `FALSE;\n                              ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:108: Define or directive not defined: \'`TRUE\'\n     pid_active     <= `TRUE;\n                       ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:108: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_active     <= `TRUE;\n                            ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:109: Define or directive not defined: \'`FALSE\'\n     pid_complete    <= `FALSE;\n                        ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:109: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_complete    <= `FALSE;\n                              ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:113: Define or directive not defined: \'`TRUE\'\n     pid_active     <= `TRUE;\n                       ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:113: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_active     <= `TRUE;\n                            ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:114: Define or directive not defined: \'`TRUE\'\n     pid_complete    <= `TRUE;\n                        ^~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:114: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_complete    <= `TRUE;\n                             ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:123: Define or directive not defined: \'`FALSE\'\n     pid_active     <= `FALSE;\n                       ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:123: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_active     <= `FALSE;\n                             ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:124: Define or directive not defined: \'`FALSE\'\n     pid_complete    <= `FALSE;\n                        ^~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:124: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     pid_complete    <= `FALSE;\n                              ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:125: Define or directive not defined: \'`ALL_ZERO_2BYTE\'\n     rate_out     <= `ALL_ZERO_2BYTE;\n                     ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:125: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     rate_out     <= `ALL_ZERO_2BYTE;\n                                    ^\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:175: Define or directive not defined: \'`RATE_BIT_WIDTH\'\nfunction automatic signed [`RATE_BIT_WIDTH-1:0] scale_val;\n                           ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:176: Define or directive not defined: \'`RATE_BIT_WIDTH\'\n input reg signed [`RATE_BIT_WIDTH-1:0]  val;\n                   ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:177: Define or directive not defined: \'`OPS_BIT_WIDTH\'\n input reg signed [`OPS_BIT_WIDTH-1:0]    k_mult;\n                   ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:178: Define or directive not defined: \'`SHIFT_OP_BIT_WIDTH\'\n input reg signed [`SHIFT_OP_BIT_WIDTH-1:0] k_shift;\n                   ^~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/114714167/Source-Code/Drone2/impl1/source/pid.v:204: Define or directive not defined: \'`RATE_BIT_WIDTH\'\n   scale_val = $signed(scaled[`RATE_BIT_WIDTH-1:0]);\n                              ^~~~~~~~~~~~~~~\n%Error: Exiting due to 49 error(s)\n%Error: Exiting due to too many errors encountered; --error-limit=50\n'
module pid #(parameter signed [15:0] RATE_MIN = 16'h8000,
	 		  parameter signed [15:0] RATE_MAX = 16'h7FFF,
			  parameter signed [15:0] K_P = 1,
			  parameter signed [15:0] K_I = 1,
			  parameter signed [15:0] K_D = 1,
			  parameter signed K_P_SHIFT = 4'h4,
			  parameter signed K_I_SHIFT = 4'h4,
			  parameter signed K_D_SHIFT = 4'h4)
 			 (output reg pid_complete,
			  output reg pid_active,
			  output reg signed  [`PID_RATE_BIT_WIDTH-1:0] 	rate_out,
			  output wire 		 [`DEBUG_WIRE_BIT_WIDTH-1:0]DEBUG_WIRE,
			  input  wire signed [`RATE_BIT_WIDTH-1:0] 	  	angle_error,
			  input  wire signed [`RATE_BIT_WIDTH-1:0] 	  	target_rotation,
 			  input  wire signed [`IMU_VAL_BIT_WIDTH-1:0]  	actual_rotation,
			  input  wire start_flag,
			  input  wire wait_flag,
			  input  wire resetn,
			  input  wire us_clk);


	reg signed [`OPS_BIT_WIDTH-1:0]
		error_change,
		rotation_total,
		rotation_error,
		prev_rotation_error,
		scaled_rotation,
		rotation_integral,
		rotation_derivative,
		rotation_proportional;


	localparam STATE_BIT_WIDTH = 4'd7;
	localparam [STATE_BIT_WIDTH-1:0]
		STATE_WAIT     		= 7'b0000001,
		STATE_EXTEND_32BIT	= 7'b0000010,
		STATE_CALC1    		= 7'b0000100,
		STATE_CALC2    		= 7'b0001000,
		STATE_CALC3    		= 7'b0010000,
		STATE_CALC4    		= 7'b0100000,
		STATE_COMPLETE 		= 7'b1000000;

	reg [STATE_BIT_WIDTH-1:0] state, next_state;


	assign DEBUG_WIRE = rotation_total[`DEBUG_WIRE_BIT_WIDTH-1:0];


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


  always @(posedge us_clk or negedge resetn) begin
		if(!resetn) begin
			pid_active 		<= `FALSE;
			pid_complete 	<= `FALSE;
			rate_out 		<= `ALL_ZERO_2BYTE;
		end
		else begin
			case(state)
				STATE_WAIT: begin
					pid_active				<= `FALSE;
					pid_complete			<= `TRUE;
				end
				STATE_CALC1: begin
					pid_active 				<= `TRUE;
					pid_complete 			<= `FALSE;
					prev_rotation_error 	<= rotation_error;
					rotation_error 			<= target_rotation - actual_rotation;
					rotation_integral 		<= scale_val(angle_error, K_I, K_I_SHIFT);
				end
				STATE_CALC2: begin
					pid_active 				<= `TRUE;
					pid_complete 			<= `FALSE;
					rotation_proportional 	<= scale_val(rotation_error, K_P, K_P_SHIFT);
					error_change 			<= rotation_error - prev_rotation_error;
				end
				STATE_CALC3: begin
					pid_active 				<= `TRUE;
					pid_complete 			<= `FALSE;
					rotation_derivative 	<= scale_val(error_change, K_D, K_D_SHIFT);
				end
				STATE_CALC4: begin
					pid_active 				<= `TRUE;
					pid_complete 			<= `FALSE;
					rotation_total 			<= rotation_proportional + rotation_integral + rotation_derivative;
				end
				STATE_COMPLETE: begin
					pid_active 				<= `TRUE;
					pid_complete 			<= `TRUE;
					if(rotation_total < RATE_MIN)
						rate_out 			<= RATE_MIN;
					else if(rotation_total > RATE_MAX)
						rate_out 			<= RATE_MAX;
					else
						rate_out 			<= rotation_total;
				end
				default: begin
					pid_active 				<= `FALSE;
					pid_complete 			<= `FALSE;
					rate_out 				<= `ALL_ZERO_2BYTE;
				end
			endcase
		end
	end


	always @(*) begin
		if(!resetn) begin
		  next_state = STATE_WAIT;
		end
		else begin
			case(state)
				STATE_WAIT: begin
					if(start_flag)
						next_state 	= STATE_CALC1;
					else
						next_state	= STATE_WAIT;
				end
				STATE_CALC1: begin
				   next_state		= STATE_CALC2;
				end
				STATE_CALC2: begin
					next_state 		= STATE_CALC3;
				end
				STATE_CALC3: begin
						next_state 	= STATE_CALC4;
				end
				STATE_CALC4: begin
					next_state 		= STATE_COMPLETE;
				end
				STATE_COMPLETE: begin
					if(wait_flag)
						next_state 	= STATE_WAIT;
					else
						next_state 	= STATE_COMPLETE;
				end
				default: begin
					next_state 		= STATE_WAIT;
				end
			endcase
		end
	end



function automatic signed [`RATE_BIT_WIDTH-1:0] scale_val;
	input reg signed [`RATE_BIT_WIDTH-1:0]		val;
	input reg signed [`OPS_BIT_WIDTH-1:0]	  	k_mult;
	input reg signed [`SHIFT_OP_BIT_WIDTH-1:0]	k_shift;

	reg signed [31:0]
		val_32,
		scaled;

	localparam
		SHIFT_BACK 	= 7'd16,
		ZERO_PAD	= 16'd0;

	localparam signed [31:0]
		OVERFLOW_PROTECTION_MIN = 32'shFFFF8000,
		OVERFLOW_PROTECTION_MAX = 32'sh00007FFF;

	begin

		val_32 = $signed({val, ZERO_PAD}) >>> SHIFT_BACK;

		scaled = (val_32 * k_mult) >>> k_shift;


		if (scaled <= OVERFLOW_PROTECTION_MIN)
			scale_val = OVERFLOW_PROTECTION_MIN;
		else if (scaled >= OVERFLOW_PROTECTION_MAX)
			scale_val = OVERFLOW_PROTECTION_MAX;
		else
			scale_val = $signed(scaled[`RATE_BIT_WIDTH-1:0]);
	end
endfunction
endmodule
module pid #(parameter signed [15:0] RATE_MIN = 16'h8000,
	 		  parameter signed [15:0] RATE_MAX = 16'h7FFF,
			  parameter signed [15:0] K_P = 1,
			  parameter signed [15:0] K_I = 1,
			  parameter signed [15:0] K_D = 1,
			  parameter signed K_P_SHIFT = 4'h4,
			  parameter signed K_I_SHIFT = 4'h4,
			  parameter signed K_D_SHIFT = 4'h4)
 			 (output reg pid_complete,
			  output reg pid_active,
			  output reg signed  [`PID_RATE_BIT_WIDTH-1:0] 	rate_out,
			  output wire 		 [`DEBUG_WIRE_BIT_WIDTH-1:0]DEBUG_WIRE,
			  input  wire signed [`RATE_BIT_WIDTH-1:0] 	  	angle_error,
			  input  wire signed [`RATE_BIT_WIDTH-1:0] 	  	target_rotation,
 			  input  wire signed [`IMU_VAL_BIT_WIDTH-1:0]  	actual_rotation,
			  input  wire start_flag,
			  input  wire wait_flag,
			  input  wire resetn,
			  input  wire us_clk);



	reg signed [`OPS_BIT_WIDTH-1:0]
		error_change,
		rotation_total,
		rotation_error,
		prev_rotation_error,
		scaled_rotation,
		rotation_integral,
		rotation_derivative,
		rotation_proportional;


	localparam STATE_BIT_WIDTH = 4'd7;
	localparam [STATE_BIT_WIDTH-1:0]
		STATE_WAIT     		= 7'b0000001,
		STATE_EXTEND_32BIT	= 7'b0000010,
		STATE_CALC1    		= 7'b0000100,
		STATE_CALC2    		= 7'b0001000,
		STATE_CALC3    		= 7'b0010000,
		STATE_CALC4    		= 7'b0100000,
		STATE_COMPLETE 		= 7'b1000000;

	reg [STATE_BIT_WIDTH-1:0] state, next_state;


	assign DEBUG_WIRE = rotation_total[`DEBUG_WIRE_BIT_WIDTH-1:0];


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


  always @(posedge us_clk or negedge resetn) begin
		if(!resetn) begin
			pid_active 		<= `FALSE;
			pid_complete 	<= `FALSE;
			rate_out 		<= `ALL_ZERO_2BYTE;
		end
		else begin
			case(state)
				STATE_WAIT: begin
					pid_active				<= `FALSE;
					pid_complete			<= `TRUE;
				end
				STATE_CALC1: begin
					pid_active 				<= `TRUE;
					pid_complete 			<= `FALSE;
					prev_rotation_error 	<= rotation_error;
					rotation_error 			<= target_rotation - actual_rotation;
					rotation_integral 		<= scale_val(angle_error, K_I, K_I_SHIFT);
				end
				STATE_CALC2: begin
					pid_active 				<= `TRUE;
					pid_complete 			<= `FALSE;
					rotation_proportional 	<= scale_val(rotation_error, K_P, K_P_SHIFT);
					error_change 			<= rotation_error - prev_rotation_error;
				end
				STATE_CALC3: begin
					pid_active 				<= `TRUE;
					pid_complete 			<= `FALSE;
					rotation_derivative 	<= scale_val(error_change, K_D, K_D_SHIFT);
				end
				STATE_CALC4: begin
					pid_active 				<= `TRUE;
					pid_complete 			<= `FALSE;
					rotation_total 			<= rotation_proportional + rotation_integral + rotation_derivative;
				end
				STATE_COMPLETE: begin
					pid_active 				<= `TRUE;
					pid_complete 			<= `TRUE;
					if(rotation_total < RATE_MIN)
						rate_out 			<= RATE_MIN;
					else if(rotation_total > RATE_MAX)
						rate_out 			<= RATE_MAX;
					else
						rate_out 			<= rotation_total;
				end
				default: begin
					pid_active 				<= `FALSE;
					pid_complete 			<= `FALSE;
					rate_out 				<= `ALL_ZERO_2BYTE;
				end
			endcase
		end
	end


	always @(*) begin
		if(!resetn) begin
		  next_state = STATE_WAIT;
		end
		else begin
			case(state)
				STATE_WAIT: begin
					if(start_flag)
						next_state 	= STATE_CALC1;
					else
						next_state	= STATE_WAIT;
				end
				STATE_CALC1: begin
				   next_state		= STATE_CALC2;
				end
				STATE_CALC2: begin
					next_state 		= STATE_CALC3;
				end
				STATE_CALC3: begin
						next_state 	= STATE_CALC4;
				end
				STATE_CALC4: begin
					next_state 		= STATE_COMPLETE;
				end
				STATE_COMPLETE: begin
					if(wait_flag)
						next_state 	= STATE_WAIT;
					else
						next_state 	= STATE_COMPLETE;
				end
				default: begin
					next_state 		= STATE_WAIT;
				end
			endcase
		end
	end



function automatic signed [`RATE_BIT_WIDTH-1:0] scale_val;
	input reg signed [`RATE_BIT_WIDTH-1:0]		val;
	input reg signed [`OPS_BIT_WIDTH-1:0]	  	k_mult;
	input reg signed [`SHIFT_OP_BIT_WIDTH-1:0]	k_shift;

	reg signed [31:0]
		val_32,
		scaled;

	localparam
		SHIFT_BACK 	= 7'd16,
		ZERO_PAD	= 16'd0;

	localparam signed [31:0]
		OVERFLOW_PROTECTION_MIN = 32'shFFFF8000,
		OVERFLOW_PROTECTION_MAX = 32'sh00007FFF;

	begin

		val_32 = $signed({val, ZERO_PAD}) >>> SHIFT_BACK;

		scaled = (val_32 * k_mult) >>> k_shift;


		if (scaled <= OVERFLOW_PROTECTION_MIN)
			scale_val = OVERFLOW_PROTECTION_MIN;
		else if (scaled >= OVERFLOW_PROTECTION_MAX)
			scale_val = OVERFLOW_PROTECTION_MAX;
		else
			scale_val = $signed(scaled[`RATE_BIT_WIDTH-1:0]);
	end
endfunction
endmodule