Subversion Repositories Kolibri OS

Rev

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

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