Subversion Repositories Kolibri OS

Rev

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

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