From 8902730756f7dc6037cea35ea08c7511eb89cb4b Mon Sep 17 00:00:00 2001
From: mrdudz <mrdudz@users.noreply.github.com>
Date: Mon, 11 Dec 2017 19:49:14 +0100
Subject: [PATCH 1/8] cbm stuff from greggs pull request

---
 asminc/c128.inc             |  12 ++-
 doc/funcref.sgml            | 121 +++++++++++++++++++++++-
 include/cbm610.h            |   4 +-
 include/conio.h             |  20 ++++
 include/pet.h               |   1 +
 libsrc/c128/cpeekc.s        |  65 +++++++++++++
 libsrc/c128/cpeekcolor.s    |  56 ++++++++++++
 libsrc/c128/cpeekrevers.s   |  51 +++++++++++
 libsrc/c128/cpeeks.s        |  15 +++
 libsrc/cbm/cpeekc.s         |  53 +++++++++++
 libsrc/cbm/cpeekcolor.s     |  28 ++++++
 libsrc/cbm/cpeekrevers.s    |  35 +++++++
 libsrc/cbm/cpeeks.s         |  93 +++++++++++++++++++
 libsrc/cbm510/cpeekcolor.s  |  24 +++++
 libsrc/cbm510/doesclrscr.s  |  16 ++++
 libsrc/cbm610/cpeekc.s      |  45 +++++++++
 libsrc/cbm610/cpeekcolor.s  |   8 ++
 libsrc/cbm610/cpeekrevers.s |  29 ++++++
 libsrc/cbm610/cpeeks.s      |  82 +++++++++++++++++
 libsrc/pet/cpeekcolor.s     |   8 ++
 testcode/lib/cpeek-test.c   | 177 ++++++++++++++++++++++++++++++++++++
 21 files changed, 936 insertions(+), 7 deletions(-)
 create mode 100644 libsrc/c128/cpeekc.s
 create mode 100644 libsrc/c128/cpeekcolor.s
 create mode 100644 libsrc/c128/cpeekrevers.s
 create mode 100644 libsrc/c128/cpeeks.s
 create mode 100644 libsrc/cbm/cpeekc.s
 create mode 100644 libsrc/cbm/cpeekcolor.s
 create mode 100644 libsrc/cbm/cpeekrevers.s
 create mode 100644 libsrc/cbm/cpeeks.s
 create mode 100644 libsrc/cbm510/cpeekcolor.s
 create mode 100644 libsrc/cbm510/doesclrscr.s
 create mode 100644 libsrc/cbm610/cpeekc.s
 create mode 100644 libsrc/cbm610/cpeekcolor.s
 create mode 100644 libsrc/cbm610/cpeekrevers.s
 create mode 100644 libsrc/cbm610/cpeeks.s
 create mode 100644 libsrc/pet/cpeekcolor.s
 create mode 100644 testcode/lib/cpeek-test.c

diff --git a/asminc/c128.inc b/asminc/c128.inc
index 745a3ad6d..45ac3935b 100644
--- a/asminc/c128.inc
+++ b/asminc/c128.inc
@@ -15,7 +15,7 @@ FNAM            := $BB          ; Address of filename
 FNAM_BANK       := $C7          ; Bank for filename
 KEY_COUNT       := $D0          ; Number of keys in input buffer
 FKEY_COUNT      := $D1          ; Characters for function key
-MODE            := $D7          ; 40/80 column mode flag
+MODE            := $D7          ; 40-/80-column mode (bit 7: 80 columns)
 CURS_X          := $EC          ; Cursor column
 CURS_Y          := $EB          ; Cursor row
 SCREEN_PTR      := $E0          ; Pointer to current char in text screen
@@ -167,8 +167,14 @@ SID_Read3       := $D41C
 ; ---------------------------------------------------------------------------
 ; I/O: VDC (128 only)
 
-VDC_INDEX       := $D600
-VDC_DATA        := $D601
+VDC_INDEX       := $D600        ; register address port
+VDC_DATA        := $D601        ; data port
+
+; Registers
+VDC_DATA_HI     = 18            ; video RAM address (big endian)
+VDC_DATA_LO     = 19
+VDC_CSET        = 28
+VDC_RAM_RW      = 31            ; RAM port
 
 ; ---------------------------------------------------------------------------
 ; I/O: Complex Interface Adapters
diff --git a/doc/funcref.sgml b/doc/funcref.sgml
index d997e0a8f..b198adcf8 100644
--- a/doc/funcref.sgml
+++ b/doc/funcref.sgml
@@ -4,7 +4,7 @@
 <title>cc65 function reference
 <author><url url="mailto:uz@cc65.org" name="Ullrich von Bassewitz">,<newline>
 <url url="mailto:greg.king5@verizon.net" name="Greg King">
-<date>2017-11-21
+<date>2017-12-09
 
 <abstract>
 cc65 is a C compiler for 6502 based systems. This function reference describes
@@ -240,6 +240,10 @@ function.
 <item><ref id="chline" name="chline">
 <item><ref id="chlinexy" name="chlinexy">
 <item><ref id="clrscr" name="clrscr">
+<item><ref id="cpeekc" name="cpeekc">
+<item><ref id="cpeekcolor" name="cpeekcolor">
+<item><ref id="cpeekrevers" name="cpeekrevers">
+<item><ref id="cpeeks" name="cpeeks">
 <item><ref id="cprintf" name="cprintf">
 <item><ref id="cputc" name="cputc">
 <item><ref id="cputcxy" name="cputcxy">
@@ -2363,6 +2367,121 @@ be used in presence of a prototype.
 </quote>
 
 
