Subversion Repositories Kolibri OS

Rev

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