Skip to content

Commit

Permalink
[#201] as-z80, z80-cpu: lot of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed Apr 29, 2022
1 parent 82014ca commit 076fac5
Show file tree
Hide file tree
Showing 23 changed files with 529 additions and 295 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of emuStudio.
*
* Copyright (C) 2006-2020 Peter Jakubčo
* Copyright (C) 2006-2022 Peter Jakubčo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -96,5 +96,8 @@ subprojects {
repositories {
mavenLocal()
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@

public class Evaluated extends Node {
public final int value;
public final boolean isAddress;

public Evaluated(int line, int column, int value) {
public Evaluated(int line, int column, int value, boolean isAddress) {
super(line, column);
this.value = value;
this.isAddress = isAddress;
}

public Evaluated(int line, int column, int value) {
this(line, column, value, false);
}

@Override
protected Node mkCopy() {
return new Evaluated(line, column, value);
return new Evaluated(line, column, value, isAddress);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ protected Node mkCopy() {

@Override
public Optional<Evaluated> eval(Optional<Integer> currentAddress, NameSpace env) {
return currentAddress.map(addr -> new Evaluated(line, column, addr));
return currentAddress.map(addr -> new Evaluated(line, column, addr, true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public byte eval() {
return (byte)(((x << 6) | (y << 3) | (z & 7)) & 0xFF);
}

public boolean hasRelativeAddress() {
// DJNZ, JR, JR cc
return (x == 0 && z == 0 && y >= 2 && y <= 7);
}

@Override
public void accept(NodeVisitor visitor) {
visitor.visit(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PseudoLabel(Token label) {

@Override
public Optional<Evaluated> eval(Optional<Integer> currentAddress, NameSpace env) {
return currentAddress.map(addr -> new Evaluated(line, column, addr));
return currentAddress.map(addr -> new Evaluated(line, column, addr, true));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void visit(DataDS node) {

@Override
public void visit(Instr node) {
expectedBytes = 0;
expectedBytes = node.hasRelativeAddress() ? 1 : 0;
visitChildren(node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
public class GenerateCodeVisitor extends NodeVisitor {
private final IntelHEX hex;
private int expectedBytes;
private boolean isRelative; // if we should treat Evaluated value as "relative address"
private int currentAddress; // for computing relative address

public GenerateCodeVisitor(IntelHEX hex) {
this.hex = Objects.requireNonNull(hex);
Expand Down Expand Up @@ -48,12 +50,16 @@ public void visit(DataDS node) {

@Override
public void visit(Instr node) {
isRelative = node.hasRelativeAddress();
currentAddress = node.getAddress();

hex.add(node.eval());
int instrSize = node.getSizeBytes().orElse(1);
if (instrSize > 1) {
expectedBytes = 0;
visitChildren(node);
}
isRelative = false;
}

@Override
Expand Down Expand Up @@ -109,16 +115,18 @@ public void visit(PseudoOrg node) {

@Override
public void visit(Evaluated node) {
final int value = (isRelative && node.isAddress) ? (node.value - currentAddress) : node.value;

if (expectedBytes == 1) {
addByte(node.value);
addByte(value);
} else if (expectedBytes == 2) {
addWord(node.value);
addWord(value);
} else {
node.getSizeBytes().ifPresent(size -> {
if (size == 1) {
addByte(node.value);
addByte(value);
} else if (size == 2) {
addWord(node.value);
addWord(value);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class InstrExprTest extends AbstractCompilerTest {

@Test
public void testRST() throws Exception {
public void testJump() {
compile(
"JP EXAMPLE\n" +
"RST 00H\n" +
Expand All @@ -14,7 +14,34 @@ public void testRST() throws Exception {
);

assertProgram(
0xC3, 0x04, 0x00, 0xC7, 0x3E, 0x01
0xC3, 0x04, 0x00, 0xC7, 0x3E, 0x01
);
}

@Test
public void testRelativeJumpLabel() {
compile(
"ld A,01H\n" +
"jr z, EXAMPLE\n" +
"RST 00H\n" +
"EXAMPLE:\n" +
"halt"
);

assertProgram(
0x3E, 0x01, 0x28, 0x03, 0xC7, 0x76
);
}

@Test
public void testRelativeJumpCurrentAddress() {
compile("halt\njr $"); // infinite loop
assertProgram(0x76, 0x18, 0);
}

@Test
public void testRelativeJumpExact() {
compile("halt\ndjnz $+0x20"); // if it is complex expression, treat it like exact value
assertProgram(0x76, 0x10, 0x21);
}
}
2 changes: 1 addition & 1 deletion plugins/cpu/8080-cpu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ buildscript {

plugins {
id 'java'
id 'net.emustudio.edigen-plugin' version '1.4.0'
id 'net.emustudio.edigen-plugin' version '1.5.0'
id 'com.adarshr.test-logger' version '3.1.0'
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/cpu/brainduck-cpu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.apache.tools.ant.filters.ReplaceTokens

plugins {
id 'java'
id 'net.emustudio.edigen-plugin' version '1.4.0'
id 'net.emustudio.edigen-plugin' version '1.5.0'
id 'com.adarshr.test-logger' version '3.1.0'
}

Expand Down
1 change: 1 addition & 0 deletions plugins/cpu/ram-cpu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.apache.tools.ant.filters.ReplaceTokens

plugins {
id 'java'
id 'net.emustudio.edigen-plugin' version '1.5.0'
id 'com.adarshr.test-logger' version '3.1.0'
}

Expand Down
1 change: 1 addition & 0 deletions plugins/cpu/rasp-cpu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.apache.tools.ant.filters.ReplaceTokens

plugins {
id 'java'
id 'net.emustudio.edigen-plugin' version '1.5.0'
id 'com.adarshr.test-logger' version '3.1.0'
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/cpu/ssem-cpu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ buildscript {

plugins {
id 'java'
id 'net.emustudio.edigen-plugin' version '1.4.0'
id 'net.emustudio.edigen-plugin' version '1.5.0'
id 'com.adarshr.test-logger' version '3.1.0'
}

Expand Down
3 changes: 1 addition & 2 deletions plugins/cpu/z80-cpu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.apache.tools.ant.filters.ReplaceTokens

plugins {
id 'java'
id 'net.emustudio.edigen-plugin' version '1.4.1'
id 'net.emustudio.edigen-plugin' version '1.5.0'
id 'com.adarshr.test-logger' version '3.1.0'
}

Expand Down Expand Up @@ -53,7 +53,6 @@ jar {
edigen {
decoderName = 'net.emustudio.plugins.cpu.zilogZ80.gui.DecoderImpl'
disassemblerName = 'net.emustudio.plugins.cpu.zilogZ80.gui.DisassemblerImpl'
//debug = true
}

processResources {
Expand Down
1 change: 0 additions & 1 deletion plugins/cpu/z80-cpu/src/main/edigen/cpu.eds
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ any2 = 00 | 01 | 10 | 11;
"%s" = instruction rp;
"%s" = instruction rp2;
"%s" = instruction r;
"%s" = instruction r_no_hl;
"%s" = instruction cc;
"%s" = instruction im;
"%s" = instruction rst;
Expand Down
Loading

0 comments on commit 076fac5

Please sign in to comment.