From: Ash Wolf Date: Wed, 25 Dec 2019 23:40:27 +0000 (+0000) Subject: add Emscripten port X-Git-Url: http://git.gwright.org.uk/?a=commitdiff_plain;h=015f6c94d47cd3ba98f6e06ac11bc31fd5f4df4c;p=WindEmu.git add Emscripten port --- diff --git a/.gitignore b/.gitignore index 6df9758..3d3670c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,7 @@ *.pro.user* +WindWasm/WindEmu.data +WindWasm/WindEmu.html +WindWasm/WindEmu.js +WindWasm/WindEmu.wasm +WindWasm/rom +WindWasm/obj diff --git a/WindCore/clps7111.cpp b/WindCore/clps7111.cpp index 473756d..af98d79 100644 --- a/WindCore/clps7111.cpp +++ b/WindCore/clps7111.cpp @@ -282,6 +282,12 @@ void Emulator::configure() { reset(); } +uint8_t *Emulator::getROMBuffer() { + return ROM; +} +size_t Emulator::getROMSize() { + return sizeof(ROM); +} void Emulator::loadROM(uint8_t *buffer, size_t size) { 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::getLCDHeight() const { return 200; } -void Emulator::readLCDIntoBuffer(uint8_t **lines) const { +void Emulator::readLCDIntoBuffer(uint8_t **lines, bool is32BitOutput) const { if (lcdAddress == 0xC0000000) { int width = 320, height = 200; int bpp = 1; @@ -487,7 +493,13 @@ void Emulator::readLCDIntoBuffer(uint8_t **lines) const { palValue = (lcdPalette >> (palIdx * 4)) & 0xF; palValue |= (palValue << 4); - lines[y][x] = palValue ^ 0xFF; + 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; + } } } } diff --git a/WindCore/clps7111.h b/WindCore/clps7111.h index f494525..47dae14 100644 --- a/WindCore/clps7111.h +++ b/WindCore/clps7111.h @@ -61,6 +61,8 @@ private: public: Emulator(); + uint8_t *getROMBuffer() override; + size_t getROMSize() override; void loadROM(uint8_t *buffer, size_t size) override; void executeUntil(int64_t cycles) override; int32_t getClockSpeed() const override { return CLOCK_SPEED; } @@ -71,7 +73,7 @@ public: int getLCDOffsetY() const override; int getLCDWidth() 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 updateTouchInput(int32_t x, int32_t y, bool down) override; }; diff --git a/WindCore/emubase.h b/WindCore/emubase.h index 0fbf70f..a5a6f70 100644 --- a/WindCore/emubase.h +++ b/WindCore/emubase.h @@ -111,6 +111,8 @@ protected: public: 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 executeUntil(int64_t cycles) = 0; virtual int32_t getClockSpeed() const = 0; @@ -121,7 +123,7 @@ public: virtual int getLCDOffsetY() const = 0; virtual int getLCDWidth() 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 updateTouchInput(int32_t x, int32_t y, bool down) = 0; diff --git a/WindCore/windermere.cpp b/WindCore/windermere.cpp index 2de2128..7ca6a8c 100644 --- a/WindCore/windermere.cpp +++ b/WindCore/windermere.cpp @@ -369,6 +369,12 @@ void Emulator::configure() { reset(); } +uint8_t *Emulator::getROMBuffer() { + return ROM; +} +size_t Emulator::getROMSize() { + return sizeof(ROM); +} void Emulator::loadROM(uint8_t *buffer, size_t size) { 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::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) { const uint8_t *lcdBuf = &MemoryBlockC0[lcdAddress & MemoryBlockMask]; int width = 640, height = 240; @@ -565,8 +585,13 @@ void Emulator::readLCDIntoBuffer(uint8_t **lines) const { int palIdx = (byte >> shift) & mask; int palValue = palette[palIdx]; - palValue |= (palValue << 4); - lines[y][x] = palValue ^ 0xFF; + if (is32BitOutput) { + auto line = (uint32_t *)lines[y]; + line[x] = rgbValues[palValue]; + } else { + palValue |= (palValue << 4); + lines[y][x] = palValue ^ 0xFF; + } } } } diff --git a/WindCore/windermere.h b/WindCore/windermere.h index 5c5d5a2..defe545 100644 --- a/WindCore/windermere.h +++ b/WindCore/windermere.h @@ -62,6 +62,8 @@ private: public: Emulator(); + uint8_t *getROMBuffer() override; + size_t getROMSize() override; void loadROM(uint8_t *buffer, size_t size) override; void executeUntil(int64_t cycles) override; int32_t getClockSpeed() const override { return CLOCK_SPEED; } @@ -72,7 +74,7 @@ public: int getLCDOffsetY() const override; int getLCDWidth() 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 updateTouchInput(int32_t x, int32_t y, bool down) override; }; diff --git a/WindQt/pdascreenwindow.cpp b/WindQt/pdascreenwindow.cpp index 037baf7..4531c5b 100644 --- a/WindQt/pdascreenwindow.cpp +++ b/WindQt/pdascreenwindow.cpp @@ -57,7 +57,7 @@ void PDAScreenWindow::updateScreen() { QImage img(emu->getLCDWidth(), emu->getLCDHeight(), QImage::Format_Grayscale8); for (int y = 0; y < img.height(); y++) lines[y] = img.scanLine(y); - emu->readLCDIntoBuffer(lines); + emu->readLCDIntoBuffer(lines, false); lcd->setPixmap(QPixmap::fromImage(std::move(img))); } diff --git a/WindWasm/build.sh b/WindWasm/build.sh new file mode 100755 index 0000000..fb52887 --- /dev/null +++ b/WindWasm/build.sh @@ -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 \ No newline at end of file diff --git a/WindWasm/cover.svg b/WindWasm/cover.svg new file mode 100644 index 0000000..bf3eb9b --- /dev/null +++ b/WindWasm/cover.svg @@ -0,0 +1 @@ +WordSystemSheetAgendaEmailCalcJotterExtrasContacts \ No newline at end of file diff --git a/WindWasm/main.cpp b/WindWasm/main.cpp new file mode 100644 index 0000000..b785226 --- /dev/null +++ b/WindWasm/main.cpp @@ -0,0 +1,136 @@ +#include "../WindCore/emubase.h" +#include "../WindCore/windermere.h" +#include +#include +#include + +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; +} diff --git a/WindWasm/shell.html b/WindWasm/shell.html new file mode 100644 index 0000000..8aec56b --- /dev/null +++ b/WindWasm/shell.html @@ -0,0 +1,162 @@ + + + + + + WindEmu + + + +
+
emscripten
+
Downloading...
+
+ +
+
+ + +
+
+
+ +
+ +
+ +
+ + {{{ SCRIPT }}} + +