Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;
  3. ;; Copyright (C) KolibriOS team 2004-2013. All rights reserved.
  4. ;; PROGRAMMING:
  5. ;; Ivan Poddubny
  6. ;; Marat Zakiyanov (Mario79)
  7. ;; VaStaNi
  8. ;; Trans
  9. ;; Mihail Semenyako (mike.dld)
  10. ;; Sergey Kuzmin (Wildwest)
  11. ;; Andrey Halyavin (halyavin)
  12. ;; Mihail Lisovin (Mihasik)
  13. ;; Andrey Ignatiev (andrew_programmer)
  14. ;; NoName
  15. ;; Evgeny Grechnikov (Diamond)
  16. ;; Iliya Mihailov (Ghost)
  17. ;; Sergey Semyonov (Serge)
  18. ;; Johnny_B
  19. ;; SPraid (simba)
  20. ;; Hidnplayr
  21. ;; Alexey Teplov (<Lrz>)
  22. ;; Artem Jerdev (art_zh)
  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: 3194 $
  62.  
  63.  
  64. USE_COM_IRQ     equ 1      ; make irq 3 and irq 4 available for PCI devices
  65.  
  66. include "proc32.inc"
  67. include "kglobals.inc"
  68.  
  69. include "const.inc"
  70. max_processes    equ   255
  71. tss_step         equ   (128+8192) ; tss & i/o - 65535 ports, * 256=557056*4
  72.  
  73.  
  74. os_stack       equ  (os_data_l-gdts)    ; GDTs
  75. os_code        equ  (os_code_l-gdts)
  76. graph_data     equ  (3+graph_data_l-gdts)
  77. tss0           equ  (tss0_l-gdts)
  78. app_code       equ  (3+app_code_l-gdts)
  79. app_data       equ  (3+app_data_l-gdts)
  80. app_tls        equ  (3+tls_data_l-gdts)
  81. pci_code_sel   equ  (pci_code_32-gdts)
  82. pci_data_sel   equ  (pci_data_32-gdts)
  83.  
  84.  
  85. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  86. ;;
  87. ;;   Included files:
  88. ;;
  89. ;;   Kernel16.inc
  90. ;;    - Bootcode.inc  Hardware setup
  91. ;;
  92. ;;   Kernel32.inc
  93. ;;    - Sys32.inc     Process management
  94. ;;    - Shutdown.inc  Shutdown and restart
  95. ;;    - Fat32.inc     Read / write hd
  96. ;;    - Vesa12.inc    Vesa 1.2 driver
  97. ;;    - Vesa20.inc    Vesa 2.0 driver
  98. ;;    - Vga.inc       VGA driver
  99. ;;    - Stack.inc     Network interface
  100. ;;    - Mouse.inc     Mouse pointer
  101. ;;    - Scincode.inc  Window skinning
  102. ;;    - Pci32.inc     PCI functions
  103. ;;
  104. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  105.  
  106.  
  107. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  108. ;;                                                                      ;;
  109. ;;                  16 BIT ENTRY FROM BOOTSECTOR                        ;;
  110. ;;                                                                      ;;
  111. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  112.  
  113. use16
  114.                   org   0x0
  115.                   jmp   start_of_code
  116.  
  117. version db    'Kolibri-A   version 0.1.0.0      ',13,10,13,10,0
  118.  
  119. diff16 "bootcode start: ",0,$
  120. include "boot/bootcode.inc"    ; 16 bit system boot code
  121.  
  122. diff16 "biosdisk start: ",0,$
  123. include "detect/biosdisk.inc"
  124.  
  125. diff16 "end of code16 : ",0,$
  126.  
  127. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  128. ;;                                                                      ;;
  129. ;;                  SWITCH TO 32 BIT PROTECTED MODE                     ;;
  130. ;;                                                                      ;;
  131. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  132.  
  133. switch_32:
  134. ; CR0 Flags - Protected mode and Paging
  135.  
  136.         mov ecx, CR0_PE
  137.  
  138. ; Enabling 32 bit protected mode
  139.  
  140.         sidt    [cs:old_ints_h]
  141.  
  142.         cli                             ; disable all irqs
  143.         cld
  144.         mov     al,255                  ; mask all irqs
  145.         out     0xa1,al
  146.         out     0x21,al
  147.    l.5: in      al, 0x64                ; Enable A20
  148.         test    al, 2
  149.         jnz     l.5
  150.         mov     al, 0xD1
  151.         out     0x64, al
  152.    l.6: in      al, 0x64
  153.         test    al, 2
  154.         jnz     l.6
  155.         mov     al, 0xDF
  156.         out     0x60, al
  157.    l.7: in      al, 0x64
  158.         test    al, 2
  159.         jnz     l.7
  160.         mov     al, 0xFF
  161.         out     0x64, al
  162.  
  163.         lgdt    [cs:tmp_gdt]            ; Load GDT
  164.         mov     eax, cr0                ; protected mode
  165.         or      eax, ecx
  166.         and     eax, 10011111b *65536*256 + 0xffffff ; caching enabled
  167.         mov     cr0, eax
  168.         jmp     pword os_code:B32       ; jmp to enable 32 bit mode
  169.  
  170. align 8
  171. tmp_gdt:
  172.  
  173.         dw     23
  174.         dd     tmp_gdt+0x10000
  175.         dw     0
  176.  
  177.         dw     0xffff
  178.         dw     0x0000
  179.         db     0x00
  180.         dw     11011111b *256 +10011010b
  181.         db     0x00
  182.  
  183.         dw     0xffff
  184.         dw     0x0000
  185.         db     0x00
  186.         dw     11011111b *256 +10010010b
  187.         db     0x00
  188.  
  189. diff10 "32bit enter sz :",switch_32,$
  190. diff16 "data16   start :",0,$
  191.  
  192. include "data16.inc"
  193.  
  194.  
  195. use32
  196. org $+0x10000
  197.  
  198. align 4
  199. B32:
  200. diff16 "32-bit code start ",0,$
  201.            mov   ax,os_stack       ; Selector for os
  202.            mov   ds,ax
  203.            mov   es,ax
  204.            mov   fs,ax
  205.            mov   gs,ax
  206.            mov   ss,ax
  207.            mov   esp,0x4ec00       ; Set stack
  208.  
  209. ;-------------------------------------------------------------------------------
  210.            call preinit_mem     ; (init.inc)
  211.  
  212.            call test_cpu        ; (init.inc - to be moved to bus/CPU.inc)
  213.            bts [cpu_caps-OS_BASE], CAPS_TSC     ;force use rdtsc
  214.  
  215.            call init_BIOS32     ; (init.inc - to be removed later)
  216.  
  217. ; PCIe extended config space access
  218.  
  219.         display '** Platform chosen (const.inc): '
  220. if PLATFORM>PLATFORM_RS780
  221.         call fusion_pcie_init   ; (bus/HT.inc)
  222.         display 'RS880/Fusion '
  223. else
  224.         call rs7xx_pcie_init    ; (bus/HT.inc)
  225.         display 'RS780 '
  226. end if
  227.         display 10,13
  228.  
  229.  
  230. ; MEMORY MODEL
  231.            call init_mem        ; (init.inc)
  232.  
  233. ; ENABLE PAGING
  234.  
  235.            mov eax, sys_pgdir-OS_BASE
  236.            mov cr3, eax
  237.  
  238.            mov eax,cr0
  239.            or eax,CR0_PG+CR0_WP
  240.            mov cr0,eax
  241.  
  242.            lgdt [gdts]
  243.            jmp pword os_code:high_code
  244.  
  245. diff16 "32-bit init size ",B32,$
  246.  
  247. align 4
  248. bios32_entry    dd ?
  249. tmp_page_tabs   dd ?
  250.  
  251.  
  252. __DEBUG__ fix 1
  253. __DEBUG_LEVEL__ fix 1
  254. include 'init.inc'
  255.  
  256. org OS_BASE+$
  257.  
  258. align 4
  259. high_code:
  260.            mov ax, os_stack
  261.            mov bx, app_data
  262.            mov cx, app_tls
  263.            mov ss, ax
  264.            add esp, OS_BASE
  265.  
  266.            mov ds, bx
  267.            mov es, bx
  268.            mov fs, cx
  269.            mov gs, bx
  270.  
  271.            bt [cpu_caps], CAPS_PGE
  272.            jnc @F
  273.  
  274.            or dword [sys_pgdir+(OS_BASE shr 20)], PG_GLOBAL
  275.  
  276.            mov ebx, cr4
  277.            or ebx, CR4_PGE
  278.            mov cr4, ebx
  279. @@:
  280.            xor eax, eax
  281.            mov dword [sys_pgdir], eax
  282.            mov dword [sys_pgdir+4], eax
  283.  
  284.            mov eax, cr3
  285.            mov cr3, eax           ; flush TLB
  286.  
  287. ; SAVE REAL MODE VARIABLES
  288.         xor     eax, eax
  289.         mov     [IDEContrRegsBaseAddr], ax
  290. ; -----------------------------------------
  291.         inc     eax
  292. ;        mov     al, [BOOT_VAR+0x901F]           ; DMA access
  293.         mov     [allow_dma_access], al
  294.         mov     eax, 32                         ; << bpp
  295.         mov     [ScreenBPP],al
  296.         mov     [_display.bpp], eax
  297.  
  298.         mov     [_display.vrefresh], 60
  299.         mov     [_display.disable_mouse],  __sys_disable_mouse
  300.  
  301.         mov     eax, 1024                       ; << X max
  302.         mov     [_display.width], eax
  303.         dec     eax
  304.         mov     [Screen_Max_X],eax
  305.         mov     [screen_workarea.right],eax
  306.         mov     eax,768                         ; << Y max
  307.         mov     [_display.height], eax
  308.         dec     eax
  309.         mov     [Screen_Max_Y],eax
  310.         mov     [screen_workarea.bottom],eax
  311.         mov     eax, 0x7055                     ; << screen mode
  312.         mov     [SCR_MODE],eax
  313.  
  314.         mov     eax, 4096                       ; << may be different!
  315.         mov     [BytesPerScanLine],ax
  316.         mov     [_display.pitch], eax
  317.  
  318.         mov     eax, [_display.width]
  319.         mul     [_display.height]
  320.         mov     [_WinMapSize], eax
  321.  
  322.         mov     esi, BOOT_VAR+0x9080
  323.         movzx   ecx, byte [esi-1]
  324.         mov     [NumBiosDisks], ecx
  325.         mov     edi, BiosDisksData
  326.         rep     movsd
  327.  
  328.  
  329. ; GRAPHICS ADDRESSES
  330.  
  331.         and     byte [BOOT_VAR+0x901e],0x0
  332.         mov     eax, [BOOT_VAR+0x9018]
  333.         mov     [LFBAddress],eax
  334.  
  335.         mov     [PUTPIXEL],dword __sys_putpixel
  336.         mov     [GETPIXEL],dword get_pixel
  337.  
  338. ; -------- Fast System Call init ----------
  339. .SEnP:
  340. ; AMD SYSCALL/SYSRET
  341.            mov ecx, MSR_AMD_EFER
  342.            rdmsr
  343.            or eax, 1   ; bit_0 - System Call Extension (SCE)
  344.            wrmsr
  345.  
  346.         ; Bits of EDX :
  347.         ; Bit 31..16 During the SYSRET instruction, this field is copied into the CS register
  348.         ;  and the contents of this field, plus 8, are copied into the SS register.
  349.         ; Bit 15..0 During the SYSCALL instruction, this field is copied into the CS register
  350.         ;  and the contents of this field, plus 8, are copied into the SS register.
  351.  
  352.            mov edx, 0x1B000B    ; RING3 task stack will be used for fast syscalls!
  353.  
  354.            mov eax, syscall_entry
  355.            mov ecx, MSR_AMD_STAR
  356.            wrmsr
  357. .noSYSCALL:
  358. ; -----------------------------------------
  359.         stdcall alloc_page
  360.         stdcall map_page, tss-0xF80, eax, PG_SW         ; lower 0xF80 bytes might be used for something
  361.         stdcall alloc_page
  362.         inc     eax
  363.         mov     [SLOT_BASE+256+APPDATA.io_map], eax
  364.         stdcall map_page, tss+0x80, eax, PG_SW
  365.         stdcall alloc_page
  366.         inc     eax
  367.         mov     dword [SLOT_BASE+256+APPDATA.io_map+4], eax
  368.         stdcall map_page, tss+0x1080, eax, PG_SW
  369.  
  370. ; LOAD IDT
  371.  
  372.            call         build_interrupt_table ;lidt is executed
  373.  
  374.            call         init_kernel_heap
  375.            stdcall      kernel_alloc, RING0_STACK_SIZE+512
  376.            mov  [os_stack_seg], eax
  377.  
  378.            lea  esp, [eax+RING0_STACK_SIZE]
  379.  
  380.            mov  [tss._ss0], os_stack
  381.            mov  [tss._esp0], esp
  382.            mov  [tss._esp], esp
  383.            mov  [tss._cs],os_code
  384.            mov  [tss._ss],os_stack
  385.            mov  [tss._ds],app_data
  386.            mov  [tss._es],app_data
  387.            mov  [tss._fs],app_data
  388.            mov  [tss._gs],app_data
  389.            mov  [tss._io],128
  390. ;Add IO access table - bit array of permitted ports
  391.            mov  edi, tss._io_map_0
  392.            xor  eax, eax
  393.            mov  ecx, 2047
  394.            rep  stosd                ; access to 65504 ports granted
  395.            not  eax                  ; the last 32 ports blocked
  396.            stosd
  397.  
  398.            mov  ax,tss0
  399.            ltr  ax
  400.  
  401.            mov  [LFBSize], 0x800000
  402.            call init_LFB
  403.            call init_fpu
  404.            call init_malloc
  405. ;-
  406.            stdcall alloc_kernel_space, 0x51000
  407.            mov  [default_io_map], eax
  408.  
  409.            add  eax, 0x2000
  410.            mov  [ipc_tmp], eax
  411.            mov  ebx, 0x1000
  412.  
  413.            add  eax, 0x40000
  414.            mov  [proc_mem_map], eax
  415.  
  416.            add  eax, 0x8000
  417.            mov  [proc_mem_pdir], eax
  418.  
  419.            add  eax, ebx
  420.            mov  [proc_mem_tab], eax
  421.  
  422.            add  eax, ebx
  423.            mov  [tmp_task_pdir], eax
  424.  
  425.            add  eax, ebx
  426.            mov  [tmp_task_ptab], eax
  427.  
  428.            add  eax, ebx
  429.            mov  [ipc_pdir], eax
  430.  
  431.            add  eax, ebx
  432.            mov  [ipc_ptab], eax
  433.  
  434.            stdcall kernel_alloc, (unpack.LZMA_BASE_SIZE+(unpack.LZMA_LIT_SIZE shl \
  435.                                  (unpack.lc+unpack.lp)))*4
  436.  
  437.            mov  [unpack.p], eax     ; unpacker.inc
  438.  
  439.            call init_events
  440.            mov  eax, srv.fd-SRV_FD_OFFSET
  441.            mov  [srv.fd], eax
  442.            mov  [srv.bk], eax
  443.  
  444.            mov  edi, irq_tab
  445.            xor  eax, eax
  446.            mov  ecx, 16
  447.            rep  stosd
  448.  
  449. ;Set base of graphic segment to linear address of LFB
  450.         mov     eax,[LFBAddress]          ; set for gs
  451.         mov     [graph_data_l+2],ax
  452.         shr     eax,16
  453.         mov     [graph_data_l+4],al
  454.         mov     [graph_data_l+7],ah
  455.  
  456. ;       or      [KERNEL_ALLOC_FLAG], dword PG_NOCACHE   ;<<<<<<<<<<<<<<<<
  457.         stdcall kernel_alloc, [_WinMapSize]
  458.         mov     [_WinMapAddress], eax
  459. ;       xor     [KERNEL_ALLOC_FLAG], dword PG_NOCACHE   ;<<<<<<<<<<<<<<<<
  460.  
  461.         xor  eax,eax
  462.         inc  eax
  463.         mov [CURRENT_TASK],eax          ;dword 1
  464.         mov [TASK_COUNT],eax            ;dword 1
  465.         mov [TASK_BASE],dword TASK_DATA
  466.         mov [current_slot], SLOT_BASE+256
  467.  
  468. ; set background
  469.  
  470.         mov   [BgrDrawMode],eax
  471.         mov   [BgrDataWidth],eax
  472.         mov   [BgrDataHeight],eax
  473.         mov   [mem_BACKGROUND], 4
  474.         mov   [img_background], static_background_data
  475.  
  476.         mov     [SLOT_BASE + 256 + APPDATA.dir_table], sys_pgdir - OS_BASE
  477.  
  478.         stdcall kernel_alloc, 0x10000/8
  479.         mov     edi, eax
  480.         mov     [network_free_ports], eax
  481.         or      eax, -1
  482.         mov     ecx, 0x10000/32
  483.         rep     stosd
  484.  
  485. ; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
  486.  
  487.         call  rerouteirqs
  488.  
  489. ; Initialize system V86 machine
  490.         call    init_sys_v86
  491.  
  492. ; TIMER SET TO 1/100 S
  493.  
  494.         mov   al,0x34              ; set to 100Hz
  495.         out   0x43,al
  496.         mov   al,0x9b              ; lsb    1193180 / 1193
  497.         out   0x40,al
  498.         mov   al,0x2e              ; msb
  499.         out   0x40,al
  500.  
  501. ; Enable timer IRQ (IRQ0) and hard drives IRQs (IRQ14, IRQ15)
  502. ; they are used: when partitions are scanned, hd_read relies on timer
  503. ; Also enable IRQ2, because in some configurations
  504. ; IRQs from slave controller are not delivered until IRQ2 on master is enabled
  505.         mov     al, 0xFA
  506.         out     0x21, al
  507.         mov     al, 0x3F
  508.         out     0xA1, al
  509.  
  510. ; Enable interrupts in IDE controller
  511.         mov     al, 0
  512.         mov     dx, 0x3F6
  513.         out     dx, al
  514.         mov     dl, 0x76
  515.         out     dx, al
  516.  
  517. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  518. include 'detect/disks.inc'
  519. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  520.  
  521.   call Parser_params
  522.  
  523. ; READ RAMDISK IMAGE FROM HD
  524.  
  525. ;!!!!!!!!!!!!!!!!!!!!!!!
  526. ;include 'boot/rdload.inc'
  527. ;!!!!!!!!!!!!!!!!!!!!!!!
  528. ;    mov    [dma_hdd],1
  529. ; CALCULATE FAT CHAIN FOR RAMDISK
  530.  
  531.         call  calculatefatchain
  532.  
  533. ; LOAD VMODE DRIVER
  534.  
  535. ;!!!!!!!!!!!!!!!!!!!!!!!
  536. include 'vmodeld.inc'
  537. ;!!!!!!!!!!!!!!!!!!!!!!!
  538.  
  539.  
  540. ; LOAD FONTS I and II
  541.  
  542.         stdcall read_file, char, FONT_I, 0, 2304
  543.         stdcall read_file, char2, FONT_II, 0, 2560
  544.  
  545. ;        mov   esi,boot_fonts
  546. ;        call  boot_log
  547.  
  548. ; PRINT AMOUNT OF MEMORY
  549. ;        mov     esi, boot_memdetect
  550. ;        call    boot_log
  551.  
  552.         movzx   ecx, word [boot_y]
  553.         or      ecx, (10+29*6) shl 16 ; "Determining amount of memory"
  554.         sub     ecx, 10
  555.         mov     edx, 0xFFFFFF
  556.         mov     ebx, [MEM_AMOUNT]
  557.         shr     ebx, 20
  558.         xor     edi,edi
  559.         mov     eax, 0x00040000
  560.                 inc             edi
  561.         call    display_number_force
  562.  
  563. ; BUILD SCHEDULER
  564.  
  565.         call   build_scheduler ; sys32.inc
  566.  
  567. ;        mov    esi,boot_devices
  568. ;        call   boot_log
  569.  
  570.         mov  [pci_access_enabled],1
  571.  
  572.  
  573. ; SET PRELIMINARY WINDOW STACK AND POSITIONS
  574.  
  575.         mov   esi,boot_windefs
  576.         call  boot_log
  577.         call  set_window_defaults
  578.  
  579. ; SET BACKGROUND DEFAULTS
  580.  
  581. ;        mov   esi,boot_bgr
  582. ;        call  boot_log
  583.         call  init_background   ;
  584.         call  calculatebackground
  585.  
  586. ; SET UP OS TASK
  587.  
  588. ;        mov  esi,boot_setostask
  589. ;        call boot_log
  590.  
  591.         xor  eax, eax
  592.         mov  dword [SLOT_BASE+APPDATA.fpu_state], fpu_data
  593.         mov  dword [SLOT_BASE+APPDATA.exc_handler], eax
  594.         mov  dword [SLOT_BASE+APPDATA.except_mask], eax
  595.  
  596.         ; name for OS/IDLE process
  597.  
  598.         mov dword [SLOT_BASE+256+APPDATA.app_name],   dword 'OS/I'
  599.         mov dword [SLOT_BASE+256+APPDATA.app_name+4], dword 'DLE '
  600.         mov edi, [os_stack_seg]
  601.         mov dword [SLOT_BASE+256+APPDATA.pl0_stack], edi
  602.         add edi, 0x2000-512
  603.         mov dword [SLOT_BASE+256+APPDATA.fpu_state], edi
  604.         mov dword [SLOT_BASE+256+APPDATA.saved_esp0], edi ; just for case
  605.         ; [SLOT_BASE+256+APPDATA.io_map] was set earlier
  606.  
  607.         mov esi, fpu_data
  608.         mov ecx, 512/4
  609.         cld
  610.         rep movsd
  611.  
  612.         mov dword [SLOT_BASE+256+APPDATA.exc_handler], eax
  613.         mov dword [SLOT_BASE+256+APPDATA.except_mask], eax
  614.  
  615.         mov ebx, SLOT_BASE+256+APP_OBJ_OFFSET
  616.         mov  dword [SLOT_BASE+256+APPDATA.fd_obj], ebx
  617.         mov  dword [SLOT_BASE+256+APPDATA.bk_obj], ebx
  618.  
  619.         mov  dword [SLOT_BASE+256+APPDATA.cur_dir], sysdir_path
  620.         mov dword [SLOT_BASE+256+APPDATA.tls_base], eax
  621.  
  622.         ; task list
  623.         mov  dword [TASK_DATA+TASKDATA.mem_start],eax   ; process base address
  624.         inc  eax
  625.         mov  dword [CURRENT_TASK],eax
  626.         mov  dword [TASK_COUNT],eax
  627.         mov  [current_slot], SLOT_BASE+256
  628.         mov  [TASK_BASE],dword TASK_DATA
  629.         mov  byte[TASK_DATA+TASKDATA.wnd_number],al     ; on screen number
  630.         mov  dword [TASK_DATA+TASKDATA.pid], eax        ; process id number
  631.  
  632.         call init_display
  633.         mov eax, [def_cursor]
  634.         mov [SLOT_BASE+APPDATA.cursor],eax
  635.         mov [SLOT_BASE+APPDATA.cursor+256],eax
  636.  
  637.  
  638.   ; READ TSC / SECOND == Fusion only!
  639.  
  640.         cli
  641.         mov     edx, PCIe_CONFIG_SPACE + 0xE0
  642.         mov     eax, 0x013080F0         ; BIOS timer reg.
  643.         mov     [edx], eax
  644.         add     dl, 4
  645.         mov     edi, edx
  646.         mov     eax, [edi]              ; old microseconds
  647.         inc     eax                     ; next precise microsecond
  648.         mov     esi, eax
  649.  @@:
  650.         mov     eax, [edi]
  651.         cmp     eax, esi
  652.         jne     @b
  653.  
  654.         rdtsc
  655.         mov     ebp, eax                ; clockmark
  656.         add     esi, 20                 ; wait 20us
  657.  @@:
  658.         mov     eax, [edi]
  659.         cmp     eax, esi
  660.         jne     @b
  661.  
  662.         rdtsc
  663.         sub     eax, ebp
  664.         mov     ebx, 50000
  665.         mul     ebx                   ; clks per second
  666.         sti
  667.  
  668.         mov   [CPU_FREQ],eax          ; save tsc / sec
  669.         mov   ebx, 1000000
  670.         div   ebx
  671.         mov [stall_mcs], eax
  672. ; PRINT CPU FREQUENCY
  673.         mov     esi, boot_cpufreq
  674.         call    boot_log
  675.  
  676.         mov     ebx, eax
  677.         movzx   ecx, word [boot_y]
  678.         add     ecx, (10+17*6) shl 16 - 10 ; 'CPU frequency is '
  679.         mov     edx, 0xFFFFFF
  680.         xor     edi,edi
  681.         mov     eax, 0x00040000
  682.                 inc             edi
  683.         call    display_number_force
  684.  
  685. ; SET VARIABLES
  686.  
  687.         call  set_variables
  688.  
  689. ; SET MOUSE
  690.  
  691.         stdcall load_driver, szPS2MDriver
  692.  
  693.         cli
  694.  
  695. ; STACK AND FDC
  696.  
  697.         call  stack_init
  698.         call  fdc_init
  699.  
  700.  
  701. ; LOAD DEFAULT SKIN
  702.  
  703.         call    load_default_skin
  704.  
  705. ;protect io permission map
  706.  
  707.            mov esi, [default_io_map]
  708.            stdcall map_page,esi,[SLOT_BASE+256+APPDATA.io_map], PG_MAP
  709.            add esi, 0x1000
  710.            stdcall map_page,esi,[SLOT_BASE+256+APPDATA.io_map+4], PG_MAP
  711.  
  712.            stdcall map_page,tss._io_map_0,\
  713.                    [SLOT_BASE+256+APPDATA.io_map], PG_MAP
  714.            stdcall map_page,tss._io_map_1,\
  715.                    [SLOT_BASE+256+APPDATA.io_map+4], PG_MAP
  716.  
  717.  
  718.         call init_userDMA       ; <<<<<<<<< ============== core/memory.inc =================
  719. ;        mov     esi, boot_uDMA_ok
  720. ;        call    boot_log
  721.  
  722. ; LOAD FIRST APPLICATION
  723.         cli
  724.  
  725.         cmp   byte [BOOT_VAR+0x9030],1
  726.         jne   no_load_vrr_m
  727.  
  728.         mov     ebp, vrr_m
  729.         call    fs_execute_from_sysdir
  730.  
  731. ;        cmp   eax,2                  ; if vrr_m app found (PID=2)
  732.         sub   eax,2
  733.         jz    first_app_found
  734.  
  735. no_load_vrr_m:
  736.  
  737.         mov     ebp, firstapp
  738.         call    fs_execute_from_sysdir
  739.  
  740. ;        cmp   eax,2                  ; continue if a process has been loaded
  741.         sub   eax,2
  742.         jz    first_app_found
  743.  
  744. ;        mov     esi, boot_failed
  745. ;        call    boot_log
  746.  
  747.         mov   eax, 0xDEADBEEF        ; otherwise halt
  748.         hlt
  749.  
  750. first_app_found:
  751.  
  752.         cli
  753.  
  754.         ;mov   [TASK_COUNT],dword 2
  755.         push  1
  756.         pop   dword [CURRENT_TASK]      ; set OS task fisrt
  757.  
  758. ; SET KEYBOARD PARAMETERS
  759.         mov   al, 0xf6         ; reset keyboard, scan enabled
  760.         call  kb_write
  761.  
  762.         ; wait until 8042 is ready
  763.         xor ecx,ecx
  764.       @@:
  765.         in     al,64h
  766.         and    al,00000010b
  767.         loopnz @b
  768.  
  769.  
  770.         mov   al, 0xF3       ; set repeat rate & delay
  771.         call  kb_write
  772. ;        call  kb_read
  773.         mov   al, 0 ; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  774.         call  kb_write
  775. ;        call  kb_read
  776.      ;// mike.dld [
  777.         call  set_lights
  778.      ;// mike.dld ]
  779.  
  780.  
  781.  
  782. ; START MULTITASKING
  783.  
  784.         stdcall attach_int_handler, 1, irq1, 0
  785.  
  786.         cmp     [IDEContrRegsBaseAddr], 0
  787.         setnz   [dma_hdd]
  788.         mov [timer_ticks_enable],1              ; for cd driver
  789.  
  790.         sti
  791.         call change_task
  792.  
  793.         jmp osloop                              ; Fly :)
  794.  
  795. diff16 "init code end  ",0,$
  796.  
  797. unpacker_inc:
  798. include 'unpacker.inc'
  799. diff16 "unpacker code  ",unpacker_inc,$
  800.  
  801. include 'fdo.inc'
  802.  
  803. align 4
  804. boot_log:
  805.          pushad
  806.  
  807.         mov   ebx,10*65536
  808.         mov   bx,word [boot_y]
  809.         add   [boot_y],dword 10
  810.         mov   ecx,0x80ffffff   ; ASCIIZ string with white color
  811.                 xor       edi,edi
  812.         mov   edx,esi
  813.                 inc       edi
  814.         call  dtext
  815.  
  816.         popad
  817.  
  818.         ret
  819.  
  820. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  821. ;                                                                    ;
  822. ;                    MAIN OS LOOP START                              ;
  823. ;                                                                    ;
  824. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  825. align 32
  826. osloop:
  827.         call   [draw_pointer]
  828.         call    window_check_events
  829.         call    mouse_check_events
  830.         call   checkmisc
  831.         call   stack_handler
  832.         call   checkidle
  833.         jmp    osloop
  834. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  835. ;                                                                    ;
  836. ;                      MAIN OS LOOP END                              ;
  837. ;                                                                    ;
  838. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  839. align 4
  840. checkidle:
  841.         pushad
  842.         call    change_task
  843.         jmp     idle_loop_entry
  844.   idle_loop:
  845.         cmp     eax,[idlemem]     ; eax == [timer_ticks]
  846.         jne     idle_exit
  847.         rdtsc   ;call _rdtsc
  848.         mov     ecx,eax
  849.         hlt
  850.         rdtsc   ;call _rdtsc
  851.         sub     eax,ecx
  852.         add     [idleuse],eax
  853.   idle_loop_entry:
  854.         mov     eax,[timer_ticks] ; eax =  [timer_ticks]
  855.         cmp     [check_idle_semaphore],0
  856.         je      idle_loop
  857.         dec     [check_idle_semaphore]
  858.   idle_exit:
  859.         mov     [idlemem],eax     ; eax == [timer_ticks]
  860.         popad
  861.         ret
  862.  
  863. uglobal
  864.   idlemem               dd   0x0
  865.   idleuse               dd   0x0
  866.   idleusesec            dd   0x0
  867.   check_idle_semaphore  dd   0x0
  868. endg
  869.  
  870.  
  871.  
  872. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  873. ;                                                                      ;
  874. ;                   INCLUDED SYSTEM FILES                              ;
  875. ;                                                                      ;
  876. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  877.  
  878.  
  879. include "kernel32.inc"
  880.  
  881.  
  882. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  883. ;                                                                      ;
  884. ;                       KERNEL FUNCTIONS                               ;
  885. ;                                                                      ;
  886. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  887.  
  888. reserve_irqs_ports:
  889.         ; removed
  890.         ret
  891.  
  892. setirqreadports:
  893.         ; removed
  894.         ret
  895.  
  896. iglobal
  897.   process_number dd 0x1
  898. endg
  899.  
  900. set_variables:
  901.  
  902.         mov   ecx,0x100                       ; flush port 0x60
  903. .fl60:  in    al,0x60
  904.         loop  .fl60
  905.         push  eax
  906.  
  907.         mov   ax,[BOOT_VAR+0x900c]
  908.         shr   ax,1
  909.         shl   eax,16
  910.         mov   ax,[BOOT_VAR+0x900A]
  911.         shr   ax,1
  912.         mov   [MOUSE_X],eax
  913.  
  914.         xor   eax,eax
  915.         mov   [BTN_ADDR],dword BUTTON_INFO    ; address of button list
  916.  
  917.         mov   byte [MOUSE_BUFF_COUNT],al                 ; mouse buffer
  918.         mov   byte [KEY_COUNT],al                 ; keyboard buffer
  919.         mov   byte [BTN_COUNT],al                 ; button buffer
  920. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  921.  
  922.      ;!! IP 04.02.2005:
  923.         mov   byte [DONT_SWITCH],al ; change task if possible
  924.         pop   eax
  925.         ret
  926.  
  927. align 4
  928. ;input  eax=43,bl-byte of output, ecx - number of port
  929. sys_outport:
  930.      and   [esp+32],dword 1     ; for backward compatibility: operation failed
  931.     ret
  932.  
  933. display_number:
  934. ;It is not optimization
  935.         mov     eax, ebx
  936.         mov     ebx, ecx
  937.         mov     ecx, edx
  938.         mov     edx, esi
  939.         mov     esi, edi
  940. ; eax = print type, al=0 -> ebx is number
  941. ;                   al=1 -> ebx is pointer
  942. ;                   ah=0 -> display decimal
  943. ;                   ah=1 -> display hexadecimal
  944. ;                   ah=2 -> display binary
  945. ;                   eax bits 16-21 = number of digits to display (0-32)
  946. ;                   eax bits 22-31 = reserved
  947. ;
  948. ; ebx = number or pointer
  949. ; ecx = x shl 16 + y
  950. ; edx = color
  951.     xor     edi, edi
  952. display_number_force:
  953.     push  eax
  954.     and   eax,0x3fffffff
  955.     cmp   eax,0xffff            ; length > 0 ?
  956.     pop   eax
  957.     jge   cont_displ
  958.     ret
  959.    cont_displ:
  960.      push  eax
  961.      and   eax,0x3fffffff
  962.      cmp   eax,61*0x10000        ; length <= 60 ?
  963.      pop   eax
  964.      jb    cont_displ2
  965.      ret
  966.    cont_displ2:
  967.  
  968.      pushad
  969.  
  970.      cmp   al,1                  ; ecx is a pointer ?
  971.      jne   displnl1
  972.      mov   ebp,ebx
  973.      add   ebp,4
  974.      mov   ebp,[ebp+std_application_base_address]
  975.      mov   ebx,[ebx+std_application_base_address]
  976.  displnl1:
  977.      sub   esp,64
  978.  
  979.     test   ah,ah                  ; DECIMAL
  980.     jnz   no_display_desnum
  981.     shr   eax,16
  982.     and   eax,0xC03f
  983. ;     and   eax,0x3f
  984.     push  eax
  985.     and   eax,0x3f
  986.     mov   edi,esp
  987.     add   edi,4+64-1
  988.     mov   ecx,eax
  989.     mov   eax,ebx
  990.     mov   ebx,10
  991.  d_desnum:
  992.      xor   edx,edx
  993.      call  division_64_bits
  994.      div   ebx
  995.      add   dl,48
  996.      mov   [edi],dl
  997.      dec   edi
  998.      loop  d_desnum
  999.      pop   eax
  1000.      call  normalize_number
  1001.      call  draw_num_text
  1002.      add   esp,64
  1003.      popad
  1004.      ret
  1005.    no_display_desnum:
  1006.  
  1007.      cmp   ah,0x01               ; HEXADECIMAL
  1008.      jne   no_display_hexnum
  1009.      shr   eax,16
  1010.      and   eax,0xC03f
  1011. ;     and   eax,0x3f
  1012.      push  eax
  1013.      and   eax,0x3f
  1014.      mov   edi,esp
  1015.      add   edi,4+64-1
  1016.      mov   ecx,eax
  1017.      mov   eax,ebx
  1018.      mov   ebx,16
  1019.    d_hexnum:
  1020.      xor   edx,edx
  1021.      call  division_64_bits
  1022.      div   ebx
  1023.    hexletters = __fdo_hexdigits
  1024.      add   edx,hexletters
  1025.      mov   dl,[edx]
  1026.      mov   [edi],dl
  1027.      dec   edi
  1028.      loop  d_hexnum
  1029.      pop   eax
  1030.      call  normalize_number
  1031.      call  draw_num_text
  1032.      add   esp,64
  1033.      popad
  1034.      ret
  1035.    no_display_hexnum:
  1036.  
  1037.      cmp   ah,0x02               ; BINARY
  1038.      jne   no_display_binnum
  1039.      shr   eax,16
  1040.      and   eax,0xC03f
  1041. ;     and   eax,0x3f
  1042.      push  eax
  1043.      and   eax,0x3f
  1044.      mov   edi,esp
  1045.      add   edi,4+64-1
  1046.      mov   ecx,eax
  1047.      mov   eax,ebx
  1048.      mov   ebx,2
  1049.    d_binnum:
  1050.      xor   edx,edx
  1051.      call  division_64_bits
  1052.      div   ebx
  1053.      add   dl,48
  1054.      mov   [edi],dl
  1055.      dec   edi
  1056.      loop  d_binnum
  1057.      pop   eax
  1058.      call  normalize_number
  1059.      call  draw_num_text
  1060.      add   esp,64
  1061.      popad
  1062.      ret
  1063.    no_display_binnum:
  1064.  
  1065.      add   esp,64
  1066.      popad
  1067.      ret
  1068.  
  1069. normalize_number:
  1070.      test  ah,0x80
  1071.      jz   .continue
  1072.      mov  ecx,48
  1073.      and   eax,0x3f
  1074. @@:
  1075.      inc   edi
  1076.      cmp   [edi],cl
  1077.      jne   .continue
  1078.      dec   eax
  1079.      cmp   eax,1
  1080.      ja    @r
  1081.      mov   al,1
  1082. .continue:
  1083.      and   eax,0x3f
  1084.      ret
  1085.  
  1086. division_64_bits:
  1087.      test  [esp+1+4],byte 0x40
  1088.      jz   .continue
  1089.      push  eax
  1090.      mov   eax,ebp
  1091.      div   ebx
  1092.      mov   ebp,eax
  1093.      pop   eax
  1094. .continue:
  1095.      ret
  1096.  
  1097. draw_num_text:
  1098.      mov   esi,eax
  1099.      mov   edx,64+4
  1100.      sub   edx,eax
  1101.      add   edx,esp
  1102.      mov   ebx,[esp+64+32-8+4]
  1103. ; add window start x & y
  1104.      mov   ecx,[TASK_BASE]
  1105.  
  1106.      mov   edi,[CURRENT_TASK]
  1107.      shl   edi,8
  1108.  
  1109.      mov   eax,[ecx-twdw+WDATA.box.left]
  1110.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1111.      shl   eax,16
  1112.      add   eax,[ecx-twdw+WDATA.box.top]
  1113.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1114.      add   ebx,eax
  1115.      mov   ecx,[esp+64+32-12+4]
  1116.         and     ecx, not 0x80000000     ; force counted string
  1117.         mov     eax, [esp+64+8]         ; background color (if given)
  1118.         mov     edi, [esp+64+4]
  1119.      jmp   dtext
  1120.  
  1121. align 4
  1122.  
  1123. sys_setup:
  1124.  
  1125. ; 1=roland mpu midi base , base io address
  1126. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1127. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1128. ; 5=system language, 1eng 2fi 3ger 4rus
  1129. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1130. ; 8=fat32 partition in hd
  1131. ; 9
  1132. ; 10 = sound dma channel
  1133. ; 11 = enable lba read
  1134. ; 12 = enable pci access
  1135.  
  1136.  
  1137.         and  [esp+32],dword 0
  1138.         dec     ebx                             ; MIDI
  1139.         jnz  nsyse1
  1140.         cmp  ecx,0x100
  1141.  
  1142.         jb   nsyse1
  1143.         mov     esi,65535
  1144.         cmp     esi,ecx
  1145.  
  1146.         jb   nsyse1
  1147.         mov  [midi_base],cx     ;bx
  1148.         mov  word [mididp],cx   ;bx
  1149.         inc  cx                 ;bx
  1150.         mov  word [midisp],cx   ;bx
  1151.         ret
  1152.  
  1153. iglobal
  1154. midi_base dw 0
  1155. endg
  1156.  
  1157.    nsyse1:
  1158.         dec     ebx                              ; KEYBOARD
  1159.         jnz  nsyse2
  1160.         mov  edi,[TASK_BASE]
  1161.         mov  eax,[edi+TASKDATA.mem_start]
  1162.         add  eax,edx
  1163.  
  1164.         dec     ecx
  1165.         jnz  kbnobase
  1166.         mov  ebx,keymap
  1167.         mov  ecx,128
  1168.         call memmove
  1169.         ret
  1170.    kbnobase:
  1171.         dec  ecx
  1172.         jnz  kbnoshift
  1173.  
  1174.         mov  ebx,keymap_shift
  1175.         mov  ecx,128
  1176.         call memmove
  1177.         ret
  1178.    kbnoshift:
  1179.         dec  ecx
  1180.         jnz  kbnoalt
  1181.         mov  ebx,keymap_alt
  1182.         mov  ecx,128
  1183.         call memmove
  1184.         ret
  1185.    kbnoalt:
  1186.         sub  ecx,6
  1187.         jnz  kbnocountry
  1188.         mov  word [keyboard],dx
  1189.         ret
  1190.    kbnocountry:
  1191.         mov  [esp+32],dword 1
  1192.         ret
  1193.    nsyse2:
  1194.         dec  ebx                            ; CD
  1195.         jnz  nsyse4
  1196.  
  1197.         test ecx,ecx
  1198.         jz   nosesl
  1199.  
  1200.         cmp  ecx, 4
  1201.         ja   nosesl
  1202.         mov  [cd_base],cl
  1203.  
  1204.         dec     ecx
  1205.         jnz  noprma
  1206.         mov  [cdbase],0x1f0
  1207.         mov  [cdid],0xa0
  1208.    noprma:
  1209.  
  1210.         dec     ecx
  1211.         jnz  noprsl
  1212.         mov  [cdbase],0x1f0
  1213.         mov  [cdid],0xb0
  1214.    noprsl:
  1215.         dec     ecx
  1216.         jnz  nosema
  1217.         mov  [cdbase],0x170
  1218.         mov  [cdid],0xa0
  1219.    nosema:
  1220.         dec     ecx
  1221.         jnz  nosesl
  1222.         mov  [cdbase],0x170
  1223.         mov  [cdid],0xb0
  1224.    nosesl:
  1225.         ret
  1226.  
  1227. iglobal
  1228. cd_base db 0
  1229.  
  1230. endg
  1231.    nsyse4:
  1232.  
  1233.         sub  ebx,2               ; SYSTEM LANGUAGE
  1234.         jnz  nsyse5
  1235.         mov  [syslang],ecx
  1236.         ret
  1237.    nsyse5:
  1238.  
  1239.         sub  ebx,2              ; HD BASE
  1240.         jnz  nsyse7
  1241.  
  1242.         test ecx,ecx
  1243.         jz   nosethd
  1244.  
  1245.         cmp  ecx,4
  1246.         ja   nosethd
  1247.         mov  [hd_base],cl
  1248.  
  1249.         cmp  ecx,1
  1250.         jnz  noprmahd
  1251.         mov  [hdbase],0x1f0
  1252.         and  dword [hdid],0x0
  1253.         mov  dword [hdpos],ecx
  1254. ;     call set_FAT32_variables
  1255.    noprmahd:
  1256.  
  1257.         cmp  ecx,2
  1258.         jnz  noprslhd
  1259.         mov  [hdbase],0x1f0
  1260.         mov  [hdid],0x10
  1261.         mov  dword [hdpos],ecx
  1262. ;     call set_FAT32_variables
  1263.    noprslhd:
  1264.  
  1265.         cmp  ecx,3
  1266.         jnz  nosemahd
  1267.         mov  [hdbase],0x170
  1268.         and  dword [hdid],0x0
  1269.         mov  dword [hdpos],ecx
  1270. ;     call set_FAT32_variables
  1271.    nosemahd:
  1272.  
  1273.         cmp  ecx,4
  1274.         jnz  noseslhd
  1275.         mov  [hdbase],0x170
  1276.         mov  [hdid],0x10
  1277.         mov  dword [hdpos],ecx
  1278. ;     call set_FAT32_variables
  1279.    noseslhd:
  1280.         call  reserve_hd1
  1281.         call  reserve_hd_channel
  1282.         call  free_hd_channel
  1283.         and   dword [hd1_status],0        ; free
  1284.    nosethd:
  1285.         ret
  1286.  
  1287. iglobal
  1288. hd_base db 0
  1289. endg
  1290.  
  1291. nsyse7:
  1292.  
  1293. ;     cmp  eax,8                      ; HD PARTITION
  1294.         dec  ebx
  1295.         jnz  nsyse8
  1296.         mov  [fat32part],ecx
  1297. ;     call set_FAT32_variables
  1298.         call  reserve_hd1
  1299.         call  reserve_hd_channel
  1300.         call  free_hd_channel
  1301. ;       pusha
  1302.         call  choice_necessity_partition_1
  1303. ;       popa
  1304.         and dword [hd1_status],0        ; free
  1305.         ret
  1306.  
  1307. nsyse8:
  1308. ;     cmp  eax,11                     ; ENABLE LBA READ
  1309.         and  ecx,1
  1310.         sub  ebx,3
  1311.         jnz  no_set_lba_read
  1312.         mov  [lba_read_enabled],ecx
  1313.         ret
  1314.  
  1315. no_set_lba_read:
  1316. ;     cmp  eax,12                     ; ENABLE PCI ACCESS
  1317.         dec  ebx
  1318.         jnz  no_set_pci_access
  1319.         mov  [pci_access_enabled],ecx
  1320.         ret
  1321. no_set_pci_access:
  1322.  
  1323. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1324. include 'vmodeint.inc'
  1325. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1326.  
  1327. sys_setup_err:
  1328.         or  [esp+32],dword -1
  1329.         ret
  1330.  
  1331. align 4
  1332.  
  1333. sys_getsetup:
  1334.  
  1335. ; 1=roland mpu midi base , base io address
  1336. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1337. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1338. ; 5=system language, 1eng 2fi 3ger 4rus
  1339. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1340. ; 8=fat32 partition in hd
  1341. ; 9=get hs timer tic
  1342.  
  1343. ;     cmp  eax,1
  1344.         dec     ebx
  1345.         jnz  ngsyse1
  1346.         movzx eax,[midi_base]
  1347.         mov  [esp+32],eax
  1348.         ret
  1349. ngsyse1:
  1350. ;     cmp  eax,2
  1351.         dec     ebx
  1352.         jnz  ngsyse2
  1353.  
  1354.         mov  edi,[TASK_BASE]
  1355.         mov  ebx,[edi+TASKDATA.mem_start]
  1356.         add  ebx,edx
  1357.  
  1358. ;     cmp  ebx,1
  1359.         dec     ecx
  1360.         jnz  kbnobaseret
  1361.         mov  eax,keymap
  1362.         mov  ecx,128
  1363.         call memmove
  1364.         ret
  1365. kbnobaseret:
  1366. ;     cmp  ebx,2
  1367.         dec     ecx
  1368.         jnz  kbnoshiftret
  1369.  
  1370.         mov  eax,keymap_shift
  1371.         mov  ecx,128
  1372.         call memmove
  1373.         ret
  1374. kbnoshiftret:
  1375. ;     cmp  ebx,3
  1376.         dec     ecx
  1377.         jne  kbnoaltret
  1378.  
  1379.         mov  eax,keymap_alt
  1380.         mov  ecx,128
  1381.         call memmove
  1382.         ret
  1383. kbnoaltret:
  1384. ;     cmp  ebx,9
  1385.         sub     ecx,6
  1386.         jnz  ngsyse2
  1387.         movzx eax,word [keyboard]
  1388.         mov  [esp+32],eax
  1389.         ret
  1390.  
  1391.  
  1392. ngsyse2:
  1393. ;         cmp  eax,3
  1394.         dec     ebx
  1395.         jnz  ngsyse3
  1396.         movzx eax,[cd_base]
  1397.         mov  [esp+32],eax
  1398.         ret
  1399. ngsyse3:
  1400. ;         cmp  eax,5
  1401.         sub     ebx,2
  1402.         jnz  ngsyse5
  1403.         mov  eax,[syslang]
  1404.         mov  [esp+32],eax
  1405.         ret
  1406. ngsyse5:
  1407. ;     cmp  eax,7
  1408.         sub     ebx,2
  1409.         jnz  ngsyse7
  1410.         movzx eax,[hd_base]
  1411.         mov  [esp+32],eax
  1412.         ret
  1413. ngsyse7:
  1414. ;     cmp  eax,8
  1415.         dec     ebx
  1416.         jnz  ngsyse8
  1417.         mov eax,[fat32part]
  1418.         mov  [esp+32],eax
  1419.         ret
  1420. ngsyse8:
  1421. ;     cmp  eax,9
  1422.         dec     ebx
  1423.         jnz  ngsyse9
  1424.         mov  eax,[timer_ticks] ;[0xfdf0]
  1425.         mov  [esp+32],eax
  1426.         ret
  1427. ngsyse9:
  1428. ;     cmp  eax,11
  1429.         sub     ebx,2
  1430.         jnz  ngsyse11
  1431.         mov eax,[lba_read_enabled]
  1432.         mov  [esp+32],eax
  1433.         ret
  1434. ngsyse11:
  1435. ;     cmp  eax,12
  1436.         dec     ebx
  1437.         jnz  ngsyse12
  1438.         mov eax,[pci_access_enabled]
  1439.         mov  [esp+32],eax
  1440.         ret
  1441. ngsyse12:
  1442.         mov  [esp+32],dword 1
  1443.         ret
  1444.  
  1445.  
  1446. get_timer_ticks:
  1447.         mov eax,[timer_ticks]
  1448.         ret
  1449.  
  1450. iglobal
  1451. align 4
  1452. mousefn dd msscreen, mswin, msbutton, msset
  1453.         dd app_load_cursor
  1454.         dd app_set_cursor
  1455.         dd app_delete_cursor
  1456.         dd msz
  1457. endg
  1458.  
  1459. readmousepos:
  1460.  
  1461. ; eax=0 screen relative
  1462. ; eax=1 window relative
  1463. ; eax=2 buttons pressed
  1464. ; eax=3 set mouse pos   ; reserved
  1465. ; eax=4 load cursor
  1466. ; eax=5 set cursor
  1467. ; eax=6 delete cursor   ; reserved
  1468. ; eax=7 get mouse_z
  1469.  
  1470.            cmp ebx, 7
  1471.            ja msset
  1472.            jmp [mousefn+ebx*4]
  1473. msscreen:
  1474.            mov  eax,[MOUSE_X]
  1475.            shl  eax,16
  1476.            mov  ax,[MOUSE_Y]
  1477.            mov  [esp+36-4],eax
  1478.            ret
  1479. mswin:
  1480.            mov  eax,[MOUSE_X]
  1481.            shl  eax,16
  1482.            mov  ax,[MOUSE_Y]
  1483.            mov  esi,[TASK_BASE]
  1484.            mov  bx, word [esi-twdw+WDATA.box.left]
  1485.            shl  ebx,16
  1486.            mov  bx, word [esi-twdw+WDATA.box.top]
  1487.            sub  eax,ebx
  1488.  
  1489.            mov  edi,[CURRENT_TASK]
  1490.            shl  edi,8
  1491.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1492.            rol  eax,16
  1493.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1494.            rol  eax,16
  1495.            mov  [esp+36-4],eax
  1496.            ret
  1497. msbutton:
  1498.            movzx eax,byte [BTN_DOWN]
  1499.            mov  [esp+36-4],eax
  1500.            ret
  1501. msz:
  1502.            mov   edi, [TASK_COUNT]
  1503.            movzx edi, word [WIN_POS + edi*2]
  1504.            cmp   edi, [CURRENT_TASK]
  1505.            jne   @f
  1506.            mov   ax,[MOUSE_SCROLL_H]
  1507.            shl   eax,16
  1508.            mov   ax,[MOUSE_SCROLL_V]
  1509.            mov   [esp+36-4],eax
  1510.            and   [MOUSE_SCROLL_H],word 0
  1511.            and   [MOUSE_SCROLL_V],word 0
  1512.            ret
  1513.        @@:
  1514.            and  [esp+36-4],dword 0
  1515. ;           ret
  1516. msset:
  1517.            ret
  1518.  
  1519. app_load_cursor:
  1520.            cmp ecx, OS_BASE
  1521.            jae msset
  1522.            stdcall load_cursor, ecx, edx
  1523.            mov [esp+36-4], eax
  1524.            ret
  1525.  
  1526. app_set_cursor:
  1527.            stdcall set_cursor, ecx
  1528.            mov [esp+36-4], eax
  1529.            ret
  1530.  
  1531. app_delete_cursor:
  1532.            stdcall delete_cursor, ecx
  1533.            mov [esp+36-4], eax
  1534.            ret
  1535.  
  1536. is_input:
  1537.  
  1538.    push edx
  1539.    mov  dx,word [midisp]
  1540.    in   al,dx
  1541.    and  al,0x80
  1542.    pop  edx
  1543.    ret
  1544.  
  1545. is_output:
  1546.  
  1547.    push edx
  1548.    mov  dx,word [midisp]
  1549.    in   al,dx
  1550.    and  al,0x40
  1551.    pop  edx
  1552.    ret
  1553.  
  1554.  
  1555. get_mpu_in:
  1556.  
  1557.    push edx
  1558.    mov  dx,word [mididp]
  1559.    in   al,dx
  1560.    pop  edx
  1561.    ret
  1562.  
  1563.  
  1564. put_mpu_out:
  1565.  
  1566.    push edx
  1567.    mov  dx,word [mididp]
  1568.    out  dx,al
  1569.    pop  edx
  1570.    ret
  1571.  
  1572.  
  1573.  
  1574. align 4
  1575.  
  1576. sys_midi:
  1577.         cmp  [mididp],0
  1578.         jnz  sm0
  1579.         mov  [esp+36],dword 1
  1580.         ret
  1581. sm0:
  1582.         and  [esp+36],dword 0
  1583.         dec  ebx
  1584.         jnz  smn1
  1585.  ;    call setuart
  1586. su1:
  1587.         call is_output
  1588.         test al,al
  1589.         jnz  su1
  1590.         mov  dx,word [midisp]
  1591.         mov  al,0xff
  1592.         out  dx,al
  1593. su2:
  1594.         mov  dx,word [midisp]
  1595.         mov  al,0xff
  1596.         out  dx,al
  1597.         call is_input
  1598.         test al,al
  1599.         jnz  su2
  1600.         call get_mpu_in
  1601.         cmp  al,0xfe
  1602.         jnz  su2
  1603. su3:
  1604.         call is_output
  1605.         test  al,al
  1606.         jnz  su3
  1607.         mov  dx,word [midisp]
  1608.         mov  al,0x3f
  1609.         out  dx,al
  1610.         ret
  1611. smn1:
  1612.         dec  ebx
  1613.         jnz  smn2
  1614. sm10:
  1615.         call get_mpu_in
  1616.         call is_output
  1617.         test al,al
  1618.         jnz  sm10
  1619.         mov  al,bl
  1620.         call put_mpu_out
  1621.         smn2:
  1622.         ret
  1623.  
  1624. detect_devices:
  1625. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1626. ;include 'detect/commouse.inc'
  1627. ;include 'detect/ps2mouse.inc'
  1628. ;include 'detect/dev_fd.inc'
  1629. ;include 'detect/dev_hdcd.inc'
  1630. ;include 'detect/sear_par.inc'
  1631. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1632.     ret
  1633.  
  1634. sys_end:
  1635.  
  1636.      mov ecx, [current_slot]
  1637.      mov eax, [ecx+APPDATA.tls_base]
  1638.      test eax, eax
  1639.      jz @F
  1640.  
  1641.      stdcall user_free, eax
  1642. @@:
  1643.  
  1644.      mov   eax,[TASK_BASE]
  1645.      mov   [eax+TASKDATA.state], 3  ; terminate this program
  1646.  
  1647.     waitterm:            ; wait here for termination
  1648.      mov   ebx,100
  1649.      call  delay_hs
  1650.      jmp   waitterm
  1651.  
  1652. iglobal
  1653. align 4
  1654. sys_system_table:
  1655.         dd      exit_for_anyone         ; 1 = obsolete
  1656.         dd      sysfn_terminate         ; 2 = terminate thread
  1657.         dd      sysfn_activate          ; 3 = activate window
  1658.         dd      sysfn_getidletime       ; 4 = get idle time
  1659.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1660.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1661.         dd      sysfn_getactive         ; 7 = get active window
  1662.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1663.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1664.         dd      sysfn_minimize          ; 10 = minimize window
  1665.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1666.         dd      sysfn_lastkey           ; 12 = get last pressed key
  1667.         dd      sysfn_getversion        ; 13 = get kernel version
  1668.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1669.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1670.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1671.         dd      sysfn_getallmem         ; 17 = get total memory size
  1672.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1673.                                         ;                 instead of slot
  1674.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1675.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1676.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  1677.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  1678. sysfn_num = ($ - sys_system_table)/4
  1679. endg
  1680.  
  1681. sys_system:
  1682.         dec     ebx
  1683.         cmp     ebx, sysfn_num
  1684.         jae     @f
  1685.         jmp     dword [sys_system_table + ebx*4]
  1686. @@:
  1687.         ret
  1688.  
  1689.  
  1690. sysfn_shutdown:          ; 18.9 = system shutdown
  1691.      cmp  ecx,1
  1692.      jl   exit_for_anyone
  1693.      cmp  ecx,4
  1694.      jg   exit_for_anyone
  1695.      mov  [BOOT_VAR+0x9030],cl
  1696.  
  1697.      mov  eax,[TASK_COUNT]
  1698.      mov  [SYS_SHUTDOWN],al
  1699.      mov  [shutdown_processes],eax
  1700.      and  dword [esp+32], 0
  1701.  exit_for_anyone:
  1702.      ret
  1703.   uglobal
  1704.    shutdown_processes: dd 0x0
  1705.   endg
  1706.  
  1707. sysfn_terminate:        ; 18.2 = TERMINATE
  1708.      cmp  ecx,2
  1709.      jb   noprocessterminate
  1710.      mov  edx,[TASK_COUNT]
  1711.      cmp  ecx,edx
  1712.      ja   noprocessterminate
  1713.      mov  eax,[TASK_COUNT]
  1714.      shl  ecx,5
  1715.      mov  edx,[ecx+CURRENT_TASK+TASKDATA.pid]
  1716.      add  ecx,CURRENT_TASK+TASKDATA.state
  1717.      cmp  byte [ecx], 9
  1718.      jz   noprocessterminate
  1719.  
  1720.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  1721.      mov  [ecx],byte 3       ; clear possible i40's
  1722.      ;call MEM_Heap_UnLock
  1723.  
  1724.      cmp  edx,[application_table_status]    ; clear app table stat
  1725.      jne  noatsc
  1726.  
  1727.      and  [application_table_status],0
  1728.    noatsc:
  1729.    noprocessterminate:
  1730.      ret
  1731.  
  1732. sysfn_terminate2:
  1733. ;lock application_table_status mutex
  1734. .table_status:
  1735.     cli
  1736.     cmp    [application_table_status],0
  1737.     je     .stf
  1738.     sti
  1739.     call   change_task
  1740.     jmp    .table_status
  1741. .stf:
  1742.     call   set_application_table_status
  1743.     mov    eax,ecx
  1744.     call   pid_to_slot
  1745.     test   eax,eax
  1746.     jz     .not_found
  1747.     mov    ecx,eax
  1748.     cli
  1749.     call   sysfn_terminate
  1750.     and    [application_table_status],0
  1751.     sti
  1752.     and    dword [esp+32],0
  1753.     ret
  1754. .not_found:
  1755.     mov    [application_table_status],0
  1756.     or     dword [esp+32],-1
  1757.     ret
  1758.  
  1759. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  1760.      cmp  ecx,2
  1761.      jb   .nowindowactivate
  1762.      cmp  ecx,[TASK_COUNT]
  1763.      ja   .nowindowactivate
  1764.  
  1765.      mov   [window_minimize], 2   ; restore window if minimized
  1766.  
  1767.      movzx esi, word [WIN_STACK + ecx*2]
  1768.      cmp   esi, [TASK_COUNT]
  1769.      je    .nowindowactivate ; already active
  1770.  
  1771.      mov   edi, ecx
  1772.      shl   edi, 5
  1773.      add   edi, window_data
  1774.      movzx esi, word [WIN_STACK + ecx * 2]
  1775.      lea   esi, [WIN_POS + esi * 2]
  1776.      call  waredraw
  1777. .nowindowactivate:
  1778.      ret
  1779.  
  1780. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  1781.      mov  eax,[idleusesec]
  1782.      mov  [esp+32], eax
  1783.      ret
  1784.  
  1785. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  1786.      mov  eax,[CPU_FREQ]
  1787.      mov  [esp+32], eax
  1788.      ret
  1789.  
  1790. ;  SAVE ramdisk to /hd/1/menuet.img
  1791. ;!!!!!!!!!!!!!!!!!!!!!!!!
  1792.    include 'blkdev/rdsave.inc'
  1793. ;!!!!!!!!!!!!!!!!!!!!!!!!
  1794. align 4
  1795. sysfn_getactive:        ; 18.7 = get active window
  1796.      mov  eax, [TASK_COUNT]
  1797.    movzx  eax, word [WIN_POS + eax*2]
  1798.      mov  [esp+32],eax
  1799.      ret
  1800.  
  1801. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  1802. ;     cmp  ecx,1
  1803.      dec  ecx
  1804.      jnz  nogetsoundflag
  1805.      movzx  eax,byte [sound_flag] ; get sound_flag
  1806.      mov  [esp+32],eax
  1807.      ret
  1808.  nogetsoundflag:
  1809. ;     cmp  ecx,2
  1810.      dec  ecx
  1811.      jnz  nosoundflag
  1812.      xor  byte [sound_flag], 1
  1813.  nosoundflag:
  1814.      ret
  1815.  
  1816. sysfn_minimize:         ; 18.10 = minimize window
  1817.      mov   [window_minimize],1
  1818.      ret
  1819. align 4
  1820. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  1821. ;     cmp  ecx,1
  1822.      dec  ecx
  1823.      jnz  full_table
  1824.   small_table:
  1825.      call for_all_tables
  1826.      mov ecx,10
  1827.      cld
  1828.      rep movsb
  1829.      ret
  1830.    for_all_tables:
  1831.      mov edi,edx
  1832.      mov esi,DRIVE_DATA
  1833.      ret
  1834.   full_table:
  1835. ;     cmp  ecx,2
  1836.      dec  ecx
  1837.      jnz  exit_for_anyone
  1838.      call for_all_tables
  1839.      mov ecx,16384
  1840.      cld
  1841.      rep movsd
  1842.      ret
  1843.  
  1844. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  1845.         and     dword [esp+32], 0
  1846.         ret
  1847.  
  1848. sysfn_getversion:       ; 18.13 = get kernel ID and version
  1849.      mov edi,ecx
  1850.      mov esi,version_inf
  1851.      mov ecx,version_end-version_inf
  1852.      rep movsb
  1853.      ret
  1854.  
  1855. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  1856.      ;wait retrace functions
  1857.  sys_wait_retrace:
  1858.      mov edx,0x3da
  1859.  WaitRetrace_loop:
  1860.      in al,dx
  1861.      test al,1000b
  1862.      jz WaitRetrace_loop
  1863.      and [esp+32],dword 0
  1864.      ret
  1865.  
  1866. align 4
  1867. sysfn_centermouse:      ; 18.15 = mouse centered
  1868. ; removed here by <Lrz>
  1869. ;     call  mouse_centered
  1870. ;* mouse centered - start code- Mario79
  1871. ;mouse_centered:
  1872. ;        push  eax
  1873.         mov   eax,[Screen_Max_X]
  1874.         shr   eax,1
  1875.         mov   [MOUSE_X],ax
  1876.         mov   eax,[Screen_Max_Y]
  1877.         shr   eax,1
  1878.         mov   [MOUSE_Y],ax
  1879. ;        ret
  1880. ;* mouse centered - end code- Mario79
  1881.         xor   eax,eax
  1882.         and   [esp+32],eax
  1883. ;        pop   eax
  1884.  
  1885.      ret
  1886. align 4
  1887. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  1888.      test ecx,ecx  ; get mouse speed factor
  1889.      jnz  .set_mouse_acceleration
  1890.      xor  eax,eax
  1891.      mov  ax,[mouse_speed_factor]
  1892.      mov  [esp+32],eax
  1893.      ret
  1894.  .set_mouse_acceleration:
  1895. ;     cmp  ecx,1  ; set mouse speed factor
  1896.      dec  ecx
  1897.      jnz  .get_mouse_delay
  1898.      mov  [mouse_speed_factor],dx
  1899.      ret
  1900.  .get_mouse_delay:
  1901. ;     cmp  ecx,2  ; get mouse delay
  1902.      dec  ecx
  1903.      jnz  .set_mouse_delay
  1904.      mov  eax,[mouse_delay]
  1905.      mov  [esp+32],eax
  1906.      ret
  1907.  .set_mouse_delay:
  1908. ;     cmp  ecx,3  ; set mouse delay
  1909.      dec  ecx
  1910.      jnz  .set_pointer_position
  1911.      mov  [mouse_delay],edx
  1912.      ret
  1913.  .set_pointer_position:
  1914. ;     cmp  ecx,4  ; set mouse pointer position
  1915.      dec   ecx
  1916.      jnz  .set_mouse_button
  1917.      cmp   dx, word[Screen_Max_Y]
  1918.      ja    .end
  1919.  
  1920.      rol   edx,16
  1921.      cmp   dx, word[Screen_Max_X]
  1922.      ja    .end
  1923.      mov   [MOUSE_X], edx
  1924.      ret
  1925.  .set_mouse_button:
  1926. ;     cmp   ecx,5  ; set mouse button features
  1927.      dec   ecx
  1928.      jnz  .end
  1929.      mov   [BTN_DOWN],dl
  1930.      mov   [mouse_active],1
  1931.  .end:
  1932.      ret
  1933.  
  1934. sysfn_getfreemem:
  1935.      mov eax, [pg_data.pages_free]
  1936.      shl eax, 2
  1937.      mov [esp+32],eax
  1938.      ret
  1939.  
  1940. sysfn_getallmem:
  1941.      mov  eax,[MEM_AMOUNT]
  1942.      shr eax, 10
  1943.      mov  [esp+32],eax
  1944.      ret
  1945.  
  1946. ; // Alver, 2007-22-08 // {
  1947. sysfn_pid_to_slot:
  1948.      mov   eax, ecx
  1949.      call  pid_to_slot
  1950.      mov   [esp+32], eax
  1951.      ret
  1952.  
  1953. sysfn_min_rest_window:
  1954.      pushad
  1955.      mov   eax, edx      ; ebx - operating
  1956.      shr   ecx, 1
  1957.      jnc    @f
  1958.      call  pid_to_slot
  1959. @@:
  1960.      or    eax, eax      ; eax - number of slot
  1961.      jz    .error
  1962.      cmp   eax, 255         ; varify maximal slot number
  1963.      ja    .error
  1964.      movzx eax, word [WIN_STACK + eax*2]
  1965.      shr   ecx, 1
  1966.      jc    .restore
  1967.  ; .minimize:
  1968.      call  minimize_window
  1969.      jmp   .exit
  1970. .restore:
  1971.      call  restore_minimized_window
  1972. .exit:
  1973.      popad
  1974.      xor   eax, eax
  1975.      mov   [esp+32], eax
  1976.      ret
  1977. .error:
  1978.      popad
  1979.      xor   eax, eax
  1980.      dec   eax
  1981.      mov   [esp+32], eax
  1982.      ret
  1983. ; } \\ Alver, 2007-22-08 \\
  1984.  
  1985. uglobal
  1986. ;// mike.dld, 2006-29-01 [
  1987. screen_workarea RECT
  1988. ;// mike.dld, 2006-29-01 ]
  1989. window_minimize db 0
  1990. sound_flag      db 0
  1991. endg
  1992.  
  1993. iglobal
  1994. version_inf:
  1995.   db 0,7,7,0  ; version 0.7.7.0
  1996.   db 0          ;reserved
  1997.   dd __REV__
  1998. version_end:
  1999. endg
  2000.  
  2001. sys_cachetodiskette:
  2002.         cmp     ebx, 1
  2003.         jne     .no_floppy_a_save
  2004.         mov     [flp_number], 1
  2005.         jmp     .save_image_on_floppy
  2006. .no_floppy_a_save:
  2007.         cmp     ebx, 2
  2008.         jne     .no_floppy_b_save
  2009.         mov     [flp_number], 2
  2010. .save_image_on_floppy:
  2011.         call    save_image
  2012.         mov     [esp + 32], dword 0
  2013.         cmp     [FDC_Status], 0
  2014.         je      .yes_floppy_save
  2015. .no_floppy_b_save:
  2016.         mov     [esp + 32], dword 1
  2017. .yes_floppy_save:
  2018.         ret
  2019.  
  2020. uglobal
  2021. ;  bgrchanged  dd  0x0
  2022. align 4
  2023. bgrlockpid dd 0
  2024. bgrlock db 0
  2025. endg
  2026.  
  2027. ;===============================  SysFn 15  ================================
  2028. sys_background:
  2029.  
  2030.     cmp   ebx,1                            ; BACKGROUND SIZE
  2031.     jnz   nosb1
  2032.     test  ecx,ecx
  2033. ;    cmp   ecx,0
  2034.     jz    sbgrr
  2035.     test  edx,edx
  2036. ;    cmp   edx,0
  2037.     jz    sbgrr
  2038. @@:
  2039. ;;Maxis use atomic bts for mutexes  4.4.2009
  2040.         bts     dword [bgrlock], 0
  2041.         jnc     @f
  2042.         call    change_task
  2043.         jmp     @b
  2044. @@:
  2045.     mov   [BgrDataWidth],ecx
  2046.     mov   [BgrDataHeight],edx
  2047. ;    mov   [bgrchanged],1
  2048.  
  2049.     pushad
  2050. ; return memory for old background
  2051.         mov     eax, [img_background]
  2052.         cmp     eax, static_background_data
  2053.         jz      @f
  2054.         stdcall kernel_free, eax
  2055. @@:
  2056. ; calculate RAW size
  2057.     xor  eax,eax
  2058.     inc  eax
  2059.     cmp  [BgrDataWidth],eax
  2060.     jae   @f
  2061.     mov [BgrDataWidth],eax
  2062. @@:
  2063.     cmp  [BgrDataHeight],eax
  2064.     jae   @f
  2065.     mov [BgrDataHeight],eax
  2066. @@:
  2067.     mov  eax,[BgrDataWidth]
  2068.     imul eax,[BgrDataHeight]
  2069.     lea  eax,[eax*3]
  2070.     mov  [mem_BACKGROUND],eax
  2071. ; get memory for new background
  2072.     stdcall kernel_alloc, eax
  2073.     test eax, eax
  2074.     jz .memfailed
  2075.     mov [img_background], eax
  2076.     jmp .exit
  2077. .memfailed:
  2078. ; revert to static monotone data
  2079.         mov     [img_background], static_background_data
  2080.         xor     eax, eax
  2081.         inc     eax
  2082.         mov     [BgrDataWidth], eax
  2083.         mov     [BgrDataHeight], eax
  2084.         mov     [mem_BACKGROUND], 4
  2085. .exit:
  2086.     popad
  2087.         mov     [bgrlock], 0
  2088.  
  2089.   sbgrr:
  2090.     ret
  2091.  
  2092. nosb1:
  2093.  
  2094.     cmp   ebx,2                            ; SET PIXEL
  2095.     jnz   nosb2
  2096.  
  2097.     mov   eax, [img_background]
  2098.     test  ecx, ecx
  2099.     jz    @f
  2100.     cmp   eax, static_background_data
  2101.  
  2102.     jz    .ret
  2103. @@:
  2104.     mov ebx, [mem_BACKGROUND]
  2105.     add ebx, 4095
  2106.     and ebx, -4096
  2107.     sub ebx, 4
  2108.     cmp   ecx, ebx
  2109.     ja   .ret
  2110.  
  2111.     mov   ebx,[eax+ecx]
  2112.     and   ebx,0xFF000000 ;255*256*256*256
  2113.     and   edx,0x00FFFFFF ;255*256*256+255*256+255
  2114.     add   edx,ebx
  2115.     mov   [eax+ecx],edx
  2116. .ret:
  2117.     ret
  2118. nosb2:
  2119.  
  2120.     cmp   ebx,3                            ; DRAW BACKGROUND
  2121.     jnz   nosb3
  2122. draw_background_temp:
  2123. ;    cmp   [bgrchanged],1 ;0
  2124. ;    je    nosb31
  2125. ;draw_background_temp:
  2126. ;    mov   [bgrchanged],1 ;0
  2127.     mov    [background_defined], 1
  2128.     mov    byte[BACKGROUND_CHANGED], 1
  2129.     call  force_redraw_background
  2130.    nosb31:
  2131.     ret
  2132.   nosb3:
  2133.  
  2134.     cmp   ebx,4                            ; TILED / STRETCHED
  2135.     jnz   nosb4
  2136.     cmp   ecx,[BgrDrawMode]
  2137.     je    nosb41
  2138.     mov   [BgrDrawMode],ecx
  2139. ;    mov   [bgrchanged],1
  2140.    nosb41:
  2141.     ret
  2142.   nosb4:
  2143.  
  2144.     cmp   ebx,5                            ; BLOCK MOVE TO BGR
  2145.     jnz   nosb5
  2146.     cmp   [img_background], static_background_data
  2147.     jnz   @f
  2148.     test  edx, edx
  2149.     jnz   .fin
  2150.     cmp   esi, 4
  2151.     ja    .fin
  2152.   @@:
  2153.   ; bughere
  2154.     mov   eax, ecx
  2155.     mov   ebx, edx
  2156.     add   ebx, [img_background]   ;IMG_BACKGROUND
  2157.     mov   ecx, esi
  2158.     call  memmove
  2159.   .fin:
  2160.     ret
  2161.   nosb5:
  2162.  
  2163.         cmp     ebx, 6
  2164.         jnz     nosb6
  2165. ;;Maxis use atomic bts for mutex 4.4.2009
  2166. @@:
  2167.         bts     dword [bgrlock], 0
  2168.         jnc     @f
  2169.         call    change_task
  2170.         jmp     @b
  2171. @@:
  2172.         mov     eax, [CURRENT_TASK]
  2173.         mov     [bgrlockpid], eax
  2174.         cmp     [img_background], static_background_data
  2175.         jz      .nomem
  2176.         stdcall user_alloc, [mem_BACKGROUND]
  2177.         mov     [esp+32], eax
  2178.         test    eax, eax
  2179.         jz      .nomem
  2180.         mov     ebx, eax
  2181.         shr     ebx, 12
  2182.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2183.         mov     esi, [img_background]
  2184.         shr     esi, 12
  2185.         mov     ecx, [mem_BACKGROUND]
  2186.         add     ecx, 0xFFF
  2187.         shr     ecx, 12
  2188. .z:
  2189.         mov     eax, [page_tabs+ebx*4]
  2190.         test    al, 1
  2191.         jz      @f
  2192.         call    free_page
  2193. @@:
  2194.         mov     eax, [page_tabs+esi*4]
  2195.         or      al, PG_UW
  2196.         mov     [page_tabs+ebx*4], eax
  2197.         mov     eax, ebx
  2198.         shl     eax, 12
  2199.         invlpg  [eax]
  2200.         inc     ebx
  2201.         inc     esi
  2202.         loop    .z
  2203.         ret
  2204. .nomem:
  2205.         and     [bgrlockpid], 0
  2206.         mov     [bgrlock], 0
  2207. nosb6:
  2208.         cmp     ebx, 7
  2209.         jnz     nosb7
  2210.         cmp     [bgrlock], 0
  2211.         jz      .err
  2212.         mov     eax, [CURRENT_TASK]
  2213.         cmp     [bgrlockpid], eax
  2214.         jnz     .err
  2215.         mov     eax, ecx
  2216.         mov     ebx, ecx
  2217.         shr     eax, 12
  2218.         mov     ecx, [page_tabs+(eax-1)*4]
  2219.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2220.         jz      .err
  2221.         jnp     .err
  2222.         push    eax
  2223.         shr     ecx, 12
  2224.         dec     ecx
  2225. @@:
  2226.         and     dword [page_tabs+eax*4], 0
  2227.         mov     edx, eax
  2228.         shl     edx, 12
  2229.         push eax
  2230.         invlpg  [edx]
  2231.         pop eax
  2232.         inc     eax
  2233.         loop    @b
  2234.         pop     eax
  2235.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2236.         stdcall user_free, ebx
  2237.         mov     [esp+32], eax
  2238.         and     [bgrlockpid], 0
  2239.         mov     [bgrlock], 0
  2240.         ret
  2241. .err:
  2242.         and     dword [esp+32], 0
  2243.         ret
  2244.  
  2245. nosb7:
  2246.     ret
  2247.  
  2248. ;===============================  SysFn 39  ================================
  2249. align 4
  2250.  
  2251. sys_getbackground:
  2252.     dec   ebx
  2253.     jnz   nogb1
  2254.     mov   eax,[BgrDataWidth]
  2255.     shl   eax,16
  2256.     mov   ax,[BgrDataHeight]
  2257.     mov   [esp+32],eax
  2258.     ret
  2259.  
  2260. nogb1:
  2261. ;    cmp   eax,2                                  ; PIXEL
  2262.     dec   ebx
  2263.     jnz   nogb2
  2264.  
  2265.         mov     eax, [img_background]
  2266.         test    ecx, ecx
  2267.         jz      @f
  2268.         cmp     eax, static_background_data
  2269.         jz      .ret
  2270. @@:
  2271.     mov ebx, [mem_BACKGROUND]
  2272.     add ebx, 4095
  2273.     and ebx, -4096
  2274.     sub ebx, 4
  2275.     cmp ecx, ebx
  2276.     ja  .ret
  2277.  
  2278.     mov   eax,[ecx+eax]
  2279.  
  2280.     and   eax, 0xFFFFFF
  2281.     mov   [esp+32],eax
  2282. .ret:
  2283.     ret
  2284.   nogb2:
  2285.  
  2286. ;    cmp   eax,4                                  ; TILED / STRETCHED
  2287.     dec   ebx
  2288.     dec   ebx
  2289.     jnz   nogb4
  2290.     mov   eax,[BgrDrawMode]
  2291.   nogb4:
  2292.     mov   [esp+32],eax
  2293.     ret
  2294.  
  2295. ;===========================================
  2296.  
  2297. force_redraw_background:
  2298.     and   [draw_data+32 + RECT.left], 0
  2299.     and   [draw_data+32 + RECT.top], 0
  2300.     push  eax ebx
  2301.     mov   eax,[Screen_Max_X]
  2302.     mov   ebx,[Screen_Max_Y]
  2303.     mov   [draw_data+32 + RECT.right],eax
  2304.     mov   [draw_data+32 + RECT.bottom],ebx
  2305.     pop   ebx eax
  2306.     inc   byte[REDRAW_BACKGROUND]
  2307.     ret
  2308. ;===========================================
  2309.  
  2310.  
  2311. align 4
  2312.  
  2313. sys_getkey:
  2314.         mov     [esp + 32],dword 1
  2315.         ; test main buffer
  2316.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2317.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2318.         mov     edx, [TASK_COUNT]
  2319.         cmp     ecx, edx
  2320.         jne     .finish
  2321.         cmp     [KEY_COUNT], byte 0
  2322.         je      .finish
  2323.         movzx   eax, byte [KEY_BUFF]
  2324.         shl     eax, 8
  2325.         push    eax
  2326.         dec     byte [KEY_COUNT]
  2327.         and     byte [KEY_COUNT], 127
  2328.         movzx   ecx, byte [KEY_COUNT]
  2329.         add     ecx, 2
  2330.         mov     eax, KEY_BUFF + 1
  2331.         mov     ebx, KEY_BUFF
  2332.         call    memmove
  2333.         pop     eax
  2334. .ret_eax:
  2335.         mov     [esp + 32], eax
  2336.         ret
  2337. .finish:
  2338. ; test hotkeys buffer
  2339.         mov     ecx, hotkey_buffer
  2340. @@:
  2341.         cmp     [ecx], ebx
  2342.         jz      .found
  2343.         add     ecx, 8
  2344.         cmp     ecx, hotkey_buffer + 120 * 8
  2345.         jb      @b
  2346.         ret
  2347. .found:
  2348.         mov     ax, [ecx + 6]
  2349.         shl     eax, 16
  2350.         mov     ah, [ecx + 4]
  2351.         mov     al, 2
  2352.         and     dword [ecx + 4], 0
  2353.         and     dword [ecx], 0
  2354.         jmp     .ret_eax
  2355.  
  2356. align 4
  2357.  
  2358. sys_getbutton:
  2359.  
  2360.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2361.         mov     [esp + 32], dword 1
  2362.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2363.         mov     edx, [TASK_COUNT] ; less than 256 processes
  2364.         cmp     ecx, edx
  2365.         jne     .exit
  2366.         movzx   eax, byte [BTN_COUNT]
  2367.         test    eax, eax
  2368.         jz      .exit
  2369.         mov     eax, [BTN_BUFF]
  2370.         and     al, 0xFE                                    ; delete left button bit
  2371.         mov     [BTN_COUNT], byte 0
  2372.         mov     [esp + 32], eax
  2373. .exit:
  2374.         ret
  2375.  
  2376.  
  2377. align 4
  2378.  
  2379. sys_cpuusage:
  2380.  
  2381. ;  RETURN:
  2382. ;
  2383. ;  +00 dword     process cpu usage
  2384. ;  +04  word     position in windowing stack
  2385. ;  +06  word     windowing stack value at current position (cpu nro)
  2386. ;  +10 12 bytes  name
  2387. ;  +22 dword     start in mem
  2388. ;  +26 dword     used mem
  2389. ;  +30 dword     PID , process idenfification number
  2390. ;
  2391.  
  2392.     cmp  ecx,-1         ; who am I ?
  2393.     jne  .no_who_am_i
  2394.     mov  ecx,[CURRENT_TASK]
  2395.   .no_who_am_i:
  2396.         cmp     ecx, max_processes
  2397.         ja      .nofillbuf
  2398.  
  2399. ; +4: word: position of the window of thread in the window stack
  2400.         mov     ax, [WIN_STACK + ecx * 2]
  2401.         mov     [ebx+4], ax
  2402. ; +6: word: number of the thread slot, which window has in the window stack
  2403. ;           position ecx (has no relation to the specific thread)
  2404.         mov     ax, [WIN_POS + ecx * 2]
  2405.         mov     [ebx+6], ax
  2406.  
  2407.         shl     ecx, 5
  2408.  
  2409. ; +0: dword: memory usage
  2410.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  2411.         mov     [ebx], eax
  2412. ; +10: 11 bytes: name of the process
  2413.         push    ecx
  2414.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  2415.         add     ebx, 10
  2416.         mov     ecx, 11
  2417.         call    memmove
  2418.         pop     ecx
  2419.  
  2420. ; +22: address of the process in memory
  2421. ; +26: size of used memory - 1
  2422.         push    edi
  2423.         lea     edi, [ebx+12]
  2424.         xor     eax, eax
  2425.         mov     edx, 0x100000*16
  2426.         cmp     ecx, 1 shl 5
  2427.         je      .os_mem
  2428.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
  2429.         mov     eax, std_application_base_address
  2430. .os_mem:
  2431.         stosd
  2432.         lea     eax, [edx-1]
  2433.         stosd
  2434.  
  2435. ; +30: PID/TID
  2436.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  2437.         stosd
  2438.  
  2439.     ; window position and size
  2440.         push    esi
  2441.         lea     esi, [ecx + window_data + WDATA.box]
  2442.         movsd
  2443.         movsd
  2444.         movsd
  2445.         movsd
  2446.  
  2447.     ; Process state (+50)
  2448.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  2449.         stosd
  2450.  
  2451.     ; Window client area box
  2452.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2453.         movsd
  2454.         movsd
  2455.         movsd
  2456.         movsd
  2457.  
  2458.     ; Window state
  2459.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2460.         stosb
  2461.  
  2462.     ; Event mask (+71)
  2463.         mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
  2464.         stosd
  2465.  
  2466.         pop     esi
  2467.         pop     edi
  2468.  
  2469. .nofillbuf:
  2470.     ; return number of processes
  2471.  
  2472.     mov    eax,[TASK_COUNT]
  2473.     mov    [esp+32],eax
  2474.     ret
  2475.  
  2476. align 4
  2477. sys_clock:
  2478.         cli
  2479.   ; Mikhail Lisovin  xx Jan 2005
  2480.   @@:   mov   al, 10
  2481.         out   0x70, al
  2482.         in    al, 0x71
  2483.         test  al, al
  2484.         jns   @f
  2485.         mov   esi, 1
  2486.         call  delay_ms
  2487.         jmp   @b
  2488.   @@:
  2489.   ; end Lisovin's fix
  2490.  
  2491.         xor   al,al           ; seconds
  2492.         out   0x70,al
  2493.         in    al,0x71
  2494.         movzx ecx,al
  2495.         mov   al,02           ; minutes
  2496.         shl   ecx,16
  2497.         out   0x70,al
  2498.         in    al,0x71
  2499.         movzx edx,al
  2500.         mov   al,04           ; hours
  2501.         shl   edx,8
  2502.         out   0x70,al
  2503.         in    al,0x71
  2504.         add   ecx,edx
  2505.         movzx edx,al
  2506.         add   ecx,edx
  2507.         sti
  2508.         mov     [esp + 32], ecx
  2509.         ret
  2510.  
  2511.  
  2512. align 4
  2513.  
  2514. sys_date:
  2515.  
  2516.         cli
  2517.   @@:   mov   al, 10
  2518.         out   0x70, al
  2519.         in    al, 0x71
  2520.         test  al, al
  2521.         jns   @f
  2522.         mov   esi, 1
  2523.         call  delay_ms
  2524.         jmp   @b
  2525.   @@:
  2526.  
  2527.         mov     ch,0
  2528.         mov     al,7            ; date
  2529.         out     0x70,al
  2530.         in      al,0x71
  2531.         mov     cl,al
  2532.         mov     al,8            ; month
  2533.         shl     ecx,16
  2534.         out     0x70,al
  2535.         in      al,0x71
  2536.         mov     ch,al
  2537.         mov     al,9            ; year
  2538.         out     0x70,al
  2539.         in      al,0x71
  2540.         mov     cl,al
  2541.         sti
  2542.         mov     [esp+32], ecx
  2543.         ret
  2544.  
  2545.  
  2546. ; redraw status
  2547.  
  2548. sys_redrawstat:
  2549.         cmp     ebx, 1
  2550.         jne     no_widgets_away
  2551.         ; buttons away
  2552.         mov     ecx,[CURRENT_TASK]
  2553.   sys_newba2:
  2554.         mov     edi,[BTN_ADDR]
  2555.         cmp     [edi], dword 0  ; empty button list ?
  2556.         je      end_of_buttons_away
  2557.         movzx   ebx, word [edi]
  2558.         inc     ebx
  2559.         mov     eax,edi
  2560.   sys_newba:
  2561.         dec     ebx
  2562.         jz      end_of_buttons_away
  2563.  
  2564.         add     eax, 0x10
  2565.         cmp     cx, [eax]
  2566.         jnz     sys_newba
  2567.  
  2568.         push    eax ebx ecx
  2569.         mov     ecx,ebx
  2570.         inc     ecx
  2571.         shl     ecx, 4
  2572.         mov     ebx, eax
  2573.         add     eax, 0x10
  2574.         call    memmove
  2575.         dec     dword [edi]
  2576.         pop     ecx ebx eax
  2577.  
  2578.         jmp     sys_newba2
  2579.  
  2580.   end_of_buttons_away:
  2581.  
  2582.         ret
  2583.  
  2584.   no_widgets_away:
  2585.  
  2586.         cmp     ebx, 2
  2587.         jnz     srl1
  2588.  
  2589.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2590.         add     edx, draw_data - CURRENT_TASK
  2591.         mov     [edx + RECT.left], 0
  2592.         mov     [edx + RECT.top], 0
  2593.         mov     eax, [Screen_Max_X]
  2594.  
  2595.         mov     [edx + RECT.right], eax
  2596.         mov     eax, [Screen_Max_Y]
  2597.         mov     [edx + RECT.bottom], eax
  2598.  
  2599.   srl1:
  2600.         ret
  2601.  
  2602. ;ok - 100% work
  2603. ;nt - not tested
  2604. ;---------------------------------------------------------------------------------------------
  2605. ;eax
  2606. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2607. ;1 - change task. Ret nothing. Block. ok.
  2608. ;2 - performance control
  2609. ; ebx
  2610. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2611. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2612. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2613. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2614. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2615. ;eax
  2616. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2617. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2618. ;---------------------------------------------------------------------------------------------
  2619. iglobal
  2620. align 4
  2621. sheduler:
  2622.         dd      sys_sheduler.00
  2623.         dd      change_task
  2624.         dd      sys_sheduler.02
  2625.         dd      sys_sheduler.03
  2626.         dd      sys_sheduler.04
  2627. endg
  2628. sys_sheduler:
  2629. ;rewritten by <Lrz>  29.12.2009
  2630.         jmp     dword [sheduler+ebx*4]
  2631. ;.shed_counter:
  2632. .00:
  2633.         mov eax,[context_counter]
  2634.         mov [esp+32],eax
  2635.         ret
  2636.  
  2637. .02:
  2638. ;.perf_control:
  2639.         inc     ebx                     ;before ebx=2, ebx=3
  2640.         cmp     ebx,ecx                 ;if ecx=3, ebx=3
  2641.         jz      cache_disable
  2642.  
  2643.         dec     ebx                     ;ebx=2
  2644.         cmp     ebx,ecx                 ;
  2645.         jz      cache_enable            ;if ecx=2 and ebx=2
  2646.  
  2647.         dec     ebx                     ;ebx=1
  2648.         cmp     ebx,ecx
  2649.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  2650.  
  2651.         dec     ebx
  2652.         test    ebx,ecx                 ;ebx=0 and ecx=0
  2653.         jz      modify_pce              ;if ecx=0
  2654.  
  2655.         ret
  2656.  
  2657. .03:
  2658. ;.rdmsr_instr:
  2659. ;now counter in ecx
  2660. ;(edx:eax) esi:edi => edx:esi
  2661.         mov     eax,esi
  2662.         mov     ecx,edx
  2663.         rdmsr
  2664.         mov     [esp+32],eax
  2665.         mov     [esp+20],edx            ;ret in ebx?
  2666.         ret
  2667.  
  2668. .04:
  2669. ;.wrmsr_instr:
  2670. ;now counter in ecx
  2671. ;(edx:eax) esi:edi => edx:esi
  2672.         ; Fast Call MSR can't be destroy
  2673.         ; ® MSR_AMD_EFER ¬®¦­® ¨§¬¥­ïâì, â.ª. ¢ í⮬ ॣ¨áâॠ«¨è
  2674.         ; ¢ª«îç îâáï/¢ëª«îç îâáï à áè¨à¥­­ë¥ ¢®§¬®¦­®áâ¨
  2675.         cmp     edx,MSR_SYSENTER_CS
  2676.         je      @f
  2677.         cmp     edx,MSR_SYSENTER_ESP
  2678.         je      @f
  2679.         cmp     edx,MSR_SYSENTER_EIP
  2680.         je      @f
  2681.         cmp     edx,MSR_AMD_STAR
  2682.         je      @f
  2683.  
  2684.         mov     eax,esi
  2685.         mov     ecx,edx
  2686.         wrmsr
  2687.         ; mov   [esp + 32], eax
  2688.         ; mov   [esp + 20], edx ;ret in ebx?
  2689. @@:
  2690.         ret
  2691.  
  2692. cache_disable:
  2693.        mov eax,cr0
  2694.        or  eax,01100000000000000000000000000000b
  2695.        mov cr0,eax
  2696.        wbinvd ;set MESI
  2697. ret
  2698.  
  2699. cache_enable:
  2700.        mov eax,cr0
  2701.        and eax,10011111111111111111111111111111b
  2702.        mov cr0,eax
  2703. ret
  2704.  
  2705. is_cache_enabled:
  2706.        mov eax,cr0
  2707.        mov ebx,eax
  2708.        and eax,01100000000000000000000000000000b
  2709.        jz cache_disabled
  2710.        mov [esp+32],ebx
  2711. cache_disabled:
  2712.        mov dword [esp+32],eax ;0
  2713. ret
  2714.  
  2715. modify_pce:
  2716.        mov eax,cr4
  2717. ;       mov ebx,0
  2718. ;       or  bx,100000000b ;pce
  2719. ;       xor eax,ebx ;invert pce
  2720.        bts eax,8 ;pce=cr4[8]
  2721.        mov cr4,eax
  2722.        mov [esp+32],eax
  2723. ret
  2724. ;---------------------------------------------------------------------------------------------
  2725.  
  2726.  
  2727. iglobal
  2728.   cpustring db 'CPU',0
  2729. endg
  2730.  
  2731. uglobal
  2732. background_defined    db    0    ; diamond, 11.04.2006
  2733. endg
  2734.  
  2735. align 4
  2736. ; check misc
  2737.  
  2738. checkmisc:
  2739.  
  2740.     cmp   [ctrl_alt_del], 1
  2741.     jne   nocpustart
  2742.  
  2743.         mov     ebp, cpustring
  2744.         call    fs_execute_from_sysdir
  2745.  
  2746.     mov   [ctrl_alt_del], 0
  2747.  
  2748. nocpustart:
  2749.     cmp   [mouse_active], 1
  2750.     jne   mouse_not_active
  2751.     mov   [mouse_active], 0
  2752.     xor   edi, edi
  2753.     mov   ecx,  [TASK_COUNT]
  2754. set_mouse_event:
  2755.     add   edi, 256
  2756.     or    [edi+SLOT_BASE+APPDATA.event_mask], dword 100000b
  2757.     loop  set_mouse_event
  2758.  
  2759. mouse_not_active:
  2760.     cmp   byte[BACKGROUND_CHANGED], 0
  2761.     jz    no_set_bgr_event
  2762.     xor   edi, edi
  2763.     mov   ecx, [TASK_COUNT]
  2764. set_bgr_event:
  2765.     add   edi, 256
  2766.     or    [edi+SLOT_BASE+APPDATA.event_mask], 16
  2767.     loop  set_bgr_event
  2768.     mov   byte[BACKGROUND_CHANGED], 0
  2769. no_set_bgr_event:
  2770.     cmp   byte[REDRAW_BACKGROUND], 0               ; background update ?
  2771.     jz    nobackgr
  2772.     cmp    [background_defined], 0
  2773.     jz    nobackgr
  2774. @@:
  2775.     call  drawbackground
  2776.     xor   eax, eax
  2777.     xchg  al, [REDRAW_BACKGROUND]
  2778.     test  al, al                                   ; got new update request?
  2779.     jnz   @b
  2780.     mov   [draw_data+32 + RECT.left], eax
  2781.     mov   [draw_data+32 + RECT.top], eax
  2782.     mov   [draw_data+32 + RECT.right], eax
  2783.     mov   [draw_data+32 + RECT.bottom], eax
  2784.     mov   [MOUSE_BACKGROUND],byte 0
  2785.  
  2786. nobackgr:
  2787.  
  2788.     ; system shutdown request
  2789.  
  2790.     cmp  [SYS_SHUTDOWN],byte 0
  2791.     je   noshutdown
  2792.  
  2793.     mov  edx,[shutdown_processes]
  2794.  
  2795.     cmp  [SYS_SHUTDOWN],dl
  2796.     jne  no_mark_system_shutdown
  2797.  
  2798.     lea   ecx,[edx-1]
  2799.     mov   edx,OS_BASE+0x3040
  2800.     jecxz @f
  2801. markz:
  2802.     mov   [edx+TASKDATA.state],byte 3
  2803.     add   edx,0x20
  2804.     loop  markz
  2805. @@:
  2806.  
  2807.   no_mark_system_shutdown:
  2808.  
  2809.     call [_display.disable_mouse]
  2810.  
  2811.     dec  byte [SYS_SHUTDOWN]
  2812.     je   system_shutdown
  2813.  
  2814. noshutdown:
  2815.     mov   eax,[TASK_COUNT]                  ; termination
  2816.     mov   ebx,TASK_DATA+TASKDATA.state
  2817.     mov   esi,1
  2818.  
  2819. newct:
  2820.     mov   cl,[ebx]
  2821.     cmp   cl,byte 3
  2822.     jz    terminate
  2823.     cmp   cl,byte 4
  2824.     jz    terminate
  2825.  
  2826.     add   ebx,0x20
  2827.     inc   esi
  2828.     dec   eax
  2829.     jnz   newct
  2830.     ret
  2831.  
  2832. ; redraw screen
  2833. ; eax , if process window_data base is eax, do not set flag/limits
  2834.  
  2835. redrawscreen:
  2836.          pushad
  2837.          push  eax
  2838.  
  2839.          xor   ecx,ecx                  ; redraw flags for apps
  2840.        newdw2:
  2841.  
  2842.          inc   ecx
  2843.          push  ecx
  2844.  
  2845.          mov   eax,ecx
  2846.          shl   eax,5
  2847.          add   eax,window_data
  2848.  
  2849.          cmp   eax,[esp+4]
  2850.          je    not_this_task
  2851.                                    ; check if window in redraw area
  2852.          mov   edi,eax
  2853.  
  2854.          cmp   ecx,1               ; limit for background
  2855.          jz    bgli
  2856.  
  2857.          mov   eax, [edi + WDATA.box.left]
  2858.          mov   ebx, [edi + WDATA.box.top]
  2859.          mov   ecx, [edi + WDATA.box.width]
  2860.          mov   edx, [edi + WDATA.box.height]
  2861.          add   ecx,eax
  2862.          add   edx,ebx
  2863.  
  2864.          mov   ecx,[draw_limits.bottom]   ; ecx = area y end     ebx = window y start
  2865.          cmp   ecx,ebx
  2866.          jb    ricino
  2867.  
  2868.          mov   ecx,[draw_limits.right]   ; ecx = area x end     eax = window x start
  2869.          cmp   ecx,eax
  2870.          jb    ricino
  2871.  
  2872.          mov   eax, [edi + WDATA.box.left]
  2873.          mov   ebx, [edi + WDATA.box.top]
  2874.          mov   ecx, [edi + WDATA.box.width]
  2875.          mov   edx, [edi + WDATA.box.height]
  2876.          add   ecx, eax
  2877.          add   edx, ebx
  2878.  
  2879.          mov   eax,[draw_limits.top]    ; eax = area y start     edx = window y end
  2880.          cmp   edx,eax
  2881.          jb    ricino
  2882.  
  2883.          mov   eax,[draw_limits.left]    ; eax = area x start     ecx = window x end
  2884.          cmp   ecx,eax
  2885.          jb    ricino
  2886.  
  2887.         bgli:
  2888.  
  2889.          cmp   dword[esp], 1
  2890.          jnz   .az
  2891.          cmp   byte[REDRAW_BACKGROUND], 0
  2892.          jz    .az
  2893.          mov   dl, 0
  2894.          lea   eax,[edi+draw_data-window_data]
  2895.          mov   ebx,[draw_limits.left]
  2896.          cmp   ebx,[eax+RECT.left]
  2897.          jae   @f
  2898.          mov   [eax+RECT.left],ebx
  2899.          mov   dl, 1
  2900.         @@:
  2901.          mov   ebx,[draw_limits.top]
  2902.          cmp   ebx,[eax+RECT.top]
  2903.          jae   @f
  2904.          mov   [eax+RECT.top],ebx
  2905.          mov   dl, 1
  2906.         @@:
  2907.          mov   ebx,[draw_limits.right]
  2908.          cmp   ebx,[eax+RECT.right]
  2909.          jbe   @f
  2910.          mov   [eax+RECT.right],ebx
  2911.          mov   dl, 1
  2912.         @@:
  2913.          mov   ebx,[draw_limits.bottom]
  2914.          cmp   ebx,[eax+RECT.bottom]
  2915.          jbe   @f
  2916.          mov   [eax+RECT.bottom],ebx
  2917.          mov   dl, 1
  2918.         @@:
  2919.          add   byte[REDRAW_BACKGROUND], dl
  2920.          jmp   newdw8
  2921.         .az:
  2922.  
  2923.          mov   eax,edi
  2924.          add   eax,draw_data-window_data
  2925.  
  2926.          mov   ebx,[draw_limits.left]          ; set limits
  2927.          mov   [eax + RECT.left], ebx
  2928.          mov   ebx,[draw_limits.top]
  2929.          mov   [eax + RECT.top], ebx
  2930.          mov   ebx,[draw_limits.right]
  2931.          mov   [eax + RECT.right], ebx
  2932.          mov   ebx,[draw_limits.bottom]
  2933.          mov   [eax + RECT.bottom], ebx
  2934.  
  2935.          sub   eax,draw_data-window_data
  2936.  
  2937.          cmp   dword [esp],1
  2938.          jne   nobgrd
  2939.          inc   byte[REDRAW_BACKGROUND]
  2940.  
  2941.  
  2942.        newdw8:
  2943.        nobgrd:
  2944.  
  2945.          mov   [eax + WDATA.fl_redraw],byte 1    ; mark as redraw
  2946.  
  2947.        ricino:
  2948.  
  2949.        not_this_task:
  2950.  
  2951.          pop   ecx
  2952.  
  2953.          cmp   ecx,[TASK_COUNT]
  2954.          jle   newdw2
  2955.  
  2956.          pop  eax
  2957.          popad
  2958.  
  2959.          ret
  2960.  
  2961. calculatebackground:   ; background
  2962.         mov   edi, [_WinMapAddress]                ; set os to use all pixels
  2963.         mov   eax, 0x01010101
  2964.         mov   ecx, [_WinMapSize]
  2965.         shr   ecx, 2
  2966.         rep   stosd
  2967.  
  2968.         mov   byte[REDRAW_BACKGROUND], 0              ; do not draw background!
  2969.         mov   byte[BACKGROUND_CHANGED], 0
  2970.  
  2971.         ret
  2972.  
  2973. uglobal
  2974.   imax    dd 0x0
  2975. endg
  2976.  
  2977. delay_ms:     ; delay in 1/1000 sec
  2978.         push  eax
  2979.         push  ecx
  2980.  
  2981.         mov   ecx,esi
  2982.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  2983.         imul  ecx, 33941
  2984.         shr   ecx, 9
  2985.         ; </CPU clock fix>
  2986.  
  2987.         in    al,0x61
  2988.         and   al,0x10
  2989.         mov   ah,al
  2990.         cld
  2991.  
  2992.  cnt1:  in    al,0x61
  2993.         and   al,0x10
  2994.         cmp   al,ah
  2995.         jz    cnt1
  2996.  
  2997.         mov   ah,al
  2998.         loop  cnt1
  2999.  
  3000.         pop   ecx
  3001.         pop   eax
  3002.  
  3003.         ret
  3004.  
  3005. set_app_param:
  3006.         mov     edi, [TASK_BASE]
  3007.         mov     eax, [edi + TASKDATA.event_mask]
  3008.         mov     [edi + TASKDATA.event_mask], ebx
  3009.         mov     [esp+32], eax
  3010.         ret
  3011.  
  3012. delay_hs:     ; delay in 1/100 secs
  3013. ; ebx = delay time
  3014.         push  ecx
  3015.         push  edx
  3016.  
  3017.         mov   edx,[timer_ticks]
  3018.  
  3019.       newtic:
  3020.         mov   ecx,[timer_ticks]
  3021.         sub   ecx,edx
  3022.         cmp   ecx,ebx
  3023.         jae   zerodelay
  3024.  
  3025.         call  change_task
  3026.  
  3027.         jmp   newtic
  3028.  
  3029.       zerodelay:
  3030.         pop   edx
  3031.         pop   ecx
  3032.  
  3033.         ret
  3034.  
  3035. align 16        ;very often call this subrutine
  3036. memmove:       ; memory move in bytes
  3037. ; eax = from
  3038. ; ebx = to
  3039. ; ecx = no of bytes
  3040.     test ecx, ecx
  3041.     jle  .ret
  3042.  
  3043.     push esi edi ecx
  3044.  
  3045.     mov  edi, ebx
  3046.     mov  esi, eax
  3047.  
  3048.     test ecx, not 11b
  3049.     jz   @f
  3050.  
  3051.     push ecx
  3052.     shr  ecx, 2
  3053.     rep  movsd
  3054.     pop  ecx
  3055.     and  ecx, 11b
  3056.     jz   .finish
  3057.   @@:
  3058.     rep  movsb
  3059.  
  3060.   .finish:
  3061.     pop  ecx edi esi
  3062.   .ret:
  3063.     ret
  3064.  
  3065.  
  3066. align 4
  3067.  
  3068. sys_programirq:
  3069.         ; removed
  3070.     mov   dword [esp+32], 1     ; operation failed
  3071.     ret
  3072.  
  3073.  
  3074. align 4
  3075.  
  3076. get_irq_data:
  3077.         ; removed
  3078.      mov   dword [esp+32], -1
  3079.      ret
  3080.  
  3081.  
  3082. set_io_access_rights:
  3083.         ;removed
  3084.      ret
  3085.  
  3086. r_f_port_area:
  3087.         ; removed; always returns 0
  3088.      xor   eax, eax
  3089.      ret
  3090.  
  3091.  
  3092. reserve_free_irq:
  3093.      xor   esi, esi
  3094.      inc   esi
  3095.      cmp   ecx, 16
  3096.      jae   ril1
  3097.  
  3098.      push  ecx
  3099.      lea   ecx, [irq_owner + 4 * ecx]
  3100.      mov   edx, [ecx]
  3101.      mov   eax, [TASK_BASE]
  3102.      mov   edi, [eax + TASKDATA.pid]
  3103.      pop   eax
  3104.      dec   ebx
  3105.      jnz   reserve_irq
  3106.  
  3107.      cmp   edx, edi
  3108.      jne   ril1
  3109.      dec   esi
  3110.      mov   [ecx], esi
  3111.  
  3112.      jmp   ril1
  3113.  
  3114.   reserve_irq:
  3115.      cmp   dword [ecx], 0
  3116.      jne   ril1
  3117.  
  3118.      mov   ebx, [f_irqs + 4 * eax]
  3119.  
  3120.      stdcall attach_int_handler, eax, ebx, dword 0
  3121.  
  3122.      mov   [ecx], edi
  3123.  
  3124.      dec   esi
  3125.    ril1:
  3126.      mov   [esp+32], esi ; return in eax
  3127.      ret
  3128.  
  3129. iglobal
  3130. f_irqs:
  3131.      dd 0x0
  3132.      dd 0x0
  3133.      dd p_irq2
  3134.      dd p_irq3
  3135.      dd p_irq4
  3136.      dd p_irq5
  3137.      dd p_irq6
  3138.      dd p_irq7
  3139.      dd p_irq8
  3140.      dd p_irq9
  3141.      dd p_irq10
  3142.      dd p_irq11
  3143.      dd 0x0
  3144.      dd 0x0
  3145.      dd p_irq14
  3146.      dd p_irq15
  3147.  
  3148. endg
  3149.  
  3150. drawbackground:
  3151. ;       inc   [mouse_pause]
  3152. ;       call  draw_background    ; graph32.inc
  3153. ;       dec   [mouse_pause]
  3154. ;       call   [draw_pointer]
  3155. ;       ret
  3156.        inc   [mouse_pause]
  3157.        cmp   [BgrDrawMode],dword 1
  3158.        jne   .bgrstr
  3159.        call  vesa20_drawbackground_tiled
  3160.          jmp  @f
  3161. .bgrstr:
  3162.        call  vesa20_drawbackground_stretch
  3163. @@:
  3164.        dec   [mouse_pause]
  3165.        call   [draw_pointer]
  3166.        ret
  3167.  
  3168. ; ====================================================================
  3169. if 0
  3170. ; the new GFX sys
  3171.  
  3172. align 4
  3173. syscall_putimage:                       ; PutImage = SysFn07
  3174. sys_putimage:
  3175.      test  ecx,0x80008000               ; ecx = { SizeX | SizeY }
  3176.      jnz   .exit                        ; edx = { OrigX | OrigY }
  3177.      test  ecx,0x0000FFFF               ; ebx points to the 24bpp-image
  3178.      jz    .exit
  3179.      test  ecx,0xFFFF0000
  3180.      jnz   @f
  3181.   .exit:
  3182.      ret
  3183.  @@:
  3184.         push    edi
  3185.         mov     edi,[current_slot]
  3186.         add     dx,word[edi+APPDATA.wnd_clientbox.top]
  3187.         rol     edx,16
  3188.         add     dx,word[edi+APPDATA.wnd_clientbox.left]
  3189.         pop     edi
  3190.         rol     edx,16
  3191.   .forced:                              ; called from gui/skincode.inc [215]
  3192.         push    esi
  3193.         mov     esi, ecx
  3194.         shr     esi, 16                 ; SizeX
  3195.         lea     esi, [esi*2+esi]        ; 3 bytes per pixel
  3196.         mov     [img_buf_line_size], esi
  3197.         mov     [img_draw_core_fn], draw_core_24bpp
  3198.         mov     [img_draw_edge_fn], draw_edge_24bpp
  3199.         mov     [img_bytes_per_pix], 3
  3200.         pop     esi
  3201.  
  3202. sys_putimage_bpp:                       ; only called from sys_putimage_palette
  3203.         inc     [mouse_pause]
  3204.         call    _putimage
  3205.         dec     [mouse_pause]
  3206.         call    [draw_pointer]
  3207.         ret
  3208.  
  3209.  
  3210. align 4
  3211. sys_putimage_palette:                   ; sysFn 65
  3212. ; ebx = pointer to image
  3213. ; ecx = [xsize]*65536 + [ysize]
  3214. ; edx = [xstart]*65536 + [ystart]
  3215. ; esi = number of bits per pixel, must be 1, 8, 24 or 32
  3216. ; edi = pointer to palette
  3217. ; ebp = line offset
  3218.         mov     eax, [CURRENT_TASK]
  3219.         shl     eax, 8
  3220.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  3221.         rol     edx, 16
  3222.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  3223.         rol     edx, 16
  3224. .forced:
  3225.         push    eax
  3226.         push    esi
  3227.         mov     [img_palette], edi
  3228.         mov     eax, esi
  3229.         cmp     eax, 32                 ;>32bpp (stupid call)
  3230.         ja      .exit
  3231.         shr     al, 3                   ; 0=1bpp or solid color
  3232.         mov     [img_bytes_per_pix], eax
  3233.         mov     esi, [eax*4 + img_core_proc_0]
  3234.         mov     [img_draw_core_fn], esi
  3235.         mov     esi, [eax*4 + img_edge_proc_0]
  3236.         mov     [img_draw_edge_fn], esi
  3237.         mov     esi, ecx
  3238.         shr     esi, 16                 ; esi = SizeX
  3239.         imul    esi, eax
  3240.         or      al, al
  3241.         jnz     .done
  3242.         mov     eax, [esp]              ; bits per pixel
  3243.         or      al, al
  3244.         jz      .done
  3245. .1bpp:
  3246.         add     esi, 7
  3247.         shr     esi, 3                  ; 8 pixels per byte
  3248.         mov     [img_draw_edge_fn], draw_edge_1bpp
  3249.         mov     [img_draw_core_fn], draw_core_1bpp
  3250. .done:
  3251.         add     esi, ebp                ; + line offset
  3252.         mov     [img_buf_line_size], esi
  3253.         pop     esi
  3254.         pop     eax
  3255.         jmp     sys_putimage_bpp
  3256. .exit:
  3257.         ret
  3258.  
  3259. align 4
  3260. img_core_proc_0         dd      draw_core_0bpp
  3261. img_core_proc_1         dd      draw_core_8bpp
  3262. img_core_proc_2         dd      draw_core_16bpp
  3263. img_core_proc_3         dd      draw_core_24bpp
  3264. img_core_proc_4         dd      draw_core_32bpp
  3265.  
  3266. img_edge_proc_0         dd      draw_edge_0bpp
  3267. img_edge_proc_1         dd      draw_edge_8bpp
  3268. img_edge_proc_2         dd      draw_edge_16bpp
  3269. img_edge_proc_3         dd      draw_edge_24bpp
  3270. img_edge_proc_4         dd      draw_edge_32bpp
  3271.  
  3272. end if
  3273. ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  3274. ; the old GFX sys
  3275. align 4
  3276.  
  3277. syscall_putimage:                       ; PutImage
  3278. sys_putimage:
  3279.      test  ecx,0x80008000
  3280.      jnz   .exit
  3281.      test  ecx,0x0000FFFF
  3282.      jz    .exit
  3283.      test  ecx,0xFFFF0000
  3284.      jnz   @f
  3285.   .exit:
  3286.      ret
  3287.  @@:
  3288.         mov     edi,[current_slot]
  3289.         add     dx,word[edi+APPDATA.wnd_clientbox.top]
  3290.         rol     edx,16
  3291.         add     dx,word[edi+APPDATA.wnd_clientbox.left]
  3292.         rol     edx,16
  3293.   .forced:
  3294.         push    ebp esi 0
  3295.         mov     ebp, putimage_get24bpp
  3296.         mov     esi, putimage_init24bpp
  3297. sys_putimage_bpp:
  3298. ;        cmp     [SCR_MODE], word 0x12
  3299. ;        jz      @f   ;.doit
  3300. ;        mov     eax, vesa12_putimage
  3301. ;        cmp     [SCR_MODE], word 0100000000000000b
  3302. ;        jae     @f
  3303. ;        cmp     [SCR_MODE], word 0x13
  3304. ;        jnz     .doit
  3305. ;@@:
  3306.         mov     eax, vesa20_putimage
  3307. .doit:
  3308.         inc     [mouse_pause]
  3309.         call    eax
  3310.         dec     [mouse_pause]
  3311.         pop     ebp esi ebp
  3312.         jmp     [draw_pointer]
  3313. align 4
  3314. sys_putimage_palette:
  3315. ; ebx = pointer to image
  3316. ; ecx = [xsize]*65536 + [ysize]
  3317. ; edx = [xstart]*65536 + [ystart]
  3318. ; esi = number of bits per pixel, must be 8, 24 or 32
  3319. ; edi = pointer to palette
  3320. ; ebp = row delta
  3321.         mov     eax, [CURRENT_TASK]
  3322.         shl     eax, 8
  3323.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  3324.         rol     edx, 16
  3325.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  3326.         rol     edx, 16
  3327. .forced:
  3328.         cmp     esi, 1
  3329.         jnz     @f
  3330.         push    edi
  3331.         mov     eax, [edi+4]
  3332.         sub     eax, [edi]
  3333.         push    eax
  3334.         push    dword [edi]
  3335.         push    0ffffff80h
  3336.         mov     edi, esp
  3337.         call    put_mono_image
  3338.         add     esp, 12
  3339.         pop     edi
  3340.         ret
  3341. @@:
  3342.         cmp     esi, 2
  3343.         jnz     @f
  3344.         push    edi
  3345.         push    0ffffff80h
  3346.         mov     edi, esp
  3347.         call    put_2bit_image
  3348.         pop     eax
  3349.         pop     edi
  3350.         ret
  3351. @@:
  3352.         cmp     esi, 4
  3353.         jnz     @f
  3354.         push    edi
  3355.         push    0ffffff80h
  3356.         mov     edi, esp
  3357.         call    put_4bit_image
  3358.         pop     eax
  3359.         pop     edi
  3360.         ret
  3361. @@:
  3362.         push    ebp esi ebp
  3363.         cmp     esi, 8
  3364.         jnz     @f
  3365.         mov     ebp, putimage_get8bpp
  3366.         mov     esi, putimage_init8bpp
  3367.         jmp     sys_putimage_bpp
  3368. @@:
  3369.         cmp     esi, 15
  3370.         jnz     @f
  3371.         mov     ebp, putimage_get15bpp
  3372.         mov     esi, putimage_init15bpp
  3373.         jmp     sys_putimage_bpp
  3374. @@:
  3375.         cmp     esi, 16
  3376.         jnz     @f
  3377.         mov     ebp, putimage_get16bpp
  3378.         mov     esi, putimage_init16bpp
  3379.         jmp     sys_putimage_bpp
  3380. @@:
  3381.         cmp     esi, 24
  3382.         jnz     @f
  3383.         mov     ebp, putimage_get24bpp
  3384.         mov     esi, putimage_init24bpp
  3385.         jmp     sys_putimage_bpp
  3386. @@:
  3387.         cmp     esi, 32
  3388.         jnz     @f
  3389.         mov     ebp, putimage_get32bpp
  3390.         mov     esi, putimage_init32bpp
  3391.         jmp     sys_putimage_bpp
  3392. @@:
  3393.         pop     ebp esi ebp
  3394.         ret
  3395.  
  3396. put_mono_image:
  3397.         push    ebp esi ebp
  3398.         mov     ebp, putimage_get1bpp
  3399.         mov     esi, putimage_init1bpp
  3400.         jmp     sys_putimage_bpp
  3401. put_2bit_image:
  3402.         push    ebp esi ebp
  3403.         mov     ebp, putimage_get2bpp
  3404.         mov     esi, putimage_init2bpp
  3405.         jmp     sys_putimage_bpp
  3406. put_4bit_image:
  3407.         push    ebp esi ebp
  3408.         mov     ebp, putimage_get4bpp
  3409.         mov     esi, putimage_init4bpp
  3410.         jmp     sys_putimage_bpp
  3411.  
  3412. putimage_init24bpp:
  3413.         lea     eax, [eax*3]
  3414. putimage_init8bpp:
  3415.         ret
  3416.  
  3417. align 16
  3418. putimage_get24bpp:
  3419.         movzx   eax, byte [esi+2]
  3420.         shl     eax, 16
  3421.         mov     ax, [esi]
  3422.         add     esi, 3
  3423.         ret     4
  3424. align 16
  3425. putimage_get8bpp:
  3426.         movzx   eax, byte [esi]
  3427.         push    edx
  3428.         mov     edx, [esp+8]
  3429.         mov     eax, [edx+eax*4]
  3430.         pop     edx
  3431.         inc     esi
  3432.         ret     4
  3433.  
  3434. putimage_init1bpp:
  3435.         add     eax, ecx
  3436.         push    ecx
  3437.         add     eax, 7
  3438.         add     ecx, 7
  3439.         shr     eax, 3
  3440.         shr     ecx, 3
  3441.         sub     eax, ecx
  3442.         pop     ecx
  3443.         ret
  3444. align 16
  3445. putimage_get1bpp:
  3446.         push    edx
  3447.         mov     edx, [esp+8]
  3448.         mov     al, [edx]
  3449.         add     al, al
  3450.         jnz     @f
  3451.         lodsb
  3452.         adc     al, al
  3453. @@:
  3454.         mov     [edx], al
  3455.         sbb     eax, eax
  3456.         and     eax, [edx+8]
  3457.         add     eax, [edx+4]
  3458.         pop     edx
  3459.         ret     4
  3460.  
  3461. putimage_init2bpp:
  3462.         add     eax, ecx
  3463.         push    ecx
  3464.         add     ecx, 3
  3465.         add     eax, 3
  3466.         shr     ecx, 2
  3467.         shr     eax, 2
  3468.         sub     eax, ecx
  3469.         pop     ecx
  3470.         ret
  3471. align 16
  3472. putimage_get2bpp:
  3473.         push    edx
  3474.         mov     edx, [esp+8]
  3475.         mov     al, [edx]
  3476.         mov     ah, al
  3477.         shr     al, 6
  3478.         shl     ah, 2
  3479.         jnz     .nonewbyte
  3480.         lodsb
  3481.         mov     ah, al
  3482.         shr     al, 6
  3483.         shl     ah, 2
  3484.         add     ah, 1
  3485. .nonewbyte:
  3486.         mov     [edx], ah
  3487.         mov     edx, [edx+4]
  3488.         movzx   eax, al
  3489.         mov     eax, [edx+eax*4]
  3490.         pop     edx
  3491.         ret     4
  3492.  
  3493. putimage_init4bpp:
  3494.         add     eax, ecx
  3495.         push    ecx
  3496.         add     ecx, 1
  3497.         add     eax, 1
  3498.         shr     ecx, 1
  3499.         shr     eax, 1
  3500.         sub     eax, ecx
  3501.         pop     ecx
  3502.         ret
  3503. align 16
  3504. putimage_get4bpp:
  3505.         push    edx
  3506.         mov     edx, [esp+8]
  3507.         add     byte [edx], 80h
  3508.         jc      @f
  3509.         movzx   eax, byte [edx+1]
  3510.         mov     edx, [edx+4]
  3511.         and     eax, 0x0F
  3512.         mov     eax, [edx+eax*4]
  3513.         pop     edx
  3514.         ret     4
  3515. @@:
  3516.         movzx   eax, byte [esi]
  3517.         add     esi, 1
  3518.         mov     [edx+1], al
  3519.         shr     eax, 4
  3520.         mov     edx, [edx+4]
  3521.         mov     eax, [edx+eax*4]
  3522.         pop     edx
  3523.         ret     4
  3524.  
  3525. putimage_init32bpp:
  3526.         shl     eax, 2
  3527.         ret
  3528. align 16
  3529. putimage_get32bpp:
  3530.         lodsd
  3531.         ret     4
  3532.  
  3533. putimage_init15bpp:
  3534. putimage_init16bpp:
  3535.         add     eax, eax
  3536.         ret
  3537. align 16
  3538. putimage_get15bpp:
  3539. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  3540.         push    ecx edx
  3541.         movzx   eax, word [esi]
  3542.         add     esi, 2
  3543.         mov     ecx, eax
  3544.         mov     edx, eax
  3545.         and     eax, 0x1F
  3546.         and     ecx, 0x1F shl 5
  3547.         and     edx, 0x1F shl 10
  3548.         shl     eax, 3
  3549.         shl     ecx, 6
  3550.         shl     edx, 9
  3551.         or      eax, ecx
  3552.         or      eax, edx
  3553.         pop     edx ecx
  3554.         ret     4
  3555.  
  3556. align 16
  3557. putimage_get16bpp:
  3558. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  3559.         push    ecx edx
  3560.         movzx   eax, word [esi]
  3561.         add     esi, 2
  3562.         mov     ecx, eax
  3563.         mov     edx, eax
  3564.         and     eax, 0x1F
  3565.         and     ecx, 0x3F shl 5
  3566.         and     edx, 0x1F shl 11
  3567.         shl     eax, 3
  3568.         shl     ecx, 5
  3569.         shl     edx, 8
  3570.         or      eax, ecx
  3571.         or      eax, edx
  3572.         pop     edx ecx
  3573.         ret     4
  3574.  
  3575.  
  3576.  
  3577. ; ==================================================
  3578. ; eax x beginning
  3579. ; ebx y beginning
  3580. ; ecx x end
  3581. ; edx y end
  3582. ; edi color
  3583.  
  3584. __sys_drawbar:
  3585.         mov     esi,[current_slot]
  3586.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  3587.         add     ecx,[esi+APPDATA.wnd_clientbox.left]
  3588.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  3589.         add     edx,[esi+APPDATA.wnd_clientbox.top]
  3590.   .forced:
  3591.     inc   [mouse_pause]
  3592.     call  vesa20_drawbar
  3593.     dec   [mouse_pause]
  3594.     jmp   [draw_pointer]
  3595.  
  3596.  
  3597. kb_read:
  3598.         push    ecx edx
  3599.  
  3600.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  3601.       kr_loop:
  3602.         in      al,0x64
  3603.         test    al,1
  3604.         jnz     kr_ready
  3605.         loop    kr_loop
  3606.         mov     ah,1
  3607.         jmp     kr_exit
  3608.       kr_ready:
  3609.         push    ecx
  3610.         mov     ecx,32
  3611.       kr_delay:
  3612.         loop    kr_delay
  3613.         pop     ecx
  3614.         in      al,0x60
  3615.         xor     ah,ah
  3616.       kr_exit:
  3617.  
  3618.         pop     edx ecx
  3619.  
  3620.         ret
  3621.  
  3622.  
  3623. kb_write:
  3624.  
  3625.         push    ecx edx
  3626.  
  3627.         mov     dl,al
  3628.         in      al,0x60
  3629.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  3630.       kw_loop:
  3631.         in      al,0x64
  3632.         test    al,2
  3633.         jz      kw_ok
  3634.         loop    kw_loop
  3635.         mov     ah,1
  3636.         jmp     kw_exit
  3637.       kw_ok:
  3638.         mov     al,dl
  3639.         out     0x60,al
  3640.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  3641.       kw_loop3:
  3642.         in      al,0x64
  3643.         test    al,2
  3644.         jz      kw_ok3
  3645.         loop    kw_loop3
  3646.         mov     ah,1
  3647.         jmp     kw_exit
  3648.       kw_ok3:
  3649.         mov     ah,8
  3650.       kw_loop4:
  3651.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  3652.       kw_loop5:
  3653.         in      al,0x64
  3654.         test    al,1
  3655.         jnz     kw_ok4
  3656.         loop    kw_loop5
  3657.         dec     ah
  3658.         jnz     kw_loop4
  3659.       kw_ok4:
  3660.         xor     ah,ah
  3661.       kw_exit:
  3662.  
  3663.         pop     edx ecx
  3664.  
  3665.         ret
  3666.  
  3667.  
  3668. kb_cmd:
  3669.  
  3670.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  3671.       c_wait:
  3672.         in      al,0x64
  3673.         test    al,2
  3674.         jz      c_send
  3675.         loop    c_wait
  3676.         jmp     c_error
  3677.       c_send:
  3678.         mov     al,bl
  3679.         out     0x64,al
  3680.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  3681.       c_accept:
  3682.         in      al,0x64
  3683.         test    al,2
  3684.         jz      c_ok
  3685.         loop    c_accept
  3686.       c_error:
  3687.         mov     ah,1
  3688.         jmp     c_exit
  3689.       c_ok:
  3690.         xor     ah,ah
  3691.       c_exit:
  3692.         ret
  3693.  
  3694.  
  3695. rerouteirqs:
  3696.  
  3697.         cli
  3698.  
  3699.         mov     al,0x11         ;  icw4, edge triggered
  3700.         out     0x20,al
  3701.         call    pic_delay
  3702.         out     0xA0,al
  3703.         call    pic_delay
  3704.  
  3705.         mov     al,0x20         ;  generate 0x20 +
  3706.         out     0x21,al
  3707.         call    pic_delay
  3708.         mov     al,0x28         ;  generate 0x28 +
  3709.         out     0xA1,al
  3710.         call    pic_delay
  3711.  
  3712.         mov     al,0x04         ;  slave at irq2
  3713.         out     0x21,al
  3714.         call    pic_delay
  3715.         mov     al,0x02         ;  at irq9
  3716.         out     0xA1,al
  3717.         call    pic_delay
  3718.  
  3719.         mov     al,0x01         ;  8086 mode
  3720.         out     0x21,al
  3721.         call    pic_delay
  3722.         out     0xA1,al
  3723.         call    pic_delay
  3724.  
  3725.         mov     al,255          ; mask all irq's
  3726.         out     0xA1,al
  3727.         call    pic_delay
  3728.         out     0x21,al
  3729.         call    pic_delay
  3730.  
  3731.         mov     ecx,0x1000
  3732.         cld
  3733. picl1:  call    pic_delay
  3734.         loop    picl1
  3735.  
  3736.         mov     al,255          ; mask all irq's
  3737.         out     0xA1,al
  3738.         call    pic_delay
  3739.         out     0x21,al
  3740.         call    pic_delay
  3741.  
  3742.         cli
  3743.  
  3744.         ret
  3745.  
  3746.  
  3747. pic_delay:
  3748.  
  3749.         jmp     pdl1
  3750. pdl1:   ret
  3751.  
  3752.  
  3753. sys_msg_board_str:
  3754.  
  3755.      pushad
  3756.    @@:
  3757.      cmp    [esi],byte 0
  3758.      je     @f
  3759.      mov    eax,1
  3760.      movzx  ebx,byte [esi]
  3761.      call   sys_msg_board
  3762.      inc    esi
  3763.      jmp    @b
  3764.    @@:
  3765.      popad
  3766.      ret
  3767.  
  3768. sys_msg_board_byte:
  3769. ; in: al = byte to display
  3770. ; out: nothing
  3771. ; destroys: nothing
  3772.         pushad
  3773.         mov     ecx, 2
  3774.         shl     eax, 24
  3775.         jmp     @f
  3776.  
  3777. sys_msg_board_word:
  3778. ; in: ax = word to display
  3779. ; out: nothing
  3780. ; destroys: nothing
  3781.         pushad
  3782.         mov     ecx, 4
  3783.         shl     eax, 16
  3784.         jmp     @f
  3785.  
  3786. sys_msg_board_dword:
  3787. ; in: eax = dword to display
  3788. ; out: nothing
  3789. ; destroys: nothing
  3790.         pushad
  3791.         mov     ecx, 8
  3792. @@:
  3793.         push    ecx
  3794.         rol     eax, 4
  3795.         push    eax
  3796.         and     al, 0xF
  3797.         cmp     al, 10
  3798.         sbb     al, 69h
  3799.         das
  3800.         mov     bl, al
  3801.         xor     eax, eax
  3802.         inc     eax
  3803.         call    sys_msg_board
  3804.         pop     eax
  3805.         pop     ecx
  3806.         loop    @b
  3807.         popad
  3808.         ret
  3809.  
  3810. uglobal
  3811.   msg_board_data: times 4096 db 0
  3812.   msg_board_count dd 0x0
  3813. endg
  3814.  
  3815. sys_msg_board:
  3816.  
  3817. ; eax=1 : write :  bl byte to write
  3818. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  3819.  
  3820.         mov     ecx, [msg_board_count]
  3821.         cmp     eax, 1
  3822.         jne     .smbl1
  3823.  
  3824.         mov     [msg_board_data+ecx],bl
  3825.         inc     ecx
  3826.         and     ecx, 4095
  3827.         mov     [msg_board_count], ecx
  3828.         mov     [check_idle_semaphore], 5
  3829.         ret
  3830. .smbl1:
  3831.         cmp     eax, 2
  3832.         jne     .smbl2
  3833.         test    ecx, ecx
  3834.         jz      .smbl21
  3835.         mov     eax, msg_board_data+1
  3836.         mov     ebx, msg_board_data
  3837.         movzx   edx, byte [ebx]
  3838.         call    memmove
  3839.         dec     [msg_board_count]
  3840.         mov     [esp + 36], edx ;eax
  3841.         mov     [esp + 24], dword 1
  3842.         ret
  3843. .smbl21:
  3844.         mov     [esp+36], ecx
  3845.         mov     [esp+24], ecx
  3846. .smbl2:
  3847.         ret
  3848.  
  3849. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3850. ;; 66 sys function.                                                ;;
  3851. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  3852. ;; out eax                                                         ;;
  3853. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3854. iglobal
  3855. align 4
  3856. f66call:
  3857.            dd sys_process_def.1   ; 1 = set keyboard mode
  3858.            dd sys_process_def.2   ; 2 = get keyboard mode
  3859.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  3860.            dd sys_process_def.4
  3861.            dd sys_process_def.5
  3862. endg
  3863.  
  3864.  
  3865.  
  3866.  
  3867. sys_process_def:
  3868.         dec     ebx
  3869.         cmp     ebx,5
  3870.         jae     .not_support    ;if >=6 then or eax,-1
  3871.  
  3872.         mov     edi, [CURRENT_TASK]
  3873.         jmp     dword [f66call+ebx*4]
  3874.  
  3875. .not_support:
  3876.         or      eax,-1
  3877.         ret
  3878.  
  3879. .1:
  3880.      shl   edi,8
  3881.      mov   [edi+SLOT_BASE + APPDATA.keyboard_mode],cl
  3882.  
  3883.      ret
  3884.  
  3885. .2:                             ; 2 = get keyboard mode
  3886.      shl   edi,8
  3887.      movzx eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  3888.      mov   [esp+32],eax
  3889.      ret
  3890. ;     xor   eax,eax
  3891. ;     movzx eax,byte [shift]
  3892. ;     movzx ebx,byte [ctrl]
  3893. ;     shl   ebx,2
  3894. ;     add   eax,ebx
  3895. ;     movzx ebx,byte [alt]
  3896. ;     shl   ebx,3
  3897. ;     add   eax,ebx
  3898. .3:                             ;3 = get keyboard ctrl, alt, shift
  3899.  ;// mike.dld [
  3900.      mov   eax, [kb_state]
  3901.  ;// mike.dld ]
  3902.      mov   [esp+32],eax
  3903.      ret
  3904.  
  3905. .4:
  3906.         mov     eax, hotkey_list
  3907. @@:
  3908.         cmp     dword [eax+8], 0
  3909.         jz      .found_free
  3910.         add     eax, 16
  3911.         cmp     eax, hotkey_list+16*256
  3912.         jb      @b
  3913.         mov     dword [esp+32], 1
  3914.         ret
  3915. .found_free:
  3916.         mov     [eax+8], edi
  3917.         mov     [eax+4], edx
  3918.         movzx   ecx, cl
  3919.         lea     ecx, [hotkey_scancodes+ecx*4]
  3920.         mov     edx, [ecx]
  3921.         mov     [eax], edx
  3922.         mov     [ecx], eax
  3923.         mov     [eax+12], ecx
  3924.         jecxz   @f
  3925.         mov     [edx+12], eax
  3926. @@:
  3927.         and     dword [esp+32], 0
  3928.         ret
  3929.  
  3930. .5:
  3931.         movzx   ebx, cl
  3932.         lea     ebx, [hotkey_scancodes+ebx*4]
  3933.         mov     eax, [ebx]
  3934. .scan:
  3935.         test    eax, eax
  3936.         jz      .notfound
  3937.         cmp     [eax+8], edi
  3938.         jnz     .next
  3939.         cmp     [eax+4], edx
  3940.         jz      .found
  3941. .next:
  3942.         mov     eax, [eax]
  3943.         jmp     .scan
  3944. .notfound:
  3945.         mov     dword [esp+32], 1
  3946.         ret
  3947. .found:
  3948.         mov     ecx, [eax]
  3949.         jecxz   @f
  3950.         mov     edx, [eax+12]
  3951.         mov     [ecx+12], edx
  3952. @@:
  3953.         mov     ecx, [eax+12]
  3954.         mov     edx, [eax]
  3955.         mov     [ecx], edx
  3956.         xor     edx, edx
  3957.         mov     [eax+4], edx
  3958.         mov     [eax+8], edx
  3959.         mov     [eax+12], edx
  3960.         mov     [eax], edx
  3961.         mov     [esp+32], edx
  3962.         ret
  3963.  
  3964.  
  3965. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3966. ;; 61 sys function.                                                ;;
  3967. ;; in eax=61,ebx in [1..3]                                         ;;
  3968. ;; out eax                                                         ;;
  3969. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3970. iglobal
  3971. align 4
  3972. f61call:
  3973.            dd sys_gs.1   ; resolution
  3974.            dd sys_gs.2   ; bits per pixel
  3975.            dd sys_gs.3   ; bytes per scanline
  3976. endg
  3977.  
  3978.  
  3979. align 4
  3980.  
  3981. sys_gs:                         ; direct screen access
  3982.         dec     ebx
  3983.         cmp     ebx,2
  3984.         ja      .not_support
  3985.         jmp     dword [f61call+ebx*4]
  3986. .not_support:
  3987.         or  [esp+32],dword -1
  3988.         ret
  3989.  
  3990.  
  3991. .1:                             ; resolution
  3992.      mov  eax,[Screen_Max_X]
  3993.      shl  eax,16
  3994.      mov  ax,[Screen_Max_Y]
  3995.      add  eax,0x00010001
  3996.      mov  [esp+32],eax
  3997.      ret
  3998. .2:                             ; bits per pixel
  3999.      movzx eax,byte [ScreenBPP]
  4000.      mov   [esp+32],eax
  4001.      ret
  4002. .3:                             ; bytes per scanline
  4003.      mov   eax,[BytesPerScanLine]
  4004.      mov   [esp+32],eax
  4005.      ret
  4006.  
  4007. align 4  ;  system functions
  4008.  
  4009. syscall_setpixel:                       ; SetPixel
  4010.  
  4011.         mov     eax, ebx
  4012.         mov     ebx, ecx
  4013.         mov     ecx, edx
  4014.         xor     edi, edi ; no force
  4015.         call    [_display.disable_mouse]
  4016.         jmp     [putpixel]
  4017.  
  4018. align 4
  4019.  
  4020. syscall_writetext:                      ; WriteText
  4021.  
  4022.         mov   eax,[TASK_BASE]
  4023.         mov   ebp,[eax-twdw+WDATA.box.left]
  4024.         push  esi
  4025.         mov   esi,[current_slot]
  4026.         add   ebp,[esi+APPDATA.wnd_clientbox.left]
  4027.         shl   ebp,16
  4028.         add   ebp,[eax-twdw+WDATA.box.top]
  4029.         add   bp,word[esi+APPDATA.wnd_clientbox.top]
  4030.         pop   esi
  4031.         add   ebx,ebp
  4032.         mov   eax,edi
  4033.         xor   edi,edi
  4034.         jmp   dtext
  4035.  
  4036. align 4
  4037.  
  4038. syscall_openramdiskfile:                ; OpenRamdiskFile
  4039.  
  4040.         mov     eax, ebx
  4041.         mov     ebx, ecx
  4042.         mov     ecx, edx
  4043.         mov     edx, esi
  4044.         mov     esi, 12
  4045.         call    fileread
  4046.         mov     [esp+32], eax
  4047.         ret
  4048.  
  4049. align 4
  4050.  
  4051. syscall_drawrect:                       ; DrawRect
  4052.  
  4053.         mov     edi, edx ; color + gradient
  4054.         and     edi, 0x80FFFFFF
  4055.         test    bx, bx  ; x.size
  4056.         je      .drectr
  4057.         test    cx, cx ; y.size
  4058.         je      .drectr
  4059.  
  4060.         mov     eax, ebx ; bad idea
  4061.         mov     ebx, ecx
  4062.  
  4063.         movzx   ecx, ax ; ecx - x.size
  4064.         shr     eax, 16 ; eax - x.coord
  4065.         movzx   edx, bx ; edx - y.size
  4066.         shr     ebx, 16 ; ebx - y.coord
  4067.         mov     esi, [current_slot]
  4068.  
  4069.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  4070.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  4071.         add     ecx, eax
  4072.         add     edx, ebx
  4073.         jmp     [drawbar]
  4074. .drectr:
  4075.         ret
  4076.  
  4077. align 4
  4078. syscall_getscreensize:                  ; GetScreenSize
  4079.         mov     ax, [Screen_Max_X]
  4080.         shl     eax, 16
  4081.         mov     ax, [Screen_Max_Y]
  4082.         mov     [esp + 32], eax
  4083.         ret
  4084.  
  4085. align 4
  4086.  
  4087.  
  4088. align 4
  4089.  
  4090. syscall_getpixel:                       ; GetPixel
  4091.      mov   ecx, [Screen_Max_X]
  4092.      inc   ecx
  4093.      xor   edx, edx
  4094.      mov   eax, ebx
  4095.      div   ecx
  4096.      mov   ebx, edx
  4097.      xchg  eax, ebx
  4098.      call  get_pixel    ; eax - x, ebx - y
  4099.      mov   [esp + 32], ecx
  4100.      ret
  4101.  
  4102. align 4
  4103.  
  4104. syscall_getarea:
  4105. ;eax = 36
  4106. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4107. ;ecx = [size x]*65536 + [size y]
  4108. ;edx = [start x]*65536 + [start y]
  4109.      pushad
  4110.          inc   [mouse_pause]
  4111. ; Check of use of the hardware cursor.
  4112.       cmp  [_display.disable_mouse],__sys_disable_mouse
  4113.           jne  @f
  4114. ; Since the test for the coordinates of the mouse should not be used,
  4115. ; then use the call [disable_mouse] is not possible!
  4116.       cmp  dword [MOUSE_VISIBLE],dword 0
  4117.       jne  @f
  4118.       pushf
  4119.       cli
  4120.       call draw_mouse_under
  4121.       popf
  4122.       mov  [MOUSE_VISIBLE],dword 1
  4123. @@:
  4124.      mov   edi,ebx
  4125.      mov   eax,edx
  4126.      shr   eax,16
  4127.      mov   ebx,edx
  4128.      and   ebx,0xffff
  4129.      dec   eax
  4130.            dec   ebx
  4131.      ; eax - x, ebx - y
  4132.      mov   edx,ecx
  4133.  
  4134.      shr   ecx,16
  4135.      and   edx,0xffff
  4136.      mov   esi,ecx
  4137.      ; ecx - size x, edx - size y
  4138.  
  4139.          mov   ebp,edx
  4140.          dec   ebp
  4141.      lea   ebp,[ebp*3]
  4142.  
  4143.          imul  ebp,esi
  4144.  
  4145.          mov   esi,ecx
  4146.          dec   esi
  4147.          lea   esi,[esi*3]
  4148.  
  4149.      add   ebp,esi
  4150.      add   ebp,edi
  4151.  
  4152.      add   ebx,edx
  4153.  
  4154. .start_y:
  4155.      push  ecx edx
  4156. .start_x:
  4157.      push  eax ebx ecx
  4158.      add   eax,ecx
  4159.  
  4160.      call  get_pixel ; eax - x, ebx - y
  4161.  
  4162.      mov   [ebp],cx
  4163.      shr   ecx,16
  4164.      mov   [ebp+2],cl
  4165.  
  4166.      pop   ecx ebx eax
  4167.      sub   ebp,3
  4168.      dec   ecx
  4169.      jnz   .start_x
  4170.          pop   edx ecx
  4171.          dec   ebx
  4172.      dec   edx
  4173.      jnz   .start_y
  4174.      dec        [mouse_pause]
  4175. ; Check of use of the hardware cursor.
  4176.       cmp  [_display.disable_mouse],__sys_disable_mouse
  4177.           jne  @f
  4178.          call  [draw_pointer]
  4179. @@:
  4180.      popad
  4181.      ret
  4182.  
  4183. align 4
  4184.  
  4185. syscall_drawline:                       ; DrawLine
  4186.  
  4187.         mov     edi, [TASK_BASE]
  4188.         movzx   eax, word[edi-twdw+WDATA.box.left]
  4189.         mov     ebp, eax
  4190.         mov     esi, [current_slot]
  4191.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  4192.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  4193.         add     ebp,ebx
  4194.         shl     eax, 16
  4195.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  4196.         add     eax, ebp
  4197.         mov     ebp, ebx
  4198.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  4199.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  4200.         add     ebp, ecx
  4201.         shl     ebx, 16
  4202.         xor     edi, edi
  4203.         add     ebx, ebp
  4204.         mov     ecx, edx
  4205.         jmp     [draw_line]
  4206.  
  4207. align 4
  4208.  
  4209. syscall_getirqowner:                    ; GetIrqOwner
  4210.  
  4211.      cmp   ebx,16
  4212.      jae   .err
  4213.  
  4214.      cmp   [irq_rights + 4 * ebx], dword 2
  4215.      je    .err
  4216.  
  4217.      mov   eax,[4 * ebx + irq_owner]
  4218.      mov   [esp+32],eax
  4219.  
  4220.      ret
  4221. .err:
  4222.      or    dword [esp+32], -1
  4223.      ret
  4224.  
  4225. align 4
  4226.  
  4227. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  4228.  
  4229.      call  r_f_port_area
  4230.      mov   [esp+32],eax
  4231.      ret
  4232.  
  4233. align 4
  4234.  
  4235. syscall_threads:                        ; CreateThreads
  4236. ; eax=1 create thread
  4237. ;
  4238. ;   ebx=thread start
  4239. ;   ecx=thread stack value
  4240. ;
  4241. ; on return : eax = pid
  4242.  
  4243.      call  new_sys_threads
  4244.  
  4245.      mov   [esp+32],eax
  4246.      ret
  4247.  
  4248. align 4
  4249.  
  4250. stack_driver_stat:
  4251.  
  4252.      call  app_stack_handler            ; Stack status
  4253.  
  4254. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4255. ;     call  change_task                 ; between sent packet
  4256.  
  4257.      mov   [esp+32],eax
  4258.      ret
  4259.  
  4260. align 4
  4261.  
  4262. socket:                                 ; Socket interface
  4263.      call  app_socket_handler
  4264.  
  4265. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  4266. ;     call  change_task                 ; between sent packet
  4267.  
  4268.      mov   [esp+36],eax
  4269.      mov   [esp+24],ebx
  4270.      ret
  4271.  
  4272. align 4
  4273.  
  4274. read_from_hd:                           ; Read from hd - fn not in use
  4275.  
  4276.      mov   edi,[TASK_BASE]
  4277.      add   edi,TASKDATA.mem_start
  4278.      add   eax,[edi]
  4279.      add   ecx,[edi]
  4280.      add   edx,[edi]
  4281.      call  file_read
  4282.  
  4283.      mov   [esp+36],eax
  4284.      mov   [esp+24],ebx
  4285.  
  4286.      ret
  4287.  
  4288. paleholder:
  4289.         ret
  4290.  
  4291. align 4
  4292. set_screen:
  4293.         cmp eax, [Screen_Max_X]
  4294.         jne .set
  4295.  
  4296.         cmp edx, [Screen_Max_Y]
  4297.         jne .set
  4298.         ret
  4299. .set:
  4300.         pushfd
  4301.         cli
  4302.  
  4303.         mov [Screen_Max_X], eax
  4304.         mov [Screen_Max_Y], edx
  4305.         mov [BytesPerScanLine], ecx
  4306.  
  4307.         mov [screen_workarea.right],eax
  4308.         mov [screen_workarea.bottom], edx
  4309.  
  4310.         push ebx
  4311.         push esi
  4312.         push edi
  4313.  
  4314.         pushad
  4315.  
  4316.         stdcall kernel_free, [_WinMapAddress]
  4317.  
  4318.         mov eax, [_display.width]
  4319.         mul [_display.height]
  4320.         mov [_WinMapSize], eax
  4321.  
  4322.         stdcall kernel_alloc, eax
  4323.         mov [_WinMapAddress], eax
  4324.         test eax, eax
  4325.         jz .epic_fail
  4326.  
  4327.         popad
  4328.  
  4329.         call    repos_windows
  4330.         xor eax, eax
  4331.         xor ebx, ebx
  4332.         mov     ecx, [Screen_Max_X]
  4333.         mov     edx, [Screen_Max_Y]
  4334.         call    calculatescreen
  4335.         pop edi
  4336.         pop esi
  4337.         pop ebx
  4338.  
  4339.         popfd
  4340.         ret
  4341.  
  4342. .epic_fail:
  4343.         hlt                     ; Houston, we've had a problem
  4344.  
  4345. ; --------------- APM ---------------------
  4346.  
  4347. align 4
  4348. sys_apm:
  4349.         xor     eax,eax
  4350.         inc     eax
  4351.         or      dword [esp + 44], eax   ; error
  4352.         add     eax,7
  4353.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  4354.         ret
  4355.  
  4356. ; -----------------------------------------
  4357.  
  4358. align 4
  4359.  
  4360. undefined_syscall:                      ; Undefined system call
  4361.      mov   [esp + 32], dword -1
  4362.      ret
  4363.  
  4364. align 4
  4365. system_shutdown:          ; shut down the system
  4366.  
  4367.            cmp byte [BOOT_VAR+0x9030], 1
  4368.            jne @F
  4369.            ret
  4370. @@:
  4371.            call stop_all_services
  4372.  
  4373. yes_shutdown_param:
  4374.            cli
  4375.  
  4376.  
  4377.            mov al, 0xFF
  4378.            out 0x21, al         ;IntrCntrl1Reg2
  4379.            out 0xA1, al         ;IntrCntrl2Reg2
  4380.  
  4381.         cmp     byte [BOOT_VAR + 0x9030], 2
  4382.         jnz     pm_restart
  4383. if 0
  4384.         mov     al, SB_PM_CTRL_BLK
  4385.         mov     ah, al
  4386.         inc     ah
  4387.         mov     dx, 0x0CD6
  4388.         out     dx, al
  4389.         inc     dl
  4390.         in      al, dx
  4391.         mov     cl, al
  4392.         dec     dl
  4393.         mov     al, ah
  4394.         out     dx, al
  4395.         inc     dl
  4396.         in      al, dx
  4397.         mov     ch, al
  4398. end if
  4399.         mov     dx, 0x0804      ;cx
  4400.         mov     ax, 0x03400
  4401.         out     dx, ax
  4402. ; THE END...
  4403.         jmp     $       ; just to be absolutely sure
  4404.  
  4405.  
  4406. pm_restart:
  4407.  
  4408.            mov  al,0x0F
  4409.            out  0x70,al         ; NmiEnable
  4410.            mov  al,0x05
  4411.            out  0x71,al         ; RtcData
  4412.  
  4413.            mov  ax, 6
  4414.            mov  dx, 0xCF9       ; reset reg
  4415.            out  dx, ax
  4416.  
  4417.            hlt
  4418.            jmp $-1
  4419.  
  4420.  
  4421. diff16 "End of 32-code ",0,$
  4422.  
  4423. include "data32.inc"
  4424.  
  4425. __REV__ = __REV
  4426.  
  4427. uglobals_size = $ - endofcode
  4428. diff16 "Zero-filled blk",0,endofcode
  4429. diff16 "End of kernel  ",0,$
  4430.  
  4431.