Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;
  3. ;; Kolibri OS - based on source code Menuet OS, but not 100% compatible.
  4. ;;
  5. ;; See file COPYING or GNU.TXT for details with these additional details:
  6. ;;     - All code written in 32 bit x86 assembly language
  7. ;;     - No external code (eg. bios) at process execution time
  8. ;;
  9. ;;
  10. ;;   Compile with last version FASM
  11. ;;
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. include "kglobals.inc"
  14. include "lang.inc"
  15.  
  16. WinMapAddress           equ     0x460000
  17. display_data       = 0x460000
  18.  
  19. max_processes      equ   255
  20.  
  21. window_data        equ   0x0000
  22. tss_data           equ   0xD20000
  23. ;tss_step           equ   (128+2048) ; tss & i/o - 16384 ports, * 256=557056
  24. tss_step           equ   (128+8192) ; tss & i/o - 65535 ports, * 256=557056*4
  25. draw_data          equ   0xC00000
  26. sysint_stack_data  equ   0xC03000
  27.  
  28.  
  29. twdw               equ   (0x3000-window_data)
  30.  
  31. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  32. ;;
  33. ;;   Included files:
  34. ;;
  35. ;;   Kernel16.inc
  36. ;;    - Booteng.inc   English text for bootup
  37. ;;    - Bootcode.inc  Hardware setup
  38. ;;    - Pci16.inc     PCI functions
  39. ;;
  40. ;;   Kernel32.inc
  41. ;;    - Sys32.inc     Process management
  42. ;;    - Shutdown.inc  Shutdown and restart
  43. ;;    - Fat32.inc     Read / write hd
  44. ;;    - Vesa12.inc    Vesa 1.2 driver
  45. ;;    - Vesa20.inc    Vesa 2.0 driver
  46. ;;    - Vga.inc       VGA driver
  47. ;;    - Stack.inc     Network interface
  48. ;;    - Mouse.inc     Mouse pointer
  49. ;;    - Scincode.inc  Window skinning
  50. ;;    - Pci32.inc     PCI functions
  51. ;;
  52. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  53.  
  54.  
  55. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  56. ;;                                                                      ;;
  57. ;;                  16 BIT ENTRY FROM BOOTSECTOR                        ;;
  58. ;;                                                                      ;;
  59. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  60.  
  61. use16
  62.                   org   0x10000
  63. macro diff16 title,l2
  64.  {
  65.   local s,d,l1
  66.   s = l2
  67.   display title,': 0x'
  68.   repeat 8
  69.    d = 48 + s shr ((8-%) shl 2) and $0F
  70.    if d > 57
  71.     d = d + 65-57-1
  72.    end if
  73.    display d
  74.   end repeat
  75.   display 13,10
  76.  }      
  77.                   jmp   start_of_code
  78.  
  79. ; mike.dld {
  80. db 0
  81. dd servetable-0x10000
  82. draw_line       dd __sys_draw_line
  83. disable_mouse   dd __sys_disable_mouse
  84. draw_pointer    dd __sys_draw_pointer
  85. drawbar         dd __sys_drawbar
  86. putpixel        dd __sys_putpixel
  87. ; } mike.dld
  88.  
  89. version           db    'Kolibri OS  version 0.5.1.0      ',13,10,13,10,0
  90.                   ;dd    endofcode-0x10000
  91.  
  92.                   ;db   'Boot02'
  93. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  94. include "boot/preboot.inc"
  95. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  96.  
  97. preboot_lfb       db    0
  98. preboot_bootlog   db    0
  99.  
  100.  
  101. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  102. ;;                                                                      ;;
  103. ;;                      16 BIT INCLUDED FILES                           ;;
  104. ;;                                                                      ;;
  105. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  106.  
  107. include "kernel16.inc"
  108.  
  109. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  110. ;;                                                                      ;;
  111. ;;                  SWITCH TO 32 BIT PROTECTED MODE                     ;;
  112. ;;                                                                      ;;
  113. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  114.  
  115. os_data        =  os_data_l-gdts    ; GDTs
  116. os_code        =  os_code_l-gdts
  117. int_code       equ  int_code_l-gdts
  118. int_data       equ  int_data_l-gdts
  119. tss0sys        equ  tss0sys_l-gdts
  120. graph_data     equ  3+graph_data_l-gdts
  121. tss0           equ  tss0_l-gdts
  122. tss0i          equ  tss0i_l-gdts
  123. app_code       equ  3+app_code_l-gdts
  124. app_data       equ  3+app_data_l-gdts
  125.  
  126.  
  127.  
  128. ; CR0 Flags - Protected mode and Paging
  129.  
  130.         mov     ecx,0x00000001
  131.         ;and     ebx,65535
  132.         ;cmp     ebx,00100000000000000b ; lfb -> paging
  133.         ;jb      no_paging
  134.         ;mov     ax,0x0000
  135.         ;mov     es,ax
  136.         ;mov     al,[es:0x901E]
  137.         ;cmp     al,1
  138.         ;je      no_paging
  139.         ;or      ecx, 0x80000000
  140.        ;no_paging:
  141.  
  142. ; Enabling 32 bit protected mode
  143.  
  144.         sidt    [cs:old_ints_h-0x10000]
  145.  
  146.         cli                             ; disable all irqs
  147.         cld
  148.         mov     al,255                  ; mask all irqs
  149.         out     0xa1,al
  150.         out     0x21,al
  151.    l.5: in      al, 0x64                ; Enable A20
  152.         test    al, 2
  153.         jnz     l.5
  154.         mov     al, 0xD1
  155.         out     0x64, al
  156.    l.6: in      al, 0x64
  157.         test    al, 2
  158.         jnz     l.6
  159.         mov     al, 0xDF
  160.         out     0x60, al
  161.         lgdt    [cs:gdts-0x10000]       ; Load GDT
  162.         mov     eax, cr0                ; Turn on paging // protected mode
  163.         or      eax, ecx
  164.         and     eax, 10011111b *65536*256 + 0xffffff ; caching enabled
  165.         mov     cr0, eax
  166.         jmp     byte $+2
  167.         mov     ax,os_data              ; Selector for os
  168.         mov     ds,ax
  169.         mov     es,ax
  170.         mov     fs,ax
  171.         mov     gs,ax
  172.         mov     ss,ax
  173.         mov     esp,0x30000             ; Set stack
  174.         jmp     pword os_code:B32       ; jmp to enable 32 bit mode
  175.  
  176. use32
  177.  
  178. iglobal
  179.   boot_memdetect    db   'Determining amount of memory',0
  180.   boot_fonts        db   'Fonts loaded',0
  181.   boot_tss          db   'Setting TSSs',0
  182.   boot_cpuid        db   'Reading CPUIDs',0
  183.   boot_devices      db   'Detecting devices',0
  184.   boot_timer        db   'Setting timer',0
  185.   boot_irqs         db   'Reprogramming IRQs',0
  186.   boot_setmouse     db   'Setting mouse',0
  187.   boot_windefs      db   'Setting window defaults',0
  188.   boot_bgr          db   'Calculating background',0
  189.   boot_resirqports  db   'Reserving IRQs & ports',0
  190.   boot_setrports    db   'Setting addresses for IRQs',0
  191.   boot_setostask    db   'Setting OS task',0
  192.   boot_allirqs      db   'Unmasking all IRQs',0
  193.   boot_tsc          db   'Reading TSC',0
  194.   boot_pal_ega      db   'Setting EGA/CGA 320x200 palette',0
  195.   boot_pal_vga      db   'Setting VGA 640x480 palette',0
  196.   boot_mtrr         db   'Setting MTRR',0
  197.   boot_tasking      db   'All set - press ESC to start',0
  198. endg
  199.  
  200. iglobal
  201.   boot_y dd 10
  202. endg
  203.  
  204. boot_log:
  205.          pushad
  206.  
  207.          mov   edx,esi
  208. .bll3:   inc   edx
  209.          cmp   [edx],byte 0
  210.          jne   .bll3
  211.          sub   edx,esi
  212.          mov   eax,10*65536
  213.          mov   ax,word [boot_y]
  214.          add   [boot_y],dword 10
  215.          mov   ebx,0xffffff
  216.          mov   ecx,esi
  217.          mov   edi,1
  218.          call  dtext
  219.  
  220.          mov   [novesachecksum],1000
  221.          call  checkEgaCga
  222.  
  223.          cmp   [preboot_blogesc],byte 1
  224.          je    .bll2
  225.  
  226.          cmp   esi,boot_tasking
  227.          jne   .bll2
  228.          ; begin ealex 04.08.05
  229. ;         in    al,0x61
  230. ;         and   al,01111111b
  231. ;         out   0x61,al
  232.          ; end ealex 04.08.05
  233. .bll1:   in    al,0x60    ; wait for ESC key press
  234.          cmp   al,129
  235.          jne   .bll1
  236.  
  237. .bll2:   popad
  238.  
  239.          ret
  240.  
  241. uglobal
  242.   cpuid_0    dd  0,0,0,0
  243.   cpuid_1    dd  0,0,0,0
  244.   cpuid_2    dd  0,0,0,0
  245.   cpuid_3    dd  0,0,0,0
  246. endg
  247.  
  248. iglobal
  249.   firstapp   db  'LAUNCHER   '
  250.   char       db  'CHAR    MT '
  251.   char2      db  'CHAR2   MT '
  252.   bootpath   db  '/KOLIBRI    '
  253.   bootpath2  db  0
  254.   vmode      db  'VMODE   MDR'
  255.   vrr_m      db  'VRR_M      '
  256. endg
  257.  
  258.  
  259. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  260. ;;                                                                      ;;
  261. ;;                          32 BIT ENTRY                                ;;
  262. ;;                                                                      ;;
  263. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  264.  
  265. align 4
  266.  
  267. B32:
  268. ; CLEAR 0x280000-0xF00000
  269.  
  270.         xor   eax,eax
  271.         mov   edi,0x280000
  272.         mov   ecx,(0x100000*0xF-0x280000) / 4
  273.         cld
  274.         rep   stosd
  275. ; CLEAR 0x80000-0x90000
  276. ;       xor   eax,eax
  277.         mov   edi,0x80000
  278.         mov   ecx,(0x90000-0x80000)/4
  279. ;       cld        
  280.         rep   stosd
  281.  
  282. ; CLEAR KERNEL UNDEFINED GLOBALS
  283.         mov   edi, endofcode
  284.         mov   ecx, (uglobals_size/4)+4
  285.         rep   stosd
  286.  
  287. ; SAVE & CLEAR 0-0xffff
  288.  
  289.         mov   esi,0x0000
  290.         mov   edi,0x2F0000
  291.         mov   ecx,0x10000 / 4
  292.         cld
  293.         rep   movsd
  294.         xor   eax,eax
  295.         mov   edi,0
  296.         mov   ecx,0x10000 / 4
  297.         cld
  298.         rep   stosd
  299.  
  300. ; SAVE REAL MODE VARIABLES
  301. ;        movzx eax,byte [0x2f0000+0x9010]  ; mouse port
  302. ;        mov   [0xF604],byte 1  ;al
  303.         mov   al,[0x2f0000+0x9000]        ; bpp
  304.         mov   [0xFBF1],al
  305.         movzx eax,word [0x2f0000+0x900A]  ; X max
  306.         dec   eax
  307.         mov   [0xfe00],eax
  308.         movzx eax,word [0x2f0000+0x900C]  ; Y max
  309.         dec   eax
  310.         mov   [0xfe04],eax
  311.         movzx eax,word [0x2f0000+0x9008]  ; screen mode
  312.         mov   [0xFE0C],eax
  313.         mov   eax,[0x2f0000+0x9014]       ; Vesa 1.2 bnk sw add
  314.         mov   [0xE030],eax
  315.         mov   [0xfe08],word 640*4         ; Bytes PerScanLine
  316.         cmp   [0xFE0C],word 0x13          ; 320x200
  317.         je    @f
  318.         cmp   [0xFE0C],word 0x12          ; VGA 640x480
  319.         je    @f
  320.         mov   ax,[0x2f0000+0x9001]        ; for other modes
  321.         mov   [0xfe08],ax
  322.       @@:
  323.  
  324. ; GRAPHICS ADDRESSES
  325.  
  326.         ;mov     eax,0x100000*8                    ; LFB address
  327.         ;cmp     [0xfe0c],word 0x13
  328.         ;je      no_d_lfb
  329.         ;cmp     [0xfe0c],word 0x12
  330.         ;je      no_d_lfb
  331.         ;cmp     [0x2f0000+0x901e],byte 1
  332.         ;jne     no_d_lfb
  333.         mov     byte [0x2f0000+0x901e],0x0
  334.         mov     eax,[0x2f0000+0x9018]
  335.       ;no_d_lfb:
  336.         mov     [0xfe80],eax
  337.  
  338.         cmp     [0xfe0c],word 0100000000000000b
  339.         jge     setvesa20
  340.         cmp     [0xfe0c],word 0x13
  341.         je      v20ga32
  342.         mov     [0xe020],dword Vesa12_putpixel24  ; Vesa 1.2
  343.         mov     [0xe024],dword Vesa12_getpixel24
  344.         cmp     [0xfbf1],byte 24
  345.         jz      ga24
  346.         mov     [0xe020],dword Vesa12_putpixel32
  347.         mov     [0xe024],dword Vesa12_getpixel32
  348.       ga24:
  349.         jmp     v20ga24
  350.       setvesa20:
  351.         mov     [0xe020],dword Vesa20_putpixel24  ; Vesa 2.0
  352.         mov     [0xe024],dword Vesa20_getpixel24
  353.         cmp     [0xfbf1],byte 24
  354.         jz      v20ga24
  355.       v20ga32:
  356.         mov     [0xe020],dword Vesa20_putpixel32
  357.         mov     [0xe024],dword Vesa20_getpixel32
  358.       v20ga24:
  359.         cmp     [0xfe0c],word 0x12                ; 16 C VGA 640x480
  360.         jne     no_mode_0x12
  361.         mov     [0xe020],dword VGA_putpixel
  362.         mov     [0xe024],dword Vesa20_getpixel32
  363.       no_mode_0x12:
  364.  
  365. ; MEMORY MODEL
  366.  
  367. ;        mov     [0xfe84],dword 0x100000*16        ; apps mem base address
  368. ;        movzx   ecx,byte [0x2f0000+0x9030]
  369. ;        dec     ecx
  370. ;        mov     eax,16*0x100000 ; memory-16
  371. ;        shl     eax,cl
  372. ;        mov     [0xfe8c],eax      ; memory for use
  373. ;        cmp     eax,16*0x100000
  374. ;        jne     no16mb
  375. ;        mov     [0xfe84],dword 0xD80000 ; !!! 10 !!!
  376. ;      no16mb:
  377.  
  378. ; init:
  379. ;  1) 0xFE84 - applications base
  380. ;  2) 0xFE8C - total amount of memory
  381.  
  382.         xor     edi, edi
  383.   m_GMS_loop:
  384.         add     edi, 0x400000
  385.         mov     eax, dword [edi]
  386.         mov     dword [edi], 'TEST'
  387.         wbinvd
  388.         cmp     dword [edi], 'TEST'
  389.         jne     m_GMS_exit
  390.         cmp     dword [0], 'TEST'
  391.         je      m_GMS_exit
  392.         mov     dword [es:edi], eax
  393.         jmp     m_GMS_loop
  394.   m_GMS_exit:
  395.         mov     [edi], eax
  396.         ; now edi contains the EXACT amount of memory
  397.  
  398.         mov     eax, 0x100000*16
  399.         cmp     edi, eax ;0x100000*16
  400.         jb      $                 ; less than 16 Mb
  401.  
  402.         mov     dword [0xFE84], eax ;0x100000*16
  403.         cmp     edi, eax ;0x100000*16
  404.         jne     @f
  405.         mov     dword [0xFE84], 0xD80000 ; =0x100000*13.5
  406.       @@:
  407.         mov     dword [0xFE8C], edi
  408.        
  409. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  410. include 'detect/disks.inc'
  411. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  412.        
  413. ; CHECK EXTRA REGION
  414. ; ENABLE PAGING
  415.         mov     eax,cr0
  416.         or      eax,0x80000000
  417.         mov     cr0,eax
  418.         jmp     $+2
  419.         mov     dword [0xfe80],0x800000
  420.        
  421. ;Set base of graphic segment to linear address of LFB        
  422.         mov     eax,[0xfe80]                      ; set for gs
  423.         mov     [graph_data_l+2],ax
  424.         shr     eax,16
  425.         mov     [graph_data_l+4],al
  426.         mov     [graph_data_l+7],ah            
  427.  
  428. ; READ RAMDISK IMAGE FROM HD
  429.  
  430. ;!!!!!!!!!!!!!!!!!!!!!!!
  431. include 'boot/rdload.inc'
  432. ;!!!!!!!!!!!!!!!!!!!!!!!
  433. ;    mov    [dma_hdd],1
  434. ; CALCULATE FAT CHAIN FOR RAMDISK
  435.  
  436.         call  calculatefatchain
  437.  
  438. ; LOAD VMODE DRIVER
  439.  
  440. ;!!!!!!!!!!!!!!!!!!!!!!!
  441. include 'vmodeld.inc'
  442. ;!!!!!!!!!!!!!!!!!!!!!!!
  443.  
  444. ; LOAD FONTS I and II
  445.  
  446.         mov   [0x3000],dword 1
  447.         mov   [0x3004],dword 1
  448.         mov   [0x3010],dword 0x3020
  449.  
  450.         mov   eax,char
  451.         mov   esi,12
  452.         xor   ebx,ebx
  453.         mov   ecx,26000
  454.         mov   edx,0x37000
  455.         call  fileread
  456.  
  457.         mov   eax,char2
  458.         mov   esi,12
  459.         xor   ebx,ebx
  460.         mov   ecx,26000
  461.         mov   edx,0x30000
  462.         call  fileread
  463.  
  464.         mov   esi,boot_fonts
  465.         call  boot_log
  466.  
  467. ; PRINT AMOUNT OF MEMORY
  468.         mov     esi, boot_memdetect
  469.         call    boot_log
  470.  
  471.         movzx   ecx, word [boot_y]
  472.         or      ecx, (10+29*6) shl 16 ; "Determining amount of memory"
  473.         sub     ecx, 10
  474.         mov     edx, 0xFFFFFF
  475.         mov     ebx, [0xFE8C]
  476.         shr     ebx, 20
  477.         mov     edi, 1
  478.         mov     eax, 0x00040000
  479.         call    display_number
  480.        
  481. ; CHECK EXTENDED REGION
  482. ;        mov     dword [0x80000000],0x12345678
  483. ;        cmp     dword [0x80000000],0x12345678
  484. ;        jz      extended_region_found
  485. ;        mov     esi,boot_ext_region
  486. ;        call    boot_log
  487. ;        jmp     $
  488. ;extended_region_found:
  489.        
  490.         call    MEM_Init
  491. ;add 0x800000-0xc00000 area        
  492.         cmp     word [0xfe0c],0x13
  493.         jle     .less_memory
  494.         mov     eax,0x80000000      ;linear address
  495.         mov     ebx,0x400000 shr 12 ;size in pages (4Mb)
  496.         mov     ecx,0x800000        ;physical address
  497.         jmp     .end_first_block
  498. .less_memory:
  499.         mov     eax,0x80180000      ;linear address
  500.         mov     ebx,0x280000 shr 12 ;size in pages (2.5Mb)
  501.         mov     ecx,0x980000        ;physical address
  502. .end_first_block:                
  503.         call    MEM_Add_Heap        ;nobody can lock mutex yet
  504.  
  505.         call    create_general_page_table
  506. ;add 0x1000000(0xd80000)-end_of_memory area
  507.         mov     eax,second_base_address
  508.         mov     ebx,[0xfe8c]
  509.         mov     ecx,[0xfe84]
  510.         sub     ebx,ecx
  511.         shr     ebx,12
  512.         add     eax,ecx
  513.         call    MEM_Add_Heap
  514. ;init physical memory manager.
  515.         call    Init_Physical_Memory_Manager
  516.        
  517. ; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
  518.  
  519.         mov   esi,boot_irqs
  520.         call  boot_log
  521.         call  rerouteirqs
  522.  
  523.         mov    esi,boot_tss
  524.         call   boot_log
  525.  
  526. ; BUILD SCHEDULER
  527.  
  528.         call   build_scheduler ; sys32.inc
  529.  
  530. ; LOAD IDT
  531.         ; <IP 05.02.2005>
  532.         lidt   [cs:idtreg] ;[cs:idts]
  533.         ; </IP>
  534.  
  535. ; READ CPUID RESULT
  536.  
  537.         mov     esi,boot_cpuid
  538.         call    boot_log
  539.         pushfd                  ; get current flags
  540.         pop     eax
  541.         mov     ecx,eax
  542.         xor     eax,0x00200000  ; attempt to toggle ID bit
  543.         push    eax
  544.         popfd
  545.         pushfd                  ; get new EFLAGS
  546.         pop     eax
  547.         push    ecx             ; restore original flags
  548.         popfd
  549.         and     eax,0x00200000  ; if we couldn't toggle ID,
  550.         and     ecx,0x00200000  ; then this is i486
  551.         cmp     eax,ecx
  552.         jz      nopentium
  553.         ; It's Pentium or later. Use CPUID
  554.         mov     edi,cpuid_0
  555.         mov     esi,0
  556.       cpuid_new_read:
  557.         mov     eax,esi
  558.         cpuid
  559.         call    cpuid_save
  560.         add     edi,4*4
  561.         cmp     esi,3
  562.         jge     cpuid_done
  563.         cmp     esi,[cpuid_0]
  564.         jge     cpuid_done
  565.         inc     esi
  566.         jmp     cpuid_new_read
  567.       cpuid_save:
  568.         mov     [edi+00],eax
  569.         mov     [edi+04],ebx
  570.         mov     [edi+8],ecx
  571.         mov     [edi+12],edx
  572.         ret
  573.       cpuid_done:
  574.       nopentium:
  575.  
  576. ; CR4 flags - enable fxsave / fxrstore
  577. ;
  578. ;        finit
  579. ;        mov     eax,1
  580. ;        cpuid
  581. ;        test    edx,1000000h
  582. ;        jz      fail_fpu
  583. ;        mov     eax,cr4
  584. ;        or      eax,200h        ; Enable fxsave/fxstor
  585. ;        mov     cr4,eax
  586. ;     fail_fpu:
  587.  
  588. ; DETECT DEVICES
  589.  
  590.         mov    esi,boot_devices
  591.         call   boot_log
  592.         call   detect_devices
  593.  
  594.  ; TIMER SET TO 1/100 S
  595.  
  596.         mov   esi,boot_timer
  597.         call  boot_log
  598.         mov   al,0x34              ; set to 100Hz
  599.         out   0x43,al
  600.         mov   al,0x9b              ; lsb    1193180 / 1193
  601.         out   0x40,al
  602.         mov   al,0x2e              ; msb
  603.         out   0x40,al
  604.  
  605. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  606. ;include 'detect/commouse.inc'
  607. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  608. ; SET MOUSE
  609.  
  610.         mov   esi,boot_setmouse
  611.         call  boot_log
  612.         call  setmouse
  613.  
  614. ; SET PRELIMINARY WINDOW STACK AND POSITIONS
  615.  
  616.         mov   esi,boot_windefs
  617.         call  boot_log
  618.         call  setwindowdefaults
  619.  
  620. ; SET BACKGROUND DEFAULTS
  621.  
  622.         mov   esi,boot_bgr
  623.         call  boot_log
  624.         call  calculatebackground
  625.  
  626. ; RESERVE SYSTEM IRQ'S JA PORT'S
  627.  
  628.         mov   esi,boot_resirqports
  629.         call  boot_log
  630.         call  reserve_irqs_ports
  631.  
  632. ; SET PORTS FOR IRQ HANDLERS
  633.  
  634.         mov  esi,boot_setrports
  635.         call boot_log
  636.         call setirqreadports
  637.  
  638. ; SET UP OS TASK
  639.  
  640.         mov  esi,boot_setostask
  641.         call boot_log
  642.         ; name for OS/IDLE process
  643.         mov  [0x80000+256+0],dword 'OS/I'
  644.         mov  [0x80000+256+4],dword 'DLE '
  645.         ; task list
  646.         mov  [0x3004],dword 2         ; number of processes
  647.         mov  [0x3000],dword 0         ; process count - start with os task
  648.         mov  [0x3020+0xE],byte  1     ; on screen number
  649.         mov  [0x3020+0x4],dword 1     ; process id number
  650.  
  651.         ; set default flags & stacks
  652.         mov  [l.eflags],dword 0x11202 ; sti and resume
  653.         mov  [l.ss0], os_data
  654.         ;mov  [l.ss1], ring1_data
  655.         ;mov  [l.ss2], ring2_data
  656.         mov  [l.esp0], 0x52000
  657.         mov  [l.esp1], 0x53000
  658.         mov  [l.esp2], 0x54000
  659.         ; osloop - TSS
  660.         mov  eax,cr3
  661.         mov  [l.cr3],eax
  662.         mov  [l.eip],osloop
  663. ; <Ivan Poddubny 14/03/2004>
  664.         mov  [l.esp],0x30000 ;0x2ffff
  665. ; </Ivan Poddubny 14/03/2004>
  666.         mov  [l.cs],os_code
  667.         mov  [l.ss],os_data
  668.         mov  [l.ds],os_data
  669.         mov  [l.es],os_data
  670.         mov  [l.fs],os_data
  671.         mov  [l.gs],os_data
  672.         ; move tss to tss_data+tss_step
  673.         mov  esi,tss_sceleton
  674.         mov  edi,tss_data+tss_step
  675.         mov  ecx,120/4
  676.         cld
  677.         rep  movsd
  678.  
  679.         mov  ax,tss0
  680.         ltr  ax
  681.  
  682.  
  683. ; READ TSC / SECOND
  684.  
  685.         mov   esi,boot_tsc
  686.         call  boot_log
  687.         call  _rdtsc
  688.         mov   ecx,eax
  689.         mov   esi,250               ; wait 1/4 a second
  690.         call  delay_ms
  691.         call  _rdtsc
  692.         sub   eax,ecx
  693.         shl   eax,2
  694.         mov   [0xf600],eax          ; save tsc / sec
  695.  
  696. ; SET VARIABLES
  697.  
  698.         call  set_variables
  699.  
  700. ; STACK AND FDC
  701.  
  702.         call  stack_init
  703.         call  fdc_init
  704.  
  705. ; PALETTE FOR 320x200 and 640x480 16 col
  706.  
  707.         cmp   [0xfe0c],word 0x12
  708.         jne   no_pal_vga
  709.         mov   esi,boot_pal_vga
  710.         call  boot_log
  711.         call  paletteVGA
  712.       no_pal_vga:
  713.  
  714.         cmp   [0xfe0c],word 0x13
  715.         jne   no_pal_ega
  716.         mov   esi,boot_pal_ega
  717.         call  boot_log
  718.         call  palette320x200
  719.       no_pal_ega:
  720.  
  721. ; LOAD DEFAULT SKIN
  722.  
  723.         call  load_default_skin
  724.  
  725. ; MTRR'S
  726.  
  727.         call  enable_mtrr
  728.  
  729.  
  730. ; LOAD FIRST APPLICATION
  731.         mov   [0x3000],dword 1 ;1
  732.         mov   [0x3004],dword 1 ;1
  733.         cli
  734.         mov   al,[0x2f0000+0x9030]
  735.         cmp   al,1
  736.         jne   no_load_vrr_m
  737.         mov   eax,vrr_m
  738.         call  start_application_fl
  739.         cmp   eax,2                  ; if no vrr_m app found
  740.         je    first_app_found
  741.        
  742.     no_load_vrr_m:    
  743.         mov   eax,firstapp
  744.         call  start_application_fl
  745.  
  746.         cmp   eax,2                  ; if no first app found - halt
  747.         je    first_app_found
  748.         mov   eax, 0xDEADBEEF
  749.         hlt    ;jmp   $
  750.       first_app_found:
  751.         cli
  752.  
  753.         mov   [0x3004],dword 2
  754.         mov   [0x3000],dword 1
  755.  
  756.  
  757. ; START MULTITASKING
  758.  
  759.         mov   esi,boot_tasking
  760.         call  boot_log
  761.  
  762.         mov   [0xe000],byte 1        ; multitasking enabled
  763.  
  764.     mov   al, 0xf6         ; Ñáðîñ êëàâèàòóðû, ðàçðåøèòü ñêàíèðîâàíèå
  765.         call  kb_write
  766.  
  767.         mov     ecx,0
  768. wait_loop_1:       ; variant 2
  769. ; ÷èòàåì ïîðò ñîñòîÿíèÿ ïðîöåññîðà 8042
  770.         in      al,64h
  771.     and     al,00000010b  ; ôëàã ãîòîâíîñòè
  772. ; îæèäàåì ãîòîâíîñòü ïðîöåññîðà 8042
  773.     loopnz  wait_loop_1
  774.  
  775. ; SET KEYBOARD PARAMETERS
  776.        ; mov   al, 0xED       ; svetodiody - only for testing!
  777.        ; call  kb_write
  778.        ; call  kb_read
  779.        ; mov   al, 111b
  780.        ; call  kb_write
  781.        ; call  kb_read
  782.        
  783.         mov   al, 0xF3       ; set repeat rate & delay
  784.         call  kb_write
  785.         call  kb_read
  786.         mov   al, 00100010b ; 24 500  ;00100100b  ; 20 500
  787.         call  kb_write
  788.         call  kb_read
  789.      ;// mike.dld [
  790.         call  set_lights
  791.      ;// mike.dld ]
  792.  
  793.  
  794. ; UNMASK ALL IRQ'S
  795.  
  796.         mov   esi,boot_allirqs
  797.         call  boot_log
  798.        
  799.         cli                          ;guarantee forbidance of interrupts.
  800.         mov   al,0                   ; unmask all irq's
  801.         out   0xA1,al
  802.         out   0x21,al
  803.  
  804.         mov   ecx,32
  805.  
  806.      ready_for_irqs:
  807.  
  808.         mov   al,0x20                ; ready for irqs
  809.         out   0x20,al
  810.         out   0xa0,al
  811.  
  812.         loop  ready_for_irqs         ; flush the queue
  813.  
  814. ;        mov    [dma_hdd],1
  815.  
  816.         sti
  817.         jmp   $                      ; wait here for timer to take control
  818.  
  819.         ; Fly :)
  820.  
  821. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  822. ;                                                                    ;
  823. ;                         MAIN OS LOOP                               ;
  824. ;                                                                    ;
  825. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  826. align 32
  827. osloop:
  828.  
  829.         call   check_mouse_data
  830.         call   [draw_pointer]
  831.  
  832.         call   checkbuttons
  833.         call   main_loop_sys_getkey
  834.         call   checkwindows
  835.         call   check_window_move_request
  836.  
  837.         call   checkmisc
  838.         call   checkEgaCga
  839.  
  840.         call   stack_handler
  841.  
  842.         call   checkidle
  843.         call   check_fdd_motor_status
  844.         jmp    osloop
  845. ;temp_pointers:
  846. ; rd 32
  847. ; seed dd 0x12345678
  848.  
  849. checkidle:
  850.  
  851.         pushad
  852.  
  853.         cmp  [check_idle_semaphore],0
  854.         jne  no_idle_state
  855.  
  856.         call change_task
  857.         mov  eax,[idlemem]
  858.         mov  ebx,[timer_ticks] ;[0xfdf0]
  859.         cmp  eax,ebx
  860.         jnz  idle_exit
  861.         call _rdtsc
  862.         mov  ecx,eax
  863.       idle_loop:
  864.         hlt
  865.         cmp  [check_idle_semaphore],0
  866.         jne  idle_loop_exit
  867.         mov  eax,[timer_ticks] ;[0xfdf0]
  868.         cmp  ebx,eax
  869.         jz   idle_loop
  870.       idle_loop_exit:
  871.         mov  [idlemem],eax
  872.         call _rdtsc
  873.         sub  eax,ecx
  874.         mov  ebx,[idleuse]
  875.         add  ebx,eax
  876.         mov  [idleuse],ebx
  877.  
  878.         popad
  879.         ret
  880.  
  881.       idle_exit:
  882.  
  883.         mov  ebx,[timer_ticks] ;[0xfdf0]
  884.         mov  [idlemem],ebx
  885.         call change_task
  886.  
  887.         popad
  888.         ret
  889.  
  890.       no_idle_state:
  891.  
  892.         dec  [check_idle_semaphore]
  893.  
  894.         mov  ebx,[timer_ticks] ;[0xfdf0]
  895.         mov  [idlemem],ebx
  896.         call change_task
  897.  
  898.         popad
  899.         ret
  900.  
  901. uglobal
  902.   idlemem               dd   0x0
  903.   idleuse               dd   0x0
  904.   idleusesec            dd   0x0
  905.   check_idle_semaphore  dd   0x0
  906. endg
  907.  
  908.  
  909.  
  910. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  911. ;                                                                      ;
  912. ;                   INCLUDED SYSTEM FILES                              ;
  913. ;                                                                      ;
  914. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  915.  
  916.  
  917. include "kernel32.inc"
  918.  
  919.  
  920. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  921. ;                                                                      ;
  922. ;                       KERNEL FUNCTIONS                               ;
  923. ;                                                                      ;
  924. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  925.  
  926. enable_mtrr:
  927.  
  928.         pushad
  929.  
  930.         cmp    [0x2f0000+0x901c],byte 2
  931.         je     no_mtrr
  932.         mov    eax,[0xFE0C]                ; if no LFB then no MTRR
  933.         test   eax,0100000000000000b
  934.         jz     no_mtrr
  935.         mov    edx,[cpuid_1+3*4]           ; edx - MTRR's supported ?
  936.         test   edx,1000000000000b
  937.         jz     no_mtrr
  938.         call   find_empty_mtrr
  939.         cmp    ecx,0
  940.         jz     no_mtrr
  941.         mov    esi,boot_mtrr               ; 'setting mtrr'
  942.         call   boot_log
  943.         mov    edx,0x0                     ; LFB , +8 M , write combine
  944.         mov    eax,[0x2f9018]
  945.         or     eax,1
  946.         wrmsr
  947.         inc    ecx
  948.         mov    edx,0xf
  949.         mov    eax,0xff800800
  950.         wrmsr
  951.         mov    ecx,0x2ff                   ; enable mtrr's
  952.         rdmsr
  953.         or     eax,100000000000b           ; set
  954.         wrmsr
  955.      no_mtrr:
  956.  
  957.         popad
  958.         ret
  959.  
  960.  
  961. find_empty_mtrr:  ; 8 pairs checked
  962.  
  963.         mov    ecx,0x201-2
  964.       mtrr_find:
  965.         add    ecx,2
  966.         cmp    ecx,0x200+8*2
  967.         jge    no_free_mtrr
  968.         rdmsr
  969.         test   eax,0x0800
  970.         jnz    mtrr_find
  971.         dec    ecx
  972.         ret
  973.       no_free_mtrr:
  974.         mov    ecx,0
  975.         ret
  976.  
  977. reserve_irqs_ports:
  978.  
  979.         pushad
  980.  
  981.         mov  [irq_owner+4*0],byte 1    ; timer
  982.         mov  [irq_owner+4*1],byte 1    ; keyboard
  983.         mov  [irq_owner+4*5],byte 1    ; sound blaster
  984.         mov  [irq_owner+4*6],byte 1    ; floppy diskette
  985.         mov  [irq_owner+4*13],byte 1   ; math co-pros
  986.         mov  [irq_owner+4*14],byte 1   ; ide I
  987.         mov  [irq_owner+4*15],byte 1   ; ide II
  988.         movzx eax,byte [0xf604]        ; mouse irq
  989.         dec   eax
  990.         add   eax,mouseirqtable
  991.         movzx eax,byte [eax]
  992.         shl   eax,2
  993.         mov   [irq_owner+eax],byte 1
  994.  
  995.  
  996.                                        ; RESERVE PORTS
  997.         mov   edi,1                    ; 0x00-0xff
  998.         mov   [0x2d0000],edi
  999.         shl   edi,4
  1000.         mov   [0x2d0000+edi+0],dword 1
  1001.         mov   [0x2d0000+edi+4],dword 0x0
  1002.         mov   [0x2d0000+edi+8],dword 0xff
  1003.         cmp   [0xf604],byte 2          ; com1 mouse -> 0x3f0-0x3ff
  1004.         jne   ripl1
  1005.         inc   dword [0x2d0000]
  1006.         mov   edi,[0x2d0000]
  1007.         shl   edi,4
  1008.         mov   [0x2d0000+edi+0],dword 1
  1009.         mov   [0x2d0000+edi+4],dword 0x3f0
  1010.         mov   [0x2d0000+edi+8],dword 0x3ff
  1011.       ripl1:
  1012.         cmp   [0xf604],byte 3          ; com2 mouse -> 0x2f0-0x2ff
  1013.         jne   ripl2
  1014.         inc   dword [0x2d0000]
  1015.         mov   edi,[0x2d0000]
  1016.         shl   edi,4
  1017.         mov   [0x2d0000+edi+0],dword 1
  1018.         mov   [0x2d0000+edi+4],dword 0x2f0
  1019.         mov   [0x2d0000+edi+8],dword 0x2ff
  1020.       ripl2:
  1021.  
  1022.         popad
  1023.         ret
  1024.  
  1025. iglobal
  1026. mouseirqtable   db  12    ; ps2
  1027.                 db  4     ; com1
  1028.                 db  3     ; com2
  1029. endg
  1030.  
  1031. setirqreadports:
  1032.  
  1033.         mov   [irq12read+0],dword 0x60 + 0x01000000  ; read port 0x60 , byte
  1034.         mov   [irq12read+4],dword 0                  ; end of port list
  1035.         mov   [irq04read+0],dword 0x3f8 + 0x01000000 ; read port 0x3f8 , byte
  1036.         mov   [irq04read+4],dword 0                  ; end of port list
  1037.         mov   [irq03read+0],dword 0x2f8 + 0x01000000 ; read port 0x2f8 , byte
  1038.         mov   [irq03read+4],dword 0                  ; end of port list
  1039.  
  1040.         ret
  1041.  
  1042. iglobal
  1043.   process_number dd 0x1
  1044. endg
  1045.  
  1046. uglobal
  1047.   novesachecksum dd 0x0
  1048. endg
  1049.  
  1050. checkEgaCga:
  1051.  
  1052.         cmp    [0xfe0c],dword 0x13
  1053.         je     cnvl
  1054.         ret
  1055.       cnvl:
  1056.  
  1057.         pushad
  1058.         mov    ecx,[0xfb0a]
  1059.         cmp    ecx,[novesachecksum]
  1060.         jne    novesal
  1061.         popad
  1062.         ret
  1063.  
  1064.       novesal:
  1065.         mov    [novesachecksum],ecx
  1066.         mov    ecx,0
  1067.         movzx  eax,word [0xfb0c]
  1068.         cmp    eax,100
  1069.         jge    m13l3
  1070.         mov    eax,100
  1071.       m13l3:
  1072.         cmp    eax,480-100
  1073.         jbe    m13l4
  1074.         mov    eax,480-100
  1075.       m13l4:
  1076.         sub    eax,100
  1077.         imul   eax,640*4
  1078.         add    ecx,eax
  1079.         movzx  eax,word [0xfb0a]
  1080.         cmp    eax,160
  1081.         jge    m13l1
  1082.         mov    eax,160
  1083.       m13l1:
  1084.         cmp    eax,640-160
  1085.         jbe    m13l2
  1086.         mov    eax,640-160
  1087.       m13l2:
  1088.         sub    eax,160
  1089.         shl    eax,2
  1090.         add    ecx,eax
  1091.         mov    esi,[0xfe80]
  1092.         add    esi,ecx
  1093.         mov    edi,0xa0000
  1094.         mov    edx,200
  1095.         mov    ecx,320
  1096.         cld
  1097.      m13pix:
  1098.         lodsd
  1099.         push   eax
  1100.         mov    ebx,eax
  1101.         and    eax,(128+64+32)      ; blue
  1102.         shr    eax,5
  1103.         and    ebx,(128+64+32)*256  ; green
  1104.         shr    ebx,8+2
  1105.         add    eax,ebx
  1106.         pop    ebx
  1107.         and    ebx,(128+64)*256*256 ; red
  1108.         shr    ebx,8+8
  1109.         add    eax,ebx
  1110.         stosb
  1111.         loop   m13pix
  1112.         mov    ecx,320
  1113.         add    esi,4*(640-320)
  1114.         dec    edx
  1115.         jnz    m13pix
  1116.  
  1117.         popad
  1118.         ret
  1119.  
  1120.  
  1121. palette320x200:
  1122.  
  1123.        mov   edx,0x3c8
  1124.        xor   eax, eax
  1125.        out   dx,al
  1126.        mov   ecx,256
  1127.        mov   edx,0x3c9
  1128.        xor   eax,eax
  1129.  
  1130.      palnew:
  1131.        mov   al,0
  1132.        test  ah,64
  1133.        jz    pallbl1
  1134.        add   al,21
  1135.      pallbl1:
  1136.        test  ah,128
  1137.        jz    pallbl2
  1138.        add   al,42
  1139.      pallbl2:
  1140.        out   dx,al
  1141.        mov   al,0
  1142.        test  ah,8
  1143.        jz    pallbl3
  1144.        add   al,8
  1145.      pallbl3:
  1146.        test  ah,16
  1147.        jz    pallbl4
  1148.        add   al,15
  1149.      pallbl4:
  1150.        test  ah,32
  1151.        jz    pallbl5
  1152.        add   al,40
  1153.      pallbl5:
  1154.        out   dx,al
  1155.        mov   al,0
  1156.        test  ah,1
  1157.        jz    pallbl6
  1158.        add   al,8
  1159.      pallbl6:
  1160.        test  ah,2
  1161.        jz    pallbl7
  1162.        add   al,15
  1163.      pallbl7:
  1164.        test  ah,4
  1165.        jz    pallbl8
  1166.        add   al,40
  1167.      pallbl8:
  1168.        out   dx,al
  1169.        add   ah,1
  1170.        loop  palnew
  1171.  
  1172.        ret
  1173.  
  1174. set_variables:
  1175.  
  1176.         mov   ecx,0x100                       ; flush port 0x60
  1177. .fl60:  in    al,0x60
  1178.         loop  .fl60
  1179.         mov   [0xfcff],byte 0                 ; mouse buffer
  1180.         mov   [0xf400],byte 0                 ; keyboard buffer
  1181.         mov   [0xf500],byte 0                 ; button buffer
  1182. ;        mov   [0xfb0a],dword 100*65536+100    ; mouse x/y
  1183.  
  1184.         push  eax
  1185.         mov   ax,[0x2f0000+0x900c]
  1186.         shr   ax,1
  1187.         shl   eax,16
  1188.         mov   ax,[0x2f0000+0x900A]
  1189.         shr   ax,1
  1190.         mov   [0xfb0a],eax
  1191.         pop   eax
  1192.        
  1193.         mov   byte [SB16_Status],0            ; Minazzi Paolo
  1194.         mov   [display_data-12],dword 1       ; tiled background
  1195.         mov   [0xfe88],dword 0x2C0000         ; address of button list
  1196.  
  1197.      ;!! IP 04.02.2005:
  1198.         mov   [next_usage_update], 100
  1199.         mov   byte [0xFFFF], 0 ; change task if possible
  1200.  
  1201.         ret
  1202.  
  1203. ;* mouse centered - start code- Mario79
  1204. mouse_centered:
  1205.         push  eax
  1206.         mov   eax,[0xFE00]
  1207.         shr   eax,1
  1208.         mov   [0xFB0A],ax
  1209.         mov   eax,[0xFE04]
  1210.         shr   eax,1
  1211.         mov   [0xFB0C],ax
  1212.         pop   eax
  1213.         ret
  1214. ;* mouse centered - end code- Mario79
  1215.  
  1216. align 4
  1217.  
  1218. sys_outport:
  1219.  
  1220.     mov   edi,ebx          ; separate flag for read / write
  1221.     and   ebx,65535
  1222.  
  1223.     mov   ecx,[0x2d0000]
  1224.     test  ecx,ecx
  1225.     jne   sopl8
  1226.     mov   [esp+36],dword 1
  1227.     ret
  1228.  
  1229.   sopl8:
  1230.     mov   edx,[0x3010]
  1231.     mov   edx,[edx+0x4]
  1232.     and   ebx,65535
  1233.     cld
  1234.   sopl1:
  1235.  
  1236.     mov   esi,ecx
  1237.     shl   esi,4
  1238.     add   esi,0x2d0000
  1239.     cmp   edx,[esi+0]
  1240.     jne   sopl2
  1241.     cmp   ebx,[esi+4]
  1242.     jb    sopl2
  1243.     cmp   ebx,[esi+8]
  1244.     jg    sopl2
  1245.     jmp   sopl3
  1246.  
  1247.   sopl2:
  1248.  
  1249.     dec   ecx
  1250.     jnz   sopl1
  1251.     mov   [esp+36],dword 1
  1252.     ret
  1253.  
  1254.   sopl3:
  1255.  
  1256.     test  edi,0x80000000 ; read ?
  1257.     jnz   sopl4
  1258.  
  1259.     mov   dx,bx          ; write
  1260.     out   dx,al
  1261.     mov   [esp+36],dword 0
  1262.     ret
  1263.  
  1264.   sopl4:
  1265.  
  1266.     mov   dx,bx          ; read
  1267.     in    al,dx
  1268.     and   eax,0xff
  1269.     mov   [esp+36],dword 0
  1270.     mov   [esp+24],eax
  1271.     ret
  1272.  
  1273.  
  1274.  
  1275. align 4
  1276. sys_sb16:
  1277.  
  1278.      cmp  word [sb16],word 0
  1279.      jnz  sb16l1
  1280.      mov  [esp+36],dword 1
  1281.      ret
  1282.    sb16l1:
  1283.      mov  [esp+36],dword 0
  1284.      cmp  eax,1    ; set volume - main
  1285.      jnz  sb16l2
  1286.      mov  dx,word [sb16]
  1287.      add  dx,4
  1288.      mov  al,0x22
  1289.      out  dx,al
  1290.      mov  esi,1
  1291.      call delay_ms
  1292.      mov  eax,ebx
  1293.      inc  edx
  1294.      out  dx,al
  1295.      ret
  1296.    sb16l2:
  1297.  
  1298.      cmp  eax,2    ; set volume - cd
  1299.      jnz  sb16l3
  1300.      mov  dx,word [sb16]
  1301.      add  dx,4
  1302.      mov  al,0x28
  1303.      out  dx,al
  1304.      mov  esi,1
  1305.      call delay_ms
  1306.      mov  eax,ebx
  1307.      add  edx,1
  1308.      out  dx,al
  1309.      ret
  1310.    sb16l3:
  1311.       mov  [esp+36],dword 2
  1312.       ret
  1313.  
  1314.  
  1315. align 4
  1316.  
  1317. sys_sb16II:
  1318.  
  1319.      cmp  word [sb16],word 0
  1320.      jnz  IIsb16l1
  1321.      mov  [esp+36],dword 1
  1322.      ret
  1323.    IIsb16l1:
  1324.  
  1325.      cmp  eax,1    ; set volume - main
  1326.      jnz  IIsb16l2
  1327.      ; L
  1328.      mov  dx,word [sb16]
  1329.      add  dx,4
  1330.      mov  al,0x30
  1331.      out  dx,al
  1332.      mov  eax,ebx
  1333.      inc  edx
  1334.      out  dx,al
  1335.      ; R
  1336.      mov  dx,word [sb16]
  1337.      add  dx,4
  1338.      mov  al,0x31
  1339.      out  dx,al
  1340.      mov  eax,ebx
  1341.      inc  edx
  1342.      out  dx,al
  1343.      mov  [esp+36],dword 0
  1344.      ret
  1345.    IIsb16l2:
  1346.  
  1347.      cmp  eax,2    ; set volume - cd
  1348.      jnz  IIsb16l3
  1349.      ; L
  1350.      mov  dx,word [sb16]
  1351.      add  dx,4
  1352.      mov  al,0x36
  1353.      out  dx,al
  1354.      mov  eax,ebx
  1355.      inc  edx
  1356.      out  dx,al
  1357.      ; R
  1358.      mov  dx,word [sb16]
  1359.      add  dx,4
  1360.      mov  al,0x37
  1361.      out  dx,al
  1362.      mov  eax,ebx
  1363.      inc  edx
  1364.      out  dx,al
  1365.      mov  [esp+36],dword 0
  1366.      ret
  1367.    IIsb16l3:
  1368.  
  1369.      mov  [esp+36],dword 2
  1370.      ret
  1371.  
  1372.  
  1373. align 4
  1374.  
  1375. sys_wss:
  1376.  
  1377.      cmp  word [wss],word 0
  1378.      jnz  wssl1
  1379.      mov  [esp+36],dword 1
  1380.      ret
  1381.    wssl1:
  1382.  
  1383.      cmp  eax,1    ; set volume - main
  1384.      jnz  wssl2
  1385.      mov  [esp+36],dword 0
  1386.      ret
  1387.    wssl2:
  1388.  
  1389.      cmp  eax,2    ; set volume - cd
  1390.      jnz  wssl3
  1391.      ; L
  1392.      mov  dx,word [wss]
  1393.      add  dx,4
  1394.      mov  al,0x2
  1395.      out  dx,al
  1396.      mov  esi,1
  1397.      call delay_ms
  1398.      mov  eax,ebx
  1399.      inc  edx
  1400.      out  dx,al
  1401.      ; R
  1402.      mov  dx,word [wss]
  1403.      add  dx,4
  1404.      mov  al,0x3
  1405.      out  dx,al
  1406.      mov  esi,1
  1407.      call delay_ms
  1408.      mov  eax,ebx
  1409.      inc  edx
  1410.      out  dx,al
  1411.      mov  [esp+36],dword 0
  1412.      ret
  1413.    wssl3:
  1414.      mov   [esp+36],dword 2
  1415.      ret
  1416.  
  1417. display_number:
  1418.  
  1419. ; eax = print type, al=0 -> ebx is number
  1420. ;                   al=1 -> ebx is pointer
  1421. ;                   ah=0 -> display decimal
  1422. ;                   ah=1 -> display hexadecimal
  1423. ;                   ah=2 -> display binary
  1424. ;                   eax bits 16-21 = number of digits to display (0-32)
  1425. ;                   eax bits 22-31 = reserved
  1426. ;
  1427. ; ebx = number or pointer
  1428. ; ecx = x shl 16 + y
  1429. ; edx = color
  1430.  
  1431.      cmp   eax,0xffff            ; length > 0 ?
  1432.      jge   cont_displ
  1433.      ret
  1434.    cont_displ:
  1435.  
  1436.      cmp   eax,60*0x10000        ; length <= 60 ?
  1437.      jbe   cont_displ2
  1438.      ret
  1439.    cont_displ2:
  1440.  
  1441.      pushad
  1442.  
  1443.      cmp   al,1                  ; ecx is a pointer ?
  1444.      jne   displnl1
  1445.      mov   edi,[0x3010]
  1446.      mov   edi,[edi+0x10]
  1447.      mov   ebx,[edi+ebx]
  1448.    displnl1:
  1449.      sub   esp,64
  1450.  
  1451.      cmp   ah,0                  ; DECIMAL
  1452.      jne   no_display_desnum
  1453.      shr   eax,16
  1454.      and   eax,0x2f
  1455.      push  eax
  1456.      ;mov   edi,[0x3010]
  1457.      ;mov   edi,[edi+0x10]
  1458.      mov   edi,esp
  1459.      add   edi,4+64
  1460.      mov   ecx,eax
  1461.      mov   eax,ebx
  1462.      mov   ebx,10
  1463.    d_desnum:
  1464.      xor   edx,edx
  1465.      div   ebx
  1466.      add   dl,48
  1467.      mov   [edi],dl
  1468.      dec   edi
  1469.      loop  d_desnum
  1470.      pop   eax
  1471.      call  draw_num_text
  1472.      add   esp,64
  1473.      popad
  1474.      ret
  1475.    no_display_desnum:
  1476.  
  1477.      cmp   ah,0x01               ; HEXADECIMAL
  1478.      jne   no_display_hexnum
  1479.      shr   eax,16
  1480.      and   eax,0x2f
  1481.      push  eax
  1482.      ;mov   edi,[0x3010]
  1483.      ;mov   edi,[edi+0x10]
  1484.      mov   edi,esp
  1485.      add   edi,4+64
  1486.      mov   ecx,eax
  1487.      mov   eax,ebx
  1488.      mov   ebx,16
  1489.    d_hexnum:
  1490.      xor   edx,edx
  1491.      div   ebx
  1492.      add   edx,hexletters
  1493.      mov   dl,[edx]
  1494.      mov   [edi],dl
  1495.      dec   edi
  1496.      loop  d_hexnum
  1497.      pop   eax
  1498.      call  draw_num_text
  1499.      add   esp,64
  1500.      popad
  1501.      ret
  1502.    no_display_hexnum:
  1503.  
  1504.      cmp   ah,0x02               ; BINARY
  1505.      jne   no_display_binnum
  1506.      shr   eax,16
  1507.      and   eax,0x2f
  1508.      push  eax
  1509.      ;mov   edi,[0x3010]
  1510.      ;mov   edi,[edi+0x10]
  1511.      mov   edi,esp
  1512.      add   edi,4+64
  1513.      mov   ecx,eax
  1514.      mov   eax,ebx
  1515.      mov   ebx,2
  1516.    d_binnum:
  1517.      xor   edx,edx
  1518.      div   ebx
  1519.      add   dl,48
  1520.      mov   [edi],dl
  1521.      dec   edi
  1522.      loop  d_binnum
  1523.      pop   eax
  1524.      call  draw_num_text
  1525.      add   esp,64
  1526.      popad
  1527.      ret
  1528.    no_display_binnum:
  1529.  
  1530.      add   esp,64
  1531.      popad
  1532.      ret
  1533.  
  1534.  
  1535. draw_num_text:
  1536.  
  1537.      ; dtext
  1538.      ;
  1539.      ; eax x & y
  1540.      ; ebx color
  1541.      ; ecx start of text
  1542.      ; edx length
  1543.      ; edi 1 force
  1544.  
  1545.      mov   edx,eax
  1546.      mov   ecx,65
  1547.      sub   ecx,eax
  1548.      add   ecx,esp
  1549.      add   ecx,4
  1550.      mov   eax,[esp+64+32-8+4]
  1551.      mov   ebx,[esp+64+32-12+4]
  1552.      push  edx                       ; add window start x & y
  1553.      push  ebx
  1554.      mov   edx,[0x3010]
  1555.      mov   ebx,[edx-twdw]
  1556.      shl   ebx,16
  1557.      add   ebx,[edx-twdw+4]
  1558.      add   eax,ebx
  1559.      pop   ebx
  1560.      pop   edx
  1561.      mov   edi,0
  1562.      call  dtext
  1563.  
  1564.      ret
  1565.  
  1566.  
  1567. read_string:
  1568.  
  1569.     ; eax  read_area
  1570.     ; ebx  color of letter
  1571.     ; ecx  color of background
  1572.     ; edx  number of letters to read
  1573.     ; esi  [x start]*65536 + [y_start]
  1574.  
  1575.     ret
  1576.  
  1577.  
  1578. align 4
  1579.  
  1580. sys_setup:
  1581.  
  1582. ; 1=roland mpu midi base , base io address
  1583. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1584. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1585. ; 4=sb16 base , base io address
  1586. ; 5=system language, 1eng 2fi 3ger 4rus
  1587. ; 6=wss base , base io address
  1588. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1589. ; 8=fat32 partition in hd
  1590. ; 9
  1591. ; 10 = sound dma channel
  1592. ; 11 = enable lba read
  1593. ; 12 = enable pci access
  1594.  
  1595.  
  1596.      mov  [esp+36],dword 0
  1597.      cmp  eax,1                      ; MIDI
  1598.      jnz  nsyse1
  1599.      cmp  ebx,0x100
  1600.      jb   nsyse1
  1601.      mov  edx,65535
  1602.      cmp  edx,ebx
  1603.      jb   nsyse1
  1604.      mov  [midi_base],bx
  1605.      mov  word [mididp],bx
  1606.      inc  bx
  1607.      mov  word [midisp],bx
  1608.      ret
  1609.  
  1610. midi_base dw 0
  1611.  
  1612.    nsyse1:
  1613.  
  1614.      cmp  eax,2                      ; KEYBOARD
  1615.      jnz  nsyse2
  1616.      cmp  ebx,1
  1617.      jnz  kbnobase
  1618.      mov  edi,[0x3010]
  1619.      add  ecx,[edi+0x10]
  1620.      mov  eax,ecx
  1621.      mov  ebx,keymap
  1622.      mov  ecx,128
  1623.      call memmove
  1624.      ret
  1625.    kbnobase:
  1626.      cmp  ebx,2
  1627.      jnz  kbnoshift
  1628.      mov  edi,[0x3010]
  1629.      add  ecx,[edi+0x10]
  1630.      mov  eax,ecx
  1631.      mov  ebx,keymap_shift
  1632.      mov  ecx,128
  1633.      call memmove
  1634.      ret
  1635.    kbnoshift:
  1636.      cmp  ebx,3
  1637.      jne  kbnoalt
  1638.      mov  edi,[0x3010]
  1639.      add  ecx,[edi+0x10]
  1640.      mov  eax,ecx
  1641.      mov  ebx,keymap_alt
  1642.      mov  ecx,128
  1643.      call memmove
  1644.      ret
  1645.    kbnoalt:
  1646.      cmp  ebx,9
  1647.      jnz  kbnocountry
  1648.      mov  word [keyboard],cx
  1649.      ret
  1650.    kbnocountry:
  1651.      mov  [esp+36],dword 1
  1652.      ret
  1653.    nsyse2:
  1654.      cmp  eax,3                      ; CD
  1655.      jnz  nsyse3
  1656.      mov  [cd_base],bl
  1657.      cmp  ebx,1
  1658.      jnz  noprma
  1659.      mov  [cdbase],0x1f0
  1660.      mov  [cdid],0xa0
  1661.    noprma:
  1662.      cmp  ebx,2
  1663.      jnz  noprsl
  1664.      mov  [cdbase],0x1f0
  1665.      mov  [cdid],0xb0
  1666.    noprsl:
  1667.      cmp  ebx,3
  1668.      jnz  nosema
  1669.      mov  [cdbase],0x170
  1670.      mov  [cdid],0xa0
  1671.    nosema:
  1672.      cmp  ebx,4
  1673.      jnz  nosesl
  1674.      mov  [cdbase],0x170
  1675.      mov  [cdid],0xb0
  1676.    nosesl:
  1677.      ret
  1678.  
  1679. cd_base db 0
  1680.  
  1681.    nsyse3:
  1682.  
  1683.      cmp  eax,4                      ; SB
  1684.      jnz  nsyse4
  1685.      cmp  ebx,0x100
  1686.      jb   nsyse4
  1687.      mov  edx,65535
  1688.      cmp  edx,ebx
  1689.      jb   nsyse4
  1690.      mov  word [sb16],bx
  1691.      ret
  1692.    nsyse4:
  1693.  
  1694.      cmp  eax,5                      ; SYSTEM LANGUAGE
  1695.      jnz  nsyse5
  1696.      mov  [syslang],ebx
  1697.      ret
  1698.    nsyse5:
  1699.  
  1700.      cmp  eax,6                      ; WSS
  1701.      jnz  nsyse6
  1702.      cmp  ebx,0x100
  1703.      jb   nsyse6
  1704.      mov  [wss],ebx
  1705.      ret
  1706.  
  1707. wss_temp dd 0
  1708.  
  1709.    nsyse6:
  1710.  
  1711.      cmp  eax,7                      ; HD BASE
  1712.      jne  nsyse7
  1713.      mov  [hd_base],bl
  1714.      cmp  ebx,1
  1715.      jnz  noprmahd
  1716.      mov  [hdbase],0x1f0
  1717.      mov  [hdid],0x0
  1718.      mov  [hdpos],1
  1719. ;     call set_FAT32_variables
  1720.    noprmahd:
  1721.      cmp  ebx,2
  1722.      jnz  noprslhd
  1723.      mov  [hdbase],0x1f0
  1724.      mov  [hdid],0x10
  1725.      mov  [hdpos],2
  1726. ;     call set_FAT32_variables
  1727.    noprslhd:
  1728.      cmp  ebx,3
  1729.      jnz  nosemahd
  1730.      mov  [hdbase],0x170
  1731.      mov  [hdid],0x0
  1732.      mov  [hdpos],3
  1733. ;     call set_FAT32_variables
  1734.    nosemahd:
  1735.      cmp  ebx,4
  1736.      jnz  noseslhd
  1737.      mov  [hdbase],0x170
  1738.      mov  [hdid],0x10
  1739.      mov  [hdpos],4
  1740. ;     call set_FAT32_variables
  1741.    noseslhd:
  1742.      mov   [0xfe10],dword 0
  1743.     call  reserve_hd1
  1744.     call  clear_hd_cache
  1745.     mov   [hd1_status],0        ; free
  1746.      ret
  1747.  
  1748. hd_base db 0
  1749.  
  1750.    nsyse7:
  1751.  
  1752.      cmp  eax,8                      ; HD PARTITION
  1753.      jne  nsyse8
  1754.      mov  [fat32part],ebx
  1755. ;     call set_FAT32_variables
  1756.     call  reserve_hd1
  1757.     call  clear_hd_cache
  1758.      pusha
  1759.      call  choice_necessity_partition_1
  1760.      popa
  1761.     mov   [hd1_status],0        ; free
  1762.      ret
  1763.    nsyse8:
  1764.  
  1765.      cmp  eax,10                     ; SOUND DMA CHANNEL
  1766.      jne  no_set_sound_dma
  1767.      mov  [sound_dma],ebx
  1768.      ret
  1769.    no_set_sound_dma:
  1770.  
  1771.      cmp  eax,11                     ; ENABLE LBA READ
  1772.      jne  no_set_lba_read
  1773.      and  ebx,1
  1774.      mov  [lba_read_enabled],ebx
  1775.      ret
  1776.    no_set_lba_read:
  1777.  
  1778.      cmp  eax,12                     ; ENABLE PCI ACCESS
  1779.      jne  no_set_pci_access
  1780.      and  ebx,1
  1781.      mov  [pci_access_enabled],ebx
  1782.      ret
  1783.    no_set_pci_access:
  1784.  
  1785. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1786. include 'vmodeint.inc'
  1787. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1788.  
  1789.      mov  [esp+36],dword -1
  1790.      ret
  1791.  
  1792.  
  1793. align 4
  1794.  
  1795. sys_getsetup:
  1796.  
  1797. ; 1=roland mpu midi base , base io address
  1798. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1799. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1800. ; 4=sb16 base , base io address
  1801. ; 5=system language, 1eng 2fi 3ger 4rus
  1802. ; 6=wss base
  1803. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1804. ; 8=fat32 partition in hd
  1805. ; 9=get hs timer tic
  1806.  
  1807.      cmp  eax,1
  1808.      jne  ngsyse1
  1809.      movzx eax,[midi_base]
  1810.      mov  [esp+36],eax
  1811.      ret
  1812.    ngsyse1:
  1813.  
  1814.      cmp  eax,2
  1815.      jne  ngsyse2
  1816.      cmp  ebx,1
  1817.      jnz  kbnobaseret
  1818.      mov  edi,[0x3010]
  1819.      add  ecx,[edi+0x10]
  1820.      mov  ebx,ecx
  1821.      mov  eax,keymap
  1822.      mov  ecx,128
  1823.      call memmove
  1824.      ret
  1825.    kbnobaseret:
  1826.      cmp  ebx,2
  1827.      jnz  kbnoshiftret
  1828.      mov  edi,[0x3010]
  1829.      add  ecx,[edi+0x10]
  1830.      mov  ebx,ecx
  1831.      mov  eax,keymap_shift
  1832.      mov  ecx,128
  1833.      call memmove
  1834.      ret
  1835.    kbnoshiftret:
  1836.      cmp  ebx,3
  1837.      jne  kbnoaltret
  1838.      mov  edi,[0x3010]
  1839.      add  ecx,[edi+0x10]
  1840.      mov  ebx,ecx
  1841.      mov  eax,keymap_alt
  1842.      mov  ecx,128
  1843.      call memmove
  1844.      ret
  1845.    kbnoaltret:
  1846.      cmp  ebx,9
  1847.      jnz  ngsyse2
  1848.      movzx eax,word [keyboard]
  1849.      mov  [esp+36],eax
  1850.      ret
  1851.    ngsyse2:
  1852.  
  1853.      cmp  eax,3
  1854.      jnz  ngsyse3
  1855.      movzx eax,[cd_base]
  1856.      mov  [esp+36],eax
  1857.      ret
  1858.    ngsyse3:
  1859.  
  1860.      cmp  eax,4
  1861.      jne  ngsyse4
  1862.      mov  eax,[sb16]
  1863.      mov  [esp+36],eax
  1864.      ret
  1865.    ngsyse4:
  1866.  
  1867.      cmp  eax,5
  1868.      jnz  ngsyse5
  1869.      mov  eax,[syslang]
  1870.      mov  [esp+36],eax
  1871.      ret
  1872.    ngsyse5:
  1873.      cmp  eax,6
  1874.      jnz  ngsyse6
  1875.      mov  eax,[wss]
  1876.      mov  [esp+36],eax
  1877.      ret
  1878.    ngsyse6:
  1879.      cmp  eax,7
  1880.      jnz  ngsyse7
  1881.      movzx eax,[hd_base]
  1882.      mov  [esp+36],eax
  1883.      ret
  1884.    ngsyse7:
  1885.      cmp  eax,8
  1886.      jnz  ngsyse8
  1887.      mov eax,[fat32part]
  1888.      mov  [esp+36],eax
  1889.      ret
  1890.    ngsyse8:
  1891.      cmp  eax,9
  1892.      jne  ngsyse9
  1893.      mov  eax,[timer_ticks] ;[0xfdf0]
  1894.      mov  [esp+36],eax
  1895.      ret
  1896.    ngsyse9:
  1897.      cmp  eax,10
  1898.      jnz  ngsyse10
  1899.      mov eax,[sound_dma]
  1900.      mov  [esp+36],eax
  1901.      ret
  1902.    ngsyse10:
  1903.      cmp  eax,11
  1904.      jnz  ngsyse11
  1905.      mov eax,[lba_read_enabled]
  1906.      mov  [esp+36],eax
  1907.      ret
  1908.    ngsyse11:
  1909.      cmp  eax,12
  1910.      jnz  ngsyse12
  1911.      mov eax,[pci_access_enabled]
  1912.      mov  [esp+36],eax
  1913.      ret
  1914.    ngsyse12:
  1915.      mov  [esp+36],dword 1
  1916.      ret
  1917.  
  1918.  
  1919. align 4
  1920.  
  1921. readmousepos:
  1922.  
  1923. ; eax=0 screen relative
  1924. ; eax=1 window relative
  1925. ; eax=2 buttons pressed
  1926.  
  1927.     test eax,eax
  1928.     jnz  nosr
  1929.     mov  eax,[0xfb0a]
  1930.     shl  eax,16
  1931.     mov  ax,[0xfb0c]
  1932.     mov  [esp+36],eax
  1933.     ret
  1934.   nosr:
  1935.  
  1936.     cmp  eax,1
  1937.     jnz  nowr
  1938.     mov  eax,[0xfb0a]
  1939.     shl  eax,16
  1940.     mov  ax,[0xfb0c]
  1941.     mov  esi,[0x3010]
  1942.     sub  esi,twdw
  1943.     mov  bx,[esi]
  1944.     shl  ebx,16
  1945.     mov  bx,[esi+4]
  1946.     sub  eax,ebx
  1947.     mov  [esp+36],eax
  1948.     ret
  1949.   nowr:
  1950.  
  1951.     cmp   eax,2
  1952.     jnz   nomb
  1953.     movzx eax,byte [0xfb40]
  1954.   nomb:
  1955.     mov   [esp+36],eax
  1956.  
  1957.     ret
  1958.  
  1959. is_input:
  1960.  
  1961.    push edx
  1962.    mov  dx,word [midisp]
  1963.    in   al,dx
  1964.    and  al,0x80
  1965.    pop  edx
  1966.    ret
  1967.  
  1968.  
  1969. is_output:
  1970.  
  1971.    push edx
  1972.    mov  dx,word [midisp]
  1973.    in   al,dx
  1974.    and  al,0x40
  1975.    pop  edx
  1976.    ret
  1977.  
  1978.  
  1979. get_mpu_in:
  1980.  
  1981.    push edx
  1982.    mov  dx,word [mididp]
  1983.    in   al,dx
  1984.    pop  edx
  1985.    ret
  1986.  
  1987.  
  1988. put_mpu_out:
  1989.  
  1990.    push edx
  1991.    mov  dx,word [mididp]
  1992.    out  dx,al
  1993.    pop  edx
  1994.    ret
  1995.  
  1996.  
  1997. setuart:
  1998.  
  1999.  su1:
  2000.    call is_output
  2001.    cmp  al,0
  2002.    jnz  su1
  2003.    mov  dx,word [midisp]
  2004.    mov  al,0xff
  2005.    out  dx,al
  2006.  su2:
  2007.    mov  dx,word [midisp]
  2008.    mov  al,0xff
  2009.    out  dx,al
  2010.    call is_input
  2011.    cmp  al,0
  2012.    jnz  su2
  2013.    call get_mpu_in
  2014.    cmp  al,0xfe
  2015.    jnz  su2
  2016.  su3:
  2017.    call is_output
  2018.    cmp  al,0
  2019.    jnz  su3
  2020.    mov  dx,word [midisp]
  2021.    mov  al,0x3f
  2022.    out  dx,al
  2023.  
  2024.    ret
  2025.  
  2026.  
  2027. align 4
  2028.  
  2029. sys_midi:
  2030.  
  2031.      cmp  [mididp],0
  2032.      jnz  sm0
  2033.      mov  [esp+36],dword 1
  2034.      ret
  2035.    sm0:
  2036.  
  2037.      cmp  eax,1
  2038.      mov  [esp+36],dword 0
  2039.      jnz  smn1
  2040.      call setuart
  2041.      ret
  2042.    smn1:
  2043.  
  2044.      cmp  eax,2
  2045.      jnz  smn2
  2046.    sm10:
  2047.      call get_mpu_in
  2048.      call is_output
  2049.      test al,al
  2050.      jnz  sm10
  2051.      mov  al,bl
  2052.      call put_mpu_out
  2053.      ret
  2054.    smn2:
  2055.  
  2056.      ret
  2057.  
  2058.  
  2059. detect_devices:
  2060. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2061. include 'detect/commouse.inc'
  2062. ;include 'detect/dev_fd.inc'
  2063. ;include 'detect/dev_hdcd.inc'
  2064. ;include 'detect/sear_par.inc'
  2065. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2066.     ret
  2067.  
  2068.  
  2069. sys_end:
  2070.  
  2071.      mov   eax,[0x3010]
  2072.      add   eax,0xa
  2073.      mov   [eax],byte 3  ; terminate this program
  2074.      
  2075.     waitterm:            ; wait here for termination
  2076.      mov   eax,5
  2077.      call  delay_hs
  2078.      jmp   waitterm
  2079.  
  2080. sys_system:
  2081.  
  2082.      cmp  eax,1                              ; BOOT
  2083.      jnz  nosystemboot
  2084.      mov  [0x2f0000+0x9030],byte 0
  2085.   for_shutdown_parameter:    
  2086.      mov  eax,[0x3004]
  2087.      add  eax,2
  2088.      mov  [shutdown_processes],eax
  2089.      mov  [0xFF00],al
  2090.      xor  eax, eax
  2091.      ret
  2092.   uglobal
  2093.    shutdown_processes: dd 0x0
  2094.   endg
  2095.    nosystemboot:
  2096.  
  2097.      cmp  eax,2                              ; TERMINATE
  2098.      jnz  noprocessterminate
  2099.      cmp  ebx,2
  2100.      jb   noprocessterminate
  2101.      mov  edx,[0x3004]
  2102.      cmp  ebx,edx
  2103.      jg   noprocessterminate
  2104.      mov  eax,[0x3004]
  2105.      shl  ebx,5
  2106.      mov  edx,[ebx+0x3000+4]
  2107.      add  ebx,0x3000+0xa
  2108.      
  2109.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2110.      mov  [ebx],byte 3       ; clear possible i40's
  2111.      ;call MEM_Heap_UnLock
  2112.  
  2113.      cmp  edx,[application_table_status]    ; clear app table stat
  2114.      jne  noatsc
  2115.      mov  [application_table_status],0
  2116.    noatsc:
  2117.      ret
  2118.    noprocessterminate:
  2119.  
  2120.      cmp  eax,3                              ; ACTIVATE WINDOW
  2121.      jnz  nowindowactivate
  2122.      cmp  ebx,2
  2123.      jb   nowindowactivate
  2124.      cmp  ebx,[0x3004]
  2125.      ja   nowindowactivate
  2126.      ; edi = position at window_data+
  2127.      mov  edi, ebx          ; edi = process number
  2128.      ;shl  ebx, 1
  2129.      ;add  ebx, 0xc000
  2130.      ;mov  esi, [ebx]        ; esi = window stack value
  2131.      ;and  esi, 0xffff       ;       word
  2132.     movzx esi, word [0xC000 + ebx*2]
  2133.      mov  edx, [0x3004] ; edx = number of processes
  2134.      cmp  esi, edx
  2135.      jz   nowindowactivate ; continue if window_stack_value != number_of_processes
  2136.                            ;     i.e. if window is not already active
  2137.  
  2138. ;* start code - get active process (1) - Mario79
  2139.      mov  [window_minimize],2
  2140.      mov  [active_process],edi
  2141. ;* end code - get active process (1) - Mario79
  2142.  
  2143.      mov  [0xff01],edi     ; activate
  2144.      xor  eax, eax
  2145.      ret
  2146.      
  2147.    nowindowactivate:
  2148.  
  2149.      cmp  eax,4                              ; GET IDLETIME
  2150.      jnz  nogetidletime
  2151.      mov  eax,[idleusesec]
  2152.      ret
  2153.    nogetidletime:
  2154.  
  2155.      cmp  eax,5                              ; GET TSC/SEC
  2156.      jnz  nogettscsec
  2157.      mov  eax,[0xf600]
  2158.      ret
  2159.    nogettscsec:
  2160.  
  2161. ;  SAVE ramdisk to /hd/1/menuet.img
  2162. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2163.    include 'blkdev/rdsave.inc'
  2164. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2165. ;* start code - get active process (2) - Mario79
  2166.      cmp  eax,7
  2167.      jnz  nogetactiveprocess
  2168.      mov  eax,[active_process]
  2169.      ret
  2170.  nogetactiveprocess:  
  2171.      cmp  eax,8
  2172.      jnz  nosoundflag
  2173.      cmp  ebx,1
  2174.      jne  nogetsoundflag
  2175.      movzx  eax,byte [sound_flag] ; get sound_flag
  2176.      ret
  2177.  nogetsoundflag:
  2178.      cmp  ebx,2
  2179.      jnz  nosoundflag
  2180.      inc  byte [sound_flag]       ; set sound_flag
  2181.      and byte [sound_flag],1      ;
  2182.      ret    
  2183. nosoundflag:
  2184.      cmp  eax,9                   ; system shutdown with param
  2185.      jnz  noshutdownsystem
  2186.      cmp  ebx,1
  2187.      jl  exit_for_anyone
  2188.      cmp  ebx,4
  2189.      jg   exit_for_anyone
  2190.      mov  [0x2f0000+0x9030],bl
  2191.      jmp  for_shutdown_parameter
  2192. noshutdownsystem:
  2193.      cmp  eax,10                   ; minimize window
  2194.      jnz  nominimizewindow
  2195.      mov   [window_minimize],1
  2196.  exit_for_anyone:
  2197.      ret
  2198. nominimizewindow:
  2199.      cmp  eax,11           ; get disk info table
  2200.      jnz  nogetdiskinfo
  2201.      cmp  ebx,1
  2202.      jnz  full_table
  2203.   small_table:
  2204.      call for_all_tables
  2205.      mov cx,10
  2206.      cld
  2207.      rep movsb
  2208.      ret
  2209.    for_all_tables:
  2210.      mov edi,[3010h]
  2211.      mov edi,[edi+10h]
  2212.      add edi,ecx
  2213.      mov esi,0x40000
  2214.      xor ecx,ecx
  2215.      ret
  2216.   full_table:
  2217.      cmp  ebx,2
  2218.      jnz  exit_for_anyone
  2219.      call for_all_tables
  2220.      mov cx,16384
  2221.      cld
  2222.      rep movsd
  2223.      ret
  2224. nogetdiskinfo:
  2225.      cmp  eax,12      ; get all key pressed with ALT
  2226.      jnz  nogetkey
  2227.      mov   eax,[last_key_press]
  2228.      mov   al,[keyboard_mode_sys]
  2229.      mov   [esp+36],eax
  2230.      mov   [last_key_press],0
  2231.  .finish:
  2232.      ret
  2233. nogetkey:
  2234.      cmp  eax,13      ; get kernel ID and version
  2235.      jnz  nogetkernel_id
  2236.      mov edi,[3010h]
  2237.      mov edi,[edi+10h]
  2238.      add edi,ebx
  2239.      mov esi,version_inf
  2240.      mov ecx,version_end-version_inf
  2241.      cld
  2242.      rep movsb
  2243.      ret
  2244. nogetkernel_id:
  2245.      cmp  eax,14      ; sys wait retrace
  2246.      jnz  nosys_wait_retrace
  2247.      ;wait retrace functions
  2248.  sys_wait_retrace:
  2249.      mov edx,0x3da
  2250.  WaitRetrace_loop:
  2251.      in al,dx
  2252.      test al,1000b
  2253.      jz WaitRetrace_loop
  2254.      mov [esp+36],dword 0
  2255.      ret
  2256. nosys_wait_retrace:
  2257.      cmp  eax,15      ; mouse centered
  2258.      jnz  no_mouse_centered
  2259.      call  mouse_centered
  2260.      mov [esp+36],dword 0
  2261.      ret
  2262. no_mouse_centered:
  2263. ;* end  code - get active process (2) - Mario79
  2264.      ret
  2265. window_minimize db 0
  2266. sound_flag      db 0
  2267. last_key_press  dd 0
  2268. keyboard_mode_sys db 0
  2269.  
  2270. iglobal
  2271. version_inf:
  2272.   db 0,5,1,0  ; version 0.5.1.0
  2273.   db UID_KOLIBRI
  2274.   db 'Kolibri',0
  2275. version_end:
  2276. endg
  2277.  
  2278. UID_NONE=0
  2279. UID_MENUETOS=1   ;official
  2280. UID_KOLIBRI=2    ;russian
  2281.  
  2282. main_loop_sys_getkey:
  2283.     cmp   [0xf400],byte 0
  2284.     je    .finish
  2285.     movzx eax,byte [0xf401]
  2286.     shl   eax,8
  2287.     mov   [last_key_press],eax
  2288.  .finish:
  2289.     ret
  2290.  
  2291. sys_cachetodiskette:
  2292.     pushad
  2293.     cmp  eax,1
  2294.     jne  no_write_all_of_ramdisk
  2295.  
  2296.     call fdc_writeramdisk
  2297.     popad
  2298.     ret
  2299.   no_write_all_of_ramdisk:
  2300.     cmp eax,2
  2301.     jne no_write_part_of_ramdisk
  2302.     call fdc_commitflush
  2303.     popad
  2304.     ret
  2305.   no_write_part_of_ramdisk:
  2306.     cmp  eax,3
  2307.     jne  no_set_fdc
  2308.     call fdc_set
  2309.     popad
  2310.     ret
  2311.   no_set_fdc:
  2312.     cmp  eax,4
  2313.     jne  no_get_fdc
  2314.     popad
  2315.     call fdc_get
  2316.     mov    [esp+36],ecx
  2317.     ret
  2318.   no_get_fdc:
  2319.     popad
  2320.     ret
  2321.  
  2322. uglobal
  2323. ;  bgrchanged  dd  0x0
  2324. endg
  2325.  
  2326. sys_background:
  2327.  
  2328.     cmp   eax,1                            ; BACKGROUND SIZE
  2329.     jnz   nosb1
  2330.     cmp   ebx,0
  2331.     je    sbgrr
  2332.     cmp   ecx,0
  2333.     je    sbgrr
  2334.     mov   [display_data-8],ebx
  2335.     mov   [display_data-4],ecx
  2336. ;    mov   [bgrchanged],1
  2337.   sbgrr:
  2338.     ret
  2339.   nosb1:
  2340.  
  2341.     cmp   eax,2                            ; SET PIXEL
  2342.     jnz   nosb2
  2343.     mov   edx,0x160000-16
  2344.     cmp   edx,ebx
  2345.     jbe   nosb2
  2346.     mov   edx,[ebx]
  2347.     and   edx,0xFF000000 ;255*256*256*256
  2348.     and   ecx,0x00FFFFFF ;255*256*256+255*256+255
  2349.     add   edx,ecx
  2350.     mov   [ebx+0x300000],edx
  2351. ;    mov   [bgrchanged],1
  2352.     ret
  2353.   nosb2:
  2354.  
  2355.     cmp   eax,3                            ; DRAW BACKGROUND
  2356.     jnz   nosb3
  2357. draw_background_temp:
  2358. ;    cmp   [bgrchanged],1 ;0
  2359. ;    je    nosb31
  2360. ;draw_background_temp:
  2361. ;    mov   [bgrchanged],1 ;0
  2362.     mov   [0xfff0],byte 1
  2363.    nosb31:
  2364.     ret
  2365.   nosb3:
  2366.  
  2367.     cmp   eax,4                            ; TILED / STRETCHED
  2368.     jnz   nosb4
  2369.     cmp   ebx,[display_data-12]
  2370.     je    nosb41
  2371.     mov   [display_data-12],ebx
  2372. ;    mov   [bgrchanged],1
  2373.    nosb41:
  2374.     ret
  2375.   nosb4:
  2376.  
  2377.     cmp   eax,5                            ; BLOCK MOVE TO BGR
  2378.     jnz   nosb5
  2379.   ; bughere
  2380.     mov   edi, [0x3010]
  2381.     add   ebx, [edi+0x10]
  2382.  ;   mov   esi, ebx
  2383.  ;   mov   edi, ecx
  2384.     mov   eax, ebx
  2385.     mov   ebx, ecx
  2386.     add   ecx, edx
  2387.     cmp   ecx, 0x160000-16
  2388.     ja    .fin
  2389.  ;   add   edi, 0x300000
  2390.     add   ebx, 0x300000
  2391.     mov   ecx, edx
  2392.     cmp   ecx, 0x160000-16
  2393.     ja    .fin
  2394. ;    mov   [bgrchanged],1
  2395.   ;  cld
  2396.   ;  rep   movsb
  2397.     call  memmove
  2398.   .fin:
  2399.     ret
  2400.   nosb5:
  2401.  
  2402.     ret
  2403.  
  2404.  
  2405. align 4
  2406.  
  2407. sys_getbackground:
  2408.  
  2409.     cmp   eax,1                                  ; SIZE
  2410.     jnz   nogb1
  2411.     mov   eax,[display_data-8]
  2412.     shl   eax,16
  2413.     mov   ax,[display_data-4]
  2414.     mov   [esp+36],eax
  2415.     ret
  2416.   nogb1:
  2417.  
  2418.     cmp   eax,2                                  ; PIXEL
  2419.     jnz   nogb2
  2420.     mov   edx,0x160000-16
  2421.     cmp   edx,ebx
  2422.     jbe   nogb2
  2423.     mov   eax, [ebx+0x300000]
  2424.     and   eax, 0xFFFFFF
  2425.     mov   [esp+36],eax
  2426.     ret
  2427.   nogb2:
  2428.  
  2429.     cmp   eax,4                                  ; TILED / STRETCHED
  2430.     jnz   nogb4
  2431.     mov   eax,[display_data-12]
  2432.   nogb4:
  2433.     mov   [esp+36],eax
  2434.     ret
  2435.  
  2436.  
  2437. align 4
  2438.  
  2439. sys_getkey:
  2440.     mov   [esp+36],dword 1
  2441.     mov   ebx, [0x3000]                          ; TOP OF WINDOW STACK
  2442.     movzx ecx,word [0xC000 + ebx * 2]
  2443.     mov   edx,[0x3004]
  2444.     cmp   ecx,edx
  2445.     jne   .finish
  2446.     cmp   [0xf400],byte 0
  2447.     je    .finish
  2448.     movzx eax,byte [0xf401]
  2449.     shl   eax,8
  2450.     push  eax
  2451.     dec   byte [0xf400]
  2452.     and   byte [0xf400],127
  2453.     movzx ecx,byte [0xf400]
  2454.     add   ecx,2
  2455.  ;   mov   esi,0xf402
  2456.  ;   mov   edi,0xf401
  2457.  ;   cld
  2458.  ;  rep   movsb
  2459.     mov   eax, 0xF402
  2460.     mov   ebx, 0xF401
  2461.     call  memmove
  2462.     pop   eax
  2463.     mov   [last_key_press],eax
  2464.  
  2465.     mov   eax,[kb_state]
  2466.     and   al,110000b
  2467.     cmp   al,100000b
  2468.     je    .yes_win_key
  2469.     cmp   al,10000b
  2470.     je    .yes_win_key
  2471.     mov   eax,[last_key_press]
  2472.     jmp   .no_win_key
  2473. ;    cmp   ah,232
  2474. ;    je    .yes_win_key
  2475. ;    cmp   ah,233
  2476. ;    jne   .no_win_key
  2477.  .yes_win_key:
  2478.     mov   eax,1
  2479.  .no_win_key:
  2480.     mov   [esp+36],eax
  2481.  .finish:
  2482.     ret
  2483.  
  2484.  
  2485. align 4
  2486.  
  2487. sys_getbutton:
  2488.  
  2489.     mov   ebx, [0x3000]                         ; TOP OF WINDOW STACK
  2490.     mov   [esp+36],dword 1
  2491.     movzx ecx, word [0xC000 + ebx * 2]
  2492.     mov   edx, [0x3004] ; less than 256 processes
  2493.     cmp   ecx,edx
  2494.     jne   .exit
  2495.     movzx eax,byte [0xf500]
  2496.     test  eax,eax
  2497.     jz    .exit
  2498.     mov   eax,[0xf501]
  2499.     shl   eax,8
  2500.     mov   [0xf500],byte 0
  2501.     mov   [esp+36],eax
  2502.  .exit:
  2503.     ret
  2504.  
  2505.  
  2506. align 4
  2507.  
  2508. sys_cpuusage:
  2509.  
  2510. ;  RETURN:
  2511. ;
  2512. ;  +00 dword     process cpu usage
  2513. ;  +04  word     position in windowing stack
  2514. ;  +06  word     windowing stack value at current position (cpu nro)
  2515. ;  +10 12 bytes  name
  2516. ;  +22 dword     start in mem
  2517. ;  +26 dword     used mem
  2518. ;  +30 dword     PID , process idenfification number
  2519. ;
  2520.  
  2521.     mov  edi,[0x3010]   ; eax = return area
  2522.     add  edi,0x10
  2523.     add  eax,[edi]
  2524.  
  2525.     cmp  ebx,-1         ; who am I ?
  2526.     jne  no_who_am_i
  2527.     mov  ebx,[0x3000]
  2528.   no_who_am_i:
  2529.  
  2530.     push eax            ; return area
  2531.     push ebx            ; process number
  2532.  
  2533.     push ebx
  2534.     push ebx
  2535.     push eax
  2536.  
  2537.     ; return memory usage
  2538.  
  2539.     xor  edx,edx
  2540.     mov  eax,0x20
  2541.     mul  ebx
  2542.     add  eax,0x3000+0x1c
  2543.     mov  ebx,eax
  2544.     pop  eax
  2545.     mov  ecx,[ebx]
  2546.     mov  [eax],ecx
  2547.     pop  ebx
  2548. ;    mov  ebx,[esp]
  2549. ;    shl  ebx,1
  2550. ;    add  ebx,0xc000
  2551.     mov  cx, [0xC000 + ebx * 2]
  2552.     mov  [eax+4],cx
  2553. ;    mov  ebx,[esp]
  2554. ;    shl  ebx,1
  2555. ;    add  ebx,0xc400
  2556.     mov  cx, [0xC400 + ebx * 2]
  2557.     mov  [eax+6],cx
  2558. ;    pop  ebx
  2559.     push eax
  2560.     mov  eax,ebx
  2561.     shl  eax,8
  2562.     add  eax,0x80000
  2563.     pop  ebx
  2564.     add  ebx,10
  2565.     mov  ecx,11
  2566.     call memmove
  2567.  
  2568.     ; memory usage
  2569.  
  2570.     xor    eax,eax
  2571.     mov    edx,0x100000*16
  2572.     pop    ecx                                   ; get gdt of tss
  2573.     cmp    ecx,1
  2574.     je     os_mem
  2575.     shl    ecx,8
  2576.     mov    edx,[0x80000+ecx+0x8c]
  2577.     mov    eax,std_application_base_address
  2578.     ;add    ecx,0x80000+0x88
  2579.     ;mov    ecx,[ecx]
  2580. ;    shl    ecx,3
  2581.     ; eax run base -> edx used memory
  2582. ;    mov    al,[ecx+gdts+ app_code-3 +4]        ;  base  23:16
  2583. ;    mov    ah,[ecx+gdts+ app_code-3 +7]        ;  base  31:24
  2584. ;    shl    eax,16
  2585. ;    mov    ax,[ecx+gdts+ app_code-3 +2]        ;  base  0:15
  2586. ;    movzx  edx,word [ecx+gdts+ app_code-3 +0]
  2587. ;    shl    edx,12
  2588.  
  2589.   os_mem:
  2590.     dec    edx
  2591.     mov    [ebx+12],eax
  2592.     mov    [ebx+16],edx
  2593.  
  2594.     ; PID (+30)
  2595.  
  2596.     mov    eax,[esp]
  2597.     shl    eax,5
  2598.     add    eax,0x3000+0x4
  2599.     mov    eax,[eax]
  2600.     mov    [ebx+20],eax
  2601.  
  2602.     ; window position and size
  2603.  
  2604.     mov    esi,[esp]
  2605.     shl    esi,5
  2606.     add    esi,window_data
  2607.     mov    edi,[esp+4]
  2608.     add    edi,34
  2609.     mov    ecx,4*4
  2610.     cld
  2611.     rep    movsb
  2612.  
  2613.     ; Process state (+50)
  2614.  
  2615.     mov    eax,[esp]
  2616.     shl    eax,5
  2617.     add    eax,0x3000+0xa
  2618.     mov    eax,[eax]
  2619.     mov    [ebx+40],ax
  2620.  
  2621.  
  2622.     pop    ebx
  2623.     pop    eax
  2624.  
  2625.     ; return number of processes
  2626.  
  2627.     mov    eax,[0x3004]
  2628.     mov    [esp+36],eax
  2629.     ret
  2630.  
  2631.  
  2632.  
  2633.  
  2634. align 4
  2635. sys_clock:
  2636.         cli
  2637.   ; Mikhail Lisovin  xx Jan 2005
  2638.   @@:   mov   al, 10
  2639.         out   0x70, al
  2640.         in    al, 0x71
  2641.         test  al, al
  2642.         jns   @f
  2643.         mov   esi, 1
  2644.         call  delay_ms
  2645.         jmp   @b
  2646.   @@:
  2647.   ; end Lisovin's fix
  2648.  
  2649.         xor   al,al           ; seconds
  2650.         out   0x70,al
  2651.         in    al,0x71
  2652.         movzx ecx,al
  2653.         mov   al,02           ; minutes
  2654.         shl   ecx,16
  2655.         out   0x70,al
  2656.         in    al,0x71
  2657.         movzx edx,al
  2658.         mov   al,04           ; hours
  2659.         shl   edx,8
  2660.         out   0x70,al
  2661.         in    al,0x71
  2662.         add   ecx,edx
  2663.         movzx edx,al
  2664.         add   ecx,edx
  2665.         sti
  2666.         mov   [esp+36],ecx
  2667.         ret
  2668.  
  2669.  
  2670. align 4
  2671.  
  2672. sys_date:
  2673.  
  2674.         cli
  2675.         mov     al,6            ; day of week
  2676.         out     0x70,al
  2677.         in      al,0x71
  2678.         mov     ch,al
  2679.         mov     al,7            ; date
  2680.         out     0x70,al
  2681.         in      al,0x71
  2682.         mov     cl,al
  2683.         mov     al,8            ; month
  2684.         shl     ecx,16
  2685.         out     0x70,al
  2686.         in      al,0x71
  2687.         mov     ch,al
  2688.         mov     al,9            ; year
  2689.         out     0x70,al
  2690.         in      al,0x71
  2691.         mov     cl,al
  2692.         sti
  2693.         mov     [esp+36],ecx
  2694.         ret
  2695.  
  2696.  
  2697. ; redraw status
  2698.  
  2699. sys_redrawstat:
  2700.  
  2701.     cmp  eax,1
  2702.     jne  no_widgets_away
  2703.  
  2704.     ; buttons away
  2705.  
  2706.     mov   ecx,[0x3000]
  2707.  
  2708.   sys_newba2:
  2709.  
  2710.     mov   edi,[0xfe88]
  2711.     cmp   [edi],dword 0  ; empty button list ?
  2712.     je    end_of_buttons_away
  2713.  
  2714.     movzx ebx,word [edi]
  2715.     inc   ebx
  2716.  
  2717.     mov   eax,edi
  2718.  
  2719.   sys_newba:
  2720.  
  2721.     dec   ebx
  2722.     jz    end_of_buttons_away
  2723.  
  2724.     add   eax,0x10
  2725.     cmp   cx,[eax]
  2726.     jnz   sys_newba
  2727.  
  2728.     push  eax ebx ecx
  2729.     mov   ecx,ebx
  2730.     inc   ecx
  2731.     shl   ecx,4
  2732.     mov   ebx,eax
  2733.     add   eax,0x10
  2734.     call  memmove
  2735.     dec   dword [edi]
  2736.     pop   ecx ebx eax
  2737.  
  2738.     jmp   sys_newba2
  2739.  
  2740.   end_of_buttons_away:
  2741.  
  2742.     ret
  2743.  
  2744.   no_widgets_away:
  2745.  
  2746.     cmp   eax,2
  2747.     jnz   srl1
  2748.  
  2749.     mov   edx,[0x3010]      ; return whole screen draw area for this app
  2750.     add   edx,draw_data-0x3000
  2751.     mov   [edx+0],dword 0
  2752.     mov   [edx+4],dword 0
  2753.     mov   eax,[0xfe00]
  2754.     mov   [edx+8],eax
  2755.     mov   eax,[0xfe04]
  2756.     mov   [edx+12],eax
  2757.  
  2758.     mov   edi,[0x3010]
  2759.     sub   edi,twdw
  2760.     mov   [edi+30],byte 1   ; no new position & buttons from app
  2761.  
  2762.     call  sys_window_mouse
  2763.  
  2764.     ret
  2765.  
  2766.   srl1:
  2767.  
  2768.     ret
  2769.  
  2770.  
  2771. sys_drawwindow:
  2772.  
  2773.     mov   edi,ecx
  2774.     shr   edi,16+8
  2775.     and   edi,15
  2776.  
  2777.     cmp   edi,0   ; type I    - original style
  2778.     jne   nosyswI
  2779.     call  sys_set_window
  2780.     call  drawwindow_I
  2781.     ret
  2782.   nosyswI:
  2783.  
  2784.     cmp   edi,1   ; type II   - only reserve area, no draw
  2785.     jne   nosyswII
  2786.     call  sys_set_window
  2787.     call  sys_window_mouse
  2788.     ret
  2789.   nosyswII:
  2790.  
  2791.     cmp   edi,2   ; type III  - new style
  2792.     jne   nosyswIII
  2793.     call  sys_set_window
  2794.     call  drawwindow_III
  2795.     ret
  2796.   nosyswIII:
  2797.  
  2798.     cmp   edi,3   ; type IV - skinned window
  2799.     jne   nosyswIV
  2800.     call  sys_set_window
  2801.     call  drawwindow_IV
  2802.     ret
  2803.   nosyswIV:
  2804.  
  2805.     ret
  2806.  
  2807.  
  2808. sys_set_window:
  2809.  
  2810.     mov   edi,[0x3000]
  2811.     shl   edi,5
  2812.     add   edi,window_data
  2813.  
  2814.     ; colors
  2815.     mov   [edi+16],ecx
  2816.     mov   [edi+20],edx
  2817.     mov   [edi+24],esi
  2818.  
  2819.     ; check flag (?)
  2820.     cmp   [edi+30],byte 1
  2821.     jz    newd
  2822.  
  2823.     push  eax
  2824.     mov   eax,[timer_ticks] ;[0xfdf0]
  2825.     add   eax,100
  2826.     mov   [new_window_starting],eax
  2827.     pop   eax
  2828.  
  2829.     mov   [edi+8],ax
  2830.     mov   [edi+12],bx
  2831.     shr   eax,16
  2832.     shr   ebx,16
  2833.     mov   [edi+00],ax
  2834.     mov   [edi+04],bx
  2835.  
  2836.  
  2837.     call  check_window_position
  2838.  
  2839.  
  2840.     push  ecx esi edi               ; save for window fullscreen/resize
  2841.     mov   esi,edi
  2842.     sub   edi,window_data
  2843.     shr   edi,5
  2844.     shl   edi,8
  2845.     add   edi,0x80000+0x90
  2846.     mov   ecx,4
  2847.     cld
  2848.     rep   movsd
  2849.     pop   edi esi ecx
  2850.  
  2851.     push  eax ebx ecx edx
  2852. ;;;    mov   eax, 1
  2853. ;;;    call  delay_hs
  2854.     movzx eax, word [edi+00]
  2855.     movzx ebx, word [edi+04]
  2856.     movzx ecx, word [edi+8]
  2857.     movzx edx, word [edi+12]
  2858.     add   ecx, eax
  2859.     add   edx, ebx
  2860.     call  calculatescreen
  2861.     pop   edx ecx ebx eax
  2862.  
  2863.     mov   [0xf400],byte 0           ; empty keyboard buffer
  2864.     mov   [0xf500],byte 0           ; empty button buffer
  2865.  
  2866.   newd:
  2867.     mov   [edi+31],byte 0   ; no redraw
  2868.     mov   edx,edi
  2869.  
  2870.     ret
  2871.  
  2872.  
  2873. sys_window_move:
  2874.  
  2875.         cmp  [window_move_pr],0