Page 46 - Designing Autonomous Mobile Robots : Inside the Mindo f an Intellegent Machine
P. 46

The Basics of Real-time Software (For Mere Mortals)

               The various operations that the computer has been programmed to perform are
               called tasks or threads. The kernel is the first code to execute when the computer is
               booted, and it essentially decides which tasks should receive the resources of the
               CPU and at what time. The kernel controls most of the interrupts of the system.

               In most real-time systems, the kernel switches the CPU between tasks so rapidly
               that all of the tasks appear to be operating simultaneously. This is of course an illu-
               sion since the CPU can only execute one instruction at a time, but with CPU clock
               speeds in the gigahertz range, the CPU is virtually performing multiple tasks at the
               same time.
               When the kernel switches tasks, it must do more than simply save the return address
               of the program it is interrupting. It must save the context. The context is the tempo-
               rary data that the computer was manipulating to accomplish its task. This is most
               commonly the values of the CPU’s registers, and any data already on the stack.

                            ;------------------------------------------
                            ;      Serial Input Interrupt Procedure
                            ;
                        SER0IN:PUSH HL
                            PUSH DE
                            PUSH BC
                            PUSH IX
                            LD (SPMAIN0),SP ;Save main stack pointer.
                            LD SP,(SP0)
                            ;
                            IN0 A,(RDR0)  ;Get received character.
                            AND 07FH      ;Strip bit 8.
                            RET           ;Return back into LOADER.
                            ;
                            ;------------------------------------------
                            ;GETCHR is called from the serial loader
                            ;program as if it were a subroutine used
                            ;to wait for the next character. The
                            ;the stack is switched back to the main
                            ;program stack and the task is suspended
                            ;until the next character arrives.
                            ;
                        GETCHR0:PUSH IX
                            PUSH HL
                            PUSH DE
                            ;  Back to foreground.
                            CALL SR0EXIT
                            ;  Return from foreground.
                            POP DE
                            POP HL
                            POP IX
                            RET
                            ;------------------------------------------
                            ;      EXIT from Interrupt level 1.
                            ;      (Return to Foreground)


                                                        29
   41   42   43   44   45   46   47   48   49   50   51