1 package net.mograsim.plugin.tables.mi;
4 import java.io.IOException;
5 import java.util.Arrays;
6 import java.util.Optional;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.jface.viewers.ColumnLabelProvider;
10 import org.eclipse.jface.viewers.EditingSupport;
11 import org.eclipse.jface.viewers.TableViewerColumn;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.FileDialog;
17 import org.eclipse.swt.widgets.Table;
18 import org.eclipse.swt.widgets.TableColumn;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IEditorSite;
21 import org.eclipse.ui.IPathEditorInput;
22 import org.eclipse.ui.PartInitException;
23 import org.eclipse.ui.part.EditorPart;
25 import net.mograsim.machine.Machine;
26 import net.mograsim.machine.MemoryObserver;
27 import net.mograsim.machine.mi.MicroInstructionDefinition;
28 import net.mograsim.machine.mi.MicroInstructionMemory;
29 import net.mograsim.machine.mi.MicroInstructionMemoryParseException;
30 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
31 import net.mograsim.machine.mi.parameters.MnemonicFamily;
32 import net.mograsim.machine.mi.parameters.ParameterClassification;
33 import net.mograsim.plugin.MachineContext;
34 import net.mograsim.plugin.MachineContext.ContextObserver;
35 import net.mograsim.plugin.tables.AddressLabelProvider;
36 import net.mograsim.plugin.tables.DisplaySettings;
37 import net.mograsim.plugin.tables.LazyTableViewer;
38 import net.mograsim.plugin.tables.RadixSelector;
39 import net.mograsim.plugin.util.DropDownMenu;
40 import net.mograsim.plugin.util.DropDownMenu.DropDownEntry;
42 public class InstructionView extends EditorPart implements ContextObserver, MemoryObserver
44 private String saveLoc = null;
45 private LazyTableViewer viewer;
46 private TableViewerColumn[] columns = new TableViewerColumn[0];
47 private MicroInstructionDefinition miDef;
48 private MicroInstructionMemory memory;
49 private DisplaySettings displaySettings;
50 private InstructionTableContentProvider provider;
51 private int highlighted = 0;
52 private boolean dirty = false;
54 @SuppressWarnings("unused")
56 public void createPartControl(Composite parent)
58 provider = new InstructionTableContentProvider();
59 GridLayout layout = new GridLayout(3, false);
60 setupMenuButtons(parent);
62 displaySettings = new DisplaySettings();
63 new RadixSelector(parent, displaySettings);
65 parent.setLayout(layout);
66 viewer = new LazyTableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER | SWT.VIRTUAL);
68 Table table = viewer.getTable();
69 table.setHeaderVisible(true);
70 table.setLinesVisible(true);
71 viewer.setUseHashlookup(true);
72 viewer.setContentProvider(provider);
73 getSite().setSelectionProvider(viewer);
75 GridData viewerData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
76 viewerData.horizontalSpan = 3;
77 viewer.getTable().setLayoutData(viewerData);
79 displaySettings.addObserver(() -> viewer.refresh());
80 MachineContext.getInstance().registerObserver(this);
81 setMachine(Optional.ofNullable(MachineContext.getInstance().getMachine()));
84 public void highlight(int index)
86 viewer.highlightRow(highlighted, false);
87 viewer.highlightRow(index, true);
88 viewer.getTable().setTopIndex(index);
91 @SuppressWarnings("unused")
92 private void setupMenuButtons(Composite parent)
94 DropDownEntry open = new DropDownEntry("Open", (e) ->
96 FileDialog d = new FileDialog(parent.getShell(), SWT.NONE);
98 String filename = d.getFileName();
99 if (!filename.equals(""))
100 open(d.getFilterPath() + File.separator + filename);
103 DropDownEntry save = new DropDownEntry("Save", (e) ->
106 openSaveAsDialog(parent);
109 DropDownEntry saveAs = new DropDownEntry("SaveAs", (e) ->
111 openSaveAsDialog(parent);
114 new DropDownMenu(parent, "File", open, save, saveAs);
117 private void openSaveAsDialog(Composite parent)
119 FileDialog d = new FileDialog(parent.getShell(), SWT.SAVE);
121 String filename = d.getFileName();
122 if (!filename.equals(""))
123 saveLoc = d.getFilterPath() + File.separator + filename;
126 public void bindMicroInstructionMemory(MicroInstructionMemory memory)
129 this.memory = memory;
130 viewer.setInput(memory);
131 this.miDef = memory.getDefinition().getMicroInstructionDefinition();
132 this.memory.registerObserver(this);
136 private void deleteColumns()
138 for (TableViewerColumn col : columns)
139 col.getColumn().dispose();
142 private void createColumns()
144 int size = miDef.size();
145 columns = new TableViewerColumn[size + 1];
147 TableViewerColumn col = createTableViewerColumn("Address", generateLongestHexStrings(12));
149 col.setLabelProvider(new AddressLabelProvider());
151 int bit = miDef.sizeInBits();
152 ParameterClassification[] classes = miDef.getParameterClassifications();
154 for (int i = 0; i < size; i++)
156 int startBit = bit - 1;
157 int endBit = bit = bit - classes[i].getExpectedBits();
158 String name = startBit == endBit ? Integer.toString(startBit) : startBit + "..." + endBit;
160 String[] longestPossibleContents;
161 switch (classes[i].getExpectedType())
163 case INTEGER_IMMEDIATE:
164 longestPossibleContents = generateLongestHexStrings(classes[i].getExpectedBits());
166 case BOOLEAN_IMMEDIATE:
168 longestPossibleContents = ((MnemonicFamily) classes[i]).getStringValues();
171 longestPossibleContents = new String[0];
175 col = createTableViewerColumn(name, longestPossibleContents);
176 columns[i + 1] = col;
177 createEditingAndLabel(col, miDef, i);
181 private static final String[] HEX_DIGITS = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
183 private static String[] generateLongestHexStrings(int bitWidth)
185 return Arrays.stream(HEX_DIGITS).map(s -> "0x" + s.repeat((bitWidth + 3) / 4)).toArray(String[]::new);
188 private void createEditingAndLabel(TableViewerColumn col, MicroInstructionDefinition miDef, int index)
190 ParameterClassification parameterClassification = miDef.getParameterClassifications()[index];
191 EditingSupport support;
192 ColumnLabelProvider provider;
193 switch (parameterClassification.getExpectedType())
195 case BOOLEAN_IMMEDIATE:
196 support = new BooleanEditingSupport(viewer, miDef, index);
197 provider = new ParameterLabelProvider(index);
199 case INTEGER_IMMEDIATE:
200 support = new IntegerEditingSupport(viewer, miDef, index, displaySettings, this.provider);
201 provider = new IntegerColumnLabelProvider(displaySettings, index);
204 support = new MnemonicEditingSupport(viewer, miDef, index, this.provider);
205 provider = new ParameterLabelProvider(index);
208 throw new IllegalStateException(
209 "Unable to create EditingSupport for unknown ParameterType " + parameterClassification.getExpectedType());
211 col.setEditingSupport(support);
212 col.setLabelProvider(provider);
213 col.getColumn().setToolTipText(miDef.getParameterDescription(index).orElse(""));
216 private TableViewerColumn createTableViewerColumn(String title, String... longestPossibleContents)
218 TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
219 TableColumn column = viewerColumn.getColumn();
221 for (String s : longestPossibleContents)
225 if (column.getWidth() > maxWidth)
226 maxWidth = column.getWidth();
228 column.setText(title);
230 if (column.getWidth() < maxWidth)
231 column.setWidth(maxWidth);
232 column.setResizable(true);
233 column.setMoveable(false);
237 private void open(String file)
241 System.err.println("Failed to parse MicroprogrammingMemory from File. No MicroInstructionDefinition assigned.");
246 MicroInstructionMemoryParser.parseMemory(memory, file);
250 catch (IOException | MicroInstructionMemoryParseException e)
256 private void save(String file)
260 System.err.println("Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned.");
265 MicroInstructionMemoryParser.write(memory, file);
267 catch (IOException e)
274 public void setFocus()
276 viewer.getControl().setFocus();
280 public void setMachine(Optional<Machine> machine)
282 if (machine.isPresent())
284 Machine actualMachine = machine.get();
285 bindMicroInstructionMemory(actualMachine.getMicroInstructionMemory());
290 public void doSave(IProgressMonitor progressMonitor)
292 IEditorInput input = getEditorInput();
293 if (input instanceof IPathEditorInput)
295 IPathEditorInput pathInput = (IPathEditorInput) input;
296 save(pathInput.getPath().toOSString());
298 firePropertyChange(PROP_DIRTY);
303 public void doSaveAs()
309 public void init(IEditorSite site, IEditorInput input) throws PartInitException
313 if (input instanceof IPathEditorInput)
315 IPathEditorInput pathInput = (IPathEditorInput) input;
316 open(pathInput.getPath().toOSString());
321 public boolean isDirty()
327 public boolean isSaveAsAllowed()
333 public void update(long address)
336 firePropertyChange(PROP_DIRTY);