#include <iostream>
#include <stdexcept>
using namespace std;
unsigned char memory[0x1000] = {};
const size_t bits_in_byte = 8;
const size_t video_mem_offset = 0xF00;
const size_t video_mem_row_bitsize = 0x40;
const size_t video_mem_row_stride =
(video_mem_row_bitsize + (bits_in_byte - 1)) / bits_in_byte;
typedef unsigned char video_mem_row[video_mem_row_stride];
video_mem_row* video_mem = (video_mem_row*)(memory + video_mem_offset);
const size_t video_mem_number_of_rows = 0x20;
bool get_video_bit(size_t row, size_t col)
{
if (row >= video_mem_number_of_rows || col > video_mem_row_bitsize)
throw std::invalid_argument("index");
unsigned char* p = &video_mem[row][col / bits_in_byte];
unsigned char mask = ((unsigned char)1) << col % bits_in_byte;
return *p & mask;
}
void set_video_bit(size_t row, size_t col, bool value)
{
if (row >= video_mem_number_of_rows || col > video_mem_row_bitsize)
throw std::invalid_argument("index");
unsigned char* p = &video_mem[row][col / bits_in_byte];
unsigned char mask = ((unsigned char)1) << col % bits_in_byte;
if (value)
*p |= mask;
else
*p &= ~mask;
}
int main()
{
size_t test_rows = 3;
for (size_t i = 0; i < test_rows; i++)
for (int j = 0; j < video_mem_row_bitsize; j++)
set_video_bit(i, j, (i + j) % 2);
bool check_failed = false;
for (size_t k = 0; k < test_rows * video_mem_row_stride; k++)
if (memory[video_mem_offset + k] != (((k / video_mem_row_stride) % 2) ? 0x55 : 0xaa))
check_failed = true;
cout << boolalpha << check_failed << endl;
return 0;
}