Subversion Repositories Kolibri OS

Rev

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

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