Subversion Repositories Kolibri OS

Rev

Rev 1689 | Rev 1710 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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