Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;
9958 dunkaist 3
;; Copyright (C) KolibriOS team 2004-2024. All rights reserved.
9947 ace_dent 4
;; Kernel programmers are acknowledged in CREDITS.TXT
2288 clevermous 5
;;
6
;; Data in this file was originally part of MenuetOS project which is
7
;; distributed under the terms of GNU GPL. It is modified and redistributed as
8
;; part of KolibriOS project under the terms of GNU GPL.
9
;;
10
;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa
11
;;
12
;; KolibriOS is distributed in the hope that it will be useful, but WITHOUT ANY
13
;; WARRANTY. No author or distributor accepts responsibility to anyone for the
14
;; consequences of using it or for whether it serves any particular purpose or
15
;; works at all, unless he says so in writing. Refer to the GNU General Public
16
;; License (the "GPL") for full details.
17
;
18
;; Everyone is granted permission to copy, modify and redistribute KolibriOS,
19
;; but only under the conditions described in the GPL. A copy of this license
20
;; is supposed to have been given to you along with KolibriOS so you can know
21
;; your rights and responsibilities. It should be in a file named COPYING.
22
;; Among other things, the copyright notice and this notice must be preserved
23
;; on all copies.
24
;;
25
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26
 
3383 hidnplayr 27
format binary as "mnt"
28
 
2288 clevermous 29
include 'macros.inc'
2381 hidnplayr 30
include 'struct.inc'
2288 clevermous 31
 
32
$Revision: 10002 $
33
 
34
 
7136 dunkaist 35
USE_COM_IRQ     = 1      ; make irq 3 and irq 4 available for PCI devices
36
VESA_1_2_VIDEO  = 0      ; enable vesa 1.2 bank switch functions
2288 clevermous 37
 
38
; Enabling the next line will enable serial output console
9958 dunkaist 39
;debug_com_base  = 0x3f8  ; 0x3f8 is com1, 0x2f8 is com2, 0x3e8 is com3,
40
                          ; 0x2e8 is com4, no irq's are used
2288 clevermous 41
 
42
include "proc32.inc"
43
include "kglobals.inc"
44
include "lang.inc"
3539 clevermous 45
include "encoding.inc"
2288 clevermous 46
 
47
include "const.inc"
3777 yogev_ezra 48
 
49
iglobal
50
; The following variable, if equal to 1, duplicates debug output to the screen.
51
debug_direct_print db 0
52
; Start the first app (LAUNCHER) after kernel is loaded? (1=yes, 2 or 0=no)
53
launcher_start db 1
54
endg
55
 
7136 dunkaist 56
max_processes  =  255
57
tss_step       =  128 + 8192 ; tss & i/o - 65535 ports, * 256=557056*4
2288 clevermous 58
 
7136 dunkaist 59
os_stack       =  os_data_l - gdts    ; GDTs
60
os_code        =  os_code_l - gdts
61
graph_data     =  3 + graph_data_l - gdts
62
tss0           =  tss0_l - gdts
63
app_code       =  3 + app_code_l - gdts
64
app_data       =  3 + app_data_l - gdts
65
app_tls        =  3 + tls_data_l - gdts
66
pci_code_sel   =  pci_code_32-gdts
67
pci_data_sel   =  pci_data_32-gdts
2288 clevermous 68
 
69
 
70
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
71
;;
72
;;   Included files:
73
;;
74
;;   Kernel16.inc
75
;;    - Booteng.inc   English text for bootup
76
;;    - Bootcode.inc  Hardware setup
77
;;    - Pci16.inc     PCI functions
78
;;
79
;;   Kernel32.inc
80
;;    - Sys32.inc     Process management
81
;;    - Shutdown.inc  Shutdown and restart
82
;;    - Fat32.inc     Read / write hd
83
;;    - Vesa12.inc    Vesa 1.2 driver
84
;;    - Vesa20.inc    Vesa 2.0 driver
85
;;    - Vga.inc       VGA driver
86
;;    - Stack.inc     Network interface
87
;;    - Mouse.inc     Mouse pointer
88
;;    - Scincode.inc  Window skinning
89
;;    - Pci32.inc     PCI functions
90
;;
91
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92
 
7129 dunkaist 93
; That is a loading and initialization code that also draws the blue screen
9958 dunkaist 94
; menu with svn revision number near top right corner of the screen.
95
include "bootbios.inc"
2288 clevermous 96
 
97
use32
9958 dunkaist 98
org $+KERNEL_BASE
2288 clevermous 99
 
100
align 4
101
B32:
102
        mov     ax, os_stack       ; Selector for os
103
        mov     ds, ax
104
        mov     es, ax
105
        mov     fs, ax
106
        mov     gs, ax
107
        mov     ss, ax
6244 serge 108
        mov     esp, TMP_STACK_TOP       ; Set stack
2288 clevermous 109
 
110
; CLEAR 0x280000 - HEAP_BASE
111
 
112
        xor     eax, eax
113
        mov     edi, CLEAN_ZONE
9715 Doczom 114
        mov     ecx, (HEAP_BASE - OS_BASE - CLEAN_ZONE) / 4
2288 clevermous 115
        cld
116
        rep stosd
117
 
9969 dunkaist 118
; Clear kernel undefined globals and some more (what exactly?)
9715 Doczom 119
        mov     edi, endofcode - OS_BASE
9969 dunkaist 120
        mov     ecx, 0x90000    ; why uglobals_size isn't enough?
2288 clevermous 121
        sub     ecx, edi
122
        shr     ecx, 2
123
        rep stosd
124
 
125
; SAVE & CLEAR 0-0xffff
126
 
127
        mov     edi, 0x1000
3732 Serge 128
        mov     ecx, 0x8000 / 4
2288 clevermous 129
        rep stosd
3732 Serge 130
        mov     edi, 0xa000
131
        mov     ecx, 0x6000 / 4
132
        rep stosd
2288 clevermous 133
 
134
        call    test_cpu
9715 Doczom 135
        bts     [cpu_caps - OS_BASE], CAPS_TSC    ;force use rdtsc
2288 clevermous 136
 
8111 dunkaist 137
        call    acpi_locate
2288 clevermous 138
        call    init_BIOS32
139
; MEMORY MODEL
140
        call    mem_test
141
        call    init_mem
142
        call    init_page_map
143
 
144
; ENABLE PAGING
145
 
9715 Doczom 146
        mov     eax, sys_proc - OS_BASE + PROC.pdt_0
2288 clevermous 147
        mov     cr3, eax
148
 
149
        mov     eax, cr0
9715 Doczom 150
        or      eax, CR0_PG + CR0_WP
2288 clevermous 151
        mov     cr0, eax
152
 
153
        lgdt    [gdts]
154
        jmp     pword os_code:high_code
155
 
156
align 4
157
bios32_entry    dd ?
158
tmp_page_tabs   dd ?
159
use16
5033 clevermous 160
ap_init16:
161
        cli
9715 Doczom 162
        lgdt    [cs:gdts_ap - ap_init16]
163
        mov     eax, [cs:cr3_ap - ap_init16]
5033 clevermous 164
        mov     cr3, eax
9715 Doczom 165
        mov     eax, [cs:cr4_ap - ap_init16]
5033 clevermous 166
        mov     cr4, eax
9715 Doczom 167
        mov     eax, CR0_PE + CR0_PG + CR0_WP
5033 clevermous 168
        mov     cr0, eax
169
        jmp     pword os_code:ap_init_high
170
align 16
171
gdts_ap:
172
        dw     gdte-gdts-1
173
        dd     gdts
174
        dw     0
175
cr3_ap  dd     ?
176
cr4_ap  dd     ?
177
ap_init16_size = $ - ap_init16
2288 clevermous 178
use32
179
 
180
__DEBUG__ fix 1
181
__DEBUG_LEVEL__ fix 1
182
include 'init.inc'
183
 
184
org OS_BASE+$
185
 
2443 Serge 186
include 'fdo.inc'
187
 
2288 clevermous 188
align 4
189
high_code:
190
        mov     ax, os_stack
191
        mov     bx, app_data
192
        mov     cx, app_tls
193
        mov     ss, ax
194
        add     esp, OS_BASE
195
 
196
        mov     ds, bx
197
        mov     es, bx
198
        mov     fs, cx
199
        mov     gs, bx
200
 
5360 serge 201
        xor     eax, eax
9715 Doczom 202
        mov     ebx, 0xFFFFF000 + PG_SHARED + PG_NOCACHE + PG_UWR
5360 serge 203
        bt      [cpu_caps], CAPS_PAT
204
        setc    al
205
        shl     eax, 7
206
        or      ebx, eax
207
 
5359 serge 208
        mov     eax, PG_GLOBAL
2288 clevermous 209
        bt      [cpu_caps], CAPS_PGE
210
        jnc     @F
211
 
9715 Doczom 212
        or      [sys_proc + PROC.pdt_0 + (OS_BASE shr 20)], eax
5360 serge 213
        or      ebx, eax
2288 clevermous 214
 
5360 serge 215
        mov     eax, cr4
216
        or      eax, CR4_PGE
217
        mov     cr4, eax
2288 clevermous 218
@@:
5360 serge 219
        mov     [pte_valid_mask], ebx
220
 
2288 clevermous 221
        xor     eax, eax
9968 dunkaist 222
        mov     [sys_proc + PROC.pdt_0], eax
223
        mov     [sys_proc + PROC.pdt_0+4], eax
2288 clevermous 224
 
225
        mov     eax, cr3
226
        mov     cr3, eax          ; flush TLB
227
 
228
        mov     ecx, pg_data.mutex
229
        call    mutex_init
230
 
231
        mov     ecx, disk_list_mutex
232
        call    mutex_init
233
 
2601 clevermous 234
        mov     ecx, keyboard_list_mutex
235
        call    mutex_init
236
 
2489 mario79 237
        mov     ecx, unpack_mutex
238
        call    mutex_init
239
 
3534 clevermous 240
        mov     ecx, application_table_mutex
241
        call    mutex_init
242
 
3742 clevermous 243
        mov     ecx, ide_mutex
244
        call    mutex_init
245
        mov     ecx, ide_channel1_mutex
246
        call    mutex_init
247
        mov     ecx, ide_channel2_mutex
248
        call    mutex_init
4700 mario79 249
        mov     ecx, ide_channel3_mutex
250
        call    mutex_init
251
        mov     ecx, ide_channel4_mutex
252
        call    mutex_init
253
        mov     ecx, ide_channel5_mutex
254
        call    mutex_init
255
        mov     ecx, ide_channel6_mutex
256
        call    mutex_init
3774 mario79 257
;-----------------------------------------------------------------------------
2288 clevermous 258
; SAVE REAL MODE VARIABLES
3774 mario79 259
;-----------------------------------------------------------------------------
2288 clevermous 260
; --------------- APM ---------------------
261
 
262
; init selectors
7132 dunkaist 263
        mov     ebx, [BOOT.apm_entry]        ; offset of APM entry point
264
        movzx   eax, word [BOOT.apm_code_32] ; real-mode segment base address of
265
                                             ; protected-mode 32-bit code segment
266
        movzx   ecx, word [BOOT.apm_code_16] ; real-mode segment base address of
267
                                             ; protected-mode 16-bit code segment
268
        movzx   edx, word [BOOT.apm_data_16] ; real-mode segment base address of
269
                                             ; protected-mode 16-bit data segment
2288 clevermous 270
 
271
        shl     eax, 4
272
        mov     [dword apm_code_32 + 2], ax
273
        shr     eax, 16
274
        mov     [dword apm_code_32 + 4], al
275
 
276
        shl     ecx, 4
277
        mov     [dword apm_code_16 + 2], cx
278
        shr     ecx, 16
279
        mov     [dword apm_code_16 + 4], cl
280
 
281
        shl     edx, 4
282
        mov     [dword apm_data_16 + 2], dx
283
        shr     edx, 16
284
        mov     [dword apm_data_16 + 4], dl
285
 
286
        mov     dword[apm_entry], ebx
287
        mov     word [apm_entry + 4], apm_code_32 - gdts
288
 
7132 dunkaist 289
        mov     eax, dword[BOOT.apm_version]   ; version & flags
2288 clevermous 290
        mov     [apm_vf], eax
291
; -----------------------------------------
7132 dunkaist 292
        mov     al, [BOOT.dma]            ; DMA access
2288 clevermous 293
        mov     [allow_dma_access], al
6263 serge 294
 
7132 dunkaist 295
        mov     al, [BOOT.debug_print]    ; If nonzero, duplicates debug output to the screen
3777 yogev_ezra 296
        mov     [debug_direct_print], al
6263 serge 297
 
7132 dunkaist 298
        mov     al, [BOOT.launcher_start] ; Start the first app (LAUNCHER) after kernel is loaded?
3777 yogev_ezra 299
        mov     [launcher_start], al
2288 clevermous 300
 
8092 dunkaist 301
        mov     eax, [BOOT.devicesdat_size]
302
        mov     [acpi_dev_size], eax
303
        mov     eax, [BOOT.devicesdat_data]
304
        mov     [acpi_dev_data], eax
305
 
7132 dunkaist 306
        mov     esi, BOOT.bios_hd
2288 clevermous 307
        movzx   ecx, byte [esi-1]
308
        mov     [NumBiosDisks], ecx
309
        mov     edi, BiosDisksData
6843 dunkaist 310
        shl     ecx, 2
2288 clevermous 311
        rep movsd
312
 
313
; -------- Fast System Call init ----------
314
; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
315
        bt      [cpu_caps], CAPS_SEP
316
        jnc     .SEnP  ; SysEnter not Present
317
        xor     edx, edx
318
        mov     ecx, MSR_SYSENTER_CS
319
        mov     eax, os_code
320
        wrmsr
321
        mov     ecx, MSR_SYSENTER_ESP
322
;           mov eax, sysenter_stack ; Check it
323
        xor     eax, eax
324
        wrmsr
325
        mov     ecx, MSR_SYSENTER_EIP
326
        mov     eax, sysenter_entry
327
        wrmsr
328
.SEnP:
329
; AMD SYSCALL/SYSRET
330
        cmp     byte[cpu_vendor], 'A'
331
        jne     .noSYSCALL
332
        mov     eax, 0x80000001
333
        cpuid
334
        test    edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
335
        jz      .noSYSCALL
336
        mov     ecx, MSR_AMD_EFER
337
        rdmsr
338
        or      eax, 1 ; bit_0 - System Call Extension (SCE)
339
        wrmsr
340
 
