Subversion Repositories Kolibri OS

Rev

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

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