You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
15 KiB

  1. use rand::Rng;
  2. use std::fs;
  3. use std::io::Read;
  4. const w: usize = 64;
  5. const h: usize = 32;
  6. pub struct Chip8 {
  7. opcode: u16,
  8. memory: [u8; 4096],
  9. v: [u8; 16],
  10. index: u16,
  11. pc: u16,
  12. pub gfx: [u8; w * h],
  13. delay_timer: u8,
  14. sound_timer: u8,
  15. stack: [u16; 16],
  16. sp: isize,
  17. key: [u8; 16],
  18. pub draw_flag: bool,
  19. }
  20. const font_set: [u8; 80] = [
  21. 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
  22. 0x20, 0x60, 0x20, 0x20, 0x70, // 1
  23. 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
  24. 0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
  25. 0x90, 0x90, 0xF0, 0x10, 0x10, // 4
  26. 0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
  27. 0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
  28. 0xF0, 0x10, 0x20, 0x40, 0x40, // 7
  29. 0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
  30. 0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
  31. 0xF0, 0x90, 0xF0, 0x90, 0x90, // A
  32. 0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
  33. 0xF0, 0x80, 0x80, 0x80, 0xF0, // C
  34. 0xE0, 0x90, 0x90, 0x90, 0xE0, // D
  35. 0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
  36. 0xF0, 0x80, 0xF0, 0x80, 0x80, // F
  37. ];
  38. impl Chip8 {
  39. pub fn new() -> Chip8 {
  40. let mut c = Chip8 {
  41. opcode: 0,
  42. memory: [0; 4096],
  43. v: [0; 16],
  44. index: 0,
  45. pc: 0x200,
  46. gfx: [0; w * h],
  47. delay_timer: 0,
  48. sound_timer: 0,
  49. stack: [0; 16],
  50. sp: 0,
  51. key: [0; 16],
  52. draw_flag: false,
  53. };
  54. for i in 0..font_set.len() {
  55. c.memory[i] = font_set[i];
  56. }
  57. c
  58. }
  59. pub fn load_game(&mut self, filepath: &str) {
  60. let mut f = fs::File::open(filepath).expect("can not load rom file");
  61. let metadata = fs::metadata(filepath).expect("unable to read metadata");
  62. let mut b = vec![0; metadata.len() as usize];
  63. f.read(&mut b).expect("buffer overflow");
  64. for i in 0..b.len() {
  65. self.memory[512 + i] = b[i];
  66. }
  67. }
  68. pub fn emulate_cycle(&mut self) {
  69. self.opcode = (self.memory[self.pc as usize] as u16) << 8
  70. | self.memory[(self.pc + 1) as usize] as u16;
  71. let x: usize = ((self.opcode & 0x0F00) >> 8) as usize;
  72. let y: usize = ((self.opcode & 0x00F0) >> 4) as usize;
  73. let nn: u8 = (self.opcode & 0x00FF) as u8;
  74. let nnn: u16 = (self.opcode & 0x0FFF) as u16;
  75. // Decode Opcode
  76. // https://en.wikipedia.org/wiki/CHIP-8#Opcode_table
  77. match self.opcode & 0xF000 {
  78. 0x0000 => {
  79. match self.opcode & 0x000F {
  80. 0x0000 => {
  81. // 00E0 Clear screen
  82. for i in 0..self.gfx.len() {
  83. self.gfx[i] = 0;
  84. }
  85. self.pc += 2;
  86. self.draw_flag = true;
  87. }
  88. 0x000E => {
  89. // 00EE Returns from a subroutine
  90. self.sp -= 1;
  91. self.pc = self.stack[self.sp as usize];
  92. self.pc += 2;
  93. }
  94. _ => println!("unk {:x}", self.opcode),
  95. }
  96. }
  97. 0x1000 => {
  98. // 1NNN Jumps to address NNN
  99. self.pc = nnn;
  100. }
  101. 0x2000 => {
  102. // 2NNN Calls subroutine at NNN
  103. self.stack[self.sp as usize] = self.pc;
  104. self.sp += 1;
  105. self.pc = nnn;
  106. }
  107. 0x3000 => {
  108. // 3XNN Skips the next instruction if VX equals NN. (Usually
  109. // the next instruction is a jump to skip a code block)
  110. if self.v[x] == nn {
  111. self.pc += 2;
  112. }
  113. self.pc += 2;
  114. }
  115. 0x4000 => {
  116. // 4XNN Skips the next instruction if VX doesn't equal NN.
  117. // (Usually the next instruction is a jump to skip a code
  118. // block)
  119. if self.v[x] != nn {
  120. self.pc += 2;
  121. }
  122. self.pc += 2;
  123. }
  124. 0x5000 => {
  125. // 5XY0 Skips the next instruction if VX equals VY. (Usually
  126. // the next instruction is a jump to skip a code block)
  127. if self.v[x] == self.v[y] {
  128. self.pc += 2;
  129. }
  130. self.pc += 2;
  131. }
  132. 0x6000 => {
  133. // 6XNN Sets VX to NN
  134. self.v[x] = nn;
  135. self.pc += 2;
  136. }
  137. 0x7000 => {
  138. // 7XNN Adds NN to VX. (Carry flag is not changed)
  139. self.v[x] += nn;
  140. self.pc += 2;
  141. }
  142. 0x8000 => {
  143. match self.opcode & 0x000F {
  144. 0x0000 => {
  145. // 0x8XY0 Sets VX to the value of VY
  146. self.v[x] = self.v[y];
  147. self.pc += 2;
  148. }
  149. 0x0001 => {
  150. // 0x8XY1 Sets VX to VX or VY. (Bitwise OR operation)
  151. self.v[x] = (self.v[x] | self.v[y]);
  152. self.pc += 2;
  153. }
  154. 0x0002 => {
  155. // 0x8XY2 Sets VX to VX and VY. (Bitwise AND operation)
  156. self.v[x] = (self.v[x] & self.v[y]);
  157. self.pc += 2;
  158. }
  159. 0x0003 => {
  160. // 0x8XY3 Sets VX to VX xor VY
  161. self.v[x] = (self.v[x] ^ self.v[y]);
  162. self.pc += 2;
  163. }
  164. 0x0004 => {
  165. // 0x8XY4 Adds VY to VX. VF is set to 1 when there's a
  166. // carry, and to 0 when there isn't
  167. if self.v[y] > (0xFF - self.v[x]) {
  168. self.v[0xF] = 1;
  169. } else {
  170. self.v[0xF] = 0;
  171. }
  172. self.v[x] += self.v[y];
  173. self.pc += 2;
  174. }
  175. 0x0005 => {
  176. // 0x8XY5 VY is subtracted from VX. VF is set to 0 when
  177. // there's a borrow, and 1 when there isn't
  178. if self.v[x] > self.v[y] {
  179. self.v[0xF] = 1;
  180. } else {
  181. self.v[0xF] = 0;
  182. }
  183. self.v[x] -= self.v[y];
  184. self.pc += 2;
  185. }
  186. 0x0006 => {
  187. // 0x8XY6 Stores the least significant bit of VX in VF
  188. // and then shifts VX to the right by 1
  189. if self.opcode & 0x1 >= 1 {
  190. self.v[0xF] = 1;
  191. } else {
  192. self.v[0xF] = 0;
  193. }
  194. self.v[x] = self.v[x] >> 1;
  195. self.pc += 2;
  196. }
  197. 0x0007 => {
  198. // 0x8XY7 Sets VX to VY minus VX. VF is set to 0 when
  199. // there's a borrow, and 1 when there isn't
  200. if self.v[y] > self.v[x] {
  201. self.v[0xF] = 1;
  202. } else {
  203. self.v[0xF] = 0;
  204. }
  205. self.v[x] = self.v[y] - self.v[x];
  206. self.pc += 2;
  207. }
  208. 0x000E => {
  209. // 0x8XYE Stores the most significant bit of VX in VF
  210. // and then shifts VX to the left by 1
  211. if self.opcode & 0x80 == 0x80 {
  212. self.v[0xF] = 1;
  213. } else {
  214. self.v[0xF] = 0;
  215. }
  216. self.v[x] = self.v[x] << 1;
  217. self.pc += 2;
  218. }
  219. _ => println!("unk {:x}", self.opcode),
  220. }
  221. }
  222. 0x9000 => {
  223. // 9XY0 Skips the next instruction if VX doesn't equal VY.
  224. // (Usually the next instruction is a jump to skip a code
  225. // block)
  226. if self.v[x] != self.v[y] {
  227. self.pc += 2;
  228. }
  229. self.pc += 2;
  230. }
  231. 0xA000 => {
  232. // ANNN set index to NNN position
  233. self.index = nnn;
  234. self.pc += 2;
  235. }
  236. 0xB000 => {
  237. // BNNN Jumps to the address NNN plus V0
  238. self.pc = nnn + self.v[0] as u16;
  239. self.pc += 2;
  240. }
  241. 0xC000 => {
  242. // CXNN Sets VX to the result of a bitwise and operation on a
  243. // random number (Typically: 0 to 255) and NN
  244. let mut rng = rand::thread_rng();
  245. let r: u8 = rng.gen_range(0, 255);
  246. self.v[x] = r & nn;
  247. self.pc += 2;
  248. }
  249. 0xD000 => {
  250. // DXYN Draws a sprite at coordinate (VX, VY) that has a width
  251. // of 8 pixels and a height of N+1 pixels. Each row of 8 pixels
  252. // is read as bit-coded starting from memory location I; I
  253. // value doesn’t change after the execution of this
  254. // instruction. As described above, VF is set to 1 if any
  255. // screen pixels are flipped from set to unset when the sprite
  256. // is drawn, and to 0 if that doesn’t happen
  257. let heigh = self.opcode & 0x000F;
  258. let mut pixel: u8;
  259. self.v[0xF] = 0;
  260. for yline in 0..heigh {
  261. pixel = self.memory[(self.index + yline) as usize];
  262. for xline in 0..8 {
  263. if (pixel & (0x80 >> xline)) != 0 {
  264. let pos = (self.v[x] as u16 + xline) as usize
  265. + (self.v[y] as u16 + yline) as usize * w;
  266. if pos >= 2048 {
  267. break;
  268. }
  269. if self.gfx[pos] == 1 {
  270. self.v[0xF] = 1;
  271. } else {
  272. self.v[0xF] ^= 1;
  273. }
  274. }
  275. }
  276. }
  277. self.draw_flag = true;
  278. self.pc += 2;
  279. }
  280. 0xE000 => {
  281. match self.opcode & 0x00FF {
  282. 0x009E => {
  283. // EX9E Skips the next instruction if the key stored in
  284. // VX is pressed. (Usually the next instruction is a
  285. // jump to skip a code block)
  286. if self.key[self.v[x] as usize] != 0 {
  287. self.pc += 2;
  288. }
  289. self.pc += 2;
  290. }
  291. 0x00A1 => {
  292. // EXA1 Skips the next instruction if the key stored in
  293. // VX isn't pressed. (Usually the next instruction is a
  294. // jump to skip a code block)
  295. if self.key[self.v[x] as usize] != 1 {
  296. self.pc += 2;
  297. }
  298. self.pc += 2;
  299. }
  300. _ => println!("unk {:x}", self.opcode),
  301. }
  302. }
  303. 0xF000 => {
  304. match self.opcode & 0x00FF {
  305. 0x0007 => {
  306. // FX07 Sets VX to the value of the delay timer
  307. self.v[x] = self.delay_timer;
  308. self.pc += 2;
  309. }
  310. 0x000A => {
  311. // FX0A A key press is awaited, and then stored in VX.
  312. // (Blocking Operation. All instruction halted until
  313. // next key event)
  314. let mut pressed: bool = false;
  315. for i in 0..16 {
  316. if self.key[i] == 1 {
  317. self.v[x] = i as u8;
  318. pressed = true;
  319. }
  320. }
  321. if pressed {
  322. self.pc += 2;
  323. }
  324. }
  325. 0x0015 => {
  326. // FX15 Sets the delay timer to VX
  327. self.delay_timer = self.v[x];
  328. self.pc += 2;
  329. }
  330. 0x0018 => {
  331. // FX18 Sets the sound timer to VX
  332. self.sound_timer = self.v[x];
  333. self.pc += 2;
  334. }
  335. 0x001E => {
  336. // FX1E Adds VX to I. VF is not affected
  337. self.index += self.v[x] as u16;
  338. self.pc += 2;
  339. }
  340. 0x0029 => {
  341. // FX29 Sets I to the location of the sprite for the
  342. // character in VX. Characters 0-F (in hexadecimal) are
  343. // represented by a 4x5 font
  344. self.index = self.v[x] as u16 * 5;
  345. self.pc += 2;
  346. }
  347. 0x0033 => {
  348. self.memory[self.index as usize] = self.v[x] / 100;
  349. self.memory[self.index as usize + 1] = (self.v[x] / 10) % 10;
  350. self.memory[self.index as usize + 2] = (self.v[x] / 100) % 10;
  351. self.pc += 2;
  352. }
  353. 0x0055 => {
  354. // FX55 Stores V0 to VX (including VX) in memory
  355. // starting at address I. The offset from I is
  356. // increased by 1 for each value written, but I itself
  357. // is left unmodified
  358. for i in 0..(x + 1) {
  359. self.memory[self.index as usize + i] = self.v[i];
  360. }
  361. self.pc += 2;
  362. }
  363. 0x0064 => {
  364. // 0xFX65 Fills V0 to VX (including VX) with values
  365. // from memory starting at address I. The offset from I
  366. // is increased by 1 for each value written, but I
  367. // itself is left unmodified
  368. for i in 0..(x + 1) {
  369. self.v[i] = self.memory[self.index as usize + i];
  370. }
  371. self.pc += 2;
  372. }
  373. _ => println!("unk {:x}", self.opcode),
  374. }
  375. }
  376. _ => println!("opc {:x}", self.opcode),
  377. }
  378. }
  379. }
  380. #[cfg(test)]
  381. mod tests {
  382. use super::*;
  383. #[test]
  384. fn test_load_game() {
  385. let mut c = Chip8::new();
  386. c.load_game("Cargo.toml");
  387. c.emulate_cycle();
  388. }
  389. }