pub fn load_elf_from_path(&mut self, path: &Path) -> Result<(), String> {
        let bytes = match fs::read(path) {
            Ok(b) => b,
            Err(e) => {
                return Err(format!("Failed to read file \"{:?}\": {}", path, e));
            }
        };

        let elf = match Elf::parse(&bytes) {
            Ok(e) => e,
            Err(e) => {
                return Err(format!("Failed to parse elf file \"{:?}\": {}", path, e));
            }
        };

        for sym in elf.syms.iter() {
            let offset = sym.st_name;
            let name = match elf.strtab.get(offset) {
                Some(Ok(s)) => s,
                _ => return Err(String::from("missing symbols")),
            };

            match name {
                "SystemInit" |
                "__libc_init_array" |
                "init" |
                "audio_init" |
                "audio_play_sample" |
                "init_joystick" |
                "joystick_init_all" |
                "joystick_enable_interrupts_all" |
                "lcd_init" |
                "lcd_write_char" |
                "lcd_write_string" |
                "lcd_update_display" |
                "maximise_clock_speed" |
                "BSP_AUDIO_OUT_Play_Sample" => {
                    self.branch_map.insert((sym.st_value as u32) & !0b1, name.to_string());
                }
                _ => {}
            }
        }

        match self.memory.load_elf(elf, &bytes) {
            Ok(_) => {}
            Err(e) => return Err(e),
        };

        // https://developer.arm.com/docs/dui0553/a/the-cortex-m4-processor/programmers-model/core-registers
        self.cpu.write_reg(13, self.memory.read_mem_a(0x0000_0000, 4).expect("failed to read memory at 0x0000_0000")); // set to value at address 0x0000_0000 on reset
        self.cpu.write_reg(14, 0xFFFF_FFFF); // set to 0xFFFF_FFFF on reset
        let pc = self.memory.read_mem_a(0x0000_0004, 4).expect("failed to read memory at 0x0000_0004"); // set to value at 0x0000_0004 on reset
        println!("setting emulator pc to 0x{:08X}", pc);
        self.cpu.write_reg(15, pc & !0b1);
        self.bx_write_pc(pc);

        return Ok(());
    }
/**
     * Takes a path to an ELF file and initialises the board with its contents
     */
Takes a path to an ELF file and initialises the board with its contents
Takes a path to an ELF file and initialises the board with its contents
pub fn load_elf_from_path(&mut self, path: &Path) -> Result<(), String> {
        let bytes = match fs::read(path) {
            Ok(b) => b,
            Err(e) => {
                return Err(format!("Failed to read file \"{:?}\": {}", path, e));
            }
        };
        let elf = match Elf::parse(&bytes) {
            Ok(e) => e,
            Err(e) => {
                return Err(format!("Failed to parse elf file \"{:?}\": {}", path, e));
            }
        };
        for sym in elf.syms.iter() {
            let offset = sym.st_name;
            let name = match elf.strtab.get(offset) {
                Some(Ok(s)) => s,
                _ => return Err(String::from("missing symbols")),
            };
            match name {
                "SystemInit" |
                "__libc_init_array" |
                "init" |
                "audio_init" |
                "audio_play_sample" |
                "init_joystick" |
                "joystick_init_all" |
                "joystick_enable_interrupts_all" |
                "lcd_init" |
                "lcd_write_char" |
                "lcd_write_string" |
                "lcd_update_display" |
                "maximise_clock_speed" |
                "BSP_AUDIO_OUT_Play_Sample" => {
                    self.branch_map.insert((sym.st_value as u32) & !0b1, name.to_string());
                }
                _ => {}
            }
        }
        match self.memory.load_elf(elf, &bytes) {
            Ok(_) => {}
            Err(e) => return Err(e),
        };
        // https://developer.arm.com/docs/dui0553/a/the-cortex-m4-processor/programmers-model/core-registers
        self.cpu.write_reg(13, self.memory.read_mem_a(0x0000_0000, 4).expect("failed to read memory at 0x0000_0000")); // set to value at address 0x0000_0000 on reset
        self.cpu.write_reg(14, 0xFFFF_FFFF); // set to 0xFFFF_FFFF on reset
        let pc = self.memory.read_mem_a(0x0000_0004, 4).expect("failed to read memory at 0x0000_0004"); // set to value at 0x0000_0004 on reset
        println!("setting emulator pc to 0x{:08X}", pc);
        self.cpu.write_reg(15, pc & !0b1);
        self.bx_write_pc(pc);
        return Ok(());
    }
pub fn load_elf_from_path ( & mut self , path : & Path ) -> Result < ( ) , String > { let bytes = match fs :: read ( path ) { Ok ( b ) => b , Err ( e ) => { return Err ( format ! ( " \" \" " path e ) ) ; } } ; let elf = match Elf :: parse ( & bytes ) { Ok ( e ) => e , Err ( e ) => { return Err ( format ! ( " \" \" " path e ) ) ; } } ; for sym in elf . syms . iter ( ) { let offset = sym . st_name ; let name = match elf . strtab . get ( offset ) { Some ( Ok ( s ) ) => s , _ => return Err ( String :: from ( " " ) ) , } ; match name { " " | " " | " " | " " | " " | " " | " " | " " | " " | " " | " " | " " | " " | " " => { self . branch_map . insert ( ( sym . st_value as u32 ) & ! 0b1 , name . to_string ( ) ) ; } _ => { } } } match self . memory . load_elf ( elf , & bytes ) { Ok ( _ ) => { } Err ( e ) => return Err ( e ) , } ; // https://developer.arm.com/docs/dui0553/a/the-cortex-m4-processor/programmers-model/core-registers self . cpu . write_reg ( 13 , self . memory . read_mem_a ( 0x0000_0000 , 4 ) . expect ( " " ) ) ; // set to value at address 0x0000_0000 on reset self . cpu . write_reg ( 14 , 0xFFFF_FFFF ) ; // set to 0xFFFF_FFFF on reset let pc = self . memory . read_mem_a ( 0x0000_0004 , 4 ) . expect ( " " ) ; // set to value at 0x0000_0004 on reset println ! ( " " pc ) ; self . cpu . write_reg ( 15 , pc & ! 0b1 ) ; self . bx_write_pc ( pc ) ; return Ok ( ( ) ) ; }
Takes a path to an ELF file and initialises the board with its contents
Takes a path to an ELF file and initialises the board with its contents
&mut self
path
&Path