Subversion Repositories Kolibri OS

Rev

Rev 129 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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