Subversion Repositories Kolibri OS

Rev

Rev 2407 | Rev 2411 | 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: 2408 $
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
2069
 ;------------------------------------------------------------------------------
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
3082
        xor     edi, edi
3083
        mov     ecx, [TASK_COUNT]
2408 Serge 3084
        movzx   eax, word [WIN_POS + ecx*2]     ; active window
3085
        shl     eax, 8
3086
 
3087
align 4
3088
.set_mouse_event:
2288 clevermous 3089
        add     edi, 256
2408 Serge 3090
        test    [edi+SLOT_BASE+APPDATA.event_filter], 1
3091
        jz      @F
2288 clevermous 3092
 
2408 Serge 3093
        cmp     eax, edi                        ; skip if filtration active
3094
        jne     .set_mouse_event
3095
@@:
3096
        or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b
3097
        loop    .set_mouse_event
3098
 
2288 clevermous 3099
mouse_not_active:
3100
        cmp     byte[BACKGROUND_CHANGED], 0
3101
        jz      no_set_bgr_event
3102
        xor     edi, edi
3103
        mov     ecx, [TASK_COUNT]
3104
set_bgr_event:
3105
        add     edi, 256
3106
        or      [edi+SLOT_BASE+APPDATA.event_mask], 16
3107
        loop    set_bgr_event
3108
        mov     byte[BACKGROUND_CHANGED], 0
3109
no_set_bgr_event:
3110
        cmp     byte[REDRAW_BACKGROUND], 0         ; background update ?
3111
        jz      nobackgr
3112
        cmp     [background_defined], 0
3113
        jz      nobackgr
3114
;    mov   [draw_data+32 + RECT.left],dword 0
3115
;    mov   [draw_data+32 + RECT.top],dword 0
3116
;    mov   eax,[Screen_Max_X]
3117
;    mov   ebx,[Screen_Max_Y]
3118
;    mov   [draw_data+32 + RECT.right],eax
3119
;    mov   [draw_data+32 + RECT.bottom],ebx
3120
@@:
3121
        call    drawbackground
3122
        xor     eax, eax
3123
        xchg    al, [REDRAW_BACKGROUND]
3124
        test    al, al                             ; got new update request?
3125
        jnz     @b
3126
        mov     [draw_data+32 + RECT.left], eax
3127
        mov     [draw_data+32 + RECT.top], eax
3128
        mov     [draw_data+32 + RECT.right], eax
3129
        mov     [draw_data+32 + RECT.bottom], eax
3130
        mov     [MOUSE_BACKGROUND], byte 0
3131
 
3132
nobackgr:
3133
 
3134
    ; system shutdown request
3135
 
3136
        cmp     [SYS_SHUTDOWN], byte 0
3137
        je      noshutdown
3138
 
3139
        mov     edx, [shutdown_processes]
3140
 
3141
        cmp     [SYS_SHUTDOWN], dl
3142
        jne     no_mark_system_shutdown
3143
 
3144
        lea     ecx, [edx-1]
3145
        mov     edx, OS_BASE+0x3040
3146
        jecxz   @f
3147
markz:
3148
        mov     [edx+TASKDATA.state], byte 3
3149
        add     edx, 0x20
3150
        loop    markz
3151
@@:
3152
 
3153
  no_mark_system_shutdown:
3154
 
3155
        call    [_display.disable_mouse]
3156
 
3157
        dec     byte [SYS_SHUTDOWN]
3158
        je      system_shutdown
3159
 
3160
noshutdown:
3161
 
3162
 
3163
        mov     eax, [TASK_COUNT]           ; termination
3164
        mov     ebx, TASK_DATA+TASKDATA.state
3165
        mov     esi, 1
3166
 
3167
newct:
3168
        mov     cl, [ebx]
3169
        cmp     cl, byte 3
3170
        jz      terminate
3171
        cmp     cl, byte 4
3172
        jz      terminate
3173
 
3174
        add     ebx, 0x20
3175
        inc     esi
3176
        dec     eax
3177
        jnz     newct
3178
        ret
3179
 
3180
; redraw screen
3181
 
3182
redrawscreen:
3183
 
3184
; eax , if process window_data base is eax, do not set flag/limits
3185
 
3186
        pushad
3187
        push    eax
3188
 
3189
;;;         mov   ebx,2
3190
;;;         call  delay_hs
3191
 
3192
         ;mov   ecx,0               ; redraw flags for apps
3193
        xor     ecx, ecx
3194
       newdw2:
3195
 
3196
        inc     ecx
3197
        push    ecx
3198
 
3199
        mov     eax, ecx
3200
        shl     eax, 5
3201
        add     eax, window_data
3202
 
3203
        cmp     eax, [esp+4]
3204
        je      not_this_task
3205
                                   ; check if window in redraw area
3206
        mov     edi, eax
3207
 
3208
        cmp     ecx, 1             ; limit for background
3209
        jz      bgli
3210
 
3211
        mov     eax, [edi + WDATA.box.left]
3212
        mov     ebx, [edi + WDATA.box.top]
3213
        mov     ecx, [edi + WDATA.box.width]
3214
        mov     edx, [edi + WDATA.box.height]
3215
        add     ecx, eax
3216
        add     edx, ebx
3217
 
3218
        mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
3219
        cmp     ecx, ebx
3220
        jb      ricino
3221
 
3222
        mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
3223
        cmp     ecx, eax
3224
        jb      ricino
3225
 
3226
        mov     eax, [edi + WDATA.box.left]
3227
        mov     ebx, [edi + WDATA.box.top]
3228
        mov     ecx, [edi + WDATA.box.width]
3229
        mov     edx, [edi + WDATA.box.height]
3230
        add     ecx, eax
3231
        add     edx, ebx
3232
 
3233
        mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
3234
        cmp     edx, eax
3235
        jb      ricino
3236
 
3237
        mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
3238
        cmp     ecx, eax
3239
        jb      ricino
3240
 
3241
        bgli:
3242
 
3243
        cmp     dword[esp], 1
3244
        jnz     .az
3245
;         cmp   byte[BACKGROUND_CHANGED], 0
3246
;         jnz   newdw8
3247
        cmp     byte[REDRAW_BACKGROUND], 0
3248
        jz      .az
3249
        mov     dl, 0
3250
        lea     eax, [edi+draw_data-window_data]
3251
        mov     ebx, [draw_limits.left]
3252
        cmp     ebx, [eax+RECT.left]
3253
        jae     @f
3254
        mov     [eax+RECT.left], ebx
3255
        mov     dl, 1
3256
        @@:
3257
        mov     ebx, [draw_limits.top]
3258
        cmp     ebx, [eax+RECT.top]
3259
        jae     @f
3260
        mov     [eax+RECT.top], ebx
3261
        mov     dl, 1
3262
        @@:
3263
        mov     ebx, [draw_limits.right]
3264
        cmp     ebx, [eax+RECT.right]
3265
        jbe     @f
3266
        mov     [eax+RECT.right], ebx
3267
        mov     dl, 1
3268
        @@:
3269
        mov     ebx, [draw_limits.bottom]
3270
        cmp     ebx, [eax+RECT.bottom]
3271
        jbe     @f
3272
        mov     [eax+RECT.bottom], ebx
3273
        mov     dl, 1
3274
        @@:
3275
        add     byte[REDRAW_BACKGROUND], dl
3276
        jmp     newdw8
3277
        .az:
3278
 
3279
        mov     eax, edi
3280
        add     eax, draw_data-window_data
3281
 
3282
        mov     ebx, [draw_limits.left]        ; set limits
3283
        mov     [eax + RECT.left], ebx
3284
        mov     ebx, [draw_limits.top]
3285
        mov     [eax + RECT.top], ebx
3286
        mov     ebx, [draw_limits.right]
3287
        mov     [eax + RECT.right], ebx
3288
        mov     ebx, [draw_limits.bottom]
3289
        mov     [eax + RECT.bottom], ebx
3290
 
3291
        sub     eax, draw_data-window_data
3292
 
3293
        cmp     dword [esp], 1
3294
        jne     nobgrd
3295
        inc     byte[REDRAW_BACKGROUND]
3296
 
3297
       newdw8:
3298
       nobgrd:
3299
 
3300
        mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
3301
 
3302
       ricino:
3303
 
3304
       not_this_task:
