/
memaccess.c
99 lines (83 loc) · 2.49 KB
/
memaccess.c
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
// memaccess.c
/*
File for accessing graphics memory in real mode
Not written by maniek86 2022 (c) - https://stackoverflow.com/questions/37354717/displaying-text-video-memory-at-0xb8000-without-using-the-c-library
*/
#include "typedef.h"
#include "memaccess.h"
static int VIDEO_SEG = 0xa000;
void set_video_seg(int seg) {
VIDEO_SEG = seg;
}
/* Display character with FS change */
fastcall void dispchar(uint16_t celldata, uint16_t offset)
{
uint32_t oldfs = set_videomode_fs();
dispchar_nofsupd(celldata, offset);
set_fs(oldfs);
}
fastcall void disppixel(uint8_t celldata, uint16_t offset)
{
uint32_t oldfs = set_videomode_fs();
disppixel_nofsupd(celldata, offset);
set_fs(oldfs);
}
fastcall uint16_t getchar(uint16_t offset)
{
uint16_t ret=0;
uint32_t oldfs = set_videomode_fs();
__asm__ __volatile__("movw %%fs:%[memloc], %w[ret]\n\t"
:[ret] "=&rm"(ret)
:[memloc] "m"(*(uint32_t *)(uint32_t)offset));
set_fs(oldfs);
return ret;
}
fastcall uint8_t getpixel(uint16_t offset)
{
uint8_t ret=0;
uint32_t oldfs = set_videomode_fs();
__asm__ __volatile__("movb %%fs:%[memloc], %b[ret]\n\t"
:[ret] "=&rm"(ret)
:[memloc] "m"(*(uint32_t *)(uint32_t)offset));
set_fs(oldfs);
return ret;
}
/* Display character with no FS change */
fastcall void dispchar_nofsupd(uint16_t celldata, uint16_t offset)
{
__asm__ ("movw %w[wordval], %%fs:%[memloc]\n\t"
:
:[wordval]"ri"(celldata),
[memloc] "m"(*(uint32_t *)(uint32_t)offset)
:"memory");
}
fastcall void disppixel_nofsupd(uint8_t celldata, uint16_t offset)
{
__asm__ ("movb %b[wordval], %%fs:%[memloc]\n\t"
:
:[wordval]"ri"(celldata),
[memloc] "m"(*(uint32_t *)(uint32_t)offset)
:"memory");
}
/* Set FS segment and return previous value */
fastcall uint32_t getset_fs(uint32_t segment)
{
uint32_t origfs;
__asm__ __volatile__("mov %%fs, %w[origfs]\n\t"
"mov %w[segment], %%fs\n\t"
:[origfs] "=&rm"(origfs)
:[segment] "rm"(segment));
return origfs;
}
/* Set FS segment */
fastcall void set_fs(uint32_t segment)
{
__asm__("mov %w[segment], %%fs\n\t"
:
:[segment]"rm"(segment));
}
/* Set FS to video mode segment 0xb800 */
fastcall uint32_t set_videomode_fs(void)
{
return getset_fs(VIDEO_SEG);
}