Code

 

 

//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 <Mega32.h>  

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <delay.h>  

//cycles = 63.625 * 16 Note NTSC is 63.55

//but this line duration makes each frame exactly 1/60 sec

//which is nice for keeping a realtime clock

#define lineTime 1018

 

#define begin {

#define end   }

#define ScreenTop 30

#define ScreenBot 230

#define T0reload 256-60

//keypad

#define maxkeys 16

               

//new code

#define XMAX  106 // maximum x value of the screen

#define YMAX  99 // maximum y value of the screen

#define maxAst  4 // maximum number of asteroids on the screen

#define astRate  4  // speed of asteroids (larger the value, slower the speed)

#define monRate  2 // speed of monsters (larger the value, slower the speed)

#define astFreq  10 // frequency of the appearance of asteroids

    // the bigger, the less frequent

#define monFreq  200 // frequency of the appearance of monsters

#define maxMon  3 // maximum number of monsters on the screen

#define maxAmmo  3 // maximum number of ammos on the screen

#define ammoSpeed 0 // speed of an ammo

#define initLife  3 // number of lives user have

// State constants

// SqState

#define SQUARE 0

#define DIAMOND 1

// gameState

#define RUNNING 0

#define DEAD 1

#define GAMEOVER1 2

#define GAMEOVER2 3

#define GAMEOVER3 4

#define REV1 5

#define REV2 6

#define REV3 7

#define CALIB1 8

#define CALIB2 9

unsigned char key, butnum;  

                                                       

//revised version:

//1 - 9, 0, A, B, C, D, *, #       

flash unsigned char keytbl[16]={0xee, 0xde, 0xbe, 0xed, 0xdd, 0xbd,

0xeb, 0xdb, 0xbb, 0xd7, 0x7e, 0x7d, 0x7b, 0x77, 0xe7, 0xb7};

 

//NOTE that v1 to v8 and i must be in registers! 

register char v1 @4;

register char v2 @5;

register char v3 @6;

register char v4 @7;

register char v5 @8;

register char v6 @9;

register char v7 @10;

register char v8 @11;

register int i @12;

#pragma regalloc+

char syncON, syncOFF;

int LineCount;

int time;

//animation

char x, y, vx, vy;

//Point plot lookup table

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

char screen[1600], t; 

//new variables

// specifies whether or not the fighter is moving towards the x or y

// direction

bit xmotion, ymotion;

// specifies which direction the fighter is moving

bit    xincr, yincr;

// variables for specifying the speed of the fighter

unsigned char   xcntmax, ycntmax;

// counting variable

unsigned char xcnt, ycnt;

// current x and y position of the fighter

unsigned char xpos, ypos;

// State variables

char randomState;

char inputState;

char gameState;   

// seed of rand()

char seed;   

// random variables

unsigned int random, random2, random3, random4;  

// x and y position for array of asteroids

char xast[maxAst], yast[maxAst];

// whether each asteroid is present or not

char  astPresent[maxAst];

// counter variables to help generating asteroids

char astCnt;

char astMoveCnt[maxAst];

// variables used for calculation                                                

char vmon, vstone, vchar;

bit monflag;

// counter variables to help generating monsters

char monMoveCnt[maxMon];

char monPresent[maxMon];

// position of monsters

char xmon[maxMon], ymon[maxMon];

// movement directions of monsters

char xmonincr[maxMon], ymonincr[maxMon];

// positions for ammos

unsigned char xammo[maxAmmo], yammo[maxAmmo];

// indicate the press of button 'D'

bit shoot;

// to let the user to recognize the button only once for continuous pushing

bit buttonPress; 

char ammoPresent[maxAmmo];

// indicate the next ammo available

char nextAmmo;

// the voltage got from ADC

unsigned int Ain;

// x and y voltage

char yvolt;

char xvolt;

                  

//score

unsigned int score;

unsigned int tempscore;

unsigned int temprmd;

 

// string variable

char teststr[10];

// variables for generating the series of squares and diamonds after the fighter

unsigned int sqWidth;

int xmin, ymin, xmax, ymax;

bit sqState;

int a;

int t1;                                                                  

// the counter which increments every frame, used to enable alternate movement of monsters or asteroids

char univCnt;   

// no. of lives

char life;

// calibration variables, to specify the voltage level for each direction movement

char LEFT1, LEFT2,  RIGHT1, RIGHT2, UP1, UP2, DOWN1, DOWN2;

char calibState;                                                                 

// voltage level when the device is in horizontal position.

char xstdvolt, ystdvolt;

 

//fighter

//5X4

flash char fighter[2][7]={

   //normal      

   0b00000000,

   0b00010000,

   0b00111000,

   0b00111000,

   0b01111100,

   0b00010000,

   0b00000000,

 

   //explode

   0b00000000,

   0b00010000,

   0b00111000,

   0b01111100,

   0b00010000,

   0b00010000,

   0b01010000};

/*

//monster pattern

 

*/

flash char mon[2][8]={

   //type I

   0b11111111,

   0b10011001,

   0b10011001,

   0b11111111,

   0b10000001,

   0b10000001,

   0b11000011,

   0b11111111,

 

   //type II

   0b11111111,

   0b11111111,

   0b11111111,

   0b11111111,

   0b11111111,

   0b11111111,

   0b11111111,

   0b11111111};

 

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