summaryrefslogtreecommitdiff
path: root/Assembler/Assembler.cs
blob: 5df068d83371f177377d00bf2e0440001080fc33 (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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
namespace Q1Emu.Assembler;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

public class Assembler
{
    public readonly BinaryWriter Writer;
    public readonly StringReader Reader;

    private Dictionary<string, u16> _labels        = new();
    private u16                     _addressOffset = 0;

    public Assembler(StringReader reader, BinaryWriter writer)
    {
        this.Reader = reader;
        this.Writer = writer;
    }

    public void Assemble()
    {
        while (this.Reader.ReadLine() is { } line)
        {
            string trimmedLine = line.Split(";")[0].Trim(); // Remove comments
            if (string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith(';'))
                continue;

            if (trimmedLine.StartsWith(".org"))
            {
                // Handle .org directive
                string orgValue = trimmedLine[5..].Trim();
                this._addressOffset = this.GetConstant(orgValue);
                Console.WriteLine($"Setting address offset to {this._addressOffset:X4}");
            }

            else if (trimmedLine.EndsWith(":"))
                this._labels[trimmedLine[..^1]] = (u16) (this.Writer.BaseStream.Position + this._addressOffset);
            
            else
                this.ParseInstruction(trimmedLine);
        }
    }

    private void ParseInstruction(string instruction)
    {
        string[] parts = instruction.Split(' ', 2);
        string opcode = parts[0].ToUpperInvariant();

        parts = parts.Length > 1 ? parts[1].Split(',') : [];
        if (parts.Length > 2)
            throw new ArgumentException($"Invalid instruction format: {instruction}");

        u8 opcodeValue = this.ParseOpcode(opcode);

        u8 m1 = 0, m2 = 0;
        if (parts.Length > 0)
            m1 = this.ParseMode(parts[0]);
        if (parts.Length > 1)
            m2 = this.ParseMode(parts[1]);

        Console.WriteLine($"Parsed instruction: {opcode} with modes {m1:X2}, {m2:X2} (at address {this.Writer.BaseStream.Position + this._addressOffset:X4})");
        this.WriteInstruction(opcodeValue, m1, m2);
        if (parts.Length > 0)
            this.WriteMode(m1, parts[0]);
        if (parts.Length > 1)
            this.WriteMode(m2, parts[1]);
    }

    private u8 ParseOpcode(string opcode)
    {
        return opcode switch
        {
            "NOP" => 0x00, "JMP"  => 0x00, "MOV" => 0x00,
            "RET" => 0x01, "CALL" => 0x01, "CMP" => 0x02,
            "BR"  => 0x02, "BZ"   => 0x02, "LT"  => 0x02,
            "GT"  => 0x03,
            "INC" => 0x08, "ADD" => 0x08,
            "DEC" => 0x09,
            "MUL" => 0x0B,
            _     => throw new ArgumentException($"Unknown opcode: {opcode}")
        };
    }

    private u8 ParseMode(string mode)
    {
        mode = mode.Trim();

        bool IsRegister(string text)
        {
            if (text.Length > 1 && text[0] == 'V')
                return true;

            string[] hardcodedRegisters =
            [
                "AX",
                "AH",
                "AL",
                "DX",
                "DH",
                "DL",
                "LX",
                "PC",
                "SP",
            ];

            return hardcodedRegisters.Contains(text);
        }

        if (this._labels.ContainsKey(mode))
        {
            return 0x01; // Immediate
        }

        if (mode.StartsWith("@"))
        {
            return 0x03; // Indirect
        }

        if (mode.StartsWith("[") && mode.EndsWith("]"))
        {
            string inner = mode[1..^1].Trim();
            if (inner.Contains("+"))
                return 0x07; // Register indirect with offset

            if (IsRegister(inner))
                return 0x06; // Register indirect

            return 0x02; // Direct
        }

        if (IsRegister(mode))
            return 0x05; // Register

        return 0x01; // Immediate
    }

    private void WriteInstruction(u8 opcode, u8 m1, u8 m2)
    {
        Write(opcode);
        Write((u8) (m1 << 4 | m2)); // Combine m1 and m2 into a single byte
    }

    private void WriteMode(u8 mode, string operand)
    {
        operand = operand.Trim();

        switch (mode)
        {
            case 0x00: // Implicit
                // No additional data needed
                break;

            case 0x01: // Immediate
                u16 immediateValue = this.GetConstant(operand);
                this.Write(immediateValue);
                break;

            case 0x02:                                                // Direct
                u16 directAddress = this.GetConstant(operand); // Remove brackets
                this.Write(directAddress);
                break;

            case 0x03:                                                  // Indirect
                u16 indirectAddress = this.GetConstant(operand); // Remove brackets and @
                this.Write(indirectAddress);
                break;

            case 0x04: // Relative
                i16 relativeOffset = this.GetSignedConstant(operand);
                Write(relativeOffset);
                break;

            case 0x05: // Register
                u8 regIndex = this.GetRegisterIndex(operand);
                Write(regIndex);
                break;

            case 0x06:                                                       // Register Indirect
                u8 indirectRegIndex = this.GetRegisterIndex(operand); // Remove brackets
                Write(indirectRegIndex);
                break;

            case 0x07: // Register Indirect with Offset
                u8 offsetRegIndex = this.GetRegisterIndex(operand[1..^1].Split('+')[0].Trim());
                i16 offsetValue = this.GetSignedConstant(operand[1..^1].Split('+')[1].Trim());
                Write(offsetRegIndex);
                Write(offsetValue);
                break;
        }
    }
    private i16 GetSignedConstant(string value)
    {
        value = this.Clean(value);
        
        if (value.StartsWith("$"))
        {
            // Hexadecimal offset
            return Convert.ToInt16(value[1..], 16);
        }
        else if (i16.TryParse(value, out i16 result))
            // Decimal offset
            return result;
        else
        {
            throw new ArgumentException($"Invalid offset value: {value}");
        }
    }
    private u8 GetRegisterIndex(string reg)
    {
        reg = this.Clean(reg);
        
        if (reg.StartsWith("V"))
            if (u8.TryParse(reg[1..], out u8 index) && index < 0x3F)
                return index;

        return reg switch
        {
            "AX" => 0x40,
            "AH" => 0x41,
            "AL" => 0x42,
            "DX" => 0x43,
            "DH" => 0x44,
            "DL" => 0x45,
            "LX" => 0x46,
            "PC" => 0x50,
            "SP" => 0x51,
            _    => throw new ArgumentException($"Invalid register index: {reg}")
        };
    }
    private u16 GetConstant(string value)
    {
        value = this.Clean(value);
        
        if (this._labels.ContainsKey(value))
        {
            // Label reference
            return this._labels[value];
        }
        if (value.StartsWith("$"))
        {
            // Hexadecimal constant (backwards) $1FFF
            u16 x = Convert.ToUInt16(value[1..], 16);
            return x;
        }
        else if (u16.TryParse(value, out u16 result))
            // Decimal constant
            return result;
        else
        {
            throw new ArgumentException($"Invalid constant value: {value}");
        }
    }

    private string Clean(string value)
    {
        if (value.StartsWith("@"))
            value = value[1..]; // Remove @ for indirect addressing
        
        if (value.StartsWith("[") && value.EndsWith("]"))
            value = value[1..^1].Trim(); // Remove brackets for direct addressing
        
        return value.Trim();
    }
    
    private void Write(u16 value)
    {
        u16 swappedValue = (u16) ((value >> 8) | (value << 8));
        this.Writer.Write(swappedValue);
    }
    
    private void Write(u8 value)
    {
        this.Writer.Write(value);
    }
    
    private void Write(i16 value)
    {
        u16 swappedValue = (u16) ((value >> 8) | (value << 8));
        this.Writer.Write(swappedValue);
    }
}