// C code for Atmel CPU
// Edward Mengel
// EE476 Final Project

#include <90s8515.h> 
#include <stdio.h>       //for debugging using printf, etc 
#include <delay.h>

//timeout values for each task
#define t1 250   

//I like these definitions
#define begin {
#define end   }

//the  subroutines
void readcontroller(void);              //start a new conversion 
void initialize(void);  		//all the usual mcu stuff 
          
unsigned char reload;   //timer 0 reload to set 1 mSec
unsigned char reload1;  //timer 1 reload
unsigned int time1;    //task scheduling timeout counter
unsigned char cstate;
unsigned char fishstate;
unsigned char rand;
unsigned char randold;
                                                                
//**********************************************************
//timer 0 overflow ISR
interrupt [TIM0_OVF] void timer0_overflow(void)
begin   
    
  //reload to force 1 mSec overflow
  TCNT0=reload;
  
  //Decrement the three times if they are not already zero
  if (time1>0)  --time1;
  
end  

//**********************************************************
//timer 1 overflow ISR
interrupt [TIM1_OVF] void timer1_overflow(void)
begin   
    
  //reload to force no depenence on Timer 0
  TCNT1=reload1;
  
end  
 
//**********************************************************       
//Entry point and task scheduler loop
void main(void)
begin  
  initialize();
  
  //main task scheduler loop -- never exits!
  while(1)
  begin     
    if (time1==0)       readcontroller();
  end 
  
end  
  
//**********************************************************          
//Task 1                            

void readcontroller(void) 
 {
   time1=t1;

   cstate=0;
   if(!PIND.2) {              // left
      cstate=cstate|4;
      fishstate=fishstate&253;
      }      
   if(!PIND.3) {              // right
      cstate=cstate|8;
      fishstate=fishstate|2;
      }      
   if(!PIND.4) {				 // B
      cstate=cstate|32;
      }
   if(!PIND.5) {              // C
      cstate=cstate|64;
      }
                             
   PORTD=0x7f;

   if(!PIND.0) {               // up
      cstate=cstate|1;
      fishstate=fishstate&254;
      }
   if(!PIND.1) {				// down
      cstate=cstate|2;
      fishstate=fishstate|1;
      }
  
   if(!PIND.4) {				 // A
      cstate=cstate|16;
      }
   if(!PIND.5) {              // Start
      cstate=cstate|128;
      fishstate=fishstate|128;
      }
   else {
      fishstate=fishstate&127;
      }
             
   PORTD=0xff;
   
   randold=rand;
   rand=TCNT1L;
   rand=(rand>>2)&(rand<<2);
   fishstate=fishstate^(rand & randold);

   PORTB=fishstate;
   PORTC=fishstate;
 }  

//**********************************************************          
//Set it all up
void initialize(void)
begin

  // debug
  DDRB=0xff;	// all outputs
  PORTB=0x00;

  // out to Altera Board
  DDRC=0xff;	// all outputs
  PORTC=0x00;

  // initialize controller port
  DDRD=0x80;	// all inputs except 7
  PORTD=0xff;	// turn on pull-up resistors

  //serial setop for using printf     
  UCR = 0x10 + 0x08 ;
  UBRR = 25 ;
           
  //set up timer 0     
  //62.5 x (64x.25) microSec = 1.0 mSec, so prescale 64, and count 62 times.
  reload=256-62; //value for 1 Msec  
  TCNT0=reload;  //preload timer 1 so that is interrupts after 1 mSec.
  TCCR0=3;              //prescalar to 64

  // set up timer 1 (random number generator)
  reload1=1;
  TCNT1=reload1;
  TCCR1B=3;

  TIMSK=130;              //turn on timer 0 an 1 overflow ISR  
  
  //init the task timer
  time1=t1;    
                             
  fishstate=3;
  
  //crank up the ISRs
  #asm
        sei
  #endasm 
end  

   
