m68000 0.1.0

A Motorola 68000 interpreter, disassembler and assembler (code emitter)
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
//! Addressing mode-related structs, enums and functions.

use crate::M68000;
use crate::execution_times as EXEC;
use crate::memory_access::{MemoryAccess, MemoryIter};
use crate::instruction::Size;
use crate::utils::bits;

/// Addressing modes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AddressingMode {
    /// Data Register Direct.
    Drd(u8),
    /// Address Register Direct.
    Ard(u8),
    /// Address Register Indirect.
    Ari(u8),
    /// Address Register Indirect With POstincrement.
    Ariwpo(u8),
    /// Address Register Indirect With PRedecrement.
    Ariwpr(u8),
    /// Address Register Indirect With Displacement (address reg, displacement).
    Ariwd(u8, i16),
    /// Address Register Indirect With Index 8 (address reg, brief extension word).
    Ariwi8(u8, BriefExtensionWord),
    /// Absolute Short.
    AbsShort(u16),
    /// Absolute Long.
    AbsLong(u32),
    /// Program Counter Indirect With Displacement (PC value, displacement).
    ///
    /// When using it with the assembler, the PC value is ignored.
    Pciwd(u32, i16),
    /// Program Counter Indirect With Index 8 (PC value, brief extension word).
    ///
    /// When using it with the assembler, the PC value is ignored.
    Pciwi8(u32, BriefExtensionWord),
    /// Immediate Data (cast this variant to the correct type when used).
    Immediate(u32),
}

impl AddressingMode {
    /// New addressing mode.
    pub fn new(mode: u16, reg: u8, size: Option<Size>, memory: &mut MemoryIter) -> Self {
        match mode {
            0 => Self::Drd(reg),
            1 => Self::Ard(reg),
            2 => Self::Ari(reg),
            3 => Self::Ariwpo(reg),
            4 => Self::Ariwpr(reg),
            5 => Self::Ariwd(reg, memory.next().unwrap().expect("An Access Error occured in Ariwd.") as i16),
            6 => Self::Ariwi8(reg, BriefExtensionWord(memory.next().unwrap().expect("An Access Error occured in Ariwi8."))),
            7 => match reg {
                0 => Self::AbsShort(memory.next().unwrap().expect("An Access Error occured in AbsShort.")),
                1 => {
                    let high = (memory.next().unwrap().expect("An Access Error occured in AbsLong high.") as u32) << 16;
                    let low = memory.next().unwrap().expect("An Access Error occured in AbsLong low.") as u32;
                    Self::AbsLong(high | low)
                },
                2 => Self::Pciwd(memory.next_addr, memory.next().unwrap().expect("An Access Error occured in Pciwd.") as i16),
                3 => Self::Pciwi8(memory.next_addr, BriefExtensionWord(memory.next().unwrap().expect("An Access Error occured in Pciwi8."))),
                4 => {
                    if size.unwrap().is_long() {
                        let high = (memory.next().unwrap().expect("An Access Error occured in Immediate high.") as u32) << 16;
                        let low = memory.next().unwrap().expect("An Access Error occured in Immediate low.") as u32;
                        Self::Immediate(high | low)
                    } else {
                        let low = memory.next().unwrap().expect("An Access Error occured in Immediate.") as u32;
                        Self::Immediate(low as u32)
                    }
                },
                _ => panic!("[AddressingMode::new] Wrong register {}", reg),
            },
            _ => panic!("[AddressingMode::new] Wrong mode {}", mode),
        }
    }

    /// Fast new addressing mode.
    pub fn new_fast(mode: u16, reg: u8, size: Option<Size>, m68000: &mut M68000, memory: &mut impl MemoryAccess) -> Self {
        match mode {
            0 => Self::Drd(reg),
            1 => Self::Ard(reg),
            2 => Self::Ari(reg),
            3 => Self::Ariwpo(reg),
            4 => Self::Ariwpr(reg),
            5 => Self::Ariwd(reg, m68000.get_next_word(memory).unwrap() as i16),
            6 => Self::Ariwi8(reg, BriefExtensionWord(m68000.get_next_word(memory).unwrap())),
            7 => match reg {
                0 => Self::AbsShort(m68000.get_next_word(memory).unwrap()),
                1 => Self::AbsLong(m68000.get_next_long(memory).unwrap()),
                2 => Self::Pciwd(m68000.regs.pc, m68000.get_next_word(memory).unwrap() as i16),
                3 => Self::Pciwi8(m68000.regs.pc, BriefExtensionWord(m68000.get_next_word(memory).unwrap())),
                4 => {
                    if size.unwrap().is_long() {
                        Self::Immediate(m68000.get_next_long(memory).unwrap())
                    } else {
                        Self::Immediate(m68000.get_next_word(memory).unwrap() as u32)
                    }
                },
                _ => panic!("[AddressingMode::new_fast] Wrong register {}", reg),
            },
            _ => panic!("[AddressingMode::new_fast] Wrong mode {}", mode),
        }
    }

    /// Return the register of the addressing mode, or None if the mode has no associated register.
    #[inline(always)]
    pub const fn register(self) -> Option<u8> {
        match self {
            AddressingMode::Drd(reg) => Some(reg),
            AddressingMode::Ard(reg) => Some(reg),
            AddressingMode::Ari(reg) => Some(reg),
            AddressingMode::Ariwpo(reg) => Some(reg),
            AddressingMode::Ariwpr(reg) => Some(reg),
            AddressingMode::Ariwd(reg, _)  => Some(reg),
            AddressingMode::Ariwi8(reg, _) => Some(reg),
            _ => None,
        }
    }

    /// Returns true if `self` is `Drd`, false otherwise.
    #[inline(always)]
    pub const fn is_drd(self) -> bool {
        match self {
            Self::Drd(_) => true,
            _ => false,
        }
    }

    /// Returns true if `self` is `Ard`, false otherwise.
    #[inline(always)]
    pub const fn is_ard(self) -> bool {
        match self {
            Self::Ard(_) => true,
            _ => false,
        }
    }

    /// Returns true if `self` is `Drd` or `Ard`, false otherwise.
    #[inline(always)]
    pub const fn is_dard(self) -> bool {
        match self {
            Self::Drd(_) => true,
            Self::Ard(_) => true,
            _ => false,
        }
    }

    /// Returns true if `self` is `Ariwpo`, false otherwise.
    #[inline(always)]
    pub const fn is_ariwpo(self) -> bool {
        match self {
            Self::Ariwpo(_) => true,
            _ => false,
        }
    }

    /// Returns true if `self` is `Ariwpr`, false otherwise.
    #[inline(always)]
    pub const fn is_ariwpr(self) -> bool {
        match self {
            Self::Ariwpr(_) => true,
            _ => false,
        }
    }

    /// Returns true if `self` is `Immediate`, false otherwise.
    #[inline(always)]
    pub const fn is_immediate(self) -> bool {
        match self {
            Self::Immediate(_) => true,
            _ => false,
        }
    }

    /// Assembles `self` as an opcode effective address field.
    ///
    /// Set `long` to true if the immediate operand is long, false for byte and word sizes.
    ///
    /// Left contains the mode and register encoded as in the low 6 bits of the opcode.
    /// Right contains the extension words.
    pub fn assemble(self, long: bool) -> (u16, Box<[u16]>) {
        match self {
            AddressingMode::Drd(reg) => (reg as u16, Box::new([])),
            AddressingMode::Ard(reg) => (1 << 3 | reg as u16, Box::new([])),
            AddressingMode::Ari(reg) => (2 << 3 | reg as u16, Box::new([])),
            AddressingMode::Ariwpo(reg) => (3 << 3 | reg as u16, Box::new([])),
            AddressingMode::Ariwpr(reg) => (4 << 3 | reg as u16, Box::new([])),
            AddressingMode::Ariwd(reg, disp) => (5 << 3 | reg as u16, Box::new([disp as u16])),
            AddressingMode::Ariwi8(reg, bew) => (6 << 3 | reg as u16, Box::new([bew.0])),
            AddressingMode::AbsShort(addr) => (7 << 3, Box::new([addr])),
            AddressingMode::AbsLong(addr) => (7 << 3 | 1, Box::new([(addr >> 16) as u16, addr as u16])),
            AddressingMode::Pciwd(_, disp) => (7 << 3 | 2, Box::new([disp as u16])),
            AddressingMode::Pciwi8(_, bew) => (7 << 3 | 3, Box::new([bew.0])),
            AddressingMode::Immediate(imm) => {
                if long {
                    (7 << 3 | 4, Box::new([(imm >> 16) as u16, imm as u16]))
                } else {
                    (7 << 3 | 4, Box::new([imm as u16]))
                }
            },
        }
    }

