m68000 0.2.3

A Motorola 68000 interpreter, disassembler and assembler (code emitter)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Disassembler module.

use crate::instruction::{Direction, Instruction};
use crate::isa::Isa;
use crate::status_register::disassemble_conditional_test;
use crate::utils::bits;

pub fn disassemble_unknown_instruction(inst: &Instruction) -> String {
    format!("Unknown instruction {:04X} at {:#X}", inst.opcode, inst.pc)
}

pub fn disassemble_abcd(inst: &Instruction) -> String {
    let (rx, _, mode, ry) = inst.operands.register_size_mode_register();
    if mode == Direction::MemoryToMemory {
        format!("ABCD -(A{}), -(A{})", ry, rx)
    } else {
        format!("ABCD D{}, D{}", ry, rx)
    }
}

pub fn disassemble_add(inst: &Instruction) -> String {
    let (r, d, s, am) = inst.operands.register_direction_size_effective_address();
    if d == Direction::DstEa {
        format!("ADD.{} D{}, {}", s, r, am)
    } else {
        format!("ADD.{} {}, D{}", s, am, r)
    }
}

pub fn disassemble_adda(inst: &Instruction) -> String {
    let (r, s, am) = inst.operands.register_size_effective_address();
    format!("ADDA.{} {}, A{}", s, am, r)
}

pub fn disassemble_addi(inst: &Instruction) -> String {
    let (s, am, imm) = inst.operands.size_effective_address_immediate();
    format!("ADDI.{} #{}, {}", s, imm, am)
}

pub fn disassemble_addq(inst: &Instruction) -> String {
    let (d, s, am) = inst.operands.data_size_effective_address();
    let d = if d == 0 { 8 } else { d };
    format!("ADDQ.{} #{}, {}", s, d, am)
}

pub fn disassemble_addx(inst: &Instruction) -> String {
    let (rx, s, mode, ry) = inst.operands.register_size_mode_register();
    if mode == Direction::MemoryToMemory {
        format!("ADDX.{} -(A{}), -(A{})", s, ry, rx)
    } else {
        format!("ADDX.{} D{}, D{}", s, ry, rx)
    }
}

pub fn disassemble_and(inst: &Instruction) -> String {
    let (r, d, s, am) = inst.operands.register_direction_size_effective_address();
    if d == Direction::DstEa {
        format!("AND.{} D{}, {}", s, r, am)
    } else {
        format!("AND.{} {}, D{}", s, am, r)
    }
}

pub fn disassemble_andi(inst: &Instruction) -> String {
    let (s, am, imm) = inst.operands.size_effective_address_immediate();
    format!("ANDI.{} #{}, {}", s, imm, am)
}

pub fn disassemble_andiccr(inst: &Instruction) -> String {
    let imm = inst.operands.immediate();
    format!("ANDI {:#X}, CCR", imm)
}

pub fn disassemble_andisr(inst: &Instruction) -> String {
    let imm = inst.operands.immediate();
    format!("ANDI {:#X}, SR", imm)
}

pub fn disassemble_asm(inst: &Instruction) -> String {
    let (d, am) = inst.operands.direction_effective_address();
    format!("AS{} {}", d, am)
}

pub fn disassemble_asr(inst: &Instruction) -> String {
    let (rot, d, s, ir, reg) = inst.operands.rotation_direction_size_mode_register();
    if ir {
        format!("AS{}.{} D{}, D{}", d, s, rot, reg)
    } else {
        let rot = if rot == 0 { 8 } else { rot };
        format!("AS{}.{} #{}, D{}", d, s, rot, reg)
    }
}

pub fn disassemble_bcc(inst: &Instruction) -> String {
    let (cc, disp) = inst.operands.condition_displacement();
    format!("B{} {} <{:#X}>", disassemble_conditional_test(cc), disp, inst.pc.wrapping_add(2).wrapping_add(disp as u32))
}

pub fn disassemble_bchg(inst: &Instruction) -> String {
    let (am, count) = inst.operands.effective_address_count();
    if bits(inst.opcode, 8, 8) != 0 {
        format!("BCHG D{}, {}", count, am)
    } else {
        format!("BCHG #{}, {}", count, am)
    }
}

pub fn disassemble_bclr(inst: &Instruction) -> String {
    let (am, count) = inst.operands.effective_address_count();
    if bits(inst.opcode, 8, 8) != 0 {
        format!("BCLR D{}, {}", count, am)
    } else {
        format!("BCLR #{}, {}", count, am)
    }
}

