tests.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* ___DISCLAIMER___ */
  2. #include <arm/NXP/LPC17xx/LPC17xx.h>
  3. #include "bits.h"
  4. #include "config.h"
  5. #include "uart.h"
  6. #include "timer.h"
  7. #include "led.h"
  8. #include "cli.h"
  9. #include "fpga.h"
  10. #include "fpga_spi.h"
  11. #include "ff.h"
  12. #include "fileops.h"
  13. #include "crc32.h"
  14. #include "diskio.h"
  15. #include "cic.h"
  16. #include "rtc.h"
  17. #include "memory.h"
  18. #include "snes.h"
  19. #include "cli.h"
  20. #include "tests.h"
  21. #define PROGRESS ("-\\|/")
  22. int test_sd() {
  23. printf("SD test... please insert card\n=============================\n");
  24. while(disk_status(0) & (STA_NOINIT|STA_NODISK)) cli_entrycheck();
  25. file_open((uint8_t*)"/sd2snes/testfile.bin", FA_WRITE | FA_CREATE_ALWAYS);
  26. if(file_res) {
  27. printf("could not open /sd2snes/testfile.bin: Error %d\n", file_res);
  28. printf("FAILED\n\n\n");
  29. return FAILED;
  30. }
  31. uint32_t testval = 0x55AA55AA;
  32. uint32_t crc = 0;
  33. uint32_t count, blkcount;
  34. for(count=0; count < 8192; count++) {
  35. for(blkcount=0; blkcount < 512; blkcount++) {
  36. file_buf[blkcount] = testval&0xff;
  37. crc=crc32_update(crc, testval&0xff);
  38. testval ^= (crc * (count + blkcount + 7)) - 1;
  39. }
  40. file_write();
  41. }
  42. printf("crc1 = %08lx ", crc);
  43. file_close();
  44. file_open((uint8_t*)"/sd2snes/testfile.bin", FA_READ);
  45. uint32_t crc2 = 0;
  46. for(count=0; count < 8192; count++) {
  47. file_read();
  48. for(blkcount=0; blkcount < 512; blkcount++) {
  49. testval = file_buf[blkcount];
  50. crc2 = crc32_update(crc2, testval&0xff);
  51. }
  52. }
  53. file_close();
  54. printf("crc2 = %08lx ", crc2);
  55. if(crc==crc2) {
  56. printf(" PASSED\n\n\n");
  57. return PASSED;
  58. } else {
  59. printf(" FAILED\n\n\n");
  60. return FAILED;
  61. }
  62. }
  63. int test_cic() {
  64. int cic_state = get_cic_state();
  65. printf("CIC Test:\n=========\n");
  66. printf("Current CIC state: %s\n", get_cic_statename(cic_state));
  67. if(cic_state == CIC_FAIL) {
  68. printf("CIC reports error, push reset...\n");
  69. while((cic_state = get_cic_state()) == CIC_FAIL);
  70. }
  71. if(cic_state == CIC_OK) {
  72. printf("CIC reports OK; no pair mode available. Provoking CIC error...\n");
  73. cic_pair(1,1);
  74. delay_ms(200);
  75. cic_init(0);
  76. printf("new CIC state: %s\n", get_cic_statename(get_cic_state()));
  77. if(get_cic_state() == CIC_FAIL) {
  78. printf("***Please reset SNES***\n");
  79. int failcount=2;
  80. while(failcount--) {
  81. while(get_cic_state() == CIC_FAIL);
  82. delay_ms(200);
  83. }
  84. if(get_cic_state() != CIC_FAIL) {
  85. printf("PASSED\n\n\n");
  86. return PASSED;
  87. }
  88. printf("CIC did not recover properly.\nFAILED\n");
  89. return FAILED;
  90. }
  91. printf("FAILED\n\n\n");
  92. return FAILED;
  93. }
  94. if(cic_state == CIC_SCIC) {
  95. printf("CIC reports OK; pair mode available. Switching to pair mode...\n");
  96. cic_init(1);
  97. delay_ms(100);
  98. cic_pair(0,0);
  99. delay_ms(1000);
  100. printf("new CIC state: %s\n", get_cic_statename(cic_state = get_cic_state()));
  101. if(get_cic_state() != CIC_PAIR) {
  102. printf("FAILED to switch to pair mode!!!\n");
  103. return FAILED;
  104. }
  105. }
  106. if(cic_state == CIC_PAIR) {
  107. cic_init(1);
  108. cic_pair(0,0);
  109. printf("cycling modes, observe power LED color\n");
  110. for(cic_state = 0; cic_state < 17; cic_state++) {
  111. cic_videomode(cic_state & 1);
  112. delay_ms(200);
  113. }
  114. }
  115. printf("PASSED\n\n\n");
  116. return PASSED;
  117. }
  118. int test_rtc() {
  119. struct tm time;
  120. printf("RTC Test\n========\n");
  121. printf("setting clock to 2011-01-01 00:00:00\n");
  122. set_bcdtime(0x20110101000000LL);
  123. printf("waiting 5 seconds\n");
  124. delay_ms(5000);
  125. // uint64_t newtime = get_bcdtime();
  126. printf("new time: ");
  127. read_rtc(&time);
  128. printtime(&time);
  129. if((get_bcdtime() & 0xffffffffffffff) >= 0x20110101000004LL) {
  130. printf("PASSED\n\n\n");
  131. return PASSED;
  132. } else printf("FAILED\n\n\n");
  133. return FAILED;
  134. }
  135. int test_fpga() {
  136. printf("FPGA test\n=========\n");
  137. printf("configuring fpga...\n");
  138. fpga_pgm((uint8_t*)"/sd2snes/test.bit");
  139. printf("basic communication test...");
  140. if(fpga_test() != FPGA_TEST_TOKEN) {
  141. printf("FAILED\n\n\n");
  142. return FAILED;
  143. } else printf("PASSED\n\n\n");
  144. return PASSED;
  145. }
  146. /*************************************************************************************/
  147. /*************************************************************************************/
  148. typedef struct memory_test
  149. {
  150. char name[20];
  151. int a_len;
  152. int d_len;
  153. unsigned int (*read)(unsigned int addr);
  154. void (*write)(unsigned int addr, unsigned int data);
  155. void (*open)(void);
  156. void (*close)(void);
  157. } memory_test;
  158. /*************************************************************************************/
  159. void rom_open(void)
  160. {
  161. snes_reset(1);
  162. fpga_select_mem(0);
  163. FPGA_DESELECT();
  164. delay_ms(1);
  165. FPGA_SELECT();
  166. delay_ms(1);
  167. }
  168. void rom_close(void)
  169. {
  170. }
  171. unsigned int rom_read(unsigned int addr)
  172. {
  173. return sram_readbyte(addr);
  174. }
  175. void rom_write(unsigned int addr, unsigned int data)
  176. {
  177. sram_writebyte(data, addr);
  178. }
  179. memory_test rom = {
  180. .name = "RAM0 (128Mbit)",
  181. .a_len = 22,
  182. .d_len = 8,
  183. .read = rom_read,
  184. .write = rom_write,
  185. .open = rom_open,
  186. .close = rom_close,
  187. };
  188. /*************************************************************************************/
  189. void sram_open(void)
  190. {
  191. snes_reset(1);
  192. fpga_select_mem(1);
  193. }
  194. void sram_close(void)
  195. {
  196. }
  197. unsigned int sram_read(unsigned int addr)
  198. {
  199. return sram_readbyte(addr);
  200. }
  201. void sram_write(unsigned int addr, unsigned int data)
  202. {
  203. sram_writebyte(data, addr);
  204. }
  205. memory_test sram =
  206. {
  207. .name = "RAM1(4Mbit)",
  208. .a_len = 19,
  209. .d_len = 8,
  210. .read = sram_read,
  211. .write = sram_write,
  212. .open = sram_open,
  213. .close = sram_close,
  214. };
  215. int do_test(memory_test *test)
  216. {
  217. int i, j, read, want;
  218. int ret = 0;
  219. int a_mask = (1 << test->a_len) - 1;
  220. int d_mask = (1 << test->d_len) - 1;
  221. test->open();
  222. printf("-- Will test %s\n", test->name);
  223. printf("---- Fill with AA55 ");
  224. test->write(0, 0xAA);
  225. for (i = 1; i < a_mask; i++)
  226. {
  227. if((i&0xffff) == 0)printf("\x8%c", PROGRESS[(i>>16)&3]);
  228. want = (i&1)?0x55:0xAA;
  229. test->write(i, want);
  230. want = ((i-1)&1)?0x55:0xAA;
  231. read = test->read(i-1);
  232. if (read != want)
  233. {
  234. printf("Failed [@%8X Want: %02X Get: %02X]", i-1, want, read);
  235. ret |= 1;
  236. break;
  237. }
  238. }
  239. printf("Ok \n---- Fill with 00 ");
  240. for (i = 0; i < a_mask; i++)
  241. {
  242. if((i&0xffff) == 0)printf("\x8%c", PROGRESS[(i>>16)&3]);
  243. test->write(i, 0);
  244. }
  245. printf("Ok \n---- Check data lines...\n"
  246. "----- ");
  247. for (i = 0; i < test->d_len; i++) printf("%X", i);
  248. printf("\n");
  249. /* Check on 4 addresses, taken evenly */
  250. #define TEST_NUM (10)
  251. for (j = 0; j < TEST_NUM; j ++)
  252. {
  253. printf("----- %8X [", j * a_mask/TEST_NUM);
  254. for (i = 0; i < test->d_len; i++)
  255. {
  256. read = test->read(j * a_mask/TEST_NUM);
  257. if ((test->read(j * a_mask/TEST_NUM) & (1<<i)) != 0)
  258. {
  259. printf("1", read);
  260. ret |= 2;
  261. goto next_data;
  262. }
  263. test->write(j * a_mask/TEST_NUM, (1<<i));
  264. read = test->read(j * a_mask/TEST_NUM);
  265. if (read == 0)
  266. {
  267. printf("0");
  268. ret |= 4;
  269. goto next_data;
  270. }
  271. printf("x");
  272. next_data:
  273. test->write(j * a_mask/4, 0);
  274. }
  275. printf("]\n");
  276. }
  277. test->close();
  278. return ret;
  279. }
  280. int test_mem()
  281. {
  282. int ret = PASSED;
  283. printf("RAM test\n========\n");
  284. if (do_test(&rom) != 0)
  285. ret = FAILED;
  286. if (do_test(&sram) != 0);
  287. ret = FAILED;
  288. return PASSED;
  289. }
  290. int test_clk() {
  291. uint32_t sysclk[4];
  292. int32_t diff, max_diff = 0;
  293. int i, error=0;
  294. printf("sysclk test\n===========\n");
  295. printf("measuring SNES clock...\n");
  296. for(i=0; i<4; i++) {
  297. sysclk[i]=get_snes_sysclk();
  298. if(sysclk[i] < 21000000 || sysclk[i] > 22000000) error = 1;
  299. printf("%lu Hz ", sysclk[i]);
  300. if(i) {
  301. diff = sysclk[i] - sysclk[i-1];
  302. if(diff < 0) diff = -diff;
  303. if(diff > max_diff) max_diff = diff;
  304. printf("diff = %ld max = %ld", diff, max_diff);
  305. }
  306. printf("\n");
  307. delay_ms(1010);
  308. }
  309. if(error) {
  310. printf("clock frequency out of range!\n");
  311. }
  312. if(diff > 1000000) {
  313. printf("clock variation too great!\n");
  314. error = 1;
  315. }
  316. printf(" CPUCLK: %lu\n", get_snes_cpuclk());
  317. printf(" READCLK: %lu\n", get_snes_readclk());
  318. printf(" WRITECLK: %lu\n", get_snes_writeclk());
  319. printf(" PARDCLK: %lu\n", get_snes_pardclk());
  320. printf(" PAWRCLK: %lu\n", get_snes_pawrclk());
  321. printf(" REFRCLK: %lu\n", get_snes_refreshclk());
  322. printf("ROMSELCLK: %lu\n", get_snes_romselclk());
  323. if(error) {
  324. printf("FAILED\n\n\n");
  325. return FAILED;
  326. }
  327. printf("PASSED\n\n\n");
  328. return PASSED;
  329. }