Subversion Repositories Kolibri OS

Rev

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

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