Subversion Repositories Kolibri OS

Rev

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

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