--=============================================================================
library IEEE, std;
use IEEE.electrical_systems.all;
use IEEE.math_real.all;
--=============================================================================

package PDA_functions is

--=============================================================================
function FindCursorIndex (Wfm : real_vector) return integer;
-------------------------------------------------------------------------------
function EyeContour (Wfm         : real_vector;
                     CursorIndex : integer;
                     BitWidth    : integer;
                     EyeSelector : string := "L") return real_vector;
--=============================================================================

end package PDA_functions;

--XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
--XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

package body PDA_functions is

--=============================================================================
function FindCursorIndex (Wfm : real_vector) return integer is
-------------------------------------------------------------------------------
  variable Index : integer := 0;
  variable Value : real    := 0.0;
-------------------------------------------------------------------------------
begin
  for i in Wfm'range loop
    if i = 0 then
      Value := Wfm(i);
    else
      if Wfm(i) > Value then
        Value := Wfm(i);
        Index := i;
      end if;
    end if;
  end loop;
--  report "Index: " & integer'image(Index);

  return Index;
-------------------------------------------------------------------------------
end function FindCursorIndex;
--=============================================================================
function EyeContour (Wfm         : real_vector;
                     CursorIndex : integer;
                     BitWidth    : integer;
                     EyeSelector : string := "L") return real_vector is
-------------------------------------------------------------------------------
  variable EyeContour : real_vector(1 to BitWidth) := (others => 0.0);
  variable i          : integer := 0;
  variable StartIndex : integer := CursorIndex;
-------------------------------------------------------------------------------
begin
  while (CursorIndex - BitWidth/2 - i*BitWidth) > 0 loop
    i := i + 1;
  end loop;
  StartIndex := CursorIndex - BitWidth/2 - (i-1)*BitWidth;
--  report " StartIndex: " & integer'image(StartIndex);

  i := 0;
  while (StartIndex + (i+1)*BitWidth) <= Wfm'right loop
    if StartIndex + i*BitWidth + BitWidth/2 = CursorIndex then
--      report " This is the waveform. " & integer'image(StartIndex + i*BitWidth + BitWidth/2);
      if (EyeSelector = "U") then
        for j in EyeContour'range loop
          EyeContour(j) := EyeContour(j) + Wfm(StartIndex + j-1 + i*BitWidth);
        end loop;
      end if;
    else
--      report " This is NOT the waveform. " & integer'image(StartIndex + i*BitWidth + BitWidth/2);
      if (EyeSelector = "U") then
        for j in EyeContour'range loop
          EyeContour(j) := EyeContour(j) + realmin(0.0, Wfm(StartIndex + j-1 + i*BitWidth));
--          report " j: " & integer'image(j) &
--                 " i: " & integer'image(i) &
--                 " StartIndex: " & integer'image(StartIndex) &
--                 " Index: " & integer'image(StartIndex + j-1 + i*BitWidth);
        end loop;
      else
        for j in EyeContour'range loop
          EyeContour(j) := EyeContour(j) + realmax(0.0, Wfm(StartIndex + j-1 + i*BitWidth));
--          report " j: " & integer'image(j) &
--                 " i: " & integer'image(i) &
--                 " StartIndex: " & integer'image(StartIndex) &
--                 " Index: " & integer'image(StartIndex + j-1 + i*BitWidth);
        end loop;
      end if;
    end if;
    i := i + 1;
  end loop;

  return EyeContour;
-------------------------------------------------------------------------------
end function EyeContour;
--=============================================================================

end package body PDA_functions;

--=============================================================================
