Subversion Repositories Kolibri OS

Rev

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

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