+<sect1>cpeekc<label id="cpeekc"><p>
+
+<quote>
+<descrip>
+<tag/Function/Get a character from the display memory.
+<tag/Header/<tt/<ref id="conio.h" name="conio.h">/
+<tag/Declaration/<tt/char cpeekc (void);/
+<tag/Description/The function gets the character that's at the current location
+of the cursor in the display screen RAM.  That character is converted, if
+needed, into the encoding that can be passed to <tt/cputc()/.
+<tag/Notes/<itemize>
+<item>Conio peek functions don't have <tt/cpeek...xy()/ versions.  That was
+done to make it obvious that peeking doesn't move the cursor in any way.  Your
+program must place the cursor where it wants to peek before it calls any of
+those functions.
+</itemize>
+<tag/Availability/cc65
+<tag/See also/
+<ref id="cpeekcolor" name="cpeekcolor">,
+<ref id="cpeekrevers" name="cpeekrevers">,
+<ref id="cpeeks" name="cpeeks">,
+<ref id="cputc" name="cputc">
+<tag/Example/None.
+</descrip>
+</quote>
+
+
+<sect1>cpeekcolor<label id="cpeekcolor"><p>
+
+<quote>
+<descrip>
+<tag/Function/Get a color from the display memory.
+<tag/Header/<tt/<ref id="conio.h" name="conio.h">/
+<tag/Declaration/<tt/unsigned char cpeekcolor (void);/
+<tag/Description/The function gets the color number that's at the current
+location of the cursor in the display screen RAM.  That number can be passed to
+<tt/textcolor()/.
+<tag/Notes/<itemize>
+<item>Conio peek functions don't have <tt/cpeek...xy()/ versions.  That was
+done to make it obvious that peeking doesn't move the cursor in any way.  Your
+program must place the cursor where it wants to peek before it calls any of
+those functions.
+</itemize>
+<tag/Availability/cc65
+<tag/See also/
+<ref id="cpeekc" name="cpeekc">,
+<ref id="cpeekrevers" name="cpeekrevers">,
+<ref id="cpeeks" name="cpeeks">,
+<ref id="cputc" name="cputc">,
+<ref id="textcolor" name="textcolor">
+<tag/Example/None.
+</descrip>
+</quote>
+
+
+<sect1>cpeekrevers<label id="cpeekrevers"><p>
+
+<quote>
+<descrip>
+<tag/Function/Get a reverse-character attribute from the display memory.
+<tag/Header/<tt/<ref id="conio.h" name="conio.h">/
+<tag/Declaration/<tt/unsigned char cpeekrevers (void);/
+<tag/Description/The function gets the "reverse-mode" attribute of the
+character that's at the current location of the cursor in the display screen
+RAM.  It returns a boolean value (0/1) that can be passed to <tt/revers()/.
+<tag/Notes/<itemize>
+<item>Conio peek functions don't have <tt/cpeek...xy()/ versions.  That was
+done to make it obvious that peeking doesn't move the cursor in any way.  Your
+program must place the cursor where it wants to peek before it calls any of
+those functions.
+</itemize>
+<tag/Availability/cc65
+<tag/See also/
+<ref id="cpeekc" name="cpeekc">,
+<ref id="cpeekcolor" name="cpeekcolor">,
+<ref id="cpeeks" name="cpeeks">,
+<ref id="cputc" name="cputc">,
+<ref id="revers" name="revers">
+<tag/Example/None.
+</descrip>
+</quote>
+
+
+<sect1>cpeeks<label id="cpeeks"><p>
+
+<quote>
+<descrip>
+<tag/Function/Get a string from the display memory.
+<tag/Header/<tt/<ref id="conio.h" name="conio.h">/
+<tag/Declaration/<tt/void __fastcall__ cpeeks (char* s, unsigned length);/
+<tag/Description/The function gets a fixed-length string ('\0'-terminated) of
+characters that start at the current location of the cursor in the display
+screen RAM.  Those characters are converted, if needed, into the encoding that
+can be passed to <tt/cputs()/.  The first argument must point to a RAM area
+that's large enough to hold "length + 1" bytes.
+<tag/Notes/<itemize>
+<item>Conio peek functions don't have <tt/cpeek...xy()/ versions.  That was
+done to make it obvious that peeking doesn't move the cursor in any way.  Your
+program must place the cursor where it wants to peek before it calls any of
+those functions.
+<item>The function is available as only a fastcall function;
+so, it may be used only in the presence of a prototype.
+</itemize>
+<tag/Availability/cc65
+<tag/See also/
+<ref id="cpeekc" name="cpeekc">,
+<ref id="cpeekcolor" name="cpeekcolor">,
+<ref id="cpeekrevers" name="cpeekrevers">,
+<ref id="cputc" name="cputc">,
+<ref id="cputs" name="cputs">
+<tag/Example/None.
+</descrip>
+</quote>
+
+
 <sect1>creat<label id="creat"><p>
 
 <quote>
diff --git a/include/cbm610.h b/include/cbm610.h
index 79d498431..b0486044b 100644
--- a/include/cbm610.h
+++ b/include/cbm610.h
@@ -144,11 +144,9 @@ void __fastcall__ pokewsys (unsigned addr, unsigned val);
 #define _textcolor(color)       COLOR_WHITE
 #define _bgcolor(color)         COLOR_BLACK
 #define _bordercolor(color)     COLOR_BLACK
+#define _cpeekcolor(color)      COLOR_WHITE
 
 
 
 /* End of cbm610.h */
 #endif
-
-
-
diff --git a/include/conio.h b/include/conio.h
index baf55dd08..9cd505766 100644
--- a/include/conio.h
+++ b/include/conio.h
@@ -148,6 +148,23 @@ int cscanf (const char* format, ...);
 int __fastcall__ vcscanf (const char* format, va_list ap);
 /* Like vscanf(), but uses direct keyboard input */
 
+char cpeekc (void);
+/* Return the character from the current cursor position */
+
+unsigned char cpeekcolor (void);
+/* Return the color from the current cursor position */
+
+unsigned char cpeekrevers (void);
+/* Return the reverse attribute from the current cursor position.
+** If the character is reversed, then return 1; return 0 otherwise.
+*/
+
+void __fastcall__ cpeeks (char* s, unsigned int length);
+/* Return a string of the characters that start at the current cursor position.
+** Put the string into the buffer to which "s" points.  The string will have
+** "length" characters, then will be '\0'-terminated.
+*/
+
 unsigned char __fastcall__ cursor (unsigned char onoff);
 /* If onoff is 1, a cursor is displayed when waiting for keyboard input. If
 ** onoff is 0, the cursor is hidden when waiting for keyboard input. The
@@ -224,6 +241,9 @@ void __fastcall__ cputhex16 (unsigned val);
 #if defined(_bordercolor)
 #  define bordercolor(x)        _bordercolor(x)
 #endif
+#if defined(_cpeekcolor)
+#  define cpeekcolor(x)         _cpeekcolor(x)
+#endif
 
 
 
diff --git a/include/pet.h b/include/pet.h
index e9659d524..249d8b608 100644
--- a/include/pet.h
+++ b/include/pet.h
@@ -105,6 +105,7 @@ extern void pet_stdjoy_joy[];       /* Referred to by joy_static_stddrv[] */
 #define _textcolor(color)       COLOR_WHITE
 #define _bgcolor(color)         COLOR_BLACK
 #define _bordercolor(color)     COLOR_BLACK
+#define _cpeekcolor(color)      COLOR_WHITE
 
 
 
