Switch to using cards to provide memory
This commit is contained in:
parent
f45583892b
commit
da8a5a729b
@ -65,7 +65,7 @@ impl LoadBinWindow {
|
|||||||
match self.ftype {
|
match self.ftype {
|
||||||
BinaryType::Raw => {
|
BinaryType::Raw => {
|
||||||
let bin = std::fs::read(path).unwrap();
|
let bin = std::fs::read(path).unwrap();
|
||||||
state.write_binary(self.start_addr as usize, &bin);
|
state.write_binary(self.start_addr, &bin);
|
||||||
}
|
}
|
||||||
BinaryType::IntelHex => {
|
BinaryType::IntelHex => {
|
||||||
let data = std::fs::read_to_string(path).unwrap();
|
let data = std::fs::read_to_string(path).unwrap();
|
||||||
@ -73,7 +73,7 @@ impl LoadBinWindow {
|
|||||||
let record = record.unwrap();
|
let record = record.unwrap();
|
||||||
match record {
|
match record {
|
||||||
ihex::Record::Data { offset, value } => {
|
ihex::Record::Data { offset, value } => {
|
||||||
state.write_binary(offset as usize, &value);
|
state.write_binary(offset, &value);
|
||||||
}
|
}
|
||||||
ihex::Record::StartLinearAddress(_) => todo!(),
|
ihex::Record::StartLinearAddress(_) => todo!(),
|
||||||
ihex::Record::EndOfFile => (),
|
ihex::Record::EndOfFile => (),
|
||||||
|
35
src/state.rs
35
src/state.rs
@ -1,7 +1,5 @@
|
|||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
use rand::RngCore;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
audio::AudioMessage,
|
audio::AudioMessage,
|
||||||
card::{Card, Type},
|
card::{Card, Type},
|
||||||
@ -11,7 +9,6 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub struct EmuState {
|
pub struct EmuState {
|
||||||
mem: [u8; 65536],
|
|
||||||
cpu: I8080,
|
cpu: I8080,
|
||||||
running: bool,
|
running: bool,
|
||||||
audio_tx: Sender<AudioMessage>,
|
audio_tx: Sender<AudioMessage>,
|
||||||
@ -26,14 +23,11 @@ impl EmuState {
|
|||||||
options: Options,
|
options: Options,
|
||||||
cards: Vec<(String, String)>,
|
cards: Vec<(String, String)>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut mem = [0; 65536];
|
|
||||||
rand::thread_rng().fill_bytes(&mut mem);
|
|
||||||
let cards = cards.iter().map(|(typ_name, settings)| {
|
let cards = cards.iter().map(|(typ_name, settings)| {
|
||||||
let typ = Type::get(&typ_name).unwrap();
|
let typ = Type::get(&typ_name).unwrap();
|
||||||
(typ, typ.new_card(ron::from_str(&settings).unwrap()).unwrap())
|
(typ, typ.new_card(ron::from_str(&settings).unwrap()).unwrap())
|
||||||
}).collect();
|
}).collect();
|
||||||
let mut slf = Self {
|
let mut slf = Self {
|
||||||
mem,
|
|
||||||
cpu: I8080::new(),
|
cpu: I8080::new(),
|
||||||
running: false,
|
running: false,
|
||||||
audio_tx,
|
audio_tx,
|
||||||
@ -77,13 +71,12 @@ impl EmuState {
|
|||||||
ActionSwitch::Deposit => {
|
ActionSwitch::Deposit => {
|
||||||
if state == SwitchState::Up {
|
if state == SwitchState::Up {
|
||||||
// Assume M1
|
// Assume M1
|
||||||
self.mem[self.cpu.get_mem_cycle().address() as usize] =
|
|
||||||
self.fp_state.ad_sws() as u8;
|
self.write_mem(self.cpu.get_mem_cycle().address(), self.fp_state.ad_sws() as u8);
|
||||||
} else if state == SwitchState::Down {
|
} else if state == SwitchState::Down {
|
||||||
// Assume M1
|
// Assume M1
|
||||||
self.cpu.finish_m_cycle(0x0); // NOP
|
self.cpu.finish_m_cycle(0x0); // NOP
|
||||||
self.mem[self.cpu.get_mem_cycle().address() as usize] =
|
self.write_mem(self.cpu.get_mem_cycle().address(), self.fp_state.ad_sws() as u8);
|
||||||
self.fp_state.ad_sws() as u8;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ActionSwitch::Reset => {
|
ActionSwitch::Reset => {
|
||||||
@ -123,7 +116,8 @@ impl EmuState {
|
|||||||
self.fp_state.set_status(cycle.get_status());
|
self.fp_state.set_status(cycle.get_status());
|
||||||
match cycle {
|
match cycle {
|
||||||
MemCycle::Fetch(a) | MemCycle::Read(a) | MemCycle::StackRead(a) => {
|
MemCycle::Fetch(a) | MemCycle::Read(a) | MemCycle::StackRead(a) => {
|
||||||
self.fp_state.set_data(self.mem[a as usize]);
|
let data = self.read_mem(a);
|
||||||
|
self.fp_state.set_data(data);
|
||||||
}
|
}
|
||||||
MemCycle::Write(_, _)
|
MemCycle::Write(_, _)
|
||||||
| MemCycle::StackWrite(_, _)
|
| MemCycle::StackWrite(_, _)
|
||||||
@ -142,9 +136,9 @@ impl EmuState {
|
|||||||
pub fn run_cpu_cycle(&mut self) {
|
pub fn run_cpu_cycle(&mut self) {
|
||||||
let cycle = self.cpu.get_mem_cycle();
|
let cycle = self.cpu.get_mem_cycle();
|
||||||
let data = match cycle {
|
let data = match cycle {
|
||||||
MemCycle::Fetch(a) | MemCycle::Read(a) | MemCycle::StackRead(a) => self.mem[a as usize],
|
MemCycle::Fetch(a) | MemCycle::Read(a) | MemCycle::StackRead(a) => self.read_mem(a),
|
||||||
MemCycle::Write(a, d) | MemCycle::StackWrite(a, d) => {
|
MemCycle::Write(a, d) | MemCycle::StackWrite(a, d) => {
|
||||||
self.mem[a as usize] = d;
|
self.write_mem(a, d);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
MemCycle::In(_) => 0,
|
MemCycle::In(_) => 0,
|
||||||
@ -159,9 +153,11 @@ impl EmuState {
|
|||||||
self.cpu.finish_m_cycle(data);
|
self.cpu.finish_m_cycle(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_binary(&mut self, start: usize, data: &[u8]) {
|
pub fn write_binary(&mut self, start: u16, data: &[u8]) {
|
||||||
assert!(0x1_0000 - start >= data.len());
|
assert!(0x1_0000 - (start as usize) >= data.len());
|
||||||
self.mem[start..(start + data.len())].copy_from_slice(data);
|
for (i, byte) in data.iter().enumerate() {
|
||||||
|
self.write_mem(i as u16 + start, *byte);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_options(&mut self, new_opts: Options) {
|
pub fn update_options(&mut self, new_opts: Options) {
|
||||||
@ -214,4 +210,11 @@ impl EmuState {
|
|||||||
pub fn save_cards(&self) -> Vec<(String, String)> {
|
pub fn save_cards(&self) -> Vec<(String, String)> {
|
||||||
self.cards.iter().map(|(typ, card)| (typ.name().to_string(), card.get_settings())).collect()
|
self.cards.iter().map(|(typ, card)| (typ.name().to_string(), card.get_settings())).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_mem(&mut self, address: u16) -> u8 {
|
||||||
|
self.cards.iter_mut().find_map(|(_, card)| card.read_mem(address)).unwrap_or(0xFF)
|
||||||
|
}
|
||||||
|
fn write_mem(&mut self, address: u16, data: u8) {
|
||||||
|
self.cards.iter_mut().find_map(|(_, card)| card.write_mem(address, data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user