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

// Used to convert degrees into radians

#define R2A .01745328

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

 

// Hole number

int hole

// If this is the first cycle in which we're aiming

int first_aim;

// Angle of putter

int angle;

// Location of ball

unsigned int x, y;

// Location of hole's hole

char hole_x, hole_y;

// Change in location of ball during hit

int dx, dy;

// IF we're done with the game

int done, timer_count;

// Speed of ball

int speed, speed_init;

// If we're waiting for button press to start game

int starting;

// If we're in the aiming state

int aiming;

// If we're in running stage, where ball is moving

int running;

// IF we're in hitting stage

int hitting;

// If we're hitting the ball or waiting for button press

int hit_state;

// Number of hits on this hole

int hits;

// Number of hits so far on course

int total_hits;

// If we're in hitting stage

// Cycles pausing between holes

int pause;

char game[] = "GAME";

char over[] = "OVER";

char mini[] = "MINIATURE";

char golf[] = "GOLF";

char hole_num[10][2] = {"1","2","3","4","5","6","7","8","9","T"};

char screen[1600], ts[10];

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

 

flash char pic_of_hole[1][7] = {

0b00111000,

0b01000100,

0b10000010,

0b10000010,

0b10000010,

0b01000100,

0b00111000 };

 

 

//================================

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

 

timer_count++;

//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<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(4); //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

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

 

// Create a hole at the specified location

void video_hole(char x, char y) {

i=((int)x>>3) + ((int)y<<4) ;

screen[i] = pic_of_hole[0][0];

screen[i+16] = pic_of_hole[0][1];

screen[i+32] = pic_of_hole[0][2];

screen[i+48] = pic_of_hole[0][3];

screen[i+64] = pic_of_hole[0][4];

screen[i+80] = pic_of_hole[0][5];

screen[i+96] = pic_of_hole[0][6];

}

// Create a filled-in hole at the specified location

void fill_hole(char x, char y) {

i=((int)x>>3) + ((int)y<<4) ;

screen[i] = 0b00111000;

screen[i+16] = 0b01111100;

screen[i+32] = 0b11111110;

screen[i+48] = 0b11111110;

screen[i+64] = 0b11111110;

screen[i+80] = 0b01111100;

screen[i+96] = 0b00111000;

}

// Erase a hole at the specified location

void blank_hole(char x, char y) {

i=((int)x>>3) + ((int)y<<4) ;

screen[i] = 0b00000000;

screen[i+16] = 0b00000000;

screen[i+32] = 0b00000000;

screen[i+48] = 0b00000000;

screen[i+64] = 0b00000000;

screen[i+80] = 0b00000000;

screen[i+96] = 0b00000000;

}

//==================================

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

// Check the buttons for use during aiming

void check_buttons(void) {

// For button 7, turn counter-clockwise one degree

if (PINA.7 != 1) {

angle = (angle+1)%360;

dx = (int)(256.0 * cos(R2A*(double)angle));

dy = (int)(256.0 * sin(R2A*(double)angle));

}

// For button 6, turn clockwise one degree

else if (PINA.6 != 1) {

angle = (angle-1)%360;

dx = (int)(256.0 * cos(R2A*(double)angle));

dy = (int)(256.0 * sin(R2A*(double)angle));

}

// For button 5, set angle and go to hitting stage

else if (PINA.5 != 1) {

video_line((char)(x>>8),(char)(y>>8),(char)((x+8*dx)>>8),(char)((y+8*dy)>>8),2);

// Run the hit

aiming = 0;

hitting = 1;

hit_state = WAIT;

}

}

// Check if ball is in hole

void in_hole(void) {

int hole_dx, hole_dy;

// Get x and y distances from hole

hole_dx = (x>>8) - (int)hole_x;

hole_dy = (y>>8) - (int)hole_y;

// If in hole, we're done

if ((hole_dx*hole_dx + hole_dy*hole_dy) < 9) {// && (speed > 10)) {

// Show hole filled

fill_hole(hole_x-3,hole_y-3);

// Stop running hit

running = 0;

// Go to intermission stage, pause before next hole

pause = 10;

// Update hit total

total_hits += hits;

}

}

 

// Walls for hole 1

void hole1_walls(void) {

char temp_x, temp_y, new_temp_x, new_temp_y;

int new_x, new_y;

double mult;

// Get current location and next location

temp_x = x >> 8;

temp_y = y >> 8;

mult = ((double)speed) / 250.0;

new_x += x + (int)(mult * (double)dx);

new_y += y + (int)(mult * (double)dy);

new_temp_x = new_x >> 8;

new_temp_y = new_y >> 8;

// Top wall

if (((new_temp_x >= 10) && (new_temp_x <= 110)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 10) && (new_temp_x <= 110)) && (new_temp_y >= 75) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 10) && ((new_temp_y >= 25) && (new_temp_y <= 75)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 110) && ((new_temp_y >= 25) && (new_temp_y <= 75)) && (dx > 0)) {

dx = -dx;

}

}

