Newsgroups: comp.sys.apple2.programmer
Path: blue.weeg.uiowa.edu!news.uiowa.edu!hobbes.physics.uiowa.edu!newsrelay.iastate.edu!vixen.cso.uiuc.edu!howland.reston.ans.net!news.sprintlink.net!crash!shack
From: shack@crash.cts.com (Randy Shackelford)
Subject: Re: Apple Mouse
Organization: CTS Network Services (CTSNET), San Diego, CA
Date: Fri, 20 Jan 1995 06:44:40 GMT
Message-ID: <D2oyqH.Jw2@crash.cts.com>
References: <1995Jan18.125304.2732@pro-lep.bga.com>
Sender: news@crash.cts.com (news subsystem)
Nntp-Posting-Host: crash.cts.com
Lines: 306

In article <1995Jan18.125304.2732@pro-lep.bga.com>,
Robert Fletcher <robertf@pro-lep.bga.com> wrote:

>If you get the Mouse information in the mail and not on the public board, I
>would like to see a copy. I wont to program using the Mouse also.

OK I'm posting the info from my Applemouse manual and including an example
assembly program I wrote many moons ago to try it out:

finding the mouse: look for the signature bytes $20 in $Cs0C and $D6 in
$CsFB. When you find a slot with these values, you've found it.

setting operating mode: load the accumulator with a mode byte and call the 
setmouse routine. the mode is set by the low 4 bits:
bit 0: turn mouse on
bit 1: interrupts on mouse movement
bit 2: interrupts on buttn down
bit 3: interrupts on screen refresh

reading mouse data: n is the slot number
$478+n: low byte of x pos
$4F8+n: low byte of y pos
$578+n: high byte of x pos
$5F8+n: high byte of y pos
$778+n: button/interrupt status
$7F8+n: current mode

interrupt/button byte:
bit:
7: button is down
6: down at last read
5: x or y changed since last read
4: reserved
3: interrupt by screen refresh
2: interrupt by button press
1: interrupt by mouse movement
0: reserved

Use mode 1 to put mouse in passive mode - any other mode requires handling
interrupts from the hardware.
There are eight routines. Except for servemouse you have to load $Cn in x
and $n0 in y before calling. On return the carry bit is clear on a successful
call and set otherwise.

The setmouse call sets up operation in the mode corresponding to the value
in the accumulator.

Servemouse updates the status byte and clears the carry if the mouse caused
an interrupt and sets it otherwise.

Readmouse transfers the x,y position to the screen holes and clears bits 1-3.

Clearmouse sets the position to 0,0.

Posmouse sets the x/y on the mouse to the values found in the screen holes.

Clampmouse sets the boundaries for x/y values for the mouse. You put either
0 or 1 in the accumulator to determine which axis to set:
A=0 changes x limits, A=1 sets y limits.
The limit values are found in memory:
$478: low byte of lower boundary
$4F8: low byte of upper boundary
$578: high byte of lower boundary
$5F8: high byte of upper boundary

Homemouse sets the position to the lower boundary values for x and y. It does
not update the screen holes so it should be followed by readmouse.

Initmouse sets the internal defualt values and synchs it with the system's 
vertical blanking cycle. It should be called before any other routines. The
typical sequence to set up the mouse is initmouse, setmouse, clearmouse.

Locating the entry points for the routines:

$Cn12: setmouse
$Cn13: servemouse
$Cn14: readmouse
$Cn15: clearmouse
$Cn16: posmouse
$Cn17: clampmouse
$Cn18: homemouse
$Cn19: initmouse

The entry point is determined by taking the value in the location listed for 
each routine as the low byte and using $Cn as the high byte.

OK here comes some ORCA/M source for a program I wrote when I was learning to
program the mouse. I put the machine code in a binscii segment for those
who don't have ORCA/M. I've tested it on my IIgs and //e which have the mouse
in slot 4 and my memory expandable //c which has it in slot 7.

         keep  simouse.o
         org   $2000
         65c02 on
**  This program reads the mouse and places a pointer on the eighty  **
**  column screen which is controlled by the mouse                   **
slot     gequ  $04
cs       gequ  $03
s0       gequ  $05
mouse    start
         jsr   $c300
         jsr   locate                   switch to 80 columns
         lda   #$9b
         jsr   $fded
         ldx   cs                       $CS must always be in x and $S0 in y
         ldy   s0                       to call the mouse routines.
         jsr   initm                    The first call is to initmouse.
         lda   #$01
         ldx   cs
         ldy   s0
         jsr   setm                     Then a call to set mouse. We want pas-
         stz   $578                     sive mode (no interrupts) so we set
         stz   $5f8                     the mode to 1.
         lda   #$01
         sta   $478
         lda   #$4e                     We restrict the mouse to the ranges of
         sta   $4f8                     1-79 for x and 1-23 for y. This is
         lda   #$00                     the entire 80 column text screen minus
         ldx   cs                       the extreme rows and columns.
         ldy   s0
         jsr   clampm                   This call sets the x boundaries
         stz   $578
         stz   $5f8
         lda   #$17
         sta   $4f8
         lda   #$00
         sta   $478
         ldx   cs
         ldy   s0
         lda   #$01
         jsr   clampm                   and this call sets the y boundaries.
         lda   #$03
         ldx   slot
         sta   $478,x
         sta   $24
         lda   #$02
         sta   $4f8,x
         sta   $25
         ldx   cs
         ldy   s0
         jsr   posm                     We position the mouse to x = 3, y = 2
         jsr   scrn
         sta   char
