/*
 * Copyright (c) 2018, Linz Center of Mechatronics GmbH (LCM), web: www.lcm.at
 * All rights reserved.
 */
/*
 * This file is licensed according to the BSD 3-clause license as follows:
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the "Linz Center of Mechatronics GmbH" and "LCM" nor
 *       the names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL "Linz Center of Mechatronics GmbH" BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * This file is part of X2C. web: x2c.lcm.at
 * $LastChangedRevision: 4306 $
 * $LastChangedDate:: 2026-07-02 15:48:24 +0200#$
 */
/* USERCODE-BEGIN:Description                                                                                         */
/* Description: */
/* USERCODE-END:Description                                                                                           */
#include <stddef.h>
#include "IIR_Float64.h"

/* all used update functions to ram for c2000                                                                         */
#if defined(__ALL_UPDATE_FUNC_2_RAM_C2000__)
    #pragma CODE_SECTION(IIR_Float64_Update, "ramfuncs")
#endif

/* USERCODE-BEGIN:PreProcessor                                                                                        */
/* Inputs */
#define IN              (*block->In)

/* Outputs */
#define OUT             (block->Out)

/* Parameter */
#if !defined(X2C_USE_UNION_FOR_POINTER)
#define COEFF_A         (block->coeffA)       /* Denominator coefficients array in reversed order (an,...,a2,a1) */
#define COEFF_B         (block->coeffB)       /* Numerator coefficients array in reversed order (bn,...,b2,b1,b0) */
#else
#define COEFF_A         (block->coeffA.ptr)   /* Denominator coefficients array in reversed order (an,...,a2,a1) */
#define COEFF_B         (block->coeffB.ptr)   /* Numerator coefficients array in reversed order (bn,...,b2,b1,b0) */
#endif
#define ORDER_N         (block->orderN)       /* Length of numerator coefficients array */
#define ORDER_D         (block->orderD)       /* Length of denominator coefficients array */

/* Variables */
#if !defined(X2C_USE_UNION_FOR_POINTER)
#define BUFFER_IN       (block->inputBuffer)      /* Circular buffer for input signal */
#define BUFFER_OUT      (block->outputBuffer)     /* Circular buffer for output signal */
#else
#define BUFFER_IN       (block->inputBuffer.ptr)  /* Circular buffer for input signal */
#define BUFFER_OUT      (block->outputBuffer.ptr) /* Circular buffer for output signal */
#endif
#define IDX_IN          (block->indexIn)          /* Pointer to current position in input buffer */
#define IDX_OUT         (block->indexOut)         /* Pointer to current position in output buffer */

/* Constants */
#define BUFFER_LEN      (32)                            /* Maximum length of circular buffer */
/* USERCODE-END:PreProcessor                                                                                          */

/**********************************************************************************************************************/
/** Declare private functions with the C inline keyword to ensure function inlining for better runtime performance.  **/
/** Since all tested compilers DO NOT perform inlining at the LOWEST optimization level, compiler optimizations may  **/
/** need to be adjusted accordingly.                                                                                 **/
/**********************************************************************************************************************/
/* USERCODE-BEGIN:PrivateFncPrototypeOrDefinition                                                                     */
/* USERCODE-END:PrivateFncPrototypeOrDefinition                                                                       */

/**********************************************************************************************************************/
/** Update                                                                                                           **/
/**********************************************************************************************************************/
void IIR_Float64_Update(IIR_FLOAT64 *block)
{
/* USERCODE-BEGIN:UpdateFnc                                                                                           */
    int8 i;
    float64 sumA = 0.0, sumB = 0.0;

    /* enter In(k) and Out(k-1) in circular buffer */
    BUFFER_IN[IDX_IN]   = IN;
    BUFFER_OUT[IDX_OUT] = OUT;

    /* increase pointers to circular buffers */
    IDX_IN  = (IDX_IN  + 1) % ORDER_N;  /* pointer to element k-N */
    IDX_OUT = (IDX_OUT + 1) % ORDER_D;  /* pointer to element k-D */

    /* calculate sums of numerator and denominator */
    for (i=0; i < ORDER_N; i++)
    {
        sumB += (COEFF_B[i] * BUFFER_IN[(IDX_IN + i) % ORDER_N]);
        sumA += (COEFF_A[i] * BUFFER_OUT[(IDX_OUT + i) % ORDER_D]);
    }
    for (i=ORDER_N; i < ORDER_D; i++)
    {
        sumA += (COEFF_A[i] * BUFFER_OUT[(IDX_OUT + i) % ORDER_D]);
    }

    /* calculate total sum and assign output */
    OUT = sumB - sumA;

/* USERCODE-END:UpdateFnc                                                                                             */
}

/**********************************************************************************************************************/
/** Initialization                                                                                                   **/
/**********************************************************************************************************************/
void IIR_Float64_Init(IIR_FLOAT64 *block)
{
    block->identifier = IIR_FLOAT64_ID;
    block->Out = 0.0;
/* USERCODE-BEGIN:InitFnc                                                                                             */
    IDX_IN = 0;
    IDX_OUT = 0;
    {
        int8 i;
        for (i=0; i < (BUFFER_LEN-1); i++)
        {
            BUFFER_IN[i]  = 0.0;
            BUFFER_OUT[i] = 0.0;
        }
        BUFFER_IN[i+1] = 0.0;
    }
/* USERCODE-END:InitFnc                                                                                               */
}

