<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Thu, 11 Aug 1994 17:51:33 EST
From   : Stephen Quan <quan@...>
Subject: adc and sbc (again).

Hi James, I just had a look at your emulator and I think it is a
very well laid out C program!  I think your use of C macros and
variable function calls is really good!

A while ago we talked about ADC and you posted a portion of the
code on the net and ask if there were ways of shortening the
logic.  I have another set of ideas you might be interested in :

The following two macros are used for updating P in normal addition
and subtraction.

#define ADC(cycles,amode) \
  pre##amode; \
  M=amode; \
  A2=A+M+(P&P_C); \
  P=P&~(P_N^P_V^P_Z^P_C)^P_NZ[A2]^((A2<A)?P_C:0)^((((A2^M))&(A^A2)&0x80)?P_V:0);
\
  A=A2; \
  post##amode; \
  IncClock(cycles); \
  break;

there are a few parts to this :

   P_NZ[A2]              - that's just the lookup table for N and Z values.
   (A2<A) ? P_C : 0      - we have a carry if somehow A2 ended smaller than A
   (A2^M) & A2^A) & 0x80 - this is my test for overflow, it is based on

                                  -ve + -ve = +ve (overflow)
                                  +ve + +ve = -ve (overflow)

                            (other combinations are not an overflow case).

#define SBC(cycles,amode) \
  pre##amode; \
  M=amode; \
  A2=A+(~M)+(P&P_C); \
  P=P&~(P_N^P_V^P_Z^P_C)^P_NZ[A2]^((A<M)?0:P_C)^((((A^M))&(A^A2)&0x80)?P_V:0); \
  A=A2; \
  post##amode; \
  IncClock(cycles); \
  break;

similar logic for SBC :

  P_NZ[A2]              - using looking table for N and Z values
  A<M ? 0 : P_C         - this is the borrow condition
  (A^M) & (A^A2) & 0x80 - this is  my test for overflow for SBC based on

                                  -ve - +ve = +ve (overflow)
                                  +ve - -ve = -ve (overflow)

                            (other combinations are not an overflow case).
-- 
Stephen Quan
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>