Subversion Repositories Kolibri OS

Rev

Rev 429 | Rev 435 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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