COURS 5.TXT

From Atari Wiki
Jump to navigation Jump to search
   ******************************************************************
   *                                                                *
   *        68000 ASSEMBLER COURSE ON ATARI ST                      *
   *                                                                *
   *               by Le Féroce Lapin (from 44E)                    *
   *                                                                *
   *                        Lesson number 5                         *
   *                                                                *
   ******************************************************************

   Continuing the study of the Status Register, interrupts.

   Now that we have perfectly understood what happens when the 68000 finds the T bit of the Status Register set to 1, i.e., the whole fixed address system at which the address of the routine is found, we can continue and in fact finish the description of the other bits of this Status Register.

   The S bit / Supervisor

   The 68000 can evolve in 2 modes: Supervisor mode and User mode. In supervisor mode, we have access to ALL the instructions of the 68000 and to ALL the memory, whereas in user mode, certain instructions cannot be used, and access to certain parts of memory is forbidden.

   This may seem surprising at first: You bought a machine, after all, to be able to use it completely! Again, we fall into the trap of mixing up ATARI ST and 68000 MOTOROLA. Thanks to the huge power of this microprocessor, it is entirely possible to consider a multi-user work.

   Let's beef up our ST to 8 Mega bytes, equip it with a huge hard drive, and connect it to several terminals. We then have multiple keyboards, multiple screens, but on the other hand, only one microprocessor, that of the central unit (whose name takes on its full value here) and only one memory, from which everyone takes turns. Here, the Supervisor/User differentiation makes sense. The Supervisor is the 'owner' of the central unit, the people using the terminals being only 'users'. Restricting them to a part of the instructions and memory aims to prevent crashes, because if in the case of single usage, a total crash of the machine is always annoying, in the case of multi-usage, it escalates to a catastrophe, as the work of several people is disrupted!

   The S bit of the Status Register, if it is 0, indicates that we are in User mode. If it is 1, it indicates that we are in Supervisor mode.

   Just like MONST indicated the Trace state by showing T next to the SR, it indicates U or S depending on the mode we are in.

   Let's take a look back at the chapter describing the 68000 pinout (additional course A). We find this distinction at the FC0, FC1, FC2 pins.

   Before studying the remaining 3 bits of the SR (I2, I1, I0), we need to know that the Status Register is actually split into 2 bytes. The high byte (bits 8 to 15) is called the supervisor byte, while the low byte is the user byte.

   In user mode, we can only write to the user byte, whereas in supervisor mode, we have access to the word in its entirety.

   The user byte, containing the condition bits (X N Z V C), is also called the Condition Code Register, or CCR.

   The I2, I1 and I0 bits (Interrupt Mask)

   These 3 bits are used to represent the interrupt masks. But first, let's see what an interrupt is. We have previously studied the functioning related to the T bit (trace). When this bit is set, the main program is interrupted, in favor of a special routine. It is in a way the principle of interruption.

   An interrupt routine is a piece of program different from the main program. At regular intervals or because of an external element, the 68000 will interrupt (that's the word!) the main program to execute this routine. Once this is done, it returns to the main program.

   The simplest example is that of the telephone: I am working at my desk (the main program) when the phone rings. I detect the interruption, I stop my work and pick up the phone (execute the interruption). Once the conversation is over, I hang up and return to my main task.

   Now, something more complicated: Interruption of my main job. I pick up the phone, but in the middle of the conversation, someone rings the doorbell. This is where the principle of interrupt priority comes in. If the front door has a higher priority than the telephone, I interrupt the telephone conversation to open the door: this is an interruption of an interruption. Once I've closed the door on the 124th broom salesman of the day, I return to the phone, finish the conversation, hang up, and get back to my main task.

   However, if the 'front door' interruption has a lower priority than the telephone, I will wait until I'm done with it before opening the door.

   The 3 bits I2, I1 and I0 (Interrupt mask) allow defining the minimum level of interruption that will be taken into account. Since we have only 3 bits, we can define only 7 levels, from 1 to 7 (we do not talk here about level 0, as that is the 'normal' working level of the machine. If the level is 0, it means there is no interruption.). Thus, if we have 011 for these 3 bits, we obtain 3 as the minimum level. The interruptions of levels 1 and 2 will therefore not be taken into account. Since the level indicated by the 3 bits will be accepted as the interruption level, we deduce that if the bits are at 111, only the level 7 interruptions will be taken into account. It is also evident that it is not possible to define a minimum level of 8, for example, and thus to prevent a level 7 interruption. This level is therefore called 'non-maskable'.

   Level 7 interruptions are called NMI, i.e., non-maskable interrupts.

   Note that it is not possible to make a precise selection and, for example, to allow interruptions of levels 4, 5, and 7, but not those of level 6. If the bits are at 100, the interruptions of levels 4, 5, 6, and 7 will be permitted. You can take a look again at additional course A. You will find the 68000 pins I2, I1, and I0. One remark, however, these pins are active at the low state, meaning they indicate something when there is no current, unlike the other pins.

   On the other hand, their representation within the Status Register is in the right 'direction'.
   We are now led to ask a question similar to the one we asked when studying the Trace mode. The 68000 receives a request for an interrupt. It compares the level of this interrupt with the limit set by the I bits of the Status Register.

   If the interrupt is acceptable, it saves the Status Register and sets the interrupt level in the I bits to a level at which it will execute in order not to be hampered by a weaker request. It then stops the execution of its main program to turn to the routine. Once this is done, it returns to the main program. That's all well and good, but where did it find the interrupt routine? Quite simply by using the same principle as for the Trace mode. We saw that when the T bit was set, the 68000 went to the address $24 and found a long word, this long word being the address of the routine. For interruptions, the principle is the same: if it is a level 4 interruption, it is at address $70 that the 68000 will find a long word, this long word, as in the case of the Trace mode, being the address of the routine to be executed. If the interruption is at level 1, it is the long word located at address $64, and so on... It is obvious that it is up to the programmer to place these long words at these addresses: Prepare a routine, find its starting address, then note it at the precise location where you know the 68000 will look for it.

   Since all these addresses are located in the first kilobyte of memory of our machine, let's take a closer look at these 1024 bytes. (You will find a table representing this kilo in the appendix) For now, we are only going to identify the few elements we have already studied. All these addresses have order numbers, and because of their specific function (only communicating the address of a routine), they are called 'vectors'.

   We clearly find at $24 the vector 9, corresponding to the Trace mode, from $64 to $7C are the vectors corresponding to interruptions of levels 1 to 7. Level 0, being the 'normal' working level, does not have a vector.

   We can already explain other vectors: Thus the number 5 (address $14) is the vector for division by 0. The 68000 cannot perform division by 0. When the program tries, the same thing happens as for the Trace mode: Detecting a division by 0, the 68000 rushes to address $14, finds an address of a routine, and executes that. In most cases, this routine will display a few bombs on the screen and freeze everything. However, nothing prevents you from preparing your own routine and putting its address at $14. So in a math program (yuck!), this routine can display "division by 0 impossible". If the user tries such a division, there is no need to perform tests to warn them of this impossibility, the 68000 will take care of it on its own.


   The other vectors

   Bus error. We have previously seen that the 68000 uses what we call a bus to receive or transmit data. If an error occurs on it, it jumps to address $8 to find the address of the routine that will then be executed.

   Address error. The 68000 can only access even addresses. If it tries to access an odd address, an address error occurs (the same processing principle as the bus error, or the Trace mode, vector, address etc.). We will see later that it will be possible for us to access odd addresses, but with precautions.
   
   Illegal instructions. We have seen that the assembler's work simply consists of transforming our program into numbers, line by line. However, if we put an image in memory, it too will be placed in the 'memory tube' in the form of numbers. The difference is that these numbers do not mean anything to the 68000 as instructions. If we tell the 68000 to go to this address (that of the image) it will try to decipher these numbers as instructions, which will trigger an 'illegal instruction' error.

   Privilege violation. We have seen that the 68000 can evolve in user mode or supervisor mode. It is said that the supervisor state is the privileged state (or the state with the highest privilege). Trying to access in user mode a memory area reserved for supervisor mode or trying to execute a privileged instruction (usable only in supervisor mode) will cause a 'privilege violation' error.

   Understanding these different types of errors is very important. Indeed, the debugging phase is generally long in assembly language, especially at the beginning. Many errors can occur, the causes of which are sometimes right under our noses. The very type of the error, if well understood, can often be enough to more precisely guide the search and thus shorten the (tedious) time spent searching for the grain of sand that is blocking everything!

   All the vectors constituting the first kilobyte of memory, with the aim of diverting the main program to an exceptional routine, are called 'exception vectors'.

   The remaining vectors will be studied in subsequent series, as needed. Everything in its time!

   For today, we will stop here. It was short but the next chapter will be dedicated to the stack and will be quite long!

   The stack is a problem as simple as the others, which requires only attention. After studying what the stack is, we will have only half a lesson before tackling our first 'big' programs!

   Courage! but most importantly, take your time! Reread the previous lessons even if everything seems understandable. The further we progress, the more the number of small things will increase, and the less there will be room for the smallest misunderstanding.

Back to ASM_Tutorial