Subversion Repositories Kolibri OS

Rev

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

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