
//video gen and sound
//D.5 is sync:1000 ohm + diode to 75 ohm resistor
//D.6 is video:330 ohm + diode to 75 ohm resistor  
//C.0 is sound

#pragma regalloc-    //I allocate the registers myself
#pragma optsize-     //optimize for speed
                    
#include <Mega32.h>   
#include <stdio.h>
#include <stdlib.h> 
#include <math.h> 
#include <delay.h>   

//cycles = 63.625 * 16 Note NTSC is 63.55 
//but this line duration makes each frame exactly 1/60 sec
//which is nice for keeping a realtime clock 
#define lineTime 1018
 
#define begin {
#define end   }
#define ScreenTop 30
#define ScreenBot 230
#define T0reload 256-60


/*		Genesis gamepad pins		5 4 3 2 1
		 				 9 8 7 6
	 				 
		Select low / Select High	Connection on Port C
		1 - Up / Up						PC5
		2 - Down / Down                 PC0
		3 - Ground / Left               PC4
		4 - Ground / Right				PC3
		5 - +5V							Vcc
		6 - Button A / Button B			PC2
		7 - Mux select line				PC7
		8 - Ground						Gnd
		9 - Start / Button C			PC1
	
		Buttons variable bits - A/Start/Down/Left/Right/B/C/Down, low on press
*/

#define BUTTON_A     0x80
#define BUTTON_START 0x40
#define BUTTON_UP    0x20
#define BUTTON_LEFT  0x10
#define BUTTON_RIGHT 0x08
#define BUTTON_B     0x04
#define BUTTON_C     0x02
#define BUTTON_DOWN  0x01

//state machine
#define ST_INVALID  (-1)
#define ST_WELCOME    0
#define ST_TMSEL      1
#define ST_PLSEL    2
#define ST_LENSEL     3
#define ST_PAUSE      4
#define ST_GOAL       5
#define ST_FACEOFF    6
#define ST_PLAY       7
#define ST_PEREND     8
#define ST_ENDGAME    9



//song slector
#define SONG_INVALID  (-1)
#define SONG_7		    0
#define SONG_HEYBABY      1
#define SONG_MILLER	    2
#define SONG_MACHO	    3
#define SONG_GARY	     4
#define SONG_ALMA       5
#define SONG_DAVY	    6
#define SONG_CHEERS		7


//screen size settings
#define screenheight  98
#define screenwidth  122
#define screenleft     6
#define screentop     10

//NOTE that v1 to v8 and i must be in registers!  
register char v1 @4; 
register char v2 @5;
register char v3 @6;
register char v4 @7;
register char v5 @8;
register char v6 @9;
register char v7 @10;
register char v8 @11; 
register int i @12;

#pragma regalloc+ 

char syncON, syncOFF; 
int LineCount;
int time; 

char screen[1600], t, ts[10]; 
char Player1ax, Player1ay, Player2ax, Player2ay, Puckx, Pucky;
char prevPlayer1ax, prevPlayer1ay, prevPlayer2ax, prevPlayer2ay, prevPuckx, prevPucky;
int	 PuckVx, PuckVy;
int  Puckxdir, Puckydir;
char Player1aspeed, Player2aspeed, Player1burst, Player2burst;
char Player1fat, Player2fat;
char Player1fatcount, Player2fatcount;
char Player1askill, Player2askill; 
int Player1ShotInc, Player2ShotInc;
int Player1ShotMax, Player2ShotMax;
char Player1addspeed, Player2addspeed;
char playerheight, playerwidth;  
char Player1aPuck, Player2aPuck;   
char Player1Score, Player2Score;            
int p1al, p1ar, p1au, p1ad, p2al, p2ar, p2au, p2ad;       
char PuckSlow;
char P2aShoot, P1aShoot;
char PuckFight;
char Rand;
char Goalie1, Goalie2;           
char GoalTop, GoalBottom, Goalieheight;     
char g1u, g1d, g2u, g2d, Goalie1speed, Goalie2speed, Goalie1Save, Goalie2Save;
unsigned char buttons1, buttons2;                   // Gamepad state
char Player1aCont, Player2aCont, Goalie1Cont, Goalie2Cont;   	     			
char Player1Change, Player2Change;
unsigned char sticks; //seconds tick counter
unsigned char mins, secs, dmins, dsecs, MinCount;  //counters for time
char running;
char gamestate, laststate;
char faceoff, goalscore, pause, period, betper, endgamescreen;
char frame = 0;   
char statecount = 0;       
char statecounterase = 0;
char Team1 = 1;
char Team2 = 1;
char GameLength = 1;
char but1push = 0;
char but2push = 0;
char Playerselect = 1;
char Player = 1;
char songstate = SONG_DAVY;
char cheerstate = 0;
char playsong = 0;     
char Teamselect;

//strings
//char *welcome = "W E L C O M E";
char *welcomeOff = "             ";
//char *welcome1 = "PRESS START TO PLAY";
char *welcome1Off = "                   ";
char *goal = "GOAL!";
char *goalOff = "     ";
//char *periodEnd = "END OF PERIOD";
char *periodEndOff = "             ";
//char *TeamSelect = "SELECT A TEAM";
//char *Player1p = "PLAYER 1";
//char *Player2p = "PLAYER 2";
char *CU = "CU";    
char *HU = "HU";
char *UN = "UN";
char *MN = "MN";  
char *TE = "  ";

//tone related variables
//set note to a number to play a tone
char note, halfnote, notecount, inote, tnote;

//define some musical notes
//number of video lines per cycle of audio
//starting at c3 to c5
//frequency error is less than 1%   
//The 3 at the end is a "rest"
flash char notes[16]={120,107,95,90,80,71,64,60,
			54,48,45,40,36,32,30,3};
//Davy
flash unsigned char song0[2][73]={{3, 134, 119,106, 100, 89, 100, 106, 134, 119, 3, 119, 3, 119, 3, 119, 106, 3, 106, 134, 3, 134, 119, 106, 100, 119, 106, 100, 89, 3, 89, 3, 89, 119, 106,
									100, 89, 3 ,134, 119, 106, 100, 89, 100, 106, 134, 119, 3, 119, 3, 119, 3, 119, 106, 3, 106, 134, 80, 95, 119, 75, 80, 89, 95, 89, 84, 80, 100, 3, 100, 89, 100,3 },
				    {24, 12,24, 12, 24, 24, 48, 36, 12, 11, 1,  11, 1, 11, 1, 12, 23, 1, 24, 95, 1, 12, 24, 12, 24, 24, 24, 24, 35, 1, 11, 1, 24, 24, 24, 24, 60, 36,12,24, 12, 24, 24, 48, 36, 
				    12, 11,1, 11,1, 11,1, 12, 23,1, 24, 96, 12, 24, 12, 24, 24, 24, 24, 24, 24, 36, 11, 1, 24, 24, 60, 120}};

//Alma
flash unsigned char song1[2][62] = {{3,150, 134, 119, 134, 150, 179, 3, 179, 201, 134, 150, 159, 150, 134, 150, 134, 119, 134, 150, 179, 3, 179, 201, 134, 119, 113, 159, 150, 119, 3, 119, 134, 3, 134,
									150, 3, 150, 159, 3, 159, 179, 3, 179, 201, 150, 134, 150, 134, 119, 134, 150, 179, 3, 179, 201, 134, 119, 113, 159, 150, 3},
									{60, 36, 12, 36, 12, 24, 23, 1, 24, 24, 36, 12, 24, 24, 96, 36, 12, 36, 12, 24, 23, 1, 24, 24, 36, 12, 24, 24, 96, 35, 1, 12,
									 23, 1, 24, 35, 1, 12, 23, 1, 24, 35, 1, 12, 24, 24, 96, 36, 12, 36, 12, 24, 23, 1, 24, 24, 36, 12, 24, 24, 138, 120}};
//#7
flash unsigned char song2[2][35] = {{3, 119, 3, 	119, 3, 	119, 3, 	119, 113, 3, 	113, 3, 	113, 3, 	113,  	100, 	113, 119, 3, 	119, 3, 	119, 3, 		119, 	113, 3, 		113, 3, 		113, 3, 		113,  	100, 	113, 119,	 	3},
							{120, 23, 1,  	23, 1,  	23, 1, 	12, 	11, 1, 	23, 1, 	23, 1, 	24, 	12, 	12,  23, 1,  	23, 1,  	23, 1, 	12, 	11, 1, 			23, 1, 			23, 1, 			24, 	12, 	12, 24, 	 	 180}}; 
									
//Hey Baby					
flash unsigned char song3[2][28] = {{3,		89,		80,		106,	89,		80,		3,		106,	89,		106,	89,		106,	119,	134,	3,	134, 3, 134,	106, 3, 106,	134,	89,		80,	3,		80, 89, 3},
									{120,	144,	24,		12,		24,		12,		120,	12,		12,		12,		36,		12,		12,		60,		24, 11,	1,	12,		11, 1,	24,		36,		24,		11,	 1, 	24, 12, 180}};
									
//Miller Time
flash unsigned char song4[2][41] = {{3, 	106, 3, 		106, 	 119, 106, 		100, 	106, 	119, 	106, 	134, 	3, 	134, 	119, 	134, 	142, 	159, 	179, 	3, 	179, 	106, 	13, 	106, 	119, 	106, 	134, 	106, 3, 		106, 3, 	106, 	3, 	106, 		119, 	134, 	106, 		119, 		134, 3, 		134, 3}, 
												{120, 	11, 1, 		12, 	 12, 	6, 		18, 	12, 	12, 	12, 	24, 	12, 	6, 	18, 	12, 	12, 	6, 	30, 	12, 	12, 	11, 	1, 	12, 	6, 	12, 	54, 	11, 1, 		36, 12, 	11, 	1, 	6, 		12, 	30, 	6, 		6, 		5,		1, 		42, 	180}};

//Macho Man
flash unsigned char song5[2][44] = {{3,  134, 	3,  	134, 	3,  	134, 	3, 	134, 3, 	134, 	3,  	106, 	119, 	134, 	179, 	159, 	106, 3, 	106, 3, 	106, 3, 	106, 	3, 		134, 	119, 	142, 	3, 		179, 	106, 	119, 		134, 		179, 	159, 	106, 	3,   106, 3,   106, 	100, 	106, 	119, 	134, 	3},
									{120, 24,  	24, 	24,  	24, 	23,  	1, 	23,  1, 	24,  	24, 	24,  	24,  	24,  	12,   	108,   	23,  1, 	11,   1, 	11,   1, 	24,  	12, 	12,   	12,   	12,   	12, 	60, 	24,  	24,	  		24,  		12,  	108,  	23,  	1,   11,   1,   12,   	12,   	12,  	12,   	12, 	180}};

//Gary Glitter							
//flash unsigned char song6[2][39] = {{3,119, 3, 119, 106, 100,3,  100, 106,3,  106, 89,3,  89, 95, 3, 119, 3, 119,3,  119,3,  119,3,  119,3,  119, 3, 119, 89,3,  89, 100, 3, 134, 3, 134, 119, 3, 119,3,  119,3,  119,3,  119,3,  89,3,  89, 100, 3, 134, 3, 134, 119, 3, 119, 3, 119, 3, 119,3,  119, 89, 3, 89, 100, 3, 134, 3, 134, 119,3,  119, 3, 119,3,  119,3,  119, 89, 3},
//									{24, 5, 1, 18, 18, 65, 1, 6, 65, 1, 6, 65, 1, 6, 172, 12, 5, 1, 11, 1, 5, 1, 11, 5, 1, 11, 1, 6, 17, 1, 18, 18, 30, 5, 1, 12, 5, 1, 12, 5, 1, 11,  1, 17, 1, 18, 30, 5, 1, 11, 1, 5, 1, 11, 1, 5, 1, 11, 1, 6, 18}};
                                                                                                                                                                                                                                                          					

flash unsigned char song6[2][82] = {{3,119, 3, 119, 106, 100,3,  100, 106,3, 106, 89, 3, 89, 95, 3, 119, 3, 119, 3, 119, 3, 119, 3, 119, 3, 119, 3, 119, 89, 3, 89, 100,
									3, 134, 3, 134, 119, 3, 119, 3, 119, 3, 119, 3, 119, 89, 3, 89, 100, 3, 134, 3, 134, 119, 3, 119, 3, 119, 3, 119, 3, 119, 89, 3, 89, 100, 3, 134, 3, 134, 119, 3, 119, 3, 119, 3, 119, 3, 119, 89, 3},
									{24, 7, 1, 24,  24,  87, 1,  8,   87, 1,  8,  87, 1, 8, 96, 16,  7, 1,  15, 1,  7, 1,   15, 1,  7, 1,   15, 1,  8, 23,  1,  24, 24,
									40, 7,  1, 12,  7,  1,  11, 1,   7, 1,  11, 1,  8,   23, 1, 24, 24, 40, 7,  1, 12,  7,  1,  11, 1,   7, 1,  11, 1,  8,   23, 1, 24, 24, 40, 7,  1, 12,  7,  1,  11, 1,   7, 1,  11, 1,  8,   23, 180}};		
//Point plot lookup table
flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};			

