3 #define u8 unsigned char
4 #define u16 unsigned short
5 #define u32 unsigned int
7 #define noinline __attribute__((noinline))
12 #define GFX_DATA_PORT 0xC00000
13 #define GFX_CTRL_PORT 0xC00004
15 #define TILE_MEM_END 0xB000
18 #define TILE_FONT_BASE (TILE_MEM_END / 32 - FONT_LEN)
20 /* note: using ED menu's layout here.. */
21 #define WPLAN (TILE_MEM_END + 0x0000)
22 #define HSCRL (TILE_MEM_END + 0x0800)
23 #define SLIST (TILE_MEM_END + 0x0C00)
24 #define APLANE (TILE_MEM_END + 0x1000)
25 #define BPLANE (TILE_MEM_END + 0x3000)
27 #define write16(a, d) \
28 *((volatile u16 *) (a)) = (d)
29 #define write32(a, d) \
30 *((volatile u32 *) (a)) = (d)
32 #define GFX_WRITE_VRAM_ADDR(adr) \
33 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00)
34 #define GFX_WRITE_VSRAM_ADDR(adr) \
35 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x10)
48 #define LEFT_BORDER 1 /* lame TV */
53 static noinline void VDP_drawTextML(const char *str, u16 plane_base,
56 const u8 *src = (const u8 *)str;
58 int max_len = 40 - LEFT_BORDER;
64 for (len = 0; str[len] && len < max_len; len++)
66 if (len > (PLANE_W - x))
69 addr = plane_base + ((x + (PLANE_W * y)) << 1);
70 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr));
73 write16(GFX_DATA_PORT,
74 basetile | ((*src++) - 32 + TILE_FONT_BASE));
78 static int printf_ypos;
80 static void printf_line(int x, const char *buf)
85 VDP_drawTextML(buf, APLANE, x, printf_ypos++ & (PLANE_H - 1));
87 if (printf_ypos >= CSCREEN_H) {
90 addr += (PLANE_W * (printf_ypos & (PLANE_H - 1))) << 1;
91 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr));
92 for (i = 0; i < 40 / 2; i++)
93 write32(GFX_DATA_PORT, 0);
96 write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
97 write16(GFX_DATA_PORT, (printf_ypos - 27) * 8);
101 #define PRINTF_LEN 40
103 static noinline int printf(const char *fmt, ...)
105 static const char hexchars[] = "0123456789abcdef";
106 static int printf_xpos;
107 char c, buf[PRINTF_LEN + 11 + 1];
116 for (d = 0; *fmt; ) {
127 printf_line(printf_xpos, buf);
143 while ('1' <= *fmt && *fmt <= '9') {
144 fwidth = fwidth * 10 + *fmt - '0';
154 ival = va_arg(ap, int);
159 for (i = 1000000000; i >= 10; i /= 10)
162 for (; i >= 10; i /= 10) {
163 buf[d++] = '0' + ival / i;
166 buf[d++] = '0' + ival;
169 uval = va_arg(ap, int);
170 while (fwidth > 0 && uval < (1 << (fwidth - 1) * 4)) {
171 buf[d++] = prefix0 ? '0' : ' ';
174 for (j = 1; j < 8 && uval >= (1 << j * 4); j++)
176 for (j--; j >= 0; j--)
177 buf[d++] = hexchars[(uval >> j * 4) & 0x0f];
180 s = va_arg(ap, char *);
181 while (*s && d < PRINTF_LEN)
185 // don't handle, for now
196 VDP_drawTextML(buf, APLANE, printf_xpos,
197 printf_ypos & (PLANE_H - 1));
206 VDP_drawTextML("============", APLANE, 0, 0);
207 VDP_drawTextML(" exception! ", APLANE, 0, 1);
208 VDP_drawTextML("============", APLANE, 0, 2);
217 static int usb_read_while_ready(OsRoutine *ed,
218 void *buf_, int maxlen)
223 while (ed->usbRdReady() && r < maxlen)
224 buf[r++] = ed->usbReadByte();
230 * TH = 1 : ?1CBRLDU 3-button pad return value (not read)
231 * TH = 0 : ?0SA00DU 3-button pad return value
232 * TH = 1 : ?1CBRLDU 3-button pad return value
233 * TH = 0 : ?0SA0000 D3-0 are forced to '0'
234 * TH = 1 : ?1CBMXYZ Extra buttons returned in D3-0
235 * TH = 0 : ?0SA1111 D3-0 are forced to '1'
237 static void test_joy_latency(int *min_out, int *max_out)
244 for (i = 0; i < 64; i++) {
245 read_joy_responses(rbuf);
247 for (b = 0; b < 8 * 4; b++) {
249 e = (b & 0x08) ? 0x0c : 0;
250 if ((rbuf[b] & 0x0c) == e) {
259 /* print out the last test */
260 for (b = 0; b < 8 * 5; b++) {
261 printf(" %02x", rbuf[b]);
271 static int do_test(OsRoutine *ed, u8 b3)
273 int min = 0, max = 0;
278 test_joy_latency(&min, &max);
279 printf("latency: %d - %d\n\n", min, max);
294 ed = (OsRoutine *) *(u32 *)0x1A0;
295 ed->memInitDmaCode();
297 ed->VDP_setReg(VDP_MODE1, 0x04);
298 ed->VDP_setReg(VDP_MODE2, 0x64);
299 ed->VDP_setReg(VDP_AUTOINC, 2);
300 ed->VDP_setReg(VDP_SCROLLSZ, 0x01);
302 /* clear name tables */
303 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(APLANE));
304 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
305 write32(GFX_DATA_PORT, 0);
307 write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(BPLANE));
308 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
309 write32(GFX_DATA_PORT, 0);
311 /* note: relying on ED menu's font setup here.. */
314 if (!ed->usbRdReady()) {
315 asm volatile("stop #0x2000");
319 buf[0] = ed->usbReadByte();
327 /* note: OS uses Twofgsr */
328 buf[1] = ed->usbReadByte();
331 ed->usbWriteByte('k');
335 buf[2] = ed->usbReadByte();
336 ret = do_test(ed, buf[2]);
341 ed->usbWriteByte('k');
351 ret = usb_read_while_ready(ed, buf + d, sizeof(buf) - d);
353 printf("bad cmd: %s\n", buf);
354 /* consume all remaining data */
355 while (ed->usbRdReady())
356 usb_read_while_ready(ed, buf, sizeof(buf));
358 ed->usbWriteByte('b');
364 // vim:ts=4:sw=4:expandtab