Subversion Repositories Kolibri OS

Rev

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

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