Subversion Repositories Kolibri OS

Rev

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