// Walls for hole 2

void hole2_walls(void) {

double mult;

char temp_x, temp_y, new_temp_x, new_temp_y;

int new_x, new_y;

// Get current location and next location

temp_x = x >> 8;

temp_y = y >> 8;

mult = ((double)speed) / 250.0;

new_x += x + (int)(mult * (double)dx);

new_y += y + (int)(mult * (double)dy);

new_temp_x = new_x >> 8;

new_temp_y = new_y >> 8;

// Top wall

if (((new_temp_x >= 10) && (new_temp_x <= 75)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 45) && (new_temp_x <= 75)) && (new_temp_y >= 85) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 10) && ((new_temp_y >= 25) && (new_temp_y <= 55)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 75) && ((new_temp_y >= 25) && (new_temp_y <= 85)) && (dx > 0))

dx = -dx;

}

// Corner

if ((new_temp_x <= 45) && (new_temp_y >= 55)) {

if ((temp_x >= 10) && (temp_x <= 45)) {

dy = -dy;

}

if ((temp_y >= 55) && (temp_y <= 85)) {

dx = -dx;

}

}

}

// Walls for hole 3

void hole3_walls(void) {

char temp_x, temp_y, new_temp_x, new_temp_y;

int new_x, new_y;

double mult;

// Get current location and next location

temp_x = x >> 8;

temp_y = y >> 8;

mult = ((double)speed) / 250.0;

new_x += x + (int)(mult * (double)dx);

new_y += y + (int)(mult * (double)dy);

new_temp_x = new_x >> 8;

new_temp_y = new_y >> 8;

// Top wall

if (((new_temp_x >= 10) && (new_temp_x <= 110)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 10) && (new_temp_x <= 110)) && (new_temp_y >= 75) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 10) && ((new_temp_y >= 25) && (new_temp_y <= 75)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 110) && ((new_temp_y >= 25) && (new_temp_y <= 75)) && (dx > 0)) {

dx = -dx;

}

// Check the walls in the middle of the hole

if ((temp_y <= 50) && (((new_temp_x <= 40) && (temp_x >= 40)) || ((new_temp_x >= 40) && (temp_x <= 40)))) {

dx = -dx;

}

if ((temp_y >= 50) && (((new_temp_x <= 70) && (temp_x >= 70)) || ((new_temp_x >= 70) && (temp_x <= 70)))) {

dx = -dx;

}

}

// Walls for hole 4

void hole4_walls(void) {

char temp_x, temp_y, new_temp_x, new_temp_y;

int new_x, new_y;

double mult;

int hole_dx, hole_dy;

// Get current location and next location

temp_x = x >> 8;

temp_y = y >> 8;

mult = ((double)speed) / 250.0;

new_x += x + (int)(mult * (double)dx);

new_y += y + (int)(mult * (double)dy);

new_temp_x = new_x >> 8;

new_temp_y = new_y >> 8;

// Check if in tube

hole_dx = (x>>8) - (int)60;

hole_dy = (y>>8) - (int)40;

if ((hole_dx*hole_dx + hole_dy*hole_dy) < 9) {// && (speed > 10)) {

x = 10 << 8;

y = 80 << 8;

dx = 0x7f;

dy = 0x00;

return;

}

// Upper-left tier

if ((new_temp_x <= 73) && (new_temp_y <= 60)) {

// Top wall

if (((new_temp_x >= 10) && (new_temp_x <= 70)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 10) && (new_temp_x <= 70)) && (new_temp_y >= 55) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 10) && ((new_temp_y >= 25) && (new_temp_y <= 55)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 70) && ((new_temp_y >= 25) && (new_temp_y <= 55)) && (dx > 0)) {

dx = -dx;

}

}

// Bottom-right tier

else {

if (((new_temp_x >= 80) && (new_temp_x <= 120)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 10) && (new_temp_x <= 120)) && (new_temp_y >= 95) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 10) && ((new_temp_y >= 65) && (new_temp_y <= 95)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 120) && ((new_temp_y >= 25) && (new_temp_y <= 95)) && (dx > 0)) {

dx = -dx;

}

// Corner

if ((new_temp_x <= 80) && (new_temp_y <= 65)) {

if ((temp_x >= 10) && (temp_x <= 80)) {

dy = -dy;

}

if ((temp_y >= 25) && (temp_y <= 65)) {

dx = -dx;

}

}

}

}

// Walls for hole 5

