summaryrefslogtreecommitdiff
path: root/core/font.c
blob: ff98635c0aee775b1aa35062952bd9854fde27ab (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
/*
 * -----------------------------------------------------------------------
 *
 *   Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * -----------------------------------------------------------------------
 *
 *
 * font.c
 *
 * VGA font handling code
 *
 */

#include <sys/io.h>
#include <stdio.h>
#include <fs.h>
#include "bios.h"
#include "core.h"

struct aux {
	char fontbuf[8192];
	char serial[serial_buf_size];
};

#define fontbuf		offsetof(struct aux, fontbuf)

extern uint16_t VGAFontSize;
extern uint8_t UserFont;

uint16_t GXPixCols = 1;		/* Graphics mode pixel columns */
uint16_t GXPixRows = 1;		/* Graphics mode pixel rows */

/*
 * loadfont:	Load a .psf font file and install it onto the VGA console
 *		(if we're not on a VGA screen then ignore.)
 *
 * The .psf font file must alredy be open and getc_file must be set.
 */
void loadfont(char *filename)
{
	uint16_t height, magic;
	uint32_t *di, *si;
	FILE *f;
	char *p;
	int i;

	f = fopen(filename, "r");
	if (!f)
		return;

	p = trackbuf;
	/* Read header */
	for (i = 0; i < 4; i++) {
		char ch = getc(f);
		if (ch == EOF)
			return;
		*p++ = ch;
	}

	/* Magic number */
	magic = *(uint16_t *)trackbuf;
	if (magic != 0x0436)
		return;

	/* File mode: font modes 0-5 supported */
	if (*(trackbuf) > 5)
		return;

	height = *(trackbuf + 3); /* Height of font */

	/* VGA minimum/maximum */
	if (height < 2 || height > 32)
		return;

	/* Load the actual font. Bytes = font height * 256 */
	p = trackbuf;
	for (i = 0; i < (height << 8); i++) {
		char ch = getc(f);

		if (ch == EOF)
			return;
		*p++ = ch;
	}

	/* Copy to font buffer */
	VGAFontSize = height;
	di = (uint32_t *)MK_PTR(aux_seg, fontbuf);
	si = (uint32_t *)trackbuf;
	for (i = 0; i < (height << 6); i++)
		*di++ = *si++;

	UserFont = 1;		/* Set font flag */
	use_font();
}

/*
 * use_font:
 *	This routine activates whatever font happens to be in the
 *	vgafontbuf, and updates the bios_adjust_screen data.
 *      Must be called with CS = DS
 */
void use_font(void)
{
	com32sys_t ireg, oreg;
	uint8_t bytes = VGAFontSize;


	/* Nonstandard mode? */
	if (UsingVGA & ~0x3)
		vgaclearmode();

	memset(&ireg, 0, sizeof(ireg));

	ireg.es = aux_seg;
	ireg.ebp.w[0] = fontbuf; /* ES:BP -> font */

	/* Are we using a user-specified font? */
	if (UserFont & 0x1) {
		/* Are we in graphics mode? */
		if (UsingVGA & 0x1) {
			uint8_t rows;

			rows = GXPixRows / bytes;
			VidRows = rows - 1;

			/* Set user character table */
			ireg.eax.w[0] = 0x1121;
			ireg.ebx.b[0] = 0;
			ireg.ecx.b[0] = bytes; /* bytes/character */
			ireg.edx.b[0] = rows;

			__intcall(0x10, &ireg, &oreg);

			/* 8 pixels/character */
			VidCols = ((GXPixCols >> 3) - 1);

			/* No need to call bios_adjust_screen */
			return;
		} else {
			ireg.eax.w[0] = 0x1110;	/* Load into VGA RAM */
			ireg.ebx.b[0] = 0;
			ireg.ebx.b[1] = bytes; /* bytes/character */
			ireg.ecx.w[0] = 256;
			ireg.edx.w[0] = 0;

			__intcall(0x10, &ireg, &oreg);

			ireg.ebx.b[0] = 0;
			ireg.eax.w[0] = 0x1103; /* Select page 0 */
			__intcall(0x10, &ireg, NULL);
		}

	}

	bios_adjust_screen();
}

/*
 * bios_adjust_screen: Set the internal variables associated with the screen size.
 *		This is a subroutine in case we're loading a custom font.
 */
void bios_adjust_screen(void)
{
	com32sys_t ireg, oreg;
	volatile uint8_t *vidrows = BIOS_vidrows;
	uint8_t rows, cols;

	rows = *vidrows;
	if (!rows) {
		/*
		 * No vidrows in BIOS, assume 25.
		 * (Remember: vidrows == rows-1)
		 */
		rows = 24;
	}

	VidRows = rows;

	ireg.eax.b[1] = 0x0f;	/* Read video state */
	__intcall(0x10, &ireg, &oreg);
	cols = oreg.eax.b[1];

	VidCols = --cols;	/* Store count-1 (same as rows) */
}

void pm_adjust_screen(com32sys_t *regs)
{
	bios_adjust_screen();
}