]> 4ch.mooo.com Git - 16.git/blob - 16/tweak16/TWKUSER.C
16_ca needs huge amounts of work and I should remember what needs to be done soon...
[16.git] / 16 / tweak16 / TWKUSER.C
1 #include <stdio.h>\r
2 #include <stdlib.h>\r
3 #include <dos.h>\r
4 #include <io.h>\r
5 #include <fcntl.h>\r
6 \r
7 #include "TwkUser.h"\r
8 \r
9 /*\r
10     readyVgaRegs() does the initialization to make the VGA ready to\r
11         accept any combination of configuration register settings.\r
12 \r
13         This involves enabling writes to index 0 to 7 of the CRT controller\r
14         (port 0x3d4), by clearing the most significant bit (bit 7) of index\r
15         0x11.\r
16 */\r
17 \r
18 void readyVgaRegs(void)\r
19         {\r
20         int v;\r
21         outportb(0x3d4,0x11);\r
22     v = inportb(0x3d5) & 0x7f;\r
23         outportb(0x3d4,0x11);\r
24         outportb(0x3d5,v);\r
25         }\r
26 \r
27 /*\r
28         outReg sets a single register according to the contents of the\r
29         passed Register structure.\r
30 */\r
31 \r
32 void outReg(Register r)\r
33         {\r
34         switch (r.port)\r
35                 {\r
36                 /* First handle special cases: */\r
37 \r
38                 case ATTRCON_ADDR:\r
39                         inportb(STATUS_ADDR);           /* reset read/write flip-flop */\r
40                         outportb(ATTRCON_ADDR, r.index | 0x20);\r
41                                                                                 /* ensure VGA output is enabled */\r
42                         outportb(ATTRCON_ADDR, r.value);\r
43                         break;\r
44 \r
45                 case MISC_ADDR:\r
46                 case VGAENABLE_ADDR:\r
47                         outportb(r.port, r.value);      /*      directly to the port */\r
48                         break;\r
49 \r
50                 case SEQ_ADDR:\r
51                 case GRACON_ADDR:\r
52                 case CRTC_ADDR:\r
53                 default:                                                /* This is the default method: */\r
54                         outportb(r.port, r.index);      /*      index to port                      */\r
55                         outportb(r.port+1, r.value);/*  value to port+1                    */\r
56                         break;\r
57                 }\r
58         }\r
59 \r
60 \r
61 /*\r
62         outRegArray sets n registers according to the array pointed to by r.\r
63         First, indexes 0-7 of the CRT controller are enabled for writing.\r
64 */\r
65 \r
66 void outRegArray(Register *r, int n)\r
67         {\r
68     readyVgaRegs();\r
69         while (n--)\r
70                 outReg(*r++);\r
71         }\r
72 \r
73 \r
74 /*\r
75         loadRegArray opens the given file, does some validity checking, then\r
76         reads the entire file into an array of Registers, which is returned\r
77         via the array parameter.\r
78 \r
79         You will probably want to provide your own error handling code in\r
80         this function, as it never aborts the program, rather than just\r
81         printing an error message and returning NULL.\r
82 \r
83         The returned value is the number of Registers read.  The &array\r
84         parameter is set to the allocated Register array.\r
85 \r
86         If you use this function, remember to free() the returned array\r
87         pointer, as it was allocated dynamically using malloc() (unless NULL\r
88         is returned, which designates an error)!\r
89 */\r
90 \r
91 int loadRegArray(char *fpath, RegisterPtr *array)\r
92         {\r
93         int handle, regs;\r
94         long fsize;\r
95         *array = NULL;\r
96 \r
97         if ((handle = open(fpath, O_BINARY | O_RDONLY)) == -1)\r
98                 /* error opening file */\r
99                 /* include your error handling code here */\r
100                 goto fileerror;\r
101 \r
102     if ((fsize = filelength(handle)) == -1)\r
103                 /* error acquiring file size */\r
104                 goto fileerror;\r
105         if (fsize % sizeof(Register))\r
106                 {\r
107                 printf("Illegal TWEAK file size: %s\n", fpath);\r
108                 return 0;\r
109                 }\r
110         regs = fsize / sizeof(Register);\r
111 \r
112     if (!(*array = (Register *)malloc(fsize)))\r
113                 {\r
114                 printf("Out of memory allocating buffer for %s\n", fpath);\r
115                 return 0;\r
116                 }\r
117         if (read(handle, (void*)*array, fsize) == -1)\r
118                 /* error reading file */\r
119                 goto fileerror;\r
120 \r
121     if (close(handle) == -1)\r
122                 {\r
123                 /* error closing file */\r
124                 goto fileerror;\r
125                 }\r
126 \r
127         /* file read ok, return pointer to buffer */\r
128         return regs;\r
129 \r
130 fileerror:\r
131         perror(fpath);\r
132         if (*array) free(*array);\r
133         return 0;\r
134         }\r