diff --git a/libsrc/c128/cpeekc.s b/libsrc/c128/cpeekc.s
new file mode 100644
index 000000000..ce6bb4ff5
--- /dev/null
+++ b/libsrc/c128/cpeekc.s
@@ -0,0 +1,65 @@
+;
+; 2016-02-28, Groepaz
+; 2017-06-26, Greg King
+;
+; char cpeekc (void);
+;
+
+        .export         _cpeekc
+
+        .import         plot, popa
+
+        .include        "zeropage.inc"
+        .include        "c128.inc"
+
+
+_cpeekc:
+        lda     MODE
+        bmi     @c80
+
+        ldy     CURS_X
+        lda     (SCREEN_PTR),y  ; get char
+
+@return:
+        and     #<~$80          ; remove reverse flag
+
+; Convert the screen code into a PetSCII code.
+; $00 - $1F: +$40
+; $20 - $3F
+; $40 - $7F: +$80
+
+        cmp     #$20
+        bcs     @sk1            ;(bge)
+        ora     #$40
+        rts
+
+@sk1:   cmp     #$40
+        bcc     @end            ;(blt)
+        ora     #$80
+@end:   ldx     #0
+        rts
+
+@c80:
+        lda     SCREEN_PTR
+        ldy     SCREEN_PTR+1
+        clc
+        adc     CURS_X
+        bcc     @s
+        iny
+
+        ; get byte from VDC mem
+@s:     ldx     #VDC_DATA_LO
+        stx     VDC_INDEX
+@L0:    bit     VDC_INDEX
+        bpl     @L0
+        sta     VDC_DATA
+        dex
+        stx     VDC_INDEX
+        sty     VDC_DATA
+
+        ldx     #VDC_RAM_RW
+        stx     VDC_INDEX
+@L1:    bit     VDC_INDEX
+        bpl     @L1             ; wait for blanking
+        lda     VDC_DATA
+        jmp     @return
diff --git a/libsrc/c128/cpeekcolor.s b/libsrc/c128/cpeekcolor.s
new file mode 100644
index 000000000..9a8c3bfdc
--- /dev/null
+++ b/libsrc/c128/cpeekcolor.s
@@ -0,0 +1,56 @@
+;
+; 2016-02-28, Groepaz
+; 2017-06-26, Greg King
+;
+; unsigned char cpeekcolor (void);
+;
+
+        .export         _cpeekcolor
+
+        .include        "c128.inc"
+
+
+_cpeekcolor:
+        bit     MODE
+        bmi     @c80
+
+        ldy     CURS_X
+        lda     (CRAM_PTR),y    ; get color
+        and     #$0F
+        ldx     #>$0000
+        rts
+
+@c80:   lda     CRAM_PTR
+        ldy     CRAM_PTR+1
+        clc
+        adc     CURS_X
+        bcc     @s
+        iny
+
+        ; get byte from VDC mem
+@s:     ldx     #VDC_DATA_LO
+        stx     VDC_INDEX
+@L0:    bit     VDC_INDEX
+        bpl     @L0
+        sta     VDC_DATA
+        dex
+        stx     VDC_INDEX
+        sty     VDC_DATA
+
+        ldx     #VDC_RAM_RW
+        stx     VDC_INDEX
+@L1:    bit     VDC_INDEX
+        bpl     @L1             ; wait for blanking
+        lda     VDC_DATA
+        and     #$0F
+
+; translate VDC->VIC colour
+
+vdctovic:
+        ldy     #$0F + 1
+@L2:    dey
+        cmp     $CE5C,y
+        bne     @L2
+        tya
+        ldx     #>$0000
+        rts
diff --git a/libsrc/c128/cpeekrevers.s b/libsrc/c128/cpeekrevers.s
new file mode 100644
index 000000000..c36e4ac44
--- /dev/null
+++ b/libsrc/c128/cpeekrevers.s
@@ -0,0 +1,51 @@
+;
+; 2016-02-28, Groepaz
+; 2017-06-26, Greg King
+;
+; unsigned char cpeekrevers (void);
+;
+
+        .export         _cpeekrevers
+
+        .include        "zeropage.inc"
+        .include        "c128.inc"
+
+
+_cpeekrevers:
+        lda     MODE
+        bmi     @c80
+
+        ldy     CURS_X
+        lda     (SCREEN_PTR),y  ; get char
+
+@return:
+        and     #$80            ; get reverse flag
+        asl     a
+        tax                     ; ldx #>$0000
+        rol     a               ; return boolean value
+        rts
+
+@c80:
+        lda     SCREEN_PTR
+        ldy     SCREEN_PTR+1
+        clc
+        adc     CURS_X
+        bcc     @s
+        iny
+
+        ; get byte from VDC mem
+@s:     ldx     #VDC_DATA_LO
+        stx     VDC_INDEX
+@L0:    bit     VDC_INDEX
+        bpl     @L0
+        sta     VDC_DATA
+        dex
+        stx     VDC_INDEX
+        sty     VDC_DATA
+
+        ldx     #VDC_RAM_RW
+        stx     VDC_INDEX
+@L1:    bit     VDC_INDEX
+        bpl     @L1             ; wait for blanking
+        lda     VDC_DATA
+        jmp     @return
diff --git a/libsrc/c128/cpeeks.s b/libsrc/c128/cpeeks.s
new file mode 100644
index 000000000..c9d806ef3
--- /dev/null
+++ b/libsrc/c128/cpeeks.s
@@ -0,0 +1,15 @@
+;
+; 2017-11-23, Greg King
+;
+; void cpeeks (char* s, unsigned length);
+;
+; C128 can't use "cbm/cpeeks.s" because both 40 and 80 columns must be handled.
+; Stub file, for now, so that its library can be built.
+
+        .export         _cpeeks
+
+        .import         popax
+
+
+_cpeeks:
+        jmp     popax           ; pop s
diff --git a/libsrc/cbm/cpeekc.s b/libsrc/cbm/cpeekc.s
new file mode 100644
index 000000000..05c7fc718
--- /dev/null
+++ b/libsrc/cbm/cpeekc.s
@@ -0,0 +1,53 @@
+;
+; 2016-02-28, Groepaz
+; 2017-06-22, Greg King
+;
+; char cpeekc (void);
+;
+
+        .export         _cpeekc
+
+; Get a system-specific file.
+; Note:  The cbm610, and c128 targets need special
+; versions that handle RAM banking and the 80-column VDC.
+
+.if     .def(__C16__)
+        .include        "plus4.inc"     ; both C16 and Plus4
+.elseif .def(__C64__)
+        .include        "c64.inc"
+.elseif .def(__CBM510__)
+        .import         CURS_X: zp, SCREEN_PTR: zp
+        .include        "cbm510.inc"
+.elseif .def(__PET__)
+        .include        "pet.inc"
+.elseif .def(__VIC20__)
+        .include        "vic20.inc"
+.endif
+
+
+_cpeekc:
+        ldy     CURS_X
+        lda     (SCREEN_PTR),y  ; get screen code
+        ldx     #>$0000
+        and     #<~$80          ; remove reverse bit
+
+; Convert the screen code into a PetSCII code.
+; $00 - $1F: +$40
+; $20 - $3F
+; $40 - $5f: +$20
+; $60 - $7F: +$40
+
+        cmp     #$20
+        bcs     @sk1            ;(bge)
+        ora     #$40
+        rts
+
+@sk1:   cmp     #$40
+        bcc     @end            ;(blt)
+        cmp     #$60
+        bcc     @sk2            ;(blt)
+        ;sec
+        adc     #$20 - $01
+@sk2:   ;clc                    ; both above cmp and adc clear carry flag
+        adc     #$20
+@end:   rts
diff --git a/libsrc/cbm/cpeekcolor.s b/libsrc/cbm/cpeekcolor.s
new file mode 100644
index 000000000..9c961b771
--- /dev/null
+++ b/libsrc/cbm/cpeekcolor.s
@@ -0,0 +1,28 @@
+;
+; 2016-02-28, Groepaz
+; 2017-06-22, Greg King
+;
+; unsigned char cpeekcolor (void);
+;
+
+        .export         _cpeekcolor
+
+; Get a system-specific file.
+; Note:  The cbm510, cbm610, c128, and Pet targets need special
+; versions that handle RAM banking, the 80-column VDC, and monochrome.
+
+.if     .def(__C16__)
+        .include        "plus4.inc"     ; both C16 and Plus4
+.elseif .def(__C64__)
+        .include        "c64.inc"
+.elseif .def(__VIC20__)
+        .include        "vic20.inc"
+.endif
+
+
+_cpeekcolor:
+        ldy     CURS_X
+        lda     (CRAM_PTR),y    ; get color
+        and     #$0F
+        ldx     #>$0000
+        rts
diff --git a/libsrc/cbm/cpeekrevers.s b/libsrc/cbm/cpeekrevers.s
new file mode 100644
index 000000000..e8e210167
--- /dev/null
+++ b/libsrc/cbm/cpeekrevers.s
@@ -0,0 +1,35 @@
+;
+; 2016-02-28, Groepaz
+; 2017-06-15, Greg King
+;
+; unsigned char cpeekrevers (void);
+;
+
+        .export         _cpeekrevers
+
+; Get a system-specific file.
+; Note:  The cbm610, and c128 targets need special
+; versions that handle RAM banking and the 80-column VDC.
+
+.if     .def(__C16__)
+        .include        "plus4.inc"     ; both C16 and Plus4
+.elseif .def(__C64__)
+        .include        "c64.inc"
+.elseif .def(__CBM510__)
+        .import         CURS_X: zp, SCREEN_PTR: zp
+        .include        "cbm510.inc"
+.elseif .def(__PET__)
+        .include        "pet.inc"
+.elseif .def(__VIC20__)
+        .include        "vic20.inc"
+.endif
+
+
+_cpeekrevers:
+        ldy     CURS_X
+        lda     (SCREEN_PTR),y  ; get screen code
+        and     #$80            ; get reverse bit
+        asl     a
+        tax                     ; ldx #>$0000
+        rol     a               ; return boolean value
+        rts
diff --git a/libsrc/cbm/cpeeks.s b/libsrc/cbm/cpeeks.s
new file mode 100644
index 000000000..215998e37
--- /dev/null
+++ b/libsrc/cbm/cpeeks.s
@@ -0,0 +1,93 @@
+;
+; 2017-07-05, Greg King
+;
+; void cpeeks (char* s, unsigned length);
+;
+
+        .export         _cpeeks
+
+        .import         popax
+        .importzp       ptr1, ptr2, ptr3, tmp1, tmp2
+
+        .macpack        generic
+
+; Get a system-specific file.
+; Note:  The cbm610, and c128 targets need special
+; versions that handle RAM banking and the 80-column VDC.
+
+.if     .def(__C16__)
+        .include        "plus4.inc"     ; both C16 and Plus4
+.elseif .def(__C64__)
+        .include        "c64.inc"
+.elseif .def(__CBM510__)
+        .import         CURS_X: zp, SCREEN_PTR: zp
+        .include        "cbm510.inc"
+.elseif .def(__PET__)
+        .include        "pet.inc"
+.elseif .def(__VIC20__)
+        .include        "vic20.inc"
+.endif
+
+
+_cpeeks:
+        eor     #<$FFFF         ; counting a word upward is faster
+        sta     ptr3            ; so, we use -(length + 1)
+        txa
+        eor     #>$FFFF
+        sta     ptr3+1
+
+        lda     SCREEN_PTR
+        ldx     SCREEN_PTR+1
+        sta     ptr2
+        stx     ptr2+1
+        ldy     CURS_X
+        sty     tmp2
+
+        jsr     popax
+        sta     tmp1            ; (will be a .Y index)
+        stx     ptr1+1
+        ldx     #<$0000
+        stx     ptr1
+        bze     L3              ; branch always
+
+L4:     ldy     tmp2
+        lda     (ptr2),y        ; get char
+        iny
+        bnz     L2
+        inc     ptr2+1
+L2:     sty     tmp2
+        and     #<~$80          ; remove reverse bit
+
+; Convert the screen code into a PetSCII code.
+; $00 - $1F: +$40
+; $20 - $3F
+; $40 - $5f: +$20
+; $60 - $7F: +$40
+
+        cmp     #$20
+        blt     @sk1            ;(bcc)
+        cmp     #$40
+        blt     L5
+        cmp     #$60
+        blt     @sk2            ;(bcc)
+        clc
+@sk1:   adc     #$20
+@sk2:   ;clc                    ; both above cmp and adc clear carry flag
+        adc     #$20
+
+L5:     ldy     tmp1
+        sta     (ptr1),y
+        iny
+        bnz     L1
+        inc     ptr1+1
+L1:     sty     tmp1
+
+L3:     inc     ptr3            ; count length
+        bnz     L4
+        inc     ptr3+1
+        bnz     L4
+
+        txa                     ; terminate the string
+        ldy     tmp1
+        sta     (ptr1),y
+        rts
diff --git a/libsrc/cbm510/cpeekcolor.s b/libsrc/cbm510/cpeekcolor.s
new file mode 100644
index 000000000..44c0a1595
--- /dev/null
+++ b/libsrc/cbm510/cpeekcolor.s
@@ -0,0 +1,24 @@
+;
+; 2016-02-28, Groepaz
+; 2017-06-19, Greg King
+;
+; unsigned char cpeekcolor (void);
+;
+
+        .export         _cpeekcolor
+
+        .import         CURS_X: zp, CRAM_PTR: zp
+
+        .include        "cbm510.inc"
+
+
+_cpeekcolor:
+        ldx     IndReg
+        lda     #$0F
+        sta     IndReg
+        ldy     CURS_X
+        lda     (CRAM_PTR),y    ; get color
+        stx     IndReg
+        and     #$0F
+        ldx     #>$0000
+        rts
diff --git a/libsrc/cbm510/doesclrscr.s b/libsrc/cbm510/doesclrscr.s
new file mode 100644
index 000000000..e0f57374c
--- /dev/null
+++ b/libsrc/cbm510/doesclrscr.s
@@ -0,0 +1,16 @@
+;
+; 2016-06, Christian Groessler
+; 2017-07-05, Greg King
+;
+; unsigned char doesclrscrafterexit (void);
+;
+; Returns 0/1 if, after program termination, the screen isn't/is cleared.
+;
+
+        .import         return1
+
+; cc65's CBM510 programs switch to a display screen in the program RAM bank;
+; then, they switch back to the system bank when they exit.
+; The screen is cleared.
+
+        .export         _doesclrscrafterexit := return1
diff --git a/libsrc/cbm610/cpeekc.s b/libsrc/cbm610/cpeekc.s
new file mode 100644
index 000000000..295d096f3
--- /dev/null
+++ b/libsrc/cbm610/cpeekc.s
@@ -0,0 +1,45 @@
+;
+; 2016-02-28, Groepaz
+; 2017-06-19, Greg King
+;
+; char cpeekc (void);
+;
+
+        .export         _cpeekc
+
+        .import         CURS_X: zp, CharPtr: zp
+
+        .include        "cbm610.inc"
+
+
+_cpeekc:
+        ldx     IndReg
+        ldy     #$0F
+        sty     IndReg
+
+        ldy     CURS_X
+        lda     (CharPtr),y     ; get char from system bank
+        stx     IndReg
+        ldx     #>$0000
+        and     #<~$80          ; remove reverse bit
+
+; Convert the screen code into a PetSCII code.
+; $00 - $1F: +$40
+; $20 - $3F
+; $40 - $5f: +$20
+; $60 - $7F: +$40
+
+        cmp     #$20
+        bcs     @sk1            ;(bge)
+        ora     #$40
+        rts
+
+@sk1:   cmp     #$40
+        bcc     @end            ;(blt)
+        cmp     #$60
+        bcc     @sk2            ;(blt)
+        ;sec
+        adc     #$20 - $01
+@sk2:   ;clc                    ; both above cmp and adc clear carry flag
+        adc     #$20
+@end:   rts
diff --git a/libsrc/cbm610/cpeekcolor.s b/libsrc/cbm610/cpeekcolor.s
new file mode 100644
index 000000000..ed275ec95
--- /dev/null
+++ b/libsrc/cbm610/cpeekcolor.s
@@ -0,0 +1,8 @@
+;
+; 2017-06-03, Greg King
+;
+; unsigned char cpeekcolor (void);
+;
+
+        .import         return1
+        .export         _cpeekcolor := return1  ; always COLOR_WHITE
diff --git a/libsrc/cbm610/cpeekrevers.s b/libsrc/cbm610/cpeekrevers.s
new file mode 100644
index 000000000..52e166e85
--- /dev/null
+++ b/libsrc/cbm610/cpeekrevers.s
@@ -0,0 +1,29 @@
+;
+; 2016-02-28, Groepaz
+; 2017-06-19, Greg King
+;
+; unsigned char cpeekrevers (void);
+;
+
+        .export         _cpeekrevers
+
+        .import         plot
+        .import         CURS_X: zp, CharPtr: zp
+
+        .include        "cbm610.inc"
+
+
+_cpeekrevers:
+        ldx     IndReg
+        ldy     #$0F
+        sty     IndReg
+
+        ldy     CURS_X
+        lda     (CharPtr),y     ; get char from system bank
+        stx     IndReg
+        ldx     #>$0000
+        and     #$80            ; get reverse bit
+        asl     a
+        tax                     ; ldx #>$0000
+        rol     a               ; return boolean value
+        rts
diff --git a/libsrc/cbm610/cpeeks.s b/libsrc/cbm610/cpeeks.s
new file mode 100644
index 000000000..91aae8957
--- /dev/null
+++ b/libsrc/cbm610/cpeeks.s
@@ -0,0 +1,82 @@
+;
+; 2017-07-05, Greg King
+;
+; void cpeeks (char* s, unsigned length);
+;
+
+        .export         _cpeeks
+
+        .import         popax
+        .importzp       ptr1, ptr2, ptr3, tmp1, tmp2
+        .importzp       CURS_X, SCREEN_PTR
+
+        .include        "cbm610.inc"
+        .macpack        generic
+
+
+_cpeeks:
+        eor     #<$FFFF         ; counting a word upward is faster
+        sta     ptr3            ; so, we use -(length + 1)
+        txa
+        eor     #>$FFFF
+        sta     ptr3+1
+
+        lda     SCREEN_PTR
+        ldx     SCREEN_PTR+1
+        sta     ptr2
+        stx     ptr2+1
+        ldy     CURS_X
+        sty     tmp2
+
+        jsr     popax
+        sta     tmp1            ; (will be a .Y index)
+        stx     ptr1+1
+        ldx     IndReg
+        ldy     #<$0000
+        sty     ptr1
+        bze     L3              ; branch always
+
+L4:     ldy     #$0F
+        sty     IndReg
+        ldy     tmp2
+        lda     (ptr2),y        ; get char from system bank
+        stx     IndReg
+        iny
+        bnz     L2
+        inc     ptr2+1
+L2:     sty     tmp2
+        and     #<~$80          ; remove reverse bit
+
+; Convert the screen code into a PetSCII code.
+; $00 - $1F: +$40
+; $20 - $3F
+; $40 - $5f: +$20
+; $60 - $7F: +$40
+
+        cmp     #$20
+        blt     @sk1            ;(bcc)
+        cmp     #$40
+        blt     L5
+        cmp     #$60
+        blt     @sk2            ;(bcc)
+        clc
+@sk1:   adc     #$20
+@sk2:   ;clc                    ; both above cmp and adc clear carry flag
+        adc     #$20
+
+L5:     ldy     tmp1
+        sta     (ptr1),y
+        iny
+        bnz     L1
+        inc     ptr1+1
+L1:     sty     tmp1
+
+L3:     inc     ptr3            ; count length
+        bnz     L4
+        inc     ptr3+1
+        bnz     L4
+
+        lda     #$00            ; terminate the string
+        ldy     tmp1
+        sta     (ptr1),y
+        rts
diff --git a/libsrc/pet/cpeekcolor.s b/libsrc/pet/cpeekcolor.s
new file mode 100644
index 000000000..ed275ec95
--- /dev/null
+++ b/libsrc/pet/cpeekcolor.s
@@ -0,0 +1,8 @@
+;
+; 2017-06-03, Greg King
+;
+; unsigned char cpeekcolor (void);
+;
+
+        .import         return1
+        .export         _cpeekcolor := return1  ; always COLOR_WHITE
diff --git a/testcode/lib/cpeek-test.c b/testcode/lib/cpeek-test.c
new file mode 100644
index 000000000..27923432b
--- /dev/null
+++ b/testcode/lib/cpeek-test.c
@@ -0,0 +1,177 @@
+/* Test that the cpeek...() functions are the inverses of cputc(),
+** revers(), and textcolor() for the full range of character codes.
+**
+** 2017-07-15, Greg King
+*/
+
+#include <conio.h>
+#include <cc65.h>
+
+/* Standard location of the screen */
+
+#if defined(__C128__) || defined(__C64__)
+/* only 40-column screen */
+#  define SCREEN_RAM ((unsigned char*)0x0400)
+#elif defined(__C16__)                  /* Plus4 also */
+#  define SCREEN_RAM ((unsigned char*)0x0C00)
+#elif defined(__CBM510__)
+#  define SCREEN_RAM ((unsigned char*)0xF000)
+#elif defined(__CBM610__)
+#  define SCREEN_RAM ((unsigned char*)0xD000)
+#elif defined(__PET__)
+#  define SCREEN_RAM ((unsigned char*)0x0800)
+#elif defined(__VIC20__)
+#  define SCREEN_RAM ((unsigned char*)0x1000)
+#else
+#  error This program cannot test that target.
+#  define SCREEN_RAM ((unsigned char*)0)
+#endif
+
+static unsigned char width;
+
+
+/* Move the cursor backward one char with
+** the recognition of a row change.
+*/
+static void chBack (void)
+{
+    unsigned char y = wherey ();
+    unsigned char x = wherex ();
+
+    if (x == 0) {
+        x = width;
+        --y;
+    }
+    --x;
+
+    gotoxy (x, y);
+}
+
+
+/* Move the cursor forward one char with
+** the recognition of a row change.
+*/
+static void chForth (void)
+{
+    unsigned char y = wherey ();
+    unsigned char x = wherex ();
+
+    if (++x >= width) {
+        x = 0;
+        ++y;
+    }
+
+    gotoxy (x, y);
+}
+
+
+/* A hack to get an unmodified byte from the
+** screen memory at the current cursor position.
+*/
+static unsigned char peekChWithoutTranslation (void)
+{
+#if defined(__CBM610__)
+    return peekbsys ((unsigned)&SCREEN_RAM[wherey () * width + wherex ()]);
+#else
+    return SCREEN_RAM[wherey () * width + wherex ()];
+#endif
+}
+
+
+/* A test which outputs the given char, reads it back from
+** screen memory, outputs the returned char at the next position,
+** then compares the two screen memory bytes for identity.
+**
+** Note:  cpeekc() must be tested indirectly because some platforms "fold" their
+** character code-set into a smaller screen code-set.  Therefore, cpeekc() might
+** return an equivalent, but not equal, character to the one that was cputc().
+*/
+static unsigned char testCPeekC (char ch)
+{
+    unsigned char ch2_a, ch2_b, ch2_c;
+
+    /* Test the given char-code, but not the
+    ** special characters NEWLINE and RETURN
+    ** (they don't put anything on the screen).
+    */
+    if (('\n' == ch) || ('\r' == ch)
+        ) {
+        return 1;
+    }
+
+    /* Output the char to the screen. */
+    cputc (ch);
+
+    /* Move the cursor pos. to the previous output. */
+    chBack ();
+
+    /* Get back the written char without any translation. */
+    ch2_b = peekChWithoutTranslation ();
+
+    /* Get back the written char,
+    ** including the translation, screen-code -> text.
+    */
+    ch2_a = cpeekc ();
+
+    /* Move the cursor to the following writing position. */
+    chForth ();
+
+    /* Output again the char which was read back by cpeekc(). */
+    cputc (ch2_a);
+
+    /* Move the cursor pos. to the second output. */
+    chBack ();
+
+    /* Get back the second written char without any translation;
+    ** and, compare it to the first untranslated char.
+    */
+    ch2_c = peekChWithoutTranslation ();
+    if (ch2_c != ch2_b) {
+        /* The test was NOT succesful.
+        ** Output a diagnostic; and, return FAILURE.
+        */
+        revers(0);
+        cprintf ("\r\nError on char: %#x was %#x instead.", ch, ch2_a);
+        cprintf ("\r\nRaw screen codes: %#x, %#x.", ch2_b, ch2_c);
+        return 0;
+    }
+
+    /* The test was succesful.
+    ** Move the cursor to the following writing position.
+    */
+    chForth ();
+    return 1;
+}
+
+
+/* The main code initiates the screen for the tests, and sets the reverse flag.
+** Then, it calls testCPeekC() for every char within 0..255.
+** Returns zero for success, one for failure.
+*/
+int main (void)
+{
+    unsigned char i;
+    int ret = 0;
+
+    clrscr ();
+    revers (1);
+    screensize (&width, &i);
+
+#if defined(__VIC20__)
+    /* The VIC-20's screen is too small to hold the full test. */
+    i = 2;
+#else
+    i = 0;
+#endif
+    do {
+        if (!testCPeekC (i)) {
+            ret = 1;
+            break;
+        }
+    } while (++i != 0);         /* will wrap around when finished */
+
+    if (doesclrscrafterexit()) {
+        cgetc();
+    }
+    return ret;
+}

