Skip to content

Commit

Permalink
nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
pineman committed Oct 2, 2023
1 parent 495a65c commit 738bf85
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod bitwise;
mod bitwise;
pub mod lr35902;
106 changes: 56 additions & 50 deletions src/lr35902.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use std::{thread, time::Duration};
pub mod instructions;
use instructions::{instructions, Instruction, InstructionKind};

use crate::bitwise;
use crate::bitwise as bw;

#[allow(dead_code)]
#[derive(PartialEq)]
pub struct LR35902 {
af: u16,
Expand Down Expand Up @@ -72,92 +71,99 @@ impl LR35902 {
}

fn a(&self) -> u8 {
bitwise::get_byte16::<1>(self.af)
bw::get_byte16::<1>(self.af)
}

fn b(&self) -> u8 {
bitwise::get_byte16::<1>(self.bc)
bw::get_byte16::<1>(self.bc)
}

fn c(&self) -> u8 {
bitwise::get_byte16::<0>(self.bc)
}

fn set_b(&mut self, value: u8) {
self.bc = bitwise::set_byte16::<1>(self.bc, value);
}

fn set_c(&mut self, value: u8) {
self.bc = bitwise::set_byte16::<0>(self.bc, value);
}

pub fn set_bc(&mut self, bc: u16) {
self.bc = bc;
bw::get_byte16::<0>(self.bc)
}

fn d(&self) -> u8 {
bitwise::get_byte16::<1>(self.de)
bw::get_byte16::<1>(self.de)
}

fn e(&self) -> u8 {
bitwise::get_byte16::<0>(self.de)
bw::get_byte16::<0>(self.de)
}

fn h(&self) -> u8 {
bitwise::get_byte16::<1>(self.hl)
bw::get_byte16::<1>(self.hl)
}

fn l(&self) -> u8 {
bitwise::get_byte16::<0>(self.hl)
bw::get_byte16::<0>(self.hl)
}

fn z_flag(&self) -> bool {
bitwise::test_bit16::<8>(self.af)
bw::test_bit16::<8>(self.af)
}

fn n_flag(&self) -> bool {
bitwise::test_bit16::<7>(self.af)
bw::test_bit16::<7>(self.af)
}

fn h_flag(&self) -> bool {
bitwise::test_bit16::<6>(self.af)
bw::test_bit16::<6>(self.af)
}

fn c_flag(&self) -> bool {
bitwise::test_bit16::<5>(self.af)
bw::test_bit16::<5>(self.af)
}

fn set_z_flag(&mut self, value: bool) {
self.af = bitwise::set_bit16::<8>(self.af, value);
fn set_a(&mut self, value: u8) {
self.af = bw::set_byte16::<1>(self.af, value);
}

fn set_n_flag(&mut self, value: bool) {
self.af = bitwise::set_bit16::<7>(self.af, value);
pub fn set_af(&mut self, af: u16) {
self.af = af;
}

fn set_h_flag(&mut self, value: bool) {
self.af = bitwise::set_bit16::<6>(self.af, value);
fn set_b(&mut self, value: u8) {
self.bc = bw::set_byte16::<1>(self.bc, value);
}

fn set_c_flag(&mut self, value: bool) {
self.af = bitwise::set_bit16::<5>(self.af, value);
fn set_c(&mut self, value: u8) {
self.bc = bw::set_byte16::<0>(self.bc, value);
}

fn set_a(&mut self, value: u8) {
self.af = bitwise::set_byte16::<1>(self.af, value);
pub fn set_bc(&mut self, bc: u16) {
self.bc = bc;
}

fn set_d(&mut self, value: u8) {
self.de = bitwise::set_byte16::<1>(self.de, value);
self.de = bw::set_byte16::<1>(self.de, value);
}

fn set_e(&mut self, value: u8) {
self.de = bitwise::set_byte16::<0>(self.de, value);
self.de = bw::set_byte16::<0>(self.de, value);
}

fn set_h(&mut self, value: u8) {
self.hl = bitwise::set_byte16::<1>(self.hl, value);
self.hl = bw::set_byte16::<1>(self.hl, value);
}

fn set_l(&mut self, value: u8) {
self.hl = bitwise::set_byte16::<0>(self.hl, value);
self.hl = bw::set_byte16::<0>(self.hl, value);
}

fn set_z_flag(&mut self, value: bool) {
self.af = bw::set_bit16::<8>(self.af, value);
}

fn set_n_flag(&mut self, value: bool) {
self.af = bw::set_bit16::<7>(self.af, value);
}

fn set_h_flag(&mut self, value: bool) {
self.af = bw::set_bit16::<6>(self.af, value);
}

fn set_c_flag(&mut self, value: bool) {
self.af = bw::set_bit16::<5>(self.af, value);
}

pub fn set_pc(&mut self, pc: u16) {
Expand Down Expand Up @@ -1061,7 +1067,7 @@ impl LR35902 {
unimplemented!()
}
0xD3 => {
// NOTHING
// Not implemented
unimplemented!()
}
0xD4 => {
Expand Down Expand Up @@ -1093,15 +1099,15 @@ impl LR35902 {
unimplemented!()
}
0xDB => {
// NOTHING
// Not implemented
unimplemented!()
}
0xDC => {
// CALL C,a16
unimplemented!()
}
0xDD => {
// NOTHING
// Not implemented
unimplemented!()
}
0xDE => {
Expand All @@ -1125,11 +1131,11 @@ impl LR35902 {
self.set_memory8(self.c().into(), self.a());
}
0xE3 => {
// NOTHING
// Not implemented
unimplemented!()
}
0xE4 => {
// NOTHING
// Not implemented
unimplemented!()
}
0xE5 => {
Expand Down Expand Up @@ -1157,15 +1163,15 @@ impl LR35902 {
unimplemented!()
}
0xEB => {
// NOTHING
// Not implemented
unimplemented!()
}
0xEC => {
// NOTHING
// Not implemented
unimplemented!()
}
0xED => {
// NOTHING
// Not implemented
unimplemented!()
}
0xEE => {
Expand Down Expand Up @@ -1193,7 +1199,7 @@ impl LR35902 {
unimplemented!()
}
0xF4 => {
// NOTHING
// Not implemented
unimplemented!()
}
0xF5 => {
Expand Down Expand Up @@ -1225,11 +1231,11 @@ impl LR35902 {
unimplemented!()
}
0xFC => {
// NOTHING
// Not implemented
unimplemented!()
}
0xFD => {
// NOTHING
// Not implemented
unimplemented!()
}
0xFE => {
Expand Down
12 changes: 6 additions & 6 deletions tests/lr35902.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fpt::lr35902::instructions::{instructions, Instruction};
use fpt::lr35902::LR35902;

#[derive(Clone)]
#[allow(dead_code)]
struct LR35902Builder {
af: u16,
bc: u16,
Expand All @@ -15,6 +15,7 @@ struct LR35902Builder {
clock_cycles: u64,
}

#[allow(dead_code)]
impl LR35902Builder {
pub fn new() -> Self {
Self {
Expand Down Expand Up @@ -81,19 +82,19 @@ impl LR35902Builder {
fn test_instr_0x000_nop() {
let builder = LR35902Builder::new().with_memory_byte(0, 0);
let mut sut = builder.clone().build();

sut.step();

let expected = builder.with_pc(1).with_clock_cycles(4).build();

assert_eq!(sut, expected);
}

#[test]
fn test_instr_0x001_ld_bc_d16() {
let builder = LR35902Builder::new()
.with_memory_byte(0x0000, 0x1) // instruction ld bc from imediate16
.with_memory_byte(0x0001, 2) // lsb of imediate16
.with_memory_byte(0x0002, 1); // msb of imediate16
.with_memory_byte(0x0000, 0x1) // instruction ld bc from immediate16
.with_memory_byte(0x0001, 2) // lsb of immediate16
.with_memory_byte(0x0002, 1); // msb of immediate16
let mut sut = builder.clone().build();

sut.step();
Expand All @@ -103,6 +104,5 @@ fn test_instr_0x001_ld_bc_d16() {
.with_bc(0x0102) // (1 << 8) + 2 == 0x0102
.with_clock_cycles(12)
.build();

assert_eq!(sut, expected);
}

0 comments on commit 738bf85

Please sign in to comment.