3305
 
3306
        pop     ecx
3307
 
3308
        cmp     ecx, [TASK_COUNT]
3309
        jle     newdw2
3310
 
3311
        pop     eax
3312
        popad
3313
 
3314
        ret
3315
 
3316
calculatebackground:   ; background
3317
 
3318
        mov     edi, [_WinMapAddress]              ; set os to use all pixels
3319
        mov     eax, 0x01010101
3320
        mov     ecx, [_WinMapSize]
3321
        shr     ecx, 2
3322
        rep stosd
3323
 
3324
        mov     byte[REDRAW_BACKGROUND], 0            ; do not draw background!
3325
        mov     byte[BACKGROUND_CHANGED], 0
3326
 
3327
        ret
3328
 
3329
uglobal
3330
  imax    dd 0x0
3331
endg
3332
 
3333
 
3334
 
3335
delay_ms:     ; delay in 1/1000 sec
3336
 
3337
 
3338
        push    eax
3339
        push    ecx
3340
 
3341
        mov     ecx, esi
3342
        ; 
3343
        imul    ecx, 33941
3344
        shr     ecx, 9
3345
        ; 
3346
 
3347
        in      al, 0x61
3348
        and     al, 0x10
3349
        mov     ah, al
3350
        cld
3351
 
3352
 cnt1:
3353
        in      al, 0x61
3354
        and     al, 0x10
3355
        cmp     al, ah
3356
        jz      cnt1
3357
 
3358
        mov     ah, al
3359
        loop    cnt1
3360
 
3361
        pop     ecx
3362
        pop     eax
3363
 
3364
        ret
3365
 
3366
 
3367
set_app_param:
3368
        mov     edi, [TASK_BASE]
2408 Serge 3369
        mov     eax, ebx
3370
        btr     eax, 3                           ; move MOUSE_FILTRATION
3371
        mov     ebx, [current_slot]              ; bit into event_filter
3372
        setc    byte [ebx+APPDATA.event_filter]
3373
        xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
3374
        mov     [esp+32], eax                    ; return old mask value
2288 clevermous 3375
        ret
3376
 
3377
 
3378
 
3379
delay_hs:     ; delay in 1/100 secs
3380
; ebx = delay time
3381
        push    ecx
3382
        push    edx
3383
 
3384
        mov     edx, [timer_ticks]
3385
 
3386
      newtic:
3387
        mov     ecx, [timer_ticks]
3388
        sub     ecx, edx
3389
        cmp     ecx, ebx
3390
        jae     zerodelay
3391
 
3392
        call    change_task
3393
 
3394
        jmp     newtic
3395
 
3396
      zerodelay:
3397
        pop     edx
3398
        pop     ecx
3399
 
3400
        ret
3401
 
3402
align 16        ;very often call this subrutine
3403
memmove:       ; memory move in bytes
3404
 
3405
; eax = from
3406
; ebx = to
3407
; ecx = no of bytes
3408
        test    ecx, ecx
3409
        jle     .ret
3410
 
3411
        push    esi edi ecx
3412
 
3413
        mov     edi, ebx
3414
        mov     esi, eax
3415
 
3416
        test    ecx, not 11b
3417
        jz      @f
3418
 
3419
        push    ecx
3420
        shr     ecx, 2
3421
        rep movsd
3422
        pop     ecx
3423
        and     ecx, 11b
3424
        jz      .finish
3425
  @@:
3426
        rep movsb
3427
 
3428
  .finish:
3429
        pop     ecx edi esi
3430
  .ret:
3431
        ret
3432
 
3433
 
3434
;  Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
3435
;align 4
3436
;
3437
;read_floppy_file:
3438
;
3439
;; as input
3440
;;
3441
;; eax pointer to file
3442
;; ebx file lenght
3443
;; ecx start 512 byte block number
3444
;; edx number of blocks to read
3445
;; esi pointer to return/work area (atleast 20 000 bytes)
3446
;;
3447
;;
3448
;; on return
3449
;;
3450
;; eax = 0 command succesful
3451
;;       1 no fd base and/or partition defined
3452
;;       2 yet unsupported FS
3453
;;       3 unknown FS
3454
;;       4 partition not defined at hd
3455
;;       5 file not found
3456
;; ebx = size of file
3457
;
3458
;     mov   edi,[TASK_BASE]
3459
;     add   edi,0x10
3460
;     add   esi,[edi]
3461
;     add   eax,[edi]
3462
;
3463
;     pushad
3464
;     mov  edi,esi
3465
;     add  edi,1024
3466
;     mov  esi,0x100000+19*512
3467
;     sub  ecx,1
3468
;     shl  ecx,9
3469
;     add  esi,ecx
3470
;     shl  edx,9
3471
;     mov  ecx,edx
3472
;     cld
3473
;     rep  movsb
3474
;     popad
3475
;
3476
;     mov   [esp+36],eax
3477
;     mov   [esp+24],ebx
3478
;     ret
3479
 
3480
 
3481
 
3482
align 4
3483
set_io_access_rights:
3484
        push    edi eax
3485
        mov     edi, tss._io_map_0
3486
;     mov   ecx,eax
3487
;     and   ecx,7    ; offset in byte
3488
;     shr   eax,3    ; number of byte
3489
;     add   edi,eax
3490
;     mov   ebx,1
3491
;     shl   ebx,cl
3492
        test    ebp, ebp
3493
;     cmp   ebp,0                ; enable access - ebp = 0
3494
        jnz     .siar1
3495
;     not   ebx
3496
;     and   [edi],byte bl
3497
        btr     [edi], eax
3498
        pop     eax edi
3499
        ret
3500
.siar1:
3501
        bts     [edi], eax
3502
  ;  or    [edi],byte bl        ; disable access - ebp = 1
3503
        pop     eax edi
3504
        ret
3505
;reserve/free group of ports
3506
;  * eax = 46 - number function
3507
;  * ebx = 0 - reserve, 1 - free
3508
;  * ecx = number start arrea of ports
3509
;  * edx = number end arrea of ports (include last number of port)
3510
;Return value:
3511
;  * eax = 0 - succesful
3512
;  * eax = 1 - error
3513
;  * The system has reserve this ports:
3514
;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
3515
;destroys eax,ebx, ebp
3516
r_f_port_area:
3517
 
3518
        test    ebx, ebx
3519
        jnz     free_port_area
3520
;     je    r_port_area
3521
;     jmp   free_port_area
3522
 
3523
;   r_port_area:
3524
 
3525
;     pushad
3526
 
3527
        cmp     ecx, edx      ; beginning > end ?
3528
        ja      rpal1
3529
        cmp     edx, 65536
3530
        jae     rpal1
3531
        mov     eax, [RESERVED_PORTS]
3532
        test    eax, eax      ; no reserved areas ?
3533
        je      rpal2
3534
        cmp     eax, 255      ; max reserved
3535
        jae     rpal1
3536
 rpal3:
3537
        mov     ebx, eax
3538
        shl     ebx, 4
3539
        add     ebx, RESERVED_PORTS
3540
        cmp     ecx, [ebx+8]
3541
        ja      rpal4
3542
        cmp     edx, [ebx+4]
3543
        jae     rpal1
3544
;     jb    rpal4
3545
;     jmp   rpal1
3546
 rpal4:
3547
        dec     eax
3548
        jnz     rpal3
3549
        jmp     rpal2
3550
   rpal1:
3551
;     popad
3552
;     mov   eax,1
3553
        xor     eax, eax
3554
        inc     eax
3555
        ret
3556
   rpal2:
3557
;     popad
3558
     ; enable port access at port IO map
3559
        cli
3560
        pushad                        ; start enable io map
3561
 
3562
        cmp     edx, 65536;16384
3563
        jae     no_unmask_io; jge
3564
        mov     eax, ecx
3565
;       push    ebp
3566
        xor     ebp, ebp               ; enable - eax = port
3567
new_port_access:
3568
;     pushad
3569
        call    set_io_access_rights
3570
;     popad
3571
        inc     eax
3572
        cmp     eax, edx
3573
        jbe     new_port_access
3574
;       pop     ebp
3575
no_unmask_io:
3576
        popad                         ; end enable io map
3577
        sti
3578
 
3579
        mov     eax, [RESERVED_PORTS]
3580
        add     eax, 1
