Page 1 of 1

Reading mouse ports

Posted: Fri Feb 19, 2016 5:20 pm
by tcaud
I realized it was probably possible to create an ASM-less method of accessing the mouse by polling it directly. This is theoretically straight forward, but it seems like something's in the way. The port (0x60) is always reading either 21 or 28.

Running under WinXP SP3 32-bit. (Dosbox doesn't do real mouse simulation).

Code: Select all

DIM mouseBytes(2) AS INTEGER
byteCount% = 0

WAIT &H64, 2
OUT &H64, &HD4
OUT &H60, &HF4

DO UNTIL INKEY$ = "Q"

    PRINT INP(&H64)

	WAIT &H64, 2
	OUT &H64, &HD4
	OUT &H60, &HF5

    IF INP(&H64) AND &H20 THEN
                        
        PRINT "Mouse active..."

        mouseBytes(byteCount%) = INP(&H60)
        byteCount% = byteCount% + 1

        IF byteCount% = 3 THEN

            IF mouseBytes(0) = &H80 OR mouseBytes(0) = &H40 THEN
            ELSE

                'PRINT "Byte 3"

                'xDifference = (mouseBytes(0) AND 3) * 64 + mouseBytes%(1)
                'yDifference = (mouseBytes(0) AND 12) * 16 + mouseBytes%(2)

                'IF xDifference% >= 128 THEN :xDifference% = xDifference% - 256
                'IF yDifference% >= 128 THEN :yDifference% = yDifference% - 256
                '_MOUSEX% = _MOUSEX% + xDifference
                'mouseY% = mouseY% + yDifference
                mouseBtn1% = mouseBytes%(0) AND 32
                mouseBtn2% = mouseBytes%(0) AND 16
                PRINT "Byte: ", mouseBytes%(0), ", Status 1:", mouseBtn1%, ", Status 2:", mouseBtn2%
                SLEEP 1
            END IF
            byteCount% = 0
           
        END IF
    END IF
LOOP

Re: Reading mouse ports

Posted: Sat Feb 20, 2016 8:14 am
by burger2227
mouseBytes(byteCount%) = INP(&H60)
INP(96) is reading the keyboard into an array that never prints.

http://www.qb64.net/wiki/index.php/INP

Re: Reading mouse ports

Posted: Sat Feb 20, 2016 9:29 pm
by tcaud
I looked into it some more... the problems are intractible in the modern age. Neither Dosbox or NTVDM accurately simulate the PS/2 mouse port, nor do they map USB mice to that port. The only way to access the mouse, now, is by calling the IRQ.

Here's the corrected code. It is correct, based on the DOS port docs I've found, but it doesn't work because NTVDM doesn't accept the write to get the mouse status (0xE5 at 0x60). That port 0x64, the status read port, doesn't set the mouse read flag at all is further evidence NTVDM doesn't accurately manage the mouse.

Code: Select all

mouseStatus% = 0
DO
sleep 1
WAIT &H64, 0 'wait for port clear
OUT &H60, &HE9	'request mouse status
WAIT &H64, 1	'wait for port ready
mouseStatus% = INP(&H60) AND 1	' get mouse status bytes
mouseStatus2% = INP(&H60)
LOOP UNTIL mouseStatus% > 0 OR INKEY$ = "Q"
PRINT mouseStatus%
END

Re: Reading mouse ports

Posted: Sat Feb 20, 2016 10:42 pm
by burger2227
This code returns 1 and 129 in QB64 and changes with key presses, not mouse:

Code: Select all

mouseStatus% = 0
DO
    SLEEP 1
    WAIT &H64, 0 'wait for port clear
    OUT &H60, &HE9 'request mouse status
    WAIT &H64, 1 'wait for port ready
    mouseStatus% = INP(&H60) AND 1 ' get mouse status bytes
    mouseStatus2% = INP(&H60)

    PRINT mouseStatus%; mouseStatus2%;
LOOP UNTIL INKEY$ = "q"
END

Use CALL Absolute in QB. That should still work.

http://www.qb64.net/wiki/index.php/CALL_ABSOLUTE

Re: Reading mouse ports

Posted: Sun Feb 21, 2016 6:37 am
by tcaud
The keyboard and the mouse read from the same port. And yes, Call ABSOLUTE works everywhere.

Re: Reading mouse ports

Posted: Sun Feb 21, 2016 8:44 am
by burger2227
I have never used INP(&H60) to read a mouse! Where are you getting such information?

Re: Reading mouse ports

Posted: Sun Feb 21, 2016 10:40 am
by tcaud

Re: Reading mouse ports

Posted: Sun Feb 21, 2016 11:27 am
by burger2227
No, those settings are probably blocked on NT. They SET mouse and keyboard settings or read them.

Note that there is nothing listed for the mouse coordinates, just buttons.