Subversion Repositories Kolibri OS

Rev

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

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