Subversion Repositories Kolibri OS

Rev

Rev 2446 | Rev 2452 | 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: 2450 $
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
;------------------------------------------------------------------------------
1952
restore_default_cursor_before_killing:
1953
        mov     eax, [def_cursor]
1954
        mov     [ecx+APPDATA.cursor], eax
2288 clevermous 1955
 
2435 mario79 1956
        movzx   eax, word [MOUSE_Y]
1957
        movzx   ebx, word [MOUSE_X]
2446 mario79 1958
;        mov     ecx, [Screen_Max_X]
1959
;        inc     ecx
1960
;        mul     ecx
1961
        mov     eax, [d_width_calc_area + eax*4]
1962
 
2435 mario79 1963
        add     eax, [_WinMapAddress]
1964
        movzx   edx, byte [ebx+eax]
1965
        shl     edx, 8
1966
        mov     esi, [edx+SLOT_BASE+APPDATA.cursor]
1967
        push    esi
1968
        call    [_display.select_cursor]
1969
        mov     [current_cursor], esi
2450 mario79 1970
        mov     [redrawmouse_unconditional], 1
1971
        call    [draw_pointer]
2435 mario79 1972
        ret
1973
;------------------------------------------------------------------------------
2288 clevermous 1974
iglobal
1975
align 4
1976
sys_system_table:
1977
        dd      sysfn_deactivate        ; 1 = deactivate window
1978
        dd      sysfn_terminate         ; 2 = terminate thread
1979
        dd      sysfn_activate          ; 3 = activate window
1980
        dd      sysfn_getidletime       ; 4 = get idle time
1981
        dd      sysfn_getcpuclock       ; 5 = get cpu clock
1982
        dd      sysfn_saveramdisk       ; 6 = save ramdisk
1983
        dd      sysfn_getactive         ; 7 = get active window
1984
        dd      sysfn_sound_flag        ; 8 = get/set sound_flag
1985
        dd      sysfn_shutdown          ; 9 = shutdown with parameter
1986
        dd      sysfn_minimize          ; 10 = minimize window
1987
        dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
1988
        dd      sysfn_lastkey           ; 12 = get last pressed key
1989
        dd      sysfn_getversion        ; 13 = get kernel version
1990
        dd      sysfn_waitretrace       ; 14 = wait retrace
1991
        dd      sysfn_centermouse       ; 15 = center mouse cursor
1992
        dd      sysfn_getfreemem        ; 16 = get free memory size
1993
        dd      sysfn_getallmem         ; 17 = get total memory size
1994
        dd      sysfn_terminate2        ; 18 = terminate thread using PID
1995
                                        ;                 instead of slot
1996
        dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
1997
        dd      sysfn_meminfo           ; 20 = get extended memory info
1998
        dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
1999
        dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
2000
sysfn_num = ($ - sys_system_table)/4
2001
endg
2002
;------------------------------------------------------------------------------
2003
sys_system:
2004
        dec     ebx
2005
        cmp     ebx, sysfn_num
2006
        jae     @f
2007
        jmp     dword [sys_system_table + ebx*4]
2008
@@:
2009
        ret
2010
;------------------------------------------------------------------------------
2011
sysfn_shutdown:          ; 18.9 = system shutdown
2012
        cmp     ecx, 1
2013
        jl      exit_for_anyone
2014
        cmp     ecx, 4
2015
        jg      exit_for_anyone
2016
        mov     [BOOT_VAR+0x9030], cl
2017
 
2018
        mov     eax, [TASK_COUNT]
2019
        mov     [SYS_SHUTDOWN], al
2020
        mov     [shutdown_processes], eax
2021
        and     dword [esp+32], 0
2022
 exit_for_anyone:
2023
        ret
2024
  uglobal
2025
   shutdown_processes:
2026
                       dd 0x0
2027
  endg
2028
;------------------------------------------------------------------------------
2029
sysfn_terminate:        ; 18.2 = TERMINATE
2030
        cmp     ecx, 2
2031
        jb      noprocessterminate
2032
        mov     edx, [TASK_COUNT]
2033
        cmp     ecx, edx
2034
        ja      noprocessterminate
2035
        mov     eax, [TASK_COUNT]
2430 mario79 2036
        push    ecx
2288 clevermous 2037
        shl     ecx, 5
2038
        mov     edx, [ecx+CURRENT_TASK+TASKDATA.pid]
2039
        add     ecx, CURRENT_TASK+TASKDATA.state
2040
        cmp     byte [ecx], 9
2041
        jz      noprocessterminate
2430 mario79 2042
;--------------------------------------
2043
        cmp     [_display.select_cursor], 0
2044
        je      @f
2045
; restore default cursor before killing
2046
        pusha
2047
        mov     ecx, [esp+32]
2048
        shl     ecx, 8
2435 mario79 2049
        add     ecx, SLOT_BASE
2050
        call    restore_default_cursor_before_killing
2430 mario79 2051
        popa
2052
@@:
2053
        add     esp, 4
2054
;--------------------------------------
2288 clevermous 2055
     ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
2056
        mov     [ecx], byte 3; clear possible i40's
2057
     ;call MEM_Heap_UnLock
2058
 
2059
        cmp     edx, [application_table_status]; clear app table stat
2060
        jne     noatsc
2061
        and     [application_table_status], 0
2062
   noatsc:
2063
   noprocessterminate:
2064
        ret
2065
;------------------------------------------------------------------------------
2066
sysfn_terminate2:
2067
;lock application_table_status mutex
2068
.table_status:
2069
        cli
2070
        cmp     [application_table_status], 0
2071
        je      .stf
2072
        sti
2073
        call    change_task
2074
        jmp     .table_status
2075
.stf:
2076
        call    set_application_table_status
2077
        mov     eax, ecx
2078
        call    pid_to_slot
2079
        test    eax, eax
2080
        jz      .not_found
2081
        mov     ecx, eax
2082
        cli
2083
        call    sysfn_terminate
2084
        and     [application_table_status], 0
2085
        sti
2086
        and     dword [esp+32], 0
2087
        ret
2088
.not_found:
2089
        mov     [application_table_status], 0
2090
        or      dword [esp+32], -1
2091
        ret
2092
;------------------------------------------------------------------------------
2093
sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
2094
        cmp     ecx, 2
2095
        jb      .nowindowdeactivate
2096
        cmp     ecx, [TASK_COUNT]
2097
        ja      .nowindowdeactivate
2408 Serge 2098
 
2288 clevermous 2099
        movzx   esi, word [WIN_STACK + ecx*2]
2100
        cmp     esi, 1
2101
        je      .nowindowdeactivate ; already deactive
2102
 
2103
        mov     edi, ecx
2104
        shl     edi, 5
2105
        add     edi, window_data
2106
        movzx   esi, word [WIN_STACK + ecx * 2]
2107
        lea     esi, [WIN_POS + esi * 2]
2108
        call    window._.window_deactivate
2408 Serge 2109
 
2288 clevermous 2110
        xor     eax, eax
2111
        mov     byte[MOUSE_BACKGROUND], al
2112
        mov     byte[DONT_DRAW_MOUSE], al
2113
        mov     byte[MOUSE_DOWN], 0
2114
 
2115
        call    syscall_display_settings._.calculate_whole_screen
2116
        call    syscall_display_settings._.redraw_whole_screen
2117
.nowindowdeactivate:
2118
        ret
2411 Serge 2119
 ;------------------------------------------------------------------------------
2288 clevermous 2120
sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
2121
        cmp     ecx, 2
2122
        jb      .nowindowactivate
2123
        cmp     ecx, [TASK_COUNT]
2124
        ja      .nowindowactivate
2125
 
2126
        mov     [window_minimize], 2; restore window if minimized
2127
 
2128
        movzx   esi, word [WIN_STACK + ecx*2]
2129
        cmp     esi, [TASK_COUNT]
2130
        je      .nowindowactivate; already active
2131
 
2132
        mov     edi, ecx
2133
        shl     edi, 5
2134
        add     edi, window_data
2135
        movzx   esi, word [WIN_STACK + ecx * 2]
2136
        lea     esi, [WIN_POS + esi * 2]
2137
        call    waredraw
2138
.nowindowactivate:
2139
        ret
2140
;------------------------------------------------------------------------------
2141
sysfn_getidletime:              ; 18.4 = GET IDLETIME
2142
        mov     eax, [idleusesec]
2143
        mov     [esp+32], eax
2144
        ret
2145
;------------------------------------------------------------------------------
2146
sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
2147
        mov     eax, [CPU_FREQ]
2148
        mov     [esp+32], eax
2149
        ret
2150
;------------------------------------------------------------------------------
2151
;  SAVE ramdisk to /hd/1/menuet.img
2152
;!!!!!!!!!!!!!!!!!!!!!!!!
2153
   include 'blkdev/rdsave.inc'
2154
;!!!!!!!!!!!!!!!!!!!!!!!!
2155
;------------------------------------------------------------------------------
2156
align 4
2157
sysfn_getactive:        ; 18.7 = get active window
2158
        mov     eax, [TASK_COUNT]
2159
        movzx   eax, word [WIN_POS + eax*2]
2160
        mov     [esp+32], eax
2161
        ret
2162
;------------------------------------------------------------------------------
2163
sysfn_sound_flag:       ; 18.8 = get/set sound_flag
2164
;     cmp  ecx,1
2165
        dec     ecx
2166
        jnz     nogetsoundflag
2167
        movzx   eax, byte [sound_flag]; get sound_flag
2168
        mov     [esp+32], eax
2169
        ret
2170
 nogetsoundflag:
2171
;     cmp  ecx,2
2172
        dec     ecx
2173
        jnz     nosoundflag
2174
        xor     byte [sound_flag], 1
2175
 nosoundflag:
2176
        ret
2177
;------------------------------------------------------------------------------
2178
sysfn_minimize:         ; 18.10 = minimize window
2179
        mov     [window_minimize], 1
2180
        ret
2181
;------------------------------------------------------------------------------
2182
align 4
2183
sysfn_getdiskinfo:      ; 18.11 = get disk info table
2184
;     cmp  ecx,1
2185
        dec     ecx
2186
        jnz     full_table
2187
  small_table:
2188
        call    for_all_tables
2189
        mov     ecx, 10
2190
        cld
2191
        rep movsb
2192
        ret
2193
   for_all_tables:
2194
        mov     edi, edx
2195
        mov     esi, DRIVE_DATA
2196
        ret
2197
  full_table:
2198
;     cmp  ecx,2
2199
        dec     ecx
2200
        jnz     exit_for_anyone
2201
        call    for_all_tables
2202
        mov     ecx, 16384
2203
        cld
2204
        rep movsd
2205
        ret
2206
;------------------------------------------------------------------------------
2207
sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
2208
        and     dword [esp+32], 0
2209
        ret
2210
;------------------------------------------------------------------------------
2211
sysfn_getversion:       ; 18.13 = get kernel ID and version
2212
        mov     edi, ecx
2213
        mov     esi, version_inf
2214
        mov     ecx, version_end-version_inf
2215
        rep movsb
2216
        ret
2217
;------------------------------------------------------------------------------
2218
sysfn_waitretrace:     ; 18.14 = sys wait retrace
2219
     ;wait retrace functions
2220
 sys_wait_retrace:
2221
        mov     edx, 0x3da
2222
 WaitRetrace_loop:
2223
        in      al, dx
2224
        test    al, 1000b
2225
        jz      WaitRetrace_loop
2226
        and     [esp+32], dword 0
2227
        ret
2228
;------------------------------------------------------------------------------
2229
align 4
2230
sysfn_centermouse:      ; 18.15 = mouse centered
2231
; removed here by 
2232
;     call  mouse_centered
2233
;* mouse centered - start code- Mario79
2234
;mouse_centered:
2235
;        push  eax
2236
        mov     eax, [Screen_Max_X]
2237
        shr     eax, 1
2238
        mov     [MOUSE_X], ax
2239
        mov     eax, [Screen_Max_Y]
2240
        shr     eax, 1
2241
        mov     [MOUSE_Y], ax
2242
;        ret
2243
;* mouse centered - end code- Mario79
2244
        xor     eax, eax
2245
        and     [esp+32], eax
2246
;        pop   eax
2247
        ret
2248
;------------------------------------------------------------------------------
2249
align 4
2250
sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
2251
        test    ecx, ecx; get mouse speed factor
2252
        jnz     .set_mouse_acceleration
2253
        xor     eax, eax
2254
        mov     ax, [mouse_speed_factor]
2255
        mov     [esp+32], eax
2256
        ret
2257
 .set_mouse_acceleration:
2258
;     cmp  ecx,1  ; set mouse speed factor
2259
        dec     ecx
2260
        jnz     .get_mouse_delay
2261
        mov     [mouse_speed_factor], dx
2262
        ret
2263
 .get_mouse_delay:
2264
;     cmp  ecx,2  ; get mouse delay
2265
        dec     ecx
2266
        jnz     .set_mouse_delay
2267
        mov     eax, [mouse_delay]
2268
        mov     [esp+32], eax
2269
        ret
2270
 .set_mouse_delay:
2271
;     cmp  ecx,3  ; set mouse delay
2272
        dec     ecx
2273
        jnz     .set_pointer_position
2274
        mov     [mouse_delay], edx
2275
        ret
2276
 .set_pointer_position:
2277
;     cmp  ecx,4  ; set mouse pointer position
2278
        dec     ecx
2279
        jnz     .set_mouse_button
2280
        cmp     dx, word[Screen_Max_Y]
2281
        ja      .end
2282
        rol     edx, 16
2283
        cmp     dx, word[Screen_Max_X]
2284
        ja      .end
2285
        mov     [MOUSE_X], edx
2286
        ret
2287
 .set_mouse_button:
