//Cantneroid.c
//Matt Cantlon and Chris Moschner

//D.5 is sync:1000 ohm + diode to 100 ohm resistor
//D.6 is video:330 ohm + diode to 100 ohm resistor
//D.7 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

//All case Definitions for state-machine
#define Start 1
#define Right 2
#define Left 3
#define Reset 4
#define upleft 5
#define downleft 6
#define upright 7
#define downright 8
#define first_screen 9
#define level_one 10
#define level_two 11
#define die_screen 12
#define gameplay 13
#define nothing 14
#define Startnothing 15
#define Leftnothing 16
#define Rightnothing 17
#define evaluate 18
#define level_three 19
#define lives 20
#define end_screen 21

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

//animation variables
char x, y, s, vx, vy, vxpill, xpill, paddle, length;
char vxtemp, vytemp,direction, action, paddlenothing;
char xprime, yprime;
char screen[1600];
char paddle_left,paddle_middle,paddle_right;
char hitcount,brickcount,bee2,beey2,bee3,beediag,beediag1,beediag2;
char loneflag, ltwoflag, lthreeflag, bee, firstcount;

//sound "chord"
char notetable [6] = {119, 107, 95, 90, 30, 10};
char notecounter;
char life, stampcount, gameover, stampcount1, youwin, clrextra;

//Used to Check if adjacent pixels were illuminated
bit lefty, righty, top, bott, up_R, dn_R, up_L, dn_L;
bit leftyT, leftyB, topL, topR, bottR, bottL,rightyT,rightyB;


//Point plot lookup table
flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

//A 5x7 Bitmap used to place large numbers on the screen
flash char numbers[10][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
0b01110000,
0b10001000,
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 4x8 Bitmap to draw the bricks
flash char bitmap[3][4]={
//Standard brick
0b11111111,
0b10000001,
0b10000001,
0b11111111,
//filled "unbreakable" brick
0b11111111,
0b11111111,
0b11111111,
0b11111111,
//Clear brick used to erase
0b00000000,
0b00000000,
0b00000000,
0b00000000};

//A 5x7 bitmap used to place large letters on the screen
flash char letters[29][7]={
//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,
//Clear
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
//W
0b10001000,
0b10001000,
0b10001000,
0b10101000,
0b10101000,
0b01010000,
0b01010000,
//X
0b00000000,
0b10001000,
0b01010000,
0b00100000,
0b01010000,
0b10001000,
0b00000000,
//Y
0b10001000,
0b10001000,
0b01010000,
0b00100000,
0b00100000,
0b00100000,
0b00100000};

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

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 letter 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 = letters[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);
video_pt(v7+5, v8, (v1 & 0x04)==0x04);
video_pt(v7+6, v8, (v1 & 0x02)==0x02);
video_pt(v7+7, v8, (v1 & 0x01)==0x01);
end
end
//====================================
//put a big number character on the screen
//c is index into bitmap
void video_putcharn(char x, char y, char c)
begin
v7 = x;
for (v6=0;v6<7;v6++)
begin
v1 = numbers[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);
video_pt(v7+5, v8, (v1 & 0x04)==0x04);
video_pt(v7+6, v8, (v1 & 0x02)==0x02);
video_pt(v7+7, v8, (v1 & 0x01)==0x01);
end
end
//=====================================
//draw an array of bricks on the screen

void video_putchar1(char x, char y, char c)
begin
v7 = x;
for (v6=0;v6<4;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);
video_pt(v7+5, v8, (v1 & 0x04)==0x04);
video_pt(v7+6, v8, (v1 & 0x02)==0x02);
video_pt(v7+7, v8, (v1 & 0x01)==0x01);
end
end

//=====================================
//Erase a brick at location x,y
void erasebrick(char x, char y, char c)
begin
v7 = x;
for (v6=0;v6<4;v6++)
begin
v1 = bitmap[c][v6];
v8 = y+v6;
video_pt(v7, v8, 0);
video_pt(v7+1, v8, 0);
video_pt(v7+2, v8, 0);
video_pt(v7+3, v8, 0);
video_pt(v7+4, v8, 0);
video_pt(v7+5, v8, 0);
video_pt(v7+6, v8, 0);
video_pt(v7+7, v8, 0);
end
end
//======================================
//Draw a rectangular based array of bricks
//Note: bricks can only be placed at x locations
//divisible by 8 and y locations divisible by 4
void drawbricks_rect(char x, char y, char l, char h,char g)
begin
char i;
char k;
for (k=0; k<h; k++)
begin
for (i=0; i<l; i++)
begin
video_putchar1((x+(i*8)), y+(k*4), g);
end
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

notecounter=6; //Dictates sound of note played
OCR2 = 10; //Start with high pitch sound
TCCR2 = 0x1F; //divide by 1024; clear-on-match
TCNT2=0; //clear timer2

//init ports
DDRD = 0xf0; //video/sound out and switches
//D.5 is sync:1000 ohm + diode to 100 ohm resistor
//D.6 is video:330 ohm + diode to 100 ohm resistor
//D.7 is sound

//Initialize controller ports (B.5 is select output to controller)
DDRB.2 = 0;
DDRB.3 = 0;
DDRB.4 = 0;
DDRB.5 = 1;
DDRB.6 = 0;

//Initialize controller to select=1 mode
PORTB.5 = 1;

//init timer 0 to 1/uSec
TCCR0 = 2;

//initialize synch constants
LineCount = 1;
syncON = 0b00000000;
syncOFF = 0b00100000;

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

//init ball animation and location
x = 64;
y=80;
vx=-1;
vy=-1;
vxpill=1;
xpill=10;
paddle = Reset;
direction = upleft;
action = first_screen;
bee=1;
beey2=13;
bee2=1;
bee3=1;
beediag=1;
beediag1=24;
beediag2=48;
firstcount = 0;
life = 9;
stampcount=0;
gameover = 0;
youwin = 0;
clrextra=1;

//paddle length
length=20;

//divide paddle into sections for ball bouncing
paddle_left=xpill+floor(length/3);
paddle_middle=paddle_left+floor(length/3);
paddle_right=paddle_middle+floor(length/3);

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

//the animated point- the ball is 4 points (a square)
//with x,y defined as the upper left most point
//erase old one

TCCR2 = 0x00; //turn off sound until prompted


//Matster state-machine which controls hand-shake between
//levels, draws all levels, draws first-screen, draws last screen,
//keeps track of lives and when you win/lose

switch (action)
begin
//draws the First Screen with Game title
case first_screen:

//Erase leftover bricks from old unfinished levels
if(stampcount1 == 0)
begin
video_putchar(28,85,22);
video_putchar(34,85,22);
video_putchar(40,85,22);
video_putchar(46,85,22);
video_putchar(52,85,22);
video_putchar(58,85,22);
stampcount1=1;
break;
end

if(stampcount == 1)
begin
video_putchar(64,85,22);
video_putchar(70,85,22);
video_putchar(76,85,22);
video_putchar(82,85,22);
end

//Write "CANTNEROID" to screen
//write "PRESS START" to screen
if (firstcount == 0)
begin
video_putchar(20,16,2);
video_putchar(28,16,0);
video_putchar(36,16,13);
video_putchar(44,16,19);

video_putchar(17,40,15);
video_putchar(25,40,17);
video_putchar(33,40,4);
firstcount++;
break;
end

if (firstcount == 1)
begin
video_putchar(52,16,13);
video_putchar(60,16,4);
video_putchar(68,16,17);

video_putchar(41,40,18);
video_putchar(49,40,18);
video_putchar(65,40,18);
firstcount++;
break;
end

if(firstcount == 2)
begin
video_putchar(76,16,14);
video_putchar(84,16,8);
video_putchar(92,16,3);

video_putchar(73,40,19);
video_putchar(81,40,0);
video_putchar(89,40,17);
video_putchar(97,40,19);
firstcount++;
break;
end

//set controller to select=0 mode
PORTB.5=0;
//if user presses start, goto first level
if (PINB.4 == 0)
begin
action = level_one;
bee=1;
clrextra=1;
PORTB.5=1;
end
break;


//Draws level one and breaks immediately
//to case nothing
case level_one:

life = 9;

if(bee<15)
begin
while(bee<15)
begin
drawbricks_rect((bee<<3),8,1,10,0);
break;
end
bee++;
break;
end

if (clrextra<15)
begin
while(clrextra<15)
begin
drawbricks_rect((clrextra<<3),48,1,5,2);
break;
end
clrextra++;
break;
end

vxpill = 0;
xpill=50;
x=xpill+length-5;
y=93;
vy=0;
vx=0;
//set appropriate flags and goto nothing
loneflag = 1;
ltwoflag = 0;
lthreeflag = 0;
brickcount=140;
hitcount=0;
action = nothing;
paddlenothing = Startnothing;
bee=1;
clrextra=1;
break;

//Draws level two and breaks immediately to
//case nothing
case level_two:

if(bee2<15)
begin
while(bee2<15)
begin
while(beey2>0)
begin
drawbricks_rect((bee2<<3),(bee2<<2)+4,1,beey2,0);
if (bee2%2 == 0) drawbricks_rect((bee2<<3),64,1,1,1);
beey2--;
break;
end
break;
end
bee2++;
break;
end


vxpill = 0;
xpill=50;
x=xpill+length-5;
y=93;
vy=0;
vx=0;

loneflag = 0;
ltwoflag = 1;
lthreeflag = 0;

brickcount=91;
hitcount=0;
action = nothing;
paddlenothing = Startnothing;
bee2=1;
beey2=13;
break;

//Draws level three --> to case nothing
case level_three:
if(bee3<15)
begin
while(bee3<15)
begin
drawbricks_rect((bee3<<3),8,1,10,0);
break;
end
bee3++;
break;
end

if(beediag<12)
begin
while (beediag<12)
begin
drawbricks_rect((beediag<<3)+8,(beediag<<2)+4,1,1,2);
drawbricks_rect((beediag<<3)+16,(beediag<<2)+4,1,1,2);
break;
end
beediag++;
break;
end

if(beediag1<105)
begin
while (beediag1<105)
begin
drawbricks_rect(beediag1-8,beediag2,1,1,2);
drawbricks_rect(beediag1,beediag2,1,1,2);
break;
end
beediag1 = beediag1+8;
beediag2 = beediag2-4;
break;
end

drawbricks_rect(19,63,3,1,1);
drawbricks_rect(85,63,3,1,1);

//set up initial ball location
vxpill = 0;
xpill=50;
x=xpill+length-5;
y=93;
vy=0;
vx=0;

loneflag = 0;
ltwoflag = 0;
lthreeflag = 1;
brickcount=102;
hitcount=0;
action = nothing;
paddlenothing = Startnothing;
bee=3;
beediag=1;
beediag1=24;
beediag2=48;
break;

//Case where paddle can move across screen
//before ball is released from paddle...
//once ball is released, goto gameplay state
case nothing:
switch (paddlenothing)
begin
case Startnothing:

if(stampcount == 0)
begin
video_putchar(40,85,22);
video_putchar(46,85,22);
video_putchar(52,85,22);
video_putchar(58,85,22);
stampcount=1;
break;
end

if(stampcount == 1)
begin
video_putchar(64,85,22);
video_putchar(70,85,22);
video_putchar(76,85,22);
video_putchar(82,85,22);
end


//draw paddle
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
//draw ball
video_pt(x,y,1);
video_pt(x+1,y,1);
video_pt(x,y+1,1);
video_pt(x+1,y+1,1);

//if left/right pad push, goto left/right state
if (PINB.2 == 0) paddlenothing = Rightnothing;
if (PINB.3 == 0) paddlenothing = Leftnothing;
if (PINB.6 == 0)
begin
action = gameplay;
direction = upright;
paddle = Start;
vx=1;
vy=-1;
end
break;

//Paddle moving right, ball not moving
case Rightnothing:
if (PINB.2 == 0)
begin
//wall boundary
if ((xpill+length+1) == 125) paddlenothing=Startnothing;
else
begin
vxpill = 2;
video_pt(xpill,95,0);
video_pt(xpill-1,96,0);
video_pt(xpill,97,0);

video_pt(xpill+1,95,0);
video_pt(xpill,96,0);
video_pt(xpill+1,97,0);

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

x=x+vxpill;
video_pt(x,y,1);
video_pt(x+1,y,1);
video_pt(x,y+1,1);
video_pt(x+1,y+1,1);

xpill=xpill+vxpill;
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
end
end
//if not at wall keep moving
else paddlenothing = Startnothing;
break;

//paddle moving left, ball not moving
case Leftnothing:
if (PINB.3 == 0)
begin
//wall boundary
if ((xpill-1) == 1) paddlenothing=Startnothing;
else
begin
vxpill = -2;
video_pt(xpill+length,95,0);
video_pt(xpill+length+1,96,0);
video_pt(xpill+length,97,0);

video_pt(xpill+length-1,95,0);
video_pt(xpill+length,96,0);
video_pt(xpill+length-1,97,0);

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

x=x+vxpill;
video_pt(x,y,1);
video_pt(x+1,y,1);
video_pt(x,y+1,1);
video_pt(x+1,y+1,1);

xpill=xpill+vxpill;
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
end
end
//else stop moving
else paddlenothing = Startnothing;
break;
end
break;

//Print GAME OVER to screen
//go back to first level
case die_screen:

if(gameover == 0)
begin
video_putchar(30,85,6);
video_putchar(36,85,0);
video_putchar(42,85,12);
video_putchar(48,85,4);
gameover = 1;
break;
end

if(gameover == 1)
begin
video_putchar(60,85,14);
video_putchar(66,85,21);
video_putchar(72,85,4);
video_putchar(78,85,17);
end


PORTB.5=0;
if (PINB.4 == 0)
begin
gameover=0;
xpill = 50;
x=xpill+length-5;
y=93;

stampcount1 = 0;
action = first_screen;
PORTB.5=1;
end
break;

//print YOU WIN to the screen
//then go back to first level with
//"start" button push
case end_screen:

if(youwin == 0)
begin
video_putchar(44,85,26);
video_putchar(50,85,14);
video_putchar(56,85,20);
video_putchar(68,85,24);
youwin = 1;
break;
end

if(youwin == 1)
begin
video_putchar(74,85,8);
video_putchar(80,85,13);
end

PORTB.5=0;
if (PINB.4 == 0)
begin
youwin=0;
xpill = 50;
x=xpill+length-5;
y=93;

stampcount1 = 0;
action = first_screen;
PORTB.5=1;
end
break;



//This case controls the motion of the ball and paddle
//ball goes into directional state based on current
//direction of motion, checks boundary pixels and then bounces
//accordingly, changing state to next dirction. Paddle can move left and
//right in paddle states

case gameplay:
video_pt(x,y,0) ;
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

vxtemp=vx;
vytemp=vy;
switch (direction)
begin
//ball moving up and to the right
case upright:
//check boundary on top and right
top = video_set(x,y-1) || video_set(x+1,y-1);
righty = video_set(x+2,y) || video_set(x+2,y+1);
up_R = video_set(x+2,y-1);
topL = video_set(x,y-1);
topR = video_set(x+1,y-1);
rightyT = video_set(x+2,y);
rightyB = video_set(x+2,y+1);
//several hit scenerios
if (top && righty)
begin
vy = -vy;
vx = -vx;
xprime = (x>>3)<<3;
yprime = y-4;
direction = downleft;
break;
end
if (top)
begin
vy = -vy;
if(!topL)
begin
xprime=x+1;
yprime=y-4;
direction = downright;
break;
end
if(!topR)
begin
xprime=x-7;
yprime=y-4;
direction = downright;
break;
end
xprime = (x>>3)<<3;
yprime = y-4;
direction = downright;
break;
end
if (righty)
begin
vx = -vx;
if (!rightyT)
begin
xprime=x+2;
yprime=y+1;
direction = upleft;
break;
end
if(!rightyB)
begin
xprime=x+2;
yprime=y-3;
direction = upleft;
break;
end
xprime = x+2;
yprime = (y>>2)<<2;
direction = upleft;
break;
end
if (up_R)
begin
vx = -vx;
vy = -vy;
xprime=((x>>3)+1)<<3;
yprime=y-4;
direction = downleft;
break;
end
break;

//ball moving down and to the right
case downright:
//check boundary pixels
dn_R = video_set(x+2,y+2);
bott = video_set(x,y+2) || video_set(x+1,y+2);
bottL = video_set(x,y+2);
bottR = video_set(x+1,y+2);
righty = video_set(x+2,y) || video_set(x+2,y+1);
rightyT = video_set(x+2,y);
rightyB = video_set(x+2,y+1);
//checks for hit off paddle
if (y == 93)
begin
//if hit off left of paddle, move left
if (x>=(xpill-2) && x<=(xpill+(length>>1)-2))
begin
vy=-vy;
vx=-vx;
direction=upleft;
OCR2=notetable[4];
TCCR2=0x1F;
break;
end
//if hit off right side of paddle, send back to the right
else if (x>(xpill+(length>>1)-2) && x<=(xpill+length+2))
begin
vy=-vy;
direction=upright;
OCR2=notetable[4];
TCCR2=0x1F;
break;
end
else
begin
life--;
action = lives;
break;
end
break;
end
if (bott && righty)
begin
vy = -vy;
vx = -vx;
xprime=(x>>3)<<3;
yprime=y+2;
direction = upleft;
break;
end
if (bott)
begin
vy = -vy;
if(!bottL)
begin
xprime=x+1;
yprime=y+2;
direction = upright;
break;
end
if(!bottR)
begin
xprime=x-7;
yprime=y+2;
direction = upright;
break;
end
xprime=(x>>3)<<3;
yprime=y+2;
direction = upright;
break;
end
if (righty)
begin
vx = -vx;
if (!rightyT)
begin
xprime=x+2;
yprime=y+1;
direction = downleft;
break;
end
if(!rightyB)
begin
xprime=x+2;
yprime=y-3;
direction = downleft;
break;
end
xprime = x+2;
yprime = (y>>2)<<2;
direction = downleft;
break;
end
if (dn_R)
begin
vx = -vx;
vy = -vy;
xprime=x+2;
yprime=y+2;
direction = upleft;
break;
end
break;

//ball moving down and to the left
case downleft:
lefty = video_set(x-1,y) || video_set(x-1,y+1);
leftyT = video_set(x-1,y);
leftyB = video_set(x-1,y+1);
bott = video_set(x,y+2) || video_set(x+1,y+2);
bottL = video_set(x,y+2);
bottR = video_set(x+1,y+2);
dn_L = video_set(x-1,y+2);
//check for paddle hit
if (y == 93)
begin
//hit of left of paddle, move back to the left
if (x>=(xpill-2) && x<=(xpill+(length>>1)+2) && x>2)
begin
vy=-vy;
direction=upleft;
OCR2=notetable[4];
TCCR2=0x1F;
break;
end
//if hit off right of paddle, keep moving it right
else if (x>(xpill+(length>>1)+2) && x<=(xpill+length+2))
begin
vy=-vy;
vx=-vx;
direction=upright;
OCR2=notetable[4];
TCCR2=0x1F;
break;
end
else
begin
life--;
action = lives;
break;
end
break;
end
if (bott && lefty)
begin
vy = -vy;
vx = -vx;
xprime=x;
yprime=y+2;
direction = upright;
break;
end
if (bott)
begin
vy = -vy;
if(!bottL)
begin
xprime=x+1;
yprime=y+2;
direction = upleft;
break;
end
if(!bottR)
begin
xprime=x-7;
yprime=y+2;
direction = upleft;
break;
end
xprime=(x>>3)<<3;
yprime=y+2;
direction = upleft;
break;
end
if (lefty)
begin
vx = -vx;
if(!leftyT)
begin
xprime=x-8;
yprime=y+1;
direction = downright;
break;
end
if(!leftyB)
begin
xprime=x-8;
yprime=y-3;
direction = downright;
break;
end
xprime=x-8;
yprime=(y>>2)<<2;
direction = downright;
break;
end
if (dn_L)
begin
vx = -vx;
vy = -vy;
xprime=x-8;
yprime=y+2;
direction = upright;
break;
end
break;

//ball moving up and to the left
case upleft:
lefty = video_set(x-1,y) || video_set(x-1,y+1);
leftyT = video_set(x-1,y);
leftyB = video_set(x-1,y+1);
up_L = video_set(x-1,y-1);
top = video_set(x,y-1) || video_set(x+1,y-1);
topL = video_set(x,y-1);
topR = video_set(x+1,y-1);
//several hit scenerios
if (top && lefty)
begin
vy = -vy;
vx = -vx;
xprime = x;
yprime = y-4;
direction = downright;
break;
end
if (top)
begin
vy = -vy;
if(!topL)
begin
xprime=x+1;
yprime=y-4;
direction = downleft;
break;
end
if(!topR)
begin
xprime=x-7;
yprime=y-4;
direction = downleft;
break;
end
xprime = (x>>3)<<3;
yprime = y-4;
direction = downleft;
break;
end
if (lefty)
begin
vx = -vx;
if(!leftyT)
begin
xprime=x-8;
yprime=y+1;
direction = upright;
break;
end
if(!leftyB)
begin
xprime=x-8;
yprime=y-3;
direction = upright;
break;
end
xprime=x-8;
yprime=(y>>2)<<2;
direction = upright;
break;
end
if (up_L)
begin
vx = -vx;
vy = -vy;
xprime=x-8;
yprime=y-4;
direction = downright;
break;
end
break;
end

//If x-velocity or y-velocity has changed since the last frame
//we must have hit something... conditions account for walls, so we must
//erase the appropriate brick
if((vxtemp != vx || vytemp != vy) && x>4 && x<122 && y>3 && y<61)
begin
erasebrick(xprime,yprime,1);
hitcount++;
OCR2=notetable[5];
TCCR2=0x1F;
end

//Once we hit all bricks on a level, go to evaluate state, which
//determines which level we should goto next
if (hitcount == brickcount)
begin
action = evaluate;
end

//redraw ball position and update velocity for each frame
x = x + vx;
y = y + vy;
video_pt(x,y,1);
video_pt(x+1,y,1);
video_pt(x,y+1,1);
video_pt(x+1,y+1,1);


//cases for paddle action
//the pill is drawn with 3 lines - xpill is defined to be the
//leftmost point of the top line.

switch (paddle)
begin
//Draw paddle in middle of screen (not moving)
//If button push, goto left/right state
case Start:
vxpill = 0;
//draw paddle
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
if (PINB.2 == 0) paddle = Right;
if (PINB.3 == 0) paddle = Left;
break;
//paddle moving right, if we get to the wall, stop
case Right:
if (PINB.2 == 0)
begin
if ((xpill+length+1) == 125) paddle=Start;

else
begin
vxpill = 2;
video_pt(xpill,95,0);
video_pt(xpill-1,96,0);
video_pt(xpill,97,0);

video_pt(xpill+1,95,0);
video_pt(xpill,96,0);
video_pt(xpill+1,97,0);

xpill=xpill+vxpill;
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
end
end

else paddle = Start;

break;
//paddle moving left, if we get to the wall, stop
case Left:
if (PINB.3 == 0)
begin
if ((xpill-1) == 1) paddle=Start;
else
begin
vxpill = -2;

video_pt(xpill+length,95,0);
video_pt(xpill+length+1,96,0);
video_pt(xpill+length,97,0);

video_pt(xpill+length-1,95,0);
video_pt(xpill+length,96,0);
video_pt(xpill+length-1,97,0);

xpill=xpill+vxpill;
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
end
end
else paddle = Start;
break;
end
break;

//This case determines how many lives player has left
//and prints a message to screen after every death to notify
//player of their lives status
case lives:

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

video_line(xpill,95,xpill+length,95,0);
video_line(xpill-1,96,xpill+length+1,96,0);
video_line(xpill,97,xpill+length,97,0);


xpill=60;

if (life>0)
begin
video_putcharn(40,85,life);
video_putchar(50,85,25);

video_line(xpill,87,xpill+length,87,1);
video_line(xpill-1,88,xpill+length+1,88,1);
video_line(xpill,89,xpill+length,89,1);
end

//Once you run out of lives, you're dead
if (life == 0)
begin
action = die_screen;
break;
end

//User must press start to move paddle after death
PORTB.5=0;
if (PINB.4 == 0)
begin
stampcount=0;
xpill = 50;
x=xpill+length-5;
y=93;

action = nothing;
paddlenothing = Startnothing;
PORTB.5=1;
end
break;


//Case determines which level to goto next
//after we have hit all the bricks on the current level
case evaluate:
//if in level 1, goto level 2
if (loneflag == 1)
begin
action = level_two;
video_line(xpill,95,xpill+length,95,0);
video_line(xpill-1,96,xpill+length+1,96,0);
video_line(xpill,97,xpill+length,97,0);

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

break;
end
//if in level 2, goto level 3
if (ltwoflag == 1)
begin
action = level_three;
video_line(xpill,95,xpill+length,95,0);
video_line(xpill-1,96,xpill+length+1,96,0);
video_line(xpill,97,xpill+length,97,0);

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

drawbricks_rect(12,64,14,1,2);

break;
end

//if in level 3, goto end screen
if (lthreeflag == 1)
begin
action = end_screen;
video_line(xpill,95,xpill+length,95,0);
video_line(xpill-1,96,xpill+length+1,96,0);
video_line(xpill,97,xpill+length,97,0);

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

break;
end
break;

end //end action state-machine
end //line 231
end //while
end //main

//Cantneroid.c
//Matt Cantlon and Chris Moschner

//D.5 is sync:1000 ohm + diode to 100 ohm resistor
//D.6 is video:330 ohm + diode to 100 ohm resistor
//D.7 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

//All case Definitions for state-machine
#define Start 1
#define Right 2
#define Left 3
#define Reset 4
#define upleft 5
#define downleft 6
#define upright 7
#define downright 8
#define first_screen 9
#define level_one 10
#define level_two 11
#define die_screen 12
#define gameplay 13
#define nothing 14
#define Startnothing 15
#define Leftnothing 16
#define Rightnothing 17
#define evaluate 18
#define level_three 19
#define lives 20
#define end_screen 21

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

//animation variables
char x, y, s, vx, vy, vxpill, xpill, paddle, length;
char vxtemp, vytemp,direction, action, paddlenothing;
char xprime, yprime;
char screen[1600];
char paddle_left,paddle_middle,paddle_right;
char hitcount,brickcount,bee2,beey2,bee3,beediag,beediag1,beediag2;
char loneflag, ltwoflag, lthreeflag, bee, firstcount;

//sound "chord"
char notetable [6] = {119, 107, 95, 90, 30, 10};
char notecounter;
char life, stampcount, gameover, stampcount1, youwin, clrextra;

//Used to Check if adjacent pixels were illuminated
bit lefty, righty, top, bott, up_R, dn_R, up_L, dn_L;
bit leftyT, leftyB, topL, topR, bottR, bottL,rightyT,rightyB;


//Point plot lookup table
flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

//A 5x7 Bitmap used to place large numbers on the screen
flash char numbers[10][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
0b01110000,
0b10001000,
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 4x8 Bitmap to draw the bricks
flash char bitmap[3][4]={
//Standard brick
0b11111111,
0b10000001,
0b10000001,
0b11111111,
//filled "unbreakable" brick
0b11111111,
0b11111111,
0b11111111,
0b11111111,
//Clear brick used to erase
0b00000000,
0b00000000,
0b00000000,
0b00000000};

//A 5x7 bitmap used to place large letters on the screen
flash char letters[29][7]={
//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,
//Clear
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
//W
0b10001000,
0b10001000,
0b10001000,
0b10101000,
0b10101000,
0b01010000,
0b01010000,
//X
0b00000000,
0b10001000,
0b01010000,
0b00100000,
0b01010000,
0b10001000,
0b00000000,
//Y
0b10001000,
0b10001000,
0b01010000,
0b00100000,
0b00100000,
0b00100000,
0b00100000};

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

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 letter 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 = letters[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);
video_pt(v7+5, v8, (v1 & 0x04)==0x04);
video_pt(v7+6, v8, (v1 & 0x02)==0x02);
video_pt(v7+7, v8, (v1 & 0x01)==0x01);
end
end
//====================================
//put a big number character on the screen
//c is index into bitmap
void video_putcharn(char x, char y, char c)
begin
v7 = x;
for (v6=0;v6<7;v6++)
begin
v1 = numbers[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);
video_pt(v7+5, v8, (v1 & 0x04)==0x04);
video_pt(v7+6, v8, (v1 & 0x02)==0x02);
video_pt(v7+7, v8, (v1 & 0x01)==0x01);
end
end
//=====================================
//draw an array of bricks on the screen

void video_putchar1(char x, char y, char c)
begin
v7 = x;
for (v6=0;v6<4;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);
video_pt(v7+5, v8, (v1 & 0x04)==0x04);
video_pt(v7+6, v8, (v1 & 0x02)==0x02);
video_pt(v7+7, v8, (v1 & 0x01)==0x01);
end
end

//=====================================
//Erase a brick at location x,y
void erasebrick(char x, char y, char c)
begin
v7 = x;
for (v6=0;v6<4;v6++)
begin
v1 = bitmap[c][v6];
v8 = y+v6;
video_pt(v7, v8, 0);
video_pt(v7+1, v8, 0);
video_pt(v7+2, v8, 0);
video_pt(v7+3, v8, 0);
video_pt(v7+4, v8, 0);
video_pt(v7+5, v8, 0);
video_pt(v7+6, v8, 0);
video_pt(v7+7, v8, 0);
end
end
//======================================
//Draw a rectangular based array of bricks
//Note: bricks can only be placed at x locations
//divisible by 8 and y locations divisible by 4
void drawbricks_rect(char x, char y, char l, char h,char g)
begin
char i;
char k;
for (k=0; k<h; k++)
begin
for (i=0; i<l; i++)
begin
video_putchar1((x+(i*8)), y+(k*4), g);
end
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

notecounter=6; //Dictates sound of note played
OCR2 = 10; //Start with high pitch sound
TCCR2 = 0x1F; //divide by 1024; clear-on-match
TCNT2=0; //clear timer2

//init ports
DDRD = 0xf0; //video/sound out and switches
//D.5 is sync:1000 ohm + diode to 100 ohm resistor
//D.6 is video:330 ohm + diode to 100 ohm resistor
//D.7 is sound

//Initialize controller ports (B.5 is select output to controller)
DDRB.2 = 0;
DDRB.3 = 0;
DDRB.4 = 0;
DDRB.5 = 1;
DDRB.6 = 0;

//Initialize controller to select=1 mode
PORTB.5 = 1;

//init timer 0 to 1/uSec
TCCR0 = 2;

//initialize synch constants
LineCount = 1;
syncON = 0b00000000;
syncOFF = 0b00100000;

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

//init ball animation and location
x = 64;
y=80;
vx=-1;
vy=-1;
vxpill=1;
xpill=10;
paddle = Reset;
direction = upleft;
action = first_screen;
bee=1;
beey2=13;
bee2=1;
bee3=1;
beediag=1;
beediag1=24;
beediag2=48;
firstcount = 0;
life = 9;
stampcount=0;
gameover = 0;
youwin = 0;
clrextra=1;

//paddle length
length=20;

//divide paddle into sections for ball bouncing
paddle_left=xpill+floor(length/3);
paddle_middle=paddle_left+floor(length/3);
paddle_right=paddle_middle+floor(length/3);

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

//the animated point- the ball is 4 points (a square)
//with x,y defined as the upper left most point
//erase old one

TCCR2 = 0x00; //turn off sound until prompted


//Matster state-machine which controls hand-shake between
//levels, draws all levels, draws first-screen, draws last screen,
//keeps track of lives and when you win/lose

switch (action)
begin
//draws the First Screen with Game title
case first_screen:

//Erase leftover bricks from old unfinished levels
if(stampcount1 == 0)
begin
video_putchar(28,85,22);
video_putchar(34,85,22);
video_putchar(40,85,22);
video_putchar(46,85,22);
video_putchar(52,85,22);
video_putchar(58,85,22);
stampcount1=1;
break;
end

if(stampcount == 1)
begin
video_putchar(64,85,22);
video_putchar(70,85,22);
video_putchar(76,85,22);
video_putchar(82,85,22);
end

//Write "CANTNEROID" to screen
//write "PRESS START" to screen
if (firstcount == 0)
begin
video_putchar(20,16,2);
video_putchar(28,16,0);
video_putchar(36,16,13);
video_putchar(44,16,19);

video_putchar(17,40,15);
video_putchar(25,40,17);
video_putchar(33,40,4);
firstcount++;
break;
end

if (firstcount == 1)
begin
video_putchar(52,16,13);
video_putchar(60,16,4);
video_putchar(68,16,17);

video_putchar(41,40,18);
video_putchar(49,40,18);
video_putchar(65,40,18);
firstcount++;
break;
end

if(firstcount == 2)
begin
video_putchar(76,16,14);
video_putchar(84,16,8);
video_putchar(92,16,3);

video_putchar(73,40,19);
video_putchar(81,40,0);
video_putchar(89,40,17);
video_putchar(97,40,19);
firstcount++;
break;
end

//set controller to select=0 mode
PORTB.5=0;
//if user presses start, goto first level
if (PINB.4 == 0)
begin
action = level_one;
bee=1;
clrextra=1;
PORTB.5=1;
end
break;


//Draws level one and breaks immediately
//to case nothing
case level_one:

life = 9;

if(bee<15)
begin
while(bee<15)
begin
drawbricks_rect((bee<<3),8,1,10,0);
break;
end
bee++;
break;
end

if (clrextra<15)
begin
while(clrextra<15)
begin
drawbricks_rect((clrextra<<3),48,1,5,2);
break;
end
clrextra++;
break;
end

vxpill = 0;
xpill=50;
x=xpill+length-5;
y=93;
vy=0;
vx=0;
//set appropriate flags and goto nothing
loneflag = 1;
ltwoflag = 0;
lthreeflag = 0;
brickcount=140;
hitcount=0;
action = nothing;
paddlenothing = Startnothing;
bee=1;
clrextra=1;
break;

//Draws level two and breaks immediately to
//case nothing
case level_two:

if(bee2<15)
begin
while(bee2<15)
begin
while(beey2>0)
begin
drawbricks_rect((bee2<<3),(bee2<<2)+4,1,beey2,0);
if (bee2%2 == 0) drawbricks_rect((bee2<<3),64,1,1,1);
beey2--;
break;
end
break;
end
bee2++;
break;
end


vxpill = 0;
xpill=50;
x=xpill+length-5;
y=93;
vy=0;
vx=0;

loneflag = 0;
ltwoflag = 1;
lthreeflag = 0;

brickcount=91;
hitcount=0;
action = nothing;
paddlenothing = Startnothing;
bee2=1;
beey2=13;
break;

//Draws level three --> to case nothing
case level_three:
if(bee3<15)
begin
while(bee3<15)
begin
drawbricks_rect((bee3<<3),8,1,10,0);
break;
end
bee3++;
break;
end

if(beediag<12)
begin
while (beediag<12)
begin
drawbricks_rect((beediag<<3)+8,(beediag<<2)+4,1,1,2);
drawbricks_rect((beediag<<3)+16,(beediag<<2)+4,1,1,2);
break;
end
beediag++;
break;
end

if(beediag1<105)
begin
while (beediag1<105)
begin
drawbricks_rect(beediag1-8,beediag2,1,1,2);
drawbricks_rect(beediag1,beediag2,1,1,2);
break;
end
beediag1 = beediag1+8;
beediag2 = beediag2-4;
break;
end

drawbricks_rect(19,63,3,1,1);
drawbricks_rect(85,63,3,1,1);

//set up initial ball location
vxpill = 0;
xpill=50;
x=xpill+length-5;
y=93;
vy=0;
vx=0;

loneflag = 0;
ltwoflag = 0;
lthreeflag = 1;
brickcount=102;
hitcount=0;
action = nothing;
paddlenothing = Startnothing;
bee=3;
beediag=1;
beediag1=24;
beediag2=48;
break;

//Case where paddle can move across screen
//before ball is released from paddle...
//once ball is released, goto gameplay state
case nothing:
switch (paddlenothing)
begin
case Startnothing:

if(stampcount == 0)
begin
video_putchar(40,85,22);
video_putchar(46,85,22);
video_putchar(52,85,22);
video_putchar(58,85,22);
stampcount=1;
break;
end

if(stampcount == 1)
begin
video_putchar(64,85,22);
video_putchar(70,85,22);
video_putchar(76,85,22);
video_putchar(82,85,22);
end


//draw paddle
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
//draw ball
video_pt(x,y,1);
video_pt(x+1,y,1);
video_pt(x,y+1,1);
video_pt(x+1,y+1,1);

//if left/right pad push, goto left/right state
if (PINB.2 == 0) paddlenothing = Rightnothing;
if (PINB.3 == 0) paddlenothing = Leftnothing;
if (PINB.6 == 0)
begin
action = gameplay;
direction = upright;
paddle = Start;
vx=1;
vy=-1;
end
break;

//Paddle moving right, ball not moving
case Rightnothing:
if (PINB.2 == 0)
begin
//wall boundary
if ((xpill+length+1) == 125) paddlenothing=Startnothing;
else
begin
vxpill = 2;
video_pt(xpill,95,0);
video_pt(xpill-1,96,0);
video_pt(xpill,97,0);

video_pt(xpill+1,95,0);
video_pt(xpill,96,0);
video_pt(xpill+1,97,0);

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

x=x+vxpill;
video_pt(x,y,1);
video_pt(x+1,y,1);
video_pt(x,y+1,1);
video_pt(x+1,y+1,1);

xpill=xpill+vxpill;
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
end
end
//if not at wall keep moving
else paddlenothing = Startnothing;
break;

//paddle moving left, ball not moving
case Leftnothing:
if (PINB.3 == 0)
begin
//wall boundary
if ((xpill-1) == 1) paddlenothing=Startnothing;
else
begin
vxpill = -2;
video_pt(xpill+length,95,0);
video_pt(xpill+length+1,96,0);
video_pt(xpill+length,97,0);

video_pt(xpill+length-1,95,0);
video_pt(xpill+length,96,0);
video_pt(xpill+length-1,97,0);

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

x=x+vxpill;
video_pt(x,y,1);
video_pt(x+1,y,1);
video_pt(x,y+1,1);
video_pt(x+1,y+1,1);

xpill=xpill+vxpill;
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
end
end
//else stop moving
else paddlenothing = Startnothing;
break;
end
break;

//Print GAME OVER to screen
//go back to first level
case die_screen:

if(gameover == 0)
begin
video_putchar(30,85,6);
video_putchar(36,85,0);
video_putchar(42,85,12);
video_putchar(48,85,4);
gameover = 1;
break;
end

if(gameover == 1)
begin
video_putchar(60,85,14);
video_putchar(66,85,21);
video_putchar(72,85,4);
video_putchar(78,85,17);
end


PORTB.5=0;
if (PINB.4 == 0)
begin
gameover=0;
xpill = 50;
x=xpill+length-5;
y=93;

stampcount1 = 0;
action = first_screen;
PORTB.5=1;
end
break;

//print YOU WIN to the screen
//then go back to first level with
//"start" button push
case end_screen:

if(youwin == 0)
begin
video_putchar(44,85,26);
video_putchar(50,85,14);
video_putchar(56,85,20);
video_putchar(68,85,24);
youwin = 1;
break;
end

if(youwin == 1)
begin
video_putchar(74,85,8);
video_putchar(80,85,13);
end

PORTB.5=0;
if (PINB.4 == 0)
begin
youwin=0;
xpill = 50;
x=xpill+length-5;
y=93;

stampcount1 = 0;
action = first_screen;
PORTB.5=1;
end
break;



//This case controls the motion of the ball and paddle
//ball goes into directional state based on current
//direction of motion, checks boundary pixels and then bounces
//accordingly, changing state to next dirction. Paddle can move left and
//right in paddle states

case gameplay:
video_pt(x,y,0) ;
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

vxtemp=vx;
vytemp=vy;
switch (direction)
begin
//ball moving up and to the right
case upright:
//check boundary on top and right
top = video_set(x,y-1) || video_set(x+1,y-1);
righty = video_set(x+2,y) || video_set(x+2,y+1);
up_R = video_set(x+2,y-1);
topL = video_set(x,y-1);
topR = video_set(x+1,y-1);
rightyT = video_set(x+2,y);
rightyB = video_set(x+2,y+1);
//several hit scenerios
if (top && righty)
begin
vy = -vy;
vx = -vx;
xprime = (x>>3)<<3;
yprime = y-4;
direction = downleft;
break;
end
if (top)
begin
vy = -vy;
if(!topL)
begin
xprime=x+1;
yprime=y-4;
direction = downright;
break;
end
if(!topR)
begin
xprime=x-7;
yprime=y-4;
direction = downright;
break;
end
xprime = (x>>3)<<3;
yprime = y-4;
direction = downright;
break;
end
if (righty)
begin
vx = -vx;
if (!rightyT)
begin
xprime=x+2;
yprime=y+1;
direction = upleft;
break;
end
if(!rightyB)
begin
xprime=x+2;
yprime=y-3;
direction = upleft;
break;
end
xprime = x+2;
yprime = (y>>2)<<2;
direction = upleft;
break;
end
if (up_R)
begin
vx = -vx;
vy = -vy;
xprime=((x>>3)+1)<<3;
yprime=y-4;
direction = downleft;
break;
end
break;

//ball moving down and to the right
case downright:
//check boundary pixels
dn_R = video_set(x+2,y+2);
bott = video_set(x,y+2) || video_set(x+1,y+2);
bottL = video_set(x,y+2);
bottR = video_set(x+1,y+2);
righty = video_set(x+2,y) || video_set(x+2,y+1);
rightyT = video_set(x+2,y);
rightyB = video_set(x+2,y+1);
//checks for hit off paddle
if (y == 93)
begin
//if hit off left of paddle, move left
if (x>=(xpill-2) && x<=(xpill+(length>>1)-2))
begin
vy=-vy;
vx=-vx;
direction=upleft;
OCR2=notetable[4];
TCCR2=0x1F;
break;
end
//if hit off right side of paddle, send back to the right
else if (x>(xpill+(length>>1)-2) && x<=(xpill+length+2))
begin
vy=-vy;
direction=upright;
OCR2=notetable[4];
TCCR2=0x1F;
break;
end
else
begin
life--;
action = lives;
break;
end
break;
end
if (bott && righty)
begin
vy = -vy;
vx = -vx;
xprime=(x>>3)<<3;
yprime=y+2;
direction = upleft;
break;
end
if (bott)
begin
vy = -vy;
if(!bottL)
begin
xprime=x+1;
yprime=y+2;
direction = upright;
break;
end
if(!bottR)
begin
xprime=x-7;
yprime=y+2;
direction = upright;
break;
end
xprime=(x>>3)<<3;
yprime=y+2;
direction = upright;
break;
end
if (righty)
begin
vx = -vx;
if (!rightyT)
begin
xprime=x+2;
yprime=y+1;
direction = downleft;
break;
end
if(!rightyB)
begin
xprime=x+2;
yprime=y-3;
direction = downleft;
break;
end
xprime = x+2;
yprime = (y>>2)<<2;
direction = downleft;
break;
end
if (dn_R)
begin
vx = -vx;
vy = -vy;
xprime=x+2;
yprime=y+2;
direction = upleft;
break;
end
break;

//ball moving down and to the left
case downleft:
lefty = video_set(x-1,y) || video_set(x-1,y+1);
leftyT = video_set(x-1,y);
leftyB = video_set(x-1,y+1);
bott = video_set(x,y+2) || video_set(x+1,y+2);
bottL = video_set(x,y+2);
bottR = video_set(x+1,y+2);
dn_L = video_set(x-1,y+2);
//check for paddle hit
if (y == 93)
begin
//hit of left of paddle, move back to the left
if (x>=(xpill-2) && x<=(xpill+(length>>1)+2) && x>2)
begin
vy=-vy;
direction=upleft;
OCR2=notetable[4];
TCCR2=0x1F;
break;
end
//if hit off right of paddle, keep moving it right
else if (x>(xpill+(length>>1)+2) && x<=(xpill+length+2))
begin
vy=-vy;
vx=-vx;
direction=upright;
OCR2=notetable[4];
TCCR2=0x1F;
break;
end
else
begin
life--;
action = lives;
break;
end
break;
end
if (bott && lefty)
begin
vy = -vy;
vx = -vx;
xprime=x;
yprime=y+2;
direction = upright;
break;
end
if (bott)
begin
vy = -vy;
if(!bottL)
begin
xprime=x+1;
yprime=y+2;
direction = upleft;
break;
end
if(!bottR)
begin
xprime=x-7;
yprime=y+2;
direction = upleft;
break;
end
xprime=(x>>3)<<3;
yprime=y+2;
direction = upleft;
break;
end
if (lefty)
begin
vx = -vx;
if(!leftyT)
begin
xprime=x-8;
yprime=y+1;
direction = downright;
break;
end
if(!leftyB)
begin
xprime=x-8;
yprime=y-3;
direction = downright;
break;
end
xprime=x-8;
yprime=(y>>2)<<2;
direction = downright;
break;
end
if (dn_L)
begin
vx = -vx;
vy = -vy;
xprime=x-8;
yprime=y+2;
direction = upright;
break;
end
break;

//ball moving up and to the left
case upleft:
lefty = video_set(x-1,y) || video_set(x-1,y+1);
leftyT = video_set(x-1,y);
leftyB = video_set(x-1,y+1);
up_L = video_set(x-1,y-1);
top = video_set(x,y-1) || video_set(x+1,y-1);
topL = video_set(x,y-1);
topR = video_set(x+1,y-1);
//several hit scenerios
if (top && lefty)
begin
vy = -vy;
vx = -vx;
xprime = x;
yprime = y-4;
direction = downright;
break;
end
if (top)
begin
vy = -vy;
if(!topL)
begin
xprime=x+1;
yprime=y-4;
direction = downleft;
break;
end
if(!topR)
begin
xprime=x-7;
yprime=y-4;
direction = downleft;
break;
end
xprime = (x>>3)<<3;
yprime = y-4;
direction = downleft;
break;
end
if (lefty)
begin
vx = -vx;
if(!leftyT)
begin
xprime=x-8;
yprime=y+1;
direction = upright;
break;
end
if(!leftyB)
begin
xprime=x-8;
yprime=y-3;
direction = upright;
break;
end
xprime=x-8;
yprime=(y>>2)<<2;
direction = upright;
break;
end
if (up_L)
begin
vx = -vx;
vy = -vy;
xprime=x-8;
yprime=y-4;
direction = downright;
break;
end
break;
end

//If x-velocity or y-velocity has changed since the last frame
//we must have hit something... conditions account for walls, so we must
//erase the appropriate brick
if((vxtemp != vx || vytemp != vy) && x>4 && x<122 && y>3 && y<61)
begin
erasebrick(xprime,yprime,1);
hitcount++;
OCR2=notetable[5];
TCCR2=0x1F;
end

//Once we hit all bricks on a level, go to evaluate state, which
//determines which level we should goto next
if (hitcount == brickcount)
begin
action = evaluate;
end

//redraw ball position and update velocity for each frame
x = x + vx;
y = y + vy;
video_pt(x,y,1);
video_pt(x+1,y,1);
video_pt(x,y+1,1);
video_pt(x+1,y+1,1);


//cases for paddle action
//the pill is drawn with 3 lines - xpill is defined to be the
//leftmost point of the top line.

switch (paddle)
begin
//Draw paddle in middle of screen (not moving)
//If button push, goto left/right state
case Start:
vxpill = 0;
//draw paddle
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
if (PINB.2 == 0) paddle = Right;
if (PINB.3 == 0) paddle = Left;
break;
//paddle moving right, if we get to the wall, stop
case Right:
if (PINB.2 == 0)
begin
if ((xpill+length+1) == 125) paddle=Start;

else
begin
vxpill = 2;
video_pt(xpill,95,0);
video_pt(xpill-1,96,0);
video_pt(xpill,97,0);

video_pt(xpill+1,95,0);
video_pt(xpill,96,0);
video_pt(xpill+1,97,0);

xpill=xpill+vxpill;
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
end
end

else paddle = Start;

break;
//paddle moving left, if we get to the wall, stop
case Left:
if (PINB.3 == 0)
begin
if ((xpill-1) == 1) paddle=Start;
else
begin
vxpill = -2;

video_pt(xpill+length,95,0);
video_pt(xpill+length+1,96,0);
video_pt(xpill+length,97,0);

video_pt(xpill+length-1,95,0);
video_pt(xpill+length,96,0);
video_pt(xpill+length-1,97,0);

xpill=xpill+vxpill;
video_line(xpill,95,xpill+length,95,1);
video_line(xpill-1,96,xpill+length+1,96,1);
video_line(xpill,97,xpill+length,97,1);
end
end
else paddle = Start;
break;
end
break;

//This case determines how many lives player has left
//and prints a message to screen after every death to notify
//player of their lives status
case lives:

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

video_line(xpill,95,xpill+length,95,0);
video_line(xpill-1,96,xpill+length+1,96,0);
video_line(xpill,97,xpill+length,97,0);


xpill=60;

if (life>0)
begin
video_putcharn(40,85,life);
video_putchar(50,85,25);

video_line(xpill,87,xpill+length,87,1);
video_line(xpill-1,88,xpill+length+1,88,1);
video_line(xpill,89,xpill+length,89,1);
end

//Once you run out of lives, you're dead
if (life == 0)
begin
action = die_screen;
break;
end

//User must press start to move paddle after death
PORTB.5=0;
if (PINB.4 == 0)
begin
stampcount=0;
xpill = 50;
x=xpill+length-5;
y=93;

action = nothing;
paddlenothing = Startnothing;
PORTB.5=1;
end
break;


//Case determines which level to goto next
//after we have hit all the bricks on the current level
case evaluate:
//if in level 1, goto level 2
if (loneflag == 1)
begin
action = level_two;
video_line(xpill,95,xpill+length,95,0);
video_line(xpill-1,96,xpill+length+1,96,0);
video_line(xpill,97,xpill+length,97,0);

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

break;
end
//if in level 2, goto level 3
if (ltwoflag == 1)
begin
action = level_three;
video_line(xpill,95,xpill+length,95,0);
video_line(xpill-1,96,xpill+length+1,96,0);
video_line(xpill,97,xpill+length,97,0);

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

drawbricks_rect(12,64,14,1,2);

break;
end

//if in level 3, goto end screen
if (lthreeflag == 1)
begin
action = end_screen;
video_line(xpill,95,xpill+length,95,0);
video_line(xpill-1,96,xpill+length+1,96,0);
video_line(xpill,97,xpill+length,97,0);

video_pt(x,y,0);
video_pt(x+1,y,0);
video_pt(x,y+1,0);
video_pt(x+1,y+1,0);

break;
end
break;

end //end action state-machine
end //line 231
end //while
end //main