# Look mom, a CHIP8 CPU being emulated in Node RED

**URL:** <https://discourse.nodered.org/t/look-mom-a-chip8-cpu-being-emulated-in-node-red/83206>\
**Category:** Share Your Projects\
**Tags:** fun\
**Created:** [26 November 2023 19:28 UTC](https://discourse.nodered.org/t/look-mom-a-chip8-cpu-being-emulated-in-node-red/83206 "2023-11-26T19:28:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [26 November 2023 19:28 UTC](https://discourse.nodered.org/t/look-mom-a-chip8-cpu-being-emulated-in-node-red/83206/1 "2023-11-26T19:28:28Z")

</div>

Yup you herd me!

So...

It does not have Input (if someone is daring enough to add it - manipulating the registers, I think is the key) or Beeps, and it requires that you install the `canvas` package for Node JS.  
This package requires the following libs

- build-essential
- libcairo2-dev
- libpango1.0-dev
- libjpeg-dev
- libgif-dev
- ibrsvg2-dev

This is to enable out-of-browser canvas's

I use the `canvas` package inside the `Render Video` and `Pixel Buffer` nodes

I also use `node-red-contrib-image-tools` to display the video

- Deploy
- Load the invaders ROM (public domain) - there are a few on the web for CHIP8
- Click Start

The CPU executes various writes to the pixel buffer, and I wait for 80ms after the last write before rendering a frame - this seems to be a good middle ground for smooth video.

I print out the current executing Opcode and Args for that operation (just to feel like a real nerd 🤓 )

Anyway this was fun - so enjoy  
I'm sure some optimisations/improvements can be had

[flows.json](https://discourse.nodered.org/uploads/short-url/rfp4cy6fldz58IKfgmFzwFQJzb7.json) (30.7 KB)

![Nov-26-2023 19-06-31](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5b8345cde90115b73a4997d8f74b7f7113717746.gif)

**EDIT**

I forgot to credit the original code that helped me write it.

Based of : [GitHub - taniarascia/chip8: 🎮 ‎ A Chip-8 emulator written in JavaScript for web, CLI, and native UI.](https://github.com/taniarascia/chip8)  
Reference : [CHIP-8 - Wikipedia](https://en.wikipedia.org/wiki/CHIP-8)

---

<div class="post-metadata">

**Author:** ![Shaquu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shaquu/32/9701_2.png) [@Shaquu](https://discourse.nodered.org/u/Shaquu)\
**Post date:** [1 December 2023 15:18 UTC](https://discourse.nodered.org/t/look-mom-a-chip8-cpu-being-emulated-in-node-red/83206/2 "2023-12-01T15:18:42Z")

</div>

You are crazy 🙂

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [1 December 2023 15:21 UTC](https://discourse.nodered.org/t/look-mom-a-chip8-cpu-being-emulated-in-node-red/83206/3 "2023-12-01T15:21:24Z")

</div>

> [@Shaquu](#):
>
> You are crazy 🙂

Its the Discord community I hang around with 😉

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [2 December 2023 17:17 UTC](https://discourse.nodered.org/t/look-mom-a-chip8-cpu-being-emulated-in-node-red/83206/4 "2023-12-02T17:17:35Z")

</div>

So....

For those monitoring this:

- Pointless
- Silly
- But Crazy achievement in Node RED

If not following: What has been achieved here, is Emulating A CHIP8 Console CPU with **Node RED only**  
It's now become quite a project of mine, and has become an almost Full CHIP8 Console.

The following changes/break throughs have been made since I last posted:

- No longer needs **ANY** external lib/Node RED Module
- The rendering is now achieved with a Native Canvas object in a browser window ( **HTTP IN** )
  - I have employed a type of RFB to send pixel changes over a web socket to the Browser window

- Now Supports Input. YES!!! you can now play the games 😃
- Now renders Audio - square wave only. YES!!! audio is now rendered
- Reduce the use of `function` nodes where possible (@TotallyInformation😉 )

This started as a fun time waster, and I quickly realised what has been achieved here...  
A CPU Stack being emulated / Run inside a Node RED flow!

It's pretty crazy to think, you can see the CPU being executed in a flow.

1. Import The flow (again, no external libs or Node RED modules are needed - **its using all core nodes** )
2. Deploy
3. On a new browser tab - Go to [http://x.x.x.x:1880/chip8](http://x.x.x.x:1880/chip8)
4. Click the title (this is to allow audio to be played - most browsers require interaction for audio to be allowed) - the video panel should go black
5. Go back to the flow editor, Inject your chosen ROM (I have included 2)
6. Inject `Start`

There are some CPU instructions that I need to fix - but mostly its playable.  
To load a new ROM - you have to redo the above steps (you might need to disable/enable the flow - Hey it's a Console being emulated in Node RED, give me a break 😉

Enjoy.

Keys for Space Invaders as example.

**W** - Start new Game / Fire  
**Q** - Move Left  
**E** - Move Right

 ![Screenshot 2023-12-02 at 17.08.26](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bc7a117044847b8149f1973c9cb085d009ec97b8.png)

 ![Screenshot 2023-12-02 at 17.13.45](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6b67f18fa7657ad9fb8a1eac1e9515a2dc7dd37c.png)

**Flow**

[flows-2.json](https://discourse.nodered.org/uploads/short-url/wQZibm24GL12UKkw08MGktDGsTk.json) (33.4 KB)

This whole thing started when @cymplecy implemented an MQTT Broker in Node RED - so wanted to push the bounderies of what is possible after seeing his work.

> [@\[Fun Exercise\]Make an MQTT broker in Node-RED](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229):
>
> For fun, I thought - let's build an MQTT broker in Node-RED! slight_smile Anyway, I soon realised that I didn't know how to setup the TCP nodes properly - I often get confused by them frowning Can someone clue me in on what I should stick in the TCP in/out nodes (or whether I should be using the TCP request one instead?) Image of my initial flow idea (plan on using port 2883)

Have a great weekend!

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 December 2023 11:29 UTC](https://discourse.nodered.org/t/look-mom-a-chip8-cpu-being-emulated-in-node-red/83206/5 "2023-12-03T11:29:43Z")

</div>

Small Update

- I have updated the CPU Clock - it now runs a lot faster
- You can now load a new ROM without reinitializing everything  
Just be sure to inject `Start` after loading a new ROM - and to follow the initial Init routine above

![Dec-03-2023 11-27-33](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/eaeada576a725d53516e690666fa2740c885d85f.gif)

Flow  
[flows-3.json](https://discourse.nodered.org/uploads/short-url/7Rew6uXKOP5goElZOwXUTAeZi38.json) (33.7 KB)

---

<div class="post-metadata">

**Author:** ![nico25](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nico25](https://discourse.nodered.org/u/nico25)\
**Post date:** [7 December 2023 20:37 UTC](https://discourse.nodered.org/t/look-mom-a-chip8-cpu-being-emulated-in-node-red/83206/6 "2023-12-07T20:37:59Z")

</div>

> Keys for Space Invaders as example.
> 
> **W** - Start new Game / Fire  
> **Q** - Move Left  
> **E** - Move Right

I was about to write something very positive about your work, but then i realised what abomination of inputs you used. How dare you to ignore the mighty **WASD**?! 🤣

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [7 December 2023 20:41 UTC](https://discourse.nodered.org/t/look-mom-a-chip8-cpu-being-emulated-in-node-red/83206/7 "2023-12-07T20:41:44Z")

</div>

You can probably change it all.  
But the array in the page Node for the map will need updating + maybe updating the CPU instruction for Input. It's shifting bits based on the index

it was based on the work here: [GitHub - taniarascia/chip8: 🎮 ‎ A Chip-8 emulator written in JavaScript for web, CLI, and native UI.](https://github.com/taniarascia/chip8)
