From f34f9329f1069b0dd0ea6947134f4b8474a181e7 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 23 Jan 2021 19:49:53 +0100 Subject: [PATCH] fixed bug in memcopy --- compiler/res/prog8lib/c64/syslib.p8 | 26 ++++++++++------- docs/source/todo.rst | 1 - examples/test.p8 | 43 +++++++++++++++++------------ 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/compiler/res/prog8lib/c64/syslib.p8 b/compiler/res/prog8lib/c64/syslib.p8 index fcc97c4be..9ed05e340 100644 --- a/compiler/res/prog8lib/c64/syslib.p8 +++ b/compiler/res/prog8lib/c64/syslib.p8 @@ -500,19 +500,23 @@ sys { asmsub memcopy(uword source @R0, uword target @R1, uword count @AY) clobbers(A,X,Y) { %asm {{ ldx cx16.r0 - stx P8ZP_SCRATCH_W1 + stx P8ZP_SCRATCH_W1 ; source in ZP ldx cx16.r0+1 stx P8ZP_SCRATCH_W1+1 ldx cx16.r1 - stx P8ZP_SCRATCH_W2 + stx P8ZP_SCRATCH_W2 ; target in ZP ldx cx16.r1+1 stx P8ZP_SCRATCH_W2+1 cpy #0 bne _longcopy - ; copy <= 255 - tay -_remainder + ; copy <= 255 bytes + tay + bne _copyshort + rts ; nothing to copy + +_copyshort + ; decrease source and target pointers so we can simply index by Y lda P8ZP_SCRATCH_W1 bne + dec P8ZP_SCRATCH_W1+1 @@ -521,18 +525,19 @@ _remainder bne + dec P8ZP_SCRATCH_W2+1 + dec P8ZP_SCRATCH_W2 -- lda (P8ZP_SCRATCH_W1), y - sta (P8ZP_SCRATCH_W2), y + +- lda (P8ZP_SCRATCH_W1),y + sta (P8ZP_SCRATCH_W2),y dey bne - rts _longcopy - sta P8ZP_SCRATCH_B1 ; lsb(count) = remainder + sta P8ZP_SCRATCH_B1 ; lsb(count) = remainder in last page tya tax ; x = num pages (1+) ldy #0 -- lda (P8ZP_SCRATCH_W1),y ; copy a page at a time +- lda (P8ZP_SCRATCH_W1),y sta (P8ZP_SCRATCH_W2),y iny bne - @@ -541,7 +546,8 @@ _longcopy dex bne - ldy P8ZP_SCRATCH_B1 - jmp _remainder + bne _copyshort + rts }} } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 28551b145..fd1e0473f 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,7 +2,6 @@ TODO ==== -- fix sys.memcopy() - allow uwordpointer[index] syntax -> transform into @(uwordpointer+index) allow index to be >255! - add offsetof() to get the byte offset of struct members - add any2(), all2(), max2(), min2(), reverse2(), sum2(), sort2() that take (array, startindex, length) arguments diff --git a/examples/test.p8 b/examples/test.p8 index cac481529..601a5863d 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -8,26 +8,33 @@ main { sub start() { - ubyte[] sarray = [11,22,33] - ubyte[] tarray = [0,0,0] + ; TODO test memcopy + ; counts: 0, 1, 2, 254, 255, 256, 257, 512, 1000 - uword target = &tarray - uword source = &sarray - ubyte bb - @(target) = @(source) - target++ - source++ - @(target) = @(source) - target++ - source++ - @(target) = @(source) - target++ - source++ + uword buffer=memory("buffer",1000) + uword ones=memory("ones",1000) - for bb in tarray { - txt.print_ub(bb) - txt.chrout('\n') + sys.memset(buffer, 1000, '.') + @(buffer) = '<' + @(buffer+255) = '>' + @(buffer+256) = '<' + @(buffer+511) = '>' + @(buffer+512) = '<' + @(buffer+767) = '>' + @(buffer+768) = '<' + @(buffer+999) = '!' + sys.memset(ones, 1000, '*') + + txt.clear_screen() + txt.print("\n\n\n\n\n\n\n\n\n") + + sys.memcopy(ones, buffer, 999) + + uword scr = $0400 + uword ix + for ix in 0 to 999 { + @(scr) = @(buffer+ix) + scr++ } } - }