Subversion Repositories Kolibri OS

Rev

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