Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve genesis emulation #88

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/genesis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ idf_component_register(
REQUIRES box-emu statistics
)
# target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-char-subscripts -Wno-attributes -Wno-implicit-fallthrough -Wno-unused-function -Wno-unused-variable -Wno-discarded-qualifiers)
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-const-variable -Wno-unused-value -O3)
# target_compile_definitions(${COMPONENT_LIB} PRIVATE GWENESIS_AUDIO_ACCURATE=0)
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-const-variable -Wno-unused-value -Ofast)
target_compile_definitions(${COMPONENT_LIB} PRIVATE GWENESIS_AUDIO_ACCURATE=1)
2 changes: 0 additions & 2 deletions components/genesis/gwenesis/src/bus/gwenesis_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ __license__ = "GPLv3"
#define GWENESIS_REFRESH_RATE_PAL 50
#define GWENESIS_AUDIO_FREQ_PAL 52781

#define GWENESIS_AUDIO_ACCURATE 0

#define Z80_FREQ_DIVISOR 14 // Frequency divisor to Z80 clock
#define VDP_CYCLES_PER_LINE 3420// VDP Cycles per Line
#define SCREEN_WIDTH 320
Expand Down
27 changes: 15 additions & 12 deletions components/genesis/src/genesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static uint8_t *frame_buffer = nullptr;

extern unsigned char* VRAM;
extern int zclk;
int system_clock;
static int system_clock;
int scan_line;

int16_t *gwenesis_sn76489_buffer = nullptr;
Expand All @@ -46,7 +46,9 @@ int16_t *gwenesis_ym2612_buffer = nullptr;
int ym2612_index;
int ym2612_clock;

static int frameskip = 3;
static constexpr int full_frameskip = 3;
static constexpr int muted_frameskip = 2;
static int frameskip = full_frameskip;

static FILE *savestate_fp = NULL;
static int savestate_errors = 0;
Expand Down Expand Up @@ -185,8 +187,9 @@ void IRAM_ATTR run_genesis_rom() {
static GamepadState previous_state = {};
auto state = BoxEmu::get().gamepad_state();

// set frameskip to be 3 if muted, 60 otherwise
frameskip = 3; // hal::is_muted() ? 3 : 60;
bool sound_enabled = !espp::EspBox::get().is_muted();

frameskip = sound_enabled ? full_frameskip : muted_frameskip;

if (previous_state != state) {
// button mapping:
Expand Down Expand Up @@ -224,21 +227,22 @@ void IRAM_ATTR run_genesis_rom() {

gwenesis_vdp_render_config();

bool sound_enabled = !espp::EspBox::get().is_muted();

/* Reset the difference clocks and audio index */
system_clock = 0;
zclk = sound_enabled ? 0 : 0x1000000;
// zclk = 0x1000000;

ym2612_clock = sound_enabled ? 0 : 0x1000000;
// ym2612_clock = 0x1000000;
ym2612_index = 0;

sn76489_clock = sound_enabled ? 0 : 0x1000000;
// sn76489_clock = sound_enabled ? 0 : 0x1000000;
sn76489_clock = 0x1000000; // disable sn76489
sn76489_index = 0;

scan_line = 0;

int _vdp_cycles_per_line = VDP_CYCLES_PER_LINE / 2;
static constexpr int _vdp_cycles_per_line = VDP_CYCLES_PER_LINE;

while (scan_line < lines_per_frame) {
system_clock += _vdp_cycles_per_line;
Expand All @@ -251,7 +255,7 @@ void IRAM_ATTR run_genesis_rom() {
* =1 : cycle accurate mode. audio is refreshed when CPUs are performing a R/W access
* =0 : line accurate mode. audio is refreshed every lines.
*/
if (GWENESIS_AUDIO_ACCURATE == 0) {
if (GWENESIS_AUDIO_ACCURATE == 0 && sound_enabled) {
gwenesis_SN76489_run(system_clock);
ym2612_run(system_clock);
}
Expand Down Expand Up @@ -292,14 +296,13 @@ void IRAM_ATTR run_genesis_rom() {
if (scan_line == (screen_height + 1)) {
z80_irq_line(0);
}

} // end of scanline loop

/* Audio
* synchronize YM2612 and SN76489 to system_clock
* it completes the missing audio sample for accurate audio mode
*/
if (GWENESIS_AUDIO_ACCURATE == 1) {
if (GWENESIS_AUDIO_ACCURATE == 1 && sound_enabled) {
gwenesis_SN76489_run(system_clock);
ym2612_run(system_clock);
}
Expand All @@ -325,7 +328,7 @@ void IRAM_ATTR run_genesis_rom() {
if (sound_enabled) {
// push the audio buffer to the audio task
int audio_len = REG1_PAL ? GWENESIS_AUDIO_BUFFER_LENGTH_PAL : GWENESIS_AUDIO_BUFFER_LENGTH_NTSC;
espp::EspBox::get().play_audio((uint8_t*)gwenesis_ym2612_buffer, audio_len);
espp::EspBox::get().play_audio((uint8_t*)gwenesis_ym2612_buffer, audio_len >> 1);
}

// manage statistics
Expand Down
6 changes: 5 additions & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ extern "C" void app_main(void) {
logger.info("Bootup");

// initialize the hardware abstraction layer
BoxEmu &emu = BoxEmu::get();
espp::EspBox &box = espp::EspBox::get();
logger.info("Running on {}", box.box_type());
BoxEmu &emu = BoxEmu::get();
logger.info("Box Emu version: {}", emu.version());

// initialize
Expand Down Expand Up @@ -78,6 +78,10 @@ extern "C" void app_main(void) {

print_heap_state();

// set the task priority (for main) to high
vTaskPrioritySet(nullptr, 20);

// main loop
while (true) {
// reset gui ready to play and user_quit
gui.ready_to_play(false);
Expand Down