Subversion Repositories Kolibri OS

Rev

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

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