Clean (void) casts from usr.sbin
[dragonfly.git] / usr.sbin / pcvt / vgaio / vgaio.y
1 %{
2 /*
3  * Copyright (c) 1994 Joerg Wunsch
4  *
5  * All rights reserved.
6  *
7  * This program is free software.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Joerg Wunsch
20  * 4. The name of the developer may not be used to endorse or promote
21  *    products derived from this software without specific prior written
22  *    permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #ident "$FreeBSD: src/usr.sbin/pcvt/vgaio/vgaio.y,v 1.5 1999/09/06 07:39:30 peter Exp $"
37 #ident "$DragonFly: src/usr.sbin/pcvt/vgaio/Attic/vgaio.y,v 1.3 2004/12/18 22:48:04 swildner Exp $"
38
39 /*
40  * $Log: vgaio.y,v $
41  * Revision 1.1  1994/03/29  02:47:27  mycroft
42  * pcvt 3.0, with some performance enhancements by Joerg Wunsch and me.
43  *
44  * Revision 1.2  1994/01/08  17:42:58  j
45  * cleanup
46  * made multiple commands per line work
47  * wrote man page
48  *
49  * Revision 1.3  21.12.1994 -hm
50  * Added mi command for accessing the misc out register
51  * hex values shown as 2 fixed chars, added binary output
52  */
53
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <sys/fcntl.h>
57 #include <machine/cpufunc.h>
58 #include <machine/pcvt_ioctl.h>
59
60 #ifdef __NetBSD__
61 #include <machine/pio.h>
62 #endif
63
64 #include "vgaio.h"
65
66 void setreg(struct reg r, int val);
67 void getreg(struct reg r);
68 void yyerror(const char *msg);
69
70 #define YYDEBUG 1
71
72 unsigned short vgabase;
73
74 %}
75
76 %union {
77         int num;
78         struct reg reg;
79 }
80
81 %token MI GR CR SR AR NEWLINE
82 %token <num> NUM
83
84 %type <num> reggroup
85 %type <reg> register
86
87 %%
88
89 interpret:      lines ;
90
91 lines:          line
92                 | lines line
93                 ;
94
95 line:           statements NEWLINE
96                 | NEWLINE
97                 | error NEWLINE         { fprintf(stderr, "bing!\n"); }
98                 ;
99
100 statements:     statement
101                 | statements ';' statement
102                 ;
103
104 statement:      register '?'            { getreg($1); }
105                 | register '=' NUM      { setreg($1, $3); }
106                 | /* lambda */
107                 ;
108
109 register:       reggroup NUM            { $$.num = $2; $$.group = $1; }
110                 ;
111
112 reggroup:       GR              { $$ = GR; }
113                 | CR            { $$ = CR; }
114                 | SR            { $$ = SR; }
115                 | AR            { $$ = AR; }
116                 | MI            { $$ = MI; }
117                 ;
118
119 %%
120
121 static struct {
122         int id;
123         const char *name;
124 } regnames[] = {
125         {GR, "gr"}, {CR, "cr"}, {SR, "sr"}, {AR, "ar"}, {MI, "mi"},
126         {0, 0}
127 };
128
129 const char *getname(struct reg r) {
130         int idx;
131         for(idx = 0; regnames[idx].id; idx++)
132                 if(regnames[idx].id == r.group)
133                         return regnames[idx].name;
134         return "??";
135 }       
136
137 /*---------------------------------------------------------------------------*
138  *      return ptr to string of 1's and 0's for value
139  *---------------------------------------------------------------------------*/
140 char *
141 bin_str(unsigned long val, int length)
142 {
143         static char buffer[80];
144         int i = 0;
145
146         if (length > 32)
147                 length = 32;
148
149         val = val << (32 - length);
150
151         while (length--)
152         {
153                 if (val & 0x80000000)
154                         buffer[i++] = '1';
155                 else
156                         buffer[i++] = '0';
157                 if ((length % 4) == 0 && length)
158                         buffer[i++] = '.';
159                 val = val << 1;
160         }
161         return (buffer);
162 }
163
164 void getreg(struct reg r) {
165         int val;                        /* FreeBSD gcc ONLY accepts an int !! */
166
167         switch(r.group) {
168         case GR:
169                 outb(0x3ce, r.num);
170                 val = inb(0x3cf);
171                 break;
172
173         case AR:
174                 r.num &= 0x1f;
175                 inb(vgabase + 0x0a);
176                 outb(0x3c0, r.num + 0x20);
177                 val = inb(0x3c1);
178                 break;
179
180         case CR:
181                 outb(vgabase + 4, r.num);
182                 val = inb(vgabase + 5);
183                 break;
184
185         case SR:
186                 outb(0x3c4, r.num);
187                 val = inb(0x3c5);
188                 break;
189
190         case MI:
191                 val = inb(0x3cc);
192                 break;
193         }
194                 
195         printf("%s%02x = 0x%02x = %s (bin)\n", getname(r), r.num, val, bin_str(val,8));
196 }
197
198 void setreg(struct reg r, int val) {
199         switch(r.group) {
200         case GR:
201                 outb(0x3ce, r.num);
202                 outb(0x3cf, val);
203                 break;
204
205         case AR:
206                 r.num &= 0x1f;
207                 inb(vgabase + 0x0a);
208                 outb(0x3c0, r.num);
209                 outb(0x3c0, val);
210                 outb(0x3c0, r.num + 0x20);
211                 break;
212
213         case CR:
214                 outb(vgabase + 4, r.num);
215                 outb(vgabase + 5, val);
216                 break;
217
218         case SR:
219                 outb(0x3c4, r.num);
220                 outb(0x3c5, val);
221                 break;
222
223         case MI:
224                 outb(0x3c2, val);
225                 break;
226         }
227                 
228         printf("%s%02x set to 0x%02x = %s (bin) now\n", getname(r), r.num, val, bin_str(val,8));
229 }
230
231 void yyerror(const char *msg) {
232         fprintf(stderr, "yyerror: %s\n", msg);
233 }
234
235 int main(int argc, char **argv) {
236         int fd;
237
238         if(argc > 1) yydebug = 1;
239
240         if((fd = open("/dev/console", O_RDONLY)) < 0)
241                 fd = 0;
242
243         if(ioctl(fd, KDENABIO, 0) < 0) {
244                 perror("ioctl(KDENABIO)");
245                 return 1;
246         }
247         vgabase = (inb(0x3cc) & 1)? 0x3d0: 0x3b0;
248         yyparse();
249
250         ioctl(fd, KDDISABIO, 0);
251         return 0;
252 }