Subversion Repositories Kolibri OS

Rev

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