341
        ; !!!! It`s dirty hack, fix it !!!
342
        ; Bits of EDX :
3539 clevermous 343
        ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
2288 clevermous 344
        ;  and the contents of this field, plus 8, are copied into the SS register.
3539 clevermous 345
        ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
2288 clevermous 346
        ;  and the contents of this field, plus 8, are copied into the SS register.
347
 
348
        ; mov   edx, (os_code + 16) * 65536 + os_code
349
        mov     edx, 0x1B0008
350
 
351
        mov     eax, syscall_entry
352
        mov     ecx, MSR_AMD_STAR
353
        wrmsr
354
.noSYSCALL:
355
; -----------------------------------------
356
        stdcall alloc_page
5356 serge 357
        stdcall map_page, tss-0xF80, eax, PG_SWR
2288 clevermous 358
        stdcall alloc_page
5356 serge 359
        stdcall map_page, tss+0x80, eax, PG_SWR
2288 clevermous 360
        stdcall alloc_page
5356 serge 361
        stdcall map_page, tss+0x1080, eax, PG_SWR
2288 clevermous 362
 
363
; LOAD IDT
364
 
365
        call    build_interrupt_table ;lidt is executed
366
          ;lidt [idtreg]
367
 
368
        call    init_kernel_heap
7276 dunkaist 369
        call    init_fpu
370
        mov     eax, [xsave_area_size]
371
        lea     eax, [eax*2 + RING0_STACK_SIZE*2]
372
        stdcall kernel_alloc, eax
2288 clevermous 373
        mov     [os_stack_seg], eax
374
 
9715 Doczom 375
        lea     esp, [eax + RING0_STACK_SIZE]
2288 clevermous 376
 
377
        mov     [tss._ss0], os_stack
378
        mov     [tss._esp0], esp
379
        mov     [tss._esp], esp
380
        mov     [tss._cs], os_code
381
        mov     [tss._ss], os_stack
382
        mov     [tss._ds], app_data
383
        mov     [tss._es], app_data
384
        mov     [tss._fs], app_data
385
        mov     [tss._gs], app_data
386
        mov     [tss._io], 128
387
;Add IO access table - bit array of permitted ports
388
        mov     edi, tss._io_map_0
389
        xor     eax, eax
390
        not     eax
391
        mov     ecx, 8192/4
392
        rep stosd                    ; access to 4096*8=65536 ports
393
 
394
        mov     ax, tss0
395
        ltr     ax
396
 
6263 serge 397
        mov     eax, sys_proc
398
        list_init eax
399
        add     eax, PROC.thr_list
400
        list_init eax
401
 
402
        call    init_video
9950 turbocat 403
        call    init_pat_mtrr
6263 serge 404
        mov     [LFBAddress], LFB_BASE
405
        mov     ecx, bios_fb
406
        call    set_framebuffer
2288 clevermous 407
        call    init_malloc
408
 
5130 serge 409
        stdcall alloc_kernel_space, 0x50000         ; FIXME check size
2288 clevermous 410
        mov     [default_io_map], eax
411
 
412
        add     eax, 0x2000
413
        mov     [ipc_tmp], eax
414
        mov     ebx, 0x1000
415
 
416
        add     eax, 0x40000
417
        mov     [proc_mem_map], eax
418
 
419
        add     eax, 0x8000
420
        mov     [proc_mem_pdir], eax
421
 
422
        add     eax, ebx
423
        mov     [proc_mem_tab], eax
424
 
425
        add     eax, ebx
426
        mov     [tmp_task_ptab], eax
427
 
428
        add     eax, ebx
429
        mov     [ipc_pdir], eax
430
 
431
        add     eax, ebx
432
        mov     [ipc_ptab], eax
433
 
9715 Doczom 434
        stdcall kernel_alloc, (unpack.LZMA_BASE_SIZE + (unpack.LZMA_LIT_SIZE shl \
435
                (unpack.lc + unpack.lp)))*4
2288 clevermous 436
 
437
        mov     [unpack.p], eax
438
 
439
        call    init_events
9715 Doczom 440
        mov     eax, srv.fd - SRV.fd
2288 clevermous 441
        mov     [srv.fd], eax
442
        mov     [srv.bk], eax
443
 
444
;Set base of graphic segment to linear address of LFB
445
        mov     eax, [LFBAddress]         ; set for gs
446
        mov     [graph_data_l+2], ax
447
        shr     eax, 16
448
        mov     [graph_data_l+4], al
449
        mov     [graph_data_l+7], ah
450
 
5351 serge 451
        stdcall kernel_alloc, [_display.win_map_size]
452
        mov     [_display.win_map], eax
2288 clevermous 453
 
454
        xor     eax, eax
455
        inc     eax
456
 
457
; set background
458
 
459
        mov     [BgrDrawMode], eax
460
        mov     [BgrDataWidth], eax
461
        mov     [BgrDataHeight], eax
462
        mov     [mem_BACKGROUND], 4
463
        mov     [img_background], static_background_data
464
 
4199 mario79 465
; set clipboard
466
 
467
        xor     eax, eax
468
        mov     [clipboard_slots], eax
469
        mov     [clipboard_write_lock], eax
9926 Doczom 470
        stdcall kernel_alloc, PAGE_SIZE
4199 mario79 471
        test    eax, eax
472
        jnz     @f
473
 
474
        dec     eax
475
@@:
476
        mov     [clipboard_main_list], eax
477
 
8111 dunkaist 478
        call    check_acpi
479
 
5787 serge 480
        mov     eax, [hpet_base]
481
        test    eax, eax
482
        jz      @F
9715 Doczom 483
        stdcall map_io_mem, [hpet_base], 1024, PG_GLOBAL + PAT_UC + PG_SWR
5787 serge 484
        mov     [hpet_base], eax
8111 dunkaist 485
        mov     eax, [eax+HPET_ID]
5787 serge 486
        DEBUGF  1, "K : HPET caps %x\n", eax
8111 dunkaist 487
        call    init_hpet
5787 serge 488
@@:
3296 clevermous 489
; SET UP OS TASK
490
 
491
        mov     esi, boot_setostask
492
        call    boot_log
493
 
9715 Doczom 494
        mov     edi, sys_proc + PROC.heap_lock
495
        mov     ecx, (PROC.ht_free - PROC.heap_lock)/4
5130 serge 496
 
5595 serge 497
        xor     eax, eax
498
        cld
499
        rep stosd
500
 
501
        mov     [edi], dword (PROC.pdt_0 - PROC.htab)/4 - 3
502
        mov     [edi+4], dword 3           ;reserve handles for stdin stdout and stderr
503
        mov     ecx, (PROC.pdt_0 - PROC.htab)/4
504
        add     edi, 8
505
        inc     eax
506
@@:
507
        stosd
508
        inc     eax
509
        cmp     eax, ecx
510
        jbe     @B
511
 
9715 Doczom 512
        mov     [sys_proc + PROC.pdt_0_phys], sys_proc - OS_BASE + PROC.pdt_0
5595 serge 513
 
5130 serge 514
        mov     eax, -1
515
        mov     edi, thr_slot_map+4
516
        mov     [edi-4], dword 0xFFFFFFF8
517
        stosd
518
        stosd
519
        stosd
520
        stosd
521
        stosd
522
        stosd
523
        stosd
524
 
525
        mov     [current_process], sys_proc
526
 
9968 dunkaist 527
        ; set all the threads state to free
528
        mov     edi, SLOT_BASE
529
        movi    eax, TSTATE_FREE
530
        movi    ecx, max_processes
531
@@:
532
        mov     [edi+APPDATA.state], TSTATE_FREE
533
        add     edi, sizeof.APPDATA
534
        dec     ecx
535
        jns     @b
536
 
9715 Doczom 537
        mov     edx, SLOT_BASE + sizeof.APPDATA*1
3534 clevermous 538
        mov     ebx, [os_stack_seg]
7276 dunkaist 539
        add     ebx, RING0_STACK_SIZE
540
        add     ebx, [xsave_area_size]
3534 clevermous 541
        call    setup_os_slot
542
        mov     dword [edx], 'IDLE'
9715 Doczom 543
        sub     [edx + APPDATA.saved_esp], 4
544
        mov     eax, [edx + APPDATA.saved_esp]
3534 clevermous 545
        mov     dword [eax], idle_thread
546
        mov     ecx, IDLE_PRIORITY
547
        call    scheduler_add_thread
3296 clevermous 548
 
9715 Doczom 549
        mov     edx, SLOT_BASE + sizeof.APPDATA*2
3534 clevermous 550
        mov     ebx, [os_stack_seg]
551
        call    setup_os_slot
552
        mov     dword [edx], 'OS'
553
        xor     ecx, ecx
554
        call    scheduler_add_thread
3296 clevermous 555
 
9968 dunkaist 556
        mov     [current_slot_idx], 2
8867 rgimad 557
        mov     [thread_count], 2
9968 dunkaist 558
        mov     [current_slot], SLOT_BASE + sizeof.APPDATA*2
3296 clevermous 559
 
5033 clevermous 560
; Move other CPUs to deep sleep, if it is useful
561
uglobal
562
use_mwait_for_idle db 0
563
endg
564
        cmp     [cpu_vendor+8], 'ntel'
565
        jnz     .no_wake_cpus
566
        bt      [cpu_caps+4], CAPS_MONITOR-32
567
        jnc     .no_wake_cpus
568
        dbgstr 'using mwait for idle loop'
569
        inc     [use_mwait_for_idle]
570
        mov     ebx, [cpu_count]
571
        cmp     ebx, 1
572
        jbe     .no_wake_cpus
573
        call    create_trampoline_pgmap
9715 Doczom 574
        mov     [cr3_ap + OS_BASE], eax
5033 clevermous 575
        mov     eax, cr4
9715 Doczom 576
        mov     [cr4_ap + OS_BASE], eax
5033 clevermous 577
        mov     esi, OS_BASE + ap_init16
578
        mov     edi, OS_BASE + 8000h
579
        mov     ecx, (ap_init16_size + 3) / 4
580
        rep movsd
8119 dunkaist 581
        mov     eax, [LAPIC_BASE]
582
        test    eax, eax
583
        jnz     @f
9715 Doczom 584
        stdcall map_io_mem, [acpi_lapic_base], 0x1000, PG_GLOBAL + PG_NOCACHE + PG_SWR
5033 clevermous 585
        mov     [LAPIC_BASE], eax
8119 dunkaist 586
@@:
9715 Doczom 587
        lea     edi, [eax + APIC_ICRL]
5033 clevermous 588
        mov     esi, smpt+4
589
        dec     ebx
590
.wake_cpus_loop:
591
        lodsd
5049 clevermous 592
        push    esi
593
        xor     esi, esi
594
        inc     esi
5033 clevermous 595
        shl     eax, 24
596
        mov     [edi+10h], eax
597
; assert INIT IPI
598
        mov     dword [edi], 0C500h
5049 clevermous 599
        call    delay_ms
5033 clevermous 600
@@:
601
        test    dword [edi], 1000h
602
        jnz     @b
603
; deassert INIT IPI
604
        mov     dword [edi], 8500h
5049 clevermous 605
        call    delay_ms
5033 clevermous 606
@@:
607
        test    dword [edi], 1000h
608
        jnz     @b
609
; send STARTUP IPI
610
        mov     dword [edi], 600h + (8000h shr 12)
5049 clevermous 611
        call    delay_ms
5033 clevermous 612
@@:
613
        test    dword [edi], 1000h
614
        jnz     @b
5049 clevermous 615
        pop     esi
5033 clevermous 616
        dec     ebx
617
        jnz     .wake_cpus_loop
618
        mov     eax, [cpu_count]
619
        dec     eax
620
@@:
621
        cmp     [ap_initialized], eax
622
        jnz     @b
9715 Doczom 623
        mov     eax, [cr3_ap + OS_BASE]
5033 clevermous 624
        call    free_page
625
.no_wake_cpus:
2288 clevermous 626
 
627
; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
2642 mario79 628
        mov     esi, boot_initirq
629
        call    boot_log
630
        call    init_irqs
2288 clevermous 631
 
2642 mario79 632
        mov     esi, boot_picinit
633
        call    boot_log
2288 clevermous 634
        call    PIC_init
635
 
2642 mario79 636
        mov     esi, boot_v86machine
637
        call    boot_log
2288 clevermous 638
; Initialize system V86 machine
639
        call    init_sys_v86
640
 
2642 mario79 641
        mov     esi, boot_inittimer
642
        call    boot_log
2288 clevermous 643
; Initialize system timer (IRQ0)
644
        call    PIT_init
645
 
4273 clevermous 646
; Register ramdisk file system
8091 dunkaist 647
if ~ defined extended_primary_loader
8089 dunkaist 648
        cmp     [BOOT.rd_load_from], RD_LOAD_FROM_HD    ; will be loaded later
4772 mario79 649
        je      @f
8091 dunkaist 650
end if
8089 dunkaist 651
        cmp     [BOOT.rd_load_from], RD_LOAD_FROM_NONE
652
        je      @f
653
        call    register_ramdisk
4772 mario79 654
;--------------------------------------
655
@@:
2642 mario79 656
        mov     esi, boot_initapic
657
        call    boot_log
2288 clevermous 658
; Try to Initialize APIC
659
        call    APIC_init
660
 
2642 mario79 661
        mov     esi, boot_enableirq
662
        call    boot_log
3785 mario79 663
; Enable timer IRQ (IRQ0) and co-processor IRQ (IRQ13)
2288 clevermous 664
; they are used: when partitions are scanned, hd_read relies on timer
665
        call    unmask_timer
7733 dunkaist 666
        ; Prevent duplicate timer IRQs in APIC mode
667
        cmp     [irq_mode], IRQ_APIC
668
        jz      @f
2288 clevermous 669
        stdcall enable_irq, 2               ; @#$%! PIC
7733 dunkaist 670
@@:
2288 clevermous 671
        stdcall enable_irq, 13              ; co-processor
4741 hidnplayr 672
 
673
; Setup serial output console (if enabled)
674
if defined debug_com_base
675
 
676
        ; reserve port so nobody else will use it
677
        xor     ebx, ebx
678
        mov     ecx, debug_com_base
679
        mov     edx, debug_com_base+7
680
        call    r_f_port_area
681
 
682
        ; enable Divisor latch
683
        mov     dx, debug_com_base+3
684
        mov     al, 1 shl 7
685
        out     dx, al
686
 
687
        ; Set speed to 115200 baud (max speed)
688
        mov     dx, debug_com_base
689
        mov     al, 0x01
690
        out     dx, al
691
 
692
        mov     dx, debug_com_base+1
693
        mov     al, 0x00
694
        out     dx, al
695
 
696
        ; No parity, 8bits words, one stop bit, dlab bit back to 0
697
        mov     dx, debug_com_base+3
698
        mov     al, 3
699
        out     dx, al
700
 
701
        ; disable interrupts
702
        mov     dx, debug_com_base+1
703
        mov     al, 0
704
        out     dx, al
705
 
706
        ; clear +  enable fifo (64 bits)
707
        mov     dx, debug_com_base+2
708
        mov     al, 0x7 + 1 shl 5
709
        out     dx, al
710
 
711
end if
712
 
713
 
4575 mario79 714
;-----------------------------------------------------------------------------
715
; show SVN version of kernel on the message board
716
;-----------------------------------------------------------------------------
717
        mov     eax, [version_inf.rev]
718
        DEBUGF  1, "K : kernel SVN r%d\n", eax
719
;-----------------------------------------------------------------------------
720
; show CPU count on the message board
721
;-----------------------------------------------------------------------------
722
        mov     eax, [cpu_count]
723
        test    eax, eax
724
        jnz     @F
725
        mov     al, 1                             ; at least one CPU
726
@@:
727
        DEBUGF  1, "K : %d CPU detected\n", eax
728
;-----------------------------------------------------------------------------
729
; detect Floppy drives
730
;-----------------------------------------------------------------------------
731
        mov     esi, boot_detectfloppy
732
        call    boot_log
733
include 'detect/dev_fd.inc'
734
;-----------------------------------------------------------------------------
4624 mario79 735
; create pci-devices list
4575 mario79 736
;-----------------------------------------------------------------------------
4624 mario79 737
        mov     [pci_access_enabled], 1
738
        call    pci_enum
3762 mario79 739
;-----------------------------------------------------------------------------
4624 mario79 740
; initialisation IDE ATA code
4575 mario79 741
;-----------------------------------------------------------------------------
4624 mario79 742
include 'detect/init_ata.inc'
4575 mario79 743
;-----------------------------------------------------------------------------
9183 rgimad 744
; initialisation AHCI code
745
;-----------------------------------------------------------------------------
746
        jmp     ahci_code_end
747
include 'blkdev/ahci.inc'
748
ahci_code_end:
749
        call    ahci_init
750
;-----------------------------------------------------------------------------
2288 clevermous 751
if 0
8089 dunkaist 752
        mov     ax, [BOOT.sys_disk]
2288 clevermous 753
        cmp     ax, 'r1'; if using not ram disk, then load librares and parameters {SPraid.simba}
754
        je      no_lib_load
2642 mario79 755
 
756
        mov     esi, boot_loadlibs
757
        call    boot_log
2288 clevermous 758
; LOADING LIBRARES
759
        stdcall dll.Load, @IMPORT           ; loading librares for kernel (.obj files)
760
        call    load_file_parse_table       ; prepare file parse table
761
        call    set_kernel_conf             ; configure devices and gui
762
no_lib_load:
763
end if
764
 
765
; Display APIC status
766
        mov     esi, boot_APIC_found
767
        cmp     [irq_mode], IRQ_APIC
768
        je      @f
769
        mov     esi, boot_APIC_nfound
770
@@:
2642 mario79 771
        call    boot_log
2288 clevermous 772
 
773
; PRINT AMOUNT OF MEMORY
774
        mov     esi, boot_memdetect
775
        call    boot_log
776
 
777
        movzx   ecx, word [boot_y]
778
        if lang eq ru
779
        or      ecx, (10+30*6) shl 16
3274 esevece 780
        else if lang eq sp
781
        or      ecx, (10+33*6) shl 16
782
        else
2288 clevermous 783
        or      ecx, (10+29*6) shl 16
3274 esevece 784
        end if
2288 clevermous 785
        sub     ecx, 10
786
        mov     edx, 0xFFFFFF
787
        mov     ebx, [MEM_AMOUNT]
788
        shr     ebx, 20
789
        xor     edi, edi
790
        mov     eax, 0x00040000
791
        inc     edi
792
        call    display_number_force
793
 
794
; BUILD SCHEDULER
795
 
2642 mario79 796
;        call    build_scheduler; sys32.inc
2288 clevermous 797
 
2642 mario79 798
;        mov     esi, boot_devices
799
;        call    boot_log
2288 clevermous 800
 
4297 yogev_ezra 801
include "detect/vortex86.inc"                     ; Vortex86 SoC detection code
802
 
5039 clevermous 803
        stdcall load_pe_driver, szVidintel, 0
3508 clevermous 804
 
3520 clevermous 805
        call    usb_init
806
 
2288 clevermous 807
; SET PRELIMINARY WINDOW STACK AND POSITIONS
808
 
809
        mov     esi, boot_windefs
810
        call    boot_log
811
        call    set_window_defaults
812
 
813
; SET BACKGROUND DEFAULTS
814
 
815
        mov     esi, boot_bgr
816
        call    boot_log
817
        call    init_background
818
        call    calculatebackground
819
 
820
; RESERVE SYSTEM IRQ'S JA PORT'S
821
 
822
        mov     esi, boot_resirqports
823
        call    boot_log
824
        call    reserve_irqs_ports
825
 
9930 Doczom 826
        mov     [SLOT_BASE + APPDATA.window], window_data
827
        mov     [SLOT_BASE + sizeof.APPDATA + APPDATA.window], window_data + sizeof.WDATA
828
        mov     [SLOT_BASE + sizeof.APPDATA*2 + APPDATA.window], window_data + sizeof.WDATA*2
829
        mov     [window_data + WDATA.thread], SLOT_BASE
830
        mov     [window_data + sizeof.WDATA + WDATA.thread], SLOT_BASE + sizeof.APPDATA
831
        mov     [window_data + sizeof.WDATA*2 + WDATA.thread], SLOT_BASE + sizeof.APPDATA*2
832
 
2288 clevermous 833
        call    init_display
834
        mov     eax, [def_cursor]
9930 Doczom 835
        mov     [window_data + sizeof.WDATA + WDATA.cursor], eax
836
        mov     [window_data + sizeof.WDATA*2 + WDATA.cursor], eax
2288 clevermous 837
 
3501 Serge 838
; PRINT CPU FREQUENCY
2288 clevermous 839
 
3501 Serge 840
        mov     esi, boot_cpufreq
2288 clevermous 841
        call    boot_log
3501 Serge 842
 
5792 serge 843
        cli
844
        mov     ebx, [hpet_base]
845
        test    ebx, ebx
846
        jz      @F
9715 Doczom 847
        mov     ebx, [ebx + HPET_COUNTER]
5792 serge 848
 
3501 Serge 849
        rdtsc
5792 serge 850
        mov     ecx, 1000
851
        sub     eax, [hpet_tsc_start]
9715 Doczom 852
        sbb     edx, [hpet_tsc_start + 4]
5792 serge 853
        shld    edx, eax, 10
854
        shl     eax, 10
855
        mov     esi, eax
856
        mov     eax, edx
857
        mul     ecx
858
        xchg    eax, esi
859
        mul     ecx
860
        adc     edx, esi
861
        div     ebx
862
        mul     ecx
863
        div     [hpet_period]
864
        mul     ecx
865
        DEBUGF  1, "K : cpu frequency %u Hz\n", eax
866
        jmp     .next
867
@@:
868
        rdtsc
2288 clevermous 869
        mov     ecx, eax
870
        mov     esi, 250            ; wait 1/4 a second
871
        call    delay_ms
3501 Serge 872
        rdtsc
873
 
2288 clevermous 874
        sub     eax, ecx
3481 Serge 875
        xor     edx, edx
876
        shld    edx, eax, 2
2288 clevermous 877
        shl     eax, 2
5792 serge 878
.next:
3481 Serge 879
        mov     dword [cpu_freq], eax
9715 Doczom 880
        mov     dword [cpu_freq + 4], edx
3502 Serge 881
        mov     ebx, 1000000
882
        div     ebx
3501 Serge 883
        mov     ebx, eax
2288 clevermous 884
 
885
        movzx   ecx, word [boot_y]
886
        if lang eq ru
3501 Serge 887
        add     ecx, (10+19*6) shl 16 - 10
3274 esevece 888
        else if lang eq sp
3501 Serge 889
        add     ecx, (10+25*6) shl 16 - 10
3274 esevece 890
        else
3501 Serge 891
        add     ecx, (10+17*6) shl 16 - 10
3274 esevece 892
        end if
3501 Serge 893
 
2288 clevermous 894
        mov     edx, 0xFFFFFF
895
        xor     edi, edi
896
        mov     eax, 0x00040000
897
        inc     edi
898
        call    display_number_force
899
 
900
; SET VARIABLES
901
 
902
        call    set_variables
903
 
904
; STACK AND FDC
905
 
906
        call    stack_init
907
        call    fdc_init
908
 
909
; PALETTE FOR 320x200 and 640x480 16 col
910
 
911
        cmp     [SCR_MODE], word 0x12
912
        jne     no_pal_vga
913
        mov     esi, boot_pal_vga
914
        call    boot_log
915
        call    paletteVGA
916
      no_pal_vga:
917
 
918
        cmp     [SCR_MODE], word 0x13
919
        jne     no_pal_ega
920
        mov     esi, boot_pal_ega
921
        call    boot_log
922
        call    palette320x200
923
      no_pal_ega:
924
 
925
; LOAD DEFAULT SKIN
926
 
927
        call    load_default_skin
928
 
5635 hidnplayr 929
; Protect I/O permission map
2288 clevermous 930
 
931
        mov     esi, [default_io_map]
9715 Doczom 932
        stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
2288 clevermous 933
        add     esi, 0x1000
9715 Doczom 934
        stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
2288 clevermous 935
 
936
        stdcall map_page, tss._io_map_0, \
9715 Doczom 937
                [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
2288 clevermous 938
        stdcall map_page, tss._io_map_1, \
9715 Doczom 939
                [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
2288 clevermous 940
 
941
; SET KEYBOARD PARAMETERS
942
        mov     al, 0xf6       ; reset keyboard, scan enabled
5012 clevermous 943
        call    kb_write_wait_ack
2601 clevermous 944
        test    ah, ah
945
        jnz     .no_keyboard
2288 clevermous 946
 
2601 clevermous 947
iglobal
948
align 4
949
ps2_keyboard_functions:
950
        dd      .end - $
951
        dd      0       ; no close
952
        dd      ps2_set_lights
953
.end:
954
endg
955
        stdcall register_keyboard, ps2_keyboard_functions, 0
2288 clevermous 956
       ; mov   al, 0xED       ; Keyboard LEDs - only for testing!
5012 clevermous 957
       ; call  kb_write_wait_ack
2288 clevermous 958
       ; mov   al, 111b
5012 clevermous 959
       ; call  kb_write_wait_ack
2288 clevermous 960
 
961
        mov     al, 0xF3     ; set repeat rate & delay
5012 clevermous 962
        call    kb_write_wait_ack
2288 clevermous 963
        mov     al, 0; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
5012 clevermous 964
        call    kb_write_wait_ack
2288 clevermous 965
     ;// mike.dld [
966
        call    set_lights
967
     ;// mike.dld ]
968
        stdcall attach_int_handler, 1, irq1, 0
4734 mario79 969
        DEBUGF  1, "K : IRQ1 return code %x\n", eax
2601 clevermous 970
.no_keyboard:
2288 clevermous 971
 
4553 hidnplayr 972
; Load PS/2 mouse driver
2288 clevermous 973
        mov     esi, boot_setmouse
974
        call    boot_log
9514 rgimad 975
        stdcall load_pe_driver, szPS2MDriver, 0
2288 clevermous 976
 
5031 clevermous 977
; LOAD FIRST APPLICATION
978
        cmp     byte [launcher_start], 1        ; Check if starting LAUNCHER is selected on blue screen (1 = yes)
979
        jnz     first_app_found
980
 
981
        cli
982
        mov     ebp, firstapp
983
        call    fs_execute_from_sysdir
984
        test    eax, eax
985
        jns     first_app_found
986
 
987
        mov     esi, boot_failed
988
        call    boot_log
989
 
990
        mov     eax, 0xDEADBEEF      ; otherwise halt
991
        hlt
992
 
993
first_app_found:
994
 
2288 clevermous 995
; START MULTITASKING
7129 dunkaist 996
preboot_blogesc = 0       ; start immediately after bootlog
2288 clevermous 997
 
998
; A 'All set - press ESC to start' messages if need
999
if preboot_blogesc
1000
        mov     esi, boot_tasking
1001
        call    boot_log
1002
.bll1:
1003
        in      al, 0x60        ; wait for ESC key press
1004
        cmp     al, 129
1005
        jne     .bll1
1006
end if
1007
 
1008
        mov     [timer_ticks_enable], 1         ; for cd driver
1009
        sti
4734 mario79 1010
 
4608 clevermous 1011
        call    mtrr_validate
1012
 
2288 clevermous 1013
        jmp     osloop
1014
        ; Fly :)
1015
 
5033 clevermous 1016
uglobal
1017
align 4
1018
ap_initialized  dd      0
1019
endg
1020
 
1021
ap_init_high:
1022
        mov     ax, os_stack
1023
        mov     bx, app_data
1024
        mov     cx, app_tls
1025
        mov     ss, ax
1026
        mov     ds, bx
1027
        mov     es, bx
1028
        mov     fs, cx
1029
        mov     gs, bx
1030
        xor     esp, esp
9715 Doczom 1031
        mov     eax, sys_proc - OS_BASE + PROC.pdt_0
5033 clevermous 1032
        mov     cr3, eax
1033
        lock inc [ap_initialized]
1034
        jmp     idle_loop
1035
 
1036
 
2288 clevermous 1037
include 'unpacker.inc'
1038
 
1039
align 4
1040
boot_log:
1041
        pushad
1042
 
1043
        mov     ebx, 10*65536
1044
        mov     bx, word [boot_y]
1045
        add     [boot_y], dword 10
1046
        mov     ecx, 0x80ffffff; ASCIIZ string with white color
1047
        xor     edi, edi
1048
        mov     edx, esi
1049
        inc     edi
1050
        call    dtext
1051
 
1052
        mov     [novesachecksum], 1000
1053
        call    checkVga_N13
1054
 
1055
        popad
1056
 
1057
        ret
1058
 
8091 dunkaist 1059
;-----------------------------------------------------------------------------
1060
; Register ramdisk file system
1061
register_ramdisk:
1062
        mov     esi, boot_initramdisk
1063
        call    boot_log
1064
        call    ramdisk_init
1065
        ret
1066
 
3534 clevermous 1067
; in: edx -> APPDATA for OS/IDLE slot
1068
; in: ebx = stack base
1069
proc setup_os_slot
1070
        xor     eax, eax
8093 dunkaist 1071
        mov     ecx, sizeof.APPDATA/4
3534 clevermous 1072
        mov     edi, edx
1073
        rep stosd
1074
 
3535 clevermous 1075
        mov     eax, tss+0x80
1076
        call    get_pg_addr
1077
        inc     eax
9715 Doczom 1078
        mov     [edx + APPDATA.io_map], eax
3535 clevermous 1079
        mov     eax, tss+0x1080
1080
        call    get_pg_addr
1081
        inc     eax
9715 Doczom 1082
        mov     [edx + APPDATA.io_map + 4], eax
3535 clevermous 1083
 
9968 dunkaist 1084
        mov     [edx + APPDATA.pl0_stack], ebx
9715 Doczom 1085
        lea     edi, [ebx + RING0_STACK_SIZE]
9968 dunkaist 1086
        mov     [edx + APPDATA.fpu_state], edi
1087
        mov     [edx + APPDATA.saved_esp0], edi
1088
        mov     [edx + APPDATA.saved_esp], edi
1089
        mov     [edx + APPDATA.terminate_protection], 1 ; make unkillable
3534 clevermous 1090
 
1091
        mov     esi, fpu_data
7165 clevermous 1092
        mov     ecx, [xsave_area_size]
1093
        add     ecx, 3
1094
        shr     ecx, 2
3534 clevermous 1095
        rep movsd
1096
 
9715 Doczom 1097
        lea     eax, [edx + APP_EV_OFFSET]
9968 dunkaist 1098
        mov     [edx + APPDATA.fd_ev], eax
1099
        mov     [edx + APPDATA.bk_ev], eax
8087 dunkaist 1100
 
9715 Doczom 1101
        lea     eax, [edx + APP_OBJ_OFFSET]
9968 dunkaist 1102
        mov     [edx + APPDATA.fd_obj], eax
1103
        mov     [edx + APPDATA.bk_obj], eax
3534 clevermous 1104
 
9968 dunkaist 1105
        mov     [edx + APPDATA.cur_dir], sysdir_path-2
3534 clevermous 1106
 
5130 serge 1107
        mov     [edx + APPDATA.process], sys_proc
3534 clevermous 1108
 
9715 Doczom 1109
        lea     ebx, [edx + APPDATA.list]
1110
        lea     ecx, [sys_proc + PROC.thr_list]
5130 serge 1111
        list_add_tail ebx, ecx
1112
 
9715 Doczom 1113
        mov     [edx + APPDATA.wnd_number], dh
1114
        mov     byte [edx + APPDATA.tid], dh
9930 Doczom 1115
        movzx   eax, dh
1116
        shl     eax, BSF sizeof.WDATA
1117
        add     eax, window_data
1118
        mov     [edx + APPDATA.window], eax
3534 clevermous 1119
 
1120
        ret
1121
endp
1122
 
2288 clevermous 1123
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1124
;                                                                    ;
1125
;                    MAIN OS LOOP START                              ;
1126
;                                                                    ;
1127
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1128
align 32
1129
osloop:
3534 clevermous 1130
        mov     edx, osloop_has_work?
1131
        xor     ecx, ecx
1132
        call    Wait_events
1133
        xor     eax, eax
1134
        xchg    eax, [osloop_nonperiodic_work]
1135
        test    eax, eax
1136
        jz      .no_periodic
4734 mario79 1137
 
2453 mario79 1138
        call    __sys_draw_pointer
2288 clevermous 1139
        call    window_check_events
1140
        call    mouse_check_events
1141
        call    checkmisc
1142
        call    checkVga_N13
4734 mario79 1143
;--------------------------------------
3534 clevermous 1144
.no_periodic:
2288 clevermous 1145
        call    stack_handler
1146
        call    check_fdd_motor_status
1147
        call    check_ATAPI_device_event
2601 clevermous 1148
        call    check_lights_state
2288 clevermous 1149
        call    check_timers
4734 mario79 1150
 
2288 clevermous 1151
        jmp     osloop
1152
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1153
;                                                                    ;
1154
;                      MAIN OS LOOP END                              ;
1155
;                                                                    ;
1156
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3534 clevermous 1157
proc osloop_has_work?
1158
        cmp     [osloop_nonperiodic_work], 0
1159
        jnz     .yes
1160
        call    stack_handler_has_work?
1161
        jnz     .yes
4273 clevermous 1162
        call    check_fdd_motor_status_has_work?
1163
        jnz     .yes
3534 clevermous 1164
        call    check_ATAPI_device_event_has_work?
1165
        jnz     .yes
1166
        call    check_lights_state_has_work?
1167
        jnz     .yes
1168
        call    check_timers_has_work?
1169
        jnz     .yes
1170
.no:
1171
        xor     eax, eax
2288 clevermous 1172
        ret
3534 clevermous 1173
.yes:
1174
        xor     eax, eax
1175
        inc     eax
1176
        ret
1177
endp
2288 clevermous 1178
 
3534 clevermous 1179
proc wakeup_osloop
1180
        mov     [osloop_nonperiodic_work], 1
1181
        ret
1182
endp
1183
 
2288 clevermous 1184
uglobal
3534 clevermous 1185
align 4
1186
osloop_nonperiodic_work dd      ?
2288 clevermous 1187
endg
1188
 
5033 clevermous 1189
uglobal
1190
align 64
1191
idle_addr       rb      64
1192
endg
1193
 
3534 clevermous 1194
idle_thread:
1195
        sti
5033 clevermous 1196
 
1197
; The following code can be executed by all CPUs in the system.
1198
; All other parts of the kernel do not expect multi-CPU.
1199
; Also, APs don't even have a stack here.
1200
; Beware. Don't do anything here. Anything at all.
3534 clevermous 1201
idle_loop:
5033 clevermous 1202
        cmp     [use_mwait_for_idle], 0
9911 Doczom 1203
        jnz     .mwait
5033 clevermous 1204
 
9911 Doczom 1205
.hlt:
3534 clevermous 1206
        hlt
9911 Doczom 1207
        jmp     .hlt
2288 clevermous 1208
 
9911 Doczom 1209
.mwait:
5033 clevermous 1210
        mov     eax, idle_addr
1211
        xor     ecx, ecx
1212
        xor     edx, edx
1213
        monitor
1214
        xor     ecx, ecx
1215
        mov     eax, 20h        ; or 10h
1216
        mwait
9911 Doczom 1217
        jmp     .mwait
2288 clevermous 1218
 
3534 clevermous 1219
 
2288 clevermous 1220
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1221
;                                                                      ;
1222
;                   INCLUDED SYSTEM FILES                              ;
1223
;                                                                      ;
1224
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1225
 
1226
 
1227
include "kernel32.inc"
1228
 
1229
 
1230
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1231
;                                                                      ;
1232
;                       KERNEL FUNCTIONS                               ;
1233
;                                                                      ;
1234
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1235
 
1236
reserve_irqs_ports:
1237
 
1238
 
1239
; RESERVE PORTS
1240
        mov     eax, RESERVED_PORTS
1241
        mov     ecx, 1
1242
 
1243
        mov     [eax], dword 4
1244
 
1245
        mov     [eax+16], ecx
1246
        mov     [eax+16+4], dword 0
3426 hidnplayr 1247
        mov     [eax+16+8], dword 0x2D
2288 clevermous 1248
 
1249
        mov     [eax+32], ecx
1250
        mov     [eax+32+4], dword 0x30
1251
        mov     [eax+32+8], dword 0x4D
1252
 
1253
        mov     [eax+48], ecx
1254
        mov     [eax+48+4], dword 0x50
3426 hidnplayr 1255
        mov     [eax+48+8], dword 0xDF
2288 clevermous 1256
 
1257
        mov     [eax+64], ecx
1258
        mov     [eax+64+4], dword 0xE5
1259
        mov     [eax+64+8], dword 0xFF
1260
 
1261
        ret
1262
 
1263
 
1264
iglobal
3534 clevermous 1265
  process_number dd 0x2
2288 clevermous 1266
endg
1267
 
1268
set_variables:
1269
 
1270
        mov     ecx, 0x16                    ; flush port 0x60
9911 Doczom 1271
@@:
2288 clevermous 1272
        in      al, 0x60
9911 Doczom 1273
        loop    @b
2288 clevermous 1274
        push    eax
1275
 
7132 dunkaist 1276
        mov     ax, [BOOT.y_res]
2288 clevermous 1277
        shr     ax, 1
1278
        shl     eax, 16
7132 dunkaist 1279
        mov     ax, [BOOT.x_res]
2288 clevermous 1280
        shr     ax, 1
1281
        mov     [MOUSE_X], eax
3534 clevermous 1282
        call    wakeup_osloop
2288 clevermous 1283
 
1284
        xor     eax, eax
1285
        mov     [BTN_ADDR], dword BUTTON_INFO ; address of button list
1286
 
1287
        mov     byte [KEY_COUNT], al              ; keyboard buffer
1288
        mov     byte [BTN_COUNT], al              ; button buffer
1289
 
1290
        pop     eax
1291
        ret
9488 rgimad 1292
;-----------------------------------------------------------------------------
2288 clevermous 1293
 
1294
align 4
1295
display_number:
8713 Doczom 1296
; add check pointers
1297
        test    bl, bl
1298
        jz      @f
9910 Doczom 1299
        bt      ebx, 30  ; check 30 bit
1300
        jb      @f
1301
        stdcall is_region_userspace, ecx, 4
9045 dunkaist 1302
        jz      @f
8713 Doczom 1303
        ret
1304
@@:
9910 Doczom 1305
        jz      @f
1306
        stdcall is_region_userspace, ecx, 8
1307
        jz      @f
1308
        ret
1309
@@:
8713 Doczom 1310
        test    esi, 0x08000000
1311
        jz      @f
1312
        stdcall is_region_userspace, edi, 1
9045 dunkaist 1313
        jz      @f
8713 Doczom 1314
        ret
1315
@@:
2288 clevermous 1316
;It is not optimization
1317
        mov     eax, ebx
1318
        mov     ebx, ecx
1319
        mov     ecx, edx
1320
        mov     edx, esi
1321
        mov     esi, edi
1322
; eax = print type, al=0 -> ebx is number
1323
;                   al=1 -> ebx is pointer
1324
;                   ah=0 -> display decimal
1325
;                   ah=1 -> display hexadecimal
1326
;                   ah=2 -> display binary
1327
;                   eax bits 16-21 = number of digits to display (0-32)
1328
;                   eax bits 22-31 = reserved
1329
;
1330
; ebx = number or pointer
1331
; ecx = x shl 16 + y
1332
; edx = color
1333
        xor     edi, edi
1334
display_number_force:
1335
        push    eax
1336
        and     eax, 0x3fffffff
1337
        cmp     eax, 0xffff     ; length > 0 ?
1338
        pop     eax
9911 Doczom 1339
        jge     .cont_displ
2288 clevermous 1340
        ret
9911 Doczom 1341
   .cont_displ:
2288 clevermous 1342
        push    eax
1343
        and     eax, 0x3fffffff
1344
        cmp     eax, 61*0x10000  ; length <= 60 ?
1345
        pop     eax
9911 Doczom 1346
        jb      .cont_displ2
2288 clevermous 1347
        ret
9911 Doczom 1348
   .cont_displ2:
2288 clevermous 1349
 
1350
        pushad
1351
 
1352
        cmp     al, 1            ; ecx is a pointer ?
9911 Doczom 1353
        jne     @f
2288 clevermous 1354
        mov     ebp, ebx
1355
        add     ebp, 4
9911 Doczom 1356
        mov     ebp, [ebp] ;[ebp + std_application_base_address]
1357
        mov     ebx, [ebx] ;[ebx + std_application_base_address]
1358
@@:
2288 clevermous 1359
        sub     esp, 64
1360
 
1361
        test    ah, ah            ; DECIMAL
9911 Doczom 1362
        jnz     .no_display_desnum
1363
 
2288 clevermous 1364
        shr     eax, 16
1365
        and     eax, 0xC03f
1366
;     and   eax,0x3f
1367
        push    eax
1368
        and     eax, 0x3f
1369
        mov     edi, esp
1370
        add     edi, 4+64-1
1371
        mov     ecx, eax
1372
        mov     eax, ebx
1373
        mov     ebx, 10
9911 Doczom 1374
@@:
2288 clevermous 1375
        xor     edx, edx
1376
        call    division_64_bits
1377
        div     ebx
1378
        add     dl, 48
1379
        mov     [edi], dl
1380
        dec     edi
9911 Doczom 1381
        loop    @b
1382
 
2288 clevermous 1383
        pop     eax
1384
        call    normalize_number
1385
        call    draw_num_text
1386
        add     esp, 64
1387
        popad
1388
        ret
9911 Doczom 1389
.no_display_desnum:
2288 clevermous 1390
 
1391
        cmp     ah, 0x01         ; HEXADECIMAL
9911 Doczom 1392
        jne     .no_display_hexnum
1393
 
2288 clevermous 1394
        shr     eax, 16
1395
        and     eax, 0xC03f
1396
;     and   eax,0x3f
1397
        push    eax
1398
        and     eax, 0x3f
1399
        mov     edi, esp
1400
        add     edi, 4+64-1
1401
        mov     ecx, eax
1402
        mov     eax, ebx
1403
        mov     ebx, 16
9911 Doczom 1404
@@:
2288 clevermous 1405
        xor     edx, edx
1406
        call    division_64_bits
1407
        div     ebx
9911 Doczom 1408
   ;hexletters = __fdo_hexdigits
1409
        add     edx, __fdo_hexdigits ;hexletters
2288 clevermous 1410
        mov     dl, [edx]
1411
        mov     [edi], dl
1412
        dec     edi
9911 Doczom 1413
        loop    @b
1414
 
2288 clevermous 1415
        pop     eax
1416
        call    normalize_number
1417
        call    draw_num_text
1418
        add     esp, 64
1419
        popad
1420
        ret
9911 Doczom 1421
.no_display_hexnum:
2288 clevermous 1422
 
1423
        cmp     ah, 0x02         ; BINARY
9911 Doczom 1424
        jne     .no_display_binnum
1425
 
2288 clevermous 1426
        shr     eax, 16
1427
        and     eax, 0xC03f
1428
;     and   eax,0x3f
1429
        push    eax
1430
        and     eax, 0x3f
1431
        mov     edi, esp
1432
        add     edi, 4+64-1
1433
        mov     ecx, eax
1434
        mov     eax, ebx
1435
        mov     ebx, 2
9911 Doczom 1436
@@:
2288 clevermous 1437
        xor     edx, edx
1438
        call    division_64_bits
1439
        div     ebx
1440
        add     dl, 48
1441
        mov     [edi], dl
1442
        dec     edi
9911 Doczom 1443
        loop    @b
1444
 
2288 clevermous 1445
        pop     eax
1446
        call    normalize_number
1447
        call    draw_num_text
1448
        add     esp, 64
1449
        popad
1450
        ret
9911 Doczom 1451
.no_display_binnum:
2288 clevermous 1452
 
1453
        add     esp, 64
1454
        popad
1455
        ret
1456
 
1457
normalize_number:
1458
        test    ah, 0x80
1459
        jz      .continue
1460
        mov     ecx, 48
1461
        and     eax, 0x3f
1462
@@:
1463
        inc     edi
1464
        cmp     [edi], cl
1465
        jne     .continue
1466
        dec     eax
1467
        cmp     eax, 1
1468
        ja      @r
1469
        mov     al, 1
1470
.continue:
1471
        and     eax, 0x3f
1472
        ret
1473
 
1474
division_64_bits:
1475
        test    [esp+1+4], byte 0x40
1476
        jz      .continue
1477
        push    eax
1478
        mov     eax, ebp
1479
        div     ebx
1480
        mov     ebp, eax
1481
        pop     eax
1482
.continue:
1483
        ret
1484
 
1485
draw_num_text:
1486
        mov     esi, eax
1487
        mov     edx, 64+4
1488
        sub     edx, eax
1489
        add     edx, esp
1490
        mov     ebx, [esp+64+32-8+4]
1491
; add window start x & y
1492
 
9930 Doczom 1493
        mov     ecx, [current_slot]
1494
        mov     ecx, [ecx + APPDATA.window]
2288 clevermous 1495
 
9930 Doczom 1496
        mov     eax, [ecx + WDATA.box.left]
1497
        add     eax, [ecx + WDATA.clientbox.left]
2288 clevermous 1498
        shl     eax, 16
9930 Doczom 1499
        add     eax, [ecx + WDATA.box.top]
1500
        add     eax, [ecx + WDATA.clientbox.top]
2288 clevermous 1501
        add     ebx, eax
1502
        mov     ecx, [esp+64+32-12+4]
1503
        mov     eax, [esp+64+8]         ; background color (if given)
1504
        mov     edi, [esp+64+4]
6272 pathoswith 1505
        and     ecx, 5FFFFFFFh
1506
        bt      ecx, 27
1507
        jnc     @f
1508
        mov     edi, eax
1509
@@:
2288 clevermous 1510
        jmp     dtext
4700 mario79 1511
;-----------------------------------------------------------------------------
2288 clevermous 1512
align 4
1513
sys_setup:
4700 mario79 1514
;  1 = roland mpu midi base , base io address
1515
;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
1516
;  3 = not used
1517
;  4 = not used
1518
;  5 = system language, 1eng 2fi 3ger 4rus
1519
;  6 = not used
1520
;  7 = not used
1521
;  8 = not used
1522
;  9 = not used
1523
; 10 = not used
2288 clevermous 1524
; 11 = enable lba read
1525
; 12 = enable pci access
4700 mario79 1526
;-----------------------------------------------------------------------------
9968 dunkaist 1527
        and     [esp + SYSCALL_STACK.eax], 0
4700 mario79 1528
; F.21.1 - set MPU MIDI base port
1529
        dec     ebx
1530
        jnz     @f
2288 clevermous 1531
 
1532
        cmp     ecx, 0x100
4700 mario79 1533
        jb      @f
2288 clevermous 1534
 
1535
        mov     esi, 65535
1536
        cmp     esi, ecx
4700 mario79 1537
        jb      @f
2288 clevermous 1538
 
4700 mario79 1539
        mov     [midi_base], cx
1540
        mov     word [mididp], cx
1541
        inc     cx
1542
        mov     word [midisp], cx
2288 clevermous 1543
        ret
4700 mario79 1544
;--------------------------------------
1545
@@:
1546
; F.21.2 - set keyboard layout
1547
        dec     ebx
1548
        jnz     @f
2288 clevermous 1549
 
9710 Doczom 1550
        mov     eax, edx
4700 mario79 1551
; 1 = normal layout
1552
        dec     ecx
1553
        jnz     .shift
2288 clevermous 1554
 
1555
        mov     ebx, keymap
1556
        mov     ecx, 128
1557
        call    memmove
1558
        ret
4700 mario79 1559
;--------------------------------------
1560
.shift:
1561
; 2 = layout at pressed Shift
2288 clevermous 1562
        dec     ecx
4700 mario79 1563
        jnz     .alt
2288 clevermous 1564
 
1565
        mov     ebx, keymap_shift
1566
        mov     ecx, 128
1567
        call    memmove
1568
        ret
4700 mario79 1569
;--------------------------------------
1570
.alt:
1571
; 3 = layout at pressed Alt
2288 clevermous 1572
        dec     ecx
4700 mario79 1573
        jnz     .country
1574
 
2288 clevermous 1575
        mov     ebx, keymap_alt
1576
        mov     ecx, 128
1577
        call    memmove
1578
        ret
4700 mario79 1579
;--------------------------------------
1580
.country:
1581
; country identifier
2288 clevermous 1582
        sub     ecx, 6
4700 mario79 1583
        jnz     .error
1584
 
2288 clevermous 1585
        mov     word [keyboard], dx
1586
        ret
4700 mario79 1587
;--------------------------------------
1588
@@:
1589
; F.21.5 - set system language
1590
        sub     ebx, 3
1591
        jnz     @f
2288 clevermous 1592
 
1593
        mov     [syslang], ecx
1594
        ret
4700 mario79 1595
;--------------------------------------
1596
@@:
1597
; F.21.11 - enable/disable low-level access to HD
1598
        and     ecx, 1
1599
        sub     ebx, 6
1600
        jnz     @f
2288 clevermous 1601
 
4700 mario79 1602
        mov     [lba_read_enabled], ecx
2288 clevermous 1603
        ret
4700 mario79 1604
;--------------------------------------
1605
@@:
1606
; F.21.12 - enable/disable low-level access to PCI
2288 clevermous 1607
        dec     ebx
4700 mario79 1608
        jnz     .error
2288 clevermous 1609
 
1610
        mov     [pci_access_enabled], ecx
1611
        ret
4700 mario79 1612
;--------------------------------------
1613
.error:
9968 dunkaist 1614
        or      [esp + SYSCALL_STACK.eax], -1
2288 clevermous 1615
        ret
4700 mario79 1616
;-----------------------------------------------------------------------------
2288 clevermous 1617
align 4
1618
sys_getsetup:
4700 mario79 1619
;  1 = roland mpu midi base , base io address
1620
;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
1621
;  3 = not used
1622
;  4 = not used
1623
;  5 = system language, 1eng 2fi 3ger 4rus
1624
;  6 = not used
1625
;  7 = not used
1626
;  8 = not used
1627
;  9 = get hs timer tic
1628
; 10 = not used
1629
; 11 = get the state "lba read"
1630
; 12 = get the state "pci access"
1631
;-----------------------------------------------------------------------------
1632
; F.26.1 - get MPU MIDI base port
1633
        dec     ebx
1634
        jnz     @f
2288 clevermous 1635
 
1636
        movzx   eax, [midi_base]
9831 dunkaist 1637
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1638
        ret
4700 mario79 1639
;--------------------------------------
1640
@@:
1641
; F.26.2 - get keyboard layout
2288 clevermous 1642
        dec     ebx
4700 mario79 1643
        jnz     @f
2288 clevermous 1644
 
9710 Doczom 1645
        mov     ebx, edx
1646
        ; if given memory address belongs to kernel then error
1647
        stdcall is_region_userspace, ebx, 128
1648
        jnz     .addr_error
4700 mario79 1649
; 1 = normal layout
1650
        dec     ecx
1651
        jnz     .shift
2288 clevermous 1652
 
1653
        mov     eax, keymap
1654
        mov     ecx, 128
1655
        call    memmove
1656
        ret
4700 mario79 1657
;--------------------------------------
1658
.shift:
1659
; 2 = layout with pressed Shift
2288 clevermous 1660
        dec     ecx
4700 mario79 1661
        jnz     .alt
2288 clevermous 1662
 
1663
        mov     eax, keymap_shift
1664
        mov     ecx, 128
1665
        call    memmove
1666
        ret
4700 mario79 1667
;--------------------------------------
1668
.alt:
1669
; 3 = layout with pressed Alt
2288 clevermous 1670
        dec     ecx
4700 mario79 1671
        jne     .country
2288 clevermous 1672
 
1673
        mov     eax, keymap_alt
1674
        mov     ecx, 128
1675
        call    memmove
1676
        ret
4700 mario79 1677
;--------------------------------------
1678
.country:
1679
; 9 = country identifier
2288 clevermous 1680
        sub     ecx, 6
4700 mario79 1681
        jnz     .error
1682
 
2288 clevermous 1683
        movzx   eax, word [keyboard]
9831 dunkaist 1684
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1685
        ret
8158 rgimad 1686
 
1687
.addr_error:    ; if given memory address is illegal
9968 dunkaist 1688
        or      [esp + SYSCALL_STACK.eax], -1
9947 ace_dent 1689
        ret
4700 mario79 1690
;--------------------------------------
1691
@@:
1692
; F.26.5 - get system language
1693
        sub     ebx, 3
1694
        jnz     @f
2288 clevermous 1695
 
1696
        mov     eax, [syslang]
9831 dunkaist 1697
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1698
        ret
4700 mario79 1699
;--------------------------------------
1700
@@:
1701
; F.26.9 - get the value of the time counter
4573 clevermous 1702
        sub     ebx, 4
4700 mario79 1703
        jnz     @f
1704
 
1705
        mov     eax, [timer_ticks]
9831 dunkaist 1706
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1707
        ret
4700 mario79 1708
;--------------------------------------
1709
@@:
5794 serge 1710
; F.26.10 - get the time from kernel launch in nanoseconds
9571 Doczom 1711
        dec     ebx
5794 serge 1712
        jnz     @f
1713
 
1714
        call    get_clock_ns
9831 dunkaist 1715
        mov     [esp + SYSCALL_STACK.edx], edx
1716
        mov     [esp + SYSCALL_STACK.eax], eax
5794 serge 1717
        ret
1718
;--------------------------------------
1719
@@:
4700 mario79 1720
; F.26.11 - Find out whether low-level HD access is enabled
9571 Doczom 1721
        dec     ebx
4700 mario79 1722
        jnz     @f
1723
 
2288 clevermous 1724
        mov     eax, [lba_read_enabled]
9831 dunkaist 1725
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1726
        ret
4700 mario79 1727
;--------------------------------------
1728
@@:
1729
; F.26.12 - Find out whether low-level PCI access is enabled
2288 clevermous 1730
        dec     ebx
4700 mario79 1731
        jnz     .error
1732
 
2288 clevermous 1733
        mov     eax, [pci_access_enabled]
9831 dunkaist 1734
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1735
        ret
4700 mario79 1736
;--------------------------------------
1737
.error:
9968 dunkaist 1738
        or      [esp + SYSCALL_STACK.eax], -1
2288 clevermous 1739
        ret
4700 mario79 1740
;-----------------------------------------------------------------------------
2288 clevermous 1741
get_timer_ticks:
1742
        mov     eax, [timer_ticks]
1743
        ret
4700 mario79 1744
;-----------------------------------------------------------------------------
9911 Doczom 1745
iglobal
1746
midi_base dw 0
1747
endg
5851 pathoswith 1748
 
9911 Doczom 1749
align 4
1750
sys_midi:
1751
        cmp     word [mididp], 0
1752
        jnz     @f
1753
        mov     [esp + SYSCALL_STACK.eax], 1
1754
        ret
1755
@@:
1756
        and     [esp + SYSCALL_STACK.eax], 0
1757
        dec     ebx
1758
        jnz     .smn1
1759
 ;    call setuart
1760
@@:
1761
        call    .is_output
1762
        test    al, al
1763
        jnz     @b
1764
        mov     dx, word [midisp]
1765
        mov     al, 0xff
1766
        out     dx, al
1767
@@:
1768
        mov     dx, word [midisp]
1769
        mov     al, 0xff
1770
        out     dx, al
1771
        call    .is_input
1772
        test    al, al
1773
        jnz     @b
1774
        call    .get_mpu_in
1775
        cmp     al, 0xfe
1776
        jnz     @b
1777
@@:
1778
        call    .is_output
1779
        test    al, al
1780
        jnz     @b
1781
        mov     dx, word [midisp]
1782
        mov     al, 0x3f
1783
        out     dx, al
1784
        ret
1785
.smn1:
1786
        dec     ebx
1787
        jnz     .ret
1788
@@:
1789
        call    .get_mpu_in
1790
        call    .is_output
1791
        test    al, al
1792
        jnz     @b
1793
        mov     al, bl
1794
        call    .put_mpu_out
1795
.ret:
1796
        ret
1797
 
1798
.is_input:
2288 clevermous 1799
        push    edx
1800
        mov     dx, word [midisp]
1801
        in      al, dx
1802
        and     al, 0x80
1803
        pop     edx
1804
        ret
1805
 
9911 Doczom 1806
.is_output:
2288 clevermous 1807
        push    edx
1808
        mov     dx, word [midisp]
1809
        in      al, dx
1810
        and     al, 0x40
1811
        pop     edx
1812
        ret
1813
 
9911 Doczom 1814
.get_mpu_in:
2288 clevermous 1815
        push    edx
1816
        mov     dx, word [mididp]
1817
        in      al, dx
1818
        pop     edx
1819
        ret
1820
 
9911 Doczom 1821
.put_mpu_out:
2288 clevermous 1822
        push    edx
1823
        mov     dx, word [mididp]
1824
        out     dx, al
1825
        pop     edx
1826
        ret
1827
 
9911 Doczom 1828
;-----------------------------------------------------------------------------
2288 clevermous 1829
sys_end:
2430 mario79 1830
; restore default cursor before killing
1831
        pusha
1832
        mov     ecx, [current_slot]
9930 Doczom 1833
        mov     ecx, [ecx + APPDATA.window]
2435 mario79 1834
        call    restore_default_cursor_before_killing
2430 mario79 1835
        popa
1836
;--------------------------------------
3545 hidnplayr 1837
; kill all sockets this process owns
1838
        pusha
9612 Doczom 1839
        mov     edx, [current_slot]
9715 Doczom 1840
        mov     edx, [edx + APPDATA.tid]
6011 hidnplayr 1841
        call    socket_process_end
3545 hidnplayr 1842
        popa
1843
;--------------------------------------
2288 clevermous 1844
        mov     ecx, [current_slot]
9715 Doczom 1845
        mov     eax, [ecx + APPDATA.tls_base]
2288 clevermous 1846
        test    eax, eax
1847
        jz      @F
1848
 
1849
        stdcall user_free, eax
1850
@@:
1851
 
9612 Doczom 1852
        mov     eax, [current_slot]
9715 Doczom 1853
        mov     [eax + APPDATA.state], TSTATE_ZOMBIE
3534 clevermous 1854
        call    wakeup_osloop
2288 clevermous 1855
 
3597 Serge 1856
.waitterm:            ; wait here for termination
1857
        call    change_task
1858
        jmp     .waitterm
2435 mario79 1859
;------------------------------------------------------------------------------
2452 mario79 1860
align 4
9930 Doczom 1861
; ecx - ptr WDATA
2435 mario79 1862
restore_default_cursor_before_killing:
2468 mario79 1863
        pushfd
1864
        cli
2435 mario79 1865
        mov     eax, [def_cursor]
9930 Doczom 1866
        mov     [ecx + WDATA.cursor], eax
2288 clevermous 1867
 
2435 mario79 1868
        movzx   eax, word [MOUSE_Y]
1869
        movzx   ebx, word [MOUSE_X]
2446 mario79 1870
        mov     eax, [d_width_calc_area + eax*4]
1871
 
5351 serge 1872
        add     eax, [_display.win_map]
9715 Doczom 1873
        movzx   edx, byte [ebx + eax]
9930 Doczom 1874
        shl     edx, BSF sizeof.WDATA
1875
        mov     esi, [window_data + edx + WDATA.cursor]
2452 mario79 1876
 
1877
        cmp     esi, [current_cursor]
1878
        je      @f
1879
 
9941 Doczom 1880
        cmp     [_display.select_cursor], 0
1881
        jz      @f
1882
 
1883
        stdcall [_display.select_cursor], esi
2435 mario79 1884
        mov     [current_cursor], esi
2452 mario79 1885
@@:
2450 mario79 1886
        mov     [redrawmouse_unconditional], 1
3534 clevermous 1887
        call    wakeup_osloop
2468 mario79 1888
        popfd
2435 mario79 1889
        ret
1890
;------------------------------------------------------------------------------
2288 clevermous 1891
iglobal
1892
align 4
1893
sys_system_table:
1894
        dd      sysfn_deactivate        ; 1 = deactivate window
1895
        dd      sysfn_terminate         ; 2 = terminate thread
1896
        dd      sysfn_activate          ; 3 = activate window
1897
        dd      sysfn_getidletime       ; 4 = get idle time
1898
        dd      sysfn_getcpuclock       ; 5 = get cpu clock
1899
        dd      sysfn_saveramdisk       ; 6 = save ramdisk
1900
        dd      sysfn_getactive         ; 7 = get active window
1901
        dd      sysfn_sound_flag        ; 8 = get/set sound_flag
1902
        dd      sysfn_shutdown          ; 9 = shutdown with parameter
1903
        dd      sysfn_minimize          ; 10 = minimize window
1904
        dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
9268 Doczom 1905
        dd      undefined_syscall       ; 12 = get last pressed key. function removed. sysfn_lastkey
2288 clevermous 1906
        dd      sysfn_getversion        ; 13 = get kernel version
1907
        dd      sysfn_waitretrace       ; 14 = wait retrace
1908
        dd      sysfn_centermouse       ; 15 = center mouse cursor
1909
        dd      sysfn_getfreemem        ; 16 = get free memory size
1910
        dd      sysfn_getallmem         ; 17 = get total memory size
1911
        dd      sysfn_terminate2        ; 18 = terminate thread using PID
1912
                                        ;                 instead of slot
1913
        dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
1914
        dd      sysfn_meminfo           ; 20 = get extended memory info
1915
        dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
1916
        dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
2648 mario79 1917
        dd      sysfn_min_windows       ; 23 = minimize all windows
2654 mario79 1918
        dd      sysfn_set_screen_sizes  ; 24 = set screen sizes for Vesa
5836 GerdtR 1919
 
1920
        dd      sysfn_zmodif            ; 25 = get/set window z modifier  ;Fantomer
2288 clevermous 1921
sysfn_num = ($ - sys_system_table)/4
1922
endg
1923
;------------------------------------------------------------------------------
1924
sys_system:
1925
        dec     ebx
1926
        cmp     ebx, sysfn_num
1927
        jae     @f
1928
        jmp     dword [sys_system_table + ebx*4]
1929
@@:
1930
        ret
1931
;------------------------------------------------------------------------------
1932
sysfn_shutdown:          ; 18.9 = system shutdown
7122 dunkaist 1933
        cmp     ecx, SYSTEM_SHUTDOWN
9930 Doczom 1934
        jl      .exit_for_anyone
7122 dunkaist 1935
        cmp     ecx, SYSTEM_RESTART
9930 Doczom 1936
        jg      .exit_for_anyone
7132 dunkaist 1937
        mov     [BOOT.shutdown_type], cl
2288 clevermous 1938
 
8866 rgimad 1939
        mov     eax, [thread_count]
2288 clevermous 1940
        mov     [SYS_SHUTDOWN], al
1941
        mov     [shutdown_processes], eax
3534 clevermous 1942
        call    wakeup_osloop
9968 dunkaist 1943
        and     [esp + SYSCALL_STACK.eax], 0
9930 Doczom 1944
.exit_for_anyone:
2288 clevermous 1945
        ret
1946
  uglobal
1947
   shutdown_processes:
1948
                       dd 0x0
1949
  endg
1950
;------------------------------------------------------------------------------
8270 dunkaist 1951
; in: eax -- APPDATA ptr
1952
; out: Z/z -- is/not kernel thread
1953
is_kernel_thread:
9715 Doczom 1954
        mov     eax, [eax + APPDATA.process]
1955
        cmp     eax, [SLOT_BASE + 2*sizeof.APPDATA + APPDATA.process]       ; OS
8270 dunkaist 1956
        ret
1957
;------------------------------------------------------------------------------
2288 clevermous 1958
sysfn_terminate:        ; 18.2 = TERMINATE
2472 mario79 1959
        push    ecx
2288 clevermous 1960
        cmp     ecx, 2
9910 Doczom 1961
        jb      .noprocessterminate
8866 rgimad 1962
        mov     edx, [thread_count]
2288 clevermous 1963
        cmp     ecx, edx
9910 Doczom 1964
        ja      .noprocessterminate
8866 rgimad 1965
        mov     eax, [thread_count]
9709 Doczom 1966
        shl     ecx, BSF sizeof.APPDATA
1967
        add     ecx, SLOT_BASE
9911 Doczom 1968
 
9709 Doczom 1969
        mov     edx, [ecx + APPDATA.tid]
1970
        cmp     byte [ecx + APPDATA.state], TSTATE_FREE
9910 Doczom 1971
        jz      .noprocessterminate
8270 dunkaist 1972
        push    eax
9709 Doczom 1973
        mov     eax, ecx
8270 dunkaist 1974
        call    is_kernel_thread
1975
        pop     eax
9910 Doczom 1976
        jz      .noprocessterminate
3296 clevermous 1977
        push    ecx edx
9709 Doczom 1978
        mov     edx, ecx
3296 clevermous 1979
        call    request_terminate
1980
        pop     edx ecx
1981
        test    eax, eax
9910 Doczom 1982
        jz      .noprocessterminate
2430 mario79 1983
;--------------------------------------
3545 hidnplayr 1984
; terminate all network sockets it used
1985
        pusha
9709 Doczom 1986
        mov     eax, edx     ;TODO: check function
6011 hidnplayr 1987
        call    socket_process_end
3545 hidnplayr 1988
        popa
1989
;--------------------------------------
2430 mario79 1990
; restore default cursor before killing
1991
        pusha
1992
        mov     ecx, [esp+32]
9930 Doczom 1993
        shl     ecx, BSF sizeof.WDATA
1994
        add     ecx, window_data
2452 mario79 1995
        mov     eax, [def_cursor]
9930 Doczom 1996
        cmp     [ecx + WDATA.cursor], eax
2452 mario79 1997
        je      @f
2435 mario79 1998
        call    restore_default_cursor_before_killing
2452 mario79 1999
@@:
2430 mario79 2000
        popa
2001
;--------------------------------------
2288 clevermous 2002
     ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
9709 Doczom 2003
        mov     [ecx + APPDATA.state], TSTATE_ZOMBIE; clear possible i40's
3534 clevermous 2004
        call    wakeup_osloop
2288 clevermous 2005
     ;call MEM_Heap_UnLock
2006
 
3534 clevermous 2007
        cmp     edx, [application_table_owner]; clear app table stat
9910 Doczom 2008
        jne     .noatsc
3534 clevermous 2009
        call    unlock_application_table
9910 Doczom 2010
.noatsc:
2011
.noprocessterminate:
2472 mario79 2012
        add     esp, 4
2288 clevermous 2013
        ret
2014
;------------------------------------------------------------------------------
2015
sysfn_terminate2:
3534 clevermous 2016
        call    lock_application_table
2288 clevermous 2017
        mov     eax, ecx
2018
        call    pid_to_slot
2019
        test    eax, eax
2020
        jz      .not_found
2021
        mov     ecx, eax
2022
        cli
2023
        call    sysfn_terminate
3534 clevermous 2024
        call    unlock_application_table
2288 clevermous 2025
        sti
9968 dunkaist 2026
        and     [esp + SYSCALL_STACK.eax], 0
2288 clevermous 2027
        ret
2028
.not_found:
3534 clevermous 2029
        call    unlock_application_table
9968 dunkaist 2030
        or      [esp + SYSCALL_STACK.eax], -1
2288 clevermous 2031
        ret
2032
;------------------------------------------------------------------------------
2033
sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
2034
        cmp     ecx, 2
2035
        jb      .nowindowdeactivate
8866 rgimad 2036
        cmp     ecx, [thread_count]
2288 clevermous 2037
        ja      .nowindowdeactivate
2408 Serge 2038
 
2288 clevermous 2039
        movzx   esi, word [WIN_STACK + ecx*2]
2040
        cmp     esi, 1
2041
        je      .nowindowdeactivate ; already deactive
2042
 
2043
        mov     edi, ecx
9926 Doczom 2044
        shl     edi, BSF sizeof.WDATA
2288 clevermous 2045
        add     edi, window_data
2046
        movzx   esi, word [WIN_STACK + ecx * 2]
2047
        lea     esi, [WIN_POS + esi * 2]
2048
        call    window._.window_deactivate
6800 pathoswith 2049
        call    syscall_display_settings.calculateScreen
2050
        call    syscall_display_settings.redrawScreen
2288 clevermous 2051
.nowindowdeactivate:
2052
        ret
3455 mario79 2053
;------------------------------------------------------------------------------
2288 clevermous 2054
sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
5675 leency 2055
        cmp     ecx, 2
2056
        jb      .nowindowactivate
8866 rgimad 2057
        cmp     ecx, [thread_count]
2288 clevermous 2058
        ja      .nowindowactivate
3455 mario79 2059
;-------------------------------------
2060
@@:
2061
; If the window is captured and moved by the user,
2062
; then you can't change the position in window stack!!!
2063
        mov     al, [mouse.active_sys_window.action]
2064
        and     al, WINDOW_MOVE_AND_RESIZE_FLAGS
2065
        test    al, al
2066
        jz      @f
2067
        call    change_task
2068
        jmp     @b
2069
@@:
2070
;-------------------------------------
2288 clevermous 2071
        mov     [window_minimize], 2; restore window if minimized
3534 clevermous 2072
        call    wakeup_osloop
2288 clevermous 2073
 
2074
        movzx   esi, word [WIN_STACK + ecx*2]
8866 rgimad 2075
        cmp     esi, [thread_count]
2288 clevermous 2076
        je      .nowindowactivate; already active
2077
 
2078
        mov     edi, ecx
9715 Doczom 2079
        shl     edi, BSF sizeof.WDATA
2288 clevermous 2080
        add     edi, window_data
2081
        movzx   esi, word [WIN_STACK + ecx * 2]
2082
        lea     esi, [WIN_POS + esi * 2]
2083
        call    waredraw
2084
.nowindowactivate:
2085
        ret
2086
;------------------------------------------------------------------------------
5865 GerdtR 2087
align 4
5836 GerdtR 2088
sysfn_zmodif:
2089
;18,25,1 - get z_modif
2090
;18,25,2 - set z_modif
2091
;edx = -1(for current task) or TID
2092
;esi(for 2) = new value z_modif
2093
;return:
2094
;1:   eax = z_modif
2095
;2: eax=0(fail),1(success) for set z_modif
2096
 
2097
        cmp     edx, -1
2098
        jne     @f
8869 rgimad 2099
        mov     edx, [current_slot_idx]
5836 GerdtR 2100
     @@:
8866 rgimad 2101
        cmp     edx, [thread_count]
5865 GerdtR 2102
        ja      .fail
5836 GerdtR 2103
        cmp     edx, 1
5865 GerdtR 2104
        je      .fail
5836 GerdtR 2105
 
5865 GerdtR 2106
        mov     eax, edx
9715 Doczom 2107
        shl     edx, BSF sizeof.WDATA
9949 Doczom 2108
        add     edx, window_data
5836 GerdtR 2109
 
9949 Doczom 2110
        test    [edx + WDATA.fl_wstate], WSTATE_USED
9930 Doczom 2111
        jz      .fail
5836 GerdtR 2112
 
2113
        cmp     ecx, 1
2114
        jnz     .set_zmod
2115
 
9949 Doczom 2116
        movzx   eax, [edx + WDATA.z_modif]
5865 GerdtR 2117
        jmp     .exit
5836 GerdtR 2118
 
2119
.set_zmod:
2120
        cmp     ecx, 2
5865 GerdtR 2121
        jnz     .fail
5836 GerdtR 2122
 
5865 GerdtR 2123
        mov     ebx, esi
2124
        mov     esi, eax
5836 GerdtR 2125
 
5865 GerdtR 2126
        cmp     bl, ZPOS_ALWAYS_TOP
2127
        jg      .fail
5836 GerdtR 2128
 
9949 Doczom 2129
        mov     [edx + WDATA.z_modif], bl
5836 GerdtR 2130
 
9949 Doczom 2131
        mov     eax, [edx + WDATA.box.left]
2132
        mov     ebx, [edx + WDATA.box.top]
2133
        mov     ecx, [edx + WDATA.box.width]
2134
        mov     edx, [edx + WDATA.box.height]
5836 GerdtR 2135
        add     ecx, eax
2136
        add     edx, ebx
5865 GerdtR 2137
        call    window._.set_screen
5836 GerdtR 2138
        call    window._.set_top_wnd
5865 GerdtR 2139
        call    window._.redraw_top_wnd
5836 GerdtR 2140
 
9715 Doczom 2141
        shl     esi, BSF sizeof.WDATA
9930 Doczom 2142
        mov     [esi + window_data + WDATA.fl_redraw], WSTATE_REDRAW
5865 GerdtR 2143
 
2144
 
5836 GerdtR 2145
        mov     eax, 1
5865 GerdtR 2146
        jmp     .exit
2147
.fail:
2148
        xor     eax, eax
5836 GerdtR 2149
.exit:
9831 dunkaist 2150
        mov     [esp + SYSCALL_STACK.eax], eax
5836 GerdtR 2151
        ret
2152
 
2153
;------------------------------------------------------------------------------
2288 clevermous 2154
sysfn_getidletime:              ; 18.4 = GET IDLETIME
9932 Doczom 2155
        mov     eax, [SLOT_BASE + sizeof.APPDATA + APPDATA.cpu_usage]
9831 dunkaist 2156
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2157
        ret
2158
;------------------------------------------------------------------------------
2159
sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
3481 Serge 2160
        mov     eax, dword [cpu_freq]
9831 dunkaist 2161
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2162
        ret
2163
;------------------------------------------------------------------------------
3481 Serge 2164
get_cpu_freq:
2165
        mov     eax, dword [cpu_freq]
2166
        mov     edx, dword [cpu_freq+4]
2167
        ret
9268 Doczom 2168
;  SAVE ramdisk to /hd/1/kolibri.img
2288 clevermous 2169
;!!!!!!!!!!!!!!!!!!!!!!!!
2170
   include 'blkdev/rdsave.inc'
2171
;!!!!!!!!!!!!!!!!!!!!!!!!
2172
;------------------------------------------------------------------------------
2173
align 4
2174
sysfn_getactive:        ; 18.7 = get active window
8866 rgimad 2175
        mov     eax, [thread_count]
2288 clevermous 2176
        movzx   eax, word [WIN_POS + eax*2]
9831 dunkaist 2177
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2178
        ret
2179
;------------------------------------------------------------------------------
2180
sysfn_sound_flag:       ; 18.8 = get/set sound_flag
2181
;     cmp  ecx,1
2182
        dec     ecx
9911 Doczom 2183
        jnz     .set_flag
2288 clevermous 2184
        movzx   eax, byte [sound_flag]; get sound_flag
9831 dunkaist 2185
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2186
        ret
9911 Doczom 2187
.set_flag:
2288 clevermous 2188
;     cmp  ecx,2
2189
        dec     ecx
9911 Doczom 2190
        jnz     .err
2288 clevermous 2191
        xor     byte [sound_flag], 1
9911 Doczom 2192
.err:
2288 clevermous 2193
        ret
2194
;------------------------------------------------------------------------------
2195
sysfn_minimize:         ; 18.10 = minimize window
2196
        mov     [window_minimize], 1
3534 clevermous 2197
        call    wakeup_osloop
2288 clevermous 2198
        ret
2199
;------------------------------------------------------------------------------
2200
align 4
2201
sysfn_getdiskinfo:      ; 18.11 = get disk info table
2202
        dec     ecx
4641 mario79 2203
        jnz     .exit
2204
.small_table:
8598 rgimad 2205
        stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
9045 dunkaist 2206
        jnz     .exit
2288 clevermous 2207
        mov     edi, edx
2208
        mov     esi, DRIVE_DATA
4641 mario79 2209
        mov     ecx, DRIVE_DATA_SIZE ;10
2288 clevermous 2210
        cld
4641 mario79 2211
        rep movsb
2212
.exit:
2288 clevermous 2213
        ret
2214
;------------------------------------------------------------------------------
2215
sysfn_getversion:       ; 18.13 = get kernel ID and version
8247 rgimad 2216
        ; if given memory address belongs to kernel then error
9917 Doczom 2217
        stdcall is_region_userspace, ecx, version_inf.size
9045 dunkaist 2218
        jnz     .addr_error
8247 rgimad 2219
 
2288 clevermous 2220
        mov     edi, ecx
2221
        mov     esi, version_inf
9917 Doczom 2222
        mov     ecx, version_inf.size
2288 clevermous 2223
        rep movsb
2224
        ret
8247 rgimad 2225
.addr_error:    ; if given memory address is illegal
9968 dunkaist 2226
        mov     [esp + SYSCALL_STACK.eax], -1
9947 ace_dent 2227
        ret
2288 clevermous 2228
;------------------------------------------------------------------------------
2229
sysfn_waitretrace:     ; 18.14 = sys wait retrace
9911 Doczom 2230
        ;wait retrace functions
2288 clevermous 2231
        mov     edx, 0x3da
9911 Doczom 2232
.loop:
2288 clevermous 2233
        in      al, dx
2234
        test    al, 1000b
9911 Doczom 2235
        jz      .loop
9968 dunkaist 2236
        and     [esp + SYSCALL_STACK.eax], 0
2288 clevermous 2237
        ret
2238
;------------------------------------------------------------------------------
2239
align 4
2240
sysfn_centermouse:      ; 18.15 = mouse centered
5350 serge 2241
        mov     eax, [_display.width]
2288 clevermous 2242
        shr     eax, 1
2243
        mov     [MOUSE_X], ax
5350 serge 2244
        mov     eax, [_display.height]
2288 clevermous 2245
        shr     eax, 1
2246
        mov     [MOUSE_Y], ax
3534 clevermous 2247
        call    wakeup_osloop
2288 clevermous 2248
        xor     eax, eax
9831 dunkaist 2249
        and     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2250
        ret
2251
;------------------------------------------------------------------------------
5851 pathoswith 2252
sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
2253
        cmp     ecx, 8
2254
        jnc     @f
9715 Doczom 2255
        jmp     dword [.table + ecx*4]
5851 pathoswith 2256
.get_mouse_acceleration:
2288 clevermous 2257
        xor     eax, eax
2258
        mov     ax, [mouse_speed_factor]
9831 dunkaist 2259
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2260
        ret
5851 pathoswith 2261
.set_mouse_acceleration:
2288 clevermous 2262
        mov     [mouse_speed_factor], dx
2263
        ret
5851 pathoswith 2264
.get_mouse_delay:
5853 pathoswith 2265
        xor     eax, eax
2266
        mov     al, [mouse_delay]
9831 dunkaist 2267
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2268
        ret
5851 pathoswith 2269
.set_mouse_delay:
5853 pathoswith 2270
        mov     [mouse_delay], dl
5851 pathoswith 2271
@@:
2288 clevermous 2272
        ret
5851 pathoswith 2273
.set_pointer_position:
5350 serge 2274
        cmp     dx, word[_display.height]
5851 pathoswith 2275
        jae     @b
2288 clevermous 2276
        rol     edx, 16
5350 serge 2277
        cmp     dx, word[_display.width]
5851 pathoswith 2278
        jae     @b
2288 clevermous 2279
        mov     [MOUSE_X], edx
4381 hidnplayr 2280
        mov     [mouse_active], 1
5851 pathoswith 2281
        jmp     wakeup_osloop
2282
.set_mouse_button:
2283
        mov     [BTN_DOWN], edx
2288 clevermous 2284
        mov     [mouse_active], 1
5851 pathoswith 2285
        jmp     wakeup_osloop
2286
.get_doubleclick_delay:
2287
        xor     eax, eax
2288
        mov     al, [mouse_doubleclick_delay]
9831 dunkaist 2289
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2290
        ret
5851 pathoswith 2291
.set_doubleclick_delay:
2292
        mov     [mouse_doubleclick_delay], dl
2293
        ret
2294
align 4
2295
.table:
2296
dd      .get_mouse_acceleration
2297
dd      .set_mouse_acceleration
2298
dd      .get_mouse_delay
2299
dd      .set_mouse_delay
2300
dd      .set_pointer_position
2301
dd      .set_mouse_button
2302
dd      .get_doubleclick_delay
2303
dd      .set_doubleclick_delay
2288 clevermous 2304
;------------------------------------------------------------------------------
2305
sysfn_getfreemem:
2306
        mov     eax, [pg_data.pages_free]
2307
        shl     eax, 2
9831 dunkaist 2308
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2309
        ret
2648 mario79 2310
;------------------------------------------------------------------------------
2288 clevermous 2311
sysfn_getallmem:
2312
        mov     eax, [MEM_AMOUNT]
2313
        shr     eax, 10
9831 dunkaist 2314
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2315
        ret
2648 mario79 2316
;------------------------------------------------------------------------------
2288 clevermous 2317
sysfn_pid_to_slot:
2318
        mov     eax, ecx
2319
        call    pid_to_slot
9831 dunkaist 2320
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2321
        ret
2648 mario79 2322
;------------------------------------------------------------------------------
2288 clevermous 2323
sysfn_min_rest_window:
2324
        pushad
2325
        mov     eax, edx ; ebx - operating
2326
        shr     ecx, 1
2327
        jnc     @f
2328
        call    pid_to_slot
2329
@@:
2330
        or      eax, eax ; eax - number of slot
2331
        jz      .error
9828 Doczom 2332
        cmp     eax, max_processes    ; varify maximal slot number
2288 clevermous 2333
        ja      .error
2334
        movzx   eax, word [WIN_STACK + eax*2]
2335
        shr     ecx, 1
2336
        jc      .restore
2337
 ; .minimize:
2338
        call    minimize_window
2339
        jmp     .exit
2340
.restore:
2341
        call    restore_minimized_window
2342
.exit:
2343
        popad
2344
        xor     eax, eax
9831 dunkaist 2345
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2346
        ret
2347
.error:
2348
        popad
2349
        xor     eax, eax
2350
        dec     eax
9831 dunkaist 2351
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2352
        ret
2648 mario79 2353
;------------------------------------------------------------------------------
2354
sysfn_min_windows:
2355
        call    minimize_all_window
9831 dunkaist 2356
        mov     [esp + SYSCALL_STACK.eax], eax
2648 mario79 2357
        call    change_task
2358
        ret
2359
;------------------------------------------------------------------------------
2654 mario79 2360
sysfn_set_screen_sizes:
2361
        cmp     [SCR_MODE], word 0x13
2362
        jbe     .exit
2363
 
2364
        cmp     [_display.select_cursor], select_cursor
2365
        jne     .exit
2366
 
2367
        cmp     ecx, [display_width_standard]
2368
        ja      .exit
2369
 
2370
        cmp     edx, [display_height_standard]
2371
        ja      .exit
2372
 
2373
        pushfd
2374
        cli
2375
        mov     eax, ecx
5351 serge 2376
        mov     ecx, [_display.lfb_pitch]
2654 mario79 2377
        mov     [_display.width], eax
2378
        dec     eax
2379
        mov     [_display.height], edx
2380
        dec     edx
2381
; eax - new Screen_Max_X
2382
; edx - new Screen_Max_Y
2383
        mov     [do_not_touch_winmap], 1
2384
        call    set_screen
2385
        mov     [do_not_touch_winmap], 0
2386
        popfd
2387
        call    change_task
2388
.exit:
2389
        ret
2390
;------------------------------------------------------------------------------
2288 clevermous 2391
uglobal
2392
screen_workarea RECT
2654 mario79 2393
display_width_standard dd 0
2394
display_height_standard dd 0
2395
do_not_touch_winmap db 0
2288 clevermous 2396
window_minimize db 0
2397
sound_flag      db 0
2654 mario79 2398
 
2288 clevermous 2399
endg
2400
 
2401
iglobal
2402
version_inf:
3454 mario79 2403
        db 0,7,7,0  ; version 0.7.7.0
2404
        db 0
2405
.rev    dd __REV__
9917 Doczom 2406
.size = $ - version_inf
2288 clevermous 2407
endg
2515 mario79 2408
;------------------------------------------------------------------------------
2409
align 4
2288 clevermous 2410
sys_cachetodiskette:
2411
        cmp     ebx, 1
4273 clevermous 2412
        jb      .no_floppy_save
2288 clevermous 2413
        cmp     ebx, 2
4273 clevermous 2414
        ja      .no_floppy_save
2288 clevermous 2415
        call    save_image
9831 dunkaist 2416
        mov     [esp + SYSCALL_STACK.eax], eax
4273 clevermous 2417
        ret
2418
.no_floppy_save:
9968 dunkaist 2419
        mov     [esp + SYSCALL_STACK.eax], 1
2288 clevermous 2420
        ret
2515 mario79 2421
;------------------------------------------------------------------------------
2422
align 4
2288 clevermous 2423
sys_cpuusage:
2424
 
9967 dunkaist 2425
;  RETURN: process_information structure
2288 clevermous 2426
;
8246 rgimad 2427
        ; if given memory address belongs to kernel then error
9967 dunkaist 2428
        stdcall is_region_userspace, ebx, sizeof.process_information
9045 dunkaist 2429
        jnz     .addr_error
2288 clevermous 2430
 
2431
        cmp     ecx, -1 ; who am I ?
2432
        jne     .no_who_am_i
8869 rgimad 2433
        mov     ecx, [current_slot_idx]
9968 dunkaist 2434
.no_who_am_i:
2435
        jecxz   .empty_slot
2288 clevermous 2436
        cmp     ecx, max_processes
9968 dunkaist 2437
        ja      .empty_slot
2438
        mov     edx, ecx
2439
        shl     edx, BSF sizeof.APPDATA
2440
        cmp     [SLOT_BASE+edx+APPDATA.state], TSTATE_FREE
2441
        jnz     .thread_found
2442
.empty_slot:
2443
        ; zero buffer for an empty slot
2444
        push    edi
2445
        xor     eax, eax
2446
        mov     edi, ebx
2447
        movi    ecx, sizeof.process_information
2448
        rep stosb
2449
        pop     edi
2450
        jmp     .nofillbuf
2451
.thread_found:
2288 clevermous 2452
; +4: word: position of the window of thread in the window stack
2453
        mov     ax, [WIN_STACK + ecx * 2]
9967 dunkaist 2454
        mov     [ebx+process_information.window_stack_position], ax
2288 clevermous 2455
; +6: word: number of the thread slot, which window has in the window stack
2456
;           position ecx (has no relation to the specific thread)
2457
        mov     ax, [WIN_POS + ecx * 2]
9967 dunkaist 2458
        mov     [ebx+process_information.window_stack_value], ax
2288 clevermous 2459
 
9715 Doczom 2460
        shl     ecx, BSF sizeof.APPDATA
2288 clevermous 2461
 
2462
; +0: dword: memory usage
9932 Doczom 2463
        mov     eax, [SLOT_BASE + ecx + APPDATA.cpu_usage]
9967 dunkaist 2464
        mov     [ebx+process_information.cpu_usage], eax
2288 clevermous 2465
; +10: 11 bytes: name of the process
2466
        push    ecx
9715 Doczom 2467
        lea     eax, [SLOT_BASE + ecx + APPDATA.app_name]
9967 dunkaist 2468
        add     ebx, process_information.process_name
2288 clevermous 2469
        mov     ecx, 11
2470
        call    memmove
2471
        pop     ecx
2472
 
2473
; +22: address of the process in memory
2474
; +26: size of used memory - 1
2475
        push    edi
2476
        lea     edi, [ebx+12]
2477
        xor     eax, eax
2478
        mov     edx, 0x100000*16
9932 Doczom 2479
        cmp     ecx, 1 shl BSF sizeof.APPDATA
2288 clevermous 2480
        je      .os_mem
9932 Doczom 2481
        mov     edx, [SLOT_BASE + ecx + APPDATA.process]
9715 Doczom 2482
        mov     edx, [edx + PROC.mem_used]
2288 clevermous 2483
        mov     eax, std_application_base_address
2484
.os_mem:
2485
        stosd
2486
        lea     eax, [edx-1]
2487
        stosd
2488
 
9941 Doczom 2489
        mov     edx, [SLOT_BASE + ecx + APPDATA.window]
9932 Doczom 2490
 
2288 clevermous 2491
; +30: PID/TID
9932 Doczom 2492
        mov     eax, [SLOT_BASE + ecx + APPDATA.tid]
2288 clevermous 2493
        stosd
2494
 
2495
    ; window position and size
2496
        push    esi
9941 Doczom 2497
        lea     esi, [edx + WDATA.box]
2288 clevermous 2498
        movsd
2499
        movsd
2500
        movsd
2501
        movsd
2502
 
2503
    ; Process state (+50)
9932 Doczom 2504
        movzx   eax, byte [SLOT_BASE + ecx + APPDATA.state]
2288 clevermous 2505
        stosd
2506
 
2507
    ; Window client area box
9941 Doczom 2508
        lea     esi, [edx + WDATA.clientbox]
2288 clevermous 2509
        movsd
2510
        movsd
2511
        movsd
2512
        movsd
2513
 
2514
    ; Window state
9941 Doczom 2515
        mov     al, [edx + WDATA.fl_wstate]
2288 clevermous 2516
        stosb
2517
 
2518
    ; Event mask (+71)
9968 dunkaist 2519
        mov     eax, [SLOT_BASE + ecx + APPDATA.event_mask]
2288 clevermous 2520
        stosd
4286 Serge 2521
 
4262 0CodErr 2522
    ; Keyboard mode (+75)
9968 dunkaist 2523
        mov     al, [SLOT_BASE + ecx + APPDATA.keyboard_mode]
4286 Serge 2524
        stosb
2288 clevermous 2525
 
2526
        pop     esi
2527
        pop     edi
2528
 
2529
.nofillbuf:
2530
    ; return number of processes
2531
 
8866 rgimad 2532
        mov     eax, [thread_count]
9831 dunkaist 2533
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2534
        ret
2535
 
8246 rgimad 2536
.addr_error:    ; if given memory address is illegal
9968 dunkaist 2537
        mov     [esp + SYSCALL_STACK.eax], -1
9947 ace_dent 2538
        ret
8246 rgimad 2539
 
2288 clevermous 2540
 
9612 Doczom 2541
; redraw status
2288 clevermous 2542
align 4
2543
sys_redrawstat:
2544
        cmp     ebx, 1
9910 Doczom 2545
        jne     .no_widgets_away
2288 clevermous 2546
        ; buttons away
8869 rgimad 2547
        mov     ecx, [current_slot_idx]
9910 Doczom 2548
.sys_newba2:
2288 clevermous 2549
        mov     edi, [BTN_ADDR]
2550
        cmp     [edi], dword 0  ; empty button list ?
9910 Doczom 2551
        je      .end_of_buttons_away
2288 clevermous 2552
        movzx   ebx, word [edi]
2553
        inc     ebx
2554
        mov     eax, edi
9910 Doczom 2555
.sys_newba:
2288 clevermous 2556
        dec     ebx
9910 Doczom 2557
        jz      .end_of_buttons_away
2288 clevermous 2558
 
2559
        add     eax, 0x10
2560
        cmp     cx, [eax]
9910 Doczom 2561
        jnz     .sys_newba
2288 clevermous 2562
 
2563
        push    eax ebx ecx
2564
        mov     ecx, ebx
2565
        inc     ecx
2566
        shl     ecx, 4
2567
        mov     ebx, eax
2568
        add     eax, 0x10
2569
        call    memmove
2570
        dec     dword [edi]
2571
        pop     ecx ebx eax
2572
 
9910 Doczom 2573
        jmp     .sys_newba2
2288 clevermous 2574
 
9910 Doczom 2575
.end_of_buttons_away:
2288 clevermous 2576
        ret
2577
 
9910 Doczom 2578
.no_widgets_away:
2288 clevermous 2579
 
2580
        cmp     ebx, 2
9910 Doczom 2581
        jnz     .srl1
2288 clevermous 2582
 
9930 Doczom 2583
        mov     edx, [current_slot]      ; return whole screen draw area for this app
2584
        mov     edx, [edx + APPDATA.window]
2585
        mov     [edx + WDATA.draw_data.left], 0
2586
        mov     [edx + WDATA.draw_data.top], 0
5350 serge 2587
        mov     eax, [_display.width]
2588
        dec     eax
9930 Doczom 2589
        mov     [edx + WDATA.draw_data.right], eax
5350 serge 2590
        mov     eax, [_display.height]
2591
        dec     eax
9930 Doczom 2592
        mov     [edx + WDATA.draw_data.bottom], eax
2288 clevermous 2593
 
9910 Doczom 2594
.srl1:
2288 clevermous 2595
        ret
2596
 
2597
;ok - 100% work
2598
;nt - not tested
2599
;---------------------------------------------------------------------------------------------
2600
;eax
2601
;0 - task switch counter. Ret switch counter in eax. Block. ok.
2602
;1 - change task. Ret nothing. Block. ok.
2603
;2 - performance control
2604
; ebx
2605
; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
2606
; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
2607
; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
2608
; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
2609
; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
2610
;eax
2611
;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
2612
;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
2613
;---------------------------------------------------------------------------------------------
2614
iglobal
2615
align 4
2616
sheduler:
2617
        dd      sys_sheduler.00
2618
        dd      change_task
2619
        dd      sys_sheduler.02
2620
        dd      sys_sheduler.03
2621
        dd      sys_sheduler.04
2622
endg
2623
sys_sheduler:
2624
;rewritten by   29.12.2009
9715 Doczom 2625
        jmp     dword [sheduler + ebx*4]
2288 clevermous 2626
;.shed_counter:
2627
.00:
2628
        mov     eax, [context_counter]
9831 dunkaist 2629
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2630
        ret
2631
 
2632
.02:
2633
;.perf_control:
2634
        inc     ebx                     ;before ebx=2, ebx=3
2635
        cmp     ebx, ecx                ;if ecx=3, ebx=3
2636
        jz      cache_disable
2637
 
2638
        dec     ebx                     ;ebx=2
2639
        cmp     ebx, ecx                ;
2640
        jz      cache_enable            ;if ecx=2 and ebx=2
2641
 
2642
        dec     ebx                     ;ebx=1
2643
        cmp     ebx, ecx
2644
        jz      is_cache_enabled        ;if ecx=1 and ebx=1
2645
 
2646
        dec     ebx
2647
        test    ebx, ecx                ;ebx=0 and ecx=0
2648
        jz      modify_pce              ;if ecx=0
2649
 
2650
        ret
2651
 
2652
.03:
2653
;.rdmsr_instr:
2654
;now counter in ecx
2655
;(edx:eax) esi:edi => edx:esi
2656
        mov     eax, esi
2657
        mov     ecx, edx
2658
        rdmsr
9831 dunkaist 2659
        mov     [esp + SYSCALL_STACK.eax], eax
2660
        mov     [esp + SYSCALL_STACK.ebx], edx           ;ret in ebx?
2288 clevermous 2661
        ret
2662
 
2663
.04:
2664
;.wrmsr_instr:
2665
;now counter in ecx
2666
;(edx:eax) esi:edi => edx:esi
2667
        ; Fast Call MSR can't be destroy
3539 clevermous 2668
        ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
2669
        ; включаются/выключаются расширенные возможности
2288 clevermous 2670
        cmp     edx, MSR_SYSENTER_CS
2671
        je      @f
2672
        cmp     edx, MSR_SYSENTER_ESP
2673
        je      @f
2674
        cmp     edx, MSR_SYSENTER_EIP
2675
        je      @f
2676
        cmp     edx, MSR_AMD_STAR
2677
        je      @f
2678
 
2679
        mov     eax, esi
2680
        mov     ecx, edx
2681
        wrmsr
2682
        ; mov   [esp + 32], eax
2683
        ; mov   [esp + 20], edx ;ret in ebx?
2684
@@:
2685
        ret
2686
 
2687
cache_disable:
2688
        mov     eax, cr0
9831 dunkaist 2689
        or      eax, 01100000_00000000_00000000_00000000b
2288 clevermous 2690
        mov     cr0, eax
2691
        wbinvd  ;set MESI
2692
        ret
2693
 
2694
cache_enable:
2695
        mov     eax, cr0
9831 dunkaist 2696
        and     eax, 10011111_11111111_11111111_11111111b
2288 clevermous 2697
        mov     cr0, eax
2698
        ret
2699
 
2700
is_cache_enabled:
2701
        mov     eax, cr0
9831 dunkaist 2702
        and     eax, 01100000_00000000_00000000_00000000b
9941 Doczom 2703
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2704
        ret
2705
 
2706
modify_pce:
2707
        mov     eax, cr4
2708
;       mov ebx,0
2709
;       or  bx,100000000b ;pce
2710
;       xor eax,ebx ;invert pce
2711
        bts     eax, 8;pce=cr4[8]
2712
        mov     cr4, eax
9831 dunkaist 2713
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2714
        ret
2715
;---------------------------------------------------------------------------------------------
2716
 
2717
 
2718
iglobal
2719
  cpustring db 'CPU',0
2720
endg
2721
 
2722
uglobal
2723
background_defined    db    0    ; diamond, 11.04.2006
2724
endg
2513 mario79 2725
;-----------------------------------------------------------------------------
2288 clevermous 2726
align 4
2727
checkmisc:
2728
        cmp     [ctrl_alt_del], 1
9910 Doczom 2729
        jne     .nocpustart
2288 clevermous 2730
 
2731
        mov     ebp, cpustring
2732
        call    fs_execute_from_sysdir
2733
 
2734
        mov     [ctrl_alt_del], 0
2513 mario79 2735
;--------------------------------------
2736
align 4
9910 Doczom 2737
.nocpustart:
2288 clevermous 2738
        cmp     [mouse_active], 1
9910 Doczom 2739
        jne     .mouse_not_active
2288 clevermous 2740
        mov     [mouse_active], 0
2411 Serge 2741
 
2288 clevermous 2742
        xor     edi, edi
9709 Doczom 2743
        mov     ebx, window_data
2411 Serge 2744
 
8866 rgimad 2745
        mov     ecx, [thread_count]
2408 Serge 2746
        movzx   eax, word [WIN_POS + ecx*2]     ; active window
9925 Doczom 2747
        shl     eax, BSF sizeof.APPDATA
2414 Serge 2748
        push    eax
2408 Serge 2749
 
2414 Serge 2750
        movzx   eax, word [MOUSE_X]
2751
        movzx   edx, word [MOUSE_Y]
2513 mario79 2752
;--------------------------------------
2408 Serge 2753
align 4
2754
.set_mouse_event:
8093 dunkaist 2755
        add     edi, sizeof.APPDATA
9709 Doczom 2756
        add     ebx, sizeof.WDATA
9917 Doczom 2757
        test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_MOUSE_FILTER
2414 Serge 2758
        jz      .pos_filter
2759
 
2760
        cmp     edi, [esp]                      ; skip if filtration active
2761
        jne     .skip
2513 mario79 2762
;--------------------------------------
2763
align 4
2414 Serge 2764
.pos_filter:
9917 Doczom 2765
        test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_CURSOR_FILTER
2411 Serge 2766
        jz      .set
2288 clevermous 2767
 
9709 Doczom 2768
        mov     esi, [ebx + WDATA.box.left]
2414 Serge 2769
        cmp     eax, esi
2770
        jb      .skip
9709 Doczom 2771
        add     esi, [ebx + WDATA.box.width]
2414 Serge 2772
        cmp     eax, esi
2773
        ja      .skip
2774
 
9709 Doczom 2775
        mov     esi, [ebx + WDATA.box.top]
2414 Serge 2776
        cmp     edx, esi
2777
        jb      .skip
9709 Doczom 2778
        add     esi, [ebx + WDATA.box.height]
2414 Serge 2779
        cmp     edx, esi
2780
        ja      .skip
2513 mario79 2781
;--------------------------------------
2782
align 4
2411 Serge 2783
.set:
9715 Doczom 2784
        or      [SLOT_BASE + edi + APPDATA.occurred_events], EVENT_MOUSE
2513 mario79 2785
;--------------------------------------
2786
align 4
2411 Serge 2787
.skip:
2408 Serge 2788
        loop    .set_mouse_event
2789
 
2414 Serge 2790
        pop     eax
2513 mario79 2791
;--------------------------------------
2792
align 4
9910 Doczom 2793
.mouse_not_active:
6585 pathoswith 2794
        cmp     [REDRAW_BACKGROUND], 0  ; background update ?
2288 clevermous 2795
        jz      nobackgr
2524 mario79 2796
 
2288 clevermous 2797
        cmp     [background_defined], 0
2798
        jz      nobackgr
2513 mario79 2799
;--------------------------------------
2800
align 4
3536 clevermous 2801
backgr:
9930 Doczom 2802
        mov     eax, [background_window + WDATA.draw_data.left]
2537 mario79 2803
        shl     eax, 16
9930 Doczom 2804
        add     eax, [background_window + WDATA.draw_data.right]
2537 mario79 2805
        mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
2806
 
9930 Doczom 2807
        mov     eax, [background_window + WDATA.draw_data.top]
2537 mario79 2808
        shl     eax, 16
9930 Doczom 2809
        add     eax, [background_window + WDATA.draw_data.bottom]
2537 mario79 2810
        mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
2811
 
2288 clevermous 2812
        call    drawbackground
2580 mario79 2813
;        DEBUGF  1, "K : drawbackground\n"
2620 mario79 2814
;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
2815
;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
2513 mario79 2816
;--------- set event 5 start ----------
2817
        push    ecx edi
9941 Doczom 2818
        mov     edi, window_data
8866 rgimad 2819
        mov     ecx, [thread_count]
2513 mario79 2820
;--------------------------------------
2821
align 4
2822
set_bgr_event:
9930 Doczom 2823
        add     edi, sizeof.WDATA
3536 clevermous 2824
        mov     eax, [BG_Rect_X_left_right]
2825
        mov     edx, [BG_Rect_Y_top_bottom]
9941 Doczom 2826
        cmp     [edi + WDATA.draw_bgr_x], 0
3536 clevermous 2827
        jz      .set
2828
.join:
9941 Doczom 2829
        cmp     word [edi + WDATA.draw_bgr_x], ax
3611 clevermous 2830
        jae     @f
9941 Doczom 2831
        mov     word [edi + WDATA.draw_bgr_x], ax
3611 clevermous 2832
@@:
2833
        shr     eax, 16
9941 Doczom 2834
        cmp     word [edi + WDATA.draw_bgr_x + 2], ax
3536 clevermous 2835
        jbe     @f
9941 Doczom 2836
        mov     word [edi + WDATA.draw_bgr_x + 2], ax
3536 clevermous 2837
@@:
9941 Doczom 2838
        cmp     word [edi + WDATA.draw_bgr_y], dx
3536 clevermous 2839
        jae     @f
9941 Doczom 2840
        mov     word [edi + WDATA.draw_bgr_y], dx
3536 clevermous 2841
@@:
3611 clevermous 2842
        shr     edx, 16
9941 Doczom 2843
        cmp     word [edi + WDATA.draw_bgr_y+2], dx
3536 clevermous 2844
        jbe     @f
9941 Doczom 2845
        mov     word [edi + WDATA.draw_bgr_y+2], dx
3536 clevermous 2846
@@:
2847
        jmp     .common
2848
.set:
9941 Doczom 2849
        mov     [edi + WDATA.draw_bgr_x], eax
2850
        mov     [edi + WDATA.draw_bgr_y], edx
3536 clevermous 2851
.common:
9941 Doczom 2852
        mov     eax, [edi + WDATA.thread]
9930 Doczom 2853
        test    eax, eax
2854
        jz      @f
2855
        or      [eax + APPDATA.occurred_events], EVENT_BACKGROUND
2856
@@:
9941 Doczom 2857
        loop    set_bgr_event
2513 mario79 2858
        pop     edi ecx
2859
;--------- set event 5 stop -----------
6585 pathoswith 2860
        dec     [REDRAW_BACKGROUND]     ; got new update request?
3536 clevermous 2861
        jnz     backgr
2513 mario79 2862
 
3536 clevermous 2863
        xor     eax, eax
9930 Doczom 2864
        mov     [background_window + WDATA.draw_data.left], eax
2865
        mov     [background_window + WDATA.draw_data.top], eax
2866
        mov     [background_window + WDATA.draw_data.right], eax
2867
        mov     [background_window + WDATA.draw_data.bottom], eax
2513 mario79 2868
;--------------------------------------
2869
align 4
2288 clevermous 2870
nobackgr:
2513 mario79 2871
; system shutdown request
2288 clevermous 2872
        cmp     [SYS_SHUTDOWN], byte 0
2873
        je      noshutdown
2874
 
2875
        mov     edx, [shutdown_processes]
2876
 
2877
        cmp     [SYS_SHUTDOWN], dl
3325 clevermous 2878
        jne     noshutdown
2288 clevermous 2879
 
2880
        lea     ecx, [edx-1]
9941 Doczom 2881
        mov     edx, SLOT_BASE + sizeof.APPDATA*2 ;OS_BASE+0x3040
3325 clevermous 2882
        jecxz   no_mark_system_shutdown
2513 mario79 2883
;--------------------------------------
2884
align 4
2288 clevermous 2885
markz:
3296 clevermous 2886
        push    ecx edx
9709 Doczom 2887
        cmp     [edx + APPDATA.state], TSTATE_FREE
3325 clevermous 2888
        jz      .nokill
9715 Doczom 2889
        cmp     [edx + APPDATA.process], sys_proc
3325 clevermous 2890
        jz      .nokill
3296 clevermous 2891
        call    request_terminate
3325 clevermous 2892
        jmp     .common
2893
.nokill:
2894
        dec     byte [SYS_SHUTDOWN]
2895
        xor     eax, eax
2896
.common:
3296 clevermous 2897
        pop     edx ecx
2898
        test    eax, eax
2899
        jz      @f
9709 Doczom 2900
        mov     [edx + APPDATA.state], TSTATE_ZOMBIE
3296 clevermous 2901
@@:
9709 Doczom 2902
        add     edx, sizeof.APPDATA
2288 clevermous 2903
        loop    markz
3534 clevermous 2904
        call    wakeup_osloop
2513 mario79 2905
;--------------------------------------
2906
align 4
2288 clevermous 2907
@@:
2513 mario79 2908
no_mark_system_shutdown:
2288 clevermous 2909
        dec     byte [SYS_SHUTDOWN]
2910
        je      system_shutdown
2513 mario79 2911
;--------------------------------------
2912
align 4
2288 clevermous 2913
noshutdown:
8866 rgimad 2914
        mov     eax, [thread_count]           ; termination
9709 Doczom 2915
        mov     ebx, SLOT_BASE + sizeof.APPDATA + APPDATA.state
2288 clevermous 2916
        mov     esi, 1
2513 mario79 2917
;--------------------------------------
2918
align 4
2288 clevermous 2919
newct:
2920
        mov     cl, [ebx]
9715 Doczom 2921
        cmp     cl, TSTATE_ZOMBIE
3325 clevermous 2922
        jz      .terminate
2513 mario79 2923
 
9715 Doczom 2924
        cmp     cl, TSTATE_TERMINATING
3325 clevermous 2925
        jnz     .noterminate
2926
.terminate:
2927
        pushad
9941 Doczom 2928
        push    esi
2929
        mov     ecx, dword[ebx - APPDATA.state + APPDATA.window]
3493 mario79 2930
        call    restore_default_cursor_before_killing
2931
 
9941 Doczom 2932
        pop     esi
3325 clevermous 2933
        call    terminate
2934
        popad
2935
        cmp     byte [SYS_SHUTDOWN], 0
2936
        jz      .noterminate
2937
        dec     byte [SYS_SHUTDOWN]
2938
        je      system_shutdown
2288 clevermous 2939
 
3325 clevermous 2940
.noterminate:
9709 Doczom 2941
        add     ebx, sizeof.APPDATA
2288 clevermous 2942
        inc     esi
2943
        dec     eax
2944
        jnz     newct
2945
        ret
2513 mario79 2946
;-----------------------------------------------------------------------------
2947
align 4
9941 Doczom 2948
; eax - ptr to WDATA
2288 clevermous 2949
redrawscreen:
2950
; eax , if process window_data base is eax, do not set flag/limits
2951
 
2952
        pushad
2953
        push    eax
2954
 
2955
;;;         mov   ebx,2
2956
;;;         call  delay_hs
2957
 
2958
         ;mov   ecx,0               ; redraw flags for apps
2959
        xor     ecx, ecx
2513 mario79 2960
;--------------------------------------
2961
align 4
2962
newdw2:
2288 clevermous 2963
        inc     ecx
2964
        push    ecx
2965
 
2966
        mov     eax, ecx
9715 Doczom 2967
        shl     eax, BSF sizeof.WDATA
2288 clevermous 2968
        add     eax, window_data
2969
 
2970
        cmp     eax, [esp+4]
2971
        je      not_this_task
2972
                                   ; check if window in redraw area
2973
        mov     edi, eax
2974
 
2975
        cmp     ecx, 1             ; limit for background
2976
        jz      bgli
2977
 
5870 GerdtR 2978
        mov     eax, [esp+4]        ;if upper in z-position - no redraw
2979
        test    eax, eax
2980
        jz      @f
2981
        mov     al, [eax + WDATA.z_modif]
2982
        cmp     [edi + WDATA.z_modif], al
2983
        jg      ricino
2984
      @@:
2985
 
2288 clevermous 2986
        mov     eax, [edi + WDATA.box.left]
2987
        mov     ebx, [edi + WDATA.box.top]
2988
 
9941 Doczom 2989
        cmp     ebx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
2990
        jae     ricino
2288 clevermous 2991
 
9941 Doczom 2992
        cmp     eax, [draw_limits.right] ; ecx = area x end     eax = window x start
2993
        jae     ricino
2288 clevermous 2994
 
2995
        mov     eax, [edi + WDATA.box.left]
2996
        mov     ebx, [edi + WDATA.box.top]
2997
        mov     ecx, [edi + WDATA.box.width]
2998
        mov     edx, [edi + WDATA.box.height]
2999
        add     ecx, eax
3000
        add     edx, ebx
3001
 
3002
        mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
3003
        cmp     edx, eax
3004
        jb      ricino
3005
 
3006
        mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
3007
        cmp     ecx, eax
3008
        jb      ricino
2513 mario79 3009
;--------------------------------------
3010
align 4
3011
bgli:
9941 Doczom 3012
        cmp     dword[esp], 1  ; check index in window_data array, 1 - idle
2288 clevermous 3013
        jnz     .az
2513 mario79 3014
 
6585 pathoswith 3015
        cmp     [REDRAW_BACKGROUND], 0
2288 clevermous 3016
        jz      .az
2513 mario79 3017
 
2288 clevermous 3018
        mov     dl, 0
3019
        mov     ebx, [draw_limits.left]
9930 Doczom 3020
        cmp     ebx, [edi + WDATA.draw_data.left]
2288 clevermous 3021
        jae     @f
2513 mario79 3022
 
9930 Doczom 3023
        mov     [edi + WDATA.draw_data.left], ebx
2288 clevermous 3024
        mov     dl, 1
2513 mario79 3025
;--------------------------------------
3026
align 4
3027
@@:
2288 clevermous 3028
        mov     ebx, [draw_limits.top]
9930 Doczom 3029
        cmp     ebx, [edi + WDATA.draw_data.top]
2288 clevermous 3030
        jae     @f
2513 mario79 3031
 
9930 Doczom 3032
        mov     [edi + WDATA.draw_data.top], ebx
2288 clevermous 3033
        mov     dl, 1
2513 mario79 3034
;--------------------------------------
3035
align 4
3036
@@:
2288 clevermous 3037
        mov     ebx, [draw_limits.right]
9930 Doczom 3038
        cmp     ebx, [edi + WDATA.draw_data.right]
2288 clevermous 3039
        jbe     @f
2513 mario79 3040
 
9930 Doczom 3041
        mov     [edi + WDATA.draw_data.right], ebx
2288 clevermous 3042
        mov     dl, 1
2513 mario79 3043
;--------------------------------------
3044
align 4
3045
@@:
2288 clevermous 3046
        mov     ebx, [draw_limits.bottom]
9930 Doczom 3047
        cmp     ebx, [edi + WDATA.draw_data.bottom]
2288 clevermous 3048
        jbe     @f
2513 mario79 3049
 
9930 Doczom 3050
        mov     [edi + WDATA.draw_data.bottom], ebx
2288 clevermous 3051
        mov     dl, 1
2513 mario79 3052
;--------------------------------------
3053
align 4
3054
@@:
6585 pathoswith 3055
        add     [REDRAW_BACKGROUND], dl
3534 clevermous 3056
        call    wakeup_osloop
2288 clevermous 3057
        jmp     newdw8
2513 mario79 3058
;--------------------------------------
3059
align 4
3060
.az:
2288 clevermous 3061
        mov     ebx, [draw_limits.left]        ; set limits
9941 Doczom 3062
        mov     [edi + WDATA.draw_data.left], ebx
2288 clevermous 3063
        mov     ebx, [draw_limits.top]
9941 Doczom 3064
        mov     [edi + WDATA.draw_data.top], ebx
2288 clevermous 3065
        mov     ebx, [draw_limits.right]
9941 Doczom 3066
        mov     [edi + WDATA.draw_data.right], ebx
2288 clevermous 3067
        mov     ebx, [draw_limits.bottom]
9941 Doczom 3068
        mov     [edi + WDATA.draw_data.bottom], ebx
2288 clevermous 3069
 
9941 Doczom 3070
        cmp     dword [esp], 1  ; check idle thread
2288 clevermous 3071
        jne     nobgrd
6585 pathoswith 3072
        inc     [REDRAW_BACKGROUND]
3534 clevermous 3073
        call    wakeup_osloop
2513 mario79 3074
;--------------------------------------
3075
align 4
3076
newdw8:
3077
nobgrd:
3936 mario79 3078
;--------------------------------------
9941 Doczom 3079
        push    edi ebp
3080
        mov     edi, [esp+8]
3936 mario79 3081
        cmp     edi, 1
3082
        je      .found
2288 clevermous 3083
 
3936 mario79 3084
        mov     eax, [draw_limits.left]
3085
        mov     ebx, [draw_limits.top]
3086
        mov     ecx, [draw_limits.right]
3087
        sub     ecx, eax
3088
        test    ecx, ecx
3089
        jz      .not_found
3090
 
3091
        mov     edx, [draw_limits.bottom]
3092
        sub     edx, ebx
3093
        test    edx, edx
3094
        jz      .not_found
3095
 
3096
; eax - x, ebx - y
3097
; ecx - size x, edx - size y
3098
        add     ebx, edx
3099
;--------------------------------------
3100
align 4
3101
.start_y:
3102
        push    ecx
3103
;--------------------------------------
3104
align 4
3105
.start_x:
3106
        add     eax, ecx
3107
        mov     ebp, [d_width_calc_area + ebx*4]
5351 serge 3108
        add     ebp, [_display.win_map]
3936 mario79 3109
        movzx   ebp, byte[eax+ebp] ; get value for current point
3110
        cmp     ebp, edi
3111
        jne     @f
3112
 
3113
        pop     ecx
3114
        jmp     .found
3115
;--------------------------------------
3116
align 4
3117
@@:
3118
        sub     eax, ecx
3119
 
3120
        dec     ecx
9221 dunkaist 3121
        jns     .start_x
3936 mario79 3122
 
3123
        pop     ecx
3124
        dec     ebx
3125
        dec     edx
9221 dunkaist 3126
        jns     .start_y
3936 mario79 3127
;--------------------------------------
3128
align 4
3129
.not_found:
9941 Doczom 3130
        pop     ebp edi
3936 mario79 3131
        jmp     ricino
3132
;--------------------------------------
3133
align 4
3134
.found:
9941 Doczom 3135
        pop     ebp edi
3936 mario79 3136
 
9941 Doczom 3137
        mov     [edi + WDATA.fl_redraw], WSTATE_REDRAW  ; mark as redraw
2513 mario79 3138
;--------------------------------------
3139
align 4
3140
ricino:
3141
not_this_task:
2288 clevermous 3142
        pop     ecx
3143
 
8866 rgimad 3144
        cmp     ecx, [thread_count]
2288 clevermous 3145
        jle     newdw2
3146
 
3147
        pop     eax
3148
        popad
3149
        ret
2513 mario79 3150
;-----------------------------------------------------------------------------
3151
align 4
2288 clevermous 3152
calculatebackground:   ; background
5351 serge 3153
        mov     edi, [_display.win_map]              ; set os to use all pixels
2288 clevermous 3154
        mov     eax, 0x01010101
5351 serge 3155
        mov     ecx, [_display.win_map_size]
2288 clevermous 3156
        shr     ecx, 2
3157
        rep stosd
9930 Doczom 3158
        mov     byte[background_window + WDATA.z_modif], ZPOS_DESKTOP
6585 pathoswith 3159
        mov     [REDRAW_BACKGROUND], 0
2288 clevermous 3160
        ret
2513 mario79 3161
;-----------------------------------------------------------------------------
2288 clevermous 3162
uglobal
3163
  imax    dd 0x0
3164
endg
2513 mario79 3165
;-----------------------------------------------------------------------------
3166
align 4
2288 clevermous 3167
delay_ms:     ; delay in 1/1000 sec
8139 dunkaist 3168
        pushad
3169
 
3170
        cmp     [hpet_base], 0
3171
        jz      .no_hpet
3172
        mov     eax, esi
8176 dunkaist 3173
        mov     edx, 1_000_000 ; ms to ns
8139 dunkaist 3174
        mul     edx
3175
        mov     ebx, edx
3176
        mov     ecx, eax
3177
 
2288 clevermous 3178
        push    ecx
8139 dunkaist 3179
        call    get_clock_ns
3180
        pop     ecx
3181
        mov     edi, edx
3182
        mov     esi, eax
3183
.wait:
3184
        push    ecx
3185
        call    get_clock_ns
3186
        pop     ecx
3187
        sub     eax, esi
3188
        sbb     edx, edi
3189
        sub     eax, ecx
3190
        sbb     edx, ebx
3191
        jc      .wait
3192
        jmp     .done
2288 clevermous 3193
 
8139 dunkaist 3194
.no_hpet:
2288 clevermous 3195
        mov     ecx, esi
3196
        ; 
3197
        imul    ecx, 33941
3198
        shr     ecx, 9
3199
        ; 
3200
 
3201
        in      al, 0x61
3202
        and     al, 0x10
3203
        mov     ah, al
3204
        cld
8139 dunkaist 3205
 
3206
.cnt1:
2288 clevermous 3207
        in      al, 0x61
3208
        and     al, 0x10
3209
        cmp     al, ah
8139 dunkaist 3210
        jz      .cnt1
2288 clevermous 3211
 
3212
        mov     ah, al
8139 dunkaist 3213
        loop    .cnt1
2288 clevermous 3214
 
8139 dunkaist 3215
.done:
3216
        popad
2288 clevermous 3217
        ret
2513 mario79 3218
;-----------------------------------------------------------------------------
2411 Serge 3219
align 4
2288 clevermous 3220
set_app_param:
9612 Doczom 3221
        mov     edi, [current_slot]
9614 Doczom 3222
        xchg    ebx, [edi + APPDATA.event_mask] ; set new event mask
9897 dunkaist 3223
        mov     [esp + SYSCALL_STACK.eax], ebx  ; return old mask value
2288 clevermous 3224
        ret
2513 mario79 3225
;-----------------------------------------------------------------------------
3303 clevermous 3226
 
3227
; this is for syscall
3228
proc delay_hs_unprotected
3229
        call    unprotect_from_terminate
3230
        call    delay_hs
3231
        call    protect_from_terminate
3232
        ret
3233
endp
3234
 
3597 Serge 3235
if 1
2513 mario79 3236
align 4
2288 clevermous 3237
delay_hs:     ; delay in 1/100 secs
3238
; ebx = delay time
3595 Serge 3239
        pushad
3240
        push    ebx
3241
        xor     esi, esi
3242
        mov     ecx, MANUAL_DESTROY
3243
        call    create_event
3244
        test    eax, eax
3245
        jz      .done
2288 clevermous 3246
 
3595 Serge 3247
        mov     ebx, edx
3248
        mov     ecx, [esp]
3597 Serge 3249
        push    edx
3595 Serge 3250
        push    eax
3251
        call    wait_event_timeout
3252
        pop     eax
3597 Serge 3253
        pop     ebx
3595 Serge 3254
        call    destroy_event
3255
.done:
3256
        add     esp, 4
3257
        popad
2288 clevermous 3258
        ret
3596 Serge 3259
 
3260
else
3261
 
3262
align 4
3263
delay_hs:     ; delay in 1/100 secs
3264
; ebx = delay time
3265
        push    ecx
3266
        push    edx
3267
 
3268
        mov     edx, [timer_ticks]
3269
;--------------------------------------
3270
align 4
9910 Doczom 3271
.newtic:
3596 Serge 3272
        mov     ecx, [timer_ticks]
3273
        sub     ecx, edx
3274
        cmp     ecx, ebx
9910 Doczom 3275
        jae     .zerodelay
3596 Serge 3276
 
3277
        call    change_task
3278
 
9910 Doczom 3279
        jmp     .newtic
3596 Serge 3280
;--------------------------------------
3281
align 4
9910 Doczom 3282
.zerodelay:
3596 Serge 3283
        pop     edx
3284
        pop     ecx
3285
        ret
3286
end if
3287
 
2513 mario79 3288
;-----------------------------------------------------------------------------
2288 clevermous 3289
align 16        ;very often call this subrutine
3290
memmove:       ; memory move in bytes
3291
; eax = from
3292
; ebx = to
3293
; ecx = no of bytes
3294
        test    ecx, ecx
3295
        jle     .ret
3296
 
3297
        push    esi edi ecx
3298
 
3299
        mov     edi, ebx
3300
        mov     esi, eax
3301
 
3302
        test    ecx, not 11b
3303
        jz      @f
3304
 
3305
        push    ecx
3306
        shr     ecx, 2
3307
        rep movsd
3308
        pop     ecx
3309
        and     ecx, 11b
3310
        jz      .finish
2513 mario79 3311
;--------------------------------------
3312
align 4
3313
@@:
2288 clevermous 3314
        rep movsb
2513 mario79 3315
;--------------------------------------
3316
align 4
3317
.finish:
2288 clevermous 3318
        pop     ecx edi esi
2513 mario79 3319
;--------------------------------------
3320
align 4
3321
.ret:
2288 clevermous 3322
        ret
2513 mario79 3323
;-----------------------------------------------------------------------------
2288 clevermous 3324
 
9742 Doczom 3325
; in: eax = port
3326
;     ebp = subfunction
3327
;          0 - set access
3328
;          1 - clear access
3329
; out: not return value
2288 clevermous 3330
align 4
3331
set_io_access_rights:
3332
        push    edi eax
3333
        mov     edi, tss._io_map_0
9743 Doczom 3334
 
9742 Doczom 3335
        test    ebp, ebp         ; enable access - ebp = 0
2288 clevermous 3336
        jnz     .siar1
9743 Doczom 3337
 
2288 clevermous 3338
        btr     [edi], eax
3339
        pop     eax edi
3340
        ret
3341
.siar1:
9742 Doczom 3342
        bts     [edi], eax      ; disable access - ebp = 1
2288 clevermous 3343
        pop     eax edi
3344
        ret
9279 Doczom 3345
 
3346
align 4
9743 Doczom 3347
; @brief ReservePortArea and FreePortArea
3348
; @param edx number end arrea of ports (include last number of port)
3349
; @param ecx number start arrea of ports
3350
; @param ebx sub function 0 - reserve, 1 - free
3351
; @param eax 46 - number function
3352
; @returns  eax = 0 - succesful eax = 1 - error
3353
syscall_reserveportarea:        ; ReservePortArea and FreePortArea
9279 Doczom 3354
 
3355
        call    r_f_port_area
9831 dunkaist 3356
        mov     [esp + SYSCALL_STACK.eax], eax
9279 Doczom 3357
        ret
3358
 
2288 clevermous 3359
;reserve/free group of ports
3360
;  * eax = 46 - number function
3361
;  * ebx = 0 - reserve, 1 - free
3362
;  * ecx = number start arrea of ports
3363
;  * edx = number end arrea of ports (include last number of port)
3364
;Return value:
3365
;  * eax = 0 - succesful
3366
;  * eax = 1 - error
3367
;  * The system has reserve this ports:
3368
;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
3369
;destroys eax,ebx, ebp
3370
r_f_port_area:
3371
 
3372
        test    ebx, ebx
9910 Doczom 3373
        jnz     .free_port_area
2288 clevermous 3374
 
3375
        cmp     ecx, edx      ; beginning > end ?
9917 Doczom 3376
        ja      .err
9743 Doczom 3377
        cmp     edx, 65536    ;test ebx, not 0xffff
9917 Doczom 3378
        jae     .err
2288 clevermous 3379
        mov     eax, [RESERVED_PORTS]
3380
        test    eax, eax      ; no reserved areas ?
9910 Doczom 3381
        je      .rpal2
2288 clevermous 3382
        cmp     eax, 255      ; max reserved
9917 Doczom 3383
        jae     .err
9910 Doczom 3384
 .rpal3:
2288 clevermous 3385
        mov     ebx, eax
9743 Doczom 3386
        shl     ebx, 4   ;16 byte is sizeof item in RESERVED_PORTS table
2288 clevermous 3387
        add     ebx, RESERVED_PORTS
3388
        cmp     ecx, [ebx+8]
9910 Doczom 3389
        ja      .rpal4
2288 clevermous 3390
        cmp     edx, [ebx+4]
9917 Doczom 3391
        jae     .err
9910 Doczom 3392
 .rpal4:
2288 clevermous 3393
        dec     eax
9910 Doczom 3394
        jnz     .rpal3
3395
        jmp     .rpal2
9917 Doczom 3396
.err:
2288 clevermous 3397
        xor     eax, eax
3398
        inc     eax
3399
        ret
9917 Doczom 3400
 
9910 Doczom 3401
   .rpal2:
2288 clevermous 3402
     ; enable port access at port IO map
3403
        pushad                        ; start enable io map
3404
        mov     eax, ecx
3405
        xor     ebp, ebp               ; enable - eax = port
9742 Doczom 3406
        cli
9910 Doczom 3407
.new_port_access:
2288 clevermous 3408
        call    set_io_access_rights
9742 Doczom 3409
 
2288 clevermous 3410
        inc     eax
3411
        cmp     eax, edx
9910 Doczom 3412
        jbe     .new_port_access
9917 Doczom 3413
 
9742 Doczom 3414
        sti
2288 clevermous 3415
        popad                         ; end enable io map
3416
 
3417
        mov     eax, [RESERVED_PORTS]
9742 Doczom 3418
        inc     eax
2288 clevermous 3419
        mov     [RESERVED_PORTS], eax
3420
        shl     eax, 4
3421
        add     eax, RESERVED_PORTS
9692 Doczom 3422
        mov     ebx, [current_slot]
9715 Doczom 3423
        mov     ebx, [ebx + APPDATA.tid]
9743 Doczom 3424
        mov     [eax], ebx   ; tid
3425
        mov     [eax+4], ecx ;start port
3426
        mov     [eax+8], edx ;finish port
2288 clevermous 3427
 
3428
        xor     eax, eax
3429
        ret
3430
 
9910 Doczom 3431
.free_port_area:
2288 clevermous 3432
 
3433
        mov     eax, [RESERVED_PORTS]; no reserved areas ?
3434
        test    eax, eax
9910 Doczom 3435
        jz      .frpal2
9692 Doczom 3436
        mov     ebx, [current_slot]
9715 Doczom 3437
        mov     ebx, [ebx + APPDATA.tid]
9910 Doczom 3438
   .frpal3:
2288 clevermous 3439
        mov     edi, eax
3440
        shl     edi, 4
3441
        add     edi, RESERVED_PORTS
3442
        cmp     ebx, [edi]
9910 Doczom 3443
        jne     .frpal4
2288 clevermous 3444
        cmp     ecx, [edi+4]
9910 Doczom 3445
        jne     .frpal4
2288 clevermous 3446
        cmp     edx, [edi+8]
9910 Doczom 3447
        jne     .frpal4
3448
        jmp     .frpal1
3449
   .frpal4:
2288 clevermous 3450
        dec     eax
9910 Doczom 3451
        jnz     .frpal3
3452
   .frpal2:
2288 clevermous 3453
        inc     eax
3454
        ret
9910 Doczom 3455
   .frpal1:
2288 clevermous 3456
        push    ecx
3457
        mov     ecx, 256
3458
        sub     ecx, eax
3459
        shl     ecx, 4
3460
        mov     esi, edi
3461
        add     esi, 16
3462
        cld
3463
        rep movsb
3464
 
3465
        dec     dword [RESERVED_PORTS]
3466
;disable port access at port IO map
9742 Doczom 3467
                        ; start disable io map
2288 clevermous 3468
        pop     eax     ;start port
9742 Doczom 3469
        ;cmp     edx, 65536
3470
        ;jge     no_mask_io
2288 clevermous 3471
 
3472
        xor     ebp, ebp
3473
        inc     ebp
9910 Doczom 3474
.new_port_access_disable:           ; disable - eax = port
2288 clevermous 3475
        call    set_io_access_rights
9742 Doczom 3476
 
2288 clevermous 3477
        inc     eax
3478
        cmp     eax, edx
9910 Doczom 3479
        jbe     .new_port_access_disable
9917 Doczom 3480
                                    ; end disable io map
2288 clevermous 3481
        xor     eax, eax
3482
        ret
2430 mario79 3483
;-----------------------------------------------------------------------------
2288 clevermous 3484
align 4
3485
drawbackground:
9968 dunkaist 3486
        cmp     [BgrDrawMode], 1
9910 Doczom 3487
        jne     .bgrstr
2288 clevermous 3488
        call    vesa20_drawbackground_tiled
2453 mario79 3489
        call    __sys_draw_pointer
2288 clevermous 3490
        ret
2430 mario79 3491
;--------------------------------------
3492
align 4
9910 Doczom 3493
.bgrstr:
2288 clevermous 3494
        call    vesa20_drawbackground_stretch
2453 mario79 3495
        call    __sys_draw_pointer
2288 clevermous 3496
        ret
2430 mario79 3497
;-----------------------------------------------------------------------------
2288 clevermous 3498
align 4
3499
syscall_putimage:                       ; PutImage
8714 Doczom 3500
; add check pointer
3501
        push    ecx
3502
        mov     ax, cx
3503
        shr     ecx, 16
3504
        imul    eax, ecx
3505
        lea     eax, [eax*3]
3506
        stdcall is_region_userspace, ebx, eax
3507
        pop     ecx
9045 dunkaist 3508
        jnz     sys_putimage.exit
8714 Doczom 3509
 
2288 clevermous 3510
sys_putimage:
3511
        test    ecx, 0x80008000
3512
        jnz     .exit
3513
        test    ecx, 0x0000FFFF
3514
        jz      .exit
3515
        test    ecx, 0xFFFF0000
3516
        jnz     @f
2430 mario79 3517
;--------------------------------------
3518
align 4
3519
.exit:
2288 clevermous 3520
        ret
2430 mario79 3521
;--------------------------------------
3522
align 4
3523
@@:
9948 Doczom 3524
        mov     edi, [current_slot]
9949 Doczom 3525
        mov     edi, [edi + APPDATA.window]
9948 Doczom 3526
        add     dx, word[edi + WDATA.clientbox.top]
2288 clevermous 3527
        rol     edx, 16
9948 Doczom 3528
        add     dx, word[edi + WDATA.clientbox.left]
2288 clevermous 3529
        rol     edx, 16
2430 mario79 3530
;--------------------------------------
3531
align 4
3532
.forced:
2288 clevermous 3533
        push    ebp esi 0
3534
        mov     ebp, putimage_get24bpp
3535
        mov     esi, putimage_init24bpp
2430 mario79 3536
;--------------------------------------
3537
align 4
2288 clevermous 3538
sys_putimage_bpp:
2430 mario79 3539
        call    vesa20_putimage
2288 clevermous 3540
        pop     ebp esi ebp
2430 mario79 3541
        ret
3542
;        jmp     [draw_pointer]
3543
;-----------------------------------------------------------------------------
2288 clevermous 3544
align 4
3545
sys_putimage_palette:
3546
; ebx = pointer to image
3547
; ecx = [xsize]*65536 + [ysize]
3548
; edx = [xstart]*65536 + [ystart]
3549
; esi = number of bits per pixel, must be 8, 24 or 32
3550
; edi = pointer to palette
3551
; ebp = row delta
8715 Doczom 3552
; check pointer
9910 Doczom 3553
        push    ecx esi
8715 Doczom 3554
        mov     ax, cx
3555
        shr     ecx, 16
3556
        imul    eax, ecx
9910 Doczom 3557
;        imul    eax, esi ; eax*count bit in 1 pixel
3558
;        shr     eax, 3
8715 Doczom 3559
        stdcall is_region_userspace, ebx, eax
9910 Doczom 3560
        pop     esi ecx
9045 dunkaist 3561
        jnz     sys_putimage.exit
8715 Doczom 3562
 
9949 Doczom 3563
        mov     eax, [current_slot]
3564
        mov     eax, [eax + APPDATA.window]
3565
        add     dx, word [eax + WDATA.clientbox.top]
2288 clevermous 3566
        rol     edx, 16
9949 Doczom 3567
        add     dx, word [eax + WDATA.clientbox.left]
2288 clevermous 3568
        rol     edx, 16
2430 mario79 3569
;--------------------------------------
3570
align 4
2288 clevermous 3571
.forced:
3572
        cmp     esi, 1
3573
        jnz     @f
3574
        push    edi
3575
        mov     eax, [edi+4]
3576
        sub     eax, [edi]
3577
        push    eax
3578
        push    dword [edi]
3579
        push    0ffffff80h
3580
        mov     edi, esp
3581
        call    put_mono_image
3582
        add     esp, 12
3583
        pop     edi
3584
        ret
2430 mario79 3585
;--------------------------------------
3586
align 4
2288 clevermous 3587
@@:
3588
        cmp     esi, 2
3589
        jnz     @f
3590
        push    edi
3591
        push    0ffffff80h
3592
        mov     edi, esp
3593
        call    put_2bit_image
3594
        pop     eax
3595
        pop     edi
3596
        ret
2430 mario79 3597
;--------------------------------------
3598
align 4
2288 clevermous 3599
@@:
3600
        cmp     esi, 4
3601
        jnz     @f
3602
        push    edi
3603
        push    0ffffff80h
3604
        mov     edi, esp
3605
        call    put_4bit_image
3606
        pop     eax
3607
        pop     edi
3608
        ret
2430 mario79 3609
;--------------------------------------
3610
align 4
2288 clevermous 3611
@@:
3612
        push    ebp esi ebp
3613
        cmp     esi, 8
3614
        jnz     @f
3615
        mov     ebp, putimage_get8bpp
3616
        mov     esi, putimage_init8bpp
3617
        jmp     sys_putimage_bpp
2430 mario79 3618
;--------------------------------------
3619
align 4
2288 clevermous 3620
@@:
2727 dunkaist 3621
        cmp     esi, 9
3622
        jnz     @f
3623
        mov     ebp, putimage_get9bpp
3624
        mov     esi, putimage_init9bpp
3625
        jmp     sys_putimage_bpp
3626
;--------------------------------------
3627
align 4
3628
@@:
2288 clevermous 3629
        cmp     esi, 15
3630
        jnz     @f
3631
        mov     ebp, putimage_get15bpp
3632
        mov     esi, putimage_init15bpp
3633
        jmp     sys_putimage_bpp
2430 mario79 3634
;--------------------------------------
3635
align 4
2288 clevermous 3636
@@:
3637
        cmp     esi, 16
3638
        jnz     @f
3639
        mov     ebp, putimage_get16bpp
3640
        mov     esi, putimage_init16bpp
3641
        jmp     sys_putimage_bpp
2430 mario79 3642
;--------------------------------------
3643
align 4
2288 clevermous 3644
@@:
3645
        cmp     esi, 24
3646
        jnz     @f
3647
        mov     ebp, putimage_get24bpp
3648
        mov     esi, putimage_init24bpp
3649
        jmp     sys_putimage_bpp
2430 mario79 3650
;--------------------------------------
3651
align 4
2288 clevermous 3652
@@:
3653
        cmp     esi, 32
3654
        jnz     @f
3655
        mov     ebp, putimage_get32bpp
3656
        mov     esi, putimage_init32bpp
3657
        jmp     sys_putimage_bpp
2430 mario79 3658
;--------------------------------------
3659
align 4
2288 clevermous 3660
@@:
3661
        pop     ebp esi ebp
3662
        ret
2430 mario79 3663
;-----------------------------------------------------------------------------
3664
align 4
2288 clevermous 3665
put_mono_image:
3666
        push    ebp esi ebp
3667
        mov     ebp, putimage_get1bpp
3668
        mov     esi, putimage_init1bpp
3669
        jmp     sys_putimage_bpp
2430 mario79 3670
;-----------------------------------------------------------------------------
3671
align 4
2288 clevermous 3672
put_2bit_image:
3673
        push    ebp esi ebp
3674
        mov     ebp, putimage_get2bpp
3675
        mov     esi, putimage_init2bpp
3676
        jmp     sys_putimage_bpp
2430 mario79 3677
;-----------------------------------------------------------------------------
3678
align 4
2288 clevermous 3679
put_4bit_image:
3680
        push    ebp esi ebp
3681
        mov     ebp, putimage_get4bpp
3682
        mov     esi, putimage_init4bpp
3683
        jmp     sys_putimage_bpp
2430 mario79 3684
;-----------------------------------------------------------------------------
3685
align 4
2288 clevermous 3686
putimage_init24bpp:
3687
        lea     eax, [eax*3]
3688
putimage_init8bpp:
2727 dunkaist 3689
putimage_init9bpp:
2288 clevermous 3690
        ret
2430 mario79 3691
;-----------------------------------------------------------------------------
2288 clevermous 3692
align 16
3693
putimage_get24bpp:
3694
        movzx   eax, byte [esi+2]
3695
        shl     eax, 16
3696
        mov     ax, [esi]
3697
        add     esi, 3
3698
        ret     4
2430 mario79 3699
;-----------------------------------------------------------------------------
2288 clevermous 3700
align 16
3701
putimage_get8bpp:
3702
        movzx   eax, byte [esi]
3703
        push    edx
3704
        mov     edx, [esp+8]
9715 Doczom 3705
        mov     eax, [edx + eax*4]
2288 clevermous 3706
        pop     edx
3707
        inc     esi
3708
        ret     4
2430 mario79 3709
;-----------------------------------------------------------------------------
2727 dunkaist 3710
align 16
3711
putimage_get9bpp:
3712
        lodsb
3713
        mov     ah, al
3714
        shl     eax, 8
3715
        mov     al, ah
3716
        ret     4
3717
;-----------------------------------------------------------------------------
2430 mario79 3718
align 4
2288 clevermous 3719
putimage_init1bpp:
3720
        add     eax, ecx
3721
        push    ecx
3722
        add     eax, 7
3723
        add     ecx, 7
3724
        shr     eax, 3
3725
        shr     ecx, 3
3726
        sub     eax, ecx
3727
        pop     ecx
3728
        ret
2430 mario79 3729
;-----------------------------------------------------------------------------
2288 clevermous 3730
align 16
3731
putimage_get1bpp:
3732
        push    edx
3733
        mov     edx, [esp+8]
3734
        mov     al, [edx]
3735
        add     al, al
3736
        jnz     @f
3737
        lodsb
3738
        adc     al, al
3739
@@:
3740
        mov     [edx], al
3741
        sbb     eax, eax
3742
        and     eax, [edx+8]
3743
        add     eax, [edx+4]
3744
        pop     edx
3745
        ret     4
2430 mario79 3746
;-----------------------------------------------------------------------------
3747
align 4
2288 clevermous 3748
putimage_init2bpp:
3749
        add     eax, ecx
3750
        push    ecx
3751
        add     ecx, 3
3752
        add     eax, 3
3753
        shr     ecx, 2
3754
        shr     eax, 2
3755
        sub     eax, ecx
3756
        pop     ecx
3757
        ret
2430 mario79 3758
;-----------------------------------------------------------------------------
2288 clevermous 3759
align 16
3760
putimage_get2bpp:
3761
        push    edx
3762
        mov     edx, [esp+8]
3763
        mov     al, [edx]
3764
        mov     ah, al
3765
        shr     al, 6
3766
        shl     ah, 2
3767
        jnz     .nonewbyte
3768
        lodsb
3769
        mov     ah, al
3770
        shr     al, 6
3771
        shl     ah, 2
3772
        add     ah, 1
3773
.nonewbyte:
3774
        mov     [edx], ah
3775
        mov     edx, [edx+4]
3776
        movzx   eax, al
9715 Doczom 3777
        mov     eax, [edx + eax*4]
2288 clevermous 3778
        pop     edx
3779
        ret     4
2430 mario79 3780
;-----------------------------------------------------------------------------
3781
align 4
2288 clevermous 3782
putimage_init4bpp:
3783
        add     eax, ecx
3784
        push    ecx
3785
        add     ecx, 1
9268 Doczom 3786
        inc     eax      ;add   eax, 1
2288 clevermous 3787
        shr     ecx, 1
3788
        shr     eax, 1
3789
        sub     eax, ecx
3790
        pop     ecx
3791
        ret
2430 mario79 3792
;-----------------------------------------------------------------------------
2288 clevermous 3793
align 16
3794
putimage_get4bpp:
3795
        push    edx
3796
        mov     edx, [esp+8]
3797
        add     byte [edx], 80h
3798
        jc      @f
3799
        movzx   eax, byte [edx+1]
3800
        mov     edx, [edx+4]
3801
        and     eax, 0x0F
9715 Doczom 3802
        mov     eax, [edx + eax*4]
2288 clevermous 3803
        pop     edx
3804
        ret     4
3805
@@:
3806
        movzx   eax, byte [esi]
3807
        add     esi, 1
3808
        mov     [edx+1], al
3809
        shr     eax, 4
3810
        mov     edx, [edx+4]
9715 Doczom 3811
        mov     eax, [edx + eax*4]
2288 clevermous 3812
        pop     edx
3813
        ret     4
2430 mario79 3814
;-----------------------------------------------------------------------------
3815
align 4
2288 clevermous 3816
putimage_init32bpp:
3817
        shl     eax, 2
3818
        ret
2430 mario79 3819
;-----------------------------------------------------------------------------
2288 clevermous 3820
align 16
3821
putimage_get32bpp:
3822
        lodsd
3823
        ret     4
2430 mario79 3824
;-----------------------------------------------------------------------------
3825
align 4
2288 clevermous 3826
putimage_init15bpp:
3827
putimage_init16bpp:
3828
        add     eax, eax
3829
        ret
2430 mario79 3830
;-----------------------------------------------------------------------------
2288 clevermous 3831
align 16
3832
putimage_get15bpp:
3833
; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
3834
        push    ecx edx
3835
        movzx   eax, word [esi]
3836
        add     esi, 2
3837
        mov     ecx, eax
3838
        mov     edx, eax
3839
        and     eax, 0x1F
3840
        and     ecx, 0x1F shl 5
3841
        and     edx, 0x1F shl 10
3842
        shl     eax, 3
3843
        shl     ecx, 6
3844
        shl     edx, 9
3845
        or      eax, ecx
3846
        or      eax, edx
3847
        pop     edx ecx
3848
        ret     4
2430 mario79 3849
;-----------------------------------------------------------------------------
2288 clevermous 3850
align 16
3851
putimage_get16bpp:
3852
; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
3853
        push    ecx edx
3854
        movzx   eax, word [esi]
3855
        add     esi, 2
3856
        mov     ecx, eax
3857
        mov     edx, eax
3858
        and     eax, 0x1F
3859
        and     ecx, 0x3F shl 5
3860
        and     edx, 0x1F shl 11
3861
        shl     eax, 3
3862
        shl     ecx, 5
3863
        shl     edx, 8
3864
        or      eax, ecx
3865
        or      eax, edx
3866
        pop     edx ecx
3867
        ret     4
2430 mario79 3868
;-----------------------------------------------------------------------------
3869
;align 4
2288 clevermous 3870
; eax x beginning
3871
; ebx y beginning
3872
; ecx x end
3873
        ; edx y end
3874
; edi color
2430 mario79 3875
;__sys_drawbar:
9949 Doczom 3876
;        mov     esi, [current_slot]
3877
;        mov     esi, [esi + APPDATA.window]
3878
;        add     eax, [esi + WDATA.clientbox.left]
3879
;        add     ecx, [esi + WDATA.clientbox.left]
3880
;        add     ebx, [esi + WDATA.clientbox.top]
3881
;        add     edx, [esi + WDATA.clientbox.top]
2430 mario79 3882
;--------------------------------------
3883
;align 4
3884
;.forced:
3885
;        call    vesa20_drawbar
2407 mario79 3886
;        call    [draw_pointer]
3887
;        ret
2430 mario79 3888
;-----------------------------------------------------------------------------
2288 clevermous 3889
if used _rdtsc
3890
_rdtsc:
3891
        bt      [cpu_caps], CAPS_TSC
9910 Doczom 3892
        jnc     .ret_rdtsc
2288 clevermous 3893
        rdtsc
3894
        ret
9910 Doczom 3895
   .ret_rdtsc:
2288 clevermous 3896
        mov     edx, 0xffffffff
3897
        mov     eax, 0xffffffff
3898
        ret
3899
end if
3900
 
3901
sys_msg_board_str:
3902
 
3903
        pushad
3904
   @@:
3905
        cmp     [esi], byte 0
3906
        je      @f
3614 shikhin 3907
        mov     ebx, 1
3908
        movzx   ecx, byte [esi]
2288 clevermous 3909
        call    sys_msg_board
3910
        inc     esi
3911
        jmp     @b
3912
   @@:
3913
        popad
3914
        ret
3915
 
3916
sys_msg_board_byte:
3917
; in: al = byte to display
3918
; out: nothing
3919
; destroys: nothing
3920
        pushad
3921
        mov     ecx, 2
3922
        shl     eax, 24
3923
        jmp     @f
3924
 
3925
sys_msg_board_word:
3926
; in: ax = word to display
3927
; out: nothing
3928
; destroys: nothing
3929
        pushad
3930
        mov     ecx, 4
3931
        shl     eax, 16
3932
        jmp     @f
3933
 
3934
sys_msg_board_dword:
3935
; in: eax = dword to display
3936
; out: nothing
3937
; destroys: nothing
3938
        pushad
3939
        mov     ecx, 8
3940
@@:
3941
        push    ecx
3942
        rol     eax, 4
3943
        push    eax
3944
        and     al, 0xF
3945
        cmp     al, 10
3946
        sbb     al, 69h
3947
        das
3614 shikhin 3948
        mov     cl, al
3949
        xor     ebx, ebx
3950
        inc     ebx
2288 clevermous 3951
        call    sys_msg_board
3952
        pop     eax
3953
        pop     ecx
3954
        loop    @b
3955
        popad
3956
        ret
3957
 
3392 clevermous 3958
msg_board_data_size = 65536 ; Must be power of two
3959
 
2288 clevermous 3960
uglobal
6501 pathoswith 3961
msg_board_data  rb  msg_board_data_size
3962
msg_board_count dd  ?
2288 clevermous 3963
endg
3964
 
6501 pathoswith 3965
iglobal
3966
msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
3967
endg
3968
 
2288 clevermous 3969
sys_msg_board:
6501 pathoswith 3970
; ebx=1 -> write, cl = byte to write
3971
; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
3972
        push    eax ebx
3614 shikhin 3973
        mov     eax, ebx
3974
        mov     ebx, ecx
2288 clevermous 3975
        mov     ecx, [msg_board_count]
3976
        cmp     eax, 1
6501 pathoswith 3977
        jne     .read
2288 clevermous 3978
 
3979
if defined debug_com_base
3980
        push    dx ax
6501 pathoswith 3981
@@: ; wait for empty transmit register
2288 clevermous 3982
        mov     dx, debug_com_base+5
3983
        in      al, dx
3984
        test    al, 1 shl 5
3985
        jz      @r
3986
        mov     dx, debug_com_base      ; Output the byte
3987
        mov     al, bl
3988
        out     dx, al
3989
        pop     ax dx
3990
end if
3991
 
3992
        mov     [msg_board_data+ecx], bl
3777 yogev_ezra 3993
        cmp     byte [debug_direct_print], 1
6501 pathoswith 3994
        jnz     .end
3507 clevermous 3995
        pusha
3996
        lea     edx, [msg_board_data+ecx]
3997
        mov     ecx, 0x40FFFFFF
3998
        mov     ebx, [msg_board_pos]
3999
        mov     edi, 1
9268 Doczom 4000
        mov     esi, edi ;1
3507 clevermous 4001
        call    dtext
4002
        popa
4003
        add     word [msg_board_pos+2], 6
6501 pathoswith 4004
        cmp     word [msg_board_pos+2], 105*6
4005
        jnc     @f
3507 clevermous 4006
        cmp     bl, 10
6501 pathoswith 4007
        jnz     .end
4008
@@:
4009
        mov     word [msg_board_pos+2], 42*6
3507 clevermous 4010
        add     word [msg_board_pos], 10
6501 pathoswith 4011
        mov     eax, [_display.height]
4012
        sub     eax, 10
4013
        cmp     ax, word [msg_board_pos]
4014
        jnc     @f
3507 clevermous 4015
        mov     word [msg_board_pos], 10
4016
@@:
3534 clevermous 4017
        pusha
6501 pathoswith 4018
        mov     eax, [msg_board_pos]
4019
        movzx   ebx, ax
4020
        shr     eax, 16
4021
        mov     edx, 105*6
4022
        xor     ecx, ecx
4023
        mov     edi, 1
4024
        mov     esi, 9
4025
@@:
4026
        call    hline
4027
        inc     ebx
4028
        dec     esi
4029
        jnz     @b
3534 clevermous 4030
        popa
6501 pathoswith 4031
.end:
2288 clevermous 4032
        inc     ecx
3392 clevermous 4033
        and     ecx, msg_board_data_size - 1
2288 clevermous 4034
        mov     [msg_board_count], ecx
6501 pathoswith 4035
.ret:
3614 shikhin 4036
        pop     ebx eax
2288 clevermous 4037
        ret
6501 pathoswith 4038
 
4039
.read:
2288 clevermous 4040
        cmp     eax, 2
6501 pathoswith 4041
        jne     .ret
9897 dunkaist 4042
        add     esp, 8  ; returning data in ebx and eax, so no need to restore them
2288 clevermous 4043
        test    ecx, ecx
9897 dunkaist 4044
        jnz     @f
4045
        mov     [esp + SYSCALL_STACK.eax], ecx
4046
        mov     [esp + SYSCALL_STACK.ebx], ecx
4047
        ret
4048
@@:
2288 clevermous 4049
        mov     eax, msg_board_data+1
4050
        mov     ebx, msg_board_data
4051
        movzx   edx, byte [ebx]
4052
        call    memmove
4053
        dec     [msg_board_count]
9897 dunkaist 4054
        mov     [esp + SYSCALL_STACK.eax], edx
4055
        mov     [esp + SYSCALL_STACK.ebx], 1
2288 clevermous 4056
        ret
4057
 
4058
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4059
;; 61 sys function.                                                ;;
4060
;; in eax=61,ebx in [1..3]                                         ;;
4061
;; out eax                                                         ;;
4062
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4063
iglobal
4064
align 4
4065
f61call:
4066
           dd sys_gs.1   ; resolution
4067
           dd sys_gs.2   ; bits per pixel
4068
           dd sys_gs.3   ; bytes per scanline
4069
endg
4070
 
4071
 
4072
align 4
4073
 
4074
sys_gs:                         ; direct screen access
4075
        dec     ebx
4076
        cmp     ebx, 2
4077
        ja      .not_support
9715 Doczom 4078
        jmp     dword [f61call + ebx*4]
2288 clevermous 4079
.not_support:
9968 dunkaist 4080
        or      [esp + SYSCALL_STACK.eax], -1
2288 clevermous 4081
        ret
4082
 
4083
 
4084
.1:                             ; resolution
5350 serge 4085
        mov     eax, [_display.width]
2288 clevermous 4086
        shl     eax, 16
5350 serge 4087
        mov     ax, word [_display.height]
9831 dunkaist 4088
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4089
        ret
4090
.2:                             ; bits per pixel
5154 hidnplayr 4091
        mov     eax, [_display.bits_per_pixel]
9831 dunkaist 4092
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4093
        ret
4094
.3:                             ; bytes per scanline
5351 serge 4095
        mov     eax, [_display.lfb_pitch]
9831 dunkaist 4096
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4097
        ret
4098
 
4099
align 4
4100
syscall_getscreensize:                  ; GetScreenSize
5350 serge 4101
        mov     ax, word [_display.width]
4102
        dec     ax
2288 clevermous 4103
        shl     eax, 16
5350 serge 4104
        mov     ax, word [_display.height]
4105
        dec     ax
9831 dunkaist 4106
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4107
        ret
4700 mario79 4108
;-----------------------------------------------------------------------------
2288 clevermous 4109
align 4
4713 mario79 4110
syscall_cdaudio:
4711 mario79 4111
; ECX - position of CD/DVD-drive
4112
; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
4113
; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
4114
; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
4115
        cmp     ecx, 11
4116
        ja      .exit
4117
 
4118
        mov     eax, ecx
4119
        shr     eax, 2
4120
        lea     eax, [eax*5]
9715 Doczom 4121
        mov     al, [eax + DRIVE_DATA + 1]
4711 mario79 4122
 
4123
        push    ecx ebx
4124
        mov     ebx, ecx
4125
        and     ebx, 11b
4126
        shl     ebx, 1
4127
        mov     cl, 6
4128
        sub     cl, bl
4129
        shr     al, cl
4130
        test    al, 2 ; it's not an ATAPI device
4131
        pop     ebx ecx
4132
 
4133
        jz      .exit
4134
 
4700 mario79 4135
        cmp     ebx, 4
4136
        je      .eject
2288 clevermous 4137
 
4138
        cmp     ebx, 5
4700 mario79 4139
        je      .load
4711 mario79 4140
;--------------------------------------
4141
.exit:
4700 mario79 4142
        ret
4143
;--------------------------------------
2288 clevermous 4144
.load:
4145
        call    .reserve
4146
        call    LoadMedium
4147
        jmp     .free
4700 mario79 4148
;--------------------------------------
2288 clevermous 4149
.eject:
4150
        call    .reserve
4151
        call    clear_CD_cache
4152
        call    allow_medium_removal
4153
        call    EjectMedium
4154
        jmp     .free
4700 mario79 4155
;--------------------------------------
2288 clevermous 4156
.reserve:
4157
        call    reserve_cd
4700 mario79 4158
 
4159
        mov     ebx, ecx
4160
        inc     ebx
4161
        mov     [cdpos], ebx
4162
 
2288 clevermous 4163
        mov     eax, ecx
4164
        shr     eax, 1
4165
        and     eax, 1
4166
        inc     eax
6464 pathoswith 4167
        mov     [ChannelNumber], al
2288 clevermous 4168
        mov     eax, ecx
4169
        and     eax, 1
4170
        mov     [DiskNumber], al
4171
        call    reserve_cd_channel
4172
        ret
4700 mario79 4173
;--------------------------------------
2288 clevermous 4174
.free:
4175
        call    free_cd_channel
4176
        and     [cd_status], 0
4177
        ret
2511 mario79 4178
;-----------------------------------------------------------------------------
2288 clevermous 4179
align 4
2511 mario79 4180
syscall_getpixel_WinMap:                       ; GetPixel WinMap
9948 Doczom 4181
        xor     eax, eax
4182
 
5350 serge 4183
        cmp     ebx, [_display.width]
9948 Doczom 4184
        jae     .store
5350 serge 4185
        cmp     ecx, [_display.height]
9948 Doczom 4186
        jae     .store
2511 mario79 4187
;--------------------------------------
4188
        mov     eax, [d_width_calc_area + ecx*4]
5351 serge 4189
        add     eax, [_display.win_map]
2511 mario79 4190
        movzx   eax, byte[eax+ebx]        ; get value for current point
4191
;--------------------------------------
4192
align 4
4193
.store:
9831 dunkaist 4194
        mov     [esp + SYSCALL_STACK.eax], eax
2511 mario79 4195
        ret
4196
;-----------------------------------------------------------------------------
4197
align 4
2288 clevermous 4198
syscall_getpixel:                       ; GetPixel
5350 serge 4199
        mov     ecx, [_display.width]
2288 clevermous 4200
        xor     edx, edx
4201
        mov     eax, ebx
4202
        div     ecx
4203
        mov     ebx, edx
4204
        xchg    eax, ebx
2430 mario79 4205
        and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
2288 clevermous 4206
        call    dword [GETPIXEL]; eax - x, ebx - y
9831 dunkaist 4207
        mov     [esp + SYSCALL_STACK.eax], ecx
2288 clevermous 4208
        ret
2509 mario79 4209
;-----------------------------------------------------------------------------
2288 clevermous 4210
align 4
4211
syscall_getarea:
4212
;eax = 36
4213
;ebx = pointer to bufer for img BBGGRRBBGGRR...
4214
;ecx = [size x]*65536 + [size y]
4215
;edx = [start x]*65536 + [start y]
4216
        pushad
4217
        mov     edi, ebx
4218
        mov     eax, edx
4219
        shr     eax, 16
4220
        mov     ebx, edx
4221
        and     ebx, 0xffff
4222
        dec     eax
4223
        dec     ebx
4224
     ; eax - x, ebx - y
4225
        mov     edx, ecx
4226
 
4227
        shr     ecx, 16
4228
        and     edx, 0xffff
4229
        mov     esi, ecx
4230
     ; ecx - size x, edx - size y
4231
 
4232
        mov     ebp, edx
8598 rgimad 4233
        lea     ebp, [ebp*3]
4234
        imul    ebp, esi
4235
        stdcall is_region_userspace, edi, ebp
9045 dunkaist 4236
        jnz     .exit
8598 rgimad 4237
 
4238
        mov     ebp, edx
2288 clevermous 4239
        dec     ebp
4240
        lea     ebp, [ebp*3]
4241
 
4242
        imul    ebp, esi
4243
 
4244
        mov     esi, ecx
4245
        dec     esi
4246
        lea     esi, [esi*3]
4247
 
4248
        add     ebp, esi
4249
        add     ebp, edi
4250
 
4251
        add     ebx, edx
2509 mario79 4252
;--------------------------------------
4253
align 4
2288 clevermous 4254
.start_y:
4255
        push    ecx edx
2509 mario79 4256
;--------------------------------------
4257
align 4
2288 clevermous 4258
.start_x:
4259
        push    eax ebx ecx
4260
        add     eax, ecx
4261
 
2430 mario79 4262
        and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
2288 clevermous 4263
        call    dword [GETPIXEL]; eax - x, ebx - y
4264
 
4265
        mov     [ebp], cx
4266
        shr     ecx, 16
4267
        mov     [ebp+2], cl
4268
 
4269
        pop     ecx ebx eax
4270
        sub     ebp, 3
4271
        dec     ecx
4272
        jnz     .start_x
4273
        pop     edx ecx
4274
        dec     ebx
4275
        dec     edx
4276
        jnz     .start_y
9947 ace_dent 4277
 
8598 rgimad 4278
.exit:
2288 clevermous 4279
        popad
4280
        ret
2509 mario79 4281
;-----------------------------------------------------------------------------
8675 rgimad 4282
 
2288 clevermous 4283
align 4
10002 Jurgen 4284
syscall_threads:
4285
; eax = 51
4286
; if ebx = 1  CreateThreads
4287
; ecx = thread entry point
4288
; edx = thread stack pointer
4289
; return eax = pid
4290
 
4291
; if ebx = 2 GetCurrentThreadId
4292
; return eax = current id slot
4293
 
4294
; if ebx = 3 GetThreadPriority
4295
; ecx = -1 curr slot  or  id slot
4296
; return eax = priority
4297
 
4298
; if ebx = 4 SetThreadPriority
4299
; ecx = -1 curr slot or id slot
4300
; edx = set priority
4301
; return eax = old priority
4302
 
4303
        dec     ebx
4304
        jz      .create
4305
        mov     eax, [current_slot_idx]
4306
        dec     ebx
4307
        jz      .end  ;get_curr_slot
4308
 
4309
        test    ecx, ecx  ;-1 curr slot
4310
        jl      .cur_slot
4311
        mov     eax, ecx
4312
.cur_slot:
4313
        shl     eax, BSF sizeof.APPDATA
4314
        add     eax, SLOT_BASE
4315
 
4316
        test    ecx, ecx  ;-1 curr slot
4317
        jl      .curr_slot
4318
 
4319
        mov     esi, .err_exit
4320
        mov     edi, CONTROL_EXCEPTION
4321
 
4322
        movzx   ecx, [eax+APPDATA.state]
4323
        test    ecx, ecx
4324
        jnz     .check_state
4325
 
4326
        cmp     [eax+APPDATA.tid], ecx
4327
        jnz     .curr_slot
4328
        jmp     .err_exit
4329
 
4330
.check_state:
4331
        sub     ecx, 3 ;TSTATE_ZOMBIE
4332
        jl      .curr_slot
4333
        je      .err_exit
4334
        dec     ecx ; 4 TSTATE_TERMINATING
4335
        dec     ecx ; 5 TSTATE_WAITING
4336
        jnz     .err_exit
4337
 
4338
.curr_slot:
4339
        dec     ebx
4340
        jz      .get_priority
4341
        dec     ebx
4342
        jz      .set_priority
4343
 
4344
.err_exit:
4345
        push    -1
4346
        pop     eax
4347
        jmp     .end
4348
 
4349
.set_priority:  ;;sysfn 51,4
4350
        mov     dh, dl
4351
        lock xchg word [eax+APPDATA.def_priority], dx
4352
        movzx   eax, dl ;old priority
4353
        jmp     .end
4354
 
4355
.get_priority:  ;;sysfn 51,3
4356
        movzx   eax, [eax+APPDATA.def_priority]
4357
        jmp     .end
4358
 
4359
.create:        ;sysfn 51,1
2288 clevermous 4360
        call    new_sys_threads
4361
 
10002 Jurgen 4362
.end:
9831 dunkaist 4363
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4364
        ret
4365
 
2438 mario79 4366
;------------------------------------------------------------------------------
2288 clevermous 4367
align 4
2438 mario79 4368
calculate_fast_getting_offset_for_WinMapAddress:
4369
; calculate data area for fast getting offset to _WinMapAddress
2446 mario79 4370
        xor     eax, eax
2438 mario79 4371
        mov     ecx, [_display.height]
4372
        mov     edi, d_width_calc_area
4373
        cld
4374
@@:
4375
        stosd
4376
        add     eax, [_display.width]
4377
        dec     ecx
4378
        jnz     @r
4379
        ret
4380
;------------------------------------------------------------------------------
4381
align 4
2480 mario79 4382
calculate_fast_getting_offset_for_LFB:
4383
; calculate data area for fast getting offset to LFB
4384
        xor     eax, eax
4385
        mov     ecx, [_display.height]
4386
        mov     edi, BPSLine_calc_area
4387
        cld
4388
@@:
4389
        stosd
5351 serge 4390
        add     eax, [_display.lfb_pitch]
2480 mario79 4391
        dec     ecx
4392
        jnz     @r
4393
        ret
4394
;------------------------------------------------------------------------------
4395
align 4
2288 clevermous 4396
set_screen:
2654 mario79 4397
; in:
4398
; eax - new Screen_Max_X
4399
; ecx - new BytesPerScanLine
4400
; edx - new Screen_Max_Y
2288 clevermous 4401
 
4402
        pushfd
4403
        cli
4404
 
5351 serge 4405
        mov     [_display.lfb_pitch], ecx
2288 clevermous 4406
 
4407
        mov     [screen_workarea.right], eax
4408
        mov     [screen_workarea.bottom], edx
4409
 
4410
        push    ebx
4411
        push    esi
4412
        push    edi
4413
 
4414
        pushad
4415
 
2654 mario79 4416
        cmp     [do_not_touch_winmap], 1
4417
        je      @f
4418
 
5351 serge 4419
        stdcall kernel_free, [_display.win_map]
2288 clevermous 4420
 
4421
        mov     eax, [_display.width]
4422
        mul     [_display.height]
5351 serge 4423
        mov     [_display.win_map_size], eax
2288 clevermous 4424
 
4425
        stdcall kernel_alloc, eax
5351 serge 4426
        mov     [_display.win_map], eax
2288 clevermous 4427
        test    eax, eax
4428
        jz      .epic_fail
2654 mario79 4429
; store for f.18.24
4430
        mov     eax, [_display.width]
4431
        mov     [display_width_standard], eax
2288 clevermous 4432
 
2654 mario79 4433
        mov     eax, [_display.height]
4434
        mov     [display_height_standard], eax
4435
@@:
2438 mario79 4436
        call    calculate_fast_getting_offset_for_WinMapAddress
2545 mario79 4437
; for Qemu or non standart video cards
2991 Serge 4438
; Unfortunately [BytesPerScanLine] does not always
2545 mario79 4439
;                             equal to [_display.width] * [ScreenBPP] / 8
4440
        call    calculate_fast_getting_offset_for_LFB
2288 clevermous 4441
        popad
4442
 
4443
        call    repos_windows
4444
        xor     eax, eax
4445
        xor     ebx, ebx
5350 serge 4446
        mov     ecx, [_display.width]
4447
        mov     edx, [_display.height]
4448
        dec     ecx
4449
        dec     edx
2288 clevermous 4450
        call    calculatescreen
4451
        pop     edi
4452
        pop     esi
4453
        pop     ebx
4454
 
4455
        popfd
4456
        ret
4457
 
4458
.epic_fail:
4459
        hlt                     ; Houston, we've had a problem
4460
 
4461
; --------------- APM ---------------------
4462
uglobal
4463
apm_entry       dp      0
4464
apm_vf          dd      0
4465
endg
4466
 
4467
align 4
4468
sys_apm:
4469
        xor     eax, eax
4470
        cmp     word [apm_vf], ax       ; Check APM BIOS enable
4471
        jne     @f
4472
        inc     eax
4473
        or      dword [esp + 44], eax   ; error
4474
        add     eax, 7
9968 dunkaist 4475
        mov     [esp + SYSCALL_STACK.eax], eax  ; 32-bit protected-mode
4476
                                                ; interface not supported
2288 clevermous 4477
        ret
4478
 
4479
@@:
4480
;       xchg    eax, ecx
4481
;       xchg    ebx, ecx
4482
 
4483
        cmp     dx, 3
4484
        ja      @f
4485
        and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
4486
        mov     eax, [apm_vf]
9911 Doczom 4487
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4488
        shr     eax, 16
9911 Doczom 4489
        mov     [esp + SYSCALL_STACK.ecx], eax
2288 clevermous 4490
        ret
4491
 
4492
@@:
4493
 
4494
        mov     esi, [master_tab+(OS_BASE shr 20)]
4495
        xchg    [master_tab], esi
4496
        push    esi
4497
        mov     edi, cr3
4498
        mov     cr3, edi                ;flush TLB
4499
 
4500
        call    pword [apm_entry]       ;call APM BIOS
4501
 
4502
        xchg    eax, [esp]
4503
        mov     [master_tab], eax
4504
        mov     eax, cr3
4505
        mov     cr3, eax
4506
        pop     eax
4507
 
9911 Doczom 4508
        mov     [esp + SYSCALL_STACK.edi], edi
4509
        mov     [esp + SYSCALL_STACK.esi], esi
4510
        mov     [esp + SYSCALL_STACK.ebx], ebx
4511
        mov     [esp + SYSCALL_STACK.edx], edx
4512
        mov     [esp + SYSCALL_STACK.ecx], ecx
4513
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4514
        setc    al
4515
        and     [esp + 44], byte 0xfe
4516
        or      [esp + 44], al
4517
        ret
4518
; -----------------------------------------
4519
 
4520
align 4
4521
undefined_syscall:                      ; Undefined system call
9968 dunkaist 4522
        mov     [esp + SYSCALL_STACK.eax], -1
2288 clevermous 4523
        ret
4524
 
8160 rgimad 4525
align 4
8962 Boppan 4526
; @brief Check if given memory region lays in lower 2gb (userspace memory) or not
4527
; @param base Base address of region
4528
; @param len Lenght of region
4529
; @return ZF = 1 if region in userspace memory,
4530
;         ZF = 0 otherwise
8329 rgimad 4531
proc is_region_userspace stdcall, base:dword, len:dword
9045 dunkaist 4532
        push    eax
8329 rgimad 4533
        mov     eax, [base]
8216 rgimad 4534
 
9045 dunkaist 4535
        cmp     eax, OS_BASE-1
4536
        ja      @f              ; zf
8216 rgimad 4537
 
8329 rgimad 4538
        add     eax, [len]
9045 dunkaist 4539
        jc      @f              ; zf
8160 rgimad 4540
        cmp     eax, OS_BASE
9045 dunkaist 4541
        ja      @f              ; zf
2288 clevermous 4542
 
9045 dunkaist 4543
        cmp     eax, eax        ; ZF
8160 rgimad 4544
@@:
9045 dunkaist 4545
        pop     eax
9947 ace_dent 4546
        ret
8329 rgimad 4547
endp
8160 rgimad 4548
 
9823 dunkaist 4549
align 4
4550
; @brief Check whether given string lays in userspace memory, i.e. below OS_BASE
4551
; @param base Base address of string
4552
; @return ZF = 1 if string in userspace memory,
4553
;         zf = 0 otherwise
4554
proc is_string_userspace stdcall, base:dword
4555
        push    eax ecx edi
4556
        xor     eax, eax
4557
        mov     edi, [base]
4558
 
4559
        mov     ecx, OS_BASE-1
4560
        sub     ecx, edi
4561
        jb      .done           ; zf
4562
        inc     ecx
4563
        cmp     ecx, 0x10000    ; don't allow strings larger than 64k?
4564
        jbe     @f
4565
        mov     ecx, 0x10000
4566
@@:
4567
        repnz scasb
4568
.done:
4569
        pop     edi ecx eax
4570
        ret
4571
endp
4572
 
3627 Serge 4573
if ~ lang eq sp
4574
diff16 "end of .text segment",0,$
4575
end if
4576
 
2288 clevermous 4577
include "data32.inc"
4578
 
4579
__REV__ = __REV
4580
 
3341 yogev_ezra 4581
if ~ lang eq sp
2288 clevermous 4582
diff16 "end of kernel code",0,$
3287 esevece 4583
end if