Subversion Repositories Kolibri OS

Rev

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

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