gcrecomp-core 0.0.1-alpha

Static recompiler for GameCube games to Rust
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
//! Memory Manager
//!
//! This module provides memory management for the GameCube recompiler runtime.
//! It handles address translation, memory reads/writes, and bulk operations.
//!
//! # Memory Map
//! GameCube uses a flat memory model with the following regions:
//! - **0x80000000 - 0x817FFFFF**: Main RAM (24MB)
//! - **0xCC000000 - 0xCFFFFFFF**: ARAM (16MB audio RAM)
//! - **0x80000000 - 0x807FFFFF**: Locked cache (8MB, overlaps with main RAM)
//!
//! # Memory Optimizations
//! - All hot-path functions use `#[inline(always)]` for address translation
//! - Read/write functions use `#[inline]` for performance
//! - Explicit type annotations to reduce compiler inference overhead
//! - Bulk operations use optimized copy_from_slice for non-overlapping ranges
//!
//! # Address Translation
//! GameCube uses physical addresses directly. Main RAM is mapped at 0x80000000,
//! so we subtract this base address to get the RAM offset.

use anyhow::{Context, Result};

/// Memory manager for GameCube memory operations.
///
/// # Memory Layout
/// - `ram`: 24MB byte array (heap allocation required for large size)
///
/// # Address Translation
/// GameCube main RAM is mapped to virtual addresses 0x80000000-0x817FFFFF.
/// Physical addresses are computed by subtracting the base address (0x80000000).
#[derive(Debug)]
pub struct MemoryManager {
    /// Main RAM (24MB)
    ram: Vec<u8>,
}

impl MemoryManager {
    /// Create a new memory manager with 24MB of RAM.
    ///
    /// # Returns
    /// `MemoryManager` - Initialized memory manager with all bytes set to 0
    ///
    /// # Examples
    /// ```rust
    /// let mut memory = MemoryManager::new();
    /// ```
    #[inline] // Constructor - simple, may be inlined
    pub fn new() -> Self {
        // 24MB RAM model
        const RAM_SIZE: usize = 24usize * 1024usize * 1024usize; // 24MB
        Self {
            ram: vec![0u8; RAM_SIZE],
        }
    }

    /// Translate a virtual address to a physical RAM offset.
    ///
    /// # Algorithm
    /// GameCube uses a flat memory model with physical addresses.
    /// Main RAM is at 0x80000000 - 0x817FFFFF (24MB).
    /// Physical offset = virtual_address - 0x80000000
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address
    ///
    /// # Returns
    /// `Option<usize>` - Physical RAM offset if address is in main RAM, None otherwise
    ///
    /// # Examples
    /// ```rust
    /// let offset = memory.translate_address(0x80000000);
    /// assert_eq!(offset, Some(0));
    /// ```
    #[inline(always)] // Hot path - always inline for performance
    fn translate_address(&self, address: u32) -> Option<usize> {
        // GameCube uses a flat memory model with physical addresses
        // Main RAM is at 0x80000000 - 0x817FFFFF
        if address >= 0x80000000u32 && address < 0x81800000u32 {
            Some((address.wrapping_sub(0x80000000u32)) as usize)
        } else {
            None
        }
    }

    /// Read a single byte from memory.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address
    ///
    /// # Returns
    /// `Result<u8>` - Byte value at address, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address is not in main RAM or out of bounds
    ///
    /// # Examples
    /// ```rust
    /// let value = memory.read_u8(0x80000000)?;
    /// ```
    #[inline] // Hot path - may be inlined
    pub fn read_u8(&self, address: u32) -> Result<u8> {
        let offset: usize = self
            .translate_address(address)
            .context("Invalid memory address")?;
        Ok(self.ram[offset])
    }

    /// Read a 16-bit word (big-endian) from memory.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address (must be aligned, but we don't enforce it)
    ///
    /// # Returns
    /// `Result<u16>` - 16-bit value at address, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address+1 is out of bounds
    ///
    /// # Examples
    /// ```rust
    /// let value = memory.read_u16(0x80000000)?;
    /// ```
    #[inline] // Hot path - may be inlined
    pub fn read_u16(&self, address: u32) -> Result<u16> {
        let offset: usize = self
            .translate_address(address)
            .context("Invalid memory address")?;
        if offset.wrapping_add(1usize) >= self.ram.len() {
            anyhow::bail!("Memory read out of bounds");
        }
        let bytes: [u8; 2] = [self.ram[offset], self.ram[offset.wrapping_add(1usize)]];
        Ok(u16::from_be_bytes(bytes))
    }

    /// Read a 32-bit word (big-endian) from memory.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address (must be aligned, but we don't enforce it)
    ///
    /// # Returns
    /// `Result<u32>` - 32-bit value at address, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address+3 is out of bounds
    ///
    /// # Examples
    /// ```rust
    /// let value = memory.read_u32(0x80000000)?;
    /// ```
    #[inline] // Hot path - may be inlined
    pub fn read_u32(&self, address: u32) -> Result<u32> {
        let offset: usize = self
            .translate_address(address)
            .context("Invalid memory address")?;
        if offset.wrapping_add(3usize) >= self.ram.len() {
            anyhow::bail!("Memory read out of bounds");
        }
        let bytes: [u8; 4] = [
            self.ram[offset],
            self.ram[offset.wrapping_add(1usize)],
            self.ram[offset.wrapping_add(2usize)],
            self.ram[offset.wrapping_add(3usize)],
        ];
        Ok(u32::from_be_bytes(bytes))
    }

