nespy - A NES emulator. Update: PPU working

Discussion in 'Programming & Software Development' started by domlebo, Jun 19, 2010.

  1. domlebo

    domlebo Member

    Joined:
    Nov 13, 2004
    Messages:
    7,776
    Location:
    Brisbane
    So uni is over and I'm looking for someone who wants to help me develop a NES emulator in Python. Someone I can learn from (and vice versa I suppose) who is keen to hack this together in 4 or so weeks.

    Up until the past few days I've had no emulation experience, but I managed to develop a Chip8 emulator/interpeter in Python. It's buggy, no user input works, but it works quite reliably. It was a challenge implementing operations from 1970's documentation, but it's awesome seeing graphics displayed correctly for the first time.

    I figured a NES emulator wouldn't be too far off in difficulty (final last words).

    So let me know if you're interested.
     
    Last edited: Jul 13, 2010
  2. Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,061
    Location:
    Sleepwithyourdadelaide
    Why python? I guess it is just NES so you don't really need much cpu power but you are going to get better performance with a language that compiles to machine code.

    Nice work on coding it up though, could quite easily translate that if you wanted.
     
  3. OP
    OP
    domlebo

    domlebo Member

    Joined:
    Nov 13, 2004
    Messages:
    7,776
    Location:
    Brisbane
    I chose Python, A.) because it's a language I know reasonably well (I'm still learning) B.) because there's only 1 other Python implementation I know of.

    I realise it's going to be a lot slower (thought for certain operations Python isn't far behind C++/C) but it will still be playable. And the performance issues will give me a chance at refactoring solely for speed, which can be fun.
     
  4. andydev

    andydev Member

    Joined:
    Jun 28, 2008
    Messages:
    1,619
    I thought the same, before I checked which applications have been developed on Python

    Examples:
    BitTorrent
    Deluge
    MusicBrainz
    most of Civilization IV
    Eve Online (all of it!)
    Vampire: The Masquerade
    Etc...


    Quite impressive for a scripting language!
     
  5. Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,061
    Location:
    Sleepwithyourdadelaide
    No doubt about it being a brilliant language to write in. :thumbup:

    Just never going to compete for raw speed like C/C++ but in a large number of cases this is not an issue.
     
  6. OP
    OP
    domlebo

    domlebo Member

    Joined:
    Nov 13, 2004
    Messages:
    7,776
    Location:
    Brisbane
    I'm not sure all those programs use Python for the dirty stuff, perhaps only as a tacked on scripting replacement for Lua.
     
  7. mikeyyy

    mikeyyy Member

    Joined:
    Apr 7, 2005
    Messages:
    590
    Location:
    Sydney
    The CPU emulation would probably be okay in python since it runs only at 1.66MHz, but emulating the graphics processor might be more of a performance issue.

    I found http://nesdev.parodius.com/NES emulator development guide.txt which seems like a good guide. Though I'm guessing you would have stumbled across it already. :)
     
  8. Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,061
    Location:
    Sleepwithyourdadelaide
    If you can't get anyone to help perhaps post up specific questions or chunks of code you want help with, might entice people that way. I know I generally can't help but give most of them a shot if they are shoved in my face.
     
  9. swipe

    swipe (Banned or Deleted)

    Joined:
    Jun 27, 2001
    Messages:
    355
    It's not uncommon for initial development to be done solely in Python and then once development has stabilised, to redo the slow bits in C where it'd help. This is largely due to the ease of swapping out slower pieces with more performant C code without large interface changes. This is why you'll commonly see this sort of thing in Python projects:
    Code:
    try:
    	import cModule as module
    except ImportError:
    	import module
    
     
  10. Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,061
    Location:
    Sleepwithyourdadelaide
    Yep theres lots of advantages to doing that as well, mainly that development in a high level language like python or java is much faster to get a result compared to C etc.

    Personally I would be doing C# and the slow bits in C opposed to python, but the principle is the same.
     
  11. OP
    OP
    domlebo

    domlebo Member

    Joined:
    Nov 13, 2004
    Messages:
    7,776
    Location:
    Brisbane
    Most of the graphics will be drawn using SDL wrappers, and are fairly quick.

    I have completed a memory map and the roms load correctly. Will post code later.
     
  12. Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,061
    Location:
    Sleepwithyourdadelaide
    :thumbup:

    Look forward to it.
     
  13. OP
    OP
    domlebo

    domlebo Member

    Joined:
    Nov 13, 2004
    Messages:
    7,776
    Location:
    Brisbane
    K here's the basic memory loader. Still getting my head around the architecture, and exactly how it's all implemented. Very interesting stuff though.

    Am going to keep it a whole lot more organized (yay for OO) then the Chip8 emulator. Here's a link to the mercurial rep. Here's the cartridge loader.
     
  14. mikeyyy

    mikeyyy Member

    Joined:
    Apr 7, 2005
    Messages:
    590
    Location:
    Sydney
    Cool. Though it looks like you're only loading 1 prg and chr header instead of using prgCount and chrCount?
     
  15. OP
    OP
    domlebo

    domlebo Member

    Joined:
    Nov 13, 2004
    Messages:
    7,776
    Location:
    Brisbane
    Yeah atm. I cbf handling multiple prg/chr's just yet. Just trying to make sure my disassemblies match other emulators.
     
  16. OP
    OP
    domlebo

    domlebo Member

    Joined:
    Nov 13, 2004
    Messages:
    7,776
    Location:
    Brisbane
    Update: CPU execution architecture started, with the program counter working. So it now executes 3 opcodes lol.

    I spent a lot of time trying to understand how to deal with different mapper's (a bit like a pagefile for nes carts to overcome the memory limitation of the NES system) but it's too complex atm, so I'm just going to go without them and run mapper 0 roms for now.

    So yeah, steadily plodding along. Hope to have it looping soon instead of just breaking at unknown opcodes :p

    Going to be very interesting to see how slow Python is when it comes to switching out memory banks every few cycles. Have to emulate pointers somehow.

    Here is a list of all the opCodes. I've implemented like 10, so AGES to go :sick::

    Code:
            00 - BRK                        20 - JSR
            01 - ORA - (Indirect,X)         21 - AND - (Indirect,X)
            02 - Future Expansion           22 - Future Expansion
            03 - Future Expansion           23 - Future Expansion
            04 - Future Expansion           24 - BIT - Zero Page
            05 - ORA - Zero Page            25 - AND - Zero Page
            06 - ASL - Zero Page            26 - ROL - Zero Page
            07 - Future Expansion           27 - Future Expansion
            08 - PHP                        28 - PLP
            09 - ORA - Immediate            29 - AND - Immediate
            0A - ASL - Accumulator          2A - ROL - Accumulator
            0B - Future Expansion           2B - Future Expansion
            0C - Future Expansion           2C - BIT - Absolute
            0D - ORA - Absolute             2D - AND - Absolute
            0E - ASL - Absolute             2E - ROL - Absolute
            0F - Future Expansion           2F - Future Expansion
            10 - BPL                        30 - BMI
            11 - ORA - (Indirect),Y         31 - AND - (Indirect),Y
            12 - Future Expansion           32 - Future Expansion
            13 - Future Expansion           33 - Future Expansion
            14 - Future Expansion           34 - Future Expansion
            15 - ORA - Zero Page,X          35 - AND - Zero Page,X
            16 - ASL - Zero Page,X          36 - ROL - Zero Page,X
            17 - Future Expansion           37 - Future Expansion
            18 - CLC                        38 - SEC
            19 - ORA - Absolute,Y           39 - AND - Absolute,Y
            1A - Future Expansion           3A - Future Expansion
            1B - Future Expansion           3B - Future Expansion
            1C - Future Expansion           3C - Future Expansion
            1D - ORA - Absolute,X           3D - AND - Absolute,X
            1E - ASL - Absolute,X           3E - ROL - Absolute,X
            1F - Future Expansion           3F - Future Expansion
    
            40 - RTI                        60 - RTS
            41 - EOR - (Indirect,X)         61 - ADC - (Indirect,X)
            42 - Future Expansion           62 - Future Expansion
            43 - Future Expansion           63 - Future Expansion
            44 - Future Expansion           64 - Future Expansion
            45 - EOR - Zero Page            65 - ADC - Zero Page
            46 - LSR - Zero Page            66 - ROR - Zero Page
            47 - Future Expansion           67 - Future Expansion
            48 - PHA                        68 - PLA
            49 - EOR - Immediate            69 - ADC - Immediate
            4A - LSR - Accumulator          6A - ROR - Accumulator
            4B - Future Expansion           6B - Future Expansion
            4C - JMP - Absolute             6C - JMP - Indirect
            4D - EOR - Absolute             6D - ADC - Absolute
            4E - LSR - Absolute             6E - ROR - Absolute
            4F - Future Expansion           6F - Future Expansion
            50 - BVC                        70 - BVS
            51 - EOR - (Indirect),Y         71 - ADC - (Indirect),Y
            52 - Future Expansion           72 - Future Expansion
            53 - Future Expansion           73 - Future Expansion
            54 - Future Expansion           74 - Future Expansion
            55 - EOR - Zero Page,X          75 - ADC - Zero Page,X
            56 - LSR - Zero Page,X          76 - ROR - Zero Page,X
            57 - Future Expansion           77 - Future Expansion
            58 - CLI                        78 - SEI
            59 - EOR - Absolute,Y           79 - ADC - Absolute,Y
            5A - Future Expansion           7A - Future Expansion
            5B - Future Expansion           7B - Future Expansion
            5C - Future Expansion           7C - Future Expansion
            5D - EOR - Absolute,X           7D - ADC - Absolute,X
            5E - LSR - Absolute,X           7E - ROR - Absolute,X
            5F - Future Expansion           7F - Future Expansion
    
            80 - Future Expansion           A0 - LDY - Immediate
            81 - STA - (Indirect,X)         A1 - LDA - (Indirect,X)
            82 - Future Expansion           A2 - LDX - Immediate
            83 - Future Expansion           A3 - Future Expansion
            84 - STY - Zero Page            A4 - LDY - Zero Page
            85 - STA - Zero Page            A5 - LDA - Zero Page
            86 - STX - Zero Page            A6 - LDX - Zero Page
            87 - Future Expansion           A7 - Future Expansion
            88 - DEY                        A8 - TAY
            89 - Future Expansion           A9 - LDA - Immediate
            8A - TXA                        AA - TAX
            8B - Future Expansion           AB - Future Expansion
            8C - STY - Absolute             AC - LDY - Absolute
            8D - STA - Absolute             AD - LDA - Absolute
            8E - STX - Absolute             AE - LDX - Absolute
            8F - Future Expansion           AF - Future Expansion
            90 - BCC                        B0 - BCS
            91 - STA - (Indirect),Y         B1 - LDA - (Indirect),Y
            92 - Future Expansion           B2 - Future Expansion
            93 - Future Expansion           B3 - Future Expansion
            94 - STY - Zero Page,X          B4 - LDY - Zero Page,X
            95 - STA - Zero Page,X          BS - LDA - Zero Page,X
            96 - STX - Zero Page,Y          B6 - LDX - Zero Page,Y
            97 - Future Expansion           B7 - Future Expansion
            98 - TYA                        B8 - CLV
            99 - STA - Absolute,Y           B9 - LDA - Absolute,Y
            9A - TXS                        BA - TSX
            9B - Future Expansion           BB - Future Expansion
            9C - Future Expansion           BC - LDY - Absolute,X
            9D - STA - Absolute,X           BD - LDA - Absolute,X
            9E - Future Expansion           BE - LDX - Absolute,Y
            9F - Future Expansion           BF - Future Expansion
    
            C0 - Cpy - Immediate            E0 - CPX - Immediate
            C1 - CMP - (Indirect,X)         E1 - SBC - (Indirect,X)
            C2 - Future Expansion           E2 - Future Expansion
            C3 - Future Expansion           E3 - Future Expansion
            C4 - CPY - Zero Page            E4 - CPX - Zero Page
            C5 - CMP - Zero Page            E5 - SBC - Zero Page
            C6 - DEC - Zero Page            E6 - INC - Zero Page
            C7 - Future Expansion           E7 - Future Expansion
            C8 - INY                        E8 - INX
            C9 - CMP - Immediate            E9 - SBC - Immediate
            CA - DEX                        EA - NOP
            CB - Future Expansion           EB - Future Expansion
            CC - CPY - Absolute             EC - CPX - Absolute
            CD - CMP - Absolute             ED - SBC - Absolute
            CE - DEC - Absolute             EE - INC - Absolute
            CF - Future Expansion           EF - Future Expansion
            D0 - BNE                        F0 - BEQ
            D1 - CMP   (Indirect@,Y         F1 - SBC - (Indirect),Y
            D2 - Future Expansion           F2 - Future Expansion
            D3 - Future Expansion           F3 - Future Expansion
            D4 - Future Expansion           F4 - Future Expansion
            D5 - CMP - Zero Page,X          F5 - SBC - Zero Page,X
            D6 - DEC - Zero Page,X          F6 - INC - Zero Page,X
            D7 - Future Expansion           F7 - Future Expansion
            D8 - CLD                        F8 - SED
            D9 - CMP - Absolute,Y           F9 - SBC - Absolute,Y
            DA - Future Expansion           FA - Future Expansion
            DB - Future Expansion           FB - Future Expansion
            DC - Future Expansion           FC - Future Expansion
            DD - CMP - Absolute,X           FD - SBC - Absolute,X
            DE - DEC - Absolute,X           FE - INC - Absolute,X
            DF - Future Expansion           FF - Future Expansion
     
    Last edited: Jul 3, 2010
  17. SaTaN

    SaTaN Member

    Joined:
    Jun 18, 2002
    Messages:
    4,790
    Location:
    Caulfield-ish
    going from that mapper description page I dont see anything hard about implementing it?

    presumably you need to implement the memory system anyway so why not load every bank into some class and just keep track of the current bank and then do a lookup into the correct bank for read/writes?
     
  18. Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,061
    Location:
    Sleepwithyourdadelaide
    I would do this that way you can just access the banks via an array giving you O(n) access time, the fact that you preload them all means they are in ram as well so it should be lightening quick, I doubt you will run into any issues with speed.

    I'm on holidays for the next 3 weeks, I might be able to lend a hand with writing up some of the op codes for you, want to PM me with your msn or something and we can have a chat?

    You can easily write functions that call to external C methods, could write it all in python and if you have issues with speed and want to use pointers, could port some of the more intensive code to C and then use pointers when necessary. Write it all in python first though, don't bother optimising until it becomes obvious where the bottle necks are.
     
    Last edited: Jul 3, 2010
  19. OP
    OP
    domlebo

    domlebo Member

    Joined:
    Nov 13, 2004
    Messages:
    7,776
    Location:
    Brisbane
    Sure. My skype name is dominic.bou.samra. No MSN :( The opcodes are fairly easy (they are just tedious). It's the PPU emulation that looks hardest. I have it looping for mapper 0 roms now which is nice.

    My immediate goal (when opcodes are done) is a VERY basic GUI based debugger, where I can step into/over and view the systems state (registers, flags, memory etc). This will make debugging so much easier. I looked at Python GUI frameworks briefly, but have no experience with them - WTB VS2008 and Winforms :p
     
    Last edited: Jul 3, 2010
  20. Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,061
    Location:
    Sleepwithyourdadelaide
    Do you know if there are any GUI builders for python? Would make generating a basic gui much quicker if you don't feel like learning the APIs. Eg something like netbeans for java where you can drag and drop buttons, textboxes etc.
     

Share This Page

Advertisement: