selftest.pde 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include <SPI.h>
  2. #include <GD.h>
  3. int atxy(int x, int y)
  4. {
  5. return (y << 6) + x;
  6. }
  7. void readn(byte *dst, unsigned int addr, int c)
  8. {
  9. GD.__start(addr);
  10. while (c--)
  11. *dst++ = SPI.transfer(0);
  12. GD.__end();
  13. }
  14. static byte coll[256];
  15. static void debug_coll()
  16. {
  17. while (GD.rd(VBLANK) == 0) // Wait until vblank
  18. ;
  19. while (GD.rd(VBLANK) == 1) // Wait until display
  20. ;
  21. while (GD.rd(VBLANK) == 0) // Wait until vblank
  22. ;
  23. readn(coll, COLLISION, 256);
  24. }
  25. #define FAIL do { Serial.print("Fail at line: "); Serial.println(__LINE__, DEC); return 0; } while (0)
  26. int test_collision()
  27. {
  28. int i, j;
  29. #define NOCOLL 0xff
  30. GD.wr16(RAM_SPRPAL, 0x8000); // color 0 transparent, 1-255 0x5555 (pinkish)
  31. GD.fill(RAM_SPRPAL + 2, 0x55, 510);
  32. GD.fill(RAM_SPRIMG, 1, 256);
  33. for (i = 0; i < 256; i++)
  34. GD.sprite(i, 400, 400, 0, 0, 0);
  35. debug_coll();
  36. for (i = 0; i < 256; i++)
  37. if (coll[i] != NOCOLL)
  38. FAIL;
  39. GD.sprite(7, 200, 100, 0, 0, 0);
  40. GD.sprite(117, 200, 200, 0, 0, 0);
  41. byte jkmode, jk;
  42. for (jkmode = 0; jkmode < 2; jkmode++) {
  43. for (jk = 0; jk < 2; jk++) {
  44. GD.wr(JK_MODE, jkmode);
  45. for (i = -20; i < 20; i++) {
  46. GD.sprite(8, 200, 100 + i, 0, 0, 0, jk);
  47. GD.sprite(200, 200 + i, 200, 0, 0, 0, jk);
  48. debug_coll();
  49. byte expected = ((!jkmode || jk) && (abs(i) < 16)) ? 7 : NOCOLL;
  50. if (coll[8] != expected)
  51. FAIL;
  52. expected = ((!jkmode || jk) && (abs(i) < 16)) ? 117 : NOCOLL;
  53. if (coll[200] != expected)
  54. FAIL;
  55. }
  56. }
  57. }
  58. randomSeed(1);
  59. for (j = 100; j; j--) {
  60. for (i = 0; i < 256; i++) {
  61. GD.sprite(i, random(512), random(512), 0, 0, 0);
  62. }
  63. debug_coll();
  64. for (i = 0; i < 256; i++) {
  65. if (coll[i] != 0xff && (coll[i] >= i))
  66. FAIL;
  67. }
  68. }
  69. for (i = 0; i < 256; i++)
  70. GD.sprite(i, 400, 400, 0, 0, 0);
  71. return 1;
  72. }
  73. int test_ident()
  74. {
  75. byte id = GD.rd(IDENT);
  76. if (id != 0x6d) {
  77. Serial.println(id, HEX);
  78. FAIL;
  79. }
  80. return 1;
  81. }
  82. int test_frame()
  83. {
  84. byte v;
  85. const int nframes = 200;
  86. v = GD.rd(FRAME);
  87. while (GD.rd(FRAME) != ((v + 1) & 0xff))
  88. ;
  89. long t0 = micros();
  90. while (GD.rd(FRAME) != ((v + nframes + 1) & 0xff))
  91. ;
  92. long t10 = micros();
  93. Serial.println(t10 - t0, DEC);
  94. Serial.print("(");
  95. Serial.print(nframes / (1.e-6 * (t10 - t0)), DEC);
  96. Serial.println(" fps)");
  97. return 1;
  98. }
  99. // low-level SPI test. Write a random pattern to the 16K image RAM,
  100. // then read it back, verifying the same random values. Meant to
  101. // catch SPI transmission errors.
  102. int test_spi()
  103. {
  104. int i;
  105. randomSeed(947);
  106. GD.__wstart(RAM_SPRIMG);
  107. for (i = 0; i < 16384; i++)
  108. SPI.transfer(random(256));
  109. GD.__end();
  110. randomSeed(947);
  111. GD.__start(RAM_SPRIMG);
  112. for (i = 0; i < 16384; i++)
  113. if (SPI.transfer(0) != random(256))
  114. FAIL;
  115. GD.__end();
  116. return 1;
  117. }
  118. // Test a RAM area (addr, c)
  119. int test_a_ram(unsigned int addr, int c)
  120. {
  121. while (c--) {
  122. byte prev = GD.rd(addr);
  123. GD.wr(addr, 0xff); if (GD.rd(addr) != 0xff) FAIL;
  124. GD.wr(addr, 0x00); if (GD.rd(addr) != 0x00) FAIL;
  125. GD.wr(addr, 0x47); if (GD.rd(addr) != 0x47) FAIL;
  126. GD.wr(addr, prev); if (GD.rd(addr) != prev) FAIL;
  127. addr++;
  128. }
  129. return 1;
  130. }
  131. // Write/read a simple pattern to each RAM byte.
  132. // (Restores RAM values so display is preserved.)
  133. int test_rams()
  134. {
  135. test_a_ram(0, (4 + 4 + 2) * 1024); /* Pic, chr and pal */
  136. test_a_ram(RAM_SPR, 0x5000); /* Sprites */
  137. test_a_ram(PALETTE16A, 64);
  138. test_a_ram(PALETTE4A, 16);
  139. test_a_ram(VOICES, 64 * 4);
  140. GD.wr(J1_RESET, 1);
  141. test_a_ram(J1_CODE, 256);
  142. return 1;
  143. }
  144. int test_audio_l()
  145. {
  146. GD.fill(VOICES, 0, 64 * 4);
  147. GD.voice(0, 0, 4 * 440, 255, 0);
  148. delay(1000);
  149. return 1;
  150. }
  151. int test_audio_r()
  152. {
  153. GD.fill(VOICES, 0, 64 * 4);
  154. GD.voice(0, 0, 4 * 440, 0, 255);
  155. delay(1000);
  156. GD.fill(VOICES, 0, 64 * 4);
  157. return 1;
  158. }
  159. int test_speed()
  160. {
  161. long t0 = millis();
  162. int i, j;
  163. for (i = 0; i < 1000; i++) {
  164. GD.fill(RAM_SPRIMG, 0x55, 1000);
  165. }
  166. Serial.print("(Took ");
  167. Serial.print(millis() - t0);
  168. Serial.print(")");
  169. return 1;
  170. }
  171. #include "lena.h"
  172. static void show_lena()
  173. {
  174. GD.copy(RAM_SPRPAL, lenapal, sizeof(lenapal));
  175. int i;
  176. for (i = 0; i < 64; i++)
  177. GD.sprite(i, 256 + ((i & 7) << 4), 64 + 2 * (i & 070), i, 0, 0);
  178. for (i = 64; i < 512; i++)
  179. GD.sprite(i, 400, 400, 0, 0, 0);
  180. GD.uncompress(RAM_SPRIMG, lenaimg);
  181. }
  182. void show_stripes()
  183. {
  184. int i;
  185. for (i = 0; i < 32; i++) {
  186. GD.wr16(RAM_PAL + (0x80 + i) * 8, RGB(8 * i, 0, 0));
  187. GD.wr16(RAM_PAL + (0xa0 + i) * 8, RGB(0, 8 * i, 0));
  188. GD.wr16(RAM_PAL + (0xc0 + i) * 8, RGB(0, 0, 8 * i));
  189. GD.wr(atxy(i, 24), 0x80 + i);
  190. GD.wr(atxy(i, 25), 0xa0 + i);
  191. GD.wr(atxy(i, 26), 0xc0 + i);
  192. }
  193. GD.putstr(0, 28, "R");
  194. GD.putstr(0, 29, "G");
  195. GD.putstr(0, 30, "B");
  196. GD.putstr(4, 31, "0");
  197. GD.putstr(8, 31, "1");
  198. GD.putstr(16, 31, "2");
  199. GD.wr(atxy(4, 28), 0x80 + 4);
  200. GD.wr(atxy(8, 28), 0x80 + 8);
  201. GD.wr(atxy(16, 28), 0x80 + 16);
  202. GD.wr(atxy(4, 29), 0xa0 + 4);
  203. GD.wr(atxy(8, 29), 0xa0 + 8);
  204. GD.wr(atxy(16, 29), 0xa0 + 16);
  205. GD.wr(atxy(4, 30), 0xc0 + 4);
  206. GD.wr(atxy(8, 30), 0xc0 + 8);
  207. GD.wr(atxy(16, 30), 0xc0 + 16);
  208. }
  209. byte y;
  210. static void logn(const char*s)
  211. {
  212. Serial.print(s);
  213. GD.putstr(0, y, s);
  214. }
  215. static void log(const char*s)
  216. {
  217. Serial.println(s);
  218. GD.putstr(16, y++, s);
  219. }
  220. #define RUNTEST(NAME) \
  221. do { \
  222. logn(#NAME ": "); \
  223. r = NAME(); \
  224. log(r ? "pass" : "FAIL"); \
  225. pass &= r; \
  226. } while (0)
  227. #include "selftest1.h"
  228. static unsigned long rd32()
  229. {
  230. return GD.rd16(COMM+0) + ((unsigned long)GD.rd16(COMM+2) << 16);
  231. }
  232. int test_coproc()
  233. {
  234. GD.microcode(selftest1_code, sizeof(selftest1_code));
  235. GD.wr(COMM+15, 0); // stop
  236. GD.wr16(COMM+0, 0);
  237. GD.wr16(COMM+2, 0);
  238. unsigned long started;
  239. unsigned long cycles0, cycles1;
  240. byte regime;
  241. int jj;
  242. for (regime = 0; regime < 6; regime++) {
  243. cycles0 = rd32();
  244. started = micros();
  245. GD.wr(COMM+15, 1); // go
  246. switch (regime) {
  247. case 0:
  248. delay(1000);
  249. break;
  250. case 1:
  251. GD.__start(0);
  252. delay(1000);
  253. GD.__end();
  254. break;
  255. case 2:
  256. GD.__start(0);
  257. SPI.transfer(0);
  258. delay(1000);
  259. GD.__end();
  260. break;
  261. case 3:
  262. GD.__start(0);
  263. for (jj = 0; jj < 1000; jj++) {
  264. SPI.transfer(0);
  265. delay(1);
  266. }
  267. GD.__end();
  268. break;
  269. case 4:
  270. for (jj = 0; jj < 1000; jj++) {
  271. GD.rd(0);
  272. delay(1);
  273. }
  274. break;
  275. case 5:
  276. while ((micros() - started) < 1000000) {
  277. GD.__start(0);
  278. for (jj = 0; jj < 1000; jj++)
  279. SPI.transfer(0);
  280. GD.__end();
  281. }
  282. break;
  283. }
  284. GD.wr(COMM+15, 0); // stop
  285. delay(1);
  286. cycles1 = rd32();
  287. long cps = long(1e6 * (cycles1 - cycles0) / (micros() - started));
  288. if (cps < 1000000)
  289. FAIL;
  290. // Serial.println(micros() - started, DEC);
  291. // Serial.print(regime, DEC);
  292. // Serial.print(' ');
  293. // Serial.println(cps, DEC);
  294. }
  295. return 1;
  296. }
  297. // See Atmel AT45DB021D datasheet:
  298. // http://www.atmel.com/dyn/resources/prod_documents/doc3638.pdf
  299. static int test_flash()
  300. {
  301. GD.wr(IOMODE, 'F');
  302. pinMode(2, OUTPUT);
  303. digitalWrite(2, HIGH);
  304. delay(1);
  305. digitalWrite(2, LOW);
  306. SPI.transfer(0xd7); // read SPI flash status
  307. byte status = SPI.transfer(0);
  308. digitalWrite(2, HIGH);
  309. if (status != 0x94) // 0x94 means "idle; all is well"
  310. FAIL;
  311. GD.wr(IOMODE, 0);
  312. return 1;
  313. }
  314. static void runtests()
  315. {
  316. char msg[50];
  317. GD.begin();
  318. GD.ascii();
  319. GD.fill(0, ' ', 4096);
  320. GD.putstr(0, 0,"<------------------- TOP LINE ------------------->");
  321. GD.putstr(0,36,"<----------------- BOTTOM LINE ------------------>");
  322. show_stripes();
  323. y = 3;
  324. byte r, pass = 1;
  325. log("Starting self-test");
  326. RUNTEST(test_ident);
  327. // RUNTEST(test_frame);
  328. RUNTEST(test_flash);
  329. RUNTEST(test_audio_l);
  330. RUNTEST(test_audio_r);
  331. RUNTEST(test_coproc);
  332. RUNTEST(test_speed);
  333. RUNTEST(test_spi);
  334. RUNTEST(test_rams);
  335. RUNTEST(test_collision);
  336. if (pass) {
  337. log("All tests passed");
  338. show_lena();
  339. long seconds = millis() / 1000;
  340. long minutes = seconds / 60;
  341. sprintf(msg, "%d minutes", minutes);
  342. log(msg);
  343. // GD.screenshot(0);
  344. } else {
  345. for (;;) {
  346. GD.wr16(BG_COLOR, RGB(255,0,0));
  347. delay(100);
  348. GD.wr16(BG_COLOR, RGB(0,0,0));
  349. delay(100);
  350. }
  351. }
  352. byte i;
  353. for (i = 9; i; i--) {
  354. sprintf(msg, "Restarting in %d", i);
  355. GD.putstr(0, y, msg);
  356. delay(1000);
  357. }
  358. }
  359. void setup()
  360. {
  361. Serial.begin(1000000);
  362. runtests();
  363. }
  364. void loop()
  365. {
  366. runtests();
  367. }