//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FFT_generator.h"
#include "generate_hdl_sources.h"
#include "generate_ver_env.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define MAX_BIT_WIDTH 32
#define MAX_STAGE_CNT 16

#define LABEL_PRINT  \
   data_reg_ff = pow(2,stage_count)*bit_size*(4-2*not_use_load_reg); \
   logic_ff = 4*(stage_count-1)+4+arith_synch*(2*stage_count+4*bit_size+1); \
   sprintf(_string,"%d FF used for data memory", data_reg_ff); \
   labelDataRegFF->Caption = _string; \
   sprintf(_string,"%d FF used in driving logic", logic_ff); \
   labelLogicFF->Caption = _string;
   
TForm1 *Form1;

int stage_count = 6;
int bit_size = 8;
int arith_synch_switch = 1;
int arith_synch = 1;
int not_use_load_reg = 0;
int generate_ver_env = 0;
int error = 0;

  int data_reg_ff;
  int logic_ff;
  char _string [50];
  
//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  char _string [4];
  int  i;
  
  for (i = 1; i <= MAX_STAGE_CNT; i++)
   {
    itoa(pow(2,i), _string,10);
    selectFFT->Items->Add(_string);
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::btnGenerateClick(TObject *Sender)
{
   FILE *report;
   report = fopen("report.txt","w");

  if (bit_size > MAX_BIT_WIDTH)
  {
   error = error + 1;
   fprintf(report, "Bit size exceeded maximal bit width, selected bit width =%2d, maximal bit width = %2d",
           bit_size, MAX_BIT_WIDTH);
  }
  else
  {
   if (arith_synch_switch == 1)
    {
     if (stage_count < 3)
      {
       fprintf(report, "Warning: Delay in arithmetic block can be used only with FFT size 8+.\nGenerated source files without delay in arithmetic.\n");
       labelWarn->Caption =
     "Warning: Delay in arithmetic block can be\nused only with FFT size 8+. Generated\nsource files without delay in arithmetic.";
       arith_synch = 0;
      }
     else
      {
       labelWarn->Caption = "";
       arith_synch = 1;
      }
    }
   else
    {
     labelWarn->Caption = "";
     arith_synch = 0;
    }

   error = fce_gen_hdl_sources(stage_count, bit_size,arith_synch,
                              not_use_load_reg, report);

   if ( generate_ver_env == 1)
    error = error + fce_gen_ver_env (stage_count, bit_size, report);
  }
  
  fclose(report);

  if (error)
  {
   labelErr->Caption =
   "Error during generating files, \nplease check report file !!!";
  }
  else
  {
   labelErr->Caption = "File generating succesfull!";
  }
}

//---------------------------------------------------------------------------

void __fastcall TForm1::useInOutRegClick(TObject *Sender)
{
  if (not_use_load_reg == 0)
      not_use_load_reg = 1;
  else
      not_use_load_reg = 0;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::useArithDelayClick(TObject *Sender)
{
  if (arith_synch_switch == 0)
      arith_synch_switch = 1;
  else
      arith_synch_switch = 0;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::dataWidthChange(TObject *Sender)
{
  char str_bit_size [2];
  strcpy (str_bit_size, dataWidth->Text.c_str());
  bit_size = atoi (str_bit_size);
  
  LABEL_PRINT
  //Label1 -> Caption = str_bit_size;  
}
//---------------------------------------------------------------------------

void __fastcall TForm1::selectFFTClick(TObject *Sender)
{
 int i;
 for (i = 0; i < MAX_STAGE_CNT; i++)
 {
  if(selectFFT->ItemIndex == i)
    stage_count = i + 1;
  }
  LABEL_PRINT
}
//---------------------------------------------------------------------------

void __fastcall TForm1::genVerEnvClick(TObject *Sender)
{
  if (generate_ver_env == 0)
      generate_ver_env = 1;
  else
      generate_ver_env = 0;
}
//---------------------------------------------------------------------------