Subversion Repositories Kolibri OS

Rev

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