-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutil.h
172 lines (131 loc) · 4.63 KB
/
util.h
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
/*
* util.h - BIOS interface and other basic helpers
*
* author: luke8086
* license: GPL-2
*/
/*
* Note: this library only covers a tiny subset of available functionality
* of BIOS. To learn more, see http://stanislavs.org/helppc/idx_interrupt.html
* and http://www.ctyme.com/rbrown.htm
*/
#ifndef _UTIL_H_
#define _UTIL_H_
#include <stdint.h>
/* ------------------------------------------------------------------------- *
* Misc declarations and basic helper functions
* ------------------------------------------------------------------------- */
typedef __SIZE_TYPE__ size_t;
/* Function attribute moving it to the .start section */
#define ENTRY_POINT __attribute__((section(".start")))
/* Breakpoint trigger for Bochs */
#define BOCHS_BREAKPOINT __asm__ volatile ("xchg %bx, %bx")
/* Set values of the FS / GS registers */
void set_fs(uint16_t);
void set_gs(uint16_t);
/* Standard library functions (may be called implicitly by the compiler) */
void *memcpy(void *, const void *, size_t);
size_t strlen(const char *s);
/* Return a pseudo-random number between 0 - 65534 */
uint16_t rand(void);
/* ------------------------------------------------------------------------- *
* Code for triggering software interrupts, in particular to call BIOS services
* ------------------------------------------------------------------------- */
/*
* Container for values of the CPU registers
*
* Note: initializing fields of different structs at once,
* e.g. struct regs r = { .ah = 1, .bx = 2 }), doesn't seem to work
*/
struct regs {
union {
struct {
int32_t eax, ebx, ecx, edx;
uint32_t ebp, edi, esi;
};
struct {
int16_t ax, _eax_fill;
int16_t bx, _ebx_fill;
int16_t cx, _ecx_fill;
int16_t dx, _edx_fill;
uint16_t bp, _ebp_fill;
uint16_t di, _edi_fill;
uint16_t si, _esi_fill;
};
struct {
int8_t al, ah; int16_t _eax_fill_;
int8_t bl, bh; int16_t _ebx_fill_;
int8_t cl, ch; int16_t _ecx_fill_;
int8_t dl, dh; int16_t _edx_fill_;
};
};
uint32_t eflags;
};
/* Trigger software interrupt */
void intr(int int_no, struct regs *);
/* ------------------------------------------------------------------------- *
* Video services
* ------------------------------------------------------------------------- */
/* Text attributes */
enum {
CL_BLACK, CL_BLUE, CL_GREEN, CL_CYAN,
CL_RED, CL_MAGENTA, CL_BROWN, CL_LIGHT_GRAY,
/* Bright colors require additional setup to be used for bg */
CL_DARK_GRAY, CL_LIGHT_BLUE, CL_LIGHT_GREEN, CL_LIGHT_CYAN,
CL_LIGHT_RED, CL_LIGHT_MAGENTA, CL_YELLOW, CL_WHITE
};
/* Toggle visibility of the cursor */
void toggle_cursor(int visible);
/* Get cursor position (high byte = row, low byte = column) */
uint16_t get_cursor(void);
/* Move text cursor to the given position */
void move_cursor(uint16_t pos);
/* Write a single character at the cursor position */
void put_char(char);
/* Write a null-terminated string with attribute at the cursor position */
void put_string(char *, uint8_t);
/*
* Direct access to the text-mode video memory.
* Requires calling set_fs(TEXT_MEM_SEG);
*/
/* Default text-mode dimensions */
#define TEXT_WIDTH 80
#define TEXT_HEIGHT 25
/* Attributed character, a single item of the text-mode memory */
struct attr_char {
uint8_t ascii;
uint8_t attr;
};
/* Text-mode memory */
#define TEXT_MEM_SEG 0xb800
#define TEXT_MEM ((struct attr_char __seg_fs volatile *) 0x00)
/* Access a single character, assuming default screen dimensions */
#define CHAR_AT(x, y) (TEXT_MEM[(y) * TEXT_WIDTH + (x)])
/* ------------------------------------------------------------------------- *
* Keyboard services
* ------------------------------------------------------------------------- */
/* Single keystroke in the keyboard buffer */
struct keystroke {
char scancode;
char ascii;
};
/* Keyboard scan codes */
enum {
SC_ESC = 0x01,
SC_UP = 0x48,
SC_DOWN = 0x50,
SC_LEFT = 0x4b,
SC_RIGHT = 0x4d,
SC_PGUP = 0x49,
SC_PGDN = 0x51,
};
/* Check if a keystroke is ready in the keyboard buffer */
int check_keystroke(void);
/* Wait for, and return the next keystroke from the keyboard buffer */
struct keystroke get_keystroke(void);
/* ------------------------------------------------------------------------- *
* Clock services
* ------------------------------------------------------------------------- */
/* Get system time in 18.2Hz ticks since midnight */
uint32_t get_time(void);
#endif /* _UTIL_H_ */