summaryrefslogtreecommitdiff
path: root/src/core.rs
blob: a05d75e5fc989246bfdc273816dd479790c49eae (plain)
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
use crate::ram::Ram;
use std::fmt;

#[derive(Debug, Clone)]
pub enum Register {
    R0,
    R1,
    R2,
    R3,
    R4,
    R5,
    R6,
    R7,
}

#[derive(Debug, Clone)]
pub enum Operand {
    Acc,
    Direct(u8),
    Indirect(Register),
    Data(u8),
    Register(Register),
    Reladdr(i8),
}

#[derive(Debug, Clone)]
pub struct Op {
    opcode: Isa8051Opcode,
    size: usize,
    operand1: Option<Operand>,
    operand2: Option<Operand>,
}

const OPCODE_NOP: u8 = 0x00;
const OPCODE_MOV_A_DATA: u8 = 0x74;
const OPCODE_MOV_DIRECT_DATA: u8 = 0x75;
const OPCODE_MOV_A_DIRECT: u8 = 0xE5;
const OPCODE_MOV_A_INDIRECT_R0: u8 = 0xE6;
const OPCODE_MOV_A_INDIRECT_R1: u8 = 0xE7;
const OPCODE_MOV_A_R0: u8 = 0xE8;
const OPCODE_MOV_A_R1: u8 = 0xE9;
const OPCODE_MOV_A_R2: u8 = 0xEA;
const OPCODE_MOV_A_R3: u8 = 0xEB;
const OPCODE_MOV_A_R4: u8 = 0xEC;
const OPCODE_MOV_A_R5: u8 = 0xED;
const OPCODE_MOV_A_R6: u8 = 0xEE;
const OPCODE_MOV_A_R7: u8 = 0xEF;
const OPCODE_PUSH: u8 = 0xC0;
const OPCODE_POP: u8 = 0xD0;
const OPCODE_SJMP: u8 = 0xD2;

impl fmt::Display for Operand {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Operand::Acc => format!("A"),
                Operand::Direct(dir) => format!("{:x}h", dir),
                Operand::Indirect(r) => format!("@{:?}", r),
                Operand::Data(data) => format!("#{}", data),
                Operand::Register(r) => format!("{:?}", r),
                Operand::Reladdr(rel) => format!("{}", rel),
            }
        )
    }
}

impl fmt::Display for Op {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut output = String::new();
        output.push_str(&format!("{} ", self.opcode));
        if let Some(op1) = &self.operand1 {
            output.push_str(&format!("{}", op1));
        }
        if let Some(op2) = &self.operand2 {
            output.push_str(&format!(",{}", op2));
        }
        write!(f, "{}", output)
    }
}

impl Operand {
    fn size(&self) -> usize {
        match self {
            Operand::Acc => 0,
            Operand::Direct(_) => 1,
            Operand::Indirect(_) => 0,
            Operand::Data(_) => 1,
            Operand::Register(_) => 0,
            Operand::Reladdr(_) => 1,
        }
    }
}

pub struct Core {
    pc: u16,
    ram: Ram,
}

impl Core {
    ///
    /// Constructor
    ///
    pub fn new_with_ram_from_hex(hex: String) -> Result<Self, String> {
        let ram = match Ram::from_hex(hex) {
            Ok(value) => value,
            Err(err_string) => return Err(err_string),
        };
        Ok(Self { pc: 0, ram })
    }

    ///
    /// Fetch and execute one instruction
    ///
    pub fn step(&mut self) -> u16 {
        let op = self.fetch();
        self.exec(op);
        self.pc
    }

    ///
    /// Get current instruction
    ///
    pub fn op(&self) -> Op {
        let op = Isa8051Op::from_u8(self.ram.array[self.pc as usize]);
        let operand1 = if let Some(operand) = op.operand1 {
            Some(self.map_operand(operand, 1))
        } else {
            None
        };
        let operand2_offset = match &operand1 {
            Some(o) => o.size(),
            _ => 0,
        };
        let operand2 = if let Some(operand) = op.operand2 {
            Some(self.map_operand(operand, operand2_offset))
        } else {
            None
        };
        Op {
            opcode: op.opcode,
            size: op.size,
            operand1,
            operand2,
        }
    }

    ///
    /// Increment PC
    /// Return the whole operation
    ///
    fn fetch(&mut self) -> Op {
        let op = self.op();
        self.pc += op.size as u16;
        return op;
    }