3581
        mov     [RESERVED_PORTS], eax
3582
        shl     eax, 4
3583
        add     eax, RESERVED_PORTS
3584
        mov     ebx, [TASK_BASE]
3585
        mov     ebx, [ebx+TASKDATA.pid]
3586
        mov     [eax], ebx
3587
        mov     [eax+4], ecx
3588
        mov     [eax+8], edx
3589
 
3590
        xor     eax, eax
3591
        ret
3592
 
3593
free_port_area:
3594
 
3595
;     pushad
3596
        mov     eax, [RESERVED_PORTS]; no reserved areas ?
3597
        test    eax, eax
3598
        jz      frpal2
3599
        mov     ebx, [TASK_BASE]
3600
        mov     ebx, [ebx+TASKDATA.pid]
3601
   frpal3:
3602
        mov     edi, eax
3603
        shl     edi, 4
3604
        add     edi, RESERVED_PORTS
3605
        cmp     ebx, [edi]
3606
        jne     frpal4
3607
        cmp     ecx, [edi+4]
3608
        jne     frpal4
3609
        cmp     edx, [edi+8]
3610
        jne     frpal4
3611
        jmp     frpal1
3612
   frpal4:
3613
        dec     eax
3614
        jnz     frpal3
3615
   frpal2:
3616
;     popad
3617
        inc     eax
3618
        ret
3619
   frpal1:
3620
        push    ecx
3621
        mov     ecx, 256
3622
        sub     ecx, eax
3623
        shl     ecx, 4
3624
        mov     esi, edi
3625
        add     esi, 16
3626
        cld
3627
        rep movsb
3628
 
3629
        dec     dword [RESERVED_PORTS]
3630
;popad
3631
;disable port access at port IO map
3632
 
3633
;     pushad                        ; start disable io map
3634
        pop     eax     ;start port
3635
        cmp     edx, 65536;16384
3636
        jge     no_mask_io
3637
 
3638
;     mov   eax,ecx
3639
        xor     ebp, ebp
3640
        inc     ebp
3641
new_port_access_disable:
3642
;     pushad
3643
;     mov   ebp,1                  ; disable - eax = port
3644
        call    set_io_access_rights
3645
;     popad
3646
        inc     eax
3647
        cmp     eax, edx
3648
        jbe     new_port_access_disable
3649
no_mask_io:
3650
;     popad                         ; end disable io map
3651
        xor     eax, eax
3652
        ret
3653
 
3654
 
3655
align 4
3656
drawbackground:
3657
        inc     [mouse_pause]
2407 mario79 3658
;        cmp     [SCR_MODE], word 0x12
3659
;        je      dbrv20
3660
;     dbrv12:
3661
;        cmp     [SCR_MODE], word 0100000000000000b
3662
;        jge     dbrv20
3663
;        cmp     [SCR_MODE], word 0x13
3664
;        je      dbrv20
3665
;        call    vesa12_drawbackground
3666
;        dec     [mouse_pause]
3667
;        call    [draw_pointer]
3668
;        ret
2288 clevermous 3669
     dbrv20:
3670
        cmp     [BgrDrawMode], dword 1
3671
        jne     bgrstr
3672
        call    vesa20_drawbackground_tiled
3673
        dec     [mouse_pause]
3674
        call    [draw_pointer]
3675
        ret
3676
     bgrstr:
3677
        call    vesa20_drawbackground_stretch
3678
        dec     [mouse_pause]
3679
        call    [draw_pointer]
3680
        ret
3681
 
3682
align 4
3683
 
3684
syscall_putimage:                       ; PutImage
3685
sys_putimage:
3686
        test    ecx, 0x80008000
3687
        jnz     .exit
3688
        test    ecx, 0x0000FFFF
3689
        jz      .exit
3690
        test    ecx, 0xFFFF0000
3691
        jnz     @f
3692
  .exit:
3693
        ret
3694
 @@:
3695
        mov     edi, [current_slot]
3696
        add     dx, word[edi+APPDATA.wnd_clientbox.top]
3697
        rol     edx, 16
3698
        add     dx, word[edi+APPDATA.wnd_clientbox.left]
3699
        rol     edx, 16
3700
  .forced:
3701
        push    ebp esi 0
3702
        mov     ebp, putimage_get24bpp
3703
        mov     esi, putimage_init24bpp
3704
sys_putimage_bpp:
3705
;        call    [disable_mouse] ; this will be done in xxx_putimage
3706
;        mov     eax, vga_putimage
2407 mario79 3707
;        cmp     [SCR_MODE], word 0x12
3708
;        jz      @f   ;.doit
3709
;        mov     eax, vesa12_putimage
3710
;        cmp     [SCR_MODE], word 0100000000000000b
3711
;        jae     @f
3712
;        cmp     [SCR_MODE], word 0x13
3713
;        jnz     .doit
3714
;@@:
2288 clevermous 3715
        mov     eax, vesa20_putimage
3716
.doit:
3717
        inc     [mouse_pause]
3718
        call    eax
3719
        dec     [mouse_pause]
3720
        pop     ebp esi ebp
3721
        jmp     [draw_pointer]
3722
align 4
3723
sys_putimage_palette:
3724
; ebx = pointer to image
3725
; ecx = [xsize]*65536 + [ysize]
3726
; edx = [xstart]*65536 + [ystart]
3727
; esi = number of bits per pixel, must be 8, 24 or 32
3728
; edi = pointer to palette
3729
; ebp = row delta
3730
        mov     eax, [CURRENT_TASK]
3731
        shl     eax, 8
3732
        add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
3733
        rol     edx, 16
3734
        add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
3735
        rol     edx, 16
3736
.forced:
3737
        cmp     esi, 1
3738
        jnz     @f
3739
        push    edi
3740
        mov     eax, [edi+4]
3741
        sub     eax, [edi]
3742
        push    eax
3743
        push    dword [edi]
3744
        push    0ffffff80h
3745
        mov     edi, esp
3746
        call    put_mono_image
3747
        add     esp, 12
3748
        pop     edi
3749
        ret
3750
@@:
3751
        cmp     esi, 2
3752
        jnz     @f
3753
        push    edi
3754
        push    0ffffff80h
3755
        mov     edi, esp
3756
        call    put_2bit_image
3757
        pop     eax
3758
        pop     edi
3759
        ret
3760
@@:
3761
        cmp     esi, 4
3762
        jnz     @f
3763
        push    edi
3764
        push    0ffffff80h
3765
        mov     edi, esp
3766
        call    put_4bit_image
3767
        pop     eax
3768
        pop     edi
3769
        ret
3770
@@:
3771
        push    ebp esi ebp
3772
        cmp     esi, 8
3773
        jnz     @f
3774
        mov     ebp, putimage_get8bpp
3775
        mov     esi, putimage_init8bpp
3776
        jmp     sys_putimage_bpp
3777
@@:
3778
        cmp     esi, 15
3779
        jnz     @f
3780
        mov     ebp, putimage_get15bpp
3781
        mov     esi, putimage_init15bpp
3782
        jmp     sys_putimage_bpp
3783
@@:
3784
        cmp     esi, 16
3785
        jnz     @f
3786
        mov     ebp, putimage_get16bpp
3787
        mov     esi, putimage_init16bpp
3788
        jmp     sys_putimage_bpp
3789
@@:
3790
        cmp     esi, 24
3791
        jnz     @f
3792
        mov     ebp, putimage_get24bpp
3793
        mov     esi, putimage_init24bpp
3794
        jmp     sys_putimage_bpp
3795
@@:
3796
        cmp     esi, 32
3797
        jnz     @f
3798
        mov     ebp, putimage_get32bpp
3799
        mov     esi, putimage_init32bpp
3800
        jmp     sys_putimage_bpp
3801
@@:
3802
        pop     ebp esi ebp
3803
        ret
3804
 
3805
put_mono_image:
3806
        push    ebp esi ebp
3807
        mov     ebp, putimage_get1bpp
3808
        mov     esi, putimage_init1bpp
3809
        jmp     sys_putimage_bpp
3810
put_2bit_image:
3811
        push    ebp esi ebp
3812
        mov     ebp, putimage_get2bpp
3813
        mov     esi, putimage_init2bpp
3814
        jmp     sys_putimage_bpp
3815
put_4bit_image:
3816
        push    ebp esi ebp
