Subversion Repositories Kolibri OS

Rev

Rev 7 | Rev 12 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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