pub fn disassemble_bra(inst: &Instruction) -> String {
    let disp = inst.operands.displacement();
    format!("BRA {} <{:#X}>", disp, inst.pc.wrapping_add(2).wrapping_add(disp as u32))
}

pub fn disassemble_bset(inst: &Instruction) -> String {
    let (am, count) = inst.operands.effective_address_count();
    if bits(inst.opcode, 8, 8) != 0 {
        format!("BSET D{}, {}", count, am)
    } else {
        format!("BSET #{}, {}", count, am)
    }
}

pub fn disassemble_bsr(inst: &Instruction) -> String {
    let disp = inst.operands.displacement();
    format!("BSR {} <{:#X}>", disp, inst.pc.wrapping_add(2).wrapping_add(disp as u32))
}

pub fn disassemble_btst(inst: &Instruction) -> String {
    let (am, count) = inst.operands.effective_address_count();
    if bits(inst.opcode, 8, 8) != 0 {
        format!("BTST D{}, {}", count, am)
    } else {
        format!("BTST #{}, {}", count, am)
    }
}

pub fn disassemble_chk(inst: &Instruction) -> String {
    let (r, am) = inst.operands.register_effective_address();
    format!("CHK.W {}, D{}", am, r)
}

pub fn disassemble_clr(inst: &Instruction) -> String {
    let (s, am) = inst.operands.size_effective_address();
    format!("CLR.{} {}", s, am)
}

pub fn disassemble_cmp(inst: &Instruction) -> String {
    let (r, _, s, am) = inst.operands.register_direction_size_effective_address();
    format!("CMP.{} {}, D{}", s, am, r)
}

pub fn disassemble_cmpa(inst: &Instruction) -> String {
    let (r, s, am) = inst.operands.register_size_effective_address();
    format!("CMPA.{} {}, A{}", s, am, r)
}

pub fn disassemble_cmpi(inst: &Instruction) -> String {
    let (s, am, imm) = inst.operands.size_effective_address_immediate();
    format!("CMPI.{} #{}, {}", s, imm, am)
}

pub fn disassemble_cmpm(inst: &Instruction) -> String {
    let (rx, s, ry) = inst.operands.register_size_register();
    format!("CMPM.{} (A{})+, (A{})+", s, ry, rx)
}

pub fn disassemble_dbcc(inst: &Instruction) -> String {
    let (cc, r, disp) = inst.operands.condition_register_displacement();
    format!("DB{} D{}, {} <{:#X}>", disassemble_conditional_test(cc), r, disp, inst.pc.wrapping_add(2).wrapping_add(disp as u32))
}

pub fn disassemble_divs(inst: &Instruction) -> String {
    let (r, am) = inst.operands.register_effective_address();
    format!("DIVS.W {}, D{}", am, r)
}

pub fn disassemble_divu(inst: &Instruction) -> String {
    let (r, am) = inst.operands.register_effective_address();
    format!("DIVU.W {}, D{}", am, r)
}

pub fn disassemble_eor(inst: &Instruction) -> String {
    let (r, _, s, am) = inst.operands.register_direction_size_effective_address();
    format!("EOR.{} D{}, {}", s, r, am)
}

pub fn disassemble_eori(inst: &Instruction) -> String {
    let (s, am, imm) = inst.operands.size_effective_address_immediate();
    format!("EORI.{} #{}, {}", s, imm, am)
}

pub fn disassemble_eoriccr(inst: &Instruction) -> String {
    let imm = inst.operands.immediate();
    format!("EORI {:#X}, CCR", imm)
}

pub fn disassemble_eorisr(inst: &Instruction) -> String {
    let imm = inst.operands.immediate();
    format!("EORI {:#X}, SR", imm)
}

pub fn disassemble_exg(inst: &Instruction) -> String {
    let (rx, mode, ry) = inst.operands.register_opmode_register();
    if mode == Direction::ExchangeData {
        format!("EXG D{}, D{}", rx, ry)
    } else if mode == Direction::ExchangeAddress {
        format!("EXG A{}, A{}", rx, ry)
    } else {
        format!("EXG D{}, A{}", rx, ry)
    }
}

pub fn disassemble_ext(inst: &Instruction) -> String {
    let (mode, r) = inst.operands.opmode_register();
    if mode == 0b010 {
        format!("EXT.W D{}", r)
    } else {
        format!("EXT.L D{}", r)
    }
}

pub fn disassemble_illegal(_: &Instruction) -> String {
    "ILLEGAL".to_string()
}

pub fn disassemble_jmp(inst: &Instruction) -> String {
    let am = inst.operands.effective_address();
    format!("JMP {}", am)
}

pub fn disassemble_jsr(inst: &Instruction) -> String {
    let am = inst.operands.effective_address();
    format!("JSR {}", am)
}

pub fn disassemble_lea(inst: &Instruction) -> String {
    let (r, am) = inst.operands.register_effective_address();
    format!("LEA {}, A{}", am, r)
}

pub fn disassemble_link(inst: &Instruction) -> String {
    let (r, disp) = inst.operands.register_displacement();
    format!("LINK.W A{}, #{}", r, disp)
}

pub fn disassemble_lsm(inst: &Instruction) -> String {
    let (d, am) = inst.operands.direction_effective_address();
    format!("LS{} {}", d, am)
}

pub fn disassemble_lsr(inst: &Instruction) -> String {
    let (rot, d, s, ir, reg) = inst.operands.rotation_direction_size_mode_register();
    if ir {
        format!("LS{}.{} D{}, D{}", d, s, rot, reg)
    } else {
        let rot = if rot == 0 { 8 } else { rot };
        format!("LS{}.{} #{}, D{}", d, s, rot, reg)
    }
}

pub fn disassemble_move(inst: &Instruction) -> String {
    let (s, dst, src) = inst.operands.size_effective_address_effective_address();
    format!("MOVE.{} {}, {}", s, src, dst)
}

pub fn disassemble_movea(inst: &Instruction) -> String {
    let (s, r, am) = inst.operands.size_register_effective_address();
    format!("MOVEA.{} {:#X}, A{}", s, am, r)
}

pub fn disassemble_moveccr(inst: &Instruction) -> String {
    let am = inst.operands.effective_address();
    format!("MOVE {:#X}, CCR", am)
}

pub fn disassemble_movefsr(inst: &Instruction) -> String {
    let am = inst.operands.effective_address();
    format!("MOVE SR, {:#X}", am)
}

pub fn disassemble_movesr(inst: &Instruction) -> String {
    let am = inst.operands.effective_address();
    format!("MOVE {:#X}, SR", am)
}

pub fn disassemble_moveusp(inst: &Instruction) -> String {
    let (dir, reg) = inst.operands.direction_register();
    if dir == Direction::UspToRegister {
        format!("MOVE USP, A{}", reg)
    } else {
        format!("MOVE A{}, USP", reg)
    }
}

pub fn disassemble_movem(inst: &Instruction) -> String {
    // TODO: disassemble register list.
    let (d, s, am, list) = inst.operands.direction_size_effective_address_list();
    if d == Direction::MemoryToRegister {
        format!("MOVEM.{} {}, {:#X}", s, am, list)
    } else {
        format!("MOVEM.{} {:#X}, {}", s, list, am)
    }
}

pub fn disassemble_movep(inst: &Instruction) -> String {
    let (dreg, d, s, areg, disp) = inst.operands.register_direction_size_register_displacement();
    if d == Direction::RegisterToMemory {
        format!("MOVEP.{} D{}, ({}, A{})", s, dreg, disp, areg)
    } else {
        format!("MOVEP.{} ({}, A{}), D{}", s, disp, areg, dreg)
    }
}

pub fn disassemble_moveq(inst: &Instruction) -> String {
    let (r, d) = inst.operands.register_data();
    format!("MOVEQ.L #{}, D{}", d, r)
}

pub fn disassemble_muls(inst: &Instruction) -> String {
    let (r, am) = inst.operands.register_effective_address();
    format!("MULS.W {}, D{}", am, r)
}

pub fn disassemble_mulu(inst: &Instruction) -> String {
    let (r, am) = inst.operands.register_effective_address();
    format!("MULU.W {}, D{}", am, r)
}

pub fn disassemble_nbcd(inst: &Instruction) -> String {
    let am = inst.operands.effective_address();
    format!("NBCD {}", am)
}

pub fn disassemble_neg(inst: &Instruction) -> String {
    let (s, am) = inst.operands.size_effective_address();
    format!("NEG.{} {}", s, am)
}

pub fn disassemble_negx(inst: &Instruction) -> String {
    let (s, am) = inst.operands.size_effective_address();
    format!("NEGX.{} {}", s, am)
}

