i8051_disassembler/commands/
decoding.rs1use crate::address::AddressValue;
2use crate::db::{Db, Error, ErrorKind};
3
4use super::{Apply, Command, Environment, boxed};
5
6#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
7pub struct SetAddressBits {
8 pub space: String,
9 pub bits: AddressValue,
10}
11
12register!(SetAddressBits(
13 space: String,
18 bits: AddressValue,
19));
20
21impl Apply for SetAddressBits {
22 fn apply(
23 self,
24 db: &mut Db,
25 _env: Option<&dyn Environment>,
26 ) -> Result<Vec<Box<dyn Command>>, Error> {
27 let Self { space, bits } = self;
28 let target = db.resolve_space(&space)?;
29 if bits == 0 || bits > AddressValue::BITS {
30 return Err(ErrorKind::InvalidArgument {
31 value: bits.to_string(),
32 reason: "address width must be between 1 and 32 bits",
33 }
34 .into());
35 }
36 let previous = db.region_mut(target).set_address_bits(Some(bits as u8));
37 Ok(undo_for(&space, previous))
38 }
39}
40
41#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
42pub struct ClearAddressBits {
43 pub space: String,
44}
45
46register!(ClearAddressBits(
47 space: String,
49));
50
51impl Apply for ClearAddressBits {
52 fn apply(
53 self,
54 db: &mut Db,
55 _env: Option<&dyn Environment>,
56 ) -> Result<Vec<Box<dyn Command>>, Error> {
57 let Self { space } = self;
58 let target = db.resolve_space(&space)?;
59 let previous = db.region_mut(target).set_address_bits(None);
60 Ok(undo_for(&space, previous))
61 }
62}
63
64fn undo_for(space: &str, previous: Option<u8>) -> Vec<Box<dyn Command>> {
65 match previous {
66 Some(bits) => vec![boxed(SetAddressBits {
67 space: space.to_string(),
68 bits: AddressValue::from(bits),
69 })],
70 None => vec![boxed(ClearAddressBits {
71 space: space.to_string(),
72 })],
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use crate::address::PhysicalAddr;
80 use crate::commands::{AutoDisassemble, MapBytes};
81 use crate::platform::i8051::CODE;
82
83 struct Env;
84 impl crate::commands::Environment for Env {
85 fn load_file_bytes(
86 &self,
87 _f: &str,
88 offset: usize,
89 size: AddressValue,
90 ) -> Result<Vec<u8>, std::io::Error> {
91 const IMAGE: [u8; 8] = [0x02, 0xF0, 0x04, 0x22, 0x00, 0x22, 0x00, 0x22];
94 Ok(IMAGE[offset..offset + size as usize].to_vec())
95 }
96 }
97
98 #[test]
101 fn narrowing_resolves_references() {
102 let mut db = Db::with_platform(crate::platform::i8051::platform());
103 db.apply(
104 boxed(MapBytes::new((CODE, 0), "img", 0usize, 8u32)),
105 Some(&Env),
106 )
107 .unwrap();
108 db.apply(boxed(AutoDisassemble::new((CODE, 0u32))), Some(&Env))
109 .unwrap();
110
111 let wrapped = PhysicalAddr {
112 space: CODE,
113 offset: 0x04,
114 };
115 let raw = PhysicalAddr {
116 space: CODE,
117 offset: 0xF004,
118 };
119 assert!(
120 db.xrefs_to(&wrapped).is_empty(),
121 "unmasked, the jump leaves the image"
122 );
123 assert_eq!(db.xrefs_to(&raw).len(), 1);
124
125 let undo = db
126 .apply(
127 boxed(SetAddressBits {
128 space: "CODE".to_string(),
129 bits: 3,
130 }),
131 Some(&Env),
132 )
133 .unwrap();
134 assert_eq!(db.xrefs_to(&wrapped).len(), 1, "masked, it reaches 0x04");
135 assert!(db.xrefs_to(&raw).is_empty(), "and no longer points outside");
136 let dsl = crate::store::to_dsl_many(&db.to_commands());
139 assert!(dsl.contains("set_address_bits"), "{dsl}");
140 let mut replayed = Db::new();
141 for command in crate::store::from_dsl_many(&dsl).expect("export parses") {
142 replayed.apply(command, Some(&Env)).expect("export replays");
143 }
144 assert_eq!(replayed.xrefs_to(&wrapped).len(), 1, "reloaded: {dsl}");
145
146 for command in undo {
147 db.apply(command, Some(&Env)).unwrap();
148 }
149 assert_eq!(db.xrefs_to(&raw).len(), 1, "undo restores the full width");
150 }
151
152 #[test]
154 fn impossible_width_refused() {
155 let mut db = Db::with_platform(crate::platform::i8051::platform());
156 for bits in [0u32, 33] {
157 let err = db
158 .apply(
159 boxed(SetAddressBits {
160 space: "CODE".to_string(),
161 bits,
162 }),
163 None,
164 )
165 .expect_err("width must be 1..=32");
166 assert!(
167 matches!(err.what, ErrorKind::InvalidArgument { .. }),
168 "{err:?}"
169 );
170 }
171 let err = db
173 .apply(
174 boxed(SetAddressBits {
175 space: "COED".to_string(),
176 bits: 12,
177 }),
178 None,
179 )
180 .expect_err("a space this database does not have");
181 assert!(matches!(
182 &err.what,
183 ErrorKind::UnknownSpace { name, suggestion: Some(s) } if name == "COED" && s == "CODE"
184 ));
185 }
186}