Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
coolq1000 committed May 31, 2022
1 parent ae699e5 commit b3662fb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 48 deletions.
1 change: 1 addition & 0 deletions client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ target_include_directories(client PRIVATE include deps/sdl2/include)
target_link_libraries(client SDL2-static)

#target_compile_options(client PRIVATE -Wno-switch)
#target_link_options(client PRIVATE "-mconsole")
2 changes: 2 additions & 0 deletions client/include/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class window
SDL_Texture* texture;
u32* pixels;

u32 last_time = 0;

const u8* keys;

gmb::ppu ppu;
Expand Down
17 changes: 6 additions & 11 deletions client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const char* save_path = "../../res/roms/za.sav";
namespace app
{
gmb::rom rom(cart_path, save_path);
gmb::dmg gameboy(rom, true, 48000, 2048);
gmb::dmg gameboy(rom, true, 48000, 1024);

window win(gameboy.ppu_);
audio as(gameboy.apu_);
Expand All @@ -27,6 +27,7 @@ namespace app
void start()
{
sample_buffer = std::unique_ptr<i16>(new i16[gameboy.apu_.latency * AUDIO_CHANNELS]);
for (u32 i = 0; i < gameboy.apu_.latency * AUDIO_CHANNELS; i++) sample_buffer.get()[i] = 0;
}

void stop()
Expand All @@ -50,14 +51,11 @@ namespace app

void run()
{
usize i = 0;

while (win.open())
{
gameboy.cycle();
if (gameboy.core_dmg.apu.update) audio();
if (gameboy.core_dmg.ppu.draw || i % 1000000 == 0) draw();
i++;
if (gameboy.core_dmg.ppu.draw) draw();
}
}

Expand Down Expand Up @@ -86,18 +84,15 @@ namespace app
void audio()
{
static float buffer_fill = 0;
sample_buffer.get()[static_cast<usize>(buffer_fill * 2) + 0] = gameboy.core_dmg.apu.output_left;
sample_buffer.get()[static_cast<usize>(buffer_fill * 2) + 1] = gameboy.core_dmg.apu.output_right;
sample_buffer.get()[static_cast<usize>(buffer_fill * 2) + 0] = gameboy.core_dmg.apu.output_left * 4;
sample_buffer.get()[static_cast<usize>(buffer_fill * 2) + 1] = gameboy.core_dmg.apu.output_right * 4;

buffer_fill += 1.0f / (turbo_active ? SPEED_SHIFT : 1.0f);

if (buffer_fill >= gameboy.apu_.latency)
{
buffer_fill = 0;
while (as.queued() > gameboy.apu_.latency * AUDIO_CHANNELS)
{
SDL_Delay(1);
}
while (as.queued() > gameboy.apu_.latency * AUDIO_CHANNELS) {}
as.queue(sample_buffer.get(), gameboy.apu_.latency * AUDIO_CHANNELS);
}
}
Expand Down
9 changes: 9 additions & 0 deletions client/src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ void window::update()
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);

while (true) {
u32 delta = SDL_GetTicks() - last_time;

if (delta > 1000 / 60.0) {
last_time = SDL_GetTicks();
break;
}
}
}

bool window::open()
Expand Down
8 changes: 2 additions & 6 deletions core/src/apu.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,8 @@ void apu_poke(apu_t* apu, u16 address, u8 value)
case MMAP_IO_NR10:
apu->nr10 = value;
apu->ch1.sweep.timer.period = (value & 0x70) >> 4;
// if (!apu->ch1.sweep.timer.period) apu->ch1.sweep.timer.period = 8; /* this will always make it sweep? */
if (value & BIT(3) && apu->ch1.sweep.decreasing && apu->ch1.sweep.calculated)
{
apu->ch1.enabled = false;
}

// if (!apu->ch1.sweep.timer.period) apu->ch1.sweep.timer.period = 8;
if (value & BIT(3) && apu->ch1.sweep.decreasing && apu->ch1.sweep.calculated) apu->ch1.enabled = false;
apu->ch1.sweep.decreasing = value & BIT(3);
apu->ch1.sweep.shift = value & 0x7;
break;
Expand Down
67 changes: 36 additions & 31 deletions core/src/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -2747,8 +2747,7 @@ void cpu_cycle_clock(cpu_t* cpu, bus_t* bus, usize cycles)

if (cpu->clock.tima_clock >= tima_rate)
{
bus->mmu->io.tima++;
if (!bus->mmu->io.tima)
if (!++bus->mmu->io.tima)
{
bus->mmu->io.tima = bus->mmu->io.tma;
cpu_request(cpu, bus, INT_TIMER_INDEX);
Expand All @@ -2766,42 +2765,48 @@ void cpu_cycle_clock(cpu_t* cpu, bus_t* bus, usize cycles)
}
}

// u16 tac_mask;
// switch (bus->mmu->io.tac & 0x3)
// {
// case 0: tac_mask = BIT(9); break;
// case 1: tac_mask = BIT(3); break;
// case 2: tac_mask = BIT(5); break;
// case 3: tac_mask = BIT(7); break;
// }
// for (u32 i = 0; i < cycles * (bus->mmu->io.current_speed ? 2 : 1); i++) {
// u16 tac_mask;
// switch (bus->mmu->io.tac & 0x3) {
// case 0:
// tac_mask = BIT(9);
// break;
// case 1:
// tac_mask = BIT(3);
// break;
// case 2:
// tac_mask = BIT(5);
// break;
// case 3:
// tac_mask = BIT(7);
// break;
// }
//
// tac_mask *= bus->mmu->io.tac & BIT(2);
// tac_mask *= bus->mmu->io.tac & BIT(2);
//
// u16 div_old = bus->mmu->io.div;
// bus->mmu->io.div++;
// u16 div_old = bus->mmu->io.div;
// bus->mmu->io.div++;
//
// bool tac_bit_old = div_old & tac_mask;
// bool tac_bit = bus->mmu->io.div & tac_mask;
// bool tac_bit_old = div_old & tac_mask;
// bool tac_bit = bus->mmu->io.div & tac_mask;
//
// if (tac_bit_old && !tac_bit)
// {
// bus->mmu->io.tima++;
// if (bus->mmu->io.tima == 0) /* tima overflown */
// {
// bus->mmu->io.tima = bus->mmu->io.tma;
// cpu_request(cpu, bus, INT_TIMER_INDEX);
// if (tac_bit_old && !tac_bit) {
// bus->mmu->io.tima++;
// if (bus->mmu->io.tima == 0) /* tima overflown */
// {
// bus->mmu->io.tima = bus->mmu->io.tma;
// cpu_request(cpu, bus, INT_TIMER_INDEX);
// }
// }
// }
// if (bus->apu->enabled)
// {
// u16 fs_mask = bus->mmu->io.current_speed ? BIT(14) : BIT(13);
// if (bus->apu->enabled) {
// u16 fs_mask = bus->mmu->io.current_speed ? BIT(14) : BIT(13);
//
// bool fs_bit_old = div_old & fs_mask;
// bool fs_bit = bus->mmu->io.div & fs_mask;
// bool fs_bit_old = div_old & fs_mask;
// bool fs_bit = bus->mmu->io.div & fs_mask;
//
// if (fs_bit_old && !fs_bit)
// {
// apu_frame_sequencer(bus->apu);
// if (fs_bit_old && !fs_bit) {
// apu_frame_sequencer(bus->apu);
// }
// }
// }
}

0 comments on commit b3662fb

Please sign in to comment.