Subversion Repositories Kolibri OS

Rev

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