use super::super::State;
use super::{Pin, PinType, Chip};
use std::cell::RefCell;
use std::rc::Rc;
use rand::random;
pub struct Ram256B {
pin: [Rc<RefCell<Pin>>; 22],
ram: [u8; 256],
powered: bool
}
impl Default for Ram256B {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for Ram256B {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
fmt.write_str("----------\nRam256B\n")?;
fmt.write_str(self.to_string().as_str())?;
fmt.write_str(format!("\naddress: {:02X}", self.get_address()).as_str())?;
fmt.write_str(format!("\ndata: {:02X}", self.ram[self.get_address() as usize]).as_str())?;
fmt.write_str(format!("\nCS: {}\tWE: {}\tOE: {}", !self.pin[0].borrow().state.as_bool(), !self.pin[1].borrow().state.as_bool(), !self.pin[2].borrow().state.as_bool()).as_str())?;
fmt.write_str("\n----------")?;
Ok(())
}
}
impl ToString for Ram256B {
fn to_string(&self) -> std::string::String {
let mut string = String::new();
for byte in self.ram.iter() {
string.push_str(format!("{:02X}", byte).as_str());
}
string
}
}
impl Ram256B {
pub const CS: u8 = 1;
pub const WE: u8 = 2;
pub const OE: u8 = 3;
pub const A0: u8 = 4;
pub const A1: u8 = 5;
pub const A2: u8 = 6;
pub const A3: u8 = 7;
pub const A4: u8 = 8;
pub const A5: u8 = 9;
pub const A6: u8 = 10;
pub const A7: u8 = 12;
pub const IO0: u8 = 13;
pub const IO1: u8 = 14;
pub const IO2: u8 = 15;
pub const IO3: u8 = 16;
pub const IO4: u8 = 17;
pub const IO5: u8 = 18;
pub const IO6: u8 = 19;
pub const IO7: u8 = 20;
pub const VCC: u8 = 22;
pub const GND: u8 = 11;
pub fn new() -> Self {
Ram256B {
pin: [
Rc::new(RefCell::new(Pin::new(1, PinType::Input))),
Rc::new(RefCell::new(Pin::new(2, PinType::Input))),
Rc::new(RefCell::new(Pin::new(3, PinType::Input))),
Rc::new(RefCell::new(Pin::new(4, PinType::Input))),
Rc::new(RefCell::new(Pin::new(5, PinType::Input))),
Rc::new(RefCell::new(Pin::new(6, PinType::Input))),
Rc::new(RefCell::new(Pin::new(7, PinType::Input))),
Rc::new(RefCell::new(Pin::new(8, PinType::Input))),
Rc::new(RefCell::new(Pin::new(9, PinType::Input))),
Rc::new(RefCell::new(Pin::new(10, PinType::Input))),
Rc::new(RefCell::new(Pin::new(11, PinType::Input))),
Rc::new(RefCell::new(Pin::new(12, PinType::Input))),
Rc::new(RefCell::new(Pin::new(13, PinType::Output))),
Rc::new(RefCell::new(Pin::new(14, PinType::Output))),
Rc::new(RefCell::new(Pin::new(15, PinType::Output))),
Rc::new(RefCell::new(Pin::new(16, PinType::Output))),
Rc::new(RefCell::new(Pin::new(17, PinType::Output))),
Rc::new(RefCell::new(Pin::new(18, PinType::Output))),
Rc::new(RefCell::new(Pin::new(19, PinType::Output))),
Rc::new(RefCell::new(Pin::new(20, PinType::Output))),
Rc::new(RefCell::new(Pin::new(21, PinType::Input))),
Rc::new(RefCell::new(Pin::new(22, PinType::Input)))
],
ram: [0; 256],
powered: false
}
}
fn get_address(&self) -> u8 {
let mut addr: u8 = 0;
for i in 3..10 {
let bit = if self.pin[i].borrow().state == State::High {1} else {0};
addr += bit << (i-3);
}
let bit = if self.pin[11].borrow().state == State::High {1} else {0};
addr += bit << 7;
addr
}
fn get_data(&self) -> u8 {
let mut addr: u8 = 0;
for i in 12..20 {
let bit = if self.pin[i].borrow().state == State::High {1} else {0};
addr += bit << (i-12);
}
addr
}
}
impl Chip for Ram256B {
fn get_pin_qty(&self) -> u8 {
22
}
fn get_pin(&mut self, pin: u8) -> Result<Rc<RefCell<Pin>>, &str> {
if pin > 0 && pin <= 22 {
Ok(self.pin[pin as usize-1].clone())
} else {
Err("Pin out of bounds")
}
}
fn run(&mut self, _: std::time::Duration) {
if self.pin[10].borrow().state == State::Low && self.pin[21].borrow().state == State::High {
if !self.powered {
for i in 0..256 {
self.ram[i] = random::<u8>();
}
self.powered = true;
}
if self.pin[0].borrow().state == State::Low {
if self.pin[1].borrow().state == State::Low {
for i in 12..20 {
self.pin[i].borrow_mut().pin_type = PinType::Input;
}
let addr = self.get_address() as usize;
self.ram[addr] = self.get_data();
}
if self.pin[2].borrow().state == State::Low {
for i in 12..21 {
self.pin[i].borrow_mut().pin_type = PinType::Output;
}
let addr = self.get_address() as usize;
self.pin[12].borrow_mut().state = State::from_u8(self.ram[addr], 0);
self.pin[13].borrow_mut().state = State::from_u8(self.ram[addr], 1);
self.pin[14].borrow_mut().state = State::from_u8(self.ram[addr], 2);
self.pin[15].borrow_mut().state = State::from_u8(self.ram[addr], 3);
self.pin[16].borrow_mut().state = State::from_u8(self.ram[addr], 4);
self.pin[17].borrow_mut().state = State::from_u8(self.ram[addr], 5);
self.pin[18].borrow_mut().state = State::from_u8(self.ram[addr], 6);
self.pin[19].borrow_mut().state = State::from_u8(self.ram[addr], 7);
}
} else {
for i in 12..20 {
self.pin[i].borrow_mut().pin_type = PinType::Undefined;
}
}
} else if self.powered {
for i in 0..22 {
self.pin[i].borrow_mut().state = State::Undefined
}
self.powered = false;
}
}
}
pub struct Rom256B {
pin: [Rc<RefCell<Pin>>; 22],
rom: [u8; 256],
}
impl Default for Rom256B {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for Rom256B {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
fmt.write_str("----------\nRom256B\n")?;
fmt.write_str(self.to_string().as_str())?;
fmt.write_str(format!("\naddress: {:02X}", self.get_address()).as_str())?;
fmt.write_str(format!("\ndata: {:02X}", self.rom[self.get_address() as usize]).as_str())?;
fmt.write_str(format!("\nCS: {}\tOE: {}", !self.pin[0].borrow().state.as_bool(), !self.pin[2].borrow().state.as_bool()).as_str())?;
fmt.write_str("\n----------")?;
Ok(())
}
}
impl ToString for Rom256B {
fn to_string(&self) -> std::string::String {
let mut string = String::new();
for byte in self.rom.iter() {
string.push_str(format!("{:02X}", byte).as_str());
}
string
}
}
impl Rom256B {
pub const CS: u8 = 1;
pub const OE: u8 = 3;
pub const A0: u8 = 4;
pub const A1: u8 = 5;
pub const A2: u8 = 6;
pub const A3: u8 = 7;
pub const A4: u8 = 8;
pub const A5: u8 = 9;
pub const A6: u8 = 10;
pub const A7: u8 = 12;
pub const IO0: u8 = 13;
pub const IO1: u8 = 14;
pub const IO2: u8 = 15;
pub const IO3: u8 = 16;
pub const IO4: u8 = 17;
pub const IO5: u8 = 18;
pub const IO6: u8 = 19;
pub const IO7: u8 = 20;
pub const VCC: u8 = 22;
pub const GND: u8 = 11;
pub fn new() -> Self {
Rom256B {
pin: [
Rc::new(RefCell::new(Pin::new(1, PinType::Input))),
Rc::new(RefCell::new(Pin::new(2, PinType::Input))),
Rc::new(RefCell::new(Pin::new(3, PinType::Input))),
Rc::new(RefCell::new(Pin::new(4, PinType::Input))),
Rc::new(RefCell::new(Pin::new(5, PinType::Input))),
Rc::new(RefCell::new(Pin::new(6, PinType::Input))),
Rc::new(RefCell::new(Pin::new(7, PinType::Input))),
Rc::new(RefCell::new(Pin::new(8, PinType::Input))),
Rc::new(RefCell::new(Pin::new(9, PinType::Input))),
Rc::new(RefCell::new(Pin::new(10, PinType::Input))),
Rc::new(RefCell::new(Pin::new(11, PinType::Input))),
Rc::new(RefCell::new(Pin::new(12, PinType::Input))),
Rc::new(RefCell::new(Pin::new(13, PinType::Output))),
Rc::new(RefCell::new(Pin::new(14, PinType::Output))),
Rc::new(RefCell::new(Pin::new(15, PinType::Output))),
Rc::new(RefCell::new(Pin::new(16, PinType::Output))),
Rc::new(RefCell::new(Pin::new(17, PinType::Output))),
Rc::new(RefCell::new(Pin::new(18, PinType::Output))),
Rc::new(RefCell::new(Pin::new(19, PinType::Output))),
Rc::new(RefCell::new(Pin::new(20, PinType::Output))),
Rc::new(RefCell::new(Pin::new(21, PinType::Input))),
Rc::new(RefCell::new(Pin::new(22, PinType::Input)))
],
rom: [0; 256],
}
}
pub fn from_data(data: [u8;256]) -> Rom256B {
let mut rom = Rom256B::new();
rom.load_data(data);
rom
}
pub fn load_data(&mut self, data: [u8;256]) {
self.rom.clone_from_slice(&data);
}
fn get_address(&self) -> u8 {
let mut addr: u8 = 0;
for i in 3..10 {
let bit = if self.pin[i].borrow().state == State::High {1} else {0};
addr += bit << (i-3);
}
let bit = if self.pin[11].borrow().state == State::High {1} else {0};
addr += bit << 7;
addr
}
}
impl Chip for Rom256B {
fn get_pin_qty(&self) -> u8 {
22
}
fn get_pin(&mut self, pin: u8) -> Result<Rc<RefCell<Pin>>, &str> {
if pin > 0 && pin <= 22 {
Ok(self.pin[pin as usize-1].clone())
} else {
Err("Pin out of bounds")
}
}
fn run(&mut self, _: std::time::Duration) {
if self.pin[10].borrow().state == State::Low && self.pin[21].borrow().state == State::High {
if self.pin[0].borrow().state == State::Low {
if self.pin[2].borrow().state == State::Low {
for i in 12..21 {
self.pin[i].borrow_mut().pin_type = PinType::Output;
}
let addr = self.get_address() as usize;
self.pin[12].borrow_mut().state = State::from_u8(self.rom[addr], 0);
self.pin[13].borrow_mut().state = State::from_u8(self.rom[addr], 1);
self.pin[14].borrow_mut().state = State::from_u8(self.rom[addr], 2);
self.pin[15].borrow_mut().state = State::from_u8(self.rom[addr], 3);
self.pin[16].borrow_mut().state = State::from_u8(self.rom[addr], 4);
self.pin[17].borrow_mut().state = State::from_u8(self.rom[addr], 5);
self.pin[18].borrow_mut().state = State::from_u8(self.rom[addr], 6);
self.pin[19].borrow_mut().state = State::from_u8(self.rom[addr], 7);
}
} else {
for i in 12..20 {
self.pin[i].borrow_mut().pin_type = PinType::Undefined;
}
}
} else {
for i in 0..22 {
self.pin[i].borrow_mut().state = State::Undefined
}
}
}
}