gpcas_forwardcom 0.2.0

ForwardCom instruction set architecture (ISA) properties for use with the General Purpose Core Architecture Simulator (GPCAS).
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
// Filename: emulator.rs
// Author:	 Kai Rese
// Version:	 0.25
// Date:	 30-01-2023 (DD-MM-YYYY)
// Library:  gpcas_forwardcom
//
// Copyright (c) 2022 Kai Rese
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <https://www.gnu.org/licenses/>.

//! This module contains the actual emulator.
//!
//! Many components in this module are public so components in submodules can use them in a public
//! interface.

pub mod constants;
mod core;
pub mod instructions;

use self::core::OperandType;
use constants::{default_values, operand_indices, register_indices};
use instructions::{SimpleInstructionFunction, VectorMode, NO_MEMORY};

use gpcas_isa::{format_c_string, instruction_flags, Emulator, Instruction, OperandStorage};
use std::{
    io::Write,
    sync::atomic::{AtomicUsize, Ordering},
};

/// Emulates the given ForwardCom program and feeds data to the simulator.
pub struct ForwardComEmulator {
    /// The current instruction pointer value.
    ip: usize,
    /// The instruction pointer value upon program start.
    start_address: usize,
    /// The count of emulation calls.
    instruction_count: AtomicUsize,
    /// The count of emulated non-speculative instructions.
    retired_instruction_count: AtomicUsize,
    /// The memory data of the program image.
    memory: Vec<u8>,
    /// The list of return addresses.
    ///
    /// As ForwardCom uses its own stack for this, it has its own data field in this struct.
    call_stack: Vec<usize>,
    /// Holds information needed for generating sub-instructions of an multi-instruction.
    decoder: core::Decoder,
    /// A container for the operands of an instruction.
    ///
    /// Is defined inside the emulator as opposed to inside the instruction because it enables
    /// instructions to be newly generated, while avoiding the heap allocation from the operand
    /// storage.
    operands: OperandStorage,
    /// Stores the current register values.
    registers: RegisterFile,
    /// Buffers the output of STDOUT for the emulation.
    output_print_buffer: String,
    /// The base address of the instruction pointer, used when debugging.
    ip_base: usize,
    file_handles: Vec<Option<std::fs::File>>,
}

/// Atmomic instruction of the simulator.
///
/// Contains the information that get sent to the simulator, as well as additional fields needed
/// for executing the instruction. Does not include actual operand values, these can be found in the
/// [`ForwardComEmulator`].
pub struct EmulatorInstruction {
    /// The part that gets sent to the simulator.
    pub core: Instruction,
    /// The execution function that gets called by the emulator.
    pub function: SimpleInstructionFunction,
    /// How the instruction function operates on vectors.
    pub vector_mode: VectorMode,
    /// The type of the operand data.
    pub operand_type: OperandType,
    /// The total operand size of the instruction in bytes.
    pub operand_size: usize,
    /// Additional custom option bitfield for the execution function.
    pub option_bits: u8,
    /// As the simulator currently doesn't support addressing special registers, this signals the
    /// output register being a special register.
    pub special_output: bool,
    /// If the instruction can be committed.
    pub valid: bool,
}

/// Holds all register values.
pub struct RegisterFile {
    /// The general purpose registers of the ForwardCom specification.
    pub general_purpose: [u64; 32],
    /// The data of the vector registers, collapsed into one block of continuous block of memory.
    pub vector: Vec<u8>,
    /// The used length of each vector register.
    pub vector_lengths: [usize; 32],
    /// Special registers as defined in the ForwardCom specification.
    pub special: [u64; 32],
    /// The maximum length of a vector in bytes.
    max_vector_size: usize,
}

/// Function IDs of system call.
mod sys_call_ids {
    /// Exit the program.
    pub const EXIT: u32 = 0x10;
    /// Print something to stdout.
    pub const PRINT: u32 = 0x103;
    /// Print something to a file.
    pub const FILE_PRINT: u32 = 0x104;
    /// Open a file.
    pub const FILE_OPEN: u32 = 0x110;
    /// Close a file.
    pub const FILE_CLOSE: u32 = 0x111;
    /// Write binary data to a file.
    pub const FILE_WRITE: u32 = 0x113;
}

