home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / ST_USER / 1990 / USERDC90.MSA / TEXT_CLINIC.TXT < prev    next >
Text File  |  1990-10-17  |  11KB  |  236 lines

  1. In Mathew Lodge's Clinic this month the rather strange-sounding Atari Cookie
  2. Jar is opened, dynamic C arrays are created and FATs are queried.
  3.  
  4.  
  5. This month's clinic kicks off with a question from Richard Wheeldon of
  6. Stoke-on-Trent:
  7.  
  8. "Can anybody explain how to read the File Allocation Table (FAT) entries of an
  9. ST disk? I do know that each entry is 12-bits (on a floppy at least), but how
  10. are the three 4-bit nibbles arranged? Once I've sorted out the order of the
  11. nibbles, what does the number mean?
  12.  
  13. Finally, does all this apply unchanged to hard disks? - I've got a 20MB drive
  14. running under TOS 1.4"
  15.  
  16.  
  17. Cookie Monster
  18. ==============
  19.  
  20. In the Clinic on User disk 55 David Hoggan and I wanted to know what the Atari
  21. "Cookie Jar" was. Kent Johansson from Sweden has written in with an excellent,
  22. comprehensive guide to all things cookie.
  23.  
  24. "The 'cookie' is a nice try from Atari to make programs more compatible with
  25. various machines. If you follow the 'rules' and start using the 'cookie' in
  26. your own programs, it is more likely they will run on the TT (and future ST
  27. computers) as well. The Cookie Jar features information about the system that
  28. might be useful while writing programs, and you can also insert your own
  29. 'cookies' using TSR-programs.
  30.  
  31. The address $5A0 contains zero on all ST computers except for the STE/TT where
  32. it contains the pointer to the Cookie Jar. Below is a system variable that you
  33. can add to your Concise/Internals manual, on the system variables page. It is
  34. an official system variable from Atari that you can trust!
  35.  
  36. _p_cookies     $5A0 L    Cookie Jar pointer
  37.  Always check $5A0 when using the Cookie Jar because its location varies on
  38. different ST models. A program that allocates memory (such as a RAM disk) can
  39. force the cookie to be moved to another location since it's installed in RAM
  40. and not in ROM.
  41.  
  42. The 'Cookie' is consists of two pairs of longwords. Always remember to read
  43. them two and two, like this (assuming address of the cookie jar was in a0):
  44.  
  45.      MOVE.L    (A0)+,D0
  46.      MOVE.L    (A0)+,D1
  47.  
  48. First you have an ID number that identifies the cookie, this number is unique
  49. for each cookie. When you are searching for a specific cookie you must stop
  50. when you find an empty longword instead of the ID longword. The existing
  51. "official" cookies are: _CPU,_VDO,_MCH,_SND and _SWI (all official Atari
  52. cookies start with the underscore character)
  53.  
  54. This is what you can find in Atari's basic Cookie Jar:
  55.  
  56. The _CPU cookie features information about the CPU fitted in you machine. The
  57. lowest byte contains any of these values: 00, 10, 20, 30 or even 40 (I want
  58. one!) This value is written in decimal form. For instance, if you should find
  59. the value 30 here it's most likely that you have a TT (or an '030 board) since
  60. that would mean that you have a 68030 in your machine.
  61.  
  62. The _VDO cookie shows what kind of shifter you have. Look at this table (But
  63. note that the value is found in the high word!):
  64.  
  65. Value     Shifter type
  66. ----------------------
  67.   0       A plain normal ST shifter
  68.   1       The improved ST shifter that is found in the STE.
  69.   2       TT Graphic chip.
  70.  
  71. The _SND cookie describes what sound hardware you have. It only describes what
  72. internal sound hardware you have. External hardware such as FM-Melody Maker or
  73. ST-Replay/AVR should not be described here. If they want to use a cookie they
  74. have to install one of their own. The value here is on bit level, and it's
  75. found in the lowest byte.
  76.  
  77. Bit       Sound hardware:
  78. ------------------------
  79.  0        Normal ST sound chip (Yamaha/GI)
  80.  1        STE/TT Hardware DMA stereo chip.
  81.  
  82. The _MCH cookie describes the system in general, information you can find at
  83. other locations but it's easier to read this one. In this one (just as in the
  84. _VDO cookie) the important information is found in the higher word, the lower
  85. word is reserved for smaller "changes" such as special things that might pop-up
  86. in the computers in the future.
  87.  
  88. Value    Hardware:
  89. ------------------
  90.   0      Plain ST (520,1040)
  91.   1      Mega ST
  92.   2      STE
  93.   3      TT
  94.  
  95. Finally, there is a last cookie called _SWI where the value of the
  96. configuration switches can be found. Only STE and TT computers have those
  97. switches, and I don't know what those switches do. I found the value $FF in the
  98. lower word on my STE.
  99.  
  100. On TT's you can also find a cookie called _FRB, this means Fast Ram Buffers and
  101. it is used with the ACSI DMA controller. The longword points to a 64kb buffer
  102. used by the ACSI DMA. If it is zero, no buffers are installed and if one
  103. doesn't find it, there is no FRB at all (not to be found in ST/STE).
  104.  
  105. When you check the 'cookies' for some information it would be nice to find the
  106. end somewhere. That's why this 'empty longword' exists. The longword after the
  107. empty one (remember, always read pairs of longwords) holds the number of
  108. 'cookies' in the system. If you find an 8 here (like I did on my STE) it means
  109. that 8 slots are reserved (allocated) for the jar. On a plain STE, five are
  110. used by cookies and one is used by the 'empty longword'. That leaves two, which
  111. come after the empty longword.
  112.  
  113. Let's say that I decided to make a external utility - a sound extension,
  114. featuring a 6 channel stereo chip. After having written the software I want to
  115. tell other programs that such an extension was connected to the ST. I use the
  116. Cookie Jar! Of course I would also hope that the game making firms would read
  117. off the Jar and check if my great thing was present and then use it. How would
  118. I do this?
  119.  
  120. First I must check if a Cookie Jar exists by reading of $5A0. If it doesn't
  121. exist ($5A0 is zero) then I need to install a Cookie Jar.
  122.  
  123. Let's assume that the Jar does exist and let's say I got the address $980 from
  124. $5A0. As you would know by now the jar have some "locked" information and if no
  125. one else has mucked around with it then I should have two slots free, but I'll
  126. describe how to use the jar whether there is any free space or not.
  127.  
  128. As we scanned through the jar we (hopefully) came across the 'empty longword'
  129. and as we did that we also found a value saying how many slots that have been
  130. reserved. (The number of slots multiplied with eight gives us the size of the
  131. jar in bytes. I.e. the normal jar is 64 bytes). And as I was only going to add
  132. one more slot I might write code like this:
  133.  
  134.         MOVE.W  #32,-(SP)     ; SET SUPERVISOR
  135.         TRAP    #1
  136.         ADDQ    #2,SP
  137.  
  138.         MOVE.L  $5A0.W,A0     ; GET START ADDRESS OF THE JAR
  139.         MOVEQ   #0,D7         ; CLEAR "OUR" COUNTER
  140. NO:     MOVE.L  (A0)+,D0      ; GET INFORMATION
  141.         MOVE.L  (A0)+,D1      ; IN LONGWORD PAIRS!
  142.         ADDQ    #1,D7         ; INCREASE OUR JAR COUNTER
  143.         TST     D0            ; CHECK IF IT'S THE EMPTY LONGWORD
  144.         BNE     NO            ; IF NOT, CONTINUE
  145.  
  146.         CMP.L   D7,D1         ; CHECK IF THE JAR IS FULL
  147.         BEQ     FULLJAR
  148.         MOVE.L  #"_STU",-8(A0) ; IF THERE IS SPACE, INSERT OUR
  149.         MOVE.L  #56,-4(A0)    ; NEW JAR, OVER THE OLD "EMPTY" ONE
  150.         MOVE.L  D1,4(A0)      ; AND  MAKE A NEW EMPTY  ONE  WITH
  151. ; DATA ABOUT THE SIZE OF THE JAR.
  152.  
  153. FULLJAR CLR.W   -(SP)         ; BYE!
  154.         TRAP    #1
  155.  
  156. If the jar is full, then you must make a new bigger jar:
  157.  
  158.      * Check how big the old one was.
  159.      * Allocate memory for the new one. Size = Old_size + 8_new_slots.
  160.      * Copy the old one to the new spot.
  161.      * Make my new cookie.
  162.      * Write a new "empty longword".
  163.      * Overwrite the old address in $5A0 with the new one.
  164.  
  165. I recommend that you reserve enough space for 8 cookies at a time. If you only
  166. reserve space for one cookie at a time the next program (that uses the cookie)
  167. would have to allocate more memory and your poor ST will end up with a nice
  168. fragmented memory."
  169.  
  170.  
  171. Dynamic Arrays
  172. ==============
  173.  
  174. The array is one of the basic building blocks of almost every computer
  175. language, but in most cases the size of the array is fixed - you can't change
  176. its size while the program is running. This month, John Merrill presents a
  177. technique for dynamically changing the size of an array in your 'C' programs.
  178. John's method takes advantage of the fact that there is no array bound checking
  179. in C at run-time:
  180.  
  181. "For number crunching, or when a large number of huge arrays may be needed
  182. fixed-sized arrays are not an ideal solution. It would be nice if there was a
  183. way in which arrays could be created and destroyed at will.
  184.  
  185. The functions below - vector() and free_vector() rely on the fact that that an
  186. array is simply a pointer. For example in the declaration 'int a[6]', a is
  187. simply a pointer to the memory containing the array. Suppose that the pointer
  188. 'int *p_aray' is declared in the same function, then the statement 'p_aray=a'
  189. is perfectly legal and the elements 'p_aray[0]' to 'p_aray[5]' all exist.
  190.  
  191. The trick is to declare just the pointer in the function requiring the array
  192. and when the array is needed, vector() is called. vector() takes one parameter,
  193. the size of the array, and returns a pointer to some allocated memory for the
  194. array. The original version used 'malloc(nl*sizeof(int))' but in my book this
  195. is identical to 'calloc(nl, sizeof(int)'. The array can then be used just like
  196. any other (although remember its name is now a pointer variable) and exists
  197. until it's name is passed to free_vector() which releases the allocated
  198. memory.
  199.  
  200. The example below shows how to use vector() and free_vector(). Note that they
  201. only work with integer type arrays. However it is a trivial matter to convert
  202. them to types float, double or short. Further information can be found in
  203. 'Numerical Recipes in C' by William H. Press, Brian P. Flannery, Saul A.
  204. Teukolsky and Wiliam T. Vetterling."
  205.  
  206.  
  207. Winding down
  208. ============
  209.  
  210. Keep the letters coming - especially the solutions and comments on other
  211. people's problems and ideas, but don't forget that the clinic depends on your
  212. problems too! Remember to include your full name (or title, if preferred), and
  213. give your phone number if possible.
  214.  
  215. If you have a listing longer than about 25 lines then please include it on a
  216. disk - I don't have time to type long listings in. Putting the text of your
  217. letter on disk is doubly helpful - First Word or First Word Plus format if
  218. possible, but straight ASCII is fine. Whilst on the subject of file formats,
  219. note that I can't decipher tokenised Fast Basic .BSC files - please send an
  220. ASCII version.
  221.  
  222. If you are sending a complete program, then I also like to see it running
  223. before putting it into the column, so please include a double-
  224. clickable version of your program if at all possible. If you want the disk
  225. and/or listing back, also include a stamped addressed envelope. No SAE, no
  226. disk.
  227.  
  228. Mathew Lodge
  229. "Programmer's Clinic"
  230. "Maen Melin"
  231. Holmes Chapel Road
  232. Lach Dennis
  233. Northwich
  234. Cheshire
  235. CW9 7SZ
  236.