    /// Assembles `self` as an opcode effective address field for MOVE or MOVEA destination field.
    ///
    /// Set `long` to true if the immediate operand is long, false for byte and word sizes.
    ///
    /// Left contains the mode and register encoded as in the destination (bits 6 to 11).
    /// Right contains the extension words.
    pub fn assemble_move_dst(self) -> (u16, Box<[u16]>) {
        match self {
            AddressingMode::Drd(reg) => ((reg as u16) << 9, Box::new([])),
            AddressingMode::Ard(reg) => ((reg as u16) << 9 | 1 << 6, Box::new([])),
            AddressingMode::Ari(reg) => ((reg as u16) << 9 | 2 << 6, Box::new([])),
            AddressingMode::Ariwpo(reg) => ((reg as u16) << 9 | 3 << 6, Box::new([])),
            AddressingMode::Ariwpr(reg) => ((reg as u16) << 9 | 4 << 6, Box::new([])),
            AddressingMode::Ariwd(reg, disp) => ((reg as u16) << 9 | 5 << 6, Box::new([disp as u16])),
            AddressingMode::Ariwi8(reg, bew) => ((reg as u16) << 9 | 6 << 6, Box::new([bew.0])),
            AddressingMode::AbsShort(addr) => (7 << 6, Box::new([addr])),
            AddressingMode::AbsLong(addr) => (1 << 9 | 7 << 6, Box::new([(addr >> 16) as u16, addr as u16])),
            _ => panic!("{self:?} mode cannot be used as a destination mode."),
        }
    }

    /// Verifies that `self` is one of the given modes.
    ///
    /// `regs` are the valid Mode 7 registers.
    pub fn verify(self, modes: &[u8], regs: &[u8]) -> bool {
        match self {
            AddressingMode::Drd(reg) => reg <= 7 && modes.contains(&0),
            AddressingMode::Ard(reg) => reg <= 7 && modes.contains(&1),
            AddressingMode::Ari(reg) => reg <= 7 && modes.contains(&2),
            AddressingMode::Ariwpo(reg) => reg <= 7 && modes.contains(&3),
            AddressingMode::Ariwpr(reg) => reg <= 7 && modes.contains(&4),
            AddressingMode::Ariwd(reg, _) => reg <= 7 && modes.contains(&5),
            AddressingMode::Ariwi8(reg,_) => reg <= 7 && modes.contains(&6),
            AddressingMode::AbsShort(_) => modes.contains(&7) && regs.contains(&0),
            AddressingMode::AbsLong(_) => modes.contains(&7) && regs.contains(&1),
            AddressingMode::Pciwd(_, _) => modes.contains(&7) && regs.contains(&2),
            AddressingMode::Pciwi8(_, _) => modes.contains(&7) && regs.contains(&3),
            AddressingMode::Immediate(_) => modes.contains(&7) && regs.contains(&4),
        }
    }
}

impl std::fmt::Display for AddressingMode {
    /// Disassembles the addressing mode.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AddressingMode::Drd(reg) => write!(f, "D{}", reg),
            AddressingMode::Ard(reg) => write!(f, "A{}", reg),
            AddressingMode::Ari(reg) => write!(f, "(A{})", reg),
            AddressingMode::Ariwpo(reg) => write!(f, "(A{})+", reg),
            AddressingMode::Ariwpr(reg) => write!(f, "-(A{})", reg),
            AddressingMode::Ariwd(reg, disp) => write!(f, "({}, A{})", disp, reg),
            AddressingMode::Ariwi8(reg, bew) => write!(f, "({}, A{}, {})", bew.disp(), reg, bew),
            AddressingMode::AbsShort(addr) => write!(f, "({:#X}).W", addr),
            AddressingMode::AbsLong(addr) => write!(f, "({:#X}).L", addr),
            AddressingMode::Pciwd(_, disp) => write!(f, "({}, PC)", disp),
            AddressingMode::Pciwi8(_, bew) => write!(f, "({}, PC, {})", bew.disp(), bew),
            AddressingMode::Immediate(imm) => write!(f, "#{}", imm),
        }
    }
}

impl std::fmt::UpperHex for AddressingMode {
    /// Same as Display but with the mode 7 immediate value written in upper hex format.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AddressingMode::Immediate(imm) => write!(f, "#{:#X}", imm),
            _ => std::fmt::Display::fmt(self, f),
        }
    }
}

/// Raw Brief Extension Word.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BriefExtensionWord(u16);

impl BriefExtensionWord {
    /// Returns the displacement associated with the brief extension word.
    pub const fn disp(self) -> i8 {
        self.0 as i8
    }

