home *** CD-ROM | disk | FTP | other *** search
/ Emulator Universe CD / emulatoruniversecd1998.iso / CPC / Utils / PC2Tape / REFRNCE.TXT < prev   
Encoding:
Text File  |  1996-11-26  |  7.4 KB  |  169 lines

  1. How the CPC stores data on its tapes:
  2. ------------------------------------
  3.  
  4.  1. Introduction
  5.  2. Higher Level
  6.  3. Lower  Level
  7.  
  8.  
  9.  
  10. 1. Introduction
  11.  
  12. This document was written by Pierre Guerrier, guerrier@ecoledoc.ibp.fr.
  13.  
  14. It is intended to show how data is actually stored on a tape. It deals
  15. with the analog structure of the tape signal, and not with the way you
  16. can use the firmware to store and retrieve data on tapes.
  17.  
  18. The information here has been gathered from the firmware manual, from
  19. a disassembly of the 6128 ROM and from observations on sampled sounds.
  20. It has been used successfully in my program AIFFdec to translate audio
  21. recordings into binary data. The source code of this program is also
  22. available.
  23.  
  24. You can use this information if you want to write another AIFFdec, or
  25. for whatever you want to do. But cannot be held liable for any damage
  26. to your hardware or data that could arise from the use or misuse of
  27. this document.
  28.  
  29. Another interesting source of information is the SOFT968 firmware
  30. manual from Amstrad plc. It has been converted to HTML by Kevin Thacker
  31. and should be available some day on the CPC guide. Kev's paper filled
  32. some gaps and corrected some "experimental" measurements error in mine.
  33.  
  34.  
  35.  
  36. 2. Higher Level
  37.  
  38. When a file is stored on a CPC tape, it is segmented in 2k *blocks*.
  39. The last of these blocks can be shorter than 2k.
  40. Each block is preceded by a 64 bytes *header*, containing useful info
  41. like the file name and the block ordering. See its structure in the
  42. CPC guide for instance (http://andercheran.aiind.upv.es/~amstrad).
  43. It is not very complicated to understand. Just note that the "block
  44. size" indicates the actual size of the forthcoming data block (2048
  45. bytes at most).
  46.  
  47. Before each stream of data (block or header), there is a *leader*.
  48. The leader is a stream of about 256 bytes of value &FF. This leader
  49. is used to guess the recording speed of the file, by measuring the
  50. duration of all the "1"s in it (see next section for details).
  51. After the data stream, whatever is kind, you find a *trailer*, which
  52. is 4 bytes of &FF.
  53.  
  54. Between the leader and the data stream, there is a *synchronization*
  55. pattern, used to distinguish block and header streams. The pattern
  56. consists of a single "0" bit followed by a byte, that is either &2C
  57. (header) or &16 (block). Note that &2C is &16 shifted by one bit.
  58. So if you really want to make the difference, what you need to count
  59. is the number of "0"s between the last "1" in the leader and the
  60. first "1" in the pattern.
  61.  
  62. Now if we look inside the data streams, we don't find only the user
  63. data. There is also a Cyclic Redundancy Check system. It works by
  64. cutting the stream into 256 bytes chunks (the last chunk is padded
  65. to 256 with &00 bytes). After each chunk is appended a 16 bits CRC.
  66. So actually a full 2k data block has 2060 bytes, 16 of which are
  67. CRCs, lying every 258 bytes in the stream.
  68.  
  69. Here is a synopsys of one data stream (whatever kind) of length L.
  70.   C = L/256  number of full chunks
  71.   R = L%256  remainder
  72.  
  73.  bits   bytes    value      notes
  74.  ----------------------------------
  75.  2048    256      &FF      Leader
  76.   1     0.125      0       Pattern delimiter
  77.   8       1    &2C or &16  Sync Pattern (header or block)
  78.  
  79. (repeated for C+1 chunks)
  80.  2048    256    variable   Chunk of User data (R or 256 valid bytes)
  81.   16      2     variable   CRC for this chunk (including padding)
  82.  
  83.   32      4       &FF      Trailer
  84.  
  85.  
  86. Algorithm for the CRC: although the implementation in the ROM is
  87. pretty messy and unrecognizable, the CRC is actually a very usual
  88. method using "primitive polynomials over the integers modulo 2".
  89. The base polynomial is X^16+X^12+X^5+1 (it is the one recommended
  90. by the CCITT and used in Xmodem, Kermit, X25... ).
  91. The CRC seed is &FFFF and it is stored negated on the tape. No
  92. other protocol is exactly identical, but all these features can be
  93. seen somewhere else. The only unusual thing (for the CPC) is that
  94. it is stored big-end-first. (Informations provided by Kevin Thacker
  95. from the CPC firmware manual)
  96. The CRC code in AIFF decoder comes from an algorithmics book. Rest
  97. assured, I don't understand it myself, but the book says its just
  98. a long division on polynomials.
  99.  
  100.  
  101.  
  102. 2. Lower Level
  103.  
  104. Here we talk about the way the CPC stores single bits, and how it
  105. makes the difference between a "0" and a "1". For a better under-
  106. standing of this section, you should see by yourself what the
  107. waveform looks like. The best is to use a sound card with audio
  108. input ability and sample the output of the tape plug of your CPC
  109. (or a CPC tape of course). Then run a sound editor and zoom in down
  110. to the samples.
  111.  
  112. The aspect of the bits in the sampled sound the depends on:
  113.   - the SPEED WRITE of the CPC (roughly 1200 or 2400 bps),
  114.   - the sampling frequency of the digitized sound,
  115.   - the electrical characteristics of the wires and tapes that where
  116.     used for the transfer.
  117. In the following, we will assume that the speed is 2400 bps (who wants
  118. to be slower ?) and that the sampling frequency is 11kHz (who wants
  119. bigger files ?).
  120.  
  121. The principle of the encoding could be pedantically called "frequency
  122. modulation" or "pulse width modulation". The idea is to send a strobe
  123. (low-high-low) signal, with one of two possible durations: short for
  124. "0" and long for "1".
  125. The exact parameters of the strobes can be set by a firmware call,
  126. but trying non-standard values often results in an unreadable file,
  127. so you can forget it (unless you want to do that kind of thing, like
  128. for a protection...)
  129. The leader before a data stream, provides the read-back routines in
  130. the CPC with many sample "1"s so that the routines know what 
  131. parameters where used for recording.
  132.  
  133. Typical durations in microseconds are summarized here:
  134.  
  135.    time   frequency     note
  136.   ---------------------------
  137.    363     2750 Hz      "0" in SW1
  138.    730     1375 Hz      "1" in SW1, "0" in SW0
  139.   1500     680  Hz      "1" in SW0
  140.  
  141. The exact baud rate depends on the average number of "1"s in the user
  142. data. The frequency spectrum of the sound also depends on the user
  143. data, but if you want to safely digitize a SW1 file, you must have at
  144. least 5.5k samples/second (Nyquist frequency) and preferably 11kHz to
  145. include the first harmonic of the shortest strobes.
  146.  
  147. Now what does it look like ? Roughly, a "0" is an arch with 4 samples
  148. and a "1" is an arch with 8 samples. Forget about the blabla above,
  149. this is the only key to the translation.
  150. We decide of the limit between two bits by looking for samples where
  151. the signal falls below 0V (in the CPCTERM recording). Of course, if
  152. your wires have reversed polarities, this won't work: try changing
  153. the sign of all your samples.
  154. Then it goes Low, High, Low (and as it goes low again, it falls under
  155. 0V and this is another bit). This is the strobe created by switching
  156. the PIO bit. Check its length, you get a bit. All done.
  157.  
  158. Well, not that easily... Actually, "0"s are in a [3..5] window, with
  159. some occasionnal 2-samples dwarves. And "1" are in a [6..9] window.
  160. Plus a number of weird glitches that happen some times. In AIFFdec,
  161. you will find a "filter" function that processes all this mess. Don't
  162. ask me how it works, I still wonder myself. And it's been pretty hard
  163. to tune.
  164.  
  165. I believe that the Sinclair range of computers had a very similar way
  166. of storing bits on their tapes. I also know of a Spectrum emulator for
  167. PowerPC which has built in tape emulation, but I don't know its
  168. internals.
  169.