void hole5_walls(void) {

char temp_x, temp_y, new_temp_x, new_temp_y;

int new_x, new_y;

double mult;

// Get current location and next location

temp_x = x >> 8;

temp_y = y >> 8;

mult = ((double)speed) / 250.0;

new_x += x + (int)(mult * (double)dx);

new_y += y + (int)(mult * (double)dy);

new_temp_x = new_x >> 8;

new_temp_y = new_y >> 8;

// Top wall

if (((new_temp_x >= 10) && (new_temp_x <= 90)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 30) && (new_temp_x <= 110)) && (new_temp_y >= 85) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 10) && ((new_temp_y >= 25) && (new_temp_y <= 45)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 110) && ((new_temp_y >= 65) && (new_temp_y <= 85)) && (dx > 0)) {

dx = -dx;

}

// Check the corners

if ((new_temp_x <= 30) && (new_temp_y >= 45)) {

if ((temp_x >= 10) && (temp_x <= 30)) {

dy = -dy;

}

if ((temp_y >= 45) && (temp_y <= 85)) {

dx = -dx;

}

}

if ((new_temp_x >= 90) && (new_temp_y <= 65)) {

if ((temp_x >= 90) && (temp_x <= 110)) {

dy = -dy;

}

if ((temp_y >= 25) && (temp_y <= 65)) {

dx = -dx;

}

}

// Center block

if ((new_temp_x >= 50) && (new_temp_x <= 70) && (new_temp_y >= 45) && (new_temp_y <= 65)) {

if ((temp_x >= 50) && (temp_x <= 70)) {

dy = -dy;

}

if ((temp_y >= 45) && (temp_y <= 65)) {

dx = -dx;

}

}

}

// Walls for hole 6

void hole6_walls(void) {

char temp_x, temp_y, new_temp_x, new_temp_y;

int new_x, new_y;

double mult;

// Get current location and next location

temp_x = x >> 8;

temp_y = y >> 8;

mult = ((double)speed) / 250.0;

new_x += x + (int)(mult * (double)dx);

new_y += y + (int)(mult * (double)dy);

new_temp_x = new_x >> 8;

new_temp_y = new_y >> 8;

// Top wall

if (((new_temp_x >= 30) && (new_temp_x <= 90)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 30) && (new_temp_x <= 90)) && (new_temp_y >= 85) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 10) && ((temp_y >= 45) && (new_temp_y <= 65)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 110) && ((temp_y >= 45) && (new_temp_y <= 65)) && (dx > 0)) {

dx = -dx;

}

// Check the corners in the hole

if ((new_temp_x <= 30) && (new_temp_y <= 45)) {

if ((temp_x >= 10) && (temp_x <= 30)) {

dy = -dy;

}

if ((temp_y >= 25) && (temp_y <= 45)) {

dx = -dx;

}

}

if ((new_temp_x >= 90) && (new_temp_y <= 45)) {

if ((temp_x >= 90) && (temp_x <= 110)) {

dy = -dy;

}

if ((temp_y >= 25) && (temp_y <= 45)) {

dx = -dx;

}

}

if ((new_temp_x <= 30) && (new_temp_y >= 65)) {

if ((temp_x >= 10) && (temp_x <= 30)) {

dy = -dy;

}

if ((temp_y >= 65) && (temp_y <= 85)) {

dx = -dx;

}

}

if ((new_temp_x >= 90) && (new_temp_y >= 65)) {

if ((temp_x >= 90) && (temp_x <= 110)) {

dy = -dy;

}

if ((temp_y >= 65) && (temp_y <= 85)) {

dx = -dx;

}

}

 

// Check the blocks in the hole

if ((new_temp_x >= 35) && (new_temp_x <= 45) && (new_temp_y >= 30) && (new_temp_y <= 40)) {

if ((temp_x >= 35) && (temp_x <= 45)) {

dy = -dy;

}

if ((temp_y >= 30) && (temp_y <= 40)) {

dx = -dx;

}

}

if ((new_temp_x >= 55) && (new_temp_x <= 65) && (new_temp_y >= 30) && (new_temp_y <= 40)) {

if ((temp_x >= 55) && (temp_x <= 65)) {

dy = -dy;

}

if ((temp_y >= 30) && (temp_y <= 40)) {

dx = -dx;

}

}

if ((new_temp_x >= 75) && (new_temp_x <= 85) && (new_temp_y >= 30) && (new_temp_y <= 40)) {

if ((temp_x >= 75) && (temp_x <= 85)) {

dy = -dy;

}

if ((temp_y >= 30) && (temp_y <= 40)) {

dx = -dx;

}

}

if ((new_temp_x >= 35) && (new_temp_x <= 45) && (new_temp_y >= 50) && (new_temp_y <= 60)) {

if ((temp_x >= 35) && (temp_x <= 45)) {

dy = -dy;

}

if ((temp_y >= 50) && (temp_y <= 60)) {

dx = -dx;

}

}