    /// Creates a new brief extension word, to be used when using the assembler.
    ///
    /// - `address`: true if the index register is an address register, false for a data register.
    /// - `reg`: the register number.
    /// - `long`: true if long size, false for word size.
    /// - `disp:`: the associated displacement value.
    pub const fn new(address: bool, reg: u8, long: bool, disp: i8) -> Self {
        let a = (address as u16) << 15;
        let r = (reg as u16 & 0x7) << 12;
        let s = (long as u16) << 11;
        let d = disp as u8 as u16;
        Self(a | r | s | d)
    }
}

impl std::fmt::Display for BriefExtensionWord {
    /// Disassembles the index register field of a brief extension word.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let x = if self.0 & 0x8000 != 0 { "A" } else { "D" };
        let reg = bits(self.0, 12, 14);
        let size = if self.0 & 0x0800 != 0 { "L" } else { "W" };
        write!(f, "{}{}.{}", x, reg, size)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct EffectiveAddress {
    /// The addressing mode.
    pub mode: AddressingMode,
    /// Where this effective address points to. `None` if the address has not been calculated yet.
    pub address: Option<u32>,
    /// The size of the data.
    pub size: Option<Size>,
}

impl EffectiveAddress {
    pub fn new(am: AddressingMode, size: Option<Size>) -> Self {
        Self {
            mode: am,
            address: None,
            size,
        }
    }
}

impl M68000 {
    /// Calculates the value of the given effective address.
    ///
    /// If the address has already been calculated (`ea.address` is Some), it is returned and no computation is performed.
    /// Otherwise the address is computed and assigned to `ea.address` and returned, or panic if the addressing mode is not in memory.
    pub(super) fn get_effective_address(&mut self, ea: &mut EffectiveAddress, exec_time: &mut usize) -> u32 {
        if ea.address.is_none() {
            ea.address = match ea.mode {
                AddressingMode::Ari(reg) => {
                    *exec_time += EXEC::EA_ARI;
                    Some(self.a(reg))
                },
                AddressingMode::Ariwpo(reg) => {
                    *exec_time += EXEC::EA_ARIWPO;
                    Some(self.ariwpo(reg, ea.size.expect("ariwpo must have a size")))
                },
                AddressingMode::Ariwpr(reg) => {
                    *exec_time += EXEC::EA_ARIWPR;
                    Some(self.ariwpr(reg, ea.size.expect("ariwpr must have a size")))
                },
                AddressingMode::Ariwd(reg, disp)  => {
                    *exec_time += EXEC::EA_ARIWD;
                    Some(self.a(reg) + disp as u32)
                },
                AddressingMode::Ariwi8(reg, bew) => {
                    *exec_time += EXEC::EA_ARIWI8;
                    Some(self.a(reg) + bew.disp() as u32 + self.get_index_register(bew))
                },
                AddressingMode::AbsShort(addr) => {
                    *exec_time += EXEC::EA_ABSSHORT;
                    Some(addr as i16 as u32)
                },
                AddressingMode::AbsLong(addr) => {
                    *exec_time += EXEC::EA_ABSLONG;
                    Some(addr)
                },
                AddressingMode::Pciwd(pc, disp) => {
                    *exec_time += EXEC::EA_PCIWD;
                    Some(pc + disp as u32)
                },
                AddressingMode::Pciwi8(pc, bew) => {
                    *exec_time += EXEC::EA_PCIWI8;
                    Some(pc + bew.disp() as u32 + self.get_index_register(bew))
                },
                _ => None,
            };
        }

        ea.address.expect("[get_effective_address] Trying to read effective address of a value not in memory.")
    }

    const fn get_index_register(&self, bew: BriefExtensionWord) -> u32 {
        let reg = bits(bew.0, 12, 14) as u8;

        if bew.0 & 0x8000 != 0 { // Address register
            if bew.0 & 0x0800 != 0 { // Long
                self.a(reg)
            } else { // Word
                self.a(reg) as i16 as u32
            }
        } else { // Data register
            if bew.0 & 0x0800 != 0 { // Long
                self.regs.d[reg as usize]
            } else { // Word
                self.regs.d[reg as usize] as i16 as u32
            }
        }
    }

    /// Address Register Indirect With POstincrement
    pub(super) fn ariwpo(&mut self, reg: u8, size: Size) -> u32 {
        let areg = self.a_mut(reg);
        let addr = *areg;
        *areg += if reg == 7 { size.as_word_long() } else { size } as u32;
        addr
    }

    /// Address Register Indirect With PRedecrement
    pub(super) fn ariwpr(&mut self, reg: u8, size: Size) -> u32 {
        let areg = self.a_mut(reg);
        *areg -= if reg == 7 { size.as_word_long() } else { size } as u32;
        *areg
    }
}