CLEANUP: cleans up dependencies to use workspace version
CLEANUP: standardizes on pretty_env_logger NEWBIN: Adds gemmarat to use Ratatui as an interface CLEANUP: removes debugging display from computer.rs ENHANCEMENT: Constants for locations of test roms built from environment BUGFIX: SoundTimer was using i32 internally when it needs to be u8 CLEANUP: removes commented code used during diagnostics
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
use std::path::Path;
|
||||
use std::rc::Rc;
|
||||
use ratatui::prelude::*;
|
||||
use std::time::Duration;
|
||||
use color_eyre::{eyre::Context, Result};
|
||||
use ratatui::{
|
||||
crossterm::event::{self, Event, KeyCode},
|
||||
widgets::Paragraph,
|
||||
DefaultTerminal, Frame,
|
||||
};
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::layout::{Constraint, Direction};
|
||||
use ratatui::prelude::Layout;
|
||||
use ratatui::widgets::{Block, Borders};
|
||||
use gemma::chip8::computer::Chip8Computer;
|
||||
use gemma::chip8::computer_manager::Chip8ComputerManager;
|
||||
use gemmarat::register_widget::RegisterWidget;
|
||||
use gemmarat::timer_widget::TimerWidget;
|
||||
use gemmarat::video_widget::VideoWidget;
|
||||
|
||||
struct GemmaRat {
|
||||
pub computer: Chip8ComputerManager
|
||||
}
|
||||
|
||||
impl Default for GemmaRat {
|
||||
fn default() -> Self {
|
||||
GemmaRat {
|
||||
computer: Chip8ComputerManager::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
color_eyre::install()?;
|
||||
let terminal = ratatui::init();
|
||||
let app_result = run(terminal).context("app loop failed");
|
||||
ratatui::restore();
|
||||
app_result
|
||||
}
|
||||
|
||||
|
||||
fn run(mut terminal: DefaultTerminal) -> Result<()> {
|
||||
let mut manager = Chip8ComputerManager::default();
|
||||
manager.load_new_program_from_disk_to_system_memory(Path::new("/home/tmerritt/Projects/trevors_chip8_toy/resources/roms/2-ibm-logo.ch8"));
|
||||
for _ in 0..30 {
|
||||
manager.tick();
|
||||
}
|
||||
loop {
|
||||
terminal.draw(|frame| {
|
||||
do_draw(frame, &mut manager);
|
||||
}).expect("unable to render display.");
|
||||
if should_quit()? {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn do_draw(frame: &mut Frame, draw_with: &mut Chip8ComputerManager) {
|
||||
// build the layout...
|
||||
let main_layout = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints(vec![
|
||||
Constraint::Length(32),
|
||||
Constraint::Max(3)
|
||||
])
|
||||
.split(frame.area());
|
||||
let upper_block = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints(vec![
|
||||
Constraint::Length(64),
|
||||
Constraint::Fill(1)
|
||||
])
|
||||
.split(main_layout[0]);
|
||||
let column_block = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints(vec![
|
||||
// Registers
|
||||
Constraint::Min(20),
|
||||
// Timers
|
||||
Constraint::Min(6),
|
||||
// Keypad
|
||||
Constraint::Min(8)
|
||||
])
|
||||
.split(upper_block[1]);
|
||||
// ...then put the parts in the layout.
|
||||
|
||||
|
||||
// Video Block
|
||||
let state = draw_with.state().clone().video_memory;
|
||||
frame.render_widget(
|
||||
VideoWidget::with_display(state), upper_block[0]
|
||||
);
|
||||
|
||||
// Registers Block
|
||||
let mut state = draw_with.state().registers;
|
||||
frame.render_stateful_widget(
|
||||
RegisterWidget::new(),
|
||||
column_block[0],
|
||||
&mut state);
|
||||
|
||||
// Times Block
|
||||
frame.render_stateful_widget(
|
||||
TimerWidget::new(),
|
||||
column_block[1],
|
||||
&mut TimerWidget::with_values(
|
||||
draw_with.state().sound_timer.current(),
|
||||
draw_with.state().delay_timer.current()
|
||||
)
|
||||
);
|
||||
|
||||
// Keypad Block
|
||||
frame.render_widget(
|
||||
Paragraph::new("Keypad Goes Here")
|
||||
.block(Block::new().borders(Borders::ALL)), column_block[2]
|
||||
);
|
||||
|
||||
// Footer Bar
|
||||
frame.render_widget(
|
||||
Paragraph::new("F1 - Step | F2 - Run | F3 - Stop | Q - Quit")
|
||||
.centered()
|
||||
.block(Block::new().borders(Borders::ALL)), main_layout[1]);
|
||||
|
||||
}
|
||||
|
||||
fn should_quit() -> Result<bool> {
|
||||
if event::poll(Duration::from_millis(250)).context("event poll failed")? {
|
||||
if let Event::Key(key) = event::read().context("event read failed")? {
|
||||
return Ok(KeyCode::Char('q') == key.code);
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
pub mod timer_widget;
|
||||
pub mod register_widget;
|
||||
pub mod video_widget;
|
||||
@@ -0,0 +1,43 @@
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::widgets::{Block, Borders, List, ListItem, ListState};
|
||||
use gemma::chip8::registers::Chip8Registers;
|
||||
|
||||
pub struct RegisterWidget {
|
||||
state: Chip8Registers
|
||||
}
|
||||
|
||||
impl RegisterWidget {
|
||||
pub fn new() -> RegisterWidget {
|
||||
RegisterWidget { state: Chip8Registers::default() }
|
||||
}
|
||||
|
||||
pub fn with_values(state: Chip8Registers) -> RegisterWidget {
|
||||
RegisterWidget {
|
||||
state
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StatefulWidget for RegisterWidget {
|
||||
type State = Chip8Registers;
|
||||
|
||||
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Chip8Registers) {
|
||||
let mut working_lines: Vec<ListItem> = vec![];
|
||||
|
||||
// build the lines for the display...
|
||||
let working_text = state.format_as_string();
|
||||
|
||||
for current_line in working_text.lines() {
|
||||
working_lines.push(ListItem::new(current_line));
|
||||
}
|
||||
|
||||
let my_list = List::new(working_lines)
|
||||
.block(Block::new()
|
||||
.title(Line::raw("Registers").centered())
|
||||
.borders(Borders::ALL));
|
||||
// ...draw it out;
|
||||
|
||||
StatefulWidget::render(my_list, area, buf, &mut ListState::default());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::widgets::{Block, Paragraph};
|
||||
use ratatui::text::Line;
|
||||
|
||||
pub struct TimerWidget {
|
||||
sound_timer_value: u8,
|
||||
delay_timer_value: u8
|
||||
}
|
||||
|
||||
impl TimerWidget {
|
||||
pub fn new() -> TimerWidget {
|
||||
TimerWidget { sound_timer_value: 0, delay_timer_value: 0 }
|
||||
}
|
||||
|
||||
pub fn with_values(sound: u8, delay: u8) -> TimerWidget {
|
||||
TimerWidget {
|
||||
sound_timer_value: sound,
|
||||
delay_timer_value: delay
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StatefulWidget for TimerWidget {
|
||||
type State = TimerWidget;
|
||||
|
||||
fn render(self, area: Rect, buf: &mut Buffer, state: &mut TimerWidget) {
|
||||
let line_1 = Span::raw(format!("Sound Timer: {} [{:02x}]", state.sound_timer_value, state.sound_timer_value));
|
||||
let line_2 = Span::raw(format!("Delay Timer: {} [{:02x}]", state.delay_timer_value, state.delay_timer_value));
|
||||
|
||||
let text = vec![
|
||||
Line::from(vec![line_1]),
|
||||
Line::from(vec![line_2])
|
||||
];
|
||||
|
||||
let line = Paragraph::new(text)
|
||||
.block(Block::bordered().title("Timers"));
|
||||
line.render(area, buf);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::widgets::Paragraph;
|
||||
use gemma::chip8::video::Chip8Video;
|
||||
|
||||
pub struct VideoWidget {
|
||||
video: Chip8Video
|
||||
}
|
||||
|
||||
impl VideoWidget {
|
||||
pub fn new() -> Self {
|
||||
VideoWidget {
|
||||
video : Chip8Video::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_display(new_display: Chip8Video) -> Self {
|
||||
VideoWidget {
|
||||
video: new_display
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for VideoWidget {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
let working_text = self.video.format_as_string();
|
||||
|
||||
//println!("WORKING_TEXT = {}", working_text);
|
||||
let text = Paragraph::new(working_text);
|
||||
Widget::render(text, area, buf);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user