From a54b4bebde59962594734e89c9765b4dd3d4eb74 Mon Sep 17 00:00:00 2001
From: mrdudz <mrdudz@users.noreply.github.com>
Date: Mon, 11 Dec 2017 21:15:29 +0100
Subject: [PATCH 2/8] PET screen memory is at $8000, not $0800

---
 testcode/lib/cpeek-test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/testcode/lib/cpeek-test.c b/testcode/lib/cpeek-test.c
index 27923432b..86bdc74ee 100644
--- a/testcode/lib/cpeek-test.c
+++ b/testcode/lib/cpeek-test.c
@@ -19,7 +19,7 @@
 #elif defined(__CBM610__)
 #  define SCREEN_RAM ((unsigned char*)0xD000)
 #elif defined(__PET__)
-#  define SCREEN_RAM ((unsigned char*)0x0800)
+#  define SCREEN_RAM ((unsigned char*)0x8000)
 #elif defined(__VIC20__)
 #  define SCREEN_RAM ((unsigned char*)0x1000)
 #else

From 9a609f6766f62ff0b609c6174bbaf24b8e2b8eb8 Mon Sep 17 00:00:00 2001
From: mrdudz <mrdudz@users.noreply.github.com>
Date: Mon, 11 Dec 2017 21:53:00 +0100
Subject: [PATCH 3/8] fix petscii conversion for C128

