Subversion Repositories Kolibri OS

Rev

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