4 #define u8 unsigned char
5 #define u16 unsigned short
6 #define u32 unsigned int
8 #define noinline __attribute__((noinline))
9 #define _packed __attribute__((packed))
11 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
18 #define GFX_DATA_PORT 0xC00000
19 #define GFX_CTRL_PORT 0xC00004
21 #define TILE_MEM_END 0xB000
24 #define TILE_FONT_BASE (TILE_MEM_END / 32 - FONT_LEN)
26 /* note: using ED menu's layout here.. */
27 #define WPLANE (TILE_MEM_END + 0x0000)
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)
34 *((volatile u8 *) (a))
36 *((volatile u16 *) (a))
38 *((volatile u32 *) (a))
39 #define write8(a, d) \
40 *((volatile u8 *) (a)) = (d)
41 #define write16(a, d) \
42 *((volatile u16 *) (a)) = (d)
43 #define write32(a, d) \
44 *((volatile u32 *) (a)) = (d)
46 #define GFX_WRITE_VRAM_ADDR(adr) \
47 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00)
48 #define GFX_WRITE_VSRAM_ADDR(adr) \
49 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x10)
62 #define LEFT_BORDER 1 /* lame TV */
67 static noinline void VDP_drawTextML(const char *str, u16 plane_base,
70 const u8 *src = (const u8 *)str;
72 int max_len = 40 - LEFT_BORDER;
78 for (len = 0; str[len] && len < max_len; len++)
80 if (len > (PLANE_W - x))
83 addr = plane_base + ((x + (PLANE_W * y)) << 1);
84 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr));
87 write16(GFX_DATA_PORT,
88 basetile | ((*src++) - 32 + TILE_FONT_BASE));
92 static int printf_ypos;
94 static void printf_line(int x, const char *buf)
99 VDP_drawTextML(buf, APLANE, x, printf_ypos++ & (PLANE_H - 1));
101 if (printf_ypos >= CSCREEN_H) {
102 /* clear next line */
104 addr += (PLANE_W * (printf_ypos & (PLANE_H - 1))) << 1;
105 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr));
106 for (i = 0; i < 40 / 2; i++)
107 write32(GFX_DATA_PORT, 0);
110 write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
111 write16(GFX_DATA_PORT, (printf_ypos - CSCREEN_H + 1) * 8);
115 #define PRINTF_LEN 40
117 static noinline int printf(const char *fmt, ...)
119 static const char hexchars[] = "0123456789abcdef";
120 static int printf_xpos;
121 char c, buf[PRINTF_LEN + 11 + 1];
130 for (d = 0; *fmt; ) {
141 printf_line(printf_xpos, buf);
157 while ('1' <= *fmt && *fmt <= '9') {
158 fwidth = fwidth * 10 + *fmt - '0';
168 ival = va_arg(ap, int);
173 for (i = 1000000000; i >= 10; i /= 10)
176 for (; i >= 10; i /= 10) {
177 buf[d++] = '0' + ival / i;
180 buf[d++] = '0' + ival;
183 uval = va_arg(ap, int);
184 while (fwidth > 1 && uval < (1 << (fwidth - 1) * 4)) {
185 buf[d++] = prefix0 ? '0' : ' ';
188 for (j = 1; j < 8 && uval >= (1 << j * 4); j++)
190 for (j--; j >= 0; j--)
191 buf[d++] = hexchars[(uval >> j * 4) & 0x0f];
194 s = va_arg(ap, char *);
195 while (*s && d < PRINTF_LEN)
199 // don't handle, for now
210 VDP_drawTextML(buf, APLANE, printf_xpos,
211 printf_ypos & (PLANE_H - 1));
218 static u8 gethex(char c)
220 if ('0' <= c && c <= '9')
222 if ('a' <= c && c <= 'f')
224 if ('A' <= c && c <= 'F')
229 static const char *exc_names[] = {
234 "Illegal Instruction",
238 "Privilege Violation", /* 8 8 */
240 "Line 1010 Emulator",
241 "Line 1111 Emulator",
245 "Uninitialized Interrupt",
254 "Spurious Interrupt", /* 18 24 */
267 u16 ecxnum; // from handler
279 } bae _packed; // bus/address error frame
283 int xtttt(void) { return sizeof(struct exc_frame); }
285 void exception(const struct exc_frame *f)
289 while (read16(GFX_CTRL_PORT) & 2)
291 write16(GFX_CTRL_PORT, 0x8000 | (VDP_MODE1 << 8) | 0x04);
292 write16(GFX_CTRL_PORT, 0x8000 | (VDP_MODE2 << 8) | 0x44);
294 write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
295 write16(GFX_DATA_PORT,
296 printf_ypos >= CSCREEN_H ?
297 (printf_ypos - CSCREEN_H + 1) * 8 : 0);
299 printf("exception %i ", f->ecxnum);
300 if (f->ecxnum < ARRAY_SIZE(exc_names) && exc_names[f->ecxnum] != NULL)
301 printf("(%s)", exc_names[f->ecxnum]);
303 printf(" (%s)", (f->bae.fc & 0x10) ? "r" : "w");
307 printf(" PC: %08x SR: %04x \n", f->bae.pc, f->bae.sr);
308 printf("addr: %08x IR: %04x FC: %02x \n",
309 f->bae.addr, f->bae.ir, f->bae.fc);
312 printf(" PC: %08x SR: %04x \n", f->g.pc, f->g.sr);
314 for (i = 0; i < 8; i++)
315 printf(" D%d: %08x A%d: %08x \n", i, f->dr[i], i, f->ar[i]);
323 static int usb_read_while_ready(OsRoutine *ed,
324 void *buf_, unsigned int maxlen)
329 while (ed->usbRdReady() && r < maxlen)
330 buf[r++] = ed->usbReadByte();
335 static int usb_read(OsRoutine *ed, void *buf_, unsigned int maxlen)
341 buf[r++] = ed->usbReadByte();
346 static int usb_write(OsRoutine *ed, const void *buf_, unsigned int maxlen)
348 const u8 *buf = buf_;
352 ed->usbWriteByte(buf[r++]);
358 * TH = 1 : ?1CBRLDU 3-button pad return value (not read)
359 * TH = 0 : ?0SA00DU 3-button pad return value
360 * TH = 1 : ?1CBRLDU 3-button pad return value
361 * TH = 0 : ?0SA0000 D3-0 are forced to '0'
362 * TH = 1 : ?1CBMXYZ Extra buttons returned in D3-0
363 * TH = 0 : ?0SA1111 D3-0 are forced to '1'
365 static void test_joy_latency(int *min_out, int *max_out)
372 for (i = 0; i < 64; i++) {
373 read_joy_responses(rbuf);
375 for (b = 0; b < 8 * 4; b++) {
377 e = (b & 0x08) ? 0x0c : 0;
378 if ((rbuf[b] & 0x0c) == e) {
387 /* print out the last test */
388 for (b = 0; b < 8 * 5; b++) {
389 printf(" %02x", rbuf[b]);
399 static int do_test(OsRoutine *ed, u8 b3)
401 int min = 0, max = 0;
408 printf("reading..\n");
409 test_joy_read_log((void *)0x200000, 0x20000, 1);
410 //test_joy_read_log((void *)0xff0200, 0x0f000, 1);
414 printf("reading w/vsync..\n");
415 test_joy_read_log_vsync((void *)0x200000, 3600 * 2);
420 printf("3btn_idle test..\n");
421 p = (void *)0x200000;
423 test_joy_read_log(p, len, b3 == '3');
424 for (i = 0; i < len; i++) {
425 static const u8 e[2] = { 0x33, 0x7f };
428 printf("%06x: bad: %02x %02x\n", &p[i], p[i], v);
433 printf("3btn_idle data test..\n");
434 p = (void *)0x200000;
436 for (i = 0; i < len; i++) {
437 static const u8 e[2] = { 0x33, 0x7f };
440 printf("%06x: bad: %02x %02x\n", &p[i], p[i], v);
445 seed = read8(0xC00009);
446 printf("testing, seed=%02x\n", seed);
447 p = (void *)0x200000;
449 test_byte_write(p, len, seed);
450 for (t = 0; t < 2; t++) {
451 for (i = 0; i < len; i++) {
454 printf("%06x: bad: %02x %02x\n", &p[i], p[i], v);
456 printf("done (%d)\n", t);
460 test_joy_latency(&min, &max);
461 printf("latency: %d - %d\n\n", min, max);
470 static int do_custom(OsRoutine *ed, u8 b3)
480 usb_read(ed, &d, sizeof(d));
481 ed->usbWriteByte('k');
482 printf("sending %i bytes from %06x..\n", d.size, d.addr);
483 usb_write(ed, (void *)d.addr, d.size);
501 static int do_run(OsRoutine *ed, u8 b3, int tas_sync)
508 mapper = MTYPE_SMS | (7 << 4);
526 // raw numer: hex XX: mtype | x;
527 // x: bits [4-7]: SRAM_ON, SRAM_3M_ON, SNAP_SAVE_ON, MKEY
528 mapper = gethex(ed->usbReadByte()) << 4;
529 mapper |= gethex(ed->usbReadByte());
535 printf("syncing and starting mapper %x..\n", mapper);
537 while (read16(GFX_CTRL_PORT) & 2)
539 ed->VDP_setReg(VDP_MODE1, 0x04);
540 ed->VDP_setReg(VDP_MODE2, 0x44);
542 ed->usbWriteByte('k');
544 run_game(mapper, tas_sync);
545 /* should not get here.. */
552 u8 *mem = (u8 *)0xa00000;
558 while (read8(0xa11100) & 1)
561 /* must use byte access */
562 for (i = 0x2000; i > 0; i--)
565 /* console starts with reset on, busreq off,
566 * gens starts with busreq on, keep that for gmv.. */
576 ed = (OsRoutine *) *(u32 *)0x1A0;
577 ed->memInitDmaCode();
580 while (read16(GFX_CTRL_PORT) & 2)
583 ed->VDP_setReg(VDP_MODE1, 0x04);
584 ed->VDP_setReg(VDP_MODE2, 0x64);
585 ed->VDP_setReg(VDP_AUTOINC, 2);
586 ed->VDP_setReg(VDP_SCROLLSZ, 0x01);
588 /* clear name tables */
589 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(APLANE));
590 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
591 write32(GFX_DATA_PORT, 0);
593 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(BPLANE));
594 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
595 write32(GFX_DATA_PORT, 0);
598 write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
599 write32(GFX_DATA_PORT, 0);
601 /* note: relying on ED menu's font setup here.. */
604 printf("version: %02x, hvc: %04x %04x\n",
605 read8(0xa10001), start_hvc, read16(0xc00008));
606 printf("ED os/fw: %d/%d\n\n", ed->osGetOsVersion(),
607 ed->osGetFirmVersion());
612 if (!ed->usbRdReady()) {
613 /* note: stop corrupts SDRAM */
614 //asm volatile("stop #0x2000");
616 "move.l #1000/10, %0\n"
617 "0: dbra %0, 0b\n" : "=r" (i) :: "cc");
621 buf[0] = ed->usbReadByte();
629 /* note: OS uses Twofgsr */
630 buf[1] = ed->usbReadByte();
633 ed->usbWriteByte('k');
636 len = ed->usbReadByte() * 128;
637 printf("loading %d bytes.. ", len * 512);
638 ed->usbWriteByte('k');
639 ed->usbReadDma((void *)0x200000, len);
640 ed->usbWriteByte('d');
645 buf[2] = ed->usbReadByte();
646 ret = do_run(ed, buf[2], buf[1] == 'R');
651 printf("run returned??\n");
656 buf[2] = ed->usbReadByte();
657 ret = do_test(ed, buf[2]);
662 ed->usbWriteByte('k');
665 buf[2] = ed->usbReadByte();
666 ret = do_custom(ed, buf[2]);
673 ed->usbWriteByte('k');
683 ret = usb_read_while_ready(ed, buf + d, sizeof(buf) - d);
685 printf("bad cmd: %s\n", buf);
686 /* consume all remaining data */
687 while (ed->usbRdReady())
688 usb_read_while_ready(ed, buf, sizeof(buf));
690 ed->usbWriteByte('b');
696 // vim:ts=4:sw=4:expandtab