Subversion Repositories Kolibri OS

Rev

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

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