Basic high res gfx
This commit is contained in:
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
use crate::memory::PeekPoke;
|
use crate::memory::PeekPoke;
|
||||||
use crate::address::Word;
|
use crate::word::Word;
|
||||||
|
|
||||||
pub trait Device {
|
pub trait Device {
|
||||||
fn tick(&mut self);
|
fn tick(&mut self);
|
||||||
|
|||||||
+8
-3
@@ -1,12 +1,12 @@
|
|||||||
use crate::opcodes::Opcode;
|
use crate::opcodes::Opcode;
|
||||||
use crate::opcodes::InvalidOpcode;
|
use crate::opcodes::InvalidOpcode;
|
||||||
use crate::memory::Memory;
|
use crate::memory::Memory;
|
||||||
use crate::address::Word;
|
use crate::word::Word;
|
||||||
use crate::memory::PeekPoke;
|
use crate::memory::PeekPoke;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
#[allow(clippy::upper_case_acronyms)]
|
#[allow(clippy::upper_case_acronyms)]
|
||||||
struct CPU {
|
pub struct CPU {
|
||||||
memory: Memory, // Main memory, all of it
|
memory: Memory, // Main memory, all of it
|
||||||
pc: Word, // program counter, address of the low byte of the instruction
|
pc: Word, // program counter, address of the low byte of the instruction
|
||||||
dp: Word, // data pointer, address of the low byte of one cell above the data stack
|
dp: Word, // data pointer, address of the low byte of one cell above the data stack
|
||||||
@@ -23,8 +23,13 @@ struct Instruction {
|
|||||||
length: u8
|
length: u8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PeekPoke for CPU {
|
||||||
|
fn peek(&self, addr: Word) -> u8 { self.memory.peek(addr) }
|
||||||
|
fn poke(&mut self, addr: Word, val: u8) { self.memory.poke(addr, val) }
|
||||||
|
}
|
||||||
|
|
||||||
impl CPU {
|
impl CPU {
|
||||||
fn new(memory: Memory) -> Self {
|
pub fn new(memory: Memory) -> Self {
|
||||||
Self {
|
Self {
|
||||||
memory,
|
memory,
|
||||||
pc: 1024.into(),
|
pc: 1024.into(),
|
||||||
|
|||||||
+26
-11
@@ -1,5 +1,5 @@
|
|||||||
mod memory;
|
mod memory;
|
||||||
mod address;
|
mod word;
|
||||||
mod opcodes;
|
mod opcodes;
|
||||||
mod cpu;
|
mod cpu;
|
||||||
mod bus;
|
mod bus;
|
||||||
@@ -14,6 +14,10 @@ use winit::{
|
|||||||
use pixels::{Pixels, SurfaceTexture};
|
use pixels::{Pixels, SurfaceTexture};
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
use winit::window::Window;
|
||||||
|
use crate::cpu::CPU;
|
||||||
|
use crate::memory::{ Memory, PeekPoke };
|
||||||
|
use crate::word::Word;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let event_loop = EventLoop::new();
|
let event_loop = EventLoop::new();
|
||||||
@@ -33,6 +37,14 @@ fn main() {
|
|||||||
Pixels::new(640, 480, surface_texture).unwrap()
|
Pixels::new(640, 480, surface_texture).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
|
let memory = Memory::from(rng);
|
||||||
|
let cpu = CPU::new(memory);
|
||||||
|
window_loop(event_loop, window, pixels, cpu)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn window_loop(event_loop: EventLoop<()>, window: Window, mut pixels: Pixels, mut cpu: CPU) -> ! {
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
*control_flow = ControlFlow::Poll;
|
*control_flow = ControlFlow::Poll;
|
||||||
|
|
||||||
@@ -45,27 +57,30 @@ fn main() {
|
|||||||
}
|
}
|
||||||
Event::MainEventsCleared => {
|
Event::MainEventsCleared => {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
draw(pixels.get_frame());
|
draw(pixels.get_frame(), &mut cpu);
|
||||||
let draw_time = Instant::now() - start;
|
let draw_time = Instant::now() - start;
|
||||||
pixels.render().expect("Problem displaying framebuffer");
|
pixels.render().expect("Problem displaying framebuffer");
|
||||||
let total_time = Instant::now() - start;
|
let total_time = Instant::now() - start;
|
||||||
println!("Tick took {} total, {} to draw", total_time.as_micros(), draw_time.as_micros());
|
//println!("Tick took {} total, {} to draw", total_time.as_micros(), draw_time.as_micros());
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(frame: &mut [u8]) {
|
fn draw(frame: &mut [u8], cpu: &mut CPU) {
|
||||||
assert_eq!(frame.len(), 640 * 480 * 4);
|
assert_eq!(frame.len(), 640 * 480 * 4);
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
|
|
||||||
for (_, pixel) in frame.chunks_exact_mut(4).enumerate() {
|
// For now, assume 160x120 direct graphics mode
|
||||||
let p = rng.next_u32();
|
for (i, pixel) in frame.chunks_exact_mut(4).enumerate() {
|
||||||
let [low, mid, high, _] = p.to_le_bytes();
|
let (display_row, display_col) = (i / 640, i % 640);
|
||||||
pixel[0] = low;
|
let (vulcan_row, vulcan_col) = (display_row >> 2, display_col >> 2);
|
||||||
pixel[1] = mid;
|
let vb = cpu.peek(Word::from((0x10000 + vulcan_row * 160 + vulcan_col) as u32));
|
||||||
pixel[2] = high;
|
let (red, green, blue) = (vb >> 5, (vb >> 3) & 7, (vb & 3) << 1);
|
||||||
|
|
||||||
|
pixel[0] = red << 5;
|
||||||
|
pixel[1] = green << 5;
|
||||||
|
pixel[2] = blue << 5;
|
||||||
pixel[3] = 0xff;
|
pixel[3] = 0xff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use crate::address::Word;
|
use crate::word::Word;
|
||||||
use crate::address::MEM_SIZE;
|
use crate::word::MEM_SIZE;
|
||||||
|
|
||||||
pub struct Memory([u8; MEM_SIZE as usize]);
|
pub struct Memory([u8; MEM_SIZE as usize]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user