impl ForwardComEmulator {
    /// Constructs a new emulator instance.
    ///
    /// For ease of use, this also takes care of loading the program.
    /// `max_vector_size` is the maximum **bit** size that fits into a vector.
    pub fn new(executable_data: Vec<u8>, max_vector_size: usize) -> ForwardComEmulator {
        debug_assert!(max_vector_size >= 128);
        debug_assert!(max_vector_size % 8 == 0);

        let program = crate::program::load_forwardcom_elf(&executable_data).unwrap();
        // bit -> byte conversion
        let mut registers = RegisterFile::new(max_vector_size / 8);
        registers.general_purpose[register_indices::STACK_POINTER] = program.stack_address as u64;
        registers.special[register_indices::NUMCONTR] = default_values::NUMCONTR;
        registers.special[register_indices::THREADP] = program.threadp;
        registers.special[register_indices::DATAP] = program.datap;

        // TODO initialize all special registers

        ForwardComEmulator {
            ip: program.start_address,
            start_address: program.start_address,
            instruction_count: AtomicUsize::new(0),
            retired_instruction_count: AtomicUsize::new(0),
            memory: program.data,
            call_stack: Vec::new(),
            decoder: core::Decoder::default(),
            // bit -> byte conversion
            operands: OperandStorage::new(max_vector_size / 8, 32),
            registers,
            output_print_buffer: String::new(),
            ip_base: program.ip_base as usize,
            file_handles: Vec::new(),
        }
    }

    /// Execute a system call.
    ///
    /// If available, input 1 is the address of a shared memory block and input 2 the size of said
    /// block. If both are the stack pointer, the whole application memory is shared.
    /// The last input contains the module ID at an offset of the element size and the function ID
    /// at an offset of zero.
    pub fn handle_sys_call<const MEMORY: usize>(
        &mut self,
        instruction: &mut EmulatorInstruction,
        _offset: usize,
    ) {
        let id_offset = if MEMORY == NO_MEMORY { 0 } else { 2 };
        let function_id = if let OperandType::I64 = instruction.operand_type {
            self.operands.get_u32(operand_indices::INPUT1 + id_offset)
        } else {
            self.operands.get_u16(operand_indices::INPUT1 + id_offset) as u32
        };

        match function_id {
            sys_call_ids::EXIT => {
                log::info!(
                    "Program exit with code {}",
                    self.registers.general_purpose[0]
                );
                instruction.core.flags |= instruction_flags::TERMINATE;
            }
            sys_call_ids::PRINT => {
                let string_start = self.registers.general_purpose[0] as usize;
                if let Some(index) = self.memory[string_start..]
                    .iter()
                    .position(|&char| char == 0)
                {
                    self.output_print_buffer.push_str(
                        format_c_string(
                            &String::from_utf8_lossy(
                                &self.memory[string_start..string_start + index],
                            ),
                            &self.memory[self.registers.general_purpose[1] as usize..],
                        )
                        .unwrap_or_else(|e| format!("Error printing: {e}"))
                        .as_str(),
                    );
                } else {
                    log::error!("The string to print is not zero-terminated.");
                    instruction.valid = false;
                }
            }
            sys_call_ids::FILE_PRINT => {
                if let Err(e) = self.file_print() {
                    log::error!("Error printing to file: {}", e);
                    instruction.valid = false;
                }
            }
            sys_call_ids::FILE_OPEN => {
                if let Err(e) = self.file_open() {
                    log::error!("Error opening file: {}", e);
                    instruction.valid = false;
                }
            }
            sys_call_ids::FILE_CLOSE => {
                let file_id = self.registers.general_purpose[0] as usize;
                if let Some(file) = self.file_handles.get_mut(file_id) {
                    file.take();
                    self.registers.general_purpose[0] = 0;
                } else {
                    self.registers.general_purpose[0] = -1i64 as u64;
                }
            }
            sys_call_ids::FILE_WRITE => {
                let pointer = self.registers.general_purpose[0] as usize;
                let size = self.registers.general_purpose[1] as usize;
                let count = self.registers.general_purpose[2] as usize;
                let file_id = self.registers.general_purpose[3] as usize;

                let len = size * count;
                let memory = &self.memory[pointer..pointer + len];
                if let Some(Some(file)) = self.file_handles.get_mut(file_id) {
                    if let Err(e) = file.write_all(memory) {
                        log::error!("Error writing to file: {}", e);
                        instruction.valid = false;
                    } else {
                        self.registers.general_purpose[0] = count as u64;
                    }
                } else {
                    log::error!("Error writing to file: The file handle doesn't exist");
                    instruction.valid = false;
                }
            }
            id => {
                log::error!("Unrecognized system call (ID {:#X}).", id);
                instruction.valid = false;
            }
        }
    }

    fn file_print(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        let file = self
            .file_handles
            .get_mut(self.registers.general_purpose[0] as usize)
            .ok_or("The file handle doesn't exist")?
            .as_mut()
            .ok_or("The file has been closed")?;
        let format_start = self.registers.general_purpose[1] as usize;
        let format_len = self.memory[format_start..]
            .iter()
            .position(|&char| char == 0)
            .ok_or("The format string isn't zero-terminated")?;

        let text = format_c_string(
            &String::from_utf8_lossy(&self.memory[format_start..format_start + format_len]),
            &self.memory[self.registers.general_purpose[2] as usize..],
        )
        .map_err(|e| e.to_string())?;

        file.write_all(text.as_bytes()).map_err(|e| e.to_string())?;
        self.registers.general_purpose[0] = text.len() as u64;

        Ok(())
    }

    fn file_open(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        let path_start = self.registers.general_purpose[0] as usize;
        let path_len = self.memory[path_start..]
            .iter()
            .position(|&char| char == 0)
            .ok_or("The path isn't zero-terminated")?;
        let path = String::from_utf8_lossy(&self.memory[path_start..path_start + path_len]);
        let mode_start = self.registers.general_purpose[1] as usize;
        let mode_len = self.memory[mode_start..]
            .iter()
            .position(|&char| char == 0)
            .ok_or("The mode string isn't zero-terminated")?;
        let mode = String::from_utf8_lossy(&self.memory[mode_start..mode_start + mode_len]);

        let (read, write, append, truncate) = match &*mode {
            "r" | "rb" => (true, false, false, false),
            "w" | "wb" => (false, true, false, true),
            "a" | "ab" => (false, false, true, false),
            "r+" | "rb+" | "r+b" => (true, true, false, false),
            "w+" | "wb+" | "w+b" => (true, true, false, true),
            "a+" | "ab+" | "a+b" => (true, false, true, false),
            e => return Err(Box::from(format!("Invalid mode \"{e}\""))),
        };

        let file = std::fs::OpenOptions::new()
            .read(read)
            .write(write)
            .append(append)
            .truncate(truncate)
            .create(append | truncate)
            .open(&*path)?;
        self.file_handles.push(Some(file));

        self.registers.general_purpose[0] = self.file_handles.len() as u64 - 1;
        Ok(())
    }
}

impl Emulator for ForwardComEmulator {
    #[inline]
    fn emulate_instruction(&mut self, address: usize, commit: bool) -> Option<Instruction> {
        debug_assert!(
            !commit | (address == self.ip),
            "Control flow failure, expected {:#X}, got {:#X}",
            self.ip,
            address
        );
        log::debug!(
            "Processing ip {:#X} ({:#X})",
            (address - self.ip_base) >> 2,
            address
        );
        let (instruction, has_follow_up) =
            self.decoder
                .get_next_instruction(address, &self.memory, &self.registers);
        // Some instructions need the ip as register
        self.registers.special[register_indices::IP] = address as u64 + instruction.size as u64;

        let mut instruction = core::prepare(self, instruction, has_follow_up);

        if commit {
            core::execute(&mut instruction, self);
            core::write_results(
                &mut self.memory,
                &mut self.registers,
                &self.operands,
                &instruction,
            );
            self.ip = core::set_next_ip(&mut self.call_stack, &mut instruction);
            self.retired_instruction_count
                .fetch_add(1, Ordering::Relaxed);
        }

        log::trace!("");
        self.instruction_count.fetch_add(1, Ordering::Relaxed);
        if (instruction.core.flags & instruction_flags::TERMINATE) > 0 {
            None
        } else {
            Some(instruction.core)
        }
    }

