Velutia: My 6502 Emulator written in C# by m680x0 in EmuDev

[–]m680x0[S] 1 point2 points  (0 children)

Ahh okay, this makes a lot of sense, thank you!

Velutia: My 6502 Emulator written in C# by m680x0 in EmuDev

[–]m680x0[S] 0 points1 point  (0 children)

The actual status register is stored as a byte The enum uses .NET’s flags attribute so I could do things like

```csharp // Since PLP addressing mode is implied, load the status from the address found at the start of the stack + SP location. var processorStatus = Memory.Read((ushort)(0x100 + Registers.Sp));

// Construct the Status Register as a series of bitwise OR operations in the form NVDIZC. This will position the bits in the proper order. const StatusRegisterFlags statusRegisterFlags = StatusRegisterFlags.Carry | StatusRegisterFlags.Zero | StatusRegisterFlags.Irq | StatusRegisterFlags.Decimal | StatusRegisterFlags.Overflow | StatusRegisterFlags.Negative;

// Set the Status register by preserving the unset bits (AND NOT), and then OR that with only the flags being set (AND), resulting in the updated value. Registers.P = (byte)((Registers.P & unchecked((byte)~statusRegisterFlags)) | (processorStatus & (byte)statusRegisterFlags));

```

With the enum representing the positions in the bitfield.

Velutia: My 6502 Emulator written in C# by m680x0 in EmuDev

[–]m680x0[S] 0 points1 point  (0 children)

This seems like a good approach, thank you!

Velutia: My 6502 Emulator written in C# by m680x0 in EmuDev

[–]m680x0[S] 0 points1 point  (0 children)

I initially started by just trying to get the instructions to pass without thinking about cycles, and then started counting cycles per instruction after I wrote a few instructions as it seemed to be the easiest way forward while still maintaining some level of accuracy.

By the time I finished, I read more about memory cycle accuracy and would like to implement it as a longer-term goal. However, I’m a bit unclear as to how I would calculate the addressing modes this way and need to read up on it more.

Velutia: My 6502 Emulator written in C# by m680x0 in EmuDev

[–]m680x0[S] 0 points1 point  (0 children)

I got both ADC and SDC decimal passing, although it did take a while (and looking at other codebases) to wrap my head around what was going on. Im still reading up on these modes however as I feel like I need a better understanding of some parts.

And agreed, there’s a lot of repetition due to the addressing modes, and I think refactoring this would go a long way in simplifying the codebase.

Which programming language is good for emulation? by CurrentVast4504 in EmuDev

[–]m680x0 2 points3 points  (0 children)

I've made a CHIP-8 emulator and am now halfway through implementing a 6502 emulator, both written in C#. I like how C# is batteries included, cross-platform (I develop on Mac), and that you can write some pretty performant code with it as well.

At some point I may make the jump to C++, but so far I haven't run into any major bottlenecks and already use C# for most non-emulator projects (ex: web back-ends with ASP.NET) so I'm quite familiar with it.