//video gen and sound //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor //C.0 is sound #pragma regalloc- //I allocate the registers myself #pragma optsize- //optimize for speed #include #include #include #include #include //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 #define MaxBlocks 14 #define width 126 //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, score; unsigned int frame_count; int time; //animation char s; int dx, dy; unsigned int calcx, calcy; char screenx, screeny; unsigned int speed; char out1[20], out2[20]; char level,bricks_remaining; unsigned char paddle_position; unsigned char platform_width; unsigned char old_paddle_position; unsigned char lives,brick_value,miss_penalty; char startover,game_start; char screen[1600]; //, t, ts[10]; char level_str[4]; char ball_str[4]; char bricks_str[5]; char end_game_str[18]; void calc_speed(void); void setup_level(void); void clear_screen(void); char boot_str1[] = "ARKANOID"; char boot_str2[] = "BY"; char boot_str3[] = "RICHARD"; char boot_str4[] = "BALLANTYNE"; char boot_str5[] = "AND"; char boot_str6[] = "DAVID"; char boot_str7[] = "FRANKFORD"; //Point plot lookup table 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 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, //figure2 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000}; //================================ //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 }; flash char level1[14][2] = { {8, 38}, {16, 38}, {24, 38}, {32, 38}, {40, 38}, {48, 38}, {56, 38}, {64, 38}, {72, 38}, {80, 38}, {88, 38}, {96, 38}, {104, 38}, {112, 38} }; flash char level2[28][2] = { {8, 38}, {16, 38}, {24, 38}, {32, 38}, {40, 38}, {48, 38}, {56, 38}, {64, 38}, {72, 38}, {80, 38}, {88, 38}, {96, 38}, {104, 38}, {112, 38}, {8, 34}, {16, 34}, {24, 34}, {32, 34}, {40, 34}, {48, 34}, {56, 34}, {64, 34}, {72, 34}, {80, 34}, {88, 34}, {96, 34}, {104, 34}, {112, 34} }; flash char level3[42][2] = { {8, 38}, {16, 38}, {24, 38}, {32, 38}, {40, 38}, {48, 38}, {56, 38}, {64, 38}, {72, 38}, {80, 38}, {88, 38}, {96, 38}, {104, 38}, {112, 38}, {8, 34}, {16, 34}, {24, 34}, {32, 34}, {40, 34}, {48, 34}, {56, 34}, {64, 34}, {72, 34}, {80, 34}, {88, 34}, {96, 34}, {104, 34}, {112, 34}, {8, 30}, {16, 30}, {24, 30}, {32, 30}, {40, 30}, {48, 30}, {56, 30}, {64, 30}, {72, 30}, {80, 30}, {88, 30}, {96, 30}, {104, 30}, {112, 30} }; flash char level4[56][2] = { {8, 38}, {16, 38}, {24, 38}, {32, 38}, {40, 38}, {48, 38}, {56, 38}, {64, 38}, {72, 38}, {80, 38}, {88, 38}, {96, 38}, {104, 38}, {112, 38}, {8, 34}, {16, 34}, {24, 34}, {32, 34}, {40, 34}, {48, 34}, {56, 34}, {64, 34}, {72, 34}, {80, 34}, {88, 34}, {96, 34}, {104, 34}, {112, 34}, {8, 30}, {16, 30}, {24, 30}, {32, 30}, {40, 30}, {48, 30}, {56, 30}, {64, 30}, {72, 30}, {80, 30}, {88, 30}, {96, 30}, {104, 30}, {112, 30}, {8, 26}, {16, 26}, {24, 26}, {32, 26}, {40, 26}, {48, 26}, {56, 26}, {64, 26}, {72, 26}, {80, 26}, {88, 26}, {96, 26}, {104, 26}, {112, 26} }; flash char level5[70][2] = { {8, 38}, {16, 38}, {24, 38}, {32, 38}, {40, 38}, {48, 38}, {56, 38}, {64, 38}, {72, 38}, {80, 38}, {88, 38}, {96, 38}, {104, 38}, {112, 38}, {8, 34}, {16, 34}, {24, 34}, {32, 34}, {40, 34}, {48, 34}, {56, 34}, {64, 34}, {72, 34}, {80, 34}, {88, 34}, {96, 34}, {104, 34}, {112, 34}, {8, 30}, {16, 30}, {24, 30}, {32, 30}, {40, 30}, {48, 30}, {56, 30}, {64, 30}, {72, 30}, {80, 30}, {88, 30}, {96, 30}, {104, 30}, {112, 30}, {8, 26}, {16, 26}, {24, 26}, {32, 26}, {40, 26}, {48, 26}, {56, 26}, {64, 26}, {72, 26}, {80, 26}, {88, 26}, {96, 26}, {104, 26}, {112, 26}, {8, 22}, {16, 22}, {24, 22}, {32, 22}, {40, 22}, {48, 22}, {56, 22}, {64, 22}, {72, 22}, {80, 22}, {88, 22}, {96, 22}, {104, 22}, {112, 22} }; //================================== //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 ++ ; ADCSR = 0xC6; //read value off AD converter //begin inverted (Vertical) synch after line 247 if (LineCount==248) begin syncON = 0b00100000; syncOFF = 0; end //back to regular sync after line 250 if (LineCount==251) begin syncON = 0; syncOFF = 0b00100000; end //start new frame after line 262 if (LineCount==263) begin LineCount = 1; end delay_us(2); //adjust to make 5 us pulses //end sync pulse PORTD = syncOFF; if (LineCount=ScreenTop) { //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 } if (!ADCSR.6) { paddle_position = ADCH>>1; if (paddle_position < platform_width) { paddle_position = platform_width; } else if (paddle_position > width-(platform_width)) { paddle_position = width-(platform_width); } } 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 if (str[i]==' ') video_putchar(x, y, 37); else video_putchar(x,y,str[i]-0x40+9); x = x+6; end end //================================== // put a small character on the screen // x-cood must be on divisible by 4 // c is index into bitmap void video_smallchar(char x, char y, char c) begin char mask; i=((int)x>>3) + ((int)y<<4) ; if (x == (x & 0xf8)) mask = 0x0f; //f8 else mask = 0xf0; screen[i] = (screen[i] & mask) | (smallbitmap[c][0] & ~mask); screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask); screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask); screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask); screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask); end //================================== // put a string of small characters on the screen // x-cood must be on divisible by 4 void video_putsmalls(char x, char y, char *str) begin char i ; for (i=0; str[i]!=0; i++) begin if (str[i]>=0x30 && str[i]<=0x3a) video_smallchar(x,y,str[i]-0x30); else video_smallchar(x,y,str[i]-0x40+12); x = x+4; end end //================================== //plot a line //at x1,y1 to x2,y2 with color 1=white 0=black 2=invert //NOTE: this function requires signed chars //Code is from David Rodgers, //"Procedural Elements of Computer Graphics",1985 void video_line(char x1, char y1, char x2, char y2, char c) begin int e; signed char dx,dy,j, temp; signed char s1,s2, xchange; signed char x,y; x = x1; y = y1; dx = cabs(x2-x1); dy = cabs(y2-y1); s1 = csign(x2-x1); s2 = csign(y2-y1); xchange = 0; if (dy>dx) begin temp = dx; dx = dy; dy = temp; xchange = 1; end e = ((int)dy<<1) - dx; for (j=0; j<=dx; j++) begin video_pt(x,y,c) ; if (e>=0) begin if (xchange==1) x = x + s1; else y = y + s2; e = e - ((int)dx<<1); end if (xchange==1) y = y + s2; else x = x + s1; e = e + ((int)dy<<1); end end //================================== //return the value of one point //at x,y with color 1=white 0=black 2=invert char video_set(char x, char y) begin //The following construction //detects exactly one bit at the x,y location i=((int)x>>3) + ((int)y<<4) ; return ( screen[i] & 1<<(7-(x & 0x7))); end void video_brick(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 ; now have the bytes current value in r5 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 ; clr r5 cpi r16,1 brne tst0a ldi r16, 0x7e or r5,r16 tst0a: 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 void clear_screen (void) { int j; for (j = 0; j < 1600; j++) screen[j] = 0; } //================================== // set up the ports and timers void main(void) begin //init timer 1 to generate sync OCR1A = lineTime; //One NTSC line TCCR1B = 9; //full speed; clear-on-match TCCR1A = 0x00; //turn off pwm and oc lines TIMSK = 0x10; //enable interrupt T1 cmp //init ports DDRD = 0xf0; //video out and switches //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor ADMUX = 0x20; //sets internal reference and analog channel 0 ADCSR = 0x80; //enables AD converter // ADMUX = 0b11100000; //enable ADC and set prescaler to 1/64*8MHz=125,000 //and set int enable // ADCSR = 0x80 + 0x06 + 0x08; //init timer 0 to 1/uSec TCCR0 = 2; //initialize synch constants LineCount = 1; syncON = 0b00000000; syncOFF = 0b00100000; startover = 0; level = 0; game_start = 1; clear_screen(); while (1) { platform_width = 7; //old_paddle_position = 128; //init animation lives = 2; frame_count=0; bricks_remaining = 0; //video_pt(screenx,screeny,1); //enable sleep mode MCUCR = 0b10000000; #asm ("sei"); //The following loop executes once/video line during lines //1-230, then does all of the frame-end processing while((!startover) && (!game_start)) { //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) { //the animated point if (bricks_remaining ==0) { level++; lives++; bricks_remaining = MaxBlocks*level; //change maxblocks later to value dependant on current level setup_level(); } calc_speed(); if (lives == 0) { startover = 1; clear_screen(); } } //line 231 } //while !startover while(startover || level==6 || game_start) { #asm ("sleep"); if (LineCount==231) { if (startover && frame_count==0) { sprintf(end_game_str,"GAME OVER"); video_puts(40, 47, end_game_str); } else if ((level==6) && (frame_count==0)) { sprintf(end_game_str,"YOU WIN"); video_puts(40, 47, end_game_str); } else if ((frame_count==0) && (game_start)) { video_puts(42, 15, boot_str1); video_puts(59, 30, boot_str2); } else if ((frame_count==1) && (game_start)) { video_puts(10, 45, boot_str3); } else if ((frame_count==2) && (game_start)) { video_puts(55, 45, boot_str4); } else if ((frame_count==3) && (game_start)) { video_puts(57, 60, boot_str5); } else if ((frame_count==4) && (game_start)) { video_puts(20, 75, boot_str6); } else if ((frame_count==5) && (game_start)) { video_puts(55, 75, boot_str7); } frame_count++; if (frame_count > 200) { //wait for 7 seconds startover = 0; frame_count = 0; level = 0; game_start = 0; lives = 2; score = 0; } } //line 231 } //while startover } // while(1) } //main void calc_speed(void) { // char paddle_pos_str[4]; //test code - string is printed to screen video_pt(screenx,screeny,0) ; //erase old one calcx = calcx + dx; calcy = calcy + dy; s = video_set(screenx,screeny); if (s>0) dy = -dy; //if true, must be a line there screenx = calcx>>9; screeny = calcy>>9; sprintf(out1, "%4d %4d", dx, dy); video_line(old_paddle_position-platform_width,99,old_paddle_position+platform_width,99,0); video_line(paddle_position-platform_width,99,paddle_position+platform_width,99,1); old_paddle_position = paddle_position; if (screenx>=124 || screenx<=1) dx = -dx; else if (screeny<=12) dy = -dy; // Determine whether platform is hit at screen bottom else if (screeny >= 98) { if ((screenx >= paddle_position-platform_width)&& (screenx <= paddle_position+platform_width)) { dx = -((int)((signed)paddle_position-(signed)screenx))<<3; if (score>= miss_penalty) { score-=miss_penalty; } } else { lives--; calcx = 32768; calcy = 50000; dx = 10; } dy = -(int)(sqrt(speed - dx^2)); dx <<=3; dy <<=3; } // Determine whether an obstacle was hit else if (screen[(screenx>>3) + ((int)screeny<<4)]&pos[screenx&7]) { if (screen[(screenx>>3) + ((int)screeny+2<<4)]&pos[screenx&7] || screen[(screenx>>3) + ((int)screeny-2<<4)]&pos[screenx&7]) dx = -dx; else dy = -dy; //erase 2 brick layers (one brick..) video_brick(screenx, screeny&0xfe, 0); video_brick(screenx, screeny|0x01, 0); bricks_remaining--; score+=brick_value; } video_pt(screenx,screeny,1) ; // Print status info sprintf(level_str,"L%d",level); sprintf(bricks_str,"%2d",score); sprintf(ball_str,"B%2d",lives); video_puts(3,2,level_str); video_puts(50,2,bricks_str); video_puts(100,2,ball_str); // video_puts(10,50,out1); //testing paddle position & ADC // sprintf(paddle_pos_str,"%3u",paddle_position); // video_puts(50,50,paddle_pos_str); //end paddle position testing } void setup_level(void) { char i,j; #asm ("cli"); clear_screen(); miss_penalty = 1*level; brick_value = 10*level; speed = 4000+1000*level; calcx=32768; calcy=50000; dx=10; dy = -(int)(sqrt(speed - dx^2)); dx<<=3; dy<<=3; 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,10,width,10,1); switch(level) { case 1: for (i = 0; i < 14; i++) { video_brick(level1[i][0], level1[i][1], 1); video_brick(level1[i][0], level1[i][1]+1, 1); } break; case 2: for (i = 0; i < 28; i++) { video_brick(level2[i][0], level2[i][1], 1); video_brick(level2[i][0], level2[i][1]+1, 1); } break; case 3: for (i = 0; i < 42; i++) { video_brick(level3[i][0], level3[i][1], 1); video_brick(level3[i][0], level3[i][1]+1, 1); } break; case 4: for (i = 0; i < 56; i++) { video_brick(level4[i][0], level4[i][1], 1); video_brick(level4[i][0], level4[i][1]+1, 1); } break; case 5: for (i = 0; i < 70; i++) { video_brick(level5[i][0], level5[i][1], 1); video_brick(level5[i][0], level5[i][1]+1, 1); } break; default: ; } #asm ("sei"); }