2288
;     cmp   ecx,5  ; set mouse button features
2289
        dec     ecx
2290
        jnz     .end
2291
        mov     [BTN_DOWN], dl
2292
        mov     [mouse_active], 1
2293
 .end:
2294
        ret
2295
;------------------------------------------------------------------------------
2296
sysfn_getfreemem:
2297
        mov     eax, [pg_data.pages_free]
2298
        shl     eax, 2
2299
        mov     [esp+32], eax
2300
        ret
2301
 
2302
sysfn_getallmem:
2303
        mov     eax, [MEM_AMOUNT]
2304
        shr     eax, 10
2305
        mov     [esp+32], eax
2306
        ret
2307
 
2308
; // Alver, 2007-22-08 // {
2309
sysfn_pid_to_slot:
2310
        mov     eax, ecx
2311
        call    pid_to_slot
2312
        mov     [esp+32], eax
2313
        ret
2314
 
2315
sysfn_min_rest_window:
2316
        pushad
2317
        mov     eax, edx ; ebx - operating
2318
        shr     ecx, 1
2319
        jnc     @f
2320
        call    pid_to_slot
2321
@@:
2322
        or      eax, eax ; eax - number of slot
2323
        jz      .error
2324
        cmp     eax, 255    ; varify maximal slot number
2325
        ja      .error
2326
        movzx   eax, word [WIN_STACK + eax*2]
2327
        shr     ecx, 1
2328
        jc      .restore
2329
 ; .minimize:
2330
        call    minimize_window
2331
        jmp     .exit
2332
.restore:
2333
        call    restore_minimized_window
2334
.exit:
2335
        popad
2336
        xor     eax, eax
2337
        mov     [esp+32], eax
2338
        ret
2339
.error:
2340
        popad
2341
        xor     eax, eax
2342
        dec     eax
2343
        mov     [esp+32], eax
2344
        ret
2345
; } \\ Alver, 2007-22-08 \\
2346
 
2347
uglobal
2348
;// mike.dld, 2006-29-01 [
2349
screen_workarea RECT
2350
;// mike.dld, 2006-29-01 ]
2351
window_minimize db 0
2352
sound_flag      db 0
2353
endg
2354
 
2355
UID_NONE=0
2356
UID_MENUETOS=1   ;official
2357
UID_KOLIBRI=2    ;russian
2358
 
2359
iglobal
2360
version_inf:
2361
  db 0,7,7,0  ; version 0.7.7.0
2362
  db 0
2363
  dd __REV__
2364
version_end:
2365
endg
2366
 
2367
sys_cachetodiskette:
2368
        cmp     ebx, 1
2369
        jne     .no_floppy_a_save
2370
        mov     [flp_number], 1
2371
        jmp     .save_image_on_floppy
2372
.no_floppy_a_save:
2373
        cmp     ebx, 2
2374
        jne     .no_floppy_b_save
2375
        mov     [flp_number], 2
2376
.save_image_on_floppy:
2377
        call    save_image
2378
        mov     [esp + 32], dword 0
2379
        cmp     [FDC_Status], 0
2380
        je      .yes_floppy_save
2381
.no_floppy_b_save:
2382
        mov     [esp + 32], dword 1
2383
.yes_floppy_save:
2384
        ret
2385
 
2386
uglobal
2387
;  bgrchanged  dd  0x0
2388
align 4
2389
bgrlockpid dd 0
2390
bgrlock db 0
2391
endg
2392
 
2393
sys_background:
2394
 
2395
        cmp     ebx, 1                     ; BACKGROUND SIZE
2396
        jnz     nosb1
2397
        test    ecx, ecx
2398
;    cmp   ecx,0
2399
        jz      sbgrr
2400
        test    edx, edx
2401
;    cmp   edx,0
2402
        jz      sbgrr
2403
@@:
2404
;;Maxis use atomic bts for mutexes  4.4.2009
2405
        bts     dword [bgrlock], 0
2406
        jnc     @f
2407
        call    change_task
2408
        jmp     @b
2409
@@:
2410
        mov     [BgrDataWidth], ecx
2411
        mov     [BgrDataHeight], edx
2412
;    mov   [bgrchanged],1
2413
 
2414
        pushad
2415
; return memory for old background
2416
        mov     eax, [img_background]
2417
        cmp     eax, static_background_data
2418
        jz      @f
2419
        stdcall kernel_free, eax
2420
@@:
2421
; calculate RAW size
2422
        xor     eax, eax
2423
        inc     eax
2424
        cmp     [BgrDataWidth], eax
2425
        jae     @f
2426
        mov     [BgrDataWidth], eax
2427
@@:
2428
        cmp     [BgrDataHeight], eax
2429
        jae     @f
2430
        mov     [BgrDataHeight], eax
2431
@@:
2432
        mov     eax, [BgrDataWidth]
2433
        imul    eax, [BgrDataHeight]
2434
        lea     eax, [eax*3]
2435
        mov     [mem_BACKGROUND], eax
2436
; get memory for new background
2437
        stdcall kernel_alloc, eax
2438
        test    eax, eax
2439
        jz      .memfailed
2440
        mov     [img_background], eax
2441
        jmp     .exit
2442
.memfailed:
2443
; revert to static monotone data
2444
        mov     [img_background], static_background_data
2445
        xor     eax, eax
2446
        inc     eax
2447
        mov     [BgrDataWidth], eax
2448
        mov     [BgrDataHeight], eax
2449
        mov     [mem_BACKGROUND], 4
2450
.exit:
2451
        popad
2452
        mov     [bgrlock], 0
2453
 
2454
  sbgrr:
2455
        ret
2456
 
2457
nosb1:
2458
 
2459
        cmp     ebx, 2                     ; SET PIXEL
2460
        jnz     nosb2
2461
 
2462
        mov     eax, [img_background]
2463
        test    ecx, ecx
2464
        jz      @f
2465
        cmp     eax, static_background_data
2466
        jz      .ret
2467
@@:
2468
        mov     ebx, [mem_BACKGROUND]
2469
        add     ebx, 4095
2470
        and     ebx, -4096
2471
        sub     ebx, 4
2472
        cmp     ecx, ebx
2473
        ja      .ret
2474
 
2475
        mov     ebx, [eax+ecx]
2476
        and     ebx, 0xFF000000;255*256*256*256
2477
        and     edx, 0x00FFFFFF;255*256*256+255*256+255
2478
        add     edx, ebx
2479
        mov     [eax+ecx], edx
2480
.ret:
2481
        ret
2482
nosb2:
2483
 
2484
        cmp     ebx, 3                     ; DRAW BACKGROUND
2485
        jnz     nosb3
2486
draw_background_temp:
2487
;    cmp   [bgrchanged],1 ;0
2488
;    je    nosb31
2489
;draw_background_temp:
2490
;    mov   [bgrchanged],1 ;0
2491
        mov     [background_defined], 1
2492
        mov     byte[BACKGROUND_CHANGED], 1
2493
        call    force_redraw_background
2494
   nosb31:
2495
        ret
2496
  nosb3:
2497
 
2498
        cmp     ebx, 4                     ; TILED / STRETCHED
2499
        jnz     nosb4
2500
        cmp     ecx, [BgrDrawMode]
2501
        je      nosb41
2502
        mov     [BgrDrawMode], ecx
2503
;    mov   [bgrchanged],1
2504
   nosb41:
2505
        ret
2506
  nosb4:
2507
 
2508
        cmp     ebx, 5                     ; BLOCK MOVE TO BGR
2509
        jnz     nosb5
2510
        cmp     [img_background], static_background_data
2511
        jnz     @f
2512
        test    edx, edx
2513
        jnz     .fin
2514
        cmp     esi, 4
2515
        ja      .fin
2516
  @@:
2517
  ; bughere
2518
        mov     eax, ecx
2519
        mov     ebx, edx
2520
        add     ebx, [img_background];IMG_BACKGROUND
2521
        mov     ecx, esi
2522
        call    memmove
2523
  .fin:
2524
        ret
2525
  nosb5:
2526
 
2527
        cmp     ebx, 6
2528
        jnz     nosb6
2529
;;Maxis use atomic bts for mutex 4.4.2009
2530
@@:
2531
        bts     dword [bgrlock], 0
2532
        jnc     @f
2533
        call    change_task
2534
        jmp     @b
2535
@@:
2536
        mov     eax, [CURRENT_TASK]
2537
        mov     [bgrlockpid], eax
2538
        cmp     [img_background], static_background_data
2539
        jz      .nomem
2540
        stdcall user_alloc, [mem_BACKGROUND]
2541
        mov     [esp+32], eax
2542
        test    eax, eax
2543
        jz      .nomem
2544
        mov     ebx, eax
2545
        shr     ebx, 12
2546
        or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
2547
        mov     esi, [img_background]
2548
        shr     esi, 12
2549
        mov     ecx, [mem_BACKGROUND]
2550
        add     ecx, 0xFFF
2551
        shr     ecx, 12
2552
.z:
2553
        mov     eax, [page_tabs+ebx*4]
2554
        test    al, 1
2555
        jz      @f
2556
        call    free_page
2557
@@:
2558
        mov     eax, [page_tabs+esi*4]
2559
        or      al, PG_UW
2560
        mov     [page_tabs+ebx*4], eax
2561
        mov     eax, ebx
2562
        shl     eax, 12
2563
        invlpg  [eax]
2564
        inc     ebx
2565
        inc     esi
2566
        loop    .z
2567
        ret
2568
.nomem:
2569
        and     [bgrlockpid], 0
2570
        mov     [bgrlock], 0
2571
nosb6:
2572
        cmp     ebx, 7
2573
        jnz     nosb7
2574
        cmp     [bgrlock], 0
2575
        jz      .err
2576
        mov     eax, [CURRENT_TASK]
2577
        cmp     [bgrlockpid], eax
2578
        jnz     .err
2579
        mov     eax, ecx
2580
        mov     ebx, ecx
2581
        shr     eax, 12
2582
        mov     ecx, [page_tabs+(eax-1)*4]
2583
        test    cl, USED_BLOCK+DONT_FREE_BLOCK
2584
        jz      .err
2585
        jnp     .err
2586
        push    eax
2587
        shr     ecx, 12
2588
        dec     ecx
2589
@@:
2590
        and     dword [page_tabs+eax*4], 0
2591
        mov     edx, eax
2592
        shl     edx, 12
2593
        push    eax
2594
        invlpg  [edx]
2595
        pop     eax
2596
        inc     eax
2597
        loop    @b
2598
        pop     eax
2599
        and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
2600
        stdcall user_free, ebx
2601
        mov     [esp+32], eax
2602
        and     [bgrlockpid], 0
2603
        mov     [bgrlock], 0
2604
        ret
2605
.err:
2606
        and     dword [esp+32], 0
2607
        ret
2608
 
2609
nosb7:
2610
        ret
2611
 
2612
force_redraw_background:
2613
        and     [draw_data+32 + RECT.left], 0
2614
        and     [draw_data+32 + RECT.top], 0
2615
        push    eax ebx
2616
        mov     eax, [Screen_Max_X]
2617
        mov     ebx, [Screen_Max_Y]
2618
        mov     [draw_data+32 + RECT.right], eax
2619
        mov     [draw_data+32 + RECT.bottom], ebx
2620
        pop     ebx eax
2621
        inc     byte[REDRAW_BACKGROUND]
2622
        ret
2623
 
2624
align 4
2625
 
2626
sys_getbackground:
2627
;    cmp   eax,1                                  ; SIZE
2628
        dec     ebx
2629
        jnz     nogb1
2630
        mov     eax, [BgrDataWidth]
2631
        shl     eax, 16
2632
        mov     ax, [BgrDataHeight]
2633
        mov     [esp+32], eax
2634
        ret
2635
 
2636
nogb1:
2637
;    cmp   eax,2                                  ; PIXEL
2638
        dec     ebx
2639
        jnz     nogb2
2640
 
2641
        mov     eax, [img_background]
2642
        test    ecx, ecx
2643
        jz      @f
2644
        cmp     eax, static_background_data
2645
        jz      .ret
2646
@@:
2647
        mov     ebx, [mem_BACKGROUND]
2648
        add     ebx, 4095
2649
        and     ebx, -4096
2650
        sub     ebx, 4
2651
        cmp     ecx, ebx
2652
        ja      .ret
2653
 
2654
        mov     eax, [ecx+eax]
2655
 
2656
        and     eax, 0xFFFFFF
2657
        mov     [esp+32], eax
2658
.ret:
2659
        ret
2660
  nogb2:
2661
 
2662
;    cmp   eax,4                                  ; TILED / STRETCHED
2663
        dec     ebx
2664
        dec     ebx
2665
        jnz     nogb4
2666
        mov     eax, [BgrDrawMode]
2667
  nogb4:
2668
        mov     [esp+32], eax
2669
        ret
2670
 
2671
align 4
2672
 
2673
sys_getkey:
2674
        mov     [esp + 32], dword 1
2675
        ; test main buffer
2676
        mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
2677
        movzx   ecx, word [WIN_STACK + ebx * 2]
2678
        mov     edx, [TASK_COUNT]
2679
        cmp     ecx, edx
2680
        jne     .finish
2681
        cmp     [KEY_COUNT], byte 0
2682
        je      .finish
2683
        movzx   eax, byte [KEY_BUFF]
2684
        shl     eax, 8
2685
        push    eax
2686
        dec     byte [KEY_COUNT]
2687
        and     byte [KEY_COUNT], 127
2688
        movzx   ecx, byte [KEY_COUNT]
2689
        add     ecx, 2
2690
        mov     eax, KEY_BUFF + 1
2691
        mov     ebx, KEY_BUFF
2692
        call    memmove
2693
        pop     eax
2694
.ret_eax:
2695
        mov     [esp + 32], eax
2696
        ret
2697
.finish:
2698
; test hotkeys buffer
2699
        mov     ecx, hotkey_buffer
2700
@@:
2701
        cmp     [ecx], ebx
2702
        jz      .found
2703
        add     ecx, 8
2704
        cmp     ecx, hotkey_buffer + 120 * 8
2705
        jb      @b
2706
        ret
2707
.found:
2708
        mov     ax, [ecx + 6]
2709
        shl     eax, 16
2710
        mov     ah, [ecx + 4]
2711
        mov     al, 2
2712
        and     dword [ecx + 4], 0
2713
        and     dword [ecx], 0
2714
        jmp     .ret_eax
2715
 
2716
align 4
2717
 
2718
sys_getbutton:
2719
 
2720
        mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
2721
        mov     [esp + 32], dword 1
2722
        movzx   ecx, word [WIN_STACK + ebx * 2]
2723
        mov     edx, [TASK_COUNT] ; less than 256 processes
2724
        cmp     ecx, edx
2725
        jne     .exit
2726
        movzx   eax, byte [BTN_COUNT]
2727
        test    eax, eax
2728
        jz      .exit
2729
        mov     eax, [BTN_BUFF]
2730
        and     al, 0xFE                                    ; delete left button bit
2731
        mov     [BTN_COUNT], byte 0
2732
        mov     [esp + 32], eax
2733
.exit:
2734
        ret
2735
 
2736
 
2737
align 4
2738
 
2739
sys_cpuusage:
2740
 
2741
;  RETURN:
2742
;
2743
;  +00 dword     process cpu usage
2744
;  +04  word     position in windowing stack
2745
;  +06  word     windowing stack value at current position (cpu nro)
2746
;  +10 12 bytes  name
2747
;  +22 dword     start in mem
2748
;  +26 dword     used mem
2749
;  +30 dword     PID , process idenfification number
2750
;
2751
 
2752
        cmp     ecx, -1 ; who am I ?
2753
        jne     .no_who_am_i
2754
        mov     ecx, [CURRENT_TASK]
2755
  .no_who_am_i:
2756
        cmp     ecx, max_processes
2757
        ja      .nofillbuf
2758
 
2759
; +4: word: position of the window of thread in the window stack
2760
        mov     ax, [WIN_STACK + ecx * 2]
2761
        mov     [ebx+4], ax
2762
; +6: word: number of the thread slot, which window has in the window stack
2763
;           position ecx (has no relation to the specific thread)
2764
        mov     ax, [WIN_POS + ecx * 2]
2765
        mov     [ebx+6], ax
2766
 
2767
        shl     ecx, 5
2768
 
2769
; +0: dword: memory usage
2770
        mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
2771
        mov     [ebx], eax
2772
; +10: 11 bytes: name of the process
2773
        push    ecx
2774
        lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
2775
        add     ebx, 10
2776
        mov     ecx, 11
2777
        call    memmove
2778
        pop     ecx
2779
 
2780
; +22: address of the process in memory
2781
; +26: size of used memory - 1
2782
        push    edi
2783
        lea     edi, [ebx+12]
2784
        xor     eax, eax
2785
        mov     edx, 0x100000*16
2786
        cmp     ecx, 1 shl 5
2787
        je      .os_mem
2788
        mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
2789
        mov     eax, std_application_base_address
2790
.os_mem:
2791
        stosd
2792
        lea     eax, [edx-1]
2793
        stosd
2794
 
2795
; +30: PID/TID
2796
        mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
2797
        stosd
2798
 
2799
    ; window position and size
2800
        push    esi
2801
        lea     esi, [ecx + window_data + WDATA.box]
2802
        movsd
2803
        movsd
2804
        movsd
2805
        movsd
2806
 
2807
    ; Process state (+50)
2808
        mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
2809
        stosd
2810
 
2811
    ; Window client area box
2812
        lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
2813
        movsd
2814
        movsd
2815
        movsd
2816
        movsd
2817
 
2818
    ; Window state
2819
        mov     al, [ecx+window_data+WDATA.fl_wstate]
2820
        stosb
2821
 
2822
    ; Event mask (+71)
2823
        mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
2824
        stosd
2825
 
2826
        pop     esi
2827
        pop     edi
2828
 
2829
.nofillbuf:
2830
    ; return number of processes
2831
 
2832
        mov     eax, [TASK_COUNT]
2833
        mov     [esp+32], eax
2834
        ret
2835
 
2836
align 4
2837
sys_clock:
2838
        cli
2839
  ; Mikhail Lisovin  xx Jan 2005
2840
  @@:
2841
        mov     al, 10
2842
        out     0x70, al
2843
        in      al, 0x71
2844
        test    al, al
2845
        jns     @f
2846
        mov     esi, 1
2847
        call    delay_ms
2848
        jmp     @b
2849
  @@:
2850
  ; end Lisovin's fix
2851
 
2852
        xor     al, al        ; seconds
2853
        out     0x70, al
2854
        in      al, 0x71
2855
        movzx   ecx, al
2856
        mov     al, 02        ; minutes
2857
        shl     ecx, 16
2858
        out     0x70, al
2859
        in      al, 0x71
2860
        movzx   edx, al
2861
        mov     al, 04        ; hours
2862
        shl     edx, 8
2863
        out     0x70, al
2864
        in      al, 0x71
2865
        add     ecx, edx
2866
        movzx   edx, al
2867
        add     ecx, edx
2868
        sti
2869
        mov     [esp + 32], ecx
2870
        ret
2871
 
2872
 
2873
align 4
2874
 
2875
sys_date:
2876
 
2877
        cli
2878
  @@:
2879
        mov     al, 10
2880
        out     0x70, al
2881
        in      al, 0x71
2882
        test    al, al
2883
        jns     @f
2884
        mov     esi, 1
2885
        call    delay_ms
2886
        jmp     @b
2887
  @@:
2888
 
2889
        mov     ch, 0
2890
        mov     al, 7           ; date
2891
        out     0x70, al
2892
        in      al, 0x71
2893
        mov     cl, al
2894
        mov     al, 8           ; month
2895
        shl     ecx, 16
2896
        out     0x70, al
2897
        in      al, 0x71
2898
        mov     ch, al
2899
        mov     al, 9           ; year
2900
        out     0x70, al
2901
        in      al, 0x71
2902
        mov     cl, al
2903
        sti
2904
        mov     [esp+32], ecx
2905
        ret
2906
 
2907
 
2908
; redraw status
2909
 
2910
sys_redrawstat:
2911
        cmp     ebx, 1
2912
        jne     no_widgets_away
2913
        ; buttons away
2914
        mov     ecx, [CURRENT_TASK]
2915
  sys_newba2:
2916
        mov     edi, [BTN_ADDR]
2917
        cmp     [edi], dword 0  ; empty button list ?
2918
        je      end_of_buttons_away
2919
        movzx   ebx, word [edi]
2920
        inc     ebx
2921
        mov     eax, edi
2922
  sys_newba:
2923
        dec     ebx
2924
        jz      end_of_buttons_away
2925
 
2926
        add     eax, 0x10
2927
        cmp     cx, [eax]
2928
        jnz     sys_newba
2929
 
2930
        push    eax ebx ecx
2931
        mov     ecx, ebx
2932
        inc     ecx
2933
        shl     ecx, 4
2934
        mov     ebx, eax
2935
        add     eax, 0x10
2936
        call    memmove
2937
        dec     dword [edi]
2938
        pop     ecx ebx eax
2939
 
2940
        jmp     sys_newba2
2941
 
2942
  end_of_buttons_away:
2943
 
2944
        ret
2945
 
2946
  no_widgets_away:
2947
 
2948
        cmp     ebx, 2
2949
        jnz     srl1
2950
 
2951
        mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
2952
        add     edx, draw_data - CURRENT_TASK
2953
        mov     [edx + RECT.left], 0
2954
        mov     [edx + RECT.top], 0
2955
        mov     eax, [Screen_Max_X]
2956
        mov     [edx + RECT.right], eax
2957
        mov     eax, [Screen_Max_Y]
2958
        mov     [edx + RECT.bottom], eax
2959
 
2960
  srl1:
2961
        ret
2962
 
2963
;ok - 100% work
2964
;nt - not tested
2965
;---------------------------------------------------------------------------------------------
2966
;eax
2967
;0 - task switch counter. Ret switch counter in eax. Block. ok.
2968
;1 - change task. Ret nothing. Block. ok.
2969
;2 - performance control
2970
; ebx
2971
; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
2972
; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
2973
; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
2974
; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
2975
; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
2976
;eax
2977
;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
2978
;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
2979
;---------------------------------------------------------------------------------------------
2980
iglobal
2981
align 4
2982
sheduler:
2983
        dd      sys_sheduler.00
2984
        dd      change_task
2985
        dd      sys_sheduler.02
2986
        dd      sys_sheduler.03
2987
        dd      sys_sheduler.04
2988
endg
2989
sys_sheduler:
2990
;rewritten by   29.12.2009
2991
        jmp     dword [sheduler+ebx*4]
2992
;.shed_counter:
2993
.00:
2994
        mov     eax, [context_counter]
2995
        mov     [esp+32], eax
2996
        ret
2997
 
2998
.02:
2999
;.perf_control:
3000
        inc     ebx                     ;before ebx=2, ebx=3
3001
        cmp     ebx, ecx                ;if ecx=3, ebx=3
3002
        jz      cache_disable
3003
 
3004
        dec     ebx                     ;ebx=2
3005
        cmp     ebx, ecx                ;
3006
        jz      cache_enable            ;if ecx=2 and ebx=2
3007
 
3008
        dec     ebx                     ;ebx=1
3009
        cmp     ebx, ecx
3010
        jz      is_cache_enabled        ;if ecx=1 and ebx=1
3011
 
3012
        dec     ebx
3013
        test    ebx, ecx                ;ebx=0 and ecx=0
3014
        jz      modify_pce              ;if ecx=0
3015
 
3016
        ret
3017
 
3018
.03:
3019
;.rdmsr_instr:
3020
;now counter in ecx
3021
;(edx:eax) esi:edi => edx:esi
3022
        mov     eax, esi
3023
        mov     ecx, edx
3024
        rdmsr
3025
        mov     [esp+32], eax
3026
        mov     [esp+20], edx           ;ret in ebx?
3027
        ret
3028
 
3029
.04:
3030
;.wrmsr_instr:
3031
;now counter in ecx
3032
;(edx:eax) esi:edi => edx:esi
3033
        ; Fast Call MSR can't be destroy
3034
        ; ® MSR_AMD_EFER ¬®¦­® ¨§¬¥­ïâì, â.ª. ¢ í⮬ ॣ¨áâॠ«¨è
3035
        ; ¢ª«îç îâáï/¢ëª«îç îâáï à áè¨à¥­­ë¥ ¢®§¬®¦­®áâ¨
3036
        cmp     edx, MSR_SYSENTER_CS
3037
        je      @f
3038
        cmp     edx, MSR_SYSENTER_ESP
3039
        je      @f
3040
        cmp     edx, MSR_SYSENTER_EIP
3041
        je      @f
3042
        cmp     edx, MSR_AMD_STAR
3043
        je      @f
3044
 
3045
        mov     eax, esi
3046
        mov     ecx, edx
3047
        wrmsr
3048
        ; mov   [esp + 32], eax
3049
        ; mov   [esp + 20], edx ;ret in ebx?
3050
@@:
3051
        ret
3052
 
3053
cache_disable:
3054
        mov     eax, cr0
3055
        or      eax, 01100000000000000000000000000000b
3056
        mov     cr0, eax
3057
        wbinvd  ;set MESI
3058
        ret
3059
 
3060
cache_enable:
3061
        mov     eax, cr0
3062
        and     eax, 10011111111111111111111111111111b
3063
        mov     cr0, eax
3064
        ret
3065
 
3066
is_cache_enabled:
3067
        mov     eax, cr0
3068
        mov     ebx, eax
3069
        and     eax, 01100000000000000000000000000000b
3070
        jz      cache_disabled
3071
        mov     [esp+32], ebx
3072
cache_disabled:
3073
        mov     dword [esp+32], eax;0
3074
        ret
3075
 
3076
modify_pce:
3077
        mov     eax, cr4
3078
;       mov ebx,0
3079
;       or  bx,100000000b ;pce
3080
;       xor eax,ebx ;invert pce
3081
        bts     eax, 8;pce=cr4[8]
3082
        mov     cr4, eax
3083
        mov     [esp+32], eax
3084
        ret
3085
;---------------------------------------------------------------------------------------------
3086
 
3087
 
3088
; check if pixel is allowed to be drawn
3089
 
2446 mario79 3090
;checkpixel:
3091
;        push    eax edx
2288 clevermous 3092
 
2446 mario79 3093
;;        mov     edx, [Screen_Max_X] ; screen x size
3094
;;        inc     edx
3095
;;        imul    edx, ebx
3096
;        mov     edx, [d_width_calc_area + ebx*4]
3097
;        add     eax, [_WinMapAddress]
3098
;        mov     dl, [eax+edx]; lea eax, [...]
2288 clevermous 3099
 
2446 mario79 3100
;        xor     ecx, ecx
3101
;        mov     eax, [CURRENT_TASK]
3102
;        cmp     al, dl
3103
;        setne   cl
2288 clevermous 3104
 
2446 mario79 3105
;        pop     edx eax
3106
;        ret
2288 clevermous 3107
 
3108
iglobal
3109
  cpustring db 'CPU',0
3110
endg
3111
 
3112
uglobal
3113
background_defined    db    0    ; diamond, 11.04.2006
3114
endg
3115
 
3116
align 4
3117
; check misc
3118
 
3119
checkmisc:
3120
 
