home *** CD-ROM | disk | FTP | other *** search
/ TCE Demo 2 / TCE_DEMO_CD2.iso / demo_cd_.2 / mags / stosser / stoser08.arj / stoser08.msa / ISSUE_8 / 9.PNE < prev   
Text File  |  1987-04-22  |  19KB  |  588 lines

  1.  
  2.                  WRITING STOS EXTENSIONS - PART 1 of 3
  3.                  =====================================
  4.  
  5.                  By Martin Cubitt for STOSSER disczine
  6.  
  7.  
  8.     This small series takes a look at how to write an extension for 
  9. STOS. A reasonable knowledge of assembly language is assumed.
  10.  
  11.     Firstly may I emphasise that this is not a definitive set of rules 
  12. and I am certainly no expert on the subject. If you want to get the full 
  13. details of writing STOS extensions then I suggest you write to Manadarin 
  14. or Francois Lionet.
  15.  
  16.     The only documentation I have on the subject are three similar 
  17. articles which appeared in the STOS Newsletter issues 8,9 and 10. I have 
  18. been unable to obtain the Game Makers Manual which apparently has 
  19. further information of the subject.
  20.  
  21.     Don't worry though. I do know enough to get you started! We will be 
  22. writing an interpreter extension and then a compiler one. Finally we 
  23. will write an installer program, just a simple one, so that we can add a 
  24. little professionalism to our extension.
  25.  
  26.     Let's start with a little background on STOS extensions. STOS can 
  27. only handle 26 extensions, using the letters A-Z. many of these are 
  28. already in use and some have been reserved by Mandarin. Here is a small 
  29. list of some extensions already defined by Mandarin:
  30.  
  31.     A     -    Compactor extension
  32.     B     -    A bug in version 2.3 of STOS prevents 
  33.                this being valid!
  34.     C     -    Compiler extension
  35.     D     -    Maestro extension
  36.     E     -    Squasher extension
  37.     G     -    reserved
  38.     H     -    reserved
  39.     M     -    Musician extension
  40.     Q     -    reserved
  41.     R     -    reserved
  42.     V     -    VIDI extension
  43.  
  44.     Many third part extension writers have used further extension id's:
  45.  
  46.     F     -    Double joystick extension
  47.     G     -    Blitter extension
  48.     H     -    Start extension
  49.     M     -    Misty extension
  50.     Q     -    Missing link extension part 1
  51.     R     -    Missing link extension part 2
  52.     S     -    Missing link extension part 3
  53.     T     -    TOME extension
  54.     Z     -    EXTRA extension
  55.  
  56.     As you can see many third part programmers have ignored (or not 
  57. known about) the standard laid down by Mandarin and clashed with 
  58. existing or reserved id's. If Mandarin decide to use these reserved 
  59. extension id's the third part extensions will not work and any BASIC 
  60. program will be useless. So, be warned. There are still plenty of 
  61. letters left to use, namely I,J,K,L,N,O,P,U,W,X and Y. Plus, some 
  62. extensions may become obsolete or used rarely, like the TOME extension. 
  63. I doubt a lot of people use it so if you become desperate for a letter 
  64. to use I suggest you use one which is being used for a rarely used 
  65. extension. Do not, however, be tempted to use the Mandarin-defined 
  66. extensions with the exception of F (the extension is pretty much unused 
  67. now).
  68.  
  69.     Before we start I must point out that I the source provided was 
  70. written in Devpac 3.10 so you will need to convert anything your 
  71. assembler does not like. Devpac is easily the most popular assembler so 
  72. there should not be too many problems.
  73.  
  74.     Right, back to writing the actual extensions...
  75.  
  76.  
  77.     Once you have decided your letter we can continue. The example 
  78. extension for this article uses letter W. The interpreter version will 
  79. be EG.EXW and the compiler will be EG.ECW.
  80.  
  81.     Please not that a command may accept parameters but never returns 
  82. any whereas a function does return a value but may also accept them too! 
  83. Take the following STOS commands/extensions:
  84.  
  85.     screen copy 5 to physic     -   command
  86.     A=rnd(5)                    -   function
  87.     print A                     -   command
  88.     B=A/2                       -   function
  89.  
  90.     Throughout the series I will refer to both commands and functions 
  91. just as commands unless this be incorrect!
  92.  
  93.     First thing to do is decide on what commands you want to have in 
  94. your extension. For this example I am going to create one new command 
  95. and two functions. You should first decide what the command should do 
  96. and then decide on the command name. Remember that the command may not 
  97. contain any embedded reserved word or command. Therefore you could not 
  98. use the command printer status as the reserved command print is present. 
  99. Personally I like to load up STOS and type my new command (yes, before I 
  100. have written it) and if it appears ALL in upper case it will be okay.
  101.  
  102.     New command:
  103.  
  104.     eg
  105.  
  106.     This will display, on screen, a list of the commands available in 
  107. the EG extension with a small description of each. This is extremely 
  108. useful as a quick reference guide and can be found in the EXTRA 
  109. extension by simply typing extra.
  110.  
  111.     New functions:
  112.  
  113.     =ndrv
  114.  
  115.     This returns the number of drives attached to the system. It ignores 
  116. the internal drive as that has to be there anyway. It will include any 
  117. RAM discs or hard drives attached. 
  118.  
  119.     =range(A,B,C)
  120.  
  121.     This returns a value reflecting A in the range B to C.
  122.           If A < B then =B
  123.           else If A > C then =C
  124.                else =A
  125.  
  126.     So we have decided on the new commands now. At this point I would 
  127. decide on an apt name for the extension. I have called this extension EG 
  128. as it is an example extension but a more descriptive name may have been 
  129. the Pot Pourri extension as it contains various commands.
  130.  
  131.  
  132.     As I am not the best person to explain things I have decided its 
  133. examples all the way. I have loaded the EGINT.S file into this article 
  134. and will now break it down further...
  135.  
  136.  
  137.     Programmers all program using different methods so feel free to use 
  138. your own programming techniques and commenting procedure.
  139.  
  140.     I think it is important to start your program with basic 
  141. information...
  142.  
  143.  
  144. * Source name .......: EGINT.S
  145. * Object name .......: EG.EXW
  146. * Author ............: Martin Cubitt
  147. * Date ..............: 13th Oct 1993
  148. * Description .......: Example STOS Interpreter extension
  149. *                      Contains 1 new command and 2 new functions:-
  150. *
  151. *                       (Remember that a function returns a value)
  152. *
  153. *
  154. *               eg              - this command displays the command
  155. *                                 list of the EG extension. I think                      
  156. *                                 all new extensions should have a
  157. *                                 similar command as an on-line
  158. *                                 reference to commands available.
  159. *
  160. *               A=range(A,B,C)  - a function which returns a value
  161. *                                 based on A in the range B to C. If
  162. *                                 A is less than B then A will become
  163. *                                 the value of B. If A is greater than
  164. *                                 C then A will become the value of C.
  165. *                                 If the value of A >=B and <=C then
  166. *                                 it will be unchanged.
  167. *
  168. *               A=ndrv          - this function returns the number
  169. *                                 drives attached to your system (and
  170. *                                 are available or turned on). If you
  171. *                                 have the internal, one external and
  172. *                                 a RAM disk the value will be 2
  173. *                                 because the internal drive is
  174. *                                 assumed anyway.
  175.  
  176.     Well, that's the intro over with let's get down to the meat of the 
  177. program...
  178.  
  179.     When STOS installs the extension it initialises any variables and 
  180. sets any flag required by it. STOS call the start of your program which 
  181. should be a BRA operation, pointing to your initialisation routine.
  182.  
  183. * Jump to initialisation routine
  184.         bra.s   INIT
  185.  
  186. * Create token list for commands/functions
  187. * Remember commands have even token numbers and functions odd ones
  188.  
  189.     The following byte is, presumably, a sort of marker used by STOS.
  190.  
  191. * The following byte value of 128 is simply required by STOS
  192.         dc.b    128
  193.  
  194.     All commands use even numbered token where functions use the odd 
  195. numbered tokens. The first token MUST be 128. This means the commands 
  196. can use token number 128 - 254 and functions 129 - 255. The token is 
  197. used internally by STOS as a reference to each command for each 
  198. extension. Tokens 160 and 184 may not be used but I do not know why. I 
  199. have used them with no problems!
  200.  
  201.     When entering your token/command list note the following:
  202.  
  203.     The command names MUST be in lower case.
  204.  
  205.     They can contain most characters including spaces.
  206.  
  207.     All character are significant so 'disc verify on' and 'disc verify 
  208. off' will be treated as two completely separate commands as far as STOS 
  209. is concerned.
  210.  
  211.     They cannot contain reserved words or commands.
  212.  
  213.  
  214. * Ensure your new command/function names are lower case and do not
  215. * contain any embedded reserved words such as 'printer value=...' as
  216. * this contains print and val!
  217.  
  218. TOKENS:
  219.         dc.b    "eg",128
  220.         dc.b    "range",129
  221.         dc.b    "ndrv",131
  222.  
  223.     Note that token 130 is not used but if I wanted another command it 
  224. would go there.
  225.  
  226.     The token list is terminated with a 0.
  227.  
  228.         dc.b    0
  229.  
  230.     The even instruction ensure that the next piece of code is assembles 
  231. on an even boundary, nothing to do with STOS but necessary as a lot of 
  232. byte declarations are used.
  233.  
  234.         even
  235.  
  236.     The jump table is a simple address look-up table which point to 
  237. where each command is in memory.
  238.  
  239.     Although assembled as offsets STOS re-calculates actual address when 
  240. your extension is installed. If any tokens were not used in the token 
  241. list you must still store a dummy in the jump table for the non-used 
  242. tokens. So I must include such a dummy for token 130.
  243.  
  244. * Create the jump table
  245. * This extension has THREE new commands (or functions) and ONE dummy!
  246.  
  247. JUMP:
  248.         dc.w    4
  249.  
  250.         dc.l    EG
  251.         dc.l    RANGE
  252.         dc.l    DUMMY
  253.         dc.l    NDRV
  254.  
  255.     Next up you enter the message to be displayed when your extension is 
  256. installed. Try to keep them short (unlike Misty & Missing Link) so that 
  257. the previously installed extensions do not scroll up the screen where 
  258. they are lost forever! Besides, they take up unnecessary memory.
  259.  
  260.     You actually need two messages. The first is for those who use 
  261. English STOS and the second for those who use French STOS.
  262.  
  263.     You may use control character to get inverse image or underline but 
  264. it would not be a good idea to clear the screen or something as all 
  265. other install messages would vanish then!
  266.  
  267. * Message to be shown when extension installed in STOS
  268. * First message for ENGLISH
  269. * Second for FRENCH
  270.  
  271. MESSAGE:
  272.         dc.b    10,10,"Example extension for STOSSER Disczine",0
  273.         dc.b    10,10,"Extension Example de STOSSER",0
  274.  
  275.     A 0 byte terminates the messages
  276.  
  277.         dc.b    0
  278.  
  279.         even
  280.  
  281.     Next we must define two variables which will be used later by the 
  282. extension.
  283.  
  284.     The SYSTEM variable will hold the address of the STOS system 
  285. routines.
  286.  
  287. SYSTEM:
  288.         dc.l    0
  289.  
  290.     This will be used to hold the return address from where your command 
  291. was called so that STOS can retake control when your command has 
  292. finished.
  293.  
  294. RETURN:
  295.         dc.l    0
  296.  
  297.     The initialisation routine is the first this executed by STOS when 
  298. your extension is installed. It does two things. Firstly, it moves the 
  299. address of the end of the extension into register a0. Secondly it moves 
  300. the address of the cold start routine into the register a1. A return 
  301. (rts) instruction finishes the routine.
  302.  
  303.     The label EXIT is used to hold the last address of the extension.
  304.  
  305. INIT:
  306.         lea     EXIT,a0
  307.         lea     COLDST,a1
  308.         rts
  309.  
  310.     The cold start routine actually performs some internal setting up by 
  311. STOS regarding the extension. The routine passes the addresses of the 
  312. messages, warm start routine, the token table and the jump table. The 
  313. warm start routine is used to reset variables used in the extension when 
  314. UNDO is pressed twice.
  315.  
  316. COLDST:
  317.         move.l  a0,SYSTEM       ; save address of STOS' system table
  318.         lea     MESSAGE,a0
  319.         lea     WARM,a1
  320.         lea     TOKENS,a2
  321.         lea     JUMP,a3
  322.  
  323. WARM:
  324.         rts
  325.  
  326.     Great, that is the interpreter header finished. Easy, eh? Next up we 
  327. need to write the actual commands themselves. 
  328.  
  329. * eg command
  330. EG:
  331.  
  332.     Save the return address so STOS can return to where it was when the 
  333. command was called.
  334.  
  335.         move.l  (a7)+,RETURN
  336.  
  337.     Check data register d0. This holds the number of parameters which 
  338. were passed.
  339.         
  340.         cmpi.w  #0,d0                           ; expect no parms
  341.         bne     NOPARMSERR
  342.  
  343.     Save stack, safe not sorry!
  344.  
  345.         movem.l a0-a6,-(a7)
  346.  
  347.         lea     EGINFO(pc),a0
  348.         move.w  #1,d7
  349.         trap    #3
  350.  
  351.     Restore stack.
  352.  
  353.         movem.l (a7)+,a0-a6
  354.  
  355.     Allow a smooth return back to STOS.
  356.  
  357.         move.l  RETURN,a0
  358.         jmp     (a0)
  359.  
  360. * Range function
  361. RANGE:
  362.         move.l  (a7)+,RETURN                    ;save return address
  363.         
  364.         cmpi.w  #3,d0                           ; expect 3 parms
  365.         bne.s   RANGEPARMERROR
  366.  
  367.     get each parameter form stack. d3 will hold the value of the 
  368. returned parameter.
  369.  
  370.         bsr.s   GETPARM
  371.         move.l  d3,d0                           ; C
  372.  
  373.         bsr.s   GETPARM
  374.         move.l  d3,d1                           ; B
  375.  
  376.         bsr.s   GETPARM
  377.         move.l  d3,d2                           ; A
  378.  
  379.         movem.l a0-a6,-(a7)                     ;save stack
  380.  
  381.         cmp.l   d1,d2
  382.         blt.s   TOO_LOW                         ; A<B so A=B
  383.  
  384.         cmp.l   d2,d0
  385.         bge.s   OKAY                            ; Leave A as is
  386.  
  387.         move.w  d0,d2                           ; C<A so A=C
  388.         bra.s   OKAY
  389.  
  390. TOO_LOW:
  391.         move.w  d1,d2
  392.  
  393. OKAY:
  394.         move.l  d2,d0
  395.  
  396.         bsr.s   SAVEPARM                        ;return parm/s
  397.  
  398.         movem.l (a7)+,a0-a6                     ;restore stack
  399.         move.l  RETURN,a0                       ;end routine
  400.         jmp     (a0)
  401.  
  402. * Number of drives function
  403. NDRV:
  404.         move.l  (a7)+,RETURN                    ;save return address
  405.         
  406.         cmpi.w  #0,d0                           ; expect no parms
  407.         bne.s   NOPARMSERR
  408.  
  409.         movem.l a0-a6,-(a7)                     ;save stack
  410.  
  411.         moveq.l #0,d0
  412.         move.w  ($4a6),d0
  413.  
  414.         bsr.s   SAVEPARM                        ;return parm/s
  415.  
  416.         movem.l (a7)+,a0-a6                     ;restore stack
  417.         move.l  RETURN,a0                       ;end routine
  418.         jmp     (a0)
  419.  
  420.  
  421. * Routines...
  422.  
  423.     Parameters are passed to your extension from STOS via the stack. 
  424. They need to be pulled off in order to store them elsewhere. The 
  425. following routine get an integer parameter only.
  426.  
  427.     Data registers d2,d3 and d4 hold information about each parameter 
  428. pulled from the stack. They are stored in the following format:
  429.  
  430.     d2=0 - Integer
  431.     d3=integer number
  432.     d4=0 - not used
  433.  
  434.     d2=$40 - Real (floating point)
  435.     d3=Top half of number
  436.     d4=Bottom half of number
  437.  
  438.     d2=$80 - String
  439.     d3=Address of string
  440.     d4=0 - not used
  441.  
  442.     Remember that STOS string are not standard and are not terminated 
  443. with a zero, as 68000 strings are. The first word of a STOS string holds 
  444. the length of the string and then the string follows this. You will need 
  445. to convert or code very carefully if you wish to use strings.
  446.  
  447. * Get parameter
  448. GETPARM:
  449.         move.l  (a7)+,a0
  450.         movem.l (a7)+,d2-d4
  451.  
  452.     Here we test that D2 is 0, for an integer
  453.  
  454.         tst.b   d2
  455.         bne.s   MISMATCH
  456.         jmp     (a0)
  457.  
  458.     Parameters returned by a function are simply stores as below using 
  459. the rules for those values of incoming parameters.
  460.  
  461. * Save parms for return
  462. SAVEPARM:
  463.  
  464.     Store actual value in d3
  465.  
  466.         move.l  d0,d3
  467.  
  468.     Make d2 0 for integer
  469.  
  470.         move.w  #0,d2
  471.  
  472.     Make d4 0 as it is not used
  473.  
  474.         move.w  d2,d4
  475.         rts
  476.  
  477. * Errors...
  478.  
  479.     You must do your own error handling in STOS extensions. You may use 
  480. the standard STOS error messages or use your own (or of course mix). The 
  481. mismatch error is issued if the incorrect parameter is sent.
  482.  
  483. * Mismatch error
  484. MISMATCH:
  485.  
  486.     Move the STOS error number (back of manual) into d0
  487.  
  488.         moveq.l #19,d0
  489.         bra.s   STOSERROR
  490.  
  491.     Where parms are sent to a command when they should not be there is 
  492. no message so I wrote one...
  493.  
  494. * No parms expected
  495. NOPARMSERR:
  496.  
  497.     Move the STOS error number reflecting the type of error (this should 
  498. be shown when UNDO pressed but hardly ever works! See Maestro errors!).
  499.  
  500.         moveq.l #12,d0
  501.  
  502.     Load the address of your error text
  503.  
  504.         lea     NOPARMSERRTEXT(pc),a2
  505.         bra.s   MYERROR
  506.  
  507. * Range error parms
  508. RANGEPARMERROR:
  509.         moveq.l #12,d0
  510.         lea     RANGEPARMERRTEXT(pc),a2
  511.         bra.s   MYERROR
  512.  
  513.     This procedure handles STOS errors
  514.  
  515. * STOS error messages handled here
  516. STOSERROR:
  517.         move.l  SYSTEM,a0
  518.         move.l  $14(a0),a0
  519.         jmp     (a0)
  520.  
  521.     This procedure handles my own errors
  522.  
  523. * My own error messages handled here!
  524. MYERROR:
  525.         clr.l   d1
  526.         clr.l   d2
  527.         clr.l   d3
  528.         clr.l   d4
  529.         clr.l   d5
  530.         clr.l   d6
  531.         move.l  SYSTEM,a0
  532.         move.l  $18(a0),a0
  533.         jmp     (a0)
  534.  
  535.  
  536.     Dummy label for tokens not used
  537.  
  538. DUMMY:
  539.  
  540.     Data for eg command
  541.  
  542. EGINFO:
  543.         dc.b    10,13,"* EG (Example) extension by M.Cubitt 1993 *"
  544.         dc.b    10,13,"eg................: Information"
  545.         dc.b    10,13,"=range(A,B,C).....: Make A in range B-C"
  546.         dc.b    10,13,"=ndrv.............: No. of drives"
  547.         dc.b    10,13,0
  548.  
  549.         even
  550.  
  551.     Error text (English and then French)...
  552.  
  553. RANGEPARMERRTEXT:
  554.         dc.b    "Expected three values for function",0
  555.         dc.b    "3 values expected",0
  556.  
  557.         even
  558.  
  559. NOPARMSERRTEXT:
  560.         dc.b    "No parameters expected",0
  561.         dc.b    "No parameters expected",0
  562.  
  563.         even
  564.  
  565.     End of program marker
  566.  
  567.         dc.l    0
  568. EXIT    equ     *
  569.  
  570.  
  571.  
  572.     And so there is a simple interpreter extension for STOS. Compiler 
  573. the source as EG.EXW. Place this in your STOS folder. When you next boot 
  574. STOS you should have a new set of commands.
  575.  
  576.     I have written a small program which uses all three commands so you 
  577. can see what they do. I think it is a nice touch to provide examples of 
  578. ALL new commands so that fellow STOSSERS understand how to use them.
  579.  
  580.     That is all for this part. In the next part I will show you how to 
  581. write a compiler extension. It seems that a lot of people out there are 
  582. unsure of how to do this.
  583.  
  584.     If you have any questions relating to this series please do not 
  585. hesitate to contact me. As I said before, I only aim to get you started.
  586.  
  587.     Until part 2, good bye!
  588.