#Building an opcode matrix

15 messages · Page 1 of 1 (latest)

manic marten
#

Some preface: Interface is a trait that has a read method and write method.

Is this a good way to build an opcode matrix? If not, what improvements can I make?

const OPCODE_MATRIX: [fn(&mut Chip6502, &mut dyn Interface) -> (); 2] = [
    |cpu, _interface| {
        cpu.nop();
    },
    |cpu, interface| {
        let value = cpu.get_immediate(interface);
        cpu.sta(value);
    },
];

impl Chip6502 {
    pub(crate) fn instruction<T: Interface>(&mut self, interface: &mut T, instruction: u8) {
        OPCODE_MATRIX[instruction as usize](self, interface);
    }
}```
plush brook
#

Why not just a match?

manic marten
#

Because I don't wanna match 0, 1, 2, 3 etc.

plush brook
#

Why not?

#

You're already doing that, just hidden

manic marten
#

Feels a bit dumb but idk

plush brook
#

Besides, you can use an enum

manic marten
#

The instruction I am getting is full range of u8

plush brook
#

Read from a file or similar I guess?

manic marten
#

Ermm ya basically

plush brook
#

Ah, that's why you call it a matrix

manic marten
#

Lol ya

plush brook
#

I think 0xA7 => is much clearer than a [fn(...); 256]

manic marten
#

fair