    ///
    /// Execute one instruction without incrementing PC
    ///
    fn exec(&mut self, op: Op) {
        match op.opcode {
            Isa8051Opcode::Nop => (),
            Isa8051Opcode::Mov => (),
            Isa8051Opcode::Illegal => (),
            Isa8051Opcode::Push => (),
            Isa8051Opcode::Pop => (),
            Isa8051Opcode::Sjmp => {
                if let Some(operand) = op.operand1 {
                    match operand {
                        Operand::Reladdr(reladdr) => self.sjmp(reladdr),
                        _ => panic!("SJMP got incompatible operand"),
                    }
                } else {
                    panic!("SJMP has no operand given")
                }
            }
        }
    }

    fn sjmp(&mut self, reladdr: i8) {
        self.pc = (self.pc as i16 + reladdr as i16) as u16;
    }

    fn map_operand(&self, operand: Isa8051Operand, offset: usize) -> Operand {
        match operand {
            Isa8051Operand::Acc => Operand::Acc,
            Isa8051Operand::Direct => Operand::Direct(self.ram.array[(self.pc as usize + offset)]),
            Isa8051Operand::Data => Operand::Data(self.ram.array[(self.pc as usize + offset)]),
            Isa8051Operand::Indirect(r) => Operand::Indirect(r),
            Isa8051Operand::Register(r) => Operand::Register(r),
            Isa8051Operand::Reladdr => {
                Operand::Reladdr(self.ram.array[(self.pc as usize + offset)] as i8)
            }
        }
    }
}

#[derive(Debug, Clone)]
pub enum Isa8051Operand {
    Acc,
    Direct,
    Data,
    Indirect(Register),
    Register(Register),
    Reladdr,
}

#[derive(Debug, Clone)]
pub enum Isa8051Opcode {
    Nop,
    Mov,
    Push,
    Pop,
    Illegal,
    Sjmp,
}

impl fmt::Display for Isa8051Opcode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[derive(Debug, Clone)]
pub struct Isa8051Op {
    opcode: Isa8051Opcode,
    size: usize,
    operand1: Option<Isa8051Operand>,
    operand2: Option<Isa8051Operand>,
}

impl Isa8051Op {
    fn from_u8(data: u8) -> Self {
        match data {
            OPCODE_NOP => Isa8051Op {
                opcode: Isa8051Opcode::Nop,
                size: 1,
                operand1: None,
                operand2: None,
            },
            OPCODE_MOV_A_DATA => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 2,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Data),
            },
            OPCODE_MOV_DIRECT_DATA => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 3,
                operand1: Some(Isa8051Operand::Direct),
                operand2: Some(Isa8051Operand::Data),
            },
            OPCODE_MOV_A_DIRECT => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 2,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Direct),
            },
            OPCODE_MOV_A_INDIRECT_R0 => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 1,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Indirect(Register::R0)),
            },
            OPCODE_MOV_A_INDIRECT_R1 => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 1,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Indirect(Register::R1)),
            },
            OPCODE_MOV_A_R0 => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 1,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Register(Register::R0)),
            },
            OPCODE_MOV_A_R1 => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 1,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Register(Register::R1)),
            },
            OPCODE_MOV_A_R2 => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 1,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Register(Register::R2)),
            },
            OPCODE_MOV_A_R3 => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 1,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Register(Register::R3)),
            },
            OPCODE_MOV_A_R4 => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 1,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Register(Register::R4)),
            },
            OPCODE_MOV_A_R5 => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 1,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Register(Register::R5)),
            },
            OPCODE_MOV_A_R6 => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 1,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Register(Register::R6)),
            },
            OPCODE_MOV_A_R7 => Isa8051Op {
                opcode: Isa8051Opcode::Mov,
                size: 1,
                operand1: Some(Isa8051Operand::Acc),
                operand2: Some(Isa8051Operand::Register(Register::R7)),
            },
            OPCODE_PUSH => Isa8051Op {
                opcode: Isa8051Opcode::Push,
                size: 2,
                operand1: Some(Isa8051Operand::Direct),
                operand2: None,
            },
            OPCODE_POP => Isa8051Op {
                opcode: Isa8051Opcode::Pop,
                size: 2,
                operand1: Some(Isa8051Operand::Direct),
                operand2: None,
            },
            OPCODE_SJMP => Isa8051Op {
                opcode: Isa8051Opcode::Sjmp,
                size: 2,
                operand1: Some(Isa8051Operand::Reladdr),
                operand2: None,
            },
            _ => Isa8051Op {
                opcode: Isa8051Opcode::Illegal,
                size: 1,
                operand1: None,
                operand2: None,
            },
        }
    }
}