    #[inline]
    fn get_instruction_count(&self) -> usize {
        self.instruction_count.load(Ordering::Relaxed)
    }

    #[inline]
    fn get_current_ip(&self) -> usize {
        self.ip
    }

    #[inline]
    fn get_printed_output(&self) -> &str {
        self.output_print_buffer.as_str()
    }

    #[inline]
    fn get_start_address(&self) -> usize {
        self.start_address
    }
}

// TODO support for all special function registers
impl RegisterFile {
    /// Creates a new register file. All registers are initialized to zero.
    ///
    /// `max_vector_size` is the maximum **byte** size that fits into a vector.
    pub fn new(max_vector_size: usize) -> Self {
        RegisterFile {
            general_purpose: [0; 32],
            vector: vec![0; 32 * max_vector_size],
            vector_lengths: [0; 32],
            special: [0; 32],
            max_vector_size,
        }
    }

    /// Returns the vector register of the specified number.
    ///
    /// The slice has the length of the actual vector.
    #[inline]
    pub fn get_vector(&self, index: usize) -> &[u8] {
        let base = index * self.max_vector_size;
        &self.vector.as_slice()[base..base + self.vector_lengths[index]]
    }

    /// Sets the vector register of the specified number to the provided value.
    ///
    /// The length is taken from the length of the slice.
    #[inline]
    pub fn set_vector(&mut self, index: usize, value: &[u8]) {
        let base = index * self.max_vector_size;
        self.vector.as_mut_slice()[base..base + value.len()].copy_from_slice(value);
        self.vector_lengths[index] = value.len();
    }
}

#[cfg(test)]
mod tests {
    use super::ForwardComEmulator;
    use super::RegisterFile;
    use crate::emulator::constants::{default_values, register_indices};

    use gpcas_isa::{Emulator, OperandStorage};
    use std::convert::TryInto;

    #[test]
    fn push_pop() {
        let mut memory = vec![0u8; 96];
        // push registers 16-23
        memory[0..4].copy_from_slice(&u32::to_le_bytes(0x471f_f017_u32)[..]);
        // pop registers 16-23
        memory[4..8].copy_from_slice(&u32::to_le_bytes(0x473f_f017_u32)[..]);

        let mut emulator = get_emulator(memory);
        for i in 0..8 {
            emulator.emulate_instruction(0x0, true);
            emulator.registers.general_purpose[i + 16] = 0;
            assert_eq!(
                u64::from_le_bytes(
                    emulator.memory
                        [emulator.memory.len() - 8 * (i + 1)..emulator.memory.len() - 8 * i]
                        .try_into()
                        .unwrap()
                ),
                i as u64 + 17,
                "Register {} at address {}",
                i + 16,
                emulator.memory.len() - 8 * (i + 1),
            );
        }
        emulator.emulate_instruction(0x0, true);
        for i in 0..8 {
            emulator.emulate_instruction(0x4, true);
            assert_eq!(
                emulator.registers.general_purpose[24 - i - 1],
                24 - i as u64,
                "Register {} had the wrong value!",
                24 - i - 1,
            );
        }
        emulator.emulate_instruction(0x4, true);
    }

    fn get_emulator(data: Vec<u8>) -> ForwardComEmulator {
        let mut registers = RegisterFile::new(2);

        for i in 0..31 {
            registers.general_purpose[i] = i as u64 + 1;
        }
        registers.general_purpose[register_indices::STACK_POINTER] = data.len() as u64;
        registers.special[register_indices::NUMCONTR] = default_values::NUMCONTR;

        ForwardComEmulator {
            ip: 0,
            start_address: 0,
            instruction_count: Default::default(),
            retired_instruction_count: Default::default(),
            memory: data,
            call_stack: Vec::new(),
            decoder: Default::default(),
            operands: OperandStorage::new(16, 32),
            registers,
            output_print_buffer: String::new(),
            ip_base: 0,
            file_handles: Vec::new(),
        }
    }
}