refactoring of EEPROM

This commit is contained in:
hneemann 2017-09-07 18:34:11 +02:00
parent 3945584945
commit 13a8f03410
2 changed files with 20 additions and 65 deletions

View File

@ -1,10 +1,5 @@
package de.neemann.digital.core.memory; package de.neemann.digital.core.memory;
import de.neemann.digital.core.Node;
import de.neemann.digital.core.NodeException;
import de.neemann.digital.core.ObservableValue;
import de.neemann.digital.core.ObservableValues;
import de.neemann.digital.core.element.Element;
import de.neemann.digital.core.element.ElementAttributes; import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.ElementTypeDescription; import de.neemann.digital.core.element.ElementTypeDescription;
import de.neemann.digital.core.element.Keys; import de.neemann.digital.core.element.Keys;
@ -16,7 +11,7 @@ import static de.neemann.digital.core.element.PinInfo.input;
* *
* @author hneemann * @author hneemann
*/ */
public class EEPROM extends Node implements Element { public class EEPROM extends RAMSinglePortSel {
/** /**
* The EEPROMs {@link ElementTypeDescription} * The EEPROMs {@link ElementTypeDescription}
@ -32,73 +27,22 @@ public class EEPROM extends Node implements Element {
.addAttribute(Keys.LABEL) .addAttribute(Keys.LABEL)
.addAttribute(Keys.DATA); .addAttribute(Keys.DATA);
private final int bits;
private final int addrBits;
private final DataField memory;
private final ObservableValue dataOut;
private ObservableValue addrIn;
private ObservableValue csIn;
private ObservableValue weIn;
private ObservableValue oeIn;
private ObservableValue dataIn;
private int addr;
private boolean cs;
private boolean oe;
/** /**
* Creates a new instance * Creates a new instance
* *
* @param attr the elements attributes * @param attr the elements attributes
*/ */
public EEPROM(ElementAttributes attr) { public EEPROM(ElementAttributes attr) {
super(true); super(attr);
bits = attr.get(Keys.BITS); }
addrBits = attr.get(Keys.ADDR_BITS);
int size = 1 << addrBits; @Override
protected DataField createDataField(ElementAttributes attr, int size) {
DataField memory = attr.get(Keys.DATA); DataField memory = attr.get(Keys.DATA);
if (memory.size() < size) { if (memory.size() != size) {
memory = new DataField(memory, size); memory = new DataField(memory, size);
attr.set(Keys.DATA, memory); attr.set(Keys.DATA, memory);
} }
this.memory = memory; return memory;
dataOut = new ObservableValue("D", bits, true).setPinDescription(DESCRIPTION).setBidirectional();
} }
@Override
public void setInputs(ObservableValues inputs) throws NodeException {
addrIn = inputs.get(0).checkBits(addrBits, this).addObserverToValue(this);
csIn = inputs.get(1).checkBits(1, this).addObserverToValue(this);
weIn = inputs.get(2).checkBits(1, this).addObserverToValue(this);
oeIn = inputs.get(3).checkBits(1, this).addObserverToValue(this);
dataIn = inputs.get(4).checkBits(bits, this).addObserverToValue(this);
}
@Override
public void readInputs() throws NodeException {
cs = csIn.getBool();
if (cs) {
addr = (int) addrIn.getValue();
oe = oeIn.getBool();
if (weIn.getBool()) {
long data = dataIn.getValue();
memory.setData(addr, data);
}
}
}
@Override
public void writeOutputs() throws NodeException {
if (cs && oe) {
dataOut.set(memory.getDataWord(addr), false);
} else {
dataOut.set(0, true);
}
}
@Override
public ObservableValues getOutputs() {
return dataOut.asList();
}
} }

View File

@ -60,11 +60,22 @@ public class RAMSinglePortSel extends Node implements Element, RAMInterface {
bits = attr.get(Keys.BITS); bits = attr.get(Keys.BITS);
addrBits = attr.get(Keys.ADDR_BITS); addrBits = attr.get(Keys.ADDR_BITS);
size = 1 << addrBits; size = 1 << addrBits;
memory = new DataField(size); memory = createDataField(attr, size);
label = attr.getCleanLabel(); label = attr.getCleanLabel();
dataOut = new ObservableValue("D", bits, true).setPinDescription(DESCRIPTION).setBidirectional(); dataOut = new ObservableValue("D", bits, true).setPinDescription(DESCRIPTION).setBidirectional();
} }
/**
* creates the data field to use
*
* @param attr the elements attributes
* @param size the size of the memory
* @return the memory to use
*/
protected DataField createDataField(ElementAttributes attr, int size) {
return new DataField(size);
}
@Override @Override
public void setInputs(ObservableValues inputs) throws NodeException { public void setInputs(ObservableValues inputs) throws NodeException {
addrIn = inputs.get(0).checkBits(addrBits, this).addObserverToValue(this); addrIn = inputs.get(0).checkBits(addrBits, this).addObserverToValue(this);