Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potentially stronger README example? #14

Open
brandonros opened this issue Jun 29, 2020 · 1 comment
Open

Potentially stronger README example? #14

brandonros opened this issue Jun 29, 2020 · 1 comment

Comments

@brandonros
Copy link

pub struct MemoryRegion {
    pub start: u32,
    pub end: u32,
    pub name: String,
    pub buffer: Vec<u8>,
}

impl MemoryRegion {
    fn address_in_region(&self, address: u32) -> bool {
        address >= self.start && address <= self.end
    }

    fn set(&mut self, address: u32, value: u8) {
        let offset = (address - self.start) as usize;
        self.buffer[offset] = value;
    }

    fn get(&self, address: u32) -> u8 {
        let offset = (address - self.start) as usize;
        self.buffer[offset]
    }

    fn to_ihex(&self) -> String {
        let mut data: Vec<u8> = vec![];
        for i in (self.start..self.end) {
            data.push(self.get(self.start + i));
        }
        let offset = (self.start >> 16) as u16;
        return ihex::create_object_file_representation(&[
            ihex::Record::ExtendedSegmentAddress((self.start >> 16) as u16),
            ihex::Record::Data { offset, value: data },
            ihex::Record::EndOfFile,
        ]).unwrap();
    }
}

I'm trying to dump this MemoryRegion:

MemoryRegion {
                start: 0x50000000,
                end: 0x5001DFFF,
                buffer: vec![0; 0x1E000],
                name: String::from("CPU2_DSPR"),
            }

and I'm a little confused how I should be doing the looping given the constraints from ihex::Record::Data (Specifies a 16-bit offset address and up to 255 bytes of data)

Can you maybe help me better understand what it would take to dump this memory region and maybe we can PR it to the README for a stronger, more real world Intel hex example?

@martinmroz
Copy link
Owner

martinmroz commented Jul 3, 2020

Hey @brandonros, sorry for the delay in getting back to you, things have been pretty hectic around here lately.

Philosophically this library was meant to be a really light wrapper around reading and writing IHEX objects, and I chose to do that in the simplest way possible, mapping ihex::Records onto individual lines within an IHEX file. It's pretty low-level.

I've actually written (and am happy to open-source some time) something built on ihex that allows you to compose sparse memory regions and read/write them as entire ihex objects. With that said, I created a gist that does what you're after from your example.

https://gist.github.com/martinmroz/709e1fc96d7f91974b9de17491a2bf20

I'll update the example in the README, maybe leveraging what I wrote in the gist.

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants