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]))
16 #define GFX_DATA_PORT 0xC00000
17 #define GFX_CTRL_PORT 0xC00004
19 #define TILE_MEM_END 0xB000
22 #define TILE_FONT_BASE (TILE_MEM_END / 32 - FONT_LEN)
24 /* note: using ED menu's layout here.. */
25 #define WPLAN (TILE_MEM_END + 0x0000)
26 #define HSCRL (TILE_MEM_END + 0x0800)
27 #define SLIST (TILE_MEM_END + 0x0C00)
28 #define APLANE (TILE_MEM_END + 0x1000)
29 #define BPLANE (TILE_MEM_END + 0x3000)
32 *((volatile u8 *) (a))
34 *((volatile u16 *) (a))
36 *((volatile u32 *) (a))
37 #define write16(a, d) \
38 *((volatile u16 *) (a)) = (d)
39 #define write32(a, d) \
40 *((volatile u32 *) (a)) = (d)
42 #define GFX_WRITE_VRAM_ADDR(adr) \
43 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00)
44 #define GFX_WRITE_VSRAM_ADDR(adr) \
45 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x10)
58 #define LEFT_BORDER 1 /* lame TV */
63 static noinline void VDP_drawTextML(const char *str, u16 plane_base,
66 const u8 *src = (const u8 *)str;
68 int max_len = 40 - LEFT_BORDER;
74 for (len = 0; str[len] && len < max_len; len++)
76 if (len > (PLANE_W - x))
79 addr = plane_base + ((x + (PLANE_W * y)) << 1);
80 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr));
83 write16(GFX_DATA_PORT,
84 basetile | ((*src++) - 32 + TILE_FONT_BASE));
88 static int printf_ypos;
90 static void printf_line(int x, const char *buf)
95 VDP_drawTextML(buf, APLANE, x, printf_ypos++ & (PLANE_H - 1));
97 if (printf_ypos >= CSCREEN_H) {
100 addr += (PLANE_W * (printf_ypos & (PLANE_H - 1))) << 1;
101 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr));
102 for (i = 0; i < 40 / 2; i++)
103 write32(GFX_DATA_PORT, 0);
106 write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
107 write16(GFX_DATA_PORT, (printf_ypos - CSCREEN_H + 1) * 8);
111 #define PRINTF_LEN 40
113 static noinline int printf(const char *fmt, ...)
115 static const char hexchars[] = "0123456789abcdef";
116 static int printf_xpos;
117 char c, buf[PRINTF_LEN + 11 + 1];
126 for (d = 0; *fmt; ) {
137 printf_line(printf_xpos, buf);
153 while ('1' <= *fmt && *fmt <= '9') {
154 fwidth = fwidth * 10 + *fmt - '0';
164 ival = va_arg(ap, int);
169 for (i = 1000000000; i >= 10; i /= 10)
172 for (; i >= 10; i /= 10) {
173 buf[d++] = '0' + ival / i;
176 buf[d++] = '0' + ival;
179 uval = va_arg(ap, int);
180 while (fwidth > 1 && uval < (1 << (fwidth - 1) * 4)) {
181 buf[d++] = prefix0 ? '0' : ' ';
184 for (j = 1; j < 8 && uval >= (1 << j * 4); j++)
186 for (j--; j >= 0; j--)
187 buf[d++] = hexchars[(uval >> j * 4) & 0x0f];
190 s = va_arg(ap, char *);
191 while (*s && d < PRINTF_LEN)
195 // don't handle, for now
206 VDP_drawTextML(buf, APLANE, printf_xpos,
207 printf_ypos & (PLANE_H - 1));
214 static const char *exc_names[] = {
219 "Illegal Instruction",
223 "Privilege Violation", /* 8 8 */
225 "Line 1010 Emulator",
226 "Line 1111 Emulator",
230 "Uninitialized Interrupt",
239 "Spurious Interrupt", /* 18 24 */
252 u16 ecxnum; // from handler
264 } bae _packed; // bus/address error frame
268 int xtttt(void) { return sizeof(struct exc_frame); }
270 void exception(const struct exc_frame *f)
274 while (read16(GFX_CTRL_PORT) & 2)
276 write16(GFX_CTRL_PORT, 0x8000 | (VDP_MODE1 << 8) | 0x04);
277 write16(GFX_CTRL_PORT, 0x8000 | (VDP_MODE2 << 8) | 0x44);
279 write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
280 write16(GFX_DATA_PORT,
281 printf_ypos >= CSCREEN_H ?
282 (printf_ypos - CSCREEN_H + 1) * 8 : 0);
284 printf("exception %i ", f->ecxnum);
285 if (f->ecxnum < ARRAY_SIZE(exc_names) && exc_names[f->ecxnum] != NULL)
286 printf("(%s)", exc_names[f->ecxnum]);
288 printf(" (%s)", (f->bae.fc & 0x10) ? "r" : "w");
292 printf(" PC: %08x SR: %04x \n", f->bae.pc, f->bae.sr);
293 printf("addr: %08x IR: %04x FC: %02x \n",
294 f->bae.addr, f->bae.ir, f->bae.fc);
297 printf(" PC: %08x SR: %04x \n", f->g.pc, f->g.sr);
299 for (i = 0; i < 8; i++)
300 printf(" D%d: %08x A%d: %08x \n", i, f->dr[i], i, f->ar[i]);
308 static int usb_read_while_ready(OsRoutine *ed,
309 void *buf_, int maxlen)
314 while (ed->usbRdReady() && r < maxlen)
315 buf[r++] = ed->usbReadByte();
321 * TH = 1 : ?1CBRLDU 3-button pad return value (not read)
322 * TH = 0 : ?0SA00DU 3-button pad return value
323 * TH = 1 : ?1CBRLDU 3-button pad return value
324 * TH = 0 : ?0SA0000 D3-0 are forced to '0'
325 * TH = 1 : ?1CBMXYZ Extra buttons returned in D3-0
326 * TH = 0 : ?0SA1111 D3-0 are forced to '1'
328 static void test_joy_latency(int *min_out, int *max_out)
335 for (i = 0; i < 64; i++) {
336 read_joy_responses(rbuf);
338 for (b = 0; b < 8 * 4; b++) {
340 e = (b & 0x08) ? 0x0c : 0;
341 if ((rbuf[b] & 0x0c) == e) {
350 /* print out the last test */
351 for (b = 0; b < 8 * 5; b++) {
352 printf(" %02x", rbuf[b]);
362 static int do_test(OsRoutine *ed, u8 b3)
364 int min = 0, max = 0;
369 test_joy_latency(&min, &max);
370 printf("latency: %d - %d\n\n", min, max);
387 static int do_run(OsRoutine *ed, u8 b3)
394 mapper = MTYPE_SMS | (7 << 4);
415 while (read32(GFX_CTRL_PORT) & 2)
417 ed->VDP_setReg(VDP_MODE1, 0x04);
418 ed->VDP_setReg(VDP_MODE2, 0x44);
420 ed->usbWriteByte('k');
423 /* should not get here.. */
435 ed = (OsRoutine *) *(u32 *)0x1A0;
436 ed->memInitDmaCode();
438 ed->VDP_setReg(VDP_MODE1, 0x04);
439 ed->VDP_setReg(VDP_MODE2, 0x64);
440 ed->VDP_setReg(VDP_AUTOINC, 2);
441 ed->VDP_setReg(VDP_SCROLLSZ, 0x01);
443 /* clear name tables */
444 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(APLANE));
445 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
446 write32(GFX_DATA_PORT, 0);
448 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(BPLANE));
449 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
450 write32(GFX_DATA_PORT, 0);
452 /* note: relying on ED menu's font setup here.. */
454 printf("version: %02x\n", read8(0xa10001));
455 printf("ED os/fw: %x/%x\n\n", ed->osGetOsVersion(),
456 ed->osGetFirmVersion());
459 if (!ed->usbRdReady()) {
460 asm volatile("stop #0x2000");
464 buf[0] = ed->usbReadByte();
472 /* note: OS uses Twofgsr */
473 buf[1] = ed->usbReadByte();
476 ed->usbWriteByte('k');
479 len = ed->usbReadByte() * 128;
480 printf("loading %d bytes.. ", len * 512);
481 ed->usbWriteByte('k');
482 ed->usbReadDma((void *)0x200000, len);
483 ed->usbWriteByte('d');
487 buf[2] = ed->usbReadByte();
488 ret = do_run(ed, buf[2]);
493 printf("run returned??\n");
498 buf[2] = ed->usbReadByte();
499 ret = do_test(ed, buf[2]);
504 ed->usbWriteByte('k');
514 ret = usb_read_while_ready(ed, buf + d, sizeof(buf) - d);
516 printf("bad cmd: %s\n", buf);
517 /* consume all remaining data */
518 while (ed->usbRdReady())
519 usb_read_while_ready(ed, buf, sizeof(buf));
521 ed->usbWriteByte('b');
527 // vim:ts=4:sw=4:expandtab