if ((new_temp_x >= 75) && (new_temp_x <= 85) && (new_temp_y >= 50) && (new_temp_y <= 60)) {

if ((temp_x >= 75) && (temp_x <= 85)) {

dy = -dy;

}

if ((temp_y >= 50) && (temp_y <= 60)) {

dx = -dx;

}

}

if ((new_temp_x >= 35) && (new_temp_x <= 45) && (new_temp_y >= 70) && (new_temp_y <= 80)) {

if ((temp_x >= 35) && (temp_x <= 45)) {

dy = -dy;

}

if ((temp_y >= 70) && (temp_y <= 80)) {

dx = -dx;

}

}

if ((new_temp_x >= 55) && (new_temp_x <= 65) && (new_temp_y >= 70) && (new_temp_y <= 80)) {

if ((temp_x >= 55) && (temp_x <= 65)) {

dy = -dy;

}

if ((temp_y >= 70) && (temp_y <= 80)) {

dx = -dx;

}

}

if ((new_temp_x >= 75) && (new_temp_x <= 85) && (new_temp_y >= 70) && (new_temp_y <= 80)) {

if ((temp_x >= 75) && (temp_x <= 85)) {

dy = -dy;

}

if ((temp_y >= 70) && (temp_y <= 80)) {

dx = -dx;

}

}

}

// Walls for hole 7

void hole7_walls(void) {

char temp_x, temp_y, new_temp_x, new_temp_y;

int new_x, new_y;

double mult;

// Get current location and next location

temp_x = x >> 8;

temp_y = y >> 8;

mult = ((double)speed) / 250.0;

new_x += x + (int)(mult * (double)dx);

new_y += y + (int)(mult * (double)dy);

new_temp_x = new_x >> 8;

new_temp_y = new_y >> 8;

// Top wall

if (((new_temp_x >= 10) && (new_temp_x <= 90)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 10) && (new_temp_x <= 90)) && (new_temp_y >= 85) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 10) && ((new_temp_y >= 25) && (new_temp_y <= 45)) && (dx < 0)) {

dx = -dx;

}

// Left wall

if ((new_temp_x <= 10) && ((new_temp_y >= 50) && (new_temp_y <= 85)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 90) && ((new_temp_y >= 25) && (new_temp_y <= 85)) && (dx > 0)) {

dx = -dx;

}

// Center block

if ((new_temp_x >= 30) && (new_temp_x <= 70) && (new_temp_y >= 45) && (new_temp_y <= 65)) {

if ((temp_x >= 30) && (temp_x <= 70)) {

dy = -dy;

}

if ((temp_y >= 45) && (temp_y <= 65)) {

dx = -dx;

}

}

// Left block

if ((new_temp_x >= 10) && (new_temp_x <= 30) && (new_temp_y >= 45) && (new_temp_y <= 50)) {

if ((temp_x >= 10) && (temp_x <= 30)) {

dy = -dy;

}

if ((temp_y >= 45) && (temp_y <= 50)) {

dx = -dx;

}

}

}

// Walls for hole 8

void hole8_walls(void) {

char temp_x, temp_y, new_temp_x, new_temp_y;

int new_x, new_y;

double mult;

int hole_dx, hole_dy;

// Get current location and next location

temp_x = x >> 8;

temp_y = y >> 8;

mult = ((double)speed) / 250.0;

new_x += x + (int)(mult * (double)dx);

new_y += y + (int)(mult * (double)dy);

new_temp_x = new_x >> 8;

new_temp_y = new_y >> 8;

// Check if in a tube

hole_dx = (x>>8) - (int)60;

hole_dy = (y>>8) - (int)35;

if ((hole_dx*hole_dx + hole_dy*hole_dy) < 9) {// && (speed > 10)) {

x = 100 << 8;

y = 85 << 8;

dx = 0x00;

dy = 0xff;

return;

}

if ((hole_dx*hole_dx + hole_dy*hole_dy) < 9) {// && (speed > 10)) {

x = 90 << 8;

y = 45 << 8;

dx = 0x7f;

dy = 0x00;

return;

}

if ((hole_dx*hole_dx + hole_dy*hole_dy) < 9) {// && (speed > 10)) {

x = 90 << 8;

y = 65 << 8;

dx = 0x7f;

dy = 0x00;

return;

}

// Left tier

if (temp_x <= 80) {

// Top wall

if (((new_temp_x >= 50) && (new_temp_x <= 70)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 50) && (new_temp_x <= 70)) && (new_temp_y >= 85) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 10) && ((temp_y >= 45) && (new_temp_y <= 65)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 70) && ((temp_y >= 25) && (new_temp_y <= 85)) && (dx > 0)) {

dx = -dx;

}

