// Convert 16 packed ARGB 16b-values to r[], g[], b[] static WEBP_INLINE void RGBA32PackedToPlanar_16b_SSE41( const uint16_t* const rgbx, __m128i* const r, __m128i* const g, __m128i* const b) { const __m128i in0 = LOAD_16(rgbx + 0); // r0 | g0 | b0 |x| r1 | g1 | b1 |x const __m128i in1 = LOAD_16(rgbx + 8); // r2 | g2 | b2 |x| r3 | g3 | b3 |x const __m128i in2 = LOAD_16(rgbx + 16); // r4 | ... const __m128i in3 = LOAD_16(rgbx + 24); // r6 | ... // aarrggbb as 16-bit. const __m128i shuff0 = _mm_set_epi8(-1, -1, -1, -1, 13, 12, 5, 4, 11, 10, 3, 2, 9, 8, 1, 0); const __m128i shuff1 = _mm_set_epi8(13, 12, 5, 4, -1, -1, -1, -1, 11, 10, 3, 2, 9, 8, 1, 0); const __m128i A0 = _mm_shuffle_epi8(in0, shuff0); const __m128i A1 = _mm_shuffle_epi8(in1, shuff1); const __m128i A2 = _mm_shuffle_epi8(in2, shuff0); const __m128i A3 = _mm_shuffle_epi8(in3, shuff1); // R0R1G0G1 // B0B1**** // R2R3G2G3 // B2B3**** // (OR is used to free port 5 for the unpack) const __m128i B0 = _mm_unpacklo_epi32(A0, A1); const __m128i B1 = _mm_or_si128(A0, A1); const __m128i B2 = _mm_unpacklo_epi32(A2, A3); const __m128i B3 = _mm_or_si128(A2, A3); // Gather the channels. *r = _mm_unpacklo_epi64(B0, B2); *g = _mm_unpackhi_epi64(B0, B2); *b = _mm_unpackhi_epi64(B1, B3); }
static void ConvertARGBToUV_SSE41(const uint32_t* argb, uint8_t* u, uint8_t* v, int src_width, int do_store) { const int max_width = src_width & ~31; int i; for (i = 0; i < max_width; i += 32, u += 16, v += 16) { __m128i rgb[6], U0, V0, U1, V1; RGB32PackedToPlanar_SSE41(&argb[i], rgb); HorizontalAddPack_SSE41(&rgb[0], &rgb[1], &rgb[0]); HorizontalAddPack_SSE41(&rgb[2], &rgb[3], &rgb[2]); HorizontalAddPack_SSE41(&rgb[4], &rgb[5], &rgb[4]); ConvertRGBToUV_SSE41(&rgb[0], &rgb[2], &rgb[4], &U0, &V0); RGB32PackedToPlanar_SSE41(&argb[i + 16], rgb); HorizontalAddPack_SSE41(&rgb[0], &rgb[1], &rgb[0]); HorizontalAddPack_SSE41(&rgb[2], &rgb[3], &rgb[2]); HorizontalAddPack_SSE41(&rgb[4], &rgb[5], &rgb[4]); ConvertRGBToUV_SSE41(&rgb[0], &rgb[2], &rgb[4], &U1, &V1); U0 = _mm_packus_epi16(U0, U1); V0 = _mm_packus_epi16(V0, V1); if (!do_store) { const __m128i prev_u = LOAD_16(u); const __m128i prev_v = LOAD_16(v); U0 = _mm_avg_epu8(U0, prev_u); V0 = _mm_avg_epu8(V0, prev_v); } STORE_16(U0, u); STORE_16(V0, v); } if (i < src_width) { // left-over WebPConvertARGBToUV_C(argb + i, u, v, src_width - i, do_store); } }
// Convert 8 packed ARGB to r[], g[], b[] static WEBP_INLINE void RGB32PackedToPlanar_SSE41( const uint32_t* const argb, __m128i* const rgb /*in[6]*/) { const __m128i zero = _mm_setzero_si128(); __m128i a0 = LOAD_16(argb + 0); __m128i a1 = LOAD_16(argb + 4); __m128i a2 = LOAD_16(argb + 8); __m128i a3 = LOAD_16(argb + 12); VP8L32bToPlanar_SSE41(&a0, &a1, &a2, &a3); rgb[0] = _mm_unpacklo_epi8(a1, zero); rgb[1] = _mm_unpackhi_epi8(a1, zero); rgb[2] = _mm_unpacklo_epi8(a2, zero); rgb[3] = _mm_unpackhi_epi8(a2, zero); rgb[4] = _mm_unpacklo_epi8(a3, zero); rgb[5] = _mm_unpackhi_epi8(a3, zero); }
void GBAHardwareGPIOWrite(struct GBACartridgeHardware* hw, uint32_t address, uint16_t value) { switch (address) { case GPIO_REG_DATA: hw->pinState &= ~hw->direction; hw->pinState |= value; _readPins(hw); break; case GPIO_REG_DIRECTION: hw->direction = value; break; case GPIO_REG_CONTROL: hw->readWrite = value; break; default: mLOG(GBA_HW, WARN, "Invalid GPIO address"); } if (hw->readWrite) { uint16_t old; LOAD_16(old, 0, hw->gpioBase); old &= ~hw->direction; old |= hw->pinState; STORE_16(old, 0, hw->gpioBase); } else { hw->gpioBase[0] = 0; } }
void _outputPins(struct GBACartridgeHardware* hw, unsigned pins) { if (hw->readWrite) { uint16_t old; LOAD_16(old, 0, hw->gpioBase); old &= hw->direction; hw->pinState = old | (pins & ~hw->direction & 0xF); STORE_16(hw->pinState, 0, hw->gpioBase); } }
bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state) { bool error = false; int32_t check; uint32_t ucheck; LOAD_32(ucheck, 0, &state->versionMagic); if (ucheck > GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION) { mLOG(GBA_STATE, WARN, "Invalid or too new savestate: expected %08X, got %08X", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck); error = true; } else if (ucheck < GBA_SAVESTATE_MAGIC) { mLOG(GBA_STATE, WARN, "Invalid savestate: expected %08X, got %08X", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck); error = true; } else if (ucheck < GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION) { mLOG(GBA_STATE, WARN, "Old savestate: expected %08X, got %08X, continuing anyway", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck); } LOAD_32(ucheck, 0, &state->biosChecksum); if (ucheck != gba->biosChecksum) { mLOG(GBA_STATE, WARN, "Savestate created using a different version of the BIOS: expected %08X, got %08X", gba->biosChecksum, ucheck); uint32_t pc; LOAD_32(pc, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs); if (pc < SIZE_BIOS && pc >= 0x20) { error = true; } } if (gba->memory.rom && (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title)))) { mLOG(GBA_STATE, WARN, "Savestate is for a different game"); error = true; } else if (!gba->memory.rom && state->id != 0) { mLOG(GBA_STATE, WARN, "Savestate is for a game, but no game loaded"); error = true; } LOAD_32(ucheck, 0, &state->romCrc32); if (ucheck != gba->romCrc32) { mLOG(GBA_STATE, WARN, "Savestate is for a different version of the game"); } LOAD_32(check, 0, &state->cpu.cycles); if (check < 0) { mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are negative"); error = true; } if (check >= (int32_t) GBA_ARM7TDMI_FREQUENCY) { mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are too high"); error = true; } LOAD_32(check, 0, &state->video.eventDiff); if (check < 0) { mLOG(GBA_STATE, WARN, "Savestate is corrupted: video eventDiff is negative"); error = true; } LOAD_32(check, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs); int region = (check >> BASE_OFFSET); if ((region == REGION_CART0 || region == REGION_CART1 || region == REGION_CART2) && ((check - WORD_SIZE_ARM) & SIZE_CART0) >= gba->memory.romSize - WORD_SIZE_ARM) { mLOG(GBA_STATE, WARN, "Savestate created using a differently sized version of the ROM"); error = true; } if (error) { return false; } size_t i; for (i = 0; i < 16; ++i) { LOAD_32(gba->cpu->gprs[i], i * sizeof(gba->cpu->gprs[0]), state->cpu.gprs); } LOAD_32(gba->cpu->cpsr.packed, 0, &state->cpu.cpsr.packed); LOAD_32(gba->cpu->spsr.packed, 0, &state->cpu.spsr.packed); LOAD_32(gba->cpu->cycles, 0, &state->cpu.cycles); LOAD_32(gba->cpu->nextEvent, 0, &state->cpu.nextEvent); for (i = 0; i < 6; ++i) { int j; for (j = 0; j < 7; ++j) { LOAD_32(gba->cpu->bankedRegisters[i][j], (i * 7 + j) * sizeof(gba->cpu->bankedRegisters[0][0]), state->cpu.bankedRegisters); } LOAD_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs); } gba->cpu->privilegeMode = gba->cpu->cpsr.a.priv; gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]); if (state->biosPrefetch) { LOAD_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch); } LOAD_32(gba->memory.lastPrefetchedPc, 0, &state->lastPrefetchedPc); if (gba->cpu->cpsr.a.t) { gba->cpu->executionMode = MODE_THUMB; if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) { LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch); LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch); gba->cpu->prefetch[0] &= 0xFFFF; gba->cpu->prefetch[1] &= 0xFFFF; } else { // Maintain backwards compat LOAD_16(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_THUMB) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion); LOAD_16(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion); } } else { gba->cpu->executionMode = MODE_ARM; if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) { LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch); LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch); } else { // Maintain backwards compat LOAD_32(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_ARM) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion); LOAD_32(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion); } } GBASerializedMiscFlags miscFlags = 0; LOAD_32(miscFlags, 0, &state->miscFlags); gba->cpu->halted = GBASerializedMiscFlagsGetHalted(miscFlags); GBAVideoDeserialize(&gba->video, state); GBAMemoryDeserialize(&gba->memory, state); GBAIODeserialize(gba, state); GBAAudioDeserialize(&gba->audio, state); GBASavedataDeserialize(&gba->memory.savedata, state); if (gba->rr) { gba->rr->stateLoaded(gba->rr, state); } return true; }
bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state) { bool error = false; if (state->versionMagic != GBA_SAVESTATE_MAGIC) { GBALog(gba, GBA_LOG_WARN, "Invalid or too new savestate"); error = true; } if (state->biosChecksum != gba->biosChecksum) { GBALog(gba, GBA_LOG_WARN, "Savestate created using a different version of the BIOS"); if (state->cpu.gprs[ARM_PC] < SIZE_BIOS && state->cpu.gprs[ARM_PC] >= 0x20) { error = true; } } if (gba->memory.rom && (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title)))) { GBALog(gba, GBA_LOG_WARN, "Savestate is for a different game"); error = true; } else if (!gba->memory.rom && state->id != 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is for a game, but no game loaded"); error = true; } if (state->romCrc32 != gba->romCrc32) { GBALog(gba, GBA_LOG_WARN, "Savestate is for a different version of the game"); } if (state->cpu.cycles < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: CPU cycles are negative"); error = true; } if (state->cpu.cycles >= (int32_t) GBA_ARM7TDMI_FREQUENCY) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: CPU cycles are too high"); error = true; } if (state->video.eventDiff < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: video eventDiff is negative"); error = true; } int region = (state->cpu.gprs[ARM_PC] >> BASE_OFFSET); if ((region == REGION_CART0 || region == REGION_CART1 || region == REGION_CART2) && ((state->cpu.gprs[ARM_PC] - WORD_SIZE_ARM) & SIZE_CART0) >= gba->memory.romSize - WORD_SIZE_ARM) { GBALog(gba, GBA_LOG_WARN, "Savestate created using a differently sized version of the ROM"); error = true; } if (error) { return false; } memcpy(gba->cpu->gprs, state->cpu.gprs, sizeof(gba->cpu->gprs)); gba->cpu->cpsr = state->cpu.cpsr; gba->cpu->spsr = state->cpu.spsr; gba->cpu->cycles = state->cpu.cycles; gba->cpu->nextEvent = state->cpu.nextEvent; memcpy(gba->cpu->bankedRegisters, state->cpu.bankedRegisters, 6 * 7 * sizeof(int32_t)); memcpy(gba->cpu->bankedSPSRs, state->cpu.bankedSPSRs, 6 * sizeof(int32_t)); gba->cpu->privilegeMode = gba->cpu->cpsr.priv; gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]); if (state->biosPrefetch) { gba->memory.biosPrefetch = state->biosPrefetch; } if (gba->cpu->cpsr.t) { gba->cpu->executionMode = MODE_THUMB; if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) { gba->cpu->prefetch[0] = state->cpuPrefetch[0] & 0xFFFF; gba->cpu->prefetch[1] = state->cpuPrefetch[1] & 0xFFFF; } else { // Maintain backwards compat LOAD_16(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_THUMB) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion); LOAD_16(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion); } } else { gba->cpu->executionMode = MODE_ARM; if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) { gba->cpu->prefetch[0] = state->cpuPrefetch[0]; gba->cpu->prefetch[1] = state->cpuPrefetch[1]; } else { // Maintain backwards compat LOAD_32(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_ARM) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion); LOAD_32(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion); } } GBAMemoryDeserialize(&gba->memory, state); GBAIODeserialize(gba, state); GBAVideoDeserialize(&gba->video, state); GBAAudioDeserialize(&gba->audio, state); GBASavedataDeserialize(&gba->memory.savedata, state, false); if (gba->rr) { gba->rr->stateLoaded(gba->rr, state); } return true; }