3121
        cmp     [ctrl_alt_del], 1
3122
        jne     nocpustart
3123
 
3124
        mov     ebp, cpustring
3125
        call    fs_execute_from_sysdir
3126
 
3127
        mov     [ctrl_alt_del], 0
3128
 
3129
nocpustart:
3130
        cmp     [mouse_active], 1
3131
        jne     mouse_not_active
3132
        mov     [mouse_active], 0
2411 Serge 3133
 
2288 clevermous 3134
        xor     edi, edi
2411 Serge 3135
        mov     ebx, CURRENT_TASK
3136
 
2288 clevermous 3137
        mov     ecx, [TASK_COUNT]
2408 Serge 3138
        movzx   eax, word [WIN_POS + ecx*2]     ; active window
3139
        shl     eax, 8
2414 Serge 3140
        push    eax
2408 Serge 3141
 
2414 Serge 3142
        movzx   eax, word [MOUSE_X]
3143
        movzx   edx, word [MOUSE_Y]
3144
 
2408 Serge 3145
align 4
3146
.set_mouse_event:
2288 clevermous 3147
        add     edi, 256
2411 Serge 3148
        add     ebx, 32
3149
        test    [ebx+TASKDATA.event_mask], 0x80000000
2414 Serge 3150
        jz      .pos_filter
3151
 
3152
        cmp     edi, [esp]                      ; skip if filtration active
3153
        jne     .skip
3154
 
3155
.pos_filter:
3156
        test    [ebx+TASKDATA.event_mask], 0x40000000
2411 Serge 3157
        jz      .set
2288 clevermous 3158
 
2414 Serge 3159
        mov     esi, [ebx-twdw+WDATA.box.left]
3160
        cmp     eax, esi
3161
        jb      .skip
3162
        add     esi, [ebx-twdw+WDATA.box.width]
3163
        cmp     eax, esi
3164
        ja      .skip
3165
 
3166
        mov     esi, [ebx-twdw+WDATA.box.top]
3167
        cmp     edx, esi
3168
        jb      .skip
3169
        add     esi, [ebx-twdw+WDATA.box.height]
3170
        cmp     edx, esi
3171
        ja      .skip
2411 Serge 3172
.set:
2408 Serge 3173
        or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b
2411 Serge 3174
.skip:
2408 Serge 3175
        loop    .set_mouse_event
3176
 
2414 Serge 3177
        pop     eax
3178
 
2288 clevermous 3179
mouse_not_active:
3180
        cmp     byte[BACKGROUND_CHANGED], 0
3181
        jz      no_set_bgr_event
3182
        xor     edi, edi
3183
        mov     ecx, [TASK_COUNT]
3184
set_bgr_event:
3185
        add     edi, 256
3186
        or      [edi+SLOT_BASE+APPDATA.event_mask], 16
3187
        loop    set_bgr_event
3188
        mov     byte[BACKGROUND_CHANGED], 0
3189
no_set_bgr_event:
3190
        cmp     byte[REDRAW_BACKGROUND], 0         ; background update ?
3191
        jz      nobackgr
3192
        cmp     [background_defined], 0
3193
        jz      nobackgr
3194
;    mov   [draw_data+32 + RECT.left],dword 0
3195
;    mov   [draw_data+32 + RECT.top],dword 0
3196
;    mov   eax,[Screen_Max_X]
3197
;    mov   ebx,[Screen_Max_Y]
3198
;    mov   [draw_data+32 + RECT.right],eax
3199
;    mov   [draw_data+32 + RECT.bottom],ebx
3200
@@:
3201
        call    drawbackground
3202
        xor     eax, eax
3203
        xchg    al, [REDRAW_BACKGROUND]
3204
        test    al, al                             ; got new update request?
3205
        jnz     @b
3206
        mov     [draw_data+32 + RECT.left], eax
3207
        mov     [draw_data+32 + RECT.top], eax
3208
        mov     [draw_data+32 + RECT.right], eax
3209
        mov     [draw_data+32 + RECT.bottom], eax
3210
        mov     [MOUSE_BACKGROUND], byte 0
3211
 
3212
nobackgr:
3213
 
3214
    ; system shutdown request
3215
 
3216
        cmp     [SYS_SHUTDOWN], byte 0
3217
        je      noshutdown
3218
 
3219
        mov     edx, [shutdown_processes]
3220
 
3221
        cmp     [SYS_SHUTDOWN], dl
3222
        jne     no_mark_system_shutdown
3223
 
3224
        lea     ecx, [edx-1]
3225
        mov     edx, OS_BASE+0x3040
3226
        jecxz   @f
3227
markz:
3228
        mov     [edx+TASKDATA.state], byte 3
3229
        add     edx, 0x20
3230
        loop    markz
3231
@@:
3232
 
3233
  no_mark_system_shutdown:
3234
 
3235
        dec     byte [SYS_SHUTDOWN]
3236
        je      system_shutdown
3237
 
3238
noshutdown:
3239
 
3240
 
3241
        mov     eax, [TASK_COUNT]           ; termination
3242
        mov     ebx, TASK_DATA+TASKDATA.state
3243
        mov     esi, 1
3244
 
3245
newct:
3246
        mov     cl, [ebx]
3247
        cmp     cl, byte 3
3248
        jz      terminate
3249
        cmp     cl, byte 4
3250
        jz      terminate
3251
 
3252
        add     ebx, 0x20
3253
        inc     esi
3254
        dec     eax
3255
        jnz     newct
3256
        ret
3257
 
3258
; redraw screen
3259
 
3260
redrawscreen:
3261
 
3262
; eax , if process window_data base is eax, do not set flag/limits
3263
 
3264
        pushad
3265
        push    eax
3266
 
3267
;;;         mov   ebx,2
3268
;;;         call  delay_hs
3269
 
3270
         ;mov   ecx,0               ; redraw flags for apps
3271
        xor     ecx, ecx
3272
       newdw2:
3273
 
3274
        inc     ecx
3275
        push    ecx
3276
 
3277
        mov     eax, ecx
3278
        shl     eax, 5
3279
        add     eax, window_data
3280
 
3281
        cmp     eax, [esp+4]
3282
        je      not_this_task
3283
                                   ; check if window in redraw area
3284
        mov     edi, eax
3285
 
3286
        cmp     ecx, 1             ; limit for background
3287
        jz      bgli
3288
 
3289
        mov     eax, [edi + WDATA.box.left]
3290
        mov     ebx, [edi + WDATA.box.top]
3291
        mov     ecx, [edi + WDATA.box.width]
3292
        mov     edx, [edi + WDATA.box.height]
3293
        add     ecx, eax
3294
        add     edx, ebx
3295
 
3296
        mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
3297
        cmp     ecx, ebx
3298
        jb      ricino
3299
 
3300
        mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
3301
        cmp     ecx, eax
3302
        jb      ricino
3303
 
3304
        mov     eax, [edi + WDATA.box.left]
3305
        mov     ebx, [edi + WDATA.box.top]
3306
        mov     ecx, [edi + WDATA.box.width]
3307
        mov     edx, [edi + WDATA.box.height]
3308
        add     ecx, eax
3309
        add     edx, ebx
3310
 
3311
        mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
3312
        cmp     edx, eax
3313
        jb      ricino
3314
 
3315
        mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
3316
        cmp     ecx, eax
3317
        jb      ricino
3318
 
3319
        bgli:
3320
 
3321
        cmp     dword[esp], 1
3322
        jnz     .az
3323
;         cmp   byte[BACKGROUND_CHANGED], 0
3324
;         jnz   newdw8
3325
        cmp     byte[REDRAW_BACKGROUND], 0
3326
        jz      .az
3327
        mov     dl, 0
3328
        lea     eax, [edi+draw_data-window_data]
3329
        mov     ebx, [draw_limits.left]
3330
        cmp     ebx, [eax+RECT.left]
3331
        jae     @f
3332
        mov     [eax+RECT.left], ebx
3333
        mov     dl, 1
3334
        @@:
3335
        mov     ebx, [draw_limits.top]
3336
        cmp     ebx, [eax+RECT.top]
3337
        jae     @f
3338
        mov     [eax+RECT.top], ebx
3339
        mov     dl, 1
3340
        @@:
3341
        mov     ebx, [draw_limits.right]
3342
        cmp     ebx, [eax+RECT.right]
3343
        jbe     @f
3344
        mov     [eax+RECT.right], ebx
3345
        mov     dl, 1
3346
        @@:
3347
        mov     ebx, [draw_limits.bottom]
3348
        cmp     ebx, [eax+RECT.bottom]
3349
        jbe     @f
3350
        mov     [eax+RECT.bottom], ebx
3351
        mov     dl, 1
3352
        @@:
3353
        add     byte[REDRAW_BACKGROUND], dl
3354
        jmp     newdw8
3355
        .az:
3356
 
3357
        mov     eax, edi
3358
        add     eax, draw_data-window_data
3359
 
3360
        mov     ebx, [draw_limits.left]        ; set limits
3361
        mov     [eax + RECT.left], ebx
3362
        mov     ebx, [draw_limits.top]
3363
        mov     [eax + RECT.top], ebx
3364
        mov     ebx, [draw_limits.right]
3365
        mov     [eax + RECT.right], ebx
3366
        mov     ebx, [draw_limits.bottom]
3367
        mov     [eax + RECT.bottom], ebx
3368
 
3369
        sub     eax, draw_data-window_data
3370
 
3371
        cmp     dword [esp], 1
3372
        jne     nobgrd
3373
        inc     byte[REDRAW_BACKGROUND]
3374
 
3375
       newdw8:
3376
       nobgrd:
3377
 
3378
        mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
3379
 
3380
       ricino:
3381
 
3382
       not_this_task:
3383
 
3384
        pop     ecx
3385
 
3386
        cmp     ecx, [TASK_COUNT]
3387
        jle     newdw2
3388
 
3389
        pop     eax
3390
        popad
3391
 
3392
        ret
3393
 
3394
calculatebackground:   ; background
3395
 
3396
        mov     edi, [_WinMapAddress]              ; set os to use all pixels
3397
        mov     eax, 0x01010101
3398
        mov     ecx, [_WinMapSize]
3399
        shr     ecx, 2
3400
        rep stosd
3401
 
3402
        mov     byte[REDRAW_BACKGROUND], 0            ; do not draw background!
3403
        mov     byte[BACKGROUND_CHANGED], 0
3404
 
3405
        ret
3406
 
3407
uglobal
3408
  imax    dd 0x0
3409
endg
3410
 
3411
 
3412
 
3413
delay_ms:     ; delay in 1/1000 sec
3414
 
3415
 
3416
        push    eax
3417
        push    ecx
3418
 
3419
        mov     ecx, esi
3420
        ; 
3421
        imul    ecx, 33941
3422
        shr     ecx, 9
3423
        ; 
3424
 
3425
        in      al, 0x61
3426
        and     al, 0x10
3427
        mov     ah, al
3428
        cld
3429
 
3430
 cnt1:
3431
        in      al, 0x61
3432
        and     al, 0x10
3433
        cmp     al, ah
3434
        jz      cnt1
3435
 
3436
        mov     ah, al
3437
        loop    cnt1
3438
 
3439
        pop     ecx
3440
        pop     eax
3441
 
3442
        ret
3443
 
2411 Serge 3444
align 4
2288 clevermous 3445
set_app_param:
3446
        mov     edi, [TASK_BASE]
2408 Serge 3447
        mov     eax, ebx
3448
        btr     eax, 3                           ; move MOUSE_FILTRATION
3449
        mov     ebx, [current_slot]              ; bit into event_filter
3450
        setc    byte [ebx+APPDATA.event_filter]
3451
        xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
3452
        mov     [esp+32], eax                    ; return old mask value
2288 clevermous 3453
        ret
3454
 
3455
 
3456
 
3457
delay_hs:     ; delay in 1/100 secs
3458
; ebx = delay time
3459
        push    ecx
3460
        push    edx
3461
 
3462
        mov     edx, [timer_ticks]
3463
 
3464
      newtic:
3465
        mov     ecx, [timer_ticks]
3466
        sub     ecx, edx
3467
        cmp     ecx, ebx
3468
        jae     zerodelay
3469
 
3470
        call    change_task
3471
 
3472
        jmp     newtic
3473
 
3474
      zerodelay:
3475
        pop     edx
3476
        pop     ecx
3477
 
3478
        ret
3479
 
3480
align 16        ;very often call this subrutine
3481
memmove:       ; memory move in bytes
3482
 
3483
; eax = from
3484
; ebx = to
3485
; ecx = no of bytes
3486
        test    ecx, ecx
3487
        jle     .ret
3488
 
3489
        push    esi edi ecx
3490
 
3491
        mov     edi, ebx
3492
        mov     esi, eax
3493
 
3494
        test    ecx, not 11b
3495
        jz      @f
3496
 
3497
        push    ecx
3498
        shr     ecx, 2
3499
        rep movsd
3500
        pop     ecx
3501
        and     ecx, 11b
3502
        jz      .finish
3503
  @@:
3504
        rep movsb
3505
 
3506
  .finish:
3507
        pop     ecx edi esi
3508
  .ret:
3509
        ret
3510
 
3511
 
