2 * This software is released into the public domain.
3 * See UNLICENSE file in top level directory.
9 //#pragma GCC diagnostic ignored "-Wunused-function"
12 #define VERSION "unknown build"
15 #define VDP_DATA_PORT 0xC00000
16 #define VDP_CTRL_PORT 0xC00004
17 #define VDP_HV_COUNTER 0xC00008
19 #define TILE_MEM_END 0xB000
22 #define TILE_FONT_BASE (TILE_MEM_END - FONT_LEN * 32)
24 /* note: using ED menu's layout here.. */
25 #define WPLANE (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)
31 #define write16_z80le(a, d) \
32 ((volatile u8 *)(a))[0] = (u8)(d), \
33 ((volatile u8 *)(a))[1] = ((d) >> 8)
35 static inline u16 read16_z80le(const void *a_)
37 volatile const u8 *a = (volatile const u8 *)a_;
38 return a[0] | ((u16)a[1] << 8);
41 #define CTL_WRITE_VRAM(adr) \
42 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00)
43 #define CTL_WRITE_VSRAM(adr) \
44 (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x10)
45 #define CTL_WRITE_CRAM(adr) \
46 (((0xC000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00)
47 #define CTL_READ_VRAM(adr) \
48 (((0x0000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00)
49 #define CTL_READ_VSRAM(adr) \
50 (((0x0000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x10)
51 #define CTL_READ_CRAM(adr) \
52 (((0x0000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x20)
54 #define CTL_WRITE_DMA 0x80
56 #define VDP_setReg(r, v) \
57 write16(VDP_CTRL_PORT, 0x8000 | ((r) << 8) | ((v) & 0xff))
62 VDP_NT_SCROLLA = 0x02,
64 VDP_NT_SCROLLB = 0x04,
79 #define VDP_MODE1_PS 0x04
80 #define VDP_MODE1_IE1 0x10 // h int
81 #define VDP_MODE2_MD 0x04
82 #define VDP_MODE2_PAL 0x08 // 30 col
83 #define VDP_MODE2_DMA 0x10
84 #define VDP_MODE2_IE0 0x20 // v int
85 #define VDP_MODE2_DISP 0x40
86 #define VDP_MODE2_128K 0x80
88 #define SR_PAL (1 << 0)
89 #define SR_DMA (1 << 1)
90 #define SR_HB (1 << 2)
91 #define SR_VB (1 << 3)
92 #define SR_ODD (1 << 4)
94 #define SR_SOVR (1 << 6)
96 #define SR_FULL (1 << 8)
97 #define SR_EMPT (1 << 9)
100 #define LEFT_BORDER 1 /* lame TV */
106 extern const u32 font_base[];
107 extern const u8 z80_test[];
108 extern const u8 z80_test_end[];
112 static noinline void VDP_drawTextML(const char *str, u16 plane_base,
115 const u8 *src = (const u8 *)str;
116 u16 basetile = text_pal << 13;
117 int max_len = 40 - LEFT_BORDER;
123 for (len = 0; str[len] && len < max_len; len++)
125 if (len > (PLANE_W - x))
128 addr = plane_base + ((x + (PLANE_W * y)) << 1);
129 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(addr));
132 write16(VDP_DATA_PORT,
133 basetile | ((*src++) - 32 + TILE_FONT_BASE / 32));
137 static int printf_ypos;
139 static void printf_line(int x, const char *buf)
144 VDP_drawTextML(buf, APLANE, x, printf_ypos++ & (PLANE_H - 1));
146 if (printf_ypos >= CSCREEN_H) {
147 /* clear next line */
149 addr += (PLANE_W * (printf_ypos & (PLANE_H - 1))) << 1;
150 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(addr));
151 for (i = 0; i < 40 / 2; i++)
152 write32(VDP_DATA_PORT, 0);
155 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
156 write16(VDP_DATA_PORT, (printf_ypos - CSCREEN_H + 1) * 8);
160 #define PRINTF_LEN 40
162 static int printf_xpos;
164 static noinline int printf(const char *fmt, ...)
166 static const char hexchars[] = "0123456789abcdef";
167 char c, buf[PRINTF_LEN + 11 + 1];
176 for (d = 0; *fmt; ) {
187 printf_line(printf_xpos, buf);
203 while ('1' <= *fmt && *fmt <= '9') {
204 fwidth = fwidth * 10 + *fmt - '0';
214 ival = va_arg(ap, int);
219 for (i = 1000000000; i >= 10; i /= 10)
222 for (; i >= 10; i /= 10) {
223 buf[d++] = '0' + ival / i;
226 buf[d++] = '0' + ival;
229 uval = va_arg(ap, int);
230 while (fwidth > 1 && uval < (1 << (fwidth - 1) * 4)) {
231 buf[d++] = prefix0 ? '0' : ' ';
234 for (j = 1; j < 8 && uval >= (1 << j * 4); j++)
236 for (j--; j >= 0; j--)
237 buf[d++] = hexchars[(uval >> j * 4) & 0x0f];
240 buf[d++] = va_arg(ap, int);
243 s = va_arg(ap, char *);
244 while (*s && d < PRINTF_LEN)
248 // don't handle, for now
259 VDP_drawTextML(buf, APLANE, printf_xpos,
260 printf_ypos & (PLANE_H - 1));
267 static const char *exc_names[] = {
272 "Illegal Instruction",
276 "Privilege Violation", /* 8 8 */
278 "Line 1010 Emulator",
279 "Line 1111 Emulator",
283 "Uninitialized Interrupt",
292 "Spurious Interrupt", /* 18 24 */
305 u16 ecxnum; // from handler
317 } bae _packed; // bus/address error frame
321 void exception(const struct exc_frame *f)
326 while (read16(VDP_CTRL_PORT) & 2)
328 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
329 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DISP);
331 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
332 write16(VDP_DATA_PORT,
333 printf_ypos >= CSCREEN_H ?
334 (printf_ypos - CSCREEN_H + 1) * 8 : 0);
336 printf("exception %i ", f->ecxnum);
337 if (f->ecxnum < ARRAY_SIZE(exc_names) && exc_names[f->ecxnum] != NULL)
338 printf("(%s)", exc_names[f->ecxnum]);
340 printf(" (%s)", (f->bae.fc & 0x10) ? "r" : "w");
344 printf(" PC: %08x SR: %04x \n", f->bae.pc, f->bae.sr);
345 printf("addr: %08x IR: %04x FC: %02x \n",
346 f->bae.addr, f->bae.ir, f->bae.fc);
350 printf(" PC: %08x SR: %04x \n", f->g.pc, f->g.sr);
353 sp = (u32 *)(f->ar[7] + sp_add);
354 for (i = 0; i < 7; i++)
355 printf(" D%d: %08x A%d: %08x \n", i, f->dr[i], i, f->ar[i]);
356 printf(" D%d: %08x SP: %08x \n", i, f->dr[i], (u32)sp);
358 printf(" %08x %08x %08x %08x\n", sp[0], sp[1], sp[2], sp[3]);
359 printf(" %08x %08x %08x %08x\n", sp[4], sp[5], sp[6], sp[7]);
364 static void setup_default_palette(void)
366 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0));
367 write32(VDP_DATA_PORT, 0);
368 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(15 * 2)); // font normal
369 write16(VDP_DATA_PORT, 0xeee);
370 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(31 * 2)); // green
371 write16(VDP_DATA_PORT, 0x0e0);
372 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(47 * 2)); // red
373 write16(VDP_DATA_PORT, 0x00e);
376 static void do_setup_dma(const void *src_, u16 words)
379 // VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA);
380 VDP_setReg(VDP_DMA_LEN0, words);
381 VDP_setReg(VDP_DMA_LEN1, words >> 8);
382 VDP_setReg(VDP_DMA_SRC0, src >> 1);
383 VDP_setReg(VDP_DMA_SRC1, src >> 9);
384 VDP_setReg(VDP_DMA_SRC2, src >> 17);
385 // write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(addr) | CTL_WRITE_DMA);
388 static void vdp_wait_for_fifo_empty(void)
390 while (!(read16(VDP_CTRL_PORT) & 0x200))
391 /* fifo not empty */;
396 static void vdp_wait_for_dma_idle(void)
398 while (read16(VDP_CTRL_PORT) & 2)
404 static void vdp_wait_for_line_0(void)
406 // in PAL vcounter reports 0 twice in a frame,
407 // so wait for vblank to clear first
408 while (!(read16(VDP_CTRL_PORT) & 8))
410 while (read16(VDP_CTRL_PORT) & 8)
412 while (read8(VDP_HV_COUNTER) != 0)
418 static void wait_next_vsync(void)
420 while (read16(VDP_CTRL_PORT) & SR_VB)
422 while (!(read16(VDP_CTRL_PORT) & SR_VB))
428 static void t_dma_zero_wrap_early(void)
430 const u32 *src = (const u32 *)0x3c0000;
431 u32 *ram = (u32 *)0xff0000;
433 do_setup_dma(src + 4, 2);
434 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0) | CTL_WRITE_DMA);
435 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0) | CTL_WRITE_DMA);
437 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0));
438 ram[0] = read32(VDP_DATA_PORT);
439 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0xfffc));
440 ram[1] = read32(VDP_DATA_PORT);
443 static void t_dma_zero_fill_early(void)
445 u32 *ram = (u32 *)0xff0000;
447 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
448 write32(VDP_DATA_PORT, 0);
449 write32(VDP_DATA_PORT, 0);
450 write32(VDP_DATA_PORT, 0);
451 write32(VDP_DATA_PORT, 0);
453 VDP_setReg(VDP_AUTOINC, 1);
454 VDP_setReg(VDP_DMA_SRC2, 0x80);
455 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(1) | CTL_WRITE_DMA);
456 write16(VDP_DATA_PORT, 0x1122);
457 ram[2] = read16(VDP_CTRL_PORT);
458 vdp_wait_for_dma_idle();
460 VDP_setReg(VDP_AUTOINC, 2);
461 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0));
462 ram[3] = read32(VDP_DATA_PORT);
465 #define R_SKIP 0x5a5a
467 #define expect(ok_, v0_, v1_) \
468 do { if ((v0_) != (v1_)) { \
469 printf("%d %s: %08x %08x\n", __LINE__, #v0_, v0_, v1_); \
473 #define expect_sh2(ok_, sh2_, v0_, v1_) \
474 do { if ((v0_) != (v1_)) { \
475 printf("%d %csh2: %08x %08x\n", __LINE__, sh2_ ? 's' : 'm', v0_, v1_); \
479 #define expect_range(ok_, v0_, vmin_, vmax_) \
480 do { if ((v0_) < (vmin_) || (v0_) > (vmax_)) { \
481 printf("%s: %02x /%02x-%02x\n", #v0_, v0_, vmin_, vmax_); \
485 #define expect_bits(ok_, v0_, val_, mask_) \
486 do { if (((v0_) & (mask_)) != (val_)) { \
487 printf("%s: %04x & %04x != %04x\n", #v0_, v0_, mask_, val_); \
491 static int t_dma_zero_wrap(void)
493 const u32 *src = (const u32 *)0x3c0000;
494 const u32 *ram = (const u32 *)0xff0000;
497 expect(ok, ram[0], src[5 + 0x10000/4]);
498 expect(ok, ram[1], src[4]);
502 static int t_dma_zero_fill(void)
504 const u32 *ram = (const u32 *)0xff0000;
509 expect(ok, ram[3], 0x11111111);
513 static int t_dma_ram_wrap(void)
515 u32 *ram = (u32 *)0xff0000;
519 saved = read32(&ram[0x10000/4 - 1]);
520 ram[0x10000/4 - 1] = 0x01020304;
522 do_setup_dma(&ram[0x10000/4 - 1], 4);
524 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
527 write32(&ram[0x10000/4 - 1], saved);
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, 0x01020304);
534 expect(ok, v1, 0x05060708);
538 // test no src reprogram, only len0
539 static int t_dma_multi(void)
541 const u32 *src = (const u32 *)0x3c0000;
545 do_setup_dma(src, 2);
546 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
547 VDP_setReg(VDP_DMA_LEN0, 2);
548 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x104) | CTL_WRITE_DMA);
550 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
551 v0 = read32(VDP_DATA_PORT);
552 v1 = read32(VDP_DATA_PORT);
554 expect(ok, v0, src[0]);
555 expect(ok, v1, src[1]);
559 static int t_dma_cram_wrap(void)
561 u32 *ram = (u32 *)0xff0000;
565 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0));
566 write32(VDP_DATA_PORT, 0);
571 do_setup_dma(ram, 4);
572 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0x7c | 0xff81) | CTL_WRITE_DMA);
574 write32(VDP_CTRL_PORT, CTL_READ_CRAM(0x7c));
575 v0 = read32(VDP_DATA_PORT) & 0x0eee0eee;
576 write32(VDP_CTRL_PORT, CTL_READ_CRAM(0));
577 v1 = read32(VDP_DATA_PORT) & 0x0eee0eee;
579 setup_default_palette();
581 expect(ok, v0, ram[0]);
582 expect(ok, v1, ram[1]);
586 static int t_dma_vsram_wrap(void)
588 u32 *ram32 = (u32 *)0xff0000;
589 u16 *ram16 = (u16 *)0xff0000;
594 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
595 write32(VDP_DATA_PORT, 0);
597 for (i = 0; i < 0x48/2; i++)
600 do_setup_dma(ram16, 0x48/2);
601 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0x3c | 0xff81) | CTL_WRITE_DMA);
603 write32(VDP_CTRL_PORT, CTL_READ_VSRAM(0x3c));
604 v0 = read32(VDP_DATA_PORT) & 0x03ff03ff;
605 write32(VDP_CTRL_PORT, CTL_READ_VSRAM(0));
606 v1 = read32(VDP_DATA_PORT) & 0x03ff03ff;
608 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
609 write32(VDP_DATA_PORT, 0);
611 expect(ok, v0, ram32[0]);
612 expect(ok, v1, ram32[0x48/4 - 1]);
616 static int t_dma_and_data(void)
618 const u32 *src = (const u32 *)0x3c0000;
622 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
623 write32(VDP_DATA_PORT, 0);
625 do_setup_dma(src, 2);
626 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfc) | CTL_WRITE_DMA);
627 write32(VDP_DATA_PORT, 0x5ec8a248);
629 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0xfc));
630 v0 = read32(VDP_DATA_PORT);
631 v1 = read32(VDP_DATA_PORT);
633 expect(ok, v0, src[0]);
634 expect(ok, v1, 0x5ec8a248);
638 static int t_dma_short_cmd(void)
640 const u32 *src = (const u32 *)0x3c0000;
644 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x3ff4));
645 write32(VDP_DATA_PORT, 0x10111213);
646 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfff0));
647 write32(VDP_DATA_PORT, 0x20212223);
648 write32(VDP_DATA_PORT, 0x30313233);
649 vdp_wait_for_fifo_empty();
651 do_setup_dma(src, 2);
652 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfff0) | CTL_WRITE_DMA);
653 write16(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x3ff4) >> 16);
654 write32(VDP_DATA_PORT, 0x40414243);
656 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x3ff4));
657 v0 = read32(VDP_DATA_PORT);
658 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0xfff0));
659 v1 = read32(VDP_DATA_PORT);
660 v2 = read32(VDP_DATA_PORT);
662 expect(ok, v0, 0x10111213);
663 expect(ok, v1, src[0]);
664 expect(ok, v2, 0x40414243);
668 static int t_dma_fill3_odd(void)
673 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
674 write32(VDP_DATA_PORT, 0);
675 write32(VDP_DATA_PORT, 0);
676 write32(VDP_DATA_PORT, 0);
677 vdp_wait_for_fifo_empty();
679 VDP_setReg(VDP_AUTOINC, 3);
680 VDP_setReg(VDP_DMA_LEN0, 3);
681 VDP_setReg(VDP_DMA_SRC2, 0x80);
682 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x101) | CTL_WRITE_DMA);
683 write16(VDP_DATA_PORT, 0x1122);
684 vdp_wait_for_dma_idle();
686 VDP_setReg(VDP_AUTOINC, 2);
687 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
688 v0 = read32(VDP_DATA_PORT);
689 v1 = read32(VDP_DATA_PORT);
690 v2 = read32(VDP_DATA_PORT);
692 expect(ok, v0, 0x22110000);
693 expect(ok, v1, 0x00111100);
694 expect(ok, v2, 0x00000011);
698 static int t_dma_fill3_even(void)
703 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
704 write32(VDP_DATA_PORT, 0);
705 write32(VDP_DATA_PORT, 0);
706 write32(VDP_DATA_PORT, 0);
707 vdp_wait_for_fifo_empty();
709 VDP_setReg(VDP_AUTOINC, 3);
710 VDP_setReg(VDP_DMA_LEN0, 3);
711 VDP_setReg(VDP_DMA_SRC2, 0x80);
712 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
713 write16(VDP_DATA_PORT, 0x1122);
714 vdp_wait_for_dma_idle();
716 VDP_setReg(VDP_AUTOINC, 2);
717 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
718 v0 = read32(VDP_DATA_PORT);
719 v1 = read32(VDP_DATA_PORT);
720 v2 = read32(VDP_DATA_PORT);
722 expect(ok, v0, 0x11221100);
723 expect(ok, v1, 0x00000011);
724 expect(ok, v2, 0x11000000);
728 static unused int t_dma_fill3_vsram(void)
733 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
734 write32(VDP_DATA_PORT, 0);
735 write32(VDP_DATA_PORT, 0);
736 write32(VDP_DATA_PORT, 0);
738 write16(VDP_DATA_PORT, 0x0111);
739 write16(VDP_DATA_PORT, 0x0222);
740 write16(VDP_DATA_PORT, 0x0333);
741 vdp_wait_for_fifo_empty();
743 VDP_setReg(VDP_AUTOINC, 3);
744 VDP_setReg(VDP_DMA_LEN0, 3);
745 VDP_setReg(VDP_DMA_SRC2, 0x80);
746 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(1) | CTL_WRITE_DMA);
747 write16(VDP_DATA_PORT, 0x0102);
748 vdp_wait_for_dma_idle();
750 VDP_setReg(VDP_AUTOINC, 2);
751 write32(VDP_CTRL_PORT, CTL_READ_VSRAM(0));
752 v0 = read32(VDP_DATA_PORT);
753 v1 = read32(VDP_DATA_PORT);
754 v2 = read32(VDP_DATA_PORT);
756 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
757 write32(VDP_DATA_PORT, 0);
759 expect(ok, v0, 0x01020000);
760 expect(ok, v1, 0x01110111);
761 expect(ok, v2, 0x00000111);
765 static int t_dma_fill_dis(void)
770 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
771 write32(VDP_DATA_PORT, 0);
772 write32(VDP_DATA_PORT, 0);
774 VDP_setReg(VDP_DMA_LEN0, 1);
775 VDP_setReg(VDP_DMA_SRC2, 0x80);
776 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
777 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
778 write16(VDP_DATA_PORT, 0x1122);
779 vdp_wait_for_dma_idle();
781 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
782 write16(VDP_DATA_PORT, 0x3344);
783 vdp_wait_for_dma_idle();
785 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
786 v0 = read32(VDP_DATA_PORT);
787 v1 = read32(VDP_DATA_PORT);
794 static int t_dma_fill_src(void)
796 const u32 *src = (const u32 *)0x3c0000;
800 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
801 write32(VDP_DATA_PORT, 0);
803 // do_setup_dma(src, 2); // hang, can't write src2 twice
804 VDP_setReg(VDP_DMA_LEN0, 2);
805 VDP_setReg(VDP_DMA_SRC0, (u32)src >> 1);
806 VDP_setReg(VDP_DMA_SRC1, (u32)src >> 9);
807 VDP_setReg(VDP_DMA_SRC2, 0x80);
808 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
809 write16(VDP_DATA_PORT, 0x1122);
810 vdp_wait_for_dma_idle();
812 VDP_setReg(VDP_DMA_LEN0, 2);
813 VDP_setReg(VDP_DMA_SRC2, (u32)src >> 17);
814 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x104) | CTL_WRITE_DMA);
816 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
817 v0 = read32(VDP_DATA_PORT);
818 v1 = read32(VDP_DATA_PORT);
820 expect(ok, v0, 0x11220011);
821 expect(ok, v1, src[1]);
825 // should not see the busy flag
826 static int t_dma_busy_vram(void)
828 const u32 *src = (const u32 *)0x3c0000;
832 vdp_wait_for_line_0();
834 do_setup_dma(src, 1);
835 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
836 sr[0] = read16(VDP_CTRL_PORT);
838 do_setup_dma(src, 4);
839 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
840 sr[1] = read16(VDP_CTRL_PORT);
842 VDP_setReg(VDP_DMA_LEN0, 8);
843 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
844 sr[2] = read16(VDP_CTRL_PORT);
846 expect_bits(ok, sr[0], 0, SR_DMA);
847 expect_bits(ok, sr[1], 0, SR_DMA);
848 expect_bits(ok, sr[2], 0, SR_DMA);
852 // (((a & 2) >> 1) ^ 1) | ((a & $400) >> 9) | (a & $3FC) | ((a & $1F800) >> 1)
853 static int t_dma_128k(void)
855 u16 *ram = (u16 *)0xff0000;
863 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
864 write32(VDP_DATA_PORT, 0x01020304);
865 write32(VDP_DATA_PORT, 0x05060708);
866 vdp_wait_for_fifo_empty();
869 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_128K);
870 do_setup_dma(ram, 3);
871 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
872 vdp_wait_for_fifo_empty();
874 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
875 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
876 v0 = read32(VDP_DATA_PORT);
877 v1 = read32(VDP_DATA_PORT);
879 expect(ok, v0, 0x22110304);
880 expect(ok, v1, 0x05330708);
884 static int t_vdp_128k_b16(void)
889 VDP_setReg(VDP_AUTOINC, 0);
890 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x8100));
891 write32(VDP_DATA_PORT, 0x01020304);
892 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x10100));
893 write32(VDP_DATA_PORT, 0x05060708);
894 vdp_wait_for_fifo_empty();
896 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_128K);
897 write16(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) >> 16); // note: upper cmd
898 write32(VDP_DATA_PORT, 0x11223344);
899 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x10102));
900 write32(VDP_DATA_PORT, 0x55667788);
901 vdp_wait_for_fifo_empty();
903 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
904 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x8100));
905 v0 = read16(VDP_DATA_PORT);
906 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x0100));
907 v1 = read16(VDP_DATA_PORT);
909 VDP_setReg(VDP_AUTOINC, 2);
911 expect(ok, v0, 0x8844);
912 expect(ok, v1, 0x0708);
916 static unused int t_vdp_128k_b16_inc(void)
921 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
922 write32(VDP_DATA_PORT, 0x01020304);
923 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x8000));
924 write32(VDP_DATA_PORT, 0x05060708);
925 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfffe));
926 write32(VDP_DATA_PORT, 0x090a0b0c);
927 vdp_wait_for_fifo_empty();
929 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_128K);
930 write16(VDP_CTRL_PORT, CTL_WRITE_VRAM(0) >> 16); // note: upper cmd
931 write16(VDP_DATA_PORT, 0x1122);
932 vdp_wait_for_fifo_empty();
934 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
935 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0));
936 v0 = read32(VDP_DATA_PORT);
937 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x8000));
938 v1 = read32(VDP_DATA_PORT);
939 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
940 write32(VDP_DATA_PORT, 0);
941 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x8000));
942 write32(VDP_DATA_PORT, 0);
944 expect(ok, v0, 0x0b0c0304); // XXX: no 22 anywhere?
945 expect(ok, v1, 0x05060708);
949 static int t_vdp_reg_cmd(void)
954 VDP_setReg(VDP_AUTOINC, 0);
955 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
956 write32(VDP_DATA_PORT, 0x01020304);
957 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
958 write32(VDP_DATA_PORT, 0x05060708);
960 VDP_setReg(VDP_AUTOINC, 2);
961 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x0100));
962 v0 = read16(VDP_DATA_PORT);
964 expect(ok, v0, 0x0304);
968 static int t_vdp_sr_vb(void)
973 while (read8(VDP_HV_COUNTER) != 242)
975 sr[0] = read16(VDP_CTRL_PORT);
976 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
977 sr[1] = read16(VDP_CTRL_PORT);
978 while (read8(VDP_HV_COUNTER) != 4)
980 sr[2] = read16(VDP_CTRL_PORT);
981 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
982 sr[3] = read16(VDP_CTRL_PORT);
984 expect_bits(ok, sr[0], SR_VB, SR_VB);
985 expect_bits(ok, sr[1], SR_VB, SR_VB);
986 expect_bits(ok, sr[2], SR_VB, SR_VB);
987 expect_bits(ok, sr[3], 0, SR_VB);
991 /* z80 tests assume busreq state */
992 static int t_z80mem_long_mirror(void)
994 u8 *zram = (u8 *)0xa00000;
997 write8(&zram[0x1100], 0x11);
998 write8(&zram[0x1101], 0x22);
999 write8(&zram[0x1102], 0x33);
1000 write8(&zram[0x1103], 0x44);
1002 write32(&zram[0x3100], 0x55667788);
1005 expect(ok, zram[0x1100], 0x55);
1006 expect(ok, zram[0x1101], 0x22);
1007 expect(ok, zram[0x1102], 0x77);
1008 expect(ok, zram[0x1103], 0x44);
1012 static int t_z80mem_noreq_w(void)
1014 u8 *zram = (u8 *)0xa00000;
1017 write8(&zram[0x1100], 0x11);
1019 write16(0xa11100, 0x000);
1020 write8(&zram[0x1100], 0x22);
1023 write16(0xa11100, 0x100);
1024 while (read16(0xa11100) & 0x100)
1027 expect(ok, zram[0x1100], 0x11);
1031 #define Z80_C_DISPATCH 113 // see z80_test.s80
1032 #define Z80_C_END 17
1033 #define Z80_C_END_VCNT 67
1035 #define Z80_CYLES_TEST1(b) (Z80_C_DISPATCH + ((b) - 1) * 21 + 26 + Z80_C_END)
1037 static int t_z80mem_vdp_r(void)
1039 u8 *zram = (u8 *)0xa00000;
1042 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1043 write32(VDP_DATA_PORT, 0x11223344);
1044 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
1046 zram[0x1000] = 1; // cp
1047 write16_z80le(&zram[0x1002], 0x7f00); // src
1048 write16_z80le(&zram[0x1004], 0x1100); // dst
1049 write16_z80le(&zram[0x1006], 2); // len
1050 zram[0x1100] = zram[0x1101] = zram[0x1102] = 0x5a;
1052 write16(0xa11100, 0x000);
1053 burn10(Z80_CYLES_TEST1(2) * 15 / 7 / 10);
1055 write16(0xa11100, 0x100);
1056 while (read16(0xa11100) & 0x100)
1059 expect(ok, zram[0x1000], 0);
1060 expect(ok, zram[0x1100], 0x11);
1061 expect(ok, zram[0x1101], 0x44);
1062 expect(ok, zram[0x1102], 0x5a);
1066 static unused int t_z80mem_vdp_w(void)
1068 u8 *zram = (u8 *)0xa00000;
1072 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1073 write32(VDP_DATA_PORT, 0x11223344);
1074 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1075 vdp_wait_for_fifo_empty();
1077 zram[0x1000] = 1; // cp
1078 write16_z80le(&zram[0x1002], 0x1100); // src
1079 write16_z80le(&zram[0x1004], 0x7f00); // dst
1080 write16_z80le(&zram[0x1006], 2); // len
1081 zram[0x1100] = 0x55;
1082 zram[0x1101] = 0x66;
1084 write16(0xa11100, 0x000);
1085 burn10(Z80_CYLES_TEST1(2) * 15 / 7 / 10);
1087 write16(0xa11100, 0x100);
1088 while (read16(0xa11100) & 0x100)
1091 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
1092 v0 = read32(VDP_DATA_PORT);
1094 expect(ok, zram[0x1000], 0);
1095 expect(ok, v0, 0x55556666);
1099 static int t_tim_loop(void)
1104 vdp_wait_for_line_0();
1106 vcnt = read8(VDP_HV_COUNTER);
1109 //expect_range(ok, vcnt, 0x80, 0x80);
1110 expect(ok, vcnt, 223);
1114 static int t_tim_z80_loop(void)
1116 u8 pal = read8(0xa10001) & 0x40;
1117 u8 *zram = (u8 *)0xa00000;
1118 u16 z80_loops = pal ? 3420*(313*2+1)/15/100 : 3420*(262*2+1)/15/100; // 2fr + 1ln
1119 u16 _68k_loops = pal ? 3420*(313*2+1)/7/10 : 3420*(262*2+1)/7/10;
1122 zram[0x1000] = 3; // idle loop, save vcnt
1123 write16_z80le(&zram[0x1002], 0); // src (unused)
1124 write16_z80le(&zram[0x1004], 0x1100); // vcnt dst
1125 write16_z80le(&zram[0x1006], z80_loops); // x100 cycles
1129 vdp_wait_for_line_0();
1130 write16(0xa11100, 0x000);
1131 burn10(_68k_loops + (Z80_C_DISPATCH + Z80_C_END_VCNT) * 15 / 7 / 10);
1133 write16(0xa11100, 0x100);
1134 while (read16(0xa11100) & 0x100)
1136 expect(ok, zram[0x1000], 0);
1137 expect(ok, zram[0x1100], 1);
1141 #define Z80_CYCLES_TEST2(b) (Z80_C_DISPATCH + (b) * 38 + Z80_C_END_VCNT)
1144 static void z80_read_loop(u8 *zram, u16 src)
1146 const int pairs = 512 + 256;
1148 zram[0x1000] = 2; // read loop, save vcnt
1149 write16_z80le(&zram[0x1002], src); // src
1150 write16_z80le(&zram[0x1004], 0x1100); // vcnt dst
1151 write16_z80le(&zram[0x1006], pairs); // reads/2
1155 vdp_wait_for_line_0();
1156 write16(0xa11100, 0x000);
1157 burn10(Z80_CYCLES_TEST2(pairs) * 15 / 7 * 2 / 10);
1159 write16(0xa11100, 0x100);
1160 while (read16(0xa11100) & 0x100)
1164 static int t_tim_z80_ram(void)
1166 u8 *zram = (u8 *)0xa00000;
1169 z80_read_loop(zram, 0);
1171 expect(ok, zram[0x1000], 0);
1172 expect_range(ok, zram[0x1100], 0x80, 0x80);
1176 static int t_tim_z80_ym(void)
1178 u8 *zram = (u8 *)0xa00000;
1181 z80_read_loop(zram, 0x4000);
1183 expect(ok, zram[0x1000], 0);
1184 expect_range(ok, zram[0x1100], 0x80, 0x80);
1188 static int t_tim_z80_vdp(void)
1190 u8 *zram = (u8 *)0xa00000;
1193 z80_read_loop(zram, 0x7f08);
1195 expect(ok, zram[0x1000], 0);
1196 expect_range(ok, zram[0x1100], 0x91, 0x91);
1200 static int t_tim_z80_bank_rom(void)
1202 u8 *zram = (u8 *)0xa00000;
1205 for (i = 0; i < 17; i++)
1206 write8(0xa06000, 0); // bank 0
1208 z80_read_loop(zram, 0x8000);
1210 expect(ok, zram[0x1000], 0);
1211 expect_range(ok, zram[0x1100], 0x95, 0x96);
1215 /* borderline too slow */
1217 static void test_vcnt_vb(void)
1219 const u32 *srhv = (u32 *)0xc00006; // to read SR and HV counter
1220 u32 *ram = (u32 *)0xff0000;
1221 u16 vcnt, vcnt_expect = 0;
1225 vdp_wait_for_line_0();
1230 vcnt = val & 0xff00;
1231 if (vcnt == vcnt_expect)
1234 if (vcnt == 0 && !(sr & SR_VB)) // not VB
1235 break; // wrapped to start of frame
1237 vcnt_expect += 0x100;
1238 if (vcnt == vcnt_expect && !((sr ^ (old >> 16)) & SR_VB)) {
1242 // should have a vcnt jump here
1254 static int t_tim_vcnt(void)
1256 const u32 *ram32 = (u32 *)0xff0000;
1257 const u8 *ram = (u8 *)0xff0000;
1258 u8 pal = read8(0xa10001) & 0x40;
1259 u8 vc_jmp_b = pal ? 0x02 : 0xea;
1260 u8 vc_jmp_a = pal ? 0xca : 0xe5;
1261 u16 lines = pal ? 313 : 262;
1265 expect(ok, ram[0*4+2], 0); // line 0
1266 expect_bits(ok, ram[0*4+1], 0, SR_VB);
1267 expect(ok, ram[1*4+2], 223); // last no blank
1268 expect_bits(ok, ram[1*4+1], 0, SR_VB);
1269 expect(ok, ram[2*4+2], 224); // 1st blank
1270 expect_bits(ok, ram[2*4+1], SR_VB, SR_VB);
1271 expect(ok, ram[3*4+2], vc_jmp_b); // before jump
1272 expect_bits(ok, ram[3*4+1], SR_VB, SR_VB);
1273 expect(ok, ram[4*4+2], vc_jmp_a); // after jump
1274 expect_bits(ok, ram[4*4+1], SR_VB, SR_VB);
1275 expect(ok, ram[5*4+2], 0xfe); // before vb clear
1276 expect_bits(ok, ram[5*4+1], SR_VB, SR_VB);
1277 expect(ok, ram[6*4+2], 0xff); // after vb clear
1278 expect_bits(ok, ram[6*4+1], 0, SR_VB);
1279 expect(ok, ram[7*4+2], 0); // next line 0
1280 expect_bits(ok, ram[7*4+1], 0, SR_VB);
1281 expect(ok, ram32[8], lines - 1);
1285 static int t_tim_vcnt_loops(void)
1287 const u16 *ram16 = (u16 *)0xfff004;
1288 u8 pal = read8(0xa10001) & 0x40;
1289 u16 i, lines = pal ? 313 : 262;
1293 expect(ok, ram16[-1*2+0], 0xff);
1294 expect_range(ok, ram16[-1*2+1], 21, 22);
1295 for (i = 0; i < lines; i++)
1296 expect_range(ok, ram16[i*2+1], 19, 21);
1297 expect(ok, ram16[lines*2+0], 0);
1298 expect_range(ok, ram16[lines*2+1], 19, 21);
1302 static int t_tim_hblank_h40(void)
1304 const u8 *r = (u8 *)0xff0000;
1310 expect_bits(ok, r[2], SR_HB, SR_HB);
1311 expect_bits(ok, r[5], SR_HB, SR_HB);
1313 expect_bits(ok, r[7], SR_HB, SR_HB);
1315 expect_bits(ok, r[12], 0, SR_HB);
1319 static int t_tim_hblank_h32(void)
1321 const u8 *r = (u8 *)0xff0000;
1324 VDP_setReg(VDP_MODE4, 0x00);
1326 VDP_setReg(VDP_MODE4, 0x81);
1328 expect_bits(ok, r[0], 0, SR_HB);
1330 expect_bits(ok, r[4], SR_HB, SR_HB);
1331 expect_bits(ok, r[5], SR_HB, SR_HB);
1333 expect_bits(ok, r[8], SR_HB, SR_HB);
1335 expect_bits(ok, r[12], 0, SR_HB);
1339 static int t_tim_vdp_as_vram_w(void)
1344 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1345 vdp_wait_for_line_0();
1346 write16_x16(VDP_DATA_PORT, 112*18 / 16, 0);
1347 vcnt = read8(VDP_HV_COUNTER);
1350 expect(ok, vcnt, 112*2-1);
1354 static int t_tim_vdp_as_cram_w(void)
1359 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0));
1360 vdp_wait_for_line_0();
1361 write16_x16(VDP_DATA_PORT, 112*18 / 16, 0);
1362 vcnt = read8(VDP_HV_COUNTER);
1365 setup_default_palette();
1367 expect(ok, vcnt, 112);
1371 static const u8 hcnt2tm[] =
1373 0x0a, 0x1d, 0x31, 0x44, 0x58, 0x6b, 0x7f, 0x92,
1374 0xa6, 0xb9, 0xcc, 0x00, 0x00, 0x00, 0xe2, 0xf6
1377 static int t_tim_ym_timer_z80(int is_b)
1379 u8 pal = read8(0xa10001) & 0x40;
1380 u8 *zram = (u8 *)0xa00000;
1382 u16 _68k_loops = 3420*(302+5+1)/7/10; // ~ (72*1024*2)/(3420./7)
1383 u16 start, end, diff;
1386 zram[0x1000] = 4 + is_b; // ym2612 timer a/b test
1387 zram[0x1100] = zram[0x1101] = zram[0x1102] = zram[0x1103] = 0;
1390 vdp_wait_for_line_0();
1391 write16(0xa11100, 0x000);
1393 burn10(_68k_loops + (Z80_C_DISPATCH + Z80_C_END_VCNT) * 15 / 7 / 10);
1395 write16(0xa11100, 0x100);
1396 while (read16(0xa11100) & 0x100)
1399 expect(ok, zram[0x1000], 0);
1401 //start = ((u16)zram[0x1102] << 8) | hcnt2tm[zram[0x1103] >> 4];
1402 //end = ((u16)zram[0x1100] << 8) | hcnt2tm[zram[0x1101] >> 4];
1403 start = zram[0x1102];
1407 expect_range(ok, diff, 0xf4, 0xf6);
1409 expect_range(ok, diff, 0x27, 0x29);
1410 write8(&z80[0x4001], 0); // stop, but should keep the flag
1412 burn10(32*6/10); // busy bit, 32 FM ticks (M/7/6)
1414 expect(ok, z80[0x4000], 2);
1415 write8(&z80[0x4001], 0x20); // reset flag (reg 0x27, set up by z80)
1418 expect(ok, z80[0x4000], 1);
1419 write8(&z80[0x4001], 0x10);
1423 expect(ok, z80[0x4000], 0);
1427 static int t_tim_ym_timera_z80(void)
1429 return t_tim_ym_timer_z80(0);
1432 static int t_tim_ym_timerb_z80(void)
1434 return t_tim_ym_timer_z80(1);
1437 static int t_tim_ym_timerb_stop(void)
1448 } *t = (void *)0xfff000;
1449 u8 *z80 = (u8 *)0xa00000;
1452 write16(0xa11100, 0x100);
1453 while (read16(0xa11100) & 0x100)
1455 test_ym_stopped_tick();
1457 //start = ((u16)t->vcnt_start << 8) | hcnt2tm[t->hcnt_start >> 4];
1458 //end = ((u16)t->vcnt_end << 8) | hcnt2tm[t->hcnt_end >> 4];
1459 //diff = end - start;
1460 diff = t->vcnt_end - t->vcnt_start;
1461 //expect_range(ok, diff, 0x492, 0x5c2); // why so much variation?
1462 expect_range(ok, diff, 4, 5);
1463 expect(ok, t->stat0, 0);
1464 expect(ok, t->stat1, 2);
1465 expect(ok, z80[0x4000], 2);
1466 write8(&z80[0x4001], 0x30);
1470 static int t_tim_ym_timer_ab_sync(void)
1472 u16 v1, v2, v3, v4, v5, ln0, ln1, ln2;
1475 vdp_wait_for_line_0();
1476 v1 = test_ym_ab_sync();
1479 burn10(3420*15/7/10); // ~15 scanlines
1480 write8(0xa04001, 0x3f); // clear, no reload
1481 burn10(12); // wait for busy to clear
1482 v2 = read8(0xa04000);
1483 v3 = test_ym_ab_sync2();
1486 burn10(3420*15/7/10); // ~15 scanlines
1487 v4 = test_ym_ab_sync2();
1490 burn10(3420*30/7/10); // ~35 scanlines
1491 v5 = read8(0xa04000);
1498 expect_range(ok, ln1-ln0, 18, 19);
1499 expect_range(ok, ln2-ln1, 32, 34); // almost always 33
1512 // broken on fresh boot due to uknown reasons
1513 static int t_irq_hint(void)
1515 struct irq_test *it = (void *)0xfff000;
1516 struct irq_test *itv = it + 1;
1519 memset_(it, 0, sizeof(*it) * 2);
1520 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1521 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1523 // without this, tests fail after cold boot
1524 while (!(read16(VDP_CTRL_PORT) & 8))
1527 // for more fun, disable the display
1528 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
1531 while (read8(VDP_HV_COUNTER) != 100)
1533 while (read8(VDP_HV_COUNTER) != 229)
1535 // take the pending irq
1536 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1538 burn10(488 * 2 / 10);
1540 expect(ok, it->first.v, 229); // pending irq trigger
1541 expect(ok, it->cnt, 1);
1542 expect(ok, itv->cnt, 0);
1545 it->cnt = it->first.hv = it->last.hv = 0;
1547 while (read8(VDP_HV_COUNTER) != 4)
1549 while (read8(VDP_HV_COUNTER) != 228)
1552 expect(ok, it->cnt, 225);
1553 expect(ok, it->first.v, 0);
1554 expect(ok, it->last.v, 224);
1556 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1558 // detect reload line
1559 it->cnt = it->first.hv = it->last.hv = 0;
1562 while (read16(VDP_CTRL_PORT) & 8)
1564 VDP_setReg(10, 255);
1565 while (read8(VDP_HV_COUNTER) != 228)
1568 expect(ok, it->cnt, 1);
1569 expect(ok, it->first.v, 17);
1570 expect(ok, it->last.v, 17);
1572 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1577 static int t_irq_both_cpu_unmask(void)
1579 struct irq_test *ith = (void *)0xfff000;
1580 struct irq_test *itv = ith + 1;
1584 memset_(ith, 0, sizeof(*ith) * 2);
1585 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1586 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1588 while (read8(VDP_HV_COUNTER) != 100)
1590 while (read8(VDP_HV_COUNTER) != 226)
1593 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1594 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_IE0 | VDP_MODE2_DISP);
1595 /* go to active display line 100 */
1596 while (read8(VDP_HV_COUNTER) != 100)
1598 s0 = read16(VDP_CTRL_PORT);
1599 s1 = move_sr_and_read(0x2000, VDP_CTRL_PORT);
1601 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1602 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1604 expect(ok, itv->cnt, 1); // vint count
1605 expect(ok, itv->first.v, 100); // vint line
1606 expect(ok, ith->cnt, 1); // hint count
1607 expect(ok, ith->first.v, 100); // hint line
1608 expect_bits(ok, s0, SR_F, SR_F);
1609 expect_bits(ok, s1, 0, SR_F);
1613 static int t_irq_ack_v_h(void)
1615 struct irq_test *ith = (void *)0xfff000;
1616 struct irq_test *itv = ith + 1;
1620 memset_(ith, 0, sizeof(*ith) * 2);
1621 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1622 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1624 /* ensure hcnt reload */
1625 while (!(read16(VDP_CTRL_PORT) & 8))
1627 while (read16(VDP_CTRL_PORT) & 8)
1629 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1630 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_IE0);
1631 while (read8(VDP_HV_COUNTER) != 100)
1633 while (read8(VDP_HV_COUNTER) != 226)
1635 s0 = read16(VDP_CTRL_PORT);
1636 s1 = move_sr_and_read(0x2500, VDP_CTRL_PORT);
1638 s2 = move_sr_and_read(0x2000, VDP_CTRL_PORT);
1641 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1642 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1644 expect(ok, itv->cnt, 1); // vint count
1645 expect(ok, itv->first.v, 226); // vint line
1646 expect(ok, ith->cnt, 1); // hint count
1647 expect(ok, ith->first.v, 228); // hint line
1648 expect_bits(ok, s0, SR_F, SR_F);
1649 expect_bits(ok, s1, 0, SR_F);
1650 expect_bits(ok, s2, 0, SR_F);
1654 static int t_irq_ack_v_h_2(void)
1656 struct irq_test *ith = (void *)0xfff000;
1657 struct irq_test *itv = ith + 1;
1661 memset_(ith, 0, sizeof(*ith) * 2);
1662 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1663 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1665 while (read8(VDP_HV_COUNTER) != 100)
1667 while (read8(VDP_HV_COUNTER) != 226)
1669 s0 = read16(VDP_CTRL_PORT);
1671 s1 = read16(VDP_CTRL_PORT);
1672 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1673 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1675 expect(ok, itv->cnt, 2); // vint count
1676 expect(ok, itv->first.v, 226); // vint line
1677 expect(ok, ith->cnt, 1); // hint count
1678 expect(ok, ith->first.v, 227); // hint line
1679 expect_bits(ok, s0, SR_F, SR_F);
1680 expect_bits(ok, s1, 0, SR_F);
1684 static int t_irq_ack_h_v(void)
1686 u16 *ram = (u16 *)0xfff000;
1687 u8 *ram8 = (u8 *)0xfff000;
1691 ram[0] = ram[1] = ram[2] =
1692 ram[4] = ram[5] = ram[6] = 0;
1693 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1694 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1696 while (read8(VDP_HV_COUNTER) != 100)
1698 while (read8(VDP_HV_COUNTER) != 226)
1700 s0 = read16(VDP_CTRL_PORT);
1701 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1704 s1 = read16(VDP_CTRL_PORT);
1705 write_and_read1(VDP_CTRL_PORT, 0x8000 | (VDP_MODE2 << 8)
1706 | VDP_MODE2_MD | VDP_MODE2_IE0, s);
1708 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1709 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1711 expect(ok, ram[0], 1); // hint count
1712 expect(ok, ram8[2], 226); // hint line
1713 expect(ok, ram[4], 1); // vint count
1714 expect(ok, ram8[10], 228); // vint line
1715 expect_bits(ok, s0, SR_F, SR_F);
1716 expect_bits(ok, s1, SR_F, SR_F);
1717 expect_bits(ok, s[0], SR_F, SR_F);
1718 expect_bits(ok, s[1], SR_F, SR_F);
1719 expect_bits(ok, s[2], 0, SR_F);
1720 expect_bits(ok, s[3], 0, SR_F);
1724 static int t_irq_ack_h_v_2(void)
1726 u16 *ram = (u16 *)0xfff000;
1727 u8 *ram8 = (u8 *)0xfff000;
1731 ram[0] = ram[1] = ram[2] =
1732 ram[4] = ram[5] = ram[6] = 0;
1733 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1734 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1736 while (read8(VDP_HV_COUNTER) != 100)
1738 while (read8(VDP_HV_COUNTER) != 226)
1740 s0 = read16(VDP_CTRL_PORT);
1742 s1 = read16(VDP_CTRL_PORT);
1743 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1744 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1746 expect(ok, ram[0], 2); // hint count
1747 expect(ok, ram8[2], 226); // hint first line
1748 expect(ok, ram8[4], 226); // hint last line
1749 expect(ok, ram[4], 0); // vint count
1750 expect(ok, ram8[10], 0); // vint line
1751 expect_bits(ok, s0, SR_F, SR_F);
1752 expect_bits(ok, s1, 0, SR_F);
1756 static void t_irq_f_flag(void)
1758 memcpy_((void *)0xff0140, test_f_vint, test_f_vint_end - test_f_vint);
1759 memset_((void *)0xff0000, 0, 10);
1760 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_IE0 | VDP_MODE2_DISP);
1762 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1765 static int t_irq_f_flag_h40(void)
1767 u8 f, *r = (u8 *)0xff0000;
1772 expect_bits(ok, r[0], 0, SR_F);
1773 expect_bits(ok, r[1], 0, SR_F);
1774 expect_bits(ok, r[2], 0, SR_F);
1775 // hits 1-3 times in range 3-9, usually ~5
1776 f = r[3] | r[4] | r[5] | r[6] | r[7];
1778 expect_bits(ok, r[10], 0, SR_F);
1779 expect_bits(ok, r[11], 0, SR_F);
1780 expect_bits(ok, f, SR_F, SR_F);
1784 static int t_irq_f_flag_h32(void)
1786 u8 f, *r = (u8 *)0xff0000;
1789 VDP_setReg(VDP_MODE4, 0x00);
1791 VDP_setReg(VDP_MODE4, 0x81);
1793 expect_bits(ok, r[0], 0, SR_F);
1794 expect_bits(ok, r[1], 0, SR_F);
1795 // hits 1-3 times in range 2-7, usually 3
1796 f = r[2] | r[3] | r[4] | r[5] | r[6] | r[7];
1798 expect_bits(ok, r[8], 0, SR_F);
1799 expect_bits(ok, r[9], 0, SR_F);
1800 expect_bits(ok, r[10], 0, SR_F);
1801 expect_bits(ok, r[11], 0, SR_F);
1802 expect_bits(ok, f, SR_F, SR_F);
1808 #define IRQ_CNT_FB_BASE 0x1ff00
1811 static void x32_cmd(enum x32x_cmd cmd, u32 a0, u32 a1, u16 is_slave)
1813 u16 v, *r = (u16 *)0xa15120;
1815 u16 cmd_s = cmd | (is_slave << 15);
1818 write32(&r[4/2], a0);
1819 write32(&r[8/2], a1);
1823 for (i = 0; i < 10000 && (v = read16(r)) == cmd_s; i++)
1826 printf("cmd clr: %x\n", v);
1828 printf("exc m s: %02x %02x\n", r8[0x0e], r8[0x0f]);
1833 printf("cmd err: %x\n", v);
1839 static int t_32x_reset_btn(void)
1841 void (*do_32x_disable)(void) = (void *)0xff0040;
1842 u32 *fbl_icnt = (u32 *)(0x840000 + IRQ_CNT_FB_BASE);
1843 u16 *m_icnt = (u16 *)fbl_icnt;
1844 u16 *s_icnt = m_icnt + 8;
1845 u32 *r32 = (u32 *)0xa15100;
1846 u16 *r16 = (u16 *)r32, i, s;
1851 if (!(read16(r16) & 1))
1854 expect(ok, r16[0x00/2], 0x8083);
1856 write8(r8, 0x00); // FM=0
1858 expect(ok, r16[0x00/2], 0x83);
1859 expect(ok, r16[0x02/2], 0);
1860 expect(ok, r16[0x04/2], 3);
1861 expect(ok, r16[0x06/2], 1); // RV (set in sega_gcc.s reset handler)
1862 expect(ok, r32[0x08/4], 0x5a5a08);
1863 expect(ok, r32[0x0c/4], 0x5a5a0c);
1864 expect(ok, r16[0x10/2], 0x5a10);
1865 expect(ok, r32[0x14/4], 0);
1866 expect(ok, r32[0x18/4], 0);
1867 expect(ok, r32[0x1c/4], 0);
1868 expect(ok, r32[0x20/4], 0x00005a20);
1869 expect(ok, r32[0x24/4], 0x5a5a5a24);
1870 expect(ok, r32[0x28/4], 0x5a5a5a28);
1871 expect(ok, r32[0x2c/4], 0x075a5a2c); // 7 - last_irq_vec
1872 if (!(r16[0x00/2] & 0x8000)) {
1873 expect(ok, r8 [0x81], 1);
1874 expect(ok, r16[0x82/2], 1);
1875 expect(ok, r16[0x84/2], 0xff);
1876 expect(ok, r16[0x86/2], 0xffff);
1877 expect(ok, r16[0x88/2], 0);
1878 expect(ok, r8 [0x8b] & ~2, 0); // FEN toggles periodically?
1879 expect(ok, r16[0x8c/2], 0);
1880 expect(ok, r16[0x8e/2], 0);
1881 // setup vdp for t_32x_init
1883 r16[0x82/2] = r16[0x84/2] = r16[0x86/2] = 0;
1885 r32[0x20/4] = r32[0x24/4] = r32[0x28/4] = r32[0x2c/4] = 0;
1886 for (s = 0; s < 2; s++)
1888 x32_cmd(CMD_READ32, 0x20004000, 0, s); // not cleared by hw
1889 expect_sh2(ok, s, r32[0x24/4], 0x02020000); // ADEN | cmd
1890 // t_32x_sh_defaults will test the other bits
1892 // setup for t_32x_sh_defaults
1893 x32_cmd(CMD_WRITE8, 0x20004001, 0, 0);
1894 x32_cmd(CMD_WRITE8, 0x20004001, 0, 1);
1896 for (i = 0; i < 7; i++) {
1897 expect(ok, m_icnt[i], 0x100);
1898 expect(ok, s_icnt[i], 0x100);
1900 expect(ok, m_icnt[7], 0x101); // VRES happened
1901 expect(ok, s_icnt[7], 0x100); // masked on slave
1903 x32_cmd(CMD_GETSR, 0, 0, 1);
1904 expect_sh2(ok, 1, r32[0x24/4] & ~1, 0xf0); // still masked
1905 x32_cmd(CMD_SETSR, 0x10, 0, 1);
1906 expect(ok, r16[0x00/2], 0x8083);
1907 write8(r8, 0x00); // FM=0
1909 expect(ok, m_icnt[7], 0x101);
1910 expect(ok, s_icnt[7], 0x101);
1911 expect(ok, r32[0x2c/4], 0x00070000); // 7 - last_irq_vec
1914 memcpy_(do_32x_disable, x32x_disable,
1915 x32x_disable_end - x32x_disable);
1918 expect(ok, r16[0x00/2], 0x82);
1919 expect(ok, r16[0x02/2], 0);
1920 expect(ok, r16[0x04/2], 3);
1921 expect(ok, r16[0x06/2], 0); // RV cleared by x32x_disable
1922 expect(ok, r32[0x08/4], 0x5a5a08);
1923 expect(ok, r32[0x0c/4], 0x5a5a0c);
1924 expect(ok, r16[0x10/2], 0x5a10);
1925 expect(ok, rl[0x04/4], 0x000800);
1927 // setup for t_32x_init, t_32x_sh_defaults
1929 r16[0x10/2] = 0x1234; // warm reset indicator
1931 expect(ok, r16[0x06/2], 0); // RV
1935 static int t_32x_init(void)
1937 void (*do_32x_enable)(void) = (void *)0xff0040;
1938 u32 M_OK = MKLONG('M','_','O','K');
1939 u32 S_OK = MKLONG('S','_','O','K');
1940 u32 *r32 = (u32 *)0xa15100;
1941 u16 *r16 = (u16 *)r32;
1945 //v1070 = read32(0x1070);
1947 /* what does REN mean exactly?
1948 * Seems to be sometimes clear after reset */
1949 for (i = 0; i < 1000000; i++)
1950 if (read16(r16) & 0x80)
1952 expect(ok, r16[0x00/2], 0x82);
1953 expect(ok, r16[0x02/2], 0);
1954 expect(ok, r16[0x04/2], 0);
1955 expect(ok, r16[0x06/2], 0);
1956 expect(ok, r8 [0x08], 0);
1957 //expect(ok, r32[0x08/4], 0); // garbage 24bit
1958 expect(ok, r8 [0x0c], 0);
1959 //expect(ok, r32[0x0c/4], 0); // garbage 24bit
1960 if (r16[0x10/2] != 0x1234) // warm reset
1961 expect(ok, r16[0x10/2], 0xffff);
1962 expect(ok, r16[0x12/2], 0);
1963 expect(ok, r32[0x14/4], 0);
1964 expect(ok, r32[0x18/4], 0);
1965 expect(ok, r32[0x1c/4], 0);
1966 //expect(ok, r8 [0x81], 0); // VDP; hangs without ADEN
1967 r32[0x20/4] = 0; // master resp
1968 r32[0x24/4] = 0; // slave resp
1972 // check writable bits without ADEN
1973 // 08,0c have garbage or old values (survive MD's power cycle)
1974 write16(&r16[0x00/2], 0);
1976 expect(ok, r16[0x00/2], 0x80);
1977 write16(&r16[0x00/2], 0xfffe);
1979 expect(ok, r16[0x00/2], 0x8082);
1981 r16[0x02/2] = 0xffff;
1982 r32[0x04/4] = 0xffffffff;
1983 r32[0x08/4] = 0xffffffff;
1984 r32[0x0c/4] = 0xffffffff;
1985 r16[0x10/2] = 0xffff;
1986 r32[0x14/4] = 0xffffffff;
1987 r32[0x18/4] = 0xffffffff;
1988 r32[0x1c/4] = 0xffffffff;
1990 expect(ok, r16[0x00/2], 0x82);
1991 expect(ok, r16[0x02/2], 0x03);
1992 expect(ok, r16[0x04/2], 0x03);
1993 expect(ok, r16[0x06/2], 0x07);
1994 expect(ok, r32[0x08/4], 0x00fffffe);
1995 expect(ok, r32[0x0c/4], 0x00ffffff);
1996 expect(ok, r16[0x10/2], 0xfffc);
1997 expect(ok, r32[0x14/4], 0);
1998 expect(ok, r16[0x18/2], 0);
1999 expect(ok, r16[0x1a/2], 0x0101);
2000 expect(ok, r32[0x1c/4], 0);
2007 // could just set RV, but BIOS reads ROM, so can't
2008 memcpy_(do_32x_enable, x32x_enable,
2009 x32x_enable_end - x32x_enable);
2012 expect(ok, r16[0x00/2], 0x83);
2013 expect(ok, r16[0x02/2], 0);
2014 expect(ok, r16[0x04/2], 0);
2015 expect(ok, r16[0x06/2], 1); // RV
2016 expect(ok, r32[0x14/4], 0);
2017 expect(ok, r32[0x18/4], 0);
2018 expect(ok, r32[0x1c/4], 0);
2019 expect(ok, r32[0x20/4], M_OK);
2020 while (!read16(&r16[0x24/2]))
2022 expect(ok, r32[0x24/4], S_OK);
2023 write32(&r32[0x20/4], 0);
2024 if (!(r16[0x00/2] & 0x8000)) {
2025 expect(ok, r8 [0x81], 0);
2026 expect(ok, r16[0x82/2], 0);
2027 expect(ok, r16[0x84/2], 0);
2028 expect(ok, r16[0x86/2], 0);
2029 //expect(ok, r16[0x88/2], 0); // triggers fill?
2030 expect(ok, r8 [0x8b] & ~2, 0);
2031 expect(ok, r16[0x8c/2], 0);
2032 expect(ok, r16[0x8e/2], 0);
2037 static int t_32x_echo(void)
2039 u16 *r16 = (u16 *)0xa15100;
2042 r16[0x2c/2] = r16[0x2e/2] = 0;
2043 x32_cmd(CMD_ECHO, 0x12340000, 0, 0);
2044 expect_sh2(ok, 0, r16[0x26/2], 0x1234);
2045 x32_cmd(CMD_ECHO, 0x23450000, 0, 1);
2046 expect_sh2(ok, 1, r16[0x26/2], 0xa345);
2047 expect(ok, r16[0x2c/2], 0); // no last_irq_vec
2048 expect(ok, r16[0x2e/2], 0); // no exception_index
2052 static int t_32x_sh_defaults(void)
2054 u32 *r32 = (u32 *)0xa15120;
2057 for (s = 0; s < 2; s++)
2059 x32_cmd(CMD_READ32, 0x20004000, 0, s);
2060 expect_sh2(ok, s, r32[0x04/4], 0x02000000); // ADEN
2061 x32_cmd(CMD_READ32, 0x20004004, 0, s);
2062 expect_sh2(ok, s, r32[0x04/4], 0x00004001); // Empty Rv
2063 x32_cmd(CMD_READ32, 0x20004008, 0, s);
2064 expect_sh2(ok, s, r32[0x04/4], 0);
2065 x32_cmd(CMD_READ32, 0x2000400c, 0, s);
2066 expect_sh2(ok, s, r32[0x04/4], 0);
2067 x32_cmd(CMD_GETGBR, 0, 0, s);
2068 expect_sh2(ok, s, r32[0x04/4], 0x20004000);
2073 static int t_32x_md_bios(void)
2075 void (*do_call_c0)(int a, int d) = (void *)0xff0040;
2076 u8 *rmb = (u8 *)0xff0000;
2080 memcpy_(do_call_c0, test_32x_b_c0,
2081 test_32x_b_c0_end - test_32x_b_c0);
2083 do_call_c0(0xff0000, 0x5a);
2085 expect(ok, rmb[0], 0x5a);
2086 expect(ok, rl[0x04/4], 0x880200);
2087 expect(ok, rl[0x10/4], 0x880212);
2088 expect(ok, rl[0x94/4], 0x8802d8);
2092 static int t_32x_md_rom(void)
2097 expect(ok, rl[0x004/4], 0x880200);
2098 expect(ok, rl[0x100/4], 0x53454741);
2099 expect(ok, rl[0x70/4], 0);
2100 write32(&rl[0x70/4], 0xa5123456);
2101 write32(&rl[0x78/4], ~0);
2103 expect(ok, rl[0x78/4], 0x8802ae);
2104 expect(ok, rl[0x70/4], 0xa5123456);
2105 //expect(ok, rl[0x1070/4], v1070);
2106 write32(&rl[0x70/4], 0);
2107 // with RV 0x880000/0x900000 hangs, can't test
2111 static int t_32x_md_fb(void)
2113 u8 *fbb = (u8 *)0x840000;
2114 u16 *fbw = (u16 *)fbb;
2115 u32 *fbl = (u32 *)fbb;
2116 u8 *fob = (u8 *)0x860000;
2117 u16 *fow = (u16 *)fob;
2118 u32 *fol = (u32 *)fob;
2121 fbl[0] = 0x12345678;
2122 fol[1] = 0x89abcdef;
2124 expect(ok, fbw[1], 0x5678);
2125 expect(ok, fow[2], 0x89ab);
2134 expect(ok, fol[0], 0x12340000);
2135 expect(ok, fbl[1], 0x89ab0201);
2139 static int t_32x_sh_fb(void)
2141 u32 *fbl = (u32 *)0x840000;
2142 u8 *r8 = (u8 *)0xa15100;
2145 if (read8(r8) & 0x80)
2146 write8(r8, 0x00); // FM=0
2147 fbl[0] = 0x12345678;
2148 fbl[1] = 0x89abcdef;
2150 write8(r8, 0x80); // FM=1
2151 x32_cmd(CMD_WRITE8, 0x24000000, 0, 0); // should ignore
2152 x32_cmd(CMD_WRITE8, 0x24020001, 0, 0); // ignore
2153 x32_cmd(CMD_WRITE16, 0x24000002, 0, 0); // ok
2154 x32_cmd(CMD_WRITE16, 0x24020000, 0, 0); // ignore
2155 x32_cmd(CMD_WRITE32, 0x24020004, 0x5a0000a5, 1);
2156 write8(r8, 0x00); // FM=0
2158 expect(ok, fbl[0], 0x12340000);
2159 expect(ok, fbl[1], 0x5aabcda5);
2163 static int t_32x_irq_cmd(void)
2165 u32 *fbl_icnt = (u32 *)(0x840000 + IRQ_CNT_FB_BASE);
2166 u16 *m_icnt = (u16 *)fbl_icnt;
2167 u16 *s_icnt = m_icnt + 8;
2168 u32 *r = (u32 *)0xa15100;
2169 u16 *r16 = (u16 *)r;
2173 write8(r, 0x00); // FM=0
2176 for (i = 0; i < 8; i++)
2177 write32(&fbl_icnt[i], 0);
2179 write16(&r16[0x02/2], 0xfffd); // INTM+unused_bits
2181 expect(ok, r16[0x02/2], 1);
2182 x32_cmd(CMD_WRITE8, 0x20004001, 2, 0); // unmask cmd
2183 x32_cmd(CMD_WRITE8, 0x20004001, 2, 1); // unmask cmd slave
2185 write8(r, 0x00); // FM=0 (hangs without)
2187 expect(ok, r16[0x02/2], 0);
2188 expect(ok, r8 [0x2c], 4);
2189 expect(ok, r8 [0x2d], 0);
2190 expect(ok, r16[0x2e/2], 0); // no exception_index
2191 expect(ok, m_icnt[4], 1);
2192 expect(ok, s_icnt[4], 0);
2193 write16(&r16[0x02/2], 0xaaaa); // INTS+unused_bits
2195 expect(ok, r16[0x02/2], 2);
2197 x32_cmd(CMD_WRITE8, 0x20004001, 0, 0); // mask again
2198 x32_cmd(CMD_WRITE8, 0x20004001, 0, 1);
2200 expect(ok, r16[0x02/2], 0);
2201 expect(ok, r8 [0x2c], 4);
2202 expect(ok, r8 [0x2d], 4);
2203 expect(ok, r16[0x2e/2], 0); // no exception_index
2204 write8(r, 0x00); // FM=0
2206 expect(ok, m_icnt[4], 1);
2207 expect(ok, s_icnt[4], 1);
2208 for (i = 0; i < 8; i++) {
2211 expect(ok, m_icnt[i], 0);
2212 expect(ok, s_icnt[i], 0);
2217 static int t_32x_irq_vint(void)
2219 u32 *fbl_icnt = (u32 *)(0x840000 + IRQ_CNT_FB_BASE);
2220 u16 *m_icnt = (u16 *)fbl_icnt;
2221 u16 *s_icnt = m_icnt + 8;
2222 u32 *r = (u32 *)0xa15100;
2223 u16 *r16 = (u16 *)r;
2227 vdp_wait_for_line_0();
2228 write8(r, 0x00); // FM=0
2231 for (i = 0; i < 8; i++)
2232 write32(&fbl_icnt[i], 0);
2234 x32_cmd(CMD_SETSR, 0xf0, 0, 0); // master mask at sr
2235 x32_cmd(CMD_WRITE8, 0x20004001, 8, 0); // unmask both 32x vint
2236 x32_cmd(CMD_WRITE8, 0x20004001, 8, 1);
2239 expect(ok, r16[0x2c/2], 0); // no pending vints
2240 expect(ok, r16[0x2e/2], 0); // no exception_index
2242 write8(&r8[0x23], 0x5a); // no-32x-source-autoclear flag
2245 expect(ok, r8 [0x2c], 0);
2246 expect(ok, r8 [0x2d], 12/2);
2247 write8(&r8[0x2d], 0);
2249 expect(ok, r8 [0x2c], 0);
2250 expect(ok, r8 [0x2d], 12/2);
2252 x32_cmd(CMD_WRITE16, 0x20004016, 0, 0); // clear on 32x (from master)
2254 write16(&r[0x2c/4], 0);
2256 expect(ok, r8 [0x2c], 0);
2257 expect(ok, r8 [0x2d], 12/2);
2258 // (here the slave can't accept commands as it keeps retaking the irq)
2259 write8(&r8[0x23], 0); // handler 32x clear on
2261 write16(&r[0x2c/4], 0);
2263 expect(ok, r16[0x2c/2], 0); // no pending vints
2264 expect(ok, r16[0x2e/2], 0); // no exception_index
2265 write8(r, 0x00); // FM=0
2267 expect(ok, m_icnt[12/2], 0);
2268 expect_range(ok, s_icnt[12/2], 0x10, 0x1000);
2270 x32_cmd(CMD_SETSR, 0x10, 0, 0); // master unmask at sr
2273 expect(ok, r8 [0x2c], 12/2);
2274 expect(ok, r8 [0x2d], 12/2);
2276 x32_cmd(CMD_WRITE8, 0x20004001, 0, 0); // mask
2277 x32_cmd(CMD_WRITE8, 0x20004001, 0, 1);
2278 write16(&r[0x2c/4], 0);
2281 expect(ok, r16[0x2c/2], 0); // no pending vints
2282 expect(ok, r16[0x2e/2], 0); // no exception_index
2284 x32_cmd(CMD_WRITE8, 0x20004001, 8, 1); // slave unmask
2286 x32_cmd(CMD_WRITE8, 0x20004001, 0, 1); // mask
2288 expect(ok, r8 [0x2c], 0);
2289 expect(ok, r8 [0x2d], 12/2);
2290 write16(&r[0x2c/4], 0);
2292 vdp_wait_for_line_0();
2293 x32_cmd(CMD_WRITE8, 0x20004001, 8, 0); // master unmask
2295 x32_cmd(CMD_WRITE8, 0x20004001, 0, 0); // mask
2297 expect(ok, r8 [0x2c], 0);
2298 expect(ok, r8 [0x2d], 0);
2300 write8(&r8[0x23], 0); // handler 32x clear on
2301 write8(r, 0x00); // FM=0
2303 expect(ok, m_icnt[12/2], 1);
2304 for (i = 0; i < 8; i++) {
2307 expect(ok, m_icnt[i], 0);
2308 expect(ok, s_icnt[i], 0);
2313 static int t_32x_reg_w(void)
2315 u32 *r32 = (u32 *)0xa15100;
2316 u16 *r16 = (u16 *)r32, old;
2323 expect(ok, r32[0x08/4], 0xfffffe);
2324 expect(ok, r32[0x0c/4], 0xffffff);
2325 expect(ok, r16[0x10/2], 0xfffc);
2327 r32[0x08/4] = r32[0x0c/4] = 0;
2330 x32_cmd(CMD_WRITE16, 0x20004006, ~old, 0);
2331 expect(ok, r16[0x06/2], old);
2335 // prepare for reset btn press tests
2336 static int t_32x_reset_prep(void)
2338 u32 *fbl = (u32 *)0x840000;
2339 u32 *fbl_icnt = fbl + IRQ_CNT_FB_BASE / 4;
2340 u32 *r32 = (u32 *)0xa15100;
2341 u16 *r16 = (u16 *)r32;
2345 expect(ok, r16[0x00/2], 0x83);
2346 write8(r8, 0x00); // FM=0
2349 expect(ok, r8[0x8b] & ~2, 0);
2350 for (i = 0; i < 8; i++)
2351 write32(&fbl_icnt[i], 0x01000100);
2352 x32_cmd(CMD_WRITE8, 0x20004001, 0x02, 0); // unmask cmd
2353 x32_cmd(CMD_WRITE8, 0x20004001, 0x02, 1); // unmask slave
2354 x32_cmd(CMD_SETSR, 0xf0, 0, 1); // mask slave irqs (on the cpu)
2356 write8(r8, 0x00); // FM=0
2357 expect(ok, r32[0x2c/4], 0);
2359 for (i = 0; i < 8; i++)
2360 expect(ok, fbl_icnt[i], 0x01000100);
2362 r16[0x04/2] = 0xffff;
2363 r32[0x08/4] = 0x5a5a5a08;
2364 r32[0x0c/4] = 0x5a5a5a0c;
2365 r16[0x10/2] = 0x5a10;
2366 r32[0x20/4] = 0x00005a20; // no x32_cmd
2367 r32[0x24/4] = 0x5a5a5a24;
2368 r32[0x28/4] = 0x5a5a5a28;
2369 r32[0x2c/4] = 0x5a5a5a2c;
2370 if (!(r16[0x00/2] & 0x8000)) {
2372 r16[0x8a/2] = 0x0001;
2374 for (i = 0; i < 220/2; i++)
2375 write32(&fbl[i], 0);
2377 r16[0x82/2] = 0xffff;
2378 r16[0x84/2] = 0xffff;
2379 r16[0x86/2] = 0xffff;
2380 r16[0x8a/2] = 0x0000;
2381 r16[0x8c/2] = 0xffff;
2382 r16[0x8e/2] = 0xffff;
2393 static const struct {
2398 // this must be first to disable the 32x and restore the 68k vector table
2399 { T_32, t_32x_reset_btn, "32x resetbtn" },
2401 { T_MD, t_dma_zero_wrap, "dma zero len + wrap" },
2402 { T_MD, t_dma_zero_fill, "dma zero len + fill" },
2403 { T_MD, t_dma_ram_wrap, "dma ram wrap" },
2404 { T_MD, t_dma_multi, "dma multi" },
2405 { T_MD, t_dma_cram_wrap, "dma cram wrap" },
2406 { T_MD, t_dma_vsram_wrap, "dma vsram wrap" },
2407 { T_MD, t_dma_and_data, "dma and data" },
2408 { T_MD, t_dma_short_cmd, "dma short cmd" },
2409 { T_MD, t_dma_fill3_odd, "dma fill3 odd" },
2410 { T_MD, t_dma_fill3_even, "dma fill3 even" },
2411 { T_MD, t_dma_fill3_vsram, "dma fill3 vsram" },
2412 { T_MD, t_dma_fill_dis, "dma fill disabled" },
2413 { T_MD, t_dma_fill_src, "dma fill src incr" },
2414 { T_MD, t_dma_busy_vram, "dma no busy" },
2415 { T_MD, t_dma_128k, "dma 128k mode" },
2416 { T_MD, t_vdp_128k_b16, "vdp 128k addr bit16" },
2417 // { t_vdp_128k_b16_inc, "vdp 128k bit16 inc" }, // mystery
2418 { T_MD, t_vdp_reg_cmd, "vdp reg w cmd reset" },
2419 { T_MD, t_vdp_sr_vb, "vdp status reg vb" },
2420 { T_MD, t_z80mem_long_mirror, "z80 ram long mirror" },
2421 { T_MD, t_z80mem_noreq_w, "z80 ram noreq write" },
2422 { T_MD, t_z80mem_vdp_r, "z80 vdp read" },
2423 // { t_z80mem_vdp_w, "z80 vdp write" }, // hang
2424 { T_MD, t_tim_loop, "time loop" },
2425 { T_MD, t_tim_z80_loop, "time z80 loop" },
2426 { T_MD, t_tim_z80_ram, "time z80 ram" },
2427 { T_MD, t_tim_z80_ym, "time z80 ym2612" },
2428 { T_MD, t_tim_z80_vdp, "time z80 vdp" },
2429 { T_MD, t_tim_z80_bank_rom, "time z80 bank rom" },
2430 { T_MD, t_tim_vcnt, "time V counter" },
2431 { T_MD, t_tim_vcnt_loops, "time vcnt loops" },
2432 { T_MD, t_tim_hblank_h40, "time hblank h40" },
2433 { T_MD, t_tim_hblank_h32, "time hblank h32" },
2434 { T_MD, t_tim_vdp_as_vram_w, "time vdp vram w" },
2435 { T_MD, t_tim_vdp_as_cram_w, "time vdp cram w" },
2436 { T_MD, t_tim_ym_timera_z80, "time timer a z80" },
2437 { T_MD, t_tim_ym_timerb_z80, "time timer b z80" },
2438 { T_MD, t_tim_ym_timerb_stop, "timer b stop" },
2439 { T_MD, t_tim_ym_timer_ab_sync,"timer ab sync" },
2440 { T_MD, t_irq_hint, "irq4 / line" },
2441 { T_MD, t_irq_both_cpu_unmask, "irq both umask" },
2442 { T_MD, t_irq_ack_v_h, "irq ack v-h" },
2443 { T_MD, t_irq_ack_v_h_2, "irq ack v-h 2" },
2444 { T_MD, t_irq_ack_h_v, "irq ack h-v" },
2445 { T_MD, t_irq_ack_h_v_2, "irq ack h-v 2" },
2446 { T_MD, t_irq_f_flag_h40, "irq f flag h40" },
2447 { T_MD, t_irq_f_flag_h32, "irq f flag h32" },
2449 // the first one enables 32X, so must be kept
2450 // all tests assume RV=1 FM=0
2451 { T_32, t_32x_init, "32x init" },
2452 { T_32, t_32x_echo, "32x echo" },
2453 { T_32, t_32x_sh_defaults, "32x sh def" },
2454 { T_32, t_32x_md_bios, "32x md bios" },
2455 { T_32, t_32x_md_rom, "32x md rom" },
2456 { T_32, t_32x_md_fb, "32x md fb" },
2457 { T_32, t_32x_sh_fb, "32x sh fb" },
2458 { T_32, t_32x_irq_cmd, "32x irq cmd" },
2459 { T_32, t_32x_irq_vint, "32x irq vint" },
2460 { T_32, t_32x_reg_w, "32x reg w" },
2461 { T_32, t_32x_reset_prep, "32x rstprep" }, // must be last 32x
2464 static void setup_z80(void)
2466 u8 *zram = (u8 *)0xa00000;
2470 write16(0xa11100, 0x100);
2471 write16(0xa11200, 0x100);
2473 while (read16(0xa11100) & 0x100)
2476 // load the default test program, clear it's data
2477 len = z80_test_end - z80_test;
2478 for (i = 0; i < len; i++)
2479 write8(&zram[i], z80_test[i]);
2480 for (i = 0x1000; i < 0x1007; i++)
2481 write8(&zram[i], 0);
2484 write16(0xa11200, 0x000);
2485 write16(0xa11100, 0x000);
2487 write16(0xa11200, 0x100);
2489 burn10(50 * 15 / 7 / 10); // see z80_test.s80
2491 // take back the bus
2492 write16(0xa11100, 0x100);
2493 while (read16(0xa11100) & 0x100)
2497 static unused int hexinc(char *c)
2511 void (*px32x_switch_rv)(short rv);
2512 short (*pget_input)(void) = get_input;
2524 write8(0xa10009, 0x40);
2527 while (read16(VDP_CTRL_PORT) & 2)
2530 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
2531 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA);
2532 VDP_setReg(VDP_MODE3, 0x00);
2533 VDP_setReg(VDP_MODE4, 0x81);
2534 VDP_setReg(VDP_NT_SCROLLA, APLANE >> 10);
2535 VDP_setReg(VDP_NT_SCROLLB, BPLANE >> 13);
2536 VDP_setReg(VDP_SAT_BASE, SLIST >> 9);
2537 VDP_setReg(VDP_HSCROLL, HSCRL >> 10);
2538 VDP_setReg(VDP_AUTOINC, 2);
2539 VDP_setReg(VDP_SCROLLSZ, 0x01);
2540 VDP_setReg(VDP_BACKDROP, 0);
2543 t_dma_zero_wrap_early();
2544 t_dma_zero_fill_early();
2547 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
2548 for (i = 0; i < 32 / 4; i++)
2549 write32(VDP_DATA_PORT, 0);
2551 /* clear name tables */
2552 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(APLANE));
2553 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
2554 write32(VDP_DATA_PORT, 0);
2556 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(BPLANE));
2557 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
2558 write32(VDP_DATA_PORT, 0);
2560 /* SAT, h. scroll */
2561 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(SLIST));
2562 write32(VDP_DATA_PORT, 0);
2564 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(HSCRL));
2565 write32(VDP_DATA_PORT, 0);
2567 /* scroll plane vscroll */
2568 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
2569 write32(VDP_DATA_PORT, 0);
2573 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(TILE_FONT_BASE));
2574 for (i = 0; i < FONT_LEN * 32 / 4; i++)
2575 write32(VDP_DATA_PORT, font_base[i]);
2578 setup_default_palette();
2580 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
2582 printf("%s\n", VERSION);
2583 have_32x = read32(0xa130ec) == MKLONG('M','A','R','S');
2584 en_32x = have_32x && (read16(0xa15100) & 1);
2585 v8 = read8(0xa10001);
2586 printf("MD version: %02x %s %s %s%s\n", v8,
2587 (v8 & 0x80) ? "world" : "jap",
2588 (v8 & 0x40) ? "pal" : "ntsc",
2589 have_32x ? "32X" : "",
2591 printf("reset hvc %04x->%04x\n", read16(-4), read16(-2));
2594 extern u32 sh2_test[];
2595 if (sh2_test[0] != read32(0x3e0) || sh2_test[0x200/4] != read32(0x3e4))
2596 printf("bad 0x3c0 tab\n");
2598 for (i = 0; i < ARRAY_SIZE(g_tests); i++) {
2599 // print test number if we haven't scrolled away
2600 if (printf_ypos < CSCREEN_H) {
2601 int old_ypos = printf_ypos;
2603 printf("%02d/%02d", i, ARRAY_SIZE(g_tests));
2604 printf_ypos = old_ypos;
2607 if ((g_tests[i].type & T_32) && !have_32x) {
2611 ret = g_tests[i].test();
2612 if (ret == R_SKIP) {
2618 printf("failed %d: %s\n", i, g_tests[i].name);
2626 printf("%d/%d passed, %d skipped.\n",
2627 passed, ARRAY_SIZE(g_tests), skipped);
2632 if (have_32x && (read16(0xa15100) & 1)) {
2633 u8 *p = (u8 *)0xff0040;
2634 u32 len = x32x_switch_rv_end - x32x_switch_rv;
2635 px32x_switch_rv = (void *)p; p += len;
2636 memcpy_(px32x_switch_rv, x32x_switch_rv, len);
2638 len = get_input_end - get_input_s;
2639 pget_input = (void *)p; p += len;
2640 memcpy_(pget_input, get_input_s, len);
2642 // prepare for reset - run from 880xxx as the reset vector points there
2643 // todo: broken printf
2646 for (i = 0; i < 60*60 && !(pget_input() & BTNM_A); i++) {
2647 while (read16(VDP_CTRL_PORT) & SR_VB)
2648 write16(-4, read16(VDP_HV_COUNTER)); /* blanking */
2649 while (!(read16(VDP_CTRL_PORT) & SR_VB))
2650 write16(-4, read16(VDP_HV_COUNTER)); /* not blanking */;
2653 // blank due to my lame tv being burn-in prone
2654 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
2656 while (!(pget_input() & BTNM_A))
2657 write16(-4, read16(VDP_HV_COUNTER));
2658 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
2662 char c[3] = { '0', '0', '0' };
2663 short hscroll = 0, vscroll = 0;
2664 short hsz = 1, vsz = 0;
2667 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(APLANE));
2670 for (i = 0, c[0] = 'a'; i < 8 * 1024 / 2; i++) {
2671 write16(VDP_DATA_PORT, (u16)c[0] - 32 + TILE_FONT_BASE / 32);
2673 if (c[0] == 'z' + 1)
2677 for (i = 0; i < 8 * 1024 / 2 / 4; i++) {
2678 write16(VDP_DATA_PORT, (u16)'.' - 32 + TILE_FONT_BASE / 32);
2679 write16(VDP_DATA_PORT, (u16)c[2] - 32 + TILE_FONT_BASE / 32);
2680 write16(VDP_DATA_PORT, (u16)c[1] - 32 + TILE_FONT_BASE / 32);
2681 write16(VDP_DATA_PORT, (u16)c[0] - 32 + TILE_FONT_BASE / 32);
2687 while (pget_input() & BTNM_A)
2692 int b = pget_input();
2695 hscroll = 1, vscroll = -1;
2698 } while (pget_input() & BTNM_C);
2701 if (b & (BTNM_L | BTNM_R | BTNM_C)) {
2702 hscroll += (b & BTNM_L) ? 1 : -1;
2703 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(HSCRL));
2704 write16(VDP_DATA_PORT, hscroll);
2706 if (b & (BTNM_U | BTNM_D | BTNM_C)) {
2707 vscroll += (b & BTNM_U) ? -1 : 1;
2708 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
2710 int end = (int)vscroll + 21;
2711 for (i = vscroll; i < end; i++)
2712 write32(VDP_DATA_PORT, i << 17);
2713 VDP_setReg(VDP_MODE3, 0x04);
2716 write16(VDP_DATA_PORT, vscroll);
2717 VDP_setReg(VDP_MODE3, 0x00);
2721 hsz = (hsz + 1) & 3;
2724 } while (pget_input() & BTNM_A);
2727 vsz = (vsz + 1) & 3;
2730 } while (pget_input() & BTNM_B);
2732 VDP_setReg(VDP_SCROLLSZ, (vsz << 4) | hsz);
2737 printf(" %d %d ", hsz, vsz);
2749 // vim:ts=4:sw=4:expandtab