// -----------------------------------------------------------------------------
// (C) Bibix
// The content below includes confidential, proprietary information of
// Bibix. All use, disclosure, and/or reproduction is prohibited
// unless authorized in writing. All rights reserved.
// -----------------------------------------------------------------------------
// File         : tb_cordic_sine_gen.cpp
// Description  : Testbench for CORDIC sine generator
// Author       : Sabih Gerez, Bibix
//                based on work by Rene Moll, DSE
// Creation date: July 6, 2011
// -----------------------------------------------------------------------------
// $Rev: 135 $
// $Author: sabih $
// $Date: 2011-07-13 21:44:34 +0200 (Wed, 13 Jul 2011) $
// $Log$
// -----------------------------------------------------------------------------

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include "arx_util.h"
#include "cordic_sine_gen.h"

int main(int argc, char* argv[])
{
  // both input and output data stream will be written from testbench
  // open file for input stream
  FILE *infile;

  infile = fopen("cordic_sine_gen.in", "wt");
  if (infile == NULL)
     printf("Cannot open cordic_sine_gen.in for reading!\n");
  assert(infile != NULL);

  // open file for output stream
  FILE *outfile;

  outfile = fopen("cordic_sine_gen.out", "wt");
  if (outfile == NULL)
     printf("Cannot open cordic_sine_gen.out for writing!\n");
  assert(outfile != NULL);
  
  // Create an intance of the DUV
  cordic_sine_gen oDUV;
  
  // Initialize DUV
  oDUV.reset();
  
  // phase step; 1024 corresponds to 360 degrees
  // so a step of 10 corresponsd roughly to  4 degrees
  // so a step of 40 corresponsd roughly to 15 degrees
  i32 nPhase;
  
  // Output variables
  // - X, Y coordinates
  i32    nXout, nYout;
  double dYout, dXout;
  
  // Output helper
  char cBuffer[80];

  // initialize with spaces
  for (int i = 0; i < 79; i++)
    cBuffer[i] = ' ';

  cBuffer[79] = '\n';
  
  // Simulator
  for (int i = 0; i < 240; i++)  {
    if (i < 100)
       nPhase = 10;
    else if (i < 140)
       nPhase = i - 90;
    else
       nPhase = 40;

    // Apply input to DUV
    oDUV.run(nPhase, nXout, nYout);
    
    // Write input and output to file
    fprintf(infile, "%d\n", nPhase);
    fprintf(outfile, "%d %d\n", nXout, nYout);

    // Perform output conversion
    dXout = signed_to_double(nXout, 11, 2);
    dYout = signed_to_double(nYout, 11, 2);
    
    // print the output to stdout using "ASCII art"
    //
    int xpos = 40 + 40/1.7 * dXout;
    int ypos = 40 + 40/1.7 * dYout;

    cBuffer[xpos] = '*';
    cBuffer[ypos] = '+';
    
    printf("%s",cBuffer);

    cBuffer[xpos] = ' ';
    cBuffer[ypos] = ' ';
  }
  
  fclose(infile);
  fclose(outfile);
  
  printf("OK! End of simulation.\n");
}
