<commit_before>use super::cpu::*;
use num::FromPrimitive;

pub trait Instruction {
    fn execute(&self, &mut Cpu);
}

struct Instruction_UNSUPPORTED;

impl Instruction for Instruction_UNSUPPORTED {
    fn execute(&self, cpu: &mut Cpu) {
        println!("{:?}", cpu);

        panic!("Unsupported instruction {:#x}", cpu.read_word(cpu.get_pc()));
    }
}

struct Instruction_DEC_SS {
    regpair: Reg16
}

impl Instruction for Instruction_DEC_SS {
    fn execute(&self, cpu: &mut Cpu) {
        let oldregval = cpu.read_reg16(self.regpair);
        cpu.write_reg16(self.regpair, oldregval - 1);

        println!("{:#06x}: DEC {:?}", cpu.get_pc(), self.regpair);
        cpu.inc_pc(1);
    }
}

struct Instruction_LD_R_N {
    r: Reg8
}

impl Instruction for Instruction_LD_R_N {
    fn execute(&self, cpu: &mut Cpu) {
        let n = cpu.read_word(cpu.get_pc() + 1);
        cpu.write_reg8(self.r, n);

        println!("{:#06x}: LD {:?}, {:#04X}", cpu.get_pc(), self.r, n);
        cpu.inc_pc(2);
    }
}

struct Instruction_LD_DD_NN {
    regpair: Reg16
}

impl Instruction for Instruction_LD_DD_NN {
    fn execute(&self, cpu: &mut Cpu) {
        let nn =  (cpu.read_word(cpu.get_pc() + 1) as u16) |
                 ((cpu.read_word(cpu.get_pc() + 2) as u16) << 8);
        cpu.write_reg16(self.regpair, nn);

        println!("{:#06x}: LD {:?}, {:#06X}", cpu.get_pc(), self.regpair, nn);
        cpu.inc_pc(3);
    }
}

struct Instruction_LD_HL_NN;

impl Instruction for Instruction_LD_HL_NN {
    fn execute(&self, cpu: &mut Cpu) {
        let nn =  (cpu.read_word(cpu.get_pc() + 1) as u16) |
                 ((cpu.read_word(cpu.get_pc() + 2) as u16) << 8);
        let nnmemval = (cpu.read_word(nn    ) as u16) |
                      ((cpu.read_word(nn + 1) as u16) << 8);

        cpu.write_reg16(Reg16::HL, nnmemval);

        println!("{:#06x}: LD HL, {:#06X}", cpu.get_pc(), nnmemval);
        cpu.inc_pc(3);
    }
}

struct Instruction_LD_IX_NN;

impl Instruction for Instruction_LD_IX_NN {
    fn execute(&self, cpu: &mut Cpu) {
        let curr_pc = cpu.get_pc();
        let d = cpu.read_word(curr_pc + 2);
        let n = cpu.read_word(curr_pc + 3);
        let nn = (d as u16) | ((n as u16) << 8);

        match cpu.read_word(curr_pc + 1) {
            0b00100001 => {
                cpu.set_ix(nn);
                println!("{:#06x}: LD IX, {:#06X}", cpu.get_pc(), nn);
            }
            0b00101010 => {<commit_msg>Fix ld_hl_nn debug output<commit_after>use super::cpu::*;
use num::FromPrimitive;

pub trait Instruction {
    fn execute(&self, &mut Cpu);
}

struct Instruction_UNSUPPORTED;

impl Instruction for Instruction_UNSUPPORTED {
    fn execute(&self, cpu: &mut Cpu) {
        println!("{:?}", cpu);

        panic!("Unsupported instruction {:#x}", cpu.read_word(cpu.get_pc()));
    }
}

struct Instruction_DEC_SS {
    regpair: Reg16
}

impl Instruction for Instruction_DEC_SS {
    fn execute(&self, cpu: &mut Cpu) {
        let oldregval = cpu.read_reg16(self.regpair);
        cpu.write_reg16(self.regpair, oldregval - 1);

        println!("{:#06x}: DEC {:?}", cpu.get_pc(), self.regpair);
        cpu.inc_pc(1);
    }
}

struct Instruction_LD_R_N {
    r: Reg8
}

impl Instruction for Instruction_LD_R_N {
    fn execute(&self, cpu: &mut Cpu) {
        let n = cpu.read_word(cpu.get_pc() + 1);
        cpu.write_reg8(self.r, n);

        println!("{:#06x}: LD {:?}, {:#04X}", cpu.get_pc(), self.r, n);
        cpu.inc_pc(2);
    }
}

struct Instruction_LD_DD_NN {
    regpair: Reg16
}

impl Instruction for Instruction_LD_DD_NN {
    fn execute(&self, cpu: &mut Cpu) {
        let nn =  (cpu.read_word(cpu.get_pc() + 1) as u16) |
                 ((cpu.read_word(cpu.get_pc() + 2) as u16) << 8);
        cpu.write_reg16(self.regpair, nn);

        println!("{:#06x}: LD {:?}, {:#06X}", cpu.get_pc(), self.regpair, nn);
        cpu.inc_pc(3);
    }
}

struct Instruction_LD_HL_NN;

impl Instruction for Instruction_LD_HL_NN {
    fn execute(&self, cpu: &mut Cpu) {
        let nn =  (cpu.read_word(cpu.get_pc() + 1) as u16) |
                 ((cpu.read_word(cpu.get_pc() + 2) as u16) << 8);
        let nnmemval = (cpu.read_word(nn    ) as u16) |
                      ((cpu.read_word(nn + 1) as u16) << 8);

        cpu.write_reg16(Reg16::HL, nnmemval);

        println!("{:#06x}: LD HL, ({:#06X})", cpu.get_pc(), nn);
        cpu.inc_pc(3);
    }
}

struct Instruction_LD_IX_NN;

impl Instruction for Instruction_LD_IX_NN {
    fn execute(&self, cpu: &mut Cpu) {
        let curr_pc = cpu.get_pc();
        let d = cpu.read_word(curr_pc + 2);
        let n = cpu.read_word(curr_pc + 3);
        let nn = (d as u16) | ((n as u16) << 8);

        match cpu.read_word(curr_pc + 1) {
            0b00100001 => {
                cpu.set_ix(nn);
                println!("{:#06x}: LD IX, {:#06X}", cpu.get_pc(), nn);
            }
            0b00101010 => {
{'fraction_non_alphanumeric': 0.1653061224489796, 'fraction_numerical': 0.03510204081632653, 'mean_word_length': 2.475886524822695, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7444798', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1818, 'n_words': 393}