GD.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (c) 2011 by James Bowman <jamesb@excamera.com>
  3. * Gameduino library for arduino.
  4. *
  5. */
  6. #ifdef BOARD_maple
  7. #include "wirish.h"
  8. HardwareSPI SPI(1);
  9. #include "GD.h"
  10. #else
  11. #include "Arduino.h"
  12. #include <avr/pgmspace.h>
  13. #include <SPI.h>
  14. #include <GD.h>
  15. #endif
  16. GDClass GD;
  17. void GDClass::begin()
  18. {
  19. delay(250); // give Gameduino time to boot
  20. pinMode(SS_PIN, OUTPUT);
  21. #ifdef BOARD_maple
  22. SPI.begin(SPI_4_5MHZ, MSBFIRST, 0);
  23. #else
  24. SPI.begin();
  25. SPI.setClockDivider(SPI_CLOCK_DIV2);
  26. SPI.setBitOrder(MSBFIRST);
  27. SPI.setDataMode(SPI_MODE0);
  28. SPSR = (1 << SPI2X);
  29. #endif
  30. digitalWrite(SS_PIN, HIGH);
  31. GD.wr(J1_RESET, 1); // HALT coprocessor
  32. __wstart(RAM_SPR); // Hide all sprites
  33. for (int i = 0; i < 512; i++)
  34. GD.xhide();
  35. __end();
  36. fill(RAM_PIC, 0, 1024 * 10); // Zero all character RAM
  37. fill(RAM_SPRPAL, 0, 2048); // Sprite palletes black
  38. fill(RAM_SPRIMG, 0, 64 * 256); // Clear all sprite data
  39. fill(VOICES, 0, 256); // Silence
  40. fill(PALETTE16A, 0, 128); // Black 16-, 4-palletes and COMM
  41. GD.wr16(SCROLL_X, 0);
  42. GD.wr16(SCROLL_Y, 0);
  43. GD.wr(JK_MODE, 0);
  44. GD.wr(SPR_DISABLE, 0);
  45. GD.wr(SPR_PAGE, 0);
  46. GD.wr(IOMODE, 0);
  47. GD.wr16(BG_COLOR, 0);
  48. GD.wr16(SAMPLE_L, 0);
  49. GD.wr16(SAMPLE_R, 0);
  50. GD.wr16(SCREENSHOT_Y, 0);
  51. GD.wr(MODULATOR, 64);
  52. }
  53. void GDClass::end() {
  54. }
  55. void GDClass::__start(unsigned int addr) // start an SPI transaction to addr
  56. {
  57. digitalWrite(SS_PIN, LOW);
  58. SPI.transfer(highByte(addr));
  59. SPI.transfer(lowByte(addr));
  60. }
  61. void GDClass::__wstart(unsigned int addr) // start an SPI write transaction to addr
  62. {
  63. __start(0x8000|addr);
  64. }
  65. void GDClass::__wstartspr(unsigned int sprnum)
  66. {
  67. __start((0x8000 | RAM_SPR) + (sprnum << 2));
  68. spr = 0;
  69. }
  70. void GDClass::__end() // end the SPI transaction
  71. {
  72. digitalWrite(SS_PIN, HIGH);
  73. }
  74. byte GDClass::rd(unsigned int addr)
  75. {
  76. __start(addr);
  77. byte r = SPI.transfer(0);
  78. __end();
  79. return r;
  80. }
  81. void GDClass::wr(unsigned int addr, byte v)
  82. {
  83. __wstart(addr);
  84. SPI.transfer(v);
  85. __end();
  86. }
  87. unsigned int GDClass::rd16(unsigned int addr)
  88. {
  89. unsigned int r;
  90. __start(addr);
  91. r = SPI.transfer(0);
  92. r |= (SPI.transfer(0) << 8);
  93. __end();
  94. return r;
  95. }
  96. void GDClass::wr16(unsigned int addr, unsigned int v)
  97. {
  98. __wstart(addr);
  99. SPI.transfer(lowByte(v));
  100. SPI.transfer(highByte(v));
  101. __end();
  102. }
  103. void GDClass::fill(int addr, byte v, unsigned int count)
  104. {
  105. __wstart(addr);
  106. while (count--)
  107. SPI.transfer(v);
  108. __end();
  109. }
  110. void GDClass::copy(unsigned int addr, PROGMEM prog_uchar *src, int count)
  111. {
  112. __wstart(addr);
  113. while (count--) {
  114. SPI.transfer(pgm_read_byte_near(src));
  115. src++;
  116. }
  117. __end();
  118. }
  119. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  120. void GDClass::copy(unsigned int addr, uint_farptr_t src, int count)
  121. {
  122. __wstart(addr);
  123. while (count--) {
  124. SPI.transfer(pgm_read_byte_far(src));
  125. src++;
  126. }
  127. __end();
  128. }
  129. #endif
  130. void GDClass::microcode(PROGMEM prog_uchar *src, int count)
  131. {
  132. wr(J1_RESET, 1);
  133. copy(J1_CODE, src, count);
  134. wr(J1_RESET, 0);
  135. }
  136. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  137. void GDClass::microcode(uint_farptr_t src, int count)
  138. {
  139. wr(J1_RESET, 1);
  140. copy(J1_CODE, src, count);
  141. wr(J1_RESET, 0);
  142. }
  143. #endif
  144. void GDClass::setpal(int pal, unsigned int rgb)
  145. {
  146. wr16(RAM_PAL + (pal << 1), rgb);
  147. }
  148. void GDClass::sprite(int spr, int x, int y, byte image, byte palette, byte rot, byte jk)
  149. {
  150. __wstart(RAM_SPR + (spr << 2));
  151. SPI.transfer(lowByte(x));
  152. SPI.transfer((palette << 4) | (rot << 1) | (highByte(x) & 1));
  153. SPI.transfer(lowByte(y));
  154. SPI.transfer((jk << 7) | (image << 1) | (highByte(y) & 1));
  155. __end();
  156. }
  157. void GDClass::xsprite(int ox, int oy, char x, char y, byte image, byte palette, byte rot, byte jk)
  158. {
  159. if (rot & 2)
  160. x = -16-x;
  161. if (rot & 4)
  162. y = -16-y;
  163. if (rot & 1) {
  164. int s;
  165. s = x; x = y; y = s;
  166. }
  167. ox += x;
  168. oy += y;
  169. SPI.transfer(lowByte(ox));
  170. SPI.transfer((palette << 4) | (rot << 1) | (highByte(ox) & 1));
  171. SPI.transfer(lowByte(oy));
  172. SPI.transfer((jk << 7) | (image << 1) | (highByte(oy) & 1));
  173. spr++;
  174. }
  175. void GDClass::xhide()
  176. {
  177. SPI.transfer(lowByte(400));
  178. SPI.transfer(highByte(400));
  179. SPI.transfer(lowByte(400));
  180. SPI.transfer(highByte(400));
  181. spr++;
  182. }
  183. void GDClass::plots(int ox, int oy, PROGMEM sprplot *psp, byte count, byte rot, byte jk)
  184. {
  185. while (count--) {
  186. struct sprplot sp;
  187. memcpy_P((void*)&sp, psp++, sizeof(sp));
  188. GD.xsprite(ox, oy, sp.x, sp.y, sp.image, sp.palette, rot, jk);
  189. }
  190. }
  191. void GDClass::sprite2x2(int spr, int x, int y, byte image, byte palette, byte rot, byte jk)
  192. {
  193. __wstart(0x3000 + (spr << 2));
  194. GD.xsprite(x, y, -16, -16, image + 0, palette, rot, jk);
  195. GD.xsprite(x, y, 0, -16, image + 1, palette, rot, jk);
  196. GD.xsprite(x, y, -16, 0, image + 2, palette, rot, jk);
  197. GD.xsprite(x, y, 0, 0, image + 3, palette, rot, jk);
  198. __end();
  199. }
  200. void GDClass::waitvblank()
  201. {
  202. // Wait for the VLANK to go from 0 to 1: this is the start
  203. // of the vertical blanking interval.
  204. while (rd(VBLANK) == 1)
  205. ;
  206. while (rd(VBLANK) == 0)
  207. ;
  208. }
  209. /* Fixed ascii font, useful for debug */
  210. #include "font8x8.h"
  211. static byte stretch[16] = {
  212. 0x00, 0x03, 0x0c, 0x0f,
  213. 0x30, 0x33, 0x3c, 0x3f,
  214. 0xc0, 0xc3, 0xcc, 0xcf,
  215. 0xf0, 0xf3, 0xfc, 0xff
  216. };
  217. void GDClass::ascii()
  218. {
  219. long i;
  220. for (i = 0; i < 768; i++) {
  221. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  222. byte b = pgm_read_byte_far(GET_FAR_ADDRESS(font8x8) + i);
  223. #else
  224. byte b = pgm_read_byte(font8x8 + i);
  225. #endif
  226. byte h = stretch[b >> 4];
  227. byte l = stretch[b & 0xf];
  228. GD.wr(0x1000 + (16 * ' ') + (2 * i), h);
  229. GD.wr(0x1000 + (16 * ' ') + (2 * i) + 1, l);
  230. }
  231. for (i = 0x20; i < 0x80; i++) {
  232. GD.setpal(4 * i + 0, TRANSPARENT);
  233. GD.setpal(4 * i + 3, RGB(255,255,255));
  234. }
  235. GD.fill(RAM_PIC, ' ', 4096);
  236. }
  237. void GDClass::putstr(int x, int y, const char *s)
  238. {
  239. GD.__wstart((y << 6) + x);
  240. while (*s)
  241. SPI.transfer(*s++);
  242. GD.__end();
  243. }
  244. void GDClass::voice(int v, byte wave, unsigned int freq, byte lamp, byte ramp)
  245. {
  246. __wstart(VOICES + (v << 2));
  247. SPI.transfer(lowByte(freq));
  248. SPI.transfer(highByte(freq) | (wave << 7));
  249. SPI.transfer(lamp);
  250. SPI.transfer(ramp);
  251. __end();
  252. }
  253. void GDClass::screenshot(unsigned int frame)
  254. {
  255. int yy, xx;
  256. byte undone[38]; // 300-long bitmap of lines pending
  257. // initialize to 300 ones
  258. memset(undone, 0xff, 37);
  259. undone[37] = 0xf;
  260. int nundone = 300;
  261. Serial.write(0xa5); // sync byte
  262. Serial.write(lowByte(frame));
  263. Serial.write(highByte(frame));
  264. while (nundone) {
  265. // find a pending line a short distance ahead of the raster
  266. int hwline = GD.rd16(SCREENSHOT_Y) & 0x1ff;
  267. for (yy = (hwline + 7) % 300; ((undone[yy>>3] >> (yy&7)) & 1) == 0; yy = (yy + 1) % 300)
  268. ;
  269. GD.wr16(SCREENSHOT_Y, 0x8000 | yy); // ask for it
  270. // housekeeping while waiting: mark line done and send yy
  271. undone[yy>>3] ^= (1 << (yy&7));
  272. nundone--;
  273. Serial.write(lowByte(yy));
  274. Serial.write(highByte(yy));
  275. while ((GD.rd(SCREENSHOT_Y + 1) & 0x80) == 0)
  276. ;
  277. // Now send the line, compressing zero pixels
  278. uint16_t zeroes = 0;
  279. for (xx = 0; xx < 800; xx += 2) {
  280. uint16_t v = GD.rd16(SCREENSHOT + xx);
  281. if (v == 0) {
  282. zeroes++;
  283. } else {
  284. if (zeroes) {
  285. Serial.write(lowByte(zeroes));
  286. Serial.write(0x80 | highByte(zeroes));
  287. zeroes = 0;
  288. }
  289. Serial.write(lowByte(v));
  290. Serial.write(highByte(v));
  291. }
  292. }
  293. if (zeroes) {
  294. Serial.write(lowByte(zeroes));
  295. Serial.write(0x80 | highByte(zeroes));
  296. }
  297. }
  298. GD.wr16(SCREENSHOT_Y, 0); // restore screen to normal
  299. }
  300. // near ptr version
  301. class GDflashbits {
  302. public:
  303. void begin(prog_uchar *s) {
  304. src = s;
  305. mask = 0x01;
  306. }
  307. byte get1(void) {
  308. byte r = (pgm_read_byte_near(src) & mask) != 0;
  309. mask <<= 1;
  310. if (!mask) {
  311. mask = 1;
  312. src++;
  313. }
  314. return r;
  315. }
  316. unsigned short getn(byte n) {
  317. unsigned short r = 0;
  318. while (n--) {
  319. r <<= 1;
  320. r |= get1();
  321. }
  322. return r;
  323. }
  324. private:
  325. prog_uchar *src;
  326. byte mask;
  327. };
  328. static GDflashbits GDFB;
  329. void GDClass::uncompress(unsigned int addr, prog_uchar *src)
  330. {
  331. GDFB.begin(src);
  332. byte b_off = GDFB.getn(4);
  333. byte b_len = GDFB.getn(4);
  334. byte minlen = GDFB.getn(2);
  335. unsigned short items = GDFB.getn(16);
  336. while (items--) {
  337. if (GDFB.get1() == 0) {
  338. GD.wr(addr++, GDFB.getn(8));
  339. } else {
  340. int offset = -GDFB.getn(b_off) - 1;
  341. int l = GDFB.getn(b_len) + minlen;
  342. while (l--) {
  343. GD.wr(addr, GD.rd(addr + offset));
  344. addr++;
  345. }
  346. }
  347. }
  348. }
  349. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  350. // far ptr version
  351. class GDflashbitsF {
  352. public:
  353. void begin(uint_farptr_t s) {
  354. src = s;
  355. mask = 0x01;
  356. }
  357. byte get1(void) {
  358. byte r = (pgm_read_byte_far(src) & mask) != 0;
  359. mask <<= 1;
  360. if (!mask) {
  361. mask = 1;
  362. src++;
  363. }
  364. return r;
  365. }
  366. unsigned short getn(byte n) {
  367. unsigned short r = 0;
  368. while (n--) {
  369. r <<= 1;
  370. r |= get1();
  371. }
  372. return r;
  373. }
  374. private:
  375. uint_farptr_t src;
  376. byte mask;
  377. };
  378. static GDflashbitsF GDFBF;
  379. void GDClass::uncompress(unsigned int addr, uint_farptr_t src)
  380. {
  381. GDFBF.begin(src);
  382. byte b_off = GDFBF.getn(4);
  383. byte b_len = GDFBF.getn(4);
  384. byte minlen = GDFBF.getn(2);
  385. unsigned short items = GDFBF.getn(16);
  386. while (items--) {
  387. if (GDFBF.get1() == 0) {
  388. GD.wr(addr++, GDFBF.getn(8));
  389. } else {
  390. int offset = -GDFBF.getn(b_off) - 1;
  391. int l = GDFBF.getn(b_len) + minlen;
  392. while (l--) {
  393. GD.wr(addr, GD.rd(addr + offset));
  394. addr++;
  395. }
  396. }
  397. }
  398. }
  399. #endif