Subversion Repositories Kolibri OS

Rev

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