simplifies ram initialization is test cases

This commit is contained in:
hneemann 2020-11-29 09:33:02 +01:00
parent 084459f8a1
commit dc93400815
3 changed files with 21 additions and 9 deletions

View File

@ -10,6 +10,7 @@ import de.neemann.digital.core.Node;
import de.neemann.digital.core.Signal;
import de.neemann.digital.core.memory.DataField;
import de.neemann.digital.core.memory.ProgramMemory;
import de.neemann.digital.core.memory.RAMInterface;
import de.neemann.digital.lang.Lang;
import de.neemann.digital.testing.TestingDataException;
@ -35,8 +36,8 @@ public class ModelInitializer {
inits.add(new InitProgramMemory(memory));
}
void initMemory(String ramName, DataField memory) {
inits.add(new InitMemory(ramName, memory));
void initMemory(String ramName, int addr, long value) {
inits.add(new InitMemory(ramName, addr, value));
}
/**
@ -96,21 +97,23 @@ public class ModelInitializer {
private static final class InitMemory implements ModelInit {
private final String memoryName;
private final DataField memory;
private final int addr;
private final long value;
private InitMemory(String memoryName, DataField memory) {
private InitMemory(String memoryName, int addr, long value) {
this.memoryName = memoryName;
this.memory = memory;
this.addr = addr;
this.value = value;
}
@Override
public void init(Model model) throws TestingDataException {
List<Node> nodes = model.findNode(n -> n instanceof ProgramMemory && ((ProgramMemory) n).getLabel().equals(memoryName));
List<Node> nodes = model.findNode(n -> n instanceof RAMInterface && ((RAMInterface) n).getLabel().equals(memoryName));
switch (nodes.size()) {
case 0:
throw new TestingDataException(Lang.get("err_noMemoryFound", memoryName));
case 1:
((ProgramMemory) nodes.get(0)).setProgramMemory(memory);
((RAMInterface) nodes.get(0)).getMemory().setData(addr, value);
break;
default:
throw new TestingDataException(Lang.get("err_multipleMemoriesFound", memoryName));

View File

@ -128,7 +128,15 @@ public class Parser {
tok.consume();
expect(Tokenizer.Token.IDENT);
final String ramName = tok.getIdent();
modelInit.initMemory(ramName, parseData());
expect(Tokenizer.Token.OPEN);
expect(Tokenizer.Token.NUMBER);
long addr = convToLong(tok.getIdent());
expect(Tokenizer.Token.CLOSE);
expect(Tokenizer.Token.EQUAL);
expect(Tokenizer.Token.NUMBER);
long val = convToLong(tok.getIdent());
expect(Tokenizer.Token.SEMICOLON);
modelInit.initMemory(ramName, (int) addr, val);
break;
case PROGRAM:
tok.consume();

View File

@ -59,7 +59,8 @@ public class ModelInitializerTest extends TestCase {
public void test_ram() throws IOException, ParserException, TestingDataException {
ModelInitializer mi = new Parser("A B Y\n" +
"memory myRam (1,2)\n" +
"memory myRam(0)=1;\n" +
"memory myRam(1)=2;\n" +
"1 1 1").parse().getModelInitializer();
Model m = new Model();