/* This file is included into vb.c and cannot be compiled on its own. */ #ifdef VBAPI /********************************** Macros ***********************************/ /* Compute how many clocks are in some number of milliseconds */ #define vipClocksMs(x) x * 20000 /* Read a world line parameter value */ #define vipParam(x) busReadBuffer(&sim->vip.ram[x], VB_U16) /******************************** Lookup Data ********************************/ /* BG map arrangement by world background dimensions */ static const uint8_t BG_TEMPLATES[][64] = { { 0 }, /* 1x1 */ { 0, 1 }, /* 1x2 */ { 0, 1, 2, 3 }, /* 1x4 */ { 0, 1, 2, 3, 4, 5, 6, 7 }, /* 1x8 */ { 0, 1 }, /* 2x1 */ { 0, 1, 2, 3 }, /* 2x2 */ { 0, 1, 2, 3, 4, 5, 6, 7 }, /* 2x4 */ { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 }, /* 2x8 */ { 0, 1, 2, 3 }, /* 4x1 */ { 0, 1, 2, 3, 4, 5, 6, 7 }, /* 4x2 */ { 0, 1, 0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7 }, /* 4x4 */ { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, /* 4x8 */ 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7 }, { 0, 1, 2, 3, 4, 5, 6, 7 }, /* 8x1 */ { 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 7 }, /* 8x2 */ { 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, /* 8x4 */ 4, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 6, 7 }, { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, /* 8x8 */ 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7 } }; /***************************** Module Functions ******************************/ /* Retrieve a pointer to character data in host memory */ static uint8_t* vipCharacter(VB *sim, uint32_t c) { return &sim->vip.ram[0x06000 | (c << 6 & 0x18000) | (c << 4 & 0x01FF0)]; } /* Read a palette */ static int32_t vipReadPalette(uint8_t *entries) { return entries[3] << 6 | entries[2] << 4 | entries[1] << 2; } /* Raise an interrupt request */ static void vipThrow(VB *sim, uint16_t cause) { if (!(sim->vip.intenb & cause)) return; sim->vip.intpnd |= cause; sim->cpu.irq |= 0x0010; } /* Write a palette */ static void vipWritePalette(uint8_t *entries, int32_t mask, int32_t value) { if (mask & 0x00FF) return; entries[3] = value >> 6 & 3; entries[2] = value >> 4 & 3; entries[1] = value >> 2 & 3; } /* Read a typed value from an I/O register */ static int32_t vipReadIO(VB *sim, uint32_t address, int type) { int32_t mask = 0; /* Byte access mask */ int32_t value; /* Return value */ /* Adjustments by type */ switch (type) { case VB_S32: /* Word */ return vipReadIO(sim, address, VB_U16) | (uint32_t) vipReadIO(sim, address + 2, VB_U16) << 16; case VB_S8: /* Byte */ case VB_U8: mask = 0x00FF << ((address & 1) << 3); break; case VB_S16: /* Halfword */ case VB_U16: mask = 0xFFFF; } /* Access by register */ switch (address >> 1) { case 0x5F800>>1: /* INTPND */ value = sim->vip.intpnd; break; case 0x5F802>>1: /* INTENB */ value = sim->vip.intenb; break; case 0x5F820>>1: /* DPSTTS */ value = (int32_t) sim->vip.dp.lock << 10 | (int32_t) sim->vip.dp.synce << 9 | (int32_t) sim->vip.dp.re << 8 | (int32_t) sim->vip.dp.fclk << 7 | (int32_t) sim->vip.dp.scanrdy << 6 | (int32_t) sim->vip.dp.r1bsy << 5 | (int32_t) sim->vip.dp.l1bsy << 4 | (int32_t) sim->vip.dp.r0bsy << 3 | (int32_t) sim->vip.dp.l0bsy << 2 | (int32_t) sim->vip.dp.disp << 1 ; break; case 0x5F824>>1: /* BRTA */ value = sim->vip.brtRest[0]; break; case 0x5F826>>1: /* BRTB */ value = sim->vip.brtRest[1]; break; case 0x5F828>>1: /* BRTC */ value = sim->vip.brtRest[2]; break; case 0x5F82A>>1: /* REST */ value = sim->vip.brtRest[3]; break; case 0x5F82E>>1: /* FRMCYC */ value = sim->vip.frmcyc; break; case 0x5F830>>1: /* CTA */ value = (int32_t) sim->vip.cta.cta_r << 8 | sim->vip.cta.cta_l; break; case 0x5F840>>1: /* XPSTTS */ value = (int32_t) !!sim->vip.xp.sbout << 15 | (int32_t) sim->vip.xp.sbcount << 8 | (int32_t) sim->vip.xp.overtime << 4 | (int32_t) sim->vip.xp.f1bsy << 3 | (int32_t) sim->vip.xp.f0bsy << 2 | (int32_t) sim->vip.xp.xpen << 1 ; break; case 0x5F844>>1: /* VER */ value = 2; break; case 0x5F848>>1: /* SPT0 */ value = sim->vip.spt[0]; break; case 0x5F84A>>1: /* SPT1 */ value = sim->vip.spt[1]; break; case 0x5F84C>>1: /* SPT2 */ value = sim->vip.spt[2]; break; case 0x5F84E>>1: /* SPT3 */ value = sim->vip.spt[3]; break; case 0x5F860>>1: /* GPLT0 */ value = vipReadPalette(sim->vip.gplt[0]); break; case 0x5F862>>1: /* GPLT1 */ value = vipReadPalette(sim->vip.gplt[1]); break; case 0x5F864>>1: /* GPLT2 */ value = vipReadPalette(sim->vip.gplt[2]); break; case 0x5F866>>1: /* GPLT3 */ value = vipReadPalette(sim->vip.gplt[3]); break; case 0x5F868>>1: /* JPLT0 */ value = vipReadPalette(sim->vip.jplt[0]); break; case 0x5F86A>>1: /* JPLT1 */ value = vipReadPalette(sim->vip.jplt[1]); break; case 0x5F86C>>1: /* JPLT2 */ value = vipReadPalette(sim->vip.jplt[2]); break; case 0x5F86E>>1: /* JPLT3 */ value = vipReadPalette(sim->vip.jplt[3]); break; case 0x5F870>>1: /* BKCOL */ value = sim->vip.bkcol; break; /* Unmapped */ default: value = 0; } /* Select byte bits as necessary */ return mask == 0x00FF ? value & 0x00FF : mask == 0xFF00 ? value >> 8 : value; } /* Write a typed value to an I/O register */ static void vipWriteIO( VB *sim, uint32_t address, int type, int32_t value, int debug) { int32_t mask; /* Byte access mask */ /* Adjustments by type */ switch (type) { case VB_S32: /* Word */ vipWriteIO(sim, address , VB_U16, value , debug); vipWriteIO(sim, address + 2, VB_U16, value >> 16, debug); return; case VB_S8: /* Byte */ case VB_U8: /* Select which half of the register to access */ if (debug) { mask = (address & 1) << 3; value = (value & 0x00FF) << mask; mask = 0xFF00 >> mask; break; } /* Convert to a halfword access */ if ((address & 1) != 0) value = (value & 0x00FF) << 8; /* Fallthrough */ default: /* Halfword */ mask = 0x0000; } /* Access by register */ switch (address >> 1) { case 0x5F802>>1: /* INTENB */ sim->vip.intenb = (sim->vip.intenb & mask) | (value & 0xE01F); break; case 0x5F804>>1: /* INTCLR */ sim->vip.intpnd &= ~value; if ((sim->vip.intpnd & sim->vip.intenb) == 0) sim->cpu.irq &= ~0x0010; break; case 0x5F822>>1: /* DPCTRL */ if ((mask & 0xFF00) == 0) { sim->vip.dp.lock = value >> 10 & 1; sim->vip.dp.synce = value >> 9 & 1; sim->vip.dp.re = value >> 8 & 1; } if ((mask & 0x00FF) == 0) { sim->vip.dp.disp = value >> 1 & 1; /* DPRST */ if (value & 1) { sim->vip.intenb &= ~0x801F; sim->vip.intpnd &= ~0x801F; if ((sim->vip.intpnd & sim->vip.intenb) == 0) sim->cpu.irq &= ~0x0010; /* TODO: Research exact operation */ } } break; case 0x5F824>>1: /* BRTA */ sim->vip.brtRest[0] = (sim->vip.brtRest[0] & mask) | value; break; case 0x5F826>>1: /* BRTB */ sim->vip.brtRest[1] = (sim->vip.brtRest[1] & mask) | value; break; case 0x5F828>>1: /* BRTC */ sim->vip.brtRest[2] = (sim->vip.brtRest[2] & mask) | value; break; case 0x5F82A>>1: /* REST */ sim->vip.brtRest[3] = (sim->vip.brtRest[3] & mask) | value; break; case 0x5F82E>>1: /* FRMCYC */ sim->vip.frmcyc = (sim->vip.frmcyc & mask) | (value & 15); break; case 0x5F830>>1: /* CTA */ if ((mask & 0xFF00) == 0) sim->vip.cta.cta_r = value >> 8; if ((mask & 0x00FF) == 0) sim->vip.cta.cta_l = value; break; case 0x5F842>>1: /* XPCTRL */ if ((mask & 0xFF00) == 0) sim->vip.xp.sbcmp = value >> 8 & 31; if ((mask & 0x00FF) == 0) { sim->vip.xp.xpen = value >> 1 & 1; /* XPRST */ if (value & 1) { sim->vip.intenb &= ~0xE000; sim->vip.intpnd &= ~0xE000; if ((sim->vip.intpnd & sim->vip.intenb) == 0) sim->cpu.irq &= ~0x0010; /* TODO: Research exact operation */ } } break; case 0x5F848>>1: /* SPT0 */ sim->vip.spt[0] = (sim->vip.spt[0]&mask) | (value&0x03FF&~mask); break; case 0x5F84A>>1: /* SPT1 */ sim->vip.spt[1] = (sim->vip.spt[1]&mask) | (value&0x03FF&~mask); break; case 0x5F84C>>1: /* SPT2 */ sim->vip.spt[2] = (sim->vip.spt[2]&mask) | (value&0x03FF&~mask); break; case 0x5F84E>>1: /* SPT3 */ sim->vip.spt[3] = (sim->vip.spt[3]&mask) | (value&0x03FF&~mask); break; case 0x5F860>>1: /* GPLT0 */ vipWritePalette(sim->vip.gplt[0], mask, value); break; case 0x5F862>>1: /* GPLT1 */ vipWritePalette(sim->vip.gplt[1], mask, value); break; case 0x5F864>>1: /* GPLT2 */ vipWritePalette(sim->vip.gplt[2], mask, value); break; case 0x5F866>>1: /* GPLT3 */ vipWritePalette(sim->vip.gplt[3], mask, value); break; case 0x5F868>>1: /* JPLT0 */ vipWritePalette(sim->vip.jplt[0], mask, value); break; case 0x5F86A>>1: /* JPLT1 */ vipWritePalette(sim->vip.jplt[1], mask, value); break; case 0x5F86C>>1: /* JPLT2 */ vipWritePalette(sim->vip.jplt[2], mask, value); break; case 0x5F86E>>1: /* JPLT3 */ vipWritePalette(sim->vip.jplt[3], mask, value); break; case 0x5F870>>1: /* BKCOL */ sim->vip.bkcol = (sim->vip.bkcol & mask) | (value & 3 * ~mask); break; } } /***************************** Callback Handlers *****************************/ /* Prepare to handle an exception */ #ifndef VB_DIRECT_FRAME #define VB_ON_FRAME sim->onFrame #else extern int VB_DIRECT_FRAME(VB *); #define VB_ON_FRAME VB_DIRECT_FRAME #endif static int vipOnFrame(VB *sim) { return sim->onFrame != NULL && VB_ON_FRAME(sim); } #undef VB_ON_FRAME /***************************** Display Processor *****************************/ /* Precompute brightness values for image output */ static void vipComputeBrightness(VB *sim) { int32_t brt[4]; /* Working output */ int32_t repeat; /* Multiplier from column table */ int x; /* Iterator */ /* Compute brightness from hardware state */ repeat = (int32_t) sim->vip.ram[sim->vip.dp.cta + 1] + 1; brt[1] = repeat * sim->vip.brtRest[0]; brt[2] = repeat * sim->vip.brtRest[1]; brt[3] = repeat * sim->vip.brtRest[2] + brt[2] + brt[1]; /* Scale brightness values to 0..255 */ for (x = 1; x < 4; x++) sim->vip.dp.brt[x] = ((brt[x] > 127 ? 127 : brt[x]) * 510 + 127) / 254; } /* Transfer one column of frame buffer pixels to output */ static void vipTransferColumn(VB *sim, int32_t eye) { int32_t bits; /* Pixel bits from frame buffer */ uint8_t *dest; /* Host frame image output */ uint8_t *src; /* Simulation frame buffer input */ int y, z; /* Iterators */ /* Select destination address in host memory */ dest = &sim->vip.output [sim->vip.dp.buffer][eye][sim->vip.dp.column]; /* Output is disabled */ if ((sim->vip.dp.disp & sim->vip.dp.synce) == 0) { for (z = 0; z < 0x15000; z += 4) { busWriteBuffer(dest, VB_S32, 0); dest += 4; } return; } /* Select source address in host memory */ src = &sim->vip.ram[ eye << 16 | sim->vip.dp.buffer << 15 | sim->vip.dp.column << 6 ]; /* Transfer all rows as words */ for (y = 0; y < 224; y += 16, src += 4) { bits = busReadBuffer(src, VB_S32); for (z = 0; z < 16; bits >>= 2, z++, dest += 384) *dest = sim->vip.dp.brt[bits & 3]; } } /* Process sub-component */ static int vipEmulateDisplay(VB *sim, uint32_t clocks) { /* Process all clocks */ for (;;) { /* The next event is after the time remaining */ if (sim->vip.dp.clocks > clocks) { sim->vip.dp.clocks -= clocks; sim->vip.dp.until -= clocks; return 0; } /* Advance forward the component's number of clocks */ clocks -= sim->vip.dp.clocks; sim->vip.dp.until -= sim->vip.dp.clocks; sim->vip.dp.clocks = 0; /* Processing by operation phase */ switch (sim->vip.dp.step) { case 0: /* 0ms - FCLK rising edge */ sim->vip.dp.clocks = vipClocksMs(3); sim->vip.dp.fclk = 1; sim->vip.dp.step = 1; sim->vip.dp.until = vipClocksMs(8); vipThrow(sim, 0x0010); /* FRAMESTART */ /* Game frame */ if (sim->vip.xp.xpen) { if (sim->vip.xp.frame >= sim->vip.frmcyc) { sim->vip.xp.frame = 0; /* Initiate drawing procedure */ if (sim->vip.xp.step == 0) { sim->vip.dp.buffer ^= 1; sim->vip.xp.overtime = 0; sim->vip.xp.step = 1; vipThrow(sim, 0x0008); /* GAMESTART */ } /* Drawing procedure has run into overtime */ else { sim->vip.xp.overtime = 1; vipThrow(sim, 0x8000); /* TIMEERR */ } } else sim->vip.xp.frame++; } break; /* 0ms-3ms - Idle */ case 1: /* 3ms - L*BSY rising edge */ if (sim->vip.dp.buffer == 0) sim->vip.dp.l0bsy = 1; else sim->vip.dp.l1bsy = 1; sim->vip.dp.column = 0; sim->vip.dp.cta = 0x3DC00 | (uint32_t)sim->vip.cta.cta_l<<1; sim->vip.dp.step = 2; vipComputeBrightness(sim); /* Fallthrough */ case 2: /* 3ms-8ms - Display left frame buffer */ if ((sim->vip.dp.column & 3) == 0) vipComputeBrightness(sim); vipTransferColumn(sim, 0); sim->vip.dp.clocks = 260 + (0xA94 >> sim->vip.dp.column % 12 & 1); sim->vip.dp.column++; if ((sim->vip.dp.column & 3) == 0) sim->vip.dp.cta -= 2; if (sim->vip.dp.column == 384) sim->vip.dp.step = 3; break; case 3: /* 8ms - L*BSY falling edge */ if (sim->vip.dp.buffer == 0) sim->vip.dp.l0bsy = 0; else sim->vip.dp.l1bsy = 0; sim->vip.dp.clocks = vipClocksMs(2); sim->vip.dp.step = 4; sim->vip.dp.until = vipClocksMs(10); vipThrow(sim, 0x0002); /* LFBEND */ break; /* 8ms-10ms - Idle */ case 4: /* 10ms - FCLK falling edge */ sim->vip.dp.clocks = vipClocksMs(3); sim->vip.dp.fclk = 0; sim->vip.dp.step = 5; break; /* 10ms-13ms - Idle */ case 5: /* 13ms - R*BSY rising edge */ if (sim->vip.dp.buffer == 0) sim->vip.dp.r0bsy = 1; else sim->vip.dp.r1bsy = 1; sim->vip.dp.column = 0; sim->vip.dp.cta = 0x3DE00 | (uint32_t)sim->vip.cta.cta_r<<1; sim->vip.dp.step = 6; /* Fallthrough */ case 6: /* 13ms-18ms - Display right frame buffer */ if ((sim->vip.dp.column & 3) == 0) vipComputeBrightness(sim); vipTransferColumn(sim, 1); sim->vip.dp.clocks = 260 + (0xA94 >> sim->vip.dp.column % 12 & 1); sim->vip.dp.column++; if ((sim->vip.dp.column & 3) == 0) sim->vip.dp.cta -= 2; if (sim->vip.dp.column == 384) sim->vip.dp.step = 7; break; case 7: /* 18ms - R*BSY falling edge */ if (sim->vip.dp.buffer == 0) sim->vip.dp.r0bsy = 0; else sim->vip.dp.r1bsy = 0; sim->vip.dp.clocks = vipClocksMs(2); sim->vip.dp.step = 0; sim->vip.dp.until = vipClocksMs(2); vipThrow(sim, 0x0004); /* RFBEND */ if (vipOnFrame(sim)) return 1; break; /* 18ms-20ms - Idle */ } } return 0; } /****************************** Pixel Processor ******************************/ /* Draw an object group into shadow memory */ static void vipDrawObjects(VB *sim, int group) { uint8_t *dest; /* Pointer to object position in shadow memory */ int fx; /* Output horizontal position */ int fy; /* Output vertical position */ uint8_t *obj; /* Pointer to object attributes in host memory */ int32_t ox; /* Eye horizontal position */ int pixel; /* Character input pixel */ int start; /* Index of first object in group */ int stop; /* Index of last object in group */ int sx; /* Input horizontal position */ int sy; /* Input vertical position */ int32_t top; /* Window boundary */ int i, o, x, y; /* Iterators */ /* Object attributes */ uint32_t attr1; /* Attribute bits */ uint32_t attr2; /* Attribute bits */ int jhflp; /* Is flipped horizontally */ int32_t jp; /* Stereo parallax */ int jvflp; /* Is flipped vetically */ int32_t jx; /* Base horizontal position */ int32_t jy; /* Vertical position */ uint8_t *plt; /* Palette */ uint8_t *src; /* Pointer to character source data in host memory */ /* Process all objects in the group */ start = group == 0 ? 0 : (sim->vip.spt[group - 1] + 1) & 1023; stop = sim->vip.spt[group]; top = (int32_t) sim->vip.xp.sbcount << 3; for (o = stop;; o = (o - 1) & 1023) { obj = &sim->vip.ram[0x3E000 | o << 3]; /* Validate object is enabled */ attr1 = busReadBuffer(obj + 2, VB_U16); if ((attr1 & 0xC000) == 0) /* JLON, JRON */ continue; /* Validate Y position */ jy = (int8_t) obj[4]; if (jy < 7) jy &= 0xFF; if (jy < top - 7 || jy > 223) continue; /* Parse attributes */ jx = SignExtend(busReadBuffer(obj, VB_U16), 10); jp = SignExtend(attr1, 10); attr2 = busReadBuffer(obj + 6, VB_U16); jhflp = attr2 >> 13 & 1; jvflp = attr2 >> 12 & 1; /* Locate palette and character state in host memory */ plt = sim->vip.jplt[attr2 >> 14]; src = vipCharacter(sim, attr2); /* Draw the character */ for (i = 0; i < 2; i++) { if ((attr1 >> (15 - i) & 1) == 0) /* J*ON */ continue; ox = jx - (i == 0 ? jp : -jp); dest = &sim->vip.shadow[i][ox * 224 + jy]; /* Draw all columns */ for (x = 0; x < 8; x++, dest += 224) { fx = ox + x; if (fx < 0 || fx > 383) continue; sx = jhflp ? 7 - x : x; /* Draw all rows */ for (y = 0; y < 8; y++) { fy = jy + y; if (fy < top || fy > 223) continue; sy = jvflp ? 7 - y : y; pixel = src[sy << 1 | sx >> 2] >> ((sx & 3) << 1) & 3; if (pixel != 0) dest[y] = plt[pixel]; /* TODO: Research clocks */ } /* x */ } /* y */ } /* i */ /* All objects have been drawn */ if (o == start) break; } } /* Draw a background world into shadow memory */ static void vipDrawWorld(VB *sim, uint8_t *world, uint16_t attr) { uint8_t *bg; /* Background arrangement */ uint16_t bgAttr; /* BG map cell attributes */ int32_t bgh; /* Height of background in BG maps, minus 1 */ int32_t bgw; /* Width of background in BG maps, minus 1 */ int32_t bottom; /* Window bottom edge position */ int32_t bp; /* Eye BG map source parallax */ int32_t bx; /* BG horizontal map source position */ int32_t by; /* BG vertical map source position */ uint8_t *cell; /* Pointer to BG map cell in host memory */ uint8_t *chr; /* Pointer to character data in host memory */ int32_t cx; /* BG cell/character horizontal pixel source position */ int32_t cy; /* BG cell/character vertical pixel source position */ uint8_t *dest; /* Pointer to output in shadow memory */ int32_t left; /* Window left edge position */ uint32_t param; /* Current line parameter address */ int8_t pixel; /* Character pixel value */ int32_t right; /* Window right edge position */ int32_t top; /* Window top edge position */ int32_t wx; /* Eye horizontal position */ int32_t i, x, y; /* Iterators */ /* World attributes */ int32_t base; /* BG map base index */ int bgm; /* BG mode */ int32_t dx; /* Affine per-pixel X delta */ int32_t dy; /* Affine per-pixel Y delta */ int16_t gp; /* Window stereo parallax */ int16_t gx; /* Window base horizontal position */ int16_t gy; /* Window vertical position */ int16_t h; /* Window height */ int16_t hofst; /* H-bias shift */ int32_t mp; /* BG map source stereo parallax */ int32_t mx = 0; /* BG map horizontal source */ int32_t my = 0; /* BG map vertical source */ int32_t scx; /* Background width shift amount */ int32_t scy; /* Background height shift amount */ uint8_t *over; /* Pointer to overplane "cell" in host memory */ uint32_t params = 0; /* Base address of line parameters */ int16_t w; /* Window width */ /* Validate the world isn't below the frame */ gy = busReadBuffer(world + 6, VB_S16); if (gy > 223) return; /* Validate the world isn't above the frame, and height is positive */ bgm = attr >> 12 & 3; y = bgm == 2 ? 0 : 8; h = busReadBuffer(world + 16, VB_S16) + 1; if (h < y) h = y; if (h == 0) return; bottom = gy + h - 1; top = (int32_t) sim->vip.xp.sbcount << 3; if (bottom < top) return; if (bottom > 223) bottom = 223; if (top < gy) top = gy; /* Validate width is positive */ w = SignExtend(busReadBuffer(world + 14, VB_U16), bgm == 2 ? 10 : 13) + 1; if (w < 1) return; /* Parse attributes */ bg = (uint8_t *) BG_TEMPLATES[attr >> 8 & 15]; gx = SignExtend(busReadBuffer(world + 2, VB_U16), 10); gp = SignExtend(busReadBuffer(world + 4, VB_U16), 10); over = (attr & 0x0080) ? world + 20 : NULL; scx = attr >> 10 & 3; scy = attr >> 8 & 3; bgw = (1 << scx) - 1; bgh = (1 << scy) - 1; base = attr & 15 & ~((1 << (scx + scy < 3 ? scx + scy : 3)) - 1); if (bgm != 2) { mx = SignExtend(busReadBuffer(world + 8, VB_U16), 13); mp = SignExtend(busReadBuffer(world + 10, VB_U16), 15); my = SignExtend(busReadBuffer(world + 12, VB_U16), 13); } if (bgm != 0) { params = 0x20000 + (top - gy) * (bgm == 1 ? 4 : 16) + ((uint32_t) busReadBuffer(world + 18, VB_U16) << 1); } else hofst = 0; /* Draw the world */ for (i = 0; i < 2; i++) { /* Validate world is enabled */ if ((attr & (int32_t) 0x8000 >> i) == 0) /* LON, RON */ continue; /* Validate the world isn't right of frame */ wx = gx - (i == 0 ? gp : -gp); if (wx > 383) return; left = wx < 0 ? 0 : wx; /* Validate the world isn't left of frame */ right = wx + w - 1; if (right < 0) return; if (right > 383) right = 383; /* BG source parallax */ if (bgm != 2) bp = i == 0 ? -mp : mp; /* Draw all rows */ for (y = top, param = params; y <= bottom; y++) { /* Parse line parameters */ switch (bgm) { case 1: /* H-bias */ hofst = SignExtend(vipParam(param | i << 1), 13); param += 4; break; case 2: /* Affine */ dx = (int32_t) (int16_t) vipParam(param | 6) << 7; dy = (int32_t) (int16_t) vipParam(param | 8) << 7; mp = (int32_t) (int16_t) vipParam(param | 2); mx = ((int32_t) (int16_t) vipParam(param ) << 13) + dx * (left - ((mp < 0) ^ i ? mp : 0)); my = ((int32_t) (int16_t) vipParam(param | 4) << 13) + dy * (left - ((mp < 0) ^ i ? mp : 0)); param += 16; } /* Select output in shadow memory */ dest = &sim->vip.shadow[i][left * 224 + y]; /* Draw all columns */ for (x = left; x <= right; x++, dest += 224) { /* Locate the pixel in the background */ if (bgm != 2) { /* Normal, H-bias */ cx = x - wx + mx + bp + hofst; cy = y - gy + my; } else { /* Affine */ cx = (int16_t) (mx >> 16); cy = (int16_t) (my >> 16); mx += dx; my += dy; } /* Locate the BG map in the background */ bx = SignExtend(cx >> 9, 23); by = SignExtend(cy >> 9, 23); cell = NULL; /* BG map is out of bounds */ if (bx < 0 || bx > bgw || by < 0 || by > bgh) { if (over == NULL) { bx &= bgw; by &= bgh; } else cell = over; } /* Locate the cell in the BG map */ if (cell == NULL) { cell = &sim->vip.ram[ 0x20000 | (base + bg[by << scx | bx]) << 13 | (cy << 4 & 0x1F80) | (cx >> 2 & 0x007E) ]; } /* Extract the pixel from the character */ bgAttr = busReadBuffer(cell, VB_U16); chr = vipCharacter(sim, bgAttr); cx = (bgAttr & 0x2000 ? 7 - cx : cx) & 7; cy = (bgAttr & 0x1000 ? 7 - cy : cy) & 7; pixel = chr[cy << 1 | cx >> 2] >> ((cx & 3) << 1) & 3; /* Write the pixel into shadow memory */ if (pixel != 0) *dest = sim->vip.gplt[bgAttr >> 14 & 3][pixel]; } /* x */ } /* y */ } /* i */ } /* Draw the current graphics configuration into shadow memory */ static void vipRender(VB *sim) { uint16_t attr; /* World attribute bits */ int group; /* Next object group index */ uint16_t *timing; /* Column timing in host memory */ uint8_t *world; /* World attribute source in host memory */ int32_t x, y; /* Iterators */ /* Erase all pixels */ for (x = 0; x < 384*224; x += 224) for (y = (int32_t) sim->vip.xp.sbcount << 3; y < 224; y++) sim->vip.shadow[0][x + y] = sim->vip.shadow[1][x + y] = sim->vip.bkcol; /* Process all worlds */ group = 3; world = &sim->vip.ram[0x3DBE0]; /* World 31 */ for (x = 31; x >= 0; x--, world -= 32) { attr = busReadBuffer(world, VB_U16); /* Non-graphical world */ if (attr & 0x0020) /* END */ break; /* Control world */ if ((attr & 0xC000) == 0) /* LON, RON */ continue; /* Dummy world */ /* Object world */ if ((attr >> 12 & 3) == 3) { vipDrawObjects(sim, group); group = (group - 1) & 3; } /* Background world */ else if (attr & 0xC000) /* LON, RON */ vipDrawWorld(sim, world, attr); } /* Supply filler timings for each group of 8 rows of pixels Using 2.8ms experimental measurement for empty frame = 125 clocks every 24 halfwords */ timing = &sim->vip.halfwords[(uint32_t) sim->vip.xp.sbcount * 384]; x = sim->vip.xp.column; for (y = sim->vip.xp.sbcount; y < 28; y++, x = 0) for (; x < 384; x++, timing++) *timing = 5 + ((uint32_t) 0x210884 >> x % 24 & 1); } /* Transfer one halfword from shadow pixel memory to the frame buffer */ static void vipTransferHalfword(VB *sim) { uint32_t bits0; /* Upper 4 pixels from shadow memory */ uint32_t bits1; /* Lower 4 pixels from shadow memory */ uint8_t *dest; /* Pointer to frame buffer data in host memory */ uint32_t offset; /* Position of halfword in shadow memory */ uint8_t *src; /* Pointer to pixel data in host memory */ int i; /* Iterator */ /* Determine the output address in frame buffer memory */ dest = &sim->vip.ram[ (uint32_t) (sim->vip.dp.buffer ^ 1) << 15 | (uint32_t) sim->vip.xp.column << 6 | (uint32_t) sim->vip.xp.sbcount << 1 ]; /* Transfer halfwords to each eye's frame buffer */ offset = (uint32_t) sim->vip.xp.column * 224 + ((uint32_t) sim->vip.xp.sbcount << 3); for (i = 0; i < 2; i++, dest += 0x10000) { src = &sim->vip.shadow[i][offset]; bits0 = busReadBuffer(src , VB_S32); bits1 = busReadBuffer(src + 4, VB_S32); busWriteBuffer(dest, VB_U16, (bits0 & 0x0003) | (bits0 >> 6 & 0x000C) | (bits0 >> 12 & 0x0030) | (bits0 >> 18 & 0x00C0) | (bits1 << 8 & 0x0300) | (bits1 << 2 & 0x0C00) | (bits1 >> 4 & 0x3000) | (bits1 >> 10 & 0xC000) ); } } /* Process sub-component */ static void vipEmulateDrawing(VB *sim, uint32_t clocks) { /* Process all clocks */ for (;;) { /* The next event is after the time remaining */ if (sim->vip.xp.clocks > clocks) { sim->vip.xp.clocks -= clocks; sim->vip.xp.until -= clocks; return; } /* Advance forward the component's number of clocks */ clocks -= sim->vip.xp.clocks; sim->vip.xp.until -= sim->vip.xp.clocks; sim->vip.xp.clocks = 0; /* Processing by operation phase */ switch (sim->vip.xp.step) { case 1: /* Initialization */ sim->vip.xp.halfword = 0; sim->vip.xp.sbcount = 0; if (sim->vip.dp.buffer == 0) sim->vip.xp.f1bsy = 1; else sim->vip.xp.f0bsy = 1; vipRender(sim); /* Fallthrough */ case 2: /* Begin drawing halfwords */ sim->vip.xp.column = 0; sim->vip.xp.sbout = 2240; /* 112us */ sim->vip.xp.step = 3; sim->vip.xp.until = 2000; /* 100us */ if (sim->vip.xp.sbcount == sim->vip.xp.sbcmp) vipThrow(sim, 0x2000); /* SBHIT */ /* Fallthrough */ case 3: /* Draw one halfword */ vipTransferHalfword(sim); sim->vip.xp.clocks = sim->vip.halfwords[sim->vip.xp.halfword]; sim->vip.xp.column ++; sim->vip.xp.halfword++; if (sim->vip.xp.column < 384) break; /* Transition to a new line of halfwords */ sim->vip.xp.sbcount++; sim->vip.xp.step = sim->vip.xp.sbcount == 28 ? 4 : 2; break; case 4: /* Drawing complete */ sim->vip.xp.step = 0; if (sim->vip.dp.buffer == 0) sim->vip.xp.f1bsy = 0; else sim->vip.xp.f0bsy = 0; vipThrow(sim, 0x4000); /* XPEND */ return; } } return; /* Unreachable */ } /***************************** Library Functions *****************************/ /* Process component */ static int vipEmulate(VB *sim, uint32_t clocks) { /* Process sub-components */ int brk = vipEmulateDisplay(sim, clocks); if (sim->vip.xp.step != 0) vipEmulateDrawing(sim, clocks); /* Process SBOUT */ if (sim->vip.xp.sbout != 0) { if (sim->vip.xp.sbout <= clocks) { sim->vip.xp.sbout = 0; } else sim->vip.xp.sbout -= clocks; } return brk; } /* Read a typed value from the VIP bus */ static void vipRead(VB *sim, uint32_t address, int type, int32_t *value) { /* Working variables */ address &= 0x0007FFFF; /* RAM */ if (address < 0x40000) *value = busReadBuffer(&sim->vip.ram[address], type); /* Unmapped */ else if (address < 0x5E000) *value = 0; /* I/O register */ else if (address < 0x60000) *value = vipReadIO(sim, address, type); /* Unmapped */ else if (address < 0x78000) *value = 0; /* Mirrors of character memory */ else { address = 0x06000 | (address << 2 & 0x18000) | (address & 0x01FFF); *value = busReadBuffer(&sim->vip.ram[address], type); } } /* Simulate a hardware reset */ static void vipReset(VB *sim) { int x, y; /* Iterators */ /* Normal */ sim->vip.intenb = 0x0000; sim->vip.dp.disp = 0; sim->vip.dp.re = 0; sim->vip.dp.synce = 0; sim->vip.xp.xpen = 0; /* Extra (the hardware does not do this) */ sim->vip.bkcol = 0; sim->vip.cta.cta_l = 0xFA; sim->vip.cta.cta_r = 0xFA; sim->vip.frmcyc = 0; sim->vip.intpnd = 0x0000; for (x = 0; x < 0x40000; x++) sim->vip.ram[x] = 0; for (x = 0; x < 4; x++) { sim->vip.brtRest[x] = 0; sim->vip.spt [x] = 0; for (y = 0; y < 4; y++) { sim->vip.gplt[x][y] = 0; sim->vip.jplt[x][y] = 0; } } /* Display processor extra (the hardware does not do this) */ sim->vip.dp.fclk = 0; sim->vip.dp.l0bsy = 0; sim->vip.dp.l1bsy = 0; sim->vip.dp.lock = 0; sim->vip.dp.r0bsy = 0; sim->vip.dp.r1bsy = 0; sim->vip.dp.scanrdy = 1; /* Pixel processor extra (the hardware does not do this) */ sim->vip.xp.f0bsy = 0; sim->vip.xp.f1bsy = 0; sim->vip.xp.overtime = 0; sim->vip.xp.sbcmp = 0; sim->vip.xp.sbcount = 0; sim->vip.xp.sbout = 0; /* Display processor other */ sim->vip.dp.brt[0] = 0; sim->vip.dp.buffer = 0; /* TODO: Hardware might actually do this */ sim->vip.dp.clocks = 0; sim->vip.dp.step = 0; sim->vip.dp.until = 0; /* Pixel processor other */ sim->vip.xp.clocks = 0; sim->vip.xp.step = 0; sim->vip.xp.until = 0; } /* Determine how many clocks are guaranteed to process */ static uint32_t vipUntil(VB *sim, uint32_t clocks) { if (clocks > sim->vip.dp.until) clocks = sim->vip.dp.until; if (sim->vip.xp.step != 0 && clocks > sim->vip.xp.until) clocks = sim->vip.xp.until; return clocks; } /* Write a typed value to the VIP bus */ static void vipWrite(VB*sim,uint32_t address,int type,int32_t value,int debug){ /* Working variables */ address &= 0x0007FFFF; /* RAM */ if (address < 0x40000) busWriteBuffer(&sim->vip.ram[address], type, value); /* Unmapped */ else if (address < 0x5E000) ; /* I/O register */ else if (address < 0x60000) vipWriteIO(sim, address, type, value, debug); /* Unmapped */ else if (address < 0x78000) ; /* Mirrors of character memory */ else { address = 0x06000 | (address << 2 & 0x18000) | (address & 0x01FFF); busWriteBuffer(&sim->vip.ram[address], type, value); } } #endif /* VBAPI */