GB: Add double speed
@@ -27,6 +27,7 @@ static int32_t _updateChannel2(struct GBAudioChannel2* ch);
static int32_t _updateChannel3(struct GBAudioChannel3* ch, enum GBAudioStyle style); static int32_t _updateChannel4(struct GBAudioChannel4* ch); static void _sample(struct GBAudio* audio, int32_t cycles); +static void _scheduleEvent(struct GBAudio* audio); void GBAudioInit(struct GBAudio* audio, size_t samples, uint8_t* nr52, enum GBAudioStyle style) { audio->samples = samples;@@ -158,13 +159,7 @@ if (audio->ch1.control.stop && !(audio->frame & 1)) {
--audio->ch1.control.length; } } - // TODO: Don't need p - if (audio->p) { - audio->nextEvent = audio->p->cpu->cycles; - audio->p->cpu->nextEvent = audio->nextEvent; - } else { - audio->nextEvent = 0; - } + _scheduleEvent(audio); } *audio->nr52 &= ~0x0001; *audio->nr52 |= audio->playingCh1;@@ -216,13 +211,7 @@ if (audio->ch2.control.stop && !(audio->frame & 1)) {
--audio->ch2.control.length; } } - // TODO: Don't need p - if (audio->p) { - audio->nextEvent = audio->p->cpu->cycles; - audio->p->cpu->nextEvent = audio->nextEvent; - } else { - audio->nextEvent = 0; - } + _scheduleEvent(audio); } *audio->nr52 &= ~0x0002; *audio->nr52 |= audio->playingCh2 << 1;@@ -287,16 +276,9 @@ if (audio->nextEvent == INT_MAX) {
audio->eventDiff = 0; } audio->ch3.readable = audio->style != GB_AUDIO_DMG; - // TODO: Don't need p - if (audio->p) { - // TODO: Where does this cycle delay come from? - audio->nextCh3 = audio->eventDiff + audio->p->cpu->cycles + 4 + 2 * (2048 - audio->ch3.rate); - audio->nextEvent = audio->p->cpu->cycles; - audio->p->cpu->nextEvent = audio->nextEvent; - } else { - audio->nextCh3 = audio->eventDiff + 4 + 2 * (2048 - audio->ch3.rate); - audio->nextEvent = 0; - } + _scheduleEvent(audio); + // TODO: Where does this cycle delay come from? + audio->nextCh3 = audio->eventDiff + audio->nextEvent + 4 + 2 * (2048 - audio->ch3.rate); } *audio->nr52 &= ~0x0004; *audio->nr52 |= audio->playingCh3 << 2;@@ -352,13 +334,7 @@ if (audio->ch4.stop && !(audio->frame & 1)) {
--audio->ch4.length; } } - // TODO: Don't need p - if (audio->p) { - audio->nextEvent = audio->p->cpu->cycles; - audio->p->cpu->nextEvent = audio->nextEvent; - } else { - audio->nextEvent = 0; - } + _scheduleEvent(audio); } *audio->nr52 &= ~0x0008; *audio->nr52 |= audio->playingCh4 << 3;@@ -865,3 +841,13 @@ timing <<= ch->frequency;
timing *= 8; return timing; } + +void _scheduleEvent(struct GBAudio* audio) { + // TODO: Don't need p + if (audio->p) { + audio->nextEvent = audio->p->cpu->cycles >> audio->p->doubleSpeed; + audio->p->cpu->nextEvent = audio->nextEvent; + } else { + audio->nextEvent = 0; + } +}
@@ -62,6 +62,7 @@ gb->pristineRomSize = 0;
gb->yankedRomSize = 0; gb->eiPending = false; + gb->doubleSpeed = 0; } bool GBLoadROM(struct GB* gb, struct VFile* vf) {@@ -160,7 +161,7 @@ irqh->reset = GBReset;
irqh->processEvents = GBProcessEvents; irqh->setInterrupts = GBSetInterrupts; irqh->hitIllegal = GBIllegal; - irqh->hitStub = GBHitStub; + irqh->stop = GBStop; irqh->halt = GBHalt; }@@ -253,14 +254,20 @@ gb->eiPending = 0;
} } - testEvent = GBVideoProcessEvents(&gb->video, cycles); - if (testEvent < nextEvent) { - nextEvent = testEvent; + testEvent = GBVideoProcessEvents(&gb->video, cycles >> gb->doubleSpeed); + if (testEvent != INT_MAX) { + testEvent <<= gb->doubleSpeed; + if (testEvent < nextEvent) { + nextEvent = testEvent; + } } - testEvent = GBAudioProcessEvents(&gb->audio, cycles); - if (testEvent < nextEvent) { - nextEvent = testEvent; + testEvent = GBAudioProcessEvents(&gb->audio, cycles >> gb->doubleSpeed); + if (testEvent != INT_MAX) { + testEvent <<= gb->doubleSpeed; + if (testEvent < nextEvent) { + nextEvent = testEvent; + } } testEvent = GBTimerProcessEvents(&gb->timer, cycles);@@ -301,14 +308,19 @@ cpu->cycles = cpu->nextEvent;
cpu->halted = true; } +void GBStop(struct LR35902Core* cpu) { + struct GB* gb = (struct GB*) cpu->master; + if (gb->memory.io[REG_KEY1] & 1) { + gb->doubleSpeed ^= 1; + gb->memory.io[REG_KEY1] &= 1; + gb->memory.io[REG_KEY1] |= gb->doubleSpeed << 7; + } + // TODO: Actually stop +} + void GBIllegal(struct LR35902Core* cpu) { // TODO mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus); -} - -void GBHitStub(struct LR35902Core* cpu) { - // TODO - mLOG(GB, STUB, "Hit stub at address %04X:%02X\n", cpu->pc, cpu->bus); } bool GBIsROM(struct VFile* vf) {
@@ -67,6 +67,7 @@
struct mAVStream* stream; int32_t eiPending; + unsigned doubleSpeed; }; struct GBCartridge {
@@ -35,6 +35,7 @@ [REG_NR50] = 0x00,
[REG_NR51] = 0x00, [REG_NR52] = 0x70, [REG_STAT] = 0x80, + [REG_KEY1] = 0x7E, [REG_VBK] = 0xFE, [REG_OCPS] = 0x40, [REG_BCPS] = 0x40,@@ -308,6 +309,10 @@ return;
default: if (gb->model >= GB_MODEL_CGB) { switch (address) { + case REG_KEY1: + value &= 0x1; + value |= gb->memory.io[address] & 0x80; + break; case REG_VBK: GBVideoSwitchBank(&gb->video, value); break;@@ -455,6 +460,7 @@ case REG_HDMA3:
case REG_HDMA4: case REG_HDMA5: case REG_SVBK: + case REG_KEY1: // Handled transparently by the registers goto success; default:
@@ -221,7 +221,7 @@ if (video->mode != 3 || video->dotCounter < 0) {
return; } int oldX = video->x; - video->x = video->dotCounter + video->eventDiff + video->p->cpu->cycles; + video->x = video->dotCounter + video->eventDiff + (video->p->cpu->cycles >> video->p->doubleSpeed); if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) { video->x = GB_VIDEO_HORIZONTAL_PIXELS; }@@ -238,15 +238,15 @@ if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
video->mode = 2; video->nextMode = GB_VIDEO_MODE_2_LENGTH - 5; // TODO: Why is this fudge factor needed? Might be related to T-cycles for load/store differing video->nextEvent = video->nextMode; - video->eventDiff = -video->p->cpu->cycles; + video->eventDiff = -video->p->cpu->cycles >> video->p->doubleSpeed; // TODO: Does this read as 0 for 4 T-cycles? video->stat = GBRegisterSTATSetMode(video->stat, 2); video->p->memory.io[REG_STAT] = video->stat; video->ly = 0; video->p->memory.io[REG_LY] = 0; - if (video->p->cpu->cycles + video->nextEvent < video->p->cpu->nextEvent) { - video->p->cpu->nextEvent = video->p->cpu->cycles + video->nextEvent; + if (video->p->cpu->cycles + (video->nextEvent << video->p->doubleSpeed) < video->p->cpu->nextEvent) { + video->p->cpu->nextEvent = video->p->cpu->cycles + (video->nextEvent << video->p->doubleSpeed); } return; }
@@ -26,7 +26,7 @@ DECLARE_INSTRUCTION_LR35902(EMITTER, INCC), \
DECLARE_INSTRUCTION_LR35902(EMITTER, DECC), \ DECLARE_INSTRUCTION_LR35902(EMITTER, LDC_), \ DECLARE_INSTRUCTION_LR35902(EMITTER, RRCA_), \ - DECLARE_INSTRUCTION_LR35902(EMITTER, STUB), \ + DECLARE_INSTRUCTION_LR35902(EMITTER, STOP), \ DECLARE_INSTRUCTION_LR35902(EMITTER, LDDE), \ DECLARE_INSTRUCTION_LR35902(EMITTER, LDDE_A), \ DECLARE_INSTRUCTION_LR35902(EMITTER, INCDE), \
@@ -767,7 +767,15 @@ DEFINE_RST_INSTRUCTION_LR35902(30);
DEFINE_RST_INSTRUCTION_LR35902(38); DEFINE_INSTRUCTION_LR35902(ILL, cpu->irqh.hitIllegal(cpu)); -DEFINE_INSTRUCTION_LR35902(STUB, cpu->irqh.hitStub(cpu)); + +DEFINE_INSTRUCTION_LR35902(STOP2, + if (!cpu->bus) { + cpu->irqh.stop(cpu); + }); + +DEFINE_INSTRUCTION_LR35902(STOP, \ + cpu->executionState = LR35902_CORE_READ_PC; \ + cpu->instruction = _LR35902InstructionSTOP2;) static const LR35902Instruction _lr35902CBInstructionTable[0x100] = { DECLARE_LR35902_CB_EMITTER_BLOCK(_LR35902Instruction)
@@ -63,9 +63,9 @@ void (*reset)(struct LR35902Core* cpu);
void (*processEvents)(struct LR35902Core* cpu); void (*setInterrupts)(struct LR35902Core* cpu, bool enable); void (*halt)(struct LR35902Core* cpu); + void (*stop)(struct LR35902Core* cpu); void (*hitIllegal)(struct LR35902Core* cpu); - void (*hitStub)(struct LR35902Core* cpu); }; struct LR35902Core {