Contains the source code from the course work throughout my undergraduate Computer Engineering degree at Brigham Young University. There is a mixture of Go, Python, C, C++, Java, VHDL, Verilog, Matlab, Bash, Assembly, etc..
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vdma.c 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. #include <stdio.h>
  2. #include "platform.h"
  3. #include "xparameters.h"
  4. #include "xaxivdma.h"
  5. #include "xio.h"
  6. #include "time.h"
  7. #include "unistd.h"
  8. #include "xgpio.h" // Provides access to PB GPIO driver.
  9. #include "mb_interface.h" // provides the microblaze interrupt enables, etc.
  10. #include "xintc_l.h" // Provides handy macros for the interrupt controller.
  11. #include "xac97_l.h"
  12. #include "nes.h"
  13. #include "global.h"
  14. #include <xuartlite_l.h>
  15. XGpio gpLED; // This is a handle for the LED GPIO block.
  16. XGpio gpPB; // This is a handle for the push-button GPIO block.
  17. #define DEBUG
  18. #define FRAME_BUFFER_0_ADDR 0xC5000000 // Starting location in DDR where we will store the images that we display.
  19. #define VOL_ATTN_MIN 0x0
  20. #define VOL_ATTN_MAX 0x1f
  21. Xuint32 vol_attn = 0x10;
  22. int mytimer = 0;
  23. int mycounter = 0;
  24. int onesec;
  25. int fifteenms;
  26. int fivems;
  27. int tenms;
  28. int buttonValid;
  29. int currentButtonState;
  30. int speed = 30;
  31. int holding;
  32. int gameState = 1;
  33. void increaseVolume() {
  34. if((int)vol_attn > VOL_ATTN_MIN) {
  35. vol_attn = vol_attn - 0x1;
  36. XAC97_WriteReg(XPAR_AXI_AC97_0_BASEADDR, AC97_MasterVol, vol_attn | (vol_attn << 8));
  37. XAC97_WriteReg(XPAR_AXI_AC97_0_BASEADDR, AC97_AuxOutVol, vol_attn | (vol_attn << 8));
  38. }
  39. }
  40. void decreaseVolume() {
  41. if((int)vol_attn < VOL_ATTN_MAX) {
  42. vol_attn = vol_attn + 0x1;
  43. XAC97_WriteReg(XPAR_AXI_AC97_0_BASEADDR, AC97_MasterVol, vol_attn | (vol_attn << 8));
  44. XAC97_WriteReg(XPAR_AXI_AC97_0_BASEADDR, AC97_AuxOutVol, vol_attn | (vol_attn << 8));
  45. }
  46. }
  47. void delayDying() {
  48. int counter = 0;
  49. while(counter < 500000){
  50. counter++;
  51. }
  52. }
  53. void setGameState(int mystate){
  54. gameState = mystate;
  55. }
  56. unsigned int * framePointer = (unsigned int *) FRAME_BUFFER_0_ADDR;
  57. void pb_interrupt_handler();
  58. int isBottom(int value) {
  59. short* aliens = getAliens();
  60. if(value > 43) {
  61. return 1;
  62. }
  63. if(value < 44 && value > 32) {
  64. if(aliens[value+11] == 0) {
  65. return 1;
  66. }
  67. else {
  68. return 0;
  69. }
  70. }
  71. if(value < 33 && value > 21) {
  72. if(aliens[value+11] == 0 && aliens[value+22] == 0) {
  73. return 1;
  74. }
  75. else {
  76. return 0;
  77. }
  78. }
  79. if(value < 22 && value > 10) {
  80. if(aliens[value+11] == 0 && aliens[value+22] == 0 && aliens[value+33] == 0) {
  81. return 1;
  82. }
  83. else {
  84. return 0;
  85. }
  86. }
  87. if(value < 11) {
  88. if(aliens[value+11] == 0 && aliens[value+22] == 0 && aliens[value+33] == 0 && aliens[value+44] == 0) {
  89. return 1;
  90. }
  91. else {
  92. return 0;
  93. }
  94. }
  95. }
  96. int whatRow(int position) {
  97. if(position > 43) {
  98. return 120;
  99. }
  100. if(position < 44 && position > 32) {
  101. return 94;
  102. }
  103. if(position < 33 && position > 21) {
  104. return 68;
  105. }
  106. if(position < 22 && position > 10) {
  107. return 42;
  108. }
  109. if(position < 11) {
  110. return 16;
  111. }
  112. }
  113. void collisionCleanUp(int x, int y) {
  114. setHaveAlienDeathSound(1);
  115. blankTankBullet(framePointer);
  116. setTankBulletPositionY(0);
  117. setTankBulletState(0);
  118. delayDying();
  119. dyingAlienHelper(framePointer, x, y);
  120. int guise = getGuise();
  121. if(guise == 0) {
  122. renderAlien2(framePointer);
  123. }
  124. else {
  125. renderAlien1(framePointer);
  126. }
  127. alienSpeed();
  128. }
  129. void alienCollision() {
  130. int curTankY = ((getTankBulletPositionY()-2)*640);
  131. int curTankX = getTankBulletPositionX()+3;
  132. int curAlienX = getAlienBlockPositionX();
  133. int curAlienY = getAlienBlockPositionY();
  134. int ufoscore = 0;
  135. int updatescore = 0;
  136. if(framePointer[((getTankBulletPositionY()-2)*640) + getTankBulletPositionX()+3] == 0x00FF0000) {
  137. setHaveSpaceShipDeathSound(1);
  138. blankUfo(framePointer, getUfoPositionX(), getUfoPositionY());
  139. setUfoState(0);
  140. blankTankBullet(framePointer);
  141. setTankBulletPositionY(0);
  142. setTankBulletState(0);
  143. // ufo score operation
  144. ufoscore = rand()%7;
  145. updatescore = selectingUfoscore(framePointer, ufoscore);
  146. updateMyscore(updatescore);
  147. updateNumber(framePointer);
  148. }
  149. if(framePointer[((getTankBulletPositionY()-2)*640) + getTankBulletPositionX()+3] == 0x00FFFFFF || \
  150. framePointer[((getTankBulletPositionY()-2)*640) + getTankBulletPositionX()+5] == 0x00FFFFFF ||\
  151. framePointer[((getTankBulletPositionY()-2)*640) + getTankBulletPositionX()] == 0x00FFFFFF){
  152. curTankY = curTankY/640;
  153. if(curAlienX > 1000) {
  154. curAlienX -= 65535;
  155. }
  156. xil_printf("curX: %d\r\n", curAlienX);
  157. if(curAlienY < curTankY && curTankY < curAlienY + 16 ) {
  158. if(curAlienX < curTankX && curTankX < (curAlienX + 32)) {
  159. dyingAlien(framePointer, (getAlienBlockPositionX()), (getAlienBlockPositionY()));
  160. setAliens(0, 0);
  161. collisionCleanUp((getAlienBlockPositionX()), (getAlienBlockPositionY()));
  162. }
  163. if(curAlienX + 32 < curTankX && curTankX < curAlienX + 64) {
  164. dyingAlien(framePointer, (getAlienBlockPositionX()+32), (getAlienBlockPositionY()));
  165. setAliens(0, 1);
  166. collisionCleanUp((getAlienBlockPositionX()+32), (getAlienBlockPositionY()));
  167. }
  168. if(curAlienX + 64 < curTankX && curTankX < curAlienX + 96) {
  169. dyingAlien(framePointer, (getAlienBlockPositionX()+64), (getAlienBlockPositionY()));
  170. setAliens(0, 2);
  171. collisionCleanUp((getAlienBlockPositionX()+64), (getAlienBlockPositionY()));
  172. }
  173. if(curAlienX + 96 < curTankX && curTankX < curAlienX + 128) {
  174. dyingAlien(framePointer, (getAlienBlockPositionX()+96), (getAlienBlockPositionY()));
  175. setAliens(0, 3);
  176. collisionCleanUp((getAlienBlockPositionX()+96), (getAlienBlockPositionY()));
  177. }
  178. if(curAlienX + 128 < curTankX && curTankX < curAlienX + 160) {
  179. dyingAlien(framePointer, (getAlienBlockPositionX()+128), (getAlienBlockPositionY()));
  180. setAliens(0, 4);
  181. collisionCleanUp((getAlienBlockPositionX()+128), (getAlienBlockPositionY()));
  182. }
  183. if(curAlienX + 160 < curTankX && curTankX < curAlienX + 192) {
  184. dyingAlien(framePointer, (getAlienBlockPositionX()+160), (getAlienBlockPositionY()));
  185. setAliens(0, 5);
  186. collisionCleanUp((getAlienBlockPositionX()+160), (getAlienBlockPositionY()));
  187. }
  188. if(curAlienX + 192 < curTankX && curTankX < curAlienX + 224) {
  189. dyingAlien(framePointer, (getAlienBlockPositionX()+192), (getAlienBlockPositionY()));
  190. setAliens(0, 6);
  191. collisionCleanUp((getAlienBlockPositionX()+192), (getAlienBlockPositionY()));
  192. }
  193. if(curAlienX + 224 < curTankX && curTankX < curAlienX + 256) {
  194. dyingAlien(framePointer, (getAlienBlockPositionX()+ 224), (getAlienBlockPositionY()));
  195. setAliens(0, 7);
  196. collisionCleanUp((getAlienBlockPositionX()+ 224), (getAlienBlockPositionY()));
  197. }
  198. if(curAlienX + 256 < curTankX && curTankX < curAlienX + 288) {
  199. dyingAlien(framePointer, (getAlienBlockPositionX()+ 256), (getAlienBlockPositionY()));
  200. setAliens(0, 8);
  201. collisionCleanUp((getAlienBlockPositionX()+256), (getAlienBlockPositionY()));
  202. }
  203. if(curAlienX + 288 < curTankX && curTankX < curAlienX + 320) {
  204. dyingAlien(framePointer, (getAlienBlockPositionX()+288), (getAlienBlockPositionY()));
  205. setAliens(0, 9);
  206. collisionCleanUp((getAlienBlockPositionX()+288), (getAlienBlockPositionY()));
  207. }
  208. if(curAlienX + 320 < curTankX && curTankX < curAlienX + 352) {
  209. dyingAlien(framePointer, (getAlienBlockPositionX()+320), (getAlienBlockPositionY()));
  210. setAliens(0, 10);
  211. collisionCleanUp((getAlienBlockPositionX()+320), (getAlienBlockPositionY()));
  212. }
  213. updateMyscore(40);
  214. updateNumber(framePointer);
  215. }
  216. if(curAlienY + 26 < curTankY && curTankY < curAlienY + 42) {
  217. if(curAlienX < curTankX && curTankX < (curAlienX + 32)) {
  218. dyingAlien(framePointer, (getAlienBlockPositionX()), (getAlienBlockPositionY()+26));
  219. setAliens(0, 11);
  220. collisionCleanUp((getAlienBlockPositionX()), (getAlienBlockPositionY()+26));
  221. }
  222. if(curAlienX + 32 < curTankX && curTankX < curAlienX + 64) {
  223. dyingAlien(framePointer, (getAlienBlockPositionX()+32), (getAlienBlockPositionY()+26));
  224. setAliens(0, 12);
  225. collisionCleanUp((getAlienBlockPositionX()+32), (getAlienBlockPositionY()+26));
  226. }
  227. if(curAlienX + 64 < curTankX && curTankX < curAlienX + 96) {
  228. dyingAlien(framePointer, (getAlienBlockPositionX()+64), (getAlienBlockPositionY()+26));
  229. setAliens(0, 13);
  230. collisionCleanUp((getAlienBlockPositionX()+64), (getAlienBlockPositionY()+26));
  231. }
  232. if(curAlienX + 96 < curTankX && curTankX < curAlienX + 128) {
  233. dyingAlien(framePointer, (getAlienBlockPositionX()+96), (getAlienBlockPositionY()+26));
  234. setAliens(0, 14);
  235. collisionCleanUp((getAlienBlockPositionX()+96), (getAlienBlockPositionY()+26));
  236. }
  237. if(curAlienX + 128 < curTankX && curTankX < curAlienX + 160) {
  238. dyingAlien(framePointer, (getAlienBlockPositionX()+128), (getAlienBlockPositionY()+26));
  239. setAliens(0, 15);
  240. collisionCleanUp((getAlienBlockPositionX()+128), (getAlienBlockPositionY()+26));
  241. }
  242. if(curAlienX + 160 < curTankX && curTankX < curAlienX + 192) {
  243. dyingAlien(framePointer, (getAlienBlockPositionX()+160), (getAlienBlockPositionY()+26));
  244. setAliens(0, 16);
  245. collisionCleanUp((getAlienBlockPositionX()+160), (getAlienBlockPositionY()+26));
  246. }
  247. if(curAlienX + 192 < curTankX && curTankX < curAlienX + 224) {
  248. dyingAlien(framePointer, (getAlienBlockPositionX()+192), (getAlienBlockPositionY()+26));
  249. setAliens(0, 17);
  250. collisionCleanUp((getAlienBlockPositionX()+192), (getAlienBlockPositionY()+26));
  251. }
  252. if(curAlienX + 224 < curTankX && curTankX < curAlienX + 256) {
  253. dyingAlien(framePointer, (getAlienBlockPositionX()+224), (getAlienBlockPositionY()+26));
  254. setAliens(0, 18);
  255. collisionCleanUp((getAlienBlockPositionX()+224), (getAlienBlockPositionY()+26));
  256. }
  257. if(curAlienX + 256 < curTankX && curTankX < curAlienX + 288) {
  258. dyingAlien(framePointer, (getAlienBlockPositionX()+256), (getAlienBlockPositionY()+26));
  259. setAliens(0, 19);
  260. collisionCleanUp((getAlienBlockPositionX()+256), (getAlienBlockPositionY()+26));
  261. }
  262. if(curAlienX + 288 < curTankX && curTankX < curAlienX + 320) {
  263. dyingAlien(framePointer, (getAlienBlockPositionX()+288), (getAlienBlockPositionY()+26));
  264. setAliens(0, 20);
  265. collisionCleanUp((getAlienBlockPositionX()+288), (getAlienBlockPositionY()+26));
  266. }
  267. if(curAlienX + 320 < curTankX && curTankX < curAlienX + 352) {
  268. dyingAlien(framePointer, (getAlienBlockPositionX()+320), (getAlienBlockPositionY()+26));
  269. setAliens(0, 21);
  270. collisionCleanUp((getAlienBlockPositionX()+320), (getAlienBlockPositionY()+26));
  271. }
  272. updateMyscore(20);
  273. updateNumber(framePointer);
  274. }
  275. if(curAlienY + 52 < curTankY && curTankY < curAlienY + 68) {
  276. if(curAlienX < curTankX && curTankX < (curAlienX + 32)) {
  277. dyingAlien(framePointer, (getAlienBlockPositionX()), (getAlienBlockPositionY()+52));
  278. setAliens(0, 22);
  279. collisionCleanUp((getAlienBlockPositionX()), (getAlienBlockPositionY()+52));
  280. }
  281. if(curAlienX + 32 < curTankX && curTankX < curAlienX + 64) {
  282. dyingAlien(framePointer, (getAlienBlockPositionX()+32), (getAlienBlockPositionY()+52));
  283. setAliens(0, 23);
  284. collisionCleanUp((getAlienBlockPositionX()+32), (getAlienBlockPositionY()+52));
  285. }
  286. if(curAlienX + 64 < curTankX && curTankX < curAlienX + 96) {
  287. dyingAlien(framePointer, (getAlienBlockPositionX()+64), (getAlienBlockPositionY()+52));
  288. setAliens(0, 24);
  289. collisionCleanUp((getAlienBlockPositionX()+64), (getAlienBlockPositionY()+52));
  290. }
  291. if(curAlienX + 96 < curTankX && curTankX < curAlienX + 128) {
  292. dyingAlien(framePointer, (getAlienBlockPositionX()+96), (getAlienBlockPositionY()+52));
  293. setAliens(0, 25);
  294. collisionCleanUp((getAlienBlockPositionX()+96), (getAlienBlockPositionY()+52));
  295. }
  296. if(curAlienX + 128 < curTankX && curTankX < curAlienX + 160) {
  297. dyingAlien(framePointer, (getAlienBlockPositionX()+128), (getAlienBlockPositionY()+52));
  298. setAliens(0, 26);
  299. collisionCleanUp((getAlienBlockPositionX()+128), (getAlienBlockPositionY()+52));
  300. }
  301. if(curAlienX + 160 < curTankX && curTankX < curAlienX + 192) {
  302. dyingAlien(framePointer, (getAlienBlockPositionX()+160), (getAlienBlockPositionY()+52));
  303. setAliens(0, 27);
  304. collisionCleanUp((getAlienBlockPositionX()+160), (getAlienBlockPositionY()+52));
  305. }
  306. if(curAlienX + 192 < curTankX && curTankX < curAlienX + 224) {
  307. dyingAlien(framePointer, (getAlienBlockPositionX()+192), (getAlienBlockPositionY()+52));
  308. setAliens(0, 28);
  309. collisionCleanUp((getAlienBlockPositionX()+192), (getAlienBlockPositionY()+52));
  310. }
  311. if(curAlienX + 224 < curTankX && curTankX < curAlienX + 256) {
  312. dyingAlien(framePointer, (getAlienBlockPositionX()+224), (getAlienBlockPositionY()+52));
  313. setAliens(0, 29);
  314. collisionCleanUp((getAlienBlockPositionX()+224), (getAlienBlockPositionY()+52));
  315. }
  316. if(curAlienX + 256 < curTankX && curTankX < curAlienX + 288) {
  317. dyingAlien(framePointer, (getAlienBlockPositionX()+256), (getAlienBlockPositionY()+52));
  318. setAliens(0, 30);
  319. collisionCleanUp((getAlienBlockPositionX()+256), (getAlienBlockPositionY()+52));
  320. }
  321. if(curAlienX + 288 < curTankX && curTankX < curAlienX + 320) {
  322. dyingAlien(framePointer, (getAlienBlockPositionX()+288), (getAlienBlockPositionY()+52));
  323. setAliens(0, 31);
  324. collisionCleanUp((getAlienBlockPositionX()+288), (getAlienBlockPositionY()+52));
  325. }
  326. if(curAlienX + 320 < curTankX && curTankX < curAlienX + 352) {
  327. dyingAlien(framePointer, (getAlienBlockPositionX()+320), (getAlienBlockPositionY()+52));
  328. setAliens(0, 32);
  329. collisionCleanUp((getAlienBlockPositionX()+320), (getAlienBlockPositionY()+52));
  330. }
  331. updateMyscore(20);
  332. updateNumber(framePointer);
  333. }
  334. if(curAlienY + 78 < curTankY && curTankY < curAlienY + 94) {
  335. if(curAlienX < curTankX && curTankX < (curAlienX + 32)) {
  336. dyingAlien(framePointer, (getAlienBlockPositionX()), (getAlienBlockPositionY()+78));
  337. setAliens(0, 33);
  338. collisionCleanUp((getAlienBlockPositionX()), (getAlienBlockPositionY()+78));
  339. }
  340. if(curAlienX + 32 < curTankX && curTankX < curAlienX + 64) {
  341. dyingAlien(framePointer, (getAlienBlockPositionX()+32), (getAlienBlockPositionY()+78));
  342. setAliens(0, 34);
  343. collisionCleanUp((getAlienBlockPositionX()+32), (getAlienBlockPositionY()+78));
  344. }
  345. if(curAlienX + 64 < curTankX && curTankX < curAlienX + 96) {
  346. dyingAlien(framePointer, (getAlienBlockPositionX()+64), (getAlienBlockPositionY()+78));
  347. setAliens(0, 35);
  348. collisionCleanUp((getAlienBlockPositionX()+64), (getAlienBlockPositionY()+78));
  349. }
  350. if(curAlienX + 96 < curTankX && curTankX < curAlienX + 128) {
  351. dyingAlien(framePointer, (getAlienBlockPositionX()+96), (getAlienBlockPositionY()+78));
  352. setAliens(0, 36);
  353. collisionCleanUp((getAlienBlockPositionX()+96), (getAlienBlockPositionY()+78));
  354. }
  355. if(curAlienX + 128 < curTankX && curTankX < curAlienX + 160) {
  356. dyingAlien(framePointer, (getAlienBlockPositionX()+128), (getAlienBlockPositionY()+78));
  357. setAliens(0, 37);
  358. collisionCleanUp((getAlienBlockPositionX()+128), (getAlienBlockPositionY()+78));
  359. }
  360. if(curAlienX + 160 < curTankX && curTankX < curAlienX + 192) {
  361. dyingAlien(framePointer, (getAlienBlockPositionX()+160), (getAlienBlockPositionY()+78));
  362. setAliens(0, 38);
  363. collisionCleanUp((getAlienBlockPositionX()+160), (getAlienBlockPositionY()+78));
  364. }
  365. if(curAlienX + 192 < curTankX && curTankX < curAlienX + 224) {
  366. dyingAlien(framePointer, (getAlienBlockPositionX()+192), (getAlienBlockPositionY()+78));
  367. setAliens(0, 39);
  368. collisionCleanUp((getAlienBlockPositionX()+192), (getAlienBlockPositionY()+78));
  369. }
  370. if(curAlienX + 224 < curTankX && curTankX < curAlienX + 256) {
  371. dyingAlien(framePointer, (getAlienBlockPositionX()+224), (getAlienBlockPositionY()+78));
  372. setAliens(0, 40);
  373. collisionCleanUp((getAlienBlockPositionX()+224), (getAlienBlockPositionY()+78));
  374. }
  375. if(curAlienX + 256 < curTankX && curTankX < curAlienX + 288) {
  376. dyingAlien(framePointer, (getAlienBlockPositionX()+256), (getAlienBlockPositionY()+78));
  377. setAliens(0, 41);
  378. collisionCleanUp((getAlienBlockPositionX()+256), (getAlienBlockPositionY()+78));
  379. }
  380. if(curAlienX + 288 < curTankX && curTankX < curAlienX + 320) {
  381. dyingAlien(framePointer, (getAlienBlockPositionX()+288), (getAlienBlockPositionY()+78));
  382. setAliens(0, 42);
  383. collisionCleanUp((getAlienBlockPositionX()+288), (getAlienBlockPositionY()+78));
  384. }
  385. if(curAlienX + 320 < curTankX && curTankX < curAlienX + 352) {
  386. dyingAlien(framePointer, (getAlienBlockPositionX()+320), (getAlienBlockPositionY()+78));
  387. setAliens(0, 43);
  388. collisionCleanUp((getAlienBlockPositionX()+320), (getAlienBlockPositionY()+78));
  389. }
  390. updateMyscore(10);
  391. updateNumber(framePointer);
  392. }
  393. if(curAlienY + 104 < curTankY && curTankY < curAlienY +120){
  394. if(curAlienX < curTankX && curTankX < (curAlienX + 32)) {
  395. dyingAlien(framePointer, (getAlienBlockPositionX()), (getAlienBlockPositionY()+104));
  396. setAliens(0, 44);
  397. collisionCleanUp((getAlienBlockPositionX()), (getAlienBlockPositionY()+104));
  398. }
  399. if(curAlienX + 32 < curTankX && curTankX < curAlienX + 64) {
  400. dyingAlien(framePointer, (getAlienBlockPositionX()+32), (getAlienBlockPositionY()+104));
  401. setAliens(0,45);
  402. collisionCleanUp((getAlienBlockPositionX()+32), (getAlienBlockPositionY()+104));
  403. }
  404. if(curAlienX + 64 < curTankX && curTankX < curAlienX + 96) {
  405. dyingAlien(framePointer, (getAlienBlockPositionX()+64), (getAlienBlockPositionY()+104));
  406. setAliens(0, 46);
  407. collisionCleanUp((getAlienBlockPositionX()+64), (getAlienBlockPositionY()+104));
  408. }
  409. if(curAlienX + 96 < curTankX && curTankX < curAlienX + 128) {
  410. dyingAlien(framePointer, (getAlienBlockPositionX()+96), (getAlienBlockPositionY()+104));
  411. setAliens(0, 47);
  412. collisionCleanUp((getAlienBlockPositionX()+96), (getAlienBlockPositionY()+104));
  413. }
  414. if(curAlienX + 128 < curTankX && curTankX < curAlienX + 160) {
  415. dyingAlien(framePointer, (getAlienBlockPositionX()+128), (getAlienBlockPositionY()+104));
  416. setAliens(0, 48);
  417. collisionCleanUp((getAlienBlockPositionX()+128), (getAlienBlockPositionY()+104));
  418. }
  419. if(curAlienX + 160 < curTankX && curTankX < curAlienX + 192) {
  420. dyingAlien(framePointer, (getAlienBlockPositionX()+160), (getAlienBlockPositionY()+104));
  421. setAliens(0, 49);
  422. collisionCleanUp((getAlienBlockPositionX()+160), (getAlienBlockPositionY()+104));
  423. }
  424. if(curAlienX + 192 < curTankX && curTankX < curAlienX + 224) {
  425. dyingAlien(framePointer, (getAlienBlockPositionX()+192), (getAlienBlockPositionY()+104));
  426. setAliens(0, 50);
  427. collisionCleanUp((getAlienBlockPositionX()+192), (getAlienBlockPositionY()+104));
  428. }
  429. if(curAlienX + 224 < curTankX && curTankX < curAlienX + 256) {
  430. dyingAlien(framePointer, (getAlienBlockPositionX()+224), (getAlienBlockPositionY()+104));
  431. setAliens(0, 51);
  432. collisionCleanUp((getAlienBlockPositionX()+224), (getAlienBlockPositionY()+104));
  433. }
  434. if(curAlienX + 256 < curTankX && curTankX < curAlienX + 288) {
  435. dyingAlien(framePointer, (getAlienBlockPositionX()+256), (getAlienBlockPositionY()+104));
  436. setAliens(0, 52);
  437. collisionCleanUp((getAlienBlockPositionX()+256), (getAlienBlockPositionY()+104));
  438. }
  439. if(curAlienX + 288 < curTankX && curTankX < curAlienX + 320) {
  440. dyingAlien(framePointer, (getAlienBlockPositionX()+288), (getAlienBlockPositionY()+104));
  441. setAliens(0, 53);
  442. collisionCleanUp((getAlienBlockPositionX()+288), (getAlienBlockPositionY()+104));
  443. }
  444. if(curAlienX + 320 < curTankX && curTankX < curAlienX + 352) {
  445. dyingAlien(framePointer, (getAlienBlockPositionX()+320), (getAlienBlockPositionY()+104));
  446. setAliens(0, 54);
  447. collisionCleanUp((getAlienBlockPositionX()+320), (getAlienBlockPositionY()+104));
  448. }
  449. updateMyscore(10);
  450. updateNumber(framePointer);
  451. }
  452. }
  453. }
  454. void alienSpeed() {
  455. short* aliens = getAliens();
  456. int i;
  457. int total = 0;
  458. for(i = 0; i < 55; i++) {
  459. if(aliens[i] == 1) {
  460. total++;
  461. }
  462. }
  463. if(total < 55) {
  464. speed = 30;
  465. }
  466. if(total < 44) {
  467. speed = 28;
  468. }
  469. if(total < 33) {
  470. speed = 26;
  471. }
  472. if(total < 22) {
  473. speed = 24;
  474. }
  475. if(total < 11) {
  476. speed = 20;
  477. }
  478. if(total == 0) {
  479. gameOver(framePointer);
  480. }
  481. }
  482. void timer_interrupt_handler() {
  483. onesec++;
  484. fifteenms++;
  485. fivems++;
  486. tenms++;
  487. mytimer++;
  488. if(mytimer == 5000){
  489. mytimer = 0;
  490. xil_printf("my counter value: %d\r\n", mycounter);
  491. }
  492. if(onesec == 100) {
  493. onesec = 0;
  494. }
  495. if(fifteenms == speed) {
  496. fifteenms = 0;
  497. incrementAlienSoundCadence();
  498. moveAlien(framePointer);
  499. renderAlien(framePointer);
  500. if(getAlienBlockPositionY() > 240){
  501. int col;
  502. for(col=0; col<640; col++) {
  503. if(framePointer[360*640 + col] == 0x00FFFFFF) {
  504. gameOver(framePointer);
  505. }
  506. }
  507. }
  508. }
  509. if(tenms == 2){
  510. tenms = 0;
  511. if(getUfoState() == 1){
  512. moveUfo(framePointer, getUfoDirection());
  513. renderUfo(framePointer);
  514. }
  515. }
  516. if(fivems == 1) {
  517. fivems = 0;
  518. if(getTankBulletState() == 1 || getAlien1BulletState() == 1 || getAlien2BulletState() == 1 || \
  519. getAlien3BulletState() == 1 || getAlien4BulletState() == 1) {
  520. int i;
  521. for(i = 0; i < 3; i++) {
  522. moveBullets(framePointer);
  523. renderBullet(framePointer);
  524. }
  525. if(getAlien1BulletState()){
  526. selectBlock(framePointer, 1);
  527. }
  528. if(getAlien2BulletState()){
  529. selectBlock(framePointer, 2);
  530. }
  531. if(getAlien3BulletState()){
  532. selectBlock(framePointer, 3);
  533. }
  534. if(getAlien4BulletState()){
  535. selectBlock(framePointer, 4);
  536. }
  537. if(getTankBulletState()){
  538. selectBlock(framePointer, 5);
  539. }
  540. }
  541. }
  542. }
  543. // This is invoked each time there is a change in the button state (result of a push or a bounce).
  544. void pb_interrupt_handler() {
  545. // Clear the GPIO interrupt.
  546. XGpio_InterruptGlobalDisable(&gpPB); // Turn off all PB interrupts for now.
  547. currentButtonState = XGpio_DiscreteRead(&gpPB, 1); // Get the current state of the buttons.
  548. if(currentButtonState == 16) {
  549. increaseVolume();
  550. }
  551. if(currentButtonState == 4) {
  552. decreaseVolume();
  553. }
  554. XGpio_InterruptClear(&gpPB, 0xFFFFFFFF); // Ack the PB interrupt.
  555. XGpio_InterruptGlobalEnable(&gpPB); // Re-enable PB interrupts.
  556. }
  557. // Main interrupt handler
  558. void interrupt_handler_dispatcher(void* ptr) {
  559. int intc_status = XIntc_GetIntrStatus(XPAR_INTC_0_BASEADDR);
  560. // Check the AC97. Just one method - no need for a separate handler
  561. if (intc_status & XPAR_AXI_AC97_0_INTERRUPT_MASK) {
  562. XIntc_AckIntr(XPAR_INTC_0_BASEADDR, XPAR_AXI_AC97_0_INTERRUPT_MASK);
  563. fillSound();
  564. }
  565. // Check the FIT interrupt first.
  566. if (intc_status & XPAR_FIT_TIMER_0_INTERRUPT_MASK){
  567. XIntc_AckIntr(XPAR_INTC_0_BASEADDR, XPAR_FIT_TIMER_0_INTERRUPT_MASK);
  568. timer_interrupt_handler();
  569. }
  570. // Check the push buttons.
  571. if (intc_status & XPAR_PUSH_BUTTONS_5BITS_IP2INTC_IRPT_MASK){
  572. XIntc_AckIntr(XPAR_INTC_0_BASEADDR, XPAR_PUSH_BUTTONS_5BITS_IP2INTC_IRPT_MASK);
  573. pb_interrupt_handler();
  574. }
  575. }
  576. int main() {
  577. init_platform(); // Necessary for all programs.
  578. // Initialize the GPIO peripherals.
  579. XAC97_HardReset(XPAR_AXI_AC97_0_BASEADDR); //Reset XAC97
  580. XAC97_WriteReg(XPAR_AXI_AC97_0_BASEADDR, AC97_MasterVol, AC97_VOL_ATTN_23_5_DB); // Set default volume
  581. XAC97_WriteReg(XPAR_AXI_AC97_0_BASEADDR, AC97_ExtendedAudioStat, 1);
  582. XAC97_WriteReg(XPAR_AXI_AC97_0_BASEADDR,AC97_PCM_DAC_Rate,AC97_PCM_RATE_11025_HZ);
  583. XAC97_mSetControl(XPAR_AXI_AC97_0_BASEADDR, AC97_ENABLE_IN_FIFO_INTERRUPT); //Enable AC97 Interrupts
  584. //interrupt code from clock lab
  585. int success;
  586. success = XGpio_Initialize(&gpPB, XPAR_PUSH_BUTTONS_5BITS_DEVICE_ID);
  587. // Set the push button peripheral to be inputs.
  588. XGpio_SetDataDirection(&gpPB, 1, 0x0000001F);
  589. // Enable the global GPIO interrupt for push buttons.
  590. XGpio_InterruptGlobalEnable(&gpPB);
  591. // Enable all interrupts in the push button peripheral.
  592. XGpio_InterruptEnable(&gpPB, 0xFFFFFFFF);
  593. microblaze_register_handler(interrupt_handler_dispatcher, NULL);
  594. XIntc_EnableIntr(XPAR_INTC_0_BASEADDR,
  595. (XPAR_FIT_TIMER_0_INTERRUPT_MASK | XPAR_PUSH_BUTTONS_5BITS_IP2INTC_IRPT_MASK |XPAR_AXI_AC97_0_INTERRUPT_MASK ));
  596. XIntc_MasterEnable(XPAR_INTC_0_BASEADDR);
  597. microblaze_enable_interrupts();
  598. //end of interrupt code from clock lab
  599. int Status; // Keep track of success/failure of system function calls.
  600. XAxiVdma videoDMAController;
  601. // There are 3 steps to initializing the vdma driver and IP.
  602. // Step 1: lookup the memory structure that is used to access the vdma driver.
  603. XAxiVdma_Config * VideoDMAConfig = XAxiVdma_LookupConfig(XPAR_AXI_VDMA_0_DEVICE_ID);
  604. // Step 2: Initialize the memory structure and the hardware.
  605. if(XST_FAILURE == XAxiVdma_CfgInitialize(&videoDMAController, VideoDMAConfig, XPAR_AXI_VDMA_0_BASEADDR)) {
  606. xil_printf("VideoDMA Did not initialize.\r\n");
  607. }
  608. // Step 3: (optional) set the frame store number.
  609. if(XST_FAILURE == XAxiVdma_SetFrmStore(&videoDMAController, 2, XAXIVDMA_READ)) {
  610. xil_printf("Set Frame Store Failed.");
  611. }
  612. // Initialization is complete at this point.
  613. // Setup the frame counter. We want two read frames. We don't need any write frames but the
  614. // function generates an error if you set the write frame count to 0. We set it to 2
  615. // but ignore it because we don't need a write channel at all.
  616. XAxiVdma_FrameCounter myFrameConfig;
  617. myFrameConfig.ReadFrameCount = 2;
  618. myFrameConfig.ReadDelayTimerCount = 10;
  619. myFrameConfig.WriteFrameCount =2;
  620. myFrameConfig.WriteDelayTimerCount = 10;
  621. Status = XAxiVdma_SetFrameCounter(&videoDMAController, &myFrameConfig);
  622. if (Status != XST_SUCCESS) {
  623. xil_printf("Set frame counter failed %d\r\n", Status);
  624. if(Status == XST_VDMA_MISMATCH_ERROR)
  625. xil_printf("DMA Mismatch Error\r\n");
  626. }
  627. // Now we tell the driver about the geometry of our frame buffer and a few other things.
  628. // Our image is 480 x 640.
  629. XAxiVdma_DmaSetup myFrameBuffer;
  630. myFrameBuffer.VertSizeInput = 480; // 480 vertical pixels.
  631. myFrameBuffer.HoriSizeInput = 640*4; // 640 horizontal (32-bit pixels).
  632. myFrameBuffer.Stride = 640*4; // Dont' worry about the rest of the values.
  633. myFrameBuffer.FrameDelay = 0;
  634. myFrameBuffer.EnableCircularBuf=1;
  635. myFrameBuffer.EnableSync = 0;
  636. myFrameBuffer.PointNum = 0;
  637. myFrameBuffer.EnableFrameCounter = 0;
  638. myFrameBuffer.FixedFrameStoreAddr = 0;
  639. if(XST_FAILURE == XAxiVdma_DmaConfig(&videoDMAController, XAXIVDMA_READ, &myFrameBuffer)) {
  640. xil_printf("DMA Config Failed\r\n");
  641. }
  642. // We need to give the frame buffer pointers to the memory that it will use. This memory
  643. // is where you will write your video data. The vdma IP/driver then streams it to the HDMI
  644. // IP.
  645. myFrameBuffer.FrameStoreStartAddr[0] = FRAME_BUFFER_0_ADDR;
  646. if(XST_FAILURE == XAxiVdma_DmaSetBufferAddr(&videoDMAController, XAXIVDMA_READ,
  647. myFrameBuffer.FrameStoreStartAddr)) {
  648. xil_printf("DMA Set Address Failed Failed\r\n");
  649. }
  650. // Print a sanity message if you get this far.
  651. xil_printf("Woohoo! I made it through initialization.\n\r");
  652. // Now, let's get ready to start displaying some stuff on the screen.
  653. // The variables framePointer and framePointer1 are just pointers to the base address
  654. // of frame 0 and frame 1.
  655. //unsigned int * framePointer = (unsigned int *) FRAME_BUFFER_0_ADDR;
  656. int row = 0;
  657. int col = 0;
  658. for(row=0; row<480; row++) {
  659. for(col=0; col<640; col++) {
  660. if(row > 460 && row < 464) {
  661. framePointer[row*640 + col] = 0x0000FF00;
  662. }
  663. else {
  664. framePointer[row*640 + col] = 0x00000000;
  665. }
  666. }
  667. }
  668. // This tells the HDMI controller the resolution of your display (there must be a better way to do this).
  669. XIo_Out32(XPAR_AXI_HDMI_0_BASEADDR, 640*480);
  670. // Start the DMA for the read channel only.
  671. if(XST_FAILURE == XAxiVdma_DmaStart(&videoDMAController, XAXIVDMA_READ)){
  672. xil_printf("DMA START FAILED\r\n");
  673. }
  674. int frameIndex = 0;
  675. // We have two frames, let's park on frame 0. Use frameIndex to index them.
  676. // Note that you have to start the DMA process before parking on a frame.
  677. if (XST_FAILURE == XAxiVdma_StartParking(&videoDMAController, frameIndex, XAXIVDMA_READ)) {
  678. xil_printf("vdma parking failed\n\r");
  679. }
  680. setAlienBlockPosition(0, 120);
  681. setTankPosition(300, 420);
  682. renderTank(framePointer);
  683. renderAlien(framePointer);
  684. renderBunker(framePointer);
  685. renderScore(framePointer);
  686. renderNumber(framePointer);
  687. renderTanks(framePointer);
  688. alienSpeed();
  689. while (gameState) {
  690. mycounter++;
  691. alienCollision();
  692. int nesButtonState = NES_mReadReg(XPAR_NES_0_BASEADDR, 0);
  693. if(nesButtonState == 2) {
  694. xil_printf("we are in left\r\n");
  695. moveTankL(framePointer);
  696. renderTank(framePointer);
  697. }
  698. if(nesButtonState == 1) {
  699. xil_printf("we are in right\r\n");
  700. moveTankR(framePointer);
  701. renderTank(framePointer);
  702. }
  703. if(nesButtonState == 4) {
  704. xil_printf("we are in mid\r\n");
  705. if(getTankBulletState() == 1) {
  706. continue;
  707. }
  708. else {
  709. setHaveTankBulletSound(1);
  710. setTankBulletState(1);
  711. initialTankBulletPosition();
  712. renderBullet(framePointer);
  713. }
  714. }
  715. if(nesButtonState == 6) {
  716. xil_printf("we are in mid\r\n");
  717. moveTankL(framePointer);
  718. renderTank(framePointer);
  719. if(getTankBulletState() == 1) {
  720. continue;
  721. }
  722. else {
  723. setHaveTankBulletSound(1);
  724. setTankBulletState(1);
  725. initialTankBulletPosition();
  726. renderBullet(framePointer);
  727. }
  728. }
  729. if(nesButtonState == 5) {
  730. xil_printf("we are in mid\r\n");
  731. moveTankR(framePointer);
  732. renderTank(framePointer);
  733. if(getTankBulletState() == 1) {
  734. continue;
  735. }
  736. else {
  737. setHaveTankBulletSound(1);
  738. setTankBulletState(1);
  739. initialTankBulletPosition();
  740. renderBullet(framePointer);
  741. }
  742. }
  743. int ufo_show = rand() % 3000;
  744. if(ufo_show == 100){
  745. if(getUfoState() == 0){
  746. setHaveSpaceShipSound(1);
  747. setUfoState(1);
  748. setUfoDirection();
  749. if(getUfoDirection()){ // getUfoDirection = 1 means ufo going left
  750. setUfoPositionX(610);
  751. setUfoPositionY(45);
  752. renderUfo(framePointer);
  753. }
  754. else{ // getUfoDirection = 0 means ufo going right
  755. setUfoPositionX(0);
  756. setUfoPositionY(45);
  757. renderUfo(framePointer);
  758. }
  759. }
  760. }
  761. int alienShot = rand() % 100000; // this controls how frequent bullets are fired by aliens
  762. if(alienShot == 50) { //key 3
  763. int i = 0;
  764. int j = rand() % 4;
  765. int k[4] = {getAlien1BulletState(), getAlien2BulletState(), getAlien3BulletState(), getAlien4BulletState()};
  766. for(i = 0; i < 16; i ++) {
  767. if(k[j] == 0) {
  768. break;
  769. }
  770. else {
  771. j = rand()%4;
  772. }
  773. }
  774. int bottomAlien = 0;
  775. short* aliens = getAliens();
  776. int a;
  777. while(bottomAlien == 0) {
  778. a = rand () % 54;
  779. if(aliens[a] == 1 && isBottom(a)) {
  780. bottomAlien = 1;
  781. }
  782. }
  783. int position = whatRow(a);
  784. int x = a % 11;
  785. switch(j){
  786. case 0:
  787. if(getAlien1BulletState() == 0) {
  788. setAlien1BulletState(1);
  789. setAlien1BulletPosition(((getAlienBlockPositionX()+10) + 32 * x), getAlienBlockPositionY()+position);
  790. renderBullet(framePointer);
  791. }
  792. break;
  793. case 1:
  794. if(getAlien2BulletState() == 0) {
  795. setAlien2BulletState(1);
  796. setAlien2BulletPosition(((getAlienBlockPositionX()+10) + 32 * x), getAlienBlockPositionY()+position);
  797. renderBullet(framePointer);
  798. }
  799. break;
  800. case 2:
  801. if(getAlien3BulletState() == 0) {
  802. setAlien3BulletState(1);
  803. setAlien3BulletPosition(((getAlienBlockPositionX()+10) + 32 * x), getAlienBlockPositionY()+position);
  804. renderBullet(framePointer);
  805. }
  806. break;
  807. case 3:
  808. if(getAlien4BulletState() == 0) {
  809. setAlien4BulletState(1);
  810. setAlien4BulletPosition(((getAlienBlockPositionX()+10) + 32 * x), getAlienBlockPositionY()+position);
  811. renderBullet(framePointer);
  812. }
  813. break;
  814. default :
  815. break;
  816. }
  817. }
  818. }
  819. cleanup_platform();
  820. return 0;
  821. }