HP Forums
Emu48 Javascript ? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: Emu48 Javascript ? (/thread-9952.html)

Pages: 1 2 3


Emu48 Javascript ? - sunhp - 01-16-2018 04:38 PM

Hello All,

A good way to spread the HP48 huge games and programs collection (lost since HP Prime move) could be having a Javascript version of Emu48.

One say pretty ambitious (not to say impossible) ?

Check out those awesome TiXX Dark sided online emulators :

https://www.cemetech.net/projects/jstified/
https://www.ocf.berkeley.edu/~pad/emu/v11.html

Hope someone could do it or at least start doing it ;-)

Julien
http://jadegame.com/games/hp48/


RE: Emu48 Javascript ? - TheKaneB - 01-16-2018 05:51 PM

the best way to have a solid JS engine would be to write one from scratch, maybe translating an existing C or C++ engine.

Another way, not so great but "hey!", would be to use an automatic translator such as this one by Mozilla: http://kripken.github.io/emscripten-site/

https://kripken.github.io/mloc_emscripten_talk/gindex.html#/

If someone would like to start a project like this, I might bring some help with my C++ and JS knowledge, but I have no spare time to do it myself unfortunately.


RE: Emu48 Javascript ? - sunhp - 01-18-2018 11:18 PM

Wow thanks for sharing this awesome project : EmScripten

I wonder how awesome could a version of jsEmu48 be and I think it could be very nice.

I have fix a few things inside the Emu48 Mac port project to run on newest macOS's versions 10.10+ so I see a little the emu48 logic.

The next thing to do is probably to port Emu48's Window/Mac related code into SDL to get at least the basic stuff (LCD Screen, Keyboard, Import Object..)

Then convert the whole project from C to JS using Emscripten..


RE: Emu48 Javascript ? - TheKaneB - 01-18-2018 11:27 PM

If you want a very good HTML / JS emulator, the best thing would be to convert to JS only the emulation engine and to export all the keyboard logic to the HTML side, and the display rendering to a set of canvas routines. Web Storage API could be used to transfer files between the PC and the emulated calculator.

It's not trivial, but it's not too difficult either.


RE: Emu48 Javascript ? - TheKaneB - 01-18-2018 11:29 PM

As a side note: EmScipten is fully supported by Mozilla and it has been used to successfully port the Unreal Engine to Javascript and also have been used to run a JS-converted version of the ffmpeg codec suite to decode H.264 streams in real time. The performance of this solution is really good.


RE: Emu48 Javascript ? - sunhp - 01-18-2018 11:43 PM

(01-18-2018 11:29 PM)TheKaneB Wrote:  As a side note: EmScipten is fully supported by Mozilla and it has been used to successfully port the Unreal Engine to Javascript and also have been used to run a JS-converted version of the ffmpeg codec suite to decode H.264 streams in real time. The performance of this solution is really good.

And also Quake3 : http://www.quakejs.com/
I tried it and it's so incredible.


RE: Emu48 Javascript ? - TheKaneB - 01-19-2018 09:06 AM

Quake 3 is at least 2 orders of magnitude less complex than Unreal Engine Smile


RE: Emu48 Javascript ? - sunhp - 01-19-2018 03:55 PM

(01-19-2018 09:06 AM)TheKaneB Wrote:  Quake 3 is at least 2 orders of magnitude less complex than Unreal Engine Smile

And Quake3 is 2 magnitude orders of my own home made 3D game & engine Halloween 3D : http://jadegame.com/games/halloween/
Big Grin Big Grin


RE: Emu48 Javascript ? - sunhp - 01-19-2018 10:48 PM

I found this one HPEMU (C) Daniel Nilsson 2002, written in C and Allegro and probably more emscripten portable :

https://sourceforge.net/projects/hpemu/files/hpemu/v0.9.0/
http://liballeg.org/
Allegro + emscripten : https://listengine.tuxfamily.org/lists.liballeg.org/allegro-developers/2014/06/msg00023.html
Allegro + emscripten quick demo : http://zxstudio.org/projects/allegro/skater/skater_r.html

[Image: hpemu.png]


RE: Emu48 Javascript ? - TheKaneB - 01-19-2018 11:14 PM

I don't know, Allegro is a dog Big Grin its API is very badly designed. The key here is to NOT port the Allegro stuff, but only the core emulation. Allegro is just a bunch of routines for display and keyboard management, the same routines that you would probably want to rewrite from scratch anyway, to take advantage of all the HTML goodies (such as canvas)


RE: Emu48 Javascript ? - sunhp - 01-20-2018 12:38 AM

(01-19-2018 11:14 PM)TheKaneB Wrote:  I don't know, Allegro is a dog Big Grin its API is very badly designed. The key here is to NOT port the Allegro stuff, but only the core emulation. Allegro is just a bunch of routines for display and keyboard management, the same routines that you would probably want to rewrite from scratch anyway, to take advantage of all the HTML goodies (such as canvas)

I know but it's ok since it's just a bridge. Go ahead and let me know how a C code can display pixels to a canvas without using SDL or OpenGL?


RE: Emu48 Javascript ? - TheKaneB - 01-20-2018 12:53 AM

That's the easy part.
The emulator engine should have some sort of framebuffer for the display, which usually is just a byte array in the form of:

int displayHeight = 160; // or whatever
int displayWidth = 120;
uint8 * frameBuffer = (uint8 *)malloc(sizeof(uint8) * displayHeight * displayWidth);

The emulator will set / clear a pixel with code like this:

void setPixel(int x, int y, uint8 value) {
frameBuffer[x + y*frameBufferWidth] = value;
}

When the rendering is completed, the C code should call some kind of callback function telling the UI code that it's time to refresh the display, and it will pass the frameBuffer object.

That's the right time to handle the object over to the canvas routine. You will just copy the pixels and apply the necessary scale and filter.

Please note that after the engine will be converted from C to JS using EmScripten, the framebuffer object will be converted to a similar javascript array in the form:

frameBuffer = [1, 0, 0, 1, 1, 1, 0, 0, ....]; // we just have monochrome pixels here

so the glue code to render it into canvas will be very short:

var canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d"),

// Get a pointer to the current location in the image.
var palette = ctx.getImageData(0,0,160,120); //x,y,w,h
// Wrap your array as a Uint8ClampedArray
palette.data.set(new Uint8ClampedArray(frameBuffer));
// Repost the data.
ctx.putImageData(palette,0,0);

Actually, you might want to scale the pixel values from 1 to 255 and invert their values (ie: a clear pixel will have value 0, but 0 is usually associated with black and you want white instead).
Anyway, that's just little details that you can figure out once you know exactly the data format of the framebuffer.


RE: Emu48 Javascript ? - TheKaneB - 01-20-2018 01:13 AM

Anyway, I don't know the differences between the various emulators of the 48 series. Is there a "best" one? If so, I would ignore everything else and concentrate my efforts on the best available emulator.
Don't go for an emulator just because is "easy", go for the ones that are "good" Smile

If the code is too difficult to follow I will try to help as much as I can. I worked on so many projects ranging from "pretty decent" to "oh my god, who's the dog who wrote this junk?", so I have developed a spidy-sense for figuring out shitty code Big Grin


RE: Emu48 Javascript ? - sunhp - 01-20-2018 01:11 PM

(01-20-2018 01:13 AM)TheKaneB Wrote:  Anyway, I don't know the differences between the various emulators of the 48 series. Is there a "best" one? If so, I would ignore everything else and concentrate my efforts on the best available emulator.
Don't go for an emulator just because is "easy", go for the ones that are "good" Smile

If the code is too difficult to follow I will try to help as much as I can. I worked on so many projects ranging from "pretty decent" to "oh my god, who's the dog who wrote this junk?", so I have developed a spidy-sense for figuring out shitty code Big Grin

Actually it's more a matter of removing the Windows.h / MacOS code rather than "easy" or "hard" core emu code. Emu48 is a genius master piece code made by Sebastien "sebc" Carlier in 1995 (epita).
In other hand hpemu seems very good with a few Allegro stuff easily removable I think.


RE: Emu48 Javascript ? - TheKaneB - 01-20-2018 05:20 PM

Unfortunately I don't have time to dig into the code right now, but as I understand, Emu48 should already have some sort of fork for iOS and Android, so maybe someone has already done a bit of refactoring? If that's the case, one can start from the Android fork, which should have all the Windows stuff stripped down and the UI should be (hopefully) isolated from the engine code.


RE: Emu48 Javascript ? - sunhp - 01-21-2018 12:00 AM

Actually iOS and Android Emu48 versions authors didn't share their sources and they are selling/distributing it on the AppStore and PlayStore. Good point to notice any Emu48 fork has core emu code isolated and written in pure C by Sebastien Carlier ( was a MS DOS project at start ).

Christoph Gießelink did a LOT of improvements on the Window version and Cyrille de Brebisson also add a lot of Apple stuff and also HP49 flash memory addition and probably other things I miss.

Emu48 Core code is complex to follow and use a LOT of genius optimisations you wont want to know Smile I need to dig more into the code though and as CTO I'm heavily busy too.

As a side note my little contribution was based on a Project Lead Da Woon Jung and a port by Pierre Tardy on Mac OS 9, my contribution is buildable and runnable under latest macOS 10.11 and that would be a good starting point for open-source iOS version, if some developer would.


RE: Emu48 Javascript ? - TheKaneB - 01-21-2018 12:17 AM

Too bad that the mobile versions are closed source. I assumed they all shared the same license, but apparently I was wrong.

Anyway, it looks like neither of us have much time to play with it! I'm in the middle of a side project whish is eating all my spare time, and that I absolutely have to finish ASAP, but when I'm done with it I might take a closer look at this whole js 48 emulation idea of yours Smile

It's been a while, at least 3 or 4 years, since I've done any serious work in C or C++, and a departure from the routine boring web & apps development may be a good idea Big Grin


RE: Emu48 Javascript ? - sunhp - 01-21-2018 01:56 PM

Sounds great and I plan to check out ASAP how Emu48 core could run with an array as LCD screen. Great to share ideas with you and Emscripten is definitely a must try ;-)


RE: Emu48 Javascript ? - sunhp - 01-21-2018 09:07 PM

TIMER.C has contains Windows.h code such as :
uT2TimerId = timeSetEvent(uDelay,0,&TimeProc,2,TIME_ONESHOT)

And here is the MacOS version in Objective C :
uT2TimerId = [[NSTimer scheduledTimerWithTimeInterval:(uDelay/1000.0) target:self selector:@selector(TimeProc:) userInfo:nil repeats:NO] retain];

There are other OS Windows specific ( or to Mac ) C files to be refactored dunno how before being able to run through Emscripten the whole project :

THREADS.C
FILES.C
KML.C
STACK.C

Definitely not an quick job to convert all this stuff from Emu48.


RE: Emu48 Javascript ? - sunhp - 01-21-2018 09:23 PM

Droid48 Android is open sources : https://github.com/shagr4th/droid48
Let's check that ;-)