Subversion Repositories Kolibri OS

Rev

Rev 440 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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