Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanballs committed Oct 4, 2024
1 parent d3f1957 commit c6634a6
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 61 deletions.
4 changes: 1 addition & 3 deletions src/cartridge/mbc0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@ impl MBC for MBC0 {
self.rom[addr as usize]
}

fn write_byte(&mut self, _addr: u16, _value: u8) {
();
}
fn write_byte(&mut self, _addr: u16, _value: u8) {}
}
4 changes: 2 additions & 2 deletions src/cartridge/mbc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ impl MBC for MBC1 {
self.rom_bank as u16
};

let idx = bank * 0x4000 | (addr & 0x3FFF);
let idx = (bank * 0x4000) | (addr & 0x3FFF);

return self.rom[idx as usize];
self.rom[idx as usize]
}
0xA000..=0xBFFF => self.ram[addr as usize - 0xA000],
_ => {
Expand Down
6 changes: 3 additions & 3 deletions src/cpu/execution/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl CPU {
*/
pub(in crate::cpu) fn swap(&mut self, mmu: &mut MMU, r: R8) {
let register_value = self.get_r8_byte(mmu, r);
let result = (register_value >> 4) | (register_value << 4);
let result = register_value.rotate_left(4);
self.set_r8_byte(mmu, r, result);
self.registers.f.zero = result == 0;
self.registers.f.carry = false;
Expand All @@ -56,7 +56,7 @@ impl CPU {

pub(in crate::cpu) fn rlc(&mut self, mmu: &mut MMU, r: R8) {
let value = self.get_r8_byte(mmu, r);
let result = (value << 1) | (value >> 7);
let result = value.rotate_left(1);
self.set_r8_byte(mmu, r, result);
self.registers.f.zero = result == 0;
self.registers.f.subtract = false;
Expand Down Expand Up @@ -137,7 +137,7 @@ impl CPU {

pub(in crate::cpu) fn rrc(&mut self, mmu: &mut MMU, r: R8) {
let value = self.get_r8_byte(mmu, r);
let result = value >> 1 | (value << 7);
let result = value.rotate_right(1);
self.registers.f.carry = value & 1 > 0;
self.registers.f.half_carry = false;
self.registers.f.subtract = false;
Expand Down
4 changes: 2 additions & 2 deletions src/cpu/flag_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ impl FlagsRegister {
}

pub fn as_byte(&self) -> u8 {
return ((self.zero as u8) << ZERO_FLAG_BYTE_POSITION)
((self.zero as u8) << ZERO_FLAG_BYTE_POSITION)
| ((self.subtract as u8) << SUBTRACT_FLAG_BYTE_POSITION)
| ((self.half_carry as u8) << HALF_CARRY_FLAG_BYTE_POSITION)
| ((self.carry as u8) << CARRY_FLAG_BYTE_POSITION);
| ((self.carry as u8) << CARRY_FLAG_BYTE_POSITION)
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub struct CPU {
pub ime: bool,
}

impl Default for CPU {
fn default() -> Self {
Self::new()
}
}

impl CPU {
pub fn new() -> CPU {
CPU {
Expand Down Expand Up @@ -227,6 +233,6 @@ impl CPU {
pub fn get_memory_word(&mut self, memory: &MMU, addr: u16) -> u16 {
let little = memory.read_byte(addr) as u16;
let big = memory.read_byte(addr + 1) as u16;
return (big << 8) | little;
(big << 8) | little
}
}
6 changes: 6 additions & 0 deletions src/cpu/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub struct Registers {
pub pc: u16,
}

impl Default for Registers {
fn default() -> Self {
Self::new()
}
}

impl Registers {
pub fn new() -> Registers {
if is_gameboy_doctor() {
Expand Down
31 changes: 14 additions & 17 deletions src/gameboy/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn parse_number(s: &str) -> Option<u16> {
} else if s.to_lowercase().starts_with("0b") {
u16::from_str_radix(&s[2..], 2).ok()
} else {
u16::from_str_radix(s, 10).ok()
s.parse::<u16>().ok()
}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ impl GameBoy {
.map(|o| {
format!(
"{:02x}",
self.mmu.read_byte(self.cpu.registers.pc + o as u16)
self.mmu.read_byte(self.cpu.registers.pc + o)
)
})
.collect::<Vec<String>>()
Expand All @@ -144,17 +144,14 @@ impl GameBoy {
fn print_memory_range(&self, args: Vec<&str>) {
const BYTES_PER_ROW: u16 = 16;

let parsed_args = args.iter().map(|s| parse_number(s)).fold(
Some(vec![] as Vec<u16>),
|acc, curr| match (acc, curr) {
(None, _) => None,
(_, None) => None,
(Some(mut v), Some(n)) => {
v.push(n);
Some(v.to_vec())
}
},
);
let parsed_args: Option<Vec<u16>> = args.iter()
.map(|s| parse_number(s))
.try_fold(Vec::new(), |mut acc, curr| {
curr.map(|n| {
acc.push(n);
acc
})
});

let (start, end) = match parsed_args {
Some(nums) => match nums.as_slice() {
Expand Down Expand Up @@ -200,13 +197,13 @@ impl GameBoy {
if b {
return "true ".green().to_string();
}
return "false".red().to_string();
"false".red().to_string()
}

println!("======== interrupts ========");
println!(" enabled flagged");
println!("IME: {}", colored_bool(self.cpu.ime));
println!("");
println!();
println!(
"VBlank: {:5} {}",
colored_bool(self.mmu.ie & 0x1 > 0),
Expand All @@ -233,7 +230,7 @@ impl GameBoy {
colored_bool(self.mmu.read_byte(0xFF0F) & 0x10 > 0)
);

println!("");
println!();
}

fn print_help(&self) {
Expand All @@ -251,7 +248,7 @@ impl GameBoy {
println!("[ro]m display gameboy rom");
println!("[ins]tructions last cpu operations");
println!("=============================================");
println!("");
println!();
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/gameboy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ impl GameBoy {
let opcode = self.mmu.read_byte(self.cpu.registers.pc);
let arg_1 = self.mmu.read_byte(self.cpu.registers.pc + 1);
let arg_2 = self.mmu.read_byte(self.cpu.registers.pc + 2);
let (ins, _, _) = parse(opcode, arg_1, arg_2);

match parse(opcode, arg_1, arg_2) {
(ins, _, _) => ins,
}
ins
}

fn print_gameboy_doctor(&self) {
Expand Down
5 changes: 2 additions & 3 deletions src/instructions/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl fmt::Display for Instruction {
let args: &mut Vec<String> = match buffer.find('(') {
Some(index) => &mut buffer[index + 1..buffer.len() - 1]
.split(',')
.into_iter()
.map(|s| s.trim())
.map(|s| {
s.parse::<i32>()
Expand All @@ -63,7 +62,7 @@ impl fmt::Display for Instruction {
"a" => "a".cyan().to_string(),

// Memory address
"r16mem" => format!("{}", args.pop().unwrap()).blue().to_string(),
"r16mem" => args.pop().unwrap().to_string().blue().to_string(),
"imm8mem" => format!("[{}]", args.pop().unwrap()).blue().to_string(),
"imm16mem" => format!("[{}]", args.pop().unwrap()).blue().to_string(),
"cmem" => format!("[{}]", args.pop().unwrap()).blue().to_string(),
Expand All @@ -77,7 +76,7 @@ impl fmt::Display for Instruction {
.join(" ");

// Process the captured string (example: convert to uppercase)
let processed = format!("{}", instruction_formatted);
let processed = instruction_formatted.to_string();

// Write the processed string to the actual formatter
write!(f, "{}", processed.to_lowercase())
Expand Down
14 changes: 7 additions & 7 deletions src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn parse(opcode: u8, arg1: u8, arg2: u8) -> (Instruction, u16, u8) {
let imm16: u16 = (arg2 as u16) << 8 | arg1 as u16;
let imm8: u8 = arg1;

return match opcode {
match opcode {
// Block 0
0x00 => (Instruction::Nop, 1, 4),
0x01 => (Instruction::LdR16Imm16(R16::BC, imm16), 3, 12),
Expand Down Expand Up @@ -99,7 +99,7 @@ pub fn parse(opcode: u8, arg1: u8, arg2: u8) -> (Instruction, u16, u8) {
_ => 4,
};

return match (opcode >> 3) & 0x7 {
match (opcode >> 3) & 0x7 {
0 => (Instruction::AddAR8(operand), 1, cycle_count),
1 => (Instruction::AdcAR8(operand), 1, cycle_count),
2 => (Instruction::SubAR8(operand), 1, cycle_count),
Expand All @@ -108,7 +108,7 @@ pub fn parse(opcode: u8, arg1: u8, arg2: u8) -> (Instruction, u16, u8) {
5 => (Instruction::XorAR8(operand), 1, cycle_count),
6 => (Instruction::OrAR8(operand), 1, cycle_count),
_ => (Instruction::CpAR8(operand), 1, cycle_count),
};
}
}

// Block 3
Expand Down Expand Up @@ -178,7 +178,7 @@ pub fn parse(opcode: u8, arg1: u8, arg2: u8) -> (Instruction, u16, u8) {
0xF4 => (Instruction::ILLEGAL, 1, 4),
0xFC => (Instruction::ILLEGAL, 1, 4),
0xFD => (Instruction::ILLEGAL, 1, 4),
};
}
}

fn parse_prefixed(opcode: u8) -> (Instruction, u16, u8) {
Expand Down Expand Up @@ -208,7 +208,7 @@ fn parse_prefixed(opcode: u8) -> (Instruction, u16, u8) {
}
};

return match instruction {
match instruction {
Instruction::BitB3R8(_, R8::HL) => (instruction, 2, 12),
Instruction::RlcR8(R8::HL) => (instruction, 2, 16),
Instruction::RlR8(R8::HL) => (instruction, 2, 16),
Expand All @@ -221,7 +221,7 @@ fn parse_prefixed(opcode: u8) -> (Instruction, u16, u8) {
Instruction::ResB3R8(_, R8::HL) => (instruction, 2, 16),
Instruction::SetB3R8(_, R8::HL) => (instruction, 2, 16),
ins => (ins, 2, 8),
};
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -335,7 +335,7 @@ mod test {

let error_message = format!(
"{} (opcode: {:#04X} {:#04X})",
json["mnemonic"].to_string(),
json["mnemonic"],
opcode,
imm8
);
Expand Down
10 changes: 8 additions & 2 deletions src/mmu/joypad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ pub struct Joypad {
dulr: u8,
}

impl Default for Joypad {
fn default() -> Self {
Self::new()
}
}

impl Joypad {
pub fn new() -> Joypad {
Joypad {
Expand Down Expand Up @@ -58,7 +64,7 @@ impl Joypad {
match addr {
// Joy pad
0xFF00 => {
return match (self.joypad >> 4) & 0x3 {
match (self.joypad >> 4) & 0x3 {
// Return both
0x0 => 0xC0 | (self.dulr & self.ssba),

Expand All @@ -72,7 +78,7 @@ impl Joypad {
0x3 => 0xFF,

_ => unreachable!(),
};
}
}

_ => unreachable!(),
Expand Down
2 changes: 1 addition & 1 deletion src/mmu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl MMU {

// Interrupt
0xFF0F => {
self.ppu.vblank_irq = value & 0x1 == 0x2;
self.ppu.vblank_irq = value & 0x1 == 0x1;
self.ppu.stat_irq = value & 0x2 == 0x2;
self.timer.timer_irq = value & 0x4 == 0x4;
self.joypad.joypad_irq = value & 0x8 == 0x8;
Expand Down
Loading

0 comments on commit c6634a6

Please sign in to comment.