Subversion Repositories Kolibri OS

Rev

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