]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_dbg.c
refresh wwww
[16.git] / src / lib / 16_dbg.c
1
2 #include "src/16.h"
3
4 // TODO: Could we also provide a build mode to emit debug to the "Bochs E9 hack?"
5 #ifdef DEBUGSERIAL
6 # include <stdarg.h>
7 # include <stdlib.h>
8 # include <stdio.h>
9
10 unsigned char _DEBUG_INITed = 0;
11 struct info_8250 *_DEBUG_uart = NULL;
12
13 int _DEBUG_INIT() {
14         if (!_DEBUG_INITed) {
15                 unsigned int i;
16                 uint16_t port;
17
18                 if (!init_8250()) return 0;
19
20                 // what does the BIOS say the serial ports are?
21                 probe_8250_bios_ports();
22                 for (i=0;i < bios_8250_ports;i++) {
23                         port = get_8250_bios_port(i);
24                         if (port == 0) continue;
25                         probe_8250(port);
26                 }
27
28                 // what about the standard serial ports?
29                 for (i=0;i < (sizeof(standard_8250_ports)/sizeof(standard_8250_ports[0]));i++) {
30                         port = standard_8250_ports[i];
31                         if (port == 0) continue;
32                         probe_8250(port);
33                 }
34
35                 // pick the first port, which is probably COM1
36                 if (base_8250_ports == 0) return 0; // FIXME: You know "base_8250_ports" is probably a bad variable name for the max entries in info_8250_port[]
37                 _DEBUG_uart = &info_8250_port[0];
38                 _DEBUG_INITed = 1;
39
40                 // init the COM port.
41                 // in DOSBox-X, the "log" mode will receive our text and print it into the log file
42                 // on real hardware, our text will likely go over a null modem cable to another PC running a serial terminal program like PuTTY or minicom.
43                 // if nothing is connected, then the bytes go off into the ether to get lost and life goes on.
44                 uart_8250_enable_interrupt(_DEBUG_uart,0);      // disable interrupts
45                 uart_8250_set_FIFO(_DEBUG_uart,0x07);           // enable FIFO (why not?), also clear xmit/recv FIFO buffers, set threshhold to 1 byte
46                 uart_8250_set_MCR(_DEBUG_uart,3);               // RTS and DTS on
47                 uart_8250_set_line_control(_DEBUG_uart,UART_8250_LCR_8BIT | UART_8250_LCR_PARITY); // 8 bit 1 stop bit odd parity
48                 uart_8250_set_baudrate(_DEBUG_uart,uart_8250_baud_to_divisor(_DEBUG_uart,9600)); // 9600 baud
49         }
50
51         return _DEBUG_INITed;
52 }
53
54 void _DEBUG(const char *msg) {
55         if (_DEBUG_uart != NULL) {
56                 char c;
57
58                 while ((c=(*msg++)) != 0/*NUL*/) {
59                         while (!uart_8250_can_write(_DEBUG_uart)); // wait for the UART to indicate readiness for our output
60                         uart_8250_write(_DEBUG_uart,(uint8_t)c); // then write it
61                 }
62         }
63 }
64
65 static char _DEBUGF_TMP[256];
66
67 void _DEBUGF(const char *fmt,...) {
68         va_list va;
69
70         va_start(va,fmt);
71         vsnprintf(_DEBUGF_TMP,sizeof(_DEBUGF_TMP),fmt,va);
72         _DEBUG(_DEBUGF_TMP);
73         va_end(va);
74 }
75 #endif
76