JChip16BR the newest toy

Chip16

The last days I’ve been playing around a new virtual machine. This VM is known as Chip16 and its specification can be obtained from Ngemu. The Chip16 is quite simple, it has only 19 registers (including PC and SP), the CPU speed is only 1Mhz, memory’s size 64 KB (65536 bytes). The main way you to interact with the machine is by mapped io. The set of instruction (at version 0.7) is around 48. It’s a start project for those want to learn a little bit of emulation. Maybe you know its father Chip8 that has some inconsistencies and undocumented features.

Emulator Basic flow

Emulator basic flow
Emulator basic flow

Basic steps:

  • [0] Load ROM -> ROM it’s the input of the machine, sometimes it can be a ISO image or a specific structure created by community. It’s a file (structured or not) that contains the data to be executed and read by the machine.
  • [1] Load Memory -> You need to know the memory map of target system. So you properly fill the memory with data from ROM at the correct addresses.
  • [2] Initiate Machine -> It’s the initial state of the machine, some of them used to start with random data all over the RAM, and it used to have the initial address for the first opcode among other tasks.
  • [3] Decode Opcode -> It’s basically the act of read the opcode from memory (pointed by PC) and its parameters too.
  • [4] Execute Instruction -> With the full set of data, you know how to execute the instruction (using the parameters).
  • [5] Events -> Eventually the machine does some events or some interruptions are raised.
  • [6] Cycle Tasks -> This are the tasks performed at given time (measured by clock (number of cycles) or time (ms)): update screen, update inputs and etc.

JChip16BR

JChip16BR architecture
JChip16BR architecture

I did a simplest implementation using Java as language/platform and Java2D as render engine. The idea is: implement this vm and have fun with object-oriented programming. I’ve divide this into several objects. I tried to apply some design patterns, basic oop and etc. The core is the facade Machine which provide methods for start, pause, resume, see the internal state of CPU, draw and etc. The coolest part (IMHO) is the CPU, full of internal classes composing the instruction table which is findable by opcode. For graphics it used a simple short multidimensional array, acting like a screen of pixels.

Video

Source code

You can access the source code at GitHub.

PS: I used the concepts of emulator, simulator and virtual machine as synonymous but my intend was to show how a vm/emulator/simulator or mix of all this works at implementation level.

Advertisements

One thought on “JChip16BR the newest toy

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s