Skip to content

An Emulated CPU written in NodeJS with its own instruction set (proof of concept)

License

Notifications You must be signed in to change notification settings

itwithlyam/NodeCPU

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NodeCPU

NodeCPU is a "virtual computer" built in NodeJS. While this isn't exactly a proof-of-concept, it is still a bit of an experiment.


Application Binary Interface

This is NodeCPU ABI v0.0.3. This version includes the new stack.

Instruction set

Opcode Name Description Notes Link
01 /rn imm16 mov r,imm16 Put an immediate into a register mov
02 imm16 push imm16 Push an immediate onto the stack push
03 /rn inc r Increment a register by 1 inc
04 /rn pop r Pop from the stack to a register pop
05 /rn imm16 add imm16,r Add immediate to a register add
07 addr16 jmp addr16 Jump to an address in memory jmp
08 /r mov r,r Put the contents of a register into a register mov

Low Level System Information

Memory Map

Addr Desc
0x8065 End of memory
0x8000 Stack
0x4000 Randomly Accessible Memory (RAM)
0x0000 Read-Only Memory (ROM)

The entry point for programs is 0x0000. ROM can only be modified in "entry mode".

Stack

At memory address 0x8000, there are 100 bytes allocated to the stack. You can use push and pop to interact with the stack. The stack itself operates on a FILO1 (First In; Last Out) system and the stack pointer is kept in register SP. When the stack pointer reaches 0x64 it goes back to 0x00 until it reaches existing elements, when it will throw a Segmentation Fault. Be careful!

Modes

Mode Byte Features When to use
Entry 0x00 Full access to processor, including ROM While initialising
Boot 0x01 Access to bootloader and RAM When booting an operating system
Protected 0x02 Access to RAM When in an operating system

To switch modes, put the byte into Register MR.

Calling Convention

General-purpose registers

These registers can be used for any operation, but the recommened uses are as follows:

Name Recommended Use RM Nibble
RA Temporary storage/Parameter 1 0x1
RB Parameter 2 0x2
RC Counter/Parameter 3 0x4
RD Result/Parameter 4 0x5

Special registers

These registers can only be used for the operation specified.

Name Use RM Nibble
MR Mode Register 0x3
SP Stack Pointer 0x6

Definitions

Immediate: A value or address in memory 2
RAM: Randomly Accessible Memory
ROM: Read-Only Memory
RM Byte: A byte with two registers (nibbles)3

Footnotes

  1. See https://techterms.com/definition/filo for more details.

  2. Uses big endian

  3. 01: Input = RA, Target = RB. Used for transferring data between registers. In instruction set docs is shown with /r. If the opcode has /rn, the target should be left blank.

About

An Emulated CPU written in NodeJS with its own instruction set (proof of concept)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published