/************************************************************************* * Copyright (c) 2004 Altera Corporation, San Jose, California, USA. * * All rights reserved. All use of this software and documentation is * * subject to the License Agreement located at the end of this file below.* ************************************************************************** * Description: * * The following is a simple hello world program running MicroC/OS-II.The * * purpose of the design is to be a very simple application that just * * demonstrates MicroC/OS-II running on NIOS II.The design doesn't account* * for issues such as checking system call return codes. etc. * * * * Requirements: * * -Supported Example Hardware Platforms * * Standard * * Full Featured * * Low Cost * * -Supported Development Boards * * Nios II Development Board, Stratix II Edition * * Nios Development Board, Stratix Professional Edition * * Nios Development Board, Stratix Edition * * Nios Development Board, Cyclone Edition * * -System Library Settings * * RTOS Type - MicroC/OS-II * * Periodic System Timer * * -Know Issues * * If this design is run on the ISS, terminal output will take several* * minutes per iteration. * **************************************************************************/ #include #include "includes.h" #include #include "io.h" /* Definition of Task Stacks */ #define TASK_STACKSIZE 2048 OS_STK task1_stk[TASK_STACKSIZE]; OS_STK task2_stk[TASK_STACKSIZE]; OS_STK task3_stk[TASK_STACKSIZE]; OS_STK task4_stk[TASK_STACKSIZE]; /* Definition of Task Priorities */ #define TASK1_PRIORITY 1 #define TASK2_PRIORITY 2 #define TASK3_PRIORITY 3 #define TASK4_PRIORITY 4 void set_pos_image(int basex, int basey,int x, int y){// FUNCION PARA POSICIONAR IMAGENES IOWR(basex,0,x); IOWR(basey,0,y); } void init_game(void){// FUNCION PARA INICIALIZAR JUEGO // INICIALIZO LA ESCALA DE CADA IMAGEN IOWR(SCALE1_BASE,0,0); IOWR(SCALE2_BASE,0,0); IOWR(SCALE3_BASE,0,0); IOWR(SCALE4_BASE,0,0); //APAGAO CADA IMAGEN AL INICIO DEL JUEGO A MENOS DE QUE QUIERA MOSTRARLAS IOWR(ON_OFF1_BASE,0,0); IOWR(ON_OFF2_BASE,0,0); IOWR(ON_OFF3_BASE,0,0); IOWR(ON_OFF4_BASE,0,0); //INICIALIZO LA POSICION DE LAS IMAGENES set_pos_image(GOKU_X1_BASE,GOKU_Y1_BASE,0, 0); set_pos_image(GOKU_X2_BASE,GOKU_Y2_BASE,30, 0); set_pos_image(GOKU_X3_BASE,GOKU_Y3_BASE,60, 0); set_pos_image(GOKU_X4_BASE,GOKU_Y4_BASE,90, 0); } // VARIABLES GLOBALES DE BOTONES unsigned char buttons_control1[12]; unsigned char buttons_control2[12]; void task1(void* pdata)// TASK PARA ANIMAR A goku { unsigned char animation=0; // inicializo todas las imagenes en el mismo punto set_pos_image(GOKU_X1_BASE,GOKU_Y1_BASE,0, 0); set_pos_image(GOKU_X2_BASE,GOKU_Y2_BASE,0, 0); set_pos_image(GOKU_X3_BASE,GOKU_Y3_BASE,0, 0); set_pos_image(GOKU_X4_BASE,GOKU_Y4_BASE,0, 0); while (1) { animation++; if(animation>3){animation=0;} switch(animation){ case 0: IOWR(ON_OFF1_BASE,0,1); IOWR(ON_OFF2_BASE,0,0); IOWR(ON_OFF3_BASE,0,0); IOWR(ON_OFF4_BASE,0,0); break; case 1: IOWR(ON_OFF1_BASE,0,0); IOWR(ON_OFF2_BASE,0,1); IOWR(ON_OFF3_BASE,0,0); IOWR(ON_OFF4_BASE,0,0); break; case 2: IOWR(ON_OFF1_BASE,0,0); IOWR(ON_OFF2_BASE,0,0); IOWR(ON_OFF3_BASE,0,1); IOWR(ON_OFF4_BASE,0,0); break; case 3: IOWR(ON_OFF1_BASE,0,0); IOWR(ON_OFF2_BASE,0,0); IOWR(ON_OFF3_BASE,0,0); IOWR(ON_OFF4_BASE,0,1); break; } //tiempo de retrazo OSTimeDlyHMSM(0, 0, 0, 100); } } void task2(void* pdata)// TASK PARA ADQUIRIR BOTONES { unsigned short snes1_control=0; while (1) { snes1_control=IORD(SNES_CONTROL1_BASE,0); buttons_control1[0]=snes1_control & 1;// B buttons_control1[1]=(snes1_control & 2)>>1;// Y buttons_control1[2]=(snes1_control & 4)>>2;// Start buttons_control1[3]=(snes1_control & 8)>>3;// select buttons_control1[4]=(snes1_control & 16)>>4;//UP buttons_control1[5]=(snes1_control & 32)>>5;//down buttons_control1[6]=(snes1_control & 64)>>6;//left buttons_control1[7]=(snes1_control & 128)>>7;//right buttons_control1[8]=(snes1_control & 256)>>8;//A buttons_control1[9]=(snes1_control & 512)>>9;//X buttons_control1[10]=(snes1_control & 1024)>>10;//L buttons_control1[11]=(snes1_control & 2048)>>11;//R snes1_control=IORD(SNES_CONTROL2_BASE,0); buttons_control2[0]=snes1_control & 1;// B buttons_control2[1]=(snes1_control & 2)>>1;// Y buttons_control2[2]=(snes1_control & 4)>>2;// Start buttons_control2[3]=(snes1_control & 8)>>3;// select buttons_control2[4]=(snes1_control & 16)>>4;//UP buttons_control2[5]=(snes1_control & 32)>>5;//down buttons_control2[6]=(snes1_control & 64)>>6;//left buttons_control2[7]=(snes1_control & 128)>>7;//right buttons_control2[8]=(snes1_control & 256)>>8;//A buttons_control2[9]=(snes1_control & 512)>>9;//X buttons_control2[10]=(snes1_control & 1024)>>10;//L buttons_control2[11]=(snes1_control & 2048)>>11;//R OSTimeDlyHMSM(0, 0, 0, 70); } } void task3(void* pdata)// TASK PARA MOVER A GOKU { unsigned int pos_x=0,pos_y=0; while (1) { if(buttons_control1[4]==0){ pos_y=pos_y-10; } else if(buttons_control1[5]==0){ pos_y=pos_y+10; } if(buttons_control1[6]==0){ pos_x=pos_x-10; } else if(buttons_control1[7]==0){ pos_x=pos_x+10; } set_pos_image(GOKU_X1_BASE,GOKU_Y1_BASE,pos_x, pos_y); set_pos_image(GOKU_X2_BASE,GOKU_Y2_BASE,pos_x, pos_y); set_pos_image(GOKU_X3_BASE,GOKU_Y3_BASE,pos_x, pos_y); set_pos_image(GOKU_X4_BASE,GOKU_Y4_BASE,pos_x, pos_y); OSTimeDlyHMSM(0, 0, 0, 70); } } void task4(void* pdata)// TASK PARA GENERAR AUDIO { unsigned int nota_div_freq[32]={56817,56817,47777,56817,42565,56817,37920,42565,47777,47777,37920,47777,31887,47777,37920,47777,63775,63775,50619,63775,47777,63775,42565,47777,71585,71585,56817,71585,50619,71585,47777,50619}; unsigned char count=0; //init div_freq IOWR(NOTA_BASE,0,0); while (1) { count++; if(count>31){ count=0; } IOWR(NOTA_BASE,0,nota_div_freq[count]); if(buttons_control1[0]==0){// si se oprime B OSTimeDlyHMSM(0, 0, 0, 170/4); } else if(buttons_control1[1]==0){// si se oprime Y OSTimeDlyHMSM(0, 0, 0, 170/2); } else{// si no se oprime nada OSTimeDlyHMSM(0, 0, 0, 170); } } } /* The main function creates two task and starts multi-tasking */ int main(void) { //INICIALIZO JUEGO ANTES DE LANZARLO init_game(); OSTaskCreateExt(task1, NULL, (void *)&task1_stk[TASK_STACKSIZE-1], TASK1_PRIORITY, TASK1_PRIORITY, task1_stk, TASK_STACKSIZE, NULL, 0); OSTaskCreateExt(task2, NULL, (void *)&task2_stk[TASK_STACKSIZE-1], TASK2_PRIORITY, TASK2_PRIORITY, task2_stk, TASK_STACKSIZE, NULL, 0); OSTaskCreateExt(task3, NULL, (void *)&task3_stk[TASK_STACKSIZE-1], TASK3_PRIORITY, TASK3_PRIORITY, task3_stk, TASK_STACKSIZE, NULL, 0); OSTaskCreateExt(task4, NULL, (void *)&task4_stk[TASK_STACKSIZE-1], TASK4_PRIORITY, TASK4_PRIORITY, task4_stk, TASK_STACKSIZE, NULL, 0); OSStart(); return 0; } /****************************************************************************** * * * License Agreement * * * * Copyright (c) 2004 Altera Corporation, San Jose, California, USA. * * All rights reserved. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the "Software"), * * to deal in the Software without restriction, including without limitation * * the rights to use, copy, modify, merge, publish, distribute, sublicense, * * and/or sell copies of the Software, and to permit persons to whom the * * Software is furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included in * * all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * * DEALINGS IN THE SOFTWARE. * * * * This agreement shall be governed in all respects by the laws of the State * * of California and by the laws of the United States of America. * * Altera does not recommend, suggest or require that this reference design * * file be used in conjunction or combination with any other product. * ******************************************************************************/