Optron #35
25 апреля 2000 |
|
Likbez - Interrupts ZX Spectrum.
Assembler - a view from afar Continued. Beginning at | | 20, 21, 24, 25, 28-30, 32, 33 {} Infarh, 2000 Hello enthusiasts! That, probably bored on vacation? Do not worry, we will continue. At this time we will talk about paper with preemption. To begin to understand that this is. So ... Interrupt You might not suspect that 50 times a second processor is diverted from the processing of your programs and deals not know what ... For example, interrogates the keyboard, changing the system variable FRAMES etc. And it does so, what do you and do not guess. In these moments of the following happens: the processor refers to podprograme at 56, as if executes the command RST 56 or CALL 56. The difference is that the transition is carried out not by software and hardware. After completing the execution of called procedure, processor continues to perform basic program. At first glance it might seem that from such "chips" prosecutors no, because software in the ROM will not change anything. K Fortunately, the programmer has the ability to change the address at which the procedure is an interrupt. For this purpose the previously mentioned case I. First of all, we must remember that there are three modes of treatment interruptions. They are denoted by numbers 0 to 2. Standard mode - 1. It was his exploit the operating sistama. Zero mode we do not particularly interesting: the practice, it is no different from the first. Note that it is in practice! Differences, of course, is, yes only in the Spectrum they are not implemented. Thus, the most interesting for us is the mode 2. To begin with a few words about how it works and that at this point in the computer going. When it comes to an interrupt signal, processor to start with, determines the address pointer to the procedure for processing interrupts. Aforesaid consists of two bytes: the younger, read from the data bus (also called the "interrupt vector"), and older, the readout from the register I. The value obtained placed on the address bus, and her previous contents are stored on the stack. All of this, essentially a call option subroutine. "But what does this term - the" vector Interrupt? " - You ask. The fact is that this term is associated with the operation of computers, which allow for receipt of several interrupt request SIMULTANEOUSLY. Naturally, in this situation it is necessary to determine which of interrupt has priority. And it means that an interrupt request should be considered as "vector", which, like any decent vector harakterizetsya at least two values (in this case - Source and priority). When you receive the interrupt request is processed or ignored, depending on the precedence over the currently executing interrupt. Well, for the Spectrum, even if it is used only one interrupt, the term This, nevertheless, retained. That's all long. As a rule, the value of interrupt vectors y Speccy is 255, so to determine the address pointer is enough content I register multiplied by 256 and add 255. So, try to imagine the actions that must be produced to use their own procedures for handling Interrupt: 1) Disable interrupts. This is necessary avoid starting a new procedure for interruption during the current that is fraught with ... 2) Record at the address calculated in advance, a pointer to the procedure for processing interrupts, and the address. 3) Set in the I register high byte of the address pointer to the processing procedure. 4) Install a second mode of processing interrupts. 5) re-enable interrupts. Naturally, the process interrupt at this point should already be in memory. Here is a brief and all. Now - a few words about means of implementation. More precisely, about the commands. So: DI interrupt prohibition EI Interrupt Enable IM 0 IM 1 IM 2 Installation of one of three modes Interrupt Now you have both teams. However, before setting its procedures for processing think about whether it should be switched off and return control to the standard procedure. To disable as necessary: 1) Disable interrupts. 2) Restore the value of I, recorded in him 63. 3) Assign a team of IM 1, the first mode interrupts. 4) Enable interrupts. But that's not all. It must be remembered that some external devices may change the value of interrupt vectors. Except Furthermore, if your Speccy collected on their knees "Crooked pens" on splodgy beer circuit interrupt vector can skip most unpredictable ways. Naturally, in this case, the probability of a procedure call treatment equal to 1 / 256. To avoid glitches and headaches programmers have resorted to the following trick: instead of writing two bytes at a specified address table is built of at least 257 bytes in such a way that in any case reads one address. Naturally, for that all elements of the table must be identical. The most successful value to populate this table is 255. Do not be alarmed that the address of the procedure will be equal # FFFF. This one byte is just enough to accommodate the code 24 (the team JR). For it is followed by address # 0000, on which a ROM is the value # F3. Thus obtained by JR 65524. At this address is You can add Komad JP with the arbitrary address of the transition. Consider the example of setting its procedures for handling interrupts: LD A, 24; command code JR LD (65535), A LD A, 195; instruction code JP LD (65524), A LD HL, ISR; address of the handler LD (65525), HL LD HL, # FE00 LD DE, # FE01 LD BC, # 0100 LD (HL), # FF LD A, H LDIR DI LD I, A IM 2 EI So we hooked up the procedure for processing interrupts. We now consider the inverse process, namely a return to the regime of IM 1. DI LD A, 63 LD I, A IM 1 EI And finally - an example. Suppose you want to play music while working your program. If you use a melody, compiled in one of the music editors and equipped with a standard player, you have problems will not arise. To play every need 1 / 50 sekudny cause the player, and hence frequency of interruptions you perfect. Before you start playing, you must initialize the player, making CALL for sotvetstvuyuschemu address. Address is initialized, as well as playing, depends on the editor and Conditions compilation. However, it often happens that initialization is situated in the very beginning of a melody and playing music for a further six bytes further. If we assume that the melody compiled under the address # C000, then the initialization will be located at # C000, and play the notes - at # C006. Now we will correct our examples. In procedure for installing a second interrupt mode, add CALL # C000 for initialization. This should be done and the procedure restore the previous interrupt mode. For what? So after you have turned off the music, AY did not continue to pull the last note. Initialization enables you quickly "to shut the throat 'dynamics. And now posmotrm on procedure itself: ISR DI PUSH AF; save all registers PUSH BC; for a normal return PUSH DE; the main program PUSH HL CALL # C006; call the Player POP HL; restore POP DE; stored values POP BC POP AF EI; permission to interrupt RET; & Returns If you did everything correctly and determine the address of the initialization and playback, music should resound. We mention one more team to facilitate Life - HALT. When the processor to nabredaet her, he pauses and waits interrupt signal. After his admission control is passed to the procedure for processing interrupts. And after it is completed basic program continues its work with the team following the HALT. This allows the use of HALT to synchronize your program interrupts. Well, everything seems so far with the theory. Left only, as usual, to present something about flags and options for commands. Take a look: Mnemonic Flags operation C Z P / V S N H DI. . . . . . EI. . . . . . IM 0. . . . . . IM 1. . . . . . IM 2. . . . . . LD A, I. x x x 0 0 LD I, A. . . . . . HALT. . . . . . That's all the flags. Now you can safely ready to voice their programs. Do not forget just what some players have the habit to allow interrupts. Also immediately after the transition to the procedure for processing interrupts HALT interrupt are forbidden. To be continued ...
Other articles:
Similar articles:
В этот день... 21 November