megaed-sv: more tests
[megadrive.git] / megaed-sv / main.c
CommitLineData
39ac9835 1#include <stdlib.h>
ab6ed3c4 2#include <stdarg.h>
3
4#define u8 unsigned char
5#define u16 unsigned short
6#define u32 unsigned int
7
8#define noinline __attribute__((noinline))
39ac9835 9#define _packed __attribute__((packed))
10
11#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
ab6ed3c4 12
13#include "edos.h"
51f3a685 14#include "asmtools.h"
ab6ed3c4 15
65d9165c 16extern u16 start_hvc;
17
ab6ed3c4 18#define GFX_DATA_PORT 0xC00000
19#define GFX_CTRL_PORT 0xC00004
20
21#define TILE_MEM_END 0xB000
22
23#define FONT_LEN 128
24#define TILE_FONT_BASE (TILE_MEM_END / 32 - FONT_LEN)
25
26/* note: using ED menu's layout here.. */
65d9165c 27#define WPLANE (TILE_MEM_END + 0x0000)
ab6ed3c4 28#define HSCRL (TILE_MEM_END + 0x0800)
29#define SLIST (TILE_MEM_END + 0x0C00)
30#define APLANE (TILE_MEM_END + 0x1000)
31#define BPLANE (TILE_MEM_END + 0x3000)
32
df43aeea 33#define read8(a) \
34 *((volatile u8 *) (a))
35#define read16(a) \
36 *((volatile u16 *) (a))
37#define read32(a) \
38 *((volatile u32 *) (a))
ab6ed3c4 39#define write16(a, d) \
40 *((volatile u16 *) (a)) = (d)
41#define write32(a, d) \
42 *((volatile u32 *) (a)) = (d)
43
44#define GFX_WRITE_VRAM_ADDR(adr) \
45 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00)
46#define GFX_WRITE_VSRAM_ADDR(adr) \
47 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x10)
48
49enum {
50 VDP_MODE1 = 0x00,
51 VDP_MODE2 = 0x01,
52 VDP_BACKDROP = 0x07,
53 VDP_MODE3 = 0x0b,
54 VDP_MODE4 = 0x0c,
55 VDP_AUTOINC = 0x0f,
56 VDP_SCROLLSZ = 0x10,
57};
58
59/* cell counts */
60#define LEFT_BORDER 1 /* lame TV */
61#define PLANE_W 64
62#define PLANE_H 32
63#define CSCREEN_H 28
64
65static noinline void VDP_drawTextML(const char *str, u16 plane_base,
66 u16 x, u16 y)
67{
68 const u8 *src = (const u8 *)str;
69 u16 basetile = 0;
70 int max_len = 40 - LEFT_BORDER;
71 int len;
72 u32 addr;
73
74 x += LEFT_BORDER;
75
76 for (len = 0; str[len] && len < max_len; len++)
77 ;
78 if (len > (PLANE_W - x))
79 len = PLANE_W - x;
80
81 addr = plane_base + ((x + (PLANE_W * y)) << 1);
82 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr));
83
84 while (len-- > 0) {
85 write16(GFX_DATA_PORT,
86 basetile | ((*src++) - 32 + TILE_FONT_BASE));
87 }
88}
89
90static int printf_ypos;
91
92static void printf_line(int x, const char *buf)
93{
94 u32 addr;
95 int i;
96
ab6ed3c4 97 VDP_drawTextML(buf, APLANE, x, printf_ypos++ & (PLANE_H - 1));
98
99 if (printf_ypos >= CSCREEN_H) {
100 /* clear next line */
101 addr = APLANE;
102 addr += (PLANE_W * (printf_ypos & (PLANE_H - 1))) << 1;
103 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr));
104 for (i = 0; i < 40 / 2; i++)
105 write32(GFX_DATA_PORT, 0);
106
107 /* scroll plane */
108 write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
39ac9835 109 write16(GFX_DATA_PORT, (printf_ypos - CSCREEN_H + 1) * 8);
ab6ed3c4 110 }
111}
112
51f3a685 113#define PRINTF_LEN 40
114
ab6ed3c4 115static noinline int printf(const char *fmt, ...)
116{
51f3a685 117 static const char hexchars[] = "0123456789abcdef";
ab6ed3c4 118 static int printf_xpos;
51f3a685 119 char c, buf[PRINTF_LEN + 11 + 1];
120 const char *s;
ab6ed3c4 121 va_list ap;
122 int ival;
51f3a685 123 u32 uval;
ab6ed3c4 124 int d = 0;
51f3a685 125 int i, j;
ab6ed3c4 126
127 va_start(ap, fmt);
128 for (d = 0; *fmt; ) {
51f3a685 129 int prefix0 = 0;
130 int fwidth = 0;
131
132 c = *fmt++;
133 if (d < PRINTF_LEN)
134 buf[d] = c;
135
136 if (c != '%') {
137 if (c == '\n') {
ab6ed3c4 138 buf[d] = 0;
51f3a685 139 printf_line(printf_xpos, buf);
ab6ed3c4 140 d = 0;
141 printf_xpos = 0;
142 continue;
143 }
144 d++;
145 continue;
146 }
51f3a685 147 if (d >= PRINTF_LEN)
148 continue;
149
150 if (*fmt == '0') {
151 prefix0 = 1;
152 fmt++;
153 }
154
155 while ('1' <= *fmt && *fmt <= '9') {
156 fwidth = fwidth * 10 + *fmt - '0';
157 fmt++;
158 }
ab6ed3c4 159
160 switch (*fmt++) {
161 case '%':
162 d++;
163 break;
164 case 'd':
165 case 'i':
166 ival = va_arg(ap, int);
167 if (ival < 0) {
168 buf[d++] = '-';
169 ival = -ival;
170 }
171 for (i = 1000000000; i >= 10; i /= 10)
172 if (ival >= i)
173 break;
174 for (; i >= 10; i /= 10) {
175 buf[d++] = '0' + ival / i;
176 ival %= i;
177 }
178 buf[d++] = '0' + ival;
179 break;
51f3a685 180 case 'x':
181 uval = va_arg(ap, int);
39ac9835 182 while (fwidth > 1 && uval < (1 << (fwidth - 1) * 4)) {
51f3a685 183 buf[d++] = prefix0 ? '0' : ' ';
184 fwidth--;
185 }
186 for (j = 1; j < 8 && uval >= (1 << j * 4); j++)
187 ;
188 for (j--; j >= 0; j--)
189 buf[d++] = hexchars[(uval >> j * 4) & 0x0f];
190 break;
ab6ed3c4 191 case 's':
51f3a685 192 s = va_arg(ap, char *);
193 while (*s && d < PRINTF_LEN)
194 buf[d++] = *s++;
195 break;
ab6ed3c4 196 default:
197 // don't handle, for now
198 d++;
ab6ed3c4 199 va_arg(ap, void *);
200 break;
201 }
202 }
203 buf[d] = 0;
204 va_end(ap);
205
206 if (d != 0) {
207 // line without \n
208 VDP_drawTextML(buf, APLANE, printf_xpos,
209 printf_ypos & (PLANE_H - 1));
210 printf_xpos += d;
211 }
212
213 return d; // wrong..
214}
215
39ac9835 216static const char *exc_names[] = {
217 NULL,
218 NULL,
219 "Bus Error",
220 "Address Error",
221 "Illegal Instruction",
222 "Zero Divide",
223 "CHK Instruction",
224 "TRAPV Instruction",
225 "Privilege Violation", /* 8 8 */
226 "Trace",
227 "Line 1010 Emulator",
228 "Line 1111 Emulator",
229 NULL,
230 NULL,
231 NULL,
232 "Uninitialized Interrupt",
233 NULL, /* 10 16 */
234 NULL,
235 NULL,
236 NULL,
237 NULL,
238 NULL,
239 NULL,
240 NULL,
241 "Spurious Interrupt", /* 18 24 */
242 "l1 irq",
243 "l2 irq",
244 "l3 irq",
245 "l4 irq",
246 "l5 irq",
247 "l6 irq",
248 "l7 irq",
249};
250
251struct exc_frame {
252 u32 dr[8];
253 u32 ar[8];
254 u16 ecxnum; // from handler
255 union {
256 struct {
257 u16 sr;
258 u32 pc;
259 } g _packed;
260 struct {
261 u16 fc;
262 u32 addr;
263 u16 ir;
264 u16 sr;
265 u32 pc;
266 } bae _packed; // bus/address error frame
267 };
268} _packed;
269
270int xtttt(void) { return sizeof(struct exc_frame); }
271
272void exception(const struct exc_frame *f)
ab6ed3c4 273{
39ac9835 274 int i;
275
276 while (read16(GFX_CTRL_PORT) & 2)
ab6ed3c4 277 ;
39ac9835 278 write16(GFX_CTRL_PORT, 0x8000 | (VDP_MODE1 << 8) | 0x04);
279 write16(GFX_CTRL_PORT, 0x8000 | (VDP_MODE2 << 8) | 0x44);
280 /* adjust scroll */
281 write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
282 write16(GFX_DATA_PORT,
283 printf_ypos >= CSCREEN_H ?
284 (printf_ypos - CSCREEN_H + 1) * 8 : 0);
285
286 printf("exception %i ", f->ecxnum);
287 if (f->ecxnum < ARRAY_SIZE(exc_names) && exc_names[f->ecxnum] != NULL)
288 printf("(%s)", exc_names[f->ecxnum]);
289 if (f->ecxnum < 4)
290 printf(" (%s)", (f->bae.fc & 0x10) ? "r" : "w");
291 printf(" \n");
292
293 if (f->ecxnum < 4) {
294 printf(" PC: %08x SR: %04x \n", f->bae.pc, f->bae.sr);
295 printf("addr: %08x IR: %04x FC: %02x \n",
296 f->bae.addr, f->bae.ir, f->bae.fc);
297 }
298 else {
299 printf(" PC: %08x SR: %04x \n", f->g.pc, f->g.sr);
300 }
301 for (i = 0; i < 8; i++)
302 printf(" D%d: %08x A%d: %08x \n", i, f->dr[i], i, f->ar[i]);
303 printf(" \n");
ab6ed3c4 304}
305
306void vbl(void)
307{
308}
309
51f3a685 310static int usb_read_while_ready(OsRoutine *ed,
fb63f62c 311 void *buf_, unsigned int maxlen)
51f3a685 312{
313 u8 *buf = buf_;
fb63f62c 314 unsigned int r = 0;
51f3a685 315
316 while (ed->usbRdReady() && r < maxlen)
317 buf[r++] = ed->usbReadByte();
318
319 return r;
320}
321
fb63f62c 322static int usb_read(OsRoutine *ed, void *buf_, unsigned int maxlen)
323{
324 u8 *buf = buf_;
325 unsigned int r = 0;
326
327 while (r < maxlen)
328 buf[r++] = ed->usbReadByte();
329
330 return r;
331}
332
333static int usb_write(OsRoutine *ed, const void *buf_, unsigned int maxlen)
334{
335 const u8 *buf = buf_;
336 unsigned int r = 0;
337
338 while (r < maxlen)
339 ed->usbWriteByte(buf[r++]);
340
341 return r;
342}
343
51f3a685 344/*
345 * TH = 1 : ?1CBRLDU 3-button pad return value (not read)
346 * TH = 0 : ?0SA00DU 3-button pad return value
347 * TH = 1 : ?1CBRLDU 3-button pad return value
348 * TH = 0 : ?0SA0000 D3-0 are forced to '0'
349 * TH = 1 : ?1CBMXYZ Extra buttons returned in D3-0
350 * TH = 0 : ?0SA1111 D3-0 are forced to '1'
351 */
352static void test_joy_latency(int *min_out, int *max_out)
353{
354 u8 rbuf[8 * 6];
355 int min = 8;
356 int max = 0;
357 int i, v, b, e;
358
359 for (i = 0; i < 64; i++) {
360 read_joy_responses(rbuf);
361
362 for (b = 0; b < 8 * 4; b++) {
363 v = b & 7;
364 e = (b & 0x08) ? 0x0c : 0;
365 if ((rbuf[b] & 0x0c) == e) {
366 if (v < min)
367 min = v;
368 }
369 else if (v > max)
370 max = v;
371 }
372 }
373
374 /* print out the last test */
375 for (b = 0; b < 8 * 5; b++) {
376 printf(" %02x", rbuf[b]);
377 if ((b & 7) == 7)
378 printf("\n");
379 }
380 printf("\n");
381
382 *min_out = min;
383 *max_out = max;
384}
385
386static int do_test(OsRoutine *ed, u8 b3)
387{
388 int min = 0, max = 0;
eea25dd1 389 int i, t, len, seed;
390 u8 *p, v;
51f3a685 391
392 switch (b3)
393 {
ad997e79 394 case '0':
395 printf("reading..\n");
eea25dd1 396 test_joy_read_log((void *)0x200000, 0x20000, 1);
397 //test_joy_read_log((void *)0xff0200, 0x0f000, 1);
ad997e79 398 printf("done\n");
399 return 0;
400 case '1':
401 printf("reading w/vsync..\n");
402 test_joy_read_log_vsync((void *)0x200000, 3600 * 2);
403 printf("done\n");
404 return 0;
eea25dd1 405 case '2':
406 case '3':
407 printf("3btn_idle test..\n");
408 p = (void *)0x200000;
409 len = 0x20000;
410 test_joy_read_log(p, len, b3 == '3');
411 for (i = 0; i < len; i++) {
412 static const u8 e[2] = { 0x33, 0x7f };
413 v = e[i & 1];
414 if (p[i] != v)
415 printf("%06x: bad: %02x %02x\n", &p[i], p[i], v);
416 }
417 printf("done\n");
418 return 0;
419 case '4':
420 printf("3btn_idle data test..\n");
421 p = (void *)0x200000;
422 len = 0x20000;
423 for (i = 0; i < len; i++) {
424 static const u8 e[2] = { 0x33, 0x7f };
425 v = e[i & 1];
426 if (p[i] != v)
427 printf("%06x: bad: %02x %02x\n", &p[i], p[i], v);
428 }
429 printf("done\n");
430 return 0;
431 case '5':
432 seed = read8(0xC00009);
433 printf("testing, seed=%02x\n", seed);
434 p = (void *)0x200000;
435 len = 0x100000;
436 test_byte_write(p, len, seed);
437 for (t = 0; t < 2; t++) {
438 for (i = 0; i < len; i++) {
439 v = (u8)(i + seed);
440 if (p[i] != v)
441 printf("%06x: bad: %02x %02x\n", &p[i], p[i], v);
442 }
443 printf("done (%d)\n", t);
444 }
445 return 0;
51f3a685 446 case 'j':
447 test_joy_latency(&min, &max);
448 printf("latency: %d - %d\n\n", min, max);
449 return 0;
450 default:
451 break;
452 }
453
454 return -1;
455}
456
fb63f62c 457static int do_custom(OsRoutine *ed, u8 b3)
458{
459 struct {
460 unsigned int addr;
461 unsigned int size;
462 } d;
463
464 switch (b3)
465 {
466 case 'd':
467 usb_read(ed, &d, sizeof(d));
468 ed->usbWriteByte('k');
469 printf("sending %i bytes from %06x..\n", d.size, d.addr);
470 usb_write(ed, (void *)d.addr, d.size);
471 printf("done.\n");
472 return 1;
473 default:
474 break;
475 }
476
477 return -1;
478}
479
df43aeea 480#define MTYPE_OS 0
481#define MTYPE_MD 1
482#define MTYPE_SSF 2
483#define MTYPE_CD 3
484#define MTYPE_SMS 4
485#define MTYPE_10M 5
486#define MTYPE_32X 6
487
65d9165c 488static int do_run(OsRoutine *ed, u8 b3, int tas_sync)
df43aeea 489{
490 u8 mapper = 0;
491
492 switch (b3)
493 {
494 case 's':
495 mapper = MTYPE_SMS | (7 << 4);
496 break;
497 case 'm':
498 mapper = MTYPE_MD;
499 break;
500 case 'o':
501 mapper = MTYPE_OS;
502 break;
503 case 'c':
504 mapper = MTYPE_CD;
505 break;
506 case '3':
507 mapper = MTYPE_32X;
508 break;
509 case 'M':
510 mapper = MTYPE_10M;
511 break;
512 default:
513 return -1;
514 }
515
65d9165c 516 printf("starting mapper %x..\n", mapper);
517
518 while (read16(GFX_CTRL_PORT) & 2)
df43aeea 519 ;
520 ed->VDP_setReg(VDP_MODE1, 0x04);
521 ed->VDP_setReg(VDP_MODE2, 0x44);
522
523 ed->usbWriteByte('k');
524
65d9165c 525 run_game(mapper, tas_sync);
df43aeea 526 /* should not get here.. */
527
528 return -1;
529}
530
ab6ed3c4 531int main()
532{
533 OsRoutine *ed;
51f3a685 534 u8 buf[16];
df43aeea 535 int len;
51f3a685 536 int i, d, ret;
ab6ed3c4 537
538 ed = (OsRoutine *) *(u32 *)0x1A0;
539 ed->memInitDmaCode();
540
541 ed->VDP_setReg(VDP_MODE1, 0x04);
542 ed->VDP_setReg(VDP_MODE2, 0x64);
543 ed->VDP_setReg(VDP_AUTOINC, 2);
544 ed->VDP_setReg(VDP_SCROLLSZ, 0x01);
545
546 /* clear name tables */
547 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(APLANE));
548 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
549 write32(GFX_DATA_PORT, 0);
550
551 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(BPLANE));
552 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
553 write32(GFX_DATA_PORT, 0);
554
fb63f62c 555 /* scroll planes */
556 write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
557 write32(GFX_DATA_PORT, 0);
558
ab6ed3c4 559 /* note: relying on ED menu's font setup here.. */
560
fb63f62c 561 printf("\n");
65d9165c 562 printf("version: %02x, start_hvc: %04x\n",
563 read8(0xa10001), start_hvc);
c6d1079a 564 printf("ED os/fw: %d/%d\n\n", ed->osGetOsVersion(),
df43aeea 565 ed->osGetFirmVersion());
566
ab6ed3c4 567 for (;;) {
51f3a685 568 if (!ed->usbRdReady()) {
c6d1079a 569 /* note: stop corrupts SDRAM */
570 //asm volatile("stop #0x2000");
571 asm volatile(
572 "move.l #1000/10, %0\n"
573 "0: dbra %0, 0b\n" : "=r" (i) :: "cc");
51f3a685 574 continue;
575 }
576
577 buf[0] = ed->usbReadByte();
578 if (buf[0] == ' ')
579 continue;
580 if (buf[0] != '*') {
581 d = 1;
582 goto bad_input;
583 }
584
585 /* note: OS uses Twofgsr */
586 buf[1] = ed->usbReadByte();
587 switch (buf[1]) {
588 case 'T':
589 ed->usbWriteByte('k');
590 break;
df43aeea 591 case 'g':
592 len = ed->usbReadByte() * 128;
593 printf("loading %d bytes.. ", len * 512);
594 ed->usbWriteByte('k');
595 ed->usbReadDma((void *)0x200000, len);
596 ed->usbWriteByte('d');
597 printf("done\n");
598 break;
599 case 'r':
65d9165c 600 case 'R':
df43aeea 601 buf[2] = ed->usbReadByte();
65d9165c 602 ret = do_run(ed, buf[2], buf[1] == 'R');
df43aeea 603 if (ret != 0) {
604 d = 3;
605 goto bad_input;
606 }
607 printf("run returned??\n");
608 break;
609
51f3a685 610 /* custom */
611 case 't':
612 buf[2] = ed->usbReadByte();
613 ret = do_test(ed, buf[2]);
614 if (ret != 0) {
615 d = 3;
616 goto bad_input;
617 }
618 ed->usbWriteByte('k');
619 break;
fb63f62c 620 case 'x':
621 buf[2] = ed->usbReadByte();
622 ret = do_custom(ed, buf[2]);
623 if (ret == 1)
624 break;
625 if (ret != 0) {
626 d = 3;
627 goto bad_input;
628 }
629 ed->usbWriteByte('k');
630 break;
51f3a685 631 default:
632 d = 2;
633 goto bad_input;
634 }
635
636 continue;
637
638bad_input:
639 ret = usb_read_while_ready(ed, buf + d, sizeof(buf) - d);
640 buf[d + ret] = 0;
641 printf("bad cmd: %s\n", buf);
642 /* consume all remaining data */
643 while (ed->usbRdReady())
644 usb_read_while_ready(ed, buf, sizeof(buf));
ab6ed3c4 645
51f3a685 646 ed->usbWriteByte('b');
ab6ed3c4 647 }
648
649 return 0;
650}
651
652// vim:ts=4:sw=4:expandtab