2024-01-27 17:24:59 -06:00
|
|
|
mod audio;
|
2023-08-10 13:58:34 -05:00
|
|
|
mod card;
|
2023-10-12 13:11:38 -05:00
|
|
|
mod cpu;
|
2024-01-24 14:31:01 -06:00
|
|
|
mod frontpanel;
|
2023-08-10 13:58:34 -05:00
|
|
|
mod ram;
|
|
|
|
|
2024-01-29 14:19:56 -06:00
|
|
|
use std::sync::mpsc::Sender;
|
2023-08-10 13:58:34 -05:00
|
|
|
|
2024-01-27 17:24:59 -06:00
|
|
|
use audio::{AudioMessage, AudioThread};
|
2023-08-10 13:58:34 -05:00
|
|
|
use cpu::{MemCycle, Status, I8080};
|
2024-01-24 14:31:01 -06:00
|
|
|
use eframe::{
|
2024-01-29 14:19:56 -06:00
|
|
|
egui::{self, menu, Button},
|
2024-01-24 14:31:01 -06:00
|
|
|
NativeOptions,
|
|
|
|
};
|
2023-08-10 13:58:34 -05:00
|
|
|
use egui_modal::Modal;
|
2024-01-29 14:19:56 -06:00
|
|
|
use frontpanel::Textures;
|
2023-08-10 13:58:34 -05:00
|
|
|
use rand::RngCore;
|
2024-01-23 17:10:03 -06:00
|
|
|
use rfd::FileDialog;
|
2023-08-10 13:58:34 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-01-29 11:02:37 -06:00
|
|
|
use crate::frontpanel::{switch::SwitchState, Frontpanel, FrontpanelInteraction, FrontpanelState};
|
2024-01-24 14:31:01 -06:00
|
|
|
|
2023-08-10 13:58:34 -05:00
|
|
|
fn main() -> Result<(), eframe::Error> {
|
|
|
|
env_logger::init();
|
2024-01-27 17:24:59 -06:00
|
|
|
let audio_tx = AudioThread::init();
|
2023-08-10 13:58:34 -05:00
|
|
|
eframe::run_native(
|
|
|
|
"Altair 8800 Emulator",
|
2024-01-24 10:50:35 -06:00
|
|
|
NativeOptions::default(),
|
2024-01-27 17:24:59 -06:00
|
|
|
Box::new(|cc| Box::new(AltairEmulator::new(cc, audio_tx))),
|
2023-08-10 13:58:34 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
|
|
|
struct Options {
|
|
|
|
fan_enabled: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AltairEmulator {
|
2024-01-29 14:19:56 -06:00
|
|
|
textures: Textures,
|
2023-08-10 13:58:34 -05:00
|
|
|
mem: [u8; 65536],
|
|
|
|
cpu: I8080,
|
|
|
|
running: bool,
|
2024-01-27 17:24:59 -06:00
|
|
|
audio_tx: Sender<AudioMessage>,
|
2023-08-10 13:58:34 -05:00
|
|
|
options: Options,
|
|
|
|
option_window: Option<OptionWindow>,
|
2024-01-29 11:02:37 -06:00
|
|
|
fp_state: FrontpanelState,
|
2023-08-10 13:58:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AltairEmulator {
|
2024-01-27 17:24:59 -06:00
|
|
|
fn new(cc: &eframe::CreationContext<'_>, audio_tx: Sender<AudioMessage>) -> Self {
|
2023-08-10 13:58:34 -05:00
|
|
|
let options = if cc.storage.unwrap().get_string("options").is_none() {
|
|
|
|
Options { fan_enabled: true }
|
|
|
|
} else {
|
|
|
|
eframe::get_value(cc.storage.unwrap(), "options").unwrap()
|
|
|
|
};
|
|
|
|
let mut mem = [0; 65536];
|
|
|
|
let cpu = I8080::new();
|
|
|
|
rand::thread_rng().fill_bytes(&mut mem);
|
|
|
|
mem[0x0] = 0x3a;
|
|
|
|
mem[0x1] = 0x10;
|
|
|
|
mem[0x2] = 0x00;
|
|
|
|
|
|
|
|
mem[0x3] = 0x47;
|
|
|
|
|
|
|
|
mem[0x4] = 0x3a;
|
|
|
|
mem[0x5] = 0x11;
|
|
|
|
mem[0x6] = 0x00;
|
|
|
|
|
|
|
|
mem[0x7] = 0x80;
|
|
|
|
|
|
|
|
mem[0x8] = 0x32;
|
|
|
|
mem[0x9] = 0x12;
|
|
|
|
mem[0xa] = 0x00;
|
|
|
|
|
|
|
|
mem[0xb] = 0x76;
|
|
|
|
|
|
|
|
mem[0x10] = 0x4;
|
|
|
|
mem[0x11] = 0x5;
|
|
|
|
// 0: 072 020 000
|
2023-10-12 13:11:38 -05:00
|
|
|
// 3: 107
|
2023-08-10 13:58:34 -05:00
|
|
|
// 4: 072 021 000
|
|
|
|
// 7: 200
|
|
|
|
// 10: 062 022 000
|
|
|
|
// 13: 166
|
|
|
|
// 20: 004
|
|
|
|
// 21: 005
|
|
|
|
Self {
|
2024-01-29 14:19:56 -06:00
|
|
|
textures: Textures::new(&cc.egui_ctx),
|
2023-08-10 13:58:34 -05:00
|
|
|
mem,
|
|
|
|
cpu,
|
|
|
|
running: false,
|
2024-01-27 17:24:59 -06:00
|
|
|
audio_tx,
|
2023-08-10 13:58:34 -05:00
|
|
|
options,
|
|
|
|
option_window: None,
|
2024-01-29 11:02:37 -06:00
|
|
|
fp_state: FrontpanelState::new(),
|
2023-08-10 13:58:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_fp(&mut self) {
|
|
|
|
let cycle = self.cpu.get_mem_cycle();
|
2024-01-29 11:02:37 -06:00
|
|
|
self.fp_state.set_status(cycle.get_status());
|
2023-08-10 13:58:34 -05:00
|
|
|
match cycle {
|
|
|
|
MemCycle::Fetch(a) | MemCycle::Read(a) | MemCycle::StackRead(a) => {
|
2024-01-29 11:02:37 -06:00
|
|
|
self.fp_state.set_addr(a);
|
|
|
|
self.fp_state.set_data(self.mem[a as usize]);
|
2023-08-10 13:58:34 -05:00
|
|
|
}
|
|
|
|
MemCycle::Write(a, _) | MemCycle::StackWrite(a, _) | MemCycle::Out(a, _) => {
|
2024-01-29 11:02:37 -06:00
|
|
|
self.fp_state.set_addr(a);
|
|
|
|
self.fp_state.set_data(0xff);
|
2023-08-10 13:58:34 -05:00
|
|
|
}
|
2024-01-23 16:01:14 -06:00
|
|
|
MemCycle::In(a) => {
|
2024-01-29 11:02:37 -06:00
|
|
|
self.fp_state.set_addr(a);
|
|
|
|
self.fp_state.set_data(0);
|
2024-01-24 14:31:01 -06:00
|
|
|
}
|
2023-08-10 13:58:34 -05:00
|
|
|
MemCycle::Inta(_) => todo!(),
|
|
|
|
MemCycle::Hlta(_) => {
|
2024-01-29 11:02:37 -06:00
|
|
|
self.fp_state.set_addr(0xffff);
|
|
|
|
self.fp_state.set_data(0xff);
|
2023-08-10 13:58:34 -05:00
|
|
|
}
|
|
|
|
MemCycle::IntaHlt(_) => todo!(),
|
|
|
|
}
|
|
|
|
}
|
2024-01-29 11:02:37 -06:00
|
|
|
|
|
|
|
fn run_cpu_cycle(&mut self) {
|
|
|
|
let cycle = self.cpu.get_mem_cycle();
|
|
|
|
let data = match cycle {
|
|
|
|
MemCycle::Fetch(a) | MemCycle::Read(a) | MemCycle::StackRead(a) => self.mem[a as usize],
|
|
|
|
MemCycle::Write(a, d) | MemCycle::StackWrite(a, d) => {
|
|
|
|
self.mem[a as usize] = d;
|
|
|
|
0
|
|
|
|
}
|
|
|
|
MemCycle::In(_) => 0,
|
|
|
|
MemCycle::Out(_, _) => 0,
|
|
|
|
MemCycle::Inta(_) => todo!(),
|
|
|
|
MemCycle::Hlta(_) => {
|
|
|
|
self.running = false;
|
|
|
|
0
|
|
|
|
}
|
|
|
|
MemCycle::IntaHlt(_) => todo!(),
|
|
|
|
};
|
|
|
|
self.cpu.finish_m_cycle(data);
|
|
|
|
self.update_fp();
|
|
|
|
}
|
2023-08-10 13:58:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl eframe::App for AltairEmulator {
|
|
|
|
fn save(&mut self, storage: &mut dyn eframe::Storage) {
|
|
|
|
eframe::set_value(storage, "options", &self.options);
|
|
|
|
}
|
2024-01-29 11:02:37 -06:00
|
|
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
2023-08-10 13:58:34 -05:00
|
|
|
ctx.set_pixels_per_point(1.0);
|
2024-01-25 10:34:10 -06:00
|
|
|
// frame.set_window_size((800.0, 333.0).into());
|
2023-08-10 13:58:34 -05:00
|
|
|
let mut disable_fp_sws = self.option_window.is_some();
|
|
|
|
egui::TopBottomPanel::top("menu").show(ctx, |ui| {
|
|
|
|
menu::bar(ui, |ui| {
|
|
|
|
menu::menu_button(ui, "Edit", |ui| {
|
|
|
|
disable_fp_sws = true;
|
|
|
|
if ui
|
|
|
|
.add_enabled(self.option_window.is_none(), Button::new("Options"))
|
|
|
|
.clicked()
|
|
|
|
{
|
2024-01-24 10:50:35 -06:00
|
|
|
self.option_window = Some(OptionWindow::new(ctx, self.options));
|
|
|
|
ui.close_menu();
|
2023-08-10 13:58:34 -05:00
|
|
|
}
|
2024-01-23 17:10:03 -06:00
|
|
|
if ui.button("Load binary file").clicked() {
|
|
|
|
let ihex_exts = ["hex", "mcs", "int", "ihex", "ihe", "ihx"];
|
|
|
|
let file = FileDialog::new()
|
2024-01-24 14:31:01 -06:00
|
|
|
.add_filter(
|
|
|
|
"Binary files",
|
|
|
|
&["bin", "img", "hex", "mcs", "int", "ihex", "ihe", "ihx"],
|
|
|
|
)
|
2024-01-23 17:10:03 -06:00
|
|
|
.add_filter("All files", &["*"])
|
|
|
|
.pick_file();
|
|
|
|
if let Some(file) = file {
|
2024-01-24 14:31:01 -06:00
|
|
|
if file.extension().map_or(false, |ext| {
|
|
|
|
ihex_exts.contains(&ext.to_str().unwrap_or(""))
|
|
|
|
}) {
|
2024-01-23 17:10:03 -06:00
|
|
|
let data = std::fs::read_to_string(file).unwrap();
|
|
|
|
for record in ihex::Reader::new(&data) {
|
|
|
|
let record = record.unwrap();
|
|
|
|
match record {
|
|
|
|
ihex::Record::Data { offset, value } => {
|
|
|
|
for (i, &byte) in value.iter().enumerate() {
|
|
|
|
self.mem[offset as usize + i] = byte;
|
|
|
|
}
|
2024-01-24 14:31:01 -06:00
|
|
|
}
|
2024-01-23 17:10:03 -06:00
|
|
|
ihex::Record::StartLinearAddress(_) => todo!(),
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Raw binary
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-10 13:58:34 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
2024-01-29 11:02:37 -06:00
|
|
|
let mut fp = Frontpanel::new(&self.textures, &mut self.fp_state);
|
|
|
|
ui.add(&mut fp);
|
|
|
|
let interaction = fp.interaction();
|
|
|
|
if let Some(interaction) = interaction {
|
|
|
|
self.audio_tx.send(AudioMessage::PlaySwitchClick).unwrap();
|
|
|
|
match interaction {
|
2024-01-29 14:19:56 -06:00
|
|
|
FrontpanelInteraction::ActionSwChanged(sw, state) => {
|
2024-01-29 11:02:37 -06:00
|
|
|
if self.fp_state.power() {
|
|
|
|
match sw {
|
|
|
|
frontpanel::ActionSwitch::RunStop => {
|
|
|
|
if state == SwitchState::Up {
|
2024-01-29 14:19:56 -06:00
|
|
|
println!("STOP RUN");
|
2024-01-29 11:02:37 -06:00
|
|
|
self.running = false;
|
2024-01-29 14:19:56 -06:00
|
|
|
} else if state == SwitchState::Down {
|
|
|
|
println!("START RUN");
|
2024-01-29 11:02:37 -06:00
|
|
|
self.running = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
frontpanel::ActionSwitch::SingleStep => {
|
2024-01-29 14:19:56 -06:00
|
|
|
if state == SwitchState::Up {
|
|
|
|
self.run_cpu_cycle();
|
|
|
|
}
|
2024-01-29 11:02:37 -06:00
|
|
|
}
|
|
|
|
frontpanel::ActionSwitch::Examine => {
|
|
|
|
if state == SwitchState::Up {
|
|
|
|
// Assume M1
|
|
|
|
self.cpu.finish_m_cycle(0xC3); // JMP
|
|
|
|
self.cpu.finish_m_cycle(self.fp_state.ad_sws() as u8);
|
|
|
|
self.cpu
|
|
|
|
.finish_m_cycle((self.fp_state.ad_sws() >> 8) as u8);
|
|
|
|
self.update_fp();
|
2024-01-29 14:19:56 -06:00
|
|
|
} else if state == SwitchState::Down {
|
2024-01-29 11:02:37 -06:00
|
|
|
// Assume M1
|
|
|
|
self.cpu.finish_m_cycle(0x0); // NOP
|
|
|
|
self.update_fp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
frontpanel::ActionSwitch::Deposit => {
|
|
|
|
if state == SwitchState::Up {
|
|
|
|
// Assume M1
|
|
|
|
self.mem[self.cpu.get_mem_cycle().address() as usize] =
|
|
|
|
self.fp_state.ad_sws() as u8;
|
|
|
|
self.update_fp();
|
2024-01-29 14:19:56 -06:00
|
|
|
} else if state == SwitchState::Down {
|
2024-01-29 11:02:37 -06:00
|
|
|
// Assume M1
|
|
|
|
self.cpu.finish_m_cycle(0x0); // NOP
|
|
|
|
self.mem[self.cpu.get_mem_cycle().address() as usize] =
|
|
|
|
self.fp_state.ad_sws() as u8;
|
|
|
|
self.update_fp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
frontpanel::ActionSwitch::Reset => {
|
|
|
|
if state == SwitchState::Up {
|
|
|
|
self.cpu.reset();
|
|
|
|
self.update_fp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
frontpanel::ActionSwitch::Protect => (),
|
|
|
|
frontpanel::ActionSwitch::Aux1 => (),
|
|
|
|
frontpanel::ActionSwitch::Aux2 => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FrontpanelInteraction::AdChanged(_) => (),
|
|
|
|
FrontpanelInteraction::PowerChanged(pwr) => {
|
|
|
|
if pwr {
|
|
|
|
self.audio_tx.send(AudioMessage::FanOn).unwrap();
|
|
|
|
self.cpu = I8080::new();
|
|
|
|
self.update_fp();
|
|
|
|
} else {
|
|
|
|
self.audio_tx.send(AudioMessage::FanOff).unwrap();
|
|
|
|
self.running = false;
|
|
|
|
self.fp_state.set_status(Status::empty());
|
|
|
|
self.fp_state.set_addr(0);
|
|
|
|
self.fp_state.set_data(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.running {
|
|
|
|
self.run_cpu_cycle();
|
|
|
|
}
|
2023-08-10 13:58:34 -05:00
|
|
|
});
|
2024-01-24 14:31:01 -06:00
|
|
|
|
2024-01-29 11:02:37 -06:00
|
|
|
let old_fan_enabled = self.options.fan_enabled;
|
2023-08-10 13:58:34 -05:00
|
|
|
if let Some(option_window) = self.option_window.as_mut() {
|
2024-01-24 10:50:35 -06:00
|
|
|
if option_window.draw(ctx, &mut self.options) {
|
2023-08-10 13:58:34 -05:00
|
|
|
self.option_window = None;
|
|
|
|
}
|
|
|
|
}
|
2024-01-29 11:02:37 -06:00
|
|
|
if (old_fan_enabled != self.options.fan_enabled) && self.fp_state.power() {
|
|
|
|
if self.options.fan_enabled {
|
|
|
|
self.audio_tx.send(AudioMessage::FanOn).unwrap();
|
|
|
|
} else {
|
|
|
|
self.audio_tx.send(AudioMessage::FanOff).unwrap();
|
|
|
|
}
|
|
|
|
}
|
2023-08-10 13:58:34 -05:00
|
|
|
if self.running {
|
|
|
|
ctx.request_repaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OptionWindow {
|
|
|
|
options: Options,
|
|
|
|
category: OptionsCategory,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
enum OptionsCategory {
|
|
|
|
General,
|
|
|
|
Cards,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OptionWindow {
|
|
|
|
fn new(ctx: &egui::Context, options: Options) -> Self {
|
|
|
|
Modal::new(ctx, "options_modal").open();
|
2023-10-12 13:11:38 -05:00
|
|
|
Self {
|
|
|
|
options,
|
|
|
|
category: OptionsCategory::General,
|
|
|
|
}
|
2023-08-10 13:58:34 -05:00
|
|
|
}
|
|
|
|
fn draw(&mut self, ctx: &egui::Context, options: &mut Options) -> bool {
|
|
|
|
let modal = Modal::new(ctx, "options_modal");
|
|
|
|
modal.show(|ui| {
|
|
|
|
modal.title(ui, "Options");
|
|
|
|
ui.horizontal(|ui| {
|
|
|
|
ui.selectable_value(&mut self.category, OptionsCategory::General, "General");
|
|
|
|
ui.selectable_value(&mut self.category, OptionsCategory::Cards, "Cards");
|
|
|
|
});
|
|
|
|
match self.category {
|
|
|
|
OptionsCategory::General => {
|
|
|
|
ui.checkbox(&mut self.options.fan_enabled, "Fan enabled");
|
2023-10-12 13:11:38 -05:00
|
|
|
}
|
2023-08-10 13:58:34 -05:00
|
|
|
OptionsCategory::Cards => {
|
|
|
|
ui.heading("TODO");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
modal.buttons(ui, |ui| {
|
|
|
|
if ui.button("Apply").clicked() {
|
2024-01-24 10:50:35 -06:00
|
|
|
*options = self.options;
|
2023-08-10 13:58:34 -05:00
|
|
|
}
|
|
|
|
if modal.button(ui, "OK").clicked() {
|
2024-01-24 10:50:35 -06:00
|
|
|
*options = self.options;
|
2023-08-10 13:58:34 -05:00
|
|
|
}
|
|
|
|
modal.caution_button(ui, "Cancel");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
!modal.is_open()
|
|
|
|
}
|
|
|
|
}
|