// Top left corner

if ((new_temp_x <= 50) && (new_temp_y <= 45)) {

if ((temp_x >= 10) && (temp_x <= 50)) {

dy = -dy;

}

if ((temp_y >= 25) && (temp_y <= 45)) {

dx = -dx;

}

}

// Top right corner

if ((new_temp_x <= 50) && (new_temp_y >= 65)) {

if ((temp_x >= 10) && (temp_x <= 50)) {

dy = -dy;

}

if ((temp_y >= 65) && (temp_y <= 85)) {

dx = -dx;

}

}

}

// Right tier

else {

if (((new_temp_x >= 90) && (new_temp_x <= 110)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 90) && (new_temp_x <= 110)) && (new_temp_y >= 85) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 90) && ((new_temp_y >= 25) && (new_temp_y <= 85)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 110) && ((new_temp_y >= 25) && (new_temp_y <= 85)) && (dx > 0)) {

dx = -dx;

}

}

}

// Walls for hole 9

void hole9_walls(void) {

char temp_x, temp_y, new_temp_x, new_temp_y;

int new_x, new_y;

double mult;

// Get current location and next location

temp_x = x >> 8;

temp_y = y >> 8;

mult = ((double)speed) / 250.0;

new_x += x + (int)(mult * (double)dx);

new_y += y + (int)(mult * (double)dy);

new_temp_x = new_x >> 8;

new_temp_y = new_y >> 8;

// Top wall

if (((new_temp_x >= 10) && (new_temp_x <= 110)) && (new_temp_y <= 25) && (dy < 0)) {

dy = -dy;

}

// Bottom wall

if (((new_temp_x >= 90) && (new_temp_x <= 110)) && (new_temp_y >= 85) && (dy > 1)) {

dy = -dy;

}

// Left wall

if ((new_temp_x <= 10) && ((new_temp_y >= 25) && (new_temp_y <= 45)) && (dx < 0)) {

dx = -dx;

}

// Right wall

if ((new_temp_x >= 110) && ((new_temp_y >= 25) && (new_temp_y <= 85)) && (dx > 0)) { PORTC.4 = 0;

dx = -dx;

}

if ((new_temp_x <= 90) && (new_temp_y >= 45)) {

if ((temp_x >= 10) && (temp_x <= 90)) {

dy = -dy;

}

if ((temp_y >= 45) && (temp_y <= 85)) {

dx = -dx;

}

}

}

// Draw/erase hole 1

void video_hole1(int i) {

 

// Ball and hole locations

if (i) {

// Initial position

x = 20 << 8;

y = 50 << 8;

hole_x = 100;

hole_y = 50;

video_hole(hole_x-3,hole_y-3);

}

else {

blank_hole(hole_x-3,hole_y-3);

}

// Lines for walls

video_line(10,25,110,25,i);

video_line(10,75,110,75,i);

video_line(10,25,10,75,i);

video_line(110,25,110,75,i);

}

// Draw/erase hole 2

void video_hole2(int i) {

 

// Ball and hole locations

if (i) {

x = 20 << 8;

y = 40 << 8;

hole_x = 60;

hole_y = 75;

video_hole(hole_x-3,hole_y-3);

}

else {

blank_hole(hole_x-3,hole_y-3);

}

// Lines for walls

video_line(10,25,75,25,i);

video_line(75,25,75,85,i);

video_line(45,85,75,85,i);

video_line(45,55,45,85,i);

video_line(10,55,45,55,i);

video_line(10,25,10,55,i);

}

// Draw/erase hole 3

void video_hole3(int i) {

 

// Ball and hole locations

if (i) {

x = 20 << 8;

y = 50 << 8;

hole_x = 100;

hole_y = 50;

video_hole(hole_x-3,hole_y-3);

}

else {

blank_hole(hole_x-3,hole_y-3);

}

// Lines for walls

video_line(10,25,110,25,i);

video_line(40,25,40,50,i);

video_line(70,50,70,75,i);

video_line(10,75,110,75,i);

video_line(10,25,10,75,i);

video_line(110,25,110,75,i);

}

// Draw/erase hole 4

void video_hole4(int i) {

 

// Ball and hole locations

if (i) {

x = 20 << 8;

y = 40 << 8;

hole_x = 100;

hole_y = 35;

video_hole(hole_x-3,hole_y-3);

video_hole(60-3,40-3);

}

else {

blank_hole(hole_x-3,hole_y-3);

blank_hole(60-3,40-3);

}

// Lines for walls

video_line(10,25,70,25,i);

video_line(70,25,70,55,i);

video_line(10,55,70,55,i);

video_line(10,25,10,55,i);

video_line(10,65,80,65,i);

video_line(80,65,80,25,i);

video_line(80,25,120,25,i);

video_line(120,25,120,95,i);

video_line(10,95,10,65,i);

}

