Skip to content

Commit

Permalink
[cocoemu] Make memory components use ByteBuffer under the hood
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelix8996 committed Aug 12, 2024
1 parent 07f0159 commit bc215e2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.cdm.cocoemu.core.ports.OutputsClass;
import org.cdm.cocoemu.core.ports.OutputsField;

import java.nio.ByteBuffer;

public class BankedRam extends BankedRom {
@InputsField
public BankedRam.Inputs inputs;
Expand All @@ -23,14 +25,18 @@ public BankedRam(Image image) {
super(image);
}

public BankedRam(ByteBuffer buffer) {
super(buffer);
}

@Override
public void clockRising() {
if (inputs.select) {
if (!inputs.rw) {
memory[inputs.address] = inputs.data_in & 0xFF;

if (inputs.word && inputs.address % 2 == 0) {
memory[inputs.address + 1] = (inputs.data_in >> 8) & 0xFF;
buffer.putShort(inputs.address, (short) inputs.data_in);
} else {
buffer.put(inputs.address, (byte) inputs.data_in);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.cdm.cocoemu.core.ports.OutputsClass;
import org.cdm.cocoemu.core.ports.OutputsField;

import java.nio.ByteBuffer;

public class BankedRom extends Rom {
@InputsField
public BankedRom.Inputs inputs;
Expand All @@ -23,13 +25,16 @@ public BankedRom(Image image) {
super(image);
}

public BankedRom(ByteBuffer buffer) {
super(buffer);
}

@Override
public void update() {
if (inputs.select) {
if (inputs.word) {
if (inputs.address % 2 == 0) {
outputs.data_out = memory[inputs.address % memory.length]
+ (memory[(inputs.address + 1) % memory.length] << 8);
outputs.data_out = Short.toUnsignedInt(buffer.getShort(inputs.address));
} else {
outputs.data_out = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,45 @@
import org.cdm.cocoemu.core.PortedComponentBase;
import org.cdm.cocoemu.core.image.Image;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;

public abstract class Memory extends PortedComponentBase {
protected int[] memory;
protected ByteBuffer buffer;

public Memory(int size) {
memory = new int[size];
buffer = ByteBuffer.allocate(size);

buffer.order(ByteOrder.LITTLE_ENDIAN);
}

public Memory(Image image) {
memory = image.getIntegers();
buffer = ByteBuffer.wrap(image.getBytes());

buffer.order(ByteOrder.LITTLE_ENDIAN);
}

public Memory(ByteBuffer buffer) {
this.buffer = buffer;
}

public int size() {
return memory.length;
return buffer.capacity();
}

public int[] getMemory() {
return memory;
public ByteBuffer getBuffer() {
return buffer;
}

public Image getImage() {
return new Image(memory);
List<Integer> array = new ArrayList<>(buffer.capacity());

for (int i = 0; i < buffer.capacity(); i++) {
array.add(Byte.toUnsignedInt(buffer.get(i)));
}

return new Image(array);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.cdm.cocoemu.core.ports.OutputsClass;
import org.cdm.cocoemu.core.ports.OutputsField;

import java.nio.ByteBuffer;

public class Ram extends Rom {

@InputsField
Expand All @@ -24,11 +26,15 @@ public Ram(Image image) {
super(image);
}

public Ram(ByteBuffer buffer) {
super(buffer);
}

@Override
public void clockRising() {
if (inputs.select) {
if (!inputs.rw) {
memory[inputs.address] = inputs.data_in & 0xFF;
buffer.put(inputs.address, (byte) inputs.data_in);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.cdm.cocoemu.core.ports.OutputsClass;
import org.cdm.cocoemu.core.ports.OutputsField;

import java.nio.ByteBuffer;

public class Rom extends Memory {

@InputsField
Expand All @@ -24,11 +26,14 @@ public Rom(Image image) {
super(image);
}

public Rom(ByteBuffer buffer) {
super(buffer);
}

@Override
public void update() {
if (inputs.select) {
outputs.data_out = memory[inputs.address % memory.length];
outputs.data_out = Byte.toUnsignedInt(buffer.get(inputs.address));
}
}

Expand Down

0 comments on commit bc215e2

Please sign in to comment.