home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / asm / misc.s < prev    next >
Encoding:
Text File  |  1998-10-04  |  11.8 KB  |  677 lines

  1. ;
  2. ; misc.s -- an ACE linked library module: miscellaneous functions.
  3. ; Copyright (C) 1998 David Benn
  4. ; This program is free software; you can redistribute it and/or
  5. ; modify it under the terms of the GNU General Public License
  6. ; as published by the Free Software Foundation; either version 2
  7. ; of the License, or (at your option) any later version.
  8. ;
  9. ; This program is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with this program; if not, write to the Free Software
  16. ; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17. ;
  18. ; Author: David J Benn
  19. ;   Date: 3rd-30th November, 1st-13th December 1991,
  20. ;      20th, 23rd,25th-27th January 1992, 
  21. ;         2nd,4th,6th,12th-19th,21st-24th,29th February 1992,
  22. ;      1st,14th March 1992,
  23. ;      4th,7th,21st,22nd,26th April 1992,
  24. ;      2nd,3rd,5th,7th,8th,10th-17th May 1992,
  25. ;      6th,8th,11th,12th,28th,30th June 1992,
  26. ;      1st-3rd,13th,14th,18th-20th,22nd July 1992,
  27. ;      9th August 1992,
  28. ;      5th,6th December 1992,
  29. ;      6th January 1993,
  30. ;      7th,16th,18th February 1993,
  31. ;      1st,7th March 1993,
  32. ;      9th,25th,30th May 1993,
  33. ;      12th,16th,29th June 1993,
  34. ;      6th,25th September 1993,
  35. ;      9th,11th,25th,28th October 1993,
  36. ;      10th,15th November 1993
  37. ;      5th December 1993,
  38. ;      5th,7th,10th,12th-15th,23rd January 1994,
  39. ;      6th February 1994,
  40. ;      19th March 1994,
  41. ;      3rd September 1994
  42. ;
  43. ; registers d0-d6 and a0-a3 are modified by some of the following. BEWARE!
  44. ;
  45. ; a4,a5 are used by link/unlk.
  46. ; a6 is library base holder.
  47. ; a7 is stack pointer. 
  48. ; d7 is used for array index calculations.
  49. ;
  50.  
  51. ; * CONSTANTS *
  52.  
  53. PUBLIC        equ    1
  54. CHIP         equ    2
  55. FAST        equ    4
  56. LARGEST     equ    131072
  57. POTGO        equ    $dff034
  58. POT1DAT        equ    $dff014
  59. cp_x        equ    36
  60. cp_y        equ    38
  61. Font        equ    52
  62. tf_YSize    equ    20
  63. tf_XSize    equ    24
  64. UserPort    equ    86
  65. GZZMouseX    equ    108
  66. GZZMouseY    equ    110
  67.     
  68.        ; miscellaneous functions
  69.        xdef      _rnd
  70.        xdef      _absw
  71.        xdef      _absl
  72.        xdef      _sgnw
  73.        xdef      _sgnl
  74.     xdef    _inputshort
  75.     xdef    _inputlong
  76.     xdef    _stick
  77.     xdef    _strig
  78.     xdef    _mouse
  79.     xdef    _fre
  80.     xdef    _potx
  81.     xdef    _poty
  82.     xdef    _err
  83.     xdef    _testerror
  84.  
  85.     ; miscellaneous routines
  86.     xdef    _randomise
  87.     xdef    _open_library
  88.     xdef    _close_library
  89.     xdef    _printLF
  90.     xdef    _printspc
  91.     xdef    _printqun
  92.     xdef    _sleep
  93.  
  94.     ; exportable symbols
  95.     xdef    _error_code
  96.     
  97.        ; external references
  98.     xref    _inputstring
  99.     xref    _short_from_string
  100.     xref    _long_from_string    
  101.     xref    _strlen
  102.     xref    _strip    
  103.        xref      _RangeRand
  104.     xref    _RangeSeed
  105.     xref    _DOSBase
  106.     xref    _LVODelay
  107.     xref    _LVOWrite
  108.     xref    _MathBase
  109.     xref    _LVOSPFix
  110.     xref    _LVOSPFlt
  111.     xref    _LVOSPDiv
  112.     xref    _LVOSPMul
  113.     xref    _LVOSPSub
  114.     xref    _LVOSPAdd
  115.     xref    _LVOSPCmp
  116.     xref     _AbsExecBase
  117.     xref    _LVOAvailMem
  118.     xref    _LVOGetMsg
  119.     xref    _LVOReplyMsg
  120.     xref    _LVOWaitPort
  121.     xref    _LVOOpenLibrary
  122.     xref    _LVOCloseLibrary
  123.     xref    _stdout
  124.     xref    _IntuiMode
  125.     xref    _Wdw
  126.     xref    _TaskAddress
  127.     xref    _Ustringinput
  128.  
  129.     SECTION misc_code,CODE
  130.  
  131. ;
  132. ; short & long abs functions. assumes short/long value in d0.
  133. ;
  134. _absw:
  135.        cmp.w #0,d0    ; num < 0?
  136.        bge.s _quitabsw
  137.        neg.w d0
  138. _quitabsw:
  139.        rts
  140.  
  141. _absl:
  142.        cmp.l #0,d0
  143.        bge.s _quitabsl
  144.        neg.l d0
  145. _quitabsl:
  146.        rts
  147.  
  148. ;
  149. ; short & long sgn functions. assumes short/long value in d0.
  150. ; result returned in d0 (-1,0,1).
  151. ;
  152. _sgnw:
  153.     cmpi.w    #0,d0
  154.     beq.s    _sgnwzero
  155.     blt.s    _sgnwneg
  156.     bra.s    _sgnwpos
  157. _sgnwzero:
  158.     moveq    #0,d0
  159.     rts
  160. _sgnwneg:
  161.     moveq    #-1,d0
  162.     rts
  163. _sgnwpos:
  164.     moveq    #1,d0
  165.     rts
  166.  
  167. _sgnl:
  168.     cmpi.l    #0,d0
  169.     beq.s    _sgnlzero
  170.     blt.s    _sgnlneg
  171.     bra.s    _sgnlpos
  172. _sgnlzero:
  173.     moveq    #0,d0
  174.     rts
  175. _sgnlneg:
  176.     moveq    #-1,d0
  177.     rts
  178. _sgnlpos:
  179.     moveq    #1,d0
  180.     rts
  181.  
  182. ;
  183. ; input a short integer value and return it in d0.
  184. ; see lex.c insymbol() function re: technique.
  185. ; (a1 = input buffer)
  186. ;
  187. _inputshort:
  188.     move.l    a1,-(sp)
  189.     jsr    _Ustringinput
  190.     jsr    _short_from_string
  191.     addq    #4,sp
  192.     rts
  193.  
  194. ;
  195. ; input a long integer value and return it in d0.
  196. ; see lex.c insymbol() function re: technique.
  197. ; (a1 = input buffer)
  198. ;
  199. _inputlong:
  200.     move.l    a1,-(sp)
  201.     jsr    _Ustringinput
  202.     jsr    _long_from_string
  203.     addq    #4,sp
  204.     rts
  205.  
  206. ;
  207. ; STICK - returns in d0 the state of a joystick (only port 2 at the moment).
  208. ;    - a parameter in d0 determines the port and axis to be sampled.
  209. ;    
  210. ;    The hardware is directly accessed by this routine (_joy1dat = DFF00C)
  211. ;    (see Amiga Hardware Reference Manual, pg 220). 
  212. ;    A future version will use the gameport.device.  
  213. ;
  214. _stick:
  215.     cmpi.w    #0,d0
  216.     beq.s    _stick0
  217.     cmpi.w    #1,d0
  218.     beq.s    _stick1
  219.     cmpi.w    #2,d0
  220.     beq.s    _stick2
  221.     cmpi.w    #3,d0
  222.     beq.s    _stick3
  223.  
  224.     moveq    #0,d0            ; none of the above
  225.     rts
  226.  
  227. _stick0:
  228.     ; game port 1 in x direction
  229.     moveq    #0,d0
  230.     rts
  231.  
  232. _stick1:
  233.     ; game port 1 in y direction
  234.     moveq    #0,d0
  235.     rts
  236.  
  237. _stick2:
  238.     ; game port 2 in x direction
  239.     move.l    _joy1dat,a0
  240.  
  241.     move.w    (a0),d1
  242.     andi.w    #2,d1
  243.     cmpi.w    #2,d1
  244.     beq.s    _stickpos    ; right
  245.  
  246.     move.w    (a0),d1
  247.     andi.w    #512,d1
  248.     cmpi.w    #512,d1
  249.     beq.s    _stickneg    ; left
  250.  
  251.     moveq    #0,d0        ; joystick not moving in x axis
  252.     rts
  253.     
  254. _stick3:
  255.     ; game port 2 in y direction
  256.     move.l    _joy1dat,a0
  257.  
  258.     move.w    (a0),d1
  259.     andi.w    #512,d1
  260.     move.w    (a0),d2
  261.     andi.w    #256,d2
  262.     eor.w    d2,d1
  263.  
  264.     cmpi.w    #256,d1
  265.     beq.s    _stickpos    ; up    
  266.  
  267.     move.w    (a0),d1
  268.     andi.w    #2,d1
  269.     move.w    (a0),d2
  270.     andi.w    #1,d2
  271.     eor.w    d2,d1
  272.  
  273.     cmpi.w    #1,d1
  274.     beq.s    _stickneg    
  275.     cmpi.w    #2,d1
  276.     beq.s    _stickneg    ; down
  277.  
  278.     moveq    #0,d0        ; joystick not moving in y axis
  279.     rts
  280.  
  281. _stickpos:
  282.     moveq    #1,d0
  283.     rts
  284.  
  285. _stickneg:
  286.     moveq    #-1,d0
  287.     rts
  288.  
  289. ;
  290. ; STRIG - returns in d0 the state of a joystick's fire button
  291. ;          (only port 2 at the moment).
  292. ;    - a parameter in d0 determines the port to be sampled.
  293. ;    
  294. ;    The hardware is directly accessed by this routine (_data_reg_A=BFE001).
  295. ;    A future version will use the gameport.device.  
  296. ;
  297. _strig:
  298.     cmpi.w    #0,d0
  299.     beq.s    _strig0
  300.     cmpi.w    #1,d0
  301.     beq.s    _strig1
  302.     cmpi.w    #2,d0
  303.     beq.s    _strig2
  304.     cmpi.w    #3,d0
  305.     beq.s    _strig3
  306.  
  307.     moveq    #0,d0            ; none of the above
  308.     rts
  309.  
  310. _strig0:
  311.     ; has joystick in port 1 fired since last strig(0)?
  312.     moveq    #0,d0        
  313.     rts
  314.     
  315. _strig1:
  316.     ; is joystick in port 1 currently firing?
  317.     moveq    #0,d0        
  318.     rts
  319.  
  320. _strig2:
  321.     ; has joystick in port 2 fired since last strig(2)?
  322.     moveq    #0,d0        
  323.     rts
  324.     
  325. _strig3:
  326.     ; is joystick in port 2 currently firing?
  327.     move.l    _data_reg_A,a0
  328.     move.b    (a0),d1
  329.     cmpi.b    #124,d1
  330.     beq.s    _firing
  331.     
  332.     moveq    #0,d0        ; fire button not pressed
  333.     rts
  334.  
  335. _firing:
  336.     moveq    #-1,d0
  337.     rts
  338.  
  339. ;
  340. ; MOUSE - returns in d0 information about the mouse (see ace.doc).
  341. ;    - a parameter in d0 determines what information is returned.
  342. ;
  343. ;    A future version of MOUSE(0) may use the gameport.device/Intuition.
  344. ;      
  345. ;
  346. _mouse:
  347.     cmpi.w    #0,d0
  348.     beq.s    _mouse0
  349.     cmpi.w    #1,d0
  350.     beq.s    _mouse1
  351.     cmpi.w    #2,d0
  352.     beq.s    _mouse2
  353.  
  354.     moveq    #0,d0
  355.     rts
  356.  
  357. _mouse0:
  358.     move.l    _data_reg_A,a0
  359.     move.b    (a0),d1
  360.     andi.w    #$40,d1
  361.     cmpi.b    #$40,d1
  362.     bne.s    _leftbutton
  363.     
  364.     moveq    #0,d0            ; left mouse button not pressed
  365.     rts
  366.  
  367. _leftbutton:
  368.     moveq    #-1,d0            ; left mouse button is being pressed
  369.     rts
  370.  
  371. _mouse1:
  372.     movea.l    _Wdw,a0
  373.     move.w    GZZMouseX(a0),d0    ; x-coordinate of mouse (Wdw)
  374.     rts
  375.     
  376. _mouse2:
  377.     movea.l    _Wdw,a0
  378.     move.w    GZZMouseY(a0),d0    ; y-coordinate of mouse (Wdw)
  379.     rts
  380.  
  381. ;
  382. ; FRE(n) - Gives info' about memory. d0=attribute. Returns long integer in d0.
  383. ; [attribute: -1=CHIP+FAST, 0=CHIP, 1=FAST, 2=LARGEST CHIP, 3=LARGEST FAST]
  384. ;
  385. _fre:
  386.     movea.l    _AbsExecBase,a6
  387.  
  388.     cmpi.w    #-1,d0
  389.     beq.s    _frecf        ; CHIP+FAST
  390.  
  391.     cmpi.w    #0,d0
  392.     beq.s    _frec        ; CHIP
  393.  
  394.     cmpi.w    #1,d0
  395.     beq.s    _fref        ; FAST
  396.  
  397.     cmpi.w    #2,d0
  398.     beq.s    _frelc        ; LARGEST CHIP
  399.  
  400.     cmpi.w    #3,d0
  401.     beq.s    _frelf        ; LARGEST FAST
  402.  
  403.     moveq    #0,d0        ; illegal n
  404.     rts
  405.  
  406. _frecf:
  407.     move.l    #CHIP,d1
  408.     jsr    _LVOAvailMem(a6)
  409.     move.l    d0,d4
  410.     move.l    #FAST,d1
  411.     jsr    _LVOAvailMem(a6)
  412.     add.l    d4,d0
  413.     rts
  414.  
  415. _frec:
  416.     move.l    #CHIP,d1
  417.     jsr    _LVOAvailMem(a6)
  418.     rts    
  419.  
  420. _fref:
  421.     move.l    #FAST,d1
  422.     jsr    _LVOAvailMem(a6)
  423.     rts    
  424.  
  425. _frelc:
  426.     move.l    #LARGEST,d1
  427.     or.l    #CHIP,d1
  428.     jsr    _LVOAvailMem(a6)
  429.     rts    
  430.  
  431. _frelf:
  432.     move.l    #LARGEST,d1
  433.     or.l    #FAST,d1
  434.     jsr    _LVOAvailMem(a6)
  435.     rts    
  436.  
  437. ;
  438. ; POTX(n) - returns A/D value of potentiometer-x (pin 5) of game controller.
  439. ;      - d0=n (0 or 1) and short integer returned in d0.
  440. ;      - in the current version, POTX(0) always returns 0 (game port 1).
  441. ;      - the hardware is directly sampled.
  442. ;
  443. _potx:
  444.     cmpi.w    #0,d0
  445.     beq.s    _potx0
  446.  
  447.     cmpi.w    #1,d0
  448.     beq.s    _potx1
  449.  
  450.     move.w    #0,d0    ; illegal n
  451.     rts
  452.  
  453. _potx0:
  454.     ; game port 1
  455.     move.w    #0,d0        
  456.     rts
  457.  
  458. _potx1:
  459.     ; game port 2
  460.     move.l    #POTGO,a0
  461.     and.b    #1,(a0)
  462.     movea.l    _DOSBase,a6
  463.     move.l    #1,d1
  464.     jsr    _LVODelay(a6)
  465.     move.l    #POT1DAT,a0
  466.     move.w    (a0),d0
  467.     and.w    #$00ff,d0    ; get low byte    
  468.     rts
  469.  
  470. ;
  471. ; POTY(n) - returns A/D value of potentiometer-y (pin 9) of game controller.
  472. ;      - d0=n (0 or 1) and short integer returned in d0.
  473. ;      - in the current version, POTY(0) always returns 0 (game port 1).
  474. ;      - the hardware is directly sampled.
  475. ;
  476. _poty:
  477.     cmpi.w    #0,d0
  478.     beq.s    _poty0
  479.  
  480.     cmpi.w    #1,d0
  481.     beq.s    _poty1
  482.  
  483.     move.w    #0,d0    ; illegal n
  484.     rts
  485.  
  486. _poty0:
  487.     ; game port 1
  488.     move.w    #0,d0        
  489.     rts
  490.  
  491. _poty1:
  492.     ; game port 2
  493.     move.l    #POTGO,a0
  494.     and.b    #1,(a0)
  495.     movea.l    _DOSBase,a6
  496.     move.l    #1,d1
  497.     jsr    _LVODelay(a6)
  498.     move.l    #POT1DAT,a0
  499.     move.w    (a0),d0
  500.     and.w    #$ff00,d0    ; get high byte
  501.     lsr.w    #8,d0        ; d0 \= 256
  502.     rts    
  503.  
  504. ;
  505. ; RND function. Returns pseudo-random number between 0 and 1 in d0. 
  506. ;         Makes use of amiga.lib RangeRand() function.
  507. ;
  508. _rnd:
  509.     move.l    #$ffff,-(sp)        ; max value+1 = 65535
  510.     jsr    _RangeRand        ; returns random # in d0 (0..65534)
  511.     addq    #4,sp
  512.     cmpi.l    #0,d0
  513.     bne.s    _0_to_1            
  514.     moveq    #1,d0            ; if zero returned, make d0=1
  515. _0_to_1:    
  516.     move.l    _MathBase,a6
  517.     jsr    _LVOSPFlt(a6)
  518.     move.l    #$ffff0050,d1       ; div by 65535.0 for 0 < value < 1
  519.     jsr    _LVOSPDiv(a6)
  520.     rts
  521.  
  522. ;
  523. ; RANDOMIZE - Changes amiga.lib's _RangeSeed with value in d0.
  524. ;            [see also factor() in ace.c]
  525. ;
  526. _randomise:
  527.     cmpi.l    #0,d0
  528.     bge.s    _randseed
  529.     neg.l    d0            ; make sure it's positive
  530. _randseed:
  531.     move.l    d0,_RangeSeed
  532.     jsr    _rnd            ; call RND once to avoid the same
  533.                     ; first value always being generated
  534.                     ; (bug in RangeRand?).
  535.     rts
  536.  
  537. ;
  538. ; print a line-feed.
  539. ;
  540. _printLF:
  541.     move.l    _stdout,d1
  542.     move.l    #_LF,d2
  543.     moveq    #1,d3
  544.     movea.l    _DOSBase,a6
  545.     jsr    _LVOWrite(a6)
  546.     rts
  547.  
  548. ;
  549. ; print a single space.
  550. ;
  551. _printspc:
  552.     move.l    _stdout,d1
  553.     move.l    #_SPC,d2
  554.     moveq    #1,d3
  555.     movea.l    _DOSBase,a6
  556.     jsr    _LVOWrite(a6)
  557.  
  558.     rts
  559.  
  560. ;
  561. ; print a question mark (for INPUT).
  562. ;
  563. _printqun:
  564.     move.l    _stdout,d1
  565.     move.l    #_QUN,d2
  566.     moveq    #2,d3
  567.     movea.l    _DOSBase,a6
  568.     jsr    _LVOWrite(a6)
  569.  
  570.     rts
  571.  
  572. ;
  573. ; SLEEP - wait for mouse/keyboard activity or a menu/gadget event.
  574. ;      (ie: an Intuition event!)
  575. ;
  576. _sleep:    
  577.     ; this routine gets called from C (menu.c)
  578.     movem.l    d0-d7/a0-a6,-(sp)    
  579.                 
  580.     ; IntuiMode?
  581.     cmp.b    #1,_IntuiMode
  582.     bne.s    _quitsleep
  583.     
  584.     movea.l    _AbsExecBase,a6
  585.  
  586. ;_reply_to_pending_msg:
  587. ;    movea.l    _Wdw,a0
  588. ;    movea.l    UserPort(a0),a0
  589. ;    jsr    _LVOGetMsg(a6)
  590. ;    tst.l    d0
  591. ;    beq.s    _intui_wait
  592. ;    movea.l    d0,a1
  593. ;    jsr    _LVOReplyMsg(a6)
  594. ;    bra.s    _reply_to_pending_msg
  595.     
  596. _intui_wait:
  597.     ; wait on IDCMP UserPort    
  598.     movea.l    _Wdw,a0
  599.     movea.l    UserPort(a0),a0
  600.  
  601.     jsr    _LVOWaitPort(a6)
  602.  
  603. _quitsleep:
  604.     movem.l    (sp)+,d0-d7/a0-a6
  605.     rts
  606.         
  607. ;
  608. ; open a shared library - a1=address of library name.
  609. ;            - return library base in d0.
  610. ;
  611. _open_library:
  612.     moveq    #0,d0        ; don't care which version
  613.     movea.l    _AbsExecBase,a6
  614.     jsr    _LVOOpenLibrary(a6)
  615.     rts
  616.  
  617. ;
  618. ; close a shared library - a1=library base.
  619. ;
  620. _close_library:
  621.     cmpa.l    #0,a1
  622.     beq.s    _quit_close_library
  623.     movea.l    _AbsExecBase,a6
  624.     jsr    _LVOCloseLibrary(a6)
  625. _quit_close_library:
  626.     rts
  627.  
  628. ;
  629. ; ERR - return error code from last I/O operation and clear error code.
  630. ;
  631. _err:
  632.     move.l    _error_code,d0        ; return error code
  633.     move.l    #0,_error_code        ; reset error code
  634.     rts
  635.  
  636. ;
  637. ; ON ERROR - return true or false depending upon whether an I/O 
  638. ;            error condition has been logged by an ACE function
  639. ;         (see serial and file functions).
  640. ;
  641. _testerror:
  642.     moveq    #0,d0
  643.     cmpi.l    #0,_error_code        ; is error code = 0?
  644.     bne.s    _quit_testerror        ; No -> indicate error present
  645.     rts
  646. _quit_testerror:
  647.     moveq    #1,d0            ; error condition present
  648.     rts
  649.  
  650. ;*****************************
  651.  
  652.     SECTION misc_data,DATA
  653.  
  654. ; * stick *
  655. _joy1dat:    dc.l     $dff00c
  656.  
  657. ; * strig & mouse *
  658. _data_reg_A:    dc.l     $bfe001
  659.  
  660. ; * printLF/SPC/QUN *
  661. _LF:        dc.b    10
  662. _SPC:        dc.b    32
  663. _QUN:        dc.b    63,32
  664.  
  665. ; * err & testerror *
  666. _error_code:    dc.l    0
  667.  
  668. ;************************
  669.  
  670.     SECTION misc_mem,BSS
  671.  
  672. ; * long/shortinput *
  673. _negate:              ds.b 1
  674.  
  675.     END
  676.