MBROT.C 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #define word char
  2. #include "LIB/MATH.C"
  3. #include "LIB/STDLIB.C"
  4. #include "LIB/SYS.C"
  5. #include "LIB/GFX.C"
  6. #define SCREEN_WIDTH 320
  7. #define SCREEN_HEIGHT 240
  8. #define FB_ADDR 0xD00000
  9. // Framebuffer. fb[Y][X] (bottom right is [239][319])
  10. char (*fb)[SCREEN_WIDTH] = (char (*)[SCREEN_WIDTH]) FB_ADDR;
  11. char MBROT_r[] = {224, 224, 224, 224, 224, 224, 224, 224, 192, 160, 128, 96, 64, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 64, 96, 128, 160, 192, 224, 0};
  12. char MBROT_g[] = {0, 32, 64, 96, 128, 160, 192, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 192, 160, 128, 96, 64, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  13. char MBROT_b[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 64, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 0};
  14. word MBROT_xmin = -8601;
  15. word MBROT_xmax = 2867;
  16. word MBROT_ymin = -4915;
  17. word MBROT_ymax = 4915;
  18. word MBROT_maxiter = 32;
  19. // Render mandelbrot on display
  20. // INPUT:
  21. // r4 = dx
  22. // r5 = dy
  23. void MBROT_render(word dx, word dy)
  24. {
  25. asm(
  26. "; backup registers\n"
  27. "push r1\n"
  28. "push r2\n"
  29. "push r3\n"
  30. "push r4\n"
  31. "push r5\n"
  32. "push r6\n"
  33. "push r7\n"
  34. "push r8\n"
  35. "push r9\n"
  36. "push r10\n"
  37. "push r11\n"
  38. "push r12\n"
  39. "push r13\n"
  40. "push r14\n"
  41. "push r15\n"
  42. );
  43. asm(
  44. "; r13,r14,r15 = temp reg\n"
  45. "load 0 r1 ; r1 = row\n"
  46. "load 0 r2 ; r2 = pixel\n"
  47. "load 0 r3 ; r3 = cx\n"
  48. "; r4 = dx\n"
  49. "; r5 = dy\n"
  50. "load 0 r6 ; r6 = x\n"
  51. "load 0 r7 ; r7 = y\n"
  52. "load 0 r8 ; r8 = x2\n"
  53. "load 0 r9 ; r9 = y2\n"
  54. "load 0 r10 ; r10 = iter\n"
  55. "addr2reg MBROT_ymin r15\n"
  56. "read 0 r15 r11 ; r11 = cy\n"
  57. "load32 0xD00000 r12 ; r12 = fb[] at 0xD00000\n"
  58. "MBROT_whileCyLoop:\n"
  59. "; while(cy <= MBROT_ymax)\n"
  60. "addr2reg MBROT_ymax r15\n"
  61. "read 0 r15 r15\n"
  62. "bles r11 r15 2\n"
  63. "jump MBROT_whileCyLoop_done\n"
  64. "addr2reg MBROT_xmin r15\n"
  65. "read 0 r15 r3 ; cx = MBROT_xmin;\n"
  66. "MBROT_whileCxLoop:\n"
  67. "; while(cx <= MBROT_xmax)\n"
  68. "addr2reg MBROT_xmax r15\n"
  69. "read 0 r15 r15\n"
  70. "bles r3 r15 2\n"
  71. "jump MBROT_whileCxLoop_done\n"
  72. "load 0 r6 ; x = 0\n"
  73. "load 0 r7 ; y = 0\n"
  74. "load 0 r8 ; x2 = 0\n"
  75. "load 0 r9 ; y2 = 0\n"
  76. "load 0 r10 ; iter = 0\n"
  77. "MBROT_whileIterLoop:\n"
  78. "; while(iter < MBROT_maxiter)\n"
  79. "addr2reg MBROT_maxiter r15\n"
  80. "read 0 r15 r15\n"
  81. "blts r10 r15 2\n"
  82. "jump MBROT_whileIterLoop_done\n"
  83. "; break if (x2 + y2 > 16384)\n"
  84. "add r8 r9 r14 ; x2 + y2\n"
  85. "load32 16384 r15\n"
  86. "bles r14 r15 2\n"
  87. "jump MBROT_whileIterLoop_done\n"
  88. "mults r6 r7 r7 ; y = x * y;\n"
  89. "shiftrs r7 11 r7 ; y = y >> 11;\n"
  90. "add r7 r11 r7 ; y += cy;\n"
  91. "sub r8 r9 r6 ; x = x2 - y2;\n"
  92. "add r6 r3 r6 ; x += cx;\n"
  93. "mults r6 r6 r8 ; x2 = x * x;\n"
  94. "shiftrs r8 12 r8 ; x2 = x2 >> 12;\n"
  95. "mults r7 r7 r9 ; y2 = y * y;\n"
  96. "shiftrs r9 12 r9 ; y2 = y2 >> 12;\n"
  97. "add r10 1 r10 ; iter++;\n"
  98. "jump MBROT_whileIterLoop\n"
  99. "MBROT_whileIterLoop_done:\n"
  100. "multu r1 320 r13 ; r13 = row * 320\n"
  101. "add r13 r2 r13 ; r13 += pixel\n"
  102. "add r13 r12 r13 ; r13 += fb base address\n"
  103. "; r14 = rgb pixel value\n"
  104. "addr2reg MBROT_r r15\n"
  105. "add r15 r10 r15 ; r15 = MBROT_r[iter]\n"
  106. "read 0 r15 r15\n"
  107. "shiftl r15 16 r14 ; r14 = r << 16\n"
  108. "addr2reg MBROT_g r15\n"
  109. "add r15 r10 r15 ; r15 = MBROT_g[iter]\n"
  110. "read 0 r15 r15\n"
  111. "shiftl r15 8 r15 ; r15 = g << 8\n"
  112. "add r15 r14 r14 ; r14 += g (shifted)\n"
  113. "addr2reg MBROT_b r15\n"
  114. "add r15 r10 r15 ; r15 = MBROT_b[iter]\n"
  115. "read 0 r15 r15\n"
  116. "add r15 r14 r14 ; r14 += b\n"
  117. ";write rgb value to pixel address\n"
  118. "write 0 r13 r14\n"
  119. "add r2 1 r2 ; pixel++;\n"
  120. "add r3 r4 r3 ; cx += dx;\n"
  121. "jump MBROT_whileCxLoop\n"
  122. "MBROT_whileCxLoop_done:\n"
  123. "add r1 1 r1 ; row++;\n"
  124. "load 0 r2 ; pixel = 0;\n"
  125. "add r11 r5 r11 ; cy += dy;\n"
  126. "jump MBROT_whileCyLoop\n"
  127. "MBROT_whileCyLoop_done:\n"
  128. );
  129. asm(
  130. "; restore registers\n"
  131. "pop r15\n"
  132. "pop r14\n"
  133. "pop r13\n"
  134. "pop r12\n"
  135. "pop r11\n"
  136. "pop r10\n"
  137. "pop r9\n"
  138. "pop r8\n"
  139. "pop r7\n"
  140. "pop r6\n"
  141. "pop r5\n"
  142. "pop r4\n"
  143. "pop r3\n"
  144. "pop r2\n"
  145. "pop r1\n"
  146. );
  147. }
  148. void initGraphics()
  149. {
  150. GFX_clearWindowtileTable();
  151. GFX_clearWindowpaletteTable();
  152. GFX_clearBGtileTable();
  153. GFX_clearBGpaletteTable();
  154. GFX_clearPXframebuffer();
  155. word* paletteAddress = (word*) 0xC00400;
  156. paletteAddress[1] = 0xC0;
  157. }
  158. void displayInfo()
  159. {
  160. char* xmin = "X-: ";
  161. if (MBROT_xmin < 0)
  162. xmin[3] = '-';
  163. itoa(MATH_abs(MBROT_xmin), &xmin[4]);
  164. GFX_printWindowColored(xmin, 18, 0, 1);
  165. char* xmax = "X+: ";
  166. if (MBROT_xmax < 0)
  167. xmax[3] = '-';
  168. itoa(MATH_abs(MBROT_xmax), &xmax[4]);
  169. GFX_printWindowColored(xmax, 18, 40, 1);
  170. char* ymin = "Y-: ";
  171. if (MBROT_ymin < 0)
  172. ymin[3] = '-';
  173. itoa(MATH_abs(MBROT_ymin), &ymin[4]);
  174. GFX_printWindowColored(ymin, 18, 120, 1);
  175. char* ymax = "Y+: ";
  176. if (MBROT_ymax < 0)
  177. ymax[3] = '-';
  178. itoa(MATH_abs(MBROT_ymax), &ymax[4]);
  179. GFX_printWindowColored(ymax, 18, 160, 1);
  180. }
  181. int main()
  182. {
  183. initGraphics();
  184. word dx = MATH_div((MBROT_xmax-MBROT_xmin),310);
  185. word dy = MATH_div((MBROT_ymax-MBROT_ymin),230);
  186. MBROT_render(dx, dy);
  187. while (1)
  188. {
  189. if (HID_FifoAvailable())
  190. {
  191. word c = HID_FifoRead();
  192. if (c == '=') // +
  193. {
  194. MBROT_xmin += MATH_div(MATH_abs(MBROT_xmin), 2);
  195. MBROT_xmax -= MATH_div(MATH_abs(MBROT_xmax), 2);
  196. MBROT_ymin += MATH_div(MATH_abs(MBROT_ymin), 2);
  197. MBROT_ymax -= MATH_div(MATH_abs(MBROT_ymax), 2);
  198. }
  199. if (c == '-') // -
  200. {
  201. MBROT_xmin -= MATH_div(MATH_abs(MBROT_xmin), 2);
  202. MBROT_xmax += MATH_div(MATH_abs(MBROT_xmax), 2);
  203. MBROT_ymin -= MATH_div(MATH_abs(MBROT_ymin), 2);
  204. MBROT_ymax += MATH_div(MATH_abs(MBROT_ymax), 2);
  205. }
  206. if (c == 256) // left
  207. {
  208. MBROT_xmin -= MATH_div(MATH_abs(MBROT_xmax-MBROT_xmin), 4) + 1;
  209. MBROT_xmax -= MATH_div(MATH_abs(MBROT_xmax-MBROT_xmin), 4) + 1;
  210. }
  211. if (c == 257) // right
  212. {
  213. MBROT_xmin += MATH_div(MATH_abs(MBROT_xmax-MBROT_xmin), 4) + 1;
  214. MBROT_xmax += MATH_div(MATH_abs(MBROT_xmax-MBROT_xmin), 4) + 1;
  215. }
  216. if (c == 258) // up
  217. {
  218. MBROT_ymin -= MATH_div(MATH_abs(MBROT_ymax-MBROT_ymin), 4) + 1;
  219. MBROT_ymax -= MATH_div(MATH_abs(MBROT_ymax-MBROT_ymin), 4) + 1;
  220. }
  221. if (c == 259) // down
  222. {
  223. MBROT_ymin += MATH_div(MATH_abs(MBROT_ymax-MBROT_ymin), 4) + 1;
  224. MBROT_ymax += MATH_div(MATH_abs(MBROT_ymax-MBROT_ymin), 4) + 1;
  225. }
  226. if (c == 27) // escape
  227. {
  228. GFX_clearPXframebuffer();
  229. return 'q';
  230. }
  231. word dx = MATH_div((MBROT_xmax-MBROT_xmin),310);
  232. word dy = MATH_div((MBROT_ymax-MBROT_ymin),230);
  233. displayInfo();
  234. MBROT_render(dx, dy);
  235. }
  236. }
  237. return 'q';
  238. }
  239. void interrupt()
  240. {
  241. // handle all interrupts
  242. word i = getIntID();
  243. switch(i)
  244. {
  245. case INTID_TIMER1:
  246. timer1Value = 1; // notify ending of timer1
  247. break;
  248. case INTID_TIMER2:
  249. break;
  250. case INTID_UART0:
  251. break;
  252. case INTID_GPU:
  253. break;
  254. case INTID_TIMER3:
  255. break;
  256. case INTID_PS2:
  257. break;
  258. case INTID_UART1:
  259. break;
  260. case INTID_UART2:
  261. break;
  262. }
  263. }