Subversion Repositories Kolibri OS

Rev

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

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