// Draw/erase rest of hole 4

void video_hole4_pt2(int i) {

// Rest of lines for walls

video_line(10,95,120,95,i);

}

// Draw/erase hole 5

void video_hole5(int i) {

 

// Ball and hole locations

if (i) {

x = 20 << 8;

y = 35 << 8;

hole_x = 100;

hole_y = 75;

video_hole(hole_x-3,hole_y-3);

}

else {

blank_hole(hole_x-3,hole_y-3);

}

// Lines for walls

video_line(10,25,90,25,i);

video_line(90,25,90,65,i);

video_line(90,65,110,65,i);

video_line(110,65,110,85,i);

video_line(30,85,110,85,i);

video_line(30,85,30,45,i);

video_line(30,45,10,45,i);

video_line(10,45,10,25,i);

video_line(50,45,70,45,i);

video_line(70,45,70,65,i);

video_line(50,65,70,65,i);

video_line(50,45,50,65,i);

}

// Draw/erase hole 6

void video_hole6(int i) {

int j;

// Ball and hole locations

if (i) {

x = 20 << 8;

y = 55 << 8;

hole_x = 100;

hole_y = 55;

video_hole(hole_x-3,hole_y-3);

}

else {

blank_hole(hole_x-3,hole_y-3);

}

// Lines for walls

video_line(10,45,30,45,i);

video_line(30,45,30,25,i);

video_line(30,25,90,25,i);

video_line(90,25,90,45,i);

video_line(90,45,110,45,i);

video_line(110,45,110,65,i);

video_line(90,65,110,65,i);

video_line(90,65,90,85,i);

video_line(30,85,90,85,i);

video_line(30,65,30,85,i);

video_line(10,65,30,65,i);

video_line(10,45,10,65,i);

 

for (j = 0; j <= 1; j++) {

video_line(55,30+j*40,65,30+j*40,i);

video_line(65,30+j*40,65,40+j*40,i);

video_line(55,40+j*40,65,40+j*40,i);

video_line(55,30+j*40,55,40+j*40,i);

}

}

// Draw/erase rest of hole 6

void video_hole6_pt2(int i) {

int j,k;

 

// Res of the lines for walls

for (j = 0; j <= 1; j++) {

for (k = 0; k <= 2; k++) {

video_line(35+j*40,30+k*20,45+j*40,30+k*20,i);

video_line(45+j*40,30+k*20,45+j*40,40+k*20,i);

video_line(35+j*40,40+k*20,45+j*40,40+k*20,i);

video_line(35+j*40,30+k*20,35+j*40,40+k*20,i);

}

}

}

// Draw/erase hole 7

void video_hole7(int i) {

// Ball and hole locations

if (i) {

x = 20 << 8;

y = 35 << 8;

hole_x = 20;

hole_y = 60;

video_hole(hole_x-3,hole_y-3);

}

else {

blank_hole(hole_x-3,hole_y-3);

}

// Lines for walls

video_line(10,25,90,25,i);

video_line(90,25,90,85,i);

video_line(10,85,90,85,i);

video_line(10,50,10,85,i);

video_line(10,50,30,50,i);

video_line(30,50,30,65,i);

video_line(30,65,70,65,i);

video_line(70,65,70,45,i);

video_line(10,45,70,45,i);

video_line(10,25,10,45,i);

}

// Draw/erase hole 8

void video_hole8(int i) {

 

// Ball and hole locations

if (i) {

x = 20 << 8;

y = 55 << 8;

hole_x = 100;

hole_y = 35;

video_hole(hole_x-3,hole_y-3);

video_hole(60-3,35-3);

video_hole(60-3,55-3);

video_hole(60-3,75-3);

}

else {

blank_hole(hole_x-3,hole_y-3);

blank_hole(60-3,35-3);

blank_hole(60-3,55-3);

blank_hole(60-3,75-3);

}

// Lines for walls

video_line(10,45,50,45,i);

video_line(50,45,50,25,i);

video_line(50,25,70,25,i);

video_line(70,25,70,85,i);

video_line(50,85,70,85,i);

video_line(50,65,50,85,i);

video_line(10,65,50,65,i);

video_line(10,45,10,65,i);

video_line(90,25,110,25,i);

video_line(110,25,110,85,i);

video_line(90,85,110,85,i);

video_line(90,25,90,85,i);

}

// Draw/erase hole 9

