Subversion Repositories Kolibri OS

Rev

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