Skip to content

Commit

Permalink
reimplement single threaded mode part1
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdf committed Jan 29, 2021
1 parent 6a64c11 commit 0ca2e3a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 16 deletions.
2 changes: 1 addition & 1 deletion BasiliskII/src/NOTES
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ make

there are two versions:
- 'worker': uses worker with SharedArrayBuffer. this is the default
- 'mainthread': doesn't use worker or SAB. to build this version, set the environment variable `macemujs_conf_mainthread=1` when building
- 'mainthread': (currently broken) doesn't use worker or SAB. to build this version, set the environment variable `macemujs_conf_mainthread=1` when building



Expand Down
41 changes: 32 additions & 9 deletions BasiliskII/src/SDL/video_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,12 @@ static int screen_depth; // Depth of current screen
static SDL_Cursor *sdl_cursor; // Copy of Mac cursor
static SDL_Color sdl_palette[256]; // Color palette to be used as CLUT and gamma table
static bool sdl_palette_changed = false; // Flag: Palette changed, redraw thread must set new colors
#ifndef EMSCRIPTEN
#ifndef EMSCRIPTEN_SAB
// this breaks in clang :(
static const int sdl_eventmask = SDL_MOUSEEVENTMASK | SDL_KEYEVENTMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK | SDL_ACTIVEEVENTMASK;
#endif


// Mutex to protect SDL events
static SDL_mutex *sdl_events_lock = NULL;
#define LOCK_EVENTS SDL_LockMutex(sdl_events_lock)
Expand Down Expand Up @@ -1909,14 +1910,27 @@ static int event2keycode(SDL_KeyboardEvent const &ev, bool key_down)

#define SDL_N_EVENTS_PER_HANDLER 1

#ifdef EMSCRIPTEN_MAINTHREAD
int SDL_PeepEvents(SDL_Event* events,
int numevents,
SDL_eventaction action,
Uint32 minType,
Uint32 maxType);
#endif

static void handle_events(void)
{
#ifndef EMSCRIPTEN
#ifndef EMSCRIPTEN_SAB
SDL_Event events[SDL_N_EVENTS_PER_HANDLER];
const int n_max_events = sizeof(events) / sizeof(events[0]);
int n_events;

#ifdef EMSCRIPTEN_MAINTHREAD
while ((n_events = SDL_PeepEvents(events, n_max_events, SDL_GETEVENT, /*SDL_FIRSTEVENT*/0, /*SDL_LASTEVENT*/0xFFFF)) > 0) {
#else
while ((n_events = SDL_PeepEvents(events, n_max_events, SDL_GETEVENT, sdl_eventmask)) > 0) {
#endif

for (int i = 0; i < n_events; i++) {
SDL_Event const & event = events[i];
switch (event.type) {
Expand Down Expand Up @@ -2486,6 +2500,8 @@ static void update_display_static_bbox(driver_base *drv)
}
#endif
#else


// Allocate bounding boxes for SDL_UpdateRects()
const int N_PIXELS = 64;
const int n_x_boxes = (VIDEO_MODE_X + N_PIXELS - 1) / N_PIXELS;
Expand Down Expand Up @@ -2549,7 +2565,6 @@ static void update_display_static_bbox(driver_base *drv)
if (nr_boxes)
SDL_UpdateRects(drv->s, nr_boxes, boxes);


#ifdef EMSCRIPTEN_EMTERPRET
emterpret_frame_count++;

Expand Down Expand Up @@ -2669,6 +2684,7 @@ static void video_refresh_window_static(void)
else
update_display_static(drv);
}

}


Expand Down Expand Up @@ -2699,12 +2715,8 @@ static void VideoRefreshInit(void)

static inline void do_video_refresh(void)
{
#ifdef EMSCRIPTEN
#ifdef EMSCRIPTEN_MAINTHREAD
// TODO implement event handling for mainthread
#else
sdl_fake_read_input();
#endif
#ifdef EMSCRIPTEN_SAB
sdl_fake_read_input();
#else
// Handle SDL events
handle_events();
Expand All @@ -2726,8 +2738,19 @@ void VideoRefresh(void)
if (!redraw_thread_active)
return;


int needs_frame = EM_ASM_INT({
return Module.needsFrame;
});

if (!needs_frame) return;

// Process pending events and update display
do_video_refresh();

EM_ASM(
Module.drawFrame();
);
}

const int VIDEO_REFRESH_HZ = 60;
Expand Down
4 changes: 2 additions & 2 deletions BasiliskII/src/Unix/main_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ int fake_main(int argc, char **argv)
#endif
#endif

#ifndef EMSCRIPTEN
#ifndef EMSCRIPTEN_SAB
#ifdef USE_SDL
// Initialize SDL system
int sdl_flags = 0;
Expand All @@ -587,7 +587,7 @@ int fake_main(int argc, char **argv)
}
atexit(SDL_Quit);
#endif
#endif // not EMSCRIPTEN
#endif // not EMSCRIPTEN_SAB

// Init system routines
SysInit();
Expand Down
26 changes: 23 additions & 3 deletions BasiliskII/src/uae_cpu/basilisk_glue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
#include "compiler/compemu.h"


#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif


// RAM and ROM pointers
uint32 RAMBaseMac = 0; // RAM base (Mac address space) gb-- initializer is important
uint8 *RAMBaseHost; // RAM base (host address space)
Expand Down Expand Up @@ -131,22 +136,37 @@ void InitFrameBufferMapping(void)
#endif
}

