(&mut self)
| 79 | } |
| 80 | |
| 81 | pub fn run_loop(&mut self) -> Result<(), &str> { |
| 82 | let mut executing = true; |
| 83 | let mut waiting_for_keypress = false; |
| 84 | let mut store_keypress_in: usize = 0x0; |
| 85 | let mut time_to_runloop: usize = RUNLOOP_TIMER_DEFAULT; |
| 86 | |
| 87 | while self.win.is_open() && !self.win.is_key_down(Key::Escape) && self.pc <= RAM_SIZE { |
| 88 | let keys_pressed = self.win.handle_key_events(); |
| 89 | |
| 90 | for (j, k) in keys_pressed.iter().enumerate() { |
| 91 | if *k { |
| 92 | if waiting_for_keypress { |
| 93 | executing = true; |
| 94 | waiting_for_keypress = false; |
| 95 | self.v[store_keypress_in] = j as u8; |
| 96 | break; |
| 97 | } |
| 98 | println!("{:01x} pressed!", j); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | let b1 = self.ram[self.pc] as u16; |
| 103 | let b2 = self.ram[self.pc + 1] as u16; |
| 104 | let instruction = (b1 * 256) + b2; |
| 105 | |
| 106 | let mut next_instruction = true; |
| 107 | |
| 108 | if executing { |
| 109 | println!( |
| 110 | "{:03x}, {:04x}, {:04x}, {:02x?}", |
| 111 | self.pc, instruction, self.i, self.v |
| 112 | ); |
| 113 | match instruction { |
| 114 | 0x00e0 => { |
| 115 | self.win.clear_screen(); |
| 116 | } |
| 117 | 0x00ee => { |
| 118 | if self.sp == 0 { |
| 119 | return Err("Stack empty, cannot return from subroutine!"); |
| 120 | } |
| 121 | self.sp -= 1; |
| 122 | self.pc = self.stack[self.sp]; |
| 123 | } |
| 124 | 0x1000..=0x1fff => { |
| 125 | self.pc = get_hex_digits(&instruction, 3, 0); |
| 126 | next_instruction = false; |
| 127 | } |
| 128 | 0x2000..=0x2fff => { |
| 129 | let loc = get_hex_digits(&instruction, 3, 0); |
| 130 | if self.sp == STACK_SIZE { |
| 131 | return Err("Stack full, cannot push!"); |
| 132 | } |
| 133 | self.stack[self.sp] = self.pc; |
| 134 | self.sp += 1; |
| 135 | self.pc = loc; |
| 136 | next_instruction = false; |
| 137 | } |
| 138 | 0x3000..=0x3fff => { |
no test coverage detected