Files
vulcan/vemu/src/main.rs
T

130 lines
4.7 KiB
Rust
Raw Normal View History

2022-02-20 19:15:54 -06:00
mod display;
2022-05-01 14:24:09 -05:00
mod keyboard;
2022-02-18 21:02:17 -06:00
2022-02-19 15:28:13 -06:00
use winit::{
2022-02-22 21:46:26 -06:00
dpi::LogicalSize,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
2022-02-19 15:28:13 -06:00
window::WindowBuilder,
};
2022-05-01 14:24:09 -05:00
use crate::keyboard::convert_keycode;
use pixels::{Pixels, SurfaceTexture};
use std::collections::VecDeque;
use std::time::{Duration, Instant};
use vasm::assemble_snippet;
2022-03-06 17:57:20 -06:00
use vcore::cpu::CPU;
use vcore::memory::{Memory, PeekPoke};
use vcore::word::Word;
2022-05-03 18:10:47 -05:00
use winit::event::{DeviceEvent, ElementState};
2022-02-20 14:25:43 -06:00
use winit::window::Window;
2022-02-19 15:28:13 -06:00
2022-02-18 21:02:17 -06:00
fn main() {
2022-02-19 15:28:13 -06:00
let event_loop = EventLoop::new();
let window = {
let size = LogicalSize::new(640, 480);
WindowBuilder::new()
.with_title("Vulcan")
.with_inner_size(size)
.with_min_inner_size(size)
.build(&event_loop)
.unwrap()
};
2022-02-20 21:28:18 -06:00
let pixels = {
let winit::dpi::PhysicalSize { width, height } = window.inner_size();
let surface_texture = SurfaceTexture::new(width, height, &window);
2022-02-19 15:28:13 -06:00
Pixels::new(640, 480, surface_texture).unwrap()
};
2022-02-20 21:28:18 -06:00
let rng = rand::thread_rng();
2022-02-20 14:25:43 -06:00
let memory = Memory::from(rng);
2022-02-20 19:15:54 -06:00
let mut cpu = CPU::new(memory);
display::reset(&mut cpu);
2022-06-01 16:11:34 -05:00
let code = assemble_snippet(include_str!("typewriter.asm").lines().map(String::from)).expect("Assemble error");
println!("ROM size: {} bytes", code.len());
2022-05-01 14:24:09 -05:00
cpu.poke_slice(0x400.into(), code.as_slice());
2022-02-26 01:42:32 -06:00
cpu.start();
2022-02-20 14:25:43 -06:00
window_loop(event_loop, window, pixels, cpu)
}
fn window_loop(event_loop: EventLoop<()>, window: Window, mut pixels: Pixels, mut cpu: CPU) -> ! {
2022-05-01 14:24:09 -05:00
let mut interrupt_events: VecDeque<(usize, Option<Word>)> = VecDeque::new();
2022-05-03 20:32:16 -05:00
let mut focused = true;
2022-05-01 14:24:09 -05:00
2022-02-19 15:28:13 -06:00
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
2022-02-22 21:46:26 -06:00
window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
2022-02-26 01:42:32 -06:00
Event::WindowEvent {
2022-03-12 15:39:29 -06:00
event: WindowEvent::Resized(new_size),
2022-05-01 14:24:09 -05:00
window_id,
2022-02-26 01:42:32 -06:00
} if window_id == window.id() => {
2022-03-12 15:39:29 -06:00
pixels.resize_surface(new_size.width, new_size.height);
2022-02-26 01:42:32 -06:00
}
2022-05-03 20:32:16 -05:00
// These are sent regardless of whether the window is focused or not, and are the same regardless of keyboard
// layout, so, we have to separately track focus and this will only work as expected for normal US Sholes
// keyboards.
// If we use the WindowEvent equivalent of this, however, shifted non-letter characters will send no vkeys
// at all on Linux. Which is actually worse: this way, someone with a "normal" keyboard can use the app on
// any OS, and there are probably more Linux users than Dvorak users.
// Also, this won't actually affect me, even on Linux, because the KMAC handles the dvorak mapping in the
// keyboard itself, rather than an input map; it generates scancodes as though it were a Sholes board.
2022-05-03 20:32:50 -05:00
// See this bug: https://github.com/rust-windowing/winit/issues/1443
2022-05-03 18:10:47 -05:00
Event::DeviceEvent {
event: DeviceEvent::Key(input),
device_id: _device_id,
} => {
2022-05-03 20:32:16 -05:00
if let (Some(vk), state, true) = (input.virtual_keycode, input.state, focused) {
2022-05-01 14:24:09 -05:00
let byte = convert_keycode(vk);
let word = Word::from_bytes([
byte,
if state == ElementState::Pressed { 1 } else { 0 },
0,
]);
interrupt_events.push_back((5, Some(word)))
}
}
2022-05-03 20:32:16 -05:00
Event::WindowEvent {
event: WindowEvent::Focused(new_focus),
window_id,
} if window_id == window.id() => focused = new_focus,
2022-02-19 15:28:13 -06:00
Event::MainEventsCleared => {
let start = Instant::now();
2022-02-20 14:25:43 -06:00
draw(pixels.get_frame(), &mut cpu);
2022-02-20 12:58:52 -06:00
pixels.render().expect("Problem displaying framebuffer");
2022-06-01 16:11:34 -05:00
loop { // TODO: this will run the CPU at 100%, need to not spin while halted
2022-05-01 14:24:09 -05:00
if let Some((int, arg)) = interrupt_events.pop_front() {
cpu.interrupt(int, arg)
}
2022-06-01 16:11:34 -05:00
if cpu.running() {
for _ in 1..1000 {
cpu.tick()
}
2022-02-26 01:42:32 -06:00
}
if Instant::now() > start + Duration::from_millis(25) {
break;
}
2022-02-26 01:42:32 -06:00
}
2022-02-19 15:28:13 -06:00
}
_ => {}
}
})
2022-02-18 21:02:17 -06:00
}
2022-02-19 15:28:13 -06:00
2022-02-20 14:25:43 -06:00
fn draw(frame: &mut [u8], cpu: &mut CPU) {
2022-02-19 15:28:13 -06:00
assert_eq!(frame.len(), 640 * 480 * 4);
2022-02-20 19:15:54 -06:00
display::draw(cpu, frame);
2022-02-22 21:46:26 -06:00
}