Subversion Repositories Kolibri OS

Rev

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