Advertisement
Guest User

Untitled

a guest
Oct 16th, 2014
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.58 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2009 Xilinx, Inc. All rights reserved.
  3. *
  4. * Xilinx, Inc.
  5. * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
  6. * COURTESY TO YOU. BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
  7. * ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
  8. * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
  9. * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
  10. * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
  11. * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
  12. * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
  13. * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
  14. * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
  15. * AND FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. */
  18.  
  19. /*
  20. * helloworld.c: simple test application
  21. */
  22. #include "xgpio.h" // Provides access to PB GPIO driver.
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include "mb_interface.h" // provides the microblaze interrupt enables, etc.
  26. #include "xintc_l.h" // Provides handy macros for the interrupt controller.
  27. #include "platform.h"
  28. #include "xparameters.h"
  29. #include "xaxivdma.h"
  30. #include "xio.h"
  31. #include "time.h"
  32. #include "unistd.h"
  33. #include "bitmask.h"
  34. #include "globals.h"
  35. XGpio gpLED; // This is a handle for the LED GPIO block.
  36. XGpio gpPB; // This is a handle for the push-button GPIO block.
  37.  
  38. #define DEBUG
  39. void print(char *str);
  40.  
  41. #define FRAME_BUFFER_0_ADDR 0xC1000000 // Starting location in DDR where we will store the images that we display.
  42. direction tank_direction = RIGHT;
  43. int move_alien_right = 1;
  44. u32 currentButtonState;
  45. u16 debounce = 0;
  46. u32 LEFTSBTNPRESS = 0x8;
  47. u32 MIDDLESBTNPRESS = 0x1;
  48. u32 RIGHTBTNPRESS = 0x2;
  49. u32 UPBTNPRESS = 0x10;
  50. u32 DOWNBTNPRESS = 0x4;
  51. u16 button_delay = 0;
  52. u32 update_screen_delay = 0;
  53. u32 second_delay = 0;
  54. int alien_delay_speed = 100;
  55. int random = 0;
  56. int * tankDeath_symbol;
  57. int tank_dead_count;
  58. int gameOver = 0;
  59. int gameOverCount;
  60. int UFO_speed = 0;
  61. int bulletShotCount = 0;
  62. UFO ufo;
  63. unsigned int * framePointer;
  64.  
  65. void timer_interrupt_handler() {
  66.  
  67. if (!gameOver) {
  68. UFO_speed++;
  69. button_delay++;
  70. update_screen_delay++;
  71. second_delay++;
  72.  
  73. if (button_delay == 2) {
  74. button_delay = 0;
  75.  
  76. if ((currentButtonState & LEFTSBTNPRESS) && !tank_hit) {
  77. //Move Tank Left
  78.  
  79. tank_direction = LEFT;
  80. move_tank();
  81.  
  82. }
  83.  
  84. if ((currentButtonState & RIGHTBTNPRESS) && !tank_hit) {
  85. //Move Tank Right
  86.  
  87. tank_direction = RIGHT;
  88. move_tank();
  89. }
  90.  
  91. if (currentButtonState & MIDDLESBTNPRESS) {
  92. //Shoot Tank Bullet
  93. shootTankBullet();
  94. }
  95.  
  96. if (currentButtonState & DOWNBTNPRESS) {
  97. //Make UFO
  98. spawnUFO();
  99. }
  100.  
  101. if(currentButtonState & UPBTNPRESS)
  102. init(framePointer);
  103. }
  104. if (UFO_speed == 3) {
  105. UFO_speed = 0;
  106.  
  107. moveUFO();
  108.  
  109. }
  110.  
  111. if (update_screen_delay % 128) {
  112. moveBullets();
  113. random++;
  114. if (random > 256) {
  115. shootAlienBullet();
  116. random = 0;
  117. random = rand() % 256;
  118.  
  119. bulletShotCount++;
  120. }
  121. if(bulletShotCount == 20){
  122. spawnUFO();
  123. bulletShotCount = 0;
  124. }
  125. }
  126. if (second_delay == 10) {
  127. //updateScore(5);
  128. second_delay = 0;
  129. if (ufoDie){
  130. draw(ufo.pos.x, ufo.pos.y, blankAlienSymbol, ALIEN_WIDTH, ALIEN_HEIGHT,
  131. 0x0000000);
  132. ufoDie = 0;
  133. if(ufo.ufo_direction == RIGHT){
  134. ufo.ufo_direction = LEFT;
  135. }
  136. else{
  137. ufo.ufo_direction = RIGHT;
  138. }
  139. //add score stuff
  140. updateScore(69);
  141. }
  142. if (tank_hit) {
  143. draw(tank.x, tank.y, blankAlienSymbol, TANK_WIDTH, TANK_HEIGHT,
  144. 0x00000000); //clear
  145. draw(tank.x, tank.y, tankDeath_symbol, TANK_WIDTH, TANK_HEIGHT,
  146. 0x0000FF00); //draw 1
  147. tank_dead_count++;
  148. }
  149. if (tankDeath_symbol == tankDeath_1) {
  150. tankDeath_symbol = tankDeath_2;
  151. } else {
  152. tankDeath_symbol = tankDeath_1;
  153. }
  154. if (tank_dead_count == 15) {
  155. tank_hit = 0;
  156. tank_dead_count = 0;
  157. lives--;
  158. updateLives();
  159.  
  160. draw(tank.x, tank.y, blankAlienSymbol, TANK_WIDTH, TANK_HEIGHT,
  161. 0x00000000); //clear
  162. draw(tank.x, tank.y, tankSymbol, TANK_WIDTH, TANK_HEIGHT,
  163. 0x000FF000); //clear
  164. }
  165.  
  166. if (lives == 0) {
  167.  
  168. int row, column;
  169. for (row = 0; row < 480; row++) {
  170. for (column = 0; column < 640; column++) {
  171.  
  172. framePointer[row * 640 + column] = 0x00000000;
  173.  
  174. }
  175. }
  176.  
  177. int x = 175;
  178. int i = 0;
  179. int * symbol;
  180. for (i = 0; i < 9; i++) {
  181.  
  182. switch (i) {
  183. case 0:
  184. symbol = GO_G;
  185. break;
  186. case 1:
  187. symbol = GO_A;
  188. break;
  189. case 2:
  190. symbol = GO_M;
  191. break;
  192. case 3:
  193. symbol = GO_E;
  194. break;
  195. case 4:
  196. symbol = GO_Blank;
  197. break;
  198. case 5:
  199. symbol = GO_O;
  200. break;
  201. case 6:
  202. symbol = GO_V;
  203. break;
  204. case 7:
  205. symbol = GO_E;
  206. break;
  207. case 8:
  208. symbol = GO_R;
  209. break;
  210. default:
  211. symbol = GO_G;
  212.  
  213. }
  214.  
  215. draw(x, 200, symbol, 32, 32, 0xFFFFFFF);
  216. x += 35;
  217. }
  218.  
  219. gameOver = 1;
  220.  
  221. // XGpio_InterruptClear(&gpPB, 0xFFFFFFFF); // Ack the PB interrupt.
  222. //XGpio_InterruptGlobalEnable(&gpPB); // Re-enable PB interrupts.
  223. //init(framePointer);
  224. }
  225.  
  226. }
  227.  
  228. if (update_screen_delay >= alien_delay_speed) {
  229. move_aliens();
  230. update_screen_delay = 0;
  231. }
  232. } else {
  233. gameOverCount++;
  234.  
  235. if (gameOverCount == 500) {
  236. gameOverCount = 0;
  237. gameOver = 0;
  238. init(framePointer);
  239. }
  240.  
  241. }
  242. }
  243.  
  244. void pb_interrupt_handler() {
  245.  
  246. // Clear the GPIO interrupt.
  247. XGpio_InterruptGlobalDisable(&gpPB); // Turn off all PB interrupts for now.
  248. currentButtonState = XGpio_DiscreteRead(&gpPB, 1); // Get the current state of the buttons.
  249. // You need to do something here.
  250.  
  251. //xil_printf("%x\n",currentButtonState);
  252.  
  253.  
  254. XGpio_InterruptClear(&gpPB, 0xFFFFFFFF); // Ack the PB interrupt.
  255. XGpio_InterruptGlobalEnable(&gpPB); // Re-enable PB interrupts.
  256. }
  257.  
  258. void interrupt_handler_dispatcher(void* ptr) {
  259. int intc_status = XIntc_GetIntrStatus(XPAR_INTC_0_BASEADDR);
  260. // Check the FIT interrupt first.
  261. if (intc_status & XPAR_FIT_TIMER_0_INTERRUPT_MASK) {
  262. XIntc_AckIntr(XPAR_INTC_0_BASEADDR, XPAR_FIT_TIMER_0_INTERRUPT_MASK);
  263. timer_interrupt_handler();
  264. }
  265. // Check the push buttons.
  266. if (intc_status & XPAR_PUSH_BUTTONS_5BITS_IP2INTC_IRPT_MASK) {
  267. XIntc_AckIntr(XPAR_INTC_0_BASEADDR, XPAR_PUSH_BUTTONS_5BITS_IP2INTC_IRPT_MASK);
  268. pb_interrupt_handler();
  269. }
  270. }
  271.  
  272. int main() {
  273.  
  274. srand(time(NULL));
  275. init_platform(); // Necessary for all programs.
  276. int Status; // Keep track of success/failure of system function calls.
  277.  
  278. //VDMA SETUP
  279. XAxiVdma videoDMAController;
  280. // There are 3 steps to initializing the vdma driver and IP.
  281. // Step 1: lookup the memory structure that is used to access the vdma driver.
  282. XAxiVdma_Config * VideoDMAConfig = XAxiVdma_LookupConfig(
  283. XPAR_AXI_VDMA_0_DEVICE_ID);
  284. // Step 2: Initialize the memory structure and the hardware.
  285. if (XST_FAILURE == XAxiVdma_CfgInitialize(&videoDMAController,
  286. VideoDMAConfig, XPAR_AXI_VDMA_0_BASEADDR)) {
  287. xil_printf("VideoDMA Did not initialize.\r\n");
  288. }
  289. // Step 3: (optional) set the frame store number.
  290. if (XST_FAILURE == XAxiVdma_SetFrmStore(&videoDMAController, 2,
  291. XAXIVDMA_READ)) {
  292. xil_printf("Set Frame Store Failed.");
  293. }
  294. // Initialization is complete at this point.
  295.  
  296. // Setup the frame counter. We want two read frames. We don't need any write frames but the
  297. // function generates an error if you set the write frame count to 0. We set it to 2
  298. // but ignore it because we don't need a write channel at all.
  299. XAxiVdma_FrameCounter myFrameConfig;
  300. myFrameConfig.ReadFrameCount = 2;
  301. myFrameConfig.ReadDelayTimerCount = 10;
  302. myFrameConfig.WriteFrameCount = 2;
  303. myFrameConfig.WriteDelayTimerCount = 10;
  304. Status = XAxiVdma_SetFrameCounter(&videoDMAController, &myFrameConfig);
  305. if (Status != XST_SUCCESS) {
  306. xil_printf("Set frame counter failed %d\r\n", Status);
  307. if (Status == XST_VDMA_MISMATCH_ERROR)
  308. xil_printf("DMA Mismatch Error\r\n");
  309. }
  310. // Now we tell the driver about the geometry of our frame buffer and a few other things.
  311. // Our image is 480 x 640.
  312. XAxiVdma_DmaSetup myFrameBuffer;
  313. myFrameBuffer.VertSizeInput = 480; // 480 vertical pixels.
  314. myFrameBuffer.HoriSizeInput = 640 * 4; // 640 horizontal (32-bit pixels).
  315. myFrameBuffer.Stride = 640 * 4; // Dont' worry about the rest of the values.
  316. myFrameBuffer.FrameDelay = 0;
  317. myFrameBuffer.EnableCircularBuf = 1;
  318. myFrameBuffer.EnableSync = 0;
  319. myFrameBuffer.PointNum = 0;
  320. myFrameBuffer.EnableFrameCounter = 0;
  321. myFrameBuffer.FixedFrameStoreAddr = 0;
  322. if (XST_FAILURE == XAxiVdma_DmaConfig(&videoDMAController, XAXIVDMA_READ,
  323. &myFrameBuffer)) {
  324. xil_printf("DMA Config Failed\r\n");
  325. }
  326. // We need to give the frame buffer pointers to the memory that it will use. This memory
  327. // is where you will write your video data. The vdma IP/driver then streams it to the HDMI
  328. // IP.
  329. myFrameBuffer.FrameStoreStartAddr[0] = FRAME_BUFFER_0_ADDR;
  330. myFrameBuffer.FrameStoreStartAddr[1] = FRAME_BUFFER_0_ADDR + 4 * 640 * 480;
  331.  
  332. if (XST_FAILURE == XAxiVdma_DmaSetBufferAddr(&videoDMAController,
  333. XAXIVDMA_READ, myFrameBuffer.FrameStoreStartAddr)) {
  334. xil_printf("DMA Set Address Failed Failed\r\n");
  335. }
  336. // Print a sanity message if you get this far.
  337. xil_printf("Woohoo! I made it through initialization.\n\r");
  338. // Now, let's get ready to start displaying some stuff on the screen.
  339. // The variables framePointer and framePointer1 are just pointers to the base address
  340. // of frame 0 and frame 1.
  341.  
  342. //unsigned int * framePointer = (unsigned int *) FRAME_BUFFER_0_ADDR;
  343. framePointer = (unsigned int *) FRAME_BUFFER_0_ADDR;
  344.  
  345. // Just paint some large red, green, blue, and white squares in different
  346. // positions of the image for each frame in the buffer (framePointer0 and framePointer1).
  347.  
  348.  
  349. init(framePointer);
  350.  
  351. // This tells the HDMI controller the resolution of your display (there must be a better way to do this).
  352. XIo_Out32(XPAR_AXI_HDMI_0_BASEADDR, 640*480);
  353.  
  354. // Start the DMA for the read channel only.
  355. if (XST_FAILURE == XAxiVdma_DmaStart(&videoDMAController, XAXIVDMA_READ)) {
  356. xil_printf("DMA START FAILED\r\n");
  357. }
  358. int frameIndex = 0;
  359. // We have two frames, let's park on frame 0. Use frameIndex to index them.
  360. // Note that you have to start the DMA process before parking on a frame.
  361. if (XST_FAILURE == XAxiVdma_StartParking(&videoDMAController, frameIndex,
  362. XAXIVDMA_READ)) {
  363. xil_printf("vdma parking failed\n\r");
  364. }
  365. // Oscillate between frame 0 and frame 1.
  366. // int sillyTimer = MAX_SILLY_TIMER; // Just a cheap delay between frames.
  367.  
  368. //GPIO SETUP
  369. int success;
  370. success = XGpio_Initialize(&gpPB, XPAR_PUSH_BUTTONS_5BITS_DEVICE_ID);
  371. // Set the push button peripheral to be inputs.
  372. XGpio_SetDataDirection(&gpPB, 1, 0x0000001F);
  373. // Enable the global GPIO interrupt for push buttons.
  374. XGpio_InterruptGlobalEnable(&gpPB);
  375. // Enable all interrupts in the push button peripheral.
  376. XGpio_InterruptEnable(&gpPB, 0xFFFFFFFF);
  377.  
  378. microblaze_register_handler(interrupt_handler_dispatcher, NULL);
  379. XIntc_EnableIntr(XPAR_INTC_0_BASEADDR,
  380. (XPAR_FIT_TIMER_0_INTERRUPT_MASK | XPAR_PUSH_BUTTONS_5BITS_IP2INTC_IRPT_MASK));
  381. XIntc_MasterEnable(XPAR_INTC_0_BASEADDR);
  382. microblaze_enable_interrupts();
  383.  
  384. while (1) {
  385. //char key_input = getKeyChar();
  386. //xil_printf("%c ", key_input);
  387. //processKeyPress(key_input);
  388.  
  389. if (XST_FAILURE == XAxiVdma_StartParking(&videoDMAController, 0,
  390. XAXIVDMA_READ)) {
  391. xil_printf("vdma parking failed\n\r");
  392. }
  393. }
  394. cleanup_platform();
  395.  
  396. return 0;
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement