Subversion Repositories Kolibri OS

Rev

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

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