Skip to main content

i8051_disassembler/commands/
bytes.rs

1use crate::address::{AddressRange, AddressSpace, AddressValue, SpaceAddressRange, SpaceAddressValue};
2use crate::db::{Db, Error};
3use crate::region::ByteRange;
4
5use super::{Apply, Command, Environment, boxed};
6
7register_commands!(MapBytes, ClearBytes, SetConstantBytes);
8
9#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
10pub struct MapBytes {
11    pub address: SpaceAddressValue,
12    pub file: String,
13    pub file_offset: usize,
14    pub size: AddressValue,
15}
16
17impl MapBytes {
18    pub fn new(
19        space: AddressSpace,
20        offset: AddressValue,
21        file: impl Into<String>,
22        file_offset: usize,
23        size: AddressValue,
24    ) -> Self {
25        Self {
26            address: (space, offset).into(),
27            file: file.into(),
28            file_offset,
29            size,
30        }
31    }
32}
33
34impl Apply for MapBytes {
35    fn apply(
36        self,
37        db: &mut Db,
38        env: Option<&dyn Environment>,
39    ) -> Result<Vec<Box<dyn Command>>, Error> {
40        let Self {
41            address,
42            file,
43            file_offset,
44            size,
45        } = self;
46        let SpaceAddressValue { space, offset } = address;
47        let region = db.region_mut(space);
48        let Some(env) = env else {
49            return Err(Error::NoEnvironment);
50        };
51        let bytes = env
52            .load_file_bytes(&file, file_offset, size)
53            .map_err(Error::Io)?;
54        let size = bytes.len() as AddressValue;
55        let before = region.snapshot_byte_ranges(offset, size);
56        region.map_bytes(&file, file_offset, offset, &bytes);
57        Ok(undo_byte_ranges(address, size, before))
58    }
59}
60
61#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
62pub struct ClearBytes {
63    pub range: SpaceAddressRange,
64}
65
66impl ClearBytes {
67    pub fn new(space: AddressSpace, offset: AddressValue, size: AddressValue) -> Self {
68        Self {
69            range: (space, AddressRange::new(offset, offset + size)).into(),
70        }
71    }
72}
73
74impl Apply for ClearBytes {
75    fn apply(
76        self,
77        db: &mut Db,
78        _env: Option<&dyn Environment>,
79    ) -> Result<Vec<Box<dyn Command>>, Error> {
80        let Self { range } = self;
81        let SpaceAddressRange { space, range: address_range } = range;
82        let offset = address_range.start;
83        let size = address_range.end - address_range.start;
84        let region = db.region_mut(space);
85        let before = region.snapshot_byte_ranges(offset, size);
86        region.clear_bytes(offset, size);
87        Ok(undo_byte_ranges((space, offset).into(), size, before))
88    }
89}
90
91#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
92pub struct SetConstantBytes {
93    pub range: SpaceAddressRange,
94    pub value: u8,
95}
96
97impl SetConstantBytes {
98    pub fn new(space: AddressSpace, offset: AddressValue, size: AddressValue, value: u8) -> Self {
99        Self {
100            range: (space, AddressRange::new(offset, offset + size)).into(),
101            value,
102        }
103    }
104}
105
106impl Apply for SetConstantBytes {
107    fn apply(
108        self,
109        db: &mut Db,
110        _env: Option<&dyn Environment>,
111    ) -> Result<Vec<Box<dyn Command>>, Error> {
112        let Self { range, value } = self;
113        let SpaceAddressRange { space, range: address_range } = range;
114        let offset = address_range.start;
115        let size = address_range.end - address_range.start;
116        let region = db.region_mut(space);
117        let before = region.snapshot_byte_ranges(offset, size);
118        region.set_constant(offset, size, value);
119        Ok(undo_byte_ranges((space, offset).into(), size, before))
120    }
121}
122
123fn undo_byte_ranges(
124    address: SpaceAddressValue,
125    size: AddressValue,
126    ranges: Vec<(AddressValue, ByteRange)>,
127) -> Vec<Box<dyn Command>> {
128    let SpaceAddressValue { space, offset } = address;
129    let mut undo = vec![boxed(ClearBytes {
130        range: (space, AddressRange::new(offset, offset + size)).into(),
131    })];
132    for (start, range) in ranges {
133        match range {
134            ByteRange::Mapped(file, file_offset, data) => {
135                undo.push(boxed(MapBytes {
136                    address: (space, start).into(),
137                    file,
138                    file_offset,
139                    size: data.len() as AddressValue,
140                }));
141            }
142            ByteRange::Constant(count, value) => {
143                undo.push(boxed(SetConstantBytes {
144                    range: (space, AddressRange::new(start, start + count as AddressValue)).into(),
145                    value,
146                }));
147            }
148        }
149    }
150    undo
151}
152
153// Address range + a `u8` field, both rendered in hex.
154serialize_test!(
155    constant_bytes_range_and_byte,
156    "set_constant_bytes(range=CODE:0x10..0x20, value=0xFF)",
157    SetConstantBytes {
158        range: (AddressSpace::Code, AddressRange::new(0x10, 0x20)).into(),
159        value: 0xFF,
160    }
161);