[BITS 64]
;this manages all of the interrupts, it is meant to handle most things 
%include "sys_const.asm"

%macro save 2-*

%rep %0
%rotate 1
push %1
%rotate 1
%endrep

%endmacro

%macro unsave 2-*
%rep %0
%rotate -1
push %1
%endrep

%endmacro

%macro int_macro 1
[section .ints]
db %1
dq %%start
%%start:
	push rax
	mov rax,%1
	jmp isr
%endmacro

%macro init_idt 2
	dw %1
	dw %2
	db 0x0,10001110
	dw %1 >> 16
	dd %1 >> 32
	dd 0x0
%endmacro

global idt
global idt_end
global kern_panic

extern kernel_page
extern cons_writes
extern panic_fun
extern def_int
extern ints

;the interrupt of no return


%assign i 0
%rep 49
int_macro i
%assign i i+1
%endrep

isr:
	save bx,rcx,rdx,rsi,rdi,rbp,r8,r9,r10,r11,r12,r13,r14,r15
	mov rdi,rsp
	mov rbx,rax
	shl rbx,3
	add rbx,ints
	cmp qword [rbx],0
	je .def
	call near [rbx]
	unsave  rax,rbx,rcx,rdx,rsi,rdi,rbp,r8,r9,r10,r11,r12,r13,r14,r15
	iret

	.def:
	mov rax,def_int
	jmp rax

;def_int:
;jmp misstxt

;panic_txt db 10,'kernel_panic, run away uncaught interrupt',0

;misstxt:
;mov rdi,panic_txt
;mov rax, panic_fun
;call [rax]
;hlt

