⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clean.c

📁 Run Pac-man Game Based on 8086/8088 FPGA IP Core
💻 C
📖 第 1 页 / 共 2 页
字号:
#define COM1 0x03F8
#define COMPORT	COM1
#define STATUS 1
#define TX_EMPTY 2
#define DATAREG 0

#define VGA_MEM 0xA000
#define VGAWIDTH 320
#define VGAHEIGHT 200
#define GRAPHCHARX 16	/*width of icon */
#define GRAPHCHARY 10	/*higth of icon */
#define MAXCURX VGAWIDTH/GRAPHCHARX
#define MAXCURY VGAHEIGHT/GRAPHCHARY

/**************Graphic and char***************/
int myrandom;

/*pixel map, 8 bit perpixel */
char space[]={
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};

char monsterbitmap[]={
	 0,0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,
	 0,0,0,0,0,0,2,0,0,0,0,2,2,0,0,0,
	 0,0,0,0,0,2,0,5,5,0,5,5,0,2,0,0,
	 0,0,0,0,2,0,0,0,0,0,0,0,0,0,2,0,
	 0,0,0,0,2,0,0,0,0,0,0,0,0,0,2,0,
	 0,0,0,0,2,0,2,2,2,2,2,2,2,0,0,0,
	 0,0,0,0,0,2,2,2,2,2,2,2,0,0,0,0,
	 0,0,0,0,0,0,2,2,2,2,2,2,0,0,0,0,
	 0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,
	 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};

char score[]={
	 1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,
	 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,
	 1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,
	 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,
	 1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,
	 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
	 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};

enum graphcharid{
	SPACEID,
	MONSTERID,
	MANID,
	WALLID,
	FOODID,
	SCOREID,
	GRAPHCHAR_BUTT,
};

char *bitmaptable[GRAPHCHAR_BUTT]={space,monsterbitmap,man,wall,food,score};


/*uart functions*/
void uart_putc(char x)
{
	asm		MOV		DX,COMPORT+STATUS;
WAITTX:
	asm	{
			CLD;
			IN		AL,DX;					/*Read status*/
			AND		AL,TX_EMPTY;				/*Transmit Register Empty?*/
			JZ		WAITTX;				/*No, wait*/
			MOV		DX,COMPORT+DATAREG;	/*Point to data port*/
			MOV		AL,x;
			OUT		DX,AL;
		}
}
void uart_puts(char * str)
{
	while((*str)!=0) uart_putc(*(str++));
}

void uart_putv(int val)
{
	uart_putc('0'+(val>>12));
	uart_putc('0'+((val>>8)&0x0f));
	uart_putc('0'+((val>>4)&0x0f));
	uart_putc('0'+(val&0x0f));
}

/* a dummy blocked key read for spartan3E*/
char getch()
{
 	char keyin;
	asm {
			MOV ax,0x0400;
			MOV es,ax;
			XOR ax,ax;
			MOV di,ax;
		}
WAITIN:
	asm {
			MOV al,byte ptr es:[di];
			AND al,0x0f;
			JZ	WAITIN;			
			MOV keyin,al;
		}
	//uart_puts("inside getch!\n");
	return keyin;
}

/*plot functions*/
void plot(int x, int y, unsigned char color)
{
	int memoffset;
	unsigned char vgabyte;
	memoffset=160*y+(x>>1);
	/*vgabyte=VGA[memoffset]*/
	asm {
			MOV ax,VGA_MEM;
			MOV es,ax;
			MOV ax,memoffset;
			MOV di,ax;
			MOV al,byte ptr es:[di];
			MOV vgabyte,al;
		}
	if(x%2)
		vgabyte=(vgabyte&0xf0)|(color&0x0f);
	else
		vgabyte=(vgabyte&0x0f)|(color<<4);
	/*VGA[memoffset]=vgabyte;*/
	asm {
			MOV ax,VGA_MEM;
			MOV es,ax;
			MOV ax,memoffset;
			MOV di,ax;
			MOV al,vgabyte;
			STOSB;
		}
}


abs(int a)
{
	if(a<0) a=-a;
	return a;
};

