Subversion Repositories Kolibri OS

Rev

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