Skip to content

Commit

Permalink
[#318, #315] Refactoring of byte-mem
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed Feb 19, 2023
1 parent 3a81f35 commit a40f87b
Show file tree
Hide file tree
Showing 15 changed files with 670 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,20 @@

import net.emustudio.emulib.plugins.annotations.PluginContext;
import net.emustudio.emulib.plugins.memory.AbstractMemoryContext;
import net.emustudio.emulib.runtime.interaction.Dialogs;
import net.emustudio.emulib.runtime.io.IntelHEX;
import net.emustudio.plugins.memory.bytemem.api.ByteMemoryContext;

import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@PluginContext(id = "Byte Memory")
public class MemoryContextImpl extends AbstractMemoryContext<Byte> implements ByteMemoryContext {
final static int DEFAULT_MEM_SIZE = 65536;

private final RangeTree romRanges = new RangeTree();
private final Dialogs dialogs;
int lastImageStart = 0;
private Byte[][] mem = new Byte[1][0];
private int banksCount;
private int bankSelect = 0;
private int bankCommon = 0;
public MemoryContextImpl(Dialogs dialogs) {
this.dialogs = Objects.requireNonNull(dialogs);
}

void init(int size, int banks, int bankCommon) {
if (banks <= 0) {
Expand All @@ -63,7 +51,6 @@ public void clear() {
for (Byte[] bank : mem) {
Arrays.fill(bank, (byte) 0);
}
lastImageStart = 0;
notifyMemoryChanged(-1);
}

Expand Down Expand Up @@ -95,40 +82,6 @@ public int getCommonBoundary() {
return bankCommon;
}

public void loadHex(Path hexFile, int bank) {
int currentBank = bankSelect;
try {
bankSelect = (short) bank;
lastImageStart = IntelHEX.loadIntoMemory(hexFile.toFile(), this, p -> p);
} catch (FileNotFoundException ex) {
dialogs.showError("File not found: " + hexFile);
} catch (Exception e) {
dialogs.showError("Error opening file: " + hexFile);
} finally {
bankSelect = currentBank;
notifyMemoryChanged(-1);
}
}

public void loadBin(Path binFile, int address, int bank) {
lastImageStart = 0;
try (RandomAccessFile binaryFile = new RandomAccessFile(binFile.toFile(), "r")) {
long position = 0, length = binaryFile.length();
while (position < length) {
mem[bank][address++] = (byte) (binaryFile.readUnsignedByte() & 0xFF);
position++;
}
} catch (EOFException ignored) {
// ignored intentionally
} catch (FileNotFoundException ex) {
dialogs.showError("File not found: " + binFile);
} catch (Exception e) {
dialogs.showError("Error opening file: " + binFile);
} finally {
notifyMemoryChanged(-1);
}
}

@Override
public Byte read(int from) {
int activeBank = (from < bankCommon) ? bankSelect : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.emustudio.emulib.runtime.settings.PluginSettings;
import net.emustudio.plugins.memory.bytemem.api.ByteMemoryContext;
import net.emustudio.plugins.memory.bytemem.gui.MemoryGui;
import net.emustudio.plugins.memory.bytemem.loaders.Loader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -48,15 +49,14 @@
public class MemoryImpl extends AbstractMemory {
private final static Logger LOGGER = LoggerFactory.getLogger(MemoryImpl.class);

private final MemoryContextImpl context;
private final MemoryContextImpl context = new MemoryContextImpl();
private final boolean guiNotSupported;
private MemoryGui gui;

public MemoryImpl(long pluginID, ApplicationApi applicationApi, PluginSettings settings) {
super(pluginID, applicationApi, settings);

this.guiNotSupported = settings.getBoolean(PluginSettings.EMUSTUDIO_NO_GUI, false);
this.context = new MemoryContextImpl(applicationApi.getDialogs());
try {
ContextPool contextPool = applicationApi.getContextPool();
contextPool.register(pluginID, context, ByteMemoryContext.class);
Expand Down Expand Up @@ -157,17 +157,17 @@ private void loadImages() throws PluginInitializationException {
break;
}
} catch (NumberFormatException e) {
throw new PluginInitializationException(this, "Could not parse image address", e);
throw new PluginInitializationException(this, "Could not parse image address or bank", e);
} catch (Exception e) {
throw new PluginInitializationException(this, e);
}
}
}

public void loadImage(Path imagePath, int address, int bank) {
if (imagePath.toString().toLowerCase().endsWith(".hex")) {
context.loadHex(imagePath, bank);
} else {
context.loadBin(imagePath, address, bank);
}
public void loadImage(Path imagePath, int address, int bank) throws Exception {
Loader.MemoryBank memoryBank = Loader.MemoryBank.of(bank, address);
Loader loader = Loader.createLoader(imagePath);
loader.load(imagePath, context, memoryBank);
}

/*
Expand Down Expand Up @@ -210,12 +210,6 @@ public void saveROMRanges() {
}
}

@Override
public void setProgramLocation(int location) {
super.setProgramLocation(location);
context.lastImageStart = location;
}

@Override
public void showSettings(JFrame parent) {
if (!guiNotSupported) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import net.emustudio.emulib.runtime.interaction.FileExtensionsFilter;

import java.awt.*;
import java.util.List;

import static net.emustudio.plugins.memory.bytemem.loaders.Loader.IMAGE_LOADERS;


public class Constants {
public final static Font MEMORY_CELLS_FONT = new Font(Font.MONOSPACED, Font.PLAIN, 12);

public final static FileExtensionsFilter IMAGE_EXTENSION_FILTER = new FileExtensionsFilter(
"Memory image", "hex", "bin", "com", "out"
"Memory image", List.copyOf(IMAGE_LOADERS.keySet())
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
package net.emustudio.plugins.memory.bytemem.gui;

import net.emustudio.emulib.runtime.interaction.Dialogs;
import net.emustudio.plugins.memory.bytemem.gui.actions.FindSequenceAction;
import net.emustudio.plugins.memory.bytemem.gui.actions.find_sequence.PerformFindSequenceAction;
import net.emustudio.plugins.memory.bytemem.gui.model.MemoryTableModel;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.util.function.Consumer;

public class FindSequenceDialog extends JDialog {
private final FindSequenceAction findSequenceAction;
private final PerformFindSequenceAction performFindSequenceAction;
private final JRadioButton radioCurrentPage = new JRadioButton();
private final JRadioButton radioPlainText = new JRadioButton();
private final JTextField txtPosition = new JTextField();
Expand All @@ -37,7 +37,7 @@ public FindSequenceDialog(Dialogs dialogs, JDialog parent, MemoryTableModel tabl
super(parent, true);
setLocationRelativeTo(parent);

this.findSequenceAction = new FindSequenceAction(
this.performFindSequenceAction = new PerformFindSequenceAction(
dialogs, this::dispose, tableModel, setFoundAddress, radioCurrentPage::isSelected,
radioPlainText::isSelected, currentAddress, txtPosition, txtSequence
);
Expand All @@ -51,7 +51,7 @@ private void initComponents() {
JRadioButton radioBytes = new JRadioButton();
JPanel jPanel2 = new JPanel();
JRadioButton radioSpecificPosition = new JRadioButton();
JButton btnFind = new JButton(findSequenceAction);
JButton btnFind = new JButton(performFindSequenceAction);
btnGroupSequenceToFind.add(radioPlainText);
btnGroupSequenceToFind.add(radioBytes);

Expand Down
Loading

0 comments on commit a40f87b

Please sign in to comment.