1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use super::super::State;
use super::{Pin, PinType, Chip};
use std::cell::RefCell;
use std::rc::Rc;
use rand::random;

/// # A 256-bytes RAM chip
/// 
/// # Diagram
/// CS: Chip Select
/// WE: Write Enable
/// OE: Output Enable
/// A0-7: Addresses
/// IO0-7: Input/Output
/// ```
///        ---__---
///  !CS --|1   22|-- VCC
///  !WE --|2   21|-- UNUSED
///  !OE --|3   20|-- IO7
///   A0 --|4   19|-- IO6
///   A1 --|5   18|-- IO5
///   A2 --|6   17|-- IO4
///   A3 --|7   16|-- IO3
///   A4 --|8   15|-- IO2
///   A5 --|9   14|-- IO1
///   A6 --|10  13|-- IO0
///  GND --|11  12|-- A7
///        --------
/// ```
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) {
        // check alimented
        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;
            }
            // check Chip Select (active low)
            if self.pin[0].borrow().state == State::Low {
                //print!("RAM: selected\t");
                // check Write Enable (active low)
                if self.pin[1].borrow().state == State::Low {
                    // IO = Input
                    for i in 12..20 {
                        self.pin[i].borrow_mut().pin_type = PinType::Input;
                    }
                    // read data on IO pins
                    let addr = self.get_address() as usize;
                    //print!("RAM: write [{:02X}]: {:02X} \t", addr, self.get_data());
                    self.ram[addr] = self.get_data();
                }

                // check Output Enable (active low)
                if self.pin[2].borrow().state == State::Low {
                    // IO = Output
                    for i in 12..21 {
                        self.pin[i].borrow_mut().pin_type = PinType::Output;
                    }
                    // display data on IO pins
                    let addr = self.get_address() as usize;
                    //print!("RAM: read [{:02X}]: {:02X} \t", addr, self.ram[addr]);
                    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);
                }
                //println!();
            } else {
                // IO : undefined
                for i in 12..20 {
                    self.pin[i].borrow_mut().pin_type = PinType::Undefined;
                }
            }
        } else if self.powered {
            // turn off every pin
            for i in 0..22 {
                self.pin[i].borrow_mut().state = State::Undefined
            }
            self.powered = false;
        }
    }
}

/// # A 256-bytes ROM chip
/// 
/// # Diagram
/// CS: Chip Select
/// WE: Write Enable
/// OE: Output Enable
/// A0-7: Addresses
/// IO0-7: Input/Output
/// ```
///         ---__---
///   !CS --|1   22|-- VCC
/// UNUSED--|2   21|-- UNUSED
///   !OE --|3   20|-- IO7
///    A0 --|4   19|-- IO6
///    A1 --|5   18|-- IO5
///    A2 --|6   17|-- IO4
///    A3 --|7   16|-- IO3
///    A4 --|8   15|-- IO2
///    A5 --|9   14|-- IO1
///    A6 --|10  13|-- IO0
///   GND --|11  12|-- A7
///         --------
/// ```
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) {
        // check alimented
        if self.pin[10].borrow().state == State::Low && self.pin[21].borrow().state == State::High {
            // check Chip Select (active low)
            if self.pin[0].borrow().state == State::Low {
                //print!("ROM: selected\t");
                // check Output Enable (active low)
                if self.pin[2].borrow().state == State::Low {
                    // IO = Output
                    for i in 12..21 {
                        self.pin[i].borrow_mut().pin_type = PinType::Output;
                    }
                    // display data on IO pins
                    let addr = self.get_address() as usize;
                    //print!("ROM: read [{:02X}]: {:02X} \t", addr, self.rom[addr]);
                    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);
                }
                //println!();
            } else {
                // IO : undefined
                for i in 12..20 {
                    self.pin[i].borrow_mut().pin_type = PinType::Undefined;
                }
            }
        } else {
            // turn off every pin
            for i in 0..22 {
                self.pin[i].borrow_mut().state = State::Undefined
            }
        }
    }
}