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 sp = (u32 *)(f->ar[7] + sp_add);
350 for (i = 0; i < 7; i++)
351 printf(" D%d: %08x A%d: %08x \n", i, f->dr[i], i, f->ar[i]);
352 printf(" D%d: %08x SP: %08x \n", i, f->dr[i], (u32)sp);
354 printf(" %08x %08x %08x %08x\n", sp[0], sp[1], sp[2], sp[3]);
355 printf(" %08x %08x %08x %08x\n", sp[4], sp[5], sp[6], sp[7]);
360 static void setup_default_palette(void)
362 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0));
363 write32(VDP_DATA_PORT, 0);
364 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(15 * 2)); // font normal
365 write16(VDP_DATA_PORT, 0xeee);
366 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(31 * 2)); // green
367 write16(VDP_DATA_PORT, 0x0e0);
368 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(47 * 2)); // red
369 write16(VDP_DATA_PORT, 0x00e);
372 static void do_setup_dma(const void *src_, u16 words)
375 // VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA);
376 VDP_setReg(VDP_DMA_LEN0, words);
377 VDP_setReg(VDP_DMA_LEN1, words >> 8);
378 VDP_setReg(VDP_DMA_SRC0, src >> 1);
379 VDP_setReg(VDP_DMA_SRC1, src >> 9);
380 VDP_setReg(VDP_DMA_SRC2, src >> 17);
381 // write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(addr) | CTL_WRITE_DMA);
384 static void vdp_wait_for_fifo_empty(void)
386 while (!(read16(VDP_CTRL_PORT) & 0x200))
387 /* fifo not empty */;
390 static void vdp_wait_for_dma_idle(void)
392 while (read16(VDP_CTRL_PORT) & 2)
396 static void vdp_wait_for_line_0(void)
398 // in PAL vcounter reports 0 twice in a frame,
399 // so wait for vblank to clear first
400 while (!(read16(VDP_CTRL_PORT) & 8))
402 while (read16(VDP_CTRL_PORT) & 8)
404 while (read8(VDP_HV_COUNTER) != 0)
408 static void wait_next_vsync(void)
410 while (read16(VDP_CTRL_PORT) & SR_VB)
412 while (!(read16(VDP_CTRL_PORT) & SR_VB))
416 static void t_dma_zero_wrap_early(void)
418 const u32 *src = (const u32 *)0x3c0000;
419 u32 *ram = (u32 *)0xff0000;
421 do_setup_dma(src + 4, 2);
422 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0) | CTL_WRITE_DMA);
423 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0) | CTL_WRITE_DMA);
425 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0));
426 ram[0] = read32(VDP_DATA_PORT);
427 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0xfffc));
428 ram[1] = read32(VDP_DATA_PORT);
431 static void t_dma_zero_fill_early(void)
433 u32 *ram = (u32 *)0xff0000;
435 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
436 write32(VDP_DATA_PORT, 0);
437 write32(VDP_DATA_PORT, 0);
438 write32(VDP_DATA_PORT, 0);
439 write32(VDP_DATA_PORT, 0);
441 VDP_setReg(VDP_AUTOINC, 1);
442 VDP_setReg(VDP_DMA_SRC2, 0x80);
443 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(1) | CTL_WRITE_DMA);
444 write16(VDP_DATA_PORT, 0x1122);
445 ram[2] = read16(VDP_CTRL_PORT);
446 vdp_wait_for_dma_idle();
448 VDP_setReg(VDP_AUTOINC, 2);
449 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0));
450 ram[3] = read32(VDP_DATA_PORT);
453 #define R_SKIP 0x5a5a
455 #define expect(ok_, v0_, v1_) \
456 do { if ((v0_) != (v1_)) { \
457 printf("%s: %08x %08x\n", #v0_, v0_, v1_); \
461 #define expect_sh2(ok_, sh2_, v0_, v1_) \
462 do { if ((v0_) != (v1_)) { \
463 printf("%csh2: %08x %08x\n", sh2_ ? 's' : 'm', v0_, v1_); \
467 #define expect_range(ok_, v0_, vmin_, vmax_) \
468 do { if ((v0_) < (vmin_) || (v0_) > (vmax_)) { \
469 printf("%s: %02x /%02x-%02x\n", #v0_, v0_, vmin_, vmax_); \
473 #define expect_bits(ok_, v0_, val_, mask_) \
474 do { if (((v0_) & (mask_)) != (val_)) { \
475 printf("%s: %04x & %04x != %04x\n", #v0_, v0_, mask_, val_); \
479 static int t_dma_zero_wrap(void)
481 const u32 *src = (const u32 *)0x3c0000;
482 const u32 *ram = (const u32 *)0xff0000;
485 expect(ok, ram[0], src[5 + 0x10000/4]);
486 expect(ok, ram[1], src[4]);
490 static int t_dma_zero_fill(void)
492 const u32 *ram = (const u32 *)0xff0000;
497 expect(ok, ram[3], 0x11111111);
501 static int t_dma_ram_wrap(void)
503 u32 *ram = (u32 *)0xff0000;
507 saved = read32(&ram[0x10000/4 - 1]);
508 ram[0x10000/4 - 1] = 0x01020304;
510 do_setup_dma(&ram[0x10000/4 - 1], 4);
512 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
515 write32(&ram[0x10000/4 - 1], saved);
517 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
518 v0 = read32(VDP_DATA_PORT);
519 v1 = read32(VDP_DATA_PORT);
521 expect(ok, v0, 0x01020304);
522 expect(ok, v1, 0x05060708);
526 // test no src reprogram, only len0
527 static int t_dma_multi(void)
529 const u32 *src = (const u32 *)0x3c0000;
533 do_setup_dma(src, 2);
534 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
535 VDP_setReg(VDP_DMA_LEN0, 2);
536 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x104) | CTL_WRITE_DMA);
538 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
539 v0 = read32(VDP_DATA_PORT);
540 v1 = read32(VDP_DATA_PORT);
542 expect(ok, v0, src[0]);
543 expect(ok, v1, src[1]);
547 static int t_dma_cram_wrap(void)
549 u32 *ram = (u32 *)0xff0000;
553 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0));
554 write32(VDP_DATA_PORT, 0);
559 do_setup_dma(ram, 4);
560 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0x7c | 0xff81) | CTL_WRITE_DMA);
562 write32(VDP_CTRL_PORT, CTL_READ_CRAM(0x7c));
563 v0 = read32(VDP_DATA_PORT) & 0x0eee0eee;
564 write32(VDP_CTRL_PORT, CTL_READ_CRAM(0));
565 v1 = read32(VDP_DATA_PORT) & 0x0eee0eee;
567 setup_default_palette();
569 expect(ok, v0, ram[0]);
570 expect(ok, v1, ram[1]);
574 static int t_dma_vsram_wrap(void)
576 u32 *ram32 = (u32 *)0xff0000;
577 u16 *ram16 = (u16 *)0xff0000;
582 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
583 write32(VDP_DATA_PORT, 0);
585 for (i = 0; i < 0x48/2; i++)
588 do_setup_dma(ram16, 0x48/2);
589 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0x3c | 0xff81) | CTL_WRITE_DMA);
591 write32(VDP_CTRL_PORT, CTL_READ_VSRAM(0x3c));
592 v0 = read32(VDP_DATA_PORT) & 0x03ff03ff;
593 write32(VDP_CTRL_PORT, CTL_READ_VSRAM(0));
594 v1 = read32(VDP_DATA_PORT) & 0x03ff03ff;
596 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
597 write32(VDP_DATA_PORT, 0);
599 expect(ok, v0, ram32[0]);
600 expect(ok, v1, ram32[0x48/4 - 1]);
604 static int t_dma_and_data(void)
606 const u32 *src = (const u32 *)0x3c0000;
610 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
611 write32(VDP_DATA_PORT, 0);
613 do_setup_dma(src, 2);
614 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfc) | CTL_WRITE_DMA);
615 write32(VDP_DATA_PORT, 0x5ec8a248);
617 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0xfc));
618 v0 = read32(VDP_DATA_PORT);
619 v1 = read32(VDP_DATA_PORT);
621 expect(ok, v0, src[0]);
622 expect(ok, v1, 0x5ec8a248);
626 static int t_dma_short_cmd(void)
628 const u32 *src = (const u32 *)0x3c0000;
632 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x3ff4));
633 write32(VDP_DATA_PORT, 0x10111213);
634 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfff0));
635 write32(VDP_DATA_PORT, 0x20212223);
636 write32(VDP_DATA_PORT, 0x30313233);
637 vdp_wait_for_fifo_empty();
639 do_setup_dma(src, 2);
640 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfff0) | CTL_WRITE_DMA);
641 write16(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x3ff4) >> 16);
642 write32(VDP_DATA_PORT, 0x40414243);
644 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x3ff4));
645 v0 = read32(VDP_DATA_PORT);
646 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0xfff0));
647 v1 = read32(VDP_DATA_PORT);
648 v2 = read32(VDP_DATA_PORT);
650 expect(ok, v0, 0x10111213);
651 expect(ok, v1, src[0]);
652 expect(ok, v2, 0x40414243);
656 static int t_dma_fill3_odd(void)
661 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
662 write32(VDP_DATA_PORT, 0);
663 write32(VDP_DATA_PORT, 0);
664 write32(VDP_DATA_PORT, 0);
665 vdp_wait_for_fifo_empty();
667 VDP_setReg(VDP_AUTOINC, 3);
668 VDP_setReg(VDP_DMA_LEN0, 3);
669 VDP_setReg(VDP_DMA_SRC2, 0x80);
670 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x101) | CTL_WRITE_DMA);
671 write16(VDP_DATA_PORT, 0x1122);
672 vdp_wait_for_dma_idle();
674 VDP_setReg(VDP_AUTOINC, 2);
675 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
676 v0 = read32(VDP_DATA_PORT);
677 v1 = read32(VDP_DATA_PORT);
678 v2 = read32(VDP_DATA_PORT);
680 expect(ok, v0, 0x22110000);
681 expect(ok, v1, 0x00111100);
682 expect(ok, v2, 0x00000011);
686 static int t_dma_fill3_even(void)
691 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
692 write32(VDP_DATA_PORT, 0);
693 write32(VDP_DATA_PORT, 0);
694 write32(VDP_DATA_PORT, 0);
695 vdp_wait_for_fifo_empty();
697 VDP_setReg(VDP_AUTOINC, 3);
698 VDP_setReg(VDP_DMA_LEN0, 3);
699 VDP_setReg(VDP_DMA_SRC2, 0x80);
700 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
701 write16(VDP_DATA_PORT, 0x1122);
702 vdp_wait_for_dma_idle();
704 VDP_setReg(VDP_AUTOINC, 2);
705 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
706 v0 = read32(VDP_DATA_PORT);
707 v1 = read32(VDP_DATA_PORT);
708 v2 = read32(VDP_DATA_PORT);
710 expect(ok, v0, 0x11221100);
711 expect(ok, v1, 0x00000011);
712 expect(ok, v2, 0x11000000);
716 static unused int t_dma_fill3_vsram(void)
721 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
722 write32(VDP_DATA_PORT, 0);
723 write32(VDP_DATA_PORT, 0);
724 write32(VDP_DATA_PORT, 0);
726 write16(VDP_DATA_PORT, 0x0111);
727 write16(VDP_DATA_PORT, 0x0222);
728 write16(VDP_DATA_PORT, 0x0333);
729 vdp_wait_for_fifo_empty();
731 VDP_setReg(VDP_AUTOINC, 3);
732 VDP_setReg(VDP_DMA_LEN0, 3);
733 VDP_setReg(VDP_DMA_SRC2, 0x80);
734 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(1) | CTL_WRITE_DMA);
735 write16(VDP_DATA_PORT, 0x0102);
736 vdp_wait_for_dma_idle();
738 VDP_setReg(VDP_AUTOINC, 2);
739 write32(VDP_CTRL_PORT, CTL_READ_VSRAM(0));
740 v0 = read32(VDP_DATA_PORT);
741 v1 = read32(VDP_DATA_PORT);
742 v2 = read32(VDP_DATA_PORT);
744 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
745 write32(VDP_DATA_PORT, 0);
747 expect(ok, v0, 0x01020000);
748 expect(ok, v1, 0x01110111);
749 expect(ok, v2, 0x00000111);
753 static int t_dma_fill_dis(void)
758 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
759 write32(VDP_DATA_PORT, 0);
760 write32(VDP_DATA_PORT, 0);
762 VDP_setReg(VDP_DMA_LEN0, 1);
763 VDP_setReg(VDP_DMA_SRC2, 0x80);
764 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
765 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
766 write16(VDP_DATA_PORT, 0x1122);
767 vdp_wait_for_dma_idle();
769 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
770 write16(VDP_DATA_PORT, 0x3344);
771 vdp_wait_for_dma_idle();
773 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
774 v0 = read32(VDP_DATA_PORT);
775 v1 = read32(VDP_DATA_PORT);
782 static int t_dma_fill_src(void)
784 const u32 *src = (const u32 *)0x3c0000;
788 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
789 write32(VDP_DATA_PORT, 0);
791 // do_setup_dma(src, 2); // hang, can't write src2 twice
792 VDP_setReg(VDP_DMA_LEN0, 2);
793 VDP_setReg(VDP_DMA_SRC0, (u32)src >> 1);
794 VDP_setReg(VDP_DMA_SRC1, (u32)src >> 9);
795 VDP_setReg(VDP_DMA_SRC2, 0x80);
796 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
797 write16(VDP_DATA_PORT, 0x1122);
798 vdp_wait_for_dma_idle();
800 VDP_setReg(VDP_DMA_LEN0, 2);
801 VDP_setReg(VDP_DMA_SRC2, (u32)src >> 17);
802 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x104) | CTL_WRITE_DMA);
804 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
805 v0 = read32(VDP_DATA_PORT);
806 v1 = read32(VDP_DATA_PORT);
808 expect(ok, v0, 0x11220011);
809 expect(ok, v1, src[1]);
813 // (((a & 2) >> 1) ^ 1) | ((a & $400) >> 9) | (a & $3FC) | ((a & $1F800) >> 1)
814 static int t_dma_128k(void)
816 u16 *ram = (u16 *)0xff0000;
824 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
825 write32(VDP_DATA_PORT, 0x01020304);
826 write32(VDP_DATA_PORT, 0x05060708);
827 vdp_wait_for_fifo_empty();
830 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_128K);
831 do_setup_dma(ram, 3);
832 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) | CTL_WRITE_DMA);
833 vdp_wait_for_fifo_empty();
835 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
836 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
837 v0 = read32(VDP_DATA_PORT);
838 v1 = read32(VDP_DATA_PORT);
840 expect(ok, v0, 0x22110304);
841 expect(ok, v1, 0x05330708);
845 static int t_vdp_128k_b16(void)
850 VDP_setReg(VDP_AUTOINC, 0);
851 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x8100));
852 write32(VDP_DATA_PORT, 0x01020304);
853 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x10100));
854 write32(VDP_DATA_PORT, 0x05060708);
855 vdp_wait_for_fifo_empty();
857 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_128K);
858 write16(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100) >> 16); // note: upper cmd
859 write32(VDP_DATA_PORT, 0x11223344);
860 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x10102));
861 write32(VDP_DATA_PORT, 0x55667788);
862 vdp_wait_for_fifo_empty();
864 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
865 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x8100));
866 v0 = read16(VDP_DATA_PORT);
867 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x0100));
868 v1 = read16(VDP_DATA_PORT);
870 VDP_setReg(VDP_AUTOINC, 2);
872 expect(ok, v0, 0x8844);
873 expect(ok, v1, 0x0708);
877 static unused int t_vdp_128k_b16_inc(void)
882 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
883 write32(VDP_DATA_PORT, 0x01020304);
884 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x8000));
885 write32(VDP_DATA_PORT, 0x05060708);
886 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0xfffe));
887 write32(VDP_DATA_PORT, 0x090a0b0c);
888 vdp_wait_for_fifo_empty();
890 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_128K);
891 write16(VDP_CTRL_PORT, CTL_WRITE_VRAM(0) >> 16); // note: upper cmd
892 write16(VDP_DATA_PORT, 0x1122);
893 vdp_wait_for_fifo_empty();
895 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
896 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0));
897 v0 = read32(VDP_DATA_PORT);
898 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x8000));
899 v1 = read32(VDP_DATA_PORT);
900 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
901 write32(VDP_DATA_PORT, 0);
902 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x8000));
903 write32(VDP_DATA_PORT, 0);
905 expect(ok, v0, 0x0b0c0304); // XXX: no 22 anywhere?
906 expect(ok, v1, 0x05060708);
910 static int t_vdp_reg_cmd(void)
915 VDP_setReg(VDP_AUTOINC, 0);
916 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
917 write32(VDP_DATA_PORT, 0x01020304);
918 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
919 write32(VDP_DATA_PORT, 0x05060708);
921 VDP_setReg(VDP_AUTOINC, 2);
922 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x0100));
923 v0 = read16(VDP_DATA_PORT);
925 expect(ok, v0, 0x0304);
929 static int t_vdp_sr_vb(void)
934 while (read8(VDP_HV_COUNTER) != 242)
936 sr[0] = read16(VDP_CTRL_PORT);
937 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
938 sr[1] = read16(VDP_CTRL_PORT);
939 while (read8(VDP_HV_COUNTER) != 4)
941 sr[2] = read16(VDP_CTRL_PORT);
942 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
943 sr[3] = read16(VDP_CTRL_PORT);
945 expect_bits(ok, sr[0], SR_VB, SR_VB);
946 expect_bits(ok, sr[1], SR_VB, SR_VB);
947 expect_bits(ok, sr[2], SR_VB, SR_VB);
948 expect_bits(ok, sr[3], 0, SR_VB);
952 /* z80 tests assume busreq state */
953 static int t_z80mem_long_mirror(void)
955 u8 *zram = (u8 *)0xa00000;
958 write8(&zram[0x1100], 0x11);
959 write8(&zram[0x1101], 0x22);
960 write8(&zram[0x1102], 0x33);
961 write8(&zram[0x1103], 0x44);
963 write32(&zram[0x3100], 0x55667788);
966 expect(ok, zram[0x1100], 0x55);
967 expect(ok, zram[0x1101], 0x22);
968 expect(ok, zram[0x1102], 0x77);
969 expect(ok, zram[0x1103], 0x44);
973 static int t_z80mem_noreq_w(void)
975 u8 *zram = (u8 *)0xa00000;
978 write8(&zram[0x1100], 0x11);
980 write16(0xa11100, 0x000);
981 write8(&zram[0x1100], 0x22);
984 write16(0xa11100, 0x100);
985 while (read16(0xa11100) & 0x100)
988 expect(ok, zram[0x1100], 0x11);
992 #define Z80_C_DISPATCH 113 // see z80_test.s80
994 #define Z80_C_END_VCNT 67
996 #define Z80_CYLES_TEST1(b) (Z80_C_DISPATCH + ((b) - 1) * 21 + 26 + Z80_C_END)
998 static int t_z80mem_vdp_r(void)
1000 u8 *zram = (u8 *)0xa00000;
1003 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1004 write32(VDP_DATA_PORT, 0x11223344);
1005 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
1007 zram[0x1000] = 1; // cp
1008 write16_z80le(&zram[0x1002], 0x7f00); // src
1009 write16_z80le(&zram[0x1004], 0x1100); // dst
1010 write16_z80le(&zram[0x1006], 2); // len
1011 zram[0x1100] = zram[0x1101] = zram[0x1102] = 0x5a;
1013 write16(0xa11100, 0x000);
1014 burn10(Z80_CYLES_TEST1(2) * 15 / 7 / 10);
1016 write16(0xa11100, 0x100);
1017 while (read16(0xa11100) & 0x100)
1020 expect(ok, zram[0x1000], 0);
1021 expect(ok, zram[0x1100], 0x11);
1022 expect(ok, zram[0x1101], 0x44);
1023 expect(ok, zram[0x1102], 0x5a);
1027 static unused int t_z80mem_vdp_w(void)
1029 u8 *zram = (u8 *)0xa00000;
1033 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1034 write32(VDP_DATA_PORT, 0x11223344);
1035 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1036 vdp_wait_for_fifo_empty();
1038 zram[0x1000] = 1; // cp
1039 write16_z80le(&zram[0x1002], 0x1100); // src
1040 write16_z80le(&zram[0x1004], 0x7f00); // dst
1041 write16_z80le(&zram[0x1006], 2); // len
1042 zram[0x1100] = 0x55;
1043 zram[0x1101] = 0x66;
1045 write16(0xa11100, 0x000);
1046 burn10(Z80_CYLES_TEST1(2) * 15 / 7 / 10);
1048 write16(0xa11100, 0x100);
1049 while (read16(0xa11100) & 0x100)
1052 write32(VDP_CTRL_PORT, CTL_READ_VRAM(0x100));
1053 v0 = read32(VDP_DATA_PORT);
1055 expect(ok, zram[0x1000], 0);
1056 expect(ok, v0, 0x55556666);
1060 static int t_tim_loop(void)
1065 vdp_wait_for_line_0();
1067 vcnt = read8(VDP_HV_COUNTER);
1070 //expect_range(ok, vcnt, 0x80, 0x80);
1071 expect(ok, vcnt, 223);
1075 static int t_tim_z80_loop(void)
1077 u8 pal = read8(0xa10001) & 0x40;
1078 u8 *zram = (u8 *)0xa00000;
1079 u16 z80_loops = pal ? 3420*(313*2+1)/15/100 : 3420*(262*2+1)/15/100; // 2fr + 1ln
1080 u16 _68k_loops = pal ? 3420*(313*2+1)/7/10 : 3420*(262*2+1)/7/10;
1083 zram[0x1000] = 3; // idle loop, save vcnt
1084 write16_z80le(&zram[0x1002], 0); // src (unused)
1085 write16_z80le(&zram[0x1004], 0x1100); // vcnt dst
1086 write16_z80le(&zram[0x1006], z80_loops); // x100 cycles
1090 vdp_wait_for_line_0();
1091 write16(0xa11100, 0x000);
1092 burn10(_68k_loops + (Z80_C_DISPATCH + Z80_C_END_VCNT) * 15 / 7 / 10);
1094 write16(0xa11100, 0x100);
1095 while (read16(0xa11100) & 0x100)
1097 expect(ok, zram[0x1000], 0);
1098 expect(ok, zram[0x1100], 1);
1102 #define Z80_CYCLES_TEST2(b) (Z80_C_DISPATCH + (b) * 38 + Z80_C_END_VCNT)
1105 static void z80_read_loop(u8 *zram, u16 src)
1107 const int pairs = 512 + 256;
1109 zram[0x1000] = 2; // read loop, save vcnt
1110 write16_z80le(&zram[0x1002], src); // src
1111 write16_z80le(&zram[0x1004], 0x1100); // vcnt dst
1112 write16_z80le(&zram[0x1006], pairs); // reads/2
1116 vdp_wait_for_line_0();
1117 write16(0xa11100, 0x000);
1118 burn10(Z80_CYCLES_TEST2(pairs) * 15 / 7 * 2 / 10);
1120 write16(0xa11100, 0x100);
1121 while (read16(0xa11100) & 0x100)
1125 static int t_tim_z80_ram(void)
1127 u8 *zram = (u8 *)0xa00000;
1130 z80_read_loop(zram, 0);
1132 expect(ok, zram[0x1000], 0);
1133 expect_range(ok, zram[0x1100], 0x80, 0x80);
1137 static int t_tim_z80_ym(void)
1139 u8 *zram = (u8 *)0xa00000;
1142 z80_read_loop(zram, 0x4000);
1144 expect(ok, zram[0x1000], 0);
1145 expect_range(ok, zram[0x1100], 0x80, 0x80);
1149 static int t_tim_z80_vdp(void)
1151 u8 *zram = (u8 *)0xa00000;
1154 z80_read_loop(zram, 0x7f08);
1156 expect(ok, zram[0x1000], 0);
1157 expect_range(ok, zram[0x1100], 0x91, 0x91);
1161 static int t_tim_z80_bank_rom(void)
1163 u8 *zram = (u8 *)0xa00000;
1166 for (i = 0; i < 17; i++)
1167 write8(0xa06000, 0); // bank 0
1169 z80_read_loop(zram, 0x8000);
1171 expect(ok, zram[0x1000], 0);
1172 expect_range(ok, zram[0x1100], 0x95, 0x96);
1176 /* borderline too slow */
1178 static void test_vcnt_vb(void)
1180 const u32 *srhv = (u32 *)0xc00006; // to read SR and HV counter
1181 u32 *ram = (u32 *)0xff0000;
1182 u16 vcnt, vcnt_expect = 0;
1186 vdp_wait_for_line_0();
1191 vcnt = val & 0xff00;
1192 if (vcnt == vcnt_expect)
1195 if (vcnt == 0 && !(sr & SR_VB)) // not VB
1196 break; // wrapped to start of frame
1198 vcnt_expect += 0x100;
1199 if (vcnt == vcnt_expect && !((sr ^ (old >> 16)) & SR_VB)) {
1203 // should have a vcnt jump here
1215 static int t_tim_vcnt(void)
1217 const u32 *ram32 = (u32 *)0xff0000;
1218 const u8 *ram = (u8 *)0xff0000;
1219 u8 pal = read8(0xa10001) & 0x40;
1220 u8 vc_jmp_b = pal ? 0x02 : 0xea;
1221 u8 vc_jmp_a = pal ? 0xca : 0xe5;
1222 u16 lines = pal ? 313 : 262;
1226 expect(ok, ram[0*4+2], 0); // line 0
1227 expect_bits(ok, ram[0*4+1], 0, SR_VB);
1228 expect(ok, ram[1*4+2], 223); // last no blank
1229 expect_bits(ok, ram[1*4+1], 0, SR_VB);
1230 expect(ok, ram[2*4+2], 224); // 1st blank
1231 expect_bits(ok, ram[2*4+1], SR_VB, SR_VB);
1232 expect(ok, ram[3*4+2], vc_jmp_b); // before jump
1233 expect_bits(ok, ram[3*4+1], SR_VB, SR_VB);
1234 expect(ok, ram[4*4+2], vc_jmp_a); // after jump
1235 expect_bits(ok, ram[4*4+1], SR_VB, SR_VB);
1236 expect(ok, ram[5*4+2], 0xfe); // before vb clear
1237 expect_bits(ok, ram[5*4+1], SR_VB, SR_VB);
1238 expect(ok, ram[6*4+2], 0xff); // after vb clear
1239 expect_bits(ok, ram[6*4+1], 0, SR_VB);
1240 expect(ok, ram[7*4+2], 0); // next line 0
1241 expect_bits(ok, ram[7*4+1], 0, SR_VB);
1242 expect(ok, ram32[8], lines - 1);
1246 static int t_tim_vcnt_loops(void)
1248 const u16 *ram16 = (u16 *)0xfff004;
1249 u8 pal = read8(0xa10001) & 0x40;
1250 u16 i, lines = pal ? 313 : 262;
1254 expect(ok, ram16[-1*2+0], 0xff);
1255 expect_range(ok, ram16[-1*2+1], 21, 22);
1256 for (i = 0; i < lines; i++)
1257 expect_range(ok, ram16[i*2+1], 19, 21);
1258 expect(ok, ram16[lines*2+0], 0);
1259 expect_range(ok, ram16[lines*2+1], 19, 21);
1263 static int t_tim_hblank_h40(void)
1265 const u8 *r = (u8 *)0xff0000;
1271 expect_bits(ok, r[2], SR_HB, SR_HB);
1272 expect_bits(ok, r[5], SR_HB, SR_HB);
1274 expect_bits(ok, r[7], SR_HB, SR_HB);
1276 expect_bits(ok, r[12], 0, SR_HB);
1280 static int t_tim_hblank_h32(void)
1282 const u8 *r = (u8 *)0xff0000;
1285 VDP_setReg(VDP_MODE4, 0x00);
1287 VDP_setReg(VDP_MODE4, 0x81);
1289 expect_bits(ok, r[0], 0, SR_HB);
1291 expect_bits(ok, r[4], SR_HB, SR_HB);
1292 expect_bits(ok, r[5], SR_HB, SR_HB);
1294 expect_bits(ok, r[8], SR_HB, SR_HB);
1296 expect_bits(ok, r[12], 0, SR_HB);
1300 static int t_tim_vdp_as_vram_w(void)
1305 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0x100));
1306 vdp_wait_for_line_0();
1307 write16_x16(VDP_DATA_PORT, 112*18 / 16, 0);
1308 vcnt = read8(VDP_HV_COUNTER);
1311 expect(ok, vcnt, 112*2-1);
1315 static int t_tim_vdp_as_cram_w(void)
1320 write32(VDP_CTRL_PORT, CTL_WRITE_CRAM(0));
1321 vdp_wait_for_line_0();
1322 write16_x16(VDP_DATA_PORT, 112*18 / 16, 0);
1323 vcnt = read8(VDP_HV_COUNTER);
1326 setup_default_palette();
1328 expect(ok, vcnt, 112);
1332 static const u8 hcnt2tm[] =
1334 0x0a, 0x1d, 0x31, 0x44, 0x58, 0x6b, 0x7f, 0x92,
1335 0xa6, 0xb9, 0xcc, 0x00, 0x00, 0x00, 0xe2, 0xf6
1338 static int t_tim_ym_timer_z80(int is_b)
1340 u8 pal = read8(0xa10001) & 0x40;
1341 u8 *zram = (u8 *)0xa00000;
1343 u16 _68k_loops = 3420*(302+5+1)/7/10; // ~ (72*1024*2)/(3420./7)
1344 u16 start, end, diff;
1347 zram[0x1000] = 4 + is_b; // ym2612 timer a/b test
1348 zram[0x1100] = zram[0x1101] = zram[0x1102] = zram[0x1103] = 0;
1351 vdp_wait_for_line_0();
1352 write16(0xa11100, 0x000);
1354 burn10(_68k_loops + (Z80_C_DISPATCH + Z80_C_END_VCNT) * 15 / 7 / 10);
1356 write16(0xa11100, 0x100);
1357 while (read16(0xa11100) & 0x100)
1360 expect(ok, zram[0x1000], 0);
1362 //start = ((u16)zram[0x1102] << 8) | hcnt2tm[zram[0x1103] >> 4];
1363 //end = ((u16)zram[0x1100] << 8) | hcnt2tm[zram[0x1101] >> 4];
1364 start = zram[0x1102];
1368 expect_range(ok, diff, 0xf4, 0xf6);
1370 expect_range(ok, diff, 0x27, 0x29);
1371 write8(&z80[0x4001], 0); // stop, but should keep the flag
1373 burn10(32*6/10); // busy bit, 32 FM ticks (M/7/6)
1375 expect(ok, z80[0x4000], 2);
1376 write8(&z80[0x4001], 0x20); // reset flag (reg 0x27, set up by z80)
1379 expect(ok, z80[0x4000], 1);
1380 write8(&z80[0x4001], 0x10);
1384 expect(ok, z80[0x4000], 0);
1388 static int t_tim_ym_timera_z80(void)
1390 return t_tim_ym_timer_z80(0);
1393 static int t_tim_ym_timerb_z80(void)
1395 return t_tim_ym_timer_z80(1);
1398 static int t_tim_ym_timerb_stop(void)
1409 } *t = (void *)0xfff000;
1410 u8 *z80 = (u8 *)0xa00000;
1413 write16(0xa11100, 0x100);
1414 while (read16(0xa11100) & 0x100)
1416 test_ym_stopped_tick();
1418 //start = ((u16)t->vcnt_start << 8) | hcnt2tm[t->hcnt_start >> 4];
1419 //end = ((u16)t->vcnt_end << 8) | hcnt2tm[t->hcnt_end >> 4];
1420 //diff = end - start;
1421 diff = t->vcnt_end - t->vcnt_start;
1422 //expect_range(ok, diff, 0x492, 0x5c2); // why so much variation?
1423 expect_range(ok, diff, 4, 5);
1424 expect(ok, t->stat0, 0);
1425 expect(ok, t->stat1, 2);
1426 expect(ok, z80[0x4000], 2);
1427 write8(&z80[0x4001], 0x30);
1431 static int t_tim_ym_timer_ab_sync(void)
1433 u16 v1, v2, v3, v4, v5, ln0, ln1, ln2;
1436 vdp_wait_for_line_0();
1437 v1 = test_ym_ab_sync();
1440 burn10(3420*15/7/10); // ~15 scanlines
1441 write8(0xa04001, 0x3f); // clear, no reload
1442 burn10(12); // wait for busy to clear
1443 v2 = read8(0xa04000);
1444 v3 = test_ym_ab_sync2();
1447 burn10(3420*15/7/10); // ~15 scanlines
1448 v4 = test_ym_ab_sync2();
1451 burn10(3420*30/7/10); // ~35 scanlines
1452 v5 = read8(0xa04000);
1459 expect_range(ok, ln1-ln0, 18, 19);
1460 expect_range(ok, ln2-ln1, 32, 34); // almost always 33
1473 // broken on fresh boot due to uknown reasons
1474 static int t_irq_hint(void)
1476 struct irq_test *it = (void *)0xfff000;
1477 struct irq_test *itv = it + 1;
1480 memset_(it, 0, sizeof(*it) * 2);
1481 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1482 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1484 // without this, tests fail after cold boot
1485 while (!(read16(VDP_CTRL_PORT) & 8))
1488 // for more fun, disable the display
1489 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
1492 while (read8(VDP_HV_COUNTER) != 100)
1494 while (read8(VDP_HV_COUNTER) != 229)
1496 // take the pending irq
1497 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1499 burn10(488 * 2 / 10);
1501 expect(ok, it->first.v, 229); // pending irq trigger
1502 expect(ok, it->cnt, 1);
1503 expect(ok, itv->cnt, 0);
1506 it->cnt = it->first.hv = it->last.hv = 0;
1508 while (read8(VDP_HV_COUNTER) != 4)
1510 while (read8(VDP_HV_COUNTER) != 228)
1513 expect(ok, it->cnt, 225);
1514 expect(ok, it->first.v, 0);
1515 expect(ok, it->last.v, 224);
1517 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1519 // detect reload line
1520 it->cnt = it->first.hv = it->last.hv = 0;
1523 while (read16(VDP_CTRL_PORT) & 8)
1525 VDP_setReg(10, 255);
1526 while (read8(VDP_HV_COUNTER) != 228)
1529 expect(ok, it->cnt, 1);
1530 expect(ok, it->first.v, 17);
1531 expect(ok, it->last.v, 17);
1533 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1538 static int t_irq_both_cpu_unmask(void)
1540 struct irq_test *ith = (void *)0xfff000;
1541 struct irq_test *itv = ith + 1;
1545 memset_(ith, 0, sizeof(*ith) * 2);
1546 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1547 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1549 while (read8(VDP_HV_COUNTER) != 100)
1551 while (read8(VDP_HV_COUNTER) != 226)
1554 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1555 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_IE0 | VDP_MODE2_DISP);
1556 /* go to active display line 100 */
1557 while (read8(VDP_HV_COUNTER) != 100)
1559 s0 = read16(VDP_CTRL_PORT);
1560 s1 = move_sr_and_read(0x2000, VDP_CTRL_PORT);
1562 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1563 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1565 expect(ok, itv->cnt, 1); // vint count
1566 expect(ok, itv->first.v, 100); // vint line
1567 expect(ok, ith->cnt, 1); // hint count
1568 expect(ok, ith->first.v, 100); // hint line
1569 expect_bits(ok, s0, SR_F, SR_F);
1570 expect_bits(ok, s1, 0, SR_F);
1574 static int t_irq_ack_v_h(void)
1576 struct irq_test *ith = (void *)0xfff000;
1577 struct irq_test *itv = ith + 1;
1581 memset_(ith, 0, sizeof(*ith) * 2);
1582 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1583 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1585 /* ensure hcnt reload */
1586 while (!(read16(VDP_CTRL_PORT) & 8))
1588 while (read16(VDP_CTRL_PORT) & 8)
1590 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1591 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_IE0);
1592 while (read8(VDP_HV_COUNTER) != 100)
1594 while (read8(VDP_HV_COUNTER) != 226)
1596 s0 = read16(VDP_CTRL_PORT);
1597 s1 = move_sr_and_read(0x2500, VDP_CTRL_PORT);
1599 s2 = move_sr_and_read(0x2000, VDP_CTRL_PORT);
1602 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1603 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1605 expect(ok, itv->cnt, 1); // vint count
1606 expect(ok, itv->first.v, 226); // vint line
1607 expect(ok, ith->cnt, 1); // hint count
1608 expect(ok, ith->first.v, 228); // hint line
1609 expect_bits(ok, s0, SR_F, SR_F);
1610 expect_bits(ok, s1, 0, SR_F);
1611 expect_bits(ok, s2, 0, SR_F);
1615 static int t_irq_ack_v_h_2(void)
1617 struct irq_test *ith = (void *)0xfff000;
1618 struct irq_test *itv = ith + 1;
1622 memset_(ith, 0, sizeof(*ith) * 2);
1623 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1624 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1626 while (read8(VDP_HV_COUNTER) != 100)
1628 while (read8(VDP_HV_COUNTER) != 226)
1630 s0 = read16(VDP_CTRL_PORT);
1632 s1 = read16(VDP_CTRL_PORT);
1633 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1634 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1636 expect(ok, itv->cnt, 2); // vint count
1637 expect(ok, itv->first.v, 226); // vint line
1638 expect(ok, ith->cnt, 1); // hint count
1639 expect(ok, ith->first.v, 227); // hint line
1640 expect_bits(ok, s0, SR_F, SR_F);
1641 expect_bits(ok, s1, 0, SR_F);
1645 static int t_irq_ack_h_v(void)
1647 u16 *ram = (u16 *)0xfff000;
1648 u8 *ram8 = (u8 *)0xfff000;
1652 ram[0] = ram[1] = ram[2] =
1653 ram[4] = ram[5] = ram[6] = 0;
1654 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1655 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1657 while (read8(VDP_HV_COUNTER) != 100)
1659 while (read8(VDP_HV_COUNTER) != 226)
1661 s0 = read16(VDP_CTRL_PORT);
1662 VDP_setReg(VDP_MODE1, VDP_MODE1_PS | VDP_MODE1_IE1);
1665 s1 = read16(VDP_CTRL_PORT);
1666 write_and_read1(VDP_CTRL_PORT, 0x8000 | (VDP_MODE2 << 8)
1667 | VDP_MODE2_MD | VDP_MODE2_IE0, s);
1669 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1670 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1672 expect(ok, ram[0], 1); // hint count
1673 expect(ok, ram8[2], 226); // hint line
1674 expect(ok, ram[4], 1); // vint count
1675 expect(ok, ram8[10], 228); // vint line
1676 expect_bits(ok, s0, SR_F, SR_F);
1677 expect_bits(ok, s1, SR_F, SR_F);
1678 expect_bits(ok, s[0], SR_F, SR_F);
1679 expect_bits(ok, s[1], SR_F, SR_F);
1680 expect_bits(ok, s[2], 0, SR_F);
1681 expect_bits(ok, s[3], 0, SR_F);
1685 static int t_irq_ack_h_v_2(void)
1687 u16 *ram = (u16 *)0xfff000;
1688 u8 *ram8 = (u8 *)0xfff000;
1692 ram[0] = ram[1] = ram[2] =
1693 ram[4] = ram[5] = ram[6] = 0;
1694 memcpy_((void *)0xff0100, test_hint, test_hint_end - test_hint);
1695 memcpy_((void *)0xff0140, test_vint, test_vint_end - test_vint);
1697 while (read8(VDP_HV_COUNTER) != 100)
1699 while (read8(VDP_HV_COUNTER) != 226)
1701 s0 = read16(VDP_CTRL_PORT);
1703 s1 = read16(VDP_CTRL_PORT);
1704 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
1705 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1707 expect(ok, ram[0], 2); // hint count
1708 expect(ok, ram8[2], 226); // hint first line
1709 expect(ok, ram8[4], 226); // hint last line
1710 expect(ok, ram[4], 0); // vint count
1711 expect(ok, ram8[10], 0); // vint line
1712 expect_bits(ok, s0, SR_F, SR_F);
1713 expect_bits(ok, s1, 0, SR_F);
1717 static void t_irq_f_flag(void)
1719 memcpy_((void *)0xff0140, test_f_vint, test_f_vint_end - test_f_vint);
1720 memset_((void *)0xff0000, 0, 10);
1721 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_IE0 | VDP_MODE2_DISP);
1723 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
1726 static int t_irq_f_flag_h40(void)
1728 u8 f, *r = (u8 *)0xff0000;
1733 expect_bits(ok, r[0], 0, SR_F);
1734 expect_bits(ok, r[1], 0, SR_F);
1735 expect_bits(ok, r[2], 0, SR_F);
1736 // hits 1-3 times in range 3-9, usually ~5
1737 f = r[3] | r[4] | r[5] | r[6] | r[7];
1739 expect_bits(ok, r[10], 0, SR_F);
1740 expect_bits(ok, r[11], 0, SR_F);
1741 expect_bits(ok, f, SR_F, SR_F);
1745 static int t_irq_f_flag_h32(void)
1747 u8 f, *r = (u8 *)0xff0000;
1750 VDP_setReg(VDP_MODE4, 0x00);
1752 VDP_setReg(VDP_MODE4, 0x81);
1754 expect_bits(ok, r[0], 0, SR_F);
1755 expect_bits(ok, r[1], 0, SR_F);
1756 // hits 1-3 times in range 2-7, usually 3
1757 f = r[2] | r[3] | r[4] | r[5] | r[6] | r[7];
1759 expect_bits(ok, r[8], 0, SR_F);
1760 expect_bits(ok, r[9], 0, SR_F);
1761 expect_bits(ok, r[10], 0, SR_F);
1762 expect_bits(ok, r[11], 0, SR_F);
1763 expect_bits(ok, f, SR_F, SR_F);
1769 #define IRQ_CNT_FB_BASE 0x1ff00
1772 static void x32_cmd(enum x32x_cmd cmd, u32 a0, u32 a1, u16 is_slave)
1774 u16 v, *r = (u16 *)0xa15120;
1776 u16 cmd_s = cmd | (is_slave << 15);
1779 write32(&r[4/2], a0);
1780 write32(&r[8/2], a1);
1784 for (i = 0; i < 10000 && (v = read16(r)) == cmd_s; i++)
1787 printf("cmd clr: %x\n", v);
1789 printf("exc m s: %02x %02x\n", r8[0x0e], r8[0x0f]);
1794 printf("cmd err: %x\n", v);
1799 static int t_32x_reset_btn(void)
1801 void (*do_32x_disable)(void) = (void *)0xff0040;
1802 u32 *fbl_icnt = (u32 *)(0x840000 + IRQ_CNT_FB_BASE);
1803 u16 *m_icnt = (u16 *)fbl_icnt;
1804 u16 *s_icnt = m_icnt + 8;
1805 u32 *r32 = (u32 *)0xa15100;
1806 u16 *r16 = (u16 *)r32, i, s;
1811 if (!(read16(r16) & 1))
1814 expect(ok, r16[0x00/2], 0x8083);
1816 write8(r8, 0x00); // FM=0
1818 expect(ok, r16[0x00/2], 0x83);
1819 expect(ok, r16[0x02/2], 0);
1820 expect(ok, r16[0x04/2], 3);
1821 expect(ok, r16[0x06/2], 1); // RV (set in sega_gcc.s reset handler)
1822 expect(ok, r32[0x08/4], 0x5a5a08);
1823 expect(ok, r32[0x0c/4], 0x5a5a0c);
1824 expect(ok, r16[0x10/2], 0x5a10);
1825 expect(ok, r32[0x14/4], 0);
1826 expect(ok, r32[0x18/4], 0);
1827 expect(ok, r32[0x1c/4], 0);
1828 expect(ok, r32[0x20/4], 0x00005a20);
1829 expect(ok, r32[0x24/4], 0x5a5a5a24);
1830 expect(ok, r32[0x28/4], 0x5a5a5a28);
1831 expect(ok, r32[0x2c/4], 0x075a5a2c); // 7 - last_irq_vec
1832 if (!(r16[0x00/2] & 0x8000)) {
1833 expect(ok, r8 [0x81], 1);
1834 expect(ok, r16[0x82/2], 1);
1835 expect(ok, r16[0x84/2], 0xff);
1836 expect(ok, r16[0x86/2], 0xffff);
1837 expect(ok, r16[0x88/2], 0);
1838 expect(ok, r8 [0x8b] & ~2, 0); // FEN toggles periodically?
1839 expect(ok, r16[0x8c/2], 0);
1840 expect(ok, r16[0x8e/2], 0);
1841 // setup vdp for t_32x_init
1843 r16[0x82/2] = r16[0x84/2] = r16[0x86/2] = 0;
1845 r32[0x20/4] = r32[0x24/4] = r32[0x28/4] = r32[0x2c/4] = 0;
1846 for (s = 0; s < 2; s++)
1848 x32_cmd(CMD_READ32, 0x20004000, 0, s); // not cleared by hw
1849 expect_sh2(ok, s, r32[0x24/4], 0x02020000); // ADEN | cmd
1850 // t_32x_sh_defaults will test the other bits
1852 // setup for t_32x_sh_defaults
1853 x32_cmd(CMD_WRITE8, 0x20004001, 0, 0);
1854 x32_cmd(CMD_WRITE8, 0x20004001, 0, 1);
1856 for (i = 0; i < 7; i++) {
1857 expect(ok, m_icnt[i], 0x100);
1858 expect(ok, s_icnt[i], 0x100);
1860 expect(ok, m_icnt[7], 0x101); // VRES happened
1861 expect(ok, s_icnt[7], 0x100); // masked on slave
1863 x32_cmd(CMD_GETSR, 0, 0, 1);
1864 expect_sh2(ok, 1, r32[0x24/4] & ~1, 0xf0); // still masked
1865 x32_cmd(CMD_SETSR, 0x10, 0, 1);
1866 expect(ok, r16[0x00/2], 0x8083);
1867 write8(r8, 0x00); // FM=0
1869 expect(ok, m_icnt[7], 0x101);
1870 expect(ok, s_icnt[7], 0x101);
1871 expect(ok, r32[0x2c/4], 0x00070000); // 7 - last_irq_vec
1874 memcpy_(do_32x_disable, x32x_disable,
1875 x32x_disable_end - x32x_disable);
1878 expect(ok, r16[0x00/2], 0x82);
1879 expect(ok, r16[0x02/2], 0);
1880 expect(ok, r16[0x04/2], 3);
1881 expect(ok, r16[0x06/2], 0); // RV cleared by x32x_disable
1882 expect(ok, r32[0x08/4], 0x5a5a08);
1883 expect(ok, r32[0x0c/4], 0x5a5a0c);
1884 expect(ok, r16[0x10/2], 0x5a10);
1885 expect(ok, rl[0x04/4], 0x000800);
1887 // setup for t_32x_init, t_32x_sh_defaults
1889 r16[0x10/2] = 0x1234; // warm reset indicator
1891 expect(ok, r16[0x06/2], 0); // RV
1895 static int t_32x_init(void)
1897 void (*do_32x_enable)(void) = (void *)0xff0040;
1898 u32 M_OK = MKLONG('M','_','O','K');
1899 u32 S_OK = MKLONG('S','_','O','K');
1900 u32 *r32 = (u32 *)0xa15100;
1901 u16 *r16 = (u16 *)r32;
1905 //v1070 = read32(0x1070);
1907 /* what does REN mean exactly?
1908 * Seems to be sometimes clear after reset */
1909 for (i = 0; i < 1000000; i++)
1910 if (read16(r16) & 0x80)
1912 expect(ok, r16[0x00/2], 0x82);
1913 expect(ok, r16[0x02/2], 0);
1914 expect(ok, r16[0x04/2], 0);
1915 expect(ok, r16[0x06/2], 0);
1916 expect(ok, r8 [0x08], 0);
1917 //expect(ok, r32[0x08/4], 0); // garbage 24bit
1918 expect(ok, r8 [0x0c], 0);
1919 //expect(ok, r32[0x0c/4], 0); // garbage 24bit
1920 if (r16[0x10/2] != 0x1234) // warm reset
1921 expect(ok, r16[0x10/2], 0xffff);
1922 expect(ok, r16[0x12/2], 0);
1923 expect(ok, r32[0x14/4], 0);
1924 expect(ok, r32[0x18/4], 0);
1925 expect(ok, r32[0x1c/4], 0);
1926 //expect(ok, r8 [0x81], 0); // VDP; hangs without ADEN
1927 r32[0x20/4] = 0; // master resp
1928 r32[0x24/4] = 0; // slave resp
1932 // check writable bits without ADEN
1933 // 08,0c have garbage or old values (survive MD's power cycle)
1934 write16(&r16[0x00/2], 0);
1936 expect(ok, r16[0x00/2], 0x80);
1937 write16(&r16[0x00/2], 0xfffe);
1939 expect(ok, r16[0x00/2], 0x8082);
1941 r16[0x02/2] = 0xffff;
1942 r32[0x04/4] = 0xffffffff;
1943 r32[0x08/4] = 0xffffffff;
1944 r32[0x0c/4] = 0xffffffff;
1945 r16[0x10/2] = 0xffff;
1946 r32[0x14/4] = 0xffffffff;
1947 r32[0x18/4] = 0xffffffff;
1948 r32[0x1c/4] = 0xffffffff;
1950 expect(ok, r16[0x00/2], 0x82);
1951 expect(ok, r16[0x02/2], 0x03);
1952 expect(ok, r16[0x04/2], 0x03);
1953 expect(ok, r16[0x06/2], 0x07);
1954 expect(ok, r32[0x08/4], 0x00fffffe);
1955 expect(ok, r32[0x0c/4], 0x00ffffff);
1956 expect(ok, r16[0x10/2], 0xfffc);
1957 expect(ok, r32[0x14/4], 0);
1958 expect(ok, r16[0x18/2], 0);
1959 expect(ok, r16[0x1a/2], 0x0101);
1960 expect(ok, r32[0x1c/4], 0);
1967 // could just set RV, but BIOS reads ROM, so can't
1968 memcpy_(do_32x_enable, x32x_enable,
1969 x32x_enable_end - x32x_enable);
1972 expect(ok, r16[0x00/2], 0x83);
1973 expect(ok, r16[0x02/2], 0);
1974 expect(ok, r16[0x04/2], 0);
1975 expect(ok, r16[0x06/2], 1); // RV
1976 expect(ok, r32[0x14/4], 0);
1977 expect(ok, r32[0x18/4], 0);
1978 expect(ok, r32[0x1c/4], 0);
1979 expect(ok, r32[0x20/4], M_OK);
1980 while (!read16(&r16[0x24/2]))
1982 expect(ok, r32[0x24/4], S_OK);
1983 write32(&r32[0x20/4], 0);
1984 if (!(r16[0x00/2] & 0x8000)) {
1985 expect(ok, r8 [0x81], 0);
1986 expect(ok, r16[0x82/2], 0);
1987 expect(ok, r16[0x84/2], 0);
1988 expect(ok, r16[0x86/2], 0);
1989 //expect(ok, r16[0x88/2], 0); // triggers fill?
1990 expect(ok, r8 [0x8b] & ~2, 0);
1991 expect(ok, r16[0x8c/2], 0);
1992 expect(ok, r16[0x8e/2], 0);
1997 static int t_32x_echo(void)
1999 u16 *r16 = (u16 *)0xa15100;
2002 r16[0x2c/2] = r16[0x2e/2] = 0;
2003 x32_cmd(CMD_ECHO, 0x12340000, 0, 0);
2004 expect_sh2(ok, 0, r16[0x26/2], 0x1234);
2005 x32_cmd(CMD_ECHO, 0x23450000, 0, 1);
2006 expect_sh2(ok, 1, r16[0x26/2], 0xa345);
2007 expect(ok, r16[0x2c/2], 0); // no last_irq_vec
2008 expect(ok, r16[0x2e/2], 0); // no exception_index
2012 static int t_32x_sh_defaults(void)
2014 u32 *r32 = (u32 *)0xa15120;
2017 for (s = 0; s < 2; s++)
2019 x32_cmd(CMD_READ32, 0x20004000, 0, s);
2020 expect_sh2(ok, s, r32[0x04/4], 0x02000000); // ADEN
2021 x32_cmd(CMD_READ32, 0x20004004, 0, s);
2022 expect_sh2(ok, s, r32[0x04/4], 0x00004001); // Empty Rv
2023 x32_cmd(CMD_READ32, 0x20004008, 0, s);
2024 expect_sh2(ok, s, r32[0x04/4], 0);
2025 x32_cmd(CMD_READ32, 0x2000400c, 0, s);
2026 expect_sh2(ok, s, r32[0x04/4], 0);
2027 x32_cmd(CMD_GETGBR, 0, 0, s);
2028 expect_sh2(ok, s, r32[0x04/4], 0x20004000);
2033 static int t_32x_md_bios(void)
2035 void (*do_call_c0)(int a, int d) = (void *)0xff0040;
2036 u8 *rmb = (u8 *)0xff0000;
2040 memcpy_(do_call_c0, test_32x_b_c0,
2041 test_32x_b_c0_end - test_32x_b_c0);
2043 do_call_c0(0xff0000, 0x5a);
2045 expect(ok, rmb[0], 0x5a);
2046 expect(ok, rl[0x04/4], 0x880200);
2047 expect(ok, rl[0x10/4], 0x880212);
2048 expect(ok, rl[0x94/4], 0x8802d8);
2052 static int t_32x_md_rom(void)
2057 expect(ok, rl[0x004/4], 0x880200);
2058 expect(ok, rl[0x100/4], 0x53454741);
2059 expect(ok, rl[0x70/4], 0);
2060 write32(&rl[0x70/4], 0xa5123456);
2061 write32(&rl[0x78/4], ~0);
2063 expect(ok, rl[0x78/4], 0x8802ae);
2064 expect(ok, rl[0x70/4], 0xa5123456);
2065 //expect(ok, rl[0x1070/4], v1070);
2066 write32(&rl[0x70/4], 0);
2067 // with RV 0x880000/0x900000 hangs, can't test
2071 static int t_32x_md_fb(void)
2073 u8 *fbb = (u8 *)0x840000;
2074 u16 *fbw = (u16 *)fbb;
2075 u32 *fbl = (u32 *)fbb;
2076 u8 *fob = (u8 *)0x860000;
2077 u16 *fow = (u16 *)fob;
2078 u32 *fol = (u32 *)fob;
2081 fbl[0] = 0x12345678;
2082 fol[1] = 0x89abcdef;
2084 expect(ok, fbw[1], 0x5678);
2085 expect(ok, fow[2], 0x89ab);
2094 expect(ok, fol[0], 0x12340000);
2095 expect(ok, fbl[1], 0x89ab0201);
2099 static int t_32x_sh_fb(void)
2101 u32 *fbl = (u32 *)0x840000;
2102 u8 *r8 = (u8 *)0xa15100;
2105 if (read8(r8) & 0x80)
2106 write8(r8, 0x00); // FM=0
2107 fbl[0] = 0x12345678;
2108 fbl[1] = 0x89abcdef;
2110 write8(r8, 0x80); // FM=1
2111 x32_cmd(CMD_WRITE8, 0x24000000, 0, 0); // should ignore
2112 x32_cmd(CMD_WRITE8, 0x24020001, 0, 0); // ignore
2113 x32_cmd(CMD_WRITE16, 0x24000002, 0, 0); // ok
2114 x32_cmd(CMD_WRITE16, 0x24020000, 0, 0); // ignore
2115 x32_cmd(CMD_WRITE32, 0x24020004, 0x5a0000a5, 1);
2116 write8(r8, 0x00); // FM=0
2118 expect(ok, fbl[0], 0x12340000);
2119 expect(ok, fbl[1], 0x5aabcda5);
2123 static int t_32x_irq(void)
2125 u32 *fbl_icnt = (u32 *)(0x840000 + IRQ_CNT_FB_BASE);
2126 u16 *m_icnt = (u16 *)fbl_icnt;
2127 u16 *s_icnt = m_icnt + 8;
2128 u32 *r = (u32 *)0xa15100;
2129 u16 *r16 = (u16 *)r;
2133 write8(r, 0x00); // FM=0
2136 for (i = 0; i < 8; i++)
2137 write32(&fbl_icnt[i], 0);
2139 write16(&r16[0x02/2], 0xfffd); // INTM+unused_bits
2141 expect(ok, r16[0x02/2], 1);
2142 x32_cmd(CMD_WRITE8, 0x20004001, 2, 0); // unmask cmd
2143 x32_cmd(CMD_WRITE8, 0x20004001, 2, 1); // unmask cmd slave
2145 write8(r, 0x00); // FM=0 (hangs without)
2147 expect(ok, r16[0x02/2], 0);
2148 expect(ok, r8 [0x2c], 4);
2149 expect(ok, r8 [0x2d], 0);
2150 expect(ok, r16[0x2e/2], 0); // no exception_index
2151 expect(ok, m_icnt[4], 1);
2152 expect(ok, s_icnt[4], 0);
2153 write16(&r16[0x02/2], 0xaaaa); // INTS+unused_bits
2155 expect(ok, r16[0x02/2], 2);
2158 expect(ok, r16[0x02/2], 0);
2159 expect(ok, r8 [0x2c], 4);
2160 expect(ok, r8 [0x2d], 4);
2161 expect(ok, r16[0x2e/2], 0); // no exception_index
2162 write8(r, 0x00); // FM=0
2164 expect(ok, m_icnt[4], 1);
2165 expect(ok, s_icnt[4], 1);
2166 for (i = 0; i < 8; i++) {
2169 expect(ok, m_icnt[i], 0);
2170 expect(ok, s_icnt[i], 0);
2175 static int t_32x_reg_w(void)
2177 u32 *r32 = (u32 *)0xa15100;
2178 u16 *r16 = (u16 *)r32, old;
2185 expect(ok, r32[0x08/4], 0xfffffe);
2186 expect(ok, r32[0x0c/4], 0xffffff);
2187 expect(ok, r16[0x10/2], 0xfffc);
2189 r32[0x08/4] = r32[0x0c/4] = 0;
2192 x32_cmd(CMD_WRITE16, 0x20004006, ~old, 0);
2193 expect(ok, r16[0x06/2], old);
2197 // prepare for reset btn press tests
2198 static int t_32x_reset_prep(void)
2200 u32 *fbl = (u32 *)0x840000;
2201 u32 *fbl_icnt = fbl + IRQ_CNT_FB_BASE / 4;
2202 u32 *r32 = (u32 *)0xa15100;
2203 u16 *r16 = (u16 *)r32;
2207 expect(ok, r16[0x00/2], 0x83);
2208 write8(r8, 0x00); // FM=0
2211 expect(ok, r8[0x8b] & ~2, 0);
2212 for (i = 0; i < 8; i++)
2213 write32(&fbl_icnt[i], 0x01000100);
2214 x32_cmd(CMD_WRITE8, 0x20004001, 0x02, 0); // unmask cmd
2215 x32_cmd(CMD_WRITE8, 0x20004001, 0x02, 1); // unmask slave
2216 x32_cmd(CMD_SETSR, 0xf0, 0, 1); // mask slave irqs (on the cpu)
2218 write8(r8, 0x00); // FM=0
2219 expect(ok, r32[0x2c/4], 0);
2221 for (i = 0; i < 8; i++)
2222 expect(ok, fbl_icnt[i], 0x01000100);
2224 r16[0x04/2] = 0xffff;
2225 r32[0x08/4] = 0x5a5a5a08;
2226 r32[0x0c/4] = 0x5a5a5a0c;
2227 r16[0x10/2] = 0x5a10;
2228 r32[0x20/4] = 0x00005a20; // no x32_cmd
2229 r32[0x24/4] = 0x5a5a5a24;
2230 r32[0x28/4] = 0x5a5a5a28;
2231 r32[0x2c/4] = 0x5a5a5a2c;
2232 if (!(r16[0x00/2] & 0x8000)) {
2234 r16[0x8a/2] = 0x0001;
2236 for (i = 0; i < 220/2; i++)
2237 write32(&fbl[i], 0);
2239 r16[0x82/2] = 0xffff;
2240 r16[0x84/2] = 0xffff;
2241 r16[0x86/2] = 0xffff;
2242 r16[0x8a/2] = 0x0000;
2243 r16[0x8c/2] = 0xffff;
2244 r16[0x8e/2] = 0xffff;
2255 static const struct {
2260 // this must be first to disable the 32x and restore the 68k vector table
2261 { T_32, t_32x_reset_btn, "32x resetbtn" },
2263 { T_MD, t_dma_zero_wrap, "dma zero len + wrap" },
2264 { T_MD, t_dma_zero_fill, "dma zero len + fill" },
2265 { T_MD, t_dma_ram_wrap, "dma ram wrap" },
2266 { T_MD, t_dma_multi, "dma multi" },
2267 { T_MD, t_dma_cram_wrap, "dma cram wrap" },
2268 { T_MD, t_dma_vsram_wrap, "dma vsram wrap" },
2269 { T_MD, t_dma_and_data, "dma and data" },
2270 { T_MD, t_dma_short_cmd, "dma short cmd" },
2271 { T_MD, t_dma_fill3_odd, "dma fill3 odd" },
2272 { T_MD, t_dma_fill3_even, "dma fill3 even" },
2273 { T_MD, t_dma_fill3_vsram, "dma fill3 vsram" },
2274 { T_MD, t_dma_fill_dis, "dma fill disabled" },
2275 { T_MD, t_dma_fill_src, "dma fill src incr" },
2276 { T_MD, t_dma_128k, "dma 128k mode" },
2277 { T_MD, t_vdp_128k_b16, "vdp 128k addr bit16" },
2278 // { t_vdp_128k_b16_inc, "vdp 128k bit16 inc" }, // mystery
2279 { T_MD, t_vdp_reg_cmd, "vdp reg w cmd reset" },
2280 { T_MD, t_vdp_sr_vb, "vdp status reg vb" },
2281 { T_MD, t_z80mem_long_mirror, "z80 ram long mirror" },
2282 { T_MD, t_z80mem_noreq_w, "z80 ram noreq write" },
2283 { T_MD, t_z80mem_vdp_r, "z80 vdp read" },
2284 // { t_z80mem_vdp_w, "z80 vdp write" }, // hang
2285 { T_MD, t_tim_loop, "time loop" },
2286 { T_MD, t_tim_z80_loop, "time z80 loop" },
2287 { T_MD, t_tim_z80_ram, "time z80 ram" },
2288 { T_MD, t_tim_z80_ym, "time z80 ym2612" },
2289 { T_MD, t_tim_z80_vdp, "time z80 vdp" },
2290 { T_MD, t_tim_z80_bank_rom, "time z80 bank rom" },
2291 { T_MD, t_tim_vcnt, "time V counter" },
2292 { T_MD, t_tim_vcnt_loops, "time vcnt loops" },
2293 { T_MD, t_tim_hblank_h40, "time hblank h40" },
2294 { T_MD, t_tim_hblank_h32, "time hblank h32" },
2295 { T_MD, t_tim_vdp_as_vram_w, "time vdp vram w" },
2296 { T_MD, t_tim_vdp_as_cram_w, "time vdp cram w" },
2297 { T_MD, t_tim_ym_timera_z80, "time timer a z80" },
2298 { T_MD, t_tim_ym_timerb_z80, "time timer b z80" },
2299 { T_MD, t_tim_ym_timerb_stop, "timer b stop" },
2300 { T_MD, t_tim_ym_timer_ab_sync,"timer ab sync" },
2301 { T_MD, t_irq_hint, "irq4 / line" },
2302 { T_MD, t_irq_both_cpu_unmask, "irq both umask" },
2303 { T_MD, t_irq_ack_v_h, "irq ack v-h" },
2304 { T_MD, t_irq_ack_v_h_2, "irq ack v-h 2" },
2305 { T_MD, t_irq_ack_h_v, "irq ack h-v" },
2306 { T_MD, t_irq_ack_h_v_2, "irq ack h-v 2" },
2307 { T_MD, t_irq_f_flag_h40, "irq f flag h40" },
2308 { T_MD, t_irq_f_flag_h32, "irq f flag h32" },
2310 // the first one enables 32X, so must be kept
2311 // all tests assume RV=1 FM=0
2312 { T_32, t_32x_init, "32x init" },
2313 { T_32, t_32x_echo, "32x echo" },
2314 { T_32, t_32x_sh_defaults, "32x sh def" },
2315 { T_32, t_32x_md_bios, "32x md bios" },
2316 { T_32, t_32x_md_rom, "32x md rom" },
2317 { T_32, t_32x_md_fb, "32x md fb" },
2318 { T_32, t_32x_sh_fb, "32x sh fb" },
2319 { T_32, t_32x_irq, "32x irq" },
2320 { T_32, t_32x_reg_w, "32x reg w" },
2321 { T_32, t_32x_reset_prep, "32x rstprep" }, // must be last 32x
2324 static void setup_z80(void)
2326 u8 *zram = (u8 *)0xa00000;
2330 write16(0xa11100, 0x100);
2331 write16(0xa11200, 0x100);
2333 while (read16(0xa11100) & 0x100)
2336 // load the default test program, clear it's data
2337 len = z80_test_end - z80_test;
2338 for (i = 0; i < len; i++)
2339 write8(&zram[i], z80_test[i]);
2340 for (i = 0x1000; i < 0x1007; i++)
2341 write8(&zram[i], 0);
2344 write16(0xa11200, 0x000);
2345 write16(0xa11100, 0x000);
2347 write16(0xa11200, 0x100);
2349 burn10(50 * 15 / 7 / 10); // see z80_test.s80
2351 // take back the bus
2352 write16(0xa11100, 0x100);
2353 while (read16(0xa11100) & 0x100)
2357 static unused int hexinc(char *c)
2371 void (*px32x_switch_rv)(short rv);
2372 short (*pget_input)(void) = get_input;
2384 write8(0xa10009, 0x40);
2387 while (read16(VDP_CTRL_PORT) & 2)
2390 VDP_setReg(VDP_MODE1, VDP_MODE1_PS);
2391 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA);
2392 VDP_setReg(VDP_MODE3, 0x00);
2393 VDP_setReg(VDP_MODE4, 0x81);
2394 VDP_setReg(VDP_NT_SCROLLA, APLANE >> 10);
2395 VDP_setReg(VDP_NT_SCROLLB, BPLANE >> 13);
2396 VDP_setReg(VDP_SAT_BASE, SLIST >> 9);
2397 VDP_setReg(VDP_HSCROLL, HSCRL >> 10);
2398 VDP_setReg(VDP_AUTOINC, 2);
2399 VDP_setReg(VDP_SCROLLSZ, 0x01);
2400 VDP_setReg(VDP_BACKDROP, 0);
2403 t_dma_zero_wrap_early();
2404 t_dma_zero_fill_early();
2407 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(0));
2408 for (i = 0; i < 32 / 4; i++)
2409 write32(VDP_DATA_PORT, 0);
2411 /* clear name tables */
2412 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(APLANE));
2413 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
2414 write32(VDP_DATA_PORT, 0);
2416 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(BPLANE));
2417 for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
2418 write32(VDP_DATA_PORT, 0);
2420 /* SAT, h. scroll */
2421 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(SLIST));
2422 write32(VDP_DATA_PORT, 0);
2424 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(HSCRL));
2425 write32(VDP_DATA_PORT, 0);
2427 /* scroll plane vscroll */
2428 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
2429 write32(VDP_DATA_PORT, 0);
2433 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(TILE_FONT_BASE));
2434 for (i = 0; i < FONT_LEN * 32 / 4; i++)
2435 write32(VDP_DATA_PORT, font_base[i]);
2438 setup_default_palette();
2440 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
2442 have_32x = read32(0xa130ec) == MKLONG('M','A','R','S');
2443 en_32x = have_32x && (read16(0xa15100) & 1);
2444 v8 = read8(0xa10001);
2445 printf("MD version: %02x %s %s %s%s\n", v8,
2446 (v8 & 0x80) ? "world" : "jap",
2447 (v8 & 0x40) ? "pal" : "ntsc",
2448 have_32x ? "32X" : "",
2450 printf("reset hvc %04x->%04x\n", read16(-4), read16(-2));
2453 extern u32 sh2_test[];
2454 if (sh2_test[0] != read32(0x3e0) || sh2_test[0x200/4] != read32(0x3e4))
2455 printf("bad 0x3c0 tab\n");
2457 for (i = 0; i < ARRAY_SIZE(g_tests); i++) {
2458 // print test number if we haven't scrolled away
2459 if (printf_ypos < CSCREEN_H) {
2460 int old_ypos = printf_ypos;
2462 printf("%02d/%02d", i, ARRAY_SIZE(g_tests));
2463 printf_ypos = old_ypos;
2466 if ((g_tests[i].type & T_32) && !have_32x) {
2470 ret = g_tests[i].test();
2471 if (ret == R_SKIP) {
2477 printf("failed %d: %s\n", i, g_tests[i].name);
2485 printf("%d/%d passed, %d skipped.\n",
2486 passed, ARRAY_SIZE(g_tests), skipped);
2491 if (have_32x && (read16(0xa15100) & 1)) {
2492 u8 *p = (u8 *)0xff0040;
2493 u32 len = x32x_switch_rv_end - x32x_switch_rv;
2494 px32x_switch_rv = (void *)p; p += len;
2495 memcpy_(px32x_switch_rv, x32x_switch_rv, len);
2497 len = get_input_end - get_input_s;
2498 pget_input = (void *)p; p += len;
2499 memcpy_(pget_input, get_input_s, len);
2501 // prepare for reset - run from 880xxx as the reset vector points there
2502 // todo: broken printf
2505 for (i = 0; i < 60*60 && !(pget_input() & BTNM_A); i++) {
2506 while (read16(VDP_CTRL_PORT) & SR_VB)
2507 write16(-4, read16(VDP_HV_COUNTER)); /* blanking */
2508 while (!(read16(VDP_CTRL_PORT) & SR_VB))
2509 write16(-4, read16(VDP_HV_COUNTER)); /* not blanking */;
2512 // blank due to my lame tv being burn-in prone
2513 VDP_setReg(VDP_MODE2, VDP_MODE2_MD);
2515 while (!(pget_input() & BTNM_A))
2516 write16(-4, read16(VDP_HV_COUNTER));
2517 VDP_setReg(VDP_MODE2, VDP_MODE2_MD | VDP_MODE2_DMA | VDP_MODE2_DISP);
2521 char c[3] = { '0', '0', '0' };
2522 short hscroll = 0, vscroll = 0;
2523 short hsz = 1, vsz = 0;
2526 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(APLANE));
2529 for (i = 0, c[0] = 'a'; i < 8 * 1024 / 2; i++) {
2530 write16(VDP_DATA_PORT, (u16)c[0] - 32 + TILE_FONT_BASE / 32);
2532 if (c[0] == 'z' + 1)
2536 for (i = 0; i < 8 * 1024 / 2 / 4; i++) {
2537 write16(VDP_DATA_PORT, (u16)'.' - 32 + TILE_FONT_BASE / 32);
2538 write16(VDP_DATA_PORT, (u16)c[2] - 32 + TILE_FONT_BASE / 32);
2539 write16(VDP_DATA_PORT, (u16)c[1] - 32 + TILE_FONT_BASE / 32);
2540 write16(VDP_DATA_PORT, (u16)c[0] - 32 + TILE_FONT_BASE / 32);
2546 while (pget_input() & BTNM_A)
2551 int b = pget_input();
2554 hscroll = 1, vscroll = -1;
2557 } while (pget_input() & BTNM_C);
2560 if (b & (BTNM_L | BTNM_R | BTNM_C)) {
2561 hscroll += (b & BTNM_L) ? 1 : -1;
2562 write32(VDP_CTRL_PORT, CTL_WRITE_VRAM(HSCRL));
2563 write16(VDP_DATA_PORT, hscroll);
2565 if (b & (BTNM_U | BTNM_D | BTNM_C)) {
2566 vscroll += (b & BTNM_U) ? -1 : 1;
2567 write32(VDP_CTRL_PORT, CTL_WRITE_VSRAM(0));
2569 int end = (int)vscroll + 21;
2570 for (i = vscroll; i < end; i++)
2571 write32(VDP_DATA_PORT, i << 17);
2572 VDP_setReg(VDP_MODE3, 0x04);
2575 write16(VDP_DATA_PORT, vscroll);
2576 VDP_setReg(VDP_MODE3, 0x00);
2580 hsz = (hsz + 1) & 3;
2583 } while (pget_input() & BTNM_A);
2586 vsz = (vsz + 1) & 3;
2589 } while (pget_input() & BTNM_B);
2591 VDP_setReg(VDP_SCROLLSZ, (vsz << 4) | hsz);
2596 printf(" %d %d ", hsz, vsz);
2608 // vim:ts=4:sw=4:expandtab