Browse Source

Got pong somewhat working! :)

Chris Konstad 3 years ago
parent
commit
aff4d52f9d
3 changed files with 100 additions and 14 deletions
  1. BIN
      PONG
  2. 71 11
      src/lib.rs
  3. 29 3
      src/main.rs

BIN
PONG


+ 71 - 11
src/lib.rs

@ -1,7 +1,7 @@
1 1
extern crate rand;
2 2

													
3 3
use rand::Rng;
4
use std::mem;
4
use std::{fmt, mem};
5 5
use std::num::Wrapping;
6 6
use std::vec::Vec;
7 7

													
@ -38,13 +38,13 @@ static FONTSET : [u8;80] = [
38 38
fn make_bitvector(byte :u8) -> Vec<u8> {
39 39
    let mut bits = vec![0; 8];
40 40
    for i in 0..7 {
41
        bits[7-i] = !!((byte >> i) & 0x1);
41
        bits[i] = if 0 != byte & (0x80 >> i) { 1 } else { 0 };
42 42
    }
43 43
    bits
44 44
}
45 45

													
46 46
pub struct Chip8 {
47
    drawFlag: bool,
47
    pub drawFlag: bool,
48 48
    opcode: u16,
49 49
    memory: [u8; NMEM],
50 50
    reg: [u8; NREG],
@ -58,10 +58,64 @@ pub struct Chip8 {
58 58
    key: [u8; 16],
59 59
}
60 60

													
61
impl fmt::Debug for Chip8 {
62
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63
        write!(f,
64
               "Chip8:
65
                 drawFlag: {}
66
                 opcode: {:#X}
67
                 index: {:#X}
68
                 pc: {:#X}
69
                 t_delay: {:#X}
70
                 t_sound: {:#X}
71
                 r0: {:#X}  r1: {:#X}  r2: {:#X} r3: {:#X}
72
                 r4: {:#X}  r5: {:#X}  r6: {:#X} r7: {:#X}
73
                 r8: {:#X}  r9: {:#X}  rA: {:#X} rB: {:#X}
74
                 rC: {:#X}  rD: {:#X}  rE: {:#X} rF: {:#X}
75
                 sp: {:#X}  stack: {:?}\n",
76
               self.drawFlag,
77
               self.opcode,
78
               self.index,
79
               self.pc,
80
               self.timer_delay,
81
               self.timer_sound,
82
               self.reg[0],
83
               self.reg[1],
84
               self.reg[2],
85
               self.reg[3],
86
               self.reg[4],
87
               self.reg[5],
88
               self.reg[6],
89
               self.reg[7],
90
               self.reg[8],
91
               self.reg[9],
92
               self.reg[10],
93
               self.reg[11],
94
               self.reg[12],
95
               self.reg[13],
96
               self.reg[14],
97
               self.reg[15],
98
               self.sp,
99
               self.stack,
100
               );
101
        write!(f, "  graphics:\n");
102
        let mut row = String::with_capacity(64);
103
        write!(f, "+----------------------------------------------------------------+\n");
104
        for i in 0..32 {
105
            for j in 0..64 {
106
                row.push(if 0 == self.graphics[i * 64 + j] { ' ' } else { '#' });
107
            }
108
            write!(f, "|{}|\n", row);
109
            row.clear();
110
        }
111
        write!(f, "+----------------------------------------------------------------+\n")
112
    }
113
}
114

													
61 115
impl Chip8 {
62 116
    pub fn new() -> Self {
63 117
        let mut chip = Chip8 {
64
            drawFlag: false,
118
            drawFlag: true,
65 119
            opcode: 0,
66 120
            memory: [0; NMEM],
67 121
            reg: [0; NREG],
@ -110,6 +164,7 @@ impl Chip8 {
110 164
    }
111 165

													
112 166
    pub fn executeOpcode(&mut self) {
167
        println!("Opcode: {:#X}", self.opcode);
113 168
        match self.opcode & 0xF000 {
114 169
            0x0000 => {
115 170
                match self.opcode {
@ -118,6 +173,7 @@ impl Chip8 {
118 173
                        for i in 0..NPIXELS {
119 174
                            self.graphics[i] = 0;
120 175
                        }
176
                        self.drawFlag = true;
121 177
                        self.pc += 2;
122 178
                    }
123 179
                    0x00EE => {
@ -297,8 +353,9 @@ impl Chip8 {
297 353
            }
298 354
            0xD000 => {
299 355
                // 0xDXYN: Draw sprint at regX,regY with N bytes of data, start at index
300
                let x = self.reg[((self.opcode & 0x0F00) >> 8) as usize] as usize;
301
                let y = self.reg[((self.opcode & 0x00F0) >> 4) as usize] as usize;
356
                // TODO: Fix wrapping of stuff
357
                let x = self.reg[((self.opcode & 0x0F00) >> 8) as usize] as i32;
358
                let y = self.reg[((self.opcode & 0x00F0) >> 4) as usize] as i32;
302 359
                let n = (self.opcode & 0x000F) as u16;
303 360
                let start = self.index;
304 361

													
@ -306,19 +363,22 @@ impl Chip8 {
306 363
                for i in start..(start+n) {
307 364
                    let row_num = (i - start) as u8;
308 365
                    let bits = make_bitvector(self.memory[i as usize]);
366
                    println!("BYTE: {:b}", self.memory[i as usize]);
367
                    println!("AS VEC: {:?}", bits);
309 368
                    for j in 0..8 {
310 369
                        let x_s = x + j;
311
                        let y_s = y + row_num as usize;
312
                        if 0 <= x_s &&
370
                        let y_s = y + row_num as i32;
371
                        println!("{} at {}, {}", bits[j as usize],  x_s, y_s);
372
                        if true {/*0 <= x_s &&
313 373
                           x_s < 64 &&
314 374
                           0 <= y_s &&
315
                           y_s < 32 {
375
                           y_s < 32 {*/
316 376
                               let address = (64 * y_s) + x_s;
317 377
                               if bits[j as usize] != 0 {
318
                                   if 1 == self.graphics[address] {
378
                                   if 1 == self.graphics[address as usize] {
319 379
                                       self.reg[0xF] = 1;
320 380
                                   }
321
                                   self.graphics[address] ^= 1;
381
                                   self.graphics[address as usize] ^= 1;
322 382
                               }
323 383
                           }
324 384
                    }

+ 29 - 3
src/main.rs

@ -1,5 +1,10 @@
1 1
extern crate libchip8;
2 2

													
3
use std::error::Error;
4
use std::io::prelude::*;
5
use std::fs::File;
6
use std::path::Path;
7

													
3 8
use libchip8::Chip8;
4 9

													
5 10
fn main() {
@ -8,19 +13,40 @@ fn main() {
8 13
    // TODO Setup the render system
9 14
    // setupGraphics();
10 15
    // setupInput();
16
    let path = Path::new("PONG");
17
    let display = path.display();
11 18

													
12 19
    // Initialize the emulator and load the game
13 20
    let mut chip = Chip8::new();
14
    //chip.loadGame("pong");
21
    let mut file = match File::open("PONG") {
22
        Err(why) => panic!("Couldn't open {}: {}", display, Error::description(&why)),
23
        Ok(file) => file,
24
    };
25

													
26
    let mut game = Vec::new();
27
    match file.read_to_end(&mut game) {
28
        Err(why) => panic!("Couldn't read {}: {}", display, Error::description(&why)),
29
        Ok(_) => (),
30
    };
31

													
32
    //let test = vec![0x62, 0x00, 0x61, 0x0B, 0xF1, 0x29, 0xD2, 0x05];
33
    //chip.loadHex(&test);
34
    chip.loadHex(&game);
15 35

													
16 36
    // Emulation loop
37
    let mut counter = 0;
17 38
    loop {
39
        if counter > 10 {
40
        }
18 41
        chip.emulateCycle();
19 42

													
20
        //if(chip.drawFlag) {
43
        if chip.drawFlag {
21 44
            // TODO
22 45
            // drawGraphics();
23
        //}
46
            print!("{:?}", chip);
47
            chip.drawFlag = false;
48
        }
49
        counter += 1;
24 50

													
25 51
        // Store key press state
26 52
        //chip.setKeys();