#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<conio.h>

#include<math.h>

#include<malloc.h>

#include "spincore.h"


/* 	SPINCORE TECHNOLOGIES, INC

    GAINESVILLE, FL

    www.spincore.com



    This file is being distributed as community software free of charge.  SpinCore

    Technologies, Inc retains ownership of the software but does not make any

    claims as to its functionaliy or warranty it in any way.  Any modifications

    to this code must be provide to SpinCore along with a description of what

    was changed as well as the motivation behind the changes.  The modifications

    will be reviewed by SpinCore and made availible to others if the changes are

    deemed positive by SpinCore.



    This is an beta release of code.  As such

    there may be bugs in the code. */





//  MAIN PROGRAM  //

void main(int argc, char *argv[])
{

	UINT16 port_addr = 0x340;

	UINT32 output_control_word;

	INT16 temp2;

	Instruction_Unit *program;

	DDS_Unit *dds;

	HW_Parameters hw_spec;

	ADDRESS address;

	if(argc != 1 && argc != 2)

	{

		printf("\nCommand line ERROR.\n");

		printf("Command line usage is \"driver filename\"\n");

		exit(0);

	}

	
	if(argc == 2)
	{
		if(strcmp(argv[1],"-stop")==0)
		{ 	/* command line option to issue stop */

			reset_pb(port_addr);          	/* reset command to the NMR Controller */

			printf("\nStop Command has been issued to NMR Controller.\n");

			exit(0);
		}
		
		if(strcmp(argv[1],"-start")==0)
		{	/* command line option to issue start */

			arm_pb(port_addr);

			start_pb(port_addr);               /* start command to the NMR Controller */

			printf("\nStart Command has been issued to NMR Controller.\n");

			exit(0);

		}
	}

	

	/* Initialize Code */

	program = grab_image_memory(INTERNAL_MEMORY);

	dds = grab_dds_memory();

	hw_spec = InitHW(port_addr,50,INTERNAL_MEMORY);

	

	/* Program DDS frequency registers */

	program_dds(1, 0, 1, dds);

	program_dds(1, 1, 2, dds);

	program_dds(1, 2, 3, dds);

	program_dds(1, 3, 4, dds);

	

	program_initial_flag_values(0x00AAF0F0);



	/* Create program for Pulse Programmer Core; 

  Label definitions are in spincore.h

  (you may define your own labels as well)

	*/

	// begin your program here

	output_control_word = FREQ0 | OUTPUT_OFF | 0xFFFF;
	address = delay(0.2*us, output_control_word,program);  // synchronization state

	output_control_word = FREQ0 | OUTPUT_ON | 0xFFFF; 
	address = delay(5*us, output_control_word, program);  // first pulse

	output_control_word =  FREQ1 | OUTPUT_OFF | 0xFFFF; 
	address = delay(10*us, output_control_word, program);  // first delay

	output_control_word = FREQ1 | OUTPUT_ON | 0xFFFF; 
	address = delay(2.5*us, output_control_word, program);  // second pulse

	output_control_word = FREQ2 | OUTPUT_OFF | 0xFFFF; 
	address = delay(10*us, output_control_word, program);  // second delay

	output_control_word = FREQ2 | OUTPUT_ON | 0xFFFF; 
	address = delay(1.5*us, output_control_word, program);  // third pulse

	output_control_word = FREQ3 | OUTPUT_OFF | 0xFFFF; 
	address = delay(10*us, output_control_word, program);  // third delay

	output_control_word = FREQ3 | OUTPUT_ON | 0xFFFF; 
	address = delay(1.25*us, output_control_word, program);  // fourth pulse

	output_control_word = FREQ0 | OUTPUT_OFF | 0x0000; 
	address = branch(49.55*us, 0, output_control_word, program); //repetition delay, loops back)



	/* Create file to be used by driver function */

	/* This file can be very useful in  debugging errors since it allows

  the programmer to see what is actually being programmed to the 

	board. */

	if (argc == 1)
	{
		temp2 = create_bytecode_file_dds(program, address, dds, DEFAULT_OUTPUT_FILE, hw_spec);
	}
	else
	{
		temp2 = create_bytecode_file_dds(program, address, dds, argv[1], hw_spec);
	}
	
	if(temp2 != 0)
	{

		printf("Error create bytecode file, error code %d\n",temp2);

		exit(1);

	}

	

	/* Program PulseBlaster/PulseBlasterDDS series board with info

	contained in file created by function create_bytecode_file */

	if (argc == 1)
	{
		temp2 = program_pulseblaster(DEFAULT_OUTPUT_FILE);
	}
	else
	{
		temp2 = program_pulseblaster(argv[1]);
	}
	
	if(temp2 != 0)
	{
		printf("Error programming board, error code %d\n",temp2);

		exit(1);

	}

	

	/* start execution of program */

	start_pb(port_addr);          /* command to start PulseBlaster */

}
