From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/mterm.c | 133 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/mterm.c (limited to 'reference/C/CONTRIB/SNIP/mterm.c') diff --git a/reference/C/CONTRIB/SNIP/mterm.c b/reference/C/CONTRIB/SNIP/mterm.c new file mode 100755 index 0000000..b022504 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/mterm.c @@ -0,0 +1,133 @@ +/* MTERM.C - Minimal example PC terminal program. + Released to public domain by author, David Harmon, June 1992. + Intended for use with a FOSSIL driver, but will run with BIOS alone. + I expect you'll want to add something for practical purposes. ;-) +*/ + +#include +#include +#include /* kbhit(), getch(), putch(), etc. */ +#include /* int86(), etc. */ + +#ifdef __ZTC__ + #define cputs(s) fputs((s),stderr) +#endif + +int port = 0; /* 0 = COM1:, 1 = COM2: etc. */ +int local_echo = 0; +int cr_add_lf = 0; +int exiting = 0; + +int init_comm(int flags) +{ + union REGS regs; + + regs.h.ah = 0x04; /* initialize driver (port) */ + regs.x.bx = 0x4f50; + regs.x.dx = port; + int86( 0x14, ®s, ®s); + + regs.h.ah = 0x00; /* set baud rate * port attrs */ + regs.h.al = (unsigned char)flags; + regs.x.dx = port; + int86( 0x14, ®s, ®s); + return regs.h.ah; +} + +void send_char(char ch) +{ + union REGS regs; + + regs.h.ah = 0x01; /* Send char (wait until ready)*/ + regs.h.al = ch; + regs.x.dx = port; + int86( 0x14, ®s, ®s); +} + +int input_ready(void) +{ + union REGS regs; + + regs.h.ah = 0x03; /* Get port status */ + regs.x.dx = port; + int86( 0x14, ®s, ®s); + return ((regs.h.ah & 0x01) != 0); /* input ready */ +} + +int get_char(void) +{ + union REGS regs; + + regs.h.ah = 0x02; /* receive char (wait if necessary)*/ + regs.x.dx = port; + int86( 0x14, ®s, ®s); + return regs.h.al; +} + +void deinit_comm(void) +{ + union REGS regs; + + regs.h.ah = 0x05; /* deinitialize port (pseudo close) */ + regs.h.al = 0x00; /* (lower DTR) */ + regs.x.dx = port; + int86( 0x14, ®s, ®s); +} + +void main(void) +{ + int ch; + + init_comm(0xE3); /* hard coded 0xB3 = 2400,N,8,1 */ + cputs("MTERM ready! Press F1 to exit.\r\n"); + while (!exiting) + { + if (kbhit()) /* key was hit */ + { + ch = getch(); /* Regular ASCII keys are returned as the + ASCII code; function keys, arrows, etc. + as zero followed by a special code + (on next getch.) */ + if (ch != 0) + { + send_char((char)ch); /* to com port */ + if (local_echo) + { + putch(ch); /* to screen */ + + /* add LF to CR? */ + + if (cr_add_lf && ch == '\r') + putch('\n'); + } + } + else + { + ch = getch(); /* get the special key code */ + switch (ch) + { + case 0x3B: /* F1 */ + exiting = 1; /* quit now */ + break; + + case 0x3C: /* F2 */ + local_echo = !local_echo; /* toggle echo */ + break; + + case 0x3D: /* F3 */ + cr_add_lf = !cr_add_lf; /* toggle LF */ + break; + } + } + } /* end if kbhit */ + + if (input_ready()) /* com port */ + { + ch = get_char(); + putch(ch); + if (cr_add_lf && ch == '\r') /* add LF to CR? */ + putch('\n'); + } + } /* end while not exiting */ + deinit_comm(); +} -- cgit v1.2.3-54-g00ecf