How to write a simple simulator

C

Ciko

Was wondering how I could write a simple simulator
(assembler CPU Z80). Thanks for any advice, link.
 
J

Jack Klein

Was wondering how I could write a simple simulator
(assembler CPU Z80). Thanks for any advice, link.

While getting into the specific details of the Z80 would be off-topic
here, I can offer some basic ideas that do not go beyond the range of
standard C. In general, defining a simulator for a target processor
"smaller" than the computer that will execute the simulator is not too
difficult.

First, define structure types to represent the registers of the
processor. For the Z80 you would need three types, I would think.

One type for A and flags (two instances of these).

One type for BC, DE, HL (two instances of these).

One for the other registers, SP, PC, IX, IY.

I would use a pointer to each of the first two types to address the
contents, and swap the address in the pointer to appropriate instance
on exchange instructions.

As for the instruction simulation, all Z80 instructions start with a
single (8-bit) byte, so you can build either a switch table with 256
cases, or 256 pointers to functions. The functions/switches for the
bytes that are prefixes (0xed, 0xdd, 0xfd, isn't there one more?)
would each be processed using a secondary switch table or array of
function pointers.

Of course you need to have a 64K byte block of memory to serve as the
Z80 address space, and the value in your simulated PC register, and
all other values used as addresses, would actually be offsets into
this block.

The moderately tricky part would be updating the emulated flags after
each instruction. The really tricky part would be figuring out what
to do with access to I/O space. Emulating actual peripherals can be
quite complicated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top