top of page
Search

[57-58] Fantasy Device 9000

  • Writer: Conlan Walker
    Conlan Walker
  • Nov 18, 2022
  • 3 min read

I've been having trouble with debugging code using ca65 and cc65 while only using the NES emulator, so I wanted a way to feed binary data into the Javascript 6502 emulator to test out algorithms, and whatnot, while being able to process things quickly.

The problem is this doesn't exist, so I had to make it myself.


I started on the weekend, because I thought it would be a relatively easy affair that I could finish by Monday, but it wasn't that easy. I made most of the emulator part over the weekend, but not the 6502 instruction portion, which made it a lot like a car without an engine.

At first, I was doing tests not in assembly, but in machine code.

ree

The only problem doing it this way is that I might type in the instructions incorrectly, testing for behavior that doesn't exist.

This happened when testing branches.

The assembly equivalent of the above machine code is shown below:

ree

After some time I realized that the instruction switch I was using to implement instructions had gotten large, but also unorganized, as I was sorting them based on opcode, rather than group.

ree

So Instead of 1 huge switch statement with the 100-something cases needed to complete every operation for every addressing mode, I'd split the instructions into two categories, one which fit the "aaabbbcc" pattern that most instructions fit into, and the ones that don't.

ree

I also had to separate out contingency opcodes that should otherwise be aaabbbcc, like this one here.


$6C is the only instruction that uses the indirect absolute addressing mode, and the only one that is not absolute (abs) mode within this section.
















This is what the indirect jump looks like inside the switch:

ree

I also separated out every instruction with relative addressing (only branches use it), as well as every accumulator or implied instruction, which lack operands of any kind.

Ones that do fit all of the aforementioned criterion fit within 3 groups of 6 function pointer arrays; 3 for the operations, and 3 for the addressing modes. All have 8 elements each:

ree

To sort the instruction components themselves, I use this:

ree

Finally, this is where those operations and addressing modes are executed:

ree

At this point I started using an assembler for the tests. As I completed more instructions, there was more I could work with, which made testing a lot easier over time.

For example, this is the definition of the "ORA" instruction, which is a bitwise OR with the accumulator register (CPU->A):

ree

I put the product of the ORA into the processor status register, because it prints out as base 2:

ree

If this instruction is in working order, then at the end of this, P should equal "11100011":

ree

After finishing, it seems to work, so I can move on to the next instruction.


(Bit 5 is supposed to be always set, which is why the product is 11100011, rather than 11000011.)


Here's another example of an instruction's definition, but this time it has comments.

This is the 'subtract from A with borrow' (SBC) instruction:

ree

I'd consider this whole thing to be complete, as it currently has all of the features I want, even if they're very basic in implementation. Here's a couple of demos I made for it.

This one just puts random colors on screen at 15fps (15 for visual purposes, as it can do 60.)

The other one is a comparison of the amoog program that I made first on the original javascript emulator, versus how it behaves in the new one.

This is the original, on the old emulator:

And this is the same thing, but on the new one:

That's it for these two weeks.

 
 
 

Comments


bottom of page