    /// Read a 64-bit word (big-endian) from memory.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address (must be aligned, but we don't enforce it)
    ///
    /// # Returns
    /// `Result<u64>` - 64-bit value at address, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address+7 is out of bounds
    ///
    /// # Examples
    /// ```rust
    /// let value = memory.read_u64(0x80000000)?;
    /// ```
    #[inline] // Hot path - may be inlined
    pub fn read_u64(&self, address: u32) -> Result<u64> {
        let offset: usize = self
            .translate_address(address)
            .context("Invalid memory address")?;
        if offset.wrapping_add(7usize) >= self.ram.len() {
            anyhow::bail!("Memory read out of bounds");
        }
        let bytes: [u8; 8] = [
            self.ram[offset],
            self.ram[offset.wrapping_add(1usize)],
            self.ram[offset.wrapping_add(2usize)],
            self.ram[offset.wrapping_add(3usize)],
            self.ram[offset.wrapping_add(4usize)],
            self.ram[offset.wrapping_add(5usize)],
            self.ram[offset.wrapping_add(6usize)],
            self.ram[offset.wrapping_add(7usize)],
        ];
        Ok(u64::from_be_bytes(bytes))
    }

    /// Write a single byte to memory.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address
    /// * `value` - Byte value to write
    ///
    /// # Returns
    /// `Result<()>` - Success, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address is not in main RAM or out of bounds
    ///
    /// # Examples
    /// ```rust
    /// memory.write_u8(0x80000000, 0x42)?;
    /// ```
    #[inline] // Hot path - may be inlined
    pub fn write_u8(&mut self, address: u32, value: u8) -> Result<()> {
        let offset: usize = self
            .translate_address(address)
            .context("Invalid memory address")?;
        self.ram[offset] = value;
        Ok(())
    }

    /// Write a 16-bit word (big-endian) to memory.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address (must be aligned, but we don't enforce it)
    /// * `value` - 16-bit value to write
    ///
    /// # Returns
    /// `Result<()>` - Success, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address+1 is out of bounds
    ///
    /// # Examples
    /// ```rust
    /// memory.write_u16(0x80000000, 0x1234)?;
    /// ```
    #[inline] // Hot path - may be inlined
    pub fn write_u16(&mut self, address: u32, value: u16) -> Result<()> {
        let offset: usize = self
            .translate_address(address)
            .context("Invalid memory address")?;
        if offset.wrapping_add(1usize) >= self.ram.len() {
            anyhow::bail!("Memory write out of bounds");
        }
        let bytes: [u8; 2] = value.to_be_bytes();
        self.ram[offset] = bytes[0];
        self.ram[offset.wrapping_add(1usize)] = bytes[1];
        Ok(())
    }

    /// Write a 32-bit word (big-endian) to memory.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address (must be aligned, but we don't enforce it)
    /// * `value` - 32-bit value to write
    ///
    /// # Returns
    /// `Result<()>` - Success, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address+3 is out of bounds
    ///
    /// # Examples
    /// ```rust
    /// memory.write_u32(0x80000000, 0x12345678)?;
    /// ```
    #[inline] // Hot path - may be inlined
    pub fn write_u32(&mut self, address: u32, value: u32) -> Result<()> {
        let offset: usize = self
            .translate_address(address)
            .context("Invalid memory address")?;
        if offset.wrapping_add(3usize) >= self.ram.len() {
            anyhow::bail!("Memory write out of bounds");
        }
        let bytes: [u8; 4] = value.to_be_bytes();
        self.ram[offset] = bytes[0];
        self.ram[offset.wrapping_add(1usize)] = bytes[1];
        self.ram[offset.wrapping_add(2usize)] = bytes[2];
        self.ram[offset.wrapping_add(3usize)] = bytes[3];
        Ok(())
    }

    /// Write a 64-bit word (big-endian) to memory.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address (must be aligned, but we don't enforce it)
    /// * `value` - 64-bit value to write
    ///
    /// # Returns
    /// `Result<()>` - Success, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address+7 is out of bounds
    ///
    /// # Examples
    /// ```rust
    /// memory.write_u64(0x80000000, 0x1234567890ABCDEF)?;
    /// ```
    #[inline] // Hot path - may be inlined
    pub fn write_u64(&mut self, address: u32, value: u64) -> Result<()> {
        let offset: usize = self
            .translate_address(address)
            .context("Invalid memory address")?;
        if offset.wrapping_add(7usize) >= self.ram.len() {
            anyhow::bail!("Memory write out of bounds");
        }
        let bytes: [u8; 8] = value.to_be_bytes();
        for (i, byte) in bytes.iter().enumerate() {
            self.ram[offset.wrapping_add(i)] = *byte;
        }
        Ok(())
    }

