home *** CD-ROM | disk | FTP | other *** search
/ PDA Software Library / pdasoftwarelib.iso / PILOT / PC / RUNWAY / SAMPLE.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-10-02  |  11.4 KB  |  423 lines

  1. ; Sample.asm
  2. ;
  3. ; This sample is meant to serve as a prototype for assembly language Pilot
  4. ; applications. It uses the standard startup code and implements a typical
  5. ; message loop that handles some common events. Several resources (e.g.,
  6. ; menu, icon) are included, a few inline and others from external files
  7. ; generated by PilRC.
  8. ;
  9. ; NOTE: For clarity's sake this code is not highly optimized.
  10. ;
  11. ; NOTE: Where possible this sample uses PalmOS APIs to do conversion between
  12. ; integers and strings (hex and decimal). The Palm APIs have some interesting
  13. ; limitations. StrAToI accepts signed 16-bit ints. StrIToA accepts signed
  14. ; 32-bit ints. StrIToH accepts signed 32-bit ints. My implementation of htoi
  15. ; accepts unsigned 32-bit ints. The least common denominator for all these
  16. ; routines is a signed 16-bit int so I've placed that limit on the
  17. ; conversions.
  18. ;
  19. ; Formatted for 8-space tabs. Assemble with "pila sample.asm" after creating
  20. ; the resources with "pilrc sample.rcp ."
  21. ;
  22. ; By Darrin Massena
  23. ; 29 Jul 96
  24. ; (updated 10 Sep 96)
  25.  
  26. ; The 'Appl' directive sets the application's name and four character id.
  27.  
  28.         Appl    "Sample App", 'samp'
  29.  
  30. ; Pilot.inc contains PalmOS constants, structure offsets, and API trap codes.
  31.  
  32.         include "Pilot.inc"
  33.  
  34.  
  35. ; Startup.inc contains a standard startup function. This function must be
  36. ; the first within an application and is called by the PalmOS after the app
  37. ; is loaded. The startup function in Startup.inc (__Startup__) calls the
  38. ; application-defined function PilotMain.
  39.  
  40.         include "Startup.inc"
  41.  
  42.  
  43. ; Application-defined resource ids
  44.  
  45. kidbAIB         equ             $7FFE
  46. kidrPREF        equ             1
  47. kidrTVER        equ             1
  48. kidrTAIB        equ             1000
  49. kidfMain        equ             1000
  50. kidmMain        equ             1000
  51.  
  52. kidmAbout       equ             1002
  53. kidcConvert     equ             1003
  54. kidcDecimal     equ             1004
  55. kidcHex         equ             1005
  56.  
  57. kidrAboutAlert  equ             1000
  58. kidrInputErrorAlert equ         1001
  59. kidrRangeErrorAlert equ         1002
  60.  
  61. ; Some global variables (a5-relative)
  62.  
  63.         data
  64.  
  65. global gpfldDecimal.l                   ;pointer to the Decimal field
  66. global gpfldHex.l                       ;pointer to the Hex field
  67.  
  68.         code
  69.  
  70. ; ---------------------------------------------------------------------------
  71. ; DWord PilotMain(Word cmd, void *cmdPBP, Word launchflags)
  72. ; PilotMain is called by the startup code and implements a simple event
  73. ; handling loop.
  74.  
  75. proc PilotMain(cmd.w, cmdPBP.l, launchFlags.w)
  76. local   err.w
  77. local   evt.EventType
  78.  
  79. beginproc
  80.         tst.w   cmd(a6)                 ;sysAppLaunchCmdNormalLaunch is 0
  81.         bne     PmReturn                ;not a normal launch, bag out
  82.  
  83.         systrap FrmGotoForm(#kidfMain.w)
  84.  
  85. PmEventLoop
  86. ; Doze until an event arrives
  87.  
  88.         systrap EvtGetEvent(&evt(a6), #evtWaitForever.w)
  89.  
  90. ; System gets first chance to handle the event
  91.  
  92.         systrap SysHandleEvent(&evt(a6))
  93.  
  94.         tst.b   d0                      ;handled?
  95.         bne.s   PmEventDone             ;yep
  96.  
  97. ; Menu handler gets second chance to handle the event
  98.  
  99.         systrap MenuHandleEvent(&0, &evt(a6), &err(a6))
  100.  
  101.         tst.b   d0                      ;handled?
  102.         bne.s   PmEventDone             ;yep
  103.  
  104. ; Application handler gets third chance to handle the event
  105.  
  106.         call    ApplicationHandleEvent(&evt(a6))
  107.  
  108.         tst.b   d0                      ;handled?
  109.         bne.s   PmEventDone             ;yep
  110.  
  111. ; Form handler gets fourth chance to handle the event
  112.  
  113.         call    MainFormHandleEvent(&evt(a6))
  114.  
  115.         tst.b   d0                      ;handled?
  116.         bne.s   PmEventDone             ;yep
  117.  
  118. ; Still not handled. We're not interested in it anymore so let the
  119. ; default form handler take it.
  120.  
  121.         systrap FrmGetActiveForm()
  122.         systrap FrmHandleEvent(a0.l, &evt(a6))
  123.  
  124. ; Return from PilotMain when an appStopEvent is received
  125.  
  126. PmEventDone
  127.         cmpi.w  #appStopEvent,evt+EventType.eType(a6) ;time to stop?
  128.         bne.s   PmEventLoop             ;nope, loop until it is
  129.  
  130. PmReturn
  131.         moveq   #0,d0
  132. endproc
  133.  
  134.  
  135. ; ---------------------------------------------------------------------------
  136. ; Boolean ApplicationHandleEvent(EventType *pevt)
  137. ; Handles these events:
  138. ;       frmLoadEvent
  139.  
  140. proc ApplicationHandleEvent(pevt.l)
  141. beginproc
  142.         movem.l a3,-(a7)                ;save registers we're going to trash
  143.  
  144.         movea.l pevt(a6),a0             ;a0 = pevt
  145.  
  146. ; Handle frmLoadEvents
  147.  
  148.         cmpi.w  #frmLoadEvent,EventType.eType(a0) ;frmLoadEvent?
  149.         bne     AHENotHandled           ;no
  150.  
  151. ; Initialize the form and make it active
  152.  
  153.         systrap FrmInitForm(EventType.data+frmLoad.formID(a0).w)
  154.         move.l  a0,a3                   ;save a copy of FormPtr for later
  155.  
  156.         systrap FrmSetActiveForm(a0.l)
  157.  
  158. ; Get pointers to the two fields we'll be working with (decimal, hex)
  159.  
  160.         systrap FrmGetObjectIndex(a3.l, #kidcDecimal.w)
  161.         systrap FrmGetObjectPtr(a3.l,d0.w)
  162.         move.l  a0,gpfldDecimal(a5)     ;save the field ptr away
  163.  
  164.         systrap FrmGetObjectIndex(a3.l, #kidcHex.w)
  165.         systrap FrmGetObjectPtr(a3.l,d0.w)
  166.         move.l  a0,gpfldHex(a5)         ;save the field ptr away
  167.  
  168.         moveq.l #1,d0                   ;event handled
  169.         bra.s   AHEReturn
  170.  
  171. AHENotHandled
  172.         clr.b   d0
  173.  
  174. AHEReturn
  175.         movem.l (a7)+,a3
  176. endproc
  177.  
  178.  
  179. ; ---------------------------------------------------------------------------
  180. ; Boolean MainFormHandleEvent(EventType *pevt)
  181. ; Handles these events:
  182. ;
  183.  
  184. proc MainFormHandleEvent(pevt.l)
  185. beginproc
  186.  
  187.         movea.l pevt(a6),a0             ;a0 = pevt
  188.         move.w  EventType.eType(a0),d0
  189.  
  190. ; Handle frmOpenEvent
  191.  
  192.         cmp.w   #frmOpenEvent,d0
  193.         bne     MFH1
  194.  
  195. ; Draw the form
  196.  
  197.         systrap FrmGetActiveForm()
  198.         systrap FrmDrawForm(a0.l)
  199.  
  200.         moveq.l #1,d0
  201.         bra     MFHReturn
  202.  
  203. ; Handle frmMenuEvent
  204. MFH1
  205.         cmp.w   #menuEvent,d0
  206.         bne     MFH2
  207.  
  208.         cmp.w   #kidmAbout,EventType.data+menu.itemID(a0)
  209.         bne     MFHNotHandled
  210.  
  211.         systrap FrmAlert(#kidrAboutAlert.w)
  212.  
  213.         moveq.l #1,d0
  214.         bra     MFHReturn
  215.  
  216. ; Handle ctlSelectEvent
  217. MFH2
  218.         cmp.w   #ctlSelectEvent,d0
  219.         bne     MFHNotHandled
  220.  
  221. ; Is the convert button being clicked?
  222.  
  223.         cmp.w   #kidcConvert,EventType.data+ctlEnter.controlID(a0)
  224.         bne     MFHNotHandled
  225.  
  226.         jsr     Convert(pc)             ;yes, convert the number
  227.         bra     MFHReturn
  228.  
  229. MFHNotHandled
  230.         clr.b   d0
  231.  
  232. MFHReturn
  233. endproc
  234.  
  235.  
  236. ; ---------------------------------------------------------------------------
  237. ; void Convert(void)
  238. ;
  239.  
  240. proc Convert()
  241. local   szT.12
  242. beginproc
  243.         movem.l d1-d3/a3,-(a7)
  244.  
  245. ; Yes. Convert the number
  246.  
  247. ; 1. Figure out which field was changed (if any)
  248.         systrap FldDirty(gpfldDecimal(a5).l)
  249.         tst.w   d0
  250.         bne     CVT2
  251.  
  252.         systrap FldDirty(gpfldHex(a5).l)
  253.         tst.w   d0
  254.         beq     CVTReturn
  255.         clr.w   d0
  256. CVT2
  257.         move.w  d0,d3                   ;d3 = fDecChanged
  258.  
  259. ; 2. Get its text and convert it from ASCII to integer
  260.  
  261.         move.l  gpfldHex(a5),a0         ;assume Hex changed
  262.         tst.w   d3                      ;Decimal changed?
  263.         beq     CVT3                    ;no
  264.  
  265.         move.l  gpfldDecimal(a5),a0
  266. CVT3
  267.         systrap FldGetTextPtr(a0.l)
  268.  
  269.         cmp.l   #0,a0                   ;is there any text in the field?
  270.         bne     CVT31                   ;yes
  271.  
  272.         systrap FrmAlert(#kidrInputErrorAlert.w)           ; a comment
  273.         bra     CVTReturn
  274.  
  275. CVT31
  276.         tst.w   d3                      ;convert from decimal?
  277.         beq     CVT4                    ;no
  278.  
  279. ; PalmOS includes a function for converting from ASCII to integer
  280.  
  281.         systrap StrAToI(a0.l)
  282.         bra     CVT5
  283.  
  284. ; Must implement our own ASCII-hex to integer converter
  285.  
  286. CVT4
  287.         move.l  a0,a3                   ;save a copy of the char *
  288.         systrap StrLen(a0.l)
  289.  
  290.         move.w  d0,d2                   ;convert loop counter
  291.         subq.l  #1,d2
  292.         sub.l   d0,d0
  293. CVT41
  294.         lsl.l   #4,d0
  295.         move.b  (a3)+,d1
  296.         sub.b   #'0',d1
  297.         cmp.b   #9,d1
  298.         ble     CVT42
  299.         sub.b   #7,d1
  300.         cmp.b   #$f,d1
  301.         ble     CVT42
  302.         sub.b   #$20,d1
  303. CVT42
  304.         or.b    d1,d0
  305.         dbra    d2,CVT41
  306.  
  307.         cmp.l   #$10000,d0
  308.         blt     CVT5
  309.  
  310.         systrap FrmAlert(#kidrRangeErrorAlert.w)
  311.         bra     CVTReturn
  312.  
  313. ; 3. Convert from integer to ASCII-hex or ASCII-decimal, as appropriate
  314.  
  315. CVT5
  316.         move.l  d0,-(a7)                ;push integer
  317.         pea.l   szT(a6)                 ;push pointer to string buffer
  318.  
  319.         tst.w   d3                      ;convert from decimal?
  320.         bne     CVT6                    ;yes
  321.  
  322.         trap    #15
  323.         dc.w    sysTrapStrIToA
  324.         addq.l  #8,a7                   ;pop the args off the stack
  325.         bra     CVT7
  326.  
  327. CVT6
  328.         trap    #15
  329.         dc.w    sysTrapStrIToH
  330.         addq.l  #8,a7                   ;pop the args off the stack
  331.  
  332. CVT7
  333. ;        addq.l  #8,a7                   ;pop the args off the stack
  334.  
  335. ; 4. Place the new ASCII string in the appropriate field
  336.  
  337.         move.l  gpfldDecimal(a5),a3     ;assume Hex changed
  338.         tst.w   d3                      ;Decimal changed?
  339.         beq     CVT8                    ;no
  340.  
  341.         move.l  gpfldHex(a5),a3
  342. CVT8
  343.         ; erase the old string
  344.  
  345.         systrap FldFreeMemory(a3.l)
  346.  
  347.         ; insert the new string
  348.  
  349.         systrap    StrLen(&szT(a6))
  350.  
  351.  
  352.         lea.l   szT(a6),a0
  353.         tst.w   d3                      ;converting to decimal?
  354.         beq     CVT83                   ;yes
  355.  
  356.         ; truncate hex numbers to 4 digits
  357.  
  358.         move.w  d0,d1
  359.         subq.w  #4,d1
  360.         blt     CVT83
  361.         sub.w   d1,d0
  362.         add.w   d1,a0
  363.         bra     CVT83
  364.  
  365.         ; strip off leading zeros
  366. CVT82
  367.         cmp.b   #'0',(a0)
  368.         bne     CVT81
  369.         addq.l  #1,a0
  370.         subq.w  #1,d0
  371. CVT83
  372.         cmp.w   #1,d0
  373.         bgt     CVT82
  374.  
  375. CVT81
  376.         systrap FldInsert(a3.l,a0.l,d0.w)
  377.  
  378. CVTReturn
  379.         ; Clear the dirty bits on both fields
  380.  
  381.         systrap FldSetDirty(gpfldDecimal(a5).l,#0.b)
  382.         systrap FldSetDirty(gpfldHex(a5).l,#0.b)
  383.  
  384.         movem.l  (a7)+,d1-d3/a3
  385. endproc
  386.  
  387. ;
  388. ; Resources -----------------------------------------------------------------
  389. ;
  390.  
  391. ; Application Icon Bitmap resource. Is automatically converted from Windows
  392. ; format to Pilot format and written as a 'tAIB' rather than 'Tbmp' because
  393. ; kidbAIB is a special value ($7ffe)
  394.  
  395.         res 'WBMP', kidbAIB, "Sample.bmp"
  396.  
  397. ; 'pref' resource. Defines app launch flags, stack and heap size
  398.  
  399.         res 'pref', kidrPREF
  400.         dc.w    sysAppLaunchFlagNewStack|sysAppLaunchFlagNewGlobals|sysAppLaunchFlagUIApp|sysAppLaunchFlagSubCall
  401.         dc.l    $1000                           ; stack size
  402.         dc.l    $1000                           ; heap size
  403.  
  404. ; Form resource
  405.  
  406.         res 'tFRM', kidfMain, "tfrm03e8.bin"
  407.  
  408. ; Version resource
  409.  
  410.         res 'tver', kidrTVER
  411.         dc.b    '1.0', 0
  412.  
  413. ; Menu resource
  414.  
  415.         res 'MBAR', kidmMain, "mbar03e8.bin"
  416.  
  417. ; Alert resources
  418.  
  419.         res 'Talt', kidrAboutAlert, "talt03e8.bin"
  420.         res 'Talt', kidrInputErrorAlert, "talt03e9.bin"
  421.         res 'Talt', kidrRangeErrorAlert, "talt03ea.bin"
  422.  
  423.