SEGMENT .text USE32
[BITS 32]


GLOBAL isr_00, isr_01, isr_02, isr_03, isr_04, isr_05, isr_06, isr_07
GLOBAL isr_08, isr_09, isr_0A, isr_0B, isr_0C, isr_0D, isr_0E, isr_0F
GLOBAL isr_10, isr_11, isr_12, isr_13, isr_14, isr_15, isr_16, isr_17
GLOBAL isr_18, isr_19, isr_1A, isr_1B, isr_1C, isr_1D, isr_1E, isr_1F
GLOBAL isr_20, isr_21

GLOBAL exc_with_error_code

EXTERN	exception_handler, key_handler, timer_handler, scheduler
EXTERN	process_table, current_process
EXTERN	system_clock	

%macro WRAPPER 2

%1:
				push	ebp
				mov		ebp, esp
				push	ax
				mov		ax, %2
				jmp		wrapper_code
%endmacro

wrapper_code:
				
				pushad
				push	gs
				push	fs
				push	ds
				push 	es

				mov		bx, 0x10
				mov		ds, bx
				mov		es, bx
				xor 	ebx,ebx
				mov		bx, ax
				cmp		BYTE [exc_with_error_code+ebx], 0
				je		exception_without_error_code

				push	DWORD [ss:ebp+4]  							; error code
				push	DWORD [ss:ebp+8]							; eip
				push	DWORD [ss:ebp+12]							; cs

				push	eax
				call	exception_handler
				add		esp, 0x10 

				pop		es
				pop		ds
				pop		fs
				pop		gs
				popad
				pop		ax
				pop		ebp
				add		esp,0x04
				iretd


exception_without_error_code:

				push 	DWORD 0												;error code
				push	DWORD [ss:ebp+4]							;eip
				push  DWORD [ss:ebp+8]							;cs
			
				push	eax
				call	exception_handler
				add		esp, 0x10 

				pop		es
				pop		ds
				pop		fs
				pop		gs
				popad
				pop		ax
				pop		ebp

				iretd



WRAPPER	isr_00, 0x00
WRAPPER	isr_01, 0x01
WRAPPER	isr_02, 0x02
WRAPPER	isr_03, 0x03
WRAPPER	isr_04, 0x04
WRAPPER	isr_05, 0x05
WRAPPER	isr_06, 0x06
WRAPPER	isr_07, 0x07
WRAPPER	isr_08, 0x08
WRAPPER	isr_09, 0x09
WRAPPER	isr_0A, 0x0A
WRAPPER	isr_0B, 0x0B
WRAPPER	isr_0C, 0x0C
WRAPPER	isr_0D, 0x0D
WRAPPER	isr_0E, 0x0E
WRAPPER	isr_0F, 0x0F
WRAPPER	isr_10, 0x10
WRAPPER	isr_11, 0x11
WRAPPER	isr_12, 0x12
WRAPPER	isr_13, 0x13
WRAPPER	isr_14, 0x14
WRAPPER	isr_15, 0x15
WRAPPER	isr_16, 0x16
WRAPPER	isr_17, 0x17
WRAPPER	isr_18, 0x18
WRAPPER	isr_19, 0x19
WRAPPER	isr_1A, 0x1A
WRAPPER	isr_1B, 0x1B
WRAPPER	isr_1C, 0x1C
WRAPPER	isr_1D, 0x1D
WRAPPER	isr_1E, 0x1E
WRAPPER	isr_1F, 0x1F

;/* timer interrupt wrapper */
isr_20:
				
				push	gs
				push	fs
				push	es
				push	ds
				pushad

				mov		ax, 0x10
				mov		ds, ax
				mov		es,	ax
				
				call	timer_handler
				
				mov		ebp, esp
				push	ebp
				call	scheduler
				add		esp, 4
				
				popad
				pop		ds
				pop		es
				pop		fs
				pop		gs
				iretd

;/* keyboard interrupt wrapper */
isr_21:
				pushad
				push	ds
				push	es
				push	fs
				push	gs

				mov		ax, 0x10
				mov		ds, ax
				mov		es,	ax
				call	key_handler
				
				mov		al, 0x20		;send EndOfInterrupt to PIC 8259M
				out		0x20, al

				pop		gs
				pop		fs
				pop		es
				pop		ds
				popad
				iretd

SEGMENT	.data

exc_with_error_code	DB	0, 0, 0, 0, 0, 0, 0, 0,  1, 0, 1, 1, 1, 1, 1, 0		;only exceptions 8 and 10 to 14 has error code
										DB  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0		;on stack


