Subversion Repositories Kolibri OS

Rev

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