emul8 0.1.2

A simple rust-based toolchain to interoperate and emulate the CHIP-8 architecture
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
extern crate clap;
extern crate emul8;
extern crate pest;
#[macro_use]
extern crate pest_derive;

use clap::{App, Arg};
use emul8::internals::opcode::Opcode;
use emul8::internals::opcode::*;
use emul8::internals::processor::*;
use pest::Parser;
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};

#[derive(Parser)]
#[grammar = "grammar/asm.pest"]
pub struct ASMParser;

pub struct LabelDefinition {
    pub name: String,
    pub addr: u16,
}

#[derive(Debug)]
pub struct JmpLabel {
    pub name: String,
}

#[derive(Debug)]
pub struct CallLabel {
    pub name: String,
}

impl Opcode for JmpLabel {
    fn execute(&self, _processor: &mut Processor) {
        panic!("This Opcode is not meant to be executed and should be replaced by the assembler!");
    }
    fn modified_pc(&self) -> bool {
        false // The majority does not tamper with the PC
    }
    fn assemble(&self) -> (u8, u8) {
        panic!("This Opcode is not meant to be assembled and should be replaced by the assembler!");
    }
}

impl std::fmt::Display for JmpLabel {
    fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        unreachable!()
    }
}

impl Opcode for CallLabel {
    fn execute(&self, _processor: &mut Processor) {
        panic!("This Opcode is not meant to be executed and should be replaced by the assembler!");
    }
    fn assemble(&self) -> (u8, u8) {
        panic!("This Opcode is not meant to be assembled and should be replaced by the assembler!");
    }
}

impl std::fmt::Display for CallLabel {
    fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        unreachable!()
    }
}

// @TODO: Replace min() with assertions
fn parse_register(pair: pest::iterators::Pair<Rule>) -> u8 {
    std::cmp::min(
        0xF,
        u8::from_str_radix(pair.as_span().as_str().trim_start_matches('V'), 16).unwrap(),
    )
}

fn parse_constant(pair: pest::iterators::Pair<Rule>) -> u8 {
    let addr_s = pair.as_span().as_str();

    let addr = if addr_s.starts_with("0x") {
        u16::from_str_radix(addr_s.trim_start_matches("0x"), 16).unwrap()
    } else {
        u16::from_str_radix(addr_s, 10).unwrap()
    };

    if addr > 0xFF {
        panic!("Syntax Error: Constant {} too large!", addr_s);
    }

    addr as u8
}

fn parse_address(pair: pest::iterators::Pair<Rule>) -> u16 {
    let addr_s = pair.as_span().as_str();

    let addr = if addr_s.starts_with("0x") {
        u16::from_str_radix(addr_s.trim_start_matches("0x"), 16).unwrap()
    } else {
        u16::from_str_radix(addr_s, 10).unwrap()
    };

    if addr > 0x1000 {
        panic!("Syntax Error: Address out of boundaries: {}", addr_s);
    }

    addr
}

