Subversion Repositories Kolibri OS

Rev

Rev 2430 | Rev 2436 | 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: 2435 $
  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.         call    restore_default_cursor_before_killing
  1930.         popa
  1931. @@:
  1932. ;--------------------------------------
  1933.         mov     ecx, [current_slot]
  1934.         mov     eax, [ecx+APPDATA.tls_base]
  1935.         test    eax, eax
  1936.         jz      @F
  1937.  
  1938.         stdcall user_free, eax
  1939. @@:
  1940.  
  1941.         mov     eax, [TASK_BASE]
  1942.         mov     [eax+TASKDATA.state], 3; terminate this program
  1943.  
  1944.     waitterm:            ; wait here for termination
  1945.         mov     ebx, 100
  1946.         call    delay_hs
  1947.         jmp     waitterm
  1948. ;------------------------------------------------------------------------------
  1949. restore_default_cursor_before_killing:
  1950.         mov     eax, [def_cursor]
  1951.         mov     [ecx+APPDATA.cursor], eax
  1952.  
  1953.         movzx   eax, word [MOUSE_Y]
  1954.         movzx   ebx, word [MOUSE_X]
  1955.         mov     ecx, [Screen_Max_X]
  1956.         inc     ecx
  1957.         mul     ecx
  1958.         add     eax, [_WinMapAddress]
  1959.         movzx   edx, byte [ebx+eax]
  1960.         shl     edx, 8
  1961.         mov     esi, [edx+SLOT_BASE+APPDATA.cursor]
  1962.         push    esi
  1963.         call    [_display.select_cursor]
  1964.         mov     [current_cursor], esi
  1965.         ret
  1966. ;------------------------------------------------------------------------------
  1967. iglobal
  1968. align 4
  1969. sys_system_table:
  1970.         dd      sysfn_deactivate        ; 1 = deactivate window
  1971.         dd      sysfn_terminate         ; 2 = terminate thread
  1972.         dd      sysfn_activate          ; 3 = activate window
  1973.         dd      sysfn_getidletime       ; 4 = get idle time
  1974.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1975.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1976.         dd      sysfn_getactive         ; 7 = get active window
  1977.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1978.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1979.         dd      sysfn_minimize          ; 10 = minimize window
  1980.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1981.         dd      sysfn_lastkey           ; 12 = get last pressed key
  1982.         dd      sysfn_getversion        ; 13 = get kernel version
  1983.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1984.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1985.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1986.         dd      sysfn_getallmem         ; 17 = get total memory size
  1987.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1988.                                         ;                 instead of slot
  1989.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1990.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1991.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  1992.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  1993. sysfn_num = ($ - sys_system_table)/4
  1994. endg
  1995. ;------------------------------------------------------------------------------
  1996. sys_system:
  1997.         dec     ebx
  1998.         cmp     ebx, sysfn_num
  1999.         jae     @f
  2000.         jmp     dword [sys_system_table + ebx*4]
  2001. @@:
  2002.         ret
  2003. ;------------------------------------------------------------------------------
  2004. sysfn_shutdown:          ; 18.9 = system shutdown
  2005.         cmp     ecx, 1
  2006.         jl      exit_for_anyone
  2007.         cmp     ecx, 4
  2008.         jg      exit_for_anyone
  2009.         mov     [BOOT_VAR+0x9030], cl
  2010.  
  2011.         mov     eax, [TASK_COUNT]
  2012.         mov     [SYS_SHUTDOWN], al
  2013.         mov     [shutdown_processes], eax
  2014.         and     dword [esp+32], 0
  2015.  exit_for_anyone:
  2016.         ret
  2017.   uglobal
  2018.    shutdown_processes:
  2019.                        dd 0x0
  2020.   endg
  2021. ;------------------------------------------------------------------------------
  2022. sysfn_terminate:        ; 18.2 = TERMINATE
  2023.         cmp     ecx, 2
  2024.         jb      noprocessterminate
  2025.         mov     edx, [TASK_COUNT]
  2026.         cmp     ecx, edx
  2027.         ja      noprocessterminate
  2028.         mov     eax, [TASK_COUNT]
  2029.         push    ecx
  2030.         shl     ecx, 5
  2031.         mov     edx, [ecx+CURRENT_TASK+TASKDATA.pid]
  2032.         add     ecx, CURRENT_TASK+TASKDATA.state
  2033.         cmp     byte [ecx], 9
  2034.         jz      noprocessterminate
  2035. ;--------------------------------------
  2036.         cmp     [_display.select_cursor], 0
  2037.         je      @f
  2038. ; restore default cursor before killing
  2039.         pusha
  2040.         mov     ecx, [esp+32]
  2041.         shl     ecx, 8
  2042.         add     ecx, SLOT_BASE
  2043.         call    restore_default_cursor_before_killing
  2044.         popa
  2045. @@:
  2046.         add     esp, 4
  2047. ;--------------------------------------
  2048.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2049.         mov     [ecx], byte 3; clear possible i40's
  2050.      ;call MEM_Heap_UnLock
  2051.  
  2052.         cmp     edx, [application_table_status]; clear app table stat
  2053.         jne     noatsc
  2054.         and     [application_table_status], 0
  2055.    noatsc:
  2056.    noprocessterminate:
  2057.         ret
  2058. ;------------------------------------------------------------------------------
  2059. sysfn_terminate2:
  2060. ;lock application_table_status mutex
  2061. .table_status:
  2062.         cli
  2063.         cmp     [application_table_status], 0
  2064.         je      .stf
  2065.         sti
  2066.         call    change_task
  2067.         jmp     .table_status
  2068. .stf:
  2069.         call    set_application_table_status
  2070.         mov     eax, ecx
  2071.         call    pid_to_slot
  2072.         test    eax, eax
  2073.         jz      .not_found
  2074.         mov     ecx, eax
  2075.         cli
  2076.         call    sysfn_terminate
  2077.         and     [application_table_status], 0
  2078.         sti
  2079.         and     dword [esp+32], 0
  2080.         ret
  2081. .not_found:
  2082.         mov     [application_table_status], 0
  2083.         or      dword [esp+32], -1
  2084.         ret
  2085. ;------------------------------------------------------------------------------
  2086. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  2087.         cmp     ecx, 2
  2088.         jb      .nowindowdeactivate
  2089.         cmp     ecx, [TASK_COUNT]
  2090.         ja      .nowindowdeactivate
  2091.  
  2092.         movzx   esi, word [WIN_STACK + ecx*2]
  2093.         cmp     esi, 1
  2094.         je      .nowindowdeactivate ; already deactive
  2095.  
  2096.         mov     edi, ecx
  2097.         shl     edi, 5
  2098.         add     edi, window_data
  2099.         movzx   esi, word [WIN_STACK + ecx * 2]
  2100.         lea     esi, [WIN_POS + esi * 2]
  2101.         call    window._.window_deactivate
  2102.  
  2103.         xor     eax, eax
  2104.         mov     byte[MOUSE_BACKGROUND], al
  2105.         mov     byte[DONT_DRAW_MOUSE], al
  2106.         mov     byte[MOUSE_DOWN], 0
  2107.  
  2108.         call    syscall_display_settings._.calculate_whole_screen
  2109.         call    syscall_display_settings._.redraw_whole_screen
  2110. .nowindowdeactivate:
  2111.         ret
  2112.  ;------------------------------------------------------------------------------
  2113. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2114.         cmp     ecx, 2
  2115.         jb      .nowindowactivate
  2116.         cmp     ecx, [TASK_COUNT]
  2117.         ja      .nowindowactivate
  2118.  
  2119.         mov     [window_minimize], 2; restore window if minimized
  2120.  
  2121.         movzx   esi, word [WIN_STACK + ecx*2]
  2122.         cmp     esi, [TASK_COUNT]
  2123.         je      .nowindowactivate; already active
  2124.  
  2125.         mov     edi, ecx
  2126.         shl     edi, 5
  2127.         add     edi, window_data
  2128.         movzx   esi, word [WIN_STACK + ecx * 2]
  2129.         lea     esi, [WIN_POS + esi * 2]
  2130.         call    waredraw
  2131. .nowindowactivate:
  2132.         ret
  2133. ;------------------------------------------------------------------------------
  2134. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2135.         mov     eax, [idleusesec]
  2136.         mov     [esp+32], eax
  2137.         ret
  2138. ;------------------------------------------------------------------------------
  2139. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2140.         mov     eax, [CPU_FREQ]
  2141.         mov     [esp+32], eax
  2142.         ret
  2143. ;------------------------------------------------------------------------------
  2144. ;  SAVE ramdisk to /hd/1/menuet.img
  2145. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2146.    include 'blkdev/rdsave.inc'
  2147. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2148. ;------------------------------------------------------------------------------
  2149. align 4
  2150. sysfn_getactive:        ; 18.7 = get active window
  2151.         mov     eax, [TASK_COUNT]
  2152.         movzx   eax, word [WIN_POS + eax*2]
  2153.         mov     [esp+32], eax
  2154.         ret
  2155. ;------------------------------------------------------------------------------
  2156. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2157. ;     cmp  ecx,1
  2158.         dec     ecx
  2159.         jnz     nogetsoundflag
  2160.         movzx   eax, byte [sound_flag]; get sound_flag
  2161.         mov     [esp+32], eax
  2162.         ret
  2163.  nogetsoundflag:
  2164. ;     cmp  ecx,2
  2165.         dec     ecx
  2166.         jnz     nosoundflag
  2167.         xor     byte [sound_flag], 1
  2168.  nosoundflag:
  2169.         ret
  2170. ;------------------------------------------------------------------------------
  2171. sysfn_minimize:         ; 18.10 = minimize window
  2172.         mov     [window_minimize], 1
  2173.         ret
  2174. ;------------------------------------------------------------------------------
  2175. align 4
  2176. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2177. ;     cmp  ecx,1
  2178.         dec     ecx
  2179.         jnz     full_table
  2180.   small_table:
  2181.         call    for_all_tables
  2182.         mov     ecx, 10
  2183.         cld
  2184.         rep movsb
  2185.         ret
  2186.    for_all_tables:
  2187.         mov     edi, edx
  2188.         mov     esi, DRIVE_DATA
  2189.         ret
  2190.   full_table:
  2191. ;     cmp  ecx,2
  2192.         dec     ecx
  2193.         jnz     exit_for_anyone
  2194.         call    for_all_tables
  2195.         mov     ecx, 16384
  2196.         cld
  2197.         rep movsd
  2198.         ret
  2199. ;------------------------------------------------------------------------------
  2200. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2201.         and     dword [esp+32], 0
  2202.         ret
  2203. ;------------------------------------------------------------------------------
  2204. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2205.         mov     edi, ecx
  2206.         mov     esi, version_inf
  2207.         mov     ecx, version_end-version_inf
  2208.         rep movsb
  2209.         ret
  2210. ;------------------------------------------------------------------------------
  2211. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2212.      ;wait retrace functions
  2213.  sys_wait_retrace:
  2214.         mov     edx, 0x3da
  2215.  WaitRetrace_loop:
  2216.         in      al, dx
  2217.         test    al, 1000b
  2218.         jz      WaitRetrace_loop
  2219.         and     [esp+32], dword 0
  2220.         ret
  2221. ;------------------------------------------------------------------------------
  2222. align 4
  2223. sysfn_centermouse:      ; 18.15 = mouse centered
  2224. ; removed here by <Lrz>
  2225. ;     call  mouse_centered
  2226. ;* mouse centered - start code- Mario79
  2227. ;mouse_centered:
  2228. ;        push  eax
  2229.         mov     eax, [Screen_Max_X]
  2230.         shr     eax, 1
  2231.         mov     [MOUSE_X], ax
  2232.         mov     eax, [Screen_Max_Y]
  2233.         shr     eax, 1
  2234.         mov     [MOUSE_Y], ax
  2235. ;        ret
  2236. ;* mouse centered - end code- Mario79
  2237.         xor     eax, eax
  2238.         and     [esp+32], eax
  2239. ;        pop   eax
  2240.         ret
  2241. ;------------------------------------------------------------------------------
  2242. align 4
  2243. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  2244.         test    ecx, ecx; get mouse speed factor
  2245.         jnz     .set_mouse_acceleration
  2246.         xor     eax, eax
  2247.         mov     ax, [mouse_speed_factor]
  2248.         mov     [esp+32], eax
  2249.         ret
  2250.  .set_mouse_acceleration:
  2251. ;     cmp  ecx,1  ; set mouse speed factor
  2252.         dec     ecx
  2253.         jnz     .get_mouse_delay
  2254.         mov     [mouse_speed_factor], dx
  2255.         ret
  2256.  .get_mouse_delay:
  2257. ;     cmp  ecx,2  ; get mouse delay
  2258.         dec     ecx
  2259.         jnz     .set_mouse_delay
  2260.         mov     eax, [mouse_delay]
  2261.         mov     [esp+32], eax
  2262.         ret
  2263.  .set_mouse_delay:
  2264. ;     cmp  ecx,3  ; set mouse delay
  2265.         dec     ecx
  2266.         jnz     .set_pointer_position
  2267.         mov     [mouse_delay], edx
  2268.         ret
  2269.  .set_pointer_position:
  2270. ;     cmp  ecx,4  ; set mouse pointer position
  2271.         dec     ecx
  2272.         jnz     .set_mouse_button
  2273.         cmp     dx, word[Screen_Max_Y]
  2274.         ja      .end
  2275.         rol     edx, 16
  2276.         cmp     dx, word[Screen_Max_X]
  2277.         ja      .end
  2278.         mov     [MOUSE_X], edx
  2279.         ret
  2280.  .set_mouse_button:
  2281. ;     cmp   ecx,5  ; set mouse button features
  2282.         dec     ecx
  2283.         jnz     .end
  2284.         mov     [BTN_DOWN], dl
  2285.         mov     [mouse_active], 1
  2286.  .end:
  2287.         ret
  2288. ;------------------------------------------------------------------------------
  2289. sysfn_getfreemem:
  2290.         mov     eax, [pg_data.pages_free]
  2291.         shl     eax, 2
  2292.         mov     [esp+32], eax
  2293.         ret
  2294.  
  2295. sysfn_getallmem:
  2296.         mov     eax, [MEM_AMOUNT]
  2297.         shr     eax, 10
  2298.         mov     [esp+32], eax
  2299.         ret
  2300.  
  2301. ; // Alver, 2007-22-08 // {
  2302. sysfn_pid_to_slot:
  2303.         mov     eax, ecx
  2304.         call    pid_to_slot
  2305.         mov     [esp+32], eax
  2306.         ret
  2307.  
  2308. sysfn_min_rest_window:
  2309.         pushad
  2310.         mov     eax, edx ; ebx - operating
  2311.         shr     ecx, 1
  2312.         jnc     @f
  2313.         call    pid_to_slot
  2314. @@:
  2315.         or      eax, eax ; eax - number of slot
  2316.         jz      .error
  2317.         cmp     eax, 255    ; varify maximal slot number
  2318.         ja      .error
  2319.         movzx   eax, word [WIN_STACK + eax*2]
  2320.         shr     ecx, 1
  2321.         jc      .restore
  2322.  ; .minimize:
  2323.         call    minimize_window
  2324.         jmp     .exit
  2325. .restore:
  2326.         call    restore_minimized_window
  2327. .exit:
  2328.         popad
  2329.         xor     eax, eax
  2330.         mov     [esp+32], eax
  2331.         ret
  2332. .error:
  2333.         popad
  2334.         xor     eax, eax
  2335.         dec     eax
  2336.         mov     [esp+32], eax
  2337.         ret
  2338. ; } \\ Alver, 2007-22-08 \\
  2339.  
  2340. uglobal
  2341. ;// mike.dld, 2006-29-01 [
  2342. screen_workarea RECT
  2343. ;// mike.dld, 2006-29-01 ]
  2344. window_minimize db 0
  2345. sound_flag      db 0
  2346. endg
  2347.  
  2348. UID_NONE=0
  2349. UID_MENUETOS=1   ;official
  2350. UID_KOLIBRI=2    ;russian
  2351.  
  2352. iglobal
  2353. version_inf:
  2354.   db 0,7,7,0  ; version 0.7.7.0
  2355.   db 0
  2356.   dd __REV__
  2357. version_end:
  2358. endg
  2359.  
  2360. sys_cachetodiskette:
  2361.         cmp     ebx, 1
  2362.         jne     .no_floppy_a_save
  2363.         mov     [flp_number], 1
  2364.         jmp     .save_image_on_floppy
  2365. .no_floppy_a_save:
  2366.         cmp     ebx, 2
  2367.         jne     .no_floppy_b_save
  2368.         mov     [flp_number], 2
  2369. .save_image_on_floppy:
  2370.         call    save_image
  2371.         mov     [esp + 32], dword 0
  2372.         cmp     [FDC_Status], 0
  2373.         je      .yes_floppy_save
  2374. .no_floppy_b_save:
  2375.         mov     [esp + 32], dword 1
  2376. .yes_floppy_save:
  2377.         ret
  2378.  
  2379. uglobal
  2380. ;  bgrchanged  dd  0x0
  2381. align 4
  2382. bgrlockpid dd 0
  2383. bgrlock db 0
  2384. endg
  2385.  
  2386. sys_background:
  2387.  
  2388.         cmp     ebx, 1                     ; BACKGROUND SIZE
  2389.         jnz     nosb1
  2390.         test    ecx, ecx
  2391. ;    cmp   ecx,0
  2392.         jz      sbgrr
  2393.         test    edx, edx
  2394. ;    cmp   edx,0
  2395.         jz      sbgrr
  2396. @@:
  2397. ;;Maxis use atomic bts for mutexes  4.4.2009
  2398.         bts     dword [bgrlock], 0
  2399.         jnc     @f
  2400.         call    change_task
  2401.         jmp     @b
  2402. @@:
  2403.         mov     [BgrDataWidth], ecx
  2404.         mov     [BgrDataHeight], edx
  2405. ;    mov   [bgrchanged],1
  2406.  
  2407.         pushad
  2408. ; return memory for old background
  2409.         mov     eax, [img_background]
  2410.         cmp     eax, static_background_data
  2411.         jz      @f
  2412.         stdcall kernel_free, eax
  2413. @@:
  2414. ; calculate RAW size
  2415.         xor     eax, eax
  2416.         inc     eax
  2417.         cmp     [BgrDataWidth], eax
  2418.         jae     @f
  2419.         mov     [BgrDataWidth], eax
  2420. @@:
  2421.         cmp     [BgrDataHeight], eax
  2422.         jae     @f
  2423.         mov     [BgrDataHeight], eax
  2424. @@:
  2425.         mov     eax, [BgrDataWidth]
  2426.         imul    eax, [BgrDataHeight]
  2427.         lea     eax, [eax*3]
  2428.         mov     [mem_BACKGROUND], eax
  2429. ; get memory for new background
  2430.         stdcall kernel_alloc, eax
  2431.         test    eax, eax
  2432.         jz      .memfailed
  2433.         mov     [img_background], eax
  2434.         jmp     .exit
  2435. .memfailed:
  2436. ; revert to static monotone data
  2437.         mov     [img_background], static_background_data
  2438.         xor     eax, eax
  2439.         inc     eax
  2440.         mov     [BgrDataWidth], eax
  2441.         mov     [BgrDataHeight], eax
  2442.         mov     [mem_BACKGROUND], 4
  2443. .exit:
  2444.         popad
  2445.         mov     [bgrlock], 0
  2446.  
  2447.   sbgrr:
  2448.         ret
  2449.  
  2450. nosb1:
  2451.  
  2452.         cmp     ebx, 2                     ; SET PIXEL
  2453.         jnz     nosb2
  2454.  
  2455.         mov     eax, [img_background]
  2456.         test    ecx, ecx
  2457.         jz      @f
  2458.         cmp     eax, static_background_data
  2459.         jz      .ret
  2460. @@:
  2461.         mov     ebx, [mem_BACKGROUND]
  2462.         add     ebx, 4095
  2463.         and     ebx, -4096
  2464.         sub     ebx, 4
  2465.         cmp     ecx, ebx
  2466.         ja      .ret
  2467.  
  2468.         mov     ebx, [eax+ecx]
  2469.         and     ebx, 0xFF000000;255*256*256*256
  2470.         and     edx, 0x00FFFFFF;255*256*256+255*256+255
  2471.         add     edx, ebx
  2472.         mov     [eax+ecx], edx
  2473. .ret:
  2474.         ret
  2475. nosb2:
  2476.  
  2477.         cmp     ebx, 3                     ; DRAW BACKGROUND
  2478.         jnz     nosb3
  2479. draw_background_temp:
  2480. ;    cmp   [bgrchanged],1 ;0
  2481. ;    je    nosb31
  2482. ;draw_background_temp:
  2483. ;    mov   [bgrchanged],1 ;0
  2484.         mov     [background_defined], 1
  2485.         mov     byte[BACKGROUND_CHANGED], 1
  2486.         call    force_redraw_background
  2487.    nosb31:
  2488.         ret
  2489.   nosb3:
  2490.  
  2491.         cmp     ebx, 4                     ; TILED / STRETCHED
  2492.         jnz     nosb4
  2493.         cmp     ecx, [BgrDrawMode]
  2494.         je      nosb41
  2495.         mov     [BgrDrawMode], ecx
  2496. ;    mov   [bgrchanged],1
  2497.    nosb41:
  2498.         ret
  2499.   nosb4:
  2500.  
  2501.         cmp     ebx, 5                     ; BLOCK MOVE TO BGR
  2502.         jnz     nosb5
  2503.         cmp     [img_background], static_background_data
  2504.         jnz     @f
  2505.         test    edx, edx
  2506.         jnz     .fin
  2507.         cmp     esi, 4
  2508.         ja      .fin
  2509.   @@:
  2510.   ; bughere
  2511.         mov     eax, ecx
  2512.         mov     ebx, edx
  2513.         add     ebx, [img_background];IMG_BACKGROUND
  2514.         mov     ecx, esi
  2515.         call    memmove
  2516.   .fin:
  2517.         ret
  2518.   nosb5:
  2519.  
  2520.         cmp     ebx, 6
  2521.         jnz     nosb6
  2522. ;;Maxis use atomic bts for mutex 4.4.2009
  2523. @@:
  2524.         bts     dword [bgrlock], 0
  2525.         jnc     @f
  2526.         call    change_task
  2527.         jmp     @b
  2528. @@:
  2529.         mov     eax, [CURRENT_TASK]
  2530.         mov     [bgrlockpid], eax
  2531.         cmp     [img_background], static_background_data
  2532.         jz      .nomem
  2533.         stdcall user_alloc, [mem_BACKGROUND]
  2534.         mov     [esp+32], eax
  2535.         test    eax, eax
  2536.         jz      .nomem
  2537.         mov     ebx, eax
  2538.         shr     ebx, 12
  2539.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2540.         mov     esi, [img_background]
  2541.         shr     esi, 12
  2542.         mov     ecx, [mem_BACKGROUND]
  2543.         add     ecx, 0xFFF
  2544.         shr     ecx, 12
  2545. .z:
  2546.         mov     eax, [page_tabs+ebx*4]
  2547.         test    al, 1
  2548.         jz      @f
  2549.         call    free_page
  2550. @@:
  2551.         mov     eax, [page_tabs+esi*4]
  2552.         or      al, PG_UW
  2553.         mov     [page_tabs+ebx*4], eax
  2554.         mov     eax, ebx
  2555.         shl     eax, 12
  2556.         invlpg  [eax]
  2557.         inc     ebx
  2558.         inc     esi
  2559.         loop    .z
  2560.         ret
  2561. .nomem:
  2562.         and     [bgrlockpid], 0
  2563.         mov     [bgrlock], 0
  2564. nosb6:
  2565.         cmp     ebx, 7
  2566.         jnz     nosb7
  2567.         cmp     [bgrlock], 0
  2568.         jz      .err
  2569.         mov     eax, [CURRENT_TASK]
  2570.         cmp     [bgrlockpid], eax
  2571.         jnz     .err
  2572.         mov     eax, ecx
  2573.         mov     ebx, ecx
  2574.         shr     eax, 12
  2575.         mov     ecx, [page_tabs+(eax-1)*4]
  2576.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2577.         jz      .err
  2578.         jnp     .err
  2579.         push    eax
  2580.         shr     ecx, 12
  2581.         dec     ecx
  2582. @@:
  2583.         and     dword [page_tabs+eax*4], 0
  2584.         mov     edx, eax
  2585.         shl     edx, 12
  2586.         push    eax
  2587.         invlpg  [edx]
  2588.         pop     eax
  2589.         inc     eax
  2590.         loop    @b
  2591.         pop     eax
  2592.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2593.         stdcall user_free, ebx
  2594.         mov     [esp+32], eax
  2595.         and     [bgrlockpid], 0
  2596.         mov     [bgrlock], 0
  2597.         ret
  2598. .err:
  2599.         and     dword [esp+32], 0
  2600.         ret
  2601.  
  2602. nosb7:
  2603.         ret
  2604.  
  2605. force_redraw_background:
  2606.         and     [draw_data+32 + RECT.left], 0
  2607.         and     [draw_data+32 + RECT.top], 0
  2608.         push    eax ebx
  2609.         mov     eax, [Screen_Max_X]
  2610.         mov     ebx, [Screen_Max_Y]
  2611.         mov     [draw_data+32 + RECT.right], eax
  2612.         mov     [draw_data+32 + RECT.bottom], ebx
  2613.         pop     ebx eax
  2614.         inc     byte[REDRAW_BACKGROUND]
  2615.         ret
  2616.  
  2617. align 4
  2618.  
  2619. sys_getbackground:
  2620. ;    cmp   eax,1                                  ; SIZE
  2621.         dec     ebx
  2622.         jnz     nogb1
  2623.         mov     eax, [BgrDataWidth]
  2624.         shl     eax, 16
  2625.         mov     ax, [BgrDataHeight]
  2626.         mov     [esp+32], eax
  2627.         ret
  2628.  
  2629. nogb1:
  2630. ;    cmp   eax,2                                  ; PIXEL
  2631.         dec     ebx
  2632.         jnz     nogb2
  2633.  
  2634.         mov     eax, [img_background]
  2635.         test    ecx, ecx
  2636.         jz      @f
  2637.         cmp     eax, static_background_data
  2638.         jz      .ret
  2639. @@:
  2640.         mov     ebx, [mem_BACKGROUND]
  2641.         add     ebx, 4095
  2642.         and     ebx, -4096
  2643.         sub     ebx, 4
  2644.         cmp     ecx, ebx
  2645.         ja      .ret
  2646.  
  2647.         mov     eax, [ecx+eax]
  2648.  
  2649.         and     eax, 0xFFFFFF
  2650.         mov     [esp+32], eax
  2651. .ret:
  2652.         ret
  2653.   nogb2:
  2654.  
  2655. ;    cmp   eax,4                                  ; TILED / STRETCHED
  2656.         dec     ebx
  2657.         dec     ebx
  2658.         jnz     nogb4
  2659.         mov     eax, [BgrDrawMode]
  2660.   nogb4:
  2661.         mov     [esp+32], eax
  2662.         ret
  2663.  
  2664. align 4
  2665.  
  2666. sys_getkey:
  2667.         mov     [esp + 32], dword 1
  2668.         ; test main buffer
  2669.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2670.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2671.         mov     edx, [TASK_COUNT]
  2672.         cmp     ecx, edx
  2673.         jne     .finish
  2674.         cmp     [KEY_COUNT], byte 0
  2675.         je      .finish
  2676.         movzx   eax, byte [KEY_BUFF]
  2677.         shl     eax, 8
  2678.         push    eax
  2679.         dec     byte [KEY_COUNT]
  2680.         and     byte [KEY_COUNT], 127
  2681.         movzx   ecx, byte [KEY_COUNT]
  2682.         add     ecx, 2
  2683.         mov     eax, KEY_BUFF + 1
  2684.         mov     ebx, KEY_BUFF
  2685.         call    memmove
  2686.         pop     eax
  2687. .ret_eax:
  2688.         mov     [esp + 32], eax
  2689.         ret
  2690. .finish:
  2691. ; test hotkeys buffer
  2692.         mov     ecx, hotkey_buffer
  2693. @@:
  2694.         cmp     [ecx], ebx
  2695.         jz      .found
  2696.         add     ecx, 8
  2697.         cmp     ecx, hotkey_buffer + 120 * 8
  2698.         jb      @b
  2699.         ret
  2700. .found:
  2701.         mov     ax, [ecx + 6]
  2702.         shl     eax, 16
  2703.         mov     ah, [ecx + 4]
  2704.         mov     al, 2
  2705.         and     dword [ecx + 4], 0
  2706.         and     dword [ecx], 0
  2707.         jmp     .ret_eax
  2708.  
  2709. align 4
  2710.  
  2711. sys_getbutton:
  2712.  
  2713.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2714.         mov     [esp + 32], dword 1
  2715.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2716.         mov     edx, [TASK_COUNT] ; less than 256 processes
  2717.         cmp     ecx, edx
  2718.         jne     .exit
  2719.         movzx   eax, byte [BTN_COUNT]
  2720.         test    eax, eax
  2721.         jz      .exit
  2722.         mov     eax, [BTN_BUFF]
  2723.         and     al, 0xFE                                    ; delete left button bit
  2724.         mov     [BTN_COUNT], byte 0
  2725.         mov     [esp + 32], eax
  2726. .exit:
  2727.         ret
  2728.  
  2729.  
  2730. align 4
  2731.  
  2732. sys_cpuusage:
  2733.  
  2734. ;  RETURN:
  2735. ;
  2736. ;  +00 dword     process cpu usage
  2737. ;  +04  word     position in windowing stack
  2738. ;  +06  word     windowing stack value at current position (cpu nro)
  2739. ;  +10 12 bytes  name
  2740. ;  +22 dword     start in mem
  2741. ;  +26 dword     used mem
  2742. ;  +30 dword     PID , process idenfification number
  2743. ;
  2744.  
  2745.         cmp     ecx, -1 ; who am I ?
  2746.         jne     .no_who_am_i
  2747.         mov     ecx, [CURRENT_TASK]
  2748.   .no_who_am_i:
  2749.         cmp     ecx, max_processes
  2750.         ja      .nofillbuf
  2751.  
  2752. ; +4: word: position of the window of thread in the window stack
  2753.         mov     ax, [WIN_STACK + ecx * 2]
  2754.         mov     [ebx+4], ax
  2755. ; +6: word: number of the thread slot, which window has in the window stack
  2756. ;           position ecx (has no relation to the specific thread)
  2757.         mov     ax, [WIN_POS + ecx * 2]
  2758.         mov     [ebx+6], ax
  2759.  
  2760.         shl     ecx, 5
  2761.  
  2762. ; +0: dword: memory usage
  2763.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  2764.         mov     [ebx], eax
  2765. ; +10: 11 bytes: name of the process
  2766.         push    ecx
  2767.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  2768.         add     ebx, 10
  2769.         mov     ecx, 11
  2770.         call    memmove
  2771.         pop     ecx
  2772.  
  2773. ; +22: address of the process in memory
  2774. ; +26: size of used memory - 1
  2775.         push    edi
  2776.         lea     edi, [ebx+12]
  2777.         xor     eax, eax
  2778.         mov     edx, 0x100000*16
  2779.         cmp     ecx, 1 shl 5
  2780.         je      .os_mem
  2781.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
  2782.         mov     eax, std_application_base_address
  2783. .os_mem:
  2784.         stosd
  2785.         lea     eax, [edx-1]
  2786.         stosd
  2787.  
  2788. ; +30: PID/TID
  2789.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  2790.         stosd
  2791.  
  2792.     ; window position and size
  2793.         push    esi
  2794.         lea     esi, [ecx + window_data + WDATA.box]
  2795.         movsd
  2796.         movsd
  2797.         movsd
  2798.         movsd
  2799.  
  2800.     ; Process state (+50)
  2801.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  2802.         stosd
  2803.  
  2804.     ; Window client area box
  2805.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2806.         movsd
  2807.         movsd
  2808.         movsd
  2809.         movsd
  2810.  
  2811.     ; Window state
  2812.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2813.         stosb
  2814.  
  2815.     ; Event mask (+71)
  2816.         mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
  2817.         stosd
  2818.  
  2819.         pop     esi
  2820.         pop     edi
  2821.  
  2822. .nofillbuf:
  2823.     ; return number of processes
  2824.  
  2825.         mov     eax, [TASK_COUNT]
  2826.         mov     [esp+32], eax
  2827.         ret
  2828.  
  2829. align 4
  2830. sys_clock:
  2831.         cli
  2832.   ; Mikhail Lisovin  xx Jan 2005
  2833.   @@:
  2834.         mov     al, 10
  2835.         out     0x70, al
  2836.         in      al, 0x71
  2837.         test    al, al
  2838.         jns     @f
  2839.         mov     esi, 1
  2840.         call    delay_ms
  2841.         jmp     @b
  2842.   @@:
  2843.   ; end Lisovin's fix
  2844.  
  2845.         xor     al, al        ; seconds
  2846.         out     0x70, al
  2847.         in      al, 0x71
  2848.         movzx   ecx, al
  2849.         mov     al, 02        ; minutes
  2850.         shl     ecx, 16
  2851.         out     0x70, al
  2852.         in      al, 0x71
  2853.         movzx   edx, al
  2854.         mov     al, 04        ; hours
  2855.         shl     edx, 8
  2856.         out     0x70, al
  2857.         in      al, 0x71
  2858.         add     ecx, edx
  2859.         movzx   edx, al
  2860.         add     ecx, edx
  2861.         sti
  2862.         mov     [esp + 32], ecx
  2863.         ret
  2864.  
  2865.  
  2866. align 4
  2867.  
  2868. sys_date:
  2869.  
  2870.         cli
  2871.   @@:
  2872.         mov     al, 10
  2873.         out     0x70, al
  2874.         in      al, 0x71
  2875.         test    al, al
  2876.         jns     @f
  2877.         mov     esi, 1
  2878.         call    delay_ms
  2879.         jmp     @b
  2880.   @@:
  2881.  
  2882.         mov     ch, 0
  2883.         mov     al, 7           ; date
  2884.         out     0x70, al
  2885.         in      al, 0x71
  2886.         mov     cl, al
  2887.         mov     al, 8           ; month
  2888.         shl     ecx, 16
  2889.         out     0x70, al
  2890.         in      al, 0x71
  2891.         mov     ch, al
  2892.         mov     al, 9           ; year
  2893.         out     0x70, al
  2894.         in      al, 0x71
  2895.         mov     cl, al
  2896.         sti
  2897.         mov     [esp+32], ecx
  2898.         ret
  2899.  
  2900.  
  2901. ; redraw status
  2902.  
  2903. sys_redrawstat:
  2904.         cmp     ebx, 1
  2905.         jne     no_widgets_away
  2906.         ; buttons away
  2907.         mov     ecx, [CURRENT_TASK]
  2908.   sys_newba2:
  2909.         mov     edi, [BTN_ADDR]
  2910.         cmp     [edi], dword 0  ; empty button list ?
  2911.         je      end_of_buttons_away
  2912.         movzx   ebx, word [edi]
  2913.         inc     ebx
  2914.         mov     eax, edi
  2915.   sys_newba:
  2916.         dec     ebx
  2917.         jz      end_of_buttons_away
  2918.  
  2919.         add     eax, 0x10
  2920.         cmp     cx, [eax]
  2921.         jnz     sys_newba
  2922.  
  2923.         push    eax ebx ecx
  2924.         mov     ecx, ebx
  2925.         inc     ecx
  2926.         shl     ecx, 4
  2927.         mov     ebx, eax
  2928.         add     eax, 0x10
  2929.         call    memmove
  2930.         dec     dword [edi]
  2931.         pop     ecx ebx eax
  2932.  
  2933.         jmp     sys_newba2
  2934.  
  2935.   end_of_buttons_away:
  2936.  
  2937.         ret
  2938.  
  2939.   no_widgets_away:
  2940.  
  2941.         cmp     ebx, 2
  2942.         jnz     srl1
  2943.  
  2944.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2945.         add     edx, draw_data - CURRENT_TASK
  2946.         mov     [edx + RECT.left], 0
  2947.         mov     [edx + RECT.top], 0
  2948.         mov     eax, [Screen_Max_X]
  2949.         mov     [edx + RECT.right], eax
  2950.         mov     eax, [Screen_Max_Y]
  2951.         mov     [edx + RECT.bottom], eax
  2952.  
  2953.   srl1:
  2954.         ret
  2955.  
  2956. ;ok - 100% work
  2957. ;nt - not tested
  2958. ;---------------------------------------------------------------------------------------------
  2959. ;eax
  2960. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2961. ;1 - change task. Ret nothing. Block. ok.
  2962. ;2 - performance control
  2963. ; ebx
  2964. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2965. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2966. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2967. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2968. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2969. ;eax
  2970. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2971. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2972. ;---------------------------------------------------------------------------------------------
  2973. iglobal
  2974. align 4
  2975. sheduler:
  2976.         dd      sys_sheduler.00
  2977.         dd      change_task
  2978.         dd      sys_sheduler.02
  2979.         dd      sys_sheduler.03
  2980.         dd      sys_sheduler.04
  2981. endg
  2982. sys_sheduler:
  2983. ;rewritten by <Lrz>  29.12.2009
  2984.         jmp     dword [sheduler+ebx*4]
  2985. ;.shed_counter:
  2986. .00:
  2987.         mov     eax, [context_counter]
  2988.         mov     [esp+32], eax
  2989.         ret
  2990.  
  2991. .02:
  2992. ;.perf_control:
  2993.         inc     ebx                     ;before ebx=2, ebx=3
  2994.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  2995.         jz      cache_disable
  2996.  
  2997.         dec     ebx                     ;ebx=2
  2998.         cmp     ebx, ecx                ;
  2999.         jz      cache_enable            ;if ecx=2 and ebx=2
  3000.  
  3001.         dec     ebx                     ;ebx=1
  3002.         cmp     ebx, ecx
  3003.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  3004.  
  3005.         dec     ebx
  3006.         test    ebx, ecx                ;ebx=0 and ecx=0
  3007.         jz      modify_pce              ;if ecx=0
  3008.  
  3009.         ret
  3010.  
  3011. .03:
  3012. ;.rdmsr_instr:
  3013. ;now counter in ecx
  3014. ;(edx:eax) esi:edi => edx:esi
  3015.         mov     eax, esi
  3016.         mov     ecx, edx
  3017.         rdmsr
  3018.         mov     [esp+32], eax
  3019.         mov     [esp+20], edx           ;ret in ebx?
  3020.         ret
  3021.  
  3022. .04:
  3023. ;.wrmsr_instr:
  3024. ;now counter in ecx
  3025. ;(edx:eax) esi:edi => edx:esi
  3026.         ; Fast Call MSR can't be destroy
  3027.         ; ® MSR_AMD_EFER ¬®¦­® ¨§¬¥­ïâì, â.ª. ¢ í⮬ ॣ¨áâॠ«¨è
  3028.         ; ¢ª«îç îâáï/¢ëª«îç îâáï à áè¨à¥­­ë¥ ¢®§¬®¦­®áâ¨
  3029.         cmp     edx, MSR_SYSENTER_CS
  3030.         je      @f
  3031.         cmp     edx, MSR_SYSENTER_ESP
  3032.         je      @f
  3033.         cmp     edx, MSR_SYSENTER_EIP
  3034.         je      @f
  3035.         cmp     edx, MSR_AMD_STAR
  3036.         je      @f
  3037.  
  3038.         mov     eax, esi
  3039.         mov     ecx, edx
  3040.         wrmsr
  3041.         ; mov   [esp + 32], eax
  3042.         ; mov   [esp + 20], edx ;ret in ebx?
  3043. @@:
  3044.         ret
  3045.  
  3046. cache_disable:
  3047.         mov     eax, cr0
  3048.         or      eax, 01100000000000000000000000000000b
  3049.         mov     cr0, eax
  3050.         wbinvd  ;set MESI
  3051.         ret
  3052.  
  3053. cache_enable:
  3054.         mov     eax, cr0
  3055.         and     eax, 10011111111111111111111111111111b
  3056.         mov     cr0, eax
  3057.         ret
  3058.  
  3059. is_cache_enabled:
  3060.         mov     eax, cr0
  3061.         mov     ebx, eax
  3062.         and     eax, 01100000000000000000000000000000b
  3063.         jz      cache_disabled
  3064.         mov     [esp+32], ebx
  3065. cache_disabled:
  3066.         mov     dword [esp+32], eax;0
  3067.         ret
  3068.  
  3069. modify_pce:
  3070.         mov     eax, cr4
  3071. ;       mov ebx,0
  3072. ;       or  bx,100000000b ;pce
  3073. ;       xor eax,ebx ;invert pce
  3074.         bts     eax, 8;pce=cr4[8]
  3075.         mov     cr4, eax
  3076.         mov     [esp+32], eax
  3077.         ret
  3078. ;---------------------------------------------------------------------------------------------
  3079.  
  3080.  
  3081. ; check if pixel is allowed to be drawn
  3082.  
  3083. checkpixel:
  3084.         push    eax edx
  3085.  
  3086.         mov     edx, [Screen_Max_X] ; screen x size
  3087.         inc     edx
  3088.         imul    edx, ebx
  3089.         add     eax, [_WinMapAddress]
  3090.         mov     dl, [eax+edx]; lea eax, [...]
  3091.  
  3092.         xor     ecx, ecx
  3093.         mov     eax, [CURRENT_TASK]
  3094.         cmp     al, dl
  3095.         setne   cl
  3096.  
  3097.         pop     edx eax
  3098.         ret
  3099.  
  3100. iglobal
  3101.   cpustring db 'CPU',0
  3102. endg
  3103.  
  3104. uglobal
  3105. background_defined    db    0    ; diamond, 11.04.2006
  3106. endg
  3107.  
  3108. align 4
  3109. ; check misc
  3110.  
  3111. checkmisc:
  3112.  
  3113.         cmp     [ctrl_alt_del], 1
  3114.         jne     nocpustart
  3115.  
  3116.         mov     ebp, cpustring
  3117.         call    fs_execute_from_sysdir
  3118.  
  3119.         mov     [ctrl_alt_del], 0
  3120.  
  3121. nocpustart:
  3122.         cmp     [mouse_active], 1
  3123.         jne     mouse_not_active
  3124.         mov     [mouse_active], 0
  3125.  
  3126.         xor     edi, edi
  3127.         mov     ebx, CURRENT_TASK
  3128.  
  3129.         mov     ecx, [TASK_COUNT]
  3130.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  3131.         shl     eax, 8
  3132.         push    eax
  3133.  
  3134.         movzx   eax, word [MOUSE_X]
  3135.         movzx   edx, word [MOUSE_Y]
  3136.  
  3137. align 4
  3138. .set_mouse_event:
  3139.         add     edi, 256
  3140.         add     ebx, 32
  3141.         test    [ebx+TASKDATA.event_mask], 0x80000000
  3142.         jz      .pos_filter
  3143.  
  3144.         cmp     edi, [esp]                      ; skip if filtration active
  3145.         jne     .skip
  3146.  
  3147. .pos_filter:
  3148.         test    [ebx+TASKDATA.event_mask], 0x40000000
  3149.         jz      .set
  3150.  
  3151.         mov     esi, [ebx-twdw+WDATA.box.left]
  3152.         cmp     eax, esi
  3153.         jb      .skip
  3154.         add     esi, [ebx-twdw+WDATA.box.width]
  3155.         cmp     eax, esi
  3156.         ja      .skip
  3157.  
  3158.         mov     esi, [ebx-twdw+WDATA.box.top]
  3159.         cmp     edx, esi
  3160.         jb      .skip
  3161.         add     esi, [ebx-twdw+WDATA.box.height]
  3162.         cmp     edx, esi
  3163.         ja      .skip
  3164. .set:
  3165.         or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b
  3166. .skip:
  3167.         loop    .set_mouse_event
  3168.  
  3169.         pop     eax
  3170.  
  3171. mouse_not_active:
  3172.         cmp     byte[BACKGROUND_CHANGED], 0
  3173.         jz      no_set_bgr_event
  3174.         xor     edi, edi
  3175.         mov     ecx, [TASK_COUNT]
  3176. set_bgr_event:
  3177.         add     edi, 256
  3178.         or      [edi+SLOT_BASE+APPDATA.event_mask], 16
  3179.         loop    set_bgr_event
  3180.         mov     byte[BACKGROUND_CHANGED], 0
  3181. no_set_bgr_event:
  3182.         cmp     byte[REDRAW_BACKGROUND], 0         ; background update ?
  3183.         jz      nobackgr
  3184.         cmp     [background_defined], 0
  3185.         jz      nobackgr
  3186. ;    mov   [draw_data+32 + RECT.left],dword 0
  3187. ;    mov   [draw_data+32 + RECT.top],dword 0
  3188. ;    mov   eax,[Screen_Max_X]
  3189. ;    mov   ebx,[Screen_Max_Y]
  3190. ;    mov   [draw_data+32 + RECT.right],eax
  3191. ;    mov   [draw_data+32 + RECT.bottom],ebx
  3192. @@:
  3193.         call    drawbackground
  3194.         xor     eax, eax
  3195.         xchg    al, [REDRAW_BACKGROUND]
  3196.         test    al, al                             ; got new update request?
  3197.         jnz     @b
  3198.         mov     [draw_data+32 + RECT.left], eax
  3199.         mov     [draw_data+32 + RECT.top], eax
  3200.         mov     [draw_data+32 + RECT.right], eax
  3201.         mov     [draw_data+32 + RECT.bottom], eax
  3202.         mov     [MOUSE_BACKGROUND], byte 0
  3203.  
  3204. nobackgr:
  3205.  
  3206.     ; system shutdown request
  3207.  
  3208.         cmp     [SYS_SHUTDOWN], byte 0
  3209.         je      noshutdown
  3210.  
  3211.         mov     edx, [shutdown_processes]
  3212.  
  3213.         cmp     [SYS_SHUTDOWN], dl
  3214.         jne     no_mark_system_shutdown
  3215.  
  3216.         lea     ecx, [edx-1]
  3217.         mov     edx, OS_BASE+0x3040
  3218.         jecxz   @f
  3219. markz:
  3220.         mov     [edx+TASKDATA.state], byte 3
  3221.         add     edx, 0x20
  3222.         loop    markz
  3223. @@:
  3224.  
  3225.   no_mark_system_shutdown:
  3226.  
  3227.         dec     byte [SYS_SHUTDOWN]
  3228.         je      system_shutdown
  3229.  
  3230. noshutdown:
  3231.  
  3232.  
  3233.         mov     eax, [TASK_COUNT]           ; termination
  3234.         mov     ebx, TASK_DATA+TASKDATA.state
  3235.         mov     esi, 1
  3236.  
  3237. newct:
  3238.         mov     cl, [ebx]
  3239.         cmp     cl, byte 3
  3240.         jz      terminate
  3241.         cmp     cl, byte 4
  3242.         jz      terminate
  3243.  
  3244.         add     ebx, 0x20
  3245.         inc     esi
  3246.         dec     eax
  3247.         jnz     newct
  3248.         ret
  3249.  
  3250. ; redraw screen
  3251.  
  3252. redrawscreen:
  3253.  
  3254. ; eax , if process window_data base is eax, do not set flag/limits
  3255.  
  3256.         pushad
  3257.         push    eax
  3258.  
  3259. ;;;         mov   ebx,2
  3260. ;;;         call  delay_hs
  3261.  
  3262.          ;mov   ecx,0               ; redraw flags for apps
  3263.         xor     ecx, ecx
  3264.        newdw2:
  3265.  
  3266.         inc     ecx
  3267.         push    ecx
  3268.  
  3269.         mov     eax, ecx
  3270.         shl     eax, 5
  3271.         add     eax, window_data
  3272.  
  3273.         cmp     eax, [esp+4]
  3274.         je      not_this_task
  3275.                                    ; check if window in redraw area
  3276.         mov     edi, eax
  3277.  
  3278.         cmp     ecx, 1             ; limit for background
  3279.         jz      bgli
  3280.  
  3281.         mov     eax, [edi + WDATA.box.left]
  3282.         mov     ebx, [edi + WDATA.box.top]
  3283.         mov     ecx, [edi + WDATA.box.width]
  3284.         mov     edx, [edi + WDATA.box.height]
  3285.         add     ecx, eax
  3286.         add     edx, ebx
  3287.  
  3288.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3289.         cmp     ecx, ebx
  3290.         jb      ricino
  3291.  
  3292.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3293.         cmp     ecx, eax
  3294.         jb      ricino
  3295.  
  3296.         mov     eax, [edi + WDATA.box.left]
  3297.         mov     ebx, [edi + WDATA.box.top]
  3298.         mov     ecx, [edi + WDATA.box.width]
  3299.         mov     edx, [edi + WDATA.box.height]
  3300.         add     ecx, eax
  3301.         add     edx, ebx
  3302.  
  3303.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3304.         cmp     edx, eax
  3305.         jb      ricino
  3306.  
  3307.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3308.         cmp     ecx, eax
  3309.         jb      ricino
  3310.  
  3311.         bgli:
  3312.  
  3313.         cmp     dword[esp], 1
  3314.         jnz     .az
  3315. ;         cmp   byte[BACKGROUND_CHANGED], 0
  3316. ;         jnz   newdw8
  3317.         cmp     byte[REDRAW_BACKGROUND], 0
  3318.         jz      .az
  3319.         mov     dl, 0
  3320.         lea     eax, [edi+draw_data-window_data]
  3321.         mov     ebx, [draw_limits.left]
  3322.         cmp     ebx, [eax+RECT.left]
  3323.         jae     @f
  3324.         mov     [eax+RECT.left], ebx
  3325.         mov     dl, 1
  3326.         @@:
  3327.         mov     ebx, [draw_limits.top]
  3328.         cmp     ebx, [eax+RECT.top]
  3329.         jae     @f
  3330.         mov     [eax+RECT.top], ebx
  3331.         mov     dl, 1
  3332.         @@:
  3333.         mov     ebx, [draw_limits.right]
  3334.         cmp     ebx, [eax+RECT.right]
  3335.         jbe     @f
  3336.         mov     [eax+RECT.right], ebx
  3337.         mov     dl, 1
  3338.         @@:
  3339.         mov     ebx, [draw_limits.bottom]
  3340.         cmp     ebx, [eax+RECT.bottom]
  3341.         jbe     @f
  3342.         mov     [eax+RECT.bottom], ebx
  3343.         mov     dl, 1
  3344.         @@:
  3345.         add     byte[REDRAW_BACKGROUND], dl
  3346.         jmp     newdw8
  3347.         .az:
  3348.  
  3349.         mov     eax, edi
  3350.         add     eax, draw_data-window_data
  3351.  
  3352.         mov     ebx, [draw_limits.left]        ; set limits
  3353.         mov     [eax + RECT.left], ebx
  3354.         mov     ebx, [draw_limits.top]
  3355.         mov     [eax + RECT.top], ebx
  3356.         mov     ebx, [draw_limits.right]
  3357.         mov     [eax + RECT.right], ebx
  3358.         mov     ebx, [draw_limits.bottom]
  3359.         mov     [eax + RECT.bottom], ebx
  3360.  
  3361.         sub     eax, draw_data-window_data
  3362.  
  3363.         cmp     dword [esp], 1
  3364.         jne     nobgrd
  3365.         inc     byte[REDRAW_BACKGROUND]
  3366.  
  3367.        newdw8:
  3368.        nobgrd:
  3369.  
  3370.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3371.  
  3372.        ricino:
  3373.  
  3374.        not_this_task:
  3375.  
  3376.         pop     ecx
  3377.  
  3378.         cmp     ecx, [TASK_COUNT]
  3379.         jle     newdw2
  3380.  
  3381.         pop     eax
  3382.         popad
  3383.  
  3384.         ret
  3385.  
  3386. calculatebackground:   ; background
  3387.  
  3388.         mov     edi, [_WinMapAddress]              ; set os to use all pixels
  3389.         mov     eax, 0x01010101
  3390.         mov     ecx, [_WinMapSize]
  3391.         shr     ecx, 2
  3392.         rep stosd
  3393.  
  3394.         mov     byte[REDRAW_BACKGROUND], 0            ; do not draw background!
  3395.         mov     byte[BACKGROUND_CHANGED], 0
  3396.  
  3397.         ret
  3398.  
  3399. uglobal
  3400.   imax    dd 0x0
  3401. endg
  3402.  
  3403.  
  3404.  
  3405. delay_ms:     ; delay in 1/1000 sec
  3406.  
  3407.  
  3408.         push    eax
  3409.         push    ecx
  3410.  
  3411.         mov     ecx, esi
  3412.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3413.         imul    ecx, 33941
  3414.         shr     ecx, 9
  3415.         ; </CPU clock fix>
  3416.  
  3417.         in      al, 0x61
  3418.         and     al, 0x10
  3419.         mov     ah, al
  3420.         cld
  3421.  
  3422.  cnt1:
  3423.         in      al, 0x61
  3424.         and     al, 0x10
  3425.         cmp     al, ah
  3426.         jz      cnt1
  3427.  
  3428.         mov     ah, al
  3429.         loop    cnt1
  3430.  
  3431.         pop     ecx
  3432.         pop     eax
  3433.  
  3434.         ret
  3435.  
  3436. align 4
  3437. set_app_param:
  3438.         mov     edi, [TASK_BASE]
  3439.         mov     eax, ebx
  3440.         btr     eax, 3                           ; move MOUSE_FILTRATION
  3441.         mov     ebx, [current_slot]              ; bit into event_filter
  3442.         setc    byte [ebx+APPDATA.event_filter]
  3443.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  3444.         mov     [esp+32], eax                    ; return old mask value
  3445.         ret
  3446.  
  3447.  
  3448.  
  3449. delay_hs:     ; delay in 1/100 secs
  3450. ; ebx = delay time
  3451.         push    ecx
  3452.         push    edx
  3453.  
  3454.         mov     edx, [timer_ticks]
  3455.  
  3456.       newtic:
  3457.         mov     ecx, [timer_ticks]
  3458.         sub     ecx, edx
  3459.         cmp     ecx, ebx
  3460.         jae     zerodelay
  3461.  
  3462.         call    change_task
  3463.  
  3464.         jmp     newtic
  3465.  
  3466.       zerodelay:
  3467.         pop     edx
  3468.         pop     ecx
  3469.  
  3470.         ret
  3471.  
  3472. align 16        ;very often call this subrutine
  3473. memmove:       ; memory move in bytes
  3474.  
  3475. ; eax = from
  3476. ; ebx = to
  3477. ; ecx = no of bytes
  3478.         test    ecx, ecx
  3479.         jle     .ret
  3480.  
  3481.         push    esi edi ecx
  3482.  
  3483.         mov     edi, ebx
  3484.         mov     esi, eax
  3485.  
  3486.         test    ecx, not 11b
  3487.         jz      @f
  3488.  
  3489.         push    ecx
  3490.         shr     ecx, 2
  3491.         rep movsd
  3492.         pop     ecx
  3493.         and     ecx, 11b
  3494.         jz      .finish
  3495.   @@:
  3496.         rep movsb
  3497.  
  3498.   .finish:
  3499.         pop     ecx edi esi
  3500.   .ret:
  3501.         ret
  3502.  
  3503.  
  3504. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3505. ;align 4
  3506. ;
  3507. ;read_floppy_file:
  3508. ;
  3509. ;; as input
  3510. ;;
  3511. ;; eax pointer to file
  3512. ;; ebx file lenght
  3513. ;; ecx start 512 byte block number
  3514. ;; edx number of blocks to read
  3515. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3516. ;;
  3517. ;;
  3518. ;; on return
  3519. ;;
  3520. ;; eax = 0 command succesful
  3521. ;;       1 no fd base and/or partition defined
  3522. ;;       2 yet unsupported FS
  3523. ;;       3 unknown FS
  3524. ;;       4 partition not defined at hd
  3525. ;;       5 file not found
  3526. ;; ebx = size of file
  3527. ;
  3528. ;     mov   edi,[TASK_BASE]
  3529. ;     add   edi,0x10
  3530. ;     add   esi,[edi]
  3531. ;     add   eax,[edi]
  3532. ;
  3533. ;     pushad
  3534. ;     mov  edi,esi
  3535. ;     add  edi,1024
  3536. ;     mov  esi,0x100000+19*512
  3537. ;     sub  ecx,1
  3538. ;     shl  ecx,9
  3539. ;     add  esi,ecx
  3540. ;     shl  edx,9
  3541. ;     mov  ecx,edx
  3542. ;     cld
  3543. ;     rep  movsb
  3544. ;     popad
  3545. ;
  3546. ;     mov   [esp+36],eax
  3547. ;     mov   [esp+24],ebx
  3548. ;     ret
  3549.  
  3550.  
  3551.  
  3552. align 4
  3553. set_io_access_rights:
  3554.         push    edi eax
  3555.         mov     edi, tss._io_map_0
  3556. ;     mov   ecx,eax
  3557. ;     and   ecx,7    ; offset in byte
  3558. ;     shr   eax,3    ; number of byte
  3559. ;     add   edi,eax
  3560. ;     mov   ebx,1
  3561. ;     shl   ebx,cl
  3562.         test    ebp, ebp
  3563. ;     cmp   ebp,0                ; enable access - ebp = 0
  3564.         jnz     .siar1
  3565. ;     not   ebx
  3566. ;     and   [edi],byte bl
  3567.         btr     [edi], eax
  3568.         pop     eax edi
  3569.         ret
  3570. .siar1:
  3571.         bts     [edi], eax
  3572.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  3573.         pop     eax edi
  3574.         ret
  3575. ;reserve/free group of ports
  3576. ;  * eax = 46 - number function
  3577. ;  * ebx = 0 - reserve, 1 - free
  3578. ;  * ecx = number start arrea of ports
  3579. ;  * edx = number end arrea of ports (include last number of port)
  3580. ;Return value:
  3581. ;  * eax = 0 - succesful
  3582. ;  * eax = 1 - error
  3583. ;  * The system has reserve this ports:
  3584. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3585. ;destroys eax,ebx, ebp
  3586. r_f_port_area:
  3587.  
  3588.         test    ebx, ebx
  3589.         jnz     free_port_area
  3590. ;     je    r_port_area
  3591. ;     jmp   free_port_area
  3592.  
  3593. ;   r_port_area:
  3594.  
  3595. ;     pushad
  3596.  
  3597.         cmp     ecx, edx      ; beginning > end ?
  3598.         ja      rpal1
  3599.         cmp     edx, 65536
  3600.         jae     rpal1
  3601.         mov     eax, [RESERVED_PORTS]
  3602.         test    eax, eax      ; no reserved areas ?
  3603.         je      rpal2
  3604.         cmp     eax, 255      ; max reserved
  3605.         jae     rpal1
  3606.  rpal3:
  3607.         mov     ebx, eax
  3608.         shl     ebx, 4
  3609.         add     ebx, RESERVED_PORTS
  3610.         cmp     ecx, [ebx+8]
  3611.         ja      rpal4
  3612.         cmp     edx, [ebx+4]
  3613.         jae     rpal1
  3614. ;     jb    rpal4
  3615. ;     jmp   rpal1
  3616.  rpal4:
  3617.         dec     eax
  3618.         jnz     rpal3
  3619.         jmp     rpal2
  3620.    rpal1:
  3621. ;     popad
  3622. ;     mov   eax,1
  3623.         xor     eax, eax
  3624.         inc     eax
  3625.         ret
  3626.    rpal2:
  3627. ;     popad
  3628.      ; enable port access at port IO map
  3629.         cli
  3630.         pushad                        ; start enable io map
  3631.  
  3632.         cmp     edx, 65536;16384
  3633.         jae     no_unmask_io; jge
  3634.         mov     eax, ecx
  3635. ;       push    ebp
  3636.         xor     ebp, ebp               ; enable - eax = port
  3637. new_port_access:
  3638. ;     pushad
  3639.         call    set_io_access_rights
  3640. ;     popad
  3641.         inc     eax
  3642.         cmp     eax, edx
  3643.         jbe     new_port_access
  3644. ;       pop     ebp
  3645. no_unmask_io:
  3646.         popad                         ; end enable io map
  3647.         sti
  3648.  
  3649.         mov     eax, [RESERVED_PORTS]
  3650.         add     eax, 1
  3651.         mov     [RESERVED_PORTS], eax
  3652.         shl     eax, 4
  3653.         add     eax, RESERVED_PORTS
  3654.         mov     ebx, [TASK_BASE]
  3655.         mov     ebx, [ebx+TASKDATA.pid]
  3656.         mov     [eax], ebx
  3657.         mov     [eax+4], ecx
  3658.         mov     [eax+8], edx
  3659.  
  3660.         xor     eax, eax
  3661.         ret
  3662.  
  3663. free_port_area:
  3664.  
  3665. ;     pushad
  3666.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3667.         test    eax, eax
  3668.         jz      frpal2
  3669.         mov     ebx, [TASK_BASE]
  3670.         mov     ebx, [ebx+TASKDATA.pid]
  3671.    frpal3:
  3672.         mov     edi, eax
  3673.         shl     edi, 4
  3674.         add     edi, RESERVED_PORTS
  3675.         cmp     ebx, [edi]
  3676.         jne     frpal4
  3677.         cmp     ecx, [edi+4]
  3678.         jne     frpal4
  3679.         cmp     edx, [edi+8]
  3680.         jne     frpal4
  3681.         jmp     frpal1
  3682.    frpal4:
  3683.         dec     eax
  3684.         jnz     frpal3
  3685.    frpal2:
  3686. ;     popad
  3687.         inc     eax
  3688.         ret
  3689.    frpal1:
  3690.         push    ecx
  3691.         mov     ecx, 256
  3692.         sub     ecx, eax
  3693.         shl     ecx, 4
  3694.         mov     esi, edi
  3695.         add     esi, 16
  3696.         cld
  3697.         rep movsb
  3698.  
  3699.         dec     dword [RESERVED_PORTS]
  3700. ;popad
  3701. ;disable port access at port IO map
  3702.  
  3703. ;     pushad                        ; start disable io map
  3704.         pop     eax     ;start port
  3705.         cmp     edx, 65536;16384
  3706.         jge     no_mask_io
  3707.  
  3708. ;     mov   eax,ecx
  3709.         xor     ebp, ebp
  3710.         inc     ebp
  3711. new_port_access_disable:
  3712. ;     pushad
  3713. ;     mov   ebp,1                  ; disable - eax = port
  3714.         call    set_io_access_rights
  3715. ;     popad
  3716.         inc     eax
  3717.         cmp     eax, edx
  3718.         jbe     new_port_access_disable
  3719. no_mask_io:
  3720. ;     popad                         ; end disable io map
  3721.         xor     eax, eax
  3722.         ret
  3723. ;-----------------------------------------------------------------------------
  3724. align 4
  3725. drawbackground:
  3726. dbrv20:
  3727.         cmp     [BgrDrawMode], dword 1
  3728.         jne     bgrstr
  3729.         call    vesa20_drawbackground_tiled
  3730.         call    [draw_pointer]
  3731.         ret
  3732. ;--------------------------------------
  3733. align 4
  3734. bgrstr:
  3735.         call    vesa20_drawbackground_stretch
  3736.         call    [draw_pointer]
  3737.         ret
  3738. ;-----------------------------------------------------------------------------
  3739. align 4
  3740. syscall_putimage:                       ; PutImage
  3741. sys_putimage:
  3742.         test    ecx, 0x80008000
  3743.         jnz     .exit
  3744.         test    ecx, 0x0000FFFF
  3745.         jz      .exit
  3746.         test    ecx, 0xFFFF0000
  3747.         jnz     @f
  3748. ;--------------------------------------
  3749. align 4
  3750. .exit:
  3751.         ret
  3752. ;--------------------------------------
  3753. align 4
  3754. @@:
  3755.         mov     edi, [current_slot]
  3756.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  3757.         rol     edx, 16
  3758.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  3759.         rol     edx, 16
  3760. ;--------------------------------------
  3761. align 4
  3762. .forced:
  3763.         push    ebp esi 0
  3764.         mov     ebp, putimage_get24bpp
  3765.         mov     esi, putimage_init24bpp
  3766. ;--------------------------------------
  3767. align 4
  3768. sys_putimage_bpp:
  3769.         call    vesa20_putimage
  3770.         pop     ebp esi ebp
  3771.         ret
  3772. ;        jmp     [draw_pointer]
  3773. ;-----------------------------------------------------------------------------
  3774. align 4
  3775. sys_putimage_palette:
  3776. ; ebx = pointer to image
  3777. ; ecx = [xsize]*65536 + [ysize]
  3778. ; edx = [xstart]*65536 + [ystart]
  3779. ; esi = number of bits per pixel, must be 8, 24 or 32
  3780. ; edi = pointer to palette
  3781. ; ebp = row delta
  3782.         mov     eax, [CURRENT_TASK]
  3783.         shl     eax, 8
  3784.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  3785.         rol     edx, 16
  3786.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  3787.         rol     edx, 16
  3788. ;--------------------------------------
  3789. align 4
  3790. .forced:
  3791.         cmp     esi, 1
  3792.         jnz     @f
  3793.         push    edi
  3794.         mov     eax, [edi+4]
  3795.         sub     eax, [edi]
  3796.         push    eax
  3797.         push    dword [edi]
  3798.         push    0ffffff80h
  3799.         mov     edi, esp
  3800.         call    put_mono_image
  3801.         add     esp, 12
  3802.         pop     edi
  3803.         ret
  3804. ;--------------------------------------
  3805. align 4
  3806. @@:
  3807.         cmp     esi, 2
  3808.         jnz     @f
  3809.         push    edi
  3810.         push    0ffffff80h
  3811.         mov     edi, esp
  3812.         call    put_2bit_image
  3813.         pop     eax
  3814.         pop     edi
  3815.         ret
  3816. ;--------------------------------------
  3817. align 4
  3818. @@:
  3819.         cmp     esi, 4
  3820.         jnz     @f
  3821.         push    edi
  3822.         push    0ffffff80h
  3823.         mov     edi, esp
  3824.         call    put_4bit_image
  3825.         pop     eax
  3826.         pop     edi
  3827.         ret
  3828. ;--------------------------------------
  3829. align 4
  3830. @@:
  3831.         push    ebp esi ebp
  3832.         cmp     esi, 8
  3833.         jnz     @f
  3834.         mov     ebp, putimage_get8bpp
  3835.         mov     esi, putimage_init8bpp
  3836.         jmp     sys_putimage_bpp
  3837. ;--------------------------------------
  3838. align 4
  3839. @@:
  3840.         cmp     esi, 15
  3841.         jnz     @f
  3842.         mov     ebp, putimage_get15bpp
  3843.         mov     esi, putimage_init15bpp
  3844.         jmp     sys_putimage_bpp
  3845. ;--------------------------------------
  3846. align 4
  3847. @@:
  3848.         cmp     esi, 16
  3849.         jnz     @f
  3850.         mov     ebp, putimage_get16bpp
  3851.         mov     esi, putimage_init16bpp
  3852.         jmp     sys_putimage_bpp
  3853. ;--------------------------------------
  3854. align 4
  3855. @@:
  3856.         cmp     esi, 24
  3857.         jnz     @f
  3858.         mov     ebp, putimage_get24bpp
  3859.         mov     esi, putimage_init24bpp
  3860.         jmp     sys_putimage_bpp
  3861. ;--------------------------------------
  3862. align 4
  3863. @@:
  3864.         cmp     esi, 32
  3865.         jnz     @f
  3866.         mov     ebp, putimage_get32bpp
  3867.         mov     esi, putimage_init32bpp
  3868.         jmp     sys_putimage_bpp
  3869. ;--------------------------------------
  3870. align 4
  3871. @@:
  3872.         pop     ebp esi ebp
  3873.         ret
  3874. ;-----------------------------------------------------------------------------
  3875. align 4
  3876. put_mono_image:
  3877.         push    ebp esi ebp
  3878.         mov     ebp, putimage_get1bpp
  3879.         mov     esi, putimage_init1bpp
  3880.         jmp     sys_putimage_bpp
  3881. ;-----------------------------------------------------------------------------
  3882. align 4
  3883. put_2bit_image:
  3884.         push    ebp esi ebp
  3885.         mov     ebp, putimage_get2bpp
  3886.         mov     esi, putimage_init2bpp
  3887.         jmp     sys_putimage_bpp
  3888. ;-----------------------------------------------------------------------------
  3889. align 4
  3890. put_4bit_image:
  3891.         push    ebp esi ebp
  3892.         mov     ebp, putimage_get4bpp
  3893.         mov     esi, putimage_init4bpp
  3894.         jmp     sys_putimage_bpp
  3895. ;-----------------------------------------------------------------------------
  3896. align 4
  3897. putimage_init24bpp:
  3898.         lea     eax, [eax*3]
  3899. putimage_init8bpp:
  3900.         ret
  3901. ;-----------------------------------------------------------------------------
  3902. align 16
  3903. putimage_get24bpp:
  3904.         movzx   eax, byte [esi+2]
  3905.         shl     eax, 16
  3906.         mov     ax, [esi]
  3907.         add     esi, 3
  3908.         ret     4
  3909. ;-----------------------------------------------------------------------------
  3910. align 16
  3911. putimage_get8bpp:
  3912.         movzx   eax, byte [esi]
  3913.         push    edx
  3914.         mov     edx, [esp+8]
  3915.         mov     eax, [edx+eax*4]
  3916.         pop     edx
  3917.         inc     esi
  3918.         ret     4
  3919. ;-----------------------------------------------------------------------------
  3920. align 4
  3921. putimage_init1bpp:
  3922.         add     eax, ecx
  3923.         push    ecx
  3924.         add     eax, 7
  3925.         add     ecx, 7
  3926.         shr     eax, 3
  3927.         shr     ecx, 3
  3928.         sub     eax, ecx
  3929.         pop     ecx
  3930.         ret
  3931. ;-----------------------------------------------------------------------------
  3932. align 16
  3933. putimage_get1bpp:
  3934.         push    edx
  3935.         mov     edx, [esp+8]
  3936.         mov     al, [edx]
  3937.         add     al, al
  3938.         jnz     @f
  3939.         lodsb
  3940.         adc     al, al
  3941. @@:
  3942.         mov     [edx], al
  3943.         sbb     eax, eax
  3944.         and     eax, [edx+8]
  3945.         add     eax, [edx+4]
  3946.         pop     edx
  3947.         ret     4
  3948. ;-----------------------------------------------------------------------------
  3949. align 4
  3950. putimage_init2bpp:
  3951.         add     eax, ecx
  3952.         push    ecx
  3953.         add     ecx, 3
  3954.         add     eax, 3
  3955.         shr     ecx, 2
  3956.         shr     eax, 2
  3957.         sub     eax, ecx
  3958.         pop     ecx
  3959.         ret
  3960. ;-----------------------------------------------------------------------------
  3961. align 16
  3962. putimage_get2bpp:
  3963.         push    edx
  3964.         mov     edx, [esp+8]
  3965.         mov     al, [edx]
  3966.         mov     ah, al
  3967.         shr     al, 6
  3968.         shl     ah, 2
  3969.         jnz     .nonewbyte
  3970.         lodsb
  3971.         mov     ah, al
  3972.         shr     al, 6
  3973.         shl     ah, 2
  3974.         add     ah, 1
  3975. .nonewbyte:
  3976.         mov     [edx], ah
  3977.         mov     edx, [edx+4]
  3978.         movzx   eax, al
  3979.         mov     eax, [edx+eax*4]
  3980.         pop     edx
  3981.         ret     4
  3982. ;-----------------------------------------------------------------------------
  3983. align 4
  3984. putimage_init4bpp:
  3985.         add     eax, ecx
  3986.         push    ecx
  3987.         add     ecx, 1
  3988.         add     eax, 1
  3989.         shr     ecx, 1
  3990.         shr     eax, 1
  3991.         sub     eax, ecx
  3992.         pop     ecx
  3993.         ret
  3994. ;-----------------------------------------------------------------------------
  3995. align 16
  3996. putimage_get4bpp:
  3997.         push    edx
  3998.         mov     edx, [esp+8]
  3999.         add     byte [edx], 80h
  4000.         jc      @f
  4001.         movzx   eax, byte [edx+1]
  4002.         mov     edx, [edx+4]
  4003.         and     eax, 0x0F
  4004.         mov     eax, [edx+eax*4]
  4005.         pop     edx
  4006.         ret     4
  4007. @@:
  4008.         movzx   eax, byte [esi]
  4009.         add     esi, 1
  4010.         mov     [edx+1], al
  4011.         shr     eax, 4
  4012.         mov     edx, [edx+4]
  4013.         mov     eax, [edx+eax*4]
  4014.         pop     edx
  4015.         ret     4
  4016. ;-----------------------------------------------------------------------------
  4017. align 4
  4018. putimage_init32bpp:
  4019.         shl     eax, 2
  4020.         ret
  4021. ;-----------------------------------------------------------------------------
  4022. align 16
  4023. putimage_get32bpp:
  4024.         lodsd
  4025.         ret     4
  4026. ;-----------------------------------------------------------------------------
  4027. align 4
  4028. putimage_init15bpp:
  4029. putimage_init16bpp:
  4030.         add     eax, eax
  4031.         ret
  4032. ;-----------------------------------------------------------------------------
  4033. align 16
  4034. putimage_get15bpp:
  4035. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4036.         push    ecx edx
  4037.         movzx   eax, word [esi]
  4038.         add     esi, 2
  4039.         mov     ecx, eax
  4040.         mov     edx, eax
  4041.         and     eax, 0x1F
  4042.         and     ecx, 0x1F shl 5
  4043.         and     edx, 0x1F shl 10
  4044.         shl     eax, 3
  4045.         shl     ecx, 6
  4046.         shl     edx, 9
  4047.         or      eax, ecx
  4048.         or      eax, edx
  4049.         pop     edx ecx
  4050.         ret     4
  4051. ;-----------------------------------------------------------------------------
  4052. align 16
  4053. putimage_get16bpp:
  4054. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4055.         push    ecx edx
  4056.         movzx   eax, word [esi]
  4057.         add     esi, 2
  4058.         mov     ecx, eax
  4059.         mov     edx, eax
  4060.         and     eax, 0x1F
  4061.         and     ecx, 0x3F shl 5
  4062.         and     edx, 0x1F shl 11
  4063.         shl     eax, 3
  4064.         shl     ecx, 5
  4065.         shl     edx, 8
  4066.         or      eax, ecx
  4067.         or      eax, edx
  4068.         pop     edx ecx
  4069.         ret     4
  4070. ;-----------------------------------------------------------------------------
  4071. ;align 4
  4072. ; eax x beginning
  4073. ; ebx y beginning
  4074. ; ecx x end
  4075.         ; edx y end
  4076. ; edi color
  4077. ;__sys_drawbar:
  4078. ;        mov     esi, [current_slot]
  4079. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4080. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4081. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4082. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4083. ;--------------------------------------
  4084. ;align 4
  4085. ;.forced:
  4086. ;        call    vesa20_drawbar
  4087. ;        call    [draw_pointer]
  4088. ;        ret
  4089. ;-----------------------------------------------------------------------------
  4090. align 4
  4091. kb_read:
  4092.  
  4093.         push    ecx edx
  4094.  
  4095.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4096.       kr_loop:
  4097.         in      al, 0x64
  4098.         test    al, 1
  4099.         jnz     kr_ready
  4100.         loop    kr_loop
  4101.         mov     ah, 1
  4102.         jmp     kr_exit
  4103.       kr_ready:
  4104.         push    ecx
  4105.         mov     ecx, 32
  4106.       kr_delay:
  4107.         loop    kr_delay
  4108.         pop     ecx
  4109.         in      al, 0x60
  4110.         xor     ah, ah
  4111.       kr_exit:
  4112.  
  4113.         pop     edx ecx
  4114.  
  4115.         ret
  4116. ;-----------------------------------------------------------------------------
  4117. align 4
  4118. kb_write:
  4119.  
  4120.         push    ecx edx
  4121.  
  4122.         mov     dl, al
  4123. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4124. ;      kw_loop1:
  4125. ;        in      al,0x64
  4126. ;        test    al,0x20
  4127. ;        jz      kw_ok1
  4128. ;        loop    kw_loop1
  4129. ;        mov     ah,1
  4130. ;        jmp     kw_exit
  4131. ;      kw_ok1:
  4132.         in      al, 0x60
  4133.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4134.       kw_loop:
  4135.         in      al, 0x64
  4136.         test    al, 2
  4137.         jz      kw_ok
  4138.         loop    kw_loop
  4139.         mov     ah, 1
  4140.         jmp     kw_exit
  4141.       kw_ok:
  4142.         mov     al, dl
  4143.         out     0x60, al
  4144.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4145.       kw_loop3:
  4146.         in      al, 0x64
  4147.         test    al, 2
  4148.         jz      kw_ok3
  4149.         loop    kw_loop3
  4150.         mov     ah, 1
  4151.         jmp     kw_exit
  4152.       kw_ok3:
  4153.         mov     ah, 8
  4154.       kw_loop4:
  4155.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4156.       kw_loop5:
  4157.         in      al, 0x64
  4158.         test    al, 1
  4159.         jnz     kw_ok4
  4160.         loop    kw_loop5
  4161.         dec     ah
  4162.         jnz     kw_loop4
  4163.       kw_ok4:
  4164.         xor     ah, ah
  4165.       kw_exit:
  4166.  
  4167.         pop     edx ecx
  4168.  
  4169.         ret
  4170. ;-----------------------------------------------------------------------------
  4171. align 4
  4172. kb_cmd:
  4173.  
  4174.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4175.       c_wait:
  4176.         in      al, 0x64
  4177.         test    al, 2
  4178.         jz      c_send
  4179.         loop    c_wait
  4180.         jmp     c_error
  4181.       c_send:
  4182.         mov     al, bl
  4183.         out     0x64, al
  4184.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4185.       c_accept:
  4186.         in      al, 0x64
  4187.         test    al, 2
  4188.         jz      c_ok
  4189.         loop    c_accept
  4190.       c_error:
  4191.         mov     ah, 1
  4192.         jmp     c_exit
  4193.       c_ok:
  4194.         xor     ah, ah
  4195.       c_exit:
  4196.         ret
  4197.  
  4198.  
  4199. setmouse:  ; set mousepicture -pointer
  4200.            ; ps2 mouse enable
  4201.  
  4202. ;        mov     [MOUSE_PICTURE], dword mousepointer
  4203.  
  4204.         cli
  4205.  
  4206.         ret
  4207.  
  4208. if used _rdtsc
  4209. _rdtsc:
  4210.         bt      [cpu_caps], CAPS_TSC
  4211.         jnc     ret_rdtsc
  4212.         rdtsc
  4213.         ret
  4214.    ret_rdtsc:
  4215.         mov     edx, 0xffffffff
  4216.         mov     eax, 0xffffffff
  4217.         ret
  4218. end if
  4219.  
  4220. sys_msg_board_str:
  4221.  
  4222.         pushad
  4223.    @@:
  4224.         cmp     [esi], byte 0
  4225.         je      @f
  4226.         mov     eax, 1
  4227.         movzx   ebx, byte [esi]
  4228.         call    sys_msg_board
  4229.         inc     esi
  4230.         jmp     @b
  4231.    @@:
  4232.         popad
  4233.         ret
  4234.  
  4235. sys_msg_board_byte:
  4236. ; in: al = byte to display
  4237. ; out: nothing
  4238. ; destroys: nothing
  4239.         pushad
  4240.         mov     ecx, 2
  4241.         shl     eax, 24
  4242.         jmp     @f
  4243.  
  4244. sys_msg_board_word:
  4245. ; in: ax = word to display
  4246. ; out: nothing
  4247. ; destroys: nothing
  4248.         pushad
  4249.         mov     ecx, 4
  4250.         shl     eax, 16
  4251.         jmp     @f
  4252.  
  4253. sys_msg_board_dword:
  4254. ; in: eax = dword to display
  4255. ; out: nothing
  4256. ; destroys: nothing
  4257.         pushad
  4258.         mov     ecx, 8
  4259. @@:
  4260.         push    ecx
  4261.         rol     eax, 4
  4262.         push    eax
  4263.         and     al, 0xF
  4264.         cmp     al, 10
  4265.         sbb     al, 69h
  4266.         das
  4267.         mov     bl, al
  4268.         xor     eax, eax
  4269.         inc     eax
  4270.         call    sys_msg_board
  4271.         pop     eax
  4272.         pop     ecx
  4273.         loop    @b
  4274.         popad
  4275.         ret
  4276.  
  4277. uglobal
  4278.   msg_board_data:
  4279.                   times 4096 db 0
  4280.   msg_board_count dd 0x0
  4281. endg
  4282.  
  4283. sys_msg_board:
  4284.  
  4285. ; eax=1 : write :  bl byte to write
  4286. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4287.  
  4288.         mov     ecx, [msg_board_count]
  4289.         cmp     eax, 1
  4290.         jne     .smbl1
  4291.  
  4292. if defined debug_com_base
  4293.  
  4294.         push    dx ax
  4295.  
  4296.        @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
  4297.         mov     dx, debug_com_base+5
  4298.         in      al, dx
  4299.         test    al, 1 shl 5
  4300.         jz      @r
  4301.  
  4302.         mov     dx, debug_com_base      ; Output the byte
  4303.         mov     al, bl
  4304.         out     dx, al
  4305.  
  4306.         pop     ax dx
  4307.  
  4308. end if
  4309.  
  4310.         mov     [msg_board_data+ecx], bl
  4311.         inc     ecx
  4312.         and     ecx, 4095
  4313.         mov     [msg_board_count], ecx
  4314.         mov     [check_idle_semaphore], 5
  4315.         ret
  4316. .smbl1:
  4317.         cmp     eax, 2
  4318.         jne     .smbl2
  4319.         test    ecx, ecx
  4320.         jz      .smbl21
  4321.         mov     eax, msg_board_data+1
  4322.         mov     ebx, msg_board_data
  4323.         movzx   edx, byte [ebx]
  4324.         call    memmove
  4325.         dec     [msg_board_count]
  4326.         mov     [esp + 36], edx ;eax
  4327.         mov     [esp + 24], dword 1
  4328.         ret
  4329. .smbl21:
  4330.         mov     [esp+36], ecx
  4331.         mov     [esp+24], ecx
  4332. .smbl2:
  4333.         ret
  4334.  
  4335. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4336. ;; 66 sys function.                                                ;;
  4337. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4338. ;; out eax                                                         ;;
  4339. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4340. iglobal
  4341. align 4
  4342. f66call:
  4343.            dd sys_process_def.1   ; 1 = set keyboard mode
  4344.            dd sys_process_def.2   ; 2 = get keyboard mode
  4345.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4346.            dd sys_process_def.4
  4347.            dd sys_process_def.5
  4348. endg
  4349.  
  4350.  
  4351.  
  4352.  
  4353. sys_process_def:
  4354.         dec     ebx
  4355.         cmp     ebx, 5
  4356.         jae     .not_support    ;if >=6 then or eax,-1
  4357.  
  4358.         mov     edi, [CURRENT_TASK]
  4359.         jmp     dword [f66call+ebx*4]
  4360.  
  4361. .not_support:
  4362.         or      eax, -1
  4363.         ret
  4364.  
  4365. .1:
  4366.         shl     edi, 8
  4367.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4368.  
  4369.         ret
  4370.  
  4371. .2:                             ; 2 = get keyboard mode
  4372.         shl     edi, 8
  4373.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4374.         mov     [esp+32], eax
  4375.         ret
  4376. ;     xor   eax,eax
  4377. ;     movzx eax,byte [shift]
  4378. ;     movzx ebx,byte [ctrl]
  4379. ;     shl   ebx,2
  4380. ;     add   eax,ebx
  4381. ;     movzx ebx,byte [alt]
  4382. ;     shl   ebx,3
  4383. ;     add   eax,ebx
  4384. .3:                             ;3 = get keyboard ctrl, alt, shift
  4385.  ;// mike.dld [
  4386.         mov     eax, [kb_state]
  4387.  ;// mike.dld ]
  4388.         mov     [esp+32], eax
  4389.         ret
  4390.  
  4391. .4:
  4392.         mov     eax, hotkey_list
  4393. @@:
  4394.         cmp     dword [eax+8], 0
  4395.         jz      .found_free
  4396.         add     eax, 16
  4397.         cmp     eax, hotkey_list+16*256
  4398.         jb      @b
  4399.         mov     dword [esp+32], 1
  4400.         ret
  4401. .found_free:
  4402.         mov     [eax+8], edi
  4403.         mov     [eax+4], edx
  4404.         movzx   ecx, cl
  4405.         lea     ecx, [hotkey_scancodes+ecx*4]
  4406.         mov     edx, [ecx]
  4407.         mov     [eax], edx
  4408.         mov     [ecx], eax
  4409.         mov     [eax+12], ecx
  4410.         jecxz   @f
  4411.         mov     [edx+12], eax
  4412. @@:
  4413.         and     dword [esp+32], 0
  4414.         ret
  4415.  
  4416. .5:
  4417.         movzx   ebx, cl
  4418.         lea     ebx, [hotkey_scancodes+ebx*4]
  4419.         mov     eax, [ebx]
  4420. .scan:
  4421.         test    eax, eax
  4422.         jz      .notfound
  4423.         cmp     [eax+8], edi
  4424.         jnz     .next
  4425.         cmp     [eax+4], edx
  4426.         jz      .found
  4427. .next:
  4428.         mov     eax, [eax]
  4429.         jmp     .scan
  4430. .notfound:
  4431.         mov     dword [esp+32], 1
  4432.         ret
  4433. .found:
  4434.         mov     ecx, [eax]
  4435.         jecxz   @f
  4436.         mov     edx, [eax+12]
  4437.         mov     [ecx+12], edx
  4438. @@:
  4439.         mov     ecx, [eax+12]
  4440.         mov     edx, [eax]
  4441.         mov     [ecx], edx
  4442.         xor     edx, edx
  4443.         mov     [eax+4], edx
  4444.         mov     [eax+8], edx
  4445.         mov     [eax+12], edx
  4446.         mov     [eax], edx
  4447.         mov     [esp+32], edx
  4448.         ret
  4449.  
  4450.  
  4451. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4452. ;; 61 sys function.                                                ;;
  4453. ;; in eax=61,ebx in [1..3]                                         ;;
  4454. ;; out eax                                                         ;;
  4455. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4456. iglobal
  4457. align 4
  4458. f61call:
  4459.            dd sys_gs.1   ; resolution
  4460.            dd sys_gs.2   ; bits per pixel
  4461.            dd sys_gs.3   ; bytes per scanline
  4462. endg
  4463.  
  4464.  
  4465. align 4
  4466.  
  4467. sys_gs:                         ; direct screen access
  4468.         dec     ebx
  4469.         cmp     ebx, 2
  4470.         ja      .not_support
  4471.         jmp     dword [f61call+ebx*4]
  4472. .not_support:
  4473.         or      [esp+32], dword -1
  4474.         ret
  4475.  
  4476.  
  4477. .1:                             ; resolution
  4478.         mov     eax, [Screen_Max_X]
  4479.         shl     eax, 16
  4480.         mov     ax, [Screen_Max_Y]
  4481.         add     eax, 0x00010001
  4482.         mov     [esp+32], eax
  4483.         ret
  4484. .2:                             ; bits per pixel
  4485.         movzx   eax, byte [ScreenBPP]
  4486.         mov     [esp+32], eax
  4487.         ret
  4488. .3:                             ; bytes per scanline
  4489.         mov     eax, [BytesPerScanLine]
  4490.         mov     [esp+32], eax
  4491.         ret
  4492.  
  4493. align 4  ;  system functions
  4494.  
  4495. syscall_setpixel:                       ; SetPixel
  4496.  
  4497.         mov     eax, ebx
  4498.         mov     ebx, ecx
  4499.         mov     ecx, edx
  4500.         mov     edx, [TASK_BASE]
  4501.         add     eax, [edx-twdw+WDATA.box.left]
  4502.         add     ebx, [edx-twdw+WDATA.box.top]
  4503.         mov     edi, [current_slot]
  4504.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  4505.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  4506.         xor     edi, edi ; no force
  4507.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 save to mouseunder area
  4508.         jmp     [putpixel]
  4509.  
  4510. align 4
  4511.  
  4512. syscall_writetext:                      ; WriteText
  4513.  
  4514.         mov     eax, [TASK_BASE]
  4515.         mov     ebp, [eax-twdw+WDATA.box.left]
  4516.         push    esi
  4517.         mov     esi, [current_slot]
  4518.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4519.         shl     ebp, 16
  4520.         add     ebp, [eax-twdw+WDATA.box.top]
  4521.         add     bp, word[esi+APPDATA.wnd_clientbox.top]
  4522.         pop     esi
  4523.         add     ebx, ebp
  4524.         mov     eax, edi
  4525.         xor     edi, edi
  4526.         jmp     dtext
  4527.  
  4528. align 4
  4529.  
  4530. syscall_openramdiskfile:                ; OpenRamdiskFile
  4531.  
  4532.         mov     eax, ebx
  4533.         mov     ebx, ecx
  4534.         mov     ecx, edx
  4535.         mov     edx, esi
  4536.         mov     esi, 12
  4537.         call    fileread
  4538.         mov     [esp+32], eax
  4539.         ret
  4540.  
  4541. align 4
  4542.  
  4543. syscall_drawrect:                       ; DrawRect
  4544.  
  4545.         mov     edi, edx ; color + gradient
  4546.         and     edi, 0x80FFFFFF
  4547.         test    bx, bx  ; x.size
  4548.         je      .drectr
  4549.         test    cx, cx ; y.size
  4550.         je      .drectr
  4551.  
  4552.         mov     eax, ebx ; bad idea
  4553.         mov     ebx, ecx
  4554.  
  4555.         movzx   ecx, ax ; ecx - x.size
  4556.         shr     eax, 16 ; eax - x.coord
  4557.         movzx   edx, bx ; edx - y.size
  4558.         shr     ebx, 16 ; ebx - y.coord
  4559.         mov     esi, [current_slot]
  4560.  
  4561.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  4562.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  4563.         add     ecx, eax
  4564.         add     edx, ebx
  4565.         jmp     [drawbar]
  4566. .drectr:
  4567.         ret
  4568.  
  4569. align 4
  4570. syscall_getscreensize:                  ; GetScreenSize
  4571.         mov     ax, [Screen_Max_X]
  4572.         shl     eax, 16
  4573.         mov     ax, [Screen_Max_Y]
  4574.         mov     [esp + 32], eax
  4575.         ret
  4576.  
  4577. align 4
  4578.  
  4579. syscall_cdaudio:                        ; CD
  4580.  
  4581.         cmp     ebx, 4
  4582.         jb      .audio
  4583.         jz      .eject
  4584.         cmp     ebx, 5
  4585.         jnz     .ret
  4586. .load:
  4587.         call    .reserve
  4588.         call    LoadMedium
  4589.         ;call    .free
  4590.         jmp     .free
  4591. ;        ret
  4592. .eject:
  4593.         call    .reserve
  4594.         call    clear_CD_cache
  4595.         call    allow_medium_removal
  4596.         call    EjectMedium
  4597. ;        call    .free
  4598.         jmp     .free
  4599. ;        ret
  4600. .audio:
  4601.         call    sys_cd_audio
  4602.         mov     [esp+36-4], eax
  4603. .ret:
  4604.         ret
  4605.  
  4606. .reserve:
  4607.         call    reserve_cd
  4608.         mov     eax, ecx
  4609.         shr     eax, 1
  4610.         and     eax, 1
  4611.         inc     eax
  4612.         mov     [ChannelNumber], ax
  4613.         mov     eax, ecx
  4614.         and     eax, 1
  4615.         mov     [DiskNumber], al
  4616.         call    reserve_cd_channel
  4617.         and     ebx, 3
  4618.         inc     ebx
  4619.         mov     [cdpos], ebx
  4620.         add     ebx, ebx
  4621.         mov     cl, 8
  4622.         sub     cl, bl
  4623.         mov     al, [DRIVE_DATA+1]
  4624.         shr     al, cl
  4625.         test    al, 2
  4626.         jz      .free;.err
  4627.         ret
  4628. .free:
  4629.         call    free_cd_channel
  4630.         and     [cd_status], 0
  4631.         ret
  4632. .err:
  4633.         call    .free
  4634. ;        pop     eax
  4635.         ret
  4636.  
  4637. align 4
  4638.  
  4639. syscall_getpixel:                       ; GetPixel
  4640.         mov     ecx, [Screen_Max_X]
  4641.         inc     ecx
  4642.         xor     edx, edx
  4643.         mov     eax, ebx
  4644.         div     ecx
  4645.         mov     ebx, edx
  4646.         xchg    eax, ebx
  4647.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4648.         call    dword [GETPIXEL]; eax - x, ebx - y
  4649.         mov     [esp + 32], ecx
  4650.         ret
  4651.  
  4652. align 4
  4653.  
  4654. syscall_getarea:
  4655. ;eax = 36
  4656. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4657. ;ecx = [size x]*65536 + [size y]
  4658. ;edx = [start x]*65536 + [start y]
  4659.         pushad
  4660.         mov     edi, ebx
  4661.         mov     eax, edx
  4662.         shr     eax, 16
  4663.         mov     ebx, edx
  4664.         and     ebx, 0xffff
  4665.         dec     eax
  4666.         dec     ebx
  4667.      ; eax - x, ebx - y
  4668.         mov     edx, ecx
  4669.  
  4670.         shr     ecx, 16
  4671.         and     edx, 0xffff
  4672.         mov     esi, ecx
  4673.      ; ecx - size x, edx - size y
  4674.  
  4675.         mov     ebp, edx
  4676.         dec     ebp
  4677.         lea     ebp, [ebp*3]
  4678.  
  4679.         imul    ebp, esi
  4680.  
  4681.         mov     esi, ecx
  4682.         dec     esi
  4683.         lea     esi, [esi*3]
  4684.  
  4685.         add     ebp, esi
  4686.         add     ebp, edi
  4687.  
  4688.         add     ebx, edx
  4689.  
  4690. .start_y:
  4691.         push    ecx edx
  4692. .start_x:
  4693.         push    eax ebx ecx
  4694.         add     eax, ecx
  4695.  
  4696.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4697.         call    dword [GETPIXEL]; eax - x, ebx - y
  4698.  
  4699.         mov     [ebp], cx
  4700.         shr     ecx, 16
  4701.         mov     [ebp+2], cl
  4702.  
  4703.         pop     ecx ebx eax
  4704.         sub     ebp, 3
  4705.         dec     ecx
  4706.         jnz     .start_x
  4707.         pop     edx ecx
  4708.         dec     ebx
  4709.         dec     edx
  4710.         jnz     .start_y
  4711.         popad
  4712.         ret
  4713.  
  4714. align 4
  4715.  
  4716. syscall_drawline:                       ; DrawLine
  4717.  
  4718.         mov     edi, [TASK_BASE]
  4719.         movzx   eax, word[edi-twdw+WDATA.box.left]
  4720.         mov     ebp, eax
  4721.         mov     esi, [current_slot]
  4722.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4723.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  4724.         add     ebp, ebx
  4725.         shl     eax, 16
  4726.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  4727.         add     eax, ebp
  4728.         mov     ebp, ebx
  4729.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  4730.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  4731.         add     ebp, ecx
  4732.         shl     ebx, 16
  4733.         xor     edi, edi
  4734.         add     ebx, ebp
  4735.         mov     ecx, edx
  4736.         jmp     [draw_line]
  4737.  
  4738.  
  4739.  
  4740. align 4
  4741. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  4742.  
  4743.         call    r_f_port_area
  4744.         mov     [esp+32], eax
  4745.         ret
  4746.  
  4747. align 4
  4748. syscall_threads:                        ; CreateThreads
  4749. ; eax=1 create thread
  4750. ;
  4751. ;   ebx=thread start
  4752. ;   ecx=thread stack value
  4753. ;
  4754. ; on return : eax = pid
  4755.  
  4756.         call    new_sys_threads
  4757.  
  4758.         mov     [esp+32], eax
  4759.         ret
  4760.  
  4761. align 4
  4762.  
  4763. stack_driver_stat:
  4764.  
  4765.         call    app_stack_handler       ; Stack status
  4766.  
  4767. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4768. ;     call  change_task                 ; between sent packet
  4769.  
  4770.         mov     [esp+32], eax
  4771.         ret
  4772.  
  4773. align 4
  4774.  
  4775. socket:                                 ; Socket interface
  4776.         call    app_socket_handler
  4777.  
  4778. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4779. ;     call  change_task                 ; between sent packet
  4780.  
  4781.         mov     [esp+36], eax
  4782.         mov     [esp+24], ebx
  4783.         ret
  4784.  
  4785. align 4
  4786.  
  4787. read_from_hd:                           ; Read from hd - fn not in use
  4788.  
  4789.         mov     edi, [TASK_BASE]
  4790.         add     edi, TASKDATA.mem_start
  4791.         add     eax, [edi]
  4792.         add     ecx, [edi]
  4793.         add     edx, [edi]
  4794.         call    file_read
  4795.  
  4796.         mov     [esp+36], eax
  4797.         mov     [esp+24], ebx
  4798.  
  4799.         ret
  4800.  
  4801. paleholder:
  4802.         ret
  4803.  
  4804. align 4
  4805. set_screen:
  4806.         cmp     eax, [Screen_Max_X]
  4807.         jne     .set
  4808.  
  4809.         cmp     edx, [Screen_Max_Y]
  4810.         jne     .set
  4811.         ret
  4812. .set:
  4813.         pushfd
  4814.         cli
  4815.  
  4816.         mov     [Screen_Max_X], eax
  4817.         mov     [Screen_Max_Y], edx
  4818.         mov     [BytesPerScanLine], ecx
  4819.  
  4820.         mov     [screen_workarea.right], eax
  4821.         mov     [screen_workarea.bottom], edx
  4822.  
  4823.         push    ebx
  4824.         push    esi
  4825.         push    edi
  4826.  
  4827.         pushad
  4828.  
  4829.         stdcall kernel_free, [_WinMapAddress]
  4830.  
  4831.         mov     eax, [_display.width]
  4832.         mul     [_display.height]
  4833.         mov     [_WinMapSize], eax
  4834.  
  4835.         stdcall kernel_alloc, eax
  4836.         mov     [_WinMapAddress], eax
  4837.         test    eax, eax
  4838.         jz      .epic_fail
  4839.  
  4840.         popad
  4841.  
  4842.         call    repos_windows
  4843.         xor     eax, eax
  4844.         xor     ebx, ebx
  4845.         mov     ecx, [Screen_Max_X]
  4846.         mov     edx, [Screen_Max_Y]
  4847.         call    calculatescreen
  4848.         pop     edi
  4849.         pop     esi
  4850.         pop     ebx
  4851.  
  4852.         popfd
  4853.         ret
  4854.  
  4855. .epic_fail:
  4856.         hlt                     ; Houston, we've had a problem
  4857.  
  4858. ; --------------- APM ---------------------
  4859. uglobal
  4860. apm_entry       dp      0
  4861. apm_vf          dd      0
  4862. endg
  4863.  
  4864. align 4
  4865. sys_apm:
  4866.         xor     eax, eax
  4867.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4868.         jne     @f
  4869.         inc     eax
  4870.         or      dword [esp + 44], eax   ; error
  4871.         add     eax, 7
  4872.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  4873.         ret
  4874.  
  4875. @@:
  4876. ;       xchg    eax, ecx
  4877. ;       xchg    ebx, ecx
  4878.  
  4879.         cmp     dx, 3
  4880.         ja      @f
  4881.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4882.         mov     eax, [apm_vf]
  4883.         mov     [esp + 32], eax
  4884.         shr     eax, 16
  4885.         mov     [esp + 28], eax
  4886.         ret
  4887.  
  4888. @@:
  4889.  
  4890.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4891.         xchg    [master_tab], esi
  4892.         push    esi
  4893.         mov     edi, cr3
  4894.         mov     cr3, edi                ;flush TLB
  4895.  
  4896.         call    pword [apm_entry]       ;call APM BIOS
  4897.  
  4898.         xchg    eax, [esp]
  4899.         mov     [master_tab], eax
  4900.         mov     eax, cr3
  4901.         mov     cr3, eax
  4902.         pop     eax
  4903.  
  4904.         mov     [esp + 4 ], edi
  4905.         mov     [esp + 8], esi
  4906.         mov     [esp + 20], ebx
  4907.         mov     [esp + 24], edx
  4908.         mov     [esp + 28], ecx
  4909.         mov     [esp + 32], eax
  4910.         setc    al
  4911.         and     [esp + 44], byte 0xfe
  4912.         or      [esp + 44], al
  4913.         ret
  4914. ; -----------------------------------------
  4915.  
  4916. align 4
  4917.  
  4918. undefined_syscall:                      ; Undefined system call
  4919.         mov     [esp + 32], dword -1
  4920.         ret
  4921.  
  4922. align 4
  4923. system_shutdown:          ; shut down the system
  4924.  
  4925.         cmp     byte [BOOT_VAR+0x9030], 1
  4926.         jne     @F
  4927.         ret
  4928. @@:
  4929.         call    stop_all_services
  4930.         push    3                ; stop playing cd
  4931.         pop     eax
  4932.         call    sys_cd_audio
  4933.  
  4934. yes_shutdown_param:
  4935.         cli
  4936.  
  4937. if ~ defined extended_primary_loader
  4938.         mov     eax, kernel_file ; load kernel.mnt to 0x7000:0
  4939.         push    12
  4940.         pop     esi
  4941.         xor     ebx, ebx
  4942.         or      ecx, -1
  4943.         mov     edx, OS_BASE+0x70000
  4944.         call    fileread
  4945.  
  4946.         mov     esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  4947.         mov     edi, OS_BASE+0x40000
  4948.         mov     ecx, 1000
  4949.         rep movsb
  4950. end if
  4951.  
  4952.         mov     esi, BOOT_VAR    ; restore 0x0 - 0xffff
  4953.         mov     edi, OS_BASE
  4954.         mov     ecx, 0x10000/4
  4955.         cld
  4956.         rep movsd
  4957.  
  4958.         call    restorefatchain
  4959.  
  4960.         call    IRQ_mask_all
  4961.  
  4962. if 0
  4963.         mov     word [OS_BASE+0x467+0], pr_mode_exit
  4964.         mov     word [OS_BASE+0x467+2], 0x1000
  4965.  
  4966.         mov     al, 0x0F
  4967.         out     0x70, al
  4968.         mov     al, 0x05
  4969.         out     0x71, al
  4970.  
  4971.         mov     al, 0xFE
  4972.         out     0x64, al
  4973.  
  4974.         hlt
  4975.         jmp     $-1
  4976.  
  4977. else
  4978.         cmp     byte [OS_BASE + 0x9030], 2
  4979.         jnz     no_acpi_power_off
  4980.  
  4981. ; scan for RSDP
  4982. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  4983.         movzx   eax, word [OS_BASE + 0x40E]
  4984.         shl     eax, 4
  4985.         jz      @f
  4986.         mov     ecx, 1024/16
  4987.         call    scan_rsdp
  4988.         jnc     .rsdp_found
  4989. @@:
  4990. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  4991.         mov     eax, 0xE0000
  4992.         mov     ecx, 0x2000
  4993.         call    scan_rsdp
  4994.         jc      no_acpi_power_off
  4995. .rsdp_found:
  4996.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  4997.         mov     ebp, [ipc_tmp]
  4998.         stdcall map_page, ebp, esi, PG_MAP
  4999.         lea     eax, [esi+1000h]
  5000.         lea     edx, [ebp+1000h]
  5001.         stdcall map_page, edx, eax, PG_MAP
  5002.         and     esi, 0xFFF
  5003.         add     esi, ebp
  5004.         cmp     dword [esi], 'RSDT'
  5005.         jnz     no_acpi_power_off
  5006.         mov     ecx, [esi+4]
  5007.         sub     ecx, 24h
  5008.         jbe     no_acpi_power_off
  5009.         shr     ecx, 2
  5010.         add     esi, 24h
  5011. .scan_fadt:
  5012.         lodsd
  5013.         mov     ebx, eax
  5014.         lea     eax, [ebp+2000h]
  5015.         stdcall map_page, eax, ebx, PG_MAP
  5016.         lea     eax, [ebp+3000h]
  5017.         add     ebx, 0x1000
  5018.         stdcall map_page, eax, ebx, PG_MAP
  5019.         and     ebx, 0xFFF
  5020.         lea     ebx, [ebx+ebp+2000h]
  5021.         cmp     dword [ebx], 'FACP'
  5022.         jz      .fadt_found
  5023.         loop    .scan_fadt
  5024.         jmp     no_acpi_power_off
  5025. .fadt_found:
  5026. ; ebx is linear address of FADT
  5027.         mov     edi, [ebx+40] ; physical address of the DSDT
  5028.         lea     eax, [ebp+4000h]
  5029.         stdcall map_page, eax, edi, PG_MAP
  5030.         lea     eax, [ebp+5000h]
  5031.         lea     esi, [edi+0x1000]
  5032.         stdcall map_page, eax, esi, PG_MAP
  5033.         and     esi, 0xFFF
  5034.         sub     edi, esi
  5035.         cmp     dword [esi+ebp+4000h], 'DSDT'
  5036.         jnz     no_acpi_power_off
  5037.         mov     eax, [esi+ebp+4004h] ; DSDT length
  5038.         sub     eax, 36+4
  5039.         jbe     no_acpi_power_off
  5040.         add     esi, 36
  5041. .scan_dsdt:
  5042.         cmp     dword [esi+ebp+4000h], '_S5_'
  5043.         jnz     .scan_dsdt_cont
  5044.         cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
  5045.         jnz     .scan_dsdt_cont
  5046.         mov     dl, [esi+ebp+4000h+6]
  5047.         cmp     dl, 4 ; _S5_ package must contain 4 bytes
  5048.                       ; ...in theory; in practice, VirtualBox has 2 bytes
  5049.         ja      .scan_dsdt_cont
  5050.         cmp     dl, 1
  5051.         jb      .scan_dsdt_cont
  5052.         lea     esi, [esi+ebp+4000h+7]
  5053.         xor     ecx, ecx
  5054.         cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
  5055.         jz      @f
  5056.         cmp     byte [esi], 0xA
  5057.         jnz     no_acpi_power_off
  5058.         inc     esi
  5059.         mov     cl, [esi]
  5060. @@:
  5061.         inc     esi
  5062.         cmp     dl, 2
  5063.         jb      @f
  5064.         cmp     byte [esi], 0
  5065.         jz      @f
  5066.         cmp     byte [esi], 0xA
  5067.         jnz     no_acpi_power_off
  5068.         inc     esi
  5069.         mov     ch, [esi]
  5070. @@:
  5071.         jmp     do_acpi_power_off
  5072. .scan_dsdt_cont:
  5073.         inc     esi
  5074.         cmp     esi, 0x1000
  5075.         jb      @f
  5076.         sub     esi, 0x1000
  5077.         add     edi, 0x1000
  5078.         push    eax
  5079.         lea     eax, [ebp+4000h]
  5080.         stdcall map_page, eax, edi, PG_MAP
  5081.         push    PG_MAP
  5082.         lea     eax, [edi+1000h]
  5083.         push    eax
  5084.         lea     eax, [ebp+5000h]
  5085.         push    eax
  5086.         stdcall map_page
  5087.         pop     eax
  5088. @@:
  5089.         dec     eax
  5090.         jnz     .scan_dsdt
  5091.         jmp     no_acpi_power_off
  5092. do_acpi_power_off:
  5093.         mov     edx, [ebx+48]
  5094.         test    edx, edx
  5095.         jz      .nosmi
  5096.         mov     al, [ebx+52]
  5097.         out     dx, al
  5098.         mov     edx, [ebx+64]
  5099. @@:
  5100.         in      ax, dx
  5101.         test    al, 1
  5102.         jz      @b
  5103. .nosmi:
  5104.         and     cx, 0x0707
  5105.         shl     cx, 2
  5106.         or      cx, 0x2020
  5107.         mov     edx, [ebx+64]
  5108.         in      ax, dx
  5109.         and     ax, 203h
  5110.         or      ah, cl
  5111.         out     dx, ax
  5112.         mov     edx, [ebx+68]
  5113.         test    edx, edx
  5114.         jz      @f
  5115.         in      ax, dx
  5116.         and     ax, 203h
  5117.         or      ah, ch
  5118.         out     dx, ax
  5119. @@:
  5120.         jmp     $
  5121.  
  5122.  
  5123. no_acpi_power_off:
  5124.         mov     word [OS_BASE+0x467+0], pr_mode_exit
  5125.         mov     word [OS_BASE+0x467+2], 0x1000
  5126.  
  5127.         mov     al, 0x0F
  5128.         out     0x70, al
  5129.         mov     al, 0x05
  5130.         out     0x71, al
  5131.  
  5132.         mov     al, 0xFE
  5133.         out     0x64, al
  5134.  
  5135.         hlt
  5136.         jmp     $-1
  5137.  
  5138. scan_rsdp:
  5139.         add     eax, OS_BASE
  5140. .s:
  5141.         cmp     dword [eax], 'RSD '
  5142.         jnz     .n
  5143.         cmp     dword [eax+4], 'PTR '
  5144.         jnz     .n
  5145.         xor     edx, edx
  5146.         xor     esi, esi
  5147. @@:
  5148.         add     dl, [eax+esi]
  5149.         inc     esi
  5150.         cmp     esi, 20
  5151.         jnz     @b
  5152.         test    dl, dl
  5153.         jz      .ok
  5154. .n:
  5155.         add     eax, 10h
  5156.         loop    .s
  5157.         stc
  5158. .ok:
  5159.         ret
  5160. end if
  5161.  
  5162. include "data32.inc"
  5163.  
  5164. __REV__ = __REV
  5165.  
  5166. uglobals_size = $ - endofcode
  5167. diff16 "end of kernel code",0,$
  5168.