/* Phase 3 player, master SH-2: decode the C1V2 stream from the cartridge and show it as * direct colour at 25 fps on a 50 Hz (PAL) display, each frame for two fields. * * Per frame, both SH-2s work on one half each (MB rows 0-5 here, 6-11 on the slave, * s_main.c): decode the half into the planes not holding the reference, then convert the * half's MBs that changed in this frame or the one before (the back buffer holds frame * N-2) into the back frame buffer. Then wait for the shown frame's second field to start * and request the swap during display, so it lands at the next VBlank (a write in VBlank * would swap at once). * * Each CPU purges its cache at the start of a frame: the other one wrote half of the * reference planes in the frame before. * * COMM12 is the 68000's VBlank count (md_main.c). COMM6 is the slave's job word * (1: go, 0: done); the job itself is c1_job. COMM2 counts frames shown (read by the * harness). COMM0 is the 68000's command word and stays the skeleton's. COMM8, COMM10 and * COMM4 carry the last frame's profile in FRT ticks of 128 cycles: master decode, master * convert, wait for the slave. */ #include "mars.h" #include "c1dec.h" #include "c1job.h" extern const uint8_t c1_stream[]; uint8_t c1_plane_mem[2][C1_ROWS * C1_W * 3 / 2] __attribute__((aligned(16))); c1_planes c1_p[2]; c1_job_t c1_job; static c1_fast_t c1_fast_m; uint8_t c1_changed[C1_NMB], c1_prev[C1_NMB], c1_mask[C1_NMB]; static void init_planes(void) { for (int k = 0; k < 2; k++) { c1_p[k].y = c1_plane_mem[k]; c1_p[k].cb = c1_plane_mem[k] + C1_ROWS * C1_W; c1_p[k].cr = c1_p[k].cb + C1_ROWS * C1_W / 4; } uint8_t *y = c1_p[0].y; for (int i = 0; i < C1_ROWS * C1_W; i++) y[i] = 16; uint8_t *c = c1_p[0].cb; for (int i = 0; i < C1_ROWS * C1_W / 2; i++) c[i] = 128; } /* The on-chip free-running timer at clock/128 (TCR CKS = 2): 178 kHz on PAL, so one * tick is 128 SH-2 cycles. FRCH is read first (it latches FRCL). */ static void frt_init(void) { SH2_FRT_TCR = 2; } static inline uint16_t frt(void) { uint16_t h = SH2_FRT_FRCH; return (uint16_t)(h << 8 | SH2_FRT_FRCL); } static void swap_now(void) { uint16_t shown = MARS_VDP_FBCTL & MARS_VDP_FS; MARS_VDP_FBCTL = shown ^ 1; while ((MARS_VDP_FBCTL & MARS_VDP_FS) == shown) c1_pause(); } int m_main(void) { Hw32xInit(MARS_VDP_MODE_32K, 0); /* Our line table and black rows in both buffers. */ for (int k = 0; k < 2; k++) { volatile uint16_t *fb = &MARS_FRAMEBUFFER; c1_line_table(fb, C1_FB_BASE); for (int i = 0; i < C1_ROWS * C1_FBW; i++) fb[C1_FB_BASE + i] = 0; /* borders stay black */ swap_now(); } init_planes(); frt_init(); c1_fast_init(&c1_fast_m); c1_fast_m.clock = frt; MARS_SYS_COMM6 = 0; MARS_SYS_COMM2 = 0; for (int k = 0; k < C1_NMB; k++) c1_prev[k] = 1; /* both buffers start black */ #ifdef C1_BENCH { extern void c1_bench(c1_fast_t *, const c1_planes *, c1_planes *, volatile uint16_t *, uint16_t (*)(void), uint16_t *); uint16_t r[7]; c1_bench(&c1_fast_m, &c1_p[0], &c1_p[1], &MARS_FRAMEBUFFER + C1_FB_BASE, frt, r); /* Show each result for 4 fields: COMM2 = 0xB000 + k, COMM8 = ticks. */ for (int k = 0; k < 7; k++) { MARS_SYS_COMM8 = r[k]; MARS_SYS_COMM2 = (uint16_t)(0xB000 + k); uint32_t t0 = MARS_SYS_COMM12; while (MARS_SYS_COMM12 < t0 + 4) c1_pause(); } MARS_SYS_COMM2 = 0; init_planes(); } #endif const uint8_t *s = c1_stream; uint32_t frames = (uint32_t)s[8] << 24 | (uint32_t)s[9] << 16 | (uint32_t)s[10] << 8 | s[11]; s += 12; int ref = 0, split_prev = -1; uint8_t *prev = C1_THROUGH(c1_prev); /* both CPUs write it: read it cache-through */ uint32_t shown_at = MARS_SYS_COMM12; for (uint32_t i = 0; i < frames; i++) { unsigned len = (unsigned)s[0] << 8 | s[1]; c1_job.payload = s + 2; c1_job.ref = ref; MARS_SYS_COMM6 = 1; int split = c1_split(s + 2); uint16_t t0 = frt(); /* Coherency: only what the slave wrote into the reference last frame, within reach. */ if (split_prev < 0) CacheControl(SH2_CCTL_CP | SH2_CCTL_CE); else c1_purge_band(&c1_p[ref], split_prev, C1_NMB, 0, split); c1_decode_half(&c1_fast_m, s + 2, 0, &c1_p[ref], &c1_p[ref ^ 1], prev, c1_changed); uint16_t t1 = frt(); for (int k = 0; k < split; k++) { c1_mask[k] = c1_changed[k] | prev[k]; prev[k] = c1_changed[k]; } split_prev = split; volatile uint16_t *fb = &MARS_FRAMEBUFFER; c1_to_rgb555(&c1_fast_m, &c1_p[ref ^ 1], fb + C1_FB_BASE, c1_mask, 0, split); uint16_t t2 = frt(); while (MARS_SYS_COMM6 != 0) c1_pause(); uint16_t t3 = frt(); #ifdef C1_PROF /* Decode breakdown instead: COMM8 skip copies, COMM10 MC copies, COMM4 IDCT + add * (ticks); the rest of decode is bit parsing and loop overhead. */ MARS_SYS_COMM8 = (uint16_t)c1_fast_m.prof[0]; MARS_SYS_COMM10 = (uint16_t)c1_fast_m.prof[1]; MARS_SYS_COMM4 = (uint16_t)c1_fast_m.prof[2]; c1_fast_m.prof[0] = c1_fast_m.prof[1] = c1_fast_m.prof[2] = 0; (void)t1; (void)t2; (void)t3; #else MARS_SYS_COMM8 = (uint16_t)(t1 - t0); MARS_SYS_COMM10 = (uint16_t)(t2 - t1); MARS_SYS_COMM4 = (uint16_t)(t3 - t2); #endif ref ^= 1; s += 2 + len; /* The shown frame's second field has started, and we are out of VBlank. */ while (MARS_SYS_COMM12 < shown_at + 1) c1_pause(); while (MARS_VDP_FBCTL & MARS_VDP_VBLK) c1_pause(); swap_now(); shown_at = MARS_SYS_COMM12; MARS_SYS_COMM2 = (uint16_t)(i + 1); } for (;;); return 0; }