Subversion Repositories Kolibri OS

Rev

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