mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-08-09 04:25:12 +00:00
Added array-based bresenham
This commit is contained in:
@@ -3,17 +3,17 @@ package dk.camelot64.kickc.asm;
|
|||||||
/** 6502 Assembler Instruction Addressing Modes. */
|
/** 6502 Assembler Instruction Addressing Modes. */
|
||||||
public enum AsmAddressingMode {
|
public enum AsmAddressingMode {
|
||||||
NON("", "%i", 1),
|
NON("", "%i", 1),
|
||||||
IMM("#imm", "%i.imm #%p", 2),
|
IMM("#imm", "%i #%p", 2),
|
||||||
ZP("zp", "%i.zp %p", 2),
|
ZP("zp", "%i %p", 2),
|
||||||
ZPX("zp,x", "%i.zpx %p,x", 2),
|
ZPX("zp,x", "%i %p,x", 2),
|
||||||
ZPY("zp,y", "%i.zpy %p,y", 2),
|
ZPY("zp,y", "%i %p,y", 2),
|
||||||
ABS("abs", "%i.abs %p", 3),
|
ABS("abs", "%i %p", 3),
|
||||||
ABX("abs,x", "%i.abx %p,x", 3),
|
ABX("abs,x", "%i %p,x", 3),
|
||||||
ABY("abs,y", "%i.aby %p,y", 4),
|
ABY("abs,y", "%i %p,y", 4),
|
||||||
IZX("(zp,x)", "%i.izx (%p,x)", 2),
|
IZX("(zp,x)", "%i (%p,x)", 2),
|
||||||
IZY("(zp),y", "%i.izy (%p),y", 2),
|
IZY("(zp),y", "%i (%p),y", 2),
|
||||||
REL("rel", "%i.rel %p", 2),
|
REL("rel", "%i %p", 2),
|
||||||
IND("(ind)", "%i.ind (%p)", 3);
|
IND("(ind)", "%i (%p)", 3);
|
||||||
|
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
@@ -139,29 +139,12 @@ public class AsmFragment {
|
|||||||
this.signature = signature;
|
this.signature = signature;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Zero page register name indexing. */
|
||||||
* Zero page byte register name indexing.
|
|
||||||
*/
|
|
||||||
private int nextZpByteIdx = 1;
|
private int nextZpByteIdx = 1;
|
||||||
|
private int nextZpWordIdx = 1;
|
||||||
/**
|
|
||||||
* Zero page bool register name indexing.
|
|
||||||
*/
|
|
||||||
private int nextZpBoolIdx = 1;
|
private int nextZpBoolIdx = 1;
|
||||||
|
|
||||||
/**
|
|
||||||
* Zero page ptr register name indexing.
|
|
||||||
*/
|
|
||||||
private int nextZpPtrIdx = 1;
|
private int nextZpPtrIdx = 1;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constant byte indexing.
|
|
||||||
*/
|
|
||||||
private int nextConstByteIdx = 1;
|
private int nextConstByteIdx = 1;
|
||||||
|
|
||||||
/**
|
|
||||||
* Label indexing.
|
|
||||||
*/
|
|
||||||
private int nextLabelIdx = 1;
|
private int nextLabelIdx = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,6 +175,10 @@ public class AsmFragment {
|
|||||||
String name = "zpby" + nextZpByteIdx++;
|
String name = "zpby" + nextZpByteIdx++;
|
||||||
bindings.put(name, value);
|
bindings.put(name, value);
|
||||||
return name;
|
return name;
|
||||||
|
} else if (RegisterAllocation.RegisterType.ZP_WORD.equals(register.getType())) {
|
||||||
|
String name = "zpwo" + nextZpWordIdx++;
|
||||||
|
bindings.put(name, value);
|
||||||
|
return name;
|
||||||
} else if (RegisterAllocation.RegisterType.ZP_BOOL.equals(register.getType())) {
|
} else if (RegisterAllocation.RegisterType.ZP_BOOL.equals(register.getType())) {
|
||||||
String name = "zpbo" + nextZpBoolIdx++;
|
String name = "zpbo" + nextZpBoolIdx++;
|
||||||
bindings.put(name, value);
|
bindings.put(name, value);
|
||||||
@@ -259,6 +246,8 @@ public class AsmFragment {
|
|||||||
RegisterAllocation.Register register = (RegisterAllocation.Register) boundValue;
|
RegisterAllocation.Register register = (RegisterAllocation.Register) boundValue;
|
||||||
if (register instanceof RegisterAllocation.RegisterZpByte) {
|
if (register instanceof RegisterAllocation.RegisterZpByte) {
|
||||||
bound = Integer.toString(((RegisterAllocation.RegisterZpByte) register).getZp());
|
bound = Integer.toString(((RegisterAllocation.RegisterZpByte) register).getZp());
|
||||||
|
} else if (register instanceof RegisterAllocation.RegisterZpWord) {
|
||||||
|
bound = Integer.toString(((RegisterAllocation.RegisterZpWord) register).getZp());
|
||||||
} else if (register instanceof RegisterAllocation.RegisterZpBool) {
|
} else if (register instanceof RegisterAllocation.RegisterZpBool) {
|
||||||
bound = Integer.toString(((RegisterAllocation.RegisterZpBool) register).getZp());
|
bound = Integer.toString(((RegisterAllocation.RegisterZpBool) register).getZp());
|
||||||
} else if (register instanceof RegisterAllocation.RegisterZpPointerByte) {
|
} else if (register instanceof RegisterAllocation.RegisterZpPointerByte) {
|
||||||
|
@@ -0,0 +1,7 @@
|
|||||||
|
lda #<{cowo1}
|
||||||
|
clc
|
||||||
|
adc {zpwo1}
|
||||||
|
sta {zpptrby1}
|
||||||
|
lda #>{cowo1}
|
||||||
|
adc {zpwo1}+1
|
||||||
|
sta {zpptrby1}+1
|
@@ -0,0 +1,4 @@
|
|||||||
|
lda {zpptrby2}
|
||||||
|
sta {zpptrby1}
|
||||||
|
lda {zpptrby2}+1
|
||||||
|
sta {zpptrby1}+1
|
4
src/dk/camelot64/kickc/asm/fragment/zpwo1=coby1.asm
Normal file
4
src/dk/camelot64/kickc/asm/fragment/zpwo1=coby1.asm
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
lda #{coby1}
|
||||||
|
sta {zpwo1}
|
||||||
|
lda #0
|
||||||
|
sta {zpwo1}+1
|
@@ -0,0 +1,4 @@
|
|||||||
|
inc {zpwo1}
|
||||||
|
bne !+
|
||||||
|
inc {zpwo1}+1
|
||||||
|
!:
|
@@ -0,0 +1,7 @@
|
|||||||
|
lda {zpwo1}
|
||||||
|
clc
|
||||||
|
adc #<{coby1}
|
||||||
|
sta {zpwo1}
|
||||||
|
bcc !+
|
||||||
|
inc {zpwo1}+1
|
||||||
|
!:
|
4
src/dk/camelot64/kickc/asm/fragment/zpwo1=zpwo2.asm
Normal file
4
src/dk/camelot64/kickc/asm/fragment/zpwo1=zpwo2.asm
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
lda {zpwo2}
|
||||||
|
sta {zpwo1}
|
||||||
|
lda {zpwo2}+1
|
||||||
|
sta {zpwo1}+1
|
@@ -0,0 +1,7 @@
|
|||||||
|
lda {zpwo2}
|
||||||
|
clc
|
||||||
|
adc #1
|
||||||
|
sta {zpwo1}
|
||||||
|
lda {zpwo2}+1
|
||||||
|
adc #0
|
||||||
|
sta {zpwo1}+1
|
@@ -0,0 +1,7 @@
|
|||||||
|
lda {zpwo2}
|
||||||
|
clc
|
||||||
|
adc #<{coby1}
|
||||||
|
sta {zpwo1}
|
||||||
|
lda {zpwo2}+1
|
||||||
|
adc #0
|
||||||
|
sta {zpwo1}+1
|
@@ -18,13 +18,19 @@ public class Pass3RegisterAllocation {
|
|||||||
if(var instanceof VariableIntermediate || var instanceof VariableVersion)
|
if(var instanceof VariableIntermediate || var instanceof VariableVersion)
|
||||||
if(var.getType().equals(SymbolTypeBasic.BYTE)) {
|
if(var.getType().equals(SymbolTypeBasic.BYTE)) {
|
||||||
allocation.allocate(var, new RegisterAllocation.RegisterZpByte(currentZp++));
|
allocation.allocate(var, new RegisterAllocation.RegisterZpByte(currentZp++));
|
||||||
|
} else if(var.getType().equals(SymbolTypeBasic.WORD)) {
|
||||||
|
allocation.allocate(var, new RegisterAllocation.RegisterZpWord(currentZp));
|
||||||
|
currentZp = currentZp +2;
|
||||||
} else if(var.getType().equals(SymbolTypeBasic.BOOLEAN)) {
|
} else if(var.getType().equals(SymbolTypeBasic.BOOLEAN)) {
|
||||||
allocation.allocate(var, new RegisterAllocation.RegisterZpBool(currentZp++));
|
allocation.allocate(var, new RegisterAllocation.RegisterZpBool(currentZp++));
|
||||||
} else if(var.getType() instanceof SymbolTypePointer) {
|
} else if(var.getType() instanceof SymbolTypePointer) {
|
||||||
allocation.allocate(var, new RegisterAllocation.RegisterZpPointerByte(currentZp));
|
allocation.allocate(var, new RegisterAllocation.RegisterZpPointerByte(currentZp));
|
||||||
currentZp = currentZp +2;
|
currentZp = currentZp +2;
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Unhandled variable type "+var);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
allocation.allocate(symbols.getVariable("i#0"), RegisterAllocation.getRegisterX());
|
allocation.allocate(symbols.getVariable("i#0"), RegisterAllocation.getRegisterX());
|
||||||
allocation.allocate(symbols.getVariable("i#1"), RegisterAllocation.getRegisterX());
|
allocation.allocate(symbols.getVariable("i#1"), RegisterAllocation.getRegisterX());
|
||||||
allocation.allocate(symbols.getVariable("i#2"), RegisterAllocation.getRegisterX());
|
allocation.allocate(symbols.getVariable("i#2"), RegisterAllocation.getRegisterX());
|
||||||
@@ -34,19 +40,24 @@ public class Pass3RegisterAllocation {
|
|||||||
allocation.allocate(symbols.getVariable("ptr#1"), new RegisterAllocation.RegisterZpPointerByte(2));
|
allocation.allocate(symbols.getVariable("ptr#1"), new RegisterAllocation.RegisterZpPointerByte(2));
|
||||||
allocation.allocate(symbols.getVariable("ptr#2"), new RegisterAllocation.RegisterZpPointerByte(2));
|
allocation.allocate(symbols.getVariable("ptr#2"), new RegisterAllocation.RegisterZpPointerByte(2));
|
||||||
allocation.allocate(symbols.getVariable("ptr#3"), new RegisterAllocation.RegisterZpPointerByte(2));
|
allocation.allocate(symbols.getVariable("ptr#3"), new RegisterAllocation.RegisterZpPointerByte(2));
|
||||||
allocation.allocate(symbols.getVariable("y#1"), new RegisterAllocation.RegisterZpByte(4));
|
*/
|
||||||
allocation.allocate(symbols.getVariable("y#2"), new RegisterAllocation.RegisterZpByte(4));
|
allocation.allocate(symbols.getVariable("e#2"), new RegisterAllocation.RegisterZpByte(128));
|
||||||
allocation.allocate(symbols.getVariable("y#5"), new RegisterAllocation.RegisterZpByte(4));
|
allocation.allocate(symbols.getVariable("e#3"), new RegisterAllocation.RegisterZpByte(128));
|
||||||
allocation.allocate(symbols.getVariable("x#2"), new RegisterAllocation.RegisterZpByte(8));
|
allocation.allocate(symbols.getVariable("e#4"), new RegisterAllocation.RegisterZpByte(128));
|
||||||
allocation.allocate(symbols.getVariable("x#5"), new RegisterAllocation.RegisterZpByte(8));
|
allocation.allocate(symbols.getVariable("e#5"), new RegisterAllocation.RegisterZpByte(128));
|
||||||
allocation.allocate(symbols.getVariable("cursor#2"), new RegisterAllocation.RegisterZpPointerByte(5));
|
allocation.allocate(symbols.getVariable("idx#2"), new RegisterAllocation.RegisterZpWord(129));
|
||||||
allocation.allocate(symbols.getVariable("cursor#3"), new RegisterAllocation.RegisterZpPointerByte(5));
|
allocation.allocate(symbols.getVariable("idx#3"), new RegisterAllocation.RegisterZpWord(129));
|
||||||
allocation.allocate(symbols.getVariable("cursor#4"), new RegisterAllocation.RegisterZpPointerByte(5));
|
allocation.allocate(symbols.getVariable("idx#4"), new RegisterAllocation.RegisterZpWord(129));
|
||||||
allocation.allocate(symbols.getVariable("cursor#5"), new RegisterAllocation.RegisterZpPointerByte(5));
|
allocation.allocate(symbols.getVariable("idx#5"), new RegisterAllocation.RegisterZpWord(129));
|
||||||
allocation.allocate(symbols.getVariable("e#2"), new RegisterAllocation.RegisterZpByte(7));
|
allocation.allocate(symbols.getVariable("x#2"), new RegisterAllocation.RegisterZpByte(131));
|
||||||
allocation.allocate(symbols.getVariable("e#3"), new RegisterAllocation.RegisterZpByte(7));
|
allocation.allocate(symbols.getVariable("x#5"), new RegisterAllocation.RegisterZpByte(131));
|
||||||
allocation.allocate(symbols.getVariable("e#4"), new RegisterAllocation.RegisterZpByte(7));
|
allocation.allocate(symbols.getVariable("y#1"), new RegisterAllocation.RegisterZpByte(132));
|
||||||
allocation.allocate(symbols.getVariable("e#5"), new RegisterAllocation.RegisterZpByte(7));
|
allocation.allocate(symbols.getVariable("y#2"), new RegisterAllocation.RegisterZpByte(132));
|
||||||
|
allocation.allocate(symbols.getVariable("y#5"), new RegisterAllocation.RegisterZpByte(132));
|
||||||
|
allocation.allocate(symbols.getVariable("cursor#2"), new RegisterAllocation.RegisterZpPointerByte(133));
|
||||||
|
allocation.allocate(symbols.getVariable("cursor#3"), new RegisterAllocation.RegisterZpPointerByte(133));
|
||||||
|
allocation.allocate(symbols.getVariable("cursor#4"), new RegisterAllocation.RegisterZpPointerByte(133));
|
||||||
|
allocation.allocate(symbols.getVariable("cursor#5"), new RegisterAllocation.RegisterZpPointerByte(133));
|
||||||
symbols.setAllocation(allocation);
|
symbols.setAllocation(allocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -36,7 +36,7 @@ public class RegisterAllocation {
|
|||||||
|
|
||||||
/** The register type. */
|
/** The register type. */
|
||||||
public enum RegisterType {
|
public enum RegisterType {
|
||||||
ZP_BYTE, ZP_BOOL, REG_Y_BYTE, REG_X_BYTE, ZP_PTR_BYTE
|
ZP_BYTE, ZP_BOOL, REG_Y_BYTE, REG_X_BYTE, ZP_PTR_BYTE, ZP_WORD
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A zero page address used as a register for a single byte variable. */
|
/** A zero page address used as a register for a single byte variable. */
|
||||||
@@ -76,6 +76,44 @@ public class RegisterAllocation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Two zero page addresses used as a register for a single word variable. */
|
||||||
|
public static class RegisterZpWord implements Register {
|
||||||
|
|
||||||
|
private int zp;
|
||||||
|
|
||||||
|
public RegisterZpWord(int zp) {
|
||||||
|
this.zp = zp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getZp() {
|
||||||
|
return zp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RegisterType getType() {
|
||||||
|
return RegisterType.ZP_WORD;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "zp word :"+zp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
RegisterZpWord that = (RegisterZpWord) o;
|
||||||
|
return zp == that.zp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return zp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** A zero page address used as a register for a boolean variable. */
|
/** A zero page address used as a register for a boolean variable. */
|
||||||
public static class RegisterZpBool implements Register {
|
public static class RegisterZpBool implements Register {
|
||||||
private int zp;
|
private int zp;
|
||||||
@@ -127,7 +165,7 @@ public class RegisterAllocation {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "zp ptrbyte:"+zp;
|
return "zp ptr byte:"+zp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
23
src/dk/camelot64/kickc/test/bresenhamarr.kc
Normal file
23
src/dk/camelot64/kickc/test/bresenhamarr.kc
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
byte STAR = 81;
|
||||||
|
byte[40*25] screen = $0400;
|
||||||
|
byte x0 = 0;
|
||||||
|
byte y0 = 0;
|
||||||
|
byte x1 = 39;
|
||||||
|
byte y1 = 24;
|
||||||
|
byte xd = x1-x0;
|
||||||
|
byte yd = y1-y0;
|
||||||
|
byte x = x0;
|
||||||
|
byte y = y0;
|
||||||
|
byte e = yd/2;
|
||||||
|
word idx = x+y*40;
|
||||||
|
do {
|
||||||
|
screen[idx] = STAR;
|
||||||
|
x = x + 1;
|
||||||
|
idx = idx + 1;
|
||||||
|
e = e+yd;
|
||||||
|
if(xd<e) {
|
||||||
|
y = y+1;
|
||||||
|
idx = idx + 40;
|
||||||
|
e = e - xd;
|
||||||
|
}
|
||||||
|
} while (x<(x1+1))
|
Reference in New Issue
Block a user