Subversion Repositories Kolibri OS

Rev

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