Subversion Repositories Kolibri OS

Rev

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

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