3817
        mov     ebp, putimage_get4bpp
3818
        mov     esi, putimage_init4bpp
3819
        jmp     sys_putimage_bpp
3820
 
3821
putimage_init24bpp:
3822
        lea     eax, [eax*3]
3823
putimage_init8bpp:
3824
        ret
3825
 
3826
align 16
3827
putimage_get24bpp:
3828
        movzx   eax, byte [esi+2]
3829
        shl     eax, 16
3830
        mov     ax, [esi]
3831
        add     esi, 3
3832
        ret     4
3833
align 16
3834
putimage_get8bpp:
3835
        movzx   eax, byte [esi]
3836
        push    edx
3837
        mov     edx, [esp+8]
3838
        mov     eax, [edx+eax*4]
3839
        pop     edx
3840
        inc     esi
3841
        ret     4
3842
 
3843
putimage_init1bpp:
3844
        add     eax, ecx
3845
        push    ecx
3846
        add     eax, 7
3847
        add     ecx, 7
3848
        shr     eax, 3
3849
        shr     ecx, 3
3850
        sub     eax, ecx
3851
        pop     ecx
3852
        ret
3853
align 16
3854
putimage_get1bpp:
3855
        push    edx
3856
        mov     edx, [esp+8]
3857
        mov     al, [edx]
3858
        add     al, al
3859
        jnz     @f
3860
        lodsb
3861
        adc     al, al
3862
@@:
3863
        mov     [edx], al
3864
        sbb     eax, eax
3865
        and     eax, [edx+8]
3866
        add     eax, [edx+4]
3867
        pop     edx
3868
        ret     4
3869
 
3870
putimage_init2bpp:
3871
        add     eax, ecx
3872
        push    ecx
3873
        add     ecx, 3
3874
        add     eax, 3
3875
        shr     ecx, 2
3876
        shr     eax, 2
3877
        sub     eax, ecx
3878
        pop     ecx
3879
        ret
3880
align 16
3881
putimage_get2bpp:
3882
        push    edx
3883
        mov     edx, [esp+8]
3884
        mov     al, [edx]
3885
        mov     ah, al
3886
        shr     al, 6
3887
        shl     ah, 2
3888
        jnz     .nonewbyte
3889
        lodsb
3890
        mov     ah, al
3891
        shr     al, 6
3892
        shl     ah, 2
3893
        add     ah, 1
3894
.nonewbyte:
3895
        mov     [edx], ah
3896
        mov     edx, [edx+4]
3897
        movzx   eax, al
3898
        mov     eax, [edx+eax*4]
3899
        pop     edx
3900
        ret     4
3901
 
3902
putimage_init4bpp:
3903
        add     eax, ecx
3904
        push    ecx
3905
        add     ecx, 1
3906
        add     eax, 1
3907
        shr     ecx, 1
3908
        shr     eax, 1
3909
        sub     eax, ecx
3910
        pop     ecx
3911
        ret
3912
align 16
3913
putimage_get4bpp:
3914
        push    edx
3915
        mov     edx, [esp+8]
3916
        add     byte [edx], 80h
3917
        jc      @f
3918
        movzx   eax, byte [edx+1]
3919
        mov     edx, [edx+4]
3920
        and     eax, 0x0F
3921
        mov     eax, [edx+eax*4]
3922
        pop     edx
3923
        ret     4
3924
@@:
3925
        movzx   eax, byte [esi]
3926
        add     esi, 1
3927
        mov     [edx+1], al
3928
        shr     eax, 4
3929
        mov     edx, [edx+4]
3930
        mov     eax, [edx+eax*4]
3931
        pop     edx
3932
        ret     4
3933
 
3934
putimage_init32bpp:
3935
        shl     eax, 2
3936
        ret
3937
align 16
3938
putimage_get32bpp:
3939
        lodsd
3940
        ret     4
3941
 
3942
putimage_init15bpp:
3943
putimage_init16bpp:
3944
        add     eax, eax
3945
        ret
3946
align 16
3947
putimage_get15bpp:
3948
; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
3949
        push    ecx edx
3950
        movzx   eax, word [esi]
3951
        add     esi, 2
3952
        mov     ecx, eax
3953
        mov     edx, eax
3954
        and     eax, 0x1F
3955
        and     ecx, 0x1F shl 5
3956
        and     edx, 0x1F shl 10
3957
        shl     eax, 3
3958
        shl     ecx, 6
3959
        shl     edx, 9
3960
        or      eax, ecx
3961
        or      eax, edx
3962
        pop     edx ecx
3963
        ret     4
3964
 
3965
align 16
3966
putimage_get16bpp:
3967
; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
3968
        push    ecx edx
3969
        movzx   eax, word [esi]
3970
        add     esi, 2
3971
        mov     ecx, eax
3972
        mov     edx, eax
3973
        and     eax, 0x1F
3974
        and     ecx, 0x3F shl 5
3975
        and     edx, 0x1F shl 11
3976
        shl     eax, 3
3977
        shl     ecx, 5
3978
        shl     edx, 8
3979
        or      eax, ecx
3980
        or      eax, edx
3981
        pop     edx ecx
3982
        ret     4
3983
 
3984
; eax x beginning
3985
; ebx y beginning
3986
; ecx x end
3987
        ; edx y end
3988
; edi color
3989
 
3990
__sys_drawbar:
3991
        mov     esi, [current_slot]
3992
        add     eax, [esi+APPDATA.wnd_clientbox.left]
3993
        add     ecx, [esi+APPDATA.wnd_clientbox.left]
3994
        add     ebx, [esi+APPDATA.wnd_clientbox.top]
3995
        add     edx, [esi+APPDATA.wnd_clientbox.top]
3996
  .forced:
3997
        inc     [mouse_pause]
3998
;        call    [disable_mouse]
2407 mario79 3999
;        cmp     [SCR_MODE], word 0x12
4000
;        je      dbv20
4001
;   sdbv20:
4002
;        cmp     [SCR_MODE], word 0100000000000000b
4003
;        jge     dbv20
4004
;        cmp     [SCR_MODE], word 0x13
4005
;        je      dbv20
4006
;        call    vesa12_drawbar
4007
;        dec     [mouse_pause]
4008
;        call    [draw_pointer]
4009
;        ret
4010
;  dbv20:
2288 clevermous 4011
        call    vesa20_drawbar
4012
        dec     [mouse_pause]
4013
        call    [draw_pointer]
4014
        ret
4015
 
4016
 
4017
 
4018
kb_read:
4019
 
4020
        push    ecx edx
4021
 
4022
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4023
      kr_loop:
4024
        in      al, 0x64
4025
        test    al, 1
4026
        jnz     kr_ready
4027
        loop    kr_loop
4028
        mov     ah, 1
4029
        jmp     kr_exit
4030
      kr_ready:
4031
        push    ecx
4032
        mov     ecx, 32
4033
      kr_delay:
4034
        loop    kr_delay
4035
        pop     ecx
4036
        in      al, 0x60
4037
        xor     ah, ah
4038
      kr_exit:
4039
 
4040
        pop     edx ecx
4041
 
4042
        ret
4043
 
4044
 
4045
kb_write:
4046
 
4047
        push    ecx edx
4048
 
4049
        mov     dl, al
4050
;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
4051
;      kw_loop1:
4052
;        in      al,0x64
4053
;        test    al,0x20
4054
;        jz      kw_ok1
4055
;        loop    kw_loop1
4056
;        mov     ah,1
4057
;        jmp     kw_exit
4058
;      kw_ok1:
4059
        in      al, 0x60
4060
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4061
      kw_loop:
4062
        in      al, 0x64
4063
        test    al, 2
4064
        jz      kw_ok
4065
        loop    kw_loop
4066
        mov     ah, 1
4067
        jmp     kw_exit
4068
      kw_ok:
4069
        mov     al, dl
4070
        out     0x60, al
4071
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4072
      kw_loop3:
4073
        in      al, 0x64
4074
        test    al, 2
4075
        jz      kw_ok3
4076
        loop    kw_loop3
4077
        mov     ah, 1
4078
        jmp     kw_exit
4079
      kw_ok3:
4080
        mov     ah, 8
4081
      kw_loop4:
4082
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4083
      kw_loop5:
4084
        in      al, 0x64
4085
        test    al, 1
4086
        jnz     kw_ok4
