Subversion Repositories Kolibri OS

Rev

Rev 497 | Rev 501 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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