Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Init/UI: Several fixes around init and UI state handling
Browse files Browse the repository at this point in the history
  • Loading branch information
skmp committed Jan 29, 2020
1 parent 48cdb5a commit 6c77d4c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 37 deletions.
41 changes: 25 additions & 16 deletions libswirl/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,15 @@ struct ReicastUI_impl : GUI {
{
if (gui_state == Closed)
{
virtualDreamcast->Stop([this] {
gui_state = Commands;
settings_opening = true;
HideOSD();
});
// FIXME: This feels like a hack
// avoids assert if OpenSettings is called twice, before the core actually stops
if (sh4_cpu->IsRunning()) {
virtualDreamcast->Stop([this] {
gui_state = Commands;
settings_opening = true;
HideOSD();
});
}
}
else if (gui_state == VJoyEdit)
{
Expand All @@ -349,8 +353,8 @@ struct ReicastUI_impl : GUI {
std::string game_file = cfgLoadStr("config", "image", "");
if (!game_file.empty())
{
gui_state = ClosedNoResume;
gui_start_game(game_file);
if (gui_start_game(game_file))
gui_state = ClosedNoResume;
}
else
gui_render_content();
Expand All @@ -371,9 +375,7 @@ struct ReicastUI_impl : GUI {
break;
}

if (gui_state == Closed)
virtualDreamcast->Resume();
else if (gui_state == ClosedNoResume)
if (gui_state == ClosedNoResume)
gui_state = Closed;
}

Expand Down Expand Up @@ -552,13 +554,15 @@ struct ReicastUI_impl : GUI {
ImGui::NextColumn();
if (ImGui::Button("Resume", ImVec2(150 * scaling, 50 * scaling)))
{
virtualDreamcast->Resume();
gui_state = Closed;
}

ImGui::NextColumn();
if (ImGui::Button("Restart", ImVec2(150 * scaling, 50 * scaling)))
{
virtualDreamcast->Reset();
virtualDreamcast->Resume();
gui_state = Closed;
}
ImGui::NextColumn();
Expand Down Expand Up @@ -590,6 +594,7 @@ struct ReicastUI_impl : GUI {
if (ImGui::Button("RenderDone Int", ImVec2(150 * scaling, 50 * scaling)))
{
asic_RaiseInterrupt(holly_RENDER_DONE);
virtualDreamcast->Resume();
gui_state = Closed;
}
#endif
Expand Down Expand Up @@ -705,16 +710,17 @@ struct ReicastUI_impl : GUI {
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData(), false);
}

void gui_start_game(const std::string& path)
bool gui_start_game(const std::string& path)
{
// do disc swap
if (game_started)
{
cfgSetVirtual("config", "image", path.c_str());
g_GDRDisc->Swap();

verify(virtualDreamcast != nullptr);
virtualDreamcast->Resume();
return;
return true;
}

auto bios_path = get_readonly_data_path(DATA_PATH);
Expand Down Expand Up @@ -744,10 +750,13 @@ struct ReicastUI_impl : GUI {
default:
break;
}

return false;
}
else
{
game_started = true;
return true;
}
}

Expand Down Expand Up @@ -902,9 +911,9 @@ struct ReicastUI_impl : GUI {
ImGui::PushID("bios");
if (ImGui::Selectable("Dreamcast BIOS"))
{
gui_state = ClosedNoResume;
cfgSetVirtual("config", "image", "");
gui_start_game("");
if (gui_start_game(""))
gui_state = ClosedNoResume;
}
ImGui::PopID();
#endif
Expand All @@ -918,8 +927,8 @@ struct ReicastUI_impl : GUI {
ImGui::PushID(game.path.c_str());
if (ImGui::Selectable(game.name.c_str()))
{
gui_state = ClosedNoResume;
gui_start_game(game.path);
if (gui_start_game(game.path))
gui_state = ClosedNoResume;
}
ImGui::PopID();
}
Expand Down
12 changes: 11 additions & 1 deletion libswirl/hw/mem/_vmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@ void* malloc_pages(size_t size) {
#endif
}

void free_pages(void* ptr) {
#if HOST_OS == OS_WINDOWS
return _aligned_free(ptr);
#elif defined(_ISOC11_SOURCE)
return free(ptr);
#else
return free(ptr);;
#endif
}

// Resets the FPCB table (by either clearing it to the default val
// or by flushing it and making it fault on access again.
void _vmem_bm_reset() {
Expand Down Expand Up @@ -513,7 +523,7 @@ bool _vmem_reserve(VLockedMemory* mram, VLockedMemory* vram, VLockedMemory* aica
}

#define freedefptr(x) \
if (x) { free(x); x = NULL; }
if (x) { free_pages(x); x = NULL; }

void _vmem_release(VLockedMemory* mram, VLockedMemory* vram, VLockedMemory* aica_ram) {
if (virt_ram_base)
Expand Down
22 changes: 8 additions & 14 deletions libswirl/hw/sh4/SuperH4_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,21 @@ bool SuperH4_impl::setBackend(SuperH4Backends backend) {
}

void SuperH4_impl::Run() {
sh4_int_bCpuRun = true;

sh4_backend->Loop();

sh4_int_bCpuRun = false;
}

void SuperH4_impl::Stop()
{
if (sh4_int_bCpuRun)
{
sh4_int_bCpuRun = false;
}
verify(sh4_int_bCpuRun);

sh4_int_bCpuRun = false;
}

void SuperH4_impl::Start()
{
if (!sh4_int_bCpuRun)
{
sh4_int_bCpuRun = true;
}
verify(!sh4_int_bCpuRun);

sh4_int_bCpuRun = true;
}

void SuperH4_impl::Step()
Expand Down Expand Up @@ -175,6 +169,8 @@ bool SuperH4_impl::Init()

void SuperH4_impl::Term()
{
verify(!sh4_cpu->IsRunning());

for (const auto& dev : devices)
dev->Term();

Expand All @@ -183,8 +179,6 @@ void SuperH4_impl::Term()

sh4mmr.reset();

Stop();

sh4_sched_cleanup();

sh4_backend.reset();
Expand Down
9 changes: 9 additions & 0 deletions libswirl/libswirl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ struct Dreamcast_impl : VirtualDreamcast {
int ds_schid = -1;

cThread emu_thread;
cResetEvent emu_started;

Dreamcast_impl() : emu_thread(STATIC_FORWARD(Dreamcast_impl, dc_run), this) { }

Expand Down Expand Up @@ -706,6 +707,10 @@ struct Dreamcast_impl : VirtualDreamcast {
printf("Using DSP Interpreter\n");
}

sh4_cpu->Start();

emu_started.Set();

do {
reset_requested = false;

Expand Down Expand Up @@ -954,6 +959,8 @@ struct Dreamcast_impl : VirtualDreamcast {

void Stop(function<void()> callback)
{
verify(sh4_cpu->IsRunning());

callback_lock.Lock();
verify(this->callback == nullptr);
this->callback = callback;
Expand All @@ -970,7 +977,9 @@ struct Dreamcast_impl : VirtualDreamcast {

void Resume()
{
verify(!sh4_cpu->IsRunning());
emu_thread.Start();
emu_started.Wait();
}

void cleanup_serialize(void* data)
Expand Down
2 changes: 1 addition & 1 deletion libswirl/linux/posix_vmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ VMemType vmem_platform_init(void **vmem_base_addr, void **sh4rcb_addr) {
// Now try to allocate a contiguous piece of memory.
unsigned memsize = 512*1024*1024 + sizeof(Sh4RCB) + ARAM_SIZE_MAX + 0x10000;
void *first_ptr = mmap(0, memsize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (!first_ptr) {
if (first_ptr == MAP_FAILED) {
close(shmem_fd);
return MemTypeError;
}
Expand Down
13 changes: 8 additions & 5 deletions libswirl/windows/win_vmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ static char * base_alloc = NULL;
// Plase read the POSIX implementation for more information. On Windows this is
// rather straightforward.
VMemType vmem_platform_init(void **vmem_base_addr, void **sh4rcb_addr) {
// Firt let's try to allocate the in-memory file
// Firt allocate the actual address space (it will be 64KB aligned on windows).
unsigned memsize = 512 * 1024 * 1024 + sizeof(Sh4RCB) + ARAM_SIZE_MAX;
base_alloc = (char*)VirtualAlloc(0, memsize, MEM_RESERVE, PAGE_NOACCESS);
if (base_alloc == NULL) {
return MemTypeError;
}

// Now let's try to allocate the in-memory file
mem_handle = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, RAM_SIZE_MAX + VRAM_SIZE_MAX + ARAM_SIZE_MAX, 0);
verify(mem_handle != INVALID_HANDLE_VALUE);

// Now allocate the actual address space (it will be 64KB aligned on windows).
unsigned memsize = 512*1024*1024 + sizeof(Sh4RCB) + ARAM_SIZE_MAX;
base_alloc = (char*)VirtualAlloc(0, memsize, MEM_RESERVE, PAGE_NOACCESS);
verify(base_alloc != NULL);

// Calculate pointers now
*sh4rcb_addr = &base_alloc[0];
Expand Down

0 comments on commit 6c77d4c

Please sign in to comment.