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
use self::super::super::isa::instruction::{InstructionLoadImmediateWideRegisterPair, InstructionMadrDirection, InstructionPortDirection,
                                           InstructionStckDirection, InstructionRegisterPair, Instruction};
use self::super::MicroOp;


const FLAG_REGISTER_ADDRESS: u8 = 0b000;
const S_REGISTER_ADDRESS: u8 = 0b001;
const X_REGISTER_ADDRESS: u8 = 0b010;
const Y_REGISTER_ADDRESS: u8 = 0b011;
const A_REGISTER_ADDRESS: u8 = 0b100;
const B_REGISTER_ADDRESS: u8 = 0b101;
const C_REGISTER_ADDRESS: u8 = 0b110;
const D_REGISTER_ADDRESS: u8 = 0b111;


/// `[MicroOp; N]` typedef, where `N` is *not* to be relied upon
///
/// Use this type for storing values returned by [`MicroOp::from_instruction()`](enum.MicroOp.html#fn.from_instruction) instead
/// of any fixed-size array, as the size of this is not part of the stable API and subject to change without notice.
pub type MicroOpBlock = [MicroOp; 6];

impl MicroOp {
    /// Get μOps corresponding to the given instruction
    ///
    /// The return type is `(ops, len)`, where `&ops[..len]` are the actual μOps and the rest is padding.
    /// This was done to reduce allocations.
    ///
    /// # Examples
    ///
    /// ```
    /// # use pir_8_emu::isa::instruction::Instruction;
    /// # use pir_8_emu::micro::MicroOp;
    /// let ops = MicroOp::from_instruction(Instruction::Move { qqq: 0b100, rrr: 0b101 });
    /// let ops = &ops.0[..ops.1];
    ///
    /// assert_eq!(ops, &[MicroOp::ReadRegister(0b100), MicroOp::WriteRegister(0b101)]);
    /// ```
    pub fn from_instruction(instr: Instruction) -> (MicroOpBlock, usize) {
        match instr {
            Instruction::Reserved(_) => {
                ([// forcebreak
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 6)
            }

            Instruction::LoadImmediateByte { rrr } => {
                ([// forcebreak
                  MicroOp::LoadImmediate,
                  MicroOp::WriteRegister(rrr),
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 2)
            }

            Instruction::LoadIndirect { rrr } => {
                ([// forcebreak
                  MicroOp::FetchAddress,
                  MicroOp::WriteRegister(rrr),
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 2)
            }

            Instruction::LoadImmediateWide { rr: InstructionLoadImmediateWideRegisterPair::Adr } => {
                ([// forcebreak
                  MicroOp::LoadImmediate,
                  MicroOp::LoadImmediate,
                  MicroOp::AdrWrite,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 3)
            }
            Instruction::LoadImmediateWide { rr } => {
                let [f, s] = imm_address_pair(rr);

                ([// forcebreak
                  MicroOp::LoadImmediate,
                  MicroOp::LoadImmediate,
                  MicroOp::WriteRegister(s),
                  MicroOp::WriteRegister(f),
                  MicroOp::Nop,
                  MicroOp::Nop],
                 4)
            }

            Instruction::Jump(cond) => {
                ([// forcebreak
                  MicroOp::ReadRegister(FLAG_REGISTER_ADDRESS),
                  MicroOp::CheckJumpCondition(cond),
                  MicroOp::Jump,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 3)
            }

            Instruction::Save { rrr } => {
                ([// forcebreak
                  MicroOp::ReadRegister(rrr),
                  MicroOp::WriteAddress,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 2)
            }

            Instruction::Alu(op) => {
                ([// forcebreak
                  MicroOp::ReadRegister(X_REGISTER_ADDRESS),
                  MicroOp::ReadRegister(Y_REGISTER_ADDRESS),
                  MicroOp::ReadRegister(FLAG_REGISTER_ADDRESS),
                  MicroOp::Alu(op),
                  MicroOp::WriteRegister(FLAG_REGISTER_ADDRESS),
                  MicroOp::WriteRegister(S_REGISTER_ADDRESS)],
                 6)
            }

            Instruction::Move { qqq, rrr } => {
                ([// forcebreak
                  MicroOp::ReadRegister(qqq),
                  MicroOp::WriteRegister(rrr),
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 2)
            }

            Instruction::Madr { d: InstructionMadrDirection::Write, r } => {
                let [f, s] = address_pair(r);

                ([// forcebreak
                  MicroOp::ReadRegister(f),
                  MicroOp::ReadRegister(s),
                  MicroOp::AdrWrite,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 3)
            }
            Instruction::Madr { d: InstructionMadrDirection::Read, r } => {
                let [f, s] = address_pair(r);

                ([// forcebreak
                  MicroOp::AdrRead,
                  MicroOp::WriteRegister(s),
                  MicroOp::WriteRegister(f),
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 3)
            }

            Instruction::Port { d: InstructionPortDirection::In, rrr } => {
                ([// forcebreak
                  MicroOp::ReadRegister(A_REGISTER_ADDRESS),
                  MicroOp::PortIn,
                  MicroOp::WriteRegister(rrr),
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 3)
            }
            Instruction::Port { d: InstructionPortDirection::Out, rrr } => {
                ([// forcebreak
                  MicroOp::ReadRegister(rrr),
                  MicroOp::ReadRegister(A_REGISTER_ADDRESS),
                  MicroOp::PortOut,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 3)
            }

            Instruction::Comp { rrr } => {
                ([// forcebreak
                  MicroOp::ReadRegister(S_REGISTER_ADDRESS),
                  MicroOp::ReadRegister(rrr),
                  MicroOp::ReadRegister(FLAG_REGISTER_ADDRESS),
                  MicroOp::Compare,
                  MicroOp::WriteRegister(FLAG_REGISTER_ADDRESS),
                  MicroOp::Nop],
                 5)
            }

            Instruction::Stck { d: InstructionStckDirection::Push, r } => {
                let [f, s] = address_pair(r);

                ([// forcebreak
                  MicroOp::ReadRegister(s),
                  MicroOp::StackPush,
                  MicroOp::ReadRegister(f),
                  MicroOp::StackPush,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 4)
            }
            Instruction::Stck { d: InstructionStckDirection::Pop, r } => {
                let [f, s] = address_pair(r);

                ([// forcebreak
                  MicroOp::StackPop,
                  MicroOp::WriteRegister(f),
                  MicroOp::StackPop,
                  MicroOp::WriteRegister(s),
                  MicroOp::Nop,
                  MicroOp::Nop],
                 4)
            }

            Instruction::Clrf => {
                ([// forcebreak
                  MicroOp::MakeImmediate(0),
                  MicroOp::WriteRegister(FLAG_REGISTER_ADDRESS),
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 2)
            }

            Instruction::Halt => {
                ([// forcebreak
                  MicroOp::Halt,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop,
                  MicroOp::Nop],
                 1)
            }
        }
    }
}

fn address_pair(r: InstructionRegisterPair) -> [u8; 2] {
    match r {
        InstructionRegisterPair::Ab => [A_REGISTER_ADDRESS, B_REGISTER_ADDRESS],
        InstructionRegisterPair::Cd => [C_REGISTER_ADDRESS, D_REGISTER_ADDRESS],
    }
}

fn imm_address_pair(r: InstructionLoadImmediateWideRegisterPair) -> [u8; 2] {
    match r {
        InstructionLoadImmediateWideRegisterPair::Ab => [A_REGISTER_ADDRESS, B_REGISTER_ADDRESS],
        InstructionLoadImmediateWideRegisterPair::Cd => [C_REGISTER_ADDRESS, D_REGISTER_ADDRESS],
        InstructionLoadImmediateWideRegisterPair::Xy => [X_REGISTER_ADDRESS, Y_REGISTER_ADDRESS],
        InstructionLoadImmediateWideRegisterPair::Adr => panic!("Wrong decomposition for LOAD IMM WIDE pair"),  // Covered by explicit switch above
    }
}