Subversion Repositories Kolibri OS

Rev

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

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