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
use std::str::FromStr;
use pest::{iterators::Pair, Parser};
use pest_derive::Parser;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("syntax error")]
ParseError(#[from] pest::error::Error<Rule>),
#[error("we didn't successfully parse a top level syntax object")]
ParseFailure,
#[error("numeric format error")]
NumericFormatError(#[from] std::num::ParseIntError),
}
pub enum ContentType {
Hex,
Binary,
}
#[derive(Parser)]
#[grammar = "grammar.pest"]
struct ReadmemParser;
#[derive(Debug)]
enum Item<I> {
Address(usize),
Number(I),
}
#[doc(hidden)]
pub trait Integral: Sized {
fn from_str_radix(src: &str, radix: u32) -> Result<Self, std::num::ParseIntError>;
fn zero() -> Self;
}
macro_rules! integrate {
($t:ty) => {
impl Integral for $t {
fn from_str_radix(src: &str, radix: u32) -> Result<Self, std::num::ParseIntError> {
<$t>::from_str_radix(src, radix)
}
fn zero() -> Self {
0
}
}
};
}
integrate!(u8);
integrate!(u16);
integrate!(u32);
integrate!(u64);
integrate!(u128);
fn parse_value<I>(pair: Pair<Rule>) -> Result<Item<I>, Error>
where
I: Integral,
{
let is_zx = |c| c == 'x' || c == 'z' || c == 'X' || c == 'Z';
Ok(match pair.as_rule() {
Rule::addr => Item::Address(usize::from_str_radix(&pair.as_str()[1..], 16)?),
Rule::hexnum => {
let without_underscore = pair.as_str().replace("_", "");
let without_zx = without_underscore.replace(is_zx, "0");
Item::Number(Integral::from_str_radix(&without_zx, 16)?)
}
Rule::binnum => {
let without_underscore = pair.as_str().replace("_", "");
let without_zx = without_underscore.replace(is_zx, "0");
Item::Number(Integral::from_str_radix(&without_zx, 2)?)
}
r => unreachable!(
"should not hit this rule {:?}, all our other rules are silent",
r
),
})
}
pub fn readmem<I>(content: &str, content_type: ContentType) -> Result<Vec<I>, Error>
where
I: Integral + FromStr + Clone,
{
let rule = match content_type {
ContentType::Hex => Rule::readmemh,
ContentType::Binary => Rule::readmemb,
};
let content = ReadmemParser::parse(rule, content)?;
let mut result = Vec::new();
let mut pos = 0;
for val in content {
if let Rule::EOI = val.as_rule() {
continue;
}
let val = parse_value::<I>(val)?;
match val {
Item::Address(a) => pos = a,
Item::Number(n) => {
if pos + 1 >= result.len() {
result.resize(pos + 1, I::zero());
}
result[pos] = n;
pos += 1;
}
}
}
Ok(result)
}
#[cfg(doctest)]
mod doctest {
use doc_comment::doctest;
doctest!("../README.md");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hex() {
let hex = include_str!("testdata/hex.txt");
let arr = readmem::<u8>(hex, ContentType::Hex).unwrap();
let expected = 0..255u8;
assert_eq!(256, arr.len());
for (&item, exp) in arr.iter().zip(expected) {
assert_eq!(item, exp);
}
}
#[test]
fn test_bin() {
let bin = include_str!("testdata/bin.txt");
let arr = readmem::<u16>(bin, ContentType::Binary).unwrap();
assert_eq!(257, arr.len());
let expected = 0..256u16;
for (&item, exp) in arr.iter().zip(expected) {
assert_eq!(item, exp);
}
}
#[test]
fn test_addressing() {
let testdata = r#"
f
@4
1 2 3
@4
4 5 6
@8
e
"#;
let expected = vec![0xf, 0, 0, 0, 4, 5, 6, 0, 0xe];
assert_eq!(
expected,
readmem::<u32>(testdata, ContentType::Hex).unwrap()
);
}
}