Repository navigation
Expand file tree
/
Copy pathobject.cpp
More file actions
106 lines (90 loc) · 2.82 KB
/
Copy pathobject.cpp
File metadata and controls
106 lines (90 loc) · 2.82 KB
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
/*
* Copyright (C) 2026 Clownacy
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "object.h"
#include <iostream>
#include "utilities/io.h"
#include "version.h"
void Object::Writer::TerminatePreviousRecordSegment()
{
if (previous_segment_length_output_position != 0)
{
if (segment_length == 0)
{
/* Previous segment contained nothing; make the new segment overwrite it. */
output.stream.seekp(previous_segment_length_output_position);
output.stream.seekp(-5, output.stream.cur);
}
else
{
/* Write length of previous record. */
const auto previous_output_position = output.stream.tellp();
output.stream.seekp(previous_segment_length_output_position);
output.WriteU16LE(segment_length);
output.stream.seekp(previous_output_position);
}
}
}
void Object::Writer::WriteRecordSegment()
{
TerminatePreviousRecordSegment();
/* CPU architecture (68000). */
output.WriteU8(0x01);
/* Start address. */
output.WriteU32LE(segment_position);
/* Length (placeholder). */
previous_segment_length_output_position = output.stream.tellp();
output.WriteU16LE(0x0000);
}
void Object::Writer::WriteHeader()
{
/* Object file magic number. */
output.WriteU16BE(0x8914);
/* Begin first segment. */
WriteRecordSegment();
}
void Object::Writer::WriteFooter()
{
/* Finalise the segment record data. */
TerminatePreviousRecordSegment();
/* Write terminating record. */
output.WriteU8(0x00);
output.stream << CLOWNASSEMBLER_VERSION_STRING;
}
void Object::Writer::WriteByte(const unsigned int value, const bool doing_fixup_pass)
{
if (segment_length == 0xFFFF)
{
segment_position += segment_length;
/* Avoid rewriting the record header when doing fix-ups,
as the length field will be left blank. */
/* TODO: Make this boolean less dumb. */
if (doing_fixup_pass)
output.stream.seekp(1 + 4 + 2, output.stream.cur);
else
WriteRecordSegment();
segment_length = 0;
}
++segment_length;
output.WriteU8(value);
}
void Object::Writer::BeginNewSegment(const std::size_t position)
{
/* Begin a new segment! */
segment_position = position;
WriteRecordSegment();
segment_length = 0;
}