SayoriOS 0.3.3

ports.c
См. документацию.
1
10 #include <stdarg.h>
11 #include <io/ports.h>
12 #include <lib/sprintf.h>
13 #include "io/serial_port.h"
14 #include "sys/scheduler.h"
15 #include "mem/vmm.h"
16
17 void (*default_qemu_printf)(const char *text, ...) = qemu_printf;
18
19 void switch_qemu_logging() {
20 default_qemu_printf = new_qemu_printf;
21 }
22
30 void insl(uint16_t port, uint32_t *buffer, int32_t times) {
31 for (uint32_t index = 0; index < times; index++) {
32 buffer[index] = inl(port);
33 }
34 }
35
36
44 void outsl(uint16_t port, uint32_t *buffer, int32_t times) {
45 for (int32_t index = 0; index < times; index++) {
46 outl(port, buffer[index]);
47 }
48 }
49
50 void insw(uint16_t __port, void *__buf, unsigned long __n) {
51 __asm__ volatile("cld; rep; insw"
52 : "+D"(__buf), "+c"(__n)
53 : "d"(__port));
54 }
55
56 void outsw(uint16_t __port, const void *__buf, unsigned long __n) {
57 __asm__ volatile("cld; rep; outsw"
58 : "+S"(__buf), "+c"(__n)
59 : "d"(__port));
60 }
61
67 int32_t is_transmit_empty(uint16_t port) {
68 return inb(port + 5) & 0x20;
69 }
70
71 // Read 1 byte (char) from port.
72 uint8_t serial_readchar(uint16_t port) {
73 //size_t to = 0;
74 while (is_signal_received(port) == 0){
75 //to++;
76 //qemu_warn("TIMEOUT: %d",to);
77 }
78 return inb(port);
79 }
80
81
82 // Read 1 byte (char) from port.
83 int8_t serial_readchar_timeout(uint16_t port,size_t timeout, bool Alert) {
84 size_t to = 0;
85 while (is_signal_received(port) == 0){
86 to++;
87 //qemu_warn("TIMEOUT: %d",to);
88 if (to >= timeout){
89 if (Alert) qemu_warn("TIMEOUT: %d",to);
90 return -1;
91 }
92 }
93 return inb(port);
94 }
95
99 void io_wait(void) {
100 outb(0x80, 0);
101 }
102
109 int isprint(char c) {
110 return ((c >= ' ' && c <= '~') ? 1 : 0);
111 }
112
113
120 int is_com_port(int port) {
121 switch (port) {
122 case PORT_COM1:
123 return 1;
124 case PORT_COM2:
125 return 2;
126 case PORT_COM3:
127 return 3;
128 case PORT_COM4:
129 return 4;
130 case PORT_COM5:
131 return 5;
132 case PORT_COM6:
133 return 6;
134 case PORT_COM7:
135 return 7;
136 case PORT_COM8:
137 return 8;
138 default:
139 return 0;
140 }
141 }
142
149 void qemu_printf(const char *text, ...) {
150 va_list args;
151 va_start(args, text);
152
153 if (__com_getInit(1)) {
154 scheduler_mode(false); // Stop scheduler
155
156 __com_pre_formatString(PORT_COM1, text, args);
157
158 scheduler_mode(true); // Start scheduler
159 }
160
161 va_end(args);
162 }
163
164 void new_qemu_printf(const char *format, ...) {
165 if (!__com_getInit(1))
166 return;
167
168 va_list args;
169 va_start(args, format);
170
171 char* container;
172
173 vasprintf(&container, format, args);
174
175 va_end(args);
176
177 scheduler_mode(false); // Stop scheduler
178 __com_writeString(PORT_COM1, container);
179 scheduler_mode(true); // Start scheduler
180
181 kfree(container);
182 }
qemu_printf
void qemu_printf(const char *text,...)
Вывод QEMU через COM1 информации
Definition: ports.c:149
is_transmit_empty
int32_t is_transmit_empty(uint16_t port)
Проверка занятости порта
Definition: ports.c:67
outsl
void outsl(uint16_t port, uint32_t *buffer, int32_t times)
Запись длинного слова через порт
Definition: ports.c:44
insl
void insl(uint16_t port, uint32_t *buffer, int32_t times)
Чтение длинного слова через порт
Definition: ports.c:30
is_com_port
int is_com_port(int port)
Проверка на тип порта
Definition: ports.c:120
io_wait
void io_wait(void)
Небольшая задержка используя порт 128(0x80)
Definition: ports.c:99
isprint
int isprint(char c)
Проверка, читаем ли символ
Definition: ports.c:109
va_list
Definition: stdarg.h:9