--=============================================================================
--=============================================================================
library IEEE;
use IEEE.electrical_systems.all;
use WORK.PDA_functions.all;
--=============================================================================
  entity C_PDA is

    generic (Cval        : real    := 1.0;
             V0          : real    := 0.0;
             Scale       : real    := 1.0;
             WfmPts      : integer := 3001;
             BitWidthPts : integer := 200);

    port (terminal C_p,        C_n,
                   UpperEye_p, UpperEye_n,
                   LowerEye_P, LowerEye_n  : electrical);

  end entity C_PDA;
  --===========================================================================
  architecture ideal of C_PDA is

  quantity  Vout       across  Iout       through  C_p         to  C_n;
  quantity  UpperEyeV  across  UpperEyeI  through  UpperEye_p  to  UpperEye_n;
  quantity  LowerEyeV  across  LowerEyeI  through  LowerEye_p  to  LowerEye_n;

  begin

    if domain = quiescent_domain use
      Vout == V0;
    else
      Iout == Scale * Cval * Vout'dot;
    end use;

    UpperEyeV == 0.0;
    LowerEyeV == 0.0;

  end architecture ideal;
  --===========================================================================
  --===========================================================================
  architecture PDA_on of C_PDA is

  quantity  Vout       across  Iout       through  C_p         to  C_n;
  quantity  UpperEyeV  across  UpperEyeI  through  UpperEye_p  to  UpperEye_n;
  quantity  LowerEyeV  across  LowerEyeI  through  LowerEye_p  to  LowerEye_n;

  signal   CursorIndex : integer := 0;
  signal   EyeIndex    : integer := 1;
  signal   Count       : integer := 1;

  signal   Wfm         : real_vector(1 to WfmPts)      := (others=>0.0);
  signal   UpperEye    : real_vector(1 to BitWidthPts) := (others=>0.0);
  signal   LowerEye    : real_vector(1 to BitWidthPts) := (others=>0.0);

  begin

    GetCursorIndex : process is
    begin
      wait for 3.0e-9;
      CursorIndex <= FindCursorIndex(Wfm);
      wait;
    end process GetCursorIndex;

    GetEye : process is
    begin
      wait on CursorIndex;
      --report "Cursor index is: " & integer'image(CursorIndex);
      UpperEye <= EyeContour(Wfm, CursorIndex, BitWidthPts, "U");
      LowerEye <= EyeContour(Wfm, CursorIndex, BitWidthPts, "L");
    end process GetEye;

    Ticker : process is
    begin
      wait for 1.0e-12;
      if (Count < WfmPts) then
        Wfm(Count) <= Vout;
        Count      <= Count + 1;
      elsif (EyeIndex < BitWidthPts) then
        EyeIndex <= EyeIndex + 1;
      end if;
    end process Ticker;
    ---------------------------------------------------------------------------
    break on Count, EyeIndex;
    
    if (domain = quiescent_domain) use
      Vout == V0;
    else
      Iout == Scale * Cval * Vout'dot;
    end use;

    if (now > 3.0e-9) use
      UpperEyeV == UpperEye(EyeIndex);
      LowerEyeV == LowerEye(EyeIndex);
    else
      UpperEyeV == 0.0;
      LowerEyeV == 0.0;
    end use;

  end architecture PDA_on;
  --===========================================================================
  --===========================================================================
  architecture PDA_off of C_PDA is

  quantity  Vout       across  Iout       through  C_p         to  C_n;
  quantity  UpperEyeV  across  UpperEyeI  through  UpperEye_p  to  UpperEye_n;
  quantity  LowerEyeV  across  LowerEyeI  through  LowerEye_p  to  LowerEye_n;

  signal   CursorIndex : integer := 0;
  signal   EyeIndex    : integer := 1;
  signal   Count       : integer := 1;

  signal   UpperEye    : real := 0.0;
  signal   LowerEye    : real := 0.0;

  begin

    GetCursorIndex : process is
    begin
      wait for 3.0e-9;
      CursorIndex <= 1;
      wait;
    end process GetCursorIndex;

    GetEye : process is
    begin
      wait on CursorIndex;
      --report "Cursor index is: " & integer'image(CursorIndex);
      UpperEye <= 0.0;
      LowerEye <= 0.0;
    end process GetEye;

    Ticker : process is
    begin
      wait for 1.0e-12;
      if (Count < WfmPts) then
        Count      <= Count + 1;
      elsif (EyeIndex < BitWidthPts) then
        EyeIndex <= EyeIndex + 1;
      end if;
    end process Ticker;
    ---------------------------------------------------------------------------
    break on Count, EyeIndex;
    
    if (domain = quiescent_domain) use
      Vout == V0;
    else
      Iout == Scale * Cval * Vout'dot;
    end use;

    if (now > 3.0e-9) use
      UpperEyeV == UpperEye;
      LowerEyeV == LowerEye;
    else
      UpperEyeV == 0.0;
      LowerEyeV == 0.0;
    end use;

  end architecture PDA_off;
--=============================================================================
--=============================================================================
