evunit 1.3.4

A unit testing program for the Game Boy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use clap::Parser;
use evunit::prelude::*;
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::{stdin, BufReader, Read};
use std::process::exit;

pub const SILENCE_NONE: u8 = 0;
pub const SILENCE_PASSING: u8 = 1; // Silences passing messages when tests succeed.
pub const SILENCE_ALL: u8 = 2; // Silences all output unless an error occurs.

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
struct Cli {
	/// Path to the test configuration file
	#[clap(short, long, value_parser, value_name = "PATH")]
	config: String,

	/// Directory where crash dumps should be placed. Each dump contains the entire address space in a text file, seperated by memory type.
	#[clap(short, long, value_parser, value_name = "PATH")]
	dump_dir: Option<String>,

	/// Silence passing tests. Pass -s again to silence all output unless an error occurs.
	#[clap(short, long, action = clap::ArgAction::Count)]
	silent: u8,

	/// Path to a symfile
	#[clap(short = 'n', long, value_parser, value_name = "PATH")]
	symfile: Option<String>,

	/// Path to the ROM
	#[clap(value_parser, value_name = "PATH")]
	rom: String,
}

fn read_config(path: &str, symfile: &HashMap<String, (u32, u16)>) -> Vec<TestConfig> {
	fn parse_u8(value: &toml::Value, hint: &str) -> Option<u8> {
		match value {
			toml::Value::Integer(value) => {
				if -128 <= *value && *value < 256 {
					Some(*value as u8)
				} else {
					eprintln!("Value of `{hint}` must be an 8-bit integer.");
					None
				}
			}
			_ => {
				eprintln!("Value of `{hint}` must be an 8-bit integer.");
				None
			}
		}
	}

	fn parse_u16(
		value: &toml::Value,
		hint: &str,
		symfile: &HashMap<String, (u32, u16)>,
	) -> Option<u16> {
		match value {
			toml::Value::Integer(value) => {
				if -32768 <= *value && *value < 65536 {
					Some(*value as u16)
				} else {
					eprintln!("Value of `{hint}` must be a 16-bit integer.");
					None
				}
			}
			toml::Value::String(value) => {
				if let Some((_, addr)) = symfile.get(value) {
					Some(*addr)
				} else {
					eprintln!("Symbol \"{value}\" not found.");
					exit(1);
				}
			}
			_ => {
				eprintln!("Value of `{hint}` must be a 16-bit integer.");
				None
			}
		}
	}

	fn parse_bool(value: &toml::Value, hint: &str) -> Option<bool> {
		if let toml::Value::Boolean(value) = value {
			Some(*value)
		} else {
			eprintln!("Value of `{hint}` must be a boolean.");
			None
		}
	}

	fn parse_address(address: &str, symfile: &HashMap<String, (u32, u16)>) -> Option<u16> {
		if let Some((_, address)) = symfile.get(address) {
			// Attempt to get address from symfile
			Some(*address)
		} else if let Ok(address) = u16::deserialize(toml::de::ValueDeserializer::new(address)) {
			// Attempt to parse address as an u16 value
			Some(address)
		} else {
			// Failed to parse address
			None
		}
	}

	fn parse_memory(name: &str, value: &toml::Value) -> Result<Vec<u8>, String> {
		match value {
			toml::Value::Integer(value) => {
				if *value > 255 || *value < -128 {
					let value_array = (*value as i64)
						.to_le_bytes()
						.iter()
						.skip_while(|b| **b == 0)
						.map(|b| format!("0x{b:02x}"))
						.collect::<Vec<_>>()
						.join(", ");

					// Disallow non 8-bit values and present alternative
					Err(format!(
						"\"{value}\" is not an 8-bit value. Try \"[{value_array}]\" instead."
					))
				} else {
					// Treat any byte size number as a byte
					Ok(vec![(*value as u8)])
				}
			}
			toml::Value::String(value) => {
				if !value.is_ascii() {
					// Disallow any strings which contain non-ASCII values
					Err(format!(
						"String value \"{value}\" contains non-ASCII characters"
					))
				} else {
					// Convert string into sequence of bytes
					Ok(value.bytes().collect::<Vec<_>>())
				}
			}
			toml::Value::Array(value) => {
				// Recursively call function on all toml::Value and return their collected result
				value
					.iter()
					.map(|v| parse_memory(name, v))
					.collect::<Result<Vec<Vec<u8>>, String>>()
					.map(|mem| mem.into_iter().flatten().collect::<Vec<u8>>())
			}
			toml::Value::Boolean(value) => {
				// Convert bool into either a 1 or a 0
				Ok(vec![if *value { 1 } else { 0 }])
			}
			_ => {
				// Other types return error as they are not supported
				Err(format!("Unsupported value for {name}: {value}"))
			}
		}
	}

	fn parse_memory_assignment(
		name: &str,
		value: &toml::Value,
		symfile: &HashMap<String, (u32, u16)>,
	) -> Result<Vec<(u16, u8)>, String> {
		let address = parse_address(name, symfile);
		if address.is_none() {
			return Err(format!("Address \"{}\" is not a valid address", name));
		}
		let address = address.unwrap();

		parse_memory(name, value).map(|data| {
			data.iter()
				.enumerate()
				.map(|(i, b)| (address + (i as u16), *b))
				.collect::<Vec<(u16, u8)>>()
		})
	}

	fn parse_configuration(
		test: &mut TestConfig,
		key: &str,
		value: &toml::Value,
		symfile: &HashMap<String, (u32, u16)>,
	) {
		match key {
			"a" => test.initial.a = parse_u8(value, key),
			"b" => test.initial.b = parse_u8(value, key),
			"c" => test.initial.c = parse_u8(value, key),
			"d" => test.initial.d = parse_u8(value, key),
			"e" => test.initial.e = parse_u8(value, key),
			"h" => test.initial.h = parse_u8(value, key),
			"l" => test.initial.l = parse_u8(value, key),
			"f.z" => test.initial.zf = parse_bool(value, key),
			"f.n" => test.initial.nf = parse_bool(value, key),
			"f.h" => test.initial.hf = parse_bool(value, key),
			"f.c" => test.initial.cf = parse_bool(value, key),
			"bc" => test.initial.bc = parse_u16(value, key, symfile),
			"de" => test.initial.de = parse_u16(value, key, symfile),
			"hl" => test.initial.hl = parse_u16(value, key, symfile),
			"pc" => test.initial.pc = parse_u16(value, key, symfile),
			"sp" => test.initial.sp = parse_u16(value, key, symfile),
			"caller" => test.caller_address = parse_u16(value, key, symfile).unwrap_or(0xFFFF),
			"crash" => {
				if let toml::Value::Integer(_) | toml::Value::String(_) = value {
					if let Some(address) = parse_u16(value, key, symfile) {
						test.crash_addresses.push(address);
					}
				} else if let toml::Value::Array(addresses) = value {
					for i in addresses {
						if let Some(address) = parse_u16(i, key, symfile) {
							test.crash_addresses.push(address);
						}
					}
				} else {
					eprintln!(
						"Value of {key} must be a 16-bit integer or an array of 16-bit integers"
					)
				}
			}
			"enable-breakpoints" => test.enable_breakpoints = parse_bool(value, key).unwrap(),
			"exit" => {
				if let toml::Value::Integer(_) | toml::Value::String(_) = value {
					if let Some(address) = parse_u16(value, key, symfile) {
						test.exit_addresses.push(address);
					}
				} else if let toml::Value::Array(addresses) = value {
					for i in addresses {
						if let Some(address) = parse_u16(i, key, symfile) {
							test.exit_addresses.push(address);
						}
					}
				} else {
					eprintln!(
						"Value of {key} must be a 16-bit integer or an array of 16-bit integers"
					)
				}
			}
			"timeout" => {
				if let toml::Value::Integer(value) = value {
					test.timeout = *value as usize;
				} else {
					eprintln!("Value of `{key}` must be an integer.");
				}
			}
			"result" => {
				if let toml::Value::Table(value) = value {
					let mut result = Registers::new();
					for (key, value) in value {
						match key.as_str() {
							"a" => result.a = parse_u8(value, key),
							"b" => result.b = parse_u8(value, key),
							"c" => result.c = parse_u8(value, key),
							"d" => result.d = parse_u8(value, key),
							"e" => result.e = parse_u8(value, key),
							"h" => result.h = parse_u8(value, key),
							"l" => result.l = parse_u8(value, key),
							"f.z" => result.zf = parse_bool(value, key),
							"f.n" => result.nf = parse_bool(value, key),
							"f.h" => result.hf = parse_bool(value, key),
							"f.c" => result.cf = parse_bool(value, key),
							"bc" => result.bc = parse_u16(value, key, symfile),
							"de" => result.de = parse_u16(value, key, symfile),
							"hl" => result.hl = parse_u16(value, key, symfile),
							"pc" => result.pc = parse_u16(value, key, symfile),
							"sp" => result.sp = parse_u16(value, key, symfile),
							&_ => {
								let mut indices = key.char_indices();
								if let (Some((_, '[')), Some((begin, _)), Some((end, ']'))) =
									(indices.next(), indices.next(), indices.last())
								{
									match parse_memory_assignment(&key[begin..end], value, symfile) {
										Err(cause) => eprintln!("{}", cause),
										Ok(data) => result.memory = data,
									};
								} else {
									eprintln!("Unknown config key {key} = {value:?}");
								}
							}
						}
					}
					test.result = Some(result);
				} else {
					eprintln!("Value of `{key}` must be a table.");
				}
			}
			"stack" => {
				match parse_memory("stack", value) {
					Err(cause) => eprintln!("{}", cause),
					Ok(data) => test.stack.extend(data),
				};
			}
			_ => {
				let mut indices = key.char_indices();
				if let (Some((_, '[')), Some((begin, _)), Some((end, ']'))) =
					(indices.next(), indices.next(), indices.last())
				{
					match parse_memory_assignment(&key[begin..end], value, symfile) {
						Err(cause) => eprintln!("{}", cause),
						Ok(data) => test.initial.memory = data,
					};
				} else {
					eprintln!("Unknown config key {key} = {value:?}");
				}
			}
		}
	}

	let mut global_config = TestConfig::new(String::from("Global"));
	let mut tests: Vec<TestConfig> = vec![];
	let toml_file = path.parse::<toml::Value>().unwrap_or_else(|msg| {
		eprintln!("Failed to parse config file: {msg}");
		exit(1);
	});

	let config = if let toml::Value::Table(config) = toml_file {
		config
	} else {
		panic!("TOML root is not a table (Please report this and provide the TOML file used.)");
	};

	for (key, value) in config {
		if let toml::Value::Table(table) = value {
			let mut test = global_config.clone();
			test.name = key;
			for (key, value) in table.iter() {
				parse_configuration(&mut test, key, value, symfile);
			}
			tests.push(test);
		} else {
			parse_configuration(&mut global_config, &key, &value, symfile);
		}
	}

	tests
}

