Subversion Repositories Kolibri OS

Rev

Rev 2118 | Rev 2129 | 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: 2122 $
  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,0x3ec00       ; 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.            mov   edi,0x40000
  240.            mov   ecx,(0x90000-0x40000)/4
  241.            rep   stosd
  242.  
  243. ; CLEAR KERNEL UNDEFINED GLOBALS
  244.            mov   edi, endofcode-OS_BASE
  245.            mov   ecx, (uglobals_size/4)+4
  246.            rep   stosd
  247.  
  248. ; SAVE & CLEAR 0-0xffff
  249.  
  250.            xor   esi, esi
  251.            mov   edi,(BOOT_VAR-OS_BASE)
  252.            mov   ecx,0x10000 / 4
  253.            rep   movsd
  254.            mov   edi,0x1000
  255.            mov   ecx,0xf000 / 4
  256.            rep   stosd
  257.  
  258.            call test_cpu
  259.            bts [cpu_caps-OS_BASE], CAPS_TSC     ;force use rdtsc
  260.  
  261.            call init_BIOS32
  262. ; MEMORY MODEL
  263.            call mem_test
  264.            call init_mem
  265.            call init_page_map
  266.  
  267. ; ENABLE PAGING
  268.  
  269.            mov eax, sys_pgdir-OS_BASE
  270.            mov cr3, eax
  271.  
  272.            mov eax,cr0
  273.            or eax,CR0_PG+CR0_WP
  274.            mov cr0,eax
  275.  
  276.            lgdt [gdts]
  277.            jmp pword os_code:high_code
  278.  
  279. align 4
  280. bios32_entry    dd ?
  281. tmp_page_tabs   dd ?
  282.  
  283. use16
  284. org $-0x10000
  285. include "boot/shutdown.inc" ; shutdown or restart
  286. org $+0x10000
  287. use32
  288.  
  289. __DEBUG__ fix 1
  290. __DEBUG_LEVEL__ fix 1
  291. include 'init.inc'
  292.  
  293. org OS_BASE+$
  294.  
  295. align 4
  296. high_code:
  297.            mov ax, os_stack
  298.            mov bx, app_data
  299.            mov cx, app_tls
  300.            mov ss, ax
  301.            add esp, OS_BASE
  302.  
  303.            mov ds, bx
  304.            mov es, bx
  305.            mov fs, cx
  306.            mov gs, bx
  307.  
  308.            bt [cpu_caps], CAPS_PGE
  309.            jnc @F
  310.  
  311.            or dword [sys_pgdir+(OS_BASE shr 20)], PG_GLOBAL
  312.  
  313.            mov ebx, cr4
  314.            or ebx, CR4_PGE
  315.            mov cr4, ebx
  316. @@:
  317.            xor eax, eax
  318.            mov dword [sys_pgdir], eax
  319.            mov dword [sys_pgdir+4], eax
  320.  
  321.            mov eax, cr3
  322.            mov cr3, eax           ; flush TLB
  323.  
  324. ; SAVE REAL MODE VARIABLES
  325.         mov     ax, [BOOT_VAR + 0x9031]
  326.         mov     [IDEContrRegsBaseAddr], ax
  327. ; --------------- APM ---------------------
  328.  
  329. ; init selectors
  330.     mov ebx,[BOOT_VAR+0x9040]                   ; offset of APM entry point
  331.     movzx eax,word [BOOT_VAR+0x9050]    ; real-mode segment base address of
  332.                                                                                 ; protected-mode 32-bit code segment
  333.     movzx ecx,word [BOOT_VAR+0x9052]    ; real-mode segment base address of
  334.                                                                                 ; protected-mode 16-bit code segment
  335.     movzx edx,word [BOOT_VAR+0x9054]    ; real-mode segment base address of
  336.                                                                                 ; protected-mode 16-bit data segment
  337.  
  338.     shl    eax, 4
  339.     mov    [dword apm_code_32 + 2], ax
  340.     shr    eax, 16
  341.     mov    [dword apm_code_32 + 4], al
  342.  
  343.     shl    ecx, 4
  344.     mov    [dword apm_code_16 + 2], cx
  345.     shr    ecx, 16
  346.     mov    [dword apm_code_16 + 4], cl
  347.  
  348.     shl    edx, 4
  349.     mov    [dword apm_data_16 + 2], dx
  350.     shr    edx, 16
  351.     mov    [dword apm_data_16 + 4], dl
  352.  
  353.     mov    dword[apm_entry], ebx
  354.     mov    word [apm_entry + 4], apm_code_32 - gdts
  355.  
  356.     mov    eax, [BOOT_VAR + 0x9044]    ; version & flags
  357.     mov    [apm_vf], eax
  358. ; -----------------------------------------
  359. ;        movzx eax,byte [BOOT_VAR+0x9010]       ; mouse port
  360. ;        mov   [0xF604],byte 1  ;al
  361.                 mov     al, [BOOT_VAR+0x901F]           ; DMA access
  362.         mov     [allow_dma_access], al
  363.         movzx eax, byte [BOOT_VAR+0x9000]        ; bpp
  364.         mov [ScreenBPP],al
  365.  
  366.         mov [_display.bpp], eax
  367.         mov [_display.vrefresh], 60
  368.         mov [_display.disable_mouse],  __sys_disable_mouse
  369.  
  370.         movzx eax,word [BOOT_VAR+0x900A]  ; X max
  371.         mov [_display.width], eax
  372.         dec   eax
  373.         mov   [Screen_Max_X],eax
  374.         mov   [screen_workarea.right],eax
  375.         movzx eax,word [BOOT_VAR+0x900C]  ; Y max
  376.         mov [_display.height], eax
  377.         dec   eax
  378.         mov   [Screen_Max_Y],eax
  379.         mov   [screen_workarea.bottom],eax
  380.         movzx eax,word [BOOT_VAR+0x9008]  ; screen mode
  381.         mov   [SCR_MODE],eax
  382.         mov   eax,[BOOT_VAR+0x9014]       ; Vesa 1.2 bnk sw add
  383.         mov   [BANK_SWITCH],eax
  384.         mov   [BytesPerScanLine],word 640*4         ; Bytes PerScanLine
  385.         cmp   [SCR_MODE],word 0x13          ; 320x200
  386.         je    @f
  387.         cmp   [SCR_MODE],word 0x12          ; VGA 640x480
  388.         je    @f
  389.         movzx eax, word[BOOT_VAR+0x9001]        ; for other modes
  390.         mov   [BytesPerScanLine],ax
  391.         mov [_display.pitch], eax
  392. @@:
  393.         mov eax, [_display.width]
  394.         mul [_display.height]
  395.         mov [_WinMapSize], eax
  396.  
  397.         mov     esi, BOOT_VAR+0x9080
  398.         movzx   ecx, byte [esi-1]
  399.         mov     [NumBiosDisks], ecx
  400.         mov     edi, BiosDisksData
  401.         rep     movsd
  402.  
  403. ; GRAPHICS ADDRESSES
  404.  
  405.         and     byte [BOOT_VAR+0x901e],0x0
  406.         mov     eax,[BOOT_VAR+0x9018]
  407.         mov     [LFBAddress],eax
  408.  
  409.         cmp     [SCR_MODE],word 0100000000000000b
  410.         jge     setvesa20
  411.         cmp     [SCR_MODE],word 0x13
  412.         je      v20ga32
  413.         mov     [PUTPIXEL],dword Vesa12_putpixel24  ; Vesa 1.2
  414.         mov     [GETPIXEL],dword Vesa12_getpixel24
  415.         cmp     [ScreenBPP],byte 24
  416.         jz      ga24
  417.         mov     [PUTPIXEL],dword Vesa12_putpixel32
  418.         mov     [GETPIXEL],dword Vesa12_getpixel32
  419.       ga24:
  420.         jmp     v20ga24
  421.       setvesa20:
  422.         mov     [PUTPIXEL],dword Vesa20_putpixel24  ; Vesa 2.0
  423.         mov     [GETPIXEL],dword Vesa20_getpixel24
  424.         cmp     [ScreenBPP],byte 24
  425.         jz      v20ga24
  426.       v20ga32:
  427.         mov     [PUTPIXEL],dword Vesa20_putpixel32
  428.         mov     [GETPIXEL],dword Vesa20_getpixel32
  429.       v20ga24:
  430.         cmp     [SCR_MODE],word 0x12                ; 16 C VGA 640x480
  431.         jne     no_mode_0x12
  432.         mov     [PUTPIXEL],dword VGA_putpixel
  433.         mov     [GETPIXEL],dword Vesa20_getpixel32
  434.       no_mode_0x12:
  435.  
  436. ; -------- Fast System Call init ----------
  437. ; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
  438.            bt [cpu_caps], CAPS_SEP
  439.            jnc .SEnP   ; SysEnter not Present
  440.            xor edx, edx
  441.            mov ecx, MSR_SYSENTER_CS
  442.            mov eax, os_code
  443.            wrmsr
  444.            mov ecx, MSR_SYSENTER_ESP
  445. ;           mov eax, sysenter_stack ; Check it
  446.            xor     eax, eax
  447.            wrmsr
  448.            mov ecx, MSR_SYSENTER_EIP
  449.            mov eax, sysenter_entry
  450.            wrmsr
  451. .SEnP:
  452. ; AMD SYSCALL/SYSRET
  453.            cmp byte[cpu_vendor], 'A'
  454.            jne .noSYSCALL
  455.            mov eax, 0x80000001
  456.            cpuid
  457.            test edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
  458.            jz .noSYSCALL
  459.            mov ecx, MSR_AMD_EFER
  460.            rdmsr
  461.            or eax, 1   ; bit_0 - System Call Extension (SCE)
  462.            wrmsr
  463.  
  464.         ; !!!! It`s dirty hack, fix it !!!
  465.         ; Bits of EDX :
  466.         ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
  467.         ;  and the contents of this field, plus 8, are copied into the SS register.
  468.         ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
  469.         ;  and the contents of this field, plus 8, are copied into the SS register.
  470.  
  471.         ; mov   edx, (os_code + 16) * 65536 + os_code
  472.            mov edx, 0x1B0008
  473.  
  474.            mov eax, syscall_entry
  475.            mov ecx, MSR_AMD_STAR
  476.            wrmsr
  477. .noSYSCALL:
  478. ; -----------------------------------------
  479.         stdcall alloc_page
  480.         stdcall map_page, tss-0xF80, eax, PG_SW
  481.         stdcall alloc_page
  482.         inc     eax
  483.         mov     [SLOT_BASE+256+APPDATA.io_map], eax
  484.         stdcall map_page, tss+0x80, eax, PG_SW
  485.         stdcall alloc_page
  486.         inc     eax
  487.         mov     dword [SLOT_BASE+256+APPDATA.io_map+4], eax
  488.         stdcall map_page, tss+0x1080, eax, PG_SW
  489.  
  490. ; LOAD IDT
  491.  
  492.            call build_interrupt_table ;lidt is executed
  493.           ;lidt [idtreg]
  494.  
  495.            call init_kernel_heap
  496.            stdcall kernel_alloc, RING0_STACK_SIZE+512
  497.            mov [os_stack_seg], eax
  498.  
  499.            lea esp, [eax+RING0_STACK_SIZE]
  500.  
  501.            mov [tss._ss0], os_stack
  502.            mov [tss._esp0], esp
  503.            mov [tss._esp], esp
  504.            mov [tss._cs],os_code
  505.            mov [tss._ss],os_stack
  506.            mov [tss._ds],app_data
  507.            mov [tss._es],app_data
  508.            mov [tss._fs],app_data
  509.            mov [tss._gs],app_data
  510.            mov [tss._io],128
  511. ;Add IO access table - bit array of permitted ports
  512.            mov edi, tss._io_map_0
  513.            xor eax, eax
  514.            not eax
  515.            mov ecx, 8192/4
  516.            rep stosd                 ; access to 4096*8=65536 ports
  517.  
  518.            mov  ax,tss0
  519.            ltr  ax
  520.  
  521.            mov [LFBSize], 0x800000
  522.            call init_LFB
  523.            call init_fpu
  524.            call init_malloc
  525.  
  526.            stdcall alloc_kernel_space, 0x51000
  527.            mov [default_io_map], eax
  528.  
  529.            add eax, 0x2000
  530.            mov [ipc_tmp], eax
  531.            mov ebx, 0x1000
  532.  
  533.            add eax, 0x40000
  534.            mov [proc_mem_map], eax
  535.  
  536.            add eax, 0x8000
  537.            mov [proc_mem_pdir], eax
  538.  
  539.            add eax, ebx
  540.            mov [proc_mem_tab], eax
  541.  
  542.            add eax, ebx
  543.            mov [tmp_task_pdir], eax
  544.  
  545.            add eax, ebx
  546.            mov [tmp_task_ptab], eax
  547.  
  548.            add eax, ebx
  549.            mov [ipc_pdir], eax
  550.  
  551.            add eax, ebx
  552.            mov [ipc_ptab], eax
  553.  
  554.            stdcall kernel_alloc, (unpack.LZMA_BASE_SIZE+(unpack.LZMA_LIT_SIZE shl \
  555.                                  (unpack.lc+unpack.lp)))*4
  556.  
  557.            mov [unpack.p], eax
  558.  
  559.            call init_events
  560.            mov eax, srv.fd-SRV_FD_OFFSET
  561.            mov [srv.fd], eax
  562.            mov [srv.bk], eax
  563.  
  564. ;Set base of graphic segment to linear address of LFB
  565.         mov     eax,[LFBAddress]          ; set for gs
  566.         mov     [graph_data_l+2],ax
  567.         shr     eax,16
  568.         mov     [graph_data_l+4],al
  569.         mov     [graph_data_l+7],ah
  570.  
  571.         stdcall kernel_alloc, [_WinMapSize]
  572.         mov [_WinMapAddress], eax
  573.  
  574.         xor  eax,eax
  575.         inc  eax
  576.         mov [CURRENT_TASK],eax          ;dword 1
  577.         mov [TASK_COUNT],eax            ;dword 1
  578.         mov [TASK_BASE],dword TASK_DATA
  579.         mov [current_slot], SLOT_BASE+256
  580.  
  581. ; set background
  582.  
  583.         mov   [BgrDrawMode],eax
  584.         mov   [BgrDataWidth],eax
  585.         mov   [BgrDataHeight],eax
  586.         mov    [mem_BACKGROUND], 4
  587.         mov [img_background], static_background_data
  588.  
  589.         mov     [SLOT_BASE + 256 + APPDATA.dir_table], sys_pgdir - OS_BASE
  590.  
  591.         stdcall kernel_alloc, 0x10000/8
  592.         mov     edi, eax
  593.         mov     [network_free_ports], eax
  594.         or      eax, -1
  595.         mov     ecx, 0x10000/32
  596.         rep     stosd
  597.  
  598. ; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
  599.  
  600.         call  init_irqs
  601.         call  rerouteirqs
  602.  
  603. ; Initialize system V86 machine
  604.         call    init_sys_v86
  605.  
  606. ; TIMER SET TO 1/100 S
  607.  
  608.         mov   al,0x34              ; set to 100Hz
  609.         out   0x43,al
  610.         mov   al,0x9b              ; lsb    1193180 / 1193
  611.         out   0x40,al
  612.         mov   al,0x2e              ; msb
  613.         out   0x40,al
  614.  
  615. ; Enable timer IRQ (IRQ0) and hard drives IRQs (IRQ14, IRQ15)
  616. ; they are used: when partitions are scanned, hd_read relies on timer
  617. ; Also enable IRQ2, because in some configurations
  618. ; IRQs from slave controller are not delivered until IRQ2 on master is enabled
  619.         mov     al, 0xFA
  620.         out     0x21, al
  621.         mov     al, 0x3F
  622.         out     0xA1, al
  623.  
  624. ; Enable interrupts in IDE controller
  625.         mov     al, 0
  626.         mov     dx, 0x3F6
  627.         out     dx, al
  628.         mov     dl, 0x76
  629.         out     dx, al
  630.  
  631. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  632. include 'detect/disks.inc'
  633. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  634.  
  635.   call Parser_params
  636.  
  637. if ~ defined extended_primary_loader
  638. ; ramdisk image should be loaded by extended primary loader if it exists
  639. ; READ RAMDISK IMAGE FROM HD
  640.  
  641. ;!!!!!!!!!!!!!!!!!!!!!!!
  642. include 'boot/rdload.inc'
  643. ;!!!!!!!!!!!!!!!!!!!!!!!
  644. end if
  645. ;    mov    [dma_hdd],1
  646. ; CALCULATE FAT CHAIN FOR RAMDISK
  647.  
  648.         call  calculatefatchain
  649.  
  650. ; LOAD VMODE DRIVER
  651.  
  652. ;!!!!!!!!!!!!!!!!!!!!!!!
  653. include 'vmodeld.inc'
  654. ;!!!!!!!!!!!!!!!!!!!!!!!
  655.  
  656. if 0
  657.   mov ax,[OS_BASE+0x10000+bx_from_load]
  658.   cmp ax,'r1'           ; if using not ram disk, then load librares and parameters {SPraid.simba}
  659.   je  no_lib_load
  660. ; LOADING LIBRARES
  661.    stdcall dll.Load,@IMPORT                 ; loading librares for kernel (.obj files)
  662.    call load_file_parse_table               ; prepare file parse table
  663.    call set_kernel_conf                     ; configure devices and gui
  664. no_lib_load:
  665. end if
  666.  
  667. ; LOAD FONTS I and II
  668.  
  669.         stdcall read_file, char, FONT_I, 0, 2304
  670.         stdcall read_file, char2, FONT_II, 0, 2560
  671.  
  672.         mov   esi,boot_fonts
  673.         call  boot_log
  674.  
  675. ; PRINT AMOUNT OF MEMORY
  676.         mov     esi, boot_memdetect
  677.         call    boot_log
  678.  
  679.         movzx   ecx, word [boot_y]
  680.         if lang eq ru
  681.                 or      ecx, (10+30*6) shl 16
  682.                 else
  683.                 or      ecx, (10+29*6) shl 16
  684.                 end if
  685.         sub     ecx, 10
  686.         mov     edx, 0xFFFFFF
  687.         mov     ebx, [MEM_AMOUNT]
  688.         shr     ebx, 20
  689.         xor     edi,edi
  690.         mov     eax, 0x00040000
  691.                 inc             edi
  692.         call    display_number_force
  693.  
  694. ; BUILD SCHEDULER
  695.  
  696.         call   build_scheduler ; sys32.inc
  697.  
  698.         mov    esi,boot_devices
  699.         call   boot_log
  700.  
  701.         mov  [pci_access_enabled],1
  702.  
  703.  
  704. ; SET PRELIMINARY WINDOW STACK AND POSITIONS
  705.  
  706.         mov   esi,boot_windefs
  707.         call  boot_log
  708.         call  set_window_defaults
  709.  
  710. ; SET BACKGROUND DEFAULTS
  711.  
  712.         mov   esi,boot_bgr
  713.         call  boot_log
  714.         call  init_background
  715.         call  calculatebackground
  716.  
  717. ; RESERVE SYSTEM IRQ'S JA PORT'S
  718.  
  719.         mov   esi,boot_resirqports
  720.         call  boot_log
  721.         call  reserve_irqs_ports
  722.  
  723. ; SET UP OS TASK
  724.  
  725.         mov  esi,boot_setostask
  726.         call boot_log
  727.  
  728.         xor  eax, eax
  729.         mov  dword [SLOT_BASE+APPDATA.fpu_state], fpu_data
  730.         mov  dword [SLOT_BASE+APPDATA.exc_handler], eax
  731.         mov  dword [SLOT_BASE+APPDATA.except_mask], eax
  732.  
  733.         ; name for OS/IDLE process
  734.  
  735.         mov dword [SLOT_BASE+256+APPDATA.app_name],   dword 'OS/I'
  736.         mov dword [SLOT_BASE+256+APPDATA.app_name+4], dword 'DLE '
  737.         mov edi, [os_stack_seg]
  738.         mov dword [SLOT_BASE+256+APPDATA.pl0_stack], edi
  739.         add edi, 0x2000-512
  740.         mov dword [SLOT_BASE+256+APPDATA.fpu_state], edi
  741.         mov dword [SLOT_BASE+256+APPDATA.saved_esp0], edi ; just for case
  742.         ; [SLOT_BASE+256+APPDATA.io_map] was set earlier
  743.  
  744.         mov esi, fpu_data
  745.         mov ecx, 512/4
  746.         cld
  747.         rep movsd
  748.  
  749.         mov dword [SLOT_BASE+256+APPDATA.exc_handler], eax
  750.         mov dword [SLOT_BASE+256+APPDATA.except_mask], eax
  751.  
  752.         mov ebx, SLOT_BASE+256+APP_OBJ_OFFSET
  753.         mov  dword [SLOT_BASE+256+APPDATA.fd_obj], ebx
  754.         mov  dword [SLOT_BASE+256+APPDATA.bk_obj], ebx
  755.  
  756.         mov  dword [SLOT_BASE+256+APPDATA.cur_dir], sysdir_path
  757.         mov dword [SLOT_BASE+256+APPDATA.tls_base], eax
  758.  
  759.         ; task list
  760.         mov  dword [TASK_DATA+TASKDATA.mem_start],eax   ; process base address
  761.         inc  eax
  762.         mov  dword [CURRENT_TASK],eax
  763.         mov  dword [TASK_COUNT],eax
  764.         mov  [current_slot], SLOT_BASE+256
  765.         mov  [TASK_BASE],dword TASK_DATA
  766.         mov  byte[TASK_DATA+TASKDATA.wnd_number],al     ; on screen number
  767.         mov  dword [TASK_DATA+TASKDATA.pid], eax        ; process id number
  768.  
  769.         call init_display
  770.         mov eax, [def_cursor]
  771.         mov [SLOT_BASE+APPDATA.cursor],eax
  772.         mov [SLOT_BASE+APPDATA.cursor+256],eax
  773.  
  774.   ; READ TSC / SECOND
  775.  
  776.         mov   esi,boot_tsc
  777.         call  boot_log
  778.         cli
  779.         rdtsc ;call  _rdtsc
  780.         mov   ecx,eax
  781.         mov   esi,250               ; wait 1/4 a second
  782.         call  delay_ms
  783.         rdtsc ;call  _rdtsc
  784.         sti
  785.         sub   eax,ecx
  786.         shl   eax,2
  787.         mov   [CPU_FREQ],eax          ; save tsc / sec
  788. ;       mov ebx, 1000000
  789. ;       div ebx
  790. ; ¢®®¡é¥-â® ¯à®¨§¢®¤¨â¥«ì­®áâì ¢ ¤ ­­®¬ ª®­ªà¥â­®¬ ¬¥áâ¥
  791. ; ᮢ¥à襭­® ­¥ªà¨â¨ç­ , ­® çâ®¡ë § âª­ãâì «î¡¨â¥«¥©
  792. ; ®¯â¨¬¨§¨àãîé¨å ª®¬¯¨«ïâ®à®¢ Ÿ‚“...
  793.         mov     edx, 2251799814
  794.         mul     edx
  795.         shr     edx, 19
  796.         mov [stall_mcs], edx
  797. ; PRINT CPU FREQUENCY
  798.         mov     esi, boot_cpufreq
  799.         call    boot_log
  800.  
  801.         mov     ebx, edx
  802.         movzx   ecx, word [boot_y]
  803.         if lang eq ru
  804.                 add      ecx, (10+19*6) shl 16 - 10; 'Determining amount of memory'
  805.                 else
  806.                 add      ecx, (10+17*6) shl 16 - 10; 'Determining amount of memory'
  807.                 end if
  808.         mov     edx, 0xFFFFFF
  809.         xor     edi,edi
  810.         mov     eax, 0x00040000
  811.                 inc             edi
  812.         call    display_number_force
  813.  
  814. ; SET VARIABLES
  815.  
  816.         call  set_variables
  817.  
  818. ; SET MOUSE
  819.  
  820.         ;call   detect_devices
  821.         stdcall load_driver, szPS2MDriver
  822. ;        stdcall load_driver, szCOM_MDriver
  823.  
  824.         mov   esi,boot_setmouse
  825.         call  boot_log
  826.         call  setmouse
  827.  
  828.  
  829. ; STACK AND FDC
  830.  
  831.         call  stack_init
  832.         call  fdc_init
  833.  
  834. ; PALETTE FOR 320x200 and 640x480 16 col
  835.  
  836.         cmp   [SCR_MODE],word 0x12
  837.         jne   no_pal_vga
  838.         mov   esi,boot_pal_vga
  839.         call  boot_log
  840.         call  paletteVGA
  841.       no_pal_vga:
  842.  
  843.         cmp   [SCR_MODE],word 0x13
  844.         jne   no_pal_ega
  845.         mov   esi,boot_pal_ega
  846.         call  boot_log
  847.         call  palette320x200
  848.       no_pal_ega:
  849.  
  850. ; LOAD DEFAULT SKIN
  851.  
  852.         call    load_default_skin
  853.  
  854. ;protect io permission map
  855.  
  856.            mov esi, [default_io_map]
  857.            stdcall map_page,esi,[SLOT_BASE+256+APPDATA.io_map], PG_MAP
  858.            add esi, 0x1000
  859.            stdcall map_page,esi,[SLOT_BASE+256+APPDATA.io_map+4], PG_MAP
  860.  
  861.            stdcall map_page,tss._io_map_0,\
  862.                    [SLOT_BASE+256+APPDATA.io_map], PG_MAP
  863.            stdcall map_page,tss._io_map_1,\
  864.                    [SLOT_BASE+256+APPDATA.io_map+4], PG_MAP
  865.  
  866.   mov ax,[OS_BASE+0x10000+bx_from_load]
  867.   cmp ax,'r1'           ; if not rused ram disk - load network configuration from files {SPraid.simba}
  868.   je  no_st_network
  869.         call set_network_conf
  870.   no_st_network:
  871.  
  872. ; LOAD FIRST APPLICATION
  873.         cli
  874.  
  875.         cmp   byte [BOOT_VAR+0x9030],1
  876.         jne   no_load_vrr_m
  877.  
  878.         mov     ebp, vrr_m
  879.         call    fs_execute_from_sysdir
  880.  
  881. ;        cmp   eax,2                  ; if vrr_m app found (PID=2)
  882.         sub   eax,2
  883.         jz    first_app_found
  884.  
  885. no_load_vrr_m:
  886.  
  887.         mov     ebp, firstapp
  888.         call    fs_execute_from_sysdir
  889.  
  890. ;        cmp   eax,2                  ; continue if a process has been loaded
  891.         sub   eax,2
  892.         jz    first_app_found
  893.  
  894.         mov     esi, boot_failed
  895.         call    boot_log
  896.  
  897.         mov   eax, 0xDEADBEEF        ; otherwise halt
  898.         hlt
  899.  
  900. first_app_found:
  901.  
  902.         cli
  903.  
  904.         ;mov   [TASK_COUNT],dword 2
  905.         push  1
  906.         pop   dword [CURRENT_TASK]      ; set OS task fisrt
  907.  
  908. ; SET KEYBOARD PARAMETERS
  909.         mov   al, 0xf6         ; reset keyboard, scan enabled
  910.         call  kb_write
  911.  
  912.         ; wait until 8042 is ready
  913.         xor ecx,ecx
  914.       @@:
  915.         in     al,64h
  916.         and    al,00000010b
  917.         loopnz @b
  918.  
  919.        ; mov   al, 0xED       ; svetodiody - only for testing!
  920.        ; call  kb_write
  921.        ; call  kb_read
  922.        ; mov   al, 111b
  923.        ; call  kb_write
  924.        ; call  kb_read
  925.  
  926.         mov   al, 0xF3       ; set repeat rate & delay
  927.         call  kb_write
  928. ;        call  kb_read
  929.         mov   al, 0 ; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  930.         call  kb_write
  931. ;        call  kb_read
  932.      ;// mike.dld [
  933.         call  set_lights
  934.      ;// mike.dld ]
  935.  
  936.  
  937. ; Setup serial output console (if enabled)
  938.  
  939. if defined debug_com_base
  940.  
  941.         ; enable Divisor latch
  942.  
  943.         mov     dx, debug_com_base+3
  944.         mov     al, 1 shl 7
  945.         out     dx, al
  946.  
  947.         ; Set speed to 115200 baud (max speed)
  948.  
  949.         mov     dx, debug_com_base
  950.         mov     al, 0x01
  951.         out     dx, al
  952.  
  953.         mov     dx, debug_com_base+1
  954.         mov     al, 0x00
  955.         out     dx, al
  956.  
  957.         ; No parity, 8bits words, one stop bit, dlab bit back to 0
  958.  
  959.         mov     dx, debug_com_base+3
  960.         mov     al, 3
  961.         out     dx, al
  962.  
  963.         ; disable interrupts
  964.  
  965.         mov     dx, debug_com_base+1
  966.         mov     al, 0
  967.         out     dx, al
  968.  
  969.         ; clear +  enable fifo (64 bits)
  970.  
  971.         mov     dx, debug_com_base+2
  972.         mov     al, 0x7 + 1 shl 5
  973.         out     dx, al
  974.  
  975.  
  976. end if
  977.  
  978. ; START MULTITASKING
  979.  
  980. ; A 'All set - press ESC to start' messages if need
  981. if preboot_blogesc
  982.         mov     esi, boot_tasking
  983.         call    boot_log
  984. .bll1:  in      al, 0x60        ; wait for ESC key press
  985.         cmp     al, 129
  986.         jne     .bll1
  987. end if
  988.  
  989. ;       mov   [ENABLE_TASKSWITCH],byte 1        ; multitasking enabled
  990.  
  991. ; UNMASK ALL IRQ'S
  992.  
  993. ;        mov   esi,boot_allirqs
  994. ;        call  boot_log
  995. ;
  996. ;        cli                          ;guarantee forbidance of interrupts.
  997. ;        mov   al,0                   ; unmask all irq's
  998. ;        out   0xA1,al
  999. ;        out   0x21,al
  1000. ;
  1001. ;        mov   ecx,32
  1002. ;
  1003. ;     ready_for_irqs:
  1004. ;
  1005. ;        mov   al,0x20                ; ready for irqs
  1006. ;        out   0x20,al
  1007. ;        out   0xa0,al
  1008. ;
  1009. ;        loop  ready_for_irqs         ; flush the queue
  1010.  
  1011.         stdcall attach_int_handler, 1, irq1, 0
  1012.  
  1013. ;        mov    [dma_hdd],1
  1014.         cmp     [IDEContrRegsBaseAddr], 0
  1015.         setnz   [dma_hdd]
  1016.         mov [timer_ticks_enable],1              ; for cd driver
  1017.  
  1018.         sti
  1019.         call change_task
  1020.  
  1021.         jmp osloop
  1022.  
  1023.  
  1024.         ; Fly :)
  1025.  
  1026. include 'unpacker.inc'
  1027. include 'fdo.inc'
  1028.  
  1029. align 4
  1030. boot_log:
  1031.          pushad
  1032.  
  1033.         mov   ebx,10*65536
  1034.         mov   bx,word [boot_y]
  1035.         add   [boot_y],dword 10
  1036.         mov   ecx,0x80ffffff   ; ASCIIZ string with white color
  1037.                 xor       edi,edi
  1038.         mov   edx,esi
  1039.                 inc       edi
  1040.         call  dtext
  1041.  
  1042.         mov   [novesachecksum],1000
  1043.         call  checkVga_N13
  1044.  
  1045.         popad
  1046.  
  1047.         ret
  1048.  
  1049. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1050. ;                                                                    ;
  1051. ;                    MAIN OS LOOP START                              ;
  1052. ;                                                                    ;
  1053. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1054. align 32
  1055. osloop:
  1056.         call   [draw_pointer]
  1057.         call    window_check_events
  1058.         call    mouse_check_events
  1059.         call   checkmisc
  1060.         call   checkVga_N13
  1061.         call   stack_handler
  1062.         call   checkidle
  1063.         call   check_fdd_motor_status
  1064.         call   check_ATAPI_device_event
  1065.         call   check_timers
  1066.         jmp    osloop
  1067. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1068. ;                                                                    ;
  1069. ;                      MAIN OS LOOP END                              ;
  1070. ;                                                                    ;
  1071. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1072. align 4
  1073. checkidle:
  1074.         pushad
  1075.         call    change_task
  1076.         jmp     idle_loop_entry
  1077.   idle_loop:
  1078.         cmp     eax,[idlemem]     ; eax == [timer_ticks]
  1079.         jne     idle_exit
  1080.         rdtsc   ;call _rdtsc
  1081.         mov     ecx,eax
  1082.         hlt
  1083.         rdtsc   ;call _rdtsc
  1084.         sub     eax,ecx
  1085.         add     [idleuse],eax
  1086.   idle_loop_entry:
  1087.         mov     eax,[timer_ticks] ; eax =  [timer_ticks]
  1088.         cmp     [check_idle_semaphore],0
  1089.         je      idle_loop
  1090.         dec     [check_idle_semaphore]
  1091.   idle_exit:
  1092.         mov     [idlemem],eax     ; eax == [timer_ticks]
  1093.         popad
  1094.         ret
  1095.  
  1096. uglobal
  1097.   idlemem               dd   0x0
  1098.   idleuse               dd   0x0
  1099.   idleusesec            dd   0x0
  1100.   check_idle_semaphore  dd   0x0
  1101. endg
  1102.  
  1103.  
  1104.  
  1105. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1106. ;                                                                      ;
  1107. ;                   INCLUDED SYSTEM FILES                              ;
  1108. ;                                                                      ;
  1109. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1110.  
  1111.  
  1112. include "kernel32.inc"
  1113.  
  1114.  
  1115. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1116. ;                                                                      ;
  1117. ;                       KERNEL FUNCTIONS                               ;
  1118. ;                                                                      ;
  1119. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1120.  
  1121. reserve_irqs_ports:
  1122.  
  1123.  
  1124. ; RESERVE PORTS
  1125.         mov eax, RESERVED_PORTS
  1126.         mov ecx, 1
  1127.  
  1128.         mov [eax], dword 4
  1129.  
  1130.         mov [eax+16], ecx
  1131.         mov [eax+16+4], dword 0
  1132.         mov [eax+16+4], dword 0x2D
  1133.  
  1134.         mov [eax+32], ecx
  1135.         mov [eax+32+4], dword 0x30
  1136.         mov [eax+32+8], dword 0x4D
  1137.  
  1138.         mov [eax+48], ecx
  1139.         mov [eax+48+4], dword 0x50
  1140.         mov [eax+28+8], dword 0xDF
  1141.  
  1142.         mov [eax+64], ecx
  1143.         mov [eax+64+4], dword 0xE5
  1144.         mov [eax+64+8], dword 0xFF
  1145.  
  1146.         ret
  1147.  
  1148.  
  1149. iglobal
  1150.   process_number dd 0x1
  1151. endg
  1152.  
  1153. set_variables:
  1154.  
  1155.         mov   ecx,0x100                       ; flush port 0x60
  1156. .fl60:  in    al,0x60
  1157.         loop  .fl60
  1158.         push  eax
  1159.  
  1160.         mov   ax,[BOOT_VAR+0x900c]
  1161.         shr   ax,1
  1162.         shl   eax,16
  1163.         mov   ax,[BOOT_VAR+0x900A]
  1164.         shr   ax,1
  1165.         mov   [MOUSE_X],eax
  1166.  
  1167.         xor   eax,eax
  1168.         mov   [BTN_ADDR],dword BUTTON_INFO    ; address of button list
  1169.  
  1170.         mov   byte [MOUSE_BUFF_COUNT],al                 ; mouse buffer
  1171.         mov   byte [KEY_COUNT],al                 ; keyboard buffer
  1172.         mov   byte [BTN_COUNT],al                 ; button buffer
  1173. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  1174.  
  1175.      ;!! IP 04.02.2005:
  1176.         mov   byte [DONT_SWITCH],al ; change task if possible
  1177.         pop   eax
  1178.         ret
  1179.  
  1180. align 4
  1181. ;input  eax=43,bl-byte of output, ecx - number of port
  1182. sys_outport:
  1183.  
  1184.     mov   edi,ecx          ; separate flag for read / write
  1185.     and   ecx,65535
  1186.  
  1187.     mov   eax,[RESERVED_PORTS]
  1188.     test  eax,eax
  1189.     jnz   .sopl8
  1190.     inc   eax
  1191.         mov   [esp+32],eax
  1192.     ret
  1193.  
  1194.   .sopl8:
  1195.     mov   edx,[TASK_BASE]
  1196.     mov   edx,[edx+0x4]
  1197.     ;and   ecx,65535
  1198.     ;cld - set on interrupt 0x40
  1199.   .sopl1:
  1200.  
  1201.     mov   esi,eax
  1202.     shl   esi,4
  1203.     add   esi,RESERVED_PORTS
  1204.     cmp   edx,[esi+0]
  1205.     jne   .sopl2
  1206.     cmp   ecx,[esi+4]
  1207.     jb    .sopl2
  1208.     cmp   ecx,[esi+8]
  1209.     jg    .sopl2
  1210. .sopl3:
  1211.  
  1212.     test  edi,0x80000000 ; read ?
  1213.     jnz   .sopl4
  1214.  
  1215.         mov       eax,ebx
  1216.     mov   dx,cx          ; write
  1217.     out   dx,al
  1218.     and   [esp+32],dword 0
  1219.     ret
  1220.  
  1221.         .sopl2:
  1222.  
  1223.     dec   eax
  1224.     jnz   .sopl1
  1225.         inc       eax
  1226.     mov   [esp+32],eax
  1227.     ret
  1228.  
  1229.  
  1230.   .sopl4:
  1231.  
  1232.     mov   dx,cx          ; read
  1233.     in    al,dx
  1234.     and   eax,0xff
  1235.     and   [esp+32],dword 0
  1236.     mov   [esp+20],eax
  1237.     ret
  1238.  
  1239. display_number:
  1240. ;It is not optimization
  1241.         mov     eax, ebx
  1242.         mov     ebx, ecx
  1243.         mov     ecx, edx
  1244.         mov     edx, esi
  1245.         mov     esi, edi
  1246. ; eax = print type, al=0 -> ebx is number
  1247. ;                   al=1 -> ebx is pointer
  1248. ;                   ah=0 -> display decimal
  1249. ;                   ah=1 -> display hexadecimal
  1250. ;                   ah=2 -> display binary
  1251. ;                   eax bits 16-21 = number of digits to display (0-32)
  1252. ;                   eax bits 22-31 = reserved
  1253. ;
  1254. ; ebx = number or pointer
  1255. ; ecx = x shl 16 + y
  1256. ; edx = color
  1257.     xor     edi, edi
  1258. display_number_force:
  1259.     push  eax
  1260.     and   eax,0x3fffffff
  1261.     cmp   eax,0xffff            ; length > 0 ?
  1262.     pop   eax
  1263.     jge   cont_displ
  1264.     ret
  1265.    cont_displ:
  1266.      push  eax
  1267.      and   eax,0x3fffffff
  1268.      cmp   eax,61*0x10000        ; length <= 60 ?
  1269.      pop   eax
  1270.      jb    cont_displ2
  1271.      ret
  1272.    cont_displ2:
  1273.  
  1274.      pushad
  1275.  
  1276.      cmp   al,1                  ; ecx is a pointer ?
  1277.      jne   displnl1
  1278.      mov   ebp,ebx
  1279.      add   ebp,4
  1280.      mov   ebp,[ebp+std_application_base_address]
  1281.      mov   ebx,[ebx+std_application_base_address]
  1282.  displnl1:
  1283.      sub   esp,64
  1284.  
  1285.     test   ah,ah                  ; DECIMAL
  1286.     jnz   no_display_desnum
  1287.     shr   eax,16
  1288.     and   eax,0xC03f
  1289. ;     and   eax,0x3f
  1290.     push  eax
  1291.     and   eax,0x3f
  1292.     mov   edi,esp
  1293.     add   edi,4+64-1
  1294.     mov   ecx,eax
  1295.     mov   eax,ebx
  1296.     mov   ebx,10
  1297.  d_desnum:
  1298.      xor   edx,edx
  1299.      call  division_64_bits
  1300.      div   ebx
  1301.      add   dl,48
  1302.      mov   [edi],dl
  1303.      dec   edi
  1304.      loop  d_desnum
  1305.      pop   eax
  1306.      call  normalize_number
  1307.      call  draw_num_text
  1308.      add   esp,64
  1309.      popad
  1310.      ret
  1311.    no_display_desnum:
  1312.  
  1313.      cmp   ah,0x01               ; HEXADECIMAL
  1314.      jne   no_display_hexnum
  1315.      shr   eax,16
  1316.      and   eax,0xC03f
  1317. ;     and   eax,0x3f
  1318.      push  eax
  1319.      and   eax,0x3f
  1320.      mov   edi,esp
  1321.      add   edi,4+64-1
  1322.      mov   ecx,eax
  1323.      mov   eax,ebx
  1324.      mov   ebx,16
  1325.    d_hexnum:
  1326.      xor   edx,edx
  1327.      call  division_64_bits
  1328.      div   ebx
  1329.    hexletters = __fdo_hexdigits
  1330.      add   edx,hexletters
  1331.      mov   dl,[edx]
  1332.      mov   [edi],dl
  1333.      dec   edi
  1334.      loop  d_hexnum
  1335.      pop   eax
  1336.      call  normalize_number
  1337.      call  draw_num_text
  1338.      add   esp,64
  1339.      popad
  1340.      ret
  1341.    no_display_hexnum:
  1342.  
  1343.      cmp   ah,0x02               ; BINARY
  1344.      jne   no_display_binnum
  1345.      shr   eax,16
  1346.      and   eax,0xC03f
  1347. ;     and   eax,0x3f
  1348.      push  eax
  1349.      and   eax,0x3f
  1350.      mov   edi,esp
  1351.      add   edi,4+64-1
  1352.      mov   ecx,eax
  1353.      mov   eax,ebx
  1354.      mov   ebx,2
  1355.    d_binnum:
  1356.      xor   edx,edx
  1357.      call  division_64_bits
  1358.      div   ebx
  1359.      add   dl,48
  1360.      mov   [edi],dl
  1361.      dec   edi
  1362.      loop  d_binnum
  1363.      pop   eax
  1364.      call  normalize_number
  1365.      call  draw_num_text
  1366.      add   esp,64
  1367.      popad
  1368.      ret
  1369.    no_display_binnum:
  1370.  
  1371.      add   esp,64
  1372.      popad
  1373.      ret
  1374.  
  1375. normalize_number:
  1376.      test  ah,0x80
  1377.      jz   .continue
  1378.      mov  ecx,48
  1379.      and   eax,0x3f
  1380. @@:
  1381.      inc   edi
  1382.      cmp   [edi],cl
  1383.      jne   .continue
  1384.      dec   eax
  1385.      cmp   eax,1
  1386.      ja    @r
  1387.      mov   al,1
  1388. .continue:
  1389.      and   eax,0x3f
  1390.      ret
  1391.  
  1392. division_64_bits:
  1393.      test  [esp+1+4],byte 0x40
  1394.      jz   .continue
  1395.      push  eax
  1396.      mov   eax,ebp
  1397.      div   ebx
  1398.      mov   ebp,eax
  1399.      pop   eax
  1400. .continue:
  1401.      ret
  1402.  
  1403. draw_num_text:
  1404.      mov   esi,eax
  1405.      mov   edx,64+4
  1406.      sub   edx,eax
  1407.      add   edx,esp
  1408.      mov   ebx,[esp+64+32-8+4]
  1409. ; add window start x & y
  1410.      mov   ecx,[TASK_BASE]
  1411.  
  1412.      mov   edi,[CURRENT_TASK]
  1413.      shl   edi,8
  1414.  
  1415.      mov   eax,[ecx-twdw+WDATA.box.left]
  1416.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1417.      shl   eax,16
  1418.      add   eax,[ecx-twdw+WDATA.box.top]
  1419.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1420.      add   ebx,eax
  1421.      mov   ecx,[esp+64+32-12+4]
  1422.         and     ecx, not 0x80000000     ; force counted string
  1423.         mov     eax, [esp+64+8]         ; background color (if given)
  1424.         mov     edi, [esp+64+4]
  1425.      jmp   dtext
  1426.  
  1427. align 4
  1428.  
  1429. sys_setup:
  1430.  
  1431. ; 1=roland mpu midi base , base io address
  1432. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1433. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1434. ; 5=system language, 1eng 2fi 3ger 4rus
  1435. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1436. ; 8=fat32 partition in hd
  1437. ; 9
  1438. ; 10 = sound dma channel
  1439. ; 11 = enable lba read
  1440. ; 12 = enable pci access
  1441.  
  1442.  
  1443.         and  [esp+32],dword 0
  1444.         dec     ebx                             ; MIDI
  1445.         jnz  nsyse1
  1446.         cmp  ecx,0x100
  1447.  
  1448.         jb   nsyse1
  1449.         mov     esi,65535
  1450.         cmp     esi,ecx
  1451.  
  1452.         jb   nsyse1
  1453.         mov  [midi_base],cx     ;bx
  1454.         mov  word [mididp],cx   ;bx
  1455.         inc  cx                 ;bx
  1456.         mov  word [midisp],cx   ;bx
  1457.         ret
  1458.  
  1459. iglobal
  1460. midi_base dw 0
  1461. endg
  1462.  
  1463.    nsyse1:
  1464.         dec     ebx                              ; KEYBOARD
  1465.         jnz  nsyse2
  1466.         mov  edi,[TASK_BASE]
  1467.         mov  eax,[edi+TASKDATA.mem_start]
  1468.         add  eax,edx
  1469.  
  1470.         dec     ecx
  1471.         jnz  kbnobase
  1472.         mov  ebx,keymap
  1473.         mov  ecx,128
  1474.         call memmove
  1475.         ret
  1476.    kbnobase:
  1477.         dec  ecx
  1478.         jnz  kbnoshift
  1479.  
  1480.         mov  ebx,keymap_shift
  1481.         mov  ecx,128
  1482.         call memmove
  1483.         ret
  1484.    kbnoshift:
  1485.         dec  ecx
  1486.         jnz  kbnoalt
  1487.         mov  ebx,keymap_alt
  1488.         mov  ecx,128
  1489.         call memmove
  1490.         ret
  1491.    kbnoalt:
  1492.         sub  ecx,6
  1493.         jnz  kbnocountry
  1494.         mov  word [keyboard],dx
  1495.         ret
  1496.    kbnocountry:
  1497.         mov  [esp+32],dword 1
  1498.         ret
  1499.    nsyse2:
  1500.         dec  ebx                            ; CD
  1501.         jnz  nsyse4
  1502.  
  1503.         test ecx,ecx
  1504.         jz   nosesl
  1505.  
  1506.         cmp  ecx, 4
  1507.         ja   nosesl
  1508.         mov  [cd_base],cl
  1509.  
  1510.         dec     ecx
  1511.         jnz  noprma
  1512.         mov  [cdbase],0x1f0
  1513.         mov  [cdid],0xa0
  1514.    noprma:
  1515.  
  1516.         dec     ecx
  1517.         jnz  noprsl
  1518.         mov  [cdbase],0x1f0
  1519.         mov  [cdid],0xb0
  1520.    noprsl:
  1521.         dec     ecx
  1522.         jnz  nosema
  1523.         mov  [cdbase],0x170
  1524.         mov  [cdid],0xa0
  1525.    nosema:
  1526.         dec     ecx
  1527.         jnz  nosesl
  1528.         mov  [cdbase],0x170
  1529.         mov  [cdid],0xb0
  1530.    nosesl:
  1531.         ret
  1532.  
  1533. iglobal
  1534. cd_base db 0
  1535.  
  1536. endg
  1537.    nsyse4:
  1538.  
  1539.         sub  ebx,2               ; SYSTEM LANGUAGE
  1540.         jnz  nsyse5
  1541.         mov  [syslang],ecx
  1542.         ret
  1543.    nsyse5:
  1544.  
  1545.         sub  ebx,2              ; HD BASE
  1546.         jnz  nsyse7
  1547.  
  1548.         test ecx,ecx
  1549.         jz   nosethd
  1550.  
  1551.         cmp  ecx,4
  1552.         ja   nosethd
  1553.         mov  [hd_base],cl
  1554.  
  1555.         cmp  ecx,1
  1556.         jnz  noprmahd
  1557.         mov  [hdbase],0x1f0
  1558.         and  dword [hdid],0x0
  1559.         mov  dword [hdpos],ecx
  1560. ;     call set_FAT32_variables
  1561.    noprmahd:
  1562.  
  1563.         cmp  ecx,2
  1564.         jnz  noprslhd
  1565.         mov  [hdbase],0x1f0
  1566.         mov  [hdid],0x10
  1567.         mov  dword [hdpos],ecx
  1568. ;     call set_FAT32_variables
  1569.    noprslhd:
  1570.  
  1571.         cmp  ecx,3
  1572.         jnz  nosemahd
  1573.         mov  [hdbase],0x170
  1574.         and  dword [hdid],0x0
  1575.         mov  dword [hdpos],ecx
  1576. ;     call set_FAT32_variables
  1577.    nosemahd:
  1578.  
  1579.         cmp  ecx,4
  1580.         jnz  noseslhd
  1581.         mov  [hdbase],0x170
  1582.         mov  [hdid],0x10
  1583.         mov  dword [hdpos],ecx
  1584. ;     call set_FAT32_variables
  1585.    noseslhd:
  1586.         call  reserve_hd1
  1587.         call  reserve_hd_channel
  1588.         call  free_hd_channel
  1589.         and   dword [hd1_status],0        ; free
  1590.    nosethd:
  1591.         ret
  1592.  
  1593. iglobal
  1594. hd_base db 0
  1595. endg
  1596.  
  1597. nsyse7:
  1598.  
  1599. ;     cmp  eax,8                      ; HD PARTITION
  1600.         dec  ebx
  1601.         jnz  nsyse8
  1602.         mov  [fat32part],ecx
  1603. ;     call set_FAT32_variables
  1604.         call  reserve_hd1
  1605.         call  reserve_hd_channel
  1606.         call  free_hd_channel
  1607. ;       pusha
  1608.         call  choice_necessity_partition_1
  1609. ;       popa
  1610.         and dword [hd1_status],0        ; free
  1611.         ret
  1612.  
  1613. nsyse8:
  1614. ;     cmp  eax,11                     ; ENABLE LBA READ
  1615.         and  ecx,1
  1616.         sub  ebx,3
  1617.         jnz  no_set_lba_read
  1618.         mov  [lba_read_enabled],ecx
  1619.         ret
  1620.  
  1621. no_set_lba_read:
  1622. ;     cmp  eax,12                     ; ENABLE PCI ACCESS
  1623.         dec  ebx
  1624.         jnz  no_set_pci_access
  1625.         mov  [pci_access_enabled],ecx
  1626.         ret
  1627. no_set_pci_access:
  1628.  
  1629. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1630. include 'vmodeint.inc'
  1631. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1632.  
  1633. sys_setup_err:
  1634.         or  [esp+32],dword -1
  1635.         ret
  1636.  
  1637. align 4
  1638.  
  1639. sys_getsetup:
  1640.  
  1641. ; 1=roland mpu midi base , base io address
  1642. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1643. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1644. ; 5=system language, 1eng 2fi 3ger 4rus
  1645. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1646. ; 8=fat32 partition in hd
  1647. ; 9=get hs timer tic
  1648.  
  1649. ;     cmp  eax,1
  1650.         dec     ebx
  1651.         jnz  ngsyse1
  1652.         movzx eax,[midi_base]
  1653.         mov  [esp+32],eax
  1654.         ret
  1655. ngsyse1:
  1656. ;     cmp  eax,2
  1657.         dec     ebx
  1658.         jnz  ngsyse2
  1659.  
  1660.         mov  edi,[TASK_BASE]
  1661.         mov  ebx,[edi+TASKDATA.mem_start]
  1662.         add  ebx,edx
  1663.  
  1664. ;     cmp  ebx,1
  1665.         dec     ecx
  1666.         jnz  kbnobaseret
  1667.         mov  eax,keymap
  1668.         mov  ecx,128
  1669.         call memmove
  1670.         ret
  1671. kbnobaseret:
  1672. ;     cmp  ebx,2
  1673.         dec     ecx
  1674.         jnz  kbnoshiftret
  1675.  
  1676.         mov  eax,keymap_shift
  1677.         mov  ecx,128
  1678.         call memmove
  1679.         ret
  1680. kbnoshiftret:
  1681. ;     cmp  ebx,3
  1682.         dec     ecx
  1683.         jne  kbnoaltret
  1684.  
  1685.         mov  eax,keymap_alt
  1686.         mov  ecx,128
  1687.         call memmove
  1688.         ret
  1689. kbnoaltret:
  1690. ;     cmp  ebx,9
  1691.         sub     ecx,6
  1692.         jnz  ngsyse2
  1693.         movzx eax,word [keyboard]
  1694.         mov  [esp+32],eax
  1695.         ret
  1696.  
  1697.  
  1698. ngsyse2:
  1699. ;         cmp  eax,3
  1700.         dec     ebx
  1701.         jnz  ngsyse3
  1702.         movzx eax,[cd_base]
  1703.         mov  [esp+32],eax
  1704.         ret
  1705. ngsyse3:
  1706. ;         cmp  eax,5
  1707.         sub     ebx,2
  1708.         jnz  ngsyse5
  1709.         mov  eax,[syslang]
  1710.         mov  [esp+32],eax
  1711.         ret
  1712. ngsyse5:
  1713. ;     cmp  eax,7
  1714.         sub     ebx,2
  1715.         jnz  ngsyse7
  1716.         movzx eax,[hd_base]
  1717.         mov  [esp+32],eax
  1718.         ret
  1719. ngsyse7:
  1720. ;     cmp  eax,8
  1721.         dec     ebx
  1722.         jnz  ngsyse8
  1723.         mov eax,[fat32part]
  1724.         mov  [esp+32],eax
  1725.         ret
  1726. ngsyse8:
  1727. ;     cmp  eax,9
  1728.         dec     ebx
  1729.         jnz  ngsyse9
  1730.         mov  eax,[timer_ticks] ;[0xfdf0]
  1731.         mov  [esp+32],eax
  1732.         ret
  1733. ngsyse9:
  1734. ;     cmp  eax,11
  1735.         sub     ebx,2
  1736.         jnz  ngsyse11
  1737.         mov eax,[lba_read_enabled]
  1738.         mov  [esp+32],eax
  1739.         ret
  1740. ngsyse11:
  1741. ;     cmp  eax,12
  1742.         dec     ebx
  1743.         jnz  ngsyse12
  1744.         mov eax,[pci_access_enabled]
  1745.         mov  [esp+32],eax
  1746.         ret
  1747. ngsyse12:
  1748.         mov  [esp+32],dword 1
  1749.         ret
  1750.  
  1751.  
  1752. get_timer_ticks:
  1753.         mov eax,[timer_ticks]
  1754.         ret
  1755.  
  1756. iglobal
  1757. align 4
  1758. mousefn dd msscreen, mswin, msbutton, msset
  1759.         dd app_load_cursor
  1760.         dd app_set_cursor
  1761.         dd app_delete_cursor
  1762.         dd msz
  1763. endg
  1764.  
  1765. readmousepos:
  1766.  
  1767. ; eax=0 screen relative
  1768. ; eax=1 window relative
  1769. ; eax=2 buttons pressed
  1770. ; eax=3 set mouse pos   ; reserved
  1771. ; eax=4 load cursor
  1772. ; eax=5 set cursor
  1773. ; eax=6 delete cursor   ; reserved
  1774. ; eax=7 get mouse_z
  1775.  
  1776.            cmp ebx, 7
  1777.            ja msset
  1778.            jmp [mousefn+ebx*4]
  1779. msscreen:
  1780.            mov  eax,[MOUSE_X]
  1781.            shl  eax,16
  1782.            mov  ax,[MOUSE_Y]
  1783.            mov  [esp+36-4],eax
  1784.            ret
  1785. mswin:
  1786.            mov  eax,[MOUSE_X]
  1787.            shl  eax,16
  1788.            mov  ax,[MOUSE_Y]
  1789.            mov  esi,[TASK_BASE]
  1790.            mov  bx, word [esi-twdw+WDATA.box.left]
  1791.            shl  ebx,16
  1792.            mov  bx, word [esi-twdw+WDATA.box.top]
  1793.            sub  eax,ebx
  1794.  
  1795.            mov  edi,[CURRENT_TASK]
  1796.            shl  edi,8
  1797.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1798.            rol  eax,16
  1799.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1800.            rol  eax,16
  1801.            mov  [esp+36-4],eax
  1802.            ret
  1803. msbutton:
  1804.            movzx eax,byte [BTN_DOWN]
  1805.            mov  [esp+36-4],eax
  1806.            ret
  1807. msz:
  1808.            mov   edi, [TASK_COUNT]
  1809.            movzx edi, word [WIN_POS + edi*2]
  1810.            cmp   edi, [CURRENT_TASK]
  1811.            jne   @f
  1812.            mov   ax,[MOUSE_SCROLL_H]
  1813.            shl   eax,16
  1814.            mov   ax,[MOUSE_SCROLL_V]
  1815.            mov   [esp+36-4],eax
  1816.            and   [MOUSE_SCROLL_H],word 0
  1817.            and   [MOUSE_SCROLL_V],word 0
  1818.            ret
  1819.        @@:
  1820.            and  [esp+36-4],dword 0
  1821. ;           ret
  1822. msset:
  1823.            ret
  1824.  
  1825. app_load_cursor:
  1826.            cmp ecx, OS_BASE
  1827.            jae msset
  1828.            stdcall load_cursor, ecx, edx
  1829.            mov [esp+36-4], eax
  1830.            ret
  1831.  
  1832. app_set_cursor:
  1833.            stdcall set_cursor, ecx
  1834.            mov [esp+36-4], eax
  1835.            ret
  1836.  
  1837. app_delete_cursor:
  1838.            stdcall delete_cursor, ecx
  1839.            mov [esp+36-4], eax
  1840.            ret
  1841.  
  1842. is_input:
  1843.  
  1844.    push edx
  1845.    mov  dx,word [midisp]
  1846.    in   al,dx
  1847.    and  al,0x80
  1848.    pop  edx
  1849.    ret
  1850.  
  1851. is_output:
  1852.  
  1853.    push edx
  1854.    mov  dx,word [midisp]
  1855.    in   al,dx
  1856.    and  al,0x40
  1857.    pop  edx
  1858.    ret
  1859.  
  1860.  
  1861. get_mpu_in:
  1862.  
  1863.    push edx
  1864.    mov  dx,word [mididp]
  1865.    in   al,dx
  1866.    pop  edx
  1867.    ret
  1868.  
  1869.  
  1870. put_mpu_out:
  1871.  
  1872.    push edx
  1873.    mov  dx,word [mididp]
  1874.    out  dx,al
  1875.    pop  edx
  1876.    ret
  1877.  
  1878.  
  1879.  
  1880. align 4
  1881.  
  1882. sys_midi:
  1883.         cmp  [mididp],0
  1884.         jnz  sm0
  1885.         mov  [esp+36],dword 1
  1886.         ret
  1887. sm0:
  1888.         and  [esp+36],dword 0
  1889.         dec  ebx
  1890.         jnz  smn1
  1891.  ;    call setuart
  1892. su1:
  1893.         call is_output
  1894.         test al,al
  1895.         jnz  su1
  1896.         mov  dx,word [midisp]
  1897.         mov  al,0xff
  1898.         out  dx,al
  1899. su2:
  1900.         mov  dx,word [midisp]
  1901.         mov  al,0xff
  1902.         out  dx,al
  1903.         call is_input
  1904.         test al,al
  1905.         jnz  su2
  1906.         call get_mpu_in
  1907.         cmp  al,0xfe
  1908.         jnz  su2
  1909. su3:
  1910.         call is_output
  1911.         test  al,al
  1912.         jnz  su3
  1913.         mov  dx,word [midisp]
  1914.         mov  al,0x3f
  1915.         out  dx,al
  1916.         ret
  1917. smn1:
  1918.         dec  ebx
  1919.         jnz  smn2
  1920. sm10:
  1921.         call get_mpu_in
  1922.         call is_output
  1923.         test al,al
  1924.         jnz  sm10
  1925.         mov  al,bl
  1926.         call put_mpu_out
  1927.         smn2:
  1928.         ret
  1929.  
  1930. detect_devices:
  1931. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1932. ;include 'detect/commouse.inc'
  1933. ;include 'detect/ps2mouse.inc'
  1934. ;include 'detect/dev_fd.inc'
  1935. ;include 'detect/dev_hdcd.inc'
  1936. ;include 'detect/sear_par.inc'
  1937. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1938.     ret
  1939.  
  1940. sys_end:
  1941.  
  1942.      mov ecx, [current_slot]
  1943.      mov eax, [ecx+APPDATA.tls_base]
  1944.      test eax, eax
  1945.      jz @F
  1946.  
  1947.      stdcall user_free, eax
  1948. @@:
  1949.  
  1950.      mov   eax,[TASK_BASE]
  1951.      mov   [eax+TASKDATA.state], 3  ; terminate this program
  1952.  
  1953.     waitterm:            ; wait here for termination
  1954.      mov   ebx,100
  1955.      call  delay_hs
  1956.      jmp   waitterm
  1957.  
  1958. iglobal
  1959. align 4
  1960. sys_system_table:
  1961.         dd      exit_for_anyone         ; 1 = obsolete
  1962.         dd      sysfn_terminate         ; 2 = terminate thread
  1963.         dd      sysfn_activate          ; 3 = activate window
  1964.         dd      sysfn_getidletime       ; 4 = get idle time
  1965.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1966.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1967.         dd      sysfn_getactive         ; 7 = get active window
  1968.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1969.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1970.         dd      sysfn_minimize          ; 10 = minimize window
  1971.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1972.         dd      sysfn_lastkey           ; 12 = get last pressed key
  1973.         dd      sysfn_getversion        ; 13 = get kernel version
  1974.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1975.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1976.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1977.         dd      sysfn_getallmem         ; 17 = get total memory size
  1978.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1979.                                         ;                 instead of slot
  1980.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1981.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1982.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  1983.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  1984. sysfn_num = ($ - sys_system_table)/4
  1985. endg
  1986.  
  1987. sys_system:
  1988.         dec     ebx
  1989.         cmp     ebx, sysfn_num
  1990.         jae     @f
  1991.         jmp     dword [sys_system_table + ebx*4]
  1992. @@:
  1993.         ret
  1994.  
  1995.  
  1996. sysfn_shutdown:          ; 18.9 = system shutdown
  1997.      cmp  ecx,1
  1998.      jl   exit_for_anyone
  1999.      cmp  ecx,4
  2000.      jg   exit_for_anyone
  2001.      mov  [BOOT_VAR+0x9030],cl
  2002.  
  2003.      mov  eax,[TASK_COUNT]
  2004.      mov  [SYS_SHUTDOWN],al
  2005.      mov  [shutdown_processes],eax
  2006.      and  dword [esp+32], 0
  2007.  exit_for_anyone:
  2008.      ret
  2009.   uglobal
  2010.    shutdown_processes: dd 0x0
  2011.   endg
  2012.  
  2013. sysfn_terminate:        ; 18.2 = TERMINATE
  2014.      cmp  ecx,2
  2015.      jb   noprocessterminate
  2016.      mov  edx,[TASK_COUNT]
  2017.      cmp  ecx,edx
  2018.      ja   noprocessterminate
  2019.      mov  eax,[TASK_COUNT]
  2020.      shl  ecx,5
  2021.      mov  edx,[ecx+CURRENT_TASK+TASKDATA.pid]
  2022.      add  ecx,CURRENT_TASK+TASKDATA.state
  2023.      cmp  byte [ecx], 9
  2024.      jz   noprocessterminate
  2025.  
  2026.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2027.      mov  [ecx],byte 3       ; clear possible i40's
  2028.      ;call MEM_Heap_UnLock
  2029.  
  2030.      cmp  edx,[application_table_status]    ; clear app table stat
  2031.      jne  noatsc
  2032.      and  [application_table_status],0
  2033.    noatsc:
  2034.    noprocessterminate:
  2035.      ret
  2036.  
  2037. sysfn_terminate2:
  2038. ;lock application_table_status mutex
  2039. .table_status:
  2040.     cli
  2041.     cmp    [application_table_status],0
  2042.     je     .stf
  2043.     sti
  2044.     call   change_task
  2045.     jmp    .table_status
  2046. .stf:
  2047.     call   set_application_table_status
  2048.     mov    eax,ecx
  2049.     call   pid_to_slot
  2050.     test   eax,eax
  2051.     jz     .not_found
  2052.     mov    ecx,eax
  2053.     cli
  2054.     call   sysfn_terminate
  2055.     and    [application_table_status],0
  2056.     sti
  2057.     and    dword [esp+32],0
  2058.     ret
  2059. .not_found:
  2060.     mov    [application_table_status],0
  2061.     or     dword [esp+32],-1
  2062.     ret
  2063.  
  2064. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2065.      cmp  ecx,2
  2066.      jb   .nowindowactivate
  2067.      cmp  ecx,[TASK_COUNT]
  2068.      ja   .nowindowactivate
  2069.  
  2070.      mov   [window_minimize], 2   ; restore window if minimized
  2071.  
  2072.      movzx esi, word [WIN_STACK + ecx*2]
  2073.      cmp   esi, [TASK_COUNT]
  2074.      je    .nowindowactivate ; already active
  2075.  
  2076.      mov   edi, ecx
  2077.      shl   edi, 5
  2078.      add   edi, window_data
  2079.      movzx esi, word [WIN_STACK + ecx * 2]
  2080.      lea   esi, [WIN_POS + esi * 2]
  2081.      call  waredraw
  2082. .nowindowactivate:
  2083.      ret
  2084.  
  2085. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2086.      mov  eax,[idleusesec]
  2087.      mov  [esp+32], eax
  2088.      ret
  2089.  
  2090. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2091.      mov  eax,[CPU_FREQ]
  2092.      mov  [esp+32], eax
  2093.      ret
  2094.  
  2095. ;  SAVE ramdisk to /hd/1/menuet.img
  2096. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2097.    include 'blkdev/rdsave.inc'
  2098. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2099. align 4
  2100. sysfn_getactive:        ; 18.7 = get active window
  2101.      mov  eax, [TASK_COUNT]
  2102.    movzx  eax, word [WIN_POS + eax*2]
  2103.      mov  [esp+32],eax
  2104.      ret
  2105.  
  2106. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2107. ;     cmp  ecx,1
  2108.      dec  ecx
  2109.      jnz  nogetsoundflag
  2110.      movzx  eax,byte [sound_flag] ; get sound_flag
  2111.      mov  [esp+32],eax
  2112.      ret
  2113.  nogetsoundflag:
  2114. ;     cmp  ecx,2
  2115.      dec  ecx
  2116.      jnz  nosoundflag
  2117.      xor  byte [sound_flag], 1
  2118.  nosoundflag:
  2119.      ret
  2120.  
  2121. sysfn_minimize:         ; 18.10 = minimize window
  2122.      mov   [window_minimize],1
  2123.      ret
  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.  
  2190.      ret
  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.  
  4124.  
  4125. sys_msg_board_str:
  4126.  
  4127.      pushad
  4128.    @@:
  4129.      cmp    [esi],byte 0
  4130.      je     @f
  4131.      mov    eax,1
  4132.      movzx  ebx,byte [esi]
  4133.      call   sys_msg_board
  4134.      inc    esi
  4135.      jmp    @b
  4136.    @@:
  4137.      popad
  4138.      ret
  4139.  
  4140. sys_msg_board_byte:
  4141. ; in: al = byte to display
  4142. ; out: nothing
  4143. ; destroys: nothing
  4144.         pushad
  4145.         mov     ecx, 2
  4146.         shl     eax, 24
  4147.         jmp     @f
  4148.  
  4149. sys_msg_board_word:
  4150. ; in: ax = word to display
  4151. ; out: nothing
  4152. ; destroys: nothing
  4153.         pushad
  4154.         mov     ecx, 4
  4155.         shl     eax, 16
  4156.         jmp     @f
  4157.  
  4158. sys_msg_board_dword:
  4159. ; in: eax = dword to display
  4160. ; out: nothing
  4161. ; destroys: nothing
  4162.         pushad
  4163.         mov     ecx, 8
  4164. @@:
  4165.         push    ecx
  4166.         rol     eax, 4
  4167.         push    eax
  4168.         and     al, 0xF
  4169.         cmp     al, 10
  4170.         sbb     al, 69h
  4171.         das
  4172.         mov     bl, al
  4173.         xor     eax, eax
  4174.         inc     eax
  4175.         call    sys_msg_board
  4176.         pop     eax
  4177.         pop     ecx
  4178.         loop    @b
  4179.         popad
  4180.         ret
  4181.  
  4182. uglobal
  4183.   msg_board_data: times 4096 db 0
  4184.   msg_board_count dd 0x0
  4185. endg
  4186.  
  4187. sys_msg_board:
  4188.  
  4189. ; eax=1 : write :  bl byte to write
  4190. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4191.  
  4192.         mov     ecx, [msg_board_count]
  4193.         cmp     eax, 1
  4194.         jne     .smbl1
  4195.  
  4196. if defined debug_com_base
  4197.  
  4198.         push    dx ax
  4199.  
  4200.        @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
  4201.         mov     dx, debug_com_base+5
  4202.         in      al, dx
  4203.         test    al, 1 shl 5
  4204.         jz      @r
  4205.  
  4206.         mov     dx, debug_com_base      ; Output the byte
  4207.         mov     al, bl
  4208.         out     dx, al
  4209.  
  4210.         pop     ax dx
  4211.  
  4212. end if
  4213.  
  4214.         mov     [msg_board_data+ecx],bl
  4215.         inc     ecx
  4216.         and     ecx, 4095
  4217.         mov     [msg_board_count], ecx
  4218.         mov     [check_idle_semaphore], 5
  4219.         ret
  4220. .smbl1:
  4221.         cmp     eax, 2
  4222.         jne     .smbl2
  4223.         test    ecx, ecx
  4224.         jz      .smbl21
  4225.         mov     eax, msg_board_data+1
  4226.         mov     ebx, msg_board_data
  4227.         movzx   edx, byte [ebx]
  4228.         call    memmove
  4229.         dec     [msg_board_count]
  4230.         mov     [esp + 36], edx ;eax
  4231.         mov     [esp + 24], dword 1
  4232.         ret
  4233. .smbl21:
  4234.         mov     [esp+36], ecx
  4235.         mov     [esp+24], ecx
  4236. .smbl2:
  4237.         ret
  4238.  
  4239. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4240. ;; 66 sys function.                                                ;;
  4241. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4242. ;; out eax                                                         ;;
  4243. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4244. iglobal
  4245. align 4
  4246. f66call:
  4247.            dd sys_process_def.1   ; 1 = set keyboard mode
  4248.            dd sys_process_def.2   ; 2 = get keyboard mode
  4249.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4250.            dd sys_process_def.4
  4251.            dd sys_process_def.5
  4252. endg
  4253.  
  4254.  
  4255.  
  4256.  
  4257. sys_process_def:
  4258.         dec     ebx
  4259.         cmp     ebx,5
  4260.         jae     .not_support    ;if >=6 then or eax,-1
  4261.  
  4262.         mov     edi, [CURRENT_TASK]
  4263.         jmp     dword [f66call+ebx*4]
  4264.  
  4265. .not_support:
  4266.         or      eax,-1
  4267.         ret
  4268.  
  4269. .1:
  4270.      shl   edi,8
  4271.      mov   [edi+SLOT_BASE + APPDATA.keyboard_mode],cl
  4272.  
  4273.      ret
  4274.  
  4275. .2:                             ; 2 = get keyboard mode
  4276.      shl   edi,8
  4277.      movzx eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4278.      mov   [esp+32],eax
  4279.      ret
  4280. ;     xor   eax,eax
  4281. ;     movzx eax,byte [shift]
  4282. ;     movzx ebx,byte [ctrl]
  4283. ;     shl   ebx,2
  4284. ;     add   eax,ebx
  4285. ;     movzx ebx,byte [alt]
  4286. ;     shl   ebx,3
  4287. ;     add   eax,ebx
  4288. .3:                             ;3 = get keyboard ctrl, alt, shift
  4289.  ;// mike.dld [
  4290.      mov   eax, [kb_state]
  4291.  ;// mike.dld ]
  4292.      mov   [esp+32],eax
  4293.      ret
  4294.  
  4295. .4:
  4296.         mov     eax, hotkey_list
  4297. @@:
  4298.         cmp     dword [eax+8], 0
  4299.         jz      .found_free
  4300.         add     eax, 16
  4301.         cmp     eax, hotkey_list+16*256
  4302.         jb      @b
  4303.         mov     dword [esp+32], 1
  4304.         ret
  4305. .found_free:
  4306.         mov     [eax+8], edi
  4307.         mov     [eax+4], edx
  4308.         movzx   ecx, cl
  4309.         lea     ecx, [hotkey_scancodes+ecx*4]
  4310.         mov     edx, [ecx]
  4311.         mov     [eax], edx
  4312.         mov     [ecx], eax
  4313.         mov     [eax+12], ecx
  4314.         jecxz   @f
  4315.         mov     [edx+12], eax
  4316. @@:
  4317.         and     dword [esp+32], 0
  4318.         ret
  4319.  
  4320. .5:
  4321.         movzx   ebx, cl
  4322.         lea     ebx, [hotkey_scancodes+ebx*4]
  4323.         mov     eax, [ebx]
  4324. .scan:
  4325.         test    eax, eax
  4326.         jz      .notfound
  4327.         cmp     [eax+8], edi
  4328.         jnz     .next
  4329.         cmp     [eax+4], edx
  4330.         jz      .found
  4331. .next:
  4332.         mov     eax, [eax]
  4333.         jmp     .scan
  4334. .notfound:
  4335.         mov     dword [esp+32], 1
  4336.         ret
  4337. .found:
  4338.         mov     ecx, [eax]
  4339.         jecxz   @f
  4340.         mov     edx, [eax+12]
  4341.         mov     [ecx+12], edx
  4342. @@:
  4343.         mov     ecx, [eax+12]
  4344.         mov     edx, [eax]
  4345.         mov     [ecx], edx
  4346.         xor     edx, edx
  4347.         mov     [eax+4], edx
  4348.         mov     [eax+8], edx
  4349.         mov     [eax+12], edx
  4350.         mov     [eax], edx
  4351.         mov     [esp+32], edx
  4352.         ret
  4353.  
  4354.  
  4355. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4356. ;; 61 sys function.                                                ;;
  4357. ;; in eax=61,ebx in [1..3]                                         ;;
  4358. ;; out eax                                                         ;;
  4359. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4360. iglobal
  4361. align 4
  4362. f61call:
  4363.            dd sys_gs.1   ; resolution
  4364.            dd sys_gs.2   ; bits per pixel
  4365.            dd sys_gs.3   ; bytes per scanline
  4366. endg
  4367.  
  4368.  
  4369. align 4
  4370.  
  4371. sys_gs:                         ; direct screen access
  4372.         dec     ebx
  4373.         cmp     ebx,2
  4374.         ja      .not_support
  4375.         jmp     dword [f61call+ebx*4]
  4376. .not_support:
  4377.         or  [esp+32],dword -1
  4378.         ret
  4379.  
  4380.  
  4381. .1:                             ; resolution
  4382.      mov  eax,[Screen_Max_X]
  4383.      shl  eax,16
  4384.      mov  ax,[Screen_Max_Y]
  4385.      add  eax,0x00010001
  4386.      mov  [esp+32],eax
  4387.      ret
  4388. .2:                             ; bits per pixel
  4389.      movzx eax,byte [ScreenBPP]
  4390.      mov   [esp+32],eax
  4391.      ret
  4392. .3:                             ; bytes per scanline
  4393.      mov   eax,[BytesPerScanLine]
  4394.      mov   [esp+32],eax
  4395.      ret
  4396.  
  4397. align 4  ;  system functions
  4398.  
  4399. syscall_setpixel:                       ; SetPixel
  4400.  
  4401.         mov     eax, ebx
  4402.         mov     ebx, ecx
  4403.         mov     ecx, edx
  4404.         mov     edx, [TASK_BASE]
  4405.         add     eax, [edx-twdw+WDATA.box.left]
  4406.         add     ebx, [edx-twdw+WDATA.box.top]
  4407.         mov     edi, [current_slot]
  4408.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  4409.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  4410.         xor     edi, edi ; no force
  4411. ;       mov     edi, 1
  4412.         call    [_display.disable_mouse]
  4413.         jmp     [putpixel]
  4414.  
  4415. align 4
  4416.  
  4417. syscall_writetext:                      ; WriteText
  4418.  
  4419.         mov   eax,[TASK_BASE]
  4420.         mov   ebp,[eax-twdw+WDATA.box.left]
  4421.         push  esi
  4422.         mov   esi,[current_slot]
  4423.         add   ebp,[esi+APPDATA.wnd_clientbox.left]
  4424.         shl   ebp,16
  4425.         add   ebp,[eax-twdw+WDATA.box.top]
  4426.         add   bp,word[esi+APPDATA.wnd_clientbox.top]
  4427.         pop   esi
  4428.         add   ebx,ebp
  4429.         mov   eax,edi
  4430.         xor   edi,edi
  4431.         jmp   dtext
  4432.  
  4433. align 4
  4434.  
  4435. syscall_openramdiskfile:                ; OpenRamdiskFile
  4436.  
  4437.         mov     eax, ebx
  4438.         mov     ebx, ecx
  4439.         mov     ecx, edx
  4440.         mov     edx, esi
  4441.         mov     esi, 12
  4442.         call    fileread
  4443.         mov     [esp+32], eax
  4444.         ret
  4445.  
  4446. align 4
  4447.  
  4448. syscall_drawrect:                       ; DrawRect
  4449.  
  4450.         mov     edi, edx ; color + gradient
  4451.         and     edi, 0x80FFFFFF
  4452.         test    bx, bx  ; x.size
  4453.         je      .drectr
  4454.         test    cx, cx ; y.size
  4455.         je      .drectr
  4456.  
  4457.         mov     eax, ebx ; bad idea
  4458.         mov     ebx, ecx
  4459.  
  4460.         movzx   ecx, ax ; ecx - x.size
  4461.         shr     eax, 16 ; eax - x.coord
  4462.         movzx   edx, bx ; edx - y.size
  4463.         shr     ebx, 16 ; ebx - y.coord
  4464.         mov     esi, [current_slot]
  4465.  
  4466.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  4467.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  4468.         add     ecx, eax
  4469.         add     edx, ebx
  4470.         jmp     [drawbar]
  4471. .drectr:
  4472.         ret
  4473.  
  4474. align 4
  4475. syscall_getscreensize:                  ; GetScreenSize
  4476.         mov     ax, [Screen_Max_X]
  4477.         shl     eax, 16
  4478.         mov     ax, [Screen_Max_Y]
  4479.         mov     [esp + 32], eax
  4480.         ret
  4481.  
  4482. align 4
  4483.  
  4484. syscall_cdaudio:                        ; CD
  4485.  
  4486.         cmp     ebx, 4
  4487.         jb      .audio
  4488.         jz      .eject
  4489.         cmp     ebx, 5
  4490.         jnz     .ret
  4491. .load:
  4492.         call    .reserve
  4493.         call    LoadMedium
  4494.         ;call    .free
  4495.                 jmp             .free
  4496. ;        ret
  4497. .eject:
  4498.         call    .reserve
  4499.         call    clear_CD_cache
  4500.         call    allow_medium_removal
  4501.         call    EjectMedium
  4502. ;        call    .free
  4503.                 jmp             .free
  4504. ;        ret
  4505. .audio:
  4506.      call  sys_cd_audio
  4507.      mov   [esp+36-4],eax
  4508. .ret:
  4509.      ret
  4510.  
  4511. .reserve:
  4512.         call    reserve_cd
  4513.         mov     eax, ecx
  4514.         shr     eax, 1
  4515.         and     eax, 1
  4516.         inc     eax
  4517.         mov     [ChannelNumber], ax
  4518.         mov     eax, ecx
  4519.         and     eax, 1
  4520.         mov     [DiskNumber], al
  4521.         call    reserve_cd_channel
  4522.         and     ebx, 3
  4523.         inc     ebx
  4524.         mov     [cdpos], ebx
  4525.         add     ebx, ebx
  4526.         mov     cl, 8
  4527.         sub     cl, bl
  4528.         mov     al, [DRIVE_DATA+1]
  4529.         shr     al, cl
  4530.         test    al, 2
  4531.         jz      .free;.err
  4532.         ret
  4533. .free:
  4534.         call    free_cd_channel
  4535.         and     [cd_status], 0
  4536.         ret
  4537. .err:
  4538.         call    .free
  4539. ;        pop     eax
  4540.         ret
  4541.  
  4542. align 4
  4543.  
  4544. syscall_getpixel:                       ; GetPixel
  4545.      mov   ecx, [Screen_Max_X]
  4546.      inc   ecx
  4547.      xor   edx, edx
  4548.      mov   eax, ebx
  4549.      div   ecx
  4550.      mov   ebx, edx
  4551.      xchg  eax, ebx
  4552.      call  dword [GETPIXEL] ; eax - x, ebx - y
  4553.      mov   [esp + 32], ecx
  4554.      ret
  4555.  
  4556. align 4
  4557.  
  4558. syscall_getarea:
  4559. ;eax = 36
  4560. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4561. ;ecx = [size x]*65536 + [size y]
  4562. ;edx = [start x]*65536 + [start y]
  4563.      pushad
  4564.          inc   [mouse_pause]
  4565. ; Check of use of the hardware cursor.
  4566.       cmp  [_display.disable_mouse],__sys_disable_mouse
  4567.           jne  @f
  4568. ; Since the test for the coordinates of the mouse should not be used,
  4569. ; then use the call [disable_mouse] is not possible!
  4570.       cmp  dword [MOUSE_VISIBLE],dword 0
  4571.       jne  @f
  4572.       pushf
  4573.       cli
  4574.       call draw_mouse_under
  4575.       popf
  4576.       mov  [MOUSE_VISIBLE],dword 1
  4577. @@:
  4578.      mov   edi,ebx
  4579.      mov   eax,edx
  4580.      shr   eax,16
  4581.      mov   ebx,edx
  4582.      and   ebx,0xffff
  4583.      dec   eax
  4584.            dec   ebx
  4585.      ; eax - x, ebx - y
  4586.      mov   edx,ecx
  4587.  
  4588.      shr   ecx,16
  4589.      and   edx,0xffff
  4590.      mov   esi,ecx
  4591.      ; ecx - size x, edx - size y
  4592.  
  4593.          mov   ebp,edx
  4594.          dec   ebp
  4595.      lea   ebp,[ebp*3]
  4596.  
  4597.          imul  ebp,esi
  4598.  
  4599.          mov   esi,ecx
  4600.          dec   esi
  4601.          lea   esi,[esi*3]
  4602.  
  4603.      add   ebp,esi
  4604.      add   ebp,edi
  4605.  
  4606.      add   ebx,edx
  4607.  
  4608. .start_y:
  4609.      push  ecx edx
  4610. .start_x:
  4611.      push  eax ebx ecx
  4612.      add   eax,ecx
  4613.  
  4614.      call  dword [GETPIXEL] ; eax - x, ebx - y
  4615.  
  4616.      mov   [ebp],cx
  4617.      shr   ecx,16
  4618.      mov   [ebp+2],cl
  4619.  
  4620.      pop   ecx ebx eax
  4621.      sub   ebp,3
  4622.      dec   ecx
  4623.      jnz   .start_x
  4624.          pop   edx ecx
  4625.          dec   ebx
  4626.      dec   edx
  4627.      jnz   .start_y
  4628.      dec        [mouse_pause]
  4629. ; Check of use of the hardware cursor.
  4630.       cmp  [_display.disable_mouse],__sys_disable_mouse
  4631.           jne  @f
  4632.          call  [draw_pointer]
  4633. @@:
  4634.      popad
  4635.      ret
  4636.  
  4637. align 4
  4638.  
  4639. syscall_drawline:                       ; DrawLine
  4640.  
  4641.         mov     edi, [TASK_BASE]
  4642.         movzx   eax, word[edi-twdw+WDATA.box.left]
  4643.         mov     ebp, eax
  4644.         mov     esi, [current_slot]
  4645.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4646.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  4647.         add     ebp,ebx
  4648.         shl     eax, 16
  4649.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  4650.         add     eax, ebp
  4651.         mov     ebp, ebx
  4652.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  4653.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  4654.         add     ebp, ecx
  4655.         shl     ebx, 16
  4656.         xor     edi, edi
  4657.         add     ebx, ebp
  4658.         mov     ecx, edx
  4659.         jmp     [draw_line]
  4660.  
  4661.  
  4662.  
  4663. align 4
  4664. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  4665.  
  4666.      call  r_f_port_area
  4667.      mov   [esp+32],eax
  4668.      ret
  4669.  
  4670. align 4
  4671. syscall_threads:                        ; CreateThreads
  4672. ; eax=1 create thread
  4673. ;
  4674. ;   ebx=thread start
  4675. ;   ecx=thread stack value
  4676. ;
  4677. ; on return : eax = pid
  4678.  
  4679.      call  new_sys_threads
  4680.  
  4681.      mov   [esp+32],eax
  4682.      ret
  4683.  
  4684. align 4
  4685.  
  4686. stack_driver_stat:
  4687.  
  4688.      call  app_stack_handler            ; Stack status
  4689.  
  4690. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4691. ;     call  change_task                 ; between sent packet
  4692.  
  4693.      mov   [esp+32],eax
  4694.      ret
  4695.  
  4696. align 4
  4697.  
  4698. socket:                                 ; Socket interface
  4699.      call  app_socket_handler
  4700.  
  4701. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4702. ;     call  change_task                 ; between sent packet
  4703.  
  4704.      mov   [esp+36],eax
  4705.      mov   [esp+24],ebx
  4706.      ret
  4707.  
  4708. align 4
  4709.  
  4710. read_from_hd:                           ; Read from hd - fn not in use
  4711.  
  4712.      mov   edi,[TASK_BASE]
  4713.      add   edi,TASKDATA.mem_start
  4714.      add   eax,[edi]
  4715.      add   ecx,[edi]
  4716.      add   edx,[edi]
  4717.      call  file_read
  4718.  
  4719.      mov   [esp+36],eax
  4720.      mov   [esp+24],ebx
  4721.  
  4722.      ret
  4723.  
  4724. paleholder:
  4725.         ret
  4726.  
  4727. align 4
  4728. set_screen:
  4729.         cmp eax, [Screen_Max_X]
  4730.         jne .set
  4731.  
  4732.         cmp edx, [Screen_Max_Y]
  4733.         jne .set
  4734.         ret
  4735. .set:
  4736.         pushfd
  4737.         cli
  4738.  
  4739.         mov [Screen_Max_X], eax
  4740.         mov [Screen_Max_Y], edx
  4741.         mov [BytesPerScanLine], ecx
  4742.  
  4743.         mov [screen_workarea.right],eax
  4744.         mov [screen_workarea.bottom], edx
  4745.  
  4746.         push ebx
  4747.         push esi
  4748.         push edi
  4749.  
  4750.         pushad
  4751.  
  4752.         stdcall kernel_free, [_WinMapAddress]
  4753.  
  4754.         mov eax, [_display.width]
  4755.         mul [_display.height]
  4756.         mov [_WinMapSize], eax
  4757.  
  4758.         stdcall kernel_alloc, eax
  4759.         mov [_WinMapAddress], eax
  4760.         test eax, eax
  4761.         jz .epic_fail
  4762.  
  4763.         popad
  4764.  
  4765.         call    repos_windows
  4766.         xor eax, eax
  4767.         xor ebx, ebx
  4768.         mov     ecx, [Screen_Max_X]
  4769.         mov     edx, [Screen_Max_Y]
  4770.         call    calculatescreen
  4771.         pop edi
  4772.         pop esi
  4773.         pop ebx
  4774.  
  4775.         popfd
  4776.         ret
  4777.  
  4778. .epic_fail:
  4779.         hlt                     ; Houston, we've had a problem
  4780.  
  4781. ; --------------- APM ---------------------
  4782. uglobal
  4783. apm_entry       dp      0
  4784. apm_vf          dd      0
  4785. endg
  4786.  
  4787. align 4
  4788. sys_apm:
  4789.         xor     eax,eax
  4790.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4791.         jne     @f
  4792.         inc     eax
  4793.         or      dword [esp + 44], eax   ; error
  4794.         add     eax,7
  4795.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  4796.         ret
  4797.  
  4798. @@:
  4799. ;       xchg    eax, ecx
  4800. ;       xchg    ebx, ecx
  4801.  
  4802.         cmp     dx, 3
  4803.         ja      @f
  4804.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4805.         mov     eax,[apm_vf]
  4806.         mov     [esp + 32], eax
  4807.         shr     eax, 16
  4808.         mov     [esp + 28], eax
  4809.         ret
  4810.  
  4811. @@:
  4812.  
  4813.         mov     esi,[master_tab+(OS_BASE shr 20)]
  4814.         xchg    [master_tab], esi
  4815.         push    esi
  4816.         mov     edi, cr3
  4817.         mov     cr3, edi                ;flush TLB
  4818.  
  4819.         call    pword [apm_entry]       ;call APM BIOS
  4820.  
  4821.         xchg    eax, [esp]
  4822.         mov     [master_tab], eax
  4823.         mov     eax, cr3
  4824.         mov     cr3, eax
  4825.         pop eax
  4826.  
  4827.         mov     [esp + 4 ], edi
  4828.         mov     [esp + 8], esi
  4829.         mov     [esp + 20], ebx
  4830.         mov     [esp + 24], edx
  4831.         mov     [esp + 28], ecx
  4832.         mov     [esp + 32], eax
  4833.         setc    al
  4834.         and     [esp + 44], byte 0xfe
  4835.         or      [esp + 44], al
  4836.         ret
  4837. ; -----------------------------------------
  4838.  
  4839. align 4
  4840.  
  4841. undefined_syscall:                      ; Undefined system call
  4842.      mov   [esp + 32], dword -1
  4843.      ret
  4844.  
  4845. align 4
  4846. system_shutdown:          ; shut down the system
  4847.  
  4848.            cmp byte [BOOT_VAR+0x9030], 1
  4849.            jne @F
  4850.            ret
  4851. @@:
  4852.            call stop_all_services
  4853.            push 3                ; stop playing cd
  4854.            pop  eax
  4855.            call sys_cd_audio
  4856.  
  4857. yes_shutdown_param:
  4858.            cli
  4859.  
  4860. if ~ defined extended_primary_loader
  4861.            mov  eax, kernel_file ; load kernel.mnt to 0x7000:0
  4862.            push 12
  4863.            pop  esi
  4864.            xor  ebx,ebx
  4865.            or   ecx,-1
  4866.            mov  edx, OS_BASE+0x70000
  4867.            call fileread
  4868.  
  4869.            mov  esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  4870.            mov  edi,OS_BASE+0x40000
  4871.            mov  ecx,1000
  4872.            rep  movsb
  4873. end if
  4874.  
  4875.            mov  esi, BOOT_VAR    ; restore 0x0 - 0xffff
  4876.            mov  edi, OS_BASE
  4877.            mov  ecx,0x10000/4
  4878.            cld
  4879.            rep movsd
  4880.  
  4881.            call restorefatchain
  4882.  
  4883.            mov al, 0xFF
  4884.            out 0x21, al
  4885.            out 0xA1, al
  4886.  
  4887. if 0
  4888.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  4889.            mov  word [OS_BASE+0x467+2],0x1000
  4890.  
  4891.            mov  al,0x0F
  4892.            out  0x70,al
  4893.            mov  al,0x05
  4894.            out  0x71,al
  4895.  
  4896.            mov  al,0xFE
  4897.            out  0x64,al
  4898.  
  4899.            hlt
  4900.            jmp $-1
  4901.  
  4902. else
  4903.         cmp     byte [OS_BASE + 0x9030], 2
  4904.         jnz     no_acpi_power_off
  4905.  
  4906. ; scan for RSDP
  4907. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  4908.         movzx   eax, word [OS_BASE + 0x40E]
  4909.         shl     eax, 4
  4910.         jz      @f
  4911.         mov     ecx, 1024/16
  4912.         call    scan_rsdp
  4913.         jnc     .rsdp_found
  4914. @@:
  4915. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  4916.         mov     eax, 0xE0000
  4917.         mov     ecx, 0x2000
  4918.         call    scan_rsdp
  4919.         jc      no_acpi_power_off
  4920. .rsdp_found:
  4921.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  4922.         mov     ebp, [ipc_tmp]
  4923.         stdcall map_page, ebp, esi, PG_MAP
  4924.         lea     eax, [esi+1000h]
  4925.         lea     edx, [ebp+1000h]
  4926.         stdcall map_page, edx, eax, PG_MAP
  4927.         and     esi, 0xFFF
  4928.         add     esi, ebp
  4929.         cmp     dword [esi], 'RSDT'
  4930.         jnz     no_acpi_power_off
  4931.         mov     ecx, [esi+4]
  4932.         sub     ecx, 24h
  4933.         jbe     no_acpi_power_off
  4934.         shr     ecx, 2
  4935.         add     esi, 24h
  4936. .scan_fadt:
  4937.         lodsd
  4938.         mov     ebx, eax
  4939.         lea     eax, [ebp+2000h]
  4940.         stdcall map_page, eax, ebx, PG_MAP
  4941.         lea     eax, [ebp+3000h]
  4942.         add     ebx, 0x1000
  4943.         stdcall map_page, eax, ebx, PG_MAP
  4944.         and     ebx, 0xFFF
  4945.         lea     ebx, [ebx+ebp+2000h]
  4946.         cmp     dword [ebx], 'FACP'
  4947.         jz      .fadt_found
  4948.         loop    .scan_fadt
  4949.         jmp     no_acpi_power_off
  4950. .fadt_found:
  4951. ; ebx is linear address of FADT
  4952.         mov     edi, [ebx+40] ; physical address of the DSDT
  4953.         lea     eax, [ebp+4000h]
  4954.         stdcall map_page, eax, edi, PG_MAP
  4955.         lea     eax, [ebp+5000h]
  4956.         lea     esi, [edi+0x1000]
  4957.         stdcall map_page, eax, esi, PG_MAP
  4958.         and     esi, 0xFFF
  4959.         sub     edi, esi
  4960.         cmp     dword [esi+ebp+4000h], 'DSDT'
  4961.         jnz     no_acpi_power_off
  4962.         mov     eax, [esi+ebp+4004h] ; DSDT length
  4963.         sub     eax, 36+4
  4964.         jbe     no_acpi_power_off
  4965.         add     esi, 36
  4966. .scan_dsdt:
  4967.         cmp     dword [esi+ebp+4000h], '_S5_'
  4968.         jnz     .scan_dsdt_cont
  4969.         cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
  4970.         jnz     .scan_dsdt_cont
  4971.         mov     dl, [esi+ebp+4000h+6]
  4972.         cmp     dl, 4 ; _S5_ package must contain 4 bytes
  4973.                       ; ...in theory; in practice, VirtualBox has 2 bytes
  4974.         ja      .scan_dsdt_cont
  4975.         cmp     dl, 1
  4976.         jb      .scan_dsdt_cont
  4977.         lea     esi, [esi+ebp+4000h+7]
  4978.         xor     ecx, ecx
  4979.         cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
  4980.         jz      @f
  4981.         cmp     byte [esi], 0xA
  4982.         jnz     no_acpi_power_off
  4983.         inc     esi
  4984.         mov     cl, [esi]
  4985. @@:
  4986.         inc     esi
  4987.         cmp     dl, 2
  4988.         jb      @f
  4989.         cmp     byte [esi], 0
  4990.         jz      @f
  4991.         cmp     byte [esi], 0xA
  4992.         jnz     no_acpi_power_off
  4993.         inc     esi
  4994.         mov     ch, [esi]
  4995. @@:
  4996.         jmp     do_acpi_power_off
  4997. .scan_dsdt_cont:
  4998.         inc     esi
  4999.         cmp     esi, 0x1000
  5000.         jb      @f
  5001.         sub     esi, 0x1000
  5002.         add     edi, 0x1000
  5003.         push    eax
  5004.         lea     eax, [ebp+4000h]
  5005.         stdcall map_page, eax, edi, PG_MAP
  5006.         push    PG_MAP
  5007.         lea     eax, [edi+1000h]
  5008.         push    eax
  5009.         lea     eax, [ebp+5000h]
  5010.         push    eax
  5011.         stdcall map_page
  5012.         pop     eax
  5013. @@:
  5014.         dec     eax
  5015.         jnz     .scan_dsdt
  5016.         jmp     no_acpi_power_off
  5017. do_acpi_power_off:
  5018.         mov     edx, [ebx+48]
  5019.         test    edx, edx
  5020.         jz      .nosmi
  5021.         mov     al, [ebx+52]
  5022.         out     dx, al
  5023.         mov     edx, [ebx+64]
  5024. @@:
  5025.         in      ax, dx
  5026.         test    al, 1
  5027.         jz      @b
  5028. .nosmi:
  5029.         and     cx, 0x0707
  5030.         shl     cx, 2
  5031.         or      cx, 0x2020
  5032.         mov     edx, [ebx+64]
  5033.         in      ax, dx
  5034.         and     ax, 203h
  5035.         or      ah, cl
  5036.         out     dx, ax
  5037.         mov     edx, [ebx+68]
  5038.         test    edx, edx
  5039.         jz      @f
  5040.         in      ax, dx
  5041.         and     ax, 203h
  5042.         or      ah, ch
  5043.         out     dx, ax
  5044. @@:
  5045.         jmp     $
  5046.  
  5047.  
  5048. no_acpi_power_off:
  5049.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5050.            mov  word [OS_BASE+0x467+2],0x1000
  5051.  
  5052.            mov  al,0x0F
  5053.            out  0x70,al
  5054.            mov  al,0x05
  5055.            out  0x71,al
  5056.  
  5057.            mov  al,0xFE
  5058.            out  0x64,al
  5059.  
  5060.            hlt
  5061.            jmp $-1
  5062.  
  5063. scan_rsdp:
  5064.         add     eax, OS_BASE
  5065. .s:
  5066.         cmp     dword [eax], 'RSD '
  5067.         jnz     .n
  5068.         cmp     dword [eax+4], 'PTR '
  5069.         jnz     .n
  5070.         xor     edx, edx
  5071.         xor     esi, esi
  5072. @@:
  5073.         add     dl, [eax+esi]
  5074.         inc     esi
  5075.         cmp     esi, 20
  5076.         jnz     @b
  5077.         test    dl, dl
  5078.         jz      .ok
  5079. .n:
  5080.         add     eax, 10h
  5081.         loop    .s
  5082.         stc
  5083. .ok:
  5084.         ret
  5085. end if
  5086.  
  5087. include "data32.inc"
  5088.  
  5089. __REV__ = __REV
  5090.  
  5091. uglobals_size = $ - endofcode
  5092. diff16 "end of kernel code",0,$
  5093.