3512
;  Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
3513
;align 4
3514
;
3515
;read_floppy_file:
3516
;
3517
;; as input
3518
;;
3519
;; eax pointer to file
3520
;; ebx file lenght
3521
;; ecx start 512 byte block number
3522
;; edx number of blocks to read
3523
;; esi pointer to return/work area (atleast 20 000 bytes)
3524
;;
3525
;;
3526
;; on return
3527
;;
3528
;; eax = 0 command succesful
3529
;;       1 no fd base and/or partition defined
3530
;;       2 yet unsupported FS
3531
;;       3 unknown FS
3532
;;       4 partition not defined at hd
3533
;;       5 file not found
3534
;; ebx = size of file
3535
;
3536
;     mov   edi,[TASK_BASE]
3537
;     add   edi,0x10
3538
;     add   esi,[edi]
3539
;     add   eax,[edi]
3540
;
3541
;     pushad
3542
;     mov  edi,esi
3543
;     add  edi,1024
3544
;     mov  esi,0x100000+19*512
3545
;     sub  ecx,1
3546
;     shl  ecx,9
3547
;     add  esi,ecx
3548
;     shl  edx,9
3549
;     mov  ecx,edx
3550
;     cld
3551
;     rep  movsb
3552
;     popad
3553
;
3554
;     mov   [esp+36],eax
3555
;     mov   [esp+24],ebx
3556
;     ret
3557
 
3558
 
3559
 
3560
align 4
3561
set_io_access_rights:
3562
        push    edi eax
3563
        mov     edi, tss._io_map_0
3564
;     mov   ecx,eax
3565
;     and   ecx,7    ; offset in byte
3566
;     shr   eax,3    ; number of byte
3567
;     add   edi,eax
3568
;     mov   ebx,1
3569
;     shl   ebx,cl
3570
        test    ebp, ebp
3571
;     cmp   ebp,0                ; enable access - ebp = 0
3572
        jnz     .siar1
3573
;     not   ebx
3574
;     and   [edi],byte bl
3575
        btr     [edi], eax
3576
        pop     eax edi
3577
        ret
3578
.siar1:
3579
        bts     [edi], eax
3580
  ;  or    [edi],byte bl        ; disable access - ebp = 1
3581
        pop     eax edi
3582
        ret
3583
;reserve/free group of ports
3584
;  * eax = 46 - number function
3585
;  * ebx = 0 - reserve, 1 - free
3586
;  * ecx = number start arrea of ports
3587
;  * edx = number end arrea of ports (include last number of port)
3588
;Return value:
3589
;  * eax = 0 - succesful
3590
;  * eax = 1 - error
3591
;  * The system has reserve this ports:
3592
;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
3593
;destroys eax,ebx, ebp
3594
r_f_port_area:
3595
 
3596
        test    ebx, ebx
3597
        jnz     free_port_area
3598
;     je    r_port_area
3599
;     jmp   free_port_area
3600
 
3601
;   r_port_area:
3602
 
3603
;     pushad
3604
 
3605
        cmp     ecx, edx      ; beginning > end ?
3606
        ja      rpal1
3607
        cmp     edx, 65536
3608
        jae     rpal1
3609
        mov     eax, [RESERVED_PORTS]
3610
        test    eax, eax      ; no reserved areas ?
3611
        je      rpal2
3612
        cmp     eax, 255      ; max reserved
3613
        jae     rpal1
3614
 rpal3:
3615
        mov     ebx, eax
3616
        shl     ebx, 4
3617
        add     ebx, RESERVED_PORTS
3618
        cmp     ecx, [ebx+8]
3619
        ja      rpal4
3620
        cmp     edx, [ebx+4]
3621
        jae     rpal1
3622
;     jb    rpal4
3623
;     jmp   rpal1
3624
 rpal4:
3625
        dec     eax
3626
        jnz     rpal3
3627
        jmp     rpal2
3628
   rpal1:
3629
;     popad
3630
;     mov   eax,1
3631
        xor     eax, eax
3632
        inc     eax
3633
        ret
3634
   rpal2:
3635
;     popad
3636
     ; enable port access at port IO map
3637
        cli
3638
        pushad                        ; start enable io map
3639
 
3640
        cmp     edx, 65536;16384
3641
        jae     no_unmask_io; jge
3642
        mov     eax, ecx
3643
;       push    ebp
3644
        xor     ebp, ebp               ; enable - eax = port
3645
new_port_access:
3646
;     pushad
3647
        call    set_io_access_rights
3648
;     popad
3649
        inc     eax
3650
        cmp     eax, edx
3651
        jbe     new_port_access
3652
;       pop     ebp
3653
no_unmask_io:
3654
        popad                         ; end enable io map
3655
        sti
3656
 
3657
        mov     eax, [RESERVED_PORTS]
3658
        add     eax, 1
3659
        mov     [RESERVED_PORTS], eax
3660
        shl     eax, 4
3661
        add     eax, RESERVED_PORTS
3662
        mov     ebx, [TASK_BASE]
3663
        mov     ebx, [ebx+TASKDATA.pid]
3664
        mov     [eax], ebx
3665
        mov     [eax+4], ecx
3666
        mov     [eax+8], edx
3667
 
3668
        xor     eax, eax
3669
        ret
3670
 
3671
free_port_area:
3672
 
3673
;     pushad
3674
        mov     eax, [RESERVED_PORTS]; no reserved areas ?
3675
        test    eax, eax
3676
        jz      frpal2
3677
        mov     ebx, [TASK_BASE]
3678
        mov     ebx, [ebx+TASKDATA.pid]
3679
   frpal3:
3680
        mov     edi, eax
3681
        shl     edi, 4
3682
        add     edi, RESERVED_PORTS
3683
        cmp     ebx, [edi]
3684
        jne     frpal4
3685
        cmp     ecx, [edi+4]
3686
        jne     frpal4
3687
        cmp     edx, [edi+8]
3688
        jne     frpal4
3689
        jmp     frpal1
3690
   frpal4:
3691
        dec     eax
3692
        jnz     frpal3
3693
   frpal2:
3694
;     popad
3695
        inc     eax
3696
        ret
3697
   frpal1:
3698
        push    ecx
3699
        mov     ecx, 256
3700
        sub     ecx, eax
3701
        shl     ecx, 4
3702
        mov     esi, edi
3703
        add     esi, 16
3704
        cld
3705
        rep movsb
3706
 
3707
        dec     dword [RESERVED_PORTS]
3708
;popad
3709
;disable port access at port IO map
3710
 
3711
;     pushad                        ; start disable io map
3712
        pop     eax     ;start port
3713
        cmp     edx, 65536;16384
3714
        jge     no_mask_io
3715
 
3716
;     mov   eax,ecx
3717
        xor     ebp, ebp
3718
        inc     ebp
3719
new_port_access_disable:
3720
;     pushad
3721
;     mov   ebp,1                  ; disable - eax = port
3722
        call    set_io_access_rights
3723
;     popad
3724
        inc     eax
3725
        cmp     eax, edx
3726
        jbe     new_port_access_disable
3727
no_mask_io:
3728
;     popad                         ; end disable io map
3729
        xor     eax, eax
3730
        ret
2430 mario79 3731
;-----------------------------------------------------------------------------
2288 clevermous 3732
align 4
3733
drawbackground:
2430 mario79 3734
dbrv20:
2288 clevermous 3735
        cmp     [BgrDrawMode], dword 1
3736
        jne     bgrstr
3737
        call    vesa20_drawbackground_tiled
3738
        call    [draw_pointer]
3739
        ret
2430 mario79 3740
;--------------------------------------
3741
align 4
3742
bgrstr:
2288 clevermous 3743
        call    vesa20_drawbackground_stretch
3744
        call    [draw_pointer]
3745
        ret
2430 mario79 3746
;-----------------------------------------------------------------------------
2288 clevermous 3747
align 4
3748
syscall_putimage:                       ; PutImage
3749
sys_putimage:
3750
        test    ecx, 0x80008000
3751
        jnz     .exit
3752
        test    ecx, 0x0000FFFF
3753
        jz      .exit
3754
        test    ecx, 0xFFFF0000
3755
        jnz     @f
2430 mario79 3756
;--------------------------------------
3757
align 4
3758
.exit:
2288 clevermous 3759
        ret
2430 mario79 3760
;--------------------------------------
3761
align 4
3762
@@:
2288 clevermous 3763
        mov     edi, [current_slot]
3764
        add     dx, word[edi+APPDATA.wnd_clientbox.top]
3765
        rol     edx, 16
3766
        add     dx, word[edi+APPDATA.wnd_clientbox.left]
3767
        rol     edx, 16
2430 mario79 3768
;--------------------------------------
3769
align 4
3770
.forced:
2288 clevermous 3771
        push    ebp esi 0
3772
        mov     ebp, putimage_get24bpp
3773
        mov     esi, putimage_init24bpp
2430 mario79 3774
;--------------------------------------
3775
align 4
2288 clevermous 3776
sys_putimage_bpp:
2430 mario79 3777
        call    vesa20_putimage
2288 clevermous 3778
        pop     ebp esi ebp
2430 mario79 3779
        ret
3780
;        jmp     [draw_pointer]
3781
;-----------------------------------------------------------------------------
2288 clevermous 3782
align 4
3783
sys_putimage_palette:
3784
; ebx = pointer to image
3785
; ecx = [xsize]*65536 + [ysize]
3786
; edx = [xstart]*65536 + [ystart]
3787
; esi = number of bits per pixel, must be 8, 24 or 32
3788
; edi = pointer to palette
3789
; ebp = row delta
3790
        mov     eax, [CURRENT_TASK]
3791
        shl     eax, 8
3792
        add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
3793
        rol     edx, 16
3794
        add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
3795
        rol     edx, 16
2430 mario79 3796
;--------------------------------------
3797
align 4
2288 clevermous 3798
.forced:
3799
        cmp     esi, 1
3800
        jnz     @f
3801
        push    edi
3802
        mov     eax, [edi+4]
3803
        sub     eax, [edi]
3804
        push    eax
3805
        push    dword [edi]
3806
        push    0ffffff80h
3807
        mov     edi, esp
3808
        call    put_mono_image
3809
        add     esp, 12
3810
        pop     edi
3811
        ret
2430 mario79 3812
;--------------------------------------
3813
align 4
2288 clevermous 3814
@@:
3815
        cmp     esi, 2
3816
        jnz     @f
3817
        push    edi
3818
        push    0ffffff80h
3819
        mov     edi, esp
3820
        call    put_2bit_image
3821
        pop     eax
3822
        pop     edi
3823
        ret
2430 mario79 3824
;--------------------------------------
3825
align 4
2288 clevermous 3826
@@:
3827
        cmp     esi, 4
3828
        jnz     @f
3829
        push    edi
3830
        push    0ffffff80h
3831
        mov     edi, esp
3832
        call    put_4bit_image
3833
        pop     eax
3834
        pop     edi
3835
        ret
2430 mario79 3836
;--------------------------------------
3837
align 4
2288 clevermous 3838
@@:
3839
        push    ebp esi ebp
3840
        cmp     esi, 8
3841
        jnz     @f
3842
        mov     ebp, putimage_get8bpp
3843
        mov     esi, putimage_init8bpp
3844
        jmp     sys_putimage_bpp
2430 mario79 3845
;--------------------------------------
3846
align 4
2288 clevermous 3847
@@:
3848
        cmp     esi, 15
3849
        jnz     @f
3850
        mov     ebp, putimage_get15bpp
3851
        mov     esi, putimage_init15bpp
3852
        jmp     sys_putimage_bpp
2430 mario79 3853
;--------------------------------------
3854
align 4
2288 clevermous 3855
@@:
3856
        cmp     esi, 16
3857
        jnz     @f
3858
        mov     ebp, putimage_get16bpp
3859
        mov     esi, putimage_init16bpp
3860
        jmp     sys_putimage_bpp
2430 mario79 3861
;--------------------------------------
3862
align 4
2288 clevermous 3863
@@:
3864
        cmp     esi, 24
3865
        jnz     @f
3866
        mov     ebp, putimage_get24bpp
3867
        mov     esi, putimage_init24bpp
3868
        jmp     sys_putimage_bpp
2430 mario79 3869
;--------------------------------------
3870
align 4
2288 clevermous 3871
@@:
3872
        cmp     esi, 32
3873
        jnz     @f
3874
        mov     ebp, putimage_get32bpp
3875
        mov     esi, putimage_init32bpp
3876
        jmp     sys_putimage_bpp
2430 mario79 3877
;--------------------------------------
3878
align 4
2288 clevermous 3879
@@:
3880
        pop     ebp esi ebp
3881
        ret
2430 mario79 3882
;-----------------------------------------------------------------------------
3883
align 4
2288 clevermous 3884
put_mono_image:
3885
        push    ebp esi ebp
3886
        mov     ebp, putimage_get1bpp
3887
        mov     esi, putimage_init1bpp
3888
        jmp     sys_putimage_bpp
2430 mario79 3889
;-----------------------------------------------------------------------------
3890
align 4
2288 clevermous 3891
put_2bit_image:
3892
        push    ebp esi ebp
3893
        mov     ebp, putimage_get2bpp
3894
        mov     esi, putimage_init2bpp
3895
        jmp     sys_putimage_bpp
2430 mario79 3896
;-----------------------------------------------------------------------------
3897
align 4
2288 clevermous 3898
put_4bit_image:
3899
        push    ebp esi ebp
3900
        mov     ebp, putimage_get4bpp
3901
        mov     esi, putimage_init4bpp
3902
        jmp     sys_putimage_bpp
2430 mario79 3903
;-----------------------------------------------------------------------------
3904
align 4
2288 clevermous 3905
putimage_init24bpp:
3906
        lea     eax, [eax*3]
3907
putimage_init8bpp:
3908
        ret
2430 mario79 3909
;-----------------------------------------------------------------------------
2288 clevermous 3910
align 16
3911
putimage_get24bpp:
3912
        movzx   eax, byte [esi+2]
3913
        shl     eax, 16
3914
        mov     ax, [esi]
3915
        add     esi, 3
3916
        ret     4
2430 mario79 3917
;-----------------------------------------------------------------------------
2288 clevermous 3918
align 16
3919
putimage_get8bpp:
3920
        movzx   eax, byte [esi]
3921
        push    edx
3922
        mov     edx, [esp+8]
3923
        mov     eax, [edx+eax*4]
3924
        pop     edx
3925
        inc     esi
3926
        ret     4
2430 mario79 3927
;-----------------------------------------------------------------------------
3928
align 4
2288 clevermous 3929
putimage_init1bpp:
3930
        add     eax, ecx
3931
        push    ecx
3932
        add     eax, 7
3933
        add     ecx, 7
3934
        shr     eax, 3
3935
        shr     ecx, 3
3936
        sub     eax, ecx
3937
        pop     ecx
3938
        ret
2430 mario79 3939
;-----------------------------------------------------------------------------
2288 clevermous 3940
align 16
3941
putimage_get1bpp:
3942
        push    edx
3943
        mov     edx, [esp+8]
3944
        mov     al, [edx]
3945
        add     al, al
3946
        jnz     @f
3947
        lodsb
3948
        adc     al, al
3949
@@:
3950
        mov     [edx], al
3951
        sbb     eax, eax
3952
        and     eax, [edx+8]
3953
        add     eax, [edx+4]
3954
        pop     edx
3955
        ret     4
2430 mario79 3956
;-----------------------------------------------------------------------------
3957
align 4
2288 clevermous 3958
putimage_init2bpp:
3959
        add     eax, ecx
3960
        push    ecx
3961
        add     ecx, 3
3962
        add     eax, 3
3963
        shr     ecx, 2
3964
        shr     eax, 2
3965
        sub     eax, ecx
3966
        pop     ecx
3967
        ret
2430 mario79 3968
;-----------------------------------------------------------------------------
2288 clevermous 3969
align 16
3970
putimage_get2bpp:
3971
        push    edx
3972
        mov     edx, [esp+8]
3973
        mov     al, [edx]
3974
        mov     ah, al
3975
        shr     al, 6
3976
        shl     ah, 2
3977
        jnz     .nonewbyte
3978
        lodsb
3979
        mov     ah, al
3980
        shr     al, 6
3981
        shl     ah, 2
3982
        add     ah, 1
3983
.nonewbyte:
3984
        mov     [edx], ah
3985
        mov     edx, [edx+4]
3986
        movzx   eax, al
3987
        mov     eax, [edx+eax*4]
3988
        pop     edx
3989
        ret     4
2430 mario79 3990
;-----------------------------------------------------------------------------
3991
align 4
2288 clevermous 3992
putimage_init4bpp:
3993
        add     eax, ecx
3994
        push    ecx
3995
        add     ecx, 1
3996
        add     eax, 1
3997
        shr     ecx, 1
3998
        shr     eax, 1
3999
        sub     eax, ecx
4000
        pop     ecx
4001
        ret
2430 mario79 4002
;-----------------------------------------------------------------------------
2288 clevermous 4003
align 16
4004
putimage_get4bpp:
4005
        push    edx
4006
        mov     edx, [esp+8]
4007
        add     byte [edx], 80h
4008
        jc      @f
4009
        movzx   eax, byte [edx+1]
4010
        mov     edx, [edx+4]
4011
        and     eax, 0x0F
4012
        mov     eax, [edx+eax*4]
4013
        pop     edx
4014
        ret     4
4015
@@:
4016
        movzx   eax, byte [esi]
4017
        add     esi, 1
4018
        mov     [edx+1], al
4019
        shr     eax, 4
4020
        mov     edx, [edx+4]
4021
        mov     eax, [edx+eax*4]
4022
        pop     edx
4023
        ret     4
2430 mario79 4024
;-----------------------------------------------------------------------------
4025
align 4
2288 clevermous 4026
putimage_init32bpp:
4027
        shl     eax, 2
4028
        ret
2430 mario79 4029
;-----------------------------------------------------------------------------
2288 clevermous 4030
align 16
4031
putimage_get32bpp:
4032
        lodsd
4033
        ret     4
2430 mario79 4034
;-----------------------------------------------------------------------------
4035
align 4
2288 clevermous 4036
putimage_init15bpp:
4037
putimage_init16bpp:
4038
        add     eax, eax
4039
        ret
2430 mario79 4040
;-----------------------------------------------------------------------------
2288 clevermous 4041
align 16
4042
putimage_get15bpp:
4043
; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
4044
        push    ecx edx
4045
        movzx   eax, word [esi]
4046
        add     esi, 2
4047
        mov     ecx, eax
4048
        mov     edx, eax
4049
        and     eax, 0x1F
4050
        and     ecx, 0x1F shl 5
4051
        and     edx, 0x1F shl 10
4052
        shl     eax, 3
4053
        shl     ecx, 6
4054
        shl     edx, 9
4055
        or      eax, ecx
4056
        or      eax, edx
4057
        pop     edx ecx
4058
        ret     4
2430 mario79 4059
;-----------------------------------------------------------------------------
2288 clevermous 4060
align 16
4061
putimage_get16bpp:
4062
; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
4063
        push    ecx edx
4064
        movzx   eax, word [esi]
4065
        add     esi, 2
4066
        mov     ecx, eax
4067
        mov     edx, eax
4068
        and     eax, 0x1F
4069
        and     ecx, 0x3F shl 5
4070
        and     edx, 0x1F shl 11
4071
        shl     eax, 3
4072
        shl     ecx, 5
4073
        shl     edx, 8
4074
        or      eax, ecx
4075
        or      eax, edx
4076
        pop     edx ecx
4077
        ret     4
2430 mario79 4078
;-----------------------------------------------------------------------------
4079
;align 4
2288 clevermous 4080
; eax x beginning
4081
; ebx y beginning
4082
; ecx x end
4083
        ; edx y end
4084
; edi color
2430 mario79 4085
;__sys_drawbar:
4086
;        mov     esi, [current_slot]
4087
;        add     eax, [esi+APPDATA.wnd_clientbox.left]
4088
;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
4089
;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
4090
;        add     edx, [esi+APPDATA.wnd_clientbox.top]
4091
;--------------------------------------
4092
;align 4
4093
;.forced:
4094
;        call    vesa20_drawbar
2407 mario79 4095
;        call    [draw_pointer]
4096
;        ret
2430 mario79 4097
;-----------------------------------------------------------------------------
4098
align 4
2288 clevermous 4099
kb_read:
4100
 
4101
        push    ecx edx
4102
 
4103
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4104
      kr_loop:
4105
        in      al, 0x64
4106
        test    al, 1
4107
        jnz     kr_ready
4108
        loop    kr_loop
4109
        mov     ah, 1
4110
        jmp     kr_exit
4111
      kr_ready:
4112
        push    ecx
4113
        mov     ecx, 32
4114
      kr_delay:
4115
        loop    kr_delay
4116
        pop     ecx
4117
        in      al, 0x60
4118
        xor     ah, ah
4119
      kr_exit:
4120
 
4121
        pop     edx ecx
4122
 
4123
        ret
2430 mario79 4124
;-----------------------------------------------------------------------------
4125
align 4
2288 clevermous 4126
kb_write:
4127
 
4128
        push    ecx edx
4129
 
4130
        mov     dl, al
4131
;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
4132
;      kw_loop1:
4133
;        in      al,0x64
4134
;        test    al,0x20
4135
;        jz      kw_ok1
4136
;        loop    kw_loop1
4137
;        mov     ah,1
4138
;        jmp     kw_exit
4139
;      kw_ok1:
4140
        in      al, 0x60
4141
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4142
      kw_loop:
4143
        in      al, 0x64
4144
        test    al, 2
4145
        jz      kw_ok
4146
        loop    kw_loop
4147
        mov     ah, 1
4148
        jmp     kw_exit
4149
      kw_ok:
4150
        mov     al, dl
4151
        out     0x60, al
4152
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4153
      kw_loop3:
4154
        in      al, 0x64
4155
        test    al, 2
4156
        jz      kw_ok3
4157
        loop    kw_loop3
4158
        mov     ah, 1
4159
        jmp     kw_exit
4160
      kw_ok3:
4161
        mov     ah, 8
4162
      kw_loop4:
4163
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4164
      kw_loop5:
4165
        in      al, 0x64
4166
        test    al, 1
4167
        jnz     kw_ok4
4168
        loop    kw_loop5
4169
        dec     ah
4170
        jnz     kw_loop4
4171
      kw_ok4:
4172
        xor     ah, ah
4173
      kw_exit:
4174
 
4175
        pop     edx ecx
4176
 
4177
        ret
2430 mario79 4178
;-----------------------------------------------------------------------------
4179
align 4
2288 clevermous 4180
kb_cmd:
4181
 
4182
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4183
      c_wait:
4184
        in      al, 0x64
4185
        test    al, 2
4186
        jz      c_send
4187
        loop    c_wait
4188
        jmp     c_error
4189
      c_send:
4190
        mov     al, bl
4191
        out     0x64, al
4192
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4193
      c_accept:
4194
        in      al, 0x64
4195
        test    al, 2
4196
        jz      c_ok
4197
        loop    c_accept
4198
      c_error:
4199
        mov     ah, 1
4200
        jmp     c_exit
4201
      c_ok:
4202
        xor     ah, ah
4203
      c_exit:
4204
        ret
4205
 
4206
 
4207
setmouse:  ; set mousepicture -pointer
4208
           ; ps2 mouse enable
4209
 
2430 mario79 4210
;        mov     [MOUSE_PICTURE], dword mousepointer
2288 clevermous 4211
 
4212
        cli
4213
 
4214
        ret
4215
 
4216
if used _rdtsc
4217
_rdtsc:
4218
        bt      [cpu_caps], CAPS_TSC
4219
        jnc     ret_rdtsc
4220
        rdtsc
4221
        ret
4222
   ret_rdtsc:
4223
        mov     edx, 0xffffffff
4224
        mov     eax, 0xffffffff
4225
        ret
4226
end if
4227
 
4228
sys_msg_board_str:
4229
 
4230
        pushad
4231
   @@:
4232
        cmp     [esi], byte 0
4233
        je      @f
4234
        mov     eax, 1
4235
        movzx   ebx, byte [esi]
4236
        call    sys_msg_board
4237
        inc     esi
4238
        jmp     @b
4239
   @@:
4240
        popad
4241
        ret
4242
 
4243
sys_msg_board_byte:
4244
; in: al = byte to display
4245
; out: nothing
4246
; destroys: nothing
4247
        pushad
4248
        mov     ecx, 2
4249
        shl     eax, 24
4250
        jmp     @f
4251
 
4252
sys_msg_board_word:
4253
; in: ax = word to display
4254
; out: nothing
4255
; destroys: nothing
4256
        pushad
4257
        mov     ecx, 4
4258
        shl     eax, 16
4259
        jmp     @f
4260
 
4261
sys_msg_board_dword:
4262
; in: eax = dword to display
4263
; out: nothing
4264
; destroys: nothing
4265
        pushad
4266
        mov     ecx, 8
4267
@@:
4268
        push    ecx
4269
        rol     eax, 4
4270
        push    eax
4271
        and     al, 0xF
4272
        cmp     al, 10
4273
        sbb     al, 69h
4274
        das
4275
        mov     bl, al
4276
        xor     eax, eax
4277
        inc     eax
4278
        call    sys_msg_board
4279
        pop     eax
4280
        pop     ecx
4281
        loop    @b
4282
        popad
4283
        ret
4284
 
4285
uglobal
4286
  msg_board_data:
4287
                  times 4096 db 0
4288
  msg_board_count dd 0x0
4289
endg
4290
 
4291
sys_msg_board:
4292
 
4293
; eax=1 : write :  bl byte to write
4294
; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
4295
 
4296
        mov     ecx, [msg_board_count]
4297
        cmp     eax, 1
4298
        jne     .smbl1
4299
 
4300
if defined debug_com_base
4301
 
4302
        push    dx ax
4303
 
4304
       @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
4305
        mov     dx, debug_com_base+5
4306
        in      al, dx
4307
        test    al, 1 shl 5
4308
        jz      @r
4309
 
4310
        mov     dx, debug_com_base      ; Output the byte
4311
        mov     al, bl
4312
        out     dx, al
4313
 
4314
        pop     ax dx
4315
 
4316
end if
4317
 
4318
        mov     [msg_board_data+ecx], bl
4319
        inc     ecx
4320
        and     ecx, 4095
4321
        mov     [msg_board_count], ecx
4322
        mov     [check_idle_semaphore], 5
4323
        ret
4324
.smbl1:
4325
        cmp     eax, 2
4326
        jne     .smbl2
4327
        test    ecx, ecx
4328
        jz      .smbl21
4329
        mov     eax, msg_board_data+1
4330
        mov     ebx, msg_board_data
4331
        movzx   edx, byte [ebx]
4332
        call    memmove
4333
        dec     [msg_board_count]
4334
        mov     [esp + 36], edx ;eax
4335
        mov     [esp + 24], dword 1
4336
        ret
4337
.smbl21:
4338
        mov     [esp+36], ecx
4339
        mov     [esp+24], ecx
4340
.smbl2:
4341
        ret
4342
 
4343
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4344
;; 66 sys function.                                                ;;
4345
;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
4346
;; out eax                                                         ;;
4347
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4348
iglobal
4349
align 4
4350
f66call:
4351
           dd sys_process_def.1   ; 1 = set keyboard mode
4352
           dd sys_process_def.2   ; 2 = get keyboard mode
4353
           dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
4354
           dd sys_process_def.4
4355
           dd sys_process_def.5
4356
endg
4357
 
4358
 
4359
 
4360
 
4361
sys_process_def:
4362
        dec     ebx
4363
        cmp     ebx, 5
4364
        jae     .not_support    ;if >=6 then or eax,-1
4365
 
4366
        mov     edi, [CURRENT_TASK]
