Subversion Repositories Kolibri OS

Rev

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