4087
        loop    kw_loop5
4088
        dec     ah
4089
        jnz     kw_loop4
4090
      kw_ok4:
4091
        xor     ah, ah
4092
      kw_exit:
4093
 
4094
        pop     edx ecx
4095
 
4096
        ret
4097
 
4098
 
4099
kb_cmd:
4100
 
4101
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4102
      c_wait:
4103
        in      al, 0x64
4104
        test    al, 2
4105
        jz      c_send
4106
        loop    c_wait
4107
        jmp     c_error
4108
      c_send:
4109
        mov     al, bl
4110
        out     0x64, al
4111
        mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
4112
      c_accept:
4113
        in      al, 0x64
4114
        test    al, 2
4115
        jz      c_ok
4116
        loop    c_accept
4117
      c_error:
4118
        mov     ah, 1
4119
        jmp     c_exit
4120
      c_ok:
4121
        xor     ah, ah
4122
      c_exit:
4123
        ret
4124
 
4125
 
4126
setmouse:  ; set mousepicture -pointer
4127
           ; ps2 mouse enable
4128
 
4129
        mov     [MOUSE_PICTURE], dword mousepointer
4130
 
4131
        cli
4132
 
4133
        ret
4134
 
4135
if used _rdtsc
4136
_rdtsc:
4137
        bt      [cpu_caps], CAPS_TSC
4138
        jnc     ret_rdtsc
4139
        rdtsc
4140
        ret
4141
   ret_rdtsc:
4142
        mov     edx, 0xffffffff
4143
        mov     eax, 0xffffffff
4144
        ret
4145
end if
4146
 
4147
sys_msg_board_str:
4148
 
4149
        pushad
4150
   @@:
4151
        cmp     [esi], byte 0
4152
        je      @f
4153
        mov     eax, 1
4154
        movzx   ebx, byte [esi]
4155
        call    sys_msg_board
4156
        inc     esi
4157
        jmp     @b
4158
   @@:
4159
        popad
4160
        ret
4161
 
4162
sys_msg_board_byte:
4163
; in: al = byte to display
4164
; out: nothing
4165
; destroys: nothing
4166
        pushad
4167
        mov     ecx, 2
4168
        shl     eax, 24
4169
        jmp     @f
4170
 
4171
sys_msg_board_word:
4172
; in: ax = word to display
4173
; out: nothing
4174
; destroys: nothing
4175
        pushad
4176
        mov     ecx, 4
4177
        shl     eax, 16
4178
        jmp     @f
4179
 
4180
sys_msg_board_dword:
4181
; in: eax = dword to display
4182
; out: nothing
4183
; destroys: nothing
4184
        pushad
4185
        mov     ecx, 8
4186
@@:
4187
        push    ecx
4188
        rol     eax, 4
4189
        push    eax
4190
        and     al, 0xF
4191
        cmp     al, 10
4192
        sbb     al, 69h
4193
        das
4194
        mov     bl, al
4195
        xor     eax, eax
4196
        inc     eax
4197
        call    sys_msg_board
4198
        pop     eax
4199
        pop     ecx
4200
        loop    @b
4201
        popad
4202
        ret
4203
 
4204
uglobal
4205
  msg_board_data:
4206
                  times 4096 db 0
4207
  msg_board_count dd 0x0
4208
endg
4209
 
4210
sys_msg_board:
4211
 
4212
; eax=1 : write :  bl byte to write
4213
; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
4214
 
4215
        mov     ecx, [msg_board_count]
4216
        cmp     eax, 1
4217
        jne     .smbl1
4218
 
4219
if defined debug_com_base
4220
 
4221
        push    dx ax
4222
 
4223
       @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
4224
        mov     dx, debug_com_base+5
4225
        in      al, dx
4226
        test    al, 1 shl 5
4227
        jz      @r
4228
 
4229
        mov     dx, debug_com_base      ; Output the byte
4230
        mov     al, bl
4231
        out     dx, al
4232
 
4233
        pop     ax dx
4234
 
4235
end if
4236
 
4237
        mov     [msg_board_data+ecx], bl
4238
        inc     ecx
4239
        and     ecx, 4095
4240
        mov     [msg_board_count], ecx
4241
        mov     [check_idle_semaphore], 5
4242
        ret
4243
.smbl1:
4244
        cmp     eax, 2
4245
        jne     .smbl2
4246
        test    ecx, ecx
4247
        jz      .smbl21
4248
        mov     eax, msg_board_data+1
4249
        mov     ebx, msg_board_data
4250
        movzx   edx, byte [ebx]
4251
        call    memmove
4252
        dec     [msg_board_count]
4253
        mov     [esp + 36], edx ;eax
4254
        mov     [esp + 24], dword 1
4255
        ret
4256
.smbl21:
4257
        mov     [esp+36], ecx
4258
        mov     [esp+24], ecx
4259
.smbl2:
4260
        ret
4261
 
4262
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4263
;; 66 sys function.                                                ;;
4264
;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
4265
;; out eax                                                         ;;
4266
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4267
iglobal
4268
align 4
4269
f66call:
4270
           dd sys_process_def.1   ; 1 = set keyboard mode
4271
           dd sys_process_def.2   ; 2 = get keyboard mode
4272
           dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
4273
           dd sys_process_def.4
4274
           dd sys_process_def.5
4275
endg
4276
 
4277
 
4278
 
4279
 
4280
sys_process_def:
4281
        dec     ebx
4282
        cmp     ebx, 5
4283
        jae     .not_support    ;if >=6 then or eax,-1
4284
 
4285
        mov     edi, [CURRENT_TASK]
4286
        jmp     dword [f66call+ebx*4]
4287
 
4288
.not_support:
4289
        or      eax, -1
4290
        ret
4291
 
4292
.1:
4293
        shl     edi, 8
4294
        mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
4295
 
4296
        ret
4297
 
4298
.2:                             ; 2 = get keyboard mode
4299
        shl     edi, 8
4300
        movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
4301
        mov     [esp+32], eax
4302
        ret
4303
;     xor   eax,eax
4304
;     movzx eax,byte [shift]
4305
;     movzx ebx,byte [ctrl]
4306
;     shl   ebx,2
4307
;     add   eax,ebx
4308
;     movzx ebx,byte [alt]
4309
;     shl   ebx,3
4310
;     add   eax,ebx
4311
.3:                             ;3 = get keyboard ctrl, alt, shift
4312
 ;// mike.dld [
4313
        mov     eax, [kb_state]
4314
 ;// mike.dld ]
4315
        mov     [esp+32], eax
4316
        ret
4317
 
4318
.4:
4319
        mov     eax, hotkey_list
4320
@@:
4321
        cmp     dword [eax+8], 0
4322
        jz      .found_free
4323
        add     eax, 16
4324
        cmp     eax, hotkey_list+16*256
4325
        jb      @b
4326
        mov     dword [esp+32], 1
4327
        ret
4328
.found_free:
4329
        mov     [eax+8], edi
4330
        mov     [eax+4], edx
4331
        movzx   ecx, cl
4332
        lea     ecx, [hotkey_scancodes+ecx*4]
4333
        mov     edx, [ecx]
4334
        mov     [eax], edx
4335
        mov     [ecx], eax
4336
        mov     [eax+12], ecx
4337
        jecxz   @f
4338
        mov     [edx+12], eax
4339
@@:
4340
        and     dword [esp+32], 0
4341
        ret
4342
 
4343
.5:
4344
        movzx   ebx, cl
4345
        lea     ebx, [hotkey_scancodes+ebx*4]
4346
        mov     eax, [ebx]
4347
.scan:
4348
        test    eax, eax
4349
        jz      .notfound
4350
        cmp     [eax+8], edi
4351
        jnz     .next
4352
        cmp     [eax+4], edx
4353
        jz      .found
4354
.next:
4355
        mov     eax, [eax]
4356
        jmp     .scan
4357
.notfound:
4358
        mov     dword [esp+32], 1
4359
        ret
4360
.found:
4361
        mov     ecx, [eax]
4362
        jecxz   @f
4363
        mov     edx, [eax+12]
4364
        mov     [ecx+12], edx
4365
@@:
4366
        mov     ecx, [eax+12]
4367
        mov     edx, [eax]
4368
        mov     [ecx], edx
4369
        xor     edx, edx