l        ldx   cs                       to start
         ldy   s0
         jsr   readm                    then we read a new position.
         jsr   prtpnt                   and display the pointer.
         bit   $c061                    If the user pressed OA,
         bpl   chkbut1
         jsr   $fc58                    clear the screen.
         lda   #$a0
         sta   char
         ldx   slot
         lda   $478,x
         sta   $24
         lda   $4f8,x
         sta   $25
         jsr   $fc22
         lda   #'B'
         jsr   $fded
chkbut1  bit   $c062                    If the user pressed SA,
         bpl   l                        quit the program.
         lda   #$00
         ldx   cs
         ldy   s0
         jsr   setm                     Turn the mouse off
         lda   #$98
         jsr   $fded                    and set primary char set.
         lda   #$92
         jsr   $fded
         jmp   $fc58
prtpnt   ldx   slot
         lda   $778,x
         and   #$20
         beq   nomove
         lda   $778,x
         and   #$80
         beq   notbut
         lda   #'D'
         bra   over
notbut   lda   char
over     jsr   $fded
         ldx   slot
         lda   $478,x
         sta   $24
         lda   $4f8,x
         sta   $25
         jsr   $fc22
         jsr   scrn                     The routine to print the pointer
         sta   char
         lda   #'B'
         jsr   $fded
nomove   ldx   slot
         lda   $778,x
         and   #$80
         beq   finish
         lda   #'D'
         sta   char
finish   ldx   slot
         lda   $478,x
         sta   $24
         lda   $4f8,x
         sta   $25
         jmp   $fc22
char     ds    1
         end
locate   start
         lda   #$01
         sta   slot
         lda   #$0c
         sta   $00                      This code looks for the slot that
         lda   #$c1                     contains the mouse. The program
         sta   $01                      is slot independent so it has to
         sta   $03                      set up plenty of stuff and do lotsa
         lda   #$fb                     extra work since it does not assume
         sta   $02                      the mouse is in any particular slot.
l        lda   ($00)
         cmp   #$20                     The mouse id bytes are $20 at $CS0C
         bne   next                     and $D6 at $CSFB where S is the slot
         lda   ($02)                    number. When these are found, the slot
         cmp   #$d6                     contains the mouse.
         beq   finish
next     inc   slot
         inc   $01
         inc   $03
         bra   l
finish   lda   slot
         asl   a
         asl   a
         asl   a
         asl   a
         sta   s0
         lda   cs
         sta   setm+2
         sta   servem+2
         sta   readm+2
         sta   clearm+2
         sta   posm+2
         sta   clampm+2
         sta   homem+2
         sta   initm+2
         sta   $01
         stz   $00
         ldy   #$12
         lda   ($00),y
         sta   setm+1
         iny
         lda   ($00),y
         sta   servem+1
         iny
         lda   ($00),y
         sta   readm+1
         iny
         lda   ($00),y
         sta   clearm+1
         iny
         lda   ($00),y
         sta   posm+1
         iny
         lda   ($00),y
         sta   clampm+1
         iny
         lda   ($00),y
         sta   homem+1
         iny
         lda   ($00),y
         sta   initm+1
         rts
setm     entry
         jmp   $0000
servem   entry
         jmp   $0000
readm    entry
         jmp   $0000
clearm   entry
         jmp   $0000
posm     entry
         jmp   $0000
clampm   entry
         jmp   $0000
homem    entry
         jmp   $0000
initm    entry
         jmp   $0000
         end

FiLeStArTfIlEsTaRt
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789()
ISIMOUSE.O      AEw9AAAAAYw4BEAI(SDA0AwDA8gvAEw9A4qd
DDAIhIAIgsZqm2f7FQ6AhUJImGQqFQ6AhAIIFgHnFgPnNGQqpSAe43oTAkKBkOgp
PCSB4xZI4zZBXkaBEgfjNCQqmSAeFQ6AgEQqpGyjEY6AEgXnpSSh43pAlUIBkOgp
MCSBYCSIB0YIDYaIgUApgEihsACsQAcYYBCHgmK)hEQj9SgpFSAe43LJlUIB8LCI
gIUqs0f7QAsYAkK0kOgpACSBYmaI93OIgIZqM1f7myPW41LBgkyB9uC8pcAeEAPg
ASUqB06AtDSIEYa)EgXv9SShFSA(iASJYCC)B0YIClaI93OI9SgppcAeFAPgNSUq
mGSA41LBkUIBEgfvMVShAwvIFGQqMkKBpCQhBUYwpOQhCU4(JDgsGANIJLgsIAv1
mTg5DYeAlyOgKoABFqgCDUaBhIYjhUYjhgYjhsYjh4YjhEZjhQZjhcZjkFQhSAKA
NCQsIHSgNCQsIHChNCQsIHyhNCQsIHiiNCQsIHSjNCQsIHCkNCQsIHykNCQsgFil
AAATAAATAAATAAATAAATAAATAAATAAATKUSpH3rqAUYIhgcvsEQhQAMGkU6FFApa
ATFLsMAglCcVoqEJsAQsgBMVxSCpAAGAEAIBAWAAGAQBAYAgHA4BoSAKFgCBoUAq
GgqBoeAKEA1BQRA0FAdBQbAUHAlBAcA0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AQ0n

--
Randy Shackelford                                Huh huh, that was cool.
shack@crash.cts.com

