summaryrefslogtreecommitdiff
path: root/src/arm32/instruction_codec/encode_thumb.rs
blob: f2fa0b80479b2c0d5716aa118abd4ad9230839b2 (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
// 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::{assert_or_err, Error, Result};
use crate::arm32::{
	Instruction,
	InstructionCodec,
	Predicate,
	Shifter,
	ThumbOpcode,
};

impl InstructionCodec {
	/// Encodes the given Thumb instruction.
	///
	/// # Errors
	///
	/// If the given instruction cannot be encoded for Thumb, an error is returned.
	pub fn encode_thumb(&mut self, instruction: Instruction) -> Result<ThumbOpcode> {
		use Instruction::*;

		let mut opcode = 0b00000000_00000000_u16;

		match instruction {
			Move {
				predicate,
				destination,
				source,
				s,
			} => {
				assert_or_err!(predicate == Predicate::Always, Error::IllegalPredicate);

				if s.is_on() {
					if let Shifter::Immediate { source } = source {
						assert_or_err!(destination.is_low(), Error::IllegalRegister);
						assert_or_err!(source <= 0xFF, Error::IllegalRegister);

						opcode |= 0b00100000_00000000;
						opcode |= source as u16;
						opcode |= (destination as u16) << 0x8;

					} else if let Shifter::LogicalShiftLeftImmediate { source, shift: 0x0 } = source {
						assert_or_err!(destination.is_low(), Error::IllegalRegister);
						assert_or_err!(source.is_low(), Error::IllegalRegister);

						opcode |= 0b00100000_00000000;
						opcode |= destination as u16;
						opcode |= (source as u16) << 0x3;
					} else {
						return Err(Error::IllegalInstruction);
					}
				} else if let Shifter::LogicalShiftLeftImmediate { source, shift: 0x0 } = source {
					opcode |= 0b01000110_00000000;

					let h0 = destination.is_high();
					let h1 = source.is_high();

					opcode |= destination as u16 & 0b00000000_00000111;
					opcode |= (source as u16 & 0b00000000_00000111) << 0x3;
					opcode |= u16::from(h0) << 0x6;
					opcode |= u16::from(h1) << 0x7;
				} else {
					// TODO: Shifters &c.
					todo!();
				}
			}

			_ => return Err(Error::IllegalInstruction),
		}

		self.address += ThumbOpcode::SIZE;
		Ok(opcode.into())
	}
}