Improved disk change detection algorithm

ZXNet echo conference «code.zx»

From Kirill Frolov To All 23 October 2000

================================================================================ * Forwarded by Kirill Frolov (500:812/23.25) * Area : ZX.SPECTRUM (Emulator people hanging out) * From : Alex Letaev, 2:5020/689.53 (21 Oct 00 06:32) * To : All * Subj: Improved disk change detection algorithm ================================================================================ Hi, All! (c) Ivan Roshchin, Moscow Fido: 2:5020/689.53 ZXNet: 500:95/462.53 E-mail: asder_ffc@softhome.net WWW: http://www.zx.ru/echo/roschin Improved disk change detection algorithm ────────────────────────── ────────────────────────── (Radio amateur. Your computer 9/2000) In this article I want to talk about what I use method for detecting a disk change. In my opinion, this method the most correct and reliable among all those known to me, and this is exactly what is used in my widely used program 'BestView 2.8'. First, let me briefly remind you how the program can determine that a disk has been removed from the drive. If you look on a 5.25" floppy disk, you will see on its side like this called a write protection slot, which may be open or closed. When a floppy disk is inserted into the drive, opposite this slot, the corresponding sensor (optical or mechanical), with which you can determine its condition.╔═══════════════════════════╗ ║ ║──── disk drive ║ ║ ║ ║ ║ ║ ║ ┌───────────────────────┐ ║ ║ │ ┌─┐ │────── floppy disk ║ │ │ │ │ ║ ║ │ │ │ │ ║ ║ │ └─┘ │ ║ ║ │ o / ^ │ ║ ║ │ ( ) │ ║ ║ │ _ / │ ║ ║ │ │ ║ ║ │ ┌────────────┐ │ ║ ║~~~┐ │ │ │ ║ sensor ───║~~~┘<┐ │ │ │ ║ ║ │ │ └────────────┘ │ ║ ╚═╧═══│═══════════════════╧═╝ └─ write protection slot Fig. 1. 5.25" disk drive with a floppy disk inside (view above). In 3.5" drives, write protection is implemented differently. Instead of a write protection slot, the three-inch floppy disk is equipped with hole with a sliding plastic valve. Unlike five-inch floppy disk, recording is allowed when the hole is closed and is prohibited when open. This is only a physical difference, but at the logical level, work with 5.25" and 3.5" drives occurs absolutely the same.The program can find out the status of the sensor by running the command reading from port #1F in TR-DOS. This will read the value of the so-called microcontroller status register VG93, and the state of the sensor will be reflected in the sixth bit of this register: ┌───────────────────┬ ────────────────────┐ │ situation │ value of the 6th bit │ ├───────────────────┼ ────────────────────┤ │slot open │ 0 │ ├───────────────────┼ ────────────────────┤ │slot closed │ 1 │ └───────────────────┴ ────────────────────┘ In addition to its main function (determining write protection), The write protection sensor can be used to obtain information that the disc has been removed from the drive. The point is that its state is reversed when removed floppy disks. Here's how it happens: ╔═══════════════════════════╗ ║ ║──── disk drive ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ┌───────────────────────┐ ║║ │ ┌─┐ │────── floppy disk ║ │ │ │ │ ║ ║ │ │ │ │ ║ ║ │ └─┘ │ ║ ║ │ o / ^ │ ║ ║~~~ ( ) │ ║ sensor ───║~~~ _ / │ ║ ║ │ │ ║ ╚═╧═══════════════════════╧═╝ └─┐ │ │ │ protection slot ->┌─┘ │ │ │ entries │ └────────────┘ │ └───────────────────────┘ Fig. 2. Floppy disk with an open slot. Once the user begins to remove it from the drive, the sensor immediately begins output 1 instead of 0 (from the point of view of the drive, the slot has become closed). ╔═══════════════════════════╗ ║ ║──── disk drive ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║║ ║ ║ ║ ║~~~ ║ sensor ───║~~~ ║ ║ ┌───────────────────────┐ ║ ╚═══════════════════════════╝ │ │ │ │────── floppy disk │ │ │ │ │ └─┘ │ │ o / ^ │ │ ( ) │ │ _ / │ │ │ │ ┌────────────┐ │ protection slot ->▓▓▓ │ │ │ entries ▓▓▓ │ │ │ │ └────────────┘ │ └───────────────────────┘ Fig. 3. Floppy disk with a closed slot. When the user finishes removing it from the drive, the sensor immediately begins output 0 instead of 1 (from the point of view of the drive, the slot has become open). As we can see, the program should periodically (for example, 50 times per second) poll the sensor status and when it changes infer that the user has removed the floppy disk. All of these are fairly well-known things. And now I'll tell you actually about the algorithm for determining a disk change in my program 'BestView 2.8'. BestView uses two different procedures todisc change definitions - slow and fast. As already happened As stated above, in such a procedure the key point is reading the VG93 status register, and to perform the reading you need so that the drive motor is running. Thus, the procedure should turn on the engine, read the status register and turn off the engine. The gap between turning the engine on and off. If it is small, then VG can turn out to be unready (then VG outputs instead of the register value status number #01 or a number with bit 7 set). If he is large, then the drive will have time to respond to switching on, and there will be a crackling sound is heard. A quick procedure starts the engine by issuing the command positioning to the zero track, and then, without waiting the end of the execution of this command (i.e., really no positioning does not have time to occur), reads the register status and turns off the engine. At the same time, there is no crackling can be heard (on my 'ROBOTRON'), but sometimes the VG turns out to be unready The probability of VG unavailability depends very much on the interval between turning the engine on and off - for example, for me, when I increased this interval by about 15 clock cycles, the probability from 35% decreased to zero. If turbo mode is enabled on the computer, there is the probability that with the fast polling method the VG _always_ will be unready (since with an increase in the clock frequency of the Z80the interval between switching on and switching off is reduced engine), and, accordingly, the shift will not be determined disk. By the way, this is why some programs cannot detect disk change in turbo mode. The slow procedure turns on the engine by issuing the command positioning on the same track that it is already on magnetic head of the drive, and then, _waiting_ for the end its execution, reads the status register and turns off the engine. In this case, the VG cannot be unready, but the disk drive can have time to react to switching on, and a crackling sound will be heard (I see it on 5.25" ROBOTRON and 3.5" MITSUMI). On some drives (5311) and with a slow polling method no crackling is heard. And now the most interesting thing - how BestView determines what procedure to use? Fast is used first procedure, but if 40 times _in a row_ when calling it, the VG turns out to be unready (for example, the user has turned on turbo mode), shell switches to alternating slow/fast polling method. And if at the same time at least once the VG turns out to be ready at speed polling method, the shell switches to the fast polling method. Thus, the advantages of both fast and slow are combined method. By the way, when working in turbo mode, even at slow using the survey method, most likely, no crackling noise will be heard, since this reduces the interval between switching on andturning off the engine. Sometimes it happens that even without turbo mode there is no quick way fires, and the shell switches to the slow mode. I have This happens (from time to time) on a 3.5" drive (and on a 5.25" drive everything is perfect). In previous versions of BestView, when there was no reverse transition from the slow to the fast polling method, in In this case, the drive began to crackle. Well, in BestView 2.8 This inconvenience also disappeared. In case you decide to write a program with a definition disk change, I’ll tell you about some problems that can occur when this arise, and about the way to overcome them. 1. Remember that disk change detection only works on on a real disk drive, not on a hard drive or RAM disk. From this the conclusion follows: the detection of disk change must be disabled, preferably separately for each of the devices A-D. Not bad provide for saving these settings on disk. Well, when disk change detection is disabled, the user must have the ability to reread the disk directory by pressing any keys. 2. On some drives (EC 5323.01, etc.) there was It has been noticed that when running programs that control disk changes, The light is constantly on and the engine is running. It is clear that This does not have the best effect on the service life of the drive. This is another argument in favor of the fact that the definition of disk change it should be turned off.3. On 3.5" drives, disk change detection does not occur. always (apparently this is a purely mechanical problem that can be solved by simply modifying the drive). Which means your program should not rely entirely on that she herself will be able to determine the change of disk. It may happen that the user will replace the disk, but the program “will not know” about it and will write the directory from the old disk to the new disk - I hope not Do you need to explain what this will lead to? Conclusions: firstly, before writing to disk (when copying etc.) you need to check whether this is the right disk using the control the sum of nine sectors of the directory; secondly, the user must be able to tell the program that the disk has been replaced. 4. As already mentioned, with the fast polling method, first a subroutine is called that performs positioning to zero track (this is necessary to turn on the drive motor), and then, without waiting for the end of positioning, another one is called subroutine that reads the status register and turns off engine. There is no positioning on the real Spectrum will happen, but on some emulators... By the way, this polling method is used in many boots, determining the disk change, but this is not critical there, because magnetic the head is constantly on the zero track. What can I recommend: try to build it into youremulator recognizer program, and if detected emulator - work with the disk only through #3D13. Best regards, Alex from FFC COMPUTERS. >E-MAIL: asder_ffc@softhome.net ICQ: #13064553 -+- Spot 1.3b Unregistered + Origin: AOL = Amiga On Line. (2:5020/689.53) ================================================================================ Press RESET immediately, All!