Skip to content

Commit 7f336c6

Browse files
author
Ian Seyler
committed
Serial commit
1 parent 068deae commit 7f336c6

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed

src/init/serial.asm

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
; =============================================================================
2+
; Pure64 -- a 64-bit OS/software loader written in Assembly for x86-64 systems
3+
; Copyright (C) 2008-2025 Return Infinity -- see LICENSE.TXT
4+
;
5+
; INIT SERIAL - 115200 bps, 8N1 (8 data bits, no parity, and 1 stop bit)
6+
; =============================================================================
7+
8+
9+
init_serial:
10+
; Disable Interrupts
11+
mov dx, COM_PORT_INTERRUPT_ENABLE
12+
mov al, 0 ; Disable all interrupts
13+
out dx, al
14+
15+
; Enable divisor register for setting baud rate
16+
mov dx, COM_PORT_LINE_CONTROL
17+
mov dl, 0x80 ; DLB (7 set)
18+
out dx, al
19+
20+
; Send the divisor (baud rate will be 115200 / divisor)
21+
mov dx, COM_PORT_DATA
22+
mov ax, BAUD_115200
23+
out dx, al
24+
mov dx, COM_PORT_DATA+1
25+
shr ax, 8
26+
out dx, al
27+
28+
; Disable divisor register and set values
29+
mov dx, COM_PORT_LINE_CONTROL
30+
mov al, 00000111b ; 8 data bits (0-1 set), one stop bit (2 set), no parity (3-5 clear), DLB (7 clear)
31+
out dx, al
32+
33+
; Disable modem control
34+
mov dx, COM_PORT_MODEM_CONTROL
35+
mov al, 0
36+
out dx, al
37+
38+
; Set FIFO
39+
mov dx, COM_PORT_FIFO_CONTROL
40+
mov al, 0xC7 ; Enable FIFO, clear them, 14-byte threshold
41+
out dx, al
42+
43+
ret
44+
45+
46+
; Port Registers
47+
COM_BASE equ 0x3F8
48+
COM_PORT_DATA equ COM_BASE + 0
49+
COM_PORT_INTERRUPT_ENABLE equ COM_BASE + 1
50+
COM_PORT_FIFO_CONTROL equ COM_BASE + 2
51+
COM_PORT_LINE_CONTROL equ COM_BASE + 3
52+
COM_PORT_MODEM_CONTROL equ COM_BASE + 4
53+
COM_PORT_LINE_STATUS equ COM_BASE + 5
54+
COM_PORT_MODEM_STATUS equ COM_BASE + 6
55+
COM_PORT_SCRATCH_REGISTER equ COM_BASE + 7
56+
57+
; Baud Rates
58+
BAUD_115200 equ 1
59+
BAUD_57600 equ 2
60+
BAUD_9600 equ 12
61+
BAUD_300 equ 384
62+
63+
64+
; =============================================================================
65+
; EOF

src/pure64.asm

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,25 @@ start64:
260260
mov ebx, 0
261261
call debug_block
262262

263+
; Configure serial port @ 0x03F8 as 115200 8N1
264+
call init_serial
265+
266+
mov rsi, msg_pure64 ; Output "[ Pure64 ]"
267+
call debug_msg
268+
269+
; Output boot method
270+
mov rsi, msg_boot
271+
call debug_msg
272+
cmp byte [p_BootMode], 'U'
273+
je boot_uefi
274+
mov rsi, msg_bios
275+
call debug_msg
276+
jmp msg_boot_done
277+
boot_uefi:
278+
mov rsi, msg_uefi
279+
call debug_msg
280+
msg_boot_done:
281+
263282
; Clear out the first 20KiB of memory. This will store the 64-bit IDT, GDT, PML4, PDP Low, and PDP High
264283
mov ecx, 5120
265284
xor eax, eax
@@ -716,25 +735,41 @@ pde_end:
716735
mov ebx, 4
717736
call debug_block
718737

738+
mov rsi, msg_acpi
739+
call debug_msg
719740
call init_acpi ; Find and process the ACPI tables
741+
mov rsi, msg_ok
742+
call debug_msg
720743

721744
; Visual Debug (4/8)
722745
mov ebx, 6
723746
call debug_block
724747

748+
mov rsi, msg_bsp
749+
call debug_msg
725750
call init_cpu ; Configure the BSP CPU
751+
mov rsi, msg_ok
752+
call debug_msg
726753

727754
; Visual Debug (5/8)
728755
mov ebx, 8
729756
call debug_block
730757
758+
mov rsi, msg_hpet
759+
call debug_msg
731760
call init_hpet ; Configure the HPET
761+
mov rsi, msg_ok
762+
call debug_msg
732763

733764
; Visual Debug (6/8)
734765
mov ebx, 10
735766
call debug_block
736767

768+
mov rsi, msg_smp
769+
call debug_msg
737770
call init_smp ; Init of SMP, deactivate interrupts
771+
mov rsi, msg_ok
772+
call debug_msg
738773

739774
; Reset the stack to the proper location (was set to 0x8000 previously)
740775
mov rsi, [p_LocalAPICAddress] ; We would call p_smp_get_id here but the stack is not ...
@@ -887,6 +922,9 @@ lfb_wc_end:
887922
mov ebx, 14
888923
call debug_block
889924

