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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
use crate::mapping::{Mapping, SectionType, Segment};
use std::collections::HashMap;
// Section of the configuration being parsed.
enum Section {
Memory,
Segment,
Symbol,
}
// Same as mapping::Mapping but before translating symbols into it. Used when
// fetching the values from the configuration file but before symbols have been
// all loaded.
struct RawMapping {
name: String,
start: String,
size: String,
fill: Option<String>,
ignore: bool,
line_num: usize,
segments: String,
}
// Fetch a line of values in the format of "Name: key1=value1, key2=value2,
// ...;". The semicolon at the end is not considered and the given line should
// not contain it as the caller must have check it beforehand. Returns a tuple
// which contain the name (i.e. what's before the colon) and the line of values
// (i.e. after the colon and before the semicolon).
fn fetch_line_values(line: &str, line_num: usize) -> Result<(String, String), String> {
let mut name_values = line.split(':');
let Some(name) = name_values.next() else {
return Err(format!(
"line does not follow 'key: values' format (line {})",
line_num
));
};
let Some(values_line) = name_values.next() else {
return Err(format!(
"line does not follow 'key: values' format (line {})",
line_num
));
};
if name_values.next().is_some() {
return Err(format!(
"line does not follow 'key: values' format (line {})",
line_num
));
}
Ok((name.trim().to_string(), values_line.trim().to_string()))
}
// Returns the value contained in the given `string` as an hexadecimal number.
// If it doesn't start with '$', then it's assumed to be a symbol reference that
// can be found in `symbols`.
fn get_hex_from(
string: &String,
line_num: usize,
symbols: &HashMap<String, usize>,
) -> Result<usize, String> {
if string.is_empty() {
return Ok(0);
}
if !string.starts_with('$') {
match symbols.get(string) {
Some(value) => return Ok(*value),
None => return Err(format!("malformed hex value (line {})", line_num)),
}
}
let val = &string[1..string.len()];
if val.is_empty() || val.len() > 4 {
return Err(format!("malformed hex value (line {})", line_num));
}
Ok(usize::from_str_radix(val, 16).unwrap())
}
// Returns the RawMapping that can be extracted by treating the given `line` as
// a line from the "Memory" section.
fn fetch_memory_definition(line: &str, line_num: usize) -> Result<RawMapping, String> {
let (name, values) = fetch_line_values(line, line_num)?;
let mut res = RawMapping {
name,
start: String::from(""),
size: String::from(""),
fill: None,
ignore: false,
line_num,
segments: String::from(""),
};
for value in values.split(',') {
let mut key_value = value.trim().split('=');
match key_value.next() {
Some(key) => {
let val = key_value.next().unwrap_or("").trim();
match key.trim() {
"file" => res.ignore = val != "%O",
"fill" => {
if res.fill.is_none() {
res.fill = Some(String::from(""));
}
}
"fillval" => res.fill = Some(val.to_string()),
"start" => res.start = val.to_string(),
"size" => res.size = val.to_string(),
"segments" => res.segments = val.to_string(),
_ => {}
}
}
None => return Err(format!("malformed key-value (line {})", line_num)),
}
}
Ok(res)
}
// Returns a tuple with the name of the segment and the memory section being
// referenced in this segment.
fn fetch_segment_definition(line: &str, line_num: usize) -> Result<(String, String), String> {
let (name, values) = fetch_line_values(line, line_num)?;
if find_value("start", &values, line_num).is_ok() {
return Err("setting a 'start' at the segment level is not supported".to_string());
}
Ok((name, find_value("load", &values, line_num)?))
}
// Returns the value for the referenced `key` which is inside of the string of
// `values`.
fn find_value(key: &str, values: &str, line_num: usize) -> Result<String, String> {
for value in values.split(',') {
let mut key_value = value.trim().split('=');
match key_value.next() {
Some(k) => {
if k.trim() == key {
let val = key_value.next().unwrap_or("").trim();
return Ok(val.to_string());
}
}
None => return Err(format!("malformed key-value (line {})", line_num)),
}
}
Err(format!(
"could not find '{}' definition (line {})",
key, line_num
))
}
/// Parse the given blob of `text` as a .cfg file as it's expected by ld65:
/// https://www.cc65.org/doc/ld65-5.html.
pub fn parse_cfg_file(text: &str) -> Result<Vec<Mapping>, String> {
let mut values = vec![];
let mut raw_segments = vec![];
let mut should_skip = false;
let mut section = None;
let mut symbols = HashMap::new();
// 1. Get the raw data and build the `symbols` hash.
//
// This is done by considering only three sections: symbols, memory, and segments.
for (idx, line) in text.lines().enumerate() {
// For this given line, maybe it's empty or it's a comment. Another case
// is that it's a section we just don't care. In either case, just skip
// to the next line.
let l = line.trim();
if should_skip {
if l == "}" {
// End of section, we might care about the next line.
should_skip = false;
}
continue;
}
if l.is_empty() || l.starts_with('#') {
continue;
}
// If we are inside of a section, parse it.
if section.is_some() {
if l == "}" {
should_skip = false;
section = None;
continue;
}
// Parse up until the closing semicolon, as passed it there might be
// an inline comment.
let tline = match l.find(';') {
Some(idx) => &l[0..idx],
None => {
return Err(format!(
"line does not end with a semicolon (line {})",
idx + 1
))
}
};
// For each section there is a different action for the current
// line. Handle this now.
match section.as_ref().unwrap() {
Section::Memory => {
let definition = fetch_memory_definition(tline, idx + 1)?;
if !definition.ignore {
values.push(definition);
}
}
Section::Segment => raw_segments.push(fetch_segment_definition(tline, idx + 1)?),
Section::Symbol => {
let (name, values) = fetch_line_values(tline, idx + 1)?;
let value = find_value("value", &values, idx + 1)?;
symbols.insert(name, get_hex_from(&value, idx + 1, &symbols)?);
}
}
continue;
}
// We are not in a section and the line is not empty. Thus, this has to
// be a section definition. Parse the name and check if we actually have
// to care about it or not.
match l.find('{') {
Some(idx) => {
let id = l[0..idx].trim().to_lowercase();
match id.as_str() {
"memory" => section = Some(Section::Memory),
"segments" => section = Some(Section::Segment),
"symbols" => section = Some(Section::Symbol),
_ => should_skip = true,
}
}
None => {
return Err(format!(
"section must end with an opening bracket (line {})",
idx + 1
))
}
};
}
let mut header = true;
let mut res = vec![];
let mut section_type = SectionType::Header;
// 2. Parse each data point so we can build a Mapping out of it.
for mapping in &values {
// We can now figure out the `start`, the `size` and the `fill`
// properties given that the `symbols` should all be known at this
// point.
let start = get_hex_from(&mapping.start, mapping.line_num, &symbols)? as u16;
let size = get_hex_from(&mapping.size, mapping.line_num, &symbols)?;
let fill = match &mapping.fill {
Some(f) => Some(get_hex_from(f, mapping.line_num, &symbols)? as u8),
None => None,
};
// The section type is a bit tricky because that's not something
// considered on ld65. Hence, we make these assumptions:
// 1. If it's the first mapping we see, then it's the header.
// 2. If after the header it claims to start at 0x00, then from now on
// it's CHR ROM.
// 3. Otherwise it's PRG ROM unless in a previous iteration we
// realized it's CHR ROM.
if header {
header = false;
} else if start == 0x0000 {
section_type = SectionType::ChrRom;
} else if section_type != SectionType::ChrRom {
section_type = SectionType::PrgRom;
};
// Push in order the segments which are to be loaded on the current
// mapping.
let mut segments = vec![];
for (segment_name, load) in &raw_segments {
if mapping.name == *load {
segments.push(Segment::from(segment_name.as_str()));
}
}
res.push(Mapping {
name: mapping.name.clone(),
start,
size,
offset: 0,
fill,
segments,
section_type: section_type.clone(),
})
}
Ok(res)
}
fn get_segments_from(value: &str, line: usize) -> Result<Vec<Segment>, String> {
if !value.starts_with('[') || !value.ends_with(']') {
return Err(format!("should be enclosed inside of [] (line {})", line));
}
let mut res = vec![];
for name in value[1..value.len() - 1].split(' ') {
let trimmed_name = name.trim();
res.push(Segment::from(trimmed_name));
}
Ok(res)
}
/// Parse the given blob of `text` as a simplified .cfg file.
pub fn parse_nasm_cfg_file(text: &str) -> Result<Vec<Mapping>, String> {
let mut mappings = vec![];
let symbols = HashMap::new();
let mut header = true;
let mut section_type = SectionType::Header;
for (idx, line) in text.lines().enumerate() {
// Skip empty lines and comments.
let l = line.trim();
if l.is_empty() || l.starts_with('#') {
continue;
}
let real_line = match l.find(';') {
Some(idx) => &l[0..idx],
None => {
return Err(format!(
"line does not end with a semicolon (line {})",
idx + 1
))
}
};
// Parse the mapping definition.
let mapping = fetch_memory_definition(real_line, idx + 1)?;
// Parse hexadecimal values from start, size and fill.
let start = get_hex_from(&mapping.start, mapping.line_num, &symbols)? as u16;
let size = get_hex_from(&mapping.size, mapping.line_num, &symbols)?;
let fill = match &mapping.fill {
Some(f) => Some(get_hex_from(f, mapping.line_num, &symbols)? as u8),
None => None,
};
// As with 'parse_cfg_file', the section type:
// 1. If it's the first mapping we see, then it's the header.
// 2. If after the header it claims to start at 0x00, then from now on
// it's CHR ROM.
// 3. Otherwise it's PRG ROM unless in a previous iteration we
// realized it's CHR ROM.
if header {
header = false;
} else if start == 0x0000 {
section_type = SectionType::ChrRom;
} else if section_type != SectionType::ChrRom {
section_type = SectionType::PrgRom;
};
mappings.push(Mapping {
name: mapping.name,
offset: 0,
start,
size,
fill,
section_type: section_type.clone(),
segments: get_segments_from(&mapping.segments, idx + 1)?,
});
}
Ok(mappings)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_default_cc65_configuration() {
let res = parse_cfg_file(
r#"
SYMBOLS {
__STACKSIZE__: type = weak, value = $0300; # 3 pages stack
}
MEMORY {
ZP: file = "", start = $0002, size = $001A, type = rw, define = yes;
# INES Cartridge Header
HEADER: file = %O, start = $0000, size = $0010, fill = yes;
# 2 16K ROM Banks
# - startup
# - code
# - rodata
# - data (load)
ROM0: file = %O, start = $8000, size = $7FFA, fill = yes, define = yes;
# Hardware Vectors at End of 2nd 8K ROM
ROMV: file = %O, start = $FFFA, size = $0006, fill = yes;
# 1 8k CHR Bank
ROM2: file = %O, start = $0000, size = $2000, fill = yes;
# standard 2k SRAM (-zeropage)
# $0100-$0200 cpu stack
# $0200-$0500 3 pages for ppu memory write buffer
# $0500-$0800 3 pages for cc65 parameter stack
SRAM: file = "", start = $0500, size = __STACKSIZE__, define = yes;
# additional 8K SRAM Bank
# - data (run)
# - bss
# - heap
RAM: file = "", start = $6000, size = $2000, define = yes;
}
SEGMENTS {
ZEROPAGE: load = ZP, type = zp;
HEADER: load = HEADER, type = ro;
STARTUP: load = ROM0, type = ro, define = yes;
LOWCODE: load = ROM0, type = ro, optional = yes;
ONCE: load = ROM0, type = ro, optional = yes;
CODE: load = ROM0, type = ro, define = yes;
RODATA: load = ROM0, type = ro, define = yes;
DATA: load = ROM0, run = RAM, type = rw, define = yes;
VECTORS: load = ROMV, type = rw;
CHARS: load = ROM2, type = rw;
BSS: load = RAM, type = bss, define = yes;
}
FEATURES {
CONDES: type = constructor,
label = __CONSTRUCTOR_TABLE__,
count = __CONSTRUCTOR_COUNT__,
segment = ONCE;
CONDES: type = destructor,
label = __DESTRUCTOR_TABLE__,
count = __DESTRUCTOR_COUNT__,
segment = RODATA;
CONDES: type = interruptor,
label = __INTERRUPTOR_TABLE__,
count = __INTERRUPTOR_COUNT__,
segment = RODATA,
import = __CALLIRQ__;
}
"#,
)
.unwrap();
assert_eq!(res.len(), 4);
let header = &res[0];
assert_eq!(header.name, "HEADER");
assert_eq!(header.start, 0x00);
assert_eq!(header.size, 0x10);
assert_eq!(header.fill, Some(0x00));
assert_eq!(header.section_type, SectionType::Header);
assert_eq!(
header
.segments
.iter()
.map(|x| x.name.clone())
.collect::<Vec<_>>(),
&["HEADER"]
);
let rom0 = &res[1];
assert_eq!(rom0.name, "ROM0");
assert_eq!(rom0.start, 0x8000);
assert_eq!(rom0.size, 0x7FFA);
assert_eq!(rom0.fill, Some(0x00));
assert_eq!(rom0.section_type, SectionType::PrgRom);
assert_eq!(
rom0.segments
.iter()
.map(|x| x.name.clone())
.collect::<Vec<_>>(),
&["STARTUP", "LOWCODE", "ONCE", "CODE", "RODATA", "DATA"]
);
let romv = &res[2];
assert_eq!(romv.name, "ROMV");
assert_eq!(romv.start, 0xFFFA);
assert_eq!(romv.size, 0x06);
assert_eq!(romv.fill, Some(0x00));
assert_eq!(romv.section_type, SectionType::PrgRom);
assert_eq!(
romv.segments
.iter()
.map(|x| x.name.clone())
.collect::<Vec<_>>(),
&["VECTORS"]
);
let rom2 = &res[3];
assert_eq!(rom2.name, "ROM2");
assert_eq!(rom2.start, 0x0000);
assert_eq!(rom2.size, 0x2000);
assert_eq!(rom2.fill, Some(0x00));
assert_eq!(rom2.section_type, SectionType::ChrRom);
assert_eq!(
rom2.segments
.iter()
.map(|x| x.name.clone())
.collect::<Vec<_>>(),
&["CHARS"]
);
}
#[test]
fn parse_unrom_configuration() {
// Coming from https://github.com/mssola/code.nes.
let res = parse_cfg_file(
r#"
##
# This is a very minimalistic linker configuration for UNROM chips. This
# configuration assumes the standard UNROM configuration, with two regions
# defined that split $8000-$FFFF, where the first half is swappable. For the
# bank switching there are 7 banks, which coupled with the fixed region it sums
# up 128KB of total PRG-ROM.
#
# NOTE: it assumes that only assembly is being used, and thus a lot of magic
# required for C programs is missing.
# NOTE: it is following the example of games such as Castlevania or Megaman.
# Thus, there is no CHR segment because it's all done through RAM.
MEMORY {
# iNES header.
HEADER: start = $0, size = $10, fill = yes;
# Program RAM. Available if a battery-backed RAM was requested.
WRAM: file = "" start = $6000, size = $2000, define = yes;
# Swappable ROM addresses. Note the filled values. This is done so it's also
# clear by inspecting the memory which bank we are on. This can come in
# handy when inspecting things with an hex editor.
PRG0: start = $8000, size = $4000, fill = yes, fillval = $f8, define = yes;
PRG1: start = $8000, size = $4000, fill = yes, fillval = $f9, define = yes;
PRG2: start = $8000, size = $4000, fill = yes, fillval = $fa, define = yes;
PRG3: start = $8000, size = $4000, fill = yes, fillval = $fb, define = yes;
PRG4: start = $8000, size = $4000, fill = yes, fillval = $fc, define = yes;
PRG5: start = $8000, size = $4000, fill = yes, fillval = $fd, define = yes;
PRG6: start = $8000, size = $4000, fill = yes, fillval = $fe, define = yes;
# Fixed ROM address.
PRG: start = $C000, size = $4000, fill = yes, fillval = $ff, define = yes;
}
SEGMENTS {
# iNES header.
HEADER: load = HEADER, type = ro;
# This is the fixed bank, that spans $C000-$FFFF. Make sure to put the reset
# code and basic stuff that you don't want ever to be gone here. This will
# include stuff like the reset code, bank switching utilities, etc.
FIXED: load = PRG, type = ro, define = yes;
##
# Swappable banks.
BANK0: load = PRG0, type = ro, define = yes;
BANK1: load = PRG1, type = ro, define = yes;
BANK2: load = PRG2, type = ro, define = yes;
BANK3: load = PRG3, type = ro, define = yes;
BANK4: load = PRG4, type = ro, define = yes;
BANK5: load = PRG5, type = ro, define = yes;
BANK6: load = PRG6, type = ro, define = yes;
# Last but not least, the vectors must be the last thing and they are
# expecting a very special place on the fixed bank.
VECTORS: load = PRG, type = ro;
}
"#,
)
.unwrap();
assert_eq!(res.len(), 9);
let header = &res[0];
assert_eq!(header.name, "HEADER");
assert_eq!(header.start, 0x00);
assert_eq!(header.size, 0x10);
assert_eq!(header.fill, Some(0x00));
assert_eq!(header.section_type, SectionType::Header);
assert_eq!(
header
.segments
.iter()
.map(|x| x.name.clone())
.collect::<Vec<_>>(),
&["HEADER"]
);
for i in 1..=7 {
let prg = &res[i];
assert_eq!(prg.name, format!("PRG{}", i - 1));
assert_eq!(prg.start, 0x8000);
assert_eq!(prg.size, 0x4000);
assert_eq!(prg.fill, Some(0xf8 + i as u8 - 1));
assert_eq!(prg.section_type, SectionType::PrgRom);
assert_eq!(
prg.segments
.iter()
.map(|x| x.name.clone())
.collect::<Vec<_>>(),
&[format!("BANK{}", i - 1)]
);
}
let prg = &res[8];
assert_eq!(prg.name, "PRG");
assert_eq!(prg.start, 0xC000);
assert_eq!(prg.size, 0x4000);
assert_eq!(prg.fill, Some(0xFF));
assert_eq!(prg.section_type, SectionType::PrgRom);
assert_eq!(
prg.segments
.iter()
.map(|x| x.name.clone())
.collect::<Vec<_>>(),
&["FIXED", "VECTORS"]
);
}
}
|