summaryrefslogtreecommitdiff
path: root/src/cpu/isa_memory.rs
blob: f4abecbe314c80185d1a9c6404c28c73fa6e882a (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
/*
	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::log;
use crate::cpu::{Cpu, take_state_mut};

impl Cpu {
	pub(super) fn isa_load_halfword(&mut self, rd: u8, rn: u8, imm: i16) {
		take_state_mut!(state, self);

		log!("ldrh r{rd}, [r{rn}, #{imm:#X}]");

		let rn_value = state.read_register(rn);

		let target = rn_value.wrapping_add_signed(imm as i32);

		let result = state.read_halfword(target) as u32;
		state.write_register(rd, result);
	}

	pub(super) fn isa_load_immediate_offset(&mut self, rd: u8, rn: u8, imm: i16) {
		take_state_mut!(state, self);

		let rn_value = state.read_register(rn);

		let target = rn_value.wrapping_add_signed(imm as i32);

		log!("ldr r{rd}, [r{rn}, #{imm:#X}] @ {target:#010X}");

		let result = state.read_word(target);
		state.write_register(rd, result);
	}

	pub(super) fn isa_load_pc(&mut self, rd: u8, imm: u16) {
		// Slightly different from load_immediate_offset
		// due to the target being forced word-aligned.

		take_state_mut!(state, self);

		let rn_value = state.read_register(0xF) & 0b11111111111111111111111111111100;

		let target = rn_value.wrapping_add(imm as u32);

		log!("ldr r{rd}, [pc, #{imm:#X}] @ {target:#010X}");

		let result = state.read_word(target);
		state.write_register(rd, result);
	}

	pub(super) fn isa_store_byte_immediate_offset(&mut self, rd: u8, rn: u8, imm: i16) {
		log!("strb r{rd}, [r{rn}, #{imm:#X}]");

		take_state_mut!(state, self);

		let rn_value = state.read_register(rn);

		let target = rn_value.wrapping_add_signed(imm as i32);

		let result = state.read_register(rd) as u8;
		state.write_byte(target, result);
	}

	pub(super) fn isa_store_byte_register_offset(&mut self, rd: u8, rn: u8, rm: u8) {
		log!("strb r{rd}, [r{rn}, r{rm}]");

		take_state_mut!(state, self);

		let rn_value = state.read_register(rn);
		let rm_value = state.read_register(rm);

		let target = rn_value.wrapping_add(rm_value);

		let result = state.read_register(rd) as u8;
		state.write_byte(target, result);
	}

	pub(super) fn isa_store_halfword(&mut self, rd: u8, rn: u8, imm: i16) {
		log!("strh r{rd}, [r{rn}, #{imm:#X}]");

		take_state_mut!(state, self);

		let rn_value = state.read_register(rn);

		let target = rn_value.wrapping_add_signed(imm as i32);

		let result = state.read_register(rd) as u16;
		state.write_halfword(target, result);
	}

	pub(super) fn isa_store_immediate_offset(&mut self, rd: u8, rn: u8, imm: i16) {
		log!("str r{rd}, [r{rn}, #{imm:#X}]");

		take_state_mut!(state, self);

		let rn_value = state.read_register(rn);

		let target = rn_value.wrapping_add_signed(imm as i32);

		let result = state.read_register(rd);
		state.write_word(target, result);
	}
}