OSDN Git Service

updating copy left. i am super fuckin busy with school, and trying to get life back...
[proj16/16.git] / src / lib / 16_dbg_1.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2019 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
3  *\r
4  * This file is part of Project 16.\r
5  *\r
6  * Project 16 is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Project 16 is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or\r
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
19  * Fifth Floor, Boston, MA 02110-1301 USA.\r
20  *\r
21  */\r
22 \r
23 #include "src/lib/16_dbg.h"\r
24 \r
25 // TODO: Could we also provide a build mode to emit debug to the "Bochs E9 hack?"\r
26 #ifdef DEBUGSERIAL\r
27 # include <stdarg.h>\r
28 # include <stdlib.h>\r
29 # include <stdio.h>\r
30 \r
31 unsigned char _DEBUG_INITed = 0;\r
32 struct info_8250 *_DEBUG_uart = NULL;\r
33 \r
34 int _DEBUG_INIT() {\r
35         if (!_DEBUG_INITed) {\r
36                 unsigned int i;\r
37                 uint16_t port;\r
38 \r
39                 if (!init_8250()) return 0;\r
40 \r
41                 // what does the BIOS say the serial ports are?\r
42                 probe_8250_bios_ports();\r
43                 for (i=0;i < bios_8250_ports;i++) {\r
44                         port = get_8250_bios_port(i);\r
45                         if (port == 0) continue;\r
46                         probe_8250(port);\r
47                 }\r
48 \r
49                 // what about the standard serial ports?\r
50                 for (i=0;i < (sizeof(standard_8250_ports)/sizeof(standard_8250_ports[0]));i++) {\r
51                         port = standard_8250_ports[i];\r
52                         if (port == 0) continue;\r
53                         probe_8250(port);\r
54                 }\r
55 \r
56                 // pick the first port, which is probably COM1\r
57                 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[]\r
58                 _DEBUG_uart = &info_8250_port[0];\r
59                 _DEBUG_INITed = 1;\r
60 \r
61                 // init the COM port.\r
62                 // in DOSBox-X, the "log" mode will receive our text and print it into the log file\r
63                 // 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.\r
64                 // if nothing is connected, then the bytes go off into the ether to get lost and life goes on.\r
65                 uart_8250_enable_interrupt(_DEBUG_uart,0);      // disable interrupts\r
66                 uart_8250_set_FIFO(_DEBUG_uart,0x07);           // enable FIFO (why not?), also clear xmit/recv FIFO buffers, set threshhold to 1 byte\r
67                 uart_8250_set_MCR(_DEBUG_uart,3);               // RTS and DTS on\r
68                 uart_8250_set_line_control(_DEBUG_uart,UART_8250_LCR_8BIT | UART_8250_LCR_PARITY); // 8 bit 1 stop bit odd parity\r
69                 uart_8250_set_baudrate(_DEBUG_uart,uart_8250_baud_to_divisor(_DEBUG_uart,9600)); // 9600 baud\r
70         }\r
71 \r
72         return _DEBUG_INITed;\r
73 }\r
74 \r
75 void _DEBUG(const char *msg) {\r
76         if (_DEBUG_uart != NULL) {\r
77                 char c;\r
78 \r
79                 while ((c=(*msg++)) != 0/*NUL*/) {\r
80                         while (!uart_8250_can_write(_DEBUG_uart)); // wait for the UART to indicate readiness for our output\r
81                         uart_8250_write(_DEBUG_uart,(uint8_t)c); // then write it\r
82                 }\r
83         }\r
84 }\r
85 \r
86 static char _DEBUGF_TMP[256];\r
87 \r
88 void _DEBUGF(const char *fmt,...) {\r
89         va_list va;\r
90 \r
91         va_start(va,fmt);\r
92         vsnprintf(_DEBUGF_TMP,sizeof(_DEBUGF_TMP),fmt,va);\r
93         _DEBUG(_DEBUGF_TMP);\r
94         va_end(va);\r
95 }\r
96 #endif  //serial\r