/**********************************************************************************************************************/
/** Load block data                                                                                                  **/
/**********************************************************************************************************************/
uint8 IIR_Float64_Load(const IIR_FLOAT64 *block, uint8 data[], uint16 *dataLength, uint16 maxSize)
{
    uint8 error = 0u;
    if (maxSize < 506u)
    {
        error = 1u;
    }
    else
    {
        uint16 i;
        uint64 tmp64;
        for (i=0; i<32u; i++)
        {
            (void) memcpy((void*)&tmp64, (const void*)&block->coeffB[i], sizeof(uint64));
            data[(8u*i)] = (uint8)(tmp64 & 0xFFu);
            data[(8u*i) + 1u] = (uint8)((tmp64 >> 8) & 0xFFu);
            data[(8u*i) + 2u] = (uint8)((tmp64 >> 16) & 0xFFu);
            data[(8u*i) + 3u] = (uint8)((tmp64 >> 24) & 0xFFu);
            data[(8u*i) + 4u] = (uint8)((tmp64 >> 32) & 0xFFu);
            data[(8u*i) + 5u] = (uint8)((tmp64 >> 40) & 0xFFu);
            data[(8u*i) + 6u] = (uint8)((tmp64 >> 48) & 0xFFu);
            data[(8u*i) + 7u] = (uint8)((tmp64 >> 56) & 0xFFu);
        }
        for (i=0; i<31u; i++)
        {
            (void) memcpy((void*)&tmp64, (const void*)&block->coeffA[i], sizeof(uint64));
            data[256u + (8u*i)] = (uint8)(tmp64 & 0xFFu);
            data[256u + (8u*i) + 1u] = (uint8)((tmp64 >> 8) & 0xFFu);
            data[256u + (8u*i) + 2u] = (uint8)((tmp64 >> 16) & 0xFFu);
            data[256u + (8u*i) + 3u] = (uint8)((tmp64 >> 24) & 0xFFu);
            data[256u + (8u*i) + 4u] = (uint8)((tmp64 >> 32) & 0xFFu);
            data[256u + (8u*i) + 5u] = (uint8)((tmp64 >> 40) & 0xFFu);
            data[256u + (8u*i) + 6u] = (uint8)((tmp64 >> 48) & 0xFFu);
            data[256u + (8u*i) + 7u] = (uint8)((tmp64 >> 56) & 0xFFu);
        }
        data[504] = (uint8)((uint8)block->orderN & 0xFFu);
        data[505] = (uint8)((uint8)block->orderD & 0xFFu);
        *dataLength = 506u;
/* USERCODE-BEGIN:LoadFnc                                                                                             */
/* USERCODE-END:LoadFnc                                                                                               */
    }
    return (error);
}

/**********************************************************************************************************************/
/** Save block data                                                                                                  **/
/**********************************************************************************************************************/
uint8 IIR_Float64_Save(IIR_FLOAT64 *block, const uint8 data[], uint16 dataLength)
{
    uint8 error;
    if (dataLength != 506u)
    {
        error = 1u;
    }
    else
    {
        uint16 i;
        uint64 tmp64;
        for (i=0; i<32u; i++)
        {
            tmp64 = (uint64)((uint64)data[(8u*i)] | ((uint64)data[(8u*i) + 1u] << 8) | ((uint64)data[(8u*i) + 2u] << 16) | ((uint64)data[(8u*i) + 3u] << 24) | 
                ((uint64)data[(8u*i) + 4u] << 32) | ((uint64)data[(8u*i) + 5u] << 40) | ((uint64)data[(8u*i) + 6u] << 48) | ((uint64)data[(8u*i) + 7u] << 56));
            (void) memcpy((void*)&block->coeffB[i], (const void*)&tmp64, sizeof(float64));
        }
        for (i=0; i<31u; i++)
        {
            tmp64 = (uint64)((uint64)data[256u + (8u*i)] | ((uint64)data[256u + (8u*i) + 1u] << 8) | ((uint64)data[256u + (8u*i) + 2u] << 16) | ((uint64)data[256u + (8u*i) + 3u] << 24) | 
                ((uint64)data[256u + (8u*i) + 4u] << 32) | ((uint64)data[256u + (8u*i) + 5u] << 40) | ((uint64)data[256u + (8u*i) + 6u] << 48) | ((uint64)data[256u + (8u*i) + 7u] << 56));
            (void) memcpy((void*)&block->coeffA[i], (const void*)&tmp64, sizeof(float64));
        }
        block->orderN = castUint8ToInt8(data[504]);
        block->orderD = castUint8ToInt8(data[505]);
        error = 0u;
/* USERCODE-BEGIN:SaveFnc                                                                                             */
/* USERCODE-END:SaveFnc                                                                                               */
    }
    return (error);
}

/**********************************************************************************************************************/
/** Get block element address                                                                                        **/
/**********************************************************************************************************************/
#if !defined(IIR_FLOAT64_ISLINKED)
void* IIR_Float64_GetAddress(IIR_FLOAT64* block, uint16 elementId)
{
    void* addr;
    switch (elementId)
    {
        case 1:
            addr = block->In;
            break;
        case 2:
            addr = &block->Out;
            break;
        default:
            addr = NULL;
            break;
    }
    return (addr);
}
#endif

/**********************************************************************************************************************/
/** Declare private functions with the C inline keyword to ensure function inlining for better runtime performance.  **/
/** Since all tested compilers DO NOT perform inlining at the LOWEST optimization level, compiler optimizations may  **/
/** need to be adjusted accordingly.                                                                                 **/
/**********************************************************************************************************************/
/* USERCODE-BEGIN:PrivateFncDefinition                                                                                */
/* USERCODE-END:PrivateFncDefinition                                                                                  */
