DELETED

Basic and Machine Language

Moderator: Moderators

Post Reply
DELETED

ADC/SBC Instruction Logic

Post by DELETED »

DELETED
MacbthPSW
Vic 20 Afficionado
Posts: 478
Joined: Wed Apr 06, 2005 1:56 pm

Post by MacbthPSW »

I assume you've gleaned all you can from http://www.atarihq.com/danb/files/64doc.txt ?
DELETED

Post by DELETED »

DELETED
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

You simply want code scenarios for when the various flags are set, depending on what the flags were before the operation? I.e. when the Overflow flag gets set vs the Carry flag?
Anders Carlsson

Image Image Image Image Image
DELETED

Post by DELETED »

DELETED
User avatar
Kweepa
Vic 20 Scientist
Posts: 1315
Joined: Fri Jan 04, 2008 5:11 pm
Location: Austin, Texas
Occupation: Game maker

Post by Kweepa »

Here's the code from JarVIC's MOS6502Core.java (unchanged from jac64).
Don't blame me for the formatting!

Code: Select all

  private final void opADCimp(int data) {
    int tmp = data + acc + (carry ? 1 : 0);
    zero = (tmp & 0xff) == 0; // not valid in decimal mode

    if (decimal) {
      tmp = (acc & 0xf) + (data & 0xf) + (carry ? 1 : 0);
      if (tmp > 0x9)
	tmp += 0x6;
      if (tmp <= 0x0f)
	tmp = (tmp & 0xf) + (acc & 0xf0) + (data & 0xf0);
      else
	tmp = (tmp & 0xf) + (acc & 0xf0) + (data & 0xf0) + 0x10;

      overflow = (((acc ^ data) & 0x80) == 0) &&
	(((acc ^ tmp) & 0x80) != 0);

      sign = (tmp & 0x80) > 0;

      if ((tmp & 0x1f0) > 0x90)
	tmp += 0x60;
      carry = tmp > 0x99;
    } else {
      overflow = (((acc ^ data) & 0x80) == 0) &&
	(((acc ^ tmp) & 0x80) != 0);
      carry = tmp > 0xff;
      sign = (tmp & 0x80) > 0;
    }
    acc = tmp & 0xff;
  }

  private final void opSBCimp(int data) {
    int tmp = acc - data - (carry ? 0 : 1);
    boolean nxtcarry = (tmp >= 0);
    tmp = tmp & 0x1ff; // Carry is set!
    sign = (tmp & 0x80) == 0x80;  // Invalid in decimal mode??
    zero = ((tmp & 0xff) == 0);
    overflow = (((acc ^ tmp) & 0x80) != 0) && (((acc ^ data) & 0x80) != 0);
    if (decimal) {
      tmp = (acc & 0xf) - (data & 0xf) - (carry ? 0 : 1);
      if ((tmp & 0x10) > 0)
	tmp = ((tmp - 6) & 0xf) |
	  ((acc & 0xf0) - (data & 0xf0) - 0x10);
      else
	tmp = (tmp & 0xf) | ((acc & 0xf0) - (data & 0xf0));
      if ((tmp & 0x100) > 0)
	tmp -= 0x60;
    }
    acc = tmp & 0xff;
    carry = nxtcarry;
  }

DELETED

Post by DELETED »

DELETED
DELETED

Post by DELETED »

DELETED
DELETED

Post by DELETED »

DELETED
Post Reply