X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.plugin.core%2Fsrc%2Fnet%2Fmograsim%2Fplugin%2Ftables%2Fmi%2FInstructionView.java;h=37aab63806eef68a5e2bc10aab4a075846df291f;hb=363c9377b3c542c87fc5e7f57674751439246dbd;hp=b14e42db15ee434baef6daea3705e0e79f46e768;hpb=a9d62f9ce8942a7c70ff0a7c6648ca27a312521a;p=Mograsim.git diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/InstructionView.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/InstructionView.java index b14e42db..37aab638 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/InstructionView.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/InstructionView.java @@ -1,270 +1,121 @@ package net.mograsim.plugin.tables.mi; -import java.io.File; import java.io.IOException; -import java.util.Arrays; -import java.util.Optional; +import java.io.InputStream; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.jface.viewers.ColumnLabelProvider; -import org.eclipse.jface.viewers.EditingSupport; -import org.eclipse.jface.viewers.TableViewerColumn; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.FileDialog; -import org.eclipse.swt.widgets.Table; -import org.eclipse.swt.widgets.TableColumn; +import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorSite; -import org.eclipse.ui.IPathEditorInput; +import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.EditorPart; -import net.mograsim.machine.Machine; -import net.mograsim.machine.MemoryObserver; -import net.mograsim.machine.mi.MicroInstructionDefinition; +import net.mograsim.machine.Memory.MemoryCellModifiedListener; import net.mograsim.machine.mi.MicroInstructionMemory; +import net.mograsim.machine.mi.MicroInstructionMemory.ActiveMicroInstructionChangedListener; import net.mograsim.machine.mi.MicroInstructionMemoryParseException; import net.mograsim.machine.mi.MicroInstructionMemoryParser; -import net.mograsim.machine.mi.parameters.MnemonicFamily; -import net.mograsim.machine.mi.parameters.ParameterClassification; import net.mograsim.plugin.MachineContext; -import net.mograsim.plugin.MachineContext.ContextObserver; -import net.mograsim.plugin.tables.AddressLabelProvider; import net.mograsim.plugin.tables.DisplaySettings; import net.mograsim.plugin.tables.LazyTableViewer; import net.mograsim.plugin.tables.RadixSelector; -import net.mograsim.plugin.util.DropDownMenu; -import net.mograsim.plugin.util.DropDownMenu.DropDownEntry; -public class InstructionView extends EditorPart implements ContextObserver, MemoryObserver +public class InstructionView extends EditorPart implements MemoryCellModifiedListener, ActiveMicroInstructionChangedListener { - private String saveLoc = null; - private LazyTableViewer viewer; - private TableViewerColumn[] columns = new TableViewerColumn[0]; - private MicroInstructionDefinition miDef; - private MicroInstructionMemory memory; - private DisplaySettings displaySettings; private InstructionTableContentProvider provider; private int highlighted = 0; private boolean dirty = false; + private MicroInstructionMemory memory; + private InstructionTable table; @SuppressWarnings("unused") @Override public void createPartControl(Composite parent) { - provider = new InstructionTableContentProvider(); + provider = new InstructionTableLazyContentProvider(); GridLayout layout = new GridLayout(3, false); - setupMenuButtons(parent); + parent.setLayout(layout); - displaySettings = new DisplaySettings(); + DisplaySettings displaySettings = new DisplaySettings(); new RadixSelector(parent, displaySettings); - parent.setLayout(layout); - viewer = new LazyTableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER | SWT.VIRTUAL); + addActivationButton(parent); - Table table = viewer.getTable(); - table.setHeaderVisible(true); - table.setLinesVisible(true); - viewer.setUseHashlookup(true); - viewer.setContentProvider(provider); - getSite().setSelectionProvider(viewer); + table = new InstructionTable(parent, displaySettings); + table.setContentProvider(provider); + table.bindMicroInstructionMemory(memory); GridData viewerData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH); viewerData.horizontalSpan = 3; - viewer.getTable().setLayoutData(viewerData); - - displaySettings.addObserver(() -> viewer.refresh()); - MachineContext.getInstance().registerObserver(this); - setMachine(Optional.ofNullable(MachineContext.getInstance().getMachine())); + table.getTableViewer().getTable().setLayoutData(viewerData); } public void highlight(int index) { - viewer.highlightRow(highlighted, false); - viewer.highlightRow(index, true); - viewer.getTable().setTopIndex(index); - } - - @SuppressWarnings("unused") - private void setupMenuButtons(Composite parent) - { - DropDownEntry open = new DropDownEntry("Open", (e) -> - { - FileDialog d = new FileDialog(parent.getShell(), SWT.NONE); - d.open(); - String filename = d.getFileName(); - if (!filename.equals("")) - open(d.getFilterPath() + File.separator + filename); - }); - - DropDownEntry save = new DropDownEntry("Save", (e) -> + Display.getDefault().asyncExec(() -> { - if (saveLoc == null) - openSaveAsDialog(parent); - save(saveLoc); + LazyTableViewer viewer = table.getTableViewer(); + viewer.highlightRow(highlighted, false); + highlighted = index; + viewer.highlightRow(index, true); + viewer.getTable().showItem(viewer.getTable().getItem(Math.min((int) memory.getDefinition().getMaximalAddress(), index + 2))); + viewer.getTable().showItem(viewer.getTable().getItem(index)); }); - DropDownEntry saveAs = new DropDownEntry("SaveAs", (e) -> - { - openSaveAsDialog(parent); - save(saveLoc); - }); - new DropDownMenu(parent, "File", open, save, saveAs); } - private void openSaveAsDialog(Composite parent) + private void addActivationButton(Composite parent) { - FileDialog d = new FileDialog(parent.getShell(), SWT.SAVE); - d.open(); - String filename = d.getFileName(); - if (!filename.equals("")) - saveLoc = d.getFilterPath() + File.separator + filename; + Button activationButton = new Button(parent, SWT.PUSH); + activationButton.setText("Set Active"); + activationButton.addListener(SWT.Selection, + e -> MachineContext.getInstance().getMachine().getMicroInstructionMemory().bind(memory)); } public void bindMicroInstructionMemory(MicroInstructionMemory memory) { - deleteColumns(); this.memory = memory; - viewer.setInput(memory); - this.miDef = memory.getDefinition().getMicroInstructionDefinition(); - this.memory.registerObserver(this); - createColumns(); - } - - private void deleteColumns() - { - for (TableViewerColumn col : columns) - col.getColumn().dispose(); - } - - private void createColumns() - { - int size = miDef.size(); - columns = new TableViewerColumn[size + 1]; - - TableViewerColumn col = createTableViewerColumn("Address", generateLongestHexStrings(12)); - columns[0] = col; - col.setLabelProvider(new AddressLabelProvider()); - - int bit = miDef.sizeInBits(); - ParameterClassification[] classes = miDef.getParameterClassifications(); - - for (int i = 0; i < size; i++) - { - int startBit = bit - 1; - int endBit = bit = bit - classes[i].getExpectedBits(); - String name = startBit == endBit ? Integer.toString(startBit) : startBit + "..." + endBit; - - String[] longestPossibleContents; - switch (classes[i].getExpectedType()) - { - case INTEGER_IMMEDIATE: - longestPossibleContents = generateLongestHexStrings(classes[i].getExpectedBits()); - break; - case BOOLEAN_IMMEDIATE: - case MNEMONIC: - longestPossibleContents = ((MnemonicFamily) classes[i]).getStringValues(); - break; - default: - longestPossibleContents = new String[0]; - break; - } - - col = createTableViewerColumn(name, longestPossibleContents); - columns[i + 1] = col; - createEditingAndLabel(col, miDef, i); - } - } - - private static final String[] HEX_DIGITS = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; - - private static String[] generateLongestHexStrings(int bitWidth) - { - return Arrays.stream(HEX_DIGITS).map(s -> "0x" + s.repeat((bitWidth + 3) / 4)).toArray(String[]::new); - } - - private void createEditingAndLabel(TableViewerColumn col, MicroInstructionDefinition miDef, int index) - { - ParameterClassification parameterClassification = miDef.getParameterClassifications()[index]; - EditingSupport support; - ColumnLabelProvider provider; - switch (parameterClassification.getExpectedType()) + if (memory != null) { - case BOOLEAN_IMMEDIATE: - support = new BooleanEditingSupport(viewer, miDef, index); - provider = new ParameterLabelProvider(index); - break; - case INTEGER_IMMEDIATE: - support = new IntegerEditingSupport(viewer, miDef, index, displaySettings, this.provider); - provider = new IntegerColumnLabelProvider(displaySettings, index); - break; - case MNEMONIC: - support = new MnemonicEditingSupport(viewer, miDef, index, this.provider); - provider = new ParameterLabelProvider(index); - break; - default: - throw new IllegalStateException( - "Unable to create EditingSupport for unknown ParameterType " + parameterClassification.getExpectedType()); + this.memory.registerCellModifiedListener(this); + this.memory.registerActiveMicroInstructionChangedListener(this); } - col.setEditingSupport(support); - col.setLabelProvider(provider); - col.getColumn().setToolTipText(miDef.getParameterDescription(index).orElse("")); + if (table != null) + table.bindMicroInstructionMemory(memory); } - private TableViewerColumn createTableViewerColumn(String title, String... longestPossibleContents) + private void open(IFile file) { - TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE); - TableColumn column = viewerColumn.getColumn(); - int maxWidth = 0; - for (String s : longestPossibleContents) - { - column.setText(s); - column.pack(); - if (column.getWidth() > maxWidth) - maxWidth = column.getWidth(); - } - column.setText(title); - column.pack(); - if (column.getWidth() < maxWidth) - column.setWidth(maxWidth); - column.setResizable(true); - column.setMoveable(false); - return viewerColumn; - } - - private void open(String file) - { - if (miDef == null) - { - System.err.println("Failed to parse MicroprogrammingMemory from File. No MicroInstructionDefinition assigned."); - return; - } try { - MicroInstructionMemoryParser.parseMemory(memory, file); - viewer.refresh(); - saveLoc = file; + bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory( + MachineContext.getInstance().getMachine().getDefinition().getMicroInstructionMemoryDefinition(), file.getContents())); } - catch (IOException | MicroInstructionMemoryParseException e) + catch (IOException | MicroInstructionMemoryParseException | CoreException e) { e.printStackTrace(); } } - private void save(String file) + private void save(IFile file, IProgressMonitor progressMonitor) { if (memory == null) { System.err.println("Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned."); return; } - try + try (InputStream toWrite = MicroInstructionMemoryParser.write(memory)) { - MicroInstructionMemoryParser.write(memory, file); + file.setContents(toWrite, 0, progressMonitor); } - catch (IOException e) + catch (IOException | CoreException e) { e.printStackTrace(); } @@ -273,48 +124,51 @@ public class InstructionView extends EditorPart implements ContextObserver, Memo @Override public void setFocus() { - viewer.getControl().setFocus(); - } - - @Override - public void setMachine(Optional machine) - { - if (machine.isPresent()) - { - Machine actualMachine = machine.get(); - bindMicroInstructionMemory(actualMachine.getMicroInstructionMemory()); - } + table.getTableViewer().getControl().setFocus(); } @Override public void doSave(IProgressMonitor progressMonitor) { IEditorInput input = getEditorInput(); - if (input instanceof IPathEditorInput) + if (input instanceof IFileEditorInput) { - IPathEditorInput pathInput = (IPathEditorInput) input; - save(pathInput.getPath().toOSString()); - dirty = false; - firePropertyChange(PROP_DIRTY); + IFileEditorInput pathInput = (IFileEditorInput) input; + save(pathInput.getFile(), progressMonitor); + setDirty(false); } } @Override public void doSaveAs() { - // not allowed +// openSaveAsDialog(); } +// private void openSaveAsDialog() +// { +// FileDialog d = new FileDialog(table.getTableViewer().getTable().getShell(), SWT.SAVE); +// d.open(); +// String filename = d.getFileName(); +// if (!filename.equals("")) +// { +// save(d.getFilterPath() + File.separator + filename); +// setDirty(false); +// } +// } + @Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { setSite(site); setInput(input); - if (input instanceof IPathEditorInput) + if (input instanceof IFileEditorInput) { - IPathEditorInput pathInput = (IPathEditorInput) input; - open(pathInput.getPath().toOSString()); + IFileEditorInput fileInput = (IFileEditorInput) input; + setPartName(fileInput.getName()); + open(fileInput.getFile()); } + } @Override @@ -332,7 +186,19 @@ public class InstructionView extends EditorPart implements ContextObserver, Memo @Override public void update(long address) { - dirty = true; + setDirty(true); + table.refresh(); + } + + private void setDirty(boolean value) + { + dirty = value; firePropertyChange(PROP_DIRTY); } + + @Override + public void activeMicroInstructionChanged(long address) + { + highlight((int) (address - memory.getDefinition().getMinimalAddress())); + } }