4367
        jmp     dword [f66call+ebx*4]
4368
 
4369
.not_support:
4370
        or      eax, -1
4371
        ret
4372
 
4373
.1:
4374
        shl     edi, 8
4375
        mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
4376
 
4377
        ret
4378
 
4379
.2:                             ; 2 = get keyboard mode
4380
        shl     edi, 8
4381
        movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
4382
        mov     [esp+32], eax
4383
        ret
4384
;     xor   eax,eax
4385
;     movzx eax,byte [shift]
4386
;     movzx ebx,byte [ctrl]
4387
;     shl   ebx,2
4388
;     add   eax,ebx
4389
;     movzx ebx,byte [alt]
4390
;     shl   ebx,3
4391
;     add   eax,ebx
4392
.3:                             ;3 = get keyboard ctrl, alt, shift
4393
 ;// mike.dld [
4394
        mov     eax, [kb_state]
4395
 ;// mike.dld ]
4396
        mov     [esp+32], eax
4397
        ret
4398
 
4399
.4:
4400
        mov     eax, hotkey_list
4401
@@:
4402
        cmp     dword [eax+8], 0
4403
        jz      .found_free
4404
        add     eax, 16
4405
        cmp     eax, hotkey_list+16*256
4406
        jb      @b
4407
        mov     dword [esp+32], 1
4408
        ret
4409
.found_free:
4410
        mov     [eax+8], edi
4411
        mov     [eax+4], edx
4412
        movzx   ecx, cl
4413
        lea     ecx, [hotkey_scancodes+ecx*4]
4414
        mov     edx, [ecx]
4415
        mov     [eax], edx
4416
        mov     [ecx], eax
4417
        mov     [eax+12], ecx
4418
        jecxz   @f
4419
        mov     [edx+12], eax
4420
@@:
4421
        and     dword [esp+32], 0
4422
        ret
4423
 
4424
.5:
4425
        movzx   ebx, cl
4426
        lea     ebx, [hotkey_scancodes+ebx*4]
4427
        mov     eax, [ebx]
4428
.scan:
4429
        test    eax, eax
4430
        jz      .notfound
4431
        cmp     [eax+8], edi
4432
        jnz     .next
4433
        cmp     [eax+4], edx
4434
        jz      .found
4435
.next:
4436
        mov     eax, [eax]
4437
        jmp     .scan
4438
.notfound:
4439
        mov     dword [esp+32], 1
4440
        ret
4441
.found:
4442
        mov     ecx, [eax]
4443
        jecxz   @f
4444
        mov     edx, [eax+12]
4445
        mov     [ecx+12], edx
4446
@@:
4447
        mov     ecx, [eax+12]
4448
        mov     edx, [eax]
4449
        mov     [ecx], edx
4450
        xor     edx, edx
4451
        mov     [eax+4], edx
4452
        mov     [eax+8], edx
4453
        mov     [eax+12], edx
4454
        mov     [eax], edx
4455
        mov     [esp+32], edx
4456
        ret
4457
 
4458
 
4459
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4460
;; 61 sys function.                                                ;;
4461
;; in eax=61,ebx in [1..3]                                         ;;
4462
;; out eax                                                         ;;
4463
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4464
iglobal
4465
align 4
4466
f61call:
4467
           dd sys_gs.1   ; resolution
4468
           dd sys_gs.2   ; bits per pixel
4469
           dd sys_gs.3   ; bytes per scanline
4470
endg
4471
 
4472
 
4473
align 4
4474
 
4475
sys_gs:                         ; direct screen access
4476
        dec     ebx
4477
        cmp     ebx, 2
4478
        ja      .not_support
4479
        jmp     dword [f61call+ebx*4]
4480
.not_support:
4481
        or      [esp+32], dword -1
4482
        ret
4483
 
4484
 
4485
.1:                             ; resolution
4486
        mov     eax, [Screen_Max_X]
4487
        shl     eax, 16
4488
        mov     ax, [Screen_Max_Y]
4489
        add     eax, 0x00010001
4490
        mov     [esp+32], eax
4491
        ret
4492
.2:                             ; bits per pixel
4493
        movzx   eax, byte [ScreenBPP]
4494
        mov     [esp+32], eax
4495
        ret
4496
.3:                             ; bytes per scanline
4497
        mov     eax, [BytesPerScanLine]
4498
        mov     [esp+32], eax
4499
        ret
4500
 
4501
align 4  ;  system functions
4502
 
4503
syscall_setpixel:                       ; SetPixel
4504
 
4505
        mov     eax, ebx
4506
        mov     ebx, ecx
4507
        mov     ecx, edx
4508
        mov     edx, [TASK_BASE]
4509
        add     eax, [edx-twdw+WDATA.box.left]
4510
        add     ebx, [edx-twdw+WDATA.box.top]
4511
        mov     edi, [current_slot]
4512
        add     eax, [edi+APPDATA.wnd_clientbox.left]
4513
        add     ebx, [edi+APPDATA.wnd_clientbox.top]
4514
        xor     edi, edi ; no force
2430 mario79 4515
        and     ecx, 0xFBFFFFFF  ;negate 0x04000000 save to mouseunder area
2288 clevermous 4516
        jmp     [putpixel]
4517
 
4518
align 4
4519
 
4520
syscall_writetext:                      ; WriteText
4521
 
4522
        mov     eax, [TASK_BASE]
4523
        mov     ebp, [eax-twdw+WDATA.box.left]
4524
        push    esi
4525
        mov     esi, [current_slot]
4526
        add     ebp, [esi+APPDATA.wnd_clientbox.left]
4527
        shl     ebp, 16
4528
        add     ebp, [eax-twdw+WDATA.box.top]
4529
        add     bp, word[esi+APPDATA.wnd_clientbox.top]
4530
        pop     esi
4531
        add     ebx, ebp
4532
        mov     eax, edi
4533
        xor     edi, edi
4534
        jmp     dtext
4535
 
4536
align 4
4537
 
4538
syscall_openramdiskfile:                ; OpenRamdiskFile
4539
 
4540
        mov     eax, ebx
4541
        mov     ebx, ecx
4542
        mov     ecx, edx
4543
        mov     edx, esi
4544
        mov     esi, 12
4545
        call    fileread
4546
        mov     [esp+32], eax
4547
        ret
4548
 
4549
align 4
4550
 
4551
syscall_drawrect:                       ; DrawRect
4552
 
4553
        mov     edi, edx ; color + gradient
4554
        and     edi, 0x80FFFFFF
4555
        test    bx, bx  ; x.size
4556
        je      .drectr
4557
        test    cx, cx ; y.size
4558
        je      .drectr
4559
 
4560
        mov     eax, ebx ; bad idea
4561
        mov     ebx, ecx
4562
 
4563
        movzx   ecx, ax ; ecx - x.size
4564
        shr     eax, 16 ; eax - x.coord
4565
        movzx   edx, bx ; edx - y.size
4566
        shr     ebx, 16 ; ebx - y.coord
4567
        mov     esi, [current_slot]
4568
 
4569
        add     eax, [esi + APPDATA.wnd_clientbox.left]
4570
        add     ebx, [esi + APPDATA.wnd_clientbox.top]
4571
        add     ecx, eax
4572
        add     edx, ebx
4573
        jmp     [drawbar]
4574
.drectr:
4575
        ret
4576
 
4577
align 4
4578
syscall_getscreensize:                  ; GetScreenSize
4579
        mov     ax, [Screen_Max_X]
4580
        shl     eax, 16
4581
        mov     ax, [Screen_Max_Y]
4582
        mov     [esp + 32], eax
4583
        ret
4584
 
4585
align 4
4586
 
4587
syscall_cdaudio:                        ; CD
4588
 
4589
        cmp     ebx, 4
4590
        jb      .audio
4591
        jz      .eject
4592
        cmp     ebx, 5
4593
        jnz     .ret
4594
.load:
4595
        call    .reserve
4596
        call    LoadMedium
4597
        ;call    .free
4598
        jmp     .free
4599
;        ret
4600
.eject:
4601
        call    .reserve
4602
        call    clear_CD_cache
4603
        call    allow_medium_removal
4604
        call    EjectMedium
4605
;        call    .free
4606
        jmp     .free
4607
;        ret
4608
.audio:
4609
        call    sys_cd_audio
4610
        mov     [esp+36-4], eax
4611
.ret:
4612
        ret
4613
 
4614
.reserve:
4615
        call    reserve_cd
4616
        mov     eax, ecx
4617
        shr     eax, 1
4618
        and     eax, 1
4619
        inc     eax
4620
        mov     [ChannelNumber], ax
4621
        mov     eax, ecx
4622
        and     eax, 1
4623
        mov     [DiskNumber], al
4624
        call    reserve_cd_channel
4625
        and     ebx, 3
4626
        inc     ebx
4627
        mov     [cdpos], ebx
4628
        add     ebx, ebx
4629
        mov     cl, 8
4630
        sub     cl, bl
4631
        mov     al, [DRIVE_DATA+1]
4632
        shr     al, cl
4633
        test    al, 2
4634
        jz      .free;.err
4635
        ret
4636
.free:
4637
        call    free_cd_channel
4638
        and     [cd_status], 0
4639
        ret
4640
.err:
4641
        call    .free
4642
;        pop     eax
4643
        ret
4644
 
4645
align 4
4646
 
4647
syscall_getpixel:                       ; GetPixel
4648
        mov     ecx, [Screen_Max_X]
4649
        inc     ecx
4650
        xor     edx, edx
4651
        mov     eax, ebx
4652
        div     ecx
4653
        mov     ebx, edx
4654
        xchg    eax, ebx
2430 mario79 4655
        and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
2288 clevermous 4656
        call    dword [GETPIXEL]; eax - x, ebx - y
4657
        mov     [esp + 32], ecx
4658
        ret
4659
 
4660
align 4
4661
 
4662
syscall_getarea:
4663
;eax = 36
4664
;ebx = pointer to bufer for img BBGGRRBBGGRR...
4665
;ecx = [size x]*65536 + [size y]
4666
;edx = [start x]*65536 + [start y]
4667
        pushad
4668
        mov     edi, ebx
4669
        mov     eax, edx
4670
        shr     eax, 16
4671
        mov     ebx, edx
4672
        and     ebx, 0xffff
4673
        dec     eax
4674
        dec     ebx
4675
     ; eax - x, ebx - y
4676
        mov     edx, ecx
4677
 
4678
        shr     ecx, 16
4679
        and     edx, 0xffff
4680
        mov     esi, ecx
4681
     ; ecx - size x, edx - size y
4682
 
4683
        mov     ebp, edx
4684
        dec     ebp
4685
        lea     ebp, [ebp*3]
4686
 
4687
        imul    ebp, esi
4688
 
4689
        mov     esi, ecx
4690
        dec     esi
4691
        lea     esi, [esi*3]
4692
 
4693
        add     ebp, esi
4694
        add     ebp, edi
4695
 
4696
        add     ebx, edx
4697
 
4698
.start_y:
4699
        push    ecx edx
4700
.start_x:
4701
        push    eax ebx ecx
4702
        add     eax, ecx
4703
 
2430 mario79 4704
        and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
2288 clevermous 4705
        call    dword [GETPIXEL]; eax - x, ebx - y
4706
 
4707
        mov     [ebp], cx
4708
        shr     ecx, 16
4709
        mov     [ebp+2], cl
4710
 
4711
        pop     ecx ebx eax
4712
        sub     ebp, 3
4713
        dec     ecx
4714
        jnz     .start_x
4715
        pop     edx ecx
4716
        dec     ebx
4717
        dec     edx
4718
        jnz     .start_y
4719
        popad
4720
        ret
4721
 
4722
align 4
4723
 
4724
syscall_drawline:                       ; DrawLine
4725
 
4726
        mov     edi, [TASK_BASE]
4727
        movzx   eax, word[edi-twdw+WDATA.box.left]
4728
        mov     ebp, eax
4729
        mov     esi, [current_slot]
4730
        add     ebp, [esi+APPDATA.wnd_clientbox.left]
4731
        add     ax, word[esi+APPDATA.wnd_clientbox.left]
4732
        add     ebp, ebx
4733
        shl     eax, 16
4734
        movzx   ebx, word[edi-twdw+WDATA.box.top]
4735
        add     eax, ebp
4736
        mov     ebp, ebx
4737
        add     ebp, [esi+APPDATA.wnd_clientbox.top]
4738
        add     bx, word[esi+APPDATA.wnd_clientbox.top]
4739
        add     ebp, ecx
4740
        shl     ebx, 16
4741
        xor     edi, edi
4742
        add     ebx, ebp
4743
        mov     ecx, edx
4744
        jmp     [draw_line]
4745
 
4746
 
4747
 
4748
align 4
4749
syscall_reserveportarea:                ; ReservePortArea and FreePortArea
4750
 
4751
        call    r_f_port_area
4752
        mov     [esp+32], eax
4753
        ret
4754
 
4755
align 4
4756
syscall_threads:                        ; CreateThreads
4757
; eax=1 create thread
4758
;
4759
;   ebx=thread start
4760
;   ecx=thread stack value
4761
;
4762
; on return : eax = pid
4763
 
4764
        call    new_sys_threads
4765
 
4766
        mov     [esp+32], eax
4767
        ret
4768
 
4769
align 4
4770
 
4771
stack_driver_stat:
4772
 
4773
        call    app_stack_handler       ; Stack status
4774
 
4775
;     mov   [check_idle_semaphore],5    ; enable these for zero delay
4776
;     call  change_task                 ; between sent packet
4777
 
4778
        mov     [esp+32], eax
4779
        ret
4780
 
4781
align 4
4782
 
4783
socket:                                 ; Socket interface
4784
        call    app_socket_handler
4785
 
