Subversion Repositories Kolibri OS

Rev

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