Subversion Repositories Kolibri OS

Rev

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