Subversion Repositories Kolibri OS

Rev

Rev 1063 | Rev 1071 | 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: 1067 $
  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.fpu_handler], eax
  679.         mov  dword [SLOT_BASE+APPDATA.sse_handler], 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.fpu_handler], eax
  701.         mov dword [SLOT_BASE+256+APPDATA.sse_handler], 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. bgrlock db 0
  2258. bgrlockpid dd 0
  2259. endg
  2260.  
  2261. sys_background:
  2262.  
  2263.     cmp   ebx,1                            ; BACKGROUND SIZE
  2264.     jnz   nosb1
  2265.     cmp   ecx,0
  2266.     je    sbgrr
  2267.     cmp   edx,0
  2268.     je    sbgrr
  2269. @@:
  2270.         mov     al, 1
  2271.         xchg    [bgrlock], al
  2272.         test    al, al
  2273.         jz      @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. @@:
  2372.         mov     al, 1
  2373.         xchg    [bgrlock], al
  2374.         test    al, al
  2375.         jz      @f
  2376.         call    change_task
  2377.         jmp     @b
  2378. @@:
  2379.         mov     eax, [CURRENT_TASK]
  2380.         mov     [bgrlockpid], eax
  2381.         stdcall user_alloc, [mem_BACKGROUND]
  2382.         mov     [esp+32], eax
  2383.         test    eax, eax
  2384.         jz      .nomem
  2385.         mov     ebx, eax
  2386.         shr     ebx, 12
  2387.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2388.         mov     esi, [img_background]
  2389.         shr     esi, 12
  2390.         mov     ecx, [mem_BACKGROUND]
  2391.         add     ecx, 0xFFF
  2392.         shr     ecx, 12
  2393. .z:
  2394.         mov     eax, [page_tabs+ebx*4]
  2395.         test    al, 1
  2396.         jz      @f
  2397.         call    free_page
  2398. @@:
  2399.         mov     eax, [page_tabs+esi*4]
  2400.         or      al, PG_UW
  2401.         mov     [page_tabs+ebx*4], eax
  2402.         mov     eax, ebx
  2403.         shl     eax, 12
  2404.         invlpg  [eax]
  2405.         inc     ebx
  2406.         inc     esi
  2407.         loop    .z
  2408.         ret
  2409. .nomem:
  2410.         and     [bgrlockpid], 0
  2411.         mov     [bgrlock], 0
  2412. nosb6:
  2413.         cmp     ebx, 7
  2414.         jnz     nosb7
  2415.         cmp     [bgrlock], 0
  2416.         jz      .err
  2417.         mov     eax, [CURRENT_TASK]
  2418.         cmp     [bgrlockpid], eax
  2419.         jnz     .err
  2420.         mov     eax, ecx
  2421.         mov     ebx, ecx
  2422.         shr     eax, 12
  2423.         mov     ecx, [page_tabs+(eax-1)*4]
  2424.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2425.         jz      .err
  2426.         jnp     .err
  2427.         push    eax
  2428.         shr     ecx, 12
  2429. @@:
  2430.         and     dword [page_tabs+eax*4], 0
  2431.         mov     edx, eax
  2432.         shl     edx, 12
  2433.         push eax
  2434.         invlpg  [edx]
  2435.         pop eax
  2436.         inc     eax
  2437.         loop    @b
  2438.         pop     eax
  2439.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2440.         stdcall user_free, ebx
  2441.         mov     [esp+32], eax
  2442.         and     [bgrlockpid], 0
  2443.         mov     [bgrlock], 0
  2444.         ret
  2445. .err:
  2446.         and     dword [esp+32], 0
  2447.         ret
  2448.  
  2449. nosb7:
  2450.     ret
  2451.  
  2452. force_redraw_background:
  2453.     mov   [draw_data+32 + RECT.left],dword 0
  2454.     mov   [draw_data+32 + RECT.top],dword 0
  2455.     push  eax ebx
  2456.     mov   eax,[Screen_Max_X]
  2457.     mov   ebx,[Screen_Max_Y]
  2458.     mov   [draw_data+32 + RECT.right],eax
  2459.     mov   [draw_data+32 + RECT.bottom],ebx
  2460.     pop   ebx eax
  2461.     mov   byte [REDRAW_BACKGROUND], 1
  2462.     ret
  2463.  
  2464. align 4
  2465.  
  2466. sys_getbackground:
  2467.  
  2468.     cmp   eax,1                                  ; SIZE
  2469.     jnz   nogb1
  2470.     mov   eax,[BgrDataWidth]
  2471.     shl   eax,16
  2472.     mov   ax,[BgrDataHeight]
  2473.     mov   [esp+36],eax
  2474.     ret
  2475.  
  2476. nogb1:
  2477.  
  2478.     cmp   eax,2                                  ; PIXEL
  2479.     jnz   nogb2
  2480.  
  2481.     mov ecx, [mem_BACKGROUND]
  2482.     add ecx, 4095
  2483.     and ecx, -4096
  2484.     sub ecx, 4
  2485.     cmp ebx, ecx
  2486.     ja  @F
  2487.  
  2488.     mov   eax,[img_background]
  2489.     mov   eax,[ebx+eax]
  2490.  
  2491.     and   eax, 0xFFFFFF
  2492.     mov   [esp+36],eax
  2493. @@:
  2494.     ret
  2495.   nogb2:
  2496.  
  2497.     cmp   eax,4                                  ; TILED / STRETCHED
  2498.     jnz   nogb4
  2499.     mov   eax,[BgrDrawMode]
  2500.   nogb4:
  2501.     mov   [esp+36],eax
  2502.     ret
  2503.  
  2504.  
  2505. align 4
  2506.  
  2507. sys_getkey:
  2508.         mov     [esp + 32],dword 1
  2509.         ; test main buffer
  2510.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2511.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2512.         mov     edx, [TASK_COUNT]
  2513.         cmp     ecx, edx
  2514.         jne     .finish
  2515.         cmp     [KEY_COUNT], byte 0
  2516.         je      .finish
  2517.         movzx   eax, byte [KEY_BUFF]
  2518.         shl     eax, 8
  2519.         push    eax
  2520.         dec     byte [KEY_COUNT]
  2521.         and     byte [KEY_COUNT], 127
  2522.         movzx   ecx, byte [KEY_COUNT]
  2523.         add     ecx, 2
  2524.         mov     eax, KEY_BUFF + 1
  2525.         mov     ebx, KEY_BUFF
  2526.         call    memmove
  2527.         pop     eax
  2528. .ret_eax:
  2529.         mov     [esp + 32], eax
  2530.         ret
  2531. .finish:
  2532. ; test hotkeys buffer
  2533.         mov     ecx, hotkey_buffer
  2534. @@:
  2535.         cmp     [ecx], ebx
  2536.         jz      .found
  2537.         add     ecx, 8
  2538.         cmp     ecx, hotkey_buffer + 120 * 8
  2539.         jb      @b
  2540.         ret
  2541. .found:
  2542.         mov     ax, [ecx + 6]
  2543.         shl     eax, 16
  2544.         mov     ah, [ecx + 4]
  2545.         mov     al, 2
  2546.         and     dword [ecx + 4], 0
  2547.         and     dword [ecx], 0
  2548.         jmp     .ret_eax
  2549.  
  2550. align 4
  2551.  
  2552. sys_getbutton:
  2553.  
  2554.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2555.         mov     [esp + 32], dword 1
  2556.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2557.         mov     edx, [TASK_COUNT] ; less than 256 processes
  2558.         cmp     ecx, edx
  2559.         jne     .exit
  2560.         movzx   eax, byte [BTN_COUNT]
  2561.         test    eax, eax
  2562.         jz      .exit
  2563.         mov     eax, [BTN_BUFF]
  2564.         shl     eax, 8
  2565. ; // Alver 22.06.2008 // {
  2566.         mov       al, byte [btn_down_determ]
  2567.         and       al,0xFE                                       ; delete left button bit
  2568. ; } \\ Alver \\
  2569.         mov     [BTN_COUNT], byte 0
  2570.         mov     [esp + 32], eax
  2571. .exit:
  2572.         ret
  2573.  
  2574.  
  2575. align 4
  2576.  
  2577. sys_cpuusage:
  2578.  
  2579. ;  RETURN:
  2580. ;
  2581. ;  +00 dword     process cpu usage
  2582. ;  +04  word     position in windowing stack
  2583. ;  +06  word     windowing stack value at current position (cpu nro)
  2584. ;  +10 12 bytes  name
  2585. ;  +22 dword     start in mem
  2586. ;  +26 dword     used mem
  2587. ;  +30 dword     PID , process idenfification number
  2588. ;
  2589.  
  2590.     cmp  ecx,-1         ; who am I ?
  2591.     jne  .no_who_am_i
  2592.     mov  ecx,[CURRENT_TASK]
  2593.   .no_who_am_i:
  2594.         cmp     ecx, max_processes
  2595.         ja      .nofillbuf
  2596.  
  2597. ; +4: word: position of the window of thread in the window stack
  2598.         mov     ax, [WIN_STACK + ecx * 2]
  2599.         mov     [ebx+4], ax
  2600. ; +6: word: number of the thread slot, which window has in the window stack
  2601. ;           position ecx (has no relation to the specific thread)
  2602.         mov     ax, [WIN_POS + ecx * 2]
  2603.         mov     [ebx+6], ax
  2604.  
  2605.         shl     ecx, 5
  2606.  
  2607. ; +0: dword: memory usage
  2608.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  2609.         mov     [ebx], eax
  2610. ; +10: 11 bytes: name of the process
  2611.         push    ecx
  2612.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  2613.         add     ebx, 10
  2614.         mov     ecx, 11
  2615.         call    memmove
  2616.         pop     ecx
  2617.  
  2618. ; +22: address of the process in memory
  2619. ; +26: size of used memory - 1
  2620.         push    edi
  2621.         lea     edi, [ebx+12]
  2622.         xor     eax, eax
  2623.         mov     edx, 0x100000*16
  2624.         cmp     ecx, 1 shl 5
  2625.         je      .os_mem
  2626.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
  2627.         mov     eax, std_application_base_address
  2628. .os_mem:
  2629.         stosd
  2630.         lea     eax, [edx-1]
  2631.         stosd
  2632.  
  2633. ; +30: PID/TID
  2634.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  2635.         stosd
  2636.  
  2637.     ; window position and size
  2638.         push    esi
  2639.         lea     esi, [ecx + window_data + WDATA.box]
  2640.         movsd
  2641.         movsd
  2642.         movsd
  2643.         movsd
  2644.  
  2645.     ; Process state (+50)
  2646.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  2647.         stosd
  2648.  
  2649.     ; Window client area box
  2650.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2651.         movsd
  2652.         movsd
  2653.         movsd
  2654.         movsd
  2655.  
  2656.     ; Window state
  2657.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2658.         stosb
  2659.  
  2660.         pop     esi
  2661.         pop     edi
  2662.  
  2663. .nofillbuf:
  2664.     ; return number of processes
  2665.  
  2666.     mov    eax,[TASK_COUNT]
  2667.     mov    [esp+32],eax
  2668.     ret
  2669.  
  2670. align 4
  2671. sys_clock:
  2672.         cli
  2673.   ; Mikhail Lisovin  xx Jan 2005
  2674.   @@:   mov   al, 10
  2675.         out   0x70, al
  2676.         in    al, 0x71
  2677.         test  al, al
  2678.         jns   @f
  2679.         mov   esi, 1
  2680.         call  delay_ms
  2681.         jmp   @b
  2682.   @@:
  2683.   ; end Lisovin's fix
  2684.  
  2685.         xor   al,al           ; seconds
  2686.         out   0x70,al
  2687.         in    al,0x71
  2688.         movzx ecx,al
  2689.         mov   al,02           ; minutes
  2690.         shl   ecx,16
  2691.         out   0x70,al
  2692.         in    al,0x71
  2693.         movzx edx,al
  2694.         mov   al,04           ; hours
  2695.         shl   edx,8
  2696.         out   0x70,al
  2697.         in    al,0x71
  2698.         add   ecx,edx
  2699.         movzx edx,al
  2700.         add   ecx,edx
  2701.         sti
  2702.         mov     [esp + 32], ecx
  2703.         ret
  2704.  
  2705.  
  2706. align 4
  2707.  
  2708. sys_date:
  2709.  
  2710.         cli
  2711.   @@:   mov   al, 10
  2712.         out   0x70, al
  2713.         in    al, 0x71
  2714.         test  al, al
  2715.         jns   @f
  2716.         mov   esi, 1
  2717.         call  delay_ms
  2718.         jmp   @b
  2719.   @@:
  2720.  
  2721.         mov     ch,0
  2722.         mov     al,7            ; date
  2723.         out     0x70,al
  2724.         in      al,0x71
  2725.         mov     cl,al
  2726.         mov     al,8            ; month
  2727.         shl     ecx,16
  2728.         out     0x70,al
  2729.         in      al,0x71
  2730.         mov     ch,al
  2731.         mov     al,9            ; year
  2732.         out     0x70,al
  2733.         in      al,0x71
  2734.         mov     cl,al
  2735.         sti
  2736.         mov     [esp+32], ecx
  2737.         ret
  2738.  
  2739.  
  2740. ; redraw status
  2741.  
  2742. sys_redrawstat:
  2743.         cmp     ebx, 1
  2744.         jne     no_widgets_away
  2745.         ; buttons away
  2746.         mov     ecx,[CURRENT_TASK]
  2747.   sys_newba2:
  2748.         mov     edi,[BTN_ADDR]
  2749.         cmp     [edi], dword 0  ; empty button list ?
  2750.         je      end_of_buttons_away
  2751.         movzx   ebx, word [edi]
  2752.         inc     ebx
  2753.         mov     eax,edi
  2754.   sys_newba:
  2755.         dec     ebx
  2756.         jz      end_of_buttons_away
  2757.  
  2758.         add     eax, 0x10
  2759.         cmp     cx, [eax]
  2760.         jnz     sys_newba
  2761.  
  2762.         push    eax ebx ecx
  2763.         mov     ecx,ebx
  2764.         inc     ecx
  2765.         shl     ecx, 4
  2766.         mov     ebx, eax
  2767.         add     eax, 0x10
  2768.         call    memmove
  2769.         dec     dword [edi]
  2770.         pop     ecx ebx eax
  2771.  
  2772.         jmp     sys_newba2
  2773.  
  2774.   end_of_buttons_away:
  2775.  
  2776.         ret
  2777.  
  2778.   no_widgets_away:
  2779.  
  2780.         cmp     ebx, 2
  2781.         jnz     srl1
  2782.  
  2783.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2784.         add     edx, draw_data - CURRENT_TASK
  2785.         mov     [edx + RECT.left], 0
  2786.         mov     [edx + RECT.top], 0
  2787.         mov     eax, [Screen_Max_X]
  2788.         mov     [edx + RECT.right], eax
  2789.         mov     eax, [Screen_Max_Y]
  2790.         mov     [edx + RECT.bottom], eax
  2791.  
  2792.         mov     edi, [TASK_BASE]
  2793.         or      [edi - twdw + WDATA.fl_wdrawn], 1   ; no new position & buttons from app
  2794.         call    sys_window_mouse
  2795.         ret
  2796.  
  2797.   srl1:
  2798.         ret
  2799.  
  2800.  
  2801. sys_drawwindow:
  2802.  
  2803.     mov   eax,edx
  2804.     shr   eax,16+8
  2805.     and   eax,15
  2806.  
  2807. ;    cmp   eax,0   ; type I    - original style
  2808.     jne   nosyswI
  2809.     inc   [mouse_pause]
  2810.     call  [disable_mouse]
  2811.     call  sys_set_window
  2812.     call  [disable_mouse]
  2813.     call  drawwindow_I
  2814.     ;dec   [mouse_pause]
  2815.     ;call   [draw_pointer]
  2816.     ;ret
  2817.     jmp   draw_window_caption.2
  2818.   nosyswI:
  2819.  
  2820.     cmp   al,1    ; type II   - only reserve area, no draw
  2821.     jne   nosyswII
  2822.     inc   [mouse_pause]
  2823.     call  [disable_mouse]
  2824.     call  sys_set_window
  2825.     call  [disable_mouse]
  2826.     call  sys_window_mouse
  2827.     dec   [mouse_pause]
  2828.     call   [draw_pointer]
  2829.     ret
  2830.   nosyswII:
  2831.  
  2832.     cmp   al,2    ; type III  - new style
  2833.     jne   nosyswIII
  2834.     inc   [mouse_pause]
  2835.     call  [disable_mouse]
  2836.     call  sys_set_window
  2837.     call  [disable_mouse]
  2838.     call  drawwindow_III
  2839.     ;dec   [mouse_pause]
  2840.     ;call   [draw_pointer]
  2841.     ;ret
  2842.     jmp   draw_window_caption.2
  2843.   nosyswIII:
  2844.  
  2845.     cmp   al,3    ; type IV - skinned window
  2846.     je    draw_skin_window
  2847.     cmp   al,4    ; type V - skinned window not sized! {not_sized_skin_window}
  2848.     jne   nosyswV
  2849.   draw_skin_window:
  2850.  
  2851.     inc   [mouse_pause]
  2852.     call  [disable_mouse]
  2853.     call  sys_set_window
  2854.     call  [disable_mouse]
  2855.     mov   eax, [TASK_COUNT]
  2856.     movzx eax, word [WIN_POS + eax*2]
  2857.     cmp   eax, [CURRENT_TASK]
  2858.     setz  al
  2859.     movzx eax, al
  2860.     push  eax
  2861.     call  drawwindow_IV
  2862.     ;dec   [mouse_pause]
  2863.     ;call   [draw_pointer]
  2864.     ;ret
  2865.     jmp   draw_window_caption.2
  2866.   nosyswV:
  2867.  
  2868.     ret
  2869.  
  2870.  
  2871. draw_window_caption:
  2872.         inc     [mouse_pause]
  2873.         call    [disable_mouse]
  2874.  
  2875.         xor     eax,eax
  2876.         mov     edx,[TASK_COUNT]
  2877.         movzx   edx,word[WIN_POS+edx*2]
  2878.         cmp     edx,[CURRENT_TASK]
  2879.         jne     @f
  2880.         inc     eax
  2881.     @@: mov     edx,[CURRENT_TASK]
  2882.         shl     edx,5
  2883.         add     edx,window_data
  2884.         movzx   ebx,[edx+WDATA.fl_wstyle]
  2885.         and     bl,0x0F
  2886.         cmp     bl,3
  2887.         je      .draw_caption_style_3           ;{for 3 and 4 style write caption}
  2888.         cmp     bl,4
  2889.         je      .draw_caption_style_3
  2890.  
  2891.         jmp     .not_style_3
  2892.   .draw_caption_style_3:
  2893.  
  2894.         push    edx
  2895.         call    drawwindow_IV_caption
  2896.         add     esp,4
  2897.         jmp     .2
  2898.  
  2899.   .not_style_3:
  2900.         cmp     bl,2
  2901.         jne     .not_style_2
  2902.  
  2903.         call    drawwindow_III_caption
  2904.         jmp     .2
  2905.  
  2906.   .not_style_2:
  2907.         cmp     bl,0
  2908.         jne     .2
  2909.  
  2910.         call    drawwindow_I_caption
  2911.  
  2912. ;--------------------------------------------------------------
  2913.   .2:   ;jmp     @f
  2914.         mov     edi,[CURRENT_TASK]
  2915.         shl     edi,5
  2916.         test    [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  2917.         jz      @f
  2918.         mov     edx,[edi*8+SLOT_BASE+APPDATA.wnd_caption]
  2919.         or      edx,edx
  2920.         jz      @f
  2921.  
  2922.         movzx   eax,[edi+window_data+WDATA.fl_wstyle]
  2923.         and     al,0x0F
  2924.         cmp     al,3
  2925.         je      .skinned
  2926.         cmp     al,4
  2927.         je      .skinned
  2928.  
  2929.         jmp     .not_skinned
  2930.   .skinned:
  2931.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  2932.         mov     bp,word[edi+window_data+WDATA.box.top]
  2933.         movzx   eax,word[edi+window_data+WDATA.box.width]
  2934.         sub     ax,[_skinmargins.left]
  2935.         sub     ax,[_skinmargins.right]
  2936.         push    edx
  2937.         cwde
  2938.         cdq
  2939.         mov     ebx,6
  2940.         idiv    ebx
  2941.         pop     edx
  2942.         or      eax,eax
  2943.         js      @f
  2944.         mov     esi,eax
  2945.         mov     ebx,dword[_skinmargins.left-2]
  2946.         mov     bx,word[_skinh]
  2947.         sub     bx,[_skinmargins.bottom]
  2948.         sub     bx,[_skinmargins.top]
  2949.         sar     bx,1
  2950.         adc     bx,0
  2951.         add     bx,[_skinmargins.top]
  2952.         add     bx,-3
  2953.         add     ebx,ebp
  2954.         jmp     .dodraw
  2955.  
  2956.   .not_skinned:
  2957.         cmp     al,1
  2958.         je      @f
  2959.  
  2960.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  2961.         mov     bp,word[edi+window_data+WDATA.box.top]
  2962.         movzx   eax,word[edi+window_data+WDATA.box.width]
  2963.         sub     eax,16
  2964.         push    edx
  2965.         cwde
  2966.         cdq
  2967.         mov     ebx,6
  2968.         idiv    ebx
  2969.         pop     edx
  2970.         or      eax,eax
  2971.         js      @f
  2972.         mov     esi,eax
  2973.         mov     ebx,0x00080007
  2974.         add     ebx,ebp
  2975. .dodraw:
  2976.         mov     ecx,[common_colours+16];0x00FFFFFF
  2977.         or      ecx, 0x80000000
  2978.         xor     edi,edi
  2979. ; // Alver 22.06.2008 // {
  2980. ;       call    dtext
  2981.         call dtext_asciiz_esi
  2982. ; } \\ Alver \\
  2983.  
  2984.     @@:
  2985. ;--------------------------------------------------------------
  2986.         dec     [mouse_pause]
  2987.         call    [draw_pointer]
  2988.         ret
  2989.  
  2990. iglobal
  2991. align 4
  2992. window_topleft dd \
  2993.   1, 21,\               ;type 0
  2994.   0,  0,\       ;type 1
  2995.   5, 20,\       ;type 2
  2996.   5,  ?,\       ;type 3 {set by skin}
  2997.   5,  ?         ;type 4 {set by skin}
  2998. endg
  2999.  
  3000. set_window_clientbox:
  3001.         push    eax ecx edi
  3002.  
  3003.         mov     eax,[_skinh]
  3004.         mov     [window_topleft+4*7],eax
  3005.         mov     [window_topleft+4*9],eax
  3006.  
  3007.         mov     ecx,edi
  3008.         sub     edi,window_data
  3009.         shl     edi,3
  3010.         test    [ecx+WDATA.fl_wstyle],WSTYLE_CLIENTRELATIVE
  3011.         jz      @f
  3012.  
  3013.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3014.         and     eax,0x0F
  3015.         mov     eax,[eax*8+window_topleft+0]
  3016.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3017.         shl     eax,1
  3018.         neg     eax
  3019.         add     eax,[ecx+WDATA.box.width]
  3020.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3021.  
  3022.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3023.         and     eax,0x0F
  3024.         push    [eax*8+window_topleft+0]
  3025.         mov     eax,[eax*8+window_topleft+4]
  3026.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3027.         neg     eax
  3028.         sub     eax,[esp]
  3029.         add     eax,[ecx+WDATA.box.height]
  3030.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3031.         add     esp,4
  3032.  
  3033.         pop     edi ecx eax
  3034.         ret
  3035.     @@:
  3036.         xor     eax,eax
  3037.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3038.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3039.         mov     eax,[ecx+WDATA.box.width]
  3040.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3041.         mov     eax,[ecx+WDATA.box.height]
  3042.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3043.  
  3044.         pop     edi ecx eax
  3045.         ret
  3046.  
  3047. sys_set_window:
  3048.  
  3049.     mov   eax,[CURRENT_TASK]
  3050.     shl   eax,5
  3051.     add   eax,window_data
  3052.  
  3053.     ; colors
  3054.     mov   [eax+WDATA.cl_workarea],edx
  3055.     mov   [eax+WDATA.cl_titlebar],esi
  3056.     mov   [eax+WDATA.cl_frames],edi
  3057.  
  3058.     mov   edi, eax
  3059.  
  3060.     ; check flag (?)
  3061.     test  [edi+WDATA.fl_wdrawn],1
  3062.     jnz   newd
  3063.  
  3064.     mov   eax,[timer_ticks] ;[0xfdf0]
  3065.     add   eax,100
  3066.     mov   [new_window_starting],eax
  3067.  
  3068.     movsx eax,bx
  3069.     mov   [edi+WDATA.box.width],eax
  3070.     movsx eax,cx
  3071.     mov   [edi+WDATA.box.height],eax
  3072.     sar   ebx,16
  3073.     sar   ecx,16
  3074.     mov   [edi+WDATA.box.left],ebx
  3075.     mov   [edi+WDATA.box.top],ecx
  3076.  
  3077.     call  check_window_position
  3078.  
  3079.     call  set_window_clientbox
  3080.  
  3081.     push  ecx esi edi               ; save for window fullscreen/resize
  3082.     ;mov   esi,edi
  3083.  
  3084.         mov     cl, [edi+WDATA.fl_wstyle]
  3085.         mov     eax, [edi+WDATA.cl_frames]
  3086.  
  3087.     sub   edi,window_data
  3088.     shl   edi,3
  3089.     add   edi,SLOT_BASE
  3090.  
  3091.         and     cl,0x0F
  3092.         mov     [edi+APPDATA.wnd_caption],0
  3093.         cmp     cl,3
  3094.         je      set_APPDATA_wnd_caption
  3095.         cmp     cl,4                                                            ; {SPraid.simba}
  3096.         je      set_APPDATA_wnd_caption
  3097.  
  3098.         jmp     @f
  3099.     set_APPDATA_wnd_caption:
  3100.         mov     [edi+APPDATA.wnd_caption],eax
  3101.     @@: mov     esi,[esp+0]
  3102.  
  3103.     add   edi, APPDATA.saved_box
  3104.         movsd
  3105.         movsd
  3106.         movsd
  3107.         movsd
  3108.     pop   edi esi ecx
  3109.  
  3110.         mov     esi, [CURRENT_TASK]
  3111.         movzx   esi, word [WIN_STACK+esi*2]
  3112.         lea     esi, [WIN_POS+esi*2]
  3113.         call    waredraw
  3114.  
  3115. ;;;    mov   ebx, 1
  3116. ;;;    call  delay_hs
  3117.     mov   eax, [edi+WDATA.box.left]
  3118.     mov   ebx, [edi+WDATA.box.top]
  3119.     mov   ecx, [edi+WDATA.box.width]
  3120.     mov   edx, [edi+WDATA.box.height]
  3121.     add   ecx, eax
  3122.     add   edx, ebx
  3123.     call  calculatescreen
  3124.  
  3125.     mov   [KEY_COUNT],byte 0           ; empty keyboard buffer
  3126.     mov   [BTN_COUNT],byte 0           ; empty button buffer
  3127.  
  3128.   newd:
  3129.     mov   [edi+WDATA.fl_redraw],byte 0   ; no redraw
  3130.     mov   edx,edi
  3131.  
  3132.     ret
  3133.  
  3134. syscall_windowsettings:
  3135.  
  3136.   .set_window_caption:
  3137.         dec     eax     ; subfunction #1 - set window caption
  3138.         jnz     .get_window_caption
  3139.  
  3140.         ; NOTE: only window owner thread can set its caption,
  3141.         ;       so there's no parameter for PID/TID
  3142.  
  3143.         mov     edi,[CURRENT_TASK]
  3144.         shl     edi,5
  3145.  
  3146.         ; have to check if caption is within application memory limit
  3147.         ; check is trivial, and if application resizes its memory,
  3148.         ;   caption still can become over bounds
  3149. ; diamond, 31.10.2006: check removed because with new memory manager
  3150. ; there can be valid data after APPDATA.mem_size bound
  3151. ;        mov     ecx,[edi*8+SLOT_BASE+APPDATA.mem_size]
  3152. ;        add     ecx,255 ; max caption length
  3153. ;        cmp     ebx,ecx
  3154. ;        ja      .exit_fail
  3155.  
  3156.         mov     [edi*8+SLOT_BASE+APPDATA.wnd_caption],ebx
  3157.         or      [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3158.  
  3159.         call    draw_window_caption
  3160.  
  3161.         xor     eax,eax ; eax = 0 (success)
  3162.         ret
  3163.  
  3164.   .get_window_caption:
  3165.         dec     eax     ; subfunction #2 - get window caption
  3166.         jnz     .exit_fail
  3167.  
  3168.         ; not implemented yet
  3169.  
  3170.   .exit_fail:
  3171.         xor     eax,eax
  3172.         inc     eax     ; eax = 1 (fail)
  3173.         ret
  3174.  
  3175.  
  3176. sys_window_move:
  3177.  
  3178.         mov     edi,[CURRENT_TASK]
  3179.         shl     edi,5
  3180.         add     edi,window_data
  3181.  
  3182.         test    [edi+WDATA.fl_wstate],WSTATE_MAXIMIZED
  3183.         jnz     .window_move_return
  3184.  
  3185.         push    dword [edi + WDATA.box.left]  ; save old coordinates
  3186.         push    dword [edi + WDATA.box.top]
  3187.         push    dword [edi + WDATA.box.width]
  3188.         push    dword [edi + WDATA.box.height]
  3189.  
  3190.         cmp   eax,-1                  ; set new position and size
  3191.         je    .no_x_reposition
  3192.         mov     [edi + WDATA.box.left], eax
  3193.       .no_x_reposition:
  3194.         cmp   ebx,-1
  3195.         je    .no_y_reposition
  3196.         mov     [edi + WDATA.box.top], ebx
  3197.       .no_y_reposition:
  3198.  
  3199.         test    [edi+WDATA.fl_wstate],WSTATE_ROLLEDUP
  3200.         jnz     .no_y_resizing
  3201.  
  3202.         cmp   ecx,-1
  3203.         je    .no_x_resizing
  3204.         mov     [edi + WDATA.box.width], ecx
  3205.       .no_x_resizing:
  3206.         cmp   edx,-1
  3207.         je    .no_y_resizing
  3208.         mov     [edi + WDATA.box.height], edx
  3209.       .no_y_resizing:
  3210.  
  3211.         call  check_window_position
  3212.         call  set_window_clientbox
  3213.  
  3214.         pushad                       ; save for window fullscreen/resize
  3215.         mov   esi,edi
  3216.         sub   edi,window_data
  3217.         shr   edi,5
  3218.         shl   edi,8
  3219.         add   edi, SLOT_BASE + APPDATA.saved_box
  3220.         mov   ecx,4
  3221.         cld
  3222.         rep   movsd
  3223.         popad
  3224.  
  3225.         pushad                       ; calculcate screen at new position
  3226.         mov   eax, [edi + WDATA.box.left]
  3227.         mov   ebx, [edi + WDATA.box.top]
  3228.         mov   ecx, [edi + WDATA.box.width]
  3229.         mov   edx, [edi + WDATA.box.height]
  3230.         add   ecx,eax
  3231.         add   edx,ebx
  3232.  
  3233.         call  calculatescreen
  3234.         popad
  3235.  
  3236.         pop   edx                   ; calculcate screen at old position
  3237.         pop   ecx
  3238.         pop   ebx
  3239.         pop   eax
  3240.         add   ecx,eax
  3241.         add   edx,ebx
  3242.         mov   [dlx],eax             ; save for drawlimits
  3243.         mov   [dly],ebx
  3244.         mov   [dlxe],ecx
  3245.         mov   [dlye],edx
  3246.         call  calculatescreen
  3247.  
  3248.         mov   [edi + WDATA.fl_redraw], 1 ; flag the process as redraw
  3249.  
  3250.         mov   eax,edi               ; redraw screen at old position
  3251.         xor   esi,esi
  3252.         call  redrawscreen
  3253.  
  3254.         mov   [DONT_DRAW_MOUSE],byte 0 ; mouse pointer
  3255.         mov   [MOUSE_BACKGROUND],byte 0 ; no mouse under
  3256.         mov   [MOUSE_DOWN],byte 0 ; react to mouse up/down
  3257.  
  3258.         call  [draw_pointer]
  3259.  
  3260.         mov   [window_move_pr],0
  3261.  
  3262.       .window_move_return:
  3263.  
  3264.         ret
  3265.  
  3266. uglobal
  3267.   window_move_pr   dd  0x0
  3268.   window_move_eax  dd  0x0
  3269.   window_move_ebx  dd  0x0
  3270.   window_move_ecx  dd  0x0
  3271.   window_move_edx  dd  0x0
  3272. endg
  3273.  
  3274. ;ok - 100% work
  3275. ;nt - not tested
  3276. ;---------------------------------------------------------------------------------------------
  3277. ;eax
  3278. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3279. ;1 - change task. Ret nothing. Block. ok.
  3280. ;2 - performance control
  3281. ; ebx
  3282. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3283. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3284. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3285. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3286. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3287. ;eax
  3288. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3289. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3290. ;---------------------------------------------------------------------------------------------
  3291. sys_sheduler: ;noname & halyavin
  3292.     cmp eax,0
  3293.     je shed_counter
  3294.     cmp eax,2
  3295.     je perf_control
  3296.     cmp eax,3
  3297.     je rdmsr_instr
  3298.     cmp eax,4
  3299.     je wrmsr_instr
  3300.     cmp eax,1
  3301.     jne not_supported
  3302.     call change_task ;delay,0
  3303. ret
  3304. shed_counter:
  3305.     mov eax,[context_counter]
  3306.     mov [esp+36],eax
  3307. not_supported:
  3308. ret
  3309. perf_control:
  3310.     inc eax ;now eax=3
  3311.     cmp ebx,eax
  3312.     je cache_disable
  3313.     dec eax
  3314.     cmp ebx,eax
  3315.     je cache_enable
  3316.     dec eax
  3317.     cmp ebx,eax
  3318.     je is_cache_enabled
  3319.     dec eax
  3320.     cmp ebx,eax
  3321.     je modify_pce
  3322. ret
  3323.  
  3324. rdmsr_instr:
  3325. ;now counter in ecx
  3326. ;(edx:eax) esi:edi => edx:esi
  3327. mov eax,esi
  3328. rdmsr
  3329. mov [esp+36],eax
  3330. mov [esp+24],edx ;ret in ebx?
  3331. ret
  3332.  
  3333. wrmsr_instr:
  3334. ;now counter in ecx
  3335. ;(edx:eax) esi:edi => edx:esi
  3336.         ; Fast Call MSR can't be destroy
  3337.         ; Íî MSR_AMD_EFER ìîæíî èçìåíÿòü, ò.ê. â ýòîì ðåãèñòðå ëèø
  3338.         ; âêëþ÷àþòñÿ/âûêëþ÷àþòñÿ ðàñøèðåííûå âîçìîæíîñòè
  3339.         cmp     ecx, MSR_SYSENTER_CS
  3340.         je      @f
  3341.         cmp     ecx, MSR_SYSENTER_ESP
  3342.         je      @f
  3343.         cmp     ecx, MSR_SYSENTER_EIP
  3344.         je      @f
  3345.         cmp     ecx, MSR_AMD_STAR
  3346.         je      @f
  3347.  
  3348.         mov     eax, esi
  3349.         wrmsr
  3350.         ; mov   [esp + 36], eax
  3351.         ; mov   [esp + 24], edx ;ret in ebx?
  3352. @@:
  3353. ret
  3354.  
  3355. cache_disable:
  3356.        mov eax,cr0
  3357.        or  eax,01100000000000000000000000000000b
  3358.        mov cr0,eax
  3359.        wbinvd ;set MESI
  3360. ret
  3361.  
  3362. cache_enable:
  3363.        mov eax,cr0
  3364.        and eax,10011111111111111111111111111111b
  3365.        mov cr0,eax
  3366. ret
  3367.  
  3368. is_cache_enabled:
  3369.        mov eax,cr0
  3370.        mov ebx,eax
  3371.        and eax,01100000000000000000000000000000b
  3372.        jz cache_disabled
  3373.        mov [esp+36],ebx
  3374. cache_disabled:
  3375.        mov dword [esp+36],eax ;0
  3376. ret
  3377.  
  3378. modify_pce:
  3379.        mov eax,cr4
  3380. ;       mov ebx,0
  3381. ;       or  bx,100000000b ;pce
  3382. ;       xor eax,ebx ;invert pce
  3383.        bts eax,8 ;pce=cr4[8]
  3384.        mov cr4,eax
  3385.        mov [esp+36],eax
  3386. ret
  3387. ;---------------------------------------------------------------------------------------------
  3388.  
  3389.  
  3390. ; check if pixel is allowed to be drawn
  3391.  
  3392. checkpixel:
  3393.         push eax edx
  3394.  
  3395.         mov  edx,[Screen_Max_X]     ; screen x size
  3396.         inc  edx
  3397.         imul edx, ebx
  3398.         mov  dl, [eax+edx+display_data] ; lea eax, [...]
  3399.  
  3400.         xor  ecx, ecx
  3401.         mov  eax, [CURRENT_TASK]
  3402.         cmp  al, dl
  3403.         setne cl
  3404.  
  3405.         pop  edx eax
  3406.         ret
  3407.  
  3408. iglobal
  3409.   cpustring db 'CPU',0
  3410. endg
  3411.  
  3412. uglobal
  3413. background_defined    db    0    ; diamond, 11.04.2006
  3414. endg
  3415.  
  3416. align 4
  3417. ; check misc
  3418.  
  3419. checkmisc:
  3420.  
  3421.     cmp   [ctrl_alt_del], 1
  3422.     jne   nocpustart
  3423.  
  3424.         mov     ebp, cpustring
  3425.         call    fs_execute_from_sysdir
  3426.  
  3427.     mov   [ctrl_alt_del], 0
  3428.  
  3429. nocpustart:
  3430.     cmp   [mouse_active], 1
  3431.     jne   mouse_not_active
  3432.     mov   [mouse_active], 0
  3433.     xor   edi, edi
  3434.     mov   ecx,  [TASK_COUNT]
  3435. set_mouse_event:
  3436.     add   edi, 256
  3437.     or    [edi+SLOT_BASE+APPDATA.event_mask], dword 100000b
  3438.     loop  set_mouse_event
  3439.  
  3440. mouse_not_active:
  3441.     cmp   [REDRAW_BACKGROUND],byte 0               ; background update ?
  3442.     jz    nobackgr
  3443.     cmp    [background_defined], 0
  3444.     jz    nobackgr
  3445.     cmp   [REDRAW_BACKGROUND], byte 2
  3446.     jnz   no_set_bgr_event
  3447.     xor   edi, edi
  3448.     mov   ecx,  [TASK_COUNT]
  3449. set_bgr_event:
  3450.     add   edi, 256
  3451.     or    [edi+SLOT_BASE+APPDATA.event_mask], 16
  3452.     loop  set_bgr_event
  3453. no_set_bgr_event:
  3454. ;    mov   [draw_data+32 + RECT.left],dword 0
  3455. ;    mov   [draw_data+32 + RECT.top],dword 0
  3456. ;    mov   eax,[Screen_Max_X]
  3457. ;    mov   ebx,[Screen_Max_Y]
  3458. ;    mov   [draw_data+32 + RECT.right],eax
  3459. ;    mov   [draw_data+32 + RECT.bottom],ebx
  3460.     call  drawbackground
  3461.     mov   [REDRAW_BACKGROUND],byte 0
  3462.     mov   [MOUSE_BACKGROUND],byte 0
  3463.  
  3464. nobackgr:
  3465.  
  3466.     ; system shutdown request
  3467.  
  3468.     cmp  [SYS_SHUTDOWN],byte 0
  3469.     je   noshutdown
  3470.  
  3471.     mov  edx,[shutdown_processes]
  3472.  
  3473.     cmp  [SYS_SHUTDOWN],dl
  3474.     jne  no_mark_system_shutdown
  3475.  
  3476.     lea   ecx,[edx-1]
  3477.     mov   edx,OS_BASE+0x3040
  3478.     jecxz @f
  3479. markz:
  3480.     mov   [edx+TASKDATA.state],byte 3
  3481.     add   edx,0x20
  3482.     loop  markz
  3483. @@:
  3484.  
  3485.   no_mark_system_shutdown:
  3486.  
  3487.     call [disable_mouse]
  3488.  
  3489.     dec  byte [SYS_SHUTDOWN]
  3490.     je   system_shutdown
  3491.  
  3492. noshutdown:
  3493.  
  3494.  
  3495.     mov   eax,[TASK_COUNT]                  ; termination
  3496.     mov   ebx,TASK_DATA+TASKDATA.state
  3497.     mov   esi,1
  3498.  
  3499. newct:
  3500.     mov   cl,[ebx]
  3501.     cmp   cl,byte 3
  3502.     jz    terminate
  3503.     cmp   cl,byte 4
  3504.     jz    terminate
  3505.  
  3506.     add   ebx,0x20
  3507.     inc   esi
  3508.     dec   eax
  3509.     jnz   newct
  3510.     ret
  3511.  
  3512. ; redraw screen
  3513.  
  3514. redrawscreen:
  3515.  
  3516. ; eax , if process window_data base is eax, do not set flag/limits
  3517.  
  3518.          pushad
  3519.          push  eax
  3520.  
  3521. ;;;         mov   ebx,2
  3522. ;;;         call  delay_hs
  3523.  
  3524.          ;mov   ecx,0               ; redraw flags for apps
  3525.          xor   ecx,ecx
  3526.        newdw2:
  3527.  
  3528.          inc   ecx
  3529.          push  ecx
  3530.  
  3531.          mov   eax,ecx
  3532.          shl   eax,5
  3533.          add   eax,window_data
  3534.  
  3535.          cmp   eax,[esp+4]
  3536.          je    not_this_task
  3537.                                    ; check if window in redraw area
  3538.          mov   edi,eax
  3539.  
  3540.          cmp   ecx,1               ; limit for background
  3541.          jz    bgli
  3542.  
  3543.          mov   eax, [edi + WDATA.box.left]
  3544.          mov   ebx, [edi + WDATA.box.top]
  3545.          mov   ecx, [edi + WDATA.box.width]
  3546.          mov   edx, [edi + WDATA.box.height]
  3547.          add   ecx,eax
  3548.          add   edx,ebx
  3549.  
  3550.          mov   ecx,[dlye]   ; ecx = area y end     ebx = window y start
  3551.          cmp   ecx,ebx
  3552.          jb    ricino
  3553.  
  3554.          mov   ecx,[dlxe]   ; ecx = area x end     eax = window x start
  3555.          cmp   ecx,eax
  3556.          jb    ricino
  3557.  
  3558.          mov   eax, [edi + WDATA.box.left]
  3559.          mov   ebx, [edi + WDATA.box.top]
  3560.          mov   ecx, [edi + WDATA.box.width]
  3561.          mov   edx, [edi + WDATA.box.height]
  3562.          add   ecx, eax
  3563.          add   edx, ebx
  3564.  
  3565.          mov   eax,[dly]    ; eax = area y start     edx = window y end
  3566.          cmp   edx,eax
  3567.          jb    ricino
  3568.  
  3569.          mov   eax,[dlx]    ; eax = area x start     ecx = window x end
  3570.          cmp   ecx,eax
  3571.          jb    ricino
  3572.  
  3573.         bgli:
  3574.  
  3575.          cmp   ecx,1
  3576.          jnz   .az
  3577.          mov   al,[REDRAW_BACKGROUND]
  3578.          cmp   al,2
  3579.          jz    newdw8
  3580.          test  al,al
  3581.          jz    .az
  3582.          lea   eax,[edi+draw_data-window_data]
  3583.          mov   ebx,[dlx]
  3584.          cmp   ebx,[eax+RECT.left]
  3585.          jae   @f
  3586.          mov   [eax+RECT.left],ebx
  3587.         @@:
  3588.          mov   ebx,[dly]
  3589.          cmp   ebx,[eax+RECT.top]
  3590.          jae   @f
  3591.          mov   [eax+RECT.top],ebx
  3592.         @@:
  3593.          mov   ebx,[dlxe]
  3594.          cmp   ebx,[eax+RECT.right]
  3595.          jbe   @f
  3596.          mov   [eax+RECT.right],ebx
  3597.         @@:
  3598.          mov   ebx,[dlye]
  3599.          cmp   ebx,[eax+RECT.bottom]
  3600.          jbe   @f
  3601.          mov   [eax+RECT.bottom],ebx
  3602.         @@:
  3603.          jmp   newdw8
  3604.         .az:
  3605.  
  3606.          mov   eax,edi
  3607.          add   eax,draw_data-window_data
  3608.  
  3609.          mov   ebx,[dlx]          ; set limits
  3610.          mov   [eax + RECT.left], ebx
  3611.          mov   ebx,[dly]
  3612.          mov   [eax + RECT.top], ebx
  3613.          mov   ebx,[dlxe]
  3614.          mov   [eax + RECT.right], ebx
  3615.          mov   ebx,[dlye]
  3616.          mov   [eax + RECT.bottom], ebx
  3617.  
  3618.          sub   eax,draw_data-window_data
  3619.  
  3620.          cmp   dword [esp],1
  3621.          jne   nobgrd
  3622.          mov   byte [REDRAW_BACKGROUND], 1
  3623.  
  3624.        newdw8:
  3625.        nobgrd:
  3626.  
  3627.          mov   [eax + WDATA.fl_redraw],byte 1    ; mark as redraw
  3628.  
  3629.        ricino:
  3630.  
  3631.        not_this_task:
  3632.  
  3633.          pop   ecx
  3634.  
  3635.          cmp   ecx,[TASK_COUNT]
  3636.          jle   newdw2
  3637.  
  3638.          pop  eax
  3639.          popad
  3640.  
  3641.          ret
  3642.  
  3643. calculatebackground:   ; background
  3644.  
  3645.         ; all black
  3646.  
  3647.         mov   edi, [img_background]  ;IMG_BACKGROUND                 ; set background to black
  3648.         xor   eax, eax
  3649.         mov   ecx, 1023    ;0x0fff00 / 4
  3650.         cld
  3651.         rep   stosd
  3652.  
  3653.         mov   edi,display_data              ; set os to use all pixels
  3654.         mov   eax,0x01010101
  3655.         mov   ecx,1280*1024 / 4
  3656.         rep   stosd
  3657.  
  3658.         mov   byte [REDRAW_BACKGROUND], 0              ; do not draw background!
  3659.  
  3660.         ret
  3661.  
  3662. uglobal
  3663.   imax    dd 0x0
  3664. endg
  3665.  
  3666.  
  3667.  
  3668. delay_ms:     ; delay in 1/1000 sec
  3669.  
  3670.  
  3671.         push  eax
  3672.         push  ecx
  3673.  
  3674.         mov   ecx,esi
  3675.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3676.         imul  ecx, 33941
  3677.         shr   ecx, 9
  3678.         ; </CPU clock fix>
  3679.  
  3680.         in    al,0x61
  3681.         and   al,0x10
  3682.         mov   ah,al
  3683.         cld
  3684.  
  3685.  cnt1:  in    al,0x61
  3686.         and   al,0x10
  3687.         cmp   al,ah
  3688.         jz    cnt1
  3689.  
  3690.         mov   ah,al
  3691.         loop  cnt1
  3692.  
  3693.         pop   ecx
  3694.         pop   eax
  3695.  
  3696.         ret
  3697.  
  3698.  
  3699. set_app_param:
  3700.         mov     edi, [TASK_BASE]
  3701.         mov     [edi + TASKDATA.event_mask], ebx
  3702.         ret
  3703.  
  3704.  
  3705.  
  3706. delay_hs:     ; delay in 1/100 secs
  3707. ; ebx = delay time
  3708.         push  ecx
  3709.         push  edx
  3710.  
  3711.         mov   edx,[timer_ticks]
  3712.  
  3713.       newtic:
  3714.         mov   ecx,[timer_ticks]
  3715.         sub   ecx,edx
  3716.         cmp   ecx,ebx
  3717.         jae   zerodelay
  3718.  
  3719.         call  change_task
  3720.  
  3721.         jmp   newtic
  3722.  
  3723.       zerodelay:
  3724.         pop   edx
  3725.         pop   ecx
  3726.  
  3727.         ret
  3728.  
  3729.  
  3730. memmove:       ; memory move in bytes
  3731.  
  3732. ; eax = from
  3733. ; ebx = to
  3734. ; ecx = no of bytes
  3735.     test ecx, ecx
  3736.     jle  .ret
  3737.  
  3738.  
  3739.     push esi edi ecx
  3740.  
  3741.     mov  edi, ebx
  3742.     mov  esi, eax
  3743.  
  3744.     test ecx, not 11b
  3745.     jz   @f
  3746.  
  3747.     push ecx
  3748.     shr  ecx, 2
  3749.     rep  movsd
  3750.     pop  ecx
  3751.     and  ecx, 11b
  3752.     jz   .finish
  3753.   @@:
  3754.     rep  movsb
  3755.  
  3756.   .finish:
  3757.     pop  ecx edi esi
  3758.   .ret:
  3759.     ret
  3760.  
  3761.  
  3762. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3763. ;align 4
  3764. ;
  3765. ;read_floppy_file:
  3766. ;
  3767. ;; as input
  3768. ;;
  3769. ;; eax pointer to file
  3770. ;; ebx file lenght
  3771. ;; ecx start 512 byte block number
  3772. ;; edx number of blocks to read
  3773. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3774. ;;
  3775. ;;
  3776. ;; on return
  3777. ;;
  3778. ;; eax = 0 command succesful
  3779. ;;       1 no fd base and/or partition defined
  3780. ;;       2 yet unsupported FS
  3781. ;;       3 unknown FS
  3782. ;;       4 partition not defined at hd
  3783. ;;       5 file not found
  3784. ;; ebx = size of file
  3785. ;
  3786. ;     mov   edi,[TASK_BASE]
  3787. ;     add   edi,0x10
  3788. ;     add   esi,[edi]
  3789. ;     add   eax,[edi]
  3790. ;
  3791. ;     pushad
  3792. ;     mov  edi,esi
  3793. ;     add  edi,1024
  3794. ;     mov  esi,0x100000+19*512
  3795. ;     sub  ecx,1
  3796. ;     shl  ecx,9
  3797. ;     add  esi,ecx
  3798. ;     shl  edx,9
  3799. ;     mov  ecx,edx
  3800. ;     cld
  3801. ;     rep  movsb
  3802. ;     popad
  3803. ;
  3804. ;     mov   [esp+36],eax
  3805. ;     mov   [esp+24],ebx
  3806. ;     ret
  3807.  
  3808.  
  3809.  
  3810. align 4
  3811.  
  3812. sys_programirq:
  3813.  
  3814.     mov   eax, [TASK_BASE]
  3815.     add   ebx, [eax + TASKDATA.mem_start]
  3816.  
  3817.     cmp   ecx, 16
  3818.     jae   .not_owner
  3819.     mov   edi, [eax + TASKDATA.pid]
  3820.     cmp   edi, [irq_owner + 4 * ecx]
  3821.     je    .spril1
  3822. .not_owner:
  3823.     xor   ecx, ecx
  3824.     inc   ecx
  3825.     jmp   .end
  3826.   .spril1:
  3827.  
  3828.     shl   ecx, 6
  3829.     mov   esi, ebx
  3830.     lea   edi, [irq00read + ecx]
  3831.     push  16
  3832.     pop   ecx
  3833.  
  3834.     cld
  3835.     rep   movsd
  3836.   .end:
  3837.     mov   [esp+32], ecx
  3838.     ret
  3839.  
  3840.  
  3841. align 4
  3842.  
  3843. get_irq_data:
  3844.      movzx esi, bh                       ; save number of subfunction, if bh = 1, return data size, otherwise, read data
  3845.      xor   bh, bh
  3846.      cmp   ebx, 16
  3847.      jae   .not_owner
  3848.      mov   edx, [4 * ebx + irq_owner]    ; check for irq owner
  3849.  
  3850.      mov   eax,[TASK_BASE]
  3851.  
  3852.      cmp   edx,[eax+TASKDATA.pid]
  3853.      je    gidril1
  3854. .not_owner:
  3855.      xor   edx, edx
  3856.      dec   edx
  3857.      jmp   gid1
  3858.  
  3859.   gidril1:
  3860.  
  3861.      shl   ebx, 12
  3862.      lea   eax, [ebx + IRQ_SAVE]         ; calculate address of the beginning of buffer + 0x0 - data size
  3863.      mov   edx, [eax]                    ;                                              + 0x4 - data offset
  3864.      dec   esi
  3865.      jz    gid1
  3866.      test  edx, edx                      ; check if buffer is empty
  3867.      jz    gid1
  3868.  
  3869.      mov   ebx, [eax + 0x4]
  3870.      mov   edi, ecx
  3871.  
  3872.      mov   ecx, 4000                     ; buffer size, used frequently
  3873.  
  3874.      cmp   ebx, ecx                      ; check for the end of buffer, if end of buffer, begin cycle again
  3875.      jb    @f
  3876.  
  3877.      xor   ebx, ebx
  3878.  
  3879.    @@:
  3880.  
  3881.      lea   esi, [ebx + edx]              ; calculate data size and offset
  3882.      cld
  3883.      cmp   esi, ecx                      ; if greater than the buffer size, begin cycle again
  3884.      jbe   @f
  3885.  
  3886.      sub   ecx, ebx
  3887.      sub   edx, ecx
  3888.  
  3889.      lea   esi, [eax + ebx + 0x10]
  3890.      rep   movsb
  3891.  
  3892.      xor   ebx, ebx
  3893.    @@:
  3894.      lea   esi, [eax + ebx + 0x10]
  3895.      mov   ecx, edx
  3896.      add   ebx, edx
  3897.  
  3898.      rep   movsb
  3899.      mov   edx, [eax]
  3900.      mov   [eax], ecx                    ; set data size to zero
  3901.      mov   [eax + 0x4], ebx              ; set data offset
  3902.  
  3903.    gid1:
  3904.      mov   [esp+32], edx                 ; eax
  3905.      ret
  3906.  
  3907.  
  3908. set_io_access_rights:
  3909.  
  3910.      pushad
  3911.  
  3912.      mov edi, tss._io_map_0
  3913.  
  3914. ;     mov   ecx,eax
  3915. ;     and   ecx,7    ; offset in byte
  3916.  
  3917. ;     shr   eax,3    ; number of byte
  3918. ;     add   edi,eax
  3919.  
  3920. ;     mov   ebx,1
  3921. ;     shl   ebx,cl
  3922.  
  3923.      cmp   ebp,0                ; enable access - ebp = 0
  3924.      jne   siar1
  3925.  
  3926. ;     not   ebx
  3927. ;     and   [edi],byte bl
  3928.      btr [edi], eax
  3929.  
  3930.      popad
  3931.  
  3932.      ret
  3933.  
  3934. siar1:
  3935.  
  3936.      bts [edi], eax
  3937.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  3938.  
  3939.      popad
  3940.  
  3941.      ret
  3942.  
  3943. r_f_port_area:
  3944.  
  3945.      test  eax, eax
  3946.      jnz   free_port_area
  3947. ;     je    r_port_area
  3948. ;     jmp   free_port_area
  3949.  
  3950. ;   r_port_area:
  3951.  
  3952.      pushad
  3953.  
  3954.      cmp   ebx,ecx            ; beginning > end ?
  3955.      ja    rpal1
  3956.      cmp   ecx,65536
  3957.      jae   rpal1
  3958.      mov   esi,[RESERVED_PORTS]
  3959.      test  esi,esi            ; no reserved areas ?
  3960.      je    rpal2
  3961.      cmp   esi,255            ; max reserved
  3962.      jae   rpal1
  3963.  rpal3:
  3964.      mov   edi,esi
  3965.      shl   edi,4
  3966.      add   edi,RESERVED_PORTS
  3967.      cmp   ebx,[edi+8]
  3968.      ja    rpal4
  3969.      cmp   ecx,[edi+4]
  3970.      jae   rpal1
  3971. ;     jb    rpal4
  3972. ;     jmp   rpal1
  3973.  rpal4:
  3974.  
  3975.      dec   esi
  3976.      jnz   rpal3
  3977.      jmp   rpal2
  3978.    rpal1:
  3979.      popad
  3980.      mov   eax,1
  3981.      ret
  3982.  
  3983.    rpal2:
  3984.      popad
  3985.  
  3986.  
  3987.      ; enable port access at port IO map
  3988.      cli
  3989.      pushad                        ; start enable io map
  3990.  
  3991.      cmp   ecx,65536 ;16384
  3992.      jae   no_unmask_io ; jge
  3993.  
  3994.      mov   eax,ebx
  3995.  
  3996.    new_port_access:
  3997.  
  3998.      pushad
  3999.  
  4000.      xor   ebp,ebp                ; enable - eax = port
  4001.      call  set_io_access_rights
  4002.  
  4003.      popad
  4004.  
  4005.      inc   eax
  4006.      cmp   eax,ecx
  4007.      jbe   new_port_access
  4008.  
  4009.    no_unmask_io:
  4010.  
  4011.      popad                         ; end enable io map
  4012.      sti
  4013.  
  4014.      mov   edi,[RESERVED_PORTS]
  4015.      add   edi,1
  4016.      mov   [RESERVED_PORTS],edi
  4017.      shl   edi,4
  4018.      add   edi,RESERVED_PORTS
  4019.      mov   esi,[TASK_BASE]
  4020.      mov   esi,[esi+TASKDATA.pid]
  4021.      mov   [edi],esi
  4022.      mov   [edi+4],ebx
  4023.      mov   [edi+8],ecx
  4024.  
  4025.      xor   eax, eax
  4026.      ret
  4027.  
  4028. free_port_area:
  4029.  
  4030.      pushad
  4031.  
  4032.      mov   esi,[RESERVED_PORTS]     ; no reserved areas ?
  4033.      test  esi,esi
  4034.      je    frpal2
  4035.      mov   edx,[TASK_BASE]
  4036.      mov   edx,[edx+TASKDATA.pid]
  4037.    frpal3:
  4038.      mov   edi,esi
  4039.      shl   edi,4
  4040.      add   edi,RESERVED_PORTS
  4041.      cmp   edx,[edi]
  4042.      jne   frpal4
  4043.      cmp   ebx,[edi+4]
  4044.      jne   frpal4
  4045.      cmp   ecx,[edi+8]
  4046.      jne   frpal4
  4047.      jmp   frpal1
  4048.    frpal4:
  4049.      dec   esi
  4050.      jnz   frpal3
  4051.    frpal2:
  4052.      popad
  4053.      mov   eax,1
  4054.      ret
  4055.    frpal1:
  4056.      mov   ecx,256
  4057.      sub   ecx,esi
  4058.      shl   ecx,4
  4059.      mov   esi,edi
  4060.      add   esi,16
  4061.      cld
  4062.      rep   movsb
  4063.  
  4064.      dec   dword [RESERVED_PORTS]
  4065.  
  4066.      popad
  4067.  
  4068.  
  4069.      ; disable port access at port IO map
  4070.  
  4071.      pushad                        ; start disable io map
  4072.  
  4073.      cmp   ecx,65536 ;16384
  4074.      jge   no_mask_io
  4075.  
  4076.      mov   eax,ebx
  4077.  
  4078.    new_port_access_disable:
  4079.  
  4080.      pushad
  4081.  
  4082.      mov   ebp,1                  ; disable - eax = port
  4083.      call  set_io_access_rights
  4084.  
  4085.      popad
  4086.  
  4087.      inc   eax
  4088.      cmp   eax,ecx
  4089.      jbe   new_port_access_disable
  4090.  
  4091.    no_mask_io:
  4092.  
  4093.      popad                         ; end disable io map
  4094.  
  4095.      xor   eax, eax
  4096.      ret
  4097.  
  4098.  
  4099. reserve_free_irq:
  4100.  
  4101.      xor   esi, esi
  4102.      inc   esi
  4103.      cmp   ecx, 16
  4104.      jae   ril1
  4105.  
  4106.      push  ecx
  4107.      lea   ecx, [irq_owner + 4 * ecx]
  4108.      mov   edx, [ecx]
  4109.      mov   eax, [TASK_BASE]
  4110.      mov   edi, [eax + TASKDATA.pid]
  4111.      pop   eax
  4112.      dec   ebx
  4113.      jnz   reserve_irq
  4114.  
  4115.      cmp   edx, edi
  4116.      jne   ril1
  4117.      dec   esi
  4118.      mov   [ecx], esi
  4119.  
  4120.      jmp   ril1
  4121.  
  4122.   reserve_irq:
  4123.  
  4124.      cmp   dword [ecx], 0
  4125.      jne   ril1
  4126.  
  4127.      mov   ebx, [f_irqs + 4 * eax]
  4128.  
  4129.      stdcall attach_int_handler, eax, ebx, dword 0
  4130.  
  4131.      mov   [ecx], edi
  4132.  
  4133.      dec   esi
  4134.    ril1:
  4135.      mov   [esp+32], esi ; return in eax
  4136.      ret
  4137.  
  4138. iglobal
  4139. f_irqs:
  4140.      dd 0x0
  4141.      dd 0x0
  4142.      dd p_irq2
  4143.      dd p_irq3
  4144.      dd p_irq4
  4145.      dd p_irq5
  4146.      dd p_irq6
  4147.      dd p_irq7
  4148.      dd p_irq8
  4149.      dd p_irq9
  4150.      dd p_irq10
  4151.      dd p_irq11
  4152.      dd 0x0
  4153.      dd 0x0
  4154.      dd p_irq14
  4155.      dd p_irq15
  4156.  
  4157. endg
  4158.  
  4159. drawbackground:
  4160.        inc   [mouse_pause]
  4161.        cmp   [SCR_MODE],word 0x12
  4162.        je   dbrv20
  4163.      dbrv12:
  4164.        cmp  [SCR_MODE],word 0100000000000000b
  4165.        jge  dbrv20
  4166.        cmp  [SCR_MODE],word 0x13
  4167.        je   dbrv20
  4168.        call  vesa12_drawbackground
  4169.        dec   [mouse_pause]
  4170.        call   [draw_pointer]
  4171.        ret
  4172.      dbrv20:
  4173.        cmp   [BgrDrawMode],dword 1
  4174.        jne   bgrstr
  4175.        call  vesa20_drawbackground_tiled
  4176.        dec   [mouse_pause]
  4177.        call   [draw_pointer]
  4178.        ret
  4179.      bgrstr:
  4180.        call  vesa20_drawbackground_stretch
  4181.        dec   [mouse_pause]
  4182.        call   [draw_pointer]
  4183.        ret
  4184.  
  4185. align 4
  4186.  
  4187. syscall_putimage:                       ; PutImage
  4188. sys_putimage:
  4189.      test  ecx,0x80008000
  4190.      jnz   .exit
  4191.      test  ecx,0x0000FFFF
  4192.      jz    .exit
  4193.      test  ecx,0xFFFF0000
  4194.      jnz   @f
  4195.   .exit:
  4196.      ret
  4197.  @@:
  4198.         mov     edi,[current_slot]
  4199.         add     dx,word[edi+APPDATA.wnd_clientbox.top]
  4200.         rol     edx,16
  4201.         add     dx,word[edi+APPDATA.wnd_clientbox.left]
  4202.         rol     edx,16
  4203.   .forced:
  4204.         push    ebp esi 0
  4205.         mov     ebp, putimage_get24bpp
  4206.         mov     esi, putimage_init24bpp
  4207. sys_putimage_bpp:
  4208. ;        call    [disable_mouse] ; this will be done in xxx_putimage
  4209. ;        mov     eax, vga_putimage
  4210.         cmp     [SCR_MODE], word 0x12
  4211.         jz      @f   ;.doit
  4212.         mov     eax, vesa12_putimage
  4213.         cmp     [SCR_MODE], word 0100000000000000b
  4214.         jae     @f
  4215.         cmp     [SCR_MODE], word 0x13
  4216.         jnz     .doit
  4217. @@:
  4218.         mov     eax, vesa20_putimage
  4219. .doit:
  4220.         inc     [mouse_pause]
  4221.         call    eax
  4222.         dec     [mouse_pause]
  4223.         pop     ebp esi ebp
  4224.         jmp     [draw_pointer]
  4225.  
  4226. syscall_putimage_palette:
  4227.         mov     edi, esi
  4228.         mov     esi, edx
  4229.         mov     edx, ecx
  4230.         mov     ecx, ebx
  4231.         mov     ebx, eax
  4232. sys_putimage_palette:
  4233. ; ebx = pointer to image
  4234. ; ecx = [xsize]*65536 + [ysize]
  4235. ; edx = [xstart]*65536 + [ystart]
  4236. ; esi = number of bits per pixel, must be 8, 24 or 32
  4237. ; edi = pointer to palette
  4238. ; ebp = row delta
  4239.         mov     eax, [CURRENT_TASK]
  4240.         shl     eax, 8
  4241.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4242.         rol     edx, 16
  4243.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4244.         rol     edx, 16
  4245. .forced:
  4246.         cmp     esi, 1
  4247.         jnz     @f
  4248.         push    edi
  4249.         mov     eax, [edi+4]
  4250.         sub     eax, [edi]
  4251.         push    eax
  4252.         push    dword [edi]
  4253.         push    0ffffff80h
  4254.         mov     edi, esp
  4255.         call    put_mono_image
  4256.         add     esp, 12
  4257.         pop     edi
  4258.         ret
  4259. @@:
  4260.         cmp     esi, 2
  4261.         jnz     @f
  4262.         push    edi
  4263.         push    0ffffff80h
  4264.         mov     edi, esp
  4265.         call    put_2bit_image
  4266.         pop     eax
  4267.         pop     edi
  4268.         ret
  4269. @@:
  4270.         cmp     esi, 4
  4271.         jnz     @f
  4272.         push    edi
  4273.         push    0ffffff80h
  4274.         mov     edi, esp
  4275.         call    put_4bit_image
  4276.         pop     eax
  4277.         pop     edi
  4278.         ret
  4279. @@:
  4280.         push    ebp esi ebp
  4281.         cmp     esi, 8
  4282.         jnz     @f
  4283.         mov     ebp, putimage_get8bpp
  4284.         mov     esi, putimage_init8bpp
  4285.         jmp     sys_putimage_bpp
  4286. @@:
  4287.         cmp     esi, 15
  4288.         jnz     @f
  4289.         mov     ebp, putimage_get15bpp
  4290.         mov     esi, putimage_init15bpp
  4291.         jmp     sys_putimage_bpp
  4292. @@:
  4293.         cmp     esi, 16
  4294.         jnz     @f
  4295.         mov     ebp, putimage_get16bpp
  4296.         mov     esi, putimage_init16bpp
  4297.         jmp     sys_putimage_bpp
  4298. @@:
  4299.         cmp     esi, 24
  4300.         jnz     @f
  4301.         mov     ebp, putimage_get24bpp
  4302.         mov     esi, putimage_init24bpp
  4303.         jmp     sys_putimage_bpp
  4304. @@:
  4305.         cmp     esi, 32
  4306.         jnz     @f
  4307.         mov     ebp, putimage_get32bpp
  4308.         mov     esi, putimage_init32bpp
  4309.         jmp     sys_putimage_bpp
  4310. @@:
  4311.         pop     ebp esi ebp
  4312.         ret
  4313.  
  4314. put_mono_image:
  4315.         push    ebp esi ebp
  4316.         mov     ebp, putimage_get1bpp
  4317.         mov     esi, putimage_init1bpp
  4318.         jmp     sys_putimage_bpp
  4319. put_2bit_image:
  4320.         push    ebp esi ebp
  4321.         mov     ebp, putimage_get2bpp
  4322.         mov     esi, putimage_init2bpp
  4323.         jmp     sys_putimage_bpp
  4324. put_4bit_image:
  4325.         push    ebp esi ebp
  4326.         mov     ebp, putimage_get4bpp
  4327.         mov     esi, putimage_init4bpp
  4328.         jmp     sys_putimage_bpp
  4329.  
  4330. putimage_init24bpp:
  4331.         lea     eax, [eax*3]
  4332. putimage_init8bpp:
  4333.         ret
  4334.  
  4335. align 16
  4336. putimage_get24bpp:
  4337.         movzx   eax, byte [esi+2]
  4338.         shl     eax, 16
  4339.         mov     ax, [esi]
  4340.         add     esi, 3
  4341.         ret     4
  4342. align 16
  4343. putimage_get8bpp:
  4344.         movzx   eax, byte [esi]
  4345.         push    edx
  4346.         mov     edx, [esp+8]
  4347.         mov     eax, [edx+eax*4]
  4348.         pop     edx
  4349.         inc     esi
  4350.         ret     4
  4351.  
  4352. putimage_init1bpp:
  4353.         add     eax, ecx
  4354.         push    ecx
  4355.         add     eax, 7
  4356.         add     ecx, 7
  4357.         shr     eax, 3
  4358.         shr     ecx, 3
  4359.         sub     eax, ecx
  4360.         pop     ecx
  4361.         ret
  4362. align 16
  4363. putimage_get1bpp:
  4364.         push    edx
  4365.         mov     edx, [esp+8]
  4366.         mov     al, [edx]
  4367.         add     al, al
  4368.         jnz     @f
  4369.         lodsb
  4370.         adc     al, al
  4371. @@:
  4372.         mov     [edx], al
  4373.         sbb     eax, eax
  4374.         and     eax, [edx+8]
  4375.         add     eax, [edx+4]
  4376.         pop     edx
  4377.         ret     4
  4378.  
  4379. putimage_init2bpp:
  4380.         add     eax, ecx
  4381.         push    ecx
  4382.         add     ecx, 3
  4383.         add     eax, 3
  4384.         shr     ecx, 2
  4385.         shr     eax, 2
  4386.         sub     eax, ecx
  4387.         pop     ecx
  4388.         ret
  4389. align 16
  4390. putimage_get2bpp:
  4391.         push    edx
  4392.         mov     edx, [esp+8]
  4393.         mov     al, [edx]
  4394.         mov     ah, al
  4395.         shr     al, 6
  4396.         shl     ah, 2
  4397.         jnz     .nonewbyte
  4398.         lodsb
  4399.         mov     ah, al
  4400.         shr     al, 6
  4401.         shl     ah, 2
  4402.         add     ah, 1
  4403. .nonewbyte:
  4404.         mov     [edx], ah
  4405.         mov     edx, [edx+4]
  4406.         movzx   eax, al
  4407.         mov     eax, [edx+eax*4]
  4408.         pop     edx
  4409.         ret     4
  4410.  
  4411. putimage_init4bpp:
  4412.         add     eax, ecx
  4413.         push    ecx
  4414.         add     ecx, 1
  4415.         add     eax, 1
  4416.         shr     ecx, 1
  4417.         shr     eax, 1
  4418.         sub     eax, ecx
  4419.         pop     ecx
  4420.         ret
  4421. align 16
  4422. putimage_get4bpp:
  4423.         push    edx
  4424.         mov     edx, [esp+8]
  4425.         add     byte [edx], 80h
  4426.         jc      @f
  4427.         movzx   eax, byte [edx+1]
  4428.         mov     edx, [edx+4]
  4429.         and     eax, 0x0F
  4430.         mov     eax, [edx+eax*4]
  4431.         pop     edx
  4432.         ret     4
  4433. @@:
  4434.         movzx   eax, byte [esi]
  4435.         add     esi, 1
  4436.         mov     [edx+1], al
  4437.         shr     eax, 4
  4438.         mov     edx, [edx+4]
  4439.         mov     eax, [edx+eax*4]
  4440.         pop     edx
  4441.         ret     4
  4442.  
  4443. putimage_init32bpp:
  4444.         shl     eax, 2
  4445.         ret
  4446. align 16
  4447. putimage_get32bpp:
  4448.         lodsd
  4449.         ret     4
  4450.  
  4451. putimage_init15bpp:
  4452. putimage_init16bpp:
  4453.         add     eax, eax
  4454.         ret
  4455. align 16
  4456. putimage_get15bpp:
  4457. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4458.         push    ecx edx
  4459.         movzx   eax, word [esi]
  4460.         add     esi, 2
  4461.         mov     ecx, eax
  4462.         mov     edx, eax
  4463.         and     eax, 0x1F
  4464.         and     ecx, 0x1F shl 5
  4465.         and     edx, 0x1F shl 10
  4466.         shl     eax, 3
  4467.         shl     ecx, 6
  4468.         shl     edx, 9
  4469.         or      eax, ecx
  4470.         or      eax, edx
  4471.         pop     edx ecx
  4472.         ret     4
  4473.  
  4474. align 16
  4475. putimage_get16bpp:
  4476. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4477.         push    ecx edx
  4478.         movzx   eax, word [esi]
  4479.         add     esi, 2
  4480.         mov     ecx, eax
  4481.         mov     edx, eax
  4482.         and     eax, 0x1F
  4483.         and     ecx, 0x3F shl 5
  4484.         and     edx, 0x1F shl 11
  4485.         shl     eax, 3
  4486.         shl     ecx, 5
  4487.         shl     edx, 8
  4488.         or      eax, ecx
  4489.         or      eax, edx
  4490.         pop     edx ecx
  4491.         ret     4
  4492.  
  4493. ; eax x beginning
  4494. ; ebx y beginning
  4495. ; ecx x end
  4496.         ; edx y end
  4497. ; edi color
  4498.  
  4499. __sys_drawbar:
  4500.         mov     esi,[current_slot]
  4501.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  4502.         add     ecx,[esi+APPDATA.wnd_clientbox.left]
  4503.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  4504.         add     edx,[esi+APPDATA.wnd_clientbox.top]
  4505.   .forced:
  4506.     inc   [mouse_pause]
  4507. ;        call    [disable_mouse]
  4508.     cmp   [SCR_MODE],word 0x12
  4509.     je   dbv20
  4510.    sdbv20:
  4511.     cmp  [SCR_MODE],word 0100000000000000b
  4512.     jge  dbv20
  4513.     cmp  [SCR_MODE],word 0x13
  4514.     je   dbv20
  4515.     call vesa12_drawbar
  4516.     dec   [mouse_pause]
  4517.     call   [draw_pointer]
  4518.     ret
  4519.   dbv20:
  4520.     call vesa20_drawbar
  4521.     dec   [mouse_pause]
  4522.     call   [draw_pointer]
  4523.     ret
  4524.  
  4525.  
  4526.  
  4527. kb_read:
  4528.  
  4529.         push    ecx edx
  4530.  
  4531.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4532.       kr_loop:
  4533.         in      al,0x64
  4534.         test    al,1
  4535.         jnz     kr_ready
  4536.         loop    kr_loop
  4537.         mov     ah,1
  4538.         jmp     kr_exit
  4539.       kr_ready:
  4540.         push    ecx
  4541.         mov     ecx,32
  4542.       kr_delay:
  4543.         loop    kr_delay
  4544.         pop     ecx
  4545.         in      al,0x60
  4546.         xor     ah,ah
  4547.       kr_exit:
  4548.  
  4549.         pop     edx ecx
  4550.  
  4551.         ret
  4552.  
  4553.  
  4554. kb_write:
  4555.  
  4556.         push    ecx edx
  4557.  
  4558.         mov     dl,al
  4559. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4560. ;      kw_loop1:
  4561. ;        in      al,0x64
  4562. ;        test    al,0x20
  4563. ;        jz      kw_ok1
  4564. ;        loop    kw_loop1
  4565. ;        mov     ah,1
  4566. ;        jmp     kw_exit
  4567. ;      kw_ok1:
  4568.         in      al,0x60
  4569.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4570.       kw_loop:
  4571.         in      al,0x64
  4572.         test    al,2
  4573.         jz      kw_ok
  4574.         loop    kw_loop
  4575.         mov     ah,1
  4576.         jmp     kw_exit
  4577.       kw_ok:
  4578.         mov     al,dl
  4579.         out     0x60,al
  4580.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4581.       kw_loop3:
  4582.         in      al,0x64
  4583.         test    al,2
  4584.         jz      kw_ok3
  4585.         loop    kw_loop3
  4586.         mov     ah,1
  4587.         jmp     kw_exit
  4588.       kw_ok3:
  4589.         mov     ah,8
  4590.       kw_loop4:
  4591.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4592.       kw_loop5:
  4593.         in      al,0x64
  4594.         test    al,1
  4595.         jnz     kw_ok4
  4596.         loop    kw_loop5
  4597.         dec     ah
  4598.         jnz     kw_loop4
  4599.       kw_ok4:
  4600.         xor     ah,ah
  4601.       kw_exit:
  4602.  
  4603.         pop     edx ecx
  4604.  
  4605.         ret
  4606.  
  4607.  
  4608. kb_cmd:
  4609.  
  4610.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4611.       c_wait:
  4612.         in      al,0x64
  4613.         test    al,2
  4614.         jz      c_send
  4615.         loop    c_wait
  4616.         jmp     c_error
  4617.       c_send:
  4618.         mov     al,bl
  4619.         out     0x64,al
  4620.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4621.       c_accept:
  4622.         in      al,0x64
  4623.         test    al,2
  4624.         jz      c_ok
  4625.         loop    c_accept
  4626.       c_error:
  4627.         mov     ah,1
  4628.         jmp     c_exit
  4629.       c_ok:
  4630.         xor     ah,ah
  4631.       c_exit:
  4632.         ret
  4633.  
  4634.  
  4635. setmouse:  ; set mousepicture -pointer
  4636.            ; ps2 mouse enable
  4637.  
  4638.      mov     [MOUSE_PICTURE],dword mousepointer
  4639.  
  4640.      cli
  4641.  
  4642.      ret
  4643.  
  4644. if used _rdtsc
  4645. _rdtsc:
  4646.      bt [cpu_caps], CAPS_TSC
  4647.      jnc ret_rdtsc
  4648.      rdtsc
  4649.      ret
  4650.    ret_rdtsc:
  4651.      mov   edx,0xffffffff
  4652.      mov   eax,0xffffffff
  4653.      ret
  4654. end if
  4655.  
  4656. rerouteirqs:
  4657.  
  4658.         cli
  4659.  
  4660.         mov     al,0x11         ;  icw4, edge triggered
  4661.         out     0x20,al
  4662.         call    pic_delay
  4663.         out     0xA0,al
  4664.         call    pic_delay
  4665.  
  4666.         mov     al,0x20         ;  generate 0x20 +
  4667.         out     0x21,al
  4668.         call    pic_delay
  4669.         mov     al,0x28         ;  generate 0x28 +
  4670.         out     0xA1,al
  4671.         call    pic_delay
  4672.  
  4673.         mov     al,0x04         ;  slave at irq2
  4674.         out     0x21,al
  4675.         call    pic_delay
  4676.         mov     al,0x02         ;  at irq9
  4677.         out     0xA1,al
  4678.         call    pic_delay
  4679.  
  4680.         mov     al,0x01         ;  8086 mode
  4681.         out     0x21,al
  4682.         call    pic_delay
  4683.         out     0xA1,al
  4684.         call    pic_delay
  4685.  
  4686.         mov     al,255          ; mask all irq's
  4687.         out     0xA1,al
  4688.         call    pic_delay
  4689.         out     0x21,al
  4690.         call    pic_delay
  4691.  
  4692.         mov     ecx,0x1000
  4693.         cld
  4694. picl1:  call    pic_delay
  4695.         loop    picl1
  4696.  
  4697.         mov     al,255          ; mask all irq's
  4698.         out     0xA1,al
  4699.         call    pic_delay
  4700.         out     0x21,al
  4701.         call    pic_delay
  4702.  
  4703.         cli
  4704.  
  4705.         ret
  4706.  
  4707.  
  4708. pic_delay:
  4709.  
  4710.         jmp     pdl1
  4711. pdl1:   ret
  4712.  
  4713.  
  4714. sys_msg_board_str:
  4715.  
  4716.      pushad
  4717.    @@:
  4718.      cmp    [esi],byte 0
  4719.      je     @f
  4720.      mov    eax,1
  4721.      movzx  ebx,byte [esi]
  4722.      call   sys_msg_board
  4723.      inc    esi
  4724.      jmp    @b
  4725.    @@:
  4726.      popad
  4727.      ret
  4728.  
  4729. sys_msg_board_byte:
  4730. ; in: al = byte to display
  4731. ; out: nothing
  4732. ; destroys: nothing
  4733.         pushad
  4734.         mov     ecx, 2
  4735.         shl     eax, 24
  4736.         jmp     @f
  4737.  
  4738. sys_msg_board_word:
  4739. ; in: ax = word to display
  4740. ; out: nothing
  4741. ; destroys: nothing
  4742.         pushad
  4743.         mov     ecx, 4
  4744.         shl     eax, 16
  4745.         jmp     @f
  4746.  
  4747. sys_msg_board_dword:
  4748. ; in: eax = dword to display
  4749. ; out: nothing
  4750. ; destroys: nothing
  4751.         pushad
  4752.         mov     ecx, 8
  4753. @@:
  4754.         push    ecx
  4755.         rol     eax, 4
  4756.         push    eax
  4757.         and     al, 0xF
  4758.         cmp     al, 10
  4759.         sbb     al, 69h
  4760.         das
  4761.         mov     bl, al
  4762.         xor     eax, eax
  4763.         inc     eax
  4764.         call    sys_msg_board
  4765.         pop     eax
  4766.         pop     ecx
  4767.         loop    @b
  4768.         popad
  4769.         ret
  4770.  
  4771. uglobal
  4772.   msg_board_data: times 4096 db 0
  4773.   msg_board_count dd 0x0
  4774. endg
  4775.  
  4776. sys_msg_board:
  4777.  
  4778. ; eax=1 : write :  bl byte to write
  4779. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4780.  
  4781.         mov     ecx, [msg_board_count]
  4782.         cmp     eax, 1
  4783.         jne     .smbl1
  4784.  
  4785. if defined debug_com_base
  4786.  
  4787.         push    dx ax
  4788.  
  4789.        @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
  4790.         mov     dx, debug_com_base+5
  4791.         in      al, dx
  4792.         test    al, 1 shl 5
  4793.         jz      @r
  4794.  
  4795.         mov     dx, debug_com_base      ; Output the byte
  4796.         mov     al, bl
  4797.         out     dx, al
  4798.  
  4799.         pop     ax dx
  4800.  
  4801. end if
  4802.  
  4803.         mov     [msg_board_data+ecx],bl
  4804.         inc     ecx
  4805.         and     ecx, 4095
  4806.         mov     [msg_board_count], ecx
  4807.         mov     [check_idle_semaphore], 5
  4808.         ret
  4809. .smbl1:
  4810.         cmp     eax, 2
  4811.         jne     .smbl2
  4812.         test    ecx, ecx
  4813.         jz      .smbl21
  4814.         mov     eax, msg_board_data+1
  4815.         mov     ebx, msg_board_data
  4816.         movzx   edx, byte [ebx]
  4817.         call    memmove
  4818.         dec     [msg_board_count]
  4819.         mov     [esp + 36], edx ;eax
  4820.         mov     [esp + 24], dword 1
  4821.         ret
  4822. .smbl21:
  4823.         mov     [esp+36], ecx
  4824.         mov     [esp+24], ecx
  4825. .smbl2:
  4826.         ret
  4827.  
  4828.  
  4829.  
  4830. sys_process_def:
  4831.         mov     edi, [CURRENT_TASK]
  4832.  
  4833.         dec     eax             ; 1 = set keyboard mode
  4834.      jne   no_set_keyboard_setup
  4835.  
  4836.      shl   edi,8
  4837.      mov   [edi+SLOT_BASE + APPDATA.keyboard_mode],bl
  4838.  
  4839.      ret
  4840.  
  4841.    no_set_keyboard_setup:
  4842.  
  4843.         dec     eax             ; 2 = get keyboard mode
  4844.      jne   no_get_keyboard_setup
  4845.  
  4846.      shl   edi,8
  4847.      movzx eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4848.  
  4849.      mov   [esp+36],eax
  4850.  
  4851.      ret
  4852.  
  4853.    no_get_keyboard_setup:
  4854.  
  4855.         dec     eax             ; 3 = get keyboard ctrl, alt, shift
  4856.      jne   no_get_keyboard_cas
  4857.  
  4858. ;     xor   eax,eax
  4859. ;     movzx eax,byte [shift]
  4860. ;     movzx ebx,byte [ctrl]
  4861. ;     shl   ebx,2
  4862. ;     add   eax,ebx
  4863. ;     movzx ebx,byte [alt]
  4864. ;     shl   ebx,3
  4865. ;     add   eax,ebx
  4866.  
  4867.  ;// mike.dld [
  4868.      mov   eax, [kb_state]
  4869.  ;// mike.dld ]
  4870.  
  4871.      mov   [esp+36],eax
  4872.  
  4873.      ret
  4874.  
  4875.    no_get_keyboard_cas:
  4876.  
  4877.         dec     eax
  4878.         jnz     no_add_keyboard_hotkey
  4879.  
  4880.         mov     eax, hotkey_list
  4881. @@:
  4882.         cmp     dword [eax+8], 0
  4883.         jz      .found_free
  4884.         add     eax, 16
  4885.         cmp     eax, hotkey_list+16*256
  4886.         jb      @b
  4887.         mov     dword [esp+36], 1
  4888.         ret
  4889. .found_free:
  4890.         mov     [eax+8], edi
  4891.         mov     [eax+4], ecx
  4892.         movzx   ebx, bl
  4893.         lea     ebx, [hotkey_scancodes+ebx*4]
  4894.         mov     ecx, [ebx]
  4895.         mov     [eax], ecx
  4896.         mov     [ebx], eax
  4897.         mov     [eax+12], ebx
  4898.         jecxz   @f
  4899.         mov     [ecx+12], eax
  4900. @@:
  4901.         and     dword [esp+36], 0
  4902.         ret
  4903.  
  4904. no_add_keyboard_hotkey:
  4905.  
  4906.         dec     eax
  4907.         jnz     no_del_keyboard_hotkey
  4908.  
  4909.         movzx   ebx, bl
  4910.         lea     ebx, [hotkey_scancodes+ebx*4]
  4911.         mov     eax, [ebx]
  4912. .scan:
  4913.         test    eax, eax
  4914.         jz      .notfound
  4915.         cmp     [eax+8], edi
  4916.         jnz     .next
  4917.         cmp     [eax+4], ecx
  4918.         jz      .found
  4919. .next:
  4920.         mov     eax, [eax]
  4921.         jmp     .scan
  4922. .notfound:
  4923.         mov     dword [esp+36], 1
  4924.         ret
  4925. .found:
  4926.         mov     ecx, [eax]
  4927.         jecxz   @f
  4928.         mov     edx, [eax+12]
  4929.         mov     [ecx+12], edx
  4930. @@:
  4931.         mov     ecx, [eax+12]
  4932.         mov     edx, [eax]
  4933.         mov     [ecx], edx
  4934.         xor     edx, edx
  4935.         mov     [eax+4], edx
  4936.         mov     [eax+8], edx
  4937.         mov     [eax+12], edx
  4938.         mov     [eax], edx
  4939.         mov     [esp+36], edx
  4940.         ret
  4941.  
  4942. no_del_keyboard_hotkey:
  4943.      ret
  4944.  
  4945.  
  4946. align 4
  4947.  
  4948. sys_gs:                         ; direct screen access
  4949.  
  4950.      cmp  eax,1                 ; resolution
  4951.      jne  no_gs1
  4952.      mov  eax,[Screen_Max_X]
  4953.      shl  eax,16
  4954.      mov  ax,[Screen_Max_Y]
  4955.      add  eax,0x00010001
  4956.      mov  [esp+36],eax
  4957.      ret
  4958.    no_gs1:
  4959.  
  4960.      cmp   eax,2                ; bits per pixel
  4961.      jne   no_gs2
  4962.      movzx eax,byte [ScreenBPP]
  4963.      mov   [esp+36],eax
  4964.      ret
  4965.    no_gs2:
  4966.  
  4967.      cmp   eax,3                ; bytes per scanline
  4968.      jne   no_gs3
  4969.      mov   eax,[BytesPerScanLine]
  4970.      mov   [esp+36],eax
  4971.      ret
  4972.    no_gs3:
  4973.  
  4974.      mov  [esp+36],dword -1
  4975.      ret
  4976.  
  4977.  
  4978. align 4 ; PCI functions
  4979.  
  4980. sys_pci:
  4981.  
  4982.      call  pci_api
  4983.      mov   [esp+36],eax
  4984.      ret
  4985.  
  4986.  
  4987. align 4  ;  system functions
  4988.  
  4989. syscall_setpixel:                       ; SetPixel
  4990.  
  4991.         mov     eax, ebx
  4992.         mov     ebx, ecx
  4993.         mov     ecx, edx
  4994.         mov     edx, [TASK_BASE]
  4995.         add     eax, [edx-twdw+WDATA.box.left]
  4996.         add     ebx, [edx-twdw+WDATA.box.top]
  4997.         mov     edi, [current_slot]
  4998.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  4999.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  5000.         xor     edi, edi ; no force
  5001. ;       mov     edi, 1
  5002.         call    [disable_mouse]
  5003.         jmp     [putpixel]
  5004.  
  5005. align 4
  5006.  
  5007. syscall_writetext:                      ; WriteText
  5008.  
  5009.         mov   eax,[TASK_BASE]
  5010.         mov   ebp,[eax-twdw+WDATA.box.left]
  5011.         push  esi
  5012.         mov   esi,[current_slot]
  5013.         add   ebp,[esi+APPDATA.wnd_clientbox.left]
  5014.         shl   ebp,16
  5015.         add   ebp,[eax-twdw+WDATA.box.top]
  5016.         add   bp,word[esi+APPDATA.wnd_clientbox.top]
  5017.         pop   esi
  5018.         add   ebx,ebp
  5019.         mov   eax,edi
  5020.         xor   edi,edi
  5021.         jmp   dtext
  5022.  
  5023. align 4
  5024.  
  5025. syscall_openramdiskfile:                ; OpenRamdiskFile
  5026.  
  5027.         mov     eax, ebx
  5028.         mov     ebx, ecx
  5029.         mov     ecx, edx
  5030.         mov     edx, esi
  5031.         mov     esi, 12
  5032.         call    fileread
  5033.         mov     [esp+32], eax
  5034.         ret
  5035.  
  5036. align 4
  5037.  
  5038. syscall_drawrect:                       ; DrawRect
  5039.  
  5040.         mov     edi, edx ; color + gradient
  5041.         and     edi, 0x80FFFFFF
  5042.         test    bx, bx  ; x.size
  5043.         je      .drectr
  5044.         test    cx, cx ; y.size
  5045.         je      .drectr
  5046.  
  5047.         mov     eax, ebx ; bad idea
  5048.         mov     ebx, ecx
  5049.  
  5050.         movzx   ecx, ax ; ecx - x.size
  5051.         shr     eax, 16 ; eax - x.coord
  5052.         movzx   edx, bx ; edx - y.size
  5053.         shr     ebx, 16 ; ebx - y.coord
  5054.         mov     esi, [current_slot]
  5055.  
  5056.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  5057.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  5058.         add     ecx, eax
  5059.         add     edx, ebx
  5060.         jmp     [drawbar]
  5061. .drectr:
  5062.         ret
  5063.  
  5064. align 4
  5065. syscall_getscreensize:                  ; GetScreenSize
  5066.         mov     ax, [Screen_Max_X]
  5067.         shl     eax, 16
  5068.         mov     ax, [Screen_Max_Y]
  5069.         mov     [esp + 32], eax
  5070.         ret
  5071.  
  5072. align 4
  5073.  
  5074. syscall_cdaudio:                        ; CD
  5075.  
  5076.         cmp     eax, 4
  5077.         jb      .audio
  5078.         jz      .eject
  5079.         cmp     eax, 5
  5080.         jnz     .ret
  5081. .load:
  5082.         call    .reserve
  5083.         call    LoadMedium
  5084.         call    .free
  5085.         ret
  5086. .eject:
  5087.         call    .reserve
  5088.         call    clear_CD_cache
  5089.         call    allow_medium_removal
  5090.         call    EjectMedium
  5091.         call    .free
  5092.         ret
  5093. .audio:
  5094.      call  sys_cd_audio
  5095.      mov   [esp+36],eax
  5096. .ret:
  5097.      ret
  5098.  
  5099. .reserve:
  5100.         call    reserve_cd
  5101.         mov     eax, ebx
  5102.         shr     eax, 1
  5103.         and     eax, 1
  5104.         inc     eax
  5105.         mov     [ChannelNumber], ax
  5106.         mov     eax, ebx
  5107.         and     eax, 1
  5108.         mov     [DiskNumber], al
  5109.         call    reserve_cd_channel
  5110.         and     ebx, 3
  5111.         inc     ebx
  5112.         mov     [cdpos], ebx
  5113.         add     ebx, ebx
  5114.         mov     cl, 8
  5115.         sub     cl, bl
  5116.         mov     al, [DRIVE_DATA+1]
  5117.         shr     al, cl
  5118.         test    al, 2
  5119.         jz      .err
  5120.         ret
  5121. .free:
  5122.         call    free_cd_channel
  5123.         and     [cd_status], 0
  5124.         ret
  5125. .err:
  5126.         call    .free
  5127.         pop     eax
  5128.         ret
  5129.  
  5130. align 4
  5131.  
  5132. syscall_getpixel:                       ; GetPixel
  5133.      mov   ecx, [Screen_Max_X]
  5134.      inc   ecx
  5135.      xor   edx, edx
  5136.      mov   eax, ebx
  5137.      div   ecx
  5138.      mov   ebx, edx
  5139.      xchg  eax, ebx
  5140.      call  dword [GETPIXEL] ; eax - x, ebx - y
  5141.      mov   [esp + 32], ecx
  5142.      ret
  5143.  
  5144. align 4
  5145.  
  5146. syscall_getarea:
  5147. ;eax = 36
  5148. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5149. ;ecx = [size x]*65536 + [size y]
  5150. ;edx = [start x]*65536 + [start y]
  5151.      pushad
  5152.          inc   [mouse_pause]
  5153. ; Check of use of the hardware cursor.
  5154.       cmp  [disable_mouse],__sys_disable_mouse
  5155.           jne  @f
  5156. ; Since the test for the coordinates of the mouse should not be used,
  5157. ; then use the call [disable_mouse] is not possible!
  5158.       cmp  dword [MOUSE_VISIBLE],dword 0
  5159.       jne  @f
  5160.       pushf
  5161.       cli
  5162.       call draw_mouse_under
  5163.       popf
  5164.       mov  [MOUSE_VISIBLE],dword 1
  5165. @@:
  5166.      mov   edi,ebx
  5167.      mov   eax,edx
  5168.      shr   eax,16
  5169.      mov   ebx,edx
  5170.      and   ebx,0xffff
  5171.      dec   eax
  5172.            dec   ebx
  5173.      ; eax - x, ebx - y
  5174.      mov   edx,ecx
  5175.      
  5176.      shr   ecx,16
  5177.      and   edx,0xffff
  5178.      mov   esi,ecx
  5179.      ; ecx - size x, edx - size y
  5180.          
  5181.          mov   ebp,edx
  5182.          dec   ebp
  5183.      lea   ebp,[ebp*3]
  5184.          
  5185.          imul  ebp,esi
  5186.          
  5187.          mov   esi,ecx
  5188.          dec   esi
  5189.          lea   esi,[esi*3]
  5190.          
  5191.      add   ebp,esi
  5192.      add   ebp,edi
  5193.  
  5194.      add   ebx,edx
  5195.          
  5196. .start_y:
  5197.      push  ecx edx
  5198. .start_x:
  5199.      push  eax ebx ecx
  5200.      add   eax,ecx
  5201.  
  5202.      call  dword [GETPIXEL] ; eax - x, ebx - y
  5203.      
  5204.      mov   [ebp],cx
  5205.      shr   ecx,16
  5206.      mov   [ebp+2],cl
  5207.  
  5208.      pop   ecx ebx eax
  5209.      sub   ebp,3
  5210.      dec   ecx
  5211.      jnz   .start_x
  5212.          pop   edx ecx
  5213.          dec   ebx
  5214.      dec   edx
  5215.      jnz   .start_y
  5216.      dec        [mouse_pause]
  5217. ; Check of use of the hardware cursor.
  5218.       cmp  [disable_mouse],__sys_disable_mouse
  5219.           jne  @f
  5220.          call  [draw_pointer]
  5221. @@:
  5222.      popad
  5223.      ret
  5224.  
  5225. align 4
  5226.  
  5227. syscall_drawline:                       ; DrawLine
  5228.  
  5229.         mov     edi, [TASK_BASE]
  5230.         movzx   eax, word[edi-twdw+WDATA.box.left]
  5231.         mov     ebp, eax
  5232.         mov     esi, [current_slot]
  5233.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5234.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  5235.         add     ebp,ebx
  5236.         shl     eax, 16
  5237.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  5238.         add     eax, ebp
  5239.         mov     ebp, ebx
  5240.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  5241.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  5242.         add     ebp, ecx
  5243.         shl     ebx, 16
  5244.         xor     edi, edi
  5245.         add     ebx, ebp
  5246.         mov     ecx, edx
  5247.         jmp     [draw_line]
  5248.  
  5249. align 4
  5250.  
  5251. syscall_getirqowner:                    ; GetIrqOwner
  5252.  
  5253.      cmp   ebx,16
  5254.      jae   .err
  5255.  
  5256.      cmp   [irq_rights + 4 * ebx], dword 2
  5257.      je    .err
  5258.  
  5259.      mov   eax,[4 * ebx + irq_owner]
  5260.      mov   [esp+32],eax
  5261.  
  5262.      ret
  5263. .err:
  5264.      or    dword [esp+32], -1
  5265.      ret
  5266.  
  5267. align 4
  5268.  
  5269. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5270.  
  5271.      call  r_f_port_area
  5272.      mov   [esp+36],eax
  5273.      ret
  5274.  
  5275. align 4
  5276.  
  5277. syscall_threads:                        ; CreateThreads
  5278.  
  5279.      call  sys_threads
  5280.      mov   [esp+36],eax
  5281.      ret
  5282.  
  5283. align 4
  5284.  
  5285. stack_driver_stat:
  5286.  
  5287.      call  app_stack_handler            ; Stack status
  5288.  
  5289. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5290. ;     call  change_task                 ; between sent packet
  5291.  
  5292.      mov   [esp+36],eax
  5293.      ret
  5294.  
  5295. align 4
  5296.  
  5297. socket:                                 ; Socket interface
  5298.      call  app_socket_handler
  5299.  
  5300. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5301. ;     call  change_task                 ; between sent packet
  5302.  
  5303.      mov   [esp+36],eax
  5304.      mov   [esp+24],ebx
  5305.      ret
  5306.  
  5307. align 4
  5308.  
  5309. read_from_hd:                           ; Read from hd - fn not in use
  5310.  
  5311.      mov   edi,[TASK_BASE]
  5312.      add   edi,TASKDATA.mem_start
  5313.      add   eax,[edi]
  5314.      add   ecx,[edi]
  5315.      add   edx,[edi]
  5316.      call  file_read
  5317.  
  5318.      mov   [esp+36],eax
  5319.      mov   [esp+24],ebx
  5320.  
  5321.      ret
  5322.  
  5323. paleholder:
  5324.         ret
  5325.  
  5326. align 4
  5327. set_screen:
  5328.         cmp eax, [Screen_Max_X]
  5329.         jne .set
  5330.  
  5331.         cmp edx, [Screen_Max_Y]
  5332.         jne .set
  5333.         ret
  5334. .set:
  5335.         pushfd
  5336.         cli
  5337.  
  5338.         mov [Screen_Max_X], eax
  5339.         mov [Screen_Max_Y], edx
  5340.  
  5341.         mov [screen_workarea.right],eax
  5342.         mov [screen_workarea.bottom], edx
  5343.         inc eax
  5344.         shl eax, 2                      ;32 bpp
  5345.         mov [BytesPerScanLine], eax
  5346.         push ebx
  5347.         push esi
  5348.         push edi
  5349.         call    repos_windows
  5350.         mov     eax, 0
  5351.         mov     ebx, 0
  5352.         mov     ecx, [Screen_Max_X]
  5353.         mov     edx, [Screen_Max_Y]
  5354.         call    calculatescreen
  5355.         pop edi
  5356.         pop esi
  5357.         pop ebx
  5358.  
  5359.         popfd
  5360.         ret
  5361.  
  5362. ; --------------- APM ---------------------
  5363. apm_entry    dp    0
  5364. apm_vf        dd    0
  5365. align 4
  5366. sys_apm:
  5367.     cmp    word [apm_vf], 0    ; Check APM BIOS enable
  5368.     jne    @f
  5369.     ; or    [esp + 56], byte 1    ; error
  5370.     mov    [esp + 36], dword 8    ; 32-bit protected-mode interface not supported
  5371.     ret
  5372.  
  5373. @@:
  5374.     xchg    eax, ecx
  5375.     xchg    ebx, ecx
  5376.  
  5377.     cmp    al, 3
  5378.     ja    @f
  5379.     ;and    [esp + 56], byte 0xfe    ; emulate func 0..3 as func 0 ; bad idea modify EFLAGS...
  5380.     mov    eax, [apm_vf]
  5381.     mov    [esp + 36], eax
  5382.     shr    eax, 16
  5383.     mov    [esp + 32], eax
  5384.     ret
  5385.  
  5386. @@:
  5387.  
  5388.     mov esi, [master_tab+(OS_BASE shr 20)]
  5389.     xchg [master_tab], esi
  5390.     push esi
  5391.     mov edi, cr3
  5392.     mov cr3, edi                 ;flush TLB
  5393.  
  5394.     call    pword [apm_entry]    ; call APM BIOS
  5395.  
  5396.     xchg eax, [esp]
  5397.     mov [master_tab], eax
  5398.     mov eax, cr3
  5399.     mov cr3, eax
  5400.     pop eax
  5401.  
  5402.     mov    [esp + 8 ], edi
  5403.     mov    [esp + 12], esi
  5404.     mov    [esp + 24], ebx
  5405.     mov    [esp + 28], edx
  5406.     mov    [esp + 32], ecx
  5407.     mov    [esp + 36], eax
  5408.     ; setc    al
  5409.     ; and    [esp + 56], byte 0xfe
  5410.     ; or    [esp + 56], al
  5411.  
  5412.  
  5413.     ret
  5414. ; -----------------------------------------
  5415.  
  5416. align 4
  5417.  
  5418. undefined_syscall:                      ; Undefined system call
  5419.      mov   [esp + 32], dword -1
  5420.      ret
  5421.  
  5422. align 4
  5423. system_shutdown:          ; shut down the system
  5424.  
  5425.            cmp byte [BOOT_VAR+0x9030], 1
  5426.            jne @F
  5427.            ret
  5428. @@:
  5429.            call stop_all_services
  5430.            push 3                ; stop playing cd
  5431.            pop  eax
  5432.            call sys_cd_audio
  5433.  
  5434. yes_shutdown_param:
  5435.            cli
  5436.  
  5437.            mov  eax, kernel_file ; load kernel.mnt to 0x7000:0
  5438.            push 12
  5439.            pop  esi
  5440.            xor  ebx,ebx
  5441.            or   ecx,-1
  5442.            mov  edx, OS_BASE+0x70000
  5443.            call fileread
  5444.  
  5445.            mov  esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  5446.            mov  edi,OS_BASE+0x40000
  5447.            mov  ecx,1000
  5448.            rep  movsb
  5449.  
  5450.            mov  esi,OS_BASE+0x2F0000    ; restore 0x0 - 0xffff
  5451.            mov  edi, OS_BASE
  5452.            mov  ecx,0x10000/4
  5453.            cld
  5454.            rep movsd
  5455.  
  5456.            call restorefatchain
  5457.  
  5458.            mov al, 0xFF
  5459.            out 0x21, al
  5460.            out 0xA1, al
  5461.  
  5462. if 1
  5463.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5464.            mov  word [OS_BASE+0x467+2],0x1000
  5465.  
  5466.            mov  al,0x0F
  5467.            out  0x70,al
  5468.            mov  al,0x05
  5469.            out  0x71,al
  5470.  
  5471.            mov  al,0xFE
  5472.            out  0x64,al
  5473.  
  5474.            hlt
  5475.  
  5476. else
  5477.         cmp     byte [OS_BASE + 0x9030], 2
  5478.         jnz     no_acpi_power_off
  5479.  
  5480. ; scan for RSDP
  5481. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  5482.         movzx   eax, word [OS_BASE + 0x40E]
  5483.         shl     eax, 4
  5484.         jz      @f
  5485.         mov     ecx, 1024/16
  5486.         call    scan_rsdp
  5487.         jnc     .rsdp_found
  5488. @@:
  5489. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  5490.         mov     eax, 0xE0000
  5491.         mov     ecx, 0x2000
  5492.         call    scan_rsdp
  5493.         jc      no_acpi_power_off
  5494. .rsdp_found:
  5495.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  5496.         mov     ebp, [ipc_tmp]
  5497.         stdcall map_page, ebp, esi, PG_MAP
  5498.         lea     eax, [esi+1000h]
  5499.         lea     edx, [ebp+1000h]
  5500.         stdcall map_page, edx, eax, PG_MAP
  5501.         and     esi, 0xFFF
  5502.         add     esi, ebp
  5503.         cmp     dword [esi], 'RSDT'
  5504.         jnz     no_acpi_power_off
  5505.         mov     ecx, [esi+4]
  5506.         sub     ecx, 24h
  5507.         jbe     no_acpi_power_off
  5508.         shr     ecx, 2
  5509.         add     esi, 24h
  5510. .scan_fadt:
  5511.         lodsd
  5512.         mov     ebx, eax
  5513.         lea     eax, [ebp+2000h]
  5514.         stdcall map_page, eax, ebx, PG_MAP
  5515.         lea     eax, [ebp+3000h]
  5516.         add     ebx, 0x1000
  5517.         stdcall map_page, eax, ebx, PG_MAP
  5518.         and     ebx, 0xFFF
  5519.         lea     ebx, [ebx+ebp+2000h]
  5520.         cmp     dword [ebx], 'FACP'
  5521.         jz      .fadt_found
  5522.         loop    .scan_fadt
  5523.         jmp     no_acpi_power_off
  5524. .fadt_found:
  5525. ; ebx is linear address of FADT
  5526.         mov     edx, [ebx+48]
  5527.         test    edx, edx
  5528.         jz      .nosmi
  5529.         mov     al, [ebx+52]
  5530.         out     dx, al
  5531.         mov     edx, [ebx+64]
  5532. @@:
  5533.         in      ax, dx
  5534.         test    al, 1
  5535.         jz      @b
  5536. .nosmi:
  5537.         mov     edx, [ebx+64]
  5538.         in      ax, dx
  5539.         and     ax, 203h
  5540.         or      ax, 3C00h
  5541.         out     dx, ax
  5542.         mov     edx, [ebx+68]
  5543.         test    edx, edx
  5544.         jz      @f
  5545.         in      ax, dx
  5546.         and     ax, 203h
  5547.         or      ax, 3C00h
  5548.         out     dx, ax
  5549. @@:
  5550.         jmp     $
  5551.  
  5552.  
  5553. no_acpi_power_off:
  5554.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5555.            mov  word [OS_BASE+0x467+2],0x1000
  5556.  
  5557.            mov  al,0x0F
  5558.            out  0x70,al
  5559.            mov  al,0x05
  5560.            out  0x71,al
  5561.  
  5562.            mov  al,0xFE
  5563.            out  0x64,al
  5564.  
  5565.            hlt
  5566.  
  5567. scan_rsdp:
  5568.         add     eax, OS_BASE
  5569. .s:
  5570.         cmp     dword [eax], 'RSD '
  5571.         jnz     .n
  5572.         cmp     dword [eax+4], 'PTR '
  5573.         jnz     .n
  5574.         xor     edx, edx
  5575.         xor     esi, esi
  5576. @@:
  5577.         add     dl, [eax+esi]
  5578.         inc     esi
  5579.         cmp     esi, 20
  5580.         jnz     @b
  5581.         test    dl, dl
  5582.         jz      .ok
  5583. .n:
  5584.         add     eax, 10h
  5585.         loop    .s
  5586.         stc
  5587. .ok:
  5588.         ret
  5589. end if
  5590.  
  5591. include "data32.inc"
  5592.  
  5593. __REV__ = __REV
  5594.  
  5595. uglobals_size = $ - endofcode
  5596. diff16 "end of kernel code",0,$
  5597.