Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
328 lines (287 loc) · 10.2 KB
/
Copy pathmain.js
File metadata and controls
328 lines (287 loc) · 10.2 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// ============================================================================
// MAIN APPLICATION CONTROLLER
// ============================================================================
import { parseBaseLogic } from './parsing.js';
import { validateTypeChange, canExpand } from './validation.js';
import {
getGroupLines,
isGroupLeader,
recalculateLineAndGroupIds,
linkCondition,
unlinkCondition,
canLinkCondition as canLinkConditionUtil,
autoLinkAddressFlags
} from './groups.js';
import {
addBitfieldCondition,
removeBitfieldCondition,
addConditionAtIndex,
copyCondition,
clearBitfieldConditions,
updateBitfieldCondition,
syncBitfieldFromText
} from './bitfield-operations.js';
import {
updateExpansionField,
updateLineConfig,
cancelExpansion,
cancelLineCustomization,
confirmExpansion,
convertBitfieldConditionToText
} from './expansion-system.js';
import {
renderBitfieldConditions,
flashTypeDropdown,
formatConditionDisplay
} from './html-renderer.js';
// ============================================================================
// GLOBAL STATE
// ============================================================================
let bitfieldConditions = [];
let bitfieldExpansions = new Map();
let linkGroupColors = new Map();
// ============================================================================
// MAIN APPLICATION FUNCTIONS
// ============================================================================
/**
* Updates the GUI from the base logic textarea
*/
export function updateGuiFromText() {
const parsed = parseBaseLogic();
syncBitfieldFromText(
bitfieldConditions,
bitfieldExpansions,
parsed,
() => autoLinkAddressFlags(bitfieldConditions, linkGroupColors)
);
renderBitfieldConditions(
bitfieldConditions,
bitfieldExpansions,
linkGroupColors,
(lineId) => linkCondition(bitfieldConditions, linkGroupColors, lineId),
(lineId) => unlinkCondition(bitfieldConditions, lineId),
(lineId) => canLinkConditionUtil(bitfieldConditions, lineId),
(index) => addConditionAtIndex(bitfieldConditions, index),
(lineId) => copyCondition(bitfieldConditions, lineId),
(lineId) => removeBitfieldCondition(bitfieldConditions, bitfieldExpansions, linkGroupColors, lineId),
expandCondition,
reopenExpansion
);
}
/**
* Recomputes the expand state for a condition
* @param {number} lineId - The line ID
*/
export function recomputeExpandState(lineId) {
const condition = bitfieldConditions.find((c) => c.lineId === lineId);
if (!condition) return;
const btn = document.querySelector(
`.bitfield-condition-row[data-id="${lineId}"] .expand-btn`,
);
if (btn) {
btn.disabled = !canExpand(condition);
}
}
/**
* Reopens an expansion
* @param {number} lineId - The line ID
*/
export function reopenExpansion(lineId) {
const condition = bitfieldConditions.find((c) => c.lineId === lineId);
if (condition) {
// Reset ALL conditions in the group
const groupLines = getGroupLines(bitfieldConditions, condition.groupId);
groupLines.forEach((groupCondition) => {
groupCondition.expanded = false;
groupCondition.expandedLines = [];
});
}
delete bitfieldExpansions[lineId];
updateGuiFromText();
}
// ============================================================================
// WRAPPER FUNCTIONS FOR GLOBAL ACCESS
// ============================================================================
// Bitfield Operations
export function addBitfieldConditionWrapper() {
addBitfieldCondition(bitfieldConditions);
updateGuiFromText();
}
export function removeBitfieldConditionWrapper(lineId) {
removeBitfieldCondition(bitfieldConditions, bitfieldExpansions, linkGroupColors, lineId);
updateGuiFromText();
}
export function addConditionAtIndexWrapper(index) {
addConditionAtIndex(bitfieldConditions, index);
updateGuiFromText();
}
export function copyConditionWrapper(lineId) {
copyCondition(bitfieldConditions, lineId);
updateGuiFromText();
}
export function clearBitfieldConditionsWrapper() {
clearBitfieldConditions(bitfieldConditions, bitfieldExpansions);
updateGuiFromText();
}
export function updateBitfieldConditionWrapper(lineId, field, value) {
updateBitfieldCondition(
bitfieldConditions,
bitfieldExpansions,
lineId,
field,
value,
recomputeExpandState,
expandCondition
);
// Only render in-place for fields that affect UI structure
const uiAffectingFields = ['flag', 'type', 'cmp'];
if (uiAffectingFields.includes(field)) {
// Render directly without re-parsing from textarea to preserve manual edits
renderBitfieldConditions(
bitfieldConditions,
bitfieldExpansions,
linkGroupColors,
(lineId) => linkCondition(bitfieldConditions, linkGroupColors, lineId),
(lineId) => unlinkCondition(bitfieldConditions, lineId),
(lineId) => canLinkConditionUtil(bitfieldConditions, lineId),
(lineId) => addConditionAtIndex(bitfieldConditions, lineId),
(lineId) => copyCondition(bitfieldConditions, lineId),
(lineId) => removeBitfieldCondition(bitfieldConditions, bitfieldExpansions, linkGroupColors, lineId),
expandCondition,
reopenExpansion
);
}
}
// Group Management
export function linkConditionWrapper(lineId) {
linkCondition(bitfieldConditions, linkGroupColors, lineId);
updateGuiFromText();
}
export function unlinkConditionWrapper(lineId) {
unlinkCondition(bitfieldConditions, lineId);
updateGuiFromText();
}
export function canLinkConditionWrapper(lineId) {
return canLinkConditionUtil(bitfieldConditions, lineId);
}
// Validation
export function validateTypeChangeWrapper(lineId, newType) {
const condition = bitfieldConditions.find((c) => c.lineId === lineId);
if (!condition) return;
if (!validateTypeChange(condition.flag, newType)) {
// Revert to Mem and show red flash
updateBitfieldConditionWrapper(lineId, 'type', 'Mem');
flashTypeDropdown(lineId);
return;
}
updateBitfieldConditionWrapper(lineId, 'type', newType);
}
// Expansion System (simplified - full expansion logic would be too large for this refactor)
export function expandCondition(lineId) {
// This is a placeholder - the full expansion logic would be quite complex
// For now, we'll just log that expansion was requested
console.log('Expansion requested for line:', lineId);
}
export function updateExpansionFieldWrapper(lineId, field, value) {
updateExpansionField(bitfieldExpansions, lineId, field, value);
}
export function updateLineConfigWrapper(expansionId, lineIndex, field, value) {
updateLineConfig(bitfieldExpansions, expansionId, lineIndex, field, value);
}
export function cancelExpansionWrapper(lineId) {
cancelExpansion(bitfieldExpansions, lineId);
updateGuiFromText();
}
export function cancelLineCustomizationWrapper(expansionId, lineIndex) {
cancelLineCustomization(bitfieldExpansions, expansionId, lineIndex, expandCondition);
}
export function confirmExpansionWrapper(lineId) {
confirmExpansion(
bitfieldExpansions,
bitfieldConditions,
lineId,
// These would be the full expansion generation functions
() => '', // generateArithmeticLine placeholder
() => [], // generateCustomLines placeholder
convertBitfieldConditionToText,
() => [], // applyDeltaMemCheck placeholder
() => [] // applyAndOrNextCheck placeholder
);
updateGuiFromText();
}
// Utility Functions
export function copyToClipboard(text) {
navigator.clipboard
.writeText(text)
.then(() => {
console.log('Copied to clipboard');
})
.catch((err) => {
console.error('Failed to copy: ', err);
});
}
export function generateBitfieldLogic() {
const compressEnabled = document.getElementById('compressEnabled').checked;
// Collect all conditions including expanded ones
let allLines = [];
bitfieldConditions.forEach((condition) => {
if (condition.expanded && condition.expandedLines.length > 0) {
allLines.push(...condition.expandedLines);
} else {
allLines.push(convertBitfieldConditionToText(condition));
}
});
// Generate the final logic string
let logicString = allLines.join('_');
if (compressEnabled) {
// Apply compression logic here if needed
// For now, just return the uncompressed version
}
// Update the base logic textarea
document.getElementById('baseLogic').value = logicString;
// Re-parse and auto-link
updateGuiFromText();
// Copy to clipboard
copyToClipboard(logicString);
}
// ============================================================================
// GLOBAL WINDOW BINDINGS
// ============================================================================
// Make functions globally accessible for HTML onclick handlers
window.updateGuiFromText = updateGuiFromText;
window.addBitfieldCondition = addBitfieldConditionWrapper;
window.removeBitfieldCondition = removeBitfieldConditionWrapper;
window.addConditionAtIndex = addConditionAtIndexWrapper;
window.copyCondition = copyConditionWrapper;
window.clearBitfieldConditions = clearBitfieldConditionsWrapper;
window.updateBitfieldCondition = updateBitfieldConditionWrapper;
window.linkCondition = linkConditionWrapper;
window.unlinkCondition = unlinkConditionWrapper;
window.canLinkCondition = canLinkConditionWrapper;
window.validateTypeChange = validateTypeChangeWrapper;
window.expandCondition = expandCondition;
window.reopenExpansion = reopenExpansion;
window.recomputeExpandState = recomputeExpandState;
window.updateExpansionField = updateExpansionFieldWrapper;
window.updateLineConfig = updateLineConfigWrapper;
window.cancelExpansion = cancelExpansionWrapper;
window.cancelLineCustomization = cancelLineCustomizationWrapper;
window.confirmExpansion = confirmExpansionWrapper;
window.copyToClipboard = copyToClipboard;
window.generateBitfieldLogic = generateBitfieldLogic;
// ============================================================================
// INITIALIZATION
// ============================================================================
/**
* Initializes the application when DOM is ready
*/
function initializeApp() {
// Parse initial content from textarea if any
updateGuiFromText();
}
// Initialize when DOM is loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initializeApp);
} else {
initializeApp();
}