~maddie/custom-processor-simulator

ref: 16add4eee325406205b152194e0206613b729fec custom-processor-simulator/src/processor.rs -rw-r--r-- 20.1 KiB
16add4eeMadeline Cronin Implement tests for multiplication related behaviours. 2 months ago
                                                                                
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
use std::io;

pub struct Processor<'a> {
    reg: [u16;32],
    flags: [bool;2], //Flag A, then B
    itr_toggle: bool, //Interrupt toggle
    pub memory: &'a mut [u16], //Public since memory should be modifiable by the system.
    pub disk: &'a mut [u16],
    pub vram: &'a mut [u16],
    stdin_buf: Vec<u16>,
}

pub fn new<'a>(memory: &'a mut [u16], disk: &'a mut [u16], vram: &'a mut [u16]) -> Processor<'a> {
    Processor {
        reg: [0;32],
        flags: [false;2],
        itr_toggle: false,
        memory,
        disk,
        vram,
        stdin_buf: Vec::new(),
    }
}

impl <'a> Processor<'a> {
    ///Runs the processor through one instruction.
    ///Returns false for normal operation, and true to indicate that it should halt.
    pub fn run(&mut self) -> bool {
        let instruction: (u16,u16) = (self.memory[self.reg[31] as usize],self.memory[self.reg[31] as usize +1]);
        let conditions: ((bool,bool),(bool,bool)) = (( //.0.x is activations, .1.x is conditions
                (instruction.0 & 0x8000) != 0,
                (instruction.0 & 0x4000) != 0),(
                (instruction.0 & 0x2000) != 0,
                (instruction.0 & 0x1000) != 0)
        );
        if (conditions.0.0 && (conditions.1.0 != self.flags[0])) || (conditions.0.1 && (conditions.1.1 != self.flags[1])) {
            self.reg[31] += 2; //update the program counter at the end of instruction
                               //execution.
            return false; //if the conditions are not met, perform no further calculations.
        }

        match instruction.0 & 0x0F00 {
            0x0100 => {
                //Arithmetic operations.
                let toggles: (bool,bool,bool,bool) = ( //S,C,F,I, see ISA for meanings
                    (instruction.0 & 0x0080) != 0,
                    (instruction.0 & 0x0040) != 0,
                    (instruction.0 & 0x0020) != 0,
                    (instruction.0 & 0x0010) != 0,
                );
                let operands: (usize, usize, usize) = (
                    ((instruction.1 & 0xF800) >> 11) as usize,
                    ((instruction.1 & 0x07C0) >> 6) as usize,
                    ((instruction.1 & 0x003E) >> 1) as usize,
                );
                let mut pc_overwrite: Option<u16> = None;
                if operands.2 == 31 { //program counter is unwritable for non-control flow
                                      //instructions, but the overflow checks should still occur,
                                      //so hold onto the next value and replace it later.
                    pc_overwrite = Some(self.reg[31]);
                }

                match instruction.0 & 0x000F {
                    0x0000 => { //Addition
                        if toggles.1 { //If C=1, then extra logic is required to correctly identify
                                     //carries
                            (self.reg[operands.2], self.flags[toggles.2 as usize]) = {
                                if toggles.0 { //If dealing with signed values, cast the register
                                               //values before passing them to overflowing_add.
                                    let (uncast_return, flag) = (self.reg[operands.0] as i16).overflowing_add(self.reg[operands.1] as i16);
                                    (uncast_return as u16, flag)
                                }
                                else { //Otherwise just pass them straight to overflowing_add.
                                   self.reg[operands.0].overflowing_add(self.reg[operands.1])
                                }
                            };
                        }
                        else {self.reg[operands.2] = self.reg[operands.0].wrapping_add(self.reg[operands.1])};
                    }
                    0x0001 => { //Subtraction
                        if toggles.1 {
                            (self.reg[operands.2], self.flags[toggles.2 as usize]) = {
                                if toggles.0 {
                                    let (uncast_return, flag) = (self.reg[operands.0] as i16).overflowing_add(-(self.reg[operands.1] as i16));
                                    (uncast_return as u16, flag)
                                }
                                else {
                                    (self.reg[operands.0].wrapping_sub(self.reg[operands.1]), self.reg[operands.0] < self.reg[operands.1])
                                }
                            }
                        }
                        else {
                            self.reg[operands.2] = self.reg[operands.0].wrapping_sub(self.reg[operands.1]);
                        }
                    }
                    0x0002 => { //Multiplication
                        if toggles.1 {
                            (self.reg[operands.2], self.flags[toggles.2 as usize]) = {
                                if toggles.0 {
                                    let (uncast_return, flag) = (self.reg[operands.0] as i16).overflowing_mul(self.reg[operands.1] as i16);
                                    (uncast_return as u16, flag)
                                }
                                else {
                                    self.reg[operands.0].overflowing_mul(self.reg[operands.1])
                                }
                            }
                        }
                        else {
                            self.reg[operands.2] = self.reg[operands.0].wrapping_mul(self.reg[operands.1]);
                        }
                    }
                    0x0003 => { //Division
                        if self.reg[operands.2] == 0 && toggles.1 {self.flags[toggles.2 as usize] = true}
                        else if self.reg[operands.2] != 0 {
                            if toggles.0 {
                                self.reg[operands.2] = ((self.reg[operands.0] as i16)/(self.reg[operands.1] as i16)) as u16;
                            }
                            else {
                                self.reg[operands.2] = self.reg[operands.0]/self.reg[operands.1];
                            }
                        }
                    }
                    _ => return true
}
                if toggles.3 {self.reg[operands.2] = !self.reg[operands.2]};
                if let Some(addr) = pc_overwrite {self.reg[31] = addr};
                self.reg[31] += 2;
                return false
            }
            0x0200 => {
                let toggles: (bool,bool,bool,bool) = ( //Z,F,R,I, see ISA for meanings
                    (instruction.0 & 0x0080) != 0,
                    (instruction.0 & 0x0040) != 0,
                    (instruction.0 & 0x0020) != 0,
                    (instruction.0 & 0x0010) != 0,
                );
                let operands: (usize, usize, usize) = (
                    ((instruction.1 & 0xF800) >> 11) as usize,
                    ((instruction.1 & 0x07C0) >> 6) as usize,
                    ((instruction.1 & 0x003E) >> 1) as usize,
                );
                let mut pc_overwrite: Option<u16> = None;
                if operands.2 == 31 { //program counter is unwritable for non-control flow
                                      //instructions, but the non-zero checks should still occur,
                                      //so hold onto the next value and replace it later.
                    pc_overwrite = Some(self.reg[31]);
                }

                match instruction.0 & 0x000F {
                    0x0000 => {
                        self.reg[operands.2] = self.reg[operands.0] & self.reg[operands.1];
                    }
                    0x0001 => {
                        self.reg[operands.2] = self.reg[operands.0] | self.reg[operands.1];
                    }
                    0x0002 => {
                        self.reg[operands.2] = self.reg[operands.0] ^ self.reg[operands.1];
                    }
                    _ => return true
                }

                if toggles.3 {
                    self.reg[operands.2] = !self.reg[operands.2];
                };
                if toggles.0 {
                    self.flags[toggles.1 as usize] = (self.reg[operands.2] == 0) ^ toggles.2;
                }
                if let Some(addr) = pc_overwrite {
                    self.reg[31] = addr;
                }

                self.reg[31] += 2;
                return false
            }
            0x0300 => {
                let toggles: (bool, bool) = ( //Flag specifier, Invert
                    (instruction.0 & 0x0080) != 0,
                    (instruction.0 & 0x0040) != 0
                );
                let operands: (usize, usize) = (
                    ((instruction.1 & 0xF800) >> 11) as usize,
                    ((instruction.1 & 0x07C0) >> 6) as usize,
                );
                match instruction.0 & 0x0030 {
                    0x0000 => {
                        self.flags[toggles.0 as usize] = (self.reg[operands.0] == self.reg[operands.1]) ^ toggles.1;
                    }
                    0x0010 => {
                        self.flags[toggles.0 as usize] = (self.reg[operands.0] < self.reg[operands.1]) ^ toggles.1;
                    }
                    0x0020 => {
                        self.flags[toggles.0 as usize] = (self.reg[operands.0] > self.reg[operands.1]) ^ toggles.1;
                    }
                    _ => return true
                }
                self.reg[31] += 2;
                return false
            }
            0x0400 => { //Control flow
                let toggles: (bool,bool,bool,bool,bool,bool) = ( //J,S,P,I,TT, see ISA for meanings
                    (instruction.0 & 0x0080) != 0,
                    (instruction.0 & 0x0040) != 0,
                    (instruction.0 & 0x0020) != 0,
                    (instruction.0 & 0x0010) != 0,
                    (instruction.0 & 0x0008) != 0,
                    (instruction.0 & 0x0004) != 0,
                );
                
                if self.itr_toggle && toggles.3 { //Software interrupt
                    if toggles.1 {   //Push what would have been the next address to stack
                        self.memory[self.reg[30] as usize] = self.reg[31] + 2;
                        self.reg[30] -= 1;
                    }                 
                    self.reg[31] = 0; //Jump to zero
                    if toggles.4 { //Set the interrupt toggle
                        self.itr_toggle = toggles.5;
                    }

                    return false
                }
                let address = {
                    if toggles.2 { //Pop from the stack
                        self.reg[30] += 1;
                        self.memory[self.reg[30] as usize]
                    }
                    else { //Use a register value
                        let operand: usize = ((instruction.1 & 0xF800) >> 11) as usize;
                        self.reg[operand]
                    }
                };

                if toggles.1 { //Push to stack
                    self.memory[self.reg[30] as usize] = self.reg[31] + 2;
                    self.reg[30] -= 1;
                }
                if toggles.0 { //Relative jump
                    self.reg[31] = self.reg[31].wrapping_add(address);
                }
                else { //Static jump
                    self.reg[31] = address;
                }

                if toggles.4 { //Set the interrupt enable bit
                    self.itr_toggle = toggles.5; 
                }
                return false
            }
            0x0500 => { //Memory Access
                let rw_toggle: bool = (instruction.0 & 0x0080) != 0; //False for read, true for
                                                                     //write
                let access_type: u16 = (instruction.0 & 0x0070) >> 4;

                let operands: (usize, usize) = (
                    ((instruction.1 & 0xF800) >> 11) as usize,
                    ((instruction.1 & 0x07C0) >> 6) as usize,
                );
                if rw_toggle {
                    match access_type {
                        0 => { //Stack
                            self.memory[self.reg[30] as usize] = self.reg[operands.0];
                            self.reg[30] -= 1;
                        }
                        1 => { //Main Memory
                            self.memory[self.reg[operands.1] as usize] = self.reg[operands.0];
                        }
                        2 => { //Disk
                            self.disk[self.reg[operands.1] as usize + ((self.reg[29] as usize) << 16)] = self.reg[operands.0];
                        }
                        3 => { //VRAM
                            self.vram[self.reg[operands.1] as usize] = self.reg[operands.0];
                        }
                        4 => { //Serial port
                            print!("{}", self.reg[operands.0] as u8 as char);
                        }
                        _ => return true
                    }
                }
                else {
                    if operands.0 == 31 { //Skip the operation if writing to program counter, as
                                         //program counter is inaccessible for non-control flow
                                         //instructions
                        self.reg[31] += 2;
                        return false
                    }
                    match access_type {
                        0 => { //Stack
                            self.reg[30] += 1;
                            self.reg[operands.0] = self.memory[self.reg[30] as usize];
                        }
                        1 => self.reg[operands.0] = self.memory[self.reg[operands.1] as usize], //Main memory
                        2 => self.reg[operands.0] = self.disk[self.reg[operands.1] as usize + ((self.reg[29] as usize) << 16)], //Disk
                        3 => self.reg[operands.0] = self.vram[self.reg[operands.1] as usize], //VRAM
                        4 => { //Serial port
                            if self.stdin_buf == Vec::new() { //Fill stdin_buf if empty
                                let mut buffer = String::new();
                                io::stdin().read_line(&mut buffer).expect("STDIN read failure");
                                for byte in buffer.as_bytes().iter().rev() { //Reversed so values
                                                                             //can be simply popped
                                                                             //from the vec
                                    self.stdin_buf.push(*byte as u16);
                                }
                            }
                            self.reg[operands.0] = self.stdin_buf.pop().unwrap_or(0); //If stdin is
                                                                                      //empty, pass
                                                                                      //in 0
                        }
                        _ => return true
                    }
                }
                self.reg[31] += 2;
                return false
            }
            0x0600 => {
                let operand = ((instruction.0 & 0x00F8) >> 3) as usize;
                if operand != 31 {
                    self.reg[operand] = instruction.1;
                }
                self.reg[31] += 2;
                return false
            }
            _ => return true,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn immediate() {
        let memory: &mut [u16] = &mut [0x0600, 45, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[0], 45);
    }
    #[test]
    fn conditional() {
        let memory: &mut [u16] = &mut [0x0600, 17, 0xF600, 69, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[0], 17);
    }

    #[test]
    fn add() {
        let memory: &mut [u16] = &mut [0x0600, 3, 0x0608, 4, 0x0100, 0x0044, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], 7);
    }
    #[test]
    fn overflow () {
        let memory: &mut [u16] = &mut [0x0600, u16::MAX, 0x0608, 5, 0x0140, 0x0044, 0xA618, 5, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], 4);
        assert_eq!(processor.reg[3], 5);
    }
    #[test]
    fn signed_overflow () {
        let memory: &mut [u16] = &mut [0x0600, i16::MAX as u16, 0x0608, 5, 0x01E0, 0x0044, 0x5618, 5, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], i16::MIN as u16 + 4);
        assert_eq!(processor.reg[3], 5);
    }
    #[test]
    fn arithmetic_invert () {
        let memory: &mut [u16] = &mut [0x0600, 3, 0x0608, 4, 0x0110, 0x0044, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], !7);
    }
    #[test]
    fn sub () {
        let memory: &mut [u16] = &mut [0x0600, 5, 0x0608, 3, 0x0101, 0x0044, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], 2);
    }
    #[test]
    fn underflow () {
        let memory: &mut [u16] = &mut [0x0600, 3, 0x0608, 5, 0x0141, 0x0044, 0xA618, 7, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], u16::MAX - 1);
        assert_eq!(processor.reg[3], 7);
    }
    #[test]
    fn signed_underflow () {
        let memory: &mut [u16] = &mut [0x0600, 3, 0x0608, 5, 0x01C1, 0x0044, 0x0600, i16::MIN as u16, 0x01E1, 0x0046, 0xD620, 7, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], -2i16 as u16);
        assert_eq!(processor.reg[3], i16::MAX as u16 - 4);
        assert_eq!(processor.reg[4], 7);
    }
    #[test]
    fn mult () {
        let memory: &mut [u16] = &mut [0x0600, 3, 0x0608, 5, 0x0102, 0x0044, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], 15);
    }
    #[test]
    fn mult_overflow () {
        let memory: &mut [u16] = &mut [0x0600, 3, 0x0608, u16::MAX, 0x0142, 0x0044, 0xA618, 5, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], u16::MAX - 2);
        assert_eq!(processor.reg[3], 5);
    }
    #[test]
    fn mult_signed_overflow () {
        let memory: &mut [u16] = &mut [0x0600, 2, 0x0608, i16::MAX as u16, 0x01C2, 0x0044, 0xA618, 5, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], u16::MAX - 1);
        assert_eq!(processor.reg[3], 5);
    }
}