void writef(int col, int row, char charid)
{
	int i,j,x,y;
	char * bp;
	//uart_puts("inside writef\n");
	bp=bitmaptable[charid];
	for(i=0;i<GRAPHCHARX;i++)
		for(j=0;j<GRAPHCHARY;j++)
		{
			x=col*GRAPHCHARX;
			y=row*GRAPHCHARY;
			plot(x+i,y+j,*(bp+j*GRAPHCHARX+i));
		}
	//uart_puts("writef...done!\n");
}

/*PACMAN part, algorithem only*/
/*Constants used to adjust game definition*/
#define WIDTH 20
#define HEIGHT 20
#define GHOSTCOUNT 4
#define MAXROUND 3

int foodall;

/*constants for movement directions*/
#define STAY 0
#define UP 1
#define RIGHT 2
#define DOWN 3
#define LEFT 4

/*A simple ordered pair data type*/
typedef struct {
	char x;
	char y;
} ORDPAIR;

/****************************************************************/
/* Board data structures*/
/****************************************************************/

/* the contents of a single location on the board */
typedef struct {
	char wall : 1;
	char food : 1;
} BOARDLOC;

/* constants to indicate the status of a board */
enum gamestatus {
	PLAYING,
	ROUND_OVER,
	GAME_OVER,
};

/* a complete playing field */
typedef struct {
	char status;							/*status of the board*/
	int score;							/*score of player*/
	int food_count;						/*remaining food count*/
	char round;							/*current round number*/
	ORDPAIR player_pos;					/*position of the player*/
	ORDPAIR ghost_pos[GHOSTCOUNT];		/*position of ghosts*/
	BOARDLOC board_data[WIDTH][HEIGHT];/*the layout of the board*/
} BOARD;

char stop;
BOARD myboard;

/* the default board layout (# is wall, . is food, <space> is neither) */
char *default_board[] = {
  "####################",
  "#.....########.....#",
  "#.###..........###.#",
  "#.#.#..##..##..#.#.#",
  "#.#......##......#.#",
  "#...####.##.####...#",
  "#.#....######....#.#",
  "#.#.##.#    #.##.#.#",
  "#.#....##  ##....#.#",
  "#...####....####...#",
  "#.#......##......#.#",
  "#.#...#......#...#.#",
  "#.###.###..###.###.#",
  "#......#...#....#..#",
  "#..................#",
  "#..................#",
  "####################",
};

/* Get a random nonnegative integer less than n */
get_rand(int n)
{
	int toffset;
	char temp;
	myrandom++;
	/*temp=VGA[myrandom%100]*/
	toffset=myrandom%100;
	asm {
			MOV ax,VGA_MEM;
			MOV es,ax;
			MOV ax,toffset;
			MOV di,ax;
			MOV al,byte ptr es:[di];
			MOV temp,al;
		}
	//uart_puts("get_rand...done!\n");
	return (myrandom+temp)%n;
}


/* quickly constructs an ordered pair */
void op(char x, char y, ORDPAIR * r)
{
	r->x = x;
	r->y = y;
}

/* component sum of two ordered pairs */
void op_sum(ORDPAIR * op1, ORDPAIR * op2, ORDPAIR * ret)
{
	//uart_puts("op_sum\n");
	ret->x = op1->x + op2->x;
	ret->y = op1->y + op2->y;
}

/* test ality of two ordered pairs */
op_eq(ORDPAIR * op1, ORDPAIR * op2)
{
	//uart_puts("op_eq\n");
	return (op1->x == op2->x) && (op1->y == op2->y);
}

/* return an ordered pair that points in the given direction */
void op_from_dir(char direction,ORDPAIR *r)
{
	r->x=0;r->y=0;
	if(direction ==UP) {r->x=0;r->y=-1;}
	else if(direction ==RIGHT) {r->x=1;r->y=0;}
	else if(direction ==DOWN) {r->x=0;r->y=1;}
	else if(direction ==LEFT) {r->x=-1;r->y=0;}
	//uart_puts("op_from_dir...done!\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -