home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / scnote / sample.001 / Sample.inc1.a < prev    next >
Text File  |  1989-06-01  |  13KB  |  375 lines

  1. *
  2. *    Apple Macintosh Developer Technical Support
  3. *
  4. *    MultiFinder-Aware Simple Sample Application
  5. *
  6. *    Sample
  7. *
  8. *    Sample.inc1.a    -    Assembler Source
  9. *
  10. *    Copyright ⌐ 1989 Apple Computer, Inc.
  11. *    All rights reserved.
  12. *
  13. *    Versions:    
  14. *        1.00            08/88
  15. *        1.01            11/88
  16. *        1.02            04/89
  17. *        1.03            06/89
  18. *
  19. *    Components:
  20. *        Sample.p        June 1, 1989
  21. *        Sample.c        June 1, 1989
  22. *        Sample.a        June 1, 1989
  23. *        Sample.inc1.a        June 1, 1989
  24. *        SampleMisc.a        June 1, 1989
  25. *        Sample.r        June 1, 1989
  26. *        Sample.h        June 1, 1989
  27. *        PSample.make        June 1, 1989
  28. *        CSample.make        June 1, 1989
  29. *        ASample.make        June 1, 1989
  30. *
  31. *    Sample is an example application that demonstrates how to
  32. *    initialize the commonly used toolbox managers, operate 
  33. *    successfully under MultiFinder, handle desk accessories, 
  34. *    and create, grow, and zoom windows.
  35. *
  36. *    It does not by any means demonstrate all the techniques 
  37. *    you need for a large application. In particular, Sample 
  38. *    does not cover exception handling, multiple windows/documents, 
  39. *    sophisticated memory management, printing, or undo. All of 
  40. *    these are vital parts of a normal full-sized application.
  41. *
  42. *    This application is an example of the form of a Macintosh 
  43. *    application; it is NOT a template. It is NOT intended to be 
  44. *    used as a foundation for the next world-class, best-selling, 
  45. *    600K application. A stick figure drawing of the human body may 
  46. *    be a good example of the form for a painting, but that does not 
  47. *    mean it should be used as the basis for the next Mona Lisa.
  48. *
  49. *    We recommend that you review this program or TESample before 
  50. *    beginning a new application.
  51.  
  52. * ----------- DEBUGGING INFORMATION -------------
  53. * This is used as a global switch to turn off the generation of debugging information.
  54. * The MACRO "DbgInfo" will generate this debugging information if set to 1.
  55.  
  56. DebuggerInfo    EQU    1
  57.  
  58.  
  59. * ================================================
  60. * --------   MACRO DEFINITIONS SECTION  ----------
  61. * ================================================
  62.  
  63. * ------------- GENERATE A PASCAL "CASE" OR "IF" SEQUENCE -------------
  64. * The following macro is used to generate a branch based on an index value
  65. * in a D-register with a value from 0 to N.  The branch is through a table
  66. * of relative addresses also generated by this macro. The macro is called
  67. * in one of two forms as follows:
  68.  
  69. *  {Form #1}    Case#        (Dreg,Default),case0,case1,...caseN
  70. *  {Form #2}    Case#.<size>    (Dreg,IF),(cst0,case0),...,(cstN,caseN)
  71.  
  72. * In Form #1, the "Default" specifies a label for any omitted case labels not
  73. * specified explicitly. The "case0", "case1",..."caseN" are case labels
  74. * identifying the various cases to be processed.  A case label may be omitted,
  75. * in which case the "Default" is used. The "Default" may also be omitted, but
  76. * in that case all case labels must be specified. If there are fewer case labels
  77. * than there are cases, but there are N possible values for the case index, the
  78. * proper number of trailing commas must be supplied to generate the defaults.
  79.  
  80. * In Form #2, the default is specified as the word "IF".  In this form the macro
  81. * generates a set of compares (CMPI's) and branches (BEQ) for each specified
  82. * case (there is no implicit default).    Each case is a constant/label pair.
  83. * The constant is compared (CMPI.W) and an branch is done (BEQ) to the case if
  84. * the Dreg equals the constant.  A size may be specified for all the branches
  85. * as a <size> attribute to the Case# call itself.  This must either be an "S"
  86. * or "W" to generate BEQ.S's or BEQ.W's.  The default is for "S".
  87.  
  88.         MACRO
  89.         Case#.&Size     &IdxDef
  90.         PRINT        Push,NoMDir     ; only list generated code
  91.         LCLA        &i        ; index to macro parameters
  92.         LCLA        &n        ; total number of macro parameters
  93.         LCLC        &Dreg,&Def    ; the Dreg and Default parameters
  94.         LCLC        &sz         ; the <size> value
  95.  
  96. &Dreg        SETC        &IdxDef[1]    ; pick off 1st opnd of sublist
  97. &Def        SETC        &IdxDef[2]    ; pick off 2nd opnd of sublist
  98. &n        SETA        &Nbr(&Syslist)    ; done for efficiency
  99. &i        SETA        2        ; cases start at 2nd parameter
  100.  
  101.  IF &UpCase(&Def) <> 'IF' THEN
  102. .* Create the jump table and the index value
  103. * -----------------------------------------------
  104.     ADD         &Dreg,&Dreg
  105.     MOVE        Case&SysNdx(&Dreg),&Dreg
  106.     JMP         Case&SysNdx(&Dreg)
  107.  
  108. Case&SysNdx
  109.     WHILE &i <= &n DO                ; process each case label
  110.        IF &SysList[&i] <> '' THEN
  111.         DC.W        &SysList[&i]-Case&SysNdx
  112.        ELSE
  113.         DC.W        &Def-Case&SysNdx
  114.        ENDIF
  115.        &i: SETA &i+1                 ; count off parameter
  116.     ENDWHILE
  117.  ELSE                            ; process (Cst,lbl) pairs
  118.  
  119. .* Create a series of CMPI and BEQ instructions
  120. * -----------------------------------------------
  121.     &Sz: SETC &Default(&Size, 'S')             ; setup size attribute
  122.     WHILE &i <= &n DO                ; process each (Cst,lbl) pair
  123.        CMPI        #&SysList[&i,1],&Dreg
  124.        BEQ.&Sz     &SysList[&i,2]
  125.        &i: SETA &i+1                ; count off parameter
  126.     ENDWHILE
  127.  ENDIF
  128.  
  129.         PRINT    Pop                 ; restore original print status
  130.         ENDM
  131.  
  132.  
  133. * ------------- GENERATE DEBUGGER SYMBOL INFORMATION -------------
  134. * This Macro will generate information for the debugger to read and display
  135. * as its module name.  This aids in debugging Asm code while looking at it
  136. * in the debugger.  This macro can only work if called at the end of stack
  137. * frame.  The appearance of the Macro statement in the source code must occur
  138. * immediately after the final "JMP   (A0)" or "RTS" instruction following the UNLINK.
  139. * Spaces may be included in the name, but no quotes are allowed.
  140.  
  141. *  {Form #1}    DbgInfo        ModName
  142. *  {Form #2}    DbgInfo.New    Really Long Module Name For MacsBug 6.0
  143.  
  144. * There are now two naming conventions used in MacsBug, Form #1 is the older MacsBug,
  145. * or TMON, and Form #2 is the newer MacsBug 6.0.  The older method would only
  146. * allow for a fixed length of eight characters.  If a shorter name is passed to
  147. * this Macro, it will extend the length to 8 chars with trailing spaces.
  148. * MacsBug 6.0 will now allow for a variable length C type string. This Macro will
  149. * create the proper DC statements and takes into account word alignment issues.
  150.  
  151.  
  152.         MACRO
  153.         DbgInfo.&Opt     &ModName#    ; the name to be used in the Debugger
  154.         PRINT        Push,NoMDir     ; Only list generated code
  155.         LCLC        &DbgName#    ; name to generate for MacsBug
  156.         LCLC        &DbgTemp    ; temporary name variable
  157.         LCLC        &New        ; variable used to test old vs. new
  158.         LCLC        &S        ; variable used to save PRINT state
  159.  
  160.  IF DebuggerInfo THEN                        ; do we want debugging info?
  161.     IF &ModName# ¡ '' THEN                    ; did we get a module name?
  162.     &New: SETC &UpCase(&Opt)                ; make option all upper case
  163.     IF (&New = 'NEW') THEN                    ; do we want new style?
  164.  
  165. .* Create the new MacsBug naming convention
  166. * -----------------------------------------------
  167.        &DbgTemp: SETC    &ModName#            ; generate new type symbols
  168.        IF &Len(&ModName#) < 32 THEN                ; if module name < 32 chars
  169.         IF &Len(&ModName#) // 2 = 0 THEN         ; add space if even so that...
  170.            &DbgTemp: SETC &Concat(&ModName#,' ')     ; string length plus length byte...
  171.         ENDIF                        ; will align to word boundary
  172.        &DbgName#: SETC &Concat(&Chr($80 + &Len(&ModName#)), &DbgTemp)
  173.        ELSE                            ; Length > 32 characters
  174.         IF &Len(&ModName#) // 2 = 1 THEN         ; add space if length is odd
  175.            &DbgTemp: SETC &Concat(&ModName#,' ')
  176.         ENDIF
  177.        &DbgName#: SETC &Concat(&Chr($80), &Chr(&Len(&ModName#)), &DbgTemp)
  178.        ENDIF
  179.     ELSE                            ; make it the older style
  180.  
  181. .* Create the older MacsBug naming convention
  182. * -----------------------------------------------
  183.        IF &Len(&ModName#) < 8 THEN                ; if module name < 8 chars
  184.         &DbgName#: SETC &Concat(&ModName#,'       ')    ; add at least 7 spaces
  185.         &DbgName#: SETC &Concat(&Chr($80 + &ORD(&SubStr(&DbgName#,1,1))), &SubStr(&DbgName#,2,7))
  186.        ELSE                            ; there are at least 8 chars
  187.         &DbgName#: SETC &Concat(&Chr($80 + &ORD(&SubStr(&ModName#,1,1))), &SubStr(&ModName#,2,7))
  188.        ENDIF
  189.     ENDIF
  190.  
  191. .* Create the DC.B with the debugger name, and include the NULs if new MacsBug option
  192. * -----------------------------------------------
  193.     &S: SETC &Setting('STRING')        ; preserve STRING status
  194.     IF &S ¡ 'ASIS' THEN            ; only change it if not already ASIS
  195.        STRING    ASIS
  196.        DC.B      '&DbgName#'
  197.        IF (&New = 'NEW') THEN
  198.         DC.W        0        ; fake literal size for new MacsBug
  199.        ENDIF
  200.        STRING    &S
  201.     ELSE
  202.        DC.B      '&DbgName#'
  203.        IF (&New = 'NEW') THEN
  204.         DC.W        0        ; fake literal size for new MacsBug
  205.        ENDIF
  206.     ENDIF
  207.    ENDIF
  208.  ENDIF
  209.  
  210.         PRINT    Pop             ; restore original print status
  211.         ENDM
  212.  
  213.  
  214. * ================================================
  215. * ---------------  EQUATE SECTION  ---------------
  216. * ================================================
  217.  
  218. * Some various EQUATES we'll use throughout the program.
  219. * -----------------------------------------------
  220. False        EQU    0        ; the value of False
  221. True        EQU    $0100        ; and you thought True = 1, HA!
  222. NIL        EQU    0        ; a NIL pointer to test against
  223.  
  224. ToolTrapBit    EQU    11        ; this bit is on for Tool traps
  225. WaitNextEvent    EQU    $A860        ; the WaitNextEvent trap number
  226. Unimplemented    EQU    $A89F        ; the Unimplemented trap number
  227. EnvironsVersion    EQU    1        ; this is the version of the SysEnvirons we want
  228. SleepValue    EQU    $7FFFFFFF    ; the sleeping time ($7FFFFFFF = MaxLongInt)
  229. SuspendResume    EQU    1        ; the suspend/resume event number of an OSEvent
  230. NoEvents    EQU    0        ; no events mask
  231. ExtremeNeg    EQU    -32768        ; for wide open rects and regions, see AdjustCursor
  232. ExtremePos    EQU    32767-1        ; -1 is because of a bug in regions, see AdjustCursor
  233.  
  234. * This is the minimum result from the following equation:
  235.  
  236. *    applLimit - applZone = minimum heap size
  237.  
  238. * for the application to run. It will insure that enough memory will
  239. * be around for reasonable-sized scraps, FKEYs, etc. to exist with the
  240. * application, and still give the application some 'breathing room'.
  241. * To derive this number, we ran under a MultiFinder partition that was
  242. * our requested minimum size, as given in the 'SIZE' resource.
  243.  
  244. MinHeap        EQU    21*1024        ; minimum heap size in bytes
  245.  
  246. * This is the minimum exceptable result from PurgeSpace, when called
  247. * at initialization time, for the application to run. This number acts
  248. * as a double-check to insure that there really is enough memory for the
  249. * application to run, including what has been taken up already by
  250. * pre-loaded resources, the scrap, code, and other sundry memory blocks.
  251.  
  252. MinSpace    EQU    8*1024        ; minimum stack space in bytes
  253.  
  254.  
  255. * The following equates use for resources.  That's why they have a "r" in front.
  256. * -----------------------------------------------
  257. rMenuBar    EQU    128        ; application's menu bar
  258. rUserAlert    EQU    129        ; error alert for user
  259. rWindow        EQU    128        ; application's window
  260. rAboutAlert    EQU    128        ; about alert
  261. rStopRect    EQU    128        ; rectangle for Stop light
  262. rGoRect        EQU    129        ; rectangle for Go light
  263.  
  264.  
  265. * The following equates are for menu definitions, obviously.
  266. * -----------------------------------------------
  267. AppleMenu    EQU    128        ;  Apple menu
  268. AboutItem    EQU    1
  269.  
  270. FileMenu    EQU    129        ;  File menu
  271. NewItem        EQU    1
  272. OpenItem    EQU    2
  273. CloseItem    EQU    4
  274. SaveItem    EQU    5
  275. SaveAsItem    EQU    6
  276. RevertItem    EQU    7
  277. PageSetupItem    EQU    9
  278. PrintItem    EQU    10
  279. QuitItem    EQU    12
  280.  
  281. EditMenu    EQU    130        ;  Edit menu
  282. UndoItem    EQU    1
  283. CutItem        EQU    3
  284. CopyItem    EQU    4
  285. PasteItem    EQU    5
  286. ClearItem    EQU    6
  287.  
  288. LightMenu    EQU    131        ;  Light menu
  289. StopItem    EQU    1
  290. GoItem        EQU    2
  291.  
  292. * -----------------------------------------------
  293. DITopLeft    EQU    $00500070    ; position of Disk Init dialogs
  294.  
  295. * ================================================
  296. * ----------------  RECORD TYPES  ----------------
  297. * ================================================
  298. * This section is declaring record structures.  These records are
  299. * templates.  No data is allocated at this point.  These are just
  300. * structures, similar to Pascal TYPEs.  They simply generate a list
  301. * of equate offsets.  Since none of these types are defined already
  302. * in the MPW AIncludes, we'll need to define them.
  303.  
  304. * ------------- MOUSE POINT TYPE -------------
  305.  
  306. Point        RECORD    0
  307. v        DS.W    1
  308. h        DS.W    1
  309.         ORG     v
  310. vh        DS.W    h
  311.         ENDR
  312.  
  313. * ------------- RECTANGLE TYPE -------------
  314.  
  315. Rect        RECORD    0
  316. Top         DS.W    1
  317. Left        DS.W    1
  318. Bottom        DS.W    1
  319. Right        DS.W    1
  320.         ORG     Top
  321. TopLeft        DS.L    1
  322. BotRight    DS.L    1
  323.         ENDR
  324.  
  325. * ------------- BITMAP TYPE -------------
  326.  
  327. BitMap        RECORD    0
  328. baseAddr    DS.L    1
  329. rowBytes    DS.W    1
  330. bounds        DS.L    Rect
  331.         ENDR
  332.  
  333. * ------------- EVENT RECORD TYPE -------------
  334.  
  335. EventRecord     RECORD    0
  336. What        DS.W    1
  337. Message     DS.L    1
  338. When        DS.L    1
  339. Where        DS.L    Point
  340. Modify        DS.W    1
  341.         ENDR
  342.  
  343. * ------------- THE QUICKDRAW WORLD -------------
  344.  
  345. QDGlobals    RECORD    0,DECREMENT
  346. GrafPort     DS.L    1
  347. White        DS.B    8
  348. Black        DS.B    8
  349. Gray        DS.B    8
  350. LtGray        DS.B    8
  351. DkGray        DS.B    8
  352. Arrow        DS.B    cursRec
  353. ScreenBits    DS.B    BitMap
  354. RandSeed    DS.L    1
  355.         ORG     -GrafSize
  356.         ENDR
  357.  
  358. * ------------- ALL OF OUR GLOBAL DATA -------------
  359. * Note the minimal amount of globals we're using.  Data such as
  360. * the EventRecord, WindowRecords, etc. do not belong in global data
  361. * allocation.  Only data that basically doesn't change through out the
  362. * execution of the program is considered global.  The boolean flags are
  363. * global, since they affect the state of the program at any given time.
  364. * Also note that any appearance of a DS outside of a stack frame will
  365. * be allocated off of A5 and becomes part of global data storage.
  366.  
  367. AppGlobals    RECORD    0        ; this is our global data storage
  368. Stopped        DS.W    1        ; boolean for the state of the light
  369. HasWNEvent    DS.W    1        ; boolean for WaitNextEvent trap, see ForceEnvirons
  370. InBackground    DS.W    1        ; boolean for if in background, see OSEvent
  371. StopRect    DS    Rect        ; rect for the Stop light, set from a resource
  372. GoRect        DS    Rect        ; rect for the Go light, set from a resource
  373. Mac        DS    SysEnvRec    ; the system environment record, see ForceEnvirons
  374.           ENDR
  375.