Subversion Repositories Kolibri OS

Rev

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