shonfrazier / lib6502 Goto Github PK
View Code? Open in Web Editor NEW6502 microprocessor emulator; library and command line utility
License: Other
6502 microprocessor emulator; library and command line utility
License: Other
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.
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, ...
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?
Line 45 in f8ab5a5
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.