Subversion Repositories Kolibri OS

Rev

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