//================================ 
//3x5 font numbers, then letters
//packed two per definition for fast 
//copy to the screen at x-position divisible by 4
flash char smallbitmap[39][5]={ 
	//0
    0b11101110,
	0b10101010,
	0b10101010,
	0b10101010,
	0b11101110,
	//1
	0b01000100,
	0b11001100,
	0b01000100,
	0b01000100,
	0b11101110,
	//2
	0b11101110,
	0b00100010,
	0b11101110,
	0b10001000,
	0b11101110,
	//3
	0b11101110,
	0b00100010,
	0b11101110,
	0b00100010,
	0b11101110,
	//4
	0b10101010,
	0b10101010,
	0b11101110,
	0b00100010,
	0b00100010,
	//5
	0b11101110,
	0b10001000,
	0b11101110,
	0b00100010,
	0b11101110,
	//6
	0b11001100,
	0b10001000,
	0b11101110,
	0b10101010,
	0b11101110,
	//7
	0b11101110,
	0b00100010,
	0b01000100,
	0b10001000,
	0b10001000,
	//8
	0b11101110,
	0b10101010,
	0b11101110,
	0b10101010,
	0b11101110,
	//9
	0b11101110,
	0b10101010,
	0b11101110,
	0b00100010,
	0b01100110,
	//:
	0b00000000,
	0b01000100,
	0b00000000,
	0b01000100,
	0b00000000,
	//!
	0b00100010,
	0b00100010,
	0b00100010,
	0b00000000,
	0b00100010,
	//blank
	0b00000000,
	0b00000000,
	0b00000000,
	0b00000000,
	0b00000000,
	//A
	0b11101110,
	0b10101010,
	0b11101110,
	0b10101010,
	0b10101010,
	//B
	0b11001100,
	0b10101010,
	0b11101110,
	0b10101010,
	0b11001100,
	//C
	0b11101110,
	0b10001000,
	0b10001000,
	0b10001000,
	0b11101110,
	//D
	0b11001100,
	0b10101010,
	0b10101010,
	0b10101010,
	0b11001100,
	//E
	0b11101110,
	0b10001000,
	0b11101110,
	0b10001000,
	0b11101110,
	//F
	0b11101110,
	0b10001000,
	0b11101110,
	0b10001000,
	0b10001000,
	//G
	0b11101110,
	0b10001000,
	0b10001000,
	0b10101010,
	0b11101110,
	//H
	0b10101010,
	0b10101010,
	0b11101110,
	0b10101010,
	0b10101010,
	//I
	0b11101110,
	0b01000100,
	0b01000100,
	0b01000100,
	0b11101110,
	//J
	0b00100010,
	0b00100010,
	0b00100010,
	0b10101010,
	0b11101110,
	//K
	0b10001000,
	0b10101010,
	0b11001100,
	0b11001100,
	0b10101010,
	//L
	0b10001000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b11101110,
	//M
	0b10101010,
	0b11101110,
	0b11101110,
	0b10101010,
	0b10101010,
	//N
	0b00000000,
	0b11001100,
	0b10101010,
	0b10101010,
	0b10101010,
	//O
	0b01000100,
	0b10101010,
	0b10101010,
	0b10101010,
	0b01000100,
	//P
	0b11101110,
	0b10101010,
	0b11101110,
	0b10001000,
	0b10001000,
	//Q
	0b01000100,
	0b10101010,
	0b10101010,
	0b11101110,
	0b01100110,
	//R
	0b11101110,
	0b10101010,
	0b11001100,
	0b11101110,
	0b10101010,
	//S
	0b11101110,
	0b10001000,
	0b11101110,
	0b00100010,
	0b11101110,
	//T
	0b11101110,
	0b01000100,
	0b01000100,
	0b01000100,
	0b01000100, 
	//U
	0b10101010,
	0b10101010,
	0b10101010,
	0b10101010,
	0b11101110, 
	//V
	0b10101010,
	0b10101010,
	0b10101010,
	0b10101010,
	0b01000100,
	//W
	0b10101010,
	0b10101010,
	0b11101110,
	0b11101110,
	0b10101010,
	//X
	0b00000000,
	0b10101010,
	0b01000100,
	0b01000100,
	0b10101010,
	//Y
	0b10101010,
	0b10101010,
	0b01000100,
	0b01000100,
	0b01000100,
	//Z
	0b11101110,
	0b00100010,
	0b01000100,
	0b10001000,
	0b11101110
	};
	
//==================================
//This is the sync generator and raster generator. It MUST be entered from 
//sleep mode to get accurate timing of the sync pulses
#pragma warn-
interrupt [TIM1_COMPA] void t1_cmpA(void)  
begin 
  //start the Horizontal sync pulse    
  PORTD = syncON | (PORTD & 0x1);     
  //count timer 0 at 1/usec
  TCNT0=0;
  //update the curent scanline number
  LineCount ++ ;   
  //begin inverted (Vertical) synch after line 247
  if (LineCount==248)
  begin 
    syncON = 0b00100000;
    syncOFF = 0;
  end
  //back to regular sync after line 250
  if (LineCount==251)	
  begin
    syncON = 0;
    syncOFF = 0b00100000;
  end  
  //start new frame after line 262
  if (LineCount==263) 
  begin
     LineCount = 1;
  end  
  
  delay_us(2); //adjust to make 5 us pulses
  //end sync pulse
  PORTD = syncOFF | (PORTD & 0x1);   
  
  if (LineCount<ScreenBot && LineCount>=ScreenTop) 
    begin 
       
       //compute byte index for beginning of the next line
       //left-shift 4 would be individual lines
       // <<3 means line-double the pixels 
       //The 0xfff8 truncates the odd line bit
       //i=(LineCount-ScreenTop)<<3 & 0xfff8; //
       
       #asm
       push r16
       lds   r12, _LineCount
       lds   r13, _Linecount+1
       ldi   r16, 30
       sub  r12, r16 
       ldi  r16,0
       sbc  r13, r16 
       lsl  r12
       rol  r13
       lsl  r12
       rol  r13
       lsl  r12    
       rol  r13
       mov  r16,r12
       andi r16,0xf0
       mov  r12,r16
       pop r16 
       #endasm
        
       //load 16 registers with screen info
       #asm
       push r14
       push r15
       push r16
       push r17
       push r18 
       push r19 
       push r26
       push r27
       
       ldi  r26,low(_screen)   ;base address of screen
       ldi  r27,high(_screen)   
       add  r26,r12            ;offset into screen (add i)
       adc  r27,r13
       ld   r4,x+   	       ;load 16 registers and inc pointer
       ld   r5,x+
       ld   r6,x+  
       ld   r7,x+
       ld   r8,x+ 
       ld   r9,x+
       ld   r10,x+  
       ld   r11,x+
       ld   r12,x+ 
       ld   r13,x+
       ld   r14,x+  
       ld   r15,x+
       ld   r16,x+   
       ld   r17,x+  
       ld   r18,x+
       ld   r19,x 
       
       pop  r27
       pop  r26
       #endasm  

       delay_us(19);  //adjust to center image on screen
       
       //blast 16 bytes to the screen   
       #asm
       ;but first a macro to make the code shorter  
       ;the macro takes a register number as a parameter
       ;and dumps its bits serially to portD.6   
       ;the nop can be eliminated to make the display narrower
       .macro videobits ;regnum
        BST  @0,7
	IN   R30,0x12
	BLD  R30,6
	nop
	OUT  0x12,R30  
	
	BST  @0,6
	IN   R30,0x12
	BLD  R30,6
	nop
	OUT  0x12,R30 
	
	BST  @0,5
	IN   R30,0x12
	BLD  R30,6 
	nop
	OUT  0x12,R30 
	
	BST  @0,4
	IN   R30,0x12
	BLD  R30,6
	nop
	OUT  0x12,R30 
	
	BST  @0,3
	IN   R30,0x12
	BLD  R30,6
	nop
	OUT  0x12,R30 
	
	BST  @0,2
	IN   R30,0x12
	BLD  R30,6
	nop
	OUT  0x12,R30 
	
	BST  @0,1
	IN   R30,0x12
	BLD  R30,6 
	nop
	OUT  0x12,R30 
	
	BST  @0,0
	IN   R30,0x12
	BLD  R30,6
	nop
	OUT  0x12,R30 
       .endm     
        
	videobits r4 ;video line -- byte 1
        videobits r5 ;byte 2  
        videobits r6 ;byte 3
        videobits r7 ;byte 4
        videobits r8 ;byte 5
        videobits r9 ;byte 6
        videobits r10 ;byte 7
        videobits r11 ;byte 8 
        videobits r12 ;byte 9
        videobits r13 ;byte 10  
        videobits r14 ;byte 11
        videobits r15 ;byte 12
        videobits r16 ;byte 13
        videobits r17 ;byte 14
        videobits r18 ;byte 15
        videobits r19 ;byte 16
	clt   ;clear video after the last pixel on the line
	IN   R30,0x12
	BLD  R30,6
	OUT  0x12,R30
								
       pop r19
       pop r18
       pop r17 
       pop r16  
       pop r15
       pop r14
       #endasm
              
    end
    
    
  //toggle the audio bits 
  // the two branches of the if statment 
  // must take the same number of cycles to aviod jitter on TV
  if ((notecount<halfnote))
     PORTD = PORTD & 0xfe;
  else
  begin
     PORTD = PORTD | 1; 
     #asm
     nop
     #endasm
  end
  //maintain audio cycle counter
  // the two branches of the if statment 
  // must take the same number of cycles  
  if (notecount==1)
  begin
     notecount = note;    
  end
  else
  begin
     notecount-- ; 
     #asm
     nop
     #endasm
  end
  
  
end  
#pragma warn+

//==================================
//plot one point 
//at x,y with color 1=white 0=black 2=invert 
#pragma warn-
void video_pt(char x, char y, char c)
begin   
	
	#asm
	;  i=(x>>3) + ((int)y<<4) ;   the byte with the pixel in it

	push r16
	ldd r30,y+2 		;get x
	lsr r30
	lsr r30
	lsr r30     		;divide x by 8
	ldd r12,y+1 		;get y
       	lsl r12     		;mult y by 16
       	clr r13
	lsl r12
	rol r13
	lsl r12
	rol r13
	lsl r12
	rol r13
	add r12, r30     	;add in x/8
	
	;v2 = screen[i];   r5
        ;v3 = pos[x & 7];  r6
	;v4 = c            r7
	ldi r30,low(_screen)
	ldi r31,high(_screen)
	add r30, r12
	adc r31, r13
	ld r5,Z  		;get screen byte
	ldd r26,y+2 		;get x
	ldi r27,0
	andi r26,0x07           ;form x & 7 
	ldi r30,low(_pos*2)  
	ldi r31,high(_pos*2)
	add r30,r26
	adc r31,r27
	lpm r6,Z
	ld r16,y 		;get c 
       
       ;if (v4==1) screen[i] = v2 | v3 ; 
       ;if (v4==0) screen[i] = v2 & ~v3; 
       ;if (v4==2) screen[i] = v2 ^ v3 ; 
       
       cpi r16,1
       brne tst0
       or  r5,r6
       tst0:
       cpi r16,0
       brne tst2 
       com r6
       and r5,r6
       tst2:
       cpi r16,2
       brne writescrn
       eor r5,r6
       writescrn:
       	ldi r30,low(_screen)
	ldi r31,high(_screen)
	add r30, r12
	adc r31, r13
	st Z, r5        	;write the byte back to the screen
	
	pop r16
	#endasm
end
#pragma warn+
 
  /*
//==================================
// put a small character on the screen
// c is index into bitmap
void video_smallchar(char x, char y, char c)  
begin 
    v7 = x;
    for (v6=0;v6<5;v6++) 
    begin
        v1 = smallbitmap[c][v6]; 
        v8 = y+v6;
        video_pt(v7,   v8, (v1 & 0x80)==0x80);  
        video_pt(v7+1, v8, (v1 & 0x40)==0x40); 
        video_pt(v7+2, v8, (v1 & 0x20)==0x20);
        video_pt(v7+3, v8, (v1 & 0x10)==0x10);       
    end
end  */
     
//==================================
// put a small character on the screen
// x-cood must be on divisible by 4 
// c is index into bitmap
void video_smallchar(char x, char y, char c)  
begin 
	char mask;
	i=((int)x>>3) + ((int)y<<4) ;
	if (x == (x & 0xf8)) mask = 0x0f;     //f8
	else mask = 0xf0;
	
	screen[i] =    (screen[i] & mask) | (smallbitmap[c][0] & ~mask); 
   	screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask);
        screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask);
        screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask);
   	screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask); 
end  

//==================================
// put a string of small characters on the screen
// x-cood must be on divisible by 4 
void video_putsmalls(char x, char y, char *str)
begin
	char i ;
	for (i=0; str[i]!=0; i++)
	begin  
		if (str[i]>=0x30 && str[i]<=0x3a) 
			video_smallchar(x,y,str[i]-0x30);
		else if(str[i]==0x20) //space
		    video_smallchar(x,y,12);
		else if(str[i]==0x21) //exclamation pt
			video_smallchar(x,y,11);
		else video_smallchar(x,y,str[i]-0x40+12);
		x = x+4;	
	end
end
       
