Subversion Repositories Kolibri OS

Rev

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

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