Subversion Repositories Kolibri OS

Rev

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