4370
        mov     [eax+4], edx
4371
        mov     [eax+8], edx
4372
        mov     [eax+12], edx
4373
        mov     [eax], edx
4374
        mov     [esp+32], edx
4375
        ret
4376
 
4377
 
4378
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4379
;; 61 sys function.                                                ;;
4380
;; in eax=61,ebx in [1..3]                                         ;;
4381
;; out eax                                                         ;;
4382
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4383
iglobal
4384
align 4
4385
f61call:
4386
           dd sys_gs.1   ; resolution
4387
           dd sys_gs.2   ; bits per pixel
4388
           dd sys_gs.3   ; bytes per scanline
4389
endg
4390
 
4391
 
4392
align 4
4393
 
4394
sys_gs:                         ; direct screen access
4395
        dec     ebx
4396
        cmp     ebx, 2
4397
        ja      .not_support
4398
        jmp     dword [f61call+ebx*4]
4399
.not_support:
4400
        or      [esp+32], dword -1
4401
        ret
4402
 
4403
 
4404
.1:                             ; resolution
4405
        mov     eax, [Screen_Max_X]
4406
        shl     eax, 16
4407
        mov     ax, [Screen_Max_Y]
4408
        add     eax, 0x00010001
4409
        mov     [esp+32], eax
4410
        ret
4411
.2:                             ; bits per pixel
4412
        movzx   eax, byte [ScreenBPP]
4413
        mov     [esp+32], eax
4414
        ret
4415
.3:                             ; bytes per scanline
4416
        mov     eax, [BytesPerScanLine]
4417
        mov     [esp+32], eax
4418
        ret
4419
 
4420
align 4  ;  system functions
4421
 
4422
syscall_setpixel:                       ; SetPixel
4423
 
4424
        mov     eax, ebx
4425
        mov     ebx, ecx
4426
        mov     ecx, edx
4427
        mov     edx, [TASK_BASE]
4428
        add     eax, [edx-twdw+WDATA.box.left]
4429
        add     ebx, [edx-twdw+WDATA.box.top]
4430
        mov     edi, [current_slot]
4431
        add     eax, [edi+APPDATA.wnd_clientbox.left]
4432
        add     ebx, [edi+APPDATA.wnd_clientbox.top]
4433
        xor     edi, edi ; no force
4434
;       mov     edi, 1
4435
        call    [_display.disable_mouse]
4436
        jmp     [putpixel]
4437
 
4438
align 4
4439
 
4440
syscall_writetext:                      ; WriteText
4441
 
4442
        mov     eax, [TASK_BASE]
4443
        mov     ebp, [eax-twdw+WDATA.box.left]
4444
        push    esi
4445
        mov     esi, [current_slot]
4446
        add     ebp, [esi+APPDATA.wnd_clientbox.left]
4447
        shl     ebp, 16
4448
        add     ebp, [eax-twdw+WDATA.box.top]
4449
        add     bp, word[esi+APPDATA.wnd_clientbox.top]
4450
        pop     esi
4451
        add     ebx, ebp
4452
        mov     eax, edi
4453
        xor     edi, edi
4454
        jmp     dtext
4455
 
4456
align 4
4457
 
4458
syscall_openramdiskfile:                ; OpenRamdiskFile
4459
 
4460
        mov     eax, ebx
4461
        mov     ebx, ecx
4462
        mov     ecx, edx
4463
        mov     edx, esi
4464
        mov     esi, 12
4465
        call    fileread
4466
        mov     [esp+32], eax
4467
        ret
4468
 
4469
align 4
4470
 
4471
syscall_drawrect:                       ; DrawRect
4472
 
4473
        mov     edi, edx ; color + gradient
4474
        and     edi, 0x80FFFFFF
4475
        test    bx, bx  ; x.size
4476
        je      .drectr
4477
        test    cx, cx ; y.size
4478
        je      .drectr
4479
 
4480
        mov     eax, ebx ; bad idea
4481
        mov     ebx, ecx
4482
 
4483
        movzx   ecx, ax ; ecx - x.size
4484
        shr     eax, 16 ; eax - x.coord
4485
        movzx   edx, bx ; edx - y.size
4486
        shr     ebx, 16 ; ebx - y.coord
4487
        mov     esi, [current_slot]
4488
 
4489
        add     eax, [esi + APPDATA.wnd_clientbox.left]
4490
        add     ebx, [esi + APPDATA.wnd_clientbox.top]
4491
        add     ecx, eax
4492
        add     edx, ebx
4493
        jmp     [drawbar]
4494
.drectr:
4495
        ret
4496
 
4497
align 4
4498
syscall_getscreensize:                  ; GetScreenSize
4499
        mov     ax, [Screen_Max_X]
4500
        shl     eax, 16
4501
        mov     ax, [Screen_Max_Y]
4502
        mov     [esp + 32], eax
4503
        ret
4504
 
4505
align 4
4506
 
4507
syscall_cdaudio:                        ; CD
4508
 
4509
        cmp     ebx, 4
4510
        jb      .audio
4511
        jz      .eject
4512
        cmp     ebx, 5
4513
        jnz     .ret
4514
.load:
4515
        call    .reserve
4516
        call    LoadMedium
4517
        ;call    .free
4518
        jmp     .free
4519
;        ret
4520
.eject:
4521
        call    .reserve
4522
        call    clear_CD_cache
4523
        call    allow_medium_removal
4524
        call    EjectMedium
4525
;        call    .free
4526
        jmp     .free
4527
;        ret
4528
.audio:
4529
        call    sys_cd_audio
4530
        mov     [esp+36-4], eax
4531
.ret:
4532
        ret
4533
 
4534
.reserve:
4535
        call    reserve_cd
4536
        mov     eax, ecx
4537
        shr     eax, 1
4538
        and     eax, 1
4539
        inc     eax
4540
        mov     [ChannelNumber], ax
4541
        mov     eax, ecx
4542
        and     eax, 1
4543
        mov     [DiskNumber], al
4544
        call    reserve_cd_channel
4545
        and     ebx, 3
4546
        inc     ebx
4547
        mov     [cdpos], ebx
4548
        add     ebx, ebx
4549
        mov     cl, 8
4550
        sub     cl, bl
4551
        mov     al, [DRIVE_DATA+1]
4552
        shr     al, cl
4553
        test    al, 2
4554
        jz      .free;.err
4555
        ret
4556
.free:
4557
        call    free_cd_channel
4558
        and     [cd_status], 0
4559
        ret
4560
.err:
4561
        call    .free
4562
;        pop     eax
4563
        ret
4564
 
4565
align 4
4566
 
4567
syscall_getpixel:                       ; GetPixel
4568
        mov     ecx, [Screen_Max_X]
4569
        inc     ecx
4570
        xor     edx, edx
4571
        mov     eax, ebx
4572
        div     ecx
4573
        mov     ebx, edx
4574
        xchg    eax, ebx
4575
        call    dword [GETPIXEL]; eax - x, ebx - y
4576
        mov     [esp + 32], ecx
4577
        ret
4578
 
4579
align 4
4580
 
4581
syscall_getarea:
4582
;eax = 36
4583
;ebx = pointer to bufer for img BBGGRRBBGGRR...
4584
;ecx = [size x]*65536 + [size y]
4585
;edx = [start x]*65536 + [start y]
4586
        pushad
4587
        inc     [mouse_pause]
4588
; Check of use of the hardware cursor.
4589
        cmp     [_display.disable_mouse], __sys_disable_mouse
4590
        jne     @f
4591
; Since the test for the coordinates of the mouse should not be used,
4592
; then use the call [disable_mouse] is not possible!
4593
        cmp     dword [MOUSE_VISIBLE], dword 0
4594
        jne     @f
4595
        pushf
4596
        cli
4597
        call    draw_mouse_under
4598
        popf
4599
        mov     [MOUSE_VISIBLE], dword 1
4600
@@:
4601
        mov     edi, ebx
4602
        mov     eax, edx
4603
        shr     eax, 16
4604
        mov     ebx, edx
4605
        and     ebx, 0xffff
4606
        dec     eax
4607
        dec     ebx
4608
     ; eax - x, ebx - y
4609
        mov     edx, ecx
4610
 
4611
        shr     ecx, 16
4612
        and     edx, 0xffff
4613
        mov     esi, ecx