pub fn disassemble_nop(_: &Instruction) -> String {
    "NOP".to_string()
}

pub fn disassemble_not(inst: &Instruction) -> String {
    let (s, am) = inst.operands.size_effective_address();
    format!("NOT.{} {}", s, am)
}

pub fn disassemble_or(inst: &Instruction) -> String {
    let (r, d, s, am) = inst.operands.register_direction_size_effective_address();
    if d == Direction::DstEa {
        format!("OR.{} D{}, {}", s, r, am)
    } else {
        format!("OR.{} {}, D{}", s, am, r)
    }
}

pub fn disassemble_ori(inst: &Instruction) -> String {
    let (s, am, imm) = inst.operands.size_effective_address_immediate();
    format!("ORI.{} #{}, {}", s, imm, am)
}

pub fn disassemble_oriccr(inst: &Instruction) -> String {
    let imm = inst.operands.immediate();
    format!("ORI {:#X}, CCR", imm)
}

pub fn disassemble_orisr(inst: &Instruction) -> String {
    let imm = inst.operands.immediate();
    format!("ORI {:#X}, SR", imm)
}

pub fn disassemble_pea(inst: &Instruction) -> String {
    let am = inst.operands.effective_address();
    format!("PEA {}", am)
}

pub fn disassemble_reset(_: &Instruction) -> String {
    "RESET".to_owned()
}

pub fn disassemble_rom(inst: &Instruction) -> String {
    let (d, am) = inst.operands.direction_effective_address();
    format!("RO{} {}", d, am)
}

pub fn disassemble_ror(inst: &Instruction) -> String {
    let (rot, d, s, ir, reg) = inst.operands.rotation_direction_size_mode_register();
    if ir {
        format!("RO{}.{} D{}, D{}", d, s, rot, reg)
    } else {
        let rot = if rot == 0 { 8 } else { rot };
        format!("RO{}.{} #{}, D{}", d, s, rot, reg)
    }
}

pub fn disassemble_roxm(inst: &Instruction) -> String {
    let (d, am) = inst.operands.direction_effective_address();
    format!("ROX{} {}", d, am)
}

pub fn disassemble_roxr(inst: &Instruction) -> String {
    let (rot, d, s, ir, reg) = inst.operands.rotation_direction_size_mode_register();
    if ir {
        format!("ROX{}.{} D{}, D{}", d, s, rot, reg)
    } else {
        let rot = if rot == 0 { 8 } else { rot };
        format!("ROX{}.{} #{}, D{}", d, s, rot, reg)
    }
}

pub fn disassemble_rte(_: &Instruction) -> String {
    "RTE".to_string()
}

pub fn disassemble_rtr(_: &Instruction) -> String {
    "RTR".to_string()
}

pub fn disassemble_rts(_: &Instruction) -> String {
    "RTS".to_string()
}

pub fn disassemble_sbcd(inst: &Instruction) -> String {
    let (ry, _, mode, rx) = inst.operands.register_size_mode_register();
    if mode == Direction::MemoryToMemory {
        format!("SBCD -(A{}), -(A{})", rx, ry)
    } else {
        format!("SBCD D{}, D{}", rx, ry)
    }
}

pub fn disassemble_scc(inst: &Instruction) -> String {
    let (cc, am) = inst.operands.condition_effective_address();
    format!("S{} {}", disassemble_conditional_test(cc), am)
}

pub fn disassemble_stop(inst: &Instruction) -> String {
    let imm = inst.operands.immediate();
    format!("STOP #{:#X}", imm)
}

pub fn disassemble_sub(inst: &Instruction) -> String {
    let (r, d, s, am) = inst.operands.register_direction_size_effective_address();
    if d == Direction::DstEa {
        format!("SUB.{} D{}, {}", s, r, am)
    } else {
        format!("SUB.{} {}, D{}", s, am, r)
    }
}

pub fn disassemble_suba(inst: &Instruction) -> String {
    let (r, s, am) = inst.operands.register_size_effective_address();
    format!("SUBA.{} {}, A{}", s, am, r)
}

pub fn disassemble_subi(inst: &Instruction) -> String {
    let (s, am, imm) = inst.operands.size_effective_address_immediate();
    format!("SUBI.{} #{}, {}", s, imm, am)
}

