* added compilers lcc and bcc (linux86)
[mascara-docs.git] / i86 / mtx / mtx / samples / LAB5_FS / ts.x
blobc9792ed14fa4e3102db8327bb0f9b0c710e95eb1
1                 BOOTSEG = 0x1000
2 .globl begtext, begdata, begbss                      !! needed by linker
4 !               IMPORTS and EXPORTS
6 .globl _main,_prints                                 
7 .globl _tswitch,_running,_scheduler
9 .globl _int80h,_kcinth
10 .globl _goUmode
12 .globl _proc, _procsize
13 .globl _color
15 .globl _in_byte, _out_byte
17 .globl _inkmode
18 .globl _lock,_restore,_unlock
19 .globl _kbinth, _kbhandler
21 .text                                                !! these tell as:  
22 begtext:                                             !! text,data,bss segments
23 .data                                                !! are all the same.
24 begdata:
25 .bss
26 begbss:
27 .text                                                
29         mov     ax,cs                   ! establish segments 
30         mov     ds,ax                   ! we know ES,CS=0x1000. Let DS=CS  
31         mov     ss,ax                   ! SS = CS ===> all point to 0x1000
32         mov     es,ax
34         mov     sp,#_proc               ! SP -> proc[0].kstack HIGH end
35         add     sp,_procsize
37         mov     ax,#0x0003
38         int     #0x10
40         call _main                      ! call main[] in C
42 ! if ever return, just hang     
43         mov   ax, #msg
44         push  ax
45         call  _prints
46 dead:   jmp   dead
47 msg:    .asciz "BACK TO ASSEMBLY AND HANG\n\r"    
48         
49 !*************************************************************
50 !     KCW  added functions for MT system
51 !************************************************************
52 _tswitch:
53           push   ax
54           push   bx
55           push   cx
56           push   dx
57           push   bp
58           push   si
59           push   di
60           pushf
61           mov    bx, _running
62           mov    2[bx], sp
64 find:     call   _scheduler
66 resume:   mov    bx, _running
67           mov    sp, 2[bx]
68           popf
69           pop    di
70           pop    si
71           pop    bp
72           pop    dx
73           pop    cx
74           pop    bx
75           pop    ax
76           ret
80 !These offsets are defined in struct proc
81 USS =   4
82 USP =   6
84 ! as86 macro: parameters are ?1 ?2, etc 
85 ! as86 -m -l listing src (generates listing with macro expansion)
87          MACRO INTH
88           push ax
89           push bx
90           push cx
91           push dx
92           push bp
93           push si
94           push di
95           push es
96           push ds
98           push cs
99           pop  ds
101           inc _inkmode          ! enter Kmode : ++inkmode
102           cmp _inkmode,#1       ! if inkmode == 1 ==> interrupt was in Umode
103           jg  ?1                ! imode>1 : was in Kmode: bypass saving uss,usp
105           ! was in Umode: save interrupted (SS,SP) into proc
106           mov si,_running       ! ready to access proc
107           mov USS[si],ss        ! save SS  in proc.USS
108           mov USP[si],sp        ! save SP  in proc.USP
110           ! change DS,ES,SS to Kernel segment
111           mov  di,ds            ! stupid !!        
112           mov  es,di            ! CS=DS=SS=ES in Kmode
113           mov  ss,di
115           mov  sp, _running     ! sp -> running's kstack[] high end
116           add  sp, _procsize
118 ?1:       call  _?1             ! call handler in C
120           br    _ireturn        ! return to interrupted point
122          MEND
125 _int80h: INTH kcinth
126 _kbinth: INTH kbhandler
128 !*===========================================================================*
129 !*              _ireturn  and  goUmode()                                     *
130 !*===========================================================================*
131 ! ustack contains    flag,ucs,upc, ax,bx,cx,dx,bp,si,di,es,ds
132 ! uSS and uSP are in proc
133 _ireturn:
134 _goUmode:
135         cli
136         dec _inkmode            ! --inkmode
137         cmp _inkmode,#0         ! inkmode==0 means was in Umode
138         jg  xkmode
140 ! restore uSS, uSP from running PROC
141         mov si,_running         ! si -> proc
142         mov ax,USS[si]
143         mov ss,ax               ! restore SS
144         mov sp,USP[si]          ! restore SP
145 xkmode:                         
146         pop ds
147         pop es
148         pop di
149         pop si
150         pop bp
151         pop dx
152         pop cx
153         pop bx
154         pop ax 
155         iret
157 !*===========================================================================*
158 !*                              in_byte                                      *
159 !*===========================================================================*
160 ! PUBLIC unsigned in_byte[port_t port];
161 ! Read an [unsigned] byte from the i/o port  port  and return it.
163 _in_byte:
164         push    bp
165         mov     bp,sp
166         mov     dx,4[bp]
167         inb     al,dx           ! input 1 byte
168         subb    ah,ah           ! unsign extend
169         pop     bp
170         ret
172 !*===========================================================================*
173 !*                              out_byte                                     *
174 !*==============================================================
175 ! out_byte[port_t port, int value];
176 ! Write  value  [cast to a byte]  to the I/O port  port.
178 _out_byte:
179         push    bp
180         mov     bp,sp
181         mov     dx,4[bp]
182         mov     ax,6[bp]
183         outb    dx,al           ! output 1 byte
184         pop     bp
185         ret
188 _int_off:             ! cli, return old flag register
189         pushf
190         cli
191         pop ax
192         ret
194 _int_on:              ! int_on(int SR)
195         push bp
196         mov  bp,sp
198         mov  ax,4[bp] ! get SR passed in
200         push ax
201         popf
203         pop  bp
204         ret
208 lockvar: .word 0
209 !*===========================================================================*
210 !*                              lock                                         *
211 !*===========================================================================*
212 ! Disable CPU interrupts.
213 _lock:  
214         pushf                   ! save flags on stack
215         cli                     ! disable interrupts
216         pop lockvar             ! save flags for possible restoration later
217         ret                     ! return to caller
220 !*===========================================================================*
221 !*                              unlock                                       *
222 !*===========================================================================*
223 ! Enable CPU interrupts.
224 _unlock:
225         sti                     ! enable interrupts
226         ret                     ! return to caller
229 !*===========================================================================*
230 !*                              restore                                      *
231 !*===========================================================================*
232 ! Restore enable/disable bit to the value it had before last lock.
233 _restore:
234         push lockvar            ! push flags as they were before previous lock
235         popf                    ! restore flags
236         ret                     ! return to caller