2021-02-23 17:19:37 +11:00
|
|
|
#![no_std]
|
2021-04-09 15:57:30 +10:00
|
|
|
#![no_main]
|
2021-02-23 17:19:37 +11:00
|
|
|
|
2021-04-12 10:00:18 +10:00
|
|
|
// Note(Lokathor): this demo must be run in release mode or it'll just be way
|
|
|
|
// too slow. When testing is running the screen fills from yellow to green. If
|
|
|
|
// there's a panic you should see red. If all tests complete the screen turns
|
|
|
|
// blue at the end.
|
|
|
|
|
2021-02-23 17:19:37 +11:00
|
|
|
use core::cmp;
|
2021-04-09 15:57:30 +10:00
|
|
|
use gba::{fatal, prelude::*, save::*, warn};
|
|
|
|
|
|
|
|
fn set_screen_color(r: u8, g: u8, b: u8) {
|
|
|
|
const SETTING: DisplayControl = DisplayControl::new().with_display_mode(3).with_display_bg2(true);
|
2021-02-23 17:19:37 +11:00
|
|
|
DISPCNT.write(SETTING);
|
2021-04-09 15:57:30 +10:00
|
|
|
mode3::dma3_clear_to(Color::from_rgb(r, g, b));
|
2021-02-23 17:19:37 +11:00
|
|
|
}
|
|
|
|
fn set_screen_progress(cur: usize, max: usize) {
|
2021-04-09 15:57:30 +10:00
|
|
|
let lines = cur * (mode3::WIDTH / max);
|
2021-02-23 17:19:37 +11:00
|
|
|
let color = Color::from_rgb(0, 31, 0);
|
|
|
|
for x in 0..lines {
|
2021-04-09 15:57:30 +10:00
|
|
|
for y in 0..mode3::HEIGHT {
|
|
|
|
mode3::bitmap_xy(x, y).write(color);
|
2021-02-23 17:19:37 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[panic_handler]
|
|
|
|
fn panic(info: &core::panic::PanicInfo) -> ! {
|
|
|
|
set_screen_color(31, 0, 0);
|
2021-04-09 15:57:30 +10:00
|
|
|
fatal!("{}", info)
|
2021-02-23 17:19:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Rng(u32);
|
|
|
|
impl Rng {
|
|
|
|
fn iter(&mut self) {
|
2021-04-12 10:00:18 +10:00
|
|
|
self.0 = self.0.wrapping_mul(2891336453).wrapping_add(100001);
|
2021-02-23 17:19:37 +11:00
|
|
|
}
|
|
|
|
fn next_u8(&mut self) -> u8 {
|
|
|
|
self.iter();
|
|
|
|
(self.0 >> 22) as u8 ^ self.0 as u8
|
|
|
|
}
|
|
|
|
fn next_under(&mut self, under: u32) -> u32 {
|
|
|
|
self.iter();
|
|
|
|
let scale = 31 - under.leading_zeros();
|
|
|
|
((self.0 >> scale) ^ self.0) % under
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const MAX_BLOCK_SIZE: usize = 4 * 1024;
|
|
|
|
|
|
|
|
fn check_status<T>(r: Result<T, Error>) -> T {
|
|
|
|
match r {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => panic!("Error encountered: {:?}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_timers() {
|
2021-04-09 15:57:30 +10:00
|
|
|
TIMER0_RELOAD.write(0);
|
|
|
|
TIMER1_RELOAD.write(0);
|
2021-02-23 17:19:37 +11:00
|
|
|
|
2021-04-09 15:57:30 +10:00
|
|
|
let ctl = TimerControl::new().with_prescaler_selection(3).with_enabled(true);
|
|
|
|
TIMER0_CONTROL.write(ctl);
|
|
|
|
let ctl = TimerControl::new().with_chained_counting(true).with_enabled(true);
|
|
|
|
TIMER1_CONTROL.write(ctl);
|
2021-02-23 17:19:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// I'm fully aware how slow this is. But this is just example code, so, eh.
|
|
|
|
fn get_timer_secs() -> f32 {
|
2021-04-09 15:57:30 +10:00
|
|
|
let raw_timer = (TIMER1_COUNTER.read() as u32) << 16 | TIMER0_COUNTER.read() as u32;
|
2021-02-23 17:19:37 +11:00
|
|
|
(raw_timer as f32 * 1024.0) / ((1 << 24) as f32)
|
|
|
|
}
|
|
|
|
macro_rules! output {
|
2021-04-09 15:57:30 +10:00
|
|
|
($($args:tt)*) => {
|
|
|
|
// we use warn so it shows by default on mGBA, nothing more.
|
|
|
|
warn!("{:7.3}\t{}", get_timer_secs(), format_args!($($args)*))
|
|
|
|
};
|
2021-02-23 17:19:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn do_test(seed: Rng, offset: usize, len: usize, block_size: usize) -> Result<(), Error> {
|
|
|
|
let access = SaveAccess::new()?;
|
|
|
|
let mut buffer = [0; MAX_BLOCK_SIZE];
|
|
|
|
|
|
|
|
output!(" - Clearing media...");
|
2021-04-01 13:22:42 +11:00
|
|
|
access.prepare_write(offset..offset + len)?;
|
2021-02-23 17:19:37 +11:00
|
|
|
|
|
|
|
output!(" - Writing media...");
|
|
|
|
let mut rng = seed.clone();
|
|
|
|
let mut current = offset;
|
|
|
|
let end = offset + len;
|
|
|
|
while current != end {
|
|
|
|
let cur_len = cmp::min(end - current, block_size);
|
|
|
|
for i in 0..cur_len {
|
|
|
|
buffer[i] = rng.next_u8();
|
|
|
|
}
|
|
|
|
access.write(current, &buffer[..cur_len])?;
|
|
|
|
current += cur_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
output!(" - Validating media...");
|
|
|
|
rng = seed.clone();
|
|
|
|
current = offset;
|
|
|
|
while current != end {
|
|
|
|
let cur_len = cmp::min(end - current, block_size);
|
|
|
|
access.read(current, &mut buffer[..cur_len])?;
|
|
|
|
for i in 0..cur_len {
|
|
|
|
let cur_byte = rng.next_u8();
|
|
|
|
assert!(
|
|
|
|
buffer[i] == cur_byte,
|
|
|
|
"Read does not match earlier write: {} != {} @ 0x{:05x}",
|
|
|
|
buffer[i],
|
|
|
|
cur_byte,
|
|
|
|
current + i,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
current += cur_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
output!(" - Done!");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:57:30 +10:00
|
|
|
#[no_mangle]
|
|
|
|
fn main() -> ! {
|
2021-02-23 17:19:37 +11:00
|
|
|
// set a pattern to show that the ROM is working at all.
|
|
|
|
set_screen_color(31, 31, 0);
|
|
|
|
|
|
|
|
// sets up the timers so we can print time with our outputs.
|
|
|
|
setup_timers();
|
|
|
|
|
|
|
|
// set the save type
|
|
|
|
use_flash_128k();
|
|
|
|
set_timer_for_timeout(3);
|
|
|
|
|
|
|
|
// check some metainfo on the save type
|
|
|
|
let access = check_status(SaveAccess::new());
|
|
|
|
output!("Media info: {:#?}", access.media_info());
|
|
|
|
output!("Media size: {} bytes", access.len());
|
|
|
|
output!("");
|
|
|
|
|
|
|
|
// actually test the save implementation
|
|
|
|
if access.len() >= (1 << 12) {
|
|
|
|
output!("[ Full write, 4KiB blocks ]");
|
|
|
|
check_status(do_test(Rng(2000), 0, access.len(), 4 * 1024));
|
|
|
|
set_screen_progress(1, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
output!("[ Full write, 0.5KiB blocks ]");
|
|
|
|
check_status(do_test(Rng(1000), 0, access.len(), 512));
|
|
|
|
set_screen_progress(2, 10);
|
|
|
|
|
|
|
|
// test with random segments now.
|
|
|
|
let mut rng = Rng(12345);
|
|
|
|
for i in 0..8 {
|
|
|
|
let rand_length = rng.next_under((access.len() >> 1) as u32) as usize + 50;
|
|
|
|
let rand_offset = rng.next_under(access.len() as u32 - rand_length as u32) as usize;
|
|
|
|
let block_size = cmp::min(rand_length >> 2, MAX_BLOCK_SIZE - 100);
|
|
|
|
let block_size = rng.next_under(block_size as u32) as usize + 50;
|
|
|
|
|
|
|
|
output!(
|
|
|
|
"[ Partial, offset = 0x{:06x}, len = {}, bs = {}]",
|
2021-04-01 13:22:42 +11:00
|
|
|
rand_offset,
|
|
|
|
rand_length,
|
|
|
|
block_size,
|
2021-02-23 17:19:37 +11:00
|
|
|
);
|
|
|
|
check_status(do_test(Rng(i * 10000), rand_offset, rand_length, block_size));
|
|
|
|
set_screen_progress(3 + i as usize, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
// show a pattern so we know it worked
|
2021-04-12 10:00:18 +10:00
|
|
|
set_screen_color(0, 29, 29);
|
|
|
|
output!("All tests complete!");
|
2021-04-01 13:22:42 +11:00
|
|
|
loop {}
|
2021-02-23 17:19:37 +11:00
|
|
|
}
|