---
 libsrc/c128/cpeekc.s | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/libsrc/c128/cpeekc.s b/libsrc/c128/cpeekc.s
index ce6bb4ff5..9d64388a4 100644
--- a/libsrc/c128/cpeekc.s
+++ b/libsrc/c128/cpeekc.s
@@ -26,7 +26,8 @@ _cpeekc:
 ; Convert the screen code into a PetSCII code.
 ; $00 - $1F: +$40
 ; $20 - $3F
-; $40 - $7F: +$80
+; $40 - $5f: +$20
+; $60 - $7F: +$40
 
         cmp     #$20
         bcs     @sk1            ;(bge)
@@ -35,9 +36,13 @@ _cpeekc:
 
 @sk1:   cmp     #$40
         bcc     @end            ;(blt)
-        ora     #$80
-@end:   ldx     #0
-        rts
+        cmp     #$60
+        bcc     @sk2            ;(blt)
+        ;sec
+        adc     #$20 - $01
+@sk2:   ;clc                    ; both above cmp and adc clear carry flag
+        adc     #$20
+@end:   rts
 
 @c80:
         lda     SCREEN_PTR

From 74b5540f76c8e8ab09c219e871e7cae826d59731 Mon Sep 17 00:00:00 2001
From: mrdudz <mrdudz@users.noreply.github.com>
Date: Tue, 12 Dec 2017 03:48:43 +0100
Subject: [PATCH 4/8] copy cpeeks from generic version, this at least makes it
 work for 40 columns

