summaryrefslogtreecommitdiff
path: root/libc/bios/bios_vid.c
blob: 8c55100e101c6fa1ef2c4966f667dcb1f6ad73db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Copyright (C) 1996 Robert de Bath <robert@mayday.compulink.co.uk>
 * This file is part of the Linux-8086 C library and is distributed
 * under the GNU Library General Public License.
 */

#if !__FIRST_ARG_IN_AX__
#ifdef __AS386_16__
#ifdef __STANDALONE__

#include <bios.h>
#include <errno.h>
int errno;

#ifdef L_bios_putch
void (*__smart_putch)() = 0;

putch(c)
int c;
{
static int col = 0;
   if (__smart_putch)
      (*__smart_putch)(c);
   else if (c&0xE0) {
      col++;
      asm_putc(c);
   }
   else switch(c)
   {
   case '\t': 
      do putch(' '); while(col&7);
      break;
   case '\n':
      asm_putc('\r');
   case '\r':
      col = 0;
      asm_putc(c);
      break;
   case '\f':
      col = 0;
#asm
      mov ah,#$0F
      int $10
      mov ah,#$00
      int $10
#endasm
      break;
   }
}

static asm_putc(c)
{
#asm
#if !__FIRST_ARG_IN_AX__
  mov   bx,sp
  mov   ax,[bx+2]
#endif
  mov   ah,#$0E
  mov   bx,#7
  int   $10
#endasm
}

#endif

/****************************************************************************/

#ifdef L_bios_getch
getch()
{
#asm
  xor	ax,ax
  int	$16
#endasm
}
#endif

/****************************************************************************/

#ifdef L_bios_kbhit
kbhit()
{
#asm
  mov	ah,#1
  int	$16
  jz	nokey
  cmp	ax,#0
  jnz	dort
  mov	ax,#3
dort:
  ret
nokey:
  xor	ax,ax
#endasm
}
#endif

/****************************************************************************/

#ifdef L_bios_cputs
cputs(str)
   char * str;
{
      while(*str) putch(*str++);
}
#endif

/****************************************************************************/

#ifdef L_bios_getche
getche()
{
   static char linebuf[80];
   static int nextc = 0, endc=0;
   int rv;

   if (nextc >= endc)
   {
      endc = bios_readline(linebuf, sizeof(linebuf));
      nextc= 0;
   }
   if (endc <= nextc) return 3;
   rv = linebuf[endc++];
   if (endc == nextc) return '\r';
   return rv;
}
#endif

/****************************************************************************/

#ifdef L_bios_gotoxy
static gotoxy(x,y)
{
#asm
#if __FIRST_ARG_IN_AX__
  mov	bx,sp
  mov	dl,al
  mov	ax,[bx+2]
  mov	dh,al
#else
  mov	bx,sp
  mov	ax,[bx+4]
  mov	dh,al
  mov	ax,[bx+2]
  mov	dl,al
#endif
  mov	ah,#$02
  mov	bx,#7
  int	$10
#endasm
}
#endif

/****************************************************************************/

#ifdef L_bios_rdline
bios_rdline(buf, len)
char * buf;
int len;
{
   int ch;
   int pos=0;

   if( len < 0 ) { errno = EINVAL; return -1; }
   if( len == 0 )
   {
      if( kbhit() == 0 ) return 0;
      errno = EINTR;
      return -1;
   }
   if( len == 1 )
   {
      buf[0]=((ch=getch())&0xFF?ch&0xFF:((ch>>8)&0xFF|0x80));
      return 1;
   }

   for(ch=0;;)
   {
      if(ch != '\003')
      {
         ch = getch();
	 if( pos == 0 && (ch&0xFF) == 0 )
	 {
	    buf[0] = ((ch>>8)|0x80);
	    return 1;
	 }
	 ch &= 0x7F;
      }
      if( ch == '\r' )
      {
         putch('\n');
         buf[pos++] = '\n';
	 return pos;
      }
      if( ch >= ' ' && ch != 0x7F && pos < len-1)
         putch(buf[pos++] = ch);
      else if( (ch == '\003' || ch == '\b') && pos > 0 )
      {
         putch('\b'); putch(' '); putch('\b');
	 pos--;
      }
      else if( ch == '\003' )
         return 0;
      else
         putch('\007');
   }
}
#endif

/****************************************************************************/

#endif
#endif
#endif