Subversion Repositories Kolibri OS

Rev

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