Subversion Repositories Kolibri OS

Rev

Rev 413 | 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-OS_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.  
  206.            lgdt [gdts]
  207.            jmp pword os_code:high_code
  208.  
  209. __DEBUG__ fix 1
  210. __DEBUG_LEVEL__ fix 1
  211. include 'init.inc'
  212.  
  213. org OS_BASE+$
  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.  
  229.            mov eax, cr3
  230.            mov cr3, eax
  231.  
  232. ; SAVE REAL MODE VARIABLES
  233.         mov     ax, [BOOT_VAR + 0x9031]
  234.         mov     [IDEContrRegsBaseAddr], ax
  235. ; --------------- APM ---------------------
  236. ;    mov    eax, [BOOT_VAR + 0x9040]    ; entry point
  237. ;    mov    dword[apm_entry], eax
  238. ;    mov    word [apm_entry + 4], apm_code_32 - gdts
  239.  
  240.     mov    eax, [BOOT_VAR + 0x9044]    ; version & flags
  241.     mov    [apm_vf], eax
  242. ; -----------------------------------------
  243. ;        movzx eax,byte [BOOT_VAR+0x9010]  ; mouse port
  244. ;        mov   [0xF604],byte 1  ;al
  245.         mov     al, [BOOT_VAR+0x901F]   ; DMA writing
  246.         mov     [allow_dma_write], al
  247.         mov   al,[BOOT_VAR+0x9000]        ; bpp
  248.         mov   [ScreenBPP],al
  249.         movzx eax,word [BOOT_VAR+0x900A]  ; X max
  250.         dec   eax
  251.         mov   [ScreenWidth],eax
  252.         mov   [screen_workarea.right],eax
  253.         movzx eax,word [BOOT_VAR+0x900C]  ; Y max
  254.         dec   eax
  255.         mov   [ScreenHeight],eax
  256.         mov   [screen_workarea.bottom],eax
  257.         movzx eax,word [BOOT_VAR+0x9008]  ; screen mode
  258.         mov   [SCR_MODE],eax
  259.         mov   eax,[BOOT_VAR+0x9014]       ; Vesa 1.2 bnk sw add
  260.         mov   [BANK_SWITCH],eax
  261.         mov   [BytesPerScanLine],word 640*4         ; Bytes PerScanLine
  262.         cmp   [SCR_MODE],word 0x13          ; 320x200
  263.         je    @f
  264.         cmp   [SCR_MODE],word 0x12          ; VGA 640x480
  265.         je    @f
  266.         mov   ax,[BOOT_VAR+0x9001]        ; for other modes
  267.         mov   [BytesPerScanLine],ax
  268. @@:
  269.  
  270. ; GRAPHICS ADDRESSES
  271.  
  272.         mov     byte [BOOT_VAR+0x901e],0x0
  273.         mov     eax,[BOOT_VAR+0x9018]
  274.         mov     [LFBAddress],eax
  275.  
  276.         cmp     [SCR_MODE],word 0100000000000000b
  277.         jge     setvesa20
  278.         cmp     [SCR_MODE],word 0x13
  279.         je      v20ga32
  280.         mov     [PUTPIXEL],dword Vesa12_putpixel24  ; Vesa 1.2
  281.         mov     [GETPIXEL],dword Vesa12_getpixel24
  282.         cmp     [ScreenBPP],byte 24
  283.         jz      ga24
  284.         mov     [PUTPIXEL],dword Vesa12_putpixel32
  285.         mov     [GETPIXEL],dword Vesa12_getpixel32
  286.       ga24:
  287.         jmp     v20ga24
  288.       setvesa20:
  289.         mov     [PUTPIXEL],dword Vesa20_putpixel24  ; Vesa 2.0
  290.         mov     [GETPIXEL],dword Vesa20_getpixel24
  291.         cmp     [ScreenBPP],byte 24
  292.         jz      v20ga24
  293.       v20ga32:
  294.         mov     [PUTPIXEL],dword Vesa20_putpixel32
  295.         mov     [GETPIXEL],dword Vesa20_getpixel32
  296.       v20ga24:
  297.         cmp     [SCR_MODE],word 0x12                ; 16 C VGA 640x480
  298.         jne     no_mode_0x12
  299.         mov     [PUTPIXEL],dword VGA_putpixel
  300.         mov     [GETPIXEL],dword Vesa20_getpixel32
  301.       no_mode_0x12:
  302.  
  303. ; -------- Fast System Call init ----------
  304. ; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
  305.            bt [cpu_caps], CAPS_SEP
  306.            jnc .SEnP   ; SysEnter not Present
  307.            xor edx, edx
  308.            mov ecx, MSR_SYSENTER_CS
  309.            mov eax, os_code
  310.            wrmsr
  311.            mov ecx, MSR_SYSENTER_ESP
  312.            mov eax, sysenter_stack ; Check it
  313.            wrmsr
  314.            mov ecx, MSR_SYSENTER_EIP
  315.            mov eax, sysenter_entry
  316.            wrmsr
  317. .SEnP:
  318. ; AMD SYSCALL/SYSRET
  319.            cmp byte[cpu_vendor], 'A'
  320.            jne .noSYSCALL
  321.            mov eax, 0x80000001
  322.            cpuid
  323.            test edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
  324.            jz .noSYSCALL
  325.            mov ecx, MSR_AMD_EFER
  326.            rdmsr
  327.            or eax, 1   ; bit_0 - System Call Extension (SCE)
  328.            wrmsr
  329.  
  330.         ; !!!! It`s dirty hack, fix it !!!
  331.         ; Bits of EDX :
  332.         ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
  333.         ;  and the contents of this field, plus 8, are copied into the SS register.
  334.         ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
  335.         ;  and the contents of this field, plus 8, are copied into the SS register.
  336.  
  337.         ; mov   edx, (os_code + 16) * 65536 + os_code
  338.            mov edx, 0x1B0008
  339.  
  340.            mov eax, syscall_entry
  341.            mov ecx, MSR_AMD_STAR
  342.            wrmsr
  343. .noSYSCALL:
  344. ; -----------------------------------------
  345.  
  346.            call init_kernel_heap
  347.            stdcall kernel_alloc, 0x2000
  348.            mov [os_stack], eax
  349.  
  350.            mov [LFBSize], 0x800000
  351.            call init_mtrr
  352.  
  353.            call init_LFB
  354.            call init_fpu
  355.            call init_malloc
  356.  
  357.            stdcall alloc_kernel_space, 0x4F000
  358.            mov [ipc_tmp], eax
  359.            mov ebx, 0x1000
  360.  
  361.            add eax, 0x40000
  362.            mov [proc_mem_map], eax
  363.  
  364.            add eax, 0x8000
  365.            mov [proc_mem_pdir], eax
  366.  
  367.            add eax, ebx
  368.            mov [proc_mem_tab], eax
  369.  
  370.            add eax, ebx
  371.            mov [tmp_task_pdir], eax
  372.  
  373.            add eax, ebx
  374.            mov [tmp_task_ptab], eax
  375.  
  376.            add eax, ebx
  377.            mov [ipc_pdir], eax
  378.  
  379.            add eax, ebx
  380.            mov [ipc_ptab], eax
  381.  
  382.            call init_events
  383.            mov eax, srv.fd-SRV_FD_OFFSET
  384.            mov [srv.fd], eax
  385.            mov [srv.bk], eax
  386.  
  387.            mov edi, irq_tab
  388.            xor eax, eax
  389.            mov ecx, 16
  390.            rep stosd
  391.  
  392. ;Set base of graphic segment to linear address of LFB
  393.         mov     eax,[LFBAddress]          ; set for gs
  394.         mov     [graph_data_l+2],ax
  395.         shr     eax,16
  396.         mov     [graph_data_l+4],al
  397.         mov     [graph_data_l+7],ah
  398.  
  399. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  400. include 'detect/disks.inc'
  401. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  402.  
  403. ; READ RAMDISK IMAGE FROM HD
  404.  
  405. ;!!!!!!!!!!!!!!!!!!!!!!!
  406. include 'boot/rdload.inc'
  407. ;!!!!!!!!!!!!!!!!!!!!!!!
  408. ;    mov    [dma_hdd],1
  409. ; CALCULATE FAT CHAIN FOR RAMDISK
  410.  
  411.         call  calculatefatchain
  412.  
  413. ; LOAD VMODE DRIVER
  414.  
  415. ;!!!!!!!!!!!!!!!!!!!!!!!
  416. include 'vmodeld.inc'
  417. ;!!!!!!!!!!!!!!!!!!!!!!!
  418.  
  419. ; LOAD FONTS I and II
  420.  
  421.         mov   [CURRENT_TASK],dword 1
  422.         mov   [TASK_COUNT],dword 1
  423.         mov   [TASK_BASE],dword TASK_DATA
  424.  
  425.         mov   esi,char
  426.         xor   ebx,ebx
  427.         mov   ecx,2560
  428.         mov   edx,FONT_I
  429.         call  fs_RamdiskRead
  430.  
  431.         mov   esi,char2
  432.         xor   ebx,ebx
  433.         mov   ecx,2560;26000
  434.         mov   edx,FONT_II
  435.         call  fs_RamdiskRead
  436.  
  437.         mov   esi,boot_fonts
  438.         call  boot_log
  439.  
  440. ; PRINT AMOUNT OF MEMORY
  441.         mov     esi, boot_memdetect
  442.         call    boot_log
  443.  
  444.         movzx   ecx, word [boot_y]
  445.         or      ecx, (10+29*6) shl 16 ; "Determining amount of memory"
  446.         sub     ecx, 10
  447.         mov     edx, 0xFFFFFF
  448.         mov     ebx, [MEM_AMOUNT]
  449.         shr     ebx, 20
  450.         mov     edi, 1
  451.         mov     eax, 0x00040000
  452.         call    display_number_force
  453.  
  454. ; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
  455.  
  456.         mov   esi,boot_irqs
  457.         call  boot_log
  458.         call  rerouteirqs
  459.  
  460.         mov    esi,boot_tss
  461.         call   boot_log
  462.  
  463. ; BUILD SCHEDULER
  464.  
  465.         call   build_scheduler ; sys32.inc
  466.  
  467. ; LOAD IDT
  468.         lidt   [idtreg]
  469.  
  470.         mov    esi,boot_devices
  471.         call   boot_log
  472.         call   detect_devices
  473.  
  474. ; TIMER SET TO 1/100 S
  475.  
  476.         mov   esi,boot_timer
  477.         call  boot_log
  478.         mov   al,0x34              ; set to 100Hz
  479.         out   0x43,al
  480.         mov   al,0x9b              ; lsb    1193180 / 1193
  481.         out   0x40,al
  482.         mov   al,0x2e              ; msb
  483.         out   0x40,al
  484.  
  485. ; SET MOUSE
  486.  
  487.         mov   esi,boot_setmouse
  488.         call  boot_log
  489.         call  setmouse
  490.  
  491.         mov  [pci_access_enabled],1
  492.  
  493. ; SET PRELIMINARY WINDOW STACK AND POSITIONS
  494.  
  495.         mov   esi,boot_windefs
  496.         call  boot_log
  497.         call  setwindowdefaults
  498.  
  499. ; SET BACKGROUND DEFAULTS
  500.  
  501.         mov   esi,boot_bgr
  502.         call  boot_log
  503.         call  calculatebackground
  504.  
  505. ; RESERVE SYSTEM IRQ'S JA PORT'S
  506.  
  507.         mov   esi,boot_resirqports
  508.         call  boot_log
  509.         call  reserve_irqs_ports
  510.  
  511. ; SET PORTS FOR IRQ HANDLERS
  512.  
  513.         mov  esi,boot_setrports
  514.         call boot_log
  515.         call setirqreadports
  516.  
  517. ; SET UP OS TASK
  518.  
  519.         mov  esi,boot_setostask
  520.         call boot_log
  521.  
  522.         mov eax, fpu_data
  523.         mov  dword [SLOT_BASE+APPDATA.fpu_state], eax
  524.         mov  dword [SLOT_BASE+APPDATA.fpu_handler], 0
  525.         mov  dword [SLOT_BASE+APPDATA.sse_handler], 0
  526.  
  527.         ; name for OS/IDLE process
  528.  
  529.         mov dword [SLOT_BASE+256+APPDATA.app_name],   dword 'OS/I'
  530.         mov dword [SLOT_BASE+256+APPDATA.app_name+4], dword 'DLE '
  531.         mov edi, [os_stack]
  532.         mov dword [SLOT_BASE+256+APPDATA.pl0_stack], edi
  533.         add edi, 0x2000-512
  534.         mov dword [SLOT_BASE+256+APPDATA.fpu_state], edi
  535.  
  536.         mov esi, fpu_data
  537.         mov ecx, 512/4
  538.         cld
  539.         rep movsd
  540.  
  541.         mov dword [SLOT_BASE+256+APPDATA.fpu_handler], 0
  542.         mov dword [SLOT_BASE+256+APPDATA.sse_handler], 0
  543.         mov dword [SLOT_BASE+256+APPDATA.cursor], ebx
  544.  
  545.         mov ebx, SLOT_BASE+256+APP_OBJ_OFFSET
  546.         mov  dword [SLOT_BASE+256+APPDATA.fd_obj], ebx
  547.         mov  dword [SLOT_BASE+256+APPDATA.bk_obj], ebx
  548.  
  549.         ; task list
  550.         mov   [CURRENT_TASK],dword 1
  551.         mov   [TASK_COUNT],dword 1
  552.         mov  [TASK_DATA+TASKDATA.wnd_number], 1 ; on screen number
  553.         mov  [TASK_DATA+TASKDATA.pid], 1        ; process id number
  554.         mov  [TASK_DATA+TASKDATA.mem_start], 0  ; process base address
  555.  
  556.         mov  edi,tss_data+tss_step
  557.         mov ecx, (tss_step)/4
  558.         xor eax, eax
  559.         cld
  560.         rep stosd
  561.  
  562.         mov  edi,tss_data+tss_step
  563.         mov  [edi+TSS._ss0], os_data
  564.         mov  eax,cr3
  565.         mov  [edi+TSS._cr3],eax
  566.         mov  [edi+TSS._eip],osloop
  567.         mov  [edi+TSS._eflags],dword 0x11202 ; sti and resume
  568.         mov eax, [os_stack]
  569.         add eax, 0x2000-512
  570.         mov  [edi+TSS._esp], eax
  571.         mov  [edi+TSS._cs],os_code
  572.         mov  [edi+TSS._ss],os_data
  573.         mov  [edi+TSS._ds],os_data
  574.         mov  [edi+TSS._es],os_data
  575.         mov  [edi+TSS._fs],os_data
  576.         mov  [edi+TSS._gs],os_data
  577.  
  578.         mov  ax,tss0
  579.         ltr  ax
  580.  
  581.         call init_cursors
  582.         mov eax, [def_cursor]
  583.         mov [SLOT_BASE+APPDATA.cursor],eax
  584.         mov [SLOT_BASE+APPDATA.cursor+256],eax
  585.  
  586.   ; READ TSC / SECOND
  587.  
  588.         mov   esi,boot_tsc
  589.         call  boot_log
  590.         call  _rdtsc
  591.         mov   ecx,eax
  592.         mov   esi,250               ; wait 1/4 a second
  593.         call  delay_ms
  594.         call  _rdtsc
  595.         sub   eax,ecx
  596.         shl   eax,2
  597.         mov   [CPU_FREQ],eax          ; save tsc / sec
  598.         mov ebx, 1000000
  599.         div ebx
  600.         mov [stall_mcs], eax
  601.  
  602. ; SET VARIABLES
  603.  
  604.         call  set_variables
  605.  
  606. ; STACK AND FDC
  607.  
  608.         call  stack_init
  609.         call  fdc_init
  610.  
  611. ; PALETTE FOR 320x200 and 640x480 16 col
  612.  
  613.         cmp   [SCR_MODE],word 0x12
  614.         jne   no_pal_vga
  615.         mov   esi,boot_pal_vga
  616.         call  boot_log
  617.         call  paletteVGA
  618.       no_pal_vga:
  619.  
  620.         cmp   [SCR_MODE],word 0x13
  621.         jne   no_pal_ega
  622.         mov   esi,boot_pal_ega
  623.         call  boot_log
  624.         call  palette320x200
  625.       no_pal_ega:
  626.  
  627. ; LOAD DEFAULT SKIN
  628.  
  629.         mov     esi,_skin_file_default
  630.         mov     edi,_skin_file
  631.         movsd
  632.         movsd
  633.         movsd
  634.         call    load_skin
  635.  
  636. ; LOAD FIRST APPLICATION
  637.         cli
  638.         cmp   byte [BOOT_VAR+0x9030],1
  639.         jne   no_load_vrr_m
  640.  
  641.         mov ebp, vrr_m
  642.         xor ebx, ebx
  643.         xor edx, edx
  644.         call fs_execute
  645.         cmp   eax,2                  ; if vrr_m app found (PID=2)
  646.         je    first_app_found
  647.  
  648. no_load_vrr_m:
  649.         mov ebp, firstapp
  650.         xor ebx, ebx
  651.         xor edx, edx
  652.         call fs_execute
  653.         cmp   eax,2                  ; continue if a process has been loaded
  654.         je    first_app_found
  655.         mov   eax, 0xDEADBEEF        ; otherwise halt
  656.         hlt
  657. first_app_found:
  658.         cli
  659.  
  660.         ;mov   [TASK_COUNT],dword 2
  661.         mov   [CURRENT_TASK],dword 1       ; set OS task fisrt
  662.  
  663. ; SET KEYBOARD PARAMETERS
  664.         mov   al, 0xf6         ; reset keyboard, scan enabled
  665.         call  kb_write
  666.  
  667.         ; wait until 8042 is ready
  668.         xor ecx,ecx
  669.       @@:
  670.         in     al,64h
  671.         and    al,00000010b
  672.         loopnz @b
  673.  
  674.        ; mov   al, 0xED       ; svetodiody - only for testing!
  675.        ; call  kb_write
  676.        ; call  kb_read
  677.        ; mov   al, 111b
  678.        ; call  kb_write
  679.        ; call  kb_read
  680.  
  681.         mov   al, 0xF3       ; set repeat rate & delay
  682.         call  kb_write
  683. ;        call  kb_read
  684.         mov   al, 0 ; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  685.         call  kb_write
  686. ;        call  kb_read
  687.      ;// mike.dld [
  688.         call  set_lights
  689.      ;// mike.dld ]
  690.  
  691. ; START MULTITASKING
  692.  
  693.         mov   esi,boot_tasking
  694.         call  boot_log
  695.  
  696.  ;      mov   [ENABLE_TASKSWITCH],byte 1        ; multitasking enabled
  697.  
  698. ; UNMASK ALL IRQ'S
  699.  
  700.         mov   esi,boot_allirqs
  701.         call  boot_log
  702.  
  703.         cli                          ;guarantee forbidance of interrupts.
  704.         mov   al,0                   ; unmask all irq's
  705.         out   0xA1,al
  706.         out   0x21,al
  707.  
  708.         mov   ecx,32
  709.  
  710.      ready_for_irqs:
  711.  
  712.         mov   al,0x20                ; ready for irqs
  713.         out   0x20,al
  714.         out   0xa0,al
  715.  
  716.         loop  ready_for_irqs         ; flush the queue
  717.  
  718.         stdcall attach_int_handler, dword 1, irq1
  719.  
  720. ;        mov    [dma_hdd],1
  721.         cmp     [IDEContrRegsBaseAddr], 0
  722.         setnz   [dma_hdd]
  723.  
  724.         stdcall init_uart_service, DRV_ENTRY
  725.  
  726.         sti
  727.         jmp   $                      ; wait here for timer to take control
  728.  
  729.         ; Fly :)
  730.  
  731. include 'unpacker.inc'
  732. include 'fdo.inc'
  733.  
  734. align 4
  735. boot_log:
  736.          pushad
  737.  
  738.          mov   eax,10*65536
  739.          mov   ax,word [boot_y]
  740.          add   [boot_y],dword 10
  741.          mov   ebx,0x80ffffff   ; ASCIIZ string with white color
  742.          mov   ecx,esi
  743.          mov   edi,1
  744.          call  dtext
  745.  
  746.          mov   [novesachecksum],1000
  747.          call  checkVga_N13
  748.  
  749.          cmp   [preboot_blogesc+OS_BASE+0x10000],byte 1
  750.          je    .bll2
  751.  
  752.          cmp   esi,boot_tasking
  753.          jne   .bll2
  754.          ; begin ealex 04.08.05
  755. ;         in    al,0x61
  756. ;         and   al,01111111b
  757. ;         out   0x61,al
  758.          ; end ealex 04.08.05
  759. .bll1:   in    al,0x60    ; wait for ESC key press
  760.          cmp   al,129
  761.          jne   .bll1
  762.  
  763. .bll2:   popad
  764.  
  765.          ret
  766.  
  767.  
  768. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  769. ;                                                                    ;
  770. ;                    MAIN OS LOOP START                              ;
  771. ;                                                                    ;
  772. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  773. align 32
  774. osloop:
  775.         call   [draw_pointer]
  776.         call   checkbuttons
  777.         call   checkwindows
  778. ;       call   check_window_move_request
  779.         call   checkmisc
  780.         call   checkVga_N13
  781.         call   stack_handler
  782.         call   checkidle
  783.         call   check_fdd_motor_status
  784.         jmp    osloop
  785. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  786. ;                                                                    ;
  787. ;                      MAIN OS LOOP END                              ;
  788. ;                                                                    ;
  789. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  790.  
  791. checkidle:
  792.         pushad
  793.  
  794.         cmp  [check_idle_semaphore],0
  795.         jne  no_idle_state
  796.  
  797.         call change_task
  798.         mov  eax,[idlemem]
  799.         mov  ebx,[timer_ticks] ;[0xfdf0]
  800.         cmp  eax,ebx
  801.         jnz  idle_exit
  802.         call _rdtsc
  803.         mov  ecx,eax
  804.       idle_loop:
  805.         hlt
  806.         cmp  [check_idle_semaphore],0
  807.         jne  idle_loop_exit
  808.         mov  eax,[timer_ticks] ;[0xfdf0]
  809.         cmp  ebx,eax
  810.         jz   idle_loop
  811.       idle_loop_exit:
  812.         mov  [idlemem],eax
  813.         call _rdtsc
  814.         sub  eax,ecx
  815.         mov  ebx,[idleuse]
  816.         add  ebx,eax
  817.         mov  [idleuse],ebx
  818.  
  819.         popad
  820.         ret
  821.  
  822.       idle_exit:
  823.  
  824.         mov  ebx,[timer_ticks] ;[0xfdf0]
  825.         mov  [idlemem],ebx
  826.         call change_task
  827.  
  828.         popad
  829.         ret
  830.  
  831.       no_idle_state:
  832.  
  833.         dec  [check_idle_semaphore]
  834.  
  835.         mov  ebx,[timer_ticks] ;[0xfdf0]
  836.         mov  [idlemem],ebx
  837.         call change_task
  838.  
  839.         popad
  840.         ret
  841.  
  842. uglobal
  843.   idlemem               dd   0x0
  844.   idleuse               dd   0x0
  845.   idleusesec            dd   0x0
  846.   check_idle_semaphore  dd   0x0
  847. endg
  848.  
  849.  
  850.  
  851. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  852. ;                                                                      ;
  853. ;                   INCLUDED SYSTEM FILES                              ;
  854. ;                                                                      ;
  855. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  856.  
  857.  
  858. include "kernel32.inc"
  859.  
  860.  
  861. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  862. ;                                                                      ;
  863. ;                       KERNEL FUNCTIONS                               ;
  864. ;                                                                      ;
  865. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  866.  
  867. reserve_irqs_ports:
  868.  
  869.         pushad
  870.  
  871.         mov  [irq_owner+4*0], 1    ; timer
  872.         mov  [irq_owner+4*1], 1    ; keyboard
  873.         mov  [irq_owner+4*5], 1    ; sound blaster
  874.         mov  [irq_owner+4*6], 1    ; floppy diskette
  875.         mov  [irq_owner+4*13], 1   ; math co-pros
  876.         mov  [irq_owner+4*14], 1   ; ide I
  877.         mov  [irq_owner+4*15], 1   ; ide II
  878.  
  879. ;        movzx eax,byte [0xf604]        ; mouse irq
  880. ;        dec   eax
  881. ;        add   eax,mouseirqtable
  882. ;        movzx eax,byte [eax]
  883. ;        shl   eax,2
  884. ;        mov   [irq_owner+eax],byte 1
  885.  
  886.  
  887.                                        ; RESERVE PORTS
  888.         mov   edi,1                    ; 0x00-0x2d
  889.         mov   [RESERVED_PORTS],edi
  890.         shl   edi,4
  891.         mov   [RESERVED_PORTS+edi+0],dword 1
  892.         mov   [RESERVED_PORTS+edi+4],dword 0x0
  893.         mov   [RESERVED_PORTS+edi+8],dword 0x2d
  894.  
  895.         inc   dword [RESERVED_PORTS]          ; 0x30-0x4d
  896.         mov   edi,[RESERVED_PORTS]
  897.         shl   edi,4
  898.         mov   [RESERVED_PORTS+edi+0],dword 1
  899.         mov   [RESERVED_PORTS+edi+4],dword 0x30
  900.         mov   [RESERVED_PORTS+edi+8],dword 0x4d
  901.  
  902.         inc   dword [RESERVED_PORTS]          ; 0x50-0xdf
  903.         mov   edi,[RESERVED_PORTS]
  904.         shl   edi,4
  905.         mov   [RESERVED_PORTS+edi+0],dword 1
  906.         mov   [RESERVED_PORTS+edi+4],dword 0x50
  907.         mov   [RESERVED_PORTS+edi+8],dword 0xdf
  908.  
  909.         inc   dword [RESERVED_PORTS]          ; 0xe5-0xff
  910.         mov   edi,[RESERVED_PORTS]
  911.         shl   edi,4
  912.         mov   [RESERVED_PORTS+edi+0],dword 1
  913.         mov   [RESERVED_PORTS+edi+4],dword 0xe5
  914.         mov   [RESERVED_PORTS+edi+8],dword 0xff
  915.  
  916.  
  917. ;        cmp   [0xf604],byte 2          ; com1 mouse -> 0x3f0-0x3ff
  918. ;        jne   ripl1
  919. ;        inc   dword [0x2d0000]
  920. ;        mov   edi,[0x2d0000]
  921. ;        shl   edi,4
  922. ;        mov   [0x2d0000+edi+0],dword 1
  923. ;        mov   [0x2d0000+edi+4],dword 0x3f0
  924. ;        mov   [0x2d0000+edi+8],dword 0x3ff
  925. ;      ripl1:
  926. ;        cmp   [0xf604],byte 3          ; com2 mouse -> 0x2f0-0x2ff
  927. ;        jne   ripl2
  928. ;        inc   dword [0x2d0000]
  929. ;        mov   edi,[0x2d0000]
  930. ;        shl   edi,4
  931. ;        mov   [0x2d0000+edi+0],dword 1
  932. ;        mov   [0x2d0000+edi+4],dword 0x2f0
  933. ;        mov   [0x2d0000+edi+8],dword 0x2ff
  934. ;      ripl2:
  935.  
  936.         popad
  937.         ret
  938.  
  939. iglobal
  940. mouseirqtable   db  12    ; ps2
  941.                 db  4     ; com1
  942.                 db  3     ; com2
  943. endg
  944.  
  945. setirqreadports:
  946.  
  947.         mov   [irq12read+0],dword 0x60 + 0x01000000  ; read port 0x60 , byte
  948.         mov   [irq12read+4],dword 0                  ; end of port list
  949.         mov   [irq04read+0],dword 0x3f8 + 0x01000000 ; read port 0x3f8 , byte
  950.         mov   [irq04read+4],dword 0                  ; end of port list
  951.         mov   [irq03read+0],dword 0x2f8 + 0x01000000 ; read port 0x2f8 , byte
  952.         mov   [irq03read+4],dword 0                  ; end of port list
  953.  
  954.         ret
  955.  
  956. iglobal
  957.   process_number dd 0x1
  958. endg
  959.  
  960. set_variables:
  961.  
  962.         mov   ecx,0x100                       ; flush port 0x60
  963. .fl60:  in    al,0x60
  964.         loop  .fl60
  965.         mov   [MOUSE_BUFF_COUNT],byte 0                 ; mouse buffer
  966.         mov   [KEY_COUNT],byte 0                 ; keyboard buffer
  967.         mov   [BTN_COUNT],byte 0                 ; button buffer
  968. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  969.  
  970.         push  eax
  971.         mov   ax,[BOOT_VAR+0x900c]
  972.         shr   ax,1
  973.         shl   eax,16
  974.         mov   ax,[BOOT_VAR+0x900A]
  975.         shr   ax,1
  976.         mov   [MOUSE_X],eax
  977.         pop   eax
  978.  
  979.         mov   byte [SB16_Status],0            ; Minazzi Paolo
  980.         mov   [display_data-12],dword 1       ; tiled background
  981.         mov   [BTN_ADDR],dword BUTTON_INFO    ; address of button list
  982.  
  983.      ;!! IP 04.02.2005:
  984.         mov   [next_usage_update], 100
  985.         mov   byte [DONT_SWITCH], 0 ; change task if possible
  986.  
  987.         ret
  988.  
  989. ;* mouse centered - start code- Mario79
  990. mouse_centered:
  991.         push  eax
  992.         mov   eax,[ScreenWidth]
  993.         shr   eax,1
  994.         mov   [MOUSE_X],ax
  995.         mov   eax,[ScreenHeight]
  996.         shr   eax,1
  997.         mov   [MOUSE_Y],ax
  998.         pop   eax
  999.         ret
  1000. ;* mouse centered - end code- Mario79
  1001.  
  1002. align 4
  1003.  
  1004. sys_outport:
  1005.  
  1006.     mov   edi,ebx          ; separate flag for read / write
  1007.     and   ebx,65535
  1008.  
  1009.     mov   ecx,[RESERVED_PORTS]
  1010.     test  ecx,ecx
  1011.     jne   sopl8
  1012.     mov   [esp+36],dword 1
  1013.     ret
  1014.  
  1015.   sopl8:
  1016.     mov   edx,[TASK_BASE]
  1017.     mov   edx,[edx+0x4]
  1018.     and   ebx,65535
  1019.     cld
  1020.   sopl1:
  1021.  
  1022.     mov   esi,ecx
  1023.     shl   esi,4
  1024.     add   esi,RESERVED_PORTS
  1025.     cmp   edx,[esi+0]
  1026.     jne   sopl2
  1027.     cmp   ebx,[esi+4]
  1028.     jb    sopl2
  1029.     cmp   ebx,[esi+8]
  1030.     jg    sopl2
  1031.     jmp   sopl3
  1032.  
  1033.   sopl2:
  1034.  
  1035.     dec   ecx
  1036.     jnz   sopl1
  1037.     mov   [esp+36],dword 1
  1038.     ret
  1039.  
  1040.   sopl3:
  1041.  
  1042.     test  edi,0x80000000 ; read ?
  1043.     jnz   sopl4
  1044.  
  1045.     mov   dx,bx          ; write
  1046.     out   dx,al
  1047.     mov   [esp+36],dword 0
  1048.     ret
  1049.  
  1050.   sopl4:
  1051.  
  1052.     mov   dx,bx          ; read
  1053.     in    al,dx
  1054.     and   eax,0xff
  1055.     mov   [esp+36],dword 0
  1056.     mov   [esp+24],eax
  1057.     ret
  1058.  
  1059.  
  1060.  
  1061. align 4
  1062. sys_sb16:
  1063.  
  1064.      cmp  word [sb16],word 0
  1065.      jnz  sb16l1
  1066.      mov  [esp+36],dword 1
  1067.      ret
  1068.    sb16l1:
  1069.      mov  [esp+36],dword 0
  1070.      cmp  eax,1    ; set volume - main
  1071.      jnz  sb16l2
  1072.      mov  dx,word [sb16]
  1073.      add  dx,4
  1074.      mov  al,0x22
  1075.      out  dx,al
  1076.      mov  esi,1
  1077.      call delay_ms
  1078.      mov  eax,ebx
  1079.      inc  edx
  1080.      out  dx,al
  1081.      ret
  1082.    sb16l2:
  1083.  
  1084.      cmp  eax,2    ; set volume - cd
  1085.      jnz  sb16l3
  1086.      mov  dx,word [sb16]
  1087.      add  dx,4
  1088.      mov  al,0x28
  1089.      out  dx,al
  1090.      mov  esi,1
  1091.      call delay_ms
  1092.      mov  eax,ebx
  1093.      add  edx,1
  1094.      out  dx,al
  1095.      ret
  1096.    sb16l3:
  1097.       mov  [esp+36],dword 2
  1098.       ret
  1099.  
  1100.  
  1101. align 4
  1102.  
  1103. sys_sb16II:
  1104.  
  1105.      cmp  word [sb16],word 0
  1106.      jnz  IIsb16l1
  1107.      mov  [esp+36],dword 1
  1108.      ret
  1109.    IIsb16l1:
  1110.  
  1111.      cmp  eax,1    ; set volume - main
  1112.      jnz  IIsb16l2
  1113.      ; L
  1114.      mov  dx,word [sb16]
  1115.      add  dx,4
  1116.      mov  al,0x30
  1117.      out  dx,al
  1118.      mov  eax,ebx
  1119.      inc  edx
  1120.      out  dx,al
  1121.      ; R
  1122.      mov  dx,word [sb16]
  1123.      add  dx,4
  1124.      mov  al,0x31
  1125.      out  dx,al
  1126.      mov  eax,ebx
  1127.      inc  edx
  1128.      out  dx,al
  1129.      mov  [esp+36],dword 0
  1130.      ret
  1131.    IIsb16l2:
  1132.  
  1133.      cmp  eax,2    ; set volume - cd
  1134.      jnz  IIsb16l3
  1135.      ; L
  1136.      mov  dx,word [sb16]
  1137.      add  dx,4
  1138.      mov  al,0x36
  1139.      out  dx,al
  1140.      mov  eax,ebx
  1141.      inc  edx
  1142.      out  dx,al
  1143.      ; R
  1144.      mov  dx,word [sb16]
  1145.      add  dx,4
  1146.      mov  al,0x37
  1147.      out  dx,al
  1148.      mov  eax,ebx
  1149.      inc  edx
  1150.      out  dx,al
  1151.      mov  [esp+36],dword 0
  1152.      ret
  1153.    IIsb16l3:
  1154.  
  1155.      mov  [esp+36],dword 2
  1156.      ret
  1157.  
  1158.  
  1159. display_number:
  1160.  
  1161. ; eax = print type, al=0 -> ebx is number
  1162. ;                   al=1 -> ebx is pointer
  1163. ;                   ah=0 -> display decimal
  1164. ;                   ah=1 -> display hexadecimal
  1165. ;                   ah=2 -> display binary
  1166. ;                   eax bits 16-21 = number of digits to display (0-32)
  1167. ;                   eax bits 22-31 = reserved
  1168. ;
  1169. ; ebx = number or pointer
  1170. ; ecx = x shl 16 + y
  1171. ; edx = color
  1172.         xor     edi, edi
  1173. display_number_force:
  1174.  
  1175.      cmp   eax,0xffff            ; length > 0 ?
  1176.      jge   cont_displ
  1177.      ret
  1178.    cont_displ:
  1179.  
  1180.      cmp   eax,61*0x10000        ; length <= 60 ?
  1181.      jb    cont_displ2
  1182.      ret
  1183.    cont_displ2:
  1184.  
  1185.      pushad
  1186.  
  1187.      cmp   al,1                  ; ecx is a pointer ?
  1188.      jne   displnl1
  1189.      mov   ebx,[ebx+std_application_base_address]
  1190.    displnl1:
  1191.      sub   esp,64
  1192.  
  1193.      cmp   ah,0                  ; DECIMAL
  1194.      jne   no_display_desnum
  1195.      shr   eax,16
  1196.      and   eax,0x3f
  1197.      push  eax
  1198.      mov   edi,esp
  1199.      add   edi,4+64-1
  1200.      mov   ecx,eax
  1201.      mov   eax,ebx
  1202.      mov   ebx,10
  1203.    d_desnum:
  1204.      xor   edx,edx
  1205.      div   ebx
  1206.      add   dl,48
  1207.      mov   [edi],dl
  1208.      dec   edi
  1209.      loop  d_desnum
  1210.      pop   eax
  1211.      call  draw_num_text
  1212.      add   esp,64
  1213.      popad
  1214.      ret
  1215.    no_display_desnum:
  1216.  
  1217.      cmp   ah,0x01               ; HEXADECIMAL
  1218.      jne   no_display_hexnum
  1219.      shr   eax,16
  1220.      and   eax,0x3f
  1221.      push  eax
  1222.      mov   edi,esp
  1223.      add   edi,4+64-1
  1224.      mov   ecx,eax
  1225.      mov   eax,ebx
  1226.      mov   ebx,16
  1227.    d_hexnum:
  1228.      xor   edx,edx
  1229.      div   ebx
  1230.      add   edx,hexletters
  1231.      mov   dl,[edx]
  1232.      mov   [edi],dl
  1233.      dec   edi
  1234.      loop  d_hexnum
  1235.      pop   eax
  1236.      call  draw_num_text
  1237.      add   esp,64
  1238.      popad
  1239.      ret
  1240.    no_display_hexnum:
  1241.  
  1242.      cmp   ah,0x02               ; BINARY
  1243.      jne   no_display_binnum
  1244.      shr   eax,16
  1245.      and   eax,0x3f
  1246.      push  eax
  1247.      mov   edi,esp
  1248.      add   edi,4+64-1
  1249.      mov   ecx,eax
  1250.      mov   eax,ebx
  1251.      mov   ebx,2
  1252.    d_binnum:
  1253.      xor   edx,edx
  1254.      div   ebx
  1255.      add   dl,48
  1256.      mov   [edi],dl
  1257.      dec   edi
  1258.      loop  d_binnum
  1259.      pop   eax
  1260.      call  draw_num_text
  1261.      add   esp,64
  1262.      popad
  1263.      ret
  1264.    no_display_binnum:
  1265.  
  1266.      add   esp,64
  1267.      popad
  1268.      ret
  1269.  
  1270.  
  1271. draw_num_text:
  1272.  
  1273.      ; dtext
  1274.      ;
  1275.      ; eax x & y
  1276.      ; ebx color
  1277.      ; ecx start of text
  1278.      ; edx length
  1279.      ; edi 1 force
  1280.  
  1281. ;        mov     edi,[CURRENT_TASK]
  1282. ;        shl     edi,8
  1283. ;        add     ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1284. ;        rol     eax,16
  1285. ;        add     ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1286. ;        rol     eax,16
  1287.  
  1288.      mov   edx,eax
  1289.      mov   ecx,64+4
  1290.      sub   ecx,eax
  1291.      add   ecx,esp
  1292.      mov   eax,[esp+64+32-8+4]
  1293.      push  edx                       ; add window start x & y
  1294.      mov   edx,[TASK_BASE]
  1295.  
  1296.      mov   edi,[CURRENT_TASK]
  1297.      shl   edi,8
  1298.  
  1299.      mov   ebx,[edx-twdw+WDATA.box.left]
  1300.      add   ebx,[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1301.      shl   ebx,16
  1302.      add   ebx,[edx-twdw+WDATA.box.top]
  1303.      add   ebx,[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1304.      add   eax,ebx
  1305.      pop   edx
  1306.      mov   ebx,[esp+64+32-12+4]
  1307.         and     ebx, not 0x80000000     ; force counted string
  1308.         mov     esi, [esp+64+4+4]
  1309.         mov     edi, [esp+64+4]
  1310.      jmp   dtext
  1311.  
  1312. read_string:
  1313.  
  1314.     ; eax  read_area
  1315.     ; ebx  color of letter
  1316.     ; ecx  color of background
  1317.     ; edx  number of letters to read
  1318.     ; esi  [x start]*65536 + [y_start]
  1319.  
  1320.     ret
  1321.  
  1322.  
  1323. align 4
  1324.  
  1325. sys_setup:
  1326.  
  1327. ; 1=roland mpu midi base , base io address
  1328. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1329. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1330. ; 4=sb16 base , base io address
  1331. ; 5=system language, 1eng 2fi 3ger 4rus
  1332. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1333. ; 8=fat32 partition in hd
  1334. ; 9
  1335. ; 10 = sound dma channel
  1336. ; 11 = enable lba read
  1337. ; 12 = enable pci access
  1338.  
  1339.  
  1340.      mov  [esp+36],dword 0
  1341.      cmp  eax,1                      ; MIDI
  1342.      jnz  nsyse1
  1343.      cmp  ebx,0x100
  1344.      jb   nsyse1
  1345.      mov  edx,65535
  1346.      cmp  edx,ebx
  1347.      jb   nsyse1
  1348.      mov  [midi_base],bx
  1349.      mov  word [mididp],bx
  1350.      inc  bx
  1351.      mov  word [midisp],bx
  1352.      ret
  1353.  
  1354. iglobal
  1355. midi_base dw 0
  1356. endg
  1357.  
  1358.    nsyse1:
  1359.  
  1360.      cmp  eax,2                      ; KEYBOARD
  1361.      jnz  nsyse2
  1362.      cmp  ebx,1
  1363.      jnz  kbnobase
  1364.      mov  edi,[TASK_BASE]
  1365.      add  ecx,[edi+TASKDATA.mem_start]
  1366.      mov  eax,ecx
  1367.      mov  ebx,keymap
  1368.      mov  ecx,128
  1369.      call memmove
  1370.      ret
  1371.    kbnobase:
  1372.      cmp  ebx,2
  1373.      jnz  kbnoshift
  1374.      mov  edi,[TASK_BASE]
  1375.      add  ecx,[edi+TASKDATA.mem_start]
  1376.      mov  eax,ecx
  1377.      mov  ebx,keymap_shift
  1378.      mov  ecx,128
  1379.      call memmove
  1380.      ret
  1381.    kbnoshift:
  1382.      cmp  ebx,3
  1383.      jne  kbnoalt
  1384.      mov  edi,[TASK_BASE]
  1385.      add  ecx,[edi+TASKDATA.mem_start]
  1386.      mov  eax,ecx
  1387.      mov  ebx,keymap_alt
  1388.      mov  ecx,128
  1389.      call memmove
  1390.      ret
  1391.    kbnoalt:
  1392.      cmp  ebx,9
  1393.      jnz  kbnocountry
  1394.      mov  word [keyboard],cx
  1395.      ret
  1396.    kbnocountry:
  1397.      mov  [esp+36],dword 1
  1398.      ret
  1399.    nsyse2:
  1400.      cmp  eax,3                      ; CD
  1401.      jnz  nsyse3
  1402.      test ebx,ebx
  1403.      jz   nosesl
  1404.      cmp  ebx, 4
  1405.      ja   nosesl
  1406.      mov  [cd_base],bl
  1407.      cmp  ebx,1
  1408.      jnz  noprma
  1409.      mov  [cdbase],0x1f0
  1410.      mov  [cdid],0xa0
  1411.    noprma:
  1412.      cmp  ebx,2
  1413.      jnz  noprsl
  1414.      mov  [cdbase],0x1f0
  1415.      mov  [cdid],0xb0
  1416.    noprsl:
  1417.      cmp  ebx,3
  1418.      jnz  nosema
  1419.      mov  [cdbase],0x170
  1420.      mov  [cdid],0xa0
  1421.    nosema:
  1422.      cmp  ebx,4
  1423.      jnz  nosesl
  1424.      mov  [cdbase],0x170
  1425.      mov  [cdid],0xb0
  1426.    nosesl:
  1427.      ret
  1428.  
  1429. cd_base db 0
  1430.  
  1431.    nsyse3:
  1432.  
  1433.      cmp  eax,4                      ; SB
  1434.      jnz  nsyse4
  1435.      cmp  ebx,0x100
  1436.      jb   nsyse4
  1437.      mov  edx,65535
  1438.      cmp  edx,ebx
  1439.      jb   nsyse4
  1440.      mov  word [sb16],bx
  1441.      ret
  1442.    nsyse4:
  1443.  
  1444.      cmp  eax,5                      ; SYSTEM LANGUAGE
  1445.      jnz  nsyse5
  1446.      mov  [syslang],ebx
  1447.      ret
  1448.    nsyse5:
  1449.  
  1450.      cmp  eax,7                      ; HD BASE
  1451.      jne  nsyse7
  1452.      test ebx,ebx
  1453.      jz   nosethd
  1454.      cmp  ebx,4
  1455.      ja   nosethd
  1456.      mov  [hd_base],bl
  1457.      cmp  ebx,1
  1458.      jnz  noprmahd
  1459.      mov  [hdbase],0x1f0
  1460.      mov  [hdid],0x0
  1461.      mov  [hdpos],1
  1462. ;     call set_FAT32_variables
  1463.    noprmahd:
  1464.      cmp  ebx,2
  1465.      jnz  noprslhd
  1466.      mov  [hdbase],0x1f0
  1467.      mov  [hdid],0x10
  1468.      mov  [hdpos],2
  1469. ;     call set_FAT32_variables
  1470.    noprslhd:
  1471.      cmp  ebx,3
  1472.      jnz  nosemahd
  1473.      mov  [hdbase],0x170
  1474.      mov  [hdid],0x0
  1475.      mov  [hdpos],3
  1476. ;     call set_FAT32_variables
  1477.    nosemahd:
  1478.      cmp  ebx,4
  1479.      jnz  noseslhd
  1480.      mov  [hdbase],0x170
  1481.      mov  [hdid],0x10
  1482.      mov  [hdpos],4
  1483. ;     call set_FAT32_variables
  1484.    noseslhd:
  1485.     call  reserve_hd1
  1486.     call  reserve_hd_channel
  1487.     call  free_hd_channel
  1488.     mov   [hd1_status],0        ; free
  1489.    nosethd:
  1490.      ret
  1491.  
  1492. iglobal
  1493. hd_base db 0
  1494. endg
  1495.  
  1496.    nsyse7:
  1497.  
  1498.      cmp  eax,8                      ; HD PARTITION
  1499.      jne  nsyse8
  1500.      mov  [fat32part],ebx
  1501. ;     call set_FAT32_variables
  1502.     call  reserve_hd1
  1503.     call  reserve_hd_channel
  1504.     call  free_hd_channel
  1505.      pusha
  1506.      call  choice_necessity_partition_1
  1507.      popa
  1508.     mov   [hd1_status],0        ; free
  1509.      ret
  1510.    nsyse8:
  1511.  
  1512.      cmp  eax,10                     ; SOUND DMA CHANNEL
  1513.      jne  no_set_sound_dma
  1514.      cmp  ebx,3
  1515.      ja   sys_setup_err
  1516.      mov  [sound_dma],ebx
  1517.      ret
  1518.    no_set_sound_dma:
  1519.  
  1520.      cmp  eax,11                     ; ENABLE LBA READ
  1521.      jne  no_set_lba_read
  1522.      and  ebx,1
  1523.      mov  [lba_read_enabled],ebx
  1524.      ret
  1525.    no_set_lba_read:
  1526.  
  1527.      cmp  eax,12                     ; ENABLE PCI ACCESS
  1528.      jne  no_set_pci_access
  1529.      and  ebx,1
  1530.      mov  [pci_access_enabled],ebx
  1531.      ret
  1532.    no_set_pci_access:
  1533.  
  1534. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1535. include 'vmodeint.inc'
  1536. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1537.  
  1538. sys_setup_err:
  1539.      mov  [esp+36],dword -1
  1540.      ret
  1541.  
  1542.  
  1543. align 4
  1544.  
  1545. sys_getsetup:
  1546.  
  1547. ; 1=roland mpu midi base , base io address
  1548. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1549. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1550. ; 4=sb16 base , base io address
  1551. ; 5=system language, 1eng 2fi 3ger 4rus
  1552. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1553. ; 8=fat32 partition in hd
  1554. ; 9=get hs timer tic
  1555.  
  1556.      cmp  eax,1
  1557.      jne  ngsyse1
  1558.      movzx eax,[midi_base]
  1559.      mov  [esp+36],eax
  1560.      ret
  1561.    ngsyse1:
  1562.  
  1563.      cmp  eax,2
  1564.      jne  ngsyse2
  1565.      cmp  ebx,1
  1566.      jnz  kbnobaseret
  1567.      mov  edi,[TASK_BASE]
  1568.      add  ecx,[edi+TASKDATA.mem_start]
  1569.      mov  ebx,ecx
  1570.      mov  eax,keymap
  1571.      mov  ecx,128
  1572.      call memmove
  1573.      ret
  1574.    kbnobaseret:
  1575.      cmp  ebx,2
  1576.      jnz  kbnoshiftret
  1577.      mov  edi,[TASK_BASE]
  1578.      add  ecx,[edi+TASKDATA.mem_start]
  1579.      mov  ebx,ecx
  1580.      mov  eax,keymap_shift
  1581.      mov  ecx,128
  1582.      call memmove
  1583.      ret
  1584.    kbnoshiftret:
  1585.      cmp  ebx,3
  1586.      jne  kbnoaltret
  1587.      mov  edi,[TASK_BASE]
  1588.      add  ecx,[edi+TASKDATA.mem_start]
  1589.      mov  ebx,ecx
  1590.      mov  eax,keymap_alt
  1591.      mov  ecx,128
  1592.      call memmove
  1593.      ret
  1594.    kbnoaltret:
  1595.      cmp  ebx,9
  1596.      jnz  ngsyse2
  1597.      movzx eax,word [keyboard]
  1598.      mov  [esp+36],eax
  1599.      ret
  1600.    ngsyse2:
  1601.  
  1602.      cmp  eax,3
  1603.      jnz  ngsyse3
  1604.      movzx eax,[cd_base]
  1605.      mov  [esp+36],eax
  1606.      ret
  1607.    ngsyse3:
  1608.  
  1609.      cmp  eax,4
  1610.      jne  ngsyse4
  1611.      mov  eax,[sb16]
  1612.      mov  [esp+36],eax
  1613.      ret
  1614.    ngsyse4:
  1615.  
  1616.      cmp  eax,5
  1617.      jnz  ngsyse5
  1618.      mov  eax,[syslang]
  1619.      mov  [esp+36],eax
  1620.      ret
  1621.    ngsyse5:
  1622.      cmp  eax,7
  1623.      jnz  ngsyse7
  1624.      movzx eax,[hd_base]
  1625.      mov  [esp+36],eax
  1626.      ret
  1627.    ngsyse7:
  1628.      cmp  eax,8
  1629.      jnz  ngsyse8
  1630.      mov eax,[fat32part]
  1631.      mov  [esp+36],eax
  1632.      ret
  1633.    ngsyse8:
  1634.      cmp  eax,9
  1635.      jne  ngsyse9
  1636.      mov  eax,[timer_ticks] ;[0xfdf0]
  1637.      mov  [esp+36],eax
  1638.      ret
  1639.    ngsyse9:
  1640.      cmp  eax,10
  1641.      jnz  ngsyse10
  1642.      mov eax,[sound_dma]
  1643.      mov  [esp+36],eax
  1644.      ret
  1645.    ngsyse10:
  1646.      cmp  eax,11
  1647.      jnz  ngsyse11
  1648.      mov eax,[lba_read_enabled]
  1649.      mov  [esp+36],eax
  1650.      ret
  1651.    ngsyse11:
  1652.      cmp  eax,12
  1653.      jnz  ngsyse12
  1654.      mov eax,[pci_access_enabled]
  1655.      mov  [esp+36],eax
  1656.      ret
  1657.    ngsyse12:
  1658.      mov  [esp+36],dword 1
  1659.      ret
  1660.  
  1661. iglobal
  1662. align 4
  1663. mousefn dd msscreen, mswin, msbutton, msset
  1664.         dd app_load_cursor
  1665.         dd app_set_cursor
  1666.         dd app_delete_cursor
  1667. endg
  1668.  
  1669. readmousepos:
  1670.  
  1671. ; eax=0 screen relative
  1672. ; eax=1 window relative
  1673. ; eax=2 buttons pressed
  1674. ; eax=3 set mouse pos   ; reserved
  1675. ; eax=4 load cursor
  1676. ; eax=5 set cursor
  1677. ; eax=6 delete cursor   ; reserved
  1678.  
  1679.            cmp eax, 6
  1680.            ja msset
  1681.            jmp [mousefn+eax*4]
  1682. msscreen:
  1683.            mov  eax,[MOUSE_X]
  1684.            shl  eax,16
  1685.            mov  ax,[MOUSE_Y]
  1686.            mov  [esp+36],eax
  1687.            ret
  1688. mswin:
  1689.            mov  eax,[MOUSE_X]
  1690.            shl  eax,16
  1691.            mov  ax,[MOUSE_Y]
  1692.            mov  esi,[TASK_BASE]
  1693.            mov  bx, word [esi-twdw+WDATA.box.left]
  1694.            shl  ebx,16
  1695.            mov  bx, word [esi-twdw+WDATA.box.top]
  1696.            sub  eax,ebx
  1697.  
  1698.            mov  edi,[CURRENT_TASK]
  1699.            shl  edi,8
  1700.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1701.            rol  eax,16
  1702.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1703.            rol  eax,16
  1704.            mov  [esp+36],eax
  1705.            ret
  1706. msbutton:
  1707.            movzx eax,byte [BTN_DOWN]
  1708.            mov  [esp+36],eax
  1709.            ret
  1710. msset:
  1711.            ret
  1712.  
  1713. app_load_cursor:
  1714.            add ebx, new_app_base
  1715.            cmp ebx, new_app_base
  1716.            jb msset
  1717.            stdcall load_cursor, ebx, ecx
  1718.            mov [esp+36], eax
  1719.            ret
  1720.  
  1721. app_set_cursor:
  1722.            stdcall set_cursor, ebx
  1723.            mov [esp+36], eax
  1724.            ret
  1725.  
  1726. app_delete_cursor:
  1727.            stdcall delete_cursor, ebx
  1728.            mov [esp+36], eax
  1729.            ret
  1730.  
  1731. is_input:
  1732.  
  1733.    push edx
  1734.    mov  dx,word [midisp]
  1735.    in   al,dx
  1736.    and  al,0x80
  1737.    pop  edx
  1738.    ret
  1739.  
  1740. is_output:
  1741.  
  1742.    push edx
  1743.    mov  dx,word [midisp]
  1744.    in   al,dx
  1745.    and  al,0x40
  1746.    pop  edx
  1747.    ret
  1748.  
  1749.  
  1750. get_mpu_in:
  1751.  
  1752.    push edx
  1753.    mov  dx,word [mididp]
  1754.    in   al,dx
  1755.    pop  edx
  1756.    ret
  1757.  
  1758.  
  1759. put_mpu_out:
  1760.  
  1761.    push edx
  1762.    mov  dx,word [mididp]
  1763.    out  dx,al
  1764.    pop  edx
  1765.    ret
  1766.  
  1767.  
  1768. setuart:
  1769.  
  1770.  su1:
  1771.    call is_output
  1772.    cmp  al,0
  1773.    jnz  su1
  1774.    mov  dx,word [midisp]
  1775.    mov  al,0xff
  1776.    out  dx,al
  1777.  su2:
  1778.    mov  dx,word [midisp]
  1779.    mov  al,0xff
  1780.    out  dx,al
  1781.    call is_input
  1782.    cmp  al,0
  1783.    jnz  su2
  1784.    call get_mpu_in
  1785.    cmp  al,0xfe
  1786.    jnz  su2
  1787.  su3:
  1788.    call is_output
  1789.    cmp  al,0
  1790.    jnz  su3
  1791.    mov  dx,word [midisp]
  1792.    mov  al,0x3f
  1793.    out  dx,al
  1794.  
  1795.    ret
  1796.  
  1797.  
  1798. align 4
  1799.  
  1800. sys_midi:
  1801.  
  1802.      cmp  [mididp],0
  1803.      jnz  sm0
  1804.      mov  [esp+36],dword 1
  1805.      ret
  1806.    sm0:
  1807.  
  1808.      cmp  eax,1
  1809.      mov  [esp+36],dword 0
  1810.      jnz  smn1
  1811.      call setuart
  1812.      ret
  1813.    smn1:
  1814.  
  1815.      cmp  eax,2
  1816.      jnz  smn2
  1817.    sm10:
  1818.      call get_mpu_in
  1819.      call is_output
  1820.      test al,al
  1821.      jnz  sm10
  1822.      mov  al,bl
  1823.      call put_mpu_out
  1824.      ret
  1825.    smn2:
  1826.  
  1827.      ret
  1828.  
  1829.  
  1830. detect_devices:
  1831. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1832. include 'detect/commouse.inc'
  1833. include 'detect/ps2mouse.inc'
  1834. ;include 'detect/dev_fd.inc'
  1835. ;include 'detect/dev_hdcd.inc'
  1836. ;include 'detect/sear_par.inc'
  1837. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1838.     ret
  1839.  
  1840.  
  1841. sys_end:
  1842.  
  1843.      mov   eax,[TASK_BASE]
  1844.      mov   [eax+TASKDATA.state], 3  ; terminate this program
  1845.  
  1846.     waitterm:            ; wait here for termination
  1847.      mov   eax,5
  1848.      call  delay_hs
  1849.      jmp   waitterm
  1850.  
  1851. iglobal
  1852. align 4
  1853. sys_system_table:
  1854.         dd      sysfn_shutdown          ; 1 = system shutdown
  1855.         dd      sysfn_terminate         ; 2 = terminate thread
  1856.         dd      sysfn_activate          ; 3 = activate window
  1857.         dd      sysfn_getidletime       ; 4 = get idle time
  1858.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1859.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1860.         dd      sysfn_getactive         ; 7 = get active window
  1861.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1862.         dd      sysfn_shutdown_param    ; 9 = shutdown with parameter
  1863.         dd      sysfn_minimize          ; 10 = minimize window
  1864.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1865.         dd      sysfn_lastkey           ; 12 = get last pressed key
  1866.         dd      sysfn_getversion        ; 13 = get kernel version
  1867.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1868.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1869.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1870.         dd      sysfn_getallmem         ; 17 = get total memory size
  1871.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1872.                                         ;                 instead of slot
  1873.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1874.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1875. sysfn_num = ($ - sys_system_table)/4
  1876. endg
  1877.  
  1878. sys_system:
  1879.         dec     eax
  1880.         cmp     eax, sysfn_num
  1881.         jae     @f
  1882.         jmp     dword [sys_system_table + eax*4]
  1883. @@:
  1884.         ret
  1885.  
  1886. sysfn_shutdown:         ; 18.1 = BOOT
  1887.      mov  [BOOT_VAR+0x9030],byte 0
  1888.   for_shutdown_parameter:
  1889.  
  1890.      mov  eax,[TASK_COUNT]
  1891.      add  eax,2
  1892.      mov  [shutdown_processes],eax
  1893.      mov  [SYS_SHUTDOWN],al
  1894.      and  dword [esp+36], 0
  1895.      ret
  1896.   uglobal
  1897.    shutdown_processes: dd 0x0
  1898.   endg
  1899.  
  1900. sysfn_terminate:        ; 18.2 = TERMINATE
  1901.      cmp  ebx,2
  1902.      jb   noprocessterminate
  1903.      mov  edx,[TASK_COUNT]
  1904.      cmp  ebx,edx
  1905.      ja   noprocessterminate
  1906.      mov  eax,[TASK_COUNT]
  1907.      shl  ebx,5
  1908.      mov  edx,[ebx+CURRENT_TASK+TASKDATA.pid]
  1909.      add  ebx,CURRENT_TASK+TASKDATA.state
  1910.      cmp  byte [ebx], 9
  1911.      jz   noprocessterminate
  1912.  
  1913.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  1914.      mov  [ebx],byte 3       ; clear possible i40's
  1915.      ;call MEM_Heap_UnLock
  1916.  
  1917.      cmp  edx,[application_table_status]    ; clear app table stat
  1918.      jne  noatsc
  1919.      mov  [application_table_status],0
  1920.    noatsc:
  1921.    noprocessterminate:
  1922.      ret
  1923.  
  1924. sysfn_terminate2:
  1925. ;lock application_table_status mutex
  1926. .table_status:
  1927.     cli
  1928.     cmp    [application_table_status],0
  1929.     je     .stf
  1930.     sti
  1931.     call   change_task
  1932.     jmp    .table_status
  1933. .stf:
  1934.     call   set_application_table_status
  1935.     mov    eax,ebx
  1936.     call   pid_to_slot
  1937.     test   eax,eax
  1938.     jz     .not_found
  1939.     mov    ebx,eax
  1940.     cli
  1941.     call   sysfn_terminate
  1942.     mov    [application_table_status],0
  1943.     sti
  1944.     and    dword [esp+36],0
  1945.     ret
  1946. .not_found:
  1947.     mov    [application_table_status],0
  1948.     or     dword [esp+36],-1
  1949.     ret
  1950.  
  1951. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  1952.      cmp  ebx,2
  1953.      jb   .nowindowactivate
  1954.      cmp  ebx,[TASK_COUNT]
  1955.      ja   .nowindowactivate
  1956.  
  1957.      mov   [window_minimize], 2   ; restore window if minimized
  1958.  
  1959.      movzx esi, word [WIN_STACK + ebx*2]
  1960.      cmp   esi, [TASK_COUNT]
  1961.      je    .nowindowactivate ; already active
  1962.  
  1963.      mov   edi, ebx
  1964.      shl   edi, 5
  1965.      add   edi, window_data
  1966.      movzx esi, word [WIN_STACK + ebx * 2]
  1967.      lea   esi, [WIN_POS + esi * 2]
  1968.      call  waredraw
  1969. .nowindowactivate:
  1970.      ret
  1971.  
  1972. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  1973.      mov  eax,[idleusesec]
  1974.      mov  [esp+36], eax
  1975.      ret
  1976.  
  1977. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  1978.      mov  eax,[CPU_FREQ]
  1979.      mov  [esp+36], eax
  1980.      ret
  1981.  
  1982. ;  SAVE ramdisk to /hd/1/menuet.img
  1983. ;!!!!!!!!!!!!!!!!!!!!!!!!
  1984.    include 'blkdev/rdsave.inc'
  1985. ;!!!!!!!!!!!!!!!!!!!!!!!!
  1986.  
  1987. sysfn_getactive:        ; 18.7 = get active window
  1988.      mov  eax, [TASK_COUNT]
  1989.    movzx  eax, word [WIN_POS + eax*2]
  1990.      mov  [esp+36],eax
  1991.      ret
  1992.  
  1993. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  1994.      cmp  ebx,1
  1995.      jne  nogetsoundflag
  1996.      movzx  eax,byte [sound_flag] ; get sound_flag
  1997.      mov  [esp+36],eax
  1998.      ret
  1999.  nogetsoundflag:
  2000.      cmp  ebx,2
  2001.      jnz  nosoundflag
  2002.      xor  byte [sound_flag], 1
  2003.  nosoundflag:
  2004.      ret
  2005.  
  2006. sysfn_shutdown_param:   ; 18.9 = system shutdown with param
  2007.      cmp  ebx,1
  2008.      jl   exit_for_anyone
  2009.      cmp  ebx,4
  2010.      jg   exit_for_anyone
  2011.      mov  [BOOT_VAR+0x9030],bl
  2012.      jmp  for_shutdown_parameter
  2013.  
  2014. sysfn_minimize:         ; 18.10 = minimize window
  2015.      mov   [window_minimize],1
  2016.  exit_for_anyone:
  2017.      ret
  2018.  
  2019. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2020.      cmp  ebx,1
  2021.      jnz  full_table
  2022.   small_table:
  2023.      call for_all_tables
  2024.      mov ecx,10
  2025.      cld
  2026.      rep movsb
  2027.      ret
  2028.    for_all_tables:
  2029.      mov edi,[TASK_BASE]
  2030.      mov edi,[edi+TASKDATA.mem_start]
  2031.      add edi,ecx
  2032.      mov esi,DRIVE_DATA
  2033.      ret
  2034.   full_table:
  2035.      cmp  ebx,2
  2036.      jnz  exit_for_anyone
  2037.      call for_all_tables
  2038.      mov ecx,16384
  2039.      cld
  2040.      rep movsd
  2041.      ret
  2042.  
  2043. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2044.         and     dword [esp+36], 0
  2045.         ret
  2046.  
  2047. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2048.      mov edi,[TASK_BASE]
  2049.      mov edi,[edi+TASKDATA.mem_start]
  2050.      add edi,ebx
  2051.      mov esi,version_inf
  2052.      mov ecx,version_end-version_inf
  2053.      cld
  2054.      rep movsb
  2055.      ret
  2056.  
  2057. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2058.      ;wait retrace functions
  2059.  sys_wait_retrace:
  2060.      mov edx,0x3da
  2061.  WaitRetrace_loop:
  2062.      in al,dx
  2063.      test al,1000b
  2064.      jz WaitRetrace_loop
  2065.      mov [esp+36],dword 0
  2066.      ret
  2067.  
  2068. sysfn_centermouse:      ; 18.15 = mouse centered
  2069.      call  mouse_centered
  2070.      mov [esp+36],dword 0
  2071.      ret
  2072.  
  2073. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  2074.      cmp  ebx,0  ; get mouse speed factor
  2075.      jnz  .set_mouse_acceleration
  2076.      xor  eax,eax
  2077.      mov  ax,[mouse_speed_factor]
  2078.      mov  [esp+36],eax
  2079.      ret
  2080.  .set_mouse_acceleration:
  2081.      cmp  ebx,1  ; set mouse speed factor
  2082.      jnz  .get_mouse_delay
  2083.      mov  [mouse_speed_factor],cx
  2084.      ret
  2085.  .get_mouse_delay:
  2086.      cmp  ebx,2  ; get mouse delay
  2087.      jnz  .set_mouse_delay
  2088.      mov  eax,[mouse_delay]
  2089.      mov  [esp+36],eax
  2090.      ret
  2091.  .set_mouse_delay:
  2092.      cmp  ebx,3  ; set mouse delay
  2093.      jnz  .set_pointer_position
  2094.      mov  [mouse_delay],ecx
  2095.      ret
  2096.  .set_pointer_position:
  2097.      cmp  ebx,4  ; set mouse pointer position
  2098.      jnz  .end
  2099.      mov   [MOUSE_Y],cx    ;y
  2100.      ror   ecx,16
  2101.      mov   [MOUSE_X],cx    ;x
  2102.      rol   ecx,16
  2103.  .end:
  2104.      ret
  2105.  
  2106. sysfn_getfreemem:
  2107.      mov eax, [pg_data.pages_free]
  2108.      shl eax, 2
  2109.      mov [esp+36],eax
  2110.      ret
  2111.  
  2112. sysfn_getallmem:
  2113.      mov  eax,[MEM_AMOUNT]
  2114.      shr eax, 10
  2115.      mov  [esp+36],eax
  2116.      ret
  2117.  
  2118. uglobal
  2119. ;// mike.dld, 2006-29-01 [
  2120. screen_workarea RECT
  2121. ;// mike.dld, 2006-29-01 ]
  2122. window_minimize db 0
  2123. sound_flag      db 0
  2124. endg
  2125.  
  2126. iglobal
  2127. version_inf:
  2128.   db 0,6,5,0  ; version 0.6.5.0
  2129.   db UID_KOLIBRI
  2130.   db 'Kolibri',0
  2131. version_end:
  2132. endg
  2133.  
  2134. UID_NONE=0
  2135. UID_MENUETOS=1   ;official
  2136. UID_KOLIBRI=2    ;russian
  2137.  
  2138. sys_cachetodiskette:
  2139. ;    pushad
  2140. ;    cmp  eax,1
  2141. ;    jne  no_write_all_of_ramdisk
  2142. ;    call fdc_writeramdisk
  2143. ;    popad
  2144. ;    ret
  2145. ;  no_write_all_of_ramdisk:
  2146. ;    cmp eax,2
  2147. ;    jne no_write_part_of_ramdisk
  2148. ;    call fdc_commitflush
  2149. ;    popad
  2150. ;    ret
  2151. ;  no_write_part_of_ramdisk:
  2152. ;    cmp  eax,3
  2153. ;    jne  no_set_fdc
  2154. ;    call fdc_set
  2155. ;    popad
  2156. ;    ret
  2157. ;  no_set_fdc:
  2158. ;    cmp  eax,4
  2159. ;    jne  no_get_fdc
  2160. ;    popad
  2161. ;    call fdc_get
  2162. ;    mov    [esp+36],ecx
  2163. ;    ret
  2164. ;  no_get_fdc:
  2165. ;    popad
  2166. ;    ret
  2167.     cmp eax,1
  2168.     jne no_floppy_a_save
  2169.     mov   [flp_number],1
  2170.     jmp save_image_on_floppy
  2171.   no_floppy_a_save:
  2172.     cmp eax,2
  2173.     jne no_floppy_b_save
  2174.     mov   [flp_number],2
  2175.   save_image_on_floppy:
  2176.     call save_image
  2177.     mov  [esp+36],dword 0
  2178.     cmp  [FDC_Status],0
  2179.     je   yes_floppy_save
  2180.   no_floppy_b_save:
  2181.     mov [esp+36],dword 1
  2182.   yes_floppy_save:
  2183.     ret
  2184.  
  2185. uglobal
  2186. ;  bgrchanged  dd  0x0
  2187. endg
  2188.  
  2189. sys_background:
  2190.  
  2191.     cmp   eax,1                            ; BACKGROUND SIZE
  2192.     jnz   nosb1
  2193.     cmp   ebx,0
  2194.     je    sbgrr
  2195.     cmp   ecx,0
  2196.     je    sbgrr
  2197.     mov   [display_data-8],ebx
  2198.     mov   [display_data-4],ecx
  2199. ;    mov   [bgrchanged],1
  2200.   sbgrr:
  2201.     ret
  2202.   nosb1:
  2203.  
  2204.     cmp   eax,2                            ; SET PIXEL
  2205.     jnz   nosb2
  2206.     mov   edx,0x160000-16
  2207.     cmp   edx,ebx
  2208.     jbe   nosb2
  2209.     mov   edx,[ebx]
  2210.     and   edx,0xFF000000 ;255*256*256*256
  2211.     and   ecx,0x00FFFFFF ;255*256*256+255*256+255
  2212.     add   edx,ecx
  2213.     mov   [ebx+IMG_BACKGROUND],edx
  2214. ;    mov   [bgrchanged],1
  2215.     ret
  2216.   nosb2:
  2217.  
  2218.     cmp   eax,3                            ; DRAW BACKGROUND
  2219.     jnz   nosb3
  2220. draw_background_temp:
  2221. ;    cmp   [bgrchanged],1 ;0
  2222. ;    je    nosb31
  2223. ;draw_background_temp:
  2224. ;    mov   [bgrchanged],1 ;0
  2225.     mov   [REDRAW_BACKGROUND],byte 1
  2226.     mov    [background_defined], 1
  2227.    nosb31:
  2228.     ret
  2229.   nosb3:
  2230.  
  2231.     cmp   eax,4                            ; TILED / STRETCHED
  2232.     jnz   nosb4
  2233.     cmp   ebx,[display_data-12]
  2234.     je    nosb41
  2235.     mov   [display_data-12],ebx
  2236. ;    mov   [bgrchanged],1
  2237.    nosb41:
  2238.     ret
  2239.   nosb4:
  2240.  
  2241.     cmp   eax,5                            ; BLOCK MOVE TO BGR
  2242.     jnz   nosb5
  2243.   ; bughere
  2244.     mov   edi, [TASK_BASE]
  2245.     add   ebx, [edi+TASKDATA.mem_start]
  2246.  ;   mov   esi, ebx
  2247.  ;   mov   edi, ecx
  2248.     mov   eax, ebx
  2249.     mov   ebx, ecx
  2250.     add   ecx, edx
  2251.     cmp   ecx, 0x160000-16
  2252.     ja    .fin
  2253.  ;   add   edi, 0x300000
  2254.     add   ebx, IMG_BACKGROUND
  2255.     mov   ecx, edx
  2256.     cmp   ecx, 0x160000-16
  2257.     ja    .fin
  2258. ;    mov   [bgrchanged],1
  2259.   ;  cld
  2260.   ;  rep   movsb
  2261.     call  memmove
  2262.   .fin:
  2263.     ret
  2264.   nosb5:
  2265.  
  2266.     ret
  2267.  
  2268.  
  2269. align 4
  2270.  
  2271. sys_getbackground:
  2272.  
  2273.     cmp   eax,1                                  ; SIZE
  2274.     jnz   nogb1
  2275.     mov   eax,[display_data-8]
  2276.     shl   eax,16
  2277.     mov   ax,[display_data-4]
  2278.     mov   [esp+36],eax
  2279.     ret
  2280.   nogb1:
  2281.  
  2282.     cmp   eax,2                                  ; PIXEL
  2283.     jnz   nogb2
  2284.     mov   edx,0x160000-16
  2285.     cmp   edx,ebx
  2286.     jbe   nogb2
  2287.     mov   eax, [ebx+IMG_BACKGROUND]
  2288.     and   eax, 0xFFFFFF
  2289.     mov   [esp+36],eax
  2290.     ret
  2291.   nogb2:
  2292.  
  2293.     cmp   eax,4                                  ; TILED / STRETCHED
  2294.     jnz   nogb4
  2295.     mov   eax,[display_data-12]
  2296.   nogb4:
  2297.     mov   [esp+36],eax
  2298.     ret
  2299.  
  2300.  
  2301. align 4
  2302.  
  2303. sys_getkey:
  2304.     mov   [esp+36],dword 1
  2305. ; test main buffer
  2306.     mov   ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2307.     movzx ecx,word [WIN_STACK + ebx * 2]
  2308.     mov   edx,[TASK_COUNT]
  2309.     cmp   ecx,edx
  2310.     jne   .finish
  2311.     cmp   [KEY_COUNT],byte 0
  2312.     je    .finish
  2313.     movzx eax,byte [KEY_BUFF]
  2314.     shl   eax,8
  2315.     push  eax
  2316.     dec   byte [KEY_COUNT]
  2317.     and   byte [KEY_COUNT],127
  2318.     movzx ecx,byte [KEY_COUNT]
  2319.     add   ecx,2
  2320.  ;   mov   esi,0xf402
  2321.  ;   mov   edi,0xf401
  2322.  ;   cld
  2323.  ;  rep   movsb
  2324.     mov   eax, KEY_BUFF+1
  2325.     mov   ebx, KEY_BUFF
  2326.     call  memmove
  2327.     pop   eax
  2328. .ret_eax:
  2329.     mov   [esp+36],eax
  2330.     ret
  2331.  .finish:
  2332. ; test hotkeys buffer
  2333.         mov     ecx, hotkey_buffer
  2334. @@:
  2335.         cmp     [ecx], ebx
  2336.         jz      .found
  2337.         add     ecx, 8
  2338.         cmp     ecx, hotkey_buffer+120*8
  2339.         jb      @b
  2340.         ret
  2341. .found:
  2342.         mov     ax, [ecx+6]
  2343.         shl     eax, 16
  2344.         mov     ah, [ecx+4]
  2345.         mov     al, 2
  2346.         and     dword [ecx+4], 0
  2347.         and     dword [ecx], 0
  2348.         jmp     .ret_eax
  2349.  
  2350. align 4
  2351.  
  2352. sys_getbutton:
  2353.  
  2354.     mov   ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2355.     mov   [esp+36],dword 1
  2356.     movzx ecx, word [WIN_STACK + ebx * 2]
  2357.     mov   edx, [TASK_COUNT] ; less than 256 processes
  2358.     cmp   ecx,edx
  2359.     jne   .exit
  2360.     movzx eax,byte [BTN_COUNT]
  2361.     test  eax,eax
  2362.     jz    .exit
  2363.     mov   eax,[BTN_BUFF]
  2364.     shl   eax,8
  2365.     mov   [BTN_COUNT],byte 0
  2366.     mov   [esp+36],eax
  2367.  .exit:
  2368.     ret
  2369.  
  2370.  
  2371. align 4
  2372.  
  2373. sys_cpuusage:
  2374.  
  2375. ;  RETURN:
  2376. ;
  2377. ;  +00 dword     process cpu usage
  2378. ;  +04  word     position in windowing stack
  2379. ;  +06  word     windowing stack value at current position (cpu nro)
  2380. ;  +10 12 bytes  name
  2381. ;  +22 dword     start in mem
  2382. ;  +26 dword     used mem
  2383. ;  +30 dword     PID , process idenfification number
  2384. ;
  2385.  
  2386.     mov  edi,[TASK_BASE]   ; eax = return area
  2387.     add  eax,[edi + TASKDATA.mem_start]
  2388.  
  2389.     cmp  ebx,-1         ; who am I ?
  2390.     jne  no_who_am_i
  2391.     mov  ebx,[CURRENT_TASK]
  2392.   no_who_am_i:
  2393.  
  2394.     push eax            ; return area
  2395.     push ebx            ; process number
  2396.  
  2397.     push ebx
  2398.     push ebx
  2399.     push eax
  2400.  
  2401.     ; return memory usage
  2402.  
  2403.     xor  edx,edx
  2404.     mov  eax,0x20
  2405.     mul  ebx
  2406.     add  eax,CURRENT_TASK+TASKDATA.cpu_usage
  2407.     mov  ebx,eax
  2408.     pop  eax
  2409.     mov  ecx,[ebx]
  2410.     mov  [eax],ecx
  2411.     pop  ebx
  2412.     mov  cx, [WIN_STACK + ebx * 2]
  2413.     mov  [eax+4],cx
  2414.     mov  cx, [WIN_POS + ebx * 2]
  2415.     mov  [eax+6],cx
  2416.     push eax
  2417.     mov  eax,ebx
  2418.     shl  eax,8
  2419.     add  eax,SLOT_BASE+APPDATA.app_name
  2420.     pop  ebx
  2421.     add  ebx,10
  2422.     mov  ecx,11
  2423.     call memmove
  2424.  
  2425.     ; memory usage
  2426.  
  2427.     xor    eax,eax
  2428.     mov    edx,0x100000*16
  2429.     pop    ecx                                   ; get gdt of tss
  2430.     cmp    ecx,1
  2431.     je     os_mem
  2432.     shl    ecx,8
  2433.     mov    edx,[SLOT_BASE+ecx+APPDATA.mem_size] ;0x8c
  2434.     mov    eax,std_application_base_address
  2435.     ; eax run base -> edx used memory
  2436.   os_mem:
  2437.     dec    edx
  2438.     mov    [ebx+12],eax
  2439.     mov    [ebx+16],edx
  2440.  
  2441.     ; PID (+30)
  2442.  
  2443.     mov    eax,[esp]
  2444.     shl    eax,5
  2445.     add    eax,CURRENT_TASK+TASKDATA.pid
  2446.     mov    eax,[eax]
  2447.     mov    [ebx+20],eax
  2448.  
  2449.     ; window position and size
  2450.  
  2451.     mov    esi,[esp]
  2452.     shl    esi,5
  2453.     add    esi,window_data + WDATA.box
  2454.     mov    edi,[esp+4]
  2455.     add    edi,34
  2456.     mov    ecx,4
  2457.     cld
  2458.     rep    movsd
  2459.  
  2460.     ; Process state (+50)
  2461.  
  2462.     mov    eax,[esp]
  2463.     shl    eax,5
  2464.     add    eax,CURRENT_TASK+TASKDATA.state
  2465.     mov    eax,[eax]
  2466.     mov    [ebx+40],ax
  2467.  
  2468.     ; Window client area box
  2469.  
  2470.     mov    esi,[esp]
  2471.     shl    esi,8
  2472.     add    esi,SLOT_BASE+APPDATA.wnd_clientbox
  2473.     lea    edi,[ebx+44]
  2474.     mov    ecx,4
  2475.     rep    movsd
  2476.  
  2477.     ; Window state
  2478.  
  2479.     mov    esi,[esp]
  2480.     shl    esi,5
  2481.     add    esi,window_data + WDATA.box
  2482.     mov    al,[esi+WDATA.fl_wstate]
  2483.     mov    [edi],al
  2484.  
  2485.     pop    ebx
  2486.     pop    eax
  2487.  
  2488.     ; return number of processes
  2489.  
  2490.     mov    eax,[TASK_COUNT]
  2491.     mov    [esp+36],eax
  2492.     ret
  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.