summary refs log tree commit diff
path: root/src/interpreter.rs
blob: bc517545bfd043cb8e2ac600234954847bec9be6 (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
use core::panic;

use crate::glk::Glk;

pub type MemAddress = u32;
pub type StackAddress = u32;
pub type FunctionPointer = MemAddress;

enum StubDestType {
    DoNotStore = 0,
    MainMemory = 1,
    LocalVariable = 2,
    PushOnStack = 3,
    ResumeCompressedString = 10,
    ResumeFunctionFromString = 11,
    ResumeInteger = 12,
    ResumeCString = 13,
    ResumeUnicodeString = 14,
}

enum Nybble {
    High,
    Low,
}

enum Operand {
    Constant(i32),
    PopStack,
    FrameIndirect(StackAddress),
    MemIndirect(MemAddress),
    MemRamIndirect(MemAddress),
}

enum ReadWriteDest {
    Stack,
    Memory,
}

pub struct HeapChunk {
    pub start_address: MemAddress,
    pub length: u32,
    pub contents: Option<Vec<u32>>
}

pub enum Output {
    Null,
    Filter(FunctionPointer),
    Glk(Glk),
}

pub struct Interpreter {
    memory: Vec<u8>,
    stack: Vec<u8>,
    mem_writeable: MemAddress,
    mem_min: MemAddress,
    heap: Vec<HeapChunk>,

    reg_program: MemAddress,
    reg_stack: StackAddress,
    reg_frame: StackAddress,
    reg_strdec: MemAddress,

    output_mode: Output,
}

impl Interpreter {
    pub fn init(
        mut source_file: Vec<u8>,
        ram_start: u32,
        ext_start: u32,
        ram_end: u32,
        stack_size: u32,
        str_decode: u32,
    ) -> Self {
        // Trick to ensure everything after ext_start is cleared
        // even if the original file was longer
        source_file.resize(ext_start as usize, 0);
        source_file.resize(ext_start as usize, 0);

        let mut stack = Vec::new();
        stack.resize(stack_size as usize, 0);
        
        Self {
            memory: source_file,
            stack,
            mem_writeable: ram_start,
            mem_min: ram_end,
            heap: Vec::new(),

            reg_program: 0,
            reg_stack: 0,
            reg_frame: 0,
            reg_strdec: str_decode,

            output_mode: Output::Null,
        }
    }

    pub fn run(&mut self, entry_func: MemAddress) {
        self.reg_program = self.create_stack_frame(entry_func, Vec::new());

        loop {
            let instruction = self.load_program_instruction();

            match instruction {
                _ => todo!("Haven't done any of the instruction loading yet")
            }
        }
    }

    fn create_return_stub(&mut self, dest_type: StubDestType, dest_addr: u32, program_counter: MemAddress, frame_pointer: StackAddress) {

    }

    // Returns program counter after parsing all the args
    fn create_stack_frame(&mut self, func: FunctionPointer, mut args: Vec<u32>) -> MemAddress {
        println!("create_stack_frame: Loading function from {func:x}");

        println!("Initial frame ptr: {}", self.reg_frame);
        println!("Initial stack ptr: {}", self.reg_stack);

        // Reading function signature
        let func_type = self.get_byte(func, ReadWriteDest::Memory);
        let args_in_locals = match func_type {
            0xC0 => false,
            0xC1 => true,
            _ => panic!("Tried to call object that is not a function: {}", func_type),
        };

        let mut local_pointer = func + 1;
        let mut format_of_locals = Vec::<(u8, u8)>::new();
        loop {
            let format = (
                self.get_byte(local_pointer, ReadWriteDest::Memory),
                self.get_byte(local_pointer + 1, ReadWriteDest::Memory),
            );

            local_pointer += 2;

            if format == (0,0) {
                break
            }

            format_of_locals.push(format);
        };

        let opcode_start = local_pointer + 1;

        // Writing stack frame
        self.reg_frame = self.reg_stack;
        let mut header_len = 4 + 4 + format_of_locals.len() as StackAddress * 2;
        Interpreter::align_ptr(&mut header_len, 16);

        // Write locals pos
        println!("Writing locals pos ({header_len}) to frame + 4 ({})", self.reg_frame + 4);
        self.write_word(self.reg_frame + 4, ReadWriteDest::Stack, header_len);

        // Write format of locals
        let mut local_format_ptr = self.reg_frame + 8;
        for (local_type, local_count) in format_of_locals.iter() {
            println!("Writing local format ({}, {}) to {}", local_type, local_count, local_format_ptr);
            self.write_byte(local_format_ptr, ReadWriteDest::Stack, *local_type);
            self.write_byte(local_format_ptr + 1, ReadWriteDest::Stack, *local_count);
            local_format_ptr += 2;
        }

        // Write locals
        let mut local_pos = self.reg_frame+ header_len;
        for (local_type, local_count) in format_of_locals {
            Interpreter::align_ptr(&mut local_pos, local_type * 8);

            for _count in 0..local_count {
                let value = match args_in_locals {
                    true => if args.len() > 0 {args.remove(0)} else {0}
                    false => 0,
                };

                println!("Writing local variable ({}, {} bytes) to {}", value, local_type, local_pos);

                match local_type {
                    1 => self.write_byte(local_pos, ReadWriteDest::Stack, value as u8),
                    2 => self.write_twobyte(local_pos, ReadWriteDest::Stack, value as u16),
                    4 => self.write_word(local_pos, ReadWriteDest::Stack, value),
                    _ => panic!("Invalid local type {local_type}")
                }

                local_pos += local_type as u32;
            }
        }
        
        // Write frame len
        Interpreter::align_ptr(&mut local_pos, 32);
        println!("Writing frame len ({}) to frame ptr ({})", local_pos - self.reg_frame, self.reg_frame);
        self.write_word(self.reg_frame, ReadWriteDest::Stack, local_pos - self.reg_frame);
        self.reg_stack = local_pos;

        // Handle stack args
        if !args_in_locals {
            args.reverse();
            for arg in args {
                println!("Adding value to stack {arg}");
                self.push_stack(arg);
            }
        }

        println!("Ending frame ptr: {}", self.reg_frame);
        println!("Ending stack ptr: {}", self.reg_stack);

        for [a, b, c, d] in self.stack[self.reg_frame as usize..self.reg_stack as usize].iter()
            .array_chunks::<4>() {
                println!("{a:02x} {b:02x} {c:02x} {d:02x} ({})", u32::from_be_bytes([*a,*b,*c,*d]));
            }

        return opcode_start;
    }

    fn load_program_instruction(&mut self) {

    }

    fn push_stack(&mut self, value: u32) {
        self.write_word(self.reg_stack, ReadWriteDest::Stack, value);
        self.reg_stack += 4;
    }

    fn pop_stack(&mut self) -> u32 {
        self.reg_stack -= 4;
        self.get_word(self.reg_stack, ReadWriteDest::Stack)
    }

    fn get_byte(&self, addr: u32, src: ReadWriteDest) -> u8 {
        let addr = addr as usize;
        let buffer = match src {
            ReadWriteDest::Stack => &self.stack,
            ReadWriteDest::Memory => &self.memory,
        };

        buffer[addr]
    }

    fn get_twobyte(&self, addr: u32, src: ReadWriteDest) -> u16 {
        let addr = addr as usize;
        let buffer = match src {
            ReadWriteDest::Stack => &self.stack,
            ReadWriteDest::Memory => &self.memory,
        };

        u16::from_be_bytes(buffer[addr..addr+2].try_into().unwrap())
    }

    fn get_word(&self, addr: u32, src: ReadWriteDest) -> u32 {
        let addr = addr as usize;
        let buffer = match src {
            ReadWriteDest::Stack => &self.stack,
            ReadWriteDest::Memory => &self.memory,
        };

        u32::from_be_bytes(buffer[addr..addr+4].try_into().unwrap())
    }

    fn write_byte(&mut self, addr: u32, dst: ReadWriteDest, value: u8) {
        let addr = addr as usize;
        let buffer = match dst {
            ReadWriteDest::Stack => &mut self.stack,
            ReadWriteDest::Memory => &mut self.memory,
        };

        buffer[addr] = value;
    }

    fn write_twobyte(&mut self, addr: u32, dst: ReadWriteDest, value: u16) {
        let addr = addr as usize;
        let buffer = match dst {
            ReadWriteDest::Stack => &mut self.stack,
            ReadWriteDest::Memory => &mut self.memory,
        };

        buffer[addr..addr+2].copy_from_slice(&value.to_be_bytes());
    }

    fn write_word(&mut self, addr: u32, dst: ReadWriteDest, value: u32) {
        let addr = addr as usize;
        let buffer = match dst {
            ReadWriteDest::Stack => &mut self.stack,
            ReadWriteDest::Memory => &mut self.memory,
        };

        buffer[addr..addr+4].copy_from_slice(&value.to_be_bytes());
    }

    fn align_ptr(ptr: &mut u32, align_size: u8) {
        let modulo = match align_size {
            8 => 1,
            16 => 2,
            32 => 4,
            _ => panic!("Invalid alignment: {align_size}"),
        };

        let bytes_off = *ptr % modulo;
        *ptr += bytes_off;
    }
}