Subversion Repositories Kolibri OS

Rev

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