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 write16(a, d) \
40 *((volatile u16 *) (a)) = (d)
41 #define write32(a, d) \
42 *((volatile u32 *) (a)) = (d)
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)
60 #define LEFT_BORDER 1 /* lame TV */
65 static noinline void VDP_drawTextML(const char *str, u16 plane_base,
68 const u8 *src = (const u8 *)str;
70 int max_len = 40 - LEFT_BORDER;
76 for (len = 0; str[len] && len < max_len; len++)
78 if (len > (PLANE_W - x))
81 addr = plane_base + ((x + (PLANE_W * y)) << 1);
82 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr));
85 write16(GFX_DATA_PORT,
86 basetile | ((*src++) - 32 + TILE_FONT_BASE));
90 static int printf_ypos;
92 static void printf_line(int x, const char *buf)
97 VDP_drawTextML(buf, APLANE, x, printf_ypos++ & (PLANE_H - 1));
99 if (printf_ypos >= CSCREEN_H) {
100 /* clear next line */
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);
108 write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
109 write16(GFX_DATA_PORT, (printf_ypos - CSCREEN_H + 1) * 8);
113 #define PRINTF_LEN 40
115 static noinline int printf(const char *fmt, ...)
117 static const char hexchars[] = "0123456789abcdef";
118 static int printf_xpos;
119 char c, buf[PRINTF_LEN + 11 + 1];
128 for (d = 0; *fmt; ) {
139 printf_line(printf_xpos, buf);
155 while ('1' <= *fmt && *fmt <= '9') {
156 fwidth = fwidth * 10 + *fmt - '0';
166 ival = va_arg(ap, int);
171 for (i = 1000000000; i >= 10; i /= 10)
174 for (; i >= 10; i /= 10) {
175 buf[d++] = '0' + ival / i;
178 buf[d++] = '0' + ival;
181 uval = va_arg(ap, int);
182 while (fwidth > 1 && uval < (1 << (fwidth - 1) * 4)) {
183 buf[d++] = prefix0 ? '0' : ' ';
186 for (j = 1; j < 8 && uval >= (1 << j * 4); j++)
188 for (j--; j >= 0; j--)
189 buf[d++] = hexchars[(uval >> j * 4) & 0x0f];
192 s = va_arg(ap, char *);
193 while (*s && d < PRINTF_LEN)
197 // don't handle, for now
208 VDP_drawTextML(buf, APLANE, printf_xpos,
209 printf_ypos & (PLANE_H - 1));
216 static const char *exc_names[] = {
221 "Illegal Instruction",
225 "Privilege Violation", /* 8 8 */
227 "Line 1010 Emulator",
228 "Line 1111 Emulator",
232 "Uninitialized Interrupt",
241 "Spurious Interrupt", /* 18 24 */
254 u16 ecxnum; // from handler
266 } bae _packed; // bus/address error frame
270 int xtttt(void) { return sizeof(struct exc_frame); }
272 void exception(const struct exc_frame *f)
276 while (read16(GFX_CTRL_PORT) & 2)
278 write16(GFX_CTRL_PORT, 0x8000 | (VDP_MODE1 << 8) | 0x04);
279 write16(GFX_CTRL_PORT, 0x8000 | (VDP_MODE2 << 8) | 0x44);
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);
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]);
290 printf(" (%s)", (f->bae.fc & 0x10) ? "r" : "w");
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);
299 printf(" PC: %08x SR: %04x \n", f->g.pc, f->g.sr);
301 for (i = 0; i < 8; i++)
302 printf(" D%d: %08x A%d: %08x \n", i, f->dr[i], i, f->ar[i]);
310 static int usb_read_while_ready(OsRoutine *ed,
311 void *buf_, int maxlen)
316 while (ed->usbRdReady() && r < maxlen)
317 buf[r++] = ed->usbReadByte();
323 * TH = 1 : ?1CBRLDU 3-button pad return value (not read)
324 * TH = 0 : ?0SA00DU 3-button pad return value
325 * TH = 1 : ?1CBRLDU 3-button pad return value
326 * TH = 0 : ?0SA0000 D3-0 are forced to '0'
327 * TH = 1 : ?1CBMXYZ Extra buttons returned in D3-0
328 * TH = 0 : ?0SA1111 D3-0 are forced to '1'
330 static void test_joy_latency(int *min_out, int *max_out)
337 for (i = 0; i < 64; i++) {
338 read_joy_responses(rbuf);
340 for (b = 0; b < 8 * 4; b++) {
342 e = (b & 0x08) ? 0x0c : 0;
343 if ((rbuf[b] & 0x0c) == e) {
352 /* print out the last test */
353 for (b = 0; b < 8 * 5; b++) {
354 printf(" %02x", rbuf[b]);
364 static int do_test(OsRoutine *ed, u8 b3)
366 int min = 0, max = 0;
371 test_joy_latency(&min, &max);
372 printf("latency: %d - %d\n\n", min, max);
389 static int do_run(OsRoutine *ed, u8 b3, int tas_sync)
396 mapper = MTYPE_SMS | (7 << 4);
417 printf("starting mapper %x..\n", mapper);
419 while (read16(GFX_CTRL_PORT) & 2)
421 ed->VDP_setReg(VDP_MODE1, 0x04);
422 ed->VDP_setReg(VDP_MODE2, 0x44);
424 ed->usbWriteByte('k');
426 run_game(mapper, tas_sync);
427 /* should not get here.. */
439 ed = (OsRoutine *) *(u32 *)0x1A0;
440 ed->memInitDmaCode();
442 ed->VDP_setReg(VDP_MODE1, 0x04);
443 ed->VDP_setReg(VDP_MODE2, 0x64);
444 ed->VDP_setReg(VDP_AUTOINC, 2);
445 ed->VDP_setReg(VDP_SCROLLSZ, 0x01);
447 /* clear name tables */
448 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(APLANE));
449 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
450 write32(GFX_DATA_PORT, 0);
452 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(BPLANE));
453 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
454 write32(GFX_DATA_PORT, 0);
456 /* note: relying on ED menu's font setup here.. */
458 printf("version: %02x, start_hvc: %04x\n",
459 read8(0xa10001), start_hvc);
460 printf("ED os/fw: %x/%x\n\n", ed->osGetOsVersion(),
461 ed->osGetFirmVersion());
464 if (!ed->usbRdReady()) {
465 asm volatile("stop #0x2000");
469 buf[0] = ed->usbReadByte();
477 /* note: OS uses Twofgsr */
478 buf[1] = ed->usbReadByte();
481 ed->usbWriteByte('k');
484 len = ed->usbReadByte() * 128;
485 printf("loading %d bytes.. ", len * 512);
486 ed->usbWriteByte('k');
487 ed->usbReadDma((void *)0x200000, len);
488 ed->usbWriteByte('d');
493 buf[2] = ed->usbReadByte();
494 ret = do_run(ed, buf[2], buf[1] == 'R');
499 printf("run returned??\n");
504 buf[2] = ed->usbReadByte();
505 ret = do_test(ed, buf[2]);
510 ed->usbWriteByte('k');
520 ret = usb_read_while_ready(ed, buf + d, sizeof(buf) - d);
522 printf("bad cmd: %s\n", buf);
523 /* consume all remaining data */
524 while (ed->usbRdReady())
525 usb_read_while_ready(ed, buf, sizeof(buf));
527 ed->usbWriteByte('b');
533 // vim:ts=4:sw=4:expandtab