Subversion Repositories Kolibri OS

Rev

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