2 * This software is released into the public domain.
3 * See UNLICENSE file in top level directory.
9 //#pragma GCC diagnostic ignored "-Wunused-function"
11 #define VDP_DATA_PORT 0xC00000
12 #define VDP_CTRL_PORT 0xC00004
13 #define VDP_HV_COUNTER 0xC00008
15 #define TILE_MEM_END 0xB000
18 #define TILE_FONT_BASE (TILE_MEM_END - FONT_LEN * 32)
20 /* note: using ED menu's layout here.. */
21 #define WPLANE (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_z80le(a, d) \
28 ((volatile u8 *)(a))[0] = (u8)(d), \
29 ((volatile u8 *)(a))[1] = ((d) >> 8)
31 static inline u16 read16_z80le(const void *a_)
33 volatile const u8 *a = (volatile const u8 *)a_;
34 return a[0] | ((u16)a[1] << 8);
37 #define CTL_WRITE_VRAM(adr) \
38 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00)
39 #define CTL_WRITE_VSRAM(adr) \
40 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x10)
41 #define CTL_WRITE_CRAM(adr) \
42 (((0xC000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00)
43 #define CTL_READ_VRAM(adr) \
44 (((0x0000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00)
45 #define CTL_READ_VSRAM(adr) \
46 (((0x0000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x10)
47 #define CTL_READ_CRAM(adr) \
48 (((0x0000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x20)
50 #define CTL_WRITE_DMA 0x80
52 #define VDP_setReg(r, v) \
53 write16(VDP_CTRL_PORT, 0x8000 | ((r) << 8) | ((v) & 0xff))
58 VDP_NT_SCROLLA = 0x02,
60 VDP_NT_SCROLLB = 0x04,
75 #define VDP_MODE1_PS 0x04
76 #define VDP_MODE1_IE1 0x10 // h int
77 #define VDP_MODE2_MD 0x04
78 #define VDP_MODE2_PAL 0x08 // 30 col
79 #define VDP_MODE2_DMA 0x10
80 #define VDP_MODE2_IE0 0x20 // v int
81 #define VDP_MODE2_DISP 0x40
82 #define VDP_MODE2_128K 0x80
84 #define SR_PAL (1 << 0)
85 #define SR_DMA (1 << 1)
86 #define SR_HB (1 << 2)
87 #define SR_VB (1 << 3)
88 #define SR_ODD (1 << 4)
90 #define SR_SOVR (1 << 6)
92 #define SR_FULL (1 << 8)
93 #define SR_EMPT (1 << 9)
96 #define LEFT_BORDER 1 /* lame TV */
102 extern const u32 font_base[];
103 extern const u8 z80_test[];
104 extern const u8 z80_test_end[];
108 static noinline void VDP_drawTextML(const char *str, u16 plane_base,
111 const u8 *src = (const u8 *)str;
112 u16 basetile = text_pal << 13;
113 int max_len = 40 - LEFT_BORDER;
119 for (len = 0; str[len] && len < max_len; len++)
121 if (len > (PLANE_W - x))
124 addr = plane_base + ((x + (PLANE_W * y)) << 1);
125 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(addr));
128 write16(VDP_DATA_PORT,
129 basetile | ((*src++) - 32 + TILE_FONT_BASE / 32));
133 static int printf_ypos;
135 static void printf_line(int x, const char *buf)
140 VDP_drawTextML(buf, APLANE, x, printf_ypos++ & (PLANE_H - 1));
142 if (printf_ypos >= CSCREEN_H) {
143 /* clear next line */
145 addr += (PLANE_W * (printf_ypos & (PLANE_H - 1))) << 1;
146 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(addr));
147 for (i = 0; i < 40 / 2; i++)
148 write32(VDP_DATA_PORT, 0);
151 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
152 write16(VDP_DATA_PORT, (printf_ypos - CSCREEN_H + 1) * 8);
156 #define PRINTF_LEN 40
158 static int printf_xpos;
160 static noinline int printf(const char *fmt, ...)
162 static const char hexchars[] = "0123456789abcdef";
163 char c, buf[PRINTF_LEN + 11 + 1];
172 for (d = 0; *fmt; ) {
183 printf_line(printf_xpos, buf);
199 while ('1' <= *fmt && *fmt <= '9') {
200 fwidth = fwidth * 10 + *fmt - '0';
210 ival = va_arg(ap, int);
215 for (i = 1000000000; i >= 10; i /= 10)
218 for (; i >= 10; i /= 10) {
219 buf[d++] = '0' + ival / i;
222 buf[d++] = '0' + ival;
225 uval = va_arg(ap, int);
226 while (fwidth > 1 && uval < (1 << (fwidth - 1) * 4)) {
227 buf[d++] = prefix0 ? '0' : ' ';
230 for (j = 1; j < 8 && uval >= (1 << j * 4); j++)
232 for (j--; j >= 0; j--)
233 buf[d++] = hexchars[(uval >> j * 4) & 0x0f];
236 buf[d++] = va_arg(ap, int);
239 s = va_arg(ap, char *);
240 while (*s && d < PRINTF_LEN)
244 // don't handle, for now
255 VDP_drawTextML(buf, APLANE, printf_xpos,
256 printf_ypos & (PLANE_H - 1));
263 static const char *exc_names[] = {
268 "Illegal Instruction",
272 "Privilege Violation", /* 8 8 */
274 "Line 1010 Emulator",
275 "Line 1111 Emulator",
279 "Uninitialized Interrupt",
288 "Spurious Interrupt", /* 18 24 */
301 u16 ecxnum; // from handler
313 } bae _packed; // bus/address error frame
317 void exception(const struct exc_frame *f)
322 while (read16(VDP_CTRL_PORT) & 2)
324 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
325 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DISP);
327 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
328 write16(VDP_DATA_PORT,
329 printf_ypos >= CSCREEN_H ?
330 (printf_ypos - CSCREEN_H + 1) * 8 : 0);
332 printf("exception %i ", f->ecxnum);
333 if (f->ecxnum < ARRAY_SIZE(exc_names) && exc_names[f->ecxnum] != NULL)
334 printf("(%s)", exc_names[f->ecxnum]);
336 printf(" (%s)", (f->bae.fc & 0x10) ? "r" : "w");
340 printf(" PC: %08x SR: %04x \n", f->bae.pc, f->bae.sr);
341 printf("addr: %08x IR: %04x FC: %02x \n",
342 f->bae.addr, f->bae.ir, f->bae.fc);
346 printf(" PC: %08x SR: %04x \n", f->g.pc, f->g.sr);
349 for (i = 0; i < 8; i++)
350 printf(" D%d: %08x A%d: %08x \n", i, f->dr[i], i, f->ar[i]);
352 sp = (u32 *)(f->ar[7] + sp_add);
353 printf(" %08x %08x %08x %08x\n", sp[0], sp[1], sp[2], sp[3]);
354 printf(" %08x %08x %08x %08x\n", sp[4], sp[5], sp[6], sp[7]);
359 static void setup_default_palette(void)
361 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0));
362 write32(VDP_DATA_PORT, 0);
363 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(15 * 2)); // font normal
364 write16(VDP_DATA_PORT, 0xeee);
365 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(31 * 2)); // green
366 write16(VDP_DATA_PORT, 0x0e0);
367 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(47 * 2)); // red
368 write16(VDP_DATA_PORT, 0x00e);
371 static void do_setup_dma(const void *src_, u16 words)
374 // VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA);
375 VDP_setReg(VDP_DMA_LEN0, words);
376 VDP_setReg(VDP_DMA_LEN1, words >> 8);
377 VDP_setReg(VDP_DMA_SRC0, src >> 1);
378 VDP_setReg(VDP_DMA_SRC1, src >> 9);
379 VDP_setReg(VDP_DMA_SRC2, src >> 17);
380 // write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(addr) | CTL_WRITE_DMA);
383 static void vdp_wait_for_fifo_empty(void)
385 while (!(read16(VDP_CTRL_PORT) & 0x200))
386 /* fifo not empty */;
389 static void vdp_wait_for_dma_idle(void)
391 while (read16(VDP_CTRL_PORT) & 2)
395 static void vdp_wait_for_line_0(void)
397 // in PAL vcounter reports 0 twice in a frame,
398 // so wait for vblank to clear first
399 while (!(read16(VDP_CTRL_PORT) & 8))
401 while (read16(VDP_CTRL_PORT) & 8)
403 while (read8(VDP_HV_COUNTER) != 0)
407 static void t_dma_zero_wrap_early(void)
409 const u32 *src = (const u32 *)0x3c0000;
410 u32 *ram = (u32 *)0xff0000;
412 do_setup_dma(src + 4, 2);
413 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0) | CTL_WRITE_DMA);
414 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0) | CTL_WRITE_DMA);
416 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0));
417 ram[0] = read32(VDP_DATA_PORT);
418 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0xfffc));
419 ram[1] = read32(VDP_DATA_PORT);
422 static void t_dma_zero_fill_early(void)
424 u32 *ram = (u32 *)0xff0000;
426 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
427 write32(VDP_DATA_PORT, 0);
428 write32(VDP_DATA_PORT, 0);
429 write32(VDP_DATA_PORT, 0);
430 write32(VDP_DATA_PORT, 0);
432 VDP_setReg(VDP_AUTOINC, 1);
433 VDP_setReg(VDP_DMA_SRC2, 0x80);
434 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(1) | CTL_WRITE_DMA);
435 write16(VDP_DATA_PORT, 0x1122);
436 ram[2] = read16(VDP_CTRL_PORT);
437 vdp_wait_for_dma_idle();
439 VDP_setReg(VDP_AUTOINC, 2);
440 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0));
441 ram[3] = read32(VDP_DATA_PORT);
444 #define R_SKIP 0x5a5a
446 #define expect(ok_, v0_, v1_) \
447 do { if ((v0_) != (v1_)) { \
448 printf("%s: %08x %08x\n", #v0_, v0_, v1_); \
452 #define expect_sh2(ok_, sh2_, v0_, v1_) \
453 do { if ((v0_) != (v1_)) { \
454 printf("%csh2: %08x %08x\n", sh2_ ? 's' : 'm', v0_, v1_); \
458 #define expect_range(ok_, v0_, vmin_, vmax_) \
459 do { if ((v0_) < (vmin_) || (v0_) > (vmax_)) { \
460 printf("%s: %02x /%02x-%02x\n", #v0_, v0_, vmin_, vmax_); \
464 #define expect_bits(ok_, v0_, val_, mask_) \
465 do { if (((v0_) & (mask_)) != (val_)) { \
466 printf("%s: %04x & %04x != %04x\n", #v0_, v0_, mask_, val_); \
470 static int t_dma_zero_wrap(void)
472 const u32 *src = (const u32 *)0x3c0000;
473 const u32 *ram = (const u32 *)0xff0000;
476 expect(ok, ram[0], src[5 + 0x10000/4]);
477 expect(ok, ram[1], src[4]);
481 static int t_dma_zero_fill(void)
483 const u32 *ram = (const u32 *)0xff0000;
488 expect(ok, ram[3], 0x11111111);
492 static int t_dma_ram_wrap(void)
494 u32 *ram = (u32 *)0xff0000;
498 saved = read32(&ram[0x10000/4 - 1]);
499 ram[0x10000/4 - 1] = 0x01020304;
501 do_setup_dma(&ram[0x10000/4 - 1], 4);
503 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
506 write32(&ram[0x10000/4 - 1], saved);
508 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
509 v0 = read32(VDP_DATA_PORT);
510 v1 = read32(VDP_DATA_PORT);
512 expect(ok, v0, 0x01020304);
513 expect(ok, v1, 0x05060708);
517 // test no src reprogram, only len0
518 static int t_dma_multi(void)
520 const u32 *src = (const u32 *)0x3c0000;
524 do_setup_dma(src, 2);
525 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
526 VDP_setReg(VDP_DMA_LEN0, 2);
527 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x104) | CTL_WRITE_DMA);
529 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
530 v0 = read32(VDP_DATA_PORT);
531 v1 = read32(VDP_DATA_PORT);
533 expect(ok, v0, src[0]);
534 expect(ok, v1, src[1]);
538 static int t_dma_cram_wrap(void)
540 u32 *ram = (u32 *)0xff0000;
544 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0));
545 write32(VDP_DATA_PORT, 0);
550 do_setup_dma(ram, 4);
551 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0x7c | 0xff81) | CTL_WRITE_DMA);
553 write32(VDP_CTRL_PORT, CTL_READ_CRAM(0x7c));
554 v0 = read32(VDP_DATA_PORT) & 0x0eee0eee;
555 write32(VDP_CTRL_PORT, CTL_READ_CRAM(0));
556 v1 = read32(VDP_DATA_PORT) & 0x0eee0eee;
558 setup_default_palette();
560 expect(ok, v0, ram[0]);
561 expect(ok, v1, ram[1]);
565 static int t_dma_vsram_wrap(void)
567 u32 *ram32 = (u32 *)0xff0000;
568 u16 *ram16 = (u16 *)0xff0000;
573 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
574 write32(VDP_DATA_PORT, 0);
576 for (i = 0; i < 0x48/2; i++)
579 do_setup_dma(ram16, 0x48/2);
580 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0x3c | 0xff81) | CTL_WRITE_DMA);
582 write32(VDP_CTRL_PORT, CTL_READ_VSRAM(0x3c));
583 v0 = read32(VDP_DATA_PORT) & 0x03ff03ff;
584 write32(VDP_CTRL_PORT, CTL_READ_VSRAM(0));
585 v1 = read32(VDP_DATA_PORT) & 0x03ff03ff;
587 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
588 write32(VDP_DATA_PORT, 0);
590 expect(ok, v0, ram32[0]);
591 expect(ok, v1, ram32[0x48/4 - 1]);
595 static int t_dma_and_data(void)
597 const u32 *src = (const u32 *)0x3c0000;
601 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
602 write32(VDP_DATA_PORT, 0);
604 do_setup_dma(src, 2);
605 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfc) | CTL_WRITE_DMA);
606 write32(VDP_DATA_PORT, 0x5ec8a248);
608 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0xfc));
609 v0 = read32(VDP_DATA_PORT);
610 v1 = read32(VDP_DATA_PORT);
612 expect(ok, v0, src[0]);
613 expect(ok, v1, 0x5ec8a248);
617 static int t_dma_short_cmd(void)
619 const u32 *src = (const u32 *)0x3c0000;
623 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x3ff4));
624 write32(VDP_DATA_PORT, 0x10111213);
625 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfff0));
626 write32(VDP_DATA_PORT, 0x20212223);
627 write32(VDP_DATA_PORT, 0x30313233);
628 vdp_wait_for_fifo_empty();
630 do_setup_dma(src, 2);
631 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfff0) | CTL_WRITE_DMA);
632 write16(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x3ff4) >> 16);
633 write32(VDP_DATA_PORT, 0x40414243);
635 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x3ff4));
636 v0 = read32(VDP_DATA_PORT);
637 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0xfff0));
638 v1 = read32(VDP_DATA_PORT);
639 v2 = read32(VDP_DATA_PORT);
641 expect(ok, v0, 0x10111213);
642 expect(ok, v1, src[0]);
643 expect(ok, v2, 0x40414243);
647 static int t_dma_fill3_odd(void)
652 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
653 write32(VDP_DATA_PORT, 0);
654 write32(VDP_DATA_PORT, 0);
655 write32(VDP_DATA_PORT, 0);
656 vdp_wait_for_fifo_empty();
658 VDP_setReg(VDP_AUTOINC, 3);
659 VDP_setReg(VDP_DMA_LEN0, 3);
660 VDP_setReg(VDP_DMA_SRC2, 0x80);
661 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x101) | CTL_WRITE_DMA);
662 write16(VDP_DATA_PORT, 0x1122);
663 vdp_wait_for_dma_idle();
665 VDP_setReg(VDP_AUTOINC, 2);
666 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
667 v0 = read32(VDP_DATA_PORT);
668 v1 = read32(VDP_DATA_PORT);
669 v2 = read32(VDP_DATA_PORT);
671 expect(ok, v0, 0x22110000);
672 expect(ok, v1, 0x00111100);
673 expect(ok, v2, 0x00000011);
677 static int t_dma_fill3_even(void)
682 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
683 write32(VDP_DATA_PORT, 0);
684 write32(VDP_DATA_PORT, 0);
685 write32(VDP_DATA_PORT, 0);
686 vdp_wait_for_fifo_empty();
688 VDP_setReg(VDP_AUTOINC, 3);
689 VDP_setReg(VDP_DMA_LEN0, 3);
690 VDP_setReg(VDP_DMA_SRC2, 0x80);
691 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
692 write16(VDP_DATA_PORT, 0x1122);
693 vdp_wait_for_dma_idle();
695 VDP_setReg(VDP_AUTOINC, 2);
696 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
697 v0 = read32(VDP_DATA_PORT);
698 v1 = read32(VDP_DATA_PORT);
699 v2 = read32(VDP_DATA_PORT);
701 expect(ok, v0, 0x11221100);
702 expect(ok, v1, 0x00000011);
703 expect(ok, v2, 0x11000000);
707 static unused int t_dma_fill3_vsram(void)
712 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
713 write32(VDP_DATA_PORT, 0);
714 write32(VDP_DATA_PORT, 0);
715 write32(VDP_DATA_PORT, 0);
717 write16(VDP_DATA_PORT, 0x0111);
718 write16(VDP_DATA_PORT, 0x0222);
719 write16(VDP_DATA_PORT, 0x0333);
720 vdp_wait_for_fifo_empty();
722 VDP_setReg(VDP_AUTOINC, 3);
723 VDP_setReg(VDP_DMA_LEN0, 3);
724 VDP_setReg(VDP_DMA_SRC2, 0x80);
725 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(1) | CTL_WRITE_DMA);
726 write16(VDP_DATA_PORT, 0x0102);
727 vdp_wait_for_dma_idle();
729 VDP_setReg(VDP_AUTOINC, 2);
730 write32(VDP_CTRL_PORT, CTL_READ_VSRAM(0));
731 v0 = read32(VDP_DATA_PORT);
732 v1 = read32(VDP_DATA_PORT);
733 v2 = read32(VDP_DATA_PORT);
735 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
736 write32(VDP_DATA_PORT, 0);
738 expect(ok, v0, 0x01020000);
739 expect(ok, v1, 0x01110111);
740 expect(ok, v2, 0x00000111);
744 static int t_dma_fill_dis(void)
749 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
750 write32(VDP_DATA_PORT, 0);
751 write32(VDP_DATA_PORT, 0);
753 VDP_setReg(VDP_DMA_LEN0, 1);
754 VDP_setReg(VDP_DMA_SRC2, 0x80);
755 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
756 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
757 write16(VDP_DATA_PORT, 0x1122);
758 vdp_wait_for_dma_idle();
760 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
761 write16(VDP_DATA_PORT, 0x3344);
762 vdp_wait_for_dma_idle();
764 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
765 v0 = read32(VDP_DATA_PORT);
766 v1 = read32(VDP_DATA_PORT);
773 static int t_dma_fill_src(void)
775 const u32 *src = (const u32 *)0x3c0000;
779 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
780 write32(VDP_DATA_PORT, 0);
782 // do_setup_dma(src, 2); // hang, can't write src2 twice
783 VDP_setReg(VDP_DMA_LEN0, 2);
784 VDP_setReg(VDP_DMA_SRC0, (u32)src >> 1);
785 VDP_setReg(VDP_DMA_SRC1, (u32)src >> 9);
786 VDP_setReg(VDP_DMA_SRC2, 0x80);
787 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
788 write16(VDP_DATA_PORT, 0x1122);
789 vdp_wait_for_dma_idle();
791 VDP_setReg(VDP_DMA_LEN0, 2);
792 VDP_setReg(VDP_DMA_SRC2, (u32)src >> 17);
793 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x104) | CTL_WRITE_DMA);
795 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
796 v0 = read32(VDP_DATA_PORT);
797 v1 = read32(VDP_DATA_PORT);
799 expect(ok, v0, 0x11220011);
800 expect(ok, v1, src[1]);
804 // (((a & 2) >> 1) ^ 1) | ((a & $400) >> 9) | (a & $3FC) | ((a & $1F800) >> 1)
805 static int t_dma_128k(void)
807 u16 *ram = (u16 *)0xff0000;
815 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
816 write32(VDP_DATA_PORT, 0x01020304);
817 write32(VDP_DATA_PORT, 0x05060708);
818 vdp_wait_for_fifo_empty();
821 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_128K);
822 do_setup_dma(ram, 3);
823 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
824 vdp_wait_for_fifo_empty();
826 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
827 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
828 v0 = read32(VDP_DATA_PORT);
829 v1 = read32(VDP_DATA_PORT);
831 expect(ok, v0, 0x22110304);
832 expect(ok, v1, 0x05330708);
836 static int t_vdp_128k_b16(void)
841 VDP_setReg(VDP_AUTOINC, 0);
842 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x8100));
843 write32(VDP_DATA_PORT, 0x01020304);
844 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x10100));
845 write32(VDP_DATA_PORT, 0x05060708);
846 vdp_wait_for_fifo_empty();
848 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_128K);
849 write16(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) >> 16); // note: upper cmd
850 write32(VDP_DATA_PORT, 0x11223344);
851 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x10102));
852 write32(VDP_DATA_PORT, 0x55667788);
853 vdp_wait_for_fifo_empty();
855 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
856 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x8100));
857 v0 = read16(VDP_DATA_PORT);
858 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x0100));
859 v1 = read16(VDP_DATA_PORT);
861 VDP_setReg(VDP_AUTOINC, 2);
863 expect(ok, v0, 0x8844);
864 expect(ok, v1, 0x0708);
868 static unused int t_vdp_128k_b16_inc(void)
873 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
874 write32(VDP_DATA_PORT, 0x01020304);
875 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x8000));
876 write32(VDP_DATA_PORT, 0x05060708);
877 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfffe));
878 write32(VDP_DATA_PORT, 0x090a0b0c);
879 vdp_wait_for_fifo_empty();
881 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_128K);
882 write16(VDP_CTRL_PORT, CTL_WRITE_VRAM(0) >> 16); // note: upper cmd
883 write16(VDP_DATA_PORT, 0x1122);
884 vdp_wait_for_fifo_empty();
886 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
887 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0));
888 v0 = read32(VDP_DATA_PORT);
889 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x8000));
890 v1 = read32(VDP_DATA_PORT);
891 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
892 write32(VDP_DATA_PORT, 0);
893 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x8000));
894 write32(VDP_DATA_PORT, 0);
896 expect(ok, v0, 0x0b0c0304); // XXX: no 22 anywhere?
897 expect(ok, v1, 0x05060708);
901 static int t_vdp_reg_cmd(void)
906 VDP_setReg(VDP_AUTOINC, 0);
907 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
908 write32(VDP_DATA_PORT, 0x01020304);
909 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
910 write32(VDP_DATA_PORT, 0x05060708);
912 VDP_setReg(VDP_AUTOINC, 2);
913 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x0100));
914 v0 = read16(VDP_DATA_PORT);
916 expect(ok, v0, 0x0304);
920 static int t_vdp_sr_vb(void)
925 while (read8(VDP_HV_COUNTER) != 242)
927 sr[0] = read16(VDP_CTRL_PORT);
928 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
929 sr[1] = read16(VDP_CTRL_PORT);
930 while (read8(VDP_HV_COUNTER) != 4)
932 sr[2] = read16(VDP_CTRL_PORT);
933 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
934 sr[3] = read16(VDP_CTRL_PORT);
936 expect_bits(ok, sr[0], SR_VB, SR_VB);
937 expect_bits(ok, sr[1], SR_VB, SR_VB);
938 expect_bits(ok, sr[2], SR_VB, SR_VB);
939 expect_bits(ok, sr[3], 0, SR_VB);
943 /* z80 tests assume busreq state */
944 static int t_z80mem_long_mirror(void)
946 u8 *zram = (u8 *)0xa00000;
949 write8(&zram[0x1100], 0x11);
950 write8(&zram[0x1101], 0x22);
951 write8(&zram[0x1102], 0x33);
952 write8(&zram[0x1103], 0x44);
954 write32(&zram[0x3100], 0x55667788);
957 expect(ok, zram[0x1100], 0x55);
958 expect(ok, zram[0x1101], 0x22);
959 expect(ok, zram[0x1102], 0x77);
960 expect(ok, zram[0x1103], 0x44);
964 static int t_z80mem_noreq_w(void)
966 u8 *zram = (u8 *)0xa00000;
969 write8(&zram[0x1100], 0x11);
971 write16(0xa11100, 0x000);
972 write8(&zram[0x1100], 0x22);
975 write16(0xa11100, 0x100);
976 while (read16(0xa11100) & 0x100)
979 expect(ok, zram[0x1100], 0x11);
983 #define Z80_C_DISPATCH 113 // see z80_test.s80
985 #define Z80_C_END_VCNT 67
987 #define Z80_CYLES_TEST1(b) (Z80_C_DISPATCH + ((b) - 1) * 21 + 26 + Z80_C_END)
989 static int t_z80mem_vdp_r(void)
991 u8 *zram = (u8 *)0xa00000;
994 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
995 write32(VDP_DATA_PORT, 0x11223344);
996 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
998 zram[0x1000] = 1; // cp
999 write16_z80le(&zram[0x1002], 0x7f00); // src
1000 write16_z80le(&zram[0x1004], 0x1100); // dst
1001 write16_z80le(&zram[0x1006], 2); // len
1002 zram[0x1100] = zram[0x1101] = zram[0x1102] = 0x5a;
1004 write16(0xa11100, 0x000);
1005 burn10(Z80_CYLES_TEST1(2) * 15 / 7 / 10);
1007 write16(0xa11100, 0x100);
1008 while (read16(0xa11100) & 0x100)
1011 expect(ok, zram[0x1000], 0);
1012 expect(ok, zram[0x1100], 0x11);
1013 expect(ok, zram[0x1101], 0x44);
1014 expect(ok, zram[0x1102], 0x5a);
1018 static unused int t_z80mem_vdp_w(void)
1020 u8 *zram = (u8 *)0xa00000;
1024 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1025 write32(VDP_DATA_PORT, 0x11223344);
1026 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1027 vdp_wait_for_fifo_empty();
1029 zram[0x1000] = 1; // cp
1030 write16_z80le(&zram[0x1002], 0x1100); // src
1031 write16_z80le(&zram[0x1004], 0x7f00); // dst
1032 write16_z80le(&zram[0x1006], 2); // len
1033 zram[0x1100] = 0x55;
1034 zram[0x1101] = 0x66;
1036 write16(0xa11100, 0x000);
1037 burn10(Z80_CYLES_TEST1(2) * 15 / 7 / 10);
1039 write16(0xa11100, 0x100);
1040 while (read16(0xa11100) & 0x100)
1043 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
1044 v0 = read32(VDP_DATA_PORT);
1046 expect(ok, zram[0x1000], 0);
1047 expect(ok, v0, 0x55556666);
1051 static int t_tim_loop(void)
1056 vdp_wait_for_line_0();
1058 vcnt = read8(VDP_HV_COUNTER);
1061 //expect_range(ok, vcnt, 0x80, 0x80);
1062 expect(ok, vcnt, 223);
1066 static int t_tim_z80_loop(void)
1068 u8 pal = read8(0xa10001) & 0x40;
1069 u8 *zram = (u8 *)0xa00000;
1070 u16 z80_loops = pal ? 3420*(313*2+1)/15/100 : 3420*(262*2+1)/15/100; // 2fr + 1ln
1071 u16 _68k_loops = pal ? 3420*(313*2+1)/7/10 : 3420*(262*2+1)/7/10;
1074 zram[0x1000] = 3; // idle loop, save vcnt
1075 write16_z80le(&zram[0x1002], 0); // src (unused)
1076 write16_z80le(&zram[0x1004], 0x1100); // vcnt dst
1077 write16_z80le(&zram[0x1006], z80_loops); // x100 cycles
1081 vdp_wait_for_line_0();
1082 write16(0xa11100, 0x000);
1083 burn10(_68k_loops + (Z80_C_DISPATCH + Z80_C_END_VCNT) * 15 / 7 / 10);
1085 write16(0xa11100, 0x100);
1086 while (read16(0xa11100) & 0x100)
1088 expect(ok, zram[0x1000], 0);
1089 expect(ok, zram[0x1100], 1);
1093 #define Z80_CYCLES_TEST2(b) (Z80_C_DISPATCH + (b) * 38 + Z80_C_END_VCNT)
1096 static void z80_read_loop(u8 *zram, u16 src)
1098 const int pairs = 512 + 256;
1100 zram[0x1000] = 2; // read loop, save vcnt
1101 write16_z80le(&zram[0x1002], src); // src
1102 write16_z80le(&zram[0x1004], 0x1100); // vcnt dst
1103 write16_z80le(&zram[0x1006], pairs); // reads/2
1107 vdp_wait_for_line_0();
1108 write16(0xa11100, 0x000);
1109 burn10(Z80_CYCLES_TEST2(pairs) * 15 / 7 * 2 / 10);
1111 write16(0xa11100, 0x100);
1112 while (read16(0xa11100) & 0x100)
1116 static int t_tim_z80_ram(void)
1118 u8 *zram = (u8 *)0xa00000;
1121 z80_read_loop(zram, 0);
1123 expect(ok, zram[0x1000], 0);
1124 expect_range(ok, zram[0x1100], 0x80, 0x80);
1128 static int t_tim_z80_ym(void)
1130 u8 *zram = (u8 *)0xa00000;
1133 z80_read_loop(zram, 0x4000);
1135 expect(ok, zram[0x1000], 0);
1136 expect_range(ok, zram[0x1100], 0x80, 0x80);
1140 static int t_tim_z80_vdp(void)
1142 u8 *zram = (u8 *)0xa00000;
1145 z80_read_loop(zram, 0x7f08);
1147 expect(ok, zram[0x1000], 0);
1148 expect_range(ok, zram[0x1100], 0x91, 0x91);
1152 static int t_tim_z80_bank_rom(void)
1154 u8 *zram = (u8 *)0xa00000;
1157 for (i = 0; i < 17; i++)
1158 write8(0xa06000, 0); // bank 0
1160 z80_read_loop(zram, 0x8000);
1162 expect(ok, zram[0x1000], 0);
1163 expect_range(ok, zram[0x1100], 0x95, 0x96);
1167 /* borderline too slow */
1169 static void test_vcnt_vb(void)
1171 const u32 *srhv = (u32 *)0xc00006; // to read SR and HV counter
1172 u32 *ram = (u32 *)0xff0000;
1173 u16 vcnt, vcnt_expect = 0;
1177 vdp_wait_for_line_0();
1182 vcnt = val & 0xff00;
1183 if (vcnt == vcnt_expect)
1186 if (vcnt == 0 && !(sr & SR_VB)) // not VB
1187 break; // wrapped to start of frame
1189 vcnt_expect += 0x100;
1190 if (vcnt == vcnt_expect && !((sr ^ (old >> 16)) & SR_VB)) {
1194 // should have a vcnt jump here
1206 static int t_tim_vcnt(void)
1208 const u32 *ram32 = (u32 *)0xff0000;
1209 const u8 *ram = (u8 *)0xff0000;
1210 u8 pal = read8(0xa10001) & 0x40;
1211 u8 vc_jmp_b = pal ? 0x02 : 0xea;
1212 u8 vc_jmp_a = pal ? 0xca : 0xe5;
1213 u16 lines = pal ? 313 : 262;
1217 expect(ok, ram[0*4+2], 0); // line 0
1218 expect_bits(ok, ram[0*4+1], 0, SR_VB);
1219 expect(ok, ram[1*4+2], 223); // last no blank
1220 expect_bits(ok, ram[1*4+1], 0, SR_VB);
1221 expect(ok, ram[2*4+2], 224); // 1st blank
1222 expect_bits(ok, ram[2*4+1], SR_VB, SR_VB);
1223 expect(ok, ram[3*4+2], vc_jmp_b); // before jump
1224 expect_bits(ok, ram[3*4+1], SR_VB, SR_VB);
1225 expect(ok, ram[4*4+2], vc_jmp_a); // after jump
1226 expect_bits(ok, ram[4*4+1], SR_VB, SR_VB);
1227 expect(ok, ram[5*4+2], 0xfe); // before vb clear
1228 expect_bits(ok, ram[5*4+1], SR_VB, SR_VB);
1229 expect(ok, ram[6*4+2], 0xff); // after vb clear
1230 expect_bits(ok, ram[6*4+1], 0, SR_VB);
1231 expect(ok, ram[7*4+2], 0); // next line 0
1232 expect_bits(ok, ram[7*4+1], 0, SR_VB);
1233 expect(ok, ram32[8], lines - 1);
1237 static int t_tim_vcnt_loops(void)
1239 const u16 *ram16 = (u16 *)0xfff004;
1240 u8 pal = read8(0xa10001) & 0x40;
1241 u16 i, lines = pal ? 313 : 262;
1245 expect(ok, ram16[-1*2+0], 0xff);
1246 expect_range(ok, ram16[-1*2+1], 21, 22);
1247 for (i = 0; i < lines; i++)
1248 expect_range(ok, ram16[i*2+1], 19, 21);
1249 expect(ok, ram16[lines*2+0], 0);
1250 expect_range(ok, ram16[lines*2+1], 19, 21);
1254 static int t_tim_hblank_h40(void)
1256 const u8 *r = (u8 *)0xff0000;
1262 expect_bits(ok, r[2], SR_HB, SR_HB);
1263 expect_bits(ok, r[5], SR_HB, SR_HB);
1265 expect_bits(ok, r[7], SR_HB, SR_HB);
1267 expect_bits(ok, r[12], 0, SR_HB);
1271 static int t_tim_hblank_h32(void)
1273 const u8 *r = (u8 *)0xff0000;
1276 VDP_setReg(VDP_MODE4, 0x00);
1278 VDP_setReg(VDP_MODE4, 0x81);
1280 expect_bits(ok, r[0], 0, SR_HB);
1282 expect_bits(ok, r[4], SR_HB, SR_HB);
1283 expect_bits(ok, r[5], SR_HB, SR_HB);
1285 expect_bits(ok, r[8], SR_HB, SR_HB);
1287 expect_bits(ok, r[12], 0, SR_HB);
1291 static int t_tim_vdp_as_vram_w(void)
1296 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1297 vdp_wait_for_line_0();
1298 write16_x16(VDP_DATA_PORT, 112*18 / 16, 0);
1299 vcnt = read8(VDP_HV_COUNTER);
1302 expect(ok, vcnt, 112*2-1);
1306 static int t_tim_vdp_as_cram_w(void)
1311 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0));
1312 vdp_wait_for_line_0();
1313 write16_x16(VDP_DATA_PORT, 112*18 / 16, 0);
1314 vcnt = read8(VDP_HV_COUNTER);
1317 setup_default_palette();
1319 expect(ok, vcnt, 112);
1323 static const u8 hcnt2tm[] =
1325 0x0a, 0x1d, 0x31, 0x44, 0x58, 0x6b, 0x7f, 0x92,
1326 0xa6, 0xb9, 0xcc, 0x00, 0x00, 0x00, 0xe2, 0xf6
1329 static int t_tim_ym_timer_z80(int is_b)
1331 u8 pal = read8(0xa10001) & 0x40;
1332 u8 *zram = (u8 *)0xa00000;
1334 u16 _68k_loops = 3420*(302+5+1)/7/10; // ~ (72*1024*2)/(3420./7)
1335 u16 start, end, diff;
1338 zram[0x1000] = 4 + is_b; // ym2612 timer a/b test
1339 zram[0x1100] = zram[0x1101] = zram[0x1102] = zram[0x1103] = 0;
1342 vdp_wait_for_line_0();
1343 write16(0xa11100, 0x000);
1345 burn10(_68k_loops + (Z80_C_DISPATCH + Z80_C_END_VCNT) * 15 / 7 / 10);
1347 write16(0xa11100, 0x100);
1348 while (read16(0xa11100) & 0x100)
1351 expect(ok, zram[0x1000], 0);
1353 //start = ((u16)zram[0x1102] << 8) | hcnt2tm[zram[0x1103] >> 4];
1354 //end = ((u16)zram[0x1100] << 8) | hcnt2tm[zram[0x1101] >> 4];
1355 start = zram[0x1102];
1359 expect_range(ok, diff, 0xf4, 0xf6);
1361 expect_range(ok, diff, 0x27, 0x29);
1362 write8(&z80[0x4001], 0); // stop, but should keep the flag
1364 burn10(32*6/10); // busy bit, 32 FM ticks (M/7/6)
1366 expect(ok, z80[0x4000], 2);
1367 write8(&z80[0x4001], 0x20); // reset flag (reg 0x27, set up by z80)
1370 expect(ok, z80[0x4000], 1);
1371 write8(&z80[0x4001], 0x10);
1375 expect(ok, z80[0x4000], 0);
1379 static int t_tim_ym_timera_z80(void)
1381 return t_tim_ym_timer_z80(0);
1384 static int t_tim_ym_timerb_z80(void)
1386 return t_tim_ym_timer_z80(1);
1389 static int t_tim_ym_timerb_stop(void)
1400 } *t = (void *)0xfff000;
1401 u8 *z80 = (u8 *)0xa00000;
1404 write16(0xa11100, 0x100);
1405 while (read16(0xa11100) & 0x100)
1407 test_ym_stopped_tick();
1409 //start = ((u16)t->vcnt_start << 8) | hcnt2tm[t->hcnt_start >> 4];
1410 //end = ((u16)t->vcnt_end << 8) | hcnt2tm[t->hcnt_end >> 4];
1411 //diff = end - start;
1412 diff = t->vcnt_end - t->vcnt_start;
1413 //expect_range(ok, diff, 0x492, 0x5c2); // why so much variation?
1414 expect_range(ok, diff, 4, 5);
1415 expect(ok, t->stat0, 0);
1416 expect(ok, t->stat1, 2);
1417 expect(ok, z80[0x4000], 2);
1418 write8(&z80[0x4001], 0x30);
1422 static int t_tim_ym_timer_ab_sync(void)
1424 u16 v1, v2, v3, v4, v5, ln0, ln1, ln2;
1427 vdp_wait_for_line_0();
1428 v1 = test_ym_ab_sync();
1431 burn10(3420*15/7/10); // ~15 scanlines
1432 write8(0xa04001, 0x3f); // clear, no reload
1433 burn10(12); // wait for busy to clear
1434 v2 = read8(0xa04000);
1435 v3 = test_ym_ab_sync2();
1438 burn10(3420*15/7/10); // ~15 scanlines
1439 v4 = test_ym_ab_sync2();
1442 burn10(3420*30/7/10); // ~35 scanlines
1443 v5 = read8(0xa04000);
1450 expect_range(ok, ln1-ln0, 18, 19);
1451 expect_range(ok, ln2-ln1, 32, 34); // almost always 33
1464 // broken on fresh boot due to uknown reasons
1465 static int t_irq_hint(void)
1467 struct irq_test *it = (void *)0xfff000;
1468 struct irq_test *itv = it + 1;
1471 memset_(it, 0, sizeof(*it) * 2);
1472 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1473 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1475 // without this, tests fail after cold boot
1476 while (!(read16(VDP_CTRL_PORT) & 8))
1479 // for more fun, disable the display
1480 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
1483 while (read8(VDP_HV_COUNTER) != 100)
1485 while (read8(VDP_HV_COUNTER) != 229)
1487 // take the pending irq
1488 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1490 burn10(488 * 2 / 10);
1492 expect(ok, it->first.v, 229); // pending irq trigger
1493 expect(ok, it->cnt, 1);
1494 expect(ok, itv->cnt, 0);
1497 it->cnt = it->first.hv = it->last.hv = 0;
1499 while (read8(VDP_HV_COUNTER) != 4)
1501 while (read8(VDP_HV_COUNTER) != 228)
1504 expect(ok, it->cnt, 225);
1505 expect(ok, it->first.v, 0);
1506 expect(ok, it->last.v, 224);
1508 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1510 // detect reload line
1511 it->cnt = it->first.hv = it->last.hv = 0;
1514 while (read16(VDP_CTRL_PORT) & 8)
1516 VDP_setReg(10, 255);
1517 while (read8(VDP_HV_COUNTER) != 228)
1520 expect(ok, it->cnt, 1);
1521 expect(ok, it->first.v, 17);
1522 expect(ok, it->last.v, 17);
1524 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1529 static int t_irq_both_cpu_unmask(void)
1531 struct irq_test *ith = (void *)0xfff000;
1532 struct irq_test *itv = ith + 1;
1536 memset_(ith, 0, sizeof(*ith) * 2);
1537 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1538 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1540 while (read8(VDP_HV_COUNTER) != 100)
1542 while (read8(VDP_HV_COUNTER) != 226)
1545 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1546 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_IE0 | VDP_MODE2_DISP);
1547 /* go to active display line 100 */
1548 while (read8(VDP_HV_COUNTER) != 100)
1550 s0 = read16(VDP_CTRL_PORT);
1551 s1 = move_sr_and_read(0x2000, VDP_CTRL_PORT);
1553 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1554 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1556 expect(ok, itv->cnt, 1); // vint count
1557 expect(ok, itv->first.v, 100); // vint line
1558 expect(ok, ith->cnt, 1); // hint count
1559 expect(ok, ith->first.v, 100); // hint line
1560 expect_bits(ok, s0, SR_F, SR_F);
1561 expect_bits(ok, s1, 0, SR_F);
1565 static int t_irq_ack_v_h(void)
1567 struct irq_test *ith = (void *)0xfff000;
1568 struct irq_test *itv = ith + 1;
1572 memset_(ith, 0, sizeof(*ith) * 2);
1573 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1574 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1576 /* ensure hcnt reload */
1577 while (!(read16(VDP_CTRL_PORT) & 8))
1579 while (read16(VDP_CTRL_PORT) & 8)
1581 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1582 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_IE0);
1583 while (read8(VDP_HV_COUNTER) != 100)
1585 while (read8(VDP_HV_COUNTER) != 226)
1587 s0 = read16(VDP_CTRL_PORT);
1588 s1 = move_sr_and_read(0x2500, VDP_CTRL_PORT);
1590 s2 = move_sr_and_read(0x2000, VDP_CTRL_PORT);
1593 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1594 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1596 expect(ok, itv->cnt, 1); // vint count
1597 expect(ok, itv->first.v, 226); // vint line
1598 expect(ok, ith->cnt, 1); // hint count
1599 expect(ok, ith->first.v, 228); // hint line
1600 expect_bits(ok, s0, SR_F, SR_F);
1601 expect_bits(ok, s1, 0, SR_F);
1602 expect_bits(ok, s2, 0, SR_F);
1606 static int t_irq_ack_v_h_2(void)
1608 struct irq_test *ith = (void *)0xfff000;
1609 struct irq_test *itv = ith + 1;
1613 memset_(ith, 0, sizeof(*ith) * 2);
1614 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1615 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1617 while (read8(VDP_HV_COUNTER) != 100)
1619 while (read8(VDP_HV_COUNTER) != 226)
1621 s0 = read16(VDP_CTRL_PORT);
1623 s1 = read16(VDP_CTRL_PORT);
1624 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1625 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1627 expect(ok, itv->cnt, 2); // vint count
1628 expect(ok, itv->first.v, 226); // vint line
1629 expect(ok, ith->cnt, 1); // hint count
1630 expect(ok, ith->first.v, 227); // hint line
1631 expect_bits(ok, s0, SR_F, SR_F);
1632 expect_bits(ok, s1, 0, SR_F);
1636 static int t_irq_ack_h_v(void)
1638 u16 *ram = (u16 *)0xfff000;
1639 u8 *ram8 = (u8 *)0xfff000;
1643 ram[0] = ram[1] = ram[2] =
1644 ram[4] = ram[5] = ram[6] = 0;
1645 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1646 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1648 while (read8(VDP_HV_COUNTER) != 100)
1650 while (read8(VDP_HV_COUNTER) != 226)
1652 s0 = read16(VDP_CTRL_PORT);
1653 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1656 s1 = read16(VDP_CTRL_PORT);
1657 write_and_read1(VDP_CTRL_PORT, 0x8000 | (VDP_MODE2 << 8)
1658 | VDP_MODE2_MD | VDP_MODE2_IE0, s);
1660 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1661 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1663 expect(ok, ram[0], 1); // hint count
1664 expect(ok, ram8[2], 226); // hint line
1665 expect(ok, ram[4], 1); // vint count
1666 expect(ok, ram8[10], 228); // vint line
1667 expect_bits(ok, s0, SR_F, SR_F);
1668 expect_bits(ok, s1, SR_F, SR_F);
1669 expect_bits(ok, s[0], SR_F, SR_F);
1670 expect_bits(ok, s[1], SR_F, SR_F);
1671 expect_bits(ok, s[2], 0, SR_F);
1672 expect_bits(ok, s[3], 0, SR_F);
1676 static int t_irq_ack_h_v_2(void)
1678 u16 *ram = (u16 *)0xfff000;
1679 u8 *ram8 = (u8 *)0xfff000;
1683 ram[0] = ram[1] = ram[2] =
1684 ram[4] = ram[5] = ram[6] = 0;
1685 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1686 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1688 while (read8(VDP_HV_COUNTER) != 100)
1690 while (read8(VDP_HV_COUNTER) != 226)
1692 s0 = read16(VDP_CTRL_PORT);
1694 s1 = read16(VDP_CTRL_PORT);
1695 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1696 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1698 expect(ok, ram[0], 2); // hint count
1699 expect(ok, ram8[2], 226); // hint first line
1700 expect(ok, ram8[4], 226); // hint last line
1701 expect(ok, ram[4], 0); // vint count
1702 expect(ok, ram8[10], 0); // vint line
1703 expect_bits(ok, s0, SR_F, SR_F);
1704 expect_bits(ok, s1, 0, SR_F);
1708 static void t_irq_f_flag(void)
1710 memcpy_((void *)0xff0140, test_f_vint, test_f_vint_end - test_f_vint);
1711 memset_((void *)0xff0000, 0, 10);
1712 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_IE0 | VDP_MODE2_DISP);
1714 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1717 static int t_irq_f_flag_h40(void)
1719 u8 f, *r = (u8 *)0xff0000;
1724 expect_bits(ok, r[0], 0, SR_F);
1725 expect_bits(ok, r[1], 0, SR_F);
1726 expect_bits(ok, r[2], 0, SR_F);
1727 // hits 1-3 times in range 3-9, usually ~5
1728 f = r[3] | r[4] | r[5] | r[6] | r[7];
1730 expect_bits(ok, r[10], 0, SR_F);
1731 expect_bits(ok, r[11], 0, SR_F);
1732 expect_bits(ok, f, SR_F, SR_F);
1736 static int t_irq_f_flag_h32(void)
1738 u8 f, *r = (u8 *)0xff0000;
1741 VDP_setReg(VDP_MODE4, 0x00);
1743 VDP_setReg(VDP_MODE4, 0x81);
1745 expect_bits(ok, r[0], 0, SR_F);
1746 expect_bits(ok, r[1], 0, SR_F);
1747 // hits 1-3 times in range 2-7, usually 3
1748 f = r[2] | r[3] | r[4] | r[5] | r[6] | r[7];
1750 expect_bits(ok, r[8], 0, SR_F);
1751 expect_bits(ok, r[9], 0, SR_F);
1752 expect_bits(ok, r[10], 0, SR_F);
1753 expect_bits(ok, r[11], 0, SR_F);
1754 expect_bits(ok, f, SR_F, SR_F);
1760 #define IRQ_CNT_FB_BASE 0x1ff00
1762 static void x32_cmd(enum x32x_cmd cmd, u32 a0, u32 a1, u16 is_slave)
1764 u16 v, *r = (u16 *)0xa15120;
1765 u16 cmd_s = cmd | (is_slave << 15);
1768 write32(&r[4/2], a0);
1769 write32(&r[8/2], a1);
1773 for (i = 0; i < 10000 && (v = read16(r)) == cmd_s; i++)
1776 printf("cmd clr: %x\n", v);
1778 printf("c, e: %02x %02x\n", r[0x0c/2], r[0x0e/2]);
1783 printf("cmd err: %x\n", v);
1788 static int t_32x_reset_btn(void)
1790 void (*do_32x_disable)(void) = (void *)0xff0040;
1791 u32 *fbl_icnt = (u32 *)(0x840000 + IRQ_CNT_FB_BASE);
1792 u16 *m_icnt = (u16 *)fbl_icnt;
1793 u16 *s_icnt = m_icnt + 8;
1794 u32 *r32 = (u32 *)0xa15100;
1795 u16 *r16 = (u16 *)r32, i, s;
1800 if (!(read16(r16) & 1))
1803 expect(ok, r16[0x00/2], 0x8083);
1805 write8(r8, 0x00); // FM=0
1807 expect(ok, r16[0x00/2], 0x83);
1808 expect(ok, r16[0x02/2], 0);
1809 expect(ok, r16[0x04/2], 3);
1810 expect(ok, r16[0x06/2], 1); // RV (set in sega_gcc.s reset handler)
1811 expect(ok, r32[0x08/4], 0x5a5a08);
1812 expect(ok, r32[0x0c/4], 0x5a5a0c);
1813 expect(ok, r16[0x10/2], 0x5a10);
1814 expect(ok, r32[0x14/4], 0);
1815 expect(ok, r32[0x18/4], 0);
1816 expect(ok, r32[0x1c/4], 0);
1817 expect(ok, r32[0x20/4], 0x00005a20);
1818 expect(ok, r32[0x24/4], 0x5a5a5a24);
1819 expect(ok, r32[0x28/4], 0x5a5a5a28);
1820 expect(ok, r32[0x2c/4], 0x5a5a5a2c);
1821 if (!(r16[0x00/2] & 0x8000)) {
1822 expect(ok, r8 [0x81], 0);
1823 expect(ok, r16[0x82/2], 0);
1824 expect(ok, r16[0x84/2], 0);
1825 expect(ok, r16[0x86/2], 0);
1826 //expect(ok, r16[0x88/2], 0); // triggers fill?
1827 expect(ok, r8 [0x8b] & ~2, 0); // FEN toggles periodically?
1828 expect(ok, r16[0x8c/2], 0);
1829 expect(ok, r16[0x8e/2], 0);
1831 r32[0x20/4] = r32[0x24/4] = r32[0x28/4] = r32[0x2c/4] = 0;
1832 for (s = 0; s < 2; s++)
1834 x32_cmd(CMD_READ32, 0x20004000, 0, s); // not cleared by hw
1835 expect_sh2(ok, s, r32[0x24/4], 0x02020000); // ADEN | cmd
1836 // t_32x_sh_defaults will test the other bits
1838 // setup for t_32x_sh_defaults
1839 x32_cmd(CMD_WRITE8, 0x20004001, 0, 0);
1840 x32_cmd(CMD_WRITE8, 0x20004001, 0, 1);
1842 for (i = 0; i < 7; i++) {
1843 expect(ok, m_icnt[i], 0x100);
1844 expect(ok, s_icnt[i], 0x100);
1846 expect(ok, m_icnt[7], 0x101); // VRES happened
1847 expect(ok, s_icnt[7], 0x101);
1849 memcpy_(do_32x_disable, x32x_disable,
1850 x32x_disable_end - x32x_disable);
1853 expect(ok, r16[0x00/2], 0x82);
1854 expect(ok, r16[0x02/2], 0);
1855 expect(ok, r16[0x04/2], 3);
1856 expect(ok, r16[0x06/2], 1); // RV
1857 expect(ok, r32[0x08/4], 0x5a5a08);
1858 expect(ok, r32[0x0c/4], 0x5a5a0c);
1859 expect(ok, r16[0x10/2], 0x5a10);
1860 expect(ok, rl[0x04/4], 0x000800);
1862 // setup for t_32x_init, t_32x_sh_defaults
1864 r16[0x06/2] = 0; // can just set without ADEN
1865 r16[0x10/2] = 0x1234; // warm reset indicator
1867 expect(ok, r16[0x06/2], 0); // RV
1871 static int t_32x_init(void)
1873 void (*do_32x_enable)(void) = (void *)0xff0040;
1874 u32 M_OK = MKLONG('M','_','O','K');
1875 u32 S_OK = MKLONG('S','_','O','K');
1876 u32 *r32 = (u32 *)0xa15100;
1877 u16 *r16 = (u16 *)r32;
1881 //v1070 = read32(0x1070);
1883 /* what does REN mean exactly?
1884 * Seems to be sometimes clear after reset */
1885 for (i = 0; i < 1000000; i++)
1886 if (read16(r16) & 0x80)
1888 expect(ok, r16[0x00/2], 0x82);
1889 expect(ok, r16[0x02/2], 0);
1890 expect(ok, r16[0x04/2], 0);
1891 expect(ok, r16[0x06/2], 0);
1892 expect(ok, r8 [0x08], 0);
1893 //expect(ok, r32[0x08/4], 0); // garbage 24bit
1894 expect(ok, r8 [0x0c], 0);
1895 //expect(ok, r32[0x0c/4], 0); // garbage 24bit
1896 if (r16[0x10/2] != 0x1234) // warm reset
1897 expect(ok, r16[0x10/2], 0xffff);
1898 expect(ok, r16[0x12/2], 0);
1899 expect(ok, r32[0x14/4], 0);
1900 expect(ok, r32[0x18/4], 0);
1901 expect(ok, r32[0x1c/4], 0);
1902 //expect(ok, r8 [0x81], 0); // VDP; hangs without ADEN
1903 r32[0x20/4] = 0; // master resp
1904 r32[0x24/4] = 0; // slave resp
1908 // these have garbage or old values (survive MD's power cycle)
1912 // could just set RV, but BIOS reads ROM, so can't
1913 memcpy_(do_32x_enable, x32x_enable,
1914 x32x_enable_end - x32x_enable);
1917 expect(ok, r16[0x00/2], 0x83);
1918 expect(ok, r16[0x02/2], 0);
1919 expect(ok, r16[0x04/2], 0);
1920 expect(ok, r16[0x06/2], 1); // RV
1921 expect(ok, r32[0x14/4], 0);
1922 expect(ok, r32[0x18/4], 0);
1923 expect(ok, r32[0x1c/4], 0);
1924 expect(ok, r32[0x20/4], M_OK);
1925 while (!read16(&r16[0x24/2]))
1927 expect(ok, r32[0x24/4], S_OK);
1928 write32(&r32[0x20/4], 0);
1929 if (!(r16[0x00/2] & 0x8000)) {
1930 expect(ok, r8 [0x81], 0);
1931 expect(ok, r16[0x82/2], 0);
1932 expect(ok, r16[0x84/2], 0);
1933 expect(ok, r16[0x86/2], 0);
1934 //expect(ok, r16[0x88/2], 0); // triggers fill?
1935 expect(ok, r8 [0x8b] & ~2, 0);
1936 expect(ok, r16[0x8c/2], 0);
1937 expect(ok, r16[0x8e/2], 0);
1942 static int t_32x_echo(void)
1944 u16 *r = (u16 *)0xa15120;
1947 x32_cmd(CMD_ECHO, 0x12340000, 0, 0);
1948 expect_sh2(ok, 0, r[0x06/2], 0x1234);
1949 x32_cmd(CMD_ECHO, 0x23450000, 0, 1);
1950 expect_sh2(ok, 1, r[0x06/2], 0xa345);
1954 static int t_32x_sh_defaults(void)
1956 u32 *r32 = (u32 *)0xa15120;
1959 for (s = 0; s < 2; s++)
1961 x32_cmd(CMD_READ32, 0x20004000, 0, s);
1962 expect_sh2(ok, s, r32[0x04/4], 0x02000000); // ADEN
1963 x32_cmd(CMD_READ32, 0x20004004, 0, s);
1964 expect_sh2(ok, s, r32[0x04/4], 0x00004001); // Empty Rv
1965 x32_cmd(CMD_READ32, 0x20004008, 0, s);
1966 expect_sh2(ok, s, r32[0x04/4], 0);
1967 x32_cmd(CMD_READ32, 0x2000400c, 0, s);
1968 expect_sh2(ok, s, r32[0x04/4], 0);
1969 x32_cmd(CMD_GETGBR, 0, 0, s);
1970 expect_sh2(ok, s, r32[0x04/4], 0x20004000);
1975 static int t_32x_md_bios(void)
1977 void (*do_call_c0)(int a, int d) = (void *)0xff0040;
1978 u8 *rmb = (u8 *)0xff0000;
1982 memcpy_(do_call_c0, test_32x_b_c0,
1983 test_32x_b_c0_end - test_32x_b_c0);
1985 do_call_c0(0xff0000, 0x5a);
1987 expect(ok, rmb[0], 0x5a);
1988 expect(ok, rl[0x04/4], 0x880200);
1989 expect(ok, rl[0x10/4], 0x880212);
1990 expect(ok, rl[0x94/4], 0x8802d8);
1994 static int t_32x_md_rom(void)
1999 expect(ok, rl[0x004/4], 0x880200);
2000 expect(ok, rl[0x100/4], 0x53454741);
2001 expect(ok, rl[0x70/4], 0);
2002 write32(&rl[0x70/4], 0xa5123456);
2003 write32(&rl[0x78/4], ~0);
2005 expect(ok, rl[0x78/4], 0x8802ae);
2006 expect(ok, rl[0x70/4], 0xa5123456);
2007 //expect(ok, rl[0x1070/4], v1070);
2008 write32(&rl[0x70/4], 0);
2009 // with RV 0x880000/0x900000 hangs, can't test
2013 static int t_32x_md_fb(void)
2015 u8 *fbb = (u8 *)0x840000;
2016 u16 *fbw = (u16 *)fbb;
2017 u32 *fbl = (u32 *)fbb;
2018 u8 *fob = (u8 *)0x860000;
2019 u16 *fow = (u16 *)fob;
2020 u32 *fol = (u32 *)fob;
2023 fbl[0] = 0x12345678;
2024 fol[1] = 0x89abcdef;
2026 expect(ok, fbw[1], 0x5678);
2027 expect(ok, fow[2], 0x89ab);
2036 expect(ok, fol[0], 0x12340000);
2037 expect(ok, fbl[1], 0x89ab0201);
2041 static int t_32x_sh_fb(void)
2043 u32 *fbl = (u32 *)0x840000;
2044 u8 *r8 = (u8 *)0xa15100;
2047 if (read8(r8) & 0x80)
2048 write8(r8, 0x00); // FM=0
2049 fbl[0] = 0x12345678;
2050 fbl[1] = 0x89abcdef;
2052 write8(r8, 0x80); // FM=1
2053 x32_cmd(CMD_WRITE8, 0x24000000, 0, 0); // should ignore
2054 x32_cmd(CMD_WRITE8, 0x24020001, 0, 0); // ignore
2055 x32_cmd(CMD_WRITE16, 0x24000002, 0, 0); // ok
2056 x32_cmd(CMD_WRITE16, 0x24020000, 0, 0); // ignore
2057 x32_cmd(CMD_WRITE32, 0x24020004, 0x5a0000a5, 1);
2058 write8(r8, 0x00); // FM=0
2060 expect(ok, fbl[0], 0x12340000);
2061 expect(ok, fbl[1], 0x5aabcda5);
2065 static int t_32x_irq(void)
2067 u32 *fbl_icnt = (u32 *)(0x840000 + IRQ_CNT_FB_BASE);
2068 u16 *m_icnt = (u16 *)fbl_icnt;
2069 u16 *s_icnt = m_icnt + 8;
2070 u32 *r = (u32 *)0xa15100;
2071 u16 *r16 = (u16 *)r;
2074 write8(r, 0x00); // FM=0
2076 for (i = 0; i < 8; i++)
2077 write32(&fbl_icnt[i], 0);
2079 write16(&r16[0x02/2], 0xfffd); // INTM+unused_bits
2081 expect(ok, r16[0x02/2], 1);
2082 x32_cmd(CMD_WRITE8, 0x20004001, 2, 0); // unmask cmd
2083 x32_cmd(CMD_WRITE8, 0x20004001, 2, 1); // unmask cmd slave
2085 write8(r, 0x00); // FM=0 (hangs without)
2087 expect(ok, r16[0x02/2], 0);
2088 expect(ok, m_icnt[4], 1);
2089 expect(ok, s_icnt[4], 0);
2090 write16(&r16[0x02/2], 0xaaaa); // INTS+unused_bits
2092 expect(ok, r16[0x02/2], 2);
2095 expect(ok, r16[0x02/2], 0);
2096 write8(r, 0x00); // FM=0
2098 expect(ok, m_icnt[4], 1);
2099 expect(ok, s_icnt[4], 1);
2100 for (i = 0; i < 8; i++) {
2103 expect(ok, m_icnt[i], 0);
2104 expect(ok, s_icnt[i], 0);
2109 static int t_32x_reg_w(void)
2111 u32 *r32 = (u32 *)0xa15100;
2112 u16 *r16 = (u16 *)r32, old;
2119 expect(ok, r32[0x08/4], 0xfffffe);
2120 expect(ok, r32[0x0c/4], 0xffffff);
2121 expect(ok, r16[0x10/2], 0xfffc);
2123 r32[0x08/4] = r32[0x0c/4] = 0;
2126 x32_cmd(CMD_WRITE16, 0x20004006, ~old, 0);
2127 expect(ok, r16[0x06/2], old);
2131 // prepare for reset btn press tests
2132 static int t_32x_reset_prep(void)
2134 u32 *fbl_icnt = (u32 *)(0x840000 + IRQ_CNT_FB_BASE);
2135 u32 *r32 = (u32 *)0xa15100;
2136 u16 *r16 = (u16 *)r32;
2140 expect(ok, r16[0x00/2], 0x83);
2141 write8(r8, 0x00); // FM=0
2143 expect(ok, r8[0x8b] & ~2, 0);
2144 for (i = 0; i < 8; i++)
2145 write32(&fbl_icnt[i], 0x01000100);
2146 x32_cmd(CMD_WRITE8, 0x20004001, 0x02, 0); // unmask cmd
2147 x32_cmd(CMD_WRITE8, 0x20004001, 0x02, 1); // unmask slave
2149 write8(r8, 0x00); // FM=0
2151 for (i = 0; i < 8; i++)
2152 expect(ok, fbl_icnt[i], 0x01000100);
2154 r16[0x04/2] = 0xffff;
2155 r32[0x08/4] = 0x5a5a5a08;
2156 r32[0x0c/4] = 0x5a5a5a0c;
2157 r16[0x10/2] = 0x5a10;
2158 r32[0x20/4] = 0x00005a20; // no x32_cmd
2159 r32[0x24/4] = 0x5a5a5a24;
2160 r32[0x28/4] = 0x5a5a5a28;
2161 r32[0x2c/4] = 0x5a5a5a2c;
2170 static const struct {
2175 // this must be first to disable the 32x and restore the 68k vector table
2176 { T_32, t_32x_reset_btn, "32x resetbtn" },
2178 { T_MD, t_dma_zero_wrap, "dma zero len + wrap" },
2179 { T_MD, t_dma_zero_fill, "dma zero len + fill" },
2180 { T_MD, t_dma_ram_wrap, "dma ram wrap" },
2181 { T_MD, t_dma_multi, "dma multi" },
2182 { T_MD, t_dma_cram_wrap, "dma cram wrap" },
2183 { T_MD, t_dma_vsram_wrap, "dma vsram wrap" },
2184 { T_MD, t_dma_and_data, "dma and data" },
2185 { T_MD, t_dma_short_cmd, "dma short cmd" },
2186 { T_MD, t_dma_fill3_odd, "dma fill3 odd" },
2187 { T_MD, t_dma_fill3_even, "dma fill3 even" },
2188 { T_MD, t_dma_fill3_vsram, "dma fill3 vsram" },
2189 { T_MD, t_dma_fill_dis, "dma fill disabled" },
2190 { T_MD, t_dma_fill_src, "dma fill src incr" },
2191 { T_MD, t_dma_128k, "dma 128k mode" },
2192 { T_MD, t_vdp_128k_b16, "vdp 128k addr bit16" },
2193 // { t_vdp_128k_b16_inc, "vdp 128k bit16 inc" }, // mystery
2194 { T_MD, t_vdp_reg_cmd, "vdp reg w cmd reset" },
2195 { T_MD, t_vdp_sr_vb, "vdp status reg vb" },
2196 { T_MD, t_z80mem_long_mirror, "z80 ram long mirror" },
2197 { T_MD, t_z80mem_noreq_w, "z80 ram noreq write" },
2198 { T_MD, t_z80mem_vdp_r, "z80 vdp read" },
2199 // { t_z80mem_vdp_w, "z80 vdp write" }, // hang
2200 { T_MD, t_tim_loop, "time loop" },
2201 { T_MD, t_tim_z80_loop, "time z80 loop" },
2202 { T_MD, t_tim_z80_ram, "time z80 ram" },
2203 { T_MD, t_tim_z80_ym, "time z80 ym2612" },
2204 { T_MD, t_tim_z80_vdp, "time z80 vdp" },
2205 { T_MD, t_tim_z80_bank_rom, "time z80 bank rom" },
2206 { T_MD, t_tim_vcnt, "time V counter" },
2207 { T_MD, t_tim_vcnt_loops, "time vcnt loops" },
2208 { T_MD, t_tim_hblank_h40, "time hblank h40" },
2209 { T_MD, t_tim_hblank_h32, "time hblank h32" },
2210 { T_MD, t_tim_vdp_as_vram_w, "time vdp vram w" },
2211 { T_MD, t_tim_vdp_as_cram_w, "time vdp cram w" },
2212 { T_MD, t_tim_ym_timera_z80, "time timer a z80" },
2213 { T_MD, t_tim_ym_timerb_z80, "time timer b z80" },
2214 { T_MD, t_tim_ym_timerb_stop, "timer b stop" },
2215 { T_MD, t_tim_ym_timer_ab_sync,"timer ab sync" },
2216 { T_MD, t_irq_hint, "irq4 / line" },
2217 { T_MD, t_irq_both_cpu_unmask, "irq both umask" },
2218 { T_MD, t_irq_ack_v_h, "irq ack v-h" },
2219 { T_MD, t_irq_ack_v_h_2, "irq ack v-h 2" },
2220 { T_MD, t_irq_ack_h_v, "irq ack h-v" },
2221 { T_MD, t_irq_ack_h_v_2, "irq ack h-v 2" },
2222 { T_MD, t_irq_f_flag_h40, "irq f flag h40" },
2223 { T_MD, t_irq_f_flag_h32, "irq f flag h32" },
2225 // the first one enables 32X, so must be kept
2226 // all tests assume RV=1 FM=0
2227 { T_32, t_32x_init, "32x init" },
2228 { T_32, t_32x_echo, "32x echo" },
2229 { T_32, t_32x_sh_defaults, "32x sh def" },
2230 { T_32, t_32x_md_bios, "32x md bios" },
2231 { T_32, t_32x_md_rom, "32x md rom" },
2232 { T_32, t_32x_md_fb, "32x md fb" },
2233 { T_32, t_32x_sh_fb, "32x sh fb" },
2234 { T_32, t_32x_irq, "32x irq" },
2235 { T_32, t_32x_reg_w, "32x reg w" },
2236 { T_32, t_32x_reset_prep, "32x rstprep" }, // must be last 32x
2239 static void setup_z80(void)
2241 u8 *zram = (u8 *)0xa00000;
2245 write16(0xa11100, 0x100);
2246 write16(0xa11200, 0x100);
2248 while (read16(0xa11100) & 0x100)
2251 // load the default test program, clear it's data
2252 len = z80_test_end - z80_test;
2253 for (i = 0; i < len; i++)
2254 write8(&zram[i], z80_test[i]);
2255 for (i = 0x1000; i < 0x1007; i++)
2256 write8(&zram[i], 0);
2259 write16(0xa11200, 0x000);
2260 write16(0xa11100, 0x000);
2262 write16(0xa11200, 0x100);
2264 burn10(50 * 15 / 7 / 10); // see z80_test.s80
2266 // take back the bus
2267 write16(0xa11100, 0x100);
2268 while (read16(0xa11100) & 0x100)
2272 static void wait_next_vsync(void)
2274 while (read16(VDP_CTRL_PORT) & SR_VB)
2276 while (!(read16(VDP_CTRL_PORT) & SR_VB))
2280 static unused int hexinc(char *c)
2294 void (*px32x_switch_rv)(short rv);
2295 short (*pget_input)(void) = get_input;
2307 write8(0xa10009, 0x40);
2310 while (read16(VDP_CTRL_PORT) & 2)
2313 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
2314 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA);
2315 VDP_setReg(VDP_MODE3, 0x00);
2316 VDP_setReg(VDP_MODE4, 0x81);
2317 VDP_setReg(VDP_NT_SCROLLA, APLANE >> 10);
2318 VDP_setReg(VDP_NT_SCROLLB, BPLANE >> 13);
2319 VDP_setReg(VDP_SAT_BASE, SLIST >> 9);
2320 VDP_setReg(VDP_HSCROLL, HSCRL >> 10);
2321 VDP_setReg(VDP_AUTOINC, 2);
2322 VDP_setReg(VDP_SCROLLSZ, 0x01);
2323 VDP_setReg(VDP_BACKDROP, 0);
2326 t_dma_zero_wrap_early();
2327 t_dma_zero_fill_early();
2330 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
2331 for (i = 0; i < 32 / 4; i++)
2332 write32(VDP_DATA_PORT, 0);
2334 /* clear name tables */
2335 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(APLANE));
2336 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
2337 write32(VDP_DATA_PORT, 0);
2339 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(BPLANE));
2340 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
2341 write32(VDP_DATA_PORT, 0);
2343 /* SAT, h. scroll */
2344 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(SLIST));
2345 write32(VDP_DATA_PORT, 0);
2347 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(HSCRL));
2348 write32(VDP_DATA_PORT, 0);
2350 /* scroll plane vscroll */
2351 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
2352 write32(VDP_DATA_PORT, 0);
2356 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(TILE_FONT_BASE));
2357 for (i = 0; i < FONT_LEN * 32 / 4; i++)
2358 write32(VDP_DATA_PORT, font_base[i]);
2361 setup_default_palette();
2363 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
2365 have_32x = read32(0xa130ec) == MKLONG('M','A','R','S');
2366 en_32x = have_32x && (read16(0xa15100) & 1);
2367 v8 = read8(0xa10001);
2368 printf("MD version: %02x %s %s %s%s\n", v8,
2369 (v8 & 0x80) ? "world" : "jap",
2370 (v8 & 0x40) ? "pal" : "ntsc",
2371 have_32x ? "32X" : "",
2374 for (i = 0; i < ARRAY_SIZE(g_tests); i++) {
2375 // print test number if we haven't scrolled away
2376 if (printf_ypos < CSCREEN_H) {
2377 int old_ypos = printf_ypos;
2379 printf("%02d/%02d", i, ARRAY_SIZE(g_tests));
2380 printf_ypos = old_ypos;
2383 if ((g_tests[i].type & T_32) && !have_32x) {
2387 ret = g_tests[i].test();
2388 if (ret == R_SKIP) {
2394 printf("failed %d: %s\n", i, g_tests[i].name);
2402 printf("%d/%d passed, %d skipped.\n",
2403 passed, ARRAY_SIZE(g_tests), skipped);
2409 u8 *p = (u8 *)0xff0040;
2410 u32 len = x32x_switch_rv_end - x32x_switch_rv;
2411 px32x_switch_rv = (void *)p; p += len;
2412 memcpy_(px32x_switch_rv, x32x_switch_rv, len);
2414 len = get_input_end - get_input_s;
2415 pget_input = (void *)p; p += len;
2416 memcpy_(pget_input, get_input_s, len);
2418 // prepare for reset - run from 880xxx as the reset vector points there
2419 // todo: broken printf
2422 for (i = 0; i < 60*60 && !(pget_input() & BTNM_A); i++)
2425 // blank due to my lame tv being burn-in prone
2426 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
2428 while (!(pget_input() & BTNM_A))
2430 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
2434 char c[3] = { '0', '0', '0' };
2435 short hscroll = 0, vscroll = 0;
2436 short hsz = 1, vsz = 0;
2439 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(APLANE));
2442 for (i = 0, c[0] = 'a'; i < 8 * 1024 / 2; i++) {
2443 write16(VDP_DATA_PORT, (u16)c[0] - 32 + TILE_FONT_BASE / 32);
2445 if (c[0] == 'z' + 1)
2449 for (i = 0; i < 8 * 1024 / 2 / 4; i++) {
2450 write16(VDP_DATA_PORT, (u16)'.' - 32 + TILE_FONT_BASE / 32);
2451 write16(VDP_DATA_PORT, (u16)c[2] - 32 + TILE_FONT_BASE / 32);
2452 write16(VDP_DATA_PORT, (u16)c[1] - 32 + TILE_FONT_BASE / 32);
2453 write16(VDP_DATA_PORT, (u16)c[0] - 32 + TILE_FONT_BASE / 32);
2459 while (pget_input() & BTNM_A)
2464 int b = pget_input();
2467 hscroll = 1, vscroll = -1;
2470 } while (pget_input() & BTNM_C);
2473 if (b & (BTNM_L | BTNM_R | BTNM_C)) {
2474 hscroll += (b & BTNM_L) ? 1 : -1;
2475 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(HSCRL));
2476 write16(VDP_DATA_PORT, hscroll);
2478 if (b & (BTNM_U | BTNM_D | BTNM_C)) {
2479 vscroll += (b & BTNM_U) ? -1 : 1;
2480 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
2482 int end = (int)vscroll + 21;
2483 for (i = vscroll; i < end; i++)
2484 write32(VDP_DATA_PORT, i << 17);
2485 VDP_setReg(VDP_MODE3, 0x04);
2488 write16(VDP_DATA_PORT, vscroll);
2489 VDP_setReg(VDP_MODE3, 0x00);
2493 hsz = (hsz + 1) & 3;
2496 } while (pget_input() & BTNM_A);
2499 vsz = (vsz + 1) & 3;
2502 } while (pget_input() & BTNM_B);
2504 VDP_setReg(VDP_SCROLLSZ, (vsz << 4) | hsz);
2509 printf(" %d %d ", hsz, vsz);
2521 // vim:ts=4:sw=4:expandtab