Subversion Repositories Kolibri OS

Rev

Rev 591 | Rev 608 | 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: 593 $
  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. sysfn_num = ($ - sys_system_table)/4
  2049. endg
  2050.  
  2051. sys_system:
  2052.         dec     eax
  2053.         cmp     eax, sysfn_num
  2054.         jae     @f
  2055.         jmp     dword [sys_system_table + eax*4]
  2056. @@:
  2057.         ret
  2058.  
  2059. sysfn_shutdown:         ; 18.1 = BOOT
  2060.      mov  [BOOT_VAR+0x9030],byte 0
  2061.   for_shutdown_parameter:
  2062.  
  2063.      mov  eax,[TASK_COUNT]
  2064.      add  eax,2
  2065.      mov  [shutdown_processes],eax
  2066.      mov  [SYS_SHUTDOWN],al
  2067.      and  dword [esp+36], 0
  2068.      ret
  2069.   uglobal
  2070.    shutdown_processes: dd 0x0
  2071.   endg
  2072.  
  2073. sysfn_terminate:        ; 18.2 = TERMINATE
  2074.      cmp  ebx,2
  2075.      jb   noprocessterminate
  2076.      mov  edx,[TASK_COUNT]
  2077.      cmp  ebx,edx
  2078.      ja   noprocessterminate
  2079.      mov  eax,[TASK_COUNT]
  2080.      shl  ebx,5
  2081.      mov  edx,[ebx+CURRENT_TASK+TASKDATA.pid]
  2082.      add  ebx,CURRENT_TASK+TASKDATA.state
  2083.      cmp  byte [ebx], 9
  2084.      jz   noprocessterminate
  2085.  
  2086.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2087.      mov  [ebx],byte 3       ; clear possible i40's
  2088.      ;call MEM_Heap_UnLock
  2089.  
  2090.      cmp  edx,[application_table_status]    ; clear app table stat
  2091.      jne  noatsc
  2092.      mov  [application_table_status],0
  2093.    noatsc:
  2094.    noprocessterminate:
  2095.      ret
  2096.  
  2097. sysfn_terminate2:
  2098. ;lock application_table_status mutex
  2099. .table_status:
  2100.     cli
  2101.     cmp    [application_table_status],0
  2102.     je     .stf
  2103.     sti
  2104.     call   change_task
  2105.     jmp    .table_status
  2106. .stf:
  2107.     call   set_application_table_status
  2108.     mov    eax,ebx
  2109.     call   pid_to_slot
  2110.     test   eax,eax
  2111.     jz     .not_found
  2112.     mov    ebx,eax
  2113.     cli
  2114.     call   sysfn_terminate
  2115.     mov    [application_table_status],0
  2116.     sti
  2117.     and    dword [esp+36],0
  2118.     ret
  2119. .not_found:
  2120.     mov    [application_table_status],0
  2121.     or     dword [esp+36],-1
  2122.     ret
  2123.  
  2124. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2125.      cmp  ebx,2
  2126.      jb   .nowindowactivate
  2127.      cmp  ebx,[TASK_COUNT]
  2128.      ja   .nowindowactivate
  2129.  
  2130.      mov   [window_minimize], 2   ; restore window if minimized
  2131.  
  2132.      movzx esi, word [WIN_STACK + ebx*2]
  2133.      cmp   esi, [TASK_COUNT]
  2134.      je    .nowindowactivate ; already active
  2135.  
  2136.      mov   edi, ebx
  2137.      shl   edi, 5
  2138.      add   edi, window_data
  2139.      movzx esi, word [WIN_STACK + ebx * 2]
  2140.      lea   esi, [WIN_POS + esi * 2]
  2141.      call  waredraw
  2142. .nowindowactivate:
  2143.      ret
  2144.  
  2145. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2146.      mov  eax,[idleusesec]
  2147.      mov  [esp+36], eax
  2148.      ret
  2149.  
  2150. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2151.      mov  eax,[CPU_FREQ]
  2152.      mov  [esp+36], eax
  2153.      ret
  2154.  
  2155. ;  SAVE ramdisk to /hd/1/menuet.img
  2156. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2157.    include 'blkdev/rdsave.inc'
  2158. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2159.  
  2160. sysfn_getactive:        ; 18.7 = get active window
  2161.      mov  eax, [TASK_COUNT]
  2162.    movzx  eax, word [WIN_POS + eax*2]
  2163.      mov  [esp+36],eax
  2164.      ret
  2165.  
  2166. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2167.      cmp  ebx,1
  2168.      jne  nogetsoundflag
  2169.      movzx  eax,byte [sound_flag] ; get sound_flag
  2170.      mov  [esp+36],eax
  2171.      ret
  2172.  nogetsoundflag:
  2173.      cmp  ebx,2
  2174.      jnz  nosoundflag
  2175.      xor  byte [sound_flag], 1
  2176.  nosoundflag:
  2177.      ret
  2178.  
  2179. sysfn_shutdown_param:   ; 18.9 = system shutdown with param
  2180.      cmp  ebx,1
  2181.      jl   exit_for_anyone
  2182.      cmp  ebx,4
  2183.      jg   exit_for_anyone
  2184.      mov  [BOOT_VAR+0x9030],bl
  2185.      jmp  for_shutdown_parameter
  2186.  
  2187. sysfn_minimize:         ; 18.10 = minimize window
  2188.      mov   [window_minimize],1
  2189.  exit_for_anyone:
  2190.      ret
  2191.  
  2192. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2193.      cmp  ebx,1
  2194.      jnz  full_table
  2195.   small_table:
  2196.      call for_all_tables
  2197.      mov ecx,10
  2198.      cld
  2199.      rep movsb
  2200.      ret
  2201.    for_all_tables:
  2202.      mov edi,[TASK_BASE]
  2203.      mov edi,[edi+TASKDATA.mem_start]
  2204.      add edi,ecx
  2205.      mov esi,DRIVE_DATA
  2206.      ret
  2207.   full_table:
  2208.      cmp  ebx,2
  2209.      jnz  exit_for_anyone
  2210.      call for_all_tables
  2211.      mov ecx,16384
  2212.      cld
  2213.      rep movsd
  2214.      ret
  2215.  
  2216. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2217.         and     dword [esp+36], 0
  2218.         ret
  2219.  
  2220. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2221.      mov edi,[TASK_BASE]
  2222.      mov edi,[edi+TASKDATA.mem_start]
  2223.      add edi,ebx
  2224.      mov esi,version_inf
  2225.      mov ecx,version_end-version_inf
  2226.      cld
  2227.      rep movsb
  2228.      ret
  2229.  
  2230. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2231.      ;wait retrace functions
  2232.  sys_wait_retrace:
  2233.      mov edx,0x3da
  2234.  WaitRetrace_loop:
  2235.      in al,dx
  2236.      test al,1000b
  2237.      jz WaitRetrace_loop
  2238.      mov [esp+36],dword 0
  2239.      ret
  2240.  
  2241. sysfn_centermouse:      ; 18.15 = mouse centered
  2242.      call  mouse_centered
  2243.      mov [esp+36],dword 0
  2244.      ret
  2245.  
  2246. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  2247.      cmp  ebx,0  ; get mouse speed factor
  2248.      jnz  .set_mouse_acceleration
  2249.      xor  eax,eax
  2250.      mov  ax,[mouse_speed_factor]
  2251.      mov  [esp+36],eax
  2252.      ret
  2253.  .set_mouse_acceleration:
  2254.      cmp  ebx,1  ; set mouse speed factor
  2255.      jnz  .get_mouse_delay
  2256.      mov  [mouse_speed_factor],cx
  2257.      ret
  2258.  .get_mouse_delay:
  2259.      cmp  ebx,2  ; get mouse delay
  2260.      jnz  .set_mouse_delay
  2261.      mov  eax,[mouse_delay]
  2262.      mov  [esp+36],eax
  2263.      ret
  2264.  .set_mouse_delay:
  2265.      cmp  ebx,3  ; set mouse delay
  2266.      jnz  .set_pointer_position
  2267.      mov  [mouse_delay],ecx
  2268.      ret
  2269.  .set_pointer_position:
  2270.      cmp  ebx,4  ; set mouse pointer position
  2271.      jnz  .end
  2272.      mov   [MOUSE_Y],cx    ;y
  2273.      ror   ecx,16
  2274.      mov   [MOUSE_X],cx    ;x
  2275.      rol   ecx,16
  2276.  .end:
  2277.      ret
  2278.  
  2279. sysfn_getfreemem:
  2280.      mov eax, [pg_data.pages_free]
  2281.      shl eax, 2
  2282.      mov [esp+36],eax
  2283.      ret
  2284.  
  2285. sysfn_getallmem:
  2286.      mov  eax,[MEM_AMOUNT]
  2287.      shr eax, 10
  2288.      mov  [esp+36],eax
  2289.      ret
  2290.  
  2291. uglobal
  2292. ;// mike.dld, 2006-29-01 [
  2293. screen_workarea RECT
  2294. ;// mike.dld, 2006-29-01 ]
  2295. window_minimize db 0
  2296. sound_flag      db 0
  2297. endg
  2298.  
  2299. iglobal
  2300. version_inf:
  2301.   db 0,7,0,0  ; version 0.7.0.0
  2302.   db UID_KOLIBRI
  2303.   dd __REV__
  2304. version_end:
  2305. endg
  2306.  
  2307. UID_NONE=0
  2308. UID_MENUETOS=1   ;official
  2309. UID_KOLIBRI=2    ;russian
  2310.  
  2311. sys_cachetodiskette:
  2312. ;    pushad
  2313. ;    cmp  eax,1
  2314. ;    jne  no_write_all_of_ramdisk
  2315. ;    call fdc_writeramdisk
  2316. ;    popad
  2317. ;    ret
  2318. ;  no_write_all_of_ramdisk:
  2319. ;    cmp eax,2
  2320. ;    jne no_write_part_of_ramdisk
  2321. ;    call fdc_commitflush
  2322. ;    popad
  2323. ;    ret
  2324. ;  no_write_part_of_ramdisk:
  2325. ;    cmp  eax,3
  2326. ;    jne  no_set_fdc
  2327. ;    call fdc_set
  2328. ;    popad
  2329. ;    ret
  2330. ;  no_set_fdc:
  2331. ;    cmp  eax,4
  2332. ;    jne  no_get_fdc
  2333. ;    popad
  2334. ;    call fdc_get
  2335. ;    mov    [esp+36],ecx
  2336. ;    ret
  2337. ;  no_get_fdc:
  2338. ;    popad
  2339. ;    ret
  2340.     cmp eax,1
  2341.     jne no_floppy_a_save
  2342.     mov   [flp_number],1
  2343.     jmp save_image_on_floppy
  2344.   no_floppy_a_save:
  2345.     cmp eax,2
  2346.     jne no_floppy_b_save
  2347.     mov   [flp_number],2
  2348.   save_image_on_floppy:
  2349.     call save_image
  2350.     mov  [esp+36],dword 0
  2351.     cmp  [FDC_Status],0
  2352.     je   yes_floppy_save
  2353.   no_floppy_b_save:
  2354.     mov [esp+36],dword 1
  2355.   yes_floppy_save:
  2356.     ret
  2357.  
  2358. uglobal
  2359. ;  bgrchanged  dd  0x0
  2360. bgrlock db 0
  2361. bgrlockpid dd 0
  2362. endg
  2363.  
  2364. sys_background:
  2365.  
  2366.     cmp   eax,1                            ; BACKGROUND SIZE
  2367.     jnz   nosb1
  2368.     cmp   ebx,0
  2369.     je    sbgrr
  2370.     cmp   ecx,0
  2371.     je    sbgrr
  2372. @@:
  2373.         mov     al, 1
  2374.         xchg    [bgrlock], al
  2375.         test    al, al
  2376.         jz      @f
  2377.         call    change_task
  2378.         jmp     @b
  2379. @@:
  2380.     mov   [BgrDataWidth],ebx
  2381.     mov   [BgrDataHeight],ecx
  2382. ;    mov   [bgrchanged],1
  2383.  
  2384.     pushad
  2385. ; return memory for old background
  2386.     stdcall kernel_free, [img_background]
  2387. ; calculate RAW size
  2388.     xor  eax,eax
  2389.     inc  eax
  2390.     cmp  [BgrDataWidth],eax
  2391.     jae   @f
  2392.     mov [BgrDataWidth],eax
  2393. @@:
  2394.     cmp  [BgrDataHeight],eax
  2395.     jae   @f
  2396.     mov [BgrDataHeight],eax
  2397. @@:
  2398.     mov  eax,[BgrDataWidth]
  2399.     imul eax,[BgrDataHeight]
  2400.     lea  eax,[eax*3]
  2401.     mov  [mem_BACKGROUND],eax
  2402. ; get memory for new background
  2403.     stdcall kernel_alloc, eax
  2404.     test eax, eax
  2405.     jz .exit_mem
  2406.     mov [img_background], eax
  2407. .exit_mem:
  2408.     popad
  2409.         mov     [bgrlock], 0
  2410.  
  2411.   sbgrr:
  2412.     ret
  2413.  
  2414.   nosb1:
  2415.  
  2416.     cmp   eax,2                            ; SET PIXEL
  2417.     jnz   nosb2
  2418.     mov   edx,[mem_BACKGROUND]
  2419.     cmp   edx,ebx
  2420.     jbe   nosb2
  2421.     mov   edx,[ebx]
  2422.     and   edx,0xFF000000 ;255*256*256*256
  2423.     and   ecx,0x00FFFFFF ;255*256*256+255*256+255
  2424.     add   edx,ecx
  2425.     ;mov   [ebx+IMG_BACKGROUND],edx
  2426.     push  eax
  2427.     mov   eax,[img_background]
  2428.     mov   [ebx+eax],edx
  2429.     pop   eax
  2430. ;    mov   [bgrchanged],1
  2431.     ret
  2432.   nosb2:
  2433.  
  2434.     cmp   eax,3                            ; DRAW BACKGROUND
  2435.     jnz   nosb3
  2436. draw_background_temp:
  2437. ;    cmp   [bgrchanged],1 ;0
  2438. ;    je    nosb31
  2439. ;draw_background_temp:
  2440. ;    mov   [bgrchanged],1 ;0
  2441.     mov   [REDRAW_BACKGROUND],byte 1
  2442.     mov    [background_defined], 1
  2443.    nosb31:
  2444.     ret
  2445.   nosb3:
  2446.  
  2447.     cmp   eax,4                            ; TILED / STRETCHED
  2448.     jnz   nosb4
  2449.     cmp   ebx,[BgrDrawMode]
  2450.     je    nosb41
  2451.     mov   [BgrDrawMode],ebx
  2452. ;    mov   [bgrchanged],1
  2453.    nosb41:
  2454.     ret
  2455.   nosb4:
  2456.  
  2457.     cmp   eax,5                            ; BLOCK MOVE TO BGR
  2458.     jnz   nosb5
  2459.   ; bughere
  2460.     mov   eax, ebx
  2461.     mov   ebx, ecx
  2462.     add   ebx, [img_background]   ;IMG_BACKGROUND
  2463.     mov   ecx, edx
  2464.     call  memmove
  2465.   .fin:
  2466.     ret
  2467.   nosb5:
  2468.  
  2469.         cmp     eax, 6
  2470.         jnz     nosb6
  2471. @@:
  2472.         mov     al, 1
  2473.         xchg    [bgrlock], al
  2474.         test    al, al
  2475.         jz      @f
  2476.         call    change_task
  2477.         jmp     @b
  2478. @@:
  2479.         mov     eax, [CURRENT_TASK]
  2480.         mov     [bgrlockpid], eax
  2481.         stdcall user_alloc, [mem_BACKGROUND]
  2482.         mov     [esp+36], eax
  2483.         test    eax, eax
  2484.         jz      .nomem
  2485.         mov     ebx, eax
  2486.         shr     ebx, 12
  2487.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2488.         mov     esi, [img_background]
  2489.         shr     esi, 12
  2490.         mov     ecx, [mem_BACKGROUND]
  2491.         add     ecx, 0xFFF
  2492.         shr     ecx, 12
  2493. .z:
  2494.         mov     eax, [page_tabs+ebx*4]
  2495.         test    al, 1
  2496.         jz      @f
  2497.         call    free_page
  2498. @@:
  2499.         mov     eax, [page_tabs+esi*4]
  2500.         or      al, PG_UW
  2501.         mov     [page_tabs+ebx*4], eax
  2502.         mov     eax, ebx
  2503.         shl     eax, 12
  2504.         invlpg  [eax]
  2505.         inc     ebx
  2506.         inc     esi
  2507.         loop    .z
  2508.         ret
  2509. .nomem:
  2510.         and     [bgrlockpid], 0
  2511.         mov     [bgrlock], 0
  2512. nosb6:
  2513.         cmp     eax, 7
  2514.         jnz     nosb7
  2515.         cmp     [bgrlock], 0
  2516.         jz      .err
  2517.         mov     eax, [CURRENT_TASK]
  2518.         cmp     [bgrlockpid], eax
  2519.         jnz     .err
  2520.         mov     eax, ebx
  2521.         shr     eax, 12
  2522.         mov     ecx, [page_tabs+(eax-1)*4]
  2523.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2524.         jz      .err
  2525.         jnp     .err
  2526.         push    eax
  2527.         shr     ecx, 12
  2528. @@:
  2529.         and     dword [page_tabs+eax*4], 0
  2530.         mov     edx, eax
  2531.         shl     edx, 12
  2532.         invlpg  [edx]
  2533.         inc     eax
  2534.         loop    @b
  2535.         pop     eax
  2536.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2537.         stdcall user_free, ebx
  2538.         mov     [esp+36], eax
  2539.         and     [bgrlockpid], 0
  2540.         mov     [bgrlock], 0
  2541.         ret
  2542. .err:
  2543.         and     dword [esp+36], 0
  2544.         ret
  2545.  
  2546. nosb7:
  2547.     ret
  2548.  
  2549.  
  2550. align 4
  2551.  
  2552. sys_getbackground:
  2553.  
  2554.     cmp   eax,1                                  ; SIZE
  2555.     jnz   nogb1
  2556.     mov   eax,[BgrDataWidth]
  2557.     shl   eax,16
  2558.     mov   ax,[BgrDataHeight]
  2559.     mov   [esp+36],eax
  2560.     ret
  2561.   nogb1:
  2562.  
  2563.     cmp   eax,2                                  ; PIXEL
  2564.     jnz   nogb2
  2565. ;    mov   edx,0x160000-16
  2566. ;    cmp   edx,ebx
  2567. ;    jbe   nogb2
  2568. ;    mov   eax, [ebx+IMG_BACKGROUND]
  2569.     mov   eax,[img_background]
  2570.     mov   eax,[ebx+eax]
  2571.  
  2572.     and   eax, 0xFFFFFF
  2573.     mov   [esp+36],eax
  2574.     ret
  2575.   nogb2:
  2576.  
  2577.     cmp   eax,4                                  ; TILED / STRETCHED
  2578.     jnz   nogb4
  2579.     mov   eax,[BgrDrawMode]
  2580.   nogb4:
  2581.     mov   [esp+36],eax
  2582.     ret
  2583.  
  2584.  
  2585. align 4
  2586.  
  2587. sys_getkey:
  2588.     mov   [esp+36],dword 1
  2589. ; test main buffer
  2590.     mov   ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2591.     movzx ecx,word [WIN_STACK + ebx * 2]
  2592.     mov   edx,[TASK_COUNT]
  2593.     cmp   ecx,edx
  2594.     jne   .finish
  2595.     cmp   [KEY_COUNT],byte 0
  2596.     je    .finish
  2597.     movzx eax,byte [KEY_BUFF]
  2598.     shl   eax,8
  2599.     push  eax
  2600.     dec   byte [KEY_COUNT]
  2601.     and   byte [KEY_COUNT],127
  2602.     movzx ecx,byte [KEY_COUNT]
  2603.     add   ecx,2
  2604.  ;   mov   esi,0xf402
  2605.  ;   mov   edi,0xf401
  2606.  ;   cld
  2607.  ;  rep   movsb
  2608.     mov   eax, KEY_BUFF+1
  2609.     mov   ebx, KEY_BUFF
  2610.     call  memmove
  2611.     pop   eax
  2612. .ret_eax:
  2613.     mov   [esp+36],eax
  2614.     ret
  2615.  .finish:
  2616. ; test hotkeys buffer
  2617.         mov     ecx, hotkey_buffer
  2618. @@:
  2619.         cmp     [ecx], ebx
  2620.         jz      .found
  2621.         add     ecx, 8
  2622.         cmp     ecx, hotkey_buffer+120*8
  2623.         jb      @b
  2624.         ret
  2625. .found:
  2626.         mov     ax, [ecx+6]
  2627.         shl     eax, 16
  2628.         mov     ah, [ecx+4]
  2629.         mov     al, 2
  2630.         and     dword [ecx+4], 0
  2631.         and     dword [ecx], 0
  2632.         jmp     .ret_eax
  2633.  
  2634. align 4
  2635.  
  2636. sys_getbutton:
  2637.  
  2638.     mov   ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2639.     mov   [esp+36],dword 1
  2640.     movzx ecx, word [WIN_STACK + ebx * 2]
  2641.     mov   edx, [TASK_COUNT] ; less than 256 processes
  2642.     cmp   ecx,edx
  2643.     jne   .exit
  2644.     movzx eax,byte [BTN_COUNT]
  2645.     test  eax,eax
  2646.     jz    .exit
  2647.     mov   eax,[BTN_BUFF]
  2648.     shl   eax,8
  2649.     mov   [BTN_COUNT],byte 0
  2650.     mov   [esp+36],eax
  2651.  .exit:
  2652.     ret
  2653.  
  2654.  
  2655. align 4
  2656.  
  2657. sys_cpuusage:
  2658.  
  2659. ;  RETURN:
  2660. ;
  2661. ;  +00 dword     process cpu usage
  2662. ;  +04  word     position in windowing stack
  2663. ;  +06  word     windowing stack value at current position (cpu nro)
  2664. ;  +10 12 bytes  name
  2665. ;  +22 dword     start in mem
  2666. ;  +26 dword     used mem
  2667. ;  +30 dword     PID , process idenfification number
  2668. ;
  2669.  
  2670.     mov  edi,[TASK_BASE]   ; eax = return area
  2671.     add  eax,[edi + TASKDATA.mem_start]
  2672.  
  2673.     cmp  ebx,-1         ; who am I ?
  2674.     jne  no_who_am_i
  2675.     mov  ebx,[CURRENT_TASK]
  2676.   no_who_am_i:
  2677.  
  2678.     push eax            ; return area
  2679.     push ebx            ; process number
  2680.  
  2681.     push ebx
  2682.     push ebx
  2683.     push eax
  2684.  
  2685.     ; return memory usage
  2686.  
  2687.     xor  edx,edx
  2688.     mov  eax,0x20
  2689.     mul  ebx
  2690.     add  eax,CURRENT_TASK+TASKDATA.cpu_usage
  2691.     mov  ebx,eax
  2692.     pop  eax
  2693.     mov  ecx,[ebx]
  2694.     mov  [eax],ecx
  2695.     pop  ebx
  2696.     mov  cx, [WIN_STACK + ebx * 2]
  2697.     mov  [eax+4],cx
  2698.     mov  cx, [WIN_POS + ebx * 2]
  2699.     mov  [eax+6],cx
  2700.     push eax
  2701.     mov  eax,ebx
  2702.     shl  eax,8
  2703.     add  eax,SLOT_BASE+APPDATA.app_name
  2704.     pop  ebx
  2705.     add  ebx,10
  2706.     mov  ecx,11
  2707.     call memmove
  2708.  
  2709.     ; memory usage
  2710.  
  2711.     xor    eax,eax
  2712.     mov    edx,0x100000*16
  2713.     pop    ecx                                   ; get gdt of tss
  2714.     cmp    ecx,1
  2715.     je     os_mem
  2716.     shl    ecx,8
  2717.     mov    edx,[SLOT_BASE+ecx+APPDATA.mem_size] ;0x8c
  2718.     mov    eax,std_application_base_address
  2719.     ; eax run base -> edx used memory
  2720.   os_mem:
  2721.     dec    edx
  2722.     mov    [ebx+12],eax
  2723.     mov    [ebx+16],edx
  2724.  
  2725.     ; PID (+30)
  2726.  
  2727.     mov    eax,[esp]
  2728.     shl    eax,5
  2729.     add    eax,CURRENT_TASK+TASKDATA.pid
  2730.     mov    eax,[eax]
  2731.     mov    [ebx+20],eax
  2732.  
  2733.     ; window position and size
  2734.  
  2735.     mov    esi,[esp]
  2736.     shl    esi,5
  2737.     add    esi,window_data + WDATA.box
  2738.     mov    edi,[esp+4]
  2739.     add    edi,34
  2740.     mov    ecx,4
  2741.     cld
  2742.     rep    movsd
  2743.  
  2744.     ; Process state (+50)
  2745.  
  2746.     mov    eax,[esp]
  2747.     shl    eax,5
  2748.     add    eax,CURRENT_TASK+TASKDATA.state
  2749.     mov    eax,[eax]
  2750.     mov    [ebx+40],ax
  2751.  
  2752.     ; Window client area box
  2753.  
  2754.     mov    esi,[esp]
  2755.     shl    esi,8
  2756.     add    esi,SLOT_BASE+APPDATA.wnd_clientbox
  2757.     lea    edi,[ebx+44]
  2758.     mov    ecx,4
  2759.     rep    movsd
  2760.  
  2761.     ; Window state
  2762.  
  2763.     mov    esi,[esp]
  2764.     shl    esi,5
  2765.     add    esi,window_data + WDATA.box
  2766.     mov    al,[esi+WDATA.fl_wstate]
  2767.     mov    [edi],al
  2768.  
  2769.     pop    ebx
  2770.     pop    eax
  2771.  
  2772.     ; return number of processes
  2773.  
  2774.     mov    eax,[TASK_COUNT]
  2775.     mov    [esp+36],eax
  2776.     ret
  2777.  
  2778. align 4
  2779. sys_clock:
  2780.         cli
  2781.   ; Mikhail Lisovin  xx Jan 2005
  2782.   @@:   mov   al, 10
  2783.         out   0x70, al
  2784.         in    al, 0x71
  2785.         test  al, al
  2786.         jns   @f
  2787.         mov   esi, 1
  2788.         call  delay_ms
  2789.         jmp   @b
  2790.   @@:
  2791.   ; end Lisovin's fix
  2792.  
  2793.         xor   al,al           ; seconds
  2794.         out   0x70,al
  2795.         in    al,0x71
  2796.         movzx ecx,al
  2797.         mov   al,02           ; minutes
  2798.         shl   ecx,16
  2799.         out   0x70,al
  2800.         in    al,0x71
  2801.         movzx edx,al
  2802.         mov   al,04           ; hours
  2803.         shl   edx,8
  2804.         out   0x70,al
  2805.         in    al,0x71
  2806.         add   ecx,edx
  2807.         movzx edx,al
  2808.         add   ecx,edx
  2809.         sti
  2810.         mov   [esp+36],ecx
  2811.         ret
  2812.  
  2813.  
  2814. align 4
  2815.  
  2816. sys_date:
  2817.  
  2818.         cli
  2819.  
  2820.   @@:   mov   al, 10
  2821.         out   0x70, al
  2822.         in    al, 0x71
  2823.         test  al, al
  2824.         jns   @f
  2825.         mov   esi, 1
  2826.         call  delay_ms
  2827.         jmp   @b
  2828.   @@:
  2829.  
  2830.         mov     ch,0
  2831.         mov     al,7            ; date
  2832.         out     0x70,al
  2833.         in      al,0x71
  2834.         mov     cl,al
  2835.         mov     al,8            ; month
  2836.         shl     ecx,16
  2837.         out     0x70,al
  2838.         in      al,0x71
  2839.         mov     ch,al
  2840.         mov     al,9            ; year
  2841.         out     0x70,al
  2842.         in      al,0x71
  2843.         mov     cl,al
  2844.         sti
  2845.         mov     [esp+36],ecx
  2846.         ret
  2847.  
  2848.  
  2849. ; redraw status
  2850.  
  2851. sys_redrawstat:
  2852.  
  2853.     cmp  eax,1
  2854.     jne  no_widgets_away
  2855.  
  2856.     ; buttons away
  2857.  
  2858.     mov   ecx,[CURRENT_TASK]
  2859.  
  2860.   sys_newba2:
  2861.  
  2862.     mov   edi,[BTN_ADDR]
  2863.     cmp   [edi],dword 0  ; empty button list ?
  2864.     je    end_of_buttons_away
  2865.  
  2866.     movzx ebx,word [edi]
  2867.     inc   ebx
  2868.  
  2869.     mov   eax,edi
  2870.  
  2871.   sys_newba:
  2872.  
  2873.     dec   ebx
  2874.     jz    end_of_buttons_away
  2875.  
  2876.     add   eax,0x10
  2877.     cmp   cx,[eax]
  2878.     jnz   sys_newba
  2879.  
  2880.     push  eax ebx ecx
  2881.     mov   ecx,ebx
  2882.     inc   ecx
  2883.     shl   ecx,4
  2884.     mov   ebx,eax
  2885.     add   eax,0x10
  2886.     call  memmove
  2887.     dec   dword [edi]
  2888.     pop   ecx ebx eax
  2889.  
  2890.     jmp   sys_newba2
  2891.  
  2892.   end_of_buttons_away:
  2893.  
  2894.     ret
  2895.  
  2896.   no_widgets_away:
  2897.  
  2898.     cmp   eax,2
  2899.     jnz   srl1
  2900.  
  2901.     mov   edx,[TASK_BASE]      ; return whole screen draw area for this app
  2902.     add   edx,draw_data-CURRENT_TASK
  2903.     mov   [edx+RECT.left], 0
  2904.     mov   [edx+RECT.top], 0
  2905.     mov   eax,[ScreenWidth]
  2906.     mov   [edx+RECT.right],eax
  2907.     mov   eax,[ScreenHeight]
  2908.     mov   [edx+RECT.bottom],eax
  2909.  
  2910.     mov   edi,[TASK_BASE]
  2911.     or    [edi-twdw+WDATA.fl_wdrawn], 1   ; no new position & buttons from app
  2912.  
  2913.     call  sys_window_mouse
  2914.  
  2915.     ret
  2916.  
  2917.   srl1:
  2918.  
  2919.     ret
  2920.  
  2921.  
  2922. sys_drawwindow:
  2923.  
  2924.     mov   edi,ecx
  2925.     shr   edi,16+8
  2926.     and   edi,15
  2927.  
  2928.     cmp   edi,0   ; type I    - original style
  2929.     jne   nosyswI
  2930.     inc   [mouse_pause]
  2931.     call  [disable_mouse]
  2932.     call  sys_set_window
  2933.     call  [disable_mouse]
  2934.     call  drawwindow_I
  2935.     ;dec   [mouse_pause]
  2936.     ;call   [draw_pointer]
  2937.     ;ret
  2938.     jmp   draw_window_caption.2
  2939.   nosyswI:
  2940.  
  2941.     cmp   edi,1   ; type II   - only reserve area, no draw
  2942.     jne   nosyswII
  2943.     inc   [mouse_pause]
  2944.     call  [disable_mouse]
  2945.     call  sys_set_window
  2946.     call  [disable_mouse]
  2947.     call  sys_window_mouse
  2948.     dec   [mouse_pause]
  2949.     call   [draw_pointer]
  2950.     ret
  2951.   nosyswII:
  2952.  
  2953.     cmp   edi,2   ; type III  - new style
  2954.     jne   nosyswIII
  2955.     inc   [mouse_pause]
  2956.     call  [disable_mouse]
  2957.     call  sys_set_window
  2958.     call  [disable_mouse]
  2959.     call  drawwindow_III
  2960.     ;dec   [mouse_pause]
  2961.     ;call   [draw_pointer]
  2962.     ;ret
  2963.     jmp   draw_window_caption.2
  2964.   nosyswIII:
  2965.  
  2966.     cmp   edi,3   ; type IV - skinned window
  2967.     je    draw_skin_window
  2968.     cmp   edi,4   ; type V - skinned window not sized!          {not_sized_skin_window}
  2969.     jne   nosyswV
  2970.   draw_skin_window:
  2971.  
  2972.     inc   [mouse_pause]
  2973.     call  [disable_mouse]
  2974.     call  sys_set_window
  2975.     call  [disable_mouse]
  2976.     push  0
  2977.     mov   edi, [TASK_COUNT]
  2978.     movzx edi, word [WIN_POS + edi*2]
  2979.     cmp   edi, [CURRENT_TASK]
  2980.     jne   @f
  2981.     inc   dword [esp]
  2982.  @@:
  2983.     call  drawwindow_IV
  2984.     ;dec   [mouse_pause]
  2985.     ;call   [draw_pointer]
  2986.     ;ret
  2987.     jmp   draw_window_caption.2
  2988.   nosyswV:
  2989.  
  2990.     ret
  2991.  
  2992.  
  2993. draw_window_caption:
  2994.         inc     [mouse_pause]
  2995.         call    [disable_mouse]
  2996.  
  2997.         xor     eax,eax
  2998.         mov     edx,[TASK_COUNT]
  2999.         movzx   edx,word[WIN_POS+edx*2]
  3000.         cmp     edx,[CURRENT_TASK]
  3001.         jne     @f
  3002.         inc     eax
  3003.     @@: mov     edx,[CURRENT_TASK]
  3004.         shl     edx,5
  3005.         add     edx,window_data
  3006.         movzx   ebx,[edx+WDATA.fl_wstyle]
  3007.         and     bl,0x0F
  3008.         cmp     bl,3
  3009.         je      .draw_caption_style_3           ;{for 3 and 4 style write caption}
  3010.         cmp     bl,4
  3011.         je      .draw_caption_style_3
  3012.  
  3013.         jmp     .not_style_3
  3014.   .draw_caption_style_3:
  3015.  
  3016.         push    edx
  3017.         call    drawwindow_IV_caption
  3018.         add     esp,4
  3019.         jmp     .2
  3020.  
  3021.   .not_style_3:
  3022.         cmp     bl,2
  3023.         jne     .not_style_2
  3024.  
  3025.         call    drawwindow_III_caption
  3026.         jmp     .2
  3027.  
  3028.   .not_style_2:
  3029.         cmp     bl,0
  3030.         jne     .2
  3031.  
  3032.         call    drawwindow_I_caption
  3033.  
  3034. ;--------------------------------------------------------------
  3035.   .2:   ;jmp     @f
  3036.         mov     edi,[CURRENT_TASK]
  3037.         shl     edi,5
  3038.         test    [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3039.         jz      @f
  3040.         mov     ecx,[edi*8+SLOT_BASE+APPDATA.wnd_caption]
  3041.         or      ecx,ecx
  3042.         jz      @f
  3043.         add     ecx,[edi+CURRENT_TASK+TASKDATA.mem_start]
  3044.  
  3045.         movzx   eax,[edi+window_data+WDATA.fl_wstyle]
  3046.         and     al,0x0F
  3047.         cmp     al,3
  3048.         je      .skinned
  3049.         cmp     al,4
  3050.         je      .skinned
  3051.  
  3052.         jmp     .not_skinned
  3053.   .skinned:
  3054.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  3055.         mov     bp,word[edi+window_data+WDATA.box.top]
  3056.         movzx   eax,word[edi+window_data+WDATA.box.width]
  3057.         sub     ax,[_skinmargins.left]
  3058.         sub     ax,[_skinmargins.right]
  3059.         cwde
  3060.         cdq
  3061.         mov     ebx,6
  3062.         idiv    ebx
  3063.         or      eax,eax
  3064.         js      @f
  3065.         mov     edx,eax
  3066.         mov     eax,dword[_skinmargins.left-2]
  3067.         mov     ax,word[_skinh]
  3068.         sub     ax,[_skinmargins.bottom]
  3069.         sub     ax,[_skinmargins.top]
  3070.         sar     ax,1
  3071.         adc     ax,0
  3072.         add     ax,[_skinmargins.top]
  3073.         add     ax,-3
  3074.         add     eax,ebp
  3075.         jmp     .dodraw
  3076.  
  3077.   .not_skinned:
  3078.         cmp     al,1
  3079.         je      @f
  3080.  
  3081.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  3082.         mov     bp,word[edi+window_data+WDATA.box.top]
  3083.         movzx   eax,word[edi+window_data+WDATA.box.width]
  3084.         sub     eax,16
  3085.         cwde
  3086.         cdq
  3087.         mov     ebx,6
  3088.         idiv    ebx
  3089.         or      eax,eax
  3090.         js      @f
  3091.         mov     edx,eax
  3092.         mov     eax,0x00080007
  3093.         add     eax,ebp
  3094. .dodraw:
  3095.         mov     ebx,[common_colours+16];0x00FFFFFF
  3096.         or      ebx, 0x80000000
  3097.         xor     edi,edi
  3098.         call    dtext
  3099.  
  3100.     @@:
  3101. ;--------------------------------------------------------------
  3102.         dec     [mouse_pause]
  3103.         call    [draw_pointer]
  3104.         ret
  3105.  
  3106. iglobal
  3107. align 4
  3108. window_topleft dd \
  3109.   1, 21,\               ;type 0
  3110.   0,  0,\       ;type 1
  3111.   5, 20,\       ;type 2
  3112.   5,  ?,\       ;type 3 {set by skin}
  3113.   5,  ?         ;type 4 {set by skin}
  3114. endg
  3115.  
  3116. set_window_clientbox:
  3117.         push    eax ecx edi
  3118.  
  3119.         mov     eax,[_skinh]
  3120.         mov     [window_topleft+4*7],eax
  3121.         mov     [window_topleft+4*9],eax
  3122.  
  3123.         mov     ecx,edi
  3124.         sub     edi,window_data
  3125.         shl     edi,3
  3126.         test    [ecx+WDATA.fl_wstyle],WSTYLE_CLIENTRELATIVE
  3127.         jz      @f
  3128.  
  3129.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3130.         and     eax,0x0F
  3131.         mov     eax,[eax*8+window_topleft+0]
  3132.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3133.         shl     eax,1
  3134.         neg     eax
  3135.         add     eax,[ecx+WDATA.box.width]
  3136.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3137.  
  3138.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3139.         and     eax,0x0F
  3140.         push    [eax*8+window_topleft+0]
  3141.         mov     eax,[eax*8+window_topleft+4]
  3142.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3143.         neg     eax
  3144.         sub     eax,[esp]
  3145.         add     eax,[ecx+WDATA.box.height]
  3146.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3147.         add     esp,4
  3148.  
  3149.         pop     edi ecx eax
  3150.         ret
  3151.     @@:
  3152.         xor     eax,eax
  3153.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3154.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3155.         mov     eax,[ecx+WDATA.box.width]
  3156.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3157.         mov     eax,[ecx+WDATA.box.height]
  3158.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3159.  
  3160.         pop     edi ecx eax
  3161.         ret
  3162.  
  3163. sys_set_window:
  3164.  
  3165.     mov   edi,[CURRENT_TASK]
  3166.     shl   edi,5
  3167.     add   edi,window_data
  3168.  
  3169.     ; colors
  3170.     mov   [edi+WDATA.cl_workarea],ecx
  3171.     mov   [edi+WDATA.cl_titlebar],edx
  3172.     mov   [edi+WDATA.cl_frames],esi
  3173.  
  3174.     ; check flag (?)
  3175.     test  [edi+WDATA.fl_wdrawn],1
  3176.     jnz   newd
  3177.  
  3178.     push  eax
  3179.     mov   eax,[timer_ticks] ;[0xfdf0]
  3180.     add   eax,100
  3181.     mov   [new_window_starting],eax
  3182.     pop   eax
  3183.  
  3184.     mov   word[edi+WDATA.box.width],ax
  3185.     mov   word[edi+WDATA.box.height],bx
  3186.     sar   eax,16
  3187.     sar   ebx,16
  3188.     mov   word[edi+WDATA.box.left],ax
  3189.     mov   word[edi+WDATA.box.top],bx
  3190.  
  3191.     call  check_window_position
  3192.  
  3193.     call  set_window_clientbox
  3194.  
  3195.     push  ecx esi edi               ; save for window fullscreen/resize
  3196.     ;mov   esi,edi
  3197.  
  3198.         mov     cl,[edi+WDATA.fl_wstyle]
  3199.  
  3200.     sub   edi,window_data
  3201.     shl   edi,3
  3202.     add   edi,SLOT_BASE
  3203.  
  3204.         and     cl,0x0F
  3205.         mov     [edi+APPDATA.wnd_caption],0
  3206.         cmp     cl,3
  3207.         je      set_APPDATA_wnd_caption
  3208.         cmp     cl,4                                                            ; {SPraid.simba}
  3209.         je      set_APPDATA_wnd_caption
  3210.  
  3211.         jmp     @f
  3212.     set_APPDATA_wnd_caption:
  3213.         mov     [edi+APPDATA.wnd_caption],esi
  3214.     @@: mov     esi,[esp+0]
  3215.  
  3216.     add   edi, APPDATA.saved_box
  3217.         movsd
  3218.         movsd
  3219.         movsd
  3220.         movsd
  3221.     pop   edi esi ecx
  3222.  
  3223.         mov     esi, [CURRENT_TASK]
  3224.         movzx   esi, word [WIN_STACK+esi*2]
  3225.         lea     esi, [WIN_POS+esi*2]
  3226.         call    waredraw
  3227.  
  3228.     push  eax ebx ecx edx
  3229. ;;;    mov   eax, 1
  3230. ;;;    call  delay_hs
  3231.     mov   eax, [edi+WDATA.box.left]
  3232.     mov   ebx, [edi+WDATA.box.top]
  3233.     mov   ecx, [edi+WDATA.box.width]
  3234.     mov   edx, [edi+WDATA.box.height]
  3235.     add   ecx, eax
  3236.     add   edx, ebx
  3237.     call  calculatescreen
  3238.     pop   edx ecx ebx eax
  3239.  
  3240.     mov   [KEY_COUNT],byte 0           ; empty keyboard buffer
  3241.     mov   [BTN_COUNT],byte 0           ; empty button buffer
  3242.  
  3243.   newd:
  3244.     mov   [edi+WDATA.fl_redraw],byte 0   ; no redraw
  3245.     mov   edx,edi
  3246.  
  3247.     ret
  3248.  
  3249. syscall_windowsettings:
  3250.  
  3251.   .set_window_caption:
  3252.         dec     eax     ; subfunction #1 - set window caption
  3253.         jnz     .get_window_caption
  3254.  
  3255.         ; NOTE: only window owner thread can set its caption,
  3256.         ;       so there's no parameter for PID/TID
  3257.  
  3258.         mov     edi,[CURRENT_TASK]
  3259.         shl     edi,5
  3260.  
  3261.         ; have to check if caption is within application memory limit
  3262.         ; check is trivial, and if application resizes its memory,
  3263.         ;   caption still can become over bounds
  3264. ; diamond, 31.10.2006: check removed because with new memory manager
  3265. ; there can be valid data after APPDATA.mem_size bound
  3266. ;        mov     ecx,[edi*8+SLOT_BASE+APPDATA.mem_size]
  3267. ;        add     ecx,255 ; max caption length
  3268. ;        cmp     ebx,ecx
  3269. ;        ja      .exit_fail
  3270.  
  3271.         mov     [edi*8+SLOT_BASE+APPDATA.wnd_caption],ebx
  3272.         or      [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3273.  
  3274.         call    draw_window_caption
  3275.  
  3276.         xor     eax,eax ; eax = 0 (success)
  3277.         ret
  3278.  
  3279.   .get_window_caption:
  3280.         dec     eax     ; subfunction #2 - get window caption
  3281.         jnz     .exit_fail
  3282.  
  3283.         ; not implemented yet
  3284.  
  3285.   .exit_fail:
  3286.         xor     eax,eax
  3287.         inc     eax     ; eax = 1 (fail)
  3288.         ret
  3289.  
  3290.  
  3291. sys_window_move:
  3292.  
  3293.         mov     edi,[CURRENT_TASK]
  3294.         shl     edi,5
  3295.         add     edi,window_data
  3296.  
  3297.         test    [edi+WDATA.fl_wstate],WSTATE_MAXIMIZED
  3298.         jnz     .window_move_return
  3299.  
  3300.         push    dword [edi + WDATA.box.left]  ; save old coordinates
  3301.         push    dword [edi + WDATA.box.top]
  3302.         push    dword [edi + WDATA.box.width]
  3303.         push    dword [edi + WDATA.box.height]
  3304.  
  3305.         cmp   eax,-1                  ; set new position and size
  3306.         je    .no_x_reposition
  3307.         mov     [edi + WDATA.box.left], eax
  3308.       .no_x_reposition:
  3309.         cmp   ebx,-1
  3310.         je    .no_y_reposition
  3311.         mov     [edi + WDATA.box.top], ebx
  3312.       .no_y_reposition:
  3313.  
  3314.         test    [edi+WDATA.fl_wstate],WSTATE_ROLLEDUP
  3315.         jnz     .no_y_resizing
  3316.  
  3317.         cmp   ecx,-1
  3318.         je    .no_x_resizing
  3319.         mov     [edi + WDATA.box.width], ecx
  3320.       .no_x_resizing:
  3321.         cmp   edx,-1
  3322.         je    .no_y_resizing
  3323.         mov     [edi + WDATA.box.height], edx
  3324.       .no_y_resizing:
  3325.  
  3326.         call  check_window_position
  3327.         call  set_window_clientbox
  3328.  
  3329.         pushad                       ; save for window fullscreen/resize
  3330.         mov   esi,edi
  3331.         sub   edi,window_data
  3332.         shr   edi,5
  3333.         shl   edi,8
  3334.         add   edi, SLOT_BASE + APPDATA.saved_box
  3335.         mov   ecx,4
  3336.         cld
  3337.         rep   movsd
  3338.         popad
  3339.  
  3340.         pushad                       ; calculcate screen at new position
  3341.         mov   eax, [edi + WDATA.box.left]
  3342.         mov   ebx, [edi + WDATA.box.top]
  3343.         mov   ecx, [edi + WDATA.box.width]
  3344.         mov   edx, [edi + WDATA.box.height]
  3345.         add   ecx,eax
  3346.         add   edx,ebx
  3347.  
  3348.         call  calculatescreen
  3349.         popad
  3350.  
  3351.         pop   edx                   ; calculcate screen at old position
  3352.         pop   ecx
  3353.         pop   ebx
  3354.         pop   eax
  3355.         add   ecx,eax
  3356.         add   edx,ebx
  3357.         mov   [dlx],eax             ; save for drawlimits
  3358.         mov   [dly],ebx
  3359.         mov   [dlxe],ecx
  3360.         mov   [dlye],edx
  3361.         call  calculatescreen
  3362.  
  3363.         mov   [edi + WDATA.fl_redraw], 1 ; flag the process as redraw
  3364.  
  3365.         mov   eax,edi               ; redraw screen at old position
  3366.         xor   esi,esi
  3367.         call  redrawscreen
  3368.  
  3369.         mov   [DONT_DRAW_MOUSE],byte 0 ; mouse pointer
  3370.         mov   [MOUSE_BACKGROUND],byte 0 ; no mouse under
  3371.         mov   [MOUSE_DOWN],byte 0 ; react to mouse up/down
  3372.  
  3373.         call  [draw_pointer]
  3374.  
  3375.         mov   [window_move_pr],0
  3376.  
  3377.       .window_move_return:
  3378.  
  3379.         ret
  3380.  
  3381. uglobal
  3382.   window_move_pr   dd  0x0
  3383.   window_move_eax  dd  0x0
  3384.   window_move_ebx  dd  0x0
  3385.   window_move_ecx  dd  0x0
  3386.   window_move_edx  dd  0x0
  3387. endg
  3388.  
  3389. ;ok - 100% work
  3390. ;nt - not tested
  3391. ;---------------------------------------------------------------------------------------------
  3392. ;eax
  3393. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3394. ;1 - change task. Ret nothing. Block. ok.
  3395. ;2 - performance control
  3396. ; ebx
  3397. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3398. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3399. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3400. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3401. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3402. ;eax
  3403. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3404. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3405. ;---------------------------------------------------------------------------------------------
  3406. sys_sheduler: ;noname & halyavin
  3407.     cmp eax,0
  3408.     je shed_counter
  3409.     cmp eax,2
  3410.     je perf_control
  3411.     cmp eax,3
  3412.     je rdmsr_instr
  3413.     cmp eax,4
  3414.     je wrmsr_instr
  3415.     cmp eax,1
  3416.     jne not_supported
  3417.     call change_task ;delay,0
  3418. ret
  3419. shed_counter:
  3420.     mov eax,[context_counter]
  3421.     mov [esp+36],eax
  3422. not_supported:
  3423. ret
  3424. perf_control:
  3425.     inc eax ;now eax=3
  3426.     cmp ebx,eax
  3427.     je cache_disable
  3428.     dec eax
  3429.     cmp ebx,eax
  3430.     je cache_enable
  3431.     dec eax
  3432.     cmp ebx,eax
  3433.     je is_cache_enabled
  3434.     dec eax
  3435.     cmp ebx,eax
  3436.     je modify_pce
  3437. ret
  3438.  
  3439. rdmsr_instr:
  3440. ;now counter in ecx
  3441. ;(edx:eax) esi:edi => edx:esi
  3442. mov eax,esi
  3443. rdmsr
  3444. mov [esp+36],eax
  3445. mov [esp+24],edx ;ret in ebx?
  3446. ret
  3447.  
  3448. wrmsr_instr:
  3449. ;now counter in ecx
  3450. ;(edx:eax) esi:edi => edx:esi
  3451.         ; Fast Call MSR can't be destroy
  3452.         ; Íî MSR_AMD_EFER ìîæíî èçìåíÿòü, ò.ê. â ýòîì ðåãèñòðå ëèø
  3453.         ; âêëþ÷àþòñÿ/âûêëþ÷àþòñÿ ðàñøèðåííûå âîçìîæíîñòè
  3454.         cmp     ecx, MSR_SYSENTER_CS
  3455.         je      @f
  3456.         cmp     ecx, MSR_SYSENTER_ESP
  3457.         je      @f
  3458.         cmp     ecx, MSR_SYSENTER_EIP
  3459.         je      @f
  3460.         cmp     ecx, MSR_AMD_STAR
  3461.         je      @f
  3462.  
  3463.         mov     eax, esi
  3464.         wrmsr
  3465.         ; mov   [esp + 36], eax
  3466.         ; mov   [esp + 24], edx ;ret in ebx?
  3467. @@:
  3468. ret
  3469.  
  3470. cache_disable:
  3471.        mov eax,cr0
  3472.        or  eax,01100000000000000000000000000000b
  3473.        mov cr0,eax
  3474.        wbinvd ;set MESI
  3475. ret
  3476.  
  3477. cache_enable:
  3478.        mov eax,cr0
  3479.        and eax,10011111111111111111111111111111b
  3480.        mov cr0,eax
  3481. ret
  3482.  
  3483. is_cache_enabled:
  3484.        mov eax,cr0
  3485.        mov ebx,eax
  3486.        and eax,01100000000000000000000000000000b
  3487.        jz cache_disabled
  3488.        mov [esp+36],ebx
  3489. cache_disabled:
  3490.        mov dword [esp+36],eax ;0
  3491. ret
  3492.  
  3493. modify_pce:
  3494.        mov eax,cr4
  3495. ;       mov ebx,0
  3496. ;       or  bx,100000000b ;pce
  3497. ;       xor eax,ebx ;invert pce
  3498.        bts eax,8 ;pce=cr4[8]
  3499.        mov cr4,eax
  3500.        mov [esp+36],eax
  3501. ret
  3502. ;---------------------------------------------------------------------------------------------
  3503.  
  3504.  
  3505. ; check if pixel is allowed to be drawn
  3506.  
  3507. checkpixel:
  3508.         push eax edx
  3509.  
  3510.         mov  edx,[ScreenWidth]     ; screen x size
  3511.         inc  edx
  3512.         imul edx, ebx
  3513.         mov  dl, [eax+edx+display_data] ; lea eax, [...]
  3514.  
  3515.         xor  ecx, ecx
  3516.         mov  eax, [CURRENT_TASK]
  3517.         cmp  al, dl
  3518.         setne cl
  3519.  
  3520.         pop  edx eax
  3521.         ret
  3522.  
  3523. iglobal
  3524.   cpustring db 'CPU',0
  3525. endg
  3526.  
  3527. uglobal
  3528. background_defined    db    0    ; diamond, 11.04.2006
  3529. endg
  3530.  
  3531. align 4
  3532. ; check misc
  3533.  
  3534. checkmisc:
  3535.  
  3536.     cmp   [ctrl_alt_del], 1
  3537.     jne   nocpustart
  3538.  
  3539.         mov     ebp, cpustring
  3540.         call    fs_execute_from_sysdir
  3541.  
  3542.     mov   [ctrl_alt_del], 0
  3543.  
  3544. nocpustart:
  3545.     cmp   [mouse_active], 1
  3546.     jne   mouse_not_active
  3547.     mov   [mouse_active], 0
  3548.     xor   edi, edi
  3549.     mov   ecx,  [TASK_COUNT]
  3550. set_mouse_event:
  3551.     add   edi, 256
  3552.     or    [edi+SLOT_BASE+APPDATA.event_mask], dword 100000b
  3553.     loop  set_mouse_event
  3554.  
  3555. mouse_not_active:
  3556.     cmp   [REDRAW_BACKGROUND],byte 0               ; background update ?
  3557.     jz    nobackgr
  3558.     cmp    [background_defined], 0
  3559.     jz    nobackgr
  3560. ;    mov   [REDRAW_BACKGROUND],byte 2
  3561. ;    call  change_task
  3562.     xor   edi, edi
  3563.     mov   ecx,  [TASK_COUNT]
  3564. set_bgr_event:
  3565.     add   edi, 256
  3566.     or    [edi+SLOT_BASE+APPDATA.event_mask], 16
  3567.     loop  set_bgr_event
  3568.     mov   [draw_data+32 + RECT.left],dword 0
  3569.     mov   [draw_data+32 + RECT.top],dword 0
  3570.     mov   eax,[ScreenWidth]
  3571.     mov   ebx,[ScreenHeight]
  3572.     mov   [draw_data+32 + RECT.right],eax
  3573.     mov   [draw_data+32 + RECT.bottom],ebx
  3574.     call  drawbackground
  3575.     mov   [REDRAW_BACKGROUND],byte 0
  3576.     mov   [MOUSE_BACKGROUND],byte 0
  3577.  
  3578. nobackgr:
  3579.  
  3580.     ; system shutdown request
  3581.  
  3582.     cmp  [SYS_SHUTDOWN],byte 0
  3583.     je   noshutdown
  3584.  
  3585.     mov  edx,[shutdown_processes]
  3586.     sub  dl,2
  3587.  
  3588.     cmp  [SYS_SHUTDOWN],dl
  3589.     jne  no_mark_system_shutdown
  3590.  
  3591.     mov   edx,OS_BASE+0x3040
  3592.     movzx ecx,byte [SYS_SHUTDOWN]
  3593.     add   ecx,5
  3594. markz:
  3595.     mov   [edx+TASKDATA.state],byte 3
  3596.     add   edx,0x20
  3597.     loop  markz
  3598.  
  3599.   no_mark_system_shutdown:
  3600.  
  3601.     call [disable_mouse]
  3602.  
  3603.     dec  byte [SYS_SHUTDOWN]
  3604.  
  3605.     cmp  [SYS_SHUTDOWN],byte 0
  3606.     je   system_shutdown
  3607.  
  3608. noshutdown:
  3609.  
  3610.  
  3611.     mov   eax,[TASK_COUNT]                  ; termination
  3612.     mov   ebx,TASK_DATA+TASKDATA.state
  3613.     mov   esi,1
  3614.  
  3615. newct:
  3616.     mov   cl,[ebx]
  3617.     cmp   cl,byte 3
  3618.     jz    terminate
  3619.     cmp   cl,byte 4
  3620.     jz    terminate
  3621.  
  3622.     add   ebx,0x20
  3623.     inc   esi
  3624.     dec   eax
  3625.     jnz   newct
  3626.  
  3627.     ret
  3628.  
  3629.  
  3630. ; redraw screen
  3631.  
  3632. redrawscreen:
  3633.  
  3634. ; eax , if process window_data base is eax, do not set flag/limits
  3635.  
  3636.          pushad
  3637.          push  eax
  3638.  
  3639. ;;;         mov   eax,2
  3640. ;;;         call  delay_hs
  3641.  
  3642.          ;mov   ecx,0               ; redraw flags for apps
  3643.          xor   ecx,ecx
  3644.        newdw2:
  3645.  
  3646.          inc   ecx
  3647.          push  ecx
  3648.  
  3649.          mov   eax,ecx
  3650.          shl   eax,5
  3651.          add   eax,window_data
  3652.  
  3653.          cmp   eax,[esp+4]
  3654.          je    not_this_task
  3655.                                    ; check if window in redraw area
  3656.          mov   edi,eax
  3657.  
  3658.          cmp   ecx,1               ; limit for background
  3659.          jz    bgli
  3660.  
  3661.          mov   eax, [edi + WDATA.box.left]
  3662.          mov   ebx, [edi + WDATA.box.top]
  3663.          mov   ecx, [edi + WDATA.box.width]
  3664.          mov   edx, [edi + WDATA.box.height]
  3665.          add   ecx,eax
  3666.          add   edx,ebx
  3667.  
  3668.          mov   ecx,[dlye]   ; ecx = area y end     ebx = window y start
  3669.          cmp   ecx,ebx
  3670.          jb    ricino
  3671.  
  3672.          mov   ecx,[dlxe]   ; ecx = area x end     eax = window x start
  3673.          cmp   ecx,eax
  3674.          jb    ricino
  3675.  
  3676.          mov   eax, [edi + WDATA.box.left]
  3677.          mov   ebx, [edi + WDATA.box.top]
  3678.          mov   ecx, [edi + WDATA.box.width]
  3679.          mov   edx, [edi + WDATA.box.height]
  3680.          add   ecx, eax
  3681.          add   edx, ebx
  3682.  
  3683.          mov   eax,[dly]    ; eax = area y start     edx = window y end
  3684.          cmp   edx,eax
  3685.          jb    ricino
  3686.  
  3687.          mov   eax,[dlx]    ; eax = area x start     ecx = window x end
  3688.          cmp   ecx,eax
  3689.          jb    ricino
  3690.  
  3691.         bgli:
  3692.  
  3693.          cmp   edi,esi
  3694.          jz    ricino
  3695.  
  3696.          mov   eax,edi
  3697.          add   eax,draw_data-window_data
  3698.  
  3699.          mov   ebx,[dlx]          ; set limits
  3700.          mov   [eax + RECT.left], ebx
  3701.          mov   ebx,[dly]
  3702.          mov   [eax + RECT.top], ebx
  3703.          mov   ebx,[dlxe]
  3704.          mov   [eax + RECT.right], ebx
  3705.          mov   ebx,[dlye]
  3706.          mov   [eax + RECT.bottom], ebx
  3707.  
  3708.          sub   eax,draw_data-window_data
  3709.  
  3710.          cmp   ecx,1
  3711.          jne   nobgrd
  3712.          cmp   esi,1
  3713.          je    newdw8
  3714.          call  drawbackground
  3715.  
  3716.        newdw8:
  3717.        nobgrd:
  3718.  
  3719.          mov   [eax + WDATA.fl_redraw],byte 1    ; mark as redraw
  3720.  
  3721.        ricino:
  3722.  
  3723.        not_this_task:
  3724.  
  3725.          pop   ecx
  3726.  
  3727.          cmp   ecx,[TASK_COUNT]
  3728.          jle   newdw2
  3729.  
  3730.          pop  eax
  3731.          popad
  3732.  
  3733.          ret
  3734.  
  3735. calculatebackground:   ; background
  3736.  
  3737.         ; all black
  3738.  
  3739.         mov   edi, [img_background]  ;IMG_BACKGROUND                 ; set background to black
  3740.         xor   eax, eax
  3741.         mov   ecx, 1023    ;0x0fff00 / 4
  3742.         cld
  3743.         rep   stosd
  3744.  
  3745.         mov   edi,display_data              ; set os to use all pixels
  3746.         mov   eax,0x01010101
  3747.         mov   ecx,1280*1024 / 4
  3748.         rep   stosd
  3749.  
  3750.         mov   byte [REDRAW_BACKGROUND], 0              ; do not draw background!
  3751.  
  3752.         ret
  3753.  
  3754. uglobal
  3755.   imax    dd 0x0
  3756. endg
  3757.  
  3758.  
  3759.  
  3760. delay_ms:     ; delay in 1/1000 sec
  3761.  
  3762.  
  3763.         push  eax
  3764.         push  ecx
  3765.  
  3766.         mov   ecx,esi
  3767.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3768.         imul  ecx, 33941
  3769.         shr   ecx, 9
  3770.         ; </CPU clock fix>
  3771.  
  3772.         in    al,0x61
  3773.         and   al,0x10
  3774.         mov   ah,al
  3775.         cld
  3776.  
  3777.  cnt1:  in    al,0x61
  3778.         and   al,0x10
  3779.         cmp   al,ah
  3780.         jz    cnt1
  3781.  
  3782.         mov   ah,al
  3783.         loop  cnt1
  3784.  
  3785.         pop   ecx
  3786.         pop   eax
  3787.  
  3788.         ret
  3789.  
  3790.  
  3791. set_app_param:
  3792.         push edi
  3793.  
  3794.         mov  edi,[TASK_BASE]
  3795.         mov  [edi+TASKDATA.event_mask],eax
  3796.  
  3797.         pop  edi
  3798.         ret
  3799.  
  3800.  
  3801.  
  3802. delay_hs:     ; delay in 1/100 secs
  3803.         push  eax
  3804.         push  ecx
  3805.         push  edx
  3806.  
  3807.         mov   edx,[timer_ticks]
  3808.         add   edx,eax
  3809.  
  3810.       newtic:
  3811.         mov   ecx,[timer_ticks]
  3812.         cmp   edx,ecx
  3813.         jbe   zerodelay
  3814.  
  3815.         call  change_task
  3816.  
  3817.         jmp   newtic
  3818.  
  3819.       zerodelay:
  3820.         pop   edx
  3821.         pop   ecx
  3822.         pop   eax
  3823.  
  3824.         ret
  3825.  
  3826.  
  3827. memmove:       ; memory move in bytes
  3828.  
  3829. ; eax = from
  3830. ; ebx = to
  3831. ; ecx = no of bytes
  3832.     test ecx, ecx
  3833.     jle  .ret
  3834.  
  3835.  
  3836.     push esi edi ecx
  3837.  
  3838.     mov  edi, ebx
  3839.     mov  esi, eax
  3840.  
  3841.     test ecx, not 11b
  3842.     jz   @f
  3843.  
  3844.     push ecx
  3845.     shr  ecx, 2
  3846.     rep  movsd
  3847.     pop  ecx
  3848.     and  ecx, 11b
  3849.     jz   .finish
  3850.   @@:
  3851.     rep  movsb
  3852.  
  3853.   .finish:
  3854.     pop  ecx edi esi
  3855.   .ret:
  3856.     ret
  3857.  
  3858.  
  3859. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3860. ;align 4
  3861. ;
  3862. ;read_floppy_file:
  3863. ;
  3864. ;; as input
  3865. ;;
  3866. ;; eax pointer to file
  3867. ;; ebx file lenght
  3868. ;; ecx start 512 byte block number
  3869. ;; edx number of blocks to read
  3870. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3871. ;;
  3872. ;;
  3873. ;; on return
  3874. ;;
  3875. ;; eax = 0 command succesful
  3876. ;;       1 no fd base and/or partition defined
  3877. ;;       2 yet unsupported FS
  3878. ;;       3 unknown FS
  3879. ;;       4 partition not defined at hd
  3880. ;;       5 file not found
  3881. ;; ebx = size of file
  3882. ;
  3883. ;     mov   edi,[TASK_BASE]
  3884. ;     add   edi,0x10
  3885. ;     add   esi,[edi]
  3886. ;     add   eax,[edi]
  3887. ;
  3888. ;     pushad
  3889. ;     mov  edi,esi
  3890. ;     add  edi,1024
  3891. ;     mov  esi,0x100000+19*512
  3892. ;     sub  ecx,1
  3893. ;     shl  ecx,9
  3894. ;     add  esi,ecx
  3895. ;     shl  edx,9
  3896. ;     mov  ecx,edx
  3897. ;     cld
  3898. ;     rep  movsb
  3899. ;     popad
  3900. ;
  3901. ;     mov   [esp+36],eax
  3902. ;     mov   [esp+24],ebx
  3903. ;     ret
  3904.  
  3905.  
  3906.  
  3907. align 4
  3908.  
  3909. sys_programirq:
  3910.  
  3911.     mov   edi,[TASK_BASE]
  3912.     add   eax,[edi+TASKDATA.mem_start]
  3913.  
  3914.     cmp   ebx,16
  3915.     jae   .not_owner
  3916.     mov   edi,[TASK_BASE]
  3917.     mov   edi,[edi+TASKDATA.pid]
  3918.     cmp   edi,[irq_owner+ebx*4]
  3919.     je    spril1
  3920. .not_owner:
  3921.     mov   [esp+36],dword 1
  3922.     ret
  3923.   spril1:
  3924.  
  3925.     mov   esi,eax
  3926.     shl   ebx,6
  3927.     add   ebx,irq00read
  3928.     mov   edi,ebx
  3929.     mov   ecx,16
  3930.     cld
  3931.     rep   movsd
  3932.     mov   [esp+36],dword 0
  3933.     ret
  3934.  
  3935.  
  3936. align 4
  3937.  
  3938. get_irq_data:
  3939.      cmp   eax,16
  3940.      jae   .not_owner
  3941.      mov   edx,eax           ; check for correct owner
  3942.      shl   edx,2
  3943.      add   edx,irq_owner
  3944.      mov   edx,[edx]
  3945.      mov   edi,[TASK_BASE]
  3946.      mov   edi,[edi+TASKDATA.pid]
  3947.      cmp   edx,edi
  3948.      je    gidril1
  3949. .not_owner:
  3950.      mov   [esp+32],dword 2     ; ecx=2
  3951.      ret
  3952.  
  3953.   gidril1:
  3954.  
  3955.      mov   ebx,eax
  3956.      shl   ebx,12
  3957.      add   ebx,IRQ_SAVE
  3958.      mov   eax,[ebx]
  3959.      mov   ecx,1
  3960.      test  eax,eax
  3961.      jz    gid1
  3962.  
  3963.      dec   eax
  3964.      mov   esi,ebx
  3965.      mov   [ebx],eax
  3966.      movzx ebx,byte [ebx+0x10]
  3967.      add   esi,0x10
  3968.      mov   edi,esi
  3969.      inc   esi
  3970.      mov   ecx,4000 / 4
  3971.      cld
  3972.      rep   movsd
  3973. ;     xor   ecx,ecx     ; as result of 'rep' ecx=0
  3974.    gid1:
  3975.      mov   [esp+36],eax
  3976.      mov   [esp+32],ecx
  3977.      mov   [esp+24],ebx
  3978.      ret
  3979.  
  3980.  
  3981. set_io_access_rights:
  3982.  
  3983.      pushad
  3984.  
  3985.      mov edi, tss._io_map_0
  3986.  
  3987. ;     mov   ecx,eax
  3988. ;     and   ecx,7    ; offset in byte
  3989.  
  3990. ;     shr   eax,3    ; number of byte
  3991. ;     add   edi,eax
  3992.  
  3993. ;     mov   ebx,1
  3994. ;     shl   ebx,cl
  3995.  
  3996.      cmp   ebp,0                ; enable access - ebp = 0
  3997.      jne   siar1
  3998.  
  3999. ;     not   ebx
  4000. ;     and   [edi],byte bl
  4001.      btr [edi], eax
  4002.  
  4003.      popad
  4004.  
  4005.      ret
  4006.  
  4007. siar1:
  4008.  
  4009.      bts [edi], eax
  4010.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  4011.  
  4012.      popad
  4013.  
  4014.      ret
  4015.  
  4016. r_f_port_area:
  4017.  
  4018.      test  eax, eax
  4019.      jnz   free_port_area
  4020. ;     je    r_port_area
  4021. ;     jmp   free_port_area
  4022.  
  4023. ;   r_port_area:
  4024.  
  4025.      pushad
  4026.  
  4027.      cmp   ebx,ecx            ; beginning > end ?
  4028.      ja    rpal1
  4029.      cmp   ecx,65536
  4030.      jae   rpal1
  4031.      mov   esi,[RESERVED_PORTS]
  4032.      test  esi,esi            ; no reserved areas ?
  4033.      je    rpal2
  4034.      cmp   esi,255            ; max reserved
  4035.      jae   rpal1
  4036.  rpal3:
  4037.      mov   edi,esi
  4038.      shl   edi,4
  4039.      add   edi,RESERVED_PORTS
  4040.      cmp   ebx,[edi+8]
  4041.      ja    rpal4
  4042.      cmp   ecx,[edi+4]
  4043.      jae   rpal1
  4044. ;     jb    rpal4
  4045. ;     jmp   rpal1
  4046.  rpal4:
  4047.  
  4048.      dec   esi
  4049.      jnz   rpal3
  4050.      jmp   rpal2
  4051.    rpal1:
  4052.      popad
  4053.      mov   eax,1
  4054.      ret
  4055.  
  4056.    rpal2:
  4057.      popad
  4058.  
  4059.  
  4060.      ; enable port access at port IO map
  4061.      cli
  4062.      pushad                        ; start enable io map
  4063.  
  4064.      cmp   ecx,65536 ;16384
  4065.      jae   no_unmask_io ; jge
  4066.  
  4067.      mov   eax,ebx
  4068.  
  4069.    new_port_access:
  4070.  
  4071.      pushad
  4072.  
  4073.      xor   ebp,ebp                ; enable - eax = port
  4074.      call  set_io_access_rights
  4075.  
  4076.      popad
  4077.  
  4078.      inc   eax
  4079.      cmp   eax,ecx
  4080.      jbe   new_port_access
  4081.  
  4082.    no_unmask_io:
  4083.  
  4084.      popad                         ; end enable io map
  4085.      sti
  4086.  
  4087.      mov   edi,[RESERVED_PORTS]
  4088.      add   edi,1
  4089.      mov   [RESERVED_PORTS],edi
  4090.      shl   edi,4
  4091.      add   edi,RESERVED_PORTS
  4092.      mov   esi,[TASK_BASE]
  4093.      mov   esi,[esi+TASKDATA.pid]
  4094.      mov   [edi],esi
  4095.      mov   [edi+4],ebx
  4096.      mov   [edi+8],ecx
  4097.  
  4098.      xor   eax, eax
  4099.      ret
  4100.  
  4101. free_port_area:
  4102.  
  4103.      pushad
  4104.  
  4105.      mov   esi,[RESERVED_PORTS]     ; no reserved areas ?
  4106.      test  esi,esi
  4107.      je    frpal2
  4108.      mov   edx,[TASK_BASE]
  4109.      mov   edx,[edx+TASKDATA.pid]
  4110.    frpal3:
  4111.      mov   edi,esi
  4112.      shl   edi,4
  4113.      add   edi,RESERVED_PORTS
  4114.      cmp   edx,[edi]
  4115.      jne   frpal4
  4116.      cmp   ebx,[edi+4]
  4117.      jne   frpal4
  4118.      cmp   ecx,[edi+8]
  4119.      jne   frpal4
  4120.      jmp   frpal1
  4121.    frpal4:
  4122.      dec   esi
  4123.      jnz   frpal3
  4124.    frpal2:
  4125.      popad
  4126.      mov   eax,1
  4127.      ret
  4128.    frpal1:
  4129.      mov   ecx,256
  4130.      sub   ecx,esi
  4131.      shl   ecx,4
  4132.      mov   esi,edi
  4133.      add   esi,16
  4134.      cld
  4135.      rep   movsb
  4136.  
  4137.      dec   dword [RESERVED_PORTS]
  4138.  
  4139.      popad
  4140.  
  4141.  
  4142.      ; disable port access at port IO map
  4143.  
  4144.      pushad                        ; start disable io map
  4145.  
  4146.      cmp   ecx,65536 ;16384
  4147.      jge   no_mask_io
  4148.  
  4149.      mov   eax,ebx
  4150.  
  4151.    new_port_access_disable:
  4152.  
  4153.      pushad
  4154.  
  4155.      mov   ebp,1                  ; disable - eax = port
  4156.      call  set_io_access_rights
  4157.  
  4158.      popad
  4159.  
  4160.      inc   eax
  4161.      cmp   eax,ecx
  4162.      jbe   new_port_access_disable
  4163.  
  4164.    no_mask_io:
  4165.  
  4166.      popad                         ; end disable io map
  4167.  
  4168.      xor   eax, eax
  4169.      ret
  4170.  
  4171.  
  4172. reserve_free_irq:
  4173.  
  4174.      mov   ecx, 1
  4175.      cmp   ebx, 16
  4176.      jae   fril1
  4177.      test  eax,eax
  4178.      jz    reserve_irq
  4179.  
  4180.      lea   edi,[irq_owner+ebx*4]
  4181.      mov   edx,[edi]
  4182.      mov   eax,[TASK_BASE]
  4183.      cmp   edx,[eax+TASKDATA.pid]
  4184.      jne   fril1
  4185.      dec   ecx
  4186.      mov   [edi],ecx
  4187.    fril1:
  4188.      mov   [esp+36],ecx ; return in eax
  4189.      ret
  4190.  
  4191.   reserve_irq:
  4192.  
  4193.      lea   edi,[irq_owner+ebx*4]
  4194.      cmp   dword [edi], 0
  4195.      jnz   ril1
  4196.  
  4197.      mov   edx,[TASK_BASE]
  4198.      mov   edx,[edx+TASKDATA.pid]
  4199.      mov   [edi],edx
  4200.      dec   ecx
  4201.    ril1:
  4202.      mov   [esp+36],ecx ; return in eax
  4203.      ret
  4204.  
  4205. drawbackground:
  4206.        inc   [mouse_pause]
  4207.        cmp   [SCR_MODE],word 0x12
  4208.        je   dbrv20
  4209.      dbrv12:
  4210.        cmp  [SCR_MODE],word 0100000000000000b
  4211.        jge  dbrv20
  4212.        cmp  [SCR_MODE],word 0x13
  4213.        je   dbrv20
  4214.        call  vesa12_drawbackground
  4215.        dec   [mouse_pause]
  4216.        call   [draw_pointer]
  4217.        ret
  4218.      dbrv20:
  4219.        cmp   [BgrDrawMode],dword 1
  4220.        jne   bgrstr
  4221.        call  vesa20_drawbackground_tiled
  4222.        dec   [mouse_pause]
  4223.        call   [draw_pointer]
  4224.        ret
  4225.      bgrstr:
  4226.        call  vesa20_drawbackground_stretch
  4227.        dec   [mouse_pause]
  4228.        call   [draw_pointer]
  4229.        ret
  4230.  
  4231. align 4
  4232.  
  4233. syscall_putimage:                       ; PutImage
  4234.  
  4235.      mov   edx,ecx
  4236.      mov   ecx,ebx
  4237.      mov   ebx, eax
  4238.  
  4239. sys_putimage:
  4240.      test  ecx,0x80008000
  4241.      jnz   .exit
  4242.      test  ecx,0x0000FFFF
  4243.      jz    .exit
  4244.      test  ecx,0xFFFF0000
  4245.      jnz   @f
  4246.   .exit:
  4247.      ret
  4248.  @@:
  4249.         mov     edi,[current_slot]
  4250.         add     dx,word[edi+APPDATA.wnd_clientbox.top]
  4251.         rol     edx,16
  4252.         add     dx,word[edi+APPDATA.wnd_clientbox.left]
  4253.         rol     edx,16
  4254.   .forced:
  4255.         push    ebp esi 0
  4256.         mov     ebp, putimage_get24bpp
  4257.         mov     esi, putimage_init24bpp
  4258. sys_putimage_bpp:
  4259. ;        call    [disable_mouse] ; this will be done in xxx_putimage
  4260. ;        mov     eax, vga_putimage
  4261.         cmp     [SCR_MODE], word 0x12
  4262.         jz      @f   ;.doit
  4263.         mov     eax, vesa12_putimage
  4264.         cmp     [SCR_MODE], word 0100000000000000b
  4265.         jae     @f
  4266.         cmp     [SCR_MODE], word 0x13
  4267.         jnz     .doit
  4268. @@:
  4269.         mov     eax, vesa20_putimage
  4270. .doit:
  4271.         inc     [mouse_pause]
  4272.         call    eax
  4273.         dec     [mouse_pause]
  4274.         pop     ebp esi ebp
  4275.         jmp     [draw_pointer]
  4276.  
  4277. syscall_putimage_palette:
  4278.         mov     edi, esi
  4279.         mov     esi, edx
  4280.         mov     edx, ecx
  4281.         mov     ecx, ebx
  4282.         mov     ebx, eax
  4283. sys_putimage_palette:
  4284. ; ebx = pointer to image
  4285. ; ecx = [xsize]*65536 + [ysize]
  4286. ; edx = [xstart]*65536 + [ystart]
  4287. ; esi = number of bits per pixel, must be 8, 24 or 32
  4288. ; edi = pointer to palette
  4289. ; ebp = row delta
  4290.         mov     eax, [CURRENT_TASK]
  4291.         shl     eax, 8
  4292.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4293.         rol     edx, 16
  4294.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4295.         rol     edx, 16
  4296. .forced:
  4297.         push    ebp esi ebp
  4298.         cmp     esi, 8
  4299.         jnz     @f
  4300.         mov     ebp, putimage_get8bpp
  4301.         mov     esi, putimage_init8bpp
  4302.         jmp     sys_putimage_bpp
  4303. @@:
  4304.         cmp     esi, 24
  4305.         jnz     @f
  4306.         mov     ebp, putimage_get24bpp
  4307.         mov     esi, putimage_init24bpp
  4308.         jmp     sys_putimage_bpp
  4309. @@:
  4310.         cmp     esi, 32
  4311.         jnz     @f
  4312.         mov     ebp, putimage_get32bpp
  4313.         mov     esi, putimage_init32bpp
  4314.         jmp     sys_putimage_bpp
  4315. @@:
  4316.         pop     ebp esi
  4317.         ret
  4318.  
  4319. putimage_init24bpp:
  4320.         lea     eax, [eax*3]
  4321. putimage_init8bpp:
  4322.         ret
  4323.  
  4324. putimage_get24bpp:
  4325.         mov     eax, [esi]
  4326.         add     esi, 3
  4327.         ret     4
  4328. putimage_get8bpp:
  4329.         movzx   eax, byte [esi]
  4330.         push    edx
  4331.         mov     edx, [esp+8]
  4332.         mov     eax, [edx+eax*4]
  4333.         pop     edx
  4334.         inc     esi
  4335.         ret     4
  4336.  
  4337. putimage_init32bpp:
  4338.         shl     eax, 2
  4339.         ret
  4340. putimage_get32bpp:
  4341.         lodsd
  4342.         ret     4
  4343.  
  4344. ; eax x beginning
  4345. ; ebx y beginning
  4346. ; ecx x end
  4347.         ; edx y end
  4348. ; edi color
  4349.  
  4350. __sys_drawbar:
  4351.         mov     esi,[current_slot]
  4352.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  4353.         add     ecx,[esi+APPDATA.wnd_clientbox.left]
  4354.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  4355.         add     edx,[esi+APPDATA.wnd_clientbox.top]
  4356.   .forced:
  4357.     inc   [mouse_pause]
  4358. ;        call    [disable_mouse]
  4359.     cmp   [SCR_MODE],word 0x12
  4360.     je   dbv20
  4361.    sdbv20:
  4362.     cmp  [SCR_MODE],word 0100000000000000b
  4363.     jge  dbv20
  4364.     cmp  [SCR_MODE],word 0x13
  4365.     je   dbv20
  4366.     call vesa12_drawbar
  4367.     dec   [mouse_pause]
  4368.     call   [draw_pointer]
  4369.     ret
  4370.   dbv20:
  4371.     call vesa20_drawbar
  4372.     dec   [mouse_pause]
  4373.     call   [draw_pointer]
  4374.     ret
  4375.  
  4376.  
  4377.  
  4378. kb_read:
  4379.  
  4380.         push    ecx edx
  4381.  
  4382.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4383.       kr_loop:
  4384.         in      al,0x64
  4385.         test    al,1
  4386.         jnz     kr_ready
  4387.         loop    kr_loop
  4388.         mov     ah,1
  4389.         jmp     kr_exit
  4390.       kr_ready:
  4391.         push    ecx
  4392.         mov     ecx,32
  4393.       kr_delay:
  4394.         loop    kr_delay
  4395.         pop     ecx
  4396.         in      al,0x60
  4397.         xor     ah,ah
  4398.       kr_exit:
  4399.  
  4400.         pop     edx ecx
  4401.  
  4402.         ret
  4403.  
  4404.  
  4405. kb_write:
  4406.  
  4407.         push    ecx edx
  4408.  
  4409.         mov     dl,al
  4410. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4411. ;      kw_loop1:
  4412. ;        in      al,0x64
  4413. ;        test    al,0x20
  4414. ;        jz      kw_ok1
  4415. ;        loop    kw_loop1
  4416. ;        mov     ah,1
  4417. ;        jmp     kw_exit
  4418. ;      kw_ok1:
  4419.         in      al,0x60
  4420.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4421.       kw_loop:
  4422.         in      al,0x64
  4423.         test    al,2
  4424.         jz      kw_ok
  4425.         loop    kw_loop
  4426.         mov     ah,1
  4427.         jmp     kw_exit
  4428.       kw_ok:
  4429.         mov     al,dl
  4430.         out     0x60,al
  4431.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4432.       kw_loop3:
  4433.         in      al,0x64
  4434.         test    al,2
  4435.         jz      kw_ok3
  4436.         loop    kw_loop3
  4437.         mov     ah,1
  4438.         jmp     kw_exit
  4439.       kw_ok3:
  4440.         mov     ah,8
  4441.       kw_loop4:
  4442.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4443.       kw_loop5:
  4444.         in      al,0x64
  4445.         test    al,1
  4446.         jnz     kw_ok4
  4447.         loop    kw_loop5
  4448.         dec     ah
  4449.         jnz     kw_loop4
  4450.       kw_ok4:
  4451.         xor     ah,ah
  4452.       kw_exit:
  4453.  
  4454.         pop     edx ecx
  4455.  
  4456.         ret
  4457.  
  4458.  
  4459. kb_cmd:
  4460.  
  4461.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4462.       c_wait:
  4463.         in      al,0x64
  4464.         test    al,2
  4465.         jz      c_send
  4466.         loop    c_wait
  4467.         jmp     c_error
  4468.       c_send:
  4469.         mov     al,bl
  4470.         out     0x64,al
  4471.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4472.       c_accept:
  4473.         in      al,0x64
  4474.         test    al,2
  4475.         jz      c_ok
  4476.         loop    c_accept
  4477.       c_error:
  4478.         mov     ah,1
  4479.         jmp     c_exit
  4480.       c_ok:
  4481.         xor     ah,ah
  4482.       c_exit:
  4483.         ret
  4484.  
  4485.  
  4486. setmouse:  ; set mousepicture -pointer
  4487.            ; ps2 mouse enable
  4488.  
  4489.      mov     [MOUSE_PICTURE],dword mousepointer
  4490.  
  4491.      cli
  4492. ;     mov     bl,0xa8                 ; enable mouse cmd
  4493. ;     call    kb_cmd
  4494. ;     call    kb_read                 ; read status
  4495. ;     mov     bl,0x20                 ; get command byte
  4496. ;     call    kb_cmd
  4497. ;     call    kb_read
  4498. ;     or      al,3                    ; enable interrupt
  4499. ;     mov     bl,0x60                 ; write command
  4500. ;     push    eax
  4501. ;     call    kb_cmd
  4502. ;     pop     eax
  4503. ;     call    kb_write
  4504. ;     mov     bl,0xd4                 ; for mouse
  4505. ;     call    kb_cmd
  4506. ;     mov     al,0xf4                 ; enable mouse device
  4507. ;     call    kb_write
  4508. ;     call    kb_read           ; read status return
  4509.  
  4510.      ; com1 mouse enable
  4511.  
  4512.      mov   bx,0x3f8 ; combase
  4513.  
  4514.      mov   dx,bx
  4515.      add   dx,3
  4516.      mov   al,0x80
  4517.      out   dx,al
  4518.  
  4519.      mov   dx,bx
  4520.      add   dx,1
  4521.      mov   al,0
  4522.      out   dx,al
  4523.  
  4524.      mov   dx,bx
  4525.      add   dx,0
  4526.      mov   al,0x30*2    ; 0x30 / 4
  4527.      out   dx,al
  4528.  
  4529.      mov   dx,bx
  4530.      add   dx,3
  4531.      mov   al,2         ; 3
  4532.      out   dx,al
  4533.  
  4534.      mov   dx,bx
  4535.      add   dx,4
  4536.      mov   al,0xb
  4537.      out   dx,al
  4538.  
  4539.      mov   dx,bx
  4540.      add   dx,1
  4541.      mov   al,1
  4542.      out   dx,al
  4543.  
  4544.  
  4545.      ; com2 mouse enable
  4546.  
  4547.      mov   bx,0x2f8 ; combase
  4548.  
  4549.      mov   dx,bx
  4550.      add   dx,3
  4551.      mov   al,0x80
  4552.      out   dx,al
  4553.  
  4554.      mov   dx,bx
  4555.      add   dx,1
  4556.      mov   al,0
  4557.      out   dx,al
  4558.  
  4559.      mov   dx,bx
  4560.      add   dx,0
  4561.      mov   al,0x30*2
  4562.      out   dx,al
  4563.  
  4564.      mov   dx,bx
  4565.      add   dx,3
  4566.      mov   al,2
  4567.      out   dx,al
  4568.  
  4569.      mov   dx,bx
  4570.      add   dx,4
  4571.      mov   al,0xb
  4572.      out   dx,al
  4573.  
  4574.      mov   dx,bx
  4575.      add   dx,1
  4576.      mov   al,1
  4577.      out   dx,al
  4578.  
  4579.      ret
  4580.  
  4581.  
  4582. _rdtsc:
  4583.      bt [cpu_caps], CAPS_TSC
  4584.      jnc ret_rdtsc
  4585.      rdtsc
  4586.      ret
  4587.    ret_rdtsc:
  4588.      mov   edx,0xffffffff
  4589.      mov   eax,0xffffffff
  4590.      ret
  4591.  
  4592. rerouteirqs:
  4593.  
  4594.         cli
  4595.  
  4596.         mov     al,0x11         ;  icw4, edge triggered
  4597.         out     0x20,al
  4598.         call    pic_delay
  4599.         out     0xA0,al
  4600.         call    pic_delay
  4601.  
  4602.         mov     al,0x20         ;  generate 0x20 +
  4603.         out     0x21,al
  4604.         call    pic_delay
  4605.         mov     al,0x28         ;  generate 0x28 +
  4606.         out     0xA1,al
  4607.         call    pic_delay
  4608.  
  4609.         mov     al,0x04         ;  slave at irq2
  4610.         out     0x21,al
  4611.         call    pic_delay
  4612.         mov     al,0x02         ;  at irq9
  4613.         out     0xA1,al
  4614.         call    pic_delay
  4615.  
  4616.         mov     al,0x01         ;  8086 mode
  4617.         out     0x21,al
  4618.         call    pic_delay
  4619.         out     0xA1,al
  4620.         call    pic_delay
  4621.  
  4622.         mov     al,255          ; mask all irq's
  4623.         out     0xA1,al
  4624.         call    pic_delay
  4625.         out     0x21,al
  4626.         call    pic_delay
  4627.  
  4628.         mov     ecx,0x1000
  4629.         cld
  4630. picl1:  call    pic_delay
  4631.         loop    picl1
  4632.  
  4633.         mov     al,255          ; mask all irq's
  4634.         out     0xA1,al
  4635.         call    pic_delay
  4636.         out     0x21,al
  4637.         call    pic_delay
  4638.  
  4639.         cli
  4640.  
  4641.         ret
  4642.  
  4643.  
  4644. pic_delay:
  4645.  
  4646.         jmp     pdl1
  4647. pdl1:   ret
  4648.  
  4649.  
  4650. sys_msg_board_str:
  4651.  
  4652.      pushad
  4653.    @@:
  4654.      cmp    [esi],byte 0
  4655.      je     @f
  4656.      mov    eax,1
  4657.      movzx  ebx,byte [esi]
  4658.      call   sys_msg_board
  4659.      inc    esi
  4660.      jmp    @b
  4661.    @@:
  4662.      popad
  4663.      ret
  4664.  
  4665. uglobal
  4666.   msg_board_data: times 4096 db 0
  4667.   msg_board_count dd 0x0
  4668. endg
  4669.  
  4670. sys_msg_board:
  4671.  
  4672. ; eax=1 : write :  bl byte to write
  4673. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4674.  
  4675.      mov  ecx,[msg_board_count]
  4676.      cmp  eax, 1
  4677.      jne  smbl1
  4678.  
  4679.  
  4680.      mov  [msg_board_data+ecx],bl
  4681.      inc  ecx
  4682.      and  ecx, 4095
  4683.      mov  [msg_board_count], ecx
  4684.      mov  [check_idle_semaphore], 5
  4685.      ret
  4686.    smbl1:
  4687.  
  4688.      cmp   eax, 2
  4689.      jne   smbl2
  4690.      test  ecx, ecx
  4691.      jz    smbl21
  4692. ;     mov   edi, msg_board_data
  4693. ;     mov   esi, msg_board_data+1
  4694. ;     movzx eax, byte [edi]
  4695.      mov   eax, msg_board_data+1
  4696.      mov   ebx, msg_board_data
  4697.      movzx edx, byte [ebx]
  4698.      call  memmove
  4699. ;     push  ecx
  4700. ;     shr   ecx, 2
  4701. ;     cld
  4702. ;     rep   movsd
  4703. ;     pop   ecx
  4704. ;     and   ecx, 3
  4705. ;     rep   movsb
  4706.      dec   [msg_board_count]
  4707.      mov   [esp+36], edx ;eax
  4708.      mov   [esp+24], dword 1
  4709.      ret
  4710.    smbl21:
  4711.      mov   [esp+36], ecx
  4712.      mov   [esp+24], ecx
  4713.  
  4714.    smbl2:
  4715.      ret
  4716.  
  4717.  
  4718.  
  4719. sys_process_def:
  4720.         mov     edi, [CURRENT_TASK]
  4721.  
  4722.         dec     eax             ; 1 = set keyboard mode
  4723.      jne   no_set_keyboard_setup
  4724.  
  4725.      shl   edi,8
  4726.      mov   [edi+SLOT_BASE + APPDATA.keyboard_mode],bl
  4727.  
  4728.      ret
  4729.  
  4730.    no_set_keyboard_setup:
  4731.  
  4732.         dec     eax             ; 2 = get keyboard mode
  4733.      jne   no_get_keyboard_setup
  4734.  
  4735.      shl   edi,8
  4736.      movzx eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4737.  
  4738.      mov   [esp+36],eax
  4739.  
  4740.      ret
  4741.  
  4742.    no_get_keyboard_setup:
  4743.  
  4744.         dec     eax             ; 3 = get keyboard ctrl, alt, shift
  4745.      jne   no_get_keyboard_cas
  4746.  
  4747. ;     xor   eax,eax
  4748. ;     movzx eax,byte [shift]
  4749. ;     movzx ebx,byte [ctrl]
  4750. ;     shl   ebx,2
  4751. ;     add   eax,ebx
  4752. ;     movzx ebx,byte [alt]
  4753. ;     shl   ebx,3
  4754. ;     add   eax,ebx
  4755.  
  4756.  ;// mike.dld [
  4757.      mov   eax, [kb_state]
  4758.  ;// mike.dld ]
  4759.  
  4760.      mov   [esp+36],eax
  4761.  
  4762.      ret
  4763.  
  4764.    no_get_keyboard_cas:
  4765.  
  4766.         dec     eax
  4767.         jnz     no_add_keyboard_hotkey
  4768.  
  4769.         mov     eax, hotkey_list
  4770. @@:
  4771.         cmp     dword [eax+8], 0
  4772.         jz      .found_free
  4773.         add     eax, 16
  4774.         cmp     eax, hotkey_list+16*256
  4775.         jb      @b
  4776.         mov     dword [esp+36], 1
  4777.         ret
  4778. .found_free:
  4779.         mov     [eax+8], edi
  4780.         mov     [eax+4], ecx
  4781.         movzx   ebx, bl
  4782.         lea     ebx, [hotkey_scancodes+ebx*4]
  4783.         mov     ecx, [ebx]
  4784.         mov     [eax], ecx
  4785.         mov     [ebx], eax
  4786.         mov     [eax+12], ebx
  4787.         jecxz   @f
  4788.         mov     [ecx+12], eax
  4789. @@:
  4790.         and     dword [esp+36], 0
  4791.         ret
  4792.  
  4793. no_add_keyboard_hotkey:
  4794.  
  4795.         dec     eax
  4796.         jnz     no_del_keyboard_hotkey
  4797.  
  4798.         movzx   ebx, bl
  4799.         lea     ebx, [hotkey_scancodes+ebx*4]
  4800.         mov     eax, [ebx]
  4801. .scan:
  4802.         test    eax, eax
  4803.         jz      .notfound
  4804.         cmp     [eax+8], edi
  4805.         jnz     .next
  4806.         cmp     [eax+4], ecx
  4807.         jz      .found
  4808. .next:
  4809.         mov     eax, [eax]
  4810.         jmp     .scan
  4811. .notfound:
  4812.         mov     dword [esp+36], 1
  4813.         ret
  4814. .found:
  4815.         mov     ecx, [eax]
  4816.         jecxz   @f
  4817.         mov     edx, [eax+12]
  4818.         mov     [ecx+12], edx
  4819. @@:
  4820.         mov     ecx, [eax+12]
  4821.         mov     edx, [eax]
  4822.         mov     [ecx], edx
  4823.         xor     edx, edx
  4824.         mov     [eax+4], edx
  4825.         mov     [eax+8], edx
  4826.         mov     [eax+12], edx
  4827.         mov     [eax], edx
  4828.         mov     [esp+36], edx
  4829.         ret
  4830.  
  4831. no_del_keyboard_hotkey:
  4832.      ret
  4833.  
  4834.  
  4835. align 4
  4836.  
  4837. sys_gs:                         ; direct screen access
  4838.  
  4839.      cmp  eax,1                 ; resolution
  4840.      jne  no_gs1
  4841.      mov  eax,[ScreenWidth]
  4842.      shl  eax,16
  4843.      mov  ax,[ScreenHeight]
  4844.      add  eax,0x00010001
  4845.      mov  [esp+36],eax
  4846.      ret
  4847.    no_gs1:
  4848.  
  4849.      cmp   eax,2                ; bits per pixel
  4850.      jne   no_gs2
  4851.      movzx eax,byte [ScreenBPP]
  4852.      mov   [esp+36],eax
  4853.      ret
  4854.    no_gs2:
  4855.  
  4856.      cmp   eax,3                ; bytes per scanline
  4857.      jne   no_gs3
  4858.      mov   eax,[BytesPerScanLine]
  4859.      mov   [esp+36],eax
  4860.      ret
  4861.    no_gs3:
  4862.  
  4863.      mov  [esp+36],dword -1
  4864.      ret
  4865.  
  4866.  
  4867. align 4 ; PCI functions
  4868.  
  4869. sys_pci:
  4870.  
  4871.      call  pci_api
  4872.      mov   [esp+36],eax
  4873.      ret
  4874.  
  4875.  
  4876. align 4  ;  system functions
  4877.  
  4878. syscall_setpixel:                       ; SetPixel
  4879.  
  4880.  
  4881.      mov   edx,[TASK_BASE]
  4882.      add   eax,[edx-twdw+WDATA.box.left]
  4883.      add   ebx,[edx-twdw+WDATA.box.top]
  4884.         mov     edi,[current_slot]
  4885.         add     eax,[edi+APPDATA.wnd_clientbox.left]
  4886.         add     ebx,[edi+APPDATA.wnd_clientbox.top]
  4887.      xor   edi,edi ; no force
  4888. ;     mov   edi,1
  4889.      call  [disable_mouse]
  4890.      jmp   [putpixel]
  4891.  
  4892. align 4
  4893.  
  4894. syscall_writetext:                      ; WriteText
  4895.  
  4896.         mov   edi,[TASK_BASE]
  4897.         mov   ebp,[edi-twdw+WDATA.box.left]
  4898.         push  esi
  4899.         mov   esi,[current_slot]
  4900.         add   ebp,[esi+APPDATA.wnd_clientbox.left]
  4901.         shl   ebp,16
  4902.         add   ebp,[edi-twdw+WDATA.box.top]
  4903.         add   bp,word[esi+APPDATA.wnd_clientbox.top]
  4904.         pop   esi
  4905.         add   ecx,[edi+TASKDATA.mem_start]
  4906.         add   eax,ebp
  4907.         xor   edi,edi
  4908.         jmp   dtext
  4909.  
  4910. align 4
  4911.  
  4912. syscall_openramdiskfile:                ; OpenRamdiskFile
  4913.  
  4914.  
  4915.      mov   edi,[TASK_BASE]
  4916.      add   edi,TASKDATA.mem_start
  4917.      add   eax,[edi]
  4918.      add   edx,[edi]
  4919.      mov   esi,12
  4920.      call  fileread
  4921.      mov   [esp+36],ebx
  4922.      ret
  4923.  
  4924. align 4
  4925.  
  4926. syscall_drawrect:                       ; DrawRect
  4927.  
  4928.      mov   edi,ecx
  4929.      and   edi,0x80FFFFFF
  4930.      test  ax,ax
  4931.      je    drectr
  4932.      test  bx,bx
  4933.      je    drectr
  4934.      movzx ecx,ax
  4935.      shr   eax,16
  4936.      movzx edx,bx
  4937.      shr   ebx,16
  4938.         mov     esi,[current_slot]
  4939.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  4940.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  4941.      add   ecx,eax
  4942.      add   edx,ebx
  4943.      jmp   [drawbar]
  4944.     drectr:
  4945.      ret
  4946.  
  4947. align 4
  4948.  
  4949. syscall_getscreensize:                  ; GetScreenSize
  4950.  
  4951.      movzx eax,word[ScreenWidth]
  4952.      shl   eax,16
  4953.      mov   ax,[ScreenHeight]
  4954.      mov   [esp+36],eax
  4955.      ret
  4956.  
  4957. align 4
  4958.  
  4959. syscall_cdaudio:                        ; CD
  4960.  
  4961.         cmp     eax, 4
  4962.         jb      .audio
  4963.         jz      .eject
  4964.         cmp     eax, 5
  4965.         jnz     .ret
  4966. .load:
  4967.         call    .reserve
  4968.         call    LoadMedium
  4969.         call    .free
  4970.         ret
  4971. .eject:
  4972.         call    .reserve
  4973.         call    clear_CD_cache
  4974.         call    allow_medium_removal
  4975.         call    EjectMedium
  4976.         call    .free
  4977.         ret
  4978. .audio:
  4979.      call  sys_cd_audio
  4980.      mov   [esp+36],eax
  4981. .ret:
  4982.      ret
  4983.  
  4984. .reserve:
  4985.         call    reserve_cd
  4986.         mov     eax, ebx
  4987.         shr     eax, 1
  4988.         and     eax, 1
  4989.         inc     eax
  4990.         mov     [ChannelNumber], ax
  4991.         mov     eax, ebx
  4992.         and     eax, 1
  4993.         mov     [DiskNumber], al
  4994.         call    reserve_cd_channel
  4995.         and     ebx, 3
  4996.         inc     ebx
  4997.         mov     [cdpos], ebx
  4998.         add     ebx, ebx
  4999.         mov     cl, 8
  5000.         sub     cl, bl
  5001.         mov     al, [DRIVE_DATA+1]
  5002.         shr     al, cl
  5003.         test    al, 2
  5004.         jz      .err
  5005.         ret
  5006. .free:
  5007.         call    free_cd_channel
  5008.         and     [cd_status], 0
  5009.         ret
  5010. .err:
  5011.         call    .free
  5012.         pop     eax
  5013.         ret
  5014.  
  5015. align 4
  5016.  
  5017. syscall_delramdiskfile:                 ; DelRamdiskFile
  5018.  
  5019.      mov   edi,[TASK_BASE]
  5020.      add   edi,TASKDATA.mem_start
  5021.      add   eax,[edi]
  5022.      call  filedelete
  5023.      mov   [esp+36],eax
  5024.      ret
  5025.  
  5026. align 4
  5027.  
  5028. syscall_writeramdiskfile:               ; WriteRamdiskFile
  5029.  
  5030.      mov   edi,[TASK_BASE]
  5031.      add   edi,TASKDATA.mem_start
  5032.      add   eax,[edi]
  5033.      add   ebx,[edi]
  5034.      call  filesave
  5035.      mov   [esp+36],eax
  5036.      ret
  5037.  
  5038. align 4
  5039.  
  5040. syscall_getpixel:                       ; GetPixel
  5041.      mov   ecx,[ScreenWidth]
  5042.      inc   ecx
  5043.      xor   edx,edx
  5044.      div   ecx
  5045.      mov   ebx,edx
  5046.      xchg  eax,ebx
  5047.      call  dword [GETPIXEL]
  5048.      mov   [esp+36],ecx
  5049.      ret
  5050.  
  5051. align 4
  5052.  
  5053. syscall_readstring:                     ; ReadString
  5054.  
  5055.      mov   edi,[TASK_BASE]
  5056.      add   edi,TASKDATA.mem_start
  5057.      add   eax,[edi]
  5058.      call  read_string
  5059.      mov   [esp+36],eax
  5060.      ret
  5061.  
  5062. align 4
  5063.  
  5064. syscall_drawline:                       ; DrawLine
  5065.  
  5066.      mov   edi,[TASK_BASE]
  5067.      movzx edx,word[edi-twdw+WDATA.box.left]
  5068.      mov   ebp,edx
  5069.         mov     esi,[current_slot]
  5070.         add     ebp,[esi+APPDATA.wnd_clientbox.left]
  5071.         add     dx,word[esi+APPDATA.wnd_clientbox.left]
  5072.      shl   edx,16
  5073.      add   ebp,edx
  5074.      movzx edx,word[edi-twdw+WDATA.box.top]
  5075.      add   eax,ebp
  5076.      mov   ebp,edx
  5077.         add     ebp,[esi+APPDATA.wnd_clientbox.top]
  5078.         add     dx,word[esi+APPDATA.wnd_clientbox.top]
  5079.      shl   edx,16
  5080.      xor   edi,edi
  5081.      add   edx,ebp
  5082.      add   ebx,edx
  5083.      jmp   [draw_line]
  5084.  
  5085. align 4
  5086.  
  5087. syscall_getirqowner:                    ; GetIrqOwner
  5088.      cmp   eax,16
  5089.      jae   .err
  5090.      shl   eax,2
  5091.      add   eax,irq_owner
  5092.      mov   eax,[eax]
  5093.      mov   [esp+36],eax
  5094.      ret
  5095. .err:
  5096.      or    dword [esp+36], -1
  5097.      ret
  5098.  
  5099. align 4
  5100.  
  5101. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5102.  
  5103.      call  r_f_port_area
  5104.      mov   [esp+36],eax
  5105.      ret
  5106.  
  5107. align 4
  5108.  
  5109. syscall_threads:                        ; CreateThreads
  5110.  
  5111.      call  sys_threads
  5112.      mov   [esp+36],eax
  5113.      ret
  5114.  
  5115. align 4
  5116.  
  5117. stack_driver_stat:
  5118.  
  5119.      call  app_stack_handler            ; Stack status
  5120.  
  5121. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5122. ;     call  change_task                 ; between sent packet
  5123.  
  5124.      mov   [esp+36],eax
  5125.      ret
  5126.  
  5127. align 4
  5128.  
  5129. socket:                                 ; Socket interface
  5130.      call  app_socket_handler
  5131.  
  5132. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5133. ;     call  change_task                 ; between sent packet
  5134.  
  5135.      mov   [esp+36],eax
  5136.      mov   [esp+24],ebx
  5137.      ret
  5138.  
  5139. align 4
  5140.  
  5141. user_events:                            ; User event times
  5142.  
  5143.      mov   eax,0x12345678
  5144.      mov   [esp+36],eax
  5145.  
  5146.      ret
  5147.  
  5148. align 4
  5149.  
  5150. read_from_hd:                           ; Read from hd - fn not in use
  5151.  
  5152.      mov   edi,[TASK_BASE]
  5153.      add   edi,TASKDATA.mem_start
  5154.      add   eax,[edi]
  5155.      add   ecx,[edi]
  5156.      add   edx,[edi]
  5157.      call  file_read
  5158.  
  5159.      mov   [esp+36],eax
  5160.      mov   [esp+24],ebx
  5161.  
  5162.      ret
  5163.  
  5164. align 4
  5165. paleholder:
  5166.         ret
  5167.  
  5168. ; --------------- APM ---------------------
  5169. apm_entry    dp    0
  5170. apm_vf        dd    0
  5171. align 4
  5172. sys_apm:
  5173.     cmp    word [apm_vf], 0    ; Check APM BIOS enable
  5174.     jne    @f
  5175.     or    [esp + 56], byte 1    ; error
  5176.     mov    [esp + 36], dword 8    ; 32-bit protected-mode interface not supported
  5177.     ret
  5178.  
  5179. @@:
  5180.     xchg    eax, ecx
  5181.     xchg    ebx, ecx
  5182.  
  5183.     cmp    al, 3
  5184.     ja    @f
  5185.     and    [esp + 56], byte 0xfe    ; emulate func 0..3 as func 0
  5186.     mov    eax, [apm_vf]
  5187.     mov    [esp + 36], eax
  5188.     shr    eax, 16
  5189.     mov    [esp + 32], eax
  5190.     ret
  5191.  
  5192. @@:
  5193.  
  5194.     mov esi, [master_tab+(OS_BASE shr 20)]
  5195.     xchg [master_tab], esi
  5196.     push esi
  5197.     mov edi, cr3
  5198.     mov cr3, edi                 ;flush TLB
  5199.  
  5200.     call    pword [apm_entry]    ; call APM BIOS
  5201.  
  5202.     xchg eax, [esp]
  5203.     mov [master_tab], eax
  5204.     mov eax, cr3
  5205.     mov cr3, eax
  5206.     pop eax
  5207.  
  5208.     mov    [esp + 8 ], edi
  5209.     mov    [esp + 12], esi
  5210.     mov    [esp + 24], ebx
  5211.     mov    [esp + 28], edx
  5212.     mov    [esp + 32], ecx
  5213.     mov    [esp + 36], eax
  5214.     setc    al
  5215.     and    [esp + 56], byte 0xfe
  5216.     or    [esp + 56], al
  5217.  
  5218.  
  5219.     ret
  5220. ; -----------------------------------------
  5221.  
  5222. align 4
  5223.  
  5224. undefined_syscall:                      ; Undefined system call
  5225.  
  5226.      mov   [esp+36],dword -1
  5227.      ret
  5228.  
  5229. align 4
  5230. system_shutdown:          ; shut down the system
  5231.  
  5232.            cmp byte [BOOT_VAR+0x9030], 1
  5233.            jne @F
  5234.            ret
  5235. @@:
  5236.            call stop_all_services
  5237.            push 3                ; stop playing cd
  5238.            pop  eax
  5239.            call sys_cd_audio
  5240.  
  5241. yes_shutdown_param:
  5242.            cli
  5243.  
  5244.            mov  eax, kernel_file ; load kernel.mnt to 0x8000:0
  5245.            push 12
  5246.            pop  esi
  5247.            xor  ebx,ebx
  5248.            or   ecx,-1
  5249.            mov  edx, OS_BASE+0x80000
  5250.            call fileread
  5251.  
  5252.            mov  esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  5253.            mov  edi,OS_BASE+0x40000
  5254.            mov  ecx,1000
  5255.            rep  movsb
  5256.  
  5257.            mov  esi,OS_BASE+0x2F0000    ; restore 0x0 - 0xffff
  5258.            mov  edi, OS_BASE
  5259.            mov  ecx,0x10000/4
  5260.            cld
  5261.            rep movsd
  5262.  
  5263.            call restorefatchain
  5264.  
  5265.            mov al, 0xFF
  5266.            out 0x21, al
  5267.            out 0xA1, al
  5268.  
  5269.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5270.            mov  word [OS_BASE+0x467+2],0x1000
  5271.  
  5272.            mov  al,0x0F
  5273.            out  0x70,al
  5274.            mov  al,0x05
  5275.            out  0x71,al
  5276.  
  5277.            mov  al,0xFE
  5278.            out  0x64,al
  5279.            hlt
  5280.  
  5281.  
  5282. include "data32.inc"
  5283.  
  5284. __REV__ = __REV
  5285.  
  5286. uglobals_size = $ - endofcode
  5287. diff16 "end of kernel code",0,$
  5288.  
  5289.