Subversion Repositories Kolibri OS

Rev

Rev 1 | Rev 3 | 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.0.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        equ  os_data_l-gdts    ; GDTs
  116. os_code        equ  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,0,0  ; version 0.5.0.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.  
  4135. ; calculate fat chain
  4136.  
  4137. calculatefatchain:
  4138.  
  4139.    pushad
  4140.  
  4141.    mov  esi,0x100000+512
  4142.    mov  edi,0x280000
  4143.  
  4144.   fcnew:
  4145.    xor  eax,eax
  4146.    xor  ebx,ebx
  4147.    xor  ecx,ecx
  4148.    xor  edx,edx
  4149.    mov  al,[esi+0]  ; 1
  4150.    mov  bl,[esi+1]
  4151.    and  ebx,15
  4152.    shl  ebx,8
  4153.    add  eax,ebx
  4154.    mov  [edi],ax
  4155.    add  edi,2
  4156.  
  4157.    xor  eax,eax
  4158.    xor  ebx,ebx
  4159.    xor  ecx,ecx
  4160.    xor  edx,edx
  4161.    mov  bl,[esi+1]  ; 2
  4162.    mov  cl,[esi+2]
  4163.    shr  ebx,4
  4164.    shl  ecx,4
  4165.    add  ecx,ebx
  4166.    mov  [edi],cx
  4167.    add  edi,2
  4168.  
  4169.    add  esi,3
  4170.  
  4171.    cmp  edi,0x280000+4100*4
  4172.    jnz  fcnew
  4173.  
  4174.    popad
  4175.    ret
  4176.  
  4177.  
  4178. restorefatchain:   ; restore fat chain
  4179.  
  4180.    pushad
  4181.  
  4182.    mov  esi,0x280000
  4183.    mov  edi,0x100000+512
  4184.  
  4185.   fcnew2:
  4186.    cld
  4187.    xor  eax,eax
  4188.    xor  ebx,ebx
  4189.    xor  ecx,ecx                    ;   esi  XXXXxxxxxxxx  yyyyyyyyYYYY
  4190.    xor  edx,edx
  4191.    mov  ax,[esi]                   ;   edi  xxxxxxxx YYYYXXXX yyyyyyyy
  4192.    mov  bx,ax
  4193.    shr  bx,8
  4194.    and  ebx,15
  4195.    mov  [edi+0],al  ; 1 -> 1 & 2
  4196.    mov  [edi+1],bl
  4197.    add  esi,2
  4198.  
  4199.    xor  eax,eax
  4200.    xor  ebx,ebx
  4201.    xor  ecx,ecx
  4202.    xor  edx,edx
  4203.    mov  bx,[esi]
  4204.    mov  cx,bx
  4205.    shr  ecx,4
  4206.    mov  [edi+2],cl
  4207.    and  ebx,15
  4208.    shl  ebx,4
  4209.    mov  edx,[edi+1]
  4210.    add  edx,ebx
  4211.    mov  [edi+1],dl  ; 2 -> 2 & 3
  4212.    add  esi,2
  4213.  
  4214.    add  edi,3
  4215.  
  4216.    cmp  edi,0x100000+512+0x1200
  4217.    jb   fcnew2
  4218.  
  4219.    mov  esi,0x100000+512           ; duplicate fat chain
  4220.    mov  edi,0x100000+512+0x1200
  4221.    mov  ecx,0x1200/4
  4222.    cld
  4223.    rep  movsd
  4224.  
  4225.    popad
  4226.    ret
  4227.  
  4228.  
  4229. align 4
  4230.  
  4231. read_floppy_file:
  4232.  
  4233. ; as input
  4234. ;
  4235. ; eax pointer to file
  4236. ; ebx file lenght
  4237. ; ecx start 512 byte block number
  4238. ; edx number of blocks to read
  4239. ; esi pointer to return/work area (atleast 20 000 bytes)
  4240. ;
  4241. ;
  4242. ; on return
  4243. ;
  4244. ; eax = 0 command succesful
  4245. ;       1 no fd base and/or partition defined
  4246. ;       2 yet unsupported FS
  4247. ;       3 unknown FS
  4248. ;       4 partition not defined at hd
  4249. ;       5 file not found
  4250. ; ebx = size of file
  4251.  
  4252.      mov   edi,[0x3010]
  4253.      add   edi,0x10
  4254.      add   esi,[edi]
  4255.      add   eax,[edi]
  4256.  
  4257.      pushad
  4258.      mov  edi,esi
  4259.      add  edi,1024
  4260.      mov  esi,0x100000+19*512
  4261.      sub  ecx,1
  4262.      shl  ecx,9
  4263.      add  esi,ecx
  4264.      shl  edx,9
  4265.      mov  ecx,edx
  4266.      cld
  4267.      rep  movsb
  4268.      popad
  4269.  
  4270.      mov   [esp+36],eax
  4271.      mov   [esp+24],ebx
  4272.      ret
  4273.  
  4274.  
  4275.  
  4276. align 4
  4277.  
  4278. sys_programirq:
  4279.  
  4280.     mov   edi,[0x3010]
  4281.     add   edi,0x10
  4282.     add   eax,[edi]
  4283.  
  4284.     mov   edx,ebx
  4285.     shl   edx,2
  4286.     add   edx,irq_owner
  4287.     mov   edx,[edx]
  4288.     mov   edi,[0x3010]
  4289.     mov   edi,[edi+0x4]
  4290.     cmp   edx,edi
  4291.     je    spril1
  4292.     mov   [esp+36],dword 1
  4293.     ret
  4294.   spril1:
  4295.  
  4296.     mov   esi,eax
  4297.     shl   ebx,6
  4298.     add   ebx,irq00read
  4299.     mov   edi,ebx
  4300.     mov   ecx,16
  4301.     cld
  4302.     rep   movsd
  4303.     mov   [esp+36],dword 0
  4304.     ret
  4305.  
  4306.  
  4307. align 4
  4308.  
  4309. get_irq_data:
  4310.  
  4311.      mov   edx,eax           ; check for correct owner
  4312.      shl   edx,2
  4313.      add   edx,irq_owner
  4314.      mov   edx,[edx]
  4315.      mov   edi,[0x3010]
  4316.      mov   edi,[edi+0x4]
  4317.      cmp   edx,edi
  4318.      je    gidril1
  4319.      mov   [esp+36],eax
  4320.      mov   [esp+32],dword 2
  4321.      mov   [esp+24],ebx
  4322.      ret
  4323.  
  4324.   gidril1:
  4325.  
  4326.      mov   ebx,eax
  4327.      shl   ebx,12
  4328.      add   ebx,0x2e0000
  4329.      mov   eax,[ebx]
  4330.      mov   ecx,1
  4331.      test  eax,eax
  4332.      jz    gid1
  4333.  
  4334.      dec   eax
  4335.      mov   esi,ebx
  4336.      mov   [ebx],eax
  4337.      movzx ebx,byte [ebx+0x10]
  4338.      add   esi,0x10
  4339.      mov   edi,esi
  4340.      inc   esi
  4341.      mov   ecx,4000 / 4
  4342.      cld
  4343.      rep   movsd
  4344.      xor   ecx,ecx
  4345.    gid1:
  4346.      mov   [esp+36],eax
  4347.      mov   [esp+32],ecx
  4348.      mov   [esp+24],ebx
  4349.      ret
  4350.  
  4351.  
  4352. set_io_access_rights:
  4353.  
  4354.      pushad
  4355.  
  4356.      mov   edi,[0x3000]
  4357.      imul  edi,tss_step
  4358.      add   edi,tss_data+128
  4359. ;     add   edi,128
  4360.  
  4361.      mov   ecx,eax
  4362.      and   ecx,7    ; offset in byte
  4363.  
  4364.      shr   eax,3    ; number of byte
  4365.      add   edi,eax
  4366.  
  4367.      mov   ebx,1
  4368.      shl   ebx,cl
  4369.  
  4370.      cmp   ebp,0                ; enable access - ebp = 0
  4371.      jne   siar1
  4372.  
  4373.      not   ebx
  4374.      and   [edi],byte bl
  4375.  
  4376.      popad
  4377.  
  4378.      ret
  4379.  
  4380.    siar1:
  4381.  
  4382.      or    [edi],byte bl        ; disable access - ebp = 1
  4383.  
  4384.      popad
  4385.  
  4386.      ret
  4387.  
  4388.  
  4389.  
  4390.  
  4391.  
  4392. r_f_port_area:
  4393.  
  4394.      test  eax, eax
  4395.      jnz   free_port_area
  4396. ;     je    r_port_area
  4397. ;     jmp   free_port_area
  4398.  
  4399. ;   r_port_area:
  4400.  
  4401.      pushad
  4402.  
  4403.      cmp   ebx,ecx            ; beginning > end ?
  4404.      jg    rpal1
  4405.      mov   esi,[0x2d0000]
  4406.      cmp   esi,0              ; no reserved areas ?
  4407.      je    rpal2
  4408.      cmp   esi,255            ; max reserved
  4409.      jge   rpal1
  4410.    rpal3:
  4411.      mov   edi,esi
  4412.      shl   edi,4
  4413.      add   edi,0x2d0000
  4414.      cmp   ebx,[edi+8]
  4415.      jg    rpal4
  4416.      cmp   ecx,[edi+4]
  4417.      jae   rpal1
  4418. ;     jb    rpal4
  4419. ;     jmp   rpal1
  4420.    rpal4:
  4421.  
  4422.      dec   esi
  4423.      jnz   rpal3
  4424.      jmp   rpal2
  4425.    rpal1:
  4426.      popad
  4427.      mov   eax,1
  4428.      ret
  4429.  
  4430.    rpal2:
  4431.      popad
  4432.  
  4433.  
  4434.      ; enable port access at port IO map
  4435.      cli
  4436.      pushad                        ; start enable io map
  4437.  
  4438.      cmp   ecx,65536 ;16384
  4439.      jae   no_unmask_io ; jge
  4440.  
  4441.      mov   eax,ebx
  4442.  
  4443.    new_port_access:
  4444.  
  4445.      pushad
  4446.  
  4447.      mov   ebp,0                  ; enable - eax = port
  4448.      call  set_io_access_rights
  4449.  
  4450.      popad
  4451.  
  4452.      inc   eax
  4453.      cmp   eax,ecx
  4454.      jbe   new_port_access
  4455.  
  4456.    no_unmask_io:
  4457.  
  4458.      popad                         ; end enable io map
  4459.      sti
  4460.  
  4461.      mov   edi,[0x2d0000]
  4462.      add   edi,1
  4463.      mov   [0x2d0000],edi
  4464.      shl   edi,4
  4465.      add   edi,0x2d0000
  4466.      mov   esi,[0x3010]
  4467.      mov   esi,[esi+0x4]
  4468.      mov   [edi],esi
  4469.      mov   [edi+4],ebx
  4470.      mov   [edi+8],ecx
  4471.  
  4472.      xor   eax, eax
  4473.      ret
  4474.  
  4475.  
  4476.  
  4477.  
  4478. free_port_area:
  4479.  
  4480.      pushad
  4481.  
  4482.      mov   esi,[0x2d0000]     ; no reserved areas ?
  4483.      cmp   esi,0
  4484.      je    frpal2
  4485.      mov   edx,[0x3010]
  4486.      mov   edx,[edx+4]
  4487.    frpal3:
  4488.      mov   edi,esi
  4489.      shl   edi,4
  4490.      add   edi,0x2d0000
  4491.      cmp   edx,[edi]
  4492.      jne   frpal4
  4493.      cmp   ebx,[edi+4]
  4494.      jne   frpal4
  4495.      cmp   ecx,[edi+8]
  4496.      jne   frpal4
  4497.      jmp   frpal1
  4498.    frpal4:
  4499.      dec   esi
  4500.      jnz   frpal3
  4501.    frpal2:
  4502.      popad
  4503.      mov   eax,1
  4504.      ret
  4505.    frpal1:
  4506.      mov   ecx,256
  4507.      sub   ecx,esi
  4508.      shl   ecx,4
  4509.      mov   esi,edi
  4510.      add   esi,16
  4511.      cld
  4512.      rep   movsb
  4513.  
  4514.      dec   dword [0x2d0000]
  4515.  
  4516.      popad
  4517.  
  4518.  
  4519.      ; disable port access at port IO map
  4520.  
  4521.      pushad                        ; start disable io map
  4522.  
  4523.      cmp   ecx,65536 ;16384
  4524.      jge   no_mask_io
  4525.  
  4526.      mov   eax,ebx
  4527.  
  4528.    new_port_access_disable:
  4529.  
  4530.      pushad
  4531.  
  4532.      mov   ebp,1                  ; disable - eax = port
  4533.      call  set_io_access_rights
  4534.  
  4535.      popad
  4536.  
  4537.      inc   eax
  4538.      cmp   eax,ecx
  4539.      jbe   new_port_access_disable
  4540.  
  4541.    no_mask_io:
  4542.  
  4543.      popad                         ; end disable io map
  4544.  
  4545.      xor   eax, eax
  4546.      ret
  4547.  
  4548.  
  4549. reserve_free_irq:
  4550.  
  4551.      cmp   eax,0
  4552.      jz    reserve_irq
  4553.  
  4554.      mov   edi,ebx
  4555.      shl   edi,2
  4556.      add   edi,irq_owner
  4557.      mov   edx,[edi]
  4558.      mov   eax,[0x3010]
  4559.      mov   eax,[eax+0x4]
  4560.      mov   ecx,1
  4561.      cmp   edx,eax
  4562.      jne   fril1
  4563.      mov   [edi],dword 0
  4564.      mov   ecx,0
  4565.    fril1:
  4566.      mov   [esp+36],ecx ; return in eax
  4567.      ret
  4568.  
  4569.   reserve_irq:
  4570.  
  4571.      mov   edi,ebx
  4572.      shl   edi,2
  4573.      add   edi,irq_owner
  4574.      mov   edx,[edi]
  4575.      mov   ecx,1
  4576.      cmp   edx,0
  4577.      jne   ril1
  4578.  
  4579.      mov   edx,[0x3010]
  4580.      mov   edx,[edx+0x4]
  4581.      mov   [edi],edx
  4582.      mov   ecx,0
  4583.  
  4584.    ril1:
  4585.  
  4586.      mov   [esp+36],ecx ; return in eax
  4587.  
  4588.      ret
  4589.  
  4590.  
  4591.  
  4592. drawbackground:
  4593.  
  4594.        cmp   [0xfe0c],word 0x12
  4595.        jne   dbrv12
  4596.        cmp   [display_data-12],dword 1
  4597.        jne   bgrstr12
  4598.        call  vga_drawbackground_tiled
  4599.        ret
  4600.      bgrstr12:
  4601.        call  vga_drawbackground_stretch
  4602.        ret
  4603.      dbrv12:
  4604.  
  4605.        cmp  [0xfe0c],word 0100000000000000b
  4606.        jge  dbrv20
  4607.        cmp  [0xfe0c],word 0x13
  4608.        je   dbrv20
  4609.        call  vesa12_drawbackground
  4610.        ret
  4611.      dbrv20:
  4612.        cmp   [display_data-12],dword 1
  4613.        jne   bgrstr
  4614.        call  vesa20_drawbackground_tiled
  4615.        ret
  4616.      bgrstr:
  4617.        call  vesa20_drawbackground_stretch
  4618.        ret
  4619.  
  4620.  
  4621. sys_putimage:
  4622.  
  4623.      cmp   [0xfe0c],word 0x12
  4624.      jne   spiv20
  4625.      call  vga_putimage
  4626.      ret
  4627.    spiv20:
  4628.  
  4629.      cmp   [0xfe0c],word 0100000000000000b
  4630.      jge   piv20
  4631.      cmp   [0xfe0c],word 0x13
  4632.      je    piv20
  4633.      call  vesa12_putimage
  4634.      ret
  4635.    piv20:
  4636.      call  vesa20_putimage
  4637.      ret
  4638.  
  4639.  
  4640.  
  4641. ; eax x beginning
  4642. ; ebx y beginning
  4643. ; ecx x end
  4644. ; edx y end
  4645. ; edi color
  4646.  
  4647. __sys_drawbar:
  4648.  
  4649.      cmp   [0xfe0c],word 0x12
  4650.      jne   sdbv20
  4651.      call  vga_drawbar
  4652.      ret
  4653.    sdbv20:
  4654.  
  4655.     cmp  [0xfe0c],word 0100000000000000b
  4656.     jge  dbv20
  4657.     cmp  [0xfe0c],word 0x13
  4658.     je   dbv20
  4659.     call vesa12_drawbar
  4660.     ret
  4661.  
  4662.   dbv20:
  4663.  
  4664.     call vesa20_drawbar
  4665.     ret
  4666.  
  4667.  
  4668.  
  4669. kb_read:
  4670.  
  4671.         push    ecx edx
  4672.  
  4673.         mov     ecx,0xffff
  4674.       kr_loop:
  4675.         in      al,0x64
  4676.         test    al,1
  4677.         jnz     kr_ready
  4678.         loop    kr_loop
  4679.         mov     ah,1
  4680.         jmp     kr_exit
  4681.       kr_ready:
  4682.         push    ecx
  4683.         mov     ecx,32
  4684.       kr_delay:
  4685.         loop    kr_delay
  4686.         pop     ecx
  4687.         in      al,0x60
  4688.         xor     ah,ah
  4689.       kr_exit:
  4690.  
  4691.         pop     edx ecx
  4692.  
  4693.         ret
  4694.  
  4695.  
  4696. kb_write:
  4697.  
  4698.         push    ecx edx
  4699.  
  4700.         mov     dl,al
  4701.         mov     ecx,0xffff
  4702.       kw_loop1:
  4703.         in      al,0x64
  4704.         test    al,0x20
  4705.         jz      kw_ok1
  4706.         loop    kw_loop1
  4707.         mov     ah,1
  4708.         jmp     kw_exit
  4709.       kw_ok1:
  4710.         in      al,0x60
  4711.         mov     ecx,0xffff
  4712.       kw_loop:
  4713.         in      al,0x64
  4714.         test    al,2
  4715.         jz      kw_ok
  4716.         loop    kw_loop
  4717.         mov     ah,1
  4718.         jmp     kw_exit
  4719.       kw_ok:
  4720.         mov     al,dl
  4721.         out     0x60,al
  4722.         mov     ecx,0xffff
  4723.       kw_loop3:
  4724.         in      al,0x64
  4725.         test    al,2
  4726.         jz      kw_ok3
  4727.         loop    kw_loop3
  4728.         mov     ah,1
  4729.         jmp     kw_exit
  4730.       kw_ok3:
  4731.         mov     ah,8
  4732.       kw_loop4:
  4733.         mov     ecx,0xffff
  4734.       kw_loop5:
  4735.         in      al,0x64
  4736.         test    al,1
  4737.         jnz     kw_ok4
  4738.         loop    kw_loop5
  4739.         dec     ah
  4740.         jnz     kw_loop4
  4741.       kw_ok4:
  4742.         xor     ah,ah
  4743.       kw_exit:
  4744.  
  4745.         pop     edx ecx
  4746.  
  4747.         ret
  4748.  
  4749.  
  4750. kb_cmd:
  4751.  
  4752.         mov     ecx,0xffff
  4753.       c_wait:
  4754.         in      al,0x64
  4755.         test    al,2
  4756.         jz      c_send
  4757.         loop    c_wait
  4758.         jmp     c_error
  4759.       c_send:
  4760.         mov     al,bl
  4761.         out     0x64,al
  4762.         mov     ecx,0xffff
  4763.       c_accept:
  4764.         in      al,0x64
  4765.         test    al,2
  4766.         jz      c_ok
  4767.         loop    c_accept
  4768.       c_error:
  4769.         mov     ah,1
  4770.         jmp     c_exit
  4771.       c_ok:
  4772.         xor     ah,ah
  4773.       c_exit:
  4774.         ret
  4775.  
  4776.  
  4777. setmouse:  ; set mousepicture -pointer
  4778.            ; ps2 mouse enable
  4779.  
  4780.      mov     [0xf200],dword mousepointer
  4781.  
  4782.      cli
  4783.      mov     bl,0xa8                 ; enable mouse cmd
  4784.      call    kb_cmd
  4785.      call    kb_read                 ; read status
  4786.  
  4787.      mov     bl,0x20                 ; get command byte
  4788.      call    kb_cmd
  4789.      call    kb_read
  4790.      or      al,3                    ; enable interrupt
  4791.      mov     bl,0x60                 ; write command
  4792.      push    eax
  4793.      call    kb_cmd
  4794.      pop     eax
  4795.      call    kb_write
  4796.  
  4797.      mov     bl,0xd4                 ; for mouse
  4798.      call    kb_cmd
  4799.      mov     al,0xf4                 ; enable mouse device
  4800.      call    kb_write
  4801.      call    kb_read           ; read status return
  4802.  
  4803.      ; com1 mouse enable
  4804.  
  4805.      mov   bx,0x3f8 ; combase
  4806.  
  4807.      mov   dx,bx
  4808.      add   dx,3
  4809.      mov   al,0x80
  4810.      out   dx,al
  4811.  
  4812.      mov   dx,bx
  4813.      add   dx,1
  4814.      mov   al,0
  4815.      out   dx,al
  4816.  
  4817.      mov   dx,bx
  4818.      add   dx,0
  4819.      mov   al,0x30*2    ; 0x30 / 4
  4820.      out   dx,al
  4821.  
  4822.      mov   dx,bx
  4823.      add   dx,3
  4824.      mov   al,2         ; 3
  4825.      out   dx,al
  4826.  
  4827.      mov   dx,bx
  4828.      add   dx,4
  4829.      mov   al,0xb
  4830.      out   dx,al
  4831.  
  4832.      mov   dx,bx
  4833.      add   dx,1
  4834.      mov   al,1
  4835.      out   dx,al
  4836.  
  4837.  
  4838.      ; com2 mouse enable
  4839.  
  4840.      mov   bx,0x2f8 ; combase
  4841.  
  4842.      mov   dx,bx
  4843.      add   dx,3
  4844.      mov   al,0x80
  4845.      out   dx,al
  4846.  
  4847.      mov   dx,bx
  4848.      add   dx,1
  4849.      mov   al,0
  4850.      out   dx,al
  4851.  
  4852.      mov   dx,bx
  4853.      add   dx,0
  4854.      mov   al,0x30*2
  4855.      out   dx,al
  4856.  
  4857.      mov   dx,bx
  4858.      add   dx,3
  4859.      mov   al,2
  4860.      out   dx,al
  4861.  
  4862.      mov   dx,bx
  4863.      add   dx,4
  4864.      mov   al,0xb
  4865.      out   dx,al
  4866.  
  4867.      mov   dx,bx
  4868.      add   dx,1
  4869.      mov   al,1
  4870.      out   dx,al
  4871.  
  4872.      ret
  4873.  
  4874.  
  4875. _rdtsc:
  4876.  
  4877.      mov   edx,[cpuid_1+3*4]
  4878.      test  edx,00010000b
  4879.      jz    ret_rdtsc
  4880.      rdtsc
  4881.      ret
  4882.    ret_rdtsc:
  4883.      mov   edx,0xffffffff
  4884.      mov   eax,0xffffffff
  4885.      ret
  4886.  
  4887.  
  4888.  
  4889. rerouteirqs:
  4890.  
  4891.         cli
  4892.  
  4893.         mov     al,0x11         ;  icw4, edge triggered
  4894.         out     0x20,al
  4895.         call    pic_delay
  4896.         out     0xA0,al
  4897.         call    pic_delay
  4898.  
  4899.         mov     al,0x20         ;  generate 0x20 +
  4900.         out     0x21,al
  4901.         call    pic_delay
  4902.         mov     al,0x28         ;  generate 0x28 +
  4903.         out     0xA1,al
  4904.         call    pic_delay
  4905.  
  4906.         mov     al,0x04         ;  slave at irq2
  4907.         out     0x21,al
  4908.         call    pic_delay
  4909.         mov     al,0x02         ;  at irq9
  4910.         out     0xA1,al
  4911.         call    pic_delay
  4912.  
  4913.         mov     al,0x01         ;  8086 mode
  4914.         out     0x21,al
  4915.         call    pic_delay
  4916.         out     0xA1,al
  4917.         call    pic_delay
  4918.  
  4919.         mov     al,255          ; mask all irq's
  4920.         out     0xA1,al
  4921.         call    pic_delay
  4922.         out     0x21,al
  4923.         call    pic_delay
  4924.  
  4925.         mov     ecx,0x1000
  4926.         cld
  4927. picl1:  call    pic_delay
  4928.         loop    picl1
  4929.  
  4930.         mov     al,255          ; mask all irq's
  4931.         out     0xA1,al
  4932.         call    pic_delay
  4933.         out     0x21,al
  4934.         call    pic_delay
  4935.  
  4936.         cli
  4937.  
  4938.         ret
  4939.  
  4940.  
  4941. pic_delay:
  4942.  
  4943.         jmp     pdl1
  4944. pdl1:   ret
  4945.  
  4946.  
  4947. sys_msg_board_str:
  4948.  
  4949.      pushad
  4950.    @@:
  4951.      cmp    [esi],byte 0
  4952.      je     @f
  4953.      mov    eax,1
  4954.      movzx  ebx,byte [esi]
  4955.      call   sys_msg_board
  4956.      inc    esi
  4957.      jmp    @b
  4958.    @@:
  4959.      popad
  4960.      ret
  4961.  
  4962. uglobal
  4963.   msg_board_data: times 512 db 0
  4964.   msg_board_count dd 0x0
  4965. endg
  4966.  
  4967. sys_msg_board:
  4968.  
  4969. ; eax=1 : write :  bl byte to write
  4970. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4971.  
  4972.      mov  ecx,[msg_board_count]
  4973.      cmp  eax, 1
  4974.      jne  smbl1
  4975.  
  4976.  
  4977.      mov  [msg_board_data+ecx],bl
  4978.      inc  ecx
  4979.      and  ecx, 511
  4980.      mov  [msg_board_count], ecx
  4981.      mov  [check_idle_semaphore], 5
  4982.      ret
  4983.    smbl1:
  4984.  
  4985.      cmp   eax, 2
  4986.      jne   smbl2
  4987.      test  ecx, ecx
  4988.      jz    smbl21
  4989. ;     mov   edi, msg_board_data
  4990. ;     mov   esi, msg_board_data+1
  4991. ;     movzx eax, byte [edi]
  4992.      mov   eax, msg_board_data+1
  4993.      mov   ebx, msg_board_data
  4994.      movzx edx, byte [ebx]
  4995.      call  memmove
  4996. ;     push  ecx
  4997. ;     shr   ecx, 2
  4998. ;     cld
  4999. ;     rep   movsd
  5000. ;     pop   ecx
  5001. ;     and   ecx, 3
  5002. ;     rep   movsb
  5003.      dec   [msg_board_count]
  5004.      mov   [esp+36], edx ;eax
  5005.      mov   [esp+24], dword 1
  5006.      ret
  5007.    smbl21:
  5008.      mov   [esp+36], ecx
  5009.      mov   [esp+24], ecx
  5010.  
  5011.    smbl2:
  5012.      ret
  5013.  
  5014.  
  5015.  
  5016. sys_trace:
  5017.  
  5018.      test eax, eax                  ; get event data
  5019.      jnz  no_get_sys_events
  5020.  
  5021.      mov  esi,save_syscall_data     ; data
  5022.      mov  edi,[0x3010]
  5023.      mov  edi,[edi+0x10]
  5024.      add  edi,ebx
  5025.      cld
  5026.      rep  movsb
  5027.  
  5028.      mov  [esp+24],dword 0
  5029.      mov  eax,[save_syscall_count]  ; count
  5030.      mov  [esp+36],eax
  5031.      ret
  5032.  
  5033.    no_get_sys_events:
  5034.  
  5035.      ret
  5036.  
  5037.  
  5038. sys_process_def:
  5039.  
  5040.      cmp   eax,1                   ; set keyboard mode
  5041.      jne   no_set_keyboard_setup
  5042.  
  5043.      mov   edi,[0x3000]
  5044.      shl   edi,8
  5045.      add   edi,0x80000+0xB4
  5046.      mov   [edi],bl
  5047.  
  5048.      ret
  5049.  
  5050.    no_set_keyboard_setup:
  5051.  
  5052.      cmp   eax,2                   ; get keyboard mode
  5053.      jne   no_get_keyboard_setup
  5054.  
  5055.      mov   edi,[0x3000]
  5056.      shl   edi,8
  5057.      add   edi,0x80000+0xB4
  5058.      movzx eax, byte [edi]
  5059.  
  5060.      mov   [esp+36],eax
  5061.  
  5062.      ret
  5063.  
  5064.    no_get_keyboard_setup:
  5065.  
  5066.      cmp   eax,3                   ; get keyboard ctrl, alt, shift
  5067.      jne   no_get_keyboard_cas
  5068.  
  5069. ;     xor   eax,eax
  5070. ;     movzx eax,byte [shift]
  5071. ;     movzx ebx,byte [ctrl]
  5072. ;     shl   ebx,2
  5073. ;     add   eax,ebx
  5074. ;     movzx ebx,byte [alt]
  5075. ;     shl   ebx,3
  5076. ;     add   eax,ebx
  5077.  
  5078.  ;// mike.dld [
  5079.      mov   eax, [kb_state]
  5080.  ;// mike.dld ]
  5081.  
  5082.      mov   [esp+36],eax
  5083.  
  5084.      ret
  5085.  
  5086.    no_get_keyboard_cas:
  5087.  
  5088.  
  5089.      ret
  5090.  
  5091.  
  5092. sys_ipc:
  5093.      cmp  eax,1                      ; DEFINE IPC MEMORY
  5094.      jne  no_ipc_def
  5095.      mov  edi,[0x3000]
  5096.      shl  edi,8
  5097.      add  edi,0x80000
  5098.      mov  [edi+0xA0],ebx
  5099.      mov  [edi+0xA4],ecx
  5100.      mov  [esp+36],dword 0
  5101.      ret
  5102.    no_ipc_def:
  5103.  
  5104.      cmp  eax,2                      ; SEND IPC MESSAGE
  5105.      jne  no_ipc_send
  5106.      mov  esi,1
  5107.      mov  edi,0x3020
  5108.     ipcs1:
  5109.      cmp  [edi+4],ebx
  5110.      je   ipcs2
  5111.      add  edi,0x20
  5112.      inc  esi
  5113.      cmp  esi,[0x3004]
  5114.      jbe  ipcs1
  5115.      mov  [esp+36],dword 4
  5116.      ret
  5117.     ipcs2:
  5118.  
  5119.      cli
  5120.  
  5121.      push esi
  5122.      mov  eax,esi
  5123.      shl  eax,8
  5124.      mov  ebx,[eax+0x80000+0xa0]
  5125.      test ebx,ebx                  ; ipc area not defined ?
  5126.      je   ipc_err1
  5127.  
  5128.      add  ebx,[eax+0x80000+0xa4]
  5129.      mov  eax,esi
  5130.      shl  eax,5
  5131.      add  ebx,[eax+0x3000+0x10]    ; ebx <- max data position
  5132.  
  5133.      mov  eax,esi                  ; to
  5134.      shl  esi,8
  5135.      add  esi,0x80000
  5136.      mov  edi,[esi+0xa0]
  5137.      shl  eax,5
  5138.      add  eax,0x3000
  5139.      add  edi,[eax+0x10]
  5140.  
  5141.      cmp  [edi],byte 0             ; overrun ?
  5142.      jne  ipc_err2
  5143.  
  5144.      mov  ebp,edi
  5145.      add  edi,[edi+4]
  5146.      add  edi,8
  5147.  
  5148.      mov  esi,ecx                  ; from
  5149.      mov  eax,[0x3010]
  5150.      mov  eax,[eax+0x10]
  5151.      add  esi,eax
  5152.  
  5153.      mov  ecx,edx                  ; size
  5154.  
  5155.      mov  eax,edi
  5156.      add  eax,ecx
  5157.      cmp  eax,ebx
  5158.      jge  ipc_err3                 ; not enough room ?
  5159.  
  5160.      push ecx
  5161.  
  5162.      mov  eax,[0x3010]
  5163.      mov  eax,[eax+4]
  5164.      mov  [edi-8],eax
  5165.      mov  [edi-4],ecx
  5166.      cld
  5167.      rep  movsb
  5168.  
  5169.      pop  ecx
  5170.      add  ecx,8
  5171.  
  5172.      mov  edi,ebp                  ; increase memory position
  5173.      add  dword [edi+4],ecx
  5174.  
  5175.      mov  edi,[esp]
  5176.      shl  edi,8
  5177.      or   dword [edi+0x80000+0xA8],dword 01000000b ; ipc message
  5178.  
  5179.      cmp  [check_idle_semaphore],dword 20
  5180.      jge  ipc_no_cis
  5181.      mov  [check_idle_semaphore],5
  5182.    ipc_no_cis:
  5183.  
  5184.      xor  eax, eax
  5185.  
  5186.     ipc_err:
  5187.      add  esp,4
  5188.      mov  [esp+36],eax
  5189.      sti
  5190.      ret
  5191.  
  5192.     ipc_err1:
  5193.      add  esp,4
  5194.      mov  [esp+36],dword 1
  5195.      sti
  5196.      ret
  5197.     ipc_err2:
  5198.      add  esp,4
  5199.      mov  [esp+36],dword 2
  5200.      sti
  5201.      ret
  5202.     ipc_err3:
  5203.      add  esp,4
  5204.      mov  [esp+36],dword 3
  5205.      sti
  5206.      ret
  5207.  
  5208.    no_ipc_send:
  5209.  
  5210.      mov  [esp+36],dword -1
  5211.      ret
  5212.  
  5213.  
  5214. align 4
  5215.  
  5216. sys_gs:                         ; direct screen access
  5217.  
  5218.      cmp  eax,1                 ; resolution
  5219.      jne  no_gs1
  5220.      mov  eax,[0xfe00]
  5221.      shl  eax,16
  5222.      mov  ax,[0xfe04]
  5223.      add  eax,0x00010001
  5224.      mov  [esp+36],eax
  5225.      ret
  5226.    no_gs1:
  5227.  
  5228.      cmp   eax,2                ; bits per pixel
  5229.      jne   no_gs2
  5230.      movzx eax,byte [0xfbf1]
  5231.      mov   [esp+36],eax
  5232.      ret
  5233.    no_gs2:
  5234.  
  5235.      cmp   eax,3                ; bytes per scanline
  5236.      jne   no_gs3
  5237.      mov   eax,[0xfe08]
  5238.      mov   [esp+36],eax
  5239.      ret
  5240.    no_gs3:
  5241.  
  5242.      mov  [esp+36],dword -1
  5243.      ret
  5244.  
  5245.  
  5246. align 4 ; PCI functions
  5247.  
  5248. sys_pci:
  5249.  
  5250.      call  pci_api
  5251.      mov   [esp+36],eax
  5252.      ret
  5253.  
  5254.  
  5255. align 4  ;  system functions
  5256.  
  5257. syscall_setpixel:                       ; SetPixel
  5258.  
  5259.  
  5260.      mov   edx,[0x3010]
  5261.      add   eax,[edx-twdw]
  5262.      add   ebx,[edx-twdw+4]
  5263.      xor   edi,edi ; no force
  5264.      call  [disable_mouse]
  5265.      jmp   [putpixel]
  5266.  
  5267. align 4
  5268.  
  5269. syscall_writetext:                      ; WriteText
  5270.  
  5271.      mov   edi,[0x3010]
  5272.      mov   ebp,[edi-twdw]
  5273.      shl   ebp,16
  5274.      add   ebp,[edi-twdw+4]
  5275.      add   edi,0x10
  5276.      add   ecx,[edi]
  5277.      add   eax,ebp
  5278.      xor   edi,edi
  5279.      jmp   dtext
  5280.  
  5281. align 4
  5282.  
  5283. syscall_openramdiskfile:                ; OpenRamdiskFile
  5284.  
  5285.  
  5286.      mov   edi,[0x3010]
  5287.      add   edi,0x10
  5288.      add   eax,[edi]
  5289.      add   edx,[edi]
  5290.      mov   esi,12
  5291.      call  fileread
  5292.      mov   [esp+36],ebx
  5293.      ret
  5294.  
  5295. align 4
  5296.  
  5297. syscall_putimage:                       ; PutImage
  5298.  
  5299.      mov   edi,[0x3010]
  5300.      add   edi,0x10
  5301.      add   eax,[edi]
  5302.      mov   edx,ecx
  5303.      mov   ecx,ebx
  5304.      mov   ebx,eax
  5305.      call  sys_putimage
  5306.      mov   [esp+36],eax
  5307.      ret
  5308.  
  5309. align 4
  5310.  
  5311. syscall_drawrect:                       ; DrawRect
  5312.  
  5313.      mov   edi,ecx
  5314.      test  ax,ax
  5315.      je    drectr
  5316.      test  bx,bx
  5317.      je    drectr
  5318.      movzx ecx,ax
  5319.      shr   eax,16
  5320.      movzx edx,bx
  5321.      shr   ebx,16
  5322.      add   ecx,eax
  5323.      add   edx,ebx
  5324.      jmp   [drawbar]
  5325.     drectr:
  5326.      ret
  5327.  
  5328. align 4
  5329.  
  5330. syscall_getscreensize:                  ; GetScreenSize
  5331.  
  5332.      movzx eax,word[0xfe00]
  5333.      shl   eax,16
  5334.      mov   ax,[0xfe04]
  5335.      mov   [esp+36],eax
  5336.      ret
  5337.  
  5338. align 4
  5339.  
  5340. syscall_system:                         ; System
  5341.  
  5342.      call  sys_system
  5343.      mov   [esp+36],eax
  5344.      ret
  5345.  
  5346. align 4
  5347.  
  5348. syscall_startapp:                       ; StartApp
  5349.      mov   edi,[0x3010]
  5350.      add   edi,0x10
  5351.      add   eax,[edi]
  5352.      test  ebx,ebx
  5353.      jz    noapppar
  5354.      add   ebx,[edi]
  5355.    noapppar:
  5356. ;     call  start_application_fl
  5357.      call   new_start_application_fl
  5358.      mov   [esp+36],eax
  5359.      ret
  5360.      
  5361.  
  5362. align 4
  5363.  
  5364. syscall_cdaudio:                        ; CD
  5365.  
  5366.      call  sys_cd_audio
  5367.      mov   [esp+36],eax
  5368.      ret
  5369.  
  5370. align 4
  5371.  
  5372. syscall_readhd:                         ; ReadHd
  5373.  
  5374.      mov   edi,[0x3010]
  5375.      add   edi,0x10
  5376.      add   esi,[edi]
  5377.      add   eax,[edi]
  5378.      call  read_hd_file
  5379.      mov   [esp+36],eax
  5380.      mov   [esp+24],ebx
  5381.      ret
  5382.  
  5383. align 4
  5384.  
  5385. syscall_starthdapp:                     ; StartHdApp
  5386.  
  5387.      mov   edi,[0x3010]
  5388.      add   edi,0x10
  5389.      add   eax,[edi]
  5390.      add   ecx,[edi]
  5391.      mov   ebp,0
  5392.      call  start_application_hd
  5393.      mov   [esp+36],eax
  5394.      ret
  5395.  
  5396. align 4
  5397.  
  5398. syscall_delramdiskfile:                 ; DelRamdiskFile
  5399.  
  5400.      mov   edi,[0x3010]
  5401.      add   edi,0x10
  5402.      add   eax,[edi]
  5403.      call  filedelete
  5404.      mov   [esp+36],eax
  5405.      ret
  5406.  
  5407. align 4
  5408.  
  5409. syscall_writeramdiskfile:               ; WriteRamdiskFile
  5410.  
  5411.      mov   edi,[0x3010]
  5412.      add   edi,0x10
  5413.      add   eax,[edi]
  5414.      add   ebx,[edi]
  5415.      call  filesave
  5416.      mov   [esp+36],eax
  5417.      ret
  5418.  
  5419. align 4
  5420.  
  5421. syscall_getpixel:                       ; GetPixel
  5422.  
  5423.      mov   ecx,[0xfe00]
  5424.      inc   ecx
  5425.      xor   edx,edx
  5426.      div   ecx
  5427.      mov   ebx,edx
  5428.      xchg  eax,ebx
  5429.      call  dword [0xe024]
  5430.      mov   [esp+36],ecx
  5431.      ret
  5432.  
  5433. align 4
  5434.  
  5435. syscall_readstring:                     ; ReadString
  5436.  
  5437.      mov   edi,[0x3010]
  5438.      add   edi,0x10
  5439.      add   eax,[edi]
  5440.      call  read_string
  5441.      mov   [esp+36],eax
  5442.      ret
  5443.  
  5444. align 4
  5445.  
  5446. syscall_drawline:                       ; DrawLine
  5447.  
  5448.      mov   edi,[0x3010]
  5449.      movzx edx,word[edi-twdw]
  5450.      mov   ebp,edx
  5451.      shl   edx,16
  5452.      add   ebp,edx
  5453.      movzx edx,word[edi-twdw+4]
  5454.      add   eax,ebp
  5455.      mov   ebp,edx
  5456.      shl   edx,16
  5457.      xor   edi,edi
  5458.      add   edx,ebp
  5459.      add   ebx,edx
  5460.      jmp   [draw_line]
  5461.  
  5462. align 4
  5463.  
  5464. syscall_getirqowner:                    ; GetIrqOwner
  5465.  
  5466.      shl   eax,2
  5467.      add   eax,irq_owner
  5468.      mov   eax,[eax]
  5469.      mov   [esp+36],eax
  5470.      ret
  5471.  
  5472. align 4
  5473.  
  5474. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5475.  
  5476.      call  r_f_port_area
  5477.      mov   [esp+36],eax
  5478.      ret
  5479.  
  5480. align 4
  5481.  
  5482. syscall_appints:                        ; AppInts
  5483.  
  5484.      test  eax,eax
  5485.      jnz   unknown_app_int_fn
  5486.      mov   edi,[0x3010]
  5487.      mov   [edi+draw_data-0x3000+0x1c],ebx
  5488.      ret
  5489.    unknown_app_int_fn:
  5490.      mov   [esp+36],dword -1
  5491.      ret
  5492.  
  5493. align 4
  5494.  
  5495. syscall_threads:                        ; CreateThreads
  5496.  
  5497.      call  sys_threads
  5498.      mov   [esp+36],eax
  5499.      ret
  5500.  
  5501. align 4
  5502.  
  5503. stack_driver_stat:
  5504.  
  5505.      call  app_stack_handler            ; Stack status
  5506.  
  5507. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5508. ;     call  change_task                 ; between sent packet
  5509.  
  5510.      mov   [esp+36],eax
  5511.      ret
  5512.  
  5513. align 4
  5514.  
  5515. socket:                                 ; Socket interface
  5516.      call  app_socket_handler
  5517.  
  5518. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5519. ;     call  change_task                 ; between sent packet
  5520.  
  5521.      mov   [esp+36],eax
  5522.      mov   [esp+24],ebx
  5523.      ret
  5524.  
  5525. align 4
  5526.  
  5527. user_events:                            ; User event times
  5528.  
  5529.      mov   eax,0x12345678
  5530.      mov   [esp+36],eax
  5531.  
  5532.      ret
  5533.  
  5534. align 4
  5535.  
  5536. read_from_hd:                           ; Read from hd - fn not in use
  5537.  
  5538.      mov   edi,[0x3010]
  5539.      add   edi,0x10
  5540.      add   eax,[edi]
  5541.      add   ecx,[edi]
  5542.      add   edx,[edi]
  5543.      call  file_read
  5544.  
  5545.      mov   [esp+36],eax
  5546.      mov   [esp+24],ebx
  5547.  
  5548.      ret
  5549.  
  5550.  
  5551. align 4
  5552.  
  5553. write_to_hd:                            ; Write a file to hd
  5554.  
  5555.      mov   edi,[0x3010]
  5556.      add   edi,0x10
  5557.      add   eax,[edi]
  5558.      add   ecx,[edi]
  5559.      add   edx,[edi]
  5560.      call  file_write
  5561.      ret
  5562.  
  5563. align 4
  5564.  
  5565. delete_from_hd:                         ; Delete a file from hd
  5566.  
  5567.      mov   edi,[0x3010]
  5568.      add   edi,0x10
  5569.      add   eax,[edi]
  5570.      add   ecx,[edi]
  5571.      call  file_delete
  5572.      ret
  5573.  
  5574.  
  5575. align 4
  5576.  
  5577. undefined_syscall:                      ; Undefined system call
  5578.  
  5579.      mov   [esp+36],dword -1
  5580.      ret
  5581.  
  5582.  
  5583. ;clear_busy_flag_at_caller:
  5584.  
  5585. ;      push  edi
  5586.  
  5587. ;      mov   edi,[0x3000]    ; restore processes tss pointer in gdt, busyfl?
  5588. ;      imul  edi,8
  5589. ;      mov   [edi+gdts+ tss0 +5], word 01010000b *256 +11101001b
  5590.  
  5591. ;      pop   edi
  5592.  
  5593. ;      ret
  5594.  
  5595.  
  5596.  
  5597.  
  5598. keymap:
  5599.  
  5600.      db   '6',27
  5601.      db   '1234567890-=',8,9
  5602.      db   'qwertyuiop[]',13
  5603.      db   '~asdfghjkl;',39,96,0,'\zxcvbnm,./',0,'45 '
  5604.      db   '@234567890123',180,178,184,'6',176,'7'
  5605.      db   179,'8',181,177,183,185,182
  5606.      db   'AB<D',255,'FGHIJKLMNOPQRSTUVWXYZ'
  5607.      db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5608.      db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5609.      db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5610.  
  5611.  
  5612. keymap_shift:
  5613.  
  5614.      db   '6',27
  5615.      db   '!@#$%^&*()_+',8,9
  5616.      db   'QWERTYUIOP{}',13
  5617.      db   '~ASDFGHJKL:"~',0,'|ZXCVBNM<>?',0,'45 '
  5618.      db   '@234567890123',180,178,184,'6',176,'7'
  5619.      db   179,'8',181,177,183,185,182
  5620.      db   'AB>D',255,'FGHIJKLMNOPQRSTUVWXYZ'
  5621.      db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5622.      db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5623.      db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5624.  
  5625.  
  5626. keymap_alt:
  5627.  
  5628.      db   ' ',27
  5629.      db   ' @ $  {[]}\ ',8,9
  5630.      db   '            ',13
  5631.      db   '             ',0,'           ',0,'4',0,' '
  5632.      db   '             ',180,178,184,'6',176,'7'
  5633.      db   179,'8',181,177,183,185,182
  5634.      db   'ABCD',255,'FGHIJKLMNOPQRSTUVWXYZ'
  5635.      db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5636.      db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5637.      db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5638.  
  5639.  
  5640. ; device irq owners
  5641. uglobal
  5642. irq_owner:       ; process id
  5643.  
  5644.      dd   0x0
  5645.      dd   0x0
  5646.      dd   0x0
  5647.      dd   0x0
  5648.      dd   0x0
  5649.      dd   0x0
  5650.      dd   0x0
  5651.      dd   0x0
  5652.      dd   0x0
  5653.      dd   0x0
  5654.      dd   0x0
  5655.      dd   0x0
  5656.      dd   0x0
  5657.      dd   0x0
  5658.      dd   0x0
  5659.      dd   0x0
  5660. endg
  5661.  
  5662.  
  5663. ; on irq read ports
  5664. uglobal
  5665.   irq00read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5666.   irq01read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5667.   irq02read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5668.   irq03read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5669.   irq04read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5670.   irq05read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5671.   irq06read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5672.   irq07read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5673.   irq08read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5674.   irq09read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5675.   irq10read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5676.   irq11read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5677.   irq12read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5678.   irq13read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5679.   irq14read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5680.   irq15read  dd  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5681. endg
  5682.  
  5683. ; status
  5684. uglobal
  5685.   hd1_status                  dd 0x0  ; 0 - free : other - pid
  5686.   application_table_status    dd 0x0  ; 0 - free : other - pid
  5687. endg
  5688.  
  5689. ; device addresses
  5690. uglobal
  5691.   mididp     dd 0x0
  5692.   midisp     dd 0x0
  5693.  
  5694.   cdbase     dd 0x0
  5695.   cdid       dd 0x0
  5696.  
  5697.   hdbase              dd   0x0  ; for boot 0x1f0
  5698.   hdid                dd   0x0
  5699.   hdpos               dd   0x0  ; for boot 0x1
  5700.   fat32part           dd   0x0  ; for boot 0x1
  5701.  
  5702.   ;part2_ld            dd   0x0
  5703.  
  5704. ;* start code - get  process (3) - Mario79
  5705. active_process      dd   0
  5706. ;* end code - get active process (3) - Mario79
  5707.  
  5708. wraw_bacground_select db 0
  5709.   lba_read_enabled    dd   0x0  ; 0 = disabled , 1 = enabled
  5710.   pci_access_enabled  dd   0x0  ; 0 = disabled , 1 = enabled
  5711.  
  5712.   sb16       dd 0x0
  5713.   wss        dd 0x0
  5714.  
  5715.   buttontype         dd 0x0
  5716.   windowtypechanged  dd 0x0
  5717. endg
  5718.  
  5719. iglobal
  5720.   keyboard   dd 0x1
  5721.   sound_dma  dd 0x1
  5722.   syslang    dd 0x1
  5723. endg
  5724.  
  5725. IncludeIGlobals
  5726. endofcode:
  5727. IncludeUGlobals
  5728. uglobals_size = $ - endofcode
  5729. diff16 "end of kernel code",$
  5730.  
  5731.