---
 libsrc/c128/cpeeks.s | 72 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 68 insertions(+), 4 deletions(-)

diff --git a/libsrc/c128/cpeeks.s b/libsrc/c128/cpeeks.s
index c9d806ef3..e0723e694 100644
--- a/libsrc/c128/cpeeks.s
+++ b/libsrc/c128/cpeeks.s
@@ -1,15 +1,79 @@
 ;
-; 2017-11-23, Greg King
+; 2017-07-05, Greg King
 ;
 ; void cpeeks (char* s, unsigned length);
 ;
-; C128 can't use "cbm/cpeeks.s" because both 40 and 80 columns must be handled.
-; Stub file, for now, so that its library can be built.
 
         .export         _cpeeks
 
         .import         popax
+        .importzp       ptr1, ptr2, ptr3, tmp1, tmp2
 
+        .macpack        generic
+
+; FIXME c128 needs special version that handles the 80-column VDC.
+
+        .include        "c128.inc"
 
 _cpeeks:
-        jmp     popax           ; pop s
+        eor     #<$FFFF         ; counting a word upward is faster
+        sta     ptr3            ; so, we use -(length + 1)
+        txa
+        eor     #>$FFFF
+        sta     ptr3+1
+
+        lda     SCREEN_PTR
+        ldx     SCREEN_PTR+1
+        sta     ptr2
+        stx     ptr2+1
+        ldy     CURS_X
+        sty     tmp2
+
+        jsr     popax
+        sta     tmp1            ; (will be a .Y index)
+        stx     ptr1+1
+        ldx     #<$0000
+        stx     ptr1
+        bze     L3              ; branch always
+
+L4:     ldy     tmp2
+        lda     (ptr2),y        ; get char
+        iny
+        bnz     L2
+        inc     ptr2+1
+L2:     sty     tmp2
+        and     #<~$80          ; remove reverse bit
+
+; Convert the screen code into a PetSCII code.
+; $00 - $1F: +$40
+; $20 - $3F
+; $40 - $5f: +$20
+; $60 - $7F: +$40
+
+        cmp     #$20
+        blt     @sk1            ;(bcc)
+        cmp     #$40
+        blt     L5
+        cmp     #$60
+        blt     @sk2            ;(bcc)
+        clc
+@sk1:   adc     #$20
+@sk2:   ;clc                    ; both above cmp and adc clear carry flag
+        adc     #$20
+
+L5:     ldy     tmp1
+        sta     (ptr1),y
+        iny
+        bnz     L1
+        inc     ptr1+1
+L1:     sty     tmp1
+
+L3:     inc     ptr3            ; count length
+        bnz     L4
+        inc     ptr3+1
+        bnz     L4
+
+        txa                     ; terminate the string
+        ldy     tmp1
+        sta     (ptr1),y
+        rts

