summaryrefslogtreecommitdiff
path: root/src/state.rs
blob: 6697059a7f5d88be74092ca522367a97673a1c3a (plain) (blame)
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
/*
	Copyright 2021-2023 Gabriel Jensen.

	This file is part of Luma.

	Luma is free software: you can redistribute it
	and/or modify it under the terms of the GNU
	Affero General Public License as published by
	the Free Software Foundation, either version 3
	of the License, or (at your option) any later
	version.

	Luma 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
	Affero General Public License for more details.

	You should have received a copy of the GNU
	Affero General Public License along with Luma.
	If not, see <https://www.gnu.org/licenses/>.
*/

use crate::{BOOTLOADER_LENGTH, IMAGE_LENGTH, PALETTE_LENGTH, VIDEO_LENGTH};
use crate::cpu_mode::CpuMode;

use std::slice::{from_raw_parts, from_raw_parts_mut};

pub mod bank;
pub mod new;
pub mod read;
pub mod shifter_value;
pub mod write;

pub struct State {
	registers: [*mut u32; 0x10],
	banks:     Box<[[u32; 0x10]; 0x6]>,

	cpsr: u32,
	spsr: [u32; 0x6],

	memory: Vec::<u32>,
}

macro_rules! address_unused {
	($address: expr) => {{
		match $address {
			0x02040000..=0x02FFFFFF => true,
			0x03008000..=0x03FFFFFF => true,
			0x04000400..=0x04FFFFFF => true,
			0x05000400..=0x05FFFFFF => true,
			0x06018000..=0x06FFFFFF => true,
			0x07000400..=0x07FFFFFF => true,

			_ => false,
		}
	}};
}
pub(crate) use address_unused;

impl State {
	#[must_use]
	pub fn video8<'a>(&'a self) -> &'a [u8] {
		let slice = unsafe {
			let pointer = (self.memory.as_ptr() as *const u8).add(0x06000000);
			let slice = from_raw_parts(pointer, VIDEO_LENGTH as usize);

			slice
		};

		return slice;
	}

	#[must_use]
	pub fn palette<'a>(&'a self) -> &'a [u16] {
		let slice = unsafe {
			let pointer = (self.memory.as_ptr() as *const u8).add(0x05000000) as *const u16;
			let slice = from_raw_parts(pointer, PALETTE_LENGTH as usize);

			slice
		};

		return slice;
	}

	#[must_use]
	pub fn bootloader_buffer<'a>(&'a mut self) -> &'a mut [u8] {
		let slice = unsafe {
			let pointer = (self.memory.as_mut_ptr() as *mut u8).add(0x00000000);
			let slice = from_raw_parts_mut(pointer, BOOTLOADER_LENGTH as usize);

			slice
		};

		return slice;
	}

	#[must_use]
	pub fn image_buffer<'a>(&'a mut self) -> &'a mut [u8] {
		let slice = unsafe {
			let pointer = (self.memory.as_mut_ptr() as *mut u8).add(0x08000000);
			let slice = from_raw_parts_mut(pointer, IMAGE_LENGTH as usize);

			slice
		};

		return slice;
	}

	#[must_use]
	fn spsr_index(mode: CpuMode) -> usize {
		use CpuMode::*;

		return match mode {
			User                 => 0x0,
			System               => 0x0,
			FastInterruptRequest => 0x1,
			InterruptRequest     => 0x2,
			Supervisor           => 0x3,
			Abort                => 0x4,
			Undefined            => 0x5,
		};
	}
}

unsafe impl Send for State { }