2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023 John Tsiombikas <nuclear@mutantstargoat.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
23 #if defined(__MSDOS__) || defined(MSDOS)
24 static int setup_serial(int sdev);
29 enum { LOG_FILE, LOG_STREAM, LOG_CON, LOG_CB };
30 enum { LOG_DBG, LOG_INFO, LOG_WARN, LOG_ERR };
33 void (*func)(const char*, void*);
42 struct log_callback cb;
47 static struct log_output outputs[MAX_OUTPUTS];
48 static int num_outputs;
50 void init_logger(void)
55 void cleanup_logger(void)
59 for(i=0; i<num_outputs; i++) {
60 if(outputs[i].type == LOG_FILE) {
61 fclose(outputs[i].out.fp);
67 int add_log_file(const char *fname)
72 if(num_outputs >= MAX_OUTPUTS) {
75 if(!(fp = fopen(fname, "w"))) {
80 outputs[idx].type = LOG_FILE;
81 outputs[idx].out.fp = fp;
85 int add_log_stream(FILE *fp)
89 if(num_outputs >= MAX_OUTPUTS) {
94 outputs[idx].type = LOG_STREAM;
95 outputs[idx].out.fp = fp;
99 int add_log_console(const char *devname)
101 #if defined(MSDOS) || defined(__MSDOS__)
103 if(sscanf(devname, "COM%d", &comport) != 1 || comport < 1 || comport > 2) {
108 if(num_outputs >= MAX_OUTPUTS) {
111 for(i=0; i<num_outputs; i++) {
112 if(outputs[i].type == LOG_CON && outputs[i].out.con == comport) {
116 if(setup_serial(comport) == -1) {
121 outputs[i].type = LOG_CON;
122 outputs[i].out.con = comport;
125 #elif defined(unix) || defined(__unix__)
131 int add_log_callback(void (*cbfunc)(const char*, void*), void *cls)
135 if(num_outputs >= MAX_OUTPUTS) {
140 outputs[idx].type = LOG_CB;
141 outputs[idx].out.cb.func = cbfunc;
142 outputs[idx].out.cb.cls = cls;
146 #if defined(__WATCOMC__)
148 #define vsnprintf _vsnprintf
152 static void logmsg(int type, const char *fmt, va_list ap)
159 while((ret = vsnprintf(buf, bufsz, fmt, ap)) > bufsz || ret < 0) {
163 newsz = bufsz ? bufsz * 2 : 256;
165 if(!(tmp = realloc(buf, newsz))) {
177 for(i=0; i<num_outputs; i++) {
178 switch(outputs[i].type) {
181 fputs(buf, outputs[i].out.fp);
184 #if defined(MSDOS) || defined(__MSDOS__)
190 outputs[i].out.cb.func(buf, outputs[i].out.cb.cls);
199 void errormsg(const char *fmt, ...)
203 logmsg(LOG_ERR, fmt, ap);
207 void warnmsg(const char *fmt, ...)
211 logmsg(LOG_WARN, fmt, ap);
215 void infomsg(const char *fmt, ...)
219 logmsg(LOG_INFO, fmt, ap);
223 void dbgmsg(const char *fmt, ...)
227 logmsg(LOG_DBG, fmt, ap);
231 void verrormsg(const char *fmt, va_list ap)
233 logmsg(LOG_ERR, fmt, ap);
236 void vwarnmsg(const char *fmt, va_list ap)
238 logmsg(LOG_ERR, fmt, ap);
241 void vinfomsg(const char *fmt, va_list ap)
243 logmsg(LOG_ERR, fmt, ap);
246 void vdbgmsg(const char *fmt, va_list ap)
248 logmsg(LOG_ERR, fmt, ap);
252 #if defined(MSDOS) || defined(__MSDOS__)
253 #define UART1_BASE 0x3f8
254 #define UART2_BASE 0x2f8
264 #define DIV_9600 (115200 / 9600)
265 #define DIV_38400 (115200 / 38400)
266 #define LCTL_8N1 0x03
267 #define LCTL_DLAB 0x80
268 #define FIFO_ENABLE_CLEAR 0x07
269 #define MCTL_DTR_RTS_OUT2 0x0b
270 #define LST_TRIG_EMPTY 0x20
272 static unsigned int iobase;
274 static int setup_serial(int sdev)
276 if(sdev < 0 || sdev > 1) {
279 iobase = sdev == 0 ? UART1_BASE : UART2_BASE;
281 /* set clock divisor */
282 outp(iobase | UART_LCTL, LCTL_DLAB);
283 outp(iobase | UART_DIVLO, DIV_9600 & 0xff);
284 outp(iobase | UART_DIVHI, DIV_9600 >> 8);
286 outp(iobase | UART_LCTL, LCTL_8N1);
287 /* assert RTS and DTR */
288 outp(iobase | UART_MCTL, MCTL_DTR_RTS_OUT2);
292 void ser_putchar(int c)
298 while((inp(iobase | UART_LSTAT) & LST_TRIG_EMPTY) == 0);
299 outp(iobase | UART_DATA, c);
302 void ser_puts(const char *s)
309 void ser_printf(const char *fmt, ...)
315 vsprintf(buf, fmt, ap);