/*
 * "Hello World" example.
 *
 * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
 * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
 * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
 * device in your system's hardware.
 * The memory footprint of this hosted application is ~69 kbytes by default
 * using the standard reference design.
 *
 * For a reduced footprint version of this template, and an explanation of how
 * to reduce the memory footprint for a given application, see the
 * "small_hello_world" template.
 *
 */

// Software controller that interacts with the NIOS II hardware via an Avalon Bus interface
// Created by : Jonathan Chen (jtc2119)
// Last modified : Apr. 7, 2008


#include <io.h>
#include <system.h>
#include <stdio.h>

#define IOWR_AUDIO_DATA(base, offset, data) \
  IOWR_16DIRECT(base, (offset) * 2, data) 
#define IORD_AUDIO_DATA(base, offset) \
  IORD_16DIRECT(base, (offset) * 2)
#define IOWR_SRAM_DATA(base, offset, data) \
  IOWR_16DIRECT(base, (offset) * 2, data) 
#define IORD_SRAM_DATA(base, offset) \
  IORD_16DIRECT(base, (offset) * 2)
#define HORT_OFFSET 0
#define VERT_OFFSET 1
#define HORT_CORD 20
#define VERT_CORD 400
#define HORT_BEG 10
#define HORT_END 600
#define VERT_BEG 10
#define VERT_END 450
#define DELAY 100

int main()
{
  int count = 0;
  int hort, vert;
  int down, right;
  int i = 0;
  volatile int audio_data; 
  volatile int* buttons = SWITCH_PIO_BASE;
  
  printf("Hello Mr. Anderson\n");

  hort = HORT_CORD;
  vert = VERT_CORD;
  down = 1;
  right = 1;
  LCD_Test();
  
  
 /* short * audioPtr = SRAM_0_BASE;
  
  for (i=0; i<200; i++) {
    //ignor read
    audioPtr[i] = audio_data; 
    audioPtr[5] = 7;
    audioPtr[5] = 8; 
    value = audioPtr[5];
    value2= audioPtr[5]; 
  }*/
  
  while (1) {
       //printf( "OUTPUT!!!: %02X\n", *buttons );
       if (IORD_16DIRECT( SWITCH_PIO_BASE, 0 ) == 0x3) {
           for (i = 0; i < DELAY; i++) {    
               audio_data = IORD_AUDIO_DATA(AUDIO_BASE, HORT_OFFSET); 
               IOWR_SRAM_DATA(SRAM_0_BASE, i, audio_data);
               printf("Recording...\n");     
           }      
       }
       else if (IORD_16DIRECT( SWITCH_PIO_BASE, 0 ) == 0x5) {
           for (i = 0; i < DELAY; i++) {
               audio_data = IORD_SRAM_DATA(SRAM_0_BASE, DELAY-i);
               IOWR_AUDIO_DATA(AUDIO_BASE, HORT_OFFSET, audio_data); 
               printf("Playing...\n");
           }           
       }            


        // IOWR(SEG7_DISPLAY_BASE,0,i++);
  
        //printf("The button pressed is %x:\n", *buttons);
        //for (count = 0; count < DELAY; count++);
   }
   return 0;
}
