Subversion Repositories Kolibri OS

Rev

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