Skip to content

Commit

Permalink
Implemented data size override
Browse files Browse the repository at this point in the history
  • Loading branch information
maccasoft committed Dec 11, 2023
1 parent 09e6dca commit f9c8456
Show file tree
Hide file tree
Showing 20 changed files with 423 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.maccasoft</groupId>
<artifactId>spin-tools-runtime</artifactId>
<version>0.33.0</version>
<version>0.34.0</version>
<packaging>pom</packaging>
<build>
<plugins>
Expand Down
2 changes: 1 addition & 1 deletion modules/spin-tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.maccasoft</groupId>
<artifactId>spin-tools</artifactId>
<version>0.33.0</version>
<version>0.34.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,100 @@ void testWord() throws Exception {
+ "", compile(text));
}

@Test
void testBytefit() throws Exception {
String text = ""
+ "DAT org $000\n"
+ " bytefit $00\n"
+ " bytefit $01\n"
+ " bytefit $02\n"
+ " bytefit -$80\n"
+ " bytefit $FF\n"
+ "";

Assertions.assertEquals(""
+ "' Object header (var size 0)\n"
+ "00000 00000 09 00 Object size\n"
+ "00002 00002 01 Method count + 1\n"
+ "00003 00003 00 Object count\n"
+ "00004 00004 000 org $000\n"
+ "00004 00004 000 00 bytefit $00\n"
+ "00005 00005 000 01 bytefit $01\n"
+ "00006 00006 000 02 bytefit $02\n"
+ "00007 00007 000 80 bytefit -$80\n"
+ "00008 00008 001 FF bytefit $FF\n"
+ "", compile(text));

Assertions.assertThrows(CompilerException.class, new Executable() {

@Override
public void execute() throws Throwable {
compile(""
+ "DAT org $000\n"
+ " bytefit -$81\n"
+ "");
}
});

Assertions.assertThrows(CompilerException.class, new Executable() {

@Override
public void execute() throws Throwable {
compile(""
+ "DAT org $000\n"
+ " bytefit $100\n"
+ "");
}
});
}

@Test
void testWordfit() throws Exception {
String text = ""
+ "DAT org $000\n"
+ " wordfit $0000\n"
+ " wordfit $0001\n"
+ " wordfit $0002\n"
+ " wordfit -$8000\n"
+ " wordfit $FFFF\n"
+ "";

Assertions.assertEquals(""
+ "' Object header (var size 0)\n"
+ "00000 00000 0E 00 Object size\n"
+ "00002 00002 01 Method count + 1\n"
+ "00003 00003 00 Object count\n"
+ "00004 00004 000 org $000\n"
+ "00004 00004 000 00 00 wordfit $0000\n"
+ "00006 00006 000 01 00 wordfit $0001\n"
+ "00008 00008 001 02 00 wordfit $0002\n"
+ "0000A 0000A 001 00 80 wordfit -$8000\n"
+ "0000C 0000C 002 FF FF wordfit $FFFF\n"
+ "", compile(text));

Assertions.assertThrows(CompilerException.class, new Executable() {

@Override
public void execute() throws Throwable {
compile(""
+ "DAT org $000\n"
+ " wordfit -$8001\n"
+ "");
}
});

Assertions.assertThrows(CompilerException.class, new Executable() {

@Override
public void execute() throws Throwable {
compile(""
+ "DAT org $000\n"
+ " wordfit $10000\n"
+ "");
}
});
}

@Test
void testLong() throws Exception {
String text = ""
Expand Down Expand Up @@ -357,6 +451,37 @@ void testAbsoluteAddress() throws Exception {
+ "", compile(text));
}

@Test
void testValueSizeOverride() throws Exception {
String text = ""
+ "DAT org $000\n"
+ "\n"
+ " byte $FFAA, $BB995511\n"
+ " byte word $FFAA, long $BB995511\n"
+ " word $FFAA, long $BB995511\n"
+ "\n"
+ " bytefit word $FFAA, long $BB995511\n"
+ " wordfit $FFAA, long $BB995511\n"
+ "";

Assertions.assertEquals(""
+ "' Object header (var size 0)\n"
+ "00000 00000 1E 00 Object size\n"
+ "00002 00002 01 Method count + 1\n"
+ "00003 00003 00 Object count\n"
+ "00004 00004 000 org $000\n"
+ "00004 00004 000 AA 11 byte $FFAA, $BB995511\n"
+ "00006 00006 000 AA FF 11 55 byte word $FFAA, long $BB995511\n"
+ "0000A 0000A 001 99 BB\n"
+ "0000C 0000C 002 AA FF 11 55 word $FFAA, long $BB995511\n"
+ "00010 00010 003 99 BB\n"
+ "00012 00012 003 AA FF 11 55 bytefit word $FFAA, long $BB995511\n"
+ "00016 00016 004 99 BB\n"
+ "00018 00018 005 AA FF 11 55 wordfit $FFAA, long $BB995511\n"
+ "0001C 0001C 006 99 BB\n"
+ "", compile(text));
}

