Git Product home page Git Product logo

lib6502's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

lib6502's Issues

I'd like to contribute a Python ctypes interface to lib6502

I am not sure if I should fork the project and do the usual PR stuff, or if I can submit a Python interface library this way. Please let me know. Having a Python ctypes interface, allows for quick prototyping, and testing of lib6502-based projects. The library can as a result be played with using the standard Python interpreter opening up the library to anyone who might want to play around with the library, but might not have C programming skills. In order to get this to work on Linux, I needed to modify the CFLAGS in the Makefile to include "-fPIC", then I was able to generate a shared library by running "gcc -shared -o lib6502.so lib6502.o". Once a shared library exists, the following Python module can be written and used to connect with lib6502:

from ctypes import *

class M6502(Structure):
    pass

class M6502_Registers(Structure):
    _fields_ = [("a", c_uint8),
                ("x", c_uint8),
                ("y", c_uint8),
                ("p", c_uint8),
                ("s", c_uint8),
                ("pc", c_uint16)]

M6502_Callback = CFUNCTYPE(c_int, POINTER(M6502), POINTER(c_uint16), POINTER(c_uint8))

M6502_CallbackTable = M6502_Callback*0x10000
M6502_Memory = c_uint8*0x10000

class M6502_Callbacks(Structure):
    _fields_ = [("read", M6502_CallbackTable),
                ("write", M6502_CallbackTable),
                ("call", M6502_CallbackTable)]

M6502._fields_ = [("registers", POINTER(M6502_Registers)),
                  ("memory", POINTER(c_uint8)),
                  ("callbacks", POINTER(M6502_Callbacks)),
                  ("flags", c_uint),
                  ("ticks", c_ubyte)]

M6502_Ptr = POINTER(M6502)

l = cdll.LoadLibrary('./lib6502.so')

M6502_new = l.M6502_new
M6502_new.restype = M6502_Ptr
M6502_new.argtypes = [POINTER(M6502_Registers), M6502_Memory, POINTER(M6502_Callbacks)]

M6502_reset = l.M6502_reset
M6502_reset.argtypes = [POINTER(M6502)]

M6502_nmi = l.M6502_nmi
M6502_nmi.argtypes = [POINTER(M6502)]

M6502_irq = l.M6502_irq
M6502_irq.argtypes = [POINTER(M6502)]

M6502_run = l.M6502_run
M6502_run.argtypes = [POINTER(M6502)]

M6502_tick = l.M6502_tick
M6502_tick.argtypes = [POINTER(M6502)]

M6502_step = l.M6502_step
M6502_step.argtypes = [POINTER(M6502)]

M6502_delete = l.M6502_delete
M6502_delete.argtypes = [POINTER(M6502)]

CALLBACK_LIST = []

def M6502_getCallback(mpu, typ, addr):
    return getattr(mpu.contents.callbacks.contents, typ)[addr]

def M6502_setCallback(mpu, typ, addr, fn):
    cb_func = M6502_Callback(fn)
    CALLBACK_LIST.append(cb_func)
    getattr(mpu.contents.callbacks.contents, typ)[addr] = cb_func

def M6502_getVector(mpu, vec):
    return (mpu.contents.memory[vec] << 8)+mpu.contents.memory[vec+1]

def M6502_setVector(mpu, vec, addr):
    mpu.contents.memory[vec] = addr & 0xff
    mpu.contents.memory[vec] = addr >> 8

This code is still rather primitive, and I plan on updating it to be class-based, here's a basic example of how to use it:

from lib6502 import M6502_new, M6502_Memory

mem = M6502_Memory()
mpu = M6502_new(None, mem, None)

I have tested the various functions, such as reading and writing memory, setting callbacks, among other things. I plan on doing a more fuller test soon, and making it actually run 6502 code.

Hopefully this Python module can be helpful to some, and can be included with lib6502 as a way to easily tinker and prototype with.

Examples no longer work for new macOS

Even I copy the lib6502.h to examples and use instead of gcc-11 (not clang), the ld cannot run to get the examples to work. Guess it is so many years, ...

Shouldn't the M6502 flags be an unsigned char?

I was working with this library and using the struct in my code, and began to realize that the flags are being set as an unsigned int, which is 16-bits wide. However, I do believe that the MOS 6502 flags are actually 8-bits wide, and this should instead be an unsigned char. Is there a specific reason why it was set up this way?

unsigned int flags;

Something odd and interesting to note, is that when I used h2pas to make the library compatible with FreePascal, it decided to mark it was a dword, which is 32-bits long, but that's more of an issue with h2pas and not this code, but I just thought I'd make mention of it, as I thought it was rather odd. Otherwise, using this library in FreePascal and Lazarus works rather well. Both compiled into the binary itself, and as an external shared library.

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.