From 4d95e578f45d3ab0df41237ab67c4eab04f7e345 Mon Sep 17 00:00:00 2001
From: mrdudz <mrdudz@users.noreply.github.com>
Date: Tue, 12 Dec 2017 03:49:07 +0100
Subject: [PATCH 5/8] added missing tests

---
 testcode/lib/cpeek-test.c | 99 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 97 insertions(+), 2 deletions(-)

diff --git a/testcode/lib/cpeek-test.c b/testcode/lib/cpeek-test.c
index 86bdc74ee..d02972202 100644
--- a/testcode/lib/cpeek-test.c
+++ b/testcode/lib/cpeek-test.c
@@ -16,6 +16,7 @@
 #  define SCREEN_RAM ((unsigned char*)0x0C00)
 #elif defined(__CBM510__)
 #  define SCREEN_RAM ((unsigned char*)0xF000)
+#  define COLOR_RAM ((unsigned char*)0xd400)
 #elif defined(__CBM610__)
 #  define SCREEN_RAM ((unsigned char*)0xD000)
 #elif defined(__PET__)
@@ -77,6 +78,18 @@ static unsigned char peekChWithoutTranslation (void)
 #endif
 }
 
+/* as above, but for color ram */
+static unsigned char peekColWithoutTranslation (void)
+{
+#if defined(__CBM610__) || defined (__PET__)
+    return COLOR_WHITE;
+#elif defined(__C128__) || defined(__C64__) || defined(__VIC20__) || defined(__CBM510__)
+    return COLOR_RAM[wherey () * width + wherex ()] & 0x0f;
+#else
+    return COLOR_RAM[wherey () * width + wherex ()];
+#endif
+}
+
 
 /* A test which outputs the given char, reads it back from
 ** screen memory, outputs the returned char at the next position,
@@ -133,6 +146,7 @@ static unsigned char testCPeekC (char ch)
         revers(0);
         cprintf ("\r\nError on char: %#x was %#x instead.", ch, ch2_a);
         cprintf ("\r\nRaw screen codes: %#x, %#x.", ch2_b, ch2_c);
+        cprintf ("\r\nscreen width: %#d", width);
         return 0;
     }
 
@@ -143,18 +157,70 @@ static unsigned char testCPeekC (char ch)
     return 1;
 }
 
+static unsigned char testCPeekCol (char ch)
+{
+    unsigned char ch2_a, ch2_b, ch2_c;
+
+    /* Output the char to the screen. */
+    textcolor (ch);
+    cputc ('*');
+
+    /* Move the cursor pos. to the previous output. */
+    chBack ();
+
+    /* Get back the written char without any translation. */
+    ch2_b = peekColWithoutTranslation ();
+
+    /* Get back the written char,
+    ** including the translation, screen-code -> text.
+    */
+    ch2_a = cpeekcolor ();
+
+    /* Move the cursor to the following writing position. */
+    chForth ();
+
+    /* Output again the char which was read back by cpeekc(). */
+    textcolor (ch2_a);
+    cputc ('x');
+
+    /* Move the cursor pos. to the second output. */
+    chBack ();
+
+    /* Get back the second written char without any translation;
+    ** and, compare it to the first untranslated char.
+    */
+    ch2_c = peekColWithoutTranslation ();
+    if (ch2_c != ch2_b) {
+        /* The test was NOT succesful.
+        ** Output a diagnostic; and, return FAILURE.
+        */
+        revers(0);
+        cprintf ("\r\nError on color: %#x was %#x instead.", ch, ch2_a);
+        cprintf ("\r\nRaw color codes: %#x, %#x.", ch2_b, ch2_c);
+        return 0;
+    }
+
+    /* The test was succesful.
+    ** Move the cursor to the following writing position.
+    */
+    chForth ();
+    return 1;
+}
 
 /* The main code initiates the screen for the tests, and sets the reverse flag.
 ** Then, it calls testCPeekC() for every char within 0..255.
+** Then, it calls testCPeekCol() for each color
 ** Returns zero for success, one for failure.
 */
 int main (void)
 {
-    unsigned char i;
+    unsigned char i, c1, c2;
+    char s[10];
     int ret = 0;
 
     clrscr ();
     revers (1);
+    textcolor(1);
     screensize (&width, &i);
 
 #if defined(__VIC20__)
@@ -166,10 +232,39 @@ int main (void)
     do {
         if (!testCPeekC (i)) {
             ret = 1;
-            break;
+            goto exiterror;
         }
     } while (++i != 0);         /* will wrap around when finished */
 
+    /* test colors */
+    clrscr ();
+    revers (0);
+#if defined (__CBM610__) || defined (__PET__)
+    cprintf("no COLOR_RAM\n\r");
+#else
+    cprintf("COLOR_RAM at $%04x\n\r", COLOR_RAM);
+#endif
+    do {
+        if (!testCPeekCol (i)) {
+            ret = 1;
+            goto exiterror;
+        }
+    } while (++i != 16);        /* max 16 colors */
+
+    /* test revers */
+    textcolor(1); cputc('\n'); cputc('\r');
+    revers(0); cputc('*'); chBack (); c1 = cpeekrevers(); chForth();
+    revers(1); cputc('*'); chBack (); c2 = cpeekrevers(); chForth();
+    cputc('\n'); cputc('\r');
+    revers(c1); cputc('*'); 
+    revers(c2); cputc('*'); 
+
+    /* test cpeeks() */
+    revers(0);
+    cprintf("\n\rtest1234"); gotox(0); cpeeks(s, 8); cputs("\n");
+    cputs(s);
+
+exiterror:
     if (doesclrscrafterexit()) {
         cgetc();
     }

From 4da22fc6a095258e8ee16a6659ca4a90585c18f1 Mon Sep 17 00:00:00 2001
From: mrdudz <mrdudz@users.noreply.github.com>
Date: Tue, 12 Dec 2017 04:32:39 +0100
Subject: [PATCH 6/8] fix cpeeks for CBM610

---
 libsrc/cbm610/cpeeks.s | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libsrc/cbm610/cpeeks.s b/libsrc/cbm610/cpeeks.s
index 91aae8957..352521bcc 100644
--- a/libsrc/cbm610/cpeeks.s
+++ b/libsrc/cbm610/cpeeks.s
@@ -8,7 +8,7 @@
 
         .import         popax
         .importzp       ptr1, ptr2, ptr3, tmp1, tmp2
-        .importzp       CURS_X, SCREEN_PTR
+        .importzp       CURS_X, CharPtr
 
         .include        "cbm610.inc"
         .macpack        generic
@@ -21,8 +21,8 @@ _cpeeks:
         eor     #>$FFFF
         sta     ptr3+1
 
-        lda     SCREEN_PTR
-        ldx     SCREEN_PTR+1
+        lda     CharPtr
+        ldx     CharPtr+1
         sta     ptr2
         stx     ptr2+1
         ldy     CURS_X

From 892732f3403e187026d518bdeb690de66408b8eb Mon Sep 17 00:00:00 2001
From: mrdudz <mrdudz@users.noreply.github.com>
Date: Tue, 12 Dec 2017 17:05:24 +0100
Subject: [PATCH 7/8] fix cpeeks() for C128 VDC

---
 libsrc/c128/cpeeks.s | 80 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)

