Giter Club home page Giter Club logo

6502's Introduction

MOS Technology 6502 CPU Emulator

This is a very accurate 6502 emulator I wrote many years ago. It's very fast and small (although there are faster ones written in assembly), its structure is very clear and the code is commented.

If you are looking for an accurate MOS Technology 6502 CPU emulator for your project maybe you have found the correct one.

Building

In order to compile you must install Z, a header only library which provides types, macros, inline functions, and a lot of utilities to detect the particularities of the compiler and the target system. This is the only dependency, the standard C library and its headers are not used and the emulator doesn't need to be dynamically linked with any library.

A Xcode project is provided to build the emulator. It has the following targets:

Targets

Name Description
dynamic Shared library.
module Module for A.C.M.E.
static Static library.
static+ABI Static library with a descriptive ABI to be used in modular multi-machine emulators. Building the ABI implies callback pointers with slots enabled.
static+ABI-API The same as above but exposing only the symbol of the ABI, the API functions will be private.

Constants used in 6502.h

Name Description
CPU_6502_STATIC You need to define this if you are using the emulator as a static library or if you have added its sources to your project.
CPU_6502_USE_SLOTS Always needed when using the ABI. Each callback function will have its own context pointer, which should store the address of the object assigned to the call.

Constants used in 6502.c

Name Description
CPU_6502_DYNAMIC Needed to build a shared library. Exports the public symbols.
CPU_6502_BUILD_ABI Builds the ABI of type ZCPUEmulatorABI declared in the header with the identifier abi_emulation_cpu_6502.
CPU_6502_BUILD_MODULE_ABI Builds a generic module ABI of type ZModuleABI. This constant enables CPU_6502_BUILD_ABI automatically so abi_emulation_cpu_6502 will be build too. This option is intended to be used when building a true module loadable at runtime with dlopen(), LoadLibrary() or similar. The module ABI can be accessed retrieving the weak symbol __module_abi__.
CPU_6502_HIDE_API Makes the API functions private.
CPU_6502_HIDE_ABI Makes the abi_emulation_cpu_6502 private.
CPU_6502_USE_LOCAL_HEADER Use this if you have imported 6502.h and 6502.c to your project. 6502.c will include "6502.h" instead of <emulation/CPU/6502.h>.

API

m6502_run

Description
Runs the CPU for the given number of cycles.

Prototype

zsize m6502_run(M6502 *object, zsize cycles);

Parameters
object → A pointer to an emulator instance.
cycles → The number of cycles to be executed.

Return value
The number of cycles executed.

Discusion
Given the fact that one 6502 instruction needs between 2 and 7 cycles to be executed, it is not always possible to run the CPU the exact number of cycles specfified.

m6502_power

Description
Switchs the CPU power status.

Prototype

void m6502_power(M6502 *object, zboolean state);

Parameters
object → A pointer to an emulator instance.
stateON / OFF

m6502_reset

Description
Resets the CPU by reinitializing its variables and sets its registers to the state they would be in a real 6502 CPU after a pulse in the RESET line.

Prototype

void m6502_reset(M6502 *object);

Parameters
object → A pointer to an emulator instance.

m6502_nmi

Description
Performs a non-maskable interrupt. This is equivalent to a pulse in the NMI line of a real 6502 CPU.

Prototype

void m6502_nmi(M6502 *object);

Parameters
object → A pointer to an emulator instance.

m6502_irq

Description
Switchs the state of the maskable interrupt. This is equivalent to a change in the IRQ line of a real 6502 CPU.

Prototype

void m6502_irq(M6502 *object, zboolean state);

Parameters
object → A pointer to an emulator instance.
stateON = set line high, OFF = set line low

Callbacks

Before using an instance of the 6502 emulator, its cb structure must be initialized with the pointers to the callbacks that your program must provide in order to make possible for the CPU to access the emulated machine's memory.

read

Description
Called when the CPU needs to read 8 bits from memory.

Prototype

typedef zuint8 (* ZContext16BitAddressRead8Bit)(void *context, zuint16 address);
ZContext16BitAddressRead8Bit read;

Parameters
context → A pointer to the calling emulator instance.
address → The memory address to read.

Return value
The 8 bits read from memory.

write

Description
Called when the CPU needs to write 8 bits to memory.

Prototype

typedef void (* ZContext16BitAddressWrite8Bit)(void *context, zuint16 address, zuint8 value);
ZContext16BitAddressWrite8Bit write;

Parameters
context → A pointer to the calling emulator instance.
address → The memory address to write.
value → The value to write in address.

History

  • v1.0.0 (2016-07-05)
    • Initial release.

6502's People

Contributors

vijayparikh avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.