Skip to content

Commit 0166232

Browse files
committed
Add more debug commands
1 parent bb0284a commit 0166232

File tree

6 files changed

+65
-25
lines changed

6 files changed

+65
-25
lines changed

schematics/mlogv32.msch

9.92 KB
Binary file not shown.

src/bin/mlogv32.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ use binrw::{BinRead, BinWrite};
88
use clap::Parser;
99
use indicatif::ProgressIterator;
1010
use itertools::Itertools;
11-
use mindustry_rs::logic::vm::{
12-
Building, LogicVM, MEMORY_BANK, MEMORY_CELL, MESSAGE, MICRO_PROCESSOR, Processor, SWITCH,
13-
WORLD_PROCESSOR, decode_utf16,
14-
};
15-
use mindustry_rs::types::{Object, ProcessorConfig};
1611
use mindustry_rs::{
17-
logic::vm::{BuildingData, LogicVMBuilder},
18-
types::{Point2, Schematic},
12+
logic::vm::{
13+
Building, BuildingData, LValue, LogicVM, LogicVMBuilder, MEMORY_BANK, MEMORY_CELL, MESSAGE,
14+
MICRO_PROCESSOR, Processor, SWITCH, WORLD_PROCESSOR, decode_utf16,
15+
},
16+
types::{Object, Point2, ProcessorConfig, Schematic},
1917
};
2018
use prompted::input;
2119
use serde::Deserialize;
@@ -63,6 +61,8 @@ struct Metadata {
6361
rom_processors: usize,
6462
ram_processors: usize,
6563
icache_processors: usize,
64+
65+
mtime_frequency: usize,
6666
}
6767

6868
#[derive(Debug, Clone, Copy, Deserialize)]
@@ -97,9 +97,13 @@ fn get_building<'a>(vm: &'a LogicVM, position: MetaPoint2, name: &str) -> &'a Bu
9797
building
9898
}
9999

