Subversion Repositories Kolibri OS

Rev

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