integer word division

This commit is contained in:
Irmen de Jong 2019-01-06 21:46:35 +01:00
parent ebee8c128f
commit 3b6ffc47b2
7 changed files with 68 additions and 72 deletions

View File

@ -63,32 +63,26 @@
sub draw_edges() {
sub toscreenx(float x, float z) -> byte {
return x/(5.0+z) * (height as float) as byte + width // 2
}
sub toscreeny(float y, float z) -> byte {
return y/(5.0+z) * (height as float) as byte + height // 2
}
; plot the points of the 3d cube
; first the points on the back, then the points on the front (painter algorithm)
for ubyte i in 0 to len(xcoor)-1 {
float rz = rotatedz[i]
if rz >= 0.1 {
ubyte sx = toscreenx(rotatedx[i], rz) as ubyte
ubyte sy = toscreeny(rotatedy[i], rz) as ubyte
c64scr.setchrclr(sx, sy, 46, i+2)
float persp = (5.0+rz)/height
ubyte sx = rotatedx[i] / persp + width//2 as ubyte
ubyte sy = rotatedy[i] / persp + height//2 as ubyte
c64scr.setcc(sx, sy, 46, i+2)
}
}
for ubyte i in 0 to len(xcoor)-1 {
float rz = rotatedz[i]
if rz < 0.1 {
ubyte sx = toscreenx(rotatedx[i], rz) as ubyte
ubyte sy = toscreeny(rotatedy[i], rz) as ubyte
c64scr.setchrclr(sx, sy, 81, i+2)
float persp = (5.0+rz)/height
ubyte sx = rotatedx[i] / persp + width//2 as ubyte
ubyte sy = rotatedy[i] / persp + height//2 as ubyte
c64scr.setcc(sx, sy, 81, i+2)
}
}
}

View File

@ -1,5 +1,4 @@
%import c64utils
%import c64flt
~ main {
@ -74,30 +73,20 @@
for ubyte i in 0 to len(xcoor)-1 {
word rz = rotatedz[i]
if rz >= 10 {
const float height_f = height
float persp = (rz as float + 180.0)/height_f
byte sx = rotatedx[i] as float / persp as byte + width//2
byte sy = rotatedy[i] as float / persp as byte + height//2
; @todo switch to this once idiv_w is implemented: (and remove c64flt import)
;word persp = (rz+180) // height
;byte sx = rotatedx[i] // persp as byte + width//2
;byte sy = rotatedy[i] // persp as byte + height//2
c64scr.setchrclr(sx as ubyte, sy as ubyte, 46, i+2)
word persp = (rz+200) // height
byte sx = rotatedx[i] // persp as byte + width//2
byte sy = rotatedy[i] // persp as byte + height//2
c64scr.setcc(sx as ubyte, sy as ubyte, 46, i+2)
}
}
for ubyte i in 0 to len(xcoor)-1 {
word rz = rotatedz[i]
if rz < 10 {
const float height_f = height
float persp = (rz as float + 180.0)/height_f
byte sx = rotatedx[i] as float / persp as byte + width//2
byte sy = rotatedy[i] as float / persp as byte + height//2
; @todo switch to this once idiv_w is implemented: (and remove c64flt import)
;word persp = (rz+180) // height
;byte sx = rotatedx[i] // persp as byte + width//2
;byte sy = rotatedy[i] // persp as byte + height//2
c64scr.setchrclr(sx as ubyte, sy as ubyte, 81, i+2)
word persp = (rz+200) // height
byte sx = rotatedx[i] // persp as byte + width//2
byte sy = rotatedy[i] // persp as byte + height//2
c64scr.setcc(sx as ubyte, sy as ubyte, 81, i+2)
}
}
}

View File

@ -14,23 +14,11 @@
while true {
float x = sin(t)
float y = cos(t*1.1356)
ubyte xx=screenx(x)
ubyte yy=screeny(y)
;c64.COLOR = color
;c64scr.PLOT(xx,yy)
;c64.CHROUT('Q') ; shift-q = filled circle
c64scr.setchrclr(xx, yy, 81, color)
ubyte xx=(x * width/2.2) + width/2.0 as ubyte
ubyte yy=(y * height/2.2) + height/2.0 as ubyte
c64scr.setcc(xx, yy, 81, color)
t += 0.08
color++
}
}
sub screenx(float x) -> ubyte {
return (x * width/2.2) + width/2.0 as ubyte
}
sub screeny(float y) -> ubyte {
return (y * height/2.2) + height/2.0 as ubyte
}
}

