;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   CPUNAME.ASM for MenuetOS 64-bit
;   Version 0.02
;
;   Compile with FASM 1.60 or above
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

use64

    org   0x0

    db    'MENUET64'              ; Header identifier
    dq    0x01                    ; Version
    dq    START                   ; Start of code
    dq    image_end               ; Size of image
    dq    0x100000                ; Memory for app
    dq    0xffff0                 ; Rsp
    dq    0x00                    ; Prm 
    dq    0x00                    ; Icon


START:

    mov   eax, 0x80000002
    cpuid
    mov   dword [text], eax
    mov   dword [text + 4], ebx
    mov   dword [text + 8], ecx
    mov   dword [text + 12], edx
    mov   eax, 0x80000003
    cpuid
    mov   dword [text + 16], eax
    mov   dword [text + 20], ebx
    mov   dword [text + 24], ecx
    mov   dword [text + 28], edx
    mov   eax, 0x80000004
    cpuid
    mov   dword [text + 32], eax
    mov   dword [text + 36], ebx
    mov   dword [text + 40], ecx
    mov   dword [text + 44], edx

    mov   rax , 141         ; Enable system font
    mov   rbx , 1
    mov   rcx , 1
    mov   rdx , 5 shl 32 + 5
    mov   r8  , 9 shl 32 + 12
    int   0x60

    call  draw_window       ; At first, draw the window

still:

    mov   rax , 10          ; Wait here for event
    int   0x60

    test  rax , 1           ; Window redraw
    jnz   window_event
    test  rax , 2           ; Keyboard press
    jnz   key_event
    test  rax , 4           ; Button press
    jnz   button_event

    jmp   still

window_event:

    call  draw_window
    jmp   still

key_event:

    mov   rax , 2          ; Read the key and ignore
    int   0x60

    jmp   still

button_event:

    mov   rax , 17
    int   0x60

    ; rax = status
    ; rbx = button id

    cmp   rbx , 0x10000001  ; Button - Close application
    jne   no_application_terminate_button
    mov   rax , 0x200
    int   0x60
        
  no_application_terminate_button:

    cmp   rbx, 20
    jne   no_clickme
    mov   rax, 0x200         ; or 512
    int   0x60

  no_clickme:
    ;cmp   rbx , 0x106       ; Menu - Close application
    ;jne   no_application_terminate_menu
    ;mov   rax , 0x200
    ;int   0x60
  no_application_terminate_menu:

    jmp   still


draw_window:    

    mov   rax , 12                           ; Beginning of window draw
    mov   rbx , 1
    int   0x60

    mov   rax , 0                            ; Draw window
    mov   rbx , 256 shl 32 + 378             ; X start & size
    mov   rcx , 128 shl 32 + 128             ; Y start & size
    mov   rdx , 0x0000000000FFFFFF           ; Type    & border color  
    mov   r8  , 0x0000000000000001           ; Flags (set as 1)
    mov   r9  , window_label                 ; 0 or label - asciiz
    mov   r10 , 0                            ; 0 or pointer to menu struct
    int   0x60

    mov   rax , 4                            ; Display text
    mov   rbx , text                         ; Pointer to text
    mov   rcx , 32                           ; X position
    mov   rdx , 64                           ; Y position
    mov   rsi , 0x000000                     ; Color
    mov   r9  , 1                            ; Font
    int   0x60

    ; Define button

    mov   rax , 8
    mov   rbx , 150 shl 32 + 65              ; X start & size
    mov   rcx , 90 shl 32 + 20               ; Y start & size
    mov   rdx , 20  
    mov   r8  , 0
    mov   r9  , button_text
    int   0x60

    mov   rax , 12                           ; End of window draw
    mov   rbx , 2
    int   0x60

    ret

; Data area

window_label:

    db    'CPU NAME',0     ; Window label

text:

    rb     48
    db    0
    
button_text:               ; Button text

    db    'Close',0
    
image_end:

