home *** CD-ROM | disk | FTP | other *** search
/ Falcon 030 Power 2 / F030_POWER2.iso / ST_STE / MAGS / ICTARI08.ARJ / ictari.08 / ASSEMBLY / MOUSPROB / MOUSE.S < prev    next >
Text File  |  1997-09-17  |  14KB  |  559 lines

  1.  
  2. * How to use the IKBD to read mouse data packets, and use them to update
  3. * program variables mousex & mousey.
  4.  
  5. * Started 14th December, 1993. Mike Barnard.
  6.  
  7. ****************************************************************************
  8.  
  9. * The equals statements, for ease of changing variables.
  10.  
  11. * screen limits for mouse movement.
  12.  
  13. xmax    equ    319    low rez
  14. ymax    equ    199
  15. xmin    equ    0
  16. ymin    equ    0
  17.  
  18. ****************************************************************************
  19.  
  20. * Main loop.
  21.  
  22.     text
  23.  
  24. start    jsr    init        set it all up
  25.     jsr    printmes    print messages
  26. main    jsr    readmouse    read the mouse packet and set the variables
  27.     jsr    printxy        print the values of the variables
  28.     jsr    input        look for user input
  29. *    jsr    count        print a counter to the screen to check speed
  30.     bra    main
  31.     
  32. **************************************************************************
  33.  
  34. * loop counter
  35.  
  36. count
  37.     move.l    counter,d0
  38.     addq.l    #1,d0
  39.     move.l    d0,counter
  40.     
  41. * set the cursor
  42.  
  43.     move.b    #30,cursx    set cursx
  44.     move.b    #5,cursy    set cursy
  45.     jsr    setcurs        set the cursor position on screen
  46.     
  47. * turn counter into decimal in decbuf
  48.  
  49.     jsr    bins2dec    go to convertion routine    
  50.  
  51. * print value of x in decimal
  52.  
  53.     move.l    #decbuf,-(sp)    address of string onto stack
  54.     move.w    #9,-(sp)    GEMDOS 9 - PRINT A STRING
  55.     trap    #1
  56.     addq.l    #6,sp
  57.     
  58.     rts
  59.  
  60. **************************************************************************
  61.  
  62. * Change the variables using data from the mouse packet
  63.  
  64. readmouse
  65.  
  66. * load the registers with the X variable
  67.     
  68.     move.w    mousex,d0    put the x variable into d0
  69.     clr.w    d1        ensure no rubbish left in the word
  70.     move.b    mpack+1,d1    put the x data packet into d1
  71.     
  72. * do the calculating
  73.  
  74.     jsr    calcmouse    calculate the changes
  75.  
  76. * test the result for going beyond the screen limits
  77.     
  78.     cmpi.w    #xmin,d0    compare the result with the left border
  79.     bmi    .limitl        if it's less than xmin then branch
  80.  
  81. .aa    cmpi.w    #xmax,d0    compare the result with the right border
  82.     bgt    .limitr        if it's more than xmax then branch
  83.  
  84. * save the resulting value
  85.  
  86. .bb    move.w    d0,mousex    put new value in store
  87.     
  88. * load the registers with the Y variable
  89.     
  90.     move.w    mousey,d0    put the y variable into d0
  91.     clr.w    d1        ensure no rubbish left in the word
  92.     move.b    mpack+2,d1    put the y data packet into d1
  93.     
  94. * do the calculating
  95.  
  96.     jsr    calcmouse    calculate the changes
  97.  
  98. * test the result for going beyond the screen limits
  99.     
  100.     cmpi.w    #ymin,d0    compare the result with the top border
  101.     bmi    .limitt        if it's less than ymin then branch
  102.  
  103. .cc    cmpi.w    #ymax,d0    compare the result with the bottom border
  104.     bgt    .limitb        if it's more than ymax then branch
  105.  
  106. * save the resulting value
  107.  
  108. .dd    move.w    d0,mousey    put new value in store
  109.  
  110. * clear the data packet.
  111.  
  112.     move.l    #0,mpack    zero the 4 bytes at 'mpack'
  113.     
  114.     rts
  115.  
  116. * a few short subroutines from above...
  117.  
  118. .limitl    move.w    #xmin,d0    set d0 to xmin
  119.     bra    .aa
  120.  
  121. .limitr    move.w    #xmax,d0    set d0 to xmax
  122.     bra    .bb
  123.  
  124. .limitt    move.w    #ymin,d0    set d0 to ymin
  125.     bra    .cc
  126.     
  127. .limitb    move.w    #ymax,d0    set d0 to xmax
  128.     bra    .dd
  129.  
  130. calcmouse
  131.  
  132. * put variable to change into d0, word sized.
  133. * put the data byte into d1, word sized.
  134. * call here.
  135.  
  136. * multiply the data byte by this factor, for mouse sensitivity
  137.  
  138.     tst.b    d1        set the ccr for the data type
  139.     beq    .end        if it's a zero, no change so branch out
  140.     bmi    .neg        if it's negative then branch
  141.     
  142. * it must be positive, so add it to the variable
  143.  
  144.     add.w    d1,d0        add data to value. result in d0
  145.     bra    .end        then leave
  146.     
  147. * it must be negative, so take the data from the variable.
  148.  
  149. .neg
  150.     ext.w    d1        extend the byte to the word
  151.     neg.w    d1        then 2's complement it.
  152.     sub.w    d1,d0        take data from value. result in d0.
  153.     
  154. .end    rts
  155.  
  156. **************************************************************************
  157.  
  158. * look for input from the user
  159.  
  160. input
  161.  
  162. * get a scancode
  163.  
  164.     jsr    getscan        look for a keypress
  165.     cmpi.w    #1,scancode    is it code 1 <Esc>?
  166.     beq    exit        yes, return to desktop
  167.     
  168.     rts
  169.     
  170. **************************************************************************
  171.  
  172. * INITIALISE
  173.  
  174. init
  175.  
  176. * who cares what rez we're in...
  177. * or where the screen is...
  178. * or what the palette is...
  179. * yet...
  180.  
  181. * set initial value of mousex and mousey
  182.  
  183.     move.w    #55,mousex
  184.     move.w    #295,mousey
  185.     
  186. * set background colour to print text to green
  187.  
  188.     move.l    #textback2,-(sp)    address of esc string
  189.     move.w    #9,-(sp)        function number
  190.     trap    #1            GEMDOS 9 - PRINT A STRING
  191.     addq.l    #6,sp            tidy
  192.  
  193. * get the keyboard vector table
  194.  
  195.     move.w    #34,-(sp)    function number
  196.     trap    #14        XBIOS 34 - GET TABLE
  197.     addq.l    #2,sp        tidy
  198.     move.l    d0,keyvectable    address of keyboard vector table saved
  199.     
  200. * put address of my mouse handler into the table
  201.  
  202.     move.l    d0,a0            address of start of table
  203.     lea    mhand,a1        address of my routine
  204.     move.l    16(a0),oldmousevec    save the original mouse vector
  205.     move.l    a1,16(a0)        and replace with my routine's address
  206.     
  207. * tell the IKBD to use relative mouse movement
  208.  
  209.     pea    ikcom03        address of IKbd COMmand 03 onto stack
  210.     move.w    #0,-(sp)    length of command minus 1
  211.     move.w    #25,-(sp)    function number
  212.     trap    #14        XBIOS 25 - SEND IKBD COMMAND
  213.     add.l    #8,sp        tidy
  214.  
  215.     rts
  216.     
  217. **************************************************************************
  218.  
  219. * display the variables
  220.  
  221. printxy
  222.  
  223. * turn mousex into decimal in decbuf
  224.  
  225.     clr.l    d0        ensure totally clear register
  226.     move.w    mousex,d0    put mousex into d0
  227.     jsr    bins2dec    go to convertion routine    
  228.  
  229. * set the cursor
  230.  
  231.     move.b    #19,cursx    set cursx
  232.     move.b    #5,cursy    set cursy
  233.     jsr    setcurs        set the cursor position on screen
  234.  
  235. * print value of x in decimal
  236.  
  237.     move.l    #decbuf,-(sp)    address of string onto stack
  238.     move.w    #9,-(sp)    GEMDOS 9 - PRINT A STRING
  239.     trap    #1
  240.     addq.l    #6,sp
  241.     
  242. * turn mousey into decimal in decbuf
  243.  
  244.     clr.l    d0        ensure totally clear register
  245.     move.w    mousey,d0    put mousex into d0
  246.     jsr    bins2dec    go to convertion routine    
  247.  
  248. * set the cursor
  249.  
  250.     move.b    #19,cursx    set cursx
  251.     move.b    #7,cursy    set cursy
  252.     jsr    setcurs        set the cursor position on screen
  253.  
  254. * print value of y in decimal
  255.  
  256.     move.l    #decbuf,-(sp)    address of string onto stack
  257.     move.w    #9,-(sp)    GEMDOS 9 - PRINT A STRING
  258.     trap    #1
  259.     addq.l    #6,sp
  260.  
  261.     rts
  262.  
  263. *************************************************************************
  264.  
  265. * SET THE CURSOR POSITION
  266.  
  267. * set BOTH 'cursx' & 'cursy' values before calling.  Note that if one of
  268. * them is left from a previous call, it will have 31 added to it AGAIN.
  269.  
  270. setcurs
  271.  
  272.     movem.l    d0-d7/a0-a6,-(sp)    save registers
  273.  
  274. * ascii the cursor references
  275.  
  276.     add.b    #31,cursx    adding 31 makes it ascii for printing
  277.     add.b    #31,cursy    adding 31 makes it ascii for printing
  278.  
  279. * print the cursor string, setting the screen cursor
  280.  
  281.     move.l    #curspos,-(sp)    address of string onto stack
  282.     move.w    #9,-(sp)    GEMDOS 9 - PRINT A STRING
  283.     trap    #1
  284.     addq.l    #6,sp
  285.     
  286.     movem.l    (sp)+,d0-d7/a0-a6    restore registers
  287.  
  288.     rts
  289.  
  290.  
  291. *************************************************************************
  292.  
  293. * CONVERT A SIGNED, WORD-SIZED, NUMBER IN BINARY, TO A DECIMAL STRING OF
  294. * ASCII CHARACTERS READY TO PRINT OUT. 
  295.  
  296. * The ascii result will be justified to the left, and padded out with
  297. * spaces on the right. Negative numbers will be prefixed with a minus sign.
  298.  
  299. * Requires an 8 byte buffer. A word sized, signed, number can have values
  300. * ranging from -32768 to +32767. That gives 5 digits, a possible minus sign
  301. * if it is negative, and a zero byte marking the end of the string to
  302. * be printed. (The eigth byte ensures a word boundry.)
  303.  
  304. * put the number to be printed into the low word of d0
  305. * put the address of the 7 byte buffer into a0
  306.  
  307. bins2dec
  308.  
  309. * set up the registers
  310.  
  311.     movem.l    a0-a1/d0-d4,-(sp)    save registers
  312.  
  313.     move.l    #decbuf,a0    address of buffer into a0    
  314.     move.l    #decbuf,a1    address of buffer into a1
  315.     clr.l    d1        counter - number of decimal digits
  316.     clr.l    d2        flag - leading zero found - d2=0
  317.     
  318. * is the number negative?
  319.     
  320.     tst.w    d0        is d0 negative?
  321.     bpl    calcdg        no, branch
  322.  
  323. * if so, negate d0 & put a minus sign first on the string
  324.  
  325.     neg.w    d0        take d0 from 0. result in d0.
  326.     move.b    #"-",(a1)+    put the ascii for a minus sign in buffer
  327.     addq.b    #1,d1        inc number of digits counter
  328.     
  329. * now calculate the seperate digits
  330.     
  331. calcdg    move.w    #10000,d3    d3 = the divisor
  332.     bsr    divs16        divide d0 by d3 and save ascii in buffer
  333.  
  334.     move.w    #1000,d3
  335.     bsr    divs16
  336.  
  337.     move.w    #100,d3
  338.     bsr    divs16    
  339.  
  340.     move.w    #10,d3
  341.     bsr    divs16    
  342.     
  343.     add.b    #'0',d0        convert the units digit to ascii
  344.     move.b    d0,(a1)+    always save the units digit
  345.     addq.b    #1,d1        inc number of digits
  346.  
  347.     cmpi.b    #6,d1        Has the number come up with 6 digits?
  348.     beq    .end        Yes, branch. no spaces need to be added.
  349.     
  350. * add spaces
  351.  
  352.     move.w    #6,d3        d3 now holds the max number of digits
  353.     sub.w    d1,d3        take d1 from d3. d3 now holds number of spaces
  354.  
  355. .loop    move.b    #" ",(a1)+    put ascii for a space into buffer
  356.     subi.w    #1,d3        take one from number of spaces to be added
  357.     tst.w    d3        test d3 for zero, all spaces entered
  358.     bne    .loop        if not zero, branch.
  359.     
  360. .end    move.b    #0,(a1)+    put a terminating zero in the buffer
  361.     movem.l    (sp)+,a0-a1/d0-d4    restore registers
  362.     rts    
  363.  
  364. * divide a (long) number in d0 by a (word) number in d3 low word
  365.     
  366. divs16    divu    d3,d0        unsigned division
  367.     move.w    d0,d4        save the answer to d4
  368.     clr.w    d0        set low word of d0 to 0
  369.     swap    d0        put the remainder into low word of d0
  370.     
  371.     tst.b    d2        test the leading zero flag
  372.     bne    svdig        branch if the flag is not zero
  373.     
  374.     tst.b    d4        test this digit for zero
  375.     beq    enddiv        yes, branch
  376.     addq.b    #1,d2        d4 not zero, so set the flag to say so
  377.  
  378. * save the number in d4 to the buffer as ascii
  379.  
  380. svdig    add.b    #'0',d4        make into ascii
  381.     move.b    d4,(a1)+    save ascii digit in buffer
  382.     addq.b    #1,d1        inc number of digits        
  383.  
  384. enddiv    rts
  385.  
  386. **************************************************************************
  387.  
  388. * My mouse data packet handler.
  389.  
  390. * called by the IKBD exception routine
  391.  
  392. mhand
  393.     move.b    #3,d0        d0=counter, 3 to 0
  394.     lea    mpack,a1    address of buffer to store packet
  395. .loop    move.b    (a0)+,(a1)+    store a byte of the packet
  396.     subq.b    #1,d0        dec counter
  397.     bne    .loop        if not zero, branch
  398.  
  399.     rts
  400.     
  401. *************************************************************************
  402.  
  403. * GET, (BUT DON'T EXAMINE), A KEYPRESS
  404.  
  405. * result returned as a word in 'scancode'
  406.  
  407. getscan
  408.     movem.l    d0-d7/a0-a6,-(sp)    Save registers
  409.     move.w    #0,scancode        Ensure a null return unless a key is found
  410.     
  411. * has a key been pressed?
  412.  
  413.     move.w    #$0b,-(sp)    GEMDOS $0B - KEYBOARD BUFFER STATUS?
  414.     trap    #1        Result returned in d0.
  415.     addq.l    #2,sp
  416.  
  417.     tst.w    d0        d0.w=0, no keys pressed, -1 = yes
  418.     beq    .end        no keypresses in buffer, so return
  419.     
  420. * yes, get it's scancode
  421.  
  422.     move.w    #$08,-(sp)    GEMDOS 8 - GET A KEYPRESS
  423.     trap    #1
  424.     addq.l    #2,sp
  425.  
  426.     swap    d0        put scancode into low word
  427.     move.w    d0,scancode    save the scancode for later
  428.  
  429. .end    
  430.     movem.l    (sp)+,d0-d7/a0-a6    restore registers
  431.     rts
  432.     
  433. ******************************************************************************
  434.  
  435. * print the messages
  436.  
  437. printmes
  438.  
  439. * set the cursor
  440.  
  441.     move.b    #5,cursx    set cursx
  442.     move.b    #3,cursy    set cursy
  443.     jsr    setcurs        set the cursor position on screen
  444.  
  445. * print  message(04) 'using relative pakets'
  446.  
  447.     move.l    #mes04,-(sp)    address of message
  448.     move.w    #9,-(sp)    GEMDOS 9 - PRINT A STRING
  449.     trap    #1
  450.     addq.l    #6,sp
  451.  
  452. * set the cursor
  453.  
  454.     move.b    #5,cursx    set cursx
  455.     move.b    #5,cursy    set cursy
  456.     jsr    setcurs        set the cursor position on screen
  457.  
  458. * print mes01 (x is)
  459.  
  460.     move.l    #mes01,-(sp)    address of string onto stack
  461.     move.w    #9,-(sp)    GEMDOS 9 - PRINT A STRING
  462.     trap    #1
  463.     addq.l    #6,sp
  464.  
  465. * set the cursor
  466.  
  467.     move.b    #5,cursx    set cursx
  468.     move.b    #7,cursy    set cursy
  469.     jsr    setcurs        set the cursor position on screen
  470.  
  471. * print mes02 (y is)
  472.  
  473.     move.l    #mes02,-(sp)    address of string onto stack
  474.     move.w    #9,-(sp)    GEMDOS 9 - PRINT A STRING
  475.     trap    #1
  476.     addq.l    #6,sp
  477.  
  478. * set the cursor
  479.  
  480.     move.b    #5,cursx    set cursx
  481.     move.b    #11,cursy    set cursy
  482.     jsr    setcurs        set the cursor position on screen
  483.  
  484. * print message(03) 'press esc'
  485.  
  486.     move.l    #mes03,-(sp)    address of message
  487.     move.w    #9,-(sp)    GEMDOS 9 - PRINT A STRING
  488.     trap    #1
  489.     addq.l    #6,sp
  490.  
  491.     rts
  492.  
  493. **************************************************************************
  494.  
  495. * restore values to the system before exiting
  496.  
  497. exit
  498.  
  499. * Restore the original IKBD mouse vector
  500.  
  501.     move.l    keyvectable,a0    address of start of table
  502.     move.l    oldmousevec,a1    address of original vector
  503.     move.l    a1,16(a0)    replace original vector in the table
  504.  
  505. * restore the IKBD to relative mouse movement
  506.  
  507.     pea    ikcom03        address of IKbd COMmand 03 onto stack
  508.     move.w    #0,-(sp)    length of command minus 1
  509.     move.w    #25,-(sp)    function number
  510.     trap    #14        XBIOS 25 - SEND IKBD COMMAND
  511.     add.l    #8,sp        tidy
  512.  
  513. * Return to the desktop
  514.  
  515.     move.w    #0,-(sp)    function number
  516.     trap    #1        GEMDOS 0 - TERMinate
  517.     
  518. **************************************************************************
  519.  
  520. * Storeage areas.
  521.  
  522.     data
  523.     
  524. curspos        dc.b    27,'Y'        Escape command - set cursor
  525. cursy        dc.b    32
  526. cursx        dc.b    32
  527.         dc.b    0
  528.         
  529. ikcom01        dc.b    $09        IKBD command - set mouse absolute
  530.         dc.b    $01,$3f        xmax (319)
  531.         dc.b    $00,$c7        ymax (199)
  532. ikcom02        dc.b    $0b,2,2        IKBD command - set threshold
  533. ikcom03        dc.b    $08        IKBD command - set mouse relative
  534.  
  535. mes01        dc.b    '"MOUSEX" is : ',0    informative messages
  536. mes02        dc.b    '"MOUSEY" is : ',0
  537. mes03        dc.b    'Press <ESC> to exit...',0
  538. mes04        dc.b    'Using "relative" IKBD data packets.',0
  539.  
  540. textback0    dc.b    27,'c','0',0    escape strings to print. will set
  541. textback1    dc.b    27,'c','1',0    the background colour of printed
  542. textback2    dc.b    27,'c','2',0    text to the colour register noted.
  543. textback3    dc.b    27,'c','3',0
  544.     
  545.     bss
  546.     
  547. counter        ds.l    1    variable for a loop counter
  548. decbuf        ds.w    4    buffer for printing a signed number in dec
  549. keyvectable    ds.l    1    address of the keyboard vector table
  550. mousex        ds.w    1    mouse pointer reference
  551. mousey        ds.w    1    mouse pointer reference
  552. mpack        ds.l    1    buffer to store mouse data packets
  553. oldmousevec    ds.l    1    address of original mouse packet handler
  554. random        ds.w    1    store for random numbers
  555. scancode    ds.w    1    scancode of a keypress
  556.  
  557.  
  558.  
  559.