From: nparker@cie.uoregon.edu (Neil Parker)
Newsgroups: comp.sys.apple2
Subject: Re: Attention assembly programmers...
Date: 24 Jan 93 11:43:01 GMT
Article-I.D.: pith.1jtvc5INNofg
Organization: The Universal Society for the Prevention of Reality
NNTP-Posting-Host: cie.uoregon.edu

In article <aj74842@pro-gumbo.cts.com> tgeer@pro-gumbo.cts.com (System Administrator) writes:
>In <2B59CDAB.12197@news.service.uci.edu>
>andrep@balboa.eng.uci.edu (Andre Prellwitz) writes:
>
>>A while ago someone posted about how to read the joystick on a //gs in native
>>mode.  They said that it was possible to read both paddles at once and there-
>>fore get much more accurate readings.  I don't recall exactly how to do this,
>>but I've been trying the following: strobe the analog paddle reset ($c070)
>>and then read locations $c064 and $c065 and wait until both return to zero.
>                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
>Only the high bit of these locations is valid.  When the high bit of either
>location becomes 0 then the corresponding analog input has timed out.  You
>will actually get more accurate results by reading them one after the other
>with the accumulator set to 8 bits wide and the index registers used to hold
>the counts (16 bits wide).  This allows for a much faster loop, giving better
>resolution.  Assuming that this routine is called from full native mode, the
>following code will do the trick:
>
>strobe   equ   $C070       ; analog input timing reset
>pdl0     equ   $C064       ; analog input 0
>pdl1     equ   $C065       ; analog input 1
>
>start    php               ; save processor status register
>         phb               ; and data bank register
>         sep   #%100000    ; make accumulator 8 bits wide
>         lda   #0          ; make data bank = 0
>         pha
>         plb
>         ldx   #0          ; initialize the counters
>         txy
>         lda   strobe      ; strobe the timing reset
>loop1    inx               ; increment pdl0 count
>         lda   pdl0        ; is high bit = 0?
>         bmi   loop1       ; no, keep checking

(More code should probably go here...see below.)

>         lda   strobe      ; yes, strobe the timing reset again
>loop2    iny               ; increment pdl1 counter
>         lda   pdl1        ; is high bit = 0?
>         bmi   loop2       ; no, keep checking
>         plb               ; yes, restore data bank
>         plp               ; and processor status register
>         rts               ; return to caller (could be RTL)

And this doesn't cause you any problems with the Y value being wrong if the
X value was a little bit less than the Y value?

The paddle trigger strobe only resets the timer circuits that are all done
counting.  If one of the circuits is still counting, it will be unaffected
by the strobe.  This means if the X axis of the joystick is set somewhat
less than the Y axis, the strobe access at the end of the X-reading loop
will have no effect on the Y-axis timer, which is still counting down from
the original strobe access.  To really do it right, you need to wait until
the Y axis goes low, and THEN trigger the strobe and start counting the Y
axis.

I would stick the following code between your two loops:

loop3    lda   pdl1         ; Paddle 1 ready for reading yet?
         bmi   loop3        ; If not, try again

The reasons for this behavior are discussed in detail in Apple IIe
Technical Note #6.

               - Neil Parker
--
Neil Parker                 No cute ASCII art...no cute quote...no cute
nparker@cie.uoregon.edu     disclaimer...no deposit, no return...
parker@corona.uoregon.edu   (This space intentionally left blank:           )