Subversion Repositories Kolibri OS

Rev

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