View File

@ -16,7 +16,7 @@
word y = cos8(msb(angley)) as word
ubyte xx=msb(x*39) + 20 ; -127..127 -> 0..39
ubyte yy=msb(y*24) + 12 ; -127..127 -> 0..24
c64scr.setchrclr(xx, yy, 81, color)
c64scr.setcc(xx, yy, 81, color)
anglex+=800
angley+=947

View File

@ -21,32 +21,33 @@
word w4
sub start() {
w1 = 120
w2 = -13
w1 = 12000
w2 = -333 ; -333
w3 = w1 // w2
c64scr.print_w(w3)
c64.CHROUT(':')
w1 = -120
w2 = 13
c64.CHROUT('\n')
w1 = -12000 ; -12000
w2 = 333
w3 = w1 // w2
c64scr.print_w(w3)
c64.CHROUT(':')
w2 = 13
c64.CHROUT('\n')
w2 = 4444
w3 = w1 // w2
c64scr.print_w(w3)
c64.CHROUT(':')
c64.CHROUT('\n')
w3 = w1 // -5
w3 = w1 // -5 ; -5
c64scr.print_w(w3)
c64.CHROUT(':')
c64.CHROUT('\n')
w3 = w1 // -7
w3 = w1 // -7 ; -7
c64scr.print_w(w3)
c64.CHROUT(':')
c64.CHROUT('\n')
w3 = w1 // -99
w1 = -w1
w3 = w1 // -999 ;-999
c64scr.print_w(w3)
c64.CHROUT(':')
c64.CHROUT('\n')
c64scr.print_ub(X)
c64.CHROUT('\n')

View File

@ -1065,10 +1065,10 @@ _colorrows .word $d800 + range(0, 1000, 40)
}
sub setchrclr (ubyte column, ubyte row, ubyte char, ubyte color) {
sub setcc (ubyte column, ubyte row, ubyte char, ubyte color) {
; ---- set char+color at the given position on the screen
%asm {{
lda setchrclr_row
lda setcc_row
asl a
tay
lda setchr._screenrows+1,y
@ -1077,15 +1077,15 @@ sub setchrclr (ubyte column, ubyte row, ubyte char, ubyte color) {
sta _colormod+2
lda setchr._screenrows,y
clc
adc setchrclr_column
adc setcc_column
sta _charmod+1
sta _colormod+1
bcc +
inc _charmod+2
inc _colormod+2
+ lda setchrclr_char
+ lda setcc_char
_charmod sta $ffff ; modified
lda setchrclr_color
lda setcc_color
_colormod sta $ffff ; modified
rts
}}

View File

@ -165,11 +165,11 @@ mul_word .proc
idiv_b .proc
; signed division: use unsigned division and fix sign of result afterwards
lda c64.ESTACK_LO+1,x
eor c64.ESTACK_LO+2,x
php ; save sign of result
inx
lda c64.ESTACK_LO,x
eor c64.ESTACK_LO+1,x
php ; save sign of result
lda c64.ESTACK_LO,x
bpl +
eor #$ff
sec
@ -206,7 +206,31 @@ idiv_ub .proc
.pend
idiv_w .proc
.error "idiv_w not yet implemented"
; signed division: use unsigned division and fix sign of result afterwards
lda c64.ESTACK_HI+2,x
eor c64.ESTACK_HI+1,x
php ; save sign of result
lda c64.ESTACK_HI+1,x
bpl +
jsr neg_w ; make value positive
+ inx
lda c64.ESTACK_HI+1,x
bpl +
jsr neg_w ; make value positive
+ lda c64.ESTACK_LO+1,x
sta c64.SCRATCH_ZPWORD1
lda c64.ESTACK_HI+1,x
sta c64.SCRATCH_ZPWORD1+1
lda c64.ESTACK_LO,x
ldy c64.ESTACK_HI,x
jsr math.divmod_uw_asm
sta c64.ESTACK_LO+1,x
tya
sta c64.ESTACK_HI+1,x
plp
bpl +
jmp neg_w ; negate result
+ rts
.pend
idiv_uw .proc