4614
     ; ecx - size x, edx - size y
4615
 
4616
        mov     ebp, edx
4617
        dec     ebp
4618
        lea     ebp, [ebp*3]
4619
 
4620
        imul    ebp, esi
4621
 
4622
        mov     esi, ecx
4623
        dec     esi
4624
        lea     esi, [esi*3]
4625
 
4626
        add     ebp, esi
4627
        add     ebp, edi
4628
 
4629
        add     ebx, edx
4630
 
4631
.start_y:
4632
        push    ecx edx
4633
.start_x:
4634
        push    eax ebx ecx
4635
        add     eax, ecx
4636
 
4637
        call    dword [GETPIXEL]; eax - x, ebx - y
4638
 
4639
        mov     [ebp], cx
4640
        shr     ecx, 16
4641
        mov     [ebp+2], cl
4642
 
4643
        pop     ecx ebx eax
4644
        sub     ebp, 3
4645
        dec     ecx
4646
        jnz     .start_x
4647
        pop     edx ecx
4648
        dec     ebx
4649
        dec     edx
4650
        jnz     .start_y
4651
        dec     [mouse_pause]
4652
; Check of use of the hardware cursor.
4653
        cmp     [_display.disable_mouse], __sys_disable_mouse
4654
        jne     @f
4655
        call    [draw_pointer]
4656
@@:
4657
        popad
4658
        ret
4659
 
4660
align 4
4661
 
4662
syscall_drawline:                       ; DrawLine
4663
 
4664
        mov     edi, [TASK_BASE]
4665
        movzx   eax, word[edi-twdw+WDATA.box.left]
4666
        mov     ebp, eax
4667
        mov     esi, [current_slot]
4668
        add     ebp, [esi+APPDATA.wnd_clientbox.left]
4669
        add     ax, word[esi+APPDATA.wnd_clientbox.left]
4670
        add     ebp, ebx
4671
        shl     eax, 16
4672
        movzx   ebx, word[edi-twdw+WDATA.box.top]
4673
        add     eax, ebp
4674
        mov     ebp, ebx
4675
        add     ebp, [esi+APPDATA.wnd_clientbox.top]
4676
        add     bx, word[esi+APPDATA.wnd_clientbox.top]
4677
        add     ebp, ecx
4678
        shl     ebx, 16
4679
        xor     edi, edi
4680
        add     ebx, ebp
4681
        mov     ecx, edx
4682
        jmp     [draw_line]
4683
 
4684
 
4685
 
4686
align 4
4687
syscall_reserveportarea:                ; ReservePortArea and FreePortArea
4688
 
4689
        call    r_f_port_area
4690
        mov     [esp+32], eax
4691
        ret
4692
 
4693
align 4
4694
syscall_threads:                        ; CreateThreads
4695
; eax=1 create thread
4696
;
4697
;   ebx=thread start
4698
;   ecx=thread stack value
4699
;
4700
; on return : eax = pid
4701
 
4702
        call    new_sys_threads
4703
 
4704
        mov     [esp+32], eax
4705
        ret
4706
 
4707
align 4
4708
 
4709
stack_driver_stat:
4710
 
4711
        call    app_stack_handler       ; Stack status
4712
 
4713
;     mov   [check_idle_semaphore],5    ; enable these for zero delay
4714
;     call  change_task                 ; between sent packet
4715
 
4716
        mov     [esp+32], eax
4717
        ret
4718
 
4719
align 4
4720
 
4721
socket:                                 ; Socket interface
4722
        call    app_socket_handler
4723
 
4724
;     mov   [check_idle_semaphore],5    ; enable these for zero delay
4725
;     call  change_task                 ; between sent packet
4726
 
4727
        mov     [esp+36], eax
4728
        mov     [esp+24], ebx
4729
        ret
4730
 
4731
align 4
4732
 
4733
read_from_hd:                           ; Read from hd - fn not in use
4734
 
4735
        mov     edi, [TASK_BASE]
4736
        add     edi, TASKDATA.mem_start
4737
        add     eax, [edi]
4738
        add     ecx, [edi]
4739
        add     edx, [edi]
4740
        call    file_read
4741
 
4742
        mov     [esp+36], eax
4743
        mov     [esp+24], ebx
4744
 
4745
        ret
4746
 
4747
paleholder:
4748
        ret
4749
 
4750
align 4
4751
set_screen:
4752
        cmp     eax, [Screen_Max_X]
4753
        jne     .set
4754
 
4755
        cmp     edx, [Screen_Max_Y]
4756
        jne     .set
4757
        ret
4758
.set:
4759
        pushfd
4760
        cli
4761
 
4762
        mov     [Screen_Max_X], eax
4763
        mov     [Screen_Max_Y], edx
4764
        mov     [BytesPerScanLine], ecx
4765
 
4766
        mov     [screen_workarea.right], eax
4767
        mov     [screen_workarea.bottom], edx
4768
 
4769
        push    ebx
4770
        push    esi
4771
        push    edi
4772
 
4773
        pushad
4774
 
4775
        stdcall kernel_free, [_WinMapAddress]
4776
 
4777
        mov     eax, [_display.width]
4778
        mul     [_display.height]
4779
        mov     [_WinMapSize], eax
4780
 
4781
        stdcall kernel_alloc, eax
4782
        mov     [_WinMapAddress], eax
4783
        test    eax, eax
4784
        jz      .epic_fail
4785
 
4786
        popad
4787
 
4788
        call    repos_windows
4789
        xor     eax, eax
4790
        xor     ebx, ebx
4791
        mov     ecx, [Screen_Max_X]
4792
        mov     edx, [Screen_Max_Y]
4793
        call    calculatescreen
4794
        pop     edi
4795
        pop     esi
4796
        pop     ebx
4797
 
4798
        popfd
4799
        ret
4800
 
4801
.epic_fail:
4802
        hlt                     ; Houston, we've had a problem
4803
 
4804
; --------------- APM ---------------------
4805
uglobal
4806
apm_entry       dp      0
4807
apm_vf          dd      0
4808
endg
4809
 
4810
align 4
4811
sys_apm:
4812
        xor     eax, eax
4813
        cmp     word [apm_vf], ax       ; Check APM BIOS enable
4814
        jne     @f
4815
        inc     eax
4816
        or      dword [esp + 44], eax   ; error
4817
        add     eax, 7
4818
        mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
4819
        ret
4820
 
4821
@@:
4822
;       xchg    eax, ecx
4823
;       xchg    ebx, ecx
4824
 
4825
        cmp     dx, 3
4826
        ja      @f
4827
        and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
4828
        mov     eax, [apm_vf]
4829
        mov     [esp + 32], eax
4830
        shr     eax, 16
4831
        mov     [esp + 28], eax
4832
        ret
4833
 
4834
@@:
4835
 
4836
        mov     esi, [master_tab+(OS_BASE shr 20)]
4837
        xchg    [master_tab], esi
4838
        push    esi
4839
        mov     edi, cr3
4840
        mov     cr3, edi                ;flush TLB
4841
 
4842
        call    pword [apm_entry]       ;call APM BIOS
4843
 
4844
        xchg    eax, [esp]
4845
        mov     [master_tab], eax
4846
        mov     eax, cr3
4847
        mov     cr3, eax
4848
        pop     eax
4849
 
4850
        mov     [esp + 4 ], edi
4851
        mov     [esp + 8], esi
4852
        mov     [esp + 20], ebx
4853
        mov     [esp + 24], edx
4854
        mov     [esp + 28], ecx
4855
        mov     [esp + 32], eax
4856
        setc    al
4857
        and     [esp + 44], byte 0xfe
4858
        or      [esp + 44], al
4859
        ret
4860
; -----------------------------------------
4861
 
4862
align 4
4863
 
4864
undefined_syscall:                      ; Undefined system call
4865
        mov     [esp + 32], dword -1
4866
        ret
4867
 
4868
align 4
4869
system_shutdown:          ; shut down the system
4870
 
4871
        cmp     byte [BOOT_VAR+0x9030], 1
4872
        jne     @F
4873
        ret
4874
@@:
4875
        call    stop_all_services
4876
        push    3                ; stop playing cd
4877
        pop     eax
4878
        call    sys_cd_audio
4879
 
4880
yes_shutdown_param:
4881
        cli
4882
 
