summaryrefslogtreecommitdiff
path: root/src/arm32/instruction_codec/decode_thumb.rs
blob: fb1e8eeabda6e5ab0782b0c1d7b8accc4855d5e7 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2024 Gabriel Bjørnager Jensen.
//
// This file is part of Pollex.
//
// Pollex is free software: you can redistribute it
// and/or modify it under the terms of the GNU Af-
// fero General Public License as published by the
// Free Software Foundation, either version 3 of
// the License, or (at your option) any later ver-
// sion.
//
// Pollex 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 Af-
// fero General Public License along with Pollex.
// If not, see <https://www.gnu.org/licenses/>.

use crate::Result;
use crate::arm32::{
	Instruction,
	InstructionCodec,
	Predicate,
	Register,
	Sflag,
	Shifter,
	ThumbOpcode,
};

use core::num::Wrapping;

impl InstructionCodec {
	/// Decodes the given Thumb opcode.
	///
	/// # Errors
	///
	/// If the provided opcode is invalid (i.e. does not match any known pattern), an [InvalidOpcode](crate::Error::InvalidOpcode) error is returned.
	pub fn decode_thumb(&mut self, opcode: ThumbOpcode) -> Result<Instruction> {
		use Instruction::*;

		let opcode = opcode.to_u16();

		macro_rules! match_bits {
			($mask:expr) => {{
				opcode & $mask == $mask
			}};
		}

		let instruction = if match_bits!(0b11010000_00000000) {
			let predicate = (opcode & 0b00001111_00000000).wrapping_shr(0x8) as u8;
			let immediate = u32::from(opcode & 0b00000000_11111111);

			if predicate == 0b1111 {
				SoftwareInterrupt {
					predicate: Predicate::Always,
					immediate,
				}
			} else {
				let predicate = Predicate::from_u8(predicate).unwrap();

				let mut immediate = (immediate.wrapping_shl(0x18) as i32).wrapping_shl(0x17) as u32;
				immediate = immediate.wrapping_add(self.address.0).wrapping_add(0x4);

				Branch {
					predicate,
					immediate,
				}
			}
		} else if match_bits!(0b00011000_00000000) {
			let destination = Register::from_u8((opcode & 0b00000000_00000111) as u8).unwrap();
			let base        = Register::from_u8((opcode & 0b00000000_00111000).wrapping_shr(0x3) as u8).unwrap();

			let subtract = opcode & 0b00000010_00000000 != 0x0;

			let immediate_source = opcode & 0b00000100_00000000 != 0x0;
			if immediate_source {
				let source = u32::from((opcode & 0b00000001_11000000).wrapping_shr(0x6));

				if subtract {
					Subtract {
						predicate:   Predicate::Always,
						destination,
						base,
						source:      Shifter::Immediate(source),
						s:           Sflag::On,
					}
				} else {
					Add {
						predicate:   Predicate::Always,
						destination,
						base,
						source:      Shifter::Immediate(source),
						s:           Sflag::On,
					}
				}
			} else {
				// register source

				let source = Register::from_u8((opcode & 0b00000001_11000000).wrapping_shr(0x6) as u8).unwrap();

				if subtract {
					Subtract {
						predicate:   Predicate::Always,
						destination,
						base,
						source:      Shifter::from_register(source),
						s:           Sflag::On,
					}
				} else {
					Add {
						predicate:   Predicate::Always,
						destination,
						base,
						source:      Shifter::from_register(source),
						s:           Sflag::On,
					}
				}
			}
		} else if match_bits!(0b01000111_00000000) {
			let source = Register::from_u8((opcode & 0b00000000_01111000).wrapping_shr(0x3) as u8).unwrap();
			let l      = opcode & 0b00000000_10000000 != 0x0;

			if l {
				BranchLinkExchange {
					predicate: Predicate::Always,
					source:    Shifter::from_register(source),
				}
			} else {
				BranchExchange {
					predicate: Predicate::Always,
					source,
				}
			}
		} else {
			let _code = (opcode & 0b00011000_00000000).wrapping_shr(0xB) as u8;

			todo!();
		};

		self.address += Wrapping(ThumbOpcode::SIZE);
		Ok(instruction)
	}
}