Subversion Repositories Kolibri OS

Rev

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