Subversion Repositories Kolibri OS

Rev

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