add Emscripten port

This commit is contained in:
Ash Wolf 2019-12-25 23:40:27 +00:00
parent e5f62ceb53
commit 015f6c94d4
11 changed files with 364 additions and 9 deletions

6
.gitignore vendored
View File

@ -1 +1,7 @@
*.pro.user* *.pro.user*
WindWasm/WindEmu.data
WindWasm/WindEmu.html
WindWasm/WindEmu.js
WindWasm/WindEmu.wasm
WindWasm/rom
WindWasm/obj

View File

@ -282,6 +282,12 @@ void Emulator::configure() {
reset(); reset();
} }
uint8_t *Emulator::getROMBuffer() {
return ROM;
}
size_t Emulator::getROMSize() {
return sizeof(ROM);
}
void Emulator::loadROM(uint8_t *buffer, size_t size) { void Emulator::loadROM(uint8_t *buffer, size_t size) {
memcpy(ROM, buffer, min(size, sizeof(ROM))); memcpy(ROM, buffer, min(size, sizeof(ROM)));
} }
@ -463,7 +469,7 @@ int Emulator::getLCDOffsetY() const { return 0; }
int Emulator::getLCDWidth() const { return 320; } int Emulator::getLCDWidth() const { return 320; }
int Emulator::getLCDHeight() const { return 200; } int Emulator::getLCDHeight() const { return 200; }
void Emulator::readLCDIntoBuffer(uint8_t **lines) const { void Emulator::readLCDIntoBuffer(uint8_t **lines, bool is32BitOutput) const {
if (lcdAddress == 0xC0000000) { if (lcdAddress == 0xC0000000) {
int width = 320, height = 200; int width = 320, height = 200;
int bpp = 1; int bpp = 1;
@ -487,10 +493,16 @@ void Emulator::readLCDIntoBuffer(uint8_t **lines) const {
palValue = (lcdPalette >> (palIdx * 4)) & 0xF; palValue = (lcdPalette >> (palIdx * 4)) & 0xF;
palValue |= (palValue << 4); palValue |= (palValue << 4);
if (is32BitOutput) {
lines[y][x*4] = palValue ^ 0xFF;
lines[y][x*4+1] = palValue ^ 0xFF;
lines[y][x*4+2] = palValue ^ 0xFF;
} else {
lines[y][x] = palValue ^ 0xFF; lines[y][x] = palValue ^ 0xFF;
} }
} }
} }
}
} }

View File

@ -61,6 +61,8 @@ private:
public: public:
Emulator(); Emulator();
uint8_t *getROMBuffer() override;
size_t getROMSize() override;
void loadROM(uint8_t *buffer, size_t size) override; void loadROM(uint8_t *buffer, size_t size) override;
void executeUntil(int64_t cycles) override; void executeUntil(int64_t cycles) override;
int32_t getClockSpeed() const override { return CLOCK_SPEED; } int32_t getClockSpeed() const override { return CLOCK_SPEED; }
@ -71,7 +73,7 @@ public:
int getLCDOffsetY() const override; int getLCDOffsetY() const override;
int getLCDWidth() const override; int getLCDWidth() const override;
int getLCDHeight() const override; int getLCDHeight() const override;
void readLCDIntoBuffer(uint8_t **lines) const override; void readLCDIntoBuffer(uint8_t **lines, bool is32BitOutput) const override;
void setKeyboardKey(EpocKey key, bool value) override; void setKeyboardKey(EpocKey key, bool value) override;
void updateTouchInput(int32_t x, int32_t y, bool down) override; void updateTouchInput(int32_t x, int32_t y, bool down) override;
}; };

View File

@ -111,6 +111,8 @@ protected:
public: public:
EmuBase(bool isTVersion) : ARM710(isTVersion) { } EmuBase(bool isTVersion) : ARM710(isTVersion) { }
virtual uint8_t *getROMBuffer() = 0;
virtual size_t getROMSize() = 0;
virtual void loadROM(uint8_t *buffer, size_t size) = 0; virtual void loadROM(uint8_t *buffer, size_t size) = 0;
virtual void executeUntil(int64_t cycles) = 0; virtual void executeUntil(int64_t cycles) = 0;
virtual int32_t getClockSpeed() const = 0; virtual int32_t getClockSpeed() const = 0;
@ -121,7 +123,7 @@ public:
virtual int getLCDOffsetY() const = 0; virtual int getLCDOffsetY() const = 0;
virtual int getLCDWidth() const = 0; virtual int getLCDWidth() const = 0;
virtual int getLCDHeight() const = 0; virtual int getLCDHeight() const = 0;
virtual void readLCDIntoBuffer(uint8_t **lines) const = 0; virtual void readLCDIntoBuffer(uint8_t **lines, bool is32BitOutput) const = 0;
virtual void setKeyboardKey(EpocKey key, bool value) = 0; virtual void setKeyboardKey(EpocKey key, bool value) = 0;
virtual void updateTouchInput(int32_t x, int32_t y, bool down) = 0; virtual void updateTouchInput(int32_t x, int32_t y, bool down) = 0;

View File

@ -369,6 +369,12 @@ void Emulator::configure() {
reset(); reset();
} }
uint8_t *Emulator::getROMBuffer() {
return ROM;
}
size_t Emulator::getROMSize() {
return sizeof(ROM);
}
void Emulator::loadROM(uint8_t *buffer, size_t size) { void Emulator::loadROM(uint8_t *buffer, size_t size) {
memcpy(ROM, buffer, min(size, sizeof(ROM))); memcpy(ROM, buffer, min(size, sizeof(ROM)));
} }
@ -542,7 +548,21 @@ int Emulator::getLCDOffsetY() const { return 5; }
int Emulator::getLCDWidth() const { return 640; } int Emulator::getLCDWidth() const { return 640; }
int Emulator::getLCDHeight() const { return 240; } int Emulator::getLCDHeight() const { return 240; }
void Emulator::readLCDIntoBuffer(uint8_t **lines) const { // TODO move this elsewhere
static bool initRgbValues = false;
static uint32_t rgbValues[16];
void Emulator::readLCDIntoBuffer(uint8_t **lines, bool is32BitOutput) const {
if (!initRgbValues) {
initRgbValues = true;
for (int i = 0; i < 16; i++) {
int r = (0x99 * i) / 15;
int g = (0xAA * i) / 15;
int b = (0x88 * i) / 15;
rgbValues[15 - i] = r | (g << 8) | (b << 16) | 0xFF000000;
}
}
if ((lcdAddress >> 24) == 0xC0) { if ((lcdAddress >> 24) == 0xC0) {
const uint8_t *lcdBuf = &MemoryBlockC0[lcdAddress & MemoryBlockMask]; const uint8_t *lcdBuf = &MemoryBlockC0[lcdAddress & MemoryBlockMask];
int width = 640, height = 240; int width = 640, height = 240;
@ -565,11 +585,16 @@ void Emulator::readLCDIntoBuffer(uint8_t **lines) const {
int palIdx = (byte >> shift) & mask; int palIdx = (byte >> shift) & mask;
int palValue = palette[palIdx]; int palValue = palette[palIdx];
if (is32BitOutput) {
auto line = (uint32_t *)lines[y];
line[x] = rgbValues[palValue];
} else {
palValue |= (palValue << 4); palValue |= (palValue << 4);
lines[y][x] = palValue ^ 0xFF; lines[y][x] = palValue ^ 0xFF;
} }
} }
} }
}
} }

View File

@ -62,6 +62,8 @@ private:
public: public:
Emulator(); Emulator();
uint8_t *getROMBuffer() override;
size_t getROMSize() override;
void loadROM(uint8_t *buffer, size_t size) override; void loadROM(uint8_t *buffer, size_t size) override;
void executeUntil(int64_t cycles) override; void executeUntil(int64_t cycles) override;
int32_t getClockSpeed() const override { return CLOCK_SPEED; } int32_t getClockSpeed() const override { return CLOCK_SPEED; }
@ -72,7 +74,7 @@ public:
int getLCDOffsetY() const override; int getLCDOffsetY() const override;
int getLCDWidth() const override; int getLCDWidth() const override;
int getLCDHeight() const override; int getLCDHeight() const override;
void readLCDIntoBuffer(uint8_t **lines) const override; void readLCDIntoBuffer(uint8_t **lines, bool is32BitOutput) const override;
void setKeyboardKey(EpocKey key, bool value) override; void setKeyboardKey(EpocKey key, bool value) override;
void updateTouchInput(int32_t x, int32_t y, bool down) override; void updateTouchInput(int32_t x, int32_t y, bool down) override;
}; };

View File

@ -57,7 +57,7 @@ void PDAScreenWindow::updateScreen() {
QImage img(emu->getLCDWidth(), emu->getLCDHeight(), QImage::Format_Grayscale8); QImage img(emu->getLCDWidth(), emu->getLCDHeight(), QImage::Format_Grayscale8);
for (int y = 0; y < img.height(); y++) for (int y = 0; y < img.height(); y++)
lines[y] = img.scanLine(y); lines[y] = img.scanLine(y);
emu->readLCDIntoBuffer(lines); emu->readLCDIntoBuffer(lines, false);
lcd->setPixmap(QPixmap::fromImage(std::move(img))); lcd->setPixmap(QPixmap::fromImage(std::move(img)));
} }

7
WindWasm/build.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/sh
FLAGS="-O3 --profiling -s WASM_OBJECT_FILES=0 -std=c++17"
mkdir -p obj
for i in arm710 emubase etna windermere; do emcc -c $FLAGS -o obj/$i.o ../WindCore/$i.cpp; done
emcc $FLAGS -s TOTAL_MEMORY=78643200 --llvm-lto 1 --preload-file rom/5mx.bin --shell-file shell.html obj/*.o main.cpp -o WindEmu.html

1
WindWasm/cover.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 119 KiB

136
WindWasm/main.cpp Normal file
View File

@ -0,0 +1,136 @@
#include "../WindCore/emubase.h"
#include "../WindCore/windermere.h"
#include <stdio.h>
#include <emscripten.h>
#include <SDL.h>
EmuBase *emu;
// SDL_Window *window;
SDL_Surface *surface;
static EpocKey resolveKey(SDLKey sym) {
switch (sym) {
case SDLK_BACKSPACE: return EStdKeyBackspace;
case SDLK_TAB: return EStdKeyTab;
case SDLK_RETURN: return EStdKeyEnter;
case SDLK_ESCAPE: return EStdKeyEscape;
case SDLK_SPACE: return EStdKeySpace;
case SDLK_QUOTE: return EStdKeySingleQuote;
case SDLK_COMMA: return EStdKeyComma;
case SDLK_PERIOD: return EStdKeyFullStop;
case SDLK_0: return (EpocKey)'0';
case SDLK_1: return (EpocKey)'1';
case SDLK_2: return (EpocKey)'2';
case SDLK_3: return (EpocKey)'3';
case SDLK_4: return (EpocKey)'4';
case SDLK_5: return (EpocKey)'5';
case SDLK_6: return (EpocKey)'6';
case SDLK_7: return (EpocKey)'7';
case SDLK_8: return (EpocKey)'8';
case SDLK_9: return (EpocKey)'9';
case SDLK_a: return (EpocKey)'A';
case SDLK_b: return (EpocKey)'B';
case SDLK_c: return (EpocKey)'C';
case SDLK_d: return (EpocKey)'D';
case SDLK_e: return (EpocKey)'E';
case SDLK_f: return (EpocKey)'F';
case SDLK_g: return (EpocKey)'G';
case SDLK_h: return (EpocKey)'H';
case SDLK_i: return (EpocKey)'I';
case SDLK_j: return (EpocKey)'J';
case SDLK_k: return (EpocKey)'K';
case SDLK_l: return (EpocKey)'L';
case SDLK_m: return (EpocKey)'M';
case SDLK_n: return (EpocKey)'N';
case SDLK_o: return (EpocKey)'O';
case SDLK_p: return (EpocKey)'P';
case SDLK_q: return (EpocKey)'Q';
case SDLK_r: return (EpocKey)'R';
case SDLK_s: return (EpocKey)'S';
case SDLK_t: return (EpocKey)'T';
case SDLK_u: return (EpocKey)'U';
case SDLK_v: return (EpocKey)'V';
case SDLK_w: return (EpocKey)'W';
case SDLK_x: return (EpocKey)'X';
case SDLK_y: return (EpocKey)'Y';
case SDLK_z: return (EpocKey)'Z';
case SDLK_UP: return EStdKeyUpArrow;
case SDLK_DOWN: return EStdKeyDownArrow;
case SDLK_RIGHT: return EStdKeyRightArrow;
case SDLK_LEFT: return EStdKeyLeftArrow;
case SDLK_RSHIFT: return EStdKeyRightShift;
case SDLK_LSHIFT: return EStdKeyLeftShift;
case SDLK_RCTRL: return EStdKeyRightCtrl;
case SDLK_LCTRL: return EStdKeyLeftCtrl;
case SDLK_RALT: return EStdKeyMenu;
case SDLK_LALT: return EStdKeyMenu;
case SDLK_LSUPER: return EStdKeyLeftFunc;
case SDLK_RSUPER: return EStdKeyRightFunc;
}
return EStdKeyNull;
}
void emuEventLoop() {
// printf("Doing it\n");
emu->executeUntil(emu->currentCycles() + (emu->getClockSpeed() / 64));
uint8_t *lines[480];
SDL_LockSurface(surface);
int baseX = emu->getLCDOffsetX();
int baseY = emu->getLCDOffsetY();
int height = emu->getLCDHeight();
for (int y = 0; y < height; y++) {
lines[y] = (uint8_t *)surface->pixels + (surface->pitch * (y + baseY)) + (baseX * 4);
}
emu->readLCDIntoBuffer(lines, true);
SDL_UnlockSurface(surface);
SDL_Flip(surface);
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) {
EpocKey k = resolveKey(event.key.keysym.sym);
if (k != EStdKeyNull)
emu->setKeyboardKey(k, (event.key.state == SDL_PRESSED));
} else if (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP) {
if (event.button.button == SDL_BUTTON_LEFT)
emu->updateTouchInput(event.button.x, event.button.y, (event.button.state == SDL_PRESSED));
} else if (event.type == SDL_MOUSEMOTION) {
emu->updateTouchInput(event.motion.x, event.motion.y, (event.motion.state & SDL_BUTTON(1)));
}
}
}
int main(int argc, char **argv) {
emu = new Windermere::Emulator;
emu->setLogger([](const char *str) {
printf("%s\n", str);
});
FILE *f = fopen("rom/5mx.bin", "rb");
fread(emu->getROMBuffer(), 1, 10485760, f);
fclose(f);
if (SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO) != 0) {
printf("SDL_Init failed: %s\n", SDL_GetError());
return 1;
}
surface = SDL_SetVideoMode(emu->getDigitiserWidth(), emu->getDigitiserHeight(), 32, SDL_SWSURFACE);
if (surface == NULL) {
printf("SDL_SetVideoMode failed: %s\n", SDL_GetError());
return 1;
}
// window = SDL_CreateWindow(
// "WindEmu",
// SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
// emu->getDigitiserWidth(), emu->getDigitiserHeight(),
// 0);
// if (window == NULL) {
// printf("SDL_CreateWindow failed: %s\n", SDL_GetError());
// return 1;
// }
emscripten_set_main_loop(&emuEventLoop, 64, 1);
return 0;
}

162
WindWasm/shell.html Normal file
View File

@ -0,0 +1,162 @@
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>WindEmu</title>
<style>
.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
textarea.emscripten { font-family: monospace; width: 80%; }
div.emscripten { text-align: center; }
div.emscripten_border {
border: 1px solid black;
position: relative;
margin: 0 auto;
width: 695px;
height: 305px;
}
img.overlay {
width: 695px;
position: absolute;
top: 0;
left: 0;
pointer-events: none;
}
/* the canvas *must not* have any border or padding, or mouse coords will be wrong */
canvas.emscripten {
border: 0px none;
background-color: black;
}
.spinner {
height: 50px;
width: 50px;
margin: 0px auto;
-webkit-animation: rotation .8s linear infinite;
-moz-animation: rotation .8s linear infinite;
-o-animation: rotation .8s linear infinite;
animation: rotation 0.8s linear infinite;
border-left: 10px solid rgb(0,150,240);
border-right: 10px solid rgb(0,150,240);
border-bottom: 10px solid rgb(0,150,240);
border-top: 10px solid rgb(100,0,200);
border-radius: 100%;
background-color: rgb(200,100,250);
}
@-webkit-keyframes rotation {
from {-webkit-transform: rotate(0deg);}
to {-webkit-transform: rotate(360deg);}
}
@-moz-keyframes rotation {
from {-moz-transform: rotate(0deg);}
to {-moz-transform: rotate(360deg);}
}
@-o-keyframes rotation {
from {-o-transform: rotate(0deg);}
to {-o-transform: rotate(360deg);}
}
@keyframes rotation {
from {transform: rotate(0deg);}
to {transform: rotate(360deg);}
}
</style>
</head>
<body>
<hr/>
<figure style="overflow:visible;" id="spinner"><div class="spinner"></div><center style="margin-top:0.5em"><strong>emscripten</strong></center></figure>
<div class="emscripten" id="status">Downloading...</div>
<div class="emscripten">
<progress value="0" max="100" id="progress" hidden=1></progress>
</div>
<div class="emscripten_border">
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas>
<img src='cover.svg' class='overlay'>
</div>
<hr/>
<div class="emscripten">
<input type="button" value="Fullscreen" onclick="Module.requestFullscreen(false, false)">
</div>
<hr/>
<textarea class="emscripten" id="output" rows="8"></textarea>
<hr>
<script type='text/javascript'>
var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');
var spinnerElement = document.getElementById('spinner');
var Module = {
preRun: [],
postRun: [],
print: (function() {
var element = document.getElementById('output');
if (element) element.value = ''; // clear browser cache
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
// These replacements are necessary if you render to raw HTML
//text = text.replace(/&/g, "&amp;");
//text = text.replace(/</g, "&lt;");
//text = text.replace(/>/g, "&gt;");
//text = text.replace('\n', '<br>', 'g');
console.log(text);
if (element) {
element.value += text + "\n";
element.scrollTop = element.scrollHeight; // focus on bottom
}
};
})(),
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.error(text);
},
canvas: (function() {
var canvas = document.getElementById('canvas');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
return canvas;
})(),
setStatus: function(text) {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
Module.setStatus.last.time = now;
Module.setStatus.last.text = text;
if (m) {
text = m[1];
progressElement.value = parseInt(m[2])*100;
progressElement.max = parseInt(m[4])*100;
progressElement.hidden = false;
spinnerElement.hidden = false;
} else {
progressElement.value = null;
progressElement.max = null;
progressElement.hidden = true;
if (!text) spinnerElement.hidden = true;
}
statusElement.innerHTML = text;
},
totalDependencies: 0,
monitorRunDependencies: function(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
Module.setStatus('Downloading...');
window.onerror = function() {
Module.setStatus('Exception thrown, see JavaScript console');
spinnerElement.style.display = 'none';
Module.setStatus = function(text) {
if (text) Module.printErr('[post-exception status] ' + text);
};
};
</script>
{{{ SCRIPT }}}
</body>
</html>