Subversion Repositories Kolibri OS

Rev

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