GFX.C 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. // clears and initializes VRAM (excluding pattern and palette data table)
  370. void GFX_initVram()
  371. {
  372. GFX_clearBGtileTable();
  373. GFX_clearBGpaletteTable();
  374. GFX_clearWindowtileTable();
  375. GFX_clearWindowpaletteTable();
  376. GFX_clearSprites();
  377. GFX_clearParameters();
  378. }
  379. // convert x and y to position for window table
  380. word GFX_WindowPosFromXY(word x, word y)
  381. {
  382. return y*40 + x;
  383. }
  384. // convert x and y to position for background table
  385. word GFX_BackgroundPosFromXY(word x, word y)
  386. {
  387. return y*64 + x;
  388. }
  389. // scrolls up screen, clearing last line
  390. void GFX_ScrollUp()
  391. {
  392. asm(
  393. "; backup registers\n"
  394. "push r1\n"
  395. "push r2\n"
  396. "push r3\n"
  397. "push r4\n"
  398. "; GFX_WINDOW_PATTERN_ADDR address\n"
  399. "load32 0xC01420 r1 ; r1 = window pattern addr 0xC01420\n"
  400. "load32 0 r2 ; r2 = loopvar\n"
  401. "load32 960 r3 ; r3 = loopmax\n"
  402. "GFX_scrollUpLoop:\n"
  403. " read 40 r1 r4 ; read r1+40 to tmp r4\n"
  404. " write 0 r1 r4 ; write tmp r4 to r1\n"
  405. " add r1 1 r1 ; incr addr r1\n"
  406. " add r2 1 r2 ; incr loopvar r2\n"
  407. " beq r2 r3 2 ; keep looping for r3 times\n"
  408. " jump GFX_scrollUpLoop\n"
  409. "load32 0 r2 ; r2 = loopvar\n"
  410. "load32 40 r3 ; r3 = loopmax\n"
  411. "GFX_clearLastLineLoop:\n"
  412. " write 0 r1 r0 ; clear at r1\n"
  413. " add r1 1 r1 ; incr addr r1\n"
  414. " add r2 1 r2 ; incr loopvar r2\n"
  415. " beq r2 r3 2 ; keep looping for r3 times\n"
  416. " jump GFX_clearLastLineLoop\n"
  417. "; restore registers\n"
  418. "pop r4\n"
  419. "pop r3\n"
  420. "pop r2\n"
  421. "pop r1\n"
  422. );
  423. }
  424. // Prints cursor character at cursor
  425. void GFX_printCursor()
  426. {
  427. // print character at cursor
  428. word *v = (word *) GFX_WINDOW_PATTERN_ADDR;
  429. *(v+GFX_cursor) = GFX_CURSOR_ASCII;
  430. }
  431. // Prints character to console
  432. // Handles scrolling, newline and backspace
  433. // Uses cursor from memory
  434. // TODO: could convert this to assembly for speed
  435. void GFX_PrintcConsole(char c)
  436. {
  437. // if newline
  438. if (c == 0xa)
  439. {
  440. // remove current cursor
  441. word *v = (word *) GFX_WINDOW_PATTERN_ADDR;
  442. *(v+GFX_cursor) = 0;
  443. // get next line number
  444. word nl = MATH_divU(GFX_cursor, 40) + 1;
  445. // multiply by 40 to get the correct line
  446. GFX_cursor = nl * 40;
  447. }
  448. // if backspace
  449. else if (c == 0x8)
  450. {
  451. // NOTE: by commenting this out it now is allowed to backspace to the previous line
  452. // This is done since the shell checks for valid backspaces now,
  453. // so we can remove multiline arguments
  454. // if we are not at the start of a line
  455. //if (MATH_mod(GFX_cursor, 40) != 0)
  456. // if we are not at the first character
  457. if ((unsigned int) GFX_cursor > 0)
  458. {
  459. // set current and previous character to 0
  460. word *v = (word *) GFX_WINDOW_PATTERN_ADDR;
  461. *(v+GFX_cursor) = 0;
  462. *(v+GFX_cursor-1) = 0;
  463. // decrement cursor
  464. GFX_cursor -= 1;
  465. }
  466. }
  467. // if\r
  468. else if (c == '\r')
  469. {
  470. // ignore
  471. }
  472. // else (character)
  473. else
  474. {
  475. // print character at cursor
  476. word *v = (word *) GFX_WINDOW_PATTERN_ADDR;
  477. *(v+GFX_cursor) = (word) c;
  478. // increment cursor
  479. GFX_cursor += 1;
  480. }
  481. // if we went offscreen, scroll screen up and set cursor to last line
  482. if ((unsigned int) GFX_cursor >= 1000)
  483. {
  484. GFX_ScrollUp();
  485. // set cursor to 960
  486. GFX_cursor = 960;
  487. }
  488. // add cursor at end
  489. GFX_printCursor();
  490. }
  491. // Prints string on console untill terminator
  492. // Does not add newline at end
  493. // TODO: could convert this to assembly for speed
  494. void GFX_PrintConsole(char* str)
  495. {
  496. char chr = *str; // first character of str
  497. while (chr != 0) // continue until null value
  498. {
  499. GFX_PrintcConsole((word)chr);
  500. str++; // go to next character address
  501. chr = *str; // get character from address
  502. }
  503. }
  504. // Clears console by removing all tiles and resetting the cursor
  505. void GFX_clearConsole()
  506. {
  507. GFX_clearWindowtileTable();
  508. GFX_clearBGtileTable();
  509. GFX_cursor = 0;
  510. }