Subversion Repositories Kolibri OS

Rev

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