//Monkeyball clone //by James Rose and David Zai //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 // Controller wire settings // Brown = up (1) // Purple = down (2) // Red = left (3) // Yellow = right (4) // Black = Ground (8) // White = VDD (5,7) // Blue = Button B (controller 1) (6) #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 MAXPLAYERS 4 #define width 116 //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 char s; union myx { char highx[2]; int x; }; union myy { char highy[2]; int y; }; // Velocity vectors union myx xun[MAXPLAYERS]; union myy yun[MAXPLAYERS]; int vx[MAXPLAYERS]; int vy[MAXPLAYERS]; char screen[1600], ts[10]; // Character displays char Play1[]="P1"; char Play2[]="P2"; char Play3[]="P3"; char Play4[]="P4"; char win[]= "WINS"; char title[]="WELCOME TO MONKEYBALL 0 1"; char playerstr[]="NUMBER OF PLAYERS: "; char maxpoints[]="POINTS TO WIN: "; char start[]="PRESS B TO START"; char any[]="P1 OR P2 PRESS ANY KEY"; char clrpnt[]=" "; char GameState; char winner, timer; char numplayers, WinningPoint, edit, powerx, powery, poweravail; int powertimer; char score[MAXPLAYERS]; char deadtimer[MAXPLAYERS]; char cont[MAXPLAYERS][4]; char power[MAXPLAYERS]; char killer[MAXPLAYERS]; unsigned char invincetimer[MAXPLAYERS]; char initx[MAXPLAYERS]={ 30, 93, 93, 30} ; char inity[MAXPLAYERS]={ 25, 75, 25, 75}; // State machine and button-debouncing variables enum {titlescreen, normal, playerdie, MaybeInc, MaybeDec, ButtonDown, MaybeButtonUp, MaybeDown, MaybeUp}; enum {UP, DOWN, LEFT, RIGHT}; // Options for start menu enum {players, points}; /* //Musical note values //C below middle C to C above middle C //zeros are rests flash char notes[] = {239,213,189,179,159,142,126, 120,106,94,90,80,71,63,60,0,0,0,0}; char note, musicT; */ //Point plot lookup table //One bit masks flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //define some character bitmaps //5x7 characters flash char bitmap[38][7]={ //0 0b01110000, 0b10001000, 0b10011000, 0b10101000, 0b11001000, 0b10001000, 0b01110000, //1 0b00100000, 0b01100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b01110000, //2 0b01110000, 0b10001000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b11111000, //3 0b11111000, 0b00010000, 0b00100000, 0b00010000, 0b00001000, 0b10001000, 0b01110000, //4 0b00010000, 0b00110000, 0b01010000, 0b10010000, 0b11111000, 0b00010000, 0b00010000, //5 0b11111000, 0b10000000, 0b11110000, 0b00001000, 0b00001000, 0b10001000, 0b01110000, //6 0b01000000, 0b10000000, 0b10000000, 0b11110000, 0b10001000, 0b10001000, 0b01110000, //7 0b11111000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000, 0b10000000, //8 0b01110000, 0b10001000, 0b10001000, 0b01110000, 0b10001000, 0b10001000, 0b01110000, //9 0b01110000, 0b10001000, 0b10001000, 0b01111000, 0b00001000, 0b00001000, 0b00010000, //A 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b11111000, 0b10001000, 0b10001000, //B 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10001000, 0b10001000, 0b11110000, //C 0b01110000, 0b10001000, 0b10000000, 0b10000000, 0b10000000, 0b10001000, 0b01110000, //D 0b11110000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b11110000, //E 0b11111000, 0b10000000, 0b10000000, 0b11111000, 0b10000000, 0b10000000, 0b11111000, //F 0b11111000, 0b10000000, 0b10000000, 0b11111000, 0b10000000, 0b10000000, 0b10000000, //G 0b01110000, 0b10001000, 0b10000000, 0b10011000, 0b10001000, 0b10001000, 0b01110000, //H 0b10001000, 0b10001000, 0b10001000, 0b11111000, 0b10001000, 0b10001000, 0b10001000, //I 0b01110000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b01110000, //J 0b00111000, 0b00010000, 0b00010000, 0b00010000, 0b00010000, 0b10010000, 0b01100000, //K 0b10001000, 0b10010000, 0b10100000, 0b11000000, 0b10100000, 0b10010000, 0b10001000, //L 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b11111000, //M 0b10001000, 0b11011000, 0b10101000, 0b10101000, 0b10001000, 0b10001000, 0b10001000, //N 0b10001000, 0b10001000, 0b11001000, 0b10101000, 0b10011000, 0b10001000, 0b10001000, //O 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01110000, //P 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10000000, 0b10000000, 0b10000000, //Q 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b10101000, 0b10010000, 0b01101000, //R 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10100000, 0b10010000, 0b10001000, //S 0b01111000, 0b10000000, 0b10000000, 0b01110000, 0b00001000, 0b00001000, 0b11110000, //T 0b11111000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, //U 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01110000, //V 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01010000, 0b00100000, //W 0b10001000, 0b10001000, 0b10001000, 0b10101000, 0b10101000, 0b10101000, 0b01010000, //X 0b10001000, 0b10001000, 0b01010000, 0b00100000, 0b01010000, 0b10001000, 0b10001000, //Y 0b10001000, 0b10001000, 0b10001000, 0b01010000, 0b00100000, 0b00100000, 0b00100000, //Z 0b11111000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000, 0b11111000, //figure1 0b01110000, 0b00100000, 0b01110000, 0b10101000, 0b00100000, 0b01010000, 0b10001000, //figure2 0b01110000, 0b10101000, 0b01110000, 0b00100000, 0b00100000, 0b01010000, 0b10001000}; //================================ //3x5 font numbers, then letters //packed two per definition for fast //copy to the screen at x-position divisible by 4 flash char smallbitmap[40][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, //square 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111 }; //================================== //This is the sync generator and raster generator. It MUST be entered from //sleep mode to get accurate timing of the sync pulses #pragma warn- interrupt [TIM1_COMPA] void t1_cmpA(void) begin //start the Horizontal sync pulse PORTD = syncON; //update the curent scanline number LineCount ++ ; //begin inverted (Vertical) synch after line 247 if (LineCount==248) begin syncON = 0b00100000; syncOFF = 0; end //back to regular sync after line 250 if (LineCount==251) begin syncON = 0; syncOFF = 0b00100000; end //start new frame after line 262 if (LineCount==263) begin LineCount = 1; end delay_us(2); //adjust to make 5 us pulses //end sync pulse PORTD = syncOFF; if (LineCount=ScreenTop) begin //compute byte index for beginning of the next line //left-shift 4 would be individual lines // <<3 means line-double the pixels //The 0xfff8 truncates the odd line bit //i=(LineCount-ScreenTop)<<3 & 0xfff8; // #asm push r16 lds r12, _LineCount lds r13, _Linecount+1 ldi r16, 30 sub r12, r16 ldi r16,0 sbc r13, r16 lsl r12 rol r13 lsl r12 rol r13 lsl r12 rol r13 mov r16,r12 andi r16,0xf0 mov r12,r16 pop r16 #endasm //load 16 registers with screen info #asm push r14 push r15 push r16 push r17 push r18 push r19 push r26 push r27 ldi r26,low(_screen) ;base address of screen ldi r27,high(_screen) add r26,r12 ;offset into screen (add i) adc r27,r13 ld r4,x+ ;load 16 registers and inc pointer ld r5,x+ ld r6,x+ ld r7,x+ ld r8,x+ ld r9,x+ ld r10,x+ ld r11,x+ ld r12,x+ ld r13,x+ ld r14,x+ ld r15,x+ ld r16,x+ ld r17,x+ ld r18,x+ ld r19,x pop r27 pop r26 #endasm delay_us(4); //adjust to center image on screen //blast 16 bytes to the screen #asm ;but first a macro to make the code shorter ;the macro takes a register number as a parameter ;and dumps its bits serially to portD.6 ;the nop can be eliminated to make the display narrower .macro videobits ;regnum BST @0,7 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,6 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,5 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,4 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,3 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,2 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,1 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,0 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 .endm videobits r4 ;video line -- byte 1 videobits r5 ;byte 2 videobits r6 ;byte 3 videobits r7 ;byte 4 videobits r8 ;byte 5 videobits r9 ;byte 6 videobits r10 ;byte 7 videobits r11 ;byte 8 videobits r12 ;byte 9 videobits r13 ;byte 10 videobits r14 ;byte 11 videobits r15 ;byte 12 videobits r16 ;byte 13 videobits r17 ;byte 14 videobits r18 ;byte 15 videobits r19 ;byte 16 clt ;clear video after the last pixel on the line IN R30,0x12 BLD R30,6 OUT 0x12,R30 pop r19 pop r18 pop r17 pop r16 pop r15 pop r14 #endasm end end #pragma warn+ //================================== //plot one point //at x,y with color 1=white 0=black 2=invert #pragma warn- void video_pt(char x, char y, char c) begin #asm ; i=(x>>3) + ((int)y<<4) ; the byte with the pixel in it push r16 ldd r30,y+2 ;get x lsr r30 lsr r30 lsr r30 ;divide x by 8 ldd r12,y+1 ;get y lsl r12 ;mult y by 16 clr r13 lsl r12 rol r13 lsl r12 rol r13 lsl r12 rol r13 add r12, r30 ;add in x/8 ;v2 = screen[i]; r5 ;v3 = pos[x & 7]; r6 ;v4 = c r7 ldi r30,low(_screen) ldi r31,high(_screen) add r30, r12 adc r31, r13 ld r5,Z ;get screen byte ldd r26,y+2 ;get x ldi r27,0 andi r26,0x07 ;form x & 7 ldi r30,low(_pos*2) ldi r31,high(_pos*2) add r30,r26 adc r31,r27 lpm r6,Z ld r16,y ;get c ;if (v4==1) screen[i] = v2 | v3 ; ;if (v4==0) screen[i] = v2 & ~v3; ;if (v4==2) screen[i] = v2 ^ v3 ; cpi r16,1 brne tst0 or r5,r6 tst0: cpi r16,0 brne tst2 com r6 and r5,r6 tst2: cpi r16,2 brne writescrn eor r5,r6 writescrn: ldi r30,low(_screen) ldi r31,high(_screen) add r30, r12 adc r31, r13 st Z, r5 ;write the byte back to the screen pop r16 #endasm end #pragma warn+ //================================== // put a big character on the screen // c is index into bitmap void video_putchar(char x, char y, char c) begin v7 = x; for (v6=0;v6<7;v6++) begin v1 = bitmap[c][v6]; v8 = y+v6; video_pt(v7, v8, (v1 & 0x80)==0x80); video_pt(v7+1, v8, (v1 & 0x40)==0x40); video_pt(v7+2, v8, (v1 & 0x20)==0x20); video_pt(v7+3, v8, (v1 & 0x10)==0x10); video_pt(v7+4, v8, (v1 & 0x08)==0x08); end end //================================== // put a string of big characters on the screen void video_puts(char x, char y, char *str) begin char i ; for (i=0; str[i]!=0; i++) begin if (str[i]>=0x30 && str[i]<=0x3a) video_putchar(x,y,str[i]-0x30); else video_putchar(x,y,str[i]-0x40+9); x = x+6; end end //================================== // put a small character on the screen // x-cood must be on divisible by 4 // c is index into bitmap void video_smallchar(char x, char y, char c) begin char mask; i=((int)x>>3) + ((int)y<<4) ; if (x == (x & 0xf8)) mask = 0x0f; //f8 else mask = 0xf0; screen[i] = (screen[i] & mask) | (smallbitmap[c][0] & ~mask); screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask); screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask); screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask); screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask); end //================================== // put a string of small characters on the screen // x-cood must be on divisible by 4 void video_putsmalls(char x, char y, char *str) begin char i ; for (i=0; str[i]!=0; i++) begin if (str[i]>=0x30 && str[i]<=0x3a) video_smallchar(x,y,str[i]-0x30); else if(str[i] == 0x20) video_smallchar(x,y,12); 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 //================================== // OUR ADDITIONS: // Draws a horizontal line more efficiently void horiz_line(unsigned char x1, unsigned char x2, unsigned char y) { int i, limit; signed char newbyte = 0; i = ((int)y)<<4; i += (int)x1>>3; //i = 161; limit = (int)x2>>3; limit += ((int)y)<<4; newbyte = 0xff >> (x1&7); screen[i] = newbyte; //i = 160; i++; //PORTC ^= 0x1; //PORTC = ~(newbyte); while( i < limit) { screen[i] = 0xff; i++; } newbyte = 0xff << (7-(x2&7)); screen[i] = newbyte; } // Clears a horizontal line void horiz_lineclear(unsigned char x1, unsigned char x2, unsigned char y) { int i, limit; signed char newbyte = 0; i = ((int)y)<<4; i += (int)x1>>3; limit = (int)x2>>3; limit += ((int)y)<<4; while(i <= limit) { screen[i] = 0; i++; } } // Draws a vertical line more efficiently void vert_line(char x, char y1, char y2) { int i, limit; char newbyte; i = ((int)x>>3) + ((int)y1<<4); limit = ((int)x>>3) + ((int)y2<<4); newbyte = 0x80 >> (x&7); while(i <= limit){ screen[i] = newbyte; i+=16; } } // Clears a vertical line void vert_lineclear(char x, char y1, char y2) { int i, limit; char newbyte; i = ((int)x>>3) + ((int)y1<<4); limit = ((int)x>>3) + ((int)y2<<4); newbyte = 0; while(i <= limit){ screen[i] = newbyte; i+=16; } } // clears a small char void video_clearsmall(char x, char y, char len) { char j, test, halflen; int i; halflen = len >> 1; test = 0; i=((int)x>>3) + ((int)y<<4); if(x != (x & 0xf8)) { screen[i] = (screen[i] & 0xf0); screen[i+16] = (screen[i+16] & 0xf0); screen[i+32] = (screen[i+32] & 0xf0); screen[i+48] = (screen[i+48] & 0xf0); screen[i+64] = (screen[i+64] & 0xf0); i++; test = 1; if(len & 1 == 0) halflen--; } for(j=0; j < halflen; j++) { screen[i] = 0; screen[i+16] = 0; screen[i+32] = 0; screen[i+48] = 0; screen[i+64] = 0; i++; } if(test & len | ~test & ~len & 1) { screen[i] = (screen[i] & 0x0f); screen[i+16] = (screen[i+16] & 0x0f); screen[i+32] = (screen[i+32] & 0x0f); screen[i+48] = (screen[i+48] & 0x0f); screen[i+64] = (screen[i+64] & 0x0f); } } //================================== //Make a circle void circle_pt(char x, char y, char c, char character, char power) { char xmod4; if (x > 3) if (x < 123) if (y > 3) if (y < 96) { xmod4 = x - x%4; video_smallchar(xmod4, y-2, character); if(power == 2){ video_pt(x, y+3, c); video_pt(x, y+2, c); video_pt(x, y+1, c); video_pt(x, y, c); video_pt(x, y-1, c); video_pt(x, y-2, c); video_pt(x, y-3, c); } if(power == 1){ video_pt(x+3, y, c); video_pt(x+2, y, c); video_pt(x+1, y, c); video_pt(x, y, c); video_pt(x-1, y, c); video_pt(x-2, y, c); video_pt(x-3, y, c); } video_pt(x+4, y ,c); video_pt(x+4, y+1, c); video_pt(x+4, y-1, c); video_pt(x+3, y+2, c); video_pt(x+3, y-2, c); video_pt(x+2, y+3, c); video_pt(x+2, y-3, c); video_pt(x+1, y+4, c); video_pt(x+1, y-4, c); video_pt(x, y+4, c); video_pt(x, y-4, c); video_pt(x-4, y ,c); video_pt(x-4, y+1, c); video_pt(x-4, y-1, c); video_pt(x-3, y+2, c); video_pt(x-3, y-2, c); video_pt(x-2, y+3, c); video_pt(x-2, y-3, c); video_pt(x-1, y+4, c); video_pt(x-1, y-4, c); video_pt(x+3, y+3, c); video_pt(x+3, y-3, c); video_pt(x-3, y+3, c); video_pt(x-3, y-3, c); } } //================================== // resets board void initialize() { char plyer; timer = 0; winner = 0; GameState = ButtonDown; //GameState = normal; //top line & bottom lines vert_line(10, 10, 89); vert_line(width,10,89); horiz_line(10, width, 10); horiz_line(10, width, 89); // Power-up inits poweravail = 0; powerx = 64; powery = 50; powertimer = 0; // Initial stats for each player for(plyer = 0; plyer < MAXPLAYERS; plyer++) { score[plyer] = 0; deadtimer[plyer] = 0; killer[plyer] = -1; power[plyer] = 0; vx[plyer] = 0; vy[plyer] = 0; xun[plyer].highx[1] = initx[plyer]; yun[plyer].highy[1] = inity[plyer]; invincetimer[plyer] = 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 // PORTC used for LED debugging //DDRC = 0xff; //PORTC = 0xff; // Port A and Port B used for the Sega controller inputs DDRA = 0x00; DDRB = 0x00; //initialize synch constants LineCount = 1; syncON = 0b00000000; syncOFF = 0b00100000; //side lines vert_line(10, 10, 89); vert_line(width,10,89); //top line & bottom lines horiz_line(10, width, 10); horiz_line(10, width, 89); // function to initialize the board initialize(); numplayers = 2; WinningPoint = 10; edit = players; //init musical scale //note = 0; //musicT = 0; //use OC0 (pin B.3) for music //DDRB.3 = 1 ; //enable sleep mode MCUCR = 0b10000000; #asm ("sei"); //The following loop executes once/video line during lines //1-230, then does all of the frame-end processing while(1) begin //stall here until next line starts //sleep enable; mode=idle //use sleep to make entry into sync ISR uniform time #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 char plyer, cont12, cont34, oplyer; int dist2, dispx, dispy, delvx, delvy, dotp, tempv; // PortA is for Controllers 1 and 2 // PortB is for Controllers 3 and 4 // Cont 1 = A.7, A.5, A.3, A.1 // Cont 2 = A.6, A.4, A.2, A.0 // Cont 3 = B.7, B.5, B.3, B.1 // Cont 4 = B.6, B.4, B.2, B.0 cont12 = PINA; cont34 = PINB; //PORTC = PINA; switch(GameState){ // Title screen code: case titlescreen: // Prints "Welcome to Monkeyball 0.1" video_putsmalls(16, 25, title); video_pt(109,29,1); // Prints "Press B to Start" video_putsmalls(32, 40, start); // Prints "Number of Players:" video_putsmalls(20,60, playerstr); video_smallchar(100,60, numplayers); // Prints "Points to win:" video_putsmalls(20, 70, maxpoints); video_smallchar(96,70, WinningPoint/10); video_smallchar(100,70, WinningPoint%10); // Alternates the cursor between players and points if (edit == players){ video_smallchar(104, 60, 39); video_smallchar(104, 70, 12); }else{ // points video_smallchar(104, 60, 12); video_smallchar(104, 70, 39); } // Button debouncing for title screen if((PINA & 0x80) == 0){ GameState = MaybeUp; } if((PINA & 0x20) == 0){ GameState = MaybeDown; } if((PINA & 0x08) == 0){ GameState = MaybeDec; } if((PINA & 0x02) == 0){ GameState = MaybeInc; } // Starts the game when Player 1 presses button B if((PIND & 0x01) == 0){ GameState = normal; video_clearsmall(16, 25, 25); video_clearsmall(32, 40, 16); video_clearsmall(20, 60, 22); video_clearsmall(20, 70, 22); } break; /* Button debouncing for title screen */ // Checks if right was pushed case MaybeInc: if((PINA & 0x02) == 0){ if (edit == players){ if (numplayers < MAXPLAYERS) numplayers++; } else { if (WinningPoint < 99) WinningPoint++; } GameState = ButtonDown; } else GameState = titlescreen; break; // Checks if left was pushed case MaybeDec: if((PINA & 0x08) == 0){ if (edit == players){ if (numplayers > 2) numplayers--; } else { if (WinningPoint > 1) WinningPoint--; } GameState = ButtonDown; } else GameState = titlescreen; break; // Checks if up was pushed case MaybeUp: if((PINA & 0x80) == 0){ if (edit == players){ edit = points; } else edit = players; GameState = ButtonDown; } else GameState = titlescreen; break; // Checks if down was pushed case MaybeDown: if((PINA & 0x20) == 0){ if (edit == players){ edit = points; } else edit = players; GameState = ButtonDown; } else GameState = titlescreen; break; // Checks if the button was released case ButtonDown: if((PINA & 0xaa) == 0xaa){ GameState = MaybeButtonUp; } break; case MaybeButtonUp: if((PINA & 0xaa) == 0xaa){ GameState = titlescreen; } else GameState = ButtonDown; break; /* Normal game mode */ case normal: // Draws initial circle for each player for(plyer = 0; plyer < numplayers; plyer++) { circle_pt(xun[plyer].highx[1],yun[plyer].highy[1],0, 12, power[plyer]); } //side lines vert_line(10, 10, 89); vert_line(width,10,89); //top line & bottom lines horiz_line(10, width, 10); horiz_line(10, width, 89); // Draws a blank for the power-up character video_smallchar(powerx, powery, 12); // Checks controller input for players 1 and 2 cont[0][UP] = (cont12 & 0x80); cont[0][DOWN] = (cont12 & 0x20); cont[0][LEFT] = (cont12 & 0x08); cont[0][RIGHT] = (cont12 & 0x02); cont[1][UP] = (cont12 & 0x40); cont[1][DOWN] = (cont12 & 0x10); cont[1][LEFT] = (cont12 & 0x04); cont[1][RIGHT] = (cont12 & 0x01); // Checks controller input for players 3 and 4 cont[2][UP] = (cont34 & 0x80); cont[2][DOWN] = (cont34 & 0x20); cont[2][LEFT] = (cont34 & 0x08); cont[2][RIGHT] = (cont34 & 0x02); cont[3][UP] = (cont34 & 0x40); cont[3][DOWN] = (cont34 & 0x10); cont[3][LEFT] = (cont34 & 0x04); cont[3][RIGHT] = (cont34 & 0x01); // For-loop that cycles through each of the players // and does all of the necessary calculations for // drawing the next frame for(plyer = 0; plyer < numplayers; plyer++) { // Checks if player remains in-bounds. // This code ignores controller input if the player // is heading out of bounds. if(xun[plyer].highx[1] <= width && xun[plyer].highx[1] >= 10 && yun[plyer].highy[1] <= 89 && yun[plyer].highy[1] >= 10){ // Player has "heavy" powerup so it can // only have half the maximum velocity if(power[plyer] == 1) { if(cont[plyer][UP] == 0){ if (vy[plyer] > -240) vy[plyer]-=16; } if(cont[plyer][LEFT] == 0){ if (vx[plyer] > -240) vx[plyer]-=16; } if(cont[plyer][RIGHT] == 0){ if (vx[plyer] < 240) vx[plyer]+=16; } if(cont[plyer][DOWN] == 0){ if (vy[plyer] < 240) vy[plyer]+=16; } } else { if(cont[plyer][UP] == 0){ if (vy[plyer] > -480) vy[plyer]-=16; } if(cont[plyer][LEFT] == 0){ if (vx[plyer] > -480) vx[plyer]-=16; } if(cont[plyer][RIGHT] == 0){ if (vx[plyer] < 480) vx[plyer]+=16; } if(cont[plyer][DOWN] == 0){ if (vy[plyer] < 480) vy[plyer]+=16; } } } // "Invincible" powerup - player had the ability // to rebound off of the board walls if(power[plyer] == 2) { if(xun[plyer].highx[1] > width-5){ vx[plyer]= -vx[plyer]; xun[plyer].highx[1] = width -5; } if(xun[plyer].highx[1] < 15){ vx[plyer] = -vx[plyer]; xun[plyer].highx[1] = 15; } if(yun[plyer].highy[1] > 84){ vy[plyer] = -vy[plyer]; yun[plyer].highy[1] = 84; } if(yun[plyer].highy[1] < 15){ vy[plyer] = -vy[plyer]; yun[plyer].highy[1] = 15; } } // Player moves off the right border if(xun[plyer].highx[1] >= width+6){ if(deadtimer[plyer] == 0) { xun[plyer].highx[1] = width+6; vx[plyer] = 0; vy[plyer] = 0; deadtimer[plyer] = 90; } } // Player moves off the left border if(xun[plyer].highx[1] <= 4){ if(deadtimer[plyer] == 0) { xun[plyer].highx[1] = 4; vx[plyer] = 0; vy[plyer] = 0; deadtimer[plyer] = 90; } } // Player moves off the bottom border if(yun[plyer].highy[1] >= 95){ if(deadtimer[plyer] == 0) { yun[plyer].highy[1] = 95; vx[plyer] = 0; vy[plyer] = 0; deadtimer[plyer] = 90; } } // Player moves off the top border if(yun[plyer].highy[1] <= 5){ if(deadtimer[plyer] == 0) { yun[plyer].highy[1] = 5; vx[plyer] = 0; vy[plyer] = 0; deadtimer[plyer] = 90; } } // A player is out for 3 seconds before the player // can re-spawn. The last player to come in contact // with the dead player gains a point. If no player // comes in contact, no point is awarded (suiciding // does not hurt a player). if(deadtimer[plyer] == 90){ if(killer[plyer] == -1){ //if(score[plyer]>0) score[plyer]--; } else score[killer[plyer]]++; killer[plyer] = -1; } if(deadtimer[plyer] > 0 ) { deadtimer[plyer]--; if(deadtimer[plyer] == 0) { xun[plyer].highx[1] = initx[plyer]; yun[plyer].highy[1]=inity[plyer]; vx[plyer] = 0; vy[plyer] = 0; power[plyer] = 0; } } // Determines the velocity change for each player xun[plyer].x+=vx[plyer]; yun[plyer].y+=vy[plyer]; // Invincibility timer is set to 8 seconds if(power[plyer] == 2) { if(invincetimer[plyer] > 0) invincetimer[plyer]--; if(invincetimer[plyer] == 0)power[plyer] = 0; } // Draws the end result for each circle circle_pt(xun[plyer].highx[1],yun[plyer].highy[1],1, plyer+1, power[plyer]); } // for-loop // Code for when a power-up is available on the screen if(poweravail != 0) { // Draws the power-up character on the screen // 'h' = heavy 'i' = invincible video_smallchar(powerx, powery, 19+poweravail); // Determines whether a player moves over a power-up for(plyer = 0; plyer < numplayers; plyer++) { dispx = powerx - xun[plyer].highx[1]; dispy = powery - yun[plyer].highy[1]; dist2 = dispx*dispx + dispy*dispy; if(dist2 <= 25) { power[plyer] = poweravail; if(poweravail == 2) invincetimer[plyer] = 240; poweravail = 0; powertimer = 0; } } } // Code for when no power-up is available. Timer counts down for // ten seconds before the next power-up is available. if(poweravail == 0) { powertimer++; if(powertimer >= 300){ if((rand()&3) == 0) poweravail = 2; else poweravail = 1; } } // Code that detects collisions between two opposing balls // Three cases are considered: // 1. Neither player is heavy // 2. One player is heavy // 3. Both players are heavy for(plyer = 0; plyer < numplayers-1; plyer++) { if(deadtimer[plyer] == 0) for(oplyer = plyer+1; oplyer < numplayers; oplyer++) { if(deadtimer[oplyer] == 0){ dispx = xun[oplyer].highx[1] - xun[plyer].highx[1]; dispy = yun[oplyer].highy[1] - yun[plyer].highy[1]; delvx = vx[oplyer] - vx[plyer]; delvx = delvx/16; delvy = vy[oplyer] - vy[plyer]; delvy = delvy/16; dist2 = dispx*dispx + dispy * dispy; dotp = delvx*dispx + delvy*dispy; if (dist2 <= 81 && dotp < 0) { if(power[plyer] != 1){ if(power[oplyer] != 1){ tempv = vx[plyer]; vx[plyer] = vx[oplyer]; vx[oplyer] = tempv; tempv = vy[plyer]; vy[plyer] = vy[oplyer]; vy[oplyer] = tempv; } else { tempv = vx[plyer]>>1; vx[plyer] = vx[oplyer]<<1; vx[oplyer] = tempv; tempv = vy[plyer]>>1; vy[plyer] = vy[oplyer]<<1; vy[oplyer] = tempv; } } else { if(power[oplyer] != 1){ tempv = vx[plyer]<<1; vx[plyer] = vx[oplyer]>>1; vx[oplyer] = tempv; tempv = vy[plyer]<<1; vy[plyer] = vy[oplyer]>>1; vy[oplyer] = tempv; } else { tempv = vx[plyer]; vx[plyer] = vx[oplyer]; vx[oplyer] = tempv; tempv = vy[plyer]; vy[plyer] = vy[oplyer]; vy[oplyer] = tempv; } } // Killer marks whoever delivered the most // recent collision for scoring purposes. killer[plyer] = oplyer; killer[oplyer] = plyer; } } } } // Determines whether a player has enough points to win if(score[0] == WinningPoint || score[1] == WinningPoint || score[2] == WinningPoint || score[3] == WinningPoint){ GameState=playerdie; } // Displays the score for Player 1 video_putsmalls(12, 4, Play1); video_smallchar(20,4, 10); if(score[0] < 10) { video_smallchar(24,4, score[0]); video_smallchar(28, 4, 12); } else { video_smallchar(24,4, score[0]/10); video_smallchar(28,4, score[0]%10); } // Displays the score for Player 2 video_putsmalls(80, 4, Play2); video_smallchar(88,4, 10); if(score[1] < 10){ video_smallchar(92, 4, score[1]); video_smallchar(96, 4, 12); } else { video_smallchar(92,4, score[1]/10); video_smallchar(96,4, score[1]%10); } // Displays the score for Player 3 (if needed) if(numplayers >= 3 ) { video_putsmalls(12, 95, Play3); video_smallchar(20,95, 10); if(score[2] < 10) { video_smallchar(24,95, score[2]); video_smallchar(28, 95, 12); } else { video_smallchar(24,95, score[2]/10); video_smallchar(28,95, score[2]%10); } } // Displays the score for Player 4 (if needed) if(numplayers == 4 ) { video_putsmalls(80, 95, Play4); video_smallchar(88,95, 10); if(score[3] < 10) { video_smallchar(92,95, score[3]); video_smallchar(96, 95, 12); } else { video_smallchar(92,95, score[3]/10); video_smallchar(96,95, score[3]%10); } } break; // Playerdie is really the game-over screen case playerdie: // Displays the winning player if(score[0] == WinningPoint) video_putsmalls(52, 40, Play1); else if (score[1] == WinningPoint) video_putsmalls(52, 40, Play2); else if (score[2] == WinningPoint) video_putsmalls(52, 40, Play3); else if (score[3] == WinningPoint) video_putsmalls(52, 40, Play4); // Displays the words "win" and "Press any key to continue" video_putsmalls(64, 40, win); video_putsmalls(20, 60, any); // At the game over screen, waits for three seconds before allowing // the players to return back to the title screen. if(timer < 90) timer++; if( (timer == 90) && (PINA != 0xff) ) { for(plyer = 0; plyer < numplayers; plyer++) { circle_pt(xun[plyer].highx[1],yun[plyer].highy[1],0, 12, power[plyer]); video_clearsmall(12, 4, 6); video_clearsmall(80, 4, 6); video_clearsmall(12, 95, 6); video_clearsmall(80, 95, 6); } video_putsmalls(52, 40, clrpnt); video_clearsmall(20, 60, 22); video_smallchar(powerx, powery, 12); initialize(); // resets board } break; } // switch end //line 231 end //while end //main