Subversion Repositories Kolibri OS

Rev

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

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