Skip to content

Commit

Permalink
Merge pull request #1 from ezillinger/wasm
Browse files Browse the repository at this point in the history
Add emscripten build
  • Loading branch information
ezillinger authored May 6, 2024
2 parents b3afe28 + 0840653 commit ce9149b
Show file tree
Hide file tree
Showing 17 changed files with 33,213 additions and 71 deletions.
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ cmake_minimum_required(VERSION 3.29)
# set the project name
project( ezgb )

# specify the C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# for some reason emscripten tries to link to the system libc++ if we use CMAKE_CXX_STANDARD
if(NOT EMSCRIPTEN)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
else()
set(CMAKE_EXECUTABLE_SUFFIX ".html")
endif()

option(EZ_ADDRESS_SANITIZER "Enable Clang address sanitiizer" False)
option(EZ_USE_FREETYPE "Use Freetype for font rendering" False)
Expand Down
26 changes: 26 additions & 0 deletions data/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html style="margin: 0; height: 100%; overflow: hidden">

<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body style="margin: 0; height: 100%; overflow: hidden">

<!-- Create the canvas that the C++ code will draw into -->
<canvas style="width: 100vw; height: 100vh;" id="canvas" oncontextmenu="event.preventDefault()"></canvas>

<!-- Allow the C++ to access the canvas element -->
<script type='text/javascript'>
var Module = {
canvas: (function() { return document.getElementById('canvas'); })()
};
</script>

<!-- Add the javascript glue code (index.js) as generated by Emscripten -->
<script src="index.js"></script>

</body>

</html>
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

This is a Gameboy emulator written from scratch in C++20. It's not super accurate but it is mostly feature complete

[Click here to try me in your browser!](https://zillinger.ca/ezgb_wasm)

#### Features

* Plays most simple commercial games - ROM Only and MBC1 cartridge types
* Runs in the browser via WebAssembly
* Audio
* Debugger/Disassembler with breakpoints
* PPU Vram Debug Display

#### Things that can be improved

Expand All @@ -31,6 +35,7 @@ I tried to use as few 3rd party libraries as possible The only 3rd party librari
### Building

* Requires CMake, a C++20 compiler, and SDL2
* Tested on GCC, LLVM Clang, MSVC, and Emscripten
* On Linux/MacOS install SDL2 with your package manager
* You'll need to install GCC or LLVM Clang on MacOS - AppleClang doesn't have enough C++20 support
* On Windows download an SDL2 release and set the path in the top level CmakeLists.txt
Expand Down
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32,797 changes: 32,797 additions & 0 deletions src/Bootrom.h

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,23 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED)

target_link_libraries(ezgb SDL2::SDL2 OpenGL::GL)
if(NOT EMSCRIPTEN)
target_link_libraries(ezgb SDL2::SDL2 OpenGL::GL)
else()
# for some reason setting set(CMAKE_CXX_STANDARD 20) chooses the wrong version
target_compile_options(ezgb PRIVATE
"-std=c++20"
"-sUSE_SDL=2"
)
target_link_options(ezgb PRIVATE
"-sUSE_SDL=2"
"-sWASM=1"
"-sALLOW_MEMORY_GROWTH=1"
"-sNO_EXIT_RUNTIME=0"
"-sASSERTIONS=1"
"--shell-file ./index.html" # todo, why doesn't this work?!
)
endif()

if(EZ_USE_FREETYPE)
find_package(Freetype REQUIRED)
Expand Down
23 changes: 16 additions & 7 deletions src/Emulator.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
#include "Emulator.h"
#include "MiscOps.h"
#include "OpCodes.h"
#include "Bootrom.h"

namespace ez {

Emulator::Emulator(Cart& cart, EmuSettings settings)
: m_cart(cart)
, m_settings(settings) {
const auto bootromPath = "./roms/bootix_dmg.bin";
//const auto bootromPath = "./roms/dmg_boot.bin";
log_info("Loading bootrom: {}", bootromPath);
auto fp = fopen(bootromPath, "rb");
ez_assert(fp);
ez_assert(BOOTROM_BYTES == fread(m_bootrom.data(), 1, BOOTROM_BYTES, fp));
fclose(fp);

static constexpr bool loadBootromFromFile = false;
if(loadBootromFromFile){
const auto bootromPath = "./roms/bootix_dmg.bin";
log_info("Loading bootrom: {}", bootromPath);
auto fp = fopen(bootromPath, "rb");
ez_assert(fp);
ez_assert(BOOTROM_BYTES == fread(m_bootrom.data(), 1, BOOTROM_BYTES, fp));
fclose(fp);
}
else {
memcpy(m_bootrom.data(), BOOTROM.data(), BOOTROM_BYTES);
}

log_info("Bootrom loaded successfully");

if (m_settings.m_skipBootROM) {
m_reg.a = 0x01;
Expand Down
Loading

0 comments on commit ce9149b

Please sign in to comment.