Subversion Repositories Kolibri OS

Rev

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

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