Skip to content

Commit b6ad893

Browse files
committed
Maybe get simple text drawing ?
1 parent a62147f commit b6ad893

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

Cargo.lock

+36-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ humantime = "2.1"
2323
lazy-regex = "3.1"
2424
libloading = "0.8"
2525
logos = "0.13"
26+
minifb = "0.27.0"
2627
modular-bitfield = "0.11"
2728
once_cell = "1.18"
2829
serialport = "4.3"

build.rs

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ fn main() -> Result<(), Error> {
5353
println!("cargo:rerun-if-changed=src/microcode.asm");
5454
println!("cargo:rerun-if-changed=build.rs");
5555

56+
create_display_multiplier()?;
57+
5658
let file = fs::read_to_string("src/microcode.asm")?;
5759
let lex = Token::lexer(&file);
5860
let stream = Stream::parse(lex)?;
@@ -85,6 +87,10 @@ fn main() -> Result<(), Error> {
8587
Ok(())
8688
}
8789

90+
fn create_display_multiplier() -> Result<(), Error> {
91+
Ok(())
92+
}
93+
8894
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8995
enum Instruction {
9096
Add = 0x0,

src/emulator.rs

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use async_std::{
1919
use bitflags::bitflags;
2020
use clap::Args;
2121
use clio::Input;
22+
use display::TextBuffer;
2223
use libloading::Library;
2324
use modular_bitfield::prelude::*;
2425
use thiserror::Error;
@@ -559,6 +560,7 @@ struct State {
559560
mem: Box<[u8]>,
560561
program: Box<[u8]>,
561562
peripherals: HashMap<u8, Peripheral>,
563+
text_buffer: TextBuffer,
562564
}
563565

564566
impl State {
@@ -578,6 +580,7 @@ impl State {
578580
mem: vec![0; 1 << 16].into_boxed_slice(),
579581
program,
580582
peripherals: HashMap::new(),
583+
text_buffer: TextBuffer::spawn(),
581584
}
582585
}
583586

src/emulator/display.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
use std::{cell::UnsafeCell, marker::PhantomData, pin::Pin, sync::atomic::{AtomicU16, AtomicU8, Ordering}};
22

33
use async_std::task::JoinHandle;
4+
use minifb::{Scale, ScaleMode, WindowOptions};
45

56
const FONT: &[u8; 1 << 12] = include_bytes!("../vga-font.rom");
7+
const WIDTH: usize = 640;
8+
const HEIGHT: usize = 400;
69

10+
#[derive(Debug)]
711
pub struct TextBuffer {
812
data: Pin<Box<[u8; 1 << 12]>>,
913
handle: JoinHandle<()>,
@@ -14,7 +18,8 @@ unsafe impl Send for BufferPtr {}
1418

1519
impl TextBuffer {
1620
pub fn spawn() -> TextBuffer {
17-
let data = Box::pin([0; 1 << 12]);
21+
let mut data = Box::pin([0; 1 << 12]);
22+
data[0] = 0x12;
1823

1924
let handle = async_std::task::spawn(run_handle(BufferPtr(&*data)));
2025

@@ -23,5 +28,34 @@ impl TextBuffer {
2328
}
2429

2530
async fn run_handle(buffer: BufferPtr) {
26-
31+
let mut opts = WindowOptions::default();
32+
opts.scale = Scale::FitScreen;
33+
opts.scale_mode = ScaleMode::Center;
34+
35+
let mut window = minifb::Window::new("f8ful", WIDTH, HEIGHT, opts).expect("should be able to create window");
36+
let mut fb = [0x00000000; WIDTH*HEIGHT];
37+
38+
while window.is_open() {
39+
for y in 0..HEIGHT {
40+
for x in 0..WIDTH {
41+
let font_x = x % 8;
42+
let font_y = y % 16;
43+
44+
let char_x = x / 8;
45+
let char_y = y / 16;
46+
let char_idx = char_x + char_y*WIDTH/16;
47+
let character = unsafe {
48+
(*buffer.0)[char_idx]
49+
};
50+
51+
let font_addr = (character as usize) << 4 + font_y;
52+
let lit = FONT[font_addr] & !(1 << font_x) > 0;
53+
54+
// This part isn't part of the actual CPU,
55+
// the real value will be transmitted via wire instead of stored.
56+
fb[x + y*WIDTH] = if lit {0x00FFFFFF} else {0x00000000};
57+
window.update_with_buffer(&fb, WIDTH, HEIGHT).expect("should be able to update window");
58+
}
59+
}
60+
}
2761
}

0 commit comments

Comments
 (0)