String compile(String text) throws Exception {
return compile(text, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,34 @@ void testPtrAugs() throws Exception {
+ "", compile(text));
}

@Test
void testValueSizeOverride() throws Exception {
String text = ""
+ "DAT org $000\n"
+ "\n"
+ " byte $FFAA, $BB995511\n"
+ " byte word $FFAA, long $BB995511\n"
+ " word $FFAA, long $BB995511\n"
+ "\n"
+ " bytefit word $FFAA, long $BB995511\n"
+ " wordfit $FFAA, long $BB995511\n"
+ "";

Assertions.assertEquals(""
+ "' Object header (var size 4)\n"
+ "00000 00000 000 org $000\n"
+ "00000 00000 000 AA 11 byte $FFAA, $BB995511\n"
+ "00002 00002 000 AA FF 11 55 byte word $FFAA, long $BB995511\n"
+ "00006 00006 001 99 BB\n"
+ "00008 00008 002 AA FF 11 55 word $FFAA, long $BB995511\n"
+ "0000C 0000C 003 99 BB\n"
+ "0000E 0000E 003 AA FF 11 55 bytefit word $FFAA, long $BB995511\n"
+ "00012 00012 004 99 BB\n"
+ "00014 00014 005 AA FF 11 55 wordfit $FFAA, long $BB995511\n"
+ "00018 00018 006 99 BB\n"
+ "", compile(text));
}

String compile(String text) throws Exception {
return compile(text, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
public class SpinTools {

public static final String APP_TITLE = "Spin Tools IDE";
public static final String APP_VERSION = "0.33.0";
public static final String APP_VERSION = "0.34.0";

static final File defaultSpin1Examples = new File(System.getProperty("APP_DIR"), "examples/P1").getAbsoluteFile();
static final File defaultSpin2Examples = new File(System.getProperty("APP_DIR"), "examples/P2").getAbsoluteFile();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.maccasoft.propeller.expressions;

public class Type extends Passthrough {

private final String type;
private final Expression term;

public Type(String type, Expression term) {
this.type = type;
this.term = term;
}

public String getType() {
return type;
}

public Expression getTerm() {
return term;
}

@Override
public Expression resolve() {
return term;
}

@Override
public boolean isGroup() {
return true;
}

@Override
public String toString() {
return type + " " + term;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.maccasoft.propeller.expressions.ShiftRight;
import com.maccasoft.propeller.expressions.Subtract;
import com.maccasoft.propeller.expressions.Trunc;
import com.maccasoft.propeller.expressions.Type;
import com.maccasoft.propeller.expressions.Xor;
import com.maccasoft.propeller.model.Token;

Expand Down Expand Up @@ -347,6 +348,11 @@ Expression parseAtom() {
}
}

if ("BYTE".equalsIgnoreCase(token.getText()) || "WORD".equalsIgnoreCase(token.getText()) || "LONG".equalsIgnoreCase(token.getText())) {
token = next();
return new Type(token.getText(), parseLevel(parseAtom(), 0));
}

if ("(".equals(token.getText())) {
next();
Group expression = new Group(parseLevel(parseAtom(), 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Spin1Model extends SpinModel {
"MUXZ", "NEG", "NEGC", "NEGNC", "NEGNZ", "NEGZ", "NOP", "ONES", "OR", "RCL", "RCR", "RDBYTE", "RDLONG", "RDWORD", "RET",
"REV", "ROL", "ROR", "SAR", "SHL", "SHR", "SUB", "SUBABS", "SUBS", "SUBSX", "SUBX", "SUMC", "SUMNC", "SUMNZ", "SUMZ",
"TEST", "TESTN", "TJNZ", "TJZ", "WAITCNT", "WAITPEQ", "WAITPNE", "WAITVID", "WRBYTE", "WRLONG", "WRWORD", "XOR",
"LONG", "WORD", "BYTE"
"LONG", "WORD", "BYTE", "WORDFIT", "BYTEFIT"
}));

static Set<String> conditions = new HashSet<>(Arrays.asList(new String[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package com.maccasoft.propeller.spin1;

import com.maccasoft.propeller.expressions.Expression;
import com.maccasoft.propeller.expressions.Type;

public class Spin1PAsmExpression {

Expand Down Expand Up @@ -58,6 +59,22 @@ public byte[] getByte() {
else {
value = expression.getNumber().intValue();
}
if (expression instanceof Type) {
switch (((Type) expression).getType().toUpperCase()) {
case "WORD":
return new byte[] {
(byte) (value & 0xFF),
(byte) ((value >> 8) & 0xFF)
};
case "LONG":
return new byte[] {
(byte) (value & 0xFF),
(byte) ((value >> 8) & 0xFF),
(byte) ((value >> 16) & 0xFF),
(byte) ((value >> 24) & 0xFF)
};
}
}
return new byte[] {
(byte) (value & 0xFF)
};
Expand All @@ -71,6 +88,17 @@ public byte[] getWord() {
else {
value = expression.getNumber().intValue();
}
if (expression instanceof Type) {
switch (((Type) expression).getType().toUpperCase()) {
case "LONG":
return new byte[] {
(byte) (value & 0xFF),
(byte) ((value >> 8) & 0xFF),
(byte) ((value >> 16) & 0xFF),
(byte) ((value >> 24) & 0xFF)
};
}
}
return new byte[] {
(byte) (value & 0xFF),
(byte) ((value >> 8) & 0xFF)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.maccasoft.propeller.CompilerException;
import com.maccasoft.propeller.expressions.CharacterLiteral;
import com.maccasoft.propeller.expressions.Context;
import com.maccasoft.propeller.expressions.Type;
import com.maccasoft.propeller.spin1.Spin1InstructionObject;
import com.maccasoft.propeller.spin1.Spin1PAsmExpression;
import com.maccasoft.propeller.spin1.Spin1PAsmInstructionFactory;
Expand Down Expand Up @@ -44,7 +45,18 @@ public int getSize() {
size += ((CharacterLiteral) exp.getExpression()).getString().length();
}
else {
size += exp.getCount();
int typeSize = 1;
if (exp.getExpression() instanceof Type) {
switch (((Type) exp.getExpression()).getType().toUpperCase()) {
case "WORD":
typeSize = 2;
break;
case "LONG":
typeSize = 4;
break;
}
}
size += exp.getCount() * typeSize;
}
}
return size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.maccasoft.propeller.CompilerException;
import com.maccasoft.propeller.expressions.CharacterLiteral;
import com.maccasoft.propeller.expressions.Context;
import com.maccasoft.propeller.expressions.Type;
import com.maccasoft.propeller.spin1.Spin1InstructionObject;
import com.maccasoft.propeller.spin1.Spin1PAsmExpression;

Expand Down Expand Up @@ -43,7 +44,18 @@ public int getSize() {
size += ((CharacterLiteral) exp.getExpression()).getString().length();
}
else {
size += exp.getCount();
int typeSize = 1;
if (exp.getExpression() instanceof Type) {
switch (((Type) exp.getExpression()).getType().toUpperCase()) {
case "WORD":
typeSize = 2;
break;
case "LONG":
typeSize = 4;
break;
}
}
size += exp.getCount() * typeSize;
}
}
return size;
Expand All @@ -60,7 +72,23 @@ public byte[] getBytes() {
os.write(exp.getExpression().getString().getBytes());
}
else {
if (exp.getInteger() < -0x80 || exp.getInteger() > 0xFF) {
if (exp.getExpression() instanceof Type) {
switch (((Type) exp.getExpression()).getType().toUpperCase()) {
case "WORD":
if (exp.getInteger() < -0x8000 || exp.getInteger() > 0xFFFF) {
throw new CompilerException("Word value must range from -$8000 to $FFFF", exp.getExpression().getData());
}
break;
case "LONG":
break;
default:
if (exp.getInteger() < -0x80 || exp.getInteger() > 0xFF) {
throw new CompilerException("Byte value must range from -$80 to $FF", exp.getExpression().getData());
}
break;
}
}
else if (exp.getInteger() < -0x80 || exp.getInteger() > 0xFF) {
throw new CompilerException("Byte value must range from -$80 to $FF", exp.getExpression().getData());
}
byte[] value = exp.getByte();
Expand Down
Loading

0 comments on commit f9c8456

Please sign in to comment.