Py65: 6502 Microprocessor Simulator

  • Posted by Mike Naberezny in Hardware,Python

    I’ve started a new Python project called Py65. It aims to provide building blocks for modeling 65xx microcomputer systems in software, with a focus on homebuilt hardware. Using simulation, embedded systems software can be developed and tested much faster.

    In its current form, Py65 supports the original NMOS 6502 microprocessor from MOS Technology. The venerable 6502 and variants of it powered such famous microcomputers as the Commodore 64, game systems like the Atari 2600 and Nintendo Entertainment System (NES), as well as thousands of consumer and embedded devices today.

    I haven’t released a package for Py65 yet but I’ll be doing this after some organizational changes are complete. For now, you can get it from its Subversion repository linked from the Py65 page on Ohloh.

    The Python interactive interpreter itself can be used as a monitor:

    >>> from py65.mpu6502 import MPU
    >>> mpu = MPU()
    >>> mpu
    <6502: A=00, X=00, Y=00, Flags=20, SP=ff, PC=0000>
    >>> mpu.memory[0x0000:0x0001] = (0x69, 0x16) #=> $0000 ADC #$16
    >>> mpu.step()
    <6502: A=16, X=00, Y=00, Flags=20, SP=ff, PC=0002>

    The simulator currently supports all legal NMOS 6502 opcodes and I have written a large unit test suite to verify its core. Most of its tests look like this one:

    def test_bne_zero_set_does_not_branch(self):
      mpu = MPU()
      mpu.flags |= cpu.ZERO
      mpu.memory[0x0000:0x0001] = (0xD0, 0x06) #=> BNE +6
      mpu.step()
      self.assertEquals(0x0002, mpu.pc)

    Py65’s own unit tests hint at what tests for your own assembly language programs could look like. I am especially interested in unit testing my own embedded software and I will be working on an assertion vocabulary and test helpers for this.

    There are a lot of possibilities for what could be done with Py65. For now, I plan to include a system for mapping virtual devices into the microprocessor’s address space, add more hooks into the simulator, and include support for additional 65xx family hardware.

    Py65 does not include an assembler. If you need one, I recommend André Fachat‘s excellent xa assembler which is currently maintained by Cameron Kaiser.

7 comments

  • comment by Stefane Fermigier 1 Jul 08

    Can’t find the svn repo :(

    S.

  • comment by Mike Naberezny 2 Jul 08

    http://svn.6502.org/py65/

    I’ll put together a website for Py65 after it’s a little further along.

  • comment by David Turnbull 2 Jul 08

    Mike, the “self.extracyles” [sic] variable breaks a trivial loop I was trying to assemble through xa.

    I’m using py65 to learn the basics so I’m unsure what the intended behaviour is here.

  • comment by Mike Naberezny 2 Jul 08

    Hi David,

    Unfortunately, it looks like you hit a code path that isn’t covered by the unit tests yet. It’s still very early, but we’ll get there. The bug you found has been fixed in the SVN.

    Thanks,
    Mike

  • comment by Oscar Lindberg 27 Mar 09

    Hi Mike,

    I tried checking out the trunk and “setup.py install”, but I get:

    >>> from py65.mpu6502 import MPU
    Traceback (most recent call last):
    File “”, line 1, in
    File “c:\python25\lib\site-packages\py65-0.2-py2.5.egg\py65\mpu6502.py”, line
    1, in
    from py65 import util
    File “c:\python25\lib\site-packages\py65-0.2-py2.5.egg\py65\util.py”, line 3,
    in
    import termios
    ImportError: No module named termios
    >>>

    Is py65 for linux only?

  • comment by Oscar Lindberg 27 Mar 09

    I moved the imports for termios and fcntl into the getch function. Now the example you gave here works for me.

    I guess only the monitor needs that function.

  • comment by TC 21 Jun 11

    Great job!
    Any chance you’ve tackled the 6809E?

Sorry, the comment form is closed at this time.