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
|
use crate::errors::ParseError;
use std::fmt;
use std::ops::Range;
/// PString is a String with position information.
#[derive(Debug, Clone, PartialEq)]
pub struct PString {
pub value: String,
pub line: usize,
pub range: Range<usize>,
}
impl PString {
pub fn new() -> Self {
PString {
value: String::from(""),
line: 0,
range: Range { start: 0, end: 0 },
}
}
pub fn from(value: &str) -> Self {
PString {
value: String::from(value),
line: 0,
range: Range { start: 0, end: 0 },
}
}
pub fn parser_error(&self, message: &str) -> ParseError {
// TODO: we can go further :)
ParseError {
line: self.line,
message: String::from(message),
parse: true,
}
}
pub fn is_valid(&self) -> bool {
!(self.value.is_empty() || self.range.is_empty())
}
pub fn is_valid_identifier(&self) -> Result<(), String> {
if self.value.trim().is_empty() {
return Err(format!("empty identifier"));
}
// You cannot assign into a name which is reserved.
if matches!(self.value.to_lowercase().as_str(), "x" | "y" | "a") {
return Err(format!("cannot use reserved name '{}'", self.value));
}
// You cannot assign into scoped names: declare them into their
// respective scopes instead.
if self.value.contains("::") {
return Err(format!(
"the name '{}' is scoped: do not declare things this way",
self.value
));
}
// Let's gather info from the variable name which is relevant to later
// checks.
let mut alpha_seen = false;
let mut valid_hex = match self.value.len() {
1 | 2 | 3 | 4 => true,
_ => false,
};
for c in self.value.to_lowercase().chars() {
if c == '_' {
valid_hex = false;
} else {
if c.is_alphabetic() {
alpha_seen = true;
if c > 'f' && c <= 'z' {
valid_hex = false;
}
}
}
}
// We need at least one alphabetic character. Otherwise it might be
// confusing with numbers.
if !alpha_seen {
return Err(format!(
"name '{}' requires at least one alphabetic character",
self.value
));
}
// To avoid problems down the line, you cannot assign into names which
// are proper hexadecimal values.
if valid_hex {
return Err(format!(
"cannot use names which are valid hexadecimal values such as '{}'",
self.value
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Bundle {
pub bytes: [u8; 3],
pub size: u8,
pub address: usize,
pub cycles: u8,
pub affected_on_page: bool,
}
impl Bundle {
pub fn new() -> Self {
Self {
bytes: [0, 0, 0],
size: 0,
address: 0,
cycles: 0,
affected_on_page: false,
}
}
}
#[derive(Eq, Hash, PartialEq, Debug, Clone)]
pub enum AddressingMode {
Unknown, // TODO: is this really used?
Implied,
Immediate,
Absolute,
RelativeOrZeropage,
IndexedX,
IndexedY,
ZeropageIndexedX,
ZeropageIndexedY,
Indirect,
IndirectX,
IndirectY,
}
impl fmt::Display for AddressingMode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AddressingMode::Implied => write!(f, "implied"),
AddressingMode::Immediate => write!(f, "immediate"),
AddressingMode::Absolute => write!(f, "absolute"),
AddressingMode::RelativeOrZeropage => write!(f, "relative or zeropage"),
AddressingMode::IndexedX => write!(f, "indexed by x"),
AddressingMode::IndexedY => write!(f, "indexed by y"),
AddressingMode::ZeropageIndexedX => write!(f, "zeropage indexed by x"),
AddressingMode::ZeropageIndexedY => write!(f, "zeropage indexed by y"),
AddressingMode::Indirect => write!(f, "indirect"),
AddressingMode::IndirectX => write!(f, "indirect indexed by x"),
AddressingMode::IndirectY => write!(f, "indirect indexed by y"),
_ => write!(f, "unknown"),
}
}
}
/// Encodable is a trait to be implemented by those structs that might need to
/// be encoded into the outside world. That is, structures that make sense to
/// output into files or other output streams.
pub trait Encodable {
/// Returns a fixed array of bytes which belong to an encodable object. Note
/// that the capacity is fixed, but the actual size must be checked with the
/// `size` trait function, otherwise elements beyond that size might contain
/// junk.
fn to_bytes(&self) -> [u8; 3];
/// Returns the actual size of the data returned by `to_bytes`.
fn size(&self) -> u8;
/// Returns a vector which contains the exact byte data for the given
/// object. In contrast with `to_bytes`, the caller does not need to check
/// for `size`: the returned vector is tailored to the exact amount of
/// bytes for the object.
fn to_hex(&self) -> Vec<String>;
/// Returns a string representation which makes sense to a human (e.g.
/// instead of providing the byte encoded opcode for an instruction, show
/// the mnemonic).
fn to_human(&self) -> String;
/// Returns a string representation with higher verbosity than `to_human`.
fn to_verbose(&self) -> String;
}
#[derive(Debug, Clone, PartialEq)]
pub struct Instruction {
pub mnemonic: PString,
pub opcode: u8,
pub bytes: [u8; 2],
pub size: u8,
pub left: Option<PString>,
pub right: Option<PString>,
pub mode: AddressingMode,
pub cycles: u8, // NOTE: relative addressing makes this runtime-dependant (if branch is taken, then +1 cycle to the base cycle here).
pub affected_on_page: bool, // TODO: needed?
pub address: u16,
pub resolved: bool,
}
impl Instruction {
pub fn unknown() -> Instruction {
Instruction {
mnemonic: PString::new(),
opcode: 0,
bytes: [0, 0],
size: 0,
left: None,
right: None,
mode: AddressingMode::Unknown,
cycles: 0,
affected_on_page: false,
address: 0,
resolved: true,
}
}
pub fn from(mnemonic: &str) -> Instruction {
Instruction {
mnemonic: PString::from(mnemonic),
opcode: 0,
bytes: [0, 0],
size: 0,
left: None,
right: None,
mode: AddressingMode::Unknown,
cycles: 0,
affected_on_page: false,
address: 0,
resolved: true,
}
}
}
impl Encodable for Instruction {
fn size(&self) -> u8 {
self.size
}
fn to_hex(&self) -> Vec<String> {
let mut ret = vec![];
ret.push(format!("{:02X}", self.opcode));
if self.size > 1 {
ret.push(format!("{:02X}", self.bytes[0]));
}
if self.size == 3 {
ret.push(format!("{:02X}", self.bytes[1]));
}
ret
}
fn to_bytes(&self) -> [u8; 3] {
[self.opcode.to_le_bytes()[0], self.bytes[0], self.bytes[1]]
}
fn to_human(&self) -> String {
match self.mode {
AddressingMode::Implied => self.mnemonic.value.clone(),
AddressingMode::Immediate => format!("{} #${:02X}", self.mnemonic.value, self.bytes[0]),
AddressingMode::Absolute => format!(
"{} ${:02X}{:02X}",
self.mnemonic.value, self.bytes[1], self.bytes[0]
),
AddressingMode::RelativeOrZeropage => {
format!("{} ${:02X}", self.mnemonic.value, self.bytes[0])
}
AddressingMode::IndexedX => format!(
"{} ${:02X}{:02X}, x",
self.mnemonic.value, self.bytes[1], self.bytes[0]
),
AddressingMode::IndexedY => format!(
"{} ${:02X}{:02X}, y",
self.mnemonic.value, self.bytes[1], self.bytes[0]
),
AddressingMode::ZeropageIndexedX => {
format!("{} ${:02X}, x", self.mnemonic.value, self.bytes[0])
}
AddressingMode::ZeropageIndexedY => {
format!("{} ${:02X}, y", self.mnemonic.value, self.bytes[0])
}
AddressingMode::Indirect => format!(
"{} (${:02X}{:02X})",
self.mnemonic.value, self.bytes[1], self.bytes[0]
),
AddressingMode::IndirectX => {
format!("{} (${:02X}, x)", self.mnemonic.value, self.bytes[0])
}
AddressingMode::IndirectY => {
format!("{} (${:02X}), y", self.mnemonic.value, self.bytes[0])
}
AddressingMode::Unknown => String::from("unknown instruction"),
}
}
fn to_verbose(&self) -> String {
format!("{:#?}", self)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Generic {
pub identifier: PString,
pub left: Option<Box<Node>>,
pub right: Option<Box<Node>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Scoped {
pub identifier: PString,
pub start: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Literal {
pub identifier: PString,
pub bytes: [u8; 2],
pub size: u8,
pub resolved: bool,
}
impl Encodable for Literal {
fn size(&self) -> u8 {
self.size
}
fn to_hex(&self) -> Vec<String> {
let mut ret = vec![];
ret.push(format!("{:02X}", self.bytes[0]));
if self.size == 2 {
ret.push(format!("{:02X}", self.bytes[1]));
} else if self.size != 1 {
panic!("size for literal should be either 1 or 2");
}
ret
}
fn to_bytes(&self) -> [u8; 3] {
[self.bytes[0], self.bytes[1], 0]
}
fn to_human(&self) -> String {
match self.size {
1 => format!(".byte ${:02X}", self.bytes[0]),
2 => format!(".byte ${:02X}{:02X}", self.bytes[1], self.bytes[0]),
_ => String::from("unknown literal"),
}
}
fn to_verbose(&self) -> String {
format!("{:#?}", self)
}
}
#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)]
pub struct Fill {
pub value: u8,
}
impl Encodable for Fill {
fn size(&self) -> u8 {
1
}
fn to_hex(&self) -> Vec<String> {
let mut ret = vec![];
ret.push(format!("{:02X}", self.value));
ret
}
fn to_bytes(&self) -> [u8; 3] {
[self.value, 0, 0]
}
fn to_human(&self) -> String {
format!("${:02X}", self.value)
}
fn to_verbose(&self) -> String {
format!("{:#?}", self)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Label {
pub value: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Node {
Generic(Generic),
Instruction(Instruction),
Scoped(Scoped),
Literal(Literal),
Fill(Fill),
Label(Label),
}
impl Node {
pub fn is_encodeable(&self) -> bool {
matches!(
self,
&Node::Instruction(_) | &Node::Literal(_) | &Node::Fill(_)
)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn is_err(line: &str, message: &str) {
let pstring = PString {
value: line.to_string(),
line: 0,
range: Range::default(),
};
let ret = pstring.is_valid_identifier();
assert!(ret.is_err());
if let Err(e) = ret {
assert_eq!(e, message);
}
}
#[test]
fn bad_variable_names() {
is_err("a", "cannot use reserved name 'a'");
is_err("X", "cannot use reserved name 'X'");
is_err(
"AA",
"cannot use names which are valid hexadecimal values such as 'AA'",
);
is_err("11", "name '11' requires at least one alphabetic character");
is_err("__", "name '__' requires at least one alphabetic character");
is_err(
"Scope::Variable",
"the name 'Scope::Variable' is scoped: do not declare things this way",
);
}
}
|