//ECE 476 Final Project - Battle Tank //Matthew Ito (mmi4), Jsoon Kim (jk459) //======================================================= //Implemented a 3d-vector game //Not a general implementation, unfortunately. Will need significant tweaking //before logic can be used in other games. // //Lot of leftover code attached inside, all commented //======================================================= //video gen and sound //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor //B.3 is sound and should have a 10k resistor to gnd #pragma regalloc- //I allocate the registers myself #pragma optsize- //optimize for speed #include #include #include #include #include #include #include "flashstuff.h" //contains the necessary flash tables //fixed point conversion macros #define int2fix(a) (((int)(a))<<8) //Convert char to fix. a is a char #define fix2int(a) ((signed char)((a)>>8)) //Convert fix to char. a is an int #define float2fix(a) ((int)((a)*256.0)) //Convert float to fix. a is a float #define fix2float(a) ((float)(a)/256.0) //Convert fix to float. a is an int //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 50 #define ScreenBot 150 //pyramid height constant (in 8x8 fixed format)- #define pyramidheight 768 //target sizing - size from origin point #define targetsize 128 //scales the minimap appropriately #define scale 9 //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; char screen[1600]; //t, ts[10], tf[10]; //push button virtual timer unsigned char frametime; //index variable int j; //linear congruential generators and initial seed values int rannum1 = 0xDEADBEEF; int rannum2 = 0xBAADF00D; //spawn into different quadrants int quadrant; //basic AI unsigned int AI; //start game indicator unsigned char startflag; //death indicator unsigned char deathflag; //draw screen flag unsigned char drawscreen; //pyramid orientation unsigned char porientation; //bullet orientation unsigned char borientation; //target orientation unsigned char torientation; //buffers for the 3 variables describing pyramid, tank, bullet, etc. unsigned char torientationbuffer; unsigned char borientationbuffer; unsigned char porientationbuffer; //Point plot lookup table //One bit masks flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //temp char array char val[10]; //captions char score_cap[]="SCORE"; char ammo_cap[]="AMMO"; char gameover_cap[]="GAMEOVER"; //coordinates //pyramid int pyramid[3] = {0,0,0}; int pyramidfuture[3] = {7<<8,0,10<<8}; //your bullet int bullet[3] = {0,0,0}; int bulletfuture[3] = {3,3,5<<8}; //target location int target[3] = {0,0,0}; int targetfuture[3] = {0,0,1280}; //each bit represents whether an object is enabled or not char enabled; // ammunition counter char ammo; // score counter int score; // AI constants unsigned int threshold; unsigned char respawnflag; // variable transforms int transformB[8] = {256,0,0,0,0,0,256,0}; //bullet transform int transformT[8] = {256,0,0,0,0,0,256,0};//target transform //preset transforms flash int t_forward[8] = {256,0,0,0,0,0,256,-256}; flash int t_rotateLeft[8] = {256,0,-22,0,22,0,256,0}; flash int t_rotateRight[8] = {256,0,23,0,-23,0,256,0}; flash int t_backward[8] = {256,0,0,0,0,0,256,256}; flash int t_forwardSlow[8] = {256,0,0,0,0,0,256,-64}; //sine and cosine tables flash int cosine[72] = {257,256,253, 248,241,233, 222,210,197, 182,165,147, 128,109,88, 67,45,23, 0,-24,-46, -68,-89,-110, -130,-148,-166, -183,-198,-211, -223,-234,-242, -249,-254,-257, -257,-257,-254, -249,-242,-234, -223,-211,-198, -183,-166,-148, -129,-110,-89, -68,-46,-24, -1,22,44, 66,87,108, 127,146,164, 181,196,209, 221,232,240, 247,252,255}; flash int sine[72] ={0,22,44,66,87,108,128,146,164,181,196,209,221,232,240,247,252,255,256, 255,252,247,240,232,221,209,196,181,164,146,128,108,87,66,44,22,0,-23,-45,-67,-88,-109, -129,-147,-165,-182,-197,-210,-222,-233,-241,-248,-253,-256,-256,-256,-253,-248,-241,-233, -222,-210,-197,-182,-165,-147,-128,-109,-88,-67,-45,-23}; //================================ //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, //[ (= -) 0b00000000, 0b00000000, 0b11110000, 0b00000000, 0b00000000 }; void initialize(void); //================================== //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 && drawscreen) begin //compute byte index for beginning of the next line //left-shift 4 would be individual lines //i=(LineCount-ScreenTop)<<3 & 0xfff8; // #asm push r16 lds r12, _LineCount lds r13, _Linecount+1 ldi r16, 50 ;screenbottom sub r12, r16 ldi r16,0 sbc r13, r16 lsl r12 rol r13 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(8); //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 ;IN R30,0x12 BST @0,7 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 IN R30,0x12 ; set up r30 with PORTD 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+ //==================================== //fixed point multiply //==================================== int fixmult(int a, int b) begin #asm mov r24,r20 mov r25,r21 ldd R22,Y+2 ;load a ldd R23,Y+3 ld r20,Y ;load b ldd r21,Y+1 muls r23, r21 ; (signed)ah * (signed)bh mov r31, r0 ; mul r22, r20 ; al * bl mov r30, r1 ; mulsu r23, r20 ; (signed)ah * bl add r30, r0 ; adc r31, r1 ; mulsu r21, r22 ; (signed)bh * al add r30, r0 ; adc r31, r1 ; mov r20,r24 mov r21,r25 #endasm end //================================== //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 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 //=== fixed point divide ===================================================== int divfix(int nn, int dd) begin signed char count, neg ; //r16, r17 int x, d ; //r18:19, r20:21 int n ; d=dd ; n=nn ; #asm clr r16 ;count = 0; clr r17 ;neg = 0; ;only works with + numbers ;if (d & 0x8000) ;begin ; neg = 1; ; d = -d ; ;end tst r21 ;d negative? brpl Dnotneg ldi r17, 1 ; neg = 1; negative flag clr r18 ; d = -d ; clr r19 sub r18, r20 sbc r19, r21 mov r20, r18 mov r21, r19 Dnotneg: #endasm // range reduction // bigger than one while (d>0x0100) begin --count ; d >>= 1 ; end // less than 1/2 while (d<0x0080) begin ++count ; d <<= 1 ; end // Newton interation //x = 0x02ea - (d<<1) ; //x = multfix(x, 0x0201 + ~multfix(d,x)); //x = multfix(x, 0x0200-multfix(d,x)); #asm ; form initial x ldi r22, 0xea ;load the constant 2.914 ldi r23, 0x02 mov r24, r20 ; copy d mov r25, r21 lsl r24 ; and form 2*d rol r25 sub r22, r24 ; form 2.914-2*d sbc r23, r25 mov r18, r22 ; store x mov r19, r23 ;mov r22, r18 ; load x ;mov r23, r19 ;mov r20,r20 ; load d ;mov r21,r21 muls r23, r21 ; (signed)ah * (signed)bh mov r31, r0 ; mul r22, r20 ; al * bl mov r30, r1 ; ;mov r16, r0 mulsu r23, r20 ; (signed)ah * bl add r30, r0 ; adc r31, r1 ; mulsu r21, r22 ; (signed)bh * al add r30, r0 ; adc r31, r1 ; com r30 com r31 ldi r24, 0x01 ldi r25, 0x02 add r30, r24 adc r31, r25 mov r22, r18 ;load x mov r23, r19 mov r20,r30 ;load 2-d*x mov r21,r31 muls r23, r21 ; (signed)ah * (signed)bh mov r31, r0 ; mul r22, r20 ; al * bl mov r30, r1 ; ;mov r16, r0 mulsu r23, r20 ; (signed)ah * bl add r30, r0 ; adc r31, r1 ; mulsu r21, r22 ; (signed)bh * al add r30, r0 ; adc r31, r1 ; ; range expansion ; if (count>0) x = x<>(-count) ; tst r16 breq DrecipEnd brmi DrecipNeg lsl r30 rol r31 dec r16 breq DrecipEnd lsl r30 rol r31 dec r16 breq DrecipEnd lsl r30 rol r31 dec r16 breq DrecipEnd lsl r30 rol r31 dec r16 breq DrecipEnd lsl r30 rol r31 dec r16 breq DrecipEnd lsl r30 rol r31 dec r16 breq DrecipEnd lsl r30 rol r31 dec r16 rjmp DrecipEnd DrecipNeg: lsr r31 ror r30 inc r16 breq DrecipEnd lsr r31 ror r30 inc r16 breq DrecipEnd lsr r31 ror r30 inc r16 breq DrecipEnd lsr r31 ror r30 inc r16 breq DrecipEnd lsr r31 ror r30 inc r16 breq DrecipEnd lsr r31 ror r30 inc r16 breq DrecipEnd lsr r31 ror r30 inc r16 breq DrecipEnd lsr r31 ror r30 inc r16 DrecipEnd: ; fix sign ; if (neg==1) x=-x; tst r17 ;neg flag set? breq Drecip1 ;if not, do nothing clr r24 clr r25 sub r24, r30 sbc r25, r31 mov r30, r24 mov r31, r25 Drecip1: ; x = multfix(x,nn) ; mov r22, r30 mov r23, r31 ldd r20,Y+6 ; load n ldd r21,Y+7 muls r23, r21 ; (signed)ah * (signed)bh mov r31, r0 ; mul r22, r20 ; al * bl mov r30, r1 ; ;mov r16, r0 mulsu r23, r20 ; (signed)ah * bl add r30, r0 ; adc r31, r1 ; mulsu r21, r22 ; (signed)bh * al add r30, r0 ; adc r31, r1 ; #endasm end //================================== //plot a line in 3d //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 //in addition, perspective correction performed here, as well as //view clipping void video_line3d(int x1, int y1, int z1, int x2, int y2, int z2, char c) begin int e; signed char dx,dy,j, temp; signed char s1,s2, xchange; signed char x,y; //assure that in view before render if ( (cabs(x1>>8) > cabs(z1>>8)) || (csign(z1>>8) == -1) ) return; if ( (cabs(x2>>8) > cabs(z2>>8)) || (csign(z2>>8) == -1) ) return; //multiply by 2^(-n) factor "c" z1 >>= 6; z2 >>= 6; //convert into integer format and scale x1 = 64+fix2int(divfix(x1,z1)); //y1 = 50-fix2int(divfix(y1,z1)); y1 = 69-fix2int(divfix(y1,z1)); x2 = 64+fix2int(divfix(x2,z2)); //y2 = 50-fix2int(divfix(y2,z2)); y2 = 69-fix2int(divfix(y2,z2)); //check for out of screen, abort if found if(x1>=0 && x1<120 && x2>=0 && x2<120 && y1>=20 && y1<80 && y2>=20 && y2<80) begin 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 end //======================================================= //non-"3d" line generation code //original stuff without internal perspective correction //run as part of a test to put verticies //into memory as more of a render target, to avoid //having to store the Z coordinates along with the rest //of the data //======================================================= void video_line(int x1, int y1, int x2, int y2, char c) begin int e; signed char dx,dy,j, temp; signed char s1,s2, xchange; signed char x,y; //convert into integer format and scale x1 = 64+fix2int(x1); y1 = 69-fix2int(y1); x2 = 64+fix2int(x2); y2 = 69-fix2int(y2); //check for out of screen, abort if found if(x1>=0 && x1<128 && x2>=0 && x2<128 && y1>=20 && y1<100 && y2>=20 && y2<100) begin 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 end //============================================================ //Draws the pyramid //takes a base point, an orientation, and a color, draws a pyramid of a set size //Height of pyramid constant = pyramidheight, defined up above //as base on y = 0, cheated on implementation, as not all 4 lines are visible //just draw 2 vectors in order to cut down on function calls //============================================================ void drawpyramid(unsigned char color, int * pyramidp, unsigned char porient) begin unsigned char offset; //exploits symmetry of pyramid, as offsets will be the same for 4 //different instances of 90 degrees apart if(porient > 54) offset = porient - 54; else if(porient > 36) offset = porient - 36; else if(porient > 18) offset = porient - 18; else offset = porient; video_line3d(pyramidp[0]+(cosine[offset]), pyramidp[1], pyramidp[2]+(sine[offset]), pyramidp[0], pyramidheight, pyramidp[2], color); video_line3d(pyramidp[0]+(cosine[offset+18]), pyramidp[1], pyramidp[2]+(sine[offset+18]), pyramidp[0], pyramidheight, pyramidp[2], color); video_line3d(pyramidp[0]+(cosine[offset+36]), pyramidp[1], pyramidp[2]+(sine[offset+36]), pyramidp[0], pyramidheight, pyramidp[2], color); video_line3d(pyramidp[0]+(cosine[offset+54]), pyramidp[1], pyramidp[2]+(sine[offset+54]), pyramidp[0], pyramidheight, pyramidp[2], color); video_line3d(pyramidp[0]+(cosine[offset+18]), pyramidp[1], pyramidp[2]+(sine[offset+18]), pyramidp[0]+(cosine[offset+54]), pyramidp[1], pyramidp[2]+(sine[offset+54]), color); video_line3d(pyramidp[0]+(cosine[offset]), pyramidp[1], pyramidp[2]+(sine[offset]), pyramidp[0]+(cosine[offset+36]), pyramidp[1], pyramidp[2]+(sine[offset+36]), color); end // =============================== // Draws bullet based upon position and orientation void drawbullet(unsigned char color, int * bulletp, unsigned char borient) begin unsigned char offset; //takes advantage of the fact that the bullet looks the //same oriented 180 degrees, simplifies things if(borient > 36) offset = borient - 36; else offset = borient; video_line3d(bulletp[0]+(cosine[offset]>>1), bulletp[1], bulletp[2]+(sine[offset]>>1), bulletp[0]+(cosine[offset+36]>>1), bulletp[1], bulletp[2]+(sine[offset+36]>>1), color); video_line3d(bulletp[0], bulletp[1]-128, bulletp[2], bulletp[0], bulletp[1]+128, bulletp[2], color); end //======================================== //attempted Tank draw algorithm - not used in code, but //implemented to draw a box in space //dropped due to graphical weirdness, as well as //hardware cycle issues //======================================== void drawtank(unsigned char color, int * tankp, unsigned char torient) begin //bottom plane of tank and side lines video_line3d(tankp[0]+(cosine[torient]<<2), -1024, tankp[2]+(sine[torient]<<2), tankp[0]+(cosine[torient]<<2), -1024, tankp[2]-(sine[torient]<<2), color); video_line3d(tankp[0]+(cosine[torient]<<2), -1024, tankp[2]-(sine[torient]<<2), tankp[0]-(cosine[torient]<<2), -1024, tankp[2]-(sine[torient]<<2), color); video_line3d(tankp[0]-(cosine[torient]<<2), -1024, tankp[2]-(sine[torient]<<2), tankp[0]-(cosine[torient]<<2), -1024, tankp[2]+(sine[torient]<<2), color); video_line3d(tankp[0]-(cosine[torient]<<2), -1024, tankp[2]+(sine[torient]<<2), tankp[0]+(cosine[torient]<<2), -1024, tankp[2]+(sine[torient]<<2), color); video_line3d(tankp[0]+(cosine[torient]<<2), -1024, tankp[2]+(sine[torient]<<2), tankp[0]+(cosine[torient]<<2), tankp[1]+1024, tankp[2]+(sine[torient]<<2), color); video_line3d(tankp[0]+(cosine[torient]<<2), tankp[1], tankp[2]-(sine[torient]<<2), tankp[0]+(cosine[torient]<<2), tankp[1]+1024, tankp[2]-(sine[torient]<<2), color); video_line3d(tankp[0]+(cosine[torient]<<2), tankp[1], tankp[2]-(sine[torient]<<2), tankp[0]+(cosine[torient]<<2), tankp[1]+1024, tankp[2]-(sine[torient]<<2), color); video_line3d(tankp[0]-(cosine[torient]<<2), tankp[1], tankp[2]-(sine[torient]<<2), tankp[0]-(cosine[torient]<<2), tankp[1]+1024, tankp[2]-(sine[torient]<<2), color); //upper plane of tank video_line3d(tankp[0]+(cosine[torient]<<2), tankp[1]+1024, tankp[2]-(sine[torient]<<2), tankp[0]-(cosine[torient]<<2), tankp[1]+1024, tankp[2]-(sine[torient]<<2), color); video_line3d(tankp[0]+(cosine[torient]<<2), tankp[1]+1024, tankp[2]+(sine[torient]<<2), tankp[0]-(cosine[torient]<<2), tankp[1]+1024, tankp[2]+(sine[torient]<<2), color); video_line3d(tankp[0]-(cosine[torient]<<2), tankp[1]+1024, tankp[2]+(sine[torient]<<2), tankp[0]-(cosine[torient]<<2), tankp[1]+1024, tankp[2]-(sine[torient]<<2), color); video_line3d(tankp[0]+(cosine[torient]<<2), tankp[1]+1024, tankp[2]-(sine[torient]<<2), tankp[0]+(cosine[torient]<<2), tankp[1]+1024, tankp[2]+(sine[torient]<<2), color); end // ======================== // Draws the target/erases the target // based upon a base point, draws the target // draws the same no matter the orientation, // as the target seen at different orientations should still be visible // in order not to frustrate the player // ======================== void drawtarget(unsigned char color, int * targetp) begin video_line3d(targetp[0]-targetsize, targetp[1], targetp[2], targetp[0], targetp[1]+targetsize, targetp[2], color); video_line3d(targetp[0], targetp[1]+targetsize, targetp[2], targetp[0]+targetsize, targetp[1], targetp[2], color); video_line3d(targetp[0]+targetsize, targetp[1], targetp[2], targetp[0], targetp[1]-targetsize, targetp[2], color); video_line3d(targetp[0], targetp[1]-targetsize, targetp[2], targetp[0]-targetsize, targetp[1], targetp[2], color); end //================================ //transform vertex //transforms a vertex against a stored flash table //transformed vertex is placed at vertex2 pointer location //format is: // transform = [t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11] interpreted as matrix: // |t0 t1 t2 t3| // |t4 t5 t6 t7| // |t8 t9 t10 t11| // | 0 0 0 1| // // Assumptions: // Assume that W is always 1 for this computation // Assume that Y will only be translated, as game only uses X-Z planar transforms void transform(flash int * transform, int * vertex, int * vertex2) begin vertex2[0] = fixmult(vertex[0],transform[0]) + fixmult(vertex[1],transform[1]) + fixmult(vertex[2],transform[2]) + transform[3]; vertex2[1] = vertex[1]; vertex2[2] = fixmult(vertex[0],transform[4]) + fixmult(vertex[1],transform[5]) + fixmult(vertex[2],transform[6]) + transform[7]; end //================================ //transform shape //transforms a vertex against a transform matrix // //Same as function above, except this takes an array in SRAM as a variable void transformV(int * transform, int * vertex) begin unsigned int xtemp,ztemp; xtemp = fixmult(vertex[0],transform[0]) + fixmult(vertex[1],transform[1]) + fixmult(vertex[2],transform[2]) + transform[3]; ztemp = fixmult(vertex[0],transform[4]) + fixmult(vertex[1],transform[5]) + fixmult(vertex[2],transform[6]) + transform[7]; vertex[0] = xtemp; vertex[2] = ztemp; end //================================== //clears screen buffer void clearscreen(void) begin for(j = 0; j< 1600; j++) screen[j] = 0x00; end //================================ //for interrupt decoupling -- if necessary, not used interrupt [TIM1_COMPB] void t1_cmpB(void) begin end //================================== // set up the ports and timers void main(void) begin //init timer 1 to generate sync OCR1A = lineTime; //One NTSC line OCR1B = lineTime-10; //for interrupt decoupling TCCR1B = 9; //full speed; clear-on-match TCCR1A = 0x00; //turn off pwm and oc lines TIMSK = 0x18; //enable interrupt T1A and T1B cmp //init ports DDRD = 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 //initialize synch constants LineCount = 1; syncON = 0b00000000; syncOFF = 0b00100000; //side lines #define width 126 //set push buttons DDRA = 0x03; //setup 7-2 as inputs, lower 2 as outputs frametime = 3; //enable sleep mode MCUCR = 0b10000000; #asm ("sei"); for (j = 0; j < 1600; j++) screen[j] = titlescreen[j]; //initialize drawscreen flag drawscreen = 1; //initialize title screen startflag = 0; //set death to zero deathflag = 0; //set enemy respawn to zero respawnflag = 0; //initialize objects enabled enabled = 0b00000101; //set pyramid orientation to zero porientation = 0; //set portA output to zero PORTA = 0x00; //init ammo counter ammo = 10; //reset score score = 0; //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"); //Function which prints title screen if not starting if(LineCount==151 && !startflag) begin if (frametime > 0) frametime--; //decrements counter for flag animation if (frametime == 0) begin frametime = 3; if (j++ >= 5) j = 0; //changes flag frame in flash //if start button pressed, clear screen and print captions if(PINA.2) begin startflag = 1; clearscreen(); //clear screen deathflag = 0; //print captions video_putsmalls(20,2,score_cap); video_putsmalls(92,2,ammo_cap); LineCount++; initialize(); //draw minimap center video_pt(64,10,1); end else //otherwise, keep animating flag begin screen[1172] = anim[j][0]; screen[1173] = anim[j][1]; screen[1188] = anim[j][2]; screen[1189] = anim[j][3]; screen[1204] = anim[j][4]; screen[1205] = anim[j][5]; screen[1220] = anim[j][6]; screen[1221] = anim[j][7]; screen[1236] = anim[j][8]; screen[1237] = anim[j][9]; screen[1252] = anim[j][10]; screen[1253] = anim[j][11]; end end end //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==151 && startflag) begin if (frametime > 0) frametime--; //================================================= // Stuff to do on first frame //================================================= if (frametime == 2 && !deathflag) //on 2, do necessary calculations and place into buffer begin //=============update previous variables========== borientation = borientationbuffer; porientation = porientationbuffer; torientation = torientationbuffer; bullet[0] = bulletfuture[0]; bullet[1] = bulletfuture[1]; bullet[2] = bulletfuture[2]; target[0] = targetfuture[0]; target[1] = targetfuture[1]; target[2] = targetfuture[2]; pyramid[0] = pyramidfuture[0]; pyramid[1] = pyramidfuture[1]; pyramid[2] = pyramidfuture[2]; //==============new bullet============== if(PINA.3 && !(enabled&0x02) && (ammo != 0)) begin bulletfuture[0] = 0; bulletfuture[1] = 256; bulletfuture[2] = 256; enabled |= 0x02; borientationbuffer = 0; ammo--; end //==============process button movement============== //note - prioritizes - forward, back, left, right if(PINA.7) begin if(enabled&0x02)transform(t_forward,bullet,bulletfuture); if(enabled&0x01)transform(t_forward,pyramid,pyramidfuture); if(enabled&0x04)transform(t_forward,target,targetfuture); end else if(PINA.5) begin if(enabled&0x02)transform(t_rotateLeft,bullet,bulletfuture); if(enabled&0x01)transform(t_rotateLeft,pyramid,pyramidfuture); if(enabled&0x04)transform(t_rotateLeft,target,targetfuture); borientationbuffer = 1 + borientation; porientationbuffer = 1 + borientation; torientationbuffer = 1 + borientation; if(borientationbuffer==72) borientationbuffer=0; if(porientationbuffer==72) porientationbuffer=0; if(torientationbuffer==72) torientationbuffer=0; end else if(PINA.4) begin if(enabled&0x02)transform(t_rotateRight,bullet,bulletfuture); if(enabled&0x01)transform(t_rotateRight,pyramid,pyramidfuture); if(enabled&0x04)transform(t_rotateRight,target,targetfuture); borientationbuffer = borientation - 1; porientationbuffer = borientation - 1; torientationbuffer = borientation - 1; if(borientationbuffer==-1)borientationbuffer=71; if(porientationbuffer==-1)porientationbuffer=71; if(torientationbuffer==-1)torientationbuffer=71; end else if(PINA.6) begin if(enabled&0x02)transform(t_backward,bullet,bulletfuture); if(enabled&0x01)transform(t_backward,pyramid,pyramidfuture); if(enabled&0x04)transform(t_backward,target,targetfuture); end //==============collision detect============== //collision between pyramid and bullet //calculates dx,dz, then shifts to get whole value. If value < 2, //collision. if( (enabled & 0x02) && cabs((pyramidfuture[0] - bulletfuture[0]) >> 8) < 2 && cabs((pyramidfuture[2] - bulletfuture[2]) >> 8) < 2 ) begin enabled = enabled&0b11111101; //disable bullet if( bullet[2] > 50 ) //check to see if bullet in front of tank drawbullet(0,bullet,borientation); //erase bullet, as disabled will disable erase end //collision between bullet and target if( (enabled & 0x04) && (enabled & 0x02) && cabs( (targetfuture[0] - bulletfuture[0])>>8) < 2 && cabs((targetfuture[2] - bulletfuture[2])>>8) < 2 ) begin ammo++; //increment ammo score++; //increment score //disable bullet and target marker enabled = (enabled & 0b00000001); if ( bullet[2] > 10) begin drawtarget(0,target); drawbullet(0,bullet,borientation); //clear minimap of target and bullet end //erase target and bullet from minimap video_pt((target[0]>>scale)+64,-(target[2]>>scale)+10,0); video_pt((bullet[0]>>scale)+64,-(bullet[2]>>scale)+10,0); video_pt((bulletfuture[0]>>scale)+64,-(bulletfuture[2]>>scale)+10,0); video_pt((targetfuture[0]>>scale)+64,-(targetfuture[2]>>scale)+10,0); respawnflag = 1; // prep for respawn PORTA = 0x02; //send cue to second MCU for "target neutralized" end end //frametime == 2 //================================================ //stuff to do on second frame //================================================ if(frametime == 1 && !deathflag) begin //check for collision between pyramid and player, target and player //if either, roll back coordinate transforms if( ((enabled & 0x01) && cabs((pyramidfuture[0]) >> 8) < 4 && cabs((pyramidfuture[2]) >> 8) < 4 ) || ((enabled & 0x04) && cabs(targetfuture[0]>>8) < 2 && cabs(targetfuture[2]>>8) < 2 ) ) begin borientationbuffer = borientation; porientationbuffer = porientation; torientationbuffer = torientation; bulletfuture[0] = bullet[0]; bulletfuture[1] = bullet[1]; bulletfuture[2] = bullet[2]; targetfuture[0] = target[0]; targetfuture[1] = target[1]; targetfuture[2] = target[2]; pyramidfuture[0] = pyramid[0]; pyramidfuture[1] = pyramid[1]; pyramidfuture[2] = pyramid[2]; end //================Move Stuff================== //bullet action - propagates bullet if not out of bounds if((enabled&0x02)) begin transformB[3] = -sine[borientationbuffer]; transformB[7] = cosine[borientationbuffer]; if(enabled&0x02) begin transformV(transformB,bulletfuture); end end //==============out of bounds detect============== //checks if bullet out of bounds if((enabled&0x02) && ( cabs((bulletfuture[0] + bulletfuture[2])>>8) > 20)) begin enabled = enabled&0b11111101; if (bulletfuture[2] > bulletfuture[0]) begin drawbullet(0,bullet,borientation); //erase bullet from view, as disabling bullet will disable bullet erase end video_pt((bullet[0]>>scale)+64,-(bullet[2]>>scale)+10,0); // ALWAYS erase minimap, as disabling will disable minimap erase end /*if((enabled&0x01) && ((pyramidfuture[0] + pyramidfuture[2]) > 6400)) begin if (pyramidfuture[2] > pyramidfuture[0]) begin drawpyramid(0,pyramid,porientation); end end */ //========================================= //ALL-NON VECTOR GENERATION CODE INVOLVING RASTER //========================================= //=============redraw minimap main tank============ //redraws in case of erasure video_pt(64,10,1); //==============update text============== sprintf(val,"%06d",score); video_putsmalls(20,10,val); sprintf(val,"%04d",ammo); video_putsmalls(92,10,val); //===================Respawn===================== //respawn targets based upon timer if(respawnflag) begin respawnflag++; if (respawnflag == 10) //split up computation work over several frames begin rannum1 = rand(); end if (respawnflag == 15) begin rannum2 = rand(); end if (respawnflag == 25) //if respawn flag = 25 (i.e. 25 frames) it respawns target begin PORTA = 0x01; respawnflag == 0; enabled = enabled | 0x04; quadrant = rand(); targetfuture[1] = targetsize; //basic implementation - fix if needed //sends target to 4 quadrants, at some point in the quadrant if(quadrant < 8000) begin targetfuture[0] = rannum1>>4; targetfuture[2] = rannum2>>4; end else if (quadrant < 16000) begin targetfuture[0] = -rannum1>>4; targetfuture[2] = rannum2>>4; end else if (quadrant < 24000) begin targetfuture[0] = rannum1>>4; targetfuture[2] = -rannum2>>4; end else begin targetfuture[0] = -rannum1>>4; targetfuture[2] = -rannum2>>4; end end end //basic AI - if rand() > 22000, //move a little if(enabled & 0x04) begin AI = rand(); if( AI > 22000 ) begin targetfuture[0] -= 64; targetfuture[2] += 64; end if( AI > 26000 ) begin targetfuture[0] += 64; targetfuture[2] -= 64; end end //==============minimap erase============== if(enabled&0x04) video_pt((target[0]>>scale)+64,-(target[2]>>scale)+10,0); if(enabled&0x02) video_pt((bullet[0]>>scale)+64,-(bullet[2]>>scale)+10,0); //==============minimap draw============== if(enabled&0x04) video_pt((targetfuture[0]>>scale)+64,-(targetfuture[2]>>scale)+10,1); if(enabled&0x02) video_pt((bulletfuture[0]>>scale)+64,-(bulletfuture[2]>>scale)+10,1); end //frametime == 1 //========================================= //GENERATE RASTER HERE: //on zero, erase screen and generate raster //based on variables calculated during frametime == 2 if (frametime == 0 && (ammo != 0)) begin frametime = 4; drawscreen = 0; //==============erase objects============== if(enabled&0x01) drawpyramid(0,pyramid,porientation); if(enabled&0x02) drawbullet(0,bullet,borientation); if(enabled&0x04) drawtarget(0,target); //==============draw horizon============== screen[1056]=0xff; screen[1057]=0xff; screen[1058]=0xff; screen[1059]=0xff; screen[1060]=0xff; screen[1061]=0xff; screen[1062]=0xff; screen[1063]=0xff; screen[1064]=0xff; screen[1065]=0xff; screen[1066]=0xff; screen[1067]=0xff; screen[1068]=0xff; screen[1069]=0xff; screen[1070]=0xff; screen[1071]=0xff; //============draw crosshair============ screen[823] = 0xff; screen[824] = 0xff; screen[839] |= 0x80; screen[840] |= 0x01; screen[855] |= 0x80; screen[856] |= 0x01; screen[1047] |= 0x80; screen[1048] |= 0x01; screen[1063] |= 0x80; screen[1064] |= 0x01; screen[1079] = 0xff; screen[1080] = 0xff; //==============draw objects============== if(enabled&0x01) drawpyramid(1,pyramidfuture,porientationbuffer); if(enabled&0x02) drawbullet(1,bulletfuture,borientationbuffer); if(enabled&0x04) drawtarget(1,targetfuture); drawscreen = 1; end //frametime == 0 if( (frametime == 0) && (ammo == 0)) //if no ammo, end game begin enabled = 0x00; clearscreen(); video_putsmalls(48,40,gameover_cap); drawscreen = 1; for (j = 0; j < 1600; j++) screen[j] = titlescreen[j]; startflag = 0; end end //linecount == 151 end //while end //main void initialize(void) begin //reset settings //pyramid location pyramid[0] = 0; pyramid[1] = 0; pyramid[2] = 0; pyramidfuture[0] = 7<<8; pyramidfuture[1] = 0; pyramidfuture[2] = 10<<8; //your bullet bullet[0] = 0; bullet[1] = 0; bullet[2] = 0; bulletfuture[0] = 3; bulletfuture[1] = 3; bulletfuture[2] = 5<<8; //target location target[0] = 0; target[1] = 0; target[2] = 0; targetfuture[0] = 0; targetfuture[1] = 128; targetfuture[2] = 1280; //ammo, score, etc. ammo = 10; score = 0; enabled = 0x05; end