4883
if ~ defined extended_primary_loader
4884
        mov     eax, kernel_file ; load kernel.mnt to 0x7000:0
4885
        push    12
4886
        pop     esi
4887
        xor     ebx, ebx
4888
        or      ecx, -1
4889
        mov     edx, OS_BASE+0x70000
4890
        call    fileread
4891
 
4892
        mov     esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
4893
        mov     edi, OS_BASE+0x40000
4894
        mov     ecx, 1000
4895
        rep movsb
4896
end if
4897
 
4898
        mov     esi, BOOT_VAR    ; restore 0x0 - 0xffff
4899
        mov     edi, OS_BASE
4900
        mov     ecx, 0x10000/4
4901
        cld
4902
        rep movsd
4903
 
4904
        call    restorefatchain
4905
 
4906
        call    IRQ_mask_all
4907
 
4908
if 0
4909
        mov     word [OS_BASE+0x467+0], pr_mode_exit
4910
        mov     word [OS_BASE+0x467+2], 0x1000
4911
 
4912
        mov     al, 0x0F
4913
        out     0x70, al
4914
        mov     al, 0x05
4915
        out     0x71, al
4916
 
4917
        mov     al, 0xFE
4918
        out     0x64, al
4919
 
4920
        hlt
4921
        jmp     $-1
4922
 
4923
else
4924
        cmp     byte [OS_BASE + 0x9030], 2
4925
        jnz     no_acpi_power_off
4926
 
4927
; scan for RSDP
4928
; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
4929
        movzx   eax, word [OS_BASE + 0x40E]
4930
        shl     eax, 4
4931
        jz      @f
4932
        mov     ecx, 1024/16
4933
        call    scan_rsdp
4934
        jnc     .rsdp_found
4935
@@:
4936
; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
4937
        mov     eax, 0xE0000
4938
        mov     ecx, 0x2000
4939
        call    scan_rsdp
4940
        jc      no_acpi_power_off
4941
.rsdp_found:
4942
        mov     esi, [eax+16]   ; esi contains physical address of the RSDT
4943
        mov     ebp, [ipc_tmp]
4944
        stdcall map_page, ebp, esi, PG_MAP
4945
        lea     eax, [esi+1000h]
4946
        lea     edx, [ebp+1000h]
4947
        stdcall map_page, edx, eax, PG_MAP
4948
        and     esi, 0xFFF
4949
        add     esi, ebp
4950
        cmp     dword [esi], 'RSDT'
4951
        jnz     no_acpi_power_off
4952
        mov     ecx, [esi+4]
4953
        sub     ecx, 24h
4954
        jbe     no_acpi_power_off
4955
        shr     ecx, 2
4956
        add     esi, 24h
4957
.scan_fadt:
4958
        lodsd
4959
        mov     ebx, eax
4960
        lea     eax, [ebp+2000h]
4961
        stdcall map_page, eax, ebx, PG_MAP
4962
        lea     eax, [ebp+3000h]
4963
        add     ebx, 0x1000
4964
        stdcall map_page, eax, ebx, PG_MAP
4965
        and     ebx, 0xFFF
4966
        lea     ebx, [ebx+ebp+2000h]
4967
        cmp     dword [ebx], 'FACP'
4968
        jz      .fadt_found
4969
        loop    .scan_fadt
4970
        jmp     no_acpi_power_off
4971
.fadt_found:
4972
; ebx is linear address of FADT
4973
        mov     edi, [ebx+40] ; physical address of the DSDT
4974
        lea     eax, [ebp+4000h]
4975
        stdcall map_page, eax, edi, PG_MAP
4976
        lea     eax, [ebp+5000h]
4977
        lea     esi, [edi+0x1000]
4978
        stdcall map_page, eax, esi, PG_MAP
4979
        and     esi, 0xFFF
4980
        sub     edi, esi
4981
        cmp     dword [esi+ebp+4000h], 'DSDT'
4982
        jnz     no_acpi_power_off
4983
        mov     eax, [esi+ebp+4004h] ; DSDT length
4984
        sub     eax, 36+4
4985
        jbe     no_acpi_power_off
4986
        add     esi, 36
4987
.scan_dsdt:
4988
        cmp     dword [esi+ebp+4000h], '_S5_'
4989
        jnz     .scan_dsdt_cont
4990
        cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
4991
        jnz     .scan_dsdt_cont
4992
        mov     dl, [esi+ebp+4000h+6]
4993
        cmp     dl, 4 ; _S5_ package must contain 4 bytes
4994
                      ; ...in theory; in practice, VirtualBox has 2 bytes
4995
        ja      .scan_dsdt_cont
4996
        cmp     dl, 1
4997
        jb      .scan_dsdt_cont
4998
        lea     esi, [esi+ebp+4000h+7]
4999
        xor     ecx, ecx
5000
        cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
5001
        jz      @f
5002
        cmp     byte [esi], 0xA
5003
        jnz     no_acpi_power_off
5004
        inc     esi
5005
        mov     cl, [esi]
5006
@@:
5007
        inc     esi
5008
        cmp     dl, 2
5009
        jb      @f
5010
        cmp     byte [esi], 0
5011
        jz      @f
5012
        cmp     byte [esi], 0xA
5013
        jnz     no_acpi_power_off
5014
        inc     esi
5015
        mov     ch, [esi]
5016
@@:
5017
        jmp     do_acpi_power_off
5018
.scan_dsdt_cont:
5019
        inc     esi
5020
        cmp     esi, 0x1000
5021
        jb      @f
5022
        sub     esi, 0x1000
5023
        add     edi, 0x1000
5024
        push    eax
5025
        lea     eax, [ebp+4000h]
5026
        stdcall map_page, eax, edi, PG_MAP
5027
        push    PG_MAP
5028
        lea     eax, [edi+1000h]
5029
        push    eax
5030
        lea     eax, [ebp+5000h]
5031
        push    eax
5032
        stdcall map_page
5033
        pop     eax
5034
@@:
5035
        dec     eax
5036
        jnz     .scan_dsdt
5037
        jmp     no_acpi_power_off
5038
do_acpi_power_off:
5039
        mov     edx, [ebx+48]
5040
        test    edx, edx
5041
        jz      .nosmi
5042
        mov     al, [ebx+52]
5043
        out     dx, al
5044
        mov     edx, [ebx+64]
5045
@@:
5046
        in      ax, dx
5047
        test    al, 1
5048
        jz      @b
5049
.nosmi:
5050
        and     cx, 0x0707
5051
        shl     cx, 2
5052
        or      cx, 0x2020
5053
        mov     edx, [ebx+64]
5054
        in      ax, dx
5055
        and     ax, 203h
5056
        or      ah, cl
5057
        out     dx, ax
5058
        mov     edx, [ebx+68]
5059
        test    edx, edx
5060
        jz      @f
5061
        in      ax, dx
5062
        and     ax, 203h
5063
        or      ah, ch
5064
        out     dx, ax
5065
@@:
5066
        jmp     $
5067
 
5068
 
5069
no_acpi_power_off:
5070
        mov     word [OS_BASE+0x467+0], pr_mode_exit
5071
        mov     word [OS_BASE+0x467+2], 0x1000
5072
 
5073
        mov     al, 0x0F
5074
        out     0x70, al
5075
        mov     al, 0x05
5076
        out     0x71, al
5077
 
5078
        mov     al, 0xFE
5079
        out     0x64, al
5080
 
5081
        hlt
5082
        jmp     $-1
5083
 
5084
scan_rsdp:
5085
        add     eax, OS_BASE
5086
.s:
5087
        cmp     dword [eax], 'RSD '
5088
        jnz     .n
5089
        cmp     dword [eax+4], 'PTR '
5090
        jnz     .n
5091
        xor     edx, edx
5092
        xor     esi, esi
5093
@@:
5094
        add     dl, [eax+esi]
5095
        inc     esi
5096
        cmp     esi, 20
5097
        jnz     @b
5098
        test    dl, dl
5099
        jz      .ok
5100
.n:
5101
        add     eax, 10h
5102
        loop    .s
5103
        stc
5104
.ok:
5105
        ret
5106
end if
5107
 
5108
include "data32.inc"
5109
 
5110
__REV__ = __REV
5111
 
5112
uglobals_size = $ - endofcode
5113
diff16 "end of kernel code",0,$