Subversion Repositories Kolibri OS

Rev

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