; core kernel functionality
bits 16
%define xcore
global init

; mkisr :: vector1, vector2, ... -> *ISRs
%macro mkisr 1-*
    %assign args %0
    %rep args
        isr%1:
            ; 16 bytes (incl. pad)
            times 4 nop
            xor ax,ax
            push ax
            mov byte al,%1
            push ax
            jmp isrstub
        
        %assign alignment   ($-isr%1)
        %assign padding     (16-alignment)
            times padding nop
        %undef padding
        %undef alignment

        %rotate -1
    %endrep
%endmacro

%define isr2addr(x) _isr2addr x         ; isr2addr :: int8 vector -> int16 *addr
%define show(s,a)   _show s,a           ; show :: idtentry *seg:addr -> void
; %define showidt()   _showidt
%define showidt(s)  _showidt s          ; showidt :: int16 seg? -> void

%macro _isr2addr 1
    ; ax =
        multiply(%1,16)
    
    add ax,isr0
%endmacro

%macro _show 2
    mov ax,%2
    push ax
    mov ax,%1
    push ax
    call __show
    add sp,4
    clc
%endmacro

; %macro _showidt 0
;     push bx

;     xor ax,ax
;     push ax
;     chseg(ds,csselector)
;         xor bx,bx
;         mov word ax,ds:[bx+confseg.idtseg]
;         mov bx,sp
;         mov word ss:[bx+2],ax
;     sret(ds)

;     mov bx,sp
;     mov word ax,ss:[bx+0]
;     _showidt ax

;     pop ax
;     pop bx
; %endmacro
    
%macro _showidt 1
    mov ax,%1
    push ax
    call __showidt
    add sp,2
    clc
%endmacro

section .modhdr
    init: jmp _init
    name: dw _name

section .data
    _name: db "kernel",0x00

section .text
        %define VersionMajor    0
        %define VersionMinor    8
    
        %include "std"
        %include "printf"
        %include "memory"
        %include "confseg"

        %include "core.s"
        %include "interrupt.s"
