Subversion Repositories Kolibri OS

Rev

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