Subversion Repositories Kolibri OS

Rev

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