diff --git a/libsrc/c128/cpeeks.s b/libsrc/c128/cpeeks.s
index e0723e694..12c3349e4 100644
--- a/libsrc/c128/cpeeks.s
+++ b/libsrc/c128/cpeeks.s
@@ -1,5 +1,6 @@
 ;
 ; 2017-07-05, Greg King
+; 2017-12-12, Groepaz
 ;
 ; void cpeeks (char* s, unsigned length);
 ;
@@ -22,6 +23,9 @@ _cpeeks:
         eor     #>$FFFF
         sta     ptr3+1
 
+        lda     MODE
+        bmi     c80
+
         lda     SCREEN_PTR
         ldx     SCREEN_PTR+1
         sta     ptr2
@@ -77,3 +81,79 @@ L3:     inc     ptr3            ; count length
         ldy     tmp1
         sta     (ptr1),y
         rts
+
+        ;-----------------------------------------------------------
+c80:
+        lda     SCREEN_PTR
+        clc
+        adc     CURS_X
+        sta     ptr2
+        lda     SCREEN_PTR+1
+        adc     #0
+        sta     ptr2+1
+
+        jsr     popax
+        sta     tmp1            ; (will be a .Y index)
+        stx     ptr1+1
+        ldx     #<$0000
+        stx     ptr1
+        bze     L3a              ; branch always
+
+L4a:
+        lda     ptr2
+        ldy     ptr2+1
+        inc     ptr2
+        bne     @s
+        inc     ptr2+1
+@s:
+        ; get byte from VDC mem
+        ldx     #VDC_DATA_LO
+        stx     VDC_INDEX
+@L0:    bit     VDC_INDEX
+        bpl     @L0
+        sta     VDC_DATA
+        dex
+        stx     VDC_INDEX
+        sty     VDC_DATA
+
+        ldx     #VDC_RAM_RW
+        stx     VDC_INDEX
+@L1:    bit     VDC_INDEX
+        bpl     @L1             ; wait for blanking
+        lda     VDC_DATA
+
+        and     #<~$80          ; remove reverse bit
+
+; Convert the screen code into a PetSCII code.
+; $00 - $1F: +$40
+; $20 - $3F
+; $40 - $5f: +$20
+; $60 - $7F: +$40
+
+        cmp     #$20
+        blt     @sk1            ;(bcc)
+        cmp     #$40
+        blt     L5a
+        cmp     #$60
+        blt     @sk2            ;(bcc)
+        clc
+@sk1:   adc     #$20
+@sk2:   ;clc                    ; both above cmp and adc clear carry flag
+        adc     #$20
+
+L5a:    ldy     tmp1
+        sta     (ptr1),y
+        iny
+        bnz     L1a
+        inc     ptr1+1
+L1a:    sty     tmp1
+
+L3a:    inc     ptr3            ; count length
+        bnz     L4a
+        inc     ptr3+1
+        bnz     L4a
+
+        lda     #0              ; terminate the string
+        ldy     tmp1
+        sta     (ptr1),y
+        rts

From 2957acd133c6eb7436a984d704575b1c2b77d0f3 Mon Sep 17 00:00:00 2001
From: mrdudz <mrdudz@users.noreply.github.com>
Date: Tue, 12 Dec 2017 17:05:52 +0100
Subject: [PATCH 8/8] some tweaks to testprog

---
 testcode/lib/cpeek-test.c | 53 +++++++++++++++++++++++++++++----------
 1 file changed, 40 insertions(+), 13 deletions(-)

diff --git a/testcode/lib/cpeek-test.c b/testcode/lib/cpeek-test.c
index d02972202..5f3bfc524 100644
--- a/testcode/lib/cpeek-test.c
+++ b/testcode/lib/cpeek-test.c
@@ -2,6 +2,7 @@
 ** revers(), and textcolor() for the full range of character codes.
 **
 ** 2017-07-15, Greg King
+** 2017-12-12, Groepaz
 */
 
 #include <conio.h>
@@ -139,7 +140,12 @@ static unsigned char testCPeekC (char ch)
     ** and, compare it to the first untranslated char.
     */
     ch2_c = peekChWithoutTranslation ();
-    if (ch2_c != ch2_b) {
+    if ((ch2_c != ch2_b)
+#if defined(__C128__)
+        /* VDC memory is not accessable */
+        && (width == 40)
+#endif
+        ){
         /* The test was NOT succesful.
         ** Output a diagnostic; and, return FAILURE.
         */
@@ -190,7 +196,12 @@ static unsigned char testCPeekCol (char ch)
     ** and, compare it to the first untranslated char.
     */
     ch2_c = peekColWithoutTranslation ();
-    if (ch2_c != ch2_b) {
+    if ((ch2_c != ch2_b)
+#if defined(__C128__)
+        /* VDC memory is not accessable */
+        && (width == 40)
+#endif
+        ){
         /* The test was NOT succesful.
         ** Output a diagnostic; and, return FAILURE.
         */
@@ -221,6 +232,7 @@ int main (void)
     clrscr ();
     revers (1);
     textcolor(1);
+    bgcolor(0);
     screensize (&width, &i);
 
 #if defined(__VIC20__)
@@ -236,14 +248,29 @@ int main (void)
         }
     } while (++i != 0);         /* will wrap around when finished */
 
-    /* test colors */
-    clrscr ();
-    revers (0);
-#if defined (__CBM610__) || defined (__PET__)
-    cprintf("no COLOR_RAM\n\r");
-#else
-    cprintf("COLOR_RAM at $%04x\n\r", COLOR_RAM);
+#if defined(__VIC20__)
+    cgetc();
 #endif
+
+    /* test colors */
+#if defined(__VIC20__)
+    clrscr ();
+#endif
+    revers (0);
+    textcolor(1);
+
+#if defined (__CBM610__) || defined (__PET__)
+    cprintf("\n\rno COLOR_RAM\n\r");
+#elif defined (__C128__)
+    if (width == 40) {
+        cprintf("\n\rCOLOR_RAM at $%04x\n\r", COLOR_RAM);
+    } else {
+        cprintf("\n\rno COLOR_RAM\n\r");
+    }
+#else
+    cprintf("\n\rCOLOR_RAM at $%04x\n\r", COLOR_RAM);
+#endif
+
     do {
         if (!testCPeekCol (i)) {
             ret = 1;
@@ -253,11 +280,11 @@ int main (void)
 
     /* test revers */
     textcolor(1); cputc('\n'); cputc('\r');
-    revers(0); cputc('*'); chBack (); c1 = cpeekrevers(); chForth();
-    revers(1); cputc('*'); chBack (); c2 = cpeekrevers(); chForth();
+    revers(0); cputc('x'); chBack (); c1 = cpeekrevers(); chForth();
+    revers(1); cputc('X'); chBack (); c2 = cpeekrevers(); chForth();
     cputc('\n'); cputc('\r');
-    revers(c1); cputc('*'); 
-    revers(c2); cputc('*'); 
+    revers(c1); cputc('o'); 
+    revers(c2); cputc('O'); 
 
     /* test cpeeks() */
     revers(0);