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
|
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),
}
}
pub fn is_reserved(&self) -> bool {
matches!(self.value.to_lowercase().as_str(), "x" | "y" | "a")
}
}
#[derive(Eq, Hash, PartialEq, Debug, Clone)]
pub enum AddressingMode {
Unknown,
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,
pub affected_on_page: 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,
}
}
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,
}
}
}
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<PString>,
pub right: Option<PString>,
}
#[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,
}
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 enum Node {
Generic(Generic),
Instruction(Instruction),
Scoped(Scoped),
Literal(Literal),
Fill(Fill),
}
impl Node {
pub fn is_encodeable(&self) -> bool {
matches!(
self,
&Node::Instruction(_) | &Node::Literal(_) | &Node::Fill(_)
)
}
}
|