summaryrefslogtreecommitdiff
path: root/Q1.core/Arch/InstructionEncoding.cs
blob: 479d61f02876b46f624cf7cbd8c82b1556e14e3f (plain)
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
namespace Q1.core.Arch;

public static class InstructionEncoding
{
    public static void Decode(u16 instruction, out u8 opcode, out u8 m1, out u8 m2, out bool word)
    {
        word   = ((instruction     >> 15) & 0x1) != 0; // top bit
        opcode = (u8)((instruction >> 8)  & 0x7F);     // 7 clean bits

        m1 = (u8)((instruction >> 4) & 0x0F);
        m2 = (u8)(instruction        & 0x0F);
    }
    
    public static u16 Encode(u8 opcode, u8 m1, u8 m2, bool word)
    {
        u16 instruction = 0;

        instruction |= (u16)((opcode & 0x7F) << 8);  // 7-bit opcode
        instruction |= (u16)((word ? 1 : 0)  << 15); // top bit flag

        instruction |= (u16)((m1 & 0x0F) << 4);
        instruction |= (u16)(m2 & 0x0F);

        return instruction;
    }
}