emma now has ability to decode an instruction and to display video and system memory
This commit is contained in:
parent
ceb62d2c53
commit
6dc9ce3721
1772
Cargo.lock
generated
1772
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,3 @@
|
||||
[workspace]
|
||||
members = ["chip8_toy","chip8_core"]
|
||||
members = ["chip8_toy","chip8_core", "emma"]
|
||||
resolver = "2"
|
||||
@ -9,21 +9,20 @@ impl Chip8Registers {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct Chip8InstructionParameter {
|
||||
mask: u16
|
||||
pub mask: u16
|
||||
}
|
||||
|
||||
struct Chip8Instruction {
|
||||
id: String,
|
||||
mask: u16,
|
||||
pattern: u16,
|
||||
arguments: Vec<Chip8InstructionParameter>,
|
||||
description: Box<str>
|
||||
pub struct Chip8Instruction {
|
||||
pub id: String,
|
||||
pub mask: u16,
|
||||
pub pattern: u16,
|
||||
pub arguments: Vec<Chip8InstructionParameter>,
|
||||
pub description: Box<str>
|
||||
}
|
||||
|
||||
pub fn fill_chip8_instructions() {
|
||||
let full_instruction_set = vec![
|
||||
pub fn fill_chip8_instructions() -> [Chip8Instruction; 34] {
|
||||
[
|
||||
Chip8Instruction {
|
||||
id: "SYS".to_string(),
|
||||
mask: 0x00,
|
||||
@ -128,7 +127,7 @@ pub fn fill_chip8_instructions() {
|
||||
mask: 0x00ff
|
||||
}
|
||||
],
|
||||
description: "TBD".into()
|
||||
description: "Add value in register 0x0f00 with value 0x00ff".into()
|
||||
},
|
||||
Chip8Instruction {
|
||||
id: "CPY".to_string(),
|
||||
@ -142,7 +141,7 @@ pub fn fill_chip8_instructions() {
|
||||
mask: 0x00f0
|
||||
}
|
||||
],
|
||||
description: "TBD".into()
|
||||
description: "Copy value between Register X and Y".into()
|
||||
},
|
||||
Chip8Instruction {
|
||||
id: "OR".to_string(),
|
||||
@ -366,7 +365,7 @@ pub fn fill_chip8_instructions() {
|
||||
mask: 0x0f00
|
||||
}
|
||||
],
|
||||
description: "TBD".into()
|
||||
description: "Wait for a Key to be pressed".into()
|
||||
},
|
||||
Chip8Instruction {
|
||||
id: "LDT".to_string(),
|
||||
@ -377,7 +376,7 @@ pub fn fill_chip8_instructions() {
|
||||
mask: 0x0f00
|
||||
}
|
||||
],
|
||||
description: "TBD".into()
|
||||
description: "Load Data Timer".into()
|
||||
},
|
||||
Chip8Instruction {
|
||||
id: "LST".to_string(),
|
||||
@ -388,7 +387,7 @@ pub fn fill_chip8_instructions() {
|
||||
mask: 0x0f00
|
||||
}
|
||||
],
|
||||
description: "TBD".into()
|
||||
description: "Load Sound Timer".into()
|
||||
},
|
||||
Chip8Instruction {
|
||||
id: "ADDI".to_string(),
|
||||
@ -399,7 +398,7 @@ pub fn fill_chip8_instructions() {
|
||||
mask: 0x0f00
|
||||
}
|
||||
],
|
||||
description: "TBD".into()
|
||||
description: "Add register with value at I".into()
|
||||
},
|
||||
Chip8Instruction {
|
||||
id: "SETI".to_string(),
|
||||
@ -432,7 +431,7 @@ pub fn fill_chip8_instructions() {
|
||||
mask: 0x0f00
|
||||
}
|
||||
],
|
||||
description: "TBD".into()
|
||||
description: "Store value in register X at offset I".into()
|
||||
},
|
||||
Chip8Instruction {
|
||||
id: "MLOAD".to_string(),
|
||||
@ -443,7 +442,20 @@ pub fn fill_chip8_instructions() {
|
||||
mask: 0x0f00
|
||||
}
|
||||
],
|
||||
description: "TBD".into()
|
||||
description: "Load value into register X at offset I".into()
|
||||
}
|
||||
];
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
struct Chip8System {
|
||||
|
||||
}
|
||||
|
||||
impl Chip8System {
|
||||
|
||||
}
|
||||
|
||||
trait CpuInstruction {
|
||||
fn execute(input: Chip8System) -> Chip8System;
|
||||
}
|
||||
@ -7,7 +7,6 @@ pub struct Chip8Display {
|
||||
impl Chip8Display {
|
||||
pub fn tick(self: &Self) {
|
||||
println!("Ticking the display");
|
||||
|
||||
}
|
||||
|
||||
pub fn render_chip8_display(to_render: Chip8Display) {
|
||||
|
||||
@ -5,13 +5,15 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
trevors_chip8_core = { path = "../chip8_core" }
|
||||
easy-imgui-sys = { version = "=0.6.0" }
|
||||
easy-imgui = { version = "=0.6.0" }
|
||||
easy-imgui-renderer = { version = "=0.6.0" }
|
||||
easy-imgui-window = { version = "0.6.0" }
|
||||
log ="0.4"
|
||||
glutin = "0.32"
|
||||
glutin-winit = { version = "0.5", optional = true }
|
||||
raw-window-handle = "0.6"
|
||||
arboard = { version = "3", optional = true, default-features = false }
|
||||
winit = { version = "0.30", features = ["x11", "mint"] }
|
||||
copypasta = "0.10.1"
|
||||
glium = { version = "0.34.0", default-features = true }
|
||||
image = "0.25.2"
|
||||
imgui = { version = "0.12.0", features = ["tables-api"] }
|
||||
imgui-glium-renderer = "0.12.0"
|
||||
imgui-winit-support = "0.12.0"
|
||||
|
||||
15
chip8_toy/src/app.rs
Normal file
15
chip8_toy/src/app.rs
Normal file
@ -0,0 +1,15 @@
|
||||
/*
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct App;
|
||||
|
||||
impl UiBuilder for App {
|
||||
fn do_ui(&mut self, ui: &Ui<Self>) {
|
||||
#[cfg(feature = "docking")]
|
||||
{
|
||||
ui.dock_space_over_viewport(0, imgui::DockNodeFlags::None);
|
||||
}
|
||||
ui.show_demo_window(None);
|
||||
|
||||
}
|
||||
}*/
|
||||
@ -1,8 +1,6 @@
|
||||
use easy_imgui::{Ui, UiBuilder};
|
||||
use easy_imgui_window::{MainWindow, MainWindowWithRenderer};
|
||||
use winit::{application::ApplicationHandler, event_loop::ActiveEventLoop, window::{Window, WindowAttributes}};
|
||||
|
||||
|
||||
/*
|
||||
pub struct Chip8Display {
|
||||
pub window: MainWindowWithRenderer,
|
||||
}
|
||||
@ -54,15 +52,6 @@ impl ApplicationHandler for Chip8AppHandler {
|
||||
if window.main_window().window().id() != window_id {
|
||||
continue;
|
||||
}
|
||||
let res = window.window_event(
|
||||
&mut self,
|
||||
&event,
|
||||
easy_imgui_window::EventFlags::empty(),
|
||||
);
|
||||
if res.window_closed {
|
||||
event_loop.exit();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
fn new_events(&mut self, _event_loop: &ActiveEventLoop, _cause: winit::event::StartCause) {
|
||||
@ -77,3 +66,4 @@ impl ApplicationHandler for Chip8AppHandler {
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
@ -1,3 +1,7 @@
|
||||
pub mod app;
|
||||
|
||||
pub mod gui {
|
||||
pub mod display;
|
||||
}
|
||||
|
||||
pub mod support;
|
||||
@ -1,87 +1,128 @@
|
||||
use easy_imgui_window::{
|
||||
easy_imgui as imgui,
|
||||
easy_imgui_renderer::Renderer,
|
||||
winit::{
|
||||
event_loop::{ActiveEventLoop, EventLoop},
|
||||
window::Window,
|
||||
},
|
||||
MainWindow, MainWindowWithRenderer,
|
||||
};
|
||||
use trevors_chip8_toy::gui::display::{Chip8App, Chip8AppHandler};
|
||||
use std::rc::Rc;
|
||||
use trevors_chip8_core::parts::CPU::fill_chip8_instructions;
|
||||
use trevors_chip8_toy::support;
|
||||
use winit::{event::WindowEvent, event_loop::EventLoop};
|
||||
|
||||
use imgui::*;
|
||||
/*
|
||||
pub fn display_instruction_table(current_index: u32, ui: &Ui<App>) -> u32 {
|
||||
let to_display = fill_chip8_instructions();
|
||||
let mut return_index = current_index;
|
||||
let current_instruction = to_display.get(return_index as usize).unwrap();
|
||||
ui.text("Instructions");
|
||||
ui.set_next_item_width(100f32);
|
||||
ui.with_group(|| {
|
||||
ui.text(format!("{}", current_instruction.description).as_str());
|
||||
if current_instruction.arguments.is_empty() {
|
||||
ui.text("No Parameters");
|
||||
} else {
|
||||
for current_parameter in current_instruction.arguments {
|
||||
ui.text(format!("{}", current_parameter.mask).as_str());
|
||||
}
|
||||
}
|
||||
ui.text(format!("{:#}", current_instruction.arguments).as_str());
|
||||
ui.text("This is where cool stuff happens part 2");
|
||||
});
|
||||
ui.same_line();
|
||||
ui.with_group(|| {
|
||||
for (index, current) in to_display.iter().enumerate() {
|
||||
if ui
|
||||
.selectable_config(current.id.clone())
|
||||
.selected(index == return_index as usize)
|
||||
.build()
|
||||
{
|
||||
return_index = index as u32;
|
||||
}
|
||||
}
|
||||
});
|
||||
return_index
|
||||
}
|
||||
*/
|
||||
|
||||
fn chip8_instructions(initial_index: u32, ui: &mut Ui) -> u32 {
|
||||
let to_display = fill_chip8_instructions();
|
||||
let mut return_index = initial_index;
|
||||
let instruction_to_display = to_display.get(return_index as usize).unwrap();
|
||||
let instruction_window = ui
|
||||
.window("Instructions")
|
||||
.size([400.0, 400.0], Condition::FirstUseEver)
|
||||
.build(|| {
|
||||
ui.text("This should be my current instruction example");
|
||||
ui.same_line();
|
||||
ui.set_next_item_width(-1.0f32);
|
||||
for (index, instruction) in to_display.iter().enumerate() {
|
||||
if ui
|
||||
.selectable_config(instruction.id.to_string())
|
||||
.selected(index as u32 == return_index)
|
||||
.build()
|
||||
{
|
||||
return_index = index as u32;
|
||||
println!("RETURN INDEX {return_index}");
|
||||
}
|
||||
}
|
||||
});
|
||||
return_index
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut value = 0;
|
||||
let choices = ["test test this is 1", "test test this is 2"];
|
||||
|
||||
let mut selected_instruction = 0;
|
||||
|
||||
support::simple_init(file!(), move |_, ui| {
|
||||
selected_instruction = chip8_instructions(selected_instruction, ui);
|
||||
|
||||
ui.window("Hello world")
|
||||
.size([300.0, 110.0], Condition::FirstUseEver)
|
||||
.build(|| {
|
||||
ui.text_wrapped("Hello world!");
|
||||
if ui.button(choices[value]) {
|
||||
value += 1;
|
||||
value %= 2;
|
||||
}
|
||||
|
||||
ui.button("This...is...imgui-rs!");
|
||||
ui.separator();
|
||||
let mouse_pos = ui.io().mouse_pos;
|
||||
ui.text(format!(
|
||||
"Mouse Position: ({:.1},{:.1})",
|
||||
mouse_pos[0], mouse_pos[1]
|
||||
));
|
||||
});
|
||||
});
|
||||
}
|
||||
/*
|
||||
fn main() {
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
|
||||
let mut main = Chip8AppHandler {
|
||||
windows: vec![],
|
||||
app: Chip8App,
|
||||
};
|
||||
let mut main = AppHandler::<App>::default();
|
||||
main.attributes().title = String::from("Example-420");
|
||||
|
||||
event_loop.run_app(&mut main).unwrap();
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct AppHandler {
|
||||
windows: Vec<MainWindowWithRenderer>,
|
||||
app: App,
|
||||
pub struct App {
|
||||
pub selected_instruction_index: u32,
|
||||
}
|
||||
|
||||
impl winit::application::ApplicationHandler for AppHandler {
|
||||
fn suspended(&mut self, _event_loop: &ActiveEventLoop) {
|
||||
self.windows.clear();
|
||||
}
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
let wattr_1 = Window::default_attributes().with_title("Example #1");
|
||||
let main_window_1 = MainWindow::new::<()>(&event_loop, wattr_1).unwrap();
|
||||
let mut window_1 = MainWindowWithRenderer::new(main_window_1);
|
||||
impl App {}
|
||||
|
||||
let wattr_2 = Window::default_attributes().with_title("Example #2");
|
||||
let main_window_2 = MainWindow::new::<()>(&event_loop, wattr_2).unwrap();
|
||||
// The GL context can be reused, but the imgui context cannot
|
||||
let mut renderer_2 = Renderer::new(Rc::clone(window_1.renderer().gl_context())).unwrap();
|
||||
renderer_2.set_background_color(Some(imgui::Color::GREEN));
|
||||
let window_2 = MainWindowWithRenderer::new_with_renderer(main_window_2, renderer_2);
|
||||
|
||||
self.windows.push(window_1);
|
||||
self.windows.push(window_2);
|
||||
impl Application for App {
|
||||
type UserEvent = ();
|
||||
type Data = ();
|
||||
fn new(_: Args<()>) -> App {
|
||||
App {
|
||||
selected_instruction_index: 0,
|
||||
}
|
||||
fn window_event(
|
||||
&mut self,
|
||||
event_loop: &ActiveEventLoop,
|
||||
window_id: winit::window::WindowId,
|
||||
event: winit::event::WindowEvent,
|
||||
) {
|
||||
for window in &mut self.windows {
|
||||
if window.main_window().window().id() != window_id {
|
||||
continue;
|
||||
}
|
||||
let res = window.window_event(
|
||||
&mut self.app,
|
||||
&event,
|
||||
easy_imgui_window::EventFlags::empty(),
|
||||
);
|
||||
fn window_event(&mut self, args: Args<()>, _event: WindowEvent, res: EventResult) {
|
||||
if res.window_closed {
|
||||
event_loop.exit();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
fn new_events(&mut self, _event_loop: &ActiveEventLoop, _cause: winit::event::StartCause) {
|
||||
for window in &mut self.windows {
|
||||
window.new_events();
|
||||
}
|
||||
}
|
||||
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
|
||||
for window in &mut self.windows {
|
||||
window.about_to_wait();
|
||||
args.event_loop.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct App;
|
||||
|
||||
impl imgui::UiBuilder for App {
|
||||
fn do_ui(&mut self, ui: &imgui::Ui<Self>) {
|
||||
#[cfg(feature = "docking")]
|
||||
@ -89,6 +130,14 @@ impl imgui::UiBuilder for App {
|
||||
ui.dock_space_over_viewport(0, imgui::DockNodeFlags::None);
|
||||
}
|
||||
|
||||
let x = ui.collapsing_header_config("collapsing header");
|
||||
|
||||
ui.with_group(|| {
|
||||
self.selected_instruction_index =
|
||||
display_instruction_table(self.selected_instruction_index, ui);
|
||||
});
|
||||
|
||||
ui.show_demo_window(None);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
18
chip8_toy/src/support/clipboard.rs
Normal file
18
chip8_toy/src/support/clipboard.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use copypasta::{ClipboardContext, ClipboardProvider};
|
||||
use imgui::ClipboardBackend;
|
||||
|
||||
pub struct ClipboardSupport(pub ClipboardContext);
|
||||
|
||||
pub fn init() -> Option<ClipboardSupport> {
|
||||
ClipboardContext::new().ok().map(ClipboardSupport)
|
||||
}
|
||||
|
||||
impl ClipboardBackend for ClipboardSupport {
|
||||
fn get(&mut self) -> Option<String> {
|
||||
self.0.get_contents().ok()
|
||||
}
|
||||
fn set(&mut self, text: &str) {
|
||||
// ignore errors?
|
||||
let _ = self.0.set_contents(text.to_owned());
|
||||
}
|
||||
}
|
||||
164
chip8_toy/src/support/mod.rs
Normal file
164
chip8_toy/src/support/mod.rs
Normal file
@ -0,0 +1,164 @@
|
||||
use glium::glutin::surface::WindowSurface;
|
||||
use glium::{Display, Surface};
|
||||
use imgui::{Context, FontConfig, FontGlyphRanges, FontSource, Ui};
|
||||
use imgui_glium_renderer::Renderer;
|
||||
use imgui_winit_support::winit::dpi::LogicalSize;
|
||||
use imgui_winit_support::winit::event::{Event, WindowEvent};
|
||||
use imgui_winit_support::winit::event_loop::EventLoop;
|
||||
use imgui_winit_support::winit::window::WindowBuilder;
|
||||
use imgui_winit_support::{HiDpiMode, WinitPlatform};
|
||||
use std::path::Path;
|
||||
use std::time::Instant;
|
||||
|
||||
mod clipboard;
|
||||
|
||||
pub const FONT_SIZE: f32 = 13.0;
|
||||
|
||||
#[allow(dead_code)] // annoyingly, RA yells that this is unusued
|
||||
pub fn simple_init<F: FnMut(&mut bool, &mut Ui) + 'static>(title: &str, run_ui: F) {
|
||||
init_with_startup(title, |_, _, _| {}, run_ui);
|
||||
}
|
||||
|
||||
pub fn init_with_startup<FInit, FUi>(title: &str, mut startup: FInit, mut run_ui: FUi)
|
||||
where
|
||||
FInit: FnMut(&mut Context, &mut Renderer, &Display<WindowSurface>) + 'static,
|
||||
FUi: FnMut(&mut bool, &mut Ui) + 'static,
|
||||
{
|
||||
let mut imgui = create_context();
|
||||
|
||||
let title = match Path::new(&title).file_name() {
|
||||
Some(file_name) => file_name.to_str().unwrap(),
|
||||
None => title,
|
||||
};
|
||||
let event_loop = EventLoop::new().expect("Failed to create EventLoop");
|
||||
|
||||
let builder = WindowBuilder::new()
|
||||
.with_title(title)
|
||||
.with_inner_size(LogicalSize::new(1024, 768));
|
||||
let (window, display) = glium::backend::glutin::SimpleWindowBuilder::new()
|
||||
.set_window_builder(builder)
|
||||
.build(&event_loop);
|
||||
let mut renderer = Renderer::init(&mut imgui, &display).expect("Failed to initialize renderer");
|
||||
|
||||
if let Some(backend) = clipboard::init() {
|
||||
imgui.set_clipboard_backend(backend);
|
||||
} else {
|
||||
eprintln!("Failed to initialize clipboard");
|
||||
}
|
||||
|
||||
let mut platform = WinitPlatform::init(&mut imgui);
|
||||
{
|
||||
let dpi_mode = if let Ok(factor) = std::env::var("IMGUI_EXAMPLE_FORCE_DPI_FACTOR") {
|
||||
// Allow forcing of HiDPI factor for debugging purposes
|
||||
match factor.parse::<f64>() {
|
||||
Ok(f) => HiDpiMode::Locked(f),
|
||||
Err(e) => panic!("Invalid scaling factor: {}", e),
|
||||
}
|
||||
} else {
|
||||
HiDpiMode::Default
|
||||
};
|
||||
|
||||
platform.attach_window(imgui.io_mut(), &window, dpi_mode);
|
||||
}
|
||||
|
||||
let mut last_frame = Instant::now();
|
||||
|
||||
startup(&mut imgui, &mut renderer, &display);
|
||||
|
||||
event_loop
|
||||
.run(move |event, window_target| match event {
|
||||
Event::NewEvents(_) => {
|
||||
let now = Instant::now();
|
||||
imgui.io_mut().update_delta_time(now - last_frame);
|
||||
last_frame = now;
|
||||
}
|
||||
Event::AboutToWait => {
|
||||
platform
|
||||
.prepare_frame(imgui.io_mut(), &window)
|
||||
.expect("Failed to prepare frame");
|
||||
window.request_redraw();
|
||||
}
|
||||
Event::WindowEvent {
|
||||
event: WindowEvent::RedrawRequested,
|
||||
..
|
||||
} => {
|
||||
let ui = imgui.frame();
|
||||
|
||||
let mut run = true;
|
||||
run_ui(&mut run, ui);
|
||||
if !run {
|
||||
window_target.exit();
|
||||
}
|
||||
|
||||
let mut target = display.draw();
|
||||
target.clear_color_srgb(1.0, 1.0, 1.0, 1.0);
|
||||
platform.prepare_render(ui, &window);
|
||||
let draw_data = imgui.render();
|
||||
renderer
|
||||
.render(&mut target, draw_data)
|
||||
.expect("Rendering failed");
|
||||
target.finish().expect("Failed to swap buffers");
|
||||
}
|
||||
Event::WindowEvent {
|
||||
event: WindowEvent::Resized(new_size),
|
||||
..
|
||||
} => {
|
||||
if new_size.width > 0 && new_size.height > 0 {
|
||||
display.resize((new_size.width, new_size.height));
|
||||
}
|
||||
platform.handle_event(imgui.io_mut(), &window, &event);
|
||||
}
|
||||
Event::WindowEvent {
|
||||
event: WindowEvent::CloseRequested,
|
||||
..
|
||||
} => window_target.exit(),
|
||||
event => {
|
||||
platform.handle_event(imgui.io_mut(), &window, &event);
|
||||
}
|
||||
})
|
||||
.expect("EventLoop error");
|
||||
}
|
||||
|
||||
/// Creates the imgui context
|
||||
pub fn create_context() -> imgui::Context {
|
||||
let mut imgui = Context::create();
|
||||
// Fixed font size. Note imgui_winit_support uses "logical
|
||||
// pixels", which are physical pixels scaled by the devices
|
||||
// scaling factor. Meaning, 13.0 pixels should look the same size
|
||||
// on two different screens, and thus we do not need to scale this
|
||||
// value (as the scaling is handled by winit)
|
||||
imgui.fonts().add_font(&[
|
||||
FontSource::TtfData {
|
||||
data: include_bytes!("../../../resources/Roboto-Regular.ttf"),
|
||||
size_pixels: FONT_SIZE,
|
||||
config: Some(FontConfig {
|
||||
// As imgui-glium-renderer isn't gamma-correct with
|
||||
// it's font rendering, we apply an arbitrary
|
||||
// multiplier to make the font a bit "heavier". With
|
||||
// default imgui-glow-renderer this is unnecessary.
|
||||
rasterizer_multiply: 1.5,
|
||||
// Oversampling font helps improve text rendering at
|
||||
// expense of larger font atlas texture.
|
||||
oversample_h: 4,
|
||||
oversample_v: 4,
|
||||
..FontConfig::default()
|
||||
}),
|
||||
},
|
||||
FontSource::TtfData {
|
||||
data: include_bytes!("../../../resources/mplus-1p-regular.ttf"),
|
||||
size_pixels: FONT_SIZE,
|
||||
config: Some(FontConfig {
|
||||
// Oversampling font helps improve text rendering at
|
||||
// expense of larger font atlas texture.
|
||||
oversample_h: 4,
|
||||
oversample_v: 4,
|
||||
// Range of glyphs to rasterize
|
||||
glyph_ranges: FontGlyphRanges::japanese(),
|
||||
..FontConfig::default()
|
||||
}),
|
||||
},
|
||||
]);
|
||||
imgui.set_ini_filename(None);
|
||||
|
||||
imgui
|
||||
}
|
||||
8
emma/Cargo.toml
Normal file
8
emma/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "emmaemu"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
autobenches = true
|
||||
|
||||
[dependencies]
|
||||
ratatui = "0.28.0"
|
||||
516
emma/src/bin/emma.rs
Normal file
516
emma/src/bin/emma.rs
Normal file
@ -0,0 +1,516 @@
|
||||
use std::io::{stdout, Result};
|
||||
|
||||
use font::load_fonts_into_memory;
|
||||
use ratatui::{
|
||||
backend::CrosstermBackend,
|
||||
crossterm::{
|
||||
event::{self, KeyCode, KeyEventKind},
|
||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||
ExecutableCommand,
|
||||
},
|
||||
layout::{Alignment, Rect},
|
||||
style::Stylize,
|
||||
widgets::{List, Paragraph},
|
||||
Frame, Terminal,
|
||||
};
|
||||
|
||||
// nnn or addr - A 12-bit value, the lowest 12 bits of the instruction
|
||||
pub fn read_addr_from_instruction(instruction_to_read_from: u16) -> u16 {
|
||||
instruction_to_read_from & 0b0000111111111111
|
||||
}
|
||||
|
||||
// n or nibble - A 4-bit value, the lowest 4 bits of the instruction
|
||||
pub fn read_nibble_from_instruction(instruction_to_read_from: u16) -> u16 {
|
||||
instruction_to_read_from & 0b0000000000001111
|
||||
}
|
||||
|
||||
// x - A 4-bit value, the lower 4 bits of the high byte of the instruction
|
||||
pub fn read_x_from_instruction(instruction_to_read_from: u16) -> u16 {
|
||||
(instruction_to_read_from & 0b0000111100000000).rotate_right(8)
|
||||
}
|
||||
|
||||
// y - A 4-bit value, the upper 4 bits of the low byte of the instruction
|
||||
pub fn read_y_from_instruction(instruction_to_read_from: u16) -> u16 {
|
||||
(instruction_to_read_from & 0b0000000011110000).rotate_right(4)
|
||||
}
|
||||
|
||||
// kk or byte - An 8-bit value, the lowest 8 bits of the instruction
|
||||
pub fn read_byte_from_instruction(instruction_to_read_from: u16) -> u16 {
|
||||
(instruction_to_read_from & 0b0000000011111111)
|
||||
}
|
||||
|
||||
mod font;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Chip8CpuData {
|
||||
pub pc: u16,
|
||||
pub sp: u8,
|
||||
pub memory: [u8; CHIP8_MEMORY_SIZE as usize],
|
||||
pub video_memory: [bool; CHIP8_VIDEO_MEMORY],
|
||||
pub registers: [u8; 16],
|
||||
pub sound_timer: u8,
|
||||
pub delay_timer: u8,
|
||||
pub i_register: u16,
|
||||
}
|
||||
const CHIP8_REGISTER_COUNT: i32 = 16;
|
||||
const CHIP8_MEMORY_SIZE: i32 = 2048i32;
|
||||
const CHIP8_VIDEO_WIDTH: i32 = 64i32;
|
||||
const CHIP8_VIDEO_HEIGHT: i32 = 32i32;
|
||||
const CHIP8_VIDEO_MEMORY: usize = (CHIP8_VIDEO_HEIGHT * CHIP8_VIDEO_WIDTH) as usize;
|
||||
const CHIP8_ROM_SIZE: usize = 512;
|
||||
|
||||
impl Default for Chip8CpuData {
|
||||
fn default() -> Self {
|
||||
let mut memory: [u8; 2048] = [0; 2048];
|
||||
|
||||
memory = load_fonts_into_memory(memory);
|
||||
|
||||
dump_memory_to_console(memory);
|
||||
|
||||
Self {
|
||||
pc: 0x0200,
|
||||
sp: 0x00,
|
||||
memory: [0; 2048],
|
||||
video_memory: [false; CHIP8_VIDEO_MEMORY],
|
||||
registers: [0; CHIP8_REGISTER_COUNT as usize],
|
||||
sound_timer: 0xFF,
|
||||
delay_timer: 0xFF,
|
||||
i_register: 0x0000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Chip8CpuData {
|
||||
pub fn new() -> Self {
|
||||
Chip8CpuData::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn system_memory_to_text_render(data_to_dump: [u8; 2048]) -> String {
|
||||
let mut to_return = String::new();
|
||||
|
||||
for i in 0..256 {
|
||||
to_return += &format!("{:x}\t", data_to_dump[i as usize]).to_string();
|
||||
|
||||
if ((i + 1) % CHIP8_REGISTER_COUNT) == 0 {
|
||||
to_return += "\n";
|
||||
}
|
||||
}
|
||||
|
||||
to_return
|
||||
}
|
||||
|
||||
fn dump_memory_to_console(data_to_dump: [u8; 2048]) {
|
||||
println!("STARTING TO DUMP MEMORY TO CONSOLE");
|
||||
println!("{}", system_memory_to_text_render(data_to_dump));
|
||||
println!("DONE DUMPING!");
|
||||
panic!("DONE DUMPING");
|
||||
}
|
||||
|
||||
fn display_data_to_text_render(data_display: [bool; 2048]) -> String {
|
||||
let mut output = String::new();
|
||||
for row in 0..32 {
|
||||
let row_offset = row * 32;
|
||||
for column in 0..64 {
|
||||
let data_position = row_offset + column;
|
||||
println!("DP {} {} {} {}", data_position, row, row_offset, column);
|
||||
output += if data_display[data_position] {
|
||||
"*"
|
||||
} else {
|
||||
" "
|
||||
};
|
||||
}
|
||||
output += "\n";
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
fn render_display(display_data: [bool; 2048]) {
|
||||
println!("{}", display_data_to_text_render(display_data));
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Chip8CpuStates {
|
||||
WaitingForInstruction,
|
||||
ExecutingInstruction,
|
||||
Error,
|
||||
}
|
||||
|
||||
struct Chip8CpuState {
|
||||
state: Chip8CpuStates,
|
||||
cpu: Chip8CpuData
|
||||
}
|
||||
|
||||
enum Chip8CpuInstructions {
|
||||
SysAddr(u16), // 0x0nnn Exit to System Call
|
||||
CLS, // 0x00E0 Clear Screen
|
||||
RET, // 0x00EE Return from Subroutine
|
||||
JpAddr(u16), // 0x1nnn Jump to Address
|
||||
CallAddr(u16), // 0x2nnn Call Subroutine
|
||||
SeVxByte(u16, u16), // 0x3xkk Skip next instruction if Vx = kk.
|
||||
SneVxByte(u16, u16), // 0x4xkk Skip next instruction if Vx != kk
|
||||
SeVxVy(u16, u16), // 0x5xy0 Skip next instruction if Vx == Vy
|
||||
LdVxByte(u16, u16), // 0x6xkk Set Vx = kk
|
||||
AddVxByte(u16, u16), // 0x7xkk Set Vx = Vx + kk
|
||||
LdVxVy(u16, u16), // 0x8xy0 Set value of Vy in Vx
|
||||
OrVxVy(u16, u16), // 0x8xy1 Set Vx = Vx OR Vy
|
||||
AndVxVy(u16, u16), // 0x8xy2 Set Vx = Vx AND Vy
|
||||
XorVxVy(u16, u16), // 0x8xy3 Set Vx = Vx XOR Vy
|
||||
AddVxVy(u16, u16), // 0x8xy4 Set Vx = Vx + Vy (SET VF on Carry)
|
||||
SubVxVy(u16, u16), // 0x8xy5 Set Vx = Vx - Vy (Set VF NOT Borrow)
|
||||
ShrVxVy(u16, u16), // 0x8xy6 Set Vx = Vx SHR 1 (Shift Rotated Right 1)
|
||||
SubnVxVy(u16, u16), // 0x8xy7 Set Vx = Vy - Vx (Set VF NOT Borrow)
|
||||
ShlVxVy(u16, u16), // 0x8xyE Shift Left
|
||||
SneVxVy(u16, u16), // 0x9xy0 Skip next instruction if Vx != Vy
|
||||
LdIAddr(u16), // 0xAnnn VI = nnn
|
||||
JpV0Addr(u16), // 0xBnnn Jump to nnn+V0
|
||||
RndVxByte(u16, u16), // 0xCxkk Vx = random byte AND kk
|
||||
DrawVxVyNibble(u16, u16, u16), // 0xDxyn Display N byte sprite starting at Vx to Vy
|
||||
SkpVx(u16), // 0xE09E Skip next instruction if key in Vx pressed
|
||||
SnkpVx(u16), // 0xE0A1 Skip next instruction if key in Vx NOT pressed
|
||||
LdVxDt(u16), // 0xFx07 Set Vx = Delay timer
|
||||
LdVxK(u16), // 0xFx0A Wait for key, put in Vx
|
||||
LdDtVx(u16), // 0xFx15 Set Delay Timer
|
||||
LdStVx(u16), // 0xFx18 Set Sount Timer
|
||||
AddIVx(u16), // 0xFx1E I = I + Vx
|
||||
LdFVu(u16), // 0xFx29 Set I = Location of sprite for Digit Vx
|
||||
LdBVx(u16), // 0xFx33 Store BCD of Vx in I, I+1, I+2
|
||||
LdIVx(u16), // 0xFx55 Store V0 to Vx in memory starting at I
|
||||
LdVxI(u16), // 0xFx65 Load V0 to Vx in memory starting at I
|
||||
XXXXERRORINSTRUCTION,
|
||||
}
|
||||
|
||||
fn bytes_to_instruction(to_read: u16) -> Chip8CpuInstructions {
|
||||
let mut decoded_instruction = Chip8CpuInstructions::XXXXERRORINSTRUCTION;
|
||||
|
||||
match to_read {
|
||||
0x00E0 => {
|
||||
// 00E0 - CLS
|
||||
// Clear the display.
|
||||
decoded_instruction = Chip8CpuInstructions::CLS;
|
||||
}
|
||||
0x00EE => {
|
||||
// 00EE - RET
|
||||
// Return from a subroutine.
|
||||
|
||||
decoded_instruction = Chip8CpuInstructions::RET;
|
||||
}
|
||||
0x0000..=0x0FFF => {
|
||||
// 0nnn - SYS addr
|
||||
// Jump to a machine code routine at nnn.
|
||||
decoded_instruction =
|
||||
Chip8CpuInstructions::SysAddr(read_addr_from_instruction(to_read));
|
||||
}
|
||||
0x1000..=0x1FFF => {
|
||||
// 1nnn - JP addr
|
||||
// Jump to location nnn.
|
||||
decoded_instruction = Chip8CpuInstructions::JpAddr(read_addr_from_instruction(to_read));
|
||||
}
|
||||
0x2000..=0x2FFF => {
|
||||
// 2nnn - CALL addr
|
||||
// Call subroutine at nnn.
|
||||
decoded_instruction =
|
||||
Chip8CpuInstructions::CallAddr(read_addr_from_instruction(to_read));
|
||||
}
|
||||
0x3000..=0x3FFF => {
|
||||
// 3xkk - SE Vx, byte
|
||||
// Skip next instruction if Vx = kk.
|
||||
decoded_instruction = Chip8CpuInstructions::SeVxByte(
|
||||
read_x_from_instruction(to_read),
|
||||
read_byte_from_instruction(to_read),
|
||||
)
|
||||
}
|
||||
0x4000..=0x4FFF => {
|
||||
// 4xkk - SNE Vx, byte
|
||||
// Skip next instruction if Vx != kk.
|
||||
decoded_instruction = Chip8CpuInstructions::SneVxByte(
|
||||
read_x_from_instruction(to_read),
|
||||
read_byte_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0x5000..=0x5FF0 => {
|
||||
// 5xy0 - SE Vx, Vy
|
||||
// Skip next instruction if Vx = Vy.
|
||||
decoded_instruction = Chip8CpuInstructions::SeVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
)
|
||||
}
|
||||
0x6000..=0x6FFF => {
|
||||
// 6xkk - LD Vx, byte
|
||||
// Set Vx = kk.
|
||||
decoded_instruction = Chip8CpuInstructions::LdVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_byte_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0x7000..=0x7FFF => {
|
||||
// ADD Vx, Byte
|
||||
decoded_instruction = Chip8CpuInstructions::AddVxByte(
|
||||
read_x_from_instruction(to_read),
|
||||
read_byte_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0x8000..=0x8FFE => {
|
||||
// 0x8000 Series
|
||||
let last_nibble = to_read | 0x8000;
|
||||
match last_nibble {
|
||||
0x0 => {
|
||||
// LD Vx, Vy
|
||||
decoded_instruction = Chip8CpuInstructions::LdVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0x1 => {
|
||||
// OR Vx, Vy
|
||||
decoded_instruction = Chip8CpuInstructions::OrVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0x2 => {
|
||||
// AND Vx, Vy
|
||||
decoded_instruction = Chip8CpuInstructions::AndVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0x3 => {
|
||||
// XOR Vx, Vy
|
||||
decoded_instruction = Chip8CpuInstructions::XorVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0x4 => {
|
||||
// AND Vx, Vy
|
||||
decoded_instruction = Chip8CpuInstructions::AndVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0x5 => {
|
||||
// SUB Vx, Vy
|
||||
decoded_instruction = Chip8CpuInstructions::SubnVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0x6 => {
|
||||
// SHR Vx, {, Vy }
|
||||
decoded_instruction = Chip8CpuInstructions::ShrVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0x7 => {
|
||||
// SUBN Vx, Vy
|
||||
decoded_instruction = Chip8CpuInstructions::SubnVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0xE => {
|
||||
// SHL Vx, {, Vy}
|
||||
decoded_instruction = Chip8CpuInstructions::ShlVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
panic!("UNABLE TO DECODE 0x8000 SERIES INSTRUCTION");
|
||||
}
|
||||
}
|
||||
}
|
||||
0x9000..=0x9FF0 => {
|
||||
// SNE Vx, Vy
|
||||
decoded_instruction = Chip8CpuInstructions::SneVxVy(
|
||||
read_x_from_instruction(to_read),
|
||||
read_y_from_instruction(to_read),
|
||||
);
|
||||
}
|
||||
0xA000..=0xAFFF => {
|
||||
// LD I, Addr
|
||||
decoded_instruction =
|
||||
Chip8CpuInstructions::LdIAddr(read_addr_from_instruction(to_read));
|
||||
}
|
||||
0xB000..=0xBFFF => {
|
||||
// JP V0, Addr
|
||||
}
|
||||
0xC000..=0xCFFF => {
|
||||
// RND Vx, byte
|
||||
}
|
||||
0xD000..0xDFFF => {
|
||||
// DRAW Vx, Vy, nibble
|
||||
}
|
||||
0xE09E..=0xEFA1 => {}
|
||||
_ => {
|
||||
panic!("UNABLE TO DECODE INSTRUCTION")
|
||||
}
|
||||
}
|
||||
|
||||
return decoded_instruction;
|
||||
}
|
||||
|
||||
impl Chip8CpuState {
|
||||
fn execute(&mut self, event: Chip8CpuInstructions) {
|
||||
match (self.state.clone(), event) {
|
||||
(Chip8CpuStates::WaitingForInstruction, Chip8CpuInstructions::SysAddr(new_address)) => {
|
||||
self.cpu.pc = new_address;
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Chip8System {
|
||||
pub system_memory: [u8; 2048],
|
||||
pub rom: [u8; 512],
|
||||
pub sound_timer: u8,
|
||||
pub system_timer: u8,
|
||||
}
|
||||
|
||||
impl Default for Chip8System {
|
||||
fn default() -> Self {
|
||||
// init by loading the fonts...
|
||||
let mut working_system_memory: [u8; CHIP8_MEMORY_SIZE as usize] =
|
||||
[0; CHIP8_MEMORY_SIZE as usize];
|
||||
|
||||
// Load fonts to memory
|
||||
working_system_memory = load_fonts_into_memory(working_system_memory);
|
||||
|
||||
Self {
|
||||
system_memory: working_system_memory,
|
||||
rom: [0; CHIP8_ROM_SIZE],
|
||||
sound_timer: 0,
|
||||
system_timer: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Chip8System {
|
||||
fn add_fonts_to_memory(
|
||||
to_add_fonts_to: [u8; CHIP8_MEMORY_SIZE as usize],
|
||||
) -> [u8; CHIP8_MEMORY_SIZE as usize] {
|
||||
panic!("DONT USE THIS USE \"load_fonts_into_memory\" INSTEAD");
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Chip8System {
|
||||
system_memory: [0; CHIP8_MEMORY_SIZE as usize],
|
||||
rom: [0; CHIP8_ROM_SIZE as usize],
|
||||
sound_timer: 0,
|
||||
system_timer: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
menu_items: Vec<String>,
|
||||
selected_menu_item: i32,
|
||||
system: Chip8CpuData,
|
||||
}
|
||||
|
||||
impl Default for AppState {
|
||||
fn default() -> Self {
|
||||
println!("Creating new AppState");
|
||||
Self {
|
||||
menu_items: vec![
|
||||
"Step CPU <F5>".into(),
|
||||
"Reset CPU <F12>".into(),
|
||||
"Item 3".into(),
|
||||
],
|
||||
selected_menu_item: 0,
|
||||
system: Chip8CpuData {
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// stdout().execute(EnterAlternateScreen)?;
|
||||
// enable_raw_mode()?;
|
||||
// let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
|
||||
// terminal.clear()?;
|
||||
|
||||
let app = AppState::default();
|
||||
|
||||
loop {
|
||||
// Draw Ui...
|
||||
|
||||
// terminal.draw(|frame| {
|
||||
// render_menu_list(app.clone(), frame);
|
||||
// render_cpu_state(app.system.clone(), frame);
|
||||
// })?;
|
||||
|
||||
// ...handle Events.
|
||||
if event::poll(std::time::Duration::from_millis(16))? {
|
||||
if let event::Event::Key(key) = event::read()? {
|
||||
match key.kind {
|
||||
KeyEventKind::Press => match key.code {
|
||||
KeyCode::F(5) => {
|
||||
println!("Execute Next Instruction");
|
||||
}
|
||||
KeyCode::F(12) => {
|
||||
println!("Resetting CPU");
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// stdout().execute(LeaveAlternateScreen)?;
|
||||
// disable_raw_mode()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::*;
|
||||
#[test]
|
||||
fn blank_screen_renders_to_text() {
|
||||
let blank_data: [bool; CHIP8_VIDEO_MEMORY] = [false; CHIP8_VIDEO_MEMORY];
|
||||
let blank_screen = (" ".repeat(CHIP8_VIDEO_WIDTH.try_into().unwrap()) + "\n")
|
||||
.repeat(CHIP8_VIDEO_HEIGHT.try_into().unwrap());
|
||||
assert_eq!(display_data_to_text_render(blank_data), blank_screen);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filled_screen_renders_to_text() {
|
||||
let filled_data: [bool; CHIP8_VIDEO_MEMORY] = [true; CHIP8_VIDEO_MEMORY];
|
||||
let filled_screen = ("*".repeat(CHIP8_VIDEO_WIDTH.try_into().unwrap()) + "\n")
|
||||
.repeat(CHIP8_VIDEO_HEIGHT.try_into().unwrap());
|
||||
assert_eq!(display_data_to_text_render(filled_data), filled_screen);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grid_pattern_renders_to_text() {
|
||||
let mut grid_data: [bool; CHIP8_VIDEO_MEMORY] = [false; CHIP8_VIDEO_MEMORY];
|
||||
let mut expected_data = String::new();
|
||||
for i in 0..CHIP8_VIDEO_MEMORY {
|
||||
grid_data[i] = (i % 2 == 0);
|
||||
if i % 2 == 0 {
|
||||
grid_data[i] = true;
|
||||
expected_data += "*";
|
||||
} else {
|
||||
grid_data[i] = false;
|
||||
expected_data += " ";
|
||||
}
|
||||
if (i as i32 % CHIP8_VIDEO_WIDTH as i32 == CHIP8_VIDEO_WIDTH - 1) {
|
||||
expected_data += "\n";
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(display_data_to_text_render(grid_data), expected_data);
|
||||
}
|
||||
}
|
||||
55
emma/src/bin/font/mod.rs
Normal file
55
emma/src/bin/font/mod.rs
Normal file
@ -0,0 +1,55 @@
|
||||
use crate::CHIP8_MEMORY_SIZE;
|
||||
|
||||
|
||||
pub mod text_rendering;
|
||||
|
||||
pub const CHIP8FONT_0: [u8; 5] = [0xF0, 0x90, 0x90, 0x90, 0xF0];
|
||||
pub const CHIP8FONT_1: [u8; 5] = [0x20, 0x60, 0x20, 0x20, 0x70];
|
||||
pub const CHIP8FONT_2: [u8; 5] = [0xF0, 0x10, 0xF0, 0x80, 0xF0];
|
||||
pub const CHIP8FONT_3: [u8; 5] = [0xF0, 0x10, 0xF0, 0x10, 0xF0];
|
||||
pub const CHIP8FONT_4: [u8; 5] = [0x90, 0x90, 0xF0, 0x10, 0x10];
|
||||
pub const CHIP8FONT_5: [u8; 5] = [0xF0, 0x80, 0xF0, 0x10, 0xF0];
|
||||
pub const CHIP8FONT_6: [u8; 5] = [0xF0, 0x80, 0xF0, 0x90, 0xF0];
|
||||
pub const CHIP8FONT_7: [u8; 5] = [0xF0, 0x10, 0x20, 0x40, 0x40];
|
||||
pub const CHIP8FONT_8: [u8; 5] = [0xF0, 0x90, 0xF0, 0x90, 0xF0];
|
||||
pub const CHIP8FONT_9: [u8; 5] = [0xF0, 0x90, 0xF0, 0x10, 0xF0];
|
||||
pub const CHIP8FONT_A: [u8; 5] = [0xF0, 0x90, 0xF0, 0x90, 0x90];
|
||||
pub const CHIP8FONT_B: [u8; 5] = [0xE0, 0x90, 0xE0, 0x90, 0xE0];
|
||||
pub const CHIP8FONT_C: [u8; 5] = [0xF0, 0x80, 0x80, 0x80, 0xF0];
|
||||
pub const CHIP8FONT_D: [u8; 5] = [0xE0, 0x90, 0x90, 0x90, 0xE0];
|
||||
pub const CHIP8FONT_E: [u8; 5] = [0xF0, 0x80, 0xF0, 0x80, 0xF0];
|
||||
pub const CHIP8FONT_F: [u8; 5] = [0xF0, 0x80, 0xF0, 0x80, 0x80];
|
||||
|
||||
pub fn load_fonts_into_memory(to_add_fonts_to: [u8; CHIP8_MEMORY_SIZE as usize])
|
||||
-> [u8; CHIP8_MEMORY_SIZE as usize] {
|
||||
|
||||
let mut memory = to_add_fonts_to.clone();
|
||||
|
||||
let all_font_characters = [
|
||||
CHIP8FONT_0,
|
||||
CHIP8FONT_1,
|
||||
CHIP8FONT_2,
|
||||
CHIP8FONT_3,
|
||||
CHIP8FONT_4,
|
||||
CHIP8FONT_5,
|
||||
CHIP8FONT_6,
|
||||
CHIP8FONT_7,
|
||||
CHIP8FONT_8,
|
||||
CHIP8FONT_9,
|
||||
CHIP8FONT_A,
|
||||
CHIP8FONT_B,
|
||||
CHIP8FONT_C,
|
||||
CHIP8FONT_D,
|
||||
CHIP8FONT_E,
|
||||
CHIP8FONT_F,
|
||||
];
|
||||
|
||||
for (font_index, current_font) in all_font_characters.iter().enumerate() {
|
||||
for font_mem_offset in 0..=4 {
|
||||
let real_offset = font_index * 5 + font_mem_offset;
|
||||
memory[real_offset] = current_font[font_mem_offset];
|
||||
}
|
||||
}
|
||||
|
||||
memory
|
||||
}
|
||||
36
emma/src/bin/font/text_rendering.rs
Normal file
36
emma/src/bin/font/text_rendering.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use ratatui::{style::Stylize, widgets::{List, Paragraph}, Frame};
|
||||
|
||||
use crate::{AppState, Chip8CpuData, CHIP8_REGISTER_COUNT};
|
||||
|
||||
|
||||
pub fn render_cpu_state(cpu_state: Chip8CpuData, frame: &mut Frame) {
|
||||
let mut area = frame.area();
|
||||
|
||||
let mut current_state: Vec<String> = vec![];
|
||||
|
||||
current_state.push(format!("Delay Timer {:X}", cpu_state.delay_timer));
|
||||
current_state.push(format!("Sound Timer {:X}", cpu_state.sound_timer));
|
||||
current_state.push(format!("PC {:X}", cpu_state.pc));
|
||||
for i in 0 .. CHIP8_REGISTER_COUNT {
|
||||
current_state.push(format!("V{}: {}", i, cpu_state.registers[i as usize]));
|
||||
}
|
||||
|
||||
frame.render_widget(
|
||||
List::new(current_state).blue().on_white(), area)
|
||||
}
|
||||
|
||||
pub fn render_menu_list(state: AppState, frame: &mut Frame) {
|
||||
let mut area = frame.area();
|
||||
|
||||
frame.render_widget(List::new(state.menu_items).blue().on_white(), area);
|
||||
}
|
||||
|
||||
pub fn render_hello_world(frame: &mut Frame) {
|
||||
let area = frame.area();
|
||||
frame.render_widget(
|
||||
Paragraph::new("Hello Ratatui! (press 'q' to quit)")
|
||||
.white()
|
||||
.on_blue(),
|
||||
area,
|
||||
);
|
||||
}
|
||||
BIN
resources/Dokdo-Regular.ttf
Normal file
BIN
resources/Dokdo-Regular.ttf
Normal file
Binary file not shown.
93
resources/LICENSE-Dokdo.txt
Normal file
93
resources/LICENSE-Dokdo.txt
Normal file
@ -0,0 +1,93 @@
|
||||
Copyright (c) 2005-2017 FONTRIX. All Rights Reserved.
|
||||
|
||||
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
||||
This license is copied below, and is also available with a FAQ at:
|
||||
http://scripts.sil.org/OFL
|
||||
|
||||
|
||||
-----------------------------------------------------------
|
||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||
-----------------------------------------------------------
|
||||
|
||||
PREAMBLE
|
||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
||||
development of collaborative font projects, to support the font creation
|
||||
efforts of academic and linguistic communities, and to provide a free and
|
||||
open framework in which fonts may be shared and improved in partnership
|
||||
with others.
|
||||
|
||||
The OFL allows the licensed fonts to be used, studied, modified and
|
||||
redistributed freely as long as they are not sold by themselves. The
|
||||
fonts, including any derivative works, can be bundled, embedded,
|
||||
redistributed and/or sold with any software provided that any reserved
|
||||
names are not used by derivative works. The fonts and derivatives,
|
||||
however, cannot be released under any other type of license. The
|
||||
requirement for fonts to remain under this license does not apply
|
||||
to any document created using the fonts or their derivatives.
|
||||
|
||||
DEFINITIONS
|
||||
"Font Software" refers to the set of files released by the Copyright
|
||||
Holder(s) under this license and clearly marked as such. This may
|
||||
include source files, build scripts and documentation.
|
||||
|
||||
"Reserved Font Name" refers to any names specified as such after the
|
||||
copyright statement(s).
|
||||
|
||||
"Original Version" refers to the collection of Font Software components as
|
||||
distributed by the Copyright Holder(s).
|
||||
|
||||
"Modified Version" refers to any derivative made by adding to, deleting,
|
||||
or substituting -- in part or in whole -- any of the components of the
|
||||
Original Version, by changing formats or by porting the Font Software to a
|
||||
new environment.
|
||||
|
||||
"Author" refers to any designer, engineer, programmer, technical
|
||||
writer or other person who contributed to the Font Software.
|
||||
|
||||
PERMISSION & CONDITIONS
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
||||
redistribute, and sell modified and unmodified copies of the Font
|
||||
Software, subject to the following conditions:
|
||||
|
||||
1) Neither the Font Software nor any of its individual components,
|
||||
in Original or Modified Versions, may be sold by itself.
|
||||
|
||||
2) Original or Modified Versions of the Font Software may be bundled,
|
||||
redistributed and/or sold with any software, provided that each copy
|
||||
contains the above copyright notice and this license. These can be
|
||||
included either as stand-alone text files, human-readable headers or
|
||||
in the appropriate machine-readable metadata fields within text or
|
||||
binary files as long as those fields can be easily viewed by the user.
|
||||
|
||||
3) No Modified Version of the Font Software may use the Reserved Font
|
||||
Name(s) unless explicit written permission is granted by the corresponding
|
||||
Copyright Holder. This restriction only applies to the primary font name as
|
||||
presented to the users.
|
||||
|
||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||
Software shall not be used to promote, endorse or advertise any
|
||||
Modified Version, except to acknowledge the contribution(s) of the
|
||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||
permission.
|
||||
|
||||
5) The Font Software, modified or unmodified, in part or in whole,
|
||||
must be distributed entirely under this license, and must not be
|
||||
distributed under any other license. The requirement for fonts to
|
||||
remain under this license does not apply to any document created
|
||||
using the Font Software.
|
||||
|
||||
TERMINATION
|
||||
This license becomes null and void if any of the above conditions are
|
||||
not met.
|
||||
|
||||
DISCLAIMER
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
||||
201
resources/LICENSE-Roboto.txt
Normal file
201
resources/LICENSE-Roboto.txt
Normal file
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
BIN
resources/Lenna.jpg
Normal file
BIN
resources/Lenna.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
BIN
resources/Roboto-Regular.ttf
Normal file
BIN
resources/Roboto-Regular.ttf
Normal file
Binary file not shown.
BIN
resources/mplus-1p-regular.ttf
Normal file
BIN
resources/mplus-1p-regular.ttf
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user