1
0

gfx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Graphics library
  3. * Mostly Assembly code, because efficiency in both space and time
  4. * More complex functions are written in C
  5. * Int arguments from C are stored in order of r4, r5, r6, r7
  6. * Return value in r2, but should be written on stack using write -4 r14 r2 (add variable in C)
  7. */
  8. // uses math.c
  9. #define GFX_WINDOW_PATTERN_ADDR 0xC01420
  10. #define GFX_WINDOW_PALETTE_ADDR 0xC01C20
  11. #define GFX_CURSOR_ASCII 219
  12. word GFX_cursor = 0;
  13. // Prints to screen in window plane, with color, data is accessed in words
  14. // INPUT:
  15. // r4 = address of data to print
  16. // r5 = length of data
  17. // r6 = position on screen
  18. // r7 = palette index
  19. void GFX_printWindowColored(word addr, word len, word pos, word palette)
  20. {
  21. asm(
  22. "; backup registers\n"
  23. "push r1\n"
  24. "push r2\n"
  25. "push r3\n"
  26. "push r4\n"
  27. "push r5\n"
  28. "push r6\n"
  29. "push r7\n"
  30. "push r8\n"
  31. "push r9\n"
  32. );
  33. asm(
  34. "; vram address\n"
  35. "load32 0xC01420 r9 ; r9 = vram addr 1056+4096 0xC01420\n"
  36. "; loop variables\n"
  37. "load 0 r1 ; r1 = loopvar\n"
  38. "or r9 r0 r3 ; r3 = vram addr with offset\n"
  39. "or r4 r0 r2 ; r2 = data addr with offset\n"
  40. "add r6 r3 r3 ; apply offset from r6\n"
  41. "; copy loop\n"
  42. "GFX_printWindowColoredLoop:\n"
  43. " read 0 r2 r8 ; read 32 bits\n"
  44. " write 0 r3 r8 ; write char to vram\n"
  45. " write 2048 r3 r7 ; write palette index to vram\n"
  46. " add r3 1 r3 ; incr vram address\n"
  47. " add r2 1 r2 ; incr data address\n"
  48. " add r1 1 r1 ; incr counter\n"
  49. " bge r1 r5 2 ; keep looping until all data is copied\n"
  50. " jump GFX_printWindowColoredLoop\n"
  51. );
  52. asm(
  53. "; restore registers\n"
  54. "pop r9\n"
  55. "pop r8\n"
  56. "pop r7\n"
  57. "pop r6\n"
  58. "pop r5\n"
  59. "pop r4\n"
  60. "pop r3\n"
  61. "pop r2\n"
  62. "pop r1\n"
  63. );
  64. }
  65. // Prints to screen in background plane, with color, data is accessed in words
  66. // INPUT:
  67. // r4 = address of data to print
  68. // r5 = length of data
  69. // r6 = position on screen
  70. // r7 = palette index
  71. void GFX_printBGColored(word addr, word len, word pos, word palette)
  72. {
  73. asm(
  74. "; backup registers\n"
  75. "push r1\n"
  76. "push r2\n"
  77. "push r3\n"
  78. "push r4\n"
  79. "push r5\n"
  80. "push r6\n"
  81. "push r7\n"
  82. "push r8\n"
  83. "push r9\n"
  84. );
  85. asm(
  86. "; vram address\n"
  87. "load32 0xC00420 r9 ; r9 = vram addr 1056 0xC00420\n"
  88. "; loop variables\n"
  89. "load 0 r1 ; r1 = loopvar\n"
  90. "or r9 r0 r3 ; r3 = vram addr with offset\n"
  91. "or r4 r0 r2 ; r2 = data addr with offset\n"
  92. "add r6 r3 r3 ; apply offset from r6\n"
  93. "; copy loop\n"
  94. "GFX_printBGColoredLoop:\n"
  95. " read 0 r2 r8 ; read 32 bits\n"
  96. " write 0 r3 r8 ; write char to vram\n"
  97. " write 2048 r3 r7 ; write palette index to vram\n"
  98. " add r3 1 r3 ; incr vram address\n"
  99. " add r2 1 r2 ; incr data address\n"
  100. " add r1 1 r1 ; incr counter\n"
  101. " bge r1 r5 2 ; keep looping until all data is copied\n"
  102. " jump GFX_printBGColoredLoop\n"
  103. );
  104. asm(
  105. "; restore registers\n"
  106. "pop r9\n"
  107. "pop r8\n"
  108. "pop r7\n"
  109. "pop r6\n"
  110. "pop r5\n"
  111. "pop r4\n"
  112. "pop r3\n"
  113. "pop r2\n"
  114. "pop r1\n"
  115. );
  116. }
  117. // Loads entire pattern table
  118. // INPUT:
  119. // r4 = address of pattern table to copy
  120. void GFX_copyPatternTable(word addr)
  121. {
  122. asm(
  123. "; backup registers\n"
  124. "push r1\n"
  125. "push r2\n"
  126. "push r3\n"
  127. "push r4\n"
  128. "push r5\n"
  129. "push r6\n"
  130. "push r7\n"
  131. "; vram address\n"
  132. "load32 0xC00000 r2 ; r2 = vram addr 0 0xC00000\n"
  133. "; loop variables\n"
  134. "load 0 r3 ; r3 = loopvar\n"
  135. "load 1024 r5 ; r5 = loopmax\n"
  136. "or r2 r0 r1 ; r1 = vram addr with offset\n"
  137. "add r4 3 r6 ; r6 = ascii addr with offset\n"
  138. "; copy loop\n"
  139. "GFX_initPatternTableLoop:\n"
  140. " read 0 r6 r7 ; copy ascii to vram\n"
  141. " write 0 r1 r7 ;\n"
  142. " add r1 1 r1 ; incr vram address\n"
  143. " add r6 1 r6 ; incr ascii address\n"
  144. " add r3 1 r3 ; incr counter\n"
  145. " beq r3 r5 2 ; keep looping until all data is copied\n"
  146. " jump GFX_initPatternTableLoop\n"
  147. "; restore registers\n"
  148. "pop r7\n"
  149. "pop r6\n"
  150. "pop r5\n"
  151. "pop r4\n"
  152. "pop r3\n"
  153. "pop r2\n"
  154. "pop r1\n"
  155. );
  156. }
  157. // Loads entire palette table
  158. // INPUT:
  159. // r4 = address of palette table to copy
  160. void GFX_copyPaletteTable(word addr)
  161. {
  162. asm(
  163. "; backup registers\n"
  164. "push r1\n"
  165. "push r2\n"
  166. "push r3\n"
  167. "push r4\n"
  168. "push r5\n"
  169. "push r6\n"
  170. "push r7\n"
  171. "; vram address\n"
  172. "load32 0xC00400 r2 ; r2 = vram addr 1024 0xC00400\n"
  173. "; loop variables\n"
  174. "load 0 r3 ; r3 = loopvar\n"
  175. "load 32 r5 ; r5 = loopmax\n"
  176. "or r2 r0 r1 ; r1 = vram addr with offset\n"
  177. "add r4 3 r6 ; r6 = palette addr with offset\n"
  178. "; copy loop\n"
  179. "GFX_initPaletteTableLoop:\n"
  180. " read 0 r6 r7 ; copy ascii to vram\n"
  181. " write 0 r1 r7 ;\n"
  182. " add r1 1 r1 ; incr vram address\n"
  183. " add r6 1 r6 ; incr palette address\n"
  184. " add r3 1 r3 ; incr counter\n"
  185. " beq r3 r5 2 ; keep looping until all data is copied\n"
  186. " jump GFX_initPaletteTableLoop\n"
  187. "; restore registers\n"
  188. "pop r7\n"
  189. "pop r6\n"
  190. "pop r5\n"
  191. "pop r4\n"
  192. "pop r3\n"
  193. "pop r2\n"
  194. "pop r1\n"
  195. );
  196. }
  197. // Clear BG tile table
  198. void GFX_clearBGtileTable()
  199. {
  200. asm(
  201. "; backup registers\n"
  202. "push r1\n"
  203. "push r2\n"
  204. "push r3\n"
  205. "push r4\n"
  206. "push r5\n"
  207. "; vram address\n"
  208. "load32 0xC00420 r1 ; r1 = vram addr 1056 0xC00420\n"
  209. "; loop variables\n"
  210. "load 0 r3 ; r3 = loopvar\n"
  211. "load 2048 r4 ; r4 = loopmax\n"
  212. "or r1 r0 r5 ; r5 = vram addr with offset\n"
  213. "; copy loop\n"
  214. "GFX_clearBGtileTableLoop:\n"
  215. " write 0 r5 r0 ; clear tile\n"
  216. " add r5 1 r5 ; incr vram address\n"
  217. " add r3 1 r3 ; incr counter\n"
  218. " beq r3 r4 2 ; keep looping until all tiles are cleared\n"
  219. " jump GFX_clearBGtileTableLoop\n"
  220. "; restore registers\n"
  221. "pop r5\n"
  222. "pop r4\n"
  223. "pop r3\n"
  224. "pop r2\n"
  225. "pop r1\n"
  226. );
  227. }
  228. // Clear BG palette table
  229. void GFX_clearBGpaletteTable()
  230. {
  231. asm(
  232. "; backup registers\n"
  233. "push r1\n"
  234. "push r2\n"
  235. "push r3\n"
  236. "push r4\n"
  237. "push r5\n"
  238. "; vram address\n"
  239. "load32 0xC00C20 r1 ; r1 = vram addr 1056+2048 0xC00C20\n"
  240. "; loop variables\n"
  241. "load 0 r3 ; r3 = loopvar\n"
  242. "load 2048 r4 ; r4 = loopmax\n"
  243. "or r1 r0 r5 ; r5 = vram addr with offset\n"
  244. "; copy loop\n"
  245. "GFX_clearBGpaletteTableLoop:\n"
  246. " write 0 r5 r0 ; clear tile\n"
  247. " add r5 1 r5 ; incr vram address\n"
  248. " add r3 1 r3 ; incr counter\n"
  249. " beq r3 r4 2 ; keep looping until all tiles are cleared\n"
  250. " jump GFX_clearBGpaletteTableLoop\n"
  251. "; restore registers\n"
  252. "pop r5\n"
  253. "pop r4\n"
  254. "pop r3\n"
  255. "pop r2\n"
  256. "pop r1\n"
  257. );
  258. }
  259. // Clear Window tile table
  260. void GFX_clearWindowtileTable()
  261. {
  262. asm(
  263. "; backup registers\n"
  264. "push r1\n"
  265. "push r2\n"
  266. "push r3\n"
  267. "push r4\n"
  268. "push r5\n"
  269. "; vram address\n"
  270. "load32 0xC01420 r1 ; r1 = vram addr 1056+2048 0xC01420\n"
  271. "; loop variables\n"
  272. "load 0 r3 ; r3 = loopvar\n"
  273. "load 1920 r4 ; r4 = loopmax\n"
  274. "or r1 r0 r5 ; r5 = vram addr with offset\n"
  275. "; copy loop\n"
  276. "GFX_clearWindowtileTableLoop:\n"
  277. " write 0 r5 r0 ; clear tile\n"
  278. " add r5 1 r5 ; incr vram address\n"
  279. " add r3 1 r3 ; incr counter\n"
  280. " beq r3 r4 2 ; keep looping until all tiles are cleared\n"
  281. " jump GFX_clearWindowtileTableLoop\n"
  282. "; restore registers\n"
  283. "pop r5\n"
  284. "pop r4\n"
  285. "pop r3\n"
  286. "pop r2\n"
  287. "pop r1\n"
  288. );
  289. }
  290. // Clear Window palette table
  291. void GFX_clearWindowpaletteTable()
  292. {
  293. asm(
  294. "; backup registers\n"
  295. "push r1\n"
  296. "push r2\n"
  297. "push r3\n"
  298. "push r4\n"
  299. "push r5\n"
  300. "; vram address\n"
  301. "load32 0xC01C20 r1 ; r1 = vram addr 1056+2048 0xC01C20\n"
  302. "; loop variables\n"
  303. "load 0 r3 ; r3 = loopvar\n"
  304. "load 1920 r4 ; r4 = loopmax\n"
  305. "or r1 r0 r5 ; r5 = vram addr with offset\n"
  306. "; copy loop\n"
  307. "GFX_clearWindowpaletteTableLoop:\n"
  308. " write 0 r5 r0 ; clear tile\n"
  309. " add r5 1 r5 ; incr vram address\n"
  310. " add r3 1 r3 ; incr counter\n"
  311. " beq r3 r4 2 ; keep looping until all tiles are cleared\n"
  312. " jump GFX_clearWindowpaletteTableLoop\n"
  313. "; restore registers\n"
  314. "pop r5\n"
  315. "pop r4\n"
  316. "pop r3\n"
  317. "pop r2\n"
  318. "pop r1\n"
  319. );
  320. }
  321. // Clear Sprites
  322. void GFX_clearSprites()
  323. {
  324. asm(
  325. "; backup registers\n"
  326. "push r1\n"
  327. "push r2\n"
  328. "push r3\n"
  329. "push r4\n"
  330. "push r5\n"
  331. "; vram address\n"
  332. "load32 0xC02422 r1 ; r1 = vram addr 0xC02422\n"
  333. "; loop variables\n"
  334. "load 0 r3 ; r3 = loopvar\n"
  335. "load 64 r4 ; r4 = loopmax\n"
  336. "or r1 r0 r5 ; r5 = vram addr with offset\n"
  337. "; copy loop\n"
  338. "GFX_clearSpritesLoop:\n"
  339. " write 0 r5 r0 ; clear x\n"
  340. " write 1 r5 r0 ; clear y\n"
  341. " write 2 r5 r0 ; clear tile\n"
  342. " write 3 r5 r0 ; clear color+attrib\n"
  343. " add r5 4 r5 ; incr vram address by 4\n"
  344. " add r3 1 r3 ; incr counter\n"
  345. " beq r3 r4 2 ; keep looping until all tiles are cleared\n"
  346. " jump GFX_clearSpritesLoop\n"
  347. "; restore registers\n"
  348. "pop r5\n"
  349. "pop r4\n"
  350. "pop r3\n"
  351. "pop r2\n"
  352. "pop r1\n"
  353. );
  354. }
  355. // Clear parameters
  356. void GFX_clearParameters()
  357. {
  358. asm(
  359. "; backup registers\n"
  360. "push r1\n"
  361. "; vram address\n"
  362. "load32 0xC02420 r1 ; r1 = vram addr 0xC02420\n"
  363. "write 0 r1 r0 ; clear tile scroll\n"
  364. "write 1 r1 r0 ; clear fine scroll\n"
  365. "; restore registers\n"
  366. "pop r1\n"
  367. );
  368. }
  369. // Clear Pixel Engine framebuffer
  370. void GFX_clearPXframebuffer()
  371. {
  372. asm(
  373. "; backup registers\n"
  374. "push r1\n"
  375. "push r2\n"
  376. "push r3\n"
  377. "push r4\n"
  378. "push r5\n"
  379. "; vram address\n"
  380. "load32 0xD00000 r1 ; r1 = framebuffer base addr 0xD00000\n"
  381. "; loop variables\n"
  382. "load 0 r3 ; r3 = loopvar\n"
  383. "load32 76800 r4 ; r4 = loopmax\n"
  384. "or r1 r0 r5 ; r5 = fb addr with offset\n"
  385. "; copy loop\n"
  386. "GFX_clearPXframebufferLoop:\n"
  387. " write 0 r5 r0 ; clear pixel\n"
  388. " add r5 1 r5 ; incr vram address\n"
  389. " add r3 1 r3 ; incr counter\n"
  390. " beq r3 r4 2 ; keep looping until all pixels are cleared\n"
  391. " jump GFX_clearPXframebufferLoop\n"
  392. "; restore registers\n"
  393. "pop r5\n"
  394. "pop r4\n"
  395. "pop r3\n"
  396. "pop r2\n"
  397. "pop r1\n"
  398. );
  399. }
  400. // clears and initializes VRAM (excluding pattern and palette data table)
  401. void GFX_initVram()
  402. {
  403. GFX_clearBGtileTable();
  404. GFX_clearBGpaletteTable();
  405. GFX_clearWindowtileTable();
  406. GFX_clearWindowpaletteTable();
  407. GFX_clearSprites();
  408. GFX_clearParameters();
  409. }
  410. // Clear parameters
  411. void GFX_setHalfRes(word halfRes)
  412. {
  413. asm(
  414. "; vram address\n"
  415. "load32 0xC02749 r2 ; r2 = halfRes addr\n"
  416. "write 0 r2 r4 ; write arg to halfRes addr\n"
  417. );
  418. }
  419. // convert x and y to position for window table
  420. word GFX_WindowPosFromXY(word x, word y)
  421. {
  422. return y*40 + x;
  423. }
  424. // convert x and y to position for background table
  425. word GFX_BackgroundPosFromXY(word x, word y)
  426. {
  427. return y*64 + x;
  428. }
  429. // scrolls up screen, clearing last line
  430. void GFX_ScrollUp()
  431. {
  432. asm(
  433. "; backup registers\n"
  434. "push r1\n"
  435. "push r2\n"
  436. "push r3\n"
  437. "push r4\n"
  438. "; GFX_WINDOW_PATTERN_ADDR address\n"
  439. "load32 0xC01420 r1 ; r1 = window pattern addr 0xC01420\n"
  440. "load32 0 r2 ; r2 = loopvar\n"
  441. "load32 960 r3 ; r3 = loopmax\n"
  442. "GFX_scrollUpLoop:\n"
  443. " read 40 r1 r4 ; read r1+40 to tmp r4\n"
  444. " write 0 r1 r4 ; write tmp r4 to r1\n"
  445. " add r1 1 r1 ; incr addr r1\n"
  446. " add r2 1 r2 ; incr loopvar r2\n"
  447. " beq r2 r3 2 ; keep looping for r3 times\n"
  448. " jump GFX_scrollUpLoop\n"
  449. "load32 0 r2 ; r2 = loopvar\n"
  450. "load32 40 r3 ; r3 = loopmax\n"
  451. "GFX_clearLastLineLoop:\n"
  452. " write 0 r1 r0 ; clear at r1\n"
  453. " add r1 1 r1 ; incr addr r1\n"
  454. " add r2 1 r2 ; incr loopvar r2\n"
  455. " beq r2 r3 2 ; keep looping for r3 times\n"
  456. " jump GFX_clearLastLineLoop\n"
  457. "; restore registers\n"
  458. "pop r4\n"
  459. "pop r3\n"
  460. "pop r2\n"
  461. "pop r1\n"
  462. );
  463. }
  464. // Prints cursor character at cursor
  465. void GFX_printCursor()
  466. {
  467. // print character at cursor
  468. word *v = (word *) GFX_WINDOW_PATTERN_ADDR;
  469. *(v+GFX_cursor) = GFX_CURSOR_ASCII;
  470. }
  471. // Prints character to console
  472. // Handles scrolling, newline and backspace
  473. // Uses cursor from memory
  474. // TODO: could convert this to assembly for speed
  475. void GFX_PrintcConsole(char c)
  476. {
  477. // if newline
  478. if (c == 0xa)
  479. {
  480. // remove current cursor
  481. word *v = (word *) GFX_WINDOW_PATTERN_ADDR;
  482. *(v+GFX_cursor) = 0;
  483. // get next line number
  484. word nl = MATH_divU(GFX_cursor, 40) + 1;
  485. // multiply by 40 to get the correct line
  486. GFX_cursor = nl * 40;
  487. }
  488. // if backspace
  489. else if (c == 0x8)
  490. {
  491. // NOTE: by commenting this out it now is allowed to backspace to the previous line
  492. // This is done since the shell checks for valid backspaces now,
  493. // so we can remove multiline arguments
  494. // if we are not at the start of a line
  495. //if (MATH_mod(GFX_cursor, 40) != 0)
  496. // if we are not at the first character
  497. if ((unsigned int) GFX_cursor > 0)
  498. {
  499. // set current and previous character to 0
  500. word *v = (word *) GFX_WINDOW_PATTERN_ADDR;
  501. *(v+GFX_cursor) = 0;
  502. *(v+GFX_cursor-1) = 0;
  503. // decrement cursor
  504. GFX_cursor -= 1;
  505. }
  506. }
  507. // if\r
  508. else if (c == '\r')
  509. {
  510. // ignore
  511. }
  512. // else (character)
  513. else
  514. {
  515. // print character at cursor
  516. word *v = (word *) GFX_WINDOW_PATTERN_ADDR;
  517. *(v+GFX_cursor) = (word) c;
  518. // increment cursor
  519. GFX_cursor += 1;
  520. }
  521. // if we went offscreen, scroll screen up and set cursor to last line
  522. if ((unsigned int) GFX_cursor >= 1000)
  523. {
  524. GFX_ScrollUp();
  525. // set cursor to 960
  526. GFX_cursor = 960;
  527. }
  528. // add cursor at end
  529. GFX_printCursor();
  530. }
  531. // Prints string on console untill terminator
  532. // Does not add newline at end
  533. // TODO: could convert this to assembly for speed
  534. void GFX_PrintConsole(char* str)
  535. {
  536. char chr = *str; // first character of str
  537. while (chr != 0) // continue until null value
  538. {
  539. GFX_PrintcConsole((word)chr);
  540. str++; // go to next character address
  541. chr = *str; // get character from address
  542. }
  543. }
  544. // Clears console by removing all tiles and resetting the cursor
  545. void GFX_clearConsole()
  546. {
  547. GFX_clearWindowtileTable();
  548. GFX_clearBGtileTable();
  549. GFX_cursor = 0;
  550. }