summaryrefslogtreecommitdiff
path: root/mem.asm
blob: c99660eb337b2a0f15127ba8050f810cd46d2a25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
%ifndef __MEM_ASM
%define __MEM_ASM

check_a20:
	pushf				; preserve flags
	push ds				; data segment
	push es				; extra segment
	push di				; destination index
	push si				; source index

	cli				; disable interrupts

	xor ax, ax			; zero out ax
	mov es, ax			; zero out extra segment

	not ax				; flip all bits in ax
	mov ds, ax

	mov di, 0x0500			; destination address
	mov si, 0x0510			; source address

	mov al, byte [es:di]		; copy byte from destination (FFFF:0500)
	push ax

	mov al, byte [ds:si]		; copy byte from source (FFFF:0510)
	push ax

	mov byte [es:di], 0x00		; set es:di to 0
	mov byte [ds:si], 0xFF		; set es:si to 255

	cmp byte [es:di], 0xFF		; did it wrap around?

	pop ax
	mov byte [ds:si], al

	pop ax
	mov byte [es:di], al

	mov ax, 0
	je check_a20__exit

	mov ax, 1

check_a20__exit:
	sti				; enable interrupts
	pop si				; restore registers
	pop di
	pop es
	pop ds
	popf				; restore flags

	ret


enable_a20:				; fast A20
	cli				; disble interrupts
	call check_a20			; is A20 already enabled?
	cmp al, 1
	je .no_a20

	in al, 0x92			; enable A20
	test al, 2
	jnz .return
	or al, 2
	and al, 0xFE
	out 0x92, al
	jmp .return
.no_a20:
	stc				; set carry on failure
.return:
	sti				; enable interrupts
	clc				; clear carry
	ret

%endif