pub fn disassemble_subq(inst: &Instruction) -> String {
    let (d, s, am) = inst.operands.data_size_effective_address();
    let d = if d == 0 { 8 } else { d };
    format!("SUBQ.{} #{}, {}", s, d, am)
}

pub fn disassemble_subx(inst: &Instruction) -> String {
    let (ry, s, mode, rx) = inst.operands.register_size_mode_register();
    if mode == Direction::MemoryToMemory {
        format!("SUBX.{} -(A{}), -(A{})", s, rx, ry)
    } else {
        format!("SUBX.{} D{}, D{}", s, rx, ry)
    }
}

pub fn disassemble_swap(inst: &Instruction) -> String {
    let r = inst.operands.register();
    format!("SWAP D{}", r)
}

pub fn disassemble_tas(inst: &Instruction) -> String {
    let am = inst.operands.effective_address();
    format!("TAS {}", am)
}

pub fn disassemble_trap(inst: &Instruction) -> String {
    let v = inst.operands.vector();
    format!("TRAP #{}", v)
}

pub fn disassemble_trapv(_: &Instruction) -> String {
    "TRAPV".to_string()
}

pub fn disassemble_tst(inst: &Instruction) -> String {
    let (s, am) = inst.operands.size_effective_address();
    format!("TST.{} {}", s, am)
}

pub fn disassemble_unlk(inst: &Instruction) -> String {
    let r = inst.operands.register();
    format!("UNLK A{}", r)
}

/// Disassembler function Look-Up Table.
///
/// # Usage
///
/// ```
/// use m68000::decoder::DECODER;
/// use m68000::disassembler::DLUT;
/// use m68000::instruction::Instruction;
/// use m68000::memory_access::MemoryAccess;
///
/// let mut data: Vec<u8> = Vec::new();
/// data.resize(4, 0); // Load the binary in data.
/// let mut iter = data.iter_u16(0);
/// let inst = Instruction::from_memory(&mut iter).unwrap();
/// let disassemble = DLUT[DECODER[inst.opcode as usize] as usize];
/// println!("{:#X} {}", inst.pc, disassemble(&inst));
/// ```
pub const DLUT: [fn(&Instruction) -> String; Isa::_Size as usize] = [
    disassemble_unknown_instruction,
    disassemble_abcd,
    disassemble_add,
    disassemble_adda,
    disassemble_addi,
    disassemble_addq,
    disassemble_addx,
    disassemble_and,
    disassemble_andi,
    disassemble_andiccr,
    disassemble_andisr,
    disassemble_asm,
    disassemble_asr,
    disassemble_bcc,
    disassemble_bchg,
    disassemble_bclr,
    disassemble_bra,
    disassemble_bset,
    disassemble_bsr,
    disassemble_btst,
    disassemble_chk,
    disassemble_clr,
    disassemble_cmp,
    disassemble_cmpa,
    disassemble_cmpi,
    disassemble_cmpm,
    disassemble_dbcc,
    disassemble_divs,
    disassemble_divu,
    disassemble_eor,
    disassemble_eori,
    disassemble_eoriccr,
    disassemble_eorisr,
    disassemble_exg,
    disassemble_ext,
    disassemble_illegal,
    disassemble_jmp,
    disassemble_jsr,
    disassemble_lea,
    disassemble_link,
    disassemble_lsm,
    disassemble_lsr,
    disassemble_move,
    disassemble_movea,
    disassemble_moveccr,
    disassemble_movefsr,
    disassemble_movesr,
    disassemble_moveusp,
    disassemble_movem,
    disassemble_movep,
    disassemble_moveq,
    disassemble_muls,
    disassemble_mulu,
    disassemble_nbcd,
    disassemble_neg,
    disassemble_negx,
    disassemble_nop,
    disassemble_not,
    disassemble_or,
    disassemble_ori,
    disassemble_oriccr,
    disassemble_orisr,
    disassemble_pea,
    disassemble_reset,
    disassemble_rom,
    disassemble_ror,
    disassemble_roxm,
    disassemble_roxr,
    disassemble_rte,
    disassemble_rtr,
    disassemble_rts,
    disassemble_sbcd,
    disassemble_scc,
    disassemble_stop,
    disassemble_sub,
    disassemble_suba,
    disassemble_subi,
    disassemble_subq,
    disassemble_subx,
    disassemble_swap,
    disassemble_tas,
    disassemble_trap,
    disassemble_trapv,
    disassemble_tst,
    disassemble_unlk,
];