fn main() {
	fn open_input(path: &str) -> Box<dyn Read> {
		if path == "-" {
			Box::new(BufReader::new(stdin()))
		} else {
			Box::new(File::open(path).unwrap_or_else(|msg| {
				eprintln!("Failed to open {path}: {msg}");
				exit(1)
			}))
		}
	}

	let cli = Cli::parse();

	let rom_path = cli.rom;
	let config_path = cli.config;

	let rom = open_rom(&rom_path);

	let address_space = AddressSpace::with(&rom);

	let mut config_text = String::new();
	open_input(&config_path)
		.read_to_string(&mut config_text)
		.unwrap_or_else(|error| {
			eprintln!("Failed to read {config_path}: {error}");
			exit(1);
		});

	let symfile = open_symfile(cli.symfile.as_ref().map(|x| x.as_ref()));
	let tests = read_config(&config_text, &symfile);

	let silence_level = match cli.silent {
		SILENCE_NONE => SilenceLevel::None,
		SILENCE_PASSING => SilenceLevel::Passing,
		SILENCE_ALL.. => SilenceLevel::All,
	};

	let mut logger = Logger::new(silence_level, &rom_path);

	// create dump dir if it does not exist already
	if let Some(ref dump_dir) = cli.dump_dir {
		if let Err(msg) = fs::create_dir_all(dump_dir) {
			eprintln!("Failed to create dump dir {dump_dir}: {msg}");
			exit(1);
		}
	}

	for test in &tests {
		let mut cpu_state = cpu::State::new(address_space.clone());
		let mut test_logger = logger.make_test(test);

		if test.run(&mut cpu_state, &mut test_logger) {
			continue;
		}

		if let Some(ref dump_dir) = cli.dump_dir {
			let path = String::from(dump_dir) + &format!("/{}.txt", test.name);

			match File::create(&path) {
				Ok(file) => cpu_state.address_space.dump(file).unwrap_or_else(|msg| {
					eprintln!("Failed to write dump to {path}: {msg}");
				}),
				Err(msg) => eprintln!("Failed to open {path}: {msg}"),
			}
		}
	}

	if !logger.finish() {
		exit(1);
	}
}