Subversion Repositories Kolibri OS

Rev

Rev 2443 | Rev 2450 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;
3
;; Copyright (C) KolibriOS team 2004-2011. All rights reserved.
4
;; PROGRAMMING:
5
;; Ivan Poddubny
6
;; Marat Zakiyanov (Mario79)
7
;; VaStaNi
8
;; Trans
9
;; Mihail Semenyako (mike.dld)
10
;; Sergey Kuzmin (Wildwest)
11
;; Andrey Halyavin (halyavin)
12
;; Mihail Lisovin (Mihasik)
13
;; Andrey Ignatiev (andrew_programmer)
14
;; NoName
15
;; Evgeny Grechnikov (Diamond)
16
;; Iliya Mihailov (Ghost)
17
;; Sergey Semyonov (Serge)
18
;; Johnny_B
19
;; SPraid (simba)
20
;; Hidnplayr
21
;; Alexey Teplov ()
22
;; Rus
23
;; Nable
24
;; shurf
25
;; Alver
26
;; Maxis
27
;; Galkov
28
;; CleverMouse
29
;; tsdima
30
;; turbanoff
31
;; Asper
32
;; art_zh
33
;;
34
;; Data in this file was originally part of MenuetOS project which is
35
;; distributed under the terms of GNU GPL. It is modified and redistributed as
36
;; part of KolibriOS project under the terms of GNU GPL.
37
;;
38
;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa
39
;; PROGRAMMING:
40
;;
41
;; Ville Mikael Turjanmaa, villemt@itu.jyu.fi
42
;; - main os coding/design
43
;; Jan-Michael Brummer, BUZZ2@gmx.de
44
;; Felix Kaiser, info@felix-kaiser.de
45
;; Paolo Minazzi, paolo.minazzi@inwind.it
46
;; quickcode@mail.ru
47
;; Alexey, kgaz@crosswinds.net
48
;; Juan M. Caravaca, bitrider@wanadoo.es
49
;; kristol@nic.fi
50
;; Mike Hibbett, mikeh@oceanfree.net
51
;; Lasse Kuusijarvi, kuusijar@lut.fi
52
;; Jarek Pelczar, jarekp3@wp.pl
53
;;
54
;; KolibriOS is distributed in the hope that it will be useful, but WITHOUT ANY
55
;; WARRANTY. No author or distributor accepts responsibility to anyone for the
56
;; consequences of using it or for whether it serves any particular purpose or
57
;; works at all, unless he says so in writing. Refer to the GNU General Public
58
;; License (the "GPL") for full details.
59
;
60
;; Everyone is granted permission to copy, modify and redistribute KolibriOS,
61
;; but only under the conditions described in the GPL. A copy of this license
62
;; is supposed to have been given to you along with KolibriOS so you can know
63
;; your rights and responsibilities. It should be in a file named COPYING.
64
;; Among other things, the copyright notice and this notice must be preserved
65
;; on all copies.
66
;;
67
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
68
 
69
include 'macros.inc'
2381 hidnplayr 70
include 'struct.inc'
2288 clevermous 71
 
72
$Revision: 2446 $
73
 
74
 
75
USE_COM_IRQ     equ 1      ; make irq 3 and irq 4 available for PCI devices
76
 
77
; Enabling the next line will enable serial output console
78
;debug_com_base  equ 0x3f8  ; 0x3f8 is com1, 0x2f8 is com2, 0x3e8 is com3, 0x2e8 is com4, no irq's are used
79
 
80
include "proc32.inc"
81
include "kglobals.inc"
82
include "lang.inc"
83
 
84
include "const.inc"
85
max_processes    equ   255
86
tss_step         equ   (128+8192) ; tss & i/o - 65535 ports, * 256=557056*4
87
 
88
 
89
os_stack       equ  (os_data_l-gdts)    ; GDTs
90
os_code        equ  (os_code_l-gdts)
91
graph_data     equ  (3+graph_data_l-gdts)
92
tss0           equ  (tss0_l-gdts)
93
app_code       equ  (3+app_code_l-gdts)
94
app_data       equ  (3+app_data_l-gdts)
95
app_tls        equ  (3+tls_data_l-gdts)
96
pci_code_sel   equ  (pci_code_32-gdts)
97
pci_data_sel   equ  (pci_data_32-gdts)
98
 
99
 
100
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101
;;
102
;;   Included files:
103
;;
104
;;   Kernel16.inc
105
;;    - Booteng.inc   English text for bootup
106
;;    - Bootcode.inc  Hardware setup
107
;;    - Pci16.inc     PCI functions
108
;;
109
;;   Kernel32.inc
110
;;    - Sys32.inc     Process management
111
;;    - Shutdown.inc  Shutdown and restart
112
;;    - Fat32.inc     Read / write hd
113
;;    - Vesa12.inc    Vesa 1.2 driver
114
;;    - Vesa20.inc    Vesa 2.0 driver
115
;;    - Vga.inc       VGA driver
116
;;    - Stack.inc     Network interface
117
;;    - Mouse.inc     Mouse pointer
118
;;    - Scincode.inc  Window skinning
119
;;    - Pci32.inc     PCI functions
120
;;
121
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122
 
123
 
124
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125
;;                                                                      ;;
126
;;                  16 BIT ENTRY FROM BOOTSECTOR                        ;;
127
;;                                                                      ;;
128
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129
 
130
use16
131
                  org   0x0
132
        jmp     start_of_code
133
 
134
version db    'Kolibri OS  version 0.7.7.0+     ',13,10,13,10,0
135
 
136
include "boot/bootstr.inc"     ; language-independent boot messages
137
include "boot/preboot.inc"
138
 
139
if lang eq en
140
include "boot/booteng.inc"     ; english system boot messages
141
else if lang eq ru
142
include "boot/bootru.inc"      ; russian system boot messages
143
include "boot/ru.inc"          ; Russian font
144
else if lang eq et
145
include "boot/bootet.inc"      ; estonian system boot messages
146
include "boot/et.inc"          ; Estonian font
147
else
148
include "boot/bootge.inc"      ; german system boot messages
149
end if
150
 
151
include "boot/bootcode.inc"    ; 16 bit system boot code
152
include "bus/pci/pci16.inc"
153
include "detect/biosdisk.inc"
154
 
155
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
156
;;                                                                      ;;
157
;;                  SWITCH TO 32 BIT PROTECTED MODE                     ;;
158
;;                                                                      ;;
159
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
160
 
161
 
162
; CR0 Flags - Protected mode and Paging
163
 
164
        mov     ecx, CR0_PE
165
 
166
; Enabling 32 bit protected mode
167
 
168
        sidt    [cs:old_ints_h]
169
 
170
        cli                             ; disable all irqs
171
        cld
172
        mov     al, 255                 ; mask all irqs
173
        out     0xa1, al
174
        out     0x21, al
175
   l.5:
176
        in      al, 0x64                ; Enable A20
177
        test    al, 2
178
        jnz     l.5
179
        mov     al, 0xD1
180
        out     0x64, al
181
   l.6:
182
        in      al, 0x64
183
        test    al, 2
184
        jnz     l.6
185
        mov     al, 0xDF
186
        out     0x60, al
187
   l.7:
188
        in      al, 0x64
189
        test    al, 2
190
        jnz     l.7
191
        mov     al, 0xFF
192
        out     0x64, al
193
 
194
        lgdt    [cs:tmp_gdt]            ; Load GDT
195
        mov     eax, cr0                ; protected mode
196
        or      eax, ecx
197
        and     eax, 10011111b *65536*256 + 0xffffff ; caching enabled
198
        mov     cr0, eax
199
        jmp     pword os_code:B32       ; jmp to enable 32 bit mode
200
 
201
align 8
202
tmp_gdt:
203
 
204
        dw     23
205
        dd     tmp_gdt+0x10000
206
        dw     0
207
 
208
        dw     0xffff
209
        dw     0x0000
210
        db     0x00
211
        dw     11011111b *256 +10011010b
212
        db     0x00
213
 
214
        dw     0xffff
215
        dw     0x0000
216
        db     0x00
217
        dw     11011111b *256 +10010010b
218
        db     0x00
219
 
220
include "data16.inc"
221
 
222
use32
223
org $+0x10000
224
 
225
align 4
226
B32:
227
        mov     ax, os_stack       ; Selector for os
228
        mov     ds, ax
229
        mov     es, ax
230
        mov     fs, ax
231
        mov     gs, ax
232
        mov     ss, ax
233
        mov     esp, 0x006CC00       ; Set stack
234
 
235
; CLEAR 0x280000 - HEAP_BASE
236
 
237
        xor     eax, eax
238
        mov     edi, CLEAN_ZONE
239
        mov     ecx, (HEAP_BASE-OS_BASE-CLEAN_ZONE) / 4
240
        cld
241
        rep stosd
242
 
243
; CLEAR KERNEL UNDEFINED GLOBALS
244
        mov     edi, endofcode-OS_BASE
245
        mov     ecx, 0x90000
246
        sub     ecx, edi
247
        shr     ecx, 2
248
        rep stosd
249
 
250
; SAVE & CLEAR 0-0xffff
251
 
252
        xor     esi, esi
253
        mov     edi, (BOOT_VAR-OS_BASE)
254
        mov     ecx, 0x10000 / 4
255
        rep movsd
256
        mov     edi, 0x1000
257
        mov     ecx, 0xf000 / 4
258
        rep stosd
259
 
260
        call    test_cpu
261
        bts     [cpu_caps-OS_BASE], CAPS_TSC    ;force use rdtsc
262
 
263
        call    check_acpi
264
        call    init_BIOS32
265
; MEMORY MODEL
266
        call    mem_test
267
        call    init_mem
268
        call    init_page_map
269
 
270
; ENABLE PAGING
271
 
272
        mov     eax, sys_pgdir-OS_BASE
273
        mov     cr3, eax
274
 
275
        mov     eax, cr0
276
        or      eax, CR0_PG+CR0_WP
277
        mov     cr0, eax
278
 
279
        lgdt    [gdts]
280
        jmp     pword os_code:high_code
281
 
282
align 4
283
bios32_entry    dd ?
284
tmp_page_tabs   dd ?
285
 
286
use16
287
org $-0x10000
288
include "boot/shutdown.inc" ; shutdown or restart
289
org $+0x10000
290
use32
291
 
292
__DEBUG__ fix 1
293
__DEBUG_LEVEL__ fix 1
294
include 'init.inc'
295
 
296
org OS_BASE+$
297
 
2443 Serge 298
include 'fdo.inc'
299
 
2288 clevermous 300
align 4
301
high_code:
302
        mov     ax, os_stack
303
        mov     bx, app_data
304
        mov     cx, app_tls
305
        mov     ss, ax
306
        add     esp, OS_BASE
307
 
308
        mov     ds, bx
309
        mov     es, bx
310
        mov     fs, cx
311
        mov     gs, bx
312
 
313
        bt      [cpu_caps], CAPS_PGE
314
        jnc     @F
315
 
316
        or      dword [sys_pgdir+(OS_BASE shr 20)], PG_GLOBAL
317
 
318
        mov     ebx, cr4
319
        or      ebx, CR4_PGE
320
        mov     cr4, ebx
321
@@:
322
        xor     eax, eax
323
        mov     dword [sys_pgdir], eax
324
        mov     dword [sys_pgdir+4], eax
325
 
326
        mov     eax, cr3
327
        mov     cr3, eax          ; flush TLB
328
 
329
        mov     ecx, pg_data.mutex
330
        call    mutex_init
331
 
332
        mov     ecx, disk_list_mutex
333
        call    mutex_init
334
 
335
; SAVE REAL MODE VARIABLES
336
        mov     ax, [BOOT_VAR + 0x9031]
337
        mov     [IDEContrRegsBaseAddr], ax
338
; --------------- APM ---------------------
339
 
340
; init selectors
341
        mov     ebx, [BOOT_VAR+0x9040]          ; offset of APM entry point
342
        movzx   eax, word [BOOT_VAR+0x9050]; real-mode segment base address of
343
                                                                                ; protected-mode 32-bit code segment
344
        movzx   ecx, word [BOOT_VAR+0x9052]; real-mode segment base address of
345
                                                                                ; protected-mode 16-bit code segment
346
        movzx   edx, word [BOOT_VAR+0x9054]; real-mode segment base address of
347
                                                                                ; protected-mode 16-bit data segment
348
 
349
        shl     eax, 4
350
        mov     [dword apm_code_32 + 2], ax
351
        shr     eax, 16
352
        mov     [dword apm_code_32 + 4], al
353
 
354
        shl     ecx, 4
355
        mov     [dword apm_code_16 + 2], cx
356
        shr     ecx, 16
357
        mov     [dword apm_code_16 + 4], cl
358
 
359
        shl     edx, 4
360
        mov     [dword apm_data_16 + 2], dx
361
        shr     edx, 16
362
        mov     [dword apm_data_16 + 4], dl
363
 
364
        mov     dword[apm_entry], ebx
365
        mov     word [apm_entry + 4], apm_code_32 - gdts
366
 
367
        mov     eax, [BOOT_VAR + 0x9044]; version & flags
368
        mov     [apm_vf], eax
369
; -----------------------------------------
370
;        movzx eax,byte [BOOT_VAR+0x9010]       ; mouse port
371
;        mov   [0xF604],byte 1  ;al
372
        mov     al, [BOOT_VAR+0x901F]                   ; DMA access
373
        mov     [allow_dma_access], al
374
        movzx   eax, byte [BOOT_VAR+0x9000]      ; bpp
375
        mov     [ScreenBPP], al
376
 
377
        mov     [_display.bpp], eax
378
        mov     [_display.vrefresh], 60
379
 
380
        movzx   eax, word [BOOT_VAR+0x900A]; X max
381
        mov     [_display.width], eax
382
        dec     eax
383
        mov     [Screen_Max_X], eax
384
        mov     [screen_workarea.right], eax
385
        movzx   eax, word [BOOT_VAR+0x900C]; Y max
386
        mov     [_display.height], eax
387
        dec     eax
388
        mov     [Screen_Max_Y], eax
389
        mov     [screen_workarea.bottom], eax
390
        movzx   eax, word [BOOT_VAR+0x9008]; screen mode
391
        mov     [SCR_MODE], eax
2407 mario79 392
;        mov     eax, [BOOT_VAR+0x9014]    ; Vesa 1.2 bnk sw add
393
;        mov     [BANK_SWITCH], eax
2288 clevermous 394
        mov     [BytesPerScanLine], word 640*4      ; Bytes PerScanLine
395
        cmp     [SCR_MODE], word 0x13       ; 320x200
396
        je      @f
397
        cmp     [SCR_MODE], word 0x12       ; VGA 640x480
398
        je      @f
399
        movzx   eax, word[BOOT_VAR+0x9001]      ; for other modes
400
        mov     [BytesPerScanLine], ax
401
        mov     [_display.pitch], eax
402
@@:
403
        mov     eax, [_display.width]
404
        mul     [_display.height]
405
        mov     [_WinMapSize], eax
406
 
2438 mario79 407
        call    calculate_fast_getting_offset_for_WinMapAddress
408
 
2288 clevermous 409
        mov     esi, BOOT_VAR+0x9080
410
        movzx   ecx, byte [esi-1]
411
        mov     [NumBiosDisks], ecx
412
        mov     edi, BiosDisksData
413
        rep movsd
414
 
415
; GRAPHICS ADDRESSES
416
 
417
        and     byte [BOOT_VAR+0x901e], 0x0
418
        mov     eax, [BOOT_VAR+0x9018]
419
        mov     [LFBAddress], eax
420
 
421
        cmp     [SCR_MODE], word 0100000000000000b
422
        jge     setvesa20
2407 mario79 423
        cmp     [SCR_MODE], word 0x13  ; EGA 320*200 256 colors
2288 clevermous 424
        je      v20ga32
425
        jmp     v20ga24
2436 mario79 426
 
2407 mario79 427
setvesa20:
2288 clevermous 428
        mov     [PUTPIXEL], dword Vesa20_putpixel24 ; Vesa 2.0
429
        mov     [GETPIXEL], dword Vesa20_getpixel24
430
        cmp     [ScreenBPP], byte 24
431
        jz      v20ga24
2407 mario79 432
v20ga32:
2288 clevermous 433
        mov     [PUTPIXEL], dword Vesa20_putpixel32
434
        mov     [GETPIXEL], dword Vesa20_getpixel32
2407 mario79 435
        jmp     no_mode_0x12
436
v20ga24:
2288 clevermous 437
        cmp     [SCR_MODE], word 0x12               ; 16 C VGA 640x480
438
        jne     no_mode_0x12
439
        mov     [PUTPIXEL], dword VGA_putpixel
440
        mov     [GETPIXEL], dword Vesa20_getpixel32
2407 mario79 441
no_mode_0x12:
2288 clevermous 442
 
443
; -------- Fast System Call init ----------
444
; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
445
        bt      [cpu_caps], CAPS_SEP
446
        jnc     .SEnP  ; SysEnter not Present
447
        xor     edx, edx
448
        mov     ecx, MSR_SYSENTER_CS
449
        mov     eax, os_code
450
        wrmsr
451
        mov     ecx, MSR_SYSENTER_ESP
452
;           mov eax, sysenter_stack ; Check it
453
        xor     eax, eax
454
        wrmsr
455
        mov     ecx, MSR_SYSENTER_EIP
456
        mov     eax, sysenter_entry
457
        wrmsr
458
.SEnP:
459
; AMD SYSCALL/SYSRET
460
        cmp     byte[cpu_vendor], 'A'
461
        jne     .noSYSCALL
462
        mov     eax, 0x80000001
463
        cpuid
464
        test    edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
465
        jz      .noSYSCALL
466
        mov     ecx, MSR_AMD_EFER
467
        rdmsr
468
        or      eax, 1 ; bit_0 - System Call Extension (SCE)
469
        wrmsr
470
 
