#asm
CursorITpos equ 0eh             ;screen height 30 character, cursor IT pos 0effh
    
PORT_5_MEM_MIRR equ 2834
SND_HIport      equ 5       // Bits 0-3.: sound PITCH high 4 bits
IT_ACK_PORT		equ 7
CRTC_CUR_STRCHR	equ 10      // CRTC register #10 - first character in the cursor-interrupt line
CRTC_CUR_ENDCHR equ 11      // CRTC register #11 - last  character in the cursor-interrupt line
CRTC_REG_PORT   equ 112     // port to select the CRTC register
CRTC_DATA_PORT  equ 113     // port to the CRTC data
maxX    equ     60
maxY    equ     218

qMouseButt      db  00h
qmouseY         db  00h
qmouseX         db  00h
qtvcmouseX      db  120
qtvcmouseY      db  120

#endasm

    main()
    {
        #asm
        ld      a,90h
        out     (02h),a
        ld      (0003h),a
        #endasm
    DbInitSoundSystem()
    AGInitMouse(2);
    while(1)
        {
        }
    }

AGInitMouse(char joyIndex)
    {
    #asm
        #ifdef  SMALL_C
        pop     bc
        pop     hl
        push    hl
        push    bc
        ld      a,l
        #endif
        ld      l,a
        ld      a,0c9h          ;mouse off, no gamecard parameter
        ld      (mouseon),a
        cp      02h
        ret     c
        ld      a,(SndCardBaseAddr)
        or      a
        ret     z
        add     a,l
        ld      h,a
        ld      l,0dbh          ;in a,(xx)
        ld      (mouseon),hl    ; frq=195312,5/(4096-reg)

        di
        ld      a,0c3h
        ld      hl,MouseITHandler
        ld      (0038h),a
        ld      (0039h),hl
        ld      b,03h
        call    set_crtc        ;enable cursor IT
        ld      a,(PORT_5_MEM_MIRR)
        and     0c0h            ;clear lower 6 bits (#0-#3: PITCH high 4 bits; #4:SOUND IT, #5:SOUND SIGN)
        ei
        out     (SND_HIport),A  ;send register A value to port
        ret

MouseITHandler
        push    af
        push    bc
        push    hl
        out     (IT_ACK_PORT),a
        ld      a,0eh
        out     (CRTC_REG_PORT),a   ;select CRTC register #10
curpos  ld      a,00h           ; 7
numint  cp      09h             ; 7
        jr      c,@nvsync       ;12/8
        sub     CursorITpos     ;   7
        jr      z,@strval       ;   12/8
        ld      a,CursorITpos-1 ;   7
@nvsync inc     a               ; 4
@strval out     (CRTC_DATA_PORT),a  ;send value to CRTC data port
        ld      (curpos+1),a
        call    z,vidint
        ld      a,0ffh
        out     (00h),a
        call    qAGGetMouseData
        xor     a
        out     (00h),a
        pop     hl
        pop     bc
        pop     af
        ei
        ret
        
vidint  push    de
        call    move_mouse
        
oldcur  ld      hl,08000h       ;simple cursor
        ld      de,0040h
        ld      (hl),d
        add     hl,de
        ld      (hl),d
newcur  ld      a,(qtvcmouseY)
        ld      h,a
        xor     a
        srl     h
        rra
        scf
        rr      h
        rra
        ld      l,a
        ld      a,(qtvcmouseX)
        srl     a
        srl     a
        add     a,l
        ld      l,a
        ld      (oldcur+1),hl
        ld      (hl),06h
        add     hl,de
        ld      (hl),06h
        pop     de

        ld      a,06h           ;change cursor it setting by pressing V key
        out     (03h),a
        in      a,(058h)
oldval  cp      00h
        ld      (oldval+1),a
        ret     z
        cp      0ffh
        ret     z
        and     80h
        ret     nz
        ld      a,(numint+1)
        inc     a
        cp      0eh
        jr      c,intvok
        xor     a
intvok  ld      (numint+1),a
        ret

move_mouse
        ld      hl,qmouseX          ;move delta x
        ld      a,(qtvcmouseX)      ;x position
        bit     7,(hl)
        jr      nz,@x_sub
        add     a,(hl)
        jr      c,@stmaxx
        cp      maxX*4
        jr      c,@storx
@stmaxx ld      a,maxX*4-1
        jr      @storx
@x_sub  add     a,(hl)
        jr      c,@storx
        xor     a
@storx  ld      (qtvcmouseX),a
        ld      hl,qmouseY          ;move delta x
        ld      a,(qtvcmouseY)      ;x position
        bit     7,(hl)
        jr      nz,@y_sub
        add     a,(hl)
        jr      c,@stmaxy
        cp      maxY
        jr      c,@story
@stmaxy ld      a,maxY-1
        jr      @story
@y_sub  add     a,(hl)
        jr      c,@story
        xor     a
@story  ld      (qtvcmouseY),a
        xor     a
        ld      (qmouseX),a
        ld      (qmouseY),a
        ret

set_crtc
        ld      a,CRTC_CUR_STRCHR
        out     (CRTC_REG_PORT),a   ;select CRTC register #10
        ld      a,b                 ;restore default value 3 to enable the cursor-interrupt
        out     (CRTC_DATA_PORT),a  ;send value to CRTC data port
        ld      A,CRTC_CUR_ENDCHR
        out     (CRTC_REG_PORT),a   ;select CRTC register #11
        ld      a,b                 ;restore value from register B
        out     (CRTC_DATA_PORT),a  ;send value to CRTC data port
    #endasm
    }

AGGetMouseData()
    #asm
mouseon in      a,(00h)         ;j=DbGetJoyData(jn);
        cpl

        ld      b,a             ; 4 b0:stateAY, b1:stateAX, b2:stateBY, b3:stateBX
        ld      hl,@lastState   ;10
        ld      c,(hl)          ; 7
        ld      (hl),a          ; 7 lastState=state;

        and     10h             ; 7
        ld      (qMouseButt),a  ;13     10 if pressed, 00h if not
        
        ld      a,b             ; 4     if(stateAX !=qlastStateAX)
        xor     c               ; 4
        and     02h             ; 7
        jr      z,@lastAX       ; 8/12
        ld      a,b             ; 4     if(stateAX!=stateBX)
        rrca                    ; 4     mouseX++;          ;a 0 if =, and 1 if !=
        rrca                    ; 4     else               ;a 0 if =, and 2 if !=
        xor     b               ; 4     mouseX--;          ;a 0ffh if =, and 01h if !=
        and     02h             ; 7
        dec     a               ; 4
        add     a,a             ; 4
        ld      hl,qmouseX      ;10
        add     a,(hl)          ; 7
        ld      (hl),a          ; 7

@lastAX ld      a,b             ; 4
        xor     c               ; 4
        rrca                    ; 4
        ret     nc              ; 5/10

        ld      a,b             ; 4
        rrca                    ; 4
        rrca                    ; 4
        xor     b               ; 4
        and     01h             ; 7
        add     a,a             ; 4
        dec     a               ; 4
        add     a,a             ; 4
        ld      hl,qmouseY      ;10
        add     a,(hl)          ; 7
        ld      (hl),a          ; 7
        ret                     ;10

@lastState  db      00h

    #endasm
    }