fn main() {
    let args = App::new("CHIP-8 Assembler")
        .version("0.1")
        .author("Marc Streckfuß <marc.streckfuss@gmail.com>")
        .about("Assembler for the CHIP-8 Binary Format")
        .arg(Arg::with_name("infile")
            .index(1)
            .value_name("FILE")
            .help("Determines the file to assemble")
            .default_value("delay.as8"))
        .arg(Arg::with_name("offset")
            .long("offset")
            .help("Where the entry point is (the offset from the file start where to put the data)")
            .default_value("0x200"))
        .arg(Arg::with_name("outfile")
            .long("outfile")
            .short("o")
            .help("How the resulting binary should be called (if empty: guess from input filename)")
            .value_name("FILE"))
        .arg(Arg::with_name("verbosity")
            .short("v")
            .multiple(true)
            .help("Sets the level of verbosity"))
        .arg(Arg::with_name("overwrite")
            .long("overwrite")
            .help("If $filename is already taken, overwrite the file. Without this flag the attempt will fail"))
        .get_matches();

    let verbosity = std::cmp::min(args.occurrences_of("verbosity"), 2);

    let offset = u16::from_str_radix(
        args.value_of("offset").unwrap().trim_start_matches("0x"),
        16,
    )
    .expect("Unable to parse the offset value");

    let outfilename = if args.is_present("outfile") {
        args.value_of("outfile").unwrap().to_string()
    } else {
        format!(
            "{}{}",
            args.value_of("infile").unwrap().trim_end_matches(".as8"),
            ".obj"
        )
    };

    if verbosity > 0 {
        println!(
            "Assembling {} as {}, starting at offset {:#X}",
            args.value_of("infile").unwrap(),
            &outfilename,
            offset
        );
    }

    /* Messy code, is there a more simple solution? */
    let outfile = match File::open(&outfilename) {
        Err(_e) => File::create(&outfilename).unwrap(),
        Ok(_f) => {
            if args.is_present("overwrite") {
                OpenOptions::new().write(true).open(&outfilename).unwrap()
            } else {
                panic!("Won't overwrite the output file!")
            }
        }
    };

    // into_inner to not have file as Rule but all the expressions
    let contents =
        std::fs::read_to_string(args.value_of("infile").unwrap()).expect("Cannot read input file");
    let parse_file = ASMParser::parse(Rule::file, &contents)
        .unwrap_or_else(|e| panic!("{}", e))
        .next()
        .unwrap()
        .into_inner();
    //dbg!(parseFile);

    let mut opcodes = Vec::new();
    let mut label_definitions = std::collections::HashMap::new();

    for pair in parse_file {
        //Rule::COMMENT => println!("Comment: {}", pair.as_span().as_str()),
        if pair.as_rule() == Rule::opcode {
            let opcode: Option<Box<dyn Opcode>> = match pair.as_span().as_str() {
                "CLS" => Some(Box::new(CLS {})),
                "RET" => Some(Box::new(RET {})),
                _ => {
                    let opcode_str = pair.as_span().as_str();
                    let opcode_node = &mut pair.into_inner();
                    let operator = opcode_node.next().unwrap();
                    match operator.as_rule() {
                        Rule::ld_operator => {
                            let operand1 = opcode_node.next().unwrap();
                            let operand2 = opcode_node.next().unwrap();
                            Some(match operand1.as_rule() {
                                Rule::register => match operand2.as_rule() {
                                    Rule::special_register => match operand2.as_span().as_str() {
                                        "K" => Box::new(LDVxK {
                                            reg: parse_register(operand1),
                                        }),
                                        "DT" => Box::new(LDVxDT {
                                            reg: parse_register(operand1),
                                        }),
                                        "I" => Box::new(LDVxI {
                                            reg: parse_register(operand1),
                                        }),
                                        _ => unreachable!("Invalid special register"),
                                    },
                                    Rule::address => Box::new(LDVxByte {
                                        reg: parse_register(operand1),
                                        byte: parse_constant(operand2),
                                    }),
                                    _ => unreachable!(),
                                },
                                Rule::special_register => {
                                    let register = parse_register(operand2);
                                    match operand1.as_span().as_str() {
                                        "B" => Box::new(LDBVx { reg: register }),
                                        "F" => Box::new(LDFVx { reg: register }),
                                        "I" => Box::new(LDIVx { reg: register }),
                                        "DT" => Box::new(LDDTVx { reg: register }),
                                        _ => unreachable!("Invalid special register"),
                                    }
                                }
                                _ => unreachable!(),
                            })
                        }
                        Rule::call_operator => {
                            let operand = opcode_node.next().unwrap();
                            Some(match operand.as_rule() {
                                Rule::address => Box::new(CALL {
                                    address: parse_address(operand),
                                }),
                                Rule::identifier => Box::new(CallLabel {
                                    name: operand.as_span().as_str().to_string(),
                                }),
                                _ => unreachable!("Unknown CALL Operand {:?}", operand.as_rule()),
                            })
                        }
                        Rule::jmp_operator => {
                            let operand = opcode_node.next().unwrap();
                            Some(match operand.as_rule() {
                                Rule::address => Box::new(JMP {
                                    address: parse_address(operand),
                                }),
                                Rule::identifier => Box::new(JmpLabel {
                                    name: operand.as_span().as_str().to_string(),
                                }),
                                _ => unreachable!("Unknown JMP Operand {:?}", operand.as_rule()),
                            })
                        }
                        Rule::conditionals => {
                            let register = parse_register(opcode_node.next().unwrap());
                            let op2 = opcode_node.next().unwrap();

                            Some(match operator.as_span().as_str() {
                                "SE" => match op2.as_rule() {
                                    Rule::register => Box::new(SEVxVy {
                                        reg_a: register,
                                        reg_b: parse_register(op2),
                                    }),
                                    Rule::address => Box::new(SEVxByte {
                                        reg: register,
                                        byte: parse_constant(op2),
                                    }),
                                    _ => unreachable!(),
                                },
                                "SNE" => match op2.as_rule() {
                                    Rule::register => Box::new(SNEVxVy {
                                        reg_a: register,
                                        reg_b: parse_register(op2),
                                    }),
                                    Rule::address => Box::new(SNEVxByte {
                                        reg: register,
                                        byte: parse_constant(op2),
                                    }),
                                    _ => unreachable!(),
                                },
                                _ => unreachable!(),
                            })
                        }
                        Rule::math_operator => {
                            let op1 = opcode_node.next().unwrap();
                            let op2 = opcode_node.next().unwrap();

                            Some(match operator.as_span().as_str() {
                                "ADD" => match op1.as_rule() {
                                    Rule::special_register => {
                                        assert_eq!(op1.as_span().as_str(), "I");
                                        Box::new(ADDIVx {
                                            reg: parse_register(op2),
                                        })
                                    }
                                    Rule::register => match op2.as_rule() {
                                        Rule::register => Box::new(ADDVxVy {
                                            reg_a: parse_register(op1),
                                            reg_b: parse_register(op2),
                                        }),
                                        Rule::address => Box::new(ADDVxByte {
                                            reg: parse_register(op1),
                                            byte: parse_constant(op2),
                                        }),
                                        _ => unreachable!(),
                                    },
                                    _ => unreachable!(),
                                },
                                "SUB" => Box::new(SUBVxVy {
                                    reg_a: parse_register(op1),
                                    reg_b: parse_register(op2),
                                }),
                                "SUBN" => unimplemented!(),
                                _ => unreachable!(),
                            })
                        }
                        Rule::ldi_operator => {
                            let op = opcode_node.next().unwrap();

                            Some(match op.as_rule() {
                                Rule::register => Box::new(LDIVx {
                                    reg: parse_register(op),
                                }),
                                Rule::address => Box::new(LDIAddr {
                                    address: parse_address(op),
                                }),
                                _ => unreachable!(),
                            })
                        }
                        Rule::drw_operator => Some(Box::new(DRW {
                            reg_x: parse_register(opcode_node.next().unwrap()),
                            reg_y: parse_register(opcode_node.next().unwrap()),
                            size: std::cmp::min(0xF, parse_constant(opcode_node.next().unwrap())),
                        })),
                        // Byte arithmetic is only allowed here because we know the string is no Unicode.
                        Rule::label_definition => {
                            let s = &opcode_str[..opcode_str.len() - 1];
                            label_definitions.insert(
                                s.to_string(),
                                Box::new(LabelDefinition {
                                    name: s.to_string(),
                                    addr: (opcodes.len() * 2) as u16,
                                }),
                            );
                            None
                        }
                        _ => panic!("Unknown OPCODE {}", opcode_str),
                    }
                }
            };

            // If opcode is Some, push it to opcodes
            if let Some(op) = opcode {
                opcodes.push(op)
            };
        }
    }

    if verbosity > 1 {
        /* In practice, if we were to be invoked from a C-Compiler, we would keep our intermediary results in opcodes as an object file.
            Ideally we would already pre-assemble all opcodes which are possible into a file and keep the label definitions and the label dependants
            in place. While we could already solve in-file labels, we don't do it so that object files are "relocateable", because otherwise we'd have
            to shift them with a static offset anyway. We probably won't do that to not have a seperate object file format and instead just
            paste all files together into a big asm file.
        */
        println!("Entering Linking Stage...")
    }

    for (_idx, op) in opcodes.iter_mut().enumerate() {
        if op.is::<JmpLabel>() {
            let lbl = op.downcast_ref::<JmpLabel>().unwrap();
            if label_definitions.contains_key(&lbl.name) {
                let jmp: Box<dyn Opcode> = Box::new(JMP {
                    address: label_definitions.get(&lbl.name).unwrap().addr + offset,
                });
                *op = jmp; // like std::mem::replace(op, jmp), but doesn't care about the old value
            } else {
                panic!("ERROR LNK001: Unresolved Label {}", &lbl.name);
            }
        } else if op.is::<CallLabel>() {
            let lbl = op.downcast_ref::<CallLabel>().unwrap();
            if label_definitions.contains_key(&lbl.name) {
                let call: Box<dyn Opcode> = Box::new(CALL {
                    address: label_definitions.get(&lbl.name).unwrap().addr + offset,
                });
                *op = call;
            } else {
                panic!("ERROR LNK001: Unresolved Label {}", &lbl.name);
            }
        }
    }

    let mut buf = BufWriter::new(outfile);
    opcodes.iter().map(|x| x.assemble()).for_each(move |x| {
        buf.write_all(&[x.0])
            .expect("Error when writing to the object file!");
        buf.write_all(&[x.1])
            .expect("Error when writing to the object file!");
    });
}