;      assembly language subroutines for faerie tale adventure 
;      written august 86 by Talin

;      AmigaDOS subroutine vectors

Write           EQU $FFFFFFD0
Read            EQU $FFFFFFD6
Close           EQU $FFFFFFDC
Open            EQU $FFFFFFE2
Move            EQU $FFFFFF10
Text            EQU $FFFFFFC4
SetAPen         EQU $FFFFFEAA
Draw            EQU $FFFFFF0A
BltBitMap       EQU $FFFFFFE2
SetRGB4         EQU $FFFFFEE0
WaitBlit        EQU $FFFFFF1C
OwnBlitter      EQU $FFFFFE38
DisownBlitter   EQU $FFFFFE32


ie_X           equ     10
ie_Y           equ     12
ie_NextEvent   equ     0


xwrap      macro
           btst    #6,\1               ; if 0-64, process normally
           beq.s   98$
           btst    #5,\1               ; else wrap in various directions
           seq     d2                  ; if zero, set to all 1's (mask 63)
           and.w   #63,\1              ; else set to all zero's
98$
           endm

ywrap      macro
           btst    #5,\1
           beq.s   99$
           btst    #4,\1
           seq     \1
           and.w   #31,\1
99$
           endm


           public _trapper
_trapper
           move.l  (sp)+,4(a1)
           rte

           public  _HandlerInterface

RAWKEY         equ     1
RAWMOUSE       equ     2
POINTERPOS     equ     4
DISKIN         equ     $10

;  a0 contains inputevent stream
;  a1 contains data area

xsprite        equ     0
ysprite        equ     2
qualifier  equ     4

       public  _LVOMoveSprite

_HandlerInterface:
       movem.l d1-d2/a1-a3,-(sp)
       move.l  a0,a2               ; save list address
       lea     keytrans,a3
handloop
; first we check what the actual event was before we mess with it
       move.b  4(a0),d0            ; check event type
       cmp.b   #RAWKEY,d0          ; is it a raw key?
       bne.s   notrawkey           ; check other possibilities

       move.w  6(a0),d0            ; get raw key code
       move.b  d0,d1               ; save high bit
       and.b   #$7f,d0             ; turn off high bit
       cmp.b   #$5a,d0             ; ignore undefined keys
       bhi.s   domouse             ; is so, ignore for now

       move.w  #$0000,4(a0)        ; change to null event

       move.b  (a3,d0),d0          ; translate
       and.b   #$80,d1             ; isolate old upper bit (up/down)
       or.b    d1,d0               ; and mark key as up or down
       clr.w   d1                  ; clear upper half d1
       move.b  6(a1),d1            ; get laydown pointer
       move.b  d0,22(a1,d1.w)      ; move converted code into buffer

       addq.b  #1,d1               ; increment buffer
       and.b   #$7f,d1             ; and limit it
       cmp.b   7(a1),d1            ; same as pickup pointer?
       beq.s   nomouse             ; is so, overflow
       move.b  d1,6(a1)            ; otherwise, update buffer
       bra.s   nomouse

notrawkey
       cmp.b   #RAWMOUSE,d0
       bne.s   domouse

       move.w  8(a0),d1            ; get qualifier
       move.w  4(a1),d0
       eor.w   d1,d0               ; old xor new
       and.w   #$4000,d0           ; isolate left button bit
       beq.s   nobutn              ; left button not changed

       and.w   #$4000,d1           ; test new bit value
       bne.s   butndown            ; if down, goto down trans

       move.b  9(a1),d1            ; get old menu value
       beq.s   nobutn              ; if none, no action
       or.b    #$80,d1             ; set up bit
       clr.b   9(a1)               ; clear old action
       bra.s   butn10              ; put it in the que

butndown
       clr.l   d1
       move.w  xsprite(a1),d0      ; get current x/y coords
       move.w  ysprite(a1),d1

       cmp.w   #167,d1             ; # gadget code  = 1
       blo.s   nobutn
       cmp.w   #173,d1
       bhi.s   othergad
       cmp.w   #89,d0
       blo.s   nobutn
       cmp.w   #152,d0
       bhi.s   nobutn
       move.w  #1,d1
       bra.s   butn10
othergad
       cmp.w   #179,d1
       blo.s   nobutn
       cmp.w   #208,d1
       bhi.s   nobutn
       
       cmp.w   #79,d0             ; Call gadget code = 2
       blo.s   nobutn
       cmp.w   #100,d0
       bhi.s   startgad
       move.w  #2,d1
       bra.s   butn10

startgad
       cmp.w   #107,d0            ; Start gadget code = 3 
       blo.s   nobutn
       cmp.w   #127,d0
       bhi.s   hanggad
       move.w  #3,d1
       bra.s   butn10

hanggad
       cmp.w   #133,d0            ; HangUp gadget code = 4
       blo.s   nobutn
       cmp.w   #152,d0
       bhi.s   nobutn
       move.w  #4,d1

butn10
       move.w  d1,d0
       clr.w   d1
       move.b  6(a1),d1            ; get laydown pointer
       move.b  d0,22(a1,d1.w)      ; move converted code into buffer

       addq.b  #1,d1               ; increment buffer
       and.b   #$7f,d1             ; and limit it
       cmp.b   7(a1),d1            ; same as pickup pointer?
       beq.s   nobutn              ; is so, overflow
       move.b  d1,6(a1)            ; otherwise, update buffer
       move.b  d0,9(a1)            ; save menu choice
nobutn
       move.w  8(a0),4(a1)         ; move qualifier to in_work

domouse
       cmp.b   #DISKIN,d0
       bne.s   domouse1
       move.b  #1,8(a1)
domouse1
       move.w  xsprite(a1),d0      ; get current coords
       move.w  ysprite(a1),d1

       add.w   ie_X(a0),d0         ; add to xsprite
       add.w   ie_Y(a0),d1         ; add to ysprite

       cmp.w   #152,d0
       blt.s   xhi
       move.w  #152,d0
xhi
       cmp.w   #77,d0
       bgt.s   xlo
       move.w  #77,d0
xlo
       cmp.w   #208,d1
       blt.s   yhi
       move.w  #208,d1
yhi
       cmp.w   #160,d1
       bgt.s   ylo
       move.w  #160,d1
ylo
       move.w  d0,xsprite(a1)
       move.w  d1,ysprite(a1)
       tst.l   14(a1)              ; do we have simplesprite?
       beq.s   nosprite

       movem.l a0/a1/a6,-(sp)
       add.w   d0,d0
       sub.w   #0,d1               ; vp_text offset
       move.l  10(a1),a6           ; Gfx Base
       move.l  18(a1),a0           ; ViewPort
       move.l  14(a1),a1           ; simplesprite
       jsr     _LVOMoveSprite(a6)
       movem.l (sp)+,a0/a1/a6
nosprite

nomouse
       move.l  ie_NextEvent(a0),a0 ; next event in chain
       move.l  a0,d0               ; set zero flag -- if null, quit
       bne.s   handloop            ; else process next

       clr.l   d0

       movem.l (sp)+,d1-d2/a1-a3
       rts

keytrans   dc.b    "`1234567890-=\?0"
           dc.b    "QWERTYUIOP{}?",26,25,24
           dc.b    "ASDFGHJKL:",39,"??",27,29,23
           dc.b    "?ZXCVBNM,.??.",20,21,22
           dc.b    $20,$08,$09,$0D,$0D,$1B,$7F,0,0,0,$2D,0,1,2,3,4
           dc.b    97,98,96,100,101,102,103,104,105,106,0,0,0,0,0,0