void video_hole9(int i) {

int j,k;

// Ball and hole locations

if (i) {

x = 20 << 8;

y = 35 << 8;

hole_x = 100;

hole_y = 75;

video_hole(hole_x-3,hole_y-3);

}

else {

blank_hole(hole_x-3,hole_y-3);

}

// Lines for walls

video_line(10,25,110,25,i);

video_line(110,25,110,85,i);

video_line(90,85,110,85,i);

video_line(90,45,90,85,i);

video_line(10,45,90,45,i);

video_line(10,25,10,45,i);

 

for (j = 0; j < 6; j++) {

for (k = 1; k < 5; k++) {

video_pt(70+j*4,25+k*4,i);

}

}

for (j = 1; j < 5; j++) {

for (k = 0; k < 6; k++) {

video_pt(90+j*4,45+k*4,i);

}

}

}

// Draw and set variables to start a hole

void new_hole(void) {

// No hits initially

hits = 0;

 

// Pick which hole to draw

switch (hole) {

case 1:

video_hole1(1);

break;

case 2:

video_hole2(1);

break;

case 3:

video_hole3(1);

break;

case 4:

video_hole4(1);

break;

case 5:

video_hole5(1);

break;

case 6:

video_hole6(1);

break;

case 7:

video_hole7(1);

break;

case 8:

video_hole8(1);

break;

case 9:

video_hole9(1);

}

// Draw ball at inital location

video_pt((char)(x>>8),(char)(y>>8),2);

}

 

// When we need a second cycle to draw the hole

// Only needed for hole 4 or 6

void new_hole_pt2(void) {

if (hole == 4) {

video_hole4_pt2(1);

}

if (hole == 6) {

video_hole6_pt2(1);

}

}

// Clear a hole from the screen

void clear_hole(void) {

// Pick which hole to clear

switch (hole) {

case 1:

video_hole1(0);

break;

case 2:

video_hole2(0);

break;

case 3:

video_hole3(0);

break;

case 4:

video_hole4(0);

break;

case 5:

video_hole5(0);

break;

case 6:

video_hole6(0);

break;

case 7:

video_hole7(0);

break;

case 8:

video_hole8(0);

break;

case 9:

video_hole9(0);

}

}

// When we need a second cycle to clear a hole

// Only needed for hole 4 or 6

void clear_hole_pt2(void) {

if (hole == 4) {

video_hole4_pt2(0);

}

if (hole == 6) {

video_hole6_pt2(0);

}

}

// Update the position and speed of the ball

void update_position_and_speed(void) {

char temp_x, temp_y;

double mult;

 

// If in sand, decrease speed by 10

temp_x = x >> 8;

temp_y = y >> 8;

if ((hole == 9)

&& (((temp_x >= 70) && (temp_x <= 90) && (temp_y >= 25) && (temp_y <= 45))

|| ((temp_x >= 90) && (temp_x <= 110) && (temp_y >= 45) && (temp_y <= 65)))) {

speed = speed - 10;

if (speed < 0)

speed = 0;

}

// Otherwise, decrease by 1

else {

// Decrease speed

speed--;

}

 

// Update location of ball using speed and direction

mult = ((double)speed) / 250.0;

x += (int)(mult * (double)dx);

y += (int)(mult * (double)dy);

}

//==================================

// set up the ports and timers