//==================================
//plot a line 
//at x1,y1 to x2,y2 with color 1=white 0=black 2=invert 
//NOTE: this function requires signed chars   
//Code is from David Rodgers,
//"Procedural Elements of Computer Graphics",1985
void video_line(char x1, char y1, char x2, char y2, char c)
begin   
	int e;
	signed char dx,dy,j, temp;
	signed char s1,s2, xchange;
        signed char x,y;
        
	x = x1;
	y = y1;
	dx = cabs(x2-x1);
	dy = cabs(y2-y1);
	s1 = csign(x2-x1);
	s2 = csign(y2-y1);
	xchange = 0;   
	if (dy>dx)
	begin
		temp = dx;
		dx = dy;
		dy = temp;
		xchange = 1;
	end 
	e = ((int)dy<<1) - dx;   
	for (j=0; j<=dx; j++)
	begin
		video_pt(x,y,c) ; 
		if (e>=0)
		begin
			if (xchange==1) x = x + s1;
			else y = y + s2;
			e = e - ((int)dx<<1);
		end
		if (xchange==1) y = y + s2;
		else x = x + s1;
		e = e + ((int)dy<<1);
	end
end

//==================================
//return the value of one point 
//at x,y with color 1=white 0=black 2=invert
char video_set(char x, char y)
begin
	//The following construction 
  	//detects exactly one bit at the x,y location
	i=((int)x>>3) + ((int)y<<4) ;  
    return ( screen[i] & 1<<(7-(x & 0x7)));   	
end
//=================================

void DrawPlayer1(char x, char y, char j, char puck)
begin
	
	//video_line(x,y,x+10,y,j);
	//video_line(x+10 ,y ,x+10 ,y+10,j);
	//video_line(x+10, y+10 ,x ,y+10,j);
	//video_line(x ,y+10,x ,y ,j);
	
	video_pt(x+6,y,j); 
 	video_pt(x+7,y,j);
 	video_pt(x+8,y,j); 
       
 	video_pt(x+6,y+1,j); 
 	video_pt(x+7,y+1,j);
 	video_pt(x+8,y+1,j);

	video_pt(x+4,y+2,j); 
	video_pt(x+5,y+2,j);
	video_pt(x+6,y+2,j);
	video_pt(x+7,y+2,j); 

	video_pt(x+3,y+3,j);
	video_pt(x+4,y+3,j);
	video_pt(x+5,y+3,j); 
	video_pt(x+6,y+3,j);
	
	video_pt(x+3,y+4,j);
	video_pt(x+4,y+4,j);
	video_pt(x+5,y+4,j); 
	video_pt(x+6,y+4,j);
	
 	video_pt(x+2,y+5,j);
	video_pt(x+3,y+5,j);
	video_pt(x+4,y+5,j); 
	video_pt(x+5,y+5,j);
	video_pt(x+6,y+5,j);
	video_pt(x+7,y+5,j);
	
	video_pt(x+2,y+6,j); 
	video_pt(x+3,y+6,j);
	video_pt(x+4,y+6,j);
	video_pt(x+5,y+6,j);
	video_pt(x+8,y+6,j); 
	video_pt(x+9,y+6,j);
	
	video_pt(x+2,y+7,j); 
	video_pt(x+3,y+7,j);
	video_pt(x+6,y+7,j);
	video_pt(x+7,y+7,j);
	video_pt(x+9,y+7,j); 
	video_pt(x+10,y+7,j);

	video_pt(x,y+8,j); 
	video_pt(x+1,y+8,j);
	video_pt(x+7,y+8,j);
	video_pt(x+8,y+8,j);
	video_pt(x+11,y+8,j); 
	video_pt(x+12,y+8,j);
	
	video_pt(x,y+9,j); 
	video_pt(x+1,y+9,j);
	video_pt(x+2,y+9,j);
	video_pt(x+7,y+9,j);
	video_pt(x+8,y+9,j); 
	video_pt(x+9,y+9,j);
	video_pt(x+12,y+9,j); 
	video_pt(x+13,y+9,j);
	video_pt(x+14,y+9,j);
	video_pt(x+15,y+9,j);
	
	
  /*	if (control && j)
	begin
		video_smallchar(x, y + 11, tens); 
		video_smallchar(x + 8, y + 11, ones);
	end
	
	if (control && !j)
	begin
		video_smallchar(x, y + 11, 12); 
	   	video_smallchar(x + 8, y + 11, 12 );
	end	*/
	
  	if (puck)
	begin
		video_pt(x+3,y+11,j);
		video_pt(x+4,y+11,j);
		video_pt(x+5,y+11,j);  
		video_pt(x+6,y+11,j);
		video_pt(x+7,y+11,j);
		video_pt(x+8,y+11,j);
		video_pt(x+9,y+11,j);
		video_pt(x+10,y+11,j);
	end
    
    if (!puck)
 	begin
		video_pt(x+3,y+11,0);
		video_pt(x+4,y+11,0);
		video_pt(x+5,y+11,0);  
		video_pt(x+6,y+11,0);
		video_pt(x+7,y+11,0);
		video_pt(x+8,y+11,0);
		video_pt(x+9,y+11,0);
		video_pt(x+10,y+11,0);
	end 
end        


void DrawPlayer2(char x, char y, char j, char puck)
begin
       /*	video_line(x,y,x+10,y,j);
	video_line(x+10 ,y ,x+10 ,y+10,j);
	video_line(x+10, y+10 ,x ,y+10,j);
	video_line(x ,y+10,x ,y ,j); */
	
	video_pt(x+9,y,j); 
 	video_pt(x+8,y,j);
  	video_pt(x+7,y,j); 
       
   	video_pt(x+9,y+1,j); 
    video_pt(x+8,y+1,j);
    video_pt(x+7,y+1,j);

	video_pt(x+11,y+2,j); 
	video_pt(x+10,y+2,j);
	video_pt(x+9,y+2,j);
	video_pt(x+8,y+2,j); 

	video_pt(x+12,y+3,j);
	video_pt(x+11,y+3,j);
	video_pt(x+10,y+3,j); 
	video_pt(x+9,y+3,j);
	
	video_pt(x+12,y+4,j);
	video_pt(x+11,y+4,j);
	video_pt(x+10,y+4,j); 
	video_pt(x+9,y+4,j);
	
 	video_pt(x+13,y+5,j);
	video_pt(x+12,y+5,j);
	video_pt(x+11,y+5,j); 
	video_pt(x+10,y+5,j);
	video_pt(x+9,y+5,j);
	video_pt(x+8,y+5,j);
	
	video_pt(x+13,y+6,j); 
	video_pt(x+12,y+6,j);
	video_pt(x+11,y+6,j);
	video_pt(x+10,y+6,j);
	video_pt(x+7,y+6,j); 
	video_pt(x+6,y+6,j);
	
	video_pt(x+13,y+7,j); 
	video_pt(x+12,y+7,j);
	video_pt(x+9,y+7,j);
	video_pt(x+8,y+7,j);
	video_pt(x+6,y+7,j); 
	video_pt(x+5,y+7,j);

	video_pt(x+15,y+8,j); 
	video_pt(x+14,y+8,j);
	video_pt(x+8,y+8,j);
	video_pt(x+7,y+8,j);
	video_pt(x+4,y+8,j); 
	video_pt(x+3,y+8,j);
	
	video_pt(x+15,y+9,j); 
	video_pt(x+14,y+9,j);
	video_pt(x+13,y+9,j);
	video_pt(x+8,y+9,j);
	video_pt(x+7,y+9,j); 
	video_pt(x+6,y+9,j);
	video_pt(x+3,y+9,j); 
	video_pt(x+2,y+9,j);
	video_pt(x+1,y+9,j);
	video_pt(x,y+9,j); 

  	if (puck)
	begin
		video_pt(x+6,y+11,j);
		video_pt(x+7,y+11,j);
		video_pt(x+8,y+11,j);
		video_pt(x+9,y+11,j);
		video_pt(x+10,y+11,j);
		video_pt(x+11,y+11,j);
		video_pt(x+12,y+11,j);
		video_pt(x+13,y+11,j); 
	end
    
    if (!puck)
 	begin
		video_pt(x+6,y+11,0);
		video_pt(x+7,y+11,0);
		video_pt(x+8,y+11,0);
		video_pt(x+9,y+11,0);
		video_pt(x+10,y+11,0);
		video_pt(x+11,y+11,0);
		video_pt(x+12,y+11,0);
		video_pt(x+13,y+11,0);  
	end 
end         

//draw goalies
//x and y coordinate are the middle of goalie, the black spot between stick and glove
void DrawGoalie1(char x, char y, char j) //face right
begin

	video_pt(x-1,y-5,j); 
 	video_pt(x,y-5,j);
	video_pt(x+1,y-5,j);
	video_pt(x+2,y-5,j); 
 	video_pt(x+3,y-5,j);
 
       
	video_pt(x-1,y-4,j); 
 	video_pt(x,y-4,j);
	video_pt(x+1,y-4,j);
	video_pt(x+2,y-4,j); 
 	video_pt(x+3,y-4,j);

  	video_pt(x-4,y-3,j); 
 	video_pt(x-3,y-3,j);
	video_pt(x-2,y-3,j); 
  	video_pt(x-1,y-3,j); 
 	video_pt(x,y-3,j);
	video_pt(x+1,y-3,j);
	video_pt(x+2,y-3,j); 
 	video_pt(x+3,y-3,j);
     
 	video_pt(x-5,y-2,j);
  	video_pt(x-4,y-2,j); 
 	video_pt(x-3,y-2,j);
	video_pt(x-2,y-2,j); 
  	video_pt(x-1,y-2,j); 
 	video_pt(x,y-2,j);
	
  	video_pt(x-5,y-1,j); 
 	video_pt(x-4,y-1,j);
	video_pt(x-3,y-1,j); 
  	video_pt(x-2,y-1,j); 
 	video_pt(x-1,y-1,j);
	video_pt(x,y-1,j);
	video_pt(x+1,y-1,j); 
 	video_pt(x+2,y-1,j);
 	video_pt(x+3,y-1,j);
 	video_pt(x+4,y-1,j);
 	
 	video_pt(x-6,y,j); 
 	video_pt(x-5,y,j);
	video_pt(x-4,y,j); 
  	video_pt(x-3,y,j); 
 	video_pt(x-2,y,j);
	video_pt(x-1,y,j);
	video_pt(x+1,y,j); 
 	video_pt(x+2,y,j);
 	video_pt(x+3,y,j);
 	video_pt(x+4,y,j);  
  
 	video_pt(x-7,y+1,j);
	video_pt(x-6,y+1,j);
	video_pt(x-5,y+1,j); 
	video_pt(x-4,y+1,j);
	video_pt(x-2,y+1,j);
	video_pt(x-1,y+1,j);
	video_pt(x,y+1,j); 
 	video_pt(x+1,y+1,j);  

 	video_pt(x-7,y+2,j);
	video_pt(x-6,y+2,j);
	video_pt(x-5,y+2,j); 
	video_pt(x-4,y+2,j);
	video_pt(x-2,y+2,j);
	video_pt(x-1,y+2,j);
	video_pt(x,y+2,j); 
 	video_pt(x+1,y+2,j);
 	video_pt(x+2,y+2,j); 
 	video_pt(x+3,y+2,j);  
	
	video_pt(x-7,y+3,j);
	video_pt(x-6,y+3,j);
	video_pt(x-5,y+3,j); 
	video_pt(x-4,y+3,j);
	video_pt(x-2,y+3,j);
	video_pt(x-1,y+3,j);
	video_pt(x,y+3,j); 
 	video_pt(x+1,y+3,j);
 	video_pt(x+3,y+3,j);
	video_pt(x+4,y+3,j); 
	video_pt(x+5,y+3,j);
	video_pt(x+6,y+3,j);
	video_pt(x+7,y+3,j);
	
	video_pt(x-7,y+4,j);
	video_pt(x-6,y+4,j);
	video_pt(x-5,y+4,j); 
	video_pt(x-4,y+4,j);
	video_pt(x-2,y+4,j);
	video_pt(x-1,y+4,j);
	video_pt(x,y+4,j); 
 	video_pt(x+1,y+4,j);
	video_pt(x+4,y+4,j); 
	video_pt(x+5,y+4,j);
	video_pt(x+6,y+4,j);
	video_pt(x+7,y+4,j);
end

