use crate::Conflict;
use litz_core::{Level, PartState};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ImageKind {
Rom,
Bootloader,
PartitionTable,
Firmware,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FlashLayout {
pub size: u64,
pub bootloader: u64,
pub partition_table: u64,
pub app: u64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Stop {
Budget,
Idle,
Halted,
Trap,
Reset,
Breakpoint(u32),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RunResult {
pub cycles: u64,
pub stop: Stop,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Input {
Gpio { at_ns: u64, pin: u8, level: Level },
Uart { at_ns: u64, bytes: Vec<u8> },
}
#[derive(Clone, Debug)]
pub struct ChipInfo {
pub name: String,
pub cpu_hz: u64,
pub flash: FlashLayout,
}
pub trait Chip {
fn info(&self) -> ChipInfo;
fn load_image(&mut self, kind: ImageKind, offset: u64, bytes: &[u8]) -> Result<(), String>;
fn set_psram_size(&mut self, _bytes: usize) -> Result<(), String> {
Err("this backend has no external PSRAM".into())
}
fn boot(&mut self) -> Result<u32, String>;
fn schedule(&mut self, inputs: Vec<Input>) -> Result<(), String>;
fn run(&mut self, budget_cycles: u64) -> RunResult;
fn seconds(&self) -> f64;
fn write_console(&mut self, bytes: &[u8]);
fn take_console(&mut self) -> Vec<u8>;
fn state(&self, part: &str) -> Option<PartState>;
fn press(&mut self, part: &str, pressed: bool) -> bool;
fn set_control(&mut self, part: &str, value: u16) -> bool;
fn set_named_control(&mut self, _part: &str, _name: &str, _value: &str) -> bool {
false
}
fn part_pin_level(&self, part: &str, pin: Option<&str>) -> Option<Level>;
fn gpio_level(&self, _gpio: u8) -> Option<Level> {
None
}
fn conflicts(&self) -> Vec<Conflict>;
fn start_trace(&mut self) {}
fn vcd(&self) -> String {
String::new()
}
fn break_at(&mut self, _pc: u32) {}
fn registers(&self) -> String {
String::new()
}
fn report(&mut self) -> Vec<String> {
Vec::new()
}
fn enable_coverage(&mut self) {}
fn fault(&self) -> Option<String> {
None
}
fn display(&mut self) -> Option<(u32, u32, Vec<u16>, u64)> {
None
}
fn display_version(&mut self) -> u64 {
0
}
fn save_display(&mut self, _path: &std::path::Path) -> Result<(), String> {
Err("this backend has no display to capture".into())
}
fn save_part_display(&mut self, _part: &str, _path: &std::path::Path) -> Result<(), String> {
Err("this backend cannot capture a part".into())
}
fn touch(&mut self, _x: u16, _y: u16, _down: bool) -> Result<(), String> {
Err("this board has no touch controller".into())
}
fn sample_heap(&mut self) -> Result<u32, String> {
Err("this backend cannot sample a guest heap".into())
}
fn start_air_capture(&mut self, _path: &std::path::Path) -> Result<(), String> {
Err("this backend has no 802.11 radio".into())
}
fn start_ethernet_capture(&mut self, _path: &std::path::Path) -> Result<(), String> {
Err("this backend has no Ethernet transport".into())
}
fn attach_host_gateway(&mut self, _forwards: &[String]) -> Result<Vec<String>, String> {
Err("this backend has no Wi-Fi radio".into())
}
fn debug(&mut self) -> Option<&mut dyn ChipDebug> {
None
}
fn debug_available(&self) -> bool {
false
}
fn debug_pc(&self) -> Option<u32> {
None
}
fn debug_register_file(&self) -> Option<[u32; 33]> {
None
}
fn debug_read_memory(&mut self, _addr: u32, _len: usize) -> Vec<u8> {
Vec::new()
}
fn debug_disassemble(&mut self, _addr: u32, _count: usize) -> String {
String::new()
}
fn debug_set_breakpoints(&mut self, _pcs: &[u32]) {}
fn debug_step(&mut self) -> Option<Stop> {
None
}
}
pub trait ChipDebug {
fn serve(&mut self, port: u16) -> Result<(), String>;
fn report(&mut self) -> String {
String::new()
}
}
impl<T: Chip + ?Sized> Chip for Box<T> {
fn info(&self) -> ChipInfo {
(**self).info()
}
fn load_image(&mut self, kind: ImageKind, offset: u64, bytes: &[u8]) -> Result<(), String> {
(**self).load_image(kind, offset, bytes)
}
fn set_psram_size(&mut self, bytes: usize) -> Result<(), String> {
(**self).set_psram_size(bytes)
}
fn boot(&mut self) -> Result<u32, String> {
(**self).boot()
}
fn schedule(&mut self, inputs: Vec<Input>) -> Result<(), String> {
(**self).schedule(inputs)
}
fn run(&mut self, budget_cycles: u64) -> RunResult {
(**self).run(budget_cycles)
}
fn seconds(&self) -> f64 {
(**self).seconds()
}
fn write_console(&mut self, bytes: &[u8]) {
(**self).write_console(bytes)
}
fn take_console(&mut self) -> Vec<u8> {
(**self).take_console()
}
fn state(&self, part: &str) -> Option<PartState> {
(**self).state(part)
}
fn press(&mut self, part: &str, pressed: bool) -> bool {
(**self).press(part, pressed)
}
fn set_control(&mut self, part: &str, value: u16) -> bool {
(**self).set_control(part, value)
}
fn set_named_control(&mut self, part: &str, name: &str, value: &str) -> bool {
(**self).set_named_control(part, name, value)
}
fn part_pin_level(&self, part: &str, pin: Option<&str>) -> Option<Level> {
(**self).part_pin_level(part, pin)
}
fn gpio_level(&self, gpio: u8) -> Option<Level> {
(**self).gpio_level(gpio)
}
fn conflicts(&self) -> Vec<Conflict> {
(**self).conflicts()
}
fn fault(&self) -> Option<String> {
(**self).fault()
}
fn save_display(&mut self, path: &std::path::Path) -> Result<(), String> {
(**self).save_display(path)
}
fn save_part_display(&mut self, part: &str, path: &std::path::Path) -> Result<(), String> {
(**self).save_part_display(part, path)
}
fn sample_heap(&mut self) -> Result<u32, String> {
(**self).sample_heap()
}
fn touch(&mut self, x: u16, y: u16, down: bool) -> Result<(), String> {
(**self).touch(x, y, down)
}
fn start_air_capture(&mut self, path: &std::path::Path) -> Result<(), String> {
(**self).start_air_capture(path)
}
fn start_ethernet_capture(&mut self, path: &std::path::Path) -> Result<(), String> {
(**self).start_ethernet_capture(path)
}
fn attach_host_gateway(&mut self, forwards: &[String]) -> Result<Vec<String>, String> {
(**self).attach_host_gateway(forwards)
}
fn debug(&mut self) -> Option<&mut dyn ChipDebug> {
(**self).debug()
}
fn debug_available(&self) -> bool {
(**self).debug_available()
}
fn debug_pc(&self) -> Option<u32> {
(**self).debug_pc()
}
fn debug_register_file(&self) -> Option<[u32; 33]> {
(**self).debug_register_file()
}
fn debug_read_memory(&mut self, addr: u32, len: usize) -> Vec<u8> {
(**self).debug_read_memory(addr, len)
}
fn debug_disassemble(&mut self, addr: u32, count: usize) -> String {
(**self).debug_disassemble(addr, count)
}
fn debug_set_breakpoints(&mut self, pcs: &[u32]) {
(**self).debug_set_breakpoints(pcs)
}
fn debug_step(&mut self) -> Option<Stop> {
(**self).debug_step()
}
fn start_trace(&mut self) {
(**self).start_trace()
}
fn vcd(&self) -> String {
(**self).vcd()
}
fn break_at(&mut self, pc: u32) {
(**self).break_at(pc)
}
fn registers(&self) -> String {
(**self).registers()
}
fn report(&mut self) -> Vec<String> {
(**self).report()
}
fn enable_coverage(&mut self) {
(**self).enable_coverage()
}
}