core/font: clean up and remove trackbuf usage
[syslinux/sherbszt.git] / core / font.c
blob9d4f42c9c9f2b86b4725b06aad657642def1aa57
1 /*
2 * -----------------------------------------------------------------------
4 * Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * -----------------------------------------------------------------------
15 * font.c
17 * VGA font handling code
21 #include <sys/io.h>
22 #include <stdio.h>
23 #include <fs.h>
25 #include "bios.h"
26 #include "graphics.h"
27 #include "core.h"
29 static __lowmem char fontbuf[8192];
31 extern uint8_t UserFont;
33 uint16_t GXPixCols = 1; /* Graphics mode pixel columns */
34 uint16_t GXPixRows = 1; /* Graphics mode pixel rows */
37 * loadfont: Load a .psf font file and install it onto the VGA console
38 * (if we're not on a VGA screen then ignore.)
40 void loadfont(const char *filename)
42 struct psfheader {
43 uint16_t magic;
44 uint8_t mode;
45 uint8_t height;
46 } hdr;
47 FILE *f;
48 char *p;
49 int i;
51 f = fopen(filename, "r");
52 if (!f)
53 return;
55 p = trackbuf;
56 /* Read header */
57 if (_fread(&hdr, sizeof hdr, f) != sizeof hdr)
58 goto fail;
60 /* Magic number */
61 if (hdr.magic != 0x0436)
62 goto fail;
64 /* File mode: font modes 0-5 supported */
65 if (hdr.mode > 5)
66 goto fail;
68 /* VGA minimum/maximum */
69 if (hdr.height < 2 || hdr.height > 32)
70 goto fail;
72 /* Load the actual font into the font buffer. */
73 memset(fontbuf, 0, 256*32);
75 p = fontbuf;
76 for (i = 0; i < 256; i++) {
77 if (_fread(p, hdr.height, f) != hdr.height)
78 goto fail;
79 p += 32;
82 /* Loaded OK */
83 VGAFontSize = hdr.height;
84 UserFont = 1; /* Set font flag */
85 use_font();
87 fail:
88 fclose(f);
92 * use_font:
93 * This routine activates whatever font happens to be in the
94 * vgafontbuf, and updates the adjust_screen data.
95 * Must be called with CS = DS
97 void use_font(void)
99 com32sys_t ireg, oreg;
100 uint8_t bytes = VGAFontSize;
102 /* Nonstandard mode? */
103 if (UsingVGA & ~0x3)
104 syslinux_force_text_mode();
106 memset(&ireg, 0, sizeof(ireg));
108 ireg.es = SEG(fontbuf);
109 ireg.ebp.w[0] = OFFS(fontbuf); /* ES:BP -> font */
111 /* Are we using a user-specified font? */
112 if (UserFont & 0x1) {
113 /* Are we in graphics mode? */
114 if (UsingVGA & 0x1) {
115 uint8_t rows;
117 rows = GXPixRows / bytes;
118 VidRows = rows - 1;
120 /* Set user character table */
121 ireg.eax.w[0] = 0x1121;
122 ireg.ebx.b[0] = 0;
123 ireg.ecx.b[0] = bytes; /* bytes/character */
124 ireg.edx.b[0] = rows;
126 __intcall(0x10, &ireg, &oreg);
128 /* 8 pixels/character */
129 VidCols = ((GXPixCols >> 3) - 1);
131 /* No need to call adjust_screen */
132 return;
133 } else {
134 ireg.eax.w[0] = 0x1110; /* Load into VGA RAM */
135 ireg.ebx.b[0] = 0;
136 ireg.ebx.b[1] = bytes; /* bytes/character */
137 ireg.ecx.w[0] = 256;
138 ireg.edx.w[0] = 0;
140 __intcall(0x10, &ireg, &oreg);
142 ireg.ebx.b[0] = 0;
143 ireg.eax.w[0] = 0x1103; /* Select page 0 */
144 __intcall(0x10, &ireg, NULL);
148 adjust_screen();
152 * adjust_screen: Set the internal variables associated with the screen size.
153 * This is a subroutine in case we're loading a custom font.
155 void adjust_screen(void)
157 com32sys_t ireg, oreg;
158 volatile uint8_t *vidrows = (volatile uint8_t *)BIOS_vidrows;
159 uint8_t rows, cols;
161 rows = *vidrows;
162 if (!rows) {
164 * No vidrows in BIOS, assume 25.
165 * (Remember: vidrows == rows-1)
167 rows = 24;
170 VidRows = rows;
172 ireg.eax.b[1] = 0x0f; /* Read video state */
173 __intcall(0x10, &ireg, &oreg);
174 cols = oreg.eax.b[1];
176 VidCols = --cols; /* Store count-1 (same as rows) */
179 void pm_adjust_screen(com32sys_t *regs __unused)
181 adjust_screen();
184 void pm_userfont(com32sys_t *regs)
186 regs->es = SEG(fontbuf);
187 regs->ebx.w[0] = OFFS(fontbuf);