📄 b04278d216f2001a1a47b6584e8f9250
字号:
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include "alt_types.h"
#include "altera_avalon_dma_regs.h"
#include "altera_avalon_timer_regs.h"
#include "altera_avalon_pio_regs.h"
#include "altera_avalon_dma.h"
#include "sys/alt_irq.h"
#include "system.h"
#include "string.h"
#include "sys/alt_dma.h"
#include <stdio.h>
#include <math.h>
#include "FK.h"
#define VGA_WIDTH 640
#define VGA_HEIGHT 480
#define ASC_SIZE 128
static unsigned char ASC_DATA[ASC_SIZE][16];
static volatile unsigned char * p_asc_flash = (unsigned char *)0x00015000;
static volatile unsigned char * p_hzk_flash = (unsigned char *)0x00060000;
static unsigned int v_nwidth = H_FKN * FK_SIZE;
static unsigned int v_nheight = V_FKN * FK_SIZE;
static unsigned char FK_DATA[V_FKN][H_FKN];
static FK_STRUCT FK_CUR, FK_NEXT;
volatile int edge_capture;
static volatile unsigned char *P_VGA_RAM = (unsigned char *)0x00800000;
const unsigned long TLENGTH = 640 * 480;
const int LEVEL[10] = {2000, 1500, 1000, 800, 600, 500, 400, 300, 200, 100};
const long SCORE[10] = {0,100,300,1000,30000,100000,300000,1000000};
static int G_ROWS,G_SCORE,G_BESTSCORE,G_LEVEL;
static int G_RUNNING;
void load_asc(void);
void draw_pixel(unsigned long x,unsigned long y,char color);
void draw_asc(unsigned int x, unsigned int y, unsigned char asc, unsigned char color);
void draw_hz(unsigned int x,unsigned int y,unsigned char hz[2],char color);
void draw_str(unsigned int x, unsigned int y, unsigned char *str, int len, unsigned char color);
void clear_bk( unsigned long left,
unsigned long top,
unsigned long width,
unsigned long height,
unsigned char color);
void clear_fkdata(void);
void rand_fk(FK_STRUCT *fk);
void rotate_fk(FK_STRUCT *fk, int bT);
void add_cur_fk(FK_STRUCT *fk);
void copy_fk_next();
void move_fk_left();
void move_fk_right();
void move_fk_down();
void move_fk_rotate();
int check_fk(FK_STRUCT *fk);
int clear_full_row();
void draw_cur_fk( unsigned long left,
unsigned long top,
FK_STRUCT *fk,
unsigned char color);
void draw_fk_data( unsigned long left,
unsigned long top,
unsigned char color);
void updata_score(int rows);
void init_fk(void);
void init_dma(void);
void isr_dma(void* context1, alt_u32 id);
#ifdef BUTTON_PIO_BASE
static void button_ISR(void* context, alt_u32 id)
{
volatile int* edge_capture_ptr = (volatile int*) context;
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0);
}
static void init_button_pio()
{
void* edge_capture_ptr = (void*) &edge_capture;
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x0);
alt_irq_register( BUTTON_PIO_IRQ, edge_capture_ptr, button_ISR );
}
#endif
int main(void)
{
#ifdef BUTTON_PIO_BASE
init_button_pio();
#endif
int curvalue,lastvalue =0x0000;
int count = 0;
init_fk();
init`_dma();
while(1)
{
curvalue = edge_capture;
if (curvalue != lastvalue )
{
draw_cur_fk((VGA_WIDTH - v_nwidth) / 2, (VGA_HEIGHT - v_nheight) / 2, &FK_CUR, 0x00);
switch(curvalue & 0x000F)
{
case 0x0001:
if (G_RUNNING) move_fk_left();
break;
case 0x0002:
if (G_RUNNING) move_fk_right();
break;
case 0x0004:
if (G_RUNNING) move_fk_down();
break;
case 0x0008:
if (G_RUNNING) move_fk_rotate();
else
{
G_RUNNING = 1;
clear_bk( (VGA_WIDTH - v_nwidth) / 2 + 1,
(VGA_HEIGHT - v_nheight) / 2 + 1,
VGA_WIDTH - 2, v_nwidth - 2, 0x00);
/*srand(count);*/
}
break;
default:
break;
}
if (G_RUNNING)
draw_cur_fk((VGA_WIDTH - v_nwidth) / 2, (VGA_HEIGHT - v_nheight) / 2, &FK_CUR, 0x00);
usleep(20000);
}
if (G_RUNNING)/*&& ( IORD_ALTERA_AVALON_TIMER_STATUS(TIMER1_BASE) & ALTERA_AVALON_TIMER_STATUS_TO_MSK))*/
{
if(++count >= LEVEL[G_LEVEL])
{
draw_cur_fk((VGA_WIDTH - v_nwidth) / 2, (VGA_HEIGHT - v_nheight) / 2, &FK_CUR, 0x00);
move_fk_down();
draw_cur_fk((VGA_WIDTH - v_nwidth) / 2, (VGA_HEIGHT - v_nheight) / 2, &FK_CUR, 0x02);
count = 0;
}
IOWR_ALTERA_AVALON_TIMER_STATUS(SYS_CLK_TIMER_BASE,0);
}
}
printf ("\nExit fk.\n\004");
};
void drawpixel(unsigned long x,unsigned long y,unsigned char color)
{
unsigned long addr;
if (x > 640)
x %= 640;
if (y > 480)
y %= 480;
addr = 640 * y + x;
P_VGA_RAM[addr]=color;
};
void loadasc()
{
volatile unsigned char *xasc;
int i, j;
for (i = 0; i < ASC_SIZE; ++i)
{
xasc = (unsigned char *) 0x00015000 + i*16;
for(j = 0; j < 16; ++j)
{
ASC_DATA[i][j] = xasc[j];
}
}
};
void drawasc(unsigned int x, unsigned int y, unsigned char asc, unsigned char color)
{
unsigned char ch;
int px, py;
for(py = 0; py < 16; ++py)
{
ch = ASC_DATA[asc][py];
for(px = 0; px < 8; ++px)
{
if(ch & (0x01 << (7 - px)))
drawpixel(x + px, y + py, color);
// else
// drawpixel(x + px, y + i, 0x00);
}
}
};
void drawhz(unsigned int x,unsigned int y,unsigned char hz[2],char color)
{
volatile unsigned char *xhz;
unsigned char ch;
unsigned int dx,dy,i;
xhz = (unsigned char *) + 0x00100000;
xhz = xhz +(((hz[0]-0xa1)*94+(hz[1]-0xa1))*32);
for(dy=0;dy<16;++dy)
{
for(dx=0;dx<2;++dx)
{
ch = xhz[dy*2+dx];
for(i=0;i<8;++i)
{
if(ch&(0x01<<(7-i)))
drawpixel(x+dx*8+i,y+dy,color);
else
drawpixel(x+dx*8+i,y+dy,0);
}
}
}
};
void drawstr(unsigned int x,
unsigned int y,
unsigned char *str, int len, unsigned char color)
{
int i;
for (i = 0; i < len; ++i)
drawasc(x + i * 8, y, str[i], color);
};
void clear_bk( unsigned long left,
unsigned long top,
unsigned long width,
unsigned long height,
unsigned char color)
{
unsigned long addr, i, j;
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= 6; j++)
{
addr = 640 * (top + i) + left + j;
addr %= TLENGTH;
P_VGA_RAM[addr] = color;
}
}
};
void clear_fkdata()
{
int i, j;
for (i = 0; i < V_FKN; ++i)
for (j = 0; j < H_FKN; ++j)
FK_DATA[i][j] = 0;
};
void rand_fk(FK_STRUCT *fk)
{
int i, index;
if (!fk)
return;
index = rand() % 28;
for (i = 0; i < 16; ++i)
fk->data[i/4][i%4] = FK_RAWS[index][i/4][i%4];
fk->index = index;
fk->x = H_FKN / 2 - 2;
fk->y = 0;
for (i = 0; i < 16; ++i)
{
if (fk->data[i/4][i%4] > 0)
break;
}
fk->y -= i/4;
};
void draw_fk_data( unsigned long left,
unsigned long top,
unsigned char color)
{
int i, j;
for (i = 0; i < V_FKN; ++i)
for (j = 0; j < H_FKN; ++j)
{
if (FK_DATA[i][j] > 0)
clear_bk(left + j * FK_SIZE + 2,
top + i * FK_SIZE + 1,
FK_SIZE - 4, FK_SIZE - 2, color);
}
};
void draw_cur_fk( unsigned long left,
unsigned long top,
FK_STRUCT *fk,
unsigned char color)
{
int i, j, px, py;
if (!fk)
return;
for (i = 0; i < 4; ++i)
{
py = fk->y + i;
for (j = 0; j < 4; ++j)
{
px = fk->x + j;
if (fk->data[i][j] > 0 && px >= 0 && px < H_FKN && py >= 0 && py < V_FKN)
clear_bk(left + px * FK_SIZE + 2,
top + py * FK_SIZE + 1,
FK_SIZE - 4, FK_SIZE - 2, color);
}
}
};
void rotate_fk(FK_STRUCT *fk, int bT)
{
int i, index = fk->index;
if (!fk)
return;
if (bT > 0)
index = ((index >> 2) << 2) + (((index % 4) + 1) % 4);
else
index = ((index >> 2) << 2) + (((index % 4) + 3) % 4);
for (i = 0; i < 16; ++i)
fk->data[i/4][i%4] = FK_RAWS[index][i/4][i%4];
fk->index = index;
for (i = 0; i < 16; ++i)
{
if (fk->data[i%4][i/4] > 0 )
break;
}
if (FK_CUR.x + i/4 < 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -