Technology

Show HN: Transputer emulator in JavaScript (fast enough to be useful)

This is a Javascript port of my transputer emulator written originally in C for my series of articles about the transputer processor. In the old times, Javascript was an interpreted language, but since many years ago it is implemented as a JIT (Just-In-Time) compiler, so it can approach speeds closer to the C language.

Other implementation caveat here is that Javascript treats bitwise operators as generating 32-bit signed integers. This was avoided using the

>>> 0

operator (logical right-shift) because it generates a 32-bit unsigned integer. You can try

0x80000000 >> 0

and see how it gives back a negative number, while

0x80000000 >>> 0

preserves the unsigned value.

Another “new” thing in Javascript is Uint8Array, it is way faster than a normal array and saves memory, as there are several of these arrays (including one of 40MB to hold the entire hard disk drive!). For the drive images, I tried using an initialized array but the source code used 20 megabytes for a binary of 3 megabytes. I figured a good trick to reduce the Javascript source code size using Base64 in my bin2.c utility to encode the 3MB of data for the hard disk drive, and window.atob gets a decoded string that is copied directly to the hard disk drive’s Uint8Array.

I didn’t expected to have a floating-point emulation, but I discovered the Float32Array and Float64Array types (added in 2010) and how these can be shadowed with an Uint32Array, so the emulator can work with real floating-point math, and uses the Uint32Array to get the floating-point binary data to replicate in the transputer internal memory.

Finally, for the display I was pretty fortunate to find jsTerm by Peter Nitsch with a compatible license. I removed the Telnet and sockets portion. And as it used a bitmap for the font, I was able to replace it with my original font from 1996 and use it, so you can see the single-line borders for the text windows, and other special characters. My font was based on ECMA-94 (charset 1), also known as ISO-8859-1, with the $00-$1f and $80-$9f characters filled with graphics. It is based on a VGA BIOS font, and I think I improved some letters, but I don’t remember which ones. Try running the

C:Conjunto

command to see all the letters and symbols.

You can give a look to the Javascript source code for this emulator at https://github.com/nanochess/transputer/tree/master/js

What do we have here?

This emulator doesn’t have options to run my early Pascal compiler or the Small-C compiler, instead it comes preloaded with my full-blown operating system (Spring 1996), it includes a lot of features like multiple drives (A: is a 1.44mb floppy, B: is a 512K RAM disk, C: is a 40 MB hard drive, D: is a CD-ROM in ISO-9660 format but no image can be loaded).

The C compiler supports the full K&R syntax (except for static and extern, because there’s no linker).

Some commands you can test inside the operating system:

        DIR A:
        DIR C:
        AYUDA
        MEM
        C:EDITOR
        

Use F1 (or Fn+F1 in macOS) to access the help box of the visual text editor, and type F4 (or Fn+F4 in macOS) to open the directory browsing for reading text files.

Use

C:CC

to invoke the C compiler,

C:ENS

to invoke the assembler,

C:EJECUTABLE

to build assembler output into a working executable. There are instructions for compiling programs in the

C:/Documentos/Programas.doc

file (you could try self-compiling the C compiler!)

This is an example compilation of a program:

        C:CC
        N
        N
        C:/C/Hora.c
        B:Hora.len
        
        C:ENS
        B:Hora.len
        C:/Lib/stdio.len
        [empty line]
        B:Hora.e
        
        C:EJECUTABLE
        B:Hora.e
        512
        0
        C:Hora.p
        

My emulator in C is way faster than this demo, but if you have a speedy computer and a good browser, this Javascript version can be very interesting.

3D modeler

You can run the 3D modeler using this:

            c:modela
            c:dibujo_3d/escena_tetera
        

Select a mode, press Enter, and then press Enter again to start displaying. There are more scenes in the

C:/Dibujo_3D

directory, each one starts with the name escena_. You can list the scenes using this command:

            dir c:/dibujo_3d/escena*
        

Ray tracer

You can run the Ray Tracer using this:

            c:m3d
            c:dibujo_3d/prueba
        

Only two M3D files are available in the

C:/Dibujo_3D

directory. You should enter the names without the extension.

Multitasking

There is a small example of multitasking, you can have a clock at the top-right of the screen. Type this command:

            @c:/Desarrollo/Reloj
        

Source code

Enjoy it! Did you like this? Invite me a coffee on ko-fi!

Related links

Last modified: Apr/02/2025

Related Articles

Back to top button