News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Mini 6502 Assembler for C++ and C# projects  (Read 405 times)

Dwedit

  • Sr. Member
  • ****
  • Posts: 303
    • View Profile
    • Dwedit's Website
Mini 6502 Assembler for C++ and C# projects
« on: October 16, 2019, 07:45:04 pm »
I just made a mini 6502 assembler for C++ and C# projects.  Most useful for Romhacking tools, but if there is any other need to emit 6502 asm within C# or C++ code, it might be useful.

Github Links:
C++ Version: Assembler.h, Assembler.cpp
C# Version: Assembler.cs

Example usage of C++ version, a simple memory clear loop:

Code: [Select]
using namespace assembler;
byte code[0x8000];
SetMemory(code, 0x8000);
SetCurrentAddress(0x8000);
SEI();
LDA_IMM(0);
STA_ABS(0x2000);    //disable NMI
LDX_IMM(0xFF);
TSX();              //reset stack
TAX();
Label("clrloop");
STA_ZPG_X(0x00);
STA_ABS_X(0x100);
STA_ABS_X(0x200);
STA_ABS_X(0x300);
STA_ABS_X(0x400);
STA_ABS_X(0x500);
STA_ABS_X(0x600);
STA_ABS_X(0x700);
INX();
BNE("clrloop");
ApplyFixups();

Then the corresponding code for the C# version.  You inherit the Assembler class if you don't want to put "assembler." before each method call.

Code: [Select]
    class MyCode : Assembler
    {
        public void Build(byte[] memory)
        {
            SetMemory(memory, 0x8000);
            SetCurrentAddress(0x8000);
            SEI();
            LDA_IMM(0);
            STA_ABS(0x2000);    //disable NMI
            LDX_IMM(0xFF);
            TSX();              //reset stack
            TAX();
            Label("clrloop");
            STA_ZPG_X(0x00);
            STA_ABS_X(0x100);
            STA_ABS_X(0x200);
            STA_ABS_X(0x300);
            STA_ABS_X(0x400);
            STA_ABS_X(0x500);
            STA_ABS_X(0x600);
            STA_ABS_X(0x700);
            INX();
            BNE("clrloop");
            ApplyFixups();
        }
    }
"We are merely sprites that dance at the beck and call of our button-pressing overlord."

Mauron

  • Submission Reviewer
  • Hero Member
  • *****
  • Posts: 531
    • View Profile
Re: Mini 6502 Assembler for C++ and C# projects
« Reply #1 on: October 16, 2019, 08:07:47 pm »
This looks handy. I suddenly realize I could benefit from a 65816 version of this.

Edit: Just tried it with some simple test code. This is far more organized than working with a byte array of assembled code.
« Last Edit: October 16, 2019, 10:19:52 pm by Mauron »
Mauron wuz here.

RetroRain

  • Sr. Member
  • ****
  • Posts: 287
    • View Profile
Re: Mini 6502 Assembler for C++ and C# projects
« Reply #2 on: October 18, 2019, 01:28:48 pm »
Very interesting.  I've been delving deeper into C++ over the last 6 months, more specifically in the game development department. ;)

C++ is very complex, but once you know what you're doing, it can be a lot of fun.

It's nice to see a 6502 C++ assembler.  I will definitely bookmark this thread for future reference.

By the way, for those of you who have PMed me with questions or advice, please don't take it personal if I haven't replied back.  I know its been months, possibly over a year for some people.  Real-life has taken precedence over ROM hacking, and I haven't been on this forum in a long time.  And at the moment, what little free time I have, my focus has been on C++, as I'm attempting to develop an indie game that I hopefully can later sell.  I love ROM hacking, always have and always will, but unfortunately it doesn't pay the bills.

Take care everyone and good luck with your projects.

~RetroRain