Subversion Repositories Kolibri OS

Rev

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