Subversion Repositories Kolibri OS

Rev

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