io device

This commit is contained in:
sfj 2026-01-13 11:47:34 +01:00
parent bc79410a77
commit 9fab1ca0cf
3 changed files with 39 additions and 22 deletions

View File

@ -1,4 +1,4 @@
#include "screen.hpp"
#include "io_device.hpp"
#include <SDL2/SDL.h>
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_render.h>
@ -7,6 +7,7 @@
#include <array>
#include <cstdint>
#include <mutex>
#include <print>
using namespace vc5;
@ -62,11 +63,12 @@ constexpr auto charset = std::array {
}
auto Screen::create(int ch_size, int ch_width, int ch_height)
-> std::expected<std::unique_ptr<Screen>, std::string>
auto IoDevice::create(int ch_size, int ch_width, int ch_height)
-> std::expected<std::unique_ptr<IoDevice>, std::string>
{
auto profile = ScreenProfile(ch_size, ch_width, ch_height);
auto [_0, _1, _2, px_width, px_height] = profile;
auto px_width= profile.px_width;
auto px_height = profile.px_height;
if (!sdl_is_initialized) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
@ -94,10 +96,10 @@ auto Screen::create(int ch_size, int ch_width, int ch_height)
SDL_RenderPresent(renderer);
return std::make_unique<Screen>(profile, window, renderer, texture);
return std::make_unique<IoDevice>(profile, window, renderer, texture);
}
Screen::~Screen()
IoDevice::~IoDevice()
{
auto lock = std::lock_guard(mx);
@ -110,7 +112,7 @@ Screen::~Screen()
}
}
auto Screen::set_char(uint16_t offset, uint8_t value)
auto IoDevice::set_char(uint16_t offset, uint8_t value)
-> std::expected<void, std::string>
{
auto lock = std::lock_guard(mx);
@ -118,7 +120,7 @@ auto Screen::set_char(uint16_t offset, uint8_t value)
auto it = std::find_if(charset.begin(), charset.end(), [&](Char ch) {
return ch.ch == value;
});
Char my_char = *it;
uint64_t char_data = it->to_u64();
SDL_Color* buffer;
int pitch;
@ -128,19 +130,20 @@ auto Screen::set_char(uint16_t offset, uint8_t value)
}
auto [ch_size, ch_width, ch_height, px_width, px_height] = m_profile;
std::println("ch_size = {}, ch_width = {}, ch_height = {}, px_width = {}, px_height = {}", ch_size, ch_width, ch_height, px_width, px_height);
for (int ch_y = 0; ch_y < ch_height; ++ch_y) {
for (int ch_x = 0; ch_x < ch_width; ++ch_x) {
for (int ch_y = 0; ch_y < 8; ++ch_y) {
for (int ch_x = 0; ch_x < 8; ++ch_x) {
bool ch
= my_char.to_u64() >> (ch_y * ch_width + (ch_width - ch_x - 1))
= char_data >> (ch_y * 8 + (8 - ch_x - 1))
& 1;
for (int px_y = 0; px_y < ch_size; ++px_y) {
for (int px_x = 0; px_x < ch_size; ++px_x) {
int x = (offset % ch_width * ch_size + ch_x) * px_width
int x = (offset % 4 * ch_size + ch_x) * 4
+ px_x;
int y = (offset / ch_height * ch_size + ch_y) * px_height
int y = (offset / 4 * ch_size + ch_y) * 4
+ px_y;
buffer[y * px_width + x] = ch

View File

@ -17,8 +17,8 @@ struct ScreenProfile {
: ch_size(ch_size)
, ch_width(ch_width)
, ch_height(ch_height)
, px_width(ch_size * ch_width)
, px_height(ch_size * ch_height)
, px_width(ch_size * 8 * ch_width)
, px_height(ch_size * 8 * ch_height)
{
}
@ -29,12 +29,12 @@ struct ScreenProfile {
int px_height;
};
class Screen {
class IoDevice {
public:
static auto create(int ch_size, int ch_width, int ch_height)
-> std::expected<std::unique_ptr<Screen>, std::string>;
-> std::expected<std::unique_ptr<IoDevice>, std::string>;
explicit Screen(ScreenProfile profile,
explicit IoDevice(ScreenProfile profile,
SDL_Window* window,
SDL_Renderer* renderer,
SDL_Texture* texture)
@ -44,7 +44,7 @@ public:
, m_texture(texture)
{
}
~Screen();
~IoDevice();
auto set_char(uint16_t offset, uint8_t value)
-> std::expected<void, std::string>;

View File

@ -1,18 +1,26 @@
#include "builder.hpp"
#include "screen.hpp"
#include "io_device.hpp"
#include <SDL2/SDL_main.h>
#include <array>
#include <cstdint>
#include <print>
#include <utility>
#include <cstdlib>
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
using namespace vc5;
using R = Reg;
int main()
{
auto screen = Screen::create(4, 8 * 40, 8 * 24);
screen.value()->set_char(0, 'A');
auto device = IoDevice::create(4, 40, 24).value();
device->set_char(0, 'A').value();
std::this_thread::sleep_for(1s);
constexpr R r0 = R::R0;
constexpr R r1 = R::R1;
@ -69,3 +77,9 @@ int main()
std::println("{: 4x}\t{}", i, vm.word(i));
}
}
extern "C" const char* __asan_default_options(void)
{
return "detect_leaks=0";
}