925+
mov rsi, msg_kernel
926+
call debug_msg
927+
890928
%ifdef BIOS
891929
cmp byte [p_BootDisk], 'F' ; Check if sys is booted from floppy?
892930
jnz clear_regs
@@ -916,6 +954,7 @@ clear_regs:
916954
%include "init/acpi.asm"
917955
%include "init/cpu.asm"
918956
%include "init/hpet.asm"
957+
%include "init/serial.asm"
919958
%include "init/smp.asm"
920959
%ifdef BIOS
921960
%include "fdc/dma.asm"
@@ -1010,6 +1049,106 @@ debug_progressbar:
10101049
%endif
10111050

10121051

1052+
; -----------------------------------------------------------------------------
1053+
; debug_msg_char - Send a single char via the serial port
1054+
; IN: AL = Byte to send
1055+
debug_msg_char:
1056+
pushf
1057+
push rdx
1058+
push rax ; Save the byte
1059+
mov dx, 0x03F8 ; Address of first serial port
1060+
debug_msg_char_wait:
1061+
add dx, 5 ; Offset to Line Status Register
1062+
in al, dx
1063+
sub dx, 5 ; Back to to base
1064+
and al, 0x20
1065+
cmp al, 0
1066+
je debug_msg_char_wait
1067+
pop rax ; Restore the byte
1068+
out dx, al ; Send the char to the serial port
1069+
debug_msg_char_done:
1070+
pop rdx
1071+
popf
1072+
ret
1073+
; -----------------------------------------------------------------------------
1074+
1075+
1076+
; -----------------------------------------------------------------------------
1077+
; debug_msg_char - Send a message via the serial port
1078+
; IN: RSI = Location of message
1079+
debug_msg:
1080+
pushf
1081+
push rdx
1082+
push rax
1083+
cld ; Clear the direction flag.. we want to increment through the string
1084+
mov dx, 0x03F8 ; Address of first serial port
1085+
debug_msg_next:
1086+
add dx, 5 ; Offset to Line Status Register
1087+
in al, dx
1088+
sub dx, 5 ; Back to to base
1089+
and al, 0x20
1090+
cmp al, 0
1091+
je debug_msg_next
1092+
lodsb ; Get char from string and store in AL
1093+
cmp al, 0
1094+
je debug_msg_done
1095+
out dx, al ; Send the char to the serial port
1096+
jmp debug_msg_next
1097+
debug_msg_done:
1098+
pop rax
1099+
pop rdx
1100+
popf
1101+
ret
1102+
; -----------------------------------------------------------------------------
1103+
1104+
1105+
; -----------------------------------------------------------------------------
1106+
; debug_dump_(rax|eax|ax|al) -- Dump content of RAX, EAX, AX, or AL
1107+
; IN: RAX/EAX/AX/AL = content to dump
1108+
; OUT: Nothing, all registers preserved
1109+
debug_dump_rax:
1110+
rol rax, 8
1111+
call debug_dump_al
1112+
rol rax, 8
1113+
call debug_dump_al
1114+
rol rax, 8
1115+
call debug_dump_al
1116+
rol rax, 8
1117+
call debug_dump_al
1118+
rol rax, 32
1119+
debug_dump_eax: ; RAX is used here instead of EAX to preserve the upper 32-bits
1120+
rol rax, 40
1121+
call debug_dump_al
1122+
rol rax, 8
1123+
call debug_dump_al
1124+
rol rax, 16
1125+
debug_dump_ax:
1126+
rol ax, 8
1127+
call debug_dump_al
1128+
rol ax, 8
1129+
debug_dump_al:
1130+
push rax ; Save RAX
1131+
push ax ; Save AX for the low nibble
1132+
shr al, 4 ; Shift the high 4 bits into the low 4, high bits cleared
1133+
or al, '0' ; Add "0"
1134+
cmp al, '9'+1 ; Digit?
1135+
jl debug_dump_al_h ; Yes, store it
1136+
add al, 7 ; Add offset for character "A"
1137+
debug_dump_al_h:
1138+
call debug_msg_char
1139+
pop ax ; Restore AX
1140+
and al, 0x0F ; Keep only the low 4 bits
1141+
or al, '0' ; Add "0"
1142+
cmp al, '9'+1 ; Digit?
1143+
jl debug_dump_al_l ; Yes, store it
1144+
add al, 7 ; Add offset for character "A"
1145+
debug_dump_al_l:
1146+
call debug_msg_char
1147+
pop rax ; Restore RAX
1148+
ret
1149+
; -----------------------------------------------------------------------------
1150+
1151+
10131152
EOF:
10141153
db 0xDE, 0xAD, 0xC0, 0xDE
10151154

src/sysvar.asm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66
; =============================================================================
77

88

9+
; Messages
10+
msg_pure64: db 13, 10, 13, 10, '[ Pure64 ]', 0
11+
msg_ok: db 'ok', 0
12+
msg_error: db 'ERROR', 0
13+
msg_exception: db 'EXCEPTION 0x', 0
14+
msg_pml4: db 13, 10, 'pml4 ', 0
15+
msg_boot: db 13, 10, 'boot ', 0
16+
msg_bios: db 'bios', 0
17+
msg_uefi: db 'uefi', 0
18+
msg_acpi: db 13, 10, 'acpi ', 0
19+
msg_bsp: db 13, 10, 'bsp ', 0
20+
msg_hpet: db 13, 10, 'hpet ', 0
21+
msg_smp: db 13, 10, 'smp ', 0
22+
msg_kernel: db 13, 10, 'kernel start', 13, 10, 0
23+
924
;CONFIG
1025
cfg_smpinit: db 1 ; By default SMP is enabled. Set to 0 to disable.
1126

0 commit comments

Comments
 (0)