void main(void) {

char top_init[3];

int top[3];

int j, count;

int bias;

int Ain, Ain2, max;

first_aim = 1;

 

//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 = 0xff; //video out

//D.5 is sync:1000 ohm + diode to 75 ohm resistor

//D.6 is video:330 ohm + diode to 75 ohm resistor

DDRB = 0x00; //switches input

 

DDRA = 0x00; // Buttons on club

PORTA = 0xff;

 

//initialize synch constants

LineCount = 1;

syncON = 0b00000000;

syncOFF = 0b00100000;

 

//enable sleep mode

MCUCR = 0b10000000;

#asm ("sei");

 

// Once we finish course, exit loop

done = 0;

 

// Stat in title mode where we wait for button press to start

running = 0;

hitting = 0;

aiming = 0;

title = 1;

starting = 1;

 

// Hole 1, no hits yet

hole = 0;

total_hits = 0;

 

// Set up ADC

ADMUX = 0b11100000;

ADCSR = 0b11000111;

 

 

//The following loop executes once/video line during lines

//1-230, then does all of the frame-end processing

while (!done) {

//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) {

// When displaying title

if (title) {

// In first cycle, draw title

if (starting) {

video_puts(32,42,mini);

video_puts(48,50,golf);

starting = 0;

}

// Otherwise, wait for button press to go to intermission stage

if (PINA != 0xff) {

pause = 10;

title = 0;

}

}

// If running ball

else if (running) {

// If out of speed

if (speed <= 0) {

// Re-aim

running = 0;

aiming = 1;

first_aim = 1;

dx = (int)(256.0 * cos(R2A*(double)angle));

dy = (int)(256.0 * sin(R2A*(double)angle));

}

// Otherwise, ball is still going

else {

// Erase old ball

video_pt((char)(x>>8),(char)(y>>8),2);

// Check if it's in hole

in_hole();

// If it's in hole, don't do anything more

if (running == 1) {

// Check for wall bounce

switch (hole) {

case 1:

hole1_walls();

break;

case 2:

hole2_walls();

break;

case 3:

hole3_walls();

break;

case 4:

hole4_walls();

break;

case 5:

hole5_walls();

break;

case 6:

hole6_walls();

break;

case 7:

hole7_walls();

break;

case 8:

hole8_walls();

break;

case 9:

hole9_walls();

}

// Update position and speed

update_position_and_speed();

// Draw new ball

video_pt((char)(x>>8),(char)(y>>8),2);

}

}

}

// If aiming ball

else if (aiming) {

// Erase old putter

if (!first_aim) {

video_line((char)(x>>8),(char)(y>>8),(char)((x+8*dx)>>8),(char)((y+8*dy)>>8),2);

}

first_aim = 0;

// Update aim

check_buttons();

// Draw new putter

video_line((char)(x>>8),(char)(y>>8),(char)((x+8*dx)>>8),(char)((y+8*dy)>>8),2);

}

// If hitting ball

else if (hitting) {

switch (hit_state) {

// Wait for user to press "GO" button

case WAIT:

if (PINA.4 != 1) {

// Goto check state

hit_state = CHECK;

// And wait 250 screens

count = 250;

ADCSR.6 = 1;

max = 0;

}

break;

case CHECK:

// If we're still checking ball

if (count > 0) {

// Flash circle in corner to let user know to hit

if (count % 2 == 0) {

fill_hole(120,10);

}

else {

blank_hole(120,10);

}

 

// Get hit measurement

Ain2 = (int)ADCH-bias;

Ain = (Ain2>0)?Ain2:-Ain2;

// Set for new measurement

ADCSR.6 = 1;

// If this is hardest, record it

if (Ain > max) {

max = Ain;

}

// Count a hit of strength 1 as 0

if (max == 1)

max = 0;

count--;

}

// Otherwise, done measuring

else {

blank_hole(120,10);

 

// Start ball off

running = 1;

hitting = 0;

 

// Initial speed of hit

speed = 30*(int)(max);

// Cap it at 1000

if (speed > 1000)

speed = 1000;

// Increment number of hits we've taken

hits++;

// Display number of hits to screen

sprintf(ts,"%i",hits);

video_putsmalls(4+hole*8,12,ts);

sprintf(ts,"%i",total_hits+hits);

video_putsmalls(84,12,ts);

video_line(12,10,92,10,1);

}

}

}

// If in intermission stage

else {

switch (pause) {

case 10:

// If we're between holes, clear the previous

if (hole)

clear_hole();

// If not, draw scoreboard

else {

video_line(10,1,97,1,1);

video_line(10,10,97,10,1);

video_line(10,19,97,19,1);

video_line(9,1,9,19,1);

video_line(17,1,17,19,1);

video_line(25,1,25,19,1);

video_line(33,1,33,19,1);

}

break;

case 9:

// If we're between holes, clear the previous

if (hole) {

clear_hole_pt2();

}

// If not, draw scoreboard

if (!hole) {

// and find bias of accelerometer

bias = (int)ADCH;

video_line(41,1,41,19,1);

video_line(49,1,49,19,1);

video_line(57,1,57,19,1);

video_line(65,1,65,19,1);

video_line(73,1,73,19,1);

video_line(81,1,81,19,1);

video_line(97,1,97,19,1);

}

break;

case 8:

// Draw hole numbers on scoreboard

if (!hole) {

for (j = 0; j <= 9; j++) {

video_putsmalls(12+j*8,3,hole_num[j]);

}

}

break;

case 7:

// Erase title screen

if (!hole) {

for (j = 0; j < 11; j++) {

blank_hole(32+5*j,42);

}

for (j = 0; j < 6; j++) {

blank_hole(48+4*j,50);

}

}

break;

case 1:

// If we're not done with all holes, increment

// and draw next hole

hole++;

if (hole != 10) {

new_hole();

}

break;

case 0:

// If hole needs more lines, make them

if (hole != 10) {

new_hole_pt2();

aiming = 1;

first_aim = 1;

angle = 0;

dx = (int)(256.0 * cos(R2A*(double)angle));

dy = (int)(256.0 * sin(R2A*(double)angle));

}

// If doen with holes, end game

else {

running = 0;

done = 1;

}

}

pause--;

}

 

} //line 231

} //while

 

// Display "GAME OVER" screen

video_puts(25,50,game);

video_puts(50,50,over);

 

 

while (1) {}

} //main