Subversion Repositories Kolibri OS

Rev

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