Subversion Repositories Kolibri OS

Rev

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