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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
//! Serial Write Operations
const common = @import("../../../common/common.zig");
const asm_io = @import("../../asm/io/io.zig");
const serial_constants = common.constants.serial;
const ports = serial_constants.ports;
const registers = serial_constants.registers;
var current_port: u16 = ports.default_port;
pub fn set_port(port: u16) void {
current_port = port;
}
fn is_transmit_empty(port: u16) bool {
return (asm_io.read_byte(port + registers.line_status_register) & registers.line_status_transmit_empty) != 0;
}
fn write_char(port: u16, char: u8) void {
while (!is_transmit_empty(port)) {
asm_io.io_wait();
}
asm_io.write_byte(port + registers.data_register, char);
}
pub fn printf(comptime fmt: []const u8, args: anytype) void {
const ArgsType = @TypeOf(args);
const fields = @typeInfo(ArgsType).@"struct".fields;
comptime var i: usize = 0;
comptime var arg_index: usize = 0;
inline while (i < fmt.len) {
if (fmt[i] == '%' and i + 1 < fmt.len) {
switch (fmt[i + 1]) {
's' => {
const str = @field(args, fields[arg_index].name);
for (str) |c| {
if (c == '\n') write_char(current_port, '\r');
write_char(current_port, c);
}
arg_index += 1;
i += 2;
},
'd' => {
const val = @field(args, fields[arg_index].name);
print_decimal_value(@intCast(val));
arg_index += 1;
i += 2;
},
'x' => {
const val = @field(args, fields[arg_index].name);
print_hex_value(@intCast(val));
arg_index += 1;
i += 2;
},
'%' => {
write_char(current_port, '%');
i += 2;
},
else => {
write_char(current_port, fmt[i]);
i += 1;
},
}
} else {
if (fmt[i] == '\n') write_char(current_port, '\r');
write_char(current_port, fmt[i]);
i += 1;
}
}
}
fn print_decimal_value(value: u64) void {
if (value == 0) {
write_char(current_port, '0');
return;
}
var buffer: [20]u8 = undefined;
var temp = value;
var len: usize = 0;
while (temp > 0) {
buffer[len] = @truncate((temp % 10) + '0');
temp /= 10;
len += 1;
}
while (len > 0) {
len -= 1;
write_char(current_port, buffer[len]);
}
}
fn print_hex_value(value: u64) void {
const hex = "0123456789abcdef";
write_char(current_port, '0');
write_char(current_port, 'x');
var started = false;
var shift: u6 = 60;
while (true) {
const nibble: u4 = @truncate((value >> shift) & 0xF);
if (nibble != 0 or started or shift == 0) {
write_char(current_port, hex[nibble]);
started = true;
}
if (shift == 0) break;
shift -= 4;
}
}
|