use core::constants::constants_system::*;
use rand::random;
use core::traits::memory_chip::MemoryChip;
use core::periph::hm62256::Hm62256;
use core::traits::ram_chip::RamChip;

#[test]
#[ignore]
fn written_data_comes_back() {
    let mut ram = Hm62256::default();

    // 100,000 random read/writes to ram that all read back right
    for _ in 0..100_000 {
        let mut offset: u16 = random();
        println!("Size = {SIZE_32KB}");
        let value: u8 = random();
        println!("Wrote [{value:02x}] to [{offset:04x}]");
        ram.write(&offset, &value);

        assert_eq!(ram.read(&offset), value)
    }
}

#[test]
fn write_to_memory_read_back_works_at_0() {
    let mut ram = Hm62256::default();

    // load the data to ram
    ram.tick(0x0000, 0xab, false, true);
    // read the data back
    let (_, new_data) = ram.tick(0x0000, 0x00, true, true);

    assert_eq!(new_data, 0xab);
}

#[test]
fn address_space_is_round() {
    // addresses written past the last address 'loop' back to 0+(offset - MAX_SIZE)
    let max_offset = SIZE_32KB;
    let test_offset = max_offset;

    // all zero
    let mut ram = Hm62256::default();
    // write FF to the addresss after the last
    ram.write(&(test_offset as u16), &0xff);

    // check all the ram for anything that isn't 0x00

    assert_eq!(ram.read(&(0x0000)), 0xff);
    for offset in 1..SIZE_32KB {
        println!("Testing offset {offset:04x} for 0x00");
        assert_eq!(ram.read(&(offset as u16)), 0x00);
    }
}