Larry’s Personal & Tech ramblings

Just another WordPress.com weblog

A Clever Idea for the 6502 Stack

I realize that this is only relevant to about five people in the world, but it may help get others thinking along similar lines.  I’ve come up with a clever idea for those emulating the 6502 on ARM and I’ve decided to share it with my fellow emulator authors. One of the tricky parts of emulating the stack pointer on the 6502 is that it points to 0×100-0×1ff, but it’s an 8-bit register. Modifying its value usually involves masking the 8-bits and then ORing in the 0×100. The ARM barrel shifter allows for a more elegant solution to the problem. By using the upper 8 bits of a register to hold the stack value, and then setting bit 0, it’s value can be modified without having to worry about the 0×100 part. Here’s an example:

Increment the stack: add r0,r0,#0×1000000 ; this doesn’t affect the LSB

Write to the stack: strb r1,[r2,r0,ROR #24] ; r2=ZP/Stack memory, R0 = SP

With the rotated register, bit 0 shifts into position as bit 8 and keeps the pointer in the 0×100 to 0×1ff range.

Enjoy,
L.B.

April 11, 2007 - Posted by bitbank | arm, arm9, asm, assembly language, emulation, optimization, performance, tech | | 1 Comment

1 Comment »

  1. Glad to be 1 out of the 5 people ;) I was writing a (naive) ARM JIT compiler for a NES emulator (PocketNester) and this idea is really brilliant :)

    Comment by rlei | June 21, 2007

Leave a comment