Files
vulcan/vemu/src/main.rs
T

88 lines
2.5 KiB
Rust
Raw Normal View History

2022-02-20 19:15:54 -06:00
mod display;
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-03-06 17:57:20 -06:00
use vcore::cpu::CPU;
use vcore::memory::{Memory, PeekPoke};
use vcore::word::Word;
2022-02-20 12:58:52 -06:00
use pixels::{Pixels, SurfaceTexture};
2022-02-26 01:42:32 -06:00
use std::time::{Instant, Duration};
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 = {
2022-02-19 15:28:13 -06:00
let surface_texture = SurfaceTexture::new(640, 480, &window);
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-02-26 01:42:32 -06:00
// for n in 0..128 {
// cpu.poke(Word::from(0x10000 + 128 * n), 0b11100000);
// cpu.poke(Word::from(0x10000 + n), 0xff);
// cpu.poke(Word::from(0x10000 + 128 * n + 127), 0b00000011);
// cpu.poke(Word::from(0x10000 + 128 * 127 + n), 0b00011100);
// }
cpu.poke_slice(Word::from(0x400), include_bytes!("gfx_test.rom"));
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-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 {
event: WindowEvent::Resized(newSize),
window_id
} if window_id == window.id() => {
pixels.resize_surface(newSize.width, newSize.height);
}
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-02-26 01:42:32 -06:00
while Instant::now() < start + Duration::from_millis(25) {
for _ in 1..1000 {
cpu.tick()
}
}
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
}