ECE 476 Spring 2004
Daniel Chituc and Aaron Zahrowski

Source

//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  
//B.3 is sound  and should have a 10k resistor to gnd

#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

//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;

//game structure variables
char interleave;
char interleave2 = 0;
unsigned char randomcounter;
char playerwins = 0;
char wincount = 0;
unsigned char aiset, aibutton, aimove;

int time;

//scores
char score1, score2;

//paddle positions and ADC vars
char p1pos=30;
char p2pos=30; 
char level=30; 
unsigned char count, gameStart;
unsigned char adcout1, adcout2;
unsigned char adcout4 = 89;
unsigned char adcout4old = 89;
unsigned char adcout3 = 4;
unsigned char adcout3old = 4;

//animation
char x, y, s, vx, vy;

char screen[1600], t, ts[10]; 
char cu1[]="DEDICATED";
char cu12[]="PONG";
char cu13[]="UNIT";
char cu14[]="2"; 
char p1[]="P1";
char p2[]="P2"; 
char ai[]="AI";
char wins[]="WINS";
char scores[]="SCORE";
char get[]="GET";
char ready[]="READY";
char count3[]="3";
char count2[]="2";
char count1[]="1";  

//Musical note values
//C below middle C to  C above middle C
//zeros are rests
flash char notes[] = {239,213,189,179,159,142,126,
		120,106,94,90,80,71,63,60,0,0,0,0};  
char note, musicT;
                 			
//Point plot lookup table   
//One bit masks
flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

