5

I'm writing an interpreted 68k emulator as a personal/educational project. Right now I'm trying to develop a simple, general decoding mechanism.

As I understand it, the first two bytes of each instruction are enough to uniquely identify the operation (with two rare exceptions) and the number of words left to be read, if any.

Here is what I would like to accomplish in my decoding phase:

1. read two bytes
2. determine which instruction it is
3. extract the operands
4. pass the opcode and the operands on to the execute phase

I can't just pass the first two bytes into a lookup table like I could with the first few bits in a RISC arch, because operands are "in the way". How can I accomplish part 2 in a general way?

Broadly, my question is: How do I remove the variability of operands from the decoding process?

More background:

Here is a partial table from section 8.2 of the Programmer's Reference Manual:

Table 8.2. Operation Code Map

Bits 15-12      Operation
0000            Bit Manipulation/MOVEP/Immediate
0001            Move Byte
...
1110            Shift/Rotate/Bit Field
1111            Coprocessor Interface...

This made great sense to me, but then I look at the bit patterns for each instruction and notice that there isn't a single instruction where bits 15-12 are 0001, 0010, or 0011. There must be some big piece of the picture that I'm missing.

This Decoding Z80 Opcodes site explains decoding explicitly, which is something I haven't found in the 68k programmer's reference manual or by googling.

mwcz
  • 8,949
  • 10
  • 42
  • 63
  • How far did your project grow, do you have a disassembler or emulator? –  Oct 14 '11 at 15:30
  • I am still building out a script that generates a complete look-up table. About 70% done. – mwcz Oct 14 '11 at 19:26
  • @CountablyInfinite Are you working on a similar project? – mwcz Oct 15 '11 at 14:57
  • I have heard of a 68k decoder for the mac some 20 years ago. As part of MacMETH. I would like to reverse engineer a program of mine, since I have not yet found the source of it. –  Oct 15 '11 at 15:48
  • My project will likely never have a disassembler, but you should be able to use gdb to disassemble a 68k program. – mwcz Oct 15 '11 at 19:05
  • @CountablyInfinite It looks like libcpu has a 68k decoder as well: http://www.libcpu.org/wiki/Motorola_68K – mwcz Nov 15 '11 at 00:57

1 Answers1

2

I've decided to simply create a look-up table with every possible pattern for each instruction. It was my first idea, but I discarded it as "wasteful, inelegant". Now, I'm accepting it as "really fast".

mwcz
  • 8,949
  • 10
  • 42
  • 63