#ifdef EMSCRIPTEN
static void execution_block() {
EM_ASM(
Module.startExecutionBlock();
);
m68k_execute();
}
#endif

/*
* Reset and start 680x0 emulation (doesn't return)
*/

void Start680x0(void)
{
m68k_reset();
m68k_reset();
#if USE_JIT
if (UseJIT)
m68k_compile_execute();
m68k_compile_execute();
else
#endif
m68k_execute();

#ifdef EMSCRIPTEN_MAINTHREAD
emscripten_set_main_loop(execution_block, 0, 1);
#else
m68k_execute();
#endif
}



/*
* Trigger interrupt
*/
Expand Down
49 changes: 48 additions & 1 deletion BasiliskII/src/uae_cpu/newcpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ B2_mutex *spcflags_lock = NULL;
#include "mon_disass.h"
#endif

#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif

bool quit_program = false;
struct flag_struct regflags;

Expand Down Expand Up @@ -1386,9 +1390,50 @@ void m68k_do_execute (void)
}
}
}

static int em_execute_depth = 0;
void m68k_execute (void)
{
#ifdef EMSCRIPTEN_MAINTHREAD
// if (em_execute_depth > 3) {
// return;
// }
em_execute_depth++;
int cycle_count = 0;
double start_time = EM_ASM_DOUBLE({
return performance.now();
});
for (;;) {
if (quit_program)
break;
for (;;) {
uae_u32 opcode = GET_OPCODE;
(*cpufunctbl[opcode])(opcode);
cpu_check_ticks();
if (SPCFLAGS_TEST(SPCFLAG_ALL_BUT_EXEC_RETURN)) {
if (m68k_do_specialties())
return;
}
cycle_count++;
// every x cycles, check if we've been executing for ideal frame duration
if (cycle_count > 20000) {
double current_time = EM_ASM_DOUBLE({
return performance.now();
});

if (current_time - start_time > 16) {
// yield to browser
em_execute_depth--;
return;
} else {
cycle_count = 0;
}
}
}
}
em_execute_depth--;

#else

#if USE_JIT
++m68k_execute_depth;
#endif
Expand All @@ -1400,6 +1445,8 @@ void m68k_execute (void)
#if USE_JIT
--m68k_execute_depth;
#endif

#endif
}

static void m68k_verify (uaecptr addr, uaecptr *nextpc)
Expand Down

0 comments on commit 0ca2e3a

Please sign in to comment.