Subversion Repositories Kolibri OS

Rev

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

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