fix asm stack bug

This commit is contained in:
Irmen de Jong 2019-01-05 18:02:17 +01:00
parent 9f4ac37a00
commit 4f213191dc
4 changed files with 59 additions and 103 deletions

View File

@ -1,4 +1,5 @@
%import c64utils %import c64utils
%import c64flt
~ main { ~ main {
@ -6,104 +7,89 @@
const uword height = 25 const uword height = 25
; vertices ; vertices
byte[8] xcoor = [ -100, -100, -100, -100, 100, 100, 100, 100 ] byte[8] xcoor = [ -50, -50, -50, -50, 50, 50, 50, 50 ]
byte[8] ycoor = [ -100, -100, 100, 100, -100, -100, 100, 100 ] byte[8] ycoor = [ -50, -50, 50, 50, -50, -50, 50, 50 ]
byte[8] zcoor = [ -100, 100, -100, 100, -100, 100, -100, 100 ] byte[8] zcoor = [ -50, 50, -50, 50, -50, 50, -50, 50 ]
; storage for rotated coordinates ; storage for rotated coordinates
word[len(xcoor)] rotatedx=0 float[len(xcoor)] rotatedx=0.0
word[len(ycoor)] rotatedy=0 float[len(ycoor)] rotatedy=0.0
word[len(zcoor)] rotatedz=-32767 float[len(zcoor)] rotatedz=-1.0
sub start() { sub start() {
uword anglex uword anglex
uword angley uword angley
uword anglez uword anglez
while(true) { while(true) {
rotate_vertices(anglex, angley, anglez) rotate_vertices(msb(anglex), msb(angley), msb(anglez))
; c64.CLEARSCR() c64.CLEARSCR()
; draw_edges() draw_edges()
anglex+=256 anglex+=1000
angley+=83 angley+=333
anglez+=201 anglez+=807
} }
} }
sub rotate_vertices(uword ax, uword ay, uword az) { sub rotate_vertices(ubyte ax, ubyte ay, ubyte az) {
; rotate around origin (0,0,0) ; rotate around origin (0,0,0)
; set up the 3d rotation matrix values ; set up the 3d rotation matrix values
word cosa = cos8(msb(ax)) as word float cosa = cos8(ax) as float / 128.0
word sina = sin8(msb(ax)) as word float sina = sin8(ax) as float / 128.0
word cosb = cos8(msb(ay)) as word float cosb = cos8(ay) as float / 128.0
word sinb = sin8(msb(ay)) as word float sinb = sin8(ay) as float / 128.0
word cosc = cos8(msb(az)) as word float cosc = cos8(az) as float / 128.0
word sinc = sin8(msb(az)) as word float sinc = sin8(az) as float / 128.0
word cosa_sinb = msb(cosa*sinb) float cosa_sinb = cosa*sinb
word sina_sinb = msb(sina*sinb) float sina_sinb = sina*sinb
float Axx = cosa*cosb
c64.CHROUT('>') float Axy = cosa_sinb*sinc - sina*cosc
c64scr.print_w(cosa_sinb) float Axz = cosa_sinb*cosc + sina*sinc
c64.CHROUT(',') float Ayx = sina*cosb
c64scr.print_w(sina_sinb) float Ayy = sina_sinb*sinc + cosa*cosc
c64.CHROUT('\n') float Ayz = sina_sinb*cosc - cosa*sinc
float Azx = -sinb
word Axx = msb(cosa*cosb) float Azy = cosb*sinc
word Axy = msb(cosa_sinb*sinc - sina*cosc) float Azz = cosb*cosc
word Axz = msb(cosa_sinb*cosc + sina*sinc)
word Ayx = msb(sina*cosb)
word Ayy = msb(sina_sinb*sinc + cosa*cosc)
word Ayz = msb(sina_sinb*cosc - cosa*sinc)
word Azx = -sinb
word Azy = msb(cosb*sinc)
word Azz = msb(cosb*cosc)
c64.CHROUT('>')
c64scr.print_w(Axx)
c64.CHROUT(',')
c64scr.print_w(Axy)
c64.CHROUT(',')
c64scr.print_w(Axz)
c64.CHROUT('\n')
for ubyte i in 0 to len(xcoor)-1 { for ubyte i in 0 to len(xcoor)-1 {
word xc = xcoor[i] float xc = xcoor[i] as float
word yc = ycoor[i] float yc = ycoor[i] as float
word zc = zcoor[i] float zc = zcoor[i] as float
rotatedx[i] = Axx*xc ;+ Axy*yc + Axz*zc ; @todo wrong code generated? crash! rotatedx[i] = Axx*xc + Axy*yc + Axz*zc
rotatedy[i] = Ayx*xc ;+ Ayy*yc + Ayz*zc ; @todo wrong code generated? crash! rotatedy[i] = Ayx*xc + Ayy*yc + Ayz*zc
rotatedz[i] = Azx*xc ;+ Azy*yc + Azz*zc ; @todo wrong code generated? crash! rotatedz[i] = Azx*xc + Azy*yc + Azz*zc
} }
} }
sub draw_edges() { sub draw_edges() {
sub toscreenx(word x, word z) -> ubyte { sub toscreenx(float x, float z) -> byte {
return msb(x) and 31 return x/(250.0+z) * (height as float) as byte + width // 2
} }
sub toscreeny(word y, word z) -> ubyte { sub toscreeny(float y, float z) -> byte {
return msb(y) and 15 return y/(250.0+z) * (height as float) as byte + height // 2
} }
; plot the points of the 3d cube ; plot the points of the 3d cube
; first the points on the back, then the points on the front (painter algorithm) ; first the points on the back, then the points on the front (painter algorithm)
for ubyte i in 0 to len(xcoor)-1 { for ubyte i in 0 to len(xcoor)-1 {
word rz = rotatedz[i] float rz = rotatedz[i]
if rz >= 100 { if rz >= 0.1 {
ubyte sx = toscreenx(rotatedx[i], rz) ubyte sx = toscreenx(rotatedx[i], rz) as ubyte
ubyte sy = toscreeny(rotatedy[i], rz) ubyte sy = toscreeny(rotatedy[i], rz) as ubyte
c64scr.setchrclr(sx, sy, 46, i+2) c64scr.setchrclr(sx, sy, 46, i+2)
} }
} }
for ubyte i in 0 to len(xcoor)-1 { for ubyte i in 0 to len(xcoor)-1 {
word rz = rotatedz[i] float rz = rotatedz[i]
if rz < 100 { if rz < 0.1 {
ubyte sx = toscreenx(rotatedx[i], rz) ubyte sx = toscreenx(rotatedx[i], rz) as ubyte
ubyte sy = toscreeny(rotatedy[i], rz) ubyte sy = toscreeny(rotatedy[i], rz) as ubyte
c64scr.setchrclr(sx, sy, 81, i+2) c64scr.setchrclr(sx, sy, 81, i+2)
} }
} }

View File

@ -2,44 +2,15 @@
~ main { ~ main {
word[1] rotatedx
sub start() { sub start() {
memory byte b1 = $c000 word xc = 2
memory byte b2 = $c001 rpt:
memory word w1 = $c002 word w = 2*xc
memory word w2 = $c004 rotatedx[0] = w ; @ok!
rotatedx[0] = 2*xc ; @todo wrong code generated? crash!
float x =4.34 c64.CHROUT('.')
ubyte xx= x as ubyte goto rpt
float y = x * 5.55
y =xx as float
b1=50
b2=-50
w1=100
w2=-100
c64scr.print_b(b1)
c64.CHROUT('/')
lsr(b1)
c64scr.print_b(b1)
c64.CHROUT('\n')
c64scr.print_b(b2)
c64.CHROUT('/')
lsr(b2)
c64scr.print_b(b2)
c64.CHROUT('\n')
c64scr.print_w(w1)
c64.CHROUT('/')
lsr(w1)
c64scr.print_w(w1)
c64.CHROUT('\n')
c64scr.print_w(w2)
c64.CHROUT('/')
lsr(w2)
c64scr.print_w(w2)
c64.CHROUT('\n')
} }
} }

View File

@ -564,7 +564,6 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram,
Opcode.WRITE_INDEXED_VAR_WORD -> { Opcode.WRITE_INDEXED_VAR_WORD -> {
""" """
inx inx
inx
lda ${ESTACK_LO.toHex()},x lda ${ESTACK_LO.toHex()},x
asl a asl a
tay tay