471
        ; !!!! It`s dirty hack, fix it !!!
472
        ; Bits of EDX :
473
        ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
474
        ;  and the contents of this field, plus 8, are copied into the SS register.
475
        ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
476
        ;  and the contents of this field, plus 8, are copied into the SS register.
477
 
478
        ; mov   edx, (os_code + 16) * 65536 + os_code
479
        mov     edx, 0x1B0008
480
 
481
        mov     eax, syscall_entry
482
        mov     ecx, MSR_AMD_STAR
483
        wrmsr
484
.noSYSCALL:
485
; -----------------------------------------
486
        stdcall alloc_page
487
        stdcall map_page, tss-0xF80, eax, PG_SW
488
        stdcall alloc_page
489
        inc     eax
490
        mov     [SLOT_BASE+256+APPDATA.io_map], eax
491
        stdcall map_page, tss+0x80, eax, PG_SW
492
        stdcall alloc_page
493
        inc     eax
494
        mov     dword [SLOT_BASE+256+APPDATA.io_map+4], eax
495
        stdcall map_page, tss+0x1080, eax, PG_SW
496
 
497
; LOAD IDT
498
 
499
        call    build_interrupt_table ;lidt is executed
500
          ;lidt [idtreg]
501
 
502
        call    init_kernel_heap
503
        stdcall kernel_alloc, RING0_STACK_SIZE+512
504
        mov     [os_stack_seg], eax
505
 
506
        lea     esp, [eax+RING0_STACK_SIZE]
507
 
508
        mov     [tss._ss0], os_stack
509
        mov     [tss._esp0], esp
510
        mov     [tss._esp], esp
511
        mov     [tss._cs], os_code
512
        mov     [tss._ss], os_stack
513
        mov     [tss._ds], app_data
514
        mov     [tss._es], app_data
515
        mov     [tss._fs], app_data
516
        mov     [tss._gs], app_data
517
        mov     [tss._io], 128
518
;Add IO access table - bit array of permitted ports
519
        mov     edi, tss._io_map_0
520
        xor     eax, eax
521
        not     eax
522
        mov     ecx, 8192/4
523
        rep stosd                    ; access to 4096*8=65536 ports
524
 
525
        mov     ax, tss0
526
        ltr     ax
527
 
528
        mov     [LFBSize], 0x800000
529
        call    init_LFB
530
        call    init_fpu
531
        call    init_malloc
532
 
533
        stdcall alloc_kernel_space, 0x51000
534
        mov     [default_io_map], eax
535
 
536
        add     eax, 0x2000
537
        mov     [ipc_tmp], eax
538
        mov     ebx, 0x1000
539
 
540
        add     eax, 0x40000
541
        mov     [proc_mem_map], eax
542
 
543
        add     eax, 0x8000
544
        mov     [proc_mem_pdir], eax
545
 
546
        add     eax, ebx
547
        mov     [proc_mem_tab], eax
548
 
549
        add     eax, ebx
550
        mov     [tmp_task_pdir], eax
551
 
552
        add     eax, ebx
553
        mov     [tmp_task_ptab], eax
554
 
555
        add     eax, ebx
556
        mov     [ipc_pdir], eax
557
 
558
        add     eax, ebx
559
        mov     [ipc_ptab], eax
560
 
561
        stdcall kernel_alloc, (unpack.LZMA_BASE_SIZE+(unpack.LZMA_LIT_SIZE shl \
562
                (unpack.lc+unpack.lp)))*4
563
 
564
        mov     [unpack.p], eax
565
 
566
        call    init_events
2384 hidnplayr 567
        mov     eax, srv.fd-SRV.fd
2288 clevermous 568
        mov     [srv.fd], eax
569
        mov     [srv.bk], eax
570
 
571
;Set base of graphic segment to linear address of LFB
572
        mov     eax, [LFBAddress]         ; set for gs
573
        mov     [graph_data_l+2], ax
574
        shr     eax, 16
575
        mov     [graph_data_l+4], al
576
        mov     [graph_data_l+7], ah
577
 
578
        stdcall kernel_alloc, [_WinMapSize]
579
        mov     [_WinMapAddress], eax
580
 
581
        xor     eax, eax
582
        inc     eax
583
        mov     [CURRENT_TASK], eax     ;dword 1
584
        mov     [TASK_COUNT], eax       ;dword 1
585
        mov     [TASK_BASE], dword TASK_DATA
586
        mov     [current_slot], SLOT_BASE+256
587
 
588
; set background
589
 
590
        mov     [BgrDrawMode], eax
591
        mov     [BgrDataWidth], eax
592
        mov     [BgrDataHeight], eax
593
        mov     [mem_BACKGROUND], 4
594
        mov     [img_background], static_background_data
595
 
596
        mov     [SLOT_BASE + 256 + APPDATA.dir_table], sys_pgdir - OS_BASE
597
 
598
        stdcall kernel_alloc, 0x10000/8
599
        mov     edi, eax
600
        mov     [network_free_ports], eax
601
        or      eax, -1
602
        mov     ecx, 0x10000/32
603
        rep stosd
604
 
605
; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
606
 
607
        call    init_irqs
608
        call    PIC_init
609
 
610
; Initialize system V86 machine
611
        call    init_sys_v86
612
 
613
; Initialize system timer (IRQ0)
614
        call    PIT_init
615
 
616
; Try to Initialize APIC
617
        call    APIC_init
618
 
619
; Enable timer IRQ (IRQ0) and hard drives IRQs (IRQ14, IRQ15)
620
; they are used: when partitions are scanned, hd_read relies on timer
621
        call    unmask_timer
622
        stdcall enable_irq, 2               ; @#$%! PIC
623
        stdcall enable_irq, 6               ; FDD
624
        stdcall enable_irq, 13              ; co-processor
625
        stdcall enable_irq, 14
626
        stdcall enable_irq, 15
627
 
628
; Enable interrupts in IDE controller
629
        mov     al, 0
630
        mov     dx, 0x3F6
631
        out     dx, al
632
        mov     dl, 0x76
633
        out     dx, al
634
 
635
;!!!!!!!!!!!!!!!!!!!!!!!!!!
636
include 'detect/disks.inc'
637
;!!!!!!!!!!!!!!!!!!!!!!!!!!
638
 
639
        call    Parser_params
640
 
641
if ~ defined extended_primary_loader
642
; ramdisk image should be loaded by extended primary loader if it exists
643
; READ RAMDISK IMAGE FROM HD
644
 
645
;!!!!!!!!!!!!!!!!!!!!!!!
646
include 'boot/rdload.inc'
647
;!!!!!!!!!!!!!!!!!!!!!!!
648
end if
649
;    mov    [dma_hdd],1
650
; CALCULATE FAT CHAIN FOR RAMDISK
651
 
652
        call    calculatefatchain
653
 
654
if 0
655
        mov     ax, [OS_BASE+0x10000+bx_from_load]
656
        cmp     ax, 'r1'; if using not ram disk, then load librares and parameters {SPraid.simba}
657
        je      no_lib_load
658
; LOADING LIBRARES
659
        stdcall dll.Load, @IMPORT           ; loading librares for kernel (.obj files)
660
        call    load_file_parse_table       ; prepare file parse table
661
        call    set_kernel_conf             ; configure devices and gui
662
no_lib_load:
663
end if
664
 
665
; LOAD FONTS I and II
666
 
667
        stdcall read_file, char, FONT_I, 0, 2304
668
        stdcall read_file, char2, FONT_II, 0, 2560
669
 
2430 mario79 670
        mov     [MOUSE_PICTURE], dword mousepointer
671
        mov     [_display.check_mouse], check_mouse_area_for_putpixel
672
        mov     [_display.check_m_pixel], check_mouse_area_for_getpixel
673
 
2288 clevermous 674
        mov     esi, boot_fonts
675
        call    boot_log
676
 
677
; Display APIC status
678
        mov     esi, boot_APIC_found
679
        cmp     [irq_mode], IRQ_APIC
680
        je      @f
681
        mov     esi, boot_APIC_nfound
682
@@:
683
 
684
; PRINT AMOUNT OF MEMORY
685
        mov     esi, boot_memdetect
686
        call    boot_log
687
 
688
        movzx   ecx, word [boot_y]
689
        if lang eq ru
690
        or      ecx, (10+30*6) shl 16
691
                else
692
        or      ecx, (10+29*6) shl 16
693
                end if
694
        sub     ecx, 10
695
        mov     edx, 0xFFFFFF
696
        mov     ebx, [MEM_AMOUNT]
697
        shr     ebx, 20
698
        xor     edi, edi
699
        mov     eax, 0x00040000
700
        inc     edi
701
        call    display_number_force
702
 
703
; BUILD SCHEDULER
704
 
705
        call    build_scheduler; sys32.inc
706
 
707
        mov     esi, boot_devices
708
        call    boot_log
709
 
710
        mov     [pci_access_enabled], 1
711
 
712
 
713
; SET PRELIMINARY WINDOW STACK AND POSITIONS
714
 
715
        mov     esi, boot_windefs
716
        call    boot_log
717
        call    set_window_defaults
718
 
719
; SET BACKGROUND DEFAULTS
720
 
721
        mov     esi, boot_bgr
722
        call    boot_log
723
        call    init_background
724
        call    calculatebackground
725
 
726
; RESERVE SYSTEM IRQ'S JA PORT'S
727
 
728
        mov     esi, boot_resirqports
729
        call    boot_log
730
        call    reserve_irqs_ports
731
 
732
; SET UP OS TASK
733
 
734
        mov     esi, boot_setostask
735
        call    boot_log
736
 
737
        xor     eax, eax
738
        mov     dword [SLOT_BASE+APPDATA.fpu_state], fpu_data
739
        mov     dword [SLOT_BASE+APPDATA.exc_handler], eax
740
        mov     dword [SLOT_BASE+APPDATA.except_mask], eax
741
 
742
        ; name for OS/IDLE process
743
 
744
        mov     dword [SLOT_BASE+256+APPDATA.app_name], dword 'OS/I'
745
        mov     dword [SLOT_BASE+256+APPDATA.app_name+4], dword 'DLE '
746
        mov     edi, [os_stack_seg]
747
        mov     dword [SLOT_BASE+256+APPDATA.pl0_stack], edi
748
        add     edi, 0x2000-512
749
        mov     dword [SLOT_BASE+256+APPDATA.fpu_state], edi
750
        mov     dword [SLOT_BASE+256+APPDATA.saved_esp0], edi; just for case
751
        ; [SLOT_BASE+256+APPDATA.io_map] was set earlier
752
 
753
        mov     esi, fpu_data
754
        mov     ecx, 512/4
755
        cld
756
        rep movsd
757
 
758
        mov     dword [SLOT_BASE+256+APPDATA.exc_handler], eax
759
        mov     dword [SLOT_BASE+256+APPDATA.except_mask], eax
760
 
761
        mov     ebx, SLOT_BASE+256+APP_OBJ_OFFSET
762
        mov     dword [SLOT_BASE+256+APPDATA.fd_obj], ebx
763
        mov     dword [SLOT_BASE+256+APPDATA.bk_obj], ebx
764
 
765
        mov     dword [SLOT_BASE+256+APPDATA.cur_dir], sysdir_path
766
        mov     dword [SLOT_BASE+256+APPDATA.tls_base], eax
767
 
768
        ; task list
769
        mov     dword [TASK_DATA+TASKDATA.mem_start], eax; process base address
770
        inc     eax
771
        mov     dword [CURRENT_TASK], eax
772
        mov     dword [TASK_COUNT], eax
773
        mov     [current_slot], SLOT_BASE+256
774
        mov     [TASK_BASE], dword TASK_DATA
775
        mov     byte[TASK_DATA+TASKDATA.wnd_number], al ; on screen number
776
        mov     dword [TASK_DATA+TASKDATA.pid], eax     ; process id number
777
 
778
        call    init_display
779
        mov     eax, [def_cursor]
780
        mov     [SLOT_BASE+APPDATA.cursor], eax
781
        mov     [SLOT_BASE+APPDATA.cursor+256], eax
782
 
783
  ; READ TSC / SECOND
784
 
785
        mov     esi, boot_tsc
786
        call    boot_log
787
        cli
788
        rdtsc   ;call  _rdtsc
789
        mov     ecx, eax
790
        mov     esi, 250            ; wait 1/4 a second
791
        call    delay_ms
792
        rdtsc   ;call  _rdtsc
793
        sti
794
        sub     eax, ecx
795
        shl     eax, 2
796
        mov     [CPU_FREQ], eax       ; save tsc / sec
797
;       mov ebx, 1000000
798
;       div ebx
799
; ¢®®¡é¥-â® ¯à®¨§¢®¤¨â¥«ì­®áâì ¢ ¤ ­­®¬ ª®­ªà¥â­®¬ ¬¥áâ¥
800
; ᮢ¥à襭­® ­¥ªà¨â¨ç­ , ­® çâ®¡ë § âª­ãâì «î¡¨â¥«¥©
801
; ®¯â¨¬¨§¨àãîé¨å ª®¬¯¨«ïâ®à®¢ Ÿ‚“...
802
        mov     edx, 2251799814
803
        mul     edx
804
        shr     edx, 19
805
        mov     [stall_mcs], edx
806
; PRINT CPU FREQUENCY
807
        mov     esi, boot_cpufreq
808
        call    boot_log
809
 
810
        mov     ebx, edx
811
        movzx   ecx, word [boot_y]
812
        if lang eq ru
813
        add     ecx, (10+19*6) shl 16 - 10         ; 'Determining amount of memory'
814
                else
815
        add     ecx, (10+17*6) shl 16 - 10         ; 'Determining amount of memory'
816
                end if
817
        mov     edx, 0xFFFFFF
818
        xor     edi, edi
819
        mov     eax, 0x00040000
820
        inc     edi
821
        call    display_number_force
822
 
823
; SET VARIABLES
824
 
825
        call    set_variables
826
 
827
; STACK AND FDC
828
 
829
        call    stack_init
830
        call    fdc_init
831
 
832
; PALETTE FOR 320x200 and 640x480 16 col
833
 
834
        cmp     [SCR_MODE], word 0x12
835
        jne     no_pal_vga
836
        mov     esi, boot_pal_vga
837
        call    boot_log
838
        call    paletteVGA
839
      no_pal_vga:
840
 
841
        cmp     [SCR_MODE], word 0x13
842
        jne     no_pal_ega
843
        mov     esi, boot_pal_ega
844
        call    boot_log
845
        call    palette320x200
846
      no_pal_ega:
847
 
848
; LOAD DEFAULT SKIN
849
 
850
        call    load_default_skin
851
 
852
;protect io permission map
853
 
854
        mov     esi, [default_io_map]
855
        stdcall map_page, esi, [SLOT_BASE+256+APPDATA.io_map], PG_MAP
856
        add     esi, 0x1000
857
        stdcall map_page, esi, [SLOT_BASE+256+APPDATA.io_map+4], PG_MAP
858
 
859
        stdcall map_page, tss._io_map_0, \
860
                [SLOT_BASE+256+APPDATA.io_map], PG_MAP
861
        stdcall map_page, tss._io_map_1, \
862
                [SLOT_BASE+256+APPDATA.io_map+4], PG_MAP
863
 
864
        mov     ax, [OS_BASE+0x10000+bx_from_load]
865
        cmp     ax, 'r1'; if not rused ram disk - load network configuration from files {SPraid.simba}
866
        je      no_st_network
867
        call    set_network_conf
868
  no_st_network:
869
 
870
; LOAD FIRST APPLICATION
871
        cli
872
 
873
;        cmp   byte [BOOT_VAR+0x9030],1
874
;        jne   no_load_vrr_m
875
 
876
;        mov     ebp, vrr_m
877
;        call    fs_execute_from_sysdir
878
;
879
;;        cmp   eax,2                  ; if vrr_m app found (PID=2)
880
;       sub   eax,2
881
;        jz    first_app_found
882
;
883
;no_load_vrr_m:
884
 
885
        mov     ebp, firstapp
886
        call    fs_execute_from_sysdir
887
 
888
;        cmp   eax,2                  ; continue if a process has been loaded
889
        sub     eax, 2
890
        jz      first_app_found
891
 
892
        mov     esi, boot_failed
893
        call    boot_log
894
 
895
        mov     eax, 0xDEADBEEF      ; otherwise halt
896
        hlt
897
 
898
first_app_found:
899
 
900
        cli
901
 
902
        ;mov   [TASK_COUNT],dword 2
903
        push    1
904
        pop     dword [CURRENT_TASK]    ; set OS task fisrt
905
 
906
; SET KEYBOARD PARAMETERS
907
        mov     al, 0xf6       ; reset keyboard, scan enabled
908
        call    kb_write
909
 
910
        ; wait until 8042 is ready
911
        xor     ecx, ecx
912
      @@:
913
        in      al, 64h
914
        and     al, 00000010b
915
        loopnz  @b
916
 
917
       ; mov   al, 0xED       ; Keyboard LEDs - only for testing!
918
       ; call  kb_write
919
       ; call  kb_read
920
       ; mov   al, 111b
921
       ; call  kb_write
922
       ; call  kb_read
923
 
924
        mov     al, 0xF3     ; set repeat rate & delay
925
        call    kb_write
926
;        call  kb_read
927
        mov     al, 0; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
928
        call    kb_write
929
;        call  kb_read
930
     ;// mike.dld [
931
        call    set_lights
932
     ;// mike.dld ]
933
        stdcall attach_int_handler, 1, irq1, 0
934
 
935
; SET MOUSE
936
 
937
        stdcall load_driver, szPS2MDriver
938
;        stdcall load_driver, szCOM_MDriver
939
 
940
        mov     esi, boot_setmouse
941
        call    boot_log
942
        call    setmouse
943
 
944
; Setup serial output console (if enabled)
945
 
946
if defined debug_com_base
947
 
948
        ; enable Divisor latch
949
 
950
        mov     dx, debug_com_base+3
951
        mov     al, 1 shl 7
952
        out     dx, al
953
 
954
        ; Set speed to 115200 baud (max speed)
955
 
956
        mov     dx, debug_com_base
957
        mov     al, 0x01
958
        out     dx, al
959
 
960
        mov     dx, debug_com_base+1
961
        mov     al, 0x00
962
        out     dx, al
963
 
964
        ; No parity, 8bits words, one stop bit, dlab bit back to 0
965
 
966
        mov     dx, debug_com_base+3
967
        mov     al, 3
968
        out     dx, al
969
 
970
        ; disable interrupts
971
 
972
        mov     dx, debug_com_base+1
973
        mov     al, 0
974
        out     dx, al
975
 
976
        ; clear +  enable fifo (64 bits)
977
 
978
        mov     dx, debug_com_base+2
979
        mov     al, 0x7 + 1 shl 5
980
        out     dx, al
981
 
982
 
983
end if
984
 
2443 Serge 985
        mov     eax, [cpu_count]
986
        test    eax, eax
987
        jnz     @F
988
        mov     al, 1                             ; at least one CPU
989
@@:
990
        DEBUGF  1, "K : %d CPU detected\n", eax
991
 
2288 clevermous 992
; START MULTITASKING
993
 
994
; A 'All set - press ESC to start' messages if need
995
if preboot_blogesc
996
        mov     esi, boot_tasking
997
        call    boot_log
998
.bll1:
999
        in      al, 0x60        ; wait for ESC key press
1000
        cmp     al, 129
1001
        jne     .bll1
1002
end if
1003
 
1004
        cmp     [IDEContrRegsBaseAddr], 0
1005
        setnz   [dma_hdd]
1006
        mov     [timer_ticks_enable], 1         ; for cd driver
1007
 
1008
        sti
1009
        call    change_task
1010
 
1011
        jmp     osloop
1012
 
1013
 
1014
        ; Fly :)
1015
 
1016
include 'unpacker.inc'
1017
 
1018
align 4
1019
boot_log:
1020
        pushad
1021
 
1022
        mov     ebx, 10*65536
1023
        mov     bx, word [boot_y]
1024
        add     [boot_y], dword 10
1025
        mov     ecx, 0x80ffffff; ASCIIZ string with white color
1026
        xor     edi, edi
1027
        mov     edx, esi
1028
        inc     edi
1029
        call    dtext
1030
 
1031
        mov     [novesachecksum], 1000
1032
        call    checkVga_N13
1033
 
1034
        popad
1035
 
1036
        ret
1037
 
1038
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1039
;                                                                    ;
1040
;                    MAIN OS LOOP START                              ;
1041
;                                                                    ;
1042
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1043
align 32
1044
osloop:
1045
        call    [draw_pointer]
1046
        call    window_check_events
1047
        call    mouse_check_events
1048
        call    checkmisc
1049
        call    checkVga_N13
1050
        call    stack_handler
1051
        call    checkidle
1052
        call    check_fdd_motor_status
1053
        call    check_ATAPI_device_event
1054
        call    check_timers
1055
        jmp     osloop
1056
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1057
;                                                                    ;
1058
;                      MAIN OS LOOP END                              ;
1059
;                                                                    ;
1060
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1061
align 4
1062
checkidle:
1063
        pushad
1064
        call    change_task
1065
        jmp     idle_loop_entry
1066
  idle_loop:
1067
        cmp     eax, [idlemem]    ; eax == [timer_ticks]
1068
        jne     idle_exit
1069
        rdtsc   ;call _rdtsc
1070
        mov     ecx, eax
1071
        hlt
1072
        rdtsc   ;call _rdtsc
1073
        sub     eax, ecx
1074
        add     [idleuse], eax
1075
  idle_loop_entry:
1076
        mov     eax, [timer_ticks]; eax =  [timer_ticks]
1077
        cmp     [check_idle_semaphore], 0
1078
        je      idle_loop
1079
        dec     [check_idle_semaphore]
1080
  idle_exit:
1081
        mov     [idlemem], eax    ; eax == [timer_ticks]
1082
        popad
1083
        ret
1084
 
1085
uglobal
1086
  idlemem               dd   0x0
1087
  idleuse               dd   0x0
1088
  idleusesec            dd   0x0
1089
  check_idle_semaphore  dd   0x0
1090
endg
1091
 
1092
 
1093
 
1094
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1095
;                                                                      ;
1096
;                   INCLUDED SYSTEM FILES                              ;
1097
;                                                                      ;
1098
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1099
 
1100
 
1101
include "kernel32.inc"
1102
 
1103
 
1104
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1105
;                                                                      ;
1106
;                       KERNEL FUNCTIONS                               ;
1107
;                                                                      ;
1108
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1109
 
1110
reserve_irqs_ports:
1111
 
1112
 
1113
; RESERVE PORTS
1114
        mov     eax, RESERVED_PORTS
1115
        mov     ecx, 1
1116
 
1117
        mov     [eax], dword 4
1118
 
1119
        mov     [eax+16], ecx
1120
        mov     [eax+16+4], dword 0
1121
        mov     [eax+16+4], dword 0x2D
1122
 
1123
        mov     [eax+32], ecx
1124
        mov     [eax+32+4], dword 0x30
1125
        mov     [eax+32+8], dword 0x4D
1126
 
1127
        mov     [eax+48], ecx
1128
        mov     [eax+48+4], dword 0x50
1129
        mov     [eax+28+8], dword 0xDF
1130
 
1131
        mov     [eax+64], ecx
1132
        mov     [eax+64+4], dword 0xE5
1133
        mov     [eax+64+8], dword 0xFF
1134
 
1135
        ret
1136
 
1137
 
1138
iglobal
1139
  process_number dd 0x1
1140
endg
1141
 
1142
set_variables:
1143
 
1144
        mov     ecx, 0x16                    ; flush port 0x60
1145
.fl60:
1146
        in      al, 0x60
1147
        loop    .fl60
1148
        push    eax
1149
 
1150
        mov     ax, [BOOT_VAR+0x900c]
1151
        shr     ax, 1
1152
        shl     eax, 16
1153
        mov     ax, [BOOT_VAR+0x900A]
1154
        shr     ax, 1
1155
        mov     [MOUSE_X], eax
1156
 
1157
        xor     eax, eax
1158
        mov     [BTN_ADDR], dword BUTTON_INFO ; address of button list
1159
 
1160
        mov     byte [MOUSE_BUFF_COUNT], al              ; mouse buffer
1161
        mov     byte [KEY_COUNT], al              ; keyboard buffer
1162
        mov     byte [BTN_COUNT], al              ; button buffer
1163
;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
1164
 
1165
     ;!! IP 04.02.2005:
1166
        mov     byte [DONT_SWITCH], al; change task if possible
1167
        pop     eax
1168
        ret
1169
 
1170
align 4
1171
;input  eax=43,bl-byte of output, ecx - number of port
1172
sys_outport:
1173
 
1174
        mov     edi, ecx   ; separate flag for read / write
1175
        and     ecx, 65535
1176
 
1177
        mov     eax, [RESERVED_PORTS]
1178
        test    eax, eax
1179
        jnz     .sopl8
1180
        inc     eax
1181
        mov     [esp+32], eax
1182
        ret
1183
 
1184
  .sopl8:
1185
        mov     edx, [TASK_BASE]
1186
        mov     edx, [edx+0x4]
1187
    ;and   ecx,65535
1188
    ;cld - set on interrupt 0x40
1189
  .sopl1:
1190
 
1191
        mov     esi, eax
1192
        shl     esi, 4
1193
        add     esi, RESERVED_PORTS
1194
        cmp     edx, [esi+0]
1195
        jne     .sopl2
1196
        cmp     ecx, [esi+4]
1197
        jb      .sopl2
1198
        cmp     ecx, [esi+8]
1199
        jg      .sopl2
1200
.sopl3:
1201
 
1202
        test    edi, 0x80000000; read ?
1203
        jnz     .sopl4
1204
 
1205
        mov     eax, ebx
1206
        mov     dx, cx   ; write
1207
        out     dx, al
1208
        and     [esp+32], dword 0
1209
        ret
1210
 
1211
        .sopl2:
1212
 
1213
        dec     eax
1214
        jnz     .sopl1
1215
        inc     eax
1216
        mov     [esp+32], eax
1217
        ret
1218
 
1219
 
1220
  .sopl4:
1221
 
1222
        mov     dx, cx   ; read
1223
        in      al, dx
1224
        and     eax, 0xff
1225
        and     [esp+32], dword 0
1226
        mov     [esp+20], eax
1227
        ret
1228
 
1229
display_number:
1230
;It is not optimization
1231
        mov     eax, ebx
1232
        mov     ebx, ecx
1233
        mov     ecx, edx
1234
        mov     edx, esi
1235
        mov     esi, edi
1236
; eax = print type, al=0 -> ebx is number
1237
;                   al=1 -> ebx is pointer
1238
;                   ah=0 -> display decimal
1239
;                   ah=1 -> display hexadecimal
1240
;                   ah=2 -> display binary
1241
;                   eax bits 16-21 = number of digits to display (0-32)
1242
;                   eax bits 22-31 = reserved
1243
;
1244
; ebx = number or pointer
1245
; ecx = x shl 16 + y
1246
; edx = color
1247
        xor     edi, edi
1248
display_number_force:
1249
        push    eax
1250
        and     eax, 0x3fffffff
1251
        cmp     eax, 0xffff     ; length > 0 ?
1252
        pop     eax
1253
        jge     cont_displ
1254
        ret
1255
   cont_displ:
1256
        push    eax
1257
        and     eax, 0x3fffffff
1258
        cmp     eax, 61*0x10000  ; length <= 60 ?
1259
        pop     eax
1260
        jb      cont_displ2
1261
        ret
1262
   cont_displ2:
1263
 
1264
        pushad
1265
 
1266
        cmp     al, 1            ; ecx is a pointer ?
1267
        jne     displnl1
1268
        mov     ebp, ebx
1269
        add     ebp, 4
1270
        mov     ebp, [ebp+std_application_base_address]
1271
        mov     ebx, [ebx+std_application_base_address]
1272
 displnl1:
1273
        sub     esp, 64
1274
 
1275
        test    ah, ah            ; DECIMAL
1276
        jnz     no_display_desnum
1277
        shr     eax, 16
1278
        and     eax, 0xC03f
1279
;     and   eax,0x3f
1280
        push    eax
1281
        and     eax, 0x3f
1282
        mov     edi, esp
1283
        add     edi, 4+64-1
1284
        mov     ecx, eax
1285
        mov     eax, ebx
1286
        mov     ebx, 10
1287
 d_desnum:
1288
        xor     edx, edx
1289
        call    division_64_bits
1290
        div     ebx
1291
        add     dl, 48
1292
        mov     [edi], dl
1293
        dec     edi
1294
        loop    d_desnum
1295
        pop     eax
1296
        call    normalize_number
1297
        call    draw_num_text
1298
        add     esp, 64
1299
        popad
1300
        ret
1301
   no_display_desnum:
1302
 
1303
        cmp     ah, 0x01         ; HEXADECIMAL
1304
        jne     no_display_hexnum
1305
        shr     eax, 16
1306
        and     eax, 0xC03f
1307
;     and   eax,0x3f
1308
        push    eax
1309
        and     eax, 0x3f
1310
        mov     edi, esp
1311
        add     edi, 4+64-1
1312
        mov     ecx, eax
1313
        mov     eax, ebx
1314
        mov     ebx, 16
1315
   d_hexnum:
1316
        xor     edx, edx
1317
        call    division_64_bits
1318
        div     ebx
1319
   hexletters = __fdo_hexdigits
1320
        add     edx, hexletters
1321
        mov     dl, [edx]
1322
        mov     [edi], dl
1323
        dec     edi
1324
        loop    d_hexnum
1325
        pop     eax
1326
        call    normalize_number
1327
        call    draw_num_text
1328
        add     esp, 64
1329
        popad
1330
        ret
1331
   no_display_hexnum:
1332
 
1333
        cmp     ah, 0x02         ; BINARY
1334
        jne     no_display_binnum
1335
        shr     eax, 16
1336
        and     eax, 0xC03f
1337
;     and   eax,0x3f
1338
        push    eax
1339
        and     eax, 0x3f
1340
        mov     edi, esp
1341
        add     edi, 4+64-1
1342
        mov     ecx, eax
1343
        mov     eax, ebx
1344
        mov     ebx, 2
1345
   d_binnum:
1346
        xor     edx, edx
1347
        call    division_64_bits
1348
        div     ebx
1349
        add     dl, 48
1350
        mov     [edi], dl
1351
        dec     edi
1352
        loop    d_binnum
1353
        pop     eax
1354
        call    normalize_number
1355
        call    draw_num_text
1356
        add     esp, 64
1357
        popad
1358
        ret
1359
   no_display_binnum:
1360
 
1361
        add     esp, 64
1362
        popad
1363
        ret
1364
 
1365
normalize_number:
1366
        test    ah, 0x80
1367
        jz      .continue
1368
        mov     ecx, 48
1369
        and     eax, 0x3f
1370
@@:
1371
        inc     edi
1372
        cmp     [edi], cl
1373
        jne     .continue
1374
        dec     eax
1375
        cmp     eax, 1
1376
        ja      @r
1377
        mov     al, 1
1378
.continue:
1379
        and     eax, 0x3f
1380
        ret
1381
 
1382
division_64_bits:
1383
        test    [esp+1+4], byte 0x40
1384
        jz      .continue
1385
        push    eax
1386
        mov     eax, ebp
1387
        div     ebx
1388
        mov     ebp, eax
1389
        pop     eax
1390
.continue:
1391
        ret
1392
 
1393
draw_num_text:
1394
        mov     esi, eax
1395
        mov     edx, 64+4
1396
        sub     edx, eax
1397
        add     edx, esp
1398
        mov     ebx, [esp+64+32-8+4]
1399
; add window start x & y
1400
        mov     ecx, [TASK_BASE]
1401
 
1402
        mov     edi, [CURRENT_TASK]
1403
        shl     edi, 8
1404
 
1405
        mov     eax, [ecx-twdw+WDATA.box.left]
1406
        add     eax, [edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
1407
        shl     eax, 16
1408
        add     eax, [ecx-twdw+WDATA.box.top]
1409
        add     eax, [edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
1410
        add     ebx, eax
1411
        mov     ecx, [esp+64+32-12+4]
1412
        and     ecx, not 0x80000000     ; force counted string
1413
        mov     eax, [esp+64+8]         ; background color (if given)
1414
        mov     edi, [esp+64+4]
1415
        jmp     dtext
1416
 
1417
align 4
1418
 
1419
sys_setup:
1420
 
1421
; 1=roland mpu midi base , base io address
1422
; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
1423
; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
1424
; 5=system language, 1eng 2fi 3ger 4rus
1425
; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
1426
; 8=fat32 partition in hd
1427
; 9
1428
; 10 = sound dma channel
1429
; 11 = enable lba read
1430
; 12 = enable pci access
1431
 
1432
 
1433
        and     [esp+32], dword 0
1434
        dec     ebx                             ; MIDI
1435
        jnz     nsyse1
1436
        cmp     ecx, 0x100
1437
 
1438
        jb      nsyse1
1439
        mov     esi, 65535
1440
        cmp     esi, ecx
1441
 
1442
        jb      nsyse1
1443
        mov     [midi_base], cx ;bx
1444
        mov     word [mididp], cx;bx
1445
        inc     cx              ;bx
1446
        mov     word [midisp], cx;bx
1447
        ret
1448
 
1449
iglobal
1450
midi_base dw 0
1451
endg
1452
 
1453
   nsyse1:
1454
        dec     ebx                              ; KEYBOARD
1455
        jnz     nsyse2
1456
        mov     edi, [TASK_BASE]
1457
        mov     eax, [edi+TASKDATA.mem_start]
1458
        add     eax, edx
1459
 
1460
        dec     ecx
1461
        jnz     kbnobase
1462
        mov     ebx, keymap
1463
        mov     ecx, 128
1464
        call    memmove
1465
        ret
1466
   kbnobase:
1467
        dec     ecx
1468
        jnz     kbnoshift
1469
 
1470
        mov     ebx, keymap_shift
1471
        mov     ecx, 128
1472
        call    memmove
1473
        ret
1474
   kbnoshift:
1475
        dec     ecx
1476
        jnz     kbnoalt
1477
        mov     ebx, keymap_alt
1478
        mov     ecx, 128
1479
        call    memmove
1480
        ret
1481
   kbnoalt:
1482
        sub     ecx, 6
1483
        jnz     kbnocountry
1484
        mov     word [keyboard], dx
1485
        ret
1486
   kbnocountry:
1487
        mov     [esp+32], dword 1
1488
        ret
1489
   nsyse2:
1490
        dec     ebx                         ; CD
1491
        jnz     nsyse4
1492
 
1493
        test    ecx, ecx
1494
        jz      nosesl
1495
 
1496
        cmp     ecx, 4
1497
        ja      nosesl
1498
        mov     [cd_base], cl
1499
 
1500
        dec     ecx
1501
        jnz     noprma
1502
        mov     [cdbase], 0x1f0
1503
        mov     [cdid], 0xa0
1504
   noprma:
1505
 
1506
        dec     ecx
1507
        jnz     noprsl
1508
        mov     [cdbase], 0x1f0
1509
        mov     [cdid], 0xb0
1510
   noprsl:
1511
        dec     ecx
1512
        jnz     nosema
1513
        mov     [cdbase], 0x170
1514
        mov     [cdid], 0xa0
1515
   nosema:
1516
        dec     ecx
1517
        jnz     nosesl
1518
        mov     [cdbase], 0x170
1519
        mov     [cdid], 0xb0
1520
   nosesl:
1521
        ret
1522
 
1523
iglobal
1524
cd_base db 0
1525
 
1526
endg
1527
   nsyse4:
1528
 
1529
        sub     ebx, 2           ; SYSTEM LANGUAGE
1530
        jnz     nsyse5
1531
        mov     [syslang], ecx
1532
        ret
1533
   nsyse5:
1534
 
1535
        sub     ebx, 2          ; HD BASE
1536
        jnz     nsyse7
1537
 
1538
        test    ecx, ecx
1539
        jz      nosethd
1540
 
1541
        cmp     ecx, 4
1542
        ja      nosethd
1543
        mov     [hd_base], cl
1544
 
1545
        cmp     ecx, 1
1546
        jnz     noprmahd
1547
        mov     [hdbase], 0x1f0
1548
        and     dword [hdid], 0x0
1549
        mov     dword [hdpos], ecx
1550
;     call set_FAT32_variables
1551
   noprmahd:
1552
 
1553
        cmp     ecx, 2
1554
        jnz     noprslhd
1555
        mov     [hdbase], 0x1f0
1556
        mov     [hdid], 0x10
1557
        mov     dword [hdpos], ecx
1558
;     call set_FAT32_variables
1559
   noprslhd:
1560
 
1561
        cmp     ecx, 3
1562
        jnz     nosemahd
1563
        mov     [hdbase], 0x170
1564
        and     dword [hdid], 0x0
1565
        mov     dword [hdpos], ecx
1566
;     call set_FAT32_variables
1567
   nosemahd:
1568
 
1569
        cmp     ecx, 4
1570
        jnz     noseslhd
1571
        mov     [hdbase], 0x170
1572
        mov     [hdid], 0x10
1573
        mov     dword [hdpos], ecx
1574
;     call set_FAT32_variables
1575
   noseslhd:
1576
        call    reserve_hd1
1577
        call    reserve_hd_channel
1578
        call    free_hd_channel
1579
        and     dword [hd1_status], 0     ; free
1580
   nosethd:
1581
        ret
1582
 
1583
iglobal
1584
hd_base db 0
1585
endg
1586
 
1587
nsyse7:
1588
 
1589
;     cmp  eax,8                      ; HD PARTITION
1590
        dec     ebx
1591
        jnz     nsyse8
1592
        mov     [fat32part], ecx
1593
;     call set_FAT32_variables
1594
        call    reserve_hd1
1595
        call    reserve_hd_channel
1596
        call    free_hd_channel
1597
;       pusha
1598
        call    choice_necessity_partition_1
1599
;       popa
1600
        and     dword [hd1_status], 0   ; free
1601
        ret
1602
 
1603
nsyse8:
1604
;     cmp  eax,11                     ; ENABLE LBA READ
1605
        and     ecx, 1
1606
        sub     ebx, 3
1607
        jnz     no_set_lba_read
1608
        mov     [lba_read_enabled], ecx
1609
        ret
1610
 
1611
no_set_lba_read:
1612
;     cmp  eax,12                     ; ENABLE PCI ACCESS
1613
        dec     ebx
1614
        jnz     sys_setup_err
1615
        mov     [pci_access_enabled], ecx
1616
        ret
1617
 
1618
sys_setup_err:
1619
        or      [esp+32], dword -1
1620
        ret
1621
 
1622
align 4
1623
 
1624
sys_getsetup:
1625
 
1626
; 1=roland mpu midi base , base io address
1627
; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
1628
; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
1629
; 5=system language, 1eng 2fi 3ger 4rus
1630
; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
1631
; 8=fat32 partition in hd
1632
; 9=get hs timer tic
1633
 
1634
;     cmp  eax,1
1635
        dec     ebx
1636
        jnz     ngsyse1
1637
        movzx   eax, [midi_base]
1638
        mov     [esp+32], eax
1639
        ret
1640
ngsyse1:
1641
;     cmp  eax,2
1642
        dec     ebx
1643
        jnz     ngsyse2
1644
 
1645
        mov     edi, [TASK_BASE]
1646
        mov     ebx, [edi+TASKDATA.mem_start]
1647
        add     ebx, edx
1648
 
1649
;     cmp  ebx,1
1650
        dec     ecx
1651
        jnz     kbnobaseret
1652
        mov     eax, keymap
1653
        mov     ecx, 128
1654
        call    memmove
1655
        ret
1656
kbnobaseret:
1657
;     cmp  ebx,2
1658
        dec     ecx
1659
        jnz     kbnoshiftret
1660
 
1661
        mov     eax, keymap_shift
1662
        mov     ecx, 128
1663
        call    memmove
1664
        ret
1665
kbnoshiftret:
1666
;     cmp  ebx,3
1667
        dec     ecx
1668
        jne     kbnoaltret
1669
 
1670
        mov     eax, keymap_alt
1671
        mov     ecx, 128
1672
        call    memmove
1673
        ret
1674
kbnoaltret:
1675
;     cmp  ebx,9
1676
        sub     ecx, 6
1677
        jnz     ngsyse2
1678
        movzx   eax, word [keyboard]
1679
        mov     [esp+32], eax
1680
        ret
1681
 
1682
 
1683
ngsyse2:
1684
;         cmp  eax,3
1685
        dec     ebx
1686
        jnz     ngsyse3
1687
        movzx   eax, [cd_base]
1688
        mov     [esp+32], eax
1689
        ret
1690
ngsyse3:
1691
;         cmp  eax,5
1692
        sub     ebx, 2
1693
        jnz     ngsyse5
1694
        mov     eax, [syslang]
1695
        mov     [esp+32], eax
1696
        ret
1697
ngsyse5:
1698
;     cmp  eax,7
1699
        sub     ebx, 2
1700
        jnz     ngsyse7
1701
        movzx   eax, [hd_base]
1702
        mov     [esp+32], eax
1703
        ret
1704
ngsyse7:
1705
;     cmp  eax,8
1706
        dec     ebx
1707
        jnz     ngsyse8
1708
        mov     eax, [fat32part]
1709
        mov     [esp+32], eax
1710
        ret
1711
ngsyse8:
1712
;     cmp  eax,9
1713
        dec     ebx
1714
        jnz     ngsyse9
1715
        mov     eax, [timer_ticks];[0xfdf0]
1716
        mov     [esp+32], eax
1717
        ret
1718
ngsyse9:
1719
;     cmp  eax,11
1720
        sub     ebx, 2
1721
        jnz     ngsyse11
1722
        mov     eax, [lba_read_enabled]
1723
        mov     [esp+32], eax
1724
        ret
1725
ngsyse11:
1726
;     cmp  eax,12
1727
        dec     ebx
1728
        jnz     ngsyse12
1729
        mov     eax, [pci_access_enabled]
1730
        mov     [esp+32], eax
1731
        ret
1732
ngsyse12:
1733
        mov     [esp+32], dword 1
1734
        ret
1735
 
1736
 
1737
get_timer_ticks:
1738
        mov     eax, [timer_ticks]
1739
        ret
1740
 
1741
iglobal
1742
align 4
1743
mousefn dd msscreen, mswin, msbutton, msset
1744
        dd app_load_cursor
1745
        dd app_set_cursor
1746
        dd app_delete_cursor
1747
        dd msz
1748
endg
1749
 
1750
readmousepos:
1751
 
1752
; eax=0 screen relative
1753
; eax=1 window relative
1754
; eax=2 buttons pressed
1755
; eax=3 set mouse pos   ; reserved
1756
; eax=4 load cursor
1757
; eax=5 set cursor
1758
; eax=6 delete cursor   ; reserved
1759
; eax=7 get mouse_z
1760
 
1761
        cmp     ebx, 7
1762
        ja      msset
1763
        jmp     [mousefn+ebx*4]
1764
msscreen:
1765
        mov     eax, [MOUSE_X]
1766
        shl     eax, 16
1767
        mov     ax, [MOUSE_Y]
1768
        mov     [esp+36-4], eax
1769
        ret
1770
mswin:
1771
        mov     eax, [MOUSE_X]
1772
        shl     eax, 16
1773
        mov     ax, [MOUSE_Y]
1774
        mov     esi, [TASK_BASE]
1775
        mov     bx, word [esi-twdw+WDATA.box.left]
1776
        shl     ebx, 16
1777
        mov     bx, word [esi-twdw+WDATA.box.top]
1778
        sub     eax, ebx
1779
 
1780
        mov     edi, [CURRENT_TASK]
1781
        shl     edi, 8
1782
        sub     ax, word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
1783
        rol     eax, 16
1784
        sub     ax, word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
1785
        rol     eax, 16
1786
        mov     [esp+36-4], eax
1787
        ret
1788
msbutton:
1789
        movzx   eax, byte [BTN_DOWN]
1790
        mov     [esp+36-4], eax
1791
        ret
1792
msz:
1793
        mov     edi, [TASK_COUNT]
1794
        movzx   edi, word [WIN_POS + edi*2]
1795
        cmp     edi, [CURRENT_TASK]
1796
        jne     @f
1797
        mov     ax, [MOUSE_SCROLL_H]
1798
        shl     eax, 16
1799
        mov     ax, [MOUSE_SCROLL_V]
1800
        mov     [esp+36-4], eax
1801
        and     [MOUSE_SCROLL_H], word 0
1802
        and     [MOUSE_SCROLL_V], word 0
1803
        ret
1804
       @@:
1805
        and     [esp+36-4], dword 0
1806
;           ret
1807
msset:
1808
        ret
1809
 
1810
app_load_cursor:
1811
        cmp     ecx, OS_BASE
1812
        jae     msset
1813
        stdcall load_cursor, ecx, edx
1814
        mov     [esp+36-4], eax
1815
        ret
1816
 
1817
app_set_cursor:
1818
        stdcall set_cursor, ecx
1819
        mov     [esp+36-4], eax
1820
        ret
1821
 
1822
app_delete_cursor:
1823
        stdcall delete_cursor, ecx
1824
        mov     [esp+36-4], eax
1825
        ret
1826
 
1827
is_input:
1828
 
1829
        push    edx
1830
        mov     dx, word [midisp]
1831
        in      al, dx
1832
        and     al, 0x80
1833
        pop     edx
1834
        ret
1835
 
1836
is_output:
1837
 
1838
        push    edx
1839
        mov     dx, word [midisp]
1840
        in      al, dx
1841
        and     al, 0x40
1842
        pop     edx
1843
        ret
1844
 
1845
 
1846
get_mpu_in:
1847
 
1848
        push    edx
1849
        mov     dx, word [mididp]
1850
        in      al, dx
1851
        pop     edx
1852
        ret
1853
 
1854
 
1855
put_mpu_out:
1856
 
1857
        push    edx
1858
        mov     dx, word [mididp]
1859
        out     dx, al
1860
        pop     edx
1861
        ret
1862
 
1863
 
1864
 
1865
align 4
1866
 
1867
sys_midi:
1868
        cmp     [mididp], 0
1869
        jnz     sm0
1870
        mov     [esp+36], dword 1
1871
        ret
1872
sm0:
1873
        and     [esp+36], dword 0
1874
        dec     ebx
1875
        jnz     smn1
1876
 ;    call setuart
1877
su1:
1878
        call    is_output
1879
        test    al, al
1880
        jnz     su1
1881
        mov     dx, word [midisp]
1882
        mov     al, 0xff
1883
        out     dx, al
1884
su2:
1885
        mov     dx, word [midisp]
1886
        mov     al, 0xff
1887
        out     dx, al
1888
        call    is_input
1889
        test    al, al
1890
        jnz     su2
1891
        call    get_mpu_in
1892
        cmp     al, 0xfe
1893
        jnz     su2
1894
su3:
1895
        call    is_output
1896
        test    al, al
1897
        jnz     su3
1898
        mov     dx, word [midisp]
1899
        mov     al, 0x3f
1900
        out     dx, al
1901
        ret
1902
smn1:
1903
        dec     ebx
1904
        jnz     smn2
1905
sm10:
1906
        call    get_mpu_in
1907
        call    is_output
1908
        test    al, al
1909
        jnz     sm10
1910
        mov     al, bl
1911
        call    put_mpu_out
1912
        smn2:
1913
        ret
1914
 
1915
detect_devices:
1916
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1917
;include 'detect/commouse.inc'
1918
;include 'detect/ps2mouse.inc'
1919
;include 'detect/dev_fd.inc'
1920
;include 'detect/dev_hdcd.inc'
1921
;include 'detect/sear_par.inc'
1922
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1923
        ret
1924
 
1925
sys_end:
2430 mario79 1926
;--------------------------------------
1927
        cmp     [_display.select_cursor], 0
1928
        je      @f
1929
; restore default cursor before killing
1930
        pusha
1931
        mov     ecx, [current_slot]
2435 mario79 1932
        call    restore_default_cursor_before_killing
2430 mario79 1933
        popa
1934
@@:
1935
;--------------------------------------
2288 clevermous 1936
        mov     ecx, [current_slot]
1937
        mov     eax, [ecx+APPDATA.tls_base]
1938
        test    eax, eax
1939
        jz      @F
1940
 
1941
        stdcall user_free, eax
1942
@@:
1943
 
1944
        mov     eax, [TASK_BASE]
1945
        mov     [eax+TASKDATA.state], 3; terminate this program
1946
 
1947
    waitterm:            ; wait here for termination
1948
        mov     ebx, 100
1949
        call    delay_hs
1950
        jmp     waitterm
2435 mario79 1951
;------------------------------------------------------------------------------
1952
restore_default_cursor_before_killing:
1953
        mov     eax, [def_cursor]
1954
        mov     [ecx+APPDATA.cursor], eax
2288 clevermous 1955
 
2435 mario79 1956
        movzx   eax, word [MOUSE_Y]
1957
        movzx   ebx, word [MOUSE_X]
2446 mario79 1958
;        mov     ecx, [Screen_Max_X]
1959
;        inc     ecx
1960
;        mul     ecx
1961
        mov     eax, [d_width_calc_area + eax*4]
1962
 
2435 mario79 1963
        add     eax, [_WinMapAddress]
1964
        movzx   edx, byte [ebx+eax]
1965
        shl     edx, 8
1966
        mov     esi, [edx+SLOT_BASE+APPDATA.cursor]
1967
        push    esi
1968
        call    [_display.select_cursor]
1969
        mov     [current_cursor], esi
1970
        ret
1971
;------------------------------------------------------------------------------
2288 clevermous 1972
iglobal
1973
align 4
1974
sys_system_table:
1975
        dd      sysfn_deactivate        ; 1 = deactivate window
1976
        dd      sysfn_terminate         ; 2 = terminate thread
1977
        dd      sysfn_activate          ; 3 = activate window
1978
        dd      sysfn_getidletime       ; 4 = get idle time
1979
        dd      sysfn_getcpuclock       ; 5 = get cpu clock
1980
        dd      sysfn_saveramdisk       ; 6 = save ramdisk
1981
        dd      sysfn_getactive         ; 7 = get active window
1982
        dd      sysfn_sound_flag        ; 8 = get/set sound_flag
1983
        dd      sysfn_shutdown          ; 9 = shutdown with parameter
1984
        dd      sysfn_minimize          ; 10 = minimize window
1985
        dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
1986
        dd      sysfn_lastkey           ; 12 = get last pressed key
1987
        dd      sysfn_getversion        ; 13 = get kernel version
1988
        dd      sysfn_waitretrace       ; 14 = wait retrace
1989
        dd      sysfn_centermouse       ; 15 = center mouse cursor
1990
        dd      sysfn_getfreemem        ; 16 = get free memory size
1991
        dd      sysfn_getallmem         ; 17 = get total memory size
1992
        dd      sysfn_terminate2        ; 18 = terminate thread using PID
1993
                                        ;                 instead of slot
1994
        dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
1995
        dd      sysfn_meminfo           ; 20 = get extended memory info
1996
        dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
1997
        dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
1998
sysfn_num = ($ - sys_system_table)/4
1999
endg
2000
;------------------------------------------------------------------------------
2001
sys_system:
2002
        dec     ebx
2003
        cmp     ebx, sysfn_num
2004
        jae     @f
2005
        jmp     dword [sys_system_table + ebx*4]
2006
@@:
2007
        ret
2008
;------------------------------------------------------------------------------
2009
sysfn_shutdown:          ; 18.9 = system shutdown
2010
        cmp     ecx, 1
2011
        jl      exit_for_anyone
2012
        cmp     ecx, 4
2013
        jg      exit_for_anyone
2014
        mov     [BOOT_VAR+0x9030], cl
2015
 
2016
        mov     eax, [TASK_COUNT]
2017
        mov     [SYS_SHUTDOWN], al
2018
        mov     [shutdown_processes], eax
2019
        and     dword [esp+32], 0
2020
 exit_for_anyone:
2021
        ret
2022
  uglobal
2023
   shutdown_processes:
2024
                       dd 0x0
2025
  endg
2026
;------------------------------------------------------------------------------
2027
sysfn_terminate:        ; 18.2 = TERMINATE
2028
        cmp     ecx, 2
2029
        jb      noprocessterminate
2030
        mov     edx, [TASK_COUNT]
2031
        cmp     ecx, edx
2032
        ja      noprocessterminate
2033
        mov     eax, [TASK_COUNT]
2430 mario79 2034
        push    ecx
2288 clevermous 2035
        shl     ecx, 5
2036
        mov     edx, [ecx+CURRENT_TASK+TASKDATA.pid]
2037
        add     ecx, CURRENT_TASK+TASKDATA.state
2038
        cmp     byte [ecx], 9
2039
        jz      noprocessterminate
2430 mario79 2040
;--------------------------------------
2041
        cmp     [_display.select_cursor], 0
2042
        je      @f
2043
; restore default cursor before killing
2044
        pusha
2045
        mov     ecx, [esp+32]
2046
        shl     ecx, 8
2435 mario79 2047
        add     ecx, SLOT_BASE
2048
        call    restore_default_cursor_before_killing
2430 mario79 2049
        popa
2050
@@:
2051
        add     esp, 4
2052
;--------------------------------------
2288 clevermous 2053
     ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
2054
        mov     [ecx], byte 3; clear possible i40's
2055
     ;call MEM_Heap_UnLock
2056
 
2057
        cmp     edx, [application_table_status]; clear app table stat
2058
        jne     noatsc
2059
        and     [application_table_status], 0
2060
   noatsc:
2061
   noprocessterminate:
2062
        ret
2063
;------------------------------------------------------------------------------
2064
sysfn_terminate2:
2065
;lock application_table_status mutex
2066
.table_status:
2067
        cli
2068
        cmp     [application_table_status], 0
2069
        je      .stf
2070
        sti
2071
        call    change_task
2072
        jmp     .table_status
2073
.stf:
2074
        call    set_application_table_status
2075
        mov     eax, ecx
2076
        call    pid_to_slot
2077
        test    eax, eax
2078
        jz      .not_found
2079
        mov     ecx, eax
2080
        cli
2081
        call    sysfn_terminate
2082
        and     [application_table_status], 0
2083
        sti
2084
        and     dword [esp+32], 0
2085
        ret
2086
.not_found:
2087
        mov     [application_table_status], 0
2088
        or      dword [esp+32], -1
2089
        ret
2090
;------------------------------------------------------------------------------
2091
sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
2092
        cmp     ecx, 2
2093
        jb      .nowindowdeactivate
2094
        cmp     ecx, [TASK_COUNT]
2095
        ja      .nowindowdeactivate
2408 Serge 2096
 
2288 clevermous 2097
        movzx   esi, word [WIN_STACK + ecx*2]
2098
        cmp     esi, 1
2099
        je      .nowindowdeactivate ; already deactive
2100
 
2101
        mov     edi, ecx
2102
        shl     edi, 5
2103
        add     edi, window_data
2104
        movzx   esi, word [WIN_STACK + ecx * 2]
2105
        lea     esi, [WIN_POS + esi * 2]
2106
        call    window._.window_deactivate
2408 Serge 2107
 
2288 clevermous 2108
        xor     eax, eax
2109
        mov     byte[MOUSE_BACKGROUND], al
2110
        mov     byte[DONT_DRAW_MOUSE], al
2111
        mov     byte[MOUSE_DOWN], 0
2112
 
2113
        call    syscall_display_settings._.calculate_whole_screen
2114
        call    syscall_display_settings._.redraw_whole_screen
2115
.nowindowdeactivate:
2116
        ret
2411 Serge 2117
 ;------------------------------------------------------------------------------
2288 clevermous 2118
sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
2119
        cmp     ecx, 2
2120
        jb      .nowindowactivate
2121
        cmp     ecx, [TASK_COUNT]
2122
        ja      .nowindowactivate
2123
 
2124
        mov     [window_minimize], 2; restore window if minimized
2125
 
2126
        movzx   esi, word [WIN_STACK + ecx*2]
2127
        cmp     esi, [TASK_COUNT]
2128
        je      .nowindowactivate; already active
2129
 
2130
        mov     edi, ecx
2131
        shl     edi, 5
2132
        add     edi, window_data
2133
        movzx   esi, word [WIN_STACK + ecx * 2]
2134
        lea     esi, [WIN_POS + esi * 2]
2135
        call    waredraw
2136
.nowindowactivate:
2137
        ret
2138
;------------------------------------------------------------------------------
2139
sysfn_getidletime:              ; 18.4 = GET IDLETIME
2140
        mov     eax, [idleusesec]
2141
        mov     [esp+32], eax
2142
        ret
2143
;------------------------------------------------------------------------------
2144
sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
2145
        mov     eax, [CPU_FREQ]
2146
        mov     [esp+32], eax
2147
        ret
2148
;------------------------------------------------------------------------------
2149
;  SAVE ramdisk to /hd/1/menuet.img
2150
;!!!!!!!!!!!!!!!!!!!!!!!!
2151
   include 'blkdev/rdsave.inc'
2152
;!!!!!!!!!!!!!!!!!!!!!!!!
2153
;------------------------------------------------------------------------------
2154
align 4
2155
sysfn_getactive:        ; 18.7 = get active window
2156
        mov     eax, [TASK_COUNT]
2157
        movzx   eax, word [WIN_POS + eax*2]
2158
        mov     [esp+32], eax
2159
        ret
2160
;------------------------------------------------------------------------------
2161
sysfn_sound_flag:       ; 18.8 = get/set sound_flag
2162
;     cmp  ecx,1
2163
        dec     ecx
2164
        jnz     nogetsoundflag
2165
        movzx   eax, byte [sound_flag]; get sound_flag
2166
        mov     [esp+32], eax
2167
        ret
2168
 nogetsoundflag:
2169
;     cmp  ecx,2
2170
        dec     ecx
2171
        jnz     nosoundflag
2172
        xor     byte [sound_flag], 1
2173
 nosoundflag:
2174
        ret
2175
;------------------------------------------------------------------------------
2176
sysfn_minimize:         ; 18.10 = minimize window
2177
        mov     [window_minimize], 1
2178
        ret
2179
;------------------------------------------------------------------------------
2180
align 4
2181
sysfn_getdiskinfo:      ; 18.11 = get disk info table
2182
;     cmp  ecx,1
2183
        dec     ecx
2184
        jnz     full_table
2185
  small_table:
2186
        call    for_all_tables
2187
        mov     ecx, 10
2188
        cld
2189
        rep movsb
2190
        ret
2191
   for_all_tables:
2192
        mov     edi, edx
2193
        mov     esi, DRIVE_DATA
2194
        ret
2195
  full_table:
2196
;     cmp  ecx,2
2197
        dec     ecx
2198
        jnz     exit_for_anyone
2199
        call    for_all_tables
2200
        mov     ecx, 16384
2201
        cld
2202
        rep movsd
2203
        ret
2204
;------------------------------------------------------------------------------
2205
sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
2206
        and     dword [esp+32], 0
2207
        ret
2208
;------------------------------------------------------------------------------
2209
sysfn_getversion:       ; 18.13 = get kernel ID and version
2210
        mov     edi, ecx
2211
        mov     esi, version_inf
2212
        mov     ecx, version_end-version_inf
2213
        rep movsb
2214
        ret
2215
;------------------------------------------------------------------------------
2216
sysfn_waitretrace:     ; 18.14 = sys wait retrace
2217
     ;wait retrace functions
2218
 sys_wait_retrace:
2219
        mov     edx, 0x3da
2220
 WaitRetrace_loop:
2221
        in      al, dx
2222
        test    al, 1000b
2223
        jz      WaitRetrace_loop
2224
        and     [esp+32], dword 0
2225
        ret
2226
;------------------------------------------------------------------------------
2227
align 4
2228
sysfn_centermouse:      ; 18.15 = mouse centered
2229
; removed here by 
2230
;     call  mouse_centered
2231
;* mouse centered - start code- Mario79
2232
;mouse_centered:
2233
;        push  eax
2234
        mov     eax, [Screen_Max_X]
2235
        shr     eax, 1
2236
        mov     [MOUSE_X], ax
2237
        mov     eax, [Screen_Max_Y]
2238
        shr     eax, 1
2239
        mov     [MOUSE_Y], ax
2240
;        ret
2241
;* mouse centered - end code- Mario79
2242
        xor     eax, eax
2243
        and     [esp+32], eax
2244
;        pop   eax
2245
        ret
2246
;------------------------------------------------------------------------------
2247
align 4
2248
sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
2249
        test    ecx, ecx; get mouse speed factor
2250
        jnz     .set_mouse_acceleration
2251
        xor     eax, eax
2252
        mov     ax, [mouse_speed_factor]
2253
        mov     [esp+32], eax
2254
        ret
2255
 .set_mouse_acceleration:
2256
;     cmp  ecx,1  ; set mouse speed factor
2257
        dec     ecx
2258
        jnz     .get_mouse_delay
2259
        mov     [mouse_speed_factor], dx
2260
        ret
2261
 .get_mouse_delay:
2262
;     cmp  ecx,2  ; get mouse delay
2263
        dec     ecx
2264
        jnz     .set_mouse_delay
2265
        mov     eax, [mouse_delay]
2266
        mov     [esp+32], eax
2267
        ret
2268
 .set_mouse_delay:
2269
;     cmp  ecx,3  ; set mouse delay
2270
        dec     ecx
2271
        jnz     .set_pointer_position
2272
        mov     [mouse_delay], edx
2273
        ret
2274
 .set_pointer_position:
2275
;     cmp  ecx,4  ; set mouse pointer position
2276
        dec     ecx
2277
        jnz     .set_mouse_button
2278
        cmp     dx, word[Screen_Max_Y]
2279
        ja      .end
2280
        rol     edx, 16
2281
        cmp     dx, word[Screen_Max_X]
2282
        ja      .end
2283
        mov     [MOUSE_X], edx
2284
        ret
2285
 .set_mouse_button:
2286
;     cmp   ecx,5  ; set mouse button features
2287
        dec     ecx
2288
        jnz     .end
2289
        mov     [BTN_DOWN], dl
2290
        mov     [mouse_active], 1
2291
 .end:
2292
        ret
2293
;------------------------------------------------------------------------------
2294
sysfn_getfreemem:
2295
        mov     eax, [pg_data.pages_free]
2296
        shl     eax, 2
2297
        mov     [esp+32], eax
2298
        ret
2299
 
2300
sysfn_getallmem:
2301
        mov     eax, [MEM_AMOUNT]
2302
        shr     eax, 10
2303
        mov     [esp+32], eax
2304
        ret
2305
 
2306
; // Alver, 2007-22-08 // {
2307
sysfn_pid_to_slot:
2308
        mov     eax, ecx
2309
        call    pid_to_slot
2310
        mov     [esp+32], eax
2311
        ret
2312
 
2313
sysfn_min_rest_window:
2314
        pushad
2315
        mov     eax, edx ; ebx - operating
2316
        shr     ecx, 1
2317
        jnc     @f
2318
        call    pid_to_slot
2319
@@:
2320
        or      eax, eax ; eax - number of slot
2321
        jz      .error
2322
        cmp     eax, 255    ; varify maximal slot number
2323
        ja      .error
2324
        movzx   eax, word [WIN_STACK + eax*2]
2325
        shr     ecx, 1
2326
        jc      .restore
2327
 ; .minimize:
2328
        call    minimize_window
2329
        jmp     .exit
2330
.restore:
2331
        call    restore_minimized_window
2332
.exit:
2333
        popad
2334
        xor     eax, eax
2335
        mov     [esp+32], eax
2336
        ret
2337
.error:
2338
        popad
2339
        xor     eax, eax
2340
        dec     eax
2341
        mov     [esp+32], eax
2342
        ret
2343
; } \\ Alver, 2007-22-08 \\
2344
 
2345
uglobal
2346
;// mike.dld, 2006-29-01 [
2347
screen_workarea RECT
2348
;// mike.dld, 2006-29-01 ]
2349
window_minimize db 0
2350
sound_flag      db 0
2351
endg
2352
 
2353
UID_NONE=0
2354
UID_MENUETOS=1   ;official
2355
UID_KOLIBRI=2    ;russian
2356
 
2357
iglobal
2358
version_inf:
2359
  db 0,7,7,0  ; version 0.7.7.0
2360
  db 0
2361
  dd __REV__
2362
version_end:
2363
endg
2364
 
2365
sys_cachetodiskette:
2366
        cmp     ebx, 1
2367
        jne     .no_floppy_a_save
2368
        mov     [flp_number], 1
2369
        jmp     .save_image_on_floppy
2370
.no_floppy_a_save:
2371
        cmp     ebx, 2
2372
        jne     .no_floppy_b_save
2373
        mov     [flp_number], 2
2374
.save_image_on_floppy:
2375
        call    save_image
2376
        mov     [esp + 32], dword 0
2377
        cmp     [FDC_Status], 0
2378
        je      .yes_floppy_save
2379
.no_floppy_b_save:
2380
        mov     [esp + 32], dword 1
2381
.yes_floppy_save:
2382
        ret
2383
 
2384
uglobal
2385
;  bgrchanged  dd  0x0
2386
align 4
2387
bgrlockpid dd 0
2388
bgrlock db 0
2389
endg
2390
 
2391
sys_background:
2392
 
2393
        cmp     ebx, 1                     ; BACKGROUND SIZE
2394
        jnz     nosb1
2395
        test    ecx, ecx
2396
;    cmp   ecx,0
2397
        jz      sbgrr
2398
        test    edx, edx
2399
;    cmp   edx,0
2400
        jz      sbgrr
2401
@@:
2402
;;Maxis use atomic bts for mutexes  4.4.2009
2403
        bts     dword [bgrlock], 0
2404
        jnc     @f
2405
        call    change_task
2406
        jmp     @b
2407
@@:
2408
        mov     [BgrDataWidth], ecx
2409
        mov     [BgrDataHeight], edx
2410
;    mov   [bgrchanged],1
2411
 
2412
        pushad
2413
; return memory for old background
2414
        mov     eax, [img_background]
2415
        cmp     eax, static_background_data
2416
        jz      @f
2417
        stdcall kernel_free, eax
2418
@@:
2419
; calculate RAW size
2420
        xor     eax, eax
2421
        inc     eax
2422
        cmp     [BgrDataWidth], eax
2423
        jae     @f
2424
        mov     [BgrDataWidth], eax
2425
@@:
2426
        cmp     [BgrDataHeight], eax
2427
        jae     @f
2428
        mov     [BgrDataHeight], eax
2429
@@:
2430
        mov     eax, [BgrDataWidth]
2431
        imul    eax, [BgrDataHeight]
2432
        lea     eax, [eax*3]
2433
        mov     [mem_BACKGROUND], eax
2434
; get memory for new background
2435
        stdcall kernel_alloc, eax
2436
        test    eax, eax
2437
        jz      .memfailed
2438
        mov     [img_background], eax
2439
        jmp     .exit
2440
.memfailed:
2441
; revert to static monotone data
2442
        mov     [img_background], static_background_data
2443
        xor     eax, eax
2444
        inc     eax
2445
        mov     [BgrDataWidth], eax
2446
        mov     [BgrDataHeight], eax
2447
        mov     [mem_BACKGROUND], 4
2448
.exit:
2449
        popad
2450
        mov     [bgrlock], 0
2451
 
2452
  sbgrr:
2453
        ret
2454
 
2455
nosb1:
2456
 
2457
        cmp     ebx, 2                     ; SET PIXEL
2458
        jnz     nosb2
2459
 
2460
        mov     eax, [img_background]
2461
        test    ecx, ecx
2462
        jz      @f
2463
        cmp     eax, static_background_data
2464
        jz      .ret
2465
@@:
2466
        mov     ebx, [mem_BACKGROUND]
2467
        add     ebx, 4095
2468
        and     ebx, -4096
2469
        sub     ebx, 4
2470
        cmp     ecx, ebx
2471
        ja      .ret
2472
 
2473
        mov     ebx, [eax+ecx]
2474
        and     ebx, 0xFF000000;255*256*256*256
2475
        and     edx, 0x00FFFFFF;255*256*256+255*256+255
2476
        add     edx, ebx
2477
        mov     [eax+ecx], edx
2478
.ret:
2479
        ret
2480
nosb2:
2481
 
2482
        cmp     ebx, 3                     ; DRAW BACKGROUND
2483
        jnz     nosb3
2484
draw_background_temp:
2485
;    cmp   [bgrchanged],1 ;0
2486
;    je    nosb31
2487
;draw_background_temp:
2488
;    mov   [bgrchanged],1 ;0
2489
        mov     [background_defined], 1
2490
        mov     byte[BACKGROUND_CHANGED], 1
2491
        call    force_redraw_background
2492
   nosb31:
2493
        ret
2494
  nosb3:
2495
 
2496
        cmp     ebx, 4                     ; TILED / STRETCHED
2497
        jnz     nosb4
2498
        cmp     ecx, [BgrDrawMode]
2499
        je      nosb41
2500
        mov     [BgrDrawMode], ecx
2501
;    mov   [bgrchanged],1
2502
   nosb41:
2503
        ret
2504
  nosb4:
2505
 
2506
        cmp     ebx, 5                     ; BLOCK MOVE TO BGR
2507
        jnz     nosb5
2508
        cmp     [img_background], static_background_data
2509
        jnz     @f
2510
        test    edx, edx
2511
        jnz     .fin
2512
        cmp     esi, 4
2513
        ja      .fin
2514
  @@:
2515
  ; bughere
2516
        mov     eax, ecx
2517
        mov     ebx, edx
2518
        add     ebx, [img_background];IMG_BACKGROUND
2519
        mov     ecx, esi
2520
        call    memmove
2521
  .fin:
2522
        ret
2523
  nosb5:
2524
 
2525
        cmp     ebx, 6
2526
        jnz     nosb6
2527
;;Maxis use atomic bts for mutex 4.4.2009
2528
@@:
2529
        bts     dword [bgrlock], 0
2530
        jnc     @f
2531
        call    change_task
2532
        jmp     @b
2533
@@:
2534
        mov     eax, [CURRENT_TASK]
2535
        mov     [bgrlockpid], eax
2536
        cmp     [img_background], static_background_data
2537
        jz      .nomem
2538
        stdcall user_alloc, [mem_BACKGROUND]
2539
        mov     [esp+32], eax
2540
        test    eax, eax
2541
        jz      .nomem
2542
        mov     ebx, eax
2543
        shr     ebx, 12
2544
        or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
2545
        mov     esi, [img_background]
2546
        shr     esi, 12
2547
        mov     ecx, [mem_BACKGROUND]
2548
        add     ecx, 0xFFF
2549
        shr     ecx, 12
2550
.z:
2551
        mov     eax, [page_tabs+ebx*4]
2552
        test    al, 1
2553
        jz      @f
2554
        call    free_page
2555
@@:
2556
        mov     eax, [page_tabs+esi*4]
2557
        or      al, PG_UW
2558
        mov     [page_tabs+ebx*4], eax
2559
        mov     eax, ebx
2560
        shl     eax, 12
2561
        invlpg  [eax]
2562
        inc     ebx
2563
        inc     esi
2564
        loop    .z
2565
        ret
2566
.nomem:
2567
        and     [bgrlockpid], 0
2568
        mov     [bgrlock], 0
2569
nosb6:
2570
        cmp     ebx, 7
2571
        jnz     nosb7
2572
        cmp     [bgrlock], 0
2573
        jz      .err
2574
        mov     eax, [CURRENT_TASK]
2575
        cmp     [bgrlockpid], eax
2576
        jnz     .err
2577
        mov     eax, ecx
2578
        mov     ebx, ecx
2579
        shr     eax, 12
2580
        mov     ecx, [page_tabs+(eax-1)*4]
2581
        test    cl, USED_BLOCK+DONT_FREE_BLOCK
2582
        jz      .err
2583
        jnp     .err
2584
        push    eax
2585
        shr     ecx, 12
2586
        dec     ecx
2587
@@:
2588
        and     dword [page_tabs+eax*4], 0
2589
        mov     edx, eax
2590
        shl     edx, 12
2591
        push    eax
2592
        invlpg  [edx]
2593
        pop     eax
2594
        inc     eax
2595
        loop    @b
2596
        pop     eax
2597
        and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
2598
        stdcall user_free, ebx
2599
        mov     [esp+32], eax
2600
        and     [bgrlockpid], 0
2601
        mov     [bgrlock], 0
2602
        ret
2603
.err:
2604
        and     dword [esp+32], 0
2605
        ret
2606
 
2607
nosb7:
2608
        ret
2609
 
2610
force_redraw_background:
2611
        and     [draw_data+32 + RECT.left], 0
2612
        and     [draw_data+32 + RECT.top], 0
2613
        push    eax ebx
2614
        mov     eax, [Screen_Max_X]
2615
        mov     ebx, [Screen_Max_Y]
2616
        mov     [draw_data+32 + RECT.right], eax
2617
        mov     [draw_data+32 + RECT.bottom], ebx
2618
        pop     ebx eax
2619
        inc     byte[REDRAW_BACKGROUND]
2620
        ret
2621
 
2622
align 4
2623
 
2624
sys_getbackground:
2625
;    cmp   eax,1                                  ; SIZE
2626
        dec     ebx
2627
        jnz     nogb1
2628
        mov     eax, [BgrDataWidth]
2629
        shl     eax, 16
2630
        mov     ax, [BgrDataHeight]
2631
        mov     [esp+32], eax
2632
        ret
2633
 
2634
nogb1:
2635
;    cmp   eax,2                                  ; PIXEL
2636
        dec     ebx
2637
        jnz     nogb2
2638
 
2639
        mov     eax, [img_background]
2640
        test    ecx, ecx
2641
        jz      @f
2642
        cmp     eax, static_background_data
2643
        jz      .ret
2644
@@:
2645
        mov     ebx, [mem_BACKGROUND]
2646
        add     ebx, 4095
2647
        and     ebx, -4096
2648
        sub     ebx, 4
2649
        cmp     ecx, ebx
2650
        ja      .ret
2651
 
2652
        mov     eax, [ecx+eax]
2653
 
2654
        and     eax, 0xFFFFFF
2655
        mov     [esp+32], eax
2656
.ret:
2657
        ret
2658
  nogb2:
2659
 
2660
;    cmp   eax,4                                  ; TILED / STRETCHED
2661
        dec     ebx
2662
        dec     ebx
2663
        jnz     nogb4
2664
        mov     eax, [BgrDrawMode]
2665
  nogb4:
2666
        mov     [esp+32], eax
2667
        ret
2668
 
2669
align 4
2670
 
2671
sys_getkey:
2672
        mov     [esp + 32], dword 1
2673
        ; test main buffer
2674
        mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
2675
        movzx   ecx, word [WIN_STACK + ebx * 2]
2676
        mov     edx, [TASK_COUNT]
2677
        cmp     ecx, edx
2678
        jne     .finish
2679
        cmp     [KEY_COUNT], byte 0
2680
        je      .finish
2681
        movzx   eax, byte [KEY_BUFF]
2682
        shl     eax, 8
2683
        push    eax
2684
        dec     byte [KEY_COUNT]
2685
        and     byte [KEY_COUNT], 127
2686
        movzx   ecx, byte [KEY_COUNT]
2687
        add     ecx, 2
2688
        mov     eax, KEY_BUFF + 1
2689
        mov     ebx, KEY_BUFF
2690
        call    memmove
2691
        pop     eax
2692
.ret_eax:
2693
        mov     [esp + 32], eax
2694
        ret
2695
.finish:
2696
; test hotkeys buffer
2697
        mov     ecx, hotkey_buffer
2698
@@:
2699
        cmp     [ecx], ebx
2700
        jz      .found
2701
        add     ecx, 8
2702
        cmp     ecx, hotkey_buffer + 120 * 8
2703
        jb      @b
2704
        ret
2705
.found:
2706
        mov     ax, [ecx + 6]
2707
        shl     eax, 16
2708
        mov     ah, [ecx + 4]
2709
        mov     al, 2
2710
        and     dword [ecx + 4], 0
2711
        and     dword [ecx], 0
2712
        jmp     .ret_eax
2713
 
2714
align 4
2715
 
2716
sys_getbutton:
2717
 
2718
        mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
2719
        mov     [esp + 32], dword 1
2720
        movzx   ecx, word [WIN_STACK + ebx * 2]
2721
        mov     edx, [TASK_COUNT] ; less than 256 processes
2722
        cmp     ecx, edx
2723
        jne     .exit
2724
        movzx   eax, byte [BTN_COUNT]
2725
        test    eax, eax
2726
        jz      .exit
2727
        mov     eax, [BTN_BUFF]
2728
        and     al, 0xFE                                    ; delete left button bit
2729
        mov     [BTN_COUNT], byte 0
2730
        mov     [esp + 32], eax
2731
.exit:
2732
        ret
2733
 
2734
 
2735
align 4
2736
 
2737
sys_cpuusage:
2738
 
2739
;  RETURN:
2740
;
2741
;  +00 dword     process cpu usage
2742
;  +04  word     position in windowing stack
2743
;  +06  word     windowing stack value at current position (cpu nro)
2744
;  +10 12 bytes  name
2745
;  +22 dword     start in mem
2746
;  +26 dword     used mem
2747
;  +30 dword     PID , process idenfification number
2748
;
2749
 
2750
        cmp     ecx, -1 ; who am I ?
2751
        jne     .no_who_am_i
2752
        mov     ecx, [CURRENT_TASK]
2753
  .no_who_am_i:
2754
        cmp     ecx, max_processes
2755
        ja      .nofillbuf
2756
 
2757
; +4: word: position of the window of thread in the window stack
2758
        mov     ax, [WIN_STACK + ecx * 2]
2759
        mov     [ebx+4], ax
2760
; +6: word: number of the thread slot, which window has in the window stack
2761
;           position ecx (has no relation to the specific thread)
2762
        mov     ax, [WIN_POS + ecx * 2]
2763
        mov     [ebx+6], ax
2764
 
2765
        shl     ecx, 5
2766
 
2767
; +0: dword: memory usage
2768
        mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
2769
        mov     [ebx], eax
2770
; +10: 11 bytes: name of the process
2771
        push    ecx
2772
        lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
2773
        add     ebx, 10
2774
        mov     ecx, 11
2775
        call    memmove
2776
        pop     ecx
2777
 
2778
; +22: address of the process in memory
2779
; +26: size of used memory - 1
2780
        push    edi
2781
        lea     edi, [ebx+12]
2782
        xor     eax, eax
2783
        mov     edx, 0x100000*16
2784
        cmp     ecx, 1 shl 5
2785
        je      .os_mem
2786
        mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
2787
        mov     eax, std_application_base_address
2788
.os_mem:
2789
        stosd
2790
        lea     eax, [edx-1]
2791
        stosd
2792
 
2793
; +30: PID/TID
2794
        mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
2795
        stosd
2796
 
2797
    ; window position and size
2798
        push    esi
2799
        lea     esi, [ecx + window_data + WDATA.box]
2800
        movsd
2801
        movsd
2802
        movsd
2803
        movsd
2804
 
2805
    ; Process state (+50)
2806
        mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
2807
        stosd
2808
 
2809
    ; Window client area box
2810
        lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
2811
        movsd
2812
        movsd
2813
        movsd
2814
        movsd
2815
 
2816
    ; Window state
2817
        mov     al, [ecx+window_data+WDATA.fl_wstate]
2818
        stosb
2819
 
2820
    ; Event mask (+71)
2821
        mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
2822
        stosd
2823
 
2824
        pop     esi
2825
        pop     edi
2826
 
2827
.nofillbuf:
2828
    ; return number of processes
2829
 
2830
        mov     eax, [TASK_COUNT]
2831
        mov     [esp+32], eax
2832
        ret
2833
 
2834
align 4
2835
sys_clock:
2836
        cli
2837
  ; Mikhail Lisovin  xx Jan 2005
2838
  @@:
2839
        mov     al, 10
2840
        out     0x70, al
2841
        in      al, 0x71
2842
        test    al, al
2843
        jns     @f
2844
        mov     esi, 1
2845
        call    delay_ms
2846
        jmp     @b
2847
  @@:
2848
  ; end Lisovin's fix
2849
 
2850
        xor     al, al        ; seconds
2851
        out     0x70, al
2852
        in      al, 0x71
2853
        movzx   ecx, al
2854
        mov     al, 02        ; minutes
2855
        shl     ecx, 16
2856
        out     0x70, al
2857
        in      al, 0x71
2858
        movzx   edx, al
2859
        mov     al, 04        ; hours
2860
        shl     edx, 8
2861
        out     0x70, al
2862
        in      al, 0x71
2863
        add     ecx, edx
2864
        movzx   edx, al
2865
        add     ecx, edx
2866
        sti
2867
        mov     [esp + 32], ecx
2868
        ret
2869
 
2870
 
2871
align 4
2872
 
2873
sys_date:
2874
 
2875
        cli
2876
  @@:
2877
        mov     al, 10
2878
        out     0x70, al
2879
        in      al, 0x71
2880
        test    al, al
2881
        jns     @f
2882
        mov     esi, 1
2883
        call    delay_ms
2884
        jmp     @b
2885
  @@:
2886
 
2887
        mov     ch, 0
2888
        mov     al, 7           ; date
2889
        out     0x70, al
2890
        in      al, 0x71
2891
        mov     cl, al
2892
        mov     al, 8           ; month
2893
        shl     ecx, 16
2894
        out     0x70, al
2895
        in      al, 0x71
2896
        mov     ch, al
2897
        mov     al, 9           ; year
2898
        out     0x70, al
2899
        in      al, 0x71
2900
        mov     cl, al
2901
        sti
2902
        mov     [esp+32], ecx
2903
        ret
2904
 
2905
 
2906
; redraw status
2907
 
2908
sys_redrawstat:
2909
        cmp     ebx, 1
2910
        jne     no_widgets_away
2911
        ; buttons away
2912
        mov     ecx, [CURRENT_TASK]
2913
  sys_newba2:
2914
        mov     edi, [BTN_ADDR]
2915
        cmp     [edi], dword 0  ; empty button list ?
2916
        je      end_of_buttons_away
2917
        movzx   ebx, word [edi]
2918
        inc     ebx
2919
        mov     eax, edi
2920
  sys_newba:
2921
        dec     ebx
2922
        jz      end_of_buttons_away
2923
 
2924
        add     eax, 0x10
2925
        cmp     cx, [eax]
2926
        jnz     sys_newba
2927
 
2928
        push    eax ebx ecx
2929
        mov     ecx, ebx
2930
        inc     ecx
2931
        shl     ecx, 4
2932
        mov     ebx, eax
2933
        add     eax, 0x10
2934
        call    memmove
2935
        dec     dword [edi]
2936
        pop     ecx ebx eax
2937
 
2938
        jmp     sys_newba2
2939
 
2940
  end_of_buttons_away:
2941
 
2942
        ret
2943
 
2944
  no_widgets_away:
2945
 
2946
        cmp     ebx, 2
2947
        jnz     srl1
2948
 
2949
        mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
2950
        add     edx, draw_data - CURRENT_TASK
2951
        mov     [edx + RECT.left], 0
2952
        mov     [edx + RECT.top], 0
2953
        mov     eax, [Screen_Max_X]
2954
        mov     [edx + RECT.right], eax
2955
        mov     eax, [Screen_Max_Y]
2956
        mov     [edx + RECT.bottom], eax
2957
 
2958
  srl1:
2959
        ret
2960
 
2961
;ok - 100% work
2962
;nt - not tested
2963
;---------------------------------------------------------------------------------------------
2964
;eax
2965
;0 - task switch counter. Ret switch counter in eax. Block. ok.
2966
;1 - change task. Ret nothing. Block. ok.
2967
;2 - performance control
2968
; ebx
2969
; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
2970
; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
2971
; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
2972
; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
2973
; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
2974
;eax
2975
;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
2976
;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
2977
;---------------------------------------------------------------------------------------------
2978
iglobal
2979
align 4
2980
sheduler:
2981
        dd      sys_sheduler.00
2982
        dd      change_task
2983
        dd      sys_sheduler.02
2984
        dd      sys_sheduler.03
2985
        dd      sys_sheduler.04
2986
endg
2987
sys_sheduler:
2988
;rewritten by   29.12.2009
2989
        jmp     dword [sheduler+ebx*4]
2990
;.shed_counter:
2991
.00:
2992
        mov     eax, [context_counter]
2993
        mov     [esp+32], eax
2994
        ret
2995
 
2996
.02:
2997
;.perf_control:
2998
        inc     ebx                     ;before ebx=2, ebx=3
2999
        cmp     ebx, ecx                ;if ecx=3, ebx=3
3000
        jz      cache_disable
3001
 
3002
        dec     ebx                     ;ebx=2
3003
        cmp     ebx, ecx                ;
3004
        jz      cache_enable            ;if ecx=2 and ebx=2
3005
 
3006
        dec     ebx                     ;ebx=1
3007
        cmp     ebx, ecx
3008
        jz      is_cache_enabled        ;if ecx=1 and ebx=1
3009
 
3010
        dec     ebx
3011
        test    ebx, ecx                ;ebx=0 and ecx=0
3012
        jz      modify_pce              ;if ecx=0
3013
 
3014
        ret
3015
 
3016
.03:
3017
;.rdmsr_instr:
3018
;now counter in ecx
3019
;(edx:eax) esi:edi => edx:esi
3020
        mov     eax, esi
3021
        mov     ecx, edx
3022
        rdmsr
3023
        mov     [esp+32], eax
3024
        mov     [esp+20], edx           ;ret in ebx?
3025
        ret
3026
 
3027
.04:
3028
;.wrmsr_instr:
3029
;now counter in ecx
3030
;(edx:eax) esi:edi => edx:esi
3031
        ; Fast Call MSR can't be destroy
3032
        ; ® MSR_AMD_EFER ¬®¦­® ¨§¬¥­ïâì, â.ª. ¢ í⮬ ॣ¨áâॠ«¨è
3033
        ; ¢ª«îç îâáï/¢ëª«îç îâáï à áè¨à¥­­ë¥ ¢®§¬®¦­®áâ¨
3034
        cmp     edx, MSR_SYSENTER_CS
3035
        je      @f
3036
        cmp     edx, MSR_SYSENTER_ESP
3037
        je      @f
3038
        cmp     edx, MSR_SYSENTER_EIP
3039
        je      @f
3040
        cmp     edx, MSR_AMD_STAR
3041
        je      @f
3042
 
3043
        mov     eax, esi
3044
        mov     ecx, edx
3045
        wrmsr
3046
        ; mov   [esp + 32], eax
3047
        ; mov   [esp + 20], edx ;ret in ebx?
3048
@@:
3049
        ret
3050
 
3051
cache_disable:
3052
        mov     eax, cr0
3053
        or      eax, 01100000000000000000000000000000b
3054
        mov     cr0, eax
3055
        wbinvd  ;set MESI
3056
        ret
3057
 
3058
cache_enable:
3059
        mov     eax, cr0
3060
        and     eax, 10011111111111111111111111111111b
3061
        mov     cr0, eax
3062
        ret
3063
 
3064
is_cache_enabled:
3065
        mov     eax, cr0
3066
        mov     ebx, eax
3067
        and     eax, 01100000000000000000000000000000b
3068
        jz      cache_disabled
3069
        mov     [esp+32], ebx
3070
cache_disabled:
3071
        mov     dword [esp+32], eax;0
3072
        ret
3073
 
3074
modify_pce:
3075
        mov     eax, cr4
3076
;       mov ebx,0
3077
;       or  bx,100000000b ;pce
3078
;       xor eax,ebx ;invert pce
3079
        bts     eax, 8;pce=cr4[8]
3080
        mov     cr4, eax
3081
        mov     [esp+32], eax
3082
        ret
3083
;---------------------------------------------------------------------------------------------
3084
 
3085
 
3086
; check if pixel is allowed to be drawn
3087
 
2446 mario79 3088
;checkpixel:
3089
;        push    eax edx
2288 clevermous 3090
 
2446 mario79 3091
;;        mov     edx, [Screen_Max_X] ; screen x size
3092
;;        inc     edx
3093
;;        imul    edx, ebx
3094
;        mov     edx, [d_width_calc_area + ebx*4]
3095
;        add     eax, [_WinMapAddress]
3096
;        mov     dl, [eax+edx]; lea eax, [...]
2288 clevermous 3097
 
2446 mario79 3098
;        xor     ecx, ecx
3099
;        mov     eax, [CURRENT_TASK]
3100
;        cmp     al, dl
3101
;        setne   cl
2288 clevermous 3102
 
2446 mario79 3103
;        pop     edx eax
3104
;        ret
2288 clevermous 3105
 
3106
iglobal
3107
  cpustring db 'CPU',0
3108
endg
3109
 
3110
uglobal
3111
background_defined    db    0    ; diamond, 11.04.2006
3112
endg
3113
 
3114
align 4
3115
; check misc
3116
 
3117
checkmisc:
3118
 
3119
        cmp     [ctrl_alt_del], 1
3120
        jne     nocpustart
3121
 
3122
        mov     ebp, cpustring
3123
        call    fs_execute_from_sysdir
3124
 
3125
        mov     [ctrl_alt_del], 0
3126
 
3127
nocpustart:
3128
        cmp     [mouse_active], 1
3129
        jne     mouse_not_active
3130
        mov     [mouse_active], 0
2411 Serge 3131
 
2288 clevermous 3132
        xor     edi, edi
2411 Serge 3133
        mov     ebx, CURRENT_TASK
3134
 
2288 clevermous 3135
        mov     ecx, [TASK_COUNT]
2408 Serge 3136
        movzx   eax, word [WIN_POS + ecx*2]     ; active window
3137
        shl     eax, 8
2414 Serge 3138
        push    eax
2408 Serge 3139
 
2414 Serge 3140
        movzx   eax, word [MOUSE_X]
3141
        movzx   edx, word [MOUSE_Y]
3142
 
2408 Serge 3143
align 4
3144
.set_mouse_event:
2288 clevermous 3145
        add     edi, 256
2411 Serge 3146
        add     ebx, 32
3147
        test    [ebx+TASKDATA.event_mask], 0x80000000
2414 Serge 3148
        jz      .pos_filter
3149
 
3150
        cmp     edi, [esp]                      ; skip if filtration active
3151
        jne     .skip
3152
 
3153
.pos_filter:
3154
        test    [ebx+TASKDATA.event_mask], 0x40000000
2411 Serge 3155
        jz      .set
2288 clevermous 3156
 
2414 Serge 3157
        mov     esi, [ebx-twdw+WDATA.box.left]
3158
        cmp     eax, esi
3159
        jb      .skip
3160
        add     esi, [ebx-twdw+WDATA.box.width]
3161
        cmp     eax, esi
3162
        ja      .skip
3163
 
3164
        mov     esi, [ebx-twdw+WDATA.box.top]
3165
        cmp     edx, esi
3166
        jb      .skip
3167
        add     esi, [ebx-twdw+WDATA.box.height]
3168
        cmp     edx, esi
3169
        ja      .skip
2411 Serge 3170
.set:
2408 Serge 3171
        or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b
2411 Serge 3172
.skip:
2408 Serge 3173
        loop    .set_mouse_event
3174
 
2414 Serge 3175
        pop     eax
3176
 
2288 clevermous 3177
mouse_not_active:
3178
        cmp     byte[BACKGROUND_CHANGED], 0
3179
        jz      no_set_bgr_event
3180
        xor     edi, edi
3181
        mov     ecx, [TASK_COUNT]
3182
set_bgr_event:
3183
        add     edi, 256
3184
        or      [edi+SLOT_BASE+APPDATA.event_mask], 16
3185
        loop    set_bgr_event
3186
        mov     byte[BACKGROUND_CHANGED], 0
3187
no_set_bgr_event:
3188
        cmp     byte[REDRAW_BACKGROUND], 0         ; background update ?
3189
        jz      nobackgr
3190
        cmp     [background_defined], 0
3191
        jz      nobackgr
3192
;    mov   [draw_data+32 + RECT.left],dword 0
3193
;    mov   [draw_data+32 + RECT.top],dword 0
3194
;    mov   eax,[Screen_Max_X]
3195
;    mov   ebx,[Screen_Max_Y]
3196
;    mov   [draw_data+32 + RECT.right],eax
3197
;    mov   [draw_data+32 + RECT.bottom],ebx
3198
@@:
3199
        call    drawbackground
3200
        xor     eax, eax
3201
        xchg    al, [REDRAW_BACKGROUND]
3202
        test    al, al                             ; got new update request?
3203
        jnz     @b
3204
        mov     [draw_data+32 + RECT.left], eax
3205
        mov     [draw_data+32 + RECT.top], eax
3206
        mov     [draw_data+32 + RECT.right], eax
3207
        mov     [draw_data+32 + RECT.bottom], eax
3208
        mov     [MOUSE_BACKGROUND], byte 0
3209
 
3210
nobackgr:
3211
 
3212
    ; system shutdown request
3213
 
3214
        cmp     [SYS_SHUTDOWN], byte 0
3215
        je      noshutdown
3216
 
3217
        mov     edx, [shutdown_processes]
3218
 
3219
        cmp     [SYS_SHUTDOWN], dl
3220
        jne     no_mark_system_shutdown
3221
 
3222
        lea     ecx, [edx-1]
3223
        mov     edx, OS_BASE+0x3040
3224
        jecxz   @f
3225
markz:
3226
        mov     [edx+TASKDATA.state], byte 3
3227
        add     edx, 0x20
3228
        loop    markz
3229
@@:
3230
 
3231
  no_mark_system_shutdown:
3232
 
3233
        dec     byte [SYS_SHUTDOWN]
3234
        je      system_shutdown
3235
 
3236
noshutdown:
3237
 
3238
 
3239
        mov     eax, [TASK_COUNT]           ; termination
3240
        mov     ebx, TASK_DATA+TASKDATA.state
3241
        mov     esi, 1
3242
 
3243
newct:
3244
        mov     cl, [ebx]
3245
        cmp     cl, byte 3
3246
        jz      terminate
3247
        cmp     cl, byte 4
3248
        jz      terminate
3249
 
3250
        add     ebx, 0x20
3251
        inc     esi
3252
        dec     eax
3253
        jnz     newct
3254
        ret
3255
 
3256
; redraw screen
3257
 
3258
redrawscreen:
3259
 
3260
; eax , if process window_data base is eax, do not set flag/limits
3261
 
3262
        pushad
3263
        push    eax
3264
 
3265
;;;         mov   ebx,2
3266
;;;         call  delay_hs
3267
 
3268
         ;mov   ecx,0               ; redraw flags for apps
3269
        xor     ecx, ecx
3270
       newdw2:
3271
 
3272
        inc     ecx
3273
        push    ecx
3274
 
3275
        mov     eax, ecx
3276
        shl     eax, 5
3277
        add     eax, window_data
3278
 
3279
        cmp     eax, [esp+4]
3280
        je      not_this_task
3281
                                   ; check if window in redraw area
3282
        mov     edi, eax
3283
 
3284
        cmp     ecx, 1             ; limit for background
3285
        jz      bgli
3286
 
3287
        mov     eax, [edi + WDATA.box.left]
3288
        mov     ebx, [edi + WDATA.box.top]
3289
        mov     ecx, [edi + WDATA.box.width]
3290
        mov     edx, [edi + WDATA.box.height]
3291
        add     ecx, eax
3292
        add     edx, ebx
3293
 
3294
        mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
3295
        cmp     ecx, ebx
3296
        jb      ricino
3297
 
3298
        mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
3299
        cmp     ecx, eax
3300
        jb      ricino
3301
 
3302
        mov     eax, [edi + WDATA.box.left]
3303
        mov     ebx, [edi + WDATA.box.top]
3304
        mov     ecx, [edi + WDATA.box.width]
3305
        mov     edx, [edi + WDATA.box.height]
3306
        add     ecx, eax
3307
        add     edx, ebx
3308
 
3309
        mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
3310
        cmp     edx, eax
3311
        jb      ricino
3312
 
3313
        mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
3314
        cmp     ecx, eax
3315
        jb      ricino
3316
 
3317
        bgli:
3318
 
3319
        cmp     dword[esp], 1
3320
        jnz     .az
3321
;         cmp   byte[BACKGROUND_CHANGED], 0
3322
;         jnz   newdw8
3323
        cmp     byte[REDRAW_BACKGROUND], 0
3324
        jz      .az
3325
        mov     dl, 0
3326
        lea     eax, [edi+draw_data-window_data]
3327
        mov     ebx, [draw_limits.left]
3328
        cmp     ebx, [eax+RECT.left]
3329
        jae     @f
3330
        mov     [eax+RECT.left], ebx
3331
        mov     dl, 1
3332
        @@:
3333
        mov     ebx, [draw_limits.top]
3334
        cmp     ebx, [eax+RECT.top]
3335
        jae     @f
3336
        mov     [eax+RECT.top], ebx
3337
        mov     dl, 1
3338
        @@:
3339
        mov     ebx, [draw_limits.right]
3340
        cmp     ebx, [eax+RECT.right]
3341
        jbe     @f
3342
        mov     [eax+RECT.right], ebx
3343
        mov     dl, 1
3344
        @@:
3345
        mov     ebx, [draw_limits.bottom]
3346
        cmp     ebx, [eax+RECT.bottom]
3347
        jbe     @f
3348
        mov     [eax+RECT.bottom], ebx
3349
        mov     dl, 1
3350
        @@:
3351
        add     byte[REDRAW_BACKGROUND], dl
3352
        jmp     newdw8
3353
        .az:
3354
 
3355
        mov     eax, edi
3356
        add     eax, draw_data-window_data
3357
 
3358
        mov     ebx, [draw_limits.left]        ; set limits
3359
        mov     [eax + RECT.left], ebx
3360
        mov     ebx, [draw_limits.top]
3361
        mov     [eax + RECT.top], ebx
3362
        mov     ebx, [draw_limits.right]
3363
        mov     [eax + RECT.right], ebx
3364
        mov     ebx, [draw_limits.bottom]
3365
        mov     [eax + RECT.bottom], ebx
3366
 
3367
        sub     eax, draw_data-window_data
3368
 
3369
        cmp     dword [esp], 1
3370
        jne     nobgrd
3371
        inc     byte[REDRAW_BACKGROUND]
3372
 
3373
       newdw8:
3374
       nobgrd:
3375
 
3376
        mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
3377
 
3378
       ricino:
3379
 
3380
       not_this_task:
3381
 
3382
        pop     ecx
3383
 
3384
        cmp     ecx, [TASK_COUNT]
3385
        jle     newdw2
3386
 
3387
        pop     eax
3388
        popad
3389
 
3390
        ret
3391
 
3392
calculatebackground:   ; background
3393
 
3394
        mov     edi, [_WinMapAddress]              ; set os to use all pixels
3395
        mov     eax, 0x01010101
3396
        mov     ecx, [_WinMapSize]
3397
        shr     ecx, 2
3398
        rep stosd
3399
 
3400
        mov     byte[REDRAW_BACKGROUND], 0            ; do not draw background!
3401
        mov     byte[BACKGROUND_CHANGED], 0
3402
 
3403
        ret
3404
 
3405
uglobal
3406
  imax    dd 0x0
3407
endg
3408
 
3409
 
3410
 
3411
delay_ms:     ; delay in 1/1000 sec
3412
 
3413
 
3414
        push    eax
3415
        push    ecx
3416
 
3417
        mov     ecx, esi
3418
        ; 
3419
        imul    ecx, 33941
3420
        shr     ecx, 9
3421
        ; 
3422
 
3423
        in      al, 0x61
3424
        and     al, 0x10
3425
        mov     ah, al
3426
        cld
3427
 
3428
 cnt1:
3429
        in      al, 0x61
3430
        and     al, 0x10
3431
        cmp     al, ah
3432
        jz      cnt1
3433
 
3434
        mov     ah, al
3435
        loop    cnt1
3436
 
3437
        pop     ecx
3438
        pop     eax
3439
 
3440
        ret
3441
 
2411 Serge 3442
align 4
2288 clevermous 3443
set_app_param:
3444
        mov     edi, [TASK_BASE]
2408 Serge 3445
        mov     eax, ebx
3446
        btr     eax, 3                           ; move MOUSE_FILTRATION
3447
        mov     ebx, [current_slot]              ; bit into event_filter
3448
        setc    byte [ebx+APPDATA.event_filter]
3449
        xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
3450
        mov     [esp+32], eax                    ; return old mask value
2288 clevermous 3451
        ret
3452
 
3453
 
3454
 
3455
delay_hs:     ; delay in 1/100 secs
3456
; ebx = delay time
3457
        push    ecx
3458
        push    edx
3459
 
3460
        mov     edx, [timer_ticks]
3461
 
3462
      newtic:
3463
        mov     ecx, [timer_ticks]
3464
        sub     ecx, edx
3465
        cmp     ecx, ebx
3466
        jae     zerodelay
3467
 
3468
        call    change_task
3469
 
3470
        jmp     newtic
3471
 
3472
      zerodelay:
3473
        pop     edx
3474
        pop     ecx
3475
 
3476
        ret
3477
 
3478
align 16        ;very often call this subrutine
3479
memmove:       ; memory move in bytes
3480
 
3481
; eax = from
3482
; ebx = to
3483
; ecx = no of bytes
3484
        test    ecx, ecx
3485
        jle     .ret
3486
 
3487
        push    esi edi ecx
3488
 
3489
        mov     edi, ebx
3490
        mov     esi, eax
3491
 
3492
        test    ecx, not 11b
3493
        jz      @f
3494
 
3495
        push    ecx
3496
        shr     ecx, 2
3497
        rep movsd
3498
        pop     ecx
3499
        and     ecx, 11b
3500
        jz      .finish
3501
  @@:
3502
        rep movsb
3503
 
3504
  .finish:
3505
        pop     ecx edi esi
3506
  .ret:
3507
        ret
3508
 
3509
 
3510
;  Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
3511
;align 4
3512
;
3513
;read_floppy_file:
3514
;
3515
;; as input
3516
;;
3517
;; eax pointer to file
3518
;; ebx file lenght
3519
;; ecx start 512 byte block number
3520
;; edx number of blocks to read
3521
;; esi pointer to return/work area (atleast 20 000 bytes)
3522
;;
3523
;;
3524
;; on return
3525
;;
3526
;; eax = 0 command succesful
3527
;;       1 no fd base and/or partition defined
3528
;;       2 yet unsupported FS
3529
;;       3 unknown FS
3530
;;       4 partition not defined at hd
3531
;;       5 file not found
3532
;; ebx = size of file
3533
;
3534
;     mov   edi,[TASK_BASE]
3535
;     add   edi,0x10
3536
;     add   esi,[edi]
3537
;     add   eax,[edi]
3538
;
3539
;     pushad
3540
;     mov  edi,esi
3541
;     add  edi,1024
3542
;     mov  esi,0x100000+19*512
3543
;     sub  ecx,1
3544
;     shl  ecx,9
3545
;     add  esi,ecx
3546
;     shl  edx,9
3547
;     mov  ecx,edx
3548
;     cld
3549
;     rep  movsb
3550
;     popad
3551
;
3552
;     mov   [esp+36],eax
3553
;     mov   [esp+24],ebx
3554
;     ret
3555
 
3556
 
3557
 
3558
align 4
3559
set_io_access_rights:
3560
        push    edi eax
3561
        mov     edi, tss._io_map_0
3562
;     mov   ecx,eax
3563
;     and   ecx,7    ; offset in byte
3564
;     shr   eax,3    ; number of byte
3565
;     add   edi,eax
3566
;     mov   ebx,1
3567
;     shl   ebx,cl
3568
        test    ebp, ebp
3569
;     cmp   ebp,0                ; enable access - ebp = 0
3570
        jnz     .siar1
3571
;     not   ebx
3572
;     and   [edi],byte bl
3573
        btr     [edi], eax
3574
        pop     eax edi
3575
        ret
3576
.siar1:
3577
        bts     [edi], eax
3578
  ;  or    [edi],byte bl        ; disable access - ebp = 1
3579
        pop     eax edi
3580
        ret
3581
;reserve/free group of ports
3582
;  * eax = 46 - number function
3583
;  * ebx = 0 - reserve, 1 - free
3584
;  * ecx = number start arrea of ports
3585
;  * edx = number end arrea of ports (include last number of port)
3586
;Return value:
3587
;  * eax = 0 - succesful
3588
;  * eax = 1 - error
3589
;  * The system has reserve this ports:
3590
;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
3591
;destroys eax,ebx, ebp
3592
r_f_port_area:
3593
 
3594
        test    ebx, ebx
3595
        jnz     free_port_area
3596
;     je    r_port_area
3597
;     jmp   free_port_area
3598
 
3599
;   r_port_area:
3600
 
3601
;     pushad
3602
 
3603
        cmp     ecx, edx      ; beginning > end ?
3604
        ja      rpal1
3605
        cmp     edx, 65536
3606
        jae     rpal1
3607
        mov     eax, [RESERVED_PORTS]
3608
        test    eax, eax      ; no reserved areas ?
3609
        je      rpal2
3610
        cmp     eax, 255      ; max reserved
3611
        jae     rpal1
3612
 rpal3:
3613
        mov     ebx, eax
3614
        shl     ebx, 4
3615
        add     ebx, RESERVED_PORTS
3616
        cmp     ecx, [ebx+8]
3617
        ja      rpal4
3618
        cmp     edx, [ebx+4]
3619
        jae     rpal1
3620
;     jb    rpal4
3621
;     jmp   rpal1
3622
 rpal4:
3623
        dec     eax
3624
        jnz     rpal3
3625
        jmp     rpal2
3626
   rpal1:
3627
;     popad
3628
;     mov   eax,1
3629
        xor     eax, eax
3630
        inc     eax
3631
        ret
3632
   rpal2:
3633
;     popad
3634
     ; enable port access at port IO map
3635
        cli
3636
        pushad                        ; start enable io map
3637
 
3638
        cmp     edx, 65536;16384
3639
        jae     no_unmask_io; jge
3640
        mov     eax, ecx
3641
;       push    ebp
3642
        xor     ebp, ebp               ; enable - eax = port
3643
new_port_access:
3644
;     pushad
3645
        call    set_io_access_rights
3646
;     popad
3647
        inc     eax
3648
        cmp     eax, edx
3649
        jbe     new_port_access
3650
;       pop     ebp
3651
no_unmask_io:
3652
        popad                         ; end enable io map
3653
        sti
3654
 
3655
        mov     eax, [RESERVED_PORTS]
3656
        add     eax, 1
3657
        mov     [RESERVED_PORTS], eax
3658
        shl     eax, 4
3659
        add     eax, RESERVED_PORTS
3660
        mov     ebx, [TASK_BASE]
3661
        mov     ebx, [ebx+TASKDATA.pid]
3662
        mov     [eax], ebx
3663
        mov     [eax+4], ecx
3664
        mov     [eax+8], edx
3665
 
3666
        xor     eax, eax
3667
        ret
3668
 
3669
free_port_area:
3670
 
3671
;     pushad
3672
        mov     eax, [RESERVED_PORTS]; no reserved areas ?
3673
        test    eax, eax
3674
        jz      frpal2
3675
        mov     ebx, [TASK_BASE]
3676
        mov     ebx, [ebx+TASKDATA.pid]
3677
   frpal3:
3678
        mov     edi, eax
3679
        shl     edi, 4
3680
        add     edi, RESERVED_PORTS
3681
        cmp     ebx, [edi]
3682
        jne     frpal4
3683
        cmp     ecx, [edi+4]
3684
        jne     frpal4
3685
        cmp     edx, [edi+8]
3686
        jne     frpal4
3687
        jmp     frpal1
3688
   frpal4:
3689
        dec     eax
3690
        jnz     frpal3
3691
   frpal2:
3692
;     popad
3693
        inc     eax
3694
        ret
3695
   frpal1:
3696
        push    ecx
3697
        mov     ecx, 256
3698
        sub     ecx, eax
3699
        shl     ecx, 4
3700
        mov     esi, edi
3701
        add     esi, 16
3702
        cld
3703
        rep movsb
3704
 
3705
        dec     dword [RESERVED_PORTS]
3706
;popad
3707
;disable port access at port IO map
3708
 
3709
;     pushad                        ; start disable io map
3710
        pop     eax     ;start port
3711
        cmp     edx, 65536;16384
3712
        jge     no_mask_io
3713
 
3714
;     mov   eax,ecx
3715
        xor     ebp, ebp
3716
        inc     ebp
3717
new_port_access_disable:
3718
;     pushad
3719
;     mov   ebp,1                  ; disable - eax = port
3720
        call    set_io_access_rights
3721
;     popad
3722
        inc     eax
3723
        cmp     eax, edx
3724
        jbe     new_port_access_disable
3725
no_mask_io:
3726
;     popad                         ; end disable io map
3727
        xor     eax, eax
3728
        ret
2430 mario79 3729
;-----------------------------------------------------------------------------
2288 clevermous 3730
align 4
3731
drawbackground:
2430 mario79 3732
dbrv20:
2288 clevermous 3733
        cmp     [BgrDrawMode], dword 1
3734
        jne     bgrstr
3735
        call    vesa20_drawbackground_tiled
3736
        call    [draw_pointer]
3737
        ret
2430 mario79 3738
;--------------------------------------
3739
align 4
3740
bgrstr:
2288 clevermous 3741
        call    vesa20_drawbackground_stretch
3742
        call    [draw_pointer]
3743
        ret
2430 mario79 3744
;-----------------------------------------------------------------------------
2288 clevermous 3745
align 4
3746
syscall_putimage:                       ; PutImage
3747
sys_putimage:
3748
        test    ecx, 0x80008000
3749
        jnz     .exit
3750
        test    ecx, 0x0000FFFF
3751
        jz      .exit
3752
        test    ecx, 0xFFFF0000
3753
        jnz     @f
2430 mario79 3754
;--------------------------------------
3755
align 4
3756
.exit:
2288 clevermous 3757
        ret
2430 mario79 3758
;--------------------------------------
3759
align 4
3760
@@:
2288 clevermous 3761
        mov     edi, [current_slot]
3762
        add     dx, word[edi+APPDATA.wnd_clientbox.top]
3763
        rol     edx, 16
3764
        add     dx, word[edi+APPDATA.wnd_clientbox.left]
3765
        rol     edx, 16
2430 mario79 3766
;--------------------------------------
3767
align 4
3768
.forced:
2288 clevermous 3769
        push    ebp esi 0
3770
        mov     ebp, putimage_get24bpp
3771
        mov     esi, putimage_init24bpp
2430 mario79 3772
;--------------------------------------
3773
align 4
2288 clevermous 3774
sys_putimage_bpp:
2430 mario79 3775
        call    vesa20_putimage
2288 clevermous 3776
        pop     ebp esi ebp
2430 mario79 3777
        ret
3778
;        jmp     [draw_pointer]
3779
;-----------------------------------------------------------------------------
2288 clevermous 3780
align 4
3781
sys_putimage_palette:
3782
; ebx = pointer to image
3783
; ecx = [xsize]*65536 + [ysize]
3784
; edx = [xstart]*65536 + [ystart]
3785
; esi = number of bits per pixel, must be 8, 24 or 32
3786
; edi = pointer to palette
3787
; ebp = row delta
3788
        mov     eax, [CURRENT_TASK]
3789
        shl     eax, 8
3790
        add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
3791
        rol     edx, 16
3792
        add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
3793
        rol     edx, 16
2430 mario79 3794
;--------------------------------------
3795
align 4
2288 clevermous 3796
.forced:
3797
        cmp     esi, 1
3798
        jnz     @f
3799
        push    edi
3800
        mov     eax, [edi+4]
3801
        sub     eax, [edi]
3802
        push    eax
3803
        push    dword [edi]
3804
        push    0ffffff80h
3805
        mov     edi, esp
3806
        call    put_mono_image
3807
        add     esp, 12
3808
        pop     edi
3809
        ret
2430 mario79 3810
;--------------------------------------
3811
align 4
2288 clevermous 3812
@@:
3813
        cmp     esi, 2
3814
        jnz     @f
3815
        push    edi
3816
        push    0ffffff80h
3817
        mov     edi, esp
3818
        call    put_2bit_image
3819
        pop     eax
3820
        pop     edi
3821
        ret
2430 mario79 3822
;--------------------------------------
3823
align 4
2288 clevermous 3824
@@:
3825
        cmp     esi, 4
3826
        jnz     @f
3827
        push    edi
3828
        push    0ffffff80h
3829
        mov     edi, esp
3830
        call    put_4bit_image
3831
        pop     eax
3832
        pop     edi
3833
        ret
2430 mario79 3834
;--------------------------------------
3835
align 4
2288 clevermous 3836
@@:
3837
        push    ebp esi ebp
3838
        cmp     esi, 8
3839
        jnz     @f
3840
        mov     ebp, putimage_get8bpp
3841
        mov     esi, putimage_init8bpp
3842
        jmp     sys_putimage_bpp
2430 mario79 3843
;--------------------------------------
3844
align 4
2288 clevermous 3845
@@:
3846
        cmp     esi, 15
3847
        jnz     @f
3848
        mov     ebp, putimage_get15bpp
3849
        mov     esi, putimage_init15bpp
3850
        jmp     sys_putimage_bpp
2430 mario79 3851
;--------------------------------------
3852
align 4
2288 clevermous 3853
@@:
3854
        cmp     esi, 16
3855
        jnz     @f
3856
        mov     ebp, putimage_get16bpp
3857
        mov     esi, putimage_init16bpp
3858
        jmp     sys_putimage_bpp
2430 mario79 3859
;--------------------------------------
3860
align 4
2288 clevermous 3861
@@:
3862
        cmp     esi, 24
3863
        jnz     @f
3864
        mov     ebp, putimage_get24bpp
3865
        mov     esi, putimage_init24bpp
3866
        jmp     sys_putimage_bpp
2430 mario79 3867
;--------------------------------------
3868
align 4
2288 clevermous 3869
@@:
3870
        cmp     esi, 32
3871
        jnz     @f
3872
        mov     ebp, putimage_get32bpp
3873
        mov     esi, putimage_init32bpp
3874
        jmp     sys_putimage_bpp
2430 mario79 3875
;--------------------------------------
3876
align 4
2288 clevermous 3877
@@:
3878
        pop     ebp esi ebp
3879
        ret
2430 mario79 3880
;-----------------------------------------------------------------------------
3881
align 4
2288 clevermous 3882
put_mono_image:
3883
        push    ebp esi ebp
3884
        mov     ebp, putimage_get1bpp
3885
        mov     esi, putimage_init1bpp
3886
        jmp     sys_putimage_bpp
2430 mario79 3887
;-----------------------------------------------------------------------------
3888
align 4
2288 clevermous 3889
put_2bit_image:
3890
        push    ebp esi ebp
3891
        mov     ebp, putimage_get2bpp
3892
        mov     esi, putimage_init2bpp
3893
        jmp     sys_putimage_bpp
2430 mario79 3894
;-----------------------------------------------------------------------------
3895
align 4
2288 clevermous 3896
put_4bit_image:
3897
        push    ebp esi ebp
3898
        mov     ebp, putimage_get4bpp
3899
        mov     esi, putimage_init4bpp
3900
        jmp     sys_putimage_bpp
2430 mario79 3901
;-----------------------------------------------------------------------------
3902
align 4
2288 clevermous 3903
putimage_init24bpp:
3904
        lea     eax, [eax*3]
3905
putimage_init8bpp:
3906
        ret
2430 mario79 3907
;-----------------------------------------------------------------------------
2288 clevermous 3908
align 16
3909
putimage_get24bpp:
3910
        movzx   eax, byte [esi+2]
3911
        shl     eax, 16
3912
        mov     ax, [esi]
3913
        add     esi, 3
3914
        ret     4
2430 mario79 3915
;-----------------------------------------------------------------------------
2288 clevermous 3916
align 16
3917
putimage_get8bpp:
3918
        movzx   eax, byte [esi]
3919
        push    edx
3920
        mov     edx, [esp+8]
3921
        mov     eax, [edx+eax*4]
3922
        pop     edx
3923
        inc     esi
3924
        ret     4
2430 mario79 3925
;-----------------------------------------------------------------------------
3926
align 4
2288 clevermous 3927
putimage_init1bpp:
3928
        add     eax, ecx
3929
        push    ecx
3930
        add     eax, 7
3931
        add     ecx, 7
3932
        shr     eax, 3
3933
        shr     ecx, 3
3934
        sub     eax, ecx
3935
        pop     ecx
3936
        ret
2430 mario79 3937
;-----------------------------------------------------------------------------
2288 clevermous 3938
align 16
3939
putimage_get1bpp:
3940
        push    edx
3941
        mov     edx, [esp+8]
3942
        mov     al, [edx]
3943
        add     al, al
3944
        jnz     @f
3945
        lodsb
3946
        adc     al, al
3947
@@:
3948
        mov     [edx], al
3949
        sbb     eax, eax
3950
        and     eax, [edx+8]
3951
        add     eax, [edx+4]
3952
        pop     edx
3953
        ret     4
2430 mario79 3954
;-----------------------------------------------------------------------------
3955
align 4
2288 clevermous 3956
putimage_init2bpp:
3957
        add     eax, ecx
3958
        push    ecx
3959
        add     ecx, 3
3960
        add     eax, 3
3961
        shr     ecx, 2
3962
        shr     eax, 2
3963
        sub     eax, ecx
3964
        pop     ecx
3965
        ret
2430 mario79 3966
;-----------------------------------------------------------------------------
2288 clevermous 3967
align 16
3968
putimage_get2bpp:
3969
        push    edx
3970
        mov     edx, [esp+8]
3971
        mov     al, [edx]
3972
        mov     ah, al
3973
        shr     al, 6
3974
        shl     ah, 2
3975
        jnz     .nonewbyte
3976
        lodsb
3977
        mov     ah, al
3978
        shr     al, 6
3979
        shl     ah, 2
3980
        add     ah, 1
3981
.nonewbyte:
3982
        mov     [edx], ah
3983
        mov     edx, [edx+4]
3984
        movzx   eax, al
3985
        mov     eax, [edx+eax*4]
3986
        pop     edx
3987
        ret     4
2430 mario79 3988
;-----------------------------------------------------------------------------
3989
align 4
2288 clevermous 3990
putimage_init4bpp:
3991
        add     eax, ecx
3992
        push    ecx
3993
        add     ecx, 1
3994
        add     eax, 1
3995
        shr     ecx, 1
3996
        shr     eax, 1
3997
        sub     eax, ecx
3998
        pop     ecx
3999
        ret
2430 mario79 4000
;-----------------------------------------------------------------------------
2288 clevermous 4001
align 16
4002
putimage_get4bpp:
4003
        push    edx
4004
        mov     edx, [esp+8]
4005
        add     byte [edx], 80h
4006
        jc      @f
4007
        movzx   eax, byte [edx+1]
4008
        mov     edx, [edx+4]
4009
        and     eax, 0x0F
4010
        mov     eax, [edx+eax*4]
4011
        pop     edx
4012
        ret     4
4013
@@:
4014
        movzx   eax, byte [esi]
4015
        add     esi, 1
4016
        mov     [edx+1], al
4017
        shr     eax, 4
4018
        mov     edx, [edx+4]
4019
        mov     eax, [edx+eax*4]
4020
        pop     edx
4021
        ret     4
2430 mario79 4022
;-----------------------------------------------------------------------------
4023
align 4
2288 clevermous 4024
putimage_init32bpp:
4025
        shl     eax, 2
4026
        ret
2430 mario79 4027
;-----------------------------------------------------------------------------
2288 clevermous 4028
align 16
4029
putimage_get32bpp:
4030
        lodsd
4031
        ret     4
2430 mario79 4032
;-----------------------------------------------------------------------------
4033
align 4
2288 clevermous 4034
putimage_init15bpp:
4035
putimage_init16bpp:
4036
        add     eax, eax
4037
        ret
2430 mario79 4038
;-----------------------------------------------------------------------------
2288 clevermous 4039
align 16
4040
putimage_get15bpp:
4041
; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
4042
        push    ecx edx
4043
        movzx   eax, word [esi]
4044
        add     esi, 2
4045
        mov     ecx, eax
4046
        mov     edx, eax
4047
        and     eax, 0x1F
4048
        and     ecx, 0x1F shl 5
4049
        and     edx, 0x1F shl 10
4050
        shl     eax, 3
4051
        shl     ecx, 6
4052
        shl     edx, 9
4053
        or      eax, ecx
4054
        or      eax, edx
4055
        pop     edx ecx
4056
        ret     4
2430 mario79 4057
;-----------------------------------------------------------------------------
2288 clevermous 4058
align 16
4059
putimage_get16bpp:
4060
; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
4061
        push    ecx edx
4062
        movzx   eax, word [esi]
4063
        add     esi, 2
4064
        mov     ecx, eax
4065
        mov     edx, eax
4066
        and     eax, 0x1F
4067
        and     ecx, 0x3F shl 5
4068
        and     edx, 0x1F shl 11
4069
        shl     eax, 3
4070
        shl     ecx, 5
4071
        shl     edx, 8
4072
        or      eax, ecx
4073
        or      eax, edx
4074
        pop     edx ecx
4075
        ret     4
2430 mario79 4076
;-----------------------------------------------------------------------------
4077
;align 4
2288 clevermous 4078
; eax x beginning
4079
; ebx y beginning
4080
; ecx x end
4081
        ; edx y end
4082
; edi color
2430 mario79 4083
;__sys_drawbar:
4084
;        mov     esi, [current_slot]
4085
;        add     eax, [esi+APPDATA.wnd_clientbox.left]
4086
;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
4087
;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
4088
;        add     edx, [esi+APPDATA.wnd_clientbox.top]
4089
;--------------------------------------
4090
;align 4
4091
;.forced:
4092
;        call    vesa20_drawbar
2407 mario79 4093
;        call    [draw_pointer]
4094
;        ret
2430 mario79 4095
;-----------------------------------------------------------------------------
4096
align 4
2288 clevermous 4097
kb_read:
4098
 
4099
        push    ecx edx
4100
 
4101
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4102
      kr_loop:
4103
        in      al, 0x64
4104
        test    al, 1
4105
        jnz     kr_ready
4106
        loop    kr_loop
4107
        mov     ah, 1
4108
        jmp     kr_exit
4109
      kr_ready:
4110
        push    ecx
4111
        mov     ecx, 32
4112
      kr_delay:
4113
        loop    kr_delay
4114
        pop     ecx
4115
        in      al, 0x60
4116
        xor     ah, ah
4117
      kr_exit:
4118
 
4119
        pop     edx ecx
4120
 
4121
        ret
2430 mario79 4122
;-----------------------------------------------------------------------------
4123
align 4
2288 clevermous 4124
kb_write:
4125
 
4126
        push    ecx edx
4127
 
4128
        mov     dl, al
4129
;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
4130
;      kw_loop1:
4131
;        in      al,0x64
4132
;        test    al,0x20
4133
;        jz      kw_ok1
4134
;        loop    kw_loop1
4135
;        mov     ah,1
4136
;        jmp     kw_exit
4137
;      kw_ok1:
4138
        in      al, 0x60
4139
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4140
      kw_loop:
4141
        in      al, 0x64
4142
        test    al, 2
4143
        jz      kw_ok
4144
        loop    kw_loop
4145
        mov     ah, 1
4146
        jmp     kw_exit
4147
      kw_ok:
4148
        mov     al, dl
4149
        out     0x60, al
4150
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4151
      kw_loop3:
4152
        in      al, 0x64
4153
        test    al, 2
4154
        jz      kw_ok3
4155
        loop    kw_loop3
4156
        mov     ah, 1
4157
        jmp     kw_exit
4158
      kw_ok3:
4159
        mov     ah, 8
4160
      kw_loop4:
4161
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4162
      kw_loop5:
4163
        in      al, 0x64
4164
        test    al, 1
4165
        jnz     kw_ok4
4166
        loop    kw_loop5
4167
        dec     ah
4168
        jnz     kw_loop4
4169
      kw_ok4:
4170
        xor     ah, ah
4171
      kw_exit:
4172
 
4173
        pop     edx ecx
4174
 
4175
        ret
2430 mario79 4176
;-----------------------------------------------------------------------------
4177
align 4
2288 clevermous 4178
kb_cmd:
4179
 
4180
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4181
      c_wait:
4182
        in      al, 0x64
4183
        test    al, 2
4184
        jz      c_send
4185
        loop    c_wait
4186
        jmp     c_error
4187
      c_send:
4188
        mov     al, bl
4189
        out     0x64, al
4190
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4191
      c_accept:
4192
        in      al, 0x64
4193
        test    al, 2
4194
        jz      c_ok
4195
        loop    c_accept
4196
      c_error:
4197
        mov     ah, 1
4198
        jmp     c_exit
4199
      c_ok:
4200
        xor     ah, ah
4201
      c_exit:
4202
        ret
4203
 
4204
 
4205
setmouse:  ; set mousepicture -pointer
4206
           ; ps2 mouse enable
4207
 
2430 mario79 4208
;        mov     [MOUSE_PICTURE], dword mousepointer
2288 clevermous 4209
 
4210
        cli
4211
 
4212
        ret
4213
 
4214
if used _rdtsc
4215
_rdtsc:
4216
        bt      [cpu_caps], CAPS_TSC
4217
        jnc     ret_rdtsc
4218
        rdtsc
4219
        ret
4220
   ret_rdtsc:
4221
        mov     edx, 0xffffffff
4222
        mov     eax, 0xffffffff
4223
        ret
4224
end if
4225
 
4226
sys_msg_board_str:
4227
 
4228
        pushad
4229
   @@:
4230
        cmp     [esi], byte 0
4231
        je      @f
4232
        mov     eax, 1
4233
        movzx   ebx, byte [esi]
4234
        call    sys_msg_board
4235
        inc     esi
4236
        jmp     @b
4237
   @@:
4238
        popad
4239
        ret
4240
 
4241
sys_msg_board_byte:
4242
; in: al = byte to display
4243
; out: nothing
4244
; destroys: nothing
4245
        pushad
4246
        mov     ecx, 2
4247
        shl     eax, 24
4248
        jmp     @f
4249
 
4250
sys_msg_board_word:
4251
; in: ax = word to display
4252
; out: nothing
4253
; destroys: nothing
4254
        pushad
4255
        mov     ecx, 4
4256
        shl     eax, 16
4257
        jmp     @f
4258
 
4259
sys_msg_board_dword:
4260
; in: eax = dword to display
4261
; out: nothing
4262
; destroys: nothing
4263
        pushad
4264
        mov     ecx, 8
4265
@@:
4266
        push    ecx
4267
        rol     eax, 4
4268
        push    eax
4269
        and     al, 0xF
4270
        cmp     al, 10
4271
        sbb     al, 69h
4272
        das
4273
        mov     bl, al
4274
        xor     eax, eax
4275
        inc     eax
4276
        call    sys_msg_board
4277
        pop     eax
4278
        pop     ecx
4279
        loop    @b
4280
        popad
4281
        ret
4282
 
4283
uglobal
4284
  msg_board_data:
4285
                  times 4096 db 0
4286
  msg_board_count dd 0x0
4287
endg
4288
 
4289
sys_msg_board:
4290
 
4291
; eax=1 : write :  bl byte to write
4292
; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
4293
 
4294
        mov     ecx, [msg_board_count]
4295
        cmp     eax, 1
4296
        jne     .smbl1
4297
 
4298
if defined debug_com_base
4299
 
4300
        push    dx ax
4301
 
4302
       @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
4303
        mov     dx, debug_com_base+5
4304
        in      al, dx
4305
        test    al, 1 shl 5
4306
        jz      @r
4307
 
4308
        mov     dx, debug_com_base      ; Output the byte
4309
        mov     al, bl
4310
        out     dx, al
4311
 
4312
        pop     ax dx
4313
 
4314
end if
4315
 
4316
        mov     [msg_board_data+ecx], bl
4317
        inc     ecx
4318
        and     ecx, 4095
4319
        mov     [msg_board_count], ecx
4320
        mov     [check_idle_semaphore], 5
4321
        ret
4322
.smbl1:
4323
        cmp     eax, 2
4324
        jne     .smbl2
4325
        test    ecx, ecx
4326
        jz      .smbl21
4327
        mov     eax, msg_board_data+1
4328
        mov     ebx, msg_board_data
4329
        movzx   edx, byte [ebx]
4330
        call    memmove
4331
        dec     [msg_board_count]
4332
        mov     [esp + 36], edx ;eax
4333
        mov     [esp + 24], dword 1
4334
        ret
4335
.smbl21:
4336
        mov     [esp+36], ecx
4337
        mov     [esp+24], ecx
4338
.smbl2:
4339
        ret
4340
 
4341
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4342
;; 66 sys function.                                                ;;
4343
;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
4344
;; out eax                                                         ;;
4345
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4346
iglobal
4347
align 4
4348
f66call:
4349
           dd sys_process_def.1   ; 1 = set keyboard mode
4350
           dd sys_process_def.2   ; 2 = get keyboard mode
4351
           dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
4352
           dd sys_process_def.4
4353
           dd sys_process_def.5
4354
endg
4355
 
4356
 
4357
 
4358
 
4359
sys_process_def:
4360
        dec     ebx
4361
        cmp     ebx, 5
4362
        jae     .not_support    ;if >=6 then or eax,-1
4363
 
4364
        mov     edi, [CURRENT_TASK]
4365
        jmp     dword [f66call+ebx*4]
4366
 
4367
.not_support:
4368
        or      eax, -1
4369
        ret
4370
 
4371
.1:
4372
        shl     edi, 8
4373
        mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
4374
 
4375
        ret
4376
 
4377
.2:                             ; 2 = get keyboard mode
4378
        shl     edi, 8
4379
        movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
4380
        mov     [esp+32], eax
4381
        ret
4382
;     xor   eax,eax
4383
;     movzx eax,byte [shift]
4384
;     movzx ebx,byte [ctrl]
4385
;     shl   ebx,2
4386
;     add   eax,ebx
4387
;     movzx ebx,byte [alt]
4388
;     shl   ebx,3
4389
;     add   eax,ebx
4390
.3:                             ;3 = get keyboard ctrl, alt, shift
4391
 ;// mike.dld [
4392
        mov     eax, [kb_state]
4393
 ;// mike.dld ]
4394
        mov     [esp+32], eax
4395
        ret
4396
 
4397
.4:
4398
        mov     eax, hotkey_list
4399
@@:
4400
        cmp     dword [eax+8], 0
4401
        jz      .found_free
4402
        add     eax, 16
4403
        cmp     eax, hotkey_list+16*256
4404
        jb      @b
4405
        mov     dword [esp+32], 1
4406
        ret
4407
.found_free:
4408
        mov     [eax+8], edi
4409
        mov     [eax+4], edx
4410
        movzx   ecx, cl
4411
        lea     ecx, [hotkey_scancodes+ecx*4]
4412
        mov     edx, [ecx]
4413
        mov     [eax], edx
4414
        mov     [ecx], eax
4415
        mov     [eax+12], ecx
4416
        jecxz   @f
4417
        mov     [edx+12], eax
4418
@@:
4419
        and     dword [esp+32], 0
4420
        ret
4421
 
4422
.5:
4423
        movzx   ebx, cl
4424
        lea     ebx, [hotkey_scancodes+ebx*4]
4425
        mov     eax, [ebx]
4426
.scan:
4427
        test    eax, eax
4428
        jz      .notfound
4429
        cmp     [eax+8], edi
4430
        jnz     .next
4431
        cmp     [eax+4], edx
4432
        jz      .found
4433
.next:
4434
        mov     eax, [eax]
4435
        jmp     .scan
4436
.notfound:
4437
        mov     dword [esp+32], 1
4438
        ret
4439
.found:
4440
        mov     ecx, [eax]
4441
        jecxz   @f
4442
        mov     edx, [eax+12]
4443
        mov     [ecx+12], edx
4444
@@:
4445
        mov     ecx, [eax+12]
4446
        mov     edx, [eax]
4447
        mov     [ecx], edx
4448
        xor     edx, edx
4449
        mov     [eax+4], edx
4450
        mov     [eax+8], edx
4451
        mov     [eax+12], edx
4452
        mov     [eax], edx
4453
        mov     [esp+32], edx
4454
        ret
4455
 
4456
 
4457
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4458
;; 61 sys function.                                                ;;
4459
;; in eax=61,ebx in [1..3]                                         ;;
4460
;; out eax                                                         ;;
4461
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4462
iglobal
4463
align 4
4464
f61call:
4465
           dd sys_gs.1   ; resolution
4466
           dd sys_gs.2   ; bits per pixel
4467
           dd sys_gs.3   ; bytes per scanline
4468
endg
4469
 
4470
 
4471
align 4
4472
 
4473
sys_gs:                         ; direct screen access
4474
        dec     ebx
4475
        cmp     ebx, 2
4476
        ja      .not_support
4477
        jmp     dword [f61call+ebx*4]
4478
.not_support:
4479
        or      [esp+32], dword -1
4480
        ret
4481
 
4482
 
4483
.1:                             ; resolution
4484
        mov     eax, [Screen_Max_X]
4485
        shl     eax, 16
4486
        mov     ax, [Screen_Max_Y]
4487
        add     eax, 0x00010001
4488
        mov     [esp+32], eax
4489
        ret
4490
.2:                             ; bits per pixel
4491
        movzx   eax, byte [ScreenBPP]
4492
        mov     [esp+32], eax
4493
        ret
4494
.3:                             ; bytes per scanline
4495
        mov     eax, [BytesPerScanLine]
4496
        mov     [esp+32], eax
4497
        ret
4498
 
4499
align 4  ;  system functions
4500
 
4501
syscall_setpixel:                       ; SetPixel
4502
 
4503
        mov     eax, ebx
4504
        mov     ebx, ecx
4505
        mov     ecx, edx
4506
        mov     edx, [TASK_BASE]
4507
        add     eax, [edx-twdw+WDATA.box.left]
4508
        add     ebx, [edx-twdw+WDATA.box.top]
4509
        mov     edi, [current_slot]
4510
        add     eax, [edi+APPDATA.wnd_clientbox.left]
4511
        add     ebx, [edi+APPDATA.wnd_clientbox.top]
4512
        xor     edi, edi ; no force
2430 mario79 4513
        and     ecx, 0xFBFFFFFF  ;negate 0x04000000 save to mouseunder area
2288 clevermous 4514
        jmp     [putpixel]
4515
 
4516
align 4
4517
 
4518
syscall_writetext:                      ; WriteText
4519
 
4520
        mov     eax, [TASK_BASE]
4521
        mov     ebp, [eax-twdw+WDATA.box.left]
4522
        push    esi
4523
        mov     esi, [current_slot]
4524
        add     ebp, [esi+APPDATA.wnd_clientbox.left]
4525
        shl     ebp, 16
4526
        add     ebp, [eax-twdw+WDATA.box.top]
4527
        add     bp, word[esi+APPDATA.wnd_clientbox.top]
4528
        pop     esi
4529
        add     ebx, ebp
4530
        mov     eax, edi
4531
        xor     edi, edi
4532
        jmp     dtext
4533
 
4534
align 4
4535
 
4536
syscall_openramdiskfile:                ; OpenRamdiskFile
4537
 
4538
        mov     eax, ebx
4539
        mov     ebx, ecx
4540
        mov     ecx, edx
4541
        mov     edx, esi
4542
        mov     esi, 12
4543
        call    fileread
4544
        mov     [esp+32], eax
4545
        ret
4546
 
4547
align 4
4548
 
4549
syscall_drawrect:                       ; DrawRect
4550
 
4551
        mov     edi, edx ; color + gradient
4552
        and     edi, 0x80FFFFFF
4553
        test    bx, bx  ; x.size
4554
        je      .drectr
4555
        test    cx, cx ; y.size
4556
        je      .drectr
4557
 
4558
        mov     eax, ebx ; bad idea
4559
        mov     ebx, ecx
4560
 
4561
        movzx   ecx, ax ; ecx - x.size
4562
        shr     eax, 16 ; eax - x.coord
4563
        movzx   edx, bx ; edx - y.size
4564
        shr     ebx, 16 ; ebx - y.coord
4565
        mov     esi, [current_slot]
4566
 
4567
        add     eax, [esi + APPDATA.wnd_clientbox.left]
4568
        add     ebx, [esi + APPDATA.wnd_clientbox.top]
4569
        add     ecx, eax
4570
        add     edx, ebx
4571
        jmp     [drawbar]
4572
.drectr:
4573
        ret
4574
 
4575
align 4
4576
syscall_getscreensize:                  ; GetScreenSize
4577
        mov     ax, [Screen_Max_X]
4578
        shl     eax, 16
4579
        mov     ax, [Screen_Max_Y]
4580
        mov     [esp + 32], eax
4581
        ret
4582
 
4583
align 4
4584
 
4585
syscall_cdaudio:                        ; CD
4586
 
4587
        cmp     ebx, 4
4588
        jb      .audio
4589
        jz      .eject
4590
        cmp     ebx, 5
4591
        jnz     .ret
4592
.load:
4593
        call    .reserve
4594
        call    LoadMedium
4595
        ;call    .free
4596
        jmp     .free
4597
;        ret
4598
.eject:
4599
        call    .reserve
4600
        call    clear_CD_cache
4601
        call    allow_medium_removal
4602
        call    EjectMedium
4603
;        call    .free
4604
        jmp     .free
4605
;        ret
4606
.audio:
4607
        call    sys_cd_audio
4608
        mov     [esp+36-4], eax
4609
.ret:
4610
        ret
4611
 
4612
.reserve:
4613
        call    reserve_cd
4614
        mov     eax, ecx
4615
        shr     eax, 1
4616
        and     eax, 1
4617
        inc     eax
4618
        mov     [ChannelNumber], ax
4619
        mov     eax, ecx
4620
        and     eax, 1
4621
        mov     [DiskNumber], al
4622
        call    reserve_cd_channel
4623
        and     ebx, 3
4624
        inc     ebx
4625
        mov     [cdpos], ebx
4626
        add     ebx, ebx
4627
        mov     cl, 8
4628
        sub     cl, bl
4629
        mov     al, [DRIVE_DATA+1]
4630
        shr     al, cl
4631
        test    al, 2
4632
        jz      .free;.err
4633
        ret
4634
.free:
4635
        call    free_cd_channel
4636
        and     [cd_status], 0
4637
        ret
4638
.err:
4639
        call    .free
4640
;        pop     eax
4641
        ret
4642
 
4643
align 4
4644
 
4645
syscall_getpixel:                       ; GetPixel
4646
        mov     ecx, [Screen_Max_X]
4647
        inc     ecx
4648
        xor     edx, edx
4649
        mov     eax, ebx
4650
        div     ecx
4651
        mov     ebx, edx
4652
        xchg    eax, ebx
2430 mario79 4653
        and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
2288 clevermous 4654
        call    dword [GETPIXEL]; eax - x, ebx - y
4655
        mov     [esp + 32], ecx
4656
        ret
4657
 
4658
align 4
4659
 
4660
syscall_getarea:
4661
;eax = 36
4662
;ebx = pointer to bufer for img BBGGRRBBGGRR...
4663
;ecx = [size x]*65536 + [size y]
4664
;edx = [start x]*65536 + [start y]
4665
        pushad
4666
        mov     edi, ebx
4667
        mov     eax, edx
4668
        shr     eax, 16
4669
        mov     ebx, edx
4670
        and     ebx, 0xffff
4671
        dec     eax
4672
        dec     ebx
4673
     ; eax - x, ebx - y
4674
        mov     edx, ecx
4675
 
4676
        shr     ecx, 16
4677
        and     edx, 0xffff
4678
        mov     esi, ecx
4679
     ; ecx - size x, edx - size y
4680
 
4681
        mov     ebp, edx
4682
        dec     ebp
4683
        lea     ebp, [ebp*3]
4684
 
4685
        imul    ebp, esi
4686
 
4687
        mov     esi, ecx
4688
        dec     esi
4689
        lea     esi, [esi*3]
4690
 
4691
        add     ebp, esi
4692
        add     ebp, edi
4693
 
4694
        add     ebx, edx
4695
 
4696
.start_y:
4697
        push    ecx edx
4698
.start_x:
4699
        push    eax ebx ecx
4700
        add     eax, ecx
4701
 
2430 mario79 4702
        and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
2288 clevermous 4703
        call    dword [GETPIXEL]; eax - x, ebx - y
4704
 
4705
        mov     [ebp], cx
4706
        shr     ecx, 16
4707
        mov     [ebp+2], cl
4708
 
4709
        pop     ecx ebx eax
4710
        sub     ebp, 3
4711
        dec     ecx
4712
        jnz     .start_x
4713
        pop     edx ecx
4714
        dec     ebx
4715
        dec     edx
4716
        jnz     .start_y
4717
        popad
4718
        ret
4719
 
4720
align 4
4721
 
4722
syscall_drawline:                       ; DrawLine
4723
 
4724
        mov     edi, [TASK_BASE]
4725
        movzx   eax, word[edi-twdw+WDATA.box.left]
4726
        mov     ebp, eax
4727
        mov     esi, [current_slot]
4728
        add     ebp, [esi+APPDATA.wnd_clientbox.left]
4729
        add     ax, word[esi+APPDATA.wnd_clientbox.left]
4730
        add     ebp, ebx
4731
        shl     eax, 16
4732
        movzx   ebx, word[edi-twdw+WDATA.box.top]
4733
        add     eax, ebp
4734
        mov     ebp, ebx
4735
        add     ebp, [esi+APPDATA.wnd_clientbox.top]
4736
        add     bx, word[esi+APPDATA.wnd_clientbox.top]
4737
        add     ebp, ecx
4738
        shl     ebx, 16
4739
        xor     edi, edi
4740
        add     ebx, ebp
4741
        mov     ecx, edx
4742
        jmp     [draw_line]
4743
 
4744
 
4745
 
4746
align 4
4747
syscall_reserveportarea:                ; ReservePortArea and FreePortArea
4748
 
4749
        call    r_f_port_area
4750
        mov     [esp+32], eax
4751
        ret
4752
 
4753
align 4
4754
syscall_threads:                        ; CreateThreads
4755
; eax=1 create thread
4756
;
4757
;   ebx=thread start
4758
;   ecx=thread stack value
4759
;
4760
; on return : eax = pid
4761
 
4762
        call    new_sys_threads
4763
 
4764
        mov     [esp+32], eax
4765
        ret
4766
 
4767
align 4
4768
 
4769
stack_driver_stat:
4770
 
4771
        call    app_stack_handler       ; Stack status
4772
 
4773
;     mov   [check_idle_semaphore],5    ; enable these for zero delay
4774
;     call  change_task                 ; between sent packet
4775
 
4776
        mov     [esp+32], eax
4777
        ret
4778
 
4779
align 4
4780
 
4781
socket:                                 ; Socket interface
4782
        call    app_socket_handler
4783
 
4784
;     mov   [check_idle_semaphore],5    ; enable these for zero delay
4785
;     call  change_task                 ; between sent packet
4786
 
4787
        mov     [esp+36], eax
4788
        mov     [esp+24], ebx
4789
        ret
4790
 
4791
align 4
4792
 
4793
read_from_hd:                           ; Read from hd - fn not in use
4794
 
4795
        mov     edi, [TASK_BASE]
4796
        add     edi, TASKDATA.mem_start
4797
        add     eax, [edi]
4798
        add     ecx, [edi]
4799
        add     edx, [edi]
4800
        call    file_read
4801
 
4802
        mov     [esp+36], eax
4803
        mov     [esp+24], ebx
4804
 
4805
        ret
4806
 
4807
paleholder:
4808
        ret
2438 mario79 4809
;------------------------------------------------------------------------------
2288 clevermous 4810
align 4
2438 mario79 4811
calculate_fast_getting_offset_for_WinMapAddress:
4812
; calculate data area for fast getting offset to _WinMapAddress
2446 mario79 4813
        xor     eax, eax
2438 mario79 4814
        mov     ecx, [_display.height]
4815
        inc     ecx
4816
        mov     edi, d_width_calc_area
4817
        cld
4818
@@:
4819
        stosd
4820
        add     eax, [_display.width]
4821
        dec     ecx
4822
        jnz     @r
4823
        ret
4824
;------------------------------------------------------------------------------
4825
align 4
2288 clevermous 4826
set_screen:
4827
        cmp     eax, [Screen_Max_X]
4828
        jne     .set
4829
 
4830
        cmp     edx, [Screen_Max_Y]
4831
        jne     .set
4832
        ret
4833
.set:
4834
        pushfd
4835
        cli
4836
 
4837
        mov     [Screen_Max_X], eax
4838
        mov     [Screen_Max_Y], edx
4839
        mov     [BytesPerScanLine], ecx
4840
 
4841
        mov     [screen_workarea.right], eax
4842
        mov     [screen_workarea.bottom], edx
4843
 
4844
        push    ebx
4845
        push    esi
4846
        push    edi
4847
 
4848
        pushad
4849
 
4850
        stdcall kernel_free, [_WinMapAddress]
4851
 
4852
        mov     eax, [_display.width]
4853
        mul     [_display.height]
4854
        mov     [_WinMapSize], eax
4855
 
4856
        stdcall kernel_alloc, eax
4857
        mov     [_WinMapAddress], eax
4858
        test    eax, eax
4859
        jz      .epic_fail
4860
 
2438 mario79 4861
        call    calculate_fast_getting_offset_for_WinMapAddress
4862
 
2288 clevermous 4863
        popad
4864
 
4865
        call    repos_windows
4866
        xor     eax, eax
4867
        xor     ebx, ebx
4868
        mov     ecx, [Screen_Max_X]
4869
        mov     edx, [Screen_Max_Y]
4870
        call    calculatescreen
4871
        pop     edi
4872
        pop     esi
4873
        pop     ebx
4874
 
4875
        popfd
4876
        ret
4877
 
4878
.epic_fail:
4879
        hlt                     ; Houston, we've had a problem
4880
 
4881
; --------------- APM ---------------------
4882
uglobal
4883
apm_entry       dp      0
4884
apm_vf          dd      0
4885
endg
4886
 
4887
align 4
4888
sys_apm:
4889
        xor     eax, eax
4890
        cmp     word [apm_vf], ax       ; Check APM BIOS enable
4891
        jne     @f
4892
        inc     eax
4893
        or      dword [esp + 44], eax   ; error
4894
        add     eax, 7
4895
        mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
4896
        ret
4897
 
4898
@@:
4899
;       xchg    eax, ecx
4900
;       xchg    ebx, ecx
4901
 
4902
        cmp     dx, 3
4903
        ja      @f
4904
        and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
4905
        mov     eax, [apm_vf]
4906
        mov     [esp + 32], eax
4907
        shr     eax, 16
4908
        mov     [esp + 28], eax
4909
        ret
4910
 
4911
@@:
4912
 
4913
        mov     esi, [master_tab+(OS_BASE shr 20)]
4914
        xchg    [master_tab], esi
4915
        push    esi
4916
        mov     edi, cr3
4917
        mov     cr3, edi                ;flush TLB
4918
 
4919
        call    pword [apm_entry]       ;call APM BIOS
4920
 
4921
        xchg    eax, [esp]
4922
        mov     [master_tab], eax
4923
        mov     eax, cr3
4924
        mov     cr3, eax
4925
        pop     eax
4926
 
4927
        mov     [esp + 4 ], edi
4928
        mov     [esp + 8], esi
4929
        mov     [esp + 20], ebx
4930
        mov     [esp + 24], edx
4931
        mov     [esp + 28], ecx
4932
        mov     [esp + 32], eax
4933
        setc    al
4934
        and     [esp + 44], byte 0xfe
4935
        or      [esp + 44], al
4936
        ret
4937
; -----------------------------------------
4938
 
4939
align 4
4940
 
4941
undefined_syscall:                      ; Undefined system call
4942
        mov     [esp + 32], dword -1
4943
        ret
4944
 
4945
align 4
4946
system_shutdown:          ; shut down the system
4947
 
4948
        cmp     byte [BOOT_VAR+0x9030], 1
4949
        jne     @F
4950
        ret
4951
@@:
4952
        call    stop_all_services
4953
        push    3                ; stop playing cd
4954
        pop     eax
4955
        call    sys_cd_audio
4956
 
4957
yes_shutdown_param:
4958
        cli
4959
 
4960
if ~ defined extended_primary_loader
4961
        mov     eax, kernel_file ; load kernel.mnt to 0x7000:0
4962
        push    12
4963
        pop     esi
4964
        xor     ebx, ebx
4965
        or      ecx, -1
4966
        mov     edx, OS_BASE+0x70000
4967
        call    fileread
4968
 
4969
        mov     esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
4970
        mov     edi, OS_BASE+0x40000
4971
        mov     ecx, 1000
4972
        rep movsb
4973
end if
4974
 
4975
        mov     esi, BOOT_VAR    ; restore 0x0 - 0xffff
4976
        mov     edi, OS_BASE
4977
        mov     ecx, 0x10000/4
4978
        cld
4979
        rep movsd
4980
 
4981
        call    restorefatchain
4982
 
4983
        call    IRQ_mask_all
4984
 
4985
if 0
4986
        mov     word [OS_BASE+0x467+0], pr_mode_exit
4987
        mov     word [OS_BASE+0x467+2], 0x1000
4988
 
4989
        mov     al, 0x0F
4990
        out     0x70, al
4991
        mov     al, 0x05
4992
        out     0x71, al
4993
 
4994
        mov     al, 0xFE
4995
        out     0x64, al
4996
 
4997
        hlt
4998
        jmp     $-1
4999
 
5000
else
5001
        cmp     byte [OS_BASE + 0x9030], 2
5002
        jnz     no_acpi_power_off
5003
 
5004
; scan for RSDP
5005
; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
5006
        movzx   eax, word [OS_BASE + 0x40E]
5007
        shl     eax, 4
5008
        jz      @f
5009
        mov     ecx, 1024/16
5010
        call    scan_rsdp
5011
        jnc     .rsdp_found
5012
@@:
5013
; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
5014
        mov     eax, 0xE0000
5015
        mov     ecx, 0x2000
5016
        call    scan_rsdp
5017
        jc      no_acpi_power_off
5018
.rsdp_found:
5019
        mov     esi, [eax+16]   ; esi contains physical address of the RSDT
5020
        mov     ebp, [ipc_tmp]
5021
        stdcall map_page, ebp, esi, PG_MAP
5022
        lea     eax, [esi+1000h]
5023
        lea     edx, [ebp+1000h]
5024
        stdcall map_page, edx, eax, PG_MAP
5025
        and     esi, 0xFFF
5026
        add     esi, ebp
5027
        cmp     dword [esi], 'RSDT'
5028
        jnz     no_acpi_power_off
5029
        mov     ecx, [esi+4]
5030
        sub     ecx, 24h
5031
        jbe     no_acpi_power_off
5032
        shr     ecx, 2
5033
        add     esi, 24h
5034
.scan_fadt:
5035
        lodsd
5036
        mov     ebx, eax
5037
        lea     eax, [ebp+2000h]
5038
        stdcall map_page, eax, ebx, PG_MAP
5039
        lea     eax, [ebp+3000h]
5040
        add     ebx, 0x1000
5041
        stdcall map_page, eax, ebx, PG_MAP
5042
        and     ebx, 0xFFF
5043
        lea     ebx, [ebx+ebp+2000h]
5044
        cmp     dword [ebx], 'FACP'
5045
        jz      .fadt_found
5046
        loop    .scan_fadt
5047
        jmp     no_acpi_power_off
5048
.fadt_found:
5049
; ebx is linear address of FADT
5050
        mov     edi, [ebx+40] ; physical address of the DSDT
5051
        lea     eax, [ebp+4000h]
5052
        stdcall map_page, eax, edi, PG_MAP
5053
        lea     eax, [ebp+5000h]
5054
        lea     esi, [edi+0x1000]
5055
        stdcall map_page, eax, esi, PG_MAP
5056
        and     esi, 0xFFF
5057
        sub     edi, esi
5058
        cmp     dword [esi+ebp+4000h], 'DSDT'
5059
        jnz     no_acpi_power_off
5060
        mov     eax, [esi+ebp+4004h] ; DSDT length
5061
        sub     eax, 36+4
5062
        jbe     no_acpi_power_off
5063
        add     esi, 36
5064
.scan_dsdt:
5065
        cmp     dword [esi+ebp+4000h], '_S5_'
5066
        jnz     .scan_dsdt_cont
5067
        cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
5068
        jnz     .scan_dsdt_cont
5069
        mov     dl, [esi+ebp+4000h+6]
5070
        cmp     dl, 4 ; _S5_ package must contain 4 bytes
5071
                      ; ...in theory; in practice, VirtualBox has 2 bytes
5072
        ja      .scan_dsdt_cont
5073
        cmp     dl, 1
5074
        jb      .scan_dsdt_cont
5075
        lea     esi, [esi+ebp+4000h+7]
5076
        xor     ecx, ecx
5077
        cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
5078
        jz      @f
5079
        cmp     byte [esi], 0xA
5080
        jnz     no_acpi_power_off
5081
        inc     esi
5082
        mov     cl, [esi]
5083
@@:
5084
        inc     esi
5085
        cmp     dl, 2
5086
        jb      @f
5087
        cmp     byte [esi], 0
5088
        jz      @f
5089
        cmp     byte [esi], 0xA
5090
        jnz     no_acpi_power_off
5091
        inc     esi
5092
        mov     ch, [esi]
5093
@@:
5094
        jmp     do_acpi_power_off
5095
.scan_dsdt_cont:
5096
        inc     esi
5097
        cmp     esi, 0x1000
5098
        jb      @f
5099
        sub     esi, 0x1000
5100
        add     edi, 0x1000
5101
        push    eax
5102
        lea     eax, [ebp+4000h]
5103
        stdcall map_page, eax, edi, PG_MAP
5104
        push    PG_MAP
5105
        lea     eax, [edi+1000h]
5106
        push    eax
5107
        lea     eax, [ebp+5000h]
5108
        push    eax
5109
        stdcall map_page
5110
        pop     eax
5111
@@:
5112
        dec     eax
5113
        jnz     .scan_dsdt
5114
        jmp     no_acpi_power_off
5115
do_acpi_power_off:
5116
        mov     edx, [ebx+48]
5117
        test    edx, edx
5118
        jz      .nosmi
5119
        mov     al, [ebx+52]
5120
        out     dx, al
5121
        mov     edx, [ebx+64]
5122
@@:
5123
        in      ax, dx
5124
        test    al, 1
5125
        jz      @b
5126
.nosmi:
5127
        and     cx, 0x0707
5128
        shl     cx, 2
5129
        or      cx, 0x2020
5130
        mov     edx, [ebx+64]
5131
        in      ax, dx
5132
        and     ax, 203h
5133
        or      ah, cl
5134
        out     dx, ax
5135
        mov     edx, [ebx+68]
5136
        test    edx, edx
5137
        jz      @f
5138
        in      ax, dx
5139
        and     ax, 203h
5140
        or      ah, ch
5141
        out     dx, ax
5142
@@:
5143
        jmp     $
5144
 
5145
 
5146
no_acpi_power_off:
5147
        mov     word [OS_BASE+0x467+0], pr_mode_exit
5148
        mov     word [OS_BASE+0x467+2], 0x1000
5149
 
5150
        mov     al, 0x0F
5151
        out     0x70, al
5152
        mov     al, 0x05
5153
        out     0x71, al
5154
 
5155
        mov     al, 0xFE
5156
        out     0x64, al
5157
 
5158
        hlt
5159
        jmp     $-1
5160
 
5161
scan_rsdp:
5162
        add     eax, OS_BASE
5163
.s:
5164
        cmp     dword [eax], 'RSD '
5165
        jnz     .n
5166
        cmp     dword [eax+4], 'PTR '
5167
        jnz     .n
5168
        xor     edx, edx
5169
        xor     esi, esi
5170
@@:
5171
        add     dl, [eax+esi]
5172
        inc     esi
5173
        cmp     esi, 20
5174
        jnz     @b
5175
        test    dl, dl
5176
        jz      .ok
5177
.n:
5178
        add     eax, 10h
5179
        loop    .s
5180
        stc
5181
.ok:
5182
        ret
5183
end if
5184
 
5185
include "data32.inc"
5186
 
5187
__REV__ = __REV
5188
 
5189
uglobals_size = $ - endofcode
5190
diff16 "end of kernel code",0,$