Subversion Repositories Kolibri OS

Rev

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