home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / ddjmag / ddj8601.zip / TURNER.JAN < prev   
Text File  |  1986-01-31  |  9KB  |  245 lines

  1.  
  2. Listing 1
  3. ;
  4. ;    Terra Nova Communications multi-tasking kernel
  5. ;    Initialization and task-switcher
  6. ;
  7. ;    Note: this is not intended to be a complete listing.  It's only
  8. ;    a sample of some of the techniques used in our system.
  9. ;
  10.  
  11.     PSECT    Kernel
  12.  
  13. ;    External symbols (defined in other code segments)
  14.     EXTERN    VecTable,    ;vector table for hardware vector list
  15.         JMPTable,    ;jump table for system calls
  16.         JMPTabLen,    ;length of jump table in longwords
  17.         KernEnd,    ;end of kernel code item in heap
  18.         IOInit,    ;our private I/O initialization routine
  19.         HeapInit,    ;our private heap initialization
  20.         SysInit,    ;system variable initializer
  21.         SysConMon,    ;entry point for system console
  22.             ;monitor task
  23.         HeapMunger,    ;entry point for heap munger task
  24.         DiskMunger    ;entry point for disk munger task
  25.  
  26. ;    Entry points in this module (referenced from elsewhere)
  27.     ENTRY    Start,    ;primary entry point to boot our OS
  28.         ConSwitch,    ;main context switcher
  29.         ConSwSleep    ;alternate context switcher (puts
  30.             ;calling task to sleep)
  31.  
  32. ;    Include files (mostly equates)
  33.     INCLUDE    SysEqu    ;contains the low-memory absolute
  34.             ;address equates (jump table, etc)
  35.     INCLUDE    HeapDef    ;defines the heap data structure
  36.     INCLUDE    SysIO    ;contains hardware I/O equates
  37.  
  38. ;    Miscellaneous storage
  39. CodeHeap    DS.L    8    ;heap header for kernel heap item
  40. StackEnd    DS.L    40    ;system stack before tasking starts
  41. StackBegin    DS.L    0    ;top of startup stack area
  42.  
  43. ;    Pre-tasking initialization
  44. ;    this code works in single-task mode
  45. ;    prior to the invocation of the context switcher
  46. Start        ;Initial entry.  Calling operating system is still         ;alive and kicking at this point.
  47. TakeOver    LEA    ReEntry,A1    ;point to re-entry instruction
  48.     MOVE.L    A1,$20.W    ;move short absolute to the vector
  49.             ;for privilege exceptions
  50.     MOVE    USP,A0    ;try a privileged instruction.  If it
  51.         ;works, then we're in priv. mode. If not, then trap to         ;ReEntry and be in privileged mode anyway
  52. ReEntry    LEA    StackBegin,A7    ;set up initial stack
  53.  
  54. ;    Turn off all interrupts in the system
  55. ;    Note: this is device-specific code.
  56. ;    The labels in the operand fields are from our own
  57. ;    SysIO include file.
  58.     CLR.B    FDCIntMask    ;clear floppy disk & system console
  59.     CLR.B    HDIntMask    ;clear hard disk completion int. mask
  60.     CLR.B    SerIO1IntMask    ;clear serial boards
  61.     CLR.B    SerIO2IntMask
  62.  
  63. ;    Initialize the vector table
  64. ;    Copy the vectors from an assembled table (in another module) ;    into the actual hardware vector list in low RAM
  65.     LEA    VecTable,A0    ;source (in another code segment)
  66.     LEA    $0.W,A1    ;destination (begins at $00 0000)
  67.     MOVE    #191,D7    ;192 longwords to move
  68. VecMove    MOVE.L    (A0)+,(A1)+    ;move a longword
  69.     DBRA    D7,VecMove    ;repeat till done (fast loop on 68010)
  70.  
  71. ;    Copy system routine JMP table from assembled object code (in
  72. ;    another module) to low memory jump table, where everyone
  73. ;    can get at them.
  74.     LEA    JMPTable,A0    ;source
  75.     LEA    System.W,A1    ;dest. (name of first system call in 
  76.         ;the jump table.  "System" is from the SysEqu include         ;file.  It's the context switcher)
  77.     MOVE    #JMPTabLen/4,D7 ;number of longwords to move
  78. JPTMove    MOVE.L    (A0)+,(A1)+    ;move a longword
  79.     DBRA    D7,JPTMove    ;repeat till done (fast loop on 68010)
  80.  
  81. ;    Clear low memory to zero (between jmp table and kernel)
  82.     LEA    StackEnd,A1    ;point to top of destination
  83.         ;and bottom of destination (end of the jump table)
  84.     LEA    System+JMPTabLen.W,A0
  85.     SUBA    A0,A1    ;calculate the length
  86.     MOVE.L    A1,D7    ;move to D7 for counting
  87.     LSR.L    #4,D7     ;divide by 16 for 16-byte blocks
  88. LowClr    CLR.L    (A0)+    ;clear 16 bytes, quickly
  89.     CLR.L    (A0)+
  90.     CLR.L    (A0)+
  91.     CLR.L    (A0)+
  92.     DBRA    D7,LowClr    ;do it until done.
  93.  
  94. ;    Clear high memory to zero (between kernel and end of RAM)
  95. ;    (RAMEnd is first byte beyond RAM, defined in SysEqu)
  96.     LEA    RAMEnd,A1    ;point to top of destination
  97.         ;and bottom of destination (end of the jump table)
  98.     LEA    KernEnd,A0
  99.     SUBA    A0,A1    ;calc the length
  100.     MOVE.L    A1,D7    ;move to D7 for counting
  101.     LSR.L    #4,D7     ;divide by 16 for 16-byte blocks
  102. HiClr    CLR.L    (A0)+    ;clear 16 bytes, quickly
  103.     CLR.L    (A0)+
  104.     CLR.L    (A0)+
  105.     CLR.L    (A0)+
  106.     DBRA    D7,HiClr    ;do it until done.
  107.  
  108. ;    Initialize all of the primary I/O devices
  109. ;    Note: this is a device specific routine not treated in the article.
  110.     JSR    IOInit
  111.  
  112. ;    Initialize the heap
  113. ;    Note: this is a routine in the heap manager, which creates
  114. ;    valid heap headers for the three initial heap items discussed
  115. ;    in the text: the deletion below the kernel, the kernel code
  116. ;    item, and the deletion above the kernel.
  117.     JSR    HeapInit
  118.  
  119. ;    Initialize the system zone of low memory
  120. ;    Note: this sets up the TCB and master handle arrays, as
  121. ;    discussed in the text, as well as initializing the time of day and
  122. ;    the date and the other miscellaneous system values.
  123.     JSR    SysInit
  124.  
  125. ;    Spawn off the initial tasks
  126. ;    This will create TCBs and TData items for the tasks, but won't
  127. ;    invoke them.  They're invoked only by the context switcher.
  128.     LEA    SysConMon,A0    ;point to system console entry point
  129.     MOVE.L    #4096,D0    ;tell it how much RAM for TData
  130.     JSR    Spawn    ;jump through jump table entry
  131.         ;("Spawn" is a jump table equate in SysEqu)
  132.     LEA    HeapMunger,A0 ;spawn the heap munger
  133.     MOVE.L    #512,D0    ;heap munger's TData size
  134.     JSR    Spawn
  135.     LEA    DiskMunger,A0    ;Spawn the disk munger
  136.     MOVE.L    #8192,D0    ;(TData includes one disk buffer)
  137.     JSR    Spawn
  138.  
  139.     LEA    TCB1.W,A2    ;get address of first TCB in array
  140.             ;(TCB1 is defined in SysEqu)
  141.     BRA.S    ConSw1    ;now start the context switcher!
  142.  
  143. ;    Context Switcher: primary version
  144. ;    Simple task-switch, nothing fancy.
  145. ;    SysFlags is a low-RAM system flag byte, defined in SysEqu.
  146. ;    The data structure for the TData item is defined in SysEqu.
  147. ;    The data structure for the TCB is defined in SysEqu.
  148. ConSwitch    BTST    #StopSys,SysFlags.W ;task switching inhibited?
  149.     BNE.S    ConSwX    ;yes, exit back to caller
  150.     MOVE.L    OurTCB(A5),A0    ;get TCB address from TData
  151.     SUBA.L    A5,SP    ;subtract TData base addr from stack
  152.     MOVE.L    SP,TCBSP(A0)    ;save relative displacement in TCB
  153.  
  154.     MOVE.L    TCBNxt(A0),A2    ;get address of next TCB
  155. ConSw1    MOVE.L    TCBA5(A2),A5    ;get new TData base address
  156.     MOVE.L    TCBSP(A2),SP    ;get stack relative displacement
  157.     ADDA.L    A5,SP    ;restore absolute address
  158. ConSwX    RTS        ;return to next task
  159.  
  160. ;    Context Switcher: alternate version
  161. ;    Put the calling task to sleep.
  162. ConSwSleep    BTST    #StopSys,SysFlags.W ;task switching inhibited?
  163.     BNE.S    ConSwX    ;yes, exit back to caller
  164.             ;without going to sleep
  165.     MOVE.L    OurTCB(A5),A0    ;get TCB address from TData
  166.     SUBA.L    A5,SP    ;subtract TData base addr from stack
  167.     MOVE.L    SP,TCBSP(A0)    ;save relative displacement in TCB
  168.  
  169.     MOVE.L    TCBNxt(A0),A2    ;get address of next TCB
  170.     MOVE.L    TCBPrev(A0),A1 ;get addr of previous TCB
  171.     MOVE.L    A2,TCBNxt(A1)    ;close the pointers around the now-
  172.     MOVE.L    A1,TCBPrev(A2) ;sleeping task.
  173.     MOVE.B    #Sleep,TCBState(A0) ;mark it as asleep
  174.  
  175.     MOVE.L    TCBA5(A2),A5    ;get new TData base address
  176.     MOVE.L    TCBSP(A2),SP    ;get stack relative displacement
  177.     ADDA.L    A5,SP    ;restore absolute address
  178.     RTS        ;return to next task
  179. Table 1 Some of Terra Nova's system calls
  180.  
  181. Call name    Description
  182.  
  183.     (Task Manager Calls)
  184. Spawn    Create a new task
  185. Kill    Destroy a task (by task number)
  186. Suicide    Kill the calling task
  187.     (Heap Manager Calls)
  188. HeapGimme    Allocate an item in the heap
  189. HeapDel    Release (delete) a heap item
  190. FillZero    Re-initialize a heap item
  191. GetMaster    Assign a master handle for an item
  192.     (Message Manager Calls)
  193. SendMsg    Send a copy of a block of memory to another task
  194. Del1Msg    Delete message from top of incoming queue
  195. DelMsgs    Delete entire incoming message queue
  196. TxtMsg    Send a message of type "TEXT"
  197. GetMsg    Fetch next message in queue
  198. HandleMsg    Analyze incoming message and handle if standard type
  199.     (Character I/O Manager Calls)
  200. DevReq    Request a character I/O channel
  201. DevDemand    Demand a character I/O channel (usually impolite)
  202. DevRel    Release a character I/O channel
  203. PrToStd    Select this task's standard character I/O device
  204. PrToMem    Select the MemPrt device (see text)
  205.     (Text Manager Calls)
  206. GetCommand    Input a command line and parse it, passing control to the
  207.     appropriate routine based on the command.
  208. AddCmdTab    Add a set of commands to the existing command set
  209. DoCommand    Parse and execute a command already stored in memory
  210. GetPSW    Input and encrypt a password (to be compared with an
  211.     encrypted password from the user file)
  212. MoveString    Move an ASCII string
  213. GetLine    Input a line of ASCII text from the character I/O device
  214. PrLine    Print an ASCII string on the character I/O device
  215. Print    Print a line of text.  The line is expected to immediately
  216.     follow the JSR Print instruction.
  217. CompString    Compare two ASCII strings
  218.     (Miscellaneous System Calls)
  219. Random    What system would be complete without random
  220.     numbers?
  221. Sqrt    Square root of 32-bit integer
  222. Table 2 Comparison of TRAP and JSR oriented system calls
  223.  
  224. TRAP oriented calls
  225. Instruction    Cycles used    Description
  226. TRAP    #n    38    call the system routine
  227. MOVE.L    2(SP),A0    16    point to word argument
  228. MOVE.W    (A0)+,D0    8    fetch the argument
  229. MOVE.L    A0,2(SP)    16    update return address
  230. (variable)        (variable)    decode argument word
  231.  -----------            useful code
  232. RTE        24    return to caller
  233.         -----------------
  234.         104 (+ decode)    total cycles for overhead
  235.  
  236. JSR oriented calls
  237. Instruction    Cycles used    Description
  238. JSR    Label.W    18    call low memory entry point
  239. JMP    Label.L    12    call actual routine
  240.  -----------            useful code
  241. RTS        16    return to caller
  242.         -----------------
  243.         46    total cycles for overhead
  244.  
  245.