    /// Read multiple bytes from memory.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address
    /// * `len` - Number of bytes to read
    ///
    /// # Returns
    /// `Result<Vec<u8>>` - Byte vector, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address+len is out of bounds
    ///
    /// # Examples
    /// ```rust
    /// let data = memory.read_bytes(0x80000000, 1024)?;
    /// ```
    #[inline] // May be inlined for small lengths
    pub fn read_bytes(&self, address: u32, len: usize) -> Result<Vec<u8>> {
        let offset: usize = self
            .translate_address(address)
            .context("Invalid memory address")?;
        if offset.wrapping_add(len) > self.ram.len() {
            anyhow::bail!("Memory read out of bounds");
        }
        Ok(self.ram[offset..offset.wrapping_add(len)].to_vec())
    }

    /// Write multiple bytes to memory.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address
    /// * `data` - Byte slice to write
    ///
    /// # Returns
    /// `Result<()>` - Success, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address+data.len() is out of bounds
    ///
    /// # Examples
    /// ```rust
    /// memory.write_bytes(0x80000000, &[0x42, 0x43, 0x44])?;
    /// ```
    #[inline] // May be inlined for small lengths
    pub fn write_bytes(&mut self, address: u32, data: &[u8]) -> Result<()> {
        let offset: usize = self
            .translate_address(address)
            .context("Invalid memory address")?;
        if offset.wrapping_add(data.len()) > self.ram.len() {
            anyhow::bail!("Memory write out of bounds");
        }
        self.ram[offset..offset.wrapping_add(data.len())].copy_from_slice(data);
        Ok(())
    }

    /// Load a section of data into memory (convenience wrapper for write_bytes).
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address
    /// * `data` - Byte slice to write
    ///
    /// # Returns
    /// `Result<()>` - Success, or error if invalid/out of bounds
    ///
    /// # Examples
    /// ```rust
    /// memory.load_section(0x80000000, &section_data)?;
    /// ```
    #[inline] // Simple wrapper - may be inlined
    pub fn load_section(&mut self, address: u32, data: &[u8]) -> Result<()> {
        self.write_bytes(address, data)
    }

    /// Optimized bulk memory copy.
    ///
    /// # Algorithm
    /// Copies `len` bytes from `src` to `dest`. Uses optimized `copy_from_slice`
    /// for non-overlapping ranges. For overlapping ranges, uses temporary buffer
    /// to ensure correct copy semantics.
    ///
    /// # Arguments
    /// * `dest` - Destination address
    /// * `src` - Source address
    /// * `len` - Number of bytes to copy
    ///
    /// # Returns
    /// `Result<()>` - Success, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if either address is invalid or copy would go out of bounds
    ///
    /// # Examples
    /// ```rust
    /// memory.bulk_copy(0x80001000, 0x80000000, 1024)?;
    /// ```
    #[inline] // May be inlined for small lengths
    pub fn bulk_copy(&mut self, dest: u32, src: u32, len: usize) -> Result<()> {
        let dest_offset: usize = self.translate_address(dest)
            .context("Invalid destination address")?;
        let src_offset: usize = self.translate_address(src)
            .context("Invalid source address")?;
        
        if dest_offset.wrapping_add(len) > self.ram.len() || src_offset.wrapping_add(len) > self.ram.len() {
            anyhow::bail!("Bulk copy out of bounds");
        }
        
        // Always use temporary buffer to avoid borrow checker issues with overlapping slices
        let temp: Vec<u8> = self.ram[src_offset..src_offset.wrapping_add(len)].to_vec();
        self.ram[dest_offset..dest_offset.wrapping_add(len)].copy_from_slice(&temp);
        
        Ok(())
    }

    /// Get a read-only slice of memory.
    ///
    /// # Safety
    /// This function is safe but returns a reference to internal memory.
    /// The caller must ensure the slice is not used after the MemoryManager is dropped.
    ///
    /// # Arguments
    /// * `address` - 32-bit virtual address
    /// * `len` - Length of slice
    ///
    /// # Returns
    /// `Result<&[u8]>` - Byte slice, or error if invalid/out of bounds
    ///
    /// # Errors
    /// Returns error if address+len is out of bounds
    ///
    /// # Examples
    /// ```rust
    /// let slice = memory.get_slice(0x80000000, 1024)?;
    /// ```
    #[inline] // May be inlined for small lengths
    pub fn get_slice(&self, address: u32, len: usize) -> Result<&[u8]> {
        let offset: usize = self.translate_address(address)
            .context("Invalid memory address")?;
        if offset.wrapping_add(len) > self.ram.len() {
            anyhow::bail!("Memory slice out of bounds");
        }
        Ok(&self.ram[offset..offset.wrapping_add(len)])
    }
}

impl Default for MemoryManager {
    #[inline] // Simple default implementation
    fn default() -> Self {
        Self::new()
    }
}