4786
;     mov   [check_idle_semaphore],5    ; enable these for zero delay
4787
;     call  change_task                 ; between sent packet
4788
 
4789
        mov     [esp+36], eax
4790
        mov     [esp+24], ebx
4791
        ret
4792
 
4793
align 4
4794
 
4795
read_from_hd:                           ; Read from hd - fn not in use
4796
 
4797
        mov     edi, [TASK_BASE]
4798
        add     edi, TASKDATA.mem_start
4799
        add     eax, [edi]
4800
        add     ecx, [edi]
4801
        add     edx, [edi]
4802
        call    file_read
4803
 
4804
        mov     [esp+36], eax
4805
        mov     [esp+24], ebx
4806
 
4807
        ret
4808
 
4809
paleholder:
4810
        ret
2438 mario79 4811
;------------------------------------------------------------------------------
2288 clevermous 4812
align 4
2438 mario79 4813
calculate_fast_getting_offset_for_WinMapAddress:
4814
; calculate data area for fast getting offset to _WinMapAddress
2446 mario79 4815
        xor     eax, eax
2438 mario79 4816
        mov     ecx, [_display.height]
4817
        inc     ecx
4818
        mov     edi, d_width_calc_area
4819
        cld
4820
@@:
4821
        stosd
4822
        add     eax, [_display.width]
4823
        dec     ecx
4824
        jnz     @r
4825
        ret
4826
;------------------------------------------------------------------------------
4827
align 4
2288 clevermous 4828
set_screen:
4829
        cmp     eax, [Screen_Max_X]
4830
        jne     .set
4831
 
4832
        cmp     edx, [Screen_Max_Y]
4833
        jne     .set
4834
        ret
4835
.set:
4836
        pushfd
4837
        cli
4838
 
4839
        mov     [Screen_Max_X], eax
4840
        mov     [Screen_Max_Y], edx
4841
        mov     [BytesPerScanLine], ecx
4842
 
4843
        mov     [screen_workarea.right], eax
4844
        mov     [screen_workarea.bottom], edx
4845
 
4846
        push    ebx
4847
        push    esi
4848
        push    edi
4849
 
4850
        pushad
4851
 
4852
        stdcall kernel_free, [_WinMapAddress]
4853
 
4854
        mov     eax, [_display.width]
4855
        mul     [_display.height]
4856
        mov     [_WinMapSize], eax
4857
 
4858
        stdcall kernel_alloc, eax
4859
        mov     [_WinMapAddress], eax
4860
        test    eax, eax
4861
        jz      .epic_fail
4862
 
2438 mario79 4863
        call    calculate_fast_getting_offset_for_WinMapAddress
4864
 
2288 clevermous 4865
        popad
4866
 
4867
        call    repos_windows
4868
        xor     eax, eax
4869
        xor     ebx, ebx
4870
        mov     ecx, [Screen_Max_X]
4871
        mov     edx, [Screen_Max_Y]
4872
        call    calculatescreen
4873
        pop     edi
4874
        pop     esi
4875
        pop     ebx
4876
 
4877
        popfd
4878
        ret
4879
 
4880
.epic_fail:
4881
        hlt                     ; Houston, we've had a problem
4882
 
4883
; --------------- APM ---------------------
4884
uglobal
4885
apm_entry       dp      0
4886
apm_vf          dd      0
4887
endg
4888
 
4889
align 4
4890
sys_apm:
4891
        xor     eax, eax
4892
        cmp     word [apm_vf], ax       ; Check APM BIOS enable
4893
        jne     @f
4894
        inc     eax
4895
        or      dword [esp + 44], eax   ; error
4896
        add     eax, 7
4897
        mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
4898
        ret
4899
 
4900
@@:
4901
;       xchg    eax, ecx
4902
;       xchg    ebx, ecx
4903
 
4904
        cmp     dx, 3
4905
        ja      @f
4906
        and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
4907
        mov     eax, [apm_vf]
4908
        mov     [esp + 32], eax
4909
        shr     eax, 16
4910
        mov     [esp + 28], eax
4911
        ret
4912
 
4913
@@:
4914
 
4915
        mov     esi, [master_tab+(OS_BASE shr 20)]
4916
        xchg    [master_tab], esi
4917
        push    esi
4918
        mov     edi, cr3
4919
        mov     cr3, edi                ;flush TLB
4920
 
4921
        call    pword [apm_entry]       ;call APM BIOS
4922
 
4923
        xchg    eax, [esp]
4924
        mov     [master_tab], eax
4925
        mov     eax, cr3
4926
        mov     cr3, eax
4927
        pop     eax
4928
 
4929
        mov     [esp + 4 ], edi
4930
        mov     [esp + 8], esi
4931
        mov     [esp + 20], ebx
4932
        mov     [esp + 24], edx
4933
        mov     [esp + 28], ecx
4934
        mov     [esp + 32], eax
4935
        setc    al
4936
        and     [esp + 44], byte 0xfe
4937
        or      [esp + 44], al
4938
        ret
4939
; -----------------------------------------
4940
 
4941
align 4
4942
 
4943
undefined_syscall:                      ; Undefined system call
4944
        mov     [esp + 32], dword -1
4945
        ret
4946
 
4947
align 4
4948
system_shutdown:          ; shut down the system
4949
 
4950
        cmp     byte [BOOT_VAR+0x9030], 1
4951
        jne     @F
4952
        ret
4953
@@:
4954
        call    stop_all_services
4955
        push    3                ; stop playing cd
4956
        pop     eax
4957
        call    sys_cd_audio
4958
 
4959
yes_shutdown_param:
4960
        cli
4961
 
4962
if ~ defined extended_primary_loader
4963
        mov     eax, kernel_file ; load kernel.mnt to 0x7000:0
4964
        push    12
4965
        pop     esi
4966
        xor     ebx, ebx
4967
        or      ecx, -1
4968
        mov     edx, OS_BASE+0x70000
4969
        call    fileread
4970
 
4971
        mov     esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
4972
        mov     edi, OS_BASE+0x40000
4973
        mov     ecx, 1000
4974
        rep movsb
4975
end if
4976
 
4977
        mov     esi, BOOT_VAR    ; restore 0x0 - 0xffff
4978
        mov     edi, OS_BASE
4979
        mov     ecx, 0x10000/4
4980
        cld
4981
        rep movsd
4982
 
4983
        call    restorefatchain
4984
 
4985
        call    IRQ_mask_all
4986
 
4987
if 0
4988
        mov     word [OS_BASE+0x467+0], pr_mode_exit
4989
        mov     word [OS_BASE+0x467+2], 0x1000
4990
 
4991
        mov     al, 0x0F
4992
        out     0x70, al
4993
        mov     al, 0x05
4994
        out     0x71, al
4995
 
4996
        mov     al, 0xFE
4997
        out     0x64, al
4998
 
4999
        hlt
5000
        jmp     $-1
5001
 
5002
else
5003
        cmp     byte [OS_BASE + 0x9030], 2
5004
        jnz     no_acpi_power_off
5005
 
5006
; scan for RSDP
5007
; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
5008
        movzx   eax, word [OS_BASE + 0x40E]
5009
        shl     eax, 4
5010
        jz      @f
5011
        mov     ecx, 1024/16
5012
        call    scan_rsdp
5013
        jnc     .rsdp_found
5014
@@:
5015
; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
5016
        mov     eax, 0xE0000
5017
        mov     ecx, 0x2000
5018
        call    scan_rsdp
5019
        jc      no_acpi_power_off
5020
.rsdp_found:
5021
        mov     esi, [eax+16]   ; esi contains physical address of the RSDT
5022
        mov     ebp, [ipc_tmp]
5023
        stdcall map_page, ebp, esi, PG_MAP
5024
        lea     eax, [esi+1000h]
5025
        lea     edx, [ebp+1000h]
5026
        stdcall map_page, edx, eax, PG_MAP
5027
        and     esi, 0xFFF
5028
        add     esi, ebp
5029
        cmp     dword [esi], 'RSDT'
5030
        jnz     no_acpi_power_off
5031
        mov     ecx, [esi+4]
5032
        sub     ecx, 24h
5033
        jbe     no_acpi_power_off
5034
        shr     ecx, 2
5035
        add     esi, 24h
5036
.scan_fadt:
5037
        lodsd
5038
        mov     ebx, eax
5039
        lea     eax, [ebp+2000h]
5040
        stdcall map_page, eax, ebx, PG_MAP
5041
        lea     eax, [ebp+3000h]
5042
        add     ebx, 0x1000
5043
        stdcall map_page, eax, ebx, PG_MAP
5044
        and     ebx, 0xFFF
5045
        lea     ebx, [ebx+ebp+2000h]
5046
        cmp     dword [ebx], 'FACP'
5047
        jz      .fadt_found
5048
        loop    .scan_fadt
5049
        jmp     no_acpi_power_off
5050
.fadt_found:
5051
; ebx is linear address of FADT
5052
        mov     edi, [ebx+40] ; physical address of the DSDT
5053
        lea     eax, [ebp+4000h]
5054
        stdcall map_page, eax, edi, PG_MAP
5055
        lea     eax, [ebp+5000h]
5056
        lea     esi, [edi+0x1000]
5057
        stdcall map_page, eax, esi, PG_MAP
5058
        and     esi, 0xFFF
5059
        sub     edi, esi
5060
        cmp     dword [esi+ebp+4000h], 'DSDT'
5061
        jnz     no_acpi_power_off
5062
        mov     eax, [esi+ebp+4004h] ; DSDT length
5063
        sub     eax, 36+4
5064
        jbe     no_acpi_power_off
5065
        add     esi, 36
5066
.scan_dsdt:
5067
        cmp     dword [esi+ebp+4000h], '_S5_'
5068
        jnz     .scan_dsdt_cont
5069
        cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
5070
        jnz     .scan_dsdt_cont
5071
        mov     dl, [esi+ebp+4000h+6]
5072
        cmp     dl, 4 ; _S5_ package must contain 4 bytes
5073
                      ; ...in theory; in practice, VirtualBox has 2 bytes
5074
        ja      .scan_dsdt_cont
5075
        cmp     dl, 1
5076
        jb      .scan_dsdt_cont
5077
        lea     esi, [esi+ebp+4000h+7]
5078
        xor     ecx, ecx
5079
        cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
5080
        jz      @f
5081
        cmp     byte [esi], 0xA
5082
        jnz     no_acpi_power_off
5083
        inc     esi
5084
        mov     cl, [esi]
5085
@@:
5086
        inc     esi
5087
        cmp     dl, 2
5088
        jb      @f
5089
        cmp     byte [esi], 0
5090
        jz      @f
5091
        cmp     byte [esi], 0xA
5092
        jnz     no_acpi_power_off
5093
        inc     esi
5094
        mov     ch, [esi]
5095
@@:
5096
        jmp     do_acpi_power_off
5097
.scan_dsdt_cont:
5098
        inc     esi
5099
        cmp     esi, 0x1000
5100
        jb      @f
5101
        sub     esi, 0x1000
5102
        add     edi, 0x1000
5103
        push    eax
5104
        lea     eax, [ebp+4000h]
5105
        stdcall map_page, eax, edi, PG_MAP
5106
        push    PG_MAP
5107
        lea     eax, [edi+1000h]
5108
        push    eax
5109
        lea     eax, [ebp+5000h]
5110
        push    eax
5111
        stdcall map_page
5112
        pop     eax
5113
@@:
5114
        dec     eax
5115
        jnz     .scan_dsdt
5116
        jmp     no_acpi_power_off
5117
do_acpi_power_off:
5118
        mov     edx, [ebx+48]
5119
        test    edx, edx
5120
        jz      .nosmi
5121
        mov     al, [ebx+52]
5122
        out     dx, al
5123
        mov     edx, [ebx+64]
5124
@@:
5125
        in      ax, dx
5126
        test    al, 1
5127
        jz      @b
5128
.nosmi:
5129
        and     cx, 0x0707
5130
        shl     cx, 2
5131
        or      cx, 0x2020
5132
        mov     edx, [ebx+64]
5133
        in      ax, dx
5134
        and     ax, 203h
5135
        or      ah, cl
5136
        out     dx, ax
5137
        mov     edx, [ebx+68]
5138
        test    edx, edx
5139
        jz      @f
5140
        in      ax, dx
5141
        and     ax, 203h
5142
        or      ah, ch
5143
        out     dx, ax
5144
@@:
5145
        jmp     $
5146
 
5147
 
5148
no_acpi_power_off:
5149
        mov     word [OS_BASE+0x467+0], pr_mode_exit
5150
        mov     word [OS_BASE+0x467+2], 0x1000
5151
 
5152
        mov     al, 0x0F
5153
        out     0x70, al
5154
        mov     al, 0x05
5155
        out     0x71, al
5156
 
5157
        mov     al, 0xFE
5158
        out     0x64, al
5159
 
5160
        hlt
5161
        jmp     $-1
5162
 
5163
scan_rsdp:
5164
        add     eax, OS_BASE
5165
.s:
5166
        cmp     dword [eax], 'RSD '
5167
        jnz     .n
5168
        cmp     dword [eax+4], 'PTR '
5169
        jnz     .n
5170
        xor     edx, edx
5171
        xor     esi, esi
5172
@@:
5173
        add     dl, [eax+esi]
5174
        inc     esi
5175
        cmp     esi, 20
5176
        jnz     @b
5177
        test    dl, dl
5178
        jz      .ok
5179
.n:
5180
        add     eax, 10h
5181
        loop    .s
5182
        stc
5183
.ok:
5184
        ret
5185
end if
5186
 
5187
include "data32.inc"
5188
 
5189
__REV__ = __REV
5190
 
5191
uglobals_size = $ - endofcode
5192
diff16 "end of kernel code",0,$