100-
fn print_var(processor: &Processor, name: &str) {
100+
fn print_var(processor: &Processor, name: &str, radix: Option<&&str>) {
101101
match processor.variable(name) {
102-
Some(value) => println!("{name} = {value:?}"),
102+
Some(value) => match radix {
103+
Some(&"x") => println!("{name} = {:#010x}", value.num() as u32),
104+
Some(&"b") => println!("{name} = {:#034b}", value.num() as u32),
105+
_ => println!("{name} = {value:?}"),
106+
},
103107
None => println!("{name} = <undefined>"),
104108
}
105109
}
@@ -229,7 +233,7 @@ fn main() -> Result<(), Box<dyn Error>> {
229233
if let BuildingData::Switch(paused) = &mut *pause_switch.data.borrow_mut()
230234
&& *paused
231235
&& let BuildingData::Switch(single_step) = &mut *single_step_switch.data.borrow_mut()
232-
&& let BuildingData::Processor(ctrl) = &*controller.data.borrow()
236+
&& let BuildingData::Processor(ctrl) = &mut *controller.data.borrow_mut()
233237
&& let BuildingData::Processor(config) = &mut *config.data.borrow_mut()
234238
&& let BuildingData::Memory(mem) = &*registers.data.borrow()
235239
{
@@ -256,17 +260,26 @@ fn main() -> Result<(), Box<dyn Error>> {
256260
*single_step = false;
257261
break;
258262
}
259-
"b" | "break" if cmd.len() >= 2 => {
260-
match u64::from_str_radix(cmd[1].trim_start_matches("0x"), 16) {
263+
"rs" | "restart" => {
264+
ctrl.set_variable("pc", 0.into())?;
265+
break;
266+
}
267+
"b" | "break" if cmd.len() >= 2 => match cmd[1] {
268+
"clear" => {
269+
config.set_variable("BREAKPOINT_ADDRESS", LValue::Null)?;
270+
println!("Breakpoint cleared.");
271+
}
272+
pc => match u64::from_str_radix(pc.trim_start_matches("0x"), 16) {
261273
Ok(pc) => {
262-
config.variables["BREAKPOINT_ADDRESS"]
263-
.set(&mut config.state, pc.into());
274+
config.set_variable("BREAKPOINT_ADDRESS", pc.into())?;
264275
println!("Breakpoint set: {pc:#010x}")
265276
}
266277
Err(_) => println!("Invalid address."),
267-
}
278+
},
279+
},
280+
"p" | "print" | "v" | "var" if cmd.len() >= 2 => {
281+
print_var(ctrl, cmd[1], cmd.get(2))
268282
}
269-
"p" | "print" | "v" | "var" if cmd.len() >= 2 => print_var(ctrl, cmd[1]),
270283
"i" | "inspect" if cmd.len() >= 3 => {
271284
let Ok(x) = cmd[1].parse() else {
272285
println!("Invalid x.");
@@ -282,10 +295,10 @@ fn main() -> Result<(), Box<dyn Error>> {
282295
BuildingData::Processor(p) => match cmd.get(3) {
283296
Some(&"*") => {
284297
for name in p.variables.keys().sorted() {
285-
print_var(p, name);
298+
print_var(p, name, cmd.get(4));
286299
}
287300
}
288-
Some(addr) => print_var(p, addr),
301+
Some(addr) => print_var(p, addr, cmd.get(4)),
289302
None => println!("{}", b.block.name),
290303
},
291304
BuildingData::Memory(mem) => {

src/logic/vm/instructions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ impl SimpleInstruction for PackColor {
905905
self.a.get(state).numf().clamp(0., 1.),
906906
)
907907
.into(),
908-
)
908+
);
909909
}
910910
}
911911

src/logic/vm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod processor;
44
mod variables;
55

66
pub use buildings::*;
7-
pub use processor::{Processor, ProcessorState, decode_utf16, encode_utf16};
7+
pub use processor::*;
88
pub use variables::{Content, ContentIDLookupError, LString, LValue, LVar};
99

1010
use std::{

src/logic/vm/processor.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{
99
use binrw::BinRead;
1010
use itertools::Itertools;
1111
use replace_with::replace_with_and_return;
12+
use thiserror::Error;
1213

1314
use super::{
1415
LValue, LogicVM, VMLoadError, VMLoadResult,
@@ -176,6 +177,27 @@ impl Processor {
176177
pub fn variable(&self, name: &str) -> Option<LValue> {
177178
self.variables.get(name).map(|v| v.get(&self.state))
178179
}
180+
181+
pub fn set_variable(&mut self, name: &str, value: LValue) -> Result<(), SetVariableError> {
182+
match self.variables.get(name) {
183+
Some(var) => {
184+
if var.set(&mut self.state, value) {
185+
Ok(())
186+
} else {
187+
Err(SetVariableError::Constant)
188+
}
189+
}
190+
None => Err(SetVariableError::NotFound),
191+
}
192+
}
193+
}
194+
195+
#[derive(Debug, Error)]
196+
pub enum SetVariableError {
197+
#[error("Variable not found.")]
198+
NotFound,
199+
#[error("Variable is a constant.")]
200+
Constant,
179201
}
180202

181203
#[derive(Debug, Clone)]
@@ -273,13 +295,13 @@ pub fn decode_utf16(value: &[u16]) -> String {
273295

274296
/// A representation of a link from this processor to a building.
275297
#[derive(Debug, Clone)]
276-
pub struct ProcessorLink {
298+
pub(super) struct ProcessorLink {
277299
pub name: String,
278300
pub position: Point2,
279301
}
280302

281303
#[derive(Debug)]
282-
pub struct ProcessorBuilder<'a> {
304+
pub(super) struct ProcessorBuilder<'a> {
283305
pub ipt: usize,
284306
pub privileged: bool,
285307
pub running_processors: Rc<Cell<usize>>,

src/logic/vm/variables.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl LVar {
129129
globals
130130
}
131131

132-
pub fn late_init_locals(
132+
pub(super) fn late_init_locals(
133133
variables: &mut HashMap<String, LVar>,
134134
position: Point2,
135135
links: &[ProcessorLink],
@@ -179,10 +179,12 @@ impl LVar {
179179
}
180180
}
181181

182-
pub fn set(&self, state: &mut ProcessorState, value: LValue) {
182+
/// Returns true if the variable was successfully set.
183+
pub fn set(&self, state: &mut ProcessorState, value: LValue) -> bool {
183184
match self {
184185
Self::Variable(ptr) => {
185186
*ptr.borrow_mut() = value;
187+
true
186188
}
187189
Self::Counter => {
188190
if let LValue::Number(n) = value {
@@ -192,9 +194,12 @@ impl LVar {
192194
} else {
193195
0
194196
};
197+
true
198+
} else {
199+
false
195200
}
196201
}
197-
_ => {}
202+
_ => false,
198203
}
199204
}
200205

0 commit comments

Comments
 (0)