//define some character bitmaps
//5x7 characters
flash char bitmap[38][7]={ 
	//0
	0b01110000,
	0b10001000,
	0b10011000,
	0b10101000,
	0b11001000,
	0b10001000,
	0b01110000,
	//1
	0b00100000,
	0b01100000,
	0b00100000,
	0b00100000,
	0b00100000,
	0b00100000,
	0b01110000,  
	//2
	0b01110000,
	0b10001000,
	0b00001000,
	0b00010000,
	0b00100000,
	0b01000000,
	0b11111000,
    //3
	0b11111000,
	0b00010000,
	0b00100000,
	0b00010000,
	0b00001000,
	0b10001000,
	0b01110000,
	//4
	0b00010000,
	0b00110000,
	0b01010000,
	0b10010000,
	0b11111000,
	0b00010000,
	0b00010000,
	//5
	0b11111000,
	0b10000000,
	0b11110000,
	0b00001000,
	0b00001000,
	0b10001000,
	0b01110000,
	//6
	0b01000000,
	0b10000000,
	0b10000000,
	0b11110000,
	0b10001000,
	0b10001000,
	0b01110000,
	//7
	0b11111000,
	0b00001000,
	0b00010000,
	0b00100000,
	0b01000000,
	0b10000000,
	0b10000000,
	//8
	0b01110000,
	0b10001000,
	0b10001000,
	0b01110000,
	0b10001000,
	0b10001000,
	0b01110000,
	//9
	0b01110000,
	0b10001000,
	0b10001000,
	0b01111000,
	0b00001000,
	0b00001000,
	0b00010000,  
	//A
	0b01110000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b11111000,
	0b10001000,
	0b10001000,
	//B
	0b11110000,
	0b10001000,
	0b10001000,
	0b11110000,
	0b10001000,
	0b10001000,
	0b11110000,
	//C
	0b01110000,
	0b10001000,
	0b10000000,
	0b10000000,
	0b10000000,
	0b10001000,
	0b01110000,
	//D
	0b11110000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b11110000,
	//E
	0b11111000,
	0b10000000,
	0b10000000,
	0b11111000,
	0b10000000,
	0b10000000,
	0b11111000,
	//F
	0b11111000,
	0b10000000,
	0b10000000,
	0b11111000,
	0b10000000,
	0b10000000,
	0b10000000,
	//G
	0b01110000,
	0b10001000,
	0b10000000,
	0b10011000,
	0b10001000,
	0b10001000,
	0b01110000,
	//H
	0b10001000,
	0b10001000,
	0b10001000,
	0b11111000,
	0b10001000,
	0b10001000,
	0b10001000,
	//I
	0b01110000,
	0b00100000,
	0b00100000,
	0b00100000,
	0b00100000,
	0b00100000,
	0b01110000,
	//J
	0b00111000,
	0b00010000,
	0b00010000,
	0b00010000,
	0b00010000,
	0b10010000,
	0b01100000,
	//K
	0b10001000,
	0b10010000,
	0b10100000,
	0b11000000,
	0b10100000,
	0b10010000,
	0b10001000,
	//L
	0b10000000,
	0b10000000,
	0b10000000,
	0b10000000,
	0b10000000,
	0b10000000,
	0b11111000,
	//M
	0b10001000,
	0b11011000,
	0b10101000,
	0b10101000,
	0b10001000,
	0b10001000,
	0b10001000,
	//N
	0b10001000,
	0b10001000,
	0b11001000,
	0b10101000,
	0b10011000,
	0b10001000,
	0b10001000,
	//O
	0b01110000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b01110000,
	//P
	0b11110000,
	0b10001000,
	0b10001000,
	0b11110000,
	0b10000000,
	0b10000000,
	0b10000000,
	//Q
	0b01110000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b10101000,
	0b10010000,
	0b01101000,
	//R
	0b11110000,
	0b10001000,
	0b10001000,
	0b11110000,
	0b10100000,
	0b10010000,
	0b10001000,
	//S
	0b01111000,
	0b10000000,
	0b10000000,
	0b01110000,
	0b00001000,
	0b00001000,
	0b11110000,
	//T
	0b11111000,
	0b00100000,
	0b00100000,
	0b00100000,
	0b00100000,
	0b00100000,
	0b00100000,
	//U
	0b10001000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b01110000,
	//V
	0b10001000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b10001000,
	0b01010000,
	0b00100000,
	//W
	0b10001000,
	0b10001000,
	0b10001000,
	0b10101000,
	0b10101000,
	0b10101000,
	0b01010000,
	//X
	0b10001000,
	0b10001000,
	0b01010000,
	0b00100000,
	0b01010000,
	0b10001000,
	0b10001000,
	//Y
	0b10001000,
	0b10001000,
	0b10001000,
	0b01010000,
	0b00100000,
	0b00100000,
	0b00100000,
	//Z
	0b11111000,
	0b00001000,
	0b00010000,
	0b00100000,
	0b01000000,
	0b10000000,
	0b11111000,
	//figure1
	0b01110000,
	0b00100000,
	0b01110000,
	0b10101000,
	0b00100000,
	0b01010000,
	0b10001000,
	//figure2
	0b01110000,
	0b10101000,
	0b01110000,
	0b00100000,
	0b00100000,
	0b01010000,
	0b10001000};


//================================ 
//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,
	//=
	0b00000000,
	0b11101110,
	0b00000000,
	0b11101110,
	0b00000000,
	//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;     
  //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;   
  
  if (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(4);  //adjust to center image on screen
       randomcounter++;
       
       //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         
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 big character on the screen
// c is index into bitmap
void video_putchar(char x, char y, char c)  
begin 
    v7 = x;
    for (v6=0;v6<7;v6++) 
    begin
        v1 = bitmap[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);
        video_pt(v7+4, v8, (v1 & 0x08)==0x08);
    end
end

//==================================
// put a string of big characters on the screen
void video_puts(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_putchar(x,y,str[i]-0x30);
		else video_putchar(x,y,str[i]-0x40+9);
		x = x+6;	
	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 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

//==================================
// 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 = 0b11111111;
  DDRD = 0xff;		//video out and switches
  DDRC = 0xff;		//LED indicators
  //D.5 is sync:1000 ohm + diode to 75 ohm resistor
  //D.6 is video:330 ohm + diode to 75 ohm resistor
  
  //initialize synch constants 
  LineCount = 1;
  syncON = 0b00000000;
  syncOFF = 0b00100000;  

  //Enable AD conversion
  ADMUX = 0b11100000;
  ADCSR = 0b11000101;
  DDRA = 0x0;		//signal input 
  // mean 1.30v, pkpk 2.80v
   
  video_puts(6,3,cu1);
  video_puts(64,3,cu12);
  video_puts(96,3,cu13);
  video_putsmalls(88,3,cu14);
  video_putsmalls(4,92,p1); 
  video_putsmalls(116,92,p2); 
  //side lines 
  #define width 126
  video_line(0,0,0,99,1);
  video_line(width,0,width,99,1);
  
  //top line & bottom lines
  video_line(0,0,width,0,1);
  video_line(0,99,width,99,1);
  video_line(0,11,width,11,1);
  video_line(0,89,width,89,1); 
  
  // middle line
  video_pt(61,20,1);
  video_pt(61,22,1);
  video_pt(61,40,1);
  video_pt(61,42,1);
  video_pt(61,60,1);
  video_pt(61,62,1);
  video_pt(61,80,1);
  video_pt(61,82,1);
  
    
  //init software timer
  t=0;
  time=0;  
  
  //init animation
  x = 64; 
  y = 50;
  vx=1;
  vy=1;  


  //video_pt(x,y,1);   
  
  count = 255;
  
  //init musical scale
  note = 0;
  musicT = 0;
  //use OC0 (pin B.3) for music  
  DDRB.3 = 1 ;     
       
  //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  
     
        switch(interleave){
	case 0:        
  		ADMUX = 0b11100010;
  		#asm ("sleep"); 
        	adcout1 = 256-ADCH;
           	break;
	case 1:
		ADMUX = 0b11100001;
		#asm ("sleep");
		adcout2 = 256-ADCH;
  		break;
  	case 2:        
		ADMUX = 0b11100000;
		#asm ("sleep"); 
		break;
	case 3:
		ADMUX = 0b11100011;
		#asm ("sleep");
		break;
	}

    //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){ 
      
    
        switch (aibutton){
            case 0: // nopush
                if (PIND.3 == 1) aibutton = 1;
                break;
            case 1: // maybepush
      		    if (PIND.3 == 1) aibutton = 2;
      		    else aibutton = 0;  
      		    break;
            case 2: // pushed
                if (PIND.3 == 0) aibutton = 3;
                break;
            case 3: // maybenopush
                if (PIND.3 == 0) { aiset = aiset ^ 1; aibutton = 0; }
                else aibutton = 2;
                break;
        }
    
        if (gameStart == 0){
            if (count == 255 && PIND.1 == 1) count++;
          
            if (count < 60){              
                // print "READY"
                video_putsmalls(52,45,ready);	
                if (PORTA.6 == 0) count++;
            }else if (count != 255) {
                if (count < 120) video_putsmalls(60,52,count3);
                else if (count < 180) video_putsmalls(60,52,count2);
                else if (count < 240) video_putsmalls(60,52,count1);
                if(count == 240){
                    video_line(52,45,80,45,0);
                    video_line(52,46,80,46,0);
                    video_line(52,47,80,47,0);
                    video_line(52,48,80,48,0);
                }
                if(count == 241){
                    video_line(52,49,80,49,0);
                    video_line(52,54,80,54,0);
                    video_line(52,55,80,55,0);
                }
                if(count == 242){
                    video_line(52,52,80,52,0); 
                    video_line(52,53,80,53,0);
                    video_line(52,56,80,56,0);
                    gameStart = 1;
                    playerwins = 1;
                    wincount = 90;
                    video_pt(x,y,2);
                }
                count++;
            }
       	   	
        } else { // gameStart == 1
 
            video_pt(x,y,2) ; //erase old point

            //playerwins == 0: game is in motion
            if(playerwins == 0){  

                //poll horizontal movement more frequently here 
                ADMUX = 0b11100000;
                #asm ("sleep"); 
                adcout3 = ADCH;
                adcout3 = adcout3 / 8 + 4;
                ADMUX = 0b11100011;
                #asm ("sleep");
                adcout4 = ADCH;
                adcout4 = adcout4 / 8 + 89;

                //erase old paddles every 4; reduce video_line calls
                switch(interleave){
                    case 0:
                        interleave = 1;
                        video_line(adcout3old,12,adcout3old,88,0);
                        video_line(adcout3old+1,12,adcout3old+1,88,0);
                        break;
                    case 1:      
                        interleave = 2;
                        video_line(adcout4old,12,adcout4old,88,0);
                        video_line(adcout4old+1,12,adcout4old+1,88,0);
                        break;
                    case 2:
                        interleave = 3; 
                        video_line(adcout3old,12,adcout3old,88,0);
                        video_line(adcout3old+1,12,adcout3old+1,88,0);
                        break;
                    case 3:
                        interleave = 0;
                        video_line(adcout4old,12,adcout4old,88,0);
                        video_line(adcout4old+1,12,adcout4old+1,88,0); 
                        break;
                    }

  	        //override ADC for player 2 if AI is set
  	        if (aiset){
  	        
  	        	// if the middle of the AI paddle is higher, move down
  	        	if ((p2pos + (level/2)) < y && aimove < 254)
				aimove = aimove + (30/level);
  	        	else if ((p2pos + (level/2)) >= y && aimove > 0)
				aimove = aimove - (30/level);
  	        	adcout2 = aimove;
  	        	adcout4 = 121; // keep AI paddle against wall
  	        }

  	      		//determine range of paddle based on level
  	      		switch(level){
  	      		case 30:
  	      			p1pos = adcout1 / 5.33 + 11;
  	      			p2pos = adcout2 / 5.33 + 11;
  		      		break;
  	      		case 25:
  	      			p1pos = adcout1 / 4.82 + 11;
  	      			p2pos = adcout2 / 4.82 + 11;
  		      		break;
  	      		case 20:
  	      			p1pos = adcout1 / 4.41 + 11;
  	      			p2pos = adcout2 / 4.41 + 11;
  		      		break;
  	      		case 15:
  	      			p1pos = adcout1 / 4.06 + 11;
  	      			p2pos = adcout2 / 4.06 + 11;
  		      		break;
  	      		case 10:
  	      			p1pos = adcout1 / 3.76 + 11;
  	      			p2pos = adcout2 / 3.76 + 11;
  		      		break;
  	      		}	
  	        
  	
  	    
  	     		// draw paddles every other frame
  	     		if(interleave == 1 || interleave == 3){
		  		video_line(adcout3,p1pos,adcout3,p1pos+level,1);
   	  			video_line(adcout3+1,p1pos,adcout3+1,p1pos+level,1);
   	  			adcout3old = adcout3;
   	  		}    
   	  		if(interleave == 0 || interleave == 2){
     	  			video_line(adcout4,p2pos,adcout4,p2pos+level,1);    
        			video_line(adcout4+1,p2pos,adcout4+1,p2pos+level,1);
        			adcout4old = adcout4;                
        		}
        
      			// update animated point			
   			x = x + vx;
       			y = y + vy;  			       
       			s = video_set(x,y);
       		    	DDRA.7 = 1;
				
       	  		// ensure paddles can "push" ball into center
       	  		if ( y >= p1pos && y <= p1pos+level && x <= adcout3+1){
       	  			x = adcout3+2;
       	  			// make a sound if the ball changed direction
       	  			if (vx == -1) PORTA.7 = PORTA.7 ^ 1;
       	  			vx = 1;
       	  		}
       	  		else if ( y >= p2pos && y <= p2pos+level && x >= adcout4){
       	  			x = adcout4-1;
       	  			// make a sound if the ball changed direction
       	  			if (vx == 1) PORTA.7 = PORTA.7 ^ 1;
       	  			vx = -1;
       	  		}
        	
       			// hit boundary: change direction of the animated point
       			if (y==88 || y==12) { vy = -vy;	PORTA.7 = PORTA.7 ^ 1; }
  	            
   		} // end playerwins == 0  		

  		    
      		//update the second clock
        	if (++t>59) {
        		t=0; 
           		time = time + 1;
           		sprintf(ts,"%05d",time);
           		video_putsmalls(52,92,ts);
        	}
        
		//someone scored!
        	if(playerwins == 1){

                // check for game end
                if(score1+score2 > 34){
                    playerwins = 4;

                }else{

                    // display "SCORE" for 1.5 seconds
                    if(wincount < 87){
                        wincount++;
                        video_putsmalls(52,49,scores);
                    }
                    // begin wipe-erase of "SCORE" 
                    if(wincount == 87){
                        video_line(52,50,80,50,0);
                        video_line(52,51,80,51,0);
                        wincount=88;
                    }
                    if(wincount == 88){
                        video_line(52,52,80,52,0);
                        wincount=89;
                    }
                    if(wincount == 89){
                        video_line(52,49,80,49,0); 
                        wincount=90;
                    }

                    //reset mode into game play, randomize ball position and vector
                    if(wincount == 90){ 
        	  	PORTC.6 = 0;
        	  	PORTC.7 = 0;
        	      	playerwins = 0;
        	      	wincount = 0;
        	      	video_line(52,53,80,53,0);
        	      	if(randomcounter >= 0 && randomcounter < 64){ vx = -1; vy = -1; }
        	      	if(randomcounter >= 64 && randomcounter < 128){ vx = 1; vy = -1; }
        	      	if(randomcounter >= 128 && randomcounter < 192){ vx = -1; vy = 1; }
        	      	if(randomcounter >= 192 && randomcounter < 256){ vx = 1; vy = 1; }
        	      	y = randomcounter / 4 + 18;
        	      	x = randomcounter / 25 + 59;
                    }

                } // end check score sum < 34
        	  
        	// calculate paddle size based on total score	  
        	if(score1+score2 < 10){
        		level = 30;
        	}else{
        		if(score1+score2 < 15){
        			level = 25;
        		}else{
        	  		if(score1+score2 < 20){
        	  			level = 20;
        	  		}else{
        	  			if(score1+score2 < 25){
        	  				level = 15;
        	  			}else{
        	  				level = 10;
        	  			}
				}
			}
		} // end check score sum
        	  
        	sprintf(ts,"%02d",score1);
        	video_putsmalls(16,92,ts);
        	sprintf(ts,"%02d",score2);
		video_putsmalls(104,92,ts);
        	        	    
        } // end playerwins == 1 
            	

        //light LED of the player who scored	         
        if(playerwins == 0){
		if (x < adcout3-1) { score2++; playerwins = 1; PORTC.7 = 1; }
		if (x > adcout4+2) { score1++; playerwins = 1; PORTC.6 = 1; }
        }
        
        video_pt(x,y,2) ;    	 
                	 
        // PLAYER X WINS!
        if (playerwins == 4){
        	if(count > 10){
			if (score1 > score2) video_puts(40,49,p1);
       	   		else if (aiset) video_puts(40,49,ai);
       	   		else video_puts(40,49,p2);
       	   		video_puts(56,49,wins);
       	   		video_line(81,49,81,53,1);
       	   		video_pt(81,55,1);
                }
  	  	if(count > 50 && PIND.1 == 1) {
  	  		count = 0;
  	  	}
  	  	if(count == 0){  
  			video_line(30,50,88,50,0);	
  		  	video_line(30,49,88,49,0);
  			count++;
  		}
  		else if (count == 1){ 
  			video_line(30,51,88,51,0);
  			video_line(30,52,88,52,0);
  			video_line(30,53,88,53,0);
  			count++;
  		}
  		else if (count == 2){
			video_line(30,54,88,54,0);
  			video_line(30,55,88,55,0);
  			video_line(30,56,88,56,0);
						
  			// reset game variables and start over
  			video_pt(x,y,2);
  			score1 = 0;
  			score2 = 0;
  			playerwins = 0;
  		 	gameStart = 0;
  		 	count = 0;
                }
            } // playerwins == 4			      
           

        
        } // check gameStart 
     
        // print "AI" if computer is playing, otherwise "P2"
        if (aiset) video_putsmalls(116,92,ai);
        else video_putsmalls(116,92,p2); 
                   
    } //line 231
  end //while
end //main