How to write a simple simulator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ciko

    How to write a simple simulator

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

    #2
    Re: How to write a simple simulator

    On Thu, 23 Feb 2006 14:58:24 GMT, Ciko <cicko@ciko.i t> wrote in
    comp.lang.c:
    [color=blue]
    > Was wondering how I could write a simple simulator
    > (assembler CPU Z80). Thanks for any advice, link.[/color]

    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.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++

    Comment

    Working...