void DrawGoalie2(char x, char y, char j) //face left
begin

	video_pt(x+1,y-5,j); 
 	video_pt(x,y-5,j);
	video_pt(x-1,y-5,j);
	video_pt(x-2,y-5,j); 
 	video_pt(x-3,y-5,j);
 
       
	video_pt(x+1,y-4,j); 
 	video_pt(x,y-4,j);
	video_pt(x-1,y-4,j);
	video_pt(x-2,y-4,j); 
 	video_pt(x-3,y-4,j);

  	video_pt(x+4,y-3,j); 
 	video_pt(x+3,y-3,j);
	video_pt(x+2,y-3,j); 
  	video_pt(x+1,y-3,j); 
 	video_pt(x,y-3,j);
	video_pt(x-1,y-3,j);
	video_pt(x-2,y-3,j); 
 	video_pt(x-3,y-3,j);
     
 	video_pt(x+5,y-2,j);
  	video_pt(x+4,y-2,j); 
 	video_pt(x+3,y-2,j);
	video_pt(x+2,y-2,j); 
  	video_pt(x+1,y-2,j); 
 	video_pt(x,y-2,j);
	
  	video_pt(x+5,y-1,j); 
 	video_pt(x+4,y-1,j);
	video_pt(x+3,y-1,j); 
  	video_pt(x+2,y-1,j); 
 	video_pt(x+1,y-1,j);
	video_pt(x,y-1,j);
	video_pt(x-1,y-1,j); 
 	video_pt(x-2,y-1,j);
 	video_pt(x-3,y-1,j);
 	video_pt(x-4,y-1,j);
 	
 	video_pt(x+6,y,j); 
 	video_pt(x+5,y,j);
	video_pt(x+4,y,j); 
  	video_pt(x+3,y,j); 
 	video_pt(x+2,y,j);
	video_pt(x+1,y,j);
	video_pt(x-1,y,j); 
 	video_pt(x-2,y,j);
 	video_pt(x-3,y,j);
 	video_pt(x-4,y,j);  
  
 	video_pt(x+7,y+1,j);
	video_pt(x+6,y+1,j);
	video_pt(x+5,y+1,j); 
	video_pt(x+4,y+1,j);
	video_pt(x+2,y+1,j);
	video_pt(x+1,y+1,j);
	video_pt(x,y+1,j); 
 	video_pt(x-1,y+1,j);  

 	video_pt(x+7,y+2,j);
	video_pt(x+6,y+2,j);
	video_pt(x+5,y+2,j); 
	video_pt(x+4,y+2,j);
	video_pt(x+2,y+2,j);
	video_pt(x+1,y+2,j);
	video_pt(x,y+2,j); 
 	video_pt(x-1,y+2,j);
 	video_pt(x-2,y+2,j); 
 	video_pt(x-3,y+2,j);  
	
	video_pt(x+7,y+3,j);
	video_pt(x+6,y+3,j);
	video_pt(x+5,y+3,j); 
	video_pt(x+4,y+3,j);
	video_pt(x+2,y+3,j);
	video_pt(x+1,y+3,j);
	video_pt(x,y+3,j); 
 	video_pt(x-1,y+3,j);
 	video_pt(x-3,y+3,j);
	video_pt(x-4,y+3,j); 
	video_pt(x-5,y+3,j);
	video_pt(x-6,y+3,j);
	video_pt(x-7,y+3,j);
	
	video_pt(x+7,y+4,j);
	video_pt(x+6,y+4,j);
	video_pt(x+5,y+4,j); 
	video_pt(x+4,y+4,j);
	video_pt(x+2,y+4,j);
	video_pt(x+1,y+4,j);
	video_pt(x,y+4,j); 
 	video_pt(x-1,y+4,j);
	video_pt(x-4,y+4,j); 
	video_pt(x-5,y+4,j);
	video_pt(x-6,y+4,j);
	video_pt(x-7,y+4,j);
end

char checkGoal1(){
       if(Pucky > GoalTop & Pucky < GoalBottom && (Pucky > Goalie1 + (Goalieheight+1) || Pucky < Goalie1 -(Goalieheight+1))){
       	return 1;
       }
       else if (Pucky > GoalTop & Pucky < GoalBottom){
       	if(Rand > Goalie1Save - 10 && PuckVx <= -2000)
       		return 1;
       }
}  

char checkGoal2(){
	if(Pucky > GoalTop & Pucky < GoalBottom && (Pucky > Goalie2 + (Goalieheight+1) || Pucky < Goalie2 -(Goalieheight+1))){
	        return 1;
	}
	
	else if (Pucky > GoalTop & Pucky < GoalBottom){
       	if(Rand > Goalie2Save - 10 && PuckVx >= 2000)
       		return 1;
       }
}  

char CheckGoalie1(){
	if((Pucky == Goalie1 + 4 & Puckx > 8 & Puckx < 16) || (Pucky == Goalie1 + 3 & Puckx > 8 & Puckx < 16) || (Pucky == Goalie1 & Puckx > 8 & Puckx < 13) || (Pucky == Goalie1 - 1 & Puckx > 8 & Puckx < 13) || (Pucky == Goalie1 -4 & Puckx > 8 & Puckx < 12)){
		if(Rand < Goalie1Save - 75)
			return 1;  
	}
}      

char CheckGoalie2(){
	if((Pucky == Goalie2 + 4 & Puckx < 119 & Puckx > 111) || (Pucky == Goalie2 + 3 & Puckx < 119 & Puckx > 111) || (Pucky == Goalie2 & Puckx < 119 & Puckx > 114) || (Pucky == Goalie2 - 1 & Puckx < 119 & Puckx > 114) || (Pucky == Goalie2 -4 & Puckx < 119 & Puckx > 115)){
		if(Rand < Goalie2Save - 100)
			return 1;  
	}
}
//==================================
// set up the ports and timers
void main(void)
begin
  //init timer 1 to generate sync
  OCR1A = lineTime; 	//One NTSC line
  TCCR1B = 9; 		//full speed; clear-on-match
  TCCR1A = 0x00;	//turn off pwm and oc lines
  TIMSK = 0x10;		//enable interrupt T1 cmp 
  
  //init ports
  DDRD = 0xf0;		//video out and switches
  //D.5 is sync:1000 ohm + diode to 75 ohm resistor
  //D.6 is video:330 ohm + diode to 75 ohm resistor
   
  DDRC = 0x80;	   				// Port C MSB is the gamepad select line
  PORTC = 0x7f;	 				// Set select to low, pullups enabled
            
  DDRB = 0x80;	   				// Port B MSB is the gamepad select line
  PORTB = 0x7f;	 				// Set select to low, pullups enabled
	//audio
	PORTA = 0xff;
   
  //init timer 0 to 1/uSec  
  TCCR0 = 2;
  
  //initialize synch constants 
  LineCount = 1;
  syncON = 0b00000000;
  syncOFF = 0b00100000;  
    
  
  	Player1burst = 70;
  	Player2burst = 40;
	Player1fat = 40;
	Player2fat = 40;   
	Player1fatcount = 0;
	Player2fatcount = 0;
	Player1ShotMax = 4000;
	Player2ShotMax = 4000;
	Player1ShotInc = 300;
	Player2ShotInc = 300;
  	Player1aspeed = 75;  
  	Player2aspeed =	72;
	Player1askill = 75;  
  	Player2askill = 75;
	//screen variables moved to #defines at top
	playerwidth = 15;
	playerheight = 12;   
	Player1ax = 44;
	Player1ay = 49;  
	Player2ax = 69;
	Player2ay = 49;
	Puckx = 64; 
	Pucky = 54;   
	PuckVx = 0;
	PuckVy = 0;
	PuckSlow =0;
 	P2aShoot=0; 
 	P1aShoot=0;             
 	Goalie1 =  54;
 	Goalie2 = 54;               
 	GoalTop = 41;
 	GoalBottom = 67;
	Goalieheight = 4;      
	Player1aCont = 1; 
	Player2aCont= 1; 
	Goalie1Cont= 0; 
	Goalie2Cont=0;  
	Goalie1speed = 50;
	Goalie2speed = 55;
	Player1Change = 0;      
	Player2Change = 0;
	Goalie1Save = 235;
	Goalie2Save = 230;
	running = 1;
	MinCount = 15;         
	gamestate = ST_WELCOME;
	laststate = ST_INVALID;   
	faceoff=0;      
	pause = 0;             
	period =1;    
	dmins = 20;
	dsecs = 0;
	
	
	

	
	//DrawGoalie1(8,Goalie1, 1);
	//DrawGoalie2(119,Goalie2,1);
	//DrawPlayer1(Player1ax, Player1ay,1);
	//DrawPlayer2(Player2ax, Player2ay,1);
   
	
			
	video_line(screenwidth + 1,GoalTop, screenwidth + 4, GoalTop, 1);        
	video_line(screenwidth + 1,GoalBottom, screenwidth + 4, GoalBottom, 1);	
	
	video_line(screenleft -4,GoalTop, screenleft -1, GoalTop, 1);        
	video_line(screenleft -4,GoalBottom, screenleft -1, GoalBottom, 1);     
	
	video_line(screenwidth + 1,GoalBottom, screenwidth + 1, screenheight+1, 1);        
	video_line(screenwidth + 1,GoalTop, screenwidth + 1, screentop -1, 1);		
			                              
	video_line(screenleft -1,GoalTop, screenleft -1, screentop -1, 1);        
	video_line(screenleft -1,GoalBottom, screenleft -1, screenheight+1, 1);	 
			
	video_line(screenleft -1,screentop -1, screenwidth + 1, screentop -1, 1);        
	video_line(screenleft -1,screenheight+1, screenwidth + 1, screenheight+1, 1);
	
	

	
  //init software timer
  t=0;
  time=0;  
  
  
  //enable sleep mode
  MCUCR = 0b10000000;
  #asm ("sei");
  
  //The following loop executes once/video line during lines
  //1-230, then does all of the frame-end processing
  while(1)
  begin
  
    //stall here until next line starts
    //sleep enable; mode=idle  
    //use sleep to make entry into sync ISR uniform time  
     
    #asm ("sleep"); 
    
    //The following code executes during the vertical blanking
    //Code here can be as long as  
    //a total of 60 lines x 63.5 uSec/line x 8 cycles/uSec 
    
    if (LineCount==231)
    begin 
      
    	if(!frame) {    	
       		PORTC.7 = 0; 
			#asm
			nop
			nop
			nop
			#endasm   
    		buttons1 = (PINC << 5) & 0xc0;  	// Grab bottom 3 bits
			PORTC.7 = 1;    // Set select to high
	   		#asm
			nop
			nop
			nop
			#endasm
			buttons1 = buttons1 | (PINC & 0x3f);	// Grab the other 5 bits
		
			buttons1 = ~buttons1;
		} else {
			PORTB.7 = 0; 
			#asm
			nop
			nop
			nop
			#endasm   
    		buttons2 = (PINB << 5) & 0xc0;  							// Grab bottom 3 bits
			PORTB.7 = 1;			      					// Set select to high
			#asm
			nop
			nop
			nop
			#endasm
			buttons2 = buttons2 | (PINB & 0x3f);			// Grab the other 5 bits
		
			buttons2 = ~buttons2;
		}

		switch(gamestate) {
			case ST_WELCOME:  //welcome screen
                
				statecount ++;

				if(statecount == 2){  
					sprintf(ts, "CORNELL HOCKEY");   
 	   				video_putsmalls(36, 41, ts);   
 	   				sprintf(ts, "PRESS START TO PLAY");
					video_putsmalls(24, 71, ts);        
				}       
				
				if(statecount == 3){
				 	DrawPlayer1(8, 41, 1, 0);  
				 	DrawPlayer2(105, 41, 1, 0);
				}
					
			  	if((buttons1 & BUTTON_START) || (buttons2 & BUTTON_START)) {
					video_putsmalls(36, 41, welcome1Off); 
					video_putsmalls(24, 71, welcome1Off);   
					DrawPlayer1(8, 41, 0, 0);  
				 	DrawPlayer2(105, 41, 0, 0);  
					statecount = 0;
			  		gamestate = ST_TMSEL;           
			  		Player1Score = 0;
			  		Player2Score = 0;
			  	}

				break;
				
			case ST_TMSEL:  //team select
				if(buttons1 & BUTTON_UP){
					if(Team1 > 1 & but1push == 0){ 
						but1push = 1;
						Team1--;
					}
				}        
				
				else if(buttons1 & BUTTON_DOWN){
					if(Team1 < 4 && but1push == 0){
						Team1++;
						but1push = 1;
					}
				}          
			            
			    else{
			    	but1push = 0;
			    }
				if(buttons2 & BUTTON_UP){
					if(Team2 > 1 & but2push == 0){
						Team2--;
						but2push = 1;
					}
				}        
				
				else if(buttons2 & BUTTON_DOWN){
					if(Team2 < 4 & but2push == 0){
						Team2++;
						but2push = 1;
					}
				}          
					 
				else{
					but2push = 0;
				}
				
				if(statecounterase == 0){
					if(Team1 == 1){
						DrawPlayer1(8, 35, 1, 0); 
						DrawPlayer1(8, 51, 0, 0); 
				     
					}
					if(Team2 == 1){
						DrawPlayer1(76, 35, 1, 0); 
						DrawPlayer1(76, 51, 0, 0); 
					}                                           
				 
					if(Team1 == 2){
						DrawPlayer1(8,51, 1, 0);   
						DrawPlayer1(8,35, 0, 0); 
						DrawPlayer1(8,67, 0, 0); 
					}
					if(Team2 == 2){
						DrawPlayer1(76, 51, 1, 0); 
						DrawPlayer1(76,35, 0, 0); 
						DrawPlayer1(76,67, 0, 0); 
					}            
				 
					if(Team1 == 3){
						DrawPlayer1(8, 67, 1, 0); 
						DrawPlayer1(8,51, 0, 0); 
						DrawPlayer1(8,83, 0, 0); 
					}
					if(Team2 == 3){
						DrawPlayer1(76, 67, 1, 0);  
						DrawPlayer1(76,51, 0, 0); 
						DrawPlayer1(76,83, 0, 0); 
					}            
				 
					if(Team1 == 4){
						DrawPlayer1(8, 83, 1, 0);   
						DrawPlayer1(8, 67, 0, 0); 
					}
					if(Team2 == 4){
						DrawPlayer1(76, 83, 1, 0);   
						DrawPlayer1(76, 67, 0, 0);
					}    
				}
				if(statecount == 0){
					sprintf(ts, "SELECT A");
					video_putsmalls(36, 14, ts); 
					sprintf(ts, "TEAM");
					video_putsmalls(72, 14, ts);
					sprintf(ts, "PLAYER 1");			
					video_putsmalls(16, 24, ts);   
				}
				
				else if (statecount == 1){
					sprintf(ts, "PLAYER 2");   
					video_putsmalls(84, 24, ts);   
					video_putsmalls(28, 40, CU);   
					video_putsmalls(28, 56, HU);   
			   }
			   else if (statecount == 2){		
					
					video_putsmalls(28, 72, UN);   
					video_putsmalls(28, 88, MN);   	
						
					video_putsmalls(96, 40, CU);   
					video_putsmalls(96, 56, HU);   
					video_putsmalls(96, 72, UN);   
					video_putsmalls(96, 88, MN);   
				}		  
			
				statecount++;		

				if((buttons1 & BUTTON_A) || (buttons2 & BUTTON_A)) {
					statecount = 3;
					if(statecounterase == 0){
						statecounterase = 1;
					}
				}
			 
			  			          
			  	if(statecounterase == 1){
					video_putsmalls(36, 14, periodEndOff); 			
					video_putsmalls(16, 24, periodEndOff);   
					
				}
				
				else if (statecounterase  == 2){   
					video_putsmalls(68, 24, periodEndOff);   
					video_putsmalls(28, 40, TE);   
					video_putsmalls(28, 56, TE);    
					video_putsmalls(28, 72, TE);   
					video_putsmalls(28, 88, TE);  
				     
			   }
			   else if (statecounterase  == 3){		 	
						
					video_putsmalls(96, 40, TE);   
					video_putsmalls(96, 56, TE);   
					video_putsmalls(96, 72, TE);   
					video_putsmalls(96, 88, TE);  
					
					if(Team1 == 1){
				     DrawPlayer1(8, 35, 0, 0);   
				     //gamestate = ST_PLSELCU;
				    }
				     
				 	if(Team2 == 1)
				     DrawPlayer1(76, 35, 0, 0); 
				    if(Team1 == 2){
				     DrawPlayer1(8,51, 0, 0);     
				     //gamestate = ST_PLSELHU;
				     }
				    if(Team2 == 2)
				     DrawPlayer1(76,51, 0, 0);   
				    if(Team1 == 3){
				     DrawPlayer1(8, 67, 0, 0);   
				     //gamestate = ST_PLSELUN;
				     }
				 	if(Team2 == 3)
				     DrawPlayer1(76, 67, 0, 0); 
				    if(Team1 == 4){
				     DrawPlayer1(8,83, 0, 0);     
				     //gamestate = ST_PLSELMN;
				    }
				    if(Team2 == 4)
				     DrawPlayer1(76,83, 0, 0);  
				  
				  Teamselect = Team1;
				  gamestate = ST_PLSEL;
				  Playerselect = 1;
					statecount = 0;
					statecounterase = 0; 
				}	

			   if(statecounterase > 0){
				   statecounterase++;
			   }
				break;
	  
			case ST_PLSEL:  //player select 
				if(buttons1 & BUTTON_UP ){
					if(Player > 1 & but1push == 0){ 
						but1push = 1;
						Player--;
					}
				}        
				
				else if(buttons1 & BUTTON_DOWN ){
					if(Player < 3 && but1push == 0){
						Player++;
						but1push = 1;
					}
				}          
			            
			    else{
			    	but1push = 0;
			    }
				if(buttons2 & BUTTON_UP ){
					if(Player > 1 & but2push == 0){
						Player--;
						but2push = 1;
					}
				}        
				
				else if(buttons2 & BUTTON_DOWN ){
					if(Player < 3 & but2push == 0){
						Player++;
						but2push = 1;
					}
				}          
					 
				else{
					but2push = 0;
				}
				
				if(statecounterase == 0){                             
				 
					if(Player == 1){
						DrawPlayer1(8,51, 1, 0);   
						DrawPlayer1(8,67, 0, 0); 
					}
				 
					if(Player == 2){
						DrawPlayer1(8, 67, 1, 0); 
						DrawPlayer1(8,51, 0, 0); 
						DrawPlayer1(8,83, 0, 0); 
					}
				 
					if(Player == 3){
						DrawPlayer1(8, 83, 1, 0);   
						DrawPlayer1(8, 67, 0, 0); 
					}
				}
				
				if(statecount == 0){
					
					
					if(Playerselect == 1){
						if(Team1 ==1 )					
							video_putsmalls(48, 14, CU); 
						else if (Team1 == 2)
							video_putsmalls(48, 14, HU); 
						else if (Team1 == 3)
							video_putsmalls(48, 14, UN); 
						else if (Team1 == 4)
							video_putsmalls(48, 14, MN); 
						sprintf(ts, "PLAYER 1");			
						video_putsmalls(16, 24, ts);   
					}
					else{            
					
						if(Team2 ==1 )					
							video_putsmalls(48, 14, CU); 
						else if (Team2 == 2)
							video_putsmalls(48, 14, HU); 
						else if (Team2 == 3)
							video_putsmalls(48, 14, UN); 
						else if (Team2 == 4)
							video_putsmalls(48, 14, MN); 
						sprintf(ts, "PLAYER 2");			
						video_putsmalls(16, 24, ts);   
					}
				
				}
				
				else if (statecount == 1){
					sprintf(ts, "SPEED");
					video_putsmalls(40, 40, ts);
					
					sprintf(ts, "SKILL");
					video_putsmalls(64, 40, ts);
					
					sprintf(ts, "SHOT");
					video_putsmalls(88, 40, ts);
					
					
			   }
			   else if (statecount == 2){		
			   		
					if(Teamselect == 1){
						sprintf(ts, "21");
						video_putsmalls(28, 56, ts);   
					
						sprintf(ts, "88");
						video_putsmalls(40, 56, ts); 
					
						sprintf(ts, "99");
						video_putsmalls(64, 56, ts); 
					
						sprintf(ts, "95");
						video_putsmalls(88, 56, ts); 
					}
					else if (Teamselect == 2){
				      sprintf(ts, "17");
						video_putsmalls(28, 56, ts);   
					
						sprintf(ts, "98");
						video_putsmalls(40, 56, ts); 
					
						sprintf(ts, "99");
						video_putsmalls(64, 56, ts); 
					
						sprintf(ts, "90");
						video_putsmalls(88, 56, ts); 
					}         
					
					else if (Teamselect == 3){
						sprintf(ts, "18");
						video_putsmalls(28, 56, ts);   
					
						sprintf(ts, "90");
						video_putsmalls(40, 56, ts); 
					
						sprintf(ts, "95");
						video_putsmalls(64, 56, ts); 
					
						sprintf(ts, "90");
						video_putsmalls(88, 56, ts); 
					}
					           
					else if (Teamselect ==4){
						sprintf(ts, "26");
						video_putsmalls(28, 56, ts);   
					
						sprintf(ts, "99");
						video_putsmalls(40, 56, ts); 
					
						sprintf(ts, "99");
						video_putsmalls(64, 56, ts); 
					
						sprintf(ts, "90");
						video_putsmalls(88, 56, ts);      
					}
				}		  
				
				else if (statecount == 3){
					if (Teamselect == 1){
						sprintf(ts, "06");
						video_putsmalls(28, 72, ts);   
					
						sprintf(ts, "92");
						video_putsmalls(40, 72, ts); 
					
						sprintf(ts, "95");
						video_putsmalls(64, 72, ts); 
					
						sprintf(ts, "99");
						video_putsmalls(88, 72, ts); 
					}          
					else if (Teamselect ==2){
						sprintf(ts, "16");
						video_putsmalls(28, 72, ts);   
					
						sprintf(ts, "90");
						video_putsmalls(40, 72, ts); 
						
						sprintf(ts, "90");
						video_putsmalls(64, 72, ts); 
					
						sprintf(ts, "85");
						video_putsmalls(88, 72, ts); 
					}
					
					else if (Teamselect ==3){              
						sprintf(ts, "12");
						video_putsmalls(28, 72, ts);   
					
						sprintf(ts, "93");
						video_putsmalls(40, 72, ts); 
					
						sprintf(ts, "92");
						video_putsmalls(64, 72, ts); 
					
						sprintf(ts, "82");
						video_putsmalls(88, 72, ts);  
					}
					
					else if (Teamselect == 4){
					   sprintf(ts, "18");
						video_putsmalls(28, 72, ts);   
					
						sprintf(ts, "94");
						video_putsmalls(40, 72, ts); 
					
						sprintf(ts, "93");
						video_putsmalls(64, 72, ts); 
					
						sprintf(ts, "92");
						video_putsmalls(88, 72, ts); 
					}
					
				}
				
				else if (statecount == 4){
				   if(Teamselect == 1){
						sprintf(ts, "08");
						video_putsmalls(28, 88, ts);   
					
						sprintf(ts, "97");
						video_putsmalls(40, 88, ts); 
					
						sprintf(ts, "98");
						video_putsmalls(64, 88, ts); 
					
						sprintf(ts, "92");
						video_putsmalls(88, 88, ts); 
					}
					else if (Teamselect == 2){
						sprintf(ts, "18");
						video_putsmalls(28, 88, ts);   
					
						sprintf(ts, "60");
						video_putsmalls(40, 88, ts); 
					
						sprintf(ts, "60");
						video_putsmalls(64, 88, ts); 
					
						sprintf(ts, "60");
						video_putsmalls(88, 88, ts); 
						
					}
					
					else if (Teamselect == 3){
						sprintf(ts, "08");
						video_putsmalls(28, 88, ts);   
					
						sprintf(ts, "60");
						video_putsmalls(40, 88, ts); 
					
						sprintf(ts, "60");
						video_putsmalls(64, 88, ts); 
					
						sprintf(ts, "60");
						video_putsmalls(88, 88, ts); 
					}        
					
					else if (Teamselect == 4){
						sprintf(ts, "13");
						video_putsmalls(28, 88, ts);   
					
						sprintf(ts, "60");
						video_putsmalls(40, 88, ts); 
					
						sprintf(ts, "60");
						video_putsmalls(64, 88, ts); 
					
						sprintf(ts, "60");
						video_putsmalls(88, 88, ts); 
					}
						
					 
				}
			
				statecount++;		

				if((buttons1 & BUTTON_START ) || (buttons2 & BUTTON_START )) {
					statecount = 6;
					if(statecounterase == 0){
						statecounterase = 1;
					}
				}
			 
			  			          
			  	if(statecounterase == 1){
					video_putsmalls(16, 24, periodEndOff); 			
					video_putsmalls(48, 14, TE);   
					
				}
				
				else if (statecounterase  == 2){   
					
					sprintf(ts, "     ");
					video_putsmalls(40, 40, ts);   
					video_putsmalls(64, 40, ts);    
					video_putsmalls(88, 40, ts);   
					
				     
			   }
			   else if (statecounterase  == 3){		 	
						
					video_putsmalls(28, 56, TE);   
					video_putsmalls(40, 56, TE);   
					video_putsmalls(64, 56, TE);   
					video_putsmalls(88, 56, TE);  
					
				}
				
				else if (statecounterase == 4){
					
					video_putsmalls(28, 72, TE);   
					video_putsmalls(40, 72, TE);   
					video_putsmalls(64, 72, TE);   
					video_putsmalls(88, 72, TE);  
					
				}
				
				else if (statecounterase == 5){
					video_putsmalls(28, 88, TE);   
					video_putsmalls(40, 88, TE);   
					video_putsmalls(64, 88, TE);   
					video_putsmalls(88, 88, TE);  
				
					
				}
				
				else if (statecounterase == 6){
				
					if(Playerselect == 1){
						if (Teamselect == 1){
							Goalie1Save = 245;
							Goalie1speed = 70;
						}
						
						else if (Teamselect ==2){
							Goalie1Save = 230;
							Goalie1speed = 55;
						}
						
						else if (Teamselect ==3){
							Goalie1Save = 235;
							Goalie1speed = 60;
						}
						
						else if (Teamselect == 4){
						 	Goalie1Save = 220;
							Goalie1speed = 50;
						}
					}
					else {
						if (Teamselect == 1){
							Goalie2Save = 245;
							Goalie2speed = 70;
						}
						
						else if (Teamselect ==2){
							Goalie2Save = 230;
							Goalie2speed = 55;
						}
						
						else if (Teamselect ==3){
							Goalie2Save = 235;
							Goalie2speed = 60;
						}
						
						else if (Teamselect == 4){
						 	Goalie2Save = 220;
							Goalie2speed = 50;
						}
					}
				}
				
				if(statecounterase == 7){
				    if(Player == 1){
				     	DrawPlayer1(8,51, 0, 0);  
				     	if(Playerselect == 1 & Teamselect == 1){
				     		Player1burst = 50;
							Player1ShotMax = 4750; 
							Player1ShotInc = 375;
							Player1aspeed = 70;
							Player1askill = 100;
				     	
					     }
					    else if (Playerselect == 1 & Teamselect == 2){
					    	Player1burst = 60;
							Player1ShotMax = 4250; 
							Player1ShotInc = 300;
							Player1aspeed = 74;
							Player1askill = 98;
					    
					    }   
					    
					    else if (Playerselect ==1 & Teamselect == 3){
					    	Player1burst = 60;
							Player1ShotMax = 4250; 
							Player1ShotInc = 300;
							Player1aspeed = 70;
							Player1askill = 95;
					    
					    }           
					    
					    else if (Playerselect == 1 & Teamselect == 4){
					    	Player1burst = 50;
							Player1ShotMax = 4500; 
							Player1ShotInc = 325;
							Player1aspeed = 90;
							Player1askill = 99;
					    	    
					    }
				     
					    else if(Playerselect == 2 & Teamselect == 1){
				     		Player2burst = 50;
							Player2ShotMax = 4750; 
							Player2ShotInc = 375;
							Player2aspeed = 70;
							Player2askill = 100;
				     	
					     }
					    else if (Playerselect == 2 & Teamselect == 2){
					    	Player2burst = 60;
							Player2ShotMax = 4250; 
							Player2ShotInc = 300;
							Player2aspeed = 74;
							Player2askill = 98;
					    
					    }   
					    
					    else if (Playerselect ==2 & Teamselect == 3){
					    	Player2burst = 60;
							Player2ShotMax = 4250; 
							Player2ShotInc = 300;
							Player2aspeed = 70;
							Player2askill = 95;
					    
					    }           
					    
					    else if (Playerselect == 2 & Teamselect == 4){
					    	Player2burst = 50;
							Player2ShotMax = 4500; 
							Player2ShotInc = 325;
							Player2aspeed = 90;
							Player2askill = 99;
					    	    
					    }
				     
				    }
				}
				if(statecounterase == 8){
		
				    if(Player == 2){
					     DrawPlayer1(8, 67, 0, 0); 
					   
					   if(Playerselect == 1 & Teamselect == 1){
					     		Player1burst = 60;
								Player1ShotMax = 5000; 
								Player1ShotInc = 400;
								Player1aspeed = 72;
								Player1askill = 95;
						 } 
						 else if (Playerselect == 1 & Teamselect == 2){
						 	Player1burst = 60;
							Player1ShotMax = 4000; 
							Player1ShotInc = 300;
							Player1aspeed = 70;
							Player1askill = 90;
						 
						 }    
						 else if (Playerselect == 1 & Teamselect == 3){
						   Player1burst = 60;
							Player1ShotMax = 4100; 
							Player1ShotInc = 300;
							Player1aspeed = 70;
							Player1askill = 90;
						 }
						 
						 
						 else if (Playerselect == 1 & Teamselect == 4){
						 	Player1burst = 60;
							Player1ShotMax = 4000; 
							Player1ShotInc = 300;
							Player1aspeed = 70;
							Player1askill = 90;
						 }
						 
					  	else if(Playerselect == 2 & Teamselect == 1){
					     		Player2burst = 60;
								Player2ShotMax = 5000; 
								Player2ShotInc = 400;
								Player2aspeed = 72;
								Player2askill = 95;
						 } 
						 else if (Playerselect == 2 & Teamselect == 2){
						 	Player2burst = 60;
							Player2ShotMax = 4000; 
							Player2ShotInc = 300;
							Player2aspeed = 70;
							Player2askill = 90;
						 
						 }    
						 else if (Playerselect == 2 & Teamselect == 3){
						   Player2burst = 60;
							Player2ShotMax = 4100; 
							Player2ShotInc = 300;
							Player2aspeed = 70;
							Player2askill = 90;
						 }
						 
						 
						 else if (Playerselect == 2 & Teamselect == 4){
						 	Player2burst = 60;
							Player2ShotMax = 4000; 
							Player2ShotInc = 300;
							Player2aspeed = 70;
							Player2askill = 90;
						 }
				    }
				}
				if (statecounterase == 9){
				    if(Player == 3){
					   DrawPlayer1(8,83, 0, 0); 
					   
					   if(Playerselect == 1 & Teamselect == 1){
						     	Player1burst = 60;
								Player1ShotMax = 4500; 
								Player1ShotInc = 300;
								Player1aspeed = 80;
								Player1askill = 95;
						 }     
						else if (Playerselect == 1 & Teamselect > 1){
							   Player1burst = 30;
								Player1ShotMax = 3500; 
								Player1ShotInc = 275;
								Player1aspeed = 65;
								Player1askill = 60;
						}
						 
						else if(Playerselect == 2 & Teamselect == 1){
						     	Player2burst = 60;
								Player2ShotMax = 4500; 
								Player2ShotInc = 300;
								Player2aspeed = 80;
								Player2askill = 95;
						 }     
						else if (Playerselect == 2 & Teamselect > 1){
							   Player2burst = 30;
								Player2ShotMax = 3500; 
								Player2ShotInc = 275;
								Player2aspeed = 65;
								Player2askill = 60;
						}
					     
				    }
				  
				    if(Playerselect == 2){
						gamestate = ST_LENSEL; 
						Player = 1;
						Playerselect = 1;
					}  
					else  {
						gamestate = ST_PLSEL;
						Player = 1;
						Playerselect = 2;
						Teamselect = Team2;
					}
					
					statecount = 0;
					statecounterase = 0; 
				}	

			   	if(statecounterase > 0){
				   statecounterase++;
			   }
				
			break;
				
			case ST_LENSEL:  //game length select
				if(buttons1 & BUTTON_UP  || buttons2 & BUTTON_UP){
					if(GameLength > 1 & but1push == 0){ 
						but1push = 1;
						GameLength--;
					}
				}        
				
				else if(buttons1 & BUTTON_DOWN || buttons2 & BUTTON_DOWN){
					if(GameLength  < 4 && but1push == 0){
						GameLength++;
						but1push = 1;
					}
				}          
			            
			    else{
			    	but1push = 0;
			    }
				
				if(statecounterase == 0){
					if(GameLength == 1){
						DrawPlayer1(40, 35, 1, 0); 
						DrawPlayer1(40, 51, 0, 0); 
				     
					}                            
				 
					if(GameLength == 2){
						DrawPlayer1(40,51, 1, 0);   
						DrawPlayer1(40,35, 0, 0); 
						DrawPlayer1(40,67, 0, 0); 
					}
				 
				 
					if(GameLength == 3){
						DrawPlayer1(40, 67, 1, 0); 
						DrawPlayer1(40,51, 0, 0); 
						DrawPlayer1(40,83, 0, 0); 
					}
				 
				 
					if(GameLength == 4){
						DrawPlayer1(40, 83, 1, 0);   
						DrawPlayer1(40, 67, 0, 0); 
					}
				}

				if(statecount == 0){
					sprintf(ts, "PERIOD");
					video_putsmalls(36, 14, ts); 
					sprintf(ts, "LENGTH");
					video_putsmalls(64, 14, ts);
				}
				
				else if (statecount == 1){

					sprintf(ts, "10");
					video_putsmalls(60, 40, ts);   
					sprintf(ts, "5");
					video_putsmalls(60, 56, ts);   
			   }
			   else if (statecount == 2){		
					sprintf(ts, "3");
					video_putsmalls(60, 72, ts);   
					sprintf(ts, "2");
					video_putsmalls(60, 88, ts);   	
						 
			}		  
			
			statecount++;				
				if((buttons1 & BUTTON_START) || (buttons2 & BUTTON_START)) {
					statecount = 3;
					if(statecounterase == 0){
						statecounterase = 1;
					}
				}
			 
			  			          
			  	if(statecounterase == 1){
					video_putsmalls(36, 14, periodEndOff); 			
	
				}
				
				else if (statecounterase  == 2){   
					video_putsmalls(60, 40, TE);   
					video_putsmalls(60, 56, TE);    
					video_putsmalls(60, 72, TE);   
					video_putsmalls(60, 88, TE);  
				     
			   }
			   else if (statecounterase  == 3){		 	
					
					if(GameLength == 1){
						DrawPlayer1(40, 35, 0, 0);   
						MinCount = 30;

					}
					if(GameLength == 2){
						DrawPlayer1(40,51, 0, 0);     
						MinCount = 15;
					}
					if(GameLength == 3){
						DrawPlayer1(40, 67, 0, 0);   
						MinCount = 9;
					}
					if(GameLength == 4){
						DrawPlayer1(40,83, 0, 0);     
						MinCount = 6;
					}

					gamestate = ST_FACEOFF; 
					statecount = 0;
					statecounterase = 0; 
				}		  

				if(statecounterase > 0){
				   statecounterase++;
				}
				break;
				
			case ST_PAUSE:
				  
			  	if(pause == 0){
	  				DrawPlayer1(Player1ax, Player1ay,0,Player1aPuck);
					DrawPlayer2(Player2ax, Player2ay,0,Player2aPuck);
					DrawGoalie1(8,Goalie1, 0);
					DrawGoalie2(119,Goalie2,0);
				 	video_pt(Puckx, Pucky, 0);    
				}
				 	
			  	pause++;
	  	
			  	if( pause > 60){
	  				pause = 61;
					if((buttons1 & BUTTON_START) || (buttons2 & BUTTON_START)){
				  	   	gamestate = 7;        
				  		pause = 0;
				 	}
			  	}

				break;
				
			case ST_GOAL:  //goal message
			    
			  	goalscore ++;
			  	
				if(goalscore==1) { //"GOAL!"
					video_putsmalls(56, 23, goal);       
					if (songstate != SONG_DAVY) {
						songstate = SONG_DAVY;
						inote = 0;
						tnote = 0;
					}
	  			}
	  			
				if(goalscore > 120 & period > 3){
					gamestate = ST_ENDGAME;         
					video_putsmalls(56, 23, goalOff);
				}
			  	if(goalscore > 180) {
			  		laststate = ST_GOAL;
			  		gamestate = ST_FACEOFF; 
			  		//erase the "goal!" message
					video_putsmalls(56, 23, goalOff);
			  	}
				
				break;
				
			case ST_FACEOFF:  //faceoff
	  
				faceoff++;
				
				if (faceoff == 2){

					Player1aPuck = 0; 
				 	Player2aPuck = 0; 
					
			    	DrawPlayer1(Player1ax, Player1ay,0,Player1aPuck);
					DrawPlayer2(Player2ax, Player2ay,0,Player2aPuck);
				 	DrawGoalie1(8,Goalie1, 0);
					DrawGoalie2(119,Goalie2,0);
			    	video_pt(Puckx, Pucky, 0);    
		    
				    Player1ax = 44;
					Player1ay = 49;  
					Player2ax = 69;
					Player2ay = 49;
					Puckx = 64; 
					Pucky = 54;   
					PuckVx = 0;
					PuckVy = 0;     
			 
					Goalie1 =  54;
				 	Goalie2 = 54; 
			 				
					DrawGoalie1(8,Goalie1, 1);
					DrawGoalie2(119,Goalie2,1);
					DrawPlayer1(Player1ax, Player1ay,1,Player1aPuck);
					DrawPlayer2(Player2ax, Player2ay,1,Player2aPuck);	   
				
					Player1aCont = 1; 
					Player2aCont= 1; 
					Goalie1Cont= 0; 
					Goalie2Cont=0;  	 	
				
					PuckSlow =0;
				 	P2aShoot=0; 
				 	P1aShoot=0; 
			 	
				 	
			 	
		   	   	}	
		   	if(faceoff == 3){                           
		   	                     
		   		sprintf(ts, "P");
		   		video_putsmalls(48, 0, ts);
		   		sprintf(ts, "%1d", period);
		   		video_putsmalls(52,0,ts);
		   		
			 	    sprintf(ts, "%02d", dmins);
					video_putsmalls(60,0,ts);
			
					video_smallchar(68,0,10);
					sprintf(ts, "%02d",dsecs);
					video_putsmalls(72,0,ts);
			 		
			 		
			 		if(Team1 == 1)
						video_putsmalls(4,0, CU);
					else if (Team1 == 2)
					    video_putsmalls(4,0, HU);
					else if (Team1 == 3)
						video_putsmalls(4,0, UN);
					else if (Team1 == 4)
						video_putsmalls(4,0, MN);
					
					sprintf(ts, "%02i", Player1Score); 
			   	   	video_putsmalls(16,0,ts);
			
					if(Team2 == 1)
						video_putsmalls(104,0, CU);
					else if (Team2 == 2)
					    video_putsmalls(104,0, HU);
					else if (Team2 == 3)
						video_putsmalls(104,0, UN);
					else if (Team2 == 4)
						video_putsmalls(104,0, MN);  
						
					sprintf(ts, "%02i", Player2Score); 
		   	   		video_putsmalls(116, 0,ts);     
				}
			
				if(faceoff >= 180){   
					faceoff = 0;
					gamestate =7;
				}				
		
				break;
				
			case ST_PLAY:
		
				Rand++;  
				
				if(Rand == 254){
					 Rand = 0;
				}
				prevPlayer1ax = Player1ax;
		    	prevPlayer1ay = Player1ay;
		    	prevPlayer2ax = Player2ax;
		    	prevPlayer2ay = Player2ay;
		    	prevPuckx = Puckx;
		    	prevPucky = Pucky;
		  
			    if(buttons1 & BUTTON_A) { //Change to Goale
			    	Player1Change = 1;
			    }
		    
				if(Player1Change ==1 & !(buttons1 & BUTTON_A)){
		  			Player1Change = 0;
			    	if(Player1aCont ==1) {
						Player1aCont = 0;
						Goalie1Cont = 1;
					} else if(Goalie1Cont ==1) {
						Player1aCont = 1;
						Goalie1Cont = 0;
					}
				}
			
				if(buttons2 & BUTTON_A) { //Change to Goale
					Player2Change = 1;
			    }
		    
				if(Player2Change ==1 & !(buttons2 & BUTTON_A)){
					Player2Change = 0;
					if(Player2aCont ==1){
						Player2aCont = 0;
						Goalie2Cont = 1;
					} else if(Goalie2Cont ==1){
						Player2aCont = 1;
						Goalie2Cont = 0;
					}
				}      
			
				if(Player1aCont ==1){
					if(buttons1 & BUTTON_B) { //Speed Burst   
						if(Player1fatcount < Player1fat){
							Player1addspeed = Player1burst;
						  	Player1fatcount++;
						}
						else
							Player1addspeed = 0;
					}    
					else{
						
						if(Player1fatcount <= 1){
							Player1fatcount =1;
						}      
						Player1fatcount--;
						Player1addspeed = 0;
					}
	   				if(buttons1 & BUTTON_UP) { //up   
						p1au = p1au + Player1aspeed + Player1addspeed;
					}

					if(buttons1 & BUTTON_DOWN) { //down     
						p1ad = p1ad + Player1aspeed + Player1addspeed;
					}

					if(buttons1 & BUTTON_LEFT) { //left
						p1al = p1al + Player1aspeed + Player1addspeed;
					}

					if(buttons1 & BUTTON_RIGHT) { //right   
						p1ar = p1ar + Player1aspeed + Player1addspeed;
					}               
				     
					if(buttons1 & BUTTON_C) { //Shoot   
					 	if(Player1aPuck == 1){
					 		if(P1aShoot == 0){
					 			PuckVx = 0;
				 				PuckVy = 0;  
				 			}
					 		P1aShoot = 1;
					 	    PuckVx += Player1ShotInc;
					 	    if(PuckVx >= Player1ShotMax)
					 	    	PuckVx = Player1ShotMax;
					 	    if(buttons1 & BUTTON_UP)
				 		    	PuckVy -=Player1ShotInc;
				 	    	if(buttons1 & BUTTON_DOWN)
					 	    	PuckVy +=Player1ShotInc;
					 	    if(PuckVy >= Player1ShotMax)
					 	    	PuckVy = Player1ShotMax;
					 	    if(PuckVy <= -Player1ShotMax)
					 	    	PuckVy = -Player1ShotMax;
						}                
					}
				
					if(P1aShoot==1 & !(buttons1&BUTTON_C)){
						P1aShoot = 0;
						Player1aPuck = 0;
						Puckxdir = PuckVx;
						Puckydir = PuckVy;
					
						Puckx = 16 + Player1ax;
						Pucky = Player1ay + 9;
					
					}
				                          
				}//end if Player1aCont
				else{
			 		if(Player1ax + 15 < Puckx)
						p1ar = p1ar + Player1aspeed -5;
			 		if(Player1ax + 15 > Puckx)
			 	    	p1al = p1al + Player1aspeed -5 ;   
				 	if(Player1ay + 10 < Pucky)
				 		p1ad = p1ad + Player1aspeed -5 ;
				 	if(Player1ay + 10 > Pucky)
				 		p1au = p1au + Player1aspeed -5;
				}
			
				if(Goalie1Cont ==1){
					if(buttons1 & BUTTON_UP) { //up   
						g1u = g1u + Goalie1speed;
					}

					if(buttons1 & BUTTON_DOWN) { //down     
						g1d = g1d + Goalie1speed;
					}                
				} else if (Puckx < 70){
					if(Goalie1 < Pucky)
						g1d = g1d + Goalie1speed;
					else if (Goalie1 > Pucky)
						g1u = g1u + Goalie1speed;
				}    
			
				if(Player2aCont ==1){	
					if(buttons2 & BUTTON_B) { //Speed Burst   
						if(Player2fatcount < Player2fat){
							Player2addspeed = Player2burst;
						  	Player2fatcount++;
						}
						else
							Player2addspeed = 0;
					}    
					else{
						
						if(Player2fatcount <= 1){
							Player2fatcount =1;
						}      
						Player2fatcount--;
						Player2addspeed = 0;
					}
					if(buttons2 & BUTTON_UP) { //up       
						p2au = p2au + Player2aspeed + Player2addspeed;
					}

					if(buttons2 & BUTTON_DOWN) { //down     
						p2ad = p2ad + Player2aspeed + Player2addspeed;
					}

					if(buttons2 & BUTTON_LEFT) { //left
						p2al = p2al + Player2aspeed + Player2addspeed;
					}

					if(buttons2 & BUTTON_RIGHT) { //right   
						p2ar = p2ar + Player2aspeed + Player2addspeed;
					}               
				
					if(buttons2 & BUTTON_C) { //Shoot   
				 		if(Player2aPuck == 1){    
				 			if(P2aShoot == 0){
					 			PuckVx = 0;
					 			PuckVy = 0;  
					 		}
					 		P2aShoot = 1;  
					 		/*
					 	    PuckVx -= Player2ShotInc;
				 		    if(PuckVx <= -Player2ShotMax)
					 	    	PuckVx = -Player2ShotMax;
					 	    if(buttons2 & BUTTON_UP)
					 	    	PuckVy -=Player2ShotInc;
					 	    if(buttons2 & BUTTON_DOWN)
					 	    	PuckVy +=Player2ShotInc;
					 	    if(PuckVy >= Player2ShotMax)
				 		    	PuckVy = Player2ShotMax;
					 	    if(PuckVy <= -Player2ShotMax)
					 	    	PuckVy = -Player2ShotMax;
				 	    	*/
				 	    	PuckVx -= 300;
				 		    if(PuckVx <= -Player2ShotMax)
					 	    	PuckVx = -Player2ShotMax;
					 	    if(buttons2 & BUTTON_UP)
					 	    	PuckVy -=Player2ShotInc;
					 	    if(buttons2 & BUTTON_DOWN)
					 	    	PuckVy +=Player2ShotInc;
					 	    if(PuckVy >= Player2ShotMax)
				 		    	PuckVy = Player2ShotMax;
					 	    if(PuckVy <= -Player2ShotMax)
					 	    	PuckVy = -Player2ShotMax;
					 		//Puckx =   Player2ax + PuckVx;	       
					 	  	//PuckVy = Player2ay - prevPlayer2ay ;
					 	  	//Pucky = Player2ay + 9 + PuckVy ;
				 		}                
					}  
				
					if(P2aShoot==1 & ! (buttons2&BUTTON_C)){
						P2aShoot = 0;
						Player2aPuck = 0;
						
						Puckxdir = PuckVx;
						Puckydir = PuckVy;      
					
						Pucky = Player2ay + 9;
						Puckx = Player2ax -1;
					}
				} else {
					if(Player2ax + 15 < Puckx)
			 	    	p2ar = p2ar + Player2aspeed -5;
				 	if(Player2ax + 15 > Puckx)
			 	    	p2al = p2al + Player2aspeed -5 ;   
			 		if(Player2ay + 10 < Pucky)
			 	    	p2ad = p2ad + Player2aspeed -5 ;
			 		if(Player2ay + 10 > Pucky)
			 	 		p2au = p2au + Player2aspeed -5;
				}
			                 
				if(Goalie2Cont ==1){
		   			if(buttons2 & BUTTON_UP) { //up   
						g2u = g2u + Goalie2speed;
					}

					if(buttons2 & BUTTON_DOWN) { //down     
						g2d = g2d + Goalie2speed;
					}      

				} else if (Puckx > 58){
					if(Goalie2 < Pucky)
						g2d = g2d + Goalie2speed;
			 		else if (Goalie2 > Pucky)
			 			g2u = g2u + Goalie2speed;		
				}
				    	
			   	//if(!frame) {			
			 		//Erase the previous player
  					DrawPlayer1(Player1ax, Player1ay,0,Player1aPuck);  
  			
	           		if(p1au >= 140) { //up     
	           			p1au = p1au - 140;  
						Player1ay -= 1;
			 			if(Player1ay < screentop)
			 				Player1ay = screentop;
					}

					if(p1ad >= 140) { //down     
						p1ad = p1ad - 140;
						Player1ay += 1;
						if(Player1ay + playerheight >= screenheight) Player1ay =  screenheight - playerheight;
					}

					if(p1al >= 140) { //left       
						p1al = p1al - 140;
						Player1ax -= 1;
						if(Player1ax <screenleft) Player1ax = screenleft;
					}

					if(p1ar >= 140) { //right   
						p1ar = p1ar - 140;
						Player1ax += 1;
						if(Player1ax + playerwidth > screenwidth) Player1ax = screenwidth - playerwidth;
					}               
					//Draw the new player
					DrawPlayer1(Player1ax, Player1ay,1,Player1aPuck);   					
				 	
				if(!frame) {
				 	DrawGoalie1(8,Goalie1, 0);
				 	
					if(g1u >= 140) { //up     
	           			g1u = g1u - 140;  
						Goalie1 -= 1;
			 			if(Goalie1 - Goalieheight <= GoalTop) 
			 				Goalie1 = GoalTop + Goalieheight +1;
					}

					if(g1d >= 140) { //down     
						g1d = g1d - 140;
						Goalie1 += 1;
						if(Goalie1 + Goalieheight >= GoalBottom) 
							Goalie1 =  GoalBottom - Goalieheight  -1;
					}                       
					DrawGoalie1(8,Goalie1, 1);
     			}
				//} else {                                                      
            		//Erase the previous 2nd player
  					DrawPlayer2(Player2ax, Player2ay,0,Player2aPuck);   
  				
  					if(p2au >=140) { //up       
	           			p2au = p2au - 140;
						Player2ay -= 1;
			 			if(Player2ay < screentop)
			 				Player2ay = screentop;
					}

					if(p2ad >=140) { //down  
						p2ad = p2ad - 140;   
						Player2ay += 1;
						if(Player2ay + playerheight >= screenheight)
							Player2ay =  screenheight - playerheight;
					}

					if(p2al >=140) { //left
						p2al = p2al - 140;
						Player2ax -= 1;
						if(Player2ax <screenleft) Player2ax = screenleft;
					}

					if(p2ar >=140) { //right   
						p2ar = p2ar - 140;
						Player2ax += 1;
						if(Player2ax + playerwidth > screenwidth) Player2ax = screenwidth - playerwidth;
					}            
					
					//Draw the new 2nd player
					DrawPlayer2(Player2ax, Player2ay,1,Player2aPuck);  
				 if(frame){
					
					DrawGoalie2(119,Goalie2, 0);
					if(g2u >= 140) { //up     
	           			g2u = g2u - 140;  
						Goalie2 -= 1;
			 			if(Goalie2 - Goalieheight <= GoalTop) 
			 				Goalie2 = GoalTop + Goalieheight +1;
					}

					if(g2d >= 140) { //down     
						g2d = g2d - 140;
						Goalie2 += 1;
						if(Goalie2 + Goalieheight >= GoalBottom) 
							Goalie2 =  GoalBottom - Goalieheight  -1;
					}                       
					DrawGoalie2(119,Goalie2, 1);    
			
				}//else frame
				      
				//Draw the Puck if it is on its own
				if(Player1aPuck == 1 || Player2aPuck ==1) {

					if(Player1aPuck == 1 && Player2ax < Player1ax + 15 && Player2ax +8 > Player1ax + 15 && Player2ay + 5 < Player1ay + 9 && Player2ay +10 > Player1ay + 9){
						if(PuckFight == 0){
							if( (Rand & 0x0f) + Rand + Player2askill >= Player1askill) {
								Player1aPuck =0;
								Player2aPuck =1;     
								Player2aCont= 1; 
								Goalie2Cont=0;  								
							}
							PuckFight = 1;
						}
					} else if(Player2aPuck == 1 && Player1ax +7 < Player2ax && Player1ax +15 > Player2ax && Player1ay +5 < Player2ay + 9 && Player1ay +10 > Player2ay + 9){
						if(PuckFight == 0){
							if( (Rand & 0x0f) + Rand + Player1askill >= Player2askill) {
								Player2aPuck =0;
								Player1aPuck =1;
								Player1aCont = 1; 
								Goalie1Cont=0;  
							}
							PuckFight = 1;
						}
					} else {
						PuckFight = 0;
					}   
					
					if (Player1aPuck == 1){
						Puckx = Player1ax + 15;
						Pucky = Player1ay + 10;
					} else{
						Puckx = Player2ax + 15;
						Pucky = Player2ay + 10;
					}	
				} else if ((Puckx > Player1ax & Puckx < Player1ax + 15 & Pucky > Player1ay +5 & Pucky< Player1ay + 10) || (Puckx > Player1ax & Puckx < Player1ax + 15 & Pucky < screentop + 10 & Player1ay < screentop + 2 )){
					Player1aPuck = 1; 
					Player1aCont = 1; 
					Goalie1Cont= 0; 
					video_pt(Puckx, Pucky, 0);
				} else if ((Puckx > Player2ax & Puckx < Player2ax + 15 & Pucky > Player2ay +5  & Pucky  < Player2ay + 10) || (Puckx > Player2ax & Puckx < Player2ax + 15 & Pucky < screentop + 10 & Player2ay < screentop + 2 )){
					Player2aPuck = 1;  
					Player2aCont= 1; 
					Goalie2Cont=0;  
					video_pt(Puckx, Pucky, 0);
				} else {     
					if(Puckx !=  screenleft -1  & Puckx !=  screenwidth + 1  & Pucky != screentop -1  & Pucky != screenheight +1 )
						video_pt(Puckx, Pucky, 0);
			   		if(Pucky >= screenheight -1) {
						Pucky =  screenheight -2;   
						PuckVy = PuckVy >>2; 
						PuckVy = -PuckVy;
						Puckydir = -Puckydir;
					}                    
			
					if(Pucky <= screentop +1)  {
						Pucky = screentop + 2; 
						PuckVy = PuckVy >>2;  
						PuckVy = -PuckVy; 
						Puckydir = -Puckydir;			
					}                   
				
					if(Puckx <= screenleft + 1) {
						if(checkGoal1() == 1){
							Player2Score += 1;  
							goalscore = 0;
							gamestate = 5;  
							Puckx = screenleft;
						} else{
							Puckx = screenleft +2;
							PuckVx = PuckVx >> 2;   
						 	PuckVx = -PuckVx;
						 	Puckxdir = -Puckxdir;              
						}
					}                    
			
					if(CheckGoalie1()==1){ 
							if(PuckVx < 0){  
								Puckx = Puckx +2;
								PuckVx = PuckVx >> 2;   
							 	PuckVx = -PuckVx;
							 	Puckxdir = -Puckxdir;  
							}
						}
						
					if(CheckGoalie2() == 1){
						if(PuckVx > 0){
							PuckVx = PuckVx >> 2;   
							Puckx = Puckx - 2;  
							PuckVx = -PuckVx;
							Puckxdir = -Puckxdir;
						}
					}
					if(Puckx >= screenwidth -1) {    
						if(checkGoal2() == 1){
							Player1Score += 1;
							goalscore = 0;
							gamestate = 5; 
							Puckx = screenwidth;
						} else {
							PuckVx = PuckVx >> 2;   
							Puckx = screenwidth -2;  
							PuckVx = -PuckVx;
							Puckxdir = -Puckxdir;
						}
					}     
					
					Puckxdir += PuckVx;
					Puckydir += PuckVy;
					PuckSlow ++;
					if(PuckSlow >= 100){
						PuckVx = PuckVx >> 1;
						PuckVy = PuckVy >> 1;
						PuckSlow = 0;
					}
					
					while (Puckxdir >= 2000){
						Puckx = Puckx + 1;
						Puckxdir -= 2000;
					}
					
					while (Puckxdir <= -2000){
						Puckx = Puckx - 1;
						Puckxdir += 2000;
					}
					
					while (Puckydir <= -2000){
						Pucky = Pucky - 1;
						Puckydir += 2000;
					}
					
					while (Puckydir >= 2000){
						Pucky = Pucky + 1;
						Puckydir -= 2000;
					}
					video_pt(Puckx, Pucky, 1);
				}

				//sprintf(ts, "%03i", buttons2); 
				
				sprintf(ts, "%02i", Player1Score); 
		   	   	video_putsmalls(16,0,ts);
			         
		        sprintf(ts, "%02i", Player2Score); 
		   	   	video_putsmalls(116, 0,ts);
   	   	   	
				if(running) {
					sticks++;
			    	if(sticks>MinCount -1) {
			    		secs++;
		    			sticks = 0;
						if(secs> 59) {
							mins++;
							secs = 0;
						}
			    	}
			
					if(secs == 0) {
						dmins = 20 - mins;
						dsecs = 0;    
						if (mins == 20){	
							if (period >= 3 && Player1Score != Player2Score){
								gamestate = 9;

							}
							else
								gamestate = 8;
								period ++;
							} 
					} else {
						dmins = 19 - mins;
						dsecs = 60 - secs;
					}
				
					sprintf(ts, "%02d", dmins);
					video_putsmalls(60,0,ts);
			
					video_smallchar(68,0,10);
					sprintf(ts, "%02d",dsecs);
					video_putsmalls(72,0,ts);
				}
				if((buttons1 & BUTTON_START) || (buttons2 & BUTTON_START)){
					if(pause < 20)
						pause ++;
					else {
						gamestate = 4;
						pause = 0; 
					}    
				}

				break;
			
			case ST_PEREND:  //period end, between periods
   	  
				betper++;
				
				if(betper == 2) {
					DrawPlayer1(Player1ax, Player1ay,0,Player1aPuck);
					DrawPlayer2(Player2ax, Player2ay,0,Player2aPuck);
					DrawGoalie1(8,Goalie1, 0);
					DrawGoalie2(119,Goalie2,0);
				 	video_pt(Puckx, Pucky, 0);    
				}
				
				if(betper==3)
					sprintf(ts, "END OF PERIOD ");
					video_putsmalls(36, 46, ts);
			
	   	  		if((buttons1 & BUTTON_START) || (buttons2 & BUTTON_START)){
				
	   	  			video_putsmalls(36, 46, periodEndOff);
					gamestate = 6;   
	   	  			faceoff = 0;
	   	  			mins = 0;
	   	  			dmins = 20;
	   	  			secs = 0;   
	   	  			betper = 0;
	   	  			
	   	  			if(period == 3 ){
	   	  				songstate = SONG_GARY;
	   	  				tnote = 0;
	   	  				inote = 0;
	   	  			}
	   	  		}
   	  
				break;
			
			case ST_ENDGAME: 
			
				if(songstate != SONG_DAVY & playsong == 0){
					songstate = SONG_ALMA;
					tnote = 0;
					inote = 0;
					playsong = 1;
				}
				if(endgamescreen < 30){
   	  
					endgamescreen ++;
				}
				
	   	        
   		        if (endgamescreen == 2){
					DrawPlayer1(Player1ax, Player1ay,0,Player1aPuck);
					DrawPlayer2(Player2ax, Player2ay,0,Player2aPuck);
					DrawGoalie1(8,Goalie1, 0);
					DrawGoalie2(119,Goalie2,0);
				 	video_pt(Puckx, Pucky, 0);    
				}
				if(endgamescreen ==3){
					

					if((Team1 == 1 & Player1Score > Player2Score) ||(Team2 == 1 & Player2Score > Player1Score) )
						video_putsmalls(40,40, CU);
					else if ((Team1 == 2 & Player1Score > Player2Score) ||(Team2 == 2 & Player2Score > Player1Score) )
					    video_putsmalls(40,40, HU);
					else if ((Team1 == 3 & Player1Score > Player2Score) ||(Team2 == 3 & Player2Score > Player1Score) )
						video_putsmalls(40,40, UN);
					else if ((Team1 == 4 & Player1Score > Player2Score) ||(Team2 == 4 & Player2Score > Player1Score) )
						video_putsmalls(40,40, MN);
					
					sprintf(ts, "DEFEATED"); 
			   	   	video_putsmalls(52,40,ts);

					if((Team1 == 1 & Player1Score < Player2Score) ||(Team2 == 1 & Player2Score < Player1Score) )
						video_putsmalls(88,40, CU);
					else if ((Team1 == 2 & Player1Score < Player2Score) ||(Team2 == 2 & Player2Score < Player1Score) )
					    video_putsmalls(88,40, HU);
					else if ((Team1 == 3 & Player1Score < Player2Score) ||(Team2 == 3 & Player2Score < Player1Score) )
						video_putsmalls(88,40, UN);
					else if ((Team1 == 4 & Player1Score < Player2Score) ||(Team2 == 4 & Player2Score < Player1Score) )
						video_putsmalls(88,40, MN);

				}
				if(endgamescreen == 4){
					if(Player1Score > Player2Score){
						sprintf(ts, "%02i", Player1Score); 
			   	   		video_putsmalls(48,50,ts);

						sprintf(ts, "TO"); 
			   	   		video_putsmalls(60,50,ts);

						sprintf(ts, "%02i", Player2Score); 
		   	   			video_putsmalls(76,50,ts);    
					}
					else if(Player2Score > Player1Score){
						
						sprintf(ts, "%02i", Player2Score); 
			   	   		video_putsmalls(48,50,ts);

						sprintf(ts, "TO"); 
			   	   		video_putsmalls(60,50,ts);

						sprintf(ts, "%02i", Player1Score); 
		   	   			video_putsmalls(76, 50,ts);    
					}
				}
				
		

	   	  		if((buttons1 & BUTTON_A) || (buttons2 & BUTTON_A)){
	   	  			
					if(statecounterase == 0){
						statecounterase = 1;
					}
					
	   	  		}
	   	  		
	   	  		if(statecounterase == 1){
	   	  			video_putsmalls(40,40,TE);
	   	  			video_putsmalls(52,40 , periodEndOff);
	   	  		}
	   	  		if(statecounterase == 2){
	   	  			video_putsmalls(48, 50, TE);
	   	  			video_putsmalls(60, 50, TE);
	   	  			video_putsmalls(76, 50, TE);
	   	  			statecounterase = 0;
	   	  			gamestate = 0;   
	  				endgamescreen = 0;
	   	  			mins = 0;
	   	  			secs = 0;
	   	  			period = 1;
	   	  			playsong = 0;
	   	  		}
	   	  		
	   	  		if(statecounterase > 0){
	   	  			statecounterase ++;
	   	  		}
	   	  		
   		  	break;
		}
        
        
        switch(songstate){
        	case SONG_DAVY:
   	   		if(gamestate != 4){
		   			//make the note scale 
			       //tnote is the duration of each note in 1/60 second units
			       if (tnote++ == song0[1][inote])
			       begin 
			       tnote = 0;
			       //update tone 
			       //inote is index into notes
			            if (inote++==72){
			            	inote=0;	
			            	if(gamestate != ST_ENDGAME)
			            		songstate = SONG_CHEERS;
							else
								songstate = SONG_ALMA;
			            }
			            	
			            note = song0[0][inote];
			            halfnote = note>>1;
			        end
					}
					else {
						note = 3;
					}
			break;
			
			case SONG_ALMA:
   	   			if(gamestate != 4){
		   			//make the note scale 
			       //tnote is the duration of each note in 1/60 second units
			       if (tnote++ == song1[1][inote])
			       begin 
			       tnote = 0;
			       //update tone 
			       //inote is index into notes
			            if (inote++==61){
			            	inote=0;
			            	songstate = SONG_CHEERS;
			            }
			            	
			            note = song1[0][inote];
			            halfnote = note>>1;
			        end
				}
				else {
					note = 3;
				}
			break;
			
			case SONG_GARY:
   	   			if(gamestate != 4){
		   			//make the note scale 
			       //tnote is the duration of each note in 1/60 second units
			       if (tnote++ == song6[1][inote])
			       begin 
			       tnote = 0;
			       //update tone 
			       //inote is index into notes
			            if (inote++==81){
			            	inote=0;
			            	songstate = SONG_CHEERS;
			            }
			            note = song6[0][inote];
			            halfnote = note>>1;
			        end
				}
				else {
					note = 3;
				}
			break;
			
			case SONG_CHEERS:
			
				switch(cheerstate){
				
					case SONG_7:
   	   					if(gamestate != 4){
				   			//make the note scale 
					       //tnote is the duration of each note in 1/60 second units
					       if (tnote++ == song2[1][inote])
					       begin 
					       tnote = 0;
					       //update tone 
					       //inote is index into notes
					            if (inote++==34) {
					            	inote=0;
					            	cheerstate = SONG_HEYBABY;
					            }
					            note = song2[0][inote];
					            halfnote = note>>1;
					        end
						}
						else {
							note = 3;
						}
					break;
				
					case SONG_HEYBABY:
   	   					if(gamestate != 4){
				   			//make the note scale 
					       //tnote is the duration of each note in 1/60 second units
					       if (tnote++ == song3[1][inote])
					       begin 
					       tnote = 0;
					       //update tone 
					       //inote is index into notes
					            if (inote++==27){
					            	inote=0;
					            	cheerstate = SONG_MILLER;
					            }
					            note = song3[0][inote];
					            halfnote = note>>1;
					        end
						}
						else {
							note = 3;
						}
					break;
					
					case SONG_MILLER:
   	   					if(gamestate != 4){
				   			//make the note scale 
					       //tnote is the duration of each note in 1/60 second units
					       if (tnote++ == song4[1][inote])
					       begin 
					       tnote = 0;
					       //update tone 
					       //inote is index into notes
					            if (inote++==40){
					            	inote=0;
					            	cheerstate = SONG_MACHO;
					            }
					            note = song4[0][inote];
					            halfnote = note>>1;
					        end
						}
						else {
							note = 3;
						}
					break;
					
					case SONG_MACHO:
   	   					if(gamestate != 4){
				   			//make the note scale 
					       //tnote is the duration of each note in 1/60 second units
					       if (tnote++ == song5[1][inote])
					       begin 
					       tnote = 0;
					       //update tone 
					       //inote is index into notes
					            if (inote++==43){
					            	inote=0;
					            	cheerstate = SONG_7;
					            }
					            note = song5[0][inote];
					            halfnote = note>>1;
					        end
						}
						else {
							note = 3;
						}
					break;
				
				}//end inner Switch cheerstate
			
			
			break;
			
		}
		if(!frame)
			frame = 1;
		else
			frame = 0;
        
        
     end  //line 231
  end  //while
end  //main

