Skip to main content

Emulator Deep Dive - The Instruction Set: Part 1

I thought some of you might appreciate hearing about the internals of my emulator. In this post I'm going to introduce the instruction set, and cover the emulated memory model and registers. This is just the first post in this series, and will cover miscellaneous instructions. I'm keeping each post small, for easy digestibility.

Prerequisites

There are a couple of prerequisites to fully understanding this post. It will assume you know what a stack is and that you have some knowledge of assembly language. But the content could still be interesting even without that knowledge. I'll leave it to you to decide.

Registers/Memories

Most instructions involve the 256-byte data stack. There's also a 256-byte return stack to allow for subroutine returns. These stacks each have a stack pointer register that is not user accessible.

Addressable memory consists of 64KB of RAM, divided into the direct-page's 256 bytes at the beginning of memory, and then the remaining 64KB - 256 byte memory.

There are two directly user-accessible registers, DP and X. These offer the only means of accessing RAM, through register indexed access.

DP is an 8-bit register meant to serve as an index into the direct page. It offers pre- and post-decrement on store/read, but can also be used as a general purpose, 8-bit register.

X is a 16-bit register meant to serve as an index into the entirety of the 64KB of memory. It offers pre- and post-decrement on store/read, but can also be used as a general purpose register.

The remaining registers, CC and PC, are the condition and program counter registers, respectively. They are not directly user accessible, but affect, and are affected by, the operation of certain instructions. This will be discussed in future posts.

Instruction Types

There are four types of instructions available to programmers:

  • Stack instructions - instructions that add to or remove items from the stack, or otherwise manipulate the positions of items on the stack
  • ALU instructions - instructions that perform operations on the items at the top of the stack, such as addition, subtraction, or logic operations
  • Flow control instructions - instructions that affect the program counter to change the execution flow of a program
  • Miscellaneous instructions - instructions not falling into the other three categories, and the subject of this post

Miscellaneous Instructions

There are two miscellaneous instructions, SYSCALL, which triggers a system call, specified by its single byte operand, and SYNC, which exits the instruction execution to synchronize with the rest of the emulator loop.

SYSCALLs often require parameters, which are passed on the stack. The list of system calls is available in the source tree, under source/include/syscall.h. I'll be writing up another post in the future on the parameters and operation of different syscalls.

SYNC will interrupt instruction execution and allow the rest of the emulator's main loop to process. This was mainly intended to update the bitmapped screen from the emulated memory to the SDL window displaying the image.

SYSCALL also interrupts instruction execution in order to execute the host-side code that implements the system call. In essence, SYNC acts as a NOP syscall, since it interrupts instruction execution, but doesn't execute any code.

Next Up

The next article will focus on stack instructions; that is, instructions which manipulate the items on the stack without arithmetic or logic operations. This includes adding to and removing from the stack, moving items around on the stack, and setting or saving the DP and X registers.

Comments

Popular posts from this blog

What Kind of Games?

I started programming when I was young, with the hopes of writing video games. I think a lot of kids start that way. When you like something, or someone, you try to emulate what you’re seeing. But how has that early dream turned out? They tell writers to write what they know. It’s good advice. How can you write about life in the Serengeti without have someone to give you a first hand account or having been there yourself? You can always use your imagination, and that’s all you can really do when writing fantasy or science fiction. It works for writing video games. How can you expect to write a genre you don’t immerse yourself in? These days I spend most of my gaming time playing casual games. I’m busy doing other things, and don’t want to spend long stretches just sitting at the console or computer. Recently I read an article about the kind of video games the most people tend to flock to. Typically they’re games that are relatively simple and involve sorting things in some way. It

Am I Jonesing for the Internet?

I’m feeling a little agitated and jittery today. My internet access is down due to some nasty snow and wind. Are the two related? They might be. I know I’m certainly missing my twitter friends and feeling less in touch with the world. How long is this weather going to hold? I can’t look that up. Sure, I could pull out a radio and listen in, if I had one. I might somewhere, but I’m at the mercy of the broadcaster to decide when to report the weather and how much of it to report. Some argue that internet access should be a basic human right. Does this point of view hold water? I suppose it could be argued that since the internet allows us to draw together into a larger community that it is an essential part of improving the human condition. Its use in political organizing and to connect dissidents in repressive regimes can certainly help make the case for it as a basic human right. Is the jitteriness really from not having the internet? My doctor did just increase my dose of modafi

Piet - an esoteric programming language

There’s a certain group of programmers out there that like to come up with programming languages just for the fun of it. Some of them have profanity as their names , and some are based on internet memes . Whatever the case may be, some individual out there enjoyed thinking up the language, and many of these languages are actually useable. One esoteric language that stands out, for me, at least, is Piet , created by David Morgan-Mar. Based on the idea of making programs that look like abstract art, Piet allows the programmer to express their software in the form of coloured blocks. Numbers are represented by blocks of pixels containing a pixel count equal to the number itself. Operations are performed by changes in hue or darkness. As an example, here is a Piet program I wrote to output the string “Hello World”. This image is in fact the entirety of the program, and can be run in any of the Piet interpreters out there. Other examples of Hello World programs are available on David’s si