Max OS 0.3
Loading...
Searching...
No Matches
vga.cpp
Go to the documentation of this file.
1
11#include <drivers/video/vga.h>
12
13using namespace MaxOS;
14using namespace MaxOS::drivers;
15using namespace MaxOS::drivers::video;
16using namespace MaxOS::hardwarecommunication;
17
18
23: m_misc_port(0x3C2),
24 m_crtc_index_port(0x3D4),
25 crtc_data_port(0x3D5),
26 m_sequence_index_port(0x3C4),
27 m_sequence_data_port(0x3C5),
28 m_graphics_controller_index_port(0x3CE),
29 m_graphics_controller_data_port(0x3CF),
30 m_attribute_controller_index_port(0x3C0),
31 m_attribute_controller_read_port(0x3C1),
32 m_attribute_controller_write_port(0x3C0),
33 m_attribute_controller_reset_port(0x3DA)
34{
35}
36
37VideoGraphicsArray::~VideoGraphicsArray() = default;
38
44void VideoGraphicsArray::write_registers(uint8_t *registers) {
45
46 // Move to the next register
47 m_misc_port.write(*(registers++));
48
49 // Set the sequencer registers
50 for (uint8_t i = 0; i < 5; i++) {
51 m_sequence_index_port.write(i);
52 m_sequence_data_port.write(*(registers++));
53 }
54
55 // Clear protection bit to enable writing to CR0-7
56 m_crtc_index_port.write(0x03);
57 crtc_data_port.write(crtc_data_port.read() | 0x80);
58 m_crtc_index_port.write(0x11);
59 crtc_data_port.write(crtc_data_port.read() | ~0x80);
60
61 // Ensure protection bit is set
62 registers[0x03] = registers[0x03] | 0x80;
63 registers[0x11] = registers[0x11] & ~0x80;
64
65 // Write the CRTC registers
66 for (uint8_t i = 0; i < 25; i++) {
67 m_crtc_index_port.write(i);
68 crtc_data_port.write(*(registers++));
69 }
70
71 // Write the graphics controller registers
72 for (uint8_t i = 0; i < 9; i++) {
73 m_graphics_controller_index_port.write(i);
74 m_graphics_controller_data_port.write(*(registers++));
75 }
76
77 // Write the attribute controller registers
78 for (uint8_t i = 0; i < 21; i++) {
79 m_attribute_controller_reset_port.read();
80 m_attribute_controller_index_port.write(i);
81 m_attribute_controller_write_port.write(*(registers++));
82 }
83
84 // Re-Lock CRTC and unblank display
85 m_attribute_controller_reset_port.read();
86 m_attribute_controller_index_port.write(0x20);
87
88}
89
98bool VideoGraphicsArray::supports_mode(uint32_t width, uint32_t height, uint32_t colour_depth) {
99 return width == 320 && height == 200 && colour_depth == 8;
100}
101
111bool VideoGraphicsArray::internal_set_mode(uint32_t width, uint32_t height, uint32_t colour_depth) {
112
113 //Values from osdev / modes.c
114 unsigned char g_320x200x256[] =
115 {
116 // MISC
117 0x63,
118 // SEQ
119 0x03, 0x01, 0x0F, 0x00, 0x0E,
120 // CRTC
121 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F,
122 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123 0x9C, 0x0E, 0x8F, 0x28, 0x40, 0x96, 0xB9, 0xA3,
124 0xFF,
125 // GC
126 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F,
127 0xFF,
128 // AC
129 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
130 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
131 0x41, 0x00, 0x0F, 0x00, 0x00
132 };
133
134 write_registers(g_320x200x256);
135 return true;
136}
137
143uint8_t *VideoGraphicsArray::get_frame_buffer_segment() {
144
145 // Optimise so that don't have to read and write to the port every time
146 return (uint8_t *) 0xA0000;
147
148 //read data from index number 6
149 m_graphics_controller_index_port.write(0x06);
150 uint8_t segment_number =
151 m_graphics_controller_data_port.read() &
152 (3 << 2); //Shift by 2 as only interested in bits 3 & 4 (& 3 so all the other bits are removed)
153 switch (segment_number) {
154 default:
155 case 0 << 2:
156 return (uint8_t *) nullptr;
157 case 1 << 2:
158 return (uint8_t *) 0xA0000;
159 case 2 << 2:
160 return (uint8_t *) 0xB0000;
161 case 3 << 2:
162 return (uint8_t *) 0xB8000;
163 }
164}
165
173void VideoGraphicsArray::render_pixel_8_bit(uint32_t x, uint32_t y, uint8_t colour) {
174
175 uint8_t *pixel_address = get_frame_buffer_segment() + 320 * y + x;
176 *pixel_address = colour;
177}
178
186uint8_t VideoGraphicsArray::get_rendered_pixel_8_bit(uint32_t x, uint32_t y) {
187
188 uint8_t *pixel_address = get_frame_buffer_segment() + 320 * y + x;
189 return *pixel_address;
190}
191
198
199 // VGA was made by IBM
200 return "IBM";
201}
202
209 return "VGA compatible graphics card";
210}
uint32_t width() const
Gets the width of the screen.
uint32_t height() const
Gets the height of the screen.
string vendor_name() final
Gets the name of the vendor.
Definition vga.cpp:197
bool supports_mode(uint32_t width, uint32_t height, uint32_t colour_depth) final
Checks if the specified resolution is supported.
Definition vga.cpp:98
string device_name() final
Gets the name of the device.
Definition vga.cpp:208
VideoGraphicsArray()
Constructs a new Video Graphics Array (VGA) driver object, initializing the necessary I/O ports for V...
Definition vga.cpp:22
virtual uint8_t read()
read a byte from the port
Definition port.cpp:51
virtual void write(uint8_t data)
write a byte